vsrc_mptestsrc.c 12 KB

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