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.

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