@@ -19,43 +19,47 @@ FLACFrameHeader_t *FLACFrameHeader;
1919FLACMetadataBlock_t *FLACMetadataBlock;
2020FLACsubFramesBuff_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// ----------------------------------------------------------------------------------------------------------------------
4445bool 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// ----------------------------------------------------------------------------------------------------------------------
7478void 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- // ----------------------------------------------------------------------------------------------------------------------
183173char * 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(){
191181int 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// ----------------------------------------------------------------------------------------------------------------------
0 commit comments