2 * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
3 * Copyright (c) 2011 Stefano Sabatini
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * testsrc is based on the test pattern generator demuxer by Nicolas George:
27 * http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2007-October/037845.html
29 * rgbtestsrc is ported from MPlayer libmpcodecs/vf_rgbtest.c by
30 * Michael Niedermayer.
35 #include "libavutil/opt.h"
36 #include "libavutil/intreadwrite.h"
37 #include "libavutil/parseutils.h"
43 unsigned int nb_frame;
46 char *size; ///< video frame size
47 char *rate; ///< video frame rate
48 char *duration; ///< total duration of the generated video
49 AVRational sar; ///< sample aspect ratio
52 void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
54 /* only used by rgbtest */
58 #define OFFSET(x) offsetof(TestSourceContext, x)
60 static const AVOption testsrc_options[]= {
61 { "size", "set video size", OFFSET(size), AV_OPT_TYPE_STRING, {.str = "320x240"}, 0, 0 },
62 { "s", "set video size", OFFSET(size), AV_OPT_TYPE_STRING, {.str = "320x240"}, 0, 0 },
63 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
64 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
65 { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
66 { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
67 { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1}, 0, INT_MAX },
68 { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0}, INT_MIN, INT_MAX },
69 { "n", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0}, INT_MIN, INT_MAX },
73 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
75 TestSourceContext *test = ctx->priv;
76 AVRational frame_rate_q;
77 int64_t duration = -1;
80 av_opt_set_defaults(test);
82 if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
83 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
87 if ((ret = av_parse_video_size(&test->w, &test->h, test->size)) < 0) {
88 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", test->size);
92 if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0 ||
93 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
94 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
98 if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
99 av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
103 if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
104 av_log(ctx, AV_LOG_WARNING,
105 "Option 'decimals' is ignored with source '%s'\n",
109 test->time_base.num = frame_rate_q.den;
110 test->time_base.den = frame_rate_q.num;
111 test->max_pts = duration >= 0 ?
112 av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
116 av_log(ctx, AV_LOG_INFO, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
117 test->w, test->h, frame_rate_q.num, frame_rate_q.den,
118 duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
119 test->sar.num, test->sar.den);
123 static int config_props(AVFilterLink *outlink)
125 TestSourceContext *test = outlink->src->priv;
127 outlink->w = test->w;
128 outlink->h = test->h;
129 outlink->sample_aspect_ratio = test->sar;
130 outlink->time_base = test->time_base;
135 static int request_frame(AVFilterLink *outlink)
137 TestSourceContext *test = outlink->src->priv;
138 AVFilterBufferRef *picref;
140 if (test->max_pts >= 0 && test->pts >= test->max_pts)
142 picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
144 picref->pts = test->pts++;
146 picref->video->key_frame = 1;
147 picref->video->interlaced = 0;
148 picref->video->pict_type = AV_PICTURE_TYPE_I;
149 picref->video->sample_aspect_ratio = test->sar;
150 test->fill_picture_fn(outlink->src, picref);
153 avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
154 avfilter_draw_slice(outlink, 0, picref->video->h, 1);
155 avfilter_end_frame(outlink);
156 avfilter_unref_buffer(picref);
161 #if CONFIG_NULLSRC_FILTER
163 static const char *nullsrc_get_name(void *ctx)
168 static const AVClass nullsrc_class = {
169 .class_name = "NullSourceContext",
170 .item_name = nullsrc_get_name,
171 .option = testsrc_options,
174 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref) { }
176 static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args, void *opaque)
178 TestSourceContext *test = ctx->priv;
180 test->class = &nullsrc_class;
181 test->fill_picture_fn = nullsrc_fill_picture;
182 return init(ctx, args, opaque);
185 AVFilter avfilter_vsrc_nullsrc = {
187 .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
188 .init = nullsrc_init,
189 .priv_size = sizeof(TestSourceContext),
191 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
192 .outputs = (const AVFilterPad[]) {{ .name = "default",
193 .type = AVMEDIA_TYPE_VIDEO,
194 .request_frame = request_frame,
195 .config_props = config_props, },
199 #endif /* CONFIG_NULLSRC_FILTER */
201 #if CONFIG_TESTSRC_FILTER
203 static const char *testsrc_get_name(void *ctx)
208 static const AVClass testsrc_class = {
209 .class_name = "TestSourceContext",
210 .item_name = testsrc_get_name,
211 .option = testsrc_options,
215 * Fill a rectangle with value val.
217 * @param val the RGB value to set
218 * @param dst pointer to the destination buffer to fill
219 * @param dst_linesize linesize of destination
220 * @param segment_width width of the segment
221 * @param x horizontal coordinate where to draw the rectangle in the destination buffer
222 * @param y horizontal coordinate where to draw the rectangle in the destination buffer
223 * @param w width of the rectangle to draw, expressed as a number of segment_width units
224 * @param h height of the rectangle to draw, expressed as a number of segment_width units
226 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
227 unsigned x, unsigned y, unsigned w, unsigned h)
232 dst += segment_width * (step * x + y * dst_linesize);
233 w *= segment_width * step;
235 for (i = 0; i < h; i++) {
241 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
242 unsigned segment_width)
247 #define LEFT_TOP_VBAR 8
248 #define LEFT_BOT_VBAR 16
249 #define RIGHT_TOP_VBAR 32
250 #define RIGHT_BOT_VBAR 64
254 { 1, 0, 5, 1 }, /* TOP_HBAR */
255 { 1, 6, 5, 1 }, /* MID_HBAR */
256 { 1, 12, 5, 1 }, /* BOT_HBAR */
257 { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
258 { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
259 { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
260 { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
262 static const unsigned char masks[10] = {
263 /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
264 /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
265 /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
266 /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
267 /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
268 /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
269 /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
270 /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
271 /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
272 /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
274 unsigned mask = masks[digit];
277 draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
278 for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
280 draw_rectangle(255, dst, dst_linesize, segment_width,
281 segments[i].x, segments[i].y, segments[i].w, segments[i].h);
284 #define GRADIENT_SIZE (6 * 256)
286 static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
288 TestSourceContext *test = ctx->priv;
291 int color, color_rest;
295 int dquad_x, dquad_y;
296 int grad, dgrad, rgrad, drgrad;
300 uint8_t *data = picref->data[0];
301 int width = picref->video->w;
302 int height = picref->video->h;
304 /* draw colored bars and circle */
305 radius = (width + height) / 4;
306 quad0 = width * width / 4 + height * height / 4 - radius * radius;
307 dquad_y = 1 - height;
309 for (y = 0; y < height; y++) {
315 for (x = 0; x < width; x++) {
321 *(p++) = icolor & 1 ? 255 : 0;
322 *(p++) = icolor & 2 ? 255 : 0;
323 *(p++) = icolor & 4 ? 255 : 0;
325 if (color_rest >= width) {
332 p0 += picref->linesize[0];
335 /* draw sliding color line */
336 p0 = p = data + picref->linesize[0] * height * 3/4;
337 grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
340 dgrad = GRADIENT_SIZE / width;
341 drgrad = GRADIENT_SIZE % width;
342 for (x = 0; x < width; x++) {
344 grad < 256 || grad >= 5 * 256 ? 255 :
345 grad >= 2 * 256 && grad < 4 * 256 ? 0 :
346 grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
348 grad >= 4 * 256 ? 0 :
349 grad >= 1 * 256 && grad < 3 * 256 ? 255 :
350 grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
353 grad >= 3 * 256 && grad < 5 * 256 ? 255 :
354 grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
357 if (rgrad >= GRADIENT_SIZE) {
359 rgrad -= GRADIENT_SIZE;
361 if (grad >= GRADIENT_SIZE)
362 grad -= GRADIENT_SIZE;
365 for (y = height / 8; y > 0; y--) {
366 memcpy(p+picref->linesize[0], p, 3 * width);
367 p += picref->linesize[0];
371 seg_size = width / 80;
372 if (seg_size >= 1 && height >= 13 * seg_size) {
373 double time = av_q2d(test->time_base) * test->nb_frame *
374 pow(10, test->nb_decimals);
378 x = width - (width - seg_size * 64) / 2;
379 y = (height - seg_size * 13) / 2;
380 p = data + (x*3 + y * picref->linesize[0]);
381 for (i = 0; i < 8; i++) {
382 p -= 3 * 8 * seg_size;
383 draw_digit(second % 10, p, picref->linesize[0], seg_size);
391 static av_cold int test_init(AVFilterContext *ctx, const char *args, void *opaque)
393 TestSourceContext *test = ctx->priv;
395 test->class = &testsrc_class;
396 test->fill_picture_fn = test_fill_picture;
397 return init(ctx, args, opaque);
400 static int test_query_formats(AVFilterContext *ctx)
402 static const enum PixelFormat pix_fmts[] = {
403 PIX_FMT_RGB24, PIX_FMT_NONE
405 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
409 AVFilter avfilter_vsrc_testsrc = {
411 .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
412 .priv_size = sizeof(TestSourceContext),
415 .query_formats = test_query_formats,
417 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
419 .outputs = (const AVFilterPad[]) {{ .name = "default",
420 .type = AVMEDIA_TYPE_VIDEO,
421 .request_frame = request_frame,
422 .config_props = config_props, },
426 #endif /* CONFIG_TESTSRC_FILTER */
428 #if CONFIG_RGBTESTSRC_FILTER
430 static const char *rgbtestsrc_get_name(void *ctx)
435 static const AVClass rgbtestsrc_class = {
436 .class_name = "RGBTestSourceContext",
437 .item_name = rgbtestsrc_get_name,
438 .option = testsrc_options,
446 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
447 int x, int y, int r, int g, int b, enum PixelFormat fmt,
454 case PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
455 case PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
456 case PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
457 case PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
458 case PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
459 case PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
462 v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
463 p = dst + 3*x + y*dst_linesize;
470 v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
471 p = dst + 4*x + y*dst_linesize;
477 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
479 TestSourceContext *test = ctx->priv;
480 int x, y, w = picref->video->w, h = picref->video->h;
482 for (y = 0; y < h; y++) {
483 for (x = 0; x < picref->video->w; x++) {
485 int r = 0, g = 0, b = 0;
488 else if (3*y < 2*h) g = c;
491 rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
492 ctx->outputs[0]->format, test->rgba_map);
497 static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args, void *opaque)
499 TestSourceContext *test = ctx->priv;
501 test->class = &rgbtestsrc_class;
502 test->fill_picture_fn = rgbtest_fill_picture;
503 return init(ctx, args, opaque);
506 static int rgbtest_query_formats(AVFilterContext *ctx)
508 static const enum PixelFormat pix_fmts[] = {
509 PIX_FMT_RGBA, PIX_FMT_ARGB, PIX_FMT_BGRA, PIX_FMT_ABGR,
510 PIX_FMT_BGR24, PIX_FMT_RGB24,
511 PIX_FMT_RGB444, PIX_FMT_BGR444,
512 PIX_FMT_RGB565, PIX_FMT_BGR565,
513 PIX_FMT_RGB555, PIX_FMT_BGR555,
516 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
520 static int rgbtest_config_props(AVFilterLink *outlink)
522 TestSourceContext *test = outlink->src->priv;
524 switch (outlink->format) {
525 case PIX_FMT_ARGB: test->rgba_map[A] = 0; test->rgba_map[R] = 1; test->rgba_map[G] = 2; test->rgba_map[B] = 3; break;
526 case PIX_FMT_ABGR: test->rgba_map[A] = 0; test->rgba_map[B] = 1; test->rgba_map[G] = 2; test->rgba_map[R] = 3; break;
528 case PIX_FMT_RGB24: test->rgba_map[R] = 0; test->rgba_map[G] = 1; test->rgba_map[B] = 2; test->rgba_map[A] = 3; break;
530 case PIX_FMT_BGR24: test->rgba_map[B] = 0; test->rgba_map[G] = 1; test->rgba_map[R] = 2; test->rgba_map[A] = 3; break;
533 return config_props(outlink);
536 AVFilter avfilter_vsrc_rgbtestsrc = {
537 .name = "rgbtestsrc",
538 .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
539 .priv_size = sizeof(TestSourceContext),
540 .init = rgbtest_init,
542 .query_formats = rgbtest_query_formats,
544 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
546 .outputs = (const AVFilterPad[]) {{ .name = "default",
547 .type = AVMEDIA_TYPE_VIDEO,
548 .request_frame = request_frame,
549 .config_props = rgbtest_config_props, },
553 #endif /* CONFIG_RGBTESTSRC_FILTER */