1、avcodec_open2 函数定义
avcodec_open2是FFmpeg库中的一个函数,用于打开音视频编解码器的上下文并进行初始化。这个函数是音视频编解码过程中的一个关键步骤,它使得编解码器准备好进行编解码操作。
cpp
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options);
参数说明:
- AVCodecContext *avctx:音视频编解码器上下文,包含了一些编解码器的参数,如编码格式、帧率、分辨率等。
- const AVCodec *codec:音视频编解码器,根据avctx->codec_id来确定。
- AVDictionary **options:编解码器的配置参数,是一个字典(key-value键值对的集合),可以为空。
2、avcodec_open2 函数内部调用流程
2.1 基础检查
cpp
if (avcodec_is_open(avctx)) return 0; // 已打开则跳过
if (!codec && !avctx->codec) return AVERROR(EINVAL); // 无编解码器错误
if (codec && avctx->codec && codec != avctx->codec)
return AVERROR(EINVAL); // 编解码器不匹配
// 确保上下文未初始化。
// 验证编解码器有效性(必须通过参数或上下文提供)。
2.2 编解码器设置
cpp
codec = avctx->codec ? avctx->codec : codec;
avctx->codec_type = codec->type;
avctx->codec_id = codec->id;
avctx->codec = codec;
// 绑定编解码器到上下文。
// 同步编解码器类型(音频/视频等)和ID。
2.3 参数校验
- Extra Data 检查:avctx->extradata_size 需在 [0, FF_MAX_EXTRADATA_SIZE) 范围内。
- 采样率/块对齐:负数检测(sample_rate < 0 或 block_align < 0 报错)。
- 音频通道布局:
- 非解码器时(如编码器)必须设置通道布局。
- 通道布局有效性检查(av_channel_layout_check)。
- 通道数上限(FF_SANE_NB_CHANNELS)。
2.4 分配内部资源
cpp
AVCodecInternal *avci = (
is_decoder) ? ff_decode_internal_alloc() :
ff_encode_internal_alloc();
avci->buffer_frame = av_frame_alloc(); // 缓存帧
avci->buffer_pkt = av_packet_alloc(); // 缓存包
// 根据编解码器类型(编码/解码)分配内部上下文 AVCodecInternal。
// 初始化帧/包缓存(用于中间数据处理)。
2.5 私有数据私有化
cpp
if (codec2->priv_data_size > 0) {
avctx->priv_data = av_mallocz(codec2->priv_data_size); // 分配私有数据
*(const AVClass**)avctx->priv_data = codec->priv_class; // 绑定类
av_opt_set_defaults(avctx->priv_data); // 设默认选项
av_opt_set_dict(avctx->priv_data, options); // 应用用户选项
}
// 为编解码器私有数据(如 H264Context)分配内存。
// 绑定类并初始化选项(通过 AVDictionary** options)。
2.6 流参数处理
cpp
if (!(特殊编解码器)) {
if (coded_width/height) ff_set_dimensions(avctx, coded_w, coded_h);
else if (width/height) ff_set_dimensions(avctx, width, height);
}
av_image_check_size2(...); // 检查宽高有效性
av_image_check_sar(...); // 检查宽高比有效性
// 设置宽高(跳过 H.264/VP6F/DXV 等特殊编解码器)。
// 校验尺寸和像素宽高比(SAR)的合理性。
2.7 实验行解码器检查
cpp
if ((codec->capabilities & AV_CODEC_CAP_EXPERIMENTAL) &&
avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL) {
return AVERROR_EXPERIMENTAL; // 实验性编解码器需明确启用
}
// 若编解码器标记为实验性,且用户未通过 -strict experimental 启用,则报错。
2.8 解码器|编码器与初始化
cpp
if (is_encoder) ret = ff_encode_preinit(avctx);
else ret = ff_decode_preinit(avctx);
// 调用编码器/解码器的预初始化逻辑(设置默认参数)。
2.9 错误处理
cpp
free_and_end:
ff_codec_close(avctx); // 释放资源
end:
return ret;
3、avcodec_open2 函数调用流程图
