X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=platform%2Fgp2x%2Fcode940%2F940.c;h=a4d3a464d8a0549e9d6ded3473f0176f764bf94b;hb=1f1ff763e661bab664151c4821c65dad35777976;hp=0df19453db952452f7a61cc2f54f8954413e7984;hpb=e4fb433cd685d06ddbf0ec367c19a38b75d6ac68;p=picodrive.git diff --git a/platform/gp2x/code940/940.c b/platform/gp2x/code940/940.c index 0df1945..a4d3a46 100644 --- a/platform/gp2x/code940/940.c +++ b/platform/gp2x/code940/940.c @@ -25,39 +25,79 @@ void _memcpy(void *dst, const void *src, int count); // asm volatile ("mcr p15, 0, r0, c7, c10, 4" ::: "r0"); /* drain write buffer */ +static int find_sync_word(unsigned char *buf, int nBytes) +{ + unsigned char *p, *pe; + + /* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching bits */ + for (p = buf, pe = buf + nBytes - 4; p < pe; p++) + { + int pn; + if (p[0] != 0xff) continue; + pn = p[1]; + if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1 + (pn & 6) == 0) { // invalid layer + p++; continue; + } + pn = p[2]; + if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates + (pn & 0x0c) != 0) { // not 44kHz + continue; + } + + return p - buf; + } + + return -1; +} + static void mp3_decode(void) { int mp3_offs = shared_ctl->mp3_offs; unsigned char *readPtr = mp3_data + mp3_offs; int bytesLeft = shared_ctl->mp3_len - mp3_offs; int offset; // frame offset from readPtr - int err; + int retries = 0, err; if (bytesLeft <= 0) return; // EOF, nothing to do - offset = MP3FindSyncWord(readPtr, bytesLeft); - if (offset < 0) { - set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len); - return; // EOF - } - readPtr += offset; - bytesLeft -= offset; - - err = MP3Decode(shared_data->mp3dec, &readPtr, &bytesLeft, - shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0); - if (err) { - if (err == ERR_MP3_INDATA_UNDERFLOW) { - set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len); - return; - } else if (err <= -6 && err >= -12) { - // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_* - // just try to skip the offending frame.. - readPtr++; + for (retries = 0; retries < 2; retries++) + { + offset = find_sync_word(readPtr, bytesLeft); + if (offset < 0) + goto set_eof; + + readPtr += offset; + bytesLeft -= offset; + + err = MP3Decode(shared_data->mp3dec, &readPtr, &bytesLeft, + shared_data->mp3_buffer[shared_ctl->mp3_buffsel], 0); + if (err) { + if (err == ERR_MP3_MAINDATA_UNDERFLOW) + // just need another frame + continue; + + if (err == ERR_MP3_INDATA_UNDERFLOW) + goto set_eof; + + if (err <= -6 && err >= -12) { + // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_* + // just try to skip the offending frame.. + readPtr++; + bytesLeft--; + continue; + } + shared_ctl->mp3_errors++; + shared_ctl->mp3_lasterr = err; } - shared_ctl->mp3_errors++; - shared_ctl->mp3_lasterr = err; + break; } + set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, readPtr - mp3_data); + return; + +set_eof: + set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len); } static void ym_flush_writes(void) @@ -189,6 +229,11 @@ void Main940(void) case JOB940_MP3DECODE: mp3_decode(); break; + + case JOB940_MP3RESET: + if (shared_data->mp3dec) MP3FreeDecoder(shared_data->mp3dec); + shared_data->mp3dec = MP3InitDecoder(); + break; } shared_ctl->loopc++;