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.

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