Add libretro VFS and use VFS for windows target
[pcsx_rearmed.git] / libpcsxcore / cdriso.c
index 8171674..fc1d077 100644 (file)
 #include <unistd.h>
 #endif
 
+#ifdef USE_LIBRETRO_VFS
+#include <streams/file_stream_transforms.h>
+#undef fseeko
+#undef ftello
+#define ftello rftell
+#define fseeko rfseek
+#endif
 #define OFF_T_MSB ((off_t)1 << (sizeof(off_t) * 8 - 1))
 
 unsigned int cdrIsoMultidiskCount;
@@ -477,7 +484,10 @@ static int parsecue(const char *isofile) {
        strncpy(cuename, isofile, sizeof(cuename));
        cuename[MAXPATHLEN - 1] = '\0';
        if (strlen(cuename) >= 4) {
-               strcpy(cuename + strlen(cuename) - 4, ".cue");
+               // If 'isofile' is a '.cd<X>' file, use it as a .cue file
+               //  and don't try to search the additional .cue file
+               if (strncasecmp(cuename + strlen(cuename) - 4, ".cd", 3) != 0 )
+                       strcpy(cuename + strlen(cuename) - 4, ".cue");
        }
        else {
                return -1;
@@ -604,9 +614,9 @@ static int parsecue(const char *isofile) {
                        file_len = ftell(ti[numtracks + 1].handle) / 2352;
 
                        if (numtracks == 0 && strlen(isofile) >= 4 &&
-                               strcmp(isofile + strlen(isofile) - 4, ".cue") == 0)
-                       {
-                               // user selected .cue as image file, use it's data track instead
+                               (strcmp(isofile + strlen(isofile) - 4, ".cue") == 0 ||
+                               strncasecmp(isofile + strlen(isofile) - 4, ".cd", 3) == 0)) {
+                               // user selected .cue/.cdX as image file, use it's data track instead
                                fclose(cdHandle);
                                cdHandle = fopen(filepath, "rb");
                        }
@@ -615,6 +625,10 @@ static int parsecue(const char *isofile) {
 
        fclose(fi);
 
+       // if there are no tracks detected, then it's not a cue file
+       if (!numtracks)
+               return -1;
+
        return 0;
 }
 
@@ -1050,7 +1064,10 @@ static int handlechd(const char *isofile) {
                goto fail_io;
 
        if(chd_open(isofile, CHD_OPEN_READ, NULL, &chd_img->chd) != CHDERR_NONE)
-      goto fail_io;
+               goto fail_io;
+
+       if (Config.CHD_Precache && (chd_precache(chd_img->chd) != CHDERR_NONE))
+               goto fail_io;
 
    chd_img->header = chd_get_header(chd_img->chd);
 
@@ -1064,7 +1081,8 @@ static int handlechd(const char *isofile) {
    cddaBigEndian = TRUE;
 
        numtracks = 0;
-       int frame_offset = 150;
+       int frame_offset = 0;
+       int file_offset = 0;
        memset(ti, 0, sizeof(ti));
 
    while (1)
@@ -1089,18 +1107,20 @@ static int handlechd(const char *isofile) {
       else
          break;
 
-               ti[md.track].type = !strncmp(md.type, "AUDIO", 5) ? CDDA : DATA;
+               if(md.track == 1)
+                       md.pregap = 150;
+               else
+                       sec2msf(msf2sec(ti[md.track-1].length) + md.pregap, ti[md.track-1].length);
 
-      sec2msf(frame_offset + md.pregap, ti[md.track].start);
-      sec2msf(md.frames - md.pregap, ti[md.track].length);
+               ti[md.track].type = !strncmp(md.type, "AUDIO", 5) ? CDDA : DATA;
 
-      if (!strcmp(md.type, md.pgtype))
-         frame_offset += md.pregap;
+               sec2msf(frame_offset + md.pregap, ti[md.track].start);
+               sec2msf(md.frames, ti[md.track].length);
 
-      ti[md.track].start_offset = frame_offset * CD_FRAMESIZE_RAW;
+               ti[md.track].start_offset = file_offset;
 
-               frame_offset += md.frames;
-               frame_offset += md.postgap;
+               frame_offset += md.pregap + md.frames + md.postgap;
+               file_offset += md.frames + md.postgap;
                numtracks++;
        }
 
@@ -1163,6 +1183,199 @@ static int opensbifile(const char *isoname) {
        return LoadSBI(sbiname, s);
 }
 
+#ifdef _WIN32
+static void readThreadStop() {}
+static void readThreadStart() {}
+#else
+static pthread_t read_thread_id;
+
+static pthread_cond_t read_thread_msg_avail;
+static pthread_cond_t read_thread_msg_done;
+static pthread_mutex_t read_thread_msg_lock;
+
+static pthread_cond_t sectorbuffer_cond;
+static pthread_mutex_t sectorbuffer_lock;
+
+static boolean read_thread_running = FALSE;
+static int read_thread_sector_start = -1;
+static int read_thread_sector_end = -1;
+
+typedef struct {
+  int sector;
+  long ret;
+  unsigned char data[CD_FRAMESIZE_RAW];
+} SectorBufferEntry;
+
+#define SECTOR_BUFFER_SIZE 4096
+
+static SectorBufferEntry *sectorbuffer;
+static size_t sectorbuffer_index;
+
+int (*sync_cdimg_read_func)(FILE *f, unsigned int base, void *dest, int sector);
+unsigned char *(*sync_CDR_getBuffer)(void);
+
+static unsigned char * CALLBACK ISOgetBuffer_async(void);
+static int cdread_async(FILE *f, unsigned int base, void *dest, int sector);
+
+static void *readThreadMain(void *param) {
+  int max_sector = -1;
+  int requested_sector_start = -1;
+  int requested_sector_end = -1;
+  int last_read_sector = -1;
+  int index = 0;
+
+  int ra_sector = -1;
+  int max_ra = 128;
+  int initial_ra = 1;
+  int speedmult_ra = 4;
+
+  int ra_count = 0;
+  int how_far_ahead = 0;
+
+  unsigned char tmpdata[CD_FRAMESIZE_RAW];
+  long ret;
+
+  max_sector = msf2sec(ti[numtracks].start) + msf2sec(ti[numtracks].length);
+
+  while(1) {
+    pthread_mutex_lock(&read_thread_msg_lock);
+
+    // If we don't have readahead and we don't have a sector request, wait for one.
+    // If we still have readahead to go, don't block, just keep going.
+    // And if we ever have a sector request pending, acknowledge and reset it.
+
+    if (!ra_count) {
+      if (read_thread_sector_start == -1 && read_thread_running) {
+        pthread_cond_wait(&read_thread_msg_avail, &read_thread_msg_lock);
+      }
+    }
+
+    if (read_thread_sector_start != -1) {
+      requested_sector_start = read_thread_sector_start;
+      requested_sector_end = read_thread_sector_end;
+      read_thread_sector_start = -1;
+      read_thread_sector_end = -1;
+      pthread_cond_signal(&read_thread_msg_done);
+    }
+
+    pthread_mutex_unlock(&read_thread_msg_lock);
+
+    if (!read_thread_running)
+      break;
+
+    // Readahead code, based on the implementation in mednafen psx's cdromif.cpp
+    if (requested_sector_start != -1) {
+      if (last_read_sector != -1 && last_read_sector == (requested_sector_start - 1)) {
+        how_far_ahead = ra_sector - requested_sector_end;
+
+        if(how_far_ahead <= max_ra)
+          ra_count = (max_ra - how_far_ahead + 1 ? max_ra - how_far_ahead + 1 : speedmult_ra);
+        else
+          ra_count++;
+      } else if (requested_sector_end != last_read_sector) {
+        ra_sector = requested_sector_end;
+        ra_count = initial_ra;
+      }
+
+      last_read_sector = requested_sector_end;
+    }
+
+    index = ra_sector % SECTOR_BUFFER_SIZE;
+
+    // check for end of CD
+    if (ra_count && ra_sector >= max_sector) {
+      ra_count = 0;
+      pthread_mutex_lock(&sectorbuffer_lock);
+      sectorbuffer[index].ret = -1;
+      sectorbuffer[index].sector = ra_sector;
+      pthread_cond_signal(&sectorbuffer_cond);
+      pthread_mutex_unlock(&sectorbuffer_lock);
+    }
+
+    if (ra_count) {
+      pthread_mutex_lock(&sectorbuffer_lock);
+      if (sectorbuffer[index].sector != ra_sector) {
+        pthread_mutex_unlock(&sectorbuffer_lock);
+
+        ret = sync_cdimg_read_func(cdHandle, 0, tmpdata, ra_sector);
+
+        pthread_mutex_lock(&sectorbuffer_lock);
+        sectorbuffer[index].ret = ret;
+        sectorbuffer[index].sector = ra_sector;
+        memcpy(sectorbuffer[index].data, tmpdata, CD_FRAMESIZE_RAW);
+      }
+      pthread_cond_signal(&sectorbuffer_cond);
+      pthread_mutex_unlock(&sectorbuffer_lock);
+
+      ra_sector++;
+      ra_count--;
+    }
+  }
+
+  return NULL;
+}
+
+static void readThreadStop() {
+  if (read_thread_running == TRUE) {
+    read_thread_running = FALSE;
+    pthread_cond_signal(&read_thread_msg_avail);
+    pthread_join(read_thread_id, NULL);
+  }
+
+  pthread_cond_destroy(&read_thread_msg_done);
+  pthread_cond_destroy(&read_thread_msg_avail);
+  pthread_mutex_destroy(&read_thread_msg_lock);
+
+  pthread_cond_destroy(&sectorbuffer_cond);
+  pthread_mutex_destroy(&sectorbuffer_lock);
+
+  CDR_getBuffer = sync_CDR_getBuffer;
+  cdimg_read_func = sync_cdimg_read_func;
+
+  free(sectorbuffer);
+  sectorbuffer = NULL;
+}
+
+static void readThreadStart() {
+  SysPrintf("Starting async CD thread\n");
+
+  if (read_thread_running == TRUE)
+    return;
+
+  read_thread_running = TRUE;
+  read_thread_sector_start = -1;
+  read_thread_sector_end = -1;
+  sectorbuffer_index = 0;
+
+  sectorbuffer = calloc(SECTOR_BUFFER_SIZE, sizeof(SectorBufferEntry));
+  if(!sectorbuffer)
+    goto error;
+
+  sectorbuffer[0].sector = -1; // Otherwise we might think we've already fetched sector 0!
+
+  sync_CDR_getBuffer = CDR_getBuffer;
+  CDR_getBuffer = ISOgetBuffer_async;
+  sync_cdimg_read_func = cdimg_read_func;
+  cdimg_read_func = cdread_async;
+
+  if (pthread_cond_init(&read_thread_msg_avail, NULL) ||
+      pthread_cond_init(&read_thread_msg_done, NULL) ||
+      pthread_mutex_init(&read_thread_msg_lock, NULL) ||
+      pthread_cond_init(&sectorbuffer_cond, NULL) ||
+      pthread_mutex_init(&sectorbuffer_lock, NULL) ||
+      pthread_create(&read_thread_id, NULL, readThreadMain, NULL))
+    goto error;
+
+  return;
+
+ error:
+  SysPrintf("Error starting async CD thread\n");
+  SysPrintf("Falling back to sync\n");
+
+  readThreadStop();
+}
+#endif
+
 static int cdread_normal(FILE *f, unsigned int base, void *dest, int sector)
 {
        fseek(f, base + sector * CD_FRAMESIZE_RAW, SEEK_SET);
@@ -1289,19 +1502,17 @@ static int cdread_chd(FILE *f, unsigned int base, void *dest, int sector)
        int hunk;
 
        if (base)
-               sector += base / CD_FRAMESIZE_RAW;
+               sector += base;
 
        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;
+       if (hunk != chd_img->current_hunk)
+       {
+               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);
@@ -1323,6 +1534,54 @@ static int cdread_2048(FILE *f, unsigned int base, void *dest, int sector)
        return ret;
 }
 
+#ifndef _WIN32
+
+static int cdread_async(FILE *f, unsigned int base, void *dest, int sector) {
+  boolean found = FALSE;
+  int i = sector % SECTOR_BUFFER_SIZE;
+  long ret;
+
+  if (f != cdHandle || base != 0 || dest != cdbuffer) {
+    // Async reads are only supported for cdbuffer, so call the sync
+    // function directly.
+    return sync_cdimg_read_func(f, base, dest, sector);
+  }
+
+  pthread_mutex_lock(&read_thread_msg_lock);
+
+  // Only wait if we're not trying to read the next sector and
+  // sector_start is set (meaning the last request hasn't been
+  // processed yet)
+  while(read_thread_sector_start != -1 && read_thread_sector_end + 1 != sector) {
+    pthread_cond_wait(&read_thread_msg_done, &read_thread_msg_lock);
+  }
+
+  if (read_thread_sector_start == -1)
+    read_thread_sector_start = sector;
+
+  read_thread_sector_end = sector;
+  pthread_cond_signal(&read_thread_msg_avail);
+  pthread_mutex_unlock(&read_thread_msg_lock);
+
+  do {
+    pthread_mutex_lock(&sectorbuffer_lock);
+    if (sectorbuffer[i].sector == sector) {
+      sectorbuffer_index = i;
+      ret = sectorbuffer[i].ret;
+      found = TRUE;
+    }
+
+    if (!found) {
+      pthread_cond_wait(&sectorbuffer_cond, &sectorbuffer_lock);
+    }
+    pthread_mutex_unlock(&sectorbuffer_lock);
+  } while (!found);
+
+  return ret;
+}
+
+#endif
+
 static unsigned char * CALLBACK ISOgetBuffer_compr(void) {
        return compr_img->buff_raw[compr_img->sector_in_blk] + 12;
 }
@@ -1333,6 +1592,17 @@ static unsigned char * CALLBACK ISOgetBuffer_chd(void) {
 }
 #endif
 
+#ifndef _WIN32
+static unsigned char * CALLBACK ISOgetBuffer_async(void) {
+  unsigned char *buffer;
+  pthread_mutex_lock(&sectorbuffer_lock);
+  buffer = sectorbuffer[sectorbuffer_index].data;
+  pthread_mutex_unlock(&sectorbuffer_lock);
+  return buffer + 12;
+}
+
+#endif
+
 static unsigned char * CALLBACK ISOgetBuffer(void) {
        return cdbuffer + 12;
 }
@@ -1473,6 +1743,9 @@ static long CALLBACK ISOopen(void) {
        cdda_cur_sector = 0;
        cdda_file_offset = 0;
 
+  if (Config.AsyncCD) {
+    readThreadStart();
+  }
        return 0;
 }
 
@@ -1518,6 +1791,10 @@ static long CALLBACK ISOclose(void) {
        memset(cdbuffer, 0, sizeof(cdbuffer));
        CDR_getBuffer = ISOgetBuffer;
 
+       if (Config.AsyncCD) {
+               readThreadStop();
+       }
+
        return 0;
 }