]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_testsrc.c
Merge commit '556aab8f11b045a21182eee32413aa78d5c8539b'
[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;
55     char *frame_rate_str;       ///< video frame rate
56     char *duration_str;         ///< total duration of the generated video
57     int64_t duration;           ///< duration expressed in microseconds
58     AVRational sar;             ///< sample aspect ratio
59     int nb_decimals;
60     int draw_once;              ///< draw only the first frame, always put out the same picture
61     AVFrame *picref;            ///< cached reference containing the painted picture
62
63     void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
64
65     /* only used by color */
66     char *color_str;
67     FFDrawContext draw;
68     FFDrawColor color;
69     uint8_t color_rgba[4];
70
71     /* only used by rgbtest */
72     uint8_t rgba_map[4];
73 } TestSourceContext;
74
75 #define OFFSET(x) offsetof(TestSourceContext, x)
76 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
77
78 static const AVOption options[] = {
79     { "size",     "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },
80     { "s",        "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },
81     { "rate",     "set video rate",     OFFSET(frame_rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
82     { "r",        "set video rate",     OFFSET(frame_rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
83     { "duration", "set video duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL},   0, 0, FLAGS },
84     { "d",        "set video duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL},   0, 0, FLAGS },
85     { "sar",      "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1},  0, INT_MAX, FLAGS },
86
87     /* only used by color */
88     { "color", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
89     { "c",     "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
90
91     /* only used by testsrc */
92     { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0},  INT_MIN, INT_MAX, FLAGS },
93     { "n",        "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0},  INT_MIN, INT_MAX, FLAGS },
94     { NULL },
95 };
96
97 static av_cold int init(AVFilterContext *ctx, const char *args)
98 {
99     TestSourceContext *test = ctx->priv;
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     test->duration = -1;
113     if (test->duration_str &&
114         (ret = av_parse_time(&test->duration, test->duration_str, 1)) < 0) {
115         av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration_str);
116         return ret;
117     }
118
119     if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
120         av_log(ctx, AV_LOG_WARNING,
121                "Option 'decimals' is ignored with source '%s'\n",
122                ctx->filter->name);
123     }
124
125     if (test->color_str) {
126         if (!strcmp(ctx->filter->name, "color")) {
127             ret = av_parse_color(test->color_rgba, test->color_str, -1, ctx);
128             if (ret < 0)
129                 return ret;
130         } else {
131             av_log(ctx, AV_LOG_WARNING,
132                    "Option 'color' is ignored with source '%s'\n",
133                    ctx->filter->name);
134         }
135     }
136
137     test->time_base = av_inv_q(test->frame_rate);
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            test->duration < 0 ? -1 : (double)test->duration/1000000,
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     av_frame_free(&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     AVFrame *frame;
173
174     if (test->duration >= 0 &&
175         av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
176         return AVERROR_EOF;
177
178     if (test->draw_once) {
179         if (!test->picref) {
180             test->picref =
181                 ff_get_video_buffer(outlink, test->w, test->h);
182             if (!test->picref)
183                 return AVERROR(ENOMEM);
184             test->fill_picture_fn(outlink->src, test->picref);
185         }
186         frame = av_frame_clone(test->picref);
187     } else
188         frame = ff_get_video_buffer(outlink, test->w, test->h);
189
190     if (!frame)
191         return AVERROR(ENOMEM);
192     frame->pts                 = test->pts;
193     frame->key_frame           = 1;
194     frame->interlaced_frame    = 0;
195     frame->pict_type           = AV_PICTURE_TYPE_I;
196     frame->sample_aspect_ratio = test->sar;
197     if (!test->draw_once)
198         test->fill_picture_fn(outlink->src, frame);
199
200     test->pts++;
201     test->nb_frame++;
202
203     return ff_filter_frame(outlink, frame);
204 }
205
206 #if CONFIG_COLOR_FILTER
207
208 #define color_options options
209 AVFILTER_DEFINE_CLASS(color);
210
211 static void color_fill_picture(AVFilterContext *ctx, AVFrame *picref)
212 {
213     TestSourceContext *test = ctx->priv;
214     ff_fill_rectangle(&test->draw, &test->color,
215                       picref->data, picref->linesize,
216                       0, 0, test->w, test->h);
217 }
218
219 static av_cold int color_init(AVFilterContext *ctx, const char *args)
220 {
221     TestSourceContext *test = ctx->priv;
222     test->class = &color_class;
223     test->fill_picture_fn = color_fill_picture;
224     test->draw_once = 1;
225     av_opt_set(test, "color", "black", 0);
226     return init(ctx, args);
227 }
228
229 static int color_query_formats(AVFilterContext *ctx)
230 {
231     ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
232     return 0;
233 }
234
235 static int color_config_props(AVFilterLink *inlink)
236 {
237     AVFilterContext *ctx = inlink->src;
238     TestSourceContext *test = ctx->priv;
239     int ret;
240
241     ff_draw_init(&test->draw, inlink->format, 0);
242     ff_draw_color(&test->draw, &test->color, test->color_rgba);
243
244     test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
245     test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
246     if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
247         return AVERROR(EINVAL);
248
249     if ((ret = config_props(inlink)) < 0)
250         return ret;
251
252     av_log(ctx, AV_LOG_VERBOSE, "color:0x%02x%02x%02x%02x\n",
253            test->color_rgba[0], test->color_rgba[1], test->color_rgba[2], test->color_rgba[3]);
254     return 0;
255 }
256
257 static const AVFilterPad color_outputs[] = {
258     {
259         .name          = "default",
260         .type          = AVMEDIA_TYPE_VIDEO,
261         .request_frame = request_frame,
262         .config_props  = color_config_props,
263     },
264     {  NULL }
265 };
266
267 AVFilter avfilter_vsrc_color = {
268     .name        = "color",
269     .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
270
271     .priv_size = sizeof(TestSourceContext),
272     .init      = color_init,
273     .uninit    = uninit,
274
275     .query_formats = color_query_formats,
276     .inputs        = NULL,
277     .outputs       = color_outputs,
278     .priv_class    = &color_class,
279 };
280
281 #endif /* CONFIG_COLOR_FILTER */
282
283 #if CONFIG_NULLSRC_FILTER
284
285 #define nullsrc_options options
286 AVFILTER_DEFINE_CLASS(nullsrc);
287
288 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFrame *picref) { }
289
290 static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args)
291 {
292     TestSourceContext *test = ctx->priv;
293
294     test->class = &nullsrc_class;
295     test->fill_picture_fn = nullsrc_fill_picture;
296     return init(ctx, args);
297 }
298
299 static const AVFilterPad nullsrc_outputs[] = {
300     {
301         .name          = "default",
302         .type          = AVMEDIA_TYPE_VIDEO,
303         .request_frame = request_frame,
304         .config_props  = config_props,
305     },
306     { NULL },
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     .inputs     = NULL,
316     .outputs    = nullsrc_outputs,
317     .priv_class = &nullsrc_class,
318 };
319
320 #endif /* CONFIG_NULLSRC_FILTER */
321
322 #if CONFIG_TESTSRC_FILTER
323
324 #define testsrc_options options
325 AVFILTER_DEFINE_CLASS(testsrc);
326
327 /**
328  * Fill a rectangle with value val.
329  *
330  * @param val the RGB value to set
331  * @param dst pointer to the destination buffer to fill
332  * @param dst_linesize linesize of destination
333  * @param segment_width width of the segment
334  * @param x horizontal coordinate where to draw the rectangle in the destination buffer
335  * @param y horizontal coordinate where to draw the rectangle in the destination buffer
336  * @param w width  of the rectangle to draw, expressed as a number of segment_width units
337  * @param h height of the rectangle to draw, expressed as a number of segment_width units
338  */
339 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
340                            unsigned x, unsigned y, unsigned w, unsigned h)
341 {
342     int i;
343     int step = 3;
344
345     dst += segment_width * (step * x + y * dst_linesize);
346     w *= segment_width * step;
347     h *= segment_width;
348     for (i = 0; i < h; i++) {
349         memset(dst, val, w);
350         dst += dst_linesize;
351     }
352 }
353
354 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
355                        unsigned segment_width)
356 {
357 #define TOP_HBAR        1
358 #define MID_HBAR        2
359 #define BOT_HBAR        4
360 #define LEFT_TOP_VBAR   8
361 #define LEFT_BOT_VBAR  16
362 #define RIGHT_TOP_VBAR 32
363 #define RIGHT_BOT_VBAR 64
364     struct {
365         int x, y, w, h;
366     } segments[] = {
367         { 1,  0, 5, 1 }, /* TOP_HBAR */
368         { 1,  6, 5, 1 }, /* MID_HBAR */
369         { 1, 12, 5, 1 }, /* BOT_HBAR */
370         { 0,  1, 1, 5 }, /* LEFT_TOP_VBAR */
371         { 0,  7, 1, 5 }, /* LEFT_BOT_VBAR */
372         { 6,  1, 1, 5 }, /* RIGHT_TOP_VBAR */
373         { 6,  7, 1, 5 }  /* RIGHT_BOT_VBAR */
374     };
375     static const unsigned char masks[10] = {
376         /* 0 */ TOP_HBAR         |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
377         /* 1 */                                                        RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
378         /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR                             |RIGHT_TOP_VBAR,
379         /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR                            |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
380         /* 4 */          MID_HBAR         |LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
381         /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR                             |RIGHT_BOT_VBAR,
382         /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR               |RIGHT_BOT_VBAR,
383         /* 7 */ TOP_HBAR                                              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
384         /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
385         /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
386     };
387     unsigned mask = masks[digit];
388     int i;
389
390     draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
391     for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
392         if (mask & (1<<i))
393             draw_rectangle(255, dst, dst_linesize, segment_width,
394                            segments[i].x, segments[i].y, segments[i].w, segments[i].h);
395 }
396
397 #define GRADIENT_SIZE (6 * 256)
398
399 static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
400 {
401     TestSourceContext *test = ctx->priv;
402     uint8_t *p, *p0;
403     int x, y;
404     int color, color_rest;
405     int icolor;
406     int radius;
407     int quad0, quad;
408     int dquad_x, dquad_y;
409     int grad, dgrad, rgrad, drgrad;
410     int seg_size;
411     int second;
412     int i;
413     uint8_t *data = frame->data[0];
414     int width  = frame->width;
415     int height = frame->height;
416
417     /* draw colored bars and circle */
418     radius = (width + height) / 4;
419     quad0 = width * width / 4 + height * height / 4 - radius * radius;
420     dquad_y = 1 - height;
421     p0 = data;
422     for (y = 0; y < height; y++) {
423         p = p0;
424         color = 0;
425         color_rest = 0;
426         quad = quad0;
427         dquad_x = 1 - width;
428         for (x = 0; x < width; x++) {
429             icolor = color;
430             if (quad < 0)
431                 icolor ^= 7;
432             quad += dquad_x;
433             dquad_x += 2;
434             *(p++) = icolor & 1 ? 255 : 0;
435             *(p++) = icolor & 2 ? 255 : 0;
436             *(p++) = icolor & 4 ? 255 : 0;
437             color_rest += 8;
438             if (color_rest >= width) {
439                 color_rest -= width;
440                 color++;
441             }
442         }
443         quad0 += dquad_y;
444         dquad_y += 2;
445         p0 += frame->linesize[0];
446     }
447
448     /* draw sliding color line */
449     p0 = p = data + frame->linesize[0] * height * 3/4;
450     grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
451         GRADIENT_SIZE;
452     rgrad = 0;
453     dgrad = GRADIENT_SIZE / width;
454     drgrad = GRADIENT_SIZE % width;
455     for (x = 0; x < width; x++) {
456         *(p++) =
457             grad < 256 || grad >= 5 * 256 ? 255 :
458             grad >= 2 * 256 && grad < 4 * 256 ? 0 :
459             grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
460         *(p++) =
461             grad >= 4 * 256 ? 0 :
462             grad >= 1 * 256 && grad < 3 * 256 ? 255 :
463             grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
464         *(p++) =
465             grad < 2 * 256 ? 0 :
466             grad >= 3 * 256 && grad < 5 * 256 ? 255 :
467             grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
468         grad += dgrad;
469         rgrad += drgrad;
470         if (rgrad >= GRADIENT_SIZE) {
471             grad++;
472             rgrad -= GRADIENT_SIZE;
473         }
474         if (grad >= GRADIENT_SIZE)
475             grad -= GRADIENT_SIZE;
476     }
477     p = p0;
478     for (y = height / 8; y > 0; y--) {
479         memcpy(p+frame->linesize[0], p, 3 * width);
480         p += frame->linesize[0];
481     }
482
483     /* draw digits */
484     seg_size = width / 80;
485     if (seg_size >= 1 && height >= 13 * seg_size) {
486         double time = av_q2d(test->time_base) * test->nb_frame *
487                       pow(10, test->nb_decimals);
488         if (time > INT_MAX)
489             return;
490         second = (int)time;
491         x = width - (width - seg_size * 64) / 2;
492         y = (height - seg_size * 13) / 2;
493         p = data + (x*3 + y * frame->linesize[0]);
494         for (i = 0; i < 8; i++) {
495             p -= 3 * 8 * seg_size;
496             draw_digit(second % 10, p, frame->linesize[0], seg_size);
497             second /= 10;
498             if (second == 0)
499                 break;
500         }
501     }
502 }
503
504 static av_cold int test_init(AVFilterContext *ctx, const char *args)
505 {
506     TestSourceContext *test = ctx->priv;
507
508     test->class = &testsrc_class;
509     test->fill_picture_fn = test_fill_picture;
510     return init(ctx, args);
511 }
512
513 static int test_query_formats(AVFilterContext *ctx)
514 {
515     static const enum AVPixelFormat pix_fmts[] = {
516         AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
517     };
518     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
519     return 0;
520 }
521
522 static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
523     {
524         .name          = "default",
525         .type          = AVMEDIA_TYPE_VIDEO,
526         .request_frame = request_frame,
527         .config_props  = config_props,
528     },
529     { NULL }
530 };
531
532 AVFilter avfilter_vsrc_testsrc = {
533     .name      = "testsrc",
534     .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
535     .priv_size = sizeof(TestSourceContext),
536     .init      = test_init,
537     .uninit    = uninit,
538
539     .query_formats   = test_query_formats,
540
541     .inputs    = NULL,
542     .outputs   = avfilter_vsrc_testsrc_outputs,
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 AVPixelFormat fmt,
560                               uint8_t rgba_map[4])
561 {
562     int32_t v;
563     uint8_t *p;
564
565     switch (fmt) {
566     case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
567     case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
568     case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
569     case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
570     case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
571     case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
572     case AV_PIX_FMT_RGB24:
573     case AV_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 AV_PIX_FMT_RGBA:
579     case AV_PIX_FMT_BGRA:
580     case AV_PIX_FMT_ARGB:
581     case AV_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, AVFrame *frame)
590 {
591     TestSourceContext *test = ctx->priv;
592     int x, y, w = frame->width, h = frame->height;
593
594     for (y = 0; y < h; y++) {
595          for (x = 0; x < 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(frame->data[0], frame->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 AVPixelFormat pix_fmts[] = {
622         AV_PIX_FMT_RGBA, AV_PIX_FMT_ARGB, AV_PIX_FMT_BGRA, AV_PIX_FMT_ABGR,
623         AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
624         AV_PIX_FMT_RGB444, AV_PIX_FMT_BGR444,
625         AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565,
626         AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555,
627         AV_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 static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
642     {
643         .name          = "default",
644         .type          = AVMEDIA_TYPE_VIDEO,
645         .request_frame = request_frame,
646         .config_props  = rgbtest_config_props,
647     },
648     { NULL }
649 };
650
651 AVFilter avfilter_vsrc_rgbtestsrc = {
652     .name      = "rgbtestsrc",
653     .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
654     .priv_size = sizeof(TestSourceContext),
655     .init      = rgbtest_init,
656     .uninit    = uninit,
657
658     .query_formats   = rgbtest_query_formats,
659
660     .inputs    = NULL,
661
662     .outputs   = avfilter_vsrc_rgbtestsrc_outputs,
663     .priv_class = &rgbtestsrc_class,
664 };
665
666 #endif /* CONFIG_RGBTESTSRC_FILTER */
667
668 #if CONFIG_SMPTEBARS_FILTER
669
670 #define smptebars_options options
671 AVFILTER_DEFINE_CLASS(smptebars);
672
673 static const uint8_t rainbow[7][4] = {
674     { 191, 191, 191, 255 },     /* gray */
675     { 191, 191,   0, 255 },     /* yellow */
676     {   0, 191, 191, 255 },     /* cyan */
677     {   0, 191,   0, 255 },     /* green */
678     { 191,   0, 191, 255 },     /* magenta */
679     { 191,   0,   0, 255 },     /* red */
680     {   0,   0, 191, 255 },     /* blue */
681 };
682
683 static const uint8_t wobnair[7][4] = {
684     {   0,   0, 191, 255 },     /* blue */
685     {  19,  19,  19, 255 },     /* 7.5% intensity black */
686     { 191,   0, 191, 255 },     /* magenta */
687     {  19,  19,  19, 255 },     /* 7.5% intensity black */
688     {   0, 191, 191, 255 },     /* cyan */
689     {  19,  19,  19, 255 },     /* 7.5% intensity black */
690     { 191, 191, 191, 255 },     /* gray */
691 };
692
693 static const uint8_t white[4] = { 255, 255, 255, 255 };
694 static const uint8_t black[4] = {  19,  19,  19, 255 }; /* 7.5% intensity black */
695
696 /* pluge pulses */
697 static const uint8_t neg4ire[4] = {   9,   9,   9, 255 }; /*  3.5% intensity black */
698 static const uint8_t pos4ire[4] = {  29,  29,  29, 255 }; /* 11.5% intensity black */
699
700 /* fudged Q/-I */
701 static const uint8_t i_pixel[4] = {   0,  68, 130, 255 };
702 static const uint8_t q_pixel[4] = {  67,   0, 130, 255 };
703
704 static void smptebars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
705 {
706     TestSourceContext *test = ctx->priv;
707     FFDrawColor color;
708     int r_w, r_h, w_h, p_w, p_h, i, x = 0;
709
710     r_w = (test->w + 6) / 7;
711     r_h = test->h * 2 / 3;
712     w_h = test->h * 3 / 4 - r_h;
713     p_w = r_w * 5 / 4;
714     p_h = test->h - w_h - r_h;
715
716 #define DRAW_COLOR(rgba, x, y, w, h)                                    \
717     ff_draw_color(&test->draw, &color, rgba);                           \
718     ff_fill_rectangle(&test->draw, &color,                              \
719                       picref->data, picref->linesize, x, y, w, h)       \
720
721     for (i = 0; i < 7; i++) {
722         DRAW_COLOR(rainbow[i], x, 0,   FFMIN(r_w, test->w - x), r_h);
723         DRAW_COLOR(wobnair[i], x, r_h, FFMIN(r_w, test->w - x), w_h);
724         x += r_w;
725     }
726     x = 0;
727     DRAW_COLOR(i_pixel, x, r_h + w_h, p_w, p_h);
728     x += p_w;
729     DRAW_COLOR(white, x, r_h + w_h, p_w, p_h);
730     x += p_w;
731     DRAW_COLOR(q_pixel, x, r_h + w_h, p_w, p_h);
732     x += p_w;
733     DRAW_COLOR(black, x, r_h + w_h, 5 * r_w - x, p_h);
734     x += 5 * r_w - x;
735     DRAW_COLOR(neg4ire, x, r_h + w_h, r_w / 3, p_h);
736     x += r_w / 3;
737     DRAW_COLOR(black, x, r_h + w_h, r_w / 3, p_h);
738     x += r_w / 3;
739     DRAW_COLOR(pos4ire, x, r_h + w_h, r_w / 3, p_h);
740     x += r_w / 3;
741     DRAW_COLOR(black, x, r_h + w_h, test->w - x, p_h);
742 }
743
744 static av_cold int smptebars_init(AVFilterContext *ctx, const char *args)
745 {
746     TestSourceContext *test = ctx->priv;
747
748     test->class = &smptebars_class;
749     test->fill_picture_fn = smptebars_fill_picture;
750     test->draw_once = 1;
751     return init(ctx, args);
752 }
753
754 static int smptebars_query_formats(AVFilterContext *ctx)
755 {
756     ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
757     return 0;
758 }
759
760 static int smptebars_config_props(AVFilterLink *outlink)
761 {
762     AVFilterContext *ctx = outlink->src;
763     TestSourceContext *test = ctx->priv;
764
765     ff_draw_init(&test->draw, outlink->format, 0);
766
767     return config_props(outlink);
768 }
769
770 static const AVFilterPad smptebars_outputs[] = {
771     {
772         .name          = "default",
773         .type          = AVMEDIA_TYPE_VIDEO,
774         .request_frame = request_frame,
775         .config_props  = smptebars_config_props,
776     },
777     { NULL }
778 };
779
780 AVFilter avfilter_vsrc_smptebars = {
781     .name      = "smptebars",
782     .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
783     .priv_size = sizeof(TestSourceContext),
784     .init      = smptebars_init,
785     .uninit    = uninit,
786
787     .query_formats = smptebars_query_formats,
788     .inputs        = NULL,
789     .outputs       = smptebars_outputs,
790     .priv_class    = &smptebars_class,
791 };
792
793 #endif  /* CONFIG_SMPTEBARS_FILTER */