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.

371 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 "internal.h"
  30. #include "formats.h"
  31. #include "video.h"
  32. #define WIDTH 512
  33. #define HEIGHT 512
  34. enum test_type {
  35. TEST_DC_LUMA,
  36. TEST_DC_CHROMA,
  37. TEST_FREQ_LUMA,
  38. TEST_FREQ_CHROMA,
  39. TEST_AMP_LUMA,
  40. TEST_AMP_CHROMA,
  41. TEST_CBP,
  42. TEST_MV,
  43. TEST_RING1,
  44. TEST_RING2,
  45. TEST_ALL,
  46. TEST_NB
  47. };
  48. typedef struct MPTestContext {
  49. const AVClass *class;
  50. unsigned int frame_nb;
  51. AVRational frame_rate;
  52. int64_t pts, max_pts;
  53. int hsub, vsub;
  54. char *size, *duration;
  55. enum test_type test;
  56. } MPTestContext;
  57. #define OFFSET(x) offsetof(MPTestContext, x)
  58. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  59. static const AVOption mptestsrc_options[]= {
  60. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
  61. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
  62. { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  63. { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  64. { "test", "set test to perform", OFFSET(test), AV_OPT_TYPE_INT, {.i64=TEST_ALL}, 0, INT_MAX, FLAGS, "test" },
  65. { "t", "set test to perform", OFFSET(test), AV_OPT_TYPE_INT, {.i64=TEST_ALL}, 0, INT_MAX, FLAGS, "test" },
  66. { "dc_luma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_LUMA}, INT_MIN, INT_MAX, FLAGS, "test" },
  67. { "dc_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_DC_CHROMA}, INT_MIN, INT_MAX, FLAGS, "test" },
  68. { "freq_luma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_LUMA}, INT_MIN, INT_MAX, FLAGS, "test" },
  69. { "freq_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_FREQ_CHROMA}, INT_MIN, INT_MAX, FLAGS, "test" },
  70. { "amp_luma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_LUMA}, INT_MIN, INT_MAX, FLAGS, "test" },
  71. { "amp_chroma", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_AMP_CHROMA}, INT_MIN, INT_MAX, FLAGS, "test" },
  72. { "cbp", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_CBP}, INT_MIN, INT_MAX, FLAGS, "test" },
  73. { "mv", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_MV}, INT_MIN, INT_MAX, FLAGS, "test" },
  74. { "ring1", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING1}, INT_MIN, INT_MAX, FLAGS, "test" },
  75. { "ring2", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_RING2}, INT_MIN, INT_MAX, FLAGS, "test" },
  76. { "all", "", 0, AV_OPT_TYPE_CONST, {.i64=TEST_ALL}, INT_MIN, INT_MAX, FLAGS, "test" },
  77. { NULL },
  78. };
  79. AVFILTER_DEFINE_CLASS(mptestsrc);
  80. static double c[64];
  81. static void init_idct(void)
  82. {
  83. int i, j;
  84. for (i = 0; i < 8; i++) {
  85. double s = i == 0 ? sqrt(0.125) : 0.5;
  86. for (j = 0; j < 8; j++)
  87. c[i*8+j] = s*cos((M_PI/8.0)*i*(j+0.5));
  88. }
  89. }
  90. static void idct(uint8_t *dst, int dst_linesize, int src[64])
  91. {
  92. int i, j, k;
  93. double tmp[64];
  94. for (i = 0; i < 8; i++) {
  95. for (j = 0; j < 8; j++) {
  96. double sum = 0.0;
  97. for (k = 0; k < 8; k++)
  98. sum += c[k*8+j] * src[8*i+k];
  99. tmp[8*i+j] = sum;
  100. }
  101. }
  102. for (j = 0; j < 8; j++) {
  103. for (i = 0; i < 8; i++) {
  104. double sum = 0.0;
  105. for (k = 0; k < 8; k++)
  106. sum += c[k*8+i]*tmp[8*k+j];
  107. dst[dst_linesize*i + j] = av_clip((int)floor(sum+0.5), 0, 255);
  108. }
  109. }
  110. }
  111. static void draw_dc(uint8_t *dst, int dst_linesize, int color, int w, int h)
  112. {
  113. int x, y;
  114. for (y = 0; y < h; y++)
  115. for (x = 0; x < w; x++)
  116. dst[x + y*dst_linesize] = color;
  117. }
  118. static void draw_basis(uint8_t *dst, int dst_linesize, int amp, int freq, int dc)
  119. {
  120. int src[64];
  121. memset(src, 0, 64*sizeof(int));
  122. src[0] = dc;
  123. if (amp)
  124. src[freq] = amp;
  125. idct(dst, dst_linesize, src);
  126. }
  127. static void draw_cbp(uint8_t *dst[3], int dst_linesize[3], int cbp, int amp, int dc)
  128. {
  129. if (cbp&1) draw_basis(dst[0] , dst_linesize[0], amp, 1, dc);
  130. if (cbp&2) draw_basis(dst[0]+8 , dst_linesize[0], amp, 1, dc);
  131. if (cbp&4) draw_basis(dst[0]+ 8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
  132. if (cbp&8) draw_basis(dst[0]+8+8*dst_linesize[0], dst_linesize[0], amp, 1, dc);
  133. if (cbp&16) draw_basis(dst[1] , dst_linesize[1], amp, 1, dc);
  134. if (cbp&32) draw_basis(dst[2] , dst_linesize[2], amp, 1, dc);
  135. }
  136. static void dc_test(uint8_t *dst, int dst_linesize, int w, int h, int off)
  137. {
  138. const int step = FFMAX(256/(w*h/256), 1);
  139. int x, y, color = off;
  140. for (y = 0; y < h; y += 16) {
  141. for (x = 0; x < w; x += 16) {
  142. draw_dc(dst + x + y*dst_linesize, dst_linesize, color, 8, 8);
  143. color += step;
  144. }
  145. }
  146. }
  147. static void freq_test(uint8_t *dst, int dst_linesize, int off)
  148. {
  149. int x, y, freq = 0;
  150. for (y = 0; y < 8*16; y += 16) {
  151. for (x = 0; x < 8*16; x += 16) {
  152. draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*(96+off), freq, 128*8);
  153. freq++;
  154. }
  155. }
  156. }
  157. static void amp_test(uint8_t *dst, int dst_linesize, int off)
  158. {
  159. int x, y, amp = off;
  160. for (y = 0; y < 16*16; y += 16) {
  161. for (x = 0; x < 16*16; x += 16) {
  162. draw_basis(dst + x + y*dst_linesize, dst_linesize, 4*amp, 1, 128*8);
  163. amp++;
  164. }
  165. }
  166. }
  167. static void cbp_test(uint8_t *dst[3], int dst_linesize[3], int off)
  168. {
  169. int x, y, cbp = 0;
  170. for (y = 0; y < 16*8; y += 16) {
  171. for (x = 0; x < 16*8; x += 16) {
  172. uint8_t *dst1[3];
  173. dst1[0] = dst[0] + x*2 + y*2*dst_linesize[0];
  174. dst1[1] = dst[1] + x + y* dst_linesize[1];
  175. dst1[2] = dst[2] + x + y* dst_linesize[2];
  176. draw_cbp(dst1, dst_linesize, cbp, (64+off)*4, 128*8);
  177. cbp++;
  178. }
  179. }
  180. }
  181. static void mv_test(uint8_t *dst, int dst_linesize, int off)
  182. {
  183. int x, y;
  184. for (y = 0; y < 16*16; y++) {
  185. if (y&16)
  186. continue;
  187. for (x = 0; x < 16*16; x++)
  188. dst[x + y*dst_linesize] = x + off*8/(y/32+1);
  189. }
  190. }
  191. static void ring1_test(uint8_t *dst, int dst_linesize, int off)
  192. {
  193. int x, y, color = 0;
  194. for (y = off; y < 16*16; y += 16) {
  195. for (x = off; x < 16*16; x += 16) {
  196. draw_dc(dst + x + y*dst_linesize, dst_linesize, ((x+y)&16) ? color : -color, 16, 16);
  197. color++;
  198. }
  199. }
  200. }
  201. static void ring2_test(uint8_t *dst, int dst_linesize, int off)
  202. {
  203. int x, y;
  204. for (y = 0; y < 16*16; y++) {
  205. for (x = 0; x < 16*16; x++) {
  206. double d = sqrt((x-8*16)*(x-8*16) + (y-8*16)*(y-8*16));
  207. double r = d/20 - (int)(d/20);
  208. if (r < off/30.0) {
  209. dst[x + y*dst_linesize] = 255;
  210. dst[x + y*dst_linesize+256] = 0;
  211. } else {
  212. dst[x + y*dst_linesize] = x;
  213. dst[x + y*dst_linesize+256] = x;
  214. }
  215. }
  216. }
  217. }
  218. static av_cold int init(AVFilterContext *ctx)
  219. {
  220. MPTestContext *test = ctx->priv;
  221. int64_t duration = -1;
  222. int ret;
  223. if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
  224. av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
  225. return ret;
  226. }
  227. test->max_pts = duration >= 0 ?
  228. av_rescale_q(duration, AV_TIME_BASE_Q, av_inv_q(test->frame_rate)) : -1;
  229. test->frame_nb = 0;
  230. test->pts = 0;
  231. av_log(ctx, AV_LOG_VERBOSE, "rate:%d/%d duration:%f\n",
  232. test->frame_rate.num, test->frame_rate.den,
  233. duration < 0 ? -1 : test->max_pts * av_q2d(av_inv_q(test->frame_rate)));
  234. init_idct();
  235. return 0;
  236. }
  237. static int config_props(AVFilterLink *outlink)
  238. {
  239. AVFilterContext *ctx = outlink->src;
  240. MPTestContext *test = ctx->priv;
  241. const AVPixFmtDescriptor *pix_desc = av_pix_fmt_desc_get(outlink->format);
  242. test->hsub = pix_desc->log2_chroma_w;
  243. test->vsub = pix_desc->log2_chroma_h;
  244. outlink->w = WIDTH;
  245. outlink->h = HEIGHT;
  246. outlink->time_base = av_inv_q(test->frame_rate);
  247. return 0;
  248. }
  249. static int query_formats(AVFilterContext *ctx)
  250. {
  251. static const enum AVPixelFormat pix_fmts[] = {
  252. AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE
  253. };
  254. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  255. return 0;
  256. }
  257. static int request_frame(AVFilterLink *outlink)
  258. {
  259. MPTestContext *test = outlink->src->priv;
  260. AVFrame *picref;
  261. int w = WIDTH, h = HEIGHT, ch = h>>test->vsub;
  262. unsigned int frame = test->frame_nb;
  263. enum test_type tt = test->test;
  264. if (test->max_pts >= 0 && test->pts > test->max_pts)
  265. return AVERROR_EOF;
  266. picref = ff_get_video_buffer(outlink, w, h);
  267. if (!picref)
  268. return AVERROR(ENOMEM);
  269. picref->pts = test->pts++;
  270. // clean image
  271. memset(picref->data[0], 0, picref->linesize[0] * h);
  272. memset(picref->data[1], 128, picref->linesize[1] * ch);
  273. memset(picref->data[2], 128, picref->linesize[2] * ch);
  274. if (tt == TEST_ALL && frame%30) /* draw a black frame at the beginning of each test */
  275. tt = (frame/30)%(TEST_NB-1);
  276. switch (tt) {
  277. case TEST_DC_LUMA: dc_test(picref->data[0], picref->linesize[0], 256, 256, frame%30); break;
  278. case TEST_DC_CHROMA: dc_test(picref->data[1], picref->linesize[1], 256, 256, frame%30); break;
  279. case TEST_FREQ_LUMA: freq_test(picref->data[0], picref->linesize[0], frame%30); break;
  280. case TEST_FREQ_CHROMA: freq_test(picref->data[1], picref->linesize[1], frame%30); break;
  281. case TEST_AMP_LUMA: amp_test(picref->data[0], picref->linesize[0], frame%30); break;
  282. case TEST_AMP_CHROMA: amp_test(picref->data[1], picref->linesize[1], frame%30); break;
  283. case TEST_CBP: cbp_test(picref->data , picref->linesize , frame%30); break;
  284. case TEST_MV: mv_test(picref->data[0], picref->linesize[0], frame%30); break;
  285. case TEST_RING1: ring1_test(picref->data[0], picref->linesize[0], frame%30); break;
  286. case TEST_RING2: ring2_test(picref->data[0], picref->linesize[0], frame%30); break;
  287. }
  288. test->frame_nb++;
  289. return ff_filter_frame(outlink, picref);
  290. }
  291. static const AVFilterPad mptestsrc_outputs[] = {
  292. {
  293. .name = "default",
  294. .type = AVMEDIA_TYPE_VIDEO,
  295. .request_frame = request_frame,
  296. .config_props = config_props,
  297. },
  298. { NULL }
  299. };
  300. AVFilter avfilter_vsrc_mptestsrc = {
  301. .name = "mptestsrc",
  302. .description = NULL_IF_CONFIG_SMALL("Generate various test pattern."),
  303. .priv_size = sizeof(MPTestContext),
  304. .init = init,
  305. .query_formats = query_formats,
  306. .inputs = NULL,
  307. .outputs = mptestsrc_outputs,
  308. .priv_class = &mptestsrc_class,
  309. };