X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?p=pcsx_rearmed.git;a=blobdiff_plain;f=libpcsxcore%2Fcdriso.c;h=6bf5809559451722561cbce0a99421a34925571e;hp=c29fd775d08c34641c030147d3f9c08fafe1b907;hb=f2de172b46a4948242a53b4b7c24f9fc151c19b5;hpb=9a92bffbed92c9b6bd7d0ddde7c0982a8590959e diff --git a/libpcsxcore/cdriso.c b/libpcsxcore/cdriso.c index c29fd775..6bf58095 100644 --- a/libpcsxcore/cdriso.c +++ b/libpcsxcore/cdriso.c @@ -70,6 +70,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,33 +165,13 @@ 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); @@ -223,8 +204,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 +705,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]; @@ -791,6 +801,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); @@ -819,6 +830,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, 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)) { + fprintf(stderr, "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"); + 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); + 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) { + fprintf(stderr, "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) + fprintf(stderr, "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 +1008,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); @@ -932,7 +1026,7 @@ static int cdread_compressed(FILE *f, void *dest, int sector, int offset) 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: ", block, start_byte); @@ -940,28 +1034,33 @@ static int cdread_compressed(FILE *f, void *dest, int sector, int offset) 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); return -1; } - if (fread(compr_img->buff_compressed, 1, size, cdHandle) != size) { + 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); 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) { + fprintf(stderr, "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, + 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; @@ -1043,11 +1142,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]");