3 * DCT test. (c) 2001 Fabrice Bellard.
4 * Started from sample code by Juan J. Sierralta P.
16 #include "simple_idct.h"
20 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
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
];
52 gettimeofday(&tv
,NULL
);
53 return (int64_t)tv
.tv_sec
* 1000000 + tv
.tv_usec
;
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)
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
)
93 int64_t err2
, ti
, ti1
, it1
;
94 int64_t sysErr
[64], sysErrMax
=0;
96 int blockSumErrMax
=0, blockSumErr
;
102 for(i
=0; i
<64; i
++) sysErr
[i
]=0;
103 for(it
=0;it
<NB_ITS
;it
++) {
109 block1
[i
] = (random() % 512) -256;
118 int num
= (random()%10)+1;
120 block1
[random()%64] = (random() % 512) -256;
123 block1
[0]= (random()%4096)-2048;
124 block1
[63]= (block1
[0]&1)^1;
128 #if 0 // simulate mismatch control
133 if((sum
&1)==0) block1
[63]^=1;
138 block_org
[i
]= block1
[i
];
140 if (fdct_func
== ff_mmx_idct
||
141 fdct_func
== j_rev_dct
|| fdct_func
== ff_mmxext_idct
) {
143 block
[idct_mmx_perm
[i
]] = block1
[i
];
144 } else if(fdct_func
== ff_simple_idct_mmx
) {
146 block
[idct_simple_mmx_perm
[i
]] = block1
[i
];
152 #if 0 // simulate mismatch control for tested IDCT but not the ref
157 if((sum
&1)==0) block
[63]^=1;
162 emms(); /* for ff_mmx_idct */
164 if (fdct_func
== fdct_ifast
165 #ifndef FAAN_POSTSCALE
166 || fdct_func
== ff_faandct
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
;
179 v
= abs(block
[i
] - block1
[i
]);
183 sysErr
[i
] += block
[i
] - block1
[i
];
185 if( abs(block
[i
])>maxout
) maxout
=abs(block
[i
]);
187 if(blockSumErrMax
< blockSumErr
) blockSumErrMax
= blockSumErr
;
188 #if 0 // print different matrix pairs
192 if((i
&7)==0) printf("\n");
193 printf("%4d ", block_org
[i
]);
196 if((i
&7)==0) printf("\n");
197 printf("%4d ", block
[i
] - block1
[i
]);
202 for(i
=0; i
<64; i
++) sysErrMax
= MAX(sysErrMax
, ABS(sysErr
[i
]));
204 #if 1 // dump systematic errors
206 if(i
%8==0) printf("\n");
207 printf("%5d ", (int)sysErr
[i
]);
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
);
222 block1
[i
] = (random() % 512) -256;
232 block1
[0] = (random() % 512) -256;
233 block1
[1] = (random() % 512) -256;
234 block1
[2] = (random() % 512) -256;
235 block1
[3] = (random() % 512) -256;
239 if (fdct_func
== ff_mmx_idct
||
240 fdct_func
== j_rev_dct
|| fdct_func
== ff_mmxext_idct
) {
242 block
[idct_mmx_perm
[i
]] = block1
[i
];
243 } else if(fdct_func
== ff_simple_idct_mmx
) {
245 block
[idct_simple_mmx_perm
[i
]] = block1
[i
];
254 for(it
=0;it
<NB_ITS_SPEED
;it
++) {
257 // memcpy(block, block1, sizeof(DCTELEM) * 64);
258 // dont memcpy especially not fastmemcpy because it does movntq !!!
262 ti1
= gettime() - ti
;
263 } while (ti1
< 1000000);
266 printf("%s %s: %0.1f kdct/s\n",
267 is_idct
? "IDCT" : "DCT",
268 name
, (double)it1
* 1000.0 / (double)ti1
);
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
)
278 static double c8
[8][8];
279 static double c4
[4][4];
280 double block1
[64], block2
[64], block3
[64];
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
];
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
];
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
;
320 sum
+= c8
[k
][j
] * block1
[8*i
+k
];
331 sum
+= c4
[k
][j
] * block2
[8*(2*k
)+i
];
332 block3
[8*(2*j
)+i
] = sum
;
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 */
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
;
362 /* just one test to see if code is correct (precision is less
365 for(it
=0;it
<NB_ITS
;it
++) {
367 /* XXX: use forward transform to generate values */
369 block1
[i
] = (random() % 256) - 128;
374 idct248_ref(img_dest1
, 8, block
);
378 idct248_put(img_dest
, 8, block
);
381 v
= abs((int)img_dest
[i
] - (int)img_dest1
[i
]);
383 printf("%d %d\n", img_dest
[i
], img_dest1
[i
]);
392 printf(" %3d", img_dest1
[i
*8+j
]);
401 printf(" %3d", img_dest
[i
*8+j
]);
407 printf("%s %s: err_inf=%d\n",
408 1 ? "IDCT248" : "DCT248",
414 for(it
=0;it
<NB_ITS_SPEED
;it
++) {
417 // memcpy(block, block1, sizeof(DCTELEM) * 64);
418 // dont memcpy especially not fastmemcpy because it does movntq !!!
419 idct248_put(img_dest
, 8, block
);
422 ti1
= gettime() - ti
;
423 } while (ti1
< 1000000);
426 printf("%s %s: %0.1f kdct/s\n",
427 1 ? "IDCT248" : "DCT248",
428 name
, (double)it1
* 1000.0 / (double)ti1
);
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");
442 int main(int argc
, char **argv
)
444 int test_idct
= 0, test_248_dct
= 0;
451 for(i
=0;i
<256;i
++) cropTbl
[i
+ MAX_NEG_CROP
] = i
;
452 for(i
=0;i
<MAX_NEG_CROP
;i
++) {
454 cropTbl
[i
+ MAX_NEG_CROP
+ 256] = 255;
458 c
= getopt(argc
, argv
, "ih4");
475 if(optind
<argc
) test
= atoi(argv
[optind
]);
477 printf("ffmpeg DCT/IDCT test\n");
480 idct248_error("SIMPLE-C", simple_idct248_put
);
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
);
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);