2 * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
3 * Copyright (c) 2011 Stefano Sabatini
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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/common.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/intreadwrite.h"
39 #include "libavutil/parseutils.h"
45 typedef struct TestSourceContext {
48 unsigned int nb_frame;
49 AVRational time_base, frame_rate;
51 char *size; ///< video frame size
52 char *rate; ///< video frame rate
53 char *duration; ///< total duration of the generated video
54 AVRational sar; ///< sample aspect ratio
56 void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
58 /* only used by rgbtest */
62 #define OFFSET(x) offsetof(TestSourceContext, x)
63 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
65 static const AVOption testsrc_options[] = {
66 { "size", "set video size", OFFSET(size), AV_OPT_TYPE_STRING, {.str = "320x240"}, .flags = FLAGS },
67 { "s", "set video size", OFFSET(size), AV_OPT_TYPE_STRING, {.str = "320x240"}, .flags = FLAGS },
68 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, .flags = FLAGS },
69 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, .flags = FLAGS },
70 { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
71 { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl = 1}, 0, INT_MAX, FLAGS },
75 static av_cold int init_common(AVFilterContext *ctx)
77 TestSourceContext *test = ctx->priv;
78 int64_t duration = -1;
81 if ((ret = av_parse_video_size(&test->w, &test->h, test->size)) < 0) {
82 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", test->size);
86 if ((ret = av_parse_video_rate(&test->frame_rate, test->rate)) < 0) {
87 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
91 if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
92 av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
96 test->time_base = av_inv_q(test->frame_rate);
97 test->max_pts = duration >= 0 ?
98 av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
102 av_log(ctx, AV_LOG_DEBUG, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
103 test->w, test->h, test->frame_rate.num, test->frame_rate.den,
104 duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
105 test->sar.num, test->sar.den);
109 static int config_props(AVFilterLink *outlink)
111 TestSourceContext *test = outlink->src->priv;
113 outlink->w = test->w;
114 outlink->h = test->h;
115 outlink->sample_aspect_ratio = test->sar;
116 outlink->frame_rate = test->frame_rate;
117 outlink->time_base = test->time_base;
122 static int request_frame(AVFilterLink *outlink)
124 TestSourceContext *test = outlink->src->priv;
127 if (test->max_pts >= 0 && test->pts > test->max_pts)
129 frame = ff_get_video_buffer(outlink, test->w, test->h);
131 return AVERROR(ENOMEM);
133 frame->pts = test->pts++;
134 frame->key_frame = 1;
135 frame->interlaced_frame = 0;
136 frame->pict_type = AV_PICTURE_TYPE_I;
137 frame->sample_aspect_ratio = test->sar;
139 test->fill_picture_fn(outlink->src, frame);
141 return ff_filter_frame(outlink, frame);
144 #if CONFIG_TESTSRC_FILTER
146 static const char *testsrc_get_name(void *ctx)
151 static const AVClass testsrc_class = {
152 .class_name = "TestSourceContext",
153 .item_name = testsrc_get_name,
154 .option = testsrc_options,
158 * Fill a rectangle with value val.
160 * @param val the RGB value to set
161 * @param dst pointer to the destination buffer to fill
162 * @param dst_linesize linesize of destination
163 * @param segment_width width of the segment
164 * @param x horizontal coordinate where to draw the rectangle in the destination buffer
165 * @param y horizontal coordinate where to draw the rectangle in the destination buffer
166 * @param w width of the rectangle to draw, expressed as a number of segment_width units
167 * @param h height of the rectangle to draw, expressed as a number of segment_width units
169 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
170 unsigned x, unsigned y, unsigned w, unsigned h)
175 dst += segment_width * (step * x + y * dst_linesize);
176 w *= segment_width * step;
178 for (i = 0; i < h; i++) {
184 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
185 unsigned segment_width)
190 #define LEFT_TOP_VBAR 8
191 #define LEFT_BOT_VBAR 16
192 #define RIGHT_TOP_VBAR 32
193 #define RIGHT_BOT_VBAR 64
197 { 1, 0, 5, 1 }, /* TOP_HBAR */
198 { 1, 6, 5, 1 }, /* MID_HBAR */
199 { 1, 12, 5, 1 }, /* BOT_HBAR */
200 { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
201 { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
202 { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
203 { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
205 static const unsigned char masks[10] = {
206 /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
207 /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
208 /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
209 /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
210 /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
211 /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
212 /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
213 /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
214 /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
215 /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
217 unsigned mask = masks[digit];
220 draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
221 for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
223 draw_rectangle(255, dst, dst_linesize, segment_width,
224 segments[i].x, segments[i].y, segments[i].w, segments[i].h);
227 #define GRADIENT_SIZE (6 * 256)
229 static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
231 TestSourceContext *test = ctx->priv;
234 int color, color_rest;
238 int dquad_x, dquad_y;
239 int grad, dgrad, rgrad, drgrad;
243 uint8_t *data = frame->data[0];
244 int width = frame->width;
245 int height = frame->height;
247 /* draw colored bars and circle */
248 radius = (width + height) / 4;
249 quad0 = width * width / 4 + height * height / 4 - radius * radius;
250 dquad_y = 1 - height;
252 for (y = 0; y < height; y++) {
258 for (x = 0; x < width; x++) {
264 *(p++) = icolor & 1 ? 255 : 0;
265 *(p++) = icolor & 2 ? 255 : 0;
266 *(p++) = icolor & 4 ? 255 : 0;
268 if (color_rest >= width) {
275 p0 += frame->linesize[0];
278 /* draw sliding color line */
279 p = data + frame->linesize[0] * height * 3/4;
280 grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
283 dgrad = GRADIENT_SIZE / width;
284 drgrad = GRADIENT_SIZE % width;
285 for (x = 0; x < width; x++) {
287 grad < 256 || grad >= 5 * 256 ? 255 :
288 grad >= 2 * 256 && grad < 4 * 256 ? 0 :
289 grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
291 grad >= 4 * 256 ? 0 :
292 grad >= 1 * 256 && grad < 3 * 256 ? 255 :
293 grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
296 grad >= 3 * 256 && grad < 5 * 256 ? 255 :
297 grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
300 if (rgrad >= GRADIENT_SIZE) {
302 rgrad -= GRADIENT_SIZE;
304 if (grad >= GRADIENT_SIZE)
305 grad -= GRADIENT_SIZE;
307 for (y = height / 8; y > 0; y--) {
308 memcpy(p, p - frame->linesize[0], 3 * width);
309 p += frame->linesize[0];
313 seg_size = width / 80;
314 if (seg_size >= 1 && height >= 13 * seg_size) {
315 second = test->nb_frame * test->time_base.num / test->time_base.den;
316 x = width - (width - seg_size * 64) / 2;
317 y = (height - seg_size * 13) / 2;
318 p = data + (x*3 + y * frame->linesize[0]);
319 for (i = 0; i < 8; i++) {
320 p -= 3 * 8 * seg_size;
321 draw_digit(second % 10, p, frame->linesize[0], seg_size);
329 static av_cold int test_init(AVFilterContext *ctx)
331 TestSourceContext *test = ctx->priv;
333 test->fill_picture_fn = test_fill_picture;
334 return init_common(ctx);
337 static int test_query_formats(AVFilterContext *ctx)
339 static const enum AVPixelFormat pix_fmts[] = {
340 AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
342 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
346 static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
349 .type = AVMEDIA_TYPE_VIDEO,
350 .request_frame = request_frame,
351 .config_props = config_props,
356 AVFilter ff_vsrc_testsrc = {
358 .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
359 .priv_size = sizeof(TestSourceContext),
360 .priv_class = &testsrc_class,
363 .query_formats = test_query_formats,
367 .outputs = avfilter_vsrc_testsrc_outputs,
370 #endif /* CONFIG_TESTSRC_FILTER */
372 #if CONFIG_RGBTESTSRC_FILTER
374 static const char *rgbtestsrc_get_name(void *ctx)
379 static const AVClass rgbtestsrc_class = {
380 .class_name = "RGBTestSourceContext",
381 .item_name = rgbtestsrc_get_name,
382 .option = testsrc_options,
390 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
391 int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
398 case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
399 case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
400 case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
401 case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
402 case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
403 case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
404 case AV_PIX_FMT_RGB24:
405 case AV_PIX_FMT_BGR24:
406 v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
407 p = dst + 3*x + y*dst_linesize;
410 case AV_PIX_FMT_RGBA:
411 case AV_PIX_FMT_BGRA:
412 case AV_PIX_FMT_ARGB:
413 case AV_PIX_FMT_ABGR:
414 v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
415 p = dst + 4*x + y*dst_linesize;
421 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
423 TestSourceContext *test = ctx->priv;
424 int x, y, w = frame->width, h = frame->height;
426 for (y = 0; y < h; y++) {
427 for (x = 0; x < w; x++) {
429 int r = 0, g = 0, b = 0;
432 else if (3*y < 2*h) g = c;
435 rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
436 ctx->outputs[0]->format, test->rgba_map);
441 static av_cold int rgbtest_init(AVFilterContext *ctx)
443 TestSourceContext *test = ctx->priv;
445 test->fill_picture_fn = rgbtest_fill_picture;
446 return init_common(ctx);
449 static int rgbtest_query_formats(AVFilterContext *ctx)
451 static const enum AVPixelFormat pix_fmts[] = {
452 AV_PIX_FMT_RGBA, AV_PIX_FMT_ARGB, AV_PIX_FMT_BGRA, AV_PIX_FMT_ABGR,
453 AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
454 AV_PIX_FMT_RGB444, AV_PIX_FMT_BGR444,
455 AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565,
456 AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555,
459 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
463 static int rgbtest_config_props(AVFilterLink *outlink)
465 TestSourceContext *test = outlink->src->priv;
467 switch (outlink->format) {
468 case AV_PIX_FMT_ARGB: test->rgba_map[A] = 0; test->rgba_map[R] = 1; test->rgba_map[G] = 2; test->rgba_map[B] = 3; break;
469 case AV_PIX_FMT_ABGR: test->rgba_map[A] = 0; test->rgba_map[B] = 1; test->rgba_map[G] = 2; test->rgba_map[R] = 3; break;
470 case AV_PIX_FMT_RGBA:
471 case AV_PIX_FMT_RGB24: test->rgba_map[R] = 0; test->rgba_map[G] = 1; test->rgba_map[B] = 2; test->rgba_map[A] = 3; break;
472 case AV_PIX_FMT_BGRA:
473 case AV_PIX_FMT_BGR24: test->rgba_map[B] = 0; test->rgba_map[G] = 1; test->rgba_map[R] = 2; test->rgba_map[A] = 3; break;
476 return config_props(outlink);
479 static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
482 .type = AVMEDIA_TYPE_VIDEO,
483 .request_frame = request_frame,
484 .config_props = rgbtest_config_props,
489 AVFilter ff_vsrc_rgbtestsrc = {
490 .name = "rgbtestsrc",
491 .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
492 .priv_size = sizeof(TestSourceContext),
493 .priv_class = &rgbtestsrc_class,
494 .init = rgbtest_init,
496 .query_formats = rgbtest_query_formats,
500 .outputs = avfilter_vsrc_rgbtestsrc_outputs,
503 #endif /* CONFIG_RGBTESTSRC_FILTER */