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.

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