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.

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