aboutsummaryrefslogtreecommitdiffstats
path: root/src/atrac/at3p/at3p.cpp
blob: 514fa9687ec3add54fc6d925f296bf5f81fc84ab (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
/*
 * This file is part of AtracDEnc.
 *
 * AtracDEnc 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.
 *
 * AtracDEnc 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 AtracDEnc; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <atrac3p.h>

#include <atrac/atrac3plus_pqf/atrac3plus_pqf.h>

#include "at3p_bitstream.h"
#include "at3p_gha.h"
#include "at3p_mdct.h"
#include "at3p_tables.h"
#include <atrac/atrac_scale.h>

#include <cassert>
#include <vector>

using std::vector;

namespace NAtracDEnc {

class TAt3PEnc::TImpl {
public:
    TImpl(ICompressedOutput* out, int channels)
        : BitStream(out, 2048)
        , ChannelCtx(channels)
        , GhaProcessor(MakeGhaProcessor0(channels == 2))
    {}

    TPCMEngine::EProcessResult EncodeFrame(const float* data, int channels);
private:
    struct TChannelCtx {
        TChannelCtx()
            : PqfCtx(at3plus_pqf_create_a_ctx())
            , Specs(TAt3PEnc::NumSamples)
        {}

        ~TChannelCtx() {
            at3plus_pqf_free_a_ctx(PqfCtx);
        }

        at3plus_pqf_a_ctx_t PqfCtx;

        float* NextBuf = Buf1;
        float* CurBuf = nullptr;
        float Buf1[TAt3PEnc::NumSamples];
        float Buf2[TAt3PEnc::NumSamples];
        TAt3pMDCT::THistBuf MdctBuf;
        std::vector<float> Specs;
    };

    TAt3pMDCT Mdct;
    TScaler<NAt3p::TScaleTable> Scaler;
    TAt3PBitStream BitStream;
    vector<TChannelCtx> ChannelCtx;
    std::unique_ptr<IGhaProcessor> GhaProcessor;
};

TPCMEngine::EProcessResult TAt3PEnc::TImpl::
EncodeFrame(const float* data, int channels)
{
    int needMore = 0;
    for (int ch = 0; ch < channels; ch++) {
        float src[TAt3PEnc::NumSamples];
        for (size_t i = 0; i < NumSamples; ++i) {
            src[i] = data[i * channels  + ch];
        }

        at3plus_pqf_do_analyse(ChannelCtx[ch].PqfCtx, src, ChannelCtx[ch].NextBuf);
        if (ChannelCtx[ch].CurBuf == nullptr) {
            assert(ChannelCtx[ch].NextBuf == ChannelCtx[ch].Buf1);
            ChannelCtx[ch].CurBuf = ChannelCtx[ch].Buf2;
            std::swap(ChannelCtx[ch].NextBuf, ChannelCtx[ch].CurBuf);
            needMore++;
        }
    }

    if (needMore == channels) {
        return TPCMEngine::EProcessResult::LOOK_AHEAD;
    }

    assert(needMore == 0);

    const float* b1Cur = ChannelCtx[0].CurBuf;
    const float* b1Next = ChannelCtx[0].NextBuf;
    const float* b2Cur = (channels == 2) ? ChannelCtx[1].CurBuf : nullptr;
    const float* b2Next = (channels == 2) ? ChannelCtx[1].NextBuf : nullptr;

    auto tonalBlock = GhaProcessor->DoAnalize({b1Cur, b1Next}, {b2Cur, b2Next});

    std::vector<std::vector<TScaledBlock>> scaledBlocks;
    for (int ch = 0; ch < channels; ch++) {
        auto& c = ChannelCtx[ch];
        TAt3pMDCT::TPcmBandsData p;
        float tmp[2048];
        //TODO: scale window
        for (size_t i = 0; i < 2048; i++) {
            tmp[i] = c.CurBuf[i] / 32768.0;
        }
        for (size_t b = 0; b < 16; b++) {
            p[b] = tmp + b * 128;
        }
        Mdct.Do(c.Specs.data(), p, c.MdctBuf);

        const auto& block = Scaler.ScaleFrame(c.Specs, NAt3p::TScaleTable::TBlockSizeMod());
        scaledBlocks.push_back(block);
    }

    BitStream.WriteFrame(channels, tonalBlock, scaledBlocks);

    for (int ch = 0; ch < channels; ch++) {
        std::swap(ChannelCtx[ch].NextBuf, ChannelCtx[ch].CurBuf);
    }

    return TPCMEngine::EProcessResult::PROCESSED;
}

TAt3PEnc::TAt3PEnc(TCompressedOutputPtr&& out, int channels)
    : Out(std::move(out))
    , Channels(channels)
{
}

TPCMEngine::TProcessLambda TAt3PEnc::GetLambda() {
    Impl.reset(new TImpl(Out.get(), Channels));

    return [this](float* data, const TPCMEngine::ProcessMeta&) {
        return Impl->EncodeFrame(data, Channels);
    };
}

}