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/mathematics.h"
36 #include "libavutil/opt.h"
37 #include "libavutil/intreadwrite.h"
38 #include "libavutil/parseutils.h"
44 unsigned int nb_frame
;
47 char *size
; ///< video frame size
48 char *rate
; ///< video frame rate
49 char *duration
; ///< total duration of the generated video
50 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
), FF_OPT_TYPE_STRING
, {.str
= "320x240"}},
62 { "s", "set video size", OFFSET(size
), FF_OPT_TYPE_STRING
, {.str
= "320x240"}},
63 { "rate", "set video rate", OFFSET(rate
), FF_OPT_TYPE_STRING
, {.str
= "25"}, },
64 { "r", "set video rate", OFFSET(rate
), FF_OPT_TYPE_STRING
, {.str
= "25"}, },
65 { "duration", "set video duration", OFFSET(duration
), FF_OPT_TYPE_STRING
, {.str
= NULL
}, },
66 { "sar", "set video sample aspect ratio", OFFSET(sar
), FF_OPT_TYPE_RATIONAL
, {1}, 0, INT_MAX
},
70 static av_cold
int init_common(AVFilterContext
*ctx
, const char *args
, void *opaque
)
72 TestSourceContext
*test
= ctx
->priv
;
73 AVRational frame_rate_q
;
74 int64_t duration
= -1;
77 av_opt_set_defaults(test
);
79 if ((ret
= (av_set_options_string(test
, args
, "=", ":"))) < 0) {
80 av_log(ctx
, AV_LOG_ERROR
, "Error parsing options string: '%s'\n", args
);
84 if ((ret
= av_parse_video_size(&test
->w
, &test
->h
, test
->size
)) < 0) {
85 av_log(ctx
, AV_LOG_ERROR
, "Invalid frame size: '%s'\n", test
->size
);
89 if ((ret
= av_parse_video_rate(&frame_rate_q
, test
->rate
)) < 0 ||
90 frame_rate_q
.den
<= 0 || frame_rate_q
.num
<= 0) {
91 av_log(ctx
, AV_LOG_ERROR
, "Invalid frame rate: '%s'\n", 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
);
100 test
->time_base
.num
= frame_rate_q
.den
;
101 test
->time_base
.den
= frame_rate_q
.num
;
102 test
->max_pts
= duration
>= 0 ?
103 av_rescale_q(duration
, AV_TIME_BASE_Q
, test
->time_base
) : -1;
107 av_log(ctx
, AV_LOG_DEBUG
, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
108 test
->w
, test
->h
, frame_rate_q
.num
, frame_rate_q
.den
,
109 duration
< 0 ?
-1 : test
->max_pts
* av_q2d(test
->time_base
),
110 test
->sar
.num
, test
->sar
.den
);
114 static int config_props(AVFilterLink
*outlink
)
116 TestSourceContext
*test
= outlink
->src
->priv
;
118 outlink
->w
= test
->w
;
119 outlink
->h
= test
->h
;
120 outlink
->sample_aspect_ratio
= test
->sar
;
121 outlink
->time_base
= test
->time_base
;
126 static int request_frame(AVFilterLink
*outlink
)
128 TestSourceContext
*test
= outlink
->src
->priv
;
129 AVFilterBufferRef
*picref
;
131 if (test
->max_pts
>= 0 && test
->pts
> test
->max_pts
)
133 picref
= avfilter_get_video_buffer(outlink
, AV_PERM_WRITE
,
135 picref
->pts
= test
->pts
++;
137 picref
->video
->key_frame
= 1;
138 picref
->video
->interlaced
= 0;
139 picref
->video
->pict_type
= AV_PICTURE_TYPE_I
;
140 picref
->video
->pixel_aspect
= test
->sar
;
142 test
->fill_picture_fn(outlink
->src
, picref
);
144 avfilter_start_frame(outlink
, avfilter_ref_buffer(picref
, ~0));
145 avfilter_draw_slice(outlink
, 0, picref
->video
->h
, 1);
146 avfilter_end_frame(outlink
);
147 avfilter_unref_buffer(picref
);
152 #if CONFIG_TESTSRC_FILTER
154 static const char *testsrc_get_name(void *ctx
)
159 static const AVClass testsrc_class
= {
160 .class_name
= "TestSourceContext",
161 .item_name
= testsrc_get_name
,
162 .option
= testsrc_options
,
166 * Fill a rectangle with value val.
168 * @param val the RGB value to set
169 * @param dst pointer to the destination buffer to fill
170 * @param dst_linesize linesize of destination
171 * @param segment_width width of the segment
172 * @param x horizontal coordinate where to draw the rectangle in the destination buffer
173 * @param y horizontal coordinate where to draw the rectangle in the destination buffer
174 * @param w width of the rectangle to draw, expressed as a number of segment_width units
175 * @param h height of the rectangle to draw, expressed as a number of segment_width units
177 static void draw_rectangle(unsigned val
, uint8_t *dst
, int dst_linesize
, unsigned segment_width
,
178 unsigned x
, unsigned y
, unsigned w
, unsigned h
)
183 dst
+= segment_width
* (step
* x
+ y
* dst_linesize
);
184 w
*= segment_width
* step
;
186 for (i
= 0; i
< h
; i
++) {
192 static void draw_digit(int digit
, uint8_t *dst
, unsigned dst_linesize
,
193 unsigned segment_width
)
198 #define LEFT_TOP_VBAR 8
199 #define LEFT_BOT_VBAR 16
200 #define RIGHT_TOP_VBAR 32
201 #define RIGHT_BOT_VBAR 64
205 { 1, 0, 5, 1 }, /* TOP_HBAR */
206 { 1, 6, 5, 1 }, /* MID_HBAR */
207 { 1, 12, 5, 1 }, /* BOT_HBAR */
208 { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
209 { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
210 { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
211 { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
213 static const unsigned char masks
[10] = {
214 /* 0 */ TOP_HBAR
|BOT_HBAR
|LEFT_TOP_VBAR
|LEFT_BOT_VBAR
|RIGHT_TOP_VBAR
|RIGHT_BOT_VBAR
,
215 /* 1 */ RIGHT_TOP_VBAR
|RIGHT_BOT_VBAR
,
216 /* 2 */ TOP_HBAR
|MID_HBAR
|BOT_HBAR
|LEFT_BOT_VBAR
|RIGHT_TOP_VBAR
,
217 /* 3 */ TOP_HBAR
|MID_HBAR
|BOT_HBAR
|RIGHT_TOP_VBAR
|RIGHT_BOT_VBAR
,
218 /* 4 */ MID_HBAR
|LEFT_TOP_VBAR
|RIGHT_TOP_VBAR
|RIGHT_BOT_VBAR
,
219 /* 5 */ TOP_HBAR
|BOT_HBAR
|MID_HBAR
|LEFT_TOP_VBAR
|RIGHT_BOT_VBAR
,
220 /* 6 */ TOP_HBAR
|BOT_HBAR
|MID_HBAR
|LEFT_TOP_VBAR
|LEFT_BOT_VBAR
|RIGHT_BOT_VBAR
,
221 /* 7 */ TOP_HBAR
|RIGHT_TOP_VBAR
|RIGHT_BOT_VBAR
,
222 /* 8 */ TOP_HBAR
|BOT_HBAR
|MID_HBAR
|LEFT_TOP_VBAR
|LEFT_BOT_VBAR
|RIGHT_TOP_VBAR
|RIGHT_BOT_VBAR
,
223 /* 9 */ TOP_HBAR
|BOT_HBAR
|MID_HBAR
|LEFT_TOP_VBAR
|RIGHT_TOP_VBAR
|RIGHT_BOT_VBAR
,
225 unsigned mask
= masks
[digit
];
228 draw_rectangle(0, dst
, dst_linesize
, segment_width
, 0, 0, 8, 13);
229 for (i
= 0; i
< FF_ARRAY_ELEMS(segments
); i
++)
231 draw_rectangle(255, dst
, dst_linesize
, segment_width
,
232 segments
[i
].x
, segments
[i
].y
, segments
[i
].w
, segments
[i
].h
);
235 #define GRADIENT_SIZE (6 * 256)
237 static void test_fill_picture(AVFilterContext
*ctx
, AVFilterBufferRef
*picref
)
239 TestSourceContext
*test
= ctx
->priv
;
242 int color
, color_rest
;
246 int dquad_x
, dquad_y
;
247 int grad
, dgrad
, rgrad
, drgrad
;
251 uint8_t *data
= picref
->data
[0];
252 int width
= picref
->video
->w
;
253 int height
= picref
->video
->h
;
255 /* draw colored bars and circle */
256 radius
= (width
+ height
) / 4;
257 quad0
= width
* width
/ 4 + height
* height
/ 4 - radius
* radius
;
258 dquad_y
= 1 - height
;
260 for (y
= 0; y
< height
; y
++) {
266 for (x
= 0; x
< width
; x
++) {
272 *(p
++) = icolor
& 1 ?
255 : 0;
273 *(p
++) = icolor
& 2 ?
255 : 0;
274 *(p
++) = icolor
& 4 ?
255 : 0;
276 if (color_rest
>= width
) {
283 p0
+= picref
->linesize
[0];
286 /* draw sliding color line */
287 p
= data
+ picref
->linesize
[0] * height
* 3/4;
288 grad
= (256 * test
->nb_frame
* test
->time_base
.num
/ test
->time_base
.den
) %
291 dgrad
= GRADIENT_SIZE
/ width
;
292 drgrad
= GRADIENT_SIZE
% width
;
293 for (x
= 0; x
< width
; x
++) {
295 grad
< 256 || grad
>= 5 * 256 ?
255 :
296 grad
>= 2 * 256 && grad
< 4 * 256 ?
0 :
297 grad
< 2 * 256 ?
2 * 256 - 1 - grad
: grad
- 4 * 256;
299 grad
>= 4 * 256 ?
0 :
300 grad
>= 1 * 256 && grad
< 3 * 256 ?
255 :
301 grad
< 1 * 256 ? grad
: 4 * 256 - 1 - grad
;
304 grad
>= 3 * 256 && grad
< 5 * 256 ?
255 :
305 grad
< 3 * 256 ? grad
- 2 * 256 : 6 * 256 - 1 - grad
;
308 if (rgrad
>= GRADIENT_SIZE
) {
310 rgrad
-= GRADIENT_SIZE
;
312 if (grad
>= GRADIENT_SIZE
)
313 grad
-= GRADIENT_SIZE
;
315 for (y
= height
/ 8; y
> 0; y
--) {
316 memcpy(p
, p
- picref
->linesize
[0], 3 * width
);
317 p
+= picref
->linesize
[0];
321 seg_size
= width
/ 80;
322 if (seg_size
>= 1 && height
>= 13 * seg_size
) {
323 second
= test
->nb_frame
* test
->time_base
.num
/ test
->time_base
.den
;
324 x
= width
- (width
- seg_size
* 64) / 2;
325 y
= (height
- seg_size
* 13) / 2;
326 p
= data
+ (x
*3 + y
* picref
->linesize
[0]);
327 for (i
= 0; i
< 8; i
++) {
328 p
-= 3 * 8 * seg_size
;
329 draw_digit(second
% 10, p
, picref
->linesize
[0], seg_size
);
337 static av_cold
int test_init(AVFilterContext
*ctx
, const char *args
, void *opaque
)
339 TestSourceContext
*test
= ctx
->priv
;
341 test
->class = &testsrc_class
;
342 test
->fill_picture_fn
= test_fill_picture
;
343 return init_common(ctx
, args
, opaque
);
346 static int test_query_formats(AVFilterContext
*ctx
)
348 static const enum PixelFormat pix_fmts
[] = {
349 PIX_FMT_RGB24
, PIX_FMT_NONE
351 avfilter_set_common_formats(ctx
, avfilter_make_format_list(pix_fmts
));
355 AVFilter avfilter_vsrc_testsrc
= {
357 .description
= NULL_IF_CONFIG_SMALL("Generate test pattern."),
358 .priv_size
= sizeof(TestSourceContext
),
361 .query_formats
= test_query_formats
,
363 .inputs
= (AVFilterPad
[]) {{ .name
= NULL
}},
365 .outputs
= (AVFilterPad
[]) {{ .name
= "default",
366 .type
= AVMEDIA_TYPE_VIDEO
,
367 .request_frame
= request_frame
,
368 .config_props
= config_props
, },
372 #endif /* CONFIG_TESTSRC_FILTER */
374 #if CONFIG_RGBTESTSRC_FILTER
376 static const char *rgbtestsrc_get_name(void *ctx
)
381 static const AVClass rgbtestsrc_class
= {
382 .class_name
= "RGBTestSourceContext",
383 .item_name
= rgbtestsrc_get_name
,
384 .option
= testsrc_options
,
392 static void rgbtest_put_pixel(uint8_t *dst
, int dst_linesize
,
393 int x
, int y
, int r
, int g
, int b
, enum PixelFormat fmt
,
400 case PIX_FMT_BGR444
: ((uint16_t*)(dst
+ y
*dst_linesize
))[x
] = ((r
>> 4) << 8) | ((g
>> 4) << 4) | (b
>> 4); break;
401 case PIX_FMT_RGB444
: ((uint16_t*)(dst
+ y
*dst_linesize
))[x
] = ((b
>> 4) << 8) | ((g
>> 4) << 4) | (r
>> 4); break;
402 case PIX_FMT_BGR555
: ((uint16_t*)(dst
+ y
*dst_linesize
))[x
] = ((r
>>3)<<10) | ((g
>>3)<<5) | (b
>>3); break;
403 case PIX_FMT_RGB555
: ((uint16_t*)(dst
+ y
*dst_linesize
))[x
] = ((b
>>3)<<10) | ((g
>>3)<<5) | (r
>>3); break;
404 case PIX_FMT_BGR565
: ((uint16_t*)(dst
+ y
*dst_linesize
))[x
] = ((r
>>3)<<11) | ((g
>>2)<<5) | (b
>>3); break;
405 case PIX_FMT_RGB565
: ((uint16_t*)(dst
+ y
*dst_linesize
))[x
] = ((b
>>3)<<11) | ((g
>>2)<<5) | (r
>>3); break;
408 v
= (r
<< (rgba_map
[R
]*8)) + (g
<< (rgba_map
[G
]*8)) + (b
<< (rgba_map
[B
]*8));
409 p
= dst
+ 3*x
+ y
*dst_linesize
;
416 v
= (r
<< (rgba_map
[R
]*8)) + (g
<< (rgba_map
[G
]*8)) + (b
<< (rgba_map
[B
]*8));
417 p
= dst
+ 4*x
+ y
*dst_linesize
;
423 static void rgbtest_fill_picture(AVFilterContext
*ctx
, AVFilterBufferRef
*picref
)
425 TestSourceContext
*test
= ctx
->priv
;
426 int x
, y
, w
= picref
->video
->w
, h
= picref
->video
->h
;
428 for (y
= 0; y
< h
; y
++) {
429 for (x
= 0; x
< picref
->video
->w
; x
++) {
431 int r
= 0, g
= 0, b
= 0;
434 else if (3*y
< 2*h
) g
= c
;
437 rgbtest_put_pixel(picref
->data
[0], picref
->linesize
[0], x
, y
, r
, g
, b
,
438 ctx
->outputs
[0]->format
, test
->rgba_map
);
443 static av_cold
int rgbtest_init(AVFilterContext
*ctx
, const char *args
, void *opaque
)
445 TestSourceContext
*test
= ctx
->priv
;
447 test
->class = &rgbtestsrc_class
;
448 test
->fill_picture_fn
= rgbtest_fill_picture
;
449 return init_common(ctx
, args
, opaque
);
452 static int rgbtest_query_formats(AVFilterContext
*ctx
)
454 static const enum PixelFormat pix_fmts
[] = {
455 PIX_FMT_RGBA
, PIX_FMT_ARGB
, PIX_FMT_BGRA
, PIX_FMT_ABGR
,
456 PIX_FMT_BGR24
, PIX_FMT_RGB24
,
457 PIX_FMT_RGB444
, PIX_FMT_BGR444
,
458 PIX_FMT_RGB565
, PIX_FMT_BGR565
,
459 PIX_FMT_RGB555
, PIX_FMT_BGR555
,
462 avfilter_set_common_formats(ctx
, avfilter_make_format_list(pix_fmts
));
466 static int rgbtest_config_props(AVFilterLink
*outlink
)
468 TestSourceContext
*test
= outlink
->src
->priv
;
470 switch (outlink
->format
) {
471 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;
472 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;
474 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;
476 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;
479 return config_props(outlink
);
482 AVFilter avfilter_vsrc_rgbtestsrc
= {
483 .name
= "rgbtestsrc",
484 .description
= NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
485 .priv_size
= sizeof(TestSourceContext
),
486 .init
= rgbtest_init
,
488 .query_formats
= rgbtest_query_formats
,
490 .inputs
= (AVFilterPad
[]) {{ .name
= NULL
}},
492 .outputs
= (AVFilterPad
[]) {{ .name
= "default",
493 .type
= AVMEDIA_TYPE_VIDEO
,
494 .request_frame
= request_frame
,
495 .config_props
= rgbtest_config_props
, },
499 #endif /* CONFIG_RGBTESTSRC_FILTER */