ffmpeg 4.0解码h264比旧版本1慢(n2.8.14)

时间:2018-05-23 07:09:11

标签: ffmpeg

我使用ffmpeg解码h264并转码为jpeg 我曾经使用2.8版本,现在改为4.0 新版本avcodec_decode_video2已弃用,因此我使用新的API(发送,接收)。
我发现解码h264时新版本ffmpeg比旧版本慢得多 这是为什么?如何达到原始速度?

这里是解码器初始化代码:
版本2.8

//2.8
if (avformat_open_input(&format_, path, NULL, &options) != 0) {
    printf("FileMediaBase: open input failed\n");
    return -1;
}
if (avformat_find_stream_info(format_, NULL) < 0) {
    printf("FileMediaBase: avformat_find_stream_info failed\n");
    avformat_close_input(&format_);
    return -1;
}
AVCodecContext *code_ctx = 0;
AVCodec *codec = 0;
// Find the first audio and video stream
for (int i = 0; i < format_->nb_streams; i++) {
    code_ctx = format_->streams[i]->codec;
    codec = avcodec_find_decoder(code_ctx->codec_id);
    if (code_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
        vcode_ctx_ = code_ctx;
        vstream_ = format_->streams[i];
    }else if (code_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
        acode_ctx_ = code_ctx;
        astream_ = format_->streams[i];
    }
    // Open the codec
    if (!codec || (avcodec_open2(code_ctx, codec, NULL) < 0)) {
        printf("avcodec_open2() failed\n");
        avformat_close_input(&format_);
        return -1;
    }

版本4.0

if (avformat_open_input(&format_, path, NULL, &options) != 0) {
    printf("FileMediaBase: open input failed\n");
    return -1;
}
if (avformat_find_stream_info(format_, NULL) < 0) {
    printf("FileMediaBase: avformat_find_stream_info failed\n");
    avformat_close_input(&format_);
    return -1;
}
AVStream *st;
AVCodec *dec = NULL;
AVDictionary *opts = NULL;

ret = av_find_best_stream(format_, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if (ret < 0) {
    return ret;
}
else {
    stream_index = ret;
    st = format_->streams[stream_index];

    /* find decoder for the stream */
    dec = avcodec_find_decoder(st->codecpar->codec_id);
    if (!dec) {
        fprintf(stderr, "Failed to find %s codec\n",
            av_get_media_type_string(type));
        return AVERROR(EINVAL);
    }

    /* Allocate a codec context for the decoder */
    *dec_ctx = avcodec_alloc_context3(dec);
    if (!*dec_ctx) {
        fprintf(stderr, "Failed to allocate the %s codec context\n",
            av_get_media_type_string(type));
        return AVERROR(ENOMEM);
    }

    /* Copy codec parameters from input stream to output codec context */
    if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) {
        fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",
            av_get_media_type_string(type));
        avcodec_free_context(dec_ctx);
        return ret;
    }

    /*if (dec->id == AV_CODEC_ID_H264) {
        (*dec_ctx)->flags |= AV_CODEC_FLAG_TRUNCATED;
    }*/

    /* Init the decoders, with or without reference counting */
    //av_dict_set(&opts, "refcounted_frames", refcount ? "1" : "0", 0);
    if ((ret = avcodec_open2(*dec_ctx, dec, &opts)) < 0) {
        fprintf(stderr, "Failed to open %s codec\n",
            av_get_media_type_string(type));
        avcodec_free_context(dec_ctx);
        return ret;
    }

我打印出花在某些API上的时间 enter image description here

1 个答案:

答案 0 :(得分:0)

使用ffmpeg 2.8.14和4.0.1版本解码h264视频流时,我遇到了相同的问题。 在这两种情况下,在avformat_find_stream_info()之后访问编解码器上下文时

AVFormatContext *pFormatCtx;
AVCodecContext* pCodecCtxOrig;
...
avformat_find_stream_info(pFormatCtx, NULL)
...
for (uint i = 0; i < pFormatCtx->nb_streams; i++)
{
  if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  {
    pCodecCtxOrig = pFormatCtx->streams[i]->codec;
    ...

  }
}

然后pCodecCtxOrig :: thread_count实际上设置为0,这意味着当使用avcodec_open2()打开编解码器上下文时,ffmpeg库会根据CPU内核自行查找合适的thread_count。在我的情况下,线程数设置为5。

对于2.8.14,pCodecCtxOrig :: thread_count设置为0并从中进行复制

AVCodecContext* pCodecCtxOrig;
AVCodecContext* pCodecCtx;
....
avcodec_copy_context(pCodecCtx, pCodecCtxOrig)

thread_count也被复制。

在4.0.1的情况下,不建议使用avcodec_copy_context(),而应使用avcodec_parameters_to_context()。应对结构AVCodecParameters不包含thread_count,因此信息丢失。线程数保持默认值1,该值由avcodec_alloc_context3设置。要解决此问题,请在打开编解码器上下文之前将thread_count设置为0。