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.

649 lines
21KB

  1. /*
  2. * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Misc test sources.
  24. *
  25. * testsrc is based on the test pattern generator demuxer by Nicolas George:
  26. * http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2007-October/037845.html
  27. *
  28. * rgbtestsrc is ported from MPlayer libmpcodecs/vf_rgbtest.c by
  29. * Michael Niedermayer.
  30. */
  31. #include <float.h>
  32. #include "libavutil/opt.h"
  33. #include "libavutil/imgutils.h"
  34. #include "libavutil/intreadwrite.h"
  35. #include "libavutil/parseutils.h"
  36. #include "avfilter.h"
  37. #include "drawutils.h"
  38. #include "formats.h"
  39. #include "internal.h"
  40. #include "video.h"
  41. typedef struct {
  42. const AVClass *class;
  43. int w, h;
  44. unsigned int nb_frame;
  45. AVRational time_base;
  46. int64_t pts, max_pts;
  47. char *rate; ///< video frame rate
  48. char *duration; ///< total duration of the generated video
  49. AVRational sar; ///< sample aspect ratio
  50. int nb_decimals;
  51. int draw_once; ///< draw only the first frame, always put out the same picture
  52. AVFilterBufferRef *picref; ///< cached reference containing the painted picture
  53. void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
  54. /* only used by color */
  55. char *color_str;
  56. FFDrawContext draw;
  57. FFDrawColor color;
  58. uint8_t color_rgba[4];
  59. /* only used by rgbtest */
  60. uint8_t rgba_map[4];
  61. } TestSourceContext;
  62. #define OFFSET(x) offsetof(TestSourceContext, x)
  63. static const AVOption options[] = {
  64. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0 },
  65. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0 },
  66. { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
  67. { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
  68. { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  69. { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  70. { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1}, 0, INT_MAX },
  71. /* only used by color */
  72. { "color", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX },
  73. { "c", "set color", OFFSET(color_str), AV_OPT_TYPE_STRING, {.str = "black"}, CHAR_MIN, CHAR_MAX },
  74. /* only used by testsrc */
  75. { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0}, INT_MIN, INT_MAX },
  76. { "n", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0}, INT_MIN, INT_MAX },
  77. { NULL },
  78. };
  79. static av_cold int init(AVFilterContext *ctx, const char *args)
  80. {
  81. TestSourceContext *test = ctx->priv;
  82. AVRational frame_rate_q;
  83. int64_t duration = -1;
  84. int ret = 0;
  85. av_opt_set_defaults(test);
  86. if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
  87. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  88. return ret;
  89. }
  90. if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0) {
  91. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
  92. return ret;
  93. }
  94. if (test->duration && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
  95. av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
  96. return ret;
  97. }
  98. if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
  99. av_log(ctx, AV_LOG_WARNING,
  100. "Option 'decimals' is ignored with source '%s'\n",
  101. ctx->filter->name);
  102. }
  103. if (test->color_str && strcmp(ctx->filter->name, "color")) {
  104. av_log(ctx, AV_LOG_WARNING,
  105. "Option 'color' is ignored with source '%s'\n",
  106. ctx->filter->name);
  107. }
  108. if ((ret = av_parse_color(test->color_rgba, test->color_str, -1, ctx)) < 0)
  109. return ret;
  110. test->time_base.num = frame_rate_q.den;
  111. test->time_base.den = frame_rate_q.num;
  112. test->max_pts = duration >= 0 ?
  113. av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
  114. test->nb_frame = 0;
  115. test->pts = 0;
  116. av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
  117. test->w, test->h, frame_rate_q.num, frame_rate_q.den,
  118. duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
  119. test->sar.num, test->sar.den);
  120. return 0;
  121. }
  122. static av_cold void uninit(AVFilterContext *ctx)
  123. {
  124. TestSourceContext *test = ctx->priv;
  125. av_opt_free(test);
  126. avfilter_unref_bufferp(&test->picref);
  127. }
  128. static int config_props(AVFilterLink *outlink)
  129. {
  130. TestSourceContext *test = outlink->src->priv;
  131. outlink->w = test->w;
  132. outlink->h = test->h;
  133. outlink->sample_aspect_ratio = test->sar;
  134. outlink->time_base = test->time_base;
  135. return 0;
  136. }
  137. static int request_frame(AVFilterLink *outlink)
  138. {
  139. TestSourceContext *test = outlink->src->priv;
  140. AVFilterBufferRef *outpicref;
  141. int ret = 0;
  142. if (test->max_pts >= 0 && test->pts >= test->max_pts)
  143. return AVERROR_EOF;
  144. if (test->draw_once) {
  145. if (!test->picref) {
  146. test->picref =
  147. ff_get_video_buffer(outlink, AV_PERM_WRITE|AV_PERM_PRESERVE|AV_PERM_REUSE,
  148. test->w, test->h);
  149. if (!test->picref)
  150. return AVERROR(ENOMEM);
  151. test->fill_picture_fn(outlink->src, test->picref);
  152. }
  153. outpicref = avfilter_ref_buffer(test->picref, ~AV_PERM_WRITE);
  154. } else
  155. outpicref = ff_get_video_buffer(outlink, AV_PERM_WRITE, test->w, test->h);
  156. if (!outpicref)
  157. return AVERROR(ENOMEM);
  158. outpicref->pts = test->pts;
  159. outpicref->pos = -1;
  160. outpicref->video->key_frame = 1;
  161. outpicref->video->interlaced = 0;
  162. outpicref->video->pict_type = AV_PICTURE_TYPE_I;
  163. outpicref->video->sample_aspect_ratio = test->sar;
  164. if (!test->draw_once)
  165. test->fill_picture_fn(outlink->src, outpicref);
  166. test->pts++;
  167. test->nb_frame++;
  168. if ((ret = ff_start_frame(outlink, outpicref)) < 0 ||
  169. (ret = ff_draw_slice(outlink, 0, test->h, 1)) < 0 ||
  170. (ret = ff_end_frame(outlink)) < 0)
  171. return ret;
  172. return 0;
  173. }
  174. #if CONFIG_COLOR_FILTER
  175. #define color_options options
  176. AVFILTER_DEFINE_CLASS(color);
  177. static void color_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  178. {
  179. TestSourceContext *test = ctx->priv;
  180. ff_fill_rectangle(&test->draw, &test->color,
  181. picref->data, picref->linesize,
  182. 0, 0, test->w, test->h);
  183. }
  184. static av_cold int color_init(AVFilterContext *ctx, const char *args)
  185. {
  186. TestSourceContext *test = ctx->priv;
  187. test->class = &color_class;
  188. test->fill_picture_fn = color_fill_picture;
  189. test->draw_once = 1;
  190. return init(ctx, args);
  191. }
  192. static int color_query_formats(AVFilterContext *ctx)
  193. {
  194. ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  195. return 0;
  196. }
  197. static int color_config_props(AVFilterLink *inlink)
  198. {
  199. AVFilterContext *ctx = inlink->src;
  200. TestSourceContext *test = ctx->priv;
  201. int ret;
  202. ff_draw_init(&test->draw, inlink->format, 0);
  203. ff_draw_color(&test->draw, &test->color, test->color_rgba);
  204. test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
  205. test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
  206. if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
  207. return AVERROR(EINVAL);
  208. if (ret = config_props(inlink) < 0)
  209. return ret;
  210. av_log(ctx, AV_LOG_VERBOSE, "color:0x%02x%02x%02x%02x\n",
  211. test->color_rgba[0], test->color_rgba[1], test->color_rgba[2], test->color_rgba[3]);
  212. return 0;
  213. }
  214. AVFilter avfilter_vsrc_color = {
  215. .name = "color",
  216. .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
  217. .priv_size = sizeof(TestSourceContext),
  218. .init = color_init,
  219. .uninit = uninit,
  220. .query_formats = color_query_formats,
  221. .inputs = (const AVFilterPad[]) {
  222. { .name = NULL }
  223. },
  224. .outputs = (const AVFilterPad[]) {
  225. {
  226. .name = "default",
  227. .type = AVMEDIA_TYPE_VIDEO,
  228. .request_frame = request_frame,
  229. .config_props = color_config_props,
  230. },
  231. { .name = NULL }
  232. },
  233. };
  234. #endif /* CONFIG_COLOR_FILTER */
  235. #if CONFIG_NULLSRC_FILTER
  236. #define nullsrc_options options
  237. AVFILTER_DEFINE_CLASS(nullsrc);
  238. static void nullsrc_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref) { }
  239. static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args)
  240. {
  241. TestSourceContext *test = ctx->priv;
  242. test->class = &nullsrc_class;
  243. test->fill_picture_fn = nullsrc_fill_picture;
  244. return init(ctx, args);
  245. }
  246. AVFilter avfilter_vsrc_nullsrc = {
  247. .name = "nullsrc",
  248. .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
  249. .init = nullsrc_init,
  250. .uninit = uninit,
  251. .priv_size = sizeof(TestSourceContext),
  252. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  253. .outputs = (const AVFilterPad[]) {{ .name = "default",
  254. .type = AVMEDIA_TYPE_VIDEO,
  255. .request_frame = request_frame,
  256. .config_props = config_props, },
  257. { .name = NULL}},
  258. };
  259. #endif /* CONFIG_NULLSRC_FILTER */
  260. #if CONFIG_TESTSRC_FILTER
  261. #define testsrc_options options
  262. AVFILTER_DEFINE_CLASS(testsrc);
  263. /**
  264. * Fill a rectangle with value val.
  265. *
  266. * @param val the RGB value to set
  267. * @param dst pointer to the destination buffer to fill
  268. * @param dst_linesize linesize of destination
  269. * @param segment_width width of the segment
  270. * @param x horizontal coordinate where to draw the rectangle in the destination buffer
  271. * @param y horizontal coordinate where to draw the rectangle in the destination buffer
  272. * @param w width of the rectangle to draw, expressed as a number of segment_width units
  273. * @param h height of the rectangle to draw, expressed as a number of segment_width units
  274. */
  275. static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
  276. unsigned x, unsigned y, unsigned w, unsigned h)
  277. {
  278. int i;
  279. int step = 3;
  280. dst += segment_width * (step * x + y * dst_linesize);
  281. w *= segment_width * step;
  282. h *= segment_width;
  283. for (i = 0; i < h; i++) {
  284. memset(dst, val, w);
  285. dst += dst_linesize;
  286. }
  287. }
  288. static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
  289. unsigned segment_width)
  290. {
  291. #define TOP_HBAR 1
  292. #define MID_HBAR 2
  293. #define BOT_HBAR 4
  294. #define LEFT_TOP_VBAR 8
  295. #define LEFT_BOT_VBAR 16
  296. #define RIGHT_TOP_VBAR 32
  297. #define RIGHT_BOT_VBAR 64
  298. struct {
  299. int x, y, w, h;
  300. } segments[] = {
  301. { 1, 0, 5, 1 }, /* TOP_HBAR */
  302. { 1, 6, 5, 1 }, /* MID_HBAR */
  303. { 1, 12, 5, 1 }, /* BOT_HBAR */
  304. { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
  305. { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
  306. { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
  307. { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
  308. };
  309. static const unsigned char masks[10] = {
  310. /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  311. /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  312. /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
  313. /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  314. /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  315. /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
  316. /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
  317. /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  318. /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  319. /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  320. };
  321. unsigned mask = masks[digit];
  322. int i;
  323. draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
  324. for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
  325. if (mask & (1<<i))
  326. draw_rectangle(255, dst, dst_linesize, segment_width,
  327. segments[i].x, segments[i].y, segments[i].w, segments[i].h);
  328. }
  329. #define GRADIENT_SIZE (6 * 256)
  330. static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  331. {
  332. TestSourceContext *test = ctx->priv;
  333. uint8_t *p, *p0;
  334. int x, y;
  335. int color, color_rest;
  336. int icolor;
  337. int radius;
  338. int quad0, quad;
  339. int dquad_x, dquad_y;
  340. int grad, dgrad, rgrad, drgrad;
  341. int seg_size;
  342. int second;
  343. int i;
  344. uint8_t *data = picref->data[0];
  345. int width = picref->video->w;
  346. int height = picref->video->h;
  347. /* draw colored bars and circle */
  348. radius = (width + height) / 4;
  349. quad0 = width * width / 4 + height * height / 4 - radius * radius;
  350. dquad_y = 1 - height;
  351. p0 = data;
  352. for (y = 0; y < height; y++) {
  353. p = p0;
  354. color = 0;
  355. color_rest = 0;
  356. quad = quad0;
  357. dquad_x = 1 - width;
  358. for (x = 0; x < width; x++) {
  359. icolor = color;
  360. if (quad < 0)
  361. icolor ^= 7;
  362. quad += dquad_x;
  363. dquad_x += 2;
  364. *(p++) = icolor & 1 ? 255 : 0;
  365. *(p++) = icolor & 2 ? 255 : 0;
  366. *(p++) = icolor & 4 ? 255 : 0;
  367. color_rest += 8;
  368. if (color_rest >= width) {
  369. color_rest -= width;
  370. color++;
  371. }
  372. }
  373. quad0 += dquad_y;
  374. dquad_y += 2;
  375. p0 += picref->linesize[0];
  376. }
  377. /* draw sliding color line */
  378. p0 = p = data + picref->linesize[0] * height * 3/4;
  379. grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
  380. GRADIENT_SIZE;
  381. rgrad = 0;
  382. dgrad = GRADIENT_SIZE / width;
  383. drgrad = GRADIENT_SIZE % width;
  384. for (x = 0; x < width; x++) {
  385. *(p++) =
  386. grad < 256 || grad >= 5 * 256 ? 255 :
  387. grad >= 2 * 256 && grad < 4 * 256 ? 0 :
  388. grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
  389. *(p++) =
  390. grad >= 4 * 256 ? 0 :
  391. grad >= 1 * 256 && grad < 3 * 256 ? 255 :
  392. grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
  393. *(p++) =
  394. grad < 2 * 256 ? 0 :
  395. grad >= 3 * 256 && grad < 5 * 256 ? 255 :
  396. grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
  397. grad += dgrad;
  398. rgrad += drgrad;
  399. if (rgrad >= GRADIENT_SIZE) {
  400. grad++;
  401. rgrad -= GRADIENT_SIZE;
  402. }
  403. if (grad >= GRADIENT_SIZE)
  404. grad -= GRADIENT_SIZE;
  405. }
  406. p = p0;
  407. for (y = height / 8; y > 0; y--) {
  408. memcpy(p+picref->linesize[0], p, 3 * width);
  409. p += picref->linesize[0];
  410. }
  411. /* draw digits */
  412. seg_size = width / 80;
  413. if (seg_size >= 1 && height >= 13 * seg_size) {
  414. double time = av_q2d(test->time_base) * test->nb_frame *
  415. pow(10, test->nb_decimals);
  416. if (time > INT_MAX)
  417. return;
  418. second = (int)time;
  419. x = width - (width - seg_size * 64) / 2;
  420. y = (height - seg_size * 13) / 2;
  421. p = data + (x*3 + y * picref->linesize[0]);
  422. for (i = 0; i < 8; i++) {
  423. p -= 3 * 8 * seg_size;
  424. draw_digit(second % 10, p, picref->linesize[0], seg_size);
  425. second /= 10;
  426. if (second == 0)
  427. break;
  428. }
  429. }
  430. }
  431. static av_cold int test_init(AVFilterContext *ctx, const char *args)
  432. {
  433. TestSourceContext *test = ctx->priv;
  434. test->class = &testsrc_class;
  435. test->fill_picture_fn = test_fill_picture;
  436. return init(ctx, args);
  437. }
  438. static int test_query_formats(AVFilterContext *ctx)
  439. {
  440. static const enum PixelFormat pix_fmts[] = {
  441. PIX_FMT_RGB24, PIX_FMT_NONE
  442. };
  443. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  444. return 0;
  445. }
  446. AVFilter avfilter_vsrc_testsrc = {
  447. .name = "testsrc",
  448. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  449. .priv_size = sizeof(TestSourceContext),
  450. .init = test_init,
  451. .uninit = uninit,
  452. .query_formats = test_query_formats,
  453. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  454. .outputs = (const AVFilterPad[]) {{ .name = "default",
  455. .type = AVMEDIA_TYPE_VIDEO,
  456. .request_frame = request_frame,
  457. .config_props = config_props, },
  458. { .name = NULL }},
  459. };
  460. #endif /* CONFIG_TESTSRC_FILTER */
  461. #if CONFIG_RGBTESTSRC_FILTER
  462. #define rgbtestsrc_options options
  463. AVFILTER_DEFINE_CLASS(rgbtestsrc);
  464. #define R 0
  465. #define G 1
  466. #define B 2
  467. #define A 3
  468. static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
  469. int x, int y, int r, int g, int b, enum PixelFormat fmt,
  470. uint8_t rgba_map[4])
  471. {
  472. int32_t v;
  473. uint8_t *p;
  474. switch (fmt) {
  475. case PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
  476. case PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
  477. case PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
  478. case PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
  479. case PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
  480. case PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
  481. case PIX_FMT_RGB24:
  482. case PIX_FMT_BGR24:
  483. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  484. p = dst + 3*x + y*dst_linesize;
  485. AV_WL24(p, v);
  486. break;
  487. case PIX_FMT_RGBA:
  488. case PIX_FMT_BGRA:
  489. case PIX_FMT_ARGB:
  490. case PIX_FMT_ABGR:
  491. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
  492. p = dst + 4*x + y*dst_linesize;
  493. AV_WL32(p, v);
  494. break;
  495. }
  496. }
  497. static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  498. {
  499. TestSourceContext *test = ctx->priv;
  500. int x, y, w = picref->video->w, h = picref->video->h;
  501. for (y = 0; y < h; y++) {
  502. for (x = 0; x < picref->video->w; x++) {
  503. int c = 256*x/w;
  504. int r = 0, g = 0, b = 0;
  505. if (3*y < h ) r = c;
  506. else if (3*y < 2*h) g = c;
  507. else b = c;
  508. rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
  509. ctx->outputs[0]->format, test->rgba_map);
  510. }
  511. }
  512. }
  513. static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
  514. {
  515. TestSourceContext *test = ctx->priv;
  516. test->draw_once = 1;
  517. test->class = &rgbtestsrc_class;
  518. test->fill_picture_fn = rgbtest_fill_picture;
  519. return init(ctx, args);
  520. }
  521. static int rgbtest_query_formats(AVFilterContext *ctx)
  522. {
  523. static const enum PixelFormat pix_fmts[] = {
  524. PIX_FMT_RGBA, PIX_FMT_ARGB, PIX_FMT_BGRA, PIX_FMT_ABGR,
  525. PIX_FMT_BGR24, PIX_FMT_RGB24,
  526. PIX_FMT_RGB444, PIX_FMT_BGR444,
  527. PIX_FMT_RGB565, PIX_FMT_BGR565,
  528. PIX_FMT_RGB555, PIX_FMT_BGR555,
  529. PIX_FMT_NONE
  530. };
  531. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  532. return 0;
  533. }
  534. static int rgbtest_config_props(AVFilterLink *outlink)
  535. {
  536. TestSourceContext *test = outlink->src->priv;
  537. ff_fill_rgba_map(test->rgba_map, outlink->format);
  538. return config_props(outlink);
  539. }
  540. AVFilter avfilter_vsrc_rgbtestsrc = {
  541. .name = "rgbtestsrc",
  542. .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
  543. .priv_size = sizeof(TestSourceContext),
  544. .init = rgbtest_init,
  545. .uninit = uninit,
  546. .query_formats = rgbtest_query_formats,
  547. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  548. .outputs = (const AVFilterPad[]) {{ .name = "default",
  549. .type = AVMEDIA_TYPE_VIDEO,
  550. .request_frame = request_frame,
  551. .config_props = rgbtest_config_props, },
  552. { .name = NULL }},
  553. };
  554. #endif /* CONFIG_RGBTESTSRC_FILTER */