Add libretro VFS and use VFS for windows target
[pcsx_rearmed.git] / libpcsxcore / cdriso.c
index 09aed17..fc1d077 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>
+
+#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;
 unsigned int cdrIsoMultidiskSelect;
@@ -83,13 +96,24 @@ static unsigned int pregapOffset;
 static struct {
        unsigned char buff_raw[16][CD_FRAMESIZE_RAW];
        unsigned char buff_compressed[CD_FRAMESIZE_RAW * 16 + 100];
-       unsigned int *index_table;
+       off_t *index_table;
        unsigned int index_len;
        unsigned int block_shift;
        unsigned int current_block;
        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);
@@ -223,7 +247,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
                }
 
@@ -234,7 +260,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;
@@ -456,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;
@@ -558,20 +589,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) {
@@ -588,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");
                        }
@@ -599,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;
 }
 
@@ -787,8 +817,9 @@ static int handlepbp(const char *isofile) {
                unsigned int dontcare[6];
        } index_entry;
        char psar_sig[11];
-       unsigned int t, cd_length, cdimg_base;
-       unsigned int offsettab[8], psisoimg_offs;
+       off_t psisoimg_offs, cdimg_base;
+       unsigned int t, cd_length;
+       unsigned int offsettab[8];
        const char *ext = NULL;
        int i, ret;
 
@@ -797,7 +828,7 @@ static int handlepbp(const char *isofile) {
        if (ext == NULL || (strcmp(ext, ".pbp") != 0 && strcmp(ext, ".PBP") != 0))
                return -1;
 
-       fseek(cdHandle, 0, SEEK_SET);
+       fseeko(cdHandle, 0, SEEK_SET);
 
        numtracks = 0;
 
@@ -807,7 +838,7 @@ static int handlepbp(const char *isofile) {
                goto fail_io;
        }
 
-       ret = fseek(cdHandle, pbp_hdr.psar_offs, SEEK_SET);
+       ret = fseeko(cdHandle, pbp_hdr.psar_offs, SEEK_SET);
        if (ret != 0) {
                SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs);
                goto fail_io;
@@ -818,7 +849,7 @@ static int handlepbp(const char *isofile) {
        psar_sig[10] = 0;
        if (strcmp(psar_sig, "PSTITLEIMG") == 0) {
                // multidisk image?
-               ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x200, SEEK_SET);
+               ret = fseeko(cdHandle, pbp_hdr.psar_offs + 0x200, SEEK_SET);
                if (ret != 0) {
                        SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs + 0x200);
                        goto fail_io;
@@ -844,9 +875,9 @@ static int handlepbp(const char *isofile) {
 
                psisoimg_offs += offsettab[cdrIsoMultidiskSelect];
 
-               ret = fseek(cdHandle, psisoimg_offs, SEEK_SET);
+               ret = fseeko(cdHandle, psisoimg_offs, SEEK_SET);
                if (ret != 0) {
-                       SysPrintf("failed to seek to %x\n", psisoimg_offs);
+                       SysPrintf("failed to seek to %llx\n", (long long)psisoimg_offs);
                        goto fail_io;
                }
 
@@ -860,9 +891,9 @@ static int handlepbp(const char *isofile) {
        }
 
        // seek to TOC
-       ret = fseek(cdHandle, psisoimg_offs + 0x800, SEEK_SET);
+       ret = fseeko(cdHandle, psisoimg_offs + 0x800, SEEK_SET);
        if (ret != 0) {
-               SysPrintf("failed to seek to %x\n", psisoimg_offs + 0x800);
+               SysPrintf("failed to seek to %llx\n", (long long)psisoimg_offs + 0x800);
                goto fail_io;
        }
 
@@ -896,7 +927,7 @@ static int handlepbp(const char *isofile) {
        sec2msf(t, ti[numtracks].length);
 
        // seek to ISO index
-       ret = fseek(cdHandle, psisoimg_offs + 0x4000, SEEK_SET);
+       ret = fseeko(cdHandle, psisoimg_offs + 0x4000, SEEK_SET);
        if (ret != 0) {
                SysPrintf("failed to seek to ISO index\n");
                goto fail_io;
@@ -954,6 +985,7 @@ static int handlecbin(const char *isofile) {
                unsigned char rsv_06[2];
        } ciso_hdr;
        const char *ext = NULL;
+       unsigned int *index_table = NULL;
        unsigned int index = 0, plain;
        int i, ret;
 
@@ -975,7 +1007,7 @@ static int handlecbin(const char *isofile) {
                return -1;
        }
        if (ciso_hdr.header_size != 0 && ciso_hdr.header_size != sizeof(ciso_hdr)) {
-               ret = fseek(cdHandle, ciso_hdr.header_size, SEEK_SET);
+               ret = fseeko(cdHandle, ciso_hdr.header_size, SEEK_SET);
                if (ret != 0) {
                        SysPrintf("failed to seek to %x\n", ciso_hdr.header_size);
                        return -1;
@@ -990,30 +1022,33 @@ static int handlecbin(const char *isofile) {
        compr_img->current_block = (unsigned int)-1;
 
        compr_img->index_len = ciso_hdr.total_bytes / ciso_hdr.block_size;
-       compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
-       if (compr_img->index_table == NULL)
+       index_table = malloc((compr_img->index_len + 1) * sizeof(index_table[0]));
+       if (index_table == NULL)
                goto fail_io;
 
-       ret = fread(compr_img->index_table, sizeof(compr_img->index_table[0]), compr_img->index_len, cdHandle);
+       ret = fread(index_table, sizeof(index_table[0]), compr_img->index_len, cdHandle);
        if (ret != compr_img->index_len) {
                SysPrintf("failed to read index table\n");
                goto fail_index;
        }
 
+       compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
+       if (compr_img->index_table == NULL)
+               goto fail_index;
+
        for (i = 0; i < compr_img->index_len + 1; i++) {
-               index = compr_img->index_table[i];
+               index = index_table[i];
                plain = index & 0x80000000;
                index &= 0x7fffffff;
-               compr_img->index_table[i] = (index << ciso_hdr.align) | plain;
+               compr_img->index_table[i] = (off_t)index << ciso_hdr.align;
+               if (plain)
+                       compr_img->index_table[i] |= OFF_T_MSB;
        }
-       if ((long long)index << ciso_hdr.align >= 0x80000000ll)
-               SysPrintf("warning: ciso img too large, expect problems\n");
 
        return 0;
 
 fail_index:
-       free(compr_img->index_table);
-       compr_img->index_table = NULL;
+       free(index_table);
 fail_io:
        if (compr_img != NULL) {
                free(compr_img);
@@ -1022,6 +1057,86 @@ 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;
+
+       if (Config.CHD_Precache && (chd_precache(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 = 0;
+       int file_offset = 0;
+       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;
+
+               if(md.track == 1)
+                       md.pregap = 150;
+               else
+                       sec2msf(msf2sec(ti[md.track-1].length) + md.pregap, ti[md.track-1].length);
+
+               ti[md.track].type = !strncmp(md.type, "AUDIO", 5) ? CDDA : DATA;
+
+               sec2msf(frame_offset + md.pregap, ti[md.track].start);
+               sec2msf(md.frames, ti[md.track].length);
+
+               ti[md.track].start_offset = file_offset;
+
+               frame_offset += md.pregap + md.frames + md.postgap;
+               file_offset += md.frames + 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];
@@ -1045,13 +1160,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;
@@ -1063,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);
@@ -1082,7 +1395,7 @@ static int cdread_sub_mixed(FILE *f, unsigned int base, void *dest, int sector)
        return ret;
 }
 
-static int uncompress2(void *out, unsigned long *out_size, void *in, unsigned long in_size)
+static int uncompress2_pcsx(void *out, unsigned long *out_size, void *in, unsigned long in_size)
 {
        static z_stream z;
        int ret = 0;
@@ -1116,8 +1429,9 @@ static int uncompress2(void *out, unsigned long *out_size, void *in, unsigned lo
 static int cdread_compressed(FILE *f, unsigned int base, void *dest, int sector)
 {
        unsigned long cdbuffer_size, cdbuffer_size_expect;
-       unsigned int start_byte, size;
+       unsigned int size;
        int is_compressed;
+       off_t start_byte;
        int ret, block;
 
        if (base)
@@ -1136,16 +1450,16 @@ static int cdread_compressed(FILE *f, unsigned int base, void *dest, int sector)
                return -1;
        }
 
-       start_byte = compr_img->index_table[block] & 0x7fffffff;
-       if (fseek(cdHandle, start_byte, SEEK_SET) != 0) {
-               SysPrintf("seek error for block %d at %x: ",
-                       block, start_byte);
+       start_byte = compr_img->index_table[block] & ~OFF_T_MSB;
+       if (fseeko(cdHandle, start_byte, SEEK_SET) != 0) {
+               SysPrintf("seek error for block %d at %llx: ",
+                       block, (long long)start_byte);
                perror(NULL);
                return -1;
        }
 
-       is_compressed = !(compr_img->index_table[block] & 0x80000000);
-       size = (compr_img->index_table[block + 1] & 0x7fffffff) - start_byte;
+       is_compressed = !(compr_img->index_table[block] & OFF_T_MSB);
+       size = (compr_img->index_table[block + 1] & ~OFF_T_MSB) - start_byte;
        if (size > sizeof(compr_img->buff_compressed)) {
                SysPrintf("block %d is too large: %u\n", block, size);
                return -1;
@@ -1161,7 +1475,7 @@ static int cdread_compressed(FILE *f, unsigned int base, void *dest, int sector)
        if (is_compressed) {
                cdbuffer_size_expect = sizeof(compr_img->buff_raw[0]) << compr_img->block_shift;
                cdbuffer_size = cdbuffer_size_expect;
-               ret = uncompress2(compr_img->buff_raw[0], &cdbuffer_size, compr_img->buff_compressed, size);
+               ret = uncompress2_pcsx(compr_img->buff_raw[0], &cdbuffer_size, compr_img->buff_compressed, size);
                if (ret != 0) {
                        SysPrintf("uncompress failed with %d for block %d, sector %d\n",
                                        ret, block, sector);
@@ -1182,6 +1496,29 @@ 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;
+
+       hunk = sector / chd_img->sectors_per_hunk;
+       chd_img->sector_in_hunk = sector % chd_img->sectors_per_hunk;
+
+       if (hunk != chd_img->current_hunk)
+       {
+               chd_read(chd_img->chd, hunk, chd_img->buffer);
+               chd_img->current_hunk = hunk;
+       }
+
+       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;
@@ -1197,10 +1534,75 @@ 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;
 }
 
+#ifdef HAVE_CHD
+static unsigned char * CALLBACK ISOgetBuffer_chd(void) {
+       return chd_img->buffer[chd_img->sector_in_hunk] + 12;
+}
+#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;
 }
@@ -1222,6 +1624,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
@@ -1234,7 +1637,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;
@@ -1247,40 +1650,47 @@ 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]");
        }
 
-       fseek(cdHandle, 0, SEEK_END);
+       fseeko(cdHandle, 0, SEEK_END);
 
        // maybe user selected metadata file instead of main .bin ..
        bin_filename = GetIsoFile();
-       if (ftell(cdHandle) < 2352 * 0x10) {
+       if (ftello(cdHandle) < 2352 * 0x10) {
                static const char *exts[] = { ".bin", ".BIN", ".img", ".IMG" };
                FILE *tmpf = NULL;
                size_t i;
@@ -1301,23 +1711,23 @@ static long CALLBACK ISOopen(void) {
                        bin_filename = alt_bin_filename;
                        fclose(cdHandle);
                        cdHandle = tmpf;
-                       fseek(cdHandle, 0, SEEK_END);
+                       fseeko(cdHandle, 0, SEEK_END);
                }
        }
 
        // guess whether it is mode1/2048
-       if (ftell(cdHandle) % 2048 == 0) {
+       if (ftello(cdHandle) % 2048 == 0) {
                unsigned int modeTest = 0;
                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();
 
@@ -1333,6 +1743,9 @@ static long CALLBACK ISOopen(void) {
        cdda_cur_sector = 0;
        cdda_file_offset = 0;
 
+  if (Config.AsyncCD) {
+    readThreadStart();
+  }
        return 0;
 }
 
@@ -1356,6 +1769,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);
@@ -1369,6 +1791,10 @@ static long CALLBACK ISOclose(void) {
        memset(cdbuffer, 0, sizeof(cdbuffer));
        CDR_getBuffer = ISOgetBuffer;
 
+       if (Config.AsyncCD) {
+               readThreadStop();
+       }
+
        return 0;
 }