Skip to content

Commit

Permalink
prevent maindata underflow at start
Browse files Browse the repository at this point in the history
and remove some unnecessary code
  • Loading branch information
schreibfaul1 committed Apr 22, 2024
1 parent 59784c5 commit e2e4df3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
32 changes: 14 additions & 18 deletions src/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Created on: Oct 26.2018
*
* Version 3.0.9h
* Version 3.0.9i
* Updated on: Apr 22.2024
* Author: Wolle (schreibfaul1)
*
Expand Down Expand Up @@ -4426,25 +4426,21 @@ int Audio::sendBytes(uint8_t* data, size_t len) {
// < 0: there has been an error

if(m_decodeError < 0) { // Error, skip the frame...
// i2s_zero_dma_buffer((i2s_port_t)m_i2s_num);
if(m_codec == CODEC_MP3 && (m_decodeError == -2)) {
; // at the beginning this doesn't have to be a mistake, suppress errorcode MAINDATA_UNDERFLOW

printDecodeError(m_decodeError);
m_f_playing = false; // seek for new syncword
if(m_codec == CODEC_FLAC) {
// if(m_decodeError == ERR_FLAC_BITS_PER_SAMPLE_TOO_BIG) stopSong();
// if(m_decodeError == ERR_FLAC_RESERVED_CHANNEL_ASSIGNMENT) stopSong();
}
else {
printDecodeError(m_decodeError);
m_f_playing = false; // seek for new syncword
if(m_codec == CODEC_FLAC) {
// if(m_decodeError == ERR_FLAC_BITS_PER_SAMPLE_TOO_BIG) stopSong();
// if(m_decodeError == ERR_FLAC_RESERVED_CHANNEL_ASSIGNMENT) stopSong();
}
if(m_codec == CODEC_OPUS) {
if(m_decodeError == ERR_OPUS_HYBRID_MODE_UNSUPPORTED) stopSong();
if(m_decodeError == ERR_OPUS_SILK_MODE_UNSUPPORTED) stopSong();
if(m_decodeError == ERR_OPUS_NARROW_BAND_UNSUPPORTED) stopSong();
if(m_decodeError == ERR_OPUS_WIDE_BAND_UNSUPPORTED) stopSong();
if(m_decodeError == ERR_OPUS_SUPER_WIDE_BAND_UNSUPPORTED) stopSong();
}
if(m_codec == CODEC_OPUS) {
if(m_decodeError == ERR_OPUS_HYBRID_MODE_UNSUPPORTED) stopSong();
if(m_decodeError == ERR_OPUS_SILK_MODE_UNSUPPORTED) stopSong();
if(m_decodeError == ERR_OPUS_NARROW_BAND_UNSUPPORTED) stopSong();
if(m_decodeError == ERR_OPUS_WIDE_BAND_UNSUPPORTED) stopSong();
if(m_decodeError == ERR_OPUS_SUPER_WIDE_BAND_UNSUPPORTED) stopSong();
}

return 1; // skip one byte and seek for the next sync word
}
bytesDecoded = len - bytesLeft;
Expand Down
8 changes: 7 additions & 1 deletion src/mp3_decoder/mp3_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* libhelix_HMP3DECODER
*
* Created on: 26.10.2018
* Updated on: 29.03.2023
* Updated on: 22.04.2023
*/
#include "mp3_decoder.h"
/* clip to range [-2^n, 2^n - 1] */
Expand Down Expand Up @@ -1382,6 +1382,7 @@ int MP3Decode( unsigned char *inbuf, int *bytesLeft, short *outbuf, int useSize)
int offset, bitOffset, mainBits, gr, ch, fhBytes, siBytes, freeFrameBytes;
int prevBitOffset, sfBlockBits, huffBlockBits;
unsigned char *mainPtr;
static uint8_t underflowCounter = 0; // http://macslons-irish-pub-radio.stream.laut.fm/macslons-irish-pub-radio

/* unpack frame header */
fhBytes = UnpackFrameHeader(inbuf);
Expand Down Expand Up @@ -1444,6 +1445,7 @@ int MP3Decode( unsigned char *inbuf, int *bytesLeft, short *outbuf, int useSize)
/* fill main data buffer with enough new data for this frame */
if (m_MP3DecInfo->mainDataBytes >= m_MP3DecInfo->mainDataBegin) {
/* adequate "old" main data available (i.e. bit reservoir) */
underflowCounter = 0;
memmove(m_MP3DecInfo->mainBuf,
m_MP3DecInfo->mainBuf + m_MP3DecInfo->mainDataBytes - m_MP3DecInfo->mainDataBegin,
m_MP3DecInfo->mainDataBegin);
Expand All @@ -1456,10 +1458,14 @@ int MP3Decode( unsigned char *inbuf, int *bytesLeft, short *outbuf, int useSize)
mainPtr = m_MP3DecInfo->mainBuf;
} else {
/* not enough data in bit reservoir from previous frames (perhaps starting in middle of file) */
underflowCounter ++;
memcpy(m_MP3DecInfo->mainBuf + m_MP3DecInfo->mainDataBytes, inbuf, m_MP3DecInfo->nSlots);
m_MP3DecInfo->mainDataBytes += m_MP3DecInfo->nSlots;
inbuf += m_MP3DecInfo->nSlots;
*bytesLeft -= (m_MP3DecInfo->nSlots);
if(underflowCounter < 4){
return ERR_MP3_NONE;
}
MP3ClearBadFrame( outbuf);
return ERR_MP3_MAINDATA_UNDERFLOW;
}
Expand Down

0 comments on commit e2e4df3

Please sign in to comment.