* [FFmpeg-devel] [PATCH] avcodec/amfenc: avoid unnecessary output delay in low delay mode (PR #20473)
@ 2025-09-09 5:09 cgutman via ffmpeg-devel
0 siblings, 0 replies; only message in thread
From: cgutman via ffmpeg-devel @ 2025-09-09 5:09 UTC (permalink / raw)
To: ffmpeg-devel; +Cc: cgutman
PR #20473 opened by cgutman
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20473
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20473.patch
88a8ba5c991a0ffd00c8ce34d127b1b201b1ee19 introduced an output latency regression making it impossible to acheive lower than 1 frame of latency from AMF. I assume the new behavior was intentional to improve throughput, so I only avoid it if the user explicitly asks for low delay.
>From f3083931ad8b432ecd87dee4bef6895e9e20f9d9 Mon Sep 17 00:00:00 2001
From: Cameron Gutman <aicommander@gmail.com>
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 <aicommander@gmail.com>
---
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;
}
}
--
2.49.1
_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2025-09-09 5:09 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-09 5:09 [FFmpeg-devel] [PATCH] avcodec/amfenc: avoid unnecessary output delay in low delay mode (PR #20473) cgutman via ffmpeg-devel
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git