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.

533 lines
15KB

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