ismindex: Avoid writing ismf files if no base name has been specified
[libav.git] / libavcodec / dct-test.c
CommitLineData
04d7f601
DB
1/*
2 * (c) 2001 Fabrice Bellard
3ac35bdb 3 * 2007 Marc Hoffman <marc.hoffman@analog.com>
04d7f601 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
04d7f601
DB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
04d7f601 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
04d7f601
DB
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
04d7f601
DB
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
983e3246 22/**
ba87f080 23 * @file
94f694a4 24 * DCT test (c) 2001 Fabrice Bellard
983e3246
MN
25 * Started from sample code by Juan J. Sierralta P.
26 */
27
667fb97a 28#include "config.h"
de6d9b64
FB
29#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
667fb97a 32#if HAVE_UNISTD_H
de6d9b64 33#include <unistd.h>
667fb97a 34#endif
12807c8d 35#include <math.h>
de6d9b64 36
c6c98d08 37#include "libavutil/cpu.h"
ae32e509 38#include "libavutil/common.h"
294eaa26 39#include "libavutil/lfg.h"
980f81d9 40#include "libavutil/time.h"
de6d9b64 41
5d3d39c7 42#include "dct.h"
e0a2e60c 43#include "idctdsp.h"
86748dbc 44#include "simple_idct.h"
7a1d6ddd 45#include "xvididct.h"
10ac3618 46#include "aandcttab.h"
65e4c8c9 47#include "faandct.h"
6f08c541 48#include "faanidct.h"
6a813295 49#include "dctref.h"
9e1586fc 50
3ac35bdb 51struct algo {
36fa9ef3 52 const char *name;
88bd7fdc 53 void (*func)(int16_t *block);
e0a2e60c 54 enum idct_permutation_type perm_type;
746ad4e0 55 int cpu_flag;
dbf396d4 56 int nonspec;
3ac35bdb
MH
57};
58
81b9bf31 59static const struct algo fdct_tab[4] = {
e0a2e60c
DB
60 { "REF-DBL", ff_ref_fdct, FF_IDCT_PERM_NONE },
61 { "FAAN", ff_faandct, FF_IDCT_PERM_NONE },
62 { "IJG-AAN-INT", ff_fdct_ifast, FF_IDCT_PERM_NONE },
63 { "IJG-LLM-INT", ff_jpeg_fdct_islow_8, FF_IDCT_PERM_NONE },
4b357756
MR
64};
65
7a1d6ddd 66static const struct algo idct_tab[] = {
e0a2e60c
DB
67 { "FAANI", ff_faanidct, FF_IDCT_PERM_NONE },
68 { "REF-DBL", ff_ref_idct, FF_IDCT_PERM_NONE },
69 { "INT", ff_j_rev_dct, FF_IDCT_PERM_LIBMPEG2 },
70 { "SIMPLE-C", ff_simple_idct_8, FF_IDCT_PERM_NONE },
7a1d6ddd
PM
71#if CONFIG_MPEG4_DECODER
72 { "XVID", ff_xvid_idct, FF_IDCT_PERM_NONE, 0, 1 },
73#endif /* CONFIG_MPEG4_DECODER */
81b9bf31 74};
3ac35bdb 75
b250f9c6 76#if ARCH_ARM
81b9bf31
DB
77#include "arm/dct-test.c"
78#elif ARCH_PPC
79#include "ppc/dct-test.c"
80#elif ARCH_X86
81#include "x86/dct-test.c"
82#else
83static const struct algo fdct_tab_arch[] = { 0 };
84static const struct algo idct_tab_arch[] = { 0 };
479044ce 85#endif
479044ce 86
de6d9b64 87#define AANSCALE_BITS 12
de6d9b64 88
de6d9b64
FB
89#define NB_ITS 20000
90#define NB_ITS_SPEED 50000
91
88bd7fdc
DB
92DECLARE_ALIGNED(16, static int16_t, block)[64];
93DECLARE_ALIGNED(8, static int16_t, block1)[64];
9e1586fc 94
88bd7fdc 95static void init_block(int16_t block[64], int test, int is_idct, AVLFG *prng)
ae2e8971
MR
96{
97 int i, j;
98
99 memset(block, 0, 64 * sizeof(*block));
100
101 switch (test) {
102 case 0:
103 for (i = 0; i < 64; i++)
104 block[i] = (av_lfg_get(prng) % 512) - 256;
105 if (is_idct) {
106 ff_ref_fdct(block);
107 for (i = 0; i < 64; i++)
108 block[i] >>= 3;
109 }
110 break;
111 case 1:
112 j = av_lfg_get(prng) % 10 + 1;
113 for (i = 0; i < j; i++)
114 block[av_lfg_get(prng) % 64] = av_lfg_get(prng) % 512 - 256;
115 break;
116 case 2:
117 block[ 0] = av_lfg_get(prng) % 4096 - 2048;
118 block[63] = (block[0] & 1) ^ 1;
119 break;
120 }
121}
122
e0a2e60c
DB
123static void permute(int16_t dst[64], const int16_t src[64],
124 enum idct_permutation_type perm_type)
ae2e8971
MR
125{
126 int i;
127
81b9bf31
DB
128#if ARCH_X86
129 if (permute_x86(dst, src, perm_type))
130 return;
131#endif
132
e0a2e60c
DB
133 switch (perm_type) {
134 case FF_IDCT_PERM_LIBMPEG2:
ae2e8971 135 for (i = 0; i < 64; i++)
913fa85a 136 dst[(i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2)] = src[i];
e0a2e60c 137 break;
e0a2e60c 138 case FF_IDCT_PERM_PARTTRANS:
ae2e8971
MR
139 for (i = 0; i < 64; i++)
140 dst[(i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3)] = src[i];
e0a2e60c
DB
141 break;
142 default:
ae2e8971
MR
143 for (i = 0; i < 64; i++)
144 dst[i] = src[i];
e0a2e60c 145 break;
ae2e8971
MR
146 }
147}
148
dbf396d4 149static int dct_error(const struct algo *dct, int test, int is_idct, int speed)
de6d9b64 150{
88bd7fdc 151 void (*ref)(int16_t *block) = is_idct ? ff_ref_idct : ff_ref_fdct;
de6d9b64 152 int it, i, scale;
de6d9b64 153 int err_inf, v;
dbf396d4 154 int64_t err2, ti, ti1, it1, err_sum = 0;
36fa9ef3
MR
155 int64_t sysErr[64], sysErrMax = 0;
156 int maxout = 0;
157 int blockSumErrMax = 0, blockSumErr;
64bde197 158 AVLFG prng;
dbf396d4
MR
159 double omse, ome;
160 int spec_err;
de6d9b64 161
64bde197 162 av_lfg_init(&prng, 1);
de6d9b64
FB
163
164 err_inf = 0;
165 err2 = 0;
36fa9ef3
MR
166 for (i = 0; i < 64; i++)
167 sysErr[i] = 0;
168 for (it = 0; it < NB_ITS; it++) {
ae2e8971 169 init_block(block1, test, is_idct, &prng);
e0a2e60c 170 permute(block, block1, dct->perm_type);
9e1586fc 171
4f905a65 172 dct->func(block);
db7d8fb4 173 emms_c();
9e1586fc 174
e0a2e60c 175 if (!strcmp(dct->name, "IJG-AAN-INT")) {
36fa9ef3
MR
176 for (i = 0; i < 64; i++) {
177 scale = 8 * (1 << (AANSCALE_BITS + 11)) / ff_aanscales[i];
178 block[i] = (block[i] * scale) >> AANSCALE_BITS;
86748dbc
MN
179 }
180 }
181
74965f26 182 ref(block1);
de6d9b64 183
36fa9ef3
MR
184 blockSumErr = 0;
185 for (i = 0; i < 64; i++) {
dbf396d4
MR
186 int err = block[i] - block1[i];
187 err_sum += err;
188 v = abs(err);
de6d9b64
FB
189 if (v > err_inf)
190 err_inf = v;
191 err2 += v * v;
bb270c08
DB
192 sysErr[i] += block[i] - block1[i];
193 blockSumErr += v;
36fa9ef3
MR
194 if (abs(block[i]) > maxout)
195 maxout = abs(block[i]);
de6d9b64 196 }
36fa9ef3
MR
197 if (blockSumErrMax < blockSumErr)
198 blockSumErrMax = blockSumErr;
86748dbc 199 }
36fa9ef3
MR
200 for (i = 0; i < 64; i++)
201 sysErrMax = FFMAX(sysErrMax, FFABS(sysErr[i]));
115329f1 202
36fa9ef3
MR
203 for (i = 0; i < 64; i++) {
204 if (i % 8 == 0)
205 printf("\n");
206 printf("%7d ", (int) sysErr[i]);
de6d9b64 207 }
86748dbc 208 printf("\n");
115329f1 209
dbf396d4
MR
210 omse = (double) err2 / NB_ITS / 64;
211 ome = (double) err_sum / NB_ITS / 64;
212
213 spec_err = is_idct && (err_inf > 1 || omse > 0.02 || fabs(ome) > 0.0015);
214
215 printf("%s %s: ppe=%d omse=%0.8f ome=%0.8f syserr=%0.8f maxout=%d blockSumErr=%d\n",
cf2b4f88 216 is_idct ? "IDCT" : "DCT", dct->name, err_inf,
dbf396d4 217 omse, ome, (double) sysErrMax / NB_ITS,
36fa9ef3 218 maxout, blockSumErrMax);
e6ff0648 219
dbf396d4
MR
220 if (spec_err && !dct->nonspec)
221 return 1;
222
7fd2c138 223 if (!speed)
dbf396d4 224 return 0;
7fd2c138 225
de6d9b64 226 /* speed test */
ae2e8971 227 init_block(block, test, is_idct, &prng);
e0a2e60c 228 permute(block1, block, dct->perm_type);
9e1586fc 229
980f81d9 230 ti = av_gettime();
de6d9b64
FB
231 it1 = 0;
232 do {
36fa9ef3 233 for (it = 0; it < NB_ITS_SPEED; it++) {
ae2e8971 234 memcpy(block, block1, sizeof(block));
4f905a65 235 dct->func(block);
de6d9b64
FB
236 }
237 it1 += NB_ITS_SPEED;
980f81d9 238 ti1 = av_gettime() - ti;
de6d9b64 239 } while (ti1 < 1000000);
db7d8fb4 240 emms_c();
de6d9b64 241
cf2b4f88 242 printf("%s %s: %0.1f kdct/s\n", is_idct ? "IDCT" : "DCT", dct->name,
36fa9ef3 243 (double) it1 * 1000.0 / (double) ti1);
dbf396d4
MR
244
245 return 0;
de6d9b64
FB
246}
247
c6727809
MR
248DECLARE_ALIGNED(8, static uint8_t, img_dest)[64];
249DECLARE_ALIGNED(8, static uint8_t, img_dest1)[64];
a46a3ce4 250
504ffed1 251static void idct248_ref(uint8_t *dest, int linesize, int16_t *block)
a46a3ce4
FB
252{
253 static int init;
254 static double c8[8][8];
255 static double c4[4][4];
256 double block1[64], block2[64], block3[64];
257 double s, sum, v;
258 int i, j, k;
259
260 if (!init) {
261 init = 1;
262
36fa9ef3 263 for (i = 0; i < 8; i++) {
a46a3ce4 264 sum = 0;
36fa9ef3
MR
265 for (j = 0; j < 8; j++) {
266 s = (i == 0) ? sqrt(1.0 / 8.0) : sqrt(1.0 / 4.0);
a46a3ce4
FB
267 c8[i][j] = s * cos(M_PI * i * (j + 0.5) / 8.0);
268 sum += c8[i][j] * c8[i][j];
269 }
270 }
115329f1 271
36fa9ef3 272 for (i = 0; i < 4; i++) {
a46a3ce4 273 sum = 0;
36fa9ef3
MR
274 for (j = 0; j < 4; j++) {
275 s = (i == 0) ? sqrt(1.0 / 4.0) : sqrt(1.0 / 2.0);
a46a3ce4
FB
276 c4[i][j] = s * cos(M_PI * i * (j + 0.5) / 4.0);
277 sum += c4[i][j] * c4[i][j];
278 }
279 }
280 }
281
282 /* butterfly */
652f0197 283 s = 0.5 * sqrt(2.0);
36fa9ef3
MR
284 for (i = 0; i < 4; i++) {
285 for (j = 0; j < 8; j++) {
286 block1[8 * (2 * i) + j] =
287 (block[8 * (2 * i) + j] + block[8 * (2 * i + 1) + j]) * s;
288 block1[8 * (2 * i + 1) + j] =
289 (block[8 * (2 * i) + j] - block[8 * (2 * i + 1) + j]) * s;
a46a3ce4
FB
290 }
291 }
292
293 /* idct8 on lines */
36fa9ef3
MR
294 for (i = 0; i < 8; i++) {
295 for (j = 0; j < 8; j++) {
a46a3ce4 296 sum = 0;
36fa9ef3
MR
297 for (k = 0; k < 8; k++)
298 sum += c8[k][j] * block1[8 * i + k];
299 block2[8 * i + j] = sum;
a46a3ce4
FB
300 }
301 }
302
303 /* idct4 */
36fa9ef3
MR
304 for (i = 0; i < 8; i++) {
305 for (j = 0; j < 4; j++) {
a46a3ce4
FB
306 /* top */
307 sum = 0;
36fa9ef3
MR
308 for (k = 0; k < 4; k++)
309 sum += c4[k][j] * block2[8 * (2 * k) + i];
310 block3[8 * (2 * j) + i] = sum;
a46a3ce4
FB
311
312 /* bottom */
313 sum = 0;
36fa9ef3
MR
314 for (k = 0; k < 4; k++)
315 sum += c4[k][j] * block2[8 * (2 * k + 1) + i];
316 block3[8 * (2 * j + 1) + i] = sum;
a46a3ce4
FB
317 }
318 }
319
320 /* clamp and store the result */
36fa9ef3
MR
321 for (i = 0; i < 8; i++) {
322 for (j = 0; j < 8; j++) {
323 v = block3[8 * i + j];
324 if (v < 0) v = 0;
325 else if (v > 255) v = 255;
326 dest[i * linesize + j] = (int) rint(v);
a46a3ce4
FB
327 }
328 }
329}
330
504ffed1 331static void idct248_error(const char *name,
36fa9ef3 332 void (*idct248_put)(uint8_t *dest, int line_size,
7fd2c138
MR
333 int16_t *block),
334 int speed)
a46a3ce4
FB
335{
336 int it, i, it1, ti, ti1, err_max, v;
64bde197 337 AVLFG prng;
294eaa26 338
64bde197 339 av_lfg_init(&prng, 1);
115329f1 340
a46a3ce4
FB
341 /* just one test to see if code is correct (precision is less
342 important here) */
343 err_max = 0;
36fa9ef3 344 for (it = 0; it < NB_ITS; it++) {
652f0197 345 /* XXX: use forward transform to generate values */
36fa9ef3 346 for (i = 0; i < 64; i++)
64bde197 347 block1[i] = av_lfg_get(&prng) % 256 - 128;
652f0197
FB
348 block1[0] += 1024;
349
36fa9ef3
MR
350 for (i = 0; i < 64; i++)
351 block[i] = block1[i];
a46a3ce4 352 idct248_ref(img_dest1, 8, block);
115329f1 353
36fa9ef3
MR
354 for (i = 0; i < 64; i++)
355 block[i] = block1[i];
652f0197 356 idct248_put(img_dest, 8, block);
115329f1 357
36fa9ef3
MR
358 for (i = 0; i < 64; i++) {
359 v = abs((int) img_dest[i] - (int) img_dest1[i]);
652f0197
FB
360 if (v == 255)
361 printf("%d %d\n", img_dest[i], img_dest1[i]);
362 if (v > err_max)
363 err_max = v;
364 }
a46a3ce4 365 }
36fa9ef3 366 printf("%s %s: err_inf=%d\n", 1 ? "IDCT248" : "DCT248", name, err_max);
a46a3ce4 367
7fd2c138
MR
368 if (!speed)
369 return;
370
980f81d9 371 ti = av_gettime();
a46a3ce4
FB
372 it1 = 0;
373 do {
36fa9ef3
MR
374 for (it = 0; it < NB_ITS_SPEED; it++) {
375 for (i = 0; i < 64; i++)
376 block[i] = block1[i];
a46a3ce4
FB
377 idct248_put(img_dest, 8, block);
378 }
379 it1 += NB_ITS_SPEED;
980f81d9 380 ti1 = av_gettime() - ti;
a46a3ce4 381 } while (ti1 < 1000000);
db7d8fb4 382 emms_c();
a46a3ce4 383
36fa9ef3
MR
384 printf("%s %s: %0.1f kdct/s\n", 1 ? "IDCT248" : "DCT248", name,
385 (double) it1 * 1000.0 / (double) ti1);
a46a3ce4
FB
386}
387
504ffed1 388static void help(void)
9e1586fc 389{
86748dbc
MN
390 printf("dct-test [-i] [<test-number>]\n"
391 "test-number 0 -> test with random matrixes\n"
392 " 1 -> test with random sparse matrixes\n"
393 " 2 -> do 3. test from mpeg4 std\n"
a46a3ce4 394 "-i test IDCT implementations\n"
7fd2c138
MR
395 "-4 test IDCT248 implementations\n"
396 "-t speed test\n");
9e1586fc
FB
397}
398
667fb97a
RB
399#if !HAVE_GETOPT
400#include "compat/getopt.c"
401#endif
402
de6d9b64
FB
403int main(int argc, char **argv)
404{
a46a3ce4 405 int test_idct = 0, test_248_dct = 0;
36fa9ef3
MR
406 int c, i;
407 int test = 1;
7fd2c138 408 int speed = 0;
dbf396d4 409 int err = 0;
36fa9ef3 410
0de74546 411 ff_ref_dct_init();
f67a10cd 412
36fa9ef3 413 for (;;) {
7fd2c138 414 c = getopt(argc, argv, "ih4t");
9e1586fc
FB
415 if (c == -1)
416 break;
36fa9ef3 417 switch (c) {
9e1586fc
FB
418 case 'i':
419 test_idct = 1;
420 break;
a46a3ce4
FB
421 case '4':
422 test_248_dct = 1;
423 break;
7fd2c138
MR
424 case 't':
425 speed = 1;
426 break;
36fa9ef3 427 default:
9e1586fc
FB
428 case 'h':
429 help();
c6bdc908 430 return 0;
9e1586fc
FB
431 }
432 }
115329f1 433
36fa9ef3
MR
434 if (optind < argc)
435 test = atoi(argv[optind]);
115329f1 436
f36b3902 437 printf("Libav DCT/IDCT test\n");
9e1586fc 438
a46a3ce4 439 if (test_248_dct) {
7fd2c138 440 idct248_error("SIMPLE-C", ff_simple_idct248_put, speed);
9e1586fc 441 } else {
cb44b21d 442 const int cpu_flags = av_get_cpu_flags();
81b9bf31
DB
443 if (test_idct) {
444 for (i = 0; i < FF_ARRAY_ELEMS(idct_tab); i++)
445 err |= dct_error(&idct_tab[i], test, test_idct, speed);
446
447 for (i = 0; idct_tab_arch[i].name; i++)
448 if (!(~cpu_flags & idct_tab_arch[i].cpu_flag))
449 err |= dct_error(&idct_tab_arch[i], test, test_idct, speed);
16b73280
DB
450 }
451#if CONFIG_FDCTDSP
452 else {
81b9bf31
DB
453 for (i = 0; i < FF_ARRAY_ELEMS(fdct_tab); i++)
454 err |= dct_error(&fdct_tab[i], test, test_idct, speed);
455
456 for (i = 0; fdct_tab_arch[i].name; i++)
457 if (!(~cpu_flags & fdct_tab_arch[i].cpu_flag))
458 err |= dct_error(&fdct_tab_arch[i], test, test_idct, speed);
459 }
16b73280 460#endif /* CONFIG_FDCTDSP */
9e1586fc 461 }
dbf396d4 462
5331d2b9
DB
463 if (err)
464 printf("Error: %d.\n", err);
465
466 return !!err;
de6d9b64 467}