AVIOContext结构体位于libavformat/avio.h下:
是字节流IO上下文, AVIOContext不能直接被函数指针调用,应当在应用程序实现自定义IO时,通常是通过avio_alloc_conext()函数进行设置函数指针。
AVIOContext的成员变量:typedef struct AVIOContext { // 一个私有类选项 // 如果AVIOContext被创建通过avio_open2()函数,av_class可以通过设置的协议选项设置 // 如果AVIOContext被创建通过avio_alloc_conext(),av_class被调用者设置 const AVClass *av_class; unsigned char *buffer; // 起始buffer int buffer_size; // 最大buffer大小 unsigned char *buf_ptr; // 当前buffer中的position // 1、指向buffer数据尾部的指针 // 2、如果read的data返回小于data实际需要的, // 它将小于buffer+buffer_size的大小,如streams没有更多数据接受了 unsigned char *buf_end; void *opaque; // 一个私有容器,通过read/write/seek操作 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); int64_t pos; // 文件中当前buffer的position int must_flush; // 如果下一次seek要flush操作,返回true int eof_reached; // 如果出现EOF(资源无更多读取),返回true int write_flag; // 打开文件正在write的标识 int max_packet_size; unsigned long checksum; unsigned char *checksum_ptr; unsigned long (*update_checksum)(unsigned long checksum, const uint8_t *buf, unsigned int size); int error; /**< contains the error code or 0 if no error happened */ // 网络流协议pause或resume playback时,如MMS int (*read_pause)(void *opaque, int pause); // seek到给定的时间戳,一些网络流协议不支持seek到对应位置,如直播流 int64_t (*read_seek)(void *opaque, int stream_index, int64_t timestamp, int flags); // 是否能seek,通过AVIO_SEEKABLE标识,当stream不能seek时 int seekable; // 最大文件大小,被用于限制所分配的空间时 int64_t maxsize; // avio_read 及avio_write应满足直接读写,而不是通过一个缓冲区,avio_seek将被直接调用,当seek操作时。 int direct; // 字节读取数据 int64_t bytes_read; // seek读取数据 int seek_count; //字节写入数据 int writeout_count; //原始buffer大小 int orig_buffer_size; int short_seek_threshold; //分离用‘,’的可用协议集 const char *protocol_whitelist; //分离用‘,’的不可用协议集 const char *protocol_blacklist; // 代替write_packet的回调函数 int (*write_data_type)(void *opaque, uint8_t *buf, int buf_size, enum AVIODataMarkerType type, int64_t time); int ignore_boundary_point; enum AVIODataMarkerType current_type; int64_t last_time; } AVIOContext;AVIOContext读写时,buffer,buf_ptr,buf_end,buf_size及pos之间的关系:
/* * The following shows the relationship between buffer, buf_ptr, buf_end, buf_size, * and pos, when reading and when writing (since AVIOContext is used for both): * ********************************************************************************** * READING ********************************************************************************** * * | buffer_size | * |---------------------------------------| * | | * * buffer buf_ptr buf_end * +---------------+-----------------------+ * |/ / / / / / / /|/ / / / / / /| | * read buffer: |/ / consumed / | to be read /| | * |/ / / / / / / /|/ / / / / / /| | * +---------------+-----------------------+ * * pos * +-------------------------------------------+-----------------+ * input file: | | | * +-------------------------------------------+-----------------+ * * ********************************************************************************** * WRITING ********************************************************************************** * * | buffer_size | * |-------------------------------| * | | * * buffer buf_ptr buf_end * +-------------------+-----------+ * |/ / / / / / / / / /| | * write buffer: | / to be flushed / | | * |/ / / / / / / / / /| | * +-------------------+-----------+ * * pos * +--------------------------+-----------------------------------+ * output file: | | | * +--------------------------+-----------------------------------+ * */