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