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.

468 lines
13KB

  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/lfg.h"
  37. #include "libavutil/time.h"
  38. #include "dct.h"
  39. #include "idctdsp.h"
  40. #include "simple_idct.h"
  41. #include "xvididct.h"
  42. #include "aandcttab.h"
  43. #include "faandct.h"
  44. #include "faanidct.h"
  45. #include "dctref.h"
  46. struct algo {
  47. const char *name;
  48. void (*func)(int16_t *block);
  49. enum idct_permutation_type perm_type;
  50. int cpu_flag;
  51. int nonspec;
  52. };
  53. static const struct algo fdct_tab[4] = {
  54. { "REF-DBL", ff_ref_fdct, FF_IDCT_PERM_NONE },
  55. { "FAAN", ff_faandct, 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. };
  59. static const struct algo idct_tab[] = {
  60. { "FAANI", ff_faanidct, FF_IDCT_PERM_NONE },
  61. { "REF-DBL", ff_ref_idct, FF_IDCT_PERM_NONE },
  62. { "INT", ff_j_rev_dct, FF_IDCT_PERM_LIBMPEG2 },
  63. { "SIMPLE-C", ff_simple_idct_8, FF_IDCT_PERM_NONE },
  64. #if CONFIG_MPEG4_DECODER
  65. { "XVID", ff_xvid_idct, FF_IDCT_PERM_NONE, 0, 1 },
  66. #endif /* CONFIG_MPEG4_DECODER */
  67. };
  68. #if ARCH_ARM
  69. #include "arm/dct-test.c"
  70. #elif ARCH_PPC
  71. #include "ppc/dct-test.c"
  72. #elif ARCH_X86
  73. #include "x86/dct-test.c"
  74. #else
  75. static const struct algo fdct_tab_arch[] = { 0 };
  76. static const struct algo idct_tab_arch[] = { 0 };
  77. #endif
  78. #define AANSCALE_BITS 12
  79. #define NB_ITS 20000
  80. #define NB_ITS_SPEED 50000
  81. DECLARE_ALIGNED(16, static int16_t, block)[64];
  82. DECLARE_ALIGNED(8, static int16_t, block1)[64];
  83. static void init_block(int16_t block[64], int test, int is_idct, AVLFG *prng)
  84. {
  85. int i, j;
  86. memset(block, 0, 64 * sizeof(*block));
  87. switch (test) {
  88. case 0:
  89. for (i = 0; i < 64; i++)
  90. block[i] = (av_lfg_get(prng) % 512) - 256;
  91. if (is_idct) {
  92. ff_ref_fdct(block);
  93. for (i = 0; i < 64; i++)
  94. block[i] >>= 3;
  95. }
  96. break;
  97. case 1:
  98. j = av_lfg_get(prng) % 10 + 1;
  99. for (i = 0; i < j; i++)
  100. block[av_lfg_get(prng) % 64] = av_lfg_get(prng) % 512 - 256;
  101. break;
  102. case 2:
  103. block[ 0] = av_lfg_get(prng) % 4096 - 2048;
  104. block[63] = (block[0] & 1) ^ 1;
  105. break;
  106. }
  107. }
  108. static void permute(int16_t dst[64], const int16_t src[64],
  109. enum idct_permutation_type perm_type)
  110. {
  111. int i;
  112. #if ARCH_X86
  113. if (permute_x86(dst, src, perm_type))
  114. return;
  115. #endif
  116. switch (perm_type) {
  117. case FF_IDCT_PERM_LIBMPEG2:
  118. for (i = 0; i < 64; i++)
  119. dst[(i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2)] = src[i];
  120. break;
  121. case FF_IDCT_PERM_PARTTRANS:
  122. for (i = 0; i < 64; i++)
  123. dst[(i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3)] = src[i];
  124. break;
  125. default:
  126. for (i = 0; i < 64; i++)
  127. dst[i] = src[i];
  128. break;
  129. }
  130. }
  131. static int dct_error(const struct algo *dct, int test, int is_idct, int speed)
  132. {
  133. void (*ref)(int16_t *block) = is_idct ? ff_ref_idct : ff_ref_fdct;
  134. int it, i, scale;
  135. int err_inf, v;
  136. int64_t err2, ti, ti1, it1, err_sum = 0;
  137. int64_t sysErr[64], sysErrMax = 0;
  138. int maxout = 0;
  139. int blockSumErrMax = 0, blockSumErr;
  140. AVLFG prng;
  141. double omse, ome;
  142. int spec_err;
  143. av_lfg_init(&prng, 1);
  144. err_inf = 0;
  145. err2 = 0;
  146. for (i = 0; i < 64; i++)
  147. sysErr[i] = 0;
  148. for (it = 0; it < NB_ITS; it++) {
  149. init_block(block1, test, is_idct, &prng);
  150. permute(block, block1, dct->perm_type);
  151. dct->func(block);
  152. emms_c();
  153. if (!strcmp(dct->name, "IJG-AAN-INT")) {
  154. for (i = 0; i < 64; i++) {
  155. scale = 8 * (1 << (AANSCALE_BITS + 11)) / ff_aanscales[i];
  156. block[i] = (block[i] * scale) >> AANSCALE_BITS;
  157. }
  158. }
  159. ref(block1);
  160. blockSumErr = 0;
  161. for (i = 0; i < 64; i++) {
  162. int err = block[i] - block1[i];
  163. err_sum += err;
  164. v = abs(err);
  165. if (v > err_inf)
  166. err_inf = v;
  167. err2 += v * v;
  168. sysErr[i] += block[i] - block1[i];
  169. blockSumErr += v;
  170. if (abs(block[i]) > maxout)
  171. maxout = abs(block[i]);
  172. }
  173. if (blockSumErrMax < blockSumErr)
  174. blockSumErrMax = blockSumErr;
  175. }
  176. for (i = 0; i < 64; i++)
  177. sysErrMax = FFMAX(sysErrMax, FFABS(sysErr[i]));
  178. for (i = 0; i < 64; i++) {
  179. if (i % 8 == 0)
  180. printf("\n");
  181. printf("%7d ", (int) sysErr[i]);
  182. }
  183. printf("\n");
  184. omse = (double) err2 / NB_ITS / 64;
  185. ome = (double) err_sum / NB_ITS / 64;
  186. spec_err = is_idct && (err_inf > 1 || omse > 0.02 || fabs(ome) > 0.0015);
  187. printf("%s %s: ppe=%d omse=%0.8f ome=%0.8f syserr=%0.8f maxout=%d blockSumErr=%d\n",
  188. is_idct ? "IDCT" : "DCT", dct->name, err_inf,
  189. omse, ome, (double) sysErrMax / NB_ITS,
  190. maxout, blockSumErrMax);
  191. if (spec_err && !dct->nonspec)
  192. return 1;
  193. if (!speed)
  194. return 0;
  195. /* speed test */
  196. init_block(block, test, is_idct, &prng);
  197. permute(block1, block, dct->perm_type);
  198. ti = av_gettime();
  199. it1 = 0;
  200. do {
  201. for (it = 0; it < NB_ITS_SPEED; it++) {
  202. memcpy(block, block1, sizeof(block));
  203. dct->func(block);
  204. }
  205. it1 += NB_ITS_SPEED;
  206. ti1 = av_gettime() - ti;
  207. } while (ti1 < 1000000);
  208. emms_c();
  209. printf("%s %s: %0.1f kdct/s\n", is_idct ? "IDCT" : "DCT", dct->name,
  210. (double) it1 * 1000.0 / (double) ti1);
  211. return 0;
  212. }
  213. DECLARE_ALIGNED(8, static uint8_t, img_dest)[64];
  214. DECLARE_ALIGNED(8, static uint8_t, img_dest1)[64];
  215. static void idct248_ref(uint8_t *dest, int linesize, int16_t *block)
  216. {
  217. static int init;
  218. static double c8[8][8];
  219. static double c4[4][4];
  220. double block1[64], block2[64], block3[64];
  221. double s, sum, v;
  222. int i, j, k;
  223. if (!init) {
  224. init = 1;
  225. for (i = 0; i < 8; i++) {
  226. sum = 0;
  227. for (j = 0; j < 8; j++) {
  228. s = (i == 0) ? sqrt(1.0 / 8.0) : sqrt(1.0 / 4.0);
  229. c8[i][j] = s * cos(M_PI * i * (j + 0.5) / 8.0);
  230. sum += c8[i][j] * c8[i][j];
  231. }
  232. }
  233. for (i = 0; i < 4; i++) {
  234. sum = 0;
  235. for (j = 0; j < 4; j++) {
  236. s = (i == 0) ? sqrt(1.0 / 4.0) : sqrt(1.0 / 2.0);
  237. c4[i][j] = s * cos(M_PI * i * (j + 0.5) / 4.0);
  238. sum += c4[i][j] * c4[i][j];
  239. }
  240. }
  241. }
  242. /* butterfly */
  243. s = 0.5 * sqrt(2.0);
  244. for (i = 0; i < 4; i++) {
  245. for (j = 0; j < 8; j++) {
  246. block1[8 * (2 * i) + j] =
  247. (block[8 * (2 * i) + j] + block[8 * (2 * i + 1) + j]) * s;
  248. block1[8 * (2 * i + 1) + j] =
  249. (block[8 * (2 * i) + j] - block[8 * (2 * i + 1) + j]) * s;
  250. }
  251. }
  252. /* idct8 on lines */
  253. for (i = 0; i < 8; i++) {
  254. for (j = 0; j < 8; j++) {
  255. sum = 0;
  256. for (k = 0; k < 8; k++)
  257. sum += c8[k][j] * block1[8 * i + k];
  258. block2[8 * i + j] = sum;
  259. }
  260. }
  261. /* idct4 */
  262. for (i = 0; i < 8; i++) {
  263. for (j = 0; j < 4; j++) {
  264. /* top */
  265. sum = 0;
  266. for (k = 0; k < 4; k++)
  267. sum += c4[k][j] * block2[8 * (2 * k) + i];
  268. block3[8 * (2 * j) + i] = sum;
  269. /* bottom */
  270. sum = 0;
  271. for (k = 0; k < 4; k++)
  272. sum += c4[k][j] * block2[8 * (2 * k + 1) + i];
  273. block3[8 * (2 * j + 1) + i] = sum;
  274. }
  275. }
  276. /* clamp and store the result */
  277. for (i = 0; i < 8; i++) {
  278. for (j = 0; j < 8; j++) {
  279. v = block3[8 * i + j];
  280. if (v < 0) v = 0;
  281. else if (v > 255) v = 255;
  282. dest[i * linesize + j] = (int) rint(v);
  283. }
  284. }
  285. }
  286. static void idct248_error(const char *name,
  287. void (*idct248_put)(uint8_t *dest, int line_size,
  288. int16_t *block),
  289. int speed)
  290. {
  291. int it, i, it1, ti, ti1, err_max, v;
  292. AVLFG prng;
  293. av_lfg_init(&prng, 1);
  294. /* just one test to see if code is correct (precision is less
  295. important here) */
  296. err_max = 0;
  297. for (it = 0; it < NB_ITS; it++) {
  298. /* XXX: use forward transform to generate values */
  299. for (i = 0; i < 64; i++)
  300. block1[i] = av_lfg_get(&prng) % 256 - 128;
  301. block1[0] += 1024;
  302. for (i = 0; i < 64; i++)
  303. block[i] = block1[i];
  304. idct248_ref(img_dest1, 8, block);
  305. for (i = 0; i < 64; i++)
  306. block[i] = block1[i];
  307. idct248_put(img_dest, 8, block);
  308. for (i = 0; i < 64; i++) {
  309. v = abs((int) img_dest[i] - (int) img_dest1[i]);
  310. if (v == 255)
  311. printf("%d %d\n", img_dest[i], img_dest1[i]);
  312. if (v > err_max)
  313. err_max = v;
  314. }
  315. }
  316. printf("%s %s: err_inf=%d\n", 1 ? "IDCT248" : "DCT248", name, err_max);
  317. if (!speed)
  318. return;
  319. ti = av_gettime();
  320. it1 = 0;
  321. do {
  322. for (it = 0; it < NB_ITS_SPEED; it++) {
  323. for (i = 0; i < 64; i++)
  324. block[i] = block1[i];
  325. idct248_put(img_dest, 8, block);
  326. }
  327. it1 += NB_ITS_SPEED;
  328. ti1 = av_gettime() - ti;
  329. } while (ti1 < 1000000);
  330. emms_c();
  331. printf("%s %s: %0.1f kdct/s\n", 1 ? "IDCT248" : "DCT248", name,
  332. (double) it1 * 1000.0 / (double) ti1);
  333. }
  334. static void help(void)
  335. {
  336. printf("dct-test [-i] [<test-number>]\n"
  337. "test-number 0 -> test with random matrixes\n"
  338. " 1 -> test with random sparse matrixes\n"
  339. " 2 -> do 3. test from mpeg4 std\n"
  340. "-i test IDCT implementations\n"
  341. "-4 test IDCT248 implementations\n"
  342. "-t speed test\n");
  343. }
  344. #if !HAVE_GETOPT
  345. #include "compat/getopt.c"
  346. #endif
  347. int main(int argc, char **argv)
  348. {
  349. int test_idct = 0, test_248_dct = 0;
  350. int c, i;
  351. int test = 1;
  352. int speed = 0;
  353. int err = 0;
  354. ff_ref_dct_init();
  355. for (;;) {
  356. c = getopt(argc, argv, "ih4t");
  357. if (c == -1)
  358. break;
  359. switch (c) {
  360. case 'i':
  361. test_idct = 1;
  362. break;
  363. case '4':
  364. test_248_dct = 1;
  365. break;
  366. case 't':
  367. speed = 1;
  368. break;
  369. default:
  370. case 'h':
  371. help();
  372. return 0;
  373. }
  374. }
  375. if (optind < argc)
  376. test = atoi(argv[optind]);
  377. printf("Libav DCT/IDCT test\n");
  378. if (test_248_dct) {
  379. idct248_error("SIMPLE-C", ff_simple_idct248_put, speed);
  380. } else {
  381. const int cpu_flags = av_get_cpu_flags();
  382. if (test_idct) {
  383. for (i = 0; i < FF_ARRAY_ELEMS(idct_tab); i++)
  384. err |= dct_error(&idct_tab[i], test, test_idct, speed);
  385. for (i = 0; idct_tab_arch[i].name; i++)
  386. if (!(~cpu_flags & idct_tab_arch[i].cpu_flag))
  387. err |= dct_error(&idct_tab_arch[i], test, test_idct, speed);
  388. }
  389. #if CONFIG_FDCTDSP
  390. else {
  391. for (i = 0; i < FF_ARRAY_ELEMS(fdct_tab); i++)
  392. err |= dct_error(&fdct_tab[i], test, test_idct, speed);
  393. for (i = 0; fdct_tab_arch[i].name; i++)
  394. if (!(~cpu_flags & fdct_tab_arch[i].cpu_flag))
  395. err |= dct_error(&fdct_tab_arch[i], test, test_idct, speed);
  396. }
  397. #endif /* CONFIG_FDCTDSP */
  398. }
  399. if (err)
  400. printf("Error: %d.\n", err);
  401. return !!err;
  402. }