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