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.

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