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.

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