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.

503 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. picref->pts = test->pts++;
  118. picref->pos = -1;
  119. picref->video->key_frame = 1;
  120. picref->video->interlaced = 0;
  121. picref->video->pict_type = AV_PICTURE_TYPE_I;
  122. picref->video->pixel_aspect = test->sar;
  123. test->nb_frame++;
  124. test->fill_picture_fn(outlink->src, picref);
  125. if ((ret = ff_start_frame(outlink, picref)) < 0 ||
  126. (ret = ff_draw_slice(outlink, 0, test->h, 1)) < 0 ||
  127. (ret = ff_end_frame(outlink)) < 0)
  128. return ret;
  129. return 0;
  130. }
  131. #if CONFIG_TESTSRC_FILTER
  132. static const char *testsrc_get_name(void *ctx)
  133. {
  134. return "testsrc";
  135. }
  136. static const AVClass testsrc_class = {
  137. .class_name = "TestSourceContext",
  138. .item_name = testsrc_get_name,
  139. .option = testsrc_options,
  140. };
  141. /**
  142. * Fill a rectangle with value val.
  143. *
  144. * @param val the RGB value to set
  145. * @param dst pointer to the destination buffer to fill
  146. * @param dst_linesize linesize of destination
  147. * @param segment_width width of the segment
  148. * @param x horizontal coordinate where to draw the rectangle in the destination buffer
  149. * @param y horizontal coordinate where to draw the rectangle in the destination buffer
  150. * @param w width of the rectangle to draw, expressed as a number of segment_width units
  151. * @param h height of the rectangle to draw, expressed as a number of segment_width units
  152. */
  153. static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
  154. unsigned x, unsigned y, unsigned w, unsigned h)
  155. {
  156. int i;
  157. int step = 3;
  158. dst += segment_width * (step * x + y * dst_linesize);
  159. w *= segment_width * step;
  160. h *= segment_width;
  161. for (i = 0; i < h; i++) {
  162. memset(dst, val, w);
  163. dst += dst_linesize;
  164. }
  165. }
  166. static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
  167. unsigned segment_width)
  168. {
  169. #define TOP_HBAR 1
  170. #define MID_HBAR 2
  171. #define BOT_HBAR 4
  172. #define LEFT_TOP_VBAR 8
  173. #define LEFT_BOT_VBAR 16
  174. #define RIGHT_TOP_VBAR 32
  175. #define RIGHT_BOT_VBAR 64
  176. struct {
  177. int x, y, w, h;
  178. } segments[] = {
  179. { 1, 0, 5, 1 }, /* TOP_HBAR */
  180. { 1, 6, 5, 1 }, /* MID_HBAR */
  181. { 1, 12, 5, 1 }, /* BOT_HBAR */
  182. { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
  183. { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
  184. { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
  185. { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
  186. };
  187. static const unsigned char masks[10] = {
  188. /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  189. /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  190. /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
  191. /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  192. /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  193. /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
  194. /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
  195. /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  196. /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  197. /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  198. };
  199. unsigned mask = masks[digit];
  200. int i;
  201. draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
  202. for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
  203. if (mask & (1<<i))
  204. draw_rectangle(255, dst, dst_linesize, segment_width,
  205. segments[i].x, segments[i].y, segments[i].w, segments[i].h);
  206. }
  207. #define GRADIENT_SIZE (6 * 256)
  208. static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  209. {
  210. TestSourceContext *test = ctx->priv;
  211. uint8_t *p, *p0;
  212. int x, y;
  213. int color, color_rest;
  214. int icolor;
  215. int radius;
  216. int quad0, quad;
  217. int dquad_x, dquad_y;
  218. int grad, dgrad, rgrad, drgrad;
  219. int seg_size;
  220. int second;
  221. int i;
  222. uint8_t *data = picref->data[0];
  223. int width = picref->video->w;
  224. int height = picref->video->h;
  225. /* draw colored bars and circle */
  226. radius = (width + height) / 4;
  227. quad0 = width * width / 4 + height * height / 4 - radius * radius;
  228. dquad_y = 1 - height;
  229. p0 = data;
  230. for (y = 0; y < height; y++) {
  231. p = p0;
  232. color = 0;
  233. color_rest = 0;
  234. quad = quad0;
  235. dquad_x = 1 - width;
  236. for (x = 0; x < width; x++) {
  237. icolor = color;
  238. if (quad < 0)
  239. icolor ^= 7;
  240. quad += dquad_x;
  241. dquad_x += 2;
  242. *(p++) = icolor & 1 ? 255 : 0;
  243. *(p++) = icolor & 2 ? 255 : 0;
  244. *(p++) = icolor & 4 ? 255 : 0;
  245. color_rest += 8;
  246. if (color_rest >= width) {
  247. color_rest -= width;
  248. color++;
  249. }
  250. }
  251. quad0 += dquad_y;
  252. dquad_y += 2;
  253. p0 += picref->linesize[0];
  254. }
  255. /* draw sliding color line */
  256. p = data + picref->linesize[0] * height * 3/4;
  257. grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
  258. GRADIENT_SIZE;
  259. rgrad = 0;
  260. dgrad = GRADIENT_SIZE / width;
  261. drgrad = GRADIENT_SIZE % width;
  262. for (x = 0; x < width; x++) {
  263. *(p++) =
  264. grad < 256 || grad >= 5 * 256 ? 255 :
  265. grad >= 2 * 256 && grad < 4 * 256 ? 0 :
  266. grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
  267. *(p++) =
  268. grad >= 4 * 256 ? 0 :
  269. grad >= 1 * 256 && grad < 3 * 256 ? 255 :
  270. grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
  271. *(p++) =
  272. grad < 2 * 256 ? 0 :
  273. grad >= 3 * 256 && grad < 5 * 256 ? 255 :
  274. grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
  275. grad += dgrad;
  276. rgrad += drgrad;
  277. if (rgrad >= GRADIENT_SIZE) {
  278. grad++;
  279. rgrad -= GRADIENT_SIZE;
  280. }
  281. if (grad >= GRADIENT_SIZE)
  282. grad -= GRADIENT_SIZE;
  283. }
  284. for (y = height / 8; y > 0; y--) {
  285. memcpy(p, p - picref->linesize[0], 3 * width);
  286. p += picref->linesize[0];
  287. }
  288. /* draw digits */
  289. seg_size = width / 80;
  290. if (seg_size >= 1 && height >= 13 * seg_size) {
  291. second = test->nb_frame * test->time_base.num / test->time_base.den;
  292. x = width - (width - seg_size * 64) / 2;
  293. y = (height - seg_size * 13) / 2;
  294. p = data + (x*3 + y * picref->linesize[0]);
  295. for (i = 0; i < 8; i++) {
  296. p -= 3 * 8 * seg_size;
  297. draw_digit(second % 10, p, picref->linesize[0], seg_size);
  298. second /= 10;
  299. if (second == 0)
  300. break;
  301. }
  302. }
  303. }
  304. static av_cold int test_init(AVFilterContext *ctx, const char *args)
  305. {
  306. TestSourceContext *test = ctx->priv;
  307. test->class = &testsrc_class;
  308. test->fill_picture_fn = test_fill_picture;
  309. return init_common(ctx, args);
  310. }
  311. static int test_query_formats(AVFilterContext *ctx)
  312. {
  313. static const enum PixelFormat pix_fmts[] = {
  314. PIX_FMT_RGB24, PIX_FMT_NONE
  315. };
  316. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  317. return 0;
  318. }
  319. AVFilter avfilter_vsrc_testsrc = {
  320. .name = "testsrc",
  321. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  322. .priv_size = sizeof(TestSourceContext),
  323. .init = test_init,
  324. .query_formats = test_query_formats,
  325. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  326. .outputs = (const AVFilterPad[]) {{ .name = "default",
  327. .type = AVMEDIA_TYPE_VIDEO,
  328. .request_frame = request_frame,
  329. .config_props = config_props, },
  330. { .name = NULL }},
  331. };
  332. #endif /* CONFIG_TESTSRC_FILTER */
  333. #if CONFIG_RGBTESTSRC_FILTER
  334. static const char *rgbtestsrc_get_name(void *ctx)
  335. {
  336. return "rgbtestsrc";
  337. }
  338. static const AVClass rgbtestsrc_class = {
  339. .class_name = "RGBTestSourceContext",
  340. .item_name = rgbtestsrc_get_name,
  341. .option = testsrc_options,
  342. };
  343. #define R 0
  344. #define G 1
  345. #define B 2
  346. #define A 3
  347. static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
  348. int x, int y, int r, int g, int b, enum PixelFormat fmt,
  349. int rgba_map[4])
  350. {
  351. int32_t v;
  352. uint8_t *p;
  353. switch (fmt) {
  354. case PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
  355. case PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
  356. case PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
  357. case PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
  358. case PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
  359. case PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
  360. case PIX_FMT_RGB24:
  361. case PIX_FMT_BGR24:
  362. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  363. p = dst + 3*x + y*dst_linesize;
  364. AV_WL24(p, v);
  365. break;
  366. case PIX_FMT_RGBA:
  367. case PIX_FMT_BGRA:
  368. case PIX_FMT_ARGB:
  369. case PIX_FMT_ABGR:
  370. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  371. p = dst + 4*x + y*dst_linesize;
  372. AV_WL32(p, v);
  373. break;
  374. }
  375. }
  376. static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  377. {
  378. TestSourceContext *test = ctx->priv;
  379. int x, y, w = picref->video->w, h = picref->video->h;
  380. for (y = 0; y < h; y++) {
  381. for (x = 0; x < picref->video->w; x++) {
  382. int c = 256*x/w;
  383. int r = 0, g = 0, b = 0;
  384. if (3*y < h ) r = c;
  385. else if (3*y < 2*h) g = c;
  386. else b = c;
  387. rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
  388. ctx->outputs[0]->format, test->rgba_map);
  389. }
  390. }
  391. }
  392. static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args)
  393. {
  394. TestSourceContext *test = ctx->priv;
  395. test->class = &rgbtestsrc_class;
  396. test->fill_picture_fn = rgbtest_fill_picture;
  397. return init_common(ctx, args);
  398. }
  399. static int rgbtest_query_formats(AVFilterContext *ctx)
  400. {
  401. static const enum PixelFormat pix_fmts[] = {
  402. PIX_FMT_RGBA, PIX_FMT_ARGB, PIX_FMT_BGRA, PIX_FMT_ABGR,
  403. PIX_FMT_BGR24, PIX_FMT_RGB24,
  404. PIX_FMT_RGB444, PIX_FMT_BGR444,
  405. PIX_FMT_RGB565, PIX_FMT_BGR565,
  406. PIX_FMT_RGB555, PIX_FMT_BGR555,
  407. PIX_FMT_NONE
  408. };
  409. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  410. return 0;
  411. }
  412. static int rgbtest_config_props(AVFilterLink *outlink)
  413. {
  414. TestSourceContext *test = outlink->src->priv;
  415. switch (outlink->format) {
  416. 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;
  417. 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;
  418. case PIX_FMT_RGBA:
  419. 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;
  420. case PIX_FMT_BGRA:
  421. 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;
  422. }
  423. return config_props(outlink);
  424. }
  425. AVFilter avfilter_vsrc_rgbtestsrc = {
  426. .name = "rgbtestsrc",
  427. .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
  428. .priv_size = sizeof(TestSourceContext),
  429. .init = rgbtest_init,
  430. .query_formats = rgbtest_query_formats,
  431. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  432. .outputs = (const AVFilterPad[]) {{ .name = "default",
  433. .type = AVMEDIA_TYPE_VIDEO,
  434. .request_frame = request_frame,
  435. .config_props = rgbtest_config_props, },
  436. { .name = NULL }},
  437. };
  438. #endif /* CONFIG_RGBTESTSRC_FILTER */