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