Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
ESP32-audioI2S
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
xpstem
ESP32-audioI2S
Commits
c12a22fd
Unverified
Commit
c12a22fd
authored
Feb 09, 2023
by
Wolle
Committed by
GitHub
Feb 09, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add files via upload
parent
68ae4c67
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
83 additions
and
87 deletions
+83
-87
src/flac_decoder/flac_decoder.cpp
src/flac_decoder/flac_decoder.cpp
+80
-86
src/flac_decoder/flac_decoder.h
src/flac_decoder/flac_decoder.h
+3
-1
No files found.
src/flac_decoder/flac_decoder.cpp
View file @
c12a22fd
...
...
@@ -186,7 +186,7 @@ char* FLACgetStreamTitle(){
//----------------------------------------------------------------------------------------------------------------------
int
FLACparseOggHeader
(
unsigned
char
*
buf
){
uint
8
_t
i
=
0
;
uint
16
_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_N
ONE
;
return
FLAC_PARSE_OGG_D
ONE
;
}
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_N
ONE
;
return
FLAC_PARSE_OGG_D
ONE
;
}
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
;
}
//----------------------------------------------------------------------------------------------------------------------
...
...
src/flac_decoder/flac_decoder.h
View file @
c12a22fd
...
...
@@ -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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment