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.

541 lines
18KB

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