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.

810 lines
26KB

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