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.

794 lines
25KB

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