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.

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