vsrc_testsrc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Misc test sources.
  24. *
  25. * testsrc is based on the test pattern generator demuxer by Nicolas George:
  26. * http://lists.ffmpeg.org/pipermail/ffmpeg-devel/2007-October/037845.html
  27. *
  28. * rgbtestsrc is ported from MPlayer libmpcodecs/vf_rgbtest.c by
  29. * Michael Niedermayer.
  30. */
  31. #include <float.h>
  32. #include "libavutil/common.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/opt.h"
  35. #include "libavutil/intreadwrite.h"
  36. #include "libavutil/parseutils.h"
  37. #include "avfilter.h"
  38. #include "formats.h"
  39. #include "internal.h"
  40. #include "video.h"
  41. typedef struct TestSourceContext {
  42. const AVClass *class;
  43. int h, w;
  44. unsigned int nb_frame;
  45. AVRational time_base, frame_rate;
  46. int64_t pts, max_pts;
  47. char *size; ///< video frame size
  48. char *rate; ///< video frame rate
  49. char *duration; ///< total duration of the generated video
  50. AVRational sar; ///< sample aspect ratio
  51. void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
  52. /* only used by rgbtest */
  53. int rgba_map[4];
  54. } TestSourceContext;
  55. #define OFFSET(x) offsetof(TestSourceContext, x)
  56. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  57. static const AVOption testsrc_options[] = {
  58. { "size", "set video size", OFFSET(size), AV_OPT_TYPE_STRING, {.str = "320x240"}, .flags = FLAGS },
  59. { "s", "set video size", OFFSET(size), AV_OPT_TYPE_STRING, {.str = "320x240"}, .flags = FLAGS },
  60. { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, .flags = FLAGS },
  61. { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, .flags = FLAGS },
  62. { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
  63. { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl = 1}, 0, INT_MAX, FLAGS },
  64. { NULL },
  65. };
  66. static av_cold int init_common(AVFilterContext *ctx)
  67. {
  68. TestSourceContext *test = ctx->priv;
  69. int64_t duration = -1;
  70. int ret = 0;
  71. if ((ret = av_parse_video_size(&test->w, &test->h, test->size)) < 0) {
  72. av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", test->size);
  73. return ret;
  74. }
  75. if ((ret = av_parse_video_rate(&test->frame_rate, test->rate)) < 0) {
  76. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
  77. return ret;
  78. }
  79. if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
  80. av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
  81. return ret;
  82. }
  83. test->time_base = av_inv_q(test->frame_rate);
  84. test->max_pts = duration >= 0 ?
  85. av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
  86. test->nb_frame = 0;
  87. test->pts = 0;
  88. av_log(ctx, AV_LOG_DEBUG, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
  89. test->w, test->h, test->frame_rate.num, test->frame_rate.den,
  90. duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
  91. test->sar.num, test->sar.den);
  92. return 0;
  93. }
  94. static int config_props(AVFilterLink *outlink)
  95. {
  96. TestSourceContext *test = outlink->src->priv;
  97. outlink->w = test->w;
  98. outlink->h = test->h;
  99. outlink->sample_aspect_ratio = test->sar;
  100. outlink->frame_rate = test->frame_rate;
  101. outlink->time_base = test->time_base;
  102. return 0;
  103. }
  104. static int request_frame(AVFilterLink *outlink)
  105. {
  106. TestSourceContext *test = outlink->src->priv;
  107. AVFrame *frame;
  108. if (test->max_pts >= 0 && test->pts > test->max_pts)
  109. return AVERROR_EOF;
  110. frame = ff_get_video_buffer(outlink, test->w, test->h);
  111. if (!frame)
  112. return AVERROR(ENOMEM);
  113. frame->pts = test->pts++;
  114. frame->key_frame = 1;
  115. frame->interlaced_frame = 0;
  116. frame->pict_type = AV_PICTURE_TYPE_I;
  117. frame->sample_aspect_ratio = test->sar;
  118. test->nb_frame++;
  119. test->fill_picture_fn(outlink->src, frame);
  120. return ff_filter_frame(outlink, frame);
  121. }
  122. #if CONFIG_TESTSRC_FILTER
  123. static const char *testsrc_get_name(void *ctx)
  124. {
  125. return "testsrc";
  126. }
  127. static const AVClass testsrc_class = {
  128. .class_name = "TestSourceContext",
  129. .item_name = testsrc_get_name,
  130. .option = testsrc_options,
  131. };
  132. /**
  133. * Fill a rectangle with value val.
  134. *
  135. * @param val the RGB value to set
  136. * @param dst pointer to the destination buffer to fill
  137. * @param dst_linesize linesize of destination
  138. * @param segment_width width of the segment
  139. * @param x horizontal coordinate where to draw the rectangle in the destination buffer
  140. * @param y horizontal coordinate where to draw the rectangle in the destination buffer
  141. * @param w width of the rectangle to draw, expressed as a number of segment_width units
  142. * @param h height of the rectangle to draw, expressed as a number of segment_width units
  143. */
  144. static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
  145. unsigned x, unsigned y, unsigned w, unsigned h)
  146. {
  147. int i;
  148. int step = 3;
  149. dst += segment_width * (step * x + y * dst_linesize);
  150. w *= segment_width * step;
  151. h *= segment_width;
  152. for (i = 0; i < h; i++) {
  153. memset(dst, val, w);
  154. dst += dst_linesize;
  155. }
  156. }
  157. static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
  158. unsigned segment_width)
  159. {
  160. #define TOP_HBAR 1
  161. #define MID_HBAR 2
  162. #define BOT_HBAR 4
  163. #define LEFT_TOP_VBAR 8
  164. #define LEFT_BOT_VBAR 16
  165. #define RIGHT_TOP_VBAR 32
  166. #define RIGHT_BOT_VBAR 64
  167. struct segments {
  168. int x, y, w, h;
  169. } segments[] = {
  170. { 1, 0, 5, 1 }, /* TOP_HBAR */
  171. { 1, 6, 5, 1 }, /* MID_HBAR */
  172. { 1, 12, 5, 1 }, /* BOT_HBAR */
  173. { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
  174. { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
  175. { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
  176. { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
  177. };
  178. static const unsigned char masks[10] = {
  179. /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  180. /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  181. /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
  182. /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  183. /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  184. /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
  185. /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
  186. /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  187. /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  188. /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  189. };
  190. unsigned mask = masks[digit];
  191. int i;
  192. draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
  193. for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
  194. if (mask & (1<<i))
  195. draw_rectangle(255, dst, dst_linesize, segment_width,
  196. segments[i].x, segments[i].y, segments[i].w, segments[i].h);
  197. }
  198. #define GRADIENT_SIZE (6 * 256)
  199. static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  200. {
  201. TestSourceContext *test = ctx->priv;
  202. uint8_t *p, *p0;
  203. int x, y;
  204. int color, color_rest;
  205. int icolor;
  206. int radius;
  207. int quad0, quad;
  208. int dquad_x, dquad_y;
  209. int grad, dgrad, rgrad, drgrad;
  210. int seg_size;
  211. int second;
  212. int i;
  213. uint8_t *data = frame->data[0];
  214. int width = frame->width;
  215. int height = frame->height;
  216. /* draw colored bars and circle */
  217. radius = (width + height) / 4;
  218. quad0 = width * width / 4 + height * height / 4 - radius * radius;
  219. dquad_y = 1 - height;
  220. p0 = data;
  221. for (y = 0; y < height; y++) {
  222. p = p0;
  223. color = 0;
  224. color_rest = 0;
  225. quad = quad0;
  226. dquad_x = 1 - width;
  227. for (x = 0; x < width; x++) {
  228. icolor = color;
  229. if (quad < 0)
  230. icolor ^= 7;
  231. quad += dquad_x;
  232. dquad_x += 2;
  233. *(p++) = icolor & 1 ? 255 : 0;
  234. *(p++) = icolor & 2 ? 255 : 0;
  235. *(p++) = icolor & 4 ? 255 : 0;
  236. color_rest += 8;
  237. if (color_rest >= width) {
  238. color_rest -= width;
  239. color++;
  240. }
  241. }
  242. quad0 += dquad_y;
  243. dquad_y += 2;
  244. p0 += frame->linesize[0];
  245. }
  246. /* draw sliding color line */
  247. p = data + frame->linesize[0] * height * 3/4;
  248. grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
  249. GRADIENT_SIZE;
  250. rgrad = 0;
  251. dgrad = GRADIENT_SIZE / width;
  252. drgrad = GRADIENT_SIZE % width;
  253. for (x = 0; x < width; x++) {
  254. *(p++) =
  255. grad < 256 || grad >= 5 * 256 ? 255 :
  256. grad >= 2 * 256 && grad < 4 * 256 ? 0 :
  257. grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
  258. *(p++) =
  259. grad >= 4 * 256 ? 0 :
  260. grad >= 1 * 256 && grad < 3 * 256 ? 255 :
  261. grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
  262. *(p++) =
  263. grad < 2 * 256 ? 0 :
  264. grad >= 3 * 256 && grad < 5 * 256 ? 255 :
  265. grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
  266. grad += dgrad;
  267. rgrad += drgrad;
  268. if (rgrad >= GRADIENT_SIZE) {
  269. grad++;
  270. rgrad -= GRADIENT_SIZE;
  271. }
  272. if (grad >= GRADIENT_SIZE)
  273. grad -= GRADIENT_SIZE;
  274. }
  275. for (y = height / 8; y > 0; y--) {
  276. memcpy(p, p - frame->linesize[0], 3 * width);
  277. p += frame->linesize[0];
  278. }
  279. /* draw digits */
  280. seg_size = width / 80;
  281. if (seg_size >= 1 && height >= 13 * seg_size) {
  282. second = test->nb_frame * test->time_base.num / test->time_base.den;
  283. x = width - (width - seg_size * 64) / 2;
  284. y = (height - seg_size * 13) / 2;
  285. p = data + (x*3 + y * frame->linesize[0]);
  286. for (i = 0; i < 8; i++) {
  287. p -= 3 * 8 * seg_size;
  288. draw_digit(second % 10, p, frame->linesize[0], seg_size);
  289. second /= 10;
  290. if (second == 0)
  291. break;
  292. }
  293. }
  294. }
  295. static av_cold int test_init(AVFilterContext *ctx)
  296. {
  297. TestSourceContext *test = ctx->priv;
  298. test->fill_picture_fn = test_fill_picture;
  299. return init_common(ctx);
  300. }
  301. static int test_query_formats(AVFilterContext *ctx)
  302. {
  303. static const enum AVPixelFormat pix_fmts[] = {
  304. AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
  305. };
  306. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  307. return 0;
  308. }
  309. static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
  310. {
  311. .name = "default",
  312. .type = AVMEDIA_TYPE_VIDEO,
  313. .request_frame = request_frame,
  314. .config_props = config_props,
  315. },
  316. { NULL }
  317. };
  318. AVFilter ff_vsrc_testsrc = {
  319. .name = "testsrc",
  320. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  321. .priv_size = sizeof(TestSourceContext),
  322. .priv_class = &testsrc_class,
  323. .init = test_init,
  324. .query_formats = test_query_formats,
  325. .inputs = NULL,
  326. .outputs = avfilter_vsrc_testsrc_outputs,
  327. };
  328. #endif /* CONFIG_TESTSRC_FILTER */
  329. #if CONFIG_RGBTESTSRC_FILTER
  330. static const char *rgbtestsrc_get_name(void *ctx)
  331. {
  332. return "rgbtestsrc";
  333. }
  334. static const AVClass rgbtestsrc_class = {
  335. .class_name = "RGBTestSourceContext",
  336. .item_name = rgbtestsrc_get_name,
  337. .option = testsrc_options,
  338. };
  339. #define R 0
  340. #define G 1
  341. #define B 2
  342. #define A 3
  343. static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
  344. int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
  345. int rgba_map[4])
  346. {
  347. int32_t v;
  348. uint8_t *p;
  349. switch (fmt) {
  350. case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
  351. case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
  352. case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
  353. case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
  354. case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
  355. case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
  356. case AV_PIX_FMT_RGB24:
  357. case AV_PIX_FMT_BGR24:
  358. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  359. p = dst + 3*x + y*dst_linesize;
  360. AV_WL24(p, v);
  361. break;
  362. case AV_PIX_FMT_RGBA:
  363. case AV_PIX_FMT_BGRA:
  364. case AV_PIX_FMT_ARGB:
  365. case AV_PIX_FMT_ABGR:
  366. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  367. p = dst + 4*x + y*dst_linesize;
  368. AV_WL32(p, v);
  369. break;
  370. }
  371. }
  372. static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  373. {
  374. TestSourceContext *test = ctx->priv;
  375. int x, y, w = frame->width, h = frame->height;
  376. for (y = 0; y < h; y++) {
  377. for (x = 0; x < w; x++) {
  378. int c = 256*x/w;
  379. int r = 0, g = 0, b = 0;
  380. if (3*y < h ) r = c;
  381. else if (3*y < 2*h) g = c;
  382. else b = c;
  383. rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
  384. ctx->outputs[0]->format, test->rgba_map);
  385. }
  386. }
  387. }
  388. static av_cold int rgbtest_init(AVFilterContext *ctx)
  389. {
  390. TestSourceContext *test = ctx->priv;
  391. test->fill_picture_fn = rgbtest_fill_picture;
  392. return init_common(ctx);
  393. }
  394. static int rgbtest_query_formats(AVFilterContext *ctx)
  395. {
  396. static const enum AVPixelFormat pix_fmts[] = {
  397. AV_PIX_FMT_RGBA, AV_PIX_FMT_ARGB, AV_PIX_FMT_BGRA, AV_PIX_FMT_ABGR,
  398. AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
  399. AV_PIX_FMT_RGB444, AV_PIX_FMT_BGR444,
  400. AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565,
  401. AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555,
  402. AV_PIX_FMT_NONE
  403. };
  404. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  405. return 0;
  406. }
  407. static int rgbtest_config_props(AVFilterLink *outlink)
  408. {
  409. TestSourceContext *test = outlink->src->priv;
  410. switch (outlink->format) {
  411. case AV_PIX_FMT_ARGB: test->rgba_map[A] = 0; test->rgba_map[R] = 1; test->rgba_map[G] = 2; test->rgba_map[B] = 3; break;
  412. case AV_PIX_FMT_ABGR: test->rgba_map[A] = 0; test->rgba_map[B] = 1; test->rgba_map[G] = 2; test->rgba_map[R] = 3; break;
  413. case AV_PIX_FMT_RGBA:
  414. case AV_PIX_FMT_RGB24: test->rgba_map[R] = 0; test->rgba_map[G] = 1; test->rgba_map[B] = 2; test->rgba_map[A] = 3; break;
  415. case AV_PIX_FMT_BGRA:
  416. case AV_PIX_FMT_BGR24: test->rgba_map[B] = 0; test->rgba_map[G] = 1; test->rgba_map[R] = 2; test->rgba_map[A] = 3; break;
  417. }
  418. return config_props(outlink);
  419. }
  420. static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
  421. {
  422. .name = "default",
  423. .type = AVMEDIA_TYPE_VIDEO,
  424. .request_frame = request_frame,
  425. .config_props = rgbtest_config_props,
  426. },
  427. { NULL }
  428. };
  429. AVFilter ff_vsrc_rgbtestsrc = {
  430. .name = "rgbtestsrc",
  431. .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
  432. .priv_size = sizeof(TestSourceContext),
  433. .priv_class = &rgbtestsrc_class,
  434. .init = rgbtest_init,
  435. .query_formats = rgbtest_query_formats,
  436. .inputs = NULL,
  437. .outputs = avfilter_vsrc_rgbtestsrc_outputs,
  438. };
  439. #endif /* CONFIG_RGBTESTSRC_FILTER */