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"
37 unsigned int cdrIsoMultidiskCount;
38 unsigned int cdrIsoMultidiskSelect;
40 static FILE *cdHandle = NULL;
41 static FILE *cddaHandle = NULL;
42 static FILE *subHandle = NULL;
44 static boolean subChanMixed = FALSE;
45 static boolean subChanRaw = FALSE;
46 static boolean subChanMissing = FALSE;
48 static unsigned char cdbuffer[CD_FRAMESIZE_RAW];
49 static unsigned char subbuffer[SUB_FRAMESIZE];
51 static unsigned char sndbuffer[CD_FRAMESIZE_RAW * 10];
53 #define CDDA_FRAMETIME (1000 * (sizeof(sndbuffer) / CD_FRAMESIZE_RAW) / 75)
56 static HANDLE threadid;
58 static pthread_t threadid;
60 static unsigned int initial_offset = 0;
61 static boolean playing = FALSE;
62 static boolean cddaBigEndian = FALSE;
63 // offsets of cddaHandle file
64 static unsigned int cdda_cur_sector;
65 static unsigned int cdda_first_sector;
66 // toc_sector - file_sector
67 static unsigned int cdda_toc_delta;
68 /* Frame offset into CD image where pregap data would be found if it was there.
69 * If a game seeks there we must *not* return subchannel data since it's
70 * not in the CD image, so that cdrom code can fake subchannel data instead.
71 * XXX: there could be multiple pregaps but PSX dumps only have one? */
72 static unsigned int pregapOffset;
74 // compressed image stuff
76 unsigned char buff_raw[16][CD_FRAMESIZE_RAW];
77 unsigned char buff_compressed[CD_FRAMESIZE_RAW * 16 + 100];
78 unsigned int *index_table;
79 unsigned int index_len;
80 unsigned int block_shift;
81 unsigned int current_block;
82 unsigned int sector_in_blk;
85 int (*cdimg_read_func)(FILE *f, void *dest, int sector);
87 char* CALLBACK CDR__getDriveLetter(void);
88 long CALLBACK CDR__configure(void);
89 long CALLBACK CDR__test(void);
90 void CALLBACK CDR__about(void);
91 long CALLBACK CDR__setfilename(char *filename);
92 long CALLBACK CDR__getStatus(struct CdrStat *stat);
94 static void DecodeRawSubData(void);
96 extern void *hCDRDriver;
99 enum {DATA, CDDA} type;
100 char start[3]; // MSF-format
101 char length[3]; // MSF-format
102 FILE *handle; // for multi-track images CDDA
103 int start_offset; // sector offset from start of above file
106 #define MAXTRACKS 100 /* How many tracks can a CD hold? */
108 static int numtracks = 0;
109 static struct trackinfo ti[MAXTRACKS];
111 // get a sector from a msf-array
112 static unsigned int msf2sec(char *msf) {
113 return ((msf[0] * 60 + msf[1]) * 75) + msf[2];
116 static void sec2msf(unsigned int s, char *msf) {
117 msf[0] = s / 75 / 60;
118 s = s - msf[0] * 75 * 60;
124 // divide a string of xx:yy:zz into m, s, f
125 static void tok2msf(char *time, char *msf) {
128 token = strtok(time, ":");
130 msf[0] = atoi(token);
136 token = strtok(NULL, ":");
138 msf[1] = atoi(token);
144 token = strtok(NULL, ":");
146 msf[2] = atoi(token);
154 static long GetTickCount(void) {
155 static time_t initial_time = 0;
158 gettimeofday(&now, NULL);
160 if (initial_time == 0) {
161 initial_time = now.tv_sec;
164 return (now.tv_sec - initial_time) * 1000L + now.tv_usec / 1000L;
168 // this thread plays audio data
170 static void playthread(void *param)
172 static void *playthread(void *param)
175 long osleep, d, t, i, s;
183 for (i = 0; i < sizeof(sndbuffer) / CD_FRAMESIZE_RAW; i++) {
184 d = cdimg_read_func(cddaHandle, sndbuffer + s, cdda_cur_sector);
185 if (d < CD_FRAMESIZE_RAW)
188 if (cdda_cur_sector < cdda_first_sector)
189 memset(sndbuffer + s, 0, CD_FRAMESIZE_RAW);
201 if (!cdr.Muted && playing) {
203 for (i = 0; i < s / 2; i++) {
204 tmp = sndbuffer[i * 2];
205 sndbuffer[i * 2] = sndbuffer[i * 2 + 1];
206 sndbuffer[i * 2 + 1] = tmp;
211 ret = SPU_playCDDAchannel((short *)sndbuffer, s);
214 } while (ret == 0x7761 && playing); // rearmed_wait
217 if (ret != 0x676f) { // !rearmed_go
221 // HACK: stop feeding data while emu is paused
223 while (stop && playing)
226 now = GetTickCount();
232 else if (osleep > CDDA_FRAMETIME) {
233 osleep = CDDA_FRAMETIME;
237 usleep(osleep * 1000);
251 // stop the CDDA playback
252 static void stopCDDA() {
259 WaitForSingleObject(threadid, INFINITE);
261 pthread_join(threadid, NULL);
265 // start the CDDA playback
266 static void startCDDA(unsigned int sector) {
271 cdda_cur_sector = sector;
275 threadid = (HANDLE)_beginthread(playthread, 0, NULL);
277 pthread_create(&threadid, NULL, playthread, NULL);
281 // this function tries to get the .toc file of the given .bin
282 // the necessary data is put into the ti (trackinformation)-array
283 static int parsetoc(const char *isofile) {
284 char tocname[MAXPATHLEN];
286 char linebuf[256], dummy[256], name[256];
288 char time[20], time2[20];
289 unsigned int t, sector_offs;
293 // copy name of the iso and change extension from .bin to .toc
294 strncpy(tocname, isofile, sizeof(tocname));
295 tocname[MAXPATHLEN - 1] = '\0';
296 if (strlen(tocname) >= 4) {
297 strcpy(tocname + strlen(tocname) - 4, ".toc");
303 if ((fi = fopen(tocname, "r")) == NULL) {
304 // try changing extension to .cue (to satisfy some stupid tutorials)
305 strcpy(tocname + strlen(tocname) - 4, ".cue");
306 if ((fi = fopen(tocname, "r")) == NULL) {
307 // if filename is image.toc.bin, try removing .bin (for Brasero)
308 strcpy(tocname, isofile);
310 if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) {
311 tocname[t - 4] = '\0';
312 if ((fi = fopen(tocname, "r")) == NULL) {
322 memset(&ti, 0, sizeof(ti));
323 cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio
325 sector_offs = 2 * 75;
327 // parse the .toc file
328 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
330 strncpy(dummy, linebuf, sizeof(linebuf));
331 token = strtok(dummy, " ");
333 if (token == NULL) continue;
335 if (!strcmp(token, "TRACK")) {
337 token = strtok(NULL, " ");
340 if (!strncmp(token, "MODE2_RAW", 9)) {
341 ti[numtracks].type = DATA;
342 sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0
344 // check if this image contains mixed subchannel data
345 token = strtok(NULL, " ");
346 if (token != NULL && !strncmp(token, "RW_RAW", 6)) {
351 else if (!strncmp(token, "AUDIO", 5)) {
352 ti[numtracks].type = CDDA;
355 else if (!strcmp(token, "DATAFILE")) {
356 if (ti[numtracks].type == CDDA) {
357 sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2);
358 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
359 ti[numtracks].start_offset = t;
361 sec2msf(t, (char *)&ti[numtracks].start);
362 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
365 sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time);
366 tok2msf((char *)&time, (char *)&ti[numtracks].length);
369 else if (!strcmp(token, "FILE")) {
370 sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2);
371 tok2msf((char *)&time, (char *)&ti[numtracks].start);
372 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
373 ti[numtracks].start_offset = t;
374 t += msf2sec(ti[numtracks].start) + sector_offs;
375 sec2msf(t, (char *)&ti[numtracks].start);
376 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
378 else if (!strcmp(token, "ZERO")) {
379 sscanf(linebuf, "ZERO AUDIO RW_RAW %8s", time);
380 tok2msf((char *)&time, dummy);
381 sector_offs += msf2sec(dummy);
383 t = ti[numtracks - 1].start_offset;
384 pregapOffset = t + msf2sec(ti[numtracks - 1].length);
394 // this function tries to get the .cue file of the given .bin
395 // the necessary data is put into the ti (trackinformation)-array
396 static int parsecue(const char *isofile) {
397 char cuename[MAXPATHLEN];
398 char filepath[MAXPATHLEN];
404 char linebuf[256], tmpb[256], dummy[256];
405 unsigned int incue_max_len;
406 unsigned int t, file_len, sector_offs;
410 // copy name of the iso and change extension from .bin to .cue
411 strncpy(cuename, isofile, sizeof(cuename));
412 cuename[MAXPATHLEN - 1] = '\0';
413 if (strlen(cuename) >= 4) {
414 strcpy(cuename + strlen(cuename) - 4, ".cue");
420 if ((fi = fopen(cuename, "r")) == NULL) {
424 // Some stupid tutorials wrongly tell users to use cdrdao to rip a
425 // "bin/cue" image, which is in fact a "bin/toc" image. So let's check
427 if (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
428 if (!strncmp(linebuf, "CD_ROM_XA", 9)) {
429 // Don't proceed further, as this is actually a .toc file rather
432 return parsetoc(isofile);
434 fseek(fi, 0, SEEK_SET);
437 // build a path for files referenced in .cue
438 strncpy(filepath, cuename, sizeof(filepath));
439 tmp = strrchr(filepath, '/') + 1;
441 tmp = strrchr(filepath, '\\') + 1;
445 filepath[sizeof(filepath) - 1] = 0;
447 incue_max_len = sizeof(filepath) - (tmp - filepath) - 1;
449 memset(&ti, 0, sizeof(ti));
452 sector_offs = 2 * 75;
454 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
455 strncpy(dummy, linebuf, sizeof(linebuf));
456 token = strtok(dummy, " ");
462 if (!strcmp(token, "TRACK")) {
465 if (strstr(linebuf, "AUDIO") != NULL) {
466 ti[numtracks].type = CDDA;
468 else if (strstr(linebuf, "MODE1/2352") != NULL || strstr(linebuf, "MODE2/2352") != NULL) {
469 ti[numtracks].type = DATA;
472 else if (!strcmp(token, "INDEX")) {
473 sscanf(linebuf, " INDEX %02d %8s", &t, time);
474 tok2msf(time, (char *)&ti[numtracks].start);
476 t = msf2sec(ti[numtracks].start);
477 ti[numtracks].start_offset = t;
479 sec2msf(t, ti[numtracks].start);
481 // default track length to file length
482 t = file_len - ti[numtracks].start_offset;
483 sec2msf(t, ti[numtracks].length);
485 if (numtracks > 1 && ti[numtracks].handle == NULL) {
486 // this track uses the same file as the last,
487 // start of this track is last track's end
488 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
489 sec2msf(t, ti[numtracks - 1].length);
491 if (numtracks > 1 && pregapOffset == -1)
492 pregapOffset = ti[numtracks].start_offset;
494 else if (!strcmp(token, "PREGAP")) {
495 if (sscanf(linebuf, " PREGAP %8s", time) == 1) {
496 tok2msf(time, dummy);
497 sector_offs += msf2sec(dummy);
499 pregapOffset = -1; // mark to fill track start_offset
501 else if (!strcmp(token, "FILE")) {
502 t = sscanf(linebuf, " FILE \"%256[^\"]\"", tmpb);
504 sscanf(linebuf, " FILE %256s", tmpb);
507 ti[numtracks + 1].handle = fopen(tmpb, "rb");
508 if (ti[numtracks + 1].handle == NULL) {
510 tmp = strrchr(tmpb, '\\');
512 tmp = strrchr(tmpb, '/');
517 strncpy(incue_fname, tmp, incue_max_len);
518 ti[numtracks + 1].handle = fopen(filepath, "rb");
521 // update global offset if this is not first file in this .cue
522 if (numtracks + 1 > 1)
523 sector_offs += file_len;
526 if (ti[numtracks + 1].handle == NULL) {
527 SysPrintf(_("\ncould not open: %s\n"), filepath);
530 fseek(ti[numtracks + 1].handle, 0, SEEK_END);
531 file_len = ftell(ti[numtracks + 1].handle) / 2352;
533 if (numtracks == 0 && strlen(isofile) >= 4 &&
534 strcmp(isofile + strlen(isofile) - 4, ".cue") == 0)
536 // user selected .cue as image file, use it's data track instead
538 cdHandle = fopen(filepath, "rb");
548 // this function tries to get the .ccd file of the given .img
549 // the necessary data is put into the ti (trackinformation)-array
550 static int parseccd(const char *isofile) {
551 char ccdname[MAXPATHLEN];
558 // copy name of the iso and change extension from .img to .ccd
559 strncpy(ccdname, isofile, sizeof(ccdname));
560 ccdname[MAXPATHLEN - 1] = '\0';
561 if (strlen(ccdname) >= 4) {
562 strcpy(ccdname + strlen(ccdname) - 4, ".ccd");
568 if ((fi = fopen(ccdname, "r")) == NULL) {
572 memset(&ti, 0, sizeof(ti));
574 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
575 if (!strncmp(linebuf, "[TRACK", 6)){
578 else if (!strncmp(linebuf, "MODE=", 5)) {
579 sscanf(linebuf, "MODE=%d", &t);
580 ti[numtracks].type = ((t == 0) ? CDDA : DATA);
582 else if (!strncmp(linebuf, "INDEX 1=", 8)) {
583 sscanf(linebuf, "INDEX 1=%d", &t);
584 sec2msf(t + 2 * 75, ti[numtracks].start);
585 ti[numtracks].start_offset = t;
587 // If we've already seen another track, this is its end
589 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
590 sec2msf(t, ti[numtracks - 1].length);
597 // Fill out the last track's end based on size
598 if (numtracks >= 1) {
599 fseek(cdHandle, 0, SEEK_END);
600 t = ftell(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75;
601 sec2msf(t, ti[numtracks].length);
607 // this function tries to get the .mds file of the given .mdf
608 // the necessary data is put into the ti (trackinformation)-array
609 static int parsemds(const char *isofile) {
610 char mdsname[MAXPATHLEN];
612 unsigned int offset, extra_offset, l, i;
617 // copy name of the iso and change extension from .mdf to .mds
618 strncpy(mdsname, isofile, sizeof(mdsname));
619 mdsname[MAXPATHLEN - 1] = '\0';
620 if (strlen(mdsname) >= 4) {
621 strcpy(mdsname + strlen(mdsname) - 4, ".mds");
627 if ((fi = fopen(mdsname, "rb")) == NULL) {
631 memset(&ti, 0, sizeof(ti));
633 // check if it's a valid mds file
634 fread(&i, 1, sizeof(unsigned int), fi);
636 if (i != 0x4944454D) {
637 // not an valid mds file
642 // get offset to session block
643 fseek(fi, 0x50, SEEK_SET);
644 fread(&offset, 1, sizeof(unsigned int), fi);
645 offset = SWAP32(offset);
647 // get total number of tracks
649 fseek(fi, offset, SEEK_SET);
650 fread(&s, 1, sizeof(unsigned short), fi);
654 // get offset to track blocks
655 fseek(fi, 4, SEEK_CUR);
656 fread(&offset, 1, sizeof(unsigned int), fi);
657 offset = SWAP32(offset);
661 fseek(fi, offset + 4, SEEK_SET);
662 if (fgetc(fi) < 0xA0) {
668 // check if the image contains mixed subchannel data
669 fseek(fi, offset + 1, SEEK_SET);
670 subChanMixed = (fgetc(fi) ? TRUE : FALSE);
673 for (i = 1; i <= numtracks; i++) {
674 fseek(fi, offset, SEEK_SET);
676 // get the track type
677 ti[i].type = ((fgetc(fi) == 0xA9) ? CDDA : DATA);
678 fseek(fi, 8, SEEK_CUR);
680 // get the track starting point
681 ti[i].start[0] = fgetc(fi);
682 ti[i].start[1] = fgetc(fi);
683 ti[i].start[2] = fgetc(fi);
685 fread(&extra_offset, 1, sizeof(unsigned int), fi);
686 extra_offset = SWAP32(extra_offset);
688 // get track start offset (in .mdf)
689 fseek(fi, offset + 0x28, SEEK_SET);
690 fread(&l, 1, sizeof(unsigned int), fi);
692 ti[i].start_offset = l / CD_FRAMESIZE_RAW;
695 fseek(fi, extra_offset, SEEK_SET);
696 fread(&l, 1, sizeof(unsigned int), fi);
699 pregapOffset = ti[i].start_offset;
701 // get the track length
702 fread(&l, 1, sizeof(unsigned int), fi);
704 sec2msf(l, ti[i].length);
713 static int handlepbp(const char *isofile) {
716 unsigned int dontcare[8];
717 unsigned int psar_offs;
730 unsigned int dontcare[6];
733 unsigned int t, cd_length, cdimg_base;
734 unsigned int offsettab[8], psisoimg_offs;
735 const char *ext = NULL;
738 if (strlen(isofile) >= 4)
739 ext = isofile + strlen(isofile) - 4;
740 if (ext == NULL || (strcmp(ext, ".pbp") != 0 && strcmp(ext, ".PBP") != 0))
745 ret = fread(&pbp_hdr, 1, sizeof(pbp_hdr), cdHandle);
746 if (ret != sizeof(pbp_hdr)) {
747 SysPrintf("failed to read pbp\n");
751 ret = fseek(cdHandle, pbp_hdr.psar_offs, SEEK_SET);
753 SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs);
757 psisoimg_offs = pbp_hdr.psar_offs;
758 fread(psar_sig, 1, sizeof(psar_sig), cdHandle);
760 if (strcmp(psar_sig, "PSTITLEIMG") == 0) {
762 ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x200, SEEK_SET);
764 SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs + 0x200);
768 if (fread(&offsettab, 1, sizeof(offsettab), cdHandle) != sizeof(offsettab)) {
769 SysPrintf("failed to read offsettab\n");
773 for (i = 0; i < sizeof(offsettab) / sizeof(offsettab[0]); i++) {
774 if (offsettab[i] == 0)
777 cdrIsoMultidiskCount = i;
778 if (cdrIsoMultidiskCount == 0) {
779 SysPrintf("multidisk eboot has 0 images?\n");
783 if (cdrIsoMultidiskSelect >= cdrIsoMultidiskCount)
784 cdrIsoMultidiskSelect = 0;
786 psisoimg_offs += offsettab[cdrIsoMultidiskSelect];
788 ret = fseek(cdHandle, psisoimg_offs, SEEK_SET);
790 SysPrintf("failed to seek to %x\n", psisoimg_offs);
794 fread(psar_sig, 1, sizeof(psar_sig), cdHandle);
798 if (strcmp(psar_sig, "PSISOIMG00") != 0) {
799 SysPrintf("bad psar_sig: %s\n", psar_sig);
804 ret = fseek(cdHandle, psisoimg_offs + 0x800, SEEK_SET);
806 SysPrintf("failed to seek to %x\n", psisoimg_offs + 0x800);
810 // first 3 entries are special
811 fseek(cdHandle, sizeof(toc_entry), SEEK_CUR);
812 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
813 numtracks = btoi(toc_entry.index1[0]);
815 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
816 cd_length = btoi(toc_entry.index1[0]) * 60 * 75 +
817 btoi(toc_entry.index1[1]) * 75 + btoi(toc_entry.index1[2]);
819 for (i = 1; i <= numtracks; i++) {
820 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
822 ti[i].type = (toc_entry.type == 1) ? CDDA : DATA;
824 ti[i].start_offset = btoi(toc_entry.index0[0]) * 60 * 75 +
825 btoi(toc_entry.index0[1]) * 75 + btoi(toc_entry.index0[2]);
826 ti[i].start[0] = btoi(toc_entry.index1[0]);
827 ti[i].start[1] = btoi(toc_entry.index1[1]);
828 ti[i].start[2] = btoi(toc_entry.index1[2]);
831 t = msf2sec(ti[i].start) - msf2sec(ti[i - 1].start);
832 sec2msf(t, ti[i - 1].length);
835 t = cd_length - ti[numtracks].start_offset;
836 sec2msf(t, ti[numtracks].length);
839 ret = fseek(cdHandle, psisoimg_offs + 0x4000, SEEK_SET);
841 SysPrintf("failed to seek to ISO index\n");
845 compr_img = calloc(1, sizeof(*compr_img));
846 if (compr_img == NULL)
849 compr_img->block_shift = 4;
850 compr_img->current_block = (unsigned int)-1;
852 compr_img->index_len = (0x100000 - 0x4000) / sizeof(index_entry);
853 compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
854 if (compr_img->index_table == NULL)
857 cdimg_base = psisoimg_offs + 0x100000;
858 for (i = 0; i < compr_img->index_len; i++) {
859 ret = fread(&index_entry, 1, sizeof(index_entry), cdHandle);
860 if (ret != sizeof(index_entry)) {
861 SysPrintf("failed to read index_entry #%d\n", i);
865 if (index_entry.size == 0)
868 compr_img->index_table[i] = cdimg_base + index_entry.offset;
870 compr_img->index_table[i] = cdimg_base + index_entry.offset + index_entry.size;
875 free(compr_img->index_table);
876 compr_img->index_table = NULL;
878 if (compr_img != NULL) {
885 static int handlecbin(const char *isofile) {
889 unsigned int header_size;
890 unsigned long long total_bytes;
891 unsigned int block_size;
892 unsigned char ver; // 1
894 unsigned char rsv_06[2];
896 const char *ext = NULL;
897 unsigned int index = 0, plain;
900 if (strlen(isofile) >= 5)
901 ext = isofile + strlen(isofile) - 5;
902 if (ext == NULL || (strcasecmp(ext + 1, ".cbn") != 0 && strcasecmp(ext, ".cbin") != 0))
905 ret = fread(&ciso_hdr, 1, sizeof(ciso_hdr), cdHandle);
906 if (ret != sizeof(ciso_hdr)) {
907 SysPrintf("failed to read ciso header\n");
911 if (strncmp(ciso_hdr.magic, "CISO", 4) != 0 || ciso_hdr.total_bytes <= 0 || ciso_hdr.block_size <= 0) {
912 SysPrintf("bad ciso header\n");
915 if (ciso_hdr.header_size != 0 && ciso_hdr.header_size != sizeof(ciso_hdr)) {
916 ret = fseek(cdHandle, ciso_hdr.header_size, SEEK_SET);
918 SysPrintf("failed to seek to %x\n", ciso_hdr.header_size);
923 compr_img = calloc(1, sizeof(*compr_img));
924 if (compr_img == NULL)
927 compr_img->block_shift = 0;
928 compr_img->current_block = (unsigned int)-1;
930 compr_img->index_len = ciso_hdr.total_bytes / ciso_hdr.block_size;
931 compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
932 if (compr_img->index_table == NULL)
935 ret = fread(compr_img->index_table, sizeof(compr_img->index_table[0]), compr_img->index_len, cdHandle);
936 if (ret != compr_img->index_len) {
937 SysPrintf("failed to read index table\n");
941 for (i = 0; i < compr_img->index_len + 1; i++) {
942 index = compr_img->index_table[i];
943 plain = index & 0x80000000;
945 compr_img->index_table[i] = (index << ciso_hdr.align) | plain;
947 if ((long long)index << ciso_hdr.align >= 0x80000000ll)
948 SysPrintf("warning: ciso img too large, expect problems\n");
953 free(compr_img->index_table);
954 compr_img->index_table = NULL;
956 if (compr_img != NULL) {
963 // this function tries to get the .sub file of the given .img
964 static int opensubfile(const char *isoname) {
965 char subname[MAXPATHLEN];
967 // copy name of the iso and change extension from .img to .sub
968 strncpy(subname, isoname, sizeof(subname));
969 subname[MAXPATHLEN - 1] = '\0';
970 if (strlen(subname) >= 4) {
971 strcpy(subname + strlen(subname) - 4, ".sub");
977 subHandle = fopen(subname, "rb");
978 if (subHandle == NULL) {
985 static int opensbifile(const char *isoname) {
986 char sbiname[MAXPATHLEN];
989 strncpy(sbiname, isoname, sizeof(sbiname));
990 sbiname[MAXPATHLEN - 1] = '\0';
991 if (strlen(sbiname) >= 4) {
992 strcpy(sbiname + strlen(sbiname) - 4, ".sbi");
998 fseek(cdHandle, 0, SEEK_END);
999 s = ftell(cdHandle) / 2352;
1001 return LoadSBI(sbiname, s);
1004 static int cdread_normal(FILE *f, void *dest, int sector)
1006 fseek(f, sector * CD_FRAMESIZE_RAW, SEEK_SET);
1007 return fread(dest, 1, CD_FRAMESIZE_RAW, f);
1010 static int cdread_sub_mixed(FILE *f, void *dest, int sector)
1014 fseek(f, sector * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE), SEEK_SET);
1015 ret = fread(dest, 1, CD_FRAMESIZE_RAW, f);
1016 fread(subbuffer, 1, SUB_FRAMESIZE, f);
1018 if (subChanRaw) DecodeRawSubData();
1023 static int uncompress2(void *out, unsigned long *out_size, void *in, unsigned long in_size)
1028 if (z.zalloc == NULL) {
1029 // XXX: one-time leak here..
1035 ret = inflateInit2(&z, -15);
1038 ret = inflateReset(&z);
1043 z.avail_in = in_size;
1045 z.avail_out = *out_size;
1047 ret = inflate(&z, Z_NO_FLUSH);
1050 *out_size -= z.avail_out;
1051 return ret == 1 ? 0 : ret;
1054 static int cdread_compressed(FILE *f, void *dest, int sector)
1056 unsigned long cdbuffer_size, cdbuffer_size_expect;
1057 unsigned int start_byte, size;
1061 block = sector >> compr_img->block_shift;
1062 compr_img->sector_in_blk = sector & ((1 << compr_img->block_shift) - 1);
1064 if (block == compr_img->current_block) {
1065 //printf("hit sect %d\n", sector);
1069 if (sector >= compr_img->index_len * 16) {
1070 SysPrintf("sector %d is past img end\n", sector);
1074 start_byte = compr_img->index_table[block] & 0x7fffffff;
1075 if (fseek(cdHandle, start_byte, SEEK_SET) != 0) {
1076 SysPrintf("seek error for block %d at %x: ",
1082 is_compressed = !(compr_img->index_table[block] & 0x80000000);
1083 size = (compr_img->index_table[block + 1] & 0x7fffffff) - start_byte;
1084 if (size > sizeof(compr_img->buff_compressed)) {
1085 SysPrintf("block %d is too large: %u\n", block, size);
1089 if (fread(is_compressed ? compr_img->buff_compressed : compr_img->buff_raw[0],
1090 1, size, cdHandle) != size) {
1091 SysPrintf("read error for block %d at %x: ", block, start_byte);
1096 if (is_compressed) {
1097 cdbuffer_size_expect = sizeof(compr_img->buff_raw[0]) << compr_img->block_shift;
1098 cdbuffer_size = cdbuffer_size_expect;
1099 ret = uncompress2(compr_img->buff_raw[0], &cdbuffer_size, compr_img->buff_compressed, size);
1101 SysPrintf("uncompress failed with %d for block %d, sector %d\n",
1102 ret, block, sector);
1105 if (cdbuffer_size != cdbuffer_size_expect)
1106 SysPrintf("cdbuffer_size: %lu != %lu, sector %d\n", cdbuffer_size,
1107 cdbuffer_size_expect, sector);
1111 compr_img->current_block = block;
1114 if (dest != cdbuffer) // copy avoid HACK
1115 memcpy(dest, compr_img->buff_raw[compr_img->sector_in_blk],
1117 return CD_FRAMESIZE_RAW;
1120 static int cdread_2048(FILE *f, void *dest, int sector)
1124 fseek(f, sector * 2048, SEEK_SET);
1125 ret = fread((char *)dest + 12 * 2, 1, 2048, f);
1127 // not really necessary, fake mode 2 header
1128 memset(cdbuffer, 0, 12 * 2);
1129 sec2msf(sector + 2 * 75, (char *)&cdbuffer[12]);
1130 cdbuffer[12 + 3] = 1;
1135 static unsigned char * CALLBACK ISOgetBuffer_compr(void) {
1136 return compr_img->buff_raw[compr_img->sector_in_blk] + 12;
1139 static unsigned char * CALLBACK ISOgetBuffer(void) {
1140 return cdbuffer + 12;
1143 static void PrintTracks(void) {
1146 for (i = 1; i <= numtracks; i++) {
1147 SysPrintf(_("Track %.2d (%s) - Start %.2d:%.2d:%.2d, Length %.2d:%.2d:%.2d\n"),
1148 i, (ti[i].type == DATA ? "DATA" : "AUDIO"),
1149 ti[i].start[0], ti[i].start[1], ti[i].start[2],
1150 ti[i].length[0], ti[i].length[1], ti[i].length[2]);
1154 // This function is invoked by the front-end when opening an ISO
1155 // file for playback
1156 static long CALLBACK ISOopen(void) {
1157 boolean isMode1ISO = FALSE;
1159 if (cdHandle != NULL) {
1160 return 0; // it's already open
1163 cdHandle = fopen(GetIsoFile(), "rb");
1164 if (cdHandle == NULL) {
1168 SysPrintf(_("Loaded CD Image: %s"), GetIsoFile());
1170 cddaBigEndian = FALSE;
1171 subChanMixed = FALSE;
1174 cdrIsoMultidiskCount = 1;
1176 CDR_getBuffer = ISOgetBuffer;
1177 cdimg_read_func = cdread_normal;
1179 if (parsecue(GetIsoFile()) == 0) {
1180 SysPrintf("[+cue]");
1182 else if (parsetoc(GetIsoFile()) == 0) {
1183 SysPrintf("[+toc]");
1185 else if (parseccd(GetIsoFile()) == 0) {
1186 SysPrintf("[+ccd]");
1188 else if (parsemds(GetIsoFile()) == 0) {
1189 SysPrintf("[+mds]");
1191 if (handlepbp(GetIsoFile()) == 0) {
1193 CDR_getBuffer = ISOgetBuffer_compr;
1194 cdimg_read_func = cdread_compressed;
1196 else if (handlecbin(GetIsoFile()) == 0) {
1197 SysPrintf("[cbin]");
1198 CDR_getBuffer = ISOgetBuffer_compr;
1199 cdimg_read_func = cdread_compressed;
1202 if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
1203 SysPrintf("[+sub]");
1205 if (opensbifile(GetIsoFile()) == 0) {
1206 SysPrintf("[+sbi]");
1209 // guess whether it is mode1/2048
1210 fseek(cdHandle, 0, SEEK_END);
1211 if (ftell(cdHandle) % 2048 == 0) {
1212 unsigned int modeTest = 0;
1213 fseek(cdHandle, 0, SEEK_SET);
1214 fread(&modeTest, 4, 1, cdHandle);
1215 if (SWAP32(modeTest) != 0xffffff00) {
1216 SysPrintf("[2048]");
1220 fseek(cdHandle, 0, SEEK_SET);
1227 cdimg_read_func = cdread_sub_mixed;
1228 else if (isMode1ISO)
1229 cdimg_read_func = cdread_2048;
1231 // make sure we have another handle open for cdda
1232 if (numtracks > 1 && ti[1].handle == NULL) {
1233 ti[1].handle = fopen(GetIsoFile(), "rb");
1235 cdda_cur_sector = cdda_toc_delta = 0;
1240 static long CALLBACK ISOclose(void) {
1243 if (cdHandle != NULL) {
1247 if (subHandle != NULL) {
1254 if (compr_img != NULL) {
1255 free(compr_img->index_table);
1260 for (i = 1; i <= numtracks; i++) {
1261 if (ti[i].handle != NULL) {
1262 fclose(ti[i].handle);
1263 ti[i].handle = NULL;
1272 static long CALLBACK ISOinit(void) {
1273 assert(cdHandle == NULL);
1274 assert(subHandle == NULL);
1276 return 0; // do nothing
1279 static long CALLBACK ISOshutdown(void) {
1284 // return Starting and Ending Track
1286 // byte 0 - start track
1287 // byte 1 - end track
1288 static long CALLBACK ISOgetTN(unsigned char *buffer) {
1291 if (numtracks > 0) {
1292 buffer[1] = numtracks;
1301 // return Track Time
1306 static long CALLBACK ISOgetTD(unsigned char track, unsigned char *buffer) {
1308 // CD length according pcsxr-svn (done a bit different here)
1310 unsigned char time[3];
1311 sect = msf2sec(ti[numtracks].start) + msf2sec(ti[numtracks].length);
1312 sec2msf(sect, (char *)time);
1313 buffer[2] = time[0];
1314 buffer[1] = time[1];
1315 buffer[0] = time[2];
1317 else if (numtracks > 0 && track <= numtracks) {
1318 buffer[2] = ti[track].start[0];
1319 buffer[1] = ti[track].start[1];
1320 buffer[0] = ti[track].start[2];
1331 // decode 'raw' subchannel data ripped by cdrdao
1332 static void DecodeRawSubData(void) {
1333 unsigned char subQData[12];
1336 memset(subQData, 0, sizeof(subQData));
1338 for (i = 0; i < 8 * 12; i++) {
1339 if (subbuffer[i] & (1 << 6)) { // only subchannel Q is needed
1340 subQData[i >> 3] |= (1 << (7 - (i & 7)));
1344 memcpy(&subbuffer[12], subQData, 12);
1348 // time: byte 0 - minute; byte 1 - second; byte 2 - frame
1350 static long CALLBACK ISOreadTrack(unsigned char *time) {
1351 int sector = MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2]));
1353 if (cdHandle == NULL) {
1358 subChanMissing = FALSE;
1359 if (sector >= pregapOffset) {
1361 if (sector < pregapOffset)
1362 subChanMissing = TRUE;
1366 cdimg_read_func(cdHandle, cdbuffer, sector);
1368 if (subHandle != NULL) {
1369 fseek(subHandle, sector * SUB_FRAMESIZE, SEEK_SET);
1370 fread(subbuffer, 1, SUB_FRAMESIZE, subHandle);
1372 if (subChanRaw) DecodeRawSubData();
1379 // sector: byte 0 - minute; byte 1 - second; byte 2 - frame
1380 // does NOT uses bcd format
1381 static long CALLBACK ISOplay(unsigned char *time) {
1382 unsigned int i, abs_sect, start_sect = 0;
1383 int track_offset, file_sect;
1389 abs_sect = msf2sec((char *)time);
1390 for (i = numtracks; i > 1; i--) {
1391 start_sect = msf2sec(ti[i].start);
1392 if (start_sect <= abs_sect + 2 * 75)
1396 track_offset = abs_sect - start_sect;
1397 file_sect = ti[i].start_offset + track_offset;
1401 // find the file that contains this track
1403 if (ti[i].handle != NULL)
1406 cdda_first_sector = 0;
1408 cdda_first_sector = file_sect - track_offset;
1410 cdda_toc_delta = abs_sect - file_sect;
1411 cddaHandle = ti[i].handle;
1413 if (SPU_playCDDAchannel != NULL) {
1414 startCDDA(file_sect);
1420 static long CALLBACK ISOstop(void) {
1425 // gets subchannel data
1426 static unsigned char* CALLBACK ISOgetBufferSub(void) {
1427 if ((subHandle != NULL || subChanMixed) && !subChanMissing) {
1434 static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
1437 CDR__getStatus(stat);
1441 stat->Status |= 0x80;
1447 sec = cdda_cur_sector + cdda_toc_delta;
1448 sec2msf(sec, (char *)stat->Time);
1453 void cdrIsoInit(void) {
1455 CDR_shutdown = ISOshutdown;
1457 CDR_close = ISOclose;
1458 CDR_getTN = ISOgetTN;
1459 CDR_getTD = ISOgetTD;
1460 CDR_readTrack = ISOreadTrack;
1461 CDR_getBuffer = ISOgetBuffer;
1464 CDR_getBufferSub = ISOgetBufferSub;
1465 CDR_getStatus = ISOgetStatus;
1467 CDR_getDriveLetter = CDR__getDriveLetter;
1468 CDR_configure = CDR__configure;
1469 CDR_test = CDR__test;
1470 CDR_about = CDR__about;
1471 CDR_setfilename = CDR__setfilename;
1476 int cdrIsoActive(void) {
1477 return (cdHandle != NULL);