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