giz rc1 release

git-svn-id: file:///home/notaz/opt/svn/PicoDrive@268 be3aeb3a-fb24-0410-a615-afba39da0efa
This commit is contained in:
notaz 2007-10-04 21:14:45 +00:00
parent a8869ad1e8
commit da42200b24
14 changed files with 328 additions and 194 deletions

View file

@ -42,6 +42,7 @@ typedef enum {
static char *chunk_names[] = {
"INVALID!",
"Saving.. M68K state",
"Saving.. RAM",
"Saving.. VRAM",

View file

@ -262,7 +262,8 @@ PICO_INTERNAL int sound_render(int offset, int length)
}
// CD: CDDA audio
if ((PicoMCD & 1) && (PicoOpt & 0x800))
// CD mode, cdda enabled, not data track, CDC is reading
if ((PicoMCD & 1) && (PicoOpt & 0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1))
mp3_update(buf32, length, stereo);
// convert + limit to normal 16bit output

View file

@ -687,7 +687,8 @@ int emu_SaveLoadGame(int load, int sram)
lprintf("saveLoad (%i, %i): %s\n", load, sram, saveFname);
if(sram) {
if (sram)
{
FILE *sramFile;
int sram_size;
unsigned char *sram_data;
@ -740,10 +741,11 @@ int emu_SaveLoadGame(int load, int sram)
else
{
void *PmovFile = NULL;
if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0) {
if (strcmp(saveFname + strlen(saveFname) - 3, ".gz") == 0)
{
if( (PmovFile = gzopen(saveFname, load ? "rb" : "wb")) ) {
emu_setSaveStateCbs(1);
if(!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);
if (!load) gzsetparams(PmovFile, 9, Z_DEFAULT_STRATEGY);
}
}
else

View file

@ -1,4 +1,6 @@
#ifdef __GP2X__
void mp3_update_local(int *buffer, int length, int stereo);
void mp3_start_local(void);
#endif

251
platform/common/mp3_helix.c Normal file
View file

@ -0,0 +1,251 @@
// Some mp3 related code for Sega/Mega CD.
// Uses the Helix Fixed-point MP3 decoder
// (c) Copyright 2007, Grazvydas "notaz" Ignotas
#include <stdio.h>
#include <string.h>
#include "../../Pico/PicoInt.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 try_get_header(unsigned char *buff, MP3FrameInfo *fi)
{
int ret, offs1, offs = 0;
while (1)
{
offs1 = MP3FindSyncWord(buff + offs, 2048 - offs);
if (offs1 < 0) return -2;
offs += offs1;
if (2048 - offs < 4) return -3;
// printf("trying header %08x\n", *(int *)(buff + offs));
ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
if (ret == 0 && fi->bitrate != 0) break;
offs++;
}
return ret;
}
int mp3_get_bitrate(FILE *f, int len)
{
unsigned char buff[2048];
MP3FrameInfo fi;
int ret;
memset(buff, 0, 2048);
if (!mp3dec) mp3dec = MP3InitDecoder();
fseek(f, 0, SEEK_SET);
ret = fread(buff, 1, 2048, f);
fseek(f, 0, SEEK_SET);
if (ret <= 0) return -1;
ret = try_get_header(buff, &fi);
if (ret != 0 || fi.bitrate == 0) {
// try to read somewhere around the middle
fseek(f, len>>1, SEEK_SET);
fread(buff, 1, 2048, f);
fseek(f, 0, SEEK_SET);
ret = try_get_header(buff, &fi);
}
if (ret != 0) return ret;
// printf("bitrate: %i\n", fi.bitrate / 1000);
return fi.bitrate / 1000;
}
#ifdef __GP2X__
#include "../gp2x/code940/940shared.h"
extern _940_ctl_t *shared_ctl;
extern unsigned char *mp3_mem;
static int mp3_decode(void)
{
// tried copying this to cached mem, no improvement noticed
int mp3_offs = shared_ctl->mp3_offs;
unsigned char *readPtr = mp3_mem + mp3_offs;
int bytesLeft = shared_ctl->mp3_len - mp3_offs;
int offset; // frame offset from readPtr
int err;
if (bytesLeft <= 0) return 1; // EOF, nothing to do
offset = MP3FindSyncWord(readPtr, bytesLeft);
if (offset < 0) {
shared_ctl->mp3_offs = shared_ctl->mp3_len;
return 1; // EOF
}
readPtr += offset;
bytesLeft -= offset;
err = MP3Decode(mp3dec, &readPtr, &bytesLeft, mp3_out_buffer, 0);
if (err) {
if (err == ERR_MP3_INDATA_UNDERFLOW) {
shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF
return 1;
} else if (err <= -6 && err >= -12) {
// ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
// just try to skip the offending frame..
readPtr++;
}
shared_ctl->mp3_errors++;
shared_ctl->mp3_lasterr = err;
}
shared_ctl->mp3_offs = readPtr - mp3_mem;
return 0;
}
void mp3_start_local(void)
{
mp3_buffer_offs = 0;
mp3_decode();
}
#define mp3_update mp3_update_local
#else
static FILE *mp3_current_file = NULL;
static int mp3_file_len = 0, mp3_file_pos = 0;
static unsigned char mp3_input_buffer[2*1024];
static int mp3_decode(void)
{
unsigned char *readPtr;
int bytesLeft;
int offset; // mp3 frame offset from readPtr
int err;
do
{
if (mp3_file_pos >= mp3_file_len) return 1; // EOF, nothing to do
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);
if (offset < 0) {
//lprintf("MP3FindSyncWord (%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);
if (err) {
//lprintf("MP3Decode err (%i/%i) %i\n", mp3_file_pos, mp3_file_len, err);
if (err == ERR_MP3_INDATA_UNDERFLOW) {
if (offset == 0)
// something's really wrong here, frame had to fit
mp3_file_pos = mp3_file_len;
else
mp3_file_pos += offset;
continue;
} else if (err <= -6 && err >= -12) {
// ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
// just try to skip the offending frame..
mp3_file_pos += offset + 1;
continue;
}
mp3_file_pos = mp3_file_len;
return 1;
}
mp3_file_pos += readPtr - mp3_input_buffer;
}
while (0);
return 0;
}
void mp3_start_play(FILE *f, int pos)
{
mp3_file_len = mp3_file_pos = 0;
mp3_current_file = NULL;
mp3_buffer_offs = 0;
if (!(PicoOpt&0x800) || f == NULL) // cdda disabled or no file?
return;
//lprintf("mp3_start_play %p %i\n", f, pos);
mp3_current_file = f;
fseek(f, 0, SEEK_END);
mp3_file_len = ftell(f);
// seek..
if (pos) {
mp3_file_pos = (mp3_file_len << 6) >> 10;
mp3_file_pos *= pos;
mp3_file_pos >>= 6;
}
mp3_decode();
}
int mp3_get_offset(void)
{
unsigned int offs1024 = 0;
int cdda_on;
cdda_on = (PicoMCD & 1) && (PicoOpt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) &&
(Pico_mcd->scd.Status_CDC & 1) && mp3_current_file != NULL;
if (cdda_on) {
offs1024 = mp3_file_pos << 7;
offs1024 /= mp3_file_len >> 3;
}
//lprintf("mp3_get_offset offs1024=%u (%i/%i)\n", offs1024, mp3_file_pos, mp3_file_len);
return offs1024;
}
#endif // ifndef __GP2X__
void mp3_update(int *buffer, int length, int stereo)
{
int length_mp3, shr = 0;
void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
#ifndef __GP2X__
if (mp3_current_file == NULL || mp3_file_pos >= mp3_file_len) return; // no file / EOF
#endif
length_mp3 = length;
if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; length_mp3 <<= 1; shr = 1; }
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);
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);
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);
} else
mp3_buffer_offs = 0;
}
}

View file

@ -1,6 +1,6 @@
# you may or may not need to change this
CROSS = /opt/cegcc/arm-wince-cegcc/bin/
export CROSS = /opt/cegcc/arm-wince-cegcc/bin/
# settings
asm_memory = 1
@ -38,7 +38,8 @@ LD = $(CROSS)ld
OBJS += main.o emu.o menu.o giz.o asm_utils.o
# common
OBJS += ../common/emu.o ../common/menu.o ../common/fonts.o ../common/arm_utils.o ../common/readpng.o
OBJS += ../common/emu.o ../common/menu.o ../common/fonts.o ../common/arm_utils.o \
../common/readpng.o ../common/mp3_helix.o
# Pico
ifeq "$(amalgamate)" "1"
@ -91,8 +92,6 @@ OBJS += ../../zlib/gzio.o ../../zlib/inffast.o ../../zlib/inflate.o ../../zlib/i
../../zlib/deflate.o ../../zlib/crc32.o ../../zlib/adler32.o ../../zlib/zutil.o ../../zlib/compress.o
# unzip
OBJS += ../../unzip/unzip.o ../../unzip/unzip_stream.o
# mp3
OBJS += mp3.o
# CPU cores
# Cyclone
DEFINC += -DEMU_C68K
@ -104,7 +103,7 @@ OBJS += ../../cpu/DrZ80/drz80.o
all: PicoDrive.exe
PicoDrive.exe : $(OBJS)
PicoDrive.exe : $(OBJS) ../common/helix/helix_mp3.a
@echo ">>>" $@
$(GCC) -o $@ -static $(COPT) $^ -lm -lpng -Lkgsdk/ -lKGSDK -Wl,-Map=PicoDrive.map 2>&1 | \
grep -v ".idata$$4" # | grep -v "supports interworking, whereas"
@ -153,6 +152,9 @@ endif
@echo building Cyclone...
@make -C ../../cpu/Cyclone/proj CONFIG_FILE=config_pico.h
# build helix libs
../common/helix/helix_mp3.a:
make -C ../common/helix
# cleanup

View file

@ -73,6 +73,8 @@ static void emu_state_cb(const char *str)
Framework2D_UnlockBuffer();
giz_screen = NULL;
Sleep(0); /* yield the CPU, the system may need it */
}
static void emu_msg_tray_open(void)
@ -299,7 +301,7 @@ static void vidResetMode(void)
giz_screen = NULL;
}
/*
#include <stdarg.h>
static void stdbg(const char *fmt, ...)
{
@ -313,7 +315,7 @@ static void stdbg(const char *fmt, ...)
noticeMsgTime = GetTickCount();
}
*/
static void updateSound(int len)
{
@ -323,8 +325,8 @@ static void updateSound(int len)
PsndOut += len;
if (PsndOut - snd_cbuff >= snd_cbuf_samples)
{
if (PsndOut - snd_cbuff != snd_cbuf_samples)
stdbg("snd diff is %i, not %i", PsndOut - snd_cbuff, snd_cbuf_samples);
//if (PsndOut - snd_cbuff != snd_cbuf_samples)
// stdbg("snd diff is %i, not %i", PsndOut - snd_cbuff, snd_cbuf_samples);
PsndOut = snd_cbuff;
}
}
@ -365,31 +367,43 @@ void emu_forcedFrame(void)
static void RunEvents(unsigned int which)
{
if (which & 0x1800) { // save or load (but not both)
if (which & 0x1800) // save or load (but not both)
{
int do_it = 1;
if (PsndOut != NULL)
FrameworkAudio_SetPause(1);
if (giz_screen == NULL)
giz_screen = Framework2D_LockBuffer();
if ( emu_checkSaveFile(state_slot) &&
(( (which & 0x1000) && (currentConfig.EmuOpt & 0x800)) || // load
(!(which & 0x1000) && (currentConfig.EmuOpt & 0x200))) ) // save
{
int keys;
if (giz_screen == NULL)
giz_screen = Framework2D_LockBuffer();
blit("", (which & 0x1000) ? "LOAD STATE? (PLAY=yes, STOP=no)" : "OVERWRITE SAVE? (PLAY=yes, STOP=no)");
while( !((keys = Framework_PollGetButtons()) & (BTN_PLAY|BTN_STOP)) )
Sleep(50);
if (keys & BTN_STOP) do_it = 0;
while( ((keys = Framework_PollGetButtons()) & (BTN_PLAY|BTN_STOP)) ) // wait for release
Sleep(50);
clearArea(0);
}
if (do_it) {
if (do_it)
{
osd_text(4, 232, (which & 0x1000) ? "LOADING GAME" : "SAVING GAME");
PicoStateProgressCB = emu_state_cb;
emu_SaveLoadGame((which & 0x1000) >> 12, 0);
PicoStateProgressCB = NULL;
Sleep(0);
}
if (PsndOut != NULL)
FrameworkAudio_SetPause(0);
reset_timing = 1;
}
if (which & 0x0400) { // switch renderer
if (which & 0x0400) // switch renderer
{
if (PicoOpt&0x10) { PicoOpt&=~0x10; currentConfig.EmuOpt |= 0x80; }
else { PicoOpt|= 0x10; currentConfig.EmuOpt &= ~0x80; }
@ -405,7 +419,8 @@ static void RunEvents(unsigned int which)
noticeMsgTime = GetTickCount();
}
if (which & 0x0300) {
if (which & 0x0300)
{
if(which&0x0200) {
state_slot -= 1;
if(state_slot < 0) state_slot = 9;
@ -560,10 +575,11 @@ void emu_Loop(void)
PsndOut = NULL;
if (currentConfig.EmuOpt & 4)
{
int ret, snd_excess_add, stereo=(PicoOpt&8)>>3;
int ret, snd_excess_add, stereo;
if (PsndRate != PsndRate_old || (PicoOpt&0x0b) != (PicoOpt_old&0x0b) || Pico.m.pal != pal_old) {
sound_rerate(Pico.m.frame_count ? 1 : 0);
}
stereo=(PicoOpt&8)>>3;
snd_excess_add = ((PsndRate - PsndLen*target_fps)<<16) / target_fps;
snd_cbuf_samples = (PsndRate<<stereo) * 16 / target_fps;
lprintf("starting audio: %i len: %i (ex: %04x) stereo: %i, pal: %i\n",
@ -593,7 +609,7 @@ void emu_Loop(void)
tval = GetTickCount();
if (reset_timing || tval < tval_prev) {
stdbg("timing reset");
//stdbg("timing reset");
reset_timing = 0;
tval_thissec = tval;
frames_shown = frames_done = 0;
@ -665,7 +681,7 @@ void emu_Loop(void)
audio_skew_prev = audio_skew;
target_frametime += adj;
sec_ms = (target_frametime * target_fps) >> 8;
stdbg("%i %i %i", audio_skew, adj, sec_ms);
//stdbg("%i %i %i", audio_skew, adj, sec_ms);
frames_done = frames_shown = 0;
}
else if (currentConfig.Frameskip < 0) {

View file

@ -1257,11 +1257,18 @@ static int menu_loop_options(void)
}
break;
case MA_OPT_SOUND_QUALITY:
if ((inp & BTN_RIGHT) && currentConfig.PsndRate == 44100 && !(currentConfig.PicoOpt&0x08)) {
currentConfig.PsndRate = 11025; currentConfig.PicoOpt|= 0x08;
} else if ((inp & BTN_LEFT) && currentConfig.PsndRate == 11025 && (currentConfig.PicoOpt&0x08)) {
currentConfig.PsndRate = 44100; currentConfig.PicoOpt&=~0x08;
} else currentConfig.PsndRate = sndrate_prevnext(currentConfig.PsndRate, inp & BTN_RIGHT);
if ((inp & BTN_RIGHT) && currentConfig.PsndRate == 44100 &&
!(currentConfig.PicoOpt&0x08))
{
currentConfig.PsndRate = 11025;
currentConfig.PicoOpt |= 8;
} else if ((inp & BTN_LEFT) && currentConfig.PsndRate == 11025 &&
(currentConfig.PicoOpt&0x08) && !(PicoMCD&1))
{
currentConfig.PsndRate = 44100;
currentConfig.PicoOpt &= ~8;
} else
currentConfig.PsndRate = sndrate_prevnext(currentConfig.PsndRate, inp & BTN_RIGHT);
break;
case MA_OPT_REGION:
region_prevnext(inp & BTN_RIGHT);
@ -1347,7 +1354,7 @@ static void draw_menu_credits(void)
menu_draw_begin(1);
text_out16(tl_x, 20, "PicoDrive v" VERSION " (c) notaz, 2006,2007");
text_out16(tl_x, 30, "beta1");
text_out16(tl_x, 30, "rc1");
y = tl_y;
text_out16(tl_x, y, "Credits:");
text_out16(tl_x, (y+=10), "Dave: Cyclone 68000 core,");

View file

@ -1,8 +0,0 @@
#include <stdio.h>
int mp3_get_bitrate(FILE *f, int size){ return 1; }
void mp3_start_play(FILE *f, int pos){}
int mp3_get_offset(void){ return 0; } // 0-1023
void mp3_update(int *buffer, int length, int stereo){}

View file

@ -14,7 +14,7 @@
#include "gp2x.h"
#include "emu.h"
#include "menu.h"
#include "mp3.h"
#include "../common/mp3.h"
#include "../common/arm_utils.h"
#include "../common/menu.h"
#include "../common/emu.h"
@ -533,9 +533,8 @@ void mp3_update(int *buffer, int length, int stereo)
int length_mp3;
int cdda_on;
// not data track, CDC is reading, playback was started, track not ended
cdda_on = !(Pico_mcd->s68k_regs[0x36] & 1) && (Pico_mcd->scd.Status_CDC & 1) &&
loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len;
// playback was started, track not ended
cdda_on = loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len;
if (!cdda_on) return;
@ -649,7 +648,7 @@ void mp3_start_play(FILE *f, int pos) // pos is 0-1023
int mp3_get_offset(void)
{
int offs1024 = 0;
unsigned int offs1024 = 0;
int cdda_on;
cdda_on = (PicoMCD & 1) && (PicoOpt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) &&
@ -657,10 +656,9 @@ int mp3_get_offset(void)
if (cdda_on) {
offs1024 = shared_ctl->mp3_offs << 7;
offs1024 /= shared_ctl->mp3_len;
offs1024 <<= 3;
offs1024 /= shared_ctl->mp3_len >> 3;
}
printf("offs1024=%i (%i/%i)\n", offs1024, shared_ctl->mp3_offs, shared_ctl->mp3_len);
printf("offs1024=%u (%i/%i)\n", offs1024, shared_ctl->mp3_offs, shared_ctl->mp3_len);
return offs1024;
}

View file

@ -1,7 +1,7 @@
# you may or may not need to change this
#devkit_path = x:/stuff/dev/devkitgp2x/
CROSS = arm-linux-
export CROSS = arm-linux-
#CROSS = $(devkit_path)bin/arm-linux-
# settings
@ -58,7 +58,8 @@ OBJS += main.o menu.o gp2x.o usbjoy.o emu.o squidgehack.o cpuctrl.o
OBJS += 940ctl.o
# common
OBJS += ../common/emu.o ../common/menu.o ../common/fonts.o ../common/arm_utils.o ../common/readpng.o
OBJS += ../common/emu.o ../common/menu.o ../common/fonts.o ../common/arm_utils.o \
../common/readpng.o ../common/mp3_helix.o
# Pico
ifeq "$(amalgamate)" "1"
@ -111,8 +112,6 @@ OBJS += ../../zlib/gzio.o ../../zlib/inffast.o ../../zlib/inflate.o ../../zlib/i
../../zlib/deflate.o ../../zlib/crc32.o ../../zlib/adler32.o ../../zlib/zutil.o ../../zlib/compress.o
# unzip
OBJS += ../../unzip/unzip.o ../../unzip/unzip_stream.o
# mp3
OBJS += mp3.o
# debug
ifeq "$(debug_cyclone)" "1"
OBJS += ../../Pico/_cyclone_debug.o ../../cpu/musashi/m68kdasm.o
@ -138,7 +137,7 @@ endif
all: PicoDrive.gpe
PicoDrive.gpe : $(OBJS) helix/helix_mp3.a
PicoDrive.gpe : $(OBJS) ../common/helix/helix_mp3.a
@echo $@
@$(GCC) -o $@ $(COPT) $^ -lm -lpng -Wl,-Map=PicoDrive.map
ifeq ($(DEBUG),)
@ -206,8 +205,8 @@ testrefr.gpe : test.o gp2x.o
# build helix libs
helix/helix_mp3.a:
make -C helix
../common/helix/helix_mp3.a:
make -C ../common/helix
# cleanup

View file

@ -1,5 +1,5 @@
#include "../../../Pico/sound/ym2612.h"
#include "../helix/pub/mp3dec.h"
#include "../../common/helix/pub/mp3dec.h"
// max 16 jobs
enum _940_job_t {

View file

@ -3,7 +3,7 @@
#devkit_path = x:/stuff/dev/devkitgp2x/
devkit_path = /usr/local/devkitPro/devkitGP2X/
lgcc_path = $(devkit_path)lib/gcc/arm-linux/4.0.3/
CROSS = arm-linux-
export CROSS = arm-linux-
#CROSS = $(devkit_path)bin/arm-linux-
# settings
@ -47,7 +47,7 @@ $(BIN) : code940.gpe
@echo $@
@$(OBJCOPY) -O binary $< $@
code940.gpe : $(OBJS940) ../helix/helix_mp3.a
code940.gpe : $(OBJS940) ../../common/helix/helix_mp3.a
@echo $@
@$(LD) -static -e code940 -Ttext 0x0 $^ -L$(lgcc_path) -lgcc -o $@ -Map code940.map
@ -58,12 +58,12 @@ code940.gpe : $(OBJS940) ../helix/helix_mp3.a
mix.o : ../../../Pico/sound/mix.s
@echo $@
@$(GCC) $(COPT) $(DEFINC) -DEXTERNAL_YM2612 -c $< -o $@
misc.o : ../../../Pico/misc.s
misc.o : ../../../Pico/Misc.s
@echo $@
@$(GCC) $(COPT) $(DEFINC) -DEXTERNAL_YM2612 -c $< -o $@
../helix/helix_mp3.a:
@make -C ../helix/
../../common/helix/helix_mp3.a:
@make -C ../../common/helix/
up: $(BIN)

View file

@ -1,137 +0,0 @@
// Some mp3 related code for Sega/Mega CD
// (c) Copyright 2007, Grazvydas "notaz" Ignotas
#include <stdio.h>
#include <string.h>
#include "../../Pico/sound/mix.h"
#include "code940/940shared.h"
#include "helix/pub/mp3dec.h"
static short mp3_out_buffer[2*1152];
static HMP3Decoder mp3dec = 0;
static int mp3_buffer_offs = 0;
extern _940_ctl_t *shared_ctl;
extern unsigned char *mp3_mem;
extern int PsndRate;
static int try_get_header(unsigned char *buff, MP3FrameInfo *fi)
{
int ret, offs1, offs = 0;
while (1)
{
offs1 = MP3FindSyncWord(buff + offs, 2048 - offs);
if (offs1 < 0) return -2;
offs += offs1;
if (2048 - offs < 4) return -3;
// printf("trying header %08x\n", *(int *)(buff + offs));
ret = MP3GetNextFrameInfo(mp3dec, fi, buff + offs);
if (ret == 0 && fi->bitrate != 0) break;
offs++;
}
return ret;
}
int mp3_get_bitrate(FILE *f, int len)
{
unsigned char buff[2048];
MP3FrameInfo fi;
int ret;
memset(buff, 0, 2048);
if (!mp3dec) mp3dec = MP3InitDecoder();
fseek(f, 0, SEEK_SET);
ret = fread(buff, 1, 2048, f);
fseek(f, 0, SEEK_SET);
if (ret <= 0) return -1;
ret = try_get_header(buff, &fi);
if (ret != 0 || fi.bitrate == 0) {
// try to read somewhere around the middle
fseek(f, len>>1, SEEK_SET);
fread(buff, 1, 2048, f);
fseek(f, 0, SEEK_SET);
ret = try_get_header(buff, &fi);
}
if (ret != 0) return ret;
// printf("bitrate: %i\n", fi.bitrate / 1000);
return fi.bitrate / 1000;
}
static void mp3_decode(void)
{
// tried copying this to cached mem, no improvement noticed
int mp3_offs = shared_ctl->mp3_offs;
unsigned char *readPtr = mp3_mem + mp3_offs;
int bytesLeft = shared_ctl->mp3_len - mp3_offs;
int offset; // frame offset from readPtr
int err;
if (bytesLeft <= 0) return; // EOF, nothing to do
offset = MP3FindSyncWord(readPtr, bytesLeft);
if (offset < 0) {
shared_ctl->mp3_offs = shared_ctl->mp3_len;
return; // EOF
}
readPtr += offset;
bytesLeft -= offset;
err = MP3Decode(mp3dec, &readPtr, &bytesLeft, mp3_out_buffer, 0);
if (err) {
if (err == ERR_MP3_INDATA_UNDERFLOW) {
shared_ctl->mp3_offs = shared_ctl->mp3_len; // EOF
return;
} else if (err <= -6 && err >= -12) {
// ERR_MP3_INVALID_FRAMEHEADER, ERR_MP3_INVALID_*
// just try to skip the offending frame..
readPtr++;
}
shared_ctl->mp3_errors++;
shared_ctl->mp3_lasterr = err;
}
shared_ctl->mp3_offs = readPtr - mp3_mem;
}
void mp3_update_local(int *buffer, int length, int stereo)
{
int length_mp3, shr = 0;
void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32;
length_mp3 = length;
if (PsndRate == 22050) { mix_samples = mix_16h_to_32_s1; length_mp3 <<= 1; shr = 1; }
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);
mp3_buffer_offs += length_mp3;
} else {
int left = 1152 - mp3_buffer_offs;
mix_samples(buffer, mp3_out_buffer + mp3_buffer_offs*2, (left>>shr)<<1);
mp3_decode();
mp3_buffer_offs = length_mp3 - left;
mix_samples(buffer + ((left>>shr)<<1), mp3_out_buffer, (mp3_buffer_offs>>shr)<<1);
}
}
void mp3_start_local(void)
{
mp3_buffer_offs = 0;
mp3_decode();
}