summaryrefslogtreecommitdiff
path: root/libavutil/tx.c
diff options
context:
space:
mode:
authorLynne <dev@lynne.ee>2021-02-10 17:58:22 +0100
committerLynne <dev@lynne.ee>2021-02-21 17:05:16 +0100
commit5ca40d6d941bd802a7b953b3a21cd075725d5c98 (patch)
tree9403dfc23e7f2ec88baa4866f9f5d438d28600d7 /libavutil/tx.c
parentaa34e99f889af37e5c719737de1347e4985e0196 (diff)
downloadffmpeg-5ca40d6d941bd802a7b953b3a21cd075725d5c98.tar.gz
lavu/tx: support in-place FFT transforms
This commit adds support for in-place FFT transforms. Since our internal transforms were all in-place anyway, this only changes the permutation on the input. Unfortunately, research papers were of no help here. All focused on dry hardware implementations, where permutes are free, or on software implementations where binary bloat is of no concern so storing dozen times the transforms for each permutation and version is not considered bad practice. Still, for a pure C implementation, it's only around 28% slower than the multi-megabyte FFTW3 in unaligned mode. Unlike a closed permutation like with PFA, split-radix FFT bit-reversals contain multiple NOPs, multiple simple swaps, and a few chained swaps, so regular single-loop single-state permute loops were not possible. Instead, we filter out parts of the input indices which are redundant. This allows for a single branch, and with some clever AVX512 asm, could possibly be SIMD'd without refactoring. The inplace_idx array is guaranteed to never be larger than the revtab array, and in practice only requires around log2(len) entries. The power-of-two MDCTs can be done in-place as well. And it's possible to eliminate a copy in the compound MDCTs too, however it'll be slower than doing them out of place, and we'd need to dirty the input array.
Diffstat (limited to 'libavutil/tx.c')
-rw-r--r--libavutil/tx.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/libavutil/tx.c b/libavutil/tx.c
index 3b0568a5e1..49d5e125ae 100644
--- a/libavutil/tx.c
+++ b/libavutil/tx.c
@@ -107,6 +107,42 @@ int ff_tx_gen_ptwo_revtab(AVTXContext *s)
return 0;
}
+int ff_tx_gen_ptwo_inplace_revtab_idx(AVTXContext *s)
+{
+ int nb_inplace_idx = 0;
+
+ if (!(s->inplace_idx = av_malloc(s->m*sizeof(*s->inplace_idx))))
+ return AVERROR(ENOMEM);
+
+ for (int d = 1; d < s->m; d++) {
+ int src = d;
+ int dst = s->revtab[src];
+
+ if (dst <= src)
+ continue;
+
+ int found = 0;
+ int start_src = src;
+ do {
+ src = dst;
+ for (int j = 0; j < nb_inplace_idx; j++) {
+ if (dst == s->inplace_idx[j]) {
+ found = 1;
+ break;
+ }
+ }
+ dst = s->revtab[dst];
+ } while (dst != start_src && !found);
+
+ if (!found)
+ s->inplace_idx[nb_inplace_idx++] = start_src;
+ }
+
+ s->inplace_idx[nb_inplace_idx++] = 0;
+
+ return 0;
+}
+
av_cold void av_tx_uninit(AVTXContext **ctx)
{
if (!(*ctx))
@@ -115,6 +151,7 @@ av_cold void av_tx_uninit(AVTXContext **ctx)
av_free((*ctx)->pfatab);
av_free((*ctx)->exptab);
av_free((*ctx)->revtab);
+ av_free((*ctx)->inplace_idx);
av_free((*ctx)->tmp);
av_freep(ctx);