]> git.sesse.net Git - ffmpeg/blob - libavutil/tx_priv.h
lavu/tx: implement 32 bit fixed point FFT and MDCT
[ffmpeg] / libavutil / tx_priv.h
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef AVUTIL_TX_PRIV_H
20 #define AVUTIL_TX_PRIV_H
21
22 #include "tx.h"
23 #include <stddef.h>
24 #include "thread.h"
25 #include "mem.h"
26 #include "avassert.h"
27 #include "attributes.h"
28
29 #ifdef TX_FLOAT
30 #define TX_NAME(x) x ## _float
31 #define SCALE_TYPE float
32 typedef float FFTSample;
33 typedef AVComplexFloat FFTComplex;
34 #elif defined(TX_DOUBLE)
35 #define TX_NAME(x) x ## _double
36 #define SCALE_TYPE double
37 typedef double FFTSample;
38 typedef AVComplexDouble FFTComplex;
39 #elif defined(TX_INT32)
40 #define TX_NAME(x) x ## _int32
41 #define SCALE_TYPE float
42 typedef int32_t FFTSample;
43 typedef AVComplexInt32 FFTComplex;
44 #else
45 typedef void FFTComplex;
46 #endif
47
48 #if defined(TX_FLOAT) || defined(TX_DOUBLE)
49
50 #define MUL(x, y) ((x)*(y))
51
52 #define CMUL(dre, dim, are, aim, bre, bim) do {                                \
53         (dre) = (are) * (bre) - (aim) * (bim);                                 \
54         (dim) = (are) * (bim) + (aim) * (bre);                                 \
55     } while (0)
56
57 #define SMUL(dre, dim, are, aim, bre, bim) do {                                \
58         (dre) = (are) * (bre) - (aim) * (bim);                                 \
59         (dim) = (are) * (bim) - (aim) * (bre);                                 \
60     } while (0)
61
62 #define RESCALE(x) (x)
63
64 #define FOLD(a, b) ((a) + (b))
65
66 #elif defined(TX_INT32)
67
68 #define MUL(x, y) ((int32_t)(((int64_t)(x) * (int64_t)(y) + 0x40000000) >> 31))
69
70 /* Properly rounds the result */
71 #define CMUL(dre, dim, are, aim, bre, bim) do {                                \
72         int64_t accu;                                                          \
73         (accu)  = (int64_t)(bre) * (are);                                      \
74         (accu) -= (int64_t)(bim) * (aim);                                      \
75         (dre)   = (int)(((accu) + 0x40000000) >> 31);                          \
76         (accu)  = (int64_t)(bim) * (are);                                      \
77         (accu) += (int64_t)(bre) * (aim);                                      \
78         (dim)   = (int)(((accu) + 0x40000000) >> 31);                          \
79     } while (0)
80
81 #define SMUL(dre, dim, are, aim, bre, bim) do {                                \
82         int64_t accu;                                                          \
83         (accu)  = (int64_t)(bre) * (are);                                      \
84         (accu) -= (int64_t)(bim) * (aim);                                      \
85         (dre)   = (int)(((accu) + 0x40000000) >> 31);                          \
86         (accu)  = (int64_t)(bim) * (are);                                      \
87         (accu) -= (int64_t)(bre) * (aim);                                      \
88         (dim)   = (int)(((accu) + 0x40000000) >> 31);                          \
89     } while (0)
90
91 #define RESCALE(x) (lrintf((x) * 2147483648.0))
92
93 #define FOLD(x, y) ((int)((x) + (unsigned)(y) + 32) >> 6)
94
95 #endif
96
97 #define BF(x, y, a, b) do {                                                    \
98         x = (a) - (b);                                                         \
99         y = (a) + (b);                                                         \
100     } while (0)
101
102 #define CMUL3(c, a, b)                                                         \
103     CMUL((c).re, (c).im, (a).re, (a).im, (b).re, (b).im)
104
105 #define COSTABLE(size) \
106     DECLARE_ALIGNED(32, FFTSample, TX_NAME(ff_cos_##size))[size/2]
107
108 /* Used by asm, reorder with care */
109 struct AVTXContext {
110     int n;              /* Nptwo part */
111     int m;              /* Ptwo part */
112     int inv;            /* Is inverted */
113     int type;           /* Type */
114
115     FFTComplex *exptab; /* MDCT exptab */
116     FFTComplex *tmp;    /* Temporary buffer needed for all compound transforms */
117     int        *pfatab; /* Input/Output mapping for compound transforms */
118     int        *revtab; /* Input mapping for power of two transforms */
119 };
120
121 /* Shared functions */
122 int ff_tx_type_is_mdct(enum AVTXType type);
123 int ff_tx_gen_compound_mapping(AVTXContext *s);
124 int ff_tx_gen_ptwo_revtab(AVTXContext *s);
125
126 /* Also used by SIMD init */
127 static inline int split_radix_permutation(int i, int n, int inverse)
128 {
129     int m;
130     if (n <= 2)
131         return i & 1;
132     m = n >> 1;
133     if (!(i & m))
134         return split_radix_permutation(i, m, inverse)*2;
135     m >>= 1;
136     if (inverse == !(i & m))
137         return split_radix_permutation(i, m, inverse)*4 + 1;
138     else
139         return split_radix_permutation(i, m, inverse)*4 - 1;
140 }
141
142 /* Templated functions */
143 int ff_tx_init_mdct_fft_float(AVTXContext *s, av_tx_fn *tx,
144                               enum AVTXType type, int inv, int len,
145                               const void *scale, uint64_t flags);
146 int ff_tx_init_mdct_fft_double(AVTXContext *s, av_tx_fn *tx,
147                                enum AVTXType type, int inv, int len,
148                                const void *scale, uint64_t flags);
149 int ff_tx_init_mdct_fft_int32(AVTXContext *s, av_tx_fn *tx,
150                               enum AVTXType type, int inv, int len,
151                               const void *scale, uint64_t flags);
152
153 typedef struct CosTabsInitOnce {
154     void (*func)(void);
155     AVOnce control;
156 } CosTabsInitOnce;
157
158 #endif /* AVUTIL_TX_PRIV_H */