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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
/*
* 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_bitstream.h"
#include "at3p_gha.h"
#include "at3p_tables.h"
#include <lib/bitstream/bitstream.h>
#include <env.h>
#include <util.h>
#include <iostream>
namespace NAtracDEnc {
using namespace NAt3p;
using std::vector;
using std::make_pair;
using std::pair;
static THuffTables HuffTabs;
TTonePackResult CreateFreqBitPack(const TAt3PGhaData::TWaveParam* const param, int len)
{
const int MaxBits = 10;
int bits[2] = {MaxBits, MaxBits};
std::vector<TTonePackResult::TEntry> res[2];
res[0].reserve(len);
res[1].reserve(len);
// try ascending order
{
auto& t = res[0];
uint16_t prevFreqIndex = param->FreqIndex & 1023;
t.emplace_back(TTonePackResult::TEntry{prevFreqIndex, MaxBits});
for (int i = 1; i < len; i++) {
uint16_t curFreqIndex = param[i].FreqIndex & 1023;
if (prevFreqIndex < 512) {
t.emplace_back(TTonePackResult::TEntry{curFreqIndex, MaxBits});
bits[0] += MaxBits;
} else {
uint16_t b = GetFirstSetBit(1023 - prevFreqIndex) + 1;
uint16_t code = curFreqIndex - (1024 - (1 << b));
t.emplace_back(TTonePackResult::TEntry{code, b});
bits[0] += b;
}
prevFreqIndex = curFreqIndex;
}
}
// try descending order
if (len > 1) {
auto& t = res[1];
uint16_t prevFreqIndex = param[len - 1].FreqIndex & 1023;
t.emplace_back(TTonePackResult::TEntry{prevFreqIndex, MaxBits});
for (int i = len - 2; i >= 0; i--) {
uint16_t curFreqIndex = param[i].FreqIndex & 1023;
uint16_t b = GetFirstSetBit(prevFreqIndex) + 1;
t.emplace_back(TTonePackResult::TEntry{curFreqIndex, b});
bits[1] += b;
prevFreqIndex = curFreqIndex;
}
}
if (len == 1 || bits[0] < bits[1]) {
return {res[0], bits[0], ETonePackOrder::ASC};
} else {
return {res[1], bits[1], ETonePackOrder::DESC};
}
}
TAt3PBitStream::TAt3PBitStream(ICompressedOutput* container, uint16_t frameSz)
: Container(container)
, FrameSz(frameSz)
{
NEnv::SetRoundFloat();
}
static void WriteTonalBlock(NBitStream::TBitStream& bs, int channels, const TAt3PGhaData* tonalBlock)
{
//GHA amplidude mode 1
bs.Write(1, 1);
//Num tone bands
const TVlcElement& tbHuff = HuffTabs.NumToneBands[tonalBlock->NumToneBands - 1];
bs.Write(tbHuff.Code, tbHuff.Len);
if (channels == 2) {
bs.Write(0, 1);
bs.Write(0, 1);
bs.Write(0, 1);
}
for (int ch = 0; ch < channels; ch++) {
if (ch) {
// each channel has own envelope
bs.Write(0, 1);
}
// Envelope data
for (int i = 0; i < tonalBlock->NumToneBands; i++) {
if (ch && !tonalBlock->SecondChBands[i]) {
continue;
}
//TODO: Add actual Envelope
// start point present
bs.Write(0, 1);
// stop point present
bs.Write(0, 1);
}
// Num waves
int mode = 0; //TODO: Calc mode
bs.Write(mode, ch + 1);
for (int i = 0; i < tonalBlock->NumToneBands; i++) {
if (ch && !tonalBlock->SecondChBands[i]) {
continue;
}
bs.Write(tonalBlock->GetNumWaves(ch, i), 4);
}
// Tones freq
if (ch) {
// 0 - independed
// 1 - delta to leader
bs.Write(0, 1);
}
for (int i = 0; i < tonalBlock->NumToneBands; i++) {
if (ch && !tonalBlock->SecondChBands[i]) {
continue;
}
auto numWaves = tonalBlock->GetNumWaves(ch, i);
if (numWaves == 0) {
continue;
}
const auto w = tonalBlock->GetWaves(ch, i);
const auto pkt = CreateFreqBitPack(w.first, w.second);
if (numWaves > 1) {
bs.Write(static_cast<bool>(pkt.Order), 1);
}
for (const auto& d : pkt.Data) {
bs.Write(d.Code, d.Bits);
}
}
// Amplitude
mode = 0; //TODO: Calc mode
bs.Write(mode, ch + 1);
for (int i = 0; i < tonalBlock->NumToneBands; i++) {
if (ch && !tonalBlock->SecondChBands[i]) {
continue;
}
auto numWaves = tonalBlock->GetNumWaves(ch, i);
if (numWaves == 0) {
continue;
}
for (size_t j = 0; j < numWaves; j++)
bs.Write(0, 6);
}
// Phase
for (int i = 0; i < tonalBlock->NumToneBands; i++) {
if (ch && !tonalBlock->SecondChBands[i]) {
continue;
}
auto numWaves = tonalBlock->GetNumWaves(ch, i);
if (numWaves == 0) {
continue;
}
const auto w = tonalBlock->GetWaves(ch, i);
for (size_t j = 0; j < w.second; j++) {
bs.Write(w.first[j].PhaseIndex, 5);
}
}
}
}
void TAt3PBitStream::WriteFrame(int channels, const TAt3PGhaData* tonalBlock)
{
NBitStream::TBitStream bitStream;
// First bit must be zero
bitStream.Write(0, 1);
// Channel block type
// 0 - MONO block
// 1 - STEREO block
// 2 - Nobody know
bitStream.Write(channels - 1, 2);
// Skip some bits to produce correct zero bitstream
bitStream.Write(0, 10);
if (channels == 2) {
bitStream.Write(0, 12);
} else {
bitStream.Write(0, 3);
}
// Bit indicate tonal block is used
bitStream.Write((bool)tonalBlock, 1);
if (tonalBlock) {
WriteTonalBlock(bitStream, channels, tonalBlock);
}
bitStream.Write(0, 1); // no noise info
// Terminator
bitStream.Write(3, 2);
std::vector<char> buf = bitStream.GetBytes();
buf.resize(FrameSz);
Container->WriteFrame(buf);
}
}
|