r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / quicktime / ffmpeg / libavcodec / dct-test.c
blob649f15de678b611cfcc773587621a8719567d95f
1 /**
2 * @file dct-test.c
3 * DCT test. (c) 2001 Fabrice Bellard.
4 * Started from sample code by Juan J. Sierralta P.
5 */
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <sys/time.h>
11 #include <unistd.h>
13 #include "dsputil.h"
15 #include "i386/mmx.h"
16 #include "simple_idct.h"
17 #include "faandct.h"
19 #ifndef MAX
20 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
21 #endif
23 /* reference fdct/idct */
24 extern void fdct(DCTELEM *block);
25 extern void idct(DCTELEM *block);
26 extern void init_fdct();
28 extern void j_rev_dct(DCTELEM *data);
29 extern void ff_mmx_idct(DCTELEM *data);
30 extern void ff_mmxext_idct(DCTELEM *data);
32 extern void odivx_idct_c (short *block);
34 #define AANSCALE_BITS 12
35 static const unsigned short aanscales[64] = {
36 /* precomputed values scaled up by 14 bits */
37 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
38 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
39 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
40 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
41 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
42 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
43 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
44 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
47 uint8_t cropTbl[256 + 2 * MAX_NEG_CROP];
49 int64_t gettime(void)
51 struct timeval tv;
52 gettimeofday(&tv,NULL);
53 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
56 #define NB_ITS 20000
57 #define NB_ITS_SPEED 50000
59 static short idct_mmx_perm[64];
61 static short idct_simple_mmx_perm[64]={
62 0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D,
63 0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D,
64 0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D,
65 0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F,
66 0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F,
67 0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D,
68 0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F,
69 0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
72 void idct_mmx_init(void)
74 int i;
76 /* the mmx/mmxext idct uses a reordered input, so we patch scan tables */
77 for (i = 0; i < 64; i++) {
78 idct_mmx_perm[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
79 // idct_simple_mmx_perm[i] = simple_block_permute_op(i);
83 static DCTELEM block[64] __attribute__ ((aligned (8)));
84 static DCTELEM block1[64] __attribute__ ((aligned (8)));
85 static DCTELEM block_org[64] __attribute__ ((aligned (8)));
87 void dct_error(const char *name, int is_idct,
88 void (*fdct_func)(DCTELEM *block),
89 void (*fdct_ref)(DCTELEM *block), int test)
91 int it, i, scale;
92 int err_inf, v;
93 int64_t err2, ti, ti1, it1;
94 int64_t sysErr[64], sysErrMax=0;
95 int maxout=0;
96 int blockSumErrMax=0, blockSumErr;
98 srandom(0);
100 err_inf = 0;
101 err2 = 0;
102 for(i=0; i<64; i++) sysErr[i]=0;
103 for(it=0;it<NB_ITS;it++) {
104 for(i=0;i<64;i++)
105 block1[i] = 0;
106 switch(test){
107 case 0:
108 for(i=0;i<64;i++)
109 block1[i] = (random() % 512) -256;
110 if (is_idct){
111 fdct(block1);
113 for(i=0;i<64;i++)
114 block1[i]>>=3;
116 break;
117 case 1:{
118 int num= (random()%10)+1;
119 for(i=0;i<num;i++)
120 block1[random()%64] = (random() % 512) -256;
121 }break;
122 case 2:
123 block1[0]= (random()%4096)-2048;
124 block1[63]= (block1[0]&1)^1;
125 break;
128 #if 0 // simulate mismatch control
129 { int sum=0;
130 for(i=0;i<64;i++)
131 sum+=block1[i];
133 if((sum&1)==0) block1[63]^=1;
135 #endif
137 for(i=0; i<64; i++)
138 block_org[i]= block1[i];
140 if (fdct_func == ff_mmx_idct ||
141 fdct_func == j_rev_dct || fdct_func == ff_mmxext_idct) {
142 for(i=0;i<64;i++)
143 block[idct_mmx_perm[i]] = block1[i];
144 } else if(fdct_func == ff_simple_idct_mmx ) {
145 for(i=0;i<64;i++)
146 block[idct_simple_mmx_perm[i]] = block1[i];
148 } else {
149 for(i=0; i<64; i++)
150 block[i]= block1[i];
152 #if 0 // simulate mismatch control for tested IDCT but not the ref
153 { int sum=0;
154 for(i=0;i<64;i++)
155 sum+=block[i];
157 if((sum&1)==0) block[63]^=1;
159 #endif
161 fdct_func(block);
162 emms(); /* for ff_mmx_idct */
164 if (fdct_func == fdct_ifast
165 #ifndef FAAN_POSTSCALE
166 || fdct_func == ff_faandct
167 #endif
169 for(i=0; i<64; i++) {
170 scale = 8*(1 << (AANSCALE_BITS + 11)) / aanscales[i];
171 block[i] = (block[i] * scale /*+ (1<<(AANSCALE_BITS-1))*/) >> AANSCALE_BITS;
175 fdct_ref(block1);
177 blockSumErr=0;
178 for(i=0;i<64;i++) {
179 v = abs(block[i] - block1[i]);
180 if (v > err_inf)
181 err_inf = v;
182 err2 += v * v;
183 sysErr[i] += block[i] - block1[i];
184 blockSumErr += v;
185 if( abs(block[i])>maxout) maxout=abs(block[i]);
187 if(blockSumErrMax < blockSumErr) blockSumErrMax= blockSumErr;
188 #if 0 // print different matrix pairs
189 if(blockSumErr){
190 printf("\n");
191 for(i=0; i<64; i++){
192 if((i&7)==0) printf("\n");
193 printf("%4d ", block_org[i]);
195 for(i=0; i<64; i++){
196 if((i&7)==0) printf("\n");
197 printf("%4d ", block[i] - block1[i]);
200 #endif
202 for(i=0; i<64; i++) sysErrMax= MAX(sysErrMax, ABS(sysErr[i]));
204 #if 1 // dump systematic errors
205 for(i=0; i<64; i++){
206 if(i%8==0) printf("\n");
207 printf("%5d ", (int)sysErr[i]);
209 printf("\n");
210 #endif
212 printf("%s %s: err_inf=%d err2=%0.8f syserr=%0.8f maxout=%d blockSumErr=%d\n",
213 is_idct ? "IDCT" : "DCT",
214 name, err_inf, (double)err2 / NB_ITS / 64.0, (double)sysErrMax / NB_ITS, maxout, blockSumErrMax);
215 #if 1 //Speed test
216 /* speed test */
217 for(i=0;i<64;i++)
218 block1[i] = 0;
219 switch(test){
220 case 0:
221 for(i=0;i<64;i++)
222 block1[i] = (random() % 512) -256;
223 if (is_idct){
224 fdct(block1);
226 for(i=0;i<64;i++)
227 block1[i]>>=3;
229 break;
230 case 1:{
231 case 2:
232 block1[0] = (random() % 512) -256;
233 block1[1] = (random() % 512) -256;
234 block1[2] = (random() % 512) -256;
235 block1[3] = (random() % 512) -256;
236 }break;
239 if (fdct_func == ff_mmx_idct ||
240 fdct_func == j_rev_dct || fdct_func == ff_mmxext_idct) {
241 for(i=0;i<64;i++)
242 block[idct_mmx_perm[i]] = block1[i];
243 } else if(fdct_func == ff_simple_idct_mmx ) {
244 for(i=0;i<64;i++)
245 block[idct_simple_mmx_perm[i]] = block1[i];
246 } else {
247 for(i=0; i<64; i++)
248 block[i]= block1[i];
251 ti = gettime();
252 it1 = 0;
253 do {
254 for(it=0;it<NB_ITS_SPEED;it++) {
255 for(i=0; i<64; i++)
256 block[i]= block1[i];
257 // memcpy(block, block1, sizeof(DCTELEM) * 64);
258 // dont memcpy especially not fastmemcpy because it does movntq !!!
259 fdct_func(block);
261 it1 += NB_ITS_SPEED;
262 ti1 = gettime() - ti;
263 } while (ti1 < 1000000);
264 emms();
266 printf("%s %s: %0.1f kdct/s\n",
267 is_idct ? "IDCT" : "DCT",
268 name, (double)it1 * 1000.0 / (double)ti1);
269 #endif
272 static uint8_t img_dest[64] __attribute__ ((aligned (8)));
273 static uint8_t img_dest1[64] __attribute__ ((aligned (8)));
275 void idct248_ref(uint8_t *dest, int linesize, int16_t *block)
277 static int init;
278 static double c8[8][8];
279 static double c4[4][4];
280 double block1[64], block2[64], block3[64];
281 double s, sum, v;
282 int i, j, k;
284 if (!init) {
285 init = 1;
287 for(i=0;i<8;i++) {
288 sum = 0;
289 for(j=0;j<8;j++) {
290 s = (i==0) ? sqrt(1.0/8.0) : sqrt(1.0/4.0);
291 c8[i][j] = s * cos(M_PI * i * (j + 0.5) / 8.0);
292 sum += c8[i][j] * c8[i][j];
296 for(i=0;i<4;i++) {
297 sum = 0;
298 for(j=0;j<4;j++) {
299 s = (i==0) ? sqrt(1.0/4.0) : sqrt(1.0/2.0);
300 c4[i][j] = s * cos(M_PI * i * (j + 0.5) / 4.0);
301 sum += c4[i][j] * c4[i][j];
306 /* butterfly */
307 s = 0.5 * sqrt(2.0);
308 for(i=0;i<4;i++) {
309 for(j=0;j<8;j++) {
310 block1[8*(2*i)+j] = (block[8*(2*i)+j] + block[8*(2*i+1)+j]) * s;
311 block1[8*(2*i+1)+j] = (block[8*(2*i)+j] - block[8*(2*i+1)+j]) * s;
315 /* idct8 on lines */
316 for(i=0;i<8;i++) {
317 for(j=0;j<8;j++) {
318 sum = 0;
319 for(k=0;k<8;k++)
320 sum += c8[k][j] * block1[8*i+k];
321 block2[8*i+j] = sum;
325 /* idct4 */
326 for(i=0;i<8;i++) {
327 for(j=0;j<4;j++) {
328 /* top */
329 sum = 0;
330 for(k=0;k<4;k++)
331 sum += c4[k][j] * block2[8*(2*k)+i];
332 block3[8*(2*j)+i] = sum;
334 /* bottom */
335 sum = 0;
336 for(k=0;k<4;k++)
337 sum += c4[k][j] * block2[8*(2*k+1)+i];
338 block3[8*(2*j+1)+i] = sum;
342 /* clamp and store the result */
343 for(i=0;i<8;i++) {
344 for(j=0;j<8;j++) {
345 v = block3[8*i+j];
346 if (v < 0)
347 v = 0;
348 else if (v > 255)
349 v = 255;
350 dest[i * linesize + j] = (int)rint(v);
355 void idct248_error(const char *name,
356 void (*idct248_put)(uint8_t *dest, int line_size, int16_t *block))
358 int it, i, it1, ti, ti1, err_max, v;
360 srandom(0);
362 /* just one test to see if code is correct (precision is less
363 important here) */
364 err_max = 0;
365 for(it=0;it<NB_ITS;it++) {
367 /* XXX: use forward transform to generate values */
368 for(i=0;i<64;i++)
369 block1[i] = (random() % 256) - 128;
370 block1[0] += 1024;
372 for(i=0; i<64; i++)
373 block[i]= block1[i];
374 idct248_ref(img_dest1, 8, block);
376 for(i=0; i<64; i++)
377 block[i]= block1[i];
378 idct248_put(img_dest, 8, block);
380 for(i=0;i<64;i++) {
381 v = abs((int)img_dest[i] - (int)img_dest1[i]);
382 if (v == 255)
383 printf("%d %d\n", img_dest[i], img_dest1[i]);
384 if (v > err_max)
385 err_max = v;
387 #if 0
388 printf("ref=\n");
389 for(i=0;i<8;i++) {
390 int j;
391 for(j=0;j<8;j++) {
392 printf(" %3d", img_dest1[i*8+j]);
394 printf("\n");
397 printf("out=\n");
398 for(i=0;i<8;i++) {
399 int j;
400 for(j=0;j<8;j++) {
401 printf(" %3d", img_dest[i*8+j]);
403 printf("\n");
405 #endif
407 printf("%s %s: err_inf=%d\n",
408 1 ? "IDCT248" : "DCT248",
409 name, err_max);
411 ti = gettime();
412 it1 = 0;
413 do {
414 for(it=0;it<NB_ITS_SPEED;it++) {
415 for(i=0; i<64; i++)
416 block[i]= block1[i];
417 // memcpy(block, block1, sizeof(DCTELEM) * 64);
418 // dont memcpy especially not fastmemcpy because it does movntq !!!
419 idct248_put(img_dest, 8, block);
421 it1 += NB_ITS_SPEED;
422 ti1 = gettime() - ti;
423 } while (ti1 < 1000000);
424 emms();
426 printf("%s %s: %0.1f kdct/s\n",
427 1 ? "IDCT248" : "DCT248",
428 name, (double)it1 * 1000.0 / (double)ti1);
431 void help(void)
433 printf("dct-test [-i] [<test-number>]\n"
434 "test-number 0 -> test with random matrixes\n"
435 " 1 -> test with random sparse matrixes\n"
436 " 2 -> do 3. test from mpeg4 std\n"
437 "-i test IDCT implementations\n"
438 "-4 test IDCT248 implementations\n");
439 exit(1);
442 int main(int argc, char **argv)
444 int test_idct = 0, test_248_dct = 0;
445 int c,i;
446 int test=1;
448 init_fdct();
449 idct_mmx_init();
451 for(i=0;i<256;i++) cropTbl[i + MAX_NEG_CROP] = i;
452 for(i=0;i<MAX_NEG_CROP;i++) {
453 cropTbl[i] = 0;
454 cropTbl[i + MAX_NEG_CROP + 256] = 255;
457 for(;;) {
458 c = getopt(argc, argv, "ih4");
459 if (c == -1)
460 break;
461 switch(c) {
462 case 'i':
463 test_idct = 1;
464 break;
465 case '4':
466 test_248_dct = 1;
467 break;
468 default :
469 case 'h':
470 help();
471 break;
475 if(optind <argc) test= atoi(argv[optind]);
477 printf("ffmpeg DCT/IDCT test\n");
479 if (test_248_dct) {
480 idct248_error("SIMPLE-C", simple_idct248_put);
481 } else {
482 if (!test_idct) {
483 dct_error("REF-DBL", 0, fdct, fdct, test); /* only to verify code ! */
484 dct_error("IJG-AAN-INT", 0, fdct_ifast, fdct, test);
485 dct_error("IJG-LLM-INT", 0, ff_jpeg_fdct_islow, fdct, test);
486 dct_error("MMX", 0, ff_fdct_mmx, fdct, test);
487 dct_error("MMX2", 0, ff_fdct_mmx2, fdct, test);
488 dct_error("FAAN", 0, ff_faandct, fdct, test);
489 } else {
490 dct_error("REF-DBL", 1, idct, idct, test);
491 dct_error("INT", 1, j_rev_dct, idct, test);
492 dct_error("LIBMPEG2-MMX", 1, ff_mmx_idct, idct, test);
493 dct_error("LIBMPEG2-MMXEXT", 1, ff_mmxext_idct, idct, test);
494 dct_error("SIMPLE-C", 1, simple_idct, idct, test);
495 dct_error("SIMPLE-MMX", 1, ff_simple_idct_mmx, idct, test);
496 // dct_error("ODIVX-C", 1, odivx_idct_c, idct);
497 //printf(" test against odivx idct\n");
498 // dct_error("REF", 1, idct, odivx_idct_c);
499 // dct_error("INT", 1, j_rev_dct, odivx_idct_c);
500 // dct_error("MMX", 1, ff_mmx_idct, odivx_idct_c);
501 // dct_error("MMXEXT", 1, ff_mmxext_idct, odivx_idct_c);
502 // dct_error("SIMPLE-C", 1, simple_idct, odivx_idct_c);
503 // dct_error("SIMPLE-MMX", 1, ff_simple_idct_mmx, odivx_idct_c);
504 // dct_error("ODIVX-C", 1, odivx_idct_c, odivx_idct_c);
507 return 0;