函数说明
将AVCodecParameters结构体中码流参数拷贝到AVCodecContext结构体中,
并且重新拷贝一份extradata内容,涉及到的视频的关键参数有
format, width, height, codec_type等,这些参数在优化avformat_find_stream_info
函数的时候,手动指定该参数通过InitDecoder函数
解码统一指定H264,分辨率是1920*1080
int avcodec_parameters_to_context(AVCodecContext *codec,
const AVCodecParameters *par)
{
codec->codec_type = par->codec_type;//AVMEDIA_TYPE_VIDEO
codec->codec_id = par->codec_id;//AV_CODEC_ID_H264
codec->codec_tag = par->codec_tag;//0
codec->bit_rate = par->bit_rate;//0
codec->bits_per_coded_sample = par->bits_per_coded_sample;//0
codec->bits_per_raw_sample = par->bits_per_raw_sample;//8
codec->profile = par->profile;//66
codec->level = par->level;//42
switch (par->codec_type) {
case AVMEDIA_TYPE_VIDEO:
codec->pix_fmt = par->format;//12
codec->width = par->width;//1920
codec->height = par->height;//1080
codec->field_order = par->field_order;//AV_FIELD_PROGRESSIVE
codec->color_range = par->color_range;//AVCOL_RANGE_JPEG
codec->color_primaries = par->color_primaries;//AVCOL_PRI_BT709
codec->color_trc = par->color_trc;//AVCOL_TRC_BT709
codec->colorspace = par->color_space;//AVCOL_SPC_BT709
codec->chroma_sample_location = par->chroma_location;//AVCHROMA_LOC_LEFT
codec->sample_aspect_ratio = par->sample_aspect_ratio;//num=0,den=1
codec->has_b_frames = par->video_delay;//0
break;
case AVMEDIA_TYPE_AUDIO:
codec->sample_fmt = par->format;
codec->channel_layout = par->channel_layout;
codec->channels = par->channels;
codec->sample_rate = par->sample_rate;
codec->block_align = par->block_align;
codec->frame_size = par->frame_size;
codec->delay =
codec->initial_padding = par->initial_padding;
codec->trailing_padding = par->trailing_padding;
codec->seek_preroll = par->seek_preroll;
break;
case AVMEDIA_TYPE_SUBTITLE:
codec->width = par->width;
codec->height = par->height;
break;
}
//extradata保存SPS/PPS信息
if (par->extradata) {
av_freep(&codec->extradata);
codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
if (!codec->extradata)
return AVERROR(ENOMEM);
memcpy(codec->extradata, par->extradata, par->extradata_size);
codec->extradata_size = par->extradata_size;
}
return 0;
}
说明:
当手动指定编码格式的时候,说明探测码流格式的代码是可以省略的,例如
avformat_open_input
avformat_find_stream_info
avformat_open_input缺省情况下,无法播放图像
原文地址:http://blog.51cto.com/fengyuzaitu/2059121