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];
75 
76 #define OFFSET(x) offsetof(TestSourceContext, x)
77 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
78 
79 #define COMMON_OPTIONS \
80  { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
81  { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
82  { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
83  { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
84  { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
85  { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
86  { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1}, 0, INT_MAX, FLAGS },
87 
88 
89 static const AVOption options[] = {
91  { NULL }
92 };
93 
94 static av_cold int init(AVFilterContext *ctx)
95 {
96  TestSourceContext *test = ctx->priv;
97 
98  test->time_base = av_inv_q(test->frame_rate);
99  test->nb_frame = 0;
100  test->pts = 0;
101 
102  av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
103  test->w, test->h, test->frame_rate.num, test->frame_rate.den,
104  test->duration < 0 ? -1 : (double)test->duration/1000000,
105  test->sar.num, test->sar.den);
106  return 0;
107 }
108 
109 static av_cold void uninit(AVFilterContext *ctx)
110 {
111  TestSourceContext *test = ctx->priv;
112 
113  av_frame_free(&test->picref);
114 }
115 
116 static int config_props(AVFilterLink *outlink)
117 {
118  TestSourceContext *test = outlink->src->priv;
119 
120  outlink->w = test->w;
121  outlink->h = test->h;
122  outlink->sample_aspect_ratio = test->sar;
123  outlink->frame_rate = test->frame_rate;
124  outlink->time_base = test->time_base;
125 
126  return 0;
127 }
128 
129 static int request_frame(AVFilterLink *outlink)
130 {
131  TestSourceContext *test = outlink->src->priv;
132  AVFrame *frame;
133 
134  if (test->duration >= 0 &&
135  av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
136  return AVERROR_EOF;
137 
138  if (test->draw_once) {
139  if (test->draw_once_reset) {
140  av_frame_free(&test->picref);
141  test->draw_once_reset = 0;
142  }
143  if (!test->picref) {
144  test->picref =
145  ff_get_video_buffer(outlink, test->w, test->h);
146  if (!test->picref)
147  return AVERROR(ENOMEM);
148  test->fill_picture_fn(outlink->src, test->picref);
149  }
150  frame = av_frame_clone(test->picref);
151  } else
152  frame = ff_get_video_buffer(outlink, test->w, test->h);
153 
154  if (!frame)
155  return AVERROR(ENOMEM);
156  frame->pts = test->pts;
157  frame->key_frame = 1;
158  frame->interlaced_frame = 0;
159  frame->pict_type = AV_PICTURE_TYPE_I;
160  frame->sample_aspect_ratio = test->sar;
161  if (!test->draw_once)
162  test->fill_picture_fn(outlink->src, frame);
163 
164  test->pts++;
165  test->nb_frame++;
166 
167  return ff_filter_frame(outlink, frame);
168 }
169 
170 #if CONFIG_COLOR_FILTER
171 
172 static const AVOption color_options[] = {
173  { "color", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
174  { "c", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
176  { NULL }
177 };
178 
180 
181 static void color_fill_picture(AVFilterContext *ctx, AVFrame *picref)
182 {
183  TestSourceContext *test = ctx->priv;
184  ff_fill_rectangle(&test->draw, &test->color,
185  picref->data, picref->linesize,
186  0, 0, test->w, test->h);
187 }
188 
189 static av_cold int color_init(AVFilterContext *ctx)
190 {
191  TestSourceContext *test = ctx->priv;
192  test->fill_picture_fn = color_fill_picture;
193  test->draw_once = 1;
194  return init(ctx);
195 }
196 
197 static int color_query_formats(AVFilterContext *ctx)
198 {
200  return 0;
201 }
202 
203 static int color_config_props(AVFilterLink *inlink)
204 {
205  AVFilterContext *ctx = inlink->src;
206  TestSourceContext *test = ctx->priv;
207  int ret;
208 
209  ff_draw_init(&test->draw, inlink->format, 0);
210  ff_draw_color(&test->draw, &test->color, test->color_rgba);
211 
212  test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
213  test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
214  if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
215  return AVERROR(EINVAL);
216 
217  if ((ret = config_props(inlink)) < 0)
218  return ret;
219 
220  return 0;
221 }
222 
223 static int color_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
224  char *res, int res_len, int flags)
225 {
226  TestSourceContext *test = ctx->priv;
227  int ret;
228 
229  if (!strcmp(cmd, "color") || !strcmp(cmd, "c")) {
230  uint8_t color_rgba[4];
231 
232  ret = av_parse_color(color_rgba, args, -1, ctx);
233  if (ret < 0)
234  return ret;
235 
236  memcpy(test->color_rgba, color_rgba, sizeof(color_rgba));
237  ff_draw_color(&test->draw, &test->color, test->color_rgba);
238  test->draw_once_reset = 1;
239  return 0;
240  }
241 
242  return AVERROR(ENOSYS);
243 }
244 
245 static const AVFilterPad color_outputs[] = {
246  {
247  .name = "default",
248  .type = AVMEDIA_TYPE_VIDEO,
249  .request_frame = request_frame,
250  .config_props = color_config_props,
251  },
252  { NULL }
253 };
254 
255 AVFilter avfilter_vsrc_color = {
256  .name = "color",
257  .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
258 
259  .priv_class = &color_class,
260  .priv_size = sizeof(TestSourceContext),
261  .init = color_init,
262  .uninit = uninit,
263 
264  .query_formats = color_query_formats,
265  .inputs = NULL,
266  .outputs = color_outputs,
267  .process_command = color_process_command,
268 };
269 
270 #endif /* CONFIG_COLOR_FILTER */
271 
272 #if CONFIG_NULLSRC_FILTER
273 
274 #define nullsrc_options options
275 AVFILTER_DEFINE_CLASS(nullsrc);
276 
277 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFrame *picref) { }
278 
279 static av_cold int nullsrc_init(AVFilterContext *ctx)
280 {
281  TestSourceContext *test = ctx->priv;
282 
283  test->fill_picture_fn = nullsrc_fill_picture;
284  return init(ctx);
285 }
286 
287 static const AVFilterPad nullsrc_outputs[] = {
288  {
289  .name = "default",
290  .type = AVMEDIA_TYPE_VIDEO,
291  .request_frame = request_frame,
292  .config_props = config_props,
293  },
294  { NULL },
295 };
296 
297 AVFilter avfilter_vsrc_nullsrc = {
298  .name = "nullsrc",
299  .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
300  .init = nullsrc_init,
301  .uninit = uninit,
302  .priv_size = sizeof(TestSourceContext),
303  .priv_class = &nullsrc_class,
304  .inputs = NULL,
305  .outputs = nullsrc_outputs,
306 };
307 
308 #endif /* CONFIG_NULLSRC_FILTER */
309 
310 #if CONFIG_TESTSRC_FILTER
311 
312 static const AVOption testsrc_options[] = {
314  { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, 0, 17, FLAGS },
315  { "n", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, 0, 17, FLAGS },
316  { NULL }
317 };
318 
319 AVFILTER_DEFINE_CLASS(testsrc);
320 
321 /**
322  * Fill a rectangle with value val.
323  *
324  * @param val the RGB value to set
325  * @param dst pointer to the destination buffer to fill
326  * @param dst_linesize linesize of destination
327  * @param segment_width width of the segment
328  * @param x horizontal coordinate where to draw the rectangle in the destination buffer
329  * @param y horizontal coordinate where to draw the rectangle in the destination buffer
330  * @param w width of the rectangle to draw, expressed as a number of segment_width units
331  * @param h height of the rectangle to draw, expressed as a number of segment_width units
332  */
333 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, int segment_width,
334  int x, int y, int w, int h)
335 {
336  int i;
337  int step = 3;
338 
339  dst += segment_width * (step * x + y * dst_linesize);
340  w *= segment_width * step;
341  h *= segment_width;
342  for (i = 0; i < h; i++) {
343  memset(dst, val, w);
344  dst += dst_linesize;
345  }
346 }
347 
348 static void draw_digit(int digit, uint8_t *dst, int dst_linesize,
349  int segment_width)
350 {
351 #define TOP_HBAR 1
352 #define MID_HBAR 2
353 #define BOT_HBAR 4
354 #define LEFT_TOP_VBAR 8
355 #define LEFT_BOT_VBAR 16
356 #define RIGHT_TOP_VBAR 32
357 #define RIGHT_BOT_VBAR 64
358  struct {
359  int x, y, w, h;
360  } segments[] = {
361  { 1, 0, 5, 1 }, /* TOP_HBAR */
362  { 1, 6, 5, 1 }, /* MID_HBAR */
363  { 1, 12, 5, 1 }, /* BOT_HBAR */
364  { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
365  { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
366  { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
367  { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
368  };
369  static const unsigned char masks[10] = {
370  /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
371  /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
372  /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
373  /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
374  /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
375  /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
376  /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
377  /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
378  /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
379  /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
380  };
381  unsigned mask = masks[digit];
382  int i;
383 
384  draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
385  for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
386  if (mask & (1<<i))
387  draw_rectangle(255, dst, dst_linesize, segment_width,
388  segments[i].x, segments[i].y, segments[i].w, segments[i].h);
389 }
390 
391 #define GRADIENT_SIZE (6 * 256)
392 
393 static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
394 {
395  TestSourceContext *test = ctx->priv;
396  uint8_t *p, *p0;
397  int x, y;
398  int color, color_rest;
399  int icolor;
400  int radius;
401  int quad0, quad;
402  int dquad_x, dquad_y;
403  int grad, dgrad, rgrad, drgrad;
404  int seg_size;
405  int second;
406  int i;
407  uint8_t *data = frame->data[0];
408  int width = frame->width;
409  int height = frame->height;
410 
411  /* draw colored bars and circle */
412  radius = (width + height) / 4;
413  quad0 = width * width / 4 + height * height / 4 - radius * radius;
414  dquad_y = 1 - height;
415  p0 = data;
416  for (y = 0; y < height; y++) {
417  p = p0;
418  color = 0;
419  color_rest = 0;
420  quad = quad0;
421  dquad_x = 1 - width;
422  for (x = 0; x < width; x++) {
423  icolor = color;
424  if (quad < 0)
425  icolor ^= 7;
426  quad += dquad_x;
427  dquad_x += 2;
428  *(p++) = icolor & 1 ? 255 : 0;
429  *(p++) = icolor & 2 ? 255 : 0;
430  *(p++) = icolor & 4 ? 255 : 0;
431  color_rest += 8;
432  if (color_rest >= width) {
433  color_rest -= width;
434  color++;
435  }
436  }
437  quad0 += dquad_y;
438  dquad_y += 2;
439  p0 += frame->linesize[0];
440  }
441 
442  /* draw sliding color line */
443  p0 = p = data + frame->linesize[0] * height * 3/4;
444  grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
445  GRADIENT_SIZE;
446  rgrad = 0;
447  dgrad = GRADIENT_SIZE / width;
448  drgrad = GRADIENT_SIZE % width;
449  for (x = 0; x < width; x++) {
450  *(p++) =
451  grad < 256 || grad >= 5 * 256 ? 255 :
452  grad >= 2 * 256 && grad < 4 * 256 ? 0 :
453  grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
454  *(p++) =
455  grad >= 4 * 256 ? 0 :
456  grad >= 1 * 256 && grad < 3 * 256 ? 255 :
457  grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
458  *(p++) =
459  grad < 2 * 256 ? 0 :
460  grad >= 3 * 256 && grad < 5 * 256 ? 255 :
461  grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
462  grad += dgrad;
463  rgrad += drgrad;
464  if (rgrad >= GRADIENT_SIZE) {
465  grad++;
466  rgrad -= GRADIENT_SIZE;
467  }
468  if (grad >= GRADIENT_SIZE)
469  grad -= GRADIENT_SIZE;
470  }
471  p = p0;
472  for (y = height / 8; y > 0; y--) {
473  memcpy(p+frame->linesize[0], p, 3 * width);
474  p += frame->linesize[0];
475  }
476 
477  /* draw digits */
478  seg_size = width / 80;
479  if (seg_size >= 1 && height >= 13 * seg_size) {
480  int64_t p10decimals = 1;
481  double time = av_q2d(test->time_base) * test->nb_frame *
482  pow(10, test->nb_decimals);
483  if (time >= INT_MAX)
484  return;
485 
486  for (x = 0; x < test->nb_decimals; x++)
487  p10decimals *= 10;
488 
489  second = av_rescale_rnd(test->nb_frame * test->time_base.num, p10decimals, test->time_base.den, AV_ROUND_ZERO);
490  x = width - (width - seg_size * 64) / 2;
491  y = (height - seg_size * 13) / 2;
492  p = data + (x*3 + y * frame->linesize[0]);
493  for (i = 0; i < 8; i++) {
494  p -= 3 * 8 * seg_size;
495  draw_digit(second % 10, p, frame->linesize[0], seg_size);
496  second /= 10;
497  if (second == 0)
498  break;
499  }
500  }
501 }
502 
503 static av_cold int test_init(AVFilterContext *ctx)
504 {
505  TestSourceContext *test = ctx->priv;
506 
507  test->fill_picture_fn = test_fill_picture;
508  return init(ctx);
509 }
510 
511 static int test_query_formats(AVFilterContext *ctx)
512 {
513  static const enum AVPixelFormat pix_fmts[] = {
515  };
517  return 0;
518 }
519 
520 static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
521  {
522  .name = "default",
523  .type = AVMEDIA_TYPE_VIDEO,
524  .request_frame = request_frame,
525  .config_props = config_props,
526  },
527  { NULL }
528 };
529 
530 AVFilter avfilter_vsrc_testsrc = {
531  .name = "testsrc",
532  .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
533  .priv_size = sizeof(TestSourceContext),
534  .priv_class = &testsrc_class,
535  .init = test_init,
536  .uninit = uninit,
537 
538  .query_formats = test_query_formats,
539 
540  .inputs = NULL,
541  .outputs = avfilter_vsrc_testsrc_outputs,
542 };
543 
544 #endif /* CONFIG_TESTSRC_FILTER */
545 
546 #if CONFIG_RGBTESTSRC_FILTER
547 
548 #define rgbtestsrc_options options
549 AVFILTER_DEFINE_CLASS(rgbtestsrc);
550 
551 #define R 0
552 #define G 1
553 #define B 2
554 #define A 3
555 
556 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
557  int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
558  uint8_t rgba_map[4])
559 {
560  int32_t v;
561  uint8_t *p;
562 
563  switch (fmt) {
564  case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
565  case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
566  case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
567  case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
568  case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
569  case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
570  case AV_PIX_FMT_RGB24:
571  case AV_PIX_FMT_BGR24:
572  v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
573  p = dst + 3*x + y*dst_linesize;
574  AV_WL24(p, v);
575  break;
576  case AV_PIX_FMT_RGBA:
577  case AV_PIX_FMT_BGRA:
578  case AV_PIX_FMT_ARGB:
579  case AV_PIX_FMT_ABGR:
580  v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
581  p = dst + 4*x + y*dst_linesize;
582  AV_WL32(p, v);
583  break;
584  }
585 }
586 
587 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
588 {
589  TestSourceContext *test = ctx->priv;
590  int x, y, w = frame->width, h = frame->height;
591 
592  for (y = 0; y < h; y++) {
593  for (x = 0; x < w; x++) {
594  int c = 256*x/w;
595  int r = 0, g = 0, b = 0;
596 
597  if (3*y < h ) r = c;
598  else if (3*y < 2*h) g = c;
599  else b = c;
600 
601  rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
602  ctx->outputs[0]->format, test->rgba_map);
603  }
604  }
605 }
606 
607 static av_cold int rgbtest_init(AVFilterContext *ctx)
608 {
609  TestSourceContext *test = ctx->priv;
610 
611  test->draw_once = 1;
612  test->fill_picture_fn = rgbtest_fill_picture;
613  return init(ctx);
614 }
615 
616 static int rgbtest_query_formats(AVFilterContext *ctx)
617 {
618  static const enum AVPixelFormat pix_fmts[] = {
625  };
627  return 0;
628 }
629 
630 static int rgbtest_config_props(AVFilterLink *outlink)
631 {
632  TestSourceContext *test = outlink->src->priv;
633 
634  ff_fill_rgba_map(test->rgba_map, outlink->format);
635  return config_props(outlink);
636 }
637 
638 static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
639  {
640  .name = "default",
641  .type = AVMEDIA_TYPE_VIDEO,
642  .request_frame = request_frame,
643  .config_props = rgbtest_config_props,
644  },
645  { NULL }
646 };
647 
648 AVFilter avfilter_vsrc_rgbtestsrc = {
649  .name = "rgbtestsrc",
650  .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
651  .priv_size = sizeof(TestSourceContext),
652  .priv_class = &rgbtestsrc_class,
653  .init = rgbtest_init,
654  .uninit = uninit,
655 
656  .query_formats = rgbtest_query_formats,
657 
658  .inputs = NULL,
659 
660  .outputs = avfilter_vsrc_rgbtestsrc_outputs,
661 };
662 
663 #endif /* CONFIG_RGBTESTSRC_FILTER */
664 
665 #if CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER
666 
667 static const uint8_t rainbow[7][4] = {
668  { 191, 191, 191, 255 }, /* gray */
669  { 191, 191, 0, 255 }, /* yellow */
670  { 0, 191, 191, 255 }, /* cyan */
671  { 0, 191, 0, 255 }, /* green */
672  { 191, 0, 191, 255 }, /* magenta */
673  { 191, 0, 0, 255 }, /* red */
674  { 0, 0, 191, 255 }, /* blue */
675 };
676 
677 static const uint8_t wobnair[7][4] = {
678  { 0, 0, 191, 255 }, /* blue */
679  { 19, 19, 19, 255 }, /* 7.5% intensity black */
680  { 191, 0, 191, 255 }, /* magenta */
681  { 19, 19, 19, 255 }, /* 7.5% intensity black */
682  { 0, 191, 191, 255 }, /* cyan */
683  { 19, 19, 19, 255 }, /* 7.5% intensity black */
684  { 191, 191, 191, 255 }, /* gray */
685 };
686 
687 static const uint8_t white[4] = { 255, 255, 255, 255 };
688 static const uint8_t black[4] = { 19, 19, 19, 255 }; /* 7.5% intensity black */
689 
690 /* pluge pulses */
691 static const uint8_t neg4ire[4] = { 9, 9, 9, 255 }; /* 3.5% intensity black */
692 static const uint8_t pos4ire[4] = { 29, 29, 29, 255 }; /* 11.5% intensity black */
693 
694 /* fudged Q/-I */
695 static const uint8_t i_pixel[4] = { 0, 68, 130, 255 };
696 static const uint8_t q_pixel[4] = { 67, 0, 130, 255 };
697 
698 static const uint8_t gray40[4] = { 102, 102, 102, 255 };
699 static const uint8_t gray15[4] = { 38, 38, 38, 255 };
700 static const uint8_t cyan[4] = { 0, 255, 255, 255 };
701 static const uint8_t yellow[4] = { 255, 255, 0, 255 };
702 static const uint8_t blue[4] = { 0, 0, 255, 255 };
703 static const uint8_t red[4] = { 255, 0, 0, 255 };
704 static const uint8_t black0[4] = { 5, 5, 5, 255 };
705 static const uint8_t black2[4] = { 10, 10, 10, 255 };
706 static const uint8_t black4[4] = { 15, 15, 15, 255 };
707 static const uint8_t neg2[4] = { 0, 0, 0, 255 };
708 
709 static void inline draw_bar(TestSourceContext *test, const uint8_t *color,
710  unsigned x, unsigned y, unsigned w, unsigned h,
711  AVFrame *frame)
712 {
713  FFDrawColor draw_color;
714 
715  x = FFMIN(x, test->w - 1);
716  y = FFMIN(y, test->h - 1);
717  w = FFMIN(w, test->w - x);
718  h = FFMIN(h, test->h - y);
719 
720  av_assert0(x + w <= test->w);
721  av_assert0(y + h <= test->h);
722 
723  ff_draw_color(&test->draw, &draw_color, color);
724  ff_fill_rectangle(&test->draw, &draw_color,
725  frame->data, frame->linesize, x, y, w, h);
726 }
727 
728 static int smptebars_query_formats(AVFilterContext *ctx)
729 {
731  return 0;
732 }
733 
734 static int smptebars_config_props(AVFilterLink *outlink)
735 {
736  AVFilterContext *ctx = outlink->src;
737  TestSourceContext *test = ctx->priv;
738 
739  ff_draw_init(&test->draw, outlink->format, 0);
740 
741  return config_props(outlink);
742 }
743 
744 static const AVFilterPad smptebars_outputs[] = {
745  {
746  .name = "default",
747  .type = AVMEDIA_TYPE_VIDEO,
748  .request_frame = request_frame,
749  .config_props = smptebars_config_props,
750  },
751  { NULL }
752 };
753 
754 #if CONFIG_SMPTEBARS_FILTER
755 
756 #define smptebars_options options
757 AVFILTER_DEFINE_CLASS(smptebars);
758 
759 static void smptebars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
760 {
761  TestSourceContext *test = ctx->priv;
762  int r_w, r_h, w_h, p_w, p_h, i, tmp, x = 0;
763  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
764 
765  r_w = FFALIGN((test->w + 6) / 7, 1 << pixdesc->log2_chroma_w);
766  r_h = FFALIGN(test->h * 2 / 3, 1 << pixdesc->log2_chroma_h);
767  w_h = FFALIGN(test->h * 3 / 4 - r_h, 1 << pixdesc->log2_chroma_h);
768  p_w = FFALIGN(r_w * 5 / 4, 1 << pixdesc->log2_chroma_w);
769  p_h = test->h - w_h - r_h;
770 
771  for (i = 0; i < 7; i++) {
772  draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
773  draw_bar(test, wobnair[i], x, r_h, r_w, w_h, picref);
774  x += r_w;
775  }
776  x = 0;
777  draw_bar(test, i_pixel, x, r_h + w_h, p_w, p_h, picref);
778  x += p_w;
779  draw_bar(test, white, x, r_h + w_h, p_w, p_h, picref);
780  x += p_w;
781  draw_bar(test, q_pixel, x, r_h + w_h, p_w, p_h, picref);
782  x += p_w;
783  tmp = FFALIGN(5 * r_w - x, 1 << pixdesc->log2_chroma_w);
784  draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
785  x += tmp;
786  tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
787  draw_bar(test, neg4ire, x, r_h + w_h, tmp, p_h, picref);
788  x += tmp;
789  draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
790  x += tmp;
791  draw_bar(test, pos4ire, x, r_h + w_h, tmp, p_h, picref);
792  x += tmp;
793  draw_bar(test, black, x, r_h + w_h, test->w - x, p_h, picref);
794 }
795 
796 static av_cold int smptebars_init(AVFilterContext *ctx)
797 {
798  TestSourceContext *test = ctx->priv;
799 
800  test->fill_picture_fn = smptebars_fill_picture;
801  test->draw_once = 1;
802  return init(ctx);
803 }
804 
805 AVFilter avfilter_vsrc_smptebars = {
806  .name = "smptebars",
807  .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
808  .priv_size = sizeof(TestSourceContext),
809  .init = smptebars_init,
810  .uninit = uninit,
811 
812  .query_formats = smptebars_query_formats,
813  .inputs = NULL,
814  .outputs = smptebars_outputs,
815  .priv_class = &smptebars_class,
816 };
817 
818 #endif /* CONFIG_SMPTEBARS_FILTER */
819 
820 #if CONFIG_SMPTEHDBARS_FILTER
821 
822 #define smptehdbars_options options
823 AVFILTER_DEFINE_CLASS(smptehdbars);
824 
825 static void smptehdbars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
826 {
827  TestSourceContext *test = ctx->priv;
828  int d_w, r_w, r_h, l_w, i, tmp, x = 0, y = 0;
829  const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
830 
831  d_w = FFALIGN(test->w / 8, 1 << pixdesc->log2_chroma_w);
832  r_h = FFALIGN(test->h * 7 / 12, 1 << pixdesc->log2_chroma_h);
833  draw_bar(test, gray40, x, 0, d_w, r_h, picref);
834  x += d_w;
835 
836  r_w = FFALIGN((((test->w + 3) / 4) * 3) / 7, 1 << pixdesc->log2_chroma_w);
837  for (i = 0; i < 7; i++) {
838  draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
839  x += r_w;
840  }
841  draw_bar(test, gray40, x, 0, test->w - x, r_h, picref);
842  y = r_h;
843  r_h = FFALIGN(test->h / 12, 1 << pixdesc->log2_chroma_h);
844  draw_bar(test, cyan, 0, y, d_w, r_h, picref);
845  x = d_w;
846  draw_bar(test, i_pixel, x, y, r_w, r_h, picref);
847  x += r_w;
848  tmp = r_w * 6;
849  draw_bar(test, rainbow[0], x, y, tmp, r_h, picref);
850  x += tmp;
851  l_w = x;
852  draw_bar(test, blue, x, y, test->w - x, r_h, picref);
853  y += r_h;
854  draw_bar(test, yellow, 0, y, d_w, r_h, picref);
855  x = d_w;
856  draw_bar(test, q_pixel, x, y, r_w, r_h, picref);
857  x += r_w;
858 
859  for (i = 0; i < tmp; i += 1 << pixdesc->log2_chroma_w) {
860  uint8_t yramp[4] = {0};
861 
862  yramp[0] =
863  yramp[1] =
864  yramp[2] = i * 255 / tmp;
865  yramp[3] = 255;
866 
867  draw_bar(test, yramp, x, y, 1 << pixdesc->log2_chroma_w, r_h, picref);
868  x += 1 << pixdesc->log2_chroma_w;
869  }
870  draw_bar(test, red, x, y, test->w - x, r_h, picref);
871  y += r_h;
872  draw_bar(test, gray15, 0, y, d_w, test->h - y, picref);
873  x = d_w;
874  tmp = FFALIGN(r_w * 3 / 2, 1 << pixdesc->log2_chroma_w);
875  draw_bar(test, black0, x, y, tmp, test->h - y, picref);
876  x += tmp;
877  tmp = FFALIGN(r_w * 2, 1 << pixdesc->log2_chroma_w);
878  draw_bar(test, white, x, y, tmp, test->h - y, picref);
879  x += tmp;
880  tmp = FFALIGN(r_w * 5 / 6, 1 << pixdesc->log2_chroma_w);
881  draw_bar(test, black0, x, y, tmp, test->h - y, picref);
882  x += tmp;
883  tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
884  draw_bar(test, neg2, x, y, tmp, test->h - y, picref);
885  x += tmp;
886  draw_bar(test, black0, x, y, tmp, test->h - y, picref);
887  x += tmp;
888  draw_bar(test, black2, x, y, tmp, test->h - y, picref);
889  x += tmp;
890  draw_bar(test, black0, x, y, tmp, test->h - y, picref);
891  x += tmp;
892  draw_bar(test, black4, x, y, tmp, test->h - y, picref);
893  x += tmp;
894  r_w = l_w - x;
895  draw_bar(test, black0, x, y, r_w, test->h - y, picref);
896  x += r_w;
897  draw_bar(test, gray15, x, y, test->w - x, test->h - y, picref);
898 }
899 
900 static av_cold int smptehdbars_init(AVFilterContext *ctx)
901 {
902  TestSourceContext *test = ctx->priv;
903 
904  test->fill_picture_fn = smptehdbars_fill_picture;
905  test->draw_once = 1;
906  return init(ctx);
907 }
908 
909 AVFilter avfilter_vsrc_smptehdbars = {
910  .name = "smptehdbars",
911  .description = NULL_IF_CONFIG_SMALL("Generate SMPTE HD color bars."),
912  .priv_size = sizeof(TestSourceContext),
913  .init = smptehdbars_init,
914  .uninit = uninit,
915 
916  .query_formats = smptebars_query_formats,
917  .inputs = NULL,
918  .outputs = smptebars_outputs,
919  .priv_class = &smptehdbars_class,
920 };
921 
922 #endif /* CONFIG_SMPTEHDBARS_FILTER */
923 #endif /* CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER */