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) {
163 t = GetTickCount() + CDDA_FRAMETIME;
168 for (i = 0; i < sizeof(sndbuffer) / CD_FRAMESIZE_RAW; i++) {
170 d = fread(sndbuffer + CD_FRAMESIZE_RAW * i, 1, CD_FRAMESIZE_RAW, cddaHandle);
171 if (d < CD_FRAMESIZE_RAW) {
177 // skip the subchannel data
178 fseek(cddaHandle, SUB_FRAMESIZE, SEEK_CUR);
182 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);
230 // start the CDDA playback
231 static void startCDDA(unsigned int offset) {
233 if (initial_offset == offset) {
239 initial_offset = offset;
240 cddaCurOffset = initial_offset;
241 fseek(cddaHandle, initial_offset, SEEK_SET);
246 threadid = (HANDLE)_beginthread(playthread, 0, NULL);
248 pthread_create(&threadid, NULL, playthread, NULL);
252 // this function tries to get the .toc file of the given .bin
253 // the necessary data is put into the ti (trackinformation)-array
254 static int parsetoc(const char *isofile) {
255 char tocname[MAXPATHLEN];
257 char linebuf[256], dummy[256], name[256];
259 char time[20], time2[20];
264 // copy name of the iso and change extension from .bin to .toc
265 strncpy(tocname, isofile, sizeof(tocname));
266 tocname[MAXPATHLEN - 1] = '\0';
267 if (strlen(tocname) >= 4) {
268 strcpy(tocname + strlen(tocname) - 4, ".toc");
274 if ((fi = fopen(tocname, "r")) == NULL) {
275 // try changing extension to .cue (to satisfy some stupid tutorials)
276 strcpy(tocname + strlen(tocname) - 4, ".cue");
277 if ((fi = fopen(tocname, "r")) == NULL) {
278 // if filename is image.toc.bin, try removing .bin (for Brasero)
279 strcpy(tocname, isofile);
281 if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) {
282 tocname[t - 4] = '\0';
283 if ((fi = fopen(tocname, "r")) == NULL) {
293 memset(&ti, 0, sizeof(ti));
294 cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio
296 // parse the .toc file
297 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
299 strncpy(dummy, linebuf, sizeof(linebuf));
300 token = strtok(dummy, " ");
302 if (token == NULL) continue;
304 if (!strcmp(token, "TRACK")) {
306 token = strtok(NULL, " ");
309 if (!strncmp(token, "MODE2_RAW", 9)) {
310 ti[numtracks].type = DATA;
311 sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0
313 // check if this image contains mixed subchannel data
314 token = strtok(NULL, " ");
315 if (token != NULL && !strncmp(token, "RW_RAW", 6)) {
320 else if (!strncmp(token, "AUDIO", 5)) {
321 ti[numtracks].type = CDDA;
324 else if (!strcmp(token, "DATAFILE")) {
325 if (ti[numtracks].type == CDDA) {
326 sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2);
327 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
329 sec2msf(t, (char *)&ti[numtracks].start);
330 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
333 sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time);
334 tok2msf((char *)&time, (char *)&ti[numtracks].length);
337 else if (!strcmp(token, "FILE")) {
338 sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2);
339 tok2msf((char *)&time, (char *)&ti[numtracks].start);
340 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
341 t += msf2sec(ti[numtracks].start) + 2 * 75;
342 sec2msf(t, (char *)&ti[numtracks].start);
343 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
352 // this function tries to get the .cue file of the given .bin
353 // the necessary data is put into the ti (trackinformation)-array
354 static int parsecue(const char *isofile) {
355 char cuename[MAXPATHLEN];
356 char filepath[MAXPATHLEN];
362 char linebuf[256], dummy[256];
363 unsigned int incue_max_len;
364 unsigned int t, i, total, file_len;
368 // copy name of the iso and change extension from .bin to .cue
369 strncpy(cuename, isofile, sizeof(cuename));
370 cuename[MAXPATHLEN - 1] = '\0';
371 if (strlen(cuename) >= 4) {
372 strcpy(cuename + strlen(cuename) - 4, ".cue");
378 if ((fi = fopen(cuename, "r")) == NULL) {
382 // Some stupid tutorials wrongly tell users to use cdrdao to rip a
383 // "bin/cue" image, which is in fact a "bin/toc" image. So let's check
385 if (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
386 if (!strncmp(linebuf, "CD_ROM_XA", 9)) {
387 // Don't proceed further, as this is actually a .toc file rather
390 return parsetoc(isofile);
392 fseek(fi, 0, SEEK_SET);
395 // build a path for files referenced in .cue
396 strncpy(filepath, cuename, sizeof(filepath));
397 tmp = strrchr(filepath, '/') + 1;
399 tmp = strrchr(filepath, '\\') + 1;
403 filepath[sizeof(filepath) - 1] = 0;
405 incue_max_len = sizeof(filepath) - (tmp - filepath) - 1;
407 memset(&ti, 0, sizeof(ti));
409 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
410 strncpy(dummy, linebuf, sizeof(linebuf));
411 token = strtok(dummy, " ");
417 if (!strcmp(token, "TRACK")){
420 if (strstr(linebuf, "AUDIO") != NULL) {
421 ti[numtracks].type = CDDA;
423 else if (strstr(linebuf, "MODE1/2352") != NULL || strstr(linebuf, "MODE2/2352") != NULL) {
424 ti[numtracks].type = DATA;
427 else if (!strcmp(token, "INDEX")) {
428 tmp = strstr(linebuf, "INDEX");
430 tmp += strlen("INDEX") + 3; // 3 - space + numeric index
431 while (*tmp == ' ') tmp++;
432 if (*tmp != '\n') sscanf(tmp, "%8s", time);
435 tok2msf((char *)&time, (char *)&ti[numtracks].start);
437 t = msf2sec(ti[numtracks].start) + 2 * 75;
438 sec2msf(t, ti[numtracks].start);
440 else if (!strcmp(token, "FILE")) {
441 tmp = strstr(linebuf, "FILE");
450 tmp = strchr(token, '"');
456 tmp = strchr(token, ' ');
461 strncpy(incue_fname, token, incue_max_len);
462 ti[numtracks + 1].handle = fopen(filepath, "rb");
463 if (ti[numtracks + 1].handle == NULL) {
464 SysPrintf(_("could not open: %s\n"), filepath);
466 else if (numtracks == 0 && strlen(isofile) >= 4 &&
467 strcmp(isofile + strlen(isofile) - 4, ".cue") == 0) {
468 // user selected .cue as image file, use it's data track instead
470 cdHandle = fopen(filepath, "rb");
477 // make corrections for multi-track .cue, fill track lengths
480 for (i = 1; i <= numtracks; i++) {
481 if (ti[i].handle != NULL) {
482 sec2msf(total, ti[i].start);
483 fseek(ti[i].handle, 0, SEEK_END);
484 file_len = ftell(ti[i].handle) / 2352;
485 sec2msf(file_len, ti[i].length);
489 // this track uses the same file as the last,
490 // start of this track is last track's end
492 t = msf2sec(ti[i].start) - msf2sec(ti[i - 1].start);
493 sec2msf(t, ti[i - 1].length);
495 t = file_len - msf2sec(ti[i].start) + 2 * 75;
496 sec2msf(t, ti[i].length);
503 // this function tries to get the .ccd file of the given .img
504 // the necessary data is put into the ti (trackinformation)-array
505 static int parseccd(const char *isofile) {
506 char ccdname[MAXPATHLEN];
513 // copy name of the iso and change extension from .img to .ccd
514 strncpy(ccdname, isofile, sizeof(ccdname));
515 ccdname[MAXPATHLEN - 1] = '\0';
516 if (strlen(ccdname) >= 4) {
517 strcpy(ccdname + strlen(ccdname) - 4, ".ccd");
523 if ((fi = fopen(ccdname, "r")) == NULL) {
527 memset(&ti, 0, sizeof(ti));
529 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
530 if (!strncmp(linebuf, "[TRACK", 6)){
533 else if (!strncmp(linebuf, "MODE=", 5)) {
534 sscanf(linebuf, "MODE=%d", &t);
535 ti[numtracks].type = ((t == 0) ? CDDA : DATA);
537 else if (!strncmp(linebuf, "INDEX 1=", 8)) {
538 sscanf(linebuf, "INDEX 1=%d", &t);
539 sec2msf(t + 2 * 75, ti[numtracks].start);
541 // If we've already seen another track, this is its end
543 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
544 sec2msf(t, ti[numtracks - 1].length);
551 // Fill out the last track's end based on size
552 if (numtracks >= 1) {
553 fseek(cdHandle, 0, SEEK_END);
554 t = ftell(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75;
555 sec2msf(t, ti[numtracks].length);
561 // this function tries to get the .mds file of the given .mdf
562 // the necessary data is put into the ti (trackinformation)-array
563 static int parsemds(const char *isofile) {
564 char mdsname[MAXPATHLEN];
566 unsigned int offset, extra_offset, l, i;
571 // copy name of the iso and change extension from .mdf to .mds
572 strncpy(mdsname, isofile, sizeof(mdsname));
573 mdsname[MAXPATHLEN - 1] = '\0';
574 if (strlen(mdsname) >= 4) {
575 strcpy(mdsname + strlen(mdsname) - 4, ".mds");
581 if ((fi = fopen(mdsname, "rb")) == NULL) {
585 memset(&ti, 0, sizeof(ti));
587 // check if it's a valid mds file
588 fread(&i, 1, sizeof(unsigned int), fi);
590 if (i != 0x4944454D) {
591 // not an valid mds file
596 // get offset to session block
597 fseek(fi, 0x50, SEEK_SET);
598 fread(&offset, 1, sizeof(unsigned int), fi);
599 offset = SWAP32(offset);
601 // get total number of tracks
603 fseek(fi, offset, SEEK_SET);
604 fread(&s, 1, sizeof(unsigned short), fi);
608 // get offset to track blocks
609 fseek(fi, 4, SEEK_CUR);
610 fread(&offset, 1, sizeof(unsigned int), fi);
611 offset = SWAP32(offset);
615 fseek(fi, offset + 4, SEEK_SET);
616 if (fgetc(fi) < 0xA0) {
622 // check if the image contains mixed subchannel data
623 fseek(fi, offset + 1, SEEK_SET);
624 subChanMixed = (fgetc(fi) ? TRUE : FALSE);
627 for (i = 1; i <= numtracks; i++) {
628 fseek(fi, offset, SEEK_SET);
630 // get the track type
631 ti[i].type = ((fgetc(fi) == 0xA9) ? CDDA : DATA);
632 fseek(fi, 8, SEEK_CUR);
634 // get the track starting point
635 ti[i].start[0] = fgetc(fi);
636 ti[i].start[1] = fgetc(fi);
637 ti[i].start[2] = fgetc(fi);
640 l = msf2sec(ti[i].start);
641 sec2msf(l - 2 * 75, ti[i].start); // ???
644 // get the track length
645 fread(&extra_offset, 1, sizeof(unsigned int), fi);
646 extra_offset = SWAP32(extra_offset);
648 fseek(fi, extra_offset + 4, SEEK_SET);
649 fread(&l, 1, sizeof(unsigned int), fi);
651 sec2msf(l, ti[i].length);
660 // this function tries to get the .sub file of the given .img
661 static int opensubfile(const char *isoname) {
662 char subname[MAXPATHLEN];
664 // copy name of the iso and change extension from .img to .sub
665 strncpy(subname, isoname, sizeof(subname));
666 subname[MAXPATHLEN - 1] = '\0';
667 if (strlen(subname) >= 4) {
668 strcpy(subname + strlen(subname) - 4, ".sub");
674 subHandle = fopen(subname, "rb");
675 if (subHandle == NULL) {
682 static void PrintTracks(void) {
685 for (i = 1; i <= numtracks; i++) {
686 SysPrintf(_("Track %.2d (%s) - Start %.2d:%.2d:%.2d, Length %.2d:%.2d:%.2d\n"),
687 i, (ti[i].type == DATA ? "DATA" : "AUDIO"),
688 ti[i].start[0], ti[i].start[1], ti[i].start[2],
689 ti[i].length[0], ti[i].length[1], ti[i].length[2]);
693 // This function is invoked by the front-end when opening an ISO
695 static long CALLBACK ISOopen(void) {
696 if (cdHandle != NULL) {
697 return 0; // it's already open
700 cdHandle = fopen(GetIsoFile(), "rb");
701 if (cdHandle == NULL) {
705 SysPrintf(_("Loaded CD Image: %s"), GetIsoFile());
707 cddaBigEndian = FALSE;
708 subChanMixed = FALSE;
711 if (parsecue(GetIsoFile()) == 0) {
714 else if (parsetoc(GetIsoFile()) == 0) {
717 else if (parseccd(GetIsoFile()) == 0) {
720 else if (parsemds(GetIsoFile()) == 0) {
724 if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
732 // make sure we have another handle open for cdda
733 if (numtracks > 1 && ti[1].handle == NULL) {
734 ti[1].handle = fopen(GetIsoFile(), "rb");
740 static long CALLBACK ISOclose(void) {
743 if (cdHandle != NULL) {
747 if (subHandle != NULL) {
754 for (i = 1; i <= numtracks; i++) {
755 if (ti[i].handle != NULL) {
756 fclose(ti[i].handle);
765 static long CALLBACK ISOinit(void) {
766 assert(cdHandle == NULL);
767 assert(subHandle == NULL);
769 return 0; // do nothing
772 static long CALLBACK ISOshutdown(void) {
777 // return Starting and Ending Track
779 // byte 0 - start track
780 // byte 1 - end track
781 static long CALLBACK ISOgetTN(unsigned char *buffer) {
785 buffer[1] = numtracks;
799 static long CALLBACK ISOgetTD(unsigned char track, unsigned char *buffer) {
800 if (numtracks > 0 && track <= numtracks) {
801 buffer[2] = ti[track].start[0];
802 buffer[1] = ti[track].start[1];
803 buffer[0] = ti[track].start[2];
814 // decode 'raw' subchannel data ripped by cdrdao
815 static void DecodeRawSubData(void) {
816 unsigned char subQData[12];
819 memset(subQData, 0, sizeof(subQData));
821 for (i = 0; i < 8 * 12; i++) {
822 if (subbuffer[i] & (1 << 6)) { // only subchannel Q is needed
823 subQData[i >> 3] |= (1 << (7 - (i & 7)));
827 memcpy(&subbuffer[12], subQData, 12);
831 // time: byte 0 - minute; byte 1 - second; byte 2 - frame
833 static long CALLBACK ISOreadTrack(unsigned char *time) {
834 if (cdHandle == NULL) {
839 fseek(cdHandle, MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2])) * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE) + 12, SEEK_SET);
840 fread(cdbuffer, 1, DATA_SIZE, cdHandle);
841 fread(subbuffer, 1, SUB_FRAMESIZE, cdHandle);
843 if (subChanRaw) DecodeRawSubData();
846 fseek(cdHandle, MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2])) * CD_FRAMESIZE_RAW + 12, SEEK_SET);
847 fread(cdbuffer, 1, DATA_SIZE, cdHandle);
849 if (subHandle != NULL) {
850 fseek(subHandle, MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2])) * SUB_FRAMESIZE, SEEK_SET);
851 fread(subbuffer, 1, SUB_FRAMESIZE, subHandle);
853 if (subChanRaw) DecodeRawSubData();
860 // return readed track
861 static unsigned char * CALLBACK ISOgetBuffer(void) {
866 // sector: byte 0 - minute; byte 1 - second; byte 2 - frame
867 // does NOT uses bcd format
868 static long CALLBACK ISOplay(unsigned char *time) {
869 unsigned int i, sect;
875 sect = msf2sec(time);
876 for (i = numtracks; i > 1; i--)
877 if (msf2sec(ti[i].start) <= sect + 2 * 75)
880 // find the file that contains this track
882 if (ti[i].handle != NULL)
885 cddaStartOffset = msf2sec(ti[i].start);
886 sect -= cddaStartOffset - 2 * 75;
887 cddaHandle = ti[i].handle;
889 if (SPU_playCDDAchannel != NULL) {
891 startCDDA(sect * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE));
894 startCDDA(sect * CD_FRAMESIZE_RAW);
901 static long CALLBACK ISOstop(void) {
906 // gets subchannel data
907 static unsigned char* CALLBACK ISOgetBufferSub(void) {
908 if (subHandle != NULL || subChanMixed) {
915 static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
918 CDR__getStatus(stat);
922 stat->Status |= 0x80;
923 sec = (cddaStartOffset + cddaCurOffset) / CD_FRAMESIZE_RAW;
924 sec2msf(sec, (char *)stat->Time);
933 void cdrIsoInit(void) {
935 CDR_shutdown = ISOshutdown;
937 CDR_close = ISOclose;
938 CDR_getTN = ISOgetTN;
939 CDR_getTD = ISOgetTD;
940 CDR_readTrack = ISOreadTrack;
941 CDR_getBuffer = ISOgetBuffer;
944 CDR_getBufferSub = ISOgetBufferSub;
945 CDR_getStatus = ISOgetStatus;
947 CDR_getDriveLetter = CDR__getDriveLetter;
948 CDR_configure = CDR__configure;
949 CDR_test = CDR__test;
950 CDR_about = CDR__about;
951 CDR_setfilename = CDR__setfilename;
956 int cdrIsoActive(void) {
957 return (cdHandle != NULL);