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 current_block;
74 unsigned int sector_in_blk;
77 int (*cdimg_read_func)(FILE *f, void *dest, int sector, int offset);
79 char* CALLBACK CDR__getDriveLetter(void);
80 long CALLBACK CDR__configure(void);
81 long CALLBACK CDR__test(void);
82 void CALLBACK CDR__about(void);
83 long CALLBACK CDR__setfilename(char *filename);
84 long CALLBACK CDR__getStatus(struct CdrStat *stat);
86 static void DecodeRawSubData(void);
88 extern void *hCDRDriver;
91 enum {DATA, CDDA} type;
92 char start[3]; // MSF-format
93 char length[3]; // MSF-format
94 FILE *handle; // for multi-track images CDDA
95 int start_offset; // sector offset from start of above file
98 #define MAXTRACKS 100 /* How many tracks can a CD hold? */
100 static int numtracks = 0;
101 static struct trackinfo ti[MAXTRACKS];
103 // get a sector from a msf-array
104 static unsigned int msf2sec(char *msf) {
105 return ((msf[0] * 60 + msf[1]) * 75) + msf[2];
108 static void sec2msf(unsigned int s, char *msf) {
109 msf[0] = s / 75 / 60;
110 s = s - msf[0] * 75 * 60;
116 // divide a string of xx:yy:zz into m, s, f
117 static void tok2msf(char *time, char *msf) {
120 token = strtok(time, ":");
122 msf[0] = atoi(token);
128 token = strtok(NULL, ":");
130 msf[1] = atoi(token);
136 token = strtok(NULL, ":");
138 msf[2] = atoi(token);
146 static long GetTickCount(void) {
147 static time_t initial_time = 0;
150 gettimeofday(&now, NULL);
152 if (initial_time == 0) {
153 initial_time = now.tv_sec;
156 return (now.tv_sec - initial_time) * 1000L + now.tv_usec / 1000L;
160 // this thread plays audio data
162 static void playthread(void *param)
164 static void *playthread(void *param)
173 d = t - (long)GetTickCount();
177 else if (d > CDDA_FRAMETIME) {
185 // HACK: stop feeding data while emu is paused
192 t = GetTickCount() + CDDA_FRAMETIME;
195 for (i = 0; i < sizeof(sndbuffer) / CD_FRAMESIZE_RAW; i++) {
196 d = cdimg_read_func(cddaHandle, sndbuffer + s, cdda_cur_sector, 0);
197 if (d < CD_FRAMESIZE_RAW)
204 if (subHandle != NULL) {
205 fseek(subHandle, cdda_cur_sector * SUB_FRAMESIZE, SEEK_SET);
206 fread(subbuffer, 1, SUB_FRAMESIZE, subHandle);
208 if (subChanRaw) DecodeRawSubData();
217 if (!cdr.Muted && playing) {
219 for (i = 0; i < s / 2; i++) {
220 tmp = sndbuffer[i * 2];
221 sndbuffer[i * 2] = sndbuffer[i * 2 + 1];
222 sndbuffer[i * 2 + 1] = tmp;
226 SPU_playCDDAchannel((short *)sndbuffer, s);
238 // stop the CDDA playback
239 static void stopCDDA() {
246 WaitForSingleObject(threadid, INFINITE);
248 pthread_join(threadid, NULL);
252 // start the CDDA playback
253 static void startCDDA(unsigned int sector) {
258 cdda_cur_sector = sector;
262 threadid = (HANDLE)_beginthread(playthread, 0, NULL);
264 pthread_create(&threadid, NULL, playthread, NULL);
268 // this function tries to get the .toc file of the given .bin
269 // the necessary data is put into the ti (trackinformation)-array
270 static int parsetoc(const char *isofile) {
271 char tocname[MAXPATHLEN];
273 char linebuf[256], dummy[256], name[256];
275 char time[20], time2[20];
276 unsigned int t, sector_offs;
280 // copy name of the iso and change extension from .bin to .toc
281 strncpy(tocname, isofile, sizeof(tocname));
282 tocname[MAXPATHLEN - 1] = '\0';
283 if (strlen(tocname) >= 4) {
284 strcpy(tocname + strlen(tocname) - 4, ".toc");
290 if ((fi = fopen(tocname, "r")) == NULL) {
291 // try changing extension to .cue (to satisfy some stupid tutorials)
292 strcpy(tocname + strlen(tocname) - 4, ".cue");
293 if ((fi = fopen(tocname, "r")) == NULL) {
294 // if filename is image.toc.bin, try removing .bin (for Brasero)
295 strcpy(tocname, isofile);
297 if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) {
298 tocname[t - 4] = '\0';
299 if ((fi = fopen(tocname, "r")) == NULL) {
309 memset(&ti, 0, sizeof(ti));
310 cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio
312 sector_offs = 2 * 75;
314 // parse the .toc file
315 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
317 strncpy(dummy, linebuf, sizeof(linebuf));
318 token = strtok(dummy, " ");
320 if (token == NULL) continue;
322 if (!strcmp(token, "TRACK")) {
324 token = strtok(NULL, " ");
327 if (!strncmp(token, "MODE2_RAW", 9)) {
328 ti[numtracks].type = DATA;
329 sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0
331 // check if this image contains mixed subchannel data
332 token = strtok(NULL, " ");
333 if (token != NULL && !strncmp(token, "RW_RAW", 6)) {
338 else if (!strncmp(token, "AUDIO", 5)) {
339 ti[numtracks].type = CDDA;
342 else if (!strcmp(token, "DATAFILE")) {
343 if (ti[numtracks].type == CDDA) {
344 sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2);
345 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
346 ti[numtracks].start_offset = t;
348 sec2msf(t, (char *)&ti[numtracks].start);
349 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
352 sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time);
353 tok2msf((char *)&time, (char *)&ti[numtracks].length);
356 else if (!strcmp(token, "FILE")) {
357 sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2);
358 tok2msf((char *)&time, (char *)&ti[numtracks].start);
359 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
360 ti[numtracks].start_offset = t;
361 t += msf2sec(ti[numtracks].start) + sector_offs;
362 sec2msf(t, (char *)&ti[numtracks].start);
363 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
365 else if (!strcmp(token, "ZERO")) {
366 sscanf(linebuf, "ZERO AUDIO RW_RAW %8s", time);
367 tok2msf((char *)&time, dummy);
368 sector_offs += msf2sec(dummy);
370 t = ti[numtracks - 1].start_offset;
371 pregapOffset = t + msf2sec(ti[numtracks - 1].length);
381 // this function tries to get the .cue file of the given .bin
382 // the necessary data is put into the ti (trackinformation)-array
383 static int parsecue(const char *isofile) {
384 char cuename[MAXPATHLEN];
385 char filepath[MAXPATHLEN];
391 char linebuf[256], tmpb[256], dummy[256];
392 unsigned int incue_max_len;
393 unsigned int t, file_len, sector_offs;
397 // copy name of the iso and change extension from .bin to .cue
398 strncpy(cuename, isofile, sizeof(cuename));
399 cuename[MAXPATHLEN - 1] = '\0';
400 if (strlen(cuename) >= 4) {
401 strcpy(cuename + strlen(cuename) - 4, ".cue");
407 if ((fi = fopen(cuename, "r")) == NULL) {
411 // Some stupid tutorials wrongly tell users to use cdrdao to rip a
412 // "bin/cue" image, which is in fact a "bin/toc" image. So let's check
414 if (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
415 if (!strncmp(linebuf, "CD_ROM_XA", 9)) {
416 // Don't proceed further, as this is actually a .toc file rather
419 return parsetoc(isofile);
421 fseek(fi, 0, SEEK_SET);
424 // build a path for files referenced in .cue
425 strncpy(filepath, cuename, sizeof(filepath));
426 tmp = strrchr(filepath, '/') + 1;
428 tmp = strrchr(filepath, '\\') + 1;
432 filepath[sizeof(filepath) - 1] = 0;
434 incue_max_len = sizeof(filepath) - (tmp - filepath) - 1;
436 memset(&ti, 0, sizeof(ti));
439 sector_offs = 2 * 75;
441 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
442 strncpy(dummy, linebuf, sizeof(linebuf));
443 token = strtok(dummy, " ");
449 if (!strcmp(token, "TRACK")) {
452 if (strstr(linebuf, "AUDIO") != NULL) {
453 ti[numtracks].type = CDDA;
455 else if (strstr(linebuf, "MODE1/2352") != NULL || strstr(linebuf, "MODE2/2352") != NULL) {
456 ti[numtracks].type = DATA;
459 else if (!strcmp(token, "INDEX")) {
460 sscanf(linebuf, " INDEX %02d %8s", &t, time);
461 tok2msf(time, (char *)&ti[numtracks].start);
463 t = msf2sec(ti[numtracks].start);
464 ti[numtracks].start_offset = t;
466 sec2msf(t, ti[numtracks].start);
468 // default track length to file length
469 t = file_len - ti[numtracks].start_offset;
470 sec2msf(t, ti[numtracks].length);
472 if (numtracks > 1 && ti[numtracks].handle == NULL) {
473 // this track uses the same file as the last,
474 // start of this track is last track's end
475 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
476 sec2msf(t, ti[numtracks - 1].length);
478 if (numtracks > 1 && pregapOffset == -1)
479 pregapOffset = ti[numtracks].start_offset;
481 else if (!strcmp(token, "PREGAP")) {
482 if (sscanf(linebuf, " PREGAP %8s", time) == 1) {
483 tok2msf(time, dummy);
484 sector_offs += msf2sec(dummy);
486 pregapOffset = -1; // mark to fill track start_offset
488 else if (!strcmp(token, "FILE")) {
489 sscanf(linebuf, " FILE \"%[^\"]\"", tmpb);
492 ti[numtracks + 1].handle = fopen(tmpb, "rb");
493 if (ti[numtracks + 1].handle == NULL) {
495 tmp = strrchr(tmpb, '\\');
497 tmp = strrchr(tmpb, '/');
502 strncpy(incue_fname, tmp, incue_max_len);
503 ti[numtracks + 1].handle = fopen(filepath, "rb");
506 // update global offset if this is not first file in this .cue
507 if (numtracks + 1 > 1)
508 sector_offs += file_len;
511 if (ti[numtracks + 1].handle == NULL) {
512 SysPrintf(_("\ncould not open: %s\n"), filepath);
515 fseek(ti[numtracks + 1].handle, 0, SEEK_END);
516 file_len = ftell(ti[numtracks + 1].handle) / 2352;
518 if (numtracks == 0 && strlen(isofile) >= 4 &&
519 strcmp(isofile + strlen(isofile) - 4, ".cue") == 0)
521 // user selected .cue as image file, use it's data track instead
523 cdHandle = fopen(filepath, "rb");
533 // this function tries to get the .ccd file of the given .img
534 // the necessary data is put into the ti (trackinformation)-array
535 static int parseccd(const char *isofile) {
536 char ccdname[MAXPATHLEN];
543 // copy name of the iso and change extension from .img to .ccd
544 strncpy(ccdname, isofile, sizeof(ccdname));
545 ccdname[MAXPATHLEN - 1] = '\0';
546 if (strlen(ccdname) >= 4) {
547 strcpy(ccdname + strlen(ccdname) - 4, ".ccd");
553 if ((fi = fopen(ccdname, "r")) == NULL) {
557 memset(&ti, 0, sizeof(ti));
559 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
560 if (!strncmp(linebuf, "[TRACK", 6)){
563 else if (!strncmp(linebuf, "MODE=", 5)) {
564 sscanf(linebuf, "MODE=%d", &t);
565 ti[numtracks].type = ((t == 0) ? CDDA : DATA);
567 else if (!strncmp(linebuf, "INDEX 1=", 8)) {
568 sscanf(linebuf, "INDEX 1=%d", &t);
569 sec2msf(t + 2 * 75, ti[numtracks].start);
570 ti[numtracks].start_offset = t;
572 // If we've already seen another track, this is its end
574 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
575 sec2msf(t, ti[numtracks - 1].length);
582 // Fill out the last track's end based on size
583 if (numtracks >= 1) {
584 fseek(cdHandle, 0, SEEK_END);
585 t = ftell(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75;
586 sec2msf(t, ti[numtracks].length);
592 // this function tries to get the .mds file of the given .mdf
593 // the necessary data is put into the ti (trackinformation)-array
594 static int parsemds(const char *isofile) {
595 char mdsname[MAXPATHLEN];
597 unsigned int offset, extra_offset, l, i;
602 // copy name of the iso and change extension from .mdf to .mds
603 strncpy(mdsname, isofile, sizeof(mdsname));
604 mdsname[MAXPATHLEN - 1] = '\0';
605 if (strlen(mdsname) >= 4) {
606 strcpy(mdsname + strlen(mdsname) - 4, ".mds");
612 if ((fi = fopen(mdsname, "rb")) == NULL) {
616 memset(&ti, 0, sizeof(ti));
618 // check if it's a valid mds file
619 fread(&i, 1, sizeof(unsigned int), fi);
621 if (i != 0x4944454D) {
622 // not an valid mds file
627 // get offset to session block
628 fseek(fi, 0x50, SEEK_SET);
629 fread(&offset, 1, sizeof(unsigned int), fi);
630 offset = SWAP32(offset);
632 // get total number of tracks
634 fseek(fi, offset, SEEK_SET);
635 fread(&s, 1, sizeof(unsigned short), fi);
639 // get offset to track blocks
640 fseek(fi, 4, SEEK_CUR);
641 fread(&offset, 1, sizeof(unsigned int), fi);
642 offset = SWAP32(offset);
646 fseek(fi, offset + 4, SEEK_SET);
647 if (fgetc(fi) < 0xA0) {
653 // check if the image contains mixed subchannel data
654 fseek(fi, offset + 1, SEEK_SET);
655 subChanMixed = (fgetc(fi) ? TRUE : FALSE);
658 for (i = 1; i <= numtracks; i++) {
659 fseek(fi, offset, SEEK_SET);
661 // get the track type
662 ti[i].type = ((fgetc(fi) == 0xA9) ? CDDA : DATA);
663 fseek(fi, 8, SEEK_CUR);
665 // get the track starting point
666 ti[i].start[0] = fgetc(fi);
667 ti[i].start[1] = fgetc(fi);
668 ti[i].start[2] = fgetc(fi);
670 fread(&extra_offset, 1, sizeof(unsigned int), fi);
671 extra_offset = SWAP32(extra_offset);
673 // get track start offset (in .mdf)
674 fseek(fi, offset + 0x28, SEEK_SET);
675 fread(&l, 1, sizeof(unsigned int), fi);
677 ti[i].start_offset = l / CD_FRAMESIZE_RAW;
680 fseek(fi, extra_offset, SEEK_SET);
681 fread(&l, 1, sizeof(unsigned int), fi);
684 pregapOffset = ti[i].start_offset;
686 // get the track length
687 fread(&l, 1, sizeof(unsigned int), fi);
689 sec2msf(l, ti[i].length);
698 static int parsepbp(const char *isofile) {
701 unsigned int dontcare[8];
702 unsigned int psar_offs;
715 unsigned int dontcare[6];
718 unsigned int t, cd_length, cdimg_base;
719 const char *ext = NULL;
722 if (strlen(isofile) >= 4)
723 ext = isofile + strlen(isofile) - 4;
724 if (ext == NULL || (strcmp(ext, ".pbp") != 0 && strcmp(ext, ".PBP") != 0))
729 ret = fread(&pbp_hdr, 1, sizeof(pbp_hdr), cdHandle);
730 if (ret != sizeof(pbp_hdr)) {
731 fprintf(stderr, "failed to read pbp\n");
735 ret = fseek(cdHandle, pbp_hdr.psar_offs, SEEK_SET);
737 fprintf(stderr, "failed to seek to %x\n", pbp_hdr.psar_offs);
741 fread(psar_sig, 1, sizeof(psar_sig), cdHandle);
743 if (strcmp(psar_sig, "PSISOIMG") != 0) {
744 fprintf(stderr, "bad psar_sig: %s\n", psar_sig);
749 ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x800, SEEK_SET);
751 fprintf(stderr, "failed to seek to %x\n", pbp_hdr.psar_offs);
755 // first 3 entries are special
756 fseek(cdHandle, sizeof(toc_entry), SEEK_CUR);
757 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
758 numtracks = btoi(toc_entry.index1[0]);
760 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
761 cd_length = btoi(toc_entry.index1[0]) * 60 * 75 +
762 btoi(toc_entry.index1[1]) * 75 + btoi(toc_entry.index1[2]);
764 for (i = 1; i <= numtracks; i++) {
765 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
767 ti[i].type = (toc_entry.type == 1) ? CDDA : DATA;
769 ti[i].start_offset = btoi(toc_entry.index0[0]) * 60 * 75 +
770 btoi(toc_entry.index0[1]) * 75 + btoi(toc_entry.index0[2]);
771 ti[i].start[0] = btoi(toc_entry.index1[0]);
772 ti[i].start[1] = btoi(toc_entry.index1[1]);
773 ti[i].start[2] = btoi(toc_entry.index1[2]);
776 t = msf2sec(ti[i].start) - msf2sec(ti[i - 1].start);
777 sec2msf(t, ti[i - 1].length);
780 t = cd_length - ti[numtracks].start_offset;
781 sec2msf(t, ti[numtracks].length);
784 ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x4000, SEEK_SET);
786 fprintf(stderr, "failed to seek to ISO index\n");
790 compr_img = calloc(1, sizeof(*compr_img));
791 if (compr_img == NULL)
794 compr_img->current_block = (unsigned int)-1;
796 compr_img->index_len = (0x100000 - 0x4000) / sizeof(index_entry);
797 compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
798 if (compr_img->index_table == NULL)
801 cdimg_base = pbp_hdr.psar_offs + 0x100000;
802 for (i = 0; i < compr_img->index_len; i++) {
803 ret = fread(&index_entry, 1, sizeof(index_entry), cdHandle);
804 if (ret != sizeof(index_entry)) {
805 fprintf(stderr, "failed to read index_entry #%d\n", i);
809 if (index_entry.size == 0)
812 compr_img->index_table[i] = cdimg_base + index_entry.offset;
814 compr_img->index_table[i] = cdimg_base + index_entry.offset + index_entry.size;
819 free(compr_img->index_table);
820 compr_img->index_table = NULL;
825 // this function tries to get the .sub file of the given .img
826 static int opensubfile(const char *isoname) {
827 char subname[MAXPATHLEN];
829 // copy name of the iso and change extension from .img to .sub
830 strncpy(subname, isoname, sizeof(subname));
831 subname[MAXPATHLEN - 1] = '\0';
832 if (strlen(subname) >= 4) {
833 strcpy(subname + strlen(subname) - 4, ".sub");
839 subHandle = fopen(subname, "rb");
840 if (subHandle == NULL) {
847 static int opensbifile(const char *isoname) {
848 char sbiname[MAXPATHLEN];
851 strncpy(sbiname, isoname, sizeof(sbiname));
852 sbiname[MAXPATHLEN - 1] = '\0';
853 if (strlen(sbiname) >= 4) {
854 strcpy(sbiname + strlen(sbiname) - 4, ".sbi");
860 fseek(cdHandle, 0, SEEK_END);
861 s = ftell(cdHandle) / 2352;
863 return LoadSBI(sbiname, s);
866 static int cdread_normal(FILE *f, void *dest, int sector, int offset)
868 fseek(f, sector * CD_FRAMESIZE_RAW + offset, SEEK_SET);
869 return fread(dest, 1, CD_FRAMESIZE_RAW - offset, f);
872 static int cdread_sub_mixed(FILE *f, void *dest, int sector, int offset)
876 fseek(f, sector * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE) + offset, SEEK_SET);
877 ret = fread(dest, 1, CD_FRAMESIZE_RAW - offset, f);
878 fread(subbuffer, 1, SUB_FRAMESIZE, f);
880 if (subChanRaw) DecodeRawSubData();
885 static int uncompress2(void *out, unsigned long *out_size, void *in, unsigned long in_size)
890 if (z.zalloc == NULL) {
891 // XXX: one-time leak here..
897 ret = inflateInit2(&z, -15);
900 ret = inflateReset(&z);
905 z.avail_in = in_size;
907 z.avail_out = *out_size;
909 ret = inflate(&z, Z_NO_FLUSH);
912 *out_size -= z.avail_out;
913 return ret == 1 ? 0 : ret;
916 static int cdread_compressed(FILE *f, void *dest, int sector, int offset)
918 unsigned int start_byte, size;
919 unsigned long cdbuffer_size;
923 compr_img->sector_in_blk = sector & 15;
925 if (block == compr_img->current_block) {
926 //printf("hit sect %d\n", sector);
930 if (sector >= compr_img->index_len * 16) {
931 fprintf(stderr, "sector %d is past img end\n", sector);
935 start_byte = compr_img->index_table[block];
936 if (fseek(cdHandle, start_byte, SEEK_SET) != 0) {
937 fprintf(stderr, "seek error for block %d at %x: ",
943 size = compr_img->index_table[block + 1] - start_byte;
944 if (size > sizeof(compr_img->buff_compressed)) {
945 fprintf(stderr, "block %d is too large: %u\n", block, size);
949 if (fread(compr_img->buff_compressed, 1, size, cdHandle) != size) {
950 fprintf(stderr, "read error for block %d at %x: ", block, start_byte);
955 cdbuffer_size = sizeof(compr_img->buff_raw[0]) * 16;
956 ret = uncompress2(compr_img->buff_raw[0], &cdbuffer_size, compr_img->buff_compressed, size);
958 fprintf(stderr, "uncompress failed with %d for block %d, sector %d\n",
962 if (cdbuffer_size != sizeof(compr_img->buff_raw[0]) * 16)
963 fprintf(stderr, "cdbuffer_size: %lu != %d, sector %d\n", cdbuffer_size,
964 sizeof(compr_img->buff_raw[0]) * 16, sector);
967 compr_img->current_block = block;
970 if (dest != cdbuffer) // copy avoid HACK
971 memcpy(dest, compr_img->buff_raw[compr_img->sector_in_blk] + offset,
972 CD_FRAMESIZE_RAW - offset);
973 return CD_FRAMESIZE_RAW - offset;
976 static int cdread_2048(FILE *f, void *dest, int sector, int offset)
980 fseek(f, sector * 2048, SEEK_SET);
981 ret = fread((char *)dest + 12, 1, 2048, f);
983 // not really necessary, fake mode 2 header
984 memset(cdbuffer, 0, 12);
985 sec2msf(sector + 2 * 75, (char *)cdbuffer);
991 static unsigned char * CALLBACK ISOgetBuffer_compr(void) {
992 return compr_img->buff_raw[compr_img->sector_in_blk] + 12;
995 static unsigned char * CALLBACK ISOgetBuffer(void) {
999 static void PrintTracks(void) {
1002 for (i = 1; i <= numtracks; i++) {
1003 SysPrintf(_("Track %.2d (%s) - Start %.2d:%.2d:%.2d, Length %.2d:%.2d:%.2d\n"),
1004 i, (ti[i].type == DATA ? "DATA" : "AUDIO"),
1005 ti[i].start[0], ti[i].start[1], ti[i].start[2],
1006 ti[i].length[0], ti[i].length[1], ti[i].length[2]);
1010 // This function is invoked by the front-end when opening an ISO
1011 // file for playback
1012 static long CALLBACK ISOopen(void) {
1013 boolean isMode1ISO = FALSE;
1015 if (cdHandle != NULL) {
1016 return 0; // it's already open
1019 cdHandle = fopen(GetIsoFile(), "rb");
1020 if (cdHandle == NULL) {
1024 SysPrintf(_("Loaded CD Image: %s"), GetIsoFile());
1026 cddaBigEndian = FALSE;
1027 subChanMixed = FALSE;
1031 CDR_getBuffer = ISOgetBuffer;
1032 cdimg_read_func = cdread_normal;
1034 if (parsecue(GetIsoFile()) == 0) {
1035 SysPrintf("[+cue]");
1037 else if (parsetoc(GetIsoFile()) == 0) {
1038 SysPrintf("[+toc]");
1040 else if (parseccd(GetIsoFile()) == 0) {
1041 SysPrintf("[+ccd]");
1043 else if (parsemds(GetIsoFile()) == 0) {
1044 SysPrintf("[+mds]");
1046 else if (parsepbp(GetIsoFile()) == 0) {
1048 CDR_getBuffer = ISOgetBuffer_compr;
1049 cdimg_read_func = cdread_compressed;
1052 if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
1053 SysPrintf("[+sub]");
1055 if (opensbifile(GetIsoFile()) == 0) {
1056 SysPrintf("[+sbi]");
1059 // guess whether it is mode1/2048
1060 fseek(cdHandle, 0, SEEK_END);
1061 if (ftell(cdHandle) % 2048 == 0) {
1062 unsigned int modeTest = 0;
1063 fseek(cdHandle, 0, SEEK_SET);
1064 fread(&modeTest, 4, 1, cdHandle);
1065 if (SWAP32(modeTest) != 0xffffff00) {
1066 SysPrintf("[2048]");
1070 fseek(cdHandle, 0, SEEK_SET);
1077 cdimg_read_func = cdread_sub_mixed;
1078 else if (isMode1ISO)
1079 cdimg_read_func = cdread_2048;
1081 // make sure we have another handle open for cdda
1082 if (numtracks > 1 && ti[1].handle == NULL) {
1083 ti[1].handle = fopen(GetIsoFile(), "rb");
1085 cdda_cur_sector = cdda_start_sector = 0;
1090 static long CALLBACK ISOclose(void) {
1093 if (cdHandle != NULL) {
1097 if (subHandle != NULL) {
1104 if (compr_img != NULL) {
1105 free(compr_img->index_table);
1110 for (i = 1; i <= numtracks; i++) {
1111 if (ti[i].handle != NULL) {
1112 fclose(ti[i].handle);
1113 ti[i].handle = NULL;
1122 static long CALLBACK ISOinit(void) {
1123 assert(cdHandle == NULL);
1124 assert(subHandle == NULL);
1126 return 0; // do nothing
1129 static long CALLBACK ISOshutdown(void) {
1134 // return Starting and Ending Track
1136 // byte 0 - start track
1137 // byte 1 - end track
1138 static long CALLBACK ISOgetTN(unsigned char *buffer) {
1141 if (numtracks > 0) {
1142 buffer[1] = numtracks;
1151 // return Track Time
1156 static long CALLBACK ISOgetTD(unsigned char track, unsigned char *buffer) {
1158 // CD length according pcsxr-svn (done a bit different here)
1160 unsigned char time[3];
1161 sect = msf2sec(ti[numtracks].start) + msf2sec(ti[numtracks].length);
1162 sec2msf(sect, (char *)time);
1163 buffer[2] = time[0];
1164 buffer[1] = time[1];
1165 buffer[0] = time[2];
1167 else if (numtracks > 0 && track <= numtracks) {
1168 buffer[2] = ti[track].start[0];
1169 buffer[1] = ti[track].start[1];
1170 buffer[0] = ti[track].start[2];
1181 // decode 'raw' subchannel data ripped by cdrdao
1182 static void DecodeRawSubData(void) {
1183 unsigned char subQData[12];
1186 memset(subQData, 0, sizeof(subQData));
1188 for (i = 0; i < 8 * 12; i++) {
1189 if (subbuffer[i] & (1 << 6)) { // only subchannel Q is needed
1190 subQData[i >> 3] |= (1 << (7 - (i & 7)));
1194 memcpy(&subbuffer[12], subQData, 12);
1198 // time: byte 0 - minute; byte 1 - second; byte 2 - frame
1200 static long CALLBACK ISOreadTrack(unsigned char *time) {
1201 int sector = MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2]));
1203 if (cdHandle == NULL) {
1208 subChanMissing = FALSE;
1209 if (sector >= pregapOffset) {
1211 if (sector < pregapOffset)
1212 subChanMissing = TRUE;
1216 cdimg_read_func(cdHandle, cdbuffer, sector, 12);
1218 if (subHandle != NULL) {
1219 fseek(subHandle, sector * SUB_FRAMESIZE, SEEK_SET);
1220 fread(subbuffer, 1, SUB_FRAMESIZE, subHandle);
1222 if (subChanRaw) DecodeRawSubData();
1229 // sector: byte 0 - minute; byte 1 - second; byte 2 - frame
1230 // does NOT uses bcd format
1231 static long CALLBACK ISOplay(unsigned char *time) {
1232 unsigned int i, abs_sect;
1239 abs_sect = msf2sec((char *)time);
1240 for (i = numtracks; i > 1; i--)
1241 if (msf2sec(ti[i].start) <= abs_sect + 2 * 75)
1244 file_sect = ti[i].start_offset + (abs_sect - msf2sec(ti[i].start));
1248 // find the file that contains this track
1250 if (ti[i].handle != NULL)
1253 cdda_start_sector = abs_sect - file_sect;
1254 cddaHandle = ti[i].handle;
1256 if (pregapOffset && (unsigned int)(pregapOffset - file_sect) < 2 * 75) {
1257 // get out of the missing pregap to avoid noise
1258 file_sect = pregapOffset;
1261 if (SPU_playCDDAchannel != NULL) {
1262 startCDDA(file_sect);
1268 static long CALLBACK ISOstop(void) {
1273 // gets subchannel data
1274 static unsigned char* CALLBACK ISOgetBufferSub(void) {
1275 if ((subHandle != NULL || subChanMixed) && !subChanMissing) {
1282 static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
1285 CDR__getStatus(stat);
1289 stat->Status |= 0x80;
1295 sec = cdda_start_sector + cdda_cur_sector;
1296 sec2msf(sec, (char *)stat->Time);
1301 void cdrIsoInit(void) {
1303 CDR_shutdown = ISOshutdown;
1305 CDR_close = ISOclose;
1306 CDR_getTN = ISOgetTN;
1307 CDR_getTD = ISOgetTD;
1308 CDR_readTrack = ISOreadTrack;
1309 CDR_getBuffer = ISOgetBuffer;
1312 CDR_getBufferSub = ISOgetBufferSub;
1313 CDR_getStatus = ISOgetStatus;
1315 CDR_getDriveLetter = CDR__getDriveLetter;
1316 CDR_configure = CDR__configure;
1317 CDR_test = CDR__test;
1318 CDR_about = CDR__about;
1319 CDR_setfilename = CDR__setfilename;
1324 int cdrIsoActive(void) {
1325 return (cdHandle != NULL);