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.

812 lines
26KB

  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/avassert.h"
  36. #include "libavutil/common.h"
  37. #include "libavutil/opt.h"
  38. #include "libavutil/imgutils.h"
  39. #include "libavutil/intreadwrite.h"
  40. #include "libavutil/parseutils.h"
  41. #include "avfilter.h"
  42. #include "drawutils.h"
  43. #include "formats.h"
  44. #include "internal.h"
  45. #include "video.h"
  46. typedef struct {
  47. const AVClass *class;
  48. int w, h;
  49. unsigned int nb_frame;
  50. AVRational time_base, frame_rate;
  51. int64_t pts;
  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. AVFrame *picref; ///< cached reference containing the painted picture
  58. void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
  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. #define COMMON_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), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
  73. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.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. static const AVOption color_options[] = {
  78. /* only used by color */
  79. { "color", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
  80. { "c", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
  81. COMMON_OPTIONS
  82. { NULL },
  83. };
  84. static const AVOption options[] = {
  85. COMMON_OPTIONS
  86. /* only used by testsrc */
  87. { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, 0, 17, FLAGS },
  88. { "n", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, 0, 17, FLAGS },
  89. { NULL },
  90. };
  91. static av_cold int init(AVFilterContext *ctx, const char *args)
  92. {
  93. TestSourceContext *test = ctx->priv;
  94. int ret = 0;
  95. if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0)
  96. return ret;
  97. test->duration = -1;
  98. if (test->duration_str &&
  99. (ret = av_parse_time(&test->duration, test->duration_str, 1)) < 0) {
  100. av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration_str);
  101. return ret;
  102. }
  103. if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
  104. av_log(ctx, AV_LOG_WARNING,
  105. "Option 'decimals' is ignored with source '%s'\n",
  106. ctx->filter->name);
  107. }
  108. if (test->color_str) {
  109. if (!strcmp(ctx->filter->name, "color")) {
  110. ret = av_parse_color(test->color_rgba, test->color_str, -1, ctx);
  111. if (ret < 0)
  112. return ret;
  113. } else {
  114. av_log(ctx, AV_LOG_WARNING,
  115. "Option 'color' is ignored with source '%s'\n",
  116. ctx->filter->name);
  117. }
  118. }
  119. test->time_base = av_inv_q(test->frame_rate);
  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, test->frame_rate.num, test->frame_rate.den,
  124. test->duration < 0 ? -1 : (double)test->duration/1000000,
  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. av_frame_free(&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->frame_rate = test->frame_rate;
  141. outlink->time_base = test->time_base;
  142. return 0;
  143. }
  144. static int request_frame(AVFilterLink *outlink)
  145. {
  146. TestSourceContext *test = outlink->src->priv;
  147. AVFrame *frame;
  148. if (test->duration >= 0 &&
  149. av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
  150. return AVERROR_EOF;
  151. if (test->draw_once) {
  152. if (!test->picref) {
  153. test->picref =
  154. ff_get_video_buffer(outlink, test->w, test->h);
  155. if (!test->picref)
  156. return AVERROR(ENOMEM);
  157. test->fill_picture_fn(outlink->src, test->picref);
  158. }
  159. frame = av_frame_clone(test->picref);
  160. } else
  161. frame = ff_get_video_buffer(outlink, test->w, test->h);
  162. if (!frame)
  163. return AVERROR(ENOMEM);
  164. frame->pts = test->pts;
  165. frame->key_frame = 1;
  166. frame->interlaced_frame = 0;
  167. frame->pict_type = AV_PICTURE_TYPE_I;
  168. frame->sample_aspect_ratio = test->sar;
  169. if (!test->draw_once)
  170. test->fill_picture_fn(outlink->src, frame);
  171. test->pts++;
  172. test->nb_frame++;
  173. return ff_filter_frame(outlink, frame);
  174. }
  175. #if CONFIG_COLOR_FILTER
  176. AVFILTER_DEFINE_CLASS(color);
  177. static void color_fill_picture(AVFilterContext *ctx, AVFrame *picref)
  178. {
  179. TestSourceContext *test = ctx->priv;
  180. ff_fill_rectangle(&test->draw, &test->color,
  181. picref->data, picref->linesize,
  182. 0, 0, test->w, test->h);
  183. }
  184. static av_cold int color_init(AVFilterContext *ctx, const char *args)
  185. {
  186. TestSourceContext *test = ctx->priv;
  187. test->fill_picture_fn = color_fill_picture;
  188. test->draw_once = 1;
  189. return init(ctx, NULL);
  190. }
  191. static int color_query_formats(AVFilterContext *ctx)
  192. {
  193. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  194. return 0;
  195. }
  196. static int color_config_props(AVFilterLink *inlink)
  197. {
  198. AVFilterContext *ctx = inlink->src;
  199. TestSourceContext *test = ctx->priv;
  200. int ret;
  201. ff_draw_init(&test->draw, inlink->format, 0);
  202. ff_draw_color(&test->draw, &test->color, test->color_rgba);
  203. test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
  204. test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
  205. if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
  206. return AVERROR(EINVAL);
  207. if ((ret = config_props(inlink)) < 0)
  208. return ret;
  209. av_log(ctx, AV_LOG_VERBOSE, "color:0x%02x%02x%02x%02x\n",
  210. test->color_rgba[0], test->color_rgba[1], test->color_rgba[2], test->color_rgba[3]);
  211. return 0;
  212. }
  213. static const AVFilterPad color_outputs[] = {
  214. {
  215. .name = "default",
  216. .type = AVMEDIA_TYPE_VIDEO,
  217. .request_frame = request_frame,
  218. .config_props = color_config_props,
  219. },
  220. { NULL }
  221. };
  222. AVFilter avfilter_vsrc_color = {
  223. .name = "color",
  224. .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
  225. .priv_class = &color_class,
  226. .priv_size = sizeof(TestSourceContext),
  227. .init = color_init,
  228. .uninit = uninit,
  229. .query_formats = color_query_formats,
  230. .inputs = NULL,
  231. .outputs = color_outputs,
  232. };
  233. #endif /* CONFIG_COLOR_FILTER */
  234. #if CONFIG_NULLSRC_FILTER
  235. #define nullsrc_options options
  236. AVFILTER_DEFINE_CLASS(nullsrc);
  237. static void nullsrc_fill_picture(AVFilterContext *ctx, AVFrame *picref) { }
  238. static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args)
  239. {
  240. TestSourceContext *test = ctx->priv;
  241. test->fill_picture_fn = nullsrc_fill_picture;
  242. return init(ctx, NULL);
  243. }
  244. static const AVFilterPad nullsrc_outputs[] = {
  245. {
  246. .name = "default",
  247. .type = AVMEDIA_TYPE_VIDEO,
  248. .request_frame = request_frame,
  249. .config_props = config_props,
  250. },
  251. { NULL },
  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. .priv_class = &nullsrc_class,
  260. .inputs = NULL,
  261. .outputs = nullsrc_outputs,
  262. };
  263. #endif /* CONFIG_NULLSRC_FILTER */
  264. #if CONFIG_TESTSRC_FILTER
  265. #define testsrc_options options
  266. AVFILTER_DEFINE_CLASS(testsrc);
  267. /**
  268. * Fill a rectangle with value val.
  269. *
  270. * @param val the RGB value to set
  271. * @param dst pointer to the destination buffer to fill
  272. * @param dst_linesize linesize of destination
  273. * @param segment_width width of the segment
  274. * @param x horizontal coordinate where to draw the rectangle in the destination buffer
  275. * @param y horizontal coordinate where to draw the rectangle in the destination buffer
  276. * @param w width of the rectangle to draw, expressed as a number of segment_width units
  277. * @param h height of the rectangle to draw, expressed as a number of segment_width units
  278. */
  279. static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
  280. unsigned x, unsigned y, unsigned w, unsigned h)
  281. {
  282. int i;
  283. int step = 3;
  284. dst += segment_width * (step * x + y * dst_linesize);
  285. w *= segment_width * step;
  286. h *= segment_width;
  287. for (i = 0; i < h; i++) {
  288. memset(dst, val, w);
  289. dst += dst_linesize;
  290. }
  291. }
  292. static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
  293. unsigned segment_width)
  294. {
  295. #define TOP_HBAR 1
  296. #define MID_HBAR 2
  297. #define BOT_HBAR 4
  298. #define LEFT_TOP_VBAR 8
  299. #define LEFT_BOT_VBAR 16
  300. #define RIGHT_TOP_VBAR 32
  301. #define RIGHT_BOT_VBAR 64
  302. struct {
  303. int x, y, w, h;
  304. } segments[] = {
  305. { 1, 0, 5, 1 }, /* TOP_HBAR */
  306. { 1, 6, 5, 1 }, /* MID_HBAR */
  307. { 1, 12, 5, 1 }, /* BOT_HBAR */
  308. { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
  309. { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
  310. { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
  311. { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
  312. };
  313. static const unsigned char masks[10] = {
  314. /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  315. /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  316. /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
  317. /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  318. /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  319. /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
  320. /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
  321. /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  322. /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  323. /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  324. };
  325. unsigned mask = masks[digit];
  326. int i;
  327. draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
  328. for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
  329. if (mask & (1<<i))
  330. draw_rectangle(255, dst, dst_linesize, segment_width,
  331. segments[i].x, segments[i].y, segments[i].w, segments[i].h);
  332. }
  333. #define GRADIENT_SIZE (6 * 256)
  334. static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  335. {
  336. TestSourceContext *test = ctx->priv;
  337. uint8_t *p, *p0;
  338. int x, y;
  339. int color, color_rest;
  340. int icolor;
  341. int radius;
  342. int quad0, quad;
  343. int dquad_x, dquad_y;
  344. int grad, dgrad, rgrad, drgrad;
  345. int seg_size;
  346. int second;
  347. int i;
  348. uint8_t *data = frame->data[0];
  349. int width = frame->width;
  350. int height = frame->height;
  351. /* draw colored bars and circle */
  352. radius = (width + height) / 4;
  353. quad0 = width * width / 4 + height * height / 4 - radius * radius;
  354. dquad_y = 1 - height;
  355. p0 = data;
  356. for (y = 0; y < height; y++) {
  357. p = p0;
  358. color = 0;
  359. color_rest = 0;
  360. quad = quad0;
  361. dquad_x = 1 - width;
  362. for (x = 0; x < width; x++) {
  363. icolor = color;
  364. if (quad < 0)
  365. icolor ^= 7;
  366. quad += dquad_x;
  367. dquad_x += 2;
  368. *(p++) = icolor & 1 ? 255 : 0;
  369. *(p++) = icolor & 2 ? 255 : 0;
  370. *(p++) = icolor & 4 ? 255 : 0;
  371. color_rest += 8;
  372. if (color_rest >= width) {
  373. color_rest -= width;
  374. color++;
  375. }
  376. }
  377. quad0 += dquad_y;
  378. dquad_y += 2;
  379. p0 += frame->linesize[0];
  380. }
  381. /* draw sliding color line */
  382. p0 = p = data + frame->linesize[0] * height * 3/4;
  383. grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
  384. GRADIENT_SIZE;
  385. rgrad = 0;
  386. dgrad = GRADIENT_SIZE / width;
  387. drgrad = GRADIENT_SIZE % width;
  388. for (x = 0; x < width; x++) {
  389. *(p++) =
  390. grad < 256 || grad >= 5 * 256 ? 255 :
  391. grad >= 2 * 256 && grad < 4 * 256 ? 0 :
  392. grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
  393. *(p++) =
  394. grad >= 4 * 256 ? 0 :
  395. grad >= 1 * 256 && grad < 3 * 256 ? 255 :
  396. grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
  397. *(p++) =
  398. grad < 2 * 256 ? 0 :
  399. grad >= 3 * 256 && grad < 5 * 256 ? 255 :
  400. grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
  401. grad += dgrad;
  402. rgrad += drgrad;
  403. if (rgrad >= GRADIENT_SIZE) {
  404. grad++;
  405. rgrad -= GRADIENT_SIZE;
  406. }
  407. if (grad >= GRADIENT_SIZE)
  408. grad -= GRADIENT_SIZE;
  409. }
  410. p = p0;
  411. for (y = height / 8; y > 0; y--) {
  412. memcpy(p+frame->linesize[0], p, 3 * width);
  413. p += frame->linesize[0];
  414. }
  415. /* draw digits */
  416. seg_size = width / 80;
  417. if (seg_size >= 1 && height >= 13 * seg_size) {
  418. int64_t p10decimals = 1;
  419. double time = av_q2d(test->time_base) * test->nb_frame *
  420. pow(10, test->nb_decimals);
  421. if (time >= INT_MAX)
  422. return;
  423. for(x=0; x<test->nb_decimals; x++)
  424. p10decimals *= 10;
  425. second = av_rescale_rnd(test->nb_frame * test->time_base.num, p10decimals, test->time_base.den, AV_ROUND_ZERO);
  426. x = width - (width - seg_size * 64) / 2;
  427. y = (height - seg_size * 13) / 2;
  428. p = data + (x*3 + y * frame->linesize[0]);
  429. for (i = 0; i < 8; i++) {
  430. p -= 3 * 8 * seg_size;
  431. draw_digit(second % 10, p, frame->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->fill_picture_fn = test_fill_picture;
  442. return init(ctx, NULL);
  443. }
  444. static int test_query_formats(AVFilterContext *ctx)
  445. {
  446. static const enum AVPixelFormat pix_fmts[] = {
  447. AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
  448. };
  449. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  450. return 0;
  451. }
  452. static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
  453. {
  454. .name = "default",
  455. .type = AVMEDIA_TYPE_VIDEO,
  456. .request_frame = request_frame,
  457. .config_props = config_props,
  458. },
  459. { NULL }
  460. };
  461. AVFilter avfilter_vsrc_testsrc = {
  462. .name = "testsrc",
  463. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  464. .priv_size = sizeof(TestSourceContext),
  465. .priv_class = &testsrc_class,
  466. .init = test_init,
  467. .uninit = uninit,
  468. .query_formats = test_query_formats,
  469. .inputs = NULL,
  470. .outputs = avfilter_vsrc_testsrc_outputs,
  471. };
  472. #endif /* CONFIG_TESTSRC_FILTER */
  473. #if CONFIG_RGBTESTSRC_FILTER
  474. #define rgbtestsrc_options options
  475. AVFILTER_DEFINE_CLASS(rgbtestsrc);
  476. #define R 0
  477. #define G 1
  478. #define B 2
  479. #define A 3
  480. static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
  481. int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
  482. uint8_t rgba_map[4])
  483. {
  484. int32_t v;
  485. uint8_t *p;
  486. switch (fmt) {
  487. case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
  488. case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
  489. case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
  490. case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
  491. case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
  492. case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
  493. case AV_PIX_FMT_RGB24:
  494. case AV_PIX_FMT_BGR24:
  495. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  496. p = dst + 3*x + y*dst_linesize;
  497. AV_WL24(p, v);
  498. break;
  499. case AV_PIX_FMT_RGBA:
  500. case AV_PIX_FMT_BGRA:
  501. case AV_PIX_FMT_ARGB:
  502. case AV_PIX_FMT_ABGR:
  503. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
  504. p = dst + 4*x + y*dst_linesize;
  505. AV_WL32(p, v);
  506. break;
  507. }
  508. }
  509. static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  510. {
  511. TestSourceContext *test = ctx->priv;
  512. int x, y, w = frame->width, h = frame->height;
  513. for (y = 0; y < h; y++) {
  514. for (x = 0; x < w; x++) {
  515. int c = 256*x/w;
  516. int r = 0, g = 0, b = 0;
  517. if (3*y < h ) r = c;
  518. else if (3*y < 2*h) g = c;
  519. else b = c;
  520. rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
  521. ctx->outputs[0]->format, test->rgba_map);
  522. }
  523. }
  524. }
  525. static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
  526. {
  527. TestSourceContext *test = ctx->priv;
  528. test->draw_once = 1;
  529. test->fill_picture_fn = rgbtest_fill_picture;
  530. return init(ctx, NULL);
  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. .priv_class = &rgbtestsrc_class,
  565. .init = rgbtest_init,
  566. .uninit = uninit,
  567. .query_formats = rgbtest_query_formats,
  568. .inputs = NULL,
  569. .outputs = avfilter_vsrc_rgbtestsrc_outputs,
  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 inline draw_bar(TestSourceContext *test, const uint8_t *color,
  602. unsigned x, unsigned y, unsigned w, unsigned h,
  603. AVFrame *frame)
  604. {
  605. FFDrawColor draw_color;
  606. x = FFMIN(x, test->w - 1);
  607. y = FFMIN(y, test->h - 1);
  608. w = FFMIN(w, test->w - x);
  609. h = FFMIN(h, test->h - y);
  610. av_assert0(x + w <= test->w);
  611. av_assert0(y + h <= test->h);
  612. ff_draw_color(&test->draw, &draw_color, color);
  613. ff_fill_rectangle(&test->draw, &draw_color,
  614. frame->data, frame->linesize, x, y, w, h);
  615. }
  616. static void smptebars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
  617. {
  618. TestSourceContext *test = ctx->priv;
  619. int r_w, r_h, w_h, p_w, p_h, i, tmp, x = 0;
  620. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
  621. r_w = FFALIGN((test->w + 6) / 7, 1 << pixdesc->log2_chroma_w);
  622. r_h = FFALIGN(test->h * 2 / 3, 1 << pixdesc->log2_chroma_h);
  623. w_h = FFALIGN(test->h * 3 / 4 - r_h, 1 << pixdesc->log2_chroma_h);
  624. p_w = FFALIGN(r_w * 5 / 4, 1 << pixdesc->log2_chroma_w);
  625. p_h = test->h - w_h - r_h;
  626. for (i = 0; i < 7; i++) {
  627. draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
  628. draw_bar(test, wobnair[i], x, r_h, r_w, w_h, picref);
  629. x += r_w;
  630. }
  631. x = 0;
  632. draw_bar(test, i_pixel, x, r_h + w_h, p_w, p_h, picref);
  633. x += p_w;
  634. draw_bar(test, white, x, r_h + w_h, p_w, p_h, picref);
  635. x += p_w;
  636. draw_bar(test, q_pixel, x, r_h + w_h, p_w, p_h, picref);
  637. x += p_w;
  638. tmp = FFALIGN(5 * r_w - x, 1 << pixdesc->log2_chroma_w);
  639. draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
  640. x += tmp;
  641. tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
  642. draw_bar(test, neg4ire, x, r_h + w_h, tmp, p_h, picref);
  643. x += tmp;
  644. draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
  645. x += tmp;
  646. draw_bar(test, pos4ire, x, r_h + w_h, tmp, p_h, picref);
  647. x += tmp;
  648. draw_bar(test, black, x, r_h + w_h, test->w - x, p_h, picref);
  649. }
  650. static av_cold int smptebars_init(AVFilterContext *ctx, const char *args)
  651. {
  652. TestSourceContext *test = ctx->priv;
  653. test->class = &smptebars_class;
  654. test->fill_picture_fn = smptebars_fill_picture;
  655. test->draw_once = 1;
  656. av_opt_set_defaults(test);
  657. return init(ctx, args);
  658. }
  659. static int smptebars_query_formats(AVFilterContext *ctx)
  660. {
  661. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  662. return 0;
  663. }
  664. static int smptebars_config_props(AVFilterLink *outlink)
  665. {
  666. AVFilterContext *ctx = outlink->src;
  667. TestSourceContext *test = ctx->priv;
  668. ff_draw_init(&test->draw, outlink->format, 0);
  669. return config_props(outlink);
  670. }
  671. static const AVFilterPad smptebars_outputs[] = {
  672. {
  673. .name = "default",
  674. .type = AVMEDIA_TYPE_VIDEO,
  675. .request_frame = request_frame,
  676. .config_props = smptebars_config_props,
  677. },
  678. { NULL }
  679. };
  680. AVFilter avfilter_vsrc_smptebars = {
  681. .name = "smptebars",
  682. .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
  683. .priv_size = sizeof(TestSourceContext),
  684. .init = smptebars_init,
  685. .uninit = uninit,
  686. .query_formats = smptebars_query_formats,
  687. .inputs = NULL,
  688. .outputs = smptebars_outputs,
  689. .priv_class = &smptebars_class,
  690. };
  691. #endif /* CONFIG_SMPTEBARS_FILTER */