3 * originally written by Exophase as "bin_to_iso_ogg"
\r
4 * updated for cso/mp3 by notaz
\r
9 #include <sys/stat.h>
\r
13 #define MAX_PATH 1024
\r
17 #include <windows.h>
\r
19 #define DIR_SEPARATOR_CHAR '\\'
\r
20 #define PATH_SEPARATOR_CHAR ';'
\r
21 #define LAME_BINARY "lame.exe"
\r
22 #define CISO_BINARY "ciso.exe"
\r
23 #define NULL_REDIR "> NUL 2>&1"
\r
25 #define DIR_SEPARATOR_CHAR '/'
\r
26 #define PATH_SEPARATOR_CHAR ':'
\r
27 #define LAME_BINARY "lame"
\r
28 #define CISO_BINARY "ciso"
\r
29 #define NULL_REDIR "> /dev/null 2>&1"
\r
30 #define mkdir(x) mkdir(x, S_IRWXU)
\r
33 #define LAME_OPTIONS "-h --cbr"
\r
35 typedef unsigned char u8;
\r
36 typedef unsigned short int u16;
\r
37 typedef unsigned int u32;
\r
38 typedef unsigned long long int u64;
\r
39 typedef signed char s8;
\r
40 typedef signed short int s16;
\r
41 typedef signed int s32;
\r
42 typedef signed long long int s64;
\r
46 TRACK_FILE_TYPE_BINARY,
\r
47 TRACK_FILE_TYPE_WAVE,
\r
48 } track_file_type_enum;
\r
53 u32 physical_offset;
\r
65 track_file_type_enum type;
\r
69 } cd_track_file_struct;
\r
74 cd_track_file_struct track_files[100];
\r
79 u32 num_physical_tracks;
\r
81 s32 last_seek_track;
\r
83 cd_track_struct physical_tracks[100];
\r
84 cd_track_struct *logical_tracks[100];
\r
88 cd_bin_struct cd_bin;
\r
89 int opt_use_mp3 = 1;
\r
90 int opt_mp3_bitrate = 128;
\r
91 int opt_use_cso = 1;
\r
93 static void myexit(int code)
\r
101 char *skip_whitespace(char *str)
\r
109 s32 load_bin_cue(char *cue_file_name)
\r
111 FILE *cue_file = fopen(cue_file_name, "rb");
\r
113 printf("loading cue file %s\n", cue_file_name);
\r
117 char line_buffer[256];
\r
118 char *line_buffer_ptr;
\r
120 char bin_file_name[MAX_PATH];
\r
121 char *separator_pos;
\r
122 s32 current_physical_track_number = -1;
\r
123 u32 current_physical_offset;
\r
124 u32 current_pregap = 0;
\r
127 cd_track_struct *current_physical_track = NULL;
\r
131 // First, get filename. Only support binary right now.
\r
132 fgets(line_buffer, 255, cue_file);
\r
134 strcpy(bin_file_name, strchr(line_buffer, '"') + 1);
\r
136 *(strrchr(bin_file_name, '"')) = 0;
\r
138 // Might have to change directory first.
\r
139 separator_pos = strrchr(cue_file_name, DIR_SEPARATOR_CHAR);
\r
143 char current_dir[MAX_PATH];
\r
144 getcwd(current_dir, MAX_PATH);
\r
146 *separator_pos = 0;
\r
148 chdir(cue_file_name);
\r
151 cd_bin.bin_file = open(bin_file_name, O_RDONLY);
\r
153 cd_bin.bin_file = fopen(bin_file_name, "rb");
\r
156 printf("loaded bin file %s (%p)\n", bin_file_name, cd_bin.bin_file);
\r
158 *separator_pos = DIR_SEPARATOR_CHAR;
\r
159 chdir(current_dir);
\r
164 cd_bin.bin_file = open(bin_file_name, O_RDONLY);
\r
166 cd_bin.bin_file = fopen(bin_file_name, "rb");
\r
170 for(i = 0; i < 100; i++)
\r
172 cd_bin.logical_tracks[i] = NULL;
\r
175 cd_bin.first_track = -1;
\r
176 cd_bin.last_track = -1;
\r
177 cd_bin.num_physical_tracks = 0;
\r
178 cd_bin.num_sectors = 0;
\r
181 while(fgets(line_buffer, 256, cue_file))
\r
183 // Skip trailing whitespace
\r
184 line_buffer_ptr = skip_whitespace(line_buffer);
\r
186 // Dirty, but should work - switch on first character.
\r
187 switch(line_buffer_ptr[0])
\r
189 // New track number
\r
192 u32 new_track_number;
\r
193 char track_type[64];
\r
195 sscanf(line_buffer_ptr, "TRACK %d %s", &new_track_number,
\r
198 current_physical_track_number++;
\r
199 current_physical_track =
\r
200 cd_bin.physical_tracks + current_physical_track_number;
\r
202 current_physical_track->sector_size = 2352;
\r
204 if(!strcmp(track_type, "AUDIO"))
\r
206 current_physical_track->format_type = 0;
\r
207 current_physical_track->sector_size = 2352;
\r
210 if(!strcmp(track_type, "MODE1/2352"))
\r
212 current_physical_track->format_type = 4;
\r
213 current_physical_track->sector_size = 2352;
\r
216 if(!strcmp(track_type, "MODE1/2048"))
\r
218 current_physical_track->format_type = 4;
\r
219 current_physical_track->sector_size = 2048;
\r
222 cd_bin.logical_tracks[new_track_number] = current_physical_track;
\r
223 cd_bin.num_physical_tracks++;
\r
225 if((cd_bin.first_track == -1) ||
\r
226 (new_track_number < cd_bin.first_track))
\r
228 cd_bin.first_track = new_track_number;
\r
231 if((cd_bin.last_track == -1) ||
\r
232 (new_track_number > cd_bin.last_track))
\r
234 cd_bin.last_track = new_track_number;
\r
243 u32 minutes, seconds, frames;
\r
245 sscanf(line_buffer_ptr, "PREGAP %d:%d:%d", &minutes,
\r
246 &seconds, &frames);
\r
248 current_pregap += frames + (seconds * 75) + (minutes * 75 * 60);
\r
256 u32 minutes, seconds, frames;
\r
259 sscanf(line_buffer_ptr, "INDEX %d %d:%d:%d", &index_number,
\r
260 &minutes, &seconds, &frames);
\r
262 sector_offset = frames + (seconds * 75) + (minutes * 75 * 60);
\r
264 if(index_number == 1)
\r
266 current_physical_track->pregap_offset = current_pregap;
\r
267 current_physical_track->sector_offset = sector_offset;
\r
275 current_physical_offset = 0;
\r
277 for(i = 0; i < cd_bin.num_physical_tracks - 1; i++)
\r
279 cd_bin.physical_tracks[i].sector_count =
\r
280 cd_bin.physical_tracks[i + 1].sector_offset -
\r
281 cd_bin.physical_tracks[i].sector_offset;
\r
283 cd_bin.physical_tracks[i].physical_offset = current_physical_offset;
\r
284 current_physical_offset += (cd_bin.physical_tracks[i].sector_count *
\r
285 cd_bin.physical_tracks[i].sector_size);
\r
287 cd_bin.physical_tracks[i].sector_offset +=
\r
288 cd_bin.physical_tracks[i].pregap_offset;
\r
290 cd_bin.num_sectors += cd_bin.physical_tracks[i].sector_count;
\r
294 bin_file_size = lseek(cd_bin.bin_file, 0, SEEK_END);
\r
295 lseek(cd_bin.bin_file, 0, SEEK_SET);
\r
297 fseek(cd_bin.bin_file, 0, SEEK_END);
\r
298 bin_file_size = ftell(cd_bin.bin_file);
\r
299 fseek(cd_bin.bin_file, 0, SEEK_SET);
\r
302 // Set the last track data
\r
303 cd_bin.physical_tracks[i].physical_offset = current_physical_offset;
\r
304 cd_bin.physical_tracks[i].sector_offset +=
\r
305 cd_bin.physical_tracks[i].pregap_offset;
\r
306 cd_bin.physical_tracks[i].sector_count =
\r
307 (bin_file_size - current_physical_offset) /
\r
308 cd_bin.physical_tracks[i].sector_size;
\r
310 cd_bin.num_sectors += cd_bin.physical_tracks[i].sector_count;
\r
312 printf("finished loading cue %s\n", cue_file_name);
\r
313 printf("bin file: %s (%p)\n", bin_file_name, cd_bin.bin_file);
\r
314 printf("first track: %d, last track: %d\n", cd_bin.first_track,
\r
315 cd_bin.last_track);
\r
317 for(i = cd_bin.first_track; i <= cd_bin.last_track; i++)
\r
319 printf("track %d (%p):\n", i, cd_bin.logical_tracks[i]);
\r
320 if(cd_bin.logical_tracks[i] == NULL)
\r
322 printf(" (invalid)\n");
\r
326 printf(" physical offset 0x%x\n",
\r
327 cd_bin.logical_tracks[i]->physical_offset);
\r
328 printf(" sector offset 0x%x\n",
\r
329 cd_bin.logical_tracks[i]->sector_offset);
\r
330 printf(" sector size %d\n",
\r
331 cd_bin.logical_tracks[i]->sector_size);
\r
335 cd_bin.last_seek_track = 0;
\r
344 #define address8(base, offset) \
\r
345 *((u8 *)((u8 *)base + (offset))) \
\r
347 #define address16(base, offset) \
\r
348 *((u16 *)((u8 *)base + (offset))) \
\r
350 #define address32(base, offset) \
\r
351 *((u32 *)((u8 *)base + (offset))) \
\r
353 // This will only work on little endian platforms for now.
\r
355 s32 convert_bin_to_wav(FILE *bin_file, char *output_dir, char *wav_file_name,
\r
360 u8 *riff_header = wav_header + 0;
\r
361 u8 *fmt_header = wav_header + 0x0C;
\r
362 u8 sector_buffer[2352];
\r
363 u32 byte_length = sector_count * 2352;
\r
367 wav_file = fopen(wav_file_name, "wb");
\r
369 printf("writing wav %s, %x sectors\n", wav_file_name, sector_count);
\r
372 memcpy(riff_header + 0x00, "RIFF", 4);
\r
373 address32(riff_header, 0x04) = byte_length + 44 - 8;
\r
374 memcpy(riff_header + 0x08, "WAVE", 4);
\r
376 // WAVE file chunk: format
\r
377 memcpy(fmt_header + 0x00, "fmt ", 4);
\r
379 address32(fmt_header, 0x04) = 16;
\r
380 // Compression code: PCM
\r
381 address16(fmt_header, 0x08) = 1;
\r
382 // Number of channels: Stereo
\r
383 address16(fmt_header, 0x0a) = 2;
\r
384 // Sample rate: 44100Hz
\r
385 address32(fmt_header, 0x0c) = 44100;
\r
386 // Average bytes per second: sample rate * 4
\r
387 address32(fmt_header, 0x10) = 44100 * 4;
\r
388 // Block align (bytes per sample)
\r
389 address16(fmt_header, 0x14) = 4;
\r
391 address16(fmt_header, 0x16) = 16;
\r
393 // Write out header
\r
394 fwrite(wav_header, 36, 1, wav_file);
\r
397 fprintf(wav_file, "data");
\r
399 fwrite(&byte_length, 4, 1, wav_file);
\r
401 // Write out sectors
\r
402 for(i = 0; i < sector_count; i++)
\r
404 printf("\b\b\b%3i", i*100 / sector_count);
\r
406 fread(sector_buffer, 2352, 1, bin_file);
\r
407 fwrite(sector_buffer, 2352, 1, wav_file);
\r
409 printf("\b\b\b100\n");
\r
416 void convert_wav_to_ogg(char *wav_file_name, char *output_dir,
\r
417 char *ogg_file_name)
\r
419 char cmd_string[(MAX_PATH * 2) + 16];
\r
422 sprintf(cmd_string, "oggenc %s", wav_file_name);
\r
423 system(cmd_string);
\r
425 unlink(wav_file_name);
\r
429 void convert_wav_to_mp3(char *wav_file_name, char *output_dir,
\r
430 char *mp3_file_name)
\r
432 char cmd_string[(MAX_PATH * 2) + 16];
\r
435 sprintf(cmd_string, LAME_BINARY " " LAME_OPTIONS " -b %i \"%s\" \"%s\"",
\r
436 opt_mp3_bitrate, wav_file_name, mp3_file_name);
\r
437 if (system(cmd_string) != 0)
\r
439 printf("failed to encode mp3\n");
\r
443 unlink(wav_file_name);
\r
447 s32 convert_bin_to_iso(FILE *bin_file, char *output_dir, char *iso_file_name,
\r
451 u8 sector_buffer[2352];
\r
455 iso_file = fopen(iso_file_name, "wb");
\r
456 if (iso_file == NULL)
\r
458 printf("failed to open: %s\n", iso_file_name);
\r
461 printf("writing iso %s, %x sectors\n", iso_file_name, sector_count);
\r
463 for(i = 0; i < sector_count; i++)
\r
465 printf("\b\b\b%3i", i*100 / sector_count);
\r
467 fread(sector_buffer, 2352, 1, bin_file);
\r
468 fwrite(sector_buffer + 16, 2048, 1, iso_file);
\r
470 printf("\b\b\b100\n");
\r
477 void convert_iso_to_cso(char *output_dir, char *iso_file_name, char *cso_file_name)
\r
479 char cmd_string[(MAX_PATH * 2) + 16];
\r
482 sprintf(cmd_string, CISO_BINARY " 9 \"%s\" \"%s\"", iso_file_name, cso_file_name);
\r
483 if (system(cmd_string) != 0)
\r
485 printf("failed to convert iso to cso\n");
\r
489 unlink(iso_file_name);
\r
494 #define sector_offset_to_msf(offset, minutes, seconds, frames) \
\r
496 u32 _offset = offset; \
\r
497 minutes = (_offset / 75) / 60; \
\r
498 seconds = (_offset / 75) % 60; \
\r
499 frames = _offset % 75; \
\r
503 s32 convert_bin_cue(char *output_name_base)
\r
505 char output_file_name[MAX_PATH];
\r
506 FILE *output_cue_file;
\r
507 FILE *bin_file = cd_bin.bin_file;
\r
508 cd_track_struct *current_track;
\r
510 u32 current_pregap = 0;
\r
511 u32 last_pregap = 0;
\r
515 if(stat(output_name_base, &sb))
\r
516 mkdir(output_name_base);
\r
518 sprintf(output_file_name, "%s.cue", output_name_base);
\r
519 chdir(output_name_base);
\r
520 output_cue_file = fopen(output_file_name, "wb");
\r
523 // Every track gets its own file. It's either going to be of type ISO
\r
526 for(i = 0; i < 100; i++)
\r
528 current_track = cd_bin.logical_tracks[i];
\r
529 if(current_track != NULL)
\r
531 switch(current_track->format_type)
\r
533 char output_name_tmp[MAX_PATH];
\r
538 sprintf(output_file_name, "%s_%02d.mp3", output_name_base, i);
\r
539 sprintf(output_name_tmp, "%s_%02d.wav", output_name_base, i);
\r
541 fprintf(output_cue_file, "FILE \"%s\" %s\n",
\r
542 opt_use_mp3 ? output_file_name : output_name_tmp,
\r
543 opt_use_mp3 ? "MP3" : "WAVE");
\r
544 fprintf(output_cue_file, " TRACK %02d AUDIO\n", i);
\r
545 current_pregap = current_track->pregap_offset - last_pregap;
\r
546 last_pregap = current_track->pregap_offset;
\r
547 if(current_pregap > 0)
\r
549 sector_offset_to_msf(current_pregap, m, s, f);
\r
550 fprintf(output_cue_file, " PREGAP %02d:%02d:%02d\n", m, s, f);
\r
552 fprintf(output_cue_file, " INDEX 01 00:00:00\n");
\r
553 sector_offset_to_msf(current_track->sector_count, m, s, f);
\r
554 fprintf(output_cue_file, " REM LENGTH %02d:%02d:%02d\n", m, s, f);
\r
556 fseek(bin_file, current_track->physical_offset, SEEK_SET);
\r
557 convert_bin_to_wav(bin_file, output_name_base, output_name_tmp,
\r
558 current_track->sector_count);
\r
561 convert_wav_to_mp3(output_name_tmp, output_name_base,
\r
569 sprintf(output_file_name, "%s_%02d.cso", output_name_base, i);
\r
570 sprintf(output_name_tmp, "%s_%02d.iso", output_name_base, i);
\r
571 fprintf(output_cue_file, "FILE \"%s\" BINARY\n",
\r
572 opt_use_cso ? output_file_name : output_name_tmp);
\r
573 fprintf(output_cue_file, " TRACK %02d MODE1/2048\n", i);
\r
574 current_pregap = current_track->pregap_offset - last_pregap;
\r
575 last_pregap = current_track->pregap_offset;
\r
576 if(current_pregap > 0)
\r
578 sector_offset_to_msf(current_pregap, m, s, f);
\r
579 fprintf(output_cue_file, " PREGAP %02d:%02d:%02d\n", m, s, f);
\r
581 fprintf(output_cue_file, " INDEX 01 00:00:00\n");
\r
583 fseek(bin_file, current_track->physical_offset, SEEK_SET);
\r
584 convert_bin_to_iso(bin_file, output_name_base, output_name_tmp,
\r
585 current_track->sector_count);
\r
588 convert_iso_to_cso(output_name_base, output_name_tmp, output_file_name);
\r
595 fclose(output_cue_file);
\r
601 static void update_path(void)
\r
603 char buff1[MAX_PATH], buff2[MAX_PATH];
\r
607 path = getenv("PATH");
\r
608 GetModuleFileNameA(NULL, buff1, sizeof(buff1));
\r
609 for (i = strlen(buff1)-1; i > 0; i--)
\r
610 if (buff1[i] == '\\') break;
\r
613 snprintf(buff2, sizeof(buff2), "%s;%s", path, buff1);
\r
614 SetEnvironmentVariableA("PATH", buff2);
\r
618 int main(int argc, char *argv[])
\r
620 char out_buff[MAX_PATH], *cue_file, *out_base;
\r
625 printf("bin/cue to cso/mp3 converter\n");
\r
626 printf("usage: %s [options] <input cue> [output base]\n", argv[0]);
\r
627 printf("options:\n"
\r
628 " -m output mp3 files for audio (default) (lame required)\n"
\r
629 " -b <rate> mp3 bitrate to use (default is 128)\n"
\r
630 " -w output wav files for audio\n"
\r
631 " -c output cso as data track (default) (ciso required)\n"
\r
632 " -i output iso as data track\n");
\r
636 for (a = 1; a < argc - 1; a++)
\r
638 if (strcmp(argv[a], "-m") == 0)
\r
640 else if (strcmp(argv[a], "-w") == 0)
\r
642 else if (strcmp(argv[a], "-c") == 0)
\r
644 else if (strcmp(argv[a], "-i") == 0)
\r
646 else if (strcmp(argv[a], "-b") == 0)
\r
648 opt_mp3_bitrate = atoi(argv[++a]);
\r
653 cue_file = argv[a];
\r
654 out_base = argv[a+1];
\r
656 /* some sanity checks */
\r
657 if(strlen(cue_file) < 4 || strcasecmp(cue_file + strlen(cue_file) - 4, ".cue") != 0)
\r
659 printf("error: not a cue file specified?\n");
\r
667 if(opt_use_mp3 && system(LAME_BINARY " --help " NULL_REDIR) != 0)
\r
669 printf("LAME seems to be missing.\n"
\r
671 "Download from http://lame.sourceforge.net/links.php#Binaries and extract\n"
\r
672 "lame.exe to the same directory as %s\n", argv[0]
\r
674 "Install lame using your packet manager, obtain binaries or build from\n"
\r
675 "sources at http://lame.sourceforge.net/\n"
\r
681 if(opt_use_cso && system(CISO_BINARY " " NULL_REDIR) != 0)
\r
683 printf("CISO seems to be missing.\n"
\r
685 "Download ciso.exe and extract to the same directory as %s\n"
\r
686 "You can take ciso.exe from yacc at http://yacc.pspgen.com/\n", argv[0]
\r
688 "Install ciso using your packet manager, obtain binaries or build from\n"
\r
689 "sources at http://ciso.tenshu.fr/\n"
\r
695 if(load_bin_cue(cue_file) == 0)
\r
697 if(out_base == NULL)
\r
700 strncpy(out_buff, cue_file, sizeof(out_buff));
\r
701 out_buff[sizeof(out_buff)-1] = 0;
\r
702 p = strrchr(out_buff, DIR_SEPARATOR_CHAR);
\r
707 memmove(out_buff, p, strlen(p)+1);
\r
709 out_buff[strlen(out_buff)-4] = 0;
\r
710 out_base = out_buff;
\r
712 if(convert_bin_cue(out_base) != 0)
\r
717 printf("error: could not load cue file %s\n", cue_file);
\r