通常解封装时,当调用avformat_open_input和avformat_find_stream_info时,FFmpeg内部会自动读取文件内容来查找信息。本文要做的是,自定义IO,提供一个回调函数。这样的话,当调用前面这两个函数时,通过回调函数来提供数据。
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libavutil/file.h>
struct buffer_data {
uint8_t *ptr;
size_t size; ///< size left in the buffer
};
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
{
struct buffer_data *bd = (struct buffer_data *)opaque;
buf_size = FFMIN(buf_size, bd->size);
if (!buf_size)
return AVERROR_EOF;
printf("ptr:%p size:%zu\n", bd->ptr, bd->size);
/* copy internal buffer data to buf */
memcpy(buf, bd->ptr, buf_size);
bd->ptr += buf_size;
bd->size -= buf_size;
return buf_size;
}
static void avio_reading(void)
{
const char *input_filename = "/Users/zhw/Desktop/resource/sintel_h264_aac.flv";
AVFormatContext *f