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