forked from hegdi/libldacdec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imdct.c
154 lines (131 loc) · 3.77 KB
/
imdct.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <math.h>
#include <stdint.h>
#include "ldacdec.h"
#include "utility.h"
double MdctWindow[3][256];
double ImdctWindow[3][256];
double SinTables[9][256];
double CosTables[9][256];
int ShuffleTables[9][256];
static void GenerateTrigTables(int sizeBits)
{
const int size = 1 << sizeBits;
double* sinTab = SinTables[sizeBits];
double* cosTab = CosTables[sizeBits];
for (int i = 0; i < size; i++)
{
const double value = M_PI * (4 * i + 1) / (4 * size);
sinTab[i] = sin(value);
cosTab[i] = cos(value);
}
}
static void GenerateShuffleTable(int sizeBits)
{
const int size = 1 << sizeBits;
int* table = ShuffleTables[sizeBits];
for (int i = 0; i < size; i++)
{
table[i] = BitReverse32(i ^ (i / 2), sizeBits);
}
}
static void GenerateMdctWindow(int frameSizePower)
{
const int frameSize = 1 << frameSizePower;
double* mdct = MdctWindow[frameSizePower - 6];
for (int i = 0; i < frameSize; i++)
{
mdct[i] = (sin(((i + 0.5) / frameSize - 0.5) * M_PI) + 1.0) * 0.5;
}
}
static void GenerateImdctWindow(int frameSizePower)
{
const int frameSize = 1 << frameSizePower;
double* imdct = ImdctWindow[frameSizePower - 6];
double* mdct = MdctWindow[frameSizePower - 6];
for (int i = 0; i < frameSize; i++)
{
imdct[i] = mdct[i] / (mdct[frameSize - 1 - i] * mdct[frameSize - 1 - i] + mdct[i] * mdct[i]);
}
}
void InitMdct()
{
for (int i = 0; i < 9; i++)
{
GenerateTrigTables(i);
GenerateShuffleTable(i);
}
GenerateMdctWindow(7);
GenerateImdctWindow(7);
GenerateMdctWindow(8);
GenerateImdctWindow(8);
}
static void Dct4(Mdct* mdct, float* input, float* output);
void RunImdct(Mdct* mdct, float* input, float* output)
{
const int size = 1 << mdct->Bits;
const int half = size / 2;
float dctOut[MAX_FRAME_SAMPLES] = { 0.f };
const double* window = ImdctWindow[mdct->Bits - 6];
double* previous = mdct->ImdctPrevious;
Dct4(mdct, input, dctOut);
for (int i = 0; i < half; i++)
{
output[i] = window[i] * dctOut[i + half] + previous[i];
output[i + half] = window[i + half] * -dctOut[size - 1 - i] - previous[i + half];
previous[i] = window[size - 1 - i] * -dctOut[half - i - 1];
previous[i + half] = window[half - i - 1] * dctOut[i];
}
}
static void Dct4(Mdct* mdct, float* input, float* output)
{
int MdctBits = mdct->Bits;
int MdctSize = 1 << MdctBits;
const int* shuffleTable = ShuffleTables[MdctBits];
const double* sinTable = SinTables[MdctBits];
const double* cosTable = CosTables[MdctBits];
double dctTemp[MAX_FRAME_SAMPLES];
int size = MdctSize;
int lastIndex = size - 1;
int halfSize = size / 2;
for (int i = 0; i < halfSize; i++)
{
int i2 = i * 2;
double a = input[i2];
double b = input[lastIndex - i2];
double sin = sinTable[i];
double cos = cosTable[i];
dctTemp[i2] = a * cos + b * sin;
dctTemp[i2 + 1] = a * sin - b * cos;
}
int stageCount = MdctBits - 1;
for (int stage = 0; stage < stageCount; stage++)
{
int blockCount = 1 << stage;
int blockSizeBits = stageCount - stage;
int blockHalfSizeBits = blockSizeBits - 1;
int blockSize = 1 << blockSizeBits;
int blockHalfSize = 1 << blockHalfSizeBits;
sinTable = SinTables[blockHalfSizeBits];
cosTable = CosTables[blockHalfSizeBits];
for (int block = 0; block < blockCount; block++)
{
for (int i = 0; i < blockHalfSize; i++)
{
int frontPos = (block * blockSize + i) * 2;
int backPos = frontPos + blockSize;
double a = dctTemp[frontPos] - dctTemp[backPos];
double b = dctTemp[frontPos + 1] - dctTemp[backPos + 1];
double sin = sinTable[i];
double cos = cosTable[i];
dctTemp[frontPos] += dctTemp[backPos];
dctTemp[frontPos + 1] += dctTemp[backPos + 1];
dctTemp[backPos] = a * cos + b * sin;
dctTemp[backPos + 1] = a * sin - b * cos;
}
}
}
for (int i = 0; i < MdctSize; i++)
{
output[i] = dctTemp[shuffleTable[i]];
}
}