2010-03-07 23:56:45 +02:00
|
|
|
/*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2021-07-23 14:06:58 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-02-01 12:31:59 +03:00
|
|
|
#include "libavutil/attributes.h"
|
2021-07-23 14:06:58 +02:00
|
|
|
#include "libavutil/macros.h"
|
2010-03-07 23:56:45 +02:00
|
|
|
#include "libavutil/mem.h"
|
2022-11-10 12:23:38 +02:00
|
|
|
#include "libavutil/tx.h"
|
2010-03-07 23:56:45 +02:00
|
|
|
#include "avfft.h"
|
|
|
|
|
2022-11-10 12:23:38 +02:00
|
|
|
typedef struct AVTXWrapper {
|
|
|
|
AVTXContext *ctx;
|
|
|
|
av_tx_fn fn;
|
|
|
|
|
|
|
|
AVTXContext *ctx2;
|
|
|
|
av_tx_fn fn2;
|
|
|
|
|
|
|
|
ptrdiff_t stride;
|
2022-11-10 12:26:59 +02:00
|
|
|
int len;
|
|
|
|
int inv;
|
2023-08-07 12:07:10 +02:00
|
|
|
|
|
|
|
float *tmp;
|
|
|
|
int out_of_place;
|
2022-11-10 12:23:38 +02:00
|
|
|
} AVTXWrapper;
|
|
|
|
|
2010-03-07 23:56:45 +02:00
|
|
|
/* FFT */
|
|
|
|
|
|
|
|
FFTContext *av_fft_init(int nbits, int inverse)
|
|
|
|
{
|
2022-11-10 12:23:38 +02:00
|
|
|
int ret;
|
|
|
|
float scale = 1.0f;
|
|
|
|
AVTXWrapper *s = av_malloc(sizeof(*s));
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
2010-03-07 23:56:45 +02:00
|
|
|
|
2022-11-10 12:23:38 +02:00
|
|
|
ret = av_tx_init(&s->ctx, &s->fn, AV_TX_FLOAT_FFT, inverse, 1 << nbits,
|
|
|
|
&scale, AV_TX_INPLACE);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-03-07 23:56:45 +02:00
|
|
|
|
2022-11-10 12:23:38 +02:00
|
|
|
return (FFTContext *)s;
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void av_fft_permute(FFTContext *s, FFTComplex *z)
|
|
|
|
{
|
2022-11-10 12:23:38 +02:00
|
|
|
/* Empty */
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void av_fft_calc(FFTContext *s, FFTComplex *z)
|
|
|
|
{
|
2022-11-10 12:23:38 +02:00
|
|
|
AVTXWrapper *w = (AVTXWrapper *)s;
|
|
|
|
w->fn(w->ctx, z, (void *)z, sizeof(AVComplexFloat));
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
|
2013-05-02 11:20:12 +03:00
|
|
|
av_cold void av_fft_end(FFTContext *s)
|
2010-03-07 23:56:45 +02:00
|
|
|
{
|
|
|
|
if (s) {
|
2022-11-10 12:23:38 +02:00
|
|
|
AVTXWrapper *w = (AVTXWrapper *)s;
|
|
|
|
av_tx_uninit(&w->ctx);
|
|
|
|
av_tx_uninit(&w->ctx2);
|
|
|
|
av_free(w);
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FFTContext *av_mdct_init(int nbits, int inverse, double scale)
|
|
|
|
{
|
2022-11-10 12:26:33 +02:00
|
|
|
int ret;
|
|
|
|
float scale_f = scale;
|
|
|
|
AVTXWrapper *s = av_malloc(sizeof(*s));
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
2010-03-07 23:56:45 +02:00
|
|
|
|
2022-11-10 12:26:33 +02:00
|
|
|
ret = av_tx_init(&s->ctx, &s->fn, AV_TX_FLOAT_MDCT, inverse, 1 << (nbits - 1), &scale_f, 0);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-03-07 23:56:45 +02:00
|
|
|
|
2022-11-10 12:26:33 +02:00
|
|
|
if (inverse) {
|
|
|
|
ret = av_tx_init(&s->ctx2, &s->fn2, AV_TX_FLOAT_MDCT, inverse, 1 << (nbits - 1),
|
|
|
|
&scale_f, AV_TX_FULL_IMDCT);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_tx_uninit(&s->ctx);
|
|
|
|
av_free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (FFTContext *)s;
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void av_imdct_calc(FFTContext *s, FFTSample *output, const FFTSample *input)
|
|
|
|
{
|
2022-11-10 12:26:33 +02:00
|
|
|
AVTXWrapper *w = (AVTXWrapper *)s;
|
|
|
|
w->fn2(w->ctx2, output, (void *)input, sizeof(float));
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void av_imdct_half(FFTContext *s, FFTSample *output, const FFTSample *input)
|
|
|
|
{
|
2022-11-10 12:26:33 +02:00
|
|
|
AVTXWrapper *w = (AVTXWrapper *)s;
|
|
|
|
w->fn(w->ctx, output, (void *)input, sizeof(float));
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void av_mdct_calc(FFTContext *s, FFTSample *output, const FFTSample *input)
|
|
|
|
{
|
2022-11-10 12:26:33 +02:00
|
|
|
AVTXWrapper *w = (AVTXWrapper *)s;
|
|
|
|
w->fn(w->ctx, output, (void *)input, sizeof(float));
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
|
2013-05-02 11:20:12 +03:00
|
|
|
av_cold void av_mdct_end(FFTContext *s)
|
2010-03-07 23:56:45 +02:00
|
|
|
{
|
|
|
|
if (s) {
|
2022-11-10 12:26:33 +02:00
|
|
|
AVTXWrapper *w = (AVTXWrapper *)s;
|
|
|
|
av_tx_uninit(&w->ctx);
|
|
|
|
av_free(w);
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RDFTContext *av_rdft_init(int nbits, enum RDFTransformType trans)
|
|
|
|
{
|
2022-11-10 12:26:59 +02:00
|
|
|
int ret;
|
|
|
|
float scale = trans == IDFT_C2R ? 0.5f : 1.0f;
|
|
|
|
AVTXWrapper *s;
|
2010-03-07 23:56:45 +02:00
|
|
|
|
2022-11-10 12:26:59 +02:00
|
|
|
/* The other 2 modes are unconventional, do not form an orthogonal
|
|
|
|
* transform, have never been useful, and so they're not implemented. */
|
|
|
|
if (trans != IDFT_C2R && trans != DFT_R2C)
|
|
|
|
return NULL;
|
2010-03-07 23:56:45 +02:00
|
|
|
|
2022-11-10 12:26:59 +02:00
|
|
|
s = av_malloc(sizeof(*s));
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = av_tx_init(&s->ctx, &s->fn, AV_TX_FLOAT_RDFT, trans == IDFT_C2R,
|
|
|
|
1 << nbits, &scale, AV_TX_INPLACE);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->stride = (trans == DFT_C2R) ? sizeof(float) : sizeof(AVComplexFloat);
|
|
|
|
s->len = 1 << nbits;
|
|
|
|
s->inv = trans == IDFT_C2R;
|
|
|
|
|
|
|
|
return (RDFTContext *)s;
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void av_rdft_calc(RDFTContext *s, FFTSample *data)
|
|
|
|
{
|
2022-11-10 12:26:59 +02:00
|
|
|
AVTXWrapper *w = (AVTXWrapper *)s;
|
|
|
|
if (w->inv)
|
|
|
|
FFSWAP(float, data[1], data[w->len]);
|
|
|
|
w->fn(w->ctx, data, (void *)data, w->stride);
|
|
|
|
if (!w->inv)
|
|
|
|
FFSWAP(float, data[1], data[w->len]);
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
|
2013-05-02 11:20:12 +03:00
|
|
|
av_cold void av_rdft_end(RDFTContext *s)
|
2010-03-07 23:56:45 +02:00
|
|
|
{
|
|
|
|
if (s) {
|
2022-11-10 12:26:59 +02:00
|
|
|
AVTXWrapper *w = (AVTXWrapper *)s;
|
|
|
|
av_tx_uninit(&w->ctx);
|
|
|
|
av_free(w);
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-23 21:48:16 +02:00
|
|
|
DCTContext *av_dct_init(int nbits, enum DCTTransformType inverse)
|
2010-03-07 23:56:45 +02:00
|
|
|
{
|
2023-08-07 12:07:10 +02:00
|
|
|
int ret;
|
|
|
|
const float scale_map[] = {
|
|
|
|
[DCT_II] = 0.5f,
|
|
|
|
[DCT_III] = 1.0f / (1 << nbits),
|
|
|
|
[DCT_I] = 0.5f,
|
|
|
|
[DST_I] = 2.0f,
|
|
|
|
};
|
|
|
|
static const enum AVTXType type_map[] = {
|
|
|
|
[DCT_II] = AV_TX_FLOAT_DCT,
|
|
|
|
[DCT_III] = AV_TX_FLOAT_DCT,
|
|
|
|
[DCT_I] = AV_TX_FLOAT_DCT_I,
|
|
|
|
[DST_I] = AV_TX_FLOAT_DST_I,
|
|
|
|
};
|
|
|
|
|
|
|
|
AVTXWrapper *s = av_malloc(sizeof(*s));
|
|
|
|
if (!s)
|
|
|
|
return NULL;
|
2010-03-07 23:56:45 +02:00
|
|
|
|
2023-08-07 12:07:10 +02:00
|
|
|
s->len = (1 << nbits);
|
|
|
|
s->out_of_place = (inverse == DCT_I) || (inverse == DST_I);
|
2010-03-07 23:56:45 +02:00
|
|
|
|
2023-08-07 12:07:10 +02:00
|
|
|
ret = av_tx_init(&s->ctx, &s->fn, type_map[inverse],
|
|
|
|
(inverse == DCT_III), 1 << (nbits - (inverse == DCT_III)),
|
|
|
|
&scale_map[inverse], s->out_of_place ? 0 : AV_TX_INPLACE);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s->out_of_place) {
|
|
|
|
s->tmp = av_malloc((1 << (nbits + 1))*sizeof(float));
|
|
|
|
if (!s->tmp) {
|
|
|
|
av_tx_uninit(&s->ctx);
|
|
|
|
av_free(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (DCTContext *)s;
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void av_dct_calc(DCTContext *s, FFTSample *data)
|
|
|
|
{
|
2023-08-07 12:07:10 +02:00
|
|
|
AVTXWrapper *w = (AVTXWrapper *)s;
|
|
|
|
if (w->out_of_place) {
|
|
|
|
memcpy(w->tmp, data, w->len*sizeof(float));
|
|
|
|
w->fn(w->ctx, (void *)data, w->tmp, sizeof(float));
|
|
|
|
} else {
|
|
|
|
w->fn(w->ctx, data, (void *)data, sizeof(float));
|
|
|
|
}
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
|
2013-05-02 11:20:12 +03:00
|
|
|
av_cold void av_dct_end(DCTContext *s)
|
2010-03-07 23:56:45 +02:00
|
|
|
{
|
|
|
|
if (s) {
|
2023-08-07 12:07:10 +02:00
|
|
|
AVTXWrapper *w = (AVTXWrapper *)s;
|
|
|
|
av_tx_uninit(&w->ctx);
|
|
|
|
av_free(w->tmp);
|
|
|
|
av_free(w);
|
2010-03-07 23:56:45 +02:00
|
|
|
}
|
|
|
|
}
|