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