.cue support, Pico stubs
[picodrive.git] / Pico / cd / cd_file.c
index 895a75f..549aab9 100644 (file)
@@ -9,15 +9,61 @@
 
 #include "../PicoInt.h"
 #include "cd_file.h"
+#include "cue.h"
 
 //#define cdprintf(f,...) printf(f "\n",##__VA_ARGS__) // tmp
-#define DEBUG_CD
 
-PICO_INTERNAL int Load_CD_Image(const char *iso_name, cd_img_type type)
+static int audio_track_mp3(const char *fname, int index)
 {
-       int i, j, num_track, Cur_LBA, index, ret, iso_name_len;
+       _scd_track *Tracks = Pico_mcd->TOC.Tracks;
+       FILE *tmp_file;
+       int fs, ret;
+
+       tmp_file = fopen(fname, "rb");
+       if (tmp_file == NULL)
+               return -1;
+
+       ret = fseek(tmp_file, 0, SEEK_END);
+       fs = ftell(tmp_file);                           // used to calculate length
+       fseek(tmp_file, 0, SEEK_SET);
+
+#if DONT_OPEN_MANY_FILES
+       // some systems (like PSP) can't have many open files at a time,
+       // so we work with their names instead.
+       fclose(tmp_file);
+       tmp_file = (void *) strdup(fname);
+#endif
+       Tracks[index].KBtps = (short) mp3_get_bitrate(tmp_file, fs);
+       Tracks[index].KBtps >>= 3;
+       if (ret != 0 || Tracks[index].KBtps <= 0)
+       {
+               elprintf(EL_STATUS, "track %2i: mp3 bitrate %i", index+1, Tracks[index].KBtps);
+#if !DONT_OPEN_MANY_FILES
+               fclose(tmp_file);
+#else
+               free(tmp_file);
+#endif
+               return -1;
+       }
+
+       Tracks[index].F = tmp_file;
+
+       // MP3 File
+       Tracks[index].ftype = TYPE_MP3;
+       fs *= 75;
+       fs /= Tracks[index].KBtps * 1000;
+       Tracks[index].Length = fs;
+       Tracks[index].Offset = 0;
+
+       return 0;
+}
+
+PICO_INTERNAL int Load_CD_Image(const char *cd_img_name, cd_img_type type)
+{
+       int i, j, num_track, Cur_LBA, index, ret, iso_name_len, missed, cd_img_sectors;
        _scd_track *Tracks = Pico_mcd->TOC.Tracks;
        char tmp_name[1024], tmp_ext[10];
+       cue_data_t *cue_data = NULL;
        pm_file *pmf;
        static char *exts[] = {
                "%02d.mp3", " %02d.mp3", "-%02d.mp3", "_%02d.mp3", " - %02d.mp3",
@@ -31,109 +77,134 @@ PICO_INTERNAL int Load_CD_Image(const char *iso_name, cd_img_type type)
 
        Unload_ISO();
 
+       /* is this .cue? */
+       ret = strlen(cd_img_name);
+       if (ret >= 3 && strcasecmp(cd_img_name + ret - 3, "cue") == 0)
+               cue_data = cue_parse(cd_img_name);
+       if (cue_data != NULL)
+               cd_img_name = cue_data->tracks[1].fname;
+
        Tracks[0].ftype = type == CIT_BIN ? TYPE_BIN : TYPE_ISO;
 
-       Tracks[0].F = pmf = pm_open(iso_name);
+       Tracks[0].F = pmf = pm_open(cd_img_name);
        if (Tracks[0].F == NULL)
        {
                Tracks[0].ftype = 0;
                Tracks[0].Length = 0;
+               if (cue_data != NULL)
+                       cue_destroy(cue_data);
                return -1;
        }
 
        if (Tracks[0].ftype == TYPE_ISO)
-               Tracks[0].Length = pmf->size >>= 11;    // size in sectors
-       else    Tracks[0].Length = pmf->size /= 2352;
+            cd_img_sectors = pmf->size >>= 11; // size in sectors
+       else cd_img_sectors = pmf->size /= 2352;
+       Tracks[0].Offset = 0;
 
        Tracks[0].MSF.M = 0; // minutes
        Tracks[0].MSF.S = 2; // seconds
        Tracks[0].MSF.F = 0; // frames
 
-       cdprintf("Track 0 - %02d:%02d:%02d DATA", Tracks[0].MSF.M, Tracks[0].MSF.S, Tracks[0].MSF.F);
+       elprintf(EL_STATUS, "Track  0: %02d:%02d:%02d %9i DATA",
+               Tracks[0].MSF.M, Tracks[0].MSF.S, Tracks[0].MSF.F, Tracks[0].Length);
+
+       Cur_LBA = Tracks[0].Length = cd_img_sectors;
+
+       if (cue_data != NULL)
+       {
+               if (cue_data->tracks[2].fname == NULL) { // NULL means track2 is in same file as track1
+                       Cur_LBA = Tracks[0].Length = cue_data->tracks[2].sector_offset;
+               }
+               for (num_track = 2; num_track <= cue_data->track_count; num_track++)
+               {
+                       index = num_track - 1;
+                       Cur_LBA += cue_data->tracks[num_track].pregap;
+                       if (cue_data->tracks[num_track].type == CT_MP3) {
+                               ret = audio_track_mp3(cue_data->tracks[num_track].fname, index);
+                               if (ret != 0) break;
+                       }
+                       else
+                       {
+                               Tracks[index].ftype = cue_data->tracks[num_track].type;
+                               if (cue_data->tracks[num_track].fname != NULL)
+                               {
+                                       Tracks[index].F = fopen(cue_data->tracks[num_track].fname, "rb");
+                                       elprintf(EL_STATUS, "track %2i (%s): can't determine length",
+                                               cue_data->tracks[num_track].fname);
+                                       Tracks[index].Length = 2*75;
+                                       Tracks[index].Offset = 0;
+                               } else {
+                                       if (num_track < cue_data->track_count)
+                                               Tracks[index].Length = cue_data->tracks[num_track+1].sector_offset -
+                                                       cue_data->tracks[num_track].sector_offset;
+                                       else
+                                               Tracks[index].Length = cd_img_sectors - cue_data->tracks[num_track].sector_offset;
+                                       Tracks[index].Offset = cue_data->tracks[num_track].sector_offset;
+                               }
+                       }
+
+                       LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
+                       Cur_LBA += Tracks[index].Length;
 
-       Cur_LBA = Tracks[0].Length;                             // Size in sectors
+                       elprintf(EL_STATUS, "Track %2i: %02d:%02d:%02d %9i AUDIO - %s", index, Tracks[index].MSF.M,
+                               Tracks[index].MSF.S, Tracks[index].MSF.F, Tracks[index].Length,
+                               cue_data->tracks[num_track].fname);
+               }
+               cue_destroy(cue_data);
+               goto finish;
+       }
 
-       iso_name_len = strlen(iso_name);
+       /* track autosearch, Gens-like */
+       iso_name_len = strlen(cd_img_name);
        if (iso_name_len >= sizeof(tmp_name))
                iso_name_len = sizeof(tmp_name) - 1;
 
-       for (num_track = 2, i = 0; i < 100; i++)
+       for (num_track = 2, i = 0, missed = 0; i < 100 && missed < 4; i++)
        {
-               if (PicoCDLoadProgressCB != NULL && i > 1) PicoCDLoadProgressCB(i);
+               if (PicoCDLoadProgressCB != NULL && i > 1) PicoCDLoadProgressCB(i + (100-i)*missed/4);
 
                for (j = 0; j < sizeof(exts)/sizeof(char *); j++)
                {
                        int ext_len;
-                       FILE *tmp_file;
                        sprintf(tmp_ext, exts[j], i);
                        ext_len = strlen(tmp_ext);
 
-                       memcpy(tmp_name, iso_name, iso_name_len + 1);
+                       memcpy(tmp_name, cd_img_name, iso_name_len + 1);
                        tmp_name[iso_name_len - 4] = 0;
                        strcat(tmp_name, tmp_ext);
 
-                       tmp_file = fopen(tmp_name, "rb");
-                       if (!tmp_file && i > 1 && iso_name_len > ext_len) {
+                       index = num_track - 1;
+                       ret = audio_track_mp3(tmp_name, index);
+                       if (ret != 0 && i > 1 && iso_name_len > ext_len) {
                                tmp_name[iso_name_len - ext_len] = 0;
                                strcat(tmp_name, tmp_ext);
-                               tmp_file = fopen(tmp_name, "rb");
+                               ret = audio_track_mp3(tmp_name, index);
                        }
 
-                       if (tmp_file)
+                       if (ret == 0)
                        {
-                               int fs;
-                               index = num_track - 1;
-
-                               ret = fseek(tmp_file, 0, SEEK_END);
-                               fs = ftell(tmp_file);                           // used to calculate lenght
-                               fseek(tmp_file, 0, SEEK_SET);
-
-#if DONT_OPEN_MANY_FILES
-                               // some systems (like PSP) can't have many open files at a time,
-                               // so we work with their names instead.
-                               fclose(tmp_file);
-                               tmp_file = (void *) strdup(tmp_name);
-#endif
-                               Tracks[index].KBtps = (short) mp3_get_bitrate(tmp_file, fs);
-                               Tracks[index].KBtps >>= 3;
-                               if (ret != 0 || Tracks[index].KBtps <= 0)
-                               {
-                                       cdprintf("Error track %i: rate %i", index, Tracks[index].KBtps);
-#if !DONT_OPEN_MANY_FILES
-                                       fclose(tmp_file);
-#else
-                                       free(tmp_file);
-#endif
-                                       continue;
-                               }
-
-                               Tracks[index].F = tmp_file;
-
                                LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
-
-                               // MP3 File
-                               Tracks[index].ftype = TYPE_MP3;
-                               fs *= 75;
-                               fs /= Tracks[index].KBtps * 1000;
-                               Tracks[index].Length = fs;
                                Cur_LBA += Tracks[index].Length;
 
-                               cdprintf("Track %i: %s - %02d:%02d:%02d len=%i AUDIO", index, tmp_name, Tracks[index].MSF.M,
-                                       Tracks[index].MSF.S, Tracks[index].MSF.F, fs);
+                               elprintf(EL_STATUS, "Track %2i: %02d:%02d:%02d %9i AUDIO - %s", index, Tracks[index].MSF.M,
+                                       Tracks[index].MSF.S, Tracks[index].MSF.F, Tracks[index].Length, tmp_name);
 
                                num_track++;
+                               missed = 0;
                                break;
                        }
                }
+               if (ret != 0) missed++;
        }
 
+finish:
        Pico_mcd->TOC.Last_Track = num_track - 1;
 
        index = num_track - 1;
 
        LBA_to_MSF(Cur_LBA, &Tracks[index].MSF);
 
-       cdprintf("End CD - %02d:%02d:%02d\n\n", Tracks[index].MSF.M,
+       elprintf(EL_STATUS, "End CD -  %02d:%02d:%02d\n", Tracks[index].MSF.M,
                Tracks[index].MSF.S, Tracks[index].MSF.F);
 
        if (PicoCDLoadProgressCB != NULL) PicoCDLoadProgressCB(100);
@@ -153,11 +224,14 @@ PICO_INTERNAL void Unload_ISO(void)
        for(i = 1; i < 100; i++)
        {
                if (Pico_mcd->TOC.Tracks[i].F != NULL)
-#if !DONT_OPEN_MANY_FILES
-                       fclose(Pico_mcd->TOC.Tracks[i].F);
-#else
-                       free(Pico_mcd->TOC.Tracks[i].F);
+               {
+#if DONT_OPEN_MANY_FILES
+                       if (Pico_mcd->TOC.Tracks[i].type == TYPE_MP3)
+                               free(Pico_mcd->TOC.Tracks[i].F);
+                       else
 #endif
+                               fclose(Pico_mcd->TOC.Tracks[i].F);
+               }
        }
        memset(Pico_mcd->TOC.Tracks, 0, sizeof(Pico_mcd->TOC.Tracks));
 }
@@ -223,7 +297,6 @@ PICO_INTERNAL int FILE_Read_One_LBA_CDC(void)
                                //pm_read(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, 2048, Pico_mcd->TOC.Tracks[0].F);
                                PicoCDBufferRead(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, where_read);
 
-#ifdef DEBUG_CD
                                cdprintf("Read -> WA = %d  Buffer[%d] =", Pico_mcd->cdc.WA.N, Pico_mcd->cdc.PT.N & 0x3FFF);
                                cdprintf("Header 1 = %.2X %.2X %.2X %.2X", Pico_mcd->cdc.HEAD.B.B0,
                                        Pico_mcd->cdc.HEAD.B.B1, Pico_mcd->cdc.HEAD.B.B2, Pico_mcd->cdc.HEAD.B.B3);
@@ -234,7 +307,6 @@ PICO_INTERNAL int FILE_Read_One_LBA_CDC(void)
                                        Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 3) & 0x3FFF],
                                        Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 4) & 0x3FFF],
                                        Pico_mcd->cdc.Buffer[(Pico_mcd->cdc.PT.N + 5) & 0x3FFF]);
-#endif
                        }
 
                }