Skip to content

Commit

Permalink
ffmpeg_patches: add amfenc delay/buffering fix
Browse files Browse the repository at this point in the history
Resolves delay observed in amfenc encoding observed since
CB-based capture was introduced. Should also improve latency due
to frames no longer being buffered.
  • Loading branch information
psyke83 committed Dec 2, 2022
1 parent 4fe5637 commit 1be7118
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-ffmpeg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ jobs:
run: |
git apply -v --ignore-whitespace ../../ffmpeg_patches/ffmpeg/01-idr-on-amf.patch
git apply -v --ignore-whitespace ../../ffmpeg_patches/ffmpeg/02-amf-color-fixes.patch
git apply -v --ignore-whitespace ../../ffmpeg_patches/ffmpeg/03-amfenc-disable-buffering.patch
- name: Setup cross compilation
id: cross
Expand Down
43 changes: 43 additions & 0 deletions ffmpeg_patches/ffmpeg/03-amfenc-disable-buffering.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
From 8b0966a3723a05d29810b116454a1eb94e15a0b1 Mon Sep 17 00:00:00 2001
From: Conn O'Griofa <[email protected]>
Date: Thu, 24 Nov 2022 06:34:48 +0000
Subject: [PATCH] amfenc: disable buffering & blocking delay in IPP mode

When realtime encoding is required, the HW queue and arbitrary
1ms sleep during blocking introduces unnecessary latency, especially
in low FPS situations such as streaming desktop content at a variable
framerate.

Resolve by disabling buffering and blocking delay if no B-frames are
requested, as is typical for zero latency streaming use cases.
---
libavcodec/amfenc.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c
index fb23ed738c..c452a0fd5b 100644
--- a/libavcodec/amfenc.c
+++ b/libavcodec/amfenc.c
@@ -219,7 +219,7 @@ static int amf_init_context(AVCodecContext *avctx)
av_unused int ret;

ctx->hwsurfaces_in_queue = 0;
- ctx->hwsurfaces_in_queue_max = 16;
+ ctx->hwsurfaces_in_queue_max = avctx->max_b_frames > 0 ? 16 : 0; // avoid buffering frames if no B frames are in use

// configure AMF logger
// the return of these functions indicates old state and do not affect behaviour
@@ -769,7 +769,9 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
}
} else if (ctx->delayed_surface != NULL || ctx->delayed_drain || (ctx->eof && res_query != AMF_EOF) || (ctx->hwsurfaces_in_queue >= ctx->hwsurfaces_in_queue_max)) {
block_and_wait = 1;
- av_usleep(1000); // wait and poll again
+ if (avctx->max_b_frames > 0) {
+ av_usleep(1000); // wait and poll again
+ }
}
} while (block_and_wait);

--
2.37.2

0 comments on commit 1be7118

Please sign in to comment.