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.

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