X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=pcsx_rearmed.git;a=blobdiff_plain;f=libpcsxcore%2Fcdriso.c;h=f2886b035d588a9863b081f4ec5afb3addc9dd47;hp=c29fd775d08c34641c030147d3f9c08fafe1b907;hb=349f7d81b5f776ab69533fcb4e9c4904235b90fd;hpb=9a92bffbed92c9b6bd7d0ddde7c0982a8590959e diff --git a/libpcsxcore/cdriso.c b/libpcsxcore/cdriso.c index c29fd775..f2886b03 100644 --- a/libpcsxcore/cdriso.c +++ b/libpcsxcore/cdriso.c @@ -30,9 +30,13 @@ #else #include #include +#include #endif #include +unsigned int cdrIsoMultidiskCount; +unsigned int cdrIsoMultidiskSelect; + static FILE *cdHandle = NULL; static FILE *cddaHandle = NULL; static FILE *subHandle = NULL; @@ -56,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. @@ -70,6 +77,7 @@ static struct { unsigned char buff_compressed[CD_FRAMESIZE_RAW * 16 + 100]; unsigned int *index_table; unsigned int index_len; + unsigned int block_shift; unsigned int current_block; unsigned int sector_in_blk; } *compr_img; @@ -164,50 +172,26 @@ static void playthread(void *param) static void *playthread(void *param) #endif { - long d, t, i, s; + long osleep, d, t, i, s; unsigned char tmp; + int ret = 0; t = GetTickCount(); while (playing) { - d = t - (long)GetTickCount(); - if (d <= 0) { - d = 1; - } - else if (d > CDDA_FRAMETIME) { - d = CDDA_FRAMETIME; - } -#ifdef _WIN32 - Sleep(d); -#else - usleep(d * 1000); -#endif - // HACK: stop feeding data while emu is paused - extern int stop; - if (stop) { - usleep(100000); - continue; - } - - t = GetTickCount() + CDDA_FRAMETIME; - s = 0; for (i = 0; i < sizeof(sndbuffer) / CD_FRAMESIZE_RAW; i++) { d = cdimg_read_func(cddaHandle, sndbuffer + s, cdda_cur_sector, 0); 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++; } - if (subHandle != NULL) { - fseek(subHandle, cdda_cur_sector * SUB_FRAMESIZE, SEEK_SET); - fread(subbuffer, 1, SUB_FRAMESIZE, subHandle); - - if (subChanRaw) DecodeRawSubData(); - } - if (s == 0) { playing = FALSE; initial_offset = 0; @@ -223,8 +207,37 @@ static void *playthread(void *param) } } - SPU_playCDDAchannel((short *)sndbuffer, s); + do { + ret = SPU_playCDDAchannel((short *)sndbuffer, s); + if (ret == 0x7761) + usleep(6 * 1000); + } while (ret == 0x7761 && playing); // rearmed_wait } + + if (ret != 0x676f) { // !rearmed_go + // do approx sleep + long now; + + // HACK: stop feeding data while emu is paused + extern int stop; + while (stop && playing) + usleep(10000); + + now = GetTickCount(); + osleep = t - now; + if (osleep <= 0) { + osleep = 1; + t = now; + } + else if (osleep > CDDA_FRAMETIME) { + osleep = CDDA_FRAMETIME; + t = now; + } + + usleep(osleep * 1000); + t += CDDA_FRAMETIME; + } + } #ifdef _WIN32 @@ -695,7 +708,7 @@ static int parsemds(const char *isofile) { return 0; } -static int parsepbp(const char *isofile) { +static int handlepbp(const char *isofile) { struct { unsigned int sig; unsigned int dontcare[8]; @@ -714,8 +727,9 @@ static int parsepbp(const char *isofile) { unsigned int size; unsigned int dontcare[6]; } index_entry; - char psar_sig[9]; + char psar_sig[11]; unsigned int t, cd_length, cdimg_base; + unsigned int offsettab[8], psisoimg_offs; const char *ext = NULL; int i, ret; @@ -728,27 +742,66 @@ static int parsepbp(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; } + psisoimg_offs = pbp_hdr.psar_offs; fread(psar_sig, 1, sizeof(psar_sig), cdHandle); - psar_sig[8] = 0; - if (strcmp(psar_sig, "PSISOIMG") != 0) { - fprintf(stderr, "bad psar_sig: %s\n", psar_sig); + psar_sig[10] = 0; + if (strcmp(psar_sig, "PSTITLEIMG") == 0) { + // multidisk image? + ret = fseek(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; + } + + if (fread(&offsettab, 1, sizeof(offsettab), cdHandle) != sizeof(offsettab)) { + SysPrintf("failed to read offsettab\n"); + goto fail_io; + } + + for (i = 0; i < sizeof(offsettab) / sizeof(offsettab[0]); i++) { + if (offsettab[i] == 0) + break; + } + cdrIsoMultidiskCount = i; + if (cdrIsoMultidiskCount == 0) { + SysPrintf("multidisk eboot has 0 images?\n"); + goto fail_io; + } + + if (cdrIsoMultidiskSelect >= cdrIsoMultidiskCount) + cdrIsoMultidiskSelect = 0; + + psisoimg_offs += offsettab[cdrIsoMultidiskSelect]; + + ret = fseek(cdHandle, psisoimg_offs, SEEK_SET); + if (ret != 0) { + SysPrintf("failed to seek to %x\n", psisoimg_offs); + goto fail_io; + } + + fread(psar_sig, 1, sizeof(psar_sig), cdHandle); + psar_sig[10] = 0; + } + + if (strcmp(psar_sig, "PSISOIMG00") != 0) { + SysPrintf("bad psar_sig: %s\n", psar_sig); goto fail_io; } // seek to TOC - ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x800, SEEK_SET); + ret = fseek(cdHandle, psisoimg_offs + 0x800, SEEK_SET); if (ret != 0) { - fprintf(stderr, "failed to seek to %x\n", pbp_hdr.psar_offs); + SysPrintf("failed to seek to %x\n", psisoimg_offs + 0x800); goto fail_io; } @@ -781,9 +834,9 @@ static int parsepbp(const char *isofile) { sec2msf(t, ti[numtracks].length); // seek to ISO index - ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x4000, SEEK_SET); + 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; } @@ -791,6 +844,7 @@ static int parsepbp(const char *isofile) { if (compr_img == NULL) goto fail_io; + compr_img->block_shift = 4; compr_img->current_block = (unsigned int)-1; compr_img->index_len = (0x100000 - 0x4000) / sizeof(index_entry); @@ -798,11 +852,11 @@ static int parsepbp(const char *isofile) { if (compr_img->index_table == NULL) goto fail_io; - cdimg_base = pbp_hdr.psar_offs + 0x100000; + cdimg_base = psisoimg_offs + 0x100000; 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; } @@ -819,6 +873,88 @@ fail_index: free(compr_img->index_table); compr_img->index_table = NULL; fail_io: + if (compr_img != NULL) { + free(compr_img); + compr_img = NULL; + } + return -1; +} + +static int handlecbin(const char *isofile) { + struct + { + char magic[4]; + unsigned int header_size; + unsigned long long total_bytes; + unsigned int block_size; + unsigned char ver; // 1 + unsigned char align; + unsigned char rsv_06[2]; + } ciso_hdr; + const char *ext = NULL; + unsigned int index = 0, plain; + int i, ret; + + if (strlen(isofile) >= 5) + ext = isofile + strlen(isofile) - 5; + if (ext == NULL || (strcasecmp(ext + 1, ".cbn") != 0 && strcasecmp(ext, ".cbin") != 0)) + return -1; + + ret = fread(&ciso_hdr, 1, sizeof(ciso_hdr), cdHandle); + if (ret != sizeof(ciso_hdr)) { + 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) { + 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) { + SysPrintf("failed to seek to %x\n", ciso_hdr.header_size); + return -1; + } + } + + compr_img = calloc(1, sizeof(*compr_img)); + if (compr_img == NULL) + goto fail_io; + + compr_img->block_shift = 0; + 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) + goto fail_io; + + ret = fread(compr_img->index_table, sizeof(compr_img->index_table[0]), compr_img->index_len, cdHandle); + if (ret != compr_img->index_len) { + SysPrintf("failed to read index table\n"); + goto fail_index; + } + + for (i = 0; i < compr_img->index_len + 1; i++) { + index = compr_img->index_table[i]; + plain = index & 0x80000000; + index &= 0x7fffffff; + compr_img->index_table[i] = (index << ciso_hdr.align) | plain; + } + 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; +fail_io: + if (compr_img != NULL) { + free(compr_img); + compr_img = NULL; + } return -1; } @@ -915,12 +1051,13 @@ static int uncompress2(void *out, unsigned long *out_size, void *in, unsigned lo static int cdread_compressed(FILE *f, void *dest, int sector, int offset) { + unsigned long cdbuffer_size, cdbuffer_size_expect; unsigned int start_byte, size; - unsigned long cdbuffer_size; + int is_compressed; int ret, block; - block = sector >> 4; - compr_img->sector_in_blk = sector & 15; + block = sector >> compr_img->block_shift; + compr_img->sector_in_blk = sector & ((1 << compr_img->block_shift) - 1); if (block == compr_img->current_block) { //printf("hit sect %d\n", sector); @@ -928,40 +1065,45 @@ 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]; + 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; } - size = compr_img->index_table[block + 1] - start_byte; + 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(compr_img->buff_compressed, 1, size, cdHandle) != size) { - fprintf(stderr, "read error for block %d at %x: ", block, start_byte); + if (fread(is_compressed ? compr_img->buff_compressed : compr_img->buff_raw[0], + 1, size, cdHandle) != size) { + SysPrintf("read error for block %d at %x: ", block, start_byte); perror(NULL); return -1; } - cdbuffer_size = sizeof(compr_img->buff_raw[0]) * 16; - 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", - ret, block, sector); - return -1; + 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); + if (ret != 0) { + SysPrintf("uncompress failed with %d for block %d, sector %d\n", + ret, block, sector); + return -1; + } + if (cdbuffer_size != cdbuffer_size_expect) + SysPrintf("cdbuffer_size: %lu != %lu, sector %d\n", cdbuffer_size, + cdbuffer_size_expect, sector); } - if (cdbuffer_size != sizeof(compr_img->buff_raw[0]) * 16) - fprintf(stderr, "cdbuffer_size: %lu != %d, sector %d\n", cdbuffer_size, - sizeof(compr_img->buff_raw[0]) * 16, sector); // done at last! compr_img->current_block = block; @@ -1027,6 +1169,7 @@ static long CALLBACK ISOopen(void) { subChanMixed = FALSE; subChanRaw = FALSE; pregapOffset = 0; + cdrIsoMultidiskCount = 1; CDR_getBuffer = ISOgetBuffer; cdimg_read_func = cdread_normal; @@ -1043,11 +1186,16 @@ static long CALLBACK ISOopen(void) { else if (parsemds(GetIsoFile()) == 0) { SysPrintf("[+mds]"); } - else if (parsepbp(GetIsoFile()) == 0) { + if (handlepbp(GetIsoFile()) == 0) { SysPrintf("[pbp]"); CDR_getBuffer = ISOgetBuffer_compr; cdimg_read_func = cdread_compressed; } + else if (handlecbin(GetIsoFile()) == 0) { + SysPrintf("[cbin]"); + CDR_getBuffer = ISOgetBuffer_compr; + cdimg_read_func = cdread_compressed; + } if (!subChanMixed && opensubfile(GetIsoFile()) == 0) { SysPrintf("[+sub]"); @@ -1082,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; } @@ -1229,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; @@ -1250,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); @@ -1292,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;