Skip to content

Commit 8a5a30b

Browse files
authored
Add files via upload
1 parent a533eb8 commit 8a5a30b

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

src/Audio.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1853,7 +1853,7 @@ int Audio::read_M4A_Header(uint8_t *data, size_t len) {
18531853
if(m_controlCounter == M4A_FTYP) { /* check_m4a_file */
18541854
atomsize = bigEndian(data, 4); // length of first atom
18551855
if(specialIndexOf(data, "ftyp", 10) != 4) {
1856-
log_e("atom 'type' not found in header");
1856+
log_e("atom 'ftyp' not found in header");
18571857
stopSong();
18581858
return -1;
18591859
}

src/flac_decoder/flac_decoder.cpp

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* adapted to ESP32
55
*
66
* Created on: Jul 03,2020
7-
* Updated on: Jan 08,2023
7+
* Updated on: Feb 10,2023
88
*
99
* Author: Wolle
1010
*
@@ -101,6 +101,22 @@ uint32_t readUint(uint8_t nBits){
101101
return result;
102102
}
103103

104+
uint32_t readUint(uint8_t nBits, int *bytesLeft){
105+
while (m_bitBufferLen < nBits){
106+
uint8_t temp = *(m_inptr + m_rIndex);
107+
m_rIndex++;
108+
(*bytesLeft)--;
109+
if(*bytesLeft < 0) { log_i("error in bitreader"); }
110+
m_bitBuffer = (m_bitBuffer << 8) | temp;
111+
m_bitBufferLen += 8;
112+
}
113+
m_bitBufferLen -= nBits;
114+
uint32_t result = m_bitBuffer >> m_bitBufferLen;
115+
if (nBits < 32)
116+
result &= (1 << nBits) - 1;
117+
return result;
118+
}
119+
104120
int32_t readSignedInt(int nBits){
105121
int32_t temp = readUint(nBits) << (32 - nBits);
106122
temp = temp >> (32 - nBits); // The C++ compiler uses the sign bit to fill vacated bit positions
@@ -225,6 +241,11 @@ int FLACparseOGG(uint8_t *inbuf, int *bytesLeft){ // reference https://www.xiph
225241
}
226242
m_page0_len = s_flacSegmentTable[0];
227243

244+
// for(int i = 0; i<pageSegments; i++){
245+
// log_i("%i %i", i, s_flacSegmentTable[i]);
246+
// }
247+
248+
228249
bool continuedPage = headerType & 0x01; // set: page contains data of a packet continued from the previous page
229250
bool firstPage = headerType & 0x02; // set: this is the first page of a logical bitstream (bos)
230251
bool lastPage = headerType & 0x04; // set: this is the last page of a logical bitstream (eos)
@@ -282,8 +303,7 @@ int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
282303
s_f_flacParseOgg = true;
283304
return FLAC_PARSE_OGG_DONE;
284305
}
285-
286-
return flacDecodeFrame(inbuf, bytesLeft, outbuf);
306+
return flacDecodeFrame (inbuf, bytesLeft);
287307
}
288308

289309
if(m_status == DECODE_SUBFRAMES){
@@ -330,13 +350,13 @@ int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
330350
return ERR_FLAC_NONE;
331351
}
332352
//----------------------------------------------------------------------------------------------------------------------
333-
int8_t flacDecodeFrame(uint8_t *inbuf, int *bytesLeft, short *outbuf){
334-
readUint(14 + 1); // synccode + reserved bit
335-
FLACFrameHeader->blockingStrategy = readUint(1);
336-
FLACFrameHeader->blockSizeCode = readUint(4);
337-
FLACFrameHeader->sampleRateCode = readUint(4);
338-
FLACFrameHeader->chanAsgn = readUint(4);
339-
FLACFrameHeader->sampleSizeCode = readUint(3);
353+
int8_t flacDecodeFrame(uint8_t *inbuf, int *bytesLeft){
354+
readUint(14 + 1, bytesLeft); // synccode + reserved bit
355+
FLACFrameHeader->blockingStrategy = readUint(1, bytesLeft);
356+
FLACFrameHeader->blockSizeCode = readUint(4, bytesLeft);
357+
FLACFrameHeader->sampleRateCode = readUint(4, bytesLeft);
358+
FLACFrameHeader->chanAsgn = readUint(4, bytesLeft);
359+
FLACFrameHeader->sampleSizeCode = readUint(3, bytesLeft);
340360
if(!FLACMetadataBlock->numChannels){
341361
if(FLACFrameHeader->chanAsgn == 0) FLACMetadataBlock->numChannels = 1;
342362
if(FLACFrameHeader->chanAsgn == 1) FLACMetadataBlock->numChannels = 2;
@@ -365,8 +385,8 @@ int8_t flacDecodeFrame(uint8_t *inbuf, int *bytesLeft, short *outbuf){
365385
if(FLACFrameHeader->sampleRateCode == 10) FLACMetadataBlock->sampleRate = 48000;
366386
if(FLACFrameHeader->sampleRateCode == 11) FLACMetadataBlock->sampleRate = 96000;
367387
}
368-
readUint(1);
369-
uint32_t temp = (readUint(8) << 24);
388+
readUint(1, bytesLeft);
389+
uint32_t temp = (readUint(8, bytesLeft) << 24);
370390
temp = ~temp;
371391
uint32_t shift = 0x80000000; // Number of leading zeros
372392
int8_t count = 0;
@@ -375,33 +395,32 @@ int8_t flacDecodeFrame(uint8_t *inbuf, int *bytesLeft, short *outbuf){
375395
else break;
376396
}
377397
count--;
378-
for (int i = 0; i < count; i++) readUint(8);
398+
for (int i = 0; i < count; i++) readUint(8, bytesLeft);
379399
m_blockSize = 0;
380400
if (FLACFrameHeader->blockSizeCode == 1)
381401
m_blockSize = 192;
382402
else if (2 <= FLACFrameHeader->blockSizeCode && FLACFrameHeader->blockSizeCode <= 5)
383403
m_blockSize = 576 << (FLACFrameHeader->blockSizeCode - 2);
384404
else if (FLACFrameHeader->blockSizeCode == 6)
385-
m_blockSize = readUint(8) + 1;
405+
m_blockSize = readUint(8, bytesLeft) + 1;
386406
else if (FLACFrameHeader->blockSizeCode == 7)
387-
m_blockSize = readUint(16) + 1;
407+
m_blockSize = readUint(16, bytesLeft) + 1;
388408
else if (8 <= FLACFrameHeader->blockSizeCode && FLACFrameHeader->blockSizeCode <= 15)
389409
m_blockSize = 256 << (FLACFrameHeader->blockSizeCode - 8);
390410
else{
391411
return ERR_FLAC_RESERVED_BLOCKSIZE_UNSUPPORTED;
392412
}
393413
if(m_blockSize > 8192){
394-
log_e("Error: blockSize too big");
414+
log_e("Error: blockSize too big ,%i bytes", m_blockSize);
395415
return ERR_FLAC_BLOCKSIZE_TOO_BIG;
396416
}
397417
if(FLACFrameHeader->sampleRateCode == 12)
398-
readUint(8);
418+
readUint(8, bytesLeft);
399419
else if (FLACFrameHeader->sampleRateCode == 13 || FLACFrameHeader->sampleRateCode == 14){
400-
readUint(16);
420+
readUint(16, bytesLeft);
401421
}
402-
readUint(8);
422+
readUint(8, bytesLeft);
403423
m_status = DECODE_SUBFRAMES;
404-
*bytesLeft = m_bytesAvail;
405424
m_blockSizeLeft = m_blockSize;
406425
return ERR_FLAC_NONE;
407426
}
@@ -471,12 +490,12 @@ int8_t decodeSubframes(){
471490
}
472491
}
473492
else {
474-
log_e("unknown channel assignment");
493+
log_e("unknown channel assignment, %i", FLACFrameHeader->chanAsgn);
475494
return ERR_FLAC_UNKNOWN_CHANNEL_ASSIGNMENT;
476495
}
477496
}
478497
else{
479-
log_e("Reserved channel assignment");
498+
log_e("Reserved channel assignment, %i", FLACFrameHeader->chanAsgn);
480499
return ERR_FLAC_RESERVED_CHANNEL_ASSIGNMENT;
481500
}
482501
return ERR_FLAC_NONE;

src/flac_decoder/flac_decoder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void FLACDecoder_FreeBuffers();
154154
void FLACSetRawBlockParams(uint8_t Chans, uint32_t SampRate, uint8_t BPS, uint32_t tsis, uint32_t AuDaLength);
155155
void FLACDecoderReset();
156156
int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf);
157-
int8_t flacDecodeFrame(uint8_t *inbuf, int *bytesLeft, short *outbuf);
157+
int8_t flacDecodeFrame(uint8_t *inbuf, int *bytesLeft);
158158
uint16_t FLACGetOutputSamps();
159159
uint64_t FLACGetTotoalSamplesInStream();
160160
uint8_t FLACGetBitsPerSample();
@@ -163,6 +163,7 @@ uint32_t FLACGetSampRate();
163163
uint32_t FLACGetBitRate();
164164
uint32_t FLACGetAudioFileDuration();
165165
uint32_t readUint(uint8_t nBits);
166+
uint32_t readUint(uint8_t nBits, int *bytesLeft);
166167
int32_t readSignedInt(int nBits);
167168
int64_t readRiceSignedInt(uint8_t param);
168169
void alignToByte();

0 commit comments

Comments
 (0)