From a6a750c7ef240b72ce01e9653343a0ddf247d196 Mon Sep 17 00:00:00 2001 From: Diego Biurrun Date: Wed, 20 Apr 2016 11:40:40 +0200 Subject: tests: Move all test programs to a subdirectory --- libavcodec/tests/.gitignore | 6 + libavcodec/tests/arm/dct.c | 40 ++++ libavcodec/tests/dct.c | 472 ++++++++++++++++++++++++++++++++++++++ libavcodec/tests/fft-fixed.c | 20 ++ libavcodec/tests/fft.c | 513 ++++++++++++++++++++++++++++++++++++++++++ libavcodec/tests/golomb.c | 97 ++++++++ libavcodec/tests/iirfilter.c | 54 +++++ libavcodec/tests/ppc/dct.c | 32 +++ libavcodec/tests/rangecoder.c | 64 ++++++ libavcodec/tests/x86/dct.c | 86 +++++++ 10 files changed, 1384 insertions(+) create mode 100644 libavcodec/tests/.gitignore create mode 100644 libavcodec/tests/arm/dct.c create mode 100644 libavcodec/tests/dct.c create mode 100644 libavcodec/tests/fft-fixed.c create mode 100644 libavcodec/tests/fft.c create mode 100644 libavcodec/tests/golomb.c create mode 100644 libavcodec/tests/iirfilter.c create mode 100644 libavcodec/tests/ppc/dct.c create mode 100644 libavcodec/tests/rangecoder.c create mode 100644 libavcodec/tests/x86/dct.c (limited to 'libavcodec/tests') diff --git a/libavcodec/tests/.gitignore b/libavcodec/tests/.gitignore new file mode 100644 index 0000000000..31fa59b018 --- /dev/null +++ b/libavcodec/tests/.gitignore @@ -0,0 +1,6 @@ +/dct +/fft +/fft-fixed +/golomb +/iirfilter +/rangecoder diff --git a/libavcodec/tests/arm/dct.c b/libavcodec/tests/arm/dct.c new file mode 100644 index 0000000000..d18cb52b60 --- /dev/null +++ b/libavcodec/tests/arm/dct.c @@ -0,0 +1,40 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include "libavcodec/arm/idct.h" + +static const struct algo fdct_tab_arch[] = { + { 0 } +}; + +static const struct algo idct_tab_arch[] = { + { "SIMPLE-ARM", ff_simple_idct_arm, FF_IDCT_PERM_NONE }, + { "INT-ARM", ff_j_rev_dct_arm, FF_IDCT_PERM_LIBMPEG2 }, +#if HAVE_ARMV5TE + { "SIMPLE-ARMV5TE", ff_simple_idct_armv5te, FF_IDCT_PERM_NONE, AV_CPU_FLAG_ARMV5TE }, +#endif +#if HAVE_ARMV6 + { "SIMPLE-ARMV6", ff_simple_idct_armv6, FF_IDCT_PERM_LIBMPEG2, AV_CPU_FLAG_ARMV6 }, +#endif +#if HAVE_NEON + { "SIMPLE-NEON", ff_simple_idct_neon, FF_IDCT_PERM_PARTTRANS, AV_CPU_FLAG_NEON }, +#endif + { 0 } +}; diff --git a/libavcodec/tests/dct.c b/libavcodec/tests/dct.c new file mode 100644 index 0000000000..6c6a4e0d0d --- /dev/null +++ b/libavcodec/tests/dct.c @@ -0,0 +1,472 @@ +/* + * (c) 2001 Fabrice Bellard + * 2007 Marc Hoffman + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * DCT test (c) 2001 Fabrice Bellard + * Started from sample code by Juan J. Sierralta P. + */ + +#include "config.h" +#include +#include +#include +#if HAVE_UNISTD_H +#include +#endif +#include + +#include "libavutil/cpu.h" +#include "libavutil/common.h" +#include "libavutil/internal.h" +#include "libavutil/lfg.h" +#include "libavutil/time.h" + +#include "libavcodec/aandcttab.h" +#include "libavcodec/dct.h" +#include "libavcodec/dctref.h" +#include "libavcodec/faandct.h" +#include "libavcodec/faanidct.h" +#include "libavcodec/idctdsp.h" +#include "libavcodec/simple_idct.h" +#include "libavcodec/xvididct.h" + +struct algo { + const char *name; + void (*func)(int16_t *block); + enum idct_permutation_type perm_type; + int cpu_flag; + int nonspec; +}; + +static const struct algo fdct_tab[] = { + { "REF-DBL", ff_ref_fdct, FF_IDCT_PERM_NONE }, + { "IJG-AAN-INT", ff_fdct_ifast, FF_IDCT_PERM_NONE }, + { "IJG-LLM-INT", ff_jpeg_fdct_islow_8, FF_IDCT_PERM_NONE }, +#if CONFIG_FAANDCT + { "FAAN", ff_faandct, FF_IDCT_PERM_NONE }, +#endif /* CONFIG_FAANDCT */ +}; + +static const struct algo idct_tab[] = { + { "REF-DBL", ff_ref_idct, FF_IDCT_PERM_NONE }, + { "INT", ff_j_rev_dct, FF_IDCT_PERM_LIBMPEG2 }, + { "SIMPLE-C", ff_simple_idct_8, FF_IDCT_PERM_NONE }, +#if CONFIG_FAANIDCT + { "FAANI", ff_faanidct, FF_IDCT_PERM_NONE }, +#endif /* CONFIG_FAANIDCT */ +#if CONFIG_MPEG4_DECODER + { "XVID", ff_xvid_idct, FF_IDCT_PERM_NONE, 0, 1 }, +#endif /* CONFIG_MPEG4_DECODER */ +}; + +#if ARCH_ARM +#include "arm/dct.c" +#elif ARCH_PPC +#include "ppc/dct.c" +#elif ARCH_X86 +#include "x86/dct.c" +#else +static const struct algo fdct_tab_arch[] = { { 0 } }; +static const struct algo idct_tab_arch[] = { { 0 } }; +#endif + +#define AANSCALE_BITS 12 + +#define NB_ITS 20000 +#define NB_ITS_SPEED 50000 + +DECLARE_ALIGNED(16, static int16_t, block)[64]; +DECLARE_ALIGNED(8, static int16_t, block1)[64]; + +static void init_block(int16_t block[64], int test, int is_idct, AVLFG *prng) +{ + int i, j; + + memset(block, 0, 64 * sizeof(*block)); + + switch (test) { + case 0: + for (i = 0; i < 64; i++) + block[i] = (av_lfg_get(prng) % 512) - 256; + if (is_idct) { + ff_ref_fdct(block); + for (i = 0; i < 64; i++) + block[i] >>= 3; + } + break; + case 1: + j = av_lfg_get(prng) % 10 + 1; + for (i = 0; i < j; i++) + block[av_lfg_get(prng) % 64] = av_lfg_get(prng) % 512 - 256; + break; + case 2: + block[ 0] = av_lfg_get(prng) % 4096 - 2048; + block[63] = (block[0] & 1) ^ 1; + break; + } +} + +static void permute(int16_t dst[64], const int16_t src[64], + enum idct_permutation_type perm_type) +{ + int i; + +#if ARCH_X86 + if (permute_x86(dst, src, perm_type)) + return; +#endif + + switch (perm_type) { + case FF_IDCT_PERM_LIBMPEG2: + for (i = 0; i < 64; i++) + dst[(i & 0x38) | ((i & 6) >> 1) | ((i & 1) << 2)] = src[i]; + break; + case FF_IDCT_PERM_PARTTRANS: + for (i = 0; i < 64; i++) + dst[(i & 0x24) | ((i & 3) << 3) | ((i >> 3) & 3)] = src[i]; + break; + default: + for (i = 0; i < 64; i++) + dst[i] = src[i]; + break; + } +} + +static int dct_error(const struct algo *dct, int test, int is_idct, int speed) +{ + void (*ref)(int16_t *block) = is_idct ? ff_ref_idct : ff_ref_fdct; + int it, i, scale; + int err_inf, v; + int64_t err2, ti, ti1, it1, err_sum = 0; + int64_t sysErr[64], sysErrMax = 0; + int maxout = 0; + int blockSumErrMax = 0, blockSumErr; + AVLFG prng; + double omse, ome; + int spec_err; + + av_lfg_init(&prng, 1); + + err_inf = 0; + err2 = 0; + for (i = 0; i < 64; i++) + sysErr[i] = 0; + for (it = 0; it < NB_ITS; it++) { + init_block(block1, test, is_idct, &prng); + permute(block, block1, dct->perm_type); + + dct->func(block); + emms_c(); + + if (!strcmp(dct->name, "IJG-AAN-INT")) { + for (i = 0; i < 64; i++) { + scale = 8 * (1 << (AANSCALE_BITS + 11)) / ff_aanscales[i]; + block[i] = (block[i] * scale) >> AANSCALE_BITS; + } + } + + ref(block1); + + blockSumErr = 0; + for (i = 0; i < 64; i++) { + int err = block[i] - block1[i]; + err_sum += err; + v = abs(err); + if (v > err_inf) + err_inf = v; + err2 += v * v; + sysErr[i] += block[i] - block1[i]; + blockSumErr += v; + if (abs(block[i]) > maxout) + maxout = abs(block[i]); + } + if (blockSumErrMax < blockSumErr) + blockSumErrMax = blockSumErr; + } + for (i = 0; i < 64; i++) + sysErrMax = FFMAX(sysErrMax, FFABS(sysErr[i])); + + for (i = 0; i < 64; i++) { + if (i % 8 == 0) + printf("\n"); + printf("%7d ", (int) sysErr[i]); + } + printf("\n"); + + omse = (double) err2 / NB_ITS / 64; + ome = (double) err_sum / NB_ITS / 64; + + spec_err = is_idct && (err_inf > 1 || omse > 0.02 || fabs(ome) > 0.0015); + + printf("%s %s: ppe=%d omse=%0.8f ome=%0.8f syserr=%0.8f maxout=%d blockSumErr=%d\n", + is_idct ? "IDCT" : "DCT", dct->name, err_inf, + omse, ome, (double) sysErrMax / NB_ITS, + maxout, blockSumErrMax); + + if (spec_err && !dct->nonspec) + return 1; + + if (!speed) + return 0; + + /* speed test */ + init_block(block, test, is_idct, &prng); + permute(block1, block, dct->perm_type); + + ti = av_gettime_relative(); + it1 = 0; + do { + for (it = 0; it < NB_ITS_SPEED; it++) { + memcpy(block, block1, sizeof(block)); + dct->func(block); + } + it1 += NB_ITS_SPEED; + ti1 = av_gettime_relative() - ti; + } while (ti1 < 1000000); + emms_c(); + + printf("%s %s: %0.1f kdct/s\n", is_idct ? "IDCT" : "DCT", dct->name, + (double) it1 * 1000.0 / (double) ti1); + + return 0; +} + +DECLARE_ALIGNED(8, static uint8_t, img_dest)[64]; +DECLARE_ALIGNED(8, static uint8_t, img_dest1)[64]; + +static void idct248_ref(uint8_t *dest, int linesize, int16_t *block) +{ + static int init; + static double c8[8][8]; + static double c4[4][4]; + double block1[64], block2[64], block3[64]; + double s, sum, v; + int i, j, k; + + if (!init) { + init = 1; + + for (i = 0; i < 8; i++) { + sum = 0; + for (j = 0; j < 8; j++) { + s = (i == 0) ? sqrt(1.0 / 8.0) : sqrt(1.0 / 4.0); + c8[i][j] = s * cos(M_PI * i * (j + 0.5) / 8.0); + sum += c8[i][j] * c8[i][j]; + } + } + + for (i = 0; i < 4; i++) { + sum = 0; + for (j = 0; j < 4; j++) { + s = (i == 0) ? sqrt(1.0 / 4.0) : sqrt(1.0 / 2.0); + c4[i][j] = s * cos(M_PI * i * (j + 0.5) / 4.0); + sum += c4[i][j] * c4[i][j]; + } + } + } + + /* butterfly */ + s = 0.5 * sqrt(2.0); + for (i = 0; i < 4; i++) { + for (j = 0; j < 8; j++) { + block1[8 * (2 * i) + j] = + (block[8 * (2 * i) + j] + block[8 * (2 * i + 1) + j]) * s; + block1[8 * (2 * i + 1) + j] = + (block[8 * (2 * i) + j] - block[8 * (2 * i + 1) + j]) * s; + } + } + + /* idct8 on lines */ + for (i = 0; i < 8; i++) { + for (j = 0; j < 8; j++) { + sum = 0; + for (k = 0; k < 8; k++) + sum += c8[k][j] * block1[8 * i + k]; + block2[8 * i + j] = sum; + } + } + + /* idct4 */ + for (i = 0; i < 8; i++) { + for (j = 0; j < 4; j++) { + /* top */ + sum = 0; + for (k = 0; k < 4; k++) + sum += c4[k][j] * block2[8 * (2 * k) + i]; + block3[8 * (2 * j) + i] = sum; + + /* bottom */ + sum = 0; + for (k = 0; k < 4; k++) + sum += c4[k][j] * block2[8 * (2 * k + 1) + i]; + block3[8 * (2 * j + 1) + i] = sum; + } + } + + /* clamp and store the result */ + for (i = 0; i < 8; i++) { + for (j = 0; j < 8; j++) { + v = block3[8 * i + j]; + if (v < 0) v = 0; + else if (v > 255) v = 255; + dest[i * linesize + j] = (int) rint(v); + } + } +} + +static void idct248_error(const char *name, + void (*idct248_put)(uint8_t *dest, int line_size, + int16_t *block), + int speed) +{ + int it, i, it1, ti, ti1, err_max, v; + AVLFG prng; + + av_lfg_init(&prng, 1); + + /* just one test to see if code is correct (precision is less + important here) */ + err_max = 0; + for (it = 0; it < NB_ITS; it++) { + /* XXX: use forward transform to generate values */ + for (i = 0; i < 64; i++) + block1[i] = av_lfg_get(&prng) % 256 - 128; + block1[0] += 1024; + + for (i = 0; i < 64; i++) + block[i] = block1[i]; + idct248_ref(img_dest1, 8, block); + + for (i = 0; i < 64; i++) + block[i] = block1[i]; + idct248_put(img_dest, 8, block); + + for (i = 0; i < 64; i++) { + v = abs((int) img_dest[i] - (int) img_dest1[i]); + if (v == 255) + printf("%d %d\n", img_dest[i], img_dest1[i]); + if (v > err_max) + err_max = v; + } + } + printf("%s %s: err_inf=%d\n", 1 ? "IDCT248" : "DCT248", name, err_max); + + if (!speed) + return; + + ti = av_gettime_relative(); + it1 = 0; + do { + for (it = 0; it < NB_ITS_SPEED; it++) { + for (i = 0; i < 64; i++) + block[i] = block1[i]; + idct248_put(img_dest, 8, block); + } + it1 += NB_ITS_SPEED; + ti1 = av_gettime_relative() - ti; + } while (ti1 < 1000000); + emms_c(); + + printf("%s %s: %0.1f kdct/s\n", 1 ? "IDCT248" : "DCT248", name, + (double) it1 * 1000.0 / (double) ti1); +} + +static void help(void) +{ + printf("dct-test [-i] []\n" + "test-number 0 -> test with random matrixes\n" + " 1 -> test with random sparse matrixes\n" + " 2 -> do 3. test from MPEG-4 std\n" + "-i test IDCT implementations\n" + "-4 test IDCT248 implementations\n" + "-t speed test\n"); +} + +#if !HAVE_GETOPT +#include "compat/getopt.c" +#endif + +int main(int argc, char **argv) +{ + int test_idct = 0, test_248_dct = 0; + int c, i; + int test = 1; + int speed = 0; + int err = 0; + + ff_ref_dct_init(); + + for (;;) { + c = getopt(argc, argv, "ih4t"); + if (c == -1) + break; + switch (c) { + case 'i': + test_idct = 1; + break; + case '4': + test_248_dct = 1; + break; + case 't': + speed = 1; + break; + default: + case 'h': + help(); + return 0; + } + } + + if (optind < argc) + test = atoi(argv[optind]); + + printf("Libav DCT/IDCT test\n"); + + if (test_248_dct) { + idct248_error("SIMPLE-C", ff_simple_idct248_put, speed); + } else { + const int cpu_flags = av_get_cpu_flags(); + if (test_idct) { + for (i = 0; i < FF_ARRAY_ELEMS(idct_tab); i++) + err |= dct_error(&idct_tab[i], test, test_idct, speed); + + for (i = 0; idct_tab_arch[i].name; i++) + if (!(~cpu_flags & idct_tab_arch[i].cpu_flag)) + err |= dct_error(&idct_tab_arch[i], test, test_idct, speed); + } +#if CONFIG_FDCTDSP + else { + for (i = 0; i < FF_ARRAY_ELEMS(fdct_tab); i++) + err |= dct_error(&fdct_tab[i], test, test_idct, speed); + + for (i = 0; fdct_tab_arch[i].name; i++) + if (!(~cpu_flags & fdct_tab_arch[i].cpu_flag)) + err |= dct_error(&fdct_tab_arch[i], test, test_idct, speed); + } +#endif /* CONFIG_FDCTDSP */ + } + + if (err) + printf("Error: %d.\n", err); + + return !!err; +} diff --git a/libavcodec/tests/fft-fixed.c b/libavcodec/tests/fft-fixed.c new file mode 100644 index 0000000000..6edd810b25 --- /dev/null +++ b/libavcodec/tests/fft-fixed.c @@ -0,0 +1,20 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#define FFT_FLOAT 0 +#include "fft.c" diff --git a/libavcodec/tests/fft.c b/libavcodec/tests/fft.c new file mode 100644 index 0000000000..db1ce9807f --- /dev/null +++ b/libavcodec/tests/fft.c @@ -0,0 +1,513 @@ +/* + * (c) 2002 Fabrice Bellard + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * FFT and MDCT tests. + */ + +#include "config.h" + +#include +#if HAVE_UNISTD_H +#include +#endif +#include +#include +#include + +#include "libavutil/cpu.h" +#include "libavutil/lfg.h" +#include "libavutil/log.h" +#include "libavutil/mathematics.h" +#include "libavutil/time.h" + +#include "libavcodec/fft.h" +#if FFT_FLOAT +#include "libavcodec/dct.h" +#include "libavcodec/rdft.h" +#endif + +/* reference fft */ + +#define MUL16(a, b) ((a) * (b)) + +#define CMAC(pre, pim, are, aim, bre, bim) \ + { \ + pre += (MUL16(are, bre) - MUL16(aim, bim)); \ + pim += (MUL16(are, bim) + MUL16(bre, aim)); \ + } + +#if FFT_FLOAT +#define RANGE 1.0 +#define REF_SCALE(x, bits) (x) +#define FMT "%10.6f" +#else +#define RANGE 16384 +#define REF_SCALE(x, bits) ((x) / (1 << (bits))) +#define FMT "%6d" +#endif + +static struct { + float re, im; +} *exptab; + +static int fft_ref_init(int nbits, int inverse) +{ + int i, n = 1 << nbits; + + exptab = av_malloc((n / 2) * sizeof(*exptab)); + if (!exptab) + return AVERROR(ENOMEM); + + for (i = 0; i < (n / 2); i++) { + double alpha = 2 * M_PI * (float) i / (float) n; + double c1 = cos(alpha), s1 = sin(alpha); + if (!inverse) + s1 = -s1; + exptab[i].re = c1; + exptab[i].im = s1; + } + return 0; +} + +static void fft_ref(FFTComplex *tabr, FFTComplex *tab, int nbits) +{ + int i, j; + int n = 1 << nbits; + int n2 = n >> 1; + + for (i = 0; i < n; i++) { + double tmp_re = 0, tmp_im = 0; + FFTComplex *q = tab; + for (j = 0; j < n; j++) { + double s, c; + int k = (i * j) & (n - 1); + if (k >= n2) { + c = -exptab[k - n2].re; + s = -exptab[k - n2].im; + } else { + c = exptab[k].re; + s = exptab[k].im; + } + CMAC(tmp_re, tmp_im, c, s, q->re, q->im); + q++; + } + tabr[i].re = REF_SCALE(tmp_re, nbits); + tabr[i].im = REF_SCALE(tmp_im, nbits); + } +} + +#if CONFIG_MDCT +static void imdct_ref(FFTSample *out, FFTSample *in, int nbits) +{ + int i, k, n = 1 << nbits; + + for (i = 0; i < n; i++) { + double sum = 0; + for (k = 0; k < n / 2; k++) { + int a = (2 * i + 1 + (n / 2)) * (2 * k + 1); + double f = cos(M_PI * a / (double) (2 * n)); + sum += f * in[k]; + } + out[i] = REF_SCALE(-sum, nbits - 2); + } +} + +/* NOTE: no normalisation by 1 / N is done */ +static void mdct_ref(FFTSample *output, FFTSample *input, int nbits) +{ + int i, k, n = 1 << nbits; + + /* do it by hand */ + for (k = 0; k < n / 2; k++) { + double s = 0; + for (i = 0; i < n; i++) { + double a = (2 * M_PI * (2 * i + 1 + n / 2) * (2 * k + 1) / (4 * n)); + s += input[i] * cos(a); + } + output[k] = REF_SCALE(s, nbits - 1); + } +} +#endif /* CONFIG_MDCT */ + +#if FFT_FLOAT +#if CONFIG_DCT +static void idct_ref(float *output, float *input, int nbits) +{ + int i, k, n = 1 << nbits; + + /* do it by hand */ + for (i = 0; i < n; i++) { + double s = 0.5 * input[0]; + for (k = 1; k < n; k++) { + double a = M_PI * k * (i + 0.5) / n; + s += input[k] * cos(a); + } + output[i] = 2 * s / n; + } +} + +static void dct_ref(float *output, float *input, int nbits) +{ + int i, k, n = 1 << nbits; + + /* do it by hand */ + for (k = 0; k < n; k++) { + double s = 0; + for (i = 0; i < n; i++) { + double a = M_PI * k * (i + 0.5) / n; + s += input[i] * cos(a); + } + output[k] = s; + } +} +#endif /* CONFIG_DCT */ +#endif /* FFT_FLOAT */ + +static FFTSample frandom(AVLFG *prng) +{ + return (int16_t) av_lfg_get(prng) / 32768.0 * RANGE; +} + +static int check_diff(FFTSample *tab1, FFTSample *tab2, int n, double scale) +{ + int i, err = 0; + double error = 0, max = 0; + + for (i = 0; i < n; i++) { + double e = fabs(tab1[i] - (tab2[i] / scale)) / RANGE; + if (e >= 1e-3) { + av_log(NULL, AV_LOG_ERROR, "ERROR %5d: "FMT" "FMT"\n", + i, tab1[i], tab2[i]); + err = 1; + } + error += e * e; + if (e > max) + max = e; + } + av_log(NULL, AV_LOG_INFO, "max:%f e:%g\n", max, sqrt(error) / n); + return err; +} + +static void help(void) +{ + av_log(NULL, AV_LOG_INFO, + "usage: fft-test [-h] [-s] [-i] [-n b]\n" + "-h print this help\n" + "-s speed test\n" + "-m (I)MDCT test\n" + "-d (I)DCT test\n" + "-r (I)RDFT test\n" + "-i inverse transform test\n" + "-n b set the transform size to 2^b\n" + "-f x set scale factor for output data of (I)MDCT to x\n"); +} + +enum tf_transform { + TRANSFORM_FFT, + TRANSFORM_MDCT, + TRANSFORM_RDFT, + TRANSFORM_DCT, +}; + +#if !HAVE_GETOPT +#include "compat/getopt.c" +#endif + +int main(int argc, char **argv) +{ + FFTComplex *tab, *tab1, *tab_ref; + FFTSample *tab2; + enum tf_transform transform = TRANSFORM_FFT; + FFTContext m, s; +#if FFT_FLOAT + RDFTContext r; + DCTContext d; +#endif /* FFT_FLOAT */ + int it, i, err = 1; + int do_speed = 0, do_inverse = 0; + int fft_nbits = 9, fft_size; + double scale = 1.0; + AVLFG prng; + + av_lfg_init(&prng, 1); + + for (;;) { + int c = getopt(argc, argv, "hsimrdn:f:c:"); + if (c == -1) + break; + switch (c) { + case 'h': + help(); + return 1; + case 's': + do_speed = 1; + break; + case 'i': + do_inverse = 1; + break; + case 'm': + transform = TRANSFORM_MDCT; + break; + case 'r': + transform = TRANSFORM_RDFT; + break; + case 'd': + transform = TRANSFORM_DCT; + break; + case 'n': + fft_nbits = atoi(optarg); + break; + case 'f': + scale = atof(optarg); + break; + case 'c': + { + int cpuflags = av_parse_cpu_flags(optarg); + if (cpuflags < 0) + return 1; + av_set_cpu_flags_mask(cpuflags); + break; + } + } + } + + fft_size = 1 << fft_nbits; + tab = av_malloc(fft_size * sizeof(FFTComplex)); + tab1 = av_malloc(fft_size * sizeof(FFTComplex)); + tab_ref = av_malloc(fft_size * sizeof(FFTComplex)); + tab2 = av_malloc(fft_size * sizeof(FFTSample)); + + if (!(tab && tab1 && tab_ref && tab2)) + goto cleanup; + + switch (transform) { +#if CONFIG_MDCT + case TRANSFORM_MDCT: + av_log(NULL, AV_LOG_INFO, "Scale factor is set to %f\n", scale); + if (do_inverse) + av_log(NULL, AV_LOG_INFO, "IMDCT"); + else + av_log(NULL, AV_LOG_INFO, "MDCT"); + ff_mdct_init(&m, fft_nbits, do_inverse, scale); + break; +#endif /* CONFIG_MDCT */ + case TRANSFORM_FFT: + if (do_inverse) + av_log(NULL, AV_LOG_INFO, "IFFT"); + else + av_log(NULL, AV_LOG_INFO, "FFT"); + ff_fft_init(&s, fft_nbits, do_inverse); + if (err = fft_ref_init(fft_nbits, do_inverse) < 0) + goto cleanup; + break; +#if FFT_FLOAT +#if CONFIG_RDFT + case TRANSFORM_RDFT: + if (do_inverse) + av_log(NULL, AV_LOG_INFO, "IDFT_C2R"); + else + av_log(NULL, AV_LOG_INFO, "DFT_R2C"); + ff_rdft_init(&r, fft_nbits, do_inverse ? IDFT_C2R : DFT_R2C); + if (err = fft_ref_init(fft_nbits, do_inverse) < 0) + goto cleanup; + break; +#endif /* CONFIG_RDFT */ +#if CONFIG_DCT + case TRANSFORM_DCT: + if (do_inverse) + av_log(NULL, AV_LOG_INFO, "DCT_III"); + else + av_log(NULL, AV_LOG_INFO, "DCT_II"); + ff_dct_init(&d, fft_nbits, do_inverse ? DCT_III : DCT_II); + break; +#endif /* CONFIG_DCT */ +#endif /* FFT_FLOAT */ + default: + av_log(NULL, AV_LOG_ERROR, "Requested transform not supported\n"); + goto cleanup; + } + av_log(NULL, AV_LOG_INFO, " %d test\n", fft_size); + + /* generate random data */ + + for (i = 0; i < fft_size; i++) { + tab1[i].re = frandom(&prng); + tab1[i].im = frandom(&prng); + } + + /* checking result */ + av_log(NULL, AV_LOG_INFO, "Checking...\n"); + + switch (transform) { +#if CONFIG_MDCT + case TRANSFORM_MDCT: + if (do_inverse) { + imdct_ref(&tab_ref->re, &tab1->re, fft_nbits); + m.imdct_calc(&m, tab2, &tab1->re); + err = check_diff(&tab_ref->re, tab2, fft_size, scale); + } else { + mdct_ref(&tab_ref->re, &tab1->re, fft_nbits); + m.mdct_calc(&m, tab2, &tab1->re); + err = check_diff(&tab_ref->re, tab2, fft_size / 2, scale); + } + break; +#endif /* CONFIG_MDCT */ + case TRANSFORM_FFT: + memcpy(tab, tab1, fft_size * sizeof(FFTComplex)); + s.fft_permute(&s, tab); + s.fft_calc(&s, tab); + + fft_ref(tab_ref, tab1, fft_nbits); + err = check_diff(&tab_ref->re, &tab->re, fft_size * 2, 1.0); + break; +#if FFT_FLOAT +#if CONFIG_RDFT + case TRANSFORM_RDFT: + { + int fft_size_2 = fft_size >> 1; + if (do_inverse) { + tab1[0].im = 0; + tab1[fft_size_2].im = 0; + for (i = 1; i < fft_size_2; i++) { + tab1[fft_size_2 + i].re = tab1[fft_size_2 - i].re; + tab1[fft_size_2 + i].im = -tab1[fft_size_2 - i].im; + } + + memcpy(tab2, tab1, fft_size * sizeof(FFTSample)); + tab2[1] = tab1[fft_size_2].re; + + r.rdft_calc(&r, tab2); + fft_ref(tab_ref, tab1, fft_nbits); + for (i = 0; i < fft_size; i++) { + tab[i].re = tab2[i]; + tab[i].im = 0; + } + err = check_diff(&tab_ref->re, &tab->re, fft_size * 2, 0.5); + } else { + for (i = 0; i < fft_size; i++) { + tab2[i] = tab1[i].re; + tab1[i].im = 0; + } + r.rdft_calc(&r, tab2); + fft_ref(tab_ref, tab1, fft_nbits); + tab_ref[0].im = tab_ref[fft_size_2].re; + err = check_diff(&tab_ref->re, tab2, fft_size, 1.0); + } + break; + } +#endif /* CONFIG_RDFT */ +#if CONFIG_DCT + case TRANSFORM_DCT: + memcpy(tab, tab1, fft_size * sizeof(FFTComplex)); + d.dct_calc(&d, &tab->re); + if (do_inverse) + idct_ref(&tab_ref->re, &tab1->re, fft_nbits); + else + dct_ref(&tab_ref->re, &tab1->re, fft_nbits); + err = check_diff(&tab_ref->re, &tab->re, fft_size, 1.0); + break; +#endif /* CONFIG_DCT */ +#endif /* FFT_FLOAT */ + } + + /* do a speed test */ + + if (do_speed) { + int64_t time_start, duration; + int nb_its; + + av_log(NULL, AV_LOG_INFO, "Speed test...\n"); + /* we measure during about 1 seconds */ + nb_its = 1; + for (;;) { + time_start = av_gettime_relative(); + for (it = 0; it < nb_its; it++) { + switch (transform) { + case TRANSFORM_MDCT: + if (do_inverse) + m.imdct_calc(&m, &tab->re, &tab1->re); + else + m.mdct_calc(&m, &tab->re, &tab1->re); + break; + case TRANSFORM_FFT: + memcpy(tab, tab1, fft_size * sizeof(FFTComplex)); + s.fft_calc(&s, tab); + break; +#if FFT_FLOAT + case TRANSFORM_RDFT: + memcpy(tab2, tab1, fft_size * sizeof(FFTSample)); + r.rdft_calc(&r, tab2); + break; + case TRANSFORM_DCT: + memcpy(tab2, tab1, fft_size * sizeof(FFTSample)); + d.dct_calc(&d, tab2); + break; +#endif /* FFT_FLOAT */ + } + } + duration = av_gettime_relative() - time_start; + if (duration >= 1000000) + break; + nb_its *= 2; + } + av_log(NULL, AV_LOG_INFO, + "time: %0.1f us/transform [total time=%0.2f s its=%d]\n", + (double) duration / nb_its, + (double) duration / 1000000.0, + nb_its); + } + + switch (transform) { +#if CONFIG_MDCT + case TRANSFORM_MDCT: + ff_mdct_end(&m); + break; +#endif /* CONFIG_MDCT */ + case TRANSFORM_FFT: + ff_fft_end(&s); + break; +#if FFT_FLOAT +#if CONFIG_RDFT + case TRANSFORM_RDFT: + ff_rdft_end(&r); + break; +#endif /* CONFIG_RDFT */ +#if CONFIG_DCT + case TRANSFORM_DCT: + ff_dct_end(&d); + break; +#endif /* CONFIG_DCT */ +#endif /* FFT_FLOAT */ + } + +cleanup: + av_free(tab); + av_free(tab1); + av_free(tab2); + av_free(tab_ref); + av_free(exptab); + + if (err) + printf("Error: %d.\n", err); + + return !!err; +} diff --git a/libavcodec/tests/golomb.c b/libavcodec/tests/golomb.c new file mode 100644 index 0000000000..7587402e24 --- /dev/null +++ b/libavcodec/tests/golomb.c @@ -0,0 +1,97 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "libavutil/mem.h" + +#include "libavcodec/get_bits.h" +#include "libavcodec/put_bits.h" +#include "libavcodec/golomb.h" + +#define COUNT 8191 +#define SIZE (COUNT * 4) + +int main(void) +{ + int i, ret = 0; + uint8_t *temp; + PutBitContext pb; + GetBitContext gb; + + temp = av_malloc(SIZE); + if (!temp) + return 2; + + init_put_bits(&pb, temp, SIZE); + for (i = 0; i < COUNT; i++) + set_ue_golomb(&pb, i); + flush_put_bits(&pb); + + init_get_bits(&gb, temp, 8 * SIZE); + for (i = 0; i < COUNT; i++) { + int j, s = show_bits(&gb, 25); + + j = get_ue_golomb(&gb); + if (j != i) { + fprintf(stderr, "get_ue_golomb: expected %d, got %d. bits: %7x\n", + i, j, s); + ret = 1; + } + } + +#define EXTEND(i) (i << 3 | i & 7) + init_put_bits(&pb, temp, SIZE); + for (i = 0; i < COUNT; i++) + set_ue_golomb(&pb, EXTEND(i)); + flush_put_bits(&pb); + + init_get_bits(&gb, temp, 8 * SIZE); + for (i = 0; i < COUNT; i++) { + int j, s = show_bits_long(&gb, 32); + + j = get_ue_golomb_long(&gb); + if (j != EXTEND(i)) { + fprintf(stderr, "get_ue_golomb_long: expected %d, got %d. " + "bits: %8x\n", EXTEND(i), j, s); + ret = 1; + } + } + + init_put_bits(&pb, temp, SIZE); + for (i = 0; i < COUNT; i++) + set_se_golomb(&pb, i - COUNT / 2); + flush_put_bits(&pb); + + init_get_bits(&gb, temp, 8 * SIZE); + for (i = 0; i < COUNT; i++) { + int j, s = show_bits(&gb, 25); + + j = get_se_golomb(&gb); + if (j != i - COUNT / 2) { + fprintf(stderr, "get_se_golomb: expected %d, got %d. bits: %7x\n", + i - COUNT / 2, j, s); + ret = 1; + } + } + + av_free(temp); + + return ret; +} diff --git a/libavcodec/tests/iirfilter.c b/libavcodec/tests/iirfilter.c new file mode 100644 index 0000000000..a6001a3270 --- /dev/null +++ b/libavcodec/tests/iirfilter.c @@ -0,0 +1,54 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include + +#include "libavutil/libm.h" + +#include "libavcodec/iirfilter.h" + +#define FILT_ORDER 4 +#define SIZE 1024 + +int main(void) +{ + struct FFIIRFilterCoeffs *fcoeffs = NULL; + struct FFIIRFilterState *fstate = NULL; + float cutoff_coeff = 0.4; + int16_t x[SIZE], y[SIZE]; + int i; + + fcoeffs = ff_iir_filter_init_coeffs(NULL, FF_FILTER_TYPE_BUTTERWORTH, + FF_FILTER_MODE_LOWPASS, FILT_ORDER, + cutoff_coeff, 0.0, 0.0); + fstate = ff_iir_filter_init_state(FILT_ORDER); + + for (i = 0; i < SIZE; i++) + x[i] = lrint(0.75 * INT16_MAX * sin(0.5 * M_PI * i * i / SIZE)); + + ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1); + + for (i = 0; i < SIZE; i++) + printf("%6d %6d\n", x[i], y[i]); + + ff_iir_filter_free_coeffs(fcoeffs); + ff_iir_filter_free_state(fstate); + return 0; +} diff --git a/libavcodec/tests/ppc/dct.c b/libavcodec/tests/ppc/dct.c new file mode 100644 index 0000000000..3d160d33e0 --- /dev/null +++ b/libavcodec/tests/ppc/dct.c @@ -0,0 +1,32 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include "libavcodec/ppc/fdct.h" + +static const struct algo fdct_tab_arch[] = { +#if HAVE_ALTIVEC && HAVE_BIGENDIAN + { "altivecfdct", ff_fdct_altivec, FF_IDCT_PERM_NONE, AV_CPU_FLAG_ALTIVEC }, +#endif + { 0 } +}; + +static const struct algo idct_tab_arch[] = { + { 0 } +}; diff --git a/libavcodec/tests/rangecoder.c b/libavcodec/tests/rangecoder.c new file mode 100644 index 0000000000..26bb589928 --- /dev/null +++ b/libavcodec/tests/rangecoder.c @@ -0,0 +1,64 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +#include "libavutil/lfg.h" +#include "libavutil/log.h" + +#include "libavcodec/rangecoder.h" + +#define SIZE 10240 + +int main(void) +{ + RangeCoder c; + uint8_t b[9 * SIZE]; + uint8_t r[9 * SIZE]; + int i; + uint8_t state[10]; + AVLFG prng; + + av_lfg_init(&prng, 1); + + ff_init_range_encoder(&c, b, SIZE); + ff_build_rac_states(&c, 0.05 * (1LL << 32), 128 + 64 + 32 + 16); + + memset(state, 128, sizeof(state)); + + for (i = 0; i < SIZE; i++) + r[i] = av_lfg_get(&prng) % 7; + + for (i = 0; i < SIZE; i++) + put_rac(&c, state, r[i] & 1); + + ff_rac_terminate(&c); + + ff_init_range_decoder(&c, b, SIZE); + + memset(state, 128, sizeof(state)); + + for (i = 0; i < SIZE; i++) + if ((r[i] & 1) != get_rac(&c, state)) { + av_log(NULL, AV_LOG_ERROR, "rac failure at %d\n", i); + return 1; + } + + return 0; +} diff --git a/libavcodec/tests/x86/dct.c b/libavcodec/tests/x86/dct.c new file mode 100644 index 0000000000..2ddb55510e --- /dev/null +++ b/libavcodec/tests/x86/dct.c @@ -0,0 +1,86 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "config.h" + +#include "libavcodec/x86/fdct.h" +#include "libavcodec/x86/xvididct.h" +#include "libavcodec/x86/simple_idct.h" + +static const struct algo fdct_tab_arch[] = { +#if HAVE_MMX_INLINE + { "MMX", ff_fdct_mmx, FF_IDCT_PERM_NONE, AV_CPU_FLAG_MMX }, +#endif +#if HAVE_MMXEXT_INLINE + { "MMXEXT", ff_fdct_mmxext, FF_IDCT_PERM_NONE, AV_CPU_FLAG_MMXEXT }, +#endif +#if HAVE_SSE2_INLINE + { "SSE2", ff_fdct_sse2, FF_IDCT_PERM_NONE, AV_CPU_FLAG_SSE2 }, +#endif + { 0 } +}; + +static const struct algo idct_tab_arch[] = { +#if HAVE_MMX_INLINE + { "SIMPLE-MMX", ff_simple_idct_mmx, FF_IDCT_PERM_SIMPLE, AV_CPU_FLAG_MMX }, +#endif +#if CONFIG_MPEG4_DECODER +#if HAVE_MMX_INLINE + { "XVID-MMX", ff_xvid_idct_mmx, FF_IDCT_PERM_NONE, AV_CPU_FLAG_MMX, 1 }, +#endif +#if HAVE_MMXEXT_INLINE + { "XVID-MMXEXT", ff_xvid_idct_mmxext, FF_IDCT_PERM_NONE, AV_CPU_FLAG_MMXEXT, 1 }, +#endif +#if HAVE_SSE2_INLINE + { "XVID-SSE2", ff_xvid_idct_sse2, FF_IDCT_PERM_SSE2, AV_CPU_FLAG_SSE2, 1 }, +#endif +#endif /* CONFIG_MPEG4_DECODER */ + { 0 } +}; + +static short idct_simple_mmx_perm[64] = { + 0x00, 0x08, 0x04, 0x09, 0x01, 0x0C, 0x05, 0x0D, + 0x10, 0x18, 0x14, 0x19, 0x11, 0x1C, 0x15, 0x1D, + 0x20, 0x28, 0x24, 0x29, 0x21, 0x2C, 0x25, 0x2D, + 0x12, 0x1A, 0x16, 0x1B, 0x13, 0x1E, 0x17, 0x1F, + 0x02, 0x0A, 0x06, 0x0B, 0x03, 0x0E, 0x07, 0x0F, + 0x30, 0x38, 0x34, 0x39, 0x31, 0x3C, 0x35, 0x3D, + 0x22, 0x2A, 0x26, 0x2B, 0x23, 0x2E, 0x27, 0x2F, + 0x32, 0x3A, 0x36, 0x3B, 0x33, 0x3E, 0x37, 0x3F, +}; + +static const uint8_t idct_sse2_row_perm[8] = { 0, 4, 1, 5, 2, 6, 3, 7 }; + +static int permute_x86(int16_t dst[64], const int16_t src[64], + enum idct_permutation_type perm_type) +{ + int i; + + switch (perm_type) { + case FF_IDCT_PERM_SIMPLE: + for (i = 0; i < 64; i++) + dst[idct_simple_mmx_perm[i]] = src[i]; + return 1; + case FF_IDCT_PERM_SSE2: + for (i = 0; i < 64; i++) + dst[(i & 0x38) | idct_sse2_row_perm[i & 7]] = src[i]; + return 1; + } + + return 0; +} -- cgit v1.2.3