History

avutil/film_grain_params: add av_film_grain_params_select()

Common utility function that can be used by all codecs to select the right (any valid) film grain parameter set. In particular, this is useful for AFGS1, which has support for multiple parameters. However, it also performs parameter validation for H274.

Niklas Haas committed 2 years ago

Commit a9023371 parent ea147f34 files · +76 −1Browse files

Unified · Split

4 files
  1. doc/APIchanges +3 −0
  2. libavutil/film_grain_params.c +61 −0
  3. libavutil/film_grain_params.h +11 −0
  4. libavutil/version.h +1 −1
doc/APIchanges+3−0
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2024-03-07 · 1 unmodified line
22
33 API changes, most recent first:
44
5+2024-03-xx - xxxxxxxxxx - lavu 59.6.100 - film_grain_params.h
6+ Add av_film_grain_params_select().
7+
58 2024-03-xx - xxxxxxxxxx - lavu 59.5.100 - film_grain_params.h
69 Add AVFilmGrainParams.color_range, color_primaries, color_trc, color_space,
710 width, height, subsampling_x, subsampling_y, bit_depth_luma and
libavutil/film_grain_params.c+61−0
@@ -17,6 +17,7 @@ · 16 unmodified lines
1717 */
1818
1919 #include "film_grain_params.h"
20+#include "pixdesc.h"
2021
2122 AVFilmGrainParams *av_film_grain_params_alloc(size_t *size)
2223 {
@@ -47,3 +48,63 @@ AVFilmGrainParams *av_film_grain_params_create_side_data(AVFrame *frame) · 24 unmodified lines
4748
4849 return fgp;
4950 }
51+
52+const AVFilmGrainParams *av_film_grain_params_select(const AVFrame *frame)
53+{
54+ const AVFilmGrainParams *fgp, *best = NULL;
55+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
56+ const AVFilmGrainAOMParams *aom;
57+ const AVFilmGrainH274Params *h274;
58+ int bit_depth_luma, bit_depth_chroma;
59+ if (!desc)
60+ return NULL;
61+
62+ /* There are no YUV formats with different bit depth per component,
63+ * so just check both against the first component for simplicity */
64+ bit_depth_luma = bit_depth_chroma = desc->comp[0].depth;
65+
66+ for (int i = 0; i < frame->nb_side_data; i++) {
67+ if (frame->side_data[i]->type != AV_FRAME_DATA_FILM_GRAIN_PARAMS)
68+ continue;
69+ fgp = (const AVFilmGrainParams*)frame->side_data[i]->data;
70+ if (fgp->width && fgp->width > frame->width ||
71+ fgp->height && fgp->height > frame->height)
72+ continue;
73+
74+#define CHECK(a, b, unspec) \
75+ do { \
76+ if ((a) != (unspec) && (b) != (unspec) && (a) != (b)) \
77+ continue; \
78+ } while (0)
79+
80+ CHECK(fgp->bit_depth_luma, bit_depth_luma, 0);
81+ CHECK(fgp->bit_depth_chroma, bit_depth_chroma, 0);
82+ CHECK(fgp->color_range, frame->color_range, AVCOL_RANGE_UNSPECIFIED);
83+ CHECK(fgp->color_primaries, frame->color_primaries, AVCOL_PRI_UNSPECIFIED);
84+ CHECK(fgp->color_trc, frame->color_trc, AVCOL_TRC_UNSPECIFIED);
85+ CHECK(fgp->color_space, frame->colorspace, AVCOL_SPC_UNSPECIFIED);
86+
87+ switch (fgp->type) {
88+ case AV_FILM_GRAIN_PARAMS_NONE:
89+ continue;
90+ case AV_FILM_GRAIN_PARAMS_AV1:
91+ aom = &fgp->codec.aom;
92+ /* AOM FGS needs an exact match for the chroma resolution */
93+ if (fgp->subsampling_x != desc->log2_chroma_w ||
94+ fgp->subsampling_y != desc->log2_chroma_h)
95+ continue;
96+ break;
97+ case AV_FILM_GRAIN_PARAMS_H274:
98+ /* H.274 FGS can be adapted to any lower chroma resolution */
99+ if (fgp->subsampling_x > desc->log2_chroma_w ||
100+ fgp->subsampling_y > desc->log2_chroma_h)
101+ continue;
102+ break;
103+ }
104+
105+ if (!best || best->width < fgp->width || best->height < fgp->height)
106+ best = fgp;
107+ }
108+
109+ return best;
110+}
libavutil/film_grain_params.h+11−0
@@ -308,4 +308,15 @@ AVFilmGrainParams *av_film_grain_params_alloc(size_t *size); · 307 unmodified lines
308308 */
309309 AVFilmGrainParams *av_film_grain_params_create_side_data(AVFrame *frame);
310310
311+/**
312+ * Select the most appropriate film grain parameters set for the frame,
313+ * taking into account the frame's format, resolution and video signal
314+ * characteristics.
315+ *
316+ * @note, for H.274, this may select a film grain parameter set with
317+ * greater chroma resolution than the frame. Users should take care to
318+ * correctly adjust the chroma grain frequency to the frame.
319+ */
320+const AVFilmGrainParams *av_film_grain_params_select(const AVFrame *frame);
321+
311322 #endif /* AVUTIL_FILM_GRAIN_PARAMS_H */
libavutil/version.h+1−1
@@ -79,7 +79,7 @@ · 78 unmodified lines
7979 */
8080
8181 #define LIBAVUTIL_VERSION_MAJOR 59
82−#define LIBAVUTIL_VERSION_MINOR 5
82+#define LIBAVUTIL_VERSION_MINOR 6
8383 #define LIBAVUTIL_VERSION_MICRO 100
8484
8585 #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t