FFmpeg avcodec_parameters_to_context函數剖析

歷史說明
            以往FFmpeg版本中保存視音頻流信息參數是AVStream結構體中的AVCodecContext字段。當前FFmpeg版本是3.4,新的版本已經將AVStream結構體中的AVCodecContext字段定義爲廢棄屬性。因此無法像以前舊的版本直接通過如下的代碼獲取到AVCodecContext結構體參數:
AVCodecContext* pAVCodecContext = avcodec_alloc_context3(NULL);
pAVCodecContext = pAVFormatContext->streams[videoStream]->codec;
    當前版本保存視音頻流信息的結構體AVCodecParameters,FFmpeg提供了函數avcodec_parameters_to_context將音頻流信息拷貝到新的AVCodecContext結構體中

函數說明
    將AVCodecParameters結構體中碼流參數拷貝到AVCodecContext結構體中,並且重新拷貝一份extradata內容,涉及到的視頻的關鍵參數有format, width, height, codec_type等,這些參數在優化avformat_find_stream_info函數的時候,手動指定該參數通過InitDecoder函數解碼統一指定H264,分辨率是1920*1080

調用avformat_open_input打開網絡流後,下一步調用av_find_stream_info從網絡流中讀取視音頻流的信息,通過調試查看AVCodecParameters結構體的視頻參數
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信息,重新拷貝一份到AVCodecContext中,用於解碼
    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;
}


代碼定義
typedef struct AVStream
{
#if FF_API_LAVF_AVCTX
    
/**
     
* @deprecated use the codecpar struct instead
     
*/
    
attribute_deprecated
    AVCodecContext *codec;

#endif    
}

說明:
當手動指定編碼格式的時候,說明探測碼流格式的代碼是可以省略的,例如
avformat_open_input
avformat_find_stream_info
在其他章節中調用InitDecoder函數替換上面的函數

avformat_open_input缺省情況下,無法播放圖像,這個目前還在研究當中

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章