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