AVFormatContext
封装格式上下文结构体,也是统领全局的结构体,保存了视频文件封装格式相关信息。
• iformat:输入媒体的AVInputFormat,比如指向AVInputFormat ff_flv_demuxer
• nb_streams:输入媒体的AVStream 个数
• streams:输入媒体的AVStream []数组
• duration:输入媒体的时长(以微秒为单位),计算方式可以参
考av_dump_format()函数。
• bit_rate:输入媒体的码率
AVInputFormat demuxer
每种封装格式(例如FLV, MKV, MP4, AVI)对应一个该结构体。AVOutputFormat muxer
• name:封装格式名称
• extensions:封装格式的扩展名
• id:封装格式ID
• 一些封装格式处理的接口函数,比如read_packet()
AVStream
视频文件中每个视频(音频)流对应一个该结构体。
• index:标识该视频/音频流
• time_base:该流的时基,PTS*time_base=真正的时间(秒)
• avg_frame_rate: 该流的帧率
• duration:该视频/音频流长度
• codecpar:编解码器参数属性
AVCodecContext:
编解码器上下文结构体,保存了视频(音频)编解码相关信息。
• codec:编解码器的AVCodec,比如指向AVCodec
ff_aac_latm_decoder
• width, height:图像的宽高(只针对视频)
• pix_fmt:像素格式(只针对视频)
• sample_rate:采样率(只针对音频)
• channels:声道数(只针对音频)
• sample_fmt:采样格式(只针对音频)
AVCodec
每种视频(音频)编解码器(例如H.264解码器)对应一个该结构体。
• name:编解码器名称
• type:编解码器类型
• id:编解码器ID
• 一些编解码的接口函数,比如int (*decode)()
AVPacket
存储一帧压缩编码数据。
• pts:显示时间戳
• dts:解码时间戳
• data:压缩编码数据
• size:压缩编码数据大小
• pos:数据的偏移地址
• stream_index:所属的AVStream
AVFrame
存储一帧解码后像素(采样)数据。
• data:解码后的图像像素数据(音频采样数据)
• linesize:对视频来说是图像中一行像素的大小;对音频来说是整个音频
帧的大小
• width, height:图像的宽高(只针对视频)
• key_frame:是否为关键帧(只针对视频) 。 • pict_type:帧类型(只针对视频) 。例如I, P, B • sample_rate:音频采样率(只针对音频)
• nb_samples:音频每通道采样数(只针对音频)
• pts:显示时间戳
AVCodecParameters:
• codec_type:媒体类型,比如AVMEDIA_TYPE_VIDEO
AVMEDIA_TYPE_AUDIO等
• codec_id:编解码器类型, 比如AV_CODEC_ID_H264
AV_CODEC_ID_AAC等。
AVIOContext:
这个结构体,是FFmpeg中有关io操作的顶层结构体,是avio的核心。FFmpeg支持打开本地文件路径和流媒体协议的URL。
虽然AVIOContext时avio操作的核心,但AVIOContext中的所有函数指针都不应该直接调用,它们只应在实现自定义I / O时由客户端应用程序设置。 通常这些设置为avio_alloc_context()中指定的函数指针(下一节中的read_packet函数指针)。
AVIOContext的相关调用都是在AVFormatContext中间接触发的。
比较重要的字段有:
(*read_packet):读取音视频数据的函数。
(*write_packet):写入音视频数据的函数。
(*read_pause):暂停或恢复网络流媒体协议的播放 。
相关函数
avio_alloc_context()
AVIOContext的初始化函数是avio_alloc_context(),销毁的时候使用av_free()释放掉其中的缓存即可。它的声明位于libavformat\avio.h中,如下所示。
/**
* Allocate and initialize an AVIOContext for buffered I/O. It must be later
* freed with avio_context_free().
*
* @param buffer Memory block for input/output operations via AVIOContext.
* The buffer must be allocated with av_malloc() and friends.
* It may be freed and replaced with a new buffer by libavformat.
* AVIOContext.buffer holds the buffer currently in use,
* which must be later freed with av_free().
* @param buffer_size The buffer size is very important for performance.
* For protocols with fixed blocksize it should be set to this blocksize.
* For others a typical size is a cache page, e.g. 4kb.
* @param write_flag Set to 1 if the buffer should be writable, 0 otherwise.
* @param opaque An opaque pointer to user-specific data.
* @param read_packet A function for refilling the buffer, may be NULL.
* For stream protocols, must never return 0 but rather
* a proper AVERROR code.
* @param write_packet A function for writing the buffer contents, may be NULL.
* The function may not change the input buffers content.
* @param seek A function for seeking to specified byte position, may be NULL.
*
* @return Allocated AVIOContext or NULL on failure.
*/
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));
avio_context_free()
先通过文件的buffer大小,申请一段内存。然后使用avio_alloc_context为AVIOContext分配内存,申请时,注册内存数据读取的回掉接口read_packet,最后将申请到的AVIOContext句柄挂载到AVFormatContext的pb字段,然后就可以通过AVFormatContext对文件进行操作了。
size_t buffer_size, avio_ctx_buffer_size = 4096;
struct buffer_data bd = { 0 };
AVFormatContext *fmt_ctx = avformat_alloc_context();
unsigned char *avio_ctx_buffer = (unsigned char *) av_malloc(avio_ctx_buffer_size);
AVIOContext *avio_ctx = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, &bd, &read_packet, NULL, NULL);
fmt_ctx->pb = avio_ctx;
// 清理
av_freep(&avio_ctx->buffer);
av_freep(&avio_ctx);