aboutsummaryrefslogtreecommitdiffstats
path: root/src/atrac/atrac_scale.h
blob: c992c037241b840423c2295582c59256156f1b49 (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
/*
 * 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 FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#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