]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_testsrc.c
Merge commit '2d7d91f06d6a1d243dc74c96d3389ee237a3b906'
[ffmpeg] / libavfilter / vsrc_testsrc.c
1 /*
2  * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
3  * Copyright (c) 2011 Stefano Sabatini
4  * Copyright (c) 2012 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * Misc test sources.
26  *
27  * testsrc is based on the test pattern generator demuxer by Nicolas George:
28  * http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2007-October/037845.html
29  *
30  * rgbtestsrc is ported from MPlayer libmpcodecs/vf_rgbtest.c by
31  * Michael Niedermayer.
32  *
33  * smptebars is by Paul B Mahol.
34  */
35
36 #include <float.h>
37
38 #include "libavutil/common.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/intreadwrite.h"
42 #include "libavutil/parseutils.h"
43 #include "avfilter.h"
44 #include "drawutils.h"
45 #include "formats.h"
46 #include "internal.h"
47 #include "video.h"
48
49 typedef struct {
50     const AVClass *class;
51     int w, h;
52     unsigned int nb_frame;
53     AVRational time_base, frame_rate;
54     int64_t pts, max_pts;
55     char *frame_rate_str;       ///< video frame rate
56     char *duration;             ///< total duration of the generated video
57     AVRational sar;             ///< sample aspect ratio
58     int nb_decimals;
59     int draw_once;              ///< draw only the first frame, always put out the same picture
60     AVFilterBufferRef *picref;  ///< cached reference containing the painted picture
61
62     void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
63
64     /* only used by color */
65     char *color_str;
66     FFDrawContext draw;
67     FFDrawColor color;
68     uint8_t color_rgba[4];
69
70     /* only used by rgbtest */
71     uint8_t rgba_map[4];
72 } TestSourceContext;
73
74 #define OFFSET(x) offsetof(TestSourceContext, x)
75 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
76
77 static const AVOption options[] = {
78     { "size",     "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },
79     { "s",        "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },
80     { "rate",     "set video rate",     OFFSET(frame_rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
81     { "r",        "set video rate",     OFFSET(frame_rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
82     { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL},      0, 0, FLAGS },
83     { "d",        "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL},      0, 0, FLAGS },
84     { "sar",      "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1},  0, INT_MAX, FLAGS },
85
86     /* only used by color */
87     { "color", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
88     { "c",     "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
89
90     /* only used by testsrc */
91     { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0},  INT_MIN, INT_MAX, FLAGS },
92     { "n",        "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0},  INT_MIN, INT_MAX, FLAGS },
93     { NULL },
94 };
95
96 static av_cold int init(AVFilterContext *ctx, const char *args)
97 {
98     TestSourceContext *test = ctx->priv;
99     int64_t duration = -1;
100     int ret = 0;
101
102     av_opt_set_defaults(test);
103
104     if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0)
105         return ret;
106
107     if ((ret = av_parse_video_rate(&test->frame_rate, test->frame_rate_str)) < 0) {
108         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->frame_rate_str);
109         return ret;
110     }
111
112     if (test->duration && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
113         av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
114         return ret;
115     }
116
117     if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
118         av_log(ctx, AV_LOG_WARNING,
119                "Option 'decimals' is ignored with source '%s'\n",
120                ctx->filter->name);
121     }
122
123     if (test->color_str) {
124         if (!strcmp(ctx->filter->name, "color")) {
125             ret = av_parse_color(test->color_rgba, test->color_str, -1, ctx);
126             if (ret < 0)
127                 return ret;
128         } else {
129             av_log(ctx, AV_LOG_WARNING,
130                    "Option 'color' is ignored with source '%s'\n",
131                    ctx->filter->name);
132         }
133     }
134
135     test->time_base = av_inv_q(test->frame_rate);
136     test->max_pts = duration >= 0 ?
137         av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
138     test->nb_frame = 0;
139     test->pts = 0;
140
141     av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
142            test->w, test->h, test->frame_rate.num, test->frame_rate.den,
143            duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
144            test->sar.num, test->sar.den);
145     return 0;
146 }
147
148 static av_cold void uninit(AVFilterContext *ctx)
149 {
150     TestSourceContext *test = ctx->priv;
151
152     av_opt_free(test);
153     avfilter_unref_bufferp(&test->picref);
154 }
155
156 static int config_props(AVFilterLink *outlink)
157 {
158     TestSourceContext *test = outlink->src->priv;
159
160     outlink->w = test->w;
161     outlink->h = test->h;
162     outlink->sample_aspect_ratio = test->sar;
163     outlink->frame_rate = test->frame_rate;
164     outlink->time_base  = test->time_base;
165
166     return 0;
167 }
168
169 static int request_frame(AVFilterLink *outlink)
170 {
171     TestSourceContext *test = outlink->src->priv;
172     AVFilterBufferRef *outpicref;
173     int ret = 0;
174
175     if (test->max_pts >= 0 && test->pts >= test->max_pts)
176         return AVERROR_EOF;
177
178     if (test->draw_once) {
179         if (!test->picref) {
180             test->picref =
181                 ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_PRESERVE|AV_PERM_REUSE,
182                                     test->w, test->h);
183             if (!test->picref)
184                 return AVERROR(ENOMEM);
185             test->fill_picture_fn(outlink->src, test->picref);
186         }
187         outpicref = avfilter_ref_buffer(test->picref, ~AV_PERM_WRITE);
188     } else
189         outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, test->w, test->h);
190
191     if (!outpicref)
192         return AVERROR(ENOMEM);
193     outpicref->pts = test->pts;
194     outpicref->pos = -1;
195     outpicref->video->key_frame = 1;
196     outpicref->video->interlaced = 0;
197     outpicref->video->pict_type = AV_PICTURE_TYPE_I;
198     outpicref->video->sample_aspect_ratio = test->sar;
199     if (!test->draw_once)
200         test->fill_picture_fn(outlink->src, outpicref);
201
202     test->pts++;
203     test->nb_frame++;
204
205     if ((ret = ff_start_frame(outlink, outpicref)) < 0 ||
206         (ret = ff_draw_slice(outlink, 0, test->h, 1)) < 0 ||
207         (ret = ff_end_frame(outlink)) < 0)
208         return ret;
209
210     return 0;
211 }
212
213 #if CONFIG_COLOR_FILTER
214
215 #define color_options options
216 AVFILTER_DEFINE_CLASS(color);
217
218 static void color_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
219 {
220     TestSourceContext *test = ctx->priv;
221     ff_fill_rectangle(&test->draw, &test->color,
222                       picref->data, picref->linesize,
223                       0, 0, test->w, test->h);
224 }
225
226 static av_cold int color_init(AVFilterContext *ctx, const char *args)
227 {
228     TestSourceContext *test = ctx->priv;
229     test->class = &color_class;
230     test->fill_picture_fn = color_fill_picture;
231     test->draw_once = 1;
232     av_opt_set(test, "color", "black", 0);
233     return init(ctx, args);
234 }
235
236 static int color_query_formats(AVFilterContext *ctx)
237 {
238     ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
239     return 0;
240 }
241
242 static int color_config_props(AVFilterLink *inlink)
243 {
244     AVFilterContext *ctx = inlink->src;
245     TestSourceContext *test = ctx->priv;
246     int ret;
247
248     ff_draw_init(&test->draw, inlink->format, 0);
249     ff_draw_color(&test->draw, &test->color, test->color_rgba);
250
251     test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
252     test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
253     if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
254         return AVERROR(EINVAL);
255
256     if (ret = config_props(inlink) < 0)
257         return ret;
258
259     av_log(ctx, AV_LOG_VERBOSE, "color:0x%02x%02x%02x%02x\n",
260            test->color_rgba[0], test->color_rgba[1], test->color_rgba[2], test->color_rgba[3]);
261     return 0;
262 }
263
264 AVFilter avfilter_vsrc_color = {
265     .name        = "color",
266     .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
267
268     .priv_size = sizeof(TestSourceContext),
269     .init      = color_init,
270     .uninit    = uninit,
271
272     .query_formats = color_query_formats,
273
274     .inputs = (const AVFilterPad[]) {
275         { .name = NULL }
276     },
277
278     .outputs = (const AVFilterPad[]) {
279         {
280             .name            = "default",
281             .type            = AVMEDIA_TYPE_VIDEO,
282             .request_frame   = request_frame,
283             .config_props    = color_config_props,
284         },
285         { .name = NULL }
286     },
287
288     .priv_class = &color_class,
289 };
290
291 #endif /* CONFIG_COLOR_FILTER */
292
293 #if CONFIG_NULLSRC_FILTER
294
295 #define nullsrc_options options
296 AVFILTER_DEFINE_CLASS(nullsrc);
297
298 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref) { }
299
300 static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args)
301 {
302     TestSourceContext *test = ctx->priv;
303
304     test->class = &nullsrc_class;
305     test->fill_picture_fn = nullsrc_fill_picture;
306     return init(ctx, args);
307 }
308
309 AVFilter avfilter_vsrc_nullsrc = {
310     .name        = "nullsrc",
311     .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
312     .init       = nullsrc_init,
313     .uninit     = uninit,
314     .priv_size  = sizeof(TestSourceContext),
315
316     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
317     .outputs   = (const AVFilterPad[]) {{ .name = "default",
318                                     .type = AVMEDIA_TYPE_VIDEO,
319                                     .request_frame = request_frame,
320                                     .config_props  = config_props, },
321                                   { .name = NULL}},
322     .priv_class = &nullsrc_class,
323 };
324
325 #endif /* CONFIG_NULLSRC_FILTER */
326
327 #if CONFIG_TESTSRC_FILTER
328
329 #define testsrc_options options
330 AVFILTER_DEFINE_CLASS(testsrc);
331
332 /**
333  * Fill a rectangle with value val.
334  *
335  * @param val the RGB value to set
336  * @param dst pointer to the destination buffer to fill
337  * @param dst_linesize linesize of destination
338  * @param segment_width width of the segment
339  * @param x horizontal coordinate where to draw the rectangle in the destination buffer
340  * @param y horizontal coordinate where to draw the rectangle in the destination buffer
341  * @param w width  of the rectangle to draw, expressed as a number of segment_width units
342  * @param h height of the rectangle to draw, expressed as a number of segment_width units
343  */
344 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
345                            unsigned x, unsigned y, unsigned w, unsigned h)
346 {
347     int i;
348     int step = 3;
349
350     dst += segment_width * (step * x + y * dst_linesize);
351     w *= segment_width * step;
352     h *= segment_width;
353     for (i = 0; i < h; i++) {
354         memset(dst, val, w);
355         dst += dst_linesize;
356     }
357 }
358
359 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
360                        unsigned segment_width)
361 {
362 #define TOP_HBAR        1
363 #define MID_HBAR        2
364 #define BOT_HBAR        4
365 #define LEFT_TOP_VBAR   8
366 #define LEFT_BOT_VBAR  16
367 #define RIGHT_TOP_VBAR 32
368 #define RIGHT_BOT_VBAR 64
369     struct {
370         int x, y, w, h;
371     } segments[] = {
372         { 1,  0, 5, 1 }, /* TOP_HBAR */
373         { 1,  6, 5, 1 }, /* MID_HBAR */
374         { 1, 12, 5, 1 }, /* BOT_HBAR */
375         { 0,  1, 1, 5 }, /* LEFT_TOP_VBAR */
376         { 0,  7, 1, 5 }, /* LEFT_BOT_VBAR */
377         { 6,  1, 1, 5 }, /* RIGHT_TOP_VBAR */
378         { 6,  7, 1, 5 }  /* RIGHT_BOT_VBAR */
379     };
380     static const unsigned char masks[10] = {
381         /* 0 */ TOP_HBAR         |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
382         /* 1 */                                                        RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
383         /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR                             |RIGHT_TOP_VBAR,
384         /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR                            |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
385         /* 4 */          MID_HBAR         |LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
386         /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR                             |RIGHT_BOT_VBAR,
387         /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR               |RIGHT_BOT_VBAR,
388         /* 7 */ TOP_HBAR                                              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
389         /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
390         /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
391     };
392     unsigned mask = masks[digit];
393     int i;
394
395     draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
396     for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
397         if (mask & (1<<i))
398             draw_rectangle(255, dst, dst_linesize, segment_width,
399                            segments[i].x, segments[i].y, segments[i].w, segments[i].h);
400 }
401
402 #define GRADIENT_SIZE (6 * 256)
403
404 static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
405 {
406     TestSourceContext *test = ctx->priv;
407     uint8_t *p, *p0;
408     int x, y;
409     int color, color_rest;
410     int icolor;
411     int radius;
412     int quad0, quad;
413     int dquad_x, dquad_y;
414     int grad, dgrad, rgrad, drgrad;
415     int seg_size;
416     int second;
417     int i;
418     uint8_t *data = picref->data[0];
419     int width  = picref->video->w;
420     int height = picref->video->h;
421
422     /* draw colored bars and circle */
423     radius = (width + height) / 4;
424     quad0 = width * width / 4 + height * height / 4 - radius * radius;
425     dquad_y = 1 - height;
426     p0 = data;
427     for (y = 0; y < height; y++) {
428         p = p0;
429         color = 0;
430         color_rest = 0;
431         quad = quad0;
432         dquad_x = 1 - width;
433         for (x = 0; x < width; x++) {
434             icolor = color;
435             if (quad < 0)
436                 icolor ^= 7;
437             quad += dquad_x;
438             dquad_x += 2;
439             *(p++) = icolor & 1 ? 255 : 0;
440             *(p++) = icolor & 2 ? 255 : 0;
441             *(p++) = icolor & 4 ? 255 : 0;
442             color_rest += 8;
443             if (color_rest >= width) {
444                 color_rest -= width;
445                 color++;
446             }
447         }
448         quad0 += dquad_y;
449         dquad_y += 2;
450         p0 += picref->linesize[0];
451     }
452
453     /* draw sliding color line */
454     p0 = p = data + picref->linesize[0] * height * 3/4;
455     grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
456         GRADIENT_SIZE;
457     rgrad = 0;
458     dgrad = GRADIENT_SIZE / width;
459     drgrad = GRADIENT_SIZE % width;
460     for (x = 0; x < width; x++) {
461         *(p++) =
462             grad < 256 || grad >= 5 * 256 ? 255 :
463             grad >= 2 * 256 && grad < 4 * 256 ? 0 :
464             grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
465         *(p++) =
466             grad >= 4 * 256 ? 0 :
467             grad >= 1 * 256 && grad < 3 * 256 ? 255 :
468             grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
469         *(p++) =
470             grad < 2 * 256 ? 0 :
471             grad >= 3 * 256 && grad < 5 * 256 ? 255 :
472             grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
473         grad += dgrad;
474         rgrad += drgrad;
475         if (rgrad >= GRADIENT_SIZE) {
476             grad++;
477             rgrad -= GRADIENT_SIZE;
478         }
479         if (grad >= GRADIENT_SIZE)
480             grad -= GRADIENT_SIZE;
481     }
482     p = p0;
483     for (y = height / 8; y > 0; y--) {
484         memcpy(p+picref->linesize[0], p, 3 * width);
485         p += picref->linesize[0];
486     }
487
488     /* draw digits */
489     seg_size = width / 80;
490     if (seg_size >= 1 && height >= 13 * seg_size) {
491         double time = av_q2d(test->time_base) * test->nb_frame *
492                       pow(10, test->nb_decimals);
493         if (time > INT_MAX)
494             return;
495         second = (int)time;
496         x = width - (width - seg_size * 64) / 2;
497         y = (height - seg_size * 13) / 2;
498         p = data + (x*3 + y * picref->linesize[0]);
499         for (i = 0; i < 8; i++) {
500             p -= 3 * 8 * seg_size;
501             draw_digit(second % 10, p, picref->linesize[0], seg_size);
502             second /= 10;
503             if (second == 0)
504                 break;
505         }
506     }
507 }
508
509 static av_cold int test_init(AVFilterContext *ctx, const char *args)
510 {
511     TestSourceContext *test = ctx->priv;
512
513     test->class = &testsrc_class;
514     test->fill_picture_fn = test_fill_picture;
515     return init(ctx, args);
516 }
517
518 static int test_query_formats(AVFilterContext *ctx)
519 {
520     static const enum PixelFormat pix_fmts[] = {
521         PIX_FMT_RGB24, PIX_FMT_NONE
522     };
523     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
524     return 0;
525 }
526
527 AVFilter avfilter_vsrc_testsrc = {
528     .name      = "testsrc",
529     .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
530     .priv_size = sizeof(TestSourceContext),
531     .init      = test_init,
532     .uninit    = uninit,
533
534     .query_formats   = test_query_formats,
535
536     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
537
538     .outputs   = (const AVFilterPad[]) {{ .name = "default",
539                                           .type = AVMEDIA_TYPE_VIDEO,
540                                           .request_frame = request_frame,
541                                           .config_props  = config_props, },
542                                         { .name = NULL }},
543     .priv_class = &testsrc_class,
544 };
545
546 #endif /* CONFIG_TESTSRC_FILTER */
547
548 #if CONFIG_RGBTESTSRC_FILTER
549
550 #define rgbtestsrc_options options
551 AVFILTER_DEFINE_CLASS(rgbtestsrc);
552
553 #define R 0
554 #define G 1
555 #define B 2
556 #define A 3
557
558 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
559                               int x, int y, int r, int g, int b, enum PixelFormat fmt,
560                               uint8_t rgba_map[4])
561 {
562     int32_t v;
563     uint8_t *p;
564
565     switch (fmt) {
566     case PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
567     case PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
568     case PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
569     case PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
570     case PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
571     case PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
572     case PIX_FMT_RGB24:
573     case PIX_FMT_BGR24:
574         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
575         p = dst + 3*x + y*dst_linesize;
576         AV_WL24(p, v);
577         break;
578     case PIX_FMT_RGBA:
579     case PIX_FMT_BGRA:
580     case PIX_FMT_ARGB:
581     case PIX_FMT_ABGR:
582         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
583         p = dst + 4*x + y*dst_linesize;
584         AV_WL32(p, v);
585         break;
586     }
587 }
588
589 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
590 {
591     TestSourceContext *test = ctx->priv;
592     int x, y, w = picref->video->w, h = picref->video->h;
593
594     for (y = 0; y < h; y++) {
595          for (x = 0; x < picref->video->w; x++) {
596              int c = 256*x/w;
597              int r = 0, g = 0, b = 0;
598
599              if      (3*y < h  ) r = c;
600              else if (3*y < 2*h) g = c;
601              else                b = c;
602
603              rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
604                                ctx->outputs[0]->format, test->rgba_map);
605          }
606      }
607 }
608
609 static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
610 {
611     TestSourceContext *test = ctx->priv;
612
613     test->draw_once = 1;
614     test->class = &rgbtestsrc_class;
615     test->fill_picture_fn = rgbtest_fill_picture;
616     return init(ctx, args);
617 }
618
619 static int rgbtest_query_formats(AVFilterContext *ctx)
620 {
621     static const enum PixelFormat pix_fmts[] = {
622         PIX_FMT_RGBA, PIX_FMT_ARGB, PIX_FMT_BGRA, PIX_FMT_ABGR,
623         PIX_FMT_BGR24, PIX_FMT_RGB24,
624         PIX_FMT_RGB444, PIX_FMT_BGR444,
625         PIX_FMT_RGB565, PIX_FMT_BGR565,
626         PIX_FMT_RGB555, PIX_FMT_BGR555,
627         PIX_FMT_NONE
628     };
629     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
630     return 0;
631 }
632
633 static int rgbtest_config_props(AVFilterLink *outlink)
634 {
635     TestSourceContext *test = outlink->src->priv;
636
637     ff_fill_rgba_map(test->rgba_map, outlink->format);
638     return config_props(outlink);
639 }
640
641 AVFilter avfilter_vsrc_rgbtestsrc = {
642     .name      = "rgbtestsrc",
643     .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
644     .priv_size = sizeof(TestSourceContext),
645     .init      = rgbtest_init,
646     .uninit    = uninit,
647
648     .query_formats   = rgbtest_query_formats,
649
650     .inputs    = (const AVFilterPad[]) {{ .name = NULL}},
651
652     .outputs   = (const AVFilterPad[]) {{ .name = "default",
653                                           .type = AVMEDIA_TYPE_VIDEO,
654                                           .request_frame = request_frame,
655                                           .config_props  = rgbtest_config_props, },
656                                         { .name = NULL }},
657     .priv_class = &rgbtestsrc_class,
658 };
659
660 #endif /* CONFIG_RGBTESTSRC_FILTER */
661
662 #if CONFIG_SMPTEBARS_FILTER
663
664 #define smptebars_options options
665 AVFILTER_DEFINE_CLASS(smptebars);
666
667 static const uint8_t rainbow[7][4] = {
668     { 191, 191, 191, 255 },     /* gray */
669     { 191, 191,   0, 255 },     /* yellow */
670     {   0, 191, 191, 255 },     /* cyan */
671     {   0, 191,   0, 255 },     /* green */
672     { 191,   0, 191, 255 },     /* magenta */
673     { 191,   0,   0, 255 },     /* red */
674     {   0,   0, 191, 255 },     /* blue */
675 };
676
677 static const uint8_t wobnair[7][4] = {
678     {   0,   0, 191, 255 },     /* blue */
679     {  19,  19,  19, 255 },     /* 7.5% intensity black */
680     { 191,   0, 191, 255 },     /* magenta */
681     {  19,  19,  19, 255 },     /* 7.5% intensity black */
682     {   0, 191, 191, 255 },     /* cyan */
683     {  19,  19,  19, 255 },     /* 7.5% intensity black */
684     { 191, 191, 191, 255 },     /* gray */
685 };
686
687 static const uint8_t white[4] = { 255, 255, 255, 255 };
688 static const uint8_t black[4] = {  19,  19,  19, 255 }; /* 7.5% intensity black */
689
690 /* pluge pulses */
691 static const uint8_t neg4ire[4] = {   9,   9,   9, 255 }; /*  3.5% intensity black */
692 static const uint8_t pos4ire[4] = {  29,  29,  29, 255 }; /* 11.5% intensity black */
693
694 /* fudged Q/-I */
695 static const uint8_t i_pixel[4] = {   0,  68, 130, 255 };
696 static const uint8_t q_pixel[4] = {  67,   0, 130, 255 };
697
698 static void smptebars_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
699 {
700     TestSourceContext *test = ctx->priv;
701     FFDrawColor color;
702     int r_w, r_h, w_h, p_w, p_h, i, x = 0;
703
704     r_w = (test->w + 6) / 7;
705     r_h = test->h * 2 / 3;
706     w_h = test->h * 3 / 4 - r_h;
707     p_w = r_w * 5 / 4;
708     p_h = test->h - w_h - r_h;
709
710 #define DRAW_COLOR(rgba, x, y, w, h)                                    \
711     ff_draw_color(&test->draw, &color, rgba);                           \
712     ff_fill_rectangle(&test->draw, &color,                              \
713                       picref->data, picref->linesize, x, y, w, h)       \
714
715     for (i = 0; i < 7; i++) {
716         DRAW_COLOR(rainbow[i], x, 0,   FFMIN(r_w, test->w - x), r_h);
717         DRAW_COLOR(wobnair[i], x, r_h, FFMIN(r_w, test->w - x), w_h);
718         x += r_w;
719     }
720     x = 0;
721     DRAW_COLOR(i_pixel, x, r_h + w_h, p_w, p_h);
722     x += p_w;
723     DRAW_COLOR(white, x, r_h + w_h, p_w, p_h);
724     x += p_w;
725     DRAW_COLOR(q_pixel, x, r_h + w_h, p_w, p_h);
726     x += p_w;
727     DRAW_COLOR(black, x, r_h + w_h, 5 * r_w - x, p_h);
728     x += 5 * r_w - x;
729     DRAW_COLOR(neg4ire, x, r_h + w_h, r_w / 3, p_h);
730     x += r_w / 3;
731     DRAW_COLOR(black, x, r_h + w_h, r_w / 3, p_h);
732     x += r_w / 3;
733     DRAW_COLOR(pos4ire, x, r_h + w_h, r_w / 3, p_h);
734     x += r_w / 3;
735     DRAW_COLOR(black, x, r_h + w_h, test->w - x, p_h);
736 }
737
738 static av_cold int smptebars_init(AVFilterContext *ctx, const char *args)
739 {
740     TestSourceContext *test = ctx->priv;
741
742     test->class = &smptebars_class;
743     test->fill_picture_fn = smptebars_fill_picture;
744     test->draw_once = 1;
745     return init(ctx, args);
746 }
747
748 static int smptebars_query_formats(AVFilterContext *ctx)
749 {
750     ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
751     return 0;
752 }
753
754 static int smptebars_config_props(AVFilterLink *outlink)
755 {
756     AVFilterContext *ctx = outlink->src;
757     TestSourceContext *test = ctx->priv;
758
759     ff_draw_init(&test->draw, outlink->format, 0);
760
761     return config_props(outlink);
762 }
763
764 AVFilter avfilter_vsrc_smptebars = {
765     .name      = "smptebars",
766     .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
767     .priv_size = sizeof(TestSourceContext),
768     .init      = smptebars_init,
769     .uninit    = uninit,
770
771     .query_formats = smptebars_query_formats,
772
773     .inputs = (const AVFilterPad[]) {
774         { .name = NULL }
775     },
776
777     .outputs = (const AVFilterPad[]) {
778         {
779             .name = "default",
780             .type = AVMEDIA_TYPE_VIDEO,
781             .request_frame = request_frame,
782             .config_props  = smptebars_config_props,
783         },
784         { .name = NULL }
785     },
786
787     .priv_class = &smptebars_class,
788 };
789
790 #endif  /* CONFIG_SMPTEBARS_FILTER */