2 * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
3 * Copyright (c) 2011 Stefano Sabatini
4 * Copyright (c) 2012 Paul B Mahol
6 * This file is part of FFmpeg.
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.
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.
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
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
30 * rgbtestsrc is ported from MPlayer libmpcodecs/vf_rgbtest.c by
31 * Michael Niedermayer.
33 * smptebars and smptehdbars are by Paul B Mahol.
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"
45 #include "drawutils.h"
53 unsigned int nb_frame;
54 AVRational time_base, frame_rate;
56 int64_t duration; ///< duration expressed in microseconds
57 AVRational sar; ///< sample aspect ratio
58 int draw_once; ///< draw only the first frame, always put out the same picture
59 int draw_once_reset; ///< draw only the first frame or in case of reset
60 AVFrame *picref; ///< cached reference containing the painted picture
62 void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
64 /* only used by testsrc */
67 /* only used by color */
70 uint8_t color_rgba[4];
72 /* only used by rgbtest */
75 /* only used by haldclut */
79 #define OFFSET(x) offsetof(TestSourceContext, x)
80 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
82 #define SIZE_OPTIONS \
83 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
84 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
86 #define COMMON_OPTIONS_NOSIZE \
87 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
88 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
89 { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
90 { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
91 { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1}, 0, INT_MAX, FLAGS },
93 #define COMMON_OPTIONS SIZE_OPTIONS COMMON_OPTIONS_NOSIZE
95 static const AVOption options[] = {
100 static av_cold int init(AVFilterContext *ctx)
102 TestSourceContext *test = ctx->priv;
104 test->time_base = av_inv_q(test->frame_rate);
108 av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
109 test->w, test->h, test->frame_rate.num, test->frame_rate.den,
110 test->duration < 0 ? -1 : (double)test->duration/1000000,
111 test->sar.num, test->sar.den);
115 static av_cold void uninit(AVFilterContext *ctx)
117 TestSourceContext *test = ctx->priv;
119 av_frame_free(&test->picref);
122 static int config_props(AVFilterLink *outlink)
124 TestSourceContext *test = outlink->src->priv;
126 outlink->w = test->w;
127 outlink->h = test->h;
128 outlink->sample_aspect_ratio = test->sar;
129 outlink->frame_rate = test->frame_rate;
130 outlink->time_base = test->time_base;
135 static int request_frame(AVFilterLink *outlink)
137 TestSourceContext *test = outlink->src->priv;
140 if (test->duration >= 0 &&
141 av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
144 if (test->draw_once) {
145 if (test->draw_once_reset) {
146 av_frame_free(&test->picref);
147 test->draw_once_reset = 0;
151 ff_get_video_buffer(outlink, test->w, test->h);
153 return AVERROR(ENOMEM);
154 test->fill_picture_fn(outlink->src, test->picref);
156 frame = av_frame_clone(test->picref);
158 frame = ff_get_video_buffer(outlink, test->w, test->h);
161 return AVERROR(ENOMEM);
162 frame->pts = test->pts;
163 frame->key_frame = 1;
164 frame->interlaced_frame = 0;
165 frame->pict_type = AV_PICTURE_TYPE_I;
166 frame->sample_aspect_ratio = test->sar;
167 if (!test->draw_once)
168 test->fill_picture_fn(outlink->src, frame);
173 return ff_filter_frame(outlink, frame);
176 #if CONFIG_COLOR_FILTER
178 static const AVOption color_options[] = {
179 { "color", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
180 { "c", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
185 AVFILTER_DEFINE_CLASS(color);
187 static void color_fill_picture(AVFilterContext *ctx, AVFrame *picref)
189 TestSourceContext *test = ctx->priv;
190 ff_fill_rectangle(&test->draw, &test->color,
191 picref->data, picref->linesize,
192 0, 0, test->w, test->h);
195 static av_cold int color_init(AVFilterContext *ctx)
197 TestSourceContext *test = ctx->priv;
198 test->fill_picture_fn = color_fill_picture;
203 static int color_query_formats(AVFilterContext *ctx)
205 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
209 static int color_config_props(AVFilterLink *inlink)
211 AVFilterContext *ctx = inlink->src;
212 TestSourceContext *test = ctx->priv;
215 ff_draw_init(&test->draw, inlink->format, 0);
216 ff_draw_color(&test->draw, &test->color, test->color_rgba);
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);
223 if ((ret = config_props(inlink)) < 0)
229 static int color_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
230 char *res, int res_len, int flags)
232 TestSourceContext *test = ctx->priv;
235 if (!strcmp(cmd, "color") || !strcmp(cmd, "c")) {
236 uint8_t color_rgba[4];
238 ret = av_parse_color(color_rgba, args, -1, ctx);
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;
248 return AVERROR(ENOSYS);
251 static const AVFilterPad color_outputs[] = {
254 .type = AVMEDIA_TYPE_VIDEO,
255 .request_frame = request_frame,
256 .config_props = color_config_props,
261 AVFilter avfilter_vsrc_color = {
263 .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
265 .priv_class = &color_class,
266 .priv_size = sizeof(TestSourceContext),
270 .query_formats = color_query_formats,
272 .outputs = color_outputs,
273 .process_command = color_process_command,
276 #endif /* CONFIG_COLOR_FILTER */
278 #if CONFIG_HALDCLUTSRC_FILTER
280 static const AVOption haldclutsrc_options[] = {
281 { "level", "set level", OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 6}, 2, 8, FLAGS },
282 COMMON_OPTIONS_NOSIZE
286 AVFILTER_DEFINE_CLASS(haldclutsrc);
288 static void haldclutsrc_fill_picture(AVFilterContext *ctx, AVFrame *frame)
290 int i, j, k, x = 0, y = 0, is16bit = 0, step;
292 const TestSourceContext *hc = ctx->priv;
293 int level = hc->level;
295 const int w = frame->width;
296 const int h = frame->height;
297 const uint8_t *data = frame->data[0];
298 const int linesize = frame->linesize[0];
299 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
302 av_assert0(w == h && w == level*level*level);
304 ff_fill_rgba_map(rgba_map, frame->format);
306 switch (frame->format) {
307 case AV_PIX_FMT_RGB48:
308 case AV_PIX_FMT_BGR48:
309 case AV_PIX_FMT_RGBA64:
310 case AV_PIX_FMT_BGRA64:
314 case AV_PIX_FMT_RGBA:
315 case AV_PIX_FMT_BGRA:
316 case AV_PIX_FMT_ARGB:
317 case AV_PIX_FMT_ABGR:
322 step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
323 scale = ((float)(1 << (8*(is16bit+1))) - 1) / (level*level - 1);
325 #define LOAD_CLUT(nbits) do { \
326 uint##nbits##_t *dst = ((uint##nbits##_t *)(data + y*linesize)) + x*step; \
327 dst[rgba_map[0]] = av_clip_uint##nbits(i * scale); \
328 dst[rgba_map[1]] = av_clip_uint##nbits(j * scale); \
329 dst[rgba_map[2]] = av_clip_uint##nbits(k * scale); \
331 dst[rgba_map[3]] = alpha; \
335 for (k = 0; k < level; k++) {
336 for (j = 0; j < level; j++) {
337 for (i = 0; i < level; i++) {
351 static av_cold int haldclutsrc_init(AVFilterContext *ctx)
353 TestSourceContext *hc = ctx->priv;
354 hc->fill_picture_fn = haldclutsrc_fill_picture;
359 static int haldclutsrc_query_formats(AVFilterContext *ctx)
361 static const enum AVPixelFormat pix_fmts[] = {
362 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
363 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
364 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
365 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
366 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
367 AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
368 AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
371 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
375 static int haldclutsrc_config_props(AVFilterLink *outlink)
377 AVFilterContext *ctx = outlink->src;
378 TestSourceContext *hc = ctx->priv;
380 hc->w = hc->h = hc->level * hc->level * hc->level;
381 return config_props(outlink);
384 static const AVFilterPad haldclutsrc_outputs[] = {
387 .type = AVMEDIA_TYPE_VIDEO,
388 .request_frame = request_frame,
389 .config_props = haldclutsrc_config_props,
394 AVFilter avfilter_vsrc_haldclutsrc = {
395 .name = "haldclutsrc",
396 .description = NULL_IF_CONFIG_SMALL("Provide an identity Hald CLUT."),
397 .priv_class = &haldclutsrc_class,
398 .priv_size = sizeof(TestSourceContext),
399 .init = haldclutsrc_init,
401 .query_formats = haldclutsrc_query_formats,
403 .outputs = haldclutsrc_outputs,
405 #endif /* CONFIG_HALDCLUTSRC_FILTER */
407 #if CONFIG_NULLSRC_FILTER
409 #define nullsrc_options options
410 AVFILTER_DEFINE_CLASS(nullsrc);
412 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFrame *picref) { }
414 static av_cold int nullsrc_init(AVFilterContext *ctx)
416 TestSourceContext *test = ctx->priv;
418 test->fill_picture_fn = nullsrc_fill_picture;
422 static const AVFilterPad nullsrc_outputs[] = {
425 .type = AVMEDIA_TYPE_VIDEO,
426 .request_frame = request_frame,
427 .config_props = config_props,
432 AVFilter avfilter_vsrc_nullsrc = {
434 .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
435 .init = nullsrc_init,
437 .priv_size = sizeof(TestSourceContext),
438 .priv_class = &nullsrc_class,
440 .outputs = nullsrc_outputs,
443 #endif /* CONFIG_NULLSRC_FILTER */
445 #if CONFIG_TESTSRC_FILTER
447 static const AVOption testsrc_options[] = {
449 { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, 0, 17, FLAGS },
450 { "n", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, 0, 17, FLAGS },
454 AVFILTER_DEFINE_CLASS(testsrc);
457 * Fill a rectangle with value val.
459 * @param val the RGB value to set
460 * @param dst pointer to the destination buffer to fill
461 * @param dst_linesize linesize of destination
462 * @param segment_width width of the segment
463 * @param x horizontal coordinate where to draw the rectangle in the destination buffer
464 * @param y horizontal coordinate where to draw the rectangle in the destination buffer
465 * @param w width of the rectangle to draw, expressed as a number of segment_width units
466 * @param h height of the rectangle to draw, expressed as a number of segment_width units
468 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, int segment_width,
469 int x, int y, int w, int h)
474 dst += segment_width * (step * x + y * dst_linesize);
475 w *= segment_width * step;
477 for (i = 0; i < h; i++) {
483 static void draw_digit(int digit, uint8_t *dst, int dst_linesize,
489 #define LEFT_TOP_VBAR 8
490 #define LEFT_BOT_VBAR 16
491 #define RIGHT_TOP_VBAR 32
492 #define RIGHT_BOT_VBAR 64
496 { 1, 0, 5, 1 }, /* TOP_HBAR */
497 { 1, 6, 5, 1 }, /* MID_HBAR */
498 { 1, 12, 5, 1 }, /* BOT_HBAR */
499 { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
500 { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
501 { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
502 { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
504 static const unsigned char masks[10] = {
505 /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
506 /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
507 /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
508 /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
509 /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
510 /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
511 /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
512 /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
513 /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
514 /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
516 unsigned mask = masks[digit];
519 draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
520 for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
522 draw_rectangle(255, dst, dst_linesize, segment_width,
523 segments[i].x, segments[i].y, segments[i].w, segments[i].h);
526 #define GRADIENT_SIZE (6 * 256)
528 static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
530 TestSourceContext *test = ctx->priv;
533 int color, color_rest;
537 int dquad_x, dquad_y;
538 int grad, dgrad, rgrad, drgrad;
542 uint8_t *data = frame->data[0];
543 int width = frame->width;
544 int height = frame->height;
546 /* draw colored bars and circle */
547 radius = (width + height) / 4;
548 quad0 = width * width / 4 + height * height / 4 - radius * radius;
549 dquad_y = 1 - height;
551 for (y = 0; y < height; y++) {
557 for (x = 0; x < width; x++) {
563 *(p++) = icolor & 1 ? 255 : 0;
564 *(p++) = icolor & 2 ? 255 : 0;
565 *(p++) = icolor & 4 ? 255 : 0;
567 if (color_rest >= width) {
574 p0 += frame->linesize[0];
577 /* draw sliding color line */
578 p0 = p = data + frame->linesize[0] * (height * 3/4);
579 grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
582 dgrad = GRADIENT_SIZE / width;
583 drgrad = GRADIENT_SIZE % width;
584 for (x = 0; x < width; x++) {
586 grad < 256 || grad >= 5 * 256 ? 255 :
587 grad >= 2 * 256 && grad < 4 * 256 ? 0 :
588 grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
590 grad >= 4 * 256 ? 0 :
591 grad >= 1 * 256 && grad < 3 * 256 ? 255 :
592 grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
595 grad >= 3 * 256 && grad < 5 * 256 ? 255 :
596 grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
599 if (rgrad >= GRADIENT_SIZE) {
601 rgrad -= GRADIENT_SIZE;
603 if (grad >= GRADIENT_SIZE)
604 grad -= GRADIENT_SIZE;
607 for (y = height / 8; y > 0; y--) {
608 memcpy(p+frame->linesize[0], p, 3 * width);
609 p += frame->linesize[0];
613 seg_size = width / 80;
614 if (seg_size >= 1 && height >= 13 * seg_size) {
615 int64_t p10decimals = 1;
616 double time = av_q2d(test->time_base) * test->nb_frame *
617 pow(10, test->nb_decimals);
621 for (x = 0; x < test->nb_decimals; x++)
624 second = av_rescale_rnd(test->nb_frame * test->time_base.num, p10decimals, test->time_base.den, AV_ROUND_ZERO);
625 x = width - (width - seg_size * 64) / 2;
626 y = (height - seg_size * 13) / 2;
627 p = data + (x*3 + y * frame->linesize[0]);
628 for (i = 0; i < 8; i++) {
629 p -= 3 * 8 * seg_size;
630 draw_digit(second % 10, p, frame->linesize[0], seg_size);
638 static av_cold int test_init(AVFilterContext *ctx)
640 TestSourceContext *test = ctx->priv;
642 test->fill_picture_fn = test_fill_picture;
646 static int test_query_formats(AVFilterContext *ctx)
648 static const enum AVPixelFormat pix_fmts[] = {
649 AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
651 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
655 static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
658 .type = AVMEDIA_TYPE_VIDEO,
659 .request_frame = request_frame,
660 .config_props = config_props,
665 AVFilter avfilter_vsrc_testsrc = {
667 .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
668 .priv_size = sizeof(TestSourceContext),
669 .priv_class = &testsrc_class,
673 .query_formats = test_query_formats,
676 .outputs = avfilter_vsrc_testsrc_outputs,
679 #endif /* CONFIG_TESTSRC_FILTER */
681 #if CONFIG_RGBTESTSRC_FILTER
683 #define rgbtestsrc_options options
684 AVFILTER_DEFINE_CLASS(rgbtestsrc);
691 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
692 int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
699 case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
700 case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
701 case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
702 case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
703 case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
704 case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
705 case AV_PIX_FMT_RGB24:
706 case AV_PIX_FMT_BGR24:
707 v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
708 p = dst + 3*x + y*dst_linesize;
711 case AV_PIX_FMT_RGBA:
712 case AV_PIX_FMT_BGRA:
713 case AV_PIX_FMT_ARGB:
714 case AV_PIX_FMT_ABGR:
715 v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
716 p = dst + 4*x + y*dst_linesize;
722 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
724 TestSourceContext *test = ctx->priv;
725 int x, y, w = frame->width, h = frame->height;
727 for (y = 0; y < h; y++) {
728 for (x = 0; x < w; x++) {
730 int r = 0, g = 0, b = 0;
733 else if (3*y < 2*h) g = c;
736 rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
737 ctx->outputs[0]->format, test->rgba_map);
742 static av_cold int rgbtest_init(AVFilterContext *ctx)
744 TestSourceContext *test = ctx->priv;
747 test->fill_picture_fn = rgbtest_fill_picture;
751 static int rgbtest_query_formats(AVFilterContext *ctx)
753 static const enum AVPixelFormat pix_fmts[] = {
754 AV_PIX_FMT_RGBA, AV_PIX_FMT_ARGB, AV_PIX_FMT_BGRA, AV_PIX_FMT_ABGR,
755 AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
756 AV_PIX_FMT_RGB444, AV_PIX_FMT_BGR444,
757 AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565,
758 AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555,
761 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
765 static int rgbtest_config_props(AVFilterLink *outlink)
767 TestSourceContext *test = outlink->src->priv;
769 ff_fill_rgba_map(test->rgba_map, outlink->format);
770 return config_props(outlink);
773 static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
776 .type = AVMEDIA_TYPE_VIDEO,
777 .request_frame = request_frame,
778 .config_props = rgbtest_config_props,
783 AVFilter avfilter_vsrc_rgbtestsrc = {
784 .name = "rgbtestsrc",
785 .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
786 .priv_size = sizeof(TestSourceContext),
787 .priv_class = &rgbtestsrc_class,
788 .init = rgbtest_init,
791 .query_formats = rgbtest_query_formats,
795 .outputs = avfilter_vsrc_rgbtestsrc_outputs,
798 #endif /* CONFIG_RGBTESTSRC_FILTER */
800 #if CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER
802 static const uint8_t rainbow[7][4] = {
803 { 191, 191, 191, 255 }, /* gray */
804 { 191, 191, 0, 255 }, /* yellow */
805 { 0, 191, 191, 255 }, /* cyan */
806 { 0, 191, 0, 255 }, /* green */
807 { 191, 0, 191, 255 }, /* magenta */
808 { 191, 0, 0, 255 }, /* red */
809 { 0, 0, 191, 255 }, /* blue */
812 static const uint8_t wobnair[7][4] = {
813 { 0, 0, 191, 255 }, /* blue */
814 { 19, 19, 19, 255 }, /* 7.5% intensity black */
815 { 191, 0, 191, 255 }, /* magenta */
816 { 19, 19, 19, 255 }, /* 7.5% intensity black */
817 { 0, 191, 191, 255 }, /* cyan */
818 { 19, 19, 19, 255 }, /* 7.5% intensity black */
819 { 191, 191, 191, 255 }, /* gray */
822 static const uint8_t white[4] = { 255, 255, 255, 255 };
823 static const uint8_t black[4] = { 19, 19, 19, 255 }; /* 7.5% intensity black */
826 static const uint8_t neg4ire[4] = { 9, 9, 9, 255 }; /* 3.5% intensity black */
827 static const uint8_t pos4ire[4] = { 29, 29, 29, 255 }; /* 11.5% intensity black */
830 static const uint8_t i_pixel[4] = { 0, 68, 130, 255 };
831 static const uint8_t q_pixel[4] = { 67, 0, 130, 255 };
833 static const uint8_t gray40[4] = { 102, 102, 102, 255 };
834 static const uint8_t gray15[4] = { 38, 38, 38, 255 };
835 static const uint8_t cyan[4] = { 0, 255, 255, 255 };
836 static const uint8_t yellow[4] = { 255, 255, 0, 255 };
837 static const uint8_t blue[4] = { 0, 0, 255, 255 };
838 static const uint8_t red[4] = { 255, 0, 0, 255 };
839 static const uint8_t black0[4] = { 5, 5, 5, 255 };
840 static const uint8_t black2[4] = { 10, 10, 10, 255 };
841 static const uint8_t black4[4] = { 15, 15, 15, 255 };
842 static const uint8_t neg2[4] = { 0, 0, 0, 255 };
844 static void inline draw_bar(TestSourceContext *test, const uint8_t *color,
845 unsigned x, unsigned y, unsigned w, unsigned h,
848 FFDrawColor draw_color;
850 x = FFMIN(x, test->w - 1);
851 y = FFMIN(y, test->h - 1);
852 w = FFMIN(w, test->w - x);
853 h = FFMIN(h, test->h - y);
855 av_assert0(x + w <= test->w);
856 av_assert0(y + h <= test->h);
858 ff_draw_color(&test->draw, &draw_color, color);
859 ff_fill_rectangle(&test->draw, &draw_color,
860 frame->data, frame->linesize, x, y, w, h);
863 static int smptebars_query_formats(AVFilterContext *ctx)
865 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
869 static int smptebars_config_props(AVFilterLink *outlink)
871 AVFilterContext *ctx = outlink->src;
872 TestSourceContext *test = ctx->priv;
874 ff_draw_init(&test->draw, outlink->format, 0);
876 return config_props(outlink);
879 static const AVFilterPad smptebars_outputs[] = {
882 .type = AVMEDIA_TYPE_VIDEO,
883 .request_frame = request_frame,
884 .config_props = smptebars_config_props,
889 #if CONFIG_SMPTEBARS_FILTER
891 #define smptebars_options options
892 AVFILTER_DEFINE_CLASS(smptebars);
894 static void smptebars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
896 TestSourceContext *test = ctx->priv;
897 int r_w, r_h, w_h, p_w, p_h, i, tmp, x = 0;
898 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
900 r_w = FFALIGN((test->w + 6) / 7, 1 << pixdesc->log2_chroma_w);
901 r_h = FFALIGN(test->h * 2 / 3, 1 << pixdesc->log2_chroma_h);
902 w_h = FFALIGN(test->h * 3 / 4 - r_h, 1 << pixdesc->log2_chroma_h);
903 p_w = FFALIGN(r_w * 5 / 4, 1 << pixdesc->log2_chroma_w);
904 p_h = test->h - w_h - r_h;
906 for (i = 0; i < 7; i++) {
907 draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
908 draw_bar(test, wobnair[i], x, r_h, r_w, w_h, picref);
912 draw_bar(test, i_pixel, x, r_h + w_h, p_w, p_h, picref);
914 draw_bar(test, white, x, r_h + w_h, p_w, p_h, picref);
916 draw_bar(test, q_pixel, x, r_h + w_h, p_w, p_h, picref);
918 tmp = FFALIGN(5 * r_w - x, 1 << pixdesc->log2_chroma_w);
919 draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
921 tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
922 draw_bar(test, neg4ire, x, r_h + w_h, tmp, p_h, picref);
924 draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
926 draw_bar(test, pos4ire, x, r_h + w_h, tmp, p_h, picref);
928 draw_bar(test, black, x, r_h + w_h, test->w - x, p_h, picref);
931 static av_cold int smptebars_init(AVFilterContext *ctx)
933 TestSourceContext *test = ctx->priv;
935 test->fill_picture_fn = smptebars_fill_picture;
940 AVFilter avfilter_vsrc_smptebars = {
942 .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
943 .priv_size = sizeof(TestSourceContext),
944 .init = smptebars_init,
947 .query_formats = smptebars_query_formats,
949 .outputs = smptebars_outputs,
950 .priv_class = &smptebars_class,
953 #endif /* CONFIG_SMPTEBARS_FILTER */
955 #if CONFIG_SMPTEHDBARS_FILTER
957 #define smptehdbars_options options
958 AVFILTER_DEFINE_CLASS(smptehdbars);
960 static void smptehdbars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
962 TestSourceContext *test = ctx->priv;
963 int d_w, r_w, r_h, l_w, i, tmp, x = 0, y = 0;
964 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
966 d_w = FFALIGN(test->w / 8, 1 << pixdesc->log2_chroma_w);
967 r_h = FFALIGN(test->h * 7 / 12, 1 << pixdesc->log2_chroma_h);
968 draw_bar(test, gray40, x, 0, d_w, r_h, picref);
971 r_w = FFALIGN((((test->w + 3) / 4) * 3) / 7, 1 << pixdesc->log2_chroma_w);
972 for (i = 0; i < 7; i++) {
973 draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
976 draw_bar(test, gray40, x, 0, test->w - x, r_h, picref);
978 r_h = FFALIGN(test->h / 12, 1 << pixdesc->log2_chroma_h);
979 draw_bar(test, cyan, 0, y, d_w, r_h, picref);
981 draw_bar(test, i_pixel, x, y, r_w, r_h, picref);
984 draw_bar(test, rainbow[0], x, y, tmp, r_h, picref);
987 draw_bar(test, blue, x, y, test->w - x, r_h, picref);
989 draw_bar(test, yellow, 0, y, d_w, r_h, picref);
991 draw_bar(test, q_pixel, x, y, r_w, r_h, picref);
994 for (i = 0; i < tmp; i += 1 << pixdesc->log2_chroma_w) {
995 uint8_t yramp[4] = {0};
999 yramp[2] = i * 255 / tmp;
1002 draw_bar(test, yramp, x, y, 1 << pixdesc->log2_chroma_w, r_h, picref);
1003 x += 1 << pixdesc->log2_chroma_w;
1005 draw_bar(test, red, x, y, test->w - x, r_h, picref);
1007 draw_bar(test, gray15, 0, y, d_w, test->h - y, picref);
1009 tmp = FFALIGN(r_w * 3 / 2, 1 << pixdesc->log2_chroma_w);
1010 draw_bar(test, black0, x, y, tmp, test->h - y, picref);
1012 tmp = FFALIGN(r_w * 2, 1 << pixdesc->log2_chroma_w);
1013 draw_bar(test, white, x, y, tmp, test->h - y, picref);
1015 tmp = FFALIGN(r_w * 5 / 6, 1 << pixdesc->log2_chroma_w);
1016 draw_bar(test, black0, x, y, tmp, test->h - y, picref);
1018 tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
1019 draw_bar(test, neg2, x, y, tmp, test->h - y, picref);
1021 draw_bar(test, black0, x, y, tmp, test->h - y, picref);
1023 draw_bar(test, black2, x, y, tmp, test->h - y, picref);
1025 draw_bar(test, black0, x, y, tmp, test->h - y, picref);
1027 draw_bar(test, black4, x, y, tmp, test->h - y, picref);
1030 draw_bar(test, black0, x, y, r_w, test->h - y, picref);
1032 draw_bar(test, gray15, x, y, test->w - x, test->h - y, picref);
1035 static av_cold int smptehdbars_init(AVFilterContext *ctx)
1037 TestSourceContext *test = ctx->priv;
1039 test->fill_picture_fn = smptehdbars_fill_picture;
1040 test->draw_once = 1;
1044 AVFilter avfilter_vsrc_smptehdbars = {
1045 .name = "smptehdbars",
1046 .description = NULL_IF_CONFIG_SMALL("Generate SMPTE HD color bars."),
1047 .priv_size = sizeof(TestSourceContext),
1048 .init = smptehdbars_init,
1051 .query_formats = smptebars_query_formats,
1053 .outputs = smptebars_outputs,
1054 .priv_class = &smptehdbars_class,
1057 #endif /* CONFIG_SMPTEHDBARS_FILTER */
1058 #endif /* CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER */