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