X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=common%2Fmp3_helix.c;h=718c3def95cc0a172a31fe7391d7c316670461ba;hb=fa5e045bdc817112c1abf19e65e2d3481d51c48a;hp=a5da3761eadddd3a90bc15f78b0b7cb8e3fd24e5;hpb=11f4e72292ab00c686f2a2288e4f1cda3a0f7abd;p=libpicofe.git diff --git a/common/mp3_helix.c b/common/mp3_helix.c index a5da376..718c3de 100644 --- a/common/mp3_helix.c +++ b/common/mp3_helix.c @@ -6,23 +6,49 @@ #include #include -#include "../../Pico/PicoInt.h" -#include "../../Pico/sound/mix.h" +#include "../../pico/pico_int.h" +#include "../../pico/sound/mix.h" #include "helix/pub/mp3dec.h" #include "lprintf.h" -static short mp3_out_buffer[2*1152]; static HMP3Decoder mp3dec = 0; static int mp3_buffer_offs = 0; +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 int try_get_header(unsigned char *buff, MP3FrameInfo *fi) { int ret, offs1, offs = 0; while (1) { - offs1 = MP3FindSyncWord(buff + offs, 2048 - offs); + offs1 = find_sync_word(buff + offs, 2048 - offs); if (offs1 < 0) return -2; offs += offs1; if (2048 - offs < 4) return -3; @@ -45,7 +71,8 @@ int mp3_get_bitrate(FILE *f, int len) memset(buff, 0, 2048); - if (!mp3dec) mp3dec = MP3InitDecoder(); + if (mp3dec) MP3FreeDecoder(mp3dec); + mp3dec = MP3InitDecoder(); fseek(f, 0, SEEK_SET); ret = fread(buff, 1, 2048, f); @@ -82,11 +109,12 @@ static int mp3_decode(void) unsigned char *readPtr = mp3_mem + 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 1; // EOF, nothing to do - offset = MP3FindSyncWord(readPtr, bytesLeft); +retry: + offset = find_sync_word(readPtr, bytesLeft); if (offset < 0) { shared_ctl->mp3_offs = shared_ctl->mp3_len; return 1; // EOF @@ -94,7 +122,7 @@ static int mp3_decode(void) readPtr += offset; bytesLeft -= offset; - err = MP3Decode(mp3dec, &readPtr, &bytesLeft, mp3_out_buffer, 0); + err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0); if (err) { if (err == ERR_MP3_INDATA_UNDERFLOW) { shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF @@ -103,6 +131,9 @@ static int mp3_decode(void) // ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_* // just try to skip the offending frame.. readPtr++; + bytesLeft--; + if (retries++ < 2) goto retry; + else lprintf("mp3 decode failed with %i after %i retries\n", err, retries); } shared_ctl->mp3_errors++; shared_ctl->mp3_lasterr = err; @@ -113,13 +144,17 @@ static int mp3_decode(void) void mp3_start_local(void) { + // must re-init decoder for new track + if (mp3dec) MP3FreeDecoder(mp3dec); + mp3dec = MP3InitDecoder(); + mp3_buffer_offs = 0; mp3_decode(); } #define mp3_update mp3_update_local -#else +#else // !__GP2X__ static FILE *mp3_current_file = NULL; static int mp3_file_len = 0, mp3_file_pos = 0; @@ -139,16 +174,16 @@ static int mp3_decode(void) fseek(mp3_current_file, mp3_file_pos, SEEK_SET); bytesLeft = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), mp3_current_file); - offset = MP3FindSyncWord(mp3_input_buffer, bytesLeft); + offset = find_sync_word(mp3_input_buffer, bytesLeft); if (offset < 0) { - //lprintf("MP3FindSyncWord (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset); + //lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset); mp3_file_pos = mp3_file_len; return 1; // EOF } readPtr = mp3_input_buffer + offset; bytesLeft -= offset; - err = MP3Decode(mp3dec, &readPtr, &bytesLeft, mp3_out_buffer, 0); + err = MP3Decode(mp3dec, &readPtr, &bytesLeft, cdda_out_buffer, 0); if (err) { //lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err); if (err == ERR_MP3_INDATA_UNDERFLOW) { @@ -180,7 +215,11 @@ void mp3_start_play(FILE *f, int pos) mp3_current_file = NULL; mp3_buffer_offs = 0; - if (!(PicoOpt&0x800) || f == NULL) // cdda disabled or no file? + // must re-init decoder for new track + if (mp3dec) MP3FreeDecoder(mp3dec); + mp3dec = MP3InitDecoder(); + + if (!(PicoOpt&POPT_EN_MCD_CDDA) || f == NULL) // cdda disabled or no file? return; //lprintf("mp3_start_play %p %i\n", f, pos); @@ -204,7 +243,7 @@ int mp3_get_offset(void) unsigned int offs1024 = 0; int cdda_on; - cdda_on = (PicoMCD & 1) && (PicoOpt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) && + cdda_on = (PicoAHW & PAHW_MCD) && (PicoOpt&POPT_EN_MCD_CDDA) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1) && mp3_current_file != NULL; if (cdda_on) { @@ -232,17 +271,17 @@ void mp3_update(int *buffer, int length, int stereo) else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; length_mp3 <<= 2; shr = 2; } if (1152 - mp3_buffer_offs >= length_mp3) { - mix_samples(buffer, mp3_out_buffer + mp3_buffer_offs*2, length<<1); + mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, length<<1); mp3_buffer_offs += length_mp3; } else { int ret, left = 1152 - mp3_buffer_offs; - mix_samples(buffer, mp3_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1); + mix_samples(buffer, cdda_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1); ret = mp3_decode(); if (ret == 0) { mp3_buffer_offs = length_mp3 - left; - mix_samples(buffer + ((left>>shr)<<1), mp3_out_buffer, (mp3_buffer_offs>>shr)<<1); + mix_samples(buffer + ((left>>shr)<<1), cdda_out_buffer, (mp3_buffer_offs>>shr)<<1); } else mp3_buffer_offs = 0; }