vsrc_testsrc.c 56 KB

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