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"
46 unsigned int nb_frame;
49 char *rate; ///< video frame rate
50 char *duration; ///< total duration of the generated video
51 AVRational sar; ///< sample aspect ratio
54 void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
56 /* only used by rgbtest */
60 #define OFFSET(x) offsetof(TestSourceContext, x)
62 static const AVOption testsrc_options[]= {
63 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0 },
64 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0 },
65 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
66 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
67 { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
68 { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
69 { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1}, 0, INT_MAX },
70 { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0}, INT_MIN, INT_MAX },
71 { "n", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0}, INT_MIN, INT_MAX },
75 static av_cold int init(AVFilterContext *ctx, const char *args)
77 TestSourceContext *test = ctx->priv;
78 AVRational frame_rate_q;
79 int64_t duration = -1;
82 av_opt_set_defaults(test);
84 if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
85 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
89 if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0) {
90 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
93 av_freep(&test->rate);
95 if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
96 av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
99 av_freep(&test->duration);
101 if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
102 av_log(ctx, AV_LOG_WARNING,
103 "Option 'decimals' is ignored with source '%s'\n",
107 test->time_base.num = frame_rate_q.den;
108 test->time_base.den = frame_rate_q.num;
109 test->max_pts = duration >= 0 ?
110 av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
114 av_log(ctx, AV_LOG_INFO, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
115 test->w, test->h, frame_rate_q.num, frame_rate_q.den,
116 duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
117 test->sar.num, test->sar.den);
121 static int config_props(AVFilterLink *outlink)
123 TestSourceContext *test = outlink->src->priv;
125 outlink->w = test->w;
126 outlink->h = test->h;
127 outlink->sample_aspect_ratio = test->sar;
128 outlink->time_base = test->time_base;
133 static int request_frame(AVFilterLink *outlink)
135 TestSourceContext *test = outlink->src->priv;
136 AVFilterBufferRef *picref;
138 if (test->max_pts >= 0 && test->pts >= test->max_pts)
140 picref = ff_get_video_buffer(outlink, AV_PERM_WRITE, test->w, test->h);
141 picref->pts = test->pts++;
143 picref->video->key_frame = 1;
144 picref->video->interlaced = 0;
145 picref->video->pict_type = AV_PICTURE_TYPE_I;
146 picref->video->sample_aspect_ratio = test->sar;
147 test->fill_picture_fn(outlink->src, picref);
150 ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
151 ff_draw_slice(outlink, 0, picref->video->h, 1);
152 ff_end_frame(outlink);
153 avfilter_unref_buffer(picref);
158 #if CONFIG_NULLSRC_FILTER
160 static const AVClass nullsrc_class = {
161 .class_name = "nullsrc",
162 .item_name = av_default_item_name,
163 .option = testsrc_options,
164 .version = LIBAVUTIL_VERSION_INT,
165 .category = AV_CLASS_CATEGORY_FILTER,
168 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref) { }
170 static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args)
172 TestSourceContext *test = ctx->priv;
174 test->class = &nullsrc_class;
175 test->fill_picture_fn = nullsrc_fill_picture;
176 return init(ctx, args);
179 AVFilter avfilter_vsrc_nullsrc = {
181 .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
182 .init = nullsrc_init,
183 .priv_size = sizeof(TestSourceContext),
185 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
186 .outputs = (const AVFilterPad[]) {{ .name = "default",
187 .type = AVMEDIA_TYPE_VIDEO,
188 .request_frame = request_frame,
189 .config_props = config_props, },
193 #endif /* CONFIG_NULLSRC_FILTER */
195 #if CONFIG_TESTSRC_FILTER
197 AVFILTER_DEFINE_CLASS(testsrc);
200 * Fill a rectangle with value val.
202 * @param val the RGB value to set
203 * @param dst pointer to the destination buffer to fill
204 * @param dst_linesize linesize of destination
205 * @param segment_width width of the segment
206 * @param x horizontal coordinate where to draw the rectangle in the destination buffer
207 * @param y horizontal coordinate where to draw the rectangle in the destination buffer
208 * @param w width of the rectangle to draw, expressed as a number of segment_width units
209 * @param h height of the rectangle to draw, expressed as a number of segment_width units
211 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
212 unsigned x, unsigned y, unsigned w, unsigned h)
217 dst += segment_width * (step * x + y * dst_linesize);
218 w *= segment_width * step;
220 for (i = 0; i < h; i++) {
226 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
227 unsigned segment_width)
232 #define LEFT_TOP_VBAR 8
233 #define LEFT_BOT_VBAR 16
234 #define RIGHT_TOP_VBAR 32
235 #define RIGHT_BOT_VBAR 64
239 { 1, 0, 5, 1 }, /* TOP_HBAR */
240 { 1, 6, 5, 1 }, /* MID_HBAR */
241 { 1, 12, 5, 1 }, /* BOT_HBAR */
242 { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
243 { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
244 { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
245 { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
247 static const unsigned char masks[10] = {
248 /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
249 /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
250 /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
251 /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
252 /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
253 /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
254 /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
255 /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
256 /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
257 /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
259 unsigned mask = masks[digit];
262 draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
263 for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
265 draw_rectangle(255, dst, dst_linesize, segment_width,
266 segments[i].x, segments[i].y, segments[i].w, segments[i].h);
269 #define GRADIENT_SIZE (6 * 256)
271 static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
273 TestSourceContext *test = ctx->priv;
276 int color, color_rest;
280 int dquad_x, dquad_y;
281 int grad, dgrad, rgrad, drgrad;
285 uint8_t *data = picref->data[0];
286 int width = picref->video->w;
287 int height = picref->video->h;
289 /* draw colored bars and circle */
290 radius = (width + height) / 4;
291 quad0 = width * width / 4 + height * height / 4 - radius * radius;
292 dquad_y = 1 - height;
294 for (y = 0; y < height; y++) {
300 for (x = 0; x < width; x++) {
306 *(p++) = icolor & 1 ? 255 : 0;
307 *(p++) = icolor & 2 ? 255 : 0;
308 *(p++) = icolor & 4 ? 255 : 0;
310 if (color_rest >= width) {
317 p0 += picref->linesize[0];
320 /* draw sliding color line */
321 p0 = p = data + picref->linesize[0] * height * 3/4;
322 grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
325 dgrad = GRADIENT_SIZE / width;
326 drgrad = GRADIENT_SIZE % width;
327 for (x = 0; x < width; x++) {
329 grad < 256 || grad >= 5 * 256 ? 255 :
330 grad >= 2 * 256 && grad < 4 * 256 ? 0 :
331 grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
333 grad >= 4 * 256 ? 0 :
334 grad >= 1 * 256 && grad < 3 * 256 ? 255 :
335 grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
338 grad >= 3 * 256 && grad < 5 * 256 ? 255 :
339 grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
342 if (rgrad >= GRADIENT_SIZE) {
344 rgrad -= GRADIENT_SIZE;
346 if (grad >= GRADIENT_SIZE)
347 grad -= GRADIENT_SIZE;
350 for (y = height / 8; y > 0; y--) {
351 memcpy(p+picref->linesize[0], p, 3 * width);
352 p += picref->linesize[0];
356 seg_size = width / 80;
357 if (seg_size >= 1 && height >= 13 * seg_size) {
358 double time = av_q2d(test->time_base) * test->nb_frame *
359 pow(10, test->nb_decimals);
363 x = width - (width - seg_size * 64) / 2;
364 y = (height - seg_size * 13) / 2;
365 p = data + (x*3 + y * picref->linesize[0]);
366 for (i = 0; i < 8; i++) {
367 p -= 3 * 8 * seg_size;
368 draw_digit(second % 10, p, picref->linesize[0], seg_size);
376 static av_cold int test_init(AVFilterContext *ctx, const char *args)
378 TestSourceContext *test = ctx->priv;
380 test->class = &testsrc_class;
381 test->fill_picture_fn = test_fill_picture;
382 return init(ctx, args);
385 static int test_query_formats(AVFilterContext *ctx)
387 static const enum PixelFormat pix_fmts[] = {
388 PIX_FMT_RGB24, PIX_FMT_NONE
390 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
394 AVFilter avfilter_vsrc_testsrc = {
396 .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
397 .priv_size = sizeof(TestSourceContext),
400 .query_formats = test_query_formats,
402 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
404 .outputs = (const AVFilterPad[]) {{ .name = "default",
405 .type = AVMEDIA_TYPE_VIDEO,
406 .request_frame = request_frame,
407 .config_props = config_props, },
411 #endif /* CONFIG_TESTSRC_FILTER */
413 #if CONFIG_RGBTESTSRC_FILTER
415 static const AVClass rgbtestsrc_class = {
416 .class_name = "rgbtestsrc",
417 .item_name = av_default_item_name,
418 .option = testsrc_options,
419 .version = LIBAVUTIL_VERSION_INT,
420 .category = AV_CLASS_CATEGORY_FILTER,
428 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
429 int x, int y, int r, int g, int b, enum PixelFormat fmt,
436 case PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
437 case PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
438 case PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
439 case PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
440 case PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
441 case PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
444 v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
445 p = dst + 3*x + y*dst_linesize;
452 v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
453 p = dst + 4*x + y*dst_linesize;
459 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
461 TestSourceContext *test = ctx->priv;
462 int x, y, w = picref->video->w, h = picref->video->h;
464 for (y = 0; y < h; y++) {
465 for (x = 0; x < picref->video->w; x++) {
467 int r = 0, g = 0, b = 0;
470 else if (3*y < 2*h) g = c;
473 rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
474 ctx->outputs[0]->format, test->rgba_map);
479 static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
481 TestSourceContext *test = ctx->priv;
483 test->class = &rgbtestsrc_class;
484 test->fill_picture_fn = rgbtest_fill_picture;
485 return init(ctx, args);
488 static int rgbtest_query_formats(AVFilterContext *ctx)
490 static const enum PixelFormat pix_fmts[] = {
491 PIX_FMT_RGBA, PIX_FMT_ARGB, PIX_FMT_BGRA, PIX_FMT_ABGR,
492 PIX_FMT_BGR24, PIX_FMT_RGB24,
493 PIX_FMT_RGB444, PIX_FMT_BGR444,
494 PIX_FMT_RGB565, PIX_FMT_BGR565,
495 PIX_FMT_RGB555, PIX_FMT_BGR555,
498 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
502 static int rgbtest_config_props(AVFilterLink *outlink)
504 TestSourceContext *test = outlink->src->priv;
506 switch (outlink->format) {
507 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;
508 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;
510 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;
512 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;
515 return config_props(outlink);
518 AVFilter avfilter_vsrc_rgbtestsrc = {
519 .name = "rgbtestsrc",
520 .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
521 .priv_size = sizeof(TestSourceContext),
522 .init = rgbtest_init,
524 .query_formats = rgbtest_query_formats,
526 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
528 .outputs = (const AVFilterPad[]) {{ .name = "default",
529 .type = AVMEDIA_TYPE_VIDEO,
530 .request_frame = request_frame,
531 .config_props = rgbtest_config_props, },
535 #endif /* CONFIG_RGBTESTSRC_FILTER */