Browse code

lavfi: port mp test source

Stefano Sabatini authored on 2011/08/08 21:41:22
Showing 6 changed files
... ...
@@ -1507,6 +1507,7 @@ frei0r_src_filter_deps="frei0r dlopen strtok_r"
1507 1507
 hqdn3d_filter_deps="gpl"
1508 1508
 movie_filter_deps="avcodec avformat"
1509 1509
 mp_filter_deps="gpl avcodec"
1510
+mptestsrc_filter_deps="gpl"
1510 1511
 negate_filter_deps="lut_filter"
1511 1512
 ocv_filter_deps="libopencv"
1512 1513
 scale_filter_deps="swscale"
... ...
@@ -2034,6 +2034,63 @@ movie=/dev/video0:f=video4linux2, scale=180:-1, setpts=PTS-STARTPTS [movie];
2034 2034
 
2035 2035
 @end example
2036 2036
 
2037
+@section mptestsrc
2038
+
2039
+Generate various test patterns, as generated by the MPlayer test filter.
2040
+
2041
+The size of the generated video is fixed, and is 256x256.
2042
+This source is useful in particular for testing encoding features.
2043
+
2044
+This source accepts an optional sequence of @var{key}=@var{value} pairs,
2045
+separated by ":". The description of the accepted options follows.
2046
+
2047
+@table @option
2048
+
2049
+@item rate, r
2050
+Specify the frame rate of the sourced video, as the number of frames
2051
+generated per second. It has to be a string in the format
2052
+@var{frame_rate_num}/@var{frame_rate_den}, an integer number, a float
2053
+number or a valid video frame rate abbreviation. The default value is
2054
+"25".
2055
+
2056
+@item duration, d
2057
+Set the video duration of the sourced video. The accepted syntax is:
2058
+@example
2059
+[-]HH[:MM[:SS[.m...]]]
2060
+[-]S+[.m...]
2061
+@end example
2062
+See also the function @code{av_parse_time()}.
2063
+
2064
+If not specified, or the expressed duration is negative, the video is
2065
+supposed to be generated forever.
2066
+
2067
+@item test, t
2068
+
2069
+Set the number or the name of the test to perform. Supported tests are:
2070
+@table @option
2071
+@item dc_luma
2072
+@item dc_chroma
2073
+@item freq_luma
2074
+@item freq_chroma
2075
+@item amp_luma
2076
+@item amp_chroma
2077
+@item cbp
2078
+@item mv
2079
+@item ring1
2080
+@item ring2
2081
+@item all
2082
+@end table
2083
+
2084
+Default value is "all", which will cycle through the list of all tests.
2085
+@end table
2086
+
2087
+For example the following:
2088
+@example
2089
+testsrc=t=dc_luma
2090
+@end example
2091
+
2092
+will generate a "dc_luma" test pattern.
2093
+
2037 2094
 @section nullsrc
2038 2095
 
2039 2096
 Null video source, never return images. It is mainly useful as a
... ...
@@ -69,6 +69,7 @@ OBJS-$(CONFIG_BUFFER_FILTER)                 += vsrc_buffer.o
69 69
 OBJS-$(CONFIG_COLOR_FILTER)                  += vsrc_color.o
70 70
 OBJS-$(CONFIG_FREI0R_SRC_FILTER)             += vf_frei0r.o
71 71
 OBJS-$(CONFIG_MOVIE_FILTER)                  += vsrc_movie.o
72
+OBJS-$(CONFIG_MPTESTSRC_FILTER)              += vsrc_mptestsrc.o
72 73
 OBJS-$(CONFIG_NULLSRC_FILTER)                += vsrc_nullsrc.o
73 74
 OBJS-$(CONFIG_RGBTESTSRC_FILTER)             += vsrc_testsrc.o
74 75
 OBJS-$(CONFIG_TESTSRC_FILTER)                += vsrc_testsrc.o
... ...
@@ -85,6 +85,7 @@ void avfilter_register_all(void)
85 85
     REGISTER_FILTER (COLOR,       color,       vsrc);
86 86
     REGISTER_FILTER (FREI0R,      frei0r_src,  vsrc);
87 87
     REGISTER_FILTER (MOVIE,       movie,       vsrc);
88
+    REGISTER_FILTER (MPTESTSRC,   mptestsrc,   vsrc);
88 89
     REGISTER_FILTER (NULLSRC,     nullsrc,     vsrc);
89 90
     REGISTER_FILTER (RGBTESTSRC,  rgbtestsrc,  vsrc);
90 91
     REGISTER_FILTER (TESTSRC,     testsrc,     vsrc);
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/rational.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  2
32
-#define LIBAVFILTER_VERSION_MINOR 28
33
-#define LIBAVFILTER_VERSION_MICRO  1
32
+#define LIBAVFILTER_VERSION_MINOR 29
33
+#define LIBAVFILTER_VERSION_MICRO  0
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,392 @@
0
+/*
1
+ * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation; either version 2 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License along
16
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
17
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * MP test source, ported from MPlayer libmpcodecs/vf_test.c
23
+ */
24
+
25
+#include "libavutil/avstring.h"
26
+#include "libavutil/opt.h"
27
+#include "libavutil/parseutils.h"
28
+#include "libavutil/pixdesc.h"
29
+#include "avfilter.h"
30
+
31
+#define WIDTH 512
32
+#define HEIGHT 512
33
+
34
+enum test_type {
35
+    TEST_DC_LUMA,
36
+    TEST_DC_CHROMA,
37
+    TEST_FREQ_LUMA,
38
+    TEST_FREQ_CHROMA,
39
+    TEST_AMP_LUMA,
40
+    TEST_AMP_CHROMA,
41
+    TEST_CBP,
42
+    TEST_MV,
43
+    TEST_RING1,
44
+    TEST_RING2,
45
+    TEST_ALL,
46
+    TEST_NB
47
+};
48
+
49
+typedef struct MPTestContext {
50
+    const AVClass *class;
51
+    unsigned int frame_nb;
52
+    AVRational time_base;
53
+    int64_t pts, max_pts;
54
+    int hsub, vsub;
55
+    char *size, *rate, *duration;
56
+    enum test_type test;
57
+} MPTestContext;
58
+
59
+#define OFFSET(x) offsetof(MPTestContext, x)
60
+
61
+static const AVOption mptestsrc_options[]= {
62
+    { "rate",     "set video rate",     OFFSET(rate),     FF_OPT_TYPE_STRING, {.str = "25"},      0, 0 },
63
+    { "r",        "set video rate",     OFFSET(rate),     FF_OPT_TYPE_STRING, {.str = "25"},      0, 0 },
64
+    { "duration", "set video duration", OFFSET(duration), FF_OPT_TYPE_STRING, {.str = NULL},      0, 0 },
65
+    { "d",        "set video duration", OFFSET(duration), FF_OPT_TYPE_STRING, {.str = NULL},      0, 0 },
66
+
67
+    { "test", "set test to perform", OFFSET(test),  FF_OPT_TYPE_INT,   {.dbl=TEST_ALL}, 0, INT_MAX, 0, "test" },
68
+    { "t",    "set test to perform", OFFSET(test),  FF_OPT_TYPE_INT,   {.dbl=TEST_ALL}, 0, INT_MAX, 0, "test" },
69
+    { "dc_luma",     "", 0, FF_OPT_TYPE_CONST, {.dbl=TEST_DC_LUMA},     INT_MIN, INT_MAX, 0, "test" },
70
+    { "dc_chroma",   "", 0, FF_OPT_TYPE_CONST, {.dbl=TEST_DC_CHROMA},   INT_MIN, INT_MAX, 0, "test" },
71
+    { "freq_luma",   "", 0, FF_OPT_TYPE_CONST, {.dbl=TEST_FREQ_LUMA},   INT_MIN, INT_MAX, 0, "test" },
72
+    { "freq_chroma", "", 0, FF_OPT_TYPE_CONST, {.dbl=TEST_FREQ_CHROMA}, INT_MIN, INT_MAX, 0, "test" },
73
+    { "amp_luma",    "", 0, FF_OPT_TYPE_CONST, {.dbl=TEST_AMP_LUMA},    INT_MIN, INT_MAX, 0, "test" },
74
+    { "amp_chroma",  "", 0, FF_OPT_TYPE_CONST, {.dbl=TEST_AMP_CHROMA},  INT_MIN, INT_MAX, 0, "test" },
75
+    { "cbp",         "", 0, FF_OPT_TYPE_CONST, {.dbl=TEST_CBP},         INT_MIN, INT_MAX, 0, "test" },
76
+    { "mv",          "", 0, FF_OPT_TYPE_CONST, {.dbl=TEST_MV},          INT_MIN, INT_MAX, 0, "test" },
77
+    { "ring1",       "", 0, FF_OPT_TYPE_CONST, {.dbl=TEST_RING1},       INT_MIN, INT_MAX, 0, "test" },
78
+    { "ring2",       "", 0, FF_OPT_TYPE_CONST, {.dbl=TEST_RING2},       INT_MIN, INT_MAX, 0, "test" },
79
+    { "all",         "", 0, FF_OPT_TYPE_CONST, {.dbl=TEST_ALL},         INT_MIN, INT_MAX, 0, "test" },
80
+
81
+    { NULL },
82
+};
83
+
84
+static const char *mptestsrc_get_name(void *ctx)
85
+{
86
+    return "mptestsrc";
87
+}
88
+
89
+static const AVClass mptestsrc_class = {
90
+    "MPTestContext",
91
+    mptestsrc_get_name,
92
+    mptestsrc_options
93
+};
94
+
95
+static double c[64];
96
+
97
+static void init_idct(void)
98
+{
99
+    int i, j;
100
+
101
+    for (i = 0; i < 8; i++) {
102
+        double s = i == 0 ? sqrt(0.125) : 0.5;
103
+
104
+        for (j = 0; j < 8; j++)
105
+            c[i*8+j] = s*cos((M_PI/8.0)*i*(j+0.5));
106
+    }
107
+}
108
+
109
+static void idct(uint8_t *dst, int dst_linesize, int src[64])
110
+{
111
+    int i, j, k;
112
+    double tmp[64];
113
+
114
+    for (i = 0; i < 8; i++) {
115
+        for (j = 0; j < 8; j++) {
116
+            double sum = 0.0;
117
+
118
+            for (k = 0; k < 8; k++)
119
+                sum += c[k*8+j] * src[8*i+k];
120
+
121
+            tmp[8*i+j] = sum;
122
+        }
123
+    }
124
+
125
+    for (j = 0; j < 8; j++) {
126
+        for (i = 0; i < 8; i++) {
127
+            double sum = 0.0;
128
+
129
+            for (k = 0; k < 8; k++)
130
+                sum += c[k*8+i]*tmp[8*k+j];
131
+
132
+            dst[dst_linesize*i + j] = av_clip((int)floor(sum+0.5), 0, 255);
133
+        }
134
+    }
135
+}
136
+
137
+static void draw_dc(uint8_t *dst, int dst_linesize, int color, int w, int h)
138
+{
139
+    int x, y;
140
+
141
+    for (y = 0; y < h; y++)
142
+        for (x = 0; x < w; x++)
143
+            dst[x + y*dst_linesize] = color;
144
+}
145
+
146
+static void draw_basis(uint8_t *dst, int dst_linesize, int amp, int freq, int dc)
147
+{
148
+    int src[64];
149
+
150
+    memset(src, 0, 64*sizeof(int));
151
+    src[0] = dc;
152
+    if (amp)
153
+        src[freq] = amp;
154
+    idct(dst, dst_linesize, src);
155
+}
156
+
157
+static void draw_cbp(uint8_t *dst[3], int dst_linesize[3], int cbp, int amp, int dc)
158
+{
159
+    if (cbp&1)  draw_basis(dst[0]                    , dst_linesize[0], amp, 1, dc);
160
+    if (cbp&2)  draw_basis(dst[0]+8                  , dst_linesize[0], amp, 1, dc);
161
+    if (cbp&4)  draw_basis(dst[0]+  8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
162
+    if (cbp&8)  draw_basis(dst[0]+8+8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
163
+    if (cbp&16) draw_basis(dst[1]                    , dst_linesize[1], amp, 1, dc);
164
+    if (cbp&32) draw_basis(dst[2]                    , dst_linesize[2], amp, 1, dc);
165
+}
166
+
167
+static void dc_test(uint8_t *dst, int dst_linesize, int w, int h, int off)
168
+{
169
+    const int step = FFMAX(256/(w*h/256), 1);
170
+    int x, y, color = off;
171
+
172
+    for (y = 0; y < h; y += 16) {
173
+        for (x = 0; x < w; x += 16) {
174
+            draw_dc(dst + x + y*dst_linesize, dst_linesize, color, 8, 8);
175
+            color += step;
176
+        }
177
+    }
178
+}
179
+
180
+static void freq_test(uint8_t *dst, int dst_linesize, int off)
181
+{
182
+    int x, y, freq = 0;
183
+
184
+    for (y = 0; y < 8*16; y += 16) {
185
+        for (x = 0; x < 8*16; x += 16) {
186
+            draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*(96+off), freq, 128*8);
187
+            freq++;
188
+        }
189
+    }
190
+}
191
+
192
+static void amp_test(uint8_t *dst, int dst_linesize, int off)
193
+{
194
+    int x, y, amp = off;
195
+
196
+    for (y = 0; y < 16*16; y += 16) {
197
+        for (x = 0; x < 16*16; x += 16) {
198
+            draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*amp, 1, 128*8);
199
+            amp++;
200
+        }
201
+    }
202
+}
203
+
204
+static void cbp_test(uint8_t *dst[3], int dst_linesize[3], int off)
205
+{
206
+    int x, y, cbp = 0;
207
+
208
+    for (y = 0; y < 16*8; y += 16) {
209
+        for (x = 0; x < 16*8; x += 16) {
210
+            uint8_t *dst1[3];
211
+            dst1[0] = dst[0] + x*2 + y*2*dst_linesize[0];
212
+            dst1[1] = dst[1] + x   + y*  dst_linesize[1];
213
+            dst1[2] = dst[2] + x   + y*  dst_linesize[2];
214
+
215
+            draw_cbp(dst1, dst_linesize, cbp, (64+off)*4, 128*8);
216
+            cbp++;
217
+        }
218
+    }
219
+}
220
+
221
+static void mv_test(uint8_t *dst, int dst_linesize, int off)
222
+{
223
+    int x, y;
224
+
225
+    for (y = 0; y < 16*16; y++) {
226
+        if (y&16)
227
+            continue;
228
+        for (x = 0; x < 16*16; x++)
229
+            dst[x + y*dst_linesize] = x + off*8/(y/32+1);
230
+    }
231
+}
232
+
233
+static void ring1_test(uint8_t *dst, int dst_linesize, int off)
234
+{
235
+    int x, y, color = 0;
236
+
237
+    for (y = off; y < 16*16; y += 16) {
238
+        for (x = off; x < 16*16; x += 16) {
239
+            draw_dc(dst + x + y*dst_linesize, dst_linesize, ((x+y)&16) ? color : -color, 16, 16);
240
+            color++;
241
+        }
242
+    }
243
+}
244
+
245
+static void ring2_test(uint8_t *dst, int dst_linesize, int off)
246
+{
247
+    int x, y;
248
+
249
+    for (y = 0; y < 16*16; y++) {
250
+        for (x = 0; x < 16*16; x++) {
251
+            double d = sqrt((x-8*16)*(x-8*16) + (y-8*16)*(y-8*16));
252
+            double r = d/20 - (int)(d/20);
253
+            if (r < off/30.0) {
254
+                dst[x + y*dst_linesize]     = 255;
255
+                dst[x + y*dst_linesize+256] = 0;
256
+            } else {
257
+                dst[x + y*dst_linesize]     = x;
258
+                dst[x + y*dst_linesize+256] = x;
259
+            }
260
+        }
261
+    }
262
+}
263
+
264
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
265
+{
266
+    MPTestContext *test = ctx->priv;
267
+    AVRational frame_rate_q;
268
+    int64_t duration = -1;
269
+    int ret;
270
+
271
+    test->class = &mptestsrc_class;
272
+    av_opt_set_defaults2(test, 0, 0);
273
+
274
+    if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
275
+        av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
276
+        return ret;
277
+    }
278
+
279
+    if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0 ||
280
+        frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
281
+        av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
282
+        return ret;
283
+    }
284
+
285
+    if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
286
+        av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
287
+        return ret;
288
+    }
289
+
290
+    test->time_base.num = frame_rate_q.den;
291
+    test->time_base.den = frame_rate_q.num;
292
+    test->max_pts = duration >= 0 ?
293
+        av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
294
+    test->frame_nb = 0;
295
+    test->pts = 0;
296
+
297
+    av_log(ctx, AV_LOG_INFO, "rate:%d/%d duration:%f\n",
298
+           frame_rate_q.num, frame_rate_q.den,
299
+           duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base));
300
+    init_idct();
301
+
302
+    return 0;
303
+}
304
+
305
+static int config_props(AVFilterLink *outlink)
306
+{
307
+    AVFilterContext *ctx = outlink->src;
308
+    MPTestContext *test = ctx->priv;
309
+    const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[outlink->format];
310
+
311
+    test->hsub = pix_desc->log2_chroma_w;
312
+    test->vsub = pix_desc->log2_chroma_h;
313
+
314
+    outlink->w = WIDTH;
315
+    outlink->h = HEIGHT;
316
+    outlink->time_base = test->time_base;
317
+
318
+    return 0;
319
+}
320
+
321
+static int query_formats(AVFilterContext *ctx)
322
+{
323
+    static const enum PixelFormat pix_fmts[] = {
324
+        PIX_FMT_YUV420P, PIX_FMT_NONE
325
+    };
326
+
327
+    avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
328
+    return 0;
329
+}
330
+
331
+static int request_frame(AVFilterLink *outlink)
332
+{
333
+    MPTestContext *test = outlink->src->priv;
334
+    AVFilterBufferRef *picref;
335
+    int w = WIDTH, h = HEIGHT, ch = h>>test->vsub;
336
+    unsigned int frame = test->frame_nb;
337
+    enum test_type tt = test->test;
338
+
339
+    if (test->max_pts >= 0 && test->pts > test->max_pts)
340
+        return AVERROR_EOF;
341
+    picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, w, h);
342
+    picref->pts = test->pts++;
343
+
344
+    // clean image
345
+    memset(picref->data[0], 0,   picref->linesize[0] * h);
346
+    memset(picref->data[1], 128, picref->linesize[1] * ch);
347
+    memset(picref->data[2], 128, picref->linesize[2] * ch);
348
+
349
+    if (tt == TEST_ALL && frame%30) /* draw a black frame at the beginning of each test */
350
+        tt = (frame/30)%(TEST_NB-1);
351
+
352
+    switch (tt) {
353
+    case TEST_DC_LUMA:       dc_test(picref->data[0], picref->linesize[0], 256, 256, frame%30); break;
354
+    case TEST_DC_CHROMA:     dc_test(picref->data[1], picref->linesize[1], 256, 256, frame%30); break;
355
+    case TEST_FREQ_LUMA:   freq_test(picref->data[0], picref->linesize[0], frame%30); break;
356
+    case TEST_FREQ_CHROMA: freq_test(picref->data[1], picref->linesize[1], frame%30); break;
357
+    case TEST_AMP_LUMA:     amp_test(picref->data[0], picref->linesize[0], frame%30); break;
358
+    case TEST_AMP_CHROMA:   amp_test(picref->data[1], picref->linesize[1], frame%30); break;
359
+    case TEST_CBP:          cbp_test(picref->data   , picref->linesize   , frame%30); break;
360
+    case TEST_MV:            mv_test(picref->data[0], picref->linesize[0], frame%30); break;
361
+    case TEST_RING1:      ring1_test(picref->data[0], picref->linesize[0], frame%30); break;
362
+    case TEST_RING2:      ring2_test(picref->data[0], picref->linesize[0], frame%30); break;
363
+    }
364
+
365
+    test->frame_nb++;
366
+
367
+    avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
368
+    avfilter_draw_slice(outlink, 0, picref->video->h, 1);
369
+    avfilter_end_frame(outlink);
370
+    avfilter_unref_buffer(picref);
371
+
372
+    return 0;
373
+}
374
+
375
+AVFilter avfilter_vsrc_mptestsrc = {
376
+    .name      = "mptestsrc",
377
+    .description = NULL_IF_CONFIG_SMALL("Generate various test pattern."),
378
+    .priv_size = sizeof(MPTestContext),
379
+    .init      = init,
380
+
381
+    .query_formats   = query_formats,
382
+
383
+    .inputs    = (AVFilterPad[]) {{ .name = NULL}},
384
+
385
+    .outputs   = (AVFilterPad[]) {{ .name = "default",
386
+                                    .type = AVMEDIA_TYPE_VIDEO,
387
+                                    .config_props = config_props,
388
+                                    .request_frame = request_frame,
389
+                                    .config_props  = config_props, },
390
+                                  { .name = NULL }},
391
+};