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.

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