acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
flac_decoder.cpp (22730B)
1 /* 2 * flac_decoder.cpp 3 * Java source code from https://www.nayuki.io/page/simple-flac-implementation 4 * adapted to ESP32 5 * 6 * Created on: Jul 03,2020 7 * Updated on: Jul 03,2021 8 * 9 * Author: Wolle 10 * 11 * 12 */ 13 #include "flac_decoder.h" 14 #include "vector" 15 using namespace std; 16 17 18 FLACFrameHeader_t *FLACFrameHeader; 19 FLACMetadataBlock_t *FLACMetadataBlock; 20 FLACsubFramesBuff_t *FLACsubFramesBuff; 21 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 m_f_OggS_found = false; 36 37 //---------------------------------------------------------------------------------------------------------------------- 38 // FLAC INI SECTION 39 //---------------------------------------------------------------------------------------------------------------------- 40 bool FLACDecoder_AllocateBuffers(void){ 41 if(psramFound()) { 42 // PSRAM found, Buffer will be allocated in PSRAM 43 if(!FLACFrameHeader) {FLACFrameHeader = (FLACFrameHeader_t*) ps_malloc(sizeof(FLACFrameHeader_t));} 44 if(!FLACMetadataBlock) {FLACMetadataBlock = (FLACMetadataBlock_t*) ps_malloc(sizeof(FLACMetadataBlock_t));} 45 if(!FLACsubFramesBuff) {FLACsubFramesBuff = (FLACsubFramesBuff_t*) ps_malloc(sizeof(FLACsubFramesBuff_t));} 46 } 47 else { 48 if(!FLACFrameHeader) {FLACFrameHeader = (FLACFrameHeader_t*) malloc(sizeof(FLACFrameHeader_t));} 49 if(!FLACMetadataBlock) {FLACMetadataBlock = (FLACMetadataBlock_t*) malloc(sizeof(FLACMetadataBlock_t));} 50 if(!FLACsubFramesBuff) {FLACsubFramesBuff = (FLACsubFramesBuff_t*) malloc(sizeof(FLACsubFramesBuff_t));} 51 } 52 if(!FLACFrameHeader || !FLACMetadataBlock || !FLACsubFramesBuff ){ 53 log_e("not enough memory to allocate flacdecoder buffers"); 54 return false; 55 } 56 FLACDecoder_ClearBuffer(); 57 return true; 58 } 59 //---------------------------------------------------------------------------------------------------------------------- 60 void FLACDecoder_ClearBuffer(){ 61 memset(FLACFrameHeader, 0, sizeof(FLACFrameHeader_t)); 62 memset(FLACMetadataBlock, 0, sizeof(FLACMetadataBlock_t)); 63 memset(FLACsubFramesBuff, 0, sizeof(FLACsubFramesBuff_t)); 64 m_status = DECODE_FRAME; 65 return; 66 } 67 //---------------------------------------------------------------------------------------------------------------------- 68 void FLACDecoder_FreeBuffers(){ 69 if(FLACFrameHeader) {free(FLACFrameHeader); FLACFrameHeader = NULL;} 70 if(FLACMetadataBlock) {free(FLACMetadataBlock); FLACMetadataBlock = NULL;} 71 if(FLACsubFramesBuff) {free(FLACsubFramesBuff); FLACsubFramesBuff = NULL;} 72 } 73 //---------------------------------------------------------------------------------------------------------------------- 74 // B I T R E A D E R 75 //---------------------------------------------------------------------------------------------------------------------- 76 uint32_t readUint(uint8_t nBits){ 77 while (m_bitBufferLen < nBits){ 78 uint8_t temp = *(m_inptr + m_rIndex); 79 m_rIndex++; 80 m_bytesAvail--; 81 if(m_bytesAvail < 0) { log_i("error in bitreader"); } 82 m_bitBuffer = (m_bitBuffer << 8) | temp; 83 m_bitBufferLen += 8; 84 } 85 m_bitBufferLen -= nBits; 86 uint32_t result = m_bitBuffer >> m_bitBufferLen; 87 if (nBits < 32) 88 result &= (1 << nBits) - 1; 89 return result; 90 } 91 92 int32_t readSignedInt(int nBits){ 93 int32_t temp = readUint(nBits) << (32 - nBits); 94 temp = temp >> (32 - nBits); // The C++ compiler uses the sign bit to fill vacated bit positions 95 return temp; 96 } 97 98 int64_t readRiceSignedInt(uint8_t param){ 99 long val = 0; 100 while (readUint(1) == 0) 101 val++; 102 val = (val << param) | readUint(param); 103 return (val >> 1) ^ -(val & 1); 104 } 105 106 void alignToByte() { 107 m_bitBufferLen -= m_bitBufferLen % 8; 108 } 109 //---------------------------------------------------------------------------------------------------------------------- 110 // F L A C - D E C O D E R 111 //---------------------------------------------------------------------------------------------------------------------- 112 void FLACSetRawBlockParams(uint8_t Chans, uint32_t SampRate, uint8_t BPS, uint32_t tsis, uint32_t AuDaLength){ 113 FLACMetadataBlock->numChannels = Chans; 114 FLACMetadataBlock->sampleRate = SampRate; 115 FLACMetadataBlock->bitsPerSample = BPS; 116 FLACMetadataBlock->totalSamples = tsis; // total samples in stream 117 FLACMetadataBlock->audioDataLength = AuDaLength; 118 } 119 //---------------------------------------------------------------------------------------------------------------------- 120 void FLACDecoderReset(){ // set var to default 121 m_status = DECODE_FRAME; 122 m_bitBuffer = 0; 123 m_bitBufferLen = 0; 124 } 125 //---------------------------------------------------------------------------------------------------------------------- 126 int FLACFindSyncWord(unsigned char *buf, int nBytes) { 127 int i; 128 129 /* find byte-aligned syncword - need 13 matching bits */ 130 for (i = 0; i < nBytes - 1; i++) { 131 if ((buf[i + 0] & 0xFF) == 0xFF && (buf[i + 1] & 0xF8) == 0xF8) { 132 FLACDecoderReset(); 133 return i; 134 } 135 } 136 return -1; 137 } 138 //---------------------------------------------------------------------------------------------------------------------- 139 int FLACFindOggSyncWord(unsigned char *buf, int nBytes){ 140 int i; 141 142 /* find byte-aligned syncword - need 13 matching bits */ 143 for (i = 0; i < nBytes - 1; i++) { 144 if ((buf[i + 0] & 0xFF) == 0xFF && (buf[i + 1] & 0xF8) == 0xF8) { 145 FLACDecoderReset(); 146 log_i("FLAC sync found"); 147 return i; 148 } 149 } 150 /* find byte-aligned OGG Magic - OggS */ 151 for (i = 0; i < nBytes - 1; i++) { 152 if ((buf[i + 0] == 'O') && (buf[i + 1] == 'g') && (buf[i + 2] == 'g') && (buf[i + 3] == 'S')) { 153 FLACDecoderReset(); 154 log_i("OggS found"); 155 m_f_OggS_found = true; 156 return i; 157 } 158 } 159 return -1; 160 } 161 //---------------------------------------------------------------------------------------------------------------------- 162 int FLACparseOggHeader(unsigned char *buf){ 163 uint8_t i = 0; 164 uint8_t ssv = *(buf + i); // stream_structure_version 165 (void)ssv; 166 i++; 167 uint8_t htf = *(buf + i); // header_type_flag 168 (void)htf; 169 i++; 170 uint32_t tmp = 0; // absolute granule position 171 for (int j = 0; j < 4; j++) { 172 tmp += *(buf + j + i) << (4 -j - 1) * 8; 173 } 174 i += 4; 175 uint64_t agp = (uint64_t) tmp << 32; 176 for (int j = 0; j < 4; j++) { 177 agp += *(buf + j + i) << (4 -j - 1) * 8; 178 } 179 i += 4; 180 uint32_t ssnr = 0; // stream serial number 181 for (int j = 0; j < 4; j++) { 182 ssnr += *(buf + j + i) << (4 -j - 1) * 8; 183 } 184 i += 4; 185 uint32_t psnr = 0; // page sequence no 186 for (int j = 0; j < 4; j++) { 187 psnr += *(buf + j + i) << (4 -j - 1) * 8; 188 } 189 i += 4; 190 uint32_t pchk = 0; // page checksum 191 for (int j = 0; j < 4; j++) { 192 pchk += *(buf + j + i) << (4 -j - 1) * 8; 193 } 194 i += 4; 195 uint8_t psegm = *(buf + i); 196 i++; 197 uint8_t psegmBuff[256]; 198 uint32_t pageLen = 0; 199 for(uint8_t j = 0; j < psegm; j++){ 200 psegmBuff[j] = *(buf + i); 201 pageLen += psegmBuff[j]; 202 i++; 203 } 204 return i; 205 } 206 //---------------------------------------------------------------------------------------------------------------------- 207 int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){ 208 209 if(m_f_OggS_found == true){ 210 m_f_OggS_found = false; 211 *bytesLeft -= FLACparseOggHeader(inbuf); 212 return ERR_FLAC_NONE; 213 } 214 215 if(m_status != OUT_SAMPLES){ 216 m_rIndex = 0; 217 m_bytesAvail = (*bytesLeft); 218 m_inptr = inbuf; 219 } 220 221 if(m_status == DECODE_FRAME){ // Read a ton of header fields, and ignore most of them 222 223 if ((inbuf[0] == 'O') && (inbuf[1] == 'g') && (inbuf[2] == 'g') && (inbuf[3] == 'S')){ 224 *bytesLeft -= 4; 225 m_f_OggS_found = true; 226 return ERR_FLAC_NONE; 227 } 228 229 uint32_t temp = readUint(8); 230 uint16_t sync = temp << 6 |readUint(6); 231 if (sync != 0x3FFE){ 232 log_i("Sync code expected 0x3FFE but received %X", sync); 233 return ERR_FLAC_SYNC_CODE_NOT_FOUND; 234 } 235 236 readUint(1); 237 FLACFrameHeader->blockingStrategy = readUint(1); 238 FLACFrameHeader->blockSizeCode = readUint(4); 239 FLACFrameHeader->sampleRateCode = readUint(4); 240 FLACFrameHeader->chanAsgn = readUint(4); 241 FLACFrameHeader->sampleSizeCode = readUint(3); 242 243 if(!FLACMetadataBlock->numChannels){ 244 if(FLACFrameHeader->chanAsgn == 0) FLACMetadataBlock->numChannels = 1; 245 if(FLACFrameHeader->chanAsgn == 1) FLACMetadataBlock->numChannels = 2; 246 if(FLACFrameHeader->chanAsgn > 7) FLACMetadataBlock->numChannels = 2; 247 } 248 if(FLACMetadataBlock->numChannels < 1) return ERR_FLAC_UNKNOWN_CHANNEL_ASSIGNMENT; 249 250 if(!FLACMetadataBlock->bitsPerSample){ 251 if(FLACFrameHeader->sampleSizeCode == 1) FLACMetadataBlock->bitsPerSample = 8; 252 if(FLACFrameHeader->sampleSizeCode == 2) FLACMetadataBlock->bitsPerSample = 12; 253 if(FLACFrameHeader->sampleSizeCode == 4) FLACMetadataBlock->bitsPerSample = 16; 254 if(FLACFrameHeader->sampleSizeCode == 5) FLACMetadataBlock->bitsPerSample = 20; 255 if(FLACFrameHeader->sampleSizeCode == 6) FLACMetadataBlock->bitsPerSample = 24; 256 } 257 if(FLACMetadataBlock->bitsPerSample > 16) return ERR_FLAC_BITS_PER_SAMPLE_TOO_BIG; 258 if(FLACMetadataBlock->bitsPerSample < 8 ) return ERR_FLAG_BITS_PER_SAMPLE_UNKNOWN; 259 260 if(!FLACMetadataBlock->sampleRate){ 261 if(FLACFrameHeader->sampleRateCode == 1) FLACMetadataBlock->sampleRate = 88200; 262 if(FLACFrameHeader->sampleRateCode == 2) FLACMetadataBlock->sampleRate = 176400; 263 if(FLACFrameHeader->sampleRateCode == 3) FLACMetadataBlock->sampleRate = 192000; 264 if(FLACFrameHeader->sampleRateCode == 4) FLACMetadataBlock->sampleRate = 8000; 265 if(FLACFrameHeader->sampleRateCode == 5) FLACMetadataBlock->sampleRate = 16000; 266 if(FLACFrameHeader->sampleRateCode == 6) FLACMetadataBlock->sampleRate = 22050; 267 if(FLACFrameHeader->sampleRateCode == 7) FLACMetadataBlock->sampleRate = 24000; 268 if(FLACFrameHeader->sampleRateCode == 8) FLACMetadataBlock->sampleRate = 32000; 269 if(FLACFrameHeader->sampleRateCode == 9) FLACMetadataBlock->sampleRate = 44100; 270 if(FLACFrameHeader->sampleRateCode == 10) FLACMetadataBlock->sampleRate = 48000; 271 if(FLACFrameHeader->sampleRateCode == 11) FLACMetadataBlock->sampleRate = 96000; 272 } 273 274 readUint(1); 275 temp = (readUint(8) << 24); 276 temp = ~temp; 277 278 uint32_t shift = 0x80000000; // Number of leading zeros 279 int8_t count = 0; 280 for(int i=0; i<32; i++){ 281 if((temp & shift) == 0) {count++; shift >>= 1;} 282 else break; 283 } 284 count--; 285 for (int i = 0; i < count; i++) readUint(8); 286 m_blockSize = 0; 287 288 if (FLACFrameHeader->blockSizeCode == 1) 289 m_blockSize = 192; 290 else if (2 <= FLACFrameHeader->blockSizeCode && FLACFrameHeader->blockSizeCode <= 5) 291 m_blockSize = 576 << (FLACFrameHeader->blockSizeCode - 2); 292 else if (FLACFrameHeader->blockSizeCode == 6) 293 m_blockSize = readUint(8) + 1; 294 else if (FLACFrameHeader->blockSizeCode == 7) 295 m_blockSize = readUint(16) + 1; 296 else if (8 <= FLACFrameHeader->blockSizeCode && FLACFrameHeader->blockSizeCode <= 15) 297 m_blockSize = 256 << (FLACFrameHeader->blockSizeCode - 8); 298 else{ 299 return ERR_FLAC_RESERVED_BLOCKSIZE_UNSUPPORTED; 300 } 301 302 if(m_blockSize > 8192){ 303 log_e("Error: blockSize too big"); 304 return ERR_FLAC_BLOCKSIZE_TOO_BIG; 305 } 306 307 if(FLACFrameHeader->sampleRateCode == 12) 308 readUint(8); 309 else if (FLACFrameHeader->sampleRateCode == 13 || FLACFrameHeader->sampleRateCode == 14){ 310 readUint(16); 311 } 312 readUint(8); 313 m_status = DECODE_SUBFRAMES; 314 *bytesLeft = m_bytesAvail; 315 m_blockSizeLeft = m_blockSize; 316 317 return ERR_FLAC_NONE; 318 } 319 320 if(m_status == DECODE_SUBFRAMES){ 321 322 // Decode each channel's subframe, then skip footer 323 int ret = decodeSubframes(); 324 if(ret != 0) return ret; 325 m_status = OUT_SAMPLES; 326 } 327 328 if(m_status == OUT_SAMPLES){ // Write the decoded samples 329 // blocksize can be much greater than outbuff, so we can't stuff all in once 330 // therefore we need often more than one loop (split outputblock into pieces) 331 uint16_t blockSize; 332 static uint16_t offset = 0; 333 if(m_blockSize < outBuffSize + offset) blockSize = m_blockSize - offset; 334 else blockSize = outBuffSize; 335 336 337 for (int i = 0; i < blockSize; i++) { 338 for (int j = 0; j < FLACMetadataBlock->numChannels; j++) { 339 int val = FLACsubFramesBuff->samplesBuffer[j][i + offset]; 340 if (FLACMetadataBlock->bitsPerSample == 8) val += 128; 341 outbuf[2*i+j] = val; 342 } 343 } 344 345 m_validSamples = blockSize * FLACMetadataBlock->numChannels; 346 offset += blockSize; 347 348 if(offset != m_blockSize) return GIVE_NEXT_LOOP; 349 offset = 0; 350 if(offset > m_blockSize) { log_e("offset has a wrong value"); } 351 } 352 353 alignToByte(); 354 readUint(16); 355 m_bytesDecoded = *bytesLeft - m_bytesAvail; 356 // log_i("m_bytesDecoded %i", m_bytesDecoded); 357 // m_compressionRatio = (float)m_bytesDecoded / (float)m_blockSize * FLACMetadataBlock->numChannels * (16/8); 358 // log_i("m_compressionRatio % f", m_compressionRatio); 359 *bytesLeft = m_bytesAvail; 360 m_status = DECODE_FRAME; 361 return ERR_FLAC_NONE; 362 } 363 //---------------------------------------------------------------------------------------------------------------------- 364 uint16_t FLACGetOutputSamps(){ 365 int vs = m_validSamples; 366 m_validSamples=0; 367 return vs; 368 } 369 //---------------------------------------------------------------------------------------------------------------------- 370 uint64_t FLACGetTotoalSamplesInStream(){ 371 return FLACMetadataBlock->totalSamples; 372 } 373 //---------------------------------------------------------------------------------------------------------------------- 374 uint8_t FLACGetBitsPerSample(){ 375 return FLACMetadataBlock->bitsPerSample; 376 } 377 //---------------------------------------------------------------------------------------------------------------------- 378 uint8_t FLACGetChannels(){ 379 return FLACMetadataBlock->numChannels; 380 } 381 //---------------------------------------------------------------------------------------------------------------------- 382 uint32_t FLACGetSampRate(){ 383 return FLACMetadataBlock->sampleRate; 384 } 385 //---------------------------------------------------------------------------------------------------------------------- 386 uint32_t FLACGetBitRate(){ 387 if(FLACMetadataBlock->totalSamples){ 388 float BitsPerSamp = (float)FLACMetadataBlock->audioDataLength / (float)FLACMetadataBlock->totalSamples * 8; 389 return ((uint32_t)BitsPerSamp * FLACMetadataBlock->sampleRate); 390 } 391 return 0; 392 } 393 //---------------------------------------------------------------------------------------------------------------------- 394 uint32_t FLACGetAudioFileDuration() { 395 if(FLACGetSampRate()){ 396 uint32_t afd = FLACGetTotoalSamplesInStream()/ FLACGetSampRate(); // AudioFileDuration 397 return afd; 398 } 399 return 0; 400 } 401 //---------------------------------------------------------------------------------------------------------------------- 402 int8_t decodeSubframes(){ 403 if(FLACFrameHeader->chanAsgn <= 7) { 404 for (int ch = 0; ch < FLACMetadataBlock->numChannels; ch++) 405 decodeSubframe(FLACMetadataBlock->bitsPerSample, ch); 406 } 407 else if (8 <= FLACFrameHeader->chanAsgn && FLACFrameHeader->chanAsgn <= 10) { 408 decodeSubframe(FLACMetadataBlock->bitsPerSample + (FLACFrameHeader->chanAsgn == 9 ? 1 : 0), 0); 409 decodeSubframe(FLACMetadataBlock->bitsPerSample + (FLACFrameHeader->chanAsgn == 9 ? 0 : 1), 1); 410 if(FLACFrameHeader->chanAsgn == 8) { 411 for (int i = 0; i < m_blockSize; i++) 412 FLACsubFramesBuff->samplesBuffer[1][i] = ( 413 FLACsubFramesBuff->samplesBuffer[0][i] - 414 FLACsubFramesBuff->samplesBuffer[1][i]); 415 } 416 else if (FLACFrameHeader->chanAsgn == 9) { 417 for (int i = 0; i < m_blockSize; i++) 418 FLACsubFramesBuff->samplesBuffer[0][i] += FLACsubFramesBuff->samplesBuffer[1][i]; 419 } 420 else if (FLACFrameHeader->chanAsgn == 10) { 421 for (int i = 0; i < m_blockSize; i++) { 422 long side = FLACsubFramesBuff->samplesBuffer[1][i]; 423 long right = FLACsubFramesBuff->samplesBuffer[0][i] - (side >> 1); 424 FLACsubFramesBuff->samplesBuffer[1][i] = right; 425 FLACsubFramesBuff->samplesBuffer[0][i] = right + side; 426 } 427 } 428 else { 429 log_e("unknown channel assignment"); 430 return ERR_FLAC_UNKNOWN_CHANNEL_ASSIGNMENT; 431 } 432 } 433 else{ 434 log_e("Reserved channel assignment"); 435 return ERR_FLAC_RESERVED_CHANNEL_ASSIGNMENT; 436 } 437 return ERR_FLAC_NONE; 438 } 439 //---------------------------------------------------------------------------------------------------------------------- 440 int8_t decodeSubframe(uint8_t sampleDepth, uint8_t ch) { 441 int8_t ret = 0; 442 readUint(1); 443 uint8_t type = readUint(6); 444 int shift = readUint(1); 445 if (shift == 1) { 446 while (readUint(1) == 0) 447 shift++; 448 } 449 sampleDepth -= shift; 450 451 if(type == 0){ // Constant coding 452 int16_t s= readSignedInt(sampleDepth); 453 for(int i=0; i < m_blockSize; i++){ 454 FLACsubFramesBuff->samplesBuffer[ch][i] = s; 455 } 456 } 457 else if (type == 1) { // Verbatim coding 458 for (int i = 0; i < m_blockSize; i++) 459 FLACsubFramesBuff->samplesBuffer[ch][i] = readSignedInt(sampleDepth); 460 } 461 else if (8 <= type && type <= 12){ 462 ret = decodeFixedPredictionSubframe(type - 8, sampleDepth, ch); 463 if(ret) return ret; 464 } 465 else if (32 <= type && type <= 63){ 466 ret = decodeLinearPredictiveCodingSubframe(type - 31, sampleDepth, ch); 467 if(ret) return ret; 468 } 469 else{ 470 return ERR_FLAC_RESERVED_SUB_TYPE; 471 } 472 if(shift>0){ 473 for (int i = 0; i < m_blockSize; i++){ 474 FLACsubFramesBuff->samplesBuffer[ch][i] <<= shift; 475 } 476 } 477 return ERR_FLAC_NONE; 478 } 479 //---------------------------------------------------------------------------------------------------------------------- 480 int8_t decodeFixedPredictionSubframe(uint8_t predOrder, uint8_t sampleDepth, uint8_t ch) { 481 uint8_t ret = 0; 482 for(uint8_t i = 0; i < predOrder; i++) 483 FLACsubFramesBuff->samplesBuffer[ch][i] = readSignedInt(sampleDepth); 484 ret = decodeResiduals(predOrder, ch); 485 if(ret) return ret; 486 coefs.clear(); 487 if(predOrder == 0) coefs.resize(0); 488 if(predOrder == 1) coefs.push_back(1); // FIXED_PREDICTION_COEFFICIENTS 489 if(predOrder == 2){coefs.push_back(2); coefs.push_back(-1);} 490 if(predOrder == 3){coefs.push_back(3); coefs.push_back(-3); coefs.push_back(1);} 491 if(predOrder == 4){coefs.push_back(4); coefs.push_back(-6); coefs.push_back(4); coefs.push_back(-1);} 492 if(predOrder > 4) return ERR_FLAC_PREORDER_TOO_BIG; // Error: preorder > 4" 493 restoreLinearPrediction(ch, 0); 494 return ERR_FLAC_NONE; 495 } 496 //---------------------------------------------------------------------------------------------------------------------- 497 int8_t decodeLinearPredictiveCodingSubframe(int lpcOrder, int sampleDepth, uint8_t ch){ 498 int8_t ret = 0; 499 for (int i = 0; i < lpcOrder; i++) 500 FLACsubFramesBuff->samplesBuffer[ch][i] = readSignedInt(sampleDepth); 501 int precision = readUint(4) + 1; 502 int shift = readSignedInt(5); 503 coefs.resize(0); 504 for (uint8_t i = 0; i < lpcOrder; i++) 505 coefs.push_back(readSignedInt(precision)); 506 ret = decodeResiduals(lpcOrder, ch); 507 if(ret) return ret; 508 restoreLinearPrediction(ch, shift); 509 return ERR_FLAC_NONE; 510 } 511 //---------------------------------------------------------------------------------------------------------------------- 512 int8_t decodeResiduals(uint8_t warmup, uint8_t ch) { 513 514 int method = readUint(2); 515 if (method >= 2) 516 return ERR_FLAC_RESERVED_RESIDUAL_CODING; // Reserved residual coding method 517 uint8_t paramBits = method == 0 ? 4 : 5; 518 int escapeParam = (method == 0 ? 0xF : 0x1F); 519 int partitionOrder = readUint(4); 520 521 int numPartitions = 1 << partitionOrder; 522 if (m_blockSize % numPartitions != 0) 523 return ERR_FLAC_WRONG_RICE_PARTITION_NR; //Error: Block size not divisible by number of Rice partitions 524 int partitionSize = m_blockSize/ numPartitions; 525 526 for (int i = 0; i < numPartitions; i++) { 527 int start = i * partitionSize + (i == 0 ? warmup : 0); 528 int end = (i + 1) * partitionSize; 529 530 int param = readUint(paramBits); 531 if (param < escapeParam) { 532 for (int j = start; j < end; j++){ 533 FLACsubFramesBuff->samplesBuffer[ch][j] = readRiceSignedInt(param); 534 } 535 } else { 536 int numBits = readUint(5); 537 for (int j = start; j < end; j++){ 538 FLACsubFramesBuff->samplesBuffer[ch][j] = readSignedInt(numBits); 539 } 540 } 541 } 542 return ERR_FLAC_NONE; 543 } 544 //---------------------------------------------------------------------------------------------------------------------- 545 void restoreLinearPrediction(uint8_t ch, uint8_t shift) { 546 547 for (int i = coefs.size(); i < m_blockSize; i++) { 548 int32_t sum = 0; 549 for (int j = 0; j < coefs.size(); j++){ 550 sum += FLACsubFramesBuff->samplesBuffer[ch][i - 1 - j] * coefs[j]; 551 } 552 FLACsubFramesBuff->samplesBuffer[ch][i] += (sum >> shift); 553 } 554 } 555 //---------------------------------------------------------------------------------------------------------------------- 556