gpulib: limit height
[pcsx_rearmed.git] / libpcsxcore / cdriso.c
CommitLineData
ef79bbde
P
1/***************************************************************************
2 * Copyright (C) 2007 PCSX-df Team *
3 * Copyright (C) 2009 Wei Mingzhi *
77160d47 4 * Copyright (C) 2012 notaz *
ef79bbde
P
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA. *
20 ***************************************************************************/
21
22#include "psxcommon.h"
23#include "plugins.h"
24#include "cdrom.h"
25#include "cdriso.h"
ae4e7dc9 26#include "ppf.h"
ef79bbde
P
27
28#ifdef _WIN32
29#include <process.h>
30#include <windows.h>
31#else
32#include <pthread.h>
33#include <sys/time.h>
349f7d81 34#include <unistd.h>
ef79bbde 35#endif
9a92bffb 36#include <zlib.h>
ef79bbde 37
0c2871a7 38unsigned int cdrIsoMultidiskCount;
39unsigned int cdrIsoMultidiskSelect;
40
ef79bbde
P
41static FILE *cdHandle = NULL;
42static FILE *cddaHandle = NULL;
43static FILE *subHandle = NULL;
44
45static boolean subChanMixed = FALSE;
46static boolean subChanRaw = FALSE;
8aa91443 47static boolean subChanMissing = FALSE;
ef79bbde 48
81abe4d5 49static unsigned char cdbuffer[CD_FRAMESIZE_RAW];
ef79bbde
P
50static unsigned char subbuffer[SUB_FRAMESIZE];
51
52static unsigned char sndbuffer[CD_FRAMESIZE_RAW * 10];
53
54#define CDDA_FRAMETIME (1000 * (sizeof(sndbuffer) / CD_FRAMESIZE_RAW) / 75)
55
56#ifdef _WIN32
57static HANDLE threadid;
58#else
59static pthread_t threadid;
60#endif
61static unsigned int initial_offset = 0;
38b8102c 62static boolean playing = FALSE;
ef79bbde 63static boolean cddaBigEndian = FALSE;
77160d47 64// cdda sectors in toc, byte offset in file
9a92bffb 65static unsigned int cdda_cur_sector;
2058d51f 66static unsigned int cdda_first_sector;
77160d47 67static unsigned int cdda_file_offset;
8aa91443 68/* Frame offset into CD image where pregap data would be found if it was there.
69 * If a game seeks there we must *not* return subchannel data since it's
70 * not in the CD image, so that cdrom code can fake subchannel data instead.
71 * XXX: there could be multiple pregaps but PSX dumps only have one? */
72static unsigned int pregapOffset;
ef79bbde 73
9a92bffb 74// compressed image stuff
75static struct {
76 unsigned char buff_raw[16][CD_FRAMESIZE_RAW];
77 unsigned char buff_compressed[CD_FRAMESIZE_RAW * 16 + 100];
78 unsigned int *index_table;
79 unsigned int index_len;
0877957a 80 unsigned int block_shift;
9a92bffb 81 unsigned int current_block;
82 unsigned int sector_in_blk;
83} *compr_img;
84
77160d47 85int (*cdimg_read_func)(FILE *f, unsigned int base, void *dest, int sector);
9a92bffb 86
ef79bbde
P
87char* CALLBACK CDR__getDriveLetter(void);
88long CALLBACK CDR__configure(void);
89long CALLBACK CDR__test(void);
90void CALLBACK CDR__about(void);
91long CALLBACK CDR__setfilename(char *filename);
92long CALLBACK CDR__getStatus(struct CdrStat *stat);
93
19c03d80 94static void DecodeRawSubData(void);
95
ef79bbde
P
96extern void *hCDRDriver;
97
98struct trackinfo {
99 enum {DATA, CDDA} type;
100 char start[3]; // MSF-format
101 char length[3]; // MSF-format
38b8102c 102 FILE *handle; // for multi-track images CDDA
77160d47 103 unsigned int start_offset; // byte offset from start of above file
ef79bbde
P
104};
105
106#define MAXTRACKS 100 /* How many tracks can a CD hold? */
107
108static int numtracks = 0;
109static struct trackinfo ti[MAXTRACKS];
110
111// get a sector from a msf-array
112static unsigned int msf2sec(char *msf) {
113 return ((msf[0] * 60 + msf[1]) * 75) + msf[2];
114}
115
116static void sec2msf(unsigned int s, char *msf) {
117 msf[0] = s / 75 / 60;
118 s = s - msf[0] * 75 * 60;
119 msf[1] = s / 75;
120 s = s - msf[1] * 75;
121 msf[2] = s;
122}
123
124// divide a string of xx:yy:zz into m, s, f
125static void tok2msf(char *time, char *msf) {
126 char *token;
127
128 token = strtok(time, ":");
129 if (token) {
130 msf[0] = atoi(token);
131 }
132 else {
133 msf[0] = 0;
134 }
135
136 token = strtok(NULL, ":");
137 if (token) {
138 msf[1] = atoi(token);
139 }
140 else {
141 msf[1] = 0;
142 }
143
144 token = strtok(NULL, ":");
145 if (token) {
146 msf[2] = atoi(token);
147 }
148 else {
149 msf[2] = 0;
150 }
151}
152
153#ifndef _WIN32
154static long GetTickCount(void) {
155 static time_t initial_time = 0;
156 struct timeval now;
157
158 gettimeofday(&now, NULL);
159
160 if (initial_time == 0) {
161 initial_time = now.tv_sec;
162 }
163
164 return (now.tv_sec - initial_time) * 1000L + now.tv_usec / 1000L;
165}
166#endif
167
168// this thread plays audio data
169#ifdef _WIN32
170static void playthread(void *param)
171#else
172static void *playthread(void *param)
173#endif
174{
983a7cfd 175 long osleep, d, t, i, s;
ef79bbde 176 unsigned char tmp;
77160d47 177 int ret = 0, sector_offs;
ef79bbde
P
178
179 t = GetTickCount();
180
181 while (playing) {
9a92bffb 182 s = 0;
183 for (i = 0; i < sizeof(sndbuffer) / CD_FRAMESIZE_RAW; i++) {
77160d47 184 sector_offs = cdda_cur_sector - cdda_first_sector;
185 if (sector_offs < 0) {
186 d = CD_FRAMESIZE_RAW;
187 memset(sndbuffer + s, 0, d);
188 }
189 else {
190 d = cdimg_read_func(cddaHandle, cdda_file_offset,
191 sndbuffer + s, sector_offs);
192 if (d < CD_FRAMESIZE_RAW)
193 break;
194 }
2058d51f 195
9a92bffb 196 s += d;
197 cdda_cur_sector++;
ef79bbde 198 }
19c03d80 199
ef79bbde
P
200 if (s == 0) {
201 playing = FALSE;
ef79bbde
P
202 initial_offset = 0;
203 break;
204 }
205
206 if (!cdr.Muted && playing) {
207 if (cddaBigEndian) {
208 for (i = 0; i < s / 2; i++) {
209 tmp = sndbuffer[i * 2];
210 sndbuffer[i * 2] = sndbuffer[i * 2 + 1];
211 sndbuffer[i * 2 + 1] = tmp;
212 }
213 }
214
53598a71 215 // can't do it yet due to readahead..
216 //cdrAttenuate((short *)sndbuffer, s / 4, 1);
983a7cfd 217 do {
218 ret = SPU_playCDDAchannel((short *)sndbuffer, s);
219 if (ret == 0x7761)
220 usleep(6 * 1000);
221 } while (ret == 0x7761 && playing); // rearmed_wait
ef79bbde 222 }
983a7cfd 223
224 if (ret != 0x676f) { // !rearmed_go
225 // do approx sleep
226 long now;
227
228 // HACK: stop feeding data while emu is paused
229 extern int stop;
230 while (stop && playing)
231 usleep(10000);
232
233 now = GetTickCount();
234 osleep = t - now;
235 if (osleep <= 0) {
236 osleep = 1;
237 t = now;
238 }
239 else if (osleep > CDDA_FRAMETIME) {
240 osleep = CDDA_FRAMETIME;
241 t = now;
242 }
243
244 usleep(osleep * 1000);
245 t += CDDA_FRAMETIME;
246 }
247
ef79bbde
P
248 }
249
250#ifdef _WIN32
251 _endthread();
252#else
253 pthread_exit(0);
254 return NULL;
255#endif
256}
257
258// stop the CDDA playback
259static void stopCDDA() {
260 if (!playing) {
261 return;
262 }
263
264 playing = FALSE;
265#ifdef _WIN32
266 WaitForSingleObject(threadid, INFINITE);
267#else
268 pthread_join(threadid, NULL);
269#endif
ef79bbde
P
270}
271
272// start the CDDA playback
77160d47 273static void startCDDA(void) {
ef79bbde 274 if (playing) {
ef79bbde
P
275 stopCDDA();
276 }
277
ef79bbde
P
278 playing = TRUE;
279
280#ifdef _WIN32
281 threadid = (HANDLE)_beginthread(playthread, 0, NULL);
282#else
283 pthread_create(&threadid, NULL, playthread, NULL);
284#endif
285}
286
287// this function tries to get the .toc file of the given .bin
288// the necessary data is put into the ti (trackinformation)-array
289static int parsetoc(const char *isofile) {
290 char tocname[MAXPATHLEN];
291 FILE *fi;
292 char linebuf[256], dummy[256], name[256];
293 char *token;
294 char time[20], time2[20];
19c03d80 295 unsigned int t, sector_offs;
ef79bbde
P
296
297 numtracks = 0;
298
299 // copy name of the iso and change extension from .bin to .toc
300 strncpy(tocname, isofile, sizeof(tocname));
301 tocname[MAXPATHLEN - 1] = '\0';
302 if (strlen(tocname) >= 4) {
303 strcpy(tocname + strlen(tocname) - 4, ".toc");
304 }
305 else {
306 return -1;
307 }
308
309 if ((fi = fopen(tocname, "r")) == NULL) {
310 // try changing extension to .cue (to satisfy some stupid tutorials)
311 strcpy(tocname + strlen(tocname) - 4, ".cue");
312 if ((fi = fopen(tocname, "r")) == NULL) {
313 // if filename is image.toc.bin, try removing .bin (for Brasero)
314 strcpy(tocname, isofile);
315 t = strlen(tocname);
316 if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) {
317 tocname[t - 4] = '\0';
318 if ((fi = fopen(tocname, "r")) == NULL) {
319 return -1;
320 }
321 }
322 else {
323 return -1;
324 }
325 }
326 }
327
328 memset(&ti, 0, sizeof(ti));
329 cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio
330
19c03d80 331 sector_offs = 2 * 75;
332
ef79bbde
P
333 // parse the .toc file
334 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
335 // search for tracks
336 strncpy(dummy, linebuf, sizeof(linebuf));
337 token = strtok(dummy, " ");
338
339 if (token == NULL) continue;
340
341 if (!strcmp(token, "TRACK")) {
342 // get type of track
343 token = strtok(NULL, " ");
344 numtracks++;
345
346 if (!strncmp(token, "MODE2_RAW", 9)) {
347 ti[numtracks].type = DATA;
348 sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0
349
350 // check if this image contains mixed subchannel data
351 token = strtok(NULL, " ");
352 if (token != NULL && !strncmp(token, "RW_RAW", 6)) {
353 subChanMixed = TRUE;
354 subChanRaw = TRUE;
355 }
356 }
357 else if (!strncmp(token, "AUDIO", 5)) {
358 ti[numtracks].type = CDDA;
359 }
360 }
361 else if (!strcmp(token, "DATAFILE")) {
362 if (ti[numtracks].type == CDDA) {
363 sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2);
19c03d80 364 ti[numtracks].start_offset = t;
77160d47 365 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
19c03d80 366 t += sector_offs;
ef79bbde
P
367 sec2msf(t, (char *)&ti[numtracks].start);
368 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
369 }
370 else {
371 sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time);
372 tok2msf((char *)&time, (char *)&ti[numtracks].length);
373 }
374 }
375 else if (!strcmp(token, "FILE")) {
376 sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2);
377 tok2msf((char *)&time, (char *)&ti[numtracks].start);
19c03d80 378 ti[numtracks].start_offset = t;
77160d47 379 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
19c03d80 380 t += msf2sec(ti[numtracks].start) + sector_offs;
ef79bbde
P
381 sec2msf(t, (char *)&ti[numtracks].start);
382 tok2msf((char *)&time2, (char *)&ti[numtracks].length);
383 }
19c03d80 384 else if (!strcmp(token, "ZERO")) {
385 sscanf(linebuf, "ZERO AUDIO RW_RAW %8s", time);
386 tok2msf((char *)&time, dummy);
387 sector_offs += msf2sec(dummy);
8aa91443 388 if (numtracks > 1) {
389 t = ti[numtracks - 1].start_offset;
77160d47 390 t /= CD_FRAMESIZE_RAW + (subChanMixed ? SUB_FRAMESIZE : 0);
8aa91443 391 pregapOffset = t + msf2sec(ti[numtracks - 1].length);
392 }
19c03d80 393 }
ef79bbde
P
394 }
395
396 fclose(fi);
397
398 return 0;
399}
400
401// this function tries to get the .cue file of the given .bin
402// the necessary data is put into the ti (trackinformation)-array
403static int parsecue(const char *isofile) {
404 char cuename[MAXPATHLEN];
38b8102c 405 char filepath[MAXPATHLEN];
406 char *incue_fname;
ef79bbde
P
407 FILE *fi;
408 char *token;
409 char time[20];
410 char *tmp;
19c03d80 411 char linebuf[256], tmpb[256], dummy[256];
38b8102c 412 unsigned int incue_max_len;
77160d47 413 unsigned int t, file_len, mode, sector_offs;
414 unsigned int sector_size = 2352;
ef79bbde
P
415
416 numtracks = 0;
417
418 // copy name of the iso and change extension from .bin to .cue
419 strncpy(cuename, isofile, sizeof(cuename));
420 cuename[MAXPATHLEN - 1] = '\0';
421 if (strlen(cuename) >= 4) {
422 strcpy(cuename + strlen(cuename) - 4, ".cue");
423 }
424 else {
425 return -1;
426 }
427
428 if ((fi = fopen(cuename, "r")) == NULL) {
429 return -1;
430 }
431
432 // Some stupid tutorials wrongly tell users to use cdrdao to rip a
433 // "bin/cue" image, which is in fact a "bin/toc" image. So let's check
434 // that...
435 if (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
436 if (!strncmp(linebuf, "CD_ROM_XA", 9)) {
437 // Don't proceed further, as this is actually a .toc file rather
438 // than a .cue file.
439 fclose(fi);
440 return parsetoc(isofile);
441 }
442 fseek(fi, 0, SEEK_SET);
443 }
444
38b8102c 445 // build a path for files referenced in .cue
446 strncpy(filepath, cuename, sizeof(filepath));
447 tmp = strrchr(filepath, '/') + 1;
448 if (tmp == NULL)
449 tmp = strrchr(filepath, '\\') + 1;
450 if (tmp == NULL)
451 tmp = filepath;
452 *tmp = 0;
453 filepath[sizeof(filepath) - 1] = 0;
454 incue_fname = tmp;
455 incue_max_len = sizeof(filepath) - (tmp - filepath) - 1;
456
ef79bbde
P
457 memset(&ti, 0, sizeof(ti));
458
19c03d80 459 file_len = 0;
460 sector_offs = 2 * 75;
461
ef79bbde
P
462 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
463 strncpy(dummy, linebuf, sizeof(linebuf));
464 token = strtok(dummy, " ");
465
466 if (token == NULL) {
467 continue;
468 }
469
19c03d80 470 if (!strcmp(token, "TRACK")) {
ef79bbde
P
471 numtracks++;
472
77160d47 473 sector_size = 0;
ef79bbde
P
474 if (strstr(linebuf, "AUDIO") != NULL) {
475 ti[numtracks].type = CDDA;
77160d47 476 sector_size = 2352;
ef79bbde 477 }
77160d47 478 else if (sscanf(linebuf, " TRACK %u MODE%u/%u", &t, &mode, &sector_size) == 3)
ef79bbde 479 ti[numtracks].type = DATA;
77160d47 480 else {
481 SysPrintf(".cue: failed to parse TRACK\n");
482 ti[numtracks].type = numtracks == 1 ? DATA : CDDA;
ef79bbde 483 }
77160d47 484 if (sector_size == 0)
485 sector_size = 2352;
ef79bbde
P
486 }
487 else if (!strcmp(token, "INDEX")) {
77160d47 488 if (sscanf(linebuf, " INDEX %02d %8s", &t, time) != 2)
489 SysPrintf(".cue: failed to parse INDEX\n");
19c03d80 490 tok2msf(time, (char *)&ti[numtracks].start);
ef79bbde 491
19c03d80 492 t = msf2sec(ti[numtracks].start);
77160d47 493 ti[numtracks].start_offset = t * sector_size;
19c03d80 494 t += sector_offs;
ef79bbde 495 sec2msf(t, ti[numtracks].start);
19c03d80 496
497 // default track length to file length
77160d47 498 t = file_len - ti[numtracks].start_offset / sector_size;
19c03d80 499 sec2msf(t, ti[numtracks].length);
500
501 if (numtracks > 1 && ti[numtracks].handle == NULL) {
502 // this track uses the same file as the last,
503 // start of this track is last track's end
504 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
505 sec2msf(t, ti[numtracks - 1].length);
38b8102c 506 }
8aa91443 507 if (numtracks > 1 && pregapOffset == -1)
77160d47 508 pregapOffset = ti[numtracks].start_offset / sector_size;
19c03d80 509 }
510 else if (!strcmp(token, "PREGAP")) {
511 if (sscanf(linebuf, " PREGAP %8s", time) == 1) {
512 tok2msf(time, dummy);
513 sector_offs += msf2sec(dummy);
38b8102c 514 }
8aa91443 515 pregapOffset = -1; // mark to fill track start_offset
19c03d80 516 }
517 else if (!strcmp(token, "FILE")) {
385c023b 518 t = sscanf(linebuf, " FILE \"%256[^\"]\"", tmpb);
519 if (t != 1)
520 sscanf(linebuf, " FILE %256s", tmpb);
19c03d80 521
522 // absolute path?
523 ti[numtracks + 1].handle = fopen(tmpb, "rb");
524 if (ti[numtracks + 1].handle == NULL) {
525 // relative to .cue?
526 tmp = strrchr(tmpb, '\\');
527 if (tmp == NULL)
528 tmp = strrchr(tmpb, '/');
38b8102c 529 if (tmp != NULL)
19c03d80 530 tmp++;
531 else
532 tmp = tmpb;
533 strncpy(incue_fname, tmp, incue_max_len);
534 ti[numtracks + 1].handle = fopen(filepath, "rb");
38b8102c 535 }
ef79bbde 536
19c03d80 537 // update global offset if this is not first file in this .cue
538 if (numtracks + 1 > 1)
539 sector_offs += file_len;
540
541 file_len = 0;
38b8102c 542 if (ti[numtracks + 1].handle == NULL) {
19c03d80 543 SysPrintf(_("\ncould not open: %s\n"), filepath);
544 continue;
38b8102c 545 }
19c03d80 546 fseek(ti[numtracks + 1].handle, 0, SEEK_END);
547 file_len = ftell(ti[numtracks + 1].handle) / 2352;
548
549 if (numtracks == 0 && strlen(isofile) >= 4 &&
550 strcmp(isofile + strlen(isofile) - 4, ".cue") == 0)
551 {
38b8102c 552 // user selected .cue as image file, use it's data track instead
553 fclose(cdHandle);
554 cdHandle = fopen(filepath, "rb");
ef79bbde
P
555 }
556 }
557 }
558
559 fclose(fi);
560
ef79bbde
P
561 return 0;
562}
563
564// this function tries to get the .ccd file of the given .img
565// the necessary data is put into the ti (trackinformation)-array
566static int parseccd(const char *isofile) {
567 char ccdname[MAXPATHLEN];
568 FILE *fi;
569 char linebuf[256];
570 unsigned int t;
571
572 numtracks = 0;
573
574 // copy name of the iso and change extension from .img to .ccd
575 strncpy(ccdname, isofile, sizeof(ccdname));
576 ccdname[MAXPATHLEN - 1] = '\0';
577 if (strlen(ccdname) >= 4) {
578 strcpy(ccdname + strlen(ccdname) - 4, ".ccd");
579 }
580 else {
581 return -1;
582 }
583
584 if ((fi = fopen(ccdname, "r")) == NULL) {
585 return -1;
586 }
587
588 memset(&ti, 0, sizeof(ti));
589
590 while (fgets(linebuf, sizeof(linebuf), fi) != NULL) {
591 if (!strncmp(linebuf, "[TRACK", 6)){
592 numtracks++;
593 }
594 else if (!strncmp(linebuf, "MODE=", 5)) {
595 sscanf(linebuf, "MODE=%d", &t);
596 ti[numtracks].type = ((t == 0) ? CDDA : DATA);
597 }
598 else if (!strncmp(linebuf, "INDEX 1=", 8)) {
599 sscanf(linebuf, "INDEX 1=%d", &t);
600 sec2msf(t + 2 * 75, ti[numtracks].start);
77160d47 601 ti[numtracks].start_offset = t * 2352;
ef79bbde
P
602
603 // If we've already seen another track, this is its end
604 if (numtracks > 1) {
605 t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start);
606 sec2msf(t, ti[numtracks - 1].length);
607 }
608 }
609 }
610
611 fclose(fi);
612
613 // Fill out the last track's end based on size
614 if (numtracks >= 1) {
615 fseek(cdHandle, 0, SEEK_END);
616 t = ftell(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75;
617 sec2msf(t, ti[numtracks].length);
618 }
619
620 return 0;
621}
622
623// this function tries to get the .mds file of the given .mdf
624// the necessary data is put into the ti (trackinformation)-array
625static int parsemds(const char *isofile) {
626 char mdsname[MAXPATHLEN];
627 FILE *fi;
628 unsigned int offset, extra_offset, l, i;
629 unsigned short s;
630
631 numtracks = 0;
632
633 // copy name of the iso and change extension from .mdf to .mds
634 strncpy(mdsname, isofile, sizeof(mdsname));
635 mdsname[MAXPATHLEN - 1] = '\0';
636 if (strlen(mdsname) >= 4) {
637 strcpy(mdsname + strlen(mdsname) - 4, ".mds");
638 }
639 else {
640 return -1;
641 }
642
643 if ((fi = fopen(mdsname, "rb")) == NULL) {
644 return -1;
645 }
646
647 memset(&ti, 0, sizeof(ti));
648
649 // check if it's a valid mds file
650 fread(&i, 1, sizeof(unsigned int), fi);
651 i = SWAP32(i);
652 if (i != 0x4944454D) {
653 // not an valid mds file
654 fclose(fi);
655 return -1;
656 }
657
658 // get offset to session block
659 fseek(fi, 0x50, SEEK_SET);
660 fread(&offset, 1, sizeof(unsigned int), fi);
661 offset = SWAP32(offset);
662
663 // get total number of tracks
664 offset += 14;
665 fseek(fi, offset, SEEK_SET);
666 fread(&s, 1, sizeof(unsigned short), fi);
667 s = SWAP16(s);
668 numtracks = s;
669
670 // get offset to track blocks
671 fseek(fi, 4, SEEK_CUR);
672 fread(&offset, 1, sizeof(unsigned int), fi);
673 offset = SWAP32(offset);
674
675 // skip lead-in data
676 while (1) {
677 fseek(fi, offset + 4, SEEK_SET);
678 if (fgetc(fi) < 0xA0) {
679 break;
680 }
681 offset += 0x50;
682 }
683
684 // check if the image contains mixed subchannel data
685 fseek(fi, offset + 1, SEEK_SET);
77160d47 686 subChanMixed = subChanRaw = (fgetc(fi) ? TRUE : FALSE);
ef79bbde
P
687
688 // read track data
689 for (i = 1; i <= numtracks; i++) {
690 fseek(fi, offset, SEEK_SET);
691
692 // get the track type
693 ti[i].type = ((fgetc(fi) == 0xA9) ? CDDA : DATA);
694 fseek(fi, 8, SEEK_CUR);
695
696 // get the track starting point
697 ti[i].start[0] = fgetc(fi);
698 ti[i].start[1] = fgetc(fi);
699 ti[i].start[2] = fgetc(fi);
700
ef79bbde
P
701 fread(&extra_offset, 1, sizeof(unsigned int), fi);
702 extra_offset = SWAP32(extra_offset);
703
19c03d80 704 // get track start offset (in .mdf)
705 fseek(fi, offset + 0x28, SEEK_SET);
706 fread(&l, 1, sizeof(unsigned int), fi);
707 l = SWAP32(l);
77160d47 708 ti[i].start_offset = l;
19c03d80 709
8aa91443 710 // get pregap
711 fseek(fi, extra_offset, SEEK_SET);
712 fread(&l, 1, sizeof(unsigned int), fi);
713 l = SWAP32(l);
714 if (l != 0 && i > 1)
77160d47 715 pregapOffset = msf2sec(ti[i].start);
8aa91443 716
717 // get the track length
718 fread(&l, 1, sizeof(unsigned int), fi);
719 l = SWAP32(l);
720 sec2msf(l, ti[i].length);
721
ef79bbde
P
722 offset += 0x50;
723 }
724
725 fclose(fi);
726 return 0;
727}
728
0877957a 729static int handlepbp(const char *isofile) {
9a92bffb 730 struct {
731 unsigned int sig;
732 unsigned int dontcare[8];
733 unsigned int psar_offs;
734 } pbp_hdr;
735 struct {
736 unsigned char type;
737 unsigned char pad0;
738 unsigned char track;
739 char index0[3];
740 char pad1;
741 char index1[3];
742 } toc_entry;
743 struct {
744 unsigned int offset;
745 unsigned int size;
746 unsigned int dontcare[6];
747 } index_entry;
0c2871a7 748 char psar_sig[11];
9a92bffb 749 unsigned int t, cd_length, cdimg_base;
0c2871a7 750 unsigned int offsettab[8], psisoimg_offs;
9a92bffb 751 const char *ext = NULL;
752 int i, ret;
753
754 if (strlen(isofile) >= 4)
755 ext = isofile + strlen(isofile) - 4;
756 if (ext == NULL || (strcmp(ext, ".pbp") != 0 && strcmp(ext, ".PBP") != 0))
757 return -1;
758
759 numtracks = 0;
760
761 ret = fread(&pbp_hdr, 1, sizeof(pbp_hdr), cdHandle);
762 if (ret != sizeof(pbp_hdr)) {
f29fbd53 763 SysPrintf("failed to read pbp\n");
9a92bffb 764 goto fail_io;
765 }
766
767 ret = fseek(cdHandle, pbp_hdr.psar_offs, SEEK_SET);
768 if (ret != 0) {
f29fbd53 769 SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs);
9a92bffb 770 goto fail_io;
771 }
772
0c2871a7 773 psisoimg_offs = pbp_hdr.psar_offs;
9a92bffb 774 fread(psar_sig, 1, sizeof(psar_sig), cdHandle);
0c2871a7 775 psar_sig[10] = 0;
776 if (strcmp(psar_sig, "PSTITLEIMG") == 0) {
777 // multidisk image?
778 ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x200, SEEK_SET);
779 if (ret != 0) {
f29fbd53 780 SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs + 0x200);
0c2871a7 781 goto fail_io;
782 }
783
784 if (fread(&offsettab, 1, sizeof(offsettab), cdHandle) != sizeof(offsettab)) {
f29fbd53 785 SysPrintf("failed to read offsettab\n");
0c2871a7 786 goto fail_io;
787 }
788
789 for (i = 0; i < sizeof(offsettab) / sizeof(offsettab[0]); i++) {
790 if (offsettab[i] == 0)
791 break;
792 }
793 cdrIsoMultidiskCount = i;
794 if (cdrIsoMultidiskCount == 0) {
f29fbd53 795 SysPrintf("multidisk eboot has 0 images?\n");
0c2871a7 796 goto fail_io;
797 }
798
799 if (cdrIsoMultidiskSelect >= cdrIsoMultidiskCount)
800 cdrIsoMultidiskSelect = 0;
801
802 psisoimg_offs += offsettab[cdrIsoMultidiskSelect];
803
804 ret = fseek(cdHandle, psisoimg_offs, SEEK_SET);
805 if (ret != 0) {
f29fbd53 806 SysPrintf("failed to seek to %x\n", psisoimg_offs);
0c2871a7 807 goto fail_io;
808 }
809
810 fread(psar_sig, 1, sizeof(psar_sig), cdHandle);
811 psar_sig[10] = 0;
812 }
813
814 if (strcmp(psar_sig, "PSISOIMG00") != 0) {
f29fbd53 815 SysPrintf("bad psar_sig: %s\n", psar_sig);
9a92bffb 816 goto fail_io;
817 }
818
819 // seek to TOC
0c2871a7 820 ret = fseek(cdHandle, psisoimg_offs + 0x800, SEEK_SET);
9a92bffb 821 if (ret != 0) {
f29fbd53 822 SysPrintf("failed to seek to %x\n", psisoimg_offs + 0x800);
9a92bffb 823 goto fail_io;
824 }
825
826 // first 3 entries are special
827 fseek(cdHandle, sizeof(toc_entry), SEEK_CUR);
828 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
829 numtracks = btoi(toc_entry.index1[0]);
830
831 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
832 cd_length = btoi(toc_entry.index1[0]) * 60 * 75 +
833 btoi(toc_entry.index1[1]) * 75 + btoi(toc_entry.index1[2]);
834
835 for (i = 1; i <= numtracks; i++) {
836 fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
837
838 ti[i].type = (toc_entry.type == 1) ? CDDA : DATA;
839
840 ti[i].start_offset = btoi(toc_entry.index0[0]) * 60 * 75 +
841 btoi(toc_entry.index0[1]) * 75 + btoi(toc_entry.index0[2]);
77160d47 842 ti[i].start_offset *= 2352;
9a92bffb 843 ti[i].start[0] = btoi(toc_entry.index1[0]);
844 ti[i].start[1] = btoi(toc_entry.index1[1]);
845 ti[i].start[2] = btoi(toc_entry.index1[2]);
846
847 if (i > 1) {
848 t = msf2sec(ti[i].start) - msf2sec(ti[i - 1].start);
849 sec2msf(t, ti[i - 1].length);
850 }
851 }
77160d47 852 t = cd_length - ti[numtracks].start_offset / 2352;
9a92bffb 853 sec2msf(t, ti[numtracks].length);
854
855 // seek to ISO index
0c2871a7 856 ret = fseek(cdHandle, psisoimg_offs + 0x4000, SEEK_SET);
9a92bffb 857 if (ret != 0) {
f29fbd53 858 SysPrintf("failed to seek to ISO index\n");
9a92bffb 859 goto fail_io;
860 }
861
862 compr_img = calloc(1, sizeof(*compr_img));
863 if (compr_img == NULL)
864 goto fail_io;
865
0877957a 866 compr_img->block_shift = 4;
9a92bffb 867 compr_img->current_block = (unsigned int)-1;
868
869 compr_img->index_len = (0x100000 - 0x4000) / sizeof(index_entry);
870 compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
871 if (compr_img->index_table == NULL)
872 goto fail_io;
873
0c2871a7 874 cdimg_base = psisoimg_offs + 0x100000;
9a92bffb 875 for (i = 0; i < compr_img->index_len; i++) {
876 ret = fread(&index_entry, 1, sizeof(index_entry), cdHandle);
877 if (ret != sizeof(index_entry)) {
f29fbd53 878 SysPrintf("failed to read index_entry #%d\n", i);
9a92bffb 879 goto fail_index;
880 }
881
882 if (index_entry.size == 0)
883 break;
884
885 compr_img->index_table[i] = cdimg_base + index_entry.offset;
886 }
887 compr_img->index_table[i] = cdimg_base + index_entry.offset + index_entry.size;
888
889 return 0;
890
891fail_index:
892 free(compr_img->index_table);
893 compr_img->index_table = NULL;
894fail_io:
0877957a 895 if (compr_img != NULL) {
896 free(compr_img);
897 compr_img = NULL;
898 }
899 return -1;
900}
901
902static int handlecbin(const char *isofile) {
903 struct
904 {
905 char magic[4];
906 unsigned int header_size;
907 unsigned long long total_bytes;
908 unsigned int block_size;
909 unsigned char ver; // 1
910 unsigned char align;
911 unsigned char rsv_06[2];
912 } ciso_hdr;
913 const char *ext = NULL;
0c2871a7 914 unsigned int index = 0, plain;
0877957a 915 int i, ret;
916
917 if (strlen(isofile) >= 5)
918 ext = isofile + strlen(isofile) - 5;
919 if (ext == NULL || (strcasecmp(ext + 1, ".cbn") != 0 && strcasecmp(ext, ".cbin") != 0))
920 return -1;
921
922 ret = fread(&ciso_hdr, 1, sizeof(ciso_hdr), cdHandle);
923 if (ret != sizeof(ciso_hdr)) {
f29fbd53 924 SysPrintf("failed to read ciso header\n");
0877957a 925 return -1;
926 }
927
928 if (strncmp(ciso_hdr.magic, "CISO", 4) != 0 || ciso_hdr.total_bytes <= 0 || ciso_hdr.block_size <= 0) {
f29fbd53 929 SysPrintf("bad ciso header\n");
0877957a 930 return -1;
931 }
932 if (ciso_hdr.header_size != 0 && ciso_hdr.header_size != sizeof(ciso_hdr)) {
933 ret = fseek(cdHandle, ciso_hdr.header_size, SEEK_SET);
934 if (ret != 0) {
f29fbd53 935 SysPrintf("failed to seek to %x\n", ciso_hdr.header_size);
0877957a 936 return -1;
937 }
938 }
939
940 compr_img = calloc(1, sizeof(*compr_img));
941 if (compr_img == NULL)
942 goto fail_io;
943
944 compr_img->block_shift = 0;
945 compr_img->current_block = (unsigned int)-1;
946
947 compr_img->index_len = ciso_hdr.total_bytes / ciso_hdr.block_size;
948 compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
949 if (compr_img->index_table == NULL)
950 goto fail_io;
951
952 ret = fread(compr_img->index_table, sizeof(compr_img->index_table[0]), compr_img->index_len, cdHandle);
953 if (ret != compr_img->index_len) {
f29fbd53 954 SysPrintf("failed to read index table\n");
0877957a 955 goto fail_index;
956 }
957
958 for (i = 0; i < compr_img->index_len + 1; i++) {
959 index = compr_img->index_table[i];
960 plain = index & 0x80000000;
961 index &= 0x7fffffff;
962 compr_img->index_table[i] = (index << ciso_hdr.align) | plain;
963 }
964 if ((long long)index << ciso_hdr.align >= 0x80000000ll)
f29fbd53 965 SysPrintf("warning: ciso img too large, expect problems\n");
0877957a 966
967 return 0;
968
969fail_index:
970 free(compr_img->index_table);
971 compr_img->index_table = NULL;
972fail_io:
973 if (compr_img != NULL) {
974 free(compr_img);
975 compr_img = NULL;
976 }
9a92bffb 977 return -1;
978}
979
ef79bbde
P
980// this function tries to get the .sub file of the given .img
981static int opensubfile(const char *isoname) {
982 char subname[MAXPATHLEN];
983
984 // copy name of the iso and change extension from .img to .sub
985 strncpy(subname, isoname, sizeof(subname));
986 subname[MAXPATHLEN - 1] = '\0';
987 if (strlen(subname) >= 4) {
988 strcpy(subname + strlen(subname) - 4, ".sub");
989 }
990 else {
991 return -1;
992 }
993
994 subHandle = fopen(subname, "rb");
995 if (subHandle == NULL) {
996 return -1;
997 }
998
999 return 0;
1000}
1001
ae4e7dc9 1002static int opensbifile(const char *isoname) {
1003 char sbiname[MAXPATHLEN];
1004 int s;
1005
1006 strncpy(sbiname, isoname, sizeof(sbiname));
1007 sbiname[MAXPATHLEN - 1] = '\0';
1008 if (strlen(sbiname) >= 4) {
1009 strcpy(sbiname + strlen(sbiname) - 4, ".sbi");
1010 }
1011 else {
1012 return -1;
1013 }
1014
1015 fseek(cdHandle, 0, SEEK_END);
1016 s = ftell(cdHandle) / 2352;
1017
1018 return LoadSBI(sbiname, s);
1019}
1020
77160d47 1021static int cdread_normal(FILE *f, unsigned int base, void *dest, int sector)
9a92bffb 1022{
77160d47 1023 fseek(f, base + sector * CD_FRAMESIZE_RAW, SEEK_SET);
81abe4d5 1024 return fread(dest, 1, CD_FRAMESIZE_RAW, f);
9a92bffb 1025}
1026
77160d47 1027static int cdread_sub_mixed(FILE *f, unsigned int base, void *dest, int sector)
9a92bffb 1028{
1029 int ret;
1030
77160d47 1031 fseek(f, base + sector * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE), SEEK_SET);
81abe4d5 1032 ret = fread(dest, 1, CD_FRAMESIZE_RAW, f);
9a92bffb 1033 fread(subbuffer, 1, SUB_FRAMESIZE, f);
1034
1035 if (subChanRaw) DecodeRawSubData();
1036
1037 return ret;
1038}
1039
1040static int uncompress2(void *out, unsigned long *out_size, void *in, unsigned long in_size)
1041{
1042 static z_stream z;
1043 int ret = 0;
1044
1045 if (z.zalloc == NULL) {
1046 // XXX: one-time leak here..
1047 z.next_in = Z_NULL;
1048 z.avail_in = 0;
1049 z.zalloc = Z_NULL;
1050 z.zfree = Z_NULL;
1051 z.opaque = Z_NULL;
1052 ret = inflateInit2(&z, -15);
1053 }
1054 else
1055 ret = inflateReset(&z);
1056 if (ret != Z_OK)
1057 return ret;
1058
1059 z.next_in = in;
1060 z.avail_in = in_size;
1061 z.next_out = out;
1062 z.avail_out = *out_size;
1063
1064 ret = inflate(&z, Z_NO_FLUSH);
1065 //inflateEnd(&z);
1066
1067 *out_size -= z.avail_out;
1068 return ret == 1 ? 0 : ret;
1069}
1070
77160d47 1071static int cdread_compressed(FILE *f, unsigned int base, void *dest, int sector)
9a92bffb 1072{
0877957a 1073 unsigned long cdbuffer_size, cdbuffer_size_expect;
9a92bffb 1074 unsigned int start_byte, size;
0877957a 1075 int is_compressed;
9a92bffb 1076 int ret, block;
1077
77160d47 1078 if (base)
1079 sector += base / 2352;
1080
0877957a 1081 block = sector >> compr_img->block_shift;
1082 compr_img->sector_in_blk = sector & ((1 << compr_img->block_shift) - 1);
9a92bffb 1083
1084 if (block == compr_img->current_block) {
1085 //printf("hit sect %d\n", sector);
1086 goto finish;
1087 }
1088
1089 if (sector >= compr_img->index_len * 16) {
f29fbd53 1090 SysPrintf("sector %d is past img end\n", sector);
9a92bffb 1091 return -1;
1092 }
1093
0877957a 1094 start_byte = compr_img->index_table[block] & 0x7fffffff;
9a92bffb 1095 if (fseek(cdHandle, start_byte, SEEK_SET) != 0) {
f29fbd53 1096 SysPrintf("seek error for block %d at %x: ",
9a92bffb 1097 block, start_byte);
1098 perror(NULL);
1099 return -1;
1100 }
1101
0877957a 1102 is_compressed = !(compr_img->index_table[block] & 0x80000000);
1103 size = (compr_img->index_table[block + 1] & 0x7fffffff) - start_byte;
9a92bffb 1104 if (size > sizeof(compr_img->buff_compressed)) {
f29fbd53 1105 SysPrintf("block %d is too large: %u\n", block, size);
9a92bffb 1106 return -1;
1107 }
1108
0877957a 1109 if (fread(is_compressed ? compr_img->buff_compressed : compr_img->buff_raw[0],
1110 1, size, cdHandle) != size) {
f29fbd53 1111 SysPrintf("read error for block %d at %x: ", block, start_byte);
9a92bffb 1112 perror(NULL);
1113 return -1;
1114 }
1115
0877957a 1116 if (is_compressed) {
1117 cdbuffer_size_expect = sizeof(compr_img->buff_raw[0]) << compr_img->block_shift;
1118 cdbuffer_size = cdbuffer_size_expect;
1119 ret = uncompress2(compr_img->buff_raw[0], &cdbuffer_size, compr_img->buff_compressed, size);
1120 if (ret != 0) {
f29fbd53 1121 SysPrintf("uncompress failed with %d for block %d, sector %d\n",
0877957a 1122 ret, block, sector);
1123 return -1;
1124 }
1125 if (cdbuffer_size != cdbuffer_size_expect)
f29fbd53 1126 SysPrintf("cdbuffer_size: %lu != %lu, sector %d\n", cdbuffer_size,
0877957a 1127 cdbuffer_size_expect, sector);
9a92bffb 1128 }
9a92bffb 1129
1130 // done at last!
1131 compr_img->current_block = block;
1132
1133finish:
1134 if (dest != cdbuffer) // copy avoid HACK
81abe4d5 1135 memcpy(dest, compr_img->buff_raw[compr_img->sector_in_blk],
1136 CD_FRAMESIZE_RAW);
1137 return CD_FRAMESIZE_RAW;
9a92bffb 1138}
1139
77160d47 1140static int cdread_2048(FILE *f, unsigned int base, void *dest, int sector)
9a92bffb 1141{
1142 int ret;
1143
77160d47 1144 fseek(f, base + sector * 2048, SEEK_SET);
81abe4d5 1145 ret = fread((char *)dest + 12 * 2, 1, 2048, f);
9a92bffb 1146
1147 // not really necessary, fake mode 2 header
81abe4d5 1148 memset(cdbuffer, 0, 12 * 2);
1149 sec2msf(sector + 2 * 75, (char *)&cdbuffer[12]);
1150 cdbuffer[12 + 3] = 1;
9a92bffb 1151
1152 return ret;
1153}
1154
1155static unsigned char * CALLBACK ISOgetBuffer_compr(void) {
1156 return compr_img->buff_raw[compr_img->sector_in_blk] + 12;
1157}
1158
1159static unsigned char * CALLBACK ISOgetBuffer(void) {
81abe4d5 1160 return cdbuffer + 12;
9a92bffb 1161}
1162
ef79bbde
P
1163static void PrintTracks(void) {
1164 int i;
1165
1166 for (i = 1; i <= numtracks; i++) {
1167 SysPrintf(_("Track %.2d (%s) - Start %.2d:%.2d:%.2d, Length %.2d:%.2d:%.2d\n"),
1168 i, (ti[i].type == DATA ? "DATA" : "AUDIO"),
1169 ti[i].start[0], ti[i].start[1], ti[i].start[2],
1170 ti[i].length[0], ti[i].length[1], ti[i].length[2]);
1171 }
1172}
1173
1174// This function is invoked by the front-end when opening an ISO
1175// file for playback
1176static long CALLBACK ISOopen(void) {
9a92bffb 1177 boolean isMode1ISO = FALSE;
1178
ef79bbde
P
1179 if (cdHandle != NULL) {
1180 return 0; // it's already open
1181 }
1182
1183 cdHandle = fopen(GetIsoFile(), "rb");
1184 if (cdHandle == NULL) {
1185 return -1;
1186 }
1187
1188 SysPrintf(_("Loaded CD Image: %s"), GetIsoFile());
1189
1190 cddaBigEndian = FALSE;
1191 subChanMixed = FALSE;
1192 subChanRaw = FALSE;
8aa91443 1193 pregapOffset = 0;
0c2871a7 1194 cdrIsoMultidiskCount = 1;
ef79bbde 1195
9a92bffb 1196 CDR_getBuffer = ISOgetBuffer;
1197 cdimg_read_func = cdread_normal;
1198
ef79bbde
P
1199 if (parsecue(GetIsoFile()) == 0) {
1200 SysPrintf("[+cue]");
1201 }
1202 else if (parsetoc(GetIsoFile()) == 0) {
1203 SysPrintf("[+toc]");
1204 }
1205 else if (parseccd(GetIsoFile()) == 0) {
1206 SysPrintf("[+ccd]");
1207 }
1208 else if (parsemds(GetIsoFile()) == 0) {
1209 SysPrintf("[+mds]");
1210 }
0877957a 1211 if (handlepbp(GetIsoFile()) == 0) {
9a92bffb 1212 SysPrintf("[pbp]");
1213 CDR_getBuffer = ISOgetBuffer_compr;
1214 cdimg_read_func = cdread_compressed;
1215 }
0877957a 1216 else if (handlecbin(GetIsoFile()) == 0) {
1217 SysPrintf("[cbin]");
1218 CDR_getBuffer = ISOgetBuffer_compr;
1219 cdimg_read_func = cdread_compressed;
1220 }
ef79bbde
P
1221
1222 if (!subChanMixed && opensubfile(GetIsoFile()) == 0) {
1223 SysPrintf("[+sub]");
1224 }
ae4e7dc9 1225 if (opensbifile(GetIsoFile()) == 0) {
1226 SysPrintf("[+sbi]");
1227 }
ef79bbde 1228
9a92bffb 1229 // guess whether it is mode1/2048
1230 fseek(cdHandle, 0, SEEK_END);
1231 if (ftell(cdHandle) % 2048 == 0) {
1232 unsigned int modeTest = 0;
1233 fseek(cdHandle, 0, SEEK_SET);
1234 fread(&modeTest, 4, 1, cdHandle);
1235 if (SWAP32(modeTest) != 0xffffff00) {
1236 SysPrintf("[2048]");
1237 isMode1ISO = TRUE;
1238 }
1239 }
1240 fseek(cdHandle, 0, SEEK_SET);
1241
ef79bbde
P
1242 SysPrintf(".\n");
1243
1244 PrintTracks();
1245
9a92bffb 1246 if (subChanMixed)
1247 cdimg_read_func = cdread_sub_mixed;
1248 else if (isMode1ISO)
1249 cdimg_read_func = cdread_2048;
1250
38b8102c 1251 // make sure we have another handle open for cdda
1252 if (numtracks > 1 && ti[1].handle == NULL) {
1253 ti[1].handle = fopen(GetIsoFile(), "rb");
1254 }
77160d47 1255 cdda_cur_sector = 0;
1256 cdda_file_offset = 0;
38b8102c 1257
ef79bbde
P
1258 return 0;
1259}
1260
1261static long CALLBACK ISOclose(void) {
38b8102c 1262 int i;
1263
ef79bbde
P
1264 if (cdHandle != NULL) {
1265 fclose(cdHandle);
1266 cdHandle = NULL;
1267 }
1268 if (subHandle != NULL) {
1269 fclose(subHandle);
1270 subHandle = NULL;
1271 }
1272 stopCDDA();
38b8102c 1273 cddaHandle = NULL;
1274
9a92bffb 1275 if (compr_img != NULL) {
1276 free(compr_img->index_table);
1277 free(compr_img);
1278 compr_img = NULL;
1279 }
1280
38b8102c 1281 for (i = 1; i <= numtracks; i++) {
1282 if (ti[i].handle != NULL) {
1283 fclose(ti[i].handle);
1284 ti[i].handle = NULL;
1285 }
1286 }
1287 numtracks = 0;
ae4e7dc9 1288 UnloadSBI();
38b8102c 1289
1290 return 0;
1291}
1292
1293static long CALLBACK ISOinit(void) {
1294 assert(cdHandle == NULL);
1295 assert(subHandle == NULL);
1296
1297 return 0; // do nothing
1298}
1299
1300static long CALLBACK ISOshutdown(void) {
1301 ISOclose();
ef79bbde
P
1302 return 0;
1303}
1304
1305// return Starting and Ending Track
1306// buffer:
1307// byte 0 - start track
1308// byte 1 - end track
1309static long CALLBACK ISOgetTN(unsigned char *buffer) {
1310 buffer[0] = 1;
1311
1312 if (numtracks > 0) {
1313 buffer[1] = numtracks;
1314 }
1315 else {
1316 buffer[1] = 1;
1317 }
1318
1319 return 0;
1320}
1321
1322// return Track Time
1323// buffer:
1324// byte 0 - frame
1325// byte 1 - second
1326// byte 2 - minute
1327static long CALLBACK ISOgetTD(unsigned char track, unsigned char *buffer) {
858ad511 1328 if (track == 0) {
1329 // CD length according pcsxr-svn (done a bit different here)
1330 unsigned int sect;
1331 unsigned char time[3];
1332 sect = msf2sec(ti[numtracks].start) + msf2sec(ti[numtracks].length);
ab948f7e 1333 sec2msf(sect, (char *)time);
858ad511 1334 buffer[2] = time[0];
1335 buffer[1] = time[1];
1336 buffer[0] = time[2];
1337 }
1338 else if (numtracks > 0 && track <= numtracks) {
ef79bbde
P
1339 buffer[2] = ti[track].start[0];
1340 buffer[1] = ti[track].start[1];
1341 buffer[0] = ti[track].start[2];
1342 }
1343 else {
1344 buffer[2] = 0;
1345 buffer[1] = 2;
1346 buffer[0] = 0;
1347 }
1348
1349 return 0;
1350}
1351
1352// decode 'raw' subchannel data ripped by cdrdao
1353static void DecodeRawSubData(void) {
1354 unsigned char subQData[12];
1355 int i;
1356
1357 memset(subQData, 0, sizeof(subQData));
1358
1359 for (i = 0; i < 8 * 12; i++) {
1360 if (subbuffer[i] & (1 << 6)) { // only subchannel Q is needed
1361 subQData[i >> 3] |= (1 << (7 - (i & 7)));
1362 }
1363 }
1364
1365 memcpy(&subbuffer[12], subQData, 12);
1366}
1367
1368// read track
1369// time: byte 0 - minute; byte 1 - second; byte 2 - frame
1370// uses bcd format
1371static long CALLBACK ISOreadTrack(unsigned char *time) {
19c03d80 1372 int sector = MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2]));
1373
ef79bbde
P
1374 if (cdHandle == NULL) {
1375 return -1;
1376 }
1377
8aa91443 1378 if (pregapOffset) {
1379 subChanMissing = FALSE;
1380 if (sector >= pregapOffset) {
1381 sector -= 2 * 75;
1382 if (sector < pregapOffset)
1383 subChanMissing = TRUE;
1384 }
1385 }
1386
77160d47 1387 cdimg_read_func(cdHandle, 0, cdbuffer, sector);
ef79bbde 1388
9a92bffb 1389 if (subHandle != NULL) {
1390 fseek(subHandle, sector * SUB_FRAMESIZE, SEEK_SET);
1391 fread(subbuffer, 1, SUB_FRAMESIZE, subHandle);
ef79bbde 1392
9a92bffb 1393 if (subChanRaw) DecodeRawSubData();
ef79bbde
P
1394 }
1395
1396 return 0;
1397}
1398
ef79bbde
P
1399// plays cdda audio
1400// sector: byte 0 - minute; byte 1 - second; byte 2 - frame
1401// does NOT uses bcd format
1402static long CALLBACK ISOplay(unsigned char *time) {
77160d47 1403 unsigned int i;
38b8102c 1404
7b8da7ab 1405 if (numtracks <= 1)
1406 return 0;
1407
38b8102c 1408 // find the track
77160d47 1409 cdda_cur_sector = msf2sec((char *)time);
2058d51f 1410 for (i = numtracks; i > 1; i--) {
77160d47 1411 cdda_first_sector = msf2sec(ti[i].start);
1412 if (cdda_first_sector <= cdda_cur_sector + 2 * 75)
38b8102c 1413 break;
2058d51f 1414 }
77160d47 1415 cdda_file_offset = ti[i].start_offset;
19c03d80 1416
38b8102c 1417 // find the file that contains this track
1418 for (; i > 1; i--)
1419 if (ti[i].handle != NULL)
1420 break;
1421
2058d51f 1422 cddaHandle = ti[i].handle;
8aa91443 1423
77160d47 1424 if (SPU_playCDDAchannel != NULL)
1425 startCDDA();
1426
ef79bbde
P
1427 return 0;
1428}
1429
1430// stops cdda audio
1431static long CALLBACK ISOstop(void) {
1432 stopCDDA();
1433 return 0;
1434}
1435
1436// gets subchannel data
1437static unsigned char* CALLBACK ISOgetBufferSub(void) {
8aa91443 1438 if ((subHandle != NULL || subChanMixed) && !subChanMissing) {
ef79bbde
P
1439 return subbuffer;
1440 }
1441
1442 return NULL;
1443}
1444
1445static long CALLBACK ISOgetStatus(struct CdrStat *stat) {
1446 int sec;
1447
1448 CDR__getStatus(stat);
1449
1450 if (playing) {
1451 stat->Type = 0x02;
1452 stat->Status |= 0x80;
ef79bbde
P
1453 }
1454 else {
1455 stat->Type = 0x01;
1456 }
1457
77160d47 1458 sec = cdda_cur_sector;
858ad511 1459 sec2msf(sec, (char *)stat->Time);
1460
ef79bbde
P
1461 return 0;
1462}
1463
1464void cdrIsoInit(void) {
1465 CDR_init = ISOinit;
1466 CDR_shutdown = ISOshutdown;
1467 CDR_open = ISOopen;
1468 CDR_close = ISOclose;
1469 CDR_getTN = ISOgetTN;
1470 CDR_getTD = ISOgetTD;
1471 CDR_readTrack = ISOreadTrack;
1472 CDR_getBuffer = ISOgetBuffer;
1473 CDR_play = ISOplay;
1474 CDR_stop = ISOstop;
1475 CDR_getBufferSub = ISOgetBufferSub;
1476 CDR_getStatus = ISOgetStatus;
1477
1478 CDR_getDriveLetter = CDR__getDriveLetter;
1479 CDR_configure = CDR__configure;
1480 CDR_test = CDR__test;
1481 CDR_about = CDR__about;
1482 CDR_setfilename = CDR__setfilename;
1483
1484 numtracks = 0;
1485}
1486
1487int cdrIsoActive(void) {
1488 return (cdHandle != NULL);
1489}