xref: /third_party/ffmpeg/libavutil/tx_priv.h (revision cabdff1a)
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 "attributes.h"
26
27#ifdef TX_FLOAT
28#define TX_TAB(x) x ## _float
29#define TX_NAME(x) x ## _float_c
30#define TX_NAME_STR(x) NULL_IF_CONFIG_SMALL(x "_float_c")
31#define TX_TYPE(x) AV_TX_FLOAT_ ## x
32#define TX_FN_NAME(fn, suffix) ff_tx_ ## fn ## _float_ ## suffix
33#define TX_FN_NAME_STR(fn, suffix) NULL_IF_CONFIG_SMALL(#fn "_float_" #suffix)
34#define MULT(x, m) ((x) * (m))
35#define SCALE_TYPE float
36typedef float TXSample;
37typedef float TXUSample;
38typedef AVComplexFloat TXComplex;
39#elif defined(TX_DOUBLE)
40#define TX_TAB(x) x ## _double
41#define TX_NAME(x) x ## _double_c
42#define TX_NAME_STR(x) NULL_IF_CONFIG_SMALL(x "_double_c")
43#define TX_TYPE(x) AV_TX_DOUBLE_ ## x
44#define TX_FN_NAME(fn, suffix) ff_tx_ ## fn ## _double_ ## suffix
45#define TX_FN_NAME_STR(fn, suffix) NULL_IF_CONFIG_SMALL(#fn "_double_" #suffix)
46#define MULT(x, m) ((x) * (m))
47#define SCALE_TYPE double
48typedef double TXSample;
49typedef double TXUSample;
50typedef AVComplexDouble TXComplex;
51#elif defined(TX_INT32)
52#define TX_TAB(x) x ## _int32
53#define TX_NAME(x) x ## _int32_c
54#define TX_NAME_STR(x) NULL_IF_CONFIG_SMALL(x "_int32_c")
55#define TX_TYPE(x) AV_TX_INT32_ ## x
56#define TX_FN_NAME(fn, suffix) ff_tx_ ## fn ## _int32_ ## suffix
57#define TX_FN_NAME_STR(fn, suffix) NULL_IF_CONFIG_SMALL(#fn "_int32_" #suffix)
58#define MULT(x, m) (((((int64_t)(x)) * (int64_t)(m)) + 0x40000000) >> 31)
59#define SCALE_TYPE float
60typedef int32_t TXSample;
61typedef uint32_t TXUSample;
62typedef AVComplexInt32 TXComplex;
63#else
64typedef void TXComplex;
65#endif
66
67#define TX_DECL_FN(fn, suffix) \
68    void TX_FN_NAME(fn, suffix)(AVTXContext *s, void *o, void *i, ptrdiff_t st);
69
70#define TX_DEF(fn, tx_type, len_min, len_max, f1, f2,                          \
71               p, init_fn, suffix, cf, cd_flags, cf2)                          \
72    &(const FFTXCodelet){                                                      \
73        .name       = TX_FN_NAME_STR(fn, suffix),                              \
74        .function   = TX_FN_NAME(fn, suffix),                                  \
75        .type       = TX_TYPE(tx_type),                                        \
76        .flags      = FF_TX_ALIGNED | FF_TX_OUT_OF_PLACE | cd_flags,           \
77        .factors    = { f1, f2 },                                              \
78        .min_len    = len_min,                                                 \
79        .max_len    = len_max,                                                 \
80        .init       = init_fn,                                                 \
81        .cpu_flags  = cf2 | AV_CPU_FLAG_ ## cf,                                \
82        .prio       = p,                                                       \
83    }
84
85#if defined(TX_FLOAT) || defined(TX_DOUBLE)
86
87#define CMUL(dre, dim, are, aim, bre, bim)      \
88    do {                                        \
89        (dre) = (are) * (bre) - (aim) * (bim);  \
90        (dim) = (are) * (bim) + (aim) * (bre);  \
91    } while (0)
92
93#define SMUL(dre, dim, are, aim, bre, bim)      \
94    do {                                        \
95        (dre) = (are) * (bre) - (aim) * (bim);  \
96        (dim) = (are) * (bim) - (aim) * (bre);  \
97    } while (0)
98
99#define UNSCALE(x) (x)
100#define RESCALE(x) (x)
101
102#define FOLD(a, b) ((a) + (b))
103
104#define BF(x, y, a, b)  \
105    do {                \
106        x = (a) - (b);  \
107        y = (a) + (b);  \
108    } while (0)
109
110#elif defined(TX_INT32)
111
112/* Properly rounds the result */
113#define CMUL(dre, dim, are, aim, bre, bim)             \
114    do {                                               \
115        int64_t accu;                                  \
116        (accu)  = (int64_t)(bre) * (are);              \
117        (accu) -= (int64_t)(bim) * (aim);              \
118        (dre)   = (int)(((accu) + 0x40000000) >> 31);  \
119        (accu)  = (int64_t)(bim) * (are);              \
120        (accu) += (int64_t)(bre) * (aim);              \
121        (dim)   = (int)(((accu) + 0x40000000) >> 31);  \
122    } while (0)
123
124#define SMUL(dre, dim, are, aim, bre, bim)             \
125    do {                                               \
126        int64_t accu;                                  \
127        (accu)  = (int64_t)(bre) * (are);              \
128        (accu) -= (int64_t)(bim) * (aim);              \
129        (dre)   = (int)(((accu) + 0x40000000) >> 31);  \
130        (accu)  = (int64_t)(bim) * (are);              \
131        (accu) -= (int64_t)(bre) * (aim);              \
132        (dim)   = (int)(((accu) + 0x40000000) >> 31);  \
133    } while (0)
134
135#define UNSCALE(x) ((double)(x)/2147483648.0)
136#define RESCALE(x) (av_clip64(lrintf((x) * 2147483648.0), INT32_MIN, INT32_MAX))
137
138#define FOLD(x, y) ((int32_t)((x) + (unsigned)(y) + 32) >> 6)
139
140#define BF(x, y, a, b)  \
141    do {                \
142        x = (a) - (unsigned)(b);  \
143        y = (a) + (unsigned)(b);  \
144    } while (0)
145
146#endif /* TX_INT32 */
147
148#define CMUL3(c, a, b) CMUL((c).re, (c).im, (a).re, (a).im, (b).re, (b).im)
149
150/* Codelet flags, used to pick codelets. Must be a superset of enum AVTXFlags,
151 * but if it runs out of bits, it can be made separate. */
152#define FF_TX_OUT_OF_PLACE (1ULL << 63) /* Can be OR'd with AV_TX_INPLACE             */
153#define FF_TX_ALIGNED      (1ULL << 62) /* Cannot be OR'd with AV_TX_UNALIGNED        */
154#define FF_TX_PRESHUFFLE   (1ULL << 61) /* Codelet expects permuted coeffs            */
155#define FF_TX_INVERSE_ONLY (1ULL << 60) /* For non-orthogonal inverse-only transforms */
156#define FF_TX_FORWARD_ONLY (1ULL << 59) /* For non-orthogonal forward-only transforms */
157
158typedef enum FFTXCodeletPriority {
159    FF_TX_PRIO_BASE = 0,               /* Baseline priority */
160
161    /* For SIMD, set base prio to the register size in bits and increment in
162     * steps of 64 depending on faster/slower features, like FMA. */
163
164    FF_TX_PRIO_MIN          = -131072, /* For naive implementations */
165    FF_TX_PRIO_MAX          =  32768,  /* For custom implementations/ASICs */
166} FFTXCodeletPriority;
167
168/* Codelet options */
169typedef struct FFTXCodeletOptions {
170    int invert_lookup;     /* If codelet is flagged as FF_TX_CODELET_PRESHUFFLE,
171                              invert the lookup direction for the map generated */
172} FFTXCodeletOptions;
173
174/* Maximum amount of subtransform functions, subtransforms and factors. Arbitrary. */
175#define TX_MAX_SUB 4
176
177typedef struct FFTXCodelet {
178    const char    *name;          /* Codelet name, for debugging */
179    av_tx_fn       function;      /* Codelet function, != NULL */
180    enum AVTXType  type;          /* Type of codelet transform */
181#define TX_TYPE_ANY INT32_MAX     /* Special type to allow all types */
182
183    uint64_t flags;               /* A combination of AVTXFlags and codelet
184                                   * flags that describe its properties. */
185
186    int factors[TX_MAX_SUB];      /* Length factors */
187#define TX_FACTOR_ANY -1          /* When used alone, signals that the codelet
188                                   * supports all factors. Otherwise, if other
189                                   * factors are present, it signals that whatever
190                                   * remains will be supported, as long as the
191                                   * other factors are a component of the length */
192
193    int min_len;                  /* Minimum length of transform, must be >= 1 */
194    int max_len;                  /* Maximum length of transform */
195#define TX_LEN_UNLIMITED -1       /* Special length value to permit all lengths */
196
197    int (*init)(AVTXContext *s,   /* Optional callback for current context initialization. */
198                const struct FFTXCodelet *cd,
199                uint64_t flags,
200                FFTXCodeletOptions *opts,
201                int len, int inv,
202                const void *scale);
203
204    int (*uninit)(AVTXContext *s); /* Optional callback for uninitialization. */
205
206    int cpu_flags;                 /* CPU flags. If any negative flags like
207                                    * SLOW are present, will avoid picking.
208                                    * 0x0 to signal it's a C codelet */
209#define FF_TX_CPU_FLAGS_ALL 0x0    /* Special CPU flag for C */
210
211    int prio;                      /* < 0 = least, 0 = no pref, > 0 = prefer */
212} FFTXCodelet;
213
214struct AVTXContext {
215    /* Fields the root transform and subtransforms use or may use.
216     * NOTE: This section is used by assembly, do not reorder or change */
217    int                len;             /* Length of the transform */
218    int                inv;             /* If transform is inverse */
219    int               *map;             /* Lookup table(s) */
220    TXComplex         *exp;             /* Any non-pre-baked multiplication factors needed */
221    TXComplex         *tmp;             /* Temporary buffer, if needed */
222
223    AVTXContext       *sub;             /* Subtransform context(s), if needed */
224    av_tx_fn           fn[TX_MAX_SUB];  /* Function(s) for the subtransforms */
225    int                nb_sub;          /* Number of subtransforms.
226                                         * The reason all of these are set here
227                                         * rather than in each separate context
228                                         * is to eliminate extra pointer
229                                         * dereferences. */
230
231    /* Fields mainly useul/applicable for the root transform or initialization.
232     * Fields below are not used by assembly code. */
233    const FFTXCodelet *cd[TX_MAX_SUB];  /* Subtransform codelets */
234    const FFTXCodelet *cd_self;         /* Codelet for the current context */
235    enum AVTXType      type;            /* Type of transform */
236    uint64_t           flags;           /* A combination of AVTXFlags and
237                                         * codelet flags used when creating */
238    float              scale_f;
239    double             scale_d;
240    void              *opaque;          /* Free to use by implementations */
241};
242
243/* Create a subtransform in the current context with the given parameters.
244 * The flags parameter from FFTXCodelet.init() should be preserved as much
245 * as that's possible.
246 * MUST be called during the sub() callback of each codelet. */
247int ff_tx_init_subtx(AVTXContext *s, enum AVTXType type,
248                     uint64_t flags, FFTXCodeletOptions *opts,
249                     int len, int inv, const void *scale);
250
251/*
252 * Generates the PFA permutation table into AVTXContext->pfatab. The end table
253 * is appended to the start table.
254 */
255int ff_tx_gen_compound_mapping(AVTXContext *s, int n, int m);
256
257/*
258 * Generates a standard-ish (slightly modified) Split-Radix revtab into
259 * AVTXContext->map. Invert lookup changes how the mapping needs to be applied.
260 * If it's set to 0, it has to be applied like out[map[i]] = in[i], otherwise
261 * if it's set to 1, has to be applied as out[i] = in[map[i]]
262 */
263int ff_tx_gen_ptwo_revtab(AVTXContext *s, int invert_lookup);
264
265/*
266 * Generates an index into AVTXContext->inplace_idx that if followed in the
267 * specific order, allows the revtab to be done in-place. The sub-transform
268 * and its map should already be initialized.
269 */
270int ff_tx_gen_ptwo_inplace_revtab_idx(AVTXContext *s);
271
272/*
273 * This generates a parity-based revtab of length len and direction inv.
274 *
275 * Parity means even and odd complex numbers will be split, e.g. the even
276 * coefficients will come first, after which the odd coefficients will be
277 * placed. For example, a 4-point transform's coefficients after reordering:
278 * z[0].re, z[0].im, z[2].re, z[2].im, z[1].re, z[1].im, z[3].re, z[3].im
279 *
280 * The basis argument is the length of the largest non-composite transform
281 * supported, and also implies that the basis/2 transform is supported as well,
282 * as the split-radix algorithm requires it to be.
283 *
284 * The dual_stride argument indicates that both the basis, as well as the
285 * basis/2 transforms support doing two transforms at once, and the coefficients
286 * will be interleaved between each pair in a split-radix like so (stride == 2):
287 * tx1[0], tx1[2], tx2[0], tx2[2], tx1[1], tx1[3], tx2[1], tx2[3]
288 * A non-zero number switches this on, with the value indicating the stride
289 * (how many values of 1 transform to put first before switching to the other).
290 * Must be a power of two or 0. Must be less than the basis.
291 * Value will be clipped to the transform size, so for a basis of 16 and a
292 * dual_stride of 8, dual 8-point transforms will be laid out as if dual_stride
293 * was set to 4.
294 * Usually you'll set this to half the complex numbers that fit in a single
295 * register or 0. This allows to reuse SSE functions as dual-transform
296 * functions in AVX mode.
297 *
298 * If length is smaller than basis/2 this function will not do anything.
299 */
300int ff_tx_gen_split_radix_parity_revtab(AVTXContext *s, int invert_lookup,
301                                        int basis, int dual_stride);
302
303/* Typed init function to initialize shared tables. Will initialize all tables
304 * for all factors of a length. */
305void ff_tx_init_tabs_float (int len);
306void ff_tx_init_tabs_double(int len);
307void ff_tx_init_tabs_int32 (int len);
308
309/* Typed init function to initialize an MDCT exptab in a context. */
310int  ff_tx_mdct_gen_exp_float (AVTXContext *s);
311int  ff_tx_mdct_gen_exp_double(AVTXContext *s);
312int  ff_tx_mdct_gen_exp_int32 (AVTXContext *s);
313
314/* Lists of codelets */
315extern const FFTXCodelet * const ff_tx_codelet_list_float_c       [];
316extern const FFTXCodelet * const ff_tx_codelet_list_float_x86     [];
317
318extern const FFTXCodelet * const ff_tx_codelet_list_double_c      [];
319
320extern const FFTXCodelet * const ff_tx_codelet_list_int32_c       [];
321
322#endif /* AVUTIL_TX_PRIV_H */
323