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.

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