X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=platform%2Fgp2x%2Fcode940%2F940.c;h=760816eb4afe806b72685269faec7f8abf04937a;hb=fda2f31020bf0d6cf7b5dd70ec01cf390b7e1483;hp=90087a3e2fae583e21f485f8ffc5b450abec3713;hpb=091facb8fe133c8318ed13147b244e9af602a2ac;p=picodrive.git diff --git a/platform/gp2x/code940/940.c b/platform/gp2x/code940/940.c index 90087a3..760816e 100644 --- a/platform/gp2x/code940/940.c +++ b/platform/gp2x/code940/940.c @@ -2,6 +2,7 @@ // (c) Copyright 2006-2007, Grazvydas "notaz" Ignotas #include "940shared.h" +#include "../../common/mp3.h" static _940_data_t *shared_data = (_940_data_t *) 0x00100000; static _940_ctl_t *shared_ctl = (_940_ctl_t *) 0x00200000; @@ -25,32 +26,6 @@ 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; @@ -61,32 +36,43 @@ static void mp3_decode(void) if (bytesLeft <= 0) return; // EOF, nothing to do -retry: - offset = find_sync_word(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++; - bytesLeft--; - if (retries++ < 2) goto retry; + for (retries = 0; retries < 2; retries++) + { + offset = mp3_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)