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.

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