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;
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);
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
// 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;
void mp3_start_local(void)
{
+ // must re-init decoder for new track
+ if (mp3dec) MP3FreeDecoder(mp3dec);
+ mp3dec = MP3InitDecoder();
+
mp3_buffer_offs = 0;
mp3_decode();
}
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
}
mp3_current_file = NULL;
mp3_buffer_offs = 0;
+ // 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;
shared_ctl->mp3_len = ftell(f);\r
loaded_mp3 = f;\r
\r
- if (PicoOpt&0x200) {\r
+ if (PicoOpt & POPT_EXT_FM) {\r
// as we are going to change 940's cacheable area, we must invalidate it's cache..\r
if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE);\r
add_job_940(JOB940_INVALIDATE_DCACHE);\r
byte_offs *= pos;\r
byte_offs >>= 6;\r
}\r
- // printf("mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);\r
+ printf(" mp3 pos1024: %i, byte_offs %i/%i\n", pos, byte_offs, shared_ctl->mp3_len);\r
\r
shared_ctl->mp3_offs = byte_offs;\r
\r
mp3_job_started = 0;\r
shared_ctl->mp3_buffsel = 1; // will change to 0 on first decode\r
\r
- if (!(PicoOpt&0x200)) mp3_start_local();\r
+ if (PicoOpt & POPT_EXT_FM)\r
+ {\r
+ add_job_940(JOB940_MP3RESET);\r
+ if (CHECK_BUSY(JOB940_MP3RESET)) wait_busy_940(JOB940_MP3RESET);\r
+ }\r
+ else\r
+ mp3_start_local();\r
}\r
\r
\r
// asm volatile ("mcr p15, 0, r0, c7, c10, 4" ::: "r0"); /* drain write buffer */\r
\r
\r
+static int find_sync_word(unsigned char *buf, int nBytes)\r
+{\r
+ unsigned char *p, *pe;\r
+\r
+ /* find byte-aligned syncword - need 12 (MPEG 1,2) or 11 (MPEG 2.5) matching bits */\r
+ for (p = buf, pe = buf + nBytes - 4; p < pe; p++)\r
+ {\r
+ int pn;\r
+ if (p[0] != 0xff) continue;\r
+ pn = p[1];\r
+ if ((pn & 0xf8) != 0xf8 || // currently must be MPEG1\r
+ (pn & 6) == 0) { // invalid layer\r
+ p++; continue;\r
+ }\r
+ pn = p[2];\r
+ if ((pn & 0xf0) < 0x20 || (pn & 0xf0) == 0xf0 || // bitrates\r
+ (pn & 0x0c) != 0) { // not 44kHz\r
+ continue;\r
+ }\r
+\r
+ return p - buf;\r
+ }\r
+\r
+ return -1;\r
+}\r
+\r
static void mp3_decode(void)\r
{\r
int mp3_offs = shared_ctl->mp3_offs;\r
unsigned char *readPtr = mp3_data + mp3_offs;\r
int bytesLeft = shared_ctl->mp3_len - mp3_offs;\r
int offset; // frame offset from readPtr\r
- int err;\r
+ int retries = 0, err;\r
\r
if (bytesLeft <= 0) return; // EOF, nothing to do\r
\r
- offset = MP3FindSyncWord(readPtr, bytesLeft);\r
+retry:\r
+ offset = find_sync_word(readPtr, bytesLeft);\r
if (offset < 0) {\r
set_if_not_changed(&shared_ctl->mp3_offs, mp3_offs, shared_ctl->mp3_len);\r
return; // EOF\r
// ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*\r
// just try to skip the offending frame..\r
readPtr++;\r
+ bytesLeft--;\r
+ if (retries++ < 2) goto retry;\r
}\r
shared_ctl->mp3_errors++;\r
shared_ctl->mp3_lasterr = err;\r
case JOB940_MP3DECODE:\r
mp3_decode();\r
break;\r
+\r
+ case JOB940_MP3RESET:\r
+ if (shared_data->mp3dec) MP3FreeDecoder(shared_data->mp3dec);\r
+ shared_data->mp3dec = MP3InitDecoder();\r
+ break;\r
}\r
\r
shared_ctl->loopc++;\r