Merge pull request #351 from SindenLightgun/master
[pcsx_rearmed.git] / libpcsxcore / cdriso.c
index dca64fa..8171674 100644 (file)
 #include "cdriso.h"
 #include "ppf.h"
 
+#include <errno.h>
+#include <zlib.h>
+#ifdef HAVE_CHD
+#include <chd.h>
+#endif
+
 #ifdef _WIN32
 #define WIN32_LEAN_AND_MEAN
 #include <process.h>
 #include <windows.h>
 #define strcasecmp _stricmp
-#define usleep(x) Sleep((x) / 1000)
+#define usleep(x) (Sleep((x) / 1000))
 #else
 #include <pthread.h>
 #include <sys/time.h>
 #include <unistd.h>
 #endif
-#include <errno.h>
-#include <zlib.h>
 
 #define OFF_T_MSB ((off_t)1 << (sizeof(off_t) * 8 - 1))
 
@@ -92,6 +96,17 @@ static struct {
        unsigned int sector_in_blk;
 } *compr_img;
 
+#ifdef HAVE_CHD
+static struct {
+       unsigned char (*buffer)[CD_FRAMESIZE_RAW + SUB_FRAMESIZE];
+       chd_file* chd;
+       const chd_header* header;
+       unsigned int sectors_per_hunk;
+       unsigned int current_hunk;
+       unsigned int sector_in_hunk;
+} *chd_img;
+#endif
+
 int (*cdimg_read_func)(FILE *f, unsigned int base, void *dest, int sector);
 
 char* CALLBACK CDR__getDriveLetter(void);
@@ -225,7 +240,9 @@ static void *playthread(void *param)
                        do {
                                ret = SPU_playCDDAchannel((short *)sndbuffer, s);
                                if (ret == 0x7761)
+            {
                                        usleep(6 * 1000);
+            }
                        } while (ret == 0x7761 && playing); // rearmed_wait
                }
 
@@ -236,7 +253,9 @@ static void *playthread(void *param)
                        // HACK: stop feeding data while emu is paused
                        extern int stop;
                        while (stop && playing)
+         {
                                usleep(10000);
+         }
 
                        now = GetTickCount();
                        osleep = t - now;
@@ -560,20 +579,15 @@ static int parsecue(const char *isofile) {
                        if (t != 1)
                                sscanf(linebuf, " FILE %255s", tmpb);
 
-                       // absolute path?
-                       ti[numtracks + 1].handle = fopen(tmpb, "rb");
-                       if (ti[numtracks + 1].handle == NULL) {
-                               // relative to .cue?
-                               tmp = strrchr(tmpb, '\\');
-                               if (tmp == NULL)
-                                       tmp = strrchr(tmpb, '/');
-                               if (tmp != NULL)
-                                       tmp++;
-                               else
-                                       tmp = tmpb;
-                               strncpy(incue_fname, tmp, incue_max_len);
-                               ti[numtracks + 1].handle = fopen(filepath, "rb");
-                       }
+                       tmp = strrchr(tmpb, '\\');
+                       if (tmp == NULL)
+                               tmp = strrchr(tmpb, '/');
+                       if (tmp != NULL)
+                               tmp++;
+                       else
+                               tmp = tmpb;
+                       strncpy(incue_fname, tmp, incue_max_len);
+                       ti[numtracks + 1].handle = fopen(filepath, "rb");
 
                        // update global offset if this is not first file in this .cue
                        if (numtracks + 1 > 1) {
@@ -1029,6 +1043,80 @@ fail_io:
        return -1;
 }
 
+#ifdef HAVE_CHD
+static int handlechd(const char *isofile) {
+       chd_img = calloc(1, sizeof(*chd_img));
+       if (chd_img == NULL)
+               goto fail_io;
+
+       if(chd_open(isofile, CHD_OPEN_READ, NULL, &chd_img->chd) != CHDERR_NONE)
+      goto fail_io;
+
+   chd_img->header = chd_get_header(chd_img->chd);
+
+   chd_img->buffer = malloc(chd_img->header->hunkbytes);
+   if (chd_img->buffer == NULL)
+               goto fail_io;
+
+   chd_img->sectors_per_hunk = chd_img->header->hunkbytes / (CD_FRAMESIZE_RAW + SUB_FRAMESIZE);
+   chd_img->current_hunk = (unsigned int)-1;
+
+   cddaBigEndian = TRUE;
+
+       numtracks = 0;
+       int frame_offset = 150;
+       memset(ti, 0, sizeof(ti));
+
+   while (1)
+   {
+      struct {
+         char type[64];
+         char subtype[32];
+         char pgtype[32];
+         char pgsub[32];
+         uint32_t track;
+         uint32_t frames;
+         uint32_t pregap;
+         uint32_t postgap;
+      } md = {};
+      char meta[256];
+      uint32_t meta_size = 0;
+
+      if (chd_get_metadata(chd_img->chd, CDROM_TRACK_METADATA2_TAG, numtracks, meta, sizeof(meta), &meta_size, NULL, NULL) == CHDERR_NONE)
+         sscanf(meta, CDROM_TRACK_METADATA2_FORMAT, &md.track, md.type, md.subtype, &md.frames, &md.pregap, md.pgtype, md.pgsub, &md.postgap);
+      else if (chd_get_metadata(chd_img->chd, CDROM_TRACK_METADATA_TAG, numtracks, meta, sizeof(meta), &meta_size, NULL, NULL) == CHDERR_NONE)
+         sscanf(meta, CDROM_TRACK_METADATA_FORMAT, &md.track, md.type, md.subtype, &md.frames);
+      else
+         break;
+
+               ti[md.track].type = !strncmp(md.type, "AUDIO", 5) ? CDDA : DATA;
+
+      sec2msf(frame_offset + md.pregap, ti[md.track].start);
+      sec2msf(md.frames - md.pregap, ti[md.track].length);
+
+      if (!strcmp(md.type, md.pgtype))
+         frame_offset += md.pregap;
+
+      ti[md.track].start_offset = frame_offset * CD_FRAMESIZE_RAW;
+
+               frame_offset += md.frames;
+               frame_offset += md.postgap;
+               numtracks++;
+       }
+
+       if (numtracks)
+               return 0;
+
+fail_io:
+       if (chd_img != NULL) {
+               free(chd_img->buffer);
+               free(chd_img);
+               chd_img = NULL;
+       }
+       return -1;
+}
+#endif
+
 // this function tries to get the .sub file of the given .img
 static int opensubfile(const char *isoname) {
        char            subname[MAXPATHLEN];
@@ -1052,13 +1140,18 @@ static int opensubfile(const char *isoname) {
 }
 
 static int opensbifile(const char *isoname) {
-       char            sbiname[MAXPATHLEN];
+       char            sbiname[MAXPATHLEN], disknum[MAXPATHLEN] = "0";
        int             s;
 
        strncpy(sbiname, isoname, sizeof(sbiname));
        sbiname[MAXPATHLEN - 1] = '\0';
        if (strlen(sbiname) >= 4) {
-               strcpy(sbiname + strlen(sbiname) - 4, ".sbi");
+               if (cdrIsoMultidiskCount > 1) {
+                       sprintf(disknum, "_%i.sbi", cdrIsoMultidiskSelect + 1);
+                       strcpy(sbiname + strlen(sbiname) - 4, disknum);
+               }
+               else
+                       strcpy(sbiname + strlen(sbiname) - 4, ".sbi");
        }
        else {
                return -1;
@@ -1190,6 +1283,31 @@ finish:
        return CD_FRAMESIZE_RAW;
 }
 
+#ifdef HAVE_CHD
+static int cdread_chd(FILE *f, unsigned int base, void *dest, int sector)
+{
+       int hunk;
+
+       if (base)
+               sector += base / CD_FRAMESIZE_RAW;
+
+       hunk = sector / chd_img->sectors_per_hunk;
+       chd_img->sector_in_hunk = sector % chd_img->sectors_per_hunk;
+
+       if (hunk == chd_img->current_hunk)
+               goto finish;
+
+       chd_read(chd_img->chd, hunk, chd_img->buffer);
+
+       chd_img->current_hunk = hunk;
+
+finish:
+       if (dest != cdbuffer) // copy avoid HACK
+               memcpy(dest, chd_img->buffer[chd_img->sector_in_hunk],
+                       CD_FRAMESIZE_RAW);
+       return CD_FRAMESIZE_RAW;
+}
+#endif
 static int cdread_2048(FILE *f, unsigned int base, void *dest, int sector)
 {
        int ret;
@@ -1209,6 +1327,12 @@ static unsigned char * CALLBACK ISOgetBuffer_compr(void) {
        return compr_img->buff_raw[compr_img->sector_in_blk] + 12;
 }
 
+#ifdef HAVE_CHD
+static unsigned char * CALLBACK ISOgetBuffer_chd(void) {
+       return chd_img->buffer[chd_img->sector_in_hunk] + 12;
+}
+#endif
+
 static unsigned char * CALLBACK ISOgetBuffer(void) {
        return cdbuffer + 12;
 }
@@ -1230,6 +1354,7 @@ static long CALLBACK ISOopen(void) {
        boolean isMode1ISO = FALSE;
        char alt_bin_filename[MAXPATHLEN];
        const char *bin_filename;
+       char image_str[1024] = {0};
 
        if (cdHandle != NULL) {
                return 0; // it's already open
@@ -1242,7 +1367,7 @@ static long CALLBACK ISOopen(void) {
                return -1;
        }
 
-       SysPrintf(_("Loaded CD Image: %s"), GetIsoFile());
+       sprintf(image_str, "Loaded CD Image: %s", GetIsoFile());
 
        cddaBigEndian = FALSE;
        subChanMixed = FALSE;
@@ -1255,33 +1380,40 @@ static long CALLBACK ISOopen(void) {
        cdimg_read_func = cdread_normal;
 
        if (parsetoc(GetIsoFile()) == 0) {
-               SysPrintf("[+toc]");
+               strcat(image_str, "[+toc]");
        }
        else if (parseccd(GetIsoFile()) == 0) {
-               SysPrintf("[+ccd]");
+               strcat(image_str, "[+ccd]");
        }
        else if (parsemds(GetIsoFile()) == 0) {
-               SysPrintf("[+mds]");
+               strcat(image_str, "[+mds]");
        }
        else if (parsecue(GetIsoFile()) == 0) {
-               SysPrintf("[+cue]");
+               strcat(image_str, "[+cue]");
        }
        if (handlepbp(GetIsoFile()) == 0) {
-               SysPrintf("[pbp]");
+               strcat(image_str, "[+pbp]");
                CDR_getBuffer = ISOgetBuffer_compr;
                cdimg_read_func = cdread_compressed;
        }
        else if (handlecbin(GetIsoFile()) == 0) {
-               SysPrintf("[cbin]");
+               strcat(image_str, "[+cbin]");
                CDR_getBuffer = ISOgetBuffer_compr;
                cdimg_read_func = cdread_compressed;
        }
+#ifdef HAVE_CHD
+       else if (handlechd(GetIsoFile()) == 0) {
+               strcat(image_str, "[+chd]");
+               CDR_getBuffer = ISOgetBuffer_chd;
+               cdimg_read_func = cdread_chd;
+       }
+#endif
 
        if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
-               SysPrintf("[+sub]");
+               strcat(image_str, "[+sub]");
        }
        if (opensbifile(GetIsoFile()) == 0) {
-               SysPrintf("[+sbi]");
+               strcat(image_str, "[+sbi]");
        }
 
        fseeko(cdHandle, 0, SEEK_END);
@@ -1319,13 +1451,13 @@ static long CALLBACK ISOopen(void) {
                fseek(cdHandle, 0, SEEK_SET);
                fread(&modeTest, 4, 1, cdHandle);
                if (SWAP32(modeTest) != 0xffffff00) {
-                       SysPrintf("[2048]");
+                       strcat(image_str, "[2048]");
                        isMode1ISO = TRUE;
                }
        }
        fseek(cdHandle, 0, SEEK_SET);
 
-       SysPrintf(".\n");
+       SysPrintf("%s.\n", image_str);
 
        PrintTracks();
 
@@ -1364,6 +1496,15 @@ static long CALLBACK ISOclose(void) {
                compr_img = NULL;
        }
 
+#ifdef HAVE_CHD
+       if (chd_img != NULL) {
+               chd_close(chd_img->chd);
+               free(chd_img->buffer);
+               free(chd_img);
+               chd_img = NULL;
+       }
+#endif
+
        for (i = 1; i <= numtracks; i++) {
                if (ti[i].handle != NULL) {
                        fclose(ti[i].handle);