PR #26100 opened by Forgejo_Fairy
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26100
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26100.patch

Fixes #26063.

Include the samples preceding `start` when limiting the input frame written to 
the loop FIFO. Track those skipped samples across frames and advance the output 
PTS only by samples emitted from the frame. This prevents a negative loop 
sample count on short input and preserves the input frame tail when the loop 
fills.

Add FATE cases for a short input, a frame crossing `start`, and a trailing 
frame. Update the existing `aloop` PTS reference.

Tested with `make fate-filter-aloop fate-filter-aloop-short 
fate-filter-aloop-tail fate-filter-aloop-cross SAMPLES=/opt/fate-suite`. Also 
verified identical 250-sample output across 1, 10, 30, 70, and 200 sample input 
frames.


>From e88731c1666d9610c19aa027a46693af4c85ac9f Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <[email protected]>
Date: Mon, 16 Nov 2026 11:59:59 +0000
Subject: [PATCH] avfilter/aloop: account for skipped samples when filling loop

Include the prefix before start when limiting the input frame, track skipped 
samples across frames, and advance the output PTS only by emitted samples. Add 
FATE coverage for short inputs and frame boundaries.

Assisted-by: Fairy
---
 libavfilter/f_loop.c              |  7 ++--
 tests/fate/filter-audio.mak       |  5 +++
 tests/ref/fate/filter-aloop       | 60 +++++++++++++++----------------
 tests/ref/fate/filter-aloop-cross | 14 ++++++++
 tests/ref/fate/filter-aloop-short |  8 +++++
 tests/ref/fate/filter-aloop-tail  |  8 +++++
 6 files changed, 70 insertions(+), 32 deletions(-)
 create mode 100644 tests/ref/fate/filter-aloop-cross
 create mode 100644 tests/ref/fate/filter-aloop-short
 create mode 100644 tests/ref/fate/filter-aloop-tail

diff --git a/libavfilter/f_loop.c b/libavfilter/f_loop.c
index bfa4590ac2f..744babf5689 100644
--- a/libavfilter/f_loop.c
+++ b/libavfilter/f_loop.c
@@ -161,14 +161,17 @@ static int afilter_frame(AVFilterLink *inlink, AVFrame 
*frame)
             if (s->start < 0)
                 s->start = inl->sample_count_out - written;
 
+            if (!s->nb_samples)
+                drain = FFMIN(frame->nb_samples, FFMAX(0, s->start - 
s->ignored_samples));
+            written = FFMIN(frame->nb_samples, drain + s->size - 
s->nb_samples);
+
             ret = av_audio_fifo_write(s->fifo, (void **)frame->extended_data, 
written);
             if (ret < 0)
                 return ret;
             if (!s->nb_samples) {
-                drain = FFMAX(0, s->start - s->ignored_samples);
                 s->pts = frame->pts;
                 av_audio_fifo_drain(s->fifo, drain);
-                s->pts += av_rescale_q(s->start - s->ignored_samples, 
(AVRational){1, outlink->sample_rate}, outlink->time_base);
+                s->ignored_samples += drain;
             }
             s->nb_samples += ret - drain;
             if (s->nb_samples == s->size && frame->nb_samples > written) {
diff --git a/tests/fate/filter-audio.mak b/tests/fate/filter-audio.mak
index f1035bba24a..c7ccd158bde 100644
--- a/tests/fate/filter-audio.mak
+++ b/tests/fate/filter-audio.mak
@@ -451,6 +451,11 @@ fate-filter-atempo: REF = 
$(SAMPLES)/filter-reference/atempo.pcm
 fate-filter-aloop: CMD = framecrc -filter_complex 
"sine=r=48000:f=480:d=4,aloop=loop=4:start=48000:size=48000,asetnsamples=9600"
 FATE_AFILTER-$(call ALLYES, SINE_FILTER ALOOP_FILTER ASETNSAMPLES_FILTER 
PCM_S16LE_ENCODER FRAMECRC_MUXER PIPE_PROTOCOL) += fate-filter-aloop
 
+fate-filter-aloop-short: CMD = framecrc -filter_complex 
"sine=r=48000:d=0.02,aloop=start=700:size=400:loop=2"
+fate-filter-aloop-tail: CMD = framecrc -filter_complex 
"sine=r=1000:d=0.2,asetnsamples=n=200:p=0,aloop=start=20:size=50:loop=1"
+fate-filter-aloop-cross: CMD = framecrc -filter_complex 
"sine=r=1000:d=0.2,asetnsamples=n=30:p=0,aloop=start=20:size=50:loop=1"
+FATE_AFILTER-$(call ALLYES, SINE_FILTER ALOOP_FILTER ASETNSAMPLES_FILTER 
PCM_S16LE_ENCODER FRAMECRC_MUXER PIPE_PROTOCOL) += fate-filter-aloop-short 
fate-filter-aloop-tail fate-filter-aloop-cross
+
 fate-filter-anullsink: CMD = framecrc -f lavfi -i "sine=d=1" -af 
"aresample=22050,asplit[a][b];[a]anullsink;[b]atrim=end_sample=256"
 FATE_AFILTER-$(call ALLYES, LAVFI_INDEV SINE_FILTER ARESAMPLE_FILTER 
ASPLIT_FILTER ANULLSINK_FILTER ATRIM_FILTER PCM_S16LE_ENCODER FRAMECRC_MUXER 
PIPE_PROTOCOL) += fate-filter-anullsink
 
diff --git a/tests/ref/fate/filter-aloop b/tests/ref/fate/filter-aloop
index d6286d8855e..c78aeddb1d1 100644
--- a/tests/ref/fate/filter-aloop
+++ b/tests/ref/fate/filter-aloop
@@ -13,33 +13,33 @@
 0,      67200,      67200,     9600,    19200, 0x1476501c
 0,      76800,      76800,     9600,    19200, 0x1476501c
 0,      86400,      86400,     9600,    19200, 0x1476501c
-0,      96896,      96896,     9600,    19200, 0x1476501c
-0,     106496,     106496,     9600,    19200, 0x1476501c
-0,     116096,     116096,     9600,    19200, 0x1476501c
-0,     125696,     125696,     9600,    19200, 0x1476501c
-0,     135296,     135296,     9600,    19200, 0x1476501c
-0,     144896,     144896,     9600,    19200, 0x1476501c
-0,     154496,     154496,     9600,    19200, 0x1476501c
-0,     164096,     164096,     9600,    19200, 0x1476501c
-0,     173696,     173696,     9600,    19200, 0x1476501c
-0,     183296,     183296,     9600,    19200, 0x1476501c
-0,     192896,     192896,     9600,    19200, 0x1476501c
-0,     202496,     202496,     9600,    19200, 0x1476501c
-0,     212096,     212096,     9600,    19200, 0x1476501c
-0,     221696,     221696,     9600,    19200, 0x1476501c
-0,     231296,     231296,     9600,    19200, 0x1476501c
-0,     240896,     240896,     9600,    19200, 0x1476501c
-0,     250496,     250496,     9600,    19200, 0x1476501c
-0,     260096,     260096,     9600,    19200, 0x1476501c
-0,     269696,     269696,     9600,    19200, 0x1476501c
-0,     279296,     279296,     9600,    19200, 0x1476501c
-0,     288896,     288896,     9600,    19200, 0x1476501c
-0,     298496,     298496,     9600,    19200, 0x1476501c
-0,     308096,     308096,     9600,    19200, 0x1476501c
-0,     317696,     317696,     9600,    19200, 0x1476501c
-0,     327296,     327296,     9600,    19200, 0x1476501c
-0,     336896,     336896,     9600,    19200, 0x1476501c
-0,     346496,     346496,     9600,    19200, 0x1476501c
-0,     356096,     356096,     9600,    19200, 0x1476501c
-0,     365696,     365696,     9600,    19200, 0x1476501c
-0,     375296,     375296,     9600,    19200, 0x1476501c
+0,      96000,      96000,     9600,    19200, 0x1476501c
+0,     105600,     105600,     9600,    19200, 0x1476501c
+0,     115200,     115200,     9600,    19200, 0x1476501c
+0,     124800,     124800,     9600,    19200, 0x1476501c
+0,     134400,     134400,     9600,    19200, 0x1476501c
+0,     144000,     144000,     9600,    19200, 0x1476501c
+0,     153600,     153600,     9600,    19200, 0x1476501c
+0,     163200,     163200,     9600,    19200, 0x1476501c
+0,     172800,     172800,     9600,    19200, 0x1476501c
+0,     182400,     182400,     9600,    19200, 0x1476501c
+0,     192000,     192000,     9600,    19200, 0x1476501c
+0,     201600,     201600,     9600,    19200, 0x1476501c
+0,     211200,     211200,     9600,    19200, 0x1476501c
+0,     220800,     220800,     9600,    19200, 0x1476501c
+0,     230400,     230400,     9600,    19200, 0x1476501c
+0,     240000,     240000,     9600,    19200, 0x1476501c
+0,     249600,     249600,     9600,    19200, 0x1476501c
+0,     259200,     259200,     9600,    19200, 0x1476501c
+0,     268800,     268800,     9600,    19200, 0x1476501c
+0,     278400,     278400,     9600,    19200, 0x1476501c
+0,     288000,     288000,     9600,    19200, 0x1476501c
+0,     297600,     297600,     9600,    19200, 0x1476501c
+0,     307200,     307200,     9600,    19200, 0x1476501c
+0,     316800,     316800,     9600,    19200, 0x1476501c
+0,     326400,     326400,     9600,    19200, 0x1476501c
+0,     336000,     336000,     9600,    19200, 0x1476501c
+0,     345600,     345600,     9600,    19200, 0x1476501c
+0,     355200,     355200,     9600,    19200, 0x1476501c
+0,     364800,     364800,     9600,    19200, 0x1476501c
+0,     374400,     374400,     9600,    19200, 0x1476501c
diff --git a/tests/ref/fate/filter-aloop-cross 
b/tests/ref/fate/filter-aloop-cross
new file mode 100644
index 00000000000..372ce3d45c9
--- /dev/null
+++ b/tests/ref/fate/filter-aloop-cross
@@ -0,0 +1,14 @@
+#tb 0: 1/1000
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 1000
+#channel_layout_name 0: mono
+0,          0,          0,       30,       60, 0x36d91b61
+0,         30,         30,       30,       60, 0x87351d94
+0,         60,         60,       10,       20, 0x603a08a2
+0,         70,         70,       50,      100, 0xb3f72fec
+0,        120,        120,       20,       40, 0xa1c013c2
+0,        140,        140,       30,       60, 0x70741c2a
+0,        170,        170,       30,       60, 0x97e31e41
+0,        200,        200,       30,       60, 0x36d91b61
+0,        230,        230,       20,       40, 0x9f97148b
diff --git a/tests/ref/fate/filter-aloop-short 
b/tests/ref/fate/filter-aloop-short
new file mode 100644
index 00000000000..e4947a7dee7
--- /dev/null
+++ b/tests/ref/fate/filter-aloop-short
@@ -0,0 +1,8 @@
+#tb 0: 1/48000
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 48000
+#channel_layout_name 0: mono
+0,          0,          0,      960,     1920, 0xe420ad95
+0,        960,        960,      260,      520, 0x2af608b6
+0,       1220,       1220,      260,      520, 0x2af608b6
diff --git a/tests/ref/fate/filter-aloop-tail b/tests/ref/fate/filter-aloop-tail
new file mode 100644
index 00000000000..662b57e0540
--- /dev/null
+++ b/tests/ref/fate/filter-aloop-tail
@@ -0,0 +1,8 @@
+#tb 0: 1/1000
+#media_type 0: audio
+#codec_id 0: pcm_s16le
+#sample_rate 0: 1000
+#channel_layout_name 0: mono
+0,          0,          0,       70,      140, 0xfccd4197
+0,         70,         70,       50,      100, 0xb3f72fec
+0,        120,        120,      130,      260, 0x31267e19
-- 
2.54.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to