Previous 15994 Revisions Next

r9135 Tuesday 14th September, 2010 at 20:14:19 UTC by R. Belmont
CHDMAN: latest from MAME, .WAV tracks are supported in bin/cue images.
[src/tools]chdcd.c

src/tools/chdcd.c
r9134r9135
1313#include "osdcore.h"
1414#include "chd.h"
1515#include "chdcd.h"
16#include "corefile.h"
1617
1718
1819
r9134r9135
4950   filerr = osd_open(filename, OPEN_FLAG_READ, &file, &filesize);
5051   if (filerr == FILERR_NONE)
5152      osd_close(file);
53
5254   return filesize;
5355}
5456
r9134r9135
121123   return f;
122124}
123125
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-------------------------------------------------*/
132static 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
124256static chd_error chdcd_parse_gdi(const char *tocfname, cdrom_toc *outtoc, chdcd_track_input_info *outinfo)
125257{
126258   FILE *infile;
r9134r9135
241373   int i, trknum;
242374   static char token[128];
243375   static char lastfname[128];
376   UINT32 wavlen, wavoffs;
244377
245378   infile = fopen(tocfname, "rt");
246379
r9134r9135
254387   memset(outinfo, 0, sizeof(chdcd_track_input_info));
255388
256389   trknum = -1;
390   wavoffs = wavlen = 0;
257391
258392   while (!feof(infile))
259393   {
r9134r9135
273407            TOKENIZE
274408
275409            /* keep the filename */
276            strncpy(&outinfo->fname[trknum][0], token, strlen(token));
277410            strncpy(lastfname, token, 128);
278411
279412            /* get the file type */
r9134r9135
287420            {
288421               outinfo->swap[trknum] = 1;
289422            }
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            }
290439            else
291440            {
292441               printf("ERROR: Unhandled track type %s\n", token);
r9134r9135
302451            /* next token on the line is the track type */
303452            TOKENIZE
304453
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            }
307467            outtoc->tracks[trknum].subtype = CD_SUB_NONE;
308468            outtoc->tracks[trknum].subsize = 0;
309469            outtoc->tracks[trknum].pregap = 0;
310470            outinfo->idx0offs[trknum] = -1;
311471            outinfo->idx1offs[trknum] = 0;
312472            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]);
314474
315475            cdrom_convert_type_string_to_track_info(token, &outtoc->tracks[trknum]);
316476            if (outtoc->tracks[trknum].datasize == 0)
r9134r9135
318478               printf("ERROR: Unknown track type [%s].  Contact MAMEDEV.\n", token);
319479               return CHDERR_FILE_NOT_FOUND;
320480            }
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            }
328481
329482            /* next (optional) token on the line is the subcode type */
330483            TOKENIZE
r9134r9135
379532      }
380533   }
381534
382   /* close the input CUE */
535    /* close the input CUE */
383536   fclose(infile);
384537
385538   /* store the number of tracks found */
r9134r9135
390543   {
391544      UINT64 tlen = 0;
392545
546      // this is true for cue/bin and cue/iso, and we need it for cue/wav since .WAV is little-endian
393547      if (outtoc->tracks[trknum].trktype == CD_TRACK_AUDIO)
394548      {
395549         outinfo->swap[trknum] = 1;
396550      }
397551
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)
400554      {
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))
403557         {
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]))
406560            {
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);
409569            }
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 */
417571            {
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;
420581            }
421            tlen /= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
422            outtoc->tracks[trknum].frames = tlen;
423            outinfo->offset[trknum] = 0;
424582         }
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
430584         {
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]))
434587            {
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];
441589
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               }
446604            }
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 */
452606            {
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;
455616            }
456            tlen /= (outtoc->tracks[trknum].datasize + outtoc->tracks[trknum].subsize);
457            outtoc->tracks[trknum].frames = tlen;
458            outinfo->offset[trknum] = 0;
459617         }
460618      }
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]);
462620   }
463621
464622   return CHDERR_NONE;
r9134r9135
604762               printf("ERROR: Unknown track type [%s].  Contact MAMEDEV.\n", token);
605763               return CHDERR_FILE_NOT_FOUND;
606764            }
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            }
614765
615766            /* next (optional) token on the line is the subcode type */
616767            TOKENIZE

Previous 15994 Revisions Next


© 1998-2018 The MESS Team