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
59 int draw_once; ///< draw only the first frame, always put out the same picture
60 AVFrame *picref; ///< cached reference containing the painted picture
62 void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
64 /* only used by color */
68 uint8_t color_rgba[4];
70 /* only used by rgbtest */
74 #define OFFSET(x) offsetof(TestSourceContext, x)
75 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
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 },
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 },
96 static const AVOption 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 },
105 static av_cold int init(AVFilterContext *ctx)
107 TestSourceContext *test = ctx->priv;
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",
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);
122 av_log(ctx, AV_LOG_WARNING,
123 "Option 'color' is ignored with source '%s'\n",
128 test->time_base = av_inv_q(test->frame_rate);
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);
139 static av_cold void uninit(AVFilterContext *ctx)
141 TestSourceContext *test = ctx->priv;
143 av_frame_free(&test->picref);
146 static int config_props(AVFilterLink *outlink)
148 TestSourceContext *test = outlink->src->priv;
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;
159 static int request_frame(AVFilterLink *outlink)
161 TestSourceContext *test = outlink->src->priv;
164 if (test->duration >= 0 &&
165 av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
168 if (test->draw_once) {
171 ff_get_video_buffer(outlink, test->w, test->h);
173 return AVERROR(ENOMEM);
174 test->fill_picture_fn(outlink->src, test->picref);
176 frame = av_frame_clone(test->picref);
178 frame = ff_get_video_buffer(outlink, test->w, test->h);
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);
193 return ff_filter_frame(outlink, frame);
196 #if CONFIG_COLOR_FILTER
198 AVFILTER_DEFINE_CLASS(color);
200 static void color_fill_picture(AVFilterContext *ctx, AVFrame *picref)
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);
208 static av_cold int color_init(AVFilterContext *ctx)
210 TestSourceContext *test = ctx->priv;
211 test->fill_picture_fn = color_fill_picture;
216 static int color_query_formats(AVFilterContext *ctx)
218 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
222 static int color_config_props(AVFilterLink *inlink)
224 AVFilterContext *ctx = inlink->src;
225 TestSourceContext *test = ctx->priv;
228 ff_draw_init(&test->draw, inlink->format, 0);
229 ff_draw_color(&test->draw, &test->color, test->color_rgba);
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);
236 if ((ret = config_props(inlink)) < 0)
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]);
244 static const AVFilterPad color_outputs[] = {
247 .type = AVMEDIA_TYPE_VIDEO,
248 .request_frame = request_frame,
249 .config_props = color_config_props,
254 AVFilter avfilter_vsrc_color = {
256 .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
258 .priv_class = &color_class,
259 .priv_size = sizeof(TestSourceContext),
263 .query_formats = color_query_formats,
265 .outputs = color_outputs,
268 #endif /* CONFIG_COLOR_FILTER */
270 #if CONFIG_NULLSRC_FILTER
272 #define nullsrc_options options
273 AVFILTER_DEFINE_CLASS(nullsrc);
275 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFrame *picref) { }
277 static av_cold int nullsrc_init(AVFilterContext *ctx)
279 TestSourceContext *test = ctx->priv;
281 test->fill_picture_fn = nullsrc_fill_picture;
285 static const AVFilterPad nullsrc_outputs[] = {
288 .type = AVMEDIA_TYPE_VIDEO,
289 .request_frame = request_frame,
290 .config_props = config_props,
295 AVFilter avfilter_vsrc_nullsrc = {
297 .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
298 .init = nullsrc_init,
300 .priv_size = sizeof(TestSourceContext),
301 .priv_class = &nullsrc_class,
303 .outputs = nullsrc_outputs,
306 #endif /* CONFIG_NULLSRC_FILTER */
308 #if CONFIG_TESTSRC_FILTER
310 #define testsrc_options options
311 AVFILTER_DEFINE_CLASS(testsrc);
314 * Fill a rectangle with value val.
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
325 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
326 unsigned x, unsigned y, unsigned w, unsigned h)
331 dst += segment_width * (step * x + y * dst_linesize);
332 w *= segment_width * step;
334 for (i = 0; i < h; i++) {
340 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
341 unsigned segment_width)
346 #define LEFT_TOP_VBAR 8
347 #define LEFT_BOT_VBAR 16
348 #define RIGHT_TOP_VBAR 32
349 #define RIGHT_BOT_VBAR 64
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 */
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,
373 unsigned mask = masks[digit];
376 draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
377 for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
379 draw_rectangle(255, dst, dst_linesize, segment_width,
380 segments[i].x, segments[i].y, segments[i].w, segments[i].h);
383 #define GRADIENT_SIZE (6 * 256)
385 static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
387 TestSourceContext *test = ctx->priv;
390 int color, color_rest;
394 int dquad_x, dquad_y;
395 int grad, dgrad, rgrad, drgrad;
399 uint8_t *data = frame->data[0];
400 int width = frame->width;
401 int height = frame->height;
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;
408 for (y = 0; y < height; y++) {
414 for (x = 0; x < width; x++) {
420 *(p++) = icolor & 1 ? 255 : 0;
421 *(p++) = icolor & 2 ? 255 : 0;
422 *(p++) = icolor & 4 ? 255 : 0;
424 if (color_rest >= width) {
431 p0 += frame->linesize[0];
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) %
439 dgrad = GRADIENT_SIZE / width;
440 drgrad = GRADIENT_SIZE % width;
441 for (x = 0; x < width; x++) {
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;
447 grad >= 4 * 256 ? 0 :
448 grad >= 1 * 256 && grad < 3 * 256 ? 255 :
449 grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
452 grad >= 3 * 256 && grad < 5 * 256 ? 255 :
453 grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
456 if (rgrad >= GRADIENT_SIZE) {
458 rgrad -= GRADIENT_SIZE;
460 if (grad >= GRADIENT_SIZE)
461 grad -= GRADIENT_SIZE;
464 for (y = height / 8; y > 0; y--) {
465 memcpy(p+frame->linesize[0], p, 3 * width);
466 p += frame->linesize[0];
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);
478 for(x=0; x<test->nb_decimals; x++)
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);
495 static av_cold int test_init(AVFilterContext *ctx)
497 TestSourceContext *test = ctx->priv;
499 test->fill_picture_fn = test_fill_picture;
503 static int test_query_formats(AVFilterContext *ctx)
505 static const enum AVPixelFormat pix_fmts[] = {
506 AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
508 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
512 static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
515 .type = AVMEDIA_TYPE_VIDEO,
516 .request_frame = request_frame,
517 .config_props = config_props,
522 AVFilter avfilter_vsrc_testsrc = {
524 .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
525 .priv_size = sizeof(TestSourceContext),
526 .priv_class = &testsrc_class,
530 .query_formats = test_query_formats,
533 .outputs = avfilter_vsrc_testsrc_outputs,
536 #endif /* CONFIG_TESTSRC_FILTER */
538 #if CONFIG_RGBTESTSRC_FILTER
540 #define rgbtestsrc_options options
541 AVFILTER_DEFINE_CLASS(rgbtestsrc);
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,
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;
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;
579 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
581 TestSourceContext *test = ctx->priv;
582 int x, y, w = frame->width, h = frame->height;
584 for (y = 0; y < h; y++) {
585 for (x = 0; x < w; x++) {
587 int r = 0, g = 0, b = 0;
590 else if (3*y < 2*h) g = c;
593 rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
594 ctx->outputs[0]->format, test->rgba_map);
599 static av_cold int rgbtest_init(AVFilterContext *ctx)
601 TestSourceContext *test = ctx->priv;
604 test->fill_picture_fn = rgbtest_fill_picture;
608 static int rgbtest_query_formats(AVFilterContext *ctx)
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,
618 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
622 static int rgbtest_config_props(AVFilterLink *outlink)
624 TestSourceContext *test = outlink->src->priv;
626 ff_fill_rgba_map(test->rgba_map, outlink->format);
627 return config_props(outlink);
630 static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
633 .type = AVMEDIA_TYPE_VIDEO,
634 .request_frame = request_frame,
635 .config_props = rgbtest_config_props,
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,
648 .query_formats = rgbtest_query_formats,
652 .outputs = avfilter_vsrc_rgbtestsrc_outputs,
655 #endif /* CONFIG_RGBTESTSRC_FILTER */
657 #if CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER
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 */
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 */
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 */
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 */
687 static const uint8_t i_pixel[4] = { 0, 68, 130, 255 };
688 static const uint8_t q_pixel[4] = { 67, 0, 130, 255 };
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 };
701 static void inline draw_bar(TestSourceContext *test, const uint8_t *color,
702 unsigned x, unsigned y, unsigned w, unsigned h,
705 FFDrawColor draw_color;
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);
712 av_assert0(x + w <= test->w);
713 av_assert0(y + h <= test->h);
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);
720 static int smptebars_query_formats(AVFilterContext *ctx)
722 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
726 static int smptebars_config_props(AVFilterLink *outlink)
728 AVFilterContext *ctx = outlink->src;
729 TestSourceContext *test = ctx->priv;
731 ff_draw_init(&test->draw, outlink->format, 0);
733 return config_props(outlink);
736 static const AVFilterPad smptebars_outputs[] = {
739 .type = AVMEDIA_TYPE_VIDEO,
740 .request_frame = request_frame,
741 .config_props = smptebars_config_props,
746 #if CONFIG_SMPTEBARS_FILTER
748 #define smptebars_options options
749 AVFILTER_DEFINE_CLASS(smptebars);
751 static void smptebars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
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);
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;
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);
769 draw_bar(test, i_pixel, x, r_h + w_h, p_w, p_h, picref);
771 draw_bar(test, white, x, r_h + w_h, p_w, p_h, picref);
773 draw_bar(test, q_pixel, x, r_h + w_h, p_w, p_h, picref);
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);
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);
781 draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
783 draw_bar(test, pos4ire, x, r_h + w_h, tmp, p_h, picref);
785 draw_bar(test, black, x, r_h + w_h, test->w - x, p_h, picref);
788 static av_cold int smptebars_init(AVFilterContext *ctx)
790 TestSourceContext *test = ctx->priv;
792 test->fill_picture_fn = smptebars_fill_picture;
797 AVFilter avfilter_vsrc_smptebars = {
799 .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
800 .priv_size = sizeof(TestSourceContext),
801 .init = smptebars_init,
804 .query_formats = smptebars_query_formats,
806 .outputs = smptebars_outputs,
807 .priv_class = &smptebars_class,
810 #endif /* CONFIG_SMPTEBARS_FILTER */
812 #if CONFIG_SMPTEHDBARS_FILTER
814 #define smptehdbars_options options
815 AVFILTER_DEFINE_CLASS(smptehdbars);
817 static void smptehdbars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
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);
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);
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);
833 draw_bar(test, gray40, x, 0, test->w - x, r_h, picref);
835 r_h = FFALIGN(test->h / 12, 1 << pixdesc->log2_chroma_h);
836 draw_bar(test, cyan, 0, y, d_w, r_h, picref);
838 draw_bar(test, i_pixel, x, y, r_w, r_h, picref);
841 draw_bar(test, rainbow[0], x, y, tmp, r_h, picref);
844 draw_bar(test, blue, x, y, test->w - x, r_h, picref);
846 draw_bar(test, yellow, 0, y, d_w, r_h, picref);
848 draw_bar(test, q_pixel, x, y, r_w, r_h, picref);
851 for (i = 0; i < tmp; i += 1 << pixdesc->log2_chroma_w) {
852 uint8_t yramp[4] = {0};
856 yramp[2] = i * 255 / tmp;
859 draw_bar(test, yramp, x, y, 1 << pixdesc->log2_chroma_w, r_h, picref);
860 x += 1 << pixdesc->log2_chroma_w;
862 draw_bar(test, red, x, y, test->w - x, r_h, picref);
864 draw_bar(test, gray15, 0, y, d_w, test->h - y, picref);
866 tmp = FFALIGN(r_w * 3 / 2, 1 << pixdesc->log2_chroma_w);
867 draw_bar(test, black0, x, y, tmp, test->h - y, picref);
869 tmp = FFALIGN(r_w * 2, 1 << pixdesc->log2_chroma_w);
870 draw_bar(test, white, x, y, tmp, test->h - y, picref);
872 tmp = FFALIGN(r_w * 5 / 6, 1 << pixdesc->log2_chroma_w);
873 draw_bar(test, black0, x, y, tmp, test->h - y, picref);
875 tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
876 draw_bar(test, neg2, x, y, tmp, test->h - y, picref);
878 draw_bar(test, black0, x, y, tmp, test->h - y, picref);
880 draw_bar(test, black2, x, y, tmp, test->h - y, picref);
882 draw_bar(test, black0, x, y, tmp, test->h - y, picref);
884 draw_bar(test, black4, x, y, tmp, test->h - y, picref);
887 draw_bar(test, black0, x, y, r_w, test->h - y, picref);
889 draw_bar(test, gray15, x, y, test->w - x, test->h - y, picref);
892 static av_cold int smptehdbars_init(AVFilterContext *ctx)
894 TestSourceContext *test = ctx->priv;
896 test->fill_picture_fn = smptehdbars_fill_picture;
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,
908 .query_formats = smptebars_query_formats,
910 .outputs = smptebars_outputs,
911 .priv_class = &smptehdbars_class,
914 #endif /* CONFIG_SMPTEHDBARS_FILTER */
915 #endif /* CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER */