ffmpeg - 如何减少 FFmpeg h264_qsv 编码器的延迟?

标签 ffmpeg h.264

我在我的摄像头直播软件中使用 FFmpeg(3.4) h264_qsv 编码器,我发现发送 40 帧后收到编码数据!大约有 1.5 秒的延迟,不适合实时情况。如何配置编码器?非常感谢。

我的代码:

AVCodecContext* OpenH264Codec(int width, int height, int bitrate, int framerate)
{
    AVCodecContext* ctx = 0;
    AVCodec* c = 0;
    c = avcodec_find_encoder_by_name("h264_qsv");
    if (c == NULL) return NULL;
    ctx = avcodec_alloc_context3(c);
    if (ctx == NULL) return NULL;
    ctx->width = width;
    ctx->height = height;
    ctx->pix_fmt = c->pix_fmts[0];
    ctx->bit_rate = bitrate;
    ctx->bit_rate_tolerance = ctx->bit_rate / 2;
    ctx->rc_min_rate = 32000;
    ctx->rc_max_rate = ctx->bit_rate*1.5;
    ctx->time_base.den = framerate;
    ctx->time_base.num = 1;
    ctx->framerate.den = 1;
    ctx->framerate.num = framerate;
    ctx->gop_size = framerate*5;
    ctx->max_b_frames = 0;

    av_opt_set(ctx->priv_data, "preset", "veryfast", 0);
    av_opt_set(ctx->priv_data, "avbr_accuracy", "1", 0);
    av_opt_set(ctx->priv_data, "async_depth", "1", 0);
    av_opt_set(ctx->priv_data, "profile", "main", 0);
    ctx->flags |= AV_CODEC_FLAG_QSCALE;
    if (avcodec_open2(ctx, c, 0) < 0)
    {
        avcodec_free_context(&ctx);
        return NULL;
    }
    return ctx;
}

最佳答案

感谢 Mulvya 的提示。现在只有 5 帧延迟。

AVCodecContext* OpenH264Codec(int width, int height, int bitrate, int framerate)
{
    AVCodecContext* ctx = 0;
    AVCodec* c = 0;
    c = avcodec_find_encoder_by_name("h264_qsv");
    if (c == NULL) return NULL;
    ctx = avcodec_alloc_context3(c);
    if (ctx == NULL) return NULL;
    ctx->width = width;
    ctx->height = height;
    ctx->pix_fmt = c->pix_fmts[0];
    ctx->bit_rate = bitrate;
    ctx->bit_rate_tolerance = ctx->bit_rate / 2;
    ctx->time_base.den = framerate;
    ctx->time_base.num = 1;
    ctx->framerate.den = 1;
    ctx->framerate.num = framerate;
    ctx->gop_size = framerate*5;
    ctx->max_b_frames = 0;
    av_opt_set(ctx->priv_data, "preset", "medium", 0);
    av_opt_set(ctx->priv_data, "look_ahead", "0", 0);
    //av_opt_set(ctx->priv_data, "look_ahead_depth", "8", 0);
    if (avcodec_open2(ctx, c, 0) < 0)
    {
        avcodec_free_context(&ctx);
        return NULL;
    }
    return ctx;
}

关于ffmpeg - 如何减少 FFmpeg h264_qsv 编码器的延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47096377/

相关文章:

python - 从 Python 中的 Amazon Kinesis 输出读取字节图像

linux - 如何将原始 h264 树莓派流保存到 .h264 文件

ffmpeg - 使用 libavformat API 读取 H264 SPS 和 PPS NAL 字节

FFMPeg 快速 P-Frame 解码

FFMPEG 不会播放 rtsp 链接,但相同的链接在 VLC 中有效

c# - 如何将RGB图像编码成H264帧(关键帧+一些依赖帧)?

Android ffmpeg 保存并 append h264 流视频

android - 如何从 Android 上的一系列图像创建 h264 视频

php - 使用 CURL 在服务器中的特定文件夹中下载 MP4 视频

linux - 从 Linux 图形工具制作图像幻灯片