PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/libavcodec/tests/dct.c

https://github.com/libav/libav
C | 473 lines | 373 code | 65 blank | 35 comment | 79 complexity | e77a2044a95e2f3b72826ea685372224 MD5 | raw file
  1. /*
  2. * (c) 2001 Fabrice Bellard
  3. * 2007 Marc Hoffman <marc.hoffman@analog.com>
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * Libav is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with Libav; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * DCT test (c) 2001 Fabrice Bellard
  24. * Started from sample code by Juan J. Sierralta P.
  25. */
  26. #include "config.h"
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #if HAVE_UNISTD_H
  31. #include <unistd.h>
  32. #endif
  33. #include <math.h>
  34. #include "libavutil/cpu.h"
  35. #include "libavutil/common.h"
  36. #include "libavutil/internal.h"
  37. #include "libavutil/lfg.h"
  38. #include "libavutil/time.h"
  39. #include "libavcodec/aandcttab.h"
  40. #include "libavcodec/dct.h"
  41. #include "libavcodec/dctref.h"
  42. #include "libavcodec/faandct.h"
  43. #include "libavcodec/faanidct.h"
  44. #include "libavcodec/idctdsp.h"
  45. #include "libavcodec/simple_idct.h"
  46. #include "libavcodec/xvididct.h"
  47. struct algo {
  48. const char *name;
  49. void (*func)(int16_t *block);
  50. enum idct_permutation_type perm_type;
  51. int cpu_flag;
  52. int nonspec;
  53. };
  54. static const struct algo fdct_tab[] = {
  55. { "REF-DBL", ff_ref_fdct, FF_IDCT_PERM_NONE },
  56. { "IJG-AAN-INT", ff_fdct_ifast, FF_IDCT_PERM_NONE },
  57. { "IJG-LLM-INT", ff_jpeg_fdct_islow_8, FF_IDCT_PERM_NONE },
  58. #if CONFIG_FAANDCT
  59. { "FAAN", ff_faandct, FF_IDCT_PERM_NONE },
  60. #endif /* CONFIG_FAANDCT */
  61. };
  62. static const struct algo idct_tab[] = {
  63. { "REF-DBL", ff_ref_idct, FF_IDCT_PERM_NONE },
  64. { "INT", ff_j_rev_dct, FF_IDCT_PERM_LIBMPEG2 },
  65. { "SIMPLE-C", ff_simple_idct_8, FF_IDCT_PERM_NONE },
  66. #if CONFIG_FAANIDCT
  67. { "FAANI", ff_faanidct, FF_IDCT_PERM_NONE },
  68. #endif /* CONFIG_FAANIDCT */
  69. #if CONFIG_MPEG4_DECODER
  70. { "XVID", ff_xvid_idct, FF_IDCT_PERM_NONE, 0, 1 },
  71. #endif /* CONFIG_MPEG4_DECODER */
  72. };
  73. #if ARCH_ARM
  74. #include "arm/dct.c"
  75. #elif ARCH_PPC
  76. #include "ppc/dct.c"
  77. #elif ARCH_X86
  78. #include "x86/dct.c"
  79. #else
  80. static const struct algo fdct_tab_arch[] = { { 0 } };
  81. static const struct algo idct_tab_arch[] = { { 0 } };
  82. #endif
  83. #define AANSCALE_BITS 12
  84. #define NB_ITS 20000
  85. #define NB_ITS_SPEED 50000
  86. DECLARE_ALIGNED(16, static int16_t, block)[64];
  87. DECLARE_ALIGNED(8, static int16_t, block1)[64];
  88. static void init_block(int16_t block[64], int test, int is_idct, AVLFG *prng)
  89. {
  90. int i, j;
  91. memset(block, 0, 64 * sizeof(*block));
  92. switch (test) {
  93. case 0:
  94. for (i = 0; i < 64; i++)
  95. block[i] = (av_lfg_get(prng) % 512) - 256;
  96. if (is_idct) {
  97. ff_ref_fdct(block);
  98. for (i = 0; i < 64; i++)
  99. block[i] >>= 3;
  100. }
  101. break;
  102. case 1:
  103. j = av_lfg_get(prng) % 10 + 1;
  104. for (i = 0; i < j; i++)
  105. block[av_lfg_get(prng) % 64] = av_lfg_get(prng) % 512 - 256;
  106. break;
  107. case 2:
  108. block[ 0] = av_lfg_get(prng) % 4096 - 2048;
  109. block[63] = (block[0] & 1) ^ 1;
  110. break;
  111. }
  112. }
  113. static void permute(int16_t dst[64], const int16_t src[64],
  114. enum idct_permutation_type perm_type)
  115. {
  116. int i;
  117. #if ARCH_X86
  118. if (permute_x86(dst, src, perm_type))
  119. return;
  120. #endif
  121. switch (perm_type) {
  122. case FF_IDCT_PERM_LIBMPEG2:
  123. for (i = 0; i < 64; i++)
  124. dst[(i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2)] = src[i];
  125. break;
  126. case FF_IDCT_PERM_PARTTRANS:
  127. for (i = 0; i < 64; i++)
  128. dst[(i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3)] = src[i];
  129. break;
  130. default:
  131. for (i = 0; i < 64; i++)
  132. dst[i] = src[i];
  133. break;
  134. }
  135. }
  136. static int dct_error(const struct algo *dct, int test, int is_idct, int speed)
  137. {
  138. void (*ref)(int16_t *block) = is_idct ? ff_ref_idct : ff_ref_fdct;
  139. int it, i, scale;
  140. int err_inf, v;
  141. int64_t err2, ti, ti1, it1, err_sum = 0;
  142. int64_t sysErr[64], sysErrMax = 0;
  143. int maxout = 0;
  144. int blockSumErrMax = 0, blockSumErr;
  145. AVLFG prng;
  146. double omse, ome;
  147. int spec_err;
  148. av_lfg_init(&prng, 1);
  149. err_inf = 0;
  150. err2 = 0;
  151. for (i = 0; i < 64; i++)
  152. sysErr[i] = 0;
  153. for (it = 0; it < NB_ITS; it++) {
  154. init_block(block1, test, is_idct, &prng);
  155. permute(block, block1, dct->perm_type);
  156. dct->func(block);
  157. emms_c();
  158. if (!strcmp(dct->name, "IJG-AAN-INT")) {
  159. for (i = 0; i < 64; i++) {
  160. scale = 8 * (1 << (AANSCALE_BITS + 11)) / ff_aanscales[i];
  161. block[i] = (block[i] * scale) >> AANSCALE_BITS;
  162. }
  163. }
  164. ref(block1);
  165. blockSumErr = 0;
  166. for (i = 0; i < 64; i++) {
  167. int err = block[i] - block1[i];
  168. err_sum += err;
  169. v = abs(err);
  170. if (v > err_inf)
  171. err_inf = v;
  172. err2 += v * v;
  173. sysErr[i] += block[i] - block1[i];
  174. blockSumErr += v;
  175. if (abs(block[i]) > maxout)
  176. maxout = abs(block[i]);
  177. }
  178. if (blockSumErrMax < blockSumErr)
  179. blockSumErrMax = blockSumErr;
  180. }
  181. for (i = 0; i < 64; i++)
  182. sysErrMax = FFMAX(sysErrMax, FFABS(sysErr[i]));
  183. for (i = 0; i < 64; i++) {
  184. if (i % 8 == 0)
  185. printf("\n");
  186. printf("%7d ", (int) sysErr[i]);
  187. }
  188. printf("\n");
  189. omse = (double) err2 / NB_ITS / 64;
  190. ome = (double) err_sum / NB_ITS / 64;
  191. spec_err = is_idct && (err_inf > 1 || omse > 0.02 || fabs(ome) > 0.0015);
  192. printf("%s %s: ppe=%d omse=%0.8f ome=%0.8f syserr=%0.8f maxout=%d blockSumErr=%d\n",
  193. is_idct ? "IDCT" : "DCT", dct->name, err_inf,
  194. omse, ome, (double) sysErrMax / NB_ITS,
  195. maxout, blockSumErrMax);
  196. if (spec_err && !dct->nonspec)
  197. return 1;
  198. if (!speed)
  199. return 0;
  200. /* speed test */
  201. init_block(block, test, is_idct, &prng);
  202. permute(block1, block, dct->perm_type);
  203. ti = av_gettime_relative();
  204. it1 = 0;
  205. do {
  206. for (it = 0; it < NB_ITS_SPEED; it++) {
  207. memcpy(block, block1, sizeof(block));
  208. dct->func(block);
  209. }
  210. it1 += NB_ITS_SPEED;
  211. ti1 = av_gettime_relative() - ti;
  212. } while (ti1 < 1000000);
  213. emms_c();
  214. printf("%s %s: %0.1f kdct/s\n", is_idct ? "IDCT" : "DCT", dct->name,
  215. (double) it1 * 1000.0 / (double) ti1);
  216. return 0;
  217. }
  218. DECLARE_ALIGNED(8, static uint8_t, img_dest)[64];
  219. DECLARE_ALIGNED(8, static uint8_t, img_dest1)[64];
  220. static void idct248_ref(uint8_t *dest, ptrdiff_t linesize, int16_t *block)
  221. {
  222. static int init;
  223. static double c8[8][8];
  224. static double c4[4][4];
  225. double block1[64], block2[64], block3[64];
  226. double s, sum, v;
  227. int i, j, k;
  228. if (!init) {
  229. init = 1;
  230. for (i = 0; i < 8; i++) {
  231. sum = 0;
  232. for (j = 0; j < 8; j++) {
  233. s = (i == 0) ? sqrt(1.0 / 8.0) : sqrt(1.0 / 4.0);
  234. c8[i][j] = s * cos(M_PI * i * (j + 0.5) / 8.0);
  235. sum += c8[i][j] * c8[i][j];
  236. }
  237. }
  238. for (i = 0; i < 4; i++) {
  239. sum = 0;
  240. for (j = 0; j < 4; j++) {
  241. s = (i == 0) ? sqrt(1.0 / 4.0) : sqrt(1.0 / 2.0);
  242. c4[i][j] = s * cos(M_PI * i * (j + 0.5) / 4.0);
  243. sum += c4[i][j] * c4[i][j];
  244. }
  245. }
  246. }
  247. /* butterfly */
  248. s = 0.5 * sqrt(2.0);
  249. for (i = 0; i < 4; i++) {
  250. for (j = 0; j < 8; j++) {
  251. block1[8 * (2 * i) + j] =
  252. (block[8 * (2 * i) + j] + block[8 * (2 * i + 1) + j]) * s;
  253. block1[8 * (2 * i + 1) + j] =
  254. (block[8 * (2 * i) + j] - block[8 * (2 * i + 1) + j]) * s;
  255. }
  256. }
  257. /* idct8 on lines */
  258. for (i = 0; i < 8; i++) {
  259. for (j = 0; j < 8; j++) {
  260. sum = 0;
  261. for (k = 0; k < 8; k++)
  262. sum += c8[k][j] * block1[8 * i + k];
  263. block2[8 * i + j] = sum;
  264. }
  265. }
  266. /* idct4 */
  267. for (i = 0; i < 8; i++) {
  268. for (j = 0; j < 4; j++) {
  269. /* top */
  270. sum = 0;
  271. for (k = 0; k < 4; k++)
  272. sum += c4[k][j] * block2[8 * (2 * k) + i];
  273. block3[8 * (2 * j) + i] = sum;
  274. /* bottom */
  275. sum = 0;
  276. for (k = 0; k < 4; k++)
  277. sum += c4[k][j] * block2[8 * (2 * k + 1) + i];
  278. block3[8 * (2 * j + 1) + i] = sum;
  279. }
  280. }
  281. /* clamp and store the result */
  282. for (i = 0; i < 8; i++) {
  283. for (j = 0; j < 8; j++) {
  284. v = block3[8 * i + j];
  285. if (v < 0) v = 0;
  286. else if (v > 255) v = 255;
  287. dest[i * linesize + j] = (int) rint(v);
  288. }
  289. }
  290. }
  291. static void idct248_error(const char *name,
  292. void (*idct248_put)(uint8_t *dest,
  293. ptrdiff_t line_size,
  294. int16_t *block),
  295. int speed)
  296. {
  297. int it, i, it1, ti, ti1, err_max, v;
  298. AVLFG prng;
  299. av_lfg_init(&prng, 1);
  300. /* just one test to see if code is correct (precision is less
  301. important here) */
  302. err_max = 0;
  303. for (it = 0; it < NB_ITS; it++) {
  304. /* XXX: use forward transform to generate values */
  305. for (i = 0; i < 64; i++)
  306. block1[i] = av_lfg_get(&prng) % 256 - 128;
  307. block1[0] += 1024;
  308. for (i = 0; i < 64; i++)
  309. block[i] = block1[i];
  310. idct248_ref(img_dest1, 8, block);
  311. for (i = 0; i < 64; i++)
  312. block[i] = block1[i];
  313. idct248_put(img_dest, 8, block);
  314. for (i = 0; i < 64; i++) {
  315. v = abs((int) img_dest[i] - (int) img_dest1[i]);
  316. if (v == 255)
  317. printf("%d %d\n", img_dest[i], img_dest1[i]);
  318. if (v > err_max)
  319. err_max = v;
  320. }
  321. }
  322. printf("%s %s: err_inf=%d\n", 1 ? "IDCT248" : "DCT248", name, err_max);
  323. if (!speed)
  324. return;
  325. ti = av_gettime_relative();
  326. it1 = 0;
  327. do {
  328. for (it = 0; it < NB_ITS_SPEED; it++) {
  329. for (i = 0; i < 64; i++)
  330. block[i] = block1[i];
  331. idct248_put(img_dest, 8, block);
  332. }
  333. it1 += NB_ITS_SPEED;
  334. ti1 = av_gettime_relative() - ti;
  335. } while (ti1 < 1000000);
  336. emms_c();
  337. printf("%s %s: %0.1f kdct/s\n", 1 ? "IDCT248" : "DCT248", name,
  338. (double) it1 * 1000.0 / (double) ti1);
  339. }
  340. static void help(void)
  341. {
  342. printf("dct-test [-i] [<test-number>]\n"
  343. "test-number 0 -> test with random matrixes\n"
  344. " 1 -> test with random sparse matrixes\n"
  345. " 2 -> do 3. test from MPEG-4 std\n"
  346. "-i test IDCT implementations\n"
  347. "-4 test IDCT248 implementations\n"
  348. "-t speed test\n");
  349. }
  350. #if !HAVE_GETOPT
  351. #include "compat/getopt.c"
  352. #endif
  353. int main(int argc, char **argv)
  354. {
  355. int test_idct = 0, test_248_dct = 0;
  356. int c, i;
  357. int test = 1;
  358. int speed = 0;
  359. int err = 0;
  360. ff_ref_dct_init();
  361. for (;;) {
  362. c = getopt(argc, argv, "ih4t");
  363. if (c == -1)
  364. break;
  365. switch (c) {
  366. case 'i':
  367. test_idct = 1;
  368. break;
  369. case '4':
  370. test_248_dct = 1;
  371. break;
  372. case 't':
  373. speed = 1;
  374. break;
  375. default:
  376. case 'h':
  377. help();
  378. return 0;
  379. }
  380. }
  381. if (optind < argc)
  382. test = atoi(argv[optind]);
  383. printf("Libav DCT/IDCT test\n");
  384. if (test_248_dct) {
  385. idct248_error("SIMPLE-C", ff_simple_idct248_put, speed);
  386. } else {
  387. const int cpu_flags = av_get_cpu_flags();
  388. if (test_idct) {
  389. for (i = 0; i < FF_ARRAY_ELEMS(idct_tab); i++)
  390. err |= dct_error(&idct_tab[i], test, test_idct, speed);
  391. for (i = 0; idct_tab_arch[i].name; i++)
  392. if (!(~cpu_flags & idct_tab_arch[i].cpu_flag))
  393. err |= dct_error(&idct_tab_arch[i], test, test_idct, speed);
  394. }
  395. #if CONFIG_FDCTDSP
  396. else {
  397. for (i = 0; i < FF_ARRAY_ELEMS(fdct_tab); i++)
  398. err |= dct_error(&fdct_tab[i], test, test_idct, speed);
  399. for (i = 0; fdct_tab_arch[i].name; i++)
  400. if (!(~cpu_flags & fdct_tab_arch[i].cpu_flag))
  401. err |= dct_error(&fdct_tab_arch[i], test, test_idct, speed);
  402. }
  403. #endif /* CONFIG_FDCTDSP */
  404. }
  405. if (err)
  406. printf("Error: %d.\n", err);
  407. return !!err;
  408. }