FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vsrc_mptestsrc.c
Date: 2024-04-19 07:31:02
Exec Total Coverage
Lines: 101 150 67.3%
Functions: 10 15 66.7%
Branches: 54 99 54.5%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 /**
22 * @file
23 * MP test source, ported from MPlayer libmpcodecs/vf_test.c
24 */
25
26 #include "libavutil/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "avfilter.h"
29 #include "internal.h"
30 #include "video.h"
31
32 #define WIDTH 512
33 #define HEIGHT 512
34
35 enum test_type {
36 TEST_DC_LUMA,
37 TEST_DC_CHROMA,
38 TEST_FREQ_LUMA,
39 TEST_FREQ_CHROMA,
40 TEST_AMP_LUMA,
41 TEST_AMP_CHROMA,
42 TEST_CBP,
43 TEST_MV,
44 TEST_RING1,
45 TEST_RING2,
46 TEST_ALL,
47 TEST_NB
48 };
49
50 typedef struct MPTestContext {
51 const AVClass *class;
52 AVRational frame_rate;
53 int64_t pts, max_pts, duration;
54 int64_t max_frames;
55 int hsub, vsub;
56 int test; ///< test_type
57 } MPTestContext;
58
59 #define OFFSET(x) offsetof(MPTestContext, x)
60 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
61 static const AVOption mptestsrc_options[]= {
62 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
63 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
64 { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
65 { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
66
67 { "test", "set test to perform", OFFSET(test), AV_OPT_TYPE_INT, {.i64=TEST_ALL}, 0, INT_MAX, FLAGS, .unit = "test" },
68 { "t", "set test to perform", OFFSET(test), AV_OPT_TYPE_INT, {.i64=TEST_ALL}, 0, INT_MAX, FLAGS, .unit = "test" },
69 { "dc_luma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_LUMA}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
70 { "dc_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_CHROMA}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
71 { "freq_luma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_LUMA}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
72 { "freq_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_CHROMA}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
73 { "amp_luma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_LUMA}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
74 { "amp_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_CHROMA}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
75 { "cbp", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_CBP}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
76 { "mv", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_MV}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
77 { "ring1", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING1}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
78 { "ring2", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING2}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
79 { "all", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_ALL}, INT_MIN, INT_MAX, FLAGS, .unit = "test" },
80 { "max_frames", "Set the maximum number of frames generated for each test", OFFSET(max_frames),
81 AV_OPT_TYPE_INT64, {.i64 = 30}, 1, INT64_MAX, FLAGS },
82 { "m", "Set the maximum number of frames generated for each test", OFFSET(max_frames),
83 AV_OPT_TYPE_INT64, {.i64 = 30}, 1, INT64_MAX, FLAGS },
84 { NULL }
85 };
86
87 AVFILTER_DEFINE_CLASS(mptestsrc);
88
89 static double c[64];
90
91 1 static void init_idct(void)
92 {
93 int i, j;
94
95
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (i = 0; i < 8; i++) {
96
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
8 double s = i == 0 ? sqrt(0.125) : 0.5;
97
98
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 8 times.
72 for (j = 0; j < 8; j++)
99 64 c[i*8+j] = s*cos((M_PI/8.0)*i*(j+0.5));
100 }
101 1 }
102
103 18176 static void idct(uint8_t *dst, int dst_linesize, int src[64])
104 {
105 int i, j, k;
106 double tmp[64];
107
108
2/2
✓ Branch 0 taken 145408 times.
✓ Branch 1 taken 18176 times.
163584 for (i = 0; i < 8; i++) {
109
2/2
✓ Branch 0 taken 1163264 times.
✓ Branch 1 taken 145408 times.
1308672 for (j = 0; j < 8; j++) {
110 1163264 double sum = 0.0;
111
112
2/2
✓ Branch 0 taken 9306112 times.
✓ Branch 1 taken 1163264 times.
10469376 for (k = 0; k < 8; k++)
113 9306112 sum += c[k*8+j] * src[8*i+k];
114
115 1163264 tmp[8*i+j] = sum;
116 }
117 }
118
119
2/2
✓ Branch 0 taken 145408 times.
✓ Branch 1 taken 18176 times.
163584 for (j = 0; j < 8; j++) {
120
2/2
✓ Branch 0 taken 1163264 times.
✓ Branch 1 taken 145408 times.
1308672 for (i = 0; i < 8; i++) {
121 1163264 double sum = 0.0;
122
123
2/2
✓ Branch 0 taken 9306112 times.
✓ Branch 1 taken 1163264 times.
10469376 for (k = 0; k < 8; k++)
124 9306112 sum += c[k*8+i]*tmp[8*k+j];
125
126 1163264 dst[dst_linesize*i + j] = av_clip_uint8(lrint(sum));
127 }
128 }
129 18176 }
130
131 25600 static void draw_dc(uint8_t *dst, int dst_linesize, int color, int w, int h)
132 {
133 int x, y;
134
135
2/2
✓ Branch 0 taken 204800 times.
✓ Branch 1 taken 25600 times.
230400 for (y = 0; y < h; y++)
136
2/2
✓ Branch 0 taken 1638400 times.
✓ Branch 1 taken 204800 times.
1843200 for (x = 0; x < w; x++)
137 1638400 dst[x + y*dst_linesize] = color;
138 25600 }
139
140 18176 static void draw_basis(uint8_t *dst, int dst_linesize, int amp, int freq, int dc)
141 {
142 int src[64];
143
144 18176 memset(src, 0, 64*sizeof(int));
145 18176 src[0] = dc;
146
1/2
✓ Branch 0 taken 18176 times.
✗ Branch 1 not taken.
18176 if (amp)
147 18176 src[freq] = amp;
148 18176 idct(dst, dst_linesize, src);
149 18176 }
150
151 static void draw_cbp(uint8_t *dst[3], int dst_linesize[3], int cbp, int amp, int dc)
152 {
153 if (cbp&1) draw_basis(dst[0] , dst_linesize[0], amp, 1, dc);
154 if (cbp&2) draw_basis(dst[0]+8 , dst_linesize[0], amp, 1, dc);
155 if (cbp&4) draw_basis(dst[0]+ 8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
156 if (cbp&8) draw_basis(dst[0]+8+8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
157 if (cbp&16) draw_basis(dst[1] , dst_linesize[1], amp, 1, dc);
158 if (cbp&32) draw_basis(dst[2] , dst_linesize[2], amp, 1, dc);
159 }
160
161 100 static void dc_test(uint8_t *dst, int dst_linesize, int w, int h, int off)
162 {
163 100 const int step = FFMAX(256/(w*h/256), 1);
164 100 int x, y, color = off;
165
166
2/2
✓ Branch 0 taken 1600 times.
✓ Branch 1 taken 100 times.
1700 for (y = 0; y < h; y += 16) {
167
2/2
✓ Branch 0 taken 25600 times.
✓ Branch 1 taken 1600 times.
27200 for (x = 0; x < w; x += 16) {
168 25600 draw_dc(dst + x + y*dst_linesize, dst_linesize, color, 8, 8);
169 25600 color += step;
170 }
171 }
172 100 }
173
174 100 static void freq_test(uint8_t *dst, int dst_linesize, int off)
175 {
176 100 int x, y, freq = 0;
177
178
2/2
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 100 times.
900 for (y = 0; y < 8*16; y += 16) {
179
2/2
✓ Branch 0 taken 6400 times.
✓ Branch 1 taken 800 times.
7200 for (x = 0; x < 8*16; x += 16) {
180 6400 draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*(96+off), freq, 128*8);
181 6400 freq++;
182 }
183 }
184 100 }
185
186 46 static void amp_test(uint8_t *dst, int dst_linesize, int off)
187 {
188 46 int x, y, amp = off;
189
190
2/2
✓ Branch 0 taken 736 times.
✓ Branch 1 taken 46 times.
782 for (y = 0; y < 16*16; y += 16) {
191
2/2
✓ Branch 0 taken 11776 times.
✓ Branch 1 taken 736 times.
12512 for (x = 0; x < 16*16; x += 16) {
192 11776 draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*amp, 1, 128*8);
193 11776 amp++;
194 }
195 }
196 46 }
197
198 static void cbp_test(uint8_t *dst[3], int dst_linesize[3], int off)
199 {
200 int x, y, cbp = 0;
201
202 for (y = 0; y < 16*8; y += 16) {
203 for (x = 0; x < 16*8; x += 16) {
204 uint8_t *dst1[3];
205 dst1[0] = dst[0] + x*2 + y*2*dst_linesize[0];
206 dst1[1] = dst[1] + x + y* dst_linesize[1];
207 dst1[2] = dst[2] + x + y* dst_linesize[2];
208
209 draw_cbp(dst1, dst_linesize, cbp, (64+off)*4, 128*8);
210 cbp++;
211 }
212 }
213 }
214
215 static void mv_test(uint8_t *dst, int dst_linesize, int off)
216 {
217 int x, y;
218
219 for (y = 0; y < 16*16; y++) {
220 if (y&16)
221 continue;
222 for (x = 0; x < 16*16; x++)
223 dst[x + y*dst_linesize] = x + off*8/(y/32+1);
224 }
225 }
226
227 static void ring1_test(uint8_t *dst, int dst_linesize, int off)
228 {
229 int x, y, color = 0;
230
231 for (y = off; y < 16*16; y += 16) {
232 for (x = off; x < 16*16; x += 16) {
233 draw_dc(dst + x + y*dst_linesize, dst_linesize, ((x+y)&16) ? color : -color, 16, 16);
234 color++;
235 }
236 }
237 }
238
239 static void ring2_test(uint8_t *dst, int dst_linesize, int off)
240 {
241 int x, y;
242
243 for (y = 0; y < 16*16; y++) {
244 for (x = 0; x < 16*16; x++) {
245 double d = hypot(x-8*16, y-8*16);
246 double r = d/20 - (int)(d/20);
247 if (r < off/30.0) {
248 dst[x + y*dst_linesize] = 255;
249 dst[x + y*dst_linesize+256] = 0;
250 } else {
251 dst[x + y*dst_linesize] = x;
252 dst[x + y*dst_linesize+256] = x;
253 }
254 }
255 }
256 }
257
258 1 static av_cold int init(AVFilterContext *ctx)
259 {
260 1 MPTestContext *test = ctx->priv;
261
262 2 test->max_pts = test->duration >= 0 ?
263
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 av_rescale_q(test->duration, AV_TIME_BASE_Q, av_inv_q(test->frame_rate)) : -1;
264 1 test->pts = 0;
265
266 1 av_log(ctx, AV_LOG_VERBOSE, "rate:%d/%d duration:%f\n",
267 test->frame_rate.num, test->frame_rate.den,
268
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 test->duration < 0 ? -1 : test->max_pts * av_q2d(av_inv_q(test->frame_rate)));
269 1 init_idct();
270
271 1 return 0;
272 }
273
274 1 static int config_props(AVFilterLink *outlink)
275 {
276 1 AVFilterContext *ctx = outlink->src;
277 1 MPTestContext *test = ctx->priv;
278 1 const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(outlink->format);
279
280 1 test->hsub = pix_desc->log2_chroma_w;
281 1 test->vsub = pix_desc->log2_chroma_h;
282
283 1 outlink->w = WIDTH;
284 1 outlink->h = HEIGHT;
285 1 outlink->time_base = av_inv_q(test->frame_rate);
286 1 outlink->frame_rate = test->frame_rate;
287
288 1 return 0;
289 }
290
291 252 static int request_frame(AVFilterLink *outlink)
292 {
293 252 MPTestContext *test = outlink->src->priv;
294 AVFrame *picref;
295 252 int w = WIDTH, h = HEIGHT,
296 252 cw = AV_CEIL_RSHIFT(w, test->hsub), ch = AV_CEIL_RSHIFT(h, test->vsub);
297 252 uint64_t frame = outlink->frame_count_in / test->max_frames;
298 252 uint64_t mod = outlink->frame_count_in % test->max_frames;
299 252 enum test_type tt = test->test;
300 int i;
301
302
3/4
✓ Branch 0 taken 252 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 251 times.
252 if (test->max_pts >= 0 && test->pts > test->max_pts)
303 1 return AVERROR_EOF;
304 251 picref = ff_get_video_buffer(outlink, w, h);
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 251 times.
251 if (!picref)
306 return AVERROR(ENOMEM);
307 251 picref->pts = test->pts++;
308 251 picref->duration = 1;
309
310 // clean image
311
2/2
✓ Branch 0 taken 128512 times.
✓ Branch 1 taken 251 times.
128763 for (i = 0; i < h; i++)
312 128512 memset(picref->data[0] + i*picref->linesize[0], 0, w);
313
2/2
✓ Branch 0 taken 64256 times.
✓ Branch 1 taken 251 times.
64507 for (i = 0; i < ch; i++) {
314 64256 memset(picref->data[1] + i*picref->linesize[1], 128, cw);
315 64256 memset(picref->data[2] + i*picref->linesize[2], 128, cw);
316 }
317
318
3/4
✓ Branch 0 taken 251 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 246 times.
✓ Branch 3 taken 5 times.
251 if (tt == TEST_ALL && mod) /* draw a black frame at the beginning of each test */
319 246 tt = frame%(TEST_NB-1);
320
321
6/11
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 50 times.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 50 times.
✓ Branch 4 taken 46 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 5 times.
251 switch (tt) {
322 50 case TEST_DC_LUMA: dc_test(picref->data[0], picref->linesize[0], 256, 256, mod); break;
323 50 case TEST_DC_CHROMA: dc_test(picref->data[1], picref->linesize[1], 256, 256, mod); break;
324 50 case TEST_FREQ_LUMA: freq_test(picref->data[0], picref->linesize[0], mod); break;
325 50 case TEST_FREQ_CHROMA: freq_test(picref->data[1], picref->linesize[1], mod); break;
326 46 case TEST_AMP_LUMA: amp_test(picref->data[0], picref->linesize[0], mod); break;
327 case TEST_AMP_CHROMA: amp_test(picref->data[1], picref->linesize[1], mod); break;
328 case TEST_CBP: cbp_test(picref->data , picref->linesize , mod); break;
329 case TEST_MV: mv_test(picref->data[0], picref->linesize[0], mod); break;
330 case TEST_RING1: ring1_test(picref->data[0], picref->linesize[0], mod); break;
331 case TEST_RING2: ring2_test(picref->data[0], picref->linesize[0], mod); break;
332 }
333
334 251 return ff_filter_frame(outlink, picref);
335 }
336
337 static const AVFilterPad mptestsrc_outputs[] = {
338 {
339 .name = "default",
340 .type = AVMEDIA_TYPE_VIDEO,
341 .request_frame = request_frame,
342 .config_props = config_props,
343 },
344 };
345
346 const AVFilter ff_vsrc_mptestsrc = {
347 .name = "mptestsrc",
348 .description = NULL_IF_CONFIG_SMALL("Generate various test pattern."),
349 .priv_size = sizeof(MPTestContext),
350 .priv_class = &mptestsrc_class,
351 .init = init,
352 .inputs = NULL,
353 FILTER_OUTPUTS(mptestsrc_outputs),
354 FILTER_SINGLE_PIXFMT(AV_PIX_FMT_YUV420P),
355 };
356