]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_testsrc.c
avutil/opt: check return value of av_bprint_finalize()
[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  * allyuv, smptebars and smptehdbars are by Paul B Mahol.
34  */
35
36 #include <float.h>
37
38 #include "libavutil/avassert.h"
39 #include "libavutil/common.h"
40 #include "libavutil/ffmath.h"
41 #include "libavutil/opt.h"
42 #include "libavutil/imgutils.h"
43 #include "libavutil/intreadwrite.h"
44 #include "libavutil/parseutils.h"
45 #include "libavutil/xga_font_data.h"
46 #include "avfilter.h"
47 #include "drawutils.h"
48 #include "formats.h"
49 #include "internal.h"
50 #include "video.h"
51
52 typedef struct TestSourceContext {
53     const AVClass *class;
54     int w, h;
55     unsigned int nb_frame;
56     AVRational time_base, frame_rate;
57     int64_t pts;
58     int64_t duration;           ///< duration expressed in microseconds
59     AVRational sar;             ///< sample aspect ratio
60     int draw_once;              ///< draw only the first frame, always put out the same picture
61     int draw_once_reset;        ///< draw only the first frame or in case of reset
62     AVFrame *picref;            ///< cached reference containing the painted picture
63
64     void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
65
66     /* only used by testsrc */
67     int nb_decimals;
68
69     /* only used by testsrc2 */
70     int alpha;
71
72     /* only used by color */
73     FFDrawContext draw;
74     FFDrawColor color;
75     uint8_t color_rgba[4];
76
77     /* only used by rgbtest */
78     uint8_t rgba_map[4];
79
80     /* only used by haldclut */
81     int level;
82 } TestSourceContext;
83
84 #define OFFSET(x) offsetof(TestSourceContext, x)
85 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
86 #define FLAGSR AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
87
88 #define SIZE_OPTIONS \
89     { "size",     "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
90     { "s",        "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
91
92 #define COMMON_OPTIONS_NOSIZE \
93     { "rate",     "set video rate",     OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },\
94     { "r",        "set video rate",     OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },\
95     { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
96     { "d",        "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
97     { "sar",      "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1},  0, INT_MAX, FLAGS },
98
99 #define COMMON_OPTIONS SIZE_OPTIONS COMMON_OPTIONS_NOSIZE
100
101 static const AVOption options[] = {
102     COMMON_OPTIONS
103     { NULL }
104 };
105
106 static av_cold int init(AVFilterContext *ctx)
107 {
108     TestSourceContext *test = ctx->priv;
109
110     test->time_base = av_inv_q(test->frame_rate);
111     test->nb_frame = 0;
112     test->pts = 0;
113
114     av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
115            test->w, test->h, test->frame_rate.num, test->frame_rate.den,
116            test->duration < 0 ? -1 : (double)test->duration/1000000,
117            test->sar.num, test->sar.den);
118     return 0;
119 }
120
121 static av_cold void uninit(AVFilterContext *ctx)
122 {
123     TestSourceContext *test = ctx->priv;
124
125     av_frame_free(&test->picref);
126 }
127
128 static int config_props(AVFilterLink *outlink)
129 {
130     TestSourceContext *test = outlink->src->priv;
131
132     outlink->w = test->w;
133     outlink->h = test->h;
134     outlink->sample_aspect_ratio = test->sar;
135     outlink->frame_rate = test->frame_rate;
136     outlink->time_base  = test->time_base;
137
138     return 0;
139 }
140
141 static int request_frame(AVFilterLink *outlink)
142 {
143     TestSourceContext *test = outlink->src->priv;
144     AVFrame *frame;
145
146     if (test->duration >= 0 &&
147         av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
148         return AVERROR_EOF;
149
150     if (test->draw_once) {
151         if (test->draw_once_reset) {
152             av_frame_free(&test->picref);
153             test->draw_once_reset = 0;
154         }
155         if (!test->picref) {
156             test->picref =
157                 ff_get_video_buffer(outlink, test->w, test->h);
158             if (!test->picref)
159                 return AVERROR(ENOMEM);
160             test->fill_picture_fn(outlink->src, test->picref);
161         }
162         frame = av_frame_clone(test->picref);
163     } else
164         frame = ff_get_video_buffer(outlink, test->w, test->h);
165
166     if (!frame)
167         return AVERROR(ENOMEM);
168     frame->pts                 = test->pts;
169     frame->key_frame           = 1;
170     frame->interlaced_frame    = 0;
171     frame->pict_type           = AV_PICTURE_TYPE_I;
172     frame->sample_aspect_ratio = test->sar;
173     if (!test->draw_once)
174         test->fill_picture_fn(outlink->src, frame);
175
176     test->pts++;
177     test->nb_frame++;
178
179     return ff_filter_frame(outlink, frame);
180 }
181
182 #if CONFIG_COLOR_FILTER
183
184 static const AVOption color_options[] = {
185     { "color", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, 0, 0, FLAGSR },
186     { "c",     "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, 0, 0, FLAGSR },
187     COMMON_OPTIONS
188     { NULL }
189 };
190
191 AVFILTER_DEFINE_CLASS(color);
192
193 static void color_fill_picture(AVFilterContext *ctx, AVFrame *picref)
194 {
195     TestSourceContext *test = ctx->priv;
196     ff_fill_rectangle(&test->draw, &test->color,
197                       picref->data, picref->linesize,
198                       0, 0, test->w, test->h);
199 }
200
201 static av_cold int color_init(AVFilterContext *ctx)
202 {
203     TestSourceContext *test = ctx->priv;
204     test->fill_picture_fn = color_fill_picture;
205     test->draw_once = 1;
206     return init(ctx);
207 }
208
209 static int color_query_formats(AVFilterContext *ctx)
210 {
211     return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
212 }
213
214 static int color_config_props(AVFilterLink *inlink)
215 {
216     AVFilterContext *ctx = inlink->src;
217     TestSourceContext *test = ctx->priv;
218     int ret;
219
220     ff_draw_init(&test->draw, inlink->format, 0);
221     ff_draw_color(&test->draw, &test->color, test->color_rgba);
222
223     test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
224     test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
225     if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
226         return AVERROR(EINVAL);
227
228     if ((ret = config_props(inlink)) < 0)
229         return ret;
230
231     return 0;
232 }
233
234 static int color_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
235                                  char *res, int res_len, int flags)
236 {
237     TestSourceContext *test = ctx->priv;
238     int ret;
239
240     ret = ff_filter_process_command(ctx, cmd, args, res, res_len, flags);
241     if (ret < 0)
242         return ret;
243
244     ff_draw_color(&test->draw, &test->color, test->color_rgba);
245     test->draw_once_reset = 1;
246     return 0;
247 }
248
249 static const AVFilterPad color_outputs[] = {
250     {
251         .name          = "default",
252         .type          = AVMEDIA_TYPE_VIDEO,
253         .request_frame = request_frame,
254         .config_props  = color_config_props,
255     },
256     {  NULL }
257 };
258
259 AVFilter ff_vsrc_color = {
260     .name            = "color",
261     .description     = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
262     .priv_class      = &color_class,
263     .priv_size       = sizeof(TestSourceContext),
264     .init            = color_init,
265     .uninit          = uninit,
266     .query_formats   = color_query_formats,
267     .inputs          = NULL,
268     .outputs         = color_outputs,
269     .process_command = color_process_command,
270 };
271
272 #endif /* CONFIG_COLOR_FILTER */
273
274 #if CONFIG_HALDCLUTSRC_FILTER
275
276 static const AVOption haldclutsrc_options[] = {
277     { "level", "set level", OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 6}, 2, 16, FLAGS },
278     COMMON_OPTIONS_NOSIZE
279     { NULL }
280 };
281
282 AVFILTER_DEFINE_CLASS(haldclutsrc);
283
284 static void haldclutsrc_fill_picture(AVFilterContext *ctx, AVFrame *frame)
285 {
286     int i, j, k, x = 0, y = 0, is16bit = 0, step;
287     uint32_t alpha = 0;
288     const TestSourceContext *hc = ctx->priv;
289     int level = hc->level;
290     float scale;
291     const int w = frame->width;
292     const int h = frame->height;
293     const uint8_t *data = frame->data[0];
294     const int linesize  = frame->linesize[0];
295     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
296     uint8_t rgba_map[4];
297
298     av_assert0(w == h && w == level*level*level);
299
300     ff_fill_rgba_map(rgba_map, frame->format);
301
302     switch (frame->format) {
303     case AV_PIX_FMT_RGB48:
304     case AV_PIX_FMT_BGR48:
305     case AV_PIX_FMT_RGBA64:
306     case AV_PIX_FMT_BGRA64:
307         is16bit = 1;
308         alpha = 0xffff;
309         break;
310     case AV_PIX_FMT_RGBA:
311     case AV_PIX_FMT_BGRA:
312     case AV_PIX_FMT_ARGB:
313     case AV_PIX_FMT_ABGR:
314         alpha = 0xff;
315         break;
316     }
317
318     step  = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
319     scale = ((float)(1 << (8*(is16bit+1))) - 1) / (level*level - 1);
320
321 #define LOAD_CLUT(nbits) do {                                                   \
322     uint##nbits##_t *dst = ((uint##nbits##_t *)(data + y*linesize)) + x*step;   \
323     dst[rgba_map[0]] = av_clip_uint##nbits(i * scale);                          \
324     dst[rgba_map[1]] = av_clip_uint##nbits(j * scale);                          \
325     dst[rgba_map[2]] = av_clip_uint##nbits(k * scale);                          \
326     if (step == 4)                                                              \
327         dst[rgba_map[3]] = alpha;                                               \
328 } while (0)
329
330     level *= level;
331     for (k = 0; k < level; k++) {
332         for (j = 0; j < level; j++) {
333             for (i = 0; i < level; i++) {
334                 if (!is16bit)
335                     LOAD_CLUT(8);
336                 else
337                     LOAD_CLUT(16);
338                 if (++x == w) {
339                     x = 0;
340                     y++;
341                 }
342             }
343         }
344     }
345 }
346
347 static av_cold int haldclutsrc_init(AVFilterContext *ctx)
348 {
349     TestSourceContext *hc = ctx->priv;
350     hc->fill_picture_fn = haldclutsrc_fill_picture;
351     hc->draw_once = 1;
352     return init(ctx);
353 }
354
355 static int haldclutsrc_query_formats(AVFilterContext *ctx)
356 {
357     static const enum AVPixelFormat pix_fmts[] = {
358         AV_PIX_FMT_RGB24,  AV_PIX_FMT_BGR24,
359         AV_PIX_FMT_RGBA,   AV_PIX_FMT_BGRA,
360         AV_PIX_FMT_ARGB,   AV_PIX_FMT_ABGR,
361         AV_PIX_FMT_0RGB,   AV_PIX_FMT_0BGR,
362         AV_PIX_FMT_RGB0,   AV_PIX_FMT_BGR0,
363         AV_PIX_FMT_RGB48,  AV_PIX_FMT_BGR48,
364         AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
365         AV_PIX_FMT_NONE,
366     };
367
368     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
369     if (!fmts_list)
370         return AVERROR(ENOMEM);
371     return ff_set_common_formats(ctx, fmts_list);
372 }
373
374 static int haldclutsrc_config_props(AVFilterLink *outlink)
375 {
376     AVFilterContext *ctx = outlink->src;
377     TestSourceContext *hc = ctx->priv;
378
379     hc->w = hc->h = hc->level * hc->level * hc->level;
380     return config_props(outlink);
381 }
382
383 static const AVFilterPad haldclutsrc_outputs[] = {
384     {
385         .name          = "default",
386         .type          = AVMEDIA_TYPE_VIDEO,
387         .request_frame = request_frame,
388         .config_props  = haldclutsrc_config_props,
389     },
390     {  NULL }
391 };
392
393 AVFilter ff_vsrc_haldclutsrc = {
394     .name          = "haldclutsrc",
395     .description   = NULL_IF_CONFIG_SMALL("Provide an identity Hald CLUT."),
396     .priv_class    = &haldclutsrc_class,
397     .priv_size     = sizeof(TestSourceContext),
398     .init          = haldclutsrc_init,
399     .uninit        = uninit,
400     .query_formats = haldclutsrc_query_formats,
401     .inputs        = NULL,
402     .outputs       = haldclutsrc_outputs,
403 };
404 #endif /* CONFIG_HALDCLUTSRC_FILTER */
405
406 #if CONFIG_NULLSRC_FILTER
407
408 #define nullsrc_options options
409 AVFILTER_DEFINE_CLASS(nullsrc);
410
411 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFrame *picref) { }
412
413 static av_cold int nullsrc_init(AVFilterContext *ctx)
414 {
415     TestSourceContext *test = ctx->priv;
416
417     test->fill_picture_fn = nullsrc_fill_picture;
418     return init(ctx);
419 }
420
421 static const AVFilterPad nullsrc_outputs[] = {
422     {
423         .name          = "default",
424         .type          = AVMEDIA_TYPE_VIDEO,
425         .request_frame = request_frame,
426         .config_props  = config_props,
427     },
428     { NULL },
429 };
430
431 AVFilter ff_vsrc_nullsrc = {
432     .name        = "nullsrc",
433     .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
434     .init        = nullsrc_init,
435     .uninit      = uninit,
436     .priv_size   = sizeof(TestSourceContext),
437     .priv_class  = &nullsrc_class,
438     .inputs      = NULL,
439     .outputs     = nullsrc_outputs,
440 };
441
442 #endif /* CONFIG_NULLSRC_FILTER */
443
444 #if CONFIG_TESTSRC_FILTER
445
446 static const AVOption testsrc_options[] = {
447     COMMON_OPTIONS
448     { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0},  0, 17, FLAGS },
449     { "n",        "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0},  0, 17, FLAGS },
450     { NULL }
451 };
452
453 AVFILTER_DEFINE_CLASS(testsrc);
454
455 /**
456  * Fill a rectangle with value val.
457  *
458  * @param val the RGB value to set
459  * @param dst pointer to the destination buffer to fill
460  * @param dst_linesize linesize of destination
461  * @param segment_width width of the segment
462  * @param x horizontal coordinate where to draw the rectangle in the destination buffer
463  * @param y horizontal coordinate where to draw the rectangle in the destination buffer
464  * @param w width  of the rectangle to draw, expressed as a number of segment_width units
465  * @param h height of the rectangle to draw, expressed as a number of segment_width units
466  */
467 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, int segment_width,
468                            int x, int y, int w, int h)
469 {
470     int i;
471     int step = 3;
472
473     dst += segment_width * (step * x + y * dst_linesize);
474     w *= segment_width * step;
475     h *= segment_width;
476     for (i = 0; i < h; i++) {
477         memset(dst, val, w);
478         dst += dst_linesize;
479     }
480 }
481
482 static void draw_digit(int digit, uint8_t *dst, int dst_linesize,
483                        int segment_width)
484 {
485 #define TOP_HBAR        1
486 #define MID_HBAR        2
487 #define BOT_HBAR        4
488 #define LEFT_TOP_VBAR   8
489 #define LEFT_BOT_VBAR  16
490 #define RIGHT_TOP_VBAR 32
491 #define RIGHT_BOT_VBAR 64
492     struct segments {
493         int x, y, w, h;
494     } segments[] = {
495         { 1,  0, 5, 1 }, /* TOP_HBAR */
496         { 1,  6, 5, 1 }, /* MID_HBAR */
497         { 1, 12, 5, 1 }, /* BOT_HBAR */
498         { 0,  1, 1, 5 }, /* LEFT_TOP_VBAR */
499         { 0,  7, 1, 5 }, /* LEFT_BOT_VBAR */
500         { 6,  1, 1, 5 }, /* RIGHT_TOP_VBAR */
501         { 6,  7, 1, 5 }  /* RIGHT_BOT_VBAR */
502     };
503     static const unsigned char masks[10] = {
504         /* 0 */ TOP_HBAR         |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
505         /* 1 */                                                        RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
506         /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR                             |RIGHT_TOP_VBAR,
507         /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR                            |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
508         /* 4 */          MID_HBAR         |LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
509         /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR                             |RIGHT_BOT_VBAR,
510         /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR               |RIGHT_BOT_VBAR,
511         /* 7 */ TOP_HBAR                                              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
512         /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
513         /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
514     };
515     unsigned mask = masks[digit];
516     int i;
517
518     draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
519     for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
520         if (mask & (1<<i))
521             draw_rectangle(255, dst, dst_linesize, segment_width,
522                            segments[i].x, segments[i].y, segments[i].w, segments[i].h);
523 }
524
525 #define GRADIENT_SIZE (6 * 256)
526
527 static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
528 {
529     TestSourceContext *test = ctx->priv;
530     uint8_t *p, *p0;
531     int x, y;
532     int color, color_rest;
533     int icolor;
534     int radius;
535     int quad0, quad;
536     int dquad_x, dquad_y;
537     int grad, dgrad, rgrad, drgrad;
538     int seg_size;
539     int second;
540     int i;
541     uint8_t *data = frame->data[0];
542     int width  = frame->width;
543     int height = frame->height;
544
545     /* draw colored bars and circle */
546     radius = (width + height) / 4;
547     quad0 = width * width / 4 + height * height / 4 - radius * radius;
548     dquad_y = 1 - height;
549     p0 = data;
550     for (y = 0; y < height; y++) {
551         p = p0;
552         color = 0;
553         color_rest = 0;
554         quad = quad0;
555         dquad_x = 1 - width;
556         for (x = 0; x < width; x++) {
557             icolor = color;
558             if (quad < 0)
559                 icolor ^= 7;
560             quad += dquad_x;
561             dquad_x += 2;
562             *(p++) = icolor & 1 ? 255 : 0;
563             *(p++) = icolor & 2 ? 255 : 0;
564             *(p++) = icolor & 4 ? 255 : 0;
565             color_rest += 8;
566             if (color_rest >= width) {
567                 color_rest -= width;
568                 color++;
569             }
570         }
571         quad0 += dquad_y;
572         dquad_y += 2;
573         p0 += frame->linesize[0];
574     }
575
576     /* draw sliding color line */
577     p0 = p = data + frame->linesize[0] * (height * 3/4);
578     grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
579         GRADIENT_SIZE;
580     rgrad = 0;
581     dgrad = GRADIENT_SIZE / width;
582     drgrad = GRADIENT_SIZE % width;
583     for (x = 0; x < width; x++) {
584         *(p++) =
585             grad < 256 || grad >= 5 * 256 ? 255 :
586             grad >= 2 * 256 && grad < 4 * 256 ? 0 :
587             grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
588         *(p++) =
589             grad >= 4 * 256 ? 0 :
590             grad >= 1 * 256 && grad < 3 * 256 ? 255 :
591             grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
592         *(p++) =
593             grad < 2 * 256 ? 0 :
594             grad >= 3 * 256 && grad < 5 * 256 ? 255 :
595             grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
596         grad += dgrad;
597         rgrad += drgrad;
598         if (rgrad >= GRADIENT_SIZE) {
599             grad++;
600             rgrad -= GRADIENT_SIZE;
601         }
602         if (grad >= GRADIENT_SIZE)
603             grad -= GRADIENT_SIZE;
604     }
605     p = p0;
606     for (y = height / 8; y > 0; y--) {
607         memcpy(p+frame->linesize[0], p, 3 * width);
608         p += frame->linesize[0];
609     }
610
611     /* draw digits */
612     seg_size = width / 80;
613     if (seg_size >= 1 && height >= 13 * seg_size) {
614         int64_t p10decimals = 1;
615         double time = av_q2d(test->time_base) * test->nb_frame *
616                       ff_exp10(test->nb_decimals);
617         if (time >= INT_MAX)
618             return;
619
620         for (x = 0; x < test->nb_decimals; x++)
621             p10decimals *= 10;
622
623         second = av_rescale_rnd(test->nb_frame * test->time_base.num, p10decimals, test->time_base.den, AV_ROUND_ZERO);
624         x = width - (width - seg_size * 64) / 2;
625         y = (height - seg_size * 13) / 2;
626         p = data + (x*3 + y * frame->linesize[0]);
627         for (i = 0; i < 8; i++) {
628             p -= 3 * 8 * seg_size;
629             draw_digit(second % 10, p, frame->linesize[0], seg_size);
630             second /= 10;
631             if (second == 0)
632                 break;
633         }
634     }
635 }
636
637 static av_cold int test_init(AVFilterContext *ctx)
638 {
639     TestSourceContext *test = ctx->priv;
640
641     test->fill_picture_fn = test_fill_picture;
642     return init(ctx);
643 }
644
645 static int test_query_formats(AVFilterContext *ctx)
646 {
647     static const enum AVPixelFormat pix_fmts[] = {
648         AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
649     };
650
651     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
652     if (!fmts_list)
653         return AVERROR(ENOMEM);
654     return ff_set_common_formats(ctx, fmts_list);
655 }
656
657 static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
658     {
659         .name          = "default",
660         .type          = AVMEDIA_TYPE_VIDEO,
661         .request_frame = request_frame,
662         .config_props  = config_props,
663     },
664     { NULL }
665 };
666
667 AVFilter ff_vsrc_testsrc = {
668     .name          = "testsrc",
669     .description   = NULL_IF_CONFIG_SMALL("Generate test pattern."),
670     .priv_size     = sizeof(TestSourceContext),
671     .priv_class    = &testsrc_class,
672     .init          = test_init,
673     .uninit        = uninit,
674     .query_formats = test_query_formats,
675     .inputs        = NULL,
676     .outputs       = avfilter_vsrc_testsrc_outputs,
677 };
678
679 #endif /* CONFIG_TESTSRC_FILTER */
680
681 #if CONFIG_TESTSRC2_FILTER
682
683 static const AVOption testsrc2_options[] = {
684     COMMON_OPTIONS
685     { "alpha", "set global alpha (opacity)", OFFSET(alpha), AV_OPT_TYPE_INT, {.i64 = 255}, 0, 255, FLAGS },
686     { NULL }
687 };
688
689 AVFILTER_DEFINE_CLASS(testsrc2);
690
691 static void set_color(TestSourceContext *s, FFDrawColor *color, uint32_t argb)
692 {
693     uint8_t rgba[4] = { (argb >> 16) & 0xFF,
694                         (argb >>  8) & 0xFF,
695                         (argb >>  0) & 0xFF,
696                         (argb >> 24) & 0xFF, };
697     ff_draw_color(&s->draw, color, rgba);
698 }
699
700 static uint32_t color_gradient(unsigned index)
701 {
702     unsigned si = index & 0xFF, sd = 0xFF - si;
703     switch (index >> 8) {
704     case 0: return 0xFF0000 + (si <<  8);
705     case 1: return 0x00FF00 + (sd << 16);
706     case 2: return 0x00FF00 + (si <<  0);
707     case 3: return 0x0000FF + (sd <<  8);
708     case 4: return 0x0000FF + (si << 16);
709     case 5: return 0xFF0000 + (sd <<  0);
710     }
711     av_assert0(0);
712 }
713
714 static void draw_text(TestSourceContext *s, AVFrame *frame, FFDrawColor *color,
715                       int x0, int y0, const uint8_t *text)
716 {
717     int x = x0;
718
719     for (; *text; text++) {
720         if (*text == '\n') {
721             x = x0;
722             y0 += 16;
723             continue;
724         }
725         ff_blend_mask(&s->draw, color, frame->data, frame->linesize,
726                       frame->width, frame->height,
727                       avpriv_vga16_font + *text * 16, 1, 8, 16, 0, 0, x, y0);
728         x += 8;
729     }
730 }
731
732 static void test2_fill_picture(AVFilterContext *ctx, AVFrame *frame)
733 {
734     TestSourceContext *s = ctx->priv;
735     FFDrawColor color;
736     unsigned alpha = (uint32_t)s->alpha << 24;
737
738     /* colored background */
739     {
740         unsigned i, x = 0, x2;
741
742         x = 0;
743         for (i = 1; i < 7; i++) {
744             x2 = av_rescale(i, s->w, 6);
745             x2 = ff_draw_round_to_sub(&s->draw, 0, 0, x2);
746             set_color(s, &color, ((i & 1) ? 0xFF0000 : 0) |
747                                  ((i & 2) ? 0x00FF00 : 0) |
748                                  ((i & 4) ? 0x0000FF : 0) |
749                                  alpha);
750             ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
751                               x, 0, x2 - x, frame->height);
752             x = x2;
753         }
754     }
755
756     /* oblique gradient */
757     /* note: too slow if using blending */
758     if (s->h >= 64) {
759         unsigned x, dx, y0, y, g0, g;
760
761         dx = ff_draw_round_to_sub(&s->draw, 0, +1, 1);
762         y0 = av_rescale_q(s->pts, s->time_base, av_make_q(2, s->h - 16));
763         g0 = av_rescale_q(s->pts, s->time_base, av_make_q(1, 128));
764         for (x = 0; x < s->w; x += dx) {
765             g = (av_rescale(x, 6 * 256, s->w) + g0) % (6 * 256);
766             set_color(s, &color, color_gradient(g) | alpha);
767             y = y0 + av_rescale(x, s->h / 2, s->w);
768             y %= 2 * (s->h - 16);
769             if (y > s->h - 16)
770                 y = 2 * (s->h - 16) - y;
771             y = ff_draw_round_to_sub(&s->draw, 1, 0, y);
772             ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
773                               x, y, dx, 16);
774         }
775     }
776
777     /* top right: draw clock hands */
778     if (s->w >= 64 && s->h >= 64) {
779         int l = (FFMIN(s->w, s->h) - 32) >> 1;
780         int steps = FFMAX(4, l >> 5);
781         int xc = (s->w >> 2) + (s->w >> 1);
782         int yc = (s->h >> 2);
783         int cycle = l << 2;
784         int pos, xh, yh;
785         int c, i;
786
787         for (c = 0; c < 3; c++) {
788             set_color(s, &color, (0xBBBBBB ^ (0xFF << (c << 3))) | alpha);
789             pos = av_rescale_q(s->pts, s->time_base, av_make_q(64 >> (c << 1), cycle)) % cycle;
790             xh = pos < 1 * l ? pos :
791                  pos < 2 * l ? l :
792                  pos < 3 * l ? 3 * l - pos : 0;
793             yh = pos < 1 * l ? 0 :
794                  pos < 2 * l ? pos - l :
795                  pos < 3 * l ? l :
796                                cycle - pos;
797             xh -= l >> 1;
798             yh -= l >> 1;
799             for (i = 1; i <= steps; i++) {
800                 int x = av_rescale(xh, i, steps) + xc;
801                 int y = av_rescale(yh, i, steps) + yc;
802                 x = ff_draw_round_to_sub(&s->draw, 0, -1, x);
803                 y = ff_draw_round_to_sub(&s->draw, 1, -1, y);
804                 ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
805                                   x, y, 8, 8);
806             }
807         }
808     }
809
810     /* bottom left: beating rectangles */
811     if (s->w >= 64 && s->h >= 64) {
812         int l = (FFMIN(s->w, s->h) - 16) >> 2;
813         int cycle = l << 3;
814         int xc = (s->w >> 2);
815         int yc = (s->h >> 2) + (s->h >> 1);
816         int xm1 = ff_draw_round_to_sub(&s->draw, 0, -1, xc - 8);
817         int xm2 = ff_draw_round_to_sub(&s->draw, 0, +1, xc + 8);
818         int ym1 = ff_draw_round_to_sub(&s->draw, 1, -1, yc - 8);
819         int ym2 = ff_draw_round_to_sub(&s->draw, 1, +1, yc + 8);
820         int size, step, x1, x2, y1, y2;
821
822         size = av_rescale_q(s->pts, s->time_base, av_make_q(4, cycle));
823         step = size / l;
824         size %= l;
825         if (step & 1)
826             size = l - size;
827         step = (step >> 1) & 3;
828         set_color(s, &color, 0xFF808080);
829         x1 = ff_draw_round_to_sub(&s->draw, 0, -1, xc - 4 - size);
830         x2 = ff_draw_round_to_sub(&s->draw, 0, +1, xc + 4 + size);
831         y1 = ff_draw_round_to_sub(&s->draw, 1, -1, yc - 4 - size);
832         y2 = ff_draw_round_to_sub(&s->draw, 1, +1, yc + 4 + size);
833         if (step == 0 || step == 2)
834             ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
835                               x1, ym1, x2 - x1, ym2 - ym1);
836         if (step == 1 || step == 2)
837             ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
838                               xm1, y1, xm2 - xm1, y2 - y1);
839         if (step == 3)
840             ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
841                               x1, y1, x2 - x1, y2 - y1);
842     }
843
844     /* bottom right: checker with random noise */
845     {
846         unsigned xmin = av_rescale(5, s->w, 8);
847         unsigned xmax = av_rescale(7, s->w, 8);
848         unsigned ymin = av_rescale(5, s->h, 8);
849         unsigned ymax = av_rescale(7, s->h, 8);
850         unsigned x, y, i, r;
851         uint8_t alpha[256];
852
853         r = s->pts;
854         for (y = ymin; y + 15 < ymax; y += 16) {
855             for (x = xmin; x + 15 < xmax; x += 16) {
856                 if ((x ^ y) & 16)
857                     continue;
858                 for (i = 0; i < 256; i++) {
859                     r = r * 1664525 + 1013904223;
860                     alpha[i] = r >> 24;
861                 }
862                 set_color(s, &color, 0xFF00FF80);
863                 ff_blend_mask(&s->draw, &color, frame->data, frame->linesize,
864                                    frame->width, frame->height,
865                                    alpha, 16, 16, 16, 3, 0, x, y);
866             }
867         }
868     }
869
870     /* bouncing square */
871     if (s->w >= 16 && s->h >= 16) {
872         unsigned w = s->w - 8;
873         unsigned h = s->h - 8;
874         unsigned x = av_rescale_q(s->pts, s->time_base, av_make_q(233, 55 * w)) % (w << 1);
875         unsigned y = av_rescale_q(s->pts, s->time_base, av_make_q(233, 89 * h)) % (h << 1);
876         if (x > w)
877             x = (w << 1) - x;
878         if (y > h)
879             y = (h << 1) - y;
880         x = ff_draw_round_to_sub(&s->draw, 0, -1, x);
881         y = ff_draw_round_to_sub(&s->draw, 1, -1, y);
882         set_color(s, &color, 0xFF8000FF);
883         ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
884                           x, y, 8, 8);
885     }
886
887     /* top right: draw frame time and frame number */
888     {
889         char buf[256];
890         unsigned time;
891
892         time = av_rescale_q(s->pts, s->time_base, av_make_q(1, 1000)) % 86400000;
893         set_color(s, &color, 0xC0000000);
894         ff_blend_rectangle(&s->draw, &color, frame->data, frame->linesize,
895                            frame->width, frame->height,
896                            2, 2, 100, 36);
897         set_color(s, &color, 0xFFFF8000);
898         snprintf(buf, sizeof(buf), "%02d:%02d:%02d.%03d\n%12"PRIi64,
899                  time / 3600000, (time / 60000) % 60, (time / 1000) % 60,
900                  time % 1000, s->pts);
901         draw_text(s, frame, &color, 4, 4, buf);
902     }
903 }
904 static av_cold int test2_init(AVFilterContext *ctx)
905 {
906     TestSourceContext *s = ctx->priv;
907
908     s->fill_picture_fn = test2_fill_picture;
909     return init(ctx);
910 }
911
912 static int test2_query_formats(AVFilterContext *ctx)
913 {
914     return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
915 }
916
917 static int test2_config_props(AVFilterLink *inlink)
918 {
919     AVFilterContext *ctx = inlink->src;
920     TestSourceContext *s = ctx->priv;
921
922     av_assert0(ff_draw_init(&s->draw, inlink->format, 0) >= 0);
923     s->w = ff_draw_round_to_sub(&s->draw, 0, -1, s->w);
924     s->h = ff_draw_round_to_sub(&s->draw, 1, -1, s->h);
925     if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
926         return AVERROR(EINVAL);
927     return config_props(inlink);
928 }
929
930 static const AVFilterPad avfilter_vsrc_testsrc2_outputs[] = {
931     {
932         .name          = "default",
933         .type          = AVMEDIA_TYPE_VIDEO,
934         .request_frame = request_frame,
935         .config_props  = test2_config_props,
936     },
937     { NULL }
938 };
939
940 AVFilter ff_vsrc_testsrc2 = {
941     .name          = "testsrc2",
942     .description   = NULL_IF_CONFIG_SMALL("Generate another test pattern."),
943     .priv_size     = sizeof(TestSourceContext),
944     .priv_class    = &testsrc2_class,
945     .init          = test2_init,
946     .uninit        = uninit,
947     .query_formats = test2_query_formats,
948     .inputs        = NULL,
949     .outputs       = avfilter_vsrc_testsrc2_outputs,
950 };
951
952 #endif /* CONFIG_TESTSRC2_FILTER */
953
954 #if CONFIG_RGBTESTSRC_FILTER
955
956 #define rgbtestsrc_options options
957 AVFILTER_DEFINE_CLASS(rgbtestsrc);
958
959 #define R 0
960 #define G 1
961 #define B 2
962 #define A 3
963
964 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
965                               int x, int y, unsigned r, unsigned g, unsigned b, enum AVPixelFormat fmt,
966                               uint8_t rgba_map[4])
967 {
968     uint32_t v;
969     uint8_t *p;
970
971     switch (fmt) {
972     case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
973     case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
974     case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
975     case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
976     case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
977     case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
978     case AV_PIX_FMT_RGB24:
979     case AV_PIX_FMT_BGR24:
980         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
981         p = dst + 3*x + y*dst_linesize;
982         AV_WL24(p, v);
983         break;
984     case AV_PIX_FMT_RGBA:
985     case AV_PIX_FMT_BGRA:
986     case AV_PIX_FMT_ARGB:
987     case AV_PIX_FMT_ABGR:
988         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255U << (rgba_map[A]*8));
989         p = dst + 4*x + y*dst_linesize;
990         AV_WL32(p, v);
991         break;
992     }
993 }
994
995 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
996 {
997     TestSourceContext *test = ctx->priv;
998     int x, y, w = frame->width, h = frame->height;
999
1000     for (y = 0; y < h; y++) {
1001          for (x = 0; x < w; x++) {
1002              int c = 256*x/w;
1003              int r = 0, g = 0, b = 0;
1004
1005              if      (3*y < h  ) r = c;
1006              else if (3*y < 2*h) g = c;
1007              else                b = c;
1008
1009              rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
1010                                ctx->outputs[0]->format, test->rgba_map);
1011          }
1012      }
1013 }
1014
1015 static av_cold int rgbtest_init(AVFilterContext *ctx)
1016 {
1017     TestSourceContext *test = ctx->priv;
1018
1019     test->draw_once = 1;
1020     test->fill_picture_fn = rgbtest_fill_picture;
1021     return init(ctx);
1022 }
1023
1024 static int rgbtest_query_formats(AVFilterContext *ctx)
1025 {
1026     static const enum AVPixelFormat pix_fmts[] = {
1027         AV_PIX_FMT_RGBA, AV_PIX_FMT_ARGB, AV_PIX_FMT_BGRA, AV_PIX_FMT_ABGR,
1028         AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
1029         AV_PIX_FMT_RGB444, AV_PIX_FMT_BGR444,
1030         AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565,
1031         AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555,
1032         AV_PIX_FMT_NONE
1033     };
1034
1035     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
1036     if (!fmts_list)
1037         return AVERROR(ENOMEM);
1038     return ff_set_common_formats(ctx, fmts_list);
1039 }
1040
1041 static int rgbtest_config_props(AVFilterLink *outlink)
1042 {
1043     TestSourceContext *test = outlink->src->priv;
1044
1045     ff_fill_rgba_map(test->rgba_map, outlink->format);
1046     return config_props(outlink);
1047 }
1048
1049 static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
1050     {
1051         .name          = "default",
1052         .type          = AVMEDIA_TYPE_VIDEO,
1053         .request_frame = request_frame,
1054         .config_props  = rgbtest_config_props,
1055     },
1056     { NULL }
1057 };
1058
1059 AVFilter ff_vsrc_rgbtestsrc = {
1060     .name          = "rgbtestsrc",
1061     .description   = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
1062     .priv_size     = sizeof(TestSourceContext),
1063     .priv_class    = &rgbtestsrc_class,
1064     .init          = rgbtest_init,
1065     .uninit        = uninit,
1066     .query_formats = rgbtest_query_formats,
1067     .inputs        = NULL,
1068     .outputs       = avfilter_vsrc_rgbtestsrc_outputs,
1069 };
1070
1071 #endif /* CONFIG_RGBTESTSRC_FILTER */
1072
1073 #if CONFIG_YUVTESTSRC_FILTER
1074
1075 #define yuvtestsrc_options options
1076 AVFILTER_DEFINE_CLASS(yuvtestsrc);
1077
1078 static void yuvtest_fill_picture8(AVFilterContext *ctx, AVFrame *frame)
1079 {
1080     int x, y, w = frame->width, h = frame->height / 3;
1081     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1082     const int factor = 1 << desc->comp[0].depth;
1083     const int mid = 1 << (desc->comp[0].depth - 1);
1084     uint8_t *ydst = frame->data[0];
1085     uint8_t *udst = frame->data[1];
1086     uint8_t *vdst = frame->data[2];
1087     int ylinesize = frame->linesize[0];
1088     int ulinesize = frame->linesize[1];
1089     int vlinesize = frame->linesize[2];
1090
1091     for (y = 0; y < h; y++) {
1092         for (x = 0; x < w; x++) {
1093             int c = factor * x / w;
1094
1095             ydst[x] = c;
1096             udst[x] = mid;
1097             vdst[x] = mid;
1098         }
1099
1100         ydst += ylinesize;
1101         udst += ulinesize;
1102         vdst += vlinesize;
1103     }
1104
1105     h += h;
1106     for (; y < h; y++) {
1107         for (x = 0; x < w; x++) {
1108             int c = factor * x / w;
1109
1110             ydst[x] = mid;
1111             udst[x] = c;
1112             vdst[x] = mid;
1113         }
1114
1115         ydst += ylinesize;
1116         udst += ulinesize;
1117         vdst += vlinesize;
1118     }
1119
1120     for (; y < frame->height; y++) {
1121         for (x = 0; x < w; x++) {
1122             int c = factor * x / w;
1123
1124             ydst[x] = mid;
1125             udst[x] = mid;
1126             vdst[x] = c;
1127         }
1128
1129         ydst += ylinesize;
1130         udst += ulinesize;
1131         vdst += vlinesize;
1132     }
1133 }
1134
1135 static void yuvtest_fill_picture16(AVFilterContext *ctx, AVFrame *frame)
1136 {
1137     int x, y, w = frame->width, h = frame->height / 3;
1138     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1139     const int factor = 1 << desc->comp[0].depth;
1140     const int mid = 1 << (desc->comp[0].depth - 1);
1141     uint16_t *ydst = (uint16_t *)frame->data[0];
1142     uint16_t *udst = (uint16_t *)frame->data[1];
1143     uint16_t *vdst = (uint16_t *)frame->data[2];
1144     int ylinesize = frame->linesize[0] / 2;
1145     int ulinesize = frame->linesize[1] / 2;
1146     int vlinesize = frame->linesize[2] / 2;
1147
1148     for (y = 0; y < h; y++) {
1149         for (x = 0; x < w; x++) {
1150             int c = factor * x / w;
1151
1152             ydst[x] = c;
1153             udst[x] = mid;
1154             vdst[x] = mid;
1155         }
1156
1157         ydst += ylinesize;
1158         udst += ulinesize;
1159         vdst += vlinesize;
1160     }
1161
1162     h += h;
1163     for (; y < h; y++) {
1164         for (x = 0; x < w; x++) {
1165             int c = factor * x / w;
1166
1167             ydst[x] = mid;
1168             udst[x] = c;
1169             vdst[x] = mid;
1170         }
1171
1172         ydst += ylinesize;
1173         udst += ulinesize;
1174         vdst += vlinesize;
1175     }
1176
1177     for (; y < frame->height; y++) {
1178         for (x = 0; x < w; x++) {
1179             int c = factor * x / w;
1180
1181             ydst[x] = mid;
1182             udst[x] = mid;
1183             vdst[x] = c;
1184         }
1185
1186         ydst += ylinesize;
1187         udst += ulinesize;
1188         vdst += vlinesize;
1189     }
1190 }
1191
1192 static av_cold int yuvtest_init(AVFilterContext *ctx)
1193 {
1194     TestSourceContext *test = ctx->priv;
1195
1196     test->draw_once = 1;
1197     return init(ctx);
1198 }
1199
1200 static int yuvtest_query_formats(AVFilterContext *ctx)
1201 {
1202     static const enum AVPixelFormat pix_fmts[] = {
1203         AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUVJ444P,
1204         AV_PIX_FMT_YUV444P9, AV_PIX_FMT_YUV444P10,
1205         AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV444P14,
1206         AV_PIX_FMT_YUV444P16,
1207         AV_PIX_FMT_NONE
1208     };
1209
1210     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
1211     if (!fmts_list)
1212         return AVERROR(ENOMEM);
1213     return ff_set_common_formats(ctx, fmts_list);
1214 }
1215
1216 static int yuvtest_config_props(AVFilterLink *outlink)
1217 {
1218     TestSourceContext *test = outlink->src->priv;
1219     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(outlink->format);
1220
1221     test->fill_picture_fn = desc->comp[0].depth > 8 ? yuvtest_fill_picture16 : yuvtest_fill_picture8;
1222     return config_props(outlink);
1223 }
1224
1225 static const AVFilterPad avfilter_vsrc_yuvtestsrc_outputs[] = {
1226     {
1227         .name          = "default",
1228         .type          = AVMEDIA_TYPE_VIDEO,
1229         .request_frame = request_frame,
1230         .config_props  = yuvtest_config_props,
1231     },
1232     { NULL }
1233 };
1234
1235 AVFilter ff_vsrc_yuvtestsrc = {
1236     .name          = "yuvtestsrc",
1237     .description   = NULL_IF_CONFIG_SMALL("Generate YUV test pattern."),
1238     .priv_size     = sizeof(TestSourceContext),
1239     .priv_class    = &yuvtestsrc_class,
1240     .init          = yuvtest_init,
1241     .uninit        = uninit,
1242     .query_formats = yuvtest_query_formats,
1243     .inputs        = NULL,
1244     .outputs       = avfilter_vsrc_yuvtestsrc_outputs,
1245 };
1246
1247 #endif /* CONFIG_YUVTESTSRC_FILTER */
1248
1249 #if CONFIG_PAL75BARS_FILTER || CONFIG_PAL100BARS_FILTER || CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER
1250
1251 static const uint8_t rainbow[7][4] = {
1252     { 180, 128, 128, 255 },     /* 75% white */
1253     { 162,  44, 142, 255 },     /* 75% yellow */
1254     { 131, 156,  44, 255 },     /* 75% cyan */
1255     { 112,  72,  58, 255 },     /* 75% green */
1256     {  84, 184, 198, 255 },     /* 75% magenta */
1257     {  65, 100, 212, 255 },     /* 75% red */
1258     {  35, 212, 114, 255 },     /* 75% blue */
1259 };
1260
1261 static const uint8_t rainbow100[7][4] = {
1262     { 235, 128, 128, 255 },     /* 100% white */
1263     { 210,  16, 146, 255 },     /* 100% yellow */
1264     { 170, 166,  16, 255 },     /* 100% cyan */
1265     { 145,  54,  34, 255 },     /* 100% green */
1266     { 106, 202, 222, 255 },     /* 100% magenta */
1267     {  81,  90, 240, 255 },     /* 100% red */
1268     {  41, 240, 110, 255 },     /* 100% blue */
1269 };
1270
1271 static const uint8_t rainbowhd[7][4] = {
1272     { 180, 128, 128, 255 },     /* 75% white */
1273     { 168,  44, 136, 255 },     /* 75% yellow */
1274     { 145, 147,  44, 255 },     /* 75% cyan */
1275     { 133,  63,  52, 255 },     /* 75% green */
1276     {  63, 193, 204, 255 },     /* 75% magenta */
1277     {  51, 109, 212, 255 },     /* 75% red */
1278     {  28, 212, 120, 255 },     /* 75% blue */
1279 };
1280
1281 static const uint8_t wobnair[7][4] = {
1282     {  35, 212, 114, 255 },     /* 75% blue */
1283     {  19, 128, 128, 255 },     /* 7.5% intensity black */
1284     {  84, 184, 198, 255 },     /* 75% magenta */
1285     {  19, 128, 128, 255 },     /* 7.5% intensity black */
1286     { 131, 156,  44, 255 },     /* 75% cyan */
1287     {  19, 128, 128, 255 },     /* 7.5% intensity black */
1288     { 180, 128, 128, 255 },     /* 75% white */
1289 };
1290
1291 static const uint8_t white[4] = { 235, 128, 128, 255 };
1292
1293 /* pluge pulses */
1294 static const uint8_t neg4ire[4] = {  7, 128, 128, 255 };
1295 static const uint8_t pos4ire[4] = { 24, 128, 128, 255 };
1296
1297 /* fudged Q/-I */
1298 static const uint8_t i_pixel[4] = { 57, 156,  97, 255 };
1299 static const uint8_t q_pixel[4] = { 44, 171, 147, 255 };
1300
1301 static const uint8_t gray40[4] = { 104, 128, 128, 255 };
1302 static const uint8_t gray15[4] = {  49, 128, 128, 255 };
1303 static const uint8_t   cyan[4] = { 188, 154,  16, 255 };
1304 static const uint8_t yellow[4] = { 219,  16, 138, 255 };
1305 static const uint8_t   blue[4] = {  32, 240, 118, 255 };
1306 static const uint8_t    red[4] = {  63, 102, 240, 255 };
1307 static const uint8_t black0[4] = {  16, 128, 128, 255 };
1308 static const uint8_t black2[4] = {  20, 128, 128, 255 };
1309 static const uint8_t black4[4] = {  25, 128, 128, 255 };
1310 static const uint8_t   neg2[4] = {  12, 128, 128, 255 };
1311
1312 static void draw_bar(TestSourceContext *test, const uint8_t color[4],
1313                      int x, int y, int w, int h,
1314                      AVFrame *frame)
1315 {
1316     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
1317     uint8_t *p, *p0;
1318     int plane;
1319
1320     x = FFMIN(x, test->w - 1);
1321     y = FFMIN(y, test->h - 1);
1322     w = FFMAX(FFMIN(w, test->w - x), 0);
1323     h = FFMAX(FFMIN(h, test->h - y), 0);
1324
1325     av_assert0(x + w <= test->w);
1326     av_assert0(y + h <= test->h);
1327
1328     for (plane = 0; frame->data[plane]; plane++) {
1329         const int c = color[plane];
1330         const int linesize = frame->linesize[plane];
1331         int i, px, py, pw, ph;
1332
1333         if (plane == 1 || plane == 2) {
1334             px = x >> desc->log2_chroma_w;
1335             pw = AV_CEIL_RSHIFT(w, desc->log2_chroma_w);
1336             py = y >> desc->log2_chroma_h;
1337             ph = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
1338         } else {
1339             px = x;
1340             pw = w;
1341             py = y;
1342             ph = h;
1343         }
1344
1345         p0 = p = frame->data[plane] + py * linesize + px;
1346         memset(p, c, pw);
1347         p += linesize;
1348         for (i = 1; i < ph; i++, p += linesize)
1349             memcpy(p, p0, pw);
1350     }
1351 }
1352
1353 static int smptebars_query_formats(AVFilterContext *ctx)
1354 {
1355     static const enum AVPixelFormat pix_fmts[] = {
1356         AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
1357         AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
1358         AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
1359         AV_PIX_FMT_NONE,
1360     };
1361
1362     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
1363     if (!fmts_list)
1364         return AVERROR(ENOMEM);
1365     return ff_set_common_formats(ctx, fmts_list);
1366 }
1367
1368 static const AVFilterPad smptebars_outputs[] = {
1369     {
1370         .name          = "default",
1371         .type          = AVMEDIA_TYPE_VIDEO,
1372         .request_frame = request_frame,
1373         .config_props  = config_props,
1374     },
1375     { NULL }
1376 };
1377
1378 #if CONFIG_PAL75BARS_FILTER
1379
1380 #define pal75bars_options options
1381 AVFILTER_DEFINE_CLASS(pal75bars);
1382
1383 static void pal75bars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
1384 {
1385     TestSourceContext *test = ctx->priv;
1386     int r_w, i, x = 0;
1387     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
1388
1389     picref->color_range = AVCOL_RANGE_MPEG;
1390     picref->colorspace = AVCOL_SPC_BT470BG;
1391
1392     r_w = FFALIGN((test->w + 7) / 8, 1 << pixdesc->log2_chroma_w);
1393
1394     draw_bar(test, white, x, 0, r_w, test->h, picref);
1395     x += r_w;
1396     for (i = 1; i < 7; i++) {
1397         draw_bar(test, rainbow[i], x, 0, r_w, test->h, picref);
1398         x += r_w;
1399     }
1400     draw_bar(test, black0, x, 0, r_w, test->h, picref);
1401 }
1402
1403 static av_cold int pal75bars_init(AVFilterContext *ctx)
1404 {
1405     TestSourceContext *test = ctx->priv;
1406
1407     test->fill_picture_fn = pal75bars_fill_picture;
1408     test->draw_once = 1;
1409     return init(ctx);
1410 }
1411
1412 AVFilter ff_vsrc_pal75bars = {
1413     .name          = "pal75bars",
1414     .description   = NULL_IF_CONFIG_SMALL("Generate PAL 75% color bars."),
1415     .priv_size     = sizeof(TestSourceContext),
1416     .priv_class    = &pal75bars_class,
1417     .init          = pal75bars_init,
1418     .uninit        = uninit,
1419     .query_formats = smptebars_query_formats,
1420     .inputs        = NULL,
1421     .outputs       = smptebars_outputs,
1422 };
1423
1424 #endif  /* CONFIG_PAL75BARS_FILTER */
1425
1426 #if CONFIG_PAL100BARS_FILTER
1427
1428 #define pal100bars_options options
1429 AVFILTER_DEFINE_CLASS(pal100bars);
1430
1431 static void pal100bars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
1432 {
1433     TestSourceContext *test = ctx->priv;
1434     int r_w, i, x = 0;
1435     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
1436
1437     picref->color_range = AVCOL_RANGE_MPEG;
1438     picref->colorspace = AVCOL_SPC_BT470BG;
1439
1440     r_w = FFALIGN((test->w + 7) / 8, 1 << pixdesc->log2_chroma_w);
1441
1442     for (i = 0; i < 7; i++) {
1443         draw_bar(test, rainbow100[i], x, 0, r_w, test->h, picref);
1444         x += r_w;
1445     }
1446     draw_bar(test, black0, x, 0, r_w, test->h, picref);
1447 }
1448
1449 static av_cold int pal100bars_init(AVFilterContext *ctx)
1450 {
1451     TestSourceContext *test = ctx->priv;
1452
1453     test->fill_picture_fn = pal100bars_fill_picture;
1454     test->draw_once = 1;
1455     return init(ctx);
1456 }
1457
1458 AVFilter ff_vsrc_pal100bars = {
1459     .name          = "pal100bars",
1460     .description   = NULL_IF_CONFIG_SMALL("Generate PAL 100% color bars."),
1461     .priv_size     = sizeof(TestSourceContext),
1462     .priv_class    = &pal100bars_class,
1463     .init          = pal100bars_init,
1464     .uninit        = uninit,
1465     .query_formats = smptebars_query_formats,
1466     .inputs        = NULL,
1467     .outputs       = smptebars_outputs,
1468 };
1469
1470 #endif  /* CONFIG_PAL100BARS_FILTER */
1471
1472 #if CONFIG_SMPTEBARS_FILTER
1473
1474 #define smptebars_options options
1475 AVFILTER_DEFINE_CLASS(smptebars);
1476
1477 static void smptebars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
1478 {
1479     TestSourceContext *test = ctx->priv;
1480     int r_w, r_h, w_h, p_w, p_h, i, tmp, x = 0;
1481     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
1482
1483     picref->colorspace = AVCOL_SPC_BT470BG;
1484
1485     r_w = FFALIGN((test->w + 6) / 7, 1 << pixdesc->log2_chroma_w);
1486     r_h = FFALIGN(test->h * 2 / 3, 1 << pixdesc->log2_chroma_h);
1487     w_h = FFALIGN(test->h * 3 / 4 - r_h,  1 << pixdesc->log2_chroma_h);
1488     p_w = FFALIGN(r_w * 5 / 4, 1 << pixdesc->log2_chroma_w);
1489     p_h = test->h - w_h - r_h;
1490
1491     for (i = 0; i < 7; i++) {
1492         draw_bar(test, rainbow[i], x, 0,   r_w, r_h, picref);
1493         draw_bar(test, wobnair[i], x, r_h, r_w, w_h, picref);
1494         x += r_w;
1495     }
1496     x = 0;
1497     draw_bar(test, i_pixel, x, r_h + w_h, p_w, p_h, picref);
1498     x += p_w;
1499     draw_bar(test, white, x, r_h + w_h, p_w, p_h, picref);
1500     x += p_w;
1501     draw_bar(test, q_pixel, x, r_h + w_h, p_w, p_h, picref);
1502     x += p_w;
1503     tmp = FFALIGN(5 * r_w - x,  1 << pixdesc->log2_chroma_w);
1504     draw_bar(test, black0, x, r_h + w_h, tmp, p_h, picref);
1505     x += tmp;
1506     tmp = FFALIGN(r_w / 3,  1 << pixdesc->log2_chroma_w);
1507     draw_bar(test, neg4ire, x, r_h + w_h, tmp, p_h, picref);
1508     x += tmp;
1509     draw_bar(test, black0, x, r_h + w_h, tmp, p_h, picref);
1510     x += tmp;
1511     draw_bar(test, pos4ire, x, r_h + w_h, tmp, p_h, picref);
1512     x += tmp;
1513     draw_bar(test, black0, x, r_h + w_h, test->w - x, p_h, picref);
1514 }
1515
1516 static av_cold int smptebars_init(AVFilterContext *ctx)
1517 {
1518     TestSourceContext *test = ctx->priv;
1519
1520     test->fill_picture_fn = smptebars_fill_picture;
1521     test->draw_once = 1;
1522     return init(ctx);
1523 }
1524
1525 AVFilter ff_vsrc_smptebars = {
1526     .name          = "smptebars",
1527     .description   = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
1528     .priv_size     = sizeof(TestSourceContext),
1529     .priv_class    = &smptebars_class,
1530     .init          = smptebars_init,
1531     .uninit        = uninit,
1532     .query_formats = smptebars_query_formats,
1533     .inputs        = NULL,
1534     .outputs       = smptebars_outputs,
1535 };
1536
1537 #endif  /* CONFIG_SMPTEBARS_FILTER */
1538
1539 #if CONFIG_SMPTEHDBARS_FILTER
1540
1541 #define smptehdbars_options options
1542 AVFILTER_DEFINE_CLASS(smptehdbars);
1543
1544 static void smptehdbars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
1545 {
1546     TestSourceContext *test = ctx->priv;
1547     int d_w, r_w, r_h, l_w, i, tmp, x = 0, y = 0;
1548     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
1549
1550     picref->colorspace = AVCOL_SPC_BT709;
1551
1552     d_w = FFALIGN(test->w / 8, 1 << pixdesc->log2_chroma_w);
1553     r_h = FFALIGN(test->h * 7 / 12, 1 << pixdesc->log2_chroma_h);
1554     draw_bar(test, gray40, x, 0, d_w, r_h, picref);
1555     x += d_w;
1556
1557     r_w = FFALIGN((((test->w + 3) / 4) * 3) / 7, 1 << pixdesc->log2_chroma_w);
1558     for (i = 0; i < 7; i++) {
1559         draw_bar(test, rainbowhd[i], x, 0, r_w, r_h, picref);
1560         x += r_w;
1561     }
1562     draw_bar(test, gray40, x, 0, test->w - x, r_h, picref);
1563     y = r_h;
1564     r_h = FFALIGN(test->h / 12, 1 << pixdesc->log2_chroma_h);
1565     draw_bar(test, cyan, 0, y, d_w, r_h, picref);
1566     x = d_w;
1567     draw_bar(test, i_pixel, x, y, r_w, r_h, picref);
1568     x += r_w;
1569     tmp = r_w * 6;
1570     draw_bar(test, rainbowhd[0], x, y, tmp, r_h, picref);
1571     x += tmp;
1572     l_w = x;
1573     draw_bar(test, blue, x, y, test->w - x, r_h, picref);
1574     y += r_h;
1575     draw_bar(test, yellow, 0, y, d_w, r_h, picref);
1576     x = d_w;
1577     draw_bar(test, q_pixel, x, y, r_w, r_h, picref);
1578     x += r_w;
1579
1580     for (i = 0; i < tmp; i += 1 << pixdesc->log2_chroma_w) {
1581         uint8_t yramp[4] = {0};
1582
1583         yramp[0] = i * 255 / tmp;
1584         yramp[1] = 128;
1585         yramp[2] = 128;
1586         yramp[3] = 255;
1587
1588         draw_bar(test, yramp, x, y, 1 << pixdesc->log2_chroma_w, r_h, picref);
1589         x += 1 << pixdesc->log2_chroma_w;
1590     }
1591     draw_bar(test, red, x, y, test->w - x, r_h, picref);
1592     y += r_h;
1593     draw_bar(test, gray15, 0, y, d_w, test->h - y, picref);
1594     x = d_w;
1595     tmp = FFALIGN(r_w * 3 / 2, 1 << pixdesc->log2_chroma_w);
1596     draw_bar(test, black0, x, y, tmp, test->h - y, picref);
1597     x += tmp;
1598     tmp = FFALIGN(r_w * 2, 1 << pixdesc->log2_chroma_w);
1599     draw_bar(test, white, x, y, tmp, test->h - y, picref);
1600     x += tmp;
1601     tmp = FFALIGN(r_w * 5 / 6, 1 << pixdesc->log2_chroma_w);
1602     draw_bar(test, black0, x, y, tmp, test->h - y, picref);
1603     x += tmp;
1604     tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
1605     draw_bar(test,   neg2, x, y, tmp, test->h - y, picref);
1606     x += tmp;
1607     draw_bar(test, black0, x, y, tmp, test->h - y, picref);
1608     x += tmp;
1609     draw_bar(test, black2, x, y, tmp, test->h - y, picref);
1610     x += tmp;
1611     draw_bar(test, black0, x, y, tmp, test->h - y, picref);
1612     x += tmp;
1613     draw_bar(test, black4, x, y, tmp, test->h - y, picref);
1614     x += tmp;
1615     r_w = l_w - x;
1616     draw_bar(test, black0, x, y, r_w, test->h - y, picref);
1617     x += r_w;
1618     draw_bar(test, gray15, x, y, test->w - x, test->h - y, picref);
1619 }
1620
1621 static av_cold int smptehdbars_init(AVFilterContext *ctx)
1622 {
1623     TestSourceContext *test = ctx->priv;
1624
1625     test->fill_picture_fn = smptehdbars_fill_picture;
1626     test->draw_once = 1;
1627     return init(ctx);
1628 }
1629
1630 AVFilter ff_vsrc_smptehdbars = {
1631     .name          = "smptehdbars",
1632     .description   = NULL_IF_CONFIG_SMALL("Generate SMPTE HD color bars."),
1633     .priv_size     = sizeof(TestSourceContext),
1634     .priv_class    = &smptehdbars_class,
1635     .init          = smptehdbars_init,
1636     .uninit        = uninit,
1637     .query_formats = smptebars_query_formats,
1638     .inputs        = NULL,
1639     .outputs       = smptebars_outputs,
1640 };
1641
1642 #endif  /* CONFIG_SMPTEHDBARS_FILTER */
1643 #endif  /* CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER */
1644
1645 #if CONFIG_ALLYUV_FILTER
1646
1647 static const AVOption allyuv_options[] = {
1648     COMMON_OPTIONS_NOSIZE
1649     { NULL }
1650 };
1651
1652 AVFILTER_DEFINE_CLASS(allyuv);
1653
1654 static void allyuv_fill_picture(AVFilterContext *ctx, AVFrame *frame)
1655 {
1656     const int ys = frame->linesize[0];
1657     const int us = frame->linesize[1];
1658     const int vs = frame->linesize[2];
1659     int x, y, j;
1660
1661     for (y = 0; y < 4096; y++) {
1662         for (x = 0; x < 2048; x++) {
1663             frame->data[0][y * ys + x] = ((x / 8) % 256);
1664             frame->data[0][y * ys + 4095 - x] = ((x / 8) % 256);
1665         }
1666
1667         for (x = 0; x < 2048; x+=8) {
1668             for (j = 0; j < 8; j++) {
1669                 frame->data[1][vs * y + x + j]        = (y%16 + (j % 8) * 16);
1670                 frame->data[1][vs * y + 4095 - x - j] = (128 + y%16 + (j % 8) * 16);
1671             }
1672         }
1673
1674         for (x = 0; x < 4096; x++)
1675             frame->data[2][y * us + x] = 256 * y / 4096;
1676     }
1677 }
1678
1679 static av_cold int allyuv_init(AVFilterContext *ctx)
1680 {
1681     TestSourceContext *test = ctx->priv;
1682
1683     test->w = test->h = 4096;
1684     test->draw_once = 1;
1685     test->fill_picture_fn = allyuv_fill_picture;
1686     return init(ctx);
1687 }
1688
1689 static int allyuv_query_formats(AVFilterContext *ctx)
1690 {
1691     static const enum AVPixelFormat pix_fmts[] = {
1692         AV_PIX_FMT_YUV444P, AV_PIX_FMT_GBRP,
1693         AV_PIX_FMT_NONE
1694     };
1695
1696     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
1697     if (!fmts_list)
1698         return AVERROR(ENOMEM);
1699     return ff_set_common_formats(ctx, fmts_list);
1700 }
1701
1702 static const AVFilterPad avfilter_vsrc_allyuv_outputs[] = {
1703     {
1704         .name          = "default",
1705         .type          = AVMEDIA_TYPE_VIDEO,
1706         .request_frame = request_frame,
1707         .config_props  = config_props,
1708     },
1709     { NULL }
1710 };
1711
1712 AVFilter ff_vsrc_allyuv = {
1713     .name          = "allyuv",
1714     .description   = NULL_IF_CONFIG_SMALL("Generate all yuv colors."),
1715     .priv_size     = sizeof(TestSourceContext),
1716     .priv_class    = &allyuv_class,
1717     .init          = allyuv_init,
1718     .uninit        = uninit,
1719     .query_formats = allyuv_query_formats,
1720     .inputs        = NULL,
1721     .outputs       = avfilter_vsrc_allyuv_outputs,
1722 };
1723
1724 #endif /* CONFIG_ALLYUV_FILTER */
1725
1726 #if CONFIG_ALLRGB_FILTER
1727
1728 static const AVOption allrgb_options[] = {
1729     COMMON_OPTIONS_NOSIZE
1730     { NULL }
1731 };
1732
1733 AVFILTER_DEFINE_CLASS(allrgb);
1734
1735 static void allrgb_fill_picture(AVFilterContext *ctx, AVFrame *frame)
1736 {
1737     unsigned x, y;
1738     const int linesize = frame->linesize[0];
1739     uint8_t *line = frame->data[0];
1740
1741     for (y = 0; y < 4096; y++) {
1742         uint8_t *dst = line;
1743
1744         for (x = 0; x < 4096; x++) {
1745             *dst++ = x;
1746             *dst++ = y;
1747             *dst++ = (x >> 8) | ((y >> 8) << 4);
1748         }
1749         line += linesize;
1750     }
1751 }
1752
1753 static av_cold int allrgb_init(AVFilterContext *ctx)
1754 {
1755     TestSourceContext *test = ctx->priv;
1756
1757     test->w = test->h = 4096;
1758     test->draw_once = 1;
1759     test->fill_picture_fn = allrgb_fill_picture;
1760     return init(ctx);
1761 }
1762
1763 static int allrgb_config_props(AVFilterLink *outlink)
1764 {
1765     TestSourceContext *test = outlink->src->priv;
1766
1767     ff_fill_rgba_map(test->rgba_map, outlink->format);
1768     return config_props(outlink);
1769 }
1770
1771 static int allrgb_query_formats(AVFilterContext *ctx)
1772 {
1773     static const enum AVPixelFormat pix_fmts[] = {
1774         AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
1775     };
1776
1777     AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
1778     if (!fmts_list)
1779         return AVERROR(ENOMEM);
1780     return ff_set_common_formats(ctx, fmts_list);
1781 }
1782
1783 static const AVFilterPad avfilter_vsrc_allrgb_outputs[] = {
1784     {
1785         .name          = "default",
1786         .type          = AVMEDIA_TYPE_VIDEO,
1787         .request_frame = request_frame,
1788         .config_props  = allrgb_config_props,
1789     },
1790     { NULL }
1791 };
1792
1793 AVFilter ff_vsrc_allrgb = {
1794     .name          = "allrgb",
1795     .description   = NULL_IF_CONFIG_SMALL("Generate all RGB colors."),
1796     .priv_size     = sizeof(TestSourceContext),
1797     .priv_class    = &allrgb_class,
1798     .init          = allrgb_init,
1799     .uninit        = uninit,
1800     .query_formats = allrgb_query_formats,
1801     .inputs        = NULL,
1802     .outputs       = avfilter_vsrc_allrgb_outputs,
1803 };
1804
1805 #endif /* CONFIG_ALLRGB_FILTER */