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.

780 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 = "black"}, CHAR_MIN, CHAR_MAX },
  76. { "c", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, 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 && strcmp(ctx->filter->name, "color")) {
  107. av_log(ctx, AV_LOG_WARNING,
  108. "Option 'color' is ignored with source '%s'\n",
  109. ctx->filter->name);
  110. }
  111. if ((ret = av_parse_color(test->color_rgba, test->color_str, -1, ctx)) < 0)
  112. return ret;
  113. test->time_base.num = frame_rate_q.den;
  114. test->time_base.den = frame_rate_q.num;
  115. test->max_pts = duration >= 0 ?
  116. av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
  117. test->nb_frame = 0;
  118. test->pts = 0;
  119. av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
  120. test->w, test->h, frame_rate_q.num, frame_rate_q.den,
  121. duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
  122. test->sar.num, test->sar.den);
  123. return 0;
  124. }
  125. static av_cold void uninit(AVFilterContext *ctx)
  126. {
  127. TestSourceContext *test = ctx->priv;
  128. av_opt_free(test);
  129. avfilter_unref_bufferp(&test->picref);
  130. }
  131. static int config_props(AVFilterLink *outlink)
  132. {
  133. TestSourceContext *test = outlink->src->priv;
  134. outlink->w = test->w;
  135. outlink->h = test->h;
  136. outlink->sample_aspect_ratio = test->sar;
  137. outlink->time_base = test->time_base;
  138. return 0;
  139. }
  140. static int request_frame(AVFilterLink *outlink)
  141. {
  142. TestSourceContext *test = outlink->src->priv;
  143. AVFilterBufferRef *outpicref;
  144. int ret = 0;
  145. if (test->max_pts >= 0 && test->pts >= test->max_pts)
  146. return AVERROR_EOF;
  147. if (test->draw_once) {
  148. if (!test->picref) {
  149. test->picref =
  150. ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_PRESERVE|AV_PERM_REUSE,
  151. test->w, test->h);
  152. if (!test->picref)
  153. return AVERROR(ENOMEM);
  154. test->fill_picture_fn(outlink->src, test->picref);
  155. }
  156. outpicref = avfilter_ref_buffer(test->picref, ~AV_PERM_WRITE);
  157. } else
  158. outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, test->w, test->h);
  159. if (!outpicref)
  160. return AVERROR(ENOMEM);
  161. outpicref->pts = test->pts;
  162. outpicref->pos = -1;
  163. outpicref->video->key_frame = 1;
  164. outpicref->video->interlaced = 0;
  165. outpicref->video->pict_type = AV_PICTURE_TYPE_I;
  166. outpicref->video->sample_aspect_ratio = test->sar;
  167. if (!test->draw_once)
  168. test->fill_picture_fn(outlink->src, outpicref);
  169. test->pts++;
  170. test->nb_frame++;
  171. if ((ret = ff_start_frame(outlink, outpicref)) < 0 ||
  172. (ret = ff_draw_slice(outlink, 0, test->h, 1)) < 0 ||
  173. (ret = ff_end_frame(outlink)) < 0)
  174. return ret;
  175. return 0;
  176. }
  177. #if CONFIG_COLOR_FILTER
  178. #define color_options options
  179. AVFILTER_DEFINE_CLASS(color);
  180. static void color_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  181. {
  182. TestSourceContext *test = ctx->priv;
  183. ff_fill_rectangle(&test->draw, &test->color,
  184. picref->data, picref->linesize,
  185. 0, 0, test->w, test->h);
  186. }
  187. static av_cold int color_init(AVFilterContext *ctx, const char *args)
  188. {
  189. TestSourceContext *test = ctx->priv;
  190. test->class = &color_class;
  191. test->fill_picture_fn = color_fill_picture;
  192. test->draw_once = 1;
  193. return init(ctx, args);
  194. }
  195. static int color_query_formats(AVFilterContext *ctx)
  196. {
  197. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  198. return 0;
  199. }
  200. static int color_config_props(AVFilterLink *inlink)
  201. {
  202. AVFilterContext *ctx = inlink->src;
  203. TestSourceContext *test = ctx->priv;
  204. int ret;
  205. ff_draw_init(&test->draw, inlink->format, 0);
  206. ff_draw_color(&test->draw, &test->color, test->color_rgba);
  207. test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
  208. test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
  209. if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
  210. return AVERROR(EINVAL);
  211. if (ret = config_props(inlink) < 0)
  212. return ret;
  213. av_log(ctx, AV_LOG_VERBOSE, "color:0x%02x%02x%02x%02x\n",
  214. test->color_rgba[0], test->color_rgba[1], test->color_rgba[2], test->color_rgba[3]);
  215. return 0;
  216. }
  217. AVFilter avfilter_vsrc_color = {
  218. .name = "color",
  219. .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
  220. .priv_size = sizeof(TestSourceContext),
  221. .init = color_init,
  222. .uninit = uninit,
  223. .query_formats = color_query_formats,
  224. .inputs = (const AVFilterPad[]) {
  225. { .name = NULL }
  226. },
  227. .outputs = (const AVFilterPad[]) {
  228. {
  229. .name = "default",
  230. .type = AVMEDIA_TYPE_VIDEO,
  231. .request_frame = request_frame,
  232. .config_props = color_config_props,
  233. },
  234. { .name = NULL }
  235. },
  236. };
  237. #endif /* CONFIG_COLOR_FILTER */
  238. #if CONFIG_NULLSRC_FILTER
  239. #define nullsrc_options options
  240. AVFILTER_DEFINE_CLASS(nullsrc);
  241. static void nullsrc_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref) { }
  242. static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args)
  243. {
  244. TestSourceContext *test = ctx->priv;
  245. test->class = &nullsrc_class;
  246. test->fill_picture_fn = nullsrc_fill_picture;
  247. return init(ctx, args);
  248. }
  249. AVFilter avfilter_vsrc_nullsrc = {
  250. .name = "nullsrc",
  251. .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
  252. .init = nullsrc_init,
  253. .uninit = uninit,
  254. .priv_size = sizeof(TestSourceContext),
  255. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  256. .outputs = (const AVFilterPad[]) {{ .name = "default",
  257. .type = AVMEDIA_TYPE_VIDEO,
  258. .request_frame = request_frame,
  259. .config_props = config_props, },
  260. { .name = NULL}},
  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, AVFilterBufferRef *picref)
  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 = picref->data[0];
  348. int width = picref->video->w;
  349. int height = picref->video->h;
  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 += picref->linesize[0];
  379. }
  380. /* draw sliding color line */
  381. p0 = p = data + picref->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+picref->linesize[0], p, 3 * width);
  412. p += picref->linesize[0];
  413. }
  414. /* draw digits */
  415. seg_size = width / 80;
  416. if (seg_size >= 1 && height >= 13 * seg_size) {
  417. double time = av_q2d(test->time_base) * test->nb_frame *
  418. pow(10, test->nb_decimals);
  419. if (time > INT_MAX)
  420. return;
  421. second = (int)time;
  422. x = width - (width - seg_size * 64) / 2;
  423. y = (height - seg_size * 13) / 2;
  424. p = data + (x*3 + y * picref->linesize[0]);
  425. for (i = 0; i < 8; i++) {
  426. p -= 3 * 8 * seg_size;
  427. draw_digit(second % 10, p, picref->linesize[0], seg_size);
  428. second /= 10;
  429. if (second == 0)
  430. break;
  431. }
  432. }
  433. }
  434. static av_cold int test_init(AVFilterContext *ctx, const char *args)
  435. {
  436. TestSourceContext *test = ctx->priv;
  437. test->class = &testsrc_class;
  438. test->fill_picture_fn = test_fill_picture;
  439. return init(ctx, args);
  440. }
  441. static int test_query_formats(AVFilterContext *ctx)
  442. {
  443. static const enum PixelFormat pix_fmts[] = {
  444. PIX_FMT_RGB24, PIX_FMT_NONE
  445. };
  446. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  447. return 0;
  448. }
  449. AVFilter avfilter_vsrc_testsrc = {
  450. .name = "testsrc",
  451. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  452. .priv_size = sizeof(TestSourceContext),
  453. .init = test_init,
  454. .uninit = uninit,
  455. .query_formats = test_query_formats,
  456. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  457. .outputs = (const AVFilterPad[]) {{ .name = "default",
  458. .type = AVMEDIA_TYPE_VIDEO,
  459. .request_frame = request_frame,
  460. .config_props = config_props, },
  461. { .name = NULL }},
  462. };
  463. #endif /* CONFIG_TESTSRC_FILTER */
  464. #if CONFIG_RGBTESTSRC_FILTER
  465. #define rgbtestsrc_options options
  466. AVFILTER_DEFINE_CLASS(rgbtestsrc);
  467. #define R 0
  468. #define G 1
  469. #define B 2
  470. #define A 3
  471. static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
  472. int x, int y, int r, int g, int b, enum PixelFormat fmt,
  473. uint8_t rgba_map[4])
  474. {
  475. int32_t v;
  476. uint8_t *p;
  477. switch (fmt) {
  478. case PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
  479. case PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
  480. case PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
  481. case PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
  482. case PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
  483. case PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
  484. case PIX_FMT_RGB24:
  485. case PIX_FMT_BGR24:
  486. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  487. p = dst + 3*x + y*dst_linesize;
  488. AV_WL24(p, v);
  489. break;
  490. case PIX_FMT_RGBA:
  491. case PIX_FMT_BGRA:
  492. case PIX_FMT_ARGB:
  493. case PIX_FMT_ABGR:
  494. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
  495. p = dst + 4*x + y*dst_linesize;
  496. AV_WL32(p, v);
  497. break;
  498. }
  499. }
  500. static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  501. {
  502. TestSourceContext *test = ctx->priv;
  503. int x, y, w = picref->video->w, h = picref->video->h;
  504. for (y = 0; y < h; y++) {
  505. for (x = 0; x < picref->video->w; x++) {
  506. int c = 256*x/w;
  507. int r = 0, g = 0, b = 0;
  508. if (3*y < h ) r = c;
  509. else if (3*y < 2*h) g = c;
  510. else b = c;
  511. rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
  512. ctx->outputs[0]->format, test->rgba_map);
  513. }
  514. }
  515. }
  516. static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
  517. {
  518. TestSourceContext *test = ctx->priv;
  519. test->draw_once = 1;
  520. test->class = &rgbtestsrc_class;
  521. test->fill_picture_fn = rgbtest_fill_picture;
  522. return init(ctx, args);
  523. }
  524. static int rgbtest_query_formats(AVFilterContext *ctx)
  525. {
  526. static const enum PixelFormat pix_fmts[] = {
  527. PIX_FMT_RGBA, PIX_FMT_ARGB, PIX_FMT_BGRA, PIX_FMT_ABGR,
  528. PIX_FMT_BGR24, PIX_FMT_RGB24,
  529. PIX_FMT_RGB444, PIX_FMT_BGR444,
  530. PIX_FMT_RGB565, PIX_FMT_BGR565,
  531. PIX_FMT_RGB555, PIX_FMT_BGR555,
  532. PIX_FMT_NONE
  533. };
  534. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  535. return 0;
  536. }
  537. static int rgbtest_config_props(AVFilterLink *outlink)
  538. {
  539. TestSourceContext *test = outlink->src->priv;
  540. ff_fill_rgba_map(test->rgba_map, outlink->format);
  541. return config_props(outlink);
  542. }
  543. AVFilter avfilter_vsrc_rgbtestsrc = {
  544. .name = "rgbtestsrc",
  545. .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
  546. .priv_size = sizeof(TestSourceContext),
  547. .init = rgbtest_init,
  548. .uninit = uninit,
  549. .query_formats = rgbtest_query_formats,
  550. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  551. .outputs = (const AVFilterPad[]) {{ .name = "default",
  552. .type = AVMEDIA_TYPE_VIDEO,
  553. .request_frame = request_frame,
  554. .config_props = rgbtest_config_props, },
  555. { .name = NULL }},
  556. };
  557. #endif /* CONFIG_RGBTESTSRC_FILTER */
  558. #if CONFIG_SMPTEBARS_FILTER
  559. #define smptebars_options options
  560. AVFILTER_DEFINE_CLASS(smptebars);
  561. static const uint8_t rainbow[7][4] = {
  562. { 191, 191, 191, 255 }, /* gray */
  563. { 191, 191, 0, 255 }, /* yellow */
  564. { 0, 191, 191, 255 }, /* cyan */
  565. { 0, 191, 0, 255 }, /* green */
  566. { 191, 0, 191, 255 }, /* magenta */
  567. { 191, 0, 0, 255 }, /* red */
  568. { 0, 0, 191, 255 }, /* blue */
  569. };
  570. static const uint8_t wobnair[7][4] = {
  571. { 0, 0, 191, 255 }, /* blue */
  572. { 19, 19, 19, 255 }, /* 7.5% intensity black */
  573. { 191, 0, 191, 255 }, /* magenta */
  574. { 19, 19, 19, 255 }, /* 7.5% intensity black */
  575. { 0, 191, 191, 255 }, /* cyan */
  576. { 19, 19, 19, 255 }, /* 7.5% intensity black */
  577. { 191, 191, 191, 255 }, /* gray */
  578. };
  579. static const uint8_t white[4] = { 255, 255, 255, 255 };
  580. static const uint8_t black[4] = { 19, 19, 19, 255 }; /* 7.5% intensity black */
  581. /* pluge pulses */
  582. static const uint8_t neg4ire[4] = { 9, 9, 9, 255 }; /* 3.5% intensity black */
  583. static const uint8_t pos4ire[4] = { 29, 29, 29, 255 }; /* 11.5% intensity black */
  584. /* fudged Q/-I */
  585. static const uint8_t i_pixel[4] = { 0, 68, 130, 255 };
  586. static const uint8_t q_pixel[4] = { 67, 0, 130, 255 };
  587. static void smptebars_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  588. {
  589. TestSourceContext *test = ctx->priv;
  590. FFDrawColor color;
  591. int r_w, r_h, w_h, p_w, p_h, i, x = 0;
  592. r_w = (test->w + 6) / 7;
  593. r_h = test->h * 2 / 3;
  594. w_h = test->h * 3 / 4 - r_h;
  595. p_w = r_w * 5 / 4;
  596. p_h = test->h - w_h - r_h;
  597. #define DRAW_COLOR(rgba, x, y, w, h) \
  598. ff_draw_color(&test->draw, &color, rgba); \
  599. ff_fill_rectangle(&test->draw, &color, \
  600. picref->data, picref->linesize, x, y, w, h) \
  601. for (i = 0; i < 7; i++) {
  602. DRAW_COLOR(rainbow[i], x, 0, FFMIN(r_w, test->w - x), r_h);
  603. DRAW_COLOR(wobnair[i], x, r_h, FFMIN(r_w, test->w - x), w_h);
  604. x += r_w;
  605. }
  606. x = 0;
  607. DRAW_COLOR(i_pixel, x, r_h + w_h, p_w, p_h);
  608. x += p_w;
  609. DRAW_COLOR(white, x, r_h + w_h, p_w, p_h);
  610. x += p_w;
  611. DRAW_COLOR(q_pixel, x, r_h + w_h, p_w, p_h);
  612. x += p_w;
  613. DRAW_COLOR(black, x, r_h + w_h, 5 * r_w - x, p_h);
  614. x += 5 * r_w - x;
  615. DRAW_COLOR(neg4ire, x, r_h + w_h, r_w / 3, p_h);
  616. x += r_w / 3;
  617. DRAW_COLOR(black, x, r_h + w_h, r_w / 3, p_h);
  618. x += r_w / 3;
  619. DRAW_COLOR(pos4ire, x, r_h + w_h, r_w / 3, p_h);
  620. x += r_w / 3;
  621. DRAW_COLOR(black, x, r_h + w_h, test->w - x, p_h);
  622. }
  623. static av_cold int smptebars_init(AVFilterContext *ctx, const char *args)
  624. {
  625. TestSourceContext *test = ctx->priv;
  626. test->class = &smptebars_class;
  627. test->fill_picture_fn = smptebars_fill_picture;
  628. test->draw_once = 1;
  629. return init(ctx, args);
  630. }
  631. static int smptebars_query_formats(AVFilterContext *ctx)
  632. {
  633. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  634. return 0;
  635. }
  636. static int smptebars_config_props(AVFilterLink *outlink)
  637. {
  638. AVFilterContext *ctx = outlink->src;
  639. TestSourceContext *test = ctx->priv;
  640. ff_draw_init(&test->draw, outlink->format, 0);
  641. return config_props(outlink);
  642. }
  643. AVFilter avfilter_vsrc_smptebars = {
  644. .name = "smptebars",
  645. .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
  646. .priv_size = sizeof(TestSourceContext),
  647. .init = smptebars_init,
  648. .uninit = uninit,
  649. .query_formats = smptebars_query_formats,
  650. .inputs = (const AVFilterPad[]) {
  651. { .name = NULL }
  652. },
  653. .outputs = (const AVFilterPad[]) {
  654. {
  655. .name = "default",
  656. .type = AVMEDIA_TYPE_VIDEO,
  657. .request_frame = request_frame,
  658. .config_props = smptebars_config_props,
  659. },
  660. { .name = NULL }
  661. },
  662. };
  663. #endif /* CONFIG_SMPTEBARS_FILTER */