gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:49:25 24 4
gpt4 key购买 nike

我在我的摄像头直播软件中使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com