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.

389 lines
12KB

  1. /*
  2. * Copyright (c) 2002 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. /**
  21. * @file
  22. * MP test source, ported from MPlayer libmpcodecs/vf_test.c
  23. */
  24. #include "libavutil/avstring.h"
  25. #include "libavutil/opt.h"
  26. #include "libavutil/parseutils.h"
  27. #include "libavutil/pixdesc.h"
  28. #include "avfilter.h"
  29. #include "formats.h"
  30. #include "video.h"
  31. #define WIDTH 512
  32. #define HEIGHT 512
  33. enum test_type {
  34. TEST_DC_LUMA,
  35. TEST_DC_CHROMA,
  36. TEST_FREQ_LUMA,
  37. TEST_FREQ_CHROMA,
  38. TEST_AMP_LUMA,
  39. TEST_AMP_CHROMA,
  40. TEST_CBP,
  41. TEST_MV,
  42. TEST_RING1,
  43. TEST_RING2,
  44. TEST_ALL,
  45. TEST_NB
  46. };
  47. typedef struct MPTestContext {
  48. const AVClass *class;
  49. unsigned int frame_nb;
  50. AVRational time_base;
  51. int64_t pts, max_pts;
  52. int hsub, vsub;
  53. char *size, *rate, *duration;
  54. enum test_type test;
  55. } MPTestContext;
  56. #define OFFSET(x) offsetof(MPTestContext, x)
  57. static const AVOption mptestsrc_options[]= {
  58. { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
  59. { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
  60. { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  61. { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  62. { "test", "set test to perform", OFFSET(test), AV_OPT_TYPE_INT, {.dbl=TEST_ALL}, 0, INT_MAX, 0, "test" },
  63. { "t", "set test to perform", OFFSET(test), AV_OPT_TYPE_INT, {.dbl=TEST_ALL}, 0, INT_MAX, 0, "test" },
  64. { "dc_luma", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_DC_LUMA}, INT_MIN, INT_MAX, 0, "test" },
  65. { "dc_chroma", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_DC_CHROMA}, INT_MIN, INT_MAX, 0, "test" },
  66. { "freq_luma", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_FREQ_LUMA}, INT_MIN, INT_MAX, 0, "test" },
  67. { "freq_chroma", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_FREQ_CHROMA}, INT_MIN, INT_MAX, 0, "test" },
  68. { "amp_luma", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_AMP_LUMA}, INT_MIN, INT_MAX, 0, "test" },
  69. { "amp_chroma", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_AMP_CHROMA}, INT_MIN, INT_MAX, 0, "test" },
  70. { "cbp", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_CBP}, INT_MIN, INT_MAX, 0, "test" },
  71. { "mv", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_MV}, INT_MIN, INT_MAX, 0, "test" },
  72. { "ring1", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_RING1}, INT_MIN, INT_MAX, 0, "test" },
  73. { "ring2", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_RING2}, INT_MIN, INT_MAX, 0, "test" },
  74. { "all", "", 0, AV_OPT_TYPE_CONST, {.dbl=TEST_ALL}, INT_MIN, INT_MAX, 0, "test" },
  75. { NULL },
  76. };
  77. static const AVClass mptestsrc_class = {
  78. "MPTestContext",
  79. av_default_item_name,
  80. mptestsrc_options
  81. };
  82. static double c[64];
  83. static void init_idct(void)
  84. {
  85. int i, j;
  86. for (i = 0; i < 8; i++) {
  87. double s = i == 0 ? sqrt(0.125) : 0.5;
  88. for (j = 0; j < 8; j++)
  89. c[i*8+j] = s*cos((M_PI/8.0)*i*(j+0.5));
  90. }
  91. }
  92. static void idct(uint8_t *dst, int dst_linesize, int src[64])
  93. {
  94. int i, j, k;
  95. double tmp[64];
  96. for (i = 0; i < 8; i++) {
  97. for (j = 0; j < 8; j++) {
  98. double sum = 0.0;
  99. for (k = 0; k < 8; k++)
  100. sum += c[k*8+j] * src[8*i+k];
  101. tmp[8*i+j] = sum;
  102. }
  103. }
  104. for (j = 0; j < 8; j++) {
  105. for (i = 0; i < 8; i++) {
  106. double sum = 0.0;
  107. for (k = 0; k < 8; k++)
  108. sum += c[k*8+i]*tmp[8*k+j];
  109. dst[dst_linesize*i + j] = av_clip((int)floor(sum+0.5), 0, 255);
  110. }
  111. }
  112. }
  113. static void draw_dc(uint8_t *dst, int dst_linesize, int color, int w, int h)
  114. {
  115. int x, y;
  116. for (y = 0; y < h; y++)
  117. for (x = 0; x < w; x++)
  118. dst[x + y*dst_linesize] = color;
  119. }
  120. static void draw_basis(uint8_t *dst, int dst_linesize, int amp, int freq, int dc)
  121. {
  122. int src[64];
  123. memset(src, 0, 64*sizeof(int));
  124. src[0] = dc;
  125. if (amp)
  126. src[freq] = amp;
  127. idct(dst, dst_linesize, src);
  128. }
  129. static void draw_cbp(uint8_t *dst[3], int dst_linesize[3], int cbp, int amp, int dc)
  130. {
  131. if (cbp&1) draw_basis(dst[0] , dst_linesize[0], amp, 1, dc);
  132. if (cbp&2) draw_basis(dst[0]+8 , dst_linesize[0], amp, 1, dc);
  133. if (cbp&4) draw_basis(dst[0]+ 8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
  134. if (cbp&8) draw_basis(dst[0]+8+8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
  135. if (cbp&16) draw_basis(dst[1] , dst_linesize[1], amp, 1, dc);
  136. if (cbp&32) draw_basis(dst[2] , dst_linesize[2], amp, 1, dc);
  137. }
  138. static void dc_test(uint8_t *dst, int dst_linesize, int w, int h, int off)
  139. {
  140. const int step = FFMAX(256/(w*h/256), 1);
  141. int x, y, color = off;
  142. for (y = 0; y < h; y += 16) {
  143. for (x = 0; x < w; x += 16) {
  144. draw_dc(dst + x + y*dst_linesize, dst_linesize, color, 8, 8);
  145. color += step;
  146. }
  147. }
  148. }
  149. static void freq_test(uint8_t *dst, int dst_linesize, int off)
  150. {
  151. int x, y, freq = 0;
  152. for (y = 0; y < 8*16; y += 16) {
  153. for (x = 0; x < 8*16; x += 16) {
  154. draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*(96+off), freq, 128*8);
  155. freq++;
  156. }
  157. }
  158. }
  159. static void amp_test(uint8_t *dst, int dst_linesize, int off)
  160. {
  161. int x, y, amp = off;
  162. for (y = 0; y < 16*16; y += 16) {
  163. for (x = 0; x < 16*16; x += 16) {
  164. draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*amp, 1, 128*8);
  165. amp++;
  166. }
  167. }
  168. }
  169. static void cbp_test(uint8_t *dst[3], int dst_linesize[3], int off)
  170. {
  171. int x, y, cbp = 0;
  172. for (y = 0; y < 16*8; y += 16) {
  173. for (x = 0; x < 16*8; x += 16) {
  174. uint8_t *dst1[3];
  175. dst1[0] = dst[0] + x*2 + y*2*dst_linesize[0];
  176. dst1[1] = dst[1] + x + y* dst_linesize[1];
  177. dst1[2] = dst[2] + x + y* dst_linesize[2];
  178. draw_cbp(dst1, dst_linesize, cbp, (64+off)*4, 128*8);
  179. cbp++;
  180. }
  181. }
  182. }
  183. static void mv_test(uint8_t *dst, int dst_linesize, int off)
  184. {
  185. int x, y;
  186. for (y = 0; y < 16*16; y++) {
  187. if (y&16)
  188. continue;
  189. for (x = 0; x < 16*16; x++)
  190. dst[x + y*dst_linesize] = x + off*8/(y/32+1);
  191. }
  192. }
  193. static void ring1_test(uint8_t *dst, int dst_linesize, int off)
  194. {
  195. int x, y, color = 0;
  196. for (y = off; y < 16*16; y += 16) {
  197. for (x = off; x < 16*16; x += 16) {
  198. draw_dc(dst + x + y*dst_linesize, dst_linesize, ((x+y)&16) ? color : -color, 16, 16);
  199. color++;
  200. }
  201. }
  202. }
  203. static void ring2_test(uint8_t *dst, int dst_linesize, int off)
  204. {
  205. int x, y;
  206. for (y = 0; y < 16*16; y++) {
  207. for (x = 0; x < 16*16; x++) {
  208. double d = sqrt((x-8*16)*(x-8*16) + (y-8*16)*(y-8*16));
  209. double r = d/20 - (int)(d/20);
  210. if (r < off/30.0) {
  211. dst[x + y*dst_linesize] = 255;
  212. dst[x + y*dst_linesize+256] = 0;
  213. } else {
  214. dst[x + y*dst_linesize] = x;
  215. dst[x + y*dst_linesize+256] = x;
  216. }
  217. }
  218. }
  219. }
  220. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  221. {
  222. MPTestContext *test = ctx->priv;
  223. AVRational frame_rate_q;
  224. int64_t duration = -1;
  225. int ret;
  226. test->class = &mptestsrc_class;
  227. av_opt_set_defaults(test);
  228. if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
  229. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  230. return ret;
  231. }
  232. if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0 ||
  233. frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
  234. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
  235. return ret;
  236. }
  237. if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
  238. av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
  239. return ret;
  240. }
  241. test->time_base.num = frame_rate_q.den;
  242. test->time_base.den = frame_rate_q.num;
  243. test->max_pts = duration >= 0 ?
  244. av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
  245. test->frame_nb = 0;
  246. test->pts = 0;
  247. av_log(ctx, AV_LOG_INFO, "rate:%d/%d duration:%f\n",
  248. frame_rate_q.num, frame_rate_q.den,
  249. duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base));
  250. init_idct();
  251. return 0;
  252. }
  253. static int config_props(AVFilterLink *outlink)
  254. {
  255. AVFilterContext *ctx = outlink->src;
  256. MPTestContext *test = ctx->priv;
  257. const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[outlink->format];
  258. test->hsub = pix_desc->log2_chroma_w;
  259. test->vsub = pix_desc->log2_chroma_h;
  260. outlink->w = WIDTH;
  261. outlink->h = HEIGHT;
  262. outlink->time_base = test->time_base;
  263. return 0;
  264. }
  265. static int query_formats(AVFilterContext *ctx)
  266. {
  267. static const enum PixelFormat pix_fmts[] = {
  268. PIX_FMT_YUV420P, PIX_FMT_NONE
  269. };
  270. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  271. return 0;
  272. }
  273. static int request_frame(AVFilterLink *outlink)
  274. {
  275. MPTestContext *test = outlink->src->priv;
  276. AVFilterBufferRef *picref;
  277. int w = WIDTH, h = HEIGHT, ch = h>>test->vsub;
  278. unsigned int frame = test->frame_nb;
  279. enum test_type tt = test->test;
  280. if (test->max_pts >= 0 && test->pts > test->max_pts)
  281. return AVERROR_EOF;
  282. picref = ff_get_video_buffer(outlink, AV_PERM_WRITE, w, h);
  283. picref->pts = test->pts++;
  284. // clean image
  285. memset(picref->data[0], 0, picref->linesize[0] * h);
  286. memset(picref->data[1], 128, picref->linesize[1] * ch);
  287. memset(picref->data[2], 128, picref->linesize[2] * ch);
  288. if (tt == TEST_ALL && frame%30) /* draw a black frame at the beginning of each test */
  289. tt = (frame/30)%(TEST_NB-1);
  290. switch (tt) {
  291. case TEST_DC_LUMA: dc_test(picref->data[0], picref->linesize[0], 256, 256, frame%30); break;
  292. case TEST_DC_CHROMA: dc_test(picref->data[1], picref->linesize[1], 256, 256, frame%30); break;
  293. case TEST_FREQ_LUMA: freq_test(picref->data[0], picref->linesize[0], frame%30); break;
  294. case TEST_FREQ_CHROMA: freq_test(picref->data[1], picref->linesize[1], frame%30); break;
  295. case TEST_AMP_LUMA: amp_test(picref->data[0], picref->linesize[0], frame%30); break;
  296. case TEST_AMP_CHROMA: amp_test(picref->data[1], picref->linesize[1], frame%30); break;
  297. case TEST_CBP: cbp_test(picref->data , picref->linesize , frame%30); break;
  298. case TEST_MV: mv_test(picref->data[0], picref->linesize[0], frame%30); break;
  299. case TEST_RING1: ring1_test(picref->data[0], picref->linesize[0], frame%30); break;
  300. case TEST_RING2: ring2_test(picref->data[0], picref->linesize[0], frame%30); break;
  301. }
  302. test->frame_nb++;
  303. ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  304. ff_draw_slice(outlink, 0, picref->video->h, 1);
  305. ff_end_frame(outlink);
  306. avfilter_unref_buffer(picref);
  307. return 0;
  308. }
  309. AVFilter avfilter_vsrc_mptestsrc = {
  310. .name = "mptestsrc",
  311. .description = NULL_IF_CONFIG_SMALL("Generate various test pattern."),
  312. .priv_size = sizeof(MPTestContext),
  313. .init = init,
  314. .query_formats = query_formats,
  315. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  316. .outputs = (const AVFilterPad[]) {{ .name = "default",
  317. .type = AVMEDIA_TYPE_VIDEO,
  318. .request_frame = request_frame,
  319. .config_props = config_props, },
  320. { .name = NULL }},
  321. };