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