@@ -26,8 +26,6 @@ uint16_t m_blockSizeLeft = 0;
2626uint16_t m_validSamples = 0 ;
2727uint8_t m_status = 0 ;
2828uint8_t *m_inptr;
29- int16_t m_bytesAvail;
30- int16_t m_bytesDecoded = 0 ;
3129uint16_t *s_flacSegmentTable = NULL ;
3230float m_compressionRatio = 0 ;
3331uint16_t m_rIndex = 0 ;
@@ -85,22 +83,6 @@ void FLACDecoder_FreeBuffers(){
8583// ----------------------------------------------------------------------------------------------------------------------
8684// B I T R E A D E R
8785// ----------------------------------------------------------------------------------------------------------------------
88- uint32_t readUint (uint8_t nBits){
89- while (m_bitBufferLen < nBits){
90- uint8_t temp = *(m_inptr + m_rIndex);
91- m_rIndex++;
92- m_bytesAvail--;
93- if (m_bytesAvail < 0 ) { log_i (" error in bitreader" ); }
94- m_bitBuffer = (m_bitBuffer << 8 ) | temp;
95- m_bitBufferLen += 8 ;
96- }
97- m_bitBufferLen -= nBits;
98- uint32_t result = m_bitBuffer >> m_bitBufferLen;
99- if (nBits < 32 )
100- result &= (1 << nBits) - 1 ;
101- return result;
102- }
103-
10486uint32_t readUint (uint8_t nBits, int *bytesLeft){
10587 while (m_bitBufferLen < nBits){
10688 uint8_t temp = *(m_inptr + m_rIndex);
@@ -117,17 +99,17 @@ uint32_t readUint(uint8_t nBits, int *bytesLeft){
11799 return result;
118100}
119101
120- int32_t readSignedInt (int nBits){
121- int32_t temp = readUint (nBits) << (32 - nBits);
102+ int32_t readSignedInt (int nBits, int * bytesLeft ){
103+ int32_t temp = readUint (nBits, bytesLeft ) << (32 - nBits);
122104 temp = temp >> (32 - nBits); // The C++ compiler uses the sign bit to fill vacated bit positions
123105 return temp;
124106}
125107
126- int64_t readRiceSignedInt (uint8_t param){
108+ int64_t readRiceSignedInt (uint8_t param, int * bytesLeft ){
127109 long val = 0 ;
128- while (readUint (1 ) == 0 )
110+ while (readUint (1 , bytesLeft ) == 0 )
129111 val++;
130- val = (val << param) | readUint (param);
112+ val = (val << param) | readUint (param, bytesLeft );
131113 return (val >> 1 ) ^ -(val & 1 );
132114}
133115
@@ -293,7 +275,6 @@ int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
293275
294276 if (m_status != OUT_SAMPLES){
295277 m_rIndex = 0 ;
296- m_bytesAvail = (*bytesLeft);
297278 m_inptr = inbuf;
298279 }
299280
@@ -309,7 +290,7 @@ int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
309290 if (m_status == DECODE_SUBFRAMES){
310291
311292 // Decode each channel's subframe, then skip footer
312- int ret = decodeSubframes ();
293+ int ret = decodeSubframes (bytesLeft );
313294 if (ret != 0 ) return ret;
314295 m_status = OUT_SAMPLES;
315296 }
@@ -340,12 +321,10 @@ int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
340321 }
341322
342323 alignToByte ();
343- readUint (16 );
344- m_bytesDecoded = *bytesLeft - m_bytesAvail;
324+ readUint (16 , bytesLeft);
345325// log_i("m_bytesDecoded %i", m_bytesDecoded);
346326// m_compressionRatio = (float)m_bytesDecoded / (float)m_blockSize * FLACMetadataBlock->numChannels * (16/8);
347327// log_i("m_compressionRatio % f", m_compressionRatio);
348- *bytesLeft = m_bytesAvail;
349328 m_status = DECODE_FRAME;
350329 return ERR_FLAC_NONE;
351330}
@@ -463,14 +442,14 @@ uint32_t FLACGetAudioFileDuration() {
463442 return 0 ;
464443}
465444// ----------------------------------------------------------------------------------------------------------------------
466- int8_t decodeSubframes (){
445+ int8_t decodeSubframes (int * bytesLeft ){
467446 if (FLACFrameHeader->chanAsgn <= 7 ) {
468447 for (int ch = 0 ; ch < FLACMetadataBlock->numChannels ; ch++)
469- decodeSubframe (FLACMetadataBlock->bitsPerSample , ch);
448+ decodeSubframe (FLACMetadataBlock->bitsPerSample , ch, bytesLeft );
470449 }
471450 else if (8 <= FLACFrameHeader->chanAsgn && FLACFrameHeader->chanAsgn <= 10 ) {
472- decodeSubframe (FLACMetadataBlock->bitsPerSample + (FLACFrameHeader->chanAsgn == 9 ? 1 : 0 ), 0 );
473- decodeSubframe (FLACMetadataBlock->bitsPerSample + (FLACFrameHeader->chanAsgn == 9 ? 0 : 1 ), 1 );
451+ decodeSubframe (FLACMetadataBlock->bitsPerSample + (FLACFrameHeader->chanAsgn == 9 ? 1 : 0 ), 0 , bytesLeft );
452+ decodeSubframe (FLACMetadataBlock->bitsPerSample + (FLACFrameHeader->chanAsgn == 9 ? 0 : 1 ), 1 , bytesLeft );
474453 if (FLACFrameHeader->chanAsgn == 8 ) {
475454 for (int i = 0 ; i < m_blockSize; i++)
476455 FLACsubFramesBuff->samplesBuffer [1 ][i] = (
@@ -501,33 +480,33 @@ int8_t decodeSubframes(){
501480 return ERR_FLAC_NONE;
502481}
503482// ----------------------------------------------------------------------------------------------------------------------
504- int8_t decodeSubframe (uint8_t sampleDepth, uint8_t ch) {
483+ int8_t decodeSubframe (uint8_t sampleDepth, uint8_t ch, int * bytesLeft ) {
505484 int8_t ret = 0 ;
506- readUint (1 );
507- uint8_t type = readUint (6 );
508- int shift = readUint (1 );
485+ readUint (1 , bytesLeft );
486+ uint8_t type = readUint (6 , bytesLeft );
487+ int shift = readUint (1 , bytesLeft );
509488 if (shift == 1 ) {
510- while (readUint (1 ) == 0 )
489+ while (readUint (1 , bytesLeft ) == 0 )
511490 shift++;
512491 }
513492 sampleDepth -= shift;
514493
515494 if (type == 0 ){ // Constant coding
516- int16_t s= readSignedInt (sampleDepth);
495+ int16_t s= readSignedInt (sampleDepth, bytesLeft );
517496 for (int i=0 ; i < m_blockSize; i++){
518497 FLACsubFramesBuff->samplesBuffer [ch][i] = s;
519498 }
520499 }
521500 else if (type == 1 ) { // Verbatim coding
522501 for (int i = 0 ; i < m_blockSize; i++)
523- FLACsubFramesBuff->samplesBuffer [ch][i] = readSignedInt (sampleDepth);
502+ FLACsubFramesBuff->samplesBuffer [ch][i] = readSignedInt (sampleDepth, bytesLeft );
524503 }
525504 else if (8 <= type && type <= 12 ){
526- ret = decodeFixedPredictionSubframe (type - 8 , sampleDepth, ch);
505+ ret = decodeFixedPredictionSubframe (type - 8 , sampleDepth, ch, bytesLeft );
527506 if (ret) return ret;
528507 }
529508 else if (32 <= type && type <= 63 ){
530- ret = decodeLinearPredictiveCodingSubframe (type - 31 , sampleDepth, ch);
509+ ret = decodeLinearPredictiveCodingSubframe (type - 31 , sampleDepth, ch, bytesLeft );
531510 if (ret) return ret;
532511 }
533512 else {
@@ -541,11 +520,11 @@ int8_t decodeSubframe(uint8_t sampleDepth, uint8_t ch) {
541520 return ERR_FLAC_NONE;
542521}
543522// ----------------------------------------------------------------------------------------------------------------------
544- int8_t decodeFixedPredictionSubframe (uint8_t predOrder, uint8_t sampleDepth, uint8_t ch) {
523+ int8_t decodeFixedPredictionSubframe (uint8_t predOrder, uint8_t sampleDepth, uint8_t ch, int * bytesLeft ) {
545524 uint8_t ret = 0 ;
546525 for (uint8_t i = 0 ; i < predOrder; i++)
547- FLACsubFramesBuff->samplesBuffer [ch][i] = readSignedInt (sampleDepth);
548- ret = decodeResiduals (predOrder, ch);
526+ FLACsubFramesBuff->samplesBuffer [ch][i] = readSignedInt (sampleDepth, bytesLeft );
527+ ret = decodeResiduals (predOrder, ch, bytesLeft );
549528 if (ret) return ret;
550529 coefs.clear ();
551530 if (predOrder == 0 ) coefs.resize (0 );
@@ -558,29 +537,29 @@ int8_t decodeFixedPredictionSubframe(uint8_t predOrder, uint8_t sampleDepth, uin
558537 return ERR_FLAC_NONE;
559538}
560539// ----------------------------------------------------------------------------------------------------------------------
561- int8_t decodeLinearPredictiveCodingSubframe (int lpcOrder, int sampleDepth, uint8_t ch){
540+ int8_t decodeLinearPredictiveCodingSubframe (int lpcOrder, int sampleDepth, uint8_t ch, int * bytesLeft ){
562541 int8_t ret = 0 ;
563542 for (int i = 0 ; i < lpcOrder; i++)
564- FLACsubFramesBuff->samplesBuffer [ch][i] = readSignedInt (sampleDepth);
565- int precision = readUint (4 ) + 1 ;
566- int shift = readSignedInt (5 );
543+ FLACsubFramesBuff->samplesBuffer [ch][i] = readSignedInt (sampleDepth, bytesLeft );
544+ int precision = readUint (4 , bytesLeft ) + 1 ;
545+ int shift = readSignedInt (5 , bytesLeft );
567546 coefs.resize (0 );
568547 for (uint8_t i = 0 ; i < lpcOrder; i++)
569- coefs.push_back (readSignedInt (precision));
570- ret = decodeResiduals (lpcOrder, ch);
548+ coefs.push_back (readSignedInt (precision, bytesLeft ));
549+ ret = decodeResiduals (lpcOrder, ch, bytesLeft );
571550 if (ret) return ret;
572551 restoreLinearPrediction (ch, shift);
573552 return ERR_FLAC_NONE;
574553}
575554// ----------------------------------------------------------------------------------------------------------------------
576- int8_t decodeResiduals (uint8_t warmup, uint8_t ch) {
555+ int8_t decodeResiduals (uint8_t warmup, uint8_t ch, int * bytesLeft ) {
577556
578- int method = readUint (2 );
557+ int method = readUint (2 , bytesLeft );
579558 if (method >= 2 )
580559 return ERR_FLAC_RESERVED_RESIDUAL_CODING; // Reserved residual coding method
581560 uint8_t paramBits = method == 0 ? 4 : 5 ;
582561 int escapeParam = (method == 0 ? 0xF : 0x1F );
583- int partitionOrder = readUint (4 );
562+ int partitionOrder = readUint (4 , bytesLeft );
584563
585564 int numPartitions = 1 << partitionOrder;
586565 if (m_blockSize % numPartitions != 0 )
@@ -591,15 +570,15 @@ int8_t decodeResiduals(uint8_t warmup, uint8_t ch) {
591570 int start = i * partitionSize + (i == 0 ? warmup : 0 );
592571 int end = (i + 1 ) * partitionSize;
593572
594- int param = readUint (paramBits);
573+ int param = readUint (paramBits, bytesLeft );
595574 if (param < escapeParam) {
596575 for (int j = start; j < end; j++){
597- FLACsubFramesBuff->samplesBuffer [ch][j] = readRiceSignedInt (param);
576+ FLACsubFramesBuff->samplesBuffer [ch][j] = readRiceSignedInt (param, bytesLeft );
598577 }
599578 } else {
600- int numBits = readUint (5 );
579+ int numBits = readUint (5 , bytesLeft );
601580 for (int j = start; j < end; j++){
602- FLACsubFramesBuff->samplesBuffer [ch][j] = readSignedInt (numBits);
581+ FLACsubFramesBuff->samplesBuffer [ch][j] = readSignedInt (numBits, bytesLeft );
603582 }
604583 }
605584 }
0 commit comments