-fPIC compileable
[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>
9
10#include "dsputil.h"
11
9e1586fc 12#include "i386/mmx.h"
86748dbc 13#include "simple_idct.h"
9e1586fc 14
e366e679
FB
15#ifndef MAX
16#define MAX(a, b) (((a) > (b)) ? (a) : (b))
17#endif
18
9e1586fc 19/* reference fdct/idct */
de6d9b64 20extern void fdct(DCTELEM *block);
9e1586fc 21extern void idct(DCTELEM *block);
de6d9b64
FB
22extern void init_fdct();
23
9e1586fc
FB
24extern void j_rev_dct(DCTELEM *data);
25extern void ff_mmx_idct(DCTELEM *data);
26extern void ff_mmxext_idct(DCTELEM *data);
27
86748dbc
MN
28extern void odivx_idct_c (short *block);
29
de6d9b64
FB
30#define AANSCALE_BITS 12
31static 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
86748dbc
MN
43UINT8 cropTbl[256 + 2 * MAX_NEG_CROP];
44
de6d9b64
FB
45INT64 gettime(void)
46{
47 struct timeval tv;
48 gettimeofday(&tv,NULL);
49 return (INT64)tv.tv_sec * 1000000 + tv.tv_usec;
50}
51
52#define NB_ITS 20000
53#define NB_ITS_SPEED 50000
54
9e1586fc
FB
55static short idct_mmx_perm[64];
56
86748dbc
MN
57static 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
9e1586fc
FB
68void 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);
86748dbc 75// idct_simple_mmx_perm[i] = simple_block_permute_op(i);
9e1586fc
FB
76 }
77}
78
79static DCTELEM block[64] __attribute__ ((aligned (8)));
80static DCTELEM block1[64] __attribute__ ((aligned (8)));
86748dbc 81static DCTELEM block_org[64] __attribute__ ((aligned (8)));
9e1586fc
FB
82
83void dct_error(const char *name, int is_idct,
84 void (*fdct_func)(DCTELEM *block),
86748dbc 85 void (*fdct_ref)(DCTELEM *block), int test)
de6d9b64
FB
86{
87 int it, i, scale;
de6d9b64
FB
88 int err_inf, v;
89 INT64 err2, ti, ti1, it1;
86748dbc
MN
90 INT64 sysErr[64], sysErrMax=0;
91 int maxout=0;
86748dbc 92 int blockSumErrMax=0, blockSumErr;
de6d9b64
FB
93
94 srandom(0);
95
96 err_inf = 0;
97 err2 = 0;
86748dbc 98 for(i=0; i<64; i++) sysErr[i]=0;
de6d9b64 99 for(it=0;it<NB_ITS;it++) {
86748dbc
MN
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;
ad324c93 106 if (is_idct){
86748dbc 107 fdct(block1);
ad324c93
MN
108
109 for(i=0;i<64;i++)
110 block1[i]>>=3;
111 }
86748dbc
MN
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 }
9e1586fc 123
86748dbc
MN
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];
9e1586fc
FB
135
136 if (fdct_func == ff_mmx_idct ||
86748dbc
MN
137 fdct_func == j_rev_dct || fdct_func == ff_mmxext_idct) {
138 for(i=0;i<64;i++)
9e1586fc 139 block[idct_mmx_perm[i]] = block1[i];
a46a3ce4 140 } else if(fdct_func == ff_simple_idct_mmx ) {
86748dbc
MN
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];
9e1586fc 147 }
86748dbc
MN
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
9e1586fc 156
de6d9b64 157 fdct_func(block);
9e1586fc
FB
158 emms(); /* for ff_mmx_idct */
159
03c94ede 160 if (fdct_func == fdct_ifast) {
de6d9b64 161 for(i=0; i<64; i++) {
ad324c93 162 scale = 8*(1 << (AANSCALE_BITS + 11)) / aanscales[i];
86748dbc
MN
163 block[i] = (block[i] * scale /*+ (1<<(AANSCALE_BITS-1))*/) >> AANSCALE_BITS;
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;
ad324c93 215 if (is_idct){
86748dbc 216 fdct(block1);
ad324c93
MN
217
218 for(i=0;i<64;i++)
219 block1[i]>>=3;
220 }
86748dbc
MN
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 }
de6d9b64 230
9e1586fc 231 if (fdct_func == ff_mmx_idct ||
86748dbc
MN
232 fdct_func == j_rev_dct || fdct_func == ff_mmxext_idct) {
233 for(i=0;i<64;i++)
9e1586fc 234 block[idct_mmx_perm[i]] = block1[i];
a46a3ce4 235 } else if(fdct_func == ff_simple_idct_mmx ) {
86748dbc
MN
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];
9e1586fc
FB
241 }
242
de6d9b64
FB
243 ti = gettime();
244 it1 = 0;
245 do {
246 for(it=0;it<NB_ITS_SPEED;it++) {
86748dbc
MN
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 !!!
de6d9b64
FB
251 fdct_func(block);
252 }
253 it1 += NB_ITS_SPEED;
254 ti1 = gettime() - ti;
255 } while (ti1 < 1000000);
9e1586fc 256 emms();
de6d9b64 257
86748dbc 258 printf("%s %s: %0.1f kdct/s\n",
9e1586fc 259 is_idct ? "IDCT" : "DCT",
de6d9b64 260 name, (double)it1 * 1000.0 / (double)ti1);
86748dbc 261#endif
de6d9b64
FB
262}
263
a46a3ce4
FB
264static UINT8 img_dest[64] __attribute__ ((aligned (8)));
265static UINT8 img_dest1[64] __attribute__ ((aligned (8)));
266
267void idct248_ref(UINT8 *dest, int linesize, INT16 *block)
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 */
652f0197 299 s = 0.5 * sqrt(2.0);
a46a3ce4
FB
300 for(i=0;i<4;i++) {
301 for(j=0;j<8;j++) {
652f0197
FB
302 block1[8*(2*i)+j] = (block[8*(2*i)+j] + block[8*(2*i+1)+j]) * s;
303 block1[8*(2*i+1)+j] = (block[8*(2*i)+j] - block[8*(2*i+1)+j]) * s;
a46a3ce4
FB
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++) {
652f0197 337 v = block3[8*i+j];
a46a3ce4
FB
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
347void idct248_error(const char *name,
348 void (*idct248_put)(UINT8 *dest, int line_size, INT16 *block))
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++) {
a46a3ce4 358
652f0197
FB
359 /* XXX: use forward transform to generate values */
360 for(i=0;i<64;i++)
361 block1[i] = (random() % 256) - 128;
362 block1[0] += 1024;
363
a46a3ce4
FB
364 for(i=0; i<64; i++)
365 block[i]= block1[i];
366 idct248_ref(img_dest1, 8, block);
367
652f0197
FB
368 for(i=0; i<64; i++)
369 block[i]= block1[i];
370 idct248_put(img_dest, 8, block);
371
372 for(i=0;i<64;i++) {
373 v = abs((int)img_dest[i] - (int)img_dest1[i]);
374 if (v == 255)
375 printf("%d %d\n", img_dest[i], img_dest1[i]);
376 if (v > err_max)
377 err_max = v;
378 }
a46a3ce4
FB
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 }
a46a3ce4 388
a46a3ce4
FB
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
a46a3ce4
FB
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
9e1586fc
FB
423void help(void)
424{
86748dbc
MN
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"
a46a3ce4
FB
429 "-i test IDCT implementations\n"
430 "-4 test IDCT248 implementations\n");
9e1586fc
FB
431 exit(1);
432}
433
de6d9b64
FB
434int main(int argc, char **argv)
435{
a46a3ce4 436 int test_idct = 0, test_248_dct = 0;
86748dbc
MN
437 int c,i;
438 int test=1;
9e1586fc 439
de6d9b64 440 init_fdct();
9e1586fc 441 idct_mmx_init();
de6d9b64 442
86748dbc
MN
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
9e1586fc 449 for(;;) {
a46a3ce4 450 c = getopt(argc, argv, "ih4");
9e1586fc
FB
451 if (c == -1)
452 break;
453 switch(c) {
454 case 'i':
455 test_idct = 1;
456 break;
a46a3ce4
FB
457 case '4':
458 test_248_dct = 1;
459 break;
86748dbc 460 default :
9e1586fc
FB
461 case 'h':
462 help();
463 break;
464 }
465 }
86748dbc
MN
466
467 if(optind <argc) test= atoi(argv[optind]);
9e1586fc
FB
468
469 printf("ffmpeg DCT/IDCT test\n");
470
a46a3ce4
FB
471 if (test_248_dct) {
472 idct248_error("SIMPLE-C", simple_idct248_put);
9e1586fc 473 } else {
a46a3ce4
FB
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 }
9e1586fc 496 }
de6d9b64
FB
497 return 0;
498}