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.

519 lines
14KB

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