rearrange globals
[picodrive.git] / platform / psp / mp3.c
index 5a87877..4ea3bdd 100644 (file)
@@ -1,16 +1,26 @@
+/*
+ * PicoDrive
+ * (C) notaz, 2007,2008
+ *
+ * This work is licensed under the terms of MAME license.
+ * See COPYING file in the top-level directory.
+ */
+
 #include <stdio.h>
 #include <string.h>
 
 #include <pspkernel.h>
 #include <pspsdk.h>
 #include <pspaudiocodec.h>
+#include <kubridge.h>
 
-#include "../../Pico/PicoInt.h"
-#include "../../Pico/sound/mix.h"
+#include "../../pico/pico_int.h"
+#include "../../pico/sound/mix.h"
 #include "../common/lprintf.h"
 
 int mp3_last_error = 0;
 
+static int initialized = 0;
 static SceUID thread_job_sem = -1;
 static SceUID thread_busy_sem = -1;
 static int thread_exit = 0;
@@ -96,9 +106,14 @@ static int read_next_frame(int which_buffer)
                        continue; // bad frame
                }
 
-               if (bytes_read - frame_offset < frame_size) {
+               if (bytes_read - frame_offset < frame_size)
+               {
                        lprintf("unfit, foffs=%i\n", mp3_src_pos - bytes_read);
                        mp3_src_pos -= bytes_read - frame_offset;
+                       if (mp3_src_size - mp3_src_pos < frame_size) {
+                               mp3_src_pos = mp3_src_size;
+                               return 0; // EOF
+                       }
                        sceIoLseek32(mp3_handle, mp3_src_pos, PSP_SEEK_SET);
                        continue; // didn't fit, re-read..
                }
@@ -121,9 +136,20 @@ static int read_next_frame(int which_buffer)
 
 static SceUID load_start_module(const char *prxname)
 {
-       SceUID mod = pspSdkLoadStartModule(prxname, PSP_MEMORY_PARTITION_KERNEL);
-       if (mod < 0)
-               lprintf("failed to load %s: %08x\n", prxname, mod);
+       SceUID mod, mod1;
+       int status, ret;
+
+       mod = pspSdkLoadStartModule(prxname, PSP_MEMORY_PARTITION_KERNEL);
+       if (mod < 0) {
+               lprintf("failed to load %s (%08x), trying kuKernelLoadModule\n", prxname, mod);
+               mod1 = kuKernelLoadModule(prxname, 0, NULL);
+               if (mod1 < 0) lprintf("kuKernelLoadModule failed with %08x\n", mod1);
+               else {
+                       ret = sceKernelStartModule(mod1, 0, NULL, &status, 0);
+                       if (ret < 0) lprintf("sceKernelStartModule failed with %08x\n", ret);
+                       else mod = mod1;
+               }
+       }
        return mod;
 }
 
@@ -142,7 +168,8 @@ int mp3_init(void)
             mod = load_start_module("flash0:/kd/audiocodec.prx");
        else mod = load_start_module("flash0:/kd/avcodec.prx");
        if (mod < 0) {
-               ret = mod = load_start_module("flash0:/kd/audiocodec_260.prx"); // last chance..
+               ret = mod;
+               mod = load_start_module("flash0:/kd/audiocodec_260.prx"); // last chance..
                if (mod < 0) goto fail;
        }
 
@@ -181,8 +208,9 @@ int mp3_init(void)
                goto fail2;
        }
 
+       /* use slightly higher prio then main */
        thread_exit = 0;
-       thid = sceKernelCreateThread("mp3decode_thread", decode_thread, 30, 0x2000, 0, 0); /* use slightly higher prio then main */
+       thid = sceKernelCreateThread("mp3decode_thread", decode_thread, 30, 0x2000, 0, NULL);
        if (thid < 0) {
                lprintf("failed to create decode thread: %08x\n", thid);
                ret = thid;
@@ -195,6 +223,7 @@ int mp3_init(void)
        }
 
        mp3_last_error = 0;
+       initialized = 1;
        return 0;
 
 fail3:
@@ -207,12 +236,15 @@ fail1:
        sceAudiocodecReleaseEDRAM(mp3_codec_struct);
 fail:
        mp3_last_error = ret;
+       initialized = 0;
        return 1;
 }
 
 void mp3_deinit(void)
 {
-       lprintf("deinit\n");
+       lprintf("mp3_deinit, initialized=%i\n", initialized);
+
+       if (!initialized) return;
        thread_exit = 1;
        psp_sem_lock(thread_busy_sem);
        psp_sem_unlock(thread_busy_sem);
@@ -229,6 +261,7 @@ void mp3_deinit(void)
        sceKernelDeleteSema(thread_job_sem);
        thread_job_sem = -1;
        sceAudiocodecReleaseEDRAM(mp3_codec_struct);
+       initialized = 0;
 }
 
 // may overflow stack?
@@ -269,14 +302,16 @@ static int decode_thread(SceSize args, void *argp)
 }
 
 
-int mp3_get_bitrate(FILE *f, int size)
+// might be called before initialization
+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.. */
-       psp_sem_lock(thread_busy_sem);
+       if (thread_busy_sem >= 0)
+               psp_sem_lock(thread_busy_sem);
 
        if (mp3_handle >= 0) sceIoClose(mp3_handle);
        mp3_handle = sceIoOpen(fname, PSP_O_RDONLY, 0777);
@@ -288,19 +323,19 @@ int mp3_get_bitrate(FILE *f, int size)
        mp3_src_pos = 0;
        ret = read_next_frame(0);
        if (ret <= 0) {
-               lprintf("read_next_frame() failed\n");
+               lprintf("read_next_frame() failed (%s)\n", fname);
                goto end;
        }
        sample_rate = (mp3_src_buffer[0][2] & 0x0c) >> 2;
        bitrate = mp3_src_buffer[0][2] >> 4;
 
        if (sample_rate != 0) {
-               lprintf("unsupported samplerate\n");
+               lprintf("unsupported samplerate (%s)\n", fname);
                goto end; // only 44kHz supported..
        }
        bitrate = bitrates[bitrate];
        if (bitrate == 0) {
-               lprintf("unsupported bitrate\n");
+               lprintf("unsupported bitrate (%s)\n", fname);
                goto end;
        }
 
@@ -310,7 +345,8 @@ end:
        if (mp3_handle >= 0) sceIoClose(mp3_handle);
        mp3_handle = -1;
        mp3_fname = NULL;
-       psp_sem_unlock(thread_busy_sem);
+       if (thread_busy_sem >= 0)
+               psp_sem_unlock(thread_busy_sem);
        if (retval < 0) mp3_last_error = -1; // remember we had a problem..
        return retval;
 }
@@ -318,9 +354,11 @@ 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;
 
        lprintf("mp3_start_play(%s) @ %i\n", fname, pos);
        psp_sem_lock(thread_busy_sem);
@@ -338,6 +376,9 @@ void mp3_start_play(FILE *f, int pos)
                mp3_fname = fname;
        }
 
+       // clear decoder state
+       sceAudiocodecInit(mp3_codec_struct, 0x1002);
+
        // seek..
        mp3_src_pos = (int) (((float)pos / 1023.0f) * (float)mp3_src_size);
        sceIoLseek32(mp3_handle, mp3_src_pos, PSP_SEEK_SET);
@@ -425,7 +466,7 @@ int mp3_get_offset(void) // 0-1023
        unsigned int offs1024 = 0;
        int cdda_on;
 
-       cdda_on = (PicoMCD & 1) && (PicoOpt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) &&
+       cdda_on = (PicoIn.AHW & PAHW_MCD) && (PicoIn.opt&0x800) && !(Pico_mcd->s68k_regs[0x36] & 1) &&
                        (Pico_mcd->scd.Status_CDC & 1) && mp3_handle >= 0;
 
        if (cdda_on) {
@@ -438,3 +479,17 @@ int mp3_get_offset(void) // 0-1023
 }
 
 
+void mp3_reopen_file(void)
+{
+       if (mp3_fname == NULL) return;
+       lprintf("mp3_reopen_file(%s)\n", mp3_fname);
+
+       // try closing, just in case
+       if (mp3_handle >= 0) sceIoClose(mp3_handle);
+
+       mp3_handle = sceIoOpen(mp3_fname, PSP_O_RDONLY, 0777);
+       if (mp3_handle >= 0)
+               sceIoLseek32(mp3_handle, mp3_src_pos, PSP_SEEK_SET);
+       lprintf("mp3_reopen_file %s\n", mp3_handle >= 0 ? "ok" : "failed");
+}
+