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.

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