一、avio_read函数调用栈
av_read_frame
->read_frame_internal
->ff_read_packet
->s->iformat->read_packet(以ff_yuv4mpegpipe_demuxer为例)
->av_get_packet
->append_packet_chunked
->avio_read
ffmpeg要先缓存本地的原因是,在avformat_find_stream_info会调用try_decode_frame进行试解码,只是用前面一小段数据进行解码。后续正式转码时,这段数据会重新使用。这里缓存到本地,减少读取IO次数。
二、avio_alloc_context参数含义
AVIOContext *avio_alloc_context(
unsigned char *buffer,
int buffer_size,
int write_flag,
void *opaque,
int (*read_packet)(void *opaque, uint8_t *buf, int buf_size),
int (*write_packet)(void *opaque, uint8_t *buf, int buf_size),
int64_t (*seek)(void *opaque, int64_t offset, int whence))
{
AVIOContext *s = av_malloc(sizeof(AVIOContext));
if (!s)
return NULL;
ffio_init_context(s, buffer, buffer_size, write_flag, opaque,
read_packet, write_packet, seek);
return s;
}
unsigned char *buffer:本地缓存buffer。缓存试解码时读到的数据。
int buffer_size:缓存buffer的大小。
void *opaque:私有指针,可以传入类似this指针,在read_packet、write_packet、seek进行引用。
read_packet:钩子函数,在AVInputFormat解析时,读入数据。
write_packet:钩子函数,在AVOutputFormat封装数据时,写输出文件。
avio.h文件对AVIOContext结构体中的buffer、buffer_size、buf_ptr、buf_end图解