]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_testsrc.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / vsrc_testsrc.c
1 /*
2  * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
3  * Copyright (c) 2011 Stefano Sabatini
4  * Copyright (c) 2012 Paul B Mahol
5  *
6  * This file is part of FFmpeg.
7  *
8  * FFmpeg is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * FFmpeg is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22
23 /**
24  * @file
25  * Misc test sources.
26  *
27  * testsrc is based on the test pattern generator demuxer by Nicolas George:
28  * http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2007-October/037845.html
29  *
30  * rgbtestsrc is ported from MPlayer libmpcodecs/vf_rgbtest.c by
31  * Michael Niedermayer.
32  *
33  * smptebars 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 "avfilter.h"
45 #include "drawutils.h"
46 #include "formats.h"
47 #include "internal.h"
48 #include "video.h"
49
50 typedef struct {
51     const AVClass *class;
52     int w, h;
53     unsigned int nb_frame;
54     AVRational time_base, frame_rate;
55     int64_t pts;
56     int64_t duration;           ///< duration expressed in microseconds
57     AVRational sar;             ///< sample aspect ratio
58     int nb_decimals;
59     int draw_once;              ///< draw only the first frame, always put out the same picture
60     AVFrame *picref;            ///< cached reference containing the painted picture
61
62     void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
63
64     /* only used by color */
65     char *color_str;
66     FFDrawContext draw;
67     FFDrawColor color;
68     uint8_t color_rgba[4];
69
70     /* only used by rgbtest */
71     uint8_t rgba_map[4];
72 } TestSourceContext;
73
74 #define OFFSET(x) offsetof(TestSourceContext, x)
75 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
76
77 #define COMMON_OPTIONS \
78     { "size",     "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
79     { "s",        "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
80     { "rate",     "set video rate",     OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
81     { "r",        "set video rate",     OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
82     { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
83     { "d",        "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
84     { "sar",      "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1},  0, INT_MAX, FLAGS },
85
86
87 static const AVOption color_options[] = {
88     /* only used by color */
89     { "color", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
90     { "c",     "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
91
92     COMMON_OPTIONS
93     { NULL },
94 };
95
96 static const AVOption options[] = {
97     COMMON_OPTIONS
98     /* only used by testsrc */
99     { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0},  0, 17, FLAGS },
100     { "n",        "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0},  0, 17, FLAGS },
101
102     { NULL },
103 };
104
105 static av_cold int init(AVFilterContext *ctx)
106 {
107     TestSourceContext *test = ctx->priv;
108     int ret = 0;
109
110     if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
111         av_log(ctx, AV_LOG_WARNING,
112                "Option 'decimals' is ignored with source '%s'\n",
113                ctx->filter->name);
114     }
115
116     if (test->color_str) {
117         if (!strcmp(ctx->filter->name, "color")) {
118             ret = av_parse_color(test->color_rgba, test->color_str, -1, ctx);
119             if (ret < 0)
120                 return ret;
121         } else {
122             av_log(ctx, AV_LOG_WARNING,
123                    "Option 'color' is ignored with source '%s'\n",
124                    ctx->filter->name);
125         }
126     }
127
128     test->time_base = av_inv_q(test->frame_rate);
129     test->nb_frame = 0;
130     test->pts = 0;
131
132     av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
133            test->w, test->h, test->frame_rate.num, test->frame_rate.den,
134            test->duration < 0 ? -1 : (double)test->duration/1000000,
135            test->sar.num, test->sar.den);
136     return 0;
137 }
138
139 static av_cold void uninit(AVFilterContext *ctx)
140 {
141     TestSourceContext *test = ctx->priv;
142
143     av_frame_free(&test->picref);
144 }
145
146 static int config_props(AVFilterLink *outlink)
147 {
148     TestSourceContext *test = outlink->src->priv;
149
150     outlink->w = test->w;
151     outlink->h = test->h;
152     outlink->sample_aspect_ratio = test->sar;
153     outlink->frame_rate = test->frame_rate;
154     outlink->time_base  = test->time_base;
155
156     return 0;
157 }
158
159 static int request_frame(AVFilterLink *outlink)
160 {
161     TestSourceContext *test = outlink->src->priv;
162     AVFrame *frame;
163
164     if (test->duration >= 0 &&
165         av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
166         return AVERROR_EOF;
167
168     if (test->draw_once) {
169         if (!test->picref) {
170             test->picref =
171                 ff_get_video_buffer(outlink, test->w, test->h);
172             if (!test->picref)
173                 return AVERROR(ENOMEM);
174             test->fill_picture_fn(outlink->src, test->picref);
175         }
176         frame = av_frame_clone(test->picref);
177     } else
178         frame = ff_get_video_buffer(outlink, test->w, test->h);
179
180     if (!frame)
181         return AVERROR(ENOMEM);
182     frame->pts                 = test->pts;
183     frame->key_frame           = 1;
184     frame->interlaced_frame    = 0;
185     frame->pict_type           = AV_PICTURE_TYPE_I;
186     frame->sample_aspect_ratio = test->sar;
187     if (!test->draw_once)
188         test->fill_picture_fn(outlink->src, frame);
189
190     test->pts++;
191     test->nb_frame++;
192
193     return ff_filter_frame(outlink, frame);
194 }
195
196 #if CONFIG_COLOR_FILTER
197
198 AVFILTER_DEFINE_CLASS(color);
199
200 static void color_fill_picture(AVFilterContext *ctx, AVFrame *picref)
201 {
202     TestSourceContext *test = ctx->priv;
203     ff_fill_rectangle(&test->draw, &test->color,
204                       picref->data, picref->linesize,
205                       0, 0, test->w, test->h);
206 }
207
208 static av_cold int color_init(AVFilterContext *ctx)
209 {
210     TestSourceContext *test = ctx->priv;
211     test->fill_picture_fn = color_fill_picture;
212     test->draw_once = 1;
213     return init(ctx);
214 }
215
216 static int color_query_formats(AVFilterContext *ctx)
217 {
218     ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
219     return 0;
220 }
221
222 static int color_config_props(AVFilterLink *inlink)
223 {
224     AVFilterContext *ctx = inlink->src;
225     TestSourceContext *test = ctx->priv;
226     int ret;
227
228     ff_draw_init(&test->draw, inlink->format, 0);
229     ff_draw_color(&test->draw, &test->color, test->color_rgba);
230
231     test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
232     test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
233     if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
234         return AVERROR(EINVAL);
235
236     if ((ret = config_props(inlink)) < 0)
237         return ret;
238
239     av_log(ctx, AV_LOG_VERBOSE, "color:0x%02x%02x%02x%02x\n",
240            test->color_rgba[0], test->color_rgba[1], test->color_rgba[2], test->color_rgba[3]);
241     return 0;
242 }
243
244 static const AVFilterPad color_outputs[] = {
245     {
246         .name          = "default",
247         .type          = AVMEDIA_TYPE_VIDEO,
248         .request_frame = request_frame,
249         .config_props  = color_config_props,
250     },
251     {  NULL }
252 };
253
254 AVFilter avfilter_vsrc_color = {
255     .name        = "color",
256     .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
257
258     .priv_class = &color_class,
259     .priv_size = sizeof(TestSourceContext),
260     .init      = color_init,
261     .uninit    = uninit,
262
263     .query_formats = color_query_formats,
264     .inputs        = NULL,
265     .outputs       = color_outputs,
266 };
267
268 #endif /* CONFIG_COLOR_FILTER */
269
270 #if CONFIG_NULLSRC_FILTER
271
272 #define nullsrc_options options
273 AVFILTER_DEFINE_CLASS(nullsrc);
274
275 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFrame *picref) { }
276
277 static av_cold int nullsrc_init(AVFilterContext *ctx)
278 {
279     TestSourceContext *test = ctx->priv;
280
281     test->fill_picture_fn = nullsrc_fill_picture;
282     return init(ctx);
283 }
284
285 static const AVFilterPad nullsrc_outputs[] = {
286     {
287         .name          = "default",
288         .type          = AVMEDIA_TYPE_VIDEO,
289         .request_frame = request_frame,
290         .config_props  = config_props,
291     },
292     { NULL },
293 };
294
295 AVFilter avfilter_vsrc_nullsrc = {
296     .name        = "nullsrc",
297     .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
298     .init       = nullsrc_init,
299     .uninit     = uninit,
300     .priv_size  = sizeof(TestSourceContext),
301     .priv_class = &nullsrc_class,
302     .inputs     = NULL,
303     .outputs    = nullsrc_outputs,
304 };
305
306 #endif /* CONFIG_NULLSRC_FILTER */
307
308 #if CONFIG_TESTSRC_FILTER
309
310 #define testsrc_options options
311 AVFILTER_DEFINE_CLASS(testsrc);
312
313 /**
314  * Fill a rectangle with value val.
315  *
316  * @param val the RGB value to set
317  * @param dst pointer to the destination buffer to fill
318  * @param dst_linesize linesize of destination
319  * @param segment_width width of the segment
320  * @param x horizontal coordinate where to draw the rectangle in the destination buffer
321  * @param y horizontal coordinate where to draw the rectangle in the destination buffer
322  * @param w width  of the rectangle to draw, expressed as a number of segment_width units
323  * @param h height of the rectangle to draw, expressed as a number of segment_width units
324  */
325 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, int segment_width,
326                            int x, int y, int w, int h)
327 {
328     int i;
329     int step = 3;
330
331     dst += segment_width * (step * x + y * dst_linesize);
332     w *= segment_width * step;
333     h *= segment_width;
334     for (i = 0; i < h; i++) {
335         memset(dst, val, w);
336         dst += dst_linesize;
337     }
338 }
339
340 static void draw_digit(int digit, uint8_t *dst, int dst_linesize,
341                        int segment_width)
342 {
343 #define TOP_HBAR        1
344 #define MID_HBAR        2
345 #define BOT_HBAR        4
346 #define LEFT_TOP_VBAR   8
347 #define LEFT_BOT_VBAR  16
348 #define RIGHT_TOP_VBAR 32
349 #define RIGHT_BOT_VBAR 64
350     struct {
351         int x, y, w, h;
352     } segments[] = {
353         { 1,  0, 5, 1 }, /* TOP_HBAR */
354         { 1,  6, 5, 1 }, /* MID_HBAR */
355         { 1, 12, 5, 1 }, /* BOT_HBAR */
356         { 0,  1, 1, 5 }, /* LEFT_TOP_VBAR */
357         { 0,  7, 1, 5 }, /* LEFT_BOT_VBAR */
358         { 6,  1, 1, 5 }, /* RIGHT_TOP_VBAR */
359         { 6,  7, 1, 5 }  /* RIGHT_BOT_VBAR */
360     };
361     static const unsigned char masks[10] = {
362         /* 0 */ TOP_HBAR         |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
363         /* 1 */                                                        RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
364         /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR                             |RIGHT_TOP_VBAR,
365         /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR                            |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
366         /* 4 */          MID_HBAR         |LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
367         /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR                             |RIGHT_BOT_VBAR,
368         /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR               |RIGHT_BOT_VBAR,
369         /* 7 */ TOP_HBAR                                              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
370         /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
371         /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
372     };
373     unsigned mask = masks[digit];
374     int i;
375
376     draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
377     for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
378         if (mask & (1<<i))
379             draw_rectangle(255, dst, dst_linesize, segment_width,
380                            segments[i].x, segments[i].y, segments[i].w, segments[i].h);
381 }
382
383 #define GRADIENT_SIZE (6 * 256)
384
385 static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
386 {
387     TestSourceContext *test = ctx->priv;
388     uint8_t *p, *p0;
389     int x, y;
390     int color, color_rest;
391     int icolor;
392     int radius;
393     int quad0, quad;
394     int dquad_x, dquad_y;
395     int grad, dgrad, rgrad, drgrad;
396     int seg_size;
397     int second;
398     int i;
399     uint8_t *data = frame->data[0];
400     int width  = frame->width;
401     int height = frame->height;
402
403     /* draw colored bars and circle */
404     radius = (width + height) / 4;
405     quad0 = width * width / 4 + height * height / 4 - radius * radius;
406     dquad_y = 1 - height;
407     p0 = data;
408     for (y = 0; y < height; y++) {
409         p = p0;
410         color = 0;
411         color_rest = 0;
412         quad = quad0;
413         dquad_x = 1 - width;
414         for (x = 0; x < width; x++) {
415             icolor = color;
416             if (quad < 0)
417                 icolor ^= 7;
418             quad += dquad_x;
419             dquad_x += 2;
420             *(p++) = icolor & 1 ? 255 : 0;
421             *(p++) = icolor & 2 ? 255 : 0;
422             *(p++) = icolor & 4 ? 255 : 0;
423             color_rest += 8;
424             if (color_rest >= width) {
425                 color_rest -= width;
426                 color++;
427             }
428         }
429         quad0 += dquad_y;
430         dquad_y += 2;
431         p0 += frame->linesize[0];
432     }
433
434     /* draw sliding color line */
435     p0 = p = data + frame->linesize[0] * height * 3/4;
436     grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
437         GRADIENT_SIZE;
438     rgrad = 0;
439     dgrad = GRADIENT_SIZE / width;
440     drgrad = GRADIENT_SIZE % width;
441     for (x = 0; x < width; x++) {
442         *(p++) =
443             grad < 256 || grad >= 5 * 256 ? 255 :
444             grad >= 2 * 256 && grad < 4 * 256 ? 0 :
445             grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
446         *(p++) =
447             grad >= 4 * 256 ? 0 :
448             grad >= 1 * 256 && grad < 3 * 256 ? 255 :
449             grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
450         *(p++) =
451             grad < 2 * 256 ? 0 :
452             grad >= 3 * 256 && grad < 5 * 256 ? 255 :
453             grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
454         grad += dgrad;
455         rgrad += drgrad;
456         if (rgrad >= GRADIENT_SIZE) {
457             grad++;
458             rgrad -= GRADIENT_SIZE;
459         }
460         if (grad >= GRADIENT_SIZE)
461             grad -= GRADIENT_SIZE;
462     }
463     p = p0;
464     for (y = height / 8; y > 0; y--) {
465         memcpy(p+frame->linesize[0], p, 3 * width);
466         p += frame->linesize[0];
467     }
468
469     /* draw digits */
470     seg_size = width / 80;
471     if (seg_size >= 1 && height >= 13 * seg_size) {
472         int64_t p10decimals = 1;
473         double time = av_q2d(test->time_base) * test->nb_frame *
474                       pow(10, test->nb_decimals);
475         if (time >= INT_MAX)
476             return;
477
478         for (x = 0; x < test->nb_decimals; x++)
479             p10decimals *= 10;
480
481         second = av_rescale_rnd(test->nb_frame * test->time_base.num, p10decimals, test->time_base.den, AV_ROUND_ZERO);
482         x = width - (width - seg_size * 64) / 2;
483         y = (height - seg_size * 13) / 2;
484         p = data + (x*3 + y * frame->linesize[0]);
485         for (i = 0; i < 8; i++) {
486             p -= 3 * 8 * seg_size;
487             draw_digit(second % 10, p, frame->linesize[0], seg_size);
488             second /= 10;
489             if (second == 0)
490                 break;
491         }
492     }
493 }
494
495 static av_cold int test_init(AVFilterContext *ctx)
496 {
497     TestSourceContext *test = ctx->priv;
498
499     test->fill_picture_fn = test_fill_picture;
500     return init(ctx);
501 }
502
503 static int test_query_formats(AVFilterContext *ctx)
504 {
505     static const enum AVPixelFormat pix_fmts[] = {
506         AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
507     };
508     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
509     return 0;
510 }
511
512 static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
513     {
514         .name          = "default",
515         .type          = AVMEDIA_TYPE_VIDEO,
516         .request_frame = request_frame,
517         .config_props  = config_props,
518     },
519     { NULL }
520 };
521
522 AVFilter avfilter_vsrc_testsrc = {
523     .name          = "testsrc",
524     .description   = NULL_IF_CONFIG_SMALL("Generate test pattern."),
525     .priv_size     = sizeof(TestSourceContext),
526     .priv_class    = &testsrc_class,
527     .init          = test_init,
528     .uninit        = uninit,
529
530     .query_formats = test_query_formats,
531
532     .inputs    = NULL,
533     .outputs   = avfilter_vsrc_testsrc_outputs,
534 };
535
536 #endif /* CONFIG_TESTSRC_FILTER */
537
538 #if CONFIG_RGBTESTSRC_FILTER
539
540 #define rgbtestsrc_options options
541 AVFILTER_DEFINE_CLASS(rgbtestsrc);
542
543 #define R 0
544 #define G 1
545 #define B 2
546 #define A 3
547
548 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
549                               int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
550                               uint8_t rgba_map[4])
551 {
552     int32_t v;
553     uint8_t *p;
554
555     switch (fmt) {
556     case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
557     case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
558     case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
559     case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
560     case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
561     case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
562     case AV_PIX_FMT_RGB24:
563     case AV_PIX_FMT_BGR24:
564         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
565         p = dst + 3*x + y*dst_linesize;
566         AV_WL24(p, v);
567         break;
568     case AV_PIX_FMT_RGBA:
569     case AV_PIX_FMT_BGRA:
570     case AV_PIX_FMT_ARGB:
571     case AV_PIX_FMT_ABGR:
572         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
573         p = dst + 4*x + y*dst_linesize;
574         AV_WL32(p, v);
575         break;
576     }
577 }
578
579 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
580 {
581     TestSourceContext *test = ctx->priv;
582     int x, y, w = frame->width, h = frame->height;
583
584     for (y = 0; y < h; y++) {
585          for (x = 0; x < w; x++) {
586              int c = 256*x/w;
587              int r = 0, g = 0, b = 0;
588
589              if      (3*y < h  ) r = c;
590              else if (3*y < 2*h) g = c;
591              else                b = c;
592
593              rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
594                                ctx->outputs[0]->format, test->rgba_map);
595          }
596      }
597 }
598
599 static av_cold int rgbtest_init(AVFilterContext *ctx)
600 {
601     TestSourceContext *test = ctx->priv;
602
603     test->draw_once = 1;
604     test->fill_picture_fn = rgbtest_fill_picture;
605     return init(ctx);
606 }
607
608 static int rgbtest_query_formats(AVFilterContext *ctx)
609 {
610     static const enum AVPixelFormat pix_fmts[] = {
611         AV_PIX_FMT_RGBA, AV_PIX_FMT_ARGB, AV_PIX_FMT_BGRA, AV_PIX_FMT_ABGR,
612         AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
613         AV_PIX_FMT_RGB444, AV_PIX_FMT_BGR444,
614         AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565,
615         AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555,
616         AV_PIX_FMT_NONE
617     };
618     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
619     return 0;
620 }
621
622 static int rgbtest_config_props(AVFilterLink *outlink)
623 {
624     TestSourceContext *test = outlink->src->priv;
625
626     ff_fill_rgba_map(test->rgba_map, outlink->format);
627     return config_props(outlink);
628 }
629
630 static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
631     {
632         .name          = "default",
633         .type          = AVMEDIA_TYPE_VIDEO,
634         .request_frame = request_frame,
635         .config_props  = rgbtest_config_props,
636     },
637     { NULL }
638 };
639
640 AVFilter avfilter_vsrc_rgbtestsrc = {
641     .name          = "rgbtestsrc",
642     .description   = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
643     .priv_size     = sizeof(TestSourceContext),
644     .priv_class    = &rgbtestsrc_class,
645     .init          = rgbtest_init,
646     .uninit        = uninit,
647
648     .query_formats = rgbtest_query_formats,
649
650     .inputs    = NULL,
651
652     .outputs   = avfilter_vsrc_rgbtestsrc_outputs,
653 };
654
655 #endif /* CONFIG_RGBTESTSRC_FILTER */
656
657 #if CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER
658
659 static const uint8_t rainbow[7][4] = {
660     { 191, 191, 191, 255 },     /* gray */
661     { 191, 191,   0, 255 },     /* yellow */
662     {   0, 191, 191, 255 },     /* cyan */
663     {   0, 191,   0, 255 },     /* green */
664     { 191,   0, 191, 255 },     /* magenta */
665     { 191,   0,   0, 255 },     /* red */
666     {   0,   0, 191, 255 },     /* blue */
667 };
668
669 static const uint8_t wobnair[7][4] = {
670     {   0,   0, 191, 255 },     /* blue */
671     {  19,  19,  19, 255 },     /* 7.5% intensity black */
672     { 191,   0, 191, 255 },     /* magenta */
673     {  19,  19,  19, 255 },     /* 7.5% intensity black */
674     {   0, 191, 191, 255 },     /* cyan */
675     {  19,  19,  19, 255 },     /* 7.5% intensity black */
676     { 191, 191, 191, 255 },     /* gray */
677 };
678
679 static const uint8_t white[4] = { 255, 255, 255, 255 };
680 static const uint8_t black[4] = {  19,  19,  19, 255 }; /* 7.5% intensity black */
681
682 /* pluge pulses */
683 static const uint8_t neg4ire[4] = {   9,   9,   9, 255 }; /*  3.5% intensity black */
684 static const uint8_t pos4ire[4] = {  29,  29,  29, 255 }; /* 11.5% intensity black */
685
686 /* fudged Q/-I */
687 static const uint8_t i_pixel[4] = {   0,  68, 130, 255 };
688 static const uint8_t q_pixel[4] = {  67,   0, 130, 255 };
689
690 static const uint8_t gray40[4] = { 102, 102, 102, 255 };
691 static const uint8_t gray15[4] = {  38,  38,  38, 255 };
692 static const uint8_t   cyan[4] = {   0, 255, 255, 255 };
693 static const uint8_t yellow[4] = { 255, 255,   0, 255 };
694 static const uint8_t   blue[4] = {   0,   0, 255, 255 };
695 static const uint8_t    red[4] = { 255,   0,   0, 255 };
696 static const uint8_t black0[4] = {   5,   5,   5, 255 };
697 static const uint8_t black2[4] = {  10,  10,  10, 255 };
698 static const uint8_t black4[4] = {  15,  15,  15, 255 };
699 static const uint8_t   neg2[4] = {   0,   0,   0, 255 };
700
701 static void inline draw_bar(TestSourceContext *test, const uint8_t *color,
702                             unsigned x, unsigned y, unsigned w, unsigned h,
703                             AVFrame *frame)
704 {
705     FFDrawColor draw_color;
706
707     x = FFMIN(x, test->w - 1);
708     y = FFMIN(y, test->h - 1);
709     w = FFMIN(w, test->w - x);
710     h = FFMIN(h, test->h - y);
711
712     av_assert0(x + w <= test->w);
713     av_assert0(y + h <= test->h);
714
715     ff_draw_color(&test->draw, &draw_color, color);
716     ff_fill_rectangle(&test->draw, &draw_color,
717                       frame->data, frame->linesize, x, y, w, h);
718 }
719
720 static int smptebars_query_formats(AVFilterContext *ctx)
721 {
722     ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
723     return 0;
724 }
725
726 static int smptebars_config_props(AVFilterLink *outlink)
727 {
728     AVFilterContext *ctx = outlink->src;
729     TestSourceContext *test = ctx->priv;
730
731     ff_draw_init(&test->draw, outlink->format, 0);
732
733     return config_props(outlink);
734 }
735
736 static const AVFilterPad smptebars_outputs[] = {
737     {
738         .name          = "default",
739         .type          = AVMEDIA_TYPE_VIDEO,
740         .request_frame = request_frame,
741         .config_props  = smptebars_config_props,
742     },
743     { NULL }
744 };
745
746 #if CONFIG_SMPTEBARS_FILTER
747
748 #define smptebars_options options
749 AVFILTER_DEFINE_CLASS(smptebars);
750
751 static void smptebars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
752 {
753     TestSourceContext *test = ctx->priv;
754     int r_w, r_h, w_h, p_w, p_h, i, tmp, x = 0;
755     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
756
757     r_w = FFALIGN((test->w + 6) / 7, 1 << pixdesc->log2_chroma_w);
758     r_h = FFALIGN(test->h * 2 / 3, 1 << pixdesc->log2_chroma_h);
759     w_h = FFALIGN(test->h * 3 / 4 - r_h,  1 << pixdesc->log2_chroma_h);
760     p_w = FFALIGN(r_w * 5 / 4, 1 << pixdesc->log2_chroma_w);
761     p_h = test->h - w_h - r_h;
762
763     for (i = 0; i < 7; i++) {
764         draw_bar(test, rainbow[i], x, 0,   r_w, r_h, picref);
765         draw_bar(test, wobnair[i], x, r_h, r_w, w_h, picref);
766         x += r_w;
767     }
768     x = 0;
769     draw_bar(test, i_pixel, x, r_h + w_h, p_w, p_h, picref);
770     x += p_w;
771     draw_bar(test, white, x, r_h + w_h, p_w, p_h, picref);
772     x += p_w;
773     draw_bar(test, q_pixel, x, r_h + w_h, p_w, p_h, picref);
774     x += p_w;
775     tmp = FFALIGN(5 * r_w - x,  1 << pixdesc->log2_chroma_w);
776     draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
777     x += tmp;
778     tmp = FFALIGN(r_w / 3,  1 << pixdesc->log2_chroma_w);
779     draw_bar(test, neg4ire, x, r_h + w_h, tmp, p_h, picref);
780     x += tmp;
781     draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
782     x += tmp;
783     draw_bar(test, pos4ire, x, r_h + w_h, tmp, p_h, picref);
784     x += tmp;
785     draw_bar(test, black, x, r_h + w_h, test->w - x, p_h, picref);
786 }
787
788 static av_cold int smptebars_init(AVFilterContext *ctx)
789 {
790     TestSourceContext *test = ctx->priv;
791
792     test->fill_picture_fn = smptebars_fill_picture;
793     test->draw_once = 1;
794     return init(ctx);
795 }
796
797 AVFilter avfilter_vsrc_smptebars = {
798     .name      = "smptebars",
799     .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
800     .priv_size = sizeof(TestSourceContext),
801     .init      = smptebars_init,
802     .uninit    = uninit,
803
804     .query_formats = smptebars_query_formats,
805     .inputs        = NULL,
806     .outputs       = smptebars_outputs,
807     .priv_class    = &smptebars_class,
808 };
809
810 #endif  /* CONFIG_SMPTEBARS_FILTER */
811
812 #if CONFIG_SMPTEHDBARS_FILTER
813
814 #define smptehdbars_options options
815 AVFILTER_DEFINE_CLASS(smptehdbars);
816
817 static void smptehdbars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
818 {
819     TestSourceContext *test = ctx->priv;
820     int d_w, r_w, r_h, l_w, i, tmp, x = 0, y = 0;
821     const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
822
823     d_w = FFALIGN(test->w / 8, 1 << pixdesc->log2_chroma_w);
824     r_h = FFALIGN(test->h * 7 / 12, 1 << pixdesc->log2_chroma_h);
825     draw_bar(test, gray40, x, 0, d_w, r_h, picref);
826     x += d_w;
827
828     r_w = FFALIGN((((test->w + 3) / 4) * 3) / 7, 1 << pixdesc->log2_chroma_w);
829     for (i = 0; i < 7; i++) {
830         draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
831         x += r_w;
832     }
833     draw_bar(test, gray40, x, 0, test->w - x, r_h, picref);
834     y = r_h;
835     r_h = FFALIGN(test->h / 12, 1 << pixdesc->log2_chroma_h);
836     draw_bar(test, cyan, 0, y, d_w, r_h, picref);
837     x = d_w;
838     draw_bar(test, i_pixel, x, y, r_w, r_h, picref);
839     x += r_w;
840     tmp = r_w * 6;
841     draw_bar(test, rainbow[0], x, y, tmp, r_h, picref);
842     x += tmp;
843     l_w = x;
844     draw_bar(test, blue, x, y, test->w - x, r_h, picref);
845     y += r_h;
846     draw_bar(test, yellow, 0, y, d_w, r_h, picref);
847     x = d_w;
848     draw_bar(test, q_pixel, x, y, r_w, r_h, picref);
849     x += r_w;
850
851     for (i = 0; i < tmp; i += 1 << pixdesc->log2_chroma_w) {
852         uint8_t yramp[4] = {0};
853
854         yramp[0] =
855         yramp[1] =
856         yramp[2] = i * 255 / tmp;
857         yramp[3] = 255;
858
859         draw_bar(test, yramp, x, y, 1 << pixdesc->log2_chroma_w, r_h, picref);
860         x += 1 << pixdesc->log2_chroma_w;
861     }
862     draw_bar(test, red, x, y, test->w - x, r_h, picref);
863     y += r_h;
864     draw_bar(test, gray15, 0, y, d_w, test->h - y, picref);
865     x = d_w;
866     tmp = FFALIGN(r_w * 3 / 2, 1 << pixdesc->log2_chroma_w);
867     draw_bar(test, black0, x, y, tmp, test->h - y, picref);
868     x += tmp;
869     tmp = FFALIGN(r_w * 2, 1 << pixdesc->log2_chroma_w);
870     draw_bar(test, white, x, y, tmp, test->h - y, picref);
871     x += tmp;
872     tmp = FFALIGN(r_w * 5 / 6, 1 << pixdesc->log2_chroma_w);
873     draw_bar(test, black0, x, y, tmp, test->h - y, picref);
874     x += tmp;
875     tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
876     draw_bar(test,   neg2, x, y, tmp, test->h - y, picref);
877     x += tmp;
878     draw_bar(test, black0, x, y, tmp, test->h - y, picref);
879     x += tmp;
880     draw_bar(test, black2, x, y, tmp, test->h - y, picref);
881     x += tmp;
882     draw_bar(test, black0, x, y, tmp, test->h - y, picref);
883     x += tmp;
884     draw_bar(test, black4, x, y, tmp, test->h - y, picref);
885     x += tmp;
886     r_w = l_w - x;
887     draw_bar(test, black0, x, y, r_w, test->h - y, picref);
888     x += r_w;
889     draw_bar(test, gray15, x, y, test->w - x, test->h - y, picref);
890 }
891
892 static av_cold int smptehdbars_init(AVFilterContext *ctx)
893 {
894     TestSourceContext *test = ctx->priv;
895
896     test->fill_picture_fn = smptehdbars_fill_picture;
897     test->draw_once = 1;
898     return init(ctx);
899 }
900
901 AVFilter avfilter_vsrc_smptehdbars = {
902     .name      = "smptehdbars",
903     .description = NULL_IF_CONFIG_SMALL("Generate SMPTE HD color bars."),
904     .priv_size = sizeof(TestSourceContext),
905     .init      = smptehdbars_init,
906     .uninit    = uninit,
907
908     .query_formats = smptebars_query_formats,
909     .inputs        = NULL,
910     .outputs       = smptebars_outputs,
911     .priv_class    = &smptehdbars_class,
912 };
913
914 #endif  /* CONFIG_SMPTEHDBARS_FILTER */
915 #endif  /* CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER */