Skip to content

Commit 482f247

Browse files
committed
[AT3P] Use fast DCT-IV calculation for PQF
1 parent 80a30dc commit 482f247

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

src/atrac/atrac3plus_pqf/atrac3plus_pqf.c

+10-13
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "atrac3plus_pqf.h"
3131
#include "atrac3plus_pqf_data.h"
3232

33+
#include "../../mdct/dct.h"
34+
3335
/*
3436
* Number of subbands to split input signal
3537
*/
@@ -49,18 +51,9 @@
4951

5052
static float fir[PROTO_SZ];
5153

52-
static void dct4(float* out, const float* x, int N, float scale) {
53-
for (int k = 0; k < N; k++) {
54-
double sum = 0;
55-
for (int n = 0; n < N; n++) {
56-
sum += ((double)x[n] * cosl((M_PI/(double)N) * ((double)n + 0.5) * ((double)k + 0.5)));
57-
}
58-
out[N - 1 - k] = sum * scale;
59-
}
60-
}
61-
6254
struct at3plus_pqf_a_ctx {
6355
float buf[FRAME_SZ + OVERLAP_SZ];
56+
atde_dct_ctx_t dct_ctx;
6457
};
6558

6659
static void init(void)
@@ -95,7 +88,7 @@ static void vectoring(const float* const x, float* y)
9588
}
9689
}
9790

98-
static void matrixing(const float* y, float* samples )
91+
static void matrixing(atde_dct_ctx_t ctx, const float* y, float* samples )
9992
{
10093
float yy[SUBBANDS_NUM];
10194
float res[SUBBANDS_NUM];
@@ -105,7 +98,7 @@ static void matrixing(const float* y, float* samples )
10598
yy[i + 8] = y[i + 16] + y[31 - i];
10699
}
107100

108-
dct4(res, yy, SUBBANDS_NUM, 128.0 * 512.0);
101+
atde_do_dct4_16(ctx, yy, res);
109102

110103
for (int i = 0; i < SUBBANDS_NUM; i++) {
111104
samples[i * SUBBAND_SIZE] = res[SUBBANDS_NUM - 1 - i];
@@ -120,13 +113,17 @@ at3plus_pqf_a_ctx_t at3plus_pqf_create_a_ctx()
120113
ctx->buf[i] = 0.0;
121114
}
122115

116+
ctx->dct_ctx = atde_create_dct4_16(128 * 512.0);
117+
123118
init();
124119

125120
return ctx;
126121
}
127122

128123
void at3plus_pqf_free_a_ctx(at3plus_pqf_a_ctx_t ctx)
129124
{
125+
atde_free_dct_ctx(ctx->dct_ctx);
126+
130127
free(ctx);
131128
}
132129

@@ -142,7 +139,7 @@ void at3plus_pqf_do_analyse(at3plus_pqf_a_ctx_t ctx, const float* in, float* out
142139

143140
for (int i = 0; i < SUBBAND_SIZE; i++) {
144141
vectoring(x, y);
145-
matrixing (y, &out[i]);
142+
matrixing (ctx->dct_ctx, y, &out[i]);
146143
x += SUBBANDS_NUM;
147144
}
148145

src/mdct/mdct.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include "mdct.h"
20+
#include "dct.h"
2021
#include <iostream>
2122

2223
namespace NMDCT {
@@ -51,3 +52,31 @@ TMDCTBase::~TMDCTBase()
5152
}
5253

5354
} // namespace NMDCT
55+
56+
struct atde_dct_ctx {
57+
atde_dct_ctx(float scale)
58+
: mdct(scale)
59+
{}
60+
NMDCT::TMIDCT<32, float> mdct;
61+
};
62+
63+
atde_dct_ctx_t atde_create_dct4_16(float scale)
64+
{
65+
return new atde_dct_ctx(32.0 * scale);
66+
}
67+
68+
void atde_free_dct_ctx(atde_dct_ctx_t ctx)
69+
{
70+
delete ctx;
71+
}
72+
73+
void atde_do_dct4_16(atde_dct_ctx_t ctx, const float* in, float* out)
74+
{
75+
//TODO: rewrire more optimal
76+
const auto& x = ctx->mdct(in);
77+
78+
for (int i = 0; i < 16; i++) {
79+
out[i] = x[i + 8] * -1.0;
80+
}
81+
}
82+

src/mdct/mdct.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ class TMDCTBase {
3939
};
4040

4141

42-
template<size_t TN>
42+
template<size_t TN, typename TIO = TFloat>
4343
class TMDCT : public TMDCTBase {
44-
std::vector<TFloat> Buf;
44+
std::vector<TIO> Buf;
4545
public:
4646
TMDCT(float scale = 1.0)
4747
: TMDCTBase(TN, scale)
4848
, Buf(TN/2)
4949
{
5050
}
51-
const std::vector<TFloat>& operator()(TFloat* in) {
51+
const std::vector<TIO>& operator()(const TIO* in) {
5252

5353
const size_t n2 = N >> 1;
5454
const size_t n4 = N >> 2;
@@ -104,15 +104,15 @@ class TMDCT : public TMDCTBase {
104104
}
105105
};
106106

107-
template<size_t TN>
107+
template<size_t TN, typename TIO = TFloat>
108108
class TMIDCT : public TMDCTBase {
109-
std::vector<TFloat> Buf;
109+
std::vector<TIO> Buf;
110110
public:
111111
TMIDCT(float scale = TN)
112112
: TMDCTBase(TN, scale/2)
113113
, Buf(TN)
114114
{}
115-
const std::vector<TFloat>& operator()(TFloat* in) {
115+
const std::vector<TIO>& operator()(const TIO* in) {
116116

117117
const size_t n2 = N >> 1;
118118
const size_t n4 = N >> 2;

test/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ set(at3plus_pqf_ut
3737
../src/atrac/atrac3plus_pqf/ut/ipqf_ut.cpp
3838
../src/atrac/atrac3plus_pqf/ut/atrac3plusdsp.c
3939
../src/atrac/atrac3plus_pqf/atrac3plus_pqf.c
40+
../src/mdct/mdct.cpp
4041
)
4142

4243
add_executable(at3plus_pqf_ut ${at3plus_pqf_ut})
4344

4445
target_link_libraries(at3plus_pqf_ut
4546
m
47+
fft_impl
4648
GTest::gtest_main
4749
)
4850

0 commit comments

Comments
 (0)