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.

506 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 Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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/common.h"
  33. #include "libavutil/mathematics.h"
  34. #include "libavutil/opt.h"
  35. #include "libavutil/intreadwrite.h"
  36. #include "libavutil/parseutils.h"
  37. #include "avfilter.h"
  38. #include "formats.h"
  39. #include "internal.h"
  40. #include "video.h"
  41. typedef struct {
  42. const AVClass *class;
  43. int h, w;
  44. unsigned int nb_frame;
  45. AVRational time_base;
  46. int64_t pts, max_pts;
  47. char *size; ///< video frame size
  48. char *rate; ///< video frame rate
  49. char *duration; ///< total duration of the generated video
  50. AVRational sar; ///< sample aspect ratio
  51. void (* fill_picture_fn)(AVFilterContext *ctx, AVFrame *frame);
  52. /* only used by rgbtest */
  53. int rgba_map[4];
  54. } TestSourceContext;
  55. #define OFFSET(x) offsetof(TestSourceContext, x)
  56. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
  57. static const AVOption testsrc_options[] = {
  58. { "size", "set video size", OFFSET(size), AV_OPT_TYPE_STRING, {.str = "320x240"}, .flags = FLAGS },
  59. { "s", "set video size", OFFSET(size), AV_OPT_TYPE_STRING, {.str = "320x240"}, .flags = FLAGS },
  60. { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, .flags = FLAGS },
  61. { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, .flags = FLAGS },
  62. { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, .flags = FLAGS },
  63. { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl = 1}, 0, INT_MAX, FLAGS },
  64. { NULL },
  65. };
  66. static av_cold int init_common(AVFilterContext *ctx)
  67. {
  68. TestSourceContext *test = ctx->priv;
  69. AVRational frame_rate_q;
  70. int64_t duration = -1;
  71. int ret = 0;
  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_DEBUG, "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. AVFrame *frame;
  110. if (test->max_pts >= 0 && test->pts > test->max_pts)
  111. return AVERROR_EOF;
  112. frame = ff_get_video_buffer(outlink, test->w, test->h);
  113. if (!frame)
  114. return AVERROR(ENOMEM);
  115. frame->pts = test->pts++;
  116. frame->key_frame = 1;
  117. frame->interlaced_frame = 0;
  118. frame->pict_type = AV_PICTURE_TYPE_I;
  119. frame->sample_aspect_ratio = test->sar;
  120. test->nb_frame++;
  121. test->fill_picture_fn(outlink->src, frame);
  122. return ff_filter_frame(outlink, frame);
  123. }
  124. #if CONFIG_TESTSRC_FILTER
  125. static const char *testsrc_get_name(void *ctx)
  126. {
  127. return "testsrc";
  128. }
  129. static const AVClass testsrc_class = {
  130. .class_name = "TestSourceContext",
  131. .item_name = testsrc_get_name,
  132. .option = testsrc_options,
  133. };
  134. /**
  135. * Fill a rectangle with value val.
  136. *
  137. * @param val the RGB value to set
  138. * @param dst pointer to the destination buffer to fill
  139. * @param dst_linesize linesize of destination
  140. * @param segment_width width of the segment
  141. * @param x horizontal coordinate where to draw the rectangle in the destination buffer
  142. * @param y horizontal coordinate where to draw the rectangle in the destination buffer
  143. * @param w width of the rectangle to draw, expressed as a number of segment_width units
  144. * @param h height of the rectangle to draw, expressed as a number of segment_width units
  145. */
  146. static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
  147. unsigned x, unsigned y, unsigned w, unsigned h)
  148. {
  149. int i;
  150. int step = 3;
  151. dst += segment_width * (step * x + y * dst_linesize);
  152. w *= segment_width * step;
  153. h *= segment_width;
  154. for (i = 0; i < h; i++) {
  155. memset(dst, val, w);
  156. dst += dst_linesize;
  157. }
  158. }
  159. static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
  160. unsigned segment_width)
  161. {
  162. #define TOP_HBAR 1
  163. #define MID_HBAR 2
  164. #define BOT_HBAR 4
  165. #define LEFT_TOP_VBAR 8
  166. #define LEFT_BOT_VBAR 16
  167. #define RIGHT_TOP_VBAR 32
  168. #define RIGHT_BOT_VBAR 64
  169. struct {
  170. int x, y, w, h;
  171. } segments[] = {
  172. { 1, 0, 5, 1 }, /* TOP_HBAR */
  173. { 1, 6, 5, 1 }, /* MID_HBAR */
  174. { 1, 12, 5, 1 }, /* BOT_HBAR */
  175. { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
  176. { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
  177. { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
  178. { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
  179. };
  180. static const unsigned char masks[10] = {
  181. /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  182. /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  183. /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
  184. /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  185. /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  186. /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
  187. /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
  188. /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  189. /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  190. /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  191. };
  192. unsigned mask = masks[digit];
  193. int i;
  194. draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
  195. for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
  196. if (mask & (1<<i))
  197. draw_rectangle(255, dst, dst_linesize, segment_width,
  198. segments[i].x, segments[i].y, segments[i].w, segments[i].h);
  199. }
  200. #define GRADIENT_SIZE (6 * 256)
  201. static void test_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  202. {
  203. TestSourceContext *test = ctx->priv;
  204. uint8_t *p, *p0;
  205. int x, y;
  206. int color, color_rest;
  207. int icolor;
  208. int radius;
  209. int quad0, quad;
  210. int dquad_x, dquad_y;
  211. int grad, dgrad, rgrad, drgrad;
  212. int seg_size;
  213. int second;
  214. int i;
  215. uint8_t *data = frame->data[0];
  216. int width = frame->width;
  217. int height = frame->height;
  218. /* draw colored bars and circle */
  219. radius = (width + height) / 4;
  220. quad0 = width * width / 4 + height * height / 4 - radius * radius;
  221. dquad_y = 1 - height;
  222. p0 = data;
  223. for (y = 0; y < height; y++) {
  224. p = p0;
  225. color = 0;
  226. color_rest = 0;
  227. quad = quad0;
  228. dquad_x = 1 - width;
  229. for (x = 0; x < width; x++) {
  230. icolor = color;
  231. if (quad < 0)
  232. icolor ^= 7;
  233. quad += dquad_x;
  234. dquad_x += 2;
  235. *(p++) = icolor & 1 ? 255 : 0;
  236. *(p++) = icolor & 2 ? 255 : 0;
  237. *(p++) = icolor & 4 ? 255 : 0;
  238. color_rest += 8;
  239. if (color_rest >= width) {
  240. color_rest -= width;
  241. color++;
  242. }
  243. }
  244. quad0 += dquad_y;
  245. dquad_y += 2;
  246. p0 += frame->linesize[0];
  247. }
  248. /* draw sliding color line */
  249. p = data + frame->linesize[0] * height * 3/4;
  250. grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
  251. GRADIENT_SIZE;
  252. rgrad = 0;
  253. dgrad = GRADIENT_SIZE / width;
  254. drgrad = GRADIENT_SIZE % width;
  255. for (x = 0; x < width; x++) {
  256. *(p++) =
  257. grad < 256 || grad >= 5 * 256 ? 255 :
  258. grad >= 2 * 256 && grad < 4 * 256 ? 0 :
  259. grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
  260. *(p++) =
  261. grad >= 4 * 256 ? 0 :
  262. grad >= 1 * 256 && grad < 3 * 256 ? 255 :
  263. grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
  264. *(p++) =
  265. grad < 2 * 256 ? 0 :
  266. grad >= 3 * 256 && grad < 5 * 256 ? 255 :
  267. grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
  268. grad += dgrad;
  269. rgrad += drgrad;
  270. if (rgrad >= GRADIENT_SIZE) {
  271. grad++;
  272. rgrad -= GRADIENT_SIZE;
  273. }
  274. if (grad >= GRADIENT_SIZE)
  275. grad -= GRADIENT_SIZE;
  276. }
  277. for (y = height / 8; y > 0; y--) {
  278. memcpy(p, p - frame->linesize[0], 3 * width);
  279. p += frame->linesize[0];
  280. }
  281. /* draw digits */
  282. seg_size = width / 80;
  283. if (seg_size >= 1 && height >= 13 * seg_size) {
  284. second = test->nb_frame * test->time_base.num / test->time_base.den;
  285. x = width - (width - seg_size * 64) / 2;
  286. y = (height - seg_size * 13) / 2;
  287. p = data + (x*3 + y * frame->linesize[0]);
  288. for (i = 0; i < 8; i++) {
  289. p -= 3 * 8 * seg_size;
  290. draw_digit(second % 10, p, frame->linesize[0], seg_size);
  291. second /= 10;
  292. if (second == 0)
  293. break;
  294. }
  295. }
  296. }
  297. static av_cold int test_init(AVFilterContext *ctx)
  298. {
  299. TestSourceContext *test = ctx->priv;
  300. test->fill_picture_fn = test_fill_picture;
  301. return init_common(ctx);
  302. }
  303. static int test_query_formats(AVFilterContext *ctx)
  304. {
  305. static const enum AVPixelFormat pix_fmts[] = {
  306. AV_PIX_FMT_RGB24, AV_PIX_FMT_NONE
  307. };
  308. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  309. return 0;
  310. }
  311. static const AVFilterPad avfilter_vsrc_testsrc_outputs[] = {
  312. {
  313. .name = "default",
  314. .type = AVMEDIA_TYPE_VIDEO,
  315. .request_frame = request_frame,
  316. .config_props = config_props,
  317. },
  318. { NULL }
  319. };
  320. AVFilter ff_vsrc_testsrc = {
  321. .name = "testsrc",
  322. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  323. .priv_size = sizeof(TestSourceContext),
  324. .priv_class = &testsrc_class,
  325. .init = test_init,
  326. .query_formats = test_query_formats,
  327. .inputs = NULL,
  328. .outputs = avfilter_vsrc_testsrc_outputs,
  329. };
  330. #endif /* CONFIG_TESTSRC_FILTER */
  331. #if CONFIG_RGBTESTSRC_FILTER
  332. static const char *rgbtestsrc_get_name(void *ctx)
  333. {
  334. return "rgbtestsrc";
  335. }
  336. static const AVClass rgbtestsrc_class = {
  337. .class_name = "RGBTestSourceContext",
  338. .item_name = rgbtestsrc_get_name,
  339. .option = testsrc_options,
  340. };
  341. #define R 0
  342. #define G 1
  343. #define B 2
  344. #define A 3
  345. static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
  346. int x, int y, int r, int g, int b, enum AVPixelFormat fmt,
  347. int rgba_map[4])
  348. {
  349. int32_t v;
  350. uint8_t *p;
  351. switch (fmt) {
  352. case AV_PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
  353. case AV_PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
  354. case AV_PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
  355. case AV_PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
  356. case AV_PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
  357. case AV_PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
  358. case AV_PIX_FMT_RGB24:
  359. case AV_PIX_FMT_BGR24:
  360. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  361. p = dst + 3*x + y*dst_linesize;
  362. AV_WL24(p, v);
  363. break;
  364. case AV_PIX_FMT_RGBA:
  365. case AV_PIX_FMT_BGRA:
  366. case AV_PIX_FMT_ARGB:
  367. case AV_PIX_FMT_ABGR:
  368. v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
  369. p = dst + 4*x + y*dst_linesize;
  370. AV_WL32(p, v);
  371. break;
  372. }
  373. }
  374. static void rgbtest_fill_picture(AVFilterContext *ctx, AVFrame *frame)
  375. {
  376. TestSourceContext *test = ctx->priv;
  377. int x, y, w = frame->width, h = frame->height;
  378. for (y = 0; y < h; y++) {
  379. for (x = 0; x < w; x++) {
  380. int c = 256*x/w;
  381. int r = 0, g = 0, b = 0;
  382. if (3*y < h ) r = c;
  383. else if (3*y < 2*h) g = c;
  384. else b = c;
  385. rgbtest_put_pixel(frame->data[0], frame->linesize[0], x, y, r, g, b,
  386. ctx->outputs[0]->format, test->rgba_map);
  387. }
  388. }
  389. }
  390. static av_cold int rgbtest_init(AVFilterContext *ctx)
  391. {
  392. TestSourceContext *test = ctx->priv;
  393. test->fill_picture_fn = rgbtest_fill_picture;
  394. return init_common(ctx);
  395. }
  396. static int rgbtest_query_formats(AVFilterContext *ctx)
  397. {
  398. static const enum AVPixelFormat pix_fmts[] = {
  399. AV_PIX_FMT_RGBA, AV_PIX_FMT_ARGB, AV_PIX_FMT_BGRA, AV_PIX_FMT_ABGR,
  400. AV_PIX_FMT_BGR24, AV_PIX_FMT_RGB24,
  401. AV_PIX_FMT_RGB444, AV_PIX_FMT_BGR444,
  402. AV_PIX_FMT_RGB565, AV_PIX_FMT_BGR565,
  403. AV_PIX_FMT_RGB555, AV_PIX_FMT_BGR555,
  404. AV_PIX_FMT_NONE
  405. };
  406. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  407. return 0;
  408. }
  409. static int rgbtest_config_props(AVFilterLink *outlink)
  410. {
  411. TestSourceContext *test = outlink->src->priv;
  412. switch (outlink->format) {
  413. case AV_PIX_FMT_ARGB: test->rgba_map[A] = 0; test->rgba_map[R] = 1; test->rgba_map[G] = 2; test->rgba_map[B] = 3; break;
  414. case AV_PIX_FMT_ABGR: test->rgba_map[A] = 0; test->rgba_map[B] = 1; test->rgba_map[G] = 2; test->rgba_map[R] = 3; break;
  415. case AV_PIX_FMT_RGBA:
  416. case AV_PIX_FMT_RGB24: test->rgba_map[R] = 0; test->rgba_map[G] = 1; test->rgba_map[B] = 2; test->rgba_map[A] = 3; break;
  417. case AV_PIX_FMT_BGRA:
  418. case AV_PIX_FMT_BGR24: test->rgba_map[B] = 0; test->rgba_map[G] = 1; test->rgba_map[R] = 2; test->rgba_map[A] = 3; break;
  419. }
  420. return config_props(outlink);
  421. }
  422. static const AVFilterPad avfilter_vsrc_rgbtestsrc_outputs[] = {
  423. {
  424. .name = "default",
  425. .type = AVMEDIA_TYPE_VIDEO,
  426. .request_frame = request_frame,
  427. .config_props = rgbtest_config_props,
  428. },
  429. { NULL }
  430. };
  431. AVFilter ff_vsrc_rgbtestsrc = {
  432. .name = "rgbtestsrc",
  433. .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
  434. .priv_size = sizeof(TestSourceContext),
  435. .priv_class = &rgbtestsrc_class,
  436. .init = rgbtest_init,
  437. .query_formats = rgbtest_query_formats,
  438. .inputs = NULL,
  439. .outputs = avfilter_vsrc_rgbtestsrc_outputs,
  440. };
  441. #endif /* CONFIG_RGBTESTSRC_FILTER */