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