Skip to content

Commit 6c9eaf1

Browse files
committed
MP3Decoder: better handle underflows of mp3 stream
* Don't consume in the case of "indata overflow". Doing so leaves us at a bad boundary within the MP3 data and can continue decoding from an inappropriate spot i.e., one that looks likede mp3 data but is NOT. because there are many crashing bugs in the helix mp3 library on invalid input data, we must do our best to avoid them, and this is one way to do that. * clear the output buffer in the case there's not a sync word in the buffer. this can also happen when too little data is available. this changes more "stuttering" conditions into "silent" conditions. With these changes, I can get through 3+ plays of "idea.mp3" from a local http server with long pauses (but not stuttering glitches or safe mode crashes). I was also able to play through 10+ minutes of http://ice2.somafm.com/dronezone-128-mp3 without crashing or "end of stream", though again there are pauses due to packet loss. I think this is good now, except for the problems that arise when the socket layer doesn't deliver a fresh packet for a long time.
1 parent cb295c5 commit 6c9eaf1

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

shared-module/audiomp3/MP3Decoder.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,16 @@ audioio_get_buffer_result_t audiomp3_mp3file_get_buffer(audiomp3_mp3file_obj_t *
447447

448448
mp3file_skip_id3v2(self, false);
449449
if (!mp3file_find_sync_word(self, false)) {
450+
memset(buffer, 0, self->frame_buffer_size);
450451
*buffer_length = 0;
451452
return self->eof ? GET_BUFFER_DONE : GET_BUFFER_ERROR;
452453
}
453454
int bytes_left = BYTES_LEFT(self);
454455
uint8_t *inbuf = READ_PTR(self);
455456
int err = MP3Decode(self->decoder, &inbuf, &bytes_left, buffer, 0);
456-
CONSUME(self, BYTES_LEFT(self) - bytes_left);
457-
457+
if (err != ERR_MP3_INDATA_UNDERFLOW) {
458+
CONSUME(self, BYTES_LEFT(self) - bytes_left);
459+
}
458460
if (err) {
459461
memset(buffer, 0, frame_buffer_size_bytes);
460462
if (DO_DEBUG) {

0 commit comments

Comments
 (0)