vsrc_testsrc.c 26 KB

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