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
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/opt.h"
31 #include "libavutil/parseutils.h"
37 unsigned int nb_frame;
40 char *size; ///< video frame size
41 char *rate; ///< video frame rate
42 char *duration; ///< total duration of the generated video
45 #define OFFSET(x) offsetof(TestSourceContext, x)
47 static const AVOption testsrc_options[]= {
48 { "size", "set video size", OFFSET(size), FF_OPT_TYPE_STRING, {.str = "320x240"}, 0, 0 },
49 { "s", "set video size", OFFSET(size), FF_OPT_TYPE_STRING, {.str = "320x240"}, 0, 0 },
50 { "rate", "set video rate", OFFSET(rate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
51 { "r", "set video rate", OFFSET(rate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
52 { "duration", "set video duration", OFFSET(duration), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
56 static const char *testsrc_get_name(void *ctx)
61 static const AVClass testsrc_class = {
67 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
69 TestSourceContext *test = ctx->priv;
70 AVRational frame_rate_q;
71 int64_t duration = -1;
74 test->class = &testsrc_class;
75 av_opt_set_defaults2(test, 0, 0);
77 if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
78 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
82 if ((ret = av_parse_video_size(&test->w, &test->h, test->size)) < 0) {
83 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", test->size);
87 if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0 ||
88 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
89 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
93 if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
94 av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
98 test->time_base.num = frame_rate_q.den;
99 test->time_base.den = frame_rate_q.num;
100 test->max_pts = duration >= 0 ?
101 av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
105 av_log(ctx, AV_LOG_INFO, "size:%dx%d rate:%d/%d duration:%f\n",
106 test->w, test->h, frame_rate_q.num, frame_rate_q.den,
107 duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base));
111 static int config_props(AVFilterLink *outlink)
113 TestSourceContext *test = outlink->src->priv;
115 outlink->w = test->w;
116 outlink->h = test->h;
117 outlink->time_base = test->time_base;
123 * Fill a rectangle with value val.
125 * @param val the RGB value to set
126 * @param dst pointer to the destination buffer to fill
127 * @param dst_linesize linesize of destination
128 * @param segment_width width of the segment
129 * @param x horizontal coordinate where to draw the rectangle in the destination buffer
130 * @param y horizontal coordinate where to draw the rectangle in the destination buffer
131 * @param w width of the rectangle to draw, expressed as a number of segment_width units
132 * @param h height of the rectangle to draw, expressed as a number of segment_width units
134 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
135 unsigned x, unsigned y, unsigned w, unsigned h)
140 dst += segment_width * (step * x + y * dst_linesize);
141 w *= segment_width * step;
143 for (i = 0; i < h; i++) {
149 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
150 unsigned segment_width)
155 #define LEFT_TOP_VBAR 8
156 #define LEFT_BOT_VBAR 16
157 #define RIGHT_TOP_VBAR 32
158 #define RIGHT_BOT_VBAR 64
162 { 1, 0, 5, 1 }, /* TOP_HBAR */
163 { 1, 6, 5, 1 }, /* MID_HBAR */
164 { 1, 12, 5, 1 }, /* BOT_HBAR */
165 { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
166 { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
167 { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
168 { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
170 static const unsigned char masks[10] = {
171 /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
172 /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
173 /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
174 /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
175 /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
176 /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
177 /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
178 /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
179 /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
180 /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
182 unsigned mask = masks[digit];
185 draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
186 for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
188 draw_rectangle(255, dst, dst_linesize, segment_width,
189 segments[i].x, segments[i].y, segments[i].w, segments[i].h);
192 #define GRADIENT_SIZE (6 * 256)
194 static void fill_picture(TestSourceContext *test, AVFilterBufferRef *picref)
198 int color, color_rest;
202 int dquad_x, dquad_y;
203 int grad, dgrad, rgrad, drgrad;
207 uint8_t *data = picref->data[0];
208 int width = picref->video->w;
209 int height = picref->video->h;
211 /* draw colored bars and circle */
212 radius = (width + height) / 4;
213 quad0 = width * width / 4 + height * height / 4 - radius * radius;
214 dquad_y = 1 - height;
216 for (y = 0; y < height; y++) {
222 for (x = 0; x < width; x++) {
228 *(p++) = icolor & 1 ? 255 : 0;
229 *(p++) = icolor & 2 ? 255 : 0;
230 *(p++) = icolor & 4 ? 255 : 0;
232 if (color_rest >= width) {
239 p0 += picref->linesize[0];
242 /* draw sliding color line */
243 p = data + picref->linesize[0] * height * 3/4;
244 grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
247 dgrad = GRADIENT_SIZE / width;
248 drgrad = GRADIENT_SIZE % width;
249 for (x = 0; x < width; x++) {
251 grad < 256 || grad >= 5 * 256 ? 255 :
252 grad >= 2 * 256 && grad < 4 * 256 ? 0 :
253 grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
255 grad >= 4 * 256 ? 0 :
256 grad >= 1 * 256 && grad < 3 * 256 ? 255 :
257 grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
260 grad >= 3 * 256 && grad < 5 * 256 ? 255 :
261 grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
264 if (rgrad >= GRADIENT_SIZE) {
266 rgrad -= GRADIENT_SIZE;
268 if (grad >= GRADIENT_SIZE)
269 grad -= GRADIENT_SIZE;
271 for (y = height / 8; y > 0; y--) {
272 memcpy(p, p - picref->linesize[0], 3 * width);
273 p += picref->linesize[0];
277 seg_size = width / 80;
278 if (seg_size >= 1 && height >= 13 * seg_size) {
279 second = test->nb_frame * test->time_base.num / test->time_base.den;
280 x = width - (width - seg_size * 64) / 2;
281 y = (height - seg_size * 13) / 2;
282 p = data + (x*3 + y * picref->linesize[0]);
283 for (i = 0; i < 8; i++) {
284 p -= 3 * 8 * seg_size;
285 draw_digit(second % 10, p, picref->linesize[0], seg_size);
293 static int request_frame(AVFilterLink *outlink)
295 TestSourceContext *test = outlink->src->priv;
296 AVFilterBufferRef *picref;
298 if (test->max_pts >= 0 && test->pts > test->max_pts)
300 picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
302 picref->pts = test->pts++;
304 fill_picture(test, picref);
306 avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
307 avfilter_draw_slice(outlink, 0, picref->video->h, 1);
308 avfilter_end_frame(outlink);
309 avfilter_unref_buffer(picref);
314 static int query_formats(AVFilterContext *ctx)
316 static const enum PixelFormat pix_fmts[] = {
317 PIX_FMT_RGB24, PIX_FMT_NONE
319 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
323 AVFilter avfilter_vsrc_testsrc = {
325 .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
326 .priv_size = sizeof(TestSourceContext),
329 .query_formats = query_formats,
331 .inputs = (AVFilterPad[]) {{ .name = NULL}},
333 .outputs = (AVFilterPad[]) {{ .name = "default",
334 .type = AVMEDIA_TYPE_VIDEO,
335 .request_frame = request_frame,
336 .config_props = config_props, },