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.h (9654B)
1 /* 2 * flac_decoder.h 3 * 4 * Created on: Jul 03,2020 5 * Updated on: Apr 27,2021 6 * 7 * Author: wolle 8 * 9 * Restrictions: 10 * blocksize must not exceed 8192 11 * bits per sample must be 8 or 16 12 * num Channels must be 1 or 2 13 * 14 * 15 */ 16 #pragma once 17 #pragma GCC optimize ("Ofast") 18 19 #include "Arduino.h" 20 21 #define MAX_CHANNELS 2 22 #define MAX_BLOCKSIZE 8192 23 #define APLL_DISABLE 0 24 #define EXTERNAL_I2S 0 25 26 27 typedef struct FLACsubFramesBuff_t{ 28 int32_t samplesBuffer[MAX_CHANNELS][MAX_BLOCKSIZE]; 29 }FLACsubframesBuffer_t; 30 31 enum : uint8_t {FLACDECODER_INIT, FLACDECODER_READ_IN, FLACDECODER_WRITE_OUT}; 32 enum : uint8_t {DECODE_FRAME, DECODE_SUBFRAMES, OUT_SAMPLES}; 33 enum : int8_t {GIVE_NEXT_LOOP = +1, 34 ERR_FLAC_NONE = 0, 35 ERR_FLAC_BLOCKSIZE_TOO_BIG = -1, 36 ERR_FLAC_RESERVED_BLOCKSIZE_UNSUPPORTED = -2, 37 ERR_FLAC_SYNC_CODE_NOT_FOUND = -3, 38 ERR_FLAC_UNKNOWN_CHANNEL_ASSIGNMENT = -4, 39 ERR_FLAC_RESERVED_CHANNEL_ASSIGNMENT = -5, 40 ERR_FLAC_RESERVED_SUB_TYPE = -6, 41 ERR_FLAC_PREORDER_TOO_BIG = -7, 42 ERR_FLAC_RESERVED_RESIDUAL_CODING = -8, 43 ERR_FLAC_WRONG_RICE_PARTITION_NR = -9, 44 ERR_FLAC_BITS_PER_SAMPLE_TOO_BIG = -10, 45 ERR_FLAG_BITS_PER_SAMPLE_UNKNOWN = 11}; 46 47 typedef struct FLACMetadataBlock_t{ 48 // METADATA_BLOCK_STREAMINFO 49 uint16_t minblocksize; // The minimum block size (in samples) used in the stream. 50 //---------------------------------------------------------------------------------------- 51 // The maximum block size (in samples) used in the stream. 52 uint16_t maxblocksize; // (Minimum blocksize == maximum blocksize) implies a fixed-blocksize stream. 53 //---------------------------------------------------------------------------------------- 54 // The minimum frame size (in bytes) used in the stream. 55 uint32_t minframesize; // May be 0 to imply the value is not known. 56 //---------------------------------------------------------------------------------------- 57 // The maximum frame size (in bytes) used in the stream. 58 uint32_t maxframesize; // May be 0 to imply the value is not known. 59 //---------------------------------------------------------------------------------------- 60 // Sample rate in Hz. Though 20 bits are available, 61 // the maximum sample rate is limited by the structure of frame headers to 655350Hz. 62 uint32_t sampleRate; // Also, a value of 0 is invalid. 63 //---------------------------------------------------------------------------------------- 64 // Number of channels FLAC supports from 1 to 8 channels 65 uint8_t numChannels; // 000 : 1 channel .... 111 : 8 channels 66 //---------------------------------------------------------------------------------------- 67 // Sample size in bits: 68 // 000 : get from STREAMINFO metadata block 69 // 001 : 8 bits per sample 70 // 010 : 12 bits per sample 71 // 011 : reserved 72 // 100 : 16 bits per sample 73 // 101 : 20 bits per sample 74 // 110 : 24 bits per sample 75 uint8_t bitsPerSample; // 111 : reserved 76 //---------------------------------------------------------------------------------------- 77 // Total samples in stream. 'Samples' means inter-channel sample, 78 // i.e. one second of 44.1Khz audio will have 44100 samples regardless of the number 79 uint64_t totalSamples; // of channels. A value of zero here means the number of total samples is unknown. 80 //---------------------------------------------------------------------------------------- 81 uint32_t audioDataLength;// is not the filelength, is only the length of the audio datablock in bytes 82 83 84 85 }FLACMetadataBlock_t; 86 87 88 typedef struct FLACFrameHeader_t { 89 // 0 : fixed-blocksize stream; frame header encodes the frame number 90 uint8_t blockingStrategy; // 1 : variable-blocksize stream; frame header encodes the sample number 91 //---------------------------------------------------------------------------------------- 92 // Block size in inter-channel samples: 93 // 0000 : reserved 94 // 0001 : 192 samples 95 // 0010-0101 : 576 * (2^(n-2)) samples, i.e. 576/1152/2304/4608 96 // 0110 : get 8 bit (blocksize-1) from end of header 97 // 0111 : get 16 bit (blocksize-1) from end of header 98 uint8_t blockSizeCode; // 1000-1111 : 256 * (2^(n-8)) samples, i.e. 256/512/1024/2048/4096/8192/16384/32768 99 //---------------------------------------------------------------------------------------- 100 // 0000 : get from STREAMINFO metadata block 101 // 0001 : 88.2kHz 102 // 0010 : 176.4kHz 103 // 0011 : 192kHz 104 // 0100 : 8kHz 105 // 0101 : 16kHz 106 // 0110 : 22.05kHz 107 // 0111 : 24kHz 108 // 1000 : 32kHz 109 // 1001 : 44.1kHz 110 // 1010 : 48kHz 111 // 1011 : 96kHz 112 // 1100 : get 8 bit sample rate (in kHz) from end of header 113 // 1101 : get 16 bit sample rate (in Hz) from end of header 114 // 1110 : get 16 bit sample rate (in tens of Hz) from end of header 115 uint8_t sampleRateCode; // 1111 : invalid, to prevent sync-fooling string of 1s 116 //---------------------------------------------------------------------------------------- 117 // Channel assignment 118 // 0000 1 channel: mono 119 // 0001 2 channels: left, right 120 // 0010 3 channels 121 // 0011 4 channels 122 // 0100 5 channels 123 // 0101 6 channels 124 // 0110 7 channels 125 // 0111 8 channels 126 // 1000 : left/side stereo: channel 0 is the left channel, channel 1 is the side(difference) channel 127 // 1001 : right/side stereo: channel 0 is the side(difference) channel, channel 1 is the right channel 128 // 1010 : mid/side stereo: channel 0 is the mid(average) channel, channel 1 is the side(difference) channel 129 uint8_t chanAsgn; // 1011-1111 : reserved 130 //---------------------------------------------------------------------------------------- 131 // Sample size in bits: 132 // 000 : get from STREAMINFO metadata block 133 // 001 : 8 bits per sample 134 // 010 : 12 bits per sample 135 // 011 : reserved 136 // 100 : 16 bits per sample 137 // 101 : 20 bits per sample 138 // 110 : 24 bits per sample 139 uint8_t sampleSizeCode; // 111 : reserved 140 //---------------------------------------------------------------------------------------- 141 uint32_t totalSamples; // totalSamplesInStream 142 //---------------------------------------------------------------------------------------- 143 uint32_t bitrate; // bitrate 144 145 146 }FLACFrameHeader_t; 147 148 int FLACFindSyncWord(unsigned char *buf, int nBytes); 149 int FLACFindOggSyncWord(unsigned char *buf, int nBytes); 150 int FLACparseOggHeader(unsigned char *buf); 151 bool FLACDecoder_AllocateBuffers(void); 152 void FLACDecoder_ClearBuffer(); 153 void FLACDecoder_FreeBuffers(); 154 void FLACSetRawBlockParams(uint8_t Chans, uint32_t SampRate, uint8_t BPS, uint32_t tsis, uint32_t AuDaLength); 155 void FLACDecoderReset(); 156 int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf); 157 uint16_t FLACGetOutputSamps(); 158 uint64_t FLACGetTotoalSamplesInStream(); 159 uint8_t FLACGetBitsPerSample(); 160 uint8_t FLACGetChannels(); 161 uint32_t FLACGetSampRate(); 162 uint32_t FLACGetBitRate(); 163 uint32_t FLACGetAudioFileDuration(); 164 uint32_t readUint(uint8_t nBits); 165 int32_t readSignedInt(int nBits); 166 int64_t readRiceSignedInt(uint8_t param); 167 void alignToByte(); 168 int8_t decodeSubframes(); 169 int8_t decodeSubframe(uint8_t sampleDepth, uint8_t ch); 170 int8_t decodeFixedPredictionSubframe(uint8_t predOrder, uint8_t sampleDepth, uint8_t ch); 171 int8_t decodeLinearPredictiveCodingSubframe(int lpcOrder, int sampleDepth, uint8_t ch); 172 int8_t decodeResiduals(uint8_t warmup, uint8_t ch); 173 void restoreLinearPrediction(uint8_t ch, uint8_t shift); 174 175