warning fixes by Dominik Mierzejewski <dominik@rangers.eu.org>
[libav.git] / libavcodec / dct-test.c
CommitLineData
ff4ec49e 1/* DCT test. (c) 2001 Fabrice Bellard.
de6d9b64
FB
2 Started from sample code by Juan J. Sierralta P.
3*/
4#include <stdlib.h>
5#include <stdio.h>
6#include <string.h>
7#include <sys/time.h>
8#include <unistd.h>
9e1586fc 9#include <getopt.h>
de6d9b64
FB
10
11#include "dsputil.h"
12
9e1586fc 13#include "i386/mmx.h"
86748dbc 14#include "simple_idct.h"
9e1586fc
FB
15
16/* reference fdct/idct */
de6d9b64 17extern void fdct(DCTELEM *block);
9e1586fc 18extern void idct(DCTELEM *block);
de6d9b64
FB
19extern void init_fdct();
20
9e1586fc
FB
21extern void j_rev_dct(DCTELEM *data);
22extern void ff_mmx_idct(DCTELEM *data);
23extern void ff_mmxext_idct(DCTELEM *data);
24
86748dbc
MN
25extern void odivx_idct_c (short *block);
26
de6d9b64
FB
27#define AANSCALE_BITS 12
28static const unsigned short aanscales[64] = {
29 /* precomputed values scaled up by 14 bits */
30 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
31 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
32 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
33 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
34 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
35 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
36 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
37 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
38};
39
86748dbc
MN
40UINT8 cropTbl[256 + 2 * MAX_NEG_CROP];
41
de6d9b64
FB
42INT64 gettime(void)
43{
44 struct timeval tv;
45 gettimeofday(&tv,NULL);
46 return (INT64)tv.tv_sec * 1000000 + tv.tv_usec;
47}
48
49#define NB_ITS 20000
50#define NB_ITS_SPEED 50000
51
9e1586fc
FB
52static short idct_mmx_perm[64];
53
86748dbc
MN
54static short idct_simple_mmx_perm[64]={
55 0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D,
56 0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D,
57 0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D,
58 0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F,
59 0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F,
60 0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D,
61 0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F,
62 0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
63};
64
9e1586fc
FB
65void idct_mmx_init(void)
66{
67 int i;
68
69 /* the mmx/mmxext idct uses a reordered input, so we patch scan tables */
70 for (i = 0; i < 64; i++) {
71 idct_mmx_perm[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
86748dbc 72// idct_simple_mmx_perm[i] = simple_block_permute_op(i);
9e1586fc
FB
73 }
74}
75
76static DCTELEM block[64] __attribute__ ((aligned (8)));
77static DCTELEM block1[64] __attribute__ ((aligned (8)));
86748dbc 78static DCTELEM block_org[64] __attribute__ ((aligned (8)));
9e1586fc
FB
79
80void dct_error(const char *name, int is_idct,
81 void (*fdct_func)(DCTELEM *block),
86748dbc 82 void (*fdct_ref)(DCTELEM *block), int test)
de6d9b64
FB
83{
84 int it, i, scale;
de6d9b64
FB
85 int err_inf, v;
86 INT64 err2, ti, ti1, it1;
86748dbc
MN
87 INT64 sysErr[64], sysErrMax=0;
88 int maxout=0;
89 int max_sum=0;
90 int blockSumErrMax=0, blockSumErr;
de6d9b64
FB
91
92 srandom(0);
93
94 err_inf = 0;
95 err2 = 0;
86748dbc 96 for(i=0; i<64; i++) sysErr[i]=0;
de6d9b64 97 for(it=0;it<NB_ITS;it++) {
86748dbc
MN
98 for(i=0;i<64;i++)
99 block1[i] = 0;
100 switch(test){
101 case 0:
102 for(i=0;i<64;i++)
103 block1[i] = (random() % 512) -256;
104 if (is_idct)
105 fdct(block1);
106 break;
107 case 1:{
108 int num= (random()%10)+1;
109 for(i=0;i<num;i++)
110 block1[random()%64] = (random() % 512) -256;
111 }break;
112 case 2:
113 block1[0]= (random()%4096)-2048;
114 block1[63]= (block1[0]&1)^1;
115 break;
116 }
9e1586fc 117
86748dbc
MN
118#if 0 // simulate mismatch control
119{ int sum=0;
120 for(i=0;i<64;i++)
121 sum+=block1[i];
122
123 if((sum&1)==0) block1[63]^=1;
124}
125#endif
126
127 for(i=0; i<64; i++)
128 block_org[i]= block1[i];
9e1586fc
FB
129
130 if (fdct_func == ff_mmx_idct ||
86748dbc
MN
131 fdct_func == j_rev_dct || fdct_func == ff_mmxext_idct) {
132 for(i=0;i<64;i++)
9e1586fc 133 block[idct_mmx_perm[i]] = block1[i];
86748dbc
MN
134 } else if(fdct_func == simple_idct_mmx ) {
135 for(i=0;i<64;i++)
136 block[idct_simple_mmx_perm[i]] = block1[i];
137
138 } else {
139 for(i=0; i<64; i++)
140 block[i]= block1[i];
9e1586fc 141 }
86748dbc
MN
142#if 0 // simulate mismatch control for tested IDCT but not the ref
143{ int sum=0;
144 for(i=0;i<64;i++)
145 sum+=block[i];
146
147 if((sum&1)==0) block[63]^=1;
148}
149#endif
9e1586fc 150
de6d9b64 151 fdct_func(block);
9e1586fc
FB
152 emms(); /* for ff_mmx_idct */
153
03c94ede 154 if (fdct_func == fdct_ifast) {
de6d9b64
FB
155 for(i=0; i<64; i++) {
156 scale = (1 << (AANSCALE_BITS + 11)) / aanscales[i];
86748dbc
MN
157 block[i] = (block[i] * scale /*+ (1<<(AANSCALE_BITS-1))*/) >> AANSCALE_BITS;
158 }
159 }
160
161 if (fdct_func == ff_jpeg_fdct_islow) {
162 for(i=0; i<64; i++) {
163 block[i] = (block[i]+3)>>3;
de6d9b64
FB
164 }
165 }
166
9e1586fc 167 fdct_ref(block1);
de6d9b64 168
86748dbc 169 blockSumErr=0;
de6d9b64
FB
170 for(i=0;i<64;i++) {
171 v = abs(block[i] - block1[i]);
172 if (v > err_inf)
173 err_inf = v;
174 err2 += v * v;
86748dbc
MN
175 sysErr[i] += block[i] - block1[i];
176 blockSumErr += v;
177 if( abs(block[i])>maxout) maxout=abs(block[i]);
de6d9b64 178 }
86748dbc
MN
179 if(blockSumErrMax < blockSumErr) blockSumErrMax= blockSumErr;
180#if 0 // print different matrix pairs
181 if(blockSumErr){
182 printf("\n");
183 for(i=0; i<64; i++){
184 if((i&7)==0) printf("\n");
185 printf("%4d ", block_org[i]);
186 }
187 for(i=0; i<64; i++){
188 if((i&7)==0) printf("\n");
189 printf("%4d ", block[i] - block1[i]);
190 }
191 }
192#endif
193 }
194 for(i=0; i<64; i++) sysErrMax= MAX(sysErrMax, ABS(sysErr[i]));
195
196#if 1 // dump systematic errors
197 for(i=0; i<64; i++){
198 if(i%8==0) printf("\n");
199 printf("%5d ", (int)sysErr[i]);
de6d9b64 200 }
86748dbc
MN
201 printf("\n");
202#endif
203
204 printf("%s %s: err_inf=%d err2=%0.8f syserr=%0.8f maxout=%d blockSumErr=%d\n",
9e1586fc 205 is_idct ? "IDCT" : "DCT",
86748dbc
MN
206 name, err_inf, (double)err2 / NB_ITS / 64.0, (double)sysErrMax / NB_ITS, maxout, blockSumErrMax);
207#if 1 //Speed test
de6d9b64 208 /* speed test */
86748dbc
MN
209 for(i=0;i<64;i++)
210 block1[i] = 0;
211 switch(test){
212 case 0:
213 for(i=0;i<64;i++)
214 block1[i] = (random() % 512) -256;
215 if (is_idct)
216 fdct(block1);
217 break;
218 case 1:{
219 case 2:
220 block1[0] = (random() % 512) -256;
221 block1[1] = (random() % 512) -256;
222 block1[2] = (random() % 512) -256;
223 block1[3] = (random() % 512) -256;
224 }break;
225 }
de6d9b64 226
9e1586fc 227 if (fdct_func == ff_mmx_idct ||
86748dbc
MN
228 fdct_func == j_rev_dct || fdct_func == ff_mmxext_idct) {
229 for(i=0;i<64;i++)
9e1586fc 230 block[idct_mmx_perm[i]] = block1[i];
86748dbc
MN
231 } else if(fdct_func == simple_idct_mmx ) {
232 for(i=0;i<64;i++)
233 block[idct_simple_mmx_perm[i]] = block1[i];
234 } else {
235 for(i=0; i<64; i++)
236 block[i]= block1[i];
9e1586fc
FB
237 }
238
de6d9b64
FB
239 ti = gettime();
240 it1 = 0;
241 do {
242 for(it=0;it<NB_ITS_SPEED;it++) {
86748dbc
MN
243 for(i=0; i<64; i++)
244 block[i]= block1[i];
245// memcpy(block, block1, sizeof(DCTELEM) * 64);
246// dont memcpy especially not fastmemcpy because it does movntq !!!
de6d9b64
FB
247 fdct_func(block);
248 }
249 it1 += NB_ITS_SPEED;
250 ti1 = gettime() - ti;
251 } while (ti1 < 1000000);
9e1586fc 252 emms();
de6d9b64 253
86748dbc 254 printf("%s %s: %0.1f kdct/s\n",
9e1586fc 255 is_idct ? "IDCT" : "DCT",
de6d9b64 256 name, (double)it1 * 1000.0 / (double)ti1);
86748dbc 257#endif
de6d9b64
FB
258}
259
9e1586fc
FB
260void help(void)
261{
86748dbc
MN
262 printf("dct-test [-i] [<test-number>]\n"
263 "test-number 0 -> test with random matrixes\n"
264 " 1 -> test with random sparse matrixes\n"
265 " 2 -> do 3. test from mpeg4 std\n"
266 "-i test IDCT implementations\n");
9e1586fc
FB
267 exit(1);
268}
269
de6d9b64
FB
270int main(int argc, char **argv)
271{
9e1586fc 272 int test_idct = 0;
86748dbc
MN
273 int c,i;
274 int test=1;
9e1586fc 275
de6d9b64 276 init_fdct();
9e1586fc 277 idct_mmx_init();
de6d9b64 278
86748dbc
MN
279 for(i=0;i<256;i++) cropTbl[i + MAX_NEG_CROP] = i;
280 for(i=0;i<MAX_NEG_CROP;i++) {
281 cropTbl[i] = 0;
282 cropTbl[i + MAX_NEG_CROP + 256] = 255;
283 }
284
9e1586fc
FB
285 for(;;) {
286 c = getopt(argc, argv, "ih");
287 if (c == -1)
288 break;
289 switch(c) {
290 case 'i':
291 test_idct = 1;
292 break;
86748dbc 293 default :
9e1586fc
FB
294 case 'h':
295 help();
296 break;
297 }
298 }
86748dbc
MN
299
300 if(optind <argc) test= atoi(argv[optind]);
9e1586fc
FB
301
302 printf("ffmpeg DCT/IDCT test\n");
303
304 if (!test_idct) {
86748dbc
MN
305 dct_error("REF-DBL", 0, fdct, fdct, test); /* only to verify code ! */
306 dct_error("IJG-AAN-INT", 0, fdct_ifast, fdct, test);
307 dct_error("IJG-LLM-INT", 0, ff_jpeg_fdct_islow, fdct, test);
308 dct_error("MMX", 0, fdct_mmx, fdct, test);
9e1586fc 309 } else {
86748dbc
MN
310 dct_error("REF-DBL", 1, idct, idct, test);
311 dct_error("INT", 1, j_rev_dct, idct, test);
312 dct_error("LIBMPEG2-MMX", 1, ff_mmx_idct, idct, test);
313 dct_error("LIBMPEG2-MMXEXT", 1, ff_mmxext_idct, idct, test);
314 dct_error("SIMPLE-C", 1, simple_idct, idct, test);
315 dct_error("SIMPLE-MMX", 1, simple_idct_mmx, idct, test);
316// dct_error("ODIVX-C", 1, odivx_idct_c, idct);
317//printf(" test against odivx idct\n");
318// dct_error("REF", 1, idct, odivx_idct_c);
319// dct_error("INT", 1, j_rev_dct, odivx_idct_c);
320// dct_error("MMX", 1, ff_mmx_idct, odivx_idct_c);
321// dct_error("MMXEXT", 1, ff_mmxext_idct, odivx_idct_c);
322// dct_error("SIMPLE-C", 1, simple_idct, odivx_idct_c);
323// dct_error("SIMPLE-MMX", 1, simple_idct_mmx, odivx_idct_c);
324// dct_error("ODIVX-C", 1, odivx_idct_c, odivx_idct_c);
9e1586fc 325 }
de6d9b64
FB
326 return 0;
327}