vsrc_testsrc.c 48 KB

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