2

我目前正在使用 Libavcodec 使用 H.264 对视频帧进行编码。因为我在这些视频帧被编码后进行流式传输,所以最大限度地减少延迟对我来说至关重要。我目前的设置是:

avcodec_register_all();
codec = avcodec_find_encoder(AV_CODEC_ID_H264);

// allocate and set ffmpeg context
context = avcodec_alloc_context3(encoder->codec);
context->bit_rate = bitrate;
context->width = out_width;
context->height = out_height;
context->time_base.num = 1;
context->time_base.den = 30;
context->gop_size = 1; // send SPS/PPS headers every packet
context->max_b_frames = 0;
context->pix_fmt = AV_PIX_FMT_YUV420P;

// set encoder parameters to max performance
av_opt_set(context->priv_data, "preset", "ultrafast", 0);
av_opt_set(context->priv_data, "tune", "zerolatency", 0);

// open capture encoder
avcodec_open2(context, codec, NULL);

这些设置运行良好,但我正在尝试切换到基于硬件的编码以减轻 CPU 的工作负载。我目前有一个 NVIDIA GPU,所以我尝试使用以下设置h264_nvenc

codec = avcodec_find_encoder_by_name("h264_nvenc");

// allocate and set ffmpeg context
context = avcodec_alloc_context3(encoder->codec);
context->dct_algo = FF_DCT_FASTINT;
context->bit_rate = bitrate;
context->width = out_width;
context->height = out_height;
context->time_base.num = 1;
context->time_base.den = 30;
context->gop_size = 1; // send SPS/PPS headers every packet
context->max_b_frames = 0;
context->pix_fmt = AV_PIX_FMT_YUV420P;

// set encoder parameters to max performancen
av_opt_set(context->priv_data, "preset", "llhq", 0);
av_opt_set(context->priv_data, "tune", "zerolatency", 0);

问题是,我注意到延迟h264_nvenc明显大于延迟AV_CODEC_ID_H264(基于软件的版本)。我认为我的设置或设置h264_nvenc一定是错误的,因为基于 GPU 的编码应该比基于软件的编码更快。谁能指出我正确的方向?非常感谢!

4

0 回答 0