aboutsummaryrefslogtreecommitdiffstats
path: root/src/atrac/at3p/at3p_tables.cpp
blob: 3bfccda1e504d5089f81b20457fe83cb0194e47c (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
/*
 * 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 "at3p_tables.h"
#include "ff/atrac3plus_data.h"

#include <iostream>

namespace NAtracDEnc {
namespace NAt3p {

static void __attribute__ ((noinline)) GenHuffmanEncTable(const uint8_t *cb, const uint8_t *xlat, uint16_t outLen, TVlcElement* out)
{
    uint16_t index = 0;
    uint16_t code = 0;
    for (int b = 1; b <= 12; b++) {
        for (int i = *cb++; i > 0; i--) {
            uint8_t val = xlat[index];
            if (val > outLen) {
                throw std::runtime_error("encoded value out of range, invalid haffman table");
            }
            TVlcElement* cur = out + val;
            cur->Code = code;
            cur->Len = b;
            index++;
            code++;
        }
        code <<= 1;
    }
}

template<typename T>
void GenHuffmanEncTable(const uint8_t *cb, const uint8_t *xlat, T& out)
{
    GenHuffmanEncTable(cb, xlat, out.size(), out.data());
}

THuffTables::THuffTables()
{
    GenHuffmanEncTable(&atrac3p_tone_cbs[0][0], &atrac3p_tone_xlats[0], NumToneBands);
}

} // namespace NAt3p
} // namespace NAtracDEnc