]> git.sesse.net Git - ffmpeg/blob - libavutil/tx_priv.h
lavu/tx: add full-sized iMDCT transform flag
[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 "thread.h"
24 #include "mem_internal.h"
25 #include "avassert.h"
26 #include "attributes.h"
27
28 #ifdef TX_FLOAT
29 #define TX_NAME(x) x ## _float
30 #define SCALE_TYPE float
31 typedef float FFTSample;
32 typedef AVComplexFloat FFTComplex;
33 #elif defined(TX_DOUBLE)
34 #define TX_NAME(x) x ## _double
35 #define SCALE_TYPE double
36 typedef double FFTSample;
37 typedef AVComplexDouble FFTComplex;
38 #elif defined(TX_INT32)
39 #define TX_NAME(x) x ## _int32
40 #define SCALE_TYPE float
41 typedef int32_t FFTSample;
42 typedef AVComplexInt32 FFTComplex;
43 #else
44 typedef void FFTComplex;
45 #endif
46
47 #if defined(TX_FLOAT) || defined(TX_DOUBLE)
48
49 #define CMUL(dre, dim, are, aim, bre, bim)                                     \
50     do {                                                                       \
51         (dre) = (are) * (bre) - (aim) * (bim);                                 \
52         (dim) = (are) * (bim) + (aim) * (bre);                                 \
53     } while (0)
54
55 #define SMUL(dre, dim, are, aim, bre, bim)                                     \
56     do {                                                                       \
57         (dre) = (are) * (bre) - (aim) * (bim);                                 \
58         (dim) = (are) * (bim) - (aim) * (bre);                                 \
59     } while (0)
60
61 #define UNSCALE(x) (x)
62 #define RESCALE(x) (x)
63
64 #define FOLD(a, b) ((a) + (b))
65
66 #elif defined(TX_INT32)
67
68 /* Properly rounds the result */
69 #define CMUL(dre, dim, are, aim, bre, bim)                                     \
70     do {                                                                       \
71         int64_t accu;                                                          \
72         (accu)  = (int64_t)(bre) * (are);                                      \
73         (accu) -= (int64_t)(bim) * (aim);                                      \
74         (dre)   = (int)(((accu) + 0x40000000) >> 31);                          \
75         (accu)  = (int64_t)(bim) * (are);                                      \
76         (accu) += (int64_t)(bre) * (aim);                                      \
77         (dim)   = (int)(((accu) + 0x40000000) >> 31);                          \
78     } while (0)
79
80 #define SMUL(dre, dim, are, aim, bre, bim)                                     \
81     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 UNSCALE(x) ((double)x/2147483648.0)
92 #define RESCALE(x) (av_clip64(lrintf((x) * 2147483648.0), INT32_MIN, INT32_MAX))
93
94 #define FOLD(x, y) ((int)((x) + (unsigned)(y) + 32) >> 6)
95
96 #endif
97
98 #define BF(x, y, a, b)                                                         \
99     do {                                                                       \
100         x = (a) - (b);                                                         \
101         y = (a) + (b);                                                         \
102     } while (0)
103
104 #define CMUL3(c, a, b)                                                         \
105     CMUL((c).re, (c).im, (a).re, (a).im, (b).re, (b).im)
106
107 #define COSTABLE(size)                                                         \
108     DECLARE_ALIGNED(32, FFTSample, TX_NAME(ff_cos_##size))[size/4 + 1]
109
110 /* Used by asm, reorder with care */
111 struct AVTXContext {
112     int n;              /* Non-power-of-two part */
113     int m;              /* Power-of-two part */
114     int inv;            /* Is inverse */
115     int type;           /* Type */
116     uint64_t flags;     /* Flags */
117     double scale;       /* Scale */
118
119     FFTComplex *exptab; /* MDCT exptab */
120     FFTComplex    *tmp; /* Temporary buffer needed for all compound transforms */
121     int        *pfatab; /* Input/Output mapping for compound transforms */
122     int        *revtab; /* Input mapping for power of two transforms */
123     int   *inplace_idx; /* Required indices to revtab for in-place transforms */
124
125     av_tx_fn    top_tx; /* Used for computing transforms derived from other
126                          * transforms, like full-length iMDCTs and RDFTs.
127                          * NOTE: Do NOT use this to mix assembly with C code. */
128 };
129
130 /* Checks if type is an MDCT */
131 int ff_tx_type_is_mdct(enum AVTXType type);
132
133 /*
134  * Generates the PFA permutation table into AVTXContext->pfatab. The end table
135  * is appended to the start table.
136  */
137 int ff_tx_gen_compound_mapping(AVTXContext *s);
138
139 /*
140  * Generates a standard-ish (slightly modified) Split-Radix revtab into
141  * AVTXContext->revtab
142  */
143 int ff_tx_gen_ptwo_revtab(AVTXContext *s, int invert_lookup);
144
145 /*
146  * Generates an index into AVTXContext->inplace_idx that if followed in the
147  * specific order,  allows the revtab to be done in-place. AVTXContext->revtab
148  * must already exist.
149  */
150 int ff_tx_gen_ptwo_inplace_revtab_idx(AVTXContext *s);
151
152 /* Templated init functions */
153 int ff_tx_init_mdct_fft_float(AVTXContext *s, av_tx_fn *tx,
154                               enum AVTXType type, int inv, int len,
155                               const void *scale, uint64_t flags);
156 int ff_tx_init_mdct_fft_double(AVTXContext *s, av_tx_fn *tx,
157                                enum AVTXType type, int inv, int len,
158                                const void *scale, uint64_t flags);
159 int ff_tx_init_mdct_fft_int32(AVTXContext *s, av_tx_fn *tx,
160                               enum AVTXType type, int inv, int len,
161                               const void *scale, uint64_t flags);
162
163 typedef struct CosTabsInitOnce {
164     void (*func)(void);
165     AVOnce control;
166 } CosTabsInitOnce;
167
168 #endif /* AVUTIL_TX_PRIV_H */