some debug code improvements
[pcsx_rearmed.git] / libpcsxcore / cdriso.c
CommitLineData
ef79bbde
P
1/***************************************************************************
2 * Copyright (C) 2007 PCSX-df Team *
3 * Copyright (C) 2009 Wei Mingzhi *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21#include "psxcommon.h"
22#include "plugins.h"
23#include "cdrom.h"
24#include "cdriso.h"
25
26#ifdef _WIN32
27#include <process.h>
28#include <windows.h>
29#else
30#include <pthread.h>
31#include <sys/time.h>
32#endif
33
34static FILE *cdHandle = NULL;
35static FILE *cddaHandle = NULL;
36static FILE *subHandle = NULL;
37
38static boolean subChanMixed = FALSE;
39static boolean subChanRaw = FALSE;
40
41static unsigned char cdbuffer[DATA_SIZE];
42static unsigned char subbuffer[SUB_FRAMESIZE];
43
44static unsigned char sndbuffer[CD_FRAMESIZE_RAW * 10];
45
46#define CDDA_FRAMETIME (1000 * (sizeof(sndbuffer) / CD_FRAMESIZE_RAW) / 75)
47
48#ifdef _WIN32
49static HANDLE threadid;
50#else
51static pthread_t threadid;
52#endif
53static unsigned int initial_offset = 0;
38b8102c 54static boolean playing = FALSE;
ef79bbde 55static boolean cddaBigEndian = FALSE;
38b8102c 56static unsigned int cddaCurOffset = 0;
57static unsigned int cddaStartOffset;
ef79bbde
P
58
59char* CALLBACK CDR__getDriveLetter(void);
60long CALLBACK CDR__configure(void);
61long CALLBACK CDR__test(void);
62void CALLBACK CDR__about(void);
63long CALLBACK CDR__setfilename(char *filename);
64long CALLBACK CDR__getStatus(struct CdrStat *stat);
65
66extern void *hCDRDriver;
67
68struct trackinfo {
69 enum {DATA, CDDA} type;
70 char start[3]; // MSF-format
71 char length[3]; // MSF-format
38b8102c 72 FILE *handle; // for multi-track images CDDA
ef79bbde
P
73};
74
75#define MAXTRACKS 100 /* How many tracks can a CD hold? */
76
77static int numtracks = 0;
78static struct trackinfo ti[MAXTRACKS];
79
80// get a sector from a msf-array
81static unsigned int msf2sec(char *msf) {
82 return ((msf[0] * 60 + msf[1]) * 75) + msf[2];
83}
84
85static void sec2msf(unsigned int s, char *msf) {
86 msf[0] = s / 75 / 60;
87 s = s - msf[0] * 75 * 60;
88 msf[1] = s / 75;
89 s = s - msf[1] * 75;
90 msf[2] = s;
91}
92
93// divide a string of xx:yy:zz into m, s, f
94static void tok2msf(char *time, char *msf) {
95 char *token;
96
97 token = strtok(time, ":");
98 if (token) {
99 msf[0] = atoi(token);
100 }
101 else {
102 msf[0] = 0;
103 }
104
105 token = strtok(NULL, ":");
106 if (token) {
107 msf[1] = atoi(token);
108 }
109 else {
110 msf[1] = 0;
111 }
112
113 token = strtok(NULL, ":");
114 if (token) {
115 msf[2] = atoi(token);
116 }
117 else {
118 msf[2] = 0;
119 }
120}
121
122#ifndef _WIN32
123static long GetTickCount(void) {
124 static time_t initial_time = 0;
125 struct timeval now;
126
127 gettimeofday(&now, NULL);
128
129 if (initial_time == 0) {
130 initial_time = now.tv_sec;
131 }
132
133 return (now.tv_sec - initial_time) * 1000L + now.tv_usec / 1000L;
134}
135#endif
136
137// this thread plays audio data
138#ifdef _WIN32
139static void playthread(void *param)
140#else
141static void *playthread(void *param)
142#endif
143{
144 long d, t, i, s;
145 unsigned char tmp;
146
147 t = GetTickCount();
148
149 while (playing) {
150 d = t - (long)GetTickCount();
151 if (d <= 0) {
152 d = 1;
153 }
154 else if (d > CDDA_FRAMETIME) {
155 d = CDDA_FRAMETIME;
156 }
157#ifdef _WIN32
158 Sleep(d);
159#else
160 usleep(d * 1000);
161#endif
d4a1e87d 162 // HACK: stop feeding data while emu is paused
163 extern int stop;
164 if (stop) {
165 usleep(100000);
166 continue;
167 }
ef79bbde
P
168
169 t = GetTickCount() + CDDA_FRAMETIME;
170
171 if (subChanMixed) {
172 s = 0;
173
174 for (i = 0; i < sizeof(sndbuffer) / CD_FRAMESIZE_RAW; i++) {
175 // read one sector
176 d = fread(sndbuffer + CD_FRAMESIZE_RAW * i, 1, CD_FRAMESIZE_RAW, cddaHandle);
177 if (d < CD_FRAMESIZE_RAW) {
178 break;
179 }
180
181 s += d;
182
183 // skip the subchannel data
184 fseek(cddaHandle, SUB_FRAMESIZE, SEEK_CUR);
185 }
186 }
187 else {
188 s = fread(sndbuffer, 1, sizeof(sndbuffer), cddaHandle);
189 }
190
191 if (s == 0) {
192 playing = FALSE;
ef79bbde
P
193 initial_offset = 0;
194 break;
195 }
196
197 if (!cdr.Muted && playing) {
198 if (cddaBigEndian) {
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;
203 }
204 }
205
206 SPU_playCDDAchannel((short *)sndbuffer, s);
207 }
208
209 cddaCurOffset += s;
210 }
211
212#ifdef _WIN32
213 _endthread();
214#else
215 pthread_exit(0);
216 return NULL;
217#endif
218}
219
220// stop the CDDA playback
221static void stopCDDA() {
222 if (!playing) {
223 return;
224 }
225
226 playing = FALSE;
227#ifdef _WIN32
228 WaitForSingleObject(threadid, INFINITE);
229#else
230 pthread_join(threadid, NULL);
231#endif
232
ef79bbde
P
233 initial_offset = 0;
234}
235
236// start the CDDA playback
237static void startCDDA(unsigned int offset) {
238 if (playing) {
239 if (initial_offset == offset) {
240 return;
241 }
242 stopCDDA();
243 }
244
ef79bbde
P
245 initial_offset = offset;
246 cddaCurOffset = initial_offset;
247 fseek(cddaHandle, initial_offset, SEEK_SET);
248
249 playing = TRUE;
250
251#ifdef _WIN32
252 threadid = (HANDLE)_beginthread(playthread, 0, NULL);
253#else
254 pthread_create(&threadid, NULL, playthread, NULL);
255#endif
256}
257
258// this function tries to get the .toc file of the given .bin
259// the necessary data is put into the ti (trackinformation)-array
260static int parsetoc(const char *isofile) {
261 char tocname[MAXPATHLEN];
262 FILE *fi;
263 char linebuf[256], dummy[256], name[256];
264 char *token;
265 char time[20], time2[20];
266 unsigned int t;
267
268 numtracks = 0;
269
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");
275 }
276 else {
277 return -1;
278 }
279
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);
286 t = strlen(tocname);
287 if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) {
288 tocname[t - 4] = '\0';
289 if ((fi = fopen(tocname, "r")) == NULL) {
290 return -1;
291 }
292 }
293 else {
294 return -1;
295 }
296 }
297 }
298
299 memset(&ti, 0, sizeof(ti));
300 cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio
301
302 // parse the .toc file
303 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
304 // search for tracks
305 strncpy(dummy, linebuf, sizeof(linebuf));
306 token = strtok(dummy, " ");
307
308 if (token == NULL) continue;
309
310 if (!strcmp(token, "TRACK")) {
311 // get type of track
312 token = strtok(NULL, " ");
313 numtracks++;
314
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
318
319 // check if this image contains mixed subchannel data
320 token = strtok(NULL, " ");
321 if (token != NULL && !strncmp(token, "RW_RAW", 6)) {
322 subChanMixed = TRUE;
323 subChanRaw = TRUE;
324 }
325 }
326 else if (!strncmp(token, "AUDIO", 5)) {
327 ti[numtracks].type = CDDA;
328 }
329 }
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);
334 t += 2 * 75;
335 sec2msf(t, (char *)&ti[numtracks].start);
336 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
337 }
338 else {
339 sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time);
340 tok2msf((char *)&time, (char *)&ti[numtracks].length);
341 }
342 }
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);
350 }
351 }
352
353 fclose(fi);
354
355 return 0;
356}
357
358// this function tries to get the .cue file of the given .bin
359// the necessary data is put into the ti (trackinformation)-array
360static int parsecue(const char *isofile) {
361 char cuename[MAXPATHLEN];
38b8102c 362 char filepath[MAXPATHLEN];
363 char *incue_fname;
ef79bbde
P
364 FILE *fi;
365 char *token;
366 char time[20];
367 char *tmp;
368 char linebuf[256], dummy[256];
38b8102c 369 unsigned int incue_max_len;
370 unsigned int t, i, total, file_len;
ef79bbde
P
371
372 numtracks = 0;
373
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");
379 }
380 else {
381 return -1;
382 }
383
384 if ((fi = fopen(cuename, "r")) == NULL) {
385 return -1;
386 }
387
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
390 // that...
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
394 // than a .cue file.
395 fclose(fi);
396 return parsetoc(isofile);
397 }
398 fseek(fi, 0, SEEK_SET);
399 }
400
38b8102c 401 // build a path for files referenced in .cue
402 strncpy(filepath, cuename, sizeof(filepath));
403 tmp = strrchr(filepath, '/') + 1;
404 if (tmp == NULL)
405 tmp = strrchr(filepath, '\\') + 1;
406 if (tmp == NULL)
407 tmp = filepath;
408 *tmp = 0;
409 filepath[sizeof(filepath) - 1] = 0;
410 incue_fname = tmp;
411 incue_max_len = sizeof(filepath) - (tmp - filepath) - 1;
412
ef79bbde
P
413 memset(&ti, 0, sizeof(ti));
414
415 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
416 strncpy(dummy, linebuf, sizeof(linebuf));
417 token = strtok(dummy, " ");
418
419 if (token == NULL) {
420 continue;
421 }
422
423 if (!strcmp(token, "TRACK")){
424 numtracks++;
425
426 if (strstr(linebuf, "AUDIO") != NULL) {
427 ti[numtracks].type = CDDA;
428 }
429 else if (strstr(linebuf, "MODE1/2352") != NULL || strstr(linebuf, "MODE2/2352") != NULL) {
430 ti[numtracks].type = DATA;
431 }
432 }
433 else if (!strcmp(token, "INDEX")) {
434 tmp = strstr(linebuf, "INDEX");
435 if (tmp != NULL) {
436 tmp += strlen("INDEX") + 3; // 3 - space + numeric index
437 while (*tmp == ' ') tmp++;
438 if (*tmp != '\n') sscanf(tmp, "%8s", time);
439 }
440
441 tok2msf((char *)&time, (char *)&ti[numtracks].start);
442
443 t = msf2sec(ti[numtracks].start) + 2 * 75;
444 sec2msf(t, ti[numtracks].start);
38b8102c 445 }
446 else if (!strcmp(token, "FILE")) {
447 tmp = strstr(linebuf, "FILE");
448 if (tmp == NULL) {
449 continue;
450 }
451 tmp += 4;
452 while (*tmp == ' ')
453 tmp++;
454 if (*tmp == '"') {
455 token = tmp + 1;
456 tmp = strchr(token, '"');
457 if (tmp != NULL)
458 *tmp = 0;
459 }
460 else {
461 token = tmp;
462 tmp = strchr(token, ' ');
463 if (tmp != NULL)
464 *tmp = 0;
465 }
ef79bbde 466
38b8102c 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);
471 }
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
475 fclose(cdHandle);
476 cdHandle = fopen(filepath, "rb");
ef79bbde
P
477 }
478 }
479 }
480
481 fclose(fi);
482
38b8102c 483 // make corrections for multi-track .cue, fill track lengths
484 total = 2 * 75;
485 file_len = 0;
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);
492 total += file_len;
493 }
494 else {
495 // this track uses the same file as the last,
496 // start of this track is last track's end
497 if (i > 1) {
498 t = msf2sec(ti[i].start) - msf2sec(ti[i - 1].start);
499 sec2msf(t, ti[i - 1].length);
500 }
501 t = file_len - msf2sec(ti[i].start) + 2 * 75;
502 sec2msf(t, ti[i].length);
503 }
ef79bbde
P
504 }
505
506 return 0;
507}
508
509// this function tries to get the .ccd file of the given .img
510// the necessary data is put into the ti (trackinformation)-array
511static int parseccd(const char *isofile) {
512 char ccdname[MAXPATHLEN];
513 FILE *fi;
514 char linebuf[256];
515 unsigned int t;
516
517 numtracks = 0;
518
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");
524 }
525 else {
526 return -1;
527 }
528
529 if ((fi = fopen(ccdname, "r")) == NULL) {
530 return -1;
531 }
532
533 memset(&ti, 0, sizeof(ti));
534
535 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
536 if (!strncmp(linebuf, "[TRACK", 6)){
537 numtracks++;
538 }
539 else if (!strncmp(linebuf, "MODE=", 5)) {
540 sscanf(linebuf, "MODE=%d", &t);
541 ti[numtracks].type = ((t == 0) ? CDDA : DATA);
542 }
543 else if (!strncmp(linebuf, "INDEX 1=", 8)) {
544 sscanf(linebuf, "INDEX 1=%d", &t);
545 sec2msf(t + 2 * 75, ti[numtracks].start);
546
547 // If we've already seen another track, this is its end
548 if (numtracks > 1) {
549 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
550 sec2msf(t, ti[numtracks - 1].length);
551 }
552 }
553 }
554
555 fclose(fi);
556
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);
562 }
563
564 return 0;
565}
566
567// this function tries to get the .mds file of the given .mdf
568// the necessary data is put into the ti (trackinformation)-array
569static int parsemds(const char *isofile) {
570 char mdsname[MAXPATHLEN];
571 FILE *fi;
572 unsigned int offset, extra_offset, l, i;
573 unsigned short s;
574
575 numtracks = 0;
576
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");
582 }
583 else {
584 return -1;
585 }
586
587 if ((fi = fopen(mdsname, "rb")) == NULL) {
588 return -1;
589 }
590
591 memset(&ti, 0, sizeof(ti));
592
593 // check if it's a valid mds file
594 fread(&i, 1, sizeof(unsigned int), fi);
595 i = SWAP32(i);
596 if (i != 0x4944454D) {
597 // not an valid mds file
598 fclose(fi);
599 return -1;
600 }
601
602 // get offset to session block
603 fseek(fi, 0x50, SEEK_SET);
604 fread(&offset, 1, sizeof(unsigned int), fi);
605 offset = SWAP32(offset);
606
607 // get total number of tracks
608 offset += 14;
609 fseek(fi, offset, SEEK_SET);
610 fread(&s, 1, sizeof(unsigned short), fi);
611 s = SWAP16(s);
612 numtracks = s;
613
614 // get offset to track blocks
615 fseek(fi, 4, SEEK_CUR);
616 fread(&offset, 1, sizeof(unsigned int), fi);
617 offset = SWAP32(offset);
618
619 // skip lead-in data
620 while (1) {
621 fseek(fi, offset + 4, SEEK_SET);
622 if (fgetc(fi) < 0xA0) {
623 break;
624 }
625 offset += 0x50;
626 }
627
628 // check if the image contains mixed subchannel data
629 fseek(fi, offset + 1, SEEK_SET);
630 subChanMixed = (fgetc(fi) ? TRUE : FALSE);
631
632 // read track data
633 for (i = 1; i <= numtracks; i++) {
634 fseek(fi, offset, SEEK_SET);
635
636 // get the track type
637 ti[i].type = ((fgetc(fi) == 0xA9) ? CDDA : DATA);
638 fseek(fi, 8, SEEK_CUR);
639
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);
644
645 if (i > 1) {
646 l = msf2sec(ti[i].start);
647 sec2msf(l - 2 * 75, ti[i].start); // ???
648 }
649
650 // get the track length
651 fread(&extra_offset, 1, sizeof(unsigned int), fi);
652 extra_offset = SWAP32(extra_offset);
653
654 fseek(fi, extra_offset + 4, SEEK_SET);
655 fread(&l, 1, sizeof(unsigned int), fi);
656 l = SWAP32(l);
657 sec2msf(l, ti[i].length);
658
659 offset += 0x50;
660 }
661
662 fclose(fi);
663 return 0;
664}
665
666// this function tries to get the .sub file of the given .img
667static int opensubfile(const char *isoname) {
668 char subname[MAXPATHLEN];
669
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");
675 }
676 else {
677 return -1;
678 }
679
680 subHandle = fopen(subname, "rb");
681 if (subHandle == NULL) {
682 return -1;
683 }
684
685 return 0;
686}
687
ef79bbde
P
688static void PrintTracks(void) {
689 int i;
690
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]);
696 }
697}
698
699// This function is invoked by the front-end when opening an ISO
700// file for playback
701static long CALLBACK ISOopen(void) {
702 if (cdHandle != NULL) {
703 return 0; // it's already open
704 }
705
706 cdHandle = fopen(GetIsoFile(), "rb");
707 if (cdHandle == NULL) {
708 return -1;
709 }
710
711 SysPrintf(_("Loaded CD Image: %s"), GetIsoFile());
712
713 cddaBigEndian = FALSE;
714 subChanMixed = FALSE;
715 subChanRaw = FALSE;
716
717 if (parsecue(GetIsoFile()) == 0) {
718 SysPrintf("[+cue]");
719 }
720 else if (parsetoc(GetIsoFile()) == 0) {
721 SysPrintf("[+toc]");
722 }
723 else if (parseccd(GetIsoFile()) == 0) {
724 SysPrintf("[+ccd]");
725 }
726 else if (parsemds(GetIsoFile()) == 0) {
727 SysPrintf("[+mds]");
728 }
729
730 if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
731 SysPrintf("[+sub]");
732 }
733
734 SysPrintf(".\n");
735
736 PrintTracks();
737
38b8102c 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");
741 }
742
ef79bbde
P
743 return 0;
744}
745
746static long CALLBACK ISOclose(void) {
38b8102c 747 int i;
748
ef79bbde
P
749 if (cdHandle != NULL) {
750 fclose(cdHandle);
751 cdHandle = NULL;
752 }
753 if (subHandle != NULL) {
754 fclose(subHandle);
755 subHandle = NULL;
756 }
757 stopCDDA();
38b8102c 758 cddaHandle = NULL;
759
760 for (i = 1; i <= numtracks; i++) {
761 if (ti[i].handle != NULL) {
762 fclose(ti[i].handle);
763 ti[i].handle = NULL;
764 }
765 }
766 numtracks = 0;
767
768 return 0;
769}
770
771static long CALLBACK ISOinit(void) {
772 assert(cdHandle == NULL);
773 assert(subHandle == NULL);
774
775 return 0; // do nothing
776}
777
778static long CALLBACK ISOshutdown(void) {
779 ISOclose();
ef79bbde
P
780 return 0;
781}
782
783// return Starting and Ending Track
784// buffer:
785// byte 0 - start track
786// byte 1 - end track
787static long CALLBACK ISOgetTN(unsigned char *buffer) {
788 buffer[0] = 1;
789
790 if (numtracks > 0) {
791 buffer[1] = numtracks;
792 }
793 else {
794 buffer[1] = 1;
795 }
796
797 return 0;
798}
799
800// return Track Time
801// buffer:
802// byte 0 - frame
803// byte 1 - second
804// byte 2 - minute
805static 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];
810 }
811 else {
812 buffer[2] = 0;
813 buffer[1] = 2;
814 buffer[0] = 0;
815 }
816
817 return 0;
818}
819
820// decode 'raw' subchannel data ripped by cdrdao
821static void DecodeRawSubData(void) {
822 unsigned char subQData[12];
823 int i;
824
825 memset(subQData, 0, sizeof(subQData));
826
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)));
830 }
831 }
832
833 memcpy(&subbuffer[12], subQData, 12);
834}
835
836// read track
837// time: byte 0 - minute; byte 1 - second; byte 2 - frame
838// uses bcd format
839static long CALLBACK ISOreadTrack(unsigned char *time) {
840 if (cdHandle == NULL) {
841 return -1;
842 }
843
844 if (subChanMixed) {
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);
848
849 if (subChanRaw) DecodeRawSubData();
850 }
851 else {
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);
854
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);
858
859 if (subChanRaw) DecodeRawSubData();
860 }
861 }
862
863 return 0;
864}
865
866// return readed track
867static unsigned char * CALLBACK ISOgetBuffer(void) {
868 return cdbuffer;
869}
870
871// plays cdda audio
872// sector: byte 0 - minute; byte 1 - second; byte 2 - frame
873// does NOT uses bcd format
874static long CALLBACK ISOplay(unsigned char *time) {
38b8102c 875 unsigned int i, sect;
876
7b8da7ab 877 if (numtracks <= 1)
878 return 0;
879
38b8102c 880 // find the track
881 sect = msf2sec(time);
882 for (i = numtracks; i > 1; i--)
883 if (msf2sec(ti[i].start) <= sect + 2 * 75)
884 break;
885
886 // find the file that contains this track
887 for (; i > 1; i--)
888 if (ti[i].handle != NULL)
889 break;
890
891 cddaStartOffset = msf2sec(ti[i].start);
892 sect -= cddaStartOffset - 2 * 75;
893 cddaHandle = ti[i].handle;
894
ef79bbde
P
895 if (SPU_playCDDAchannel != NULL) {
896 if (subChanMixed) {
38b8102c 897 startCDDA(sect * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE));
ef79bbde
P
898 }
899 else {
38b8102c 900 startCDDA(sect * CD_FRAMESIZE_RAW);
ef79bbde
P
901 }
902 }
903 return 0;
904}
905
906// stops cdda audio
907static long CALLBACK ISOstop(void) {
908 stopCDDA();
909 return 0;
910}
911
912// gets subchannel data
913static unsigned char* CALLBACK ISOgetBufferSub(void) {
914 if (subHandle != NULL || subChanMixed) {
915 return subbuffer;
916 }
917
918 return NULL;
919}
920
921static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
922 int sec;
923
924 CDR__getStatus(stat);
925
926 if (playing) {
927 stat->Type = 0x02;
928 stat->Status |= 0x80;
38b8102c 929 sec = (cddaStartOffset + cddaCurOffset) / CD_FRAMESIZE_RAW;
ef79bbde
P
930 sec2msf(sec, (char *)stat->Time);
931 }
932 else {
933 stat->Type = 0x01;
934 }
935
936 return 0;
937}
938
939void cdrIsoInit(void) {
940 CDR_init = ISOinit;
941 CDR_shutdown = ISOshutdown;
942 CDR_open = ISOopen;
943 CDR_close = ISOclose;
944 CDR_getTN = ISOgetTN;
945 CDR_getTD = ISOgetTD;
946 CDR_readTrack = ISOreadTrack;
947 CDR_getBuffer = ISOgetBuffer;
948 CDR_play = ISOplay;
949 CDR_stop = ISOstop;
950 CDR_getBufferSub = ISOgetBufferSub;
951 CDR_getStatus = ISOgetStatus;
952
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;
958
959 numtracks = 0;
960}
961
962int cdrIsoActive(void) {
963 return (cdHandle != NULL);
964}