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.

796 lines
25KB

  1. /*
  2. * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. * Copyright (c) 2012 Paul B Mahol
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. /**
  23. * @file
  24. * Misc test sources.
  25. *
  26. * testsrc is based on the test pattern generator demuxer by Nicolas George:
  27. * http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2007-October/037845.html
  28. *
  29. * rgbtestsrc is ported from MPlayer libmpcodecs/vf_rgbtest.c by
  30. * Michael Niedermayer.
  31. *
  32. * smptebars is by Paul B Mahol.
  33. */
  34. #include <float.h>
  35. #include "libavutil/common.h"
  36. #include "libavutil/opt.h"
  37. #include "libavutil/imgutils.h"
  38. #include "libavutil/intreadwrite.h"
  39. #include "libavutil/parseutils.h"
  40. #include "avfilter.h"
  41. #include "drawutils.h"
  42. #include "formats.h"
  43. #include "internal.h"
  44. #include "video.h"
  45. typedef struct {
  46. const AVClass *class;
  47. int w, h;
  48. unsigned int nb_frame;
  49. AVRational time_base, frame_rate;
  50. int64_t pts;
  51. char *frame_rate_str; ///< video frame rate
  52. char *duration_str; ///< total duration of the generated video
  53. int64_t duration; ///< duration expressed in microseconds
  54. AVRational sar; ///< sample aspect ratio
  55. int nb_decimals;
  56. int draw_once; ///< draw only the first frame, always put out the same picture
  57. AVFilterBufferRef *picref; ///< cached reference containing the painted picture
  58. void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
  59. /* only used by color */
  60. char *color_str;
  61. FFDrawContext draw;
  62. FFDrawColor color;
  63. uint8_t color_rgba[4];
  64. /* only used by rgbtest */
  65. uint8_t rgba_map[4];
  66. } TestSourceContext;
  67. #define OFFSET(x) offsetof(TestSourceContext, x)
  68. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  69. static const AVOption options[] = {
  70. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },
  71. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },
  72. { "rate", "set video rate", OFFSET(frame_rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
  73. { "r", "set video rate", OFFSET(frame_rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
  74. { "duration", "set video duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  75. { "d", "set video duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  76. { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1}, 0, INT_MAX, FLAGS },
  77. /* only used by color */
  78. { "color", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  79. { "c", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  80. /* only used by testsrc */
  81. { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
  82. { "n", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
  83. { NULL },
  84. };
  85. static av_cold int init(AVFilterContext *ctx, const char *args)
  86. {
  87. TestSourceContext *test = ctx->priv;
  88. int ret = 0;
  89. av_opt_set_defaults(test);
  90. if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0)
  91. return ret;
  92. if ((ret = av_parse_video_rate(&test->frame_rate, test->frame_rate_str)) < 0) {
  93. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->frame_rate_str);
  94. return ret;
  95. }
  96. test->duration = -1;
  97. if (test->duration_str &&
  98. (ret = av_parse_time(&test->duration, test->duration_str, 1)) < 0) {
  99. av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration_str);
  100. return ret;
  101. }
  102. if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
  103. av_log(ctx, AV_LOG_WARNING,
  104. "Option 'decimals' is ignored with source '%s'\n",
  105. ctx->filter->name);
  106. }
  107. if (test->color_str) {
  108. if (!strcmp(ctx->filter->name, "color")) {
  109. ret = av_parse_color(test->color_rgba, test->color_str, -1, ctx);
  110. if (ret < 0)
  111. return ret;
  112. } else {
  113. av_log(ctx, AV_LOG_WARNING,
  114. "Option 'color' is ignored with source '%s'\n",
  115. ctx->filter->name);
  116. }
  117. }
  118. test->time_base = av_inv_q(test->frame_rate);
  119. test->nb_frame = 0;
  120. test->pts = 0;
  121. av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
  122. test->w, test->h, test->frame_rate.num, test->frame_rate.den,
  123. test->duration < 0 ? -1 : (double)test->duration/1000000,
  124. test->sar.num, test->sar.den);
  125. return 0;
  126. }
  127. static av_cold void uninit(AVFilterContext *ctx)
  128. {
  129. TestSourceContext *test = ctx->priv;
  130. av_opt_free(test);
  131. 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->frame_rate = test->frame_rate;
  140. outlink->time_base = test->time_base;
  141. return 0;
  142. }
  143. static int request_frame(AVFilterLink *outlink)
  144. {
  145. TestSourceContext *test = outlink->src->priv;
  146. AVFilterBufferRef *outpicref;
  147. if (test->duration >= 0 &&
  148. av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
  149. return AVERROR_EOF;
  150. if (test->draw_once) {
  151. if (!test->picref) {
  152. test->picref =
  153. ff_get_video_buffer(outlink, 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. return ff_filter_frame(outlink, outpicref);
  175. }
  176. #if CONFIG_COLOR_FILTER
  177. #define color_options options
  178. AVFILTER_DEFINE_CLASS(color);
  179. static void color_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  180. {
  181. TestSourceContext *test = ctx->priv;
  182. ff_fill_rectangle(&test->draw, &test->color,
  183. picref->data, picref->linesize,
  184. 0, 0, test->w, test->h);
  185. }
  186. static av_cold int color_init(AVFilterContext *ctx, const char *args)
  187. {
  188. TestSourceContext *test = ctx->priv;
  189. test->class = &color_class;
  190. test->fill_picture_fn = color_fill_picture;
  191. test->draw_once = 1;
  192. av_opt_set(test, "color", "black", 0);
  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. static const AVFilterPad color_outputs[] = {
  218. {
  219. .name = "default",
  220. .type = AVMEDIA_TYPE_VIDEO,
  221. .request_frame = request_frame,
  222. .config_props = color_config_props,
  223. },
  224. { NULL }
  225. };
  226. AVFilter avfilter_vsrc_color = {
  227. .name = "color",
  228. .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
  229. .priv_size = sizeof(TestSourceContext),
  230. .init = color_init,
  231. .uninit = uninit,
  232. .query_formats = color_query_formats,
  233. .inputs = NULL,
  234. .outputs = color_outputs,
  235. .priv_class = &color_class,
  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. static const AVFilterPad nullsrc_outputs[] = {
  250. {
  251. .name = "default",
  252. .type = AVMEDIA_TYPE_VIDEO,
  253. .request_frame = request_frame,
  254. .config_props = config_props,
  255. },
  256. { NULL },
  257. };
  258. AVFilter avfilter_vsrc_nullsrc = {
  259. .name = "nullsrc",
  260. .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
  261. .init = nullsrc_init,
  262. .uninit = uninit,
  263. .priv_size = sizeof(TestSourceContext),
  264. .inputs = NULL,
  265. .outputs = nullsrc_outputs,
  266. .priv_class = &nullsrc_class,
  267. };
  268. #endif /* CONFIG_NULLSRC_FILTER */
  269. #if CONFIG_TESTSRC_FILTER
  270. #define testsrc_options options
  271. AVFILTER_DEFINE_CLASS(testsrc);
  272. /**
  273. * Fill a rectangle with value val.
  274. *
  275. * @param val the RGB value to set
  276. * @param dst pointer to the destination buffer to fill
  277. * @param dst_linesize linesize of destination
  278. * @param segment_width width of the segment
  279. * @param x horizontal coordinate where to draw the rectangle in the destination buffer
  280. * @param y horizontal coordinate where to draw the rectangle in the destination buffer
  281. * @param w width of the rectangle to draw, expressed as a number of segment_width units
  282. * @param h height of the rectangle to draw, expressed as a number of segment_width units
  283. */
  284. static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
  285. unsigned x, unsigned y, unsigned w, unsigned h)
  286. {
  287. int i;
  288. int step = 3;
  289. dst += segment_width * (step * x + y * dst_linesize);
  290. w *= segment_width * step;
  291. h *= segment_width;
  292. for (i = 0; i < h; i++) {
  293. memset(dst, val, w);
  294. dst += dst_linesize;
  295. }
  296. }
  297. static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
  298. unsigned segment_width)
  299. {
  300. #define TOP_HBAR 1
  301. #define MID_HBAR 2
  302. #define BOT_HBAR 4
  303. #define LEFT_TOP_VBAR 8
  304. #define LEFT_BOT_VBAR 16
  305. #define RIGHT_TOP_VBAR 32
  306. #define RIGHT_BOT_VBAR 64
  307. struct {
  308. int x, y, w, h;
  309. } segments[] = {
  310. { 1, 0, 5, 1 }, /* TOP_HBAR */
  311. { 1, 6, 5, 1 }, /* MID_HBAR */
  312. { 1, 12, 5, 1 }, /* BOT_HBAR */
  313. { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
  314. { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
  315. { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
  316. { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
  317. };
  318. static const unsigned char masks[10] = {
  319. /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  320. /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  321. /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
  322. /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  323. /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  324. /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
  325. /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
  326. /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  327. /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  328. /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  329. };
  330. unsigned mask = masks[digit];
  331. int i;
  332. draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
  333. for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
  334. if (mask & (1<<i))
  335. draw_rectangle(255, dst, dst_linesize, segment_width,
  336. segments[i].x, segments[i].y, segments[i].w, segments[i].h);
  337. }
  338. #define GRADIENT_SIZE (6 * 256)
  339. static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  340. {
  341. TestSourceContext *test = ctx->priv;
  342. uint8_t *p, *p0;
  343. int x, y;
  344. int color, color_rest;
  345. int icolor;
  346. int radius;
  347. int quad0, quad;
  348. int dquad_x, dquad_y;
  349. int grad, dgrad, rgrad, drgrad;
  350. int seg_size;
  351. int second;
  352. int i;
  353. uint8_t *data = picref->data[0];
  354. int width = picref->video->w;
  355. int height = picref->video->h;
  356. /* draw colored bars and circle */
  357. radius = (width + height) / 4;
  358. quad0 = width * width / 4 + height * height / 4 - radius * radius;
  359. dquad_y = 1 - height;
  360. p0 = data;
  361. for (y = 0; y < height; y++) {
  362. p = p0;
  363. color = 0;
  364. color_rest = 0;
  365. quad = quad0;
  366. dquad_x = 1 - width;
  367. for (x = 0; x < width; x++) {
  368. icolor = color;
  369. if (quad < 0)
  370. icolor ^= 7;
  371. quad += dquad_x;
  372. dquad_x += 2;
  373. *(p++) = icolor & 1 ? 255 : 0;
  374. *(p++) = icolor & 2 ? 255 : 0;
  375. *(p++) = icolor & 4 ? 255 : 0;
  376. color_rest += 8;
  377. if (color_rest >= width) {
  378. color_rest -= width;
  379. color++;
  380. }
  381. }
  382. quad0 += dquad_y;
  383. dquad_y += 2;
  384. p0 += picref->linesize[0];
  385. }
  386. /* draw sliding color line */
  387. p0 = p = data + picref->linesize[0] * height * 3/4;
  388. grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
  389. GRADIENT_SIZE;
  390. rgrad = 0;
  391. dgrad = GRADIENT_SIZE / width;
  392. drgrad = GRADIENT_SIZE % width;
  393. for (x = 0; x < width; x++) {
  394. *(p++) =
  395. grad < 256 || grad >= 5 * 256 ? 255 :
  396. grad >= 2 * 256 && grad < 4 * 256 ? 0 :
  397. grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
  398. *(p++) =
  399. grad >= 4 * 256 ? 0 :
  400. grad >= 1 * 256 && grad < 3 * 256 ? 255 :
  401. grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
  402. *(p++) =
  403. grad < 2 * 256 ? 0 :
  404. grad >= 3 * 256 && grad < 5 * 256 ? 255 :
  405. grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
  406. grad += dgrad;
  407. rgrad += drgrad;
  408. if (rgrad >= GRADIENT_SIZE) {
  409. grad++;
  410. rgrad -= GRADIENT_SIZE;
  411. }
  412. if (grad >= GRADIENT_SIZE)
  413. grad -= GRADIENT_SIZE;
  414. }
  415. p = p0;
  416. for (y = height / 8; y > 0; y--) {
  417. memcpy(p+picref->linesize[0], p, 3 * width);
  418. p += picref->linesize[0];
  419. }
  420. /* draw digits */
  421. seg_size = width / 80;
  422. if (seg_size >= 1 && height >= 13 * seg_size) {
  423. double time = av_q2d(test->time_base) * test->nb_frame *
  424. pow(10, test->nb_decimals);
  425. if (time > INT_MAX)
  426. return;
  427. second = (int)time;
  428. x = width - (width - seg_size * 64) / 2;
  429. y = (height - seg_size * 13) / 2;
  430. p = data + (x*3 + y * picref->linesize[0]);
  431. for (i = 0; i < 8; i++) {
  432. p -= 3 * 8 * seg_size;
  433. draw_digit(second % 10, p, picref->linesize[0], seg_size);
  434. second /= 10;
  435. if (second == 0)
  436. break;
  437. }
  438. }
  439. }
  440. static av_cold int test_init(AVFilterContext *ctx, const char *args)
  441. {
  442. TestSourceContext *test = ctx->priv;
  443. test->class = &testsrc_class;
  444. test->fill_picture_fn = test_fill_picture;
  445. return init(ctx, args);
  446. }
  447. static int test_query_formats(AVFilterContext *ctx)
  448. {
  449. static const enum AVPixelFormat pix_fmts[] = {
  450. AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
  451. };
  452. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  453. return 0;
  454. }
  455. static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
  456. {
  457. .name = "default",
  458. .type = AVMEDIA_TYPE_VIDEO,
  459. .request_frame = request_frame,
  460. .config_props = config_props,
  461. },
  462. { NULL }
  463. };
  464. AVFilter avfilter_vsrc_testsrc = {
  465. .name = "testsrc",
  466. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  467. .priv_size = sizeof(TestSourceContext),
  468. .init = test_init,
  469. .uninit = uninit,
  470. .query_formats = test_query_formats,
  471. .inputs = NULL,
  472. .outputs = avfilter_vsrc_testsrc_outputs,
  473. .priv_class = &testsrc_class,
  474. };
  475. #endif /* CONFIG_TESTSRC_FILTER */
  476. #if CONFIG_RGBTESTSRC_FILTER
  477. #define rgbtestsrc_options options
  478. AVFILTER_DEFINE_CLASS(rgbtestsrc);
  479. #define R 0
  480. #define G 1
  481. #define B 2
  482. #define A 3
  483. static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
  484. int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
  485. uint8_t rgba_map[4])
  486. {
  487. int32_t v;
  488. uint8_t *p;
  489. switch (fmt) {
  490. case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
  491. case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
  492. case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
  493. case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
  494. case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
  495. case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
  496. case AV_PIX_FMT_RGB24:
  497. case AV_PIX_FMT_BGR24:
  498. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  499. p = dst + 3*x + y*dst_linesize;
  500. AV_WL24(p, v);
  501. break;
  502. case AV_PIX_FMT_RGBA:
  503. case AV_PIX_FMT_BGRA:
  504. case AV_PIX_FMT_ARGB:
  505. case AV_PIX_FMT_ABGR:
  506. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
  507. p = dst + 4*x + y*dst_linesize;
  508. AV_WL32(p, v);
  509. break;
  510. }
  511. }
  512. static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  513. {
  514. TestSourceContext *test = ctx->priv;
  515. int x, y, w = picref->video->w, h = picref->video->h;
  516. for (y = 0; y < h; y++) {
  517. for (x = 0; x < picref->video->w; x++) {
  518. int c = 256*x/w;
  519. int r = 0, g = 0, b = 0;
  520. if (3*y < h ) r = c;
  521. else if (3*y < 2*h) g = c;
  522. else b = c;
  523. rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
  524. ctx->outputs[0]->format, test->rgba_map);
  525. }
  526. }
  527. }
  528. static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
  529. {
  530. TestSourceContext *test = ctx->priv;
  531. test->draw_once = 1;
  532. test->class = &rgbtestsrc_class;
  533. test->fill_picture_fn = rgbtest_fill_picture;
  534. return init(ctx, args);
  535. }
  536. static int rgbtest_query_formats(AVFilterContext *ctx)
  537. {
  538. static const enum AVPixelFormat pix_fmts[] = {
  539. AV_PIX_FMT_RGBA, AV_PIX_FMT_ARGB, AV_PIX_FMT_BGRA, AV_PIX_FMT_ABGR,
  540. AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
  541. AV_PIX_FMT_RGB444, AV_PIX_FMT_BGR444,
  542. AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565,
  543. AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555,
  544. AV_PIX_FMT_NONE
  545. };
  546. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  547. return 0;
  548. }
  549. static int rgbtest_config_props(AVFilterLink *outlink)
  550. {
  551. TestSourceContext *test = outlink->src->priv;
  552. ff_fill_rgba_map(test->rgba_map, outlink->format);
  553. return config_props(outlink);
  554. }
  555. static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
  556. {
  557. .name = "default",
  558. .type = AVMEDIA_TYPE_VIDEO,
  559. .request_frame = request_frame,
  560. .config_props = rgbtest_config_props,
  561. },
  562. { NULL }
  563. };
  564. AVFilter avfilter_vsrc_rgbtestsrc = {
  565. .name = "rgbtestsrc",
  566. .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
  567. .priv_size = sizeof(TestSourceContext),
  568. .init = rgbtest_init,
  569. .uninit = uninit,
  570. .query_formats = rgbtest_query_formats,
  571. .inputs = NULL,
  572. .outputs = avfilter_vsrc_rgbtestsrc_outputs,
  573. .priv_class = &rgbtestsrc_class,
  574. };
  575. #endif /* CONFIG_RGBTESTSRC_FILTER */
  576. #if CONFIG_SMPTEBARS_FILTER
  577. #define smptebars_options options
  578. AVFILTER_DEFINE_CLASS(smptebars);
  579. static const uint8_t rainbow[7][4] = {
  580. { 191, 191, 191, 255 }, /* gray */
  581. { 191, 191, 0, 255 }, /* yellow */
  582. { 0, 191, 191, 255 }, /* cyan */
  583. { 0, 191, 0, 255 }, /* green */
  584. { 191, 0, 191, 255 }, /* magenta */
  585. { 191, 0, 0, 255 }, /* red */
  586. { 0, 0, 191, 255 }, /* blue */
  587. };
  588. static const uint8_t wobnair[7][4] = {
  589. { 0, 0, 191, 255 }, /* blue */
  590. { 19, 19, 19, 255 }, /* 7.5% intensity black */
  591. { 191, 0, 191, 255 }, /* magenta */
  592. { 19, 19, 19, 255 }, /* 7.5% intensity black */
  593. { 0, 191, 191, 255 }, /* cyan */
  594. { 19, 19, 19, 255 }, /* 7.5% intensity black */
  595. { 191, 191, 191, 255 }, /* gray */
  596. };
  597. static const uint8_t white[4] = { 255, 255, 255, 255 };
  598. static const uint8_t black[4] = { 19, 19, 19, 255 }; /* 7.5% intensity black */
  599. /* pluge pulses */
  600. static const uint8_t neg4ire[4] = { 9, 9, 9, 255 }; /* 3.5% intensity black */
  601. static const uint8_t pos4ire[4] = { 29, 29, 29, 255 }; /* 11.5% intensity black */
  602. /* fudged Q/-I */
  603. static const uint8_t i_pixel[4] = { 0, 68, 130, 255 };
  604. static const uint8_t q_pixel[4] = { 67, 0, 130, 255 };
  605. static void smptebars_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  606. {
  607. TestSourceContext *test = ctx->priv;
  608. FFDrawColor color;
  609. int r_w, r_h, w_h, p_w, p_h, i, x = 0;
  610. r_w = (test->w + 6) / 7;
  611. r_h = test->h * 2 / 3;
  612. w_h = test->h * 3 / 4 - r_h;
  613. p_w = r_w * 5 / 4;
  614. p_h = test->h - w_h - r_h;
  615. #define DRAW_COLOR(rgba, x, y, w, h) \
  616. ff_draw_color(&test->draw, &color, rgba); \
  617. ff_fill_rectangle(&test->draw, &color, \
  618. picref->data, picref->linesize, x, y, w, h) \
  619. for (i = 0; i < 7; i++) {
  620. DRAW_COLOR(rainbow[i], x, 0, FFMIN(r_w, test->w - x), r_h);
  621. DRAW_COLOR(wobnair[i], x, r_h, FFMIN(r_w, test->w - x), w_h);
  622. x += r_w;
  623. }
  624. x = 0;
  625. DRAW_COLOR(i_pixel, x, r_h + w_h, p_w, p_h);
  626. x += p_w;
  627. DRAW_COLOR(white, x, r_h + w_h, p_w, p_h);
  628. x += p_w;
  629. DRAW_COLOR(q_pixel, x, r_h + w_h, p_w, p_h);
  630. x += p_w;
  631. DRAW_COLOR(black, x, r_h + w_h, 5 * r_w - x, p_h);
  632. x += 5 * r_w - x;
  633. DRAW_COLOR(neg4ire, x, r_h + w_h, r_w / 3, p_h);
  634. x += r_w / 3;
  635. DRAW_COLOR(black, x, r_h + w_h, r_w / 3, p_h);
  636. x += r_w / 3;
  637. DRAW_COLOR(pos4ire, x, r_h + w_h, r_w / 3, p_h);
  638. x += r_w / 3;
  639. DRAW_COLOR(black, x, r_h + w_h, test->w - x, p_h);
  640. }
  641. static av_cold int smptebars_init(AVFilterContext *ctx, const char *args)
  642. {
  643. TestSourceContext *test = ctx->priv;
  644. test->class = &smptebars_class;
  645. test->fill_picture_fn = smptebars_fill_picture;
  646. test->draw_once = 1;
  647. return init(ctx, args);
  648. }
  649. static int smptebars_query_formats(AVFilterContext *ctx)
  650. {
  651. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  652. return 0;
  653. }
  654. static int smptebars_config_props(AVFilterLink *outlink)
  655. {
  656. AVFilterContext *ctx = outlink->src;
  657. TestSourceContext *test = ctx->priv;
  658. ff_draw_init(&test->draw, outlink->format, 0);
  659. return config_props(outlink);
  660. }
  661. static const AVFilterPad smptebars_outputs[] = {
  662. {
  663. .name = "default",
  664. .type = AVMEDIA_TYPE_VIDEO,
  665. .request_frame = request_frame,
  666. .config_props = smptebars_config_props,
  667. },
  668. { NULL }
  669. };
  670. AVFilter avfilter_vsrc_smptebars = {
  671. .name = "smptebars",
  672. .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
  673. .priv_size = sizeof(TestSourceContext),
  674. .init = smptebars_init,
  675. .uninit = uninit,
  676. .query_formats = smptebars_query_formats,
  677. .inputs = NULL,
  678. .outputs = smptebars_outputs,
  679. .priv_class = &smptebars_class,
  680. };
  681. #endif /* CONFIG_SMPTEBARS_FILTER */