目录
1、AVIO内存输⼊模式的作用
AVIO内存输入模式是FFmpeg中的一个重要功能,它主要用于实现将数据从内存中直接读取到缓冲区中,以便进行后续的音视频处理或解码等操作。以下是AVIO内存输入模式的主要作用:
1. 提高音视频处理的灵活性
- 直接内存访问:传统的音视频处理方式往往需要将音视频数据保存到文件中,然后再进行读取和处理。而使用AVIO内存输入模式,可以直接从内存中读取数据,无需经过文件系统的中间环节,从而提高了音视频处理的灵活性。
- 减少磁盘IO:避免了繁琐的文件读写操作,减少了磁盘IO的消耗,提高了处理效率。
2. 节省磁盘空间
- 内存作为数据源:在内存输入模式下,音视频数据被存储在内存中,而不是磁盘上。这对于处理大量临时数据或需要频繁读写操作的场景尤为有用,因为它可以显著减少磁盘空间的占用。
3. 支持高级应用场景
- 流媒体处理:内存输入模式可以方便地扩展为流媒体服务器等高级应用。流媒体服务器需要实时地从内存中获取音视频数据进行传输,而AVIO内存输入模式正好满足了这一需求。
- 实时音视频采集与处理:在实时音视频采集与处理系统中,数据通常首先被捕获到内存中,然后通过AVIO内存输入模式进行读取和处理,以实现低延迟的音视频处理效果。
4. 自定义功能实现
- 回调函数:FFmpeg的avio模块提供了一系列API,允许用户通过设置回调函数来实现各种自定义功能。例如,可以自定义网络协议传输方式、增加错误重试机制、实现多路复用等。这使得处理器可以根据自己的需求对avio模块进行灵活配置,以满足不同场景下的业务需求。
5. 简化处理流程
- 直接读取内存数据:在AVIO内存输入模式下,处理器可以直接从内存中读取音视频数据进行处理,无需先将数据写入文件再读取,从而简化了处理流程。
2、实现
.pro
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.c
mac {
INCLUDEPATH += "/Users/wangqi/ffmpeg/include"
LIBS += -L/Users/wangqi/ffmpeg/lib -lavformat -lavcodec -lavdevice -lavfilter -lavutil -lswresample -lswscale
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libavutil/frame.h>
#include <libavutil/mem.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#define BUFF_SIZE 20480
static char* av_get_err(int errnum)
{
static char err_buf[128] = {0};
av_strerror(errnum, err_buf, 128);
return err_buf;
}
static void print_sample_format(const AVFrame *frame)
{
printf("ar-samplerate: %uhz\n", frame->sample_rate);
printf("ac-channels: %u\n", frame->channels);
printf("f-formats: %u\n", frame->format);
}
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
FILE *in_file = (FILE*)opaque;
int read_size = fread(buf, 1, buf_size, in_file);
printf("read packet read size: %d, buf size: %d\n", read_size, buf_size);
if (read_size <= 0)
{
return AVERROR_EOF;
}
return read_size;
}
static void decode(AVCodecContext *dec_ctx, AVPacket *packet, AVFrame *frame, FILE *out_file)
{
int ret = 0;
ret = avcodec_send_packet(dec_ctx, packet);
if (ret == AVERROR(EAGAIN))
{
printf("Recieve_frame and send_packet both returned eagain, which is an API violation.\n");
}
else if (ret < 0)
{
printf("Error submitting the packet to the decode, err: %s\n", av_get_err(ret));
return;
}
while (ret >= 0)
{
ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
return;
}
else if (ret < 0)
{
printf("Error during decoding\n");
exit(1);
}
if (!packet)
{
printf("get flash frame\n");
}
int data_size = av_get_bits_per_sample(dec_ctx->sample_fmt);
for (int i = 0; i < frame->nb_samples; i++) {
for (int ch = 0; ch < dec_ctx->channels; ch++) {
fwrite(frame->data[ch] + data_size * i, 1, data_size, out_file);
}
}
}
}
int main(int argc, char **argv)
{
if (argc != 3)
{
printf("usage: %s <input file> <output file>\n", argv[0]);
return -1;
}
const char *in_file_name = argv[1];
const char *out_file_name = argv[2];
FILE *in_file = NULL;
FILE *out_file = NULL;
// 1、打开参数文件
in_file = fopen(in_file_name, "rb");
if (!in_file) {
printf("open %s failed\n", in_file_name);
return -1;
}
out_file = fopen(out_file_name, "wb");
if (!out_file) {
printf("open %s failed\n", out_file_name);
return -1;
}
//2、自定义io
uint8_t *io_buffer = av_malloc(BUFF_SIZE);
AVIOContext *avio_ctx = avio_alloc_context(io_buffer, BUFF_SIZE, 0, (void *)in_file, \
read_packet, NULL, NULL);
AVFormatContext *format_ctx = avformat_alloc_context();
format_ctx->pb = avio_ctx;
int ret = avformat_open_input(&format_ctx, NULL, NULL, NULL);
if (ret < 0)
{
printf("avformat_open_input failed: %s\n", av_err2str(ret));
return -1;
}
AVCodec *codec = avcodec_find_decoder(AV_CODEC_ID_AAC);
if (!codec)
{
printf("avcodec_find_decoder failed\n");
return -1;
}
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx)
{
printf("avcodec_alloc_context3 failed\n");
return -1;
}
ret = avcodec_open2(codec_ctx, codec, NULL);
if (ret < 0)
{
printf("avcodec_open2 failed: %s\n", av_err2str(ret));
return -1;
}
AVPacket *packet = av_packet_alloc();
AVFrame *frame = av_frame_alloc();
while (1)
{
ret = av_read_frame(format_ctx, packet);
if (ret < 0)
{
printf("av_read_frame failed: %s\n", av_err2str(ret));
break;
}
decode(codec_ctx, packet, frame, out_file);
}
printf("main read finishi\n");
decode(codec_ctx, NULL, frame, out_file);
fclose(in_file);
fclose(out_file);
av_free(io_buffer);
av_frame_free(frame);
av_packet_free(packet);
avformat_close_input(&format_ctx);
avformat_free_context(&codec_ctx);
printf("main finish!\n");
return 0;
}