diff --git a/src/add-ons/media/plugins/mp3_decoder/mp3DecoderPlugin.cpp b/src/add-ons/media/plugins/mp3_decoder/mp3DecoderPlugin.cpp index 6271a9d3da..2129fe944c 100644 --- a/src/add-ons/media/plugins/mp3_decoder/mp3DecoderPlugin.cpp +++ b/src/add-ons/media/plugins/mp3_decoder/mp3DecoderPlugin.cpp @@ -14,6 +14,9 @@ #define DECODE_BUFFER_SIZE (32 * 1024) +// reference: +// http://www.mp3-tech.org/programmer/frame_header.html + inline size_t AudioBufferSize(const media_raw_audio_format &raf, bigtime_t buffer_duration = 50000 /* 50 ms */) { @@ -73,6 +76,7 @@ mp3Decoder::mp3Decoder() fChannelCount = 0; fOutputBufferSize = 0; fNeedSync = false; + fDecodingError = false; } @@ -158,6 +162,7 @@ mp3Decoder::Seek(uint32 seekTo, InitMP3(&fMpgLibPrivate); fResidualBytes = 0; fNeedSync = true; + fDecodingError = false; return B_OK; } @@ -171,6 +176,9 @@ mp3Decoder::Decode(void *buffer, int64 *frameCount, int32 out_bytes_needed = fOutputBufferSize; int32 out_bytes = 0; + if (fDecodingError) + return B_ERROR; + mediaHeader->start_time = fStartTime; //TRACE("mp3Decoder: Decoding start time %.6f\n", fStartTime / 1000000.0); @@ -191,8 +199,10 @@ mp3Decoder::Decode(void *buffer, int64 *frameCount, } last_err = DecodeNextChunk(); - if (last_err != B_OK) + if (last_err != B_OK) { + fDecodingError = true; break; + } } *frameCount = out_bytes / fFrameSize; diff --git a/src/add-ons/media/plugins/mp3_decoder/mp3DecoderPlugin.h b/src/add-ons/media/plugins/mp3_decoder/mp3DecoderPlugin.h index bac581738d..a0cbdfd9dd 100644 --- a/src/add-ons/media/plugins/mp3_decoder/mp3DecoderPlugin.h +++ b/src/add-ons/media/plugins/mp3_decoder/mp3DecoderPlugin.h @@ -40,6 +40,7 @@ private: int fChannelCount; int fOutputBufferSize; bool fNeedSync; + bool fDecodingError; }; diff --git a/src/add-ons/media/plugins/mp3_decoder/mpglib/common.c b/src/add-ons/media/plugins/mp3_decoder/mpglib/common.c index f906845498..a71b2ab25e 100644 --- a/src/add-ons/media/plugins/mp3_decoder/mpglib/common.c +++ b/src/add-ons/media/plugins/mp3_decoder/mpglib/common.c @@ -58,8 +58,8 @@ int decode_header(struct frame *fr,unsigned long newhead) fr->lay = 4-((newhead>>17)&3); if( ((newhead>>10)&0x3) == 0x3) { - fprintf(stderr,"Stream error\n"); - exit(1); + fprintf(stderr,"Stream error, reserved sampling rate\n"); + return 0; } if(fr->mpeg25) { fr->sampling_frequency = 6 + ((newhead>>10)&0x3); @@ -84,8 +84,8 @@ int decode_header(struct frame *fr,unsigned long newhead) if(!fr->bitrate_index) { - fprintf(stderr,"Free format not supported.\n"); - return (0); + fprintf(stderr, "Stream error, free format bitrate index not supported\n"); + return 0; } switch(fr->lay) @@ -126,7 +126,7 @@ int decode_header(struct frame *fr,unsigned long newhead) fr->framesize = fr->framesize + fr->padding - 4; break; default: - fprintf(stderr,"Sorry, unknown layer type.\n"); + fprintf(stderr,"Stream error, unknown layer type.\n"); return (0); } return 1; diff --git a/src/add-ons/media/plugins/mp3_decoder/mpglib/interface.c b/src/add-ons/media/plugins/mp3_decoder/mpglib/interface.c index d7844ef494..fa29fe09e7 100644 --- a/src/add-ons/media/plugins/mp3_decoder/mpglib/interface.c +++ b/src/add-ons/media/plugins/mp3_decoder/mpglib/interface.c @@ -94,14 +94,19 @@ static int read_buf_byte(struct mpstr *mp) int pos; + if(!mp->tail) { + fprintf(stderr,"Fatal error!\n"); + return 0; + } + pos = mp->tail->pos; while(pos >= mp->tail->size) { remove_buf(mp); - pos = mp->tail->pos; if(!mp->tail) { fprintf(stderr,"Fatal error!\n"); - exit(1); + return 0; } + pos = mp->tail->pos; } b = mp->tail->pnt[pos]; @@ -149,7 +154,8 @@ int decodeMP3(struct mpstr *mp,char *in,int isize,char *out, return MP3_NEED_MORE; } read_head(mp); - decode_header(&mp->fr,mp->header); + if (decode_header(&mp->fr,mp->header) == 0) + return MP3_ERR; mp->framesize = mp->fr.framesize; }