PR #25872 opened by luoye-cpu
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25872
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25872.patch
avfilter: add film_grain_adjust filter
FFmpeg can produce AV1 film grain synthesis parameters as
AV_FRAME_DATA_FILM_GRAIN_PARAMS frame side data (native av1, libdav1d and
qsvdec, gated by AV_CODEC_EXPORT_DATA_FILM_GRAIN), it can consume them
(libplacebo synthesises them for display), and the sidedata filter can
delete such an entry wholesale. What it cannot do is modify the
parameters in flight: nothing between a decoder and the next stage of a
pipeline can rescale the grain amplitude or override individual fields.
The natural workflow "decode a grain-carrying master, dial the grain down
or up, feed the modified parameters to an encoder that consumes film
grain side data" is therefore not expressible today; re-encoding through
an analysis-based encoder re-estimates grain from scratch and loses the
authoring intent of the source.
Add the film_grain_adjust video filter to close that gap. The filter
layer (rather than a decoder option or a bitstream filter) is the natural
place for this: film grain parameters are a per-frame side data property,
the filter chain is the generic place where per-frame properties can be
rewritten between arbitrary sources and sinks, and a bitstream filter
operates on encoded bitstream units, not on frame side data.
The filter is a pixel pass-through: it never touches picture data, it
only adjusts the film grain side data of each frame and forwards the
frame unchanged. Side data buffers are refcounted and shared between
frames referencing each other (av_frame_ref() and
av_frame_side_data_clone() only take an additional reference, and
av_frame_make_writable() only covers the picture buffers). A frame may
carry several film grain parameter sets as multiple
AV_FRAME_DATA_FILM_GRAIN_PARAMS entries; every AV1 set on the frame is
adjusted, and the filter makes each adjusted entry's buffer writable with
av_buffer_make_writable() before adjusting it - a no-op when the frame
holds the only reference, a copy-on-write otherwise - so other frames
sharing the buffer are never affected. Because it does not read pixels
it works the same on hardware frames (e.g. cuda, qsv), which is the
typical path for this workflow. The filter is declared
AVFILTER_FLAG_METADATA_ONLY and FF_FILTER_FLAG_DYNAMIC_FRAME_PARAMS: it
carries no frame parameter state and never accesses pixel data based on
the size or format of its input link, so frames changing size or format
after the graph was configured pass through unharmed (same class of
filters as marked in 58892c922e).
15 options are exposed: overall and chroma-only amplitude scales
('scale', 'chroma_scale') plus overrides for chroma_scaling_from_luma,
overlap_flag, limit_output_range, scaling_shift, ar_coeff_shift,
grain_scale_shift and the cb/cr scaling-function coefficients (cb_mult,
cr_mult, cb_luma_mult, cr_luma_mult, cb_offset, cr_offset). Overrides
use sentinels one below each field's valid range (-1, -129, -257) to
mean "keep the source value", so the help output shows a tight range per
option. The chroma scaling-function coefficients are only applied when
chroma_scaling_from_luma is zero and the corresponding plane has chroma
scaling points, mirroring the AV1 bitstream syntax where they are only
transmitted in that case. All options are runtime
parameters and can be changed through process_command while the filter
is running. Out-of-bitstream-range values of scaling_shift and
ar_coeff_shift are rejected explicitly with EINVAL in init() and
process_command() instead of being silently clipped, and the
end-of-stream summary is only logged when verbose is set.
The point scaling values (point_y_scaling / point_uv_scaling) are 8-bit
amplitude multipliers in the AV1 bitstream (u(8)); the effective grain
amplitude is this value times the generated grain, right-shifted by
scaling_shift (see add_noise_to_block() in libaom's grain_synthesis.c).
libaom, dav1d and the hardware APIs use the value verbatim, so the only
hard limit is the field width: amplitude scaling clamps to 0..255, the
scale/chroma_scale option range is capped at 255.0 (a factor of 255
saturates every non-zero source point) and scale=1.0 is a bit-exact
identity.
This is independent of, and complementary to, work on the production
side (e.g. exporting film grain side data from nvdec_av1): the filter
operates on whatever AV_FRAME_DATA_FILM_GRAIN_PARAMS entry a decoder
exports and passes it to whatever consumes it downstream. The chroma
subsampling av1dec exports for CUDA frames has been corrected in
3c1aad218f8b ("avcodec/av1dec: use sw_pix_fmt for film grain
subsampling"), so the side data this filter reads is consistent on that
path as well.
## Testing
- Built on current master with a minimal configure
(--disable-everything --enable-filter=film_grain_adjust,...) on MSYS2
mingw64; make exits 0 and the filter registers
(ffmpeg -h filter=film_grain_adjust shows all 15 options with the
runtime-parameter flag).
- fate-filter-film-grain-adjust (new FATE entry, lavfi-based, no new
fate-suite sample) passes; its frame CRCs are identical to plain
testsrc2 output, i.e. the pixel pass-through invariant holds.
- fate-api-film-grain-adjust (new API test, enabled whenever the filter
is built) builds a buffer -> film_grain_adjust -> buffersink graph and
feeds frames with AV_BUFFERSRC_FLAG_KEEP_REF, so the filter chain and
the test hold separate AVFrame references to the same side data buffer
- exactly the shared-reference scenario the copy-on-write handling
addresses. The test verifies:
- the AV1 side data values are adjusted as requested (scale and
chroma_scale with saturation at 255, overlap_flag,
limit_output_range, scaling_shift, ar_coeff_shift, cb_mult,
cb_offset overrides) while every field not selected for adjustment
is preserved bit-exact;
- side data buffers shared with other frames (av_frame_clone and the
buffersrc KEEP_REF reference) are not modified, a regression test
for the copy-on-write handling;
- when a frame carries several film grain parameter sets (two AV1
sets with different video signal characteristics plus one H.274
set), every AV1 set is scaled independently with its own
copy-on-write buffer, the H.274 set and its metadata stay bit-exact,
the entry order and count are preserved, and
av_film_grain_params_select() still picks the expected set on the
output;
- runtime commands apply to the following frames; invalid values
(out-of-range scaling_shift, non-finite scale) are rejected with
EINVAL and leave the previous parameters in place;
- H.274 film grain parameters pass through untouched;
- frames without film grain side data pass through.
## Limitations
- The chroma point scaling follows the AV1 bitstream semantics, which
define the chroma points for 4:2:0; on other subsamplings the values
are adjusted the same way, but their interpretation is up to the
caller.
- The filter adjusts the parameters, it does not synthesise or remove
grain itself; synthesis is applied by the consumer (encoder or
renderer). An in-tree encoder consuming film grain side data is a
separate piece of work.
- High bit depth is not special-cased: the AV1 film grain parameters
(including the 8-bit point scaling values) are bitstream-defined and
are adjusted as such regardless of the pixel format.
## API
No new public API; a new AVFilter is registered. The filter is documented
in doc/filters.texi and covered by two FATE tests.
>From ab68283503be6439daf4bbda55fc8e73b2d94b19 Mon Sep 17 00:00:00 2001
From: luoye-cpu <[email protected]>
Date: Sun, 8 Nov 2026 23:32:37 +0800
Subject: [PATCH] avfilter: add film_grain_adjust filter
FFmpeg can produce AV1 film grain synthesis parameters as
AV_FRAME_DATA_FILM_GRAIN_PARAMS frame side data (native av1, libdav1d and
qsvdec, gated by AV_CODEC_EXPORT_DATA_FILM_GRAIN), it can consume them
(libplacebo synthesises them for display), and the sidedata filter can
delete such an entry wholesale. What it cannot do is modify the
parameters in flight: nothing between a decoder and the next stage of a
pipeline can rescale the grain amplitude or override individual fields.
The natural workflow "decode a grain-carrying master, dial the grain
down or up, feed the modified parameters to an encoder that consumes
film grain side data" is therefore not expressible today; re-encoding
through an analysis-based encoder re-estimates grain from scratch and
loses the authoring intent of the source.
Add the film_grain_adjust video filter to close that gap. The filter
layer (rather than a decoder option or a bitstream filter) is the
natural place for this: film grain parameters are a per-frame side data
property, the filter chain is the generic place where per-frame
properties can be rewritten between arbitrary sources and sinks, and a
bitstream filter operates on encoded bitstream units, not on frame side
data.
The filter is a pixel pass-through: it never touches picture data, it
only adjusts the film grain side data of each frame and forwards the
frame unchanged. Side data buffers are refcounted and shared between
frames referencing each other: av_frame_ref() and
av_frame_side_data_clone() only take an additional reference to the same
underlying AVBufferRef, and av_frame_make_writable() only covers the
picture buffers, not side data. A frame may carry several film grain
parameter sets as multiple AV_FRAME_DATA_FILM_GRAIN_PARAMS entries; every
AV1 set on the frame is adjusted. The filter therefore makes each
adjusted side data entry's buffer writable with av_buffer_make_writable()
before adjusting the parameters -- a no-op when the frame holds the only
reference, a copy-on-write otherwise -- so other frames sharing the
buffer are never affected. Because it does not read pixels it works the
same on hardware frames (e.g. cuda, qsv), which is the typical path for
this workflow.
The filter declares AVFILTER_FLAG_METADATA_ONLY (it never touches
picture data) and FF_FILTER_FLAG_DYNAMIC_FRAME_PARAMS: it carries no
frame parameter state and never accesses pixel data based on the size or
format of its input link, so frames changing size or format after the
graph was configured pass through unharmed (same class of filters as
marked in 58892c922e).
15 options are exposed: overall and chroma-only amplitude scales
('scale', 'chroma_scale') plus overrides for chroma_scaling_from_luma,
overlap_flag, limit_output_range, scaling_shift, ar_coeff_shift,
grain_scale_shift and the cb/cr scaling-function coefficients (cb_mult,
cr_mult, cb_luma_mult, cr_luma_mult, cb_offset, cr_offset). Overrides
use sentinels one below each field's valid range (-1, -129, -257) to
mean "keep the source value", so the help output shows a tight range per
option. The chroma scaling-function coefficients are only applied when
chroma_scaling_from_luma is zero and the corresponding plane has chroma
scaling points, mirroring the AV1 bitstream syntax where they are only
transmitted in that case. All options are runtime
parameters and can be changed through process_command while the filter
is running. Out-of-bitstream-range values of scaling_shift and
ar_coeff_shift are rejected explicitly with EINVAL in init() and
process_command() instead of being silently clipped, and the
end-of-stream summary is only logged when verbose is set.
The point scaling values (point_y_scaling / point_uv_scaling) are 8-bit
amplitude multipliers in the AV1 bitstream (u(8)); the effective grain
amplitude is this value times the generated grain, right-shifted by
scaling_shift (see add_noise_to_block() in libaom's grain_synthesis.c).
libaom, dav1d and the hardware APIs use the value verbatim, so the only
hard limit is the field width: amplitude scaling clamps to 0..255, the
scale/chroma_scale option range is capped at 255.0 (a factor of 255
saturates every non-zero source point) and scale=1.0 is a bit-exact
identity.
This is independent of, and complementary to, work on the production
side (e.g. exporting film grain side data from nvdec_av1): the filter
operates on whatever AV_FRAME_DATA_FILM_GRAIN_PARAMS entry a decoder
exports and passes it to whatever consumes it downstream. The chroma
subsampling av1dec exports for CUDA frames has been corrected in
3c1aad218f8b ("avcodec/av1dec: use sw_pix_fmt for film grain
subsampling"), so the side data this filter reads is consistent on that
path as well.
Signed-off-by: luoye-cpu <[email protected]>
---
doc/filters.texi | 148 +++++
libavfilter/Makefile | 1 +
libavfilter/allfilters.c | 1 +
libavfilter/version.h | 2 +-
libavfilter/vf_film_grain_adjust.c | 385 ++++++++++++
tests/api/Makefile | 1 +
tests/api/api-film-grain-adjust-test.c | 763 ++++++++++++++++++++++++
tests/fate/api.mak | 5 +
tests/fate/filter-video.mak | 3 +
tests/ref/fate/filter-film-grain-adjust | 30 +
10 files changed, 1338 insertions(+), 1 deletion(-)
create mode 100644 libavfilter/vf_film_grain_adjust.c
create mode 100644 tests/api/api-film-grain-adjust-test.c
create mode 100644 tests/ref/fate/filter-film-grain-adjust
diff --git a/doc/filters.texi b/doc/filters.texi
index ec23628067a..dae711c0d28 100644
--- a/doc/filters.texi
+++ b/doc/filters.texi
@@ -14640,6 +14640,154 @@ The command accepts the same syntax of the
corresponding option.
If the specified expression is not valid, it is kept at its current
value.
+@section film_grain_adjust
+
+Rewrite the AV1 film grain synthesis parameters carried on each frame as
+@code{AV_FRAME_DATA_FILM_GRAIN_PARAMS} side data, without touching the
+picture data.
+
+The filter is a pixel pass-through: it never reads or modifies the picture,
+it only adjusts the film grain side data of each frame and forwards the
+frame unchanged, making the buffer of each adjusted side data entry
+writable before adjusting it so that other frames sharing the buffer are
+unaffected. It therefore also works on hardware frames (e.g. @code{cuda},
+@code{qsv}).
+
+A frame may carry several film grain parameter sets as multiple
+@code{AV_FRAME_DATA_FILM_GRAIN_PARAMS} side data entries (alternative
+parameter sets for different video signal characteristics). All AV1 film
+grain parameter sets on the frame are adjusted independently, and each
+set's buffer is made writable individually; parameter sets of other types
+(e.g. H.274) and all other side data are passed through unchanged.
+
+This enables the "decode, adjust, encode" route: a decoder that exports film
+grain side data (e.g. via @code{-export_side_data +film_grain}) provides the
+source parameters, this filter rescales or overrides individual fields, and
+an encoder consuming film grain side data synthesises grain from the
+modified parameters.
+
+The chroma scaling-function coefficients (@option{cb_mult}, @option{cr_mult},
+@option{cb_luma_mult}, @option{cr_luma_mult}, @option{cb_offset},
+@option{cr_offset}) are only applied when @code{chroma_scaling_from_luma} is
+zero and the corresponding plane actually has chroma scaling points
+(@code{num_uv_points} greater than zero), mirroring the AV1 bitstream syntax
+where they are only transmitted in that case. When a plane has no scaling
+points its coefficients were not transmitted, and the overrides are not
+applied to that plane instead of being presented as valid results.
+
+Note that overriding @option{chroma_scaling_from_luma} from one to zero has
+limited effect when the chroma scaling function was derived from luma:
+source parameters with @code{chroma_scaling_from_luma} set do not contain
+independent chroma scaling points, overriding the flag to zero does not
+create those points, and clearing the flag only means the chroma scaling
+functions are no longer derived from luma. This filter provides no
+capability to create or rebuild chroma scaling points.
+
+This filter only modifies frame side data and never touches picture data;
+it can therefore be inserted anywhere in a filter graph without affecting
+the pixel output.
+
+This filter accepts the following options:
+
+@table @option
+@item scale
+Overall grain amplitude scale, multiplied onto every luma point scaling
+value. 1.0 keeps the source values unchanged, 0.0 disables grain, values
+greater than 1 amplify up to the 8-bit field limit. The point scaling
+values are 8-bit, so a factor of 255.0 saturates every non-zero source
+point and larger factors have no additional effect. Default is @var{1.0}.
+
+@item chroma_scale
+Chroma-only amplitude scale, multiplied onto every chroma point scaling
+value. A negative value follows @option{scale}. Default is @var{-1}.
+
+@item chroma_scaling_from_luma
+Override the chroma_scaling_from_luma flag. -1 keeps the source value.
+Default is @var{-1}.
+
+@item overlap_flag
+Override the grain block overlap flag. -1 keeps the source value.
+Default is @var{-1}.
+
+@item limit_output_range
+Override the clip_to_restricted_range flag. -1 keeps the source value.
+Default is @var{-1}.
+
+@item scaling_shift
+Override grain_scaling_minus_8, valid range 8..11. -1 keeps the source
+value. Values inside the option range but outside the valid range are
+rejected. Default is @var{-1}.
+
+@item ar_coeff_shift
+Override ar_coeff_shift, valid range 6..9. -1 keeps the source value.
+Values inside the option range but outside the valid range are rejected.
+Default is @var{-1}.
+
+@item grain_scale_shift
+Override grain_scale_shift, valid range 0..3. -1 keeps the source value.
+Default is @var{-1}.
+
+@item cb_mult
+Override cb_mult, valid range -128..127. -129 keeps the source value.
+Only applied when chroma_scaling_from_luma is zero and Cb has scaling
+points. Default is @var{-129}.
+
+@item cr_mult
+Override cr_mult, valid range -128..127. -129 keeps the source value.
+Only applied when chroma_scaling_from_luma is zero and Cr has scaling
+points. Default is @var{-129}.
+
+@item cb_luma_mult
+Override cb_luma_mult, valid range -128..127. -129 keeps the source value.
+Only applied when chroma_scaling_from_luma is zero and Cb has scaling
+points. Default is @var{-129}.
+
+@item cr_luma_mult
+Override cr_luma_mult, valid range -128..127. -129 keeps the source value.
+Only applied when chroma_scaling_from_luma is zero and Cr has scaling
+points. Default is @var{-129}.
+
+@item cb_offset
+Override cb_offset, valid range -256..255. -257 keeps the source value.
+Only applied when chroma_scaling_from_luma is zero and Cb has scaling
+points. Default is @var{-257}.
+
+@item cr_offset
+Override cr_offset, valid range -256..255. -257 keeps the source value.
+Only applied when chroma_scaling_from_luma is zero and Cr has scaling
+points. Default is @var{-257}.
+
+@item verbose
+Log the parameters of the first adjusted frame and a per-stream summary
+when the filter is closed. When several film grain parameter sets are
+present on that frame, the parameters of the set selected by
+@code{av_film_grain_params_select()}, i.e. the set a film grain synthesiser
+would consume, are logged. Default is disabled.
+@end table
+
+@subsection Commands
+This filter supports the same @ref{commands} as options.
+The command accepts the same syntax of the corresponding option.
+
+@subsection Examples
+
+@itemize
+@item
+Halve the grain amplitude of a source carrying film grain parameters:
+@example
+ffmpeg -c:v libdav1d -export_side_data +film_grain -i master.mkv \
+ -vf film_grain_adjust=scale=0.5 -c:v av1_nvenc -preset p5 out.mkv
+@end example
+
+@item
+Disable chroma grain and grain block overlap:
+@example
+ffmpeg -hwaccel cuda -export_side_data +film_grain -i master.mkv \
+ -vf film_grain_adjust=chroma_scale=0:overlap_flag=0 -c:v av1_nvenc
out.mkv
+@end example
+
+@end itemize
+
@section find_rect
Find a rectangular object in the input video.
diff --git a/libavfilter/Makefile b/libavfilter/Makefile
index dc5c083f013..a196cc1bb5d 100644
--- a/libavfilter/Makefile
+++ b/libavfilter/Makefile
@@ -321,6 +321,7 @@ OBJS-$(CONFIG_FIELDHINT_FILTER) +=
vf_fieldhint.o
OBJS-$(CONFIG_FIELDMATCH_FILTER) += vf_fieldmatch.o
OBJS-$(CONFIG_FIELDORDER_FILTER) += vf_fieldorder.o
OBJS-$(CONFIG_FILLBORDERS_FILTER) += vf_fillborders.o
+OBJS-$(CONFIG_FILM_GRAIN_ADJUST_FILTER) += vf_film_grain_adjust.o
OBJS-$(CONFIG_FIND_RECT_FILTER) += vf_find_rect.o lavfutils.o
OBJS-$(CONFIG_FLIP_VULKAN_FILTER) += vf_flip_vulkan.o vulkan.o
vulkan_filter.o
OBJS-$(CONFIG_FLOODFILL_FILTER) += vf_floodfill.o
diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c
index 9f07bf83dce..4a44ca9c90f 100644
--- a/libavfilter/allfilters.c
+++ b/libavfilter/allfilters.c
@@ -293,6 +293,7 @@ extern const FFFilter ff_vf_fieldhint;
extern const FFFilter ff_vf_fieldmatch;
extern const FFFilter ff_vf_fieldorder;
extern const FFFilter ff_vf_fillborders;
+extern const FFFilter ff_vf_film_grain_adjust;
extern const FFFilter ff_vf_find_rect;
extern const FFFilter ff_vf_flip_vulkan;
extern const FFFilter ff_vf_floodfill;
diff --git a/libavfilter/version.h b/libavfilter/version.h
index 64cd692ab67..537df129cd3 100644
--- a/libavfilter/version.h
+++ b/libavfilter/version.h
@@ -31,7 +31,7 @@
#include "version_major.h"
-#define LIBAVFILTER_VERSION_MINOR 12
+#define LIBAVFILTER_VERSION_MINOR 13
#define LIBAVFILTER_VERSION_MICRO 100
diff --git a/libavfilter/vf_film_grain_adjust.c
b/libavfilter/vf_film_grain_adjust.c
new file mode 100644
index 00000000000..2bda3e1733b
--- /dev/null
+++ b/libavfilter/vf_film_grain_adjust.c
@@ -0,0 +1,385 @@
+/*
+ * Film grain synthesis parameter adjustment filter (AV1)
+ *
+ * Adjusts the AV1 film grain synthesis parameters carried on each frame as
+ * AV_FRAME_DATA_FILM_GRAIN_PARAMS side data. A frame may carry several film
+ * grain parameter sets as multiple side data entries of that type (see
+ * libavutil/frame.h); every AV1 parameter set on the frame is adjusted,
+ * parameter sets of other types pass through untouched. This enables the
+ * "decode -> modify grain params -> encode" route: a decoder (libdav1d /
+ * native av1 / NVDEC / QSV) exports the source grain parameters, this filter
+ * rewrites them, and an encoder (av1_nvenc) synthesises grain from the
+ * modified parameters.
+ *
+ * The filter is a pixel pass-through: it does not touch picture data, it only
+ * edits the grain parameters in the side data of each frame and forwards the
+ * frame unchanged.
+ *
+ * Copyright (c) 2026
+ *
+ * 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
+ */
+
+#include <math.h>
+
+#include "libavutil/avstring.h"
+#include "libavutil/common.h"
+#include "libavutil/film_grain_params.h"
+#include "libavutil/opt.h"
+
+#include "avfilter.h"
+#include "filters.h"
+#include "video.h"
+
+/* "keep source value" sentinels. Each is one below the field's valid range so
+ * that -h film_grain_adjust shows a tight, meaningful range per option. */
+#define KEEP_MUL (-129) /* uv_mult / uv_mult_luma: valid [-128, 127] */
+#define KEEP_OFFSET (-257) /* uv_offset: valid [-256, 255] */
+
+typedef struct FilmGrainAdjustContext {
+ const AVClass *class;
+
+ /* amplitude */
+ double scale;
+ double chroma_scale;
+
+ /* overrides, negative / sentinel = keep source value */
+ int chroma_scaling_from_luma;
+ int overlap_flag;
+ int limit_output_range;
+ int scaling_shift;
+ int ar_coeff_shift;
+ int grain_scale_shift;
+ int cb_mult;
+ int cr_mult;
+ int cb_luma_mult;
+ int cr_luma_mult;
+ int cb_offset;
+ int cr_offset;
+
+ int verbose;
+ int logged;
+ int64_t frames;
+ int64_t frames_adjusted;
+ int64_t frames_nograin;
+ int64_t frames_other;
+} FilmGrainAdjustContext;
+
+static uint8_t scale_point(uint8_t v, double f)
+{
+ /* point scaling is an 8-bit amplitude multiplier (AV1 spec: u(8)); the
+ * effective grain amplitude is this value times the generated grain,
+ * right-shifted by scaling_shift (libaom: add_noise_to_block()). It is
+ * used verbatim by libaom, dav1d and the hardware APIs, so the only
+ * hard limit is the field width. */
+ return av_clip((int)lrint(v * f), 0, UINT8_MAX);
+}
+
+static void adjust_aom(AVFilmGrainAOMParams *a, const FilmGrainAdjustContext
*s)
+{
+ const double cs = (s->chroma_scale >= 0.0) ? s->chroma_scale : s->scale;
+ int c, i;
+
+ a->num_y_points = av_clip(a->num_y_points, 0, FF_ARRAY_ELEMS(a->y_points));
+ for (i = 0; i < a->num_y_points; i++)
+ a->y_points[i][1] = scale_point(a->y_points[i][1], s->scale);
+
+ for (c = 0; c < 2; c++) {
+ a->num_uv_points[c] = av_clip(a->num_uv_points[c], 0,
FF_ARRAY_ELEMS(a->uv_points[c]));
+ for (i = 0; i < a->num_uv_points[c]; i++)
+ a->uv_points[c][i][1] = scale_point(a->uv_points[c][i][1], cs);
+ }
+
+ if (s->chroma_scaling_from_luma >= 0)
+ a->chroma_scaling_from_luma = s->chroma_scaling_from_luma & 1;
+ if (s->overlap_flag >= 0)
+ a->overlap_flag = s->overlap_flag & 1;
+ if (s->limit_output_range >= 0)
+ a->limit_output_range = s->limit_output_range & 1;
+ /* the shift values have been validated in init() / process_command(),
+ * the clips below are a defensive no-op for valid input */
+ if (s->scaling_shift >= 0)
+ a->scaling_shift = av_clip(s->scaling_shift, 8, 11);
+ if (s->ar_coeff_shift >= 0)
+ a->ar_coeff_shift = av_clip(s->ar_coeff_shift, 6, 9);
+ if (s->grain_scale_shift >= 0)
+ a->grain_scale_shift = av_clip(s->grain_scale_shift, 0, 3);
+
+ /* chroma scaling-function coefficients are only present when the chroma
+ * scaling function is not derived from luma AND the corresponding plane
+ * actually has chroma scaling points: when num_uv_points[c] is zero the
+ * coefficients were not transmitted in the bitstream for that plane, so
+ * the overrides must not be presented as valid results for it */
+ if (!a->chroma_scaling_from_luma) {
+ if (a->num_uv_points[0]) {
+ if (s->cb_mult != KEEP_MUL) a->uv_mult[0] =
av_clip(s->cb_mult, -128, 127);
+ if (s->cb_luma_mult != KEEP_MUL) a->uv_mult_luma[0] =
av_clip(s->cb_luma_mult, -128, 127);
+ if (s->cb_offset != KEEP_OFFSET) a->uv_offset[0] =
av_clip(s->cb_offset, -256, 255);
+ }
+ if (a->num_uv_points[1]) {
+ if (s->cr_mult != KEEP_MUL) a->uv_mult[1] =
av_clip(s->cr_mult, -128, 127);
+ if (s->cr_luma_mult != KEEP_MUL) a->uv_mult_luma[1] =
av_clip(s->cr_luma_mult, -128, 127);
+ if (s->cr_offset != KEEP_OFFSET) a->uv_offset[1] =
av_clip(s->cr_offset, -256, 255);
+ }
+ }
+}
+
+/* the AVOption ranges of the shift options are deliberately wider than the
+ * values valid in the AV1 bitstream (they include the "keep source value"
+ * sentinel); values inside the AVOption range but outside the bitstream
+ * range are rejected instead of being silently clipped to the valid range */
+static int check_shift_opt(void *logctx, const char *name, long v,
+ int valid_min, int valid_max)
+{
+ if (v == -1 || (v >= valid_min && v <= valid_max))
+ return 0;
+ av_log(logctx, AV_LOG_ERROR,
+ "Invalid %s value %ld: must be -1 (keep source value) or %d..%d\n",
+ name, v, valid_min, valid_max);
+ return AVERROR(EINVAL);
+}
+
+static av_cold int init(AVFilterContext *ctx)
+{
+ FilmGrainAdjustContext *s = ctx->priv;
+ int ret;
+
+ /* non-finite factors must never reach lrint() in scale_point() */
+ if (!isfinite(s->scale) || !isfinite(s->chroma_scale)) {
+ av_log(ctx, AV_LOG_ERROR,
+ "scale and chroma_scale must be finite numbers\n");
+ return AVERROR(EINVAL);
+ }
+
+ ret = check_shift_opt(ctx, "scaling_shift", s->scaling_shift, 8, 11);
+ if (ret < 0)
+ return ret;
+ ret = check_shift_opt(ctx, "ar_coeff_shift", s->ar_coeff_shift, 6, 9);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
+static int process_command(AVFilterContext *ctx, const char *cmd, const char
*arg,
+ char *res, int res_len, int flags)
+{
+ if (!av_strcasecmp(cmd, "scale") || !av_strcasecmp(cmd, "chroma_scale")) {
+ /* reject non-finite factors before the option is updated, so a
+ * rejected command leaves the previous value in place */
+ char *end;
+ double v;
+
+ v = strtod(arg, &end);
+ while (*end == ' ' || *end == '\t')
+ end++;
+ if (end == arg || *end || !isfinite(v)) {
+ av_log(ctx, AV_LOG_ERROR,
+ "Invalid %s value '%s': must be a finite number\n",
+ cmd, arg);
+ return AVERROR(EINVAL);
+ }
+ }
+
+ if (!av_strcasecmp(cmd, "scaling_shift") || !av_strcasecmp(cmd,
"ar_coeff_shift")) {
+ const int is_scaling_shift = !av_strcasecmp(cmd, "scaling_shift");
+ const int valid_min = is_scaling_shift ? 8 : 6;
+ const int valid_max = is_scaling_shift ? 11 : 9;
+ char *end;
+ long v;
+ int ret;
+
+ v = strtol(arg, &end, 10);
+ while (*end == ' ' || *end == '\t')
+ end++;
+ if (end == arg || *end) {
+ av_log(ctx, AV_LOG_ERROR, "Invalid %s value '%s': not an
integer\n",
+ cmd, arg);
+ return AVERROR(EINVAL);
+ }
+ ret = check_shift_opt(ctx, cmd, v, valid_min, valid_max);
+ if (ret < 0)
+ return ret;
+ }
+
+ return ff_filter_process_command(ctx, cmd, arg, res, res_len, flags);
+}
+
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
+{
+ AVFilterContext *ctx = inlink->dst;
+ AVFilterLink *outlink = ctx->outputs[0];
+ FilmGrainAdjustContext *s = ctx->priv;
+ AVFilmGrainParams *first_adjusted = NULL;
+ int n_valid = 0; /* well-formed film grain side data entries */
+ int n_adjusted = 0; /* AV1 entries adjusted */
+ int ret;
+
+ s->frames++;
+
+ /* AV_FRAME_DATA_FILM_GRAIN_PARAMS may be present multiple times on a
+ * single frame, carrying alternative parameter sets (see
+ * libavutil/frame.h). Every AV1 parameter set is adjusted; sets of other
+ * types (e.g. H.274) pass through untouched. Only the buffer of an
+ * existing entry is ever replaced below, the side data array itself is
+ * not resized or reordered, so iterating over in->side_data[] is safe
+ * while entries are adjusted. */
+ for (int i = 0; i < in->nb_side_data; i++) {
+ AVFrameSideData *sd = in->side_data[i];
+ AVFilmGrainParams *fgp;
+
+ if (sd->type != AV_FRAME_DATA_FILM_GRAIN_PARAMS)
+ continue;
+ if (sd->size < sizeof(AVFilmGrainParams))
+ continue; /* malformed entry, treat like missing side data */
+
+ n_valid++;
+ fgp = (AVFilmGrainParams *)sd->data;
+ if (fgp->type != AV_FILM_GRAIN_PARAMS_AV1)
+ continue;
+
+ /* Side data buffers are refcounted and shared between frames that
+ * reference each other: av_frame_ref() and
+ * av_frame_side_data_clone() only take an additional reference to
+ * the same underlying AVBufferRef. Modifying the parameters in
+ * place would therefore leak into every other frame sharing that
+ * buffer, and av_frame_make_writable() only covers the picture
+ * buffers, not side data. Copy-on-write the side data buffer of
+ * each entry independently (a no-op when this frame holds the only
+ * reference). */
+ if (!av_buffer_is_writable(sd->buf)) {
+ ret = av_buffer_make_writable(&sd->buf);
+ if (ret < 0) {
+ av_frame_free(&in);
+ return ret;
+ }
+ /* the buffer may have been replaced, re-point the payload */
+ sd->data = sd->buf->data;
+ fgp = (AVFilmGrainParams *)sd->data;
+ }
+
+ adjust_aom(&fgp->codec.aom, s);
+ if (!first_adjusted)
+ first_adjusted = fgp;
+ n_adjusted++;
+ }
+
+ /* frame-level accounting: each frame is counted exactly once, no matter
+ * how many parameter sets it carries */
+ if (!n_valid) {
+ s->frames_nograin++;
+ } else if (n_adjusted) {
+ s->frames_adjusted++;
+ } else {
+ s->frames_other++;
+ }
+
+ if (s->verbose && !s->logged && n_adjusted) {
+ /* when several sets are present, report the one that
+ * av_film_grain_params_select() picks, i.e. the set a film grain
+ * synthesiser would actually consume; fall back to the first
+ * adjusted set if select() prefers a non-AV1 entry */
+ const AVFilmGrainParams *logp = av_film_grain_params_select(in);
+
+ if (!logp || logp->type != AV_FILM_GRAIN_PARAMS_AV1)
+ logp = first_adjusted;
+ av_log(ctx, AV_LOG_INFO,
+ "film_grain_adjust: first adjusted frame: "
+ "num_y_points=%d num_uv_points=%d/%d ar_coeff_lag=%d "
+ "scaling_shift=%d scale=%.3f chroma_scale=%.3f\n",
+ logp->codec.aom.num_y_points,
+ logp->codec.aom.num_uv_points[0],
logp->codec.aom.num_uv_points[1],
+ logp->codec.aom.ar_coeff_lag, logp->codec.aom.scaling_shift,
+ s->scale, s->chroma_scale);
+ s->logged = 1;
+ }
+
+ return ff_filter_frame(outlink, in);
+}
+
+static av_cold void uninit(AVFilterContext *ctx)
+{
+ FilmGrainAdjustContext *s = ctx->priv;
+
+ if (s->verbose)
+ av_log(ctx, AV_LOG_INFO,
+ "film_grain_adjust: frames=%"PRId64" adjusted=%"PRId64
+ " no_grain=%"PRId64" other_type=%"PRId64"\n",
+ s->frames, s->frames_adjusted, s->frames_nograin,
s->frames_other);
+}
+
+#define OFFSET(x) offsetof(FilmGrainAdjustContext, x)
+#define VF AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_FILTERING_PARAM |
AV_OPT_FLAG_RUNTIME_PARAM
+
+static const AVOption film_grain_adjust_options[] = {
+ { "scale", "overall grain amplitude scale: multiplies every point scaling
value (1.0 = unchanged, 0.0 = no grain, >1 amplifies up to the 8-bit field
limit)",
+ OFFSET(scale), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, 0.0, 255.0, VF },
+ { "chroma_scale", "chroma-only amplitude scale; negative = follow 'scale'",
+ OFFSET(chroma_scale), AV_OPT_TYPE_DOUBLE, {.dbl=-1.0}, -1.0, 255.0, VF },
+ { "chroma_scaling_from_luma", "override chroma_scaling_from_luma (-1 =
keep)",
+ OFFSET(chroma_scaling_from_luma), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, VF
},
+ { "overlap_flag", "override overlap_flag (-1 = keep)",
+ OFFSET(overlap_flag), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, VF },
+ { "limit_output_range", "override clip_to_restricted_range (-1 = keep)",
+ OFFSET(limit_output_range), AV_OPT_TYPE_INT, {.i64=-1}, -1, 1, VF },
+ { "scaling_shift", "override grain_scaling_minus_8+scaling_shift, valid
8..11 (-1 = keep, other values rejected)",
+ OFFSET(scaling_shift), AV_OPT_TYPE_INT, {.i64=-1}, -1, 11, VF },
+ { "ar_coeff_shift", "override ar_coeff_shift, valid 6..9 (-1 = keep, other
values rejected)",
+ OFFSET(ar_coeff_shift), AV_OPT_TYPE_INT, {.i64=-1}, -1, 9, VF },
+ { "grain_scale_shift", "override grain_scale_shift, valid 0..3 (-1 =
keep)",
+ OFFSET(grain_scale_shift), AV_OPT_TYPE_INT, {.i64=-1}, -1, 3, VF },
+ { "cb_mult", "override cb_mult, valid -128..127 (-129 = keep; only when
chroma_scaling_from_luma==0 and Cb has scaling points)",
+ OFFSET(cb_mult), AV_OPT_TYPE_INT, {.i64=KEEP_MUL}, KEEP_MUL, 127, VF },
+ { "cr_mult", "override cr_mult, valid -128..127 (-129 = keep; only when
chroma_scaling_from_luma==0 and Cr has scaling points)",
+ OFFSET(cr_mult), AV_OPT_TYPE_INT, {.i64=KEEP_MUL}, KEEP_MUL, 127, VF },
+ { "cb_luma_mult", "override cb_luma_mult, valid -128..127 (-129 = keep;
only when chroma_scaling_from_luma==0 and Cb has scaling points)",
+ OFFSET(cb_luma_mult), AV_OPT_TYPE_INT, {.i64=KEEP_MUL}, KEEP_MUL, 127,
VF },
+ { "cr_luma_mult", "override cr_luma_mult, valid -128..127 (-129 = keep;
only when chroma_scaling_from_luma==0 and Cr has scaling points)",
+ OFFSET(cr_luma_mult), AV_OPT_TYPE_INT, {.i64=KEEP_MUL}, KEEP_MUL, 127,
VF },
+ { "cb_offset", "override cb_offset, valid -256..255 (-257 = keep; only
when chroma_scaling_from_luma==0 and Cb has scaling points)",
+ OFFSET(cb_offset), AV_OPT_TYPE_INT, {.i64=KEEP_OFFSET}, KEEP_OFFSET,
255, VF },
+ { "cr_offset", "override cr_offset, valid -256..255 (-257 = keep; only
when chroma_scaling_from_luma==0 and Cr has scaling points)",
+ OFFSET(cr_offset), AV_OPT_TYPE_INT, {.i64=KEEP_OFFSET}, KEEP_OFFSET,
255, VF },
+ { "verbose", "log the first adjusted frame's parameters (the set
av_film_grain_params_select() picks when several are present) and a summary at
exit",
+ OFFSET(verbose), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, VF },
+ { NULL }
+};
+
+static const AVFilterPad inputs[] = {
+ {
+ .name = "default",
+ .type = AVMEDIA_TYPE_VIDEO,
+ .filter_frame = filter_frame,
+ },
+};
+
+AVFILTER_DEFINE_CLASS(film_grain_adjust);
+
+const FFFilter ff_vf_film_grain_adjust = {
+ .p.name = "film_grain_adjust",
+ .p.description = NULL_IF_CONFIG_SMALL("Adjust AV1 film grain synthesis
parameters carried as frame side data."),
+ .p.priv_class = &film_grain_adjust_class,
+ .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC |
+ AVFILTER_FLAG_METADATA_ONLY,
+ .priv_size = sizeof(FilmGrainAdjustContext),
+ .init = init,
+ .uninit = uninit,
+ FILTER_OUTPUTS(ff_video_default_filterpad),
+ FILTER_INPUTS(inputs),
+ .process_command = process_command,
+ .flags_internal = FF_FILTER_FLAG_DYNAMIC_FRAME_PARAMS,
+};
diff --git a/tests/api/Makefile b/tests/api/Makefile
index c411e82e5f1..87d899fc852 100644
--- a/tests/api/Makefile
+++ b/tests/api/Makefile
@@ -9,6 +9,7 @@ APITESTPROGS-$(call ALLYES, HEVC_MP4TOANNEXB_BSF HEVC_DECODER)
+= api-hevc-mp4to
APITESTPROGS-yes += api-seek api-dump-stream-meta
APITESTPROGS-$(call DEMDEC, H263, H263) += api-band
APITESTPROGS-$(HAVE_THREADS) += api-threadmessage
+APITESTPROGS-$(CONFIG_FILM_GRAIN_ADJUST_FILTER) += api-film-grain-adjust
APITESTPROGS-$(CONFIG_AVCODEC) += api-enc-info
APITESTPROGS-$(CONFIG_PCM_S16LE_DECODER) += api-skip-samples
APITESTPROGS-$(call ALLYES, H261_ENCODER H261_PARSER) += api-enc-parser
diff --git a/tests/api/api-film-grain-adjust-test.c
b/tests/api/api-film-grain-adjust-test.c
new file mode 100644
index 00000000000..b9c1bcc9b86
--- /dev/null
+++ b/tests/api/api-film-grain-adjust-test.c
@@ -0,0 +1,763 @@
+/*
+ * Copyright (c) 2026
+ *
+ * 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
+ */
+
+/*
+ * film_grain_adjust filter API test.
+ *
+ * Verifies that the film_grain_adjust filter
+ * - rewrites the AV1 film grain side data values as requested while
+ * preserving the fields that were not selected for adjustment,
+ * - does not modify side data buffers shared with other frames (regression
+ * test for the copy-on-write handling of the side data),
+ * - passes through H.274 film grain side data untouched,
+ * - passes through frames without film grain side data,
+ * - adjusts every AV1 film grain parameter set independently when a frame
+ * carries several of them, leaving the order, the entry count, other
+ * parameter set types and their metadata untouched,
+ * - applies runtime commands to the following frames and rejects invalid
+ * commands without partially updating the parameters.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "libavfilter/avfilter.h"
+#include "libavfilter/buffersink.h"
+#include "libavfilter/buffersrc.h"
+
+#include "libavutil/dict.h"
+#include "libavutil/film_grain_params.h"
+#include "libavutil/mem.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixfmt.h"
+
+#define WIDTH 64
+#define HEIGHT 64
+
+#define SRC_ARGS "video_size=" AV_STRINGIFY(WIDTH) "x" AV_STRINGIFY(HEIGHT) \
+ ":pix_fmt=yuv420p:time_base=1/25:pixel_aspect=1/1"
+
+static AVFrame *alloc_test_frame(void)
+{
+ AVFrame *f = av_frame_alloc();
+
+ if (!f)
+ return NULL;
+
+ f->format = AV_PIX_FMT_YUV420P;
+ f->width = WIDTH;
+ f->height = HEIGHT;
+
+ if (av_frame_get_buffer(f, 0) < 0) {
+ av_frame_free(&f);
+ return NULL;
+ }
+
+ return f;
+}
+
+/* build "buffer -> film_grain_adjust=<args> -> buffersink" and push one
+ * frame through it; the output frame is returned via *out */
+static int run_filter(AVFrame *in, AVFrame *out, const char *args)
+{
+ const AVFilter *buffersrc = avfilter_get_by_name("buffer");
+ const AVFilter *buffersink = avfilter_get_by_name("buffersink");
+ const AVFilter *fga = avfilter_get_by_name("film_grain_adjust");
+ AVFilterGraph *graph = NULL;
+ AVFilterContext *src_ctx = NULL;
+ AVFilterContext *sink_ctx = NULL;
+ AVFilterContext *fga_ctx = NULL;
+ int ret;
+
+ if (!buffersrc || !buffersink || !fga)
+ return AVERROR(ENOSYS);
+
+ graph = avfilter_graph_alloc();
+ if (!graph)
+ return AVERROR(ENOMEM);
+
+ ret = avfilter_graph_create_filter(&src_ctx, buffersrc, "src",
+ SRC_ARGS, NULL, graph);
+ if (ret < 0)
+ goto fail;
+
+ ret = avfilter_graph_create_filter(&fga_ctx, fga, "fga", args, NULL,
graph);
+ if (ret < 0)
+ goto fail;
+
+ ret = avfilter_graph_create_filter(&sink_ctx, buffersink, "sink",
+ NULL, NULL, graph);
+ if (ret < 0)
+ goto fail;
+
+ ret = avfilter_link(src_ctx, 0, fga_ctx, 0);
+ if (ret < 0)
+ goto fail;
+
+ ret = avfilter_link(fga_ctx, 0, sink_ctx, 0);
+ if (ret < 0)
+ goto fail;
+
+ ret = avfilter_graph_config(graph, NULL);
+ if (ret < 0)
+ goto fail;
+
+ /* KEEP_REF: the frame handed to the filter chain shares its side data
+ * buffers with the caller's frame, which is exactly the situation the
+ * copy-on-write handling must cope with */
+ ret = av_buffersrc_add_frame_flags(src_ctx, in,
AV_BUFFERSRC_FLAG_KEEP_REF);
+ if (ret < 0)
+ goto fail;
+
+ ret = av_buffersink_get_frame(sink_ctx, out);
+
+fail:
+ avfilter_graph_free(&graph);
+ return ret;
+}
+
+static void fill_aom_params(AVFilmGrainAOMParams *a)
+{
+ *a = (AVFilmGrainAOMParams) {
+ .num_y_points = 3,
+ .y_points = { { 10, 100 }, { 20, 150 }, { 30, 64 } },
+ .chroma_scaling_from_luma = 0,
+ .num_uv_points = { 2, 1 },
+ .uv_points = { { { 40, 40 }, { 60, 80 } }, { { 50, 200 } } },
+ .scaling_shift = 9,
+ .ar_coeff_lag = 2,
+ .ar_coeff_shift = 7,
+ .grain_scale_shift = 1,
+ .uv_mult = { 5, 3 },
+ .uv_mult_luma = { 2, 4 },
+ .uv_offset = { 15, 25 },
+ .overlap_flag = 1,
+ .limit_output_range = 0,
+ };
+}
+
+#define CHECK(expr, desc) do { \
+ if (!(expr)) { \
+ fprintf(stderr, "check failed: %s (line %d)\n", \
+ desc, __LINE__); \
+ ret = AVERROR(EINVAL); \
+ goto fail; \
+ } \
+} while (0)
+
+/* expected point scalings with scale=2.0 / chroma_scale=3.0: 100->200,
+ * 150->300 (clipped to 255), 64->128; 40->120, 80->240, 200->600 (clipped) */
+static int test_aom_adjust_and_isolation(void)
+{
+ AVFrame *in = NULL;
+ AVFrame *clone = NULL;
+ AVFrame *out = NULL;
+ AVFilmGrainParams *ref = NULL;
+ AVFilmGrainParams *wfgp; /* writable, used to fill the side data
*/
+ const AVFrameSideData *sd;
+ const AVFilmGrainParams *fgp;
+ int ret;
+
+ in = alloc_test_frame();
+ if (!in)
+ return AVERROR(ENOMEM);
+
+ wfgp = av_film_grain_params_create_side_data(in);
+ if (!wfgp) {
+ av_frame_free(&in);
+ return AVERROR(ENOMEM);
+ }
+ wfgp->type = AV_FILM_GRAIN_PARAMS_AV1;
+ wfgp->seed = 0x12345678;
+ fill_aom_params(&wfgp->codec.aom);
+
+ /* keep a copy of the input side data for the isolation checks */
+ sd = av_frame_get_side_data(in, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
+ ref = av_memdup(sd->data, sd->size);
+ if (!ref) {
+ av_frame_free(&in);
+ return AVERROR(ENOMEM);
+ }
+
+ /* a second AVFrame sharing the same side data buffer; the filter must
+ * not let its modifications leak into it */
+ clone = av_frame_clone(in);
+ if (!clone) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ out = av_frame_alloc();
+ if (!out) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ ret = run_filter(in, out,
+ "scale=2.0:chroma_scale=3.0:overlap_flag=0"
+ ":limit_output_range=1:scaling_shift=10"
+ ":ar_coeff_shift=8:cb_mult=10:cb_offset=20");
+ if (ret < 0)
+ goto fail;
+
+ sd = av_frame_get_side_data(out, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
+ CHECK(sd, "output lost the film grain side data");
+ CHECK(sd->size == sizeof(AVFilmGrainParams), "side data size changed");
+ fgp = (const AVFilmGrainParams *)sd->data;
+ CHECK(fgp->type == AV_FILM_GRAIN_PARAMS_AV1, "side data type changed");
+ CHECK(fgp->seed == 0x12345678, "seed changed");
+
+ CHECK(fgp->codec.aom.num_y_points == 3, "num_y_points changed");
+ CHECK(fgp->codec.aom.y_points[0][0] == 10 &&
+ fgp->codec.aom.y_points[0][1] == 200, "y_points[0] not scaled by
2.0");
+ CHECK(fgp->codec.aom.y_points[1][0] == 20 &&
+ fgp->codec.aom.y_points[1][1] == 255,
+ "y_points[1] not scaled and clipped to 255");
+ CHECK(fgp->codec.aom.y_points[2][0] == 30 &&
+ fgp->codec.aom.y_points[2][1] == 128, "y_points[2] not scaled by
2.0");
+ CHECK(fgp->codec.aom.uv_points[0][0][1] == 120,
+ "uv_points[0][0] not scaled by 3.0");
+ CHECK(fgp->codec.aom.uv_points[0][1][1] == 240,
+ "uv_points[0][1] not scaled by 3.0");
+ CHECK(fgp->codec.aom.uv_points[1][0][1] == 255,
+ "uv_points[1][0] not scaled and clipped to 255");
+
+ CHECK(fgp->codec.aom.scaling_shift == 10, "scaling_shift override not
applied");
+ CHECK(fgp->codec.aom.ar_coeff_shift == 8, "ar_coeff_shift override not
applied");
+ CHECK(fgp->codec.aom.overlap_flag == 0, "overlap_flag override not
applied");
+ CHECK(fgp->codec.aom.limit_output_range == 1,
+ "limit_output_range override not applied");
+ CHECK(fgp->codec.aom.uv_mult[0] == 10, "cb_mult override not applied");
+ CHECK(fgp->codec.aom.uv_offset[0] == 20, "cb_offset override not applied");
+
+ /* fields not selected for adjustment must be preserved */
+ CHECK(fgp->codec.aom.ar_coeff_lag == 2, "ar_coeff_lag not preserved");
+ CHECK(fgp->codec.aom.grain_scale_shift == 1, "grain_scale_shift not
preserved");
+ CHECK(fgp->codec.aom.uv_mult[1] == 3, "cr_mult (keep) not preserved");
+ CHECK(fgp->codec.aom.uv_mult_luma[0] == 2 &&
+ fgp->codec.aom.uv_mult_luma[1] == 4, "uv_mult_luma (keep) not
preserved");
+ CHECK(fgp->codec.aom.uv_offset[1] == 25, "cr_offset (keep) not preserved");
+ CHECK(fgp->codec.aom.ar_coeffs_y[0] == 0, "ar_coeffs_y not preserved");
+ CHECK(fgp->codec.aom.chroma_scaling_from_luma == 0,
+ "chroma_scaling_from_luma not preserved");
+
+ /* the input frame and its clone share the side data buffer; both must
+ * still hold the exact original parameters */
+ sd = av_frame_get_side_data(in, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
+ CHECK(sd && sd->size == sizeof(AVFilmGrainParams) &&
+ !memcmp(sd->data, ref, sizeof(AVFilmGrainParams)),
+ "side data of the input frame was modified (copy-on-write broken)");
+ sd = av_frame_get_side_data(clone, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
+ CHECK(sd && sd->size == sizeof(AVFilmGrainParams) &&
+ !memcmp(sd->data, ref, sizeof(AVFilmGrainParams)),
+ "side data of a frame sharing the buffer was modified "
+ "(copy-on-write broken)");
+
+ ret = 0;
+fail:
+ av_frame_free(&out);
+ av_frame_free(&clone);
+ av_frame_free(&in);
+ av_freep(&ref);
+ return ret;
+}
+
+static int test_h274_passthrough(void)
+{
+ AVFrame *in = NULL;
+ AVFrame *out = NULL;
+ AVFilmGrainParams *ref = NULL;
+ const AVFrameSideData *sd;
+ AVFilmGrainParams *fgp;
+ int ret;
+
+ in = alloc_test_frame();
+ if (!in)
+ return AVERROR(ENOMEM);
+
+ fgp = av_film_grain_params_create_side_data(in);
+ if (!fgp) {
+ av_frame_free(&in);
+ return AVERROR(ENOMEM);
+ }
+ fgp->type = AV_FILM_GRAIN_PARAMS_H274;
+ fgp->seed = 0xdeadbeef;
+ fgp->codec.h274.model_id = 1;
+ fgp->codec.h274.blending_mode_id = 0;
+ fgp->codec.h274.log2_scale_factor = 2;
+ fgp->codec.h274.component_model_present[0] = 1;
+ fgp->codec.h274.num_intensity_intervals[0] = 1;
+ fgp->codec.h274.num_model_values[0] = 3;
+ fgp->codec.h274.intensity_interval_lower_bound[0][0] = 0;
+ fgp->codec.h274.intensity_interval_upper_bound[0][0] = 255;
+ fgp->codec.h274.comp_model_value[0][0][0] = -100;
+ fgp->codec.h274.comp_model_value[0][0][1] = 0;
+ fgp->codec.h274.comp_model_value[0][0][2] = 100;
+
+ sd = av_frame_get_side_data(in, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
+ ref = av_memdup(sd->data, sd->size);
+ if (!ref) {
+ av_frame_free(&in);
+ return AVERROR(ENOMEM);
+ }
+
+ out = av_frame_alloc();
+ if (!out) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ /* non-default amplitude settings must not touch H.274 parameters */
+ ret = run_filter(in, out, "scale=2.0:chroma_scale=3.0");
+ if (ret < 0)
+ goto fail;
+
+ sd = av_frame_get_side_data(out, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
+ CHECK(sd && sd->size == sizeof(AVFilmGrainParams) &&
+ !memcmp(sd->data, ref, sizeof(AVFilmGrainParams)),
+ "H.274 film grain parameters were modified");
+
+ ret = 0;
+fail:
+ av_frame_free(&out);
+ av_freep(&ref);
+ av_frame_free(&in);
+ return ret;
+}
+
+static int test_no_side_data(void)
+{
+ AVFrame *in = NULL;
+ AVFrame *out = NULL;
+ int ret;
+
+ in = alloc_test_frame();
+ if (!in)
+ return AVERROR(ENOMEM);
+
+ out = av_frame_alloc();
+ if (!out) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ ret = run_filter(in, out, "scale=2.0");
+ if (ret < 0)
+ goto fail;
+
+ CHECK(av_frame_get_side_data(out, AV_FRAME_DATA_FILM_GRAIN_PARAMS) == NULL,
+ "a film grain side data appeared on a frame that had none");
+
+ ret = 0;
+fail:
+ av_frame_free(&out);
+ av_frame_free(&in);
+ return ret;
+}
+
+/* a frame may carry several film grain parameter sets as multiple
+ * AV_FRAME_DATA_FILM_GRAIN_PARAMS entries (see libavutil/frame.h). Build a
+ * frame with two AV1 sets and one H.274 set and verify that every AV1 set
+ * is scaled independently, the H.274 set and its metadata stay bit-exact,
+ * the input buffers are not touched, select() still picks the expected set,
+ * and the entry order/count is preserved. */
+static int test_multiple_sets(void)
+{
+ AVFrame *in = NULL;
+ AVFrame *clone = NULL;
+ AVFrame *out = NULL;
+ AVFilmGrainParams *ref[3] = { NULL, NULL, NULL };
+ AVFilmGrainParams *wfgp;
+ const AVFrameSideData *sd_out[3] = { NULL, NULL, NULL };
+ const AVFilmGrainParams *fgp;
+ const AVFilmGrainParams *sel;
+ AVDictionaryEntry *e;
+ int ret, i, n_fgs;
+
+ in = alloc_test_frame();
+ if (!in)
+ return AVERROR(ENOMEM);
+
+ /* entry 0: AV1 set A, declared for the full frame size; select() must
+ * keep preferring it over set B, which declares a smaller size */
+ wfgp = av_film_grain_params_create_side_data(in);
+ if (!wfgp) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+ wfgp->type = AV_FILM_GRAIN_PARAMS_AV1;
+ wfgp->width = WIDTH;
+ wfgp->height = HEIGHT;
+ wfgp->seed = 0xAAAA0001;
+ wfgp->subsampling_x = 1; /* match the yuv420p test frame */
+ wfgp->subsampling_y = 1;
+ wfgp->codec.aom.num_y_points = 2;
+ wfgp->codec.aom.y_points[0][0] = 10;
+ wfgp->codec.aom.y_points[0][1] = 100;
+ wfgp->codec.aom.y_points[1][0] = 20;
+ wfgp->codec.aom.y_points[1][1] = 150;
+ wfgp->codec.aom.chroma_scaling_from_luma = 0;
+ wfgp->codec.aom.num_uv_points[0] = 2;
+ wfgp->codec.aom.num_uv_points[1] = 1;
+ wfgp->codec.aom.uv_points[0][0][0] = 40;
+ wfgp->codec.aom.uv_points[0][0][1] = 40;
+ wfgp->codec.aom.uv_points[0][1][0] = 60;
+ wfgp->codec.aom.uv_points[0][1][1] = 80;
+ wfgp->codec.aom.uv_points[1][0][0] = 50;
+ wfgp->codec.aom.uv_points[1][0][1] = 200;
+ wfgp->codec.aom.scaling_shift = 9;
+
+ /* entry 1: AV1 set B with different video signal characteristics and a
+ * smaller declared size, so both AV1 sets are valid candidates for
+ * select() but set A wins on resolution */
+ wfgp = av_film_grain_params_create_side_data(in);
+ if (!wfgp) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+ wfgp->type = AV_FILM_GRAIN_PARAMS_AV1;
+ wfgp->width = WIDTH / 2;
+ wfgp->height = HEIGHT / 2;
+ wfgp->seed = 0xBBBB0002;
+ wfgp->subsampling_x = 1; /* match the yuv420p test frame */
+ wfgp->subsampling_y = 1;
+ wfgp->bit_depth_luma = 10;
+ wfgp->bit_depth_chroma = 10;
+ wfgp->color_primaries = AVCOL_PRI_BT709;
+ wfgp->color_trc = AVCOL_TRC_BT709;
+ wfgp->color_space = AVCOL_SPC_BT709;
+ wfgp->color_range = AVCOL_RANGE_MPEG;
+ wfgp->codec.aom.num_y_points = 3;
+ wfgp->codec.aom.y_points[0][0] = 30;
+ wfgp->codec.aom.y_points[0][1] = 50;
+ wfgp->codec.aom.y_points[1][0] = 40;
+ wfgp->codec.aom.y_points[1][1] = 60;
+ wfgp->codec.aom.y_points[2][0] = 50;
+ wfgp->codec.aom.y_points[2][1] = 70;
+ wfgp->codec.aom.chroma_scaling_from_luma = 0;
+ wfgp->codec.aom.num_uv_points[0] = 1;
+ wfgp->codec.aom.uv_points[0][0][0] = 70;
+ wfgp->codec.aom.uv_points[0][0][1] = 70;
+
+ /* entry 2: H.274 set, must stay bit-exact; carries metadata that must
+ * be preserved untouched */
+ wfgp = av_film_grain_params_create_side_data(in);
+ if (!wfgp) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+ wfgp->type = AV_FILM_GRAIN_PARAMS_H274;
+ wfgp->seed = 0xCCCC0003;
+ wfgp->codec.h274.model_id = 1;
+ wfgp->codec.h274.log2_scale_factor = 2;
+ wfgp->codec.h274.component_model_present[0] = 1;
+ wfgp->codec.h274.num_intensity_intervals[0] = 1;
+ wfgp->codec.h274.num_model_values[0] = 3;
+ wfgp->codec.h274.comp_model_value[0][0][0] = -100;
+ wfgp->codec.h274.comp_model_value[0][0][1] = 0;
+ wfgp->codec.h274.comp_model_value[0][0][2] = 100;
+ av_dict_set(&in->side_data[2]->metadata, "film_grain_adjust_test", "h274",
0);
+
+ /* keep copies of all three entries for the isolation checks */
+ for (i = 0; i < 3; i++) {
+ ref[i] = av_memdup(in->side_data[i]->data, in->side_data[i]->size);
+ if (!ref[i]) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+ }
+
+ /* a second AVFrame sharing all three side data buffers */
+ clone = av_frame_clone(in);
+ if (!clone) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ out = av_frame_alloc();
+ if (!out) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ ret = run_filter(in, out, "scale=2.0:chroma_scale=3.0");
+ if (ret < 0)
+ goto fail;
+
+ /* the output must carry the same three entries, in the same order */
+ CHECK(out->nb_side_data == 3, "side data entry count changed");
+ n_fgs = 0;
+ for (i = 0; i < out->nb_side_data; i++)
+ if (out->side_data[i]->type == AV_FRAME_DATA_FILM_GRAIN_PARAMS)
+ sd_out[n_fgs++] = out->side_data[i];
+ CHECK(n_fgs == 3, "film grain side data entries lost on the output");
+
+ /* entry 0 (set A): luma points scaled by 2.0, chroma by 3.0 */
+ fgp = (const AVFilmGrainParams *)sd_out[0]->data;
+ CHECK(fgp->type == AV_FILM_GRAIN_PARAMS_AV1, "entry 0 type changed");
+ CHECK(fgp->seed == 0xAAAA0001, "entry 0 seed changed");
+ CHECK(fgp->codec.aom.y_points[0][0] == 10 &&
+ fgp->codec.aom.y_points[0][1] == 200, "set A y_points[0] not
scaled");
+ CHECK(fgp->codec.aom.y_points[1][0] == 20 &&
+ fgp->codec.aom.y_points[1][1] == 255,
+ "set A y_points[1] not scaled and clipped");
+ CHECK(fgp->codec.aom.uv_points[0][0][1] == 120 &&
+ fgp->codec.aom.uv_points[0][1][1] == 240 &&
+ fgp->codec.aom.uv_points[1][0][1] == 255, "set A uv_points not
scaled");
+
+ /* entry 1 (set B): scaled independently of set A */
+ fgp = (const AVFilmGrainParams *)sd_out[1]->data;
+ CHECK(fgp->type == AV_FILM_GRAIN_PARAMS_AV1, "entry 1 type changed");
+ CHECK(fgp->seed == 0xBBBB0002, "entry 1 seed changed");
+ CHECK(fgp->codec.aom.y_points[0][0] == 30 &&
+ fgp->codec.aom.y_points[0][1] == 100, "set B y_points[0] not
scaled");
+ CHECK(fgp->codec.aom.y_points[1][1] == 120, "set B y_points[1] not
scaled");
+ CHECK(fgp->codec.aom.y_points[2][1] == 140, "set B y_points[2] not
scaled");
+ CHECK(fgp->codec.aom.uv_points[0][0][1] == 210, "set B uv_points not
scaled");
+
+ /* entry 2 (H.274): bit-exact, metadata preserved */
+ fgp = (const AVFilmGrainParams *)sd_out[2]->data;
+ CHECK(fgp->type == AV_FILM_GRAIN_PARAMS_H274, "entry 2 type changed");
+ CHECK(sd_out[2]->size == sizeof(AVFilmGrainParams) &&
+ !memcmp(sd_out[2]->data, ref[2], sizeof(AVFilmGrainParams)),
+ "H.274 set was modified");
+ e = av_dict_get(sd_out[2]->metadata, "film_grain_adjust_test", NULL, 0);
+ CHECK(e && !strcmp(e->value, "h274"), "entry 2 metadata was lost");
+
+ /* select() must still pick set A (largest declared resolution among
+ * valid candidates) and it must expose the adjusted values */
+ sel = av_film_grain_params_select(out);
+ CHECK(sel && sel->type == AV_FILM_GRAIN_PARAMS_AV1 &&
+ sel->seed == 0xAAAA0001, "select() did not pick set A");
+ CHECK(sel->codec.aom.y_points[0][1] == 200, "selected set was not
adjusted");
+
+ /* each AV1 entry must have been copy-on-written independently: the
+ * three output buffers are distinct and none is shared with the input
+ * frame anymore */
+ CHECK(sd_out[0]->buf != sd_out[1]->buf &&
+ sd_out[0]->buf != sd_out[2]->buf &&
+ sd_out[1]->buf != sd_out[2]->buf,
+ "adjusted entries share a buffer (not independently CoW'd)");
+ CHECK(in->side_data[0]->buf != sd_out[0]->buf &&
+ in->side_data[1]->buf != sd_out[1]->buf,
+ "AV1 entries were modified in place");
+
+ /* the input frame and its clone must still hold all three original
+ * buffers, bit-exact and in order */
+ for (i = 0; i < 3; i++) {
+ CHECK(in->side_data[i]->size == sizeof(AVFilmGrainParams) &&
+ !memcmp(in->side_data[i]->data, ref[i],
sizeof(AVFilmGrainParams)),
+ "input frame side data was modified");
+ CHECK(clone->side_data[i]->size == sizeof(AVFilmGrainParams) &&
+ !memcmp(clone->side_data[i]->data, ref[i],
sizeof(AVFilmGrainParams)),
+ "frame sharing the buffers was modified");
+ }
+
+ ret = 0;
+fail:
+ av_frame_free(&out);
+ av_frame_free(&clone);
+ av_frame_free(&in);
+ for (i = 0; i < 3; i++)
+ av_freep(&ref[i]);
+ return ret;
+}
+
+/* runtime commands: a valid command takes effect on the next frame, while
+ * invalid commands are rejected with EINVAL and must not partially update
+ * the parameters */
+static int test_runtime_command(void)
+{
+ const AVFilter *buffersrc = avfilter_get_by_name("buffer");
+ const AVFilter *buffersink = avfilter_get_by_name("buffersink");
+ const AVFilter *fga = avfilter_get_by_name("film_grain_adjust");
+ AVFilterGraph *graph = NULL;
+ AVFilterContext *src_ctx = NULL;
+ AVFilterContext *sink_ctx = NULL;
+ AVFilterContext *fga_ctx = NULL;
+ AVFrame *frames[3] = { NULL, NULL, NULL };
+ AVFrame *out = NULL;
+ const AVFrameSideData *sd;
+ const AVFilmGrainParams *fgp;
+ int ret, i;
+
+ if (!buffersrc || !buffersink || !fga)
+ return AVERROR(ENOSYS);
+
+ graph = avfilter_graph_alloc();
+ if (!graph)
+ return AVERROR(ENOMEM);
+
+ ret = avfilter_graph_create_filter(&src_ctx, buffersrc, "src",
+ SRC_ARGS, NULL, graph);
+ if (ret < 0)
+ goto fail;
+
+ /* default options: scale=1.0 */
+ ret = avfilter_graph_create_filter(&fga_ctx, fga, "fga", "", NULL, graph);
+ if (ret < 0)
+ goto fail;
+
+ ret = avfilter_graph_create_filter(&sink_ctx, buffersink, "sink",
+ NULL, NULL, graph);
+ if (ret < 0)
+ goto fail;
+
+ ret = avfilter_link(src_ctx, 0, fga_ctx, 0);
+ if (ret < 0)
+ goto fail;
+
+ ret = avfilter_link(fga_ctx, 0, sink_ctx, 0);
+ if (ret < 0)
+ goto fail;
+
+ ret = avfilter_graph_config(graph, NULL);
+ if (ret < 0)
+ goto fail;
+
+ /* three identical grain frames (y_points[0][1] == 100,
+ * scaling_shift == 9) */
+ for (i = 0; i < 3; i++) {
+ AVFilmGrainParams *wfgp;
+
+ frames[i] = alloc_test_frame();
+ if (!frames[i]) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+ wfgp = av_film_grain_params_create_side_data(frames[i]);
+ if (!wfgp) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+ wfgp->type = AV_FILM_GRAIN_PARAMS_AV1;
+ wfgp->seed = 0x11000000 + (unsigned)i;
+ fill_aom_params(&wfgp->codec.aom);
+ }
+
+ out = av_frame_alloc();
+ if (!out) {
+ ret = AVERROR(ENOMEM);
+ goto fail;
+ }
+
+ /* frame 0: default options, parameters unchanged */
+ ret = av_buffersrc_add_frame_flags(src_ctx, frames[0],
+ AV_BUFFERSRC_FLAG_KEEP_REF);
+ if (ret < 0)
+ goto fail;
+ ret = av_buffersink_get_frame(sink_ctx, out);
+ if (ret < 0)
+ goto fail;
+ sd = av_frame_get_side_data(out, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
+ fgp = (const AVFilmGrainParams *)sd->data;
+ CHECK(fgp->codec.aom.y_points[0][1] == 100,
+ "scale=1.0 changed the parameters");
+ CHECK(fgp->codec.aom.scaling_shift == 9, "source scaling_shift changed");
+ av_frame_unref(out);
+
+ /* a valid command applies to the following frames */
+ ret = avfilter_graph_send_command(graph, "fga", "scale", "0.5",
+ NULL, 0, 0);
+ CHECK(ret >= 0, "valid scale command rejected");
+
+ ret = av_buffersrc_add_frame_flags(src_ctx, frames[1],
+ AV_BUFFERSRC_FLAG_KEEP_REF);
+ if (ret < 0)
+ goto fail;
+ ret = av_buffersink_get_frame(sink_ctx, out);
+ if (ret < 0)
+ goto fail;
+ sd = av_frame_get_side_data(out, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
+ fgp = (const AVFilmGrainParams *)sd->data;
+ CHECK(fgp->codec.aom.y_points[0][1] == 50, "scale=0.5 not applied");
+ av_frame_unref(out);
+
+ /* out-of-range scaling_shift: rejected, nothing partially updated */
+ ret = avfilter_graph_send_command(graph, "fga", "scaling_shift", "7",
+ NULL, 0, 0);
+ CHECK(ret == AVERROR(EINVAL), "invalid scaling_shift accepted");
+
+ /* non-finite factors must be rejected as well */
+ ret = avfilter_graph_send_command(graph, "fga", "scale", "nan",
+ NULL, 0, 0);
+ CHECK(ret == AVERROR(EINVAL), "non-finite scale accepted");
+
+ ret = av_buffersrc_add_frame_flags(src_ctx, frames[2],
+ AV_BUFFERSRC_FLAG_KEEP_REF);
+ if (ret < 0)
+ goto fail;
+ ret = av_buffersink_get_frame(sink_ctx, out);
+ if (ret < 0)
+ goto fail;
+ sd = av_frame_get_side_data(out, AV_FRAME_DATA_FILM_GRAIN_PARAMS);
+ fgp = (const AVFilmGrainParams *)sd->data;
+ CHECK(fgp->codec.aom.y_points[0][1] == 50,
+ "rejected commands partially updated the parameters");
+ CHECK(fgp->codec.aom.scaling_shift == 9,
+ "rejected scaling_shift changed the parameters");
+
+ ret = 0;
+fail:
+ av_frame_free(&out);
+ for (i = 0; i < 3; i++)
+ av_frame_free(&frames[i]);
+ avfilter_graph_free(&graph);
+ return ret;
+}
+
+int main(void)
+{
+ int ret;
+
+ ret = test_aom_adjust_and_isolation();
+ if (ret < 0) {
+ fprintf(stderr, "test_aom_adjust_and_isolation failed\n");
+ return 1;
+ }
+
+ ret = test_h274_passthrough();
+ if (ret < 0) {
+ fprintf(stderr, "test_h274_passthrough failed\n");
+ return 1;
+ }
+
+ ret = test_no_side_data();
+ if (ret < 0) {
+ fprintf(stderr, "test_no_side_data failed\n");
+ return 1;
+ }
+
+ ret = test_multiple_sets();
+ if (ret < 0) {
+ fprintf(stderr, "test_multiple_sets failed\n");
+ return 1;
+ }
+
+ ret = test_runtime_command();
+ if (ret < 0) {
+ fprintf(stderr, "test_runtime_command failed\n");
+ return 1;
+ }
+
+ fprintf(stderr, "api-film-grain-adjust-test: OK\n");
+
+ return 0;
+}
diff --git a/tests/fate/api.mak b/tests/fate/api.mak
index 1659c7979c0..9e6ed545c8d 100644
--- a/tests/fate/api.mak
+++ b/tests/fate/api.mak
@@ -59,6 +59,11 @@ fate-api-threadmessage:
$(APITESTSDIR)/api-threadmessage-test$(EXESUF)
fate-api-threadmessage: CMD = run
$(APITESTSDIR)/api-threadmessage-test$(EXESUF) 3 10 30 50 2 20 40
fate-api-threadmessage: CMP = null
+FATE_API-$(CONFIG_FILM_GRAIN_ADJUST_FILTER) += fate-api-film-grain-adjust
+fate-api-film-grain-adjust: $(APITESTSDIR)/api-film-grain-adjust-test$(EXESUF)
+fate-api-film-grain-adjust: CMD = run
$(APITESTSDIR)/api-film-grain-adjust-test$(EXESUF)
+fate-api-film-grain-adjust: CMP = null
+
FATE_API_SAMPLES-$(CONFIG_AVFORMAT) += $(FATE_API_SAMPLES_LIBAVFORMAT-yes)
ifdef SAMPLES
diff --git a/tests/fate/filter-video.mak b/tests/fate/filter-video.mak
index 82ab48dc68d..8deefa57e00 100644
--- a/tests/fate/filter-video.mak
+++ b/tests/fate/filter-video.mak
@@ -1020,6 +1020,9 @@ fate-filter-xfade-slideup16: CMD = framecrc
-filter_complex "color=c=red:s=8x8:r
FATE_FILTER-$(call FILTERFRAMECRC, COLOR XFADE FORMAT) +=
fate-filter-xfade-duration0
fate-filter-xfade-duration0: CMD = framecrc -filter_complex
"color=c=red:s=8x8:r=8:d=4,format=yuv444p[a];color=c=0x0000ff:s=8x8:r=8:d=4,format=yuv444p[b];[a][b]xfade=transition=squeezeh:offset=1:duration=0"
+FATE_FILTER-$(call FILTERFRAMECRC, TESTSRC2 FILM_GRAIN_ADJUST) +=
fate-filter-film-grain-adjust
+fate-filter-film-grain-adjust: CMD = framecrc -flags bitexact -f lavfi -i
testsrc2=cif:d=1 -vf
film_grain_adjust=scale=0.5:chroma_scale=0:overlap_flag=0:limit_output_range=1:ar_coeff_shift=6:scaling_shift=11
-frames:v 25
+
FATE_SAMPLES_FFPROBE += $(FATE_METADATA_FILTER-yes)
FATE_SAMPLES_FFMPEG += $(FATE_FILTER_SAMPLES-yes)
FATE_FFPROBE += $(FATE_FILTER_FFPROBE-yes)
diff --git a/tests/ref/fate/filter-film-grain-adjust
b/tests/ref/fate/filter-film-grain-adjust
new file mode 100644
index 00000000000..d3ae3333420
--- /dev/null
+++ b/tests/ref/fate/filter-film-grain-adjust
@@ -0,0 +1,30 @@
+#tb 0: 1/25
+#media_type 0: video
+#codec_id 0: rawvideo
+#dimensions 0: 352x288
+#sar 0: 1/1
+0, 0, 0, 1, 152064, 0x53022f4b
+0, 1, 1, 1, 152064, 0xfc4a6ac2
+0, 2, 2, 1, 152064, 0x26c7c206
+0, 3, 3, 1, 152064, 0xe2aef2d1
+0, 4, 4, 1, 152064, 0x906c21cd
+0, 5, 5, 1, 152064, 0x70303034
+0, 6, 6, 1, 152064, 0xe1bb274d
+0, 7, 7, 1, 152064, 0xdb03155b
+0, 8, 8, 1, 152064, 0x655808dc
+0, 9, 9, 1, 152064, 0xa6ac0127
+0, 10, 10, 1, 152064, 0x4e4f150c
+0, 11, 11, 1, 152064, 0x601ef77d
+0, 12, 12, 1, 152064, 0xdbc3f47f
+0, 13, 13, 1, 152064, 0x293ef835
+0, 14, 14, 1, 152064, 0xbc7f01af
+0, 15, 15, 1, 152064, 0xbb78091e
+0, 16, 16, 1, 152064, 0xabb711e7
+0, 17, 17, 1, 152064, 0x31562358
+0, 18, 18, 1, 152064, 0x607334e4
+0, 19, 19, 1, 152064, 0x97e432ce
+0, 20, 20, 1, 152064, 0x4bd742da
+0, 21, 21, 1, 152064, 0x2a1d1cab
+0, 22, 22, 1, 152064, 0x0cd2131c
+0, 23, 23, 1, 152064, 0x0ff1043a
+0, 24, 24, 1, 152064, 0x0698efe6
--
2.54.0
_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]