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.

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