Statistics
| Branch: | Revision:

ffmpeg / libavcodec / dct-test.c @ 19ef2ba5

History | View | Annotate | Download (15.1 KB)

1
/*
2
 * (c) 2001 Fabrice Bellard
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20

    
21
/**
22
 * @file dct-test.c
23
 * DCT test. (c) 2001 Fabrice Bellard.
24
 * Started from sample code by Juan J. Sierralta P.
25
 */
26

    
27
#include <stdlib.h>
28
#include <stdio.h>
29
#include <string.h>
30
#include <sys/time.h>
31
#include <unistd.h>
32

    
33
#include "dsputil.h"
34

    
35
#include "simple_idct.h"
36
#include "faandct.h"
37

    
38
#ifndef MAX
39
#define MAX(a, b)  (((a) > (b)) ? (a) : (b))
40
#endif
41

    
42
#undef printf
43

    
44
void *fast_memcpy(void *a, const void *b, size_t c){return memcpy(a,b,c);};
45

    
46
/* reference fdct/idct */
47
extern void fdct(DCTELEM *block);
48
extern void idct(DCTELEM *block);
49
extern void ff_idct_xvid_mmx(DCTELEM *block);
50
extern void ff_idct_xvid_mmx2(DCTELEM *block);
51
extern void init_fdct();
52

    
53
extern void j_rev_dct(DCTELEM *data);
54
extern void ff_mmx_idct(DCTELEM *data);
55
extern void ff_mmxext_idct(DCTELEM *data);
56

    
57
extern void odivx_idct_c (short *block);
58

    
59
#define AANSCALE_BITS 12
60
static const unsigned short aanscales[64] = {
61
    /* precomputed values scaled up by 14 bits */
62
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
63
    22725, 31521, 29692, 26722, 22725, 17855, 12299,  6270,
64
    21407, 29692, 27969, 25172, 21407, 16819, 11585,  5906,
65
    19266, 26722, 25172, 22654, 19266, 15137, 10426,  5315,
66
    16384, 22725, 21407, 19266, 16384, 12873,  8867,  4520,
67
    12873, 17855, 16819, 15137, 12873, 10114,  6967,  3552,
68
    8867, 12299, 11585, 10426,  8867,  6967,  4799,  2446,
69
    4520,  6270,  5906,  5315,  4520,  3552,  2446,  1247
70
};
71

    
72
uint8_t cropTbl[256 + 2 * MAX_NEG_CROP];
73

    
74
int64_t gettime(void)
75
{
76
    struct timeval tv;
77
    gettimeofday(&tv,NULL);
78
    return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
79
}
80

    
81
#define NB_ITS 20000
82
#define NB_ITS_SPEED 50000
83

    
84
static short idct_mmx_perm[64];
85

    
86
static short idct_simple_mmx_perm[64]={
87
        0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D,
88
        0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D,
89
        0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D,
90
        0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F,
91
        0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F,
92
        0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D,
93
        0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F,
94
        0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F,
95
};
96

    
97
void idct_mmx_init(void)
98
{
99
    int i;
100

    
101
    /* the mmx/mmxext idct uses a reordered input, so we patch scan tables */
102
    for (i = 0; i < 64; i++) {
103
        idct_mmx_perm[i] = (i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2);
104
//        idct_simple_mmx_perm[i] = simple_block_permute_op(i);
105
    }
106
}
107

    
108
static DCTELEM block[64] __attribute__ ((aligned (8)));
109
static DCTELEM block1[64] __attribute__ ((aligned (8)));
110
static DCTELEM block_org[64] __attribute__ ((aligned (8)));
111

    
112
void dct_error(const char *name, int is_idct,
113
               void (*fdct_func)(DCTELEM *block),
114
               void (*fdct_ref)(DCTELEM *block), int test)
115
{
116
    int it, i, scale;
117
    int err_inf, v;
118
    int64_t err2, ti, ti1, it1;
119
    int64_t sysErr[64], sysErrMax=0;
120
    int maxout=0;
121
    int blockSumErrMax=0, blockSumErr;
122

    
123
    srandom(0);
124

    
125
    err_inf = 0;
126
    err2 = 0;
127
    for(i=0; i<64; i++) sysErr[i]=0;
128
    for(it=0;it<NB_ITS;it++) {
129
        for(i=0;i<64;i++)
130
            block1[i] = 0;
131
        switch(test){
132
        case 0:
133
            for(i=0;i<64;i++)
134
                block1[i] = (random() % 512) -256;
135
            if (is_idct){
136
                fdct(block1);
137

    
138
                for(i=0;i<64;i++)
139
                    block1[i]>>=3;
140
            }
141
        break;
142
        case 1:{
143
            int num= (random()%10)+1;
144
            for(i=0;i<num;i++)
145
                block1[random()%64] = (random() % 512) -256;
146
        }break;
147
        case 2:
148
            block1[0]= (random()%4096)-2048;
149
            block1[63]= (block1[0]&1)^1;
150
        break;
151
        }
152

    
153
#if 0 // simulate mismatch control
154
{ int sum=0;
155
        for(i=0;i<64;i++)
156
           sum+=block1[i];
157

158
        if((sum&1)==0) block1[63]^=1;
159
}
160
#endif
161

    
162
        for(i=0; i<64; i++)
163
            block_org[i]= block1[i];
164

    
165
        if (fdct_func == ff_mmx_idct ||
166
            fdct_func == j_rev_dct || fdct_func == ff_mmxext_idct) {
167
            for(i=0;i<64;i++)
168
                block[idct_mmx_perm[i]] = block1[i];
169
        } else if(fdct_func == ff_simple_idct_mmx ) {
170
            for(i=0;i<64;i++)
171
                block[idct_simple_mmx_perm[i]] = block1[i];
172

    
173
        } else {
174
            for(i=0; i<64; i++)
175
                block[i]= block1[i];
176
        }
177
#if 0 // simulate mismatch control for tested IDCT but not the ref
178
{ int sum=0;
179
        for(i=0;i<64;i++)
180
           sum+=block[i];
181

182
        if((sum&1)==0) block[63]^=1;
183
}
184
#endif
185

    
186
        fdct_func(block);
187
        emms_c(); /* for ff_mmx_idct */
188

    
189
        if (fdct_func == fdct_ifast
190
#ifndef FAAN_POSTSCALE
191
            || fdct_func == ff_faandct
192
#endif
193
            ) {
194
            for(i=0; i<64; i++) {
195
                scale = 8*(1 << (AANSCALE_BITS + 11)) / aanscales[i];
196
                block[i] = (block[i] * scale /*+ (1<<(AANSCALE_BITS-1))*/) >> AANSCALE_BITS;
197
            }
198
        }
199

    
200
        fdct_ref(block1);
201

    
202
        blockSumErr=0;
203
        for(i=0;i<64;i++) {
204
            v = abs(block[i] - block1[i]);
205
            if (v > err_inf)
206
                err_inf = v;
207
            err2 += v * v;
208
            sysErr[i] += block[i] - block1[i];
209
            blockSumErr += v;
210
            if( abs(block[i])>maxout) maxout=abs(block[i]);
211
        }
212
        if(blockSumErrMax < blockSumErr) blockSumErrMax= blockSumErr;
213
#if 0 // print different matrix pairs
214
        if(blockSumErr){
215
            printf("\n");
216
            for(i=0; i<64; i++){
217
                if((i&7)==0) printf("\n");
218
                printf("%4d ", block_org[i]);
219
            }
220
            for(i=0; i<64; i++){
221
                if((i&7)==0) printf("\n");
222
                printf("%4d ", block[i] - block1[i]);
223
            }
224
        }
225
#endif
226
    }
227
    for(i=0; i<64; i++) sysErrMax= MAX(sysErrMax, FFABS(sysErr[i]));
228

    
229
#if 1 // dump systematic errors
230
    for(i=0; i<64; i++){
231
        if(i%8==0) printf("\n");
232
        printf("%5d ", (int)sysErr[i]);
233
    }
234
    printf("\n");
235
#endif
236

    
237
    printf("%s %s: err_inf=%d err2=%0.8f syserr=%0.8f maxout=%d blockSumErr=%d\n",
238
           is_idct ? "IDCT" : "DCT",
239
           name, err_inf, (double)err2 / NB_ITS / 64.0, (double)sysErrMax / NB_ITS, maxout, blockSumErrMax);
240
#if 1 //Speed test
241
    /* speed test */
242
    for(i=0;i<64;i++)
243
        block1[i] = 0;
244
    switch(test){
245
    case 0:
246
        for(i=0;i<64;i++)
247
            block1[i] = (random() % 512) -256;
248
        if (is_idct){
249
            fdct(block1);
250

    
251
            for(i=0;i<64;i++)
252
                block1[i]>>=3;
253
        }
254
    break;
255
    case 1:{
256
    case 2:
257
        block1[0] = (random() % 512) -256;
258
        block1[1] = (random() % 512) -256;
259
        block1[2] = (random() % 512) -256;
260
        block1[3] = (random() % 512) -256;
261
    }break;
262
    }
263

    
264
    if (fdct_func == ff_mmx_idct ||
265
        fdct_func == j_rev_dct || fdct_func == ff_mmxext_idct) {
266
        for(i=0;i<64;i++)
267
            block[idct_mmx_perm[i]] = block1[i];
268
    } else if(fdct_func == ff_simple_idct_mmx ) {
269
        for(i=0;i<64;i++)
270
            block[idct_simple_mmx_perm[i]] = block1[i];
271
    } else {
272
        for(i=0; i<64; i++)
273
            block[i]= block1[i];
274
    }
275

    
276
    ti = gettime();
277
    it1 = 0;
278
    do {
279
        for(it=0;it<NB_ITS_SPEED;it++) {
280
            for(i=0; i<64; i++)
281
                block[i]= block1[i];
282
//            memcpy(block, block1, sizeof(DCTELEM) * 64);
283
// dont memcpy especially not fastmemcpy because it does movntq !!!
284
            fdct_func(block);
285
        }
286
        it1 += NB_ITS_SPEED;
287
        ti1 = gettime() - ti;
288
    } while (ti1 < 1000000);
289
    emms_c();
290

    
291
    printf("%s %s: %0.1f kdct/s\n",
292
           is_idct ? "IDCT" : "DCT",
293
           name, (double)it1 * 1000.0 / (double)ti1);
294
#endif
295
}
296

    
297
static uint8_t img_dest[64] __attribute__ ((aligned (8)));
298
static uint8_t img_dest1[64] __attribute__ ((aligned (8)));
299

    
300
void idct248_ref(uint8_t *dest, int linesize, int16_t *block)
301
{
302
    static int init;
303
    static double c8[8][8];
304
    static double c4[4][4];
305
    double block1[64], block2[64], block3[64];
306
    double s, sum, v;
307
    int i, j, k;
308

    
309
    if (!init) {
310
        init = 1;
311

    
312
        for(i=0;i<8;i++) {
313
            sum = 0;
314
            for(j=0;j<8;j++) {
315
                s = (i==0) ? sqrt(1.0/8.0) : sqrt(1.0/4.0);
316
                c8[i][j] = s * cos(M_PI * i * (j + 0.5) / 8.0);
317
                sum += c8[i][j] * c8[i][j];
318
            }
319
        }
320

    
321
        for(i=0;i<4;i++) {
322
            sum = 0;
323
            for(j=0;j<4;j++) {
324
                s = (i==0) ? sqrt(1.0/4.0) : sqrt(1.0/2.0);
325
                c4[i][j] = s * cos(M_PI * i * (j + 0.5) / 4.0);
326
                sum += c4[i][j] * c4[i][j];
327
            }
328
        }
329
    }
330

    
331
    /* butterfly */
332
    s = 0.5 * sqrt(2.0);
333
    for(i=0;i<4;i++) {
334
        for(j=0;j<8;j++) {
335
            block1[8*(2*i)+j] = (block[8*(2*i)+j] + block[8*(2*i+1)+j]) * s;
336
            block1[8*(2*i+1)+j] = (block[8*(2*i)+j] - block[8*(2*i+1)+j]) * s;
337
        }
338
    }
339

    
340
    /* idct8 on lines */
341
    for(i=0;i<8;i++) {
342
        for(j=0;j<8;j++) {
343
            sum = 0;
344
            for(k=0;k<8;k++)
345
                sum += c8[k][j] * block1[8*i+k];
346
            block2[8*i+j] = sum;
347
        }
348
    }
349

    
350
    /* idct4 */
351
    for(i=0;i<8;i++) {
352
        for(j=0;j<4;j++) {
353
            /* top */
354
            sum = 0;
355
            for(k=0;k<4;k++)
356
                sum += c4[k][j] * block2[8*(2*k)+i];
357
            block3[8*(2*j)+i] = sum;
358

    
359
            /* bottom */
360
            sum = 0;
361
            for(k=0;k<4;k++)
362
                sum += c4[k][j] * block2[8*(2*k+1)+i];
363
            block3[8*(2*j+1)+i] = sum;
364
        }
365
    }
366

    
367
    /* clamp and store the result */
368
    for(i=0;i<8;i++) {
369
        for(j=0;j<8;j++) {
370
            v = block3[8*i+j];
371
            if (v < 0)
372
                v = 0;
373
            else if (v > 255)
374
                v = 255;
375
            dest[i * linesize + j] = (int)rint(v);
376
        }
377
    }
378
}
379

    
380
void idct248_error(const char *name,
381
                    void (*idct248_put)(uint8_t *dest, int line_size, int16_t *block))
382
{
383
    int it, i, it1, ti, ti1, err_max, v;
384

    
385
    srandom(0);
386

    
387
    /* just one test to see if code is correct (precision is less
388
       important here) */
389
    err_max = 0;
390
    for(it=0;it<NB_ITS;it++) {
391

    
392
        /* XXX: use forward transform to generate values */
393
        for(i=0;i<64;i++)
394
            block1[i] = (random() % 256) - 128;
395
        block1[0] += 1024;
396

    
397
        for(i=0; i<64; i++)
398
            block[i]= block1[i];
399
        idct248_ref(img_dest1, 8, block);
400

    
401
        for(i=0; i<64; i++)
402
            block[i]= block1[i];
403
        idct248_put(img_dest, 8, block);
404

    
405
        for(i=0;i<64;i++) {
406
            v = abs((int)img_dest[i] - (int)img_dest1[i]);
407
            if (v == 255)
408
                printf("%d %d\n", img_dest[i], img_dest1[i]);
409
            if (v > err_max)
410
                err_max = v;
411
        }
412
#if 0
413
        printf("ref=\n");
414
        for(i=0;i<8;i++) {
415
            int j;
416
            for(j=0;j<8;j++) {
417
                printf(" %3d", img_dest1[i*8+j]);
418
            }
419
            printf("\n");
420
        }
421

422
        printf("out=\n");
423
        for(i=0;i<8;i++) {
424
            int j;
425
            for(j=0;j<8;j++) {
426
                printf(" %3d", img_dest[i*8+j]);
427
            }
428
            printf("\n");
429
        }
430
#endif
431
    }
432
    printf("%s %s: err_inf=%d\n",
433
           1 ? "IDCT248" : "DCT248",
434
           name, err_max);
435

    
436
    ti = gettime();
437
    it1 = 0;
438
    do {
439
        for(it=0;it<NB_ITS_SPEED;it++) {
440
            for(i=0; i<64; i++)
441
                block[i]= block1[i];
442
//            memcpy(block, block1, sizeof(DCTELEM) * 64);
443
// dont memcpy especially not fastmemcpy because it does movntq !!!
444
            idct248_put(img_dest, 8, block);
445
        }
446
        it1 += NB_ITS_SPEED;
447
        ti1 = gettime() - ti;
448
    } while (ti1 < 1000000);
449
    emms_c();
450

    
451
    printf("%s %s: %0.1f kdct/s\n",
452
           1 ? "IDCT248" : "DCT248",
453
           name, (double)it1 * 1000.0 / (double)ti1);
454
}
455

    
456
void help(void)
457
{
458
    printf("dct-test [-i] [<test-number>]\n"
459
           "test-number 0 -> test with random matrixes\n"
460
           "            1 -> test with random sparse matrixes\n"
461
           "            2 -> do 3. test from mpeg4 std\n"
462
           "-i          test IDCT implementations\n"
463
           "-4          test IDCT248 implementations\n");
464
    exit(1);
465
}
466

    
467
int main(int argc, char **argv)
468
{
469
    int test_idct = 0, test_248_dct = 0;
470
    int c,i;
471
    int test=1;
472

    
473
    init_fdct();
474
    idct_mmx_init();
475

    
476
    for(i=0;i<256;i++) cropTbl[i + MAX_NEG_CROP] = i;
477
    for(i=0;i<MAX_NEG_CROP;i++) {
478
        cropTbl[i] = 0;
479
        cropTbl[i + MAX_NEG_CROP + 256] = 255;
480
    }
481

    
482
    for(;;) {
483
        c = getopt(argc, argv, "ih4");
484
        if (c == -1)
485
            break;
486
        switch(c) {
487
        case 'i':
488
            test_idct = 1;
489
            break;
490
        case '4':
491
            test_248_dct = 1;
492
            break;
493
        default :
494
        case 'h':
495
            help();
496
            break;
497
        }
498
    }
499

    
500
    if(optind <argc) test= atoi(argv[optind]);
501

    
502
    printf("ffmpeg DCT/IDCT test\n");
503

    
504
    if (test_248_dct) {
505
        idct248_error("SIMPLE-C", simple_idct248_put);
506
    } else {
507
        if (!test_idct) {
508
            dct_error("REF-DBL", 0, fdct, fdct, test); /* only to verify code ! */
509
            dct_error("IJG-AAN-INT", 0, fdct_ifast, fdct, test);
510
            dct_error("IJG-LLM-INT", 0, ff_jpeg_fdct_islow, fdct, test);
511
            dct_error("MMX", 0, ff_fdct_mmx, fdct, test);
512
            dct_error("MMX2", 0, ff_fdct_mmx2, fdct, test);
513
            dct_error("FAAN", 0, ff_faandct, fdct, test);
514
        } else {
515
            dct_error("REF-DBL", 1, idct, idct, test);
516
            dct_error("INT", 1, j_rev_dct, idct, test);
517
            dct_error("LIBMPEG2-MMX", 1, ff_mmx_idct, idct, test);
518
            dct_error("LIBMPEG2-MMXEXT", 1, ff_mmxext_idct, idct, test);
519
            dct_error("SIMPLE-C", 1, simple_idct, idct, test);
520
            dct_error("SIMPLE-MMX", 1, ff_simple_idct_mmx, idct, test);
521
            dct_error("XVID-MMX", 1, ff_idct_xvid_mmx, idct, test);
522
            dct_error("XVID-MMX2", 1, ff_idct_xvid_mmx2, idct, test);
523
            //        dct_error("ODIVX-C", 1, odivx_idct_c, idct);
524
            //printf(" test against odivx idct\n");
525
            //        dct_error("REF", 1, idct, odivx_idct_c);
526
            //        dct_error("INT", 1, j_rev_dct, odivx_idct_c);
527
            //        dct_error("MMX", 1, ff_mmx_idct, odivx_idct_c);
528
            //        dct_error("MMXEXT", 1, ff_mmxext_idct, odivx_idct_c);
529
            //        dct_error("SIMPLE-C", 1, simple_idct, odivx_idct_c);
530
            //        dct_error("SIMPLE-MMX", 1, ff_simple_idct_mmx, odivx_idct_c);
531
            //        dct_error("ODIVX-C", 1, odivx_idct_c, odivx_idct_c);
532
        }
533
    }
534
    return 0;
535
}