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.

950 lines
30KB

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