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.

585 lines
16KB

  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 "simple_idct.h"
  40. #include "aandcttab.h"
  41. #include "faandct.h"
  42. #include "faanidct.h"
  43. #include "x86/idct_xvid.h"
  44. #include "dctref.h"
  45. #undef printf
  46. // BFIN
  47. void ff_bfin_idct(int16_t *block);
  48. void ff_bfin_fdct(int16_t *block);
  49. // ALTIVEC
  50. void ff_fdct_altivec(int16_t *block);
  51. // ARM
  52. void ff_j_rev_dct_arm(int16_t *data);
  53. void ff_simple_idct_arm(int16_t *data);
  54. void ff_simple_idct_armv5te(int16_t *data);
  55. void ff_simple_idct_armv6(int16_t *data);
  56. void ff_simple_idct_neon(int16_t *data);
  57. void ff_simple_idct_axp(int16_t *data);
  58. struct algo {
  59. const char *name;
  60. void (*func)(int16_t *block);
  61. enum formattag { NO_PERM, MMX_PERM, MMX_SIMPLE_PERM, SCALE_PERM,
  62. SSE2_PERM, PARTTRANS_PERM, TRANSPOSE_PERM } format;
  63. int mm_support;
  64. int nonspec;
  65. };
  66. static int cpu_flags;
  67. static const struct algo fdct_tab[] = {
  68. { "REF-DBL", ff_ref_fdct, NO_PERM },
  69. { "FAAN", ff_faandct, NO_PERM },
  70. { "IJG-AAN-INT", ff_fdct_ifast, SCALE_PERM },
  71. { "IJG-LLM-INT", ff_jpeg_fdct_islow_8, NO_PERM },
  72. #if HAVE_MMX_INLINE
  73. { "MMX", ff_fdct_mmx, NO_PERM, AV_CPU_FLAG_MMX },
  74. #endif
  75. #if HAVE_MMXEXT_INLINE
  76. { "MMXEXT", ff_fdct_mmxext, NO_PERM, AV_CPU_FLAG_MMXEXT },
  77. #endif
  78. #if HAVE_SSE2_INLINE
  79. { "SSE2", ff_fdct_sse2, NO_PERM, AV_CPU_FLAG_SSE2 },
  80. #endif
  81. #if HAVE_ALTIVEC
  82. { "altivecfdct", ff_fdct_altivec, NO_PERM, AV_CPU_FLAG_ALTIVEC },
  83. #endif
  84. #if ARCH_BFIN
  85. { "BFINfdct", ff_bfin_fdct, NO_PERM },
  86. #endif
  87. { 0 }
  88. };
  89. #if ARCH_X86_64 && HAVE_MMX && HAVE_YASM
  90. void ff_prores_idct_put_10_sse2(uint16_t *dst, int linesize,
  91. int16_t *block, int16_t *qmat);
  92. static void ff_prores_idct_put_10_sse2_wrap(int16_t *dst){
  93. DECLARE_ALIGNED(16, static int16_t, qmat)[64];
  94. DECLARE_ALIGNED(16, static int16_t, tmp)[64];
  95. int i;
  96. for(i=0; i<64; i++){
  97. qmat[i]=4;
  98. tmp[i]= dst[i];
  99. }
  100. ff_prores_idct_put_10_sse2(dst, 16, tmp, qmat);
  101. }
  102. #endif
  103. static const struct algo idct_tab[] = {
  104. { "FAANI", ff_faanidct, NO_PERM },
  105. { "REF-DBL", ff_ref_idct, NO_PERM },
  106. { "INT", ff_j_rev_dct, MMX_PERM },
  107. { "SIMPLE-C", ff_simple_idct_8, NO_PERM },
  108. #if HAVE_MMX_INLINE
  109. { "SIMPLE-MMX", ff_simple_idct_mmx, MMX_SIMPLE_PERM, AV_CPU_FLAG_MMX },
  110. { "XVID-MMX", ff_idct_xvid_mmx, NO_PERM, AV_CPU_FLAG_MMX, 1 },
  111. #endif
  112. #if HAVE_MMXEXT_INLINE
  113. { "XVID-MMXEXT", ff_idct_xvid_mmxext, NO_PERM, AV_CPU_FLAG_MMXEXT, 1 },
  114. #endif
  115. #if HAVE_SSE2_INLINE
  116. { "XVID-SSE2", ff_idct_xvid_sse2, SSE2_PERM, AV_CPU_FLAG_SSE2, 1 },
  117. #if ARCH_X86_64 && HAVE_YASM
  118. { "PR-SSE2", ff_prores_idct_put_10_sse2_wrap, TRANSPOSE_PERM, AV_CPU_FLAG_SSE2, 1 },
  119. #endif
  120. #endif
  121. #if ARCH_BFIN
  122. { "BFINidct", ff_bfin_idct, NO_PERM },
  123. #endif
  124. #if ARCH_ARM
  125. { "SIMPLE-ARM", ff_simple_idct_arm, NO_PERM },
  126. { "INT-ARM", ff_j_rev_dct_arm, MMX_PERM },
  127. #endif
  128. #if HAVE_ARMV5TE
  129. { "SIMPLE-ARMV5TE", ff_simple_idct_armv5te,NO_PERM, AV_CPU_FLAG_ARMV5TE },
  130. #endif
  131. #if HAVE_ARMV6
  132. { "SIMPLE-ARMV6", ff_simple_idct_armv6, MMX_PERM, AV_CPU_FLAG_ARMV6 },
  133. #endif
  134. #if HAVE_NEON
  135. { "SIMPLE-NEON", ff_simple_idct_neon, PARTTRANS_PERM, AV_CPU_FLAG_NEON },
  136. #endif
  137. #if ARCH_ALPHA
  138. { "SIMPLE-ALPHA", ff_simple_idct_axp, NO_PERM },
  139. #endif
  140. { 0 }
  141. };
  142. #define AANSCALE_BITS 12
  143. #define NB_ITS 20000
  144. #define NB_ITS_SPEED 50000
  145. static short idct_mmx_perm[64];
  146. static short idct_simple_mmx_perm[64] = {
  147. 0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D,
  148. 0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D,
  149. 0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D,
  150. 0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F,
  151. 0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F,
  152. 0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D,
  153. 0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F,
  154. 0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
  155. };
  156. static const uint8_t idct_sse2_row_perm[8] = { 0, 4, 1, 5, 2, 6, 3, 7 };
  157. static void idct_mmx_init(void)
  158. {
  159. int i;
  160. /* the mmx/mmxext idct uses a reordered input, so we patch scan tables */
  161. for (i = 0; i < 64; i++) {
  162. idct_mmx_perm[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
  163. }
  164. }
  165. DECLARE_ALIGNED(16, static int16_t, block)[64];
  166. DECLARE_ALIGNED(8, static int16_t, block1)[64];
  167. static void init_block(int16_t block[64], int test, int is_idct, AVLFG *prng, int vals)
  168. {
  169. int i, j;
  170. memset(block, 0, 64 * sizeof(*block));
  171. switch (test) {
  172. case 0:
  173. for (i = 0; i < 64; i++)
  174. block[i] = (av_lfg_get(prng) % (2*vals)) -vals;
  175. if (is_idct) {
  176. ff_ref_fdct(block);
  177. for (i = 0; i < 64; i++)
  178. block[i] >>= 3;
  179. }
  180. break;
  181. case 1:
  182. j = av_lfg_get(prng) % 10 + 1;
  183. for (i = 0; i < j; i++) {
  184. int idx = av_lfg_get(prng) % 64;
  185. block[idx] = av_lfg_get(prng) % (2*vals) -vals;
  186. }
  187. break;
  188. case 2:
  189. block[ 0] = av_lfg_get(prng) % (16*vals) - (8*vals);
  190. block[63] = (block[0] & 1) ^ 1;
  191. break;
  192. }
  193. }
  194. static void permute(int16_t dst[64], const int16_t src[64], int perm)
  195. {
  196. int i;
  197. if (perm == MMX_PERM) {
  198. for (i = 0; i < 64; i++)
  199. dst[idct_mmx_perm[i]] = src[i];
  200. } else if (perm == MMX_SIMPLE_PERM) {
  201. for (i = 0; i < 64; i++)
  202. dst[idct_simple_mmx_perm[i]] = src[i];
  203. } else if (perm == SSE2_PERM) {
  204. for (i = 0; i < 64; i++)
  205. dst[(i & 0x38) | idct_sse2_row_perm[i & 7]] = src[i];
  206. } else if (perm == PARTTRANS_PERM) {
  207. for (i = 0; i < 64; i++)
  208. dst[(i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3)] = src[i];
  209. } else if (perm == TRANSPOSE_PERM) {
  210. for (i = 0; i < 64; i++)
  211. dst[(i>>3) | ((i<<3)&0x38)] = src[i];
  212. } else {
  213. for (i = 0; i < 64; i++)
  214. dst[i] = src[i];
  215. }
  216. }
  217. static int dct_error(const struct algo *dct, int test, int is_idct, int speed, const int bits)
  218. {
  219. void (*ref)(int16_t *block) = is_idct ? ff_ref_idct : ff_ref_fdct;
  220. int it, i, scale;
  221. int err_inf, v;
  222. int64_t err2, ti, ti1, it1, err_sum = 0;
  223. int64_t sysErr[64], sysErrMax = 0;
  224. int maxout = 0;
  225. int blockSumErrMax = 0, blockSumErr;
  226. AVLFG prng;
  227. const int vals=1<<bits;
  228. double omse, ome;
  229. int spec_err;
  230. av_lfg_init(&prng, 1);
  231. err_inf = 0;
  232. err2 = 0;
  233. for (i = 0; i < 64; i++)
  234. sysErr[i] = 0;
  235. for (it = 0; it < NB_ITS; it++) {
  236. init_block(block1, test, is_idct, &prng, vals);
  237. permute(block, block1, dct->format);
  238. dct->func(block);
  239. emms_c();
  240. if (dct->format == SCALE_PERM) {
  241. for (i = 0; i < 64; i++) {
  242. scale = 8 * (1 << (AANSCALE_BITS + 11)) / ff_aanscales[i];
  243. block[i] = (block[i] * scale) >> AANSCALE_BITS;
  244. }
  245. }
  246. ref(block1);
  247. blockSumErr = 0;
  248. for (i = 0; i < 64; i++) {
  249. int err = block[i] - block1[i];
  250. err_sum += err;
  251. v = abs(err);
  252. if (v > err_inf)
  253. err_inf = v;
  254. err2 += v * v;
  255. sysErr[i] += block[i] - block1[i];
  256. blockSumErr += v;
  257. if (abs(block[i]) > maxout)
  258. maxout = abs(block[i]);
  259. }
  260. if (blockSumErrMax < blockSumErr)
  261. blockSumErrMax = blockSumErr;
  262. }
  263. for (i = 0; i < 64; i++)
  264. sysErrMax = FFMAX(sysErrMax, FFABS(sysErr[i]));
  265. for (i = 0; i < 64; i++) {
  266. if (i % 8 == 0)
  267. printf("\n");
  268. printf("%7d ", (int) sysErr[i]);
  269. }
  270. printf("\n");
  271. omse = (double) err2 / NB_ITS / 64;
  272. ome = (double) err_sum / NB_ITS / 64;
  273. spec_err = is_idct && (err_inf > 1 || omse > 0.02 || fabs(ome) > 0.0015);
  274. printf("%s %s: max_err=%d omse=%0.8f ome=%0.8f syserr=%0.8f maxout=%d blockSumErr=%d\n",
  275. is_idct ? "IDCT" : "DCT", dct->name, err_inf,
  276. omse, ome, (double) sysErrMax / NB_ITS,
  277. maxout, blockSumErrMax);
  278. if (spec_err && !dct->nonspec)
  279. return 1;
  280. if (!speed)
  281. return 0;
  282. /* speed test */
  283. init_block(block, test, is_idct, &prng, vals);
  284. permute(block1, block, dct->format);
  285. ti = av_gettime();
  286. it1 = 0;
  287. do {
  288. for (it = 0; it < NB_ITS_SPEED; it++) {
  289. memcpy(block, block1, sizeof(block));
  290. dct->func(block);
  291. }
  292. emms_c();
  293. it1 += NB_ITS_SPEED;
  294. ti1 = av_gettime() - ti;
  295. } while (ti1 < 1000000);
  296. printf("%s %s: %0.1f kdct/s\n", is_idct ? "IDCT" : "DCT", dct->name,
  297. (double) it1 * 1000.0 / (double) ti1);
  298. return 0;
  299. }
  300. DECLARE_ALIGNED(8, static uint8_t, img_dest)[64];
  301. DECLARE_ALIGNED(8, static uint8_t, img_dest1)[64];
  302. static void idct248_ref(uint8_t *dest, int linesize, int16_t *block)
  303. {
  304. static int init;
  305. static double c8[8][8];
  306. static double c4[4][4];
  307. double block1[64], block2[64], block3[64];
  308. double s, sum, v;
  309. int i, j, k;
  310. if (!init) {
  311. init = 1;
  312. for (i = 0; i < 8; i++) {
  313. sum = 0;
  314. for (j = 0; j < 8; j++) {
  315. s = (i == 0) ? sqrt(1.0 / 8.0) : sqrt(1.0 / 4.0);
  316. c8[i][j] = s * cos(M_PI * i * (j + 0.5) / 8.0);
  317. sum += c8[i][j] * c8[i][j];
  318. }
  319. }
  320. for (i = 0; i < 4; i++) {
  321. sum = 0;
  322. for (j = 0; j < 4; j++) {
  323. s = (i == 0) ? sqrt(1.0 / 4.0) : sqrt(1.0 / 2.0);
  324. c4[i][j] = s * cos(M_PI * i * (j + 0.5) / 4.0);
  325. sum += c4[i][j] * c4[i][j];
  326. }
  327. }
  328. }
  329. /* butterfly */
  330. s = 0.5 * sqrt(2.0);
  331. for (i = 0; i < 4; i++) {
  332. for (j = 0; j < 8; j++) {
  333. block1[8 * (2 * i) + j] =
  334. (block[8 * (2 * i) + j] + block[8 * (2 * i + 1) + j]) * s;
  335. block1[8 * (2 * i + 1) + j] =
  336. (block[8 * (2 * i) + j] - block[8 * (2 * i + 1) + j]) * s;
  337. }
  338. }
  339. /* idct8 on lines */
  340. for (i = 0; i < 8; i++) {
  341. for (j = 0; j < 8; j++) {
  342. sum = 0;
  343. for (k = 0; k < 8; k++)
  344. sum += c8[k][j] * block1[8 * i + k];
  345. block2[8 * i + j] = sum;
  346. }
  347. }
  348. /* idct4 */
  349. for (i = 0; i < 8; i++) {
  350. for (j = 0; j < 4; j++) {
  351. /* top */
  352. sum = 0;
  353. for (k = 0; k < 4; k++)
  354. sum += c4[k][j] * block2[8 * (2 * k) + i];
  355. block3[8 * (2 * j) + i] = sum;
  356. /* bottom */
  357. sum = 0;
  358. for (k = 0; k < 4; k++)
  359. sum += c4[k][j] * block2[8 * (2 * k + 1) + i];
  360. block3[8 * (2 * j + 1) + i] = sum;
  361. }
  362. }
  363. /* clamp and store the result */
  364. for (i = 0; i < 8; i++) {
  365. for (j = 0; j < 8; j++) {
  366. v = block3[8 * i + j];
  367. if (v < 0) v = 0;
  368. else if (v > 255) v = 255;
  369. dest[i * linesize + j] = (int) rint(v);
  370. }
  371. }
  372. }
  373. static void idct248_error(const char *name,
  374. void (*idct248_put)(uint8_t *dest, int line_size,
  375. int16_t *block),
  376. int speed)
  377. {
  378. int it, i, it1, ti, ti1, err_max, v;
  379. AVLFG prng;
  380. av_lfg_init(&prng, 1);
  381. /* just one test to see if code is correct (precision is less
  382. important here) */
  383. err_max = 0;
  384. for (it = 0; it < NB_ITS; it++) {
  385. /* XXX: use forward transform to generate values */
  386. for (i = 0; i < 64; i++)
  387. block1[i] = av_lfg_get(&prng) % 256 - 128;
  388. block1[0] += 1024;
  389. for (i = 0; i < 64; i++)
  390. block[i] = block1[i];
  391. idct248_ref(img_dest1, 8, block);
  392. for (i = 0; i < 64; i++)
  393. block[i] = block1[i];
  394. idct248_put(img_dest, 8, block);
  395. for (i = 0; i < 64; i++) {
  396. v = abs((int) img_dest[i] - (int) img_dest1[i]);
  397. if (v == 255)
  398. printf("%d %d\n", img_dest[i], img_dest1[i]);
  399. if (v > err_max)
  400. err_max = v;
  401. }
  402. #if 0
  403. printf("ref=\n");
  404. for(i=0;i<8;i++) {
  405. int j;
  406. for(j=0;j<8;j++) {
  407. printf(" %3d", img_dest1[i*8+j]);
  408. }
  409. printf("\n");
  410. }
  411. printf("out=\n");
  412. for(i=0;i<8;i++) {
  413. int j;
  414. for(j=0;j<8;j++) {
  415. printf(" %3d", img_dest[i*8+j]);
  416. }
  417. printf("\n");
  418. }
  419. #endif
  420. }
  421. printf("%s %s: err_inf=%d\n", 1 ? "IDCT248" : "DCT248", name, err_max);
  422. if (!speed)
  423. return;
  424. ti = av_gettime();
  425. it1 = 0;
  426. do {
  427. for (it = 0; it < NB_ITS_SPEED; it++) {
  428. for (i = 0; i < 64; i++)
  429. block[i] = block1[i];
  430. idct248_put(img_dest, 8, block);
  431. }
  432. emms_c();
  433. it1 += NB_ITS_SPEED;
  434. ti1 = av_gettime() - ti;
  435. } while (ti1 < 1000000);
  436. printf("%s %s: %0.1f kdct/s\n", 1 ? "IDCT248" : "DCT248", name,
  437. (double) it1 * 1000.0 / (double) ti1);
  438. }
  439. static void help(void)
  440. {
  441. printf("dct-test [-i] [<test-number>] [<bits>]\n"
  442. "test-number 0 -> test with random matrixes\n"
  443. " 1 -> test with random sparse matrixes\n"
  444. " 2 -> do 3. test from mpeg4 std\n"
  445. "bits Number of time domain bits to use, 8 is default\n"
  446. "-i test IDCT implementations\n"
  447. "-4 test IDCT248 implementations\n"
  448. "-t speed test\n");
  449. }
  450. #if !HAVE_GETOPT
  451. #include "compat/getopt.c"
  452. #endif
  453. int main(int argc, char **argv)
  454. {
  455. int test_idct = 0, test_248_dct = 0;
  456. int c, i;
  457. int test = 1;
  458. int speed = 0;
  459. int err = 0;
  460. int bits=8;
  461. cpu_flags = av_get_cpu_flags();
  462. ff_ref_dct_init();
  463. idct_mmx_init();
  464. for (;;) {
  465. c = getopt(argc, argv, "ih4t");
  466. if (c == -1)
  467. break;
  468. switch (c) {
  469. case 'i':
  470. test_idct = 1;
  471. break;
  472. case '4':
  473. test_248_dct = 1;
  474. break;
  475. case 't':
  476. speed = 1;
  477. break;
  478. default:
  479. case 'h':
  480. help();
  481. return 0;
  482. }
  483. }
  484. if (optind < argc)
  485. test = atoi(argv[optind]);
  486. if(optind+1 < argc) bits= atoi(argv[optind+1]);
  487. printf("ffmpeg DCT/IDCT test\n");
  488. if (test_248_dct) {
  489. idct248_error("SIMPLE-C", ff_simple_idct248_put, speed);
  490. } else {
  491. const struct algo *algos = test_idct ? idct_tab : fdct_tab;
  492. for (i = 0; algos[i].name; i++)
  493. if (!(~cpu_flags & algos[i].mm_support)) {
  494. err |= dct_error(&algos[i], test, test_idct, speed, bits);
  495. }
  496. }
  497. return err;
  498. }