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))
25 void *fast_memcpy(void *a
, const void *b
, size_t c
){return memcpy(a
,b
,c
);};
27 /* reference fdct/idct */
28 extern void fdct(DCTELEM
*block
);
29 extern void idct(DCTELEM
*block
);
30 extern void ff_idct_xvid_mmx(DCTELEM
*block
);
31 extern void ff_idct_xvid_mmx2(DCTELEM
*block
);
32 extern void init_fdct();
34 extern void j_rev_dct(DCTELEM
*data
);
35 extern void ff_mmx_idct(DCTELEM
*data
);
36 extern void ff_mmxext_idct(DCTELEM
*data
);
38 extern void odivx_idct_c (short *block
);
40 #define AANSCALE_BITS 12
41 static const unsigned short aanscales
[64] = {
42 /* precomputed values scaled up by 14 bits */
43 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
44 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
45 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
46 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
47 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
48 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
49 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
50 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
53 uint8_t cropTbl
[256 + 2 * MAX_NEG_CROP
];
58 gettimeofday(&tv
,NULL
);
59 return (int64_t)tv
.tv_sec
* 1000000 + tv
.tv_usec
;
63 #define NB_ITS_SPEED 50000
65 static short idct_mmx_perm
[64];
67 static short idct_simple_mmx_perm
[64]={
68 0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D,
69 0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D,
70 0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D,
71 0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F,
72 0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F,
73 0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D,
74 0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F,
75 0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
78 void idct_mmx_init(void)
82 /* the mmx/mmxext idct uses a reordered input, so we patch scan tables */
83 for (i
= 0; i
< 64; i
++) {
84 idct_mmx_perm
[i
] = (i
& 0x38) | ((i
& 6) >> 1) | ((i
& 1) << 2);
85 // idct_simple_mmx_perm[i] = simple_block_permute_op(i);
89 static DCTELEM block
[64] __attribute__ ((aligned (8)));
90 static DCTELEM block1
[64] __attribute__ ((aligned (8)));
91 static DCTELEM block_org
[64] __attribute__ ((aligned (8)));
93 void dct_error(const char *name
, int is_idct
,
94 void (*fdct_func
)(DCTELEM
*block
),
95 void (*fdct_ref
)(DCTELEM
*block
), int test
)
99 int64_t err2
, ti
, ti1
, it1
;
100 int64_t sysErr
[64], sysErrMax
=0;
102 int blockSumErrMax
=0, blockSumErr
;
108 for(i
=0; i
<64; i
++) sysErr
[i
]=0;
109 for(it
=0;it
<NB_ITS
;it
++) {
115 block1
[i
] = (random() % 512) -256;
124 int num
= (random()%10)+1;
126 block1
[random()%64] = (random() % 512) -256;
129 block1
[0]= (random()%4096)-2048;
130 block1
[63]= (block1
[0]&1)^1;
134 #if 0 // simulate mismatch control
139 if((sum
&1)==0) block1
[63]^=1;
144 block_org
[i
]= block1
[i
];
146 if (fdct_func
== ff_mmx_idct
||
147 fdct_func
== j_rev_dct
|| fdct_func
== ff_mmxext_idct
) {
149 block
[idct_mmx_perm
[i
]] = block1
[i
];
150 } else if(fdct_func
== ff_simple_idct_mmx
) {
152 block
[idct_simple_mmx_perm
[i
]] = block1
[i
];
158 #if 0 // simulate mismatch control for tested IDCT but not the ref
163 if((sum
&1)==0) block
[63]^=1;
168 emms(); /* for ff_mmx_idct */
170 if (fdct_func
== fdct_ifast
171 #ifndef FAAN_POSTSCALE
172 || fdct_func
== ff_faandct
175 for(i
=0; i
<64; i
++) {
176 scale
= 8*(1 << (AANSCALE_BITS
+ 11)) / aanscales
[i
];
177 block
[i
] = (block
[i
] * scale
/*+ (1<<(AANSCALE_BITS-1))*/) >> AANSCALE_BITS
;
185 v
= abs(block
[i
] - block1
[i
]);
189 sysErr
[i
] += block
[i
] - block1
[i
];
191 if( abs(block
[i
])>maxout
) maxout
=abs(block
[i
]);
193 if(blockSumErrMax
< blockSumErr
) blockSumErrMax
= blockSumErr
;
194 #if 0 // print different matrix pairs
198 if((i
&7)==0) printf("\n");
199 printf("%4d ", block_org
[i
]);
202 if((i
&7)==0) printf("\n");
203 printf("%4d ", block
[i
] - block1
[i
]);
208 for(i
=0; i
<64; i
++) sysErrMax
= MAX(sysErrMax
, ABS(sysErr
[i
]));
210 #if 1 // dump systematic errors
212 if(i
%8==0) printf("\n");
213 printf("%5d ", (int)sysErr
[i
]);
218 printf("%s %s: err_inf=%d err2=%0.8f syserr=%0.8f maxout=%d blockSumErr=%d\n",
219 is_idct
? "IDCT" : "DCT",
220 name
, err_inf
, (double)err2
/ NB_ITS
/ 64.0, (double)sysErrMax
/ NB_ITS
, maxout
, blockSumErrMax
);
228 block1
[i
] = (random() % 512) -256;
238 block1
[0] = (random() % 512) -256;
239 block1
[1] = (random() % 512) -256;
240 block1
[2] = (random() % 512) -256;
241 block1
[3] = (random() % 512) -256;
245 if (fdct_func
== ff_mmx_idct
||
246 fdct_func
== j_rev_dct
|| fdct_func
== ff_mmxext_idct
) {
248 block
[idct_mmx_perm
[i
]] = block1
[i
];
249 } else if(fdct_func
== ff_simple_idct_mmx
) {
251 block
[idct_simple_mmx_perm
[i
]] = block1
[i
];
260 for(it
=0;it
<NB_ITS_SPEED
;it
++) {
263 // memcpy(block, block1, sizeof(DCTELEM) * 64);
264 // dont memcpy especially not fastmemcpy because it does movntq !!!
268 ti1
= gettime() - ti
;
269 } while (ti1
< 1000000);
272 printf("%s %s: %0.1f kdct/s\n",
273 is_idct
? "IDCT" : "DCT",
274 name
, (double)it1
* 1000.0 / (double)ti1
);
278 static uint8_t img_dest
[64] __attribute__ ((aligned (8)));
279 static uint8_t img_dest1
[64] __attribute__ ((aligned (8)));
281 void idct248_ref(uint8_t *dest
, int linesize
, int16_t *block
)
284 static double c8
[8][8];
285 static double c4
[4][4];
286 double block1
[64], block2
[64], block3
[64];
296 s
= (i
==0) ? sqrt(1.0/8.0) : sqrt(1.0/4.0);
297 c8
[i
][j
] = s
* cos(M_PI
* i
* (j
+ 0.5) / 8.0);
298 sum
+= c8
[i
][j
] * c8
[i
][j
];
305 s
= (i
==0) ? sqrt(1.0/4.0) : sqrt(1.0/2.0);
306 c4
[i
][j
] = s
* cos(M_PI
* i
* (j
+ 0.5) / 4.0);
307 sum
+= c4
[i
][j
] * c4
[i
][j
];
316 block1
[8*(2*i
)+j
] = (block
[8*(2*i
)+j
] + block
[8*(2*i
+1)+j
]) * s
;
317 block1
[8*(2*i
+1)+j
] = (block
[8*(2*i
)+j
] - block
[8*(2*i
+1)+j
]) * s
;
326 sum
+= c8
[k
][j
] * block1
[8*i
+k
];
337 sum
+= c4
[k
][j
] * block2
[8*(2*k
)+i
];
338 block3
[8*(2*j
)+i
] = sum
;
343 sum
+= c4
[k
][j
] * block2
[8*(2*k
+1)+i
];
344 block3
[8*(2*j
+1)+i
] = sum
;
348 /* clamp and store the result */
356 dest
[i
* linesize
+ j
] = (int)rint(v
);
361 void idct248_error(const char *name
,
362 void (*idct248_put
)(uint8_t *dest
, int line_size
, int16_t *block
))
364 int it
, i
, it1
, ti
, ti1
, err_max
, v
;
368 /* just one test to see if code is correct (precision is less
371 for(it
=0;it
<NB_ITS
;it
++) {
373 /* XXX: use forward transform to generate values */
375 block1
[i
] = (random() % 256) - 128;
380 idct248_ref(img_dest1
, 8, block
);
384 idct248_put(img_dest
, 8, block
);
387 v
= abs((int)img_dest
[i
] - (int)img_dest1
[i
]);
389 printf("%d %d\n", img_dest
[i
], img_dest1
[i
]);
398 printf(" %3d", img_dest1
[i
*8+j
]);
407 printf(" %3d", img_dest
[i
*8+j
]);
413 printf("%s %s: err_inf=%d\n",
414 1 ? "IDCT248" : "DCT248",
420 for(it
=0;it
<NB_ITS_SPEED
;it
++) {
423 // memcpy(block, block1, sizeof(DCTELEM) * 64);
424 // dont memcpy especially not fastmemcpy because it does movntq !!!
425 idct248_put(img_dest
, 8, block
);
428 ti1
= gettime() - ti
;
429 } while (ti1
< 1000000);
432 printf("%s %s: %0.1f kdct/s\n",
433 1 ? "IDCT248" : "DCT248",
434 name
, (double)it1
* 1000.0 / (double)ti1
);
439 printf("dct-test [-i] [<test-number>]\n"
440 "test-number 0 -> test with random matrixes\n"
441 " 1 -> test with random sparse matrixes\n"
442 " 2 -> do 3. test from mpeg4 std\n"
443 "-i test IDCT implementations\n"
444 "-4 test IDCT248 implementations\n");
448 int main(int argc
, char **argv
)
450 int test_idct
= 0, test_248_dct
= 0;
457 for(i
=0;i
<256;i
++) cropTbl
[i
+ MAX_NEG_CROP
] = i
;
458 for(i
=0;i
<MAX_NEG_CROP
;i
++) {
460 cropTbl
[i
+ MAX_NEG_CROP
+ 256] = 255;
464 c
= getopt(argc
, argv
, "ih4");
481 if(optind
<argc
) test
= atoi(argv
[optind
]);
483 printf("ffmpeg DCT/IDCT test\n");
486 idct248_error("SIMPLE-C", simple_idct248_put
);
489 dct_error("REF-DBL", 0, fdct
, fdct
, test
); /* only to verify code ! */
490 dct_error("IJG-AAN-INT", 0, fdct_ifast
, fdct
, test
);
491 dct_error("IJG-LLM-INT", 0, ff_jpeg_fdct_islow
, fdct
, test
);
492 dct_error("MMX", 0, ff_fdct_mmx
, fdct
, test
);
493 dct_error("MMX2", 0, ff_fdct_mmx2
, fdct
, test
);
494 dct_error("FAAN", 0, ff_faandct
, fdct
, test
);
496 dct_error("REF-DBL", 1, idct
, idct
, test
);
497 dct_error("INT", 1, j_rev_dct
, idct
, test
);
498 dct_error("LIBMPEG2-MMX", 1, ff_mmx_idct
, idct
, test
);
499 dct_error("LIBMPEG2-MMXEXT", 1, ff_mmxext_idct
, idct
, test
);
500 dct_error("SIMPLE-C", 1, simple_idct
, idct
, test
);
501 dct_error("SIMPLE-MMX", 1, ff_simple_idct_mmx
, idct
, test
);
502 dct_error("XVID-MMX", 1, ff_idct_xvid_mmx
, idct
, test
);
503 dct_error("XVID-MMX2", 1, ff_idct_xvid_mmx2
, idct
, test
);
504 // dct_error("ODIVX-C", 1, odivx_idct_c, idct);
505 //printf(" test against odivx idct\n");
506 // dct_error("REF", 1, idct, odivx_idct_c);
507 // dct_error("INT", 1, j_rev_dct, odivx_idct_c);
508 // dct_error("MMX", 1, ff_mmx_idct, odivx_idct_c);
509 // dct_error("MMXEXT", 1, ff_mmxext_idct, odivx_idct_c);
510 // dct_error("SIMPLE-C", 1, simple_idct, odivx_idct_c);
511 // dct_error("SIMPLE-MMX", 1, ff_simple_idct_mmx, odivx_idct_c);
512 // dct_error("ODIVX-C", 1, odivx_idct_c, odivx_idct_c);