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.

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