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.

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