PR #26331 opened by Pu Cheng (GeekQAQ)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26331
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26331.patch

noise keeps the packet counter n, STARTPTS / STARTDTS and the state that
its noise is derived from; its expressions keep their registers and
random number generators. Without a flush callback all of this survives
av_bsf_flush(): after a flush and a seek (or a switch to another stream)
drop=lt(n,2) does not drop any packet, startpts is the one of the
previous position, and the bytes that get changed depend on the packets
before the flush.

Add a flush callback that resets all of these.

The new test fails without the callback.

Signed-off-by: Pu Cheng <[email protected]>



>From 6604bc7271e8d91ea820af9e5e64bc8c51140ee6 Mon Sep 17 00:00:00 2001
From: Pu Cheng <[email protected]>
Date: Wed, 25 Nov 2026 02:51:27 +0800
Subject: [PATCH 1/2] avutil/eval: add a function to reset the state of an
 expression

An AVExpr keeps its registers (st(), ld()) and random number generators
from one av_expr_eval() to the next. Add an internal function to restore
the state they have after av_expr_parse(), for users which have to start
over with the same expression, like a filter that gets flushed.

Signed-off-by: Pu Cheng <[email protected]>
---
 libavutil/eval.c          | 13 +++++++++++++
 libavutil/eval_internal.h | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+)
 create mode 100644 libavutil/eval_internal.h

diff --git a/libavutil/eval.c b/libavutil/eval.c
index 9b5ad218495..3a0b8d39ca3 100644
--- a/libavutil/eval.c
+++ b/libavutil/eval.c
@@ -32,6 +32,7 @@
 #include "avutil.h"
 #include "common.h"
 #include "eval.h"
+#include "eval_internal.h"
 #include "ffmath.h"
 #include "log.h"
 #include "mathematics.h"
@@ -821,6 +822,18 @@ int av_expr_count_func(AVExpr *e, unsigned *counter, int 
size, int arg)
     return expr_count(e, counter, size, ((int[]){e_const, e_func1, 
e_func2})[arg]);
 }
 
+void avpriv_expr_reset(AVExpr *e)
+{
+    AVExprRoot *r = (AVExprRoot *)e;
+
+    if (!e)
+        return;
+
+    av_assert1(e->root);
+    memset(r->var,        0, sizeof(*r->var)        * VARS);
+    memset(r->prng_state, 0, sizeof(*r->prng_state) * VARS);
+}
+
 double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
 {
     av_assert1(e->root);
diff --git a/libavutil/eval_internal.h b/libavutil/eval_internal.h
new file mode 100644
index 00000000000..181d333c979
--- /dev/null
+++ b/libavutil/eval_internal.h
@@ -0,0 +1,32 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_EVAL_INTERNAL_H
+#define AVUTIL_EVAL_INTERNAL_H
+
+#include "eval.h"
+
+/**
+ * Reset the registers and the random number generators of a parsed expression
+ * to their state after av_expr_parse().
+ *
+ * @param e the AVExpr to reset, may be NULL
+ */
+void avpriv_expr_reset(AVExpr *e);
+
+#endif /* AVUTIL_EVAL_INTERNAL_H */
-- 
2.54.1


>From 61da23c81ca0d3afed8e4d041b808dcf47b6f8c2 Mon Sep 17 00:00:00 2001
From: Pu Cheng <[email protected]>
Date: Wed, 25 Nov 2026 02:51:27 +0800
Subject: [PATCH 2/2] avcodec/bsf/noise: add a flush callback

noise keeps the packet counter n, STARTPTS / STARTDTS and the state that
its noise is derived from; its expressions keep their registers and
random number generators. Without a flush callback all of this survives
av_bsf_flush(): after a flush and a seek (or a switch to another stream)
drop=lt(n,2) does not drop any packet, startpts is the one of the
previous position, and the bytes that get changed depend on the packets
before the flush.

Add a flush callback that resets all of these.

The new test fails without the callback.

Signed-off-by: Pu Cheng <[email protected]>
---
 libavcodec/bsf/noise.c                | 15 +++++++++++++++
 tests/fate/ffmpeg.mak                 |  5 +++++
 tests/ref/fate/ffmpeg-bsf-noise-flush | 15 +++++++++++++++
 3 files changed, 35 insertions(+)
 create mode 100644 tests/ref/fate/ffmpeg-bsf-noise-flush

diff --git a/libavcodec/bsf/noise.c b/libavcodec/bsf/noise.c
index 9f5c367d2d3..99990c0a171 100644
--- a/libavcodec/bsf/noise.c
+++ b/libavcodec/bsf/noise.c
@@ -26,6 +26,7 @@
 #include "libavutil/log.h"
 #include "libavutil/opt.h"
 #include "libavutil/eval.h"
+#include "libavutil/eval_internal.h"
 
 static const char *const var_names[] = {
     "n",                           ///< packet index, starting from zero
@@ -207,6 +208,19 @@ static void noise_close(AVBSFContext *bsf)
     s->amount_pexpr = s->drop_pexpr = NULL;
 }
 
+static void noise_flush(AVBSFContext *bsf)
+{
+    NoiseContext *s = bsf->priv_data;
+
+    avpriv_expr_reset(s->amount_pexpr);
+    avpriv_expr_reset(s->drop_pexpr);
+    s->state   = 0;
+    s->pkt_idx = 0;
+    s->var_values[VAR_STARTPTS] = AV_NOPTS_VALUE;
+    s->var_values[VAR_STARTDTS] = AV_NOPTS_VALUE;
+    s->var_values[VAR_STATE]    = 0;
+}
+
 #define OFFSET(x) offsetof(NoiseContext, x)
 #define FLAGS 
(AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_BSF_PARAM)
 static const AVOption options[] = {
@@ -229,5 +243,6 @@ const FFBitStreamFilter ff_noise_bsf = {
     .priv_data_size = sizeof(NoiseContext),
     .init           = noise_init,
     .close          = noise_close,
+    .flush          = noise_flush,
     .filter         = noise,
 };
diff --git a/tests/fate/ffmpeg.mak b/tests/fate/ffmpeg.mak
index dca806bb980..5268ce0ef06 100644
--- a/tests/fate/ffmpeg.mak
+++ b/tests/fate/ffmpeg.mak
@@ -465,6 +465,11 @@ FATE_FFMPEG-$(call ALLYES, LAVFI_INDEV ANULLSRC_FILTER 
PCM_U8_DECODER PCM_U8_ENC
 fate-ffmpeg-bsf-input: CMD = framecrc -stream_loop 2 -bsf setts=PTS*2 -i 
$(TARGET_SAMPLES)/hevc/extradata-reload-multi-stsd.mov -c copy
 FATE_SAMPLES_FFMPEG-$(call FRAMECRC, MOV, HEVC, SETTS_BSF) += 
fate-ffmpeg-bsf-input
 
+# noise uses the packet counter, the first timestamps, an expression register
+# and its own state here, so every loop has to have the same output
+fate-ffmpeg-bsf-noise-flush: CMD = framecrc -stream_loop 2 -bsf 
"noise=amount=3+(pts-startpts)+(dts-startdts)+st(0\,ld(0)+1):drop=eq(n\,1)" -i 
$(TARGET_SAMPLES)/hevc/extradata-reload-multi-stsd.mov -c copy
+FATE_SAMPLES_FFMPEG-$(call FRAMECRC, MOV, HEVC, NOISE_BSF) += 
fate-ffmpeg-bsf-noise-flush
+
 # Test behaviour when a complex filtergraph returns EOF on one of its inputs,
 # but other inputs are still active.
 # cf. #10803
diff --git a/tests/ref/fate/ffmpeg-bsf-noise-flush 
b/tests/ref/fate/ffmpeg-bsf-noise-flush
new file mode 100644
index 00000000000..fc95548979c
--- /dev/null
+++ b/tests/ref/fate/ffmpeg-bsf-noise-flush
@@ -0,0 +1,15 @@
+#extradata 0:      110, 0xb4031479
+#tb 0: 1/25
+#media_type 0: video
+#codec_id 0: hevc
+#dimensions 0: 128x128
+#sar 0: 1/1
+0,          0,          0,        1,     2108, 0x89c2b849
+0,          2,          2,        1,     1915, 0xeb734772, S=1, New Extradata, 
     109, 0xcbe915dc
+0,          3,          3,        1,       35, 0xb8950c5c, F=0x0
+0,          4,          4,        1,     2108, 0x89c2b849, S=1, New Extradata, 
     110, 0xb4031479
+0,          6,          6,        1,     1915, 0xeb734772, S=1, New Extradata, 
     109, 0xcbe915dc
+0,          7,          7,        1,       35, 0xb8950c5c, F=0x0
+0,          8,          8,        1,     2108, 0x89c2b849, S=1, New Extradata, 
     110, 0xb4031479
+0,         10,         10,        1,     1915, 0xeb734772, S=1, New Extradata, 
     109, 0xcbe915dc
+0,         11,         11,        1,       35, 0xb8950c5c, F=0x0
-- 
2.54.1

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

Reply via email to