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