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.

816 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->class = &testsrc_class;
  442. test->fill_picture_fn = test_fill_picture;
  443. av_opt_set_defaults(test);
  444. return init(ctx, args);
  445. }
  446. static int test_query_formats(AVFilterContext *ctx)
  447. {
  448. static const enum AVPixelFormat pix_fmts[] = {
  449. AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
  450. };
  451. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  452. return 0;
  453. }
  454. static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
  455. {
  456. .name = "default",
  457. .type = AVMEDIA_TYPE_VIDEO,
  458. .request_frame = request_frame,
  459. .config_props = config_props,
  460. },
  461. { NULL }
  462. };
  463. AVFilter avfilter_vsrc_testsrc = {
  464. .name = "testsrc",
  465. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  466. .priv_size = sizeof(TestSourceContext),
  467. .init = test_init,
  468. .uninit = uninit,
  469. .query_formats = test_query_formats,
  470. .inputs = NULL,
  471. .outputs = avfilter_vsrc_testsrc_outputs,
  472. .priv_class = &testsrc_class,
  473. };
  474. #endif /* CONFIG_TESTSRC_FILTER */
  475. #if CONFIG_RGBTESTSRC_FILTER
  476. #define rgbtestsrc_options options
  477. AVFILTER_DEFINE_CLASS(rgbtestsrc);
  478. #define R 0
  479. #define G 1
  480. #define B 2
  481. #define A 3
  482. static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
  483. int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
  484. uint8_t rgba_map[4])
  485. {
  486. int32_t v;
  487. uint8_t *p;
  488. switch (fmt) {
  489. case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
  490. case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
  491. case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
  492. case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
  493. case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
  494. case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
  495. case AV_PIX_FMT_RGB24:
  496. case AV_PIX_FMT_BGR24:
  497. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  498. p = dst + 3*x + y*dst_linesize;
  499. AV_WL24(p, v);
  500. break;
  501. case AV_PIX_FMT_RGBA:
  502. case AV_PIX_FMT_BGRA:
  503. case AV_PIX_FMT_ARGB:
  504. case AV_PIX_FMT_ABGR:
  505. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
  506. p = dst + 4*x + y*dst_linesize;
  507. AV_WL32(p, v);
  508. break;
  509. }
  510. }
  511. static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  512. {
  513. TestSourceContext *test = ctx->priv;
  514. int x, y, w = frame->width, h = frame->height;
  515. for (y = 0; y < h; y++) {
  516. for (x = 0; x < w; x++) {
  517. int c = 256*x/w;
  518. int r = 0, g = 0, b = 0;
  519. if (3*y < h ) r = c;
  520. else if (3*y < 2*h) g = c;
  521. else b = c;
  522. rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
  523. ctx->outputs[0]->format, test->rgba_map);
  524. }
  525. }
  526. }
  527. static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
  528. {
  529. TestSourceContext *test = ctx->priv;
  530. test->draw_once = 1;
  531. test->class = &rgbtestsrc_class;
  532. test->fill_picture_fn = rgbtest_fill_picture;
  533. av_opt_set_defaults(test);
  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 inline draw_bar(TestSourceContext *test, const uint8_t *color,
  606. unsigned x, unsigned y, unsigned w, unsigned h,
  607. AVFrame *frame)
  608. {
  609. FFDrawColor draw_color;
  610. x = FFMIN(x, test->w - 1);
  611. y = FFMIN(y, test->h - 1);
  612. w = FFMIN(w, test->w - x);
  613. h = FFMIN(h, test->h - y);
  614. av_assert0(x + w <= test->w);
  615. av_assert0(y + h <= test->h);
  616. ff_draw_color(&test->draw, &draw_color, color);
  617. ff_fill_rectangle(&test->draw, &draw_color,
  618. frame->data, frame->linesize, x, y, w, h);
  619. }
  620. static void smptebars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
  621. {
  622. TestSourceContext *test = ctx->priv;
  623. int r_w, r_h, w_h, p_w, p_h, i, tmp, x = 0;
  624. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
  625. r_w = FFALIGN((test->w + 6) / 7, 1 << pixdesc->log2_chroma_w);
  626. r_h = FFALIGN(test->h * 2 / 3, 1 << pixdesc->log2_chroma_h);
  627. w_h = FFALIGN(test->h * 3 / 4 - r_h, 1 << pixdesc->log2_chroma_h);
  628. p_w = FFALIGN(r_w * 5 / 4, 1 << pixdesc->log2_chroma_w);
  629. p_h = test->h - w_h - r_h;
  630. for (i = 0; i < 7; i++) {
  631. draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
  632. draw_bar(test, wobnair[i], x, r_h, r_w, w_h, picref);
  633. x += r_w;
  634. }
  635. x = 0;
  636. draw_bar(test, i_pixel, x, r_h + w_h, p_w, p_h, picref);
  637. x += p_w;
  638. draw_bar(test, white, x, r_h + w_h, p_w, p_h, picref);
  639. x += p_w;
  640. draw_bar(test, q_pixel, x, r_h + w_h, p_w, p_h, picref);
  641. x += p_w;
  642. tmp = FFALIGN(5 * r_w - x, 1 << pixdesc->log2_chroma_w);
  643. draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
  644. x += tmp;
  645. tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
  646. draw_bar(test, neg4ire, x, r_h + w_h, tmp, p_h, picref);
  647. x += tmp;
  648. draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
  649. x += tmp;
  650. draw_bar(test, pos4ire, x, r_h + w_h, tmp, p_h, picref);
  651. x += tmp;
  652. draw_bar(test, black, x, r_h + w_h, test->w - x, p_h, picref);
  653. }
  654. static av_cold int smptebars_init(AVFilterContext *ctx, const char *args)
  655. {
  656. TestSourceContext *test = ctx->priv;
  657. test->class = &smptebars_class;
  658. test->fill_picture_fn = smptebars_fill_picture;
  659. test->draw_once = 1;
  660. av_opt_set_defaults(test);
  661. return init(ctx, args);
  662. }
  663. static int smptebars_query_formats(AVFilterContext *ctx)
  664. {
  665. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  666. return 0;
  667. }
  668. static int smptebars_config_props(AVFilterLink *outlink)
  669. {
  670. AVFilterContext *ctx = outlink->src;
  671. TestSourceContext *test = ctx->priv;
  672. ff_draw_init(&test->draw, outlink->format, 0);
  673. return config_props(outlink);
  674. }
  675. static const AVFilterPad smptebars_outputs[] = {
  676. {
  677. .name = "default",
  678. .type = AVMEDIA_TYPE_VIDEO,
  679. .request_frame = request_frame,
  680. .config_props = smptebars_config_props,
  681. },
  682. { NULL }
  683. };
  684. AVFilter avfilter_vsrc_smptebars = {
  685. .name = "smptebars",
  686. .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
  687. .priv_size = sizeof(TestSourceContext),
  688. .init = smptebars_init,
  689. .uninit = uninit,
  690. .query_formats = smptebars_query_formats,
  691. .inputs = NULL,
  692. .outputs = smptebars_outputs,
  693. .priv_class = &smptebars_class,
  694. };
  695. #endif /* CONFIG_SMPTEBARS_FILTER */