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

标签 ffmpeg

我使用ffmpeg解码h264并转码为jpeg。
我以前用的是2.8版本,现在换成4.0了。
新版本 avcodec_decode_video2 已弃用,因此我使用新的 API(发送、接收)。
我发现新版本的ffmpeg在解码h264时比旧版本慢很多。
这是为什么?如何达到原来的速度?

解码器初始化代码如下:
版本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

最佳答案

我已经解决了这个问题,将解码线程数(4.0版本中默认为1)更改为6(2.8版本中默认为6)。

(*dec_ctx)->thread_count = 6;

关于ffmpeg 4.0 解码 h264 比旧版本慢(n2.8.14),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50481965/

相关文章:

ffmpeg - 如何在导出时强制使用 "full range"标志

FFmpeg android - 字体配置错误 : Cannot load default config file

video - 使用 FFMPEG 将许多帧合并为一个视频?

ffmpeg - 使用ffmpeg,试图将容器从Mpeg TS更改为MP4,fps变成了Mpeg TS的两倍

video - 通过 ffmpeg 恢复损坏的 MTS 文件

video - opencv 2.4 中的 ffmpeg

android - 在android中调整视频大小

android - 如何循环播放 MPEG TS 流

ffmpeg - 使用特定的叠加起始位置从左到右移动叠加

Python Popen shell=False 导致 OSError : [Errno 2] No such file or directory FFREPORT