Statistics
| Branch: | Revision:

ffmpeg / libavcodec / dct-test.c @ ea937d01

History | View | Annotate | Download (13.8 KB)

1 ff4ec49e Fabrice Bellard
/* DCT test. (c) 2001 Fabrice Bellard. 
2 de6d9b64 Fabrice Bellard
   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
12 9e1586fc Fabrice Bellard
#include "i386/mmx.h"
13 86748dbc Michael Niedermayer
#include "simple_idct.h"
14 9e1586fc Fabrice Bellard
15 e366e679 Fabrice Bellard
#ifndef MAX
16
#define MAX(a, b)  (((a) > (b)) ? (a) : (b))
17
#endif
18
19 9e1586fc Fabrice Bellard
/* reference fdct/idct */
20 de6d9b64 Fabrice Bellard
extern void fdct(DCTELEM *block);
21 9e1586fc Fabrice Bellard
extern void idct(DCTELEM *block);
22 de6d9b64 Fabrice Bellard
extern void init_fdct();
23
24 9e1586fc Fabrice Bellard
extern void j_rev_dct(DCTELEM *data);
25
extern void ff_mmx_idct(DCTELEM *data);
26
extern void ff_mmxext_idct(DCTELEM *data);
27
28 86748dbc Michael Niedermayer
extern void odivx_idct_c (short *block);
29
30 de6d9b64 Fabrice Bellard
#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
43 86748dbc Michael Niedermayer
UINT8 cropTbl[256 + 2 * MAX_NEG_CROP];
44
45 de6d9b64 Fabrice Bellard
INT64 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
55 9e1586fc Fabrice Bellard
static short idct_mmx_perm[64];
56
57 86748dbc Michael Niedermayer
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
68 9e1586fc Fabrice Bellard
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);
75 86748dbc Michael Niedermayer
//        idct_simple_mmx_perm[i] = simple_block_permute_op(i);
76 9e1586fc Fabrice Bellard
    }
77
}
78
79
static DCTELEM block[64] __attribute__ ((aligned (8)));
80
static DCTELEM block1[64] __attribute__ ((aligned (8)));
81 86748dbc Michael Niedermayer
static DCTELEM block_org[64] __attribute__ ((aligned (8)));
82 9e1586fc Fabrice Bellard
83
void dct_error(const char *name, int is_idct,
84
               void (*fdct_func)(DCTELEM *block),
85 86748dbc Michael Niedermayer
               void (*fdct_ref)(DCTELEM *block), int test)
86 de6d9b64 Fabrice Bellard
{
87
    int it, i, scale;
88
    int err_inf, v;
89
    INT64 err2, ti, ti1, it1;
90 86748dbc Michael Niedermayer
    INT64 sysErr[64], sysErrMax=0;
91
    int maxout=0;
92
    int blockSumErrMax=0, blockSumErr;
93 de6d9b64 Fabrice Bellard
94
    srandom(0);
95
96
    err_inf = 0;
97
    err2 = 0;
98 86748dbc Michael Niedermayer
    for(i=0; i<64; i++) sysErr[i]=0;
99 de6d9b64 Fabrice Bellard
    for(it=0;it<NB_ITS;it++) {
100 86748dbc Michael Niedermayer
        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;
106 ad324c93 Michael Niedermayer
            if (is_idct){
107 86748dbc Michael Niedermayer
                fdct(block1);
108 ad324c93 Michael Niedermayer
109
                for(i=0;i<64;i++)
110
                    block1[i]>>=3;
111
            }
112 86748dbc Michael Niedermayer
        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
        }
123 9e1586fc Fabrice Bellard
124 86748dbc Michael Niedermayer
#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];
135 9e1586fc Fabrice Bellard
136
        if (fdct_func == ff_mmx_idct ||
137 86748dbc Michael Niedermayer
            fdct_func == j_rev_dct || fdct_func == ff_mmxext_idct) {
138
            for(i=0;i<64;i++)
139 9e1586fc Fabrice Bellard
                block[idct_mmx_perm[i]] = block1[i];
140 a46a3ce4 Fabrice Bellard
        } else if(fdct_func == ff_simple_idct_mmx ) {
141 86748dbc Michael Niedermayer
            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];
147 9e1586fc Fabrice Bellard
        }
148 86748dbc Michael Niedermayer
#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
156 9e1586fc Fabrice Bellard
157 de6d9b64 Fabrice Bellard
        fdct_func(block);
158 9e1586fc Fabrice Bellard
        emms(); /* for ff_mmx_idct */
159
160 03c94ede Fabrice Bellard
        if (fdct_func == fdct_ifast) {
161 de6d9b64 Fabrice Bellard
            for(i=0; i<64; i++) {
162 ad324c93 Michael Niedermayer
                scale = 8*(1 << (AANSCALE_BITS + 11)) / aanscales[i];
163 86748dbc Michael Niedermayer
                block[i] = (block[i] * scale /*+ (1<<(AANSCALE_BITS-1))*/) >> AANSCALE_BITS;
164
            }
165
        }
166
167 9e1586fc Fabrice Bellard
        fdct_ref(block1);
168 de6d9b64 Fabrice Bellard
169 86748dbc Michael Niedermayer
        blockSumErr=0;
170 de6d9b64 Fabrice Bellard
        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;
175 86748dbc Michael Niedermayer
            sysErr[i] += block[i] - block1[i];
176
            blockSumErr += v;
177
            if( abs(block[i])>maxout) maxout=abs(block[i]);
178 de6d9b64 Fabrice Bellard
        }
179 86748dbc Michael Niedermayer
        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]);
200 de6d9b64 Fabrice Bellard
    }
201 86748dbc Michael Niedermayer
    printf("\n");
202
#endif
203
    
204
    printf("%s %s: err_inf=%d err2=%0.8f syserr=%0.8f maxout=%d blockSumErr=%d\n",
205 9e1586fc Fabrice Bellard
           is_idct ? "IDCT" : "DCT",
206 86748dbc Michael Niedermayer
           name, err_inf, (double)err2 / NB_ITS / 64.0, (double)sysErrMax / NB_ITS, maxout, blockSumErrMax);
207
#if 1 //Speed test
208 de6d9b64 Fabrice Bellard
    /* speed test */
209 86748dbc Michael Niedermayer
    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 ad324c93 Michael Niedermayer
        if (is_idct){
216 86748dbc Michael Niedermayer
            fdct(block1);
217 ad324c93 Michael Niedermayer
218
            for(i=0;i<64;i++)
219
                block1[i]>>=3;
220
        }
221 86748dbc Michael Niedermayer
    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
    }
230 de6d9b64 Fabrice Bellard
231 9e1586fc Fabrice Bellard
    if (fdct_func == ff_mmx_idct ||
232 86748dbc Michael Niedermayer
        fdct_func == j_rev_dct || fdct_func == ff_mmxext_idct) {
233
        for(i=0;i<64;i++)
234 9e1586fc Fabrice Bellard
            block[idct_mmx_perm[i]] = block1[i];
235 a46a3ce4 Fabrice Bellard
    } else if(fdct_func == ff_simple_idct_mmx ) {
236 86748dbc Michael Niedermayer
        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];
241 9e1586fc Fabrice Bellard
    }
242
243 de6d9b64 Fabrice Bellard
    ti = gettime();
244
    it1 = 0;
245
    do {
246
        for(it=0;it<NB_ITS_SPEED;it++) {
247 86748dbc Michael Niedermayer
            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 !!!
251 de6d9b64 Fabrice Bellard
            fdct_func(block);
252
        }
253
        it1 += NB_ITS_SPEED;
254
        ti1 = gettime() - ti;
255
    } while (ti1 < 1000000);
256 9e1586fc Fabrice Bellard
    emms();
257 de6d9b64 Fabrice Bellard
258 86748dbc Michael Niedermayer
    printf("%s %s: %0.1f kdct/s\n",
259 9e1586fc Fabrice Bellard
           is_idct ? "IDCT" : "DCT",
260 de6d9b64 Fabrice Bellard
           name, (double)it1 * 1000.0 / (double)ti1);
261 86748dbc Michael Niedermayer
#endif
262 de6d9b64 Fabrice Bellard
}
263
264 a46a3ce4 Fabrice Bellard
static UINT8 img_dest[64] __attribute__ ((aligned (8)));
265
static UINT8 img_dest1[64] __attribute__ ((aligned (8)));
266
267
void 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 */
299 652f0197 Fabrice Bellard
    s = 0.5 * sqrt(2.0);
300 a46a3ce4 Fabrice Bellard
    for(i=0;i<4;i++) {
301
        for(j=0;j<8;j++) {
302 652f0197 Fabrice Bellard
            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;
304 a46a3ce4 Fabrice Bellard
        }
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++) {
337 652f0197 Fabrice Bellard
            v = block3[8*i+j];
338 a46a3ce4 Fabrice Bellard
            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, 
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++) {
358
        
359 652f0197 Fabrice Bellard
        /* 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
364 a46a3ce4 Fabrice Bellard
        for(i=0; i<64; i++)
365
            block[i]= block1[i];
366
        idct248_ref(img_dest1, 8, block);
367
        
368 652f0197 Fabrice Bellard
        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
        }
379 a46a3ce4 Fabrice Bellard
#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
423 9e1586fc Fabrice Bellard
void help(void)
424
{
425 86748dbc Michael Niedermayer
    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"
429 a46a3ce4 Fabrice Bellard
           "-i          test IDCT implementations\n"
430
           "-4          test IDCT248 implementations\n");
431 9e1586fc Fabrice Bellard
    exit(1);
432
}
433
434 de6d9b64 Fabrice Bellard
int main(int argc, char **argv)
435
{
436 a46a3ce4 Fabrice Bellard
    int test_idct = 0, test_248_dct = 0;
437 86748dbc Michael Niedermayer
    int c,i;
438
    int test=1;
439 9e1586fc Fabrice Bellard
440 de6d9b64 Fabrice Bellard
    init_fdct();
441 9e1586fc Fabrice Bellard
    idct_mmx_init();
442 de6d9b64 Fabrice Bellard
443 86748dbc Michael Niedermayer
    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
    
449 9e1586fc Fabrice Bellard
    for(;;) {
450 a46a3ce4 Fabrice Bellard
        c = getopt(argc, argv, "ih4");
451 9e1586fc Fabrice Bellard
        if (c == -1)
452
            break;
453
        switch(c) {
454
        case 'i':
455
            test_idct = 1;
456
            break;
457 a46a3ce4 Fabrice Bellard
        case '4':
458
            test_248_dct = 1;
459
            break;
460 86748dbc Michael Niedermayer
        default :
461 9e1586fc Fabrice Bellard
        case 'h':
462
            help();
463
            break;
464
        }
465
    }
466 86748dbc Michael Niedermayer
    
467
    if(optind <argc) test= atoi(argv[optind]);
468 9e1586fc Fabrice Bellard
               
469
    printf("ffmpeg DCT/IDCT test\n");
470
471 a46a3ce4 Fabrice Bellard
    if (test_248_dct) {
472
        idct248_error("SIMPLE-C", simple_idct248_put);
473 9e1586fc Fabrice Bellard
    } else {
474 a46a3ce4 Fabrice Bellard
        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
        }
496 9e1586fc Fabrice Bellard
    }
497 de6d9b64 Fabrice Bellard
    return 0;
498
}