1 /*
2 * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
3 * Copyright (c) 2011 Stefano Sabatini
4 * Copyright (c) 2012 Paul B Mahol
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * Misc test sources.
26 *
27 * testsrc is based on the test pattern generator demuxer by Nicolas George:
28 * http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2007-October/037845.html
29 *
30 * rgbtestsrc is ported from MPlayer libmpcodecs/vf_rgbtest.c by
31 * Michael Niedermayer.
32 *
33 * smptebars and smptehdbars are by Paul B Mahol.
34 */
35
36 #include <float.h>
37
38 #include "libavutil/avassert.h"
39 #include "libavutil/common.h"
40 #include "libavutil/opt.h"
41 #include "libavutil/imgutils.h"
42 #include "libavutil/intreadwrite.h"
43 #include "libavutil/parseutils.h"
44 #include "avfilter.h"
45 #include "drawutils.h"
46 #include "formats.h"
47 #include "internal.h"
48 #include "video.h"
49
50 typedef struct TestSourceContext {
51 const AVClass *class;
52 int w, h;
53 unsigned int nb_frame;
54 AVRational time_base, frame_rate;
55 int64_t pts;
56 int64_t duration; ///< duration expressed in microseconds
57 AVRational sar; ///< sample aspect ratio
58 int draw_once; ///< draw only the first frame, always put out the same picture
59 int draw_once_reset; ///< draw only the first frame or in case of reset
60 AVFrame *picref; ///< cached reference containing the painted picture
61
62 void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
63
64 /* only used by testsrc */
65 int nb_decimals;
66
67 /* only used by color */
68 FFDrawContext draw;
69 FFDrawColor color;
70 uint8_t color_rgba[4];
71
72 /* only used by rgbtest */
73 uint8_t rgba_map[4];
74
75 /* only used by haldclut */
76 int level;
77 } TestSourceContext;
78
79 #define OFFSET(x) offsetof(TestSourceContext, x)
80 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
81
82 #define SIZE_OPTIONS \
83 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
84 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
85
86 #define COMMON_OPTIONS_NOSIZE \
87 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
88 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
89 { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
90 { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
91 { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1}, 0, INT_MAX, FLAGS },
92
93 #define COMMON_OPTIONS SIZE_OPTIONS COMMON_OPTIONS_NOSIZE
94
95 static const AVOption options[] = {
96 COMMON_OPTIONS
97 { NULL }
98 };
99
init(AVFilterContext * ctx)100 static av_cold int init(AVFilterContext *ctx)
101 {
102 TestSourceContext *test = ctx->priv;
103
104 test->time_base = av_inv_q(test->frame_rate);
105 test->nb_frame = 0;
106 test->pts = 0;
107
108 av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
109 test->w, test->h, test->frame_rate.num, test->frame_rate.den,
110 test->duration < 0 ? -1 : (double)test->duration/1000000,
111 test->sar.num, test->sar.den);
112 return 0;
113 }
114
uninit(AVFilterContext * ctx)115 static av_cold void uninit(AVFilterContext *ctx)
116 {
117 TestSourceContext *test = ctx->priv;
118
119 av_frame_free(&test->picref);
120 }
121
config_props(AVFilterLink * outlink)122 static int config_props(AVFilterLink *outlink)
123 {
124 TestSourceContext *test = outlink->src->priv;
125
126 outlink->w = test->w;
127 outlink->h = test->h;
128 outlink->sample_aspect_ratio = test->sar;
129 outlink->frame_rate = test->frame_rate;
130 outlink->time_base = test->time_base;
131
132 return 0;
133 }
134
request_frame(AVFilterLink * outlink)135 static int request_frame(AVFilterLink *outlink)
136 {
137 TestSourceContext *test = outlink->src->priv;
138 AVFrame *frame;
139 if (test->duration >= 0 &&
140 av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
141 return AVERROR_EOF;
142
143 if (test->draw_once) {
144 if (test->draw_once_reset) {
145 av_frame_free(&test->picref);
146 test->draw_once_reset = 0;
147 }
148 if (!test->picref) {
149 test->picref =
150 ff_get_video_buffer(outlink, test->w, test->h);
151 if (!test->picref)
152 return AVERROR(ENOMEM);
153 test->fill_picture_fn(outlink->src, test->picref);
154 }
155 frame = av_frame_clone(test->picref);
156 } else
157 frame = ff_get_video_buffer(outlink, test->w, test->h);
158
159 if (!frame)
160 return AVERROR(ENOMEM);
161 frame->pts = test->pts;
162 frame->key_frame = 1;
163 frame->interlaced_frame = 0;
164 frame->pict_type = AV_PICTURE_TYPE_I;
165 frame->sample_aspect_ratio = test->sar;
166 if (!test->draw_once)
167 test->fill_picture_fn(outlink->src, frame);
168
169 test->pts++;
170 test->nb_frame++;
171
172 return ff_filter_frame(outlink, frame);
173 }
174
175 #if CONFIG_COLOR_FILTER
176
177 static const AVOption color_options[] = {
178 { "color", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
179 { "c", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
180 COMMON_OPTIONS
181 { NULL }
182 };
183
184 AVFILTER_DEFINE_CLASS(color);
185
color_fill_picture(AVFilterContext * ctx,AVFrame * picref)186 static void color_fill_picture(AVFilterContext *ctx, AVFrame *picref)
187 {
188 TestSourceContext *test = ctx->priv;
189 ff_fill_rectangle(&test->draw, &test->color,
190 picref->data, picref->linesize,
191 0, 0, test->w, test->h);
192 }
193
color_init(AVFilterContext * ctx)194 static av_cold int color_init(AVFilterContext *ctx)
195 {
196 TestSourceContext *test = ctx->priv;
197 test->fill_picture_fn = color_fill_picture;
198 test->draw_once = 1;
199 return init(ctx);
200 }
201
color_query_formats(AVFilterContext * ctx)202 static int color_query_formats(AVFilterContext *ctx)
203 {
204 ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
205 return 0;
206 }
207
color_config_props(AVFilterLink * inlink)208 static int color_config_props(AVFilterLink *inlink)
209 {
210 AVFilterContext *ctx = inlink->src;
211 TestSourceContext *test = ctx->priv;
212 int ret;
213
214 ff_draw_init(&test->draw, inlink->format, 0);
215 ff_draw_color(&test->draw, &test->color, test->color_rgba);
216
217 test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
218 test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
219 if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
220 return AVERROR(EINVAL);
221
222 if ((ret = config_props(inlink)) < 0)
223 return ret;
224
225 return 0;
226 }
227
color_process_command(AVFilterContext * ctx,const char * cmd,const char * args,char * res,int res_len,int flags)228 static int color_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
229 char *res, int res_len, int flags)
230 {
231 TestSourceContext *test = ctx->priv;
232 int ret;
233
234 if (!strcmp(cmd, "color") || !strcmp(cmd, "c")) {
235 uint8_t color_rgba[4];
236
237 ret = av_parse_color(color_rgba, args, -1, ctx);
238 if (ret < 0)
239 return ret;
240
241 memcpy(test->color_rgba, color_rgba, sizeof(color_rgba));
242 ff_draw_color(&test->draw, &test->color, test->color_rgba);
243 test->draw_once_reset = 1;
244 return 0;
245 }
246
247 return AVERROR(ENOSYS);
248 }
249
250 static const AVFilterPad color_outputs[] = {
251 {
252 .name = "default",
253 .type = AVMEDIA_TYPE_VIDEO,
254 .request_frame = request_frame,
255 .config_props = color_config_props,
256 },
257 { NULL }
258 };
259
260 AVFilter ff_vsrc_color = {
261 .name = "color",
262 .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
263 .priv_class = &color_class,
264 .priv_size = sizeof(TestSourceContext),
265 .init = color_init,
266 .uninit = uninit,
267 .query_formats = color_query_formats,
268 .inputs = NULL,
269 .outputs = color_outputs,
270 .process_command = color_process_command,
271 };
272
273 #endif /* CONFIG_COLOR_FILTER */
274
275 #if CONFIG_HALDCLUTSRC_FILTER
276
277 static const AVOption haldclutsrc_options[] = {
278 { "level", "set level", OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 6}, 2, 8, FLAGS },
279 COMMON_OPTIONS_NOSIZE
280 { NULL }
281 };
282
283 AVFILTER_DEFINE_CLASS(haldclutsrc);
284
haldclutsrc_fill_picture(AVFilterContext * ctx,AVFrame * frame)285 static void haldclutsrc_fill_picture(AVFilterContext *ctx, AVFrame *frame)
286 {
287 int i, j, k, x = 0, y = 0, is16bit = 0, step;
288 uint32_t alpha = 0;
289 const TestSourceContext *hc = ctx->priv;
290 int level = hc->level;
291 float scale;
292 const int w = frame->width;
293 const int h = frame->height;
294 const uint8_t *data = frame->data[0];
295 const int linesize = frame->linesize[0];
296 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
297 uint8_t rgba_map[4];
298
299 av_assert0(w == h && w == level*level*level);
300
301 ff_fill_rgba_map(rgba_map, frame->format);
302
303 switch (frame->format) {
304 case AV_PIX_FMT_RGB48:
305 case AV_PIX_FMT_BGR48:
306 case AV_PIX_FMT_RGBA64:
307 case AV_PIX_FMT_BGRA64:
308 is16bit = 1;
309 alpha = 0xffff;
310 break;
311 case AV_PIX_FMT_RGBA:
312 case AV_PIX_FMT_BGRA:
313 case AV_PIX_FMT_ARGB:
314 case AV_PIX_FMT_ABGR:
315 alpha = 0xff;
316 break;
317 }
318
319 step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
320 scale = ((float)(1 << (8*(is16bit+1))) - 1) / (level*level - 1);
321
322 #define LOAD_CLUT(nbits) do { \
323 uint##nbits##_t *dst = ((uint##nbits##_t *)(data + y*linesize)) + x*step; \
324 dst[rgba_map[0]] = av_clip_uint##nbits(i * scale); \
325 dst[rgba_map[1]] = av_clip_uint##nbits(j * scale); \
326 dst[rgba_map[2]] = av_clip_uint##nbits(k * scale); \
327 if (step == 4) \
328 dst[rgba_map[3]] = alpha; \
329 } while (0)
330
331 level *= level;
332 for (k = 0; k < level; k++) {
333 for (j = 0; j < level; j++) {
334 for (i = 0; i < level; i++) {
335 if (!is16bit)
336 LOAD_CLUT(8);
337 else
338 LOAD_CLUT(16);
339 if (++x == w) {
340 x = 0;
341 y++;
342 }
343 }
344 }
345 }
346 }
347
haldclutsrc_init(AVFilterContext * ctx)348 static av_cold int haldclutsrc_init(AVFilterContext *ctx)
349 {
350 TestSourceContext *hc = ctx->priv;
351 hc->fill_picture_fn = haldclutsrc_fill_picture;
352 hc->draw_once = 1;
353 return init(ctx);
354 }
355
haldclutsrc_query_formats(AVFilterContext * ctx)356 static int haldclutsrc_query_formats(AVFilterContext *ctx)
357 {
358 static const enum AVPixelFormat pix_fmts[] = {
359 AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
360 AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
361 AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
362 AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
363 AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
364 AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
365 AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
366 AV_PIX_FMT_NONE,
367 };
368 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
369 return 0;
370 }
371
haldclutsrc_config_props(AVFilterLink * outlink)372 static int haldclutsrc_config_props(AVFilterLink *outlink)
373 {
374 AVFilterContext *ctx = outlink->src;
375 TestSourceContext *hc = ctx->priv;
376
377 hc->w = hc->h = hc->level * hc->level * hc->level;
378 return config_props(outlink);
379 }
380
381 static const AVFilterPad haldclutsrc_outputs[] = {
382 {
383 .name = "default",
384 .type = AVMEDIA_TYPE_VIDEO,
385 .request_frame = request_frame,
386 .config_props = haldclutsrc_config_props,
387 },
388 { NULL }
389 };
390
391 AVFilter ff_vsrc_haldclutsrc = {
392 .name = "haldclutsrc",
393 .description = NULL_IF_CONFIG_SMALL("Provide an identity Hald CLUT."),
394 .priv_class = &haldclutsrc_class,
395 .priv_size = sizeof(TestSourceContext),
396 .init = haldclutsrc_init,
397 .uninit = uninit,
398 .query_formats = haldclutsrc_query_formats,
399 .inputs = NULL,
400 .outputs = haldclutsrc_outputs,
401 };
402 #endif /* CONFIG_HALDCLUTSRC_FILTER */
403
404 #if CONFIG_NULLSRC_FILTER
405
406 #define nullsrc_options options
407 AVFILTER_DEFINE_CLASS(nullsrc);
408
nullsrc_fill_picture(AVFilterContext * ctx,AVFrame * picref)409 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFrame *picref) { }
410
nullsrc_init(AVFilterContext * ctx)411 static av_cold int nullsrc_init(AVFilterContext *ctx)
412 {
413 TestSourceContext *test = ctx->priv;
414
415 test->fill_picture_fn = nullsrc_fill_picture;
416 return init(ctx);
417 }
418
419 static const AVFilterPad nullsrc_outputs[] = {
420 {
421 .name = "default",
422 .type = AVMEDIA_TYPE_VIDEO,
423 .request_frame = request_frame,
424 .config_props = config_props,
425 },
426 { NULL },
427 };
428
429 AVFilter ff_vsrc_nullsrc = {
430 .name = "nullsrc",
431 .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
432 .init = nullsrc_init,
433 .uninit = uninit,
434 .priv_size = sizeof(TestSourceContext),
435 .priv_class = &nullsrc_class,
436 .inputs = NULL,
437 .outputs = nullsrc_outputs,
438 };
439
440 #endif /* CONFIG_NULLSRC_FILTER */
441
442 #if CONFIG_TESTSRC_FILTER
443
444 static const AVOption testsrc_options[] = {
445 COMMON_OPTIONS
446 { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, 0, 17, FLAGS },
447 { "n", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, 0, 17, FLAGS },
448 { NULL }
449 };
450
451 AVFILTER_DEFINE_CLASS(testsrc);
452
453 /**
454 * Fill a rectangle with value val.
455 *
456 * @param val the RGB value to set
457 * @param dst pointer to the destination buffer to fill
458 * @param dst_linesize linesize of destination
459 * @param segment_width width of the segment
460 * @param x horizontal coordinate where to draw the rectangle in the destination buffer
461 * @param y horizontal coordinate where to draw the rectangle in the destination buffer
462 * @param w width of the rectangle to draw, expressed as a number of segment_width units
463 * @param h height of the rectangle to draw, expressed as a number of segment_width units
464 */
draw_rectangle(unsigned val,uint8_t * dst,int dst_linesize,int segment_width,int x,int y,int w,int h)465 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, int segment_width,
466 int x, int y, int w, int h)
467 {
468 int i;
469 int step = 3;
470
471 dst += segment_width * (step * x + y * dst_linesize);
472 w *= segment_width * step;
473 h *= segment_width;
474 for (i = 0; i < h; i++) {
475 memset(dst, val, w);
476 dst += dst_linesize;
477 }
478 }
479
draw_digit(int digit,uint8_t * dst,int dst_linesize,int segment_width)480 static void draw_digit(int digit, uint8_t *dst, int dst_linesize,
481 int segment_width)
482 {
483 #define TOP_HBAR 1
484 #define MID_HBAR 2
485 #define BOT_HBAR 4
486 #define LEFT_TOP_VBAR 8
487 #define LEFT_BOT_VBAR 16
488 #define RIGHT_TOP_VBAR 32
489 #define RIGHT_BOT_VBAR 64
490 struct segments {
491 int x, y, w, h;
492 } segments[] = {
493 { 1, 0, 5, 1 }, /* TOP_HBAR */
494 { 1, 6, 5, 1 }, /* MID_HBAR */
495 { 1, 12, 5, 1 }, /* BOT_HBAR */
496 { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
497 { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
498 { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
499 { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
500 };
501 static const unsigned char masks[10] = {
502 /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
503 /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
504 /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
505 /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
506 /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
507 /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
508 /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
509 /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
510 /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
511 /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
512 };
513 unsigned mask = masks[digit];
514 int i;
515
516 draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
517 for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
518 if (mask & (1<<i))
519 draw_rectangle(255, dst, dst_linesize, segment_width,
520 segments[i].x, segments[i].y, segments[i].w, segments[i].h);
521 }
522
523 #define GRADIENT_SIZE (6 * 256)
524
test_fill_picture(AVFilterContext * ctx,AVFrame * frame)525 static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
526 {
527 TestSourceContext *test = ctx->priv;
528 uint8_t *p, *p0;
529 int x, y;
530 int color, color_rest;
531 int icolor;
532 int radius;
533 int quad0, quad;
534 int dquad_x, dquad_y;
535 int grad, dgrad, rgrad, drgrad;
536 int seg_size;
537 int second;
538 int i;
539 uint8_t *data = frame->data[0];
540 int width = frame->width;
541 int height = frame->height;
542
543 /* draw colored bars and circle */
544 radius = (width + height) / 4;
545 quad0 = width * width / 4 + height * height / 4 - radius * radius;
546 dquad_y = 1 - height;
547 p0 = data;
548 for (y = 0; y < height; y++) {
549 p = p0;
550 color = 0;
551 color_rest = 0;
552 quad = quad0;
553 dquad_x = 1 - width;
554 for (x = 0; x < width; x++) {
555 icolor = color;
556 if (quad < 0)
557 icolor ^= 7;
558 quad += dquad_x;
559 dquad_x += 2;
560 *(p++) = icolor & 1 ? 255 : 0;
561 *(p++) = icolor & 2 ? 255 : 0;
562 *(p++) = icolor & 4 ? 255 : 0;
563 color_rest += 8;
564 if (color_rest >= width) {
565 color_rest -= width;
566 color++;
567 }
568 }
569 quad0 += dquad_y;
570 dquad_y += 2;
571 p0 += frame->linesize[0];
572 }
573
574 /* draw sliding color line */
575 p0 = p = data + frame->linesize[0] * (height * 3/4);
576 grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
577 GRADIENT_SIZE;
578 rgrad = 0;
579 dgrad = GRADIENT_SIZE / width;
580 drgrad = GRADIENT_SIZE % width;
581 for (x = 0; x < width; x++) {
582 *(p++) =
583 grad < 256 || grad >= 5 * 256 ? 255 :
584 grad >= 2 * 256 && grad < 4 * 256 ? 0 :
585 grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
586 *(p++) =
587 grad >= 4 * 256 ? 0 :
588 grad >= 1 * 256 && grad < 3 * 256 ? 255 :
589 grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
590 *(p++) =
591 grad < 2 * 256 ? 0 :
592 grad >= 3 * 256 && grad < 5 * 256 ? 255 :
593 grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
594 grad += dgrad;
595 rgrad += drgrad;
596 if (rgrad >= GRADIENT_SIZE) {
597 grad++;
598 rgrad -= GRADIENT_SIZE;
599 }
600 if (grad >= GRADIENT_SIZE)
601 grad -= GRADIENT_SIZE;
602 }
603 p = p0;
604 for (y = height / 8; y > 0; y--) {
605 memcpy(p+frame->linesize[0], p, 3 * width);
606 p += frame->linesize[0];
607 }
608
609 /* draw digits */
610 seg_size = width / 80;
611 if (seg_size >= 1 && height >= 13 * seg_size) {
612 int64_t p10decimals = 1;
613 double time = av_q2d(test->time_base) * test->nb_frame *
614 pow(10, test->nb_decimals);
615 if (time >= INT_MAX)
616 return;
617
618 for (x = 0; x < test->nb_decimals; x++)
619 p10decimals *= 10;
620
621 second = av_rescale_rnd(test->nb_frame * test->time_base.num, p10decimals, test->time_base.den, AV_ROUND_ZERO);
622 x = width - (width - seg_size * 64) / 2;
623 y = (height - seg_size * 13) / 2;
624 p = data + (x*3 + y * frame->linesize[0]);
625 for (i = 0; i < 8; i++) {
626 p -= 3 * 8 * seg_size;
627 draw_digit(second % 10, p, frame->linesize[0], seg_size);
628 second /= 10;
629 if (second == 0)
630 break;
631 }
632 }
633 }
634
test_init(AVFilterContext * ctx)635 static av_cold int test_init(AVFilterContext *ctx)
636 {
637 TestSourceContext *test = ctx->priv;
638
639 test->fill_picture_fn = test_fill_picture;
640 return init(ctx);
641 }
642
test_query_formats(AVFilterContext * ctx)643 static int test_query_formats(AVFilterContext *ctx)
644 {
645 static const enum AVPixelFormat pix_fmts[] = {
646 AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
647 };
648 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
649 return 0;
650 }
651
652 static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
653 {
654 .name = "default",
655 .type = AVMEDIA_TYPE_VIDEO,
656 .request_frame = request_frame,
657 .config_props = config_props,
658 },
659 { NULL }
660 };
661
662 AVFilter ff_vsrc_testsrc = {
663 .name = "testsrc",
664 .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
665 .priv_size = sizeof(TestSourceContext),
666 .priv_class = &testsrc_class,
667 .init = test_init,
668 .uninit = uninit,
669 .query_formats = test_query_formats,
670 .inputs = NULL,
671 .outputs = avfilter_vsrc_testsrc_outputs,
672 };
673
674 #endif /* CONFIG_TESTSRC_FILTER */
675
676 #if CONFIG_RGBTESTSRC_FILTER
677
678 #define rgbtestsrc_options options
679 AVFILTER_DEFINE_CLASS(rgbtestsrc);
680
681 #define R 0
682 #define G 1
683 #define B 2
684 #define A 3
685
rgbtest_put_pixel(uint8_t * dst,int dst_linesize,int x,int y,int r,int g,int b,enum AVPixelFormat fmt,uint8_t rgba_map[4])686 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
687 int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
688 uint8_t rgba_map[4])
689 {
690 int32_t v;
691 uint8_t *p;
692
693 switch (fmt) {
694 case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
695 case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
696 case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
697 case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
698 case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
699 case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
700 case AV_PIX_FMT_RGB24:
701 case AV_PIX_FMT_BGR24:
702 v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
703 p = dst + 3*x + y*dst_linesize;
704 AV_WL24(p, v);
705 break;
706 case AV_PIX_FMT_RGBA:
707 case AV_PIX_FMT_BGRA:
708 case AV_PIX_FMT_ARGB:
709 case AV_PIX_FMT_ABGR:
710 v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
711 p = dst + 4*x + y*dst_linesize;
712 AV_WL32(p, v);
713 break;
714 }
715 }
716
rgbtest_fill_picture(AVFilterContext * ctx,AVFrame * frame)717 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
718 {
719 TestSourceContext *test = ctx->priv;
720 int x, y, w = frame->width, h = frame->height;
721
722 for (y = 0; y < h; y++) {
723 for (x = 0; x < w; x++) {
724 int c = 256*x/w;
725 int r = 0, g = 0, b = 0;
726
727 if (3*y < h ) r = c;
728 else if (3*y < 2*h) g = c;
729 else b = c;
730
731 rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
732 ctx->outputs[0]->format, test->rgba_map);
733 }
734 }
735 }
736
rgbtest_init(AVFilterContext * ctx)737 static av_cold int rgbtest_init(AVFilterContext *ctx)
738 {
739 TestSourceContext *test = ctx->priv;
740
741 test->draw_once = 1;
742 test->fill_picture_fn = rgbtest_fill_picture;
743 return init(ctx);
744 }
745
rgbtest_query_formats(AVFilterContext * ctx)746 static int rgbtest_query_formats(AVFilterContext *ctx)
747 {
748 static const enum AVPixelFormat pix_fmts[] = {
749 AV_PIX_FMT_RGBA, AV_PIX_FMT_ARGB, AV_PIX_FMT_BGRA, AV_PIX_FMT_ABGR,
750 AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
751 AV_PIX_FMT_RGB444, AV_PIX_FMT_BGR444,
752 AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565,
753 AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555,
754 AV_PIX_FMT_NONE
755 };
756 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
757 return 0;
758 }
759
rgbtest_config_props(AVFilterLink * outlink)760 static int rgbtest_config_props(AVFilterLink *outlink)
761 {
762 TestSourceContext *test = outlink->src->priv;
763
764 ff_fill_rgba_map(test->rgba_map, outlink->format);
765 return config_props(outlink);
766 }
767
768 static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
769 {
770 .name = "default",
771 .type = AVMEDIA_TYPE_VIDEO,
772 .request_frame = request_frame,
773 .config_props = rgbtest_config_props,
774 },
775 { NULL }
776 };
777
778 AVFilter ff_vsrc_rgbtestsrc = {
779 .name = "rgbtestsrc",
780 .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
781 .priv_size = sizeof(TestSourceContext),
782 .priv_class = &rgbtestsrc_class,
783 .init = rgbtest_init,
784 .uninit = uninit,
785 .query_formats = rgbtest_query_formats,
786 .inputs = NULL,
787 .outputs = avfilter_vsrc_rgbtestsrc_outputs,
788 };
789
790 #endif /* CONFIG_RGBTESTSRC_FILTER */
791
792 #if CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER
793
794 static const uint8_t rainbow[7][4] = {
795 { 180, 128, 128, 255 }, /* gray */
796 { 168, 44, 136, 255 }, /* yellow */
797 { 145, 147, 44, 255 }, /* cyan */
798 { 133, 63, 52, 255 }, /* green */
799 { 63, 193, 204, 255 }, /* magenta */
800 { 51, 109, 212, 255 }, /* red */
801 { 28, 212, 120, 255 }, /* blue */
802 };
803
804 static const uint8_t wobnair[7][4] = {
805 { 32, 240, 118, 255 }, /* blue */
806 { 19, 128, 128, 255 }, /* 7.5% intensity black */
807 { 54, 184, 198, 255 }, /* magenta */
808 { 19, 128, 128, 255 }, /* 7.5% intensity black */
809 { 188, 154, 16, 255 }, /* cyan */
810 { 19, 128, 128, 255 }, /* 7.5% intensity black */
811 { 191, 128, 128, 255 }, /* gray */
812 };
813
814 static const uint8_t white[4] = { 235, 128, 128, 255 };
815 static const uint8_t black[4] = { 19, 128, 128, 255 }; /* 7.5% intensity black */
816
817 /* pluge pulses */
818 static const uint8_t neg4ire[4] = { 9, 128, 128, 255 }; /* 3.5% intensity black */
819 static const uint8_t pos4ire[4] = { 29, 128, 128, 255 }; /* 11.5% intensity black */
820
821 /* fudged Q/-I */
822 static const uint8_t i_pixel[4] = { 61, 153, 99, 255 };
823 static const uint8_t q_pixel[4] = { 35, 174, 152, 255 };
824
825 static const uint8_t gray40[4] = { 104, 128, 128, 255 };
826 static const uint8_t gray15[4] = { 49, 128, 128, 255 };
827 static const uint8_t cyan[4] = { 188, 154, 16, 255 };
828 static const uint8_t yellow[4] = { 219, 16, 138, 255 };
829 static const uint8_t blue[4] = { 32, 240, 118, 255 };
830 static const uint8_t red[4] = { 63, 102, 240, 255 };
831 static const uint8_t black0[4] = { 16, 128, 128, 255 };
832 static const uint8_t black2[4] = { 20, 128, 128, 255 };
833 static const uint8_t black4[4] = { 25, 128, 128, 255 };
834 static const uint8_t neg2[4] = { 12, 128, 128, 255 };
835
draw_bar(TestSourceContext * test,const uint8_t color[4],unsigned x,unsigned y,unsigned w,unsigned h,AVFrame * frame)836 static void draw_bar(TestSourceContext *test, const uint8_t color[4],
837 unsigned x, unsigned y, unsigned w, unsigned h,
838 AVFrame *frame)
839 {
840 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
841 uint8_t *p, *p0;
842 int plane;
843
844 x = FFMIN(x, test->w - 1);
845 y = FFMIN(y, test->h - 1);
846 w = FFMIN(w, test->w - x);
847 h = FFMIN(h, test->h - y);
848
849 av_assert0(x + w <= test->w);
850 av_assert0(y + h <= test->h);
851
852 for (plane = 0; frame->data[plane]; plane++) {
853 const int c = color[plane];
854 const int linesize = frame->linesize[plane];
855 int i, px, py, pw, ph;
856
857 if (plane == 1 || plane == 2) {
858 px = x >> desc->log2_chroma_w;
859 pw = w >> desc->log2_chroma_w;
860 py = y >> desc->log2_chroma_h;
861 ph = h >> desc->log2_chroma_h;
862 } else {
863 px = x;
864 pw = w;
865 py = y;
866 ph = h;
867 }
868
869 p0 = p = frame->data[plane] + py * linesize + px;
870 memset(p, c, pw);
871 p += linesize;
872 for (i = 1; i < ph; i++, p += linesize)
873 memcpy(p, p0, pw);
874 }
875 }
876
smptebars_query_formats(AVFilterContext * ctx)877 static int smptebars_query_formats(AVFilterContext *ctx)
878 {
879 static const enum AVPixelFormat pix_fmts[] = {
880 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
881 AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
882 AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
883 AV_PIX_FMT_NONE,
884 };
885 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
886 return 0;
887 }
888
889 static const AVFilterPad smptebars_outputs[] = {
890 {
891 .name = "default",
892 .type = AVMEDIA_TYPE_VIDEO,
893 .request_frame = request_frame,
894 .config_props = config_props,
895 },
896 { NULL }
897 };
898
899 #if CONFIG_SMPTEBARS_FILTER
900
901 #define smptebars_options options
902 AVFILTER_DEFINE_CLASS(smptebars);
903
smptebars_fill_picture(AVFilterContext * ctx,AVFrame * picref)904 static void smptebars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
905 {
906 TestSourceContext *test = ctx->priv;
907 int r_w, r_h, w_h, p_w, p_h, i, tmp, x = 0;
908 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
909
910 av_frame_set_colorspace(picref, AVCOL_SPC_BT470BG);
911
912 r_w = FFALIGN((test->w + 6) / 7, 1 << pixdesc->log2_chroma_w);
913 r_h = FFALIGN(test->h * 2 / 3, 1 << pixdesc->log2_chroma_h);
914 w_h = FFALIGN(test->h * 3 / 4 - r_h, 1 << pixdesc->log2_chroma_h);
915 p_w = FFALIGN(r_w * 5 / 4, 1 << pixdesc->log2_chroma_w);
916 p_h = test->h - w_h - r_h;
917
918 for (i = 0; i < 7; i++) {
919 draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
920 draw_bar(test, wobnair[i], x, r_h, r_w, w_h, picref);
921 x += r_w;
922 }
923 x = 0;
924 draw_bar(test, i_pixel, x, r_h + w_h, p_w, p_h, picref);
925 x += p_w;
926 draw_bar(test, white, x, r_h + w_h, p_w, p_h, picref);
927 x += p_w;
928 draw_bar(test, q_pixel, x, r_h + w_h, p_w, p_h, picref);
929 x += p_w;
930 tmp = FFALIGN(5 * r_w - x, 1 << pixdesc->log2_chroma_w);
931 draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
932 x += tmp;
933 tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
934 draw_bar(test, neg4ire, x, r_h + w_h, tmp, p_h, picref);
935 x += tmp;
936 draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
937 x += tmp;
938 draw_bar(test, pos4ire, x, r_h + w_h, tmp, p_h, picref);
939 x += tmp;
940 draw_bar(test, black, x, r_h + w_h, test->w - x, p_h, picref);
941 }
942
smptebars_init(AVFilterContext * ctx)943 static av_cold int smptebars_init(AVFilterContext *ctx)
944 {
945 TestSourceContext *test = ctx->priv;
946
947 test->fill_picture_fn = smptebars_fill_picture;
948 test->draw_once = 1;
949 return init(ctx);
950 }
951
952 AVFilter ff_vsrc_smptebars = {
953 .name = "smptebars",
954 .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
955 .priv_size = sizeof(TestSourceContext),
956 .priv_class = &smptebars_class,
957 .init = smptebars_init,
958 .uninit = uninit,
959 .query_formats = smptebars_query_formats,
960 .inputs = NULL,
961 .outputs = smptebars_outputs,
962 };
963
964 #endif /* CONFIG_SMPTEBARS_FILTER */
965
966 #if CONFIG_SMPTEHDBARS_FILTER
967
968 #define smptehdbars_options options
969 AVFILTER_DEFINE_CLASS(smptehdbars);
970
smptehdbars_fill_picture(AVFilterContext * ctx,AVFrame * picref)971 static void smptehdbars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
972 {
973 TestSourceContext *test = ctx->priv;
974 int d_w, r_w, r_h, l_w, i, tmp, x = 0, y = 0;
975 const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
976
977 av_frame_set_colorspace(picref, AVCOL_SPC_BT709);
978
979 d_w = FFALIGN(test->w / 8, 1 << pixdesc->log2_chroma_w);
980 r_h = FFALIGN(test->h * 7 / 12, 1 << pixdesc->log2_chroma_h);
981 draw_bar(test, gray40, x, 0, d_w, r_h, picref);
982 x += d_w;
983
984 r_w = FFALIGN((((test->w + 3) / 4) * 3) / 7, 1 << pixdesc->log2_chroma_w);
985 for (i = 0; i < 7; i++) {
986 draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
987 x += r_w;
988 }
989 draw_bar(test, gray40, x, 0, test->w - x, r_h, picref);
990 y = r_h;
991 r_h = FFALIGN(test->h / 12, 1 << pixdesc->log2_chroma_h);
992 draw_bar(test, cyan, 0, y, d_w, r_h, picref);
993 x = d_w;
994 draw_bar(test, i_pixel, x, y, r_w, r_h, picref);
995 x += r_w;
996 tmp = r_w * 6;
997 draw_bar(test, rainbow[0], x, y, tmp, r_h, picref);
998 x += tmp;
999 l_w = x;
1000 draw_bar(test, blue, x, y, test->w - x, r_h, picref);
1001 y += r_h;
1002 draw_bar(test, yellow, 0, y, d_w, r_h, picref);
1003 x = d_w;
1004 draw_bar(test, q_pixel, x, y, r_w, r_h, picref);
1005 x += r_w;
1006
1007 for (i = 0; i < tmp; i += 1 << pixdesc->log2_chroma_w) {
1008 uint8_t yramp[4] = {0};
1009
1010 yramp[0] = i * 255 / tmp;
1011 yramp[1] = 128;
1012 yramp[2] = 128;
1013 yramp[3] = 255;
1014
1015 draw_bar(test, yramp, x, y, 1 << pixdesc->log2_chroma_w, r_h, picref);
1016 x += 1 << pixdesc->log2_chroma_w;
1017 }
1018 draw_bar(test, red, x, y, test->w - x, r_h, picref);
1019 y += r_h;
1020 draw_bar(test, gray15, 0, y, d_w, test->h - y, picref);
1021 x = d_w;
1022 tmp = FFALIGN(r_w * 3 / 2, 1 << pixdesc->log2_chroma_w);
1023 draw_bar(test, black0, x, y, tmp, test->h - y, picref);
1024 x += tmp;
1025 tmp = FFALIGN(r_w * 2, 1 << pixdesc->log2_chroma_w);
1026 draw_bar(test, white, x, y, tmp, test->h - y, picref);
1027 x += tmp;
1028 tmp = FFALIGN(r_w * 5 / 6, 1 << pixdesc->log2_chroma_w);
1029 draw_bar(test, black0, x, y, tmp, test->h - y, picref);
1030 x += tmp;
1031 tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
1032 draw_bar(test, neg2, x, y, tmp, test->h - y, picref);
1033 x += tmp;
1034 draw_bar(test, black0, x, y, tmp, test->h - y, picref);
1035 x += tmp;
1036 draw_bar(test, black2, x, y, tmp, test->h - y, picref);
1037 x += tmp;
1038 draw_bar(test, black0, x, y, tmp, test->h - y, picref);
1039 x += tmp;
1040 draw_bar(test, black4, x, y, tmp, test->h - y, picref);
1041 x += tmp;
1042 r_w = l_w - x;
1043 draw_bar(test, black0, x, y, r_w, test->h - y, picref);
1044 x += r_w;
1045 draw_bar(test, gray15, x, y, test->w - x, test->h - y, picref);
1046 }
1047
smptehdbars_init(AVFilterContext * ctx)1048 static av_cold int smptehdbars_init(AVFilterContext *ctx)
1049 {
1050 TestSourceContext *test = ctx->priv;
1051
1052 test->fill_picture_fn = smptehdbars_fill_picture;
1053 test->draw_once = 1;
1054 return init(ctx);
1055 }
1056
1057 AVFilter ff_vsrc_smptehdbars = {
1058 .name = "smptehdbars",
1059 .description = NULL_IF_CONFIG_SMALL("Generate SMPTE HD color bars."),
1060 .priv_size = sizeof(TestSourceContext),
1061 .priv_class = &smptehdbars_class,
1062 .init = smptehdbars_init,
1063 .uninit = uninit,
1064 .query_formats = smptebars_query_formats,
1065 .inputs = NULL,
1066 .outputs = smptebars_outputs,
1067 };
1068
1069 #endif /* CONFIG_SMPTEHDBARS_FILTER */
1070 #endif /* CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER */
1071