FFmpeg - How to initialize the AVCodecContext

FFmpeg old version, We initialize AVCodecContext often use the follow code.

But, in the FFmpeg new version, the streams->codec code be declared attribute_deprecated, it is suggest not to use the follow code.

AVCodecContext * pCodecCtx = pFormatCtx->streams[videoIndex]->codec;
AVCodec * pCodec = avcodec_find_decoder(pCodecCtx->codec_id);

How should we do?

The avcodec_open2 method’s document advice to use avcodec_alloc_context3() method.

The document is introduced in detail how should we do.

But, it missing a method. It is avcodec_parameters_to_context() method.

AVCodec *pCodec = avcodec_find_decoder(pFormatCtx->streams[videoIndex]->codecpar->codec_id);
AVCodecContext *pCodecCtx = avcodec_alloc_context3(pCodec);
avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoIndex]->codecpar);

if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) {
   printf("Could not open codec.\n");
    return -1;
}

你可能感兴趣的:(method,ffmpeg)