1 /***************************************************************************
2 * Copyright (C) 2007 PCSX-df Team *
3 * Copyright (C) 2009 Wei Mingzhi *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. *
19 ***************************************************************************/
21 #include "psxcommon.h"
36 static FILE *cdHandle = NULL;
37 static FILE *cddaHandle = NULL;
38 static FILE *subHandle = NULL;
40 static boolean subChanMixed = FALSE;
41 static boolean subChanRaw = FALSE;
42 static boolean subChanMissing = FALSE;
44 static unsigned char cdbuffer[DATA_SIZE];
45 static unsigned char subbuffer[SUB_FRAMESIZE];
47 static unsigned char sndbuffer[CD_FRAMESIZE_RAW * 10];
49 #define CDDA_FRAMETIME (1000 * (sizeof(sndbuffer) / CD_FRAMESIZE_RAW) / 75)
52 static HANDLE threadid;
54 static pthread_t threadid;
56 static unsigned int initial_offset = 0;
57 static boolean playing = FALSE;
58 static boolean cddaBigEndian = FALSE;
59 static unsigned int cdda_cur_sector;
60 static unsigned int cdda_start_sector;
61 /* Frame offset into CD image where pregap data would be found if it was there.
62 * If a game seeks there we must *not* return subchannel data since it's
63 * not in the CD image, so that cdrom code can fake subchannel data instead.
64 * XXX: there could be multiple pregaps but PSX dumps only have one? */
65 static unsigned int pregapOffset;
67 // compressed image stuff
69 unsigned char buff_raw[16][CD_FRAMESIZE_RAW];
70 unsigned char buff_compressed[CD_FRAMESIZE_RAW * 16 + 100];
71 unsigned int *index_table;
72 unsigned int index_len;
73 unsigned int block_shift;
74 unsigned int current_block;
75 unsigned int sector_in_blk;
78 int (*cdimg_read_func)(FILE *f, void *dest, int sector, int offset);
80 char* CALLBACK CDR__getDriveLetter(void);
81 long CALLBACK CDR__configure(void);
82 long CALLBACK CDR__test(void);
83 void CALLBACK CDR__about(void);
84 long CALLBACK CDR__setfilename(char *filename);
85 long CALLBACK CDR__getStatus(struct CdrStat *stat);
87 static void DecodeRawSubData(void);
89 extern void *hCDRDriver;
92 enum {DATA, CDDA} type;
93 char start[3]; // MSF-format
94 char length[3]; // MSF-format
95 FILE *handle; // for multi-track images CDDA
96 int start_offset; // sector offset from start of above file
99 #define MAXTRACKS 100 /* How many tracks can a CD hold? */
101 static int numtracks = 0;
102 static struct trackinfo ti[MAXTRACKS];
104 // get a sector from a msf-array
105 static unsigned int msf2sec(char *msf) {
106 return ((msf[0] * 60 + msf[1]) * 75) + msf[2];
109 static void sec2msf(unsigned int s, char *msf) {
110 msf[0] = s / 75 / 60;
111 s = s - msf[0] * 75 * 60;
117 // divide a string of xx:yy:zz into m, s, f
118 static void tok2msf(char *time, char *msf) {
121 token = strtok(time, ":");
123 msf[0] = atoi(token);
129 token = strtok(NULL, ":");
131 msf[1] = atoi(token);
137 token = strtok(NULL, ":");
139 msf[2] = atoi(token);
147 static long GetTickCount(void) {
148 static time_t initial_time = 0;
151 gettimeofday(&now, NULL);
153 if (initial_time == 0) {
154 initial_time = now.tv_sec;
157 return (now.tv_sec - initial_time) * 1000L + now.tv_usec / 1000L;
161 // this thread plays audio data
163 static void playthread(void *param)
165 static void *playthread(void *param)
168 long osleep, d, t, i, s;
176 for (i = 0; i < sizeof(sndbuffer) / CD_FRAMESIZE_RAW; i++) {
177 d = cdimg_read_func(cddaHandle, sndbuffer + s, cdda_cur_sector, 0);
178 if (d < CD_FRAMESIZE_RAW)
185 if (subHandle != NULL) {
186 fseek(subHandle, cdda_cur_sector * SUB_FRAMESIZE, SEEK_SET);
187 fread(subbuffer, 1, SUB_FRAMESIZE, subHandle);
189 if (subChanRaw) DecodeRawSubData();
198 if (!cdr.Muted && playing) {
200 for (i = 0; i < s / 2; i++) {
201 tmp = sndbuffer[i * 2];
202 sndbuffer[i * 2] = sndbuffer[i * 2 + 1];
203 sndbuffer[i * 2 + 1] = tmp;
208 ret = SPU_playCDDAchannel((short *)sndbuffer, s);
211 } while (ret == 0x7761 && playing); // rearmed_wait
214 if (ret != 0x676f) { // !rearmed_go
218 // HACK: stop feeding data while emu is paused
220 while (stop && playing)
223 now = GetTickCount();
229 else if (osleep > CDDA_FRAMETIME) {
230 osleep = CDDA_FRAMETIME;
234 usleep(osleep * 1000);
248 // stop the CDDA playback
249 static void stopCDDA() {
256 WaitForSingleObject(threadid, INFINITE);
258 pthread_join(threadid, NULL);
262 // start the CDDA playback
263 static void startCDDA(unsigned int sector) {
268 cdda_cur_sector = sector;
272 threadid = (HANDLE)_beginthread(playthread, 0, NULL);
274 pthread_create(&threadid, NULL, playthread, NULL);
278 // this function tries to get the .toc file of the given .bin
279 // the necessary data is put into the ti (trackinformation)-array
280 static int parsetoc(const char *isofile) {
281 char tocname[MAXPATHLEN];
283 char linebuf[256], dummy[256], name[256];
285 char time[20], time2[20];
286 unsigned int t, sector_offs;
290 // copy name of the iso and change extension from .bin to .toc
291 strncpy(tocname, isofile, sizeof(tocname));
292 tocname[MAXPATHLEN - 1] = '\0';
293 if (strlen(tocname) >= 4) {
294 strcpy(tocname + strlen(tocname) - 4, ".toc");
300 if ((fi = fopen(tocname, "r")) == NULL) {
301 // try changing extension to .cue (to satisfy some stupid tutorials)
302 strcpy(tocname + strlen(tocname) - 4, ".cue");
303 if ((fi = fopen(tocname, "r")) == NULL) {
304 // if filename is image.toc.bin, try removing .bin (for Brasero)
305 strcpy(tocname, isofile);
307 if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) {
308 tocname[t - 4] = '\0';
309 if ((fi = fopen(tocname, "r")) == NULL) {
319 memset(&ti, 0, sizeof(ti));
320 cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio
322 sector_offs = 2 * 75;
324 // parse the .toc file
325 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
327 strncpy(dummy, linebuf, sizeof(linebuf));
328 token = strtok(dummy, " ");
330 if (token == NULL) continue;
332 if (!strcmp(token, "TRACK")) {
334 token = strtok(NULL, " ");
337 if (!strncmp(token, "MODE2_RAW", 9)) {
338 ti[numtracks].type = DATA;
339 sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0
341 // check if this image contains mixed subchannel data
342 token = strtok(NULL, " ");
343 if (token != NULL && !strncmp(token, "RW_RAW", 6)) {
348 else if (!strncmp(token, "AUDIO", 5)) {
349 ti[numtracks].type = CDDA;
352 else if (!strcmp(token, "DATAFILE")) {
353 if (ti[numtracks].type == CDDA) {
354 sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2);
355 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
356 ti[numtracks].start_offset = t;
358 sec2msf(t, (char *)&ti[numtracks].start);
359 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
362 sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time);
363 tok2msf((char *)&time, (char *)&ti[numtracks].length);
366 else if (!strcmp(token, "FILE")) {
367 sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2);
368 tok2msf((char *)&time, (char *)&ti[numtracks].start);
369 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
370 ti[numtracks].start_offset = t;
371 t += msf2sec(ti[numtracks].start) + sector_offs;
372 sec2msf(t, (char *)&ti[numtracks].start);
373 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
375 else if (!strcmp(token, "ZERO")) {
376 sscanf(linebuf, "ZERO AUDIO RW_RAW %8s", time);
377 tok2msf((char *)&time, dummy);
378 sector_offs += msf2sec(dummy);
380 t = ti[numtracks - 1].start_offset;
381 pregapOffset = t + msf2sec(ti[numtracks - 1].length);
391 // this function tries to get the .cue file of the given .bin
392 // the necessary data is put into the ti (trackinformation)-array
393 static int parsecue(const char *isofile) {
394 char cuename[MAXPATHLEN];
395 char filepath[MAXPATHLEN];
401 char linebuf[256], tmpb[256], dummy[256];
402 unsigned int incue_max_len;
403 unsigned int t, file_len, sector_offs;
407 // copy name of the iso and change extension from .bin to .cue
408 strncpy(cuename, isofile, sizeof(cuename));
409 cuename[MAXPATHLEN - 1] = '\0';
410 if (strlen(cuename) >= 4) {
411 strcpy(cuename + strlen(cuename) - 4, ".cue");
417 if ((fi = fopen(cuename, "r")) == NULL) {
421 // Some stupid tutorials wrongly tell users to use cdrdao to rip a
422 // "bin/cue" image, which is in fact a "bin/toc" image. So let's check
424 if (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
425 if (!strncmp(linebuf, "CD_ROM_XA", 9)) {
426 // Don't proceed further, as this is actually a .toc file rather
429 return parsetoc(isofile);
431 fseek(fi, 0, SEEK_SET);
434 // build a path for files referenced in .cue
435 strncpy(filepath, cuename, sizeof(filepath));
436 tmp = strrchr(filepath, '/') + 1;
438 tmp = strrchr(filepath, '\\') + 1;
442 filepath[sizeof(filepath) - 1] = 0;
444 incue_max_len = sizeof(filepath) - (tmp - filepath) - 1;
446 memset(&ti, 0, sizeof(ti));
449 sector_offs = 2 * 75;
451 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
452 strncpy(dummy, linebuf, sizeof(linebuf));
453 token = strtok(dummy, " ");
459 if (!strcmp(token, "TRACK")) {
462 if (strstr(linebuf, "AUDIO") != NULL) {
463 ti[numtracks].type = CDDA;
465 else if (strstr(linebuf, "MODE1/2352") != NULL || strstr(linebuf, "MODE2/2352") != NULL) {
466 ti[numtracks].type = DATA;
469 else if (!strcmp(token, "INDEX")) {
470 sscanf(linebuf, " INDEX %02d %8s", &t, time);
471 tok2msf(time, (char *)&ti[numtracks].start);
473 t = msf2sec(ti[numtracks].start);
474 ti[numtracks].start_offset = t;
476 sec2msf(t, ti[numtracks].start);
478 // default track length to file length
479 t = file_len - ti[numtracks].start_offset;
480 sec2msf(t, ti[numtracks].length);
482 if (numtracks > 1 && ti[numtracks].handle == NULL) {
483 // this track uses the same file as the last,
484 // start of this track is last track's end
485 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
486 sec2msf(t, ti[numtracks - 1].length);
488 if (numtracks > 1 && pregapOffset == -1)
489 pregapOffset = ti[numtracks].start_offset;
491 else if (!strcmp(token, "PREGAP")) {
492 if (sscanf(linebuf, " PREGAP %8s", time) == 1) {
493 tok2msf(time, dummy);
494 sector_offs += msf2sec(dummy);
496 pregapOffset = -1; // mark to fill track start_offset
498 else if (!strcmp(token, "FILE")) {
499 sscanf(linebuf, " FILE \"%[^\"]\"", tmpb);
502 ti[numtracks + 1].handle = fopen(tmpb, "rb");
503 if (ti[numtracks + 1].handle == NULL) {
505 tmp = strrchr(tmpb, '\\');
507 tmp = strrchr(tmpb, '/');
512 strncpy(incue_fname, tmp, incue_max_len);
513 ti[numtracks + 1].handle = fopen(filepath, "rb");
516 // update global offset if this is not first file in this .cue
517 if (numtracks + 1 > 1)
518 sector_offs += file_len;
521 if (ti[numtracks + 1].handle == NULL) {
522 SysPrintf(_("\ncould not open: %s\n"), filepath);
525 fseek(ti[numtracks + 1].handle, 0, SEEK_END);
526 file_len = ftell(ti[numtracks + 1].handle) / 2352;
528 if (numtracks == 0 && strlen(isofile) >= 4 &&
529 strcmp(isofile + strlen(isofile) - 4, ".cue") == 0)
531 // user selected .cue as image file, use it's data track instead
533 cdHandle = fopen(filepath, "rb");
543 // this function tries to get the .ccd file of the given .img
544 // the necessary data is put into the ti (trackinformation)-array
545 static int parseccd(const char *isofile) {
546 char ccdname[MAXPATHLEN];
553 // copy name of the iso and change extension from .img to .ccd
554 strncpy(ccdname, isofile, sizeof(ccdname));
555 ccdname[MAXPATHLEN - 1] = '\0';
556 if (strlen(ccdname) >= 4) {
557 strcpy(ccdname + strlen(ccdname) - 4, ".ccd");
563 if ((fi = fopen(ccdname, "r")) == NULL) {
567 memset(&ti, 0, sizeof(ti));
569 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
570 if (!strncmp(linebuf, "[TRACK", 6)){
573 else if (!strncmp(linebuf, "MODE=", 5)) {
574 sscanf(linebuf, "MODE=%d", &t);
575 ti[numtracks].type = ((t == 0) ? CDDA : DATA);
577 else if (!strncmp(linebuf, "INDEX 1=", 8)) {
578 sscanf(linebuf, "INDEX 1=%d", &t);
579 sec2msf(t + 2 * 75, ti[numtracks].start);
580 ti[numtracks].start_offset = t;
582 // If we've already seen another track, this is its end
584 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
585 sec2msf(t, ti[numtracks - 1].length);
592 // Fill out the last track's end based on size
593 if (numtracks >= 1) {
594 fseek(cdHandle, 0, SEEK_END);
595 t = ftell(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75;
596 sec2msf(t, ti[numtracks].length);
602 // this function tries to get the .mds file of the given .mdf
603 // the necessary data is put into the ti (trackinformation)-array
604 static int parsemds(const char *isofile) {
605 char mdsname[MAXPATHLEN];
607 unsigned int offset, extra_offset, l, i;
612 // copy name of the iso and change extension from .mdf to .mds
613 strncpy(mdsname, isofile, sizeof(mdsname));
614 mdsname[MAXPATHLEN - 1] = '\0';
615 if (strlen(mdsname) >= 4) {
616 strcpy(mdsname + strlen(mdsname) - 4, ".mds");
622 if ((fi = fopen(mdsname, "rb")) == NULL) {
626 memset(&ti, 0, sizeof(ti));
628 // check if it's a valid mds file
629 fread(&i, 1, sizeof(unsigned int), fi);
631 if (i != 0x4944454D) {
632 // not an valid mds file
637 // get offset to session block
638 fseek(fi, 0x50, SEEK_SET);
639 fread(&offset, 1, sizeof(unsigned int), fi);
640 offset = SWAP32(offset);
642 // get total number of tracks
644 fseek(fi, offset, SEEK_SET);
645 fread(&s, 1, sizeof(unsigned short), fi);
649 // get offset to track blocks
650 fseek(fi, 4, SEEK_CUR);
651 fread(&offset, 1, sizeof(unsigned int), fi);
652 offset = SWAP32(offset);
656 fseek(fi, offset + 4, SEEK_SET);
657 if (fgetc(fi) < 0xA0) {
663 // check if the image contains mixed subchannel data
664 fseek(fi, offset + 1, SEEK_SET);
665 subChanMixed = (fgetc(fi) ? TRUE : FALSE);
668 for (i = 1; i <= numtracks; i++) {
669 fseek(fi, offset, SEEK_SET);
671 // get the track type
672 ti[i].type = ((fgetc(fi) == 0xA9) ? CDDA : DATA);
673 fseek(fi, 8, SEEK_CUR);
675 // get the track starting point
676 ti[i].start[0] = fgetc(fi);
677 ti[i].start[1] = fgetc(fi);
678 ti[i].start[2] = fgetc(fi);
680 fread(&extra_offset, 1, sizeof(unsigned int), fi);
681 extra_offset = SWAP32(extra_offset);
683 // get track start offset (in .mdf)
684 fseek(fi, offset + 0x28, SEEK_SET);
685 fread(&l, 1, sizeof(unsigned int), fi);
687 ti[i].start_offset = l / CD_FRAMESIZE_RAW;
690 fseek(fi, extra_offset, SEEK_SET);
691 fread(&l, 1, sizeof(unsigned int), fi);
694 pregapOffset = ti[i].start_offset;
696 // get the track length
697 fread(&l, 1, sizeof(unsigned int), fi);
699 sec2msf(l, ti[i].length);
708 static int handlepbp(const char *isofile) {
711 unsigned int dontcare[8];
712 unsigned int psar_offs;
725 unsigned int dontcare[6];
728 unsigned int t, cd_length, cdimg_base;
729 const char *ext = NULL;
732 if (strlen(isofile) >= 4)
733 ext = isofile + strlen(isofile) - 4;
734 if (ext == NULL || (strcmp(ext, ".pbp") != 0 && strcmp(ext, ".PBP") != 0))
739 ret = fread(&pbp_hdr, 1, sizeof(pbp_hdr), cdHandle);
740 if (ret != sizeof(pbp_hdr)) {
741 fprintf(stderr, "failed to read pbp\n");
745 ret = fseek(cdHandle, pbp_hdr.psar_offs, SEEK_SET);
747 fprintf(stderr, "failed to seek to %x\n", pbp_hdr.psar_offs);
751 fread(psar_sig, 1, sizeof(psar_sig), cdHandle);
753 if (strcmp(psar_sig, "PSISOIMG") != 0) {
754 fprintf(stderr, "bad psar_sig: %s\n", psar_sig);
759 ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x800, SEEK_SET);
761 fprintf(stderr, "failed to seek to %x\n", pbp_hdr.psar_offs);
765 // first 3 entries are special
766 fseek(cdHandle, sizeof(toc_entry), SEEK_CUR);
767 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
768 numtracks = btoi(toc_entry.index1[0]);
770 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
771 cd_length = btoi(toc_entry.index1[0]) * 60 * 75 +
772 btoi(toc_entry.index1[1]) * 75 + btoi(toc_entry.index1[2]);
774 for (i = 1; i <= numtracks; i++) {
775 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
777 ti[i].type = (toc_entry.type == 1) ? CDDA : DATA;
779 ti[i].start_offset = btoi(toc_entry.index0[0]) * 60 * 75 +
780 btoi(toc_entry.index0[1]) * 75 + btoi(toc_entry.index0[2]);
781 ti[i].start[0] = btoi(toc_entry.index1[0]);
782 ti[i].start[1] = btoi(toc_entry.index1[1]);
783 ti[i].start[2] = btoi(toc_entry.index1[2]);
786 t = msf2sec(ti[i].start) - msf2sec(ti[i - 1].start);
787 sec2msf(t, ti[i - 1].length);
790 t = cd_length - ti[numtracks].start_offset;
791 sec2msf(t, ti[numtracks].length);
794 ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x4000, SEEK_SET);
796 fprintf(stderr, "failed to seek to ISO index\n");
800 compr_img = calloc(1, sizeof(*compr_img));
801 if (compr_img == NULL)
804 compr_img->block_shift = 4;
805 compr_img->current_block = (unsigned int)-1;
807 compr_img->index_len = (0x100000 - 0x4000) / sizeof(index_entry);
808 compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
809 if (compr_img->index_table == NULL)
812 cdimg_base = pbp_hdr.psar_offs + 0x100000;
813 for (i = 0; i < compr_img->index_len; i++) {
814 ret = fread(&index_entry, 1, sizeof(index_entry), cdHandle);
815 if (ret != sizeof(index_entry)) {
816 fprintf(stderr, "failed to read index_entry #%d\n", i);
820 if (index_entry.size == 0)
823 compr_img->index_table[i] = cdimg_base + index_entry.offset;
825 compr_img->index_table[i] = cdimg_base + index_entry.offset + index_entry.size;
830 free(compr_img->index_table);
831 compr_img->index_table = NULL;
833 if (compr_img != NULL) {
840 static int handlecbin(const char *isofile) {
844 unsigned int header_size;
845 unsigned long long total_bytes;
846 unsigned int block_size;
847 unsigned char ver; // 1
849 unsigned char rsv_06[2];
851 const char *ext = NULL;
852 unsigned int index, plain;
855 if (strlen(isofile) >= 5)
856 ext = isofile + strlen(isofile) - 5;
857 if (ext == NULL || (strcasecmp(ext + 1, ".cbn") != 0 && strcasecmp(ext, ".cbin") != 0))
860 ret = fread(&ciso_hdr, 1, sizeof(ciso_hdr), cdHandle);
861 if (ret != sizeof(ciso_hdr)) {
862 fprintf(stderr, "failed to read ciso header\n");
866 if (strncmp(ciso_hdr.magic, "CISO", 4) != 0 || ciso_hdr.total_bytes <= 0 || ciso_hdr.block_size <= 0) {
867 fprintf(stderr, "bad ciso header\n");
870 if (ciso_hdr.header_size != 0 && ciso_hdr.header_size != sizeof(ciso_hdr)) {
871 ret = fseek(cdHandle, ciso_hdr.header_size, SEEK_SET);
873 fprintf(stderr, "failed to seek to %x\n", ciso_hdr.header_size);
878 compr_img = calloc(1, sizeof(*compr_img));
879 if (compr_img == NULL)
882 compr_img->block_shift = 0;
883 compr_img->current_block = (unsigned int)-1;
885 compr_img->index_len = ciso_hdr.total_bytes / ciso_hdr.block_size;
886 compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
887 if (compr_img->index_table == NULL)
890 ret = fread(compr_img->index_table, sizeof(compr_img->index_table[0]), compr_img->index_len, cdHandle);
891 if (ret != compr_img->index_len) {
892 fprintf(stderr, "failed to read index table\n");
896 for (i = 0; i < compr_img->index_len + 1; i++) {
897 index = compr_img->index_table[i];
898 plain = index & 0x80000000;
900 compr_img->index_table[i] = (index << ciso_hdr.align) | plain;
902 if ((long long)index << ciso_hdr.align >= 0x80000000ll)
903 fprintf(stderr, "warning: ciso img too large, expect problems\n");
908 free(compr_img->index_table);
909 compr_img->index_table = NULL;
911 if (compr_img != NULL) {
918 // this function tries to get the .sub file of the given .img
919 static int opensubfile(const char *isoname) {
920 char subname[MAXPATHLEN];
922 // copy name of the iso and change extension from .img to .sub
923 strncpy(subname, isoname, sizeof(subname));
924 subname[MAXPATHLEN - 1] = '\0';
925 if (strlen(subname) >= 4) {
926 strcpy(subname + strlen(subname) - 4, ".sub");
932 subHandle = fopen(subname, "rb");
933 if (subHandle == NULL) {
940 static int opensbifile(const char *isoname) {
941 char sbiname[MAXPATHLEN];
944 strncpy(sbiname, isoname, sizeof(sbiname));
945 sbiname[MAXPATHLEN - 1] = '\0';
946 if (strlen(sbiname) >= 4) {
947 strcpy(sbiname + strlen(sbiname) - 4, ".sbi");
953 fseek(cdHandle, 0, SEEK_END);
954 s = ftell(cdHandle) / 2352;
956 return LoadSBI(sbiname, s);
959 static int cdread_normal(FILE *f, void *dest, int sector, int offset)
961 fseek(f, sector * CD_FRAMESIZE_RAW + offset, SEEK_SET);
962 return fread(dest, 1, CD_FRAMESIZE_RAW - offset, f);
965 static int cdread_sub_mixed(FILE *f, void *dest, int sector, int offset)
969 fseek(f, sector * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE) + offset, SEEK_SET);
970 ret = fread(dest, 1, CD_FRAMESIZE_RAW - offset, f);
971 fread(subbuffer, 1, SUB_FRAMESIZE, f);
973 if (subChanRaw) DecodeRawSubData();
978 static int uncompress2(void *out, unsigned long *out_size, void *in, unsigned long in_size)
983 if (z.zalloc == NULL) {
984 // XXX: one-time leak here..
990 ret = inflateInit2(&z, -15);
993 ret = inflateReset(&z);
998 z.avail_in = in_size;
1000 z.avail_out = *out_size;
1002 ret = inflate(&z, Z_NO_FLUSH);
1005 *out_size -= z.avail_out;
1006 return ret == 1 ? 0 : ret;
1009 static int cdread_compressed(FILE *f, void *dest, int sector, int offset)
1011 unsigned long cdbuffer_size, cdbuffer_size_expect;
1012 unsigned int start_byte, size;
1016 block = sector >> compr_img->block_shift;
1017 compr_img->sector_in_blk = sector & ((1 << compr_img->block_shift) - 1);
1019 if (block == compr_img->current_block) {
1020 //printf("hit sect %d\n", sector);
1024 if (sector >= compr_img->index_len * 16) {
1025 fprintf(stderr, "sector %d is past img end\n", sector);
1029 start_byte = compr_img->index_table[block] & 0x7fffffff;
1030 if (fseek(cdHandle, start_byte, SEEK_SET) != 0) {
1031 fprintf(stderr, "seek error for block %d at %x: ",
1037 is_compressed = !(compr_img->index_table[block] & 0x80000000);
1038 size = (compr_img->index_table[block + 1] & 0x7fffffff) - start_byte;
1039 if (size > sizeof(compr_img->buff_compressed)) {
1040 fprintf(stderr, "block %d is too large: %u\n", block, size);
1044 if (fread(is_compressed ? compr_img->buff_compressed : compr_img->buff_raw[0],
1045 1, size, cdHandle) != size) {
1046 fprintf(stderr, "read error for block %d at %x: ", block, start_byte);
1051 if (is_compressed) {
1052 cdbuffer_size_expect = sizeof(compr_img->buff_raw[0]) << compr_img->block_shift;
1053 cdbuffer_size = cdbuffer_size_expect;
1054 ret = uncompress2(compr_img->buff_raw[0], &cdbuffer_size, compr_img->buff_compressed, size);
1056 fprintf(stderr, "uncompress failed with %d for block %d, sector %d\n",
1057 ret, block, sector);
1060 if (cdbuffer_size != cdbuffer_size_expect)
1061 fprintf(stderr, "cdbuffer_size: %lu != %lu, sector %d\n", cdbuffer_size,
1062 cdbuffer_size_expect, sector);
1066 compr_img->current_block = block;
1069 if (dest != cdbuffer) // copy avoid HACK
1070 memcpy(dest, compr_img->buff_raw[compr_img->sector_in_blk] + offset,
1071 CD_FRAMESIZE_RAW - offset);
1072 return CD_FRAMESIZE_RAW - offset;
1075 static int cdread_2048(FILE *f, void *dest, int sector, int offset)
1079 fseek(f, sector * 2048, SEEK_SET);
1080 ret = fread((char *)dest + 12, 1, 2048, f);
1082 // not really necessary, fake mode 2 header
1083 memset(cdbuffer, 0, 12);
1084 sec2msf(sector + 2 * 75, (char *)cdbuffer);
1090 static unsigned char * CALLBACK ISOgetBuffer_compr(void) {
1091 return compr_img->buff_raw[compr_img->sector_in_blk] + 12;
1094 static unsigned char * CALLBACK ISOgetBuffer(void) {
1098 static void PrintTracks(void) {
1101 for (i = 1; i <= numtracks; i++) {
1102 SysPrintf(_("Track %.2d (%s) - Start %.2d:%.2d:%.2d, Length %.2d:%.2d:%.2d\n"),
1103 i, (ti[i].type == DATA ? "DATA" : "AUDIO"),
1104 ti[i].start[0], ti[i].start[1], ti[i].start[2],
1105 ti[i].length[0], ti[i].length[1], ti[i].length[2]);
1109 // This function is invoked by the front-end when opening an ISO
1110 // file for playback
1111 static long CALLBACK ISOopen(void) {
1112 boolean isMode1ISO = FALSE;
1114 if (cdHandle != NULL) {
1115 return 0; // it's already open
1118 cdHandle = fopen(GetIsoFile(), "rb");
1119 if (cdHandle == NULL) {
1123 SysPrintf(_("Loaded CD Image: %s"), GetIsoFile());
1125 cddaBigEndian = FALSE;
1126 subChanMixed = FALSE;
1130 CDR_getBuffer = ISOgetBuffer;
1131 cdimg_read_func = cdread_normal;
1133 if (parsecue(GetIsoFile()) == 0) {
1134 SysPrintf("[+cue]");
1136 else if (parsetoc(GetIsoFile()) == 0) {
1137 SysPrintf("[+toc]");
1139 else if (parseccd(GetIsoFile()) == 0) {
1140 SysPrintf("[+ccd]");
1142 else if (parsemds(GetIsoFile()) == 0) {
1143 SysPrintf("[+mds]");
1145 if (handlepbp(GetIsoFile()) == 0) {
1147 CDR_getBuffer = ISOgetBuffer_compr;
1148 cdimg_read_func = cdread_compressed;
1150 else if (handlecbin(GetIsoFile()) == 0) {
1151 SysPrintf("[cbin]");
1152 CDR_getBuffer = ISOgetBuffer_compr;
1153 cdimg_read_func = cdread_compressed;
1156 if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
1157 SysPrintf("[+sub]");
1159 if (opensbifile(GetIsoFile()) == 0) {
1160 SysPrintf("[+sbi]");
1163 // guess whether it is mode1/2048
1164 fseek(cdHandle, 0, SEEK_END);
1165 if (ftell(cdHandle) % 2048 == 0) {
1166 unsigned int modeTest = 0;
1167 fseek(cdHandle, 0, SEEK_SET);
1168 fread(&modeTest, 4, 1, cdHandle);
1169 if (SWAP32(modeTest) != 0xffffff00) {
1170 SysPrintf("[2048]");
1174 fseek(cdHandle, 0, SEEK_SET);
1181 cdimg_read_func = cdread_sub_mixed;
1182 else if (isMode1ISO)
1183 cdimg_read_func = cdread_2048;
1185 // make sure we have another handle open for cdda
1186 if (numtracks > 1 && ti[1].handle == NULL) {
1187 ti[1].handle = fopen(GetIsoFile(), "rb");
1189 cdda_cur_sector = cdda_start_sector = 0;
1194 static long CALLBACK ISOclose(void) {
1197 if (cdHandle != NULL) {
1201 if (subHandle != NULL) {
1208 if (compr_img != NULL) {
1209 free(compr_img->index_table);
1214 for (i = 1; i <= numtracks; i++) {
1215 if (ti[i].handle != NULL) {
1216 fclose(ti[i].handle);
1217 ti[i].handle = NULL;
1226 static long CALLBACK ISOinit(void) {
1227 assert(cdHandle == NULL);
1228 assert(subHandle == NULL);
1230 return 0; // do nothing
1233 static long CALLBACK ISOshutdown(void) {
1238 // return Starting and Ending Track
1240 // byte 0 - start track
1241 // byte 1 - end track
1242 static long CALLBACK ISOgetTN(unsigned char *buffer) {
1245 if (numtracks > 0) {
1246 buffer[1] = numtracks;
1255 // return Track Time
1260 static long CALLBACK ISOgetTD(unsigned char track, unsigned char *buffer) {
1262 // CD length according pcsxr-svn (done a bit different here)
1264 unsigned char time[3];
1265 sect = msf2sec(ti[numtracks].start) + msf2sec(ti[numtracks].length);
1266 sec2msf(sect, (char *)time);
1267 buffer[2] = time[0];
1268 buffer[1] = time[1];
1269 buffer[0] = time[2];
1271 else if (numtracks > 0 && track <= numtracks) {
1272 buffer[2] = ti[track].start[0];
1273 buffer[1] = ti[track].start[1];
1274 buffer[0] = ti[track].start[2];
1285 // decode 'raw' subchannel data ripped by cdrdao
1286 static void DecodeRawSubData(void) {
1287 unsigned char subQData[12];
1290 memset(subQData, 0, sizeof(subQData));
1292 for (i = 0; i < 8 * 12; i++) {
1293 if (subbuffer[i] & (1 << 6)) { // only subchannel Q is needed
1294 subQData[i >> 3] |= (1 << (7 - (i & 7)));
1298 memcpy(&subbuffer[12], subQData, 12);
1302 // time: byte 0 - minute; byte 1 - second; byte 2 - frame
1304 static long CALLBACK ISOreadTrack(unsigned char *time) {
1305 int sector = MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2]));
1307 if (cdHandle == NULL) {
1312 subChanMissing = FALSE;
1313 if (sector >= pregapOffset) {
1315 if (sector < pregapOffset)
1316 subChanMissing = TRUE;
1320 cdimg_read_func(cdHandle, cdbuffer, sector, 12);
1322 if (subHandle != NULL) {
1323 fseek(subHandle, sector * SUB_FRAMESIZE, SEEK_SET);
1324 fread(subbuffer, 1, SUB_FRAMESIZE, subHandle);
1326 if (subChanRaw) DecodeRawSubData();
1333 // sector: byte 0 - minute; byte 1 - second; byte 2 - frame
1334 // does NOT uses bcd format
1335 static long CALLBACK ISOplay(unsigned char *time) {
1336 unsigned int i, abs_sect;
1343 abs_sect = msf2sec((char *)time);
1344 for (i = numtracks; i > 1; i--)
1345 if (msf2sec(ti[i].start) <= abs_sect + 2 * 75)
1348 file_sect = ti[i].start_offset + (abs_sect - msf2sec(ti[i].start));
1352 // find the file that contains this track
1354 if (ti[i].handle != NULL)
1357 cdda_start_sector = abs_sect - file_sect;
1358 cddaHandle = ti[i].handle;
1360 if (pregapOffset && (unsigned int)(pregapOffset - file_sect) < 2 * 75) {
1361 // get out of the missing pregap to avoid noise
1362 file_sect = pregapOffset;
1365 if (SPU_playCDDAchannel != NULL) {
1366 startCDDA(file_sect);
1372 static long CALLBACK ISOstop(void) {
1377 // gets subchannel data
1378 static unsigned char* CALLBACK ISOgetBufferSub(void) {
1379 if ((subHandle != NULL || subChanMixed) && !subChanMissing) {
1386 static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
1389 CDR__getStatus(stat);
1393 stat->Status |= 0x80;
1399 sec = cdda_start_sector + cdda_cur_sector;
1400 sec2msf(sec, (char *)stat->Time);
1405 void cdrIsoInit(void) {
1407 CDR_shutdown = ISOshutdown;
1409 CDR_close = ISOclose;
1410 CDR_getTN = ISOgetTN;
1411 CDR_getTD = ISOgetTD;
1412 CDR_readTrack = ISOreadTrack;
1413 CDR_getBuffer = ISOgetBuffer;
1416 CDR_getBufferSub = ISOgetBufferSub;
1417 CDR_getStatus = ISOgetStatus;
1419 CDR_getDriveLetter = CDR__getDriveLetter;
1420 CDR_configure = CDR__configure;
1421 CDR_test = CDR__test;
1422 CDR_about = CDR__about;
1423 CDR_setfilename = CDR__setfilename;
1428 int cdrIsoActive(void) {
1429 return (cdHandle != NULL);