aboutsummaryrefslogtreecommitdiffstats
path: root/src/atrac/atrac_scale.h
blob: 499fac2131283b4761ca311f5f43292d1c4fc830 (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
#pragma once
#include <vector>
#include <map>
#include <cstdint>

#include "atrac1.h"
#include "../config.h"

namespace NAtracDEnc {

struct TScaledBlock {
	TScaledBlock(uint8_t sfi) : ScaleFactorIndex(sfi) {}
    /* const */ uint8_t ScaleFactorIndex = 0;
    std::vector<TFloat> Values;
};

class TBlockSize;

template <class TBaseData>
class TScaler : public TBaseData {
    std::map<TFloat, uint8_t>ScaleIndex;
public:
    TScaler() {
        for (int i = 0; i < 64; i++) {
            ScaleIndex[TBaseData::ScaleTable[i]] = i;
        }
    }
    TScaledBlock Scale(const TFloat* in, uint16_t len);
    std::vector<TScaledBlock> ScaleFrame(const std::vector<TFloat>& specs, const TBlockSize& blockSize);
};

class TBlockSize {
    static std::array<int, 4> Parse(NBitStream::TBitStream* stream) {
        //ATRAC1 - 3 subbands, ATRAC3 - 4 subbands.
        //TODO: rewrite
        std::array<int, 4> tmp;
        tmp[0] = 2 - stream->Read(2);
        tmp[1] = 2 - stream->Read(2);
        tmp[2] = 3 - stream->Read(2);
        stream->Read(2); //skip unused 2 bits
        return tmp;
    }
    static std::array<int, 4> Create(bool lowShort, bool midShort, bool hiShort) {
        std::array<int, 4> tmp;
        tmp[0] = lowShort ? 2 : 0;
        tmp[1] = midShort ? 2 : 0;
        tmp[2] = hiShort ? 3 : 0;
        return tmp;
    }
public:
    TBlockSize(NBitStream::TBitStream* stream)
        : LogCount(Parse(stream))
    {}
    TBlockSize(bool lowShort, bool midShort, bool hiShort)
        : LogCount(Create(lowShort, midShort, hiShort))
    {}
    TBlockSize()
        : LogCount({{0, 0, 0, 0}})
    {}
    const std::array<int, 4> LogCount;
};

} //namespace NAtracDEnc