3 * originally written by Exophase as "bin_to_iso_ogg"
\r
4 * updated for cso/mp3 by notaz
\r
10 #include <sys/stat.h>
\r
16 #define MAX_PATH 1024
\r
20 #include <windows.h>
\r
22 #define DIR_SEPARATOR_CHAR '\\'
\r
23 #define PATH_SEPARATOR_CHAR ';'
\r
24 #define LAME_BINARY "lame.exe"
\r
25 #define CISO_BINARY "ciso.exe"
\r
26 #define NULL_REDIR "> NUL 2>&1"
\r
28 #define DIR_SEPARATOR_CHAR '/'
\r
29 #define PATH_SEPARATOR_CHAR ':'
\r
30 #define LAME_BINARY "lame"
\r
31 #define CISO_BINARY "ciso"
\r
32 #define NULL_REDIR "> /dev/null 2>&1"
\r
33 #define mkdir(x) mkdir(x, S_IRWXU)
\r
36 #define LAME_OPTIONS "-h --cbr"
\r
39 typedef uint16_t u16;
\r
40 typedef uint32_t u32;
\r
41 typedef uint64_t u64;
\r
43 typedef int16_t s16;
\r
44 typedef int32_t s32;
\r
45 typedef int64_t s64;
\r
49 TRACK_FILE_TYPE_BINARY,
\r
50 TRACK_FILE_TYPE_WAVE,
\r
51 } track_file_type_enum;
\r
56 u32 physical_offset;
\r
68 track_file_type_enum type;
\r
72 } cd_track_file_struct;
\r
77 cd_track_file_struct track_files[100];
\r
82 u32 num_physical_tracks;
\r
84 s32 last_seek_track;
\r
86 cd_track_struct physical_tracks[100];
\r
87 cd_track_struct *logical_tracks[100];
\r
91 cd_bin_struct cd_bin;
\r
92 int opt_use_mp3 = 1;
\r
93 int opt_mp3_bitrate = 128;
\r
94 int opt_use_cso = 1;
\r
96 static void myexit(int code)
\r
104 char *skip_whitespace(char *str)
\r
106 while (isspace(*str))
\r
112 char *skip_whitespace_rev(char *str)
\r
114 while (isspace(*str))
\r
120 char *skip_nonspace_rev(char *str)
\r
122 while (!isspace(*str))
\r
128 s32 load_bin_cue(char *cue_file_name)
\r
130 FILE *cue_file = fopen(cue_file_name, "rb");
\r
132 printf("loading cue file %s\n", cue_file_name);
\r
136 char line_buffer[256];
\r
137 char *line_buffer_ptr;
\r
140 char bin_file_name[MAX_PATH];
\r
141 char *separator_pos;
\r
142 s32 current_physical_track_number = -1;
\r
143 u32 current_physical_offset;
\r
144 u32 current_pregap = 0;
\r
147 cd_track_struct *current_physical_track = NULL;
\r
151 // First, get filename. Only support binary right now.
\r
152 tmp = fgets(line_buffer, 255, cue_file);
\r
153 if (tmp == NULL) goto invalid;
\r
154 separator_pos = line_buffer + strlen(line_buffer) - 1;
\r
155 separator_pos = skip_whitespace_rev(separator_pos);
\r
156 if (separator_pos <= line_buffer) goto invalid;
\r
157 separator_pos = skip_nonspace_rev(separator_pos);
\r
158 if (separator_pos <= line_buffer) goto invalid;
\r
159 separator_pos = skip_whitespace_rev(separator_pos);
\r
160 if (separator_pos <= line_buffer) goto invalid;
\r
161 // see if what's there is a quote.
\r
162 if(*separator_pos == '"')
\r
164 separator_pos[0] = 0;
\r
165 separator_pos = strrchr(line_buffer, '"');
\r
166 if (separator_pos == NULL) goto invalid;
\r
167 strcpy(bin_file_name, separator_pos + 1);
\r
171 // Otherwise go to the next space.
\r
172 separator_pos[1] = 0;
\r
173 separator_pos = strrchr(line_buffer, ' ');
\r
174 if (separator_pos == NULL) goto invalid;
\r
175 strcpy(bin_file_name, separator_pos + 1);
\r
178 // Might have to change directory first.
\r
179 separator_pos = strrchr(cue_file_name, DIR_SEPARATOR_CHAR);
\r
183 char current_dir[MAX_PATH];
\r
184 getcwd(current_dir, MAX_PATH);
\r
186 *separator_pos = 0;
\r
188 chdir(cue_file_name);
\r
191 cd_bin.bin_file = open(bin_file_name, O_RDONLY);
\r
193 cd_bin.bin_file = fopen(bin_file_name, "rb");
\r
196 *separator_pos = DIR_SEPARATOR_CHAR;
\r
197 chdir(current_dir);
\r
202 cd_bin.bin_file = open(bin_file_name, O_RDONLY);
\r
204 cd_bin.bin_file = fopen(bin_file_name, "rb");
\r
208 if (cd_bin.bin_file == NULL)
\r
210 printf("can't open bin file: \"%s\"\n", bin_file_name);
\r
215 printf("found bin file: %s\n", bin_file_name);
\r
218 for(i = 0; i < 100; i++)
\r
220 cd_bin.logical_tracks[i] = NULL;
\r
223 cd_bin.first_track = -1;
\r
224 cd_bin.last_track = -1;
\r
225 cd_bin.num_physical_tracks = 0;
\r
226 cd_bin.num_sectors = 0;
\r
229 while(fgets(line_buffer, 256, cue_file))
\r
231 // Skip trailing whitespace
\r
232 line_buffer_ptr = skip_whitespace(line_buffer);
\r
234 // Dirty, but should work - switch on first character.
\r
235 switch(line_buffer_ptr[0])
\r
237 // New track number
\r
240 u32 new_track_number;
\r
241 char track_type[64];
\r
243 sscanf(line_buffer_ptr, "TRACK %d %s", &new_track_number,
\r
246 current_physical_track_number++;
\r
247 current_physical_track =
\r
248 cd_bin.physical_tracks + current_physical_track_number;
\r
250 current_physical_track->sector_size = 2352;
\r
252 if(!strcmp(track_type, "AUDIO"))
\r
254 current_physical_track->format_type = 0;
\r
255 current_physical_track->sector_size = 2352;
\r
258 if(!strcmp(track_type, "MODE1/2352"))
\r
260 current_physical_track->format_type = 4;
\r
261 current_physical_track->sector_size = 2352;
\r
264 if(!strcmp(track_type, "MODE1/2048"))
\r
266 current_physical_track->format_type = 4;
\r
267 current_physical_track->sector_size = 2048;
\r
270 cd_bin.logical_tracks[new_track_number] = current_physical_track;
\r
271 cd_bin.num_physical_tracks++;
\r
273 if((cd_bin.first_track == -1) ||
\r
274 (new_track_number < cd_bin.first_track))
\r
276 cd_bin.first_track = new_track_number;
\r
279 if((cd_bin.last_track == -1) ||
\r
280 (new_track_number > cd_bin.last_track))
\r
282 cd_bin.last_track = new_track_number;
\r
291 u32 minutes, seconds, frames;
\r
293 sscanf(line_buffer_ptr, "PREGAP %d:%d:%d", &minutes,
\r
294 &seconds, &frames);
\r
296 current_pregap += frames + (seconds * 75) + (minutes * 75 * 60);
\r
304 u32 minutes, seconds, frames;
\r
307 sscanf(line_buffer_ptr, "INDEX %d %d:%d:%d", &index_number,
\r
308 &minutes, &seconds, &frames);
\r
310 sector_offset = frames + (seconds * 75) + (minutes * 75 * 60);
\r
312 if(index_number == 1)
\r
314 current_physical_track->pregap_offset = current_pregap;
\r
315 current_physical_track->sector_offset = sector_offset;
\r
323 current_physical_offset = 0;
\r
325 for(i = 0; i < cd_bin.num_physical_tracks - 1; i++)
\r
327 cd_bin.physical_tracks[i].sector_count =
\r
328 cd_bin.physical_tracks[i + 1].sector_offset -
\r
329 cd_bin.physical_tracks[i].sector_offset;
\r
331 cd_bin.physical_tracks[i].physical_offset = current_physical_offset;
\r
332 current_physical_offset += (cd_bin.physical_tracks[i].sector_count *
\r
333 cd_bin.physical_tracks[i].sector_size);
\r
335 cd_bin.physical_tracks[i].sector_offset +=
\r
336 cd_bin.physical_tracks[i].pregap_offset;
\r
338 cd_bin.num_sectors += cd_bin.physical_tracks[i].sector_count;
\r
342 bin_file_size = lseek(cd_bin.bin_file, 0, SEEK_END);
\r
343 lseek(cd_bin.bin_file, 0, SEEK_SET);
\r
345 fseek(cd_bin.bin_file, 0, SEEK_END);
\r
346 bin_file_size = ftell(cd_bin.bin_file);
\r
347 fseek(cd_bin.bin_file, 0, SEEK_SET);
\r
350 // Set the last track data
\r
351 cd_bin.physical_tracks[i].physical_offset = current_physical_offset;
\r
352 cd_bin.physical_tracks[i].sector_offset +=
\r
353 cd_bin.physical_tracks[i].pregap_offset;
\r
354 cd_bin.physical_tracks[i].sector_count =
\r
355 (bin_file_size - current_physical_offset) /
\r
356 cd_bin.physical_tracks[i].sector_size;
\r
358 cd_bin.num_sectors += cd_bin.physical_tracks[i].sector_count;
\r
360 printf("finished loading cue %s\n", cue_file_name);
\r
361 printf("bin file: %s (%p)\n", bin_file_name, cd_bin.bin_file);
\r
362 printf("first track: %d, last track: %d\n", cd_bin.first_track,
\r
363 cd_bin.last_track);
\r
365 for(i = cd_bin.first_track; i <= cd_bin.last_track; i++)
\r
367 printf("track %d (%p):\n", i, cd_bin.logical_tracks[i]);
\r
368 if(cd_bin.logical_tracks[i] == NULL)
\r
370 printf(" (invalid)\n");
\r
374 printf(" physical offset 0x%x\n",
\r
375 cd_bin.logical_tracks[i]->physical_offset);
\r
376 printf(" sector offset 0x%x\n",
\r
377 cd_bin.logical_tracks[i]->sector_offset);
\r
378 printf(" sector size %d\n",
\r
379 cd_bin.logical_tracks[i]->sector_size);
\r
383 cd_bin.last_seek_track = 0;
\r
391 printf("error: invalid/unsupported .cue file\n");
\r
395 #define address8(base, offset) \
\r
396 *((u8 *)((u8 *)base + (offset))) \
\r
398 #define address16(base, offset) \
\r
399 *((u16 *)((u8 *)base + (offset))) \
\r
401 #define address32(base, offset) \
\r
402 *((u32 *)((u8 *)base + (offset))) \
\r
404 // This will only work on little endian platforms for now.
\r
406 s32 convert_bin_to_wav(FILE *bin_file, char *output_dir, char *wav_file_name,
\r
411 u8 *riff_header = wav_header + 0;
\r
412 u8 *fmt_header = wav_header + 0x0C;
\r
413 u8 sector_buffer[2352];
\r
414 u32 byte_length = sector_count * 2352;
\r
418 wav_file = fopen(wav_file_name, "wb");
\r
420 printf("writing wav %s, %x sectors\n", wav_file_name, sector_count);
\r
423 memcpy(riff_header + 0x00, "RIFF", 4);
\r
424 address32(riff_header, 0x04) = byte_length + 44 - 8;
\r
425 memcpy(riff_header + 0x08, "WAVE", 4);
\r
427 // WAVE file chunk: format
\r
428 memcpy(fmt_header + 0x00, "fmt ", 4);
\r
430 address32(fmt_header, 0x04) = 16;
\r
431 // Compression code: PCM
\r
432 address16(fmt_header, 0x08) = 1;
\r
433 // Number of channels: Stereo
\r
434 address16(fmt_header, 0x0a) = 2;
\r
435 // Sample rate: 44100Hz
\r
436 address32(fmt_header, 0x0c) = 44100;
\r
437 // Average bytes per second: sample rate * 4
\r
438 address32(fmt_header, 0x10) = 44100 * 4;
\r
439 // Block align (bytes per sample)
\r
440 address16(fmt_header, 0x14) = 4;
\r
442 address16(fmt_header, 0x16) = 16;
\r
444 // Write out header
\r
445 fwrite(wav_header, 36, 1, wav_file);
\r
448 fprintf(wav_file, "data");
\r
450 fwrite(&byte_length, 4, 1, wav_file);
\r
452 // Write out sectors
\r
453 for(i = 0; i < sector_count; i++)
\r
455 printf("\b\b\b%3i", i*100 / sector_count);
\r
457 fread(sector_buffer, 2352, 1, bin_file);
\r
458 fwrite(sector_buffer, 2352, 1, wav_file);
\r
460 printf("\b\b\b100\n");
\r
467 void convert_wav_to_ogg(char *wav_file_name, char *output_dir,
\r
468 char *ogg_file_name)
\r
470 char cmd_string[(MAX_PATH * 2) + 16];
\r
473 sprintf(cmd_string, "oggenc %s", wav_file_name);
\r
474 system(cmd_string);
\r
476 unlink(wav_file_name);
\r
480 void convert_wav_to_mp3(char *wav_file_name, char *output_dir,
\r
481 char *mp3_file_name)
\r
483 char cmd_string[(MAX_PATH * 2) + 16];
\r
486 sprintf(cmd_string, LAME_BINARY " " LAME_OPTIONS " -b %i \"%s\" \"%s\"",
\r
487 opt_mp3_bitrate, wav_file_name, mp3_file_name);
\r
488 if (system(cmd_string) != 0)
\r
490 printf("failed to encode mp3\n");
\r
494 unlink(wav_file_name);
\r
498 s32 convert_bin_to_iso(FILE *bin_file, char *output_dir, char *iso_file_name,
\r
502 u8 sector_buffer[2352];
\r
506 iso_file = fopen(iso_file_name, "wb");
\r
507 if (iso_file == NULL)
\r
509 printf("failed to open: %s\n", iso_file_name);
\r
512 printf("writing iso %s, %x sectors\n", iso_file_name, sector_count);
\r
514 for(i = 0; i < sector_count; i++)
\r
516 printf("\b\b\b%3i", i*100 / sector_count);
\r
518 fread(sector_buffer, 2352, 1, bin_file);
\r
519 fwrite(sector_buffer + 16, 2048, 1, iso_file);
\r
521 printf("\b\b\b100\n");
\r
528 void convert_iso_to_cso(char *output_dir, char *iso_file_name, char *cso_file_name)
\r
530 char cmd_string[(MAX_PATH * 2) + 16];
\r
533 sprintf(cmd_string, CISO_BINARY " 9 \"%s\" \"%s\"", iso_file_name, cso_file_name);
\r
534 if (system(cmd_string) != 0)
\r
536 printf("failed to convert iso to cso\n");
\r
540 unlink(iso_file_name);
\r
545 #define sector_offset_to_msf(offset, minutes, seconds, frames) \
\r
547 u32 _offset = offset; \
\r
548 minutes = (_offset / 75) / 60; \
\r
549 seconds = (_offset / 75) % 60; \
\r
550 frames = _offset % 75; \
\r
554 s32 convert_bin_cue(char *output_name_base)
\r
556 char output_file_name[MAX_PATH];
\r
557 FILE *output_cue_file;
\r
558 FILE *bin_file = cd_bin.bin_file;
\r
559 cd_track_struct *current_track;
\r
561 u32 current_pregap = 0;
\r
562 u32 last_pregap = 0;
\r
566 if(stat(output_name_base, &sb))
\r
567 mkdir(output_name_base);
\r
569 sprintf(output_file_name, "%s.cue", output_name_base);
\r
570 chdir(output_name_base);
\r
571 output_cue_file = fopen(output_file_name, "wb");
\r
574 // Every track gets its own file. It's either going to be of type ISO
\r
577 for(i = 0; i < 100; i++)
\r
579 current_track = cd_bin.logical_tracks[i];
\r
580 if(current_track != NULL)
\r
582 switch(current_track->format_type)
\r
584 char output_name_tmp[MAX_PATH];
\r
589 sprintf(output_file_name, "%s_%02d.mp3", output_name_base, i);
\r
590 sprintf(output_name_tmp, "%s_%02d.wav", output_name_base, i);
\r
592 fprintf(output_cue_file, "FILE \"%s\" %s\n",
\r
593 opt_use_mp3 ? output_file_name : output_name_tmp,
\r
594 opt_use_mp3 ? "MP3" : "WAVE");
\r
595 fprintf(output_cue_file, " TRACK %02d AUDIO\n", i);
\r
596 current_pregap = current_track->pregap_offset - last_pregap;
\r
597 last_pregap = current_track->pregap_offset;
\r
598 if(current_pregap > 0)
\r
600 sector_offset_to_msf(current_pregap, m, s, f);
\r
601 fprintf(output_cue_file, " PREGAP %02d:%02d:%02d\n", m, s, f);
\r
603 fprintf(output_cue_file, " INDEX 01 00:00:00\n");
\r
604 sector_offset_to_msf(current_track->sector_count, m, s, f);
\r
605 fprintf(output_cue_file, " REM LENGTH %02d:%02d:%02d\n", m, s, f);
\r
607 fseek(bin_file, current_track->physical_offset, SEEK_SET);
\r
608 convert_bin_to_wav(bin_file, output_name_base, output_name_tmp,
\r
609 current_track->sector_count);
\r
612 convert_wav_to_mp3(output_name_tmp, output_name_base,
\r
620 sprintf(output_file_name, "%s_%02d.cso", output_name_base, i);
\r
621 sprintf(output_name_tmp, "%s_%02d.iso", output_name_base, i);
\r
622 fprintf(output_cue_file, "FILE \"%s\" BINARY\n",
\r
623 opt_use_cso ? output_file_name : output_name_tmp);
\r
624 fprintf(output_cue_file, " TRACK %02d MODE1/2048\n", i);
\r
625 current_pregap = current_track->pregap_offset - last_pregap;
\r
626 last_pregap = current_track->pregap_offset;
\r
627 if(current_pregap > 0)
\r
629 sector_offset_to_msf(current_pregap, m, s, f);
\r
630 fprintf(output_cue_file, " PREGAP %02d:%02d:%02d\n", m, s, f);
\r
632 fprintf(output_cue_file, " INDEX 01 00:00:00\n");
\r
634 fseek(bin_file, current_track->physical_offset, SEEK_SET);
\r
635 convert_bin_to_iso(bin_file, output_name_base, output_name_tmp,
\r
636 current_track->sector_count);
\r
639 convert_iso_to_cso(output_name_base, output_name_tmp, output_file_name);
\r
646 fclose(output_cue_file);
\r
652 static void update_path(void)
\r
654 char buff1[MAX_PATH*4], *buff2;
\r
658 path = getenv("PATH");
\r
659 GetModuleFileNameA(NULL, buff1, sizeof(buff1));
\r
660 for (i = strlen(buff1)-1; i > 0; i--)
\r
661 if (buff1[i] == '\\') break;
\r
664 size = strlen(path) + strlen(buff1) + 3;
\r
665 buff2 = malloc(size);
\r
666 if (buff2 == NULL) return;
\r
668 snprintf(buff2, size, "%s;%s", path, buff1);
\r
669 SetEnvironmentVariableA("PATH", buff2);
\r
674 int main(int argc, char *argv[])
\r
676 char out_buff[MAX_PATH], *cue_file, *out_base;
\r
681 printf("bin/cue to cso/mp3 converter\n");
\r
682 printf("usage: %s [options] <input cue> [output base]\n", argv[0]);
\r
683 printf("options:\n"
\r
684 " -m output mp3 files for audio (default) (lame required)\n"
\r
685 " -b <rate> mp3 bitrate to use (default is 128)\n"
\r
686 " -w output wav files for audio\n"
\r
687 " -c output cso as data track (default) (ciso required)\n"
\r
688 " -i output iso as data track\n");
\r
692 for (a = 1; a < argc - 1; a++)
\r
694 if (strcmp(argv[a], "-m") == 0)
\r
696 else if (strcmp(argv[a], "-w") == 0)
\r
698 else if (strcmp(argv[a], "-c") == 0)
\r
700 else if (strcmp(argv[a], "-i") == 0)
\r
702 else if (strcmp(argv[a], "-b") == 0)
\r
704 opt_mp3_bitrate = atoi(argv[++a]);
\r
709 cue_file = argv[a];
\r
710 out_base = argv[a+1];
\r
712 /* some sanity checks */
\r
713 if(strlen(cue_file) < 4 || strcasecmp(cue_file + strlen(cue_file) - 4, ".cue") != 0)
\r
715 printf("error: not a cue file specified?\n");
\r
723 if(opt_use_mp3 && system(LAME_BINARY " --help " NULL_REDIR) != 0)
\r
725 printf("LAME seems to be missing.\n"
\r
727 "Download from http://lame.sourceforge.net/links.php#Binaries and extract\n"
\r
728 "lame.exe to the same directory as %s\n", argv[0]
\r
730 "Install lame using your packet manager, obtain binaries or build from\n"
\r
731 "sources at http://lame.sourceforge.net/\n"
\r
737 if(opt_use_cso && system(CISO_BINARY " " NULL_REDIR) != 0)
\r
739 printf("CISO seems to be missing.\n"
\r
741 "Download ciso.exe and extract to the same directory as %s\n"
\r
742 "You can take ciso.exe from yacc at http://yacc.pspgen.com/\n", argv[0]
\r
744 "Install ciso using your packet manager, obtain binaries or build from\n"
\r
745 "sources at http://ciso.tenshu.fr/\n"
\r
751 if(load_bin_cue(cue_file) == 0)
\r
753 if(out_base == NULL)
\r
756 strncpy(out_buff, cue_file, sizeof(out_buff));
\r
757 out_buff[sizeof(out_buff)-1] = 0;
\r
758 p = strrchr(out_buff, DIR_SEPARATOR_CHAR);
\r
763 memmove(out_buff, p, strlen(p)+1);
\r
765 out_buff[strlen(out_buff)-4] = 0;
\r
766 out_base = out_buff;
\r
768 if(convert_bin_cue(out_base) != 0)
\r
773 printf("error: could not load cue file %s\n", cue_file);
\r