溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

FFmpeg avcodec_parameters_to_context函數(shù)剖析

發(fā)布時(shí)間:2020-07-16 07:07:13 來(lái)源:網(wǎng)絡(luò) 閱讀:11131 作者:fengyuzaitu 欄目:編程語(yǔ)言

歷史說(shuō)明
            以往FFmpeg版本中保存視音頻流信息參數(shù)是AVStream結(jié)構(gòu)體中的AVCodecContext字段。當(dāng)前FFmpeg版本是3.4,新的版本已經(jīng)將AVStream結(jié)構(gòu)體中的AVCodecContext字段定義為廢棄屬性。因此無(wú)法像以前舊的版本直接通過(guò)如下的代碼獲取到AVCodecContext結(jié)構(gòu)體參數(shù):
AVCodecContext* pAVCodecContext = avcodec_alloc_context3(NULL);
pAVCodecContext = pAVFormatContext->streams[videoStream]->codec;
    當(dāng)前版本保存視音頻流信息的結(jié)構(gòu)體AVCodecParameters,F(xiàn)Fmpeg提供了函數(shù)avcodec_parameters_to_context將音頻流信息拷貝到新的AVCodecContext結(jié)構(gòu)體中

函數(shù)說(shuō)明
    將AVCodecParameters結(jié)構(gòu)體中碼流參數(shù)拷貝到AVCodecContext結(jié)構(gòu)體中,并且重新拷貝一份extradata內(nèi)容,涉及到的視頻的關(guān)鍵參數(shù)有format, width, height, codec_type等,這些參數(shù)在優(yōu)化avformat_find_stream_info函數(shù)的時(shí)候,手動(dòng)指定該參數(shù)通過(guò)InitDecoder函數(shù)解碼統(tǒng)一指定H264,分辨率是1920*1080

調(diào)用avformat_open_input打開網(wǎng)絡(luò)流后,下一步調(diào)用av_find_stream_info從網(wǎng)絡(luò)流中讀取視音頻流的信息,通過(guò)調(diào)試查看AVCodecParameters結(jié)構(gòu)體的視頻參數(shù)
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    
}

說(shuō)明:
當(dāng)手動(dòng)指定編碼格式的時(shí)候,說(shuō)明探測(cè)碼流格式的代碼是可以省略的,例如
avformat_open_input
avformat_find_stream_info
在其他章節(jié)中調(diào)用InitDecoder函數(shù)替換上面的函數(shù)

avformat_open_input缺省情況下,無(wú)法播放圖像,這個(gè)目前還在研究當(dāng)中

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI