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"
34 static FILE *cdHandle = NULL;
35 static FILE *cddaHandle = NULL;
36 static FILE *subHandle = NULL;
38 static boolean subChanMixed = FALSE;
39 static boolean subChanRaw = FALSE;
41 static unsigned char cdbuffer[DATA_SIZE];
42 static unsigned char subbuffer[SUB_FRAMESIZE];
44 static unsigned char sndbuffer[CD_FRAMESIZE_RAW * 10];
46 #define CDDA_FRAMETIME (1000 * (sizeof(sndbuffer) / CD_FRAMESIZE_RAW) / 75)
49 static HANDLE threadid;
51 static pthread_t threadid;
53 static unsigned int initial_offset = 0;
54 static boolean playing = FALSE;
55 static boolean cddaBigEndian = FALSE;
56 static unsigned int cddaCurOffset = 0;
57 static unsigned int cddaStartOffset;
59 char* CALLBACK CDR__getDriveLetter(void);
60 long CALLBACK CDR__configure(void);
61 long CALLBACK CDR__test(void);
62 void CALLBACK CDR__about(void);
63 long CALLBACK CDR__setfilename(char *filename);
64 long CALLBACK CDR__getStatus(struct CdrStat *stat);
66 extern void *hCDRDriver;
69 enum {DATA, CDDA} type;
70 char start[3]; // MSF-format
71 char length[3]; // MSF-format
72 FILE *handle; // for multi-track images CDDA
75 #define MAXTRACKS 100 /* How many tracks can a CD hold? */
77 static int numtracks = 0;
78 static struct trackinfo ti[MAXTRACKS];
80 // get a sector from a msf-array
81 static unsigned int msf2sec(char *msf) {
82 return ((msf[0] * 60 + msf[1]) * 75) + msf[2];
85 static void sec2msf(unsigned int s, char *msf) {
87 s = s - msf[0] * 75 * 60;
93 // divide a string of xx:yy:zz into m, s, f
94 static void tok2msf(char *time, char *msf) {
97 token = strtok(time, ":");
105 token = strtok(NULL, ":");
107 msf[1] = atoi(token);
113 token = strtok(NULL, ":");
115 msf[2] = atoi(token);
123 static long GetTickCount(void) {
124 static time_t initial_time = 0;
127 gettimeofday(&now, NULL);
129 if (initial_time == 0) {
130 initial_time = now.tv_sec;
133 return (now.tv_sec - initial_time) * 1000L + now.tv_usec / 1000L;
137 // this thread plays audio data
139 static void playthread(void *param)
141 static void *playthread(void *param)
150 d = t - (long)GetTickCount();
154 else if (d > CDDA_FRAMETIME) {
162 // HACK: stop feeding data while emu is paused
169 t = GetTickCount() + CDDA_FRAMETIME;
174 for (i = 0; i < sizeof(sndbuffer) / CD_FRAMESIZE_RAW; i++) {
176 d = fread(sndbuffer + CD_FRAMESIZE_RAW * i, 1, CD_FRAMESIZE_RAW, cddaHandle);
177 if (d < CD_FRAMESIZE_RAW) {
183 // skip the subchannel data
184 fseek(cddaHandle, SUB_FRAMESIZE, SEEK_CUR);
188 s = fread(sndbuffer, 1, sizeof(sndbuffer), cddaHandle);
197 if (!cdr.Muted && playing) {
199 for (i = 0; i < s / 2; i++) {
200 tmp = sndbuffer[i * 2];
201 sndbuffer[i * 2] = sndbuffer[i * 2 + 1];
202 sndbuffer[i * 2 + 1] = tmp;
206 SPU_playCDDAchannel((short *)sndbuffer, s);
220 // stop the CDDA playback
221 static void stopCDDA() {
228 WaitForSingleObject(threadid, INFINITE);
230 pthread_join(threadid, NULL);
236 // start the CDDA playback
237 static void startCDDA(unsigned int offset) {
239 if (initial_offset == offset) {
245 initial_offset = offset;
246 cddaCurOffset = initial_offset;
247 fseek(cddaHandle, initial_offset, SEEK_SET);
252 threadid = (HANDLE)_beginthread(playthread, 0, NULL);
254 pthread_create(&threadid, NULL, playthread, NULL);
258 // this function tries to get the .toc file of the given .bin
259 // the necessary data is put into the ti (trackinformation)-array
260 static int parsetoc(const char *isofile) {
261 char tocname[MAXPATHLEN];
263 char linebuf[256], dummy[256], name[256];
265 char time[20], time2[20];
270 // copy name of the iso and change extension from .bin to .toc
271 strncpy(tocname, isofile, sizeof(tocname));
272 tocname[MAXPATHLEN - 1] = '\0';
273 if (strlen(tocname) >= 4) {
274 strcpy(tocname + strlen(tocname) - 4, ".toc");
280 if ((fi = fopen(tocname, "r")) == NULL) {
281 // try changing extension to .cue (to satisfy some stupid tutorials)
282 strcpy(tocname + strlen(tocname) - 4, ".cue");
283 if ((fi = fopen(tocname, "r")) == NULL) {
284 // if filename is image.toc.bin, try removing .bin (for Brasero)
285 strcpy(tocname, isofile);
287 if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) {
288 tocname[t - 4] = '\0';
289 if ((fi = fopen(tocname, "r")) == NULL) {
299 memset(&ti, 0, sizeof(ti));
300 cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio
302 // parse the .toc file
303 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
305 strncpy(dummy, linebuf, sizeof(linebuf));
306 token = strtok(dummy, " ");
308 if (token == NULL) continue;
310 if (!strcmp(token, "TRACK")) {
312 token = strtok(NULL, " ");
315 if (!strncmp(token, "MODE2_RAW", 9)) {
316 ti[numtracks].type = DATA;
317 sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0
319 // check if this image contains mixed subchannel data
320 token = strtok(NULL, " ");
321 if (token != NULL && !strncmp(token, "RW_RAW", 6)) {
326 else if (!strncmp(token, "AUDIO", 5)) {
327 ti[numtracks].type = CDDA;
330 else if (!strcmp(token, "DATAFILE")) {
331 if (ti[numtracks].type == CDDA) {
332 sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2);
333 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
335 sec2msf(t, (char *)&ti[numtracks].start);
336 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
339 sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time);
340 tok2msf((char *)&time, (char *)&ti[numtracks].length);
343 else if (!strcmp(token, "FILE")) {
344 sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2);
345 tok2msf((char *)&time, (char *)&ti[numtracks].start);
346 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
347 t += msf2sec(ti[numtracks].start) + 2 * 75;
348 sec2msf(t, (char *)&ti[numtracks].start);
349 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
358 // this function tries to get the .cue file of the given .bin
359 // the necessary data is put into the ti (trackinformation)-array
360 static int parsecue(const char *isofile) {
361 char cuename[MAXPATHLEN];
362 char filepath[MAXPATHLEN];
368 char linebuf[256], dummy[256];
369 unsigned int incue_max_len;
370 unsigned int t, i, total, file_len;
374 // copy name of the iso and change extension from .bin to .cue
375 strncpy(cuename, isofile, sizeof(cuename));
376 cuename[MAXPATHLEN - 1] = '\0';
377 if (strlen(cuename) >= 4) {
378 strcpy(cuename + strlen(cuename) - 4, ".cue");
384 if ((fi = fopen(cuename, "r")) == NULL) {
388 // Some stupid tutorials wrongly tell users to use cdrdao to rip a
389 // "bin/cue" image, which is in fact a "bin/toc" image. So let's check
391 if (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
392 if (!strncmp(linebuf, "CD_ROM_XA", 9)) {
393 // Don't proceed further, as this is actually a .toc file rather
396 return parsetoc(isofile);
398 fseek(fi, 0, SEEK_SET);
401 // build a path for files referenced in .cue
402 strncpy(filepath, cuename, sizeof(filepath));
403 tmp = strrchr(filepath, '/') + 1;
405 tmp = strrchr(filepath, '\\') + 1;
409 filepath[sizeof(filepath) - 1] = 0;
411 incue_max_len = sizeof(filepath) - (tmp - filepath) - 1;
413 memset(&ti, 0, sizeof(ti));
415 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
416 strncpy(dummy, linebuf, sizeof(linebuf));
417 token = strtok(dummy, " ");
423 if (!strcmp(token, "TRACK")){
426 if (strstr(linebuf, "AUDIO") != NULL) {
427 ti[numtracks].type = CDDA;
429 else if (strstr(linebuf, "MODE1/2352") != NULL || strstr(linebuf, "MODE2/2352") != NULL) {
430 ti[numtracks].type = DATA;
433 else if (!strcmp(token, "INDEX")) {
434 tmp = strstr(linebuf, "INDEX");
436 tmp += strlen("INDEX") + 3; // 3 - space + numeric index
437 while (*tmp == ' ') tmp++;
438 if (*tmp != '\n') sscanf(tmp, "%8s", time);
441 tok2msf((char *)&time, (char *)&ti[numtracks].start);
443 t = msf2sec(ti[numtracks].start) + 2 * 75;
444 sec2msf(t, ti[numtracks].start);
446 else if (!strcmp(token, "FILE")) {
447 tmp = strstr(linebuf, "FILE");
456 tmp = strchr(token, '"');
462 tmp = strchr(token, ' ');
467 strncpy(incue_fname, token, incue_max_len);
468 ti[numtracks + 1].handle = fopen(filepath, "rb");
469 if (ti[numtracks + 1].handle == NULL) {
470 SysPrintf(_("could not open: %s\n"), filepath);
472 else if (numtracks == 0 && strlen(isofile) >= 4 &&
473 strcmp(isofile + strlen(isofile) - 4, ".cue") == 0) {
474 // user selected .cue as image file, use it's data track instead
476 cdHandle = fopen(filepath, "rb");
483 // make corrections for multi-track .cue, fill track lengths
486 for (i = 1; i <= numtracks; i++) {
487 if (ti[i].handle != NULL) {
488 sec2msf(total, ti[i].start);
489 fseek(ti[i].handle, 0, SEEK_END);
490 file_len = ftell(ti[i].handle) / 2352;
491 sec2msf(file_len, ti[i].length);
495 // this track uses the same file as the last,
496 // start of this track is last track's end
498 t = msf2sec(ti[i].start) - msf2sec(ti[i - 1].start);
499 sec2msf(t, ti[i - 1].length);
501 t = file_len - msf2sec(ti[i].start) + 2 * 75;
502 sec2msf(t, ti[i].length);
509 // this function tries to get the .ccd file of the given .img
510 // the necessary data is put into the ti (trackinformation)-array
511 static int parseccd(const char *isofile) {
512 char ccdname[MAXPATHLEN];
519 // copy name of the iso and change extension from .img to .ccd
520 strncpy(ccdname, isofile, sizeof(ccdname));
521 ccdname[MAXPATHLEN - 1] = '\0';
522 if (strlen(ccdname) >= 4) {
523 strcpy(ccdname + strlen(ccdname) - 4, ".ccd");
529 if ((fi = fopen(ccdname, "r")) == NULL) {
533 memset(&ti, 0, sizeof(ti));
535 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
536 if (!strncmp(linebuf, "[TRACK", 6)){
539 else if (!strncmp(linebuf, "MODE=", 5)) {
540 sscanf(linebuf, "MODE=%d", &t);
541 ti[numtracks].type = ((t == 0) ? CDDA : DATA);
543 else if (!strncmp(linebuf, "INDEX 1=", 8)) {
544 sscanf(linebuf, "INDEX 1=%d", &t);
545 sec2msf(t + 2 * 75, ti[numtracks].start);
547 // If we've already seen another track, this is its end
549 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
550 sec2msf(t, ti[numtracks - 1].length);
557 // Fill out the last track's end based on size
558 if (numtracks >= 1) {
559 fseek(cdHandle, 0, SEEK_END);
560 t = ftell(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75;
561 sec2msf(t, ti[numtracks].length);
567 // this function tries to get the .mds file of the given .mdf
568 // the necessary data is put into the ti (trackinformation)-array
569 static int parsemds(const char *isofile) {
570 char mdsname[MAXPATHLEN];
572 unsigned int offset, extra_offset, l, i;
577 // copy name of the iso and change extension from .mdf to .mds
578 strncpy(mdsname, isofile, sizeof(mdsname));
579 mdsname[MAXPATHLEN - 1] = '\0';
580 if (strlen(mdsname) >= 4) {
581 strcpy(mdsname + strlen(mdsname) - 4, ".mds");
587 if ((fi = fopen(mdsname, "rb")) == NULL) {
591 memset(&ti, 0, sizeof(ti));
593 // check if it's a valid mds file
594 fread(&i, 1, sizeof(unsigned int), fi);
596 if (i != 0x4944454D) {
597 // not an valid mds file
602 // get offset to session block
603 fseek(fi, 0x50, SEEK_SET);
604 fread(&offset, 1, sizeof(unsigned int), fi);
605 offset = SWAP32(offset);
607 // get total number of tracks
609 fseek(fi, offset, SEEK_SET);
610 fread(&s, 1, sizeof(unsigned short), fi);
614 // get offset to track blocks
615 fseek(fi, 4, SEEK_CUR);
616 fread(&offset, 1, sizeof(unsigned int), fi);
617 offset = SWAP32(offset);
621 fseek(fi, offset + 4, SEEK_SET);
622 if (fgetc(fi) < 0xA0) {
628 // check if the image contains mixed subchannel data
629 fseek(fi, offset + 1, SEEK_SET);
630 subChanMixed = (fgetc(fi) ? TRUE : FALSE);
633 for (i = 1; i <= numtracks; i++) {
634 fseek(fi, offset, SEEK_SET);
636 // get the track type
637 ti[i].type = ((fgetc(fi) == 0xA9) ? CDDA : DATA);
638 fseek(fi, 8, SEEK_CUR);
640 // get the track starting point
641 ti[i].start[0] = fgetc(fi);
642 ti[i].start[1] = fgetc(fi);
643 ti[i].start[2] = fgetc(fi);
646 l = msf2sec(ti[i].start);
647 sec2msf(l - 2 * 75, ti[i].start); // ???
650 // get the track length
651 fread(&extra_offset, 1, sizeof(unsigned int), fi);
652 extra_offset = SWAP32(extra_offset);
654 fseek(fi, extra_offset + 4, SEEK_SET);
655 fread(&l, 1, sizeof(unsigned int), fi);
657 sec2msf(l, ti[i].length);
666 // this function tries to get the .sub file of the given .img
667 static int opensubfile(const char *isoname) {
668 char subname[MAXPATHLEN];
670 // copy name of the iso and change extension from .img to .sub
671 strncpy(subname, isoname, sizeof(subname));
672 subname[MAXPATHLEN - 1] = '\0';
673 if (strlen(subname) >= 4) {
674 strcpy(subname + strlen(subname) - 4, ".sub");
680 subHandle = fopen(subname, "rb");
681 if (subHandle == NULL) {
688 static void PrintTracks(void) {
691 for (i = 1; i <= numtracks; i++) {
692 SysPrintf(_("Track %.2d (%s) - Start %.2d:%.2d:%.2d, Length %.2d:%.2d:%.2d\n"),
693 i, (ti[i].type == DATA ? "DATA" : "AUDIO"),
694 ti[i].start[0], ti[i].start[1], ti[i].start[2],
695 ti[i].length[0], ti[i].length[1], ti[i].length[2]);
699 // This function is invoked by the front-end when opening an ISO
701 static long CALLBACK ISOopen(void) {
702 if (cdHandle != NULL) {
703 return 0; // it's already open
706 cdHandle = fopen(GetIsoFile(), "rb");
707 if (cdHandle == NULL) {
711 SysPrintf(_("Loaded CD Image: %s"), GetIsoFile());
713 cddaBigEndian = FALSE;
714 subChanMixed = FALSE;
717 if (parsecue(GetIsoFile()) == 0) {
720 else if (parsetoc(GetIsoFile()) == 0) {
723 else if (parseccd(GetIsoFile()) == 0) {
726 else if (parsemds(GetIsoFile()) == 0) {
730 if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
738 // make sure we have another handle open for cdda
739 if (numtracks > 1 && ti[1].handle == NULL) {
740 ti[1].handle = fopen(GetIsoFile(), "rb");
746 static long CALLBACK ISOclose(void) {
749 if (cdHandle != NULL) {
753 if (subHandle != NULL) {
760 for (i = 1; i <= numtracks; i++) {
761 if (ti[i].handle != NULL) {
762 fclose(ti[i].handle);
771 static long CALLBACK ISOinit(void) {
772 assert(cdHandle == NULL);
773 assert(subHandle == NULL);
775 return 0; // do nothing
778 static long CALLBACK ISOshutdown(void) {
783 // return Starting and Ending Track
785 // byte 0 - start track
786 // byte 1 - end track
787 static long CALLBACK ISOgetTN(unsigned char *buffer) {
791 buffer[1] = numtracks;
805 static long CALLBACK ISOgetTD(unsigned char track, unsigned char *buffer) {
806 if (numtracks > 0 && track <= numtracks) {
807 buffer[2] = ti[track].start[0];
808 buffer[1] = ti[track].start[1];
809 buffer[0] = ti[track].start[2];
820 // decode 'raw' subchannel data ripped by cdrdao
821 static void DecodeRawSubData(void) {
822 unsigned char subQData[12];
825 memset(subQData, 0, sizeof(subQData));
827 for (i = 0; i < 8 * 12; i++) {
828 if (subbuffer[i] & (1 << 6)) { // only subchannel Q is needed
829 subQData[i >> 3] |= (1 << (7 - (i & 7)));
833 memcpy(&subbuffer[12], subQData, 12);
837 // time: byte 0 - minute; byte 1 - second; byte 2 - frame
839 static long CALLBACK ISOreadTrack(unsigned char *time) {
840 if (cdHandle == NULL) {
845 fseek(cdHandle, MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2])) * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE) + 12, SEEK_SET);
846 fread(cdbuffer, 1, DATA_SIZE, cdHandle);
847 fread(subbuffer, 1, SUB_FRAMESIZE, cdHandle);
849 if (subChanRaw) DecodeRawSubData();
852 fseek(cdHandle, MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2])) * CD_FRAMESIZE_RAW + 12, SEEK_SET);
853 fread(cdbuffer, 1, DATA_SIZE, cdHandle);
855 if (subHandle != NULL) {
856 fseek(subHandle, MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2])) * SUB_FRAMESIZE, SEEK_SET);
857 fread(subbuffer, 1, SUB_FRAMESIZE, subHandle);
859 if (subChanRaw) DecodeRawSubData();
866 // return readed track
867 static unsigned char * CALLBACK ISOgetBuffer(void) {
872 // sector: byte 0 - minute; byte 1 - second; byte 2 - frame
873 // does NOT uses bcd format
874 static long CALLBACK ISOplay(unsigned char *time) {
875 unsigned int i, sect;
881 sect = msf2sec(time);
882 for (i = numtracks; i > 1; i--)
883 if (msf2sec(ti[i].start) <= sect + 2 * 75)
886 // find the file that contains this track
888 if (ti[i].handle != NULL)
891 cddaStartOffset = msf2sec(ti[i].start);
892 sect -= cddaStartOffset - 2 * 75;
893 cddaHandle = ti[i].handle;
895 if (SPU_playCDDAchannel != NULL) {
897 startCDDA(sect * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE));
900 startCDDA(sect * CD_FRAMESIZE_RAW);
907 static long CALLBACK ISOstop(void) {
912 // gets subchannel data
913 static unsigned char* CALLBACK ISOgetBufferSub(void) {
914 if (subHandle != NULL || subChanMixed) {
921 static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
924 CDR__getStatus(stat);
928 stat->Status |= 0x80;
929 sec = (cddaStartOffset + cddaCurOffset) / CD_FRAMESIZE_RAW;
930 sec2msf(sec, (char *)stat->Time);
939 void cdrIsoInit(void) {
941 CDR_shutdown = ISOshutdown;
943 CDR_close = ISOclose;
944 CDR_getTN = ISOgetTN;
945 CDR_getTD = ISOgetTD;
946 CDR_readTrack = ISOreadTrack;
947 CDR_getBuffer = ISOgetBuffer;
950 CDR_getBufferSub = ISOgetBufferSub;
951 CDR_getStatus = ISOgetStatus;
953 CDR_getDriveLetter = CDR__getDriveLetter;
954 CDR_configure = CDR__configure;
955 CDR_test = CDR__test;
956 CDR_about = CDR__about;
957 CDR_setfilename = CDR__setfilename;
962 int cdrIsoActive(void) {
963 return (cdHandle != NULL);