From df4587789f1f4337ed778752010d7176a9e90241 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Mon, 8 Sep 2025 23:41:43 -0500 Subject: [PATCH] avcodec/amfenc: avoid unnecessary output delay in low delay mode The code optimizes throughput by letting the encoder work on frame N until frame N+1 is ready for submission, but this hurts low-delay uses by delaying output by one frame. Don't delay output beyond what is necessary when AV_CODEC_FLAG_LOW_DELAY is used. Signed-off-by: Cameron Gutman --- libavcodec/amfenc.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c index b16b642e4c..f363192000 100644 --- a/libavcodec/amfenc.c +++ b/libavcodec/amfenc.c @@ -497,7 +497,7 @@ static int amf_submit_frame(AVCodecContext *avctx, AVFrame *frame, AMFSurface AMF_RESULT res; int ret; int hw_surface = 0; - int max_b_frames = ctx->max_b_frames < 0 ? 0 : ctx->max_b_frames; + int output_delay = FFMAX(ctx->max_b_frames, 0) + ((avctx->flags & AV_CODEC_FLAG_LOW_DELAY) ? 0 : 1); // prepare surface from frame switch (frame->format) { @@ -634,8 +634,8 @@ static int amf_submit_frame(AVCodecContext *avctx, AVFrame *frame, AMFSurface ret = av_fifo_write(ctx->timestamp_list, &frame->pts, 1); if (ret < 0) return ret; - if(ctx->submitted_frame <= ctx->encoded_frame + max_b_frames + 1) - return AVERROR(EAGAIN); // if frame just submiited - don't poll or wait + if(ctx->submitted_frame <= ctx->encoded_frame + output_delay) + return AVERROR(EAGAIN); // too soon to poll or wait } return 0; } @@ -681,7 +681,7 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) AVFrame *frame = av_frame_alloc(); int block_and_wait; int64_t pts = 0; - int max_b_frames = ctx->max_b_frames < 0 ? 0 : ctx->max_b_frames; + int output_delay = FFMAX(ctx->max_b_frames, 0) + ((avctx->flags & AV_CODEC_FLAG_LOW_DELAY) ? 0 : 1); if (!ctx->encoder){ av_frame_free(&frame); @@ -700,7 +700,7 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt) if(ret != AVERROR_EOF){ av_frame_free(&frame); if(ret == AVERROR(EAGAIN)){ - if(ctx->submitted_frame <= ctx->encoded_frame + max_b_frames + 1) // too soon to poll + if(ctx->submitted_frame <= ctx->encoded_frame + output_delay) // too soon to poll return ret; } }