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 volatile boolean playing = FALSE;
55 static boolean cddaBigEndian = FALSE;
56 static volatile unsigned int cddaCurOffset = 0;
58 char* CALLBACK CDR__getDriveLetter(void);
59 long CALLBACK CDR__configure(void);
60 long CALLBACK CDR__test(void);
61 void CALLBACK CDR__about(void);
62 long CALLBACK CDR__setfilename(char *filename);
63 long CALLBACK CDR__getStatus(struct CdrStat *stat);
65 extern void *hCDRDriver;
68 enum {DATA, CDDA} type;
69 char start[3]; // MSF-format
70 char length[3]; // MSF-format
73 #define MAXTRACKS 100 /* How many tracks can a CD hold? */
75 static int numtracks = 0;
76 static struct trackinfo ti[MAXTRACKS];
78 // get a sector from a msf-array
79 static unsigned int msf2sec(char *msf) {
80 return ((msf[0] * 60 + msf[1]) * 75) + msf[2];
83 static void sec2msf(unsigned int s, char *msf) {
85 s = s - msf[0] * 75 * 60;
91 // divide a string of xx:yy:zz into m, s, f
92 static void tok2msf(char *time, char *msf) {
95 token = strtok(time, ":");
103 token = strtok(NULL, ":");
105 msf[1] = atoi(token);
111 token = strtok(NULL, ":");
113 msf[2] = atoi(token);
121 static long GetTickCount(void) {
122 static time_t initial_time = 0;
125 gettimeofday(&now, NULL);
127 if (initial_time == 0) {
128 initial_time = now.tv_sec;
131 return (now.tv_sec - initial_time) * 1000L + now.tv_usec / 1000L;
135 // this thread plays audio data
137 static void playthread(void *param)
139 static void *playthread(void *param)
148 d = t - (long)GetTickCount();
152 else if (d > CDDA_FRAMETIME) {
161 t = GetTickCount() + CDDA_FRAMETIME;
166 for (i = 0; i < sizeof(sndbuffer) / CD_FRAMESIZE_RAW; i++) {
168 d = fread(sndbuffer + CD_FRAMESIZE_RAW * i, 1, CD_FRAMESIZE_RAW, cddaHandle);
169 if (d < CD_FRAMESIZE_RAW) {
175 // skip the subchannel data
176 fseek(cddaHandle, SUB_FRAMESIZE, SEEK_CUR);
180 s = fread(sndbuffer, 1, sizeof(sndbuffer), cddaHandle);
191 if (!cdr.Muted && playing) {
193 for (i = 0; i < s / 2; i++) {
194 tmp = sndbuffer[i * 2];
195 sndbuffer[i * 2] = sndbuffer[i * 2 + 1];
196 sndbuffer[i * 2 + 1] = tmp;
200 SPU_playCDDAchannel((short *)sndbuffer, s);
214 // stop the CDDA playback
215 static void stopCDDA() {
222 WaitForSingleObject(threadid, INFINITE);
224 pthread_join(threadid, NULL);
227 if (cddaHandle != NULL) {
235 // start the CDDA playback
236 static void startCDDA(unsigned int offset) {
238 if (initial_offset == offset) {
244 cddaHandle = fopen(GetIsoFile(), "rb");
245 if (cddaHandle == NULL) {
249 initial_offset = offset;
250 cddaCurOffset = initial_offset;
251 fseek(cddaHandle, initial_offset, SEEK_SET);
256 threadid = (HANDLE)_beginthread(playthread, 0, NULL);
258 pthread_create(&threadid, NULL, playthread, NULL);
262 // this function tries to get the .toc file of the given .bin
263 // the necessary data is put into the ti (trackinformation)-array
264 static int parsetoc(const char *isofile) {
265 char tocname[MAXPATHLEN];
267 char linebuf[256], dummy[256], name[256];
269 char time[20], time2[20];
274 // copy name of the iso and change extension from .bin to .toc
275 strncpy(tocname, isofile, sizeof(tocname));
276 tocname[MAXPATHLEN - 1] = '\0';
277 if (strlen(tocname) >= 4) {
278 strcpy(tocname + strlen(tocname) - 4, ".toc");
284 if ((fi = fopen(tocname, "r")) == NULL) {
285 // try changing extension to .cue (to satisfy some stupid tutorials)
286 strcpy(tocname + strlen(tocname) - 4, ".cue");
287 if ((fi = fopen(tocname, "r")) == NULL) {
288 // if filename is image.toc.bin, try removing .bin (for Brasero)
289 strcpy(tocname, isofile);
291 if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) {
292 tocname[t - 4] = '\0';
293 if ((fi = fopen(tocname, "r")) == NULL) {
303 memset(&ti, 0, sizeof(ti));
304 cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio
306 // parse the .toc file
307 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
309 strncpy(dummy, linebuf, sizeof(linebuf));
310 token = strtok(dummy, " ");
312 if (token == NULL) continue;
314 if (!strcmp(token, "TRACK")) {
316 token = strtok(NULL, " ");
319 if (!strncmp(token, "MODE2_RAW", 9)) {
320 ti[numtracks].type = DATA;
321 sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0
323 // check if this image contains mixed subchannel data
324 token = strtok(NULL, " ");
325 if (token != NULL && !strncmp(token, "RW_RAW", 6)) {
330 else if (!strncmp(token, "AUDIO", 5)) {
331 ti[numtracks].type = CDDA;
334 else if (!strcmp(token, "DATAFILE")) {
335 if (ti[numtracks].type == CDDA) {
336 sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2);
337 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
339 sec2msf(t, (char *)&ti[numtracks].start);
340 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
343 sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time);
344 tok2msf((char *)&time, (char *)&ti[numtracks].length);
347 else if (!strcmp(token, "FILE")) {
348 sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2);
349 tok2msf((char *)&time, (char *)&ti[numtracks].start);
350 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
351 t += msf2sec(ti[numtracks].start) + 2 * 75;
352 sec2msf(t, (char *)&ti[numtracks].start);
353 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
362 // this function tries to get the .cue file of the given .bin
363 // the necessary data is put into the ti (trackinformation)-array
364 static int parsecue(const char *isofile) {
365 char cuename[MAXPATHLEN];
370 char linebuf[256], dummy[256];
375 // copy name of the iso and change extension from .bin to .cue
376 strncpy(cuename, isofile, sizeof(cuename));
377 cuename[MAXPATHLEN - 1] = '\0';
378 if (strlen(cuename) >= 4) {
379 strcpy(cuename + strlen(cuename) - 4, ".cue");
385 if ((fi = fopen(cuename, "r")) == NULL) {
389 // Some stupid tutorials wrongly tell users to use cdrdao to rip a
390 // "bin/cue" image, which is in fact a "bin/toc" image. So let's check
392 if (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
393 if (!strncmp(linebuf, "CD_ROM_XA", 9)) {
394 // Don't proceed further, as this is actually a .toc file rather
397 return parsetoc(isofile);
399 fseek(fi, 0, SEEK_SET);
402 memset(&ti, 0, sizeof(ti));
404 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
405 strncpy(dummy, linebuf, sizeof(linebuf));
406 token = strtok(dummy, " ");
412 if (!strcmp(token, "TRACK")){
415 if (strstr(linebuf, "AUDIO") != NULL) {
416 ti[numtracks].type = CDDA;
418 else if (strstr(linebuf, "MODE1/2352") != NULL || strstr(linebuf, "MODE2/2352") != NULL) {
419 ti[numtracks].type = DATA;
422 else if (!strcmp(token, "INDEX")) {
423 tmp = strstr(linebuf, "INDEX");
425 tmp += strlen("INDEX") + 3; // 3 - space + numeric index
426 while (*tmp == ' ') tmp++;
427 if (*tmp != '\n') sscanf(tmp, "%8s", time);
430 tok2msf((char *)&time, (char *)&ti[numtracks].start);
432 t = msf2sec(ti[numtracks].start) + 2 * 75;
433 sec2msf(t, ti[numtracks].start);
435 // If we've already seen another track, this is its end
437 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
438 sec2msf(t, ti[numtracks - 1].length);
445 // Fill out the last track's end based on size
446 if (numtracks >= 1) {
447 fseek(cdHandle, 0, SEEK_END);
448 t = ftell(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75;
449 sec2msf(t, ti[numtracks].length);
455 // this function tries to get the .ccd file of the given .img
456 // the necessary data is put into the ti (trackinformation)-array
457 static int parseccd(const char *isofile) {
458 char ccdname[MAXPATHLEN];
465 // copy name of the iso and change extension from .img to .ccd
466 strncpy(ccdname, isofile, sizeof(ccdname));
467 ccdname[MAXPATHLEN - 1] = '\0';
468 if (strlen(ccdname) >= 4) {
469 strcpy(ccdname + strlen(ccdname) - 4, ".ccd");
475 if ((fi = fopen(ccdname, "r")) == NULL) {
479 memset(&ti, 0, sizeof(ti));
481 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
482 if (!strncmp(linebuf, "[TRACK", 6)){
485 else if (!strncmp(linebuf, "MODE=", 5)) {
486 sscanf(linebuf, "MODE=%d", &t);
487 ti[numtracks].type = ((t == 0) ? CDDA : DATA);
489 else if (!strncmp(linebuf, "INDEX 1=", 8)) {
490 sscanf(linebuf, "INDEX 1=%d", &t);
491 sec2msf(t + 2 * 75, ti[numtracks].start);
493 // If we've already seen another track, this is its end
495 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
496 sec2msf(t, ti[numtracks - 1].length);
503 // Fill out the last track's end based on size
504 if (numtracks >= 1) {
505 fseek(cdHandle, 0, SEEK_END);
506 t = ftell(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75;
507 sec2msf(t, ti[numtracks].length);
513 // this function tries to get the .mds file of the given .mdf
514 // the necessary data is put into the ti (trackinformation)-array
515 static int parsemds(const char *isofile) {
516 char mdsname[MAXPATHLEN];
518 unsigned int offset, extra_offset, l, i;
523 // copy name of the iso and change extension from .mdf to .mds
524 strncpy(mdsname, isofile, sizeof(mdsname));
525 mdsname[MAXPATHLEN - 1] = '\0';
526 if (strlen(mdsname) >= 4) {
527 strcpy(mdsname + strlen(mdsname) - 4, ".mds");
533 if ((fi = fopen(mdsname, "rb")) == NULL) {
537 memset(&ti, 0, sizeof(ti));
539 // check if it's a valid mds file
540 fread(&i, 1, sizeof(unsigned int), fi);
542 if (i != 0x4944454D) {
543 // not an valid mds file
548 // get offset to session block
549 fseek(fi, 0x50, SEEK_SET);
550 fread(&offset, 1, sizeof(unsigned int), fi);
551 offset = SWAP32(offset);
553 // get total number of tracks
555 fseek(fi, offset, SEEK_SET);
556 fread(&s, 1, sizeof(unsigned short), fi);
560 // get offset to track blocks
561 fseek(fi, 4, SEEK_CUR);
562 fread(&offset, 1, sizeof(unsigned int), fi);
563 offset = SWAP32(offset);
567 fseek(fi, offset + 4, SEEK_SET);
568 if (fgetc(fi) < 0xA0) {
574 // check if the image contains mixed subchannel data
575 fseek(fi, offset + 1, SEEK_SET);
576 subChanMixed = (fgetc(fi) ? TRUE : FALSE);
579 for (i = 1; i <= numtracks; i++) {
580 fseek(fi, offset, SEEK_SET);
582 // get the track type
583 ti[i].type = ((fgetc(fi) == 0xA9) ? CDDA : DATA);
584 fseek(fi, 8, SEEK_CUR);
586 // get the track starting point
587 ti[i].start[0] = fgetc(fi);
588 ti[i].start[1] = fgetc(fi);
589 ti[i].start[2] = fgetc(fi);
592 l = msf2sec(ti[i].start);
593 sec2msf(l - 2 * 75, ti[i].start); // ???
596 // get the track length
597 fread(&extra_offset, 1, sizeof(unsigned int), fi);
598 extra_offset = SWAP32(extra_offset);
600 fseek(fi, extra_offset + 4, SEEK_SET);
601 fread(&l, 1, sizeof(unsigned int), fi);
603 sec2msf(l, ti[i].length);
612 // this function tries to get the .sub file of the given .img
613 static int opensubfile(const char *isoname) {
614 char subname[MAXPATHLEN];
616 // copy name of the iso and change extension from .img to .sub
617 strncpy(subname, isoname, sizeof(subname));
618 subname[MAXPATHLEN - 1] = '\0';
619 if (strlen(subname) >= 4) {
620 strcpy(subname + strlen(subname) - 4, ".sub");
626 subHandle = fopen(subname, "rb");
627 if (subHandle == NULL) {
634 static long CALLBACK ISOinit(void) {
635 assert(cdHandle == NULL);
636 assert(subHandle == NULL);
638 return 0; // do nothing
641 static long CALLBACK ISOshutdown(void) {
642 if (cdHandle != NULL) {
646 if (subHandle != NULL) {
654 static void PrintTracks(void) {
657 for (i = 1; i <= numtracks; i++) {
658 SysPrintf(_("Track %.2d (%s) - Start %.2d:%.2d:%.2d, Length %.2d:%.2d:%.2d\n"),
659 i, (ti[i].type == DATA ? "DATA" : "AUDIO"),
660 ti[i].start[0], ti[i].start[1], ti[i].start[2],
661 ti[i].length[0], ti[i].length[1], ti[i].length[2]);
665 // This function is invoked by the front-end when opening an ISO
667 static long CALLBACK ISOopen(void) {
668 if (cdHandle != NULL) {
669 return 0; // it's already open
672 cdHandle = fopen(GetIsoFile(), "rb");
673 if (cdHandle == NULL) {
677 SysPrintf(_("Loaded CD Image: %s"), GetIsoFile());
679 cddaBigEndian = FALSE;
680 subChanMixed = FALSE;
683 if (parsecue(GetIsoFile()) == 0) {
686 else if (parsetoc(GetIsoFile()) == 0) {
689 else if (parseccd(GetIsoFile()) == 0) {
692 else if (parsemds(GetIsoFile()) == 0) {
696 if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
707 static long CALLBACK ISOclose(void) {
708 if (cdHandle != NULL) {
712 if (subHandle != NULL) {
720 // return Starting and Ending Track
722 // byte 0 - start track
723 // byte 1 - end track
724 static long CALLBACK ISOgetTN(unsigned char *buffer) {
728 buffer[1] = numtracks;
742 static long CALLBACK ISOgetTD(unsigned char track, unsigned char *buffer) {
743 if (numtracks > 0 && track <= numtracks) {
744 buffer[2] = ti[track].start[0];
745 buffer[1] = ti[track].start[1];
746 buffer[0] = ti[track].start[2];
757 // decode 'raw' subchannel data ripped by cdrdao
758 static void DecodeRawSubData(void) {
759 unsigned char subQData[12];
762 memset(subQData, 0, sizeof(subQData));
764 for (i = 0; i < 8 * 12; i++) {
765 if (subbuffer[i] & (1 << 6)) { // only subchannel Q is needed
766 subQData[i >> 3] |= (1 << (7 - (i & 7)));
770 memcpy(&subbuffer[12], subQData, 12);
774 // time: byte 0 - minute; byte 1 - second; byte 2 - frame
776 static long CALLBACK ISOreadTrack(unsigned char *time) {
777 if (cdHandle == NULL) {
782 fseek(cdHandle, MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2])) * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE) + 12, SEEK_SET);
783 fread(cdbuffer, 1, DATA_SIZE, cdHandle);
784 fread(subbuffer, 1, SUB_FRAMESIZE, cdHandle);
786 if (subChanRaw) DecodeRawSubData();
789 fseek(cdHandle, MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2])) * CD_FRAMESIZE_RAW + 12, SEEK_SET);
790 fread(cdbuffer, 1, DATA_SIZE, cdHandle);
792 if (subHandle != NULL) {
793 fseek(subHandle, MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2])) * SUB_FRAMESIZE, SEEK_SET);
794 fread(subbuffer, 1, SUB_FRAMESIZE, subHandle);
796 if (subChanRaw) DecodeRawSubData();
803 // return readed track
804 static unsigned char * CALLBACK ISOgetBuffer(void) {
809 // sector: byte 0 - minute; byte 1 - second; byte 2 - frame
810 // does NOT uses bcd format
811 static long CALLBACK ISOplay(unsigned char *time) {
812 if (SPU_playCDDAchannel != NULL) {
814 startCDDA(MSF2SECT(time[0], time[1], time[2]) * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE));
817 startCDDA(MSF2SECT(time[0], time[1], time[2]) * CD_FRAMESIZE_RAW);
824 static long CALLBACK ISOstop(void) {
829 // gets subchannel data
830 static unsigned char* CALLBACK ISOgetBufferSub(void) {
831 if (subHandle != NULL || subChanMixed) {
838 static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
841 CDR__getStatus(stat);
845 stat->Status |= 0x80;
846 sec = cddaCurOffset / CD_FRAMESIZE_RAW;
847 sec2msf(sec, (char *)stat->Time);
856 void cdrIsoInit(void) {
858 CDR_shutdown = ISOshutdown;
860 CDR_close = ISOclose;
861 CDR_getTN = ISOgetTN;
862 CDR_getTD = ISOgetTD;
863 CDR_readTrack = ISOreadTrack;
864 CDR_getBuffer = ISOgetBuffer;
867 CDR_getBufferSub = ISOgetBufferSub;
868 CDR_getStatus = ISOgetStatus;
870 CDR_getDriveLetter = CDR__getDriveLetter;
871 CDR_configure = CDR__configure;
872 CDR_test = CDR__test;
873 CDR_about = CDR__about;
874 CDR_setfilename = CDR__setfilename;
879 int cdrIsoActive(void) {
880 return (cdHandle != NULL);