From 69dfeea15942d588fe0e4b18b51cf54cfde5b272 Mon Sep 17 00:00:00 2001 From: notaz Date: Wed, 8 Sep 2010 20:52:13 +0000 Subject: [PATCH] skip junk in mp3_helix, refactor find_sync_word git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@889 be3aeb3a-fb24-0410-a615-afba39da0efa --- common/mp3.c | 29 +++++++++++++++++++ common/mp3.h | 4 ++- common/mp3_helix.c | 70 ++++++++++++++++++++++------------------------ gp2x/940ctl.c | 3 +- gp2x/Makefile | 2 +- linux/io.c | 4 +-- pandora/Makefile | 2 +- pandora/plat.c | 2 +- psp/mp3.c | 8 +++--- win32/plat.c | 4 +-- 10 files changed, 78 insertions(+), 50 deletions(-) create mode 100644 common/mp3.c diff --git a/common/mp3.c b/common/mp3.c new file mode 100644 index 0000000..b00a5fb --- /dev/null +++ b/common/mp3.c @@ -0,0 +1,29 @@ +#include "mp3.h" + +int mp3_find_sync_word(const unsigned char *buf, int size) +{ + const 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 + size - 3; 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; +} + diff --git a/common/mp3.h b/common/mp3.h index 31b8d6f..da0987a 100644 --- a/common/mp3.h +++ b/common/mp3.h @@ -1,6 +1,8 @@ +int mp3_find_sync_word(const unsigned char *buf, int size); + #ifdef __GP2X__ void mp3_update_local(int *buffer, int length, int stereo); -void mp3_start_play_local(FILE *f, int pos); +void mp3_start_play_local(void *f, int pos); #endif diff --git a/common/mp3_helix.c b/common/mp3_helix.c index 4a4931f..75a4e75 100644 --- a/common/mp3_helix.c +++ b/common/mp3_helix.c @@ -23,39 +23,13 @@ static unsigned char mp3_input_buffer[2*1024]; #define mp3_start_play mp3_start_play_local #endif -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 = find_sync_word(buff + offs, 2048 - offs); + offs1 = mp3_find_sync_word(buff + offs, 2048 - offs); if (offs1 < 0) return -2; offs += offs1; if (2048 - offs < 4) return -3; @@ -70,21 +44,24 @@ static int try_get_header(unsigned char *buff, MP3FrameInfo *fi) return ret; } -int mp3_get_bitrate(FILE *f, int len) +int mp3_get_bitrate(void *f_, int len) { unsigned char buff[2048]; MP3FrameInfo fi; + FILE *f = f_; int ret; - memset(buff, 0, 2048); + memset(buff, 0, sizeof(buff)); - if (mp3dec) MP3FreeDecoder(mp3dec); + if (mp3dec) + MP3FreeDecoder(mp3dec); mp3dec = MP3InitDecoder(); fseek(f, 0, SEEK_SET); - ret = fread(buff, 1, 2048, f); + ret = fread(buff, 1, sizeof(buff), f); fseek(f, 0, SEEK_SET); - if (ret <= 0) return -1; + if (ret <= 0) + return -1; ret = try_get_header(buff, &fi); if (ret != 0 || fi.bitrate == 0) { @@ -94,7 +71,8 @@ int mp3_get_bitrate(FILE *f, int len) fseek(f, 0, SEEK_SET); ret = try_get_header(buff, &fi); } - if (ret != 0) return ret; + if (ret != 0) + return ret; // printf("bitrate: %i\n", fi.bitrate / 1000); @@ -117,7 +95,7 @@ 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 = find_sync_word(mp3_input_buffer, bytesLeft); + offset = mp3_find_sync_word(mp3_input_buffer, bytesLeft); if (offset < 0) { lprintf("find_sync_word (%i/%i) err %i\n", mp3_file_pos, mp3_file_len, offset); mp3_file_pos = mp3_file_len; @@ -159,8 +137,10 @@ static int mp3_decode(void) return 0; } -void mp3_start_play(FILE *f, int pos) +void mp3_start_play(void *f_, int pos) { + FILE *f = f_; + mp3_file_len = mp3_file_pos = 0; mp3_current_file = NULL; mp3_buffer_offs = 0; @@ -177,11 +157,27 @@ void mp3_start_play(FILE *f, int pos) fseek(f, 0, SEEK_END); mp3_file_len = ftell(f); + // search for first sync word, skipping stuff like ID3 tags + while (mp3_file_pos < 128*1024) { + int offs, bytes; + + fseek(f, mp3_file_pos, SEEK_SET); + bytes = fread(mp3_input_buffer, 1, sizeof(mp3_input_buffer), f); + if (bytes < 4) + break; + offs = mp3_find_sync_word(mp3_input_buffer, bytes); + if (offs >= 0) { + mp3_file_pos += offs; + break; + } + mp3_file_pos += bytes - 2; + } + // seek.. if (pos) { - unsigned long long pos64 = mp3_file_len; + unsigned long long pos64 = mp3_file_len - mp3_file_pos; pos64 *= pos; - mp3_file_pos = pos64 >> 10; + mp3_file_pos += pos64 >> 10; } mp3_decode(); diff --git a/gp2x/940ctl.c b/gp2x/940ctl.c index afd653f..76986a5 100644 --- a/gp2x/940ctl.c +++ b/gp2x/940ctl.c @@ -475,9 +475,10 @@ void mp3_update(int *buffer, int length, int stereo) } -void mp3_start_play(FILE *f, int pos) // pos is 0-1023 +void mp3_start_play(void *f_, int pos) // pos is 0-1023 { int byte_offs = 0; + FILE *f = f_; if (!(PicoOpt & POPT_EN_MCD_CDDA) || f == NULL) return; diff --git a/gp2x/Makefile b/gp2x/Makefile index 431a357..2e5a104 100644 --- a/gp2x/Makefile +++ b/gp2x/Makefile @@ -57,7 +57,7 @@ OBJS += pico/sound/mix_arm.o # common OBJS += platform/common/emu.o platform/common/menu.o platform/common/fonts.o platform/common/config.o \ platform/common/arm_utils.o platform/common/arm_linux.o platform/common/readpng.o \ - platform/common/mp3_helix.o platform/common/input.o platform/common/main.o \ + platform/common/mp3_helix.o platform/common/input.o platform/common/main.o platform/common/mp3.o \ platform/linux/sndout_oss.o platform/linux/plat.o platform/linux/in_evdev.o # unzip diff --git a/linux/io.c b/linux/io.c index 7fff211..3fc5cc2 100644 --- a/linux/io.c +++ b/linux/io.c @@ -348,12 +348,12 @@ void plat_finish(void) } /* misc */ -int mp3_get_bitrate(FILE *f, int size) +int mp3_get_bitrate(void *f, int size) { return 128; } -void mp3_start_play(FILE *f, int pos) +void mp3_start_play(void *f, int pos) { } diff --git a/pandora/Makefile b/pandora/Makefile index 20d1b23..db3413b 100644 --- a/pandora/Makefile +++ b/pandora/Makefile @@ -48,7 +48,7 @@ OBJS += plat.o asm_utils.o # common OBJS += platform/common/emu.o platform/common/menu.o platform/common/fonts.o platform/common/config.o \ platform/common/arm_utils.o platform/common/mp3_helix.o platform/common/arm_linux.o \ - platform/common/readpng.o platform/common/input.o platform/common/main.o \ + platform/common/readpng.o platform/common/input.o platform/common/main.o platform/common/mp3.o \ platform/linux/fbdev.o platform/linux/in_evdev.o platform/linux/sndout_oss.o \ platform/linux/plat.o platform/linux/oshide.o diff --git a/pandora/plat.c b/pandora/plat.c index db0690c..e857bcc 100644 --- a/pandora/plat.c +++ b/pandora/plat.c @@ -155,7 +155,7 @@ void plat_video_wait_vsync(void) void plat_status_msg_clear(void) { - vout_fbdev_clear_lines(layer_fb, g_screen_height - 8, 8); + vout_fbdev_clear_lines(layer_fb, g_osd_y, 8); } void plat_status_msg_busy_next(const char *msg) diff --git a/psp/mp3.c b/psp/mp3.c index c1e6089..049b9a5 100644 --- a/psp/mp3.c +++ b/psp/mp3.c @@ -300,11 +300,11 @@ static int decode_thread(SceSize args, void *argp) // might be called before initialization -int mp3_get_bitrate(FILE *f, int size) +int mp3_get_bitrate(void *f, int size) { int ret, retval = -1, sample_rate, bitrate; // filenames are stored instead handles in PSP, due to stupid max open file limit - char *fname = (char *)f; + char *fname = f; /* make sure thread is not busy.. */ if (thread_busy_sem >= 0) @@ -351,9 +351,9 @@ end: static int mp3_job_started = 0, mp3_samples_ready = 0, mp3_buffer_offs = 0, mp3_play_bufsel = 0; -void mp3_start_play(FILE *f, int pos) +void mp3_start_play(void *f, int pos) { - char *fname = (char *)f; + char *fname = f; if (!initialized) return; diff --git a/win32/plat.c b/win32/plat.c index 91ef0e0..15aff22 100644 --- a/win32/plat.c +++ b/win32/plat.c @@ -219,12 +219,12 @@ void plat_debug_cat(char *str) } // required by pico -int mp3_get_bitrate(FILE *f, int size) +int mp3_get_bitrate(void *f, int size) { return 128; } -void mp3_start_play(FILE *f, int pos) +void mp3_start_play(void *f, int pos) { } -- 2.39.2