]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_mptestsrc.c
lavfi/blackdetect: switch to new ff_filter_frame() API
[ffmpeg] / libavfilter / vsrc_mptestsrc.c
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/avstring.h"
27 #include "libavutil/opt.h"
28 #include "libavutil/parseutils.h"
29 #include "libavutil/pixdesc.h"
30 #include "avfilter.h"
31 #include "internal.h"
32 #include "formats.h"
33 #include "video.h"
34
35 #define WIDTH 512
36 #define HEIGHT 512
37
38 enum test_type {
39     TEST_DC_LUMA,
40     TEST_DC_CHROMA,
41     TEST_FREQ_LUMA,
42     TEST_FREQ_CHROMA,
43     TEST_AMP_LUMA,
44     TEST_AMP_CHROMA,
45     TEST_CBP,
46     TEST_MV,
47     TEST_RING1,
48     TEST_RING2,
49     TEST_ALL,
50     TEST_NB
51 };
52
53 typedef struct MPTestContext {
54     const AVClass *class;
55     unsigned int frame_nb;
56     AVRational time_base;
57     int64_t pts, max_pts;
58     int hsub, vsub;
59     char *size, *rate, *duration;
60     enum test_type test;
61 } MPTestContext;
62
63 #define OFFSET(x) offsetof(MPTestContext, x)
64 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
65 static const AVOption mptestsrc_options[]= {
66     { "rate",     "set video rate",     OFFSET(rate),     AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
67     { "r",        "set video rate",     OFFSET(rate),     AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
68     { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
69     { "d",        "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
70
71     { "test", "set test to perform", OFFSET(test),  AV_OPT_TYPE_INT,   {.i64=TEST_ALL}, 0, INT_MAX, FLAGS, "test" },
72     { "t",    "set test to perform", OFFSET(test),  AV_OPT_TYPE_INT,   {.i64=TEST_ALL}, 0, INT_MAX, FLAGS, "test" },
73     { "dc_luma",     "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_LUMA},     INT_MIN, INT_MAX, FLAGS, "test" },
74     { "dc_chroma",   "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_CHROMA},   INT_MIN, INT_MAX, FLAGS, "test" },
75     { "freq_luma",   "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_LUMA},   INT_MIN, INT_MAX, FLAGS, "test" },
76     { "freq_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_CHROMA}, INT_MIN, INT_MAX, FLAGS, "test" },
77     { "amp_luma",    "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_LUMA},    INT_MIN, INT_MAX, FLAGS, "test" },
78     { "amp_chroma",  "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_CHROMA},  INT_MIN, INT_MAX, FLAGS, "test" },
79     { "cbp",         "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_CBP},         INT_MIN, INT_MAX, FLAGS, "test" },
80     { "mv",          "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_MV},          INT_MIN, INT_MAX, FLAGS, "test" },
81     { "ring1",       "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING1},       INT_MIN, INT_MAX, FLAGS, "test" },
82     { "ring2",       "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING2},       INT_MIN, INT_MAX, FLAGS, "test" },
83     { "all",         "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_ALL},         INT_MIN, INT_MAX, FLAGS, "test" },
84
85     { NULL },
86 };
87
88 AVFILTER_DEFINE_CLASS(mptestsrc);
89
90 static double c[64];
91
92 static void init_idct(void)
93 {
94     int i, j;
95
96     for (i = 0; i < 8; i++) {
97         double s = i == 0 ? sqrt(0.125) : 0.5;
98
99         for (j = 0; j < 8; j++)
100             c[i*8+j] = s*cos((M_PI/8.0)*i*(j+0.5));
101     }
102 }
103
104 static void idct(uint8_t *dst, int dst_linesize, int src[64])
105 {
106     int i, j, k;
107     double tmp[64];
108
109     for (i = 0; i < 8; i++) {
110         for (j = 0; j < 8; j++) {
111             double sum = 0.0;
112
113             for (k = 0; k < 8; k++)
114                 sum += c[k*8+j] * src[8*i+k];
115
116             tmp[8*i+j] = sum;
117         }
118     }
119
120     for (j = 0; j < 8; j++) {
121         for (i = 0; i < 8; i++) {
122             double sum = 0.0;
123
124             for (k = 0; k < 8; k++)
125                 sum += c[k*8+i]*tmp[8*k+j];
126
127             dst[dst_linesize*i + j] = av_clip((int)floor(sum+0.5), 0, 255);
128         }
129     }
130 }
131
132 static void draw_dc(uint8_t *dst, int dst_linesize, int color, int w, int h)
133 {
134     int x, y;
135
136     for (y = 0; y < h; y++)
137         for (x = 0; x < w; x++)
138             dst[x + y*dst_linesize] = color;
139 }
140
141 static void draw_basis(uint8_t *dst, int dst_linesize, int amp, int freq, int dc)
142 {
143     int src[64];
144
145     memset(src, 0, 64*sizeof(int));
146     src[0] = dc;
147     if (amp)
148         src[freq] = amp;
149     idct(dst, dst_linesize, src);
150 }
151
152 static void draw_cbp(uint8_t *dst[3], int dst_linesize[3], int cbp, int amp, int dc)
153 {
154     if (cbp&1)  draw_basis(dst[0]                    , dst_linesize[0], amp, 1, dc);
155     if (cbp&2)  draw_basis(dst[0]+8                  , dst_linesize[0], amp, 1, dc);
156     if (cbp&4)  draw_basis(dst[0]+  8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
157     if (cbp&8)  draw_basis(dst[0]+8+8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
158     if (cbp&16) draw_basis(dst[1]                    , dst_linesize[1], amp, 1, dc);
159     if (cbp&32) draw_basis(dst[2]                    , dst_linesize[2], amp, 1, dc);
160 }
161
162 static void dc_test(uint8_t *dst, int dst_linesize, int w, int h, int off)
163 {
164     const int step = FFMAX(256/(w*h/256), 1);
165     int x, y, color = off;
166
167     for (y = 0; y < h; y += 16) {
168         for (x = 0; x < w; x += 16) {
169             draw_dc(dst + x + y*dst_linesize, dst_linesize, color, 8, 8);
170             color += step;
171         }
172     }
173 }
174
175 static void freq_test(uint8_t *dst, int dst_linesize, int off)
176 {
177     int x, y, freq = 0;
178
179     for (y = 0; y < 8*16; y += 16) {
180         for (x = 0; x < 8*16; x += 16) {
181             draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*(96+off), freq, 128*8);
182             freq++;
183         }
184     }
185 }
186
187 static void amp_test(uint8_t *dst, int dst_linesize, int off)
188 {
189     int x, y, amp = off;
190
191     for (y = 0; y < 16*16; y += 16) {
192         for (x = 0; x < 16*16; x += 16) {
193             draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*amp, 1, 128*8);
194             amp++;
195         }
196     }
197 }
198
199 static void cbp_test(uint8_t *dst[3], int dst_linesize[3], int off)
200 {
201     int x, y, cbp = 0;
202
203     for (y = 0; y < 16*8; y += 16) {
204         for (x = 0; x < 16*8; x += 16) {
205             uint8_t *dst1[3];
206             dst1[0] = dst[0] + x*2 + y*2*dst_linesize[0];
207             dst1[1] = dst[1] + x   + y*  dst_linesize[1];
208             dst1[2] = dst[2] + x   + y*  dst_linesize[2];
209
210             draw_cbp(dst1, dst_linesize, cbp, (64+off)*4, 128*8);
211             cbp++;
212         }
213     }
214 }
215
216 static void mv_test(uint8_t *dst, int dst_linesize, int off)
217 {
218     int x, y;
219
220     for (y = 0; y < 16*16; y++) {
221         if (y&16)
222             continue;
223         for (x = 0; x < 16*16; x++)
224             dst[x + y*dst_linesize] = x + off*8/(y/32+1);
225     }
226 }
227
228 static void ring1_test(uint8_t *dst, int dst_linesize, int off)
229 {
230     int x, y, color = 0;
231
232     for (y = off; y < 16*16; y += 16) {
233         for (x = off; x < 16*16; x += 16) {
234             draw_dc(dst + x + y*dst_linesize, dst_linesize, ((x+y)&16) ? color : -color, 16, 16);
235             color++;
236         }
237     }
238 }
239
240 static void ring2_test(uint8_t *dst, int dst_linesize, int off)
241 {
242     int x, y;
243
244     for (y = 0; y < 16*16; y++) {
245         for (x = 0; x < 16*16; x++) {
246             double d = sqrt((x-8*16)*(x-8*16) + (y-8*16)*(y-8*16));
247             double r = d/20 - (int)(d/20);
248             if (r < off/30.0) {
249                 dst[x + y*dst_linesize]     = 255;
250                 dst[x + y*dst_linesize+256] = 0;
251             } else {
252                 dst[x + y*dst_linesize]     = x;
253                 dst[x + y*dst_linesize+256] = x;
254             }
255         }
256     }
257 }
258
259 static av_cold int init(AVFilterContext *ctx, const char *args)
260 {
261     MPTestContext *test = ctx->priv;
262     AVRational frame_rate_q;
263     int64_t duration = -1;
264     int ret;
265
266     test->class = &mptestsrc_class;
267     av_opt_set_defaults(test);
268
269     if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0)
270         return ret;
271
272     if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0) {
273         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
274         return ret;
275     }
276
277     if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
278         av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
279         return ret;
280     }
281
282     test->time_base.num = frame_rate_q.den;
283     test->time_base.den = frame_rate_q.num;
284     test->max_pts = duration >= 0 ?
285         av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
286     test->frame_nb = 0;
287     test->pts = 0;
288
289     av_log(ctx, AV_LOG_VERBOSE, "rate:%d/%d duration:%f\n",
290            frame_rate_q.num, frame_rate_q.den,
291            duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base));
292     init_idct();
293
294     return 0;
295 }
296
297 static int config_props(AVFilterLink *outlink)
298 {
299     AVFilterContext *ctx = outlink->src;
300     MPTestContext *test = ctx->priv;
301     const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(outlink->format);
302
303     test->hsub = pix_desc->log2_chroma_w;
304     test->vsub = pix_desc->log2_chroma_h;
305
306     outlink->w = WIDTH;
307     outlink->h = HEIGHT;
308     outlink->time_base = test->time_base;
309
310     return 0;
311 }
312
313 static int query_formats(AVFilterContext *ctx)
314 {
315     static const enum AVPixelFormat pix_fmts[] = {
316         AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
317     };
318
319     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
320     return 0;
321 }
322
323 static int request_frame(AVFilterLink *outlink)
324 {
325     MPTestContext *test = outlink->src->priv;
326     AVFilterBufferRef *picref;
327     int w = WIDTH, h = HEIGHT, ch = h>>test->vsub;
328     unsigned int frame = test->frame_nb;
329     enum test_type tt = test->test;
330
331     if (test->max_pts >= 0 && test->pts > test->max_pts)
332         return AVERROR_EOF;
333     picref = ff_get_video_buffer(outlink, AV_PERM_WRITE, w, h);
334     picref->pts = test->pts++;
335
336     // clean image
337     memset(picref->data[0], 0,   picref->linesize[0] * h);
338     memset(picref->data[1], 128, picref->linesize[1] * ch);
339     memset(picref->data[2], 128, picref->linesize[2] * ch);
340
341     if (tt == TEST_ALL && frame%30) /* draw a black frame at the beginning of each test */
342         tt = (frame/30)%(TEST_NB-1);
343
344     switch (tt) {
345     case TEST_DC_LUMA:       dc_test(picref->data[0], picref->linesize[0], 256, 256, frame%30); break;
346     case TEST_DC_CHROMA:     dc_test(picref->data[1], picref->linesize[1], 256, 256, frame%30); break;
347     case TEST_FREQ_LUMA:   freq_test(picref->data[0], picref->linesize[0], frame%30); break;
348     case TEST_FREQ_CHROMA: freq_test(picref->data[1], picref->linesize[1], frame%30); break;
349     case TEST_AMP_LUMA:     amp_test(picref->data[0], picref->linesize[0], frame%30); break;
350     case TEST_AMP_CHROMA:   amp_test(picref->data[1], picref->linesize[1], frame%30); break;
351     case TEST_CBP:          cbp_test(picref->data   , picref->linesize   , frame%30); break;
352     case TEST_MV:            mv_test(picref->data[0], picref->linesize[0], frame%30); break;
353     case TEST_RING1:      ring1_test(picref->data[0], picref->linesize[0], frame%30); break;
354     case TEST_RING2:      ring2_test(picref->data[0], picref->linesize[0], frame%30); break;
355     }
356
357     test->frame_nb++;
358     ff_filter_frame(outlink, picref);
359
360     return 0;
361 }
362
363 static const AVFilterPad mptestsrc_outputs[] = {
364     {
365         .name          = "default",
366         .type          = AVMEDIA_TYPE_VIDEO,
367         .request_frame = request_frame,
368         .config_props  = config_props,
369     },
370     { NULL }
371 };
372
373 AVFilter avfilter_vsrc_mptestsrc = {
374     .name      = "mptestsrc",
375     .description = NULL_IF_CONFIG_SMALL("Generate various test pattern."),
376     .priv_size = sizeof(MPTestContext),
377     .init      = init,
378
379     .query_formats   = query_formats,
380
381     .inputs         = NULL,
382     .outputs        = mptestsrc_outputs,
383     .priv_class     = &mptestsrc_class,
384 };