From edbb82f88e8dcfd8f4afc8119f04bb863304283b Mon Sep 17 00:00:00 2001 From: notaz Date: Thu, 8 Feb 2007 20:12:45 +0000 Subject: [PATCH] some audio problems fixed, missing files added git-svn-id: file:///home/notaz/opt/svn/PicoDrive/platform@31 be3aeb3a-fb24-0410-a615-afba39da0efa --- gp2x/940ctl_ym2612.c | 241 ++++++++++++++++++++----------------------- gp2x/mp3.c | 134 ++++++++++++++++++++++++ gp2x/mp3.h | 4 + 3 files changed, 250 insertions(+), 129 deletions(-) create mode 100644 gp2x/mp3.c create mode 100644 gp2x/mp3.h diff --git a/gp2x/940ctl_ym2612.c b/gp2x/940ctl_ym2612.c index 29fd8bf..1059b5d 100644 --- a/gp2x/940ctl_ym2612.c +++ b/gp2x/940ctl_ym2612.c @@ -12,9 +12,8 @@ #include "emu.h" #include "menu.h" #include "asmutils.h" +#include "mp3.h" #include "../../Pico/PicoInt.h" - -// tmp #include "../../Pico/sound/mix.h" /* we will need some gp2x internals here */ @@ -23,8 +22,8 @@ extern volatile unsigned long *gp2x_memregl; static unsigned char *shared_mem = 0; static _940_data_t *shared_data = 0; -static _940_ctl_t *shared_ctl = 0; -static unsigned char *mp3_mem = 0; +_940_ctl_t *shared_ctl = 0; +unsigned char *mp3_mem = 0; #define MP3_SIZE_MAX (0x1000000 - 4*640*480) @@ -226,26 +225,17 @@ int YM2612PicoTick_940(int n) return 0; } -static int g_busy = 10; -static void wait_busy_940(void) +#define CHECK_BUSY(job) \ + (gp2x_memregs[0x3b46>>1] & (1<<(job-1))) + +static void wait_busy_940(int job) { int i; -#if 0 - printf("940 busy, entering wait loop.. (cnt: %i, wc: %i, ve: ", shared_ctl->loopc, shared_ctl->waitc); - for (i = 0; i < 8; i++) - printf("%i ", shared_ctl->vstarts[i]); - printf(")\n"); - for (i = 0; shared_ctl->busy; i++) - { - spend_cycles(1024); /* needs tuning */ - } - printf("wait iterations: %i\n", i); -#else - for (i = 0; /*shared_ctl->busy*/gp2x_memregs[0x3B3E>>1] && i < 0x10000; i++) + job--; + for (i = 0; (gp2x_memregs[0x3b46>>1] & (1<vstarts[i]); printf(")\n"); - printf("irq pending flags: DUALCPU %04x (see 15?), SRCPND %08lx (see 26), INTPND %08lx\n", + printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n", gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]); - printf("last irq PC: %08x, lastjob: 0x%03x, busy: %x, lastbusy: %x, g_busy: %x\n", shared_ctl->last_irq_pc, - shared_ctl->lastjob, gp2x_memregs[0x3B3E>>1]/*shared_ctl->busy*/, shared_ctl->lastbusy, g_busy); + printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob); printf("trying to interrupt..\n"); gp2x_memregs[0x3B3E>>1] = 0xffff; - for (i = 0; /*shared_ctl->busy*/gp2x_memregs[0x3B3E>>1] && i < 0x10000; i++) + for (i = 0; gp2x_memregs[0x3b46>>1] && i < 0x10000; i++) spend_cycles(8*1024); printf("i = 0x%x\n", i); - printf("irq pending flags: DUALCPU %04x (see 15?), SRCPND %08lx (see 26), INTPND %08lx\n", + printf("irq pending flags: DUALCPU %04x, SRCPND %08lx (see 26), INTPND %08lx\n", gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2]); - printf("last irq PC: %08x, lastjob: 0x%03x, busy: %x\n", shared_ctl->last_irq_pc, shared_ctl->lastjob, gp2x_memregs[0x3B3E>>1]/*shared_ctl->busy*/); + printf("last lr: %08x, lastjob: %i\n", shared_ctl->last_lr, shared_ctl->lastjob); strcpy(menuErrorMsg, "940 crashed."); engineState = PGS_Menu; crashed_940 = 1; -#endif } -static void add_job_940(int job0, int job1) +static void add_job_940(int job) { -/* if (gp2x_memregs[0x3b46>>1] || shared_ctl->busy) - { - printf("!!add_job_940: irq pending flags: DUALCPU %04x (see 15?), SRCPND %08lx (see 26), INTPND %08lx, busy: %x\n", - gp2x_memregs[0x3b46>>1], gp2x_memregl[0x4500>>2], gp2x_memregl[0x4510>>2], shared_ctl->busy); + if (job <= 0 || job > 16) { + printf("add_job_940: bad job: %i\n", job); + return; } -*/ - shared_ctl->jobs[0] = job0; - shared_ctl->jobs[1] = job1; - //shared_ctl->busy = ++g_busy; // 1; - gp2x_memregs[0x3B3E>>1] = ++g_busy; // set busy flag -// gp2x_memregs[0x3B3E>>1] = 0xffff; // cause interrupt + + // generate interrupt for this job + job--; + gp2x_memregs[(0x3B20+job*2)>>1] = 1; + +// printf("added %i, pending %04x\n", job+1, gp2x_memregs[0x3b46>>1]); } @@ -293,8 +280,6 @@ void YM2612PicoStateLoad_940(void) { int i, old_A1 = addr_A1; - if (/*shared_ctl->busy*/gp2x_memregs[0x3B3E>>1]) wait_busy_940(); - // feed all the registers and update internal state for(i = 0; i < 0x100; i++) { YM2612Write_940(0, i); @@ -307,7 +292,7 @@ void YM2612PicoStateLoad_940(void) addr_A1 = old_A1; - add_job_940(JOB940_PICOSTATELOAD, 0); + add_job_940(JOB940_PICOSTATELOAD); } @@ -338,6 +323,11 @@ void YM2612Init_940(int baseclock, int rate) gp2x_memregs[0x3B40>>1] = 0; // disable DUALCPU interrupts for 920 gp2x_memregs[0x3B42>>1] = 1; // enable DUALCPU interrupts for 940 + gp2x_memregl[0x4504>>2] = 0; // make sure no FIQs will be generated + gp2x_memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller + + + if (shared_mem == NULL) { shared_mem = (unsigned char *) mmap(0, 0x210000, PROT_READ|PROT_WRITE, MAP_SHARED, memdev, 0x2000000); @@ -406,24 +396,20 @@ void YM2612Init_940(int baseclock, int rate) loaded_mp3 = 0; - /* now cause 940 to init it's ym2612 stuff */ - shared_ctl->baseclock = baseclock; - shared_ctl->rate = rate; - shared_ctl->jobs[0] = JOB940_INITALL; - shared_ctl->jobs[1] = 0; - //shared_ctl->busy = 1; - gp2x_memregs[0x3B3E>>1] = 1; // set busy flag - gp2x_memregs[0x3B46>>1] = 0xffff; // clear pending DUALCPU interrupts for 940 - gp2x_memregl[0x4500>>2] = 0; // clear pending IRQs in SRCPND - gp2x_memregl[0x4510>>2] = 0; // clear pending IRQs in INTPND - gp2x_memregl[0x4508>>2] = ~(1<<26); // unmask DUALCPU ints in the undocumented 940's interrupt controller + gp2x_memregl[0x4500>>2] = 0xffffffff; // clear pending IRQs in SRCPND + gp2x_memregl[0x4510>>2] = 0xffffffff; // clear pending IRQs in INTPND /* start the 940 */ Reset940(0, 2); Pause940(0); // YM2612ResetChip_940(); // will be done on JOB940_YM2612INIT + + /* now cause 940 to init it's ym2612 stuff */ + shared_ctl->baseclock = baseclock; + shared_ctl->rate = rate; + add_job_940(JOB940_INITALL); } @@ -435,72 +421,85 @@ void YM2612ResetChip_940(void) return; } - if (/*shared_ctl->busy*/gp2x_memregs[0x3B3E>>1]) wait_busy_940(); - internal_reset(); - add_job_940(JOB940_YM2612RESETCHIP, 0); + add_job_940(JOB940_YM2612RESETCHIP); } -//extern int pcm_buffer[2*44100/50]; -/* -static void mix_samples(int *dest_buf, short *mp3_buf, int len, int stereo) +int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty) { -// int *pcm = pcm_buffer + offset * 2; + int *ym_buf = shared_data->ym_buffer; - if (stereo) - { - for (; len > 0; len--) - { - int lm, rm; - lm = *mp3_buf++; rm = *mp3_buf++; - *dest_buf++ += lm - lm/2; *dest_buf++ += rm - rm/2; - } + //printf("YM2612UpdateOne_940()\n"); + + if (CHECK_BUSY(JOB940_YM2612UPDATEONE)) wait_busy_940(JOB940_YM2612UPDATEONE); + + // mix in ym buffer + if (is_buf_empty) memcpy32(buffer, ym_buf, length< 0; len--) +// { +// *dest_buf++ += *ym_buf++; +// } + + if (shared_ctl->writebuffsel == 1) { + shared_ctl->writebuff0[writebuff_ptr] = 0xffff; } else { - for (; len > 0; len--) - { - int l = *mp3_buf++; - *dest_buf++ = l - l/2; - } + shared_ctl->writebuff1[writebuff_ptr] = 0xffff; } + writebuff_ptr = 0; + + /* predict sample counter for next frame */ + if (PsndLen_exc_add) { + if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length = PsndLen + 1; + else length = PsndLen; + } + + /* give 940 ym job */ + shared_ctl->writebuffsel ^= 1; + shared_ctl->length = length; + shared_ctl->stereo = stereo; + + add_job_940(JOB940_YM2612UPDATEONE); + + return 1; } -*/ -// here we assume that length is different between games, but constant in one game static int mp3_samples_ready = 0, mp3_buffer_offs = 0; -static int mp3_play_bufsel = 0; +static int mp3_play_bufsel = 0, mp3_job_started = 0; -int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty) +void mp3_update(int *buffer, int length, int stereo) { - int length_mp3 = Pico.m.pal ? 44100/50 : 44100/60; // mp3s are locked to 44100Hz stereo - int *ym_buf = shared_data->mix_buffer; -// int *dest_buf = buffer; - int cdda_on, mp3_job = 0; -// int len; + int length_mp3; + int cdda_on; - // emulating CD, enabled in opts, not data track, CDC is reading, playback was started, track not ended - cdda_on = (PicoMCD & 1) && (PicoOpt & 0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) && - (Pico_mcd->scd.Status_CDC & 1) && loaded_mp3; + // 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; - //printf("YM2612UpdateOne_940()\n"); - if (/*shared_ctl->busy*/gp2x_memregs[0x3B3E>>1]) wait_busy_940(); + if (!cdda_on) return; - // track ended? - cdda_on = cdda_on && shared_ctl->mp3_offs < shared_ctl->mp3_len; + if (!(PicoOpt&0x200)) { + mp3_update_local(buffer, length, stereo); + return; + } - // mix in ym buffer - if (is_buf_empty) memcpy32(buffer, ym_buf, length< 0; len--) -// { -// *dest_buf++ += *ym_buf++; -// } + /* do we have to wait? */ + if (mp3_job_started && mp3_samples_ready < length_mp3) { + if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE); + mp3_job_started = 0; + mp3_samples_ready += 1152; + } /* mix mp3 data, only stereo */ - if (cdda_on && mp3_samples_ready >= length_mp3) + if (mp3_samples_ready >= length_mp3) { int shr = 0; void (*mix_samples)(int *dest_buf, short *mp3_buf, int count) = mix_16h_to_32; @@ -508,15 +507,15 @@ int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty) else if (PsndRate == 11025) { mix_samples = mix_16h_to_32_s2; shr = 2; } if (1152 - mp3_buffer_offs >= length_mp3) { - mix_samples(buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, (length_mp3>>shr)<<1); + mix_samples(buffer, shared_data->mp3_buffer[mp3_play_bufsel] + mp3_buffer_offs*2, length<<1); mp3_buffer_offs += length_mp3; } else { - // collect from both buffers.. + // collect samples from both buffers.. int left = 1152 - mp3_buffer_offs; if (mp3_play_bufsel == 0) { - mix_samples(buffer, shared_data->mp3_buffer[0] + mp3_buffer_offs*2, (length_mp3>>shr)<<1); + mix_samples(buffer, shared_data->mp3_buffer[0] + mp3_buffer_offs*2, length<<1); mp3_buffer_offs = length_mp3 - left; mp3_play_bufsel = 1; } else { @@ -530,38 +529,13 @@ int YM2612UpdateOne_940(int *buffer, int length, int stereo, int is_buf_empty) mp3_samples_ready -= length_mp3; } - if (shared_ctl->writebuffsel == 1) { - shared_ctl->writebuff0[writebuff_ptr] = 0xffff; - } else { - shared_ctl->writebuff1[writebuff_ptr] = 0xffff; - } - writebuff_ptr = 0; - - /* predict sample counter for next frame */ - if (PsndLen_exc_add) { - if (PsndLen_exc_cnt + PsndLen_exc_add >= 0x10000) length = PsndLen + 1; - else length = PsndLen; - } - - /* give 940 ym job */ - shared_ctl->writebuffsel ^= 1; - shared_ctl->length = length; - shared_ctl->stereo = stereo; - - // make sure we will have enough mp3 samples next frame - if (cdda_on && mp3_samples_ready < length_mp3) + // ask to decode more if we already can + if (!mp3_job_started) { + mp3_job_started = 1; shared_ctl->mp3_buffsel ^= 1; - mp3_job = JOB940_MP3DECODE; - mp3_samples_ready += 1152; + add_job_940(JOB940_MP3DECODE); } - - add_job_940(JOB940_YM2612UPDATEONE, mp3_job); - //spend_cycles(512); - //printf("SRCPND: %08lx, INTMODE: %08lx, INTMASK: %08lx, INTPEND: %08lx\n", - // gp2x_memregl[0x4500>>2], gp2x_memregl[0x4504>>2], gp2x_memregl[0x4508>>2], gp2x_memregl[0x4510>>2]); - - return 1; } @@ -584,6 +558,12 @@ void mp3_start_play(FILE *f, int pos) // pos is 0-1023 // else printf("done. mp3 too large, not all data loaded.\n"); shared_ctl->mp3_len = ftell(f); loaded_mp3 = f; + + if (PicoOpt&0x200) { + // as we are going to change 940's cacheable area, we must invalidate it's cache.. + if (CHECK_BUSY(JOB940_MP3DECODE)) wait_busy_940(JOB940_MP3DECODE); + add_job_940(JOB940_INVALIDATE_DCACHE); + } } // seek.. @@ -596,9 +576,12 @@ void mp3_start_play(FILE *f, int pos) // pos is 0-1023 shared_ctl->mp3_offs = byte_offs; - // reset buffer pointers.. + // reset buffer pointers and stuff.. mp3_samples_ready = mp3_buffer_offs = mp3_play_bufsel = 0; + mp3_job_started = 0; shared_ctl->mp3_buffsel = 1; // will change to 0 on first decode + + if (!(PicoOpt&0x200)) mp3_start_local(); } @@ -608,7 +591,7 @@ int mp3_get_offset(void) int cdda_on; cdda_on = (PicoMCD & 1) && (PicoOpt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) && - (Pico_mcd->scd.Status_CDC & 1) && loaded_mp3 && shared_ctl->mp3_offs < shared_ctl->mp3_len; + (Pico_mcd->scd.Status_CDC & 1) && loaded_mp3; if (cdda_on) { offs1024 = shared_ctl->mp3_offs << 7; diff --git a/gp2x/mp3.c b/gp2x/mp3.c new file mode 100644 index 0000000..e37b774 --- /dev/null +++ b/gp2x/mp3.c @@ -0,0 +1,134 @@ +#include +#include + +#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(); +} + diff --git a/gp2x/mp3.h b/gp2x/mp3.h new file mode 100644 index 0000000..022d89c --- /dev/null +++ b/gp2x/mp3.h @@ -0,0 +1,4 @@ + +void mp3_update_local(int *buffer, int length, int stereo); +void mp3_start_local(void); + -- 2.39.5