You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

500 lines
17KB

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