Mercurial > libavcodec.hg
annotate dct-test.c @ 1090:8216533dd959 libavcodec
10l
author | michaelni |
---|---|
date | Fri, 28 Feb 2003 16:06:40 +0000 |
parents | b32afefe7d33 |
children | 1e39f273ecd6 |
rev | line source |
---|---|
429 | 1 /* DCT test. (c) 2001 Fabrice Bellard. |
0 | 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> | |
9 | |
10 #include "dsputil.h" | |
11 | |
33 | 12 #include "i386/mmx.h" |
633 | 13 #include "simple_idct.h" |
33 | 14 |
980 | 15 #ifndef MAX |
16 #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
17 #endif | |
18 | |
33 | 19 /* reference fdct/idct */ |
0 | 20 extern void fdct(DCTELEM *block); |
33 | 21 extern void idct(DCTELEM *block); |
0 | 22 extern void init_fdct(); |
23 | |
33 | 24 extern void j_rev_dct(DCTELEM *data); |
25 extern void ff_mmx_idct(DCTELEM *data); | |
26 extern void ff_mmxext_idct(DCTELEM *data); | |
27 | |
633 | 28 extern void odivx_idct_c (short *block); |
29 | |
0 | 30 #define AANSCALE_BITS 12 |
31 static const unsigned short aanscales[64] = { | |
32 /* precomputed values scaled up by 14 bits */ | |
33 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
34 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270, | |
35 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906, | |
36 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315, | |
37 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520, | |
38 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552, | |
39 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446, | |
40 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247 | |
41 }; | |
42 | |
1064 | 43 uint8_t cropTbl[256 + 2 * MAX_NEG_CROP]; |
633 | 44 |
1064 | 45 int64_t gettime(void) |
0 | 46 { |
47 struct timeval tv; | |
48 gettimeofday(&tv,NULL); | |
1064 | 49 return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec; |
0 | 50 } |
51 | |
52 #define NB_ITS 20000 | |
53 #define NB_ITS_SPEED 50000 | |
54 | |
33 | 55 static short idct_mmx_perm[64]; |
56 | |
633 | 57 static short idct_simple_mmx_perm[64]={ |
58 0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D, | |
59 0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D, | |
60 0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D, | |
61 0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F, | |
62 0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F, | |
63 0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D, | |
64 0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F, | |
65 0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F, | |
66 }; | |
67 | |
33 | 68 void idct_mmx_init(void) |
69 { | |
70 int i; | |
71 | |
72 /* the mmx/mmxext idct uses a reordered input, so we patch scan tables */ | |
73 for (i = 0; i < 64; i++) { | |
74 idct_mmx_perm[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2); | |
633 | 75 // idct_simple_mmx_perm[i] = simple_block_permute_op(i); |
33 | 76 } |
77 } | |
78 | |
79 static DCTELEM block[64] __attribute__ ((aligned (8))); | |
80 static DCTELEM block1[64] __attribute__ ((aligned (8))); | |
633 | 81 static DCTELEM block_org[64] __attribute__ ((aligned (8))); |
33 | 82 |
83 void dct_error(const char *name, int is_idct, | |
84 void (*fdct_func)(DCTELEM *block), | |
633 | 85 void (*fdct_ref)(DCTELEM *block), int test) |
0 | 86 { |
87 int it, i, scale; | |
88 int err_inf, v; | |
1064 | 89 int64_t err2, ti, ti1, it1; |
90 int64_t sysErr[64], sysErrMax=0; | |
633 | 91 int maxout=0; |
92 int blockSumErrMax=0, blockSumErr; | |
0 | 93 |
94 srandom(0); | |
95 | |
96 err_inf = 0; | |
97 err2 = 0; | |
633 | 98 for(i=0; i<64; i++) sysErr[i]=0; |
0 | 99 for(it=0;it<NB_ITS;it++) { |
633 | 100 for(i=0;i<64;i++) |
101 block1[i] = 0; | |
102 switch(test){ | |
103 case 0: | |
104 for(i=0;i<64;i++) | |
105 block1[i] = (random() % 512) -256; | |
635 | 106 if (is_idct){ |
633 | 107 fdct(block1); |
635 | 108 |
109 for(i=0;i<64;i++) | |
110 block1[i]>>=3; | |
111 } | |
633 | 112 break; |
113 case 1:{ | |
114 int num= (random()%10)+1; | |
115 for(i=0;i<num;i++) | |
116 block1[random()%64] = (random() % 512) -256; | |
117 }break; | |
118 case 2: | |
119 block1[0]= (random()%4096)-2048; | |
120 block1[63]= (block1[0]&1)^1; | |
121 break; | |
122 } | |
33 | 123 |
633 | 124 #if 0 // simulate mismatch control |
125 { int sum=0; | |
126 for(i=0;i<64;i++) | |
127 sum+=block1[i]; | |
128 | |
129 if((sum&1)==0) block1[63]^=1; | |
130 } | |
131 #endif | |
132 | |
133 for(i=0; i<64; i++) | |
134 block_org[i]= block1[i]; | |
33 | 135 |
136 if (fdct_func == ff_mmx_idct || | |
633 | 137 fdct_func == j_rev_dct || fdct_func == ff_mmxext_idct) { |
138 for(i=0;i<64;i++) | |
33 | 139 block[idct_mmx_perm[i]] = block1[i]; |
720 | 140 } else if(fdct_func == ff_simple_idct_mmx ) { |
633 | 141 for(i=0;i<64;i++) |
142 block[idct_simple_mmx_perm[i]] = block1[i]; | |
143 | |
144 } else { | |
145 for(i=0; i<64; i++) | |
146 block[i]= block1[i]; | |
33 | 147 } |
633 | 148 #if 0 // simulate mismatch control for tested IDCT but not the ref |
149 { int sum=0; | |
150 for(i=0;i<64;i++) | |
151 sum+=block[i]; | |
152 | |
153 if((sum&1)==0) block[63]^=1; | |
154 } | |
155 #endif | |
33 | 156 |
0 | 157 fdct_func(block); |
33 | 158 emms(); /* for ff_mmx_idct */ |
159 | |
474
11dbd00682fc
avoid name clash with libjpeg - added missing externs
bellard
parents:
429
diff
changeset
|
160 if (fdct_func == fdct_ifast) { |
0 | 161 for(i=0; i<64; i++) { |
635 | 162 scale = 8*(1 << (AANSCALE_BITS + 11)) / aanscales[i]; |
633 | 163 block[i] = (block[i] * scale /*+ (1<<(AANSCALE_BITS-1))*/) >> AANSCALE_BITS; |
164 } | |
165 } | |
166 | |
33 | 167 fdct_ref(block1); |
0 | 168 |
633 | 169 blockSumErr=0; |
0 | 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; | |
633 | 175 sysErr[i] += block[i] - block1[i]; |
176 blockSumErr += v; | |
177 if( abs(block[i])>maxout) maxout=abs(block[i]); | |
0 | 178 } |
633 | 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 | |
0 | 193 } |
633 | 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]); | |
200 } | |
201 printf("\n"); | |
202 #endif | |
203 | |
204 printf("%s %s: err_inf=%d err2=%0.8f syserr=%0.8f maxout=%d blockSumErr=%d\n", | |
33 | 205 is_idct ? "IDCT" : "DCT", |
633 | 206 name, err_inf, (double)err2 / NB_ITS / 64.0, (double)sysErrMax / NB_ITS, maxout, blockSumErrMax); |
207 #if 1 //Speed test | |
0 | 208 /* speed test */ |
633 | 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; | |
635 | 215 if (is_idct){ |
633 | 216 fdct(block1); |
635 | 217 |
218 for(i=0;i<64;i++) | |
219 block1[i]>>=3; | |
220 } | |
633 | 221 break; |
222 case 1:{ | |
223 case 2: | |
224 block1[0] = (random() % 512) -256; | |
225 block1[1] = (random() % 512) -256; | |
226 block1[2] = (random() % 512) -256; | |
227 block1[3] = (random() % 512) -256; | |
228 }break; | |
229 } | |
0 | 230 |
33 | 231 if (fdct_func == ff_mmx_idct || |
633 | 232 fdct_func == j_rev_dct || fdct_func == ff_mmxext_idct) { |
233 for(i=0;i<64;i++) | |
33 | 234 block[idct_mmx_perm[i]] = block1[i]; |
720 | 235 } else if(fdct_func == ff_simple_idct_mmx ) { |
633 | 236 for(i=0;i<64;i++) |
237 block[idct_simple_mmx_perm[i]] = block1[i]; | |
238 } else { | |
239 for(i=0; i<64; i++) | |
240 block[i]= block1[i]; | |
33 | 241 } |
242 | |
0 | 243 ti = gettime(); |
244 it1 = 0; | |
245 do { | |
246 for(it=0;it<NB_ITS_SPEED;it++) { | |
633 | 247 for(i=0; i<64; i++) |
248 block[i]= block1[i]; | |
249 // memcpy(block, block1, sizeof(DCTELEM) * 64); | |
250 // dont memcpy especially not fastmemcpy because it does movntq !!! | |
0 | 251 fdct_func(block); |
252 } | |
253 it1 += NB_ITS_SPEED; | |
254 ti1 = gettime() - ti; | |
255 } while (ti1 < 1000000); | |
33 | 256 emms(); |
0 | 257 |
633 | 258 printf("%s %s: %0.1f kdct/s\n", |
33 | 259 is_idct ? "IDCT" : "DCT", |
0 | 260 name, (double)it1 * 1000.0 / (double)ti1); |
633 | 261 #endif |
0 | 262 } |
263 | |
1064 | 264 static uint8_t img_dest[64] __attribute__ ((aligned (8))); |
265 static uint8_t img_dest1[64] __attribute__ ((aligned (8))); | |
720 | 266 |
1064 | 267 void idct248_ref(uint8_t *dest, int linesize, int16_t *block) |
720 | 268 { |
269 static int init; | |
270 static double c8[8][8]; | |
271 static double c4[4][4]; | |
272 double block1[64], block2[64], block3[64]; | |
273 double s, sum, v; | |
274 int i, j, k; | |
275 | |
276 if (!init) { | |
277 init = 1; | |
278 | |
279 for(i=0;i<8;i++) { | |
280 sum = 0; | |
281 for(j=0;j<8;j++) { | |
282 s = (i==0) ? sqrt(1.0/8.0) : sqrt(1.0/4.0); | |
283 c8[i][j] = s * cos(M_PI * i * (j + 0.5) / 8.0); | |
284 sum += c8[i][j] * c8[i][j]; | |
285 } | |
286 } | |
287 | |
288 for(i=0;i<4;i++) { | |
289 sum = 0; | |
290 for(j=0;j<4;j++) { | |
291 s = (i==0) ? sqrt(1.0/4.0) : sqrt(1.0/2.0); | |
292 c4[i][j] = s * cos(M_PI * i * (j + 0.5) / 4.0); | |
293 sum += c4[i][j] * c4[i][j]; | |
294 } | |
295 } | |
296 } | |
297 | |
298 /* butterfly */ | |
722
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
299 s = 0.5 * sqrt(2.0); |
720 | 300 for(i=0;i<4;i++) { |
301 for(j=0;j<8;j++) { | |
722
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
302 block1[8*(2*i)+j] = (block[8*(2*i)+j] + block[8*(2*i+1)+j]) * s; |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
303 block1[8*(2*i+1)+j] = (block[8*(2*i)+j] - block[8*(2*i+1)+j]) * s; |
720 | 304 } |
305 } | |
306 | |
307 /* idct8 on lines */ | |
308 for(i=0;i<8;i++) { | |
309 for(j=0;j<8;j++) { | |
310 sum = 0; | |
311 for(k=0;k<8;k++) | |
312 sum += c8[k][j] * block1[8*i+k]; | |
313 block2[8*i+j] = sum; | |
314 } | |
315 } | |
316 | |
317 /* idct4 */ | |
318 for(i=0;i<8;i++) { | |
319 for(j=0;j<4;j++) { | |
320 /* top */ | |
321 sum = 0; | |
322 for(k=0;k<4;k++) | |
323 sum += c4[k][j] * block2[8*(2*k)+i]; | |
324 block3[8*(2*j)+i] = sum; | |
325 | |
326 /* bottom */ | |
327 sum = 0; | |
328 for(k=0;k<4;k++) | |
329 sum += c4[k][j] * block2[8*(2*k+1)+i]; | |
330 block3[8*(2*j+1)+i] = sum; | |
331 } | |
332 } | |
333 | |
334 /* clamp and store the result */ | |
335 for(i=0;i<8;i++) { | |
336 for(j=0;j<8;j++) { | |
722
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
337 v = block3[8*i+j]; |
720 | 338 if (v < 0) |
339 v = 0; | |
340 else if (v > 255) | |
341 v = 255; | |
342 dest[i * linesize + j] = (int)rint(v); | |
343 } | |
344 } | |
345 } | |
346 | |
347 void idct248_error(const char *name, | |
1064 | 348 void (*idct248_put)(uint8_t *dest, int line_size, int16_t *block)) |
720 | 349 { |
350 int it, i, it1, ti, ti1, err_max, v; | |
351 | |
352 srandom(0); | |
353 | |
354 /* just one test to see if code is correct (precision is less | |
355 important here) */ | |
356 err_max = 0; | |
357 for(it=0;it<NB_ITS;it++) { | |
722
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
358 |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
359 /* XXX: use forward transform to generate values */ |
720 | 360 for(i=0;i<64;i++) |
722
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
361 block1[i] = (random() % 256) - 128; |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
362 block1[0] += 1024; |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
363 |
720 | 364 for(i=0; i<64; i++) |
365 block[i]= block1[i]; | |
366 idct248_ref(img_dest1, 8, block); | |
367 | |
722
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
368 for(i=0; i<64; i++) |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
369 block[i]= block1[i]; |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
370 idct248_put(img_dest, 8, block); |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
371 |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
372 for(i=0;i<64;i++) { |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
373 v = abs((int)img_dest[i] - (int)img_dest1[i]); |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
374 if (v == 255) |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
375 printf("%d %d\n", img_dest[i], img_dest1[i]); |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
376 if (v > err_max) |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
377 err_max = v; |
ff90043f4a2d
in fact IDCT248 needs to be normalized as I suspected
bellard
parents:
720
diff
changeset
|
378 } |
720 | 379 #if 0 |
380 printf("ref=\n"); | |
381 for(i=0;i<8;i++) { | |
382 int j; | |
383 for(j=0;j<8;j++) { | |
384 printf(" %3d", img_dest1[i*8+j]); | |
385 } | |
386 printf("\n"); | |
387 } | |
388 | |
389 printf("out=\n"); | |
390 for(i=0;i<8;i++) { | |
391 int j; | |
392 for(j=0;j<8;j++) { | |
393 printf(" %3d", img_dest[i*8+j]); | |
394 } | |
395 printf("\n"); | |
396 } | |
397 #endif | |
398 } | |
399 printf("%s %s: err_inf=%d\n", | |
400 1 ? "IDCT248" : "DCT248", | |
401 name, err_max); | |
402 | |
403 ti = gettime(); | |
404 it1 = 0; | |
405 do { | |
406 for(it=0;it<NB_ITS_SPEED;it++) { | |
407 for(i=0; i<64; i++) | |
408 block[i]= block1[i]; | |
409 // memcpy(block, block1, sizeof(DCTELEM) * 64); | |
410 // dont memcpy especially not fastmemcpy because it does movntq !!! | |
411 idct248_put(img_dest, 8, block); | |
412 } | |
413 it1 += NB_ITS_SPEED; | |
414 ti1 = gettime() - ti; | |
415 } while (ti1 < 1000000); | |
416 emms(); | |
417 | |
418 printf("%s %s: %0.1f kdct/s\n", | |
419 1 ? "IDCT248" : "DCT248", | |
420 name, (double)it1 * 1000.0 / (double)ti1); | |
421 } | |
422 | |
33 | 423 void help(void) |
424 { | |
633 | 425 printf("dct-test [-i] [<test-number>]\n" |
426 "test-number 0 -> test with random matrixes\n" | |
427 " 1 -> test with random sparse matrixes\n" | |
428 " 2 -> do 3. test from mpeg4 std\n" | |
720 | 429 "-i test IDCT implementations\n" |
430 "-4 test IDCT248 implementations\n"); | |
33 | 431 exit(1); |
432 } | |
433 | |
0 | 434 int main(int argc, char **argv) |
435 { | |
720 | 436 int test_idct = 0, test_248_dct = 0; |
633 | 437 int c,i; |
438 int test=1; | |
33 | 439 |
0 | 440 init_fdct(); |
33 | 441 idct_mmx_init(); |
0 | 442 |
633 | 443 for(i=0;i<256;i++) cropTbl[i + MAX_NEG_CROP] = i; |
444 for(i=0;i<MAX_NEG_CROP;i++) { | |
445 cropTbl[i] = 0; | |
446 cropTbl[i + MAX_NEG_CROP + 256] = 255; | |
447 } | |
448 | |
33 | 449 for(;;) { |
720 | 450 c = getopt(argc, argv, "ih4"); |
33 | 451 if (c == -1) |
452 break; | |
453 switch(c) { | |
454 case 'i': | |
455 test_idct = 1; | |
456 break; | |
720 | 457 case '4': |
458 test_248_dct = 1; | |
459 break; | |
633 | 460 default : |
33 | 461 case 'h': |
462 help(); | |
463 break; | |
464 } | |
465 } | |
633 | 466 |
467 if(optind <argc) test= atoi(argv[optind]); | |
33 | 468 |
469 printf("ffmpeg DCT/IDCT test\n"); | |
470 | |
720 | 471 if (test_248_dct) { |
472 idct248_error("SIMPLE-C", simple_idct248_put); | |
33 | 473 } else { |
720 | 474 if (!test_idct) { |
475 dct_error("REF-DBL", 0, fdct, fdct, test); /* only to verify code ! */ | |
476 dct_error("IJG-AAN-INT", 0, fdct_ifast, fdct, test); | |
477 dct_error("IJG-LLM-INT", 0, ff_jpeg_fdct_islow, fdct, test); | |
478 dct_error("MMX", 0, ff_fdct_mmx, fdct, test); | |
479 } else { | |
480 dct_error("REF-DBL", 1, idct, idct, test); | |
481 dct_error("INT", 1, j_rev_dct, idct, test); | |
482 dct_error("LIBMPEG2-MMX", 1, ff_mmx_idct, idct, test); | |
483 dct_error("LIBMPEG2-MMXEXT", 1, ff_mmxext_idct, idct, test); | |
484 dct_error("SIMPLE-C", 1, simple_idct, idct, test); | |
485 dct_error("SIMPLE-MMX", 1, ff_simple_idct_mmx, idct, test); | |
486 // dct_error("ODIVX-C", 1, odivx_idct_c, idct); | |
487 //printf(" test against odivx idct\n"); | |
488 // dct_error("REF", 1, idct, odivx_idct_c); | |
489 // dct_error("INT", 1, j_rev_dct, odivx_idct_c); | |
490 // dct_error("MMX", 1, ff_mmx_idct, odivx_idct_c); | |
491 // dct_error("MMXEXT", 1, ff_mmxext_idct, odivx_idct_c); | |
492 // dct_error("SIMPLE-C", 1, simple_idct, odivx_idct_c); | |
493 // dct_error("SIMPLE-MMX", 1, ff_simple_idct_mmx, odivx_idct_c); | |
494 // dct_error("ODIVX-C", 1, odivx_idct_c, odivx_idct_c); | |
495 } | |
33 | 496 } |
0 | 497 return 0; |
498 } |