Skip to content

Commit

Permalink
fix: Replace deprecated FFmpeg avfft.h APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeWang000000 committed Feb 5, 2025
1 parent fa10310 commit ac42a85
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/spek-fft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

#define __STDC_CONSTANT_MACROS
extern "C" {
#include <libavutil/version.h>
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(58, 18, 100)
#define USE_LIBAVUTIL_TX_API
#include <libavutil/tx.h>
#else
#include <libavcodec/avfft.h>
#endif
}

#include "spek-fft.h"
Expand All @@ -16,26 +22,46 @@ class FFTPlanImpl : public FFTPlan
void execute() override;

private:
#ifdef USE_LIBAVUTIL_TX_API
struct AVTXContext *cx;
av_tx_fn tx_func;
#else
struct RDFTContext *cx;
#endif
};

std::unique_ptr<FFTPlan> FFT::create(int nbits)
{
return std::unique_ptr<FFTPlan>(new FFTPlanImpl(nbits));
}

FFTPlanImpl::FFTPlanImpl(int nbits) : FFTPlan(nbits), cx(av_rdft_init(nbits, DFT_R2C))
FFTPlanImpl::FFTPlanImpl(int nbits) : FFTPlan(nbits)
{
#ifdef USE_LIBAVUTIL_TX_API
const float scale = 1.f;
av_tx_init(&this->cx, &this->tx_func, AV_TX_FLOAT_RDFT, 0, 1 << nbits, &scale, AV_TX_INPLACE);
#else
this->cx = av_rdft_init(nbits, DFT_R2C);
#endif
}

FFTPlanImpl::~FFTPlanImpl()
{
#ifdef USE_LIBAVUTIL_TX_API
av_tx_uninit(&this->cx);
#else
av_rdft_end(this->cx);
#endif
}

void FFTPlanImpl::execute()
{
#ifdef USE_LIBAVUTIL_TX_API
float *input = this->get_input();
this->tx_func(this->cx, input, input, sizeof(float));
#else
av_rdft_calc(this->cx, this->get_input());
#endif

// Calculate magnitudes.
int n = this->get_input_size();
Expand Down
2 changes: 1 addition & 1 deletion src/spek-fft.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FFTPlan
{
// FFmpeg uses various assembly optimizations which expect
// input data to be aligned by up to 32 bytes (e.g. AVX)
this->input = (float*) av_malloc(sizeof(float) * input_size);
this->input = (float*) av_malloc(sizeof(float) * (input_size + 2));
}

virtual ~FFTPlan()
Expand Down

0 comments on commit ac42a85

Please sign in to comment.