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.

928 lines
29KB

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