vsrc_testsrc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Copyright (c) 2007 Nicolas George <nicolas.george@normalesup.org>
  3. * Copyright (c) 2011 Stefano Sabatini
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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.mplayerhq.hu/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/opt.h"
  33. #include "libavutil/intreadwrite.h"
  34. #include "libavutil/parseutils.h"
  35. #include "avfilter.h"
  36. typedef struct {
  37. const AVClass *class;
  38. int h, w;
  39. unsigned int nb_frame;
  40. AVRational time_base;
  41. int64_t pts, max_pts;
  42. char *size; ///< video frame size
  43. char *rate; ///< video frame rate
  44. char *duration; ///< total duration of the generated video
  45. void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
  46. /* only used by rgbtest */
  47. int rgba_map[4];
  48. } TestSourceContext;
  49. #define OFFSET(x) offsetof(TestSourceContext, x)
  50. static const AVOption testsrc_options[]= {
  51. { "size", "set video size", OFFSET(size), FF_OPT_TYPE_STRING, {.str = "320x240"}, 0, 0 },
  52. { "s", "set video size", OFFSET(size), FF_OPT_TYPE_STRING, {.str = "320x240"}, 0, 0 },
  53. { "rate", "set video rate", OFFSET(rate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
  54. { "r", "set video rate", OFFSET(rate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
  55. { "duration", "set video duration", OFFSET(duration), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  56. { NULL },
  57. };
  58. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  59. {
  60. TestSourceContext *test = ctx->priv;
  61. AVRational frame_rate_q;
  62. int64_t duration = -1;
  63. int ret = 0;
  64. av_opt_set_defaults2(test, 0, 0);
  65. if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
  66. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  67. return ret;
  68. }
  69. if ((ret = av_parse_video_size(&test->w, &test->h, test->size)) < 0) {
  70. av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", test->size);
  71. return ret;
  72. }
  73. if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0 ||
  74. frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
  75. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
  76. return ret;
  77. }
  78. if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
  79. av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
  80. return ret;
  81. }
  82. test->time_base.num = frame_rate_q.den;
  83. test->time_base.den = frame_rate_q.num;
  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_INFO, "size:%dx%d rate:%d/%d duration:%f\n",
  89. test->w, test->h, frame_rate_q.num, frame_rate_q.den,
  90. duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base));
  91. return 0;
  92. }
  93. static int config_props(AVFilterLink *outlink)
  94. {
  95. TestSourceContext *test = outlink->src->priv;
  96. outlink->w = test->w;
  97. outlink->h = test->h;
  98. outlink->time_base = test->time_base;
  99. return 0;
  100. }
  101. static int request_frame(AVFilterLink *outlink)
  102. {
  103. TestSourceContext *test = outlink->src->priv;
  104. AVFilterBufferRef *picref;
  105. if (test->max_pts >= 0 && test->pts > test->max_pts)
  106. return AVERROR_EOF;
  107. picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
  108. test->w, test->h);
  109. picref->pts = test->pts++;
  110. test->nb_frame++;
  111. test->fill_picture_fn(outlink->src, picref);
  112. avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  113. avfilter_draw_slice(outlink, 0, picref->video->h, 1);
  114. avfilter_end_frame(outlink);
  115. avfilter_unref_buffer(picref);
  116. return 0;
  117. }
  118. #if CONFIG_TESTSRC_FILTER
  119. static const char *testsrc_get_name(void *ctx)
  120. {
  121. return "testsrc";
  122. }
  123. static const AVClass testsrc_class = {
  124. "TestSourceContext",
  125. testsrc_get_name,
  126. testsrc_options
  127. };
  128. /**
  129. * Fill a rectangle with value val.
  130. *
  131. * @param val the RGB value to set
  132. * @param dst pointer to the destination buffer to fill
  133. * @param dst_linesize linesize of destination
  134. * @param segment_width width of the segment
  135. * @param x horizontal coordinate where to draw the rectangle in the destination buffer
  136. * @param y horizontal coordinate where to draw the rectangle in the destination buffer
  137. * @param w width of the rectangle to draw, expressed as a number of segment_width units
  138. * @param h height of the rectangle to draw, expressed as a number of segment_width units
  139. */
  140. static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
  141. unsigned x, unsigned y, unsigned w, unsigned h)
  142. {
  143. int i;
  144. int step = 3;
  145. dst += segment_width * (step * x + y * dst_linesize);
  146. w *= segment_width * step;
  147. h *= segment_width;
  148. for (i = 0; i < h; i++) {
  149. memset(dst, val, w);
  150. dst += dst_linesize;
  151. }
  152. }
  153. static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
  154. unsigned segment_width)
  155. {
  156. #define TOP_HBAR 1
  157. #define MID_HBAR 2
  158. #define BOT_HBAR 4
  159. #define LEFT_TOP_VBAR 8
  160. #define LEFT_BOT_VBAR 16
  161. #define RIGHT_TOP_VBAR 32
  162. #define RIGHT_BOT_VBAR 64
  163. struct {
  164. int x, y, w, h;
  165. } segments[] = {
  166. { 1, 0, 5, 1 }, /* TOP_HBAR */
  167. { 1, 6, 5, 1 }, /* MID_HBAR */
  168. { 1, 12, 5, 1 }, /* BOT_HBAR */
  169. { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
  170. { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
  171. { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
  172. { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
  173. };
  174. static const unsigned char masks[10] = {
  175. /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  176. /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  177. /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
  178. /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  179. /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  180. /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
  181. /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
  182. /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  183. /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  184. /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  185. };
  186. unsigned mask = masks[digit];
  187. int i;
  188. draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
  189. for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
  190. if (mask & (1<<i))
  191. draw_rectangle(255, dst, dst_linesize, segment_width,
  192. segments[i].x, segments[i].y, segments[i].w, segments[i].h);
  193. }
  194. #define GRADIENT_SIZE (6 * 256)
  195. static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  196. {
  197. TestSourceContext *test = ctx->priv;
  198. uint8_t *p, *p0;
  199. int x, y;
  200. int color, color_rest;
  201. int icolor;
  202. int radius;
  203. int quad0, quad;
  204. int dquad_x, dquad_y;
  205. int grad, dgrad, rgrad, drgrad;
  206. int seg_size;
  207. int second;
  208. int i;
  209. uint8_t *data = picref->data[0];
  210. int width = picref->video->w;
  211. int height = picref->video->h;
  212. /* draw colored bars and circle */
  213. radius = (width + height) / 4;
  214. quad0 = width * width / 4 + height * height / 4 - radius * radius;
  215. dquad_y = 1 - height;
  216. p0 = data;
  217. for (y = 0; y < height; y++) {
  218. p = p0;
  219. color = 0;
  220. color_rest = 0;
  221. quad = quad0;
  222. dquad_x = 1 - width;
  223. for (x = 0; x < width; x++) {
  224. icolor = color;
  225. if (quad < 0)
  226. icolor ^= 7;
  227. quad += dquad_x;
  228. dquad_x += 2;
  229. *(p++) = icolor & 1 ? 255 : 0;
  230. *(p++) = icolor & 2 ? 255 : 0;
  231. *(p++) = icolor & 4 ? 255 : 0;
  232. color_rest += 8;
  233. if (color_rest >= width) {
  234. color_rest -= width;
  235. color++;
  236. }
  237. }
  238. quad0 += dquad_y;
  239. dquad_y += 2;
  240. p0 += picref->linesize[0];
  241. }
  242. /* draw sliding color line */
  243. p = data + picref->linesize[0] * height * 3/4;
  244. grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
  245. GRADIENT_SIZE;
  246. rgrad = 0;
  247. dgrad = GRADIENT_SIZE / width;
  248. drgrad = GRADIENT_SIZE % width;
  249. for (x = 0; x < width; x++) {
  250. *(p++) =
  251. grad < 256 || grad >= 5 * 256 ? 255 :
  252. grad >= 2 * 256 && grad < 4 * 256 ? 0 :
  253. grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
  254. *(p++) =
  255. grad >= 4 * 256 ? 0 :
  256. grad >= 1 * 256 && grad < 3 * 256 ? 255 :
  257. grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
  258. *(p++) =
  259. grad < 2 * 256 ? 0 :
  260. grad >= 3 * 256 && grad < 5 * 256 ? 255 :
  261. grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
  262. grad += dgrad;
  263. rgrad += drgrad;
  264. if (rgrad >= GRADIENT_SIZE) {
  265. grad++;
  266. rgrad -= GRADIENT_SIZE;
  267. }
  268. if (grad >= GRADIENT_SIZE)
  269. grad -= GRADIENT_SIZE;
  270. }
  271. for (y = height / 8; y > 0; y--) {
  272. memcpy(p, p - picref->linesize[0], 3 * width);
  273. p += picref->linesize[0];
  274. }
  275. /* draw digits */
  276. seg_size = width / 80;
  277. if (seg_size >= 1 && height >= 13 * seg_size) {
  278. second = test->nb_frame * test->time_base.num / test->time_base.den;
  279. x = width - (width - seg_size * 64) / 2;
  280. y = (height - seg_size * 13) / 2;
  281. p = data + (x*3 + y * picref->linesize[0]);
  282. for (i = 0; i < 8; i++) {
  283. p -= 3 * 8 * seg_size;
  284. draw_digit(second % 10, p, picref->linesize[0], seg_size);
  285. second /= 10;
  286. if (second == 0)
  287. break;
  288. }
  289. }
  290. }
  291. static av_cold int test_init(AVFilterContext *ctx, const char *args, void *opaque)
  292. {
  293. TestSourceContext *test = ctx->priv;
  294. test->class = &testsrc_class;
  295. test->fill_picture_fn = test_fill_picture;
  296. return init(ctx, args, opaque);
  297. }
  298. static int test_query_formats(AVFilterContext *ctx)
  299. {
  300. static const enum PixelFormat pix_fmts[] = {
  301. PIX_FMT_RGB24, PIX_FMT_NONE
  302. };
  303. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  304. return 0;
  305. }
  306. AVFilter avfilter_vsrc_testsrc = {
  307. .name = "testsrc",
  308. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  309. .priv_size = sizeof(TestSourceContext),
  310. .init = test_init,
  311. .query_formats = test_query_formats,
  312. .inputs = (AVFilterPad[]) {{ .name = NULL}},
  313. .outputs = (AVFilterPad[]) {{ .name = "default",
  314. .type = AVMEDIA_TYPE_VIDEO,
  315. .request_frame = request_frame,
  316. .config_props = config_props, },
  317. { .name = NULL }},
  318. };
  319. #endif /* CONFIG_TESTSRC_FILTER */
  320. #if CONFIG_RGBTESTSRC_FILTER
  321. static const char *rgbtestsrc_get_name(void *ctx)
  322. {
  323. return "rgbtestsrc";
  324. }
  325. static const AVClass rgbtestsrc_class = {
  326. "RGBTestSourceContext",
  327. rgbtestsrc_get_name,
  328. testsrc_options
  329. };
  330. #define R 0
  331. #define G 1
  332. #define B 2
  333. #define A 3
  334. static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
  335. int x, int y, int r, int g, int b, enum PixelFormat fmt,
  336. int rgba_map[4])
  337. {
  338. int32_t v;
  339. uint8_t *p;
  340. switch (fmt) {
  341. case PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
  342. case PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
  343. case PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
  344. case PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
  345. case PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
  346. case PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
  347. case PIX_FMT_RGB24:
  348. case PIX_FMT_BGR24:
  349. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  350. p = dst + 3*x + y*dst_linesize;
  351. AV_WL24(p, v);
  352. break;
  353. case PIX_FMT_RGBA:
  354. case PIX_FMT_BGRA:
  355. case PIX_FMT_ARGB:
  356. case PIX_FMT_ABGR:
  357. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  358. p = dst + 4*x + y*dst_linesize;
  359. AV_WL32(p, v);
  360. break;
  361. }
  362. }
  363. static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  364. {
  365. TestSourceContext *test = ctx->priv;
  366. int x, y, w = picref->video->w, h = picref->video->h;
  367. for (y = 0; y < h; y++) {
  368. for (x = 0; x < picref->video->w; x++) {
  369. int c = 256*x/w;
  370. int r = 0, g = 0, b = 0;
  371. if (3*y < h ) r = c;
  372. else if (3*y < 2*h) g = c;
  373. else b = c;
  374. rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
  375. ctx->outputs[0]->format, test->rgba_map);
  376. }
  377. }
  378. }
  379. static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args, void *opaque)
  380. {
  381. TestSourceContext *test = ctx->priv;
  382. test->class = &rgbtestsrc_class;
  383. test->fill_picture_fn = rgbtest_fill_picture;
  384. return init(ctx, args, opaque);
  385. }
  386. static int rgbtest_query_formats(AVFilterContext *ctx)
  387. {
  388. static const enum PixelFormat pix_fmts[] = {
  389. PIX_FMT_RGBA, PIX_FMT_ARGB, PIX_FMT_BGRA, PIX_FMT_ABGR,
  390. PIX_FMT_BGR24, PIX_FMT_RGB24,
  391. PIX_FMT_RGB444, PIX_FMT_BGR444,
  392. PIX_FMT_RGB565, PIX_FMT_BGR565,
  393. PIX_FMT_RGB555, PIX_FMT_BGR555,
  394. PIX_FMT_NONE
  395. };
  396. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  397. return 0;
  398. }
  399. static int rgbtest_config_props(AVFilterLink *outlink)
  400. {
  401. TestSourceContext *test = outlink->src->priv;
  402. switch (outlink->format) {
  403. case PIX_FMT_ARGB: test->rgba_map[A] = 0; test->rgba_map[R] = 1; test->rgba_map[G] = 2; test->rgba_map[B] = 3; break;
  404. case PIX_FMT_ABGR: test->rgba_map[A] = 0; test->rgba_map[B] = 1; test->rgba_map[G] = 2; test->rgba_map[R] = 3; break;
  405. case PIX_FMT_RGBA:
  406. case PIX_FMT_RGB24: test->rgba_map[R] = 0; test->rgba_map[G] = 1; test->rgba_map[B] = 2; test->rgba_map[A] = 3; break;
  407. case PIX_FMT_BGRA:
  408. case PIX_FMT_BGR24: test->rgba_map[B] = 0; test->rgba_map[G] = 1; test->rgba_map[R] = 2; test->rgba_map[A] = 3; break;
  409. }
  410. return config_props(outlink);
  411. }
  412. AVFilter avfilter_vsrc_rgbtestsrc = {
  413. .name = "rgbtestsrc",
  414. .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
  415. .priv_size = sizeof(TestSourceContext),
  416. .init = rgbtest_init,
  417. .query_formats = rgbtest_query_formats,
  418. .inputs = (AVFilterPad[]) {{ .name = NULL}},
  419. .outputs = (AVFilterPad[]) {{ .name = "default",
  420. .type = AVMEDIA_TYPE_VIDEO,
  421. .request_frame = request_frame,
  422. .config_props = rgbtest_config_props, },
  423. { .name = NULL }},
  424. };
  425. #endif /* CONFIG_RGBTESTSRC_FILTER */