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