]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_testsrc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vsrc_testsrc.c
1 /*
2  * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
3  * Copyright (c) 2011 Stefano Sabatini
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * Misc test sources.
25  *
26  * testsrc is based on the test pattern generator demuxer by Nicolas George:
27  * http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2007-October/037845.html
28  *
29  * rgbtestsrc is ported from MPlayer libmpcodecs/vf_rgbtest.c by
30  * Michael Niedermayer.
31  */
32
33 #include <float.h>
34
35 #include "libavutil/opt.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/parseutils.h"
38 #include "avfilter.h"
39
40 typedef struct {
41     const AVClass *class;
42     int w, h;
43     unsigned int nb_frame;
44     AVRational time_base;
45     int64_t pts, max_pts;
46     char *rate;                 ///< video frame rate
47     char *duration;             ///< total duration of the generated video
48     AVRational sar;             ///< sample aspect ratio
49     int nb_decimals;
50
51     void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
52
53     /* only used by rgbtest */
54     int rgba_map[4];
55 } TestSourceContext;
56
57 #define OFFSET(x) offsetof(TestSourceContext, x)
58
59 static const AVOption testsrc_options[]= {
60     { "size",     "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0 },
61     { "s",        "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0 },
62     { "rate",     "set video rate",     OFFSET(rate),     AV_OPT_TYPE_STRING, {.str = "25"},      0, 0 },
63     { "r",        "set video rate",     OFFSET(rate),     AV_OPT_TYPE_STRING, {.str = "25"},      0, 0 },
64     { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL},      0, 0 },
65     { "d",        "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL},      0, 0 },
66     { "sar",      "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1},  0, INT_MAX },
67     { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0},  INT_MIN, INT_MAX },
68     { "n",        "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0},  INT_MIN, INT_MAX },
69     { NULL },
70 };
71
72 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
73 {
74     TestSourceContext *test = ctx->priv;
75     AVRational frame_rate_q;
76     int64_t duration = -1;
77     int ret = 0;
78
79     av_opt_set_defaults(test);
80
81     if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
82         av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
83         return ret;
84     }
85
86     if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0 ||
87         frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
88         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
89         return ret;
90     }
91     av_freep(&test->rate);
92
93     if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
94         av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
95         return ret;
96     }
97     av_freep(&test->duration);
98
99     if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
100         av_log(ctx, AV_LOG_WARNING,
101                "Option 'decimals' is ignored with source '%s'\n",
102                ctx->filter->name);
103     }
104
105     test->time_base.num = frame_rate_q.den;
106     test->time_base.den = frame_rate_q.num;
107     test->max_pts = duration >= 0 ?
108         av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
109     test->nb_frame = 0;
110     test->pts = 0;
111
112     av_log(ctx, AV_LOG_INFO, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
113            test->w, test->h, frame_rate_q.num, frame_rate_q.den,
114            duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
115            test->sar.num, test->sar.den);
116     return 0;
117 }
118
119 static int config_props(AVFilterLink *outlink)
120 {
121     TestSourceContext *test = outlink->src->priv;
122
123     outlink->w = test->w;
124     outlink->h = test->h;
125     outlink->sample_aspect_ratio = test->sar;
126     outlink->time_base = test->time_base;
127
128     return 0;
129 }
130
131 static int request_frame(AVFilterLink *outlink)
132 {
133     TestSourceContext *test = outlink->src->priv;
134     AVFilterBufferRef *picref;
135
136     if (test->max_pts >= 0 && test->pts >= test->max_pts)
137         return AVERROR_EOF;
138     picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
139                                        test->w, test->h);
140     picref->pts = test->pts++;
141     picref->pos = -1;
142     picref->video->key_frame = 1;
143     picref->video->interlaced = 0;
144     picref->video->pict_type = AV_PICTURE_TYPE_I;
145     picref->video->sample_aspect_ratio = test->sar;
146     test->fill_picture_fn(outlink->src, picref);
147     test->nb_frame++;
148
149     avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
150     avfilter_draw_slice(outlink, 0, picref->video->h, 1);
151     avfilter_end_frame(outlink);
152     avfilter_unref_buffer(picref);
153
154     return 0;
155 }
156
157 #if CONFIG_NULLSRC_FILTER
158
159 static const char *nullsrc_get_name(void *ctx)
160 {
161     return "nullsrc";
162 }
163
164 static const AVClass nullsrc_class = {
165     .class_name = "NullSourceContext",
166     .item_name  = nullsrc_get_name,
167     .option     = testsrc_options,
168 };
169
170 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref) { }
171
172 static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args, void *opaque)
173 {
174     TestSourceContext *test = ctx->priv;
175
176     test->class = &nullsrc_class;
177     test->fill_picture_fn = nullsrc_fill_picture;
178     return init(ctx, args, opaque);
179 }
180
181 AVFilter avfilter_vsrc_nullsrc = {
182     .name        = "nullsrc",
183     .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
184     .init       = nullsrc_init,
185     .priv_size  = sizeof(TestSourceContext),
186
187     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
188     .outputs   = (const AVFilterPad[]) {{ .name = "default",
189                                     .type = AVMEDIA_TYPE_VIDEO,
190                                     .request_frame = request_frame,
191                                     .config_props  = config_props, },
192                                   { .name = NULL}},
193 };
194
195 #endif /* CONFIG_NULLSRC_FILTER */
196
197 #if CONFIG_TESTSRC_FILTER
198
199 static const char *testsrc_get_name(void *ctx)
200 {
201     return "testsrc";
202 }
203
204 static const AVClass testsrc_class = {
205     .class_name = "TestSourceContext",
206     .item_name  = testsrc_get_name,
207     .option     = testsrc_options,
208 };
209
210 /**
211  * Fill a rectangle with value val.
212  *
213  * @param val the RGB value to set
214  * @param dst pointer to the destination buffer to fill
215  * @param dst_linesize linesize of destination
216  * @param segment_width width of the segment
217  * @param x horizontal coordinate where to draw the rectangle in the destination buffer
218  * @param y horizontal coordinate where to draw the rectangle in the destination buffer
219  * @param w width  of the rectangle to draw, expressed as a number of segment_width units
220  * @param h height of the rectangle to draw, expressed as a number of segment_width units
221  */
222 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
223                            unsigned x, unsigned y, unsigned w, unsigned h)
224 {
225     int i;
226     int step = 3;
227
228     dst += segment_width * (step * x + y * dst_linesize);
229     w *= segment_width * step;
230     h *= segment_width;
231     for (i = 0; i < h; i++) {
232         memset(dst, val, w);
233         dst += dst_linesize;
234     }
235 }
236
237 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
238                        unsigned segment_width)
239 {
240 #define TOP_HBAR        1
241 #define MID_HBAR        2
242 #define BOT_HBAR        4
243 #define LEFT_TOP_VBAR   8
244 #define LEFT_BOT_VBAR  16
245 #define RIGHT_TOP_VBAR 32
246 #define RIGHT_BOT_VBAR 64
247     struct {
248         int x, y, w, h;
249     } segments[] = {
250         { 1,  0, 5, 1 }, /* TOP_HBAR */
251         { 1,  6, 5, 1 }, /* MID_HBAR */
252         { 1, 12, 5, 1 }, /* BOT_HBAR */
253         { 0,  1, 1, 5 }, /* LEFT_TOP_VBAR */
254         { 0,  7, 1, 5 }, /* LEFT_BOT_VBAR */
255         { 6,  1, 1, 5 }, /* RIGHT_TOP_VBAR */
256         { 6,  7, 1, 5 }  /* RIGHT_BOT_VBAR */
257     };
258     static const unsigned char masks[10] = {
259         /* 0 */ TOP_HBAR         |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
260         /* 1 */                                                        RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
261         /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR                             |RIGHT_TOP_VBAR,
262         /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR                            |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
263         /* 4 */          MID_HBAR         |LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
264         /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR                             |RIGHT_BOT_VBAR,
265         /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR               |RIGHT_BOT_VBAR,
266         /* 7 */ TOP_HBAR                                              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
267         /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
268         /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
269     };
270     unsigned mask = masks[digit];
271     int i;
272
273     draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
274     for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
275         if (mask & (1<<i))
276             draw_rectangle(255, dst, dst_linesize, segment_width,
277                            segments[i].x, segments[i].y, segments[i].w, segments[i].h);
278 }
279
280 #define GRADIENT_SIZE (6 * 256)
281
282 static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
283 {
284     TestSourceContext *test = ctx->priv;
285     uint8_t *p, *p0;
286     int x, y;
287     int color, color_rest;
288     int icolor;
289     int radius;
290     int quad0, quad;
291     int dquad_x, dquad_y;
292     int grad, dgrad, rgrad, drgrad;
293     int seg_size;
294     int second;
295     int i;
296     uint8_t *data = picref->data[0];
297     int width  = picref->video->w;
298     int height = picref->video->h;
299
300     /* draw colored bars and circle */
301     radius = (width + height) / 4;
302     quad0 = width * width / 4 + height * height / 4 - radius * radius;
303     dquad_y = 1 - height;
304     p0 = data;
305     for (y = 0; y < height; y++) {
306         p = p0;
307         color = 0;
308         color_rest = 0;
309         quad = quad0;
310         dquad_x = 1 - width;
311         for (x = 0; x < width; x++) {
312             icolor = color;
313             if (quad < 0)
314                 icolor ^= 7;
315             quad += dquad_x;
316             dquad_x += 2;
317             *(p++) = icolor & 1 ? 255 : 0;
318             *(p++) = icolor & 2 ? 255 : 0;
319             *(p++) = icolor & 4 ? 255 : 0;
320             color_rest += 8;
321             if (color_rest >= width) {
322                 color_rest -= width;
323                 color++;
324             }
325         }
326         quad0 += dquad_y;
327         dquad_y += 2;
328         p0 += picref->linesize[0];
329     }
330
331     /* draw sliding color line */
332     p0 = p = data + picref->linesize[0] * height * 3/4;
333     grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
334         GRADIENT_SIZE;
335     rgrad = 0;
336     dgrad = GRADIENT_SIZE / width;
337     drgrad = GRADIENT_SIZE % width;
338     for (x = 0; x < width; x++) {
339         *(p++) =
340             grad < 256 || grad >= 5 * 256 ? 255 :
341             grad >= 2 * 256 && grad < 4 * 256 ? 0 :
342             grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
343         *(p++) =
344             grad >= 4 * 256 ? 0 :
345             grad >= 1 * 256 && grad < 3 * 256 ? 255 :
346             grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
347         *(p++) =
348             grad < 2 * 256 ? 0 :
349             grad >= 3 * 256 && grad < 5 * 256 ? 255 :
350             grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
351         grad += dgrad;
352         rgrad += drgrad;
353         if (rgrad >= GRADIENT_SIZE) {
354             grad++;
355             rgrad -= GRADIENT_SIZE;
356         }
357         if (grad >= GRADIENT_SIZE)
358             grad -= GRADIENT_SIZE;
359     }
360     p = p0;
361     for (y = height / 8; y > 0; y--) {
362         memcpy(p+picref->linesize[0], p, 3 * width);
363         p += picref->linesize[0];
364     }
365
366     /* draw digits */
367     seg_size = width / 80;
368     if (seg_size >= 1 && height >= 13 * seg_size) {
369         double time = av_q2d(test->time_base) * test->nb_frame *
370                       pow(10, test->nb_decimals);
371         if (time > INT_MAX)
372             return;
373         second = (int)time;
374         x = width - (width - seg_size * 64) / 2;
375         y = (height - seg_size * 13) / 2;
376         p = data + (x*3 + y * picref->linesize[0]);
377         for (i = 0; i < 8; i++) {
378             p -= 3 * 8 * seg_size;
379             draw_digit(second % 10, p, picref->linesize[0], seg_size);
380             second /= 10;
381             if (second == 0)
382                 break;
383         }
384     }
385 }
386
387 static av_cold int test_init(AVFilterContext *ctx, const char *args, void *opaque)
388 {
389     TestSourceContext *test = ctx->priv;
390
391     test->class = &testsrc_class;
392     test->fill_picture_fn = test_fill_picture;
393     return init(ctx, args, opaque);
394 }
395
396 static int test_query_formats(AVFilterContext *ctx)
397 {
398     static const enum PixelFormat pix_fmts[] = {
399         PIX_FMT_RGB24, PIX_FMT_NONE
400     };
401     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
402     return 0;
403 }
404
405 AVFilter avfilter_vsrc_testsrc = {
406     .name      = "testsrc",
407     .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
408     .priv_size = sizeof(TestSourceContext),
409     .init      = test_init,
410
411     .query_formats   = test_query_formats,
412
413     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
414
415     .outputs   = (const AVFilterPad[]) {{ .name = "default",
416                                     .type = AVMEDIA_TYPE_VIDEO,
417                                     .request_frame = request_frame,
418                                     .config_props  = config_props, },
419                                   { .name = NULL }},
420 };
421
422 #endif /* CONFIG_TESTSRC_FILTER */
423
424 #if CONFIG_RGBTESTSRC_FILTER
425
426 static const char *rgbtestsrc_get_name(void *ctx)
427 {
428     return "rgbtestsrc";
429 }
430
431 static const AVClass rgbtestsrc_class = {
432     .class_name = "RGBTestSourceContext",
433     .item_name  = rgbtestsrc_get_name,
434     .option     = testsrc_options,
435 };
436
437 #define R 0
438 #define G 1
439 #define B 2
440 #define A 3
441
442 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
443                               int x, int y, int r, int g, int b, enum PixelFormat fmt,
444                               int rgba_map[4])
445 {
446     int32_t v;
447     uint8_t *p;
448
449     switch (fmt) {
450     case PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
451     case PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
452     case PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
453     case PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
454     case PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
455     case PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
456     case PIX_FMT_RGB24:
457     case PIX_FMT_BGR24:
458         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
459         p = dst + 3*x + y*dst_linesize;
460         AV_WL24(p, v);
461         break;
462     case PIX_FMT_RGBA:
463     case PIX_FMT_BGRA:
464     case PIX_FMT_ARGB:
465     case PIX_FMT_ABGR:
466         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
467         p = dst + 4*x + y*dst_linesize;
468         AV_WL32(p, v);
469         break;
470     }
471 }
472
473 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
474 {
475     TestSourceContext *test = ctx->priv;
476     int x, y, w = picref->video->w, h = picref->video->h;
477
478     for (y = 0; y < h; y++) {
479          for (x = 0; x < picref->video->w; x++) {
480              int c = 256*x/w;
481              int r = 0, g = 0, b = 0;
482
483              if      (3*y < h  ) r = c;
484              else if (3*y < 2*h) g = c;
485              else                b = c;
486
487              rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
488                                ctx->outputs[0]->format, test->rgba_map);
489          }
490      }
491 }
492
493 static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args, void *opaque)
494 {
495     TestSourceContext *test = ctx->priv;
496
497     test->class = &rgbtestsrc_class;
498     test->fill_picture_fn = rgbtest_fill_picture;
499     return init(ctx, args, opaque);
500 }
501
502 static int rgbtest_query_formats(AVFilterContext *ctx)
503 {
504     static const enum PixelFormat pix_fmts[] = {
505         PIX_FMT_RGBA, PIX_FMT_ARGB, PIX_FMT_BGRA, PIX_FMT_ABGR,
506         PIX_FMT_BGR24, PIX_FMT_RGB24,
507         PIX_FMT_RGB444, PIX_FMT_BGR444,
508         PIX_FMT_RGB565, PIX_FMT_BGR565,
509         PIX_FMT_RGB555, PIX_FMT_BGR555,
510         PIX_FMT_NONE
511     };
512     avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
513     return 0;
514 }
515
516 static int rgbtest_config_props(AVFilterLink *outlink)
517 {
518     TestSourceContext *test = outlink->src->priv;
519
520     switch (outlink->format) {
521     case PIX_FMT_ARGB:  test->rgba_map[A] = 0; test->rgba_map[R] = 1; test->rgba_map[G] = 2; test->rgba_map[B] = 3; break;
522     case PIX_FMT_ABGR:  test->rgba_map[A] = 0; test->rgba_map[B] = 1; test->rgba_map[G] = 2; test->rgba_map[R] = 3; break;
523     case PIX_FMT_RGBA:
524     case PIX_FMT_RGB24: test->rgba_map[R] = 0; test->rgba_map[G] = 1; test->rgba_map[B] = 2; test->rgba_map[A] = 3; break;
525     case PIX_FMT_BGRA:
526     case PIX_FMT_BGR24: test->rgba_map[B] = 0; test->rgba_map[G] = 1; test->rgba_map[R] = 2; test->rgba_map[A] = 3; break;
527     }
528
529     return config_props(outlink);
530 }
531
532 AVFilter avfilter_vsrc_rgbtestsrc = {
533     .name      = "rgbtestsrc",
534     .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
535     .priv_size = sizeof(TestSourceContext),
536     .init      = rgbtest_init,
537
538     .query_formats   = rgbtest_query_formats,
539
540     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
541
542     .outputs   = (const AVFilterPad[]) {{ .name = "default",
543                                     .type = AVMEDIA_TYPE_VIDEO,
544                                     .request_frame = request_frame,
545                                     .config_props  = rgbtest_config_props, },
546                                   { .name = NULL }},
547 };
548
549 #endif /* CONFIG_RGBTESTSRC_FILTER */