FFmpeg
film_grain_params.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <stdio.h>
20 
22 #include "libavutil/mem.h"
23 
25 {
27  if (!frame)
28  return NULL;
29  frame->format = format;
30  frame->width = width;
31  frame->height = height;
32  return frame;
33 }
34 
37  int width, int height,
38  int sub_x, int sub_y,
39  int bd_luma, int bd_chroma)
40 {
42  if (!fgp)
43  return NULL;
44  fgp->type = type;
45  fgp->width = width;
46  fgp->height = height;
47  fgp->subsampling_x = sub_x;
48  fgp->subsampling_y = sub_y;
49  fgp->bit_depth_luma = bd_luma;
50  fgp->bit_depth_chroma = bd_chroma;
51  return fgp;
52 }
53 
54 int main(void)
55 {
56  AVFilmGrainParams *fgp;
57  const AVFilmGrainParams *sel;
58  AVFrame *frame;
59  size_t size;
60 
61  printf("Testing av_film_grain_params_alloc()\n");
62 
64  printf("alloc with size: %s\n", (fgp && size > 0) ? "OK" : "FAIL");
65  av_free(fgp);
66 
68  printf("alloc without size: %s\n", fgp ? "OK" : "FAIL");
69  av_free(fgp);
70 
71  printf("\nTesting av_film_grain_params_create_side_data()\n");
72 
75  if (fgp)
76  printf("create: OK\ndefaults: range=%d pri=%d trc=%d space=%d\n",
77  fgp->color_range, fgp->color_primaries,
78  fgp->color_trc, fgp->color_space);
79  else
80  printf("create: FAIL\n");
82 
83  printf("\nTesting av_film_grain_params_select()\n");
84 
85  /* invalid format */
87  frame->format = -1;
89  printf("invalid format: %s\n", sel ? "FAIL" : "NULL");
91 
92  /* no side data */
93  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
95  printf("no side data: %s\n", sel ? "FAIL" : "NULL");
97 
98  /* NONE type - skipped */
99  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
100  add_grain(frame, AV_FILM_GRAIN_PARAMS_NONE, 0, 0, 1, 1, 0, 0);
102  printf("NONE type: %s\n", sel ? "FAIL" : "NULL");
104 
105  /* AV1 exact subsampling match (YUV420P: sub 1,1) */
106  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
107  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
109  printf("AV1 match: %s\n", sel ? "OK" : "FAIL");
111 
112  /* AV1 subsampling mismatch */
113  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
114  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 0, 0, 0, 0);
116  printf("AV1 sub mismatch: %s\n", sel ? "FAIL" : "NULL");
118 
119  /* H274 exact match */
120  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
121  add_grain(frame, AV_FILM_GRAIN_PARAMS_H274, 0, 0, 1, 1, 0, 0);
123  printf("H274 match: %s\n", sel ? "OK" : "FAIL");
125 
126  /* H274 lower subsampling OK (grain sub 0,0 < YUV420P sub 1,1) */
127  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
128  add_grain(frame, AV_FILM_GRAIN_PARAMS_H274, 0, 0, 0, 0, 0, 0);
130  printf("H274 lower sub: %s\n", sel ? "OK" : "FAIL");
132 
133  /* H274 higher subsampling FAIL (grain sub 1,1 > YUV444P sub 0,0) */
134  frame = create_frame(AV_PIX_FMT_YUV444P, 1920, 1080);
135  add_grain(frame, AV_FILM_GRAIN_PARAMS_H274, 0, 0, 1, 1, 0, 0);
137  printf("H274 higher sub: %s\n", sel ? "FAIL" : "NULL");
139 
140  /* width too large */
141  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
142  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 3840, 0, 1, 1, 0, 0);
144  printf("width too large: %s\n", sel ? "FAIL" : "NULL");
146 
147  /* height too large */
148  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
149  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 2160, 1, 1, 0, 0);
151  printf("height too large: %s\n", sel ? "FAIL" : "NULL");
153 
154  /* width/height = 0 (unspecified) - passes */
155  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
156  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
158  printf("size unspecified: %s\n", sel ? "OK" : "FAIL");
160 
161  /* bit_depth_luma mismatch (grain=10, YUV420P=8) */
162  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
163  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 10, 0);
165  printf("bd_luma mismatch: %s\n", sel ? "FAIL" : "NULL");
167 
168  /* bit_depth_chroma mismatch (grain=10, YUV420P=8) */
169  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
170  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 10);
172  printf("bd_chroma mismatch: %s\n", sel ? "FAIL" : "NULL");
174 
175  /* bit_depth = 0 (unspecified) - passes */
176  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
177  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
179  printf("bd unspecified: %s\n", sel ? "OK" : "FAIL");
181 
182  /* bit_depth exact match (grain=8, YUV420P=8) */
183  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
184  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 8, 8);
186  printf("bd exact match: %s\n", sel ? "OK" : "FAIL");
188 
189  /* color_range mismatch */
190  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
191  frame->color_range = AVCOL_RANGE_MPEG;
192  fgp = add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
195  printf("color_range mismatch: %s\n", sel ? "FAIL" : "NULL");
197 
198  /* color_primaries mismatch */
199  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
200  frame->color_primaries = AVCOL_PRI_BT709;
201  fgp = add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
204  printf("color_primaries mismatch: %s\n", sel ? "FAIL" : "NULL");
206 
207  /* color_trc mismatch */
208  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
209  frame->color_trc = AVCOL_TRC_BT709;
210  fgp = add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
213  printf("color_trc mismatch: %s\n", sel ? "FAIL" : "NULL");
215 
216  /* color_space mismatch */
217  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
218  frame->colorspace = AVCOL_SPC_BT709;
219  fgp = add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
222  printf("color_space mismatch: %s\n", sel ? "FAIL" : "NULL");
224 
225  /* color properties UNSPECIFIED - passes */
226  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
227  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
229  printf("color unspecified: %s\n", sel ? "OK" : "FAIL");
231 
232  /* multiple entries - best selection by size */
233  frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
234  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 640, 480, 1, 1, 0, 0);
235  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 1280, 720, 1, 1, 0, 0);
236  add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 1000, 1080, 1, 1, 0, 0);
238  printf("best selection: width=%d height=%d\n",
239  sel ? sel->width : -1, sel ? sel->height : -1);
241 
242  return 0;
243 }
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
printf
__device__ int printf(const char *,...)
AVFilmGrainParams::bit_depth_luma
int bit_depth_luma
Intended bit depth, or 0 for unknown/unspecified.
Definition: film_grain_params.h:238
av_film_grain_params_alloc
AVFilmGrainParams * av_film_grain_params_alloc(size_t *size)
This file is part of FFmpeg.
Definition: film_grain_params.c:23
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:434
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:777
AVFilmGrainParams::color_space
enum AVColorSpace color_space
Definition: film_grain_params.h:233
AVFilmGrainParams::color_trc
enum AVColorTransferCharacteristic color_trc
Definition: film_grain_params.h:232
AVCOL_SPC_BT470BG
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition: pixfmt.h:706
av_film_grain_params_select
const AVFilmGrainParams * av_film_grain_params_select(const AVFrame *frame)
Select the most appropriate film grain parameters set for the frame, taking into account the frame's ...
Definition: film_grain_params.c:53
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AVCOL_TRC_GAMMA22
@ AVCOL_TRC_GAMMA22
also ITU-R BT470M / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:671
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:52
AVFilmGrainParams::bit_depth_chroma
int bit_depth_chroma
Definition: film_grain_params.h:239
film_grain_params.h
AVFilmGrainParams::width
int width
Intended display resolution.
Definition: film_grain_params.h:220
create_frame
static AVFrame * create_frame(enum AVPixelFormat format, int width, int height)
Definition: film_grain_params.c:24
av_film_grain_params_create_side_data
AVFilmGrainParams * av_film_grain_params_create_side_data(AVFrame *frame)
Allocate a complete AVFilmGrainParams and add it to the frame.
Definition: film_grain_params.c:33
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:73
AV_FILM_GRAIN_PARAMS_NONE
@ AV_FILM_GRAIN_PARAMS_NONE
Definition: film_grain_params.h:25
NULL
#define NULL
Definition: coverity.c:32
format
New swscale design to change SwsGraph is what coordinates multiple passes These can include cascaded scaling error diffusion and so on Or we could have separate passes for the vertical and horizontal scaling In between each SwsPass lies a fully allocated image buffer Graph passes may have different levels of e g we can have a single threaded error diffusion pass following a multi threaded scaling pass SwsGraph is internally recreated whenever the image format
Definition: swscale-v2.txt:14
AVCOL_PRI_BT709
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition: pixfmt.h:638
AVFilmGrainParams::subsampling_x
int subsampling_x
Intended subsampling ratio, or 0 for luma-only streams.
Definition: film_grain_params.h:225
height
#define height
Definition: dsp.h:89
size
int size
Definition: twinvq_data.h:10344
AVFilmGrainParams
This structure describes how to handle film grain synthesis in video for specific codecs.
Definition: film_grain_params.h:201
AVCOL_TRC_BT709
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition: pixfmt.h:668
AVFilmGrainParams::color_primaries
enum AVColorPrimaries color_primaries
Definition: film_grain_params.h:231
AVFilmGrainParams::subsampling_y
int subsampling_y
Definition: film_grain_params.h:225
AVFilmGrainParamsType
AVFilmGrainParamsType
Definition: film_grain_params.h:24
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:760
AVFilmGrainParams::height
int height
Definition: film_grain_params.h:220
AVCOL_PRI_BT470M
@ AVCOL_PRI_BT470M
also FCC Title 47 Code of Federal Regulations 73.682 (a)(20)
Definition: pixfmt.h:641
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:265
AV_FILM_GRAIN_PARAMS_H274
@ AV_FILM_GRAIN_PARAMS_H274
The union is valid when interpreted as AVFilmGrainH274Params (codec.h274)
Definition: film_grain_params.h:35
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AVFilmGrainParams::color_range
enum AVColorRange color_range
Intended video signal characteristics.
Definition: film_grain_params.h:230
main
int main(void)
Definition: film_grain_params.c:54
mem.h
add_grain
static AVFilmGrainParams * add_grain(AVFrame *frame, enum AVFilmGrainParamsType type, int width, int height, int sub_x, int sub_y, int bd_luma, int bd_chroma)
Definition: film_grain_params.c:35
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
width
#define width
Definition: dsp.h:89
AVCOL_SPC_BT709
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition: pixfmt.h:702
AV_FILM_GRAIN_PARAMS_AV1
@ AV_FILM_GRAIN_PARAMS_AV1
The union is valid when interpreted as AVFilmGrainAOMParams (codec.aom)
Definition: film_grain_params.h:30
AVFilmGrainParams::type
enum AVFilmGrainParamsType type
Specifies the codec for which this structure is valid.
Definition: film_grain_params.h:205