Commit | Line | Data |
---|---|---|
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., * | |
deebc67f | 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * |
ef79bbde P |
20 | ***************************************************************************/ |
21 | ||
270c6dd1 | 22 | #include <compat/fopen_utf8.h> |
23 | ||
ef79bbde P |
24 | #include "psxcommon.h" |
25 | #include "plugins.h" | |
26 | #include "cdrom.h" | |
27 | #include "cdriso.h" | |
ae4e7dc9 | 28 | #include "ppf.h" |
ef79bbde | 29 | |
f9fb1cf7 | 30 | #include <errno.h> |
31 | #include <zlib.h> | |
b0ec2e6d | 32 | #ifdef HAVE_CHD |
ce188d4d | 33 | #include <chd.h> |
b0ec2e6d | 34 | #endif |
f9fb1cf7 | 35 | |
ef79bbde | 36 | #ifdef _WIN32 |
003cfc63 | 37 | #define WIN32_LEAN_AND_MEAN |
ef79bbde P |
38 | #include <process.h> |
39 | #include <windows.h> | |
deebc67f | 40 | #define strcasecmp _stricmp |
ce36c10b | 41 | #define usleep(x) (Sleep((x) / 1000)) |
ef79bbde P |
42 | #else |
43 | #include <pthread.h> | |
44 | #include <sys/time.h> | |
349f7d81 | 45 | #include <unistd.h> |
ef79bbde P |
46 | #endif |
47 | ||
cc376814 | 48 | #define OFF_T_MSB ((off_t)1 << (sizeof(off_t) * 8 - 1)) |
49 | ||
0c2871a7 | 50 | unsigned int cdrIsoMultidiskCount; |
51 | unsigned int cdrIsoMultidiskSelect; | |
52 | ||
ef79bbde P |
53 | static FILE *cdHandle = NULL; |
54 | static FILE *cddaHandle = NULL; | |
55 | static FILE *subHandle = NULL; | |
56 | ||
57 | static boolean subChanMixed = FALSE; | |
58 | static boolean subChanRaw = FALSE; | |
8aa91443 | 59 | static boolean subChanMissing = FALSE; |
ef79bbde | 60 | |
deebc67f | 61 | static boolean multifile = FALSE; |
62 | ||
81abe4d5 | 63 | static unsigned char cdbuffer[CD_FRAMESIZE_RAW]; |
ef79bbde P |
64 | static unsigned char subbuffer[SUB_FRAMESIZE]; |
65 | ||
66 | static unsigned char sndbuffer[CD_FRAMESIZE_RAW * 10]; | |
67 | ||
68 | #define CDDA_FRAMETIME (1000 * (sizeof(sndbuffer) / CD_FRAMESIZE_RAW) / 75) | |
69 | ||
70 | #ifdef _WIN32 | |
71 | static HANDLE threadid; | |
72 | #else | |
73 | static pthread_t threadid; | |
74 | #endif | |
75 | static unsigned int initial_offset = 0; | |
38b8102c | 76 | static boolean playing = FALSE; |
ef79bbde | 77 | static boolean cddaBigEndian = FALSE; |
77160d47 | 78 | // cdda sectors in toc, byte offset in file |
9a92bffb | 79 | static unsigned int cdda_cur_sector; |
2058d51f | 80 | static unsigned int cdda_first_sector; |
77160d47 | 81 | static unsigned int cdda_file_offset; |
8aa91443 | 82 | /* Frame offset into CD image where pregap data would be found if it was there. |
83 | * If a game seeks there we must *not* return subchannel data since it's | |
84 | * not in the CD image, so that cdrom code can fake subchannel data instead. | |
85 | * XXX: there could be multiple pregaps but PSX dumps only have one? */ | |
86 | static unsigned int pregapOffset; | |
ef79bbde | 87 | |
deebc67f | 88 | #define cddaCurPos cdda_cur_sector |
89 | ||
9a92bffb | 90 | // compressed image stuff |
91 | static struct { | |
92 | unsigned char buff_raw[16][CD_FRAMESIZE_RAW]; | |
93 | unsigned char buff_compressed[CD_FRAMESIZE_RAW * 16 + 100]; | |
cc376814 | 94 | off_t *index_table; |
9a92bffb | 95 | unsigned int index_len; |
0877957a | 96 | unsigned int block_shift; |
9a92bffb | 97 | unsigned int current_block; |
98 | unsigned int sector_in_blk; | |
99 | } *compr_img; | |
100 | ||
b0ec2e6d | 101 | #ifdef HAVE_CHD |
ce188d4d | 102 | static struct { |
103 | unsigned char (*buffer)[CD_FRAMESIZE_RAW + SUB_FRAMESIZE]; | |
104 | chd_file* chd; | |
105 | const chd_header* header; | |
106 | unsigned int sectors_per_hunk; | |
107 | unsigned int current_hunk; | |
108 | unsigned int sector_in_hunk; | |
109 | } *chd_img; | |
b0ec2e6d | 110 | #endif |
ce188d4d | 111 | |
77160d47 | 112 | int (*cdimg_read_func)(FILE *f, unsigned int base, void *dest, int sector); |
9a92bffb | 113 | |
ef79bbde P |
114 | char* CALLBACK CDR__getDriveLetter(void); |
115 | long CALLBACK CDR__configure(void); | |
116 | long CALLBACK CDR__test(void); | |
117 | void CALLBACK CDR__about(void); | |
118 | long CALLBACK CDR__setfilename(char *filename); | |
119 | long CALLBACK CDR__getStatus(struct CdrStat *stat); | |
120 | ||
19c03d80 | 121 | static void DecodeRawSubData(void); |
122 | ||
ef79bbde | 123 | struct trackinfo { |
deebc67f | 124 | enum {DATA=1, CDDA} type; |
ef79bbde P |
125 | char start[3]; // MSF-format |
126 | char length[3]; // MSF-format | |
38b8102c | 127 | FILE *handle; // for multi-track images CDDA |
77160d47 | 128 | unsigned int start_offset; // byte offset from start of above file |
ef79bbde P |
129 | }; |
130 | ||
131 | #define MAXTRACKS 100 /* How many tracks can a CD hold? */ | |
132 | ||
133 | static int numtracks = 0; | |
134 | static struct trackinfo ti[MAXTRACKS]; | |
135 | ||
136 | // get a sector from a msf-array | |
137 | static unsigned int msf2sec(char *msf) { | |
138 | return ((msf[0] * 60 + msf[1]) * 75) + msf[2]; | |
139 | } | |
140 | ||
141 | static void sec2msf(unsigned int s, char *msf) { | |
142 | msf[0] = s / 75 / 60; | |
143 | s = s - msf[0] * 75 * 60; | |
144 | msf[1] = s / 75; | |
145 | s = s - msf[1] * 75; | |
146 | msf[2] = s; | |
147 | } | |
148 | ||
149 | // divide a string of xx:yy:zz into m, s, f | |
150 | static void tok2msf(char *time, char *msf) { | |
151 | char *token; | |
152 | ||
153 | token = strtok(time, ":"); | |
154 | if (token) { | |
155 | msf[0] = atoi(token); | |
156 | } | |
157 | else { | |
158 | msf[0] = 0; | |
159 | } | |
160 | ||
161 | token = strtok(NULL, ":"); | |
162 | if (token) { | |
163 | msf[1] = atoi(token); | |
164 | } | |
165 | else { | |
166 | msf[1] = 0; | |
167 | } | |
168 | ||
169 | token = strtok(NULL, ":"); | |
170 | if (token) { | |
171 | msf[2] = atoi(token); | |
172 | } | |
173 | else { | |
174 | msf[2] = 0; | |
175 | } | |
176 | } | |
177 | ||
178 | #ifndef _WIN32 | |
179 | static long GetTickCount(void) { | |
180 | static time_t initial_time = 0; | |
181 | struct timeval now; | |
182 | ||
183 | gettimeofday(&now, NULL); | |
184 | ||
185 | if (initial_time == 0) { | |
186 | initial_time = now.tv_sec; | |
187 | } | |
188 | ||
189 | return (now.tv_sec - initial_time) * 1000L + now.tv_usec / 1000L; | |
190 | } | |
191 | #endif | |
192 | ||
193 | // this thread plays audio data | |
194 | #ifdef _WIN32 | |
195 | static void playthread(void *param) | |
196 | #else | |
197 | static void *playthread(void *param) | |
198 | #endif | |
199 | { | |
983a7cfd | 200 | long osleep, d, t, i, s; |
ef79bbde | 201 | unsigned char tmp; |
77160d47 | 202 | int ret = 0, sector_offs; |
ef79bbde P |
203 | |
204 | t = GetTickCount(); | |
205 | ||
206 | while (playing) { | |
9a92bffb | 207 | s = 0; |
208 | for (i = 0; i < sizeof(sndbuffer) / CD_FRAMESIZE_RAW; i++) { | |
77160d47 | 209 | sector_offs = cdda_cur_sector - cdda_first_sector; |
210 | if (sector_offs < 0) { | |
211 | d = CD_FRAMESIZE_RAW; | |
212 | memset(sndbuffer + s, 0, d); | |
213 | } | |
214 | else { | |
215 | d = cdimg_read_func(cddaHandle, cdda_file_offset, | |
216 | sndbuffer + s, sector_offs); | |
217 | if (d < CD_FRAMESIZE_RAW) | |
218 | break; | |
219 | } | |
2058d51f | 220 | |
9a92bffb | 221 | s += d; |
222 | cdda_cur_sector++; | |
ef79bbde | 223 | } |
19c03d80 | 224 | |
ef79bbde P |
225 | if (s == 0) { |
226 | playing = FALSE; | |
ef79bbde P |
227 | initial_offset = 0; |
228 | break; | |
229 | } | |
230 | ||
231 | if (!cdr.Muted && playing) { | |
232 | if (cddaBigEndian) { | |
233 | for (i = 0; i < s / 2; i++) { | |
234 | tmp = sndbuffer[i * 2]; | |
235 | sndbuffer[i * 2] = sndbuffer[i * 2 + 1]; | |
236 | sndbuffer[i * 2 + 1] = tmp; | |
237 | } | |
238 | } | |
239 | ||
53598a71 | 240 | // can't do it yet due to readahead.. |
241 | //cdrAttenuate((short *)sndbuffer, s / 4, 1); | |
983a7cfd | 242 | do { |
243 | ret = SPU_playCDDAchannel((short *)sndbuffer, s); | |
244 | if (ret == 0x7761) | |
ce36c10b | 245 | { |
983a7cfd | 246 | usleep(6 * 1000); |
ce36c10b | 247 | } |
983a7cfd | 248 | } while (ret == 0x7761 && playing); // rearmed_wait |
ef79bbde | 249 | } |
983a7cfd | 250 | |
251 | if (ret != 0x676f) { // !rearmed_go | |
252 | // do approx sleep | |
253 | long now; | |
254 | ||
255 | // HACK: stop feeding data while emu is paused | |
256 | extern int stop; | |
257 | while (stop && playing) | |
ce36c10b | 258 | { |
983a7cfd | 259 | usleep(10000); |
ce36c10b | 260 | } |
983a7cfd | 261 | |
262 | now = GetTickCount(); | |
263 | osleep = t - now; | |
264 | if (osleep <= 0) { | |
265 | osleep = 1; | |
266 | t = now; | |
267 | } | |
268 | else if (osleep > CDDA_FRAMETIME) { | |
269 | osleep = CDDA_FRAMETIME; | |
270 | t = now; | |
271 | } | |
272 | ||
273 | usleep(osleep * 1000); | |
274 | t += CDDA_FRAMETIME; | |
275 | } | |
276 | ||
ef79bbde P |
277 | } |
278 | ||
279 | #ifdef _WIN32 | |
280 | _endthread(); | |
281 | #else | |
282 | pthread_exit(0); | |
283 | return NULL; | |
284 | #endif | |
285 | } | |
286 | ||
287 | // stop the CDDA playback | |
288 | static void stopCDDA() { | |
289 | if (!playing) { | |
290 | return; | |
291 | } | |
292 | ||
293 | playing = FALSE; | |
294 | #ifdef _WIN32 | |
295 | WaitForSingleObject(threadid, INFINITE); | |
296 | #else | |
297 | pthread_join(threadid, NULL); | |
298 | #endif | |
ef79bbde P |
299 | } |
300 | ||
301 | // start the CDDA playback | |
77160d47 | 302 | static void startCDDA(void) { |
ef79bbde | 303 | if (playing) { |
ef79bbde P |
304 | stopCDDA(); |
305 | } | |
306 | ||
ef79bbde P |
307 | playing = TRUE; |
308 | ||
309 | #ifdef _WIN32 | |
310 | threadid = (HANDLE)_beginthread(playthread, 0, NULL); | |
311 | #else | |
312 | pthread_create(&threadid, NULL, playthread, NULL); | |
313 | #endif | |
314 | } | |
315 | ||
316 | // this function tries to get the .toc file of the given .bin | |
317 | // the necessary data is put into the ti (trackinformation)-array | |
318 | static int parsetoc(const char *isofile) { | |
319 | char tocname[MAXPATHLEN]; | |
320 | FILE *fi; | |
e999be41 | 321 | char linebuf[256], tmp[256], name[256]; |
ef79bbde P |
322 | char *token; |
323 | char time[20], time2[20]; | |
e999be41 | 324 | unsigned int t, sector_offs, sector_size; |
325 | unsigned int current_zero_gap = 0; | |
ef79bbde P |
326 | |
327 | numtracks = 0; | |
328 | ||
329 | // copy name of the iso and change extension from .bin to .toc | |
330 | strncpy(tocname, isofile, sizeof(tocname)); | |
331 | tocname[MAXPATHLEN - 1] = '\0'; | |
332 | if (strlen(tocname) >= 4) { | |
333 | strcpy(tocname + strlen(tocname) - 4, ".toc"); | |
334 | } | |
335 | else { | |
336 | return -1; | |
337 | } | |
338 | ||
270c6dd1 | 339 | if ((fi = fopen_utf8(tocname, "r")) == NULL) { |
ef79bbde P |
340 | // try changing extension to .cue (to satisfy some stupid tutorials) |
341 | strcpy(tocname + strlen(tocname) - 4, ".cue"); | |
270c6dd1 | 342 | if ((fi = fopen_utf8(tocname, "r")) == NULL) { |
ef79bbde P |
343 | // if filename is image.toc.bin, try removing .bin (for Brasero) |
344 | strcpy(tocname, isofile); | |
345 | t = strlen(tocname); | |
346 | if (t >= 8 && strcmp(tocname + t - 8, ".toc.bin") == 0) { | |
347 | tocname[t - 4] = '\0'; | |
270c6dd1 | 348 | if ((fi = fopen_utf8(tocname, "r")) == NULL) { |
ef79bbde P |
349 | return -1; |
350 | } | |
351 | } | |
352 | else { | |
353 | return -1; | |
354 | } | |
355 | } | |
92f43ab0 | 356 | // check if it's really a TOC named as a .cue |
357 | fgets(linebuf, sizeof(linebuf), fi); | |
4295c491 | 358 | token = strtok(linebuf, " "); |
359 | if (token && strncmp(token, "CD", 2) != 0 && strcmp(token, "CATALOG") != 0) { | |
92f43ab0 | 360 | fclose(fi); |
361 | return -1; | |
362 | } | |
363 | fseek(fi, 0, SEEK_SET); | |
ef79bbde P |
364 | } |
365 | ||
366 | memset(&ti, 0, sizeof(ti)); | |
367 | cddaBigEndian = TRUE; // cdrdao uses big-endian for CD Audio | |
368 | ||
e999be41 | 369 | sector_size = CD_FRAMESIZE_RAW; |
19c03d80 | 370 | sector_offs = 2 * 75; |
371 | ||
ef79bbde P |
372 | // parse the .toc file |
373 | while (fgets(linebuf, sizeof(linebuf), fi) != NULL) { | |
374 | // search for tracks | |
e999be41 | 375 | strncpy(tmp, linebuf, sizeof(linebuf)); |
376 | token = strtok(tmp, " "); | |
ef79bbde P |
377 | |
378 | if (token == NULL) continue; | |
379 | ||
380 | if (!strcmp(token, "TRACK")) { | |
e999be41 | 381 | sector_offs += current_zero_gap; |
382 | current_zero_gap = 0; | |
383 | ||
ef79bbde P |
384 | // get type of track |
385 | token = strtok(NULL, " "); | |
386 | numtracks++; | |
387 | ||
388 | if (!strncmp(token, "MODE2_RAW", 9)) { | |
389 | ti[numtracks].type = DATA; | |
390 | sec2msf(2 * 75, ti[numtracks].start); // assume data track on 0:2:0 | |
391 | ||
392 | // check if this image contains mixed subchannel data | |
393 | token = strtok(NULL, " "); | |
e999be41 | 394 | if (token != NULL && !strncmp(token, "RW", 2)) { |
395 | sector_size = CD_FRAMESIZE_RAW + SUB_FRAMESIZE; | |
ef79bbde | 396 | subChanMixed = TRUE; |
e999be41 | 397 | if (!strncmp(token, "RW_RAW", 6)) |
398 | subChanRaw = TRUE; | |
ef79bbde P |
399 | } |
400 | } | |
401 | else if (!strncmp(token, "AUDIO", 5)) { | |
402 | ti[numtracks].type = CDDA; | |
403 | } | |
404 | } | |
405 | else if (!strcmp(token, "DATAFILE")) { | |
406 | if (ti[numtracks].type == CDDA) { | |
407 | sscanf(linebuf, "DATAFILE \"%[^\"]\" #%d %8s", name, &t, time2); | |
19c03d80 | 408 | ti[numtracks].start_offset = t; |
e999be41 | 409 | t = t / sector_size + sector_offs; |
ef79bbde P |
410 | sec2msf(t, (char *)&ti[numtracks].start); |
411 | tok2msf((char *)&time2, (char *)&ti[numtracks].length); | |
412 | } | |
413 | else { | |
414 | sscanf(linebuf, "DATAFILE \"%[^\"]\" %8s", name, time); | |
415 | tok2msf((char *)&time, (char *)&ti[numtracks].length); | |
416 | } | |
417 | } | |
418 | else if (!strcmp(token, "FILE")) { | |
419 | sscanf(linebuf, "FILE \"%[^\"]\" #%d %8s %8s", name, &t, time, time2); | |
420 | tok2msf((char *)&time, (char *)&ti[numtracks].start); | |
e999be41 | 421 | t += msf2sec(ti[numtracks].start) * sector_size; |
19c03d80 | 422 | ti[numtracks].start_offset = t; |
e999be41 | 423 | t = t / sector_size + sector_offs; |
ef79bbde P |
424 | sec2msf(t, (char *)&ti[numtracks].start); |
425 | tok2msf((char *)&time2, (char *)&ti[numtracks].length); | |
426 | } | |
e999be41 | 427 | else if (!strcmp(token, "ZERO") || !strcmp(token, "SILENCE")) { |
428 | // skip unneeded optional fields | |
429 | while (token != NULL) { | |
430 | token = strtok(NULL, " "); | |
431 | if (strchr(token, ':') != NULL) | |
432 | break; | |
433 | } | |
434 | if (token != NULL) { | |
435 | tok2msf(token, tmp); | |
436 | current_zero_gap = msf2sec(tmp); | |
437 | } | |
8aa91443 | 438 | if (numtracks > 1) { |
439 | t = ti[numtracks - 1].start_offset; | |
e999be41 | 440 | t /= sector_size; |
8aa91443 | 441 | pregapOffset = t + msf2sec(ti[numtracks - 1].length); |
442 | } | |
19c03d80 | 443 | } |
e999be41 | 444 | else if (!strcmp(token, "START")) { |
445 | token = strtok(NULL, " "); | |
446 | if (token != NULL && strchr(token, ':')) { | |
447 | tok2msf(token, tmp); | |
448 | t = msf2sec(tmp); | |
449 | ti[numtracks].start_offset += (t - current_zero_gap) * sector_size; | |
450 | t = msf2sec(ti[numtracks].start) + t; | |
451 | sec2msf(t, (char *)&ti[numtracks].start); | |
452 | } | |
453 | } | |
ef79bbde P |
454 | } |
455 | ||
456 | fclose(fi); | |
457 | ||
458 | return 0; | |
459 | } | |
460 | ||
461 | // this function tries to get the .cue file of the given .bin | |
462 | // the necessary data is put into the ti (trackinformation)-array | |
463 | static int parsecue(const char *isofile) { | |
464 | char cuename[MAXPATHLEN]; | |
38b8102c | 465 | char filepath[MAXPATHLEN]; |
466 | char *incue_fname; | |
ef79bbde P |
467 | FILE *fi; |
468 | char *token; | |
469 | char time[20]; | |
470 | char *tmp; | |
19c03d80 | 471 | char linebuf[256], tmpb[256], dummy[256]; |
38b8102c | 472 | unsigned int incue_max_len; |
77160d47 | 473 | unsigned int t, file_len, mode, sector_offs; |
474 | unsigned int sector_size = 2352; | |
ef79bbde P |
475 | |
476 | numtracks = 0; | |
477 | ||
478 | // copy name of the iso and change extension from .bin to .cue | |
479 | strncpy(cuename, isofile, sizeof(cuename)); | |
480 | cuename[MAXPATHLEN - 1] = '\0'; | |
481 | if (strlen(cuename) >= 4) { | |
2cc221eb | 482 | // If 'isofile' is a '.cd<X>' file, use it as a .cue file |
483 | // and don't try to search the additional .cue file | |
484 | if (strncasecmp(cuename + strlen(cuename) - 4, ".cd", 3) != 0 ) | |
485 | strcpy(cuename + strlen(cuename) - 4, ".cue"); | |
ef79bbde P |
486 | } |
487 | else { | |
488 | return -1; | |
489 | } | |
490 | ||
270c6dd1 | 491 | if ((fi = fopen_utf8(cuename, "r")) == NULL) { |
ef79bbde P |
492 | return -1; |
493 | } | |
494 | ||
495 | // Some stupid tutorials wrongly tell users to use cdrdao to rip a | |
496 | // "bin/cue" image, which is in fact a "bin/toc" image. So let's check | |
497 | // that... | |
498 | if (fgets(linebuf, sizeof(linebuf), fi) != NULL) { | |
499 | if (!strncmp(linebuf, "CD_ROM_XA", 9)) { | |
500 | // Don't proceed further, as this is actually a .toc file rather | |
501 | // than a .cue file. | |
502 | fclose(fi); | |
503 | return parsetoc(isofile); | |
504 | } | |
505 | fseek(fi, 0, SEEK_SET); | |
506 | } | |
507 | ||
38b8102c | 508 | // build a path for files referenced in .cue |
509 | strncpy(filepath, cuename, sizeof(filepath)); | |
22bbabf6 | 510 | tmp = strrchr(filepath, '/'); |
38b8102c | 511 | if (tmp == NULL) |
22bbabf6 | 512 | tmp = strrchr(filepath, '\\'); |
513 | if (tmp != NULL) | |
514 | tmp++; | |
515 | else | |
38b8102c | 516 | tmp = filepath; |
517 | *tmp = 0; | |
518 | filepath[sizeof(filepath) - 1] = 0; | |
519 | incue_fname = tmp; | |
520 | incue_max_len = sizeof(filepath) - (tmp - filepath) - 1; | |
521 | ||
ef79bbde P |
522 | memset(&ti, 0, sizeof(ti)); |
523 | ||
19c03d80 | 524 | file_len = 0; |
525 | sector_offs = 2 * 75; | |
526 | ||
ef79bbde P |
527 | while (fgets(linebuf, sizeof(linebuf), fi) != NULL) { |
528 | strncpy(dummy, linebuf, sizeof(linebuf)); | |
529 | token = strtok(dummy, " "); | |
530 | ||
531 | if (token == NULL) { | |
532 | continue; | |
533 | } | |
534 | ||
19c03d80 | 535 | if (!strcmp(token, "TRACK")) { |
ef79bbde P |
536 | numtracks++; |
537 | ||
77160d47 | 538 | sector_size = 0; |
ef79bbde P |
539 | if (strstr(linebuf, "AUDIO") != NULL) { |
540 | ti[numtracks].type = CDDA; | |
77160d47 | 541 | sector_size = 2352; |
ef79bbde | 542 | } |
77160d47 | 543 | else if (sscanf(linebuf, " TRACK %u MODE%u/%u", &t, &mode, §or_size) == 3) |
ef79bbde | 544 | ti[numtracks].type = DATA; |
77160d47 | 545 | else { |
546 | SysPrintf(".cue: failed to parse TRACK\n"); | |
547 | ti[numtracks].type = numtracks == 1 ? DATA : CDDA; | |
ef79bbde | 548 | } |
77160d47 | 549 | if (sector_size == 0) |
550 | sector_size = 2352; | |
ef79bbde P |
551 | } |
552 | else if (!strcmp(token, "INDEX")) { | |
77160d47 | 553 | if (sscanf(linebuf, " INDEX %02d %8s", &t, time) != 2) |
554 | SysPrintf(".cue: failed to parse INDEX\n"); | |
19c03d80 | 555 | tok2msf(time, (char *)&ti[numtracks].start); |
ef79bbde | 556 | |
19c03d80 | 557 | t = msf2sec(ti[numtracks].start); |
77160d47 | 558 | ti[numtracks].start_offset = t * sector_size; |
19c03d80 | 559 | t += sector_offs; |
ef79bbde | 560 | sec2msf(t, ti[numtracks].start); |
19c03d80 | 561 | |
562 | // default track length to file length | |
77160d47 | 563 | t = file_len - ti[numtracks].start_offset / sector_size; |
19c03d80 | 564 | sec2msf(t, ti[numtracks].length); |
565 | ||
566 | if (numtracks > 1 && ti[numtracks].handle == NULL) { | |
567 | // this track uses the same file as the last, | |
568 | // start of this track is last track's end | |
569 | t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start); | |
570 | sec2msf(t, ti[numtracks - 1].length); | |
38b8102c | 571 | } |
8aa91443 | 572 | if (numtracks > 1 && pregapOffset == -1) |
77160d47 | 573 | pregapOffset = ti[numtracks].start_offset / sector_size; |
19c03d80 | 574 | } |
575 | else if (!strcmp(token, "PREGAP")) { | |
576 | if (sscanf(linebuf, " PREGAP %8s", time) == 1) { | |
577 | tok2msf(time, dummy); | |
578 | sector_offs += msf2sec(dummy); | |
38b8102c | 579 | } |
8aa91443 | 580 | pregapOffset = -1; // mark to fill track start_offset |
19c03d80 | 581 | } |
582 | else if (!strcmp(token, "FILE")) { | |
9a9bcd78 | 583 | t = sscanf(linebuf, " FILE \"%255[^\"]\"", tmpb); |
385c023b | 584 | if (t != 1) |
9a9bcd78 | 585 | sscanf(linebuf, " FILE %255s", tmpb); |
19c03d80 | 586 | |
6f4557bf | 587 | tmp = strrchr(tmpb, '\\'); |
588 | if (tmp == NULL) | |
589 | tmp = strrchr(tmpb, '/'); | |
590 | if (tmp != NULL) | |
591 | tmp++; | |
592 | else | |
593 | tmp = tmpb; | |
594 | strncpy(incue_fname, tmp, incue_max_len); | |
270c6dd1 | 595 | ti[numtracks + 1].handle = fopen_utf8(filepath, "rb"); |
ef79bbde | 596 | |
19c03d80 | 597 | // update global offset if this is not first file in this .cue |
deebc67f | 598 | if (numtracks + 1 > 1) { |
599 | multifile = 1; | |
19c03d80 | 600 | sector_offs += file_len; |
deebc67f | 601 | } |
19c03d80 | 602 | |
603 | file_len = 0; | |
38b8102c | 604 | if (ti[numtracks + 1].handle == NULL) { |
19c03d80 | 605 | SysPrintf(_("\ncould not open: %s\n"), filepath); |
606 | continue; | |
38b8102c | 607 | } |
19c03d80 | 608 | fseek(ti[numtracks + 1].handle, 0, SEEK_END); |
609 | file_len = ftell(ti[numtracks + 1].handle) / 2352; | |
610 | ||
611 | if (numtracks == 0 && strlen(isofile) >= 4 && | |
2cc221eb | 612 | (strcmp(isofile + strlen(isofile) - 4, ".cue") == 0 || |
613 | strncasecmp(isofile + strlen(isofile) - 4, ".cd", 3) == 0)) { | |
614 | // user selected .cue/.cdX as image file, use it's data track instead | |
38b8102c | 615 | fclose(cdHandle); |
270c6dd1 | 616 | cdHandle = fopen_utf8(filepath, "rb"); |
ef79bbde P |
617 | } |
618 | } | |
619 | } | |
620 | ||
621 | fclose(fi); | |
622 | ||
2cc221eb | 623 | // if there are no tracks detected, then it's not a cue file |
624 | if (!numtracks) | |
625 | return -1; | |
626 | ||
ef79bbde P |
627 | return 0; |
628 | } | |
629 | ||
630 | // this function tries to get the .ccd file of the given .img | |
631 | // the necessary data is put into the ti (trackinformation)-array | |
632 | static int parseccd(const char *isofile) { | |
633 | char ccdname[MAXPATHLEN]; | |
634 | FILE *fi; | |
635 | char linebuf[256]; | |
636 | unsigned int t; | |
637 | ||
638 | numtracks = 0; | |
639 | ||
640 | // copy name of the iso and change extension from .img to .ccd | |
641 | strncpy(ccdname, isofile, sizeof(ccdname)); | |
642 | ccdname[MAXPATHLEN - 1] = '\0'; | |
643 | if (strlen(ccdname) >= 4) { | |
644 | strcpy(ccdname + strlen(ccdname) - 4, ".ccd"); | |
645 | } | |
646 | else { | |
647 | return -1; | |
648 | } | |
649 | ||
270c6dd1 | 650 | if ((fi = fopen_utf8(ccdname, "r")) == NULL) { |
ef79bbde P |
651 | return -1; |
652 | } | |
653 | ||
654 | memset(&ti, 0, sizeof(ti)); | |
655 | ||
656 | while (fgets(linebuf, sizeof(linebuf), fi) != NULL) { | |
657 | if (!strncmp(linebuf, "[TRACK", 6)){ | |
658 | numtracks++; | |
659 | } | |
660 | else if (!strncmp(linebuf, "MODE=", 5)) { | |
661 | sscanf(linebuf, "MODE=%d", &t); | |
662 | ti[numtracks].type = ((t == 0) ? CDDA : DATA); | |
663 | } | |
664 | else if (!strncmp(linebuf, "INDEX 1=", 8)) { | |
665 | sscanf(linebuf, "INDEX 1=%d", &t); | |
666 | sec2msf(t + 2 * 75, ti[numtracks].start); | |
77160d47 | 667 | ti[numtracks].start_offset = t * 2352; |
ef79bbde P |
668 | |
669 | // If we've already seen another track, this is its end | |
670 | if (numtracks > 1) { | |
671 | t = msf2sec(ti[numtracks].start) - msf2sec(ti[numtracks - 1].start); | |
672 | sec2msf(t, ti[numtracks - 1].length); | |
673 | } | |
674 | } | |
675 | } | |
676 | ||
677 | fclose(fi); | |
678 | ||
679 | // Fill out the last track's end based on size | |
680 | if (numtracks >= 1) { | |
681 | fseek(cdHandle, 0, SEEK_END); | |
682 | t = ftell(cdHandle) / 2352 - msf2sec(ti[numtracks].start) + 2 * 75; | |
683 | sec2msf(t, ti[numtracks].length); | |
684 | } | |
685 | ||
686 | return 0; | |
687 | } | |
688 | ||
689 | // this function tries to get the .mds file of the given .mdf | |
690 | // the necessary data is put into the ti (trackinformation)-array | |
691 | static int parsemds(const char *isofile) { | |
692 | char mdsname[MAXPATHLEN]; | |
693 | FILE *fi; | |
694 | unsigned int offset, extra_offset, l, i; | |
695 | unsigned short s; | |
696 | ||
697 | numtracks = 0; | |
698 | ||
699 | // copy name of the iso and change extension from .mdf to .mds | |
700 | strncpy(mdsname, isofile, sizeof(mdsname)); | |
701 | mdsname[MAXPATHLEN - 1] = '\0'; | |
702 | if (strlen(mdsname) >= 4) { | |
703 | strcpy(mdsname + strlen(mdsname) - 4, ".mds"); | |
704 | } | |
705 | else { | |
706 | return -1; | |
707 | } | |
708 | ||
270c6dd1 | 709 | if ((fi = fopen_utf8(mdsname, "rb")) == NULL) { |
ef79bbde P |
710 | return -1; |
711 | } | |
712 | ||
713 | memset(&ti, 0, sizeof(ti)); | |
714 | ||
715 | // check if it's a valid mds file | |
716 | fread(&i, 1, sizeof(unsigned int), fi); | |
717 | i = SWAP32(i); | |
718 | if (i != 0x4944454D) { | |
719 | // not an valid mds file | |
720 | fclose(fi); | |
721 | return -1; | |
722 | } | |
723 | ||
724 | // get offset to session block | |
725 | fseek(fi, 0x50, SEEK_SET); | |
726 | fread(&offset, 1, sizeof(unsigned int), fi); | |
727 | offset = SWAP32(offset); | |
728 | ||
729 | // get total number of tracks | |
730 | offset += 14; | |
731 | fseek(fi, offset, SEEK_SET); | |
732 | fread(&s, 1, sizeof(unsigned short), fi); | |
733 | s = SWAP16(s); | |
734 | numtracks = s; | |
735 | ||
736 | // get offset to track blocks | |
737 | fseek(fi, 4, SEEK_CUR); | |
738 | fread(&offset, 1, sizeof(unsigned int), fi); | |
739 | offset = SWAP32(offset); | |
740 | ||
741 | // skip lead-in data | |
742 | while (1) { | |
743 | fseek(fi, offset + 4, SEEK_SET); | |
744 | if (fgetc(fi) < 0xA0) { | |
745 | break; | |
746 | } | |
747 | offset += 0x50; | |
748 | } | |
749 | ||
750 | // check if the image contains mixed subchannel data | |
751 | fseek(fi, offset + 1, SEEK_SET); | |
77160d47 | 752 | subChanMixed = subChanRaw = (fgetc(fi) ? TRUE : FALSE); |
ef79bbde P |
753 | |
754 | // read track data | |
755 | for (i = 1; i <= numtracks; i++) { | |
756 | fseek(fi, offset, SEEK_SET); | |
757 | ||
758 | // get the track type | |
759 | ti[i].type = ((fgetc(fi) == 0xA9) ? CDDA : DATA); | |
760 | fseek(fi, 8, SEEK_CUR); | |
761 | ||
762 | // get the track starting point | |
763 | ti[i].start[0] = fgetc(fi); | |
764 | ti[i].start[1] = fgetc(fi); | |
765 | ti[i].start[2] = fgetc(fi); | |
766 | ||
ef79bbde P |
767 | fread(&extra_offset, 1, sizeof(unsigned int), fi); |
768 | extra_offset = SWAP32(extra_offset); | |
769 | ||
19c03d80 | 770 | // get track start offset (in .mdf) |
771 | fseek(fi, offset + 0x28, SEEK_SET); | |
772 | fread(&l, 1, sizeof(unsigned int), fi); | |
773 | l = SWAP32(l); | |
77160d47 | 774 | ti[i].start_offset = l; |
19c03d80 | 775 | |
8aa91443 | 776 | // get pregap |
777 | fseek(fi, extra_offset, SEEK_SET); | |
778 | fread(&l, 1, sizeof(unsigned int), fi); | |
779 | l = SWAP32(l); | |
780 | if (l != 0 && i > 1) | |
77160d47 | 781 | pregapOffset = msf2sec(ti[i].start); |
8aa91443 | 782 | |
783 | // get the track length | |
784 | fread(&l, 1, sizeof(unsigned int), fi); | |
785 | l = SWAP32(l); | |
786 | sec2msf(l, ti[i].length); | |
787 | ||
ef79bbde P |
788 | offset += 0x50; |
789 | } | |
790 | ||
791 | fclose(fi); | |
792 | return 0; | |
793 | } | |
794 | ||
0877957a | 795 | static int handlepbp(const char *isofile) { |
9a92bffb | 796 | struct { |
797 | unsigned int sig; | |
798 | unsigned int dontcare[8]; | |
799 | unsigned int psar_offs; | |
800 | } pbp_hdr; | |
801 | struct { | |
802 | unsigned char type; | |
803 | unsigned char pad0; | |
804 | unsigned char track; | |
805 | char index0[3]; | |
806 | char pad1; | |
807 | char index1[3]; | |
808 | } toc_entry; | |
809 | struct { | |
810 | unsigned int offset; | |
811 | unsigned int size; | |
812 | unsigned int dontcare[6]; | |
813 | } index_entry; | |
0c2871a7 | 814 | char psar_sig[11]; |
cc376814 | 815 | off_t psisoimg_offs, cdimg_base; |
816 | unsigned int t, cd_length; | |
817 | unsigned int offsettab[8]; | |
9a92bffb | 818 | const char *ext = NULL; |
819 | int i, ret; | |
820 | ||
821 | if (strlen(isofile) >= 4) | |
822 | ext = isofile + strlen(isofile) - 4; | |
823 | if (ext == NULL || (strcmp(ext, ".pbp") != 0 && strcmp(ext, ".PBP") != 0)) | |
824 | return -1; | |
825 | ||
cc376814 | 826 | fseeko(cdHandle, 0, SEEK_SET); |
9726a4af | 827 | |
9a92bffb | 828 | numtracks = 0; |
829 | ||
830 | ret = fread(&pbp_hdr, 1, sizeof(pbp_hdr), cdHandle); | |
831 | if (ret != sizeof(pbp_hdr)) { | |
f29fbd53 | 832 | SysPrintf("failed to read pbp\n"); |
9a92bffb | 833 | goto fail_io; |
834 | } | |
835 | ||
cc376814 | 836 | ret = fseeko(cdHandle, pbp_hdr.psar_offs, SEEK_SET); |
9a92bffb | 837 | if (ret != 0) { |
f29fbd53 | 838 | SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs); |
9a92bffb | 839 | goto fail_io; |
840 | } | |
841 | ||
0c2871a7 | 842 | psisoimg_offs = pbp_hdr.psar_offs; |
9a92bffb | 843 | fread(psar_sig, 1, sizeof(psar_sig), cdHandle); |
0c2871a7 | 844 | psar_sig[10] = 0; |
845 | if (strcmp(psar_sig, "PSTITLEIMG") == 0) { | |
846 | // multidisk image? | |
cc376814 | 847 | ret = fseeko(cdHandle, pbp_hdr.psar_offs + 0x200, SEEK_SET); |
0c2871a7 | 848 | if (ret != 0) { |
f29fbd53 | 849 | SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs + 0x200); |
0c2871a7 | 850 | goto fail_io; |
851 | } | |
852 | ||
853 | if (fread(&offsettab, 1, sizeof(offsettab), cdHandle) != sizeof(offsettab)) { | |
f29fbd53 | 854 | SysPrintf("failed to read offsettab\n"); |
0c2871a7 | 855 | goto fail_io; |
856 | } | |
857 | ||
858 | for (i = 0; i < sizeof(offsettab) / sizeof(offsettab[0]); i++) { | |
859 | if (offsettab[i] == 0) | |
860 | break; | |
861 | } | |
862 | cdrIsoMultidiskCount = i; | |
863 | if (cdrIsoMultidiskCount == 0) { | |
f29fbd53 | 864 | SysPrintf("multidisk eboot has 0 images?\n"); |
0c2871a7 | 865 | goto fail_io; |
866 | } | |
867 | ||
868 | if (cdrIsoMultidiskSelect >= cdrIsoMultidiskCount) | |
869 | cdrIsoMultidiskSelect = 0; | |
870 | ||
871 | psisoimg_offs += offsettab[cdrIsoMultidiskSelect]; | |
872 | ||
cc376814 | 873 | ret = fseeko(cdHandle, psisoimg_offs, SEEK_SET); |
0c2871a7 | 874 | if (ret != 0) { |
cc376814 | 875 | SysPrintf("failed to seek to %llx\n", (long long)psisoimg_offs); |
0c2871a7 | 876 | goto fail_io; |
877 | } | |
878 | ||
879 | fread(psar_sig, 1, sizeof(psar_sig), cdHandle); | |
880 | psar_sig[10] = 0; | |
881 | } | |
882 | ||
883 | if (strcmp(psar_sig, "PSISOIMG00") != 0) { | |
f29fbd53 | 884 | SysPrintf("bad psar_sig: %s\n", psar_sig); |
9a92bffb | 885 | goto fail_io; |
886 | } | |
887 | ||
888 | // seek to TOC | |
cc376814 | 889 | ret = fseeko(cdHandle, psisoimg_offs + 0x800, SEEK_SET); |
9a92bffb | 890 | if (ret != 0) { |
cc376814 | 891 | SysPrintf("failed to seek to %llx\n", (long long)psisoimg_offs + 0x800); |
9a92bffb | 892 | goto fail_io; |
893 | } | |
894 | ||
895 | // first 3 entries are special | |
896 | fseek(cdHandle, sizeof(toc_entry), SEEK_CUR); | |
897 | fread(&toc_entry, 1, sizeof(toc_entry), cdHandle); | |
898 | numtracks = btoi(toc_entry.index1[0]); | |
899 | ||
900 | fread(&toc_entry, 1, sizeof(toc_entry), cdHandle); | |
901 | cd_length = btoi(toc_entry.index1[0]) * 60 * 75 + | |
902 | btoi(toc_entry.index1[1]) * 75 + btoi(toc_entry.index1[2]); | |
903 | ||
904 | for (i = 1; i <= numtracks; i++) { | |
905 | fread(&toc_entry, 1, sizeof(toc_entry), cdHandle); | |
906 | ||
907 | ti[i].type = (toc_entry.type == 1) ? CDDA : DATA; | |
908 | ||
909 | ti[i].start_offset = btoi(toc_entry.index0[0]) * 60 * 75 + | |
910 | btoi(toc_entry.index0[1]) * 75 + btoi(toc_entry.index0[2]); | |
77160d47 | 911 | ti[i].start_offset *= 2352; |
9a92bffb | 912 | ti[i].start[0] = btoi(toc_entry.index1[0]); |
913 | ti[i].start[1] = btoi(toc_entry.index1[1]); | |
914 | ti[i].start[2] = btoi(toc_entry.index1[2]); | |
915 | ||
916 | if (i > 1) { | |
917 | t = msf2sec(ti[i].start) - msf2sec(ti[i - 1].start); | |
918 | sec2msf(t, ti[i - 1].length); | |
919 | } | |
920 | } | |
77160d47 | 921 | t = cd_length - ti[numtracks].start_offset / 2352; |
9a92bffb | 922 | sec2msf(t, ti[numtracks].length); |
923 | ||
924 | // seek to ISO index | |
cc376814 | 925 | ret = fseeko(cdHandle, psisoimg_offs + 0x4000, SEEK_SET); |
9a92bffb | 926 | if (ret != 0) { |
f29fbd53 | 927 | SysPrintf("failed to seek to ISO index\n"); |
9a92bffb | 928 | goto fail_io; |
929 | } | |
930 | ||
931 | compr_img = calloc(1, sizeof(*compr_img)); | |
932 | if (compr_img == NULL) | |
933 | goto fail_io; | |
934 | ||
0877957a | 935 | compr_img->block_shift = 4; |
9a92bffb | 936 | compr_img->current_block = (unsigned int)-1; |
937 | ||
938 | compr_img->index_len = (0x100000 - 0x4000) / sizeof(index_entry); | |
939 | compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0])); | |
940 | if (compr_img->index_table == NULL) | |
941 | goto fail_io; | |
942 | ||
0c2871a7 | 943 | cdimg_base = psisoimg_offs + 0x100000; |
9a92bffb | 944 | for (i = 0; i < compr_img->index_len; i++) { |
945 | ret = fread(&index_entry, 1, sizeof(index_entry), cdHandle); | |
946 | if (ret != sizeof(index_entry)) { | |
f29fbd53 | 947 | SysPrintf("failed to read index_entry #%d\n", i); |
9a92bffb | 948 | goto fail_index; |
949 | } | |
950 | ||
951 | if (index_entry.size == 0) | |
952 | break; | |
953 | ||
954 | compr_img->index_table[i] = cdimg_base + index_entry.offset; | |
955 | } | |
956 | compr_img->index_table[i] = cdimg_base + index_entry.offset + index_entry.size; | |
957 | ||
958 | return 0; | |
959 | ||
960 | fail_index: | |
961 | free(compr_img->index_table); | |
962 | compr_img->index_table = NULL; | |
963 | fail_io: | |
0877957a | 964 | if (compr_img != NULL) { |
965 | free(compr_img); | |
966 | compr_img = NULL; | |
967 | } | |
968 | return -1; | |
969 | } | |
970 | ||
971 | static int handlecbin(const char *isofile) { | |
972 | struct | |
973 | { | |
974 | char magic[4]; | |
975 | unsigned int header_size; | |
976 | unsigned long long total_bytes; | |
977 | unsigned int block_size; | |
978 | unsigned char ver; // 1 | |
979 | unsigned char align; | |
980 | unsigned char rsv_06[2]; | |
981 | } ciso_hdr; | |
982 | const char *ext = NULL; | |
cc376814 | 983 | unsigned int *index_table = NULL; |
0c2871a7 | 984 | unsigned int index = 0, plain; |
0877957a | 985 | int i, ret; |
986 | ||
987 | if (strlen(isofile) >= 5) | |
988 | ext = isofile + strlen(isofile) - 5; | |
989 | if (ext == NULL || (strcasecmp(ext + 1, ".cbn") != 0 && strcasecmp(ext, ".cbin") != 0)) | |
990 | return -1; | |
991 | ||
9726a4af | 992 | fseek(cdHandle, 0, SEEK_SET); |
993 | ||
0877957a | 994 | ret = fread(&ciso_hdr, 1, sizeof(ciso_hdr), cdHandle); |
995 | if (ret != sizeof(ciso_hdr)) { | |
f29fbd53 | 996 | SysPrintf("failed to read ciso header\n"); |
0877957a | 997 | return -1; |
998 | } | |
999 | ||
1000 | if (strncmp(ciso_hdr.magic, "CISO", 4) != 0 || ciso_hdr.total_bytes <= 0 || ciso_hdr.block_size <= 0) { | |
f29fbd53 | 1001 | SysPrintf("bad ciso header\n"); |
0877957a | 1002 | return -1; |
1003 | } | |
1004 | if (ciso_hdr.header_size != 0 && ciso_hdr.header_size != sizeof(ciso_hdr)) { | |
cc376814 | 1005 | ret = fseeko(cdHandle, ciso_hdr.header_size, SEEK_SET); |
0877957a | 1006 | if (ret != 0) { |
f29fbd53 | 1007 | SysPrintf("failed to seek to %x\n", ciso_hdr.header_size); |
0877957a | 1008 | return -1; |
1009 | } | |
1010 | } | |
1011 | ||
1012 | compr_img = calloc(1, sizeof(*compr_img)); | |
1013 | if (compr_img == NULL) | |
1014 | goto fail_io; | |
1015 | ||
1016 | compr_img->block_shift = 0; | |
1017 | compr_img->current_block = (unsigned int)-1; | |
1018 | ||
1019 | compr_img->index_len = ciso_hdr.total_bytes / ciso_hdr.block_size; | |
cc376814 | 1020 | index_table = malloc((compr_img->index_len + 1) * sizeof(index_table[0])); |
1021 | if (index_table == NULL) | |
0877957a | 1022 | goto fail_io; |
1023 | ||
cc376814 | 1024 | ret = fread(index_table, sizeof(index_table[0]), compr_img->index_len, cdHandle); |
0877957a | 1025 | if (ret != compr_img->index_len) { |
f29fbd53 | 1026 | SysPrintf("failed to read index table\n"); |
0877957a | 1027 | goto fail_index; |
1028 | } | |
1029 | ||
cc376814 | 1030 | compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0])); |
1031 | if (compr_img->index_table == NULL) | |
1032 | goto fail_index; | |
1033 | ||
0877957a | 1034 | for (i = 0; i < compr_img->index_len + 1; i++) { |
cc376814 | 1035 | index = index_table[i]; |
0877957a | 1036 | plain = index & 0x80000000; |
1037 | index &= 0x7fffffff; | |
cc376814 | 1038 | compr_img->index_table[i] = (off_t)index << ciso_hdr.align; |
1039 | if (plain) | |
1040 | compr_img->index_table[i] |= OFF_T_MSB; | |
0877957a | 1041 | } |
0877957a | 1042 | |
1043 | return 0; | |
1044 | ||
1045 | fail_index: | |
cc376814 | 1046 | free(index_table); |
0877957a | 1047 | fail_io: |
1048 | if (compr_img != NULL) { | |
1049 | free(compr_img); | |
1050 | compr_img = NULL; | |
1051 | } | |
9a92bffb | 1052 | return -1; |
1053 | } | |
1054 | ||
b0ec2e6d | 1055 | #ifdef HAVE_CHD |
ce188d4d | 1056 | static int handlechd(const char *isofile) { |
1057 | chd_img = calloc(1, sizeof(*chd_img)); | |
1058 | if (chd_img == NULL) | |
1059 | goto fail_io; | |
1060 | ||
1061 | if(chd_open(isofile, CHD_OPEN_READ, NULL, &chd_img->chd) != CHDERR_NONE) | |
1d2757f4 | 1062 | goto fail_io; |
1063 | ||
1064 | if (Config.CHD_Precache && (chd_precache(chd_img->chd) != CHDERR_NONE)) | |
1065 | goto fail_io; | |
ce188d4d | 1066 | |
1067 | chd_img->header = chd_get_header(chd_img->chd); | |
1068 | ||
1069 | chd_img->buffer = malloc(chd_img->header->hunkbytes); | |
1070 | if (chd_img->buffer == NULL) | |
1071 | goto fail_io; | |
1072 | ||
1073 | chd_img->sectors_per_hunk = chd_img->header->hunkbytes / (CD_FRAMESIZE_RAW + SUB_FRAMESIZE); | |
1074 | chd_img->current_hunk = (unsigned int)-1; | |
1075 | ||
1076 | cddaBigEndian = TRUE; | |
1077 | ||
1078 | numtracks = 0; | |
055b81e1 | 1079 | int frame_offset = 0; |
1080 | int file_offset = 0; | |
ce188d4d | 1081 | memset(ti, 0, sizeof(ti)); |
1082 | ||
1083 | while (1) | |
1084 | { | |
1085 | struct { | |
1086 | char type[64]; | |
1087 | char subtype[32]; | |
1088 | char pgtype[32]; | |
1089 | char pgsub[32]; | |
1090 | uint32_t track; | |
1091 | uint32_t frames; | |
1092 | uint32_t pregap; | |
1093 | uint32_t postgap; | |
1094 | } md = {}; | |
1095 | char meta[256]; | |
1096 | uint32_t meta_size = 0; | |
1097 | ||
1098 | if (chd_get_metadata(chd_img->chd, CDROM_TRACK_METADATA2_TAG, numtracks, meta, sizeof(meta), &meta_size, NULL, NULL) == CHDERR_NONE) | |
1099 | sscanf(meta, CDROM_TRACK_METADATA2_FORMAT, &md.track, md.type, md.subtype, &md.frames, &md.pregap, md.pgtype, md.pgsub, &md.postgap); | |
1100 | else if (chd_get_metadata(chd_img->chd, CDROM_TRACK_METADATA_TAG, numtracks, meta, sizeof(meta), &meta_size, NULL, NULL) == CHDERR_NONE) | |
1101 | sscanf(meta, CDROM_TRACK_METADATA_FORMAT, &md.track, md.type, md.subtype, &md.frames); | |
1102 | else | |
1103 | break; | |
1104 | ||
055b81e1 | 1105 | if(md.track == 1) |
1106 | md.pregap = 150; | |
1107 | else | |
1108 | sec2msf(msf2sec(ti[md.track-1].length) + md.pregap, ti[md.track-1].length); | |
ce188d4d | 1109 | |
055b81e1 | 1110 | ti[md.track].type = !strncmp(md.type, "AUDIO", 5) ? CDDA : DATA; |
ce188d4d | 1111 | |
055b81e1 | 1112 | sec2msf(frame_offset + md.pregap, ti[md.track].start); |
1113 | sec2msf(md.frames, ti[md.track].length); | |
ce188d4d | 1114 | |
055b81e1 | 1115 | ti[md.track].start_offset = file_offset; |
ce188d4d | 1116 | |
055b81e1 | 1117 | frame_offset += md.pregap + md.frames + md.postgap; |
1118 | file_offset += md.frames + md.postgap; | |
ce188d4d | 1119 | numtracks++; |
1120 | } | |
1121 | ||
1122 | if (numtracks) | |
1123 | return 0; | |
1124 | ||
1125 | fail_io: | |
1126 | if (chd_img != NULL) { | |
1127 | free(chd_img->buffer); | |
1128 | free(chd_img); | |
1129 | chd_img = NULL; | |
1130 | } | |
1131 | return -1; | |
1132 | } | |
b0ec2e6d | 1133 | #endif |
ce188d4d | 1134 | |
ef79bbde P |
1135 | // this function tries to get the .sub file of the given .img |
1136 | static int opensubfile(const char *isoname) { | |
1137 | char subname[MAXPATHLEN]; | |
1138 | ||
1139 | // copy name of the iso and change extension from .img to .sub | |
1140 | strncpy(subname, isoname, sizeof(subname)); | |
1141 | subname[MAXPATHLEN - 1] = '\0'; | |
1142 | if (strlen(subname) >= 4) { | |
1143 | strcpy(subname + strlen(subname) - 4, ".sub"); | |
1144 | } | |
1145 | else { | |
1146 | return -1; | |
1147 | } | |
1148 | ||
270c6dd1 | 1149 | subHandle = fopen_utf8(subname, "rb"); |
ef79bbde P |
1150 | if (subHandle == NULL) { |
1151 | return -1; | |
1152 | } | |
1153 | ||
1154 | return 0; | |
1155 | } | |
1156 | ||
ae4e7dc9 | 1157 | static int opensbifile(const char *isoname) { |
ba1cce07 | 1158 | char sbiname[MAXPATHLEN], disknum[MAXPATHLEN] = "0"; |
ae4e7dc9 | 1159 | int s; |
1160 | ||
1161 | strncpy(sbiname, isoname, sizeof(sbiname)); | |
1162 | sbiname[MAXPATHLEN - 1] = '\0'; | |
1163 | if (strlen(sbiname) >= 4) { | |
ba1cce07 S |
1164 | if (cdrIsoMultidiskCount > 1) { |
1165 | sprintf(disknum, "_%i.sbi", cdrIsoMultidiskSelect + 1); | |
1166 | strcpy(sbiname + strlen(sbiname) - 4, disknum); | |
1167 | } | |
1168 | else | |
1169 | strcpy(sbiname + strlen(sbiname) - 4, ".sbi"); | |
ae4e7dc9 | 1170 | } |
1171 | else { | |
1172 | return -1; | |
1173 | } | |
1174 | ||
1175 | fseek(cdHandle, 0, SEEK_END); | |
1176 | s = ftell(cdHandle) / 2352; | |
1177 | ||
1178 | return LoadSBI(sbiname, s); | |
1179 | } | |
1180 | ||
679b71e2 JW |
1181 | #ifdef _WIN32 |
1182 | static void readThreadStop() {} | |
1183 | static void readThreadStart() {} | |
1184 | #else | |
1185 | static pthread_t read_thread_id; | |
1186 | ||
1187 | static pthread_cond_t read_thread_msg_avail; | |
1188 | static pthread_cond_t read_thread_msg_done; | |
1189 | static pthread_mutex_t read_thread_msg_lock; | |
1190 | ||
1191 | static pthread_cond_t sectorbuffer_cond; | |
1192 | static pthread_mutex_t sectorbuffer_lock; | |
1193 | ||
1194 | static boolean read_thread_running = FALSE; | |
1195 | static int read_thread_sector_start = -1; | |
1196 | static int read_thread_sector_end = -1; | |
1197 | ||
1198 | typedef struct { | |
1199 | int sector; | |
1200 | long ret; | |
1201 | unsigned char data[CD_FRAMESIZE_RAW]; | |
1202 | } SectorBufferEntry; | |
1203 | ||
1204 | #define SECTOR_BUFFER_SIZE 4096 | |
1205 | ||
1206 | static SectorBufferEntry *sectorbuffer; | |
1207 | static size_t sectorbuffer_index; | |
1208 | ||
1209 | int (*sync_cdimg_read_func)(FILE *f, unsigned int base, void *dest, int sector); | |
1210 | unsigned char *(*sync_CDR_getBuffer)(void); | |
1211 | ||
1212 | static unsigned char * CALLBACK ISOgetBuffer_async(void); | |
1213 | static int cdread_async(FILE *f, unsigned int base, void *dest, int sector); | |
1214 | ||
1215 | static void *readThreadMain(void *param) { | |
1216 | int max_sector = -1; | |
1217 | int requested_sector_start = -1; | |
1218 | int requested_sector_end = -1; | |
1219 | int last_read_sector = -1; | |
1220 | int index = 0; | |
1221 | ||
1222 | int ra_sector = -1; | |
1223 | int max_ra = 128; | |
1224 | int initial_ra = 1; | |
1225 | int speedmult_ra = 4; | |
1226 | ||
1227 | int ra_count = 0; | |
1228 | int how_far_ahead = 0; | |
1229 | ||
1230 | unsigned char tmpdata[CD_FRAMESIZE_RAW]; | |
1231 | long ret; | |
1232 | ||
1233 | max_sector = msf2sec(ti[numtracks].start) + msf2sec(ti[numtracks].length); | |
1234 | ||
1235 | while(1) { | |
1236 | pthread_mutex_lock(&read_thread_msg_lock); | |
1237 | ||
1238 | // If we don't have readahead and we don't have a sector request, wait for one. | |
1239 | // If we still have readahead to go, don't block, just keep going. | |
1240 | // And if we ever have a sector request pending, acknowledge and reset it. | |
1241 | ||
1242 | if (!ra_count) { | |
1243 | if (read_thread_sector_start == -1 && read_thread_running) { | |
1244 | pthread_cond_wait(&read_thread_msg_avail, &read_thread_msg_lock); | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | if (read_thread_sector_start != -1) { | |
1249 | requested_sector_start = read_thread_sector_start; | |
1250 | requested_sector_end = read_thread_sector_end; | |
1251 | read_thread_sector_start = -1; | |
1252 | read_thread_sector_end = -1; | |
1253 | pthread_cond_signal(&read_thread_msg_done); | |
1254 | } | |
1255 | ||
1256 | pthread_mutex_unlock(&read_thread_msg_lock); | |
1257 | ||
1258 | if (!read_thread_running) | |
1259 | break; | |
1260 | ||
1261 | // Readahead code, based on the implementation in mednafen psx's cdromif.cpp | |
1262 | if (requested_sector_start != -1) { | |
1263 | if (last_read_sector != -1 && last_read_sector == (requested_sector_start - 1)) { | |
1264 | how_far_ahead = ra_sector - requested_sector_end; | |
1265 | ||
1266 | if(how_far_ahead <= max_ra) | |
1267 | ra_count = (max_ra - how_far_ahead + 1 ? max_ra - how_far_ahead + 1 : speedmult_ra); | |
1268 | else | |
1269 | ra_count++; | |
1270 | } else if (requested_sector_end != last_read_sector) { | |
1271 | ra_sector = requested_sector_end; | |
1272 | ra_count = initial_ra; | |
1273 | } | |
1274 | ||
1275 | last_read_sector = requested_sector_end; | |
1276 | } | |
1277 | ||
00e69290 JW |
1278 | index = ra_sector % SECTOR_BUFFER_SIZE; |
1279 | ||
679b71e2 JW |
1280 | // check for end of CD |
1281 | if (ra_count && ra_sector >= max_sector) { | |
1282 | ra_count = 0; | |
00e69290 JW |
1283 | pthread_mutex_lock(§orbuffer_lock); |
1284 | sectorbuffer[index].ret = -1; | |
1285 | sectorbuffer[index].sector = ra_sector; | |
1286 | pthread_cond_signal(§orbuffer_cond); | |
1287 | pthread_mutex_unlock(§orbuffer_lock); | |
679b71e2 JW |
1288 | } |
1289 | ||
1290 | if (ra_count) { | |
679b71e2 JW |
1291 | pthread_mutex_lock(§orbuffer_lock); |
1292 | if (sectorbuffer[index].sector != ra_sector) { | |
1293 | pthread_mutex_unlock(§orbuffer_lock); | |
1294 | ||
1295 | ret = sync_cdimg_read_func(cdHandle, 0, tmpdata, ra_sector); | |
1296 | ||
1297 | pthread_mutex_lock(§orbuffer_lock); | |
1298 | sectorbuffer[index].ret = ret; | |
1299 | sectorbuffer[index].sector = ra_sector; | |
1300 | memcpy(sectorbuffer[index].data, tmpdata, CD_FRAMESIZE_RAW); | |
1301 | } | |
1302 | pthread_cond_signal(§orbuffer_cond); | |
1303 | pthread_mutex_unlock(§orbuffer_lock); | |
1304 | ||
1305 | ra_sector++; | |
1306 | ra_count--; | |
1307 | } | |
1308 | } | |
1309 | ||
1310 | return NULL; | |
1311 | } | |
1312 | ||
1313 | static void readThreadStop() { | |
1314 | if (read_thread_running == TRUE) { | |
1315 | read_thread_running = FALSE; | |
1316 | pthread_cond_signal(&read_thread_msg_avail); | |
1317 | pthread_join(read_thread_id, NULL); | |
1318 | } | |
1319 | ||
1320 | pthread_cond_destroy(&read_thread_msg_done); | |
1321 | pthread_cond_destroy(&read_thread_msg_avail); | |
1322 | pthread_mutex_destroy(&read_thread_msg_lock); | |
1323 | ||
1324 | pthread_cond_destroy(§orbuffer_cond); | |
1325 | pthread_mutex_destroy(§orbuffer_lock); | |
1326 | ||
1327 | CDR_getBuffer = sync_CDR_getBuffer; | |
1328 | cdimg_read_func = sync_cdimg_read_func; | |
1329 | ||
1330 | free(sectorbuffer); | |
1331 | sectorbuffer = NULL; | |
1332 | } | |
1333 | ||
1334 | static void readThreadStart() { | |
1335 | SysPrintf("Starting async CD thread\n"); | |
1336 | ||
1337 | if (read_thread_running == TRUE) | |
1338 | return; | |
1339 | ||
1340 | read_thread_running = TRUE; | |
1341 | read_thread_sector_start = -1; | |
1342 | read_thread_sector_end = -1; | |
1343 | sectorbuffer_index = 0; | |
1344 | ||
1345 | sectorbuffer = calloc(SECTOR_BUFFER_SIZE, sizeof(SectorBufferEntry)); | |
1346 | if(!sectorbuffer) | |
1347 | goto error; | |
1348 | ||
1349 | sectorbuffer[0].sector = -1; // Otherwise we might think we've already fetched sector 0! | |
1350 | ||
1351 | sync_CDR_getBuffer = CDR_getBuffer; | |
1352 | CDR_getBuffer = ISOgetBuffer_async; | |
1353 | sync_cdimg_read_func = cdimg_read_func; | |
1354 | cdimg_read_func = cdread_async; | |
1355 | ||
1356 | if (pthread_cond_init(&read_thread_msg_avail, NULL) || | |
1357 | pthread_cond_init(&read_thread_msg_done, NULL) || | |
1358 | pthread_mutex_init(&read_thread_msg_lock, NULL) || | |
1359 | pthread_cond_init(§orbuffer_cond, NULL) || | |
1360 | pthread_mutex_init(§orbuffer_lock, NULL) || | |
1361 | pthread_create(&read_thread_id, NULL, readThreadMain, NULL)) | |
1362 | goto error; | |
1363 | ||
1364 | return; | |
1365 | ||
1366 | error: | |
1367 | SysPrintf("Error starting async CD thread\n"); | |
1368 | SysPrintf("Falling back to sync\n"); | |
1369 | ||
1370 | readThreadStop(); | |
1371 | } | |
1372 | #endif | |
1373 | ||
77160d47 | 1374 | static int cdread_normal(FILE *f, unsigned int base, void *dest, int sector) |
9a92bffb | 1375 | { |
77160d47 | 1376 | fseek(f, base + sector * CD_FRAMESIZE_RAW, SEEK_SET); |
81abe4d5 | 1377 | return fread(dest, 1, CD_FRAMESIZE_RAW, f); |
9a92bffb | 1378 | } |
1379 | ||
77160d47 | 1380 | static int cdread_sub_mixed(FILE *f, unsigned int base, void *dest, int sector) |
9a92bffb | 1381 | { |
1382 | int ret; | |
1383 | ||
77160d47 | 1384 | fseek(f, base + sector * (CD_FRAMESIZE_RAW + SUB_FRAMESIZE), SEEK_SET); |
81abe4d5 | 1385 | ret = fread(dest, 1, CD_FRAMESIZE_RAW, f); |
9a92bffb | 1386 | fread(subbuffer, 1, SUB_FRAMESIZE, f); |
1387 | ||
1388 | if (subChanRaw) DecodeRawSubData(); | |
1389 | ||
1390 | return ret; | |
1391 | } | |
1392 | ||
2dd6a826 | 1393 | static int uncompress2_pcsx(void *out, unsigned long *out_size, void *in, unsigned long in_size) |
9a92bffb | 1394 | { |
1395 | static z_stream z; | |
1396 | int ret = 0; | |
1397 | ||
1398 | if (z.zalloc == NULL) { | |
1399 | // XXX: one-time leak here.. | |
1400 | z.next_in = Z_NULL; | |
1401 | z.avail_in = 0; | |
1402 | z.zalloc = Z_NULL; | |
1403 | z.zfree = Z_NULL; | |
1404 | z.opaque = Z_NULL; | |
1405 | ret = inflateInit2(&z, -15); | |
1406 | } | |
1407 | else | |
1408 | ret = inflateReset(&z); | |
1409 | if (ret != Z_OK) | |
1410 | return ret; | |
1411 | ||
1412 | z.next_in = in; | |
1413 | z.avail_in = in_size; | |
1414 | z.next_out = out; | |
1415 | z.avail_out = *out_size; | |
1416 | ||
1417 | ret = inflate(&z, Z_NO_FLUSH); | |
1418 | //inflateEnd(&z); | |
1419 | ||
1420 | *out_size -= z.avail_out; | |
1421 | return ret == 1 ? 0 : ret; | |
1422 | } | |
1423 | ||
77160d47 | 1424 | static int cdread_compressed(FILE *f, unsigned int base, void *dest, int sector) |
9a92bffb | 1425 | { |
0877957a | 1426 | unsigned long cdbuffer_size, cdbuffer_size_expect; |
cc376814 | 1427 | unsigned int size; |
0877957a | 1428 | int is_compressed; |
cc376814 | 1429 | off_t start_byte; |
9a92bffb | 1430 | int ret, block; |
1431 | ||
77160d47 | 1432 | if (base) |
1433 | sector += base / 2352; | |
1434 | ||
0877957a | 1435 | block = sector >> compr_img->block_shift; |
1436 | compr_img->sector_in_blk = sector & ((1 << compr_img->block_shift) - 1); | |
9a92bffb | 1437 | |
1438 | if (block == compr_img->current_block) { | |
1439 | //printf("hit sect %d\n", sector); | |
1440 | goto finish; | |
1441 | } | |
1442 | ||
1443 | if (sector >= compr_img->index_len * 16) { | |
f29fbd53 | 1444 | SysPrintf("sector %d is past img end\n", sector); |
9a92bffb | 1445 | return -1; |
1446 | } | |
1447 | ||
cc376814 | 1448 | start_byte = compr_img->index_table[block] & ~OFF_T_MSB; |
1449 | if (fseeko(cdHandle, start_byte, SEEK_SET) != 0) { | |
1450 | SysPrintf("seek error for block %d at %llx: ", | |
1451 | block, (long long)start_byte); | |
9a92bffb | 1452 | perror(NULL); |
1453 | return -1; | |
1454 | } | |
1455 | ||
cc376814 | 1456 | is_compressed = !(compr_img->index_table[block] & OFF_T_MSB); |
1457 | size = (compr_img->index_table[block + 1] & ~OFF_T_MSB) - start_byte; | |
9a92bffb | 1458 | if (size > sizeof(compr_img->buff_compressed)) { |
f29fbd53 | 1459 | SysPrintf("block %d is too large: %u\n", block, size); |
9a92bffb | 1460 | return -1; |
1461 | } | |
1462 | ||
0877957a | 1463 | if (fread(is_compressed ? compr_img->buff_compressed : compr_img->buff_raw[0], |
1464 | 1, size, cdHandle) != size) { | |
f29fbd53 | 1465 | SysPrintf("read error for block %d at %x: ", block, start_byte); |
9a92bffb | 1466 | perror(NULL); |
1467 | return -1; | |
1468 | } | |
1469 | ||
0877957a | 1470 | if (is_compressed) { |
1471 | cdbuffer_size_expect = sizeof(compr_img->buff_raw[0]) << compr_img->block_shift; | |
1472 | cdbuffer_size = cdbuffer_size_expect; | |
2dd6a826 | 1473 | ret = uncompress2_pcsx(compr_img->buff_raw[0], &cdbuffer_size, compr_img->buff_compressed, size); |
0877957a | 1474 | if (ret != 0) { |
f29fbd53 | 1475 | SysPrintf("uncompress failed with %d for block %d, sector %d\n", |
0877957a | 1476 | ret, block, sector); |
1477 | return -1; | |
1478 | } | |
1479 | if (cdbuffer_size != cdbuffer_size_expect) | |
f29fbd53 | 1480 | SysPrintf("cdbuffer_size: %lu != %lu, sector %d\n", cdbuffer_size, |
0877957a | 1481 | cdbuffer_size_expect, sector); |
9a92bffb | 1482 | } |
9a92bffb | 1483 | |
1484 | // done at last! | |
1485 | compr_img->current_block = block; | |
1486 | ||
1487 | finish: | |
1488 | if (dest != cdbuffer) // copy avoid HACK | |
81abe4d5 | 1489 | memcpy(dest, compr_img->buff_raw[compr_img->sector_in_blk], |
1490 | CD_FRAMESIZE_RAW); | |
1491 | return CD_FRAMESIZE_RAW; | |
9a92bffb | 1492 | } |
1493 | ||
b0ec2e6d | 1494 | #ifdef HAVE_CHD |
ce188d4d | 1495 | static int cdread_chd(FILE *f, unsigned int base, void *dest, int sector) |
1496 | { | |
1497 | int hunk; | |
1498 | ||
1499 | if (base) | |
055b81e1 | 1500 | sector += base; |
ce188d4d | 1501 | |
1502 | hunk = sector / chd_img->sectors_per_hunk; | |
1503 | chd_img->sector_in_hunk = sector % chd_img->sectors_per_hunk; | |
1504 | ||
055b81e1 | 1505 | if (hunk != chd_img->current_hunk) |
1506 | { | |
1507 | chd_read(chd_img->chd, hunk, chd_img->buffer); | |
1508 | chd_img->current_hunk = hunk; | |
1509 | } | |
ce188d4d | 1510 | |
ce188d4d | 1511 | if (dest != cdbuffer) // copy avoid HACK |
1512 | memcpy(dest, chd_img->buffer[chd_img->sector_in_hunk], | |
1513 | CD_FRAMESIZE_RAW); | |
1514 | return CD_FRAMESIZE_RAW; | |
1515 | } | |
b0ec2e6d | 1516 | #endif |
77160d47 | 1517 | static int cdread_2048(FILE *f, unsigned int base, void *dest, int sector) |
9a92bffb | 1518 | { |
1519 | int ret; | |
1520 | ||
77160d47 | 1521 | fseek(f, base + sector * 2048, SEEK_SET); |
81abe4d5 | 1522 | ret = fread((char *)dest + 12 * 2, 1, 2048, f); |
9a92bffb | 1523 | |
1524 | // not really necessary, fake mode 2 header | |
81abe4d5 | 1525 | memset(cdbuffer, 0, 12 * 2); |
1526 | sec2msf(sector + 2 * 75, (char *)&cdbuffer[12]); | |
1527 | cdbuffer[12 + 3] = 1; | |
9a92bffb | 1528 | |
1529 | return ret; | |
1530 | } | |
1531 | ||
679b71e2 JW |
1532 | #ifndef _WIN32 |
1533 | ||
1534 | static int cdread_async(FILE *f, unsigned int base, void *dest, int sector) { | |
1535 | boolean found = FALSE; | |
1536 | int i = sector % SECTOR_BUFFER_SIZE; | |
1537 | long ret; | |
1538 | ||
1539 | if (f != cdHandle || base != 0 || dest != cdbuffer) { | |
1540 | // Async reads are only supported for cdbuffer, so call the sync | |
1541 | // function directly. | |
1542 | return sync_cdimg_read_func(f, base, dest, sector); | |
1543 | } | |
1544 | ||
1545 | pthread_mutex_lock(&read_thread_msg_lock); | |
1546 | ||
1547 | // Only wait if we're not trying to read the next sector and | |
1548 | // sector_start is set (meaning the last request hasn't been | |
1549 | // processed yet) | |
1550 | while(read_thread_sector_start != -1 && read_thread_sector_end + 1 != sector) { | |
1551 | pthread_cond_wait(&read_thread_msg_done, &read_thread_msg_lock); | |
1552 | } | |
1553 | ||
1554 | if (read_thread_sector_start == -1) | |
1555 | read_thread_sector_start = sector; | |
1556 | ||
1557 | read_thread_sector_end = sector; | |
1558 | pthread_cond_signal(&read_thread_msg_avail); | |
1559 | pthread_mutex_unlock(&read_thread_msg_lock); | |
1560 | ||
1561 | do { | |
1562 | pthread_mutex_lock(§orbuffer_lock); | |
1563 | if (sectorbuffer[i].sector == sector) { | |
1564 | sectorbuffer_index = i; | |
1565 | ret = sectorbuffer[i].ret; | |
1566 | found = TRUE; | |
1567 | } | |
1568 | ||
1569 | if (!found) { | |
1570 | pthread_cond_wait(§orbuffer_cond, §orbuffer_lock); | |
1571 | } | |
1572 | pthread_mutex_unlock(§orbuffer_lock); | |
1573 | } while (!found); | |
1574 | ||
1575 | return ret; | |
1576 | } | |
1577 | ||
1578 | #endif | |
1579 | ||
9a92bffb | 1580 | static unsigned char * CALLBACK ISOgetBuffer_compr(void) { |
1581 | return compr_img->buff_raw[compr_img->sector_in_blk] + 12; | |
1582 | } | |
1583 | ||
b0ec2e6d | 1584 | #ifdef HAVE_CHD |
ce188d4d | 1585 | static unsigned char * CALLBACK ISOgetBuffer_chd(void) { |
1586 | return chd_img->buffer[chd_img->sector_in_hunk] + 12; | |
1587 | } | |
b0ec2e6d | 1588 | #endif |
ce188d4d | 1589 | |
679b71e2 JW |
1590 | #ifndef _WIN32 |
1591 | static unsigned char * CALLBACK ISOgetBuffer_async(void) { | |
1592 | unsigned char *buffer; | |
1593 | pthread_mutex_lock(§orbuffer_lock); | |
1594 | buffer = sectorbuffer[sectorbuffer_index].data; | |
1595 | pthread_mutex_unlock(§orbuffer_lock); | |
1596 | return buffer + 12; | |
1597 | } | |
1598 | ||
1599 | #endif | |
1600 | ||
9a92bffb | 1601 | static unsigned char * CALLBACK ISOgetBuffer(void) { |
81abe4d5 | 1602 | return cdbuffer + 12; |
9a92bffb | 1603 | } |
1604 | ||
ef79bbde P |
1605 | static void PrintTracks(void) { |
1606 | int i; | |
1607 | ||
1608 | for (i = 1; i <= numtracks; i++) { | |
1609 | SysPrintf(_("Track %.2d (%s) - Start %.2d:%.2d:%.2d, Length %.2d:%.2d:%.2d\n"), | |
1610 | i, (ti[i].type == DATA ? "DATA" : "AUDIO"), | |
1611 | ti[i].start[0], ti[i].start[1], ti[i].start[2], | |
1612 | ti[i].length[0], ti[i].length[1], ti[i].length[2]); | |
1613 | } | |
1614 | } | |
1615 | ||
1616 | // This function is invoked by the front-end when opening an ISO | |
1617 | // file for playback | |
1618 | static long CALLBACK ISOopen(void) { | |
9a92bffb | 1619 | boolean isMode1ISO = FALSE; |
689e0447 | 1620 | char alt_bin_filename[MAXPATHLEN]; |
1621 | const char *bin_filename; | |
208177a7 | 1622 | char image_str[1024] = {0}; |
9a92bffb | 1623 | |
ef79bbde P |
1624 | if (cdHandle != NULL) { |
1625 | return 0; // it's already open | |
1626 | } | |
1627 | ||
270c6dd1 | 1628 | cdHandle = fopen_utf8(GetIsoFile(), "rb"); |
ef79bbde | 1629 | if (cdHandle == NULL) { |
2e058581 | 1630 | SysPrintf(_("Could't open '%s' for reading: %s\n"), |
1631 | GetIsoFile(), strerror(errno)); | |
ef79bbde P |
1632 | return -1; |
1633 | } | |
1634 | ||
208177a7 | 1635 | sprintf(image_str, "Loaded CD Image: %s", GetIsoFile()); |
ef79bbde P |
1636 | |
1637 | cddaBigEndian = FALSE; | |
1638 | subChanMixed = FALSE; | |
1639 | subChanRaw = FALSE; | |
8aa91443 | 1640 | pregapOffset = 0; |
0c2871a7 | 1641 | cdrIsoMultidiskCount = 1; |
deebc67f | 1642 | multifile = 0; |
ef79bbde | 1643 | |
9a92bffb | 1644 | CDR_getBuffer = ISOgetBuffer; |
1645 | cdimg_read_func = cdread_normal; | |
1646 | ||
92f43ab0 | 1647 | if (parsetoc(GetIsoFile()) == 0) { |
208177a7 | 1648 | strcat(image_str, "[+toc]"); |
ef79bbde P |
1649 | } |
1650 | else if (parseccd(GetIsoFile()) == 0) { | |
208177a7 | 1651 | strcat(image_str, "[+ccd]"); |
ef79bbde P |
1652 | } |
1653 | else if (parsemds(GetIsoFile()) == 0) { | |
208177a7 | 1654 | strcat(image_str, "[+mds]"); |
ef79bbde | 1655 | } |
92f43ab0 | 1656 | else if (parsecue(GetIsoFile()) == 0) { |
208177a7 | 1657 | strcat(image_str, "[+cue]"); |
92f43ab0 | 1658 | } |
0877957a | 1659 | if (handlepbp(GetIsoFile()) == 0) { |
208177a7 | 1660 | strcat(image_str, "[+pbp]"); |
9a92bffb | 1661 | CDR_getBuffer = ISOgetBuffer_compr; |
1662 | cdimg_read_func = cdread_compressed; | |
1663 | } | |
0877957a | 1664 | else if (handlecbin(GetIsoFile()) == 0) { |
208177a7 | 1665 | strcat(image_str, "[+cbin]"); |
0877957a | 1666 | CDR_getBuffer = ISOgetBuffer_compr; |
1667 | cdimg_read_func = cdread_compressed; | |
1668 | } | |
b0ec2e6d | 1669 | #ifdef HAVE_CHD |
ce188d4d | 1670 | else if (handlechd(GetIsoFile()) == 0) { |
208177a7 | 1671 | strcat(image_str, "[+chd]"); |
ce188d4d | 1672 | CDR_getBuffer = ISOgetBuffer_chd; |
1673 | cdimg_read_func = cdread_chd; | |
1674 | } | |
b0ec2e6d | 1675 | #endif |
ef79bbde P |
1676 | |
1677 | if (!subChanMixed && opensubfile(GetIsoFile()) == 0) { | |
208177a7 | 1678 | strcat(image_str, "[+sub]"); |
ef79bbde | 1679 | } |
ae4e7dc9 | 1680 | if (opensbifile(GetIsoFile()) == 0) { |
208177a7 | 1681 | strcat(image_str, "[+sbi]"); |
ae4e7dc9 | 1682 | } |
ef79bbde | 1683 | |
cc376814 | 1684 | fseeko(cdHandle, 0, SEEK_END); |
c9e5423e | 1685 | |
1686 | // maybe user selected metadata file instead of main .bin .. | |
689e0447 | 1687 | bin_filename = GetIsoFile(); |
cc376814 | 1688 | if (ftello(cdHandle) < 2352 * 0x10) { |
c9e5423e | 1689 | static const char *exts[] = { ".bin", ".BIN", ".img", ".IMG" }; |
689e0447 | 1690 | FILE *tmpf = NULL; |
c9e5423e | 1691 | size_t i; |
689e0447 | 1692 | char *p; |
c9e5423e | 1693 | |
689e0447 | 1694 | strncpy(alt_bin_filename, bin_filename, sizeof(alt_bin_filename)); |
1695 | alt_bin_filename[MAXPATHLEN - 1] = '\0'; | |
1696 | if (strlen(alt_bin_filename) >= 4) { | |
1697 | p = alt_bin_filename + strlen(alt_bin_filename) - 4; | |
c9e5423e | 1698 | for (i = 0; i < sizeof(exts) / sizeof(exts[0]); i++) { |
1699 | strcpy(p, exts[i]); | |
270c6dd1 | 1700 | tmpf = fopen_utf8(alt_bin_filename, "rb"); |
689e0447 | 1701 | if (tmpf != NULL) |
c9e5423e | 1702 | break; |
c9e5423e | 1703 | } |
1704 | } | |
689e0447 | 1705 | if (tmpf != NULL) { |
1706 | bin_filename = alt_bin_filename; | |
1707 | fclose(cdHandle); | |
1708 | cdHandle = tmpf; | |
cc376814 | 1709 | fseeko(cdHandle, 0, SEEK_END); |
689e0447 | 1710 | } |
c9e5423e | 1711 | } |
1712 | ||
1713 | // guess whether it is mode1/2048 | |
cc376814 | 1714 | if (ftello(cdHandle) % 2048 == 0) { |
9a92bffb | 1715 | unsigned int modeTest = 0; |
1716 | fseek(cdHandle, 0, SEEK_SET); | |
1717 | fread(&modeTest, 4, 1, cdHandle); | |
1718 | if (SWAP32(modeTest) != 0xffffff00) { | |
208177a7 | 1719 | strcat(image_str, "[2048]"); |
9a92bffb | 1720 | isMode1ISO = TRUE; |
1721 | } | |
1722 | } | |
1723 | fseek(cdHandle, 0, SEEK_SET); | |
1724 | ||
208177a7 | 1725 | SysPrintf("%s.\n", image_str); |
ef79bbde P |
1726 | |
1727 | PrintTracks(); | |
1728 | ||
9a92bffb | 1729 | if (subChanMixed) |
1730 | cdimg_read_func = cdread_sub_mixed; | |
1731 | else if (isMode1ISO) | |
1732 | cdimg_read_func = cdread_2048; | |
1733 | ||
38b8102c | 1734 | // make sure we have another handle open for cdda |
1735 | if (numtracks > 1 && ti[1].handle == NULL) { | |
270c6dd1 | 1736 | ti[1].handle = fopen_utf8(bin_filename, "rb"); |
38b8102c | 1737 | } |
77160d47 | 1738 | cdda_cur_sector = 0; |
1739 | cdda_file_offset = 0; | |
38b8102c | 1740 | |
679b71e2 JW |
1741 | if (Config.AsyncCD) { |
1742 | readThreadStart(); | |
1743 | } | |
ef79bbde P |
1744 | return 0; |
1745 | } | |
1746 | ||
1747 | static long CALLBACK ISOclose(void) { | |
38b8102c | 1748 | int i; |
1749 | ||
ef79bbde P |
1750 | if (cdHandle != NULL) { |
1751 | fclose(cdHandle); | |
1752 | cdHandle = NULL; | |
1753 | } | |
1754 | if (subHandle != NULL) { | |
1755 | fclose(subHandle); | |
1756 | subHandle = NULL; | |
1757 | } | |
1758 | stopCDDA(); | |
38b8102c | 1759 | cddaHandle = NULL; |
1760 | ||
9a92bffb | 1761 | if (compr_img != NULL) { |
1762 | free(compr_img->index_table); | |
1763 | free(compr_img); | |
1764 | compr_img = NULL; | |
1765 | } | |
1766 | ||
b0ec2e6d | 1767 | #ifdef HAVE_CHD |
ce188d4d | 1768 | if (chd_img != NULL) { |
1769 | chd_close(chd_img->chd); | |
1770 | free(chd_img->buffer); | |
1771 | free(chd_img); | |
1772 | chd_img = NULL; | |
1773 | } | |
b0ec2e6d | 1774 | #endif |
ce188d4d | 1775 | |
38b8102c | 1776 | for (i = 1; i <= numtracks; i++) { |
1777 | if (ti[i].handle != NULL) { | |
1778 | fclose(ti[i].handle); | |
1779 | ti[i].handle = NULL; | |
1780 | } | |
1781 | } | |
1782 | numtracks = 0; | |
c0ee7ac6 | 1783 | ti[1].type = 0; |
ae4e7dc9 | 1784 | UnloadSBI(); |
38b8102c | 1785 | |
c0ee7ac6 | 1786 | memset(cdbuffer, 0, sizeof(cdbuffer)); |
1787 | CDR_getBuffer = ISOgetBuffer; | |
1788 | ||
49942a16 | 1789 | if (Config.AsyncCD) { |
1790 | readThreadStop(); | |
1791 | } | |
1792 | ||
38b8102c | 1793 | return 0; |
1794 | } | |
1795 | ||
1796 | static long CALLBACK ISOinit(void) { | |
1797 | assert(cdHandle == NULL); | |
1798 | assert(subHandle == NULL); | |
1799 | ||
1800 | return 0; // do nothing | |
1801 | } | |
1802 | ||
1803 | static long CALLBACK ISOshutdown(void) { | |
1804 | ISOclose(); | |
ef79bbde P |
1805 | return 0; |
1806 | } | |
1807 | ||
1808 | // return Starting and Ending Track | |
1809 | // buffer: | |
1810 | // byte 0 - start track | |
1811 | // byte 1 - end track | |
1812 | static long CALLBACK ISOgetTN(unsigned char *buffer) { | |
1813 | buffer[0] = 1; | |
1814 | ||
1815 | if (numtracks > 0) { | |
1816 | buffer[1] = numtracks; | |
1817 | } | |
1818 | else { | |
1819 | buffer[1] = 1; | |
1820 | } | |
1821 | ||
1822 | return 0; | |
1823 | } | |
1824 | ||
1825 | // return Track Time | |
1826 | // buffer: | |
1827 | // byte 0 - frame | |
1828 | // byte 1 - second | |
1829 | // byte 2 - minute | |
1830 | static long CALLBACK ISOgetTD(unsigned char track, unsigned char *buffer) { | |
858ad511 | 1831 | if (track == 0) { |
858ad511 | 1832 | unsigned int sect; |
1833 | unsigned char time[3]; | |
1834 | sect = msf2sec(ti[numtracks].start) + msf2sec(ti[numtracks].length); | |
ab948f7e | 1835 | sec2msf(sect, (char *)time); |
858ad511 | 1836 | buffer[2] = time[0]; |
1837 | buffer[1] = time[1]; | |
1838 | buffer[0] = time[2]; | |
1839 | } | |
1840 | else if (numtracks > 0 && track <= numtracks) { | |
ef79bbde P |
1841 | buffer[2] = ti[track].start[0]; |
1842 | buffer[1] = ti[track].start[1]; | |
1843 | buffer[0] = ti[track].start[2]; | |
1844 | } | |
1845 | else { | |
1846 | buffer[2] = 0; | |
1847 | buffer[1] = 2; | |
1848 | buffer[0] = 0; | |
1849 | } | |
1850 | ||
1851 | return 0; | |
1852 | } | |
1853 | ||
1854 | // decode 'raw' subchannel data ripped by cdrdao | |
1855 | static void DecodeRawSubData(void) { | |
1856 | unsigned char subQData[12]; | |
1857 | int i; | |
1858 | ||
1859 | memset(subQData, 0, sizeof(subQData)); | |
1860 | ||
1861 | for (i = 0; i < 8 * 12; i++) { | |
1862 | if (subbuffer[i] & (1 << 6)) { // only subchannel Q is needed | |
1863 | subQData[i >> 3] |= (1 << (7 - (i & 7))); | |
1864 | } | |
1865 | } | |
1866 | ||
1867 | memcpy(&subbuffer[12], subQData, 12); | |
1868 | } | |
1869 | ||
1870 | // read track | |
1871 | // time: byte 0 - minute; byte 1 - second; byte 2 - frame | |
1872 | // uses bcd format | |
1873 | static long CALLBACK ISOreadTrack(unsigned char *time) { | |
19c03d80 | 1874 | int sector = MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2])); |
c0ee7ac6 | 1875 | long ret; |
19c03d80 | 1876 | |
ef79bbde P |
1877 | if (cdHandle == NULL) { |
1878 | return -1; | |
1879 | } | |
1880 | ||
8aa91443 | 1881 | if (pregapOffset) { |
1882 | subChanMissing = FALSE; | |
1883 | if (sector >= pregapOffset) { | |
1884 | sector -= 2 * 75; | |
1885 | if (sector < pregapOffset) | |
1886 | subChanMissing = TRUE; | |
1887 | } | |
1888 | } | |
1889 | ||
c0ee7ac6 | 1890 | ret = cdimg_read_func(cdHandle, 0, cdbuffer, sector); |
1891 | if (ret < 0) | |
1892 | return -1; | |
ef79bbde | 1893 | |
9a92bffb | 1894 | if (subHandle != NULL) { |
1895 | fseek(subHandle, sector * SUB_FRAMESIZE, SEEK_SET); | |
1896 | fread(subbuffer, 1, SUB_FRAMESIZE, subHandle); | |
ef79bbde | 1897 | |
9a92bffb | 1898 | if (subChanRaw) DecodeRawSubData(); |
ef79bbde P |
1899 | } |
1900 | ||
1901 | return 0; | |
1902 | } | |
1903 | ||
ef79bbde P |
1904 | // plays cdda audio |
1905 | // sector: byte 0 - minute; byte 1 - second; byte 2 - frame | |
1906 | // does NOT uses bcd format | |
1907 | static long CALLBACK ISOplay(unsigned char *time) { | |
77160d47 | 1908 | unsigned int i; |
38b8102c | 1909 | |
7b8da7ab | 1910 | if (numtracks <= 1) |
1911 | return 0; | |
1912 | ||
38b8102c | 1913 | // find the track |
77160d47 | 1914 | cdda_cur_sector = msf2sec((char *)time); |
2058d51f | 1915 | for (i = numtracks; i > 1; i--) { |
77160d47 | 1916 | cdda_first_sector = msf2sec(ti[i].start); |
1917 | if (cdda_first_sector <= cdda_cur_sector + 2 * 75) | |
38b8102c | 1918 | break; |
2058d51f | 1919 | } |
77160d47 | 1920 | cdda_file_offset = ti[i].start_offset; |
19c03d80 | 1921 | |
38b8102c | 1922 | // find the file that contains this track |
1923 | for (; i > 1; i--) | |
1924 | if (ti[i].handle != NULL) | |
1925 | break; | |
1926 | ||
2058d51f | 1927 | cddaHandle = ti[i].handle; |
8aa91443 | 1928 | |
77160d47 | 1929 | if (SPU_playCDDAchannel != NULL) |
1930 | startCDDA(); | |
1931 | ||
ef79bbde P |
1932 | return 0; |
1933 | } | |
1934 | ||
1935 | // stops cdda audio | |
1936 | static long CALLBACK ISOstop(void) { | |
1937 | stopCDDA(); | |
1938 | return 0; | |
1939 | } | |
1940 | ||
1941 | // gets subchannel data | |
1942 | static unsigned char* CALLBACK ISOgetBufferSub(void) { | |
8aa91443 | 1943 | if ((subHandle != NULL || subChanMixed) && !subChanMissing) { |
ef79bbde P |
1944 | return subbuffer; |
1945 | } | |
1946 | ||
1947 | return NULL; | |
1948 | } | |
1949 | ||
1950 | static long CALLBACK ISOgetStatus(struct CdrStat *stat) { | |
deebc67f | 1951 | u32 sect; |
1952 | ||
ef79bbde | 1953 | CDR__getStatus(stat); |
deebc67f | 1954 | |
ef79bbde P |
1955 | if (playing) { |
1956 | stat->Type = 0x02; | |
1957 | stat->Status |= 0x80; | |
ef79bbde P |
1958 | } |
1959 | else { | |
deebc67f | 1960 | // BIOS - boot ID (CD type) |
1961 | stat->Type = ti[1].type; | |
1962 | } | |
1963 | ||
1964 | // relative -> absolute time | |
1965 | sect = cddaCurPos; | |
1966 | sec2msf(sect, (char *)stat->Time); | |
1967 | ||
1968 | return 0; | |
1969 | } | |
1970 | ||
1971 | // read CDDA sector into buffer | |
1972 | long CALLBACK ISOreadCDDA(unsigned char m, unsigned char s, unsigned char f, unsigned char *buffer) { | |
1973 | unsigned char msf[3] = {m, s, f}; | |
1974 | unsigned int file, track, track_start = 0; | |
1975 | int ret; | |
1976 | ||
1977 | cddaCurPos = msf2sec((char *)msf); | |
1978 | ||
1979 | // find current track index | |
1980 | for (track = numtracks; ; track--) { | |
1981 | track_start = msf2sec(ti[track].start); | |
1982 | if (track_start <= cddaCurPos) | |
1983 | break; | |
1984 | if (track == 1) | |
1985 | break; | |
1986 | } | |
1987 | ||
1988 | // data tracks play silent | |
1989 | if (ti[track].type != CDDA) { | |
1990 | memset(buffer, 0, CD_FRAMESIZE_RAW); | |
1991 | return 0; | |
1992 | } | |
1993 | ||
1994 | file = 1; | |
1995 | if (multifile) { | |
1996 | // find the file that contains this track | |
1997 | for (file = track; file > 1; file--) | |
1998 | if (ti[file].handle != NULL) | |
1999 | break; | |
ef79bbde P |
2000 | } |
2001 | ||
deebc67f | 2002 | ret = cdimg_read_func(ti[file].handle, ti[track].start_offset, |
2003 | buffer, cddaCurPos - track_start); | |
2004 | if (ret != CD_FRAMESIZE_RAW) { | |
2005 | memset(buffer, 0, CD_FRAMESIZE_RAW); | |
2006 | return -1; | |
2007 | } | |
2008 | ||
2009 | if (cddaBigEndian) { | |
2010 | int i; | |
2011 | unsigned char tmp; | |
2012 | ||
2013 | for (i = 0; i < CD_FRAMESIZE_RAW / 2; i++) { | |
2014 | tmp = buffer[i * 2]; | |
2015 | buffer[i * 2] = buffer[i * 2 + 1]; | |
2016 | buffer[i * 2 + 1] = tmp; | |
2017 | } | |
2018 | } | |
858ad511 | 2019 | |
ef79bbde P |
2020 | return 0; |
2021 | } | |
2022 | ||
2023 | void cdrIsoInit(void) { | |
2024 | CDR_init = ISOinit; | |
2025 | CDR_shutdown = ISOshutdown; | |
2026 | CDR_open = ISOopen; | |
2027 | CDR_close = ISOclose; | |
2028 | CDR_getTN = ISOgetTN; | |
2029 | CDR_getTD = ISOgetTD; | |
2030 | CDR_readTrack = ISOreadTrack; | |
2031 | CDR_getBuffer = ISOgetBuffer; | |
2032 | CDR_play = ISOplay; | |
2033 | CDR_stop = ISOstop; | |
2034 | CDR_getBufferSub = ISOgetBufferSub; | |
2035 | CDR_getStatus = ISOgetStatus; | |
deebc67f | 2036 | CDR_readCDDA = ISOreadCDDA; |
ef79bbde P |
2037 | |
2038 | CDR_getDriveLetter = CDR__getDriveLetter; | |
2039 | CDR_configure = CDR__configure; | |
2040 | CDR_test = CDR__test; | |
2041 | CDR_about = CDR__about; | |
2042 | CDR_setfilename = CDR__setfilename; | |
2043 | ||
2044 | numtracks = 0; | |
2045 | } | |
2046 | ||
2047 | int cdrIsoActive(void) { | |
2048 | return (cdHandle != NULL); | |
2049 | } |