vsrc_testsrc.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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. * allyuv, smptebars and smptehdbars are 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 "libavutil/xga_font_data.h"
  42. #include "avfilter.h"
  43. #include "drawutils.h"
  44. #include "formats.h"
  45. #include "internal.h"
  46. #include "video.h"
  47. typedef struct TestSourceContext {
  48. const AVClass *class;
  49. int w, h;
  50. unsigned int nb_frame;
  51. AVRational time_base, frame_rate;
  52. int64_t pts;
  53. int64_t duration; ///< duration expressed in microseconds
  54. AVRational sar; ///< sample aspect ratio
  55. int draw_once; ///< draw only the first frame, always put out the same picture
  56. int draw_once_reset; ///< draw only the first frame or in case of reset
  57. AVFrame *picref; ///< cached reference containing the painted picture
  58. void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
  59. /* only used by testsrc */
  60. int nb_decimals;
  61. /* only used by color */
  62. FFDrawContext draw;
  63. FFDrawColor color;
  64. uint8_t color_rgba[4];
  65. /* only used by rgbtest */
  66. uint8_t rgba_map[4];
  67. /* only used by haldclut */
  68. int level;
  69. } TestSourceContext;
  70. #define OFFSET(x) offsetof(TestSourceContext, x)
  71. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  72. #define SIZE_OPTIONS \
  73. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
  74. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0, FLAGS },\
  75. #define COMMON_OPTIONS_NOSIZE \
  76. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
  77. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },\
  78. { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
  79. { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },\
  80. { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1}, 0, INT_MAX, FLAGS },
  81. #define COMMON_OPTIONS SIZE_OPTIONS COMMON_OPTIONS_NOSIZE
  82. static const AVOption options[] = {
  83. COMMON_OPTIONS
  84. { NULL }
  85. };
  86. static av_cold int init(AVFilterContext *ctx)
  87. {
  88. TestSourceContext *test = ctx->priv;
  89. test->time_base = av_inv_q(test->frame_rate);
  90. test->nb_frame = 0;
  91. test->pts = 0;
  92. av_log(ctx, AV_LOG_VERBOSE, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
  93. test->w, test->h, test->frame_rate.num, test->frame_rate.den,
  94. test->duration < 0 ? -1 : (double)test->duration/1000000,
  95. test->sar.num, test->sar.den);
  96. return 0;
  97. }
  98. static av_cold void uninit(AVFilterContext *ctx)
  99. {
  100. TestSourceContext *test = ctx->priv;
  101. av_frame_free(&test->picref);
  102. }
  103. static int config_props(AVFilterLink *outlink)
  104. {
  105. TestSourceContext *test = outlink->src->priv;
  106. outlink->w = test->w;
  107. outlink->h = test->h;
  108. outlink->sample_aspect_ratio = test->sar;
  109. outlink->frame_rate = test->frame_rate;
  110. outlink->time_base = test->time_base;
  111. return 0;
  112. }
  113. static int request_frame(AVFilterLink *outlink)
  114. {
  115. TestSourceContext *test = outlink->src->priv;
  116. AVFrame *frame;
  117. if (test->duration >= 0 &&
  118. av_rescale_q(test->pts, test->time_base, AV_TIME_BASE_Q) >= test->duration)
  119. return AVERROR_EOF;
  120. if (test->draw_once) {
  121. if (test->draw_once_reset) {
  122. av_frame_free(&test->picref);
  123. test->draw_once_reset = 0;
  124. }
  125. if (!test->picref) {
  126. test->picref =
  127. ff_get_video_buffer(outlink, test->w, test->h);
  128. if (!test->picref)
  129. return AVERROR(ENOMEM);
  130. test->fill_picture_fn(outlink->src, test->picref);
  131. }
  132. frame = av_frame_clone(test->picref);
  133. } else
  134. frame = ff_get_video_buffer(outlink, test->w, test->h);
  135. if (!frame)
  136. return AVERROR(ENOMEM);
  137. frame->pts = test->pts;
  138. frame->key_frame = 1;
  139. frame->interlaced_frame = 0;
  140. frame->pict_type = AV_PICTURE_TYPE_I;
  141. frame->sample_aspect_ratio = test->sar;
  142. if (!test->draw_once)
  143. test->fill_picture_fn(outlink->src, frame);
  144. test->pts++;
  145. test->nb_frame++;
  146. return ff_filter_frame(outlink, frame);
  147. }
  148. #if CONFIG_COLOR_FILTER
  149. static const AVOption color_options[] = {
  150. { "color", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
  151. { "c", "set color", OFFSET(color_rgba), AV_OPT_TYPE_COLOR, {.str = "black"}, CHAR_MIN, CHAR_MAX, FLAGS },
  152. COMMON_OPTIONS
  153. { NULL }
  154. };
  155. AVFILTER_DEFINE_CLASS(color);
  156. static void color_fill_picture(AVFilterContext *ctx, AVFrame *picref)
  157. {
  158. TestSourceContext *test = ctx->priv;
  159. ff_fill_rectangle(&test->draw, &test->color,
  160. picref->data, picref->linesize,
  161. 0, 0, test->w, test->h);
  162. }
  163. static av_cold int color_init(AVFilterContext *ctx)
  164. {
  165. TestSourceContext *test = ctx->priv;
  166. test->fill_picture_fn = color_fill_picture;
  167. test->draw_once = 1;
  168. return init(ctx);
  169. }
  170. static int color_query_formats(AVFilterContext *ctx)
  171. {
  172. return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  173. }
  174. static int color_config_props(AVFilterLink *inlink)
  175. {
  176. AVFilterContext *ctx = inlink->src;
  177. TestSourceContext *test = ctx->priv;
  178. int ret;
  179. ff_draw_init(&test->draw, inlink->format, 0);
  180. ff_draw_color(&test->draw, &test->color, test->color_rgba);
  181. test->w = ff_draw_round_to_sub(&test->draw, 0, -1, test->w);
  182. test->h = ff_draw_round_to_sub(&test->draw, 1, -1, test->h);
  183. if (av_image_check_size(test->w, test->h, 0, ctx) < 0)
  184. return AVERROR(EINVAL);
  185. if ((ret = config_props(inlink)) < 0)
  186. return ret;
  187. return 0;
  188. }
  189. static int color_process_command(AVFilterContext *ctx, const char *cmd, const char *args,
  190. char *res, int res_len, int flags)
  191. {
  192. TestSourceContext *test = ctx->priv;
  193. int ret;
  194. if (!strcmp(cmd, "color") || !strcmp(cmd, "c")) {
  195. uint8_t color_rgba[4];
  196. ret = av_parse_color(color_rgba, args, -1, ctx);
  197. if (ret < 0)
  198. return ret;
  199. memcpy(test->color_rgba, color_rgba, sizeof(color_rgba));
  200. ff_draw_color(&test->draw, &test->color, test->color_rgba);
  201. test->draw_once_reset = 1;
  202. return 0;
  203. }
  204. return AVERROR(ENOSYS);
  205. }
  206. static const AVFilterPad color_outputs[] = {
  207. {
  208. .name = "default",
  209. .type = AVMEDIA_TYPE_VIDEO,
  210. .request_frame = request_frame,
  211. .config_props = color_config_props,
  212. },
  213. { NULL }
  214. };
  215. AVFilter ff_vsrc_color = {
  216. .name = "color",
  217. .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input."),
  218. .priv_class = &color_class,
  219. .priv_size = sizeof(TestSourceContext),
  220. .init = color_init,
  221. .uninit = uninit,
  222. .query_formats = color_query_formats,
  223. .inputs = NULL,
  224. .outputs = color_outputs,
  225. .process_command = color_process_command,
  226. };
  227. #endif /* CONFIG_COLOR_FILTER */
  228. #if CONFIG_HALDCLUTSRC_FILTER
  229. static const AVOption haldclutsrc_options[] = {
  230. { "level", "set level", OFFSET(level), AV_OPT_TYPE_INT, {.i64 = 6}, 2, 8, FLAGS },
  231. COMMON_OPTIONS_NOSIZE
  232. { NULL }
  233. };
  234. AVFILTER_DEFINE_CLASS(haldclutsrc);
  235. static void haldclutsrc_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  236. {
  237. int i, j, k, x = 0, y = 0, is16bit = 0, step;
  238. uint32_t alpha = 0;
  239. const TestSourceContext *hc = ctx->priv;
  240. int level = hc->level;
  241. float scale;
  242. const int w = frame->width;
  243. const int h = frame->height;
  244. const uint8_t *data = frame->data[0];
  245. const int linesize = frame->linesize[0];
  246. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  247. uint8_t rgba_map[4];
  248. av_assert0(w == h && w == level*level*level);
  249. ff_fill_rgba_map(rgba_map, frame->format);
  250. switch (frame->format) {
  251. case AV_PIX_FMT_RGB48:
  252. case AV_PIX_FMT_BGR48:
  253. case AV_PIX_FMT_RGBA64:
  254. case AV_PIX_FMT_BGRA64:
  255. is16bit = 1;
  256. alpha = 0xffff;
  257. break;
  258. case AV_PIX_FMT_RGBA:
  259. case AV_PIX_FMT_BGRA:
  260. case AV_PIX_FMT_ARGB:
  261. case AV_PIX_FMT_ABGR:
  262. alpha = 0xff;
  263. break;
  264. }
  265. step = av_get_padded_bits_per_pixel(desc) >> (3 + is16bit);
  266. scale = ((float)(1 << (8*(is16bit+1))) - 1) / (level*level - 1);
  267. #define LOAD_CLUT(nbits) do { \
  268. uint##nbits##_t *dst = ((uint##nbits##_t *)(data + y*linesize)) + x*step; \
  269. dst[rgba_map[0]] = av_clip_uint##nbits(i * scale); \
  270. dst[rgba_map[1]] = av_clip_uint##nbits(j * scale); \
  271. dst[rgba_map[2]] = av_clip_uint##nbits(k * scale); \
  272. if (step == 4) \
  273. dst[rgba_map[3]] = alpha; \
  274. } while (0)
  275. level *= level;
  276. for (k = 0; k < level; k++) {
  277. for (j = 0; j < level; j++) {
  278. for (i = 0; i < level; i++) {
  279. if (!is16bit)
  280. LOAD_CLUT(8);
  281. else
  282. LOAD_CLUT(16);
  283. if (++x == w) {
  284. x = 0;
  285. y++;
  286. }
  287. }
  288. }
  289. }
  290. }
  291. static av_cold int haldclutsrc_init(AVFilterContext *ctx)
  292. {
  293. TestSourceContext *hc = ctx->priv;
  294. hc->fill_picture_fn = haldclutsrc_fill_picture;
  295. hc->draw_once = 1;
  296. return init(ctx);
  297. }
  298. static int haldclutsrc_query_formats(AVFilterContext *ctx)
  299. {
  300. static const enum AVPixelFormat pix_fmts[] = {
  301. AV_PIX_FMT_RGB24, AV_PIX_FMT_BGR24,
  302. AV_PIX_FMT_RGBA, AV_PIX_FMT_BGRA,
  303. AV_PIX_FMT_ARGB, AV_PIX_FMT_ABGR,
  304. AV_PIX_FMT_0RGB, AV_PIX_FMT_0BGR,
  305. AV_PIX_FMT_RGB0, AV_PIX_FMT_BGR0,
  306. AV_PIX_FMT_RGB48, AV_PIX_FMT_BGR48,
  307. AV_PIX_FMT_RGBA64, AV_PIX_FMT_BGRA64,
  308. AV_PIX_FMT_NONE,
  309. };
  310. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  311. if (!fmts_list)
  312. return AVERROR(ENOMEM);
  313. return ff_set_common_formats(ctx, fmts_list);
  314. }
  315. static int haldclutsrc_config_props(AVFilterLink *outlink)
  316. {
  317. AVFilterContext *ctx = outlink->src;
  318. TestSourceContext *hc = ctx->priv;
  319. hc->w = hc->h = hc->level * hc->level * hc->level;
  320. return config_props(outlink);
  321. }
  322. static const AVFilterPad haldclutsrc_outputs[] = {
  323. {
  324. .name = "default",
  325. .type = AVMEDIA_TYPE_VIDEO,
  326. .request_frame = request_frame,
  327. .config_props = haldclutsrc_config_props,
  328. },
  329. { NULL }
  330. };
  331. AVFilter ff_vsrc_haldclutsrc = {
  332. .name = "haldclutsrc",
  333. .description = NULL_IF_CONFIG_SMALL("Provide an identity Hald CLUT."),
  334. .priv_class = &haldclutsrc_class,
  335. .priv_size = sizeof(TestSourceContext),
  336. .init = haldclutsrc_init,
  337. .uninit = uninit,
  338. .query_formats = haldclutsrc_query_formats,
  339. .inputs = NULL,
  340. .outputs = haldclutsrc_outputs,
  341. };
  342. #endif /* CONFIG_HALDCLUTSRC_FILTER */
  343. #if CONFIG_NULLSRC_FILTER
  344. #define nullsrc_options options
  345. AVFILTER_DEFINE_CLASS(nullsrc);
  346. static void nullsrc_fill_picture(AVFilterContext *ctx, AVFrame *picref) { }
  347. static av_cold int nullsrc_init(AVFilterContext *ctx)
  348. {
  349. TestSourceContext *test = ctx->priv;
  350. test->fill_picture_fn = nullsrc_fill_picture;
  351. return init(ctx);
  352. }
  353. static const AVFilterPad nullsrc_outputs[] = {
  354. {
  355. .name = "default",
  356. .type = AVMEDIA_TYPE_VIDEO,
  357. .request_frame = request_frame,
  358. .config_props = config_props,
  359. },
  360. { NULL },
  361. };
  362. AVFilter ff_vsrc_nullsrc = {
  363. .name = "nullsrc",
  364. .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
  365. .init = nullsrc_init,
  366. .uninit = uninit,
  367. .priv_size = sizeof(TestSourceContext),
  368. .priv_class = &nullsrc_class,
  369. .inputs = NULL,
  370. .outputs = nullsrc_outputs,
  371. };
  372. #endif /* CONFIG_NULLSRC_FILTER */
  373. #if CONFIG_TESTSRC_FILTER
  374. static const AVOption testsrc_options[] = {
  375. COMMON_OPTIONS
  376. { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, 0, 17, FLAGS },
  377. { "n", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.i64=0}, 0, 17, FLAGS },
  378. { NULL }
  379. };
  380. AVFILTER_DEFINE_CLASS(testsrc);
  381. /**
  382. * Fill a rectangle with value val.
  383. *
  384. * @param val the RGB value to set
  385. * @param dst pointer to the destination buffer to fill
  386. * @param dst_linesize linesize of destination
  387. * @param segment_width width of the segment
  388. * @param x horizontal coordinate where to draw the rectangle in the destination buffer
  389. * @param y horizontal coordinate where to draw the rectangle in the destination buffer
  390. * @param w width of the rectangle to draw, expressed as a number of segment_width units
  391. * @param h height of the rectangle to draw, expressed as a number of segment_width units
  392. */
  393. static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, int segment_width,
  394. int x, int y, int w, int h)
  395. {
  396. int i;
  397. int step = 3;
  398. dst += segment_width * (step * x + y * dst_linesize);
  399. w *= segment_width * step;
  400. h *= segment_width;
  401. for (i = 0; i < h; i++) {
  402. memset(dst, val, w);
  403. dst += dst_linesize;
  404. }
  405. }
  406. static void draw_digit(int digit, uint8_t *dst, int dst_linesize,
  407. int segment_width)
  408. {
  409. #define TOP_HBAR 1
  410. #define MID_HBAR 2
  411. #define BOT_HBAR 4
  412. #define LEFT_TOP_VBAR 8
  413. #define LEFT_BOT_VBAR 16
  414. #define RIGHT_TOP_VBAR 32
  415. #define RIGHT_BOT_VBAR 64
  416. struct segments {
  417. int x, y, w, h;
  418. } segments[] = {
  419. { 1, 0, 5, 1 }, /* TOP_HBAR */
  420. { 1, 6, 5, 1 }, /* MID_HBAR */
  421. { 1, 12, 5, 1 }, /* BOT_HBAR */
  422. { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
  423. { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
  424. { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
  425. { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
  426. };
  427. static const unsigned char masks[10] = {
  428. /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  429. /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  430. /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
  431. /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  432. /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  433. /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
  434. /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
  435. /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  436. /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  437. /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  438. };
  439. unsigned mask = masks[digit];
  440. int i;
  441. draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
  442. for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
  443. if (mask & (1<<i))
  444. draw_rectangle(255, dst, dst_linesize, segment_width,
  445. segments[i].x, segments[i].y, segments[i].w, segments[i].h);
  446. }
  447. #define GRADIENT_SIZE (6 * 256)
  448. static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  449. {
  450. TestSourceContext *test = ctx->priv;
  451. uint8_t *p, *p0;
  452. int x, y;
  453. int color, color_rest;
  454. int icolor;
  455. int radius;
  456. int quad0, quad;
  457. int dquad_x, dquad_y;
  458. int grad, dgrad, rgrad, drgrad;
  459. int seg_size;
  460. int second;
  461. int i;
  462. uint8_t *data = frame->data[0];
  463. int width = frame->width;
  464. int height = frame->height;
  465. /* draw colored bars and circle */
  466. radius = (width + height) / 4;
  467. quad0 = width * width / 4 + height * height / 4 - radius * radius;
  468. dquad_y = 1 - height;
  469. p0 = data;
  470. for (y = 0; y < height; y++) {
  471. p = p0;
  472. color = 0;
  473. color_rest = 0;
  474. quad = quad0;
  475. dquad_x = 1 - width;
  476. for (x = 0; x < width; x++) {
  477. icolor = color;
  478. if (quad < 0)
  479. icolor ^= 7;
  480. quad += dquad_x;
  481. dquad_x += 2;
  482. *(p++) = icolor & 1 ? 255 : 0;
  483. *(p++) = icolor & 2 ? 255 : 0;
  484. *(p++) = icolor & 4 ? 255 : 0;
  485. color_rest += 8;
  486. if (color_rest >= width) {
  487. color_rest -= width;
  488. color++;
  489. }
  490. }
  491. quad0 += dquad_y;
  492. dquad_y += 2;
  493. p0 += frame->linesize[0];
  494. }
  495. /* draw sliding color line */
  496. p0 = p = data + frame->linesize[0] * (height * 3/4);
  497. grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
  498. GRADIENT_SIZE;
  499. rgrad = 0;
  500. dgrad = GRADIENT_SIZE / width;
  501. drgrad = GRADIENT_SIZE % width;
  502. for (x = 0; x < width; x++) {
  503. *(p++) =
  504. grad < 256 || grad >= 5 * 256 ? 255 :
  505. grad >= 2 * 256 && grad < 4 * 256 ? 0 :
  506. grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
  507. *(p++) =
  508. grad >= 4 * 256 ? 0 :
  509. grad >= 1 * 256 && grad < 3 * 256 ? 255 :
  510. grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
  511. *(p++) =
  512. grad < 2 * 256 ? 0 :
  513. grad >= 3 * 256 && grad < 5 * 256 ? 255 :
  514. grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
  515. grad += dgrad;
  516. rgrad += drgrad;
  517. if (rgrad >= GRADIENT_SIZE) {
  518. grad++;
  519. rgrad -= GRADIENT_SIZE;
  520. }
  521. if (grad >= GRADIENT_SIZE)
  522. grad -= GRADIENT_SIZE;
  523. }
  524. p = p0;
  525. for (y = height / 8; y > 0; y--) {
  526. memcpy(p+frame->linesize[0], p, 3 * width);
  527. p += frame->linesize[0];
  528. }
  529. /* draw digits */
  530. seg_size = width / 80;
  531. if (seg_size >= 1 && height >= 13 * seg_size) {
  532. int64_t p10decimals = 1;
  533. double time = av_q2d(test->time_base) * test->nb_frame *
  534. ff_exp10(test->nb_decimals);
  535. if (time >= INT_MAX)
  536. return;
  537. for (x = 0; x < test->nb_decimals; x++)
  538. p10decimals *= 10;
  539. second = av_rescale_rnd(test->nb_frame * test->time_base.num, p10decimals, test->time_base.den, AV_ROUND_ZERO);
  540. x = width - (width - seg_size * 64) / 2;
  541. y = (height - seg_size * 13) / 2;
  542. p = data + (x*3 + y * frame->linesize[0]);
  543. for (i = 0; i < 8; i++) {
  544. p -= 3 * 8 * seg_size;
  545. draw_digit(second % 10, p, frame->linesize[0], seg_size);
  546. second /= 10;
  547. if (second == 0)
  548. break;
  549. }
  550. }
  551. }
  552. static av_cold int test_init(AVFilterContext *ctx)
  553. {
  554. TestSourceContext *test = ctx->priv;
  555. test->fill_picture_fn = test_fill_picture;
  556. return init(ctx);
  557. }
  558. static int test_query_formats(AVFilterContext *ctx)
  559. {
  560. static const enum AVPixelFormat pix_fmts[] = {
  561. AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
  562. };
  563. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  564. if (!fmts_list)
  565. return AVERROR(ENOMEM);
  566. return ff_set_common_formats(ctx, fmts_list);
  567. }
  568. static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
  569. {
  570. .name = "default",
  571. .type = AVMEDIA_TYPE_VIDEO,
  572. .request_frame = request_frame,
  573. .config_props = config_props,
  574. },
  575. { NULL }
  576. };
  577. AVFilter ff_vsrc_testsrc = {
  578. .name = "testsrc",
  579. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  580. .priv_size = sizeof(TestSourceContext),
  581. .priv_class = &testsrc_class,
  582. .init = test_init,
  583. .uninit = uninit,
  584. .query_formats = test_query_formats,
  585. .inputs = NULL,
  586. .outputs = avfilter_vsrc_testsrc_outputs,
  587. };
  588. #endif /* CONFIG_TESTSRC_FILTER */
  589. #if CONFIG_TESTSRC2_FILTER
  590. static const AVOption testsrc2_options[] = {
  591. COMMON_OPTIONS
  592. { NULL }
  593. };
  594. AVFILTER_DEFINE_CLASS(testsrc2);
  595. static void set_color(TestSourceContext *s, FFDrawColor *color, uint32_t argb)
  596. {
  597. uint8_t rgba[4] = { (argb >> 16) & 0xFF,
  598. (argb >> 8) & 0xFF,
  599. (argb >> 0) & 0xFF,
  600. (argb >> 24) & 0xFF, };
  601. ff_draw_color(&s->draw, color, rgba);
  602. }
  603. static uint32_t color_gradient(unsigned index)
  604. {
  605. unsigned si = index & 0xFF, sd = 0xFF - si;
  606. switch (index >> 8) {
  607. case 0: return 0xFF0000 + (si << 8);
  608. case 1: return 0x00FF00 + (sd << 16);
  609. case 2: return 0x00FF00 + (si << 0);
  610. case 3: return 0x0000FF + (sd << 8);
  611. case 4: return 0x0000FF + (si << 16);
  612. case 5: return 0xFF0000 + (sd << 0);
  613. }
  614. av_assert0(0);
  615. }
  616. static void draw_text(TestSourceContext *s, AVFrame *frame, FFDrawColor *color,
  617. int x0, int y0, const uint8_t *text)
  618. {
  619. int x = x0;
  620. for (; *text; text++) {
  621. if (*text == '\n') {
  622. x = x0;
  623. y0 += 16;
  624. continue;
  625. }
  626. ff_blend_mask(&s->draw, color, frame->data, frame->linesize,
  627. frame->width, frame->height,
  628. avpriv_vga16_font + *text * 16, 1, 8, 16, 0, 0, x, y0);
  629. x += 8;
  630. }
  631. }
  632. static void test2_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  633. {
  634. TestSourceContext *s = ctx->priv;
  635. FFDrawColor color;
  636. /* colored background */
  637. {
  638. unsigned i, x = 0, x2;
  639. x = 0;
  640. for (i = 1; i < 7; i++) {
  641. x2 = av_rescale(i, s->w, 6);
  642. x2 = ff_draw_round_to_sub(&s->draw, 0, 0, x2);
  643. set_color(s, &color, ((i & 1) ? 0xFF0000 : 0) |
  644. ((i & 2) ? 0x00FF00 : 0) |
  645. ((i & 4) ? 0x0000FF : 0));
  646. ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
  647. x, 0, x2 - x, frame->height);
  648. x = x2;
  649. }
  650. }
  651. /* oblique gradient */
  652. /* note: too slow if using blending */
  653. if (s->h >= 64) {
  654. unsigned x, dx, y0, y, g0, g;
  655. dx = ff_draw_round_to_sub(&s->draw, 0, +1, 1);
  656. y0 = av_rescale_q(s->pts, s->time_base, av_make_q(2, s->h - 16));
  657. g0 = av_rescale_q(s->pts, s->time_base, av_make_q(1, 128));
  658. for (x = 0; x < s->w; x += dx) {
  659. g = (av_rescale(x, 6 * 256, s->w) + g0) % (6 * 256);
  660. set_color(s, &color, color_gradient(g));
  661. y = y0 + av_rescale(x, s->h / 2, s->w);
  662. y %= 2 * (s->h - 16);
  663. if (y > s->h - 16)
  664. y = 2 * (s->h - 16) - y;
  665. y = ff_draw_round_to_sub(&s->draw, 1, 0, y);
  666. ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
  667. x, y, dx, 16);
  668. }
  669. }
  670. /* top right: draw clock hands */
  671. if (s->w >= 64 && s->h >= 64) {
  672. int l = (FFMIN(s->w, s->h) - 32) >> 1;
  673. int steps = FFMAX(4, l >> 5);
  674. int xc = (s->w >> 2) + (s->w >> 1);
  675. int yc = (s->h >> 2);
  676. int cycle = l << 2;
  677. int pos, xh, yh;
  678. int c, i;
  679. for (c = 0; c < 3; c++) {
  680. set_color(s, &color, 0xBBBBBB ^ (0xFF << (c << 3)));
  681. pos = av_rescale_q(s->pts, s->time_base, av_make_q(64 >> (c << 1), cycle)) % cycle;
  682. xh = pos < 1 * l ? pos :
  683. pos < 2 * l ? l :
  684. pos < 3 * l ? 3 * l - pos : 0;
  685. yh = pos < 1 * l ? 0 :
  686. pos < 2 * l ? pos - l :
  687. pos < 3 * l ? l :
  688. cycle - pos;
  689. xh -= l >> 1;
  690. yh -= l >> 1;
  691. for (i = 1; i <= steps; i++) {
  692. int x = av_rescale(xh, i, steps) + xc;
  693. int y = av_rescale(yh, i, steps) + yc;
  694. x = ff_draw_round_to_sub(&s->draw, 0, -1, x);
  695. y = ff_draw_round_to_sub(&s->draw, 1, -1, y);
  696. ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
  697. x, y, 8, 8);
  698. }
  699. }
  700. }
  701. /* bottom left: beating rectangles */
  702. if (s->w >= 64 && s->h >= 64) {
  703. int l = (FFMIN(s->w, s->h) - 16) >> 2;
  704. int cycle = l << 3;
  705. int xc = (s->w >> 2);
  706. int yc = (s->h >> 2) + (s->h >> 1);
  707. int xm1 = ff_draw_round_to_sub(&s->draw, 0, -1, xc - 8);
  708. int xm2 = ff_draw_round_to_sub(&s->draw, 0, +1, xc + 8);
  709. int ym1 = ff_draw_round_to_sub(&s->draw, 1, -1, yc - 8);
  710. int ym2 = ff_draw_round_to_sub(&s->draw, 1, +1, yc + 8);
  711. int size, step, x1, x2, y1, y2;
  712. size = av_rescale_q(s->pts, s->time_base, av_make_q(4, cycle));
  713. step = size / l;
  714. size %= l;
  715. if (step & 1)
  716. size = l - size;
  717. step = (step >> 1) & 3;
  718. set_color(s, &color, 0xFF808080);
  719. x1 = ff_draw_round_to_sub(&s->draw, 0, -1, xc - 4 - size);
  720. x2 = ff_draw_round_to_sub(&s->draw, 0, +1, xc + 4 + size);
  721. y1 = ff_draw_round_to_sub(&s->draw, 1, -1, yc - 4 - size);
  722. y2 = ff_draw_round_to_sub(&s->draw, 1, +1, yc + 4 + size);
  723. if (step == 0 || step == 2)
  724. ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
  725. x1, ym1, x2 - x1, ym2 - ym1);
  726. if (step == 1 || step == 2)
  727. ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
  728. xm1, y1, xm2 - xm1, y2 - y1);
  729. if (step == 3)
  730. ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
  731. x1, y1, x2 - x1, y2 - y1);
  732. }
  733. /* bottom right: checker with random noise */
  734. {
  735. unsigned xmin = av_rescale(5, s->w, 8);
  736. unsigned xmax = av_rescale(7, s->w, 8);
  737. unsigned ymin = av_rescale(5, s->h, 8);
  738. unsigned ymax = av_rescale(7, s->h, 8);
  739. unsigned x, y, i, r;
  740. uint8_t alpha[256];
  741. r = s->pts;
  742. for (y = ymin; y < ymax - 15; y += 16) {
  743. for (x = xmin; x < xmax - 15; x += 16) {
  744. if ((x ^ y) & 16)
  745. continue;
  746. for (i = 0; i < 256; i++) {
  747. r = r * 1664525 + 1013904223;
  748. alpha[i] = r >> 24;
  749. }
  750. set_color(s, &color, 0xFF00FF80);
  751. ff_blend_mask(&s->draw, &color, frame->data, frame->linesize,
  752. frame->width, frame->height,
  753. alpha, 16, 16, 16, 3, 0, x, y);
  754. }
  755. }
  756. }
  757. /* bouncing square */
  758. if (s->w >= 16 && s->h >= 16) {
  759. unsigned w = s->w - 8;
  760. unsigned h = s->h - 8;
  761. unsigned x = av_rescale_q(s->pts, s->time_base, av_make_q(233, 55 * w)) % (w << 1);
  762. unsigned y = av_rescale_q(s->pts, s->time_base, av_make_q(233, 89 * h)) % (h << 1);
  763. if (x > w)
  764. x = (w << 1) - x;
  765. if (y > h)
  766. y = (h << 1) - y;
  767. x = ff_draw_round_to_sub(&s->draw, 0, -1, x);
  768. y = ff_draw_round_to_sub(&s->draw, 1, -1, y);
  769. set_color(s, &color, 0xFF8000FF);
  770. ff_fill_rectangle(&s->draw, &color, frame->data, frame->linesize,
  771. x, y, 8, 8);
  772. }
  773. /* top right: draw frame time and frame number */
  774. {
  775. char buf[256];
  776. unsigned time;
  777. time = av_rescale_q(s->pts, s->time_base, av_make_q(1, 1000)) % 86400000;
  778. set_color(s, &color, 0xC0000000);
  779. ff_blend_rectangle(&s->draw, &color, frame->data, frame->linesize,
  780. frame->width, frame->height,
  781. 2, 2, 100, 36);
  782. set_color(s, &color, 0xFFFF8000);
  783. snprintf(buf, sizeof(buf), "%02d:%02d:%02d.%03d\n%12"PRIi64,
  784. time / 3600000, (time / 60000) % 60, (time / 1000) % 60,
  785. time % 1000, s->pts);
  786. draw_text(s, frame, &color, 4, 4, buf);
  787. }
  788. }
  789. static av_cold int test2_init(AVFilterContext *ctx)
  790. {
  791. TestSourceContext *s = ctx->priv;
  792. s->fill_picture_fn = test2_fill_picture;
  793. return init(ctx);
  794. }
  795. static int test2_query_formats(AVFilterContext *ctx)
  796. {
  797. return ff_set_common_formats(ctx, ff_draw_supported_pixel_formats(0));
  798. }
  799. static int test2_config_props(AVFilterLink *inlink)
  800. {
  801. AVFilterContext *ctx = inlink->src;
  802. TestSourceContext *s = ctx->priv;
  803. av_assert0(ff_draw_init(&s->draw, inlink->format, 0) >= 0);
  804. s->w = ff_draw_round_to_sub(&s->draw, 0, -1, s->w);
  805. s->h = ff_draw_round_to_sub(&s->draw, 1, -1, s->h);
  806. if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
  807. return AVERROR(EINVAL);
  808. return config_props(inlink);
  809. }
  810. static const AVFilterPad avfilter_vsrc_testsrc2_outputs[] = {
  811. {
  812. .name = "default",
  813. .type = AVMEDIA_TYPE_VIDEO,
  814. .request_frame = request_frame,
  815. .config_props = test2_config_props,
  816. },
  817. { NULL }
  818. };
  819. AVFilter ff_vsrc_testsrc2 = {
  820. .name = "testsrc2",
  821. .description = NULL_IF_CONFIG_SMALL("Generate another test pattern."),
  822. .priv_size = sizeof(TestSourceContext),
  823. .priv_class = &testsrc2_class,
  824. .init = test2_init,
  825. .uninit = uninit,
  826. .query_formats = test2_query_formats,
  827. .inputs = NULL,
  828. .outputs = avfilter_vsrc_testsrc2_outputs,
  829. };
  830. #endif /* CONFIG_TESTSRC2_FILTER */
  831. #if CONFIG_RGBTESTSRC_FILTER
  832. #define rgbtestsrc_options options
  833. AVFILTER_DEFINE_CLASS(rgbtestsrc);
  834. #define R 0
  835. #define G 1
  836. #define B 2
  837. #define A 3
  838. static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
  839. int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
  840. uint8_t rgba_map[4])
  841. {
  842. int32_t v;
  843. uint8_t *p;
  844. switch (fmt) {
  845. case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
  846. case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
  847. case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
  848. case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
  849. case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
  850. case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
  851. case AV_PIX_FMT_RGB24:
  852. case AV_PIX_FMT_BGR24:
  853. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  854. p = dst + 3*x + y*dst_linesize;
  855. AV_WL24(p, v);
  856. break;
  857. case AV_PIX_FMT_RGBA:
  858. case AV_PIX_FMT_BGRA:
  859. case AV_PIX_FMT_ARGB:
  860. case AV_PIX_FMT_ABGR:
  861. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
  862. p = dst + 4*x + y*dst_linesize;
  863. AV_WL32(p, v);
  864. break;
  865. }
  866. }
  867. static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  868. {
  869. TestSourceContext *test = ctx->priv;
  870. int x, y, w = frame->width, h = frame->height;
  871. for (y = 0; y < h; y++) {
  872. for (x = 0; x < w; x++) {
  873. int c = 256*x/w;
  874. int r = 0, g = 0, b = 0;
  875. if (3*y < h ) r = c;
  876. else if (3*y < 2*h) g = c;
  877. else b = c;
  878. rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
  879. ctx->outputs[0]->format, test->rgba_map);
  880. }
  881. }
  882. }
  883. static av_cold int rgbtest_init(AVFilterContext *ctx)
  884. {
  885. TestSourceContext *test = ctx->priv;
  886. test->draw_once = 1;
  887. test->fill_picture_fn = rgbtest_fill_picture;
  888. return init(ctx);
  889. }
  890. static int rgbtest_query_formats(AVFilterContext *ctx)
  891. {
  892. static const enum AVPixelFormat pix_fmts[] = {
  893. AV_PIX_FMT_RGBA, AV_PIX_FMT_ARGB, AV_PIX_FMT_BGRA, AV_PIX_FMT_ABGR,
  894. AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
  895. AV_PIX_FMT_RGB444, AV_PIX_FMT_BGR444,
  896. AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565,
  897. AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555,
  898. AV_PIX_FMT_NONE
  899. };
  900. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  901. if (!fmts_list)
  902. return AVERROR(ENOMEM);
  903. return ff_set_common_formats(ctx, fmts_list);
  904. }
  905. static int rgbtest_config_props(AVFilterLink *outlink)
  906. {
  907. TestSourceContext *test = outlink->src->priv;
  908. ff_fill_rgba_map(test->rgba_map, outlink->format);
  909. return config_props(outlink);
  910. }
  911. static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
  912. {
  913. .name = "default",
  914. .type = AVMEDIA_TYPE_VIDEO,
  915. .request_frame = request_frame,
  916. .config_props = rgbtest_config_props,
  917. },
  918. { NULL }
  919. };
  920. AVFilter ff_vsrc_rgbtestsrc = {
  921. .name = "rgbtestsrc",
  922. .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
  923. .priv_size = sizeof(TestSourceContext),
  924. .priv_class = &rgbtestsrc_class,
  925. .init = rgbtest_init,
  926. .uninit = uninit,
  927. .query_formats = rgbtest_query_formats,
  928. .inputs = NULL,
  929. .outputs = avfilter_vsrc_rgbtestsrc_outputs,
  930. };
  931. #endif /* CONFIG_RGBTESTSRC_FILTER */
  932. #if CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER
  933. static const uint8_t rainbow[7][4] = {
  934. { 180, 128, 128, 255 }, /* 75% white */
  935. { 161, 44, 141, 255 }, /* 75% yellow */
  936. { 131, 156, 44, 255 }, /* 75% cyan */
  937. { 112, 72, 57, 255 }, /* 75% green */
  938. { 83, 183, 198, 255 }, /* 75% magenta */
  939. { 65, 99, 212, 255 }, /* 75% red */
  940. { 34, 212, 114, 255 }, /* 75% blue */
  941. };
  942. static const uint8_t rainbowhd[7][4] = {
  943. { 180, 128, 128, 255 }, /* 75% white */
  944. { 168, 44, 136, 255 }, /* 75% yellow */
  945. { 145, 147, 44, 255 }, /* 75% cyan */
  946. { 133, 63, 52, 255 }, /* 75% green */
  947. { 63, 193, 204, 255 }, /* 75% magenta */
  948. { 51, 109, 212, 255 }, /* 75% red */
  949. { 28, 212, 120, 255 }, /* 75% blue */
  950. };
  951. static const uint8_t wobnair[7][4] = {
  952. { 34, 212, 114, 255 }, /* 75% blue */
  953. { 19, 128, 128, 255 }, /* 7.5% intensity black */
  954. { 83, 183, 198, 255 }, /* 75% magenta */
  955. { 19, 128, 128, 255 }, /* 7.5% intensity black */
  956. { 131, 156, 44, 255 }, /* 75% cyan */
  957. { 19, 128, 128, 255 }, /* 7.5% intensity black */
  958. { 180, 128, 128, 255 }, /* 75% white */
  959. };
  960. static const uint8_t white[4] = { 235, 128, 128, 255 };
  961. /* pluge pulses */
  962. static const uint8_t neg4ire[4] = { 7, 128, 128, 255 };
  963. static const uint8_t pos4ire[4] = { 24, 128, 128, 255 };
  964. /* fudged Q/-I */
  965. static const uint8_t i_pixel[4] = { 57, 156, 97, 255 };
  966. static const uint8_t q_pixel[4] = { 44, 171, 147, 255 };
  967. static const uint8_t gray40[4] = { 104, 128, 128, 255 };
  968. static const uint8_t gray15[4] = { 49, 128, 128, 255 };
  969. static const uint8_t cyan[4] = { 188, 154, 16, 255 };
  970. static const uint8_t yellow[4] = { 219, 16, 138, 255 };
  971. static const uint8_t blue[4] = { 32, 240, 118, 255 };
  972. static const uint8_t red[4] = { 63, 102, 240, 255 };
  973. static const uint8_t black0[4] = { 16, 128, 128, 255 };
  974. static const uint8_t black2[4] = { 20, 128, 128, 255 };
  975. static const uint8_t black4[4] = { 25, 128, 128, 255 };
  976. static const uint8_t neg2[4] = { 12, 128, 128, 255 };
  977. static void draw_bar(TestSourceContext *test, const uint8_t color[4],
  978. int x, int y, int w, int h,
  979. AVFrame *frame)
  980. {
  981. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(frame->format);
  982. uint8_t *p, *p0;
  983. int plane;
  984. x = FFMIN(x, test->w - 1);
  985. y = FFMIN(y, test->h - 1);
  986. w = FFMIN(w, test->w - x);
  987. h = FFMIN(h, test->h - y);
  988. av_assert0(x + w <= test->w);
  989. av_assert0(y + h <= test->h);
  990. for (plane = 0; frame->data[plane]; plane++) {
  991. const int c = color[plane];
  992. const int linesize = frame->linesize[plane];
  993. int i, px, py, pw, ph;
  994. if (plane == 1 || plane == 2) {
  995. px = x >> desc->log2_chroma_w;
  996. pw = AV_CEIL_RSHIFT(w, desc->log2_chroma_w);
  997. py = y >> desc->log2_chroma_h;
  998. ph = AV_CEIL_RSHIFT(h, desc->log2_chroma_h);
  999. } else {
  1000. px = x;
  1001. pw = w;
  1002. py = y;
  1003. ph = h;
  1004. }
  1005. p0 = p = frame->data[plane] + py * linesize + px;
  1006. memset(p, c, pw);
  1007. p += linesize;
  1008. for (i = 1; i < ph; i++, p += linesize)
  1009. memcpy(p, p0, pw);
  1010. }
  1011. }
  1012. static int smptebars_query_formats(AVFilterContext *ctx)
  1013. {
  1014. static const enum AVPixelFormat pix_fmts[] = {
  1015. AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P,
  1016. AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUV444P,
  1017. AV_PIX_FMT_YUV410P, AV_PIX_FMT_YUV411P,
  1018. AV_PIX_FMT_NONE,
  1019. };
  1020. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  1021. if (!fmts_list)
  1022. return AVERROR(ENOMEM);
  1023. return ff_set_common_formats(ctx, fmts_list);
  1024. }
  1025. static const AVFilterPad smptebars_outputs[] = {
  1026. {
  1027. .name = "default",
  1028. .type = AVMEDIA_TYPE_VIDEO,
  1029. .request_frame = request_frame,
  1030. .config_props = config_props,
  1031. },
  1032. { NULL }
  1033. };
  1034. #if CONFIG_SMPTEBARS_FILTER
  1035. #define smptebars_options options
  1036. AVFILTER_DEFINE_CLASS(smptebars);
  1037. static void smptebars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
  1038. {
  1039. TestSourceContext *test = ctx->priv;
  1040. int r_w, r_h, w_h, p_w, p_h, i, tmp, x = 0;
  1041. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
  1042. av_frame_set_colorspace(picref, AVCOL_SPC_BT470BG);
  1043. r_w = FFALIGN((test->w + 6) / 7, 1 << pixdesc->log2_chroma_w);
  1044. r_h = FFALIGN(test->h * 2 / 3, 1 << pixdesc->log2_chroma_h);
  1045. w_h = FFALIGN(test->h * 3 / 4 - r_h, 1 << pixdesc->log2_chroma_h);
  1046. p_w = FFALIGN(r_w * 5 / 4, 1 << pixdesc->log2_chroma_w);
  1047. p_h = test->h - w_h - r_h;
  1048. for (i = 0; i < 7; i++) {
  1049. draw_bar(test, rainbow[i], x, 0, r_w, r_h, picref);
  1050. draw_bar(test, wobnair[i], x, r_h, r_w, w_h, picref);
  1051. x += r_w;
  1052. }
  1053. x = 0;
  1054. draw_bar(test, i_pixel, x, r_h + w_h, p_w, p_h, picref);
  1055. x += p_w;
  1056. draw_bar(test, white, x, r_h + w_h, p_w, p_h, picref);
  1057. x += p_w;
  1058. draw_bar(test, q_pixel, x, r_h + w_h, p_w, p_h, picref);
  1059. x += p_w;
  1060. tmp = FFALIGN(5 * r_w - x, 1 << pixdesc->log2_chroma_w);
  1061. draw_bar(test, black0, x, r_h + w_h, tmp, p_h, picref);
  1062. x += tmp;
  1063. tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
  1064. draw_bar(test, neg4ire, x, r_h + w_h, tmp, p_h, picref);
  1065. x += tmp;
  1066. draw_bar(test, black0, x, r_h + w_h, tmp, p_h, picref);
  1067. x += tmp;
  1068. draw_bar(test, pos4ire, x, r_h + w_h, tmp, p_h, picref);
  1069. x += tmp;
  1070. draw_bar(test, black0, x, r_h + w_h, test->w - x, p_h, picref);
  1071. }
  1072. static av_cold int smptebars_init(AVFilterContext *ctx)
  1073. {
  1074. TestSourceContext *test = ctx->priv;
  1075. test->fill_picture_fn = smptebars_fill_picture;
  1076. test->draw_once = 1;
  1077. return init(ctx);
  1078. }
  1079. AVFilter ff_vsrc_smptebars = {
  1080. .name = "smptebars",
  1081. .description = NULL_IF_CONFIG_SMALL("Generate SMPTE color bars."),
  1082. .priv_size = sizeof(TestSourceContext),
  1083. .priv_class = &smptebars_class,
  1084. .init = smptebars_init,
  1085. .uninit = uninit,
  1086. .query_formats = smptebars_query_formats,
  1087. .inputs = NULL,
  1088. .outputs = smptebars_outputs,
  1089. };
  1090. #endif /* CONFIG_SMPTEBARS_FILTER */
  1091. #if CONFIG_SMPTEHDBARS_FILTER
  1092. #define smptehdbars_options options
  1093. AVFILTER_DEFINE_CLASS(smptehdbars);
  1094. static void smptehdbars_fill_picture(AVFilterContext *ctx, AVFrame *picref)
  1095. {
  1096. TestSourceContext *test = ctx->priv;
  1097. int d_w, r_w, r_h, l_w, i, tmp, x = 0, y = 0;
  1098. const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(picref->format);
  1099. av_frame_set_colorspace(picref, AVCOL_SPC_BT709);
  1100. d_w = FFALIGN(test->w / 8, 1 << pixdesc->log2_chroma_w);
  1101. r_h = FFALIGN(test->h * 7 / 12, 1 << pixdesc->log2_chroma_h);
  1102. draw_bar(test, gray40, x, 0, d_w, r_h, picref);
  1103. x += d_w;
  1104. r_w = FFALIGN((((test->w + 3) / 4) * 3) / 7, 1 << pixdesc->log2_chroma_w);
  1105. for (i = 0; i < 7; i++) {
  1106. draw_bar(test, rainbowhd[i], x, 0, r_w, r_h, picref);
  1107. x += r_w;
  1108. }
  1109. draw_bar(test, gray40, x, 0, test->w - x, r_h, picref);
  1110. y = r_h;
  1111. r_h = FFALIGN(test->h / 12, 1 << pixdesc->log2_chroma_h);
  1112. draw_bar(test, cyan, 0, y, d_w, r_h, picref);
  1113. x = d_w;
  1114. draw_bar(test, i_pixel, x, y, r_w, r_h, picref);
  1115. x += r_w;
  1116. tmp = r_w * 6;
  1117. draw_bar(test, rainbowhd[0], x, y, tmp, r_h, picref);
  1118. x += tmp;
  1119. l_w = x;
  1120. draw_bar(test, blue, x, y, test->w - x, r_h, picref);
  1121. y += r_h;
  1122. draw_bar(test, yellow, 0, y, d_w, r_h, picref);
  1123. x = d_w;
  1124. draw_bar(test, q_pixel, x, y, r_w, r_h, picref);
  1125. x += r_w;
  1126. for (i = 0; i < tmp; i += 1 << pixdesc->log2_chroma_w) {
  1127. uint8_t yramp[4] = {0};
  1128. yramp[0] = i * 255 / tmp;
  1129. yramp[1] = 128;
  1130. yramp[2] = 128;
  1131. yramp[3] = 255;
  1132. draw_bar(test, yramp, x, y, 1 << pixdesc->log2_chroma_w, r_h, picref);
  1133. x += 1 << pixdesc->log2_chroma_w;
  1134. }
  1135. draw_bar(test, red, x, y, test->w - x, r_h, picref);
  1136. y += r_h;
  1137. draw_bar(test, gray15, 0, y, d_w, test->h - y, picref);
  1138. x = d_w;
  1139. tmp = FFALIGN(r_w * 3 / 2, 1 << pixdesc->log2_chroma_w);
  1140. draw_bar(test, black0, x, y, tmp, test->h - y, picref);
  1141. x += tmp;
  1142. tmp = FFALIGN(r_w * 2, 1 << pixdesc->log2_chroma_w);
  1143. draw_bar(test, white, x, y, tmp, test->h - y, picref);
  1144. x += tmp;
  1145. tmp = FFALIGN(r_w * 5 / 6, 1 << pixdesc->log2_chroma_w);
  1146. draw_bar(test, black0, x, y, tmp, test->h - y, picref);
  1147. x += tmp;
  1148. tmp = FFALIGN(r_w / 3, 1 << pixdesc->log2_chroma_w);
  1149. draw_bar(test, neg2, x, y, tmp, test->h - y, picref);
  1150. x += tmp;
  1151. draw_bar(test, black0, x, y, tmp, test->h - y, picref);
  1152. x += tmp;
  1153. draw_bar(test, black2, x, y, tmp, test->h - y, picref);
  1154. x += tmp;
  1155. draw_bar(test, black0, x, y, tmp, test->h - y, picref);
  1156. x += tmp;
  1157. draw_bar(test, black4, x, y, tmp, test->h - y, picref);
  1158. x += tmp;
  1159. r_w = l_w - x;
  1160. draw_bar(test, black0, x, y, r_w, test->h - y, picref);
  1161. x += r_w;
  1162. draw_bar(test, gray15, x, y, test->w - x, test->h - y, picref);
  1163. }
  1164. static av_cold int smptehdbars_init(AVFilterContext *ctx)
  1165. {
  1166. TestSourceContext *test = ctx->priv;
  1167. test->fill_picture_fn = smptehdbars_fill_picture;
  1168. test->draw_once = 1;
  1169. return init(ctx);
  1170. }
  1171. AVFilter ff_vsrc_smptehdbars = {
  1172. .name = "smptehdbars",
  1173. .description = NULL_IF_CONFIG_SMALL("Generate SMPTE HD color bars."),
  1174. .priv_size = sizeof(TestSourceContext),
  1175. .priv_class = &smptehdbars_class,
  1176. .init = smptehdbars_init,
  1177. .uninit = uninit,
  1178. .query_formats = smptebars_query_formats,
  1179. .inputs = NULL,
  1180. .outputs = smptebars_outputs,
  1181. };
  1182. #endif /* CONFIG_SMPTEHDBARS_FILTER */
  1183. #endif /* CONFIG_SMPTEBARS_FILTER || CONFIG_SMPTEHDBARS_FILTER */
  1184. #if CONFIG_ALLYUV_FILTER
  1185. static const AVOption allyuv_options[] = {
  1186. COMMON_OPTIONS_NOSIZE
  1187. { NULL }
  1188. };
  1189. AVFILTER_DEFINE_CLASS(allyuv);
  1190. static void allyuv_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  1191. {
  1192. const int ys = frame->linesize[0];
  1193. const int us = frame->linesize[1];
  1194. const int vs = frame->linesize[2];
  1195. int x, y, j;
  1196. for (y = 0; y < 4096; y++) {
  1197. for (x = 0; x < 2048; x++) {
  1198. frame->data[0][y * ys + x] = ((x / 8) % 256);
  1199. frame->data[0][y * ys + 4095 - x] = ((x / 8) % 256);
  1200. }
  1201. for (x = 0; x < 2048; x+=8) {
  1202. for (j = 0; j < 8; j++) {
  1203. frame->data[1][vs * y + x + j] = (y%16 + (j % 8) * 16);
  1204. frame->data[1][vs * y + 4095 - x - j] = (128 + y%16 + (j % 8) * 16);
  1205. }
  1206. }
  1207. for (x = 0; x < 4096; x++)
  1208. frame->data[2][y * us + x] = 256 * y / 4096;
  1209. }
  1210. }
  1211. static av_cold int allyuv_init(AVFilterContext *ctx)
  1212. {
  1213. TestSourceContext *test = ctx->priv;
  1214. test->w = test->h = 4096;
  1215. test->draw_once = 1;
  1216. test->fill_picture_fn = allyuv_fill_picture;
  1217. return init(ctx);
  1218. }
  1219. static int allyuv_query_formats(AVFilterContext *ctx)
  1220. {
  1221. static const enum AVPixelFormat pix_fmts[] = {
  1222. AV_PIX_FMT_YUV444P, AV_PIX_FMT_GBRP,
  1223. AV_PIX_FMT_NONE
  1224. };
  1225. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  1226. if (!fmts_list)
  1227. return AVERROR(ENOMEM);
  1228. return ff_set_common_formats(ctx, fmts_list);
  1229. }
  1230. static const AVFilterPad avfilter_vsrc_allyuv_outputs[] = {
  1231. {
  1232. .name = "default",
  1233. .type = AVMEDIA_TYPE_VIDEO,
  1234. .request_frame = request_frame,
  1235. .config_props = config_props,
  1236. },
  1237. { NULL }
  1238. };
  1239. AVFilter ff_vsrc_allyuv = {
  1240. .name = "allyuv",
  1241. .description = NULL_IF_CONFIG_SMALL("Generate all yuv colors."),
  1242. .priv_size = sizeof(TestSourceContext),
  1243. .priv_class = &allyuv_class,
  1244. .init = allyuv_init,
  1245. .uninit = uninit,
  1246. .query_formats = allyuv_query_formats,
  1247. .inputs = NULL,
  1248. .outputs = avfilter_vsrc_allyuv_outputs,
  1249. };
  1250. #endif /* CONFIG_ALLYUV_FILTER */
  1251. #if CONFIG_ALLRGB_FILTER
  1252. static const AVOption allrgb_options[] = {
  1253. COMMON_OPTIONS_NOSIZE
  1254. { NULL }
  1255. };
  1256. AVFILTER_DEFINE_CLASS(allrgb);
  1257. static void allrgb_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  1258. {
  1259. unsigned x, y;
  1260. const int linesize = frame->linesize[0];
  1261. uint8_t *line = frame->data[0];
  1262. for (y = 0; y < 4096; y++) {
  1263. uint8_t *dst = line;
  1264. for (x = 0; x < 4096; x++) {
  1265. *dst++ = x;
  1266. *dst++ = y;
  1267. *dst++ = (x >> 8) | ((y >> 8) << 4);
  1268. }
  1269. line += linesize;
  1270. }
  1271. }
  1272. static av_cold int allrgb_init(AVFilterContext *ctx)
  1273. {
  1274. TestSourceContext *test = ctx->priv;
  1275. test->w = test->h = 4096;
  1276. test->draw_once = 1;
  1277. test->fill_picture_fn = allrgb_fill_picture;
  1278. return init(ctx);
  1279. }
  1280. static int allrgb_config_props(AVFilterLink *outlink)
  1281. {
  1282. TestSourceContext *test = outlink->src->priv;
  1283. ff_fill_rgba_map(test->rgba_map, outlink->format);
  1284. return config_props(outlink);
  1285. }
  1286. static int allrgb_query_formats(AVFilterContext *ctx)
  1287. {
  1288. static const enum AVPixelFormat pix_fmts[] = {
  1289. AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
  1290. };
  1291. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  1292. if (!fmts_list)
  1293. return AVERROR(ENOMEM);
  1294. return ff_set_common_formats(ctx, fmts_list);
  1295. }
  1296. static const AVFilterPad avfilter_vsrc_allrgb_outputs[] = {
  1297. {
  1298. .name = "default",
  1299. .type = AVMEDIA_TYPE_VIDEO,
  1300. .request_frame = request_frame,
  1301. .config_props = allrgb_config_props,
  1302. },
  1303. { NULL }
  1304. };
  1305. AVFilter ff_vsrc_allrgb = {
  1306. .name = "allrgb",
  1307. .description = NULL_IF_CONFIG_SMALL("Generate all RGB colors."),
  1308. .priv_size = sizeof(TestSourceContext),
  1309. .priv_class = &allrgb_class,
  1310. .init = allrgb_init,
  1311. .uninit = uninit,
  1312. .query_formats = allrgb_query_formats,
  1313. .inputs = NULL,
  1314. .outputs = avfilter_vsrc_allrgb_outputs,
  1315. };
  1316. #endif /* CONFIG_ALLRGB_FILTER */