FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/film_grain_params.c
Date: 2026-05-05 08:36:23
Exec Total Coverage
Lines: 150 153 98.0%
Functions: 3 3 100.0%
Branches: 28 56 50.0%

Line Branch Exec Source
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
21 #include "libavutil/film_grain_params.h"
22 #include "libavutil/mem.h"
23
24 20 static AVFrame *create_frame(enum AVPixelFormat format, int width, int height)
25 {
26 20 AVFrame *frame = av_frame_alloc();
27
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!frame)
28 return NULL;
29 20 frame->format = format;
30 20 frame->width = width;
31 20 frame->height = height;
32 20 return frame;
33 }
34
35 21 static AVFilmGrainParams *add_grain(AVFrame *frame,
36 enum AVFilmGrainParamsType type,
37 int width, int height,
38 int sub_x, int sub_y,
39 int bd_luma, int bd_chroma)
40 {
41 21 AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(frame);
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21 times.
21 if (!fgp)
43 return NULL;
44 21 fgp->type = type;
45 21 fgp->width = width;
46 21 fgp->height = height;
47 21 fgp->subsampling_x = sub_x;
48 21 fgp->subsampling_y = sub_y;
49 21 fgp->bit_depth_luma = bd_luma;
50 21 fgp->bit_depth_chroma = bd_chroma;
51 21 return fgp;
52 }
53
54 1 int main(void)
55 {
56 AVFilmGrainParams *fgp;
57 const AVFilmGrainParams *sel;
58 AVFrame *frame;
59 size_t size;
60
61 1 printf("Testing av_film_grain_params_alloc()\n");
62
63 1 fgp = av_film_grain_params_alloc(&size);
64
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 printf("alloc with size: %s\n", (fgp && size > 0) ? "OK" : "FAIL");
65 1 av_free(fgp);
66
67 1 fgp = av_film_grain_params_alloc(NULL);
68
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("alloc without size: %s\n", fgp ? "OK" : "FAIL");
69 1 av_free(fgp);
70
71 1 printf("\nTesting av_film_grain_params_create_side_data()\n");
72
73 1 frame = av_frame_alloc();
74 1 fgp = av_film_grain_params_create_side_data(frame);
75
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (fgp)
76 1 printf("create: OK\ndefaults: range=%d pri=%d trc=%d space=%d\n",
77 1 fgp->color_range, fgp->color_primaries,
78 1 fgp->color_trc, fgp->color_space);
79 else
80 printf("create: FAIL\n");
81 1 av_frame_free(&frame);
82
83 1 printf("\nTesting av_film_grain_params_select()\n");
84
85 /* invalid format */
86 1 frame = av_frame_alloc();
87 1 frame->format = -1;
88 1 sel = av_film_grain_params_select(frame);
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("invalid format: %s\n", sel ? "FAIL" : "NULL");
90 1 av_frame_free(&frame);
91
92 /* no side data */
93 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
94 1 sel = av_film_grain_params_select(frame);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("no side data: %s\n", sel ? "FAIL" : "NULL");
96 1 av_frame_free(&frame);
97
98 /* NONE type - skipped */
99 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
100 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_NONE, 0, 0, 1, 1, 0, 0);
101 1 sel = av_film_grain_params_select(frame);
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("NONE type: %s\n", sel ? "FAIL" : "NULL");
103 1 av_frame_free(&frame);
104
105 /* AV1 exact subsampling match (YUV420P: sub 1,1) */
106 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
107 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
108 1 sel = av_film_grain_params_select(frame);
109
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("AV1 match: %s\n", sel ? "OK" : "FAIL");
110 1 av_frame_free(&frame);
111
112 /* AV1 subsampling mismatch */
113 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
114 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 0, 0, 0, 0);
115 1 sel = av_film_grain_params_select(frame);
116
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("AV1 sub mismatch: %s\n", sel ? "FAIL" : "NULL");
117 1 av_frame_free(&frame);
118
119 /* H274 exact match */
120 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
121 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_H274, 0, 0, 1, 1, 0, 0);
122 1 sel = av_film_grain_params_select(frame);
123
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("H274 match: %s\n", sel ? "OK" : "FAIL");
124 1 av_frame_free(&frame);
125
126 /* H274 lower subsampling OK (grain sub 0,0 < YUV420P sub 1,1) */
127 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
128 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_H274, 0, 0, 0, 0, 0, 0);
129 1 sel = av_film_grain_params_select(frame);
130
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("H274 lower sub: %s\n", sel ? "OK" : "FAIL");
131 1 av_frame_free(&frame);
132
133 /* H274 higher subsampling FAIL (grain sub 1,1 > YUV444P sub 0,0) */
134 1 frame = create_frame(AV_PIX_FMT_YUV444P, 1920, 1080);
135 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_H274, 0, 0, 1, 1, 0, 0);
136 1 sel = av_film_grain_params_select(frame);
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("H274 higher sub: %s\n", sel ? "FAIL" : "NULL");
138 1 av_frame_free(&frame);
139
140 /* width too large */
141 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
142 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 3840, 0, 1, 1, 0, 0);
143 1 sel = av_film_grain_params_select(frame);
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("width too large: %s\n", sel ? "FAIL" : "NULL");
145 1 av_frame_free(&frame);
146
147 /* height too large */
148 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
149 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 2160, 1, 1, 0, 0);
150 1 sel = av_film_grain_params_select(frame);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("height too large: %s\n", sel ? "FAIL" : "NULL");
152 1 av_frame_free(&frame);
153
154 /* width/height = 0 (unspecified) - passes */
155 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
156 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
157 1 sel = av_film_grain_params_select(frame);
158
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("size unspecified: %s\n", sel ? "OK" : "FAIL");
159 1 av_frame_free(&frame);
160
161 /* bit_depth_luma mismatch (grain=10, YUV420P=8) */
162 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
163 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 10, 0);
164 1 sel = av_film_grain_params_select(frame);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("bd_luma mismatch: %s\n", sel ? "FAIL" : "NULL");
166 1 av_frame_free(&frame);
167
168 /* bit_depth_chroma mismatch (grain=10, YUV420P=8) */
169 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
170 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 10);
171 1 sel = av_film_grain_params_select(frame);
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("bd_chroma mismatch: %s\n", sel ? "FAIL" : "NULL");
173 1 av_frame_free(&frame);
174
175 /* bit_depth = 0 (unspecified) - passes */
176 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
177 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
178 1 sel = av_film_grain_params_select(frame);
179
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("bd unspecified: %s\n", sel ? "OK" : "FAIL");
180 1 av_frame_free(&frame);
181
182 /* bit_depth exact match (grain=8, YUV420P=8) */
183 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
184 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 8, 8);
185 1 sel = av_film_grain_params_select(frame);
186
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("bd exact match: %s\n", sel ? "OK" : "FAIL");
187 1 av_frame_free(&frame);
188
189 /* color_range mismatch */
190 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
191 1 frame->color_range = AVCOL_RANGE_MPEG;
192 1 fgp = add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
193 1 fgp->color_range = AVCOL_RANGE_JPEG;
194 1 sel = av_film_grain_params_select(frame);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("color_range mismatch: %s\n", sel ? "FAIL" : "NULL");
196 1 av_frame_free(&frame);
197
198 /* color_primaries mismatch */
199 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
200 1 frame->color_primaries = AVCOL_PRI_BT709;
201 1 fgp = add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
202 1 fgp->color_primaries = AVCOL_PRI_BT470M;
203 1 sel = av_film_grain_params_select(frame);
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("color_primaries mismatch: %s\n", sel ? "FAIL" : "NULL");
205 1 av_frame_free(&frame);
206
207 /* color_trc mismatch */
208 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
209 1 frame->color_trc = AVCOL_TRC_BT709;
210 1 fgp = add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
211 1 fgp->color_trc = AVCOL_TRC_GAMMA22;
212 1 sel = av_film_grain_params_select(frame);
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("color_trc mismatch: %s\n", sel ? "FAIL" : "NULL");
214 1 av_frame_free(&frame);
215
216 /* color_space mismatch */
217 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
218 1 frame->colorspace = AVCOL_SPC_BT709;
219 1 fgp = add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
220 1 fgp->color_space = AVCOL_SPC_BT470BG;
221 1 sel = av_film_grain_params_select(frame);
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 printf("color_space mismatch: %s\n", sel ? "FAIL" : "NULL");
223 1 av_frame_free(&frame);
224
225 /* color properties UNSPECIFIED - passes */
226 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
227 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 0, 0, 1, 1, 0, 0);
228 1 sel = av_film_grain_params_select(frame);
229
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 printf("color unspecified: %s\n", sel ? "OK" : "FAIL");
230 1 av_frame_free(&frame);
231
232 /* multiple entries - best selection by size */
233 1 frame = create_frame(AV_PIX_FMT_YUV420P, 1920, 1080);
234 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 640, 480, 1, 1, 0, 0);
235 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 1280, 720, 1, 1, 0, 0);
236 1 add_grain(frame, AV_FILM_GRAIN_PARAMS_AV1, 1000, 1080, 1, 1, 0, 0);
237 1 sel = av_film_grain_params_select(frame);
238
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 printf("best selection: width=%d height=%d\n",
239 sel ? sel->width : -1, sel ? sel->height : -1);
240 1 av_frame_free(&frame);
241
242 1 return 0;
243 }
244