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.

353 lines
12KB

  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. * Based on the test pattern generator demuxer by Nicolas George:
  24. * http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/2007-October/037845.html
  25. */
  26. #include <float.h>
  27. #include "libavutil/opt.h"
  28. #include "libavutil/parseutils.h"
  29. #include "avfilter.h"
  30. typedef struct {
  31. const AVClass *class;
  32. int h, w;
  33. unsigned int nb_frame;
  34. AVRational time_base;
  35. int64_t pts, max_pts;
  36. char *size; ///< video frame size
  37. char *rate; ///< video frame rate
  38. char *duration; ///< total duration of the generated video
  39. void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
  40. } TestSourceContext;
  41. #define OFFSET(x) offsetof(TestSourceContext, x)
  42. static const AVOption testsrc_options[]= {
  43. { "size", "set video size", OFFSET(size), FF_OPT_TYPE_STRING, {.str = "320x240"}, 0, 0 },
  44. { "s", "set video size", OFFSET(size), FF_OPT_TYPE_STRING, {.str = "320x240"}, 0, 0 },
  45. { "rate", "set video rate", OFFSET(rate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
  46. { "r", "set video rate", OFFSET(rate), FF_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
  47. { "duration", "set video duration", OFFSET(duration), FF_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  48. { NULL },
  49. };
  50. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  51. {
  52. TestSourceContext *test = ctx->priv;
  53. AVRational frame_rate_q;
  54. int64_t duration = -1;
  55. int ret = 0;
  56. av_opt_set_defaults2(test, 0, 0);
  57. if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
  58. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  59. return ret;
  60. }
  61. if ((ret = av_parse_video_size(&test->w, &test->h, test->size)) < 0) {
  62. av_log(ctx, AV_LOG_ERROR, "Invalid frame size: '%s'\n", test->size);
  63. return ret;
  64. }
  65. if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0 ||
  66. frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
  67. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
  68. return ret;
  69. }
  70. if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
  71. av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
  72. return ret;
  73. }
  74. test->time_base.num = frame_rate_q.den;
  75. test->time_base.den = frame_rate_q.num;
  76. test->max_pts = duration >= 0 ?
  77. av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
  78. test->nb_frame = 0;
  79. test->pts = 0;
  80. av_log(ctx, AV_LOG_INFO, "size:%dx%d rate:%d/%d duration:%f\n",
  81. test->w, test->h, frame_rate_q.num, frame_rate_q.den,
  82. duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base));
  83. return 0;
  84. }
  85. static int config_props(AVFilterLink *outlink)
  86. {
  87. TestSourceContext *test = outlink->src->priv;
  88. outlink->w = test->w;
  89. outlink->h = test->h;
  90. outlink->time_base = test->time_base;
  91. return 0;
  92. }
  93. static int request_frame(AVFilterLink *outlink)
  94. {
  95. TestSourceContext *test = outlink->src->priv;
  96. AVFilterBufferRef *picref;
  97. if (test->max_pts >= 0 && test->pts > test->max_pts)
  98. return AVERROR_EOF;
  99. picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
  100. test->w, test->h);
  101. picref->pts = test->pts++;
  102. test->nb_frame++;
  103. test->fill_picture_fn(outlink->src, picref);
  104. avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  105. avfilter_draw_slice(outlink, 0, picref->video->h, 1);
  106. avfilter_end_frame(outlink);
  107. avfilter_unref_buffer(picref);
  108. return 0;
  109. }
  110. #if CONFIG_TESTSRC_FILTER
  111. static const char *testsrc_get_name(void *ctx)
  112. {
  113. return "testsrc";
  114. }
  115. static const AVClass testsrc_class = {
  116. "TestSourceContext",
  117. testsrc_get_name,
  118. testsrc_options
  119. };
  120. /**
  121. * Fill a rectangle with value val.
  122. *
  123. * @param val the RGB value to set
  124. * @param dst pointer to the destination buffer to fill
  125. * @param dst_linesize linesize of destination
  126. * @param segment_width width of the segment
  127. * @param x horizontal coordinate where to draw the rectangle in the destination buffer
  128. * @param y horizontal coordinate where to draw the rectangle in the destination buffer
  129. * @param w width of the rectangle to draw, expressed as a number of segment_width units
  130. * @param h height of the rectangle to draw, expressed as a number of segment_width units
  131. */
  132. static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
  133. unsigned x, unsigned y, unsigned w, unsigned h)
  134. {
  135. int i;
  136. int step = 3;
  137. dst += segment_width * (step * x + y * dst_linesize);
  138. w *= segment_width * step;
  139. h *= segment_width;
  140. for (i = 0; i < h; i++) {
  141. memset(dst, val, w);
  142. dst += dst_linesize;
  143. }
  144. }
  145. static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
  146. unsigned segment_width)
  147. {
  148. #define TOP_HBAR 1
  149. #define MID_HBAR 2
  150. #define BOT_HBAR 4
  151. #define LEFT_TOP_VBAR 8
  152. #define LEFT_BOT_VBAR 16
  153. #define RIGHT_TOP_VBAR 32
  154. #define RIGHT_BOT_VBAR 64
  155. struct {
  156. int x, y, w, h;
  157. } segments[] = {
  158. { 1, 0, 5, 1 }, /* TOP_HBAR */
  159. { 1, 6, 5, 1 }, /* MID_HBAR */
  160. { 1, 12, 5, 1 }, /* BOT_HBAR */
  161. { 0, 1, 1, 5 }, /* LEFT_TOP_VBAR */
  162. { 0, 7, 1, 5 }, /* LEFT_BOT_VBAR */
  163. { 6, 1, 1, 5 }, /* RIGHT_TOP_VBAR */
  164. { 6, 7, 1, 5 } /* RIGHT_BOT_VBAR */
  165. };
  166. static const unsigned char masks[10] = {
  167. /* 0 */ TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  168. /* 1 */ RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  169. /* 2 */ TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
  170. /* 3 */ TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  171. /* 4 */ MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  172. /* 5 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
  173. /* 6 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
  174. /* 7 */ TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  175. /* 8 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  176. /* 9 */ TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
  177. };
  178. unsigned mask = masks[digit];
  179. int i;
  180. draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
  181. for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
  182. if (mask & (1<<i))
  183. draw_rectangle(255, dst, dst_linesize, segment_width,
  184. segments[i].x, segments[i].y, segments[i].w, segments[i].h);
  185. }
  186. #define GRADIENT_SIZE (6 * 256)
  187. static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  188. {
  189. TestSourceContext *test = ctx->priv;
  190. uint8_t *p, *p0;
  191. int x, y;
  192. int color, color_rest;
  193. int icolor;
  194. int radius;
  195. int quad0, quad;
  196. int dquad_x, dquad_y;
  197. int grad, dgrad, rgrad, drgrad;
  198. int seg_size;
  199. int second;
  200. int i;
  201. uint8_t *data = picref->data[0];
  202. int width = picref->video->w;
  203. int height = picref->video->h;
  204. /* draw colored bars and circle */
  205. radius = (width + height) / 4;
  206. quad0 = width * width / 4 + height * height / 4 - radius * radius;
  207. dquad_y = 1 - height;
  208. p0 = data;
  209. for (y = 0; y < height; y++) {
  210. p = p0;
  211. color = 0;
  212. color_rest = 0;
  213. quad = quad0;
  214. dquad_x = 1 - width;
  215. for (x = 0; x < width; x++) {
  216. icolor = color;
  217. if (quad < 0)
  218. icolor ^= 7;
  219. quad += dquad_x;
  220. dquad_x += 2;
  221. *(p++) = icolor & 1 ? 255 : 0;
  222. *(p++) = icolor & 2 ? 255 : 0;
  223. *(p++) = icolor & 4 ? 255 : 0;
  224. color_rest += 8;
  225. if (color_rest >= width) {
  226. color_rest -= width;
  227. color++;
  228. }
  229. }
  230. quad0 += dquad_y;
  231. dquad_y += 2;
  232. p0 += picref->linesize[0];
  233. }
  234. /* draw sliding color line */
  235. p = data + picref->linesize[0] * height * 3/4;
  236. grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
  237. GRADIENT_SIZE;
  238. rgrad = 0;
  239. dgrad = GRADIENT_SIZE / width;
  240. drgrad = GRADIENT_SIZE % width;
  241. for (x = 0; x < width; x++) {
  242. *(p++) =
  243. grad < 256 || grad >= 5 * 256 ? 255 :
  244. grad >= 2 * 256 && grad < 4 * 256 ? 0 :
  245. grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
  246. *(p++) =
  247. grad >= 4 * 256 ? 0 :
  248. grad >= 1 * 256 && grad < 3 * 256 ? 255 :
  249. grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
  250. *(p++) =
  251. grad < 2 * 256 ? 0 :
  252. grad >= 3 * 256 && grad < 5 * 256 ? 255 :
  253. grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
  254. grad += dgrad;
  255. rgrad += drgrad;
  256. if (rgrad >= GRADIENT_SIZE) {
  257. grad++;
  258. rgrad -= GRADIENT_SIZE;
  259. }
  260. if (grad >= GRADIENT_SIZE)
  261. grad -= GRADIENT_SIZE;
  262. }
  263. for (y = height / 8; y > 0; y--) {
  264. memcpy(p, p - picref->linesize[0], 3 * width);
  265. p += picref->linesize[0];
  266. }
  267. /* draw digits */
  268. seg_size = width / 80;
  269. if (seg_size >= 1 && height >= 13 * seg_size) {
  270. second = test->nb_frame * test->time_base.num / test->time_base.den;
  271. x = width - (width - seg_size * 64) / 2;
  272. y = (height - seg_size * 13) / 2;
  273. p = data + (x*3 + y * picref->linesize[0]);
  274. for (i = 0; i < 8; i++) {
  275. p -= 3 * 8 * seg_size;
  276. draw_digit(second % 10, p, picref->linesize[0], seg_size);
  277. second /= 10;
  278. if (second == 0)
  279. break;
  280. }
  281. }
  282. }
  283. static av_cold int test_init(AVFilterContext *ctx, const char *args, void *opaque)
  284. {
  285. TestSourceContext *test = ctx->priv;
  286. test->class = &testsrc_class;
  287. test->fill_picture_fn = test_fill_picture;
  288. return init(ctx, args, opaque);
  289. }
  290. static int test_query_formats(AVFilterContext *ctx)
  291. {
  292. static const enum PixelFormat pix_fmts[] = {
  293. PIX_FMT_RGB24, PIX_FMT_NONE
  294. };
  295. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  296. return 0;
  297. }
  298. AVFilter avfilter_vsrc_testsrc = {
  299. .name = "testsrc",
  300. .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
  301. .priv_size = sizeof(TestSourceContext),
  302. .init = test_init,
  303. .query_formats = test_query_formats,
  304. .inputs = (AVFilterPad[]) {{ .name = NULL}},
  305. .outputs = (AVFilterPad[]) {{ .name = "default",
  306. .type = AVMEDIA_TYPE_VIDEO,
  307. .request_frame = request_frame,
  308. .config_props = config_props, },
  309. { .name = NULL }},
  310. };
  311. #endif /* CONFIG_TESTSRC_FILTER */