-
Notifications
You must be signed in to change notification settings - Fork 3
/
maketree.cpp
155 lines (137 loc) · 3.54 KB
/
maketree.cpp
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
#include <stdint.h>
#include <stdio.h>
#include <stdexcept>
#include <utility>
#include <vector>
namespace
{
// ISO/IEC 13818-7 Huffman Codebook Tables
const uint32_t SCALEFACTOR_TABLE[][2]
{
{0x3ffe8, 18},
...
};
const uint32_t SPECTRUM1_TABLE[][2]
{
{0x7f8, 11},
...
};
const uint32_t SPECTRUM2_TABLE[][2]
{
...
};
const uint32_t SPECTRUM3_TABLE[][2]
{
...
};
const uint32_t SPECTRUM4_TABLE[][2]
{
...
};
const uint32_t SPECTRUM5_TABLE[][2]
{
...
};
const uint32_t SPECTRUM6_TABLE[][2]
{
...
};
const uint32_t SPECTRUM7_TABLE[][2]
{
...
};
const uint32_t SPECTRUM8_TABLE[][2]
{
...
};
const uint32_t SPECTRUM9_TABLE[][2]
{
...
};
const uint32_t SPECTRUM10_TABLE[][2]
{
...
};
const uint32_t SPECTRUM11_TABLE[][2]
{
...
};
const uint32_t (*const CODEBOOK_TABLES[])[2]
{
SCALEFACTOR_TABLE,
SPECTRUM1_TABLE,
SPECTRUM2_TABLE,
SPECTRUM3_TABLE,
SPECTRUM4_TABLE,
SPECTRUM5_TABLE,
SPECTRUM6_TABLE,
SPECTRUM7_TABLE,
SPECTRUM8_TABLE,
SPECTRUM9_TABLE,
SPECTRUM10_TABLE,
SPECTRUM11_TABLE,
};
const int CODEBOOK_TABLE_LENS[]
{
sizeof(SCALEFACTOR_TABLE) / sizeof(SCALEFACTOR_TABLE[0]),
sizeof(SPECTRUM1_TABLE) / sizeof(SPECTRUM1_TABLE[0]),
sizeof(SPECTRUM2_TABLE) / sizeof(SPECTRUM2_TABLE[0]),
sizeof(SPECTRUM3_TABLE) / sizeof(SPECTRUM3_TABLE[0]),
sizeof(SPECTRUM4_TABLE) / sizeof(SPECTRUM4_TABLE[0]),
sizeof(SPECTRUM5_TABLE) / sizeof(SPECTRUM5_TABLE[0]),
sizeof(SPECTRUM6_TABLE) / sizeof(SPECTRUM6_TABLE[0]),
sizeof(SPECTRUM7_TABLE) / sizeof(SPECTRUM7_TABLE[0]),
sizeof(SPECTRUM8_TABLE) / sizeof(SPECTRUM8_TABLE[0]),
sizeof(SPECTRUM9_TABLE) / sizeof(SPECTRUM9_TABLE[0]),
sizeof(SPECTRUM10_TABLE) / sizeof(SPECTRUM10_TABLE[0]),
sizeof(SPECTRUM11_TABLE) / sizeof(SPECTRUM11_TABLE[0]),
};
const int CODEBOOK_NUM = 12;
void AppendCodeword(std::vector<std::pair<int, int>> &tree, int codeIndex, uint32_t codeword, uint32_t len)
{
int current = 0;
while (len > 0) {
// 0: unset, <500: node, else: leaf
int &next = (codeword >> (--len)) & 1 ? tree[current].second : tree[current].first;
if (next == 0) {
if (len == 0) {
next = 500 + codeIndex;
}
else {
current = next = static_cast<int>(tree.size());
tree.emplace_back(0, 0);
}
}
else if (next >= 500) {
throw std::runtime_error("leaf overwritten");
}
else {
current = next;
}
}
}
}
int main()
{
int maxLen = 0;
for (int i = 0; i < CODEBOOK_NUM; ++i) {
std::vector<std::pair<int, int>> tree;
tree.emplace_back(0, 0);
for (int j = 0; j < CODEBOOK_TABLE_LENS[i]; ++j) {
AppendCodeword(tree, j, CODEBOOK_TABLES[i][j][0], CODEBOOK_TABLES[i][j][1]);
if (maxLen < CODEBOOK_TABLES[i][j][1]) {
maxLen = CODEBOOK_TABLES[i][j][1];
}
}
printf("const uint16_t SPECTRUM%d_TREE[][2] =\n{", i);
for (size_t j = 0; j < tree.size(); ++j) {
if (tree[j].first == 0 || tree[j].second == 0) {
throw std::runtime_error("tree has unset node");
}
printf("%s{%3d, %3d},", j % 5 ? " " : "\n ", tree[j].first, tree[j].second);
}
printf("\n};\n\n");
}
printf("const size_t MAX_CODEWORD_LEN = %d;\n", maxLen);
return 0;
}