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.

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