warning fix
[pcsx_rearmed.git] / libpcsxcore / cdriso.c
index 8a631f4..f2886b0 100644 (file)
@@ -30,6 +30,7 @@
 #else
 #include <pthread.h>
 #include <sys/time.h>
+#include <unistd.h>
 #endif
 #include <zlib.h>
 
@@ -59,8 +60,11 @@ static pthread_t threadid;
 static unsigned int initial_offset = 0;
 static boolean playing = FALSE;
 static boolean cddaBigEndian = FALSE;
+// offsets of cddaHandle file
 static unsigned int cdda_cur_sector;
-static unsigned int cdda_start_sector;
+static unsigned int cdda_first_sector;
+// toc_sector - file_sector
+static unsigned int cdda_toc_delta;
 /* Frame offset into CD image where pregap data would be found if it was there.
  * If a game seeks there we must *not* return subchannel data since it's
  * not in the CD image, so that cdrom code can fake subchannel data instead.
@@ -181,6 +185,9 @@ static void *playthread(void *param)
                        if (d < CD_FRAMESIZE_RAW)
                                break;
 
+                       if (cdda_cur_sector < cdda_first_sector)
+                               memset(sndbuffer + s, 0, CD_FRAMESIZE_RAW);
+
                        s += d;
                        cdda_cur_sector++;
                }
@@ -735,13 +742,13 @@ static int handlepbp(const char *isofile) {
 
        ret = fread(&pbp_hdr, 1, sizeof(pbp_hdr), cdHandle);
        if (ret != sizeof(pbp_hdr)) {
-               fprintf(stderr, "failed to read pbp\n");
+               SysPrintf("failed to read pbp\n");
                goto fail_io;
        }
 
        ret = fseek(cdHandle, pbp_hdr.psar_offs, SEEK_SET);
        if (ret != 0) {
-               fprintf(stderr, "failed to seek to %x\n", pbp_hdr.psar_offs);
+               SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs);
                goto fail_io;
        }
 
@@ -752,12 +759,12 @@ static int handlepbp(const char *isofile) {
                // multidisk image?
                ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x200, SEEK_SET);
                if (ret != 0) {
-                       fprintf(stderr, "failed to seek to %x\n", pbp_hdr.psar_offs + 0x200);
+                       SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs + 0x200);
                        goto fail_io;
                }
 
                if (fread(&offsettab, 1, sizeof(offsettab), cdHandle) != sizeof(offsettab)) {
-                       fprintf(stderr, "failed to read offsettab\n");
+                       SysPrintf("failed to read offsettab\n");
                        goto fail_io;
                }
 
@@ -767,7 +774,7 @@ static int handlepbp(const char *isofile) {
                }
                cdrIsoMultidiskCount = i;
                if (cdrIsoMultidiskCount == 0) {
-                       fprintf(stderr, "multidisk eboot has 0 images?\n");
+                       SysPrintf("multidisk eboot has 0 images?\n");
                        goto fail_io;
                }
 
@@ -778,7 +785,7 @@ static int handlepbp(const char *isofile) {
 
                ret = fseek(cdHandle, psisoimg_offs, SEEK_SET);
                if (ret != 0) {
-                       fprintf(stderr, "failed to seek to %x\n", psisoimg_offs);
+                       SysPrintf("failed to seek to %x\n", psisoimg_offs);
                        goto fail_io;
                }
 
@@ -787,14 +794,14 @@ static int handlepbp(const char *isofile) {
        }
 
        if (strcmp(psar_sig, "PSISOIMG00") != 0) {
-               fprintf(stderr, "bad psar_sig: %s\n", psar_sig);
+               SysPrintf("bad psar_sig: %s\n", psar_sig);
                goto fail_io;
        }
 
        // seek to TOC
        ret = fseek(cdHandle, psisoimg_offs + 0x800, SEEK_SET);
        if (ret != 0) {
-               fprintf(stderr, "failed to seek to %x\n", psisoimg_offs + 0x800);
+               SysPrintf("failed to seek to %x\n", psisoimg_offs + 0x800);
                goto fail_io;
        }
 
@@ -829,7 +836,7 @@ static int handlepbp(const char *isofile) {
        // seek to ISO index
        ret = fseek(cdHandle, psisoimg_offs + 0x4000, SEEK_SET);
        if (ret != 0) {
-               fprintf(stderr, "failed to seek to ISO index\n");
+               SysPrintf("failed to seek to ISO index\n");
                goto fail_io;
        }
 
@@ -849,7 +856,7 @@ static int handlepbp(const char *isofile) {
        for (i = 0; i < compr_img->index_len; i++) {
                ret = fread(&index_entry, 1, sizeof(index_entry), cdHandle);
                if (ret != sizeof(index_entry)) {
-                       fprintf(stderr, "failed to read index_entry #%d\n", i);
+                       SysPrintf("failed to read index_entry #%d\n", i);
                        goto fail_index;
                }
 
@@ -895,18 +902,18 @@ static int handlecbin(const char *isofile) {
 
        ret = fread(&ciso_hdr, 1, sizeof(ciso_hdr), cdHandle);
        if (ret != sizeof(ciso_hdr)) {
-               fprintf(stderr, "failed to read ciso header\n");
+               SysPrintf("failed to read ciso header\n");
                return -1;
        }
 
        if (strncmp(ciso_hdr.magic, "CISO", 4) != 0 || ciso_hdr.total_bytes <= 0 || ciso_hdr.block_size <= 0) {
-               fprintf(stderr, "bad ciso header\n");
+               SysPrintf("bad ciso header\n");
                return -1;
        }
        if (ciso_hdr.header_size != 0 && ciso_hdr.header_size != sizeof(ciso_hdr)) {
                ret = fseek(cdHandle, ciso_hdr.header_size, SEEK_SET);
                if (ret != 0) {
-                       fprintf(stderr, "failed to seek to %x\n", ciso_hdr.header_size);
+                       SysPrintf("failed to seek to %x\n", ciso_hdr.header_size);
                        return -1;
                }
        }
@@ -925,7 +932,7 @@ static int handlecbin(const char *isofile) {
 
        ret = fread(compr_img->index_table, sizeof(compr_img->index_table[0]), compr_img->index_len, cdHandle);
        if (ret != compr_img->index_len) {
-               fprintf(stderr, "failed to read index table\n");
+               SysPrintf("failed to read index table\n");
                goto fail_index;
        }
 
@@ -936,7 +943,7 @@ static int handlecbin(const char *isofile) {
                compr_img->index_table[i] = (index << ciso_hdr.align) | plain;
        }
        if ((long long)index << ciso_hdr.align >= 0x80000000ll)
-               fprintf(stderr, "warning: ciso img too large, expect problems\n");
+               SysPrintf("warning: ciso img too large, expect problems\n");
 
        return 0;
 
@@ -1058,13 +1065,13 @@ static int cdread_compressed(FILE *f, void *dest, int sector, int offset)
        }
 
        if (sector >= compr_img->index_len * 16) {
-               fprintf(stderr, "sector %d is past img end\n", sector);
+               SysPrintf("sector %d is past img end\n", sector);
                return -1;
        }
 
        start_byte = compr_img->index_table[block] & 0x7fffffff;
        if (fseek(cdHandle, start_byte, SEEK_SET) != 0) {
-               fprintf(stderr, "seek error for block %d at %x: ",
+               SysPrintf("seek error for block %d at %x: ",
                        block, start_byte);
                perror(NULL);
                return -1;
@@ -1073,13 +1080,13 @@ static int cdread_compressed(FILE *f, void *dest, int sector, int offset)
        is_compressed = !(compr_img->index_table[block] & 0x80000000);
        size = (compr_img->index_table[block + 1] & 0x7fffffff) - start_byte;
        if (size > sizeof(compr_img->buff_compressed)) {
-               fprintf(stderr, "block %d is too large: %u\n", block, size);
+               SysPrintf("block %d is too large: %u\n", block, size);
                return -1;
        }
 
        if (fread(is_compressed ? compr_img->buff_compressed : compr_img->buff_raw[0],
                                1, size, cdHandle) != size) {
-               fprintf(stderr, "read error for block %d at %x: ", block, start_byte);
+               SysPrintf("read error for block %d at %x: ", block, start_byte);
                perror(NULL);
                return -1;
        }
@@ -1089,12 +1096,12 @@ static int cdread_compressed(FILE *f, void *dest, int sector, int offset)
                cdbuffer_size = cdbuffer_size_expect;
                ret = uncompress2(compr_img->buff_raw[0], &cdbuffer_size, compr_img->buff_compressed, size);
                if (ret != 0) {
-                       fprintf(stderr, "uncompress failed with %d for block %d, sector %d\n",
+                       SysPrintf("uncompress failed with %d for block %d, sector %d\n",
                                        ret, block, sector);
                        return -1;
                }
                if (cdbuffer_size != cdbuffer_size_expect)
-                       fprintf(stderr, "cdbuffer_size: %lu != %lu, sector %d\n", cdbuffer_size,
+                       SysPrintf("cdbuffer_size: %lu != %lu, sector %d\n", cdbuffer_size,
                                        cdbuffer_size_expect, sector);
        }
 
@@ -1223,7 +1230,7 @@ static long CALLBACK ISOopen(void) {
        if (numtracks > 1 && ti[1].handle == NULL) {
                ti[1].handle = fopen(GetIsoFile(), "rb");
        }
-       cdda_cur_sector = cdda_start_sector = 0;
+       cdda_cur_sector = cdda_toc_delta = 0;
 
        return 0;
 }
@@ -1370,19 +1377,22 @@ static long CALLBACK ISOreadTrack(unsigned char *time) {
 // sector: byte 0 - minute; byte 1 - second; byte 2 - frame
 // does NOT uses bcd format
 static long CALLBACK ISOplay(unsigned char *time) {
-       unsigned int i, abs_sect;
-       int file_sect;
+       unsigned int i, abs_sect, start_sect = 0;
+       int track_offset, file_sect;
 
        if (numtracks <= 1)
                return 0;
 
        // find the track
        abs_sect = msf2sec((char *)time);
-       for (i = numtracks; i > 1; i--)
-               if (msf2sec(ti[i].start) <= abs_sect + 2 * 75)
+       for (i = numtracks; i > 1; i--) {
+               start_sect = msf2sec(ti[i].start);
+               if (start_sect <= abs_sect + 2 * 75)
                        break;
+       }
 
-       file_sect = ti[i].start_offset + (abs_sect - msf2sec(ti[i].start));
+       track_offset = abs_sect - start_sect;
+       file_sect = ti[i].start_offset + track_offset;
        if (file_sect < 0)
                file_sect = 0;
 
@@ -1391,13 +1401,12 @@ static long CALLBACK ISOplay(unsigned char *time) {
                if (ti[i].handle != NULL)
                        break;
 
-       cdda_start_sector = abs_sect - file_sect;
-       cddaHandle = ti[i].handle;
+       cdda_first_sector = 0;
+       if (i == 1)
+               cdda_first_sector = file_sect - track_offset;
 
-       if (pregapOffset && (unsigned int)(pregapOffset - file_sect) < 2 * 75) {
-               // get out of the missing pregap to avoid noise
-               file_sect = pregapOffset;
-       }
+       cdda_toc_delta = abs_sect - file_sect;
+       cddaHandle = ti[i].handle;
 
        if (SPU_playCDDAchannel != NULL) {
                startCDDA(file_sect);
@@ -1433,7 +1442,7 @@ static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
                stat->Type = 0x01;
        }
 
-       sec = cdda_start_sector + cdda_cur_sector;
+       sec = cdda_cur_sector + cdda_toc_delta;
        sec2msf(sec, (char *)stat->Time);
 
        return 0;