vsrc_mptestsrc.c 11 KB

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