]> git.sesse.net Git - ffmpeg/blob - libavfilter/vsrc_testsrc.c
Merge commit '3f47d316cda9037024ffbc76f789332e62b729bc'
[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 is by Paul B Mahol.
34  */
35
36 #include <float.h>
37
38 #include "libavutil/common.h"
39 #include "libavutil/opt.h"
40 #include "libavutil/imgutils.h"
41 #include "libavutil/intreadwrite.h"
42 #include "libavutil/parseutils.h"
43 #include "avfilter.h"
44 #include "drawutils.h"
45 #include "formats.h"
46 #include "internal.h"
47 #include "video.h"
48
49 typedef struct {
50     const AVClass *class;
51     int w, h;
52     unsigned int nb_frame;
53     AVRational time_base, frame_rate;
54     int64_t pts;
55     char *frame_rate_str;       ///< video frame rate
56     char *duration_str;         ///< total duration of the generated video
57     int64_t duration;           ///< duration expressed in microseconds
58     AVRational sar;             ///< sample aspect ratio
59     int nb_decimals;
60     int draw_once;              ///< draw only the first frame, always put out the same picture
61     AVFilterBufferRef *picref;  ///< cached reference containing the painted picture
62
63     void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
64
65     /* only used by color */
66     char *color_str;
67     FFDrawContext draw;
68     FFDrawColor color;
69     uint8_t color_rgba[4];
70
71     /* only used by rgbtest */
72     uint8_t rgba_map[4];
73 } TestSourceContext;
74
75 #define OFFSET(x) offsetof(TestSourceContext, x)
76 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
77
78 static const AVOption options[] = {
79     { "size",     "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },
80     { "s",        "set video size",     OFFSET(w),        AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },
81     { "rate",     "set video rate",     OFFSET(frame_rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
82     { "r",        "set video rate",     OFFSET(frame_rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
83     { "duration", "set video duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL},   0, 0, FLAGS },
84     { "d",        "set video duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL},   0, 0, FLAGS },
85     { "sar",      "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1},  0, INT_MAX, FLAGS },
86
87     /* only used by color */
88     { "color", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
89     { "c",     "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
90
91     /* only used by testsrc */
92     { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0},  INT_MIN, INT_MAX, FLAGS },
93     { "n",        "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0},  INT_MIN, INT_MAX, FLAGS },
94     { NULL },
95 };
96
97 static av_cold int init(AVFilterContext *ctx, const char *args)
98 {
99     TestSourceContext *test = ctx->priv;
100     int ret = 0;
101
102     av_opt_set_defaults(test);
103
104     if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0)
105         return ret;
106
107     if ((ret = av_parse_video_rate(&test->frame_rate, test->frame_rate_str)) < 0) {
108         av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->frame_rate_str);
109         return ret;
110     }
111
112     test->duration = -1;
113     if (test->duration_str &&
114         (ret = av_parse_time(&test->duration, test->duration_str, 1)) < 0) {
115         av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration_str);
116         return ret;
117     }
118
119     if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
120         av_log(ctx, AV_LOG_WARNING,
121                "Option 'decimals' is ignored with source '%s'\n",
122                ctx->filter->name);
123     }
124
125     if (test->color_str) {
126         if (!strcmp(ctx->filter->name, "color")) {
127             ret = av_parse_color(test->color_rgba, test->color_str, -1, ctx);
128             if (ret < 0)
129                 return ret;
130         } else {
131             av_log(ctx, AV_LOG_WARNING,
132                    "Option 'color' is ignored with source '%s'\n",
133                    ctx->filter->name);
134         }
135     }
136
137     test->time_base = av_inv_q(test->frame_rate);
138     test->nb_frame = 0;
139     test->pts = 0;
140
141     av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
142            test->w, test->h, test->frame_rate.num, test->frame_rate.den,
143            test->duration < 0 ? -1 : (double)test->duration/1000000,
144            test->sar.num, test->sar.den);
145     return 0;
146 }
147
148 static av_cold void uninit(AVFilterContext *ctx)
149 {
150     TestSourceContext *test = ctx->priv;
151
152     av_opt_free(test);
153     avfilter_unref_bufferp(&test->picref);
154 }
155
156 static int config_props(AVFilterLink *outlink)
157 {
158     TestSourceContext *test = outlink->src->priv;
159
160     outlink->w = test->w;
161     outlink->h = test->h;
162     outlink->sample_aspect_ratio = test->sar;
163     outlink->frame_rate = test->frame_rate;
164     outlink->time_base  = test->time_base;
165
166     return 0;
167 }
168
169 static int request_frame(AVFilterLink *outlink)
170 {
171     TestSourceContext *test = outlink->src->priv;
172     AVFilterBufferRef *outpicref;
173
174     if (test->duration >= 0 &&
175         av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
176         return AVERROR_EOF;
177
178     if (test->draw_once) {
179         if (!test->picref) {
180             test->picref =
181                 ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_PRESERVE|AV_PERM_REUSE,
182                                     test->w, test->h);
183             if (!test->picref)
184                 return AVERROR(ENOMEM);
185             test->fill_picture_fn(outlink->src, test->picref);
186         }
187         outpicref = avfilter_ref_buffer(test->picref, ~AV_PERM_WRITE);
188     } else
189         outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, test->w, test->h);
190
191     if (!outpicref)
192         return AVERROR(ENOMEM);
193     outpicref->pts = test->pts;
194     outpicref->pos = -1;
195     outpicref->video->key_frame = 1;
196     outpicref->video->interlaced = 0;
197     outpicref->video->pict_type = AV_PICTURE_TYPE_I;
198     outpicref->video->sample_aspect_ratio = test->sar;
199     if (!test->draw_once)
200         test->fill_picture_fn(outlink->src, outpicref);
201
202     test->pts++;
203     test->nb_frame++;
204
205     return ff_filter_frame(outlink, outpicref);
206 }
207
208 #if CONFIG_COLOR_FILTER
209
210 #define color_options options
211 AVFILTER_DEFINE_CLASS(color);
212
213 static void color_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
214 {
215     TestSourceContext *test = ctx->priv;
216     ff_fill_rectangle(&test->draw, &test->color,
217                       picref->data, picref->linesize,
218                       0, 0, test->w, test->h);
219 }
220
221 static av_cold int color_init(AVFilterContext *ctx, const char *args)
222 {
223     TestSourceContext *test = ctx->priv;
224     test->class = &color_class;
225     test->fill_picture_fn = color_fill_picture;
226     test->draw_once = 1;
227     av_opt_set(test, "color", "black", 0);
228     return init(ctx, args);
229 }
230
231 static int color_query_formats(AVFilterContext *ctx)
232 {
233     ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
234     return 0;
235 }
236
237 static int color_config_props(AVFilterLink *inlink)
238 {
239     AVFilterContext *ctx = inlink->src;
240     TestSourceContext *test = ctx->priv;
241     int ret;
242
243     ff_draw_init(&test->draw, inlink->format, 0);
244     ff_draw_color(&test->draw, &test->color, test->color_rgba);
245
246     test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
247     test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
248     if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
249         return AVERROR(EINVAL);
250
251     if ((ret = config_props(inlink)) < 0)
252         return ret;
253
254     av_log(ctx, AV_LOG_VERBOSE, "color:0x%02x%02x%02x%02x\n",
255            test->color_rgba[0], test->color_rgba[1], test->color_rgba[2], test->color_rgba[3]);
256     return 0;
257 }
258
259 static const AVFilterPad color_outputs[] = {
260     {
261         .name          = "default",
262         .type          = AVMEDIA_TYPE_VIDEO,
263         .request_frame = request_frame,
264         .config_props  = color_config_props,
265     },
266     {  NULL }
267 };
268
269 AVFilter avfilter_vsrc_color = {
270     .name        = "color",
271     .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
272
273     .priv_size = sizeof(TestSourceContext),
274     .init      = color_init,
275     .uninit    = uninit,
276
277     .query_formats = color_query_formats,
278     .inputs        = NULL,
279     .outputs       = color_outputs,
280     .priv_class    = &color_class,
281 };
282
283 #endif /* CONFIG_COLOR_FILTER */
284
285 #if CONFIG_NULLSRC_FILTER
286
287 #define nullsrc_options options
288 AVFILTER_DEFINE_CLASS(nullsrc);
289
290 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref) { }
291
292 static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args)
293 {
294     TestSourceContext *test = ctx->priv;
295
296     test->class = &nullsrc_class;
297     test->fill_picture_fn = nullsrc_fill_picture;
298     return init(ctx, args);
299 }
300
301 static const AVFilterPad nullsrc_outputs[] = {
302     {
303         .name          = "default",
304         .type          = AVMEDIA_TYPE_VIDEO,
305         .request_frame = request_frame,
306         .config_props  = config_props,
307     },
308     { NULL },
309 };
310
311 AVFilter avfilter_vsrc_nullsrc = {
312     .name        = "nullsrc",
313     .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
314     .init       = nullsrc_init,
315     .uninit     = uninit,
316     .priv_size  = sizeof(TestSourceContext),
317     .inputs     = NULL,
318     .outputs    = nullsrc_outputs,
319     .priv_class = &nullsrc_class,
320 };
321
322 #endif /* CONFIG_NULLSRC_FILTER */
323
324 #if CONFIG_TESTSRC_FILTER
325
326 #define testsrc_options options
327 AVFILTER_DEFINE_CLASS(testsrc);
328
329 /**
330  * Fill a rectangle with value val.
331  *
332  * @param val the RGB value to set
333  * @param dst pointer to the destination buffer to fill
334  * @param dst_linesize linesize of destination
335  * @param segment_width width of the segment
336  * @param x horizontal coordinate where to draw the rectangle in the destination buffer
337  * @param y horizontal coordinate where to draw the rectangle in the destination buffer
338  * @param w width  of the rectangle to draw, expressed as a number of segment_width units
339  * @param h height of the rectangle to draw, expressed as a number of segment_width units
340  */
341 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
342                            unsigned x, unsigned y, unsigned w, unsigned h)
343 {
344     int i;
345     int step = 3;
346
347     dst += segment_width * (step * x + y * dst_linesize);
348     w *= segment_width * step;
349     h *= segment_width;
350     for (i = 0; i < h; i++) {
351         memset(dst, val, w);
352         dst += dst_linesize;
353     }
354 }
355
356 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
357                        unsigned segment_width)
358 {
359 #define TOP_HBAR        1
360 #define MID_HBAR        2
361 #define BOT_HBAR        4
362 #define LEFT_TOP_VBAR   8
363 #define LEFT_BOT_VBAR  16
364 #define RIGHT_TOP_VBAR 32
365 #define RIGHT_BOT_VBAR 64
366     struct {
367         int x, y, w, h;
368     } segments[] = {
369         { 1,  0, 5, 1 }, /* TOP_HBAR */
370         { 1,  6, 5, 1 }, /* MID_HBAR */
371         { 1, 12, 5, 1 }, /* BOT_HBAR */
372         { 0,  1, 1, 5 }, /* LEFT_TOP_VBAR */
373         { 0,  7, 1, 5 }, /* LEFT_BOT_VBAR */
374         { 6,  1, 1, 5 }, /* RIGHT_TOP_VBAR */
375         { 6,  7, 1, 5 }  /* RIGHT_BOT_VBAR */
376     };
377     static const unsigned char masks[10] = {
378         /* 0 */ TOP_HBAR         |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
379         /* 1 */                                                        RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
380         /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR                             |RIGHT_TOP_VBAR,
381         /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR                            |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
382         /* 4 */          MID_HBAR         |LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
383         /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR                             |RIGHT_BOT_VBAR,
384         /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR               |RIGHT_BOT_VBAR,
385         /* 7 */ TOP_HBAR                                              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
386         /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
387         /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR              |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
388     };
389     unsigned mask = masks[digit];
390     int i;
391
392     draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
393     for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
394         if (mask & (1<<i))
395             draw_rectangle(255, dst, dst_linesize, segment_width,
396                            segments[i].x, segments[i].y, segments[i].w, segments[i].h);
397 }
398
399 #define GRADIENT_SIZE (6 * 256)
400
401 static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
402 {
403     TestSourceContext *test = ctx->priv;
404     uint8_t *p, *p0;
405     int x, y;
406     int color, color_rest;
407     int icolor;
408     int radius;
409     int quad0, quad;
410     int dquad_x, dquad_y;
411     int grad, dgrad, rgrad, drgrad;
412     int seg_size;
413     int second;
414     int i;
415     uint8_t *data = picref->data[0];
416     int width  = picref->video->w;
417     int height = picref->video->h;
418
419     /* draw colored bars and circle */
420     radius = (width + height) / 4;
421     quad0 = width * width / 4 + height * height / 4 - radius * radius;
422     dquad_y = 1 - height;
423     p0 = data;
424     for (y = 0; y < height; y++) {
425         p = p0;
426         color = 0;
427         color_rest = 0;
428         quad = quad0;
429         dquad_x = 1 - width;
430         for (x = 0; x < width; x++) {
431             icolor = color;
432             if (quad < 0)
433                 icolor ^= 7;
434             quad += dquad_x;
435             dquad_x += 2;
436             *(p++) = icolor & 1 ? 255 : 0;
437             *(p++) = icolor & 2 ? 255 : 0;
438             *(p++) = icolor & 4 ? 255 : 0;
439             color_rest += 8;
440             if (color_rest >= width) {
441                 color_rest -= width;
442                 color++;
443             }
444         }
445         quad0 += dquad_y;
446         dquad_y += 2;
447         p0 += picref->linesize[0];
448     }
449
450     /* draw sliding color line */
451     p0 = p = data + picref->linesize[0] * height * 3/4;
452     grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
453         GRADIENT_SIZE;
454     rgrad = 0;
455     dgrad = GRADIENT_SIZE / width;
456     drgrad = GRADIENT_SIZE % width;
457     for (x = 0; x < width; x++) {
458         *(p++) =
459             grad < 256 || grad >= 5 * 256 ? 255 :
460             grad >= 2 * 256 && grad < 4 * 256 ? 0 :
461             grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
462         *(p++) =
463             grad >= 4 * 256 ? 0 :
464             grad >= 1 * 256 && grad < 3 * 256 ? 255 :
465             grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
466         *(p++) =
467             grad < 2 * 256 ? 0 :
468             grad >= 3 * 256 && grad < 5 * 256 ? 255 :
469             grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
470         grad += dgrad;
471         rgrad += drgrad;
472         if (rgrad >= GRADIENT_SIZE) {
473             grad++;
474             rgrad -= GRADIENT_SIZE;
475         }
476         if (grad >= GRADIENT_SIZE)
477             grad -= GRADIENT_SIZE;
478     }
479     p = p0;
480     for (y = height / 8; y > 0; y--) {
481         memcpy(p+picref->linesize[0], p, 3 * width);
482         p += picref->linesize[0];
483     }
484
485     /* draw digits */
486     seg_size = width / 80;
487     if (seg_size >= 1 && height >= 13 * seg_size) {
488         double time = av_q2d(test->time_base) * test->nb_frame *
489                       pow(10, test->nb_decimals);
490         if (time > INT_MAX)
491             return;
492         second = (int)time;
493         x = width - (width - seg_size * 64) / 2;
494         y = (height - seg_size * 13) / 2;
495         p = data + (x*3 + y * picref->linesize[0]);
496         for (i = 0; i < 8; i++) {
497             p -= 3 * 8 * seg_size;
498             draw_digit(second % 10, p, picref->linesize[0], seg_size);
499             second /= 10;
500             if (second == 0)
501                 break;
502         }
503     }
504 }
505
506 static av_cold int test_init(AVFilterContext *ctx, const char *args)
507 {
508     TestSourceContext *test = ctx->priv;
509
510     test->class = &testsrc_class;
511     test->fill_picture_fn = test_fill_picture;
512     return init(ctx, args);
513 }
514
515 static int test_query_formats(AVFilterContext *ctx)
516 {
517     static const enum AVPixelFormat pix_fmts[] = {
518         AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
519     };
520     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
521     return 0;
522 }
523
524 static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
525     {
526         .name          = "default",
527         .type          = AVMEDIA_TYPE_VIDEO,
528         .request_frame = request_frame,
529         .config_props  = config_props,
530     },
531     { NULL }
532 };
533
534 AVFilter avfilter_vsrc_testsrc = {
535     .name      = "testsrc",
536     .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
537     .priv_size = sizeof(TestSourceContext),
538     .init      = test_init,
539     .uninit    = uninit,
540
541     .query_formats   = test_query_formats,
542
543     .inputs    = NULL,
544     .outputs   = avfilter_vsrc_testsrc_outputs,
545     .priv_class = &testsrc_class,
546 };
547
548 #endif /* CONFIG_TESTSRC_FILTER */
549
550 #if CONFIG_RGBTESTSRC_FILTER
551
552 #define rgbtestsrc_options options
553 AVFILTER_DEFINE_CLASS(rgbtestsrc);
554
555 #define R 0
556 #define G 1
557 #define B 2
558 #define A 3
559
560 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
561                               int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
562                               uint8_t rgba_map[4])
563 {
564     int32_t v;
565     uint8_t *p;
566
567     switch (fmt) {
568     case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
569     case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
570     case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
571     case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
572     case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
573     case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
574     case AV_PIX_FMT_RGB24:
575     case AV_PIX_FMT_BGR24:
576         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
577         p = dst + 3*x + y*dst_linesize;
578         AV_WL24(p, v);
579         break;
580     case AV_PIX_FMT_RGBA:
581     case AV_PIX_FMT_BGRA:
582     case AV_PIX_FMT_ARGB:
583     case AV_PIX_FMT_ABGR:
584         v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
585         p = dst + 4*x + y*dst_linesize;
586         AV_WL32(p, v);
587         break;
588     }
589 }
590
591 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
592 {
593     TestSourceContext *test = ctx->priv;
594     int x, y, w = picref->video->w, h = picref->video->h;
595
596     for (y = 0; y < h; y++) {
597          for (x = 0; x < picref->video->w; x++) {
598              int c = 256*x/w;
599              int r = 0, g = 0, b = 0;
600
601              if      (3*y < h  ) r = c;
602              else if (3*y < 2*h) g = c;
603              else                b = c;
604
605              rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
606                                ctx->outputs[0]->format, test->rgba_map);
607          }
608      }
609 }
610
611 static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
612 {
613     TestSourceContext *test = ctx->priv;
614
615     test->draw_once = 1;
616     test->class = &rgbtestsrc_class;
617     test->fill_picture_fn = rgbtest_fill_picture;
618     return init(ctx, args);
619 }
620
621 static int rgbtest_query_formats(AVFilterContext *ctx)
622 {
623     static const enum AVPixelFormat pix_fmts[] = {
624         AV_PIX_FMT_RGBA, AV_PIX_FMT_ARGB, AV_PIX_FMT_BGRA, AV_PIX_FMT_ABGR,
625         AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
626         AV_PIX_FMT_RGB444, AV_PIX_FMT_BGR444,
627         AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565,
628         AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555,
629         AV_PIX_FMT_NONE
630     };
631     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
632     return 0;
633 }
634
635 static int rgbtest_config_props(AVFilterLink *outlink)
636 {
637     TestSourceContext *test = outlink->src->priv;
638
639     ff_fill_rgba_map(test->rgba_map, outlink->format);
640     return config_props(outlink);
641 }
642
643 static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
644     {
645         .name          = "default",
646         .type          = AVMEDIA_TYPE_VIDEO,
647         .request_frame = request_frame,
648         .config_props  = rgbtest_config_props,
649     },
650     { NULL }
651 };
652
653 AVFilter avfilter_vsrc_rgbtestsrc = {
654     .name      = "rgbtestsrc",
655     .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
656     .priv_size = sizeof(TestSourceContext),
657     .init      = rgbtest_init,
658     .uninit    = uninit,
659
660     .query_formats   = rgbtest_query_formats,
661
662     .inputs    = NULL,
663
664     .outputs   = avfilter_vsrc_rgbtestsrc_outputs,
665     .priv_class = &rgbtestsrc_class,
666 };
667
668 #endif /* CONFIG_RGBTESTSRC_FILTER */
669
670 #if CONFIG_SMPTEBARS_FILTER
671
672 #define smptebars_options options
673 AVFILTER_DEFINE_CLASS(smptebars);
674
675 static const uint8_t rainbow[7][4] = {
676     { 191, 191, 191, 255 },     /* gray */
677     { 191, 191,   0, 255 },     /* yellow */
678     {   0, 191, 191, 255 },     /* cyan */
679     {   0, 191,   0, 255 },     /* green */
680     { 191,   0, 191, 255 },     /* magenta */
681     { 191,   0,   0, 255 },     /* red */
682     {   0,   0, 191, 255 },     /* blue */
683 };
684
685 static const uint8_t wobnair[7][4] = {
686     {   0,   0, 191, 255 },     /* blue */
687     {  19,  19,  19, 255 },     /* 7.5% intensity black */
688     { 191,   0, 191, 255 },     /* magenta */
689     {  19,  19,  19, 255 },     /* 7.5% intensity black */
690     {   0, 191, 191, 255 },     /* cyan */
691     {  19,  19,  19, 255 },     /* 7.5% intensity black */
692     { 191, 191, 191, 255 },     /* gray */
693 };
694
695 static const uint8_t white[4] = { 255, 255, 255, 255 };
696 static const uint8_t black[4] = {  19,  19,  19, 255 }; /* 7.5% intensity black */
697
698 /* pluge pulses */
699 static const uint8_t neg4ire[4] = {   9,   9,   9, 255 }; /*  3.5% intensity black */
700 static const uint8_t pos4ire[4] = {  29,  29,  29, 255 }; /* 11.5% intensity black */
701
702 /* fudged Q/-I */
703 static const uint8_t i_pixel[4] = {   0,  68, 130, 255 };
704 static const uint8_t q_pixel[4] = {  67,   0, 130, 255 };
705
706 static void smptebars_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
707 {
708     TestSourceContext *test = ctx->priv;
709     FFDrawColor color;
710     int r_w, r_h, w_h, p_w, p_h, i, x = 0;
711
712     r_w = (test->w + 6) / 7;
713     r_h = test->h * 2 / 3;
714     w_h = test->h * 3 / 4 - r_h;
715     p_w = r_w * 5 / 4;
716     p_h = test->h - w_h - r_h;
717
718 #define DRAW_COLOR(rgba, x, y, w, h)                                    \
719     ff_draw_color(&test->draw, &color, rgba);                           \
720     ff_fill_rectangle(&test->draw, &color,                              \
721                       picref->data, picref->linesize, x, y, w, h)       \
722
723     for (i = 0; i < 7; i++) {
724         DRAW_COLOR(rainbow[i], x, 0,   FFMIN(r_w, test->w - x), r_h);
725         DRAW_COLOR(wobnair[i], x, r_h, FFMIN(r_w, test->w - x), w_h);
726         x += r_w;
727     }
728     x = 0;
729     DRAW_COLOR(i_pixel, x, r_h + w_h, p_w, p_h);
730     x += p_w;
731     DRAW_COLOR(white, x, r_h + w_h, p_w, p_h);
732     x += p_w;
733     DRAW_COLOR(q_pixel, x, r_h + w_h, p_w, p_h);
734     x += p_w;
735     DRAW_COLOR(black, x, r_h + w_h, 5 * r_w - x, p_h);
736     x += 5 * r_w - x;
737     DRAW_COLOR(neg4ire, x, r_h + w_h, r_w / 3, p_h);
738     x += r_w / 3;
739     DRAW_COLOR(black, x, r_h + w_h, r_w / 3, p_h);
740     x += r_w / 3;
741     DRAW_COLOR(pos4ire, x, r_h + w_h, r_w / 3, p_h);
742     x += r_w / 3;
743     DRAW_COLOR(black, x, r_h + w_h, test->w - x, p_h);
744 }
745
746 static av_cold int smptebars_init(AVFilterContext *ctx, const char *args)
747 {
748     TestSourceContext *test = ctx->priv;
749
750     test->class = &smptebars_class;
751     test->fill_picture_fn = smptebars_fill_picture;
752     test->draw_once = 1;
753     return init(ctx, args);
754 }
755
756 static int smptebars_query_formats(AVFilterContext *ctx)
757 {
758     ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
759     return 0;
760 }
761
762 static int smptebars_config_props(AVFilterLink *outlink)
763 {
764     AVFilterContext *ctx = outlink->src;
765     TestSourceContext *test = ctx->priv;
766
767     ff_draw_init(&test->draw, outlink->format, 0);
768
769     return config_props(outlink);
770 }
771
772 static const AVFilterPad smptebars_outputs[] = {
773     {
774         .name          = "default",
775         .type          = AVMEDIA_TYPE_VIDEO,
776         .request_frame = request_frame,
777         .config_props  = smptebars_config_props,
778     },
779     { NULL }
780 };
781
782 AVFilter avfilter_vsrc_smptebars = {
783     .name      = "smptebars",
784     .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
785     .priv_size = sizeof(TestSourceContext),
786     .init      = smptebars_init,
787     .uninit    = uninit,
788
789     .query_formats = smptebars_query_formats,
790     .inputs        = NULL,
791     .outputs       = smptebars_outputs,
792     .priv_class    = &smptebars_class,
793 };
794
795 #endif  /* CONFIG_SMPTEBARS_FILTER */