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