Unverified Commit c12a22fd authored by Wolle's avatar Wolle Committed by GitHub

Add files via upload

parent 68ae4c67
......@@ -186,7 +186,7 @@ char* FLACgetStreamTitle(){
//----------------------------------------------------------------------------------------------------------------------
int FLACparseOggHeader(unsigned char *buf){
uint8_t i = 0;
uint16_t i = 0;
uint8_t ssv = *(buf + i); // stream_structure_version
(void)ssv;
i++;
......@@ -236,7 +236,7 @@ int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
if(m_f_OggS_found == true){
m_f_OggS_found = false;
*bytesLeft -= FLACparseOggHeader(inbuf);
return ERR_FLAC_NONE;
return FLAC_PARSE_OGG_DONE;
}
if(m_status != OUT_SAMPLES){
......@@ -260,26 +260,71 @@ int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
FLACFindStreamTitle(inbuf, m_page0_len);
}
*bytesLeft -= m_page0_len; // can be FLAC or title
return ERR_FLAC_NONE;
return FLAC_PARSE_OGG_DONE;
}
log_i("sync code not found");
return ERR_FLAC_SYNC_CODE_NOT_FOUND;
}
return flacDecodeFrame(inbuf, bytesLeft, outbuf);
}
if(m_status == DECODE_SUBFRAMES){
// Decode each channel's subframe, then skip footer
int ret = decodeSubframes();
if(ret != 0) return ret;
m_status = OUT_SAMPLES;
}
if(m_status == OUT_SAMPLES){ // Write the decoded samples
// blocksize can be much greater than outbuff, so we can't stuff all in once
// therefore we need often more than one loop (split outputblock into pieces)
uint16_t blockSize;
static uint16_t offset = 0;
if(m_blockSize < outBuffSize + offset) blockSize = m_blockSize - offset;
else blockSize = outBuffSize;
for (int i = 0; i < blockSize; i++) {
for (int j = 0; j < FLACMetadataBlock->numChannels; j++) {
int val = FLACsubFramesBuff->samplesBuffer[j][i + offset];
if (FLACMetadataBlock->bitsPerSample == 8) val += 128;
outbuf[2*i+j] = val;
}
}
m_validSamples = blockSize * FLACMetadataBlock->numChannels;
offset += blockSize;
if(offset != m_blockSize) return GIVE_NEXT_LOOP;
offset = 0;
if(offset > m_blockSize) { log_e("offset has a wrong value"); }
}
alignToByte();
readUint(16);
m_bytesDecoded = *bytesLeft - m_bytesAvail;
// log_i("m_bytesDecoded %i", m_bytesDecoded);
// m_compressionRatio = (float)m_bytesDecoded / (float)m_blockSize * FLACMetadataBlock->numChannels * (16/8);
// log_i("m_compressionRatio % f", m_compressionRatio);
*bytesLeft = m_bytesAvail;
m_status = DECODE_FRAME;
return ERR_FLAC_NONE;
}
//----------------------------------------------------------------------------------------------------------------------
int8_t flacDecodeFrame(uint8_t *inbuf, int *bytesLeft, short *outbuf){
readUint(14 + 1); // synccode + reserved bit
FLACFrameHeader->blockingStrategy = readUint(1);
FLACFrameHeader->blockSizeCode = readUint(4);
FLACFrameHeader->sampleRateCode = readUint(4);
FLACFrameHeader->chanAsgn = readUint(4);
FLACFrameHeader->sampleSizeCode = readUint(3);
if(!FLACMetadataBlock->numChannels){
if(FLACFrameHeader->chanAsgn == 0) FLACMetadataBlock->numChannels = 1;
if(FLACFrameHeader->chanAsgn == 1) FLACMetadataBlock->numChannels = 2;
if(FLACFrameHeader->chanAsgn > 7) FLACMetadataBlock->numChannels = 2;
}
if(FLACMetadataBlock->numChannels < 1) return ERR_FLAC_UNKNOWN_CHANNEL_ASSIGNMENT;
if(!FLACMetadataBlock->bitsPerSample){
if(FLACFrameHeader->sampleSizeCode == 1) FLACMetadataBlock->bitsPerSample = 8;
if(FLACFrameHeader->sampleSizeCode == 2) FLACMetadataBlock->bitsPerSample = 12;
......@@ -289,7 +334,6 @@ int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
}
if(FLACMetadataBlock->bitsPerSample > 16) return ERR_FLAC_BITS_PER_SAMPLE_TOO_BIG;
if(FLACMetadataBlock->bitsPerSample < 8 ) return ERR_FLAG_BITS_PER_SAMPLE_UNKNOWN;
if(!FLACMetadataBlock->sampleRate){
if(FLACFrameHeader->sampleRateCode == 1) FLACMetadataBlock->sampleRate = 88200;
if(FLACFrameHeader->sampleRateCode == 2) FLACMetadataBlock->sampleRate = 176400;
......@@ -303,11 +347,9 @@ int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
if(FLACFrameHeader->sampleRateCode == 10) FLACMetadataBlock->sampleRate = 48000;
if(FLACFrameHeader->sampleRateCode == 11) FLACMetadataBlock->sampleRate = 96000;
}
readUint(1);
uint32_t temp = (readUint(8) << 24);
temp = ~temp;
uint32_t shift = 0x80000000; // Number of leading zeros
int8_t count = 0;
for(int i=0; i<32; i++){
......@@ -317,7 +359,6 @@ int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
count--;
for (int i = 0; i < count; i++) readUint(8);
m_blockSize = 0;
if (FLACFrameHeader->blockSizeCode == 1)
m_blockSize = 192;
else if (2 <= FLACFrameHeader->blockSizeCode && FLACFrameHeader->blockSizeCode <= 5)
......@@ -331,12 +372,10 @@ int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
else{
return ERR_FLAC_RESERVED_BLOCKSIZE_UNSUPPORTED;
}
if(m_blockSize > 8192){
log_e("Error: blockSize too big");
return ERR_FLAC_BLOCKSIZE_TOO_BIG;
}
if(FLACFrameHeader->sampleRateCode == 12)
readUint(8);
else if (FLACFrameHeader->sampleRateCode == 13 || FLACFrameHeader->sampleRateCode == 14){
......@@ -346,51 +385,6 @@ int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf){
m_status = DECODE_SUBFRAMES;
*bytesLeft = m_bytesAvail;
m_blockSizeLeft = m_blockSize;
return ERR_FLAC_NONE;
}
if(m_status == DECODE_SUBFRAMES){
// Decode each channel's subframe, then skip footer
int ret = decodeSubframes();
if(ret != 0) return ret;
m_status = OUT_SAMPLES;
}
if(m_status == OUT_SAMPLES){ // Write the decoded samples
// blocksize can be much greater than outbuff, so we can't stuff all in once
// therefore we need often more than one loop (split outputblock into pieces)
uint16_t blockSize;
static uint16_t offset = 0;
if(m_blockSize < outBuffSize + offset) blockSize = m_blockSize - offset;
else blockSize = outBuffSize;
for (int i = 0; i < blockSize; i++) {
for (int j = 0; j < FLACMetadataBlock->numChannels; j++) {
int val = FLACsubFramesBuff->samplesBuffer[j][i + offset];
if (FLACMetadataBlock->bitsPerSample == 8) val += 128;
outbuf[2*i+j] = val;
}
}
m_validSamples = blockSize * FLACMetadataBlock->numChannels;
offset += blockSize;
if(offset != m_blockSize) return GIVE_NEXT_LOOP;
offset = 0;
if(offset > m_blockSize) { log_e("offset has a wrong value"); }
}
alignToByte();
readUint(16);
m_bytesDecoded = *bytesLeft - m_bytesAvail;
// log_i("m_bytesDecoded %i", m_bytesDecoded);
// m_compressionRatio = (float)m_bytesDecoded / (float)m_blockSize * FLACMetadataBlock->numChannels * (16/8);
// log_i("m_compressionRatio % f", m_compressionRatio);
*bytesLeft = m_bytesAvail;
m_status = DECODE_FRAME;
return ERR_FLAC_NONE;
}
//----------------------------------------------------------------------------------------------------------------------
......
......@@ -27,7 +27,8 @@ typedef struct FLACsubFramesBuff_t{
enum : uint8_t {FLACDECODER_INIT, FLACDECODER_READ_IN, FLACDECODER_WRITE_OUT};
enum : uint8_t {DECODE_FRAME, DECODE_SUBFRAMES, OUT_SAMPLES};
enum : int8_t {GIVE_NEXT_LOOP = +1,
enum : int8_t {FLAC_PARSE_OGG_DONE = 100,
GIVE_NEXT_LOOP = +1,
ERR_FLAC_NONE = 0,
ERR_FLAC_BLOCKSIZE_TOO_BIG = -1,
ERR_FLAC_RESERVED_BLOCKSIZE_UNSUPPORTED = -2,
......@@ -153,6 +154,7 @@ void FLACDecoder_FreeBuffers();
void FLACSetRawBlockParams(uint8_t Chans, uint32_t SampRate, uint8_t BPS, uint32_t tsis, uint32_t AuDaLength);
void FLACDecoderReset();
int8_t FLACDecode(uint8_t *inbuf, int *bytesLeft, short *outbuf);
int8_t flacDecodeFrame(uint8_t *inbuf, int *bytesLeft, short *outbuf);
uint16_t FLACGetOutputSamps();
uint64_t FLACGetTotoalSamplesInStream();
uint8_t FLACGetBitsPerSample();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment