You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1059 lines
33KB

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