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
24 * Based on the test pattern generator demuxer by Nicolas George:
25 * http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2007-October/037845.html
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
38 unsigned int nb_frame;
41 char *size; ///< video frame size
42 char *rate; ///< video frame rate
43 char *duration; ///< total duration of the generated video
44 AVRational sar; ///< sample aspect ratio
46 void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
49 #define OFFSET(x) offsetof(TestSourceContext, x)
51 static const AVOption testsrc_options[] = {
52 { "size", "set video size", OFFSET(size), FF_OPT_TYPE_STRING, {.str = "320x240"}},
53 { "s", "set video size", OFFSET(size), FF_OPT_TYPE_STRING, {.str = "320x240"}},
54 { "rate", "set video rate", OFFSET(rate), FF_OPT_TYPE_STRING, {.str = "25"}, },
55 { "r", "set video rate", OFFSET(rate), FF_OPT_TYPE_STRING, {.str = "25"}, },
56 { "duration", "set video duration", OFFSET(duration), FF_OPT_TYPE_STRING, {.str = NULL}, },
57 { "sar", "set video sample aspect ratio", OFFSET(sar), FF_OPT_TYPE_RATIONAL, {1}, 0, INT_MAX },
61 static av_cold int init_common(AVFilterContext *ctx, const char *args, void *opaque)
63 TestSourceContext *test = ctx->priv;
64 AVRational frame_rate_q;
65 int64_t duration = -1;
68 av_opt_set_defaults(test);
70 if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
71 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
75 if ((ret = av_parse_video_size(&test->w, &test->h, test->size)) < 0) {
76 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", test->size);
80 if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0 ||
81 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
82 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
86 if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
87 av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
91 test->time_base.num = frame_rate_q.den;
92 test->time_base.den = frame_rate_q.num;
93 test->max_pts = duration >= 0 ?
94 av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
98 av_log(ctx, AV_LOG_DEBUG, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
99 test->w, test->h, frame_rate_q.num, frame_rate_q.den,
100 duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
101 test->sar.num, test->sar.den);
105 static int config_props(AVFilterLink *outlink)
107 TestSourceContext *test = outlink->src->priv;
109 outlink->w = test->w;
110 outlink->h = test->h;
111 outlink->sample_aspect_ratio = test->sar;
112 outlink->time_base = test->time_base;
117 static int request_frame(AVFilterLink *outlink)
119 TestSourceContext *test = outlink->src->priv;
120 AVFilterBufferRef *picref;
122 if (test->max_pts >= 0 && test->pts > test->max_pts)
124 picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
126 picref->pts = test->pts++;
128 picref->video->key_frame = 1;
129 picref->video->interlaced = 0;
130 picref->video->pict_type = AV_PICTURE_TYPE_I;
131 picref->video->pixel_aspect = test->sar;
133 test->fill_picture_fn(outlink->src, picref);
135 avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
136 avfilter_draw_slice(outlink, 0, picref->video->h, 1);
137 avfilter_end_frame(outlink);
138 avfilter_unref_buffer(picref);
143 static const char *testsrc_get_name(void *ctx)
148 static const AVClass testsrc_class = {
149 .class_name = "TestSourceContext",
150 .item_name = testsrc_get_name,
151 .option = testsrc_options,
155 * Fill a rectangle with value val.
157 * @param val the RGB value to set
158 * @param dst pointer to the destination buffer to fill
159 * @param dst_linesize linesize of destination
160 * @param segment_width width of the segment
161 * @param x horizontal coordinate where to draw the rectangle in the destination buffer
162 * @param y horizontal coordinate where to draw the rectangle in the destination buffer
163 * @param w width of the rectangle to draw, expressed as a number of segment_width units
164 * @param h height of the rectangle to draw, expressed as a number of segment_width units
166 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
167 unsigned x, unsigned y, unsigned w, unsigned h)
172 dst += segment_width * (step * x + y * dst_linesize);
173 w *= segment_width * step;
175 for (i = 0; i < h; i++) {
181 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
182 unsigned segment_width)
187 #define LEFT_TOP_VBAR 8
188 #define LEFT_BOT_VBAR 16
189 #define RIGHT_TOP_VBAR 32
190 #define RIGHT_BOT_VBAR 64
194 { 1, 0, 5, 1 }, /* TOP_HBAR */
195 { 1, 6, 5, 1 }, /* MID_HBAR */
196 { 1, 12, 5, 1 }, /* BOT_HBAR */
197 { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
198 { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
199 { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
200 { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
202 static const unsigned char masks[10] = {
203 /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
204 /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
205 /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
206 /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
207 /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
208 /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
209 /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
210 /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
211 /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
212 /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
214 unsigned mask = masks[digit];
217 draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
218 for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
220 draw_rectangle(255, dst, dst_linesize, segment_width,
221 segments[i].x, segments[i].y, segments[i].w, segments[i].h);
224 #define GRADIENT_SIZE (6 * 256)
226 static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
228 TestSourceContext *test = ctx->priv;
231 int color, color_rest;
235 int dquad_x, dquad_y;
236 int grad, dgrad, rgrad, drgrad;
240 uint8_t *data = picref->data[0];
241 int width = picref->video->w;
242 int height = picref->video->h;
244 /* draw colored bars and circle */
245 radius = (width + height) / 4;
246 quad0 = width * width / 4 + height * height / 4 - radius * radius;
247 dquad_y = 1 - height;
249 for (y = 0; y < height; y++) {
255 for (x = 0; x < width; x++) {
261 *(p++) = icolor & 1 ? 255 : 0;
262 *(p++) = icolor & 2 ? 255 : 0;
263 *(p++) = icolor & 4 ? 255 : 0;
265 if (color_rest >= width) {
272 p0 += picref->linesize[0];
275 /* draw sliding color line */
276 p = data + picref->linesize[0] * height * 3/4;
277 grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
280 dgrad = GRADIENT_SIZE / width;
281 drgrad = GRADIENT_SIZE % width;
282 for (x = 0; x < width; x++) {
284 grad < 256 || grad >= 5 * 256 ? 255 :
285 grad >= 2 * 256 && grad < 4 * 256 ? 0 :
286 grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
288 grad >= 4 * 256 ? 0 :
289 grad >= 1 * 256 && grad < 3 * 256 ? 255 :
290 grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
293 grad >= 3 * 256 && grad < 5 * 256 ? 255 :
294 grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
297 if (rgrad >= GRADIENT_SIZE) {
299 rgrad -= GRADIENT_SIZE;
301 if (grad >= GRADIENT_SIZE)
302 grad -= GRADIENT_SIZE;
304 for (y = height / 8; y > 0; y--) {
305 memcpy(p, p - picref->linesize[0], 3 * width);
306 p += picref->linesize[0];
310 seg_size = width / 80;
311 if (seg_size >= 1 && height >= 13 * seg_size) {
312 second = test->nb_frame * test->time_base.num / test->time_base.den;
313 x = width - (width - seg_size * 64) / 2;
314 y = (height - seg_size * 13) / 2;
315 p = data + (x*3 + y * picref->linesize[0]);
316 for (i = 0; i < 8; i++) {
317 p -= 3 * 8 * seg_size;
318 draw_digit(second % 10, p, picref->linesize[0], seg_size);
326 static av_cold int test_init(AVFilterContext *ctx, const char *args, void *opaque)
328 TestSourceContext *test = ctx->priv;
330 test->class = &testsrc_class;
331 test->fill_picture_fn = test_fill_picture;
332 return init_common(ctx, args, opaque);
335 static int test_query_formats(AVFilterContext *ctx)
337 static const enum PixelFormat pix_fmts[] = {
338 PIX_FMT_RGB24, PIX_FMT_NONE
340 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
344 AVFilter avfilter_vsrc_testsrc = {
346 .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
347 .priv_size = sizeof(TestSourceContext),
350 .query_formats = test_query_formats,
352 .inputs = (AVFilterPad[]) {{ .name = NULL}},
354 .outputs = (AVFilterPad[]) {{ .name = "default",
355 .type = AVMEDIA_TYPE_VIDEO,
356 .request_frame = request_frame,
357 .config_props = config_props, },