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.

916 lines
29KB

  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 and smptehdbars 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. int64_t duration; ///< duration expressed in microseconds
  53. AVRational sar; ///< sample aspect ratio
  54. int nb_decimals;
  55. int draw_once; ///< draw only the first frame, always put out the same picture
  56. AVFrame *picref; ///< cached reference containing the painted picture
  57. void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
  58. /* only used by color */
  59. char *color_str;
  60. FFDrawContext draw;
  61. FFDrawColor color;
  62. uint8_t color_rgba[4];
  63. /* only used by rgbtest */
  64. uint8_t rgba_map[4];
  65. } TestSourceContext;
  66. #define OFFSET(x) offsetof(TestSourceContext, x)
  67. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  68. #define COMMON_OPTIONS \
  69. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
  70. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
  71. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
  72. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
  73. { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
  74. { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
  75. { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1}, 0, INT_MAX, FLAGS },
  76. static const AVOption color_options[] = {
  77. /* only used by color */
  78. { "color", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
  79. { "c", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
  80. COMMON_OPTIONS
  81. { NULL },
  82. };
  83. static const AVOption options[] = {
  84. COMMON_OPTIONS
  85. /* only used by testsrc */
  86. { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, 0, 17, FLAGS },
  87. { "n", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, 0, 17, FLAGS },
  88. { NULL },
  89. };
  90. static av_cold int init(AVFilterContext *ctx)
  91. {
  92. TestSourceContext *test = ctx->priv;
  93. int ret = 0;
  94. if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
  95. av_log(ctx, AV_LOG_WARNING,
  96. "Option 'decimals' is ignored with source '%s'\n",
  97. ctx->filter->name);
  98. }
  99. if (test->color_str) {
  100. if (!strcmp(ctx->filter->name, "color")) {
  101. ret = av_parse_color(test->color_rgba, test->color_str, -1, ctx);
  102. if (ret < 0)
  103. return ret;
  104. } else {
  105. av_log(ctx, AV_LOG_WARNING,
  106. "Option 'color' is ignored with source '%s'\n",
  107. ctx->filter->name);
  108. }
  109. }
  110. test->time_base = av_inv_q(test->frame_rate);
  111. test->nb_frame = 0;
  112. test->pts = 0;
  113. av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
  114. test->w, test->h, test->frame_rate.num, test->frame_rate.den,
  115. test->duration < 0 ? -1 : (double)test->duration/1000000,
  116. test->sar.num, test->sar.den);
  117. return 0;
  118. }
  119. static av_cold void uninit(AVFilterContext *ctx)
  120. {
  121. TestSourceContext *test = ctx->priv;
  122. av_frame_free(&test->picref);
  123. }
  124. static int config_props(AVFilterLink *outlink)
  125. {
  126. TestSourceContext *test = outlink->src->priv;
  127. outlink->w = test->w;
  128. outlink->h = test->h;
  129. outlink->sample_aspect_ratio = test->sar;
  130. outlink->frame_rate = test->frame_rate;
  131. outlink->time_base = test->time_base;
  132. return 0;
  133. }
  134. static int request_frame(AVFilterLink *outlink)
  135. {
  136. TestSourceContext *test = outlink->src->priv;
  137. AVFrame *frame;
  138. if (test->duration >= 0 &&
  139. av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
  140. return AVERROR_EOF;
  141. if (test->draw_once) {
  142. if (!test->picref) {
  143. test->picref =
  144. ff_get_video_buffer(outlink, test->w, test->h);
  145. if (!test->picref)
  146. return AVERROR(ENOMEM);
  147. test->fill_picture_fn(outlink->src, test->picref);
  148. }
  149. frame = av_frame_clone(test->picref);
  150. } else
  151. frame = ff_get_video_buffer(outlink, test->w, test->h);
  152. if (!frame)
  153. return AVERROR(ENOMEM);
  154. frame->pts = test->pts;
  155. frame->key_frame = 1;
  156. frame->interlaced_frame = 0;
  157. frame->pict_type = AV_PICTURE_TYPE_I;
  158. frame->sample_aspect_ratio = test->sar;
  159. if (!test->draw_once)
  160. test->fill_picture_fn(outlink->src, frame);
  161. test->pts++;
  162. test->nb_frame++;
  163. return ff_filter_frame(outlink, frame);
  164. }
  165. #if CONFIG_COLOR_FILTER
  166. AVFILTER_DEFINE_CLASS(color);
  167. static void color_fill_picture(AVFilterContext *ctx, AVFrame *picref)
  168. {
  169. TestSourceContext *test = ctx->priv;
  170. ff_fill_rectangle(&test->draw, &test->color,
  171. picref->data, picref->linesize,
  172. 0, 0, test->w, test->h);
  173. }
  174. static av_cold int color_init(AVFilterContext *ctx)
  175. {
  176. TestSourceContext *test = ctx->priv;
  177. test->fill_picture_fn = color_fill_picture;
  178. test->draw_once = 1;
  179. return init(ctx);
  180. }
  181. static int color_query_formats(AVFilterContext *ctx)
  182. {
  183. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  184. return 0;
  185. }
  186. static int color_config_props(AVFilterLink *inlink)
  187. {
  188. AVFilterContext *ctx = inlink->src;
  189. TestSourceContext *test = ctx->priv;
  190. int ret;
  191. ff_draw_init(&test->draw, inlink->format, 0);
  192. ff_draw_color(&test->draw, &test->color, test->color_rgba);
  193. test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
  194. test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
  195. if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
  196. return AVERROR(EINVAL);
  197. if ((ret = config_props(inlink)) < 0)
  198. return ret;
  199. av_log(ctx, AV_LOG_VERBOSE, "color:0x%02x%02x%02x%02x\n",
  200. test->color_rgba[0], test->color_rgba[1], test->color_rgba[2], test->color_rgba[3]);
  201. return 0;
  202. }
  203. static const AVFilterPad color_outputs[] = {
  204. {
  205. .name = "default",
  206. .type = AVMEDIA_TYPE_VIDEO,
  207. .request_frame = request_frame,
  208. .config_props = color_config_props,
  209. },
  210. { NULL }
  211. };
  212. AVFilter avfilter_vsrc_color = {
  213. .name = "color",
  214. .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
  215. .priv_class = &color_class,
  216. .priv_size = sizeof(TestSourceContext),
  217. .init = color_init,
  218. .uninit = uninit,
  219. .query_formats = color_query_formats,
  220. .inputs = NULL,
  221. .outputs = color_outputs,
  222. };
  223. #endif /* CONFIG_COLOR_FILTER */
  224. #if CONFIG_NULLSRC_FILTER
  225. #define nullsrc_options options
  226. AVFILTER_DEFINE_CLASS(nullsrc);
  227. static void nullsrc_fill_picture(AVFilterContext *ctx, AVFrame *picref) { }
  228. static av_cold int nullsrc_init(AVFilterContext *ctx)
  229. {
  230. TestSourceContext *test = ctx->priv;
  231. test->fill_picture_fn = nullsrc_fill_picture;
  232. return init(ctx);
  233. }
  234. static const AVFilterPad nullsrc_outputs[] = {
  235. {
  236. .name = "default",
  237. .type = AVMEDIA_TYPE_VIDEO,
  238. .request_frame = request_frame,
  239. .config_props = config_props,
  240. },
  241. { NULL },
  242. };
  243. AVFilter avfilter_vsrc_nullsrc = {
  244. .name = "nullsrc",
  245. .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
  246. .init = nullsrc_init,
  247. .uninit = uninit,
  248. .priv_size = sizeof(TestSourceContext),
  249. .priv_class = &nullsrc_class,
  250. .inputs = NULL,
  251. .outputs = nullsrc_outputs,
  252. };
  253. #endif /* CONFIG_NULLSRC_FILTER */
  254. #if CONFIG_TESTSRC_FILTER
  255. #define testsrc_options options
  256. AVFILTER_DEFINE_CLASS(testsrc);
  257. /**
  258. * Fill a rectangle with value val.
  259. *
  260. * @param val the RGB value to set
  261. * @param dst pointer to the destination buffer to fill
  262. * @param dst_linesize linesize of destination
  263. * @param segment_width width of the segment
  264. * @param x horizontal coordinate where to draw the rectangle in the destination buffer
  265. * @param y horizontal coordinate where to draw the rectangle in the destination buffer
  266. * @param w width of the rectangle to draw, expressed as a number of segment_width units
  267. * @param h height of the rectangle to draw, expressed as a number of segment_width units
  268. */
  269. static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
  270. unsigned x, unsigned y, unsigned w, unsigned h)
  271. {
  272. int i;
  273. int step = 3;
  274. dst += segment_width * (step * x + y * dst_linesize);
  275. w *= segment_width * step;
  276. h *= segment_width;
  277. for (i = 0; i < h; i++) {
  278. memset(dst, val, w);
  279. dst += dst_linesize;
  280. }
  281. }
  282. static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
  283. unsigned segment_width)
  284. {
  285. #define TOP_HBAR 1
  286. #define MID_HBAR 2
  287. #define BOT_HBAR 4
  288. #define LEFT_TOP_VBAR 8
  289. #define LEFT_BOT_VBAR 16
  290. #define RIGHT_TOP_VBAR 32
  291. #define RIGHT_BOT_VBAR 64
  292. struct {
  293. int x, y, w, h;
  294. } segments[] = {
  295. { 1, 0, 5, 1 }, /* TOP_HBAR */
  296. { 1, 6, 5, 1 }, /* MID_HBAR */
  297. { 1, 12, 5, 1 }, /* BOT_HBAR */
  298. { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
  299. { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
  300. { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
  301. { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
  302. };
  303. static const unsigned char masks[10] = {
  304. /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  305. /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  306. /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
  307. /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  308. /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  309. /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
  310. /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
  311. /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  312. /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  313. /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  314. };
  315. unsigned mask = masks[digit];
  316. int i;
  317. draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
  318. for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
  319. if (mask & (1<<i))
  320. draw_rectangle(255, dst, dst_linesize, segment_width,
  321. segments[i].x, segments[i].y, segments[i].w, segments[i].h);
  322. }
  323. #define GRADIENT_SIZE (6 * 256)
  324. static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  325. {
  326. TestSourceContext *test = ctx->priv;
  327. uint8_t *p, *p0;
  328. int x, y;
  329. int color, color_rest;
  330. int icolor;
  331. int radius;
  332. int quad0, quad;
  333. int dquad_x, dquad_y;
  334. int grad, dgrad, rgrad, drgrad;
  335. int seg_size;
  336. int second;
  337. int i;
  338. uint8_t *data = frame->data[0];
  339. int width = frame->width;
  340. int height = frame->height;
  341. /* draw colored bars and circle */
  342. radius = (width + height) / 4;
  343. quad0 = width * width / 4 + height * height / 4 - radius * radius;
  344. dquad_y = 1 - height;
  345. p0 = data;
  346. for (y = 0; y < height; y++) {
  347. p = p0;
  348. color = 0;
  349. color_rest = 0;
  350. quad = quad0;
  351. dquad_x = 1 - width;
  352. for (x = 0; x < width; x++) {
  353. icolor = color;
  354. if (quad < 0)
  355. icolor ^= 7;
  356. quad += dquad_x;
  357. dquad_x += 2;
  358. *(p++) = icolor & 1 ? 255 : 0;
  359. *(p++) = icolor & 2 ? 255 : 0;
  360. *(p++) = icolor & 4 ? 255 : 0;
  361. color_rest += 8;
  362. if (color_rest >= width) {
  363. color_rest -= width;
  364. color++;
  365. }
  366. }
  367. quad0 += dquad_y;
  368. dquad_y += 2;
  369. p0 += frame->linesize[0];
  370. }
  371. /* draw sliding color line */
  372. p0 = p = data + frame->linesize[0] * height * 3/4;
  373. grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
  374. GRADIENT_SIZE;
  375. rgrad = 0;
  376. dgrad = GRADIENT_SIZE / width;
  377. drgrad = GRADIENT_SIZE % width;
  378. for (x = 0; x < width; x++) {
  379. *(p++) =
  380. grad < 256 || grad >= 5 * 256 ? 255 :
  381. grad >= 2 * 256 && grad < 4 * 256 ? 0 :
  382. grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
  383. *(p++) =
  384. grad >= 4 * 256 ? 0 :
  385. grad >= 1 * 256 && grad < 3 * 256 ? 255 :
  386. grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
  387. *(p++) =
  388. grad < 2 * 256 ? 0 :
  389. grad >= 3 * 256 && grad < 5 * 256 ? 255 :
  390. grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
  391. grad += dgrad;
  392. rgrad += drgrad;
  393. if (rgrad >= GRADIENT_SIZE) {
  394. grad++;
  395. rgrad -= GRADIENT_SIZE;
  396. }
  397. if (grad >= GRADIENT_SIZE)
  398. grad -= GRADIENT_SIZE;
  399. }
  400. p = p0;
  401. for (y = height / 8; y > 0; y--) {
  402. memcpy(p+frame->linesize[0], p, 3 * width);
  403. p += frame->linesize[0];
  404. }
  405. /* draw digits */
  406. seg_size = width / 80;
  407. if (seg_size >= 1 && height >= 13 * seg_size) {
  408. int64_t p10decimals = 1;
  409. double time = av_q2d(test->time_base) * test->nb_frame *
  410. pow(10, test->nb_decimals);
  411. if (time >= INT_MAX)
  412. return;
  413. for(x=0; x<test->nb_decimals; x++)
  414. p10decimals *= 10;
  415. second = av_rescale_rnd(test->nb_frame * test->time_base.num, p10decimals, test->time_base.den, AV_ROUND_ZERO);
  416. x = width - (width - seg_size * 64) / 2;
  417. y = (height - seg_size * 13) / 2;
  418. p = data + (x*3 + y * frame->linesize[0]);
  419. for (i = 0; i < 8; i++) {
  420. p -= 3 * 8 * seg_size;
  421. draw_digit(second % 10, p, frame->linesize[0], seg_size);
  422. second /= 10;
  423. if (second == 0)
  424. break;
  425. }
  426. }
  427. }
  428. static av_cold int test_init(AVFilterContext *ctx)
  429. {
  430. TestSourceContext *test = ctx->priv;
  431. test->fill_picture_fn = test_fill_picture;
  432. return init(ctx);
  433. }
  434. static int test_query_formats(AVFilterContext *ctx)
  435. {
  436. static const enum AVPixelFormat pix_fmts[] = {
  437. AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
  438. };
  439. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  440. return 0;
  441. }
  442. static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
  443. {
  444. .name = "default",
  445. .type = AVMEDIA_TYPE_VIDEO,
  446. .request_frame = request_frame,
  447. .config_props = config_props,
  448. },
  449. { NULL }
  450. };
  451. AVFilter avfilter_vsrc_testsrc = {
  452. .name = "testsrc",
  453. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  454. .priv_size = sizeof(TestSourceContext),
  455. .priv_class = &testsrc_class,
  456. .init = test_init,
  457. .uninit = uninit,
  458. .query_formats = test_query_formats,
  459. .inputs = NULL,
  460. .outputs = avfilter_vsrc_testsrc_outputs,
  461. };
  462. #endif /* CONFIG_TESTSRC_FILTER */
  463. #if CONFIG_RGBTESTSRC_FILTER
  464. #define rgbtestsrc_options options
  465. AVFILTER_DEFINE_CLASS(rgbtestsrc);
  466. #define R 0
  467. #define G 1
  468. #define B 2
  469. #define A 3
  470. static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
  471. int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
  472. uint8_t rgba_map[4])
  473. {
  474. int32_t v;
  475. uint8_t *p;
  476. switch (fmt) {
  477. case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
  478. case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
  479. case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
  480. case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
  481. case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
  482. case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
  483. case AV_PIX_FMT_RGB24:
  484. case AV_PIX_FMT_BGR24:
  485. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  486. p = dst + 3*x + y*dst_linesize;
  487. AV_WL24(p, v);
  488. break;
  489. case AV_PIX_FMT_RGBA:
  490. case AV_PIX_FMT_BGRA:
  491. case AV_PIX_FMT_ARGB:
  492. case AV_PIX_FMT_ABGR:
  493. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
  494. p = dst + 4*x + y*dst_linesize;
  495. AV_WL32(p, v);
  496. break;
  497. }
  498. }
  499. static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  500. {
  501. TestSourceContext *test = ctx->priv;
  502. int x, y, w = frame->width, h = frame->height;
  503. for (y = 0; y < h; y++) {
  504. for (x = 0; x < w; x++) {
  505. int c = 256*x/w;
  506. int r = 0, g = 0, b = 0;
  507. if (3*y < h ) r = c;
  508. else if (3*y < 2*h) g = c;
  509. else b = c;
  510. rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
  511. ctx->outputs[0]->format, test->rgba_map);
  512. }
  513. }
  514. }
  515. static av_cold int rgbtest_init(AVFilterContext *ctx)
  516. {
  517. TestSourceContext *test = ctx->priv;
  518. test->draw_once = 1;
  519. test->fill_picture_fn = rgbtest_fill_picture;
  520. return init(ctx);
  521. }
  522. static int rgbtest_query_formats(AVFilterContext *ctx)
  523. {
  524. static const enum AVPixelFormat pix_fmts[] = {
  525. AV_PIX_FMT_RGBA, AV_PIX_FMT_ARGB, AV_PIX_FMT_BGRA, AV_PIX_FMT_ABGR,
  526. AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
  527. AV_PIX_FMT_RGB444, AV_PIX_FMT_BGR444,
  528. AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565,
  529. AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555,
  530. AV_PIX_FMT_NONE
  531. };
  532. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  533. return 0;
  534. }
  535. static int rgbtest_config_props(AVFilterLink *outlink)
  536. {
  537. TestSourceContext *test = outlink->src->priv;
  538. ff_fill_rgba_map(test->rgba_map, outlink->format);
  539. return config_props(outlink);
  540. }
  541. static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
  542. {
  543. .name = "default",
  544. .type = AVMEDIA_TYPE_VIDEO,
  545. .request_frame = request_frame,
  546. .config_props = rgbtest_config_props,
  547. },
  548. { NULL }
  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. .priv_class = &rgbtestsrc_class,
  555. .init = rgbtest_init,
  556. .uninit = uninit,
  557. .query_formats = rgbtest_query_formats,
  558. .inputs = NULL,
  559. .outputs = avfilter_vsrc_rgbtestsrc_outputs,
  560. };
  561. #endif /* CONFIG_RGBTESTSRC_FILTER */
  562. #if CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER
  563. static const uint8_t rainbow[7][4] = {
  564. { 191, 191, 191, 255 }, /* gray */
  565. { 191, 191, 0, 255 }, /* yellow */
  566. { 0, 191, 191, 255 }, /* cyan */
  567. { 0, 191, 0, 255 }, /* green */
  568. { 191, 0, 191, 255 }, /* magenta */
  569. { 191, 0, 0, 255 }, /* red */
  570. { 0, 0, 191, 255 }, /* blue */
  571. };
  572. static const uint8_t wobnair[7][4] = {
  573. { 0, 0, 191, 255 }, /* blue */
  574. { 19, 19, 19, 255 }, /* 7.5% intensity black */
  575. { 191, 0, 191, 255 }, /* magenta */
  576. { 19, 19, 19, 255 }, /* 7.5% intensity black */
  577. { 0, 191, 191, 255 }, /* cyan */
  578. { 19, 19, 19, 255 }, /* 7.5% intensity black */
  579. { 191, 191, 191, 255 }, /* gray */
  580. };
  581. static const uint8_t white[4] = { 255, 255, 255, 255 };
  582. static const uint8_t black[4] = { 19, 19, 19, 255 }; /* 7.5% intensity black */
  583. /* pluge pulses */
  584. static const uint8_t neg4ire[4] = { 9, 9, 9, 255 }; /* 3.5% intensity black */
  585. static const uint8_t pos4ire[4] = { 29, 29, 29, 255 }; /* 11.5% intensity black */
  586. /* fudged Q/-I */
  587. static const uint8_t i_pixel[4] = { 0, 68, 130, 255 };
  588. static const uint8_t q_pixel[4] = { 67, 0, 130, 255 };
  589. static const uint8_t gray40[4] = { 102, 102, 102, 255 };
  590. static const uint8_t gray15[4] = { 38, 38, 38, 255 };
  591. static const uint8_t cyan[4] = { 0, 255, 255, 255 };
  592. static const uint8_t yellow[4] = { 255, 255, 0, 255 };
  593. static const uint8_t blue[4] = { 0, 0, 255, 255 };
  594. static const uint8_t red[4] = { 255, 0, 0, 255 };
  595. static const uint8_t black0[4] = { 5, 5, 5, 255 };
  596. static const uint8_t black2[4] = { 10, 10, 10, 255 };
  597. static const uint8_t black4[4] = { 15, 15, 15, 255 };
  598. static const uint8_t neg2[4] = { 0, 0, 0, 255 };
  599. static void inline draw_bar(TestSourceContext *test, const uint8_t *color,
  600. unsigned x, unsigned y, unsigned w, unsigned h,
  601. AVFrame *frame)
  602. {
  603. FFDrawColor draw_color;
  604. x = FFMIN(x, test->w - 1);
  605. y = FFMIN(y, test->h - 1);
  606. w = FFMIN(w, test->w - x);
  607. h = FFMIN(h, test->h - y);
  608. av_assert0(x + w <= test->w);
  609. av_assert0(y + h <= test->h);
  610. ff_draw_color(&test->draw, &draw_color, color);
  611. ff_fill_rectangle(&test->draw, &draw_color,
  612. frame->data, frame->linesize, x, y, w, h);
  613. }
  614. static int smptebars_query_formats(AVFilterContext *ctx)
  615. {
  616. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  617. return 0;
  618. }
  619. static int smptebars_config_props(AVFilterLink *outlink)
  620. {
  621. AVFilterContext *ctx = outlink->src;
  622. TestSourceContext *test = ctx->priv;
  623. ff_draw_init(&test->draw, outlink->format, 0);
  624. return config_props(outlink);
  625. }
  626. static const AVFilterPad smptebars_outputs[] = {
  627. {
  628. .name = "default",
  629. .type = AVMEDIA_TYPE_VIDEO,
  630. .request_frame = request_frame,
  631. .config_props = smptebars_config_props,
  632. },
  633. { NULL }
  634. };
  635. #if CONFIG_SMPTEHDBARS_FILTER
  636. #define smptebars_options options
  637. AVFILTER_DEFINE_CLASS(smptebars);
  638. static void smptebars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
  639. {
  640. TestSourceContext *test = ctx->priv;
  641. int r_w, r_h, w_h, p_w, p_h, i, tmp, x = 0;
  642. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
  643. r_w = FFALIGN((test->w + 6) / 7, 1 << pixdesc->log2_chroma_w);
  644. r_h = FFALIGN(test->h * 2 / 3, 1 << pixdesc->log2_chroma_h);
  645. w_h = FFALIGN(test->h * 3 / 4 - r_h, 1 << pixdesc->log2_chroma_h);
  646. p_w = FFALIGN(r_w * 5 / 4, 1 << pixdesc->log2_chroma_w);
  647. p_h = test->h - w_h - r_h;
  648. for (i = 0; i < 7; i++) {
  649. draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
  650. draw_bar(test, wobnair[i], x, r_h, r_w, w_h, picref);
  651. x += r_w;
  652. }
  653. x = 0;
  654. draw_bar(test, i_pixel, x, r_h + w_h, p_w, p_h, picref);
  655. x += p_w;
  656. draw_bar(test, white, x, r_h + w_h, p_w, p_h, picref);
  657. x += p_w;
  658. draw_bar(test, q_pixel, x, r_h + w_h, p_w, p_h, picref);
  659. x += p_w;
  660. tmp = FFALIGN(5 * r_w - x, 1 << pixdesc->log2_chroma_w);
  661. draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
  662. x += tmp;
  663. tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
  664. draw_bar(test, neg4ire, x, r_h + w_h, tmp, p_h, picref);
  665. x += tmp;
  666. draw_bar(test, black, x, r_h + w_h, tmp, p_h, picref);
  667. x += tmp;
  668. draw_bar(test, pos4ire, x, r_h + w_h, tmp, p_h, picref);
  669. x += tmp;
  670. draw_bar(test, black, x, r_h + w_h, test->w - x, p_h, picref);
  671. }
  672. static av_cold int smptebars_init(AVFilterContext *ctx)
  673. {
  674. TestSourceContext *test = ctx->priv;
  675. test->fill_picture_fn = smptebars_fill_picture;
  676. test->draw_once = 1;
  677. return init(ctx);
  678. }
  679. AVFilter avfilter_vsrc_smptebars = {
  680. .name = "smptebars",
  681. .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
  682. .priv_size = sizeof(TestSourceContext),
  683. .init = smptebars_init,
  684. .uninit = uninit,
  685. .query_formats = smptebars_query_formats,
  686. .inputs = NULL,
  687. .outputs = smptebars_outputs,
  688. .priv_class = &smptebars_class,
  689. };
  690. #endif /* CONFIG_SMPTEBARS_FILTER */
  691. #if CONFIG_SMPTEHDBARS_FILTER
  692. #define smptehdbars_options options
  693. AVFILTER_DEFINE_CLASS(smptehdbars);
  694. static void smptehdbars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
  695. {
  696. TestSourceContext *test = ctx->priv;
  697. int d_w, r_w, r_h, l_w, i, tmp, x = 0, y = 0;
  698. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
  699. d_w = FFALIGN(test->w / 8, 1 << pixdesc->log2_chroma_w);
  700. r_h = FFALIGN(test->h * 7 / 12, 1 << pixdesc->log2_chroma_h);
  701. draw_bar(test, gray40, x, 0, d_w, r_h, picref);
  702. x += d_w;
  703. r_w = FFALIGN((((test->w + 3) / 4) * 3) / 7, 1 << pixdesc->log2_chroma_w);
  704. for (i = 0; i < 7; i++) {
  705. draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
  706. x += r_w;
  707. }
  708. draw_bar(test, gray40, x, 0, test->w - x, r_h, picref);
  709. y = r_h;
  710. r_h = FFALIGN(test->h / 12, 1 << pixdesc->log2_chroma_h);
  711. draw_bar(test, cyan, 0, y, d_w, r_h, picref);
  712. x = d_w;
  713. draw_bar(test, i_pixel, x, y, r_w, r_h, picref);
  714. x += r_w;
  715. tmp = r_w * 6;
  716. draw_bar(test, rainbow[0], x, y, tmp, r_h, picref);
  717. x += tmp;
  718. l_w = x;
  719. draw_bar(test, blue, x, y, test->w - x, r_h, picref);
  720. y += r_h;
  721. draw_bar(test, yellow, 0, y, d_w, r_h, picref);
  722. x = d_w;
  723. draw_bar(test, q_pixel, x, y, r_w, r_h, picref);
  724. x += r_w;
  725. for (i = 0; i < tmp; i += 1 << pixdesc->log2_chroma_w) {
  726. uint8_t yramp[4] = {0};
  727. yramp[0] =
  728. yramp[1] =
  729. yramp[2] = i * 255 / tmp;
  730. yramp[3] = 255;
  731. draw_bar(test, yramp, x, y, 1 << pixdesc->log2_chroma_w, r_h, picref);
  732. x += 1 << pixdesc->log2_chroma_w;
  733. }
  734. draw_bar(test, red, x, y, test->w - x, r_h, picref);
  735. y += r_h;
  736. draw_bar(test, gray15, 0, y, d_w, test->h - y, picref);
  737. x = d_w;
  738. tmp = FFALIGN(r_w * 3 / 2, 1 << pixdesc->log2_chroma_w);
  739. draw_bar(test, black0, x, y, tmp, test->h - y, picref);
  740. x += tmp;
  741. tmp = FFALIGN(r_w * 2, 1 << pixdesc->log2_chroma_w);
  742. draw_bar(test, white, x, y, tmp, test->h - y, picref);
  743. x += tmp;
  744. tmp = FFALIGN(r_w * 5 / 6, 1 << pixdesc->log2_chroma_w);
  745. draw_bar(test, black0, x, y, tmp, test->h - y, picref);
  746. x += tmp;
  747. tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
  748. draw_bar(test, neg2, x, y, tmp, test->h - y, picref);
  749. x += tmp;
  750. draw_bar(test, black0, x, y, tmp, test->h - y, picref);
  751. x += tmp;
  752. draw_bar(test, black2, x, y, tmp, test->h - y, picref);
  753. x += tmp;
  754. draw_bar(test, black0, x, y, tmp, test->h - y, picref);
  755. x += tmp;
  756. draw_bar(test, black4, x, y, tmp, test->h - y, picref);
  757. x += tmp;
  758. r_w = l_w - x;
  759. draw_bar(test, black0, x, y, r_w, test->h - y, picref);
  760. x += r_w;
  761. draw_bar(test, gray15, x, y, test->w - x, test->h - y, picref);
  762. }
  763. static av_cold int smptehdbars_init(AVFilterContext *ctx)
  764. {
  765. TestSourceContext *test = ctx->priv;
  766. test->fill_picture_fn = smptehdbars_fill_picture;
  767. test->draw_once = 1;
  768. return init(ctx);
  769. }
  770. AVFilter avfilter_vsrc_smptehdbars = {
  771. .name = "smptehdbars",
  772. .description = NULL_IF_CONFIG_SMALL("Generate SMPTE HD color bars."),
  773. .priv_size = sizeof(TestSourceContext),
  774. .init = smptehdbars_init,
  775. .uninit = uninit,
  776. .query_formats = smptebars_query_formats,
  777. .inputs = NULL,
  778. .outputs = smptebars_outputs,
  779. .priv_class = &smptebars_class,
  780. };
  781. #endif /* CONFIG_SMPTEHDBARS_FILTER */
  782. #endif /* CONFIG_SMPTEHDBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER */