Revision 7087ce08

View differences:

libavcodec/Makefile
31 31
OBJS-$(CONFIG_DCT)                     += dct.o
32 32
OBJS-$(CONFIG_DWT)                     += dwt.o
33 33
OBJS-$(CONFIG_DXVA2)                   += dxva2.o
34
FFT-OBJS-$(CONFIG_HARDCODED_TABLES)    += cos_tables.o
35
OBJS-$(CONFIG_FFT)                     += avfft.o fft.o $(FFT-OBJS-yes)
34
FFT-OBJS-$(CONFIG_HARDCODED_TABLES)    += cos_tables.o cos_fixed_tables.o
35
OBJS-$(CONFIG_FFT)                     += avfft.o fft_fixed.o fft_float.o \
36
                                          $(FFT-OBJS-yes)
36 37
OBJS-$(CONFIG_GOLOMB)                  += golomb.o
37 38
OBJS-$(CONFIG_H264DSP)                 += h264dsp.o h264idct.o
38 39
OBJS-$(CONFIG_H264PRED)                += h264pred.o
39 40
OBJS-$(CONFIG_HUFFMAN)                 += huffman.o
40 41
OBJS-$(CONFIG_LPC)                     += lpc.o
41 42
OBJS-$(CONFIG_LSP)                     += lsp.o
42
OBJS-$(CONFIG_MDCT)                    += mdct.o
43
OBJS-$(CONFIG_MDCT)                    += mdct_fixed.o mdct_float.o
43 44
RDFT-OBJS-$(CONFIG_HARDCODED_TABLES)   += sin_tables.o
44 45
OBJS-$(CONFIG_RDFT)                    += rdft.o $(RDFT-OBJS-yes)
45 46
OBJS-$(CONFIG_SINEWIN)                 += sinewin.o
......
672 673
$(SUBDIR)cos_tables.c: $(SUBDIR)costablegen$(HOSTEXESUF)
673 674
	$(M)./$< > $@
674 675

  
676
$(SUBDIR)cos_fixed_tables.c: $(SUBDIR)costablegen$(HOSTEXESUF)
677
	$(M)./$< cos fixed > $@
678

  
675 679
$(SUBDIR)sin_tables.c: $(SUBDIR)costablegen$(HOSTEXESUF)
676 680
	$(M)./$< sin > $@
677 681

  
libavcodec/costablegen.c
29 29
#endif
30 30
#define BITS 16
31 31
#define FLOATFMT "%.18e"
32
#define FIXEDFMT "%6d"
33

  
34
static int clip_f15(int v)
35
{
36
    return v < -32767 ? -32767 :
37
           v >  32767 ?  32767 :
38
           v;
39
}
40

  
41
static void printval(double val, int fixed)
42
{
43
    if (fixed)
44
        printf(" "FIXEDFMT",", clip_f15(lrint(val * (double)(1<<15))));
45
    else
46
        printf(" "FLOATFMT",", val);
47

  
48
}
32 49

  
33 50
int main(int argc, char *argv[])
34 51
{
35 52
    int i, j;
36
    int do_sin = argc == 2 && !strcmp(argv[1], "sin");
53
    int do_sin = argc > 1 && !strcmp(argv[1], "sin");
54
    int fixed  = argc > 2 && !strcmp(argv[2], "fixed");
37 55
    double (*func)(double) = do_sin ? sin : cos;
38 56

  
39 57
    printf("/* This file was generated by libavcodec/costablegen */\n");
58
    printf("#define CONFIG_FFT_FLOAT %d\n", !fixed);
40 59
    printf("#include \"libavcodec/%s\"\n", do_sin ? "rdft.h" : "fft.h");
41 60
    for (i = 4; i <= BITS; i++) {
42 61
        int m = 1 << i;
......
46 65
            int idx = j > m/4 ? m/2 - j : j;
47 66
            if (do_sin && j >= m/4)
48 67
                idx = m/4 - j;
49
            printf(" "FLOATFMT",", func(idx*freq));
68
            printval(func(idx*freq), fixed);
50 69
            if ((j & 3) == 3)
51 70
                printf("\n   ");
52 71
        }
53
        printf(" "FLOATFMT"\n};\n", func(do_sin ? -(m/4 - 1)*freq : freq));
72
        printval(func(do_sin ? -(m/4 - 1)*freq : freq), fixed);
73
        printf("\n};\n");
54 74
    }
55 75
    return 0;
56 76
}
libavcodec/fft-internal.h
1
/*
2
 * This file is part of Libav.
3
 *
4
 * Libav is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * Libav is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with Libav; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18

  
19
#ifndef AVCODEC_FFT_INTERNAL_H
20
#define AVCODEC_FFT_INTERNAL_H
21

  
22
#if CONFIG_FFT_FLOAT
23

  
24
#define FIX15(v) (v)
25
#define sqrthalf (float)M_SQRT1_2
26

  
27
#define BF(x, y, a, b) do {                     \
28
        x = a - b;                              \
29
        y = a + b;                              \
30
    } while (0)
31

  
32
#define CMUL(dre, dim, are, aim, bre, bim) do { \
33
        (dre) = (are) * (bre) - (aim) * (bim);  \
34
        (dim) = (are) * (bim) + (aim) * (bre);  \
35
    } while (0)
36

  
37
#else
38

  
39
#include "libavutil/intmath.h"
40
#include "mathops.h"
41

  
42
#define SCALE_FLOAT(a, bits) lrint((a) * (double)(1 << (bits)))
43
#define FIX15(a) av_clip(SCALE_FLOAT(a, 15), -32767, 32767)
44

  
45
#define sqrthalf ((int16_t)((1<<15)*M_SQRT1_2))
46

  
47
#define BF(x, y, a, b) do {                     \
48
        x = (a - b) >> 1;                       \
49
        y = (a + b) >> 1;                       \
50
    } while (0)
51

  
52
#define CMUL(dre, dim, are, aim, bre, bim) do {                 \
53
        (dre) = (MUL16(are, bre) - MUL16(aim, bim)) >> 15;      \
54
        (dim) = (MUL16(are, bim) + MUL16(aim, bre)) >> 15;      \
55
    } while (0)
56

  
57
#endif /* CONFIG_FFT_FLOAT */
58

  
59
#define ff_imdct_calc_c FFT_NAME(ff_imdct_calc_c)
60
#define ff_imdct_half_c FFT_NAME(ff_imdct_half_c)
61
#define ff_mdct_calc_c  FFT_NAME(ff_mdct_calc_c)
62

  
63
void ff_imdct_calc_c(FFTContext *s, FFTSample *output, const FFTSample *input);
64
void ff_imdct_half_c(FFTContext *s, FFTSample *output, const FFTSample *input);
65
void ff_mdct_calc_c(FFTContext *s, FFTSample *output, const FFTSample *input);
66

  
67
#endif /* AVCODEC_FFT_INTERNAL_H */
libavcodec/fft.c
30 30
#include <string.h>
31 31
#include "libavutil/mathematics.h"
32 32
#include "fft.h"
33
#include "fft-internal.h"
33 34

  
34 35
/* cos(2*pi*x/n) for 0<=x<=n/4, followed by its reverse */
35 36
#if !CONFIG_HARDCODED_TABLES
......
47 48
COSTABLE(32768);
48 49
COSTABLE(65536);
49 50
#endif
50
COSTABLE_CONST FFTSample * const ff_cos_tabs[] = {
51
COSTABLE_CONST FFTSample * const FFT_NAME(ff_cos_tabs)[] = {
51 52
    NULL, NULL, NULL, NULL,
52
    ff_cos_16, ff_cos_32, ff_cos_64, ff_cos_128, ff_cos_256, ff_cos_512, ff_cos_1024,
53
    ff_cos_2048, ff_cos_4096, ff_cos_8192, ff_cos_16384, ff_cos_32768, ff_cos_65536,
53
    FFT_NAME(ff_cos_16),
54
    FFT_NAME(ff_cos_32),
55
    FFT_NAME(ff_cos_64),
56
    FFT_NAME(ff_cos_128),
57
    FFT_NAME(ff_cos_256),
58
    FFT_NAME(ff_cos_512),
59
    FFT_NAME(ff_cos_1024),
60
    FFT_NAME(ff_cos_2048),
61
    FFT_NAME(ff_cos_4096),
62
    FFT_NAME(ff_cos_8192),
63
    FFT_NAME(ff_cos_16384),
64
    FFT_NAME(ff_cos_32768),
65
    FFT_NAME(ff_cos_65536),
54 66
};
55 67

  
56 68
static void ff_fft_permute_c(FFTContext *s, FFTComplex *z);
......
73 85
    int i;
74 86
    int m = 1<<index;
75 87
    double freq = 2*M_PI/m;
76
    FFTSample *tab = ff_cos_tabs[index];
88
    FFTSample *tab = FFT_NAME(ff_cos_tabs)[index];
77 89
    for(i=0; i<=m/4; i++)
78
        tab[i] = cos(i*freq);
90
        tab[i] = FIX15(cos(i*freq));
79 91
    for(i=1; i<m/4; i++)
80 92
        tab[m/2-i] = tab[i];
81 93
#endif
......
107 119
    s->mdct_calc   = ff_mdct_calc_c;
108 120
#endif
109 121

  
122
#if CONFIG_FFT_FLOAT
110 123
    if (ARCH_ARM)     ff_fft_init_arm(s);
111 124
    if (HAVE_ALTIVEC) ff_fft_init_altivec(s);
112 125
    if (HAVE_MMX)     ff_fft_init_mmx(s);
126
#endif
113 127

  
114 128
    for(j=4; j<=nbits; j++) {
115 129
        ff_init_ff_cos_tabs(j);
......
144 158
    av_freep(&s->tmp_buf);
145 159
}
146 160

  
147
#define sqrthalf (float)M_SQRT1_2
148

  
149
#define BF(x,y,a,b) {\
150
    x = a - b;\
151
    y = a + b;\
152
}
153

  
154 161
#define BUTTERFLIES(a0,a1,a2,a3) {\
155 162
    BF(t3, t5, t5, t1);\
156 163
    BF(a2.re, a0.re, a0.re, t5);\
......
174 181
}
175 182

  
176 183
#define TRANSFORM(a0,a1,a2,a3,wre,wim) {\
177
    t1 = a2.re * wre + a2.im * wim;\
178
    t2 = a2.im * wre - a2.re * wim;\
179
    t5 = a3.re * wre - a3.im * wim;\
180
    t6 = a3.im * wre + a3.re * wim;\
184
    CMUL(t1, t2, a2.re, a2.im, wre, -wim);\
185
    CMUL(t5, t6, a3.re, a3.im, wre,  wim);\
181 186
    BUTTERFLIES(a0,a1,a2,a3)\
182 187
}
183 188

  
......
193 198
#define PASS(name)\
194 199
static void name(FFTComplex *z, const FFTSample *wre, unsigned int n)\
195 200
{\
196
    FFTSample t1, t2, t3, t4, t5, t6;\
201
    FFTDouble t1, t2, t3, t4, t5, t6;\
197 202
    int o1 = 2*n;\
198 203
    int o2 = 4*n;\
199 204
    int o3 = 6*n;\
......
222 227
    fft##n2(z);\
223 228
    fft##n4(z+n4*2);\
224 229
    fft##n4(z+n4*3);\
225
    pass(z,ff_cos_##n,n4/2);\
230
    pass(z,FFT_NAME(ff_cos_##n),n4/2);\
226 231
}
227 232

  
228 233
static void fft4(FFTComplex *z)
229 234
{
230
    FFTSample t1, t2, t3, t4, t5, t6, t7, t8;
235
    FFTDouble t1, t2, t3, t4, t5, t6, t7, t8;
231 236

  
232 237
    BF(t3, t1, z[0].re, z[1].re);
233 238
    BF(t8, t6, z[3].re, z[2].re);
......
241 246

  
242 247
static void fft8(FFTComplex *z)
243 248
{
244
    FFTSample t1, t2, t3, t4, t5, t6, t7, t8;
249
    FFTDouble t1, t2, t3, t4, t5, t6, t7, t8;
245 250

  
246 251
    fft4(z);
247 252

  
......
262 267
#if !CONFIG_SMALL
263 268
static void fft16(FFTComplex *z)
264 269
{
265
    FFTSample t1, t2, t3, t4, t5, t6;
270
    FFTDouble t1, t2, t3, t4, t5, t6;
271
    FFTSample cos_16_1 = FFT_NAME(ff_cos_16)[1];
272
    FFTSample cos_16_3 = FFT_NAME(ff_cos_16)[3];
266 273

  
267 274
    fft8(z);
268 275
    fft4(z+8);
......
270 277

  
271 278
    TRANSFORM_ZERO(z[0],z[4],z[8],z[12]);
272 279
    TRANSFORM(z[2],z[6],z[10],z[14],sqrthalf,sqrthalf);
273
    TRANSFORM(z[1],z[5],z[9],z[13],ff_cos_16[1],ff_cos_16[3]);
274
    TRANSFORM(z[3],z[7],z[11],z[15],ff_cos_16[3],ff_cos_16[1]);
280
    TRANSFORM(z[1],z[5],z[9],z[13],cos_16_1,cos_16_3);
281
    TRANSFORM(z[3],z[7],z[11],z[15],cos_16_3,cos_16_1);
275 282
}
276 283
#else
277 284
DECL_FFT(16,8,4)
libavcodec/fft.h
22 22
#ifndef AVCODEC_FFT_H
23 23
#define AVCODEC_FFT_H
24 24

  
25
#ifndef CONFIG_FFT_FLOAT
26
#define CONFIG_FFT_FLOAT 1
27
#endif
28

  
25 29
#include <stdint.h>
26 30
#include "config.h"
27 31
#include "libavutil/mem.h"
32

  
33
#if CONFIG_FFT_FLOAT
34

  
28 35
#include "avfft.h"
29 36

  
37
#define FFT_NAME(x) x
38

  
39
typedef float FFTDouble;
40

  
41
#else
42

  
43
#define FFT_NAME(x) x ## _fixed
44

  
45
typedef int16_t FFTSample;
46
typedef int     FFTDouble;
47

  
48
typedef struct FFTComplex {
49
    int16_t re, im;
50
} FFTComplex;
51

  
52
typedef struct FFTContext FFTContext;
53

  
54
#endif /* CONFIG_FFT_FLOAT */
55

  
30 56
/* FFT computation */
31 57

  
32 58
struct FFTContext {
......
66 92
#endif
67 93

  
68 94
#define COSTABLE(size) \
69
    COSTABLE_CONST DECLARE_ALIGNED(16, FFTSample, ff_cos_##size)[size/2]
95
    COSTABLE_CONST DECLARE_ALIGNED(16, FFTSample, FFT_NAME(ff_cos_##size))[size/2]
70 96

  
71 97
extern COSTABLE(16);
72 98
extern COSTABLE(32);
......
81 107
extern COSTABLE(16384);
82 108
extern COSTABLE(32768);
83 109
extern COSTABLE(65536);
84
extern COSTABLE_CONST FFTSample* const ff_cos_tabs[17];
110
extern COSTABLE_CONST FFTSample* const FFT_NAME(ff_cos_tabs)[17];
111

  
112
#define ff_init_ff_cos_tabs FFT_NAME(ff_init_ff_cos_tabs)
85 113

  
86 114
/**
87 115
 * Initialize the cosine table in ff_cos_tabs[index]
......
89 117
 */
90 118
void ff_init_ff_cos_tabs(int index);
91 119

  
120
#define ff_fft_init FFT_NAME(ff_fft_init)
121
#define ff_fft_end  FFT_NAME(ff_fft_end)
122

  
92 123
/**
93 124
 * Set up a complex FFT.
94 125
 * @param nbits           log2 of the length of the input array
......
102 133

  
103 134
void ff_fft_end(FFTContext *s);
104 135

  
136
#define ff_mdct_init FFT_NAME(ff_mdct_init)
137
#define ff_mdct_end  FFT_NAME(ff_mdct_end)
138

  
105 139
int ff_mdct_init(FFTContext *s, int nbits, int inverse, double scale);
106
void ff_imdct_calc_c(FFTContext *s, FFTSample *output, const FFTSample *input);
107
void ff_imdct_half_c(FFTContext *s, FFTSample *output, const FFTSample *input);
108
void ff_mdct_calc_c(FFTContext *s, FFTSample *output, const FFTSample *input);
109 140
void ff_mdct_end(FFTContext *s);
110 141

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

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

  
19
#define CONFIG_FFT_FLOAT 1
20
#include "fft.c"
libavcodec/mdct.c
24 24
#include "libavutil/common.h"
25 25
#include "libavutil/mathematics.h"
26 26
#include "fft.h"
27
#include "fft-internal.h"
27 28

  
28 29
/**
29 30
 * @file
30 31
 * MDCT/IMDCT transforms.
31 32
 */
32 33

  
34
#if CONFIG_FFT_FLOAT
35
#   define RSCALE(x) (x)
36
#else
37
#   define RSCALE(x) ((x) >> 1)
38
#endif
39

  
33 40
/**
34 41
 * init MDCT or IMDCT computation.
35 42
 */
......
70 77
    scale = sqrt(fabs(scale));
71 78
    for(i=0;i<n4;i++) {
72 79
        alpha = 2 * M_PI * (i + theta) / n;
73
        s->tcos[i*tstep] = -cos(alpha) * scale;
74
        s->tsin[i*tstep] = -sin(alpha) * scale;
80
        s->tcos[i*tstep] = FIX15(-cos(alpha) * scale);
81
        s->tsin[i*tstep] = FIX15(-sin(alpha) * scale);
75 82
    }
76 83
    return 0;
77 84
 fail:
......
79 86
    return -1;
80 87
}
81 88

  
82
/* complex multiplication: p = a * b */
83
#define CMUL(pre, pim, are, aim, bre, bim) \
84
{\
85
    FFTSample _are = (are);\
86
    FFTSample _aim = (aim);\
87
    FFTSample _bre = (bre);\
88
    FFTSample _bim = (bim);\
89
    (pre) = _are * _bre - _aim * _bim;\
90
    (pim) = _are * _bim + _aim * _bre;\
91
}
92

  
93 89
/**
94 90
 * Compute the middle half of the inverse MDCT of size N = 2^nbits,
95 91
 * thus excluding the parts that can be derived by symmetry
......
161 157
void ff_mdct_calc_c(FFTContext *s, FFTSample *out, const FFTSample *input)
162 158
{
163 159
    int i, j, n, n8, n4, n2, n3;
164
    FFTSample re, im;
160
    FFTDouble re, im;
165 161
    const uint16_t *revtab = s->revtab;
166 162
    const FFTSample *tcos = s->tcos;
167 163
    const FFTSample *tsin = s->tsin;
......
175 171

  
176 172
    /* pre rotation */
177 173
    for(i=0;i<n8;i++) {
178
        re = -input[2*i+n3] - input[n3-1-2*i];
179
        im = -input[n4+2*i] + input[n4-1-2*i];
174
        re = RSCALE(-input[2*i+n3] - input[n3-1-2*i]);
175
        im = RSCALE(-input[n4+2*i] + input[n4-1-2*i]);
180 176
        j = revtab[i];
181 177
        CMUL(x[j].re, x[j].im, re, im, -tcos[i], tsin[i]);
182 178

  
183
        re = input[2*i] - input[n2-1-2*i];
184
        im = -(input[n2+2*i] + input[n-1-2*i]);
179
        re = RSCALE( input[2*i]    - input[n2-1-2*i]);
180
        im = RSCALE(-input[n2+2*i] - input[ n-1-2*i]);
185 181
        j = revtab[n8 + i];
186 182
        CMUL(x[j].re, x[j].im, re, im, -tcos[n8 + i], tsin[n8 + i]);
187 183
    }
libavcodec/mdct_fixed.c
1
/*
2
 * This file is part of Libav.
3
 *
4
 * Libav is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * Libav is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with Libav; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18

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

  
19
#define CONFIG_FFT_FLOAT 1
20
#include "mdct.c"

Also available in: Unified diff