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.

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