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.

464 lines
12KB

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