aboutsummaryrefslogtreecommitdiffstats
path: root/src/atracdenc.cpp
blob: 3ec20263a6a6958da8d73b04cd352459d8b559fe (plain) (blame)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <vector>

#include "atracdenc.h"
#include "bitstream/bitstream.h"
#include "atrac/atrac1.h"
#include "atrac/atrac1_dequantiser.h"
#include "atrac/atrac1_qmf.h"
#include "atrac/atrac1_bitalloc.h"

namespace NAtracDEnc {
using namespace std;
using namespace NBitStream;
using namespace NAtrac1;
TAtrac1Processor::TAtrac1Processor(TAeaPtr&& aea, bool mono)
    : MixChannel(mono)
    , Aea(std::move(aea))
{
}

static void vector_fmul_window(double *dst, const double *src0,
                                const double *src1, const double *win, int len)
{
    int i, j;

    dst  += len;
    win  += len;
    src0 += len;

    for (i = -len, j = len - 1; i < 0; i++, j--) {
        double s0 = src0[i];
        double s1 = src1[j];
        double wi = win[i];
        double wj = win[j];
        dst[i] = s0 * wj - s1 * wi;
        dst[j] = s0 * wi + s1 * wj;
    }
}

vector<double> mdct(double* x, int N) {
    vector<double> res;
    for (int k = 0; k < N; k++) {
        double sum = 0;
        for (int n = 0; n < 2 * N; n++) {
            sum += x[n]* cos((M_PI/N) * ((double)n + 0.5 + N/2) * ((double)k + 0.5));
        }
        res.push_back(sum);
    }
    return res;
}
	
vector<double> midct(double* x, int N) {
    vector<double> res;
    for (int n = 0; n < 2 * N; n++) {
        double sum = 0;
        for (int k = 0; k < N; k++) {
            sum += (x[k] * cos((M_PI/N) * ((double)n + 0.5 + N/2) * ((double)k + 0.5)));
        }
        sum = sum / N;
        res.push_back(sum);
    }
    return res;
}


void TAtrac1Processor::Mdct(double Specs[512], double* low, double* mid, double* hi) {
    uint32_t pos = 0;
    for (uint32_t band = 0; band < QMF_BANDS; band++) {
        double* srcBuf = (band == 0) ? low : (band == 1) ? mid : hi;
        uint32_t bufSz = (band == 2) ? 256 : 128; 
        uint32_t xxx = (band == 2) ? 112 : 48; 
        double tmp[512];
        //if (band == 2) {
        //    for (uint32_t i = 0; i < bufSz; i++) {
        //        srcBuf[i] = srcBuf[i] * 2;
        //    }
        //}
        for (uint32_t i = 0; i < 512; i++)
            tmp[i] = 0;
        memcpy(&tmp[xxx], &srcBuf[bufSz], 32 * sizeof(double));
        for (int i = 0; i < 32; i++) {
            srcBuf[bufSz + i] = TAtrac1Data::SineWindow[i] * srcBuf[bufSz - 32 + i];
            srcBuf[bufSz - 32 + i] = TAtrac1Data::SineWindow[31 - i] * srcBuf[bufSz - 32 + i];
        }
        memcpy(&tmp[xxx+32], &srcBuf[0], bufSz * sizeof(double));
        const vector<double>&  sp = mdct(&tmp[0], bufSz);
        if (band) {
            for (uint32_t j = 0; j < sp.size()/2; j++) {
                Specs[pos+j] = sp[bufSz - 1 -j];
                Specs[pos + bufSz - 1 -j] = sp[j];
            }
        } else {
            for (uint32_t i = 0; i < sp.size(); i++) {
                Specs[pos + i] = sp[i];
            }
        }
        /*
        if (band == 2) {
            for (int i = 0; i < bufSz * 2; i++) {
                cout << "tmp " << i << " " << tmp[i] << endl;
            }
            for (int i = 0; i < sp.size(); ++i ) {
                cout << "band2 " << i << "  " << Specs[pos + i] << " " << endl;
            }
        }
        */
        pos += bufSz;
    } 
}
void TAtrac1Processor::IMdct(double Specs[512], const TBlockSize& mode, double* low, double* mid, double* hi) {
    uint32_t pos = 0;
    for (uint32_t band = 0; band < QMF_BANDS; band++) {
        const uint32_t numMdctBlocks = 1 << mode.LogCount[band];
        const uint32_t blockSize = (numMdctBlocks == 1) ? ((band == 2) ? 256 : 128) : 32;
        //if (blockSize == 32) {
        //    cout << "SHORT: " << numMdctBlocks << " band: " << band << endl;
        //}
        uint32_t start = 0;

        double* dstBuf = (band == 0) ? low : (band == 1) ? mid : hi;


        vector<double> invBuf;
        invBuf.resize(512);
        double* prevBuf = &dstBuf[blockSize*2  - 16];
        static uint32_t fc;
        for (uint32_t block = 0; block < numMdctBlocks; block++) {

    //        cout << "fc counter: " << fc++ << endl;
            if (band) {
                for (uint32_t j = 0; j < blockSize/2; j++) {
                    double tmp = Specs[pos+j];
                    Specs[pos+j] = Specs[pos + blockSize - 1 -j];
                    Specs[pos + blockSize - 1 -j] = tmp;
                }
            }
            vector<double> inv = midct(&Specs[pos], blockSize);
            for (int i = 0; i < (inv.size()/2); i++) {

                invBuf[start+i] = ((blockSize == 32) ? 32.0 : 128.0) * inv[i + inv.size()/4];
            }

            vector_fmul_window(dstBuf + start, prevBuf, &invBuf[start], &TAtrac1Data::SineWindow[0], 16);

            prevBuf = &invBuf[start+16];
            start += blockSize;
            pos += blockSize;
        }
        if (numMdctBlocks == 1)
            memcpy(dstBuf + 32, &invBuf[16], ((band == 2) ? 240 : 112) * sizeof(double));

        for (int j = 0; j < 16; j++) {
            dstBuf[blockSize*2 - 16  + j] = invBuf[blockSize - 16 + j];
        }
    }
}
TPCMEngine<double>::TProcessLambda TAtrac1Processor::GetDecodeLambda() {
    return [this](vector<double>* data) {
        double sum[512];
        const uint32_t srcChannels = Aea->GetChannelNum();
        for (uint32_t channel = 0; channel < srcChannels; channel++) {
            std::unique_ptr<TAea::TFrame> frame(Aea->ReadFrame());
            TBitStream bitstream(&(*frame.get())[0], frame->size());
      //      cout << "frame size: " << bitstream.GetBufSize() << endl;

            TBlockSize mode(&bitstream);
            TAtrac1Dequantiser dequantiser;
            vector<double> specs;
            specs.resize(512);;
            dequantiser.Dequant(&bitstream, mode, &specs[0]);

            IMdct(&specs[0], mode, &PcmBufLow[channel][0], &PcmBufMid[channel][0], &PcmBufHi[channel][0]);
            SynthesisFilterBank[channel].Synthesis(&sum[0], &PcmBufLow[channel][0], &PcmBufMid[channel][0], &PcmBufHi[channel][0]);
            const int numChannel = data[0].size();
            for (int i = 0; i < NumSamples; ++i) {
                data[i][srcChannels - 1-channel] = sum[i];
            }
        }

    };
}


TPCMEngine<double>::TProcessLambda TAtrac1Processor::GetEncodeLambda() {
    const uint32_t srcChannels = Aea->GetChannelNum();
    //TODO: should not be here
    //vector<char> dummy;
    //dummy.resize(212 * srcChannels);
    //Aea->WriteFrame(dummy);
    //cout << "Encode, channels: " << srcChannels << endl;
    vector<IAtrac1BitAlloc*> bitAlloc;
    for (int i = 0; i < srcChannels; i++)
        bitAlloc.push_back(new TAtrac1SimpleBitAlloc(Aea.get()));
        //bitAlloc.push_back(new TAtrac1PsyBitAlloc(Aea.get()));

    return [this, srcChannels, bitAlloc](vector<double>* data) {
        for (uint32_t channel = 0; channel < srcChannels; channel++) {
            double src[NumSamples];
            double sum[512];
            vector<double> specs;
            specs.resize(512);
            for (int i = 0; i < NumSamples; ++i) {
                src[i] = data[i][channel] / 256.0;
            }
            SplitFilterBank[channel].Split(&src[0], &PcmBufLow[channel][0], &PcmBufMid[channel][0], &PcmBufHi[channel][0]);

            Mdct(&specs[0], &PcmBufLow[channel][0], &PcmBufMid[channel][0], &PcmBufHi[channel][0]);
            bitAlloc[channel]->Write(Scaler.Scale(specs));
        }
    };
}


}