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.

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