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.

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