src/tools/chdcd.c
| r9134 | r9135 | |
| 13 | 13 | #include "osdcore.h" |
| 14 | 14 | #include "chd.h" |
| 15 | 15 | #include "chdcd.h" |
| 16 | #include "corefile.h" |
| 16 | 17 | |
| 17 | 18 | |
| 18 | 19 | |
| r9134 | r9135 | |
| 49 | 50 | filerr = osd_open(filename, OPEN_FLAG_READ, &file, &filesize); |
| 50 | 51 | if (filerr == FILERR_NONE) |
| 51 | 52 | osd_close(file); |
| 53 | |
| 52 | 54 | return filesize; |
| 53 | 55 | } |
| 54 | 56 | |
| r9134 | r9135 | |
| 121 | 123 | return f; |
| 122 | 124 | } |
| 123 | 125 | |
| 126 | /*------------------------------------------------- |
| 127 | parse_wav_sample - takes a .WAV file, verifies |
| 128 | that the file is 16 bits, and returns the |
| 129 | length in bytes of the data and the offset in |
| 130 | bytes to where the data starts in the file. |
| 131 | -------------------------------------------------*/ |
| 132 | static UINT32 parse_wav_sample(char *filename, UINT32 *dataoffs) |
| 133 | { |
| 134 | unsigned long offset = 0; |
| 135 | UINT32 length, rate, filesize; |
| 136 | UINT16 bits, temp16; |
| 137 | char buf[32]; |
| 138 | osd_file *file; |
| 139 | file_error filerr; |
| 140 | UINT64 fsize = 0; |
| 141 | UINT32 actual; |
| 142 | |
| 143 | filerr = osd_open(filename, OPEN_FLAG_READ, &file, &fsize); |
| 144 | if (filerr != FILERR_NONE) |
| 145 | { |
| 146 | osd_close(file); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | /* read the core header and make sure it's a WAVE file */ |
| 151 | osd_read(file, buf, 0, 4, &actual); |
| 152 | offset += actual; |
| 153 | if (offset < 4) |
| 154 | return 0; |
| 155 | if (memcmp(&buf[0], "RIFF", 4) != 0) |
| 156 | return 0; |
| 157 | |
| 158 | /* get the total size */ |
| 159 | osd_read(file, &filesize, offset, 4, &actual); |
| 160 | offset += actual; |
| 161 | if (offset < 8) |
| 162 | return 0; |
| 163 | filesize = LITTLE_ENDIANIZE_INT32(filesize); |
| 164 | |
| 165 | /* read the RIFF file type and make sure it's a WAVE file */ |
| 166 | osd_read(file, buf, offset, 4, &actual); |
| 167 | offset += actual; |
| 168 | if (offset < 12) |
| 169 | return 0; |
| 170 | if (memcmp(&buf[0], "WAVE", 4) != 0) |
| 171 | return 0; |
| 172 | |
| 173 | /* seek until we find a format tag */ |
| 174 | while (1) |
| 175 | { |
| 176 | osd_read(file, buf, offset, 4, &actual); |
| 177 | offset += actual; |
| 178 | osd_read(file, &length, offset, 4, &actual); |
| 179 | offset += actual; |
| 180 | length = LITTLE_ENDIANIZE_INT32(length); |
| 181 | if (memcmp(&buf[0], "fmt ", 4) == 0) |
| 182 | break; |
| 183 | |
| 184 | /* seek to the next block */ |
| 185 | offset += length; |
| 186 | if (offset >= filesize) |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* read the format -- make sure it is PCM */ |
| 191 | osd_read(file, &temp16, offset, 2, &actual); |
| 192 | offset += actual; |
| 193 | temp16 = LITTLE_ENDIANIZE_INT16(temp16); |
| 194 | if (temp16 != 1) |
| 195 | return 0; |
| 196 | |
| 197 | /* number of channels -- only mono is supported */ |
| 198 | osd_read(file, &temp16, offset, 2, &actual); |
| 199 | offset += actual; |
| 200 | temp16 = LITTLE_ENDIANIZE_INT16(temp16); |
| 201 | if (temp16 != 2) |
| 202 | return 0; |
| 203 | |
| 204 | /* sample rate */ |
| 205 | osd_read(file, &rate, offset, 4, &actual); |
| 206 | offset += actual; |
| 207 | rate = LITTLE_ENDIANIZE_INT32(rate); |
| 208 | if (rate != 44100) |
| 209 | return 0; |
| 210 | |
| 211 | /* bytes/second and block alignment are ignored */ |
| 212 | osd_read(file, buf, offset, 6, &actual); |
| 213 | offset += actual; |
| 214 | |
| 215 | /* bits/sample */ |
| 216 | osd_read(file, &bits, offset, 2, &actual); |
| 217 | offset += actual; |
| 218 | if (bits != 16) |
| 219 | return 0; |
| 220 | |
| 221 | /* seek past any extra data */ |
| 222 | offset += length - 16; |
| 223 | |
| 224 | /* seek until we find a data tag */ |
| 225 | while (1) |
| 226 | { |
| 227 | osd_read(file, buf, offset, 4, &actual); |
| 228 | offset += actual; |
| 229 | osd_read(file, &length, offset, 4, &actual); |
| 230 | offset += actual; |
| 231 | length = LITTLE_ENDIANIZE_INT32(length); |
| 232 | if (memcmp(&buf[0], "data", 4) == 0) |
| 233 | break; |
| 234 | |
| 235 | /* seek to the next block */ |
| 236 | offset += length; |
| 237 | if (offset >= filesize) |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | /* if there was a 0 length data block, we're done */ |
| 242 | if (length == 0) |
| 243 | return 0; |
| 244 | |
| 245 | osd_close(file); |
| 246 | |
| 247 | *dataoffs = offset; |
| 248 | |
| 249 | return length; |
| 250 | } |
| 251 | |
| 252 | /*------------------------------------------------- |
| 253 | chdcd_parse_gdi - parse a Sega GD-ROM rip |
| 254 | -------------------------------------------------*/ |
| 255 | |
| 124 | 256 | static chd_error chdcd_parse_gdi(const char *tocfname, cdrom_toc *outtoc, chdcd_track_input_info *outinfo) |
| 125 | 257 | { |
| 126 | 258 | FILE *infile; |
| r9134 | r9135 | |
| 241 | 373 | int i, trknum; |
| 242 | 374 | static char token[128]; |
| 243 | 375 | static char lastfname[128]; |
| 376 | UINT32 wavlen, wavoffs; |
| 244 | 377 | |
| 245 | 378 | infile = fopen(tocfname, "rt"); |
| 246 | 379 | |
| r9134 | r9135 | |
| 254 | 387 | memset(outinfo, 0, sizeof(chdcd_track_input_info)); |
| 255 | 388 | |
| 256 | 389 | trknum = -1; |
| 390 | wavoffs = wavlen = 0; |
| 257 | 391 | |
| 258 | 392 | while (!feof(infile)) |
| 259 | 393 | { |
| r9134 | r9135 | |
| 273 | 407 | TOKENIZE |
| 274 | 408 | |
| 275 | 409 | /* keep the filename */ |
| 276 | | strncpy(&outinfo->fname[trknum][0], token, strlen(token)); |
| 277 | 410 | strncpy(lastfname, token, 128); |
| 278 | 411 | |
| 279 | 412 | /* get the file type */ |
| r9134 | r9135 | |
| 287 | 420 | { |
| 288 | 421 | outinfo->swap[trknum] = 1; |
| 289 | 422 | } |
| 423 | else if (!strcmp(token, "WAVE")) |
| 424 | { |
| 425 | wavlen = parse_wav_sample(lastfname, &wavoffs); |
| 426 | if (!wavlen) |
| 427 | { |
| 428 | file_error err; |
| 429 | core_file *fhand; |
| 430 | |
| 431 | err = core_fopen(lastfname, OPEN_FLAG_READ, &fhand); |
| 432 | if (err != FILERR_NONE) printf("holy shit!\n"); |
| 433 | else core_fclose(fhand); |
| 434 | |
| 435 | printf("ERROR: couldn't read [%s] or not a valid .WAV\n", lastfname); |
| 436 | return CHDERR_FILE_NOT_FOUND; |
| 437 | } |
| 438 | } |
| 290 | 439 | else |
| 291 | 440 | { |
| 292 | 441 | printf("ERROR: Unhandled track type %s\n", token); |
| r9134 | r9135 | |
| 302 | 451 | /* next token on the line is the track type */ |
| 303 | 452 | TOKENIZE |
| 304 | 453 | |
| 305 | | outtoc->tracks[trknum].trktype = CD_TRACK_MODE1; |
| 306 | | outtoc->tracks[trknum].datasize = 0; |
| 454 | if (wavlen != 0) |
| 455 | { |
| 456 | outtoc->tracks[trknum].trktype = CD_TRACK_AUDIO; |
| 457 | outtoc->tracks[trknum].frames = wavlen/2352; |
| 458 | outinfo->offset[trknum] = wavoffs; |
| 459 | wavoffs = wavlen = 0; |
| 460 | } |
| 461 | else |
| 462 | { |
| 463 | outtoc->tracks[trknum].trktype = CD_TRACK_MODE1; |
| 464 | outtoc->tracks[trknum].datasize = 0; |
| 465 | outinfo->offset[trknum] = 0; |
| 466 | } |
| 307 | 467 | outtoc->tracks[trknum].subtype = CD_SUB_NONE; |
| 308 | 468 | outtoc->tracks[trknum].subsize = 0; |
| 309 | 469 | outtoc->tracks[trknum].pregap = 0; |
| 310 | 470 | outinfo->idx0offs[trknum] = -1; |
| 311 | 471 | outinfo->idx1offs[trknum] = 0; |
| 312 | 472 | strcpy(&outinfo->fname[trknum][0], lastfname); // default filename to the last one |
| 313 | | // printf("trk %d: fname %s\n", trknum+1, &outinfo->fname[trknum][0]); |
| 473 | // printf("trk %d: fname %s offset %d\n", trknum, &outinfo->fname[trknum][0], outinfo->offset[trknum]); |
| 314 | 474 | |
| 315 | 475 | cdrom_convert_type_string_to_track_info(token, &outtoc->tracks[trknum]); |
| 316 | 476 | if (outtoc->tracks[trknum].datasize == 0) |
| r9134 | r9135 | |
| 318 | 478 | printf("ERROR: Unknown track type [%s]. Contact MAMEDEV.\n", token); |
| 319 | 479 | return CHDERR_FILE_NOT_FOUND; |
| 320 | 480 | } |
| 321 | | else if (outtoc->tracks[trknum].trktype != CD_TRACK_MODE1_RAW && |
| 322 | | outtoc->tracks[trknum].trktype != CD_TRACK_MODE2_RAW && |
| 323 | | outtoc->tracks[trknum].trktype != CD_TRACK_AUDIO) |
| 324 | | { |
| 325 | | printf("Note: MAME prefers and can accept RAW format images.\n"); |
| 326 | | printf("At least one track of this rip is not either RAW or AUDIO.\n"); |
| 327 | | } |
| 328 | 481 | |
| 329 | 482 | /* next (optional) token on the line is the subcode type */ |
| 330 | 483 | TOKENIZE |
| r9134 | r9135 | |
| 379 | 532 | } |
| 380 | 533 | } |
| 381 | 534 | |
| 382 | | /* close the input CUE */ |
| 535 | /* close the input CUE */ |
| 383 | 536 | fclose(infile); |
| 384 | 537 | |
| 385 | 538 | /* store the number of tracks found */ |
| r9134 | r9135 | |
| 390 | 543 | { |
| 391 | 544 | UINT64 tlen = 0; |
| 392 | 545 | |
| 546 | // this is true for cue/bin and cue/iso, and we need it for cue/wav since .WAV is little-endian |
| 393 | 547 | if (outtoc->tracks[trknum].trktype == CD_TRACK_AUDIO) |
| 394 | 548 | { |
| 395 | 549 | outinfo->swap[trknum] = 1; |
| 396 | 550 | } |
| 397 | 551 | |
| 398 | | // is this the last track? |
| 399 | | if (trknum == (outtoc->numtrks-1)) |
| 552 | // don't do this for .WAV tracks, we already have their length and offset filled out |
| 553 | if (outinfo->offset[trknum] == 0) |
| 400 | 554 | { |
| 401 | | /* if we have the same filename as the last track, do it that way */ |
| 402 | | if (!strcmp(&outinfo->fname[trknum][0], &outinfo->fname[trknum-1][0])) |
| 555 | // is this the last track? |
| 556 | if (trknum == (outtoc->numtrks-1)) |
| 403 | 557 | { |
| 404 | | tlen = get_file_size(outinfo->fname[trknum]); |
| 405 | | if (tlen == 0) |
| 558 | /* if we have the same filename as the last track, do it that way */ |
| 559 | if (!strcmp(&outinfo->fname[trknum][0], &outinfo->fname[trknum-1][0])) |
| 406 | 560 | { |
| 407 | | printf("ERROR: couldn't find bin file [%s]\n", outinfo->fname[trknum-1]); |
| 408 | | return CHDERR_FILE_NOT_FOUND; |
| 561 | tlen = get_file_size(outinfo->fname[trknum]); |
| 562 | if (tlen == 0) |
| 563 | { |
| 564 | printf("ERROR: couldn't find bin file [%s]\n", outinfo->fname[trknum-1]); |
| 565 | return CHDERR_FILE_NOT_FOUND; |
| 566 | } |
| 567 | outinfo->offset[trknum] = outinfo->offset[trknum-1] + outtoc->tracks[trknum-1].frames * (outtoc->tracks[trknum-1].datasize + outtoc->tracks[trknum-1].subsize); |
| 568 | outtoc->tracks[trknum].frames = (tlen - outinfo->offset[trknum]) / (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize); |
| 409 | 569 | } |
| 410 | | outinfo->offset[trknum] = outinfo->offset[trknum-1] + outtoc->tracks[trknum-1].frames * (outtoc->tracks[trknum-1].datasize + outtoc->tracks[trknum-1].subsize); |
| 411 | | outtoc->tracks[trknum].frames = (tlen - outinfo->offset[trknum]) / (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize); |
| 412 | | } |
| 413 | | else /* data files are different */ |
| 414 | | { |
| 415 | | tlen = get_file_size(outinfo->fname[trknum]); |
| 416 | | if (tlen == 0) |
| 570 | else /* data files are different */ |
| 417 | 571 | { |
| 418 | | printf("ERROR: couldn't find bin file [%s]\n", outinfo->fname[trknum-1]); |
| 419 | | return CHDERR_FILE_NOT_FOUND; |
| 572 | tlen = get_file_size(outinfo->fname[trknum]); |
| 573 | if (tlen == 0) |
| 574 | { |
| 575 | printf("ERROR: couldn't find bin file [%s]\n", outinfo->fname[trknum-1]); |
| 576 | return CHDERR_FILE_NOT_FOUND; |
| 577 | } |
| 578 | tlen /= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize); |
| 579 | outtoc->tracks[trknum].frames = tlen; |
| 580 | outinfo->offset[trknum] = 0; |
| 420 | 581 | } |
| 421 | | tlen /= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize); |
| 422 | | outtoc->tracks[trknum].frames = tlen; |
| 423 | | outinfo->offset[trknum] = 0; |
| 424 | 582 | } |
| 425 | | } |
| 426 | | else |
| 427 | | { |
| 428 | | /* if we have the same filename as the next track, do it that way */ |
| 429 | | if (!strcmp(&outinfo->fname[trknum][0], &outinfo->fname[trknum+1][0])) |
| 583 | else |
| 430 | 584 | { |
| 431 | | outtoc->tracks[trknum].frames = outinfo->idx1offs[trknum+1] - outinfo->idx1offs[trknum]; |
| 432 | | |
| 433 | | if (trknum == 0) // track 0 offset is 0 |
| 585 | /* if we have the same filename as the next track, do it that way */ |
| 586 | if (!strcmp(&outinfo->fname[trknum][0], &outinfo->fname[trknum+1][0])) |
| 434 | 587 | { |
| 435 | | outinfo->offset[trknum] = 0; |
| 436 | | } |
| 437 | | else |
| 438 | | { |
| 439 | | outinfo->offset[trknum] = outinfo->offset[trknum-1] + outtoc->tracks[trknum-1].frames * (outtoc->tracks[trknum-1].datasize + outtoc->tracks[trknum-1].subsize); |
| 440 | | } |
| 588 | outtoc->tracks[trknum].frames = outinfo->idx1offs[trknum+1] - outinfo->idx1offs[trknum]; |
| 441 | 589 | |
| 442 | | if (!outtoc->tracks[trknum].frames) |
| 443 | | { |
| 444 | | printf("ERROR: unable to determine size of track %d, missing INDEX 01 markers?\n", trknum+1); |
| 445 | | return CHDERR_FILE_NOT_FOUND; |
| 590 | if (trknum == 0) // track 0 offset is 0 |
| 591 | { |
| 592 | outinfo->offset[trknum] = 0; |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | outinfo->offset[trknum] = outinfo->offset[trknum-1] + outtoc->tracks[trknum-1].frames * (outtoc->tracks[trknum-1].datasize + outtoc->tracks[trknum-1].subsize); |
| 597 | } |
| 598 | |
| 599 | if (!outtoc->tracks[trknum].frames) |
| 600 | { |
| 601 | printf("ERROR: unable to determine size of track %d, missing INDEX 01 markers?\n", trknum+1); |
| 602 | return CHDERR_FILE_NOT_FOUND; |
| 603 | } |
| 446 | 604 | } |
| 447 | | } |
| 448 | | else /* data files are different */ |
| 449 | | { |
| 450 | | tlen = get_file_size(outinfo->fname[trknum-1]); |
| 451 | | if (tlen == 0) |
| 605 | else /* data files are different */ |
| 452 | 606 | { |
| 453 | | printf("ERROR: couldn't find bin file [%s]\n", outinfo->fname[trknum-1]); |
| 454 | | return CHDERR_FILE_NOT_FOUND; |
| 607 | tlen = get_file_size(outinfo->fname[trknum]); |
| 608 | if (tlen == 0) |
| 609 | { |
| 610 | printf("ERROR: couldn't find bin file [%s]\n", outinfo->fname[trknum]); |
| 611 | return CHDERR_FILE_NOT_FOUND; |
| 612 | } |
| 613 | tlen /= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize); |
| 614 | outtoc->tracks[trknum].frames = tlen; |
| 615 | outinfo->offset[trknum] = 0; |
| 455 | 616 | } |
| 456 | | tlen /= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize); |
| 457 | | outtoc->tracks[trknum].frames = tlen; |
| 458 | | outinfo->offset[trknum] = 0; |
| 459 | 617 | } |
| 460 | 618 | } |
| 461 | | // printf("trk %d: %d frames @ offset %d\n", trknum+1, outtoc->tracks[trknum].frames, outinfo->offset[trknum]); |
| 619 | printf("trk %d: %d frames @ offset %d\n", trknum+1, outtoc->tracks[trknum].frames, outinfo->offset[trknum]); |
| 462 | 620 | } |
| 463 | 621 | |
| 464 | 622 | return CHDERR_NONE; |
| r9134 | r9135 | |
| 604 | 762 | printf("ERROR: Unknown track type [%s]. Contact MAMEDEV.\n", token); |
| 605 | 763 | return CHDERR_FILE_NOT_FOUND; |
| 606 | 764 | } |
| 607 | | else if (outtoc->tracks[trknum].trktype != CD_TRACK_MODE1_RAW && |
| 608 | | outtoc->tracks[trknum].trktype != CD_TRACK_MODE2_RAW && |
| 609 | | outtoc->tracks[trknum].trktype != CD_TRACK_AUDIO) |
| 610 | | { |
| 611 | | printf("Note: MAME prefers and can accept RAW format images.\n"); |
| 612 | | printf("At least one track of this rip is not either RAW or AUDIO.\n"); |
| 613 | | } |
| 614 | 765 | |
| 615 | 766 | /* next (optional) token on the line is the subcode type */ |
| 616 | 767 | TOKENIZE |