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.

791 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. 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_opt_free(test);
  130. avfilter_unref_bufferp(&test->picref);
  131. }
  132. static int config_props(AVFilterLink *outlink)
  133. {
  134. TestSourceContext *test = outlink->src->priv;
  135. outlink->w = test->w;
  136. outlink->h = test->h;
  137. outlink->sample_aspect_ratio = test->sar;
  138. outlink->frame_rate = test->frame_rate;
  139. outlink->time_base = test->time_base;
  140. return 0;
  141. }
  142. static int request_frame(AVFilterLink *outlink)
  143. {
  144. TestSourceContext *test = outlink->src->priv;
  145. AVFilterBufferRef *outpicref;
  146. int ret = 0;
  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. if ((ret = ff_start_frame(outlink, outpicref)) < 0 ||
  175. (ret = ff_draw_slice(outlink, 0, test->h, 1)) < 0 ||
  176. (ret = ff_end_frame(outlink)) < 0)
  177. return ret;
  178. return 0;
  179. }
  180. #if CONFIG_COLOR_FILTER
  181. #define color_options options
  182. AVFILTER_DEFINE_CLASS(color);
  183. static void color_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  184. {
  185. TestSourceContext *test = ctx->priv;
  186. ff_fill_rectangle(&test->draw, &test->color,
  187. picref->data, picref->linesize,
  188. 0, 0, test->w, test->h);
  189. }
  190. static av_cold int color_init(AVFilterContext *ctx, const char *args)
  191. {
  192. TestSourceContext *test = ctx->priv;
  193. test->class = &color_class;
  194. test->fill_picture_fn = color_fill_picture;
  195. test->draw_once = 1;
  196. av_opt_set(test, "color", "black", 0);
  197. return init(ctx, args);
  198. }
  199. static int color_query_formats(AVFilterContext *ctx)
  200. {
  201. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  202. return 0;
  203. }
  204. static int color_config_props(AVFilterLink *inlink)
  205. {
  206. AVFilterContext *ctx = inlink->src;
  207. TestSourceContext *test = ctx->priv;
  208. int ret;
  209. ff_draw_init(&test->draw, inlink->format, 0);
  210. ff_draw_color(&test->draw, &test->color, test->color_rgba);
  211. test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
  212. test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
  213. if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
  214. return AVERROR(EINVAL);
  215. if (ret = config_props(inlink) < 0)
  216. return ret;
  217. av_log(ctx, AV_LOG_VERBOSE, "color:0x%02x%02x%02x%02x\n",
  218. test->color_rgba[0], test->color_rgba[1], test->color_rgba[2], test->color_rgba[3]);
  219. return 0;
  220. }
  221. AVFilter avfilter_vsrc_color = {
  222. .name = "color",
  223. .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
  224. .priv_size = sizeof(TestSourceContext),
  225. .init = color_init,
  226. .uninit = uninit,
  227. .query_formats = color_query_formats,
  228. .inputs = (const AVFilterPad[]) {
  229. { .name = NULL }
  230. },
  231. .outputs = (const AVFilterPad[]) {
  232. {
  233. .name = "default",
  234. .type = AVMEDIA_TYPE_VIDEO,
  235. .request_frame = request_frame,
  236. .config_props = color_config_props,
  237. },
  238. { .name = NULL }
  239. },
  240. .priv_class = &color_class,
  241. };
  242. #endif /* CONFIG_COLOR_FILTER */
  243. #if CONFIG_NULLSRC_FILTER
  244. #define nullsrc_options options
  245. AVFILTER_DEFINE_CLASS(nullsrc);
  246. static void nullsrc_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref) { }
  247. static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args)
  248. {
  249. TestSourceContext *test = ctx->priv;
  250. test->class = &nullsrc_class;
  251. test->fill_picture_fn = nullsrc_fill_picture;
  252. return init(ctx, args);
  253. }
  254. AVFilter avfilter_vsrc_nullsrc = {
  255. .name = "nullsrc",
  256. .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
  257. .init = nullsrc_init,
  258. .uninit = uninit,
  259. .priv_size = sizeof(TestSourceContext),
  260. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  261. .outputs = (const AVFilterPad[]) {{ .name = "default",
  262. .type = AVMEDIA_TYPE_VIDEO,
  263. .request_frame = request_frame,
  264. .config_props = config_props, },
  265. { .name = NULL}},
  266. .priv_class = &nullsrc_class,
  267. };
  268. #endif /* CONFIG_NULLSRC_FILTER */
  269. #if CONFIG_TESTSRC_FILTER
  270. #define testsrc_options options
  271. AVFILTER_DEFINE_CLASS(testsrc);
  272. /**
  273. * Fill a rectangle with value val.
  274. *
  275. * @param val the RGB value to set
  276. * @param dst pointer to the destination buffer to fill
  277. * @param dst_linesize linesize of destination
  278. * @param segment_width width of the segment
  279. * @param x horizontal coordinate where to draw the rectangle in the destination buffer
  280. * @param y horizontal coordinate where to draw the rectangle in the destination buffer
  281. * @param w width of the rectangle to draw, expressed as a number of segment_width units
  282. * @param h height of the rectangle to draw, expressed as a number of segment_width units
  283. */
  284. static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
  285. unsigned x, unsigned y, unsigned w, unsigned h)
  286. {
  287. int i;
  288. int step = 3;
  289. dst += segment_width * (step * x + y * dst_linesize);
  290. w *= segment_width * step;
  291. h *= segment_width;
  292. for (i = 0; i < h; i++) {
  293. memset(dst, val, w);
  294. dst += dst_linesize;
  295. }
  296. }
  297. static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
  298. unsigned segment_width)
  299. {
  300. #define TOP_HBAR 1
  301. #define MID_HBAR 2
  302. #define BOT_HBAR 4
  303. #define LEFT_TOP_VBAR 8
  304. #define LEFT_BOT_VBAR 16
  305. #define RIGHT_TOP_VBAR 32
  306. #define RIGHT_BOT_VBAR 64
  307. struct {
  308. int x, y, w, h;
  309. } segments[] = {
  310. { 1, 0, 5, 1 }, /* TOP_HBAR */
  311. { 1, 6, 5, 1 }, /* MID_HBAR */
  312. { 1, 12, 5, 1 }, /* BOT_HBAR */
  313. { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
  314. { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
  315. { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
  316. { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
  317. };
  318. static const unsigned char masks[10] = {
  319. /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  320. /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  321. /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
  322. /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  323. /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  324. /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
  325. /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
  326. /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  327. /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  328. /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  329. };
  330. unsigned mask = masks[digit];
  331. int i;
  332. draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
  333. for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
  334. if (mask & (1<<i))
  335. draw_rectangle(255, dst, dst_linesize, segment_width,
  336. segments[i].x, segments[i].y, segments[i].w, segments[i].h);
  337. }
  338. #define GRADIENT_SIZE (6 * 256)
  339. static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  340. {
  341. TestSourceContext *test = ctx->priv;
  342. uint8_t *p, *p0;
  343. int x, y;
  344. int color, color_rest;
  345. int icolor;
  346. int radius;
  347. int quad0, quad;
  348. int dquad_x, dquad_y;
  349. int grad, dgrad, rgrad, drgrad;
  350. int seg_size;
  351. int second;
  352. int i;
  353. uint8_t *data = picref->data[0];
  354. int width = picref->video->w;
  355. int height = picref->video->h;
  356. /* draw colored bars and circle */
  357. radius = (width + height) / 4;
  358. quad0 = width * width / 4 + height * height / 4 - radius * radius;
  359. dquad_y = 1 - height;
  360. p0 = data;
  361. for (y = 0; y < height; y++) {
  362. p = p0;
  363. color = 0;
  364. color_rest = 0;
  365. quad = quad0;
  366. dquad_x = 1 - width;
  367. for (x = 0; x < width; x++) {
  368. icolor = color;
  369. if (quad < 0)
  370. icolor ^= 7;
  371. quad += dquad_x;
  372. dquad_x += 2;
  373. *(p++) = icolor & 1 ? 255 : 0;
  374. *(p++) = icolor & 2 ? 255 : 0;
  375. *(p++) = icolor & 4 ? 255 : 0;
  376. color_rest += 8;
  377. if (color_rest >= width) {
  378. color_rest -= width;
  379. color++;
  380. }
  381. }
  382. quad0 += dquad_y;
  383. dquad_y += 2;
  384. p0 += picref->linesize[0];
  385. }
  386. /* draw sliding color line */
  387. p0 = p = data + picref->linesize[0] * height * 3/4;
  388. grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
  389. GRADIENT_SIZE;
  390. rgrad = 0;
  391. dgrad = GRADIENT_SIZE / width;
  392. drgrad = GRADIENT_SIZE % width;
  393. for (x = 0; x < width; x++) {
  394. *(p++) =
  395. grad < 256 || grad >= 5 * 256 ? 255 :
  396. grad >= 2 * 256 && grad < 4 * 256 ? 0 :
  397. grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
  398. *(p++) =
  399. grad >= 4 * 256 ? 0 :
  400. grad >= 1 * 256 && grad < 3 * 256 ? 255 :
  401. grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
  402. *(p++) =
  403. grad < 2 * 256 ? 0 :
  404. grad >= 3 * 256 && grad < 5 * 256 ? 255 :
  405. grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
  406. grad += dgrad;
  407. rgrad += drgrad;
  408. if (rgrad >= GRADIENT_SIZE) {
  409. grad++;
  410. rgrad -= GRADIENT_SIZE;
  411. }
  412. if (grad >= GRADIENT_SIZE)
  413. grad -= GRADIENT_SIZE;
  414. }
  415. p = p0;
  416. for (y = height / 8; y > 0; y--) {
  417. memcpy(p+picref->linesize[0], p, 3 * width);
  418. p += picref->linesize[0];
  419. }
  420. /* draw digits */
  421. seg_size = width / 80;
  422. if (seg_size >= 1 && height >= 13 * seg_size) {
  423. double time = av_q2d(test->time_base) * test->nb_frame *
  424. pow(10, test->nb_decimals);
  425. if (time > INT_MAX)
  426. return;
  427. second = (int)time;
  428. x = width - (width - seg_size * 64) / 2;
  429. y = (height - seg_size * 13) / 2;
  430. p = data + (x*3 + y * picref->linesize[0]);
  431. for (i = 0; i < 8; i++) {
  432. p -= 3 * 8 * seg_size;
  433. draw_digit(second % 10, p, picref->linesize[0], seg_size);
  434. second /= 10;
  435. if (second == 0)
  436. break;
  437. }
  438. }
  439. }
  440. static av_cold int test_init(AVFilterContext *ctx, const char *args)
  441. {
  442. TestSourceContext *test = ctx->priv;
  443. test->class = &testsrc_class;
  444. test->fill_picture_fn = test_fill_picture;
  445. return init(ctx, args);
  446. }
  447. static int test_query_formats(AVFilterContext *ctx)
  448. {
  449. static const enum PixelFormat pix_fmts[] = {
  450. PIX_FMT_RGB24, PIX_FMT_NONE
  451. };
  452. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  453. return 0;
  454. }
  455. AVFilter avfilter_vsrc_testsrc = {
  456. .name = "testsrc",
  457. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  458. .priv_size = sizeof(TestSourceContext),
  459. .init = test_init,
  460. .uninit = uninit,
  461. .query_formats = test_query_formats,
  462. .inputs = NULL,
  463. .outputs = (const AVFilterPad[]) {{ .name = "default",
  464. .type = AVMEDIA_TYPE_VIDEO,
  465. .request_frame = request_frame,
  466. .config_props = config_props, },
  467. { .name = NULL }},
  468. .priv_class = &testsrc_class,
  469. };
  470. #endif /* CONFIG_TESTSRC_FILTER */
  471. #if CONFIG_RGBTESTSRC_FILTER
  472. #define rgbtestsrc_options options
  473. AVFILTER_DEFINE_CLASS(rgbtestsrc);
  474. #define R 0
  475. #define G 1
  476. #define B 2
  477. #define A 3
  478. static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
  479. int x, int y, int r, int g, int b, enum PixelFormat fmt,
  480. uint8_t rgba_map[4])
  481. {
  482. int32_t v;
  483. uint8_t *p;
  484. switch (fmt) {
  485. case PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
  486. case PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
  487. case PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
  488. case PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
  489. case PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
  490. case PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
  491. case PIX_FMT_RGB24:
  492. case PIX_FMT_BGR24:
  493. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  494. p = dst + 3*x + y*dst_linesize;
  495. AV_WL24(p, v);
  496. break;
  497. case PIX_FMT_RGBA:
  498. case PIX_FMT_BGRA:
  499. case PIX_FMT_ARGB:
  500. case PIX_FMT_ABGR:
  501. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
  502. p = dst + 4*x + y*dst_linesize;
  503. AV_WL32(p, v);
  504. break;
  505. }
  506. }
  507. static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  508. {
  509. TestSourceContext *test = ctx->priv;
  510. int x, y, w = picref->video->w, h = picref->video->h;
  511. for (y = 0; y < h; y++) {
  512. for (x = 0; x < picref->video->w; x++) {
  513. int c = 256*x/w;
  514. int r = 0, g = 0, b = 0;
  515. if (3*y < h ) r = c;
  516. else if (3*y < 2*h) g = c;
  517. else b = c;
  518. rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
  519. ctx->outputs[0]->format, test->rgba_map);
  520. }
  521. }
  522. }
  523. static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
  524. {
  525. TestSourceContext *test = ctx->priv;
  526. test->draw_once = 1;
  527. test->class = &rgbtestsrc_class;
  528. test->fill_picture_fn = rgbtest_fill_picture;
  529. return init(ctx, args);
  530. }
  531. static int rgbtest_query_formats(AVFilterContext *ctx)
  532. {
  533. static const enum PixelFormat pix_fmts[] = {
  534. PIX_FMT_RGBA, PIX_FMT_ARGB, PIX_FMT_BGRA, PIX_FMT_ABGR,
  535. PIX_FMT_BGR24, PIX_FMT_RGB24,
  536. PIX_FMT_RGB444, PIX_FMT_BGR444,
  537. PIX_FMT_RGB565, PIX_FMT_BGR565,
  538. PIX_FMT_RGB555, PIX_FMT_BGR555,
  539. PIX_FMT_NONE
  540. };
  541. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  542. return 0;
  543. }
  544. static int rgbtest_config_props(AVFilterLink *outlink)
  545. {
  546. TestSourceContext *test = outlink->src->priv;
  547. ff_fill_rgba_map(test->rgba_map, outlink->format);
  548. return config_props(outlink);
  549. }
  550. AVFilter avfilter_vsrc_rgbtestsrc = {
  551. .name = "rgbtestsrc",
  552. .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
  553. .priv_size = sizeof(TestSourceContext),
  554. .init = rgbtest_init,
  555. .uninit = uninit,
  556. .query_formats = rgbtest_query_formats,
  557. .inputs = NULL,
  558. .outputs = (const AVFilterPad[]) {{ .name = "default",
  559. .type = AVMEDIA_TYPE_VIDEO,
  560. .request_frame = request_frame,
  561. .config_props = rgbtest_config_props, },
  562. { .name = NULL }},
  563. .priv_class = &rgbtestsrc_class,
  564. };
  565. #endif /* CONFIG_RGBTESTSRC_FILTER */
  566. #if CONFIG_SMPTEBARS_FILTER
  567. #define smptebars_options options
  568. AVFILTER_DEFINE_CLASS(smptebars);
  569. static const uint8_t rainbow[7][4] = {
  570. { 191, 191, 191, 255 }, /* gray */
  571. { 191, 191, 0, 255 }, /* yellow */
  572. { 0, 191, 191, 255 }, /* cyan */
  573. { 0, 191, 0, 255 }, /* green */
  574. { 191, 0, 191, 255 }, /* magenta */
  575. { 191, 0, 0, 255 }, /* red */
  576. { 0, 0, 191, 255 }, /* blue */
  577. };
  578. static const uint8_t wobnair[7][4] = {
  579. { 0, 0, 191, 255 }, /* blue */
  580. { 19, 19, 19, 255 }, /* 7.5% intensity black */
  581. { 191, 0, 191, 255 }, /* magenta */
  582. { 19, 19, 19, 255 }, /* 7.5% intensity black */
  583. { 0, 191, 191, 255 }, /* cyan */
  584. { 19, 19, 19, 255 }, /* 7.5% intensity black */
  585. { 191, 191, 191, 255 }, /* gray */
  586. };
  587. static const uint8_t white[4] = { 255, 255, 255, 255 };
  588. static const uint8_t black[4] = { 19, 19, 19, 255 }; /* 7.5% intensity black */
  589. /* pluge pulses */
  590. static const uint8_t neg4ire[4] = { 9, 9, 9, 255 }; /* 3.5% intensity black */
  591. static const uint8_t pos4ire[4] = { 29, 29, 29, 255 }; /* 11.5% intensity black */
  592. /* fudged Q/-I */
  593. static const uint8_t i_pixel[4] = { 0, 68, 130, 255 };
  594. static const uint8_t q_pixel[4] = { 67, 0, 130, 255 };
  595. static void smptebars_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  596. {
  597. TestSourceContext *test = ctx->priv;
  598. FFDrawColor color;
  599. int r_w, r_h, w_h, p_w, p_h, i, x = 0;
  600. r_w = (test->w + 6) / 7;
  601. r_h = test->h * 2 / 3;
  602. w_h = test->h * 3 / 4 - r_h;
  603. p_w = r_w * 5 / 4;
  604. p_h = test->h - w_h - r_h;
  605. #define DRAW_COLOR(rgba, x, y, w, h) \
  606. ff_draw_color(&test->draw, &color, rgba); \
  607. ff_fill_rectangle(&test->draw, &color, \
  608. picref->data, picref->linesize, x, y, w, h) \
  609. for (i = 0; i < 7; i++) {
  610. DRAW_COLOR(rainbow[i], x, 0, FFMIN(r_w, test->w - x), r_h);
  611. DRAW_COLOR(wobnair[i], x, r_h, FFMIN(r_w, test->w - x), w_h);
  612. x += r_w;
  613. }
  614. x = 0;
  615. DRAW_COLOR(i_pixel, x, r_h + w_h, p_w, p_h);
  616. x += p_w;
  617. DRAW_COLOR(white, x, r_h + w_h, p_w, p_h);
  618. x += p_w;
  619. DRAW_COLOR(q_pixel, x, r_h + w_h, p_w, p_h);
  620. x += p_w;
  621. DRAW_COLOR(black, x, r_h + w_h, 5 * r_w - x, p_h);
  622. x += 5 * r_w - x;
  623. DRAW_COLOR(neg4ire, x, r_h + w_h, r_w / 3, p_h);
  624. x += r_w / 3;
  625. DRAW_COLOR(black, x, r_h + w_h, r_w / 3, p_h);
  626. x += r_w / 3;
  627. DRAW_COLOR(pos4ire, x, r_h + w_h, r_w / 3, p_h);
  628. x += r_w / 3;
  629. DRAW_COLOR(black, x, r_h + w_h, test->w - x, p_h);
  630. }
  631. static av_cold int smptebars_init(AVFilterContext *ctx, const char *args)
  632. {
  633. TestSourceContext *test = ctx->priv;
  634. test->class = &smptebars_class;
  635. test->fill_picture_fn = smptebars_fill_picture;
  636. test->draw_once = 1;
  637. return init(ctx, args);
  638. }
  639. static int smptebars_query_formats(AVFilterContext *ctx)
  640. {
  641. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  642. return 0;
  643. }
  644. static int smptebars_config_props(AVFilterLink *outlink)
  645. {
  646. AVFilterContext *ctx = outlink->src;
  647. TestSourceContext *test = ctx->priv;
  648. ff_draw_init(&test->draw, outlink->format, 0);
  649. return config_props(outlink);
  650. }
  651. AVFilter avfilter_vsrc_smptebars = {
  652. .name = "smptebars",
  653. .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
  654. .priv_size = sizeof(TestSourceContext),
  655. .init = smptebars_init,
  656. .uninit = uninit,
  657. .query_formats = smptebars_query_formats,
  658. .inputs = (const AVFilterPad[]) {
  659. { .name = NULL }
  660. },
  661. .outputs = (const AVFilterPad[]) {
  662. {
  663. .name = "default",
  664. .type = AVMEDIA_TYPE_VIDEO,
  665. .request_frame = request_frame,
  666. .config_props = smptebars_config_props,
  667. },
  668. { .name = NULL }
  669. },
  670. .priv_class = &smptebars_class,
  671. };
  672. #endif /* CONFIG_SMPTEBARS_FILTER */