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.

797 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/common.h"
  36. #include "libavutil/opt.h"
  37. #include "libavutil/imgutils.h"
  38. #include "libavutil/intreadwrite.h"
  39. #include "libavutil/parseutils.h"
  40. #include "avfilter.h"
  41. #include "drawutils.h"
  42. #include "formats.h"
  43. #include "internal.h"
  44. #include "video.h"
  45. typedef struct {
  46. const AVClass *class;
  47. int w, h;
  48. unsigned int nb_frame;
  49. AVRational time_base, frame_rate;
  50. int64_t pts;
  51. char *frame_rate_str; ///< video frame rate
  52. char *duration_str; ///< total duration of the generated video
  53. int64_t duration; ///< duration expressed in microseconds
  54. AVRational sar; ///< sample aspect ratio
  55. int nb_decimals;
  56. int draw_once; ///< draw only the first frame, always put out the same picture
  57. AVFilterBufferRef *picref; ///< cached reference containing the painted picture
  58. void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
  59. /* only used by color */
  60. char *color_str;
  61. FFDrawContext draw;
  62. FFDrawColor color;
  63. uint8_t color_rgba[4];
  64. /* only used by rgbtest */
  65. uint8_t rgba_map[4];
  66. } TestSourceContext;
  67. #define OFFSET(x) offsetof(TestSourceContext, x)
  68. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  69. static const AVOption options[] = {
  70. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },
  71. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },
  72. { "rate", "set video rate", OFFSET(frame_rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
  73. { "r", "set video rate", OFFSET(frame_rate_str), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
  74. { "duration", "set video duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  75. { "d", "set video duration", OFFSET(duration_str), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  76. { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1}, 0, INT_MAX, FLAGS },
  77. /* only used by color */
  78. { "color", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  79. { "c", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = NULL}, CHAR_MIN, CHAR_MAX, FLAGS },
  80. /* only used by testsrc */
  81. { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
  82. { "n", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, FLAGS },
  83. { NULL },
  84. };
  85. static av_cold int init(AVFilterContext *ctx, const char *args)
  86. {
  87. TestSourceContext *test = ctx->priv;
  88. int ret = 0;
  89. av_opt_set_defaults(test);
  90. if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0)
  91. return ret;
  92. if ((ret = av_parse_video_rate(&test->frame_rate, test->frame_rate_str)) < 0) {
  93. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->frame_rate_str);
  94. return ret;
  95. }
  96. test->duration = -1;
  97. if (test->duration_str &&
  98. (ret = av_parse_time(&test->duration, test->duration_str, 1)) < 0) {
  99. av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration_str);
  100. return ret;
  101. }
  102. if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
  103. av_log(ctx, AV_LOG_WARNING,
  104. "Option 'decimals' is ignored with source '%s'\n",
  105. ctx->filter->name);
  106. }
  107. if (test->color_str) {
  108. if (!strcmp(ctx->filter->name, "color")) {
  109. ret = av_parse_color(test->color_rgba, test->color_str, -1, ctx);
  110. if (ret < 0)
  111. return ret;
  112. } else {
  113. av_log(ctx, AV_LOG_WARNING,
  114. "Option 'color' is ignored with source '%s'\n",
  115. ctx->filter->name);
  116. }
  117. }
  118. test->time_base = av_inv_q(test->frame_rate);
  119. test->nb_frame = 0;
  120. test->pts = 0;
  121. av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
  122. test->w, test->h, test->frame_rate.num, test->frame_rate.den,
  123. test->duration < 0 ? -1 : (double)test->duration/1000000,
  124. test->sar.num, test->sar.den);
  125. return 0;
  126. }
  127. static av_cold void uninit(AVFilterContext *ctx)
  128. {
  129. TestSourceContext *test = ctx->priv;
  130. av_opt_free(test);
  131. avfilter_unref_bufferp(&test->picref);
  132. }
  133. static int config_props(AVFilterLink *outlink)
  134. {
  135. TestSourceContext *test = outlink->src->priv;
  136. outlink->w = test->w;
  137. outlink->h = test->h;
  138. outlink->sample_aspect_ratio = test->sar;
  139. outlink->frame_rate = test->frame_rate;
  140. outlink->time_base = test->time_base;
  141. return 0;
  142. }
  143. static int request_frame(AVFilterLink *outlink)
  144. {
  145. TestSourceContext *test = outlink->src->priv;
  146. AVFilterBufferRef *outpicref;
  147. if (test->duration >= 0 &&
  148. av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
  149. return AVERROR_EOF;
  150. if (test->draw_once) {
  151. if (!test->picref) {
  152. test->picref =
  153. ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_PRESERVE|AV_PERM_REUSE,
  154. test->w, test->h);
  155. if (!test->picref)
  156. return AVERROR(ENOMEM);
  157. test->fill_picture_fn(outlink->src, test->picref);
  158. }
  159. outpicref = avfilter_ref_buffer(test->picref, ~AV_PERM_WRITE);
  160. } else
  161. outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, test->w, test->h);
  162. if (!outpicref)
  163. return AVERROR(ENOMEM);
  164. outpicref->pts = test->pts;
  165. outpicref->pos = -1;
  166. outpicref->video->key_frame = 1;
  167. outpicref->video->interlaced = 0;
  168. outpicref->video->pict_type = AV_PICTURE_TYPE_I;
  169. outpicref->video->sample_aspect_ratio = test->sar;
  170. if (!test->draw_once)
  171. test->fill_picture_fn(outlink->src, outpicref);
  172. test->pts++;
  173. test->nb_frame++;
  174. return ff_filter_frame(outlink, outpicref);
  175. }
  176. #if CONFIG_COLOR_FILTER
  177. #define color_options options
  178. AVFILTER_DEFINE_CLASS(color);
  179. static void color_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  180. {
  181. TestSourceContext *test = ctx->priv;
  182. ff_fill_rectangle(&test->draw, &test->color,
  183. picref->data, picref->linesize,
  184. 0, 0, test->w, test->h);
  185. }
  186. static av_cold int color_init(AVFilterContext *ctx, const char *args)
  187. {
  188. TestSourceContext *test = ctx->priv;
  189. test->class = &color_class;
  190. test->fill_picture_fn = color_fill_picture;
  191. test->draw_once = 1;
  192. av_opt_set(test, "color", "black", 0);
  193. return init(ctx, args);
  194. }
  195. static int color_query_formats(AVFilterContext *ctx)
  196. {
  197. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  198. return 0;
  199. }
  200. static int color_config_props(AVFilterLink *inlink)
  201. {
  202. AVFilterContext *ctx = inlink->src;
  203. TestSourceContext *test = ctx->priv;
  204. int ret;
  205. ff_draw_init(&test->draw, inlink->format, 0);
  206. ff_draw_color(&test->draw, &test->color, test->color_rgba);
  207. test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
  208. test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
  209. if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
  210. return AVERROR(EINVAL);
  211. if (ret = config_props(inlink) < 0)
  212. return ret;
  213. av_log(ctx, AV_LOG_VERBOSE, "color:0x%02x%02x%02x%02x\n",
  214. test->color_rgba[0], test->color_rgba[1], test->color_rgba[2], test->color_rgba[3]);
  215. return 0;
  216. }
  217. AVFilter avfilter_vsrc_color = {
  218. .name = "color",
  219. .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
  220. .priv_size = sizeof(TestSourceContext),
  221. .init = color_init,
  222. .uninit = uninit,
  223. .query_formats = color_query_formats,
  224. .inputs = (const AVFilterPad[]) {
  225. { .name = NULL }
  226. },
  227. .outputs = (const AVFilterPad[]) {
  228. {
  229. .name = "default",
  230. .type = AVMEDIA_TYPE_VIDEO,
  231. .request_frame = request_frame,
  232. .config_props = color_config_props,
  233. },
  234. { .name = NULL }
  235. },
  236. .priv_class = &color_class,
  237. };
  238. #endif /* CONFIG_COLOR_FILTER */
  239. #if CONFIG_NULLSRC_FILTER
  240. #define nullsrc_options options
  241. AVFILTER_DEFINE_CLASS(nullsrc);
  242. static void nullsrc_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref) { }
  243. static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args)
  244. {
  245. TestSourceContext *test = ctx->priv;
  246. test->class = &nullsrc_class;
  247. test->fill_picture_fn = nullsrc_fill_picture;
  248. return init(ctx, args);
  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. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  257. .outputs = (const AVFilterPad[]) {{ .name = "default",
  258. .type = AVMEDIA_TYPE_VIDEO,
  259. .request_frame = request_frame,
  260. .config_props = config_props, },
  261. { .name = NULL}},
  262. .priv_class = &nullsrc_class,
  263. };
  264. #endif /* CONFIG_NULLSRC_FILTER */
  265. #if CONFIG_TESTSRC_FILTER
  266. #define testsrc_options options
  267. AVFILTER_DEFINE_CLASS(testsrc);
  268. /**
  269. * Fill a rectangle with value val.
  270. *
  271. * @param val the RGB value to set
  272. * @param dst pointer to the destination buffer to fill
  273. * @param dst_linesize linesize of destination
  274. * @param segment_width width of the segment
  275. * @param x horizontal coordinate where to draw the rectangle in the destination buffer
  276. * @param y horizontal coordinate where to draw the rectangle in the destination buffer
  277. * @param w width of the rectangle to draw, expressed as a number of segment_width units
  278. * @param h height of the rectangle to draw, expressed as a number of segment_width units
  279. */
  280. static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
  281. unsigned x, unsigned y, unsigned w, unsigned h)
  282. {
  283. int i;
  284. int step = 3;
  285. dst += segment_width * (step * x + y * dst_linesize);
  286. w *= segment_width * step;
  287. h *= segment_width;
  288. for (i = 0; i < h; i++) {
  289. memset(dst, val, w);
  290. dst += dst_linesize;
  291. }
  292. }
  293. static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
  294. unsigned segment_width)
  295. {
  296. #define TOP_HBAR 1
  297. #define MID_HBAR 2
  298. #define BOT_HBAR 4
  299. #define LEFT_TOP_VBAR 8
  300. #define LEFT_BOT_VBAR 16
  301. #define RIGHT_TOP_VBAR 32
  302. #define RIGHT_BOT_VBAR 64
  303. struct {
  304. int x, y, w, h;
  305. } segments[] = {
  306. { 1, 0, 5, 1 }, /* TOP_HBAR */
  307. { 1, 6, 5, 1 }, /* MID_HBAR */
  308. { 1, 12, 5, 1 }, /* BOT_HBAR */
  309. { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
  310. { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
  311. { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
  312. { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
  313. };
  314. static const unsigned char masks[10] = {
  315. /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  316. /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  317. /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
  318. /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  319. /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  320. /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
  321. /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
  322. /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  323. /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  324. /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  325. };
  326. unsigned mask = masks[digit];
  327. int i;
  328. draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
  329. for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
  330. if (mask & (1<<i))
  331. draw_rectangle(255, dst, dst_linesize, segment_width,
  332. segments[i].x, segments[i].y, segments[i].w, segments[i].h);
  333. }
  334. #define GRADIENT_SIZE (6 * 256)
  335. static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  336. {
  337. TestSourceContext *test = ctx->priv;
  338. uint8_t *p, *p0;
  339. int x, y;
  340. int color, color_rest;
  341. int icolor;
  342. int radius;
  343. int quad0, quad;
  344. int dquad_x, dquad_y;
  345. int grad, dgrad, rgrad, drgrad;
  346. int seg_size;
  347. int second;
  348. int i;
  349. uint8_t *data = picref->data[0];
  350. int width = picref->video->w;
  351. int height = picref->video->h;
  352. /* draw colored bars and circle */
  353. radius = (width + height) / 4;
  354. quad0 = width * width / 4 + height * height / 4 - radius * radius;
  355. dquad_y = 1 - height;
  356. p0 = data;
  357. for (y = 0; y < height; y++) {
  358. p = p0;
  359. color = 0;
  360. color_rest = 0;
  361. quad = quad0;
  362. dquad_x = 1 - width;
  363. for (x = 0; x < width; x++) {
  364. icolor = color;
  365. if (quad < 0)
  366. icolor ^= 7;
  367. quad += dquad_x;
  368. dquad_x += 2;
  369. *(p++) = icolor & 1 ? 255 : 0;
  370. *(p++) = icolor & 2 ? 255 : 0;
  371. *(p++) = icolor & 4 ? 255 : 0;
  372. color_rest += 8;
  373. if (color_rest >= width) {
  374. color_rest -= width;
  375. color++;
  376. }
  377. }
  378. quad0 += dquad_y;
  379. dquad_y += 2;
  380. p0 += picref->linesize[0];
  381. }
  382. /* draw sliding color line */
  383. p0 = p = data + picref->linesize[0] * height * 3/4;
  384. grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
  385. GRADIENT_SIZE;
  386. rgrad = 0;
  387. dgrad = GRADIENT_SIZE / width;
  388. drgrad = GRADIENT_SIZE % width;
  389. for (x = 0; x < width; x++) {
  390. *(p++) =
  391. grad < 256 || grad >= 5 * 256 ? 255 :
  392. grad >= 2 * 256 && grad < 4 * 256 ? 0 :
  393. grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
  394. *(p++) =
  395. grad >= 4 * 256 ? 0 :
  396. grad >= 1 * 256 && grad < 3 * 256 ? 255 :
  397. grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
  398. *(p++) =
  399. grad < 2 * 256 ? 0 :
  400. grad >= 3 * 256 && grad < 5 * 256 ? 255 :
  401. grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
  402. grad += dgrad;
  403. rgrad += drgrad;
  404. if (rgrad >= GRADIENT_SIZE) {
  405. grad++;
  406. rgrad -= GRADIENT_SIZE;
  407. }
  408. if (grad >= GRADIENT_SIZE)
  409. grad -= GRADIENT_SIZE;
  410. }
  411. p = p0;
  412. for (y = height / 8; y > 0; y--) {
  413. memcpy(p+picref->linesize[0], p, 3 * width);
  414. p += picref->linesize[0];
  415. }
  416. /* draw digits */
  417. seg_size = width / 80;
  418. if (seg_size >= 1 && height >= 13 * seg_size) {
  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. second = (int)time;
  424. x = width - (width - seg_size * 64) / 2;
  425. y = (height - seg_size * 13) / 2;
  426. p = data + (x*3 + y * picref->linesize[0]);
  427. for (i = 0; i < 8; i++) {
  428. p -= 3 * 8 * seg_size;
  429. draw_digit(second % 10, p, picref->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, AVFilterBufferRef *picref)
  509. {
  510. TestSourceContext *test = ctx->priv;
  511. int x, y, w = picref->video->w, h = picref->video->h;
  512. for (y = 0; y < h; y++) {
  513. for (x = 0; x < picref->video->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(picref->data[0], picref->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, AVFilterBufferRef *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. AVFilter avfilter_vsrc_smptebars = {
  658. .name = "smptebars",
  659. .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
  660. .priv_size = sizeof(TestSourceContext),
  661. .init = smptebars_init,
  662. .uninit = uninit,
  663. .query_formats = smptebars_query_formats,
  664. .inputs = (const AVFilterPad[]) {
  665. { .name = NULL }
  666. },
  667. .outputs = (const AVFilterPad[]) {
  668. {
  669. .name = "default",
  670. .type = AVMEDIA_TYPE_VIDEO,
  671. .request_frame = request_frame,
  672. .config_props = smptebars_config_props,
  673. },
  674. { .name = NULL }
  675. },
  676. .priv_class = &smptebars_class,
  677. };
  678. #endif /* CONFIG_SMPTEBARS_FILTER */