-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAudioDecoderWav.cpp
248 lines (229 loc) · 6.7 KB
/
AudioDecoderWav.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
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
243
244
245
246
247
248
#include "AudioCommon.h"
#include "AudioDecoderWav.h"
#include "NqrRiffUtils.h"
#include <algorithm>
using namespace nqr;
using namespace audio;
const uint32_t CKC_RIFF = GenerateChunkCode('R', 'I', 'F', 'F');
const uint32_t CKC_RIFX = GenerateChunkCode('R', 'I', 'F', 'X');
const uint32_t CKC_FFIR = GenerateChunkCode('F', 'F', 'I', 'R');
const uint32_t CKC_WAVE = GenerateChunkCode('W', 'A', 'V', 'E');
const uint32_t CKC_fmt_ = GenerateChunkCode('f', 'm', 't', ' ');
const uint32_t CKC_fact = GenerateChunkCode('f', 'a', 'c', 't');
const uint32_t CKC_bext = GenerateChunkCode('b', 'e', 'x', 't');
const uint32_t CKC_data = GenerateChunkCode('d', 'a', 't', 'a');
DecoderWav::DecoderWav(size_t bufferSize)
: Decoder(bufferSize, DecoderType::WAV)
{
}
DecoderWav::~DecoderWav()
{
if (stream)
{
stream->seek(Stream::SeekOrigin::BEGINNING, 0);
stream->release();
stream = nullptr;
}
}
bool DecoderWav::init(Stream* src)
{
stream = src;
if (!stream)
return false;
std::string msg;
uint8_t* tempBuffer = nullptr;
bool ret = false;
stream->lock();
stream->seek(Stream::SeekOrigin::BEGINNING, 0);
auto size = stream->size();
do
{
// Read RIFF Header
RiffChunkHeader riffHeader = {};
stream->read((uint8_t*)&riffHeader, 12);
// Check RIFF
if (riffHeader.id_riff != CKC_RIFF) {
if (riffHeader.id_riff == CKC_RIFX || riffHeader.id_riff == CKC_FFIR) {
msg = "doesn't support big endian file";
break;
}
msg = "bad RIFF/RIFX/FFIR file header";
break;
}
if (riffHeader.id_wave != CKC_WAVE) {
msg = "bad WAVE header";
break;
}
auto expectedSize = size - riffHeader.file_size;
if (expectedSize != sizeof(uint32_t) * 2) {
msg = "declared size of file dosn't match real size";
break;
}
// note: assume that header is smaller than 256B
tempBuffer = (uint8_t*)malloc(256);
if (!tempBuffer) {
msg = "failed to allocate temperary buffer";
break;
}
uint64_t bufferLength;
stream->seek(Stream::SeekOrigin::BEGINNING, 0);
stream->read(tempBuffer, 256, &bufferLength);
// Read WAVE Header
auto WaveChunkInfo = ScanForChunk(tempBuffer, bufferLength, CKC_fmt_);
if (WaveChunkInfo.offset == 0) {
msg = "couldn't find fmt chunk";
break;
}
CC_ASSERT(WaveChunkInfo.size == 16 || WaveChunkInfo.size == 18 ||
WaveChunkInfo.size == 20 || WaveChunkInfo.size == 40);
WaveChunkHeader wavHeader = {};
memcpy(&wavHeader, tempBuffer + WaveChunkInfo.offset, sizeof(WaveChunkHeader));
if (wavHeader.chunk_size < 16) {
msg = "format chunk too small";
break;
}
//TODO: validate wav header (sane sample rate, bit depth, etc)
audioInfo.channelCount = wavHeader.channel_count;
audioInfo.sampleRate = wavHeader.sample_rate;
audioInfo.bytesPerFrame = wavHeader.frame_size;
auto bit_depth = wavHeader.bit_depth;
CC_ASSERT(audioInfo.channelCount*bit_depth / 8 == wavHeader.frame_size);
if (bit_depth != 16) {
// todo: not implemented
msg = "only support 16bit format, got " + std::to_string(bit_depth) + "bit";
break;
}
bool scanForFact = false;
bool grabExtensibleData = false;
//bool adpcmEncoded = false;
if (wavHeader.format == FORMAT_PCM)
{
}
else if (wavHeader.format == FORMAT_IEEE)
{
scanForFact = true;
}
else if (wavHeader.format == FORMAT_IMA_ADPCM)
{
//adpcmEncoded = true;
//scanForFact = true;
// todo: not implemented
msg = "not support ADPCM format";
break;
}
else if (wavHeader.format == FORMAT_EXT)
{
// Used when (1) PCM data has more than 16 bits; (2) channels > 2;
// (3) bits/sample !== container size; (4) channel/speaker mapping specified
//std::cout << "[format id] extended" << std::endl;
scanForFact = true;
grabExtensibleData = true;
}
else if (wavHeader.format == FORMAT_UNKNOWN) {
msg = "unknown wave format";
break;
}
// Read Additional Chunks
FactChunk factChunk{};
if (scanForFact)
{
auto FactChunkInfo = ScanForChunk(tempBuffer, bufferLength, CKC_fact);
if (FactChunkInfo.size)
memcpy(&factChunk, tempBuffer + FactChunkInfo.offset, sizeof(FactChunk));
}
if (grabExtensibleData)
{
ExtensibleData extData = {};
memcpy(&extData, tempBuffer + WaveChunkInfo.offset + sizeof(WaveChunkHeader), sizeof(ExtensibleData));
// extData can be compared against the multi-channel masks defined in the header
// eg. extData.channel_mask == SPEAKER_5POINT1
}
// Read Bext Chunk
auto BextChunkInfo = ScanForChunk(tempBuffer, bufferLength, CKC_bext);
BextChunk bextChunk = {};
if (BextChunkInfo.size)
memcpy(&bextChunk, tempBuffer + BextChunkInfo.offset, sizeof(BextChunk));
// Read DATA Chunk
auto DataChunkInfo = ScanForChunk(tempBuffer, bufferLength, CKC_data);
if (DataChunkInfo.offset == 0) {
msg = "couldn't find data chunk |size=" + std::to_string(stream->size()) +
"|temp buf size=" + std::to_string(bufferLength);
break;
}
uint32_t data_size;
memcpy(&data_size, tempBuffer + DataChunkInfo.offset + sizeof(uint32_t), sizeof(uint32_t));
data_offset = DataChunkInfo.offset + 2 * sizeof(uint32_t);// skip the header and size fields
// note: there may be some info at file tail, only use data_size
if (data_size != stream->size() - data_offset)
{
msg = "data size mismatch: expect " + std::to_string(data_size) +
", got " + std::to_string(stream->size() - data_offset);
data_size = std::min(data_size, uint32_t(stream->size() - data_offset));
}
//if (adpcmEncoded){}
audioInfo.totalFrames = data_size / wavHeader.frame_size;
stream->seek(Stream::SeekOrigin::BEGINNING, data_offset);
stream->retain();
ret = true;
}
while (false);
stream->unlock();
if (tempBuffer)
free(tempBuffer);
if (!msg.empty())
{
AINFO("%s", msg.c_str());
}
return ret;
}
DecoderWav* DecoderWav::create(Stream* src, size_t bufSize)
{
auto ret = new (std::nothrow) DecoderWav(bufSize);
if (ret&&ret->init(src))
{
ret->autorelease();
return ret;
}
delete ret;
return nullptr;
}
int64_t DecoderWav::decode(char* dst, uint64_t bytesToRead)
{
stream->lock();
int64_t tSizeRead = 0;
auto tBuffer = (uint8_t*)dst;
while (tSizeRead < bytesToRead)
{
uint64_t read;
stream->read(tBuffer, bytesToRead - tSizeRead, &read);
if (read == 0) // got end
{
eof = true;
break;
}
tSizeRead += read;
tBuffer += read;
}
stream->unlock();
return tSizeRead;
}
bool DecoderWav::seek(int64_t frameOffset)
{
if (frameOffset >= getTotalFrames())
return false;
stream->lock();
const bool ret = stream->seek(Stream::SeekOrigin::BEGINNING, data_offset + frameOffset * getBytesPerFrame());
stream->unlock();
if (ret)
eof = false;
return ret;
}
int64_t DecoderWav::tell()
{
const int64_t p = (int64_t)stream->tell() - (int64_t)data_offset;
return std::max<int64_t>(0, p) / getBytesPerFrame();
}
bool DecoderWav::isSeekable()
{
return true;
}