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.

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