h264_metadata: Add option to delete filler data
[libav.git] / libavfilter / vsrc_testsrc.c
CommitLineData
0244879f
SS
1/*
2 * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
3 * Copyright (c) 2011 Stefano Sabatini
4 *
5 * This file is part of Libav.
6 *
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.
11 *
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.
16 *
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
20 */
21
22/**
23 * @file
ec2ac927
SS
24 * Misc test sources.
25 *
26 * testsrc is based on the test pattern generator demuxer by Nicolas George:
959cfbf3 27 * http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2007-October/037845.html
ec2ac927
SS
28 *
29 * rgbtestsrc is ported from MPlayer libmpcodecs/vf_rgbtest.c by
30 * Michael Niedermayer.
0244879f
SS
31 */
32
33#include <float.h>
34
1d9c2dc8 35#include "libavutil/common.h"
0244879f
SS
36#include "libavutil/mathematics.h"
37#include "libavutil/opt.h"
ec2ac927 38#include "libavutil/intreadwrite.h"
0244879f
SS
39#include "libavutil/parseutils.h"
40#include "avfilter.h"
b74a1da4 41#include "formats.h"
9d0bfc50 42#include "internal.h"
803391f7 43#include "video.h"
0244879f 44
58400ac1 45typedef struct TestSourceContext {
0244879f
SS
46 const AVClass *class;
47 int h, w;
48 unsigned int nb_frame;
5e91a5c5 49 AVRational time_base, frame_rate;
0244879f
SS
50 int64_t pts, max_pts;
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
55
7e350379 56 void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
ec2ac927
SS
57
58 /* only used by rgbtest */
59 int rgba_map[4];
0244879f
SS
60} TestSourceContext;
61
62#define OFFSET(x) offsetof(TestSourceContext, x)
7b3eb745 63#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
0244879f
SS
64
65static const AVOption testsrc_options[] = {
7b3eb745
AK
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 },
0244879f
SS
72 { NULL },
73};
74
7b3eb745 75static av_cold int init_common(AVFilterContext *ctx)
0244879f
SS
76{
77 TestSourceContext *test = ctx->priv;
0244879f
SS
78 int64_t duration = -1;
79 int ret = 0;
80
0244879f
SS
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);
83 return ret;
84 }
85
5e91a5c5 86 if ((ret = av_parse_video_rate(&test->frame_rate, test->rate)) < 0) {
0244879f
SS
87 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
88 return ret;
89 }
90
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);
93 return ret;
94 }
95
5e91a5c5 96 test->time_base = av_inv_q(test->frame_rate);
0244879f
SS
97 test->max_pts = duration >= 0 ?
98 av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
99 test->nb_frame = 0;
100 test->pts = 0;
101
102 av_log(ctx, AV_LOG_DEBUG, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
5e91a5c5 103 test->w, test->h, test->frame_rate.num, test->frame_rate.den,
0244879f
SS
104 duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
105 test->sar.num, test->sar.den);
106 return 0;
107}
108
109static int config_props(AVFilterLink *outlink)
110{
111 TestSourceContext *test = outlink->src->priv;
112
113 outlink->w = test->w;
114 outlink->h = test->h;
115 outlink->sample_aspect_ratio = test->sar;
5e91a5c5
SS
116 outlink->frame_rate = test->frame_rate;
117 outlink->time_base = test->time_base;
0244879f
SS
118
119 return 0;
120}
121
122static int request_frame(AVFilterLink *outlink)
123{
124 TestSourceContext *test = outlink->src->priv;
7e350379 125 AVFrame *frame;
0244879f
SS
126
127 if (test->max_pts >= 0 && test->pts > test->max_pts)
128 return AVERROR_EOF;
7e350379
AK
129 frame = ff_get_video_buffer(outlink, test->w, test->h);
130 if (!frame)
8f3a3ce7
AK
131 return AVERROR(ENOMEM);
132
7e350379
AK
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;
0244879f 138 test->nb_frame++;
7e350379 139 test->fill_picture_fn(outlink->src, frame);
0244879f 140
7e350379 141 return ff_filter_frame(outlink, frame);
0244879f
SS
142}
143
ec2ac927
SS
144#if CONFIG_TESTSRC_FILTER
145
0244879f
SS
146static const char *testsrc_get_name(void *ctx)
147{
148 return "testsrc";
149}
150
151static const AVClass testsrc_class = {
152 .class_name = "TestSourceContext",
153 .item_name = testsrc_get_name,
154 .option = testsrc_options,
155};
156
157/**
158 * Fill a rectangle with value val.
159 *
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
168 */
169static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
170 unsigned x, unsigned y, unsigned w, unsigned h)
171{
172 int i;
173 int step = 3;
174
175 dst += segment_width * (step * x + y * dst_linesize);
176 w *= segment_width * step;
177 h *= segment_width;
178 for (i = 0; i < h; i++) {
179 memset(dst, val, w);
180 dst += dst_linesize;
181 }
182}
183
184static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
185 unsigned segment_width)
186{
187#define TOP_HBAR 1
188#define MID_HBAR 2
189#define BOT_HBAR 4
190#define LEFT_TOP_VBAR 8
191#define LEFT_BOT_VBAR 16
192#define RIGHT_TOP_VBAR 32
193#define RIGHT_BOT_VBAR 64
58400ac1 194 struct segments {
0244879f
SS
195 int x, y, w, h;
196 } segments[] = {
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 */
204 };
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,
216 };
217 unsigned mask = masks[digit];
218 int i;
219
220 draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
221 for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
222 if (mask & (1<<i))
223 draw_rectangle(255, dst, dst_linesize, segment_width,
224 segments[i].x, segments[i].y, segments[i].w, segments[i].h);
225}
226
227#define GRADIENT_SIZE (6 * 256)
228
7e350379 229static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
0244879f
SS
230{
231 TestSourceContext *test = ctx->priv;
232 uint8_t *p, *p0;
233 int x, y;
234 int color, color_rest;
235 int icolor;
236 int radius;
237 int quad0, quad;
238 int dquad_x, dquad_y;
239 int grad, dgrad, rgrad, drgrad;
240 int seg_size;
241 int second;
242 int i;
7e350379
AK
243 uint8_t *data = frame->data[0];
244 int width = frame->width;
245 int height = frame->height;
0244879f
SS
246
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;
251 p0 = data;
252 for (y = 0; y < height; y++) {
253 p = p0;
254 color = 0;
255 color_rest = 0;
256 quad = quad0;
257 dquad_x = 1 - width;
258 for (x = 0; x < width; x++) {
259 icolor = color;
260 if (quad < 0)
261 icolor ^= 7;
262 quad += dquad_x;
263 dquad_x += 2;
264 *(p++) = icolor & 1 ? 255 : 0;
265 *(p++) = icolor & 2 ? 255 : 0;
266 *(p++) = icolor & 4 ? 255 : 0;
267 color_rest += 8;
268 if (color_rest >= width) {
269 color_rest -= width;
270 color++;
271 }
272 }
273 quad0 += dquad_y;
274 dquad_y += 2;
7e350379 275 p0 += frame->linesize[0];
0244879f
SS
276 }
277
278 /* draw sliding color line */
7e350379 279 p = data + frame->linesize[0] * height * 3/4;
0244879f
SS
280 grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
281 GRADIENT_SIZE;
282 rgrad = 0;
283 dgrad = GRADIENT_SIZE / width;
284 drgrad = GRADIENT_SIZE % width;
285 for (x = 0; x < width; x++) {
286 *(p++) =
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;
290 *(p++) =
291 grad >= 4 * 256 ? 0 :
292 grad >= 1 * 256 && grad < 3 * 256 ? 255 :
293 grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
294 *(p++) =
295 grad < 2 * 256 ? 0 :
296 grad >= 3 * 256 && grad < 5 * 256 ? 255 :
297 grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
298 grad += dgrad;
299 rgrad += drgrad;
300 if (rgrad >= GRADIENT_SIZE) {
301 grad++;
302 rgrad -= GRADIENT_SIZE;
303 }
304 if (grad >= GRADIENT_SIZE)
305 grad -= GRADIENT_SIZE;
306 }
307 for (y = height / 8; y > 0; y--) {
7e350379
AK
308 memcpy(p, p - frame->linesize[0], 3 * width);
309 p += frame->linesize[0];
0244879f
SS
310 }
311
312 /* draw digits */
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;
7e350379 318 p = data + (x*3 + y * frame->linesize[0]);
0244879f
SS
319 for (i = 0; i < 8; i++) {
320 p -= 3 * 8 * seg_size;
7e350379 321 draw_digit(second % 10, p, frame->linesize[0], seg_size);
0244879f
SS
322 second /= 10;
323 if (second == 0)
324 break;
325 }
326 }
327}
328
c43a7eca 329static av_cold int test_init(AVFilterContext *ctx)
0244879f
SS
330{
331 TestSourceContext *test = ctx->priv;
332
0244879f 333 test->fill_picture_fn = test_fill_picture;
7b3eb745 334 return init_common(ctx);
0244879f
SS
335}
336
337static int test_query_formats(AVFilterContext *ctx)
338{
716d413c
AK
339 static const enum AVPixelFormat pix_fmts[] = {
340 AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
0244879f 341 };
b74a1da4 342 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
0244879f
SS
343 return 0;
344}
345
568c70e7
MR
346static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
347 {
348 .name = "default",
349 .type = AVMEDIA_TYPE_VIDEO,
350 .request_frame = request_frame,
351 .config_props = config_props,
352 },
353 { NULL }
354};
355
cd43ca04 356AVFilter ff_vsrc_testsrc = {
0244879f
SS
357 .name = "testsrc",
358 .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
359 .priv_size = sizeof(TestSourceContext),
7b3eb745 360 .priv_class = &testsrc_class,
0244879f
SS
361 .init = test_init,
362
363 .query_formats = test_query_formats,
364
1fce361d 365 .inputs = NULL,
0244879f 366
568c70e7 367 .outputs = avfilter_vsrc_testsrc_outputs,
0244879f 368};
ec2ac927
SS
369
370#endif /* CONFIG_TESTSRC_FILTER */
371
372#if CONFIG_RGBTESTSRC_FILTER
373
374static const char *rgbtestsrc_get_name(void *ctx)
375{
376 return "rgbtestsrc";
377}
378
379static const AVClass rgbtestsrc_class = {
380 .class_name = "RGBTestSourceContext",
381 .item_name = rgbtestsrc_get_name,
382 .option = testsrc_options,
383};
384
385#define R 0
386#define G 1
387#define B 2
388#define A 3
389
390static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
716d413c 391 int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
ec2ac927
SS
392 int rgba_map[4])
393{
394 int32_t v;
395 uint8_t *p;
396
397 switch (fmt) {
716d413c
AK
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:
ec2ac927
SS
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;
408 AV_WL24(p, v);
409 break;
716d413c
AK
410 case AV_PIX_FMT_RGBA:
411 case AV_PIX_FMT_BGRA:
412 case AV_PIX_FMT_ARGB:
413 case AV_PIX_FMT_ABGR:
ec2ac927
SS
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;
416 AV_WL32(p, v);
417 break;
418 }
419}
420
7e350379 421static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
ec2ac927
SS
422{
423 TestSourceContext *test = ctx->priv;
7e350379 424 int x, y, w = frame->width, h = frame->height;
ec2ac927
SS
425
426 for (y = 0; y < h; y++) {
7e350379 427 for (x = 0; x < w; x++) {
ec2ac927
SS
428 int c = 256*x/w;
429 int r = 0, g = 0, b = 0;
430
431 if (3*y < h ) r = c;
432 else if (3*y < 2*h) g = c;
433 else b = c;
434
7e350379 435 rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
ec2ac927
SS
436 ctx->outputs[0]->format, test->rgba_map);
437 }
438 }
439}
440
c43a7eca 441static av_cold int rgbtest_init(AVFilterContext *ctx)
ec2ac927
SS
442{
443 TestSourceContext *test = ctx->priv;
444
ec2ac927 445 test->fill_picture_fn = rgbtest_fill_picture;
7b3eb745 446 return init_common(ctx);
ec2ac927
SS
447}
448
449static int rgbtest_query_formats(AVFilterContext *ctx)
450{
716d413c
AK
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,
457 AV_PIX_FMT_NONE
ec2ac927 458 };
b74a1da4 459 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
ec2ac927
SS
460 return 0;
461}
462
463static int rgbtest_config_props(AVFilterLink *outlink)
464{
465 TestSourceContext *test = outlink->src->priv;
466
467 switch (outlink->format) {
716d413c
AK
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;
ec2ac927
SS
474 }
475
476 return config_props(outlink);
477}
478
568c70e7
MR
479static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
480 {
481 .name = "default",
482 .type = AVMEDIA_TYPE_VIDEO,
483 .request_frame = request_frame,
484 .config_props = rgbtest_config_props,
485 },
486 { NULL }
487};
488
cd43ca04 489AVFilter ff_vsrc_rgbtestsrc = {
ec2ac927
SS
490 .name = "rgbtestsrc",
491 .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
492 .priv_size = sizeof(TestSourceContext),
7b3eb745 493 .priv_class = &rgbtestsrc_class,
ec2ac927
SS
494 .init = rgbtest_init,
495
496 .query_formats = rgbtest_query_formats,
497
1fce361d 498 .inputs = NULL,
ec2ac927 499
568c70e7 500 .outputs = avfilter_vsrc_rgbtestsrc_outputs,
ec2ac927
SS
501};
502
503#endif /* CONFIG_RGBTESTSRC_FILTER */