Skip to content

Commit 70a1311

Browse files
authored
Add files via upload
1 parent 5f44585 commit 70a1311

File tree

4 files changed

+80
-63
lines changed

4 files changed

+80
-63
lines changed

src/Audio.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2898,7 +2898,7 @@ void Audio::processWebStream() {
28982898
AUDIO_INFO("stream ready");
28992899
}
29002900
if(!f_stream) return;
2901-
if(m_codec == CODEC_OGG){ log_i("determine correct codec here");
2901+
if(m_codec == CODEC_OGG){ // log_i("determine correct codec here");
29022902
uint8_t codec = determineOggCodec(InBuff.getReadPtr(), maxFrameSize);
29032903
if(codec == CODEC_FLAC) {m_codec = CODEC_FLAC; initializeDecoder(); return;}
29042904
if(codec == CODEC_OPUS) {m_codec = CODEC_OPUS; initializeDecoder(); return;}

src/flac_decoder/flac_decoder.cpp

Lines changed: 75 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -19,43 +19,47 @@ FLACFrameHeader_t *FLACFrameHeader;
1919
FLACMetadataBlock_t *FLACMetadataBlock;
2020
FLACsubFramesBuff_t *FLACsubFramesBuff;
2121

22-
vector<int32_t>coefs;
23-
const uint16_t outBuffSize = 2048;
24-
uint16_t m_blockSize=0;
25-
uint16_t m_blockSizeLeft = 0;
26-
uint16_t m_validSamples = 0;
27-
uint8_t m_status = 0;
28-
uint8_t* m_inptr;
29-
int16_t m_bytesAvail;
30-
int16_t m_bytesDecoded = 0;
31-
float m_compressionRatio = 0;
32-
uint16_t m_rIndex=0;
33-
uint64_t m_bitBuffer = 0;
34-
uint8_t m_bitBufferLen = 0;
35-
bool s_f_flacParseOgg = false;
36-
uint8_t m_flacPageSegments = 0;
37-
uint8_t m_page0_len = 0;
38-
char *m_streamTitle= NULL;
39-
boolean m_newSt = false;
22+
vector<int32_t> coefs;
23+
const uint16_t outBuffSize = 2048;
24+
uint16_t m_blockSize = 0;
25+
uint16_t m_blockSizeLeft = 0;
26+
uint16_t m_validSamples = 0;
27+
uint8_t m_status = 0;
28+
uint8_t *m_inptr;
29+
int16_t m_bytesAvail;
30+
int16_t m_bytesDecoded = 0;
31+
uint16_t *s_flacSegmentTable = NULL;
32+
float m_compressionRatio = 0;
33+
uint16_t m_rIndex = 0;
34+
uint64_t m_bitBuffer = 0;
35+
uint8_t m_bitBufferLen = 0;
36+
bool s_f_flacParseOgg = false;
37+
uint8_t m_flacPageSegments = 0;
38+
uint8_t m_page0_len = 0;
39+
char *m_streamTitle = NULL;
40+
boolean s_f_newSt = false;
4041

4142
//----------------------------------------------------------------------------------------------------------------------
4243
// FLAC INI SECTION
4344
//----------------------------------------------------------------------------------------------------------------------
4445
bool FLACDecoder_AllocateBuffers(void){
4546
if(psramFound()) {
4647
// PSRAM found, Buffer will be allocated in PSRAM
47-
if(!FLACFrameHeader) {FLACFrameHeader = (FLACFrameHeader_t*) ps_malloc(sizeof(FLACFrameHeader_t));}
48-
if(!FLACMetadataBlock) {FLACMetadataBlock = (FLACMetadataBlock_t*) ps_malloc(sizeof(FLACMetadataBlock_t));}
49-
if(!FLACsubFramesBuff) {FLACsubFramesBuff = (FLACsubFramesBuff_t*) ps_malloc(sizeof(FLACsubFramesBuff_t));}
50-
if(!m_streamTitle) {m_streamTitle = (char*) ps_malloc(256);}
48+
if(!FLACFrameHeader) {FLACFrameHeader = (FLACFrameHeader_t*) ps_malloc(sizeof(FLACFrameHeader_t));}
49+
if(!FLACMetadataBlock) {FLACMetadataBlock = (FLACMetadataBlock_t*) ps_malloc(sizeof(FLACMetadataBlock_t));}
50+
if(!FLACsubFramesBuff) {FLACsubFramesBuff = (FLACsubFramesBuff_t*) ps_malloc(sizeof(FLACsubFramesBuff_t));}
51+
if(!m_streamTitle) {m_streamTitle = (char*) ps_malloc(256);}
52+
if(!s_flacSegmentTable) {s_flacSegmentTable = (uint16_t*) ps_malloc(256 * sizeof(uint16_t));}
5153
}
5254
else {
53-
if(!FLACFrameHeader) {FLACFrameHeader = (FLACFrameHeader_t*) malloc(sizeof(FLACFrameHeader_t));}
54-
if(!FLACMetadataBlock) {FLACMetadataBlock = (FLACMetadataBlock_t*) malloc(sizeof(FLACMetadataBlock_t));}
55-
if(!FLACsubFramesBuff) {FLACsubFramesBuff = (FLACsubFramesBuff_t*) malloc(sizeof(FLACsubFramesBuff_t));}
56-
if(!m_streamTitle) {m_streamTitle = (char*) malloc(256);}
55+
if(!FLACFrameHeader) {FLACFrameHeader = (FLACFrameHeader_t*) malloc(sizeof(FLACFrameHeader_t));}
56+
if(!FLACMetadataBlock) {FLACMetadataBlock = (FLACMetadataBlock_t*) malloc(sizeof(FLACMetadataBlock_t));}
57+
if(!FLACsubFramesBuff) {FLACsubFramesBuff = (FLACsubFramesBuff_t*) malloc(sizeof(FLACsubFramesBuff_t));}
58+
if(!m_streamTitle) {m_streamTitle = (char*) malloc(256);}
59+
if(!s_flacSegmentTable) {s_flacSegmentTable = (uint16_t*) malloc(256 * sizeof(uint16_t));}
60+
5761
}
58-
if(!FLACFrameHeader || !FLACMetadataBlock || !FLACsubFramesBuff || !m_streamTitle){
62+
if(!FLACFrameHeader || !FLACMetadataBlock || !FLACsubFramesBuff || !m_streamTitle || !s_flacSegmentTable){
5963
log_e("not enough memory to allocate flacdecoder buffers");
6064
return false;
6165
}
@@ -72,10 +76,11 @@ void FLACDecoder_ClearBuffer(){
7276
}
7377
//----------------------------------------------------------------------------------------------------------------------
7478
void FLACDecoder_FreeBuffers(){
75-
if(FLACFrameHeader) {free(FLACFrameHeader); FLACFrameHeader = NULL;}
76-
if(FLACMetadataBlock) {free(FLACMetadataBlock); FLACMetadataBlock = NULL;}
77-
if(FLACsubFramesBuff) {free(FLACsubFramesBuff); FLACsubFramesBuff = NULL;}
78-
if(m_streamTitle) {free(m_streamTitle); m_streamTitle = NULL;}
79+
if(FLACFrameHeader) {free(FLACFrameHeader); FLACFrameHeader = NULL;}
80+
if(FLACMetadataBlock) {free(FLACMetadataBlock); FLACMetadataBlock = NULL;}
81+
if(FLACsubFramesBuff) {free(FLACsubFramesBuff); FLACsubFramesBuff = NULL;}
82+
if(m_streamTitle) {free(m_streamTitle); m_streamTitle = NULL;}
83+
if(s_flacSegmentTable) {free(s_flacSegmentTable); s_flacSegmentTable = NULL;}
7984
}
8085
//----------------------------------------------------------------------------------------------------------------------
8186
// B I T R E A D E R
@@ -165,24 +170,9 @@ boolean FLACFindMagicWord(unsigned char* buf, int nBytes){
165170
return false;
166171
}
167172
//----------------------------------------------------------------------------------------------------------------------
168-
boolean FLACFindStreamTitle(unsigned char* buf, int nBytes){
169-
int idx = FLAC_specialIndexOf(buf, "title=", nBytes);
170-
if(idx >0){
171-
idx += 6;
172-
int len = nBytes - idx;
173-
if(len > 255) return false;
174-
m_newSt = true;
175-
memcpy(m_streamTitle, buf + idx, len + 1);
176-
m_streamTitle[len] = '\0';
177-
// log_i("%s", m_streamTitle);
178-
return true;
179-
}
180-
return false;
181-
}
182-
//----------------------------------------------------------------------------------------------------------------------
183173
char* FLACgetStreamTitle(){
184-
if(m_newSt){
185-
m_newSt = false;
174+
if(s_f_newSt){
175+
s_f_newSt = false;
186176
return m_streamTitle;
187177
}
188178
return NULL;
@@ -191,10 +181,8 @@ char* FLACgetStreamTitle(){
191181
int FLACparseOGG(uint8_t *inbuf, int *bytesLeft){ // reference https://www.xiph.org/ogg/doc/rfc3533.txt
192182

193183
s_f_flacParseOgg = false;
194-
int ret = 0;
195184
int idx = FLAC_specialIndexOf(inbuf, "OggS", 6);
196-
197-
// if(idx != 0) return -1; //ERR_OPUS_DECODER_ASYNC;
185+
if(idx != 0) return ERR_FLAC_DECODER_ASYNC;
198186

199187
uint8_t version = *(inbuf + 4); (void) version;
200188
uint8_t headerType = *(inbuf + 5); (void) headerType;
@@ -222,7 +210,6 @@ int FLACparseOGG(uint8_t *inbuf, int *bytesLeft){ // reference https://www.xiph
222210

223211
// read the segment table (contains pageSegments bytes), 1...251: Length of the frame in bytes,
224212
// 255: A second byte is needed. The total length is first_byte + second byte
225-
uint8_t psegmBuff[256];
226213

227214
int16_t segmentTableWrPtr = 0;
228215

@@ -232,16 +219,46 @@ int FLACparseOGG(uint8_t *inbuf, int *bytesLeft){ // reference https://www.xiph
232219
i++;
233220
n+= *(inbuf + 27 + i);
234221
}
235-
psegmBuff[segmentTableWrPtr] = n;
222+
s_flacSegmentTable[segmentTableWrPtr] = n;
236223
segmentTableWrPtr++;
237224
// s_flacSegmentLength += n;
238225
}
239-
m_page0_len = psegmBuff[0];
240-
241-
uint16_t headerSize = pageSegments + 27;
242-
if(pageSegments == 1) headerSize = pageSegments + psegmBuff[0] +27;
226+
m_page0_len = s_flacSegmentTable[0];
227+
228+
bool continuedPage = headerType & 0x01; // set: page contains data of a packet continued from the previous page
229+
bool firstPage = headerType & 0x02; // set: this is the first page of a logical bitstream (bos)
230+
bool lastPage = headerType & 0x04; // set: this is the last page of a logical bitstream (eos)
231+
static uint8_t secondPage = 0; (void)continuedPage; (void)lastPage;
232+
233+
if(firstPage) secondPage = 3;
234+
if(secondPage) secondPage--;
235+
236+
uint16_t headerSize = 0;
237+
uint8_t aLen = 0, tLen = 0;
238+
uint8_t *aPos = NULL, *tPos = NULL;
239+
if(firstPage || secondPage == 1){
240+
// log_i("s_flacSegmentTable[0] %i", s_flacSegmentTable[0]);
241+
headerSize = pageSegments + s_flacSegmentTable[0] +27;
242+
idx = FLAC_specialIndexOf(inbuf + 28, "ARTIST", s_flacSegmentTable[0]);
243+
if(idx > 0){
244+
aPos = inbuf + 28 + idx + 7;
245+
aLen = *(inbuf + 28 +idx -4) - 7;
246+
}
247+
idx = FLAC_specialIndexOf(inbuf + 28, "TITLE", s_flacSegmentTable[0]);
248+
if(idx > 0){
249+
tPos = inbuf + 28 + idx + 6;
250+
tLen = *(inbuf + 28 + idx -4) - 6;
251+
}
252+
int pos = 0;
253+
if(aLen) {memcpy(m_streamTitle, aPos, aLen); m_streamTitle[aLen] = '\0'; pos = aLen;}
254+
if(aLen && tLen) {strcat(m_streamTitle, " - "); pos += 3;}
255+
if(tLen) {memcpy(m_streamTitle + pos, tPos, tLen); m_streamTitle[pos + tLen] = '\0';}
256+
if(tLen || aLen) s_f_newSt = true;
257+
}
258+
else{
259+
headerSize = pageSegments + 27;
260+
}
243261
*bytesLeft -= headerSize;
244-
245262
return ERR_FLAC_NONE; // no error
246263
}
247264
//----------------------------------------------------------------------------------------------------------------------

src/flac_decoder/flac_decoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ enum : int8_t {FLAC_PARSE_OGG_DONE = 100,
4040
ERR_FLAC_RESERVED_RESIDUAL_CODING = -8,
4141
ERR_FLAC_WRONG_RICE_PARTITION_NR = -9,
4242
ERR_FLAC_BITS_PER_SAMPLE_TOO_BIG = -10,
43-
ERR_FLAG_BITS_PER_SAMPLE_UNKNOWN = 11};
43+
ERR_FLAG_BITS_PER_SAMPLE_UNKNOWN = -11,
44+
ERR_FLAC_DECODER_ASYNC = -12};
4445

4546
typedef struct FLACMetadataBlock_t{
4647
// METADATA_BLOCK_STREAMINFO
@@ -145,7 +146,6 @@ typedef struct FLACFrameHeader_t {
145146

146147
int FLACFindSyncWord(unsigned char *buf, int nBytes);
147148
boolean FLACFindMagicWord(unsigned char* buf, int nBytes);
148-
boolean FLACFindStreamTitle(unsigned char* buf, int nBytes);
149149
char* FLACgetStreamTitle();
150150
int FLACparseOGG(uint8_t *inbuf, int *bytesLeft);
151151
bool FLACDecoder_AllocateBuffers(void);

src/opus_decoder/opus_decoder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ int parseOpusTOC(uint8_t TOC_Byte){ // https://www.rfc-editor.org/rfc/rfc6716
165165

166166
if(s_opusOldMode != mode) {
167167
s_opusOldMode = mode;
168-
if(mode == 2) log_i("opus mode is MODE_CELT_ONLY");
168+
// if(mode == 2) log_i("opus mode is MODE_CELT_ONLY");
169169
}
170170

171171
/* Configuration Mode Bandwidth FrameSizes Audio Bandwidth Sample Rate (Effective)
@@ -353,7 +353,7 @@ int OPUSFindSyncWord(unsigned char *buf, int nBytes){
353353
// assume we have a ogg wrapper
354354
int idx = OPUS_specialIndexOf(buf, "OggS", nBytes);
355355
if(idx >= 0){ // Magic Word found
356-
log_i("OggS found at %i", idx);
356+
// log_i("OggS found at %i", idx);
357357
s_f_opusParseOgg = true;
358358
return idx;
359359
}

0 commit comments

Comments
 (0)