| 1 | /* Extract - monster.sou to MP3-compressed monster.so3 converter
|
|---|
| 2 | * Copyright (C) 2002-2005 The ScummVM Team
|
|---|
| 3 | *
|
|---|
| 4 | * This program is free software; you can redistribute it and/or
|
|---|
| 5 | * modify it under the terms of the GNU General Public License
|
|---|
| 6 | * as published by the Free Software Foundation; either version 2
|
|---|
| 7 | * of the License, or (at your option) any later version.
|
|---|
| 8 |
|
|---|
| 9 | * This program is distributed in the hope that it will be useful,
|
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | * GNU General Public License for more details.
|
|---|
| 13 |
|
|---|
| 14 | * You should have received a copy of the GNU General Public License
|
|---|
| 15 | * along with this program; if not, write to the Free Software
|
|---|
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|---|
| 17 | *
|
|---|
| 18 | * $Header: /cvsroot/scummvm/tools/compress_scumm_sou.c,v 1.4 2005/01/01 10:33:29 aquadran Exp $
|
|---|
| 19 | *
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include "compress.h"
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 | static const char f_hdr[] = {
|
|---|
| 26 | 'S', 'O', 'U', ' ', 0, 0, 0, 0, 0
|
|---|
| 27 | };
|
|---|
| 28 |
|
|---|
| 29 | #define OUTPUT_MP3 "monster.so3"
|
|---|
| 30 | #define OUTPUT_OGG "monster.sog"
|
|---|
| 31 | #define OUTPUT_FLAC "monster.sof"
|
|---|
| 32 |
|
|---|
| 33 | static const char *outputName = OUTPUT_MP3;
|
|---|
| 34 |
|
|---|
| 35 | #define TEMP_DAT "tempfile.dat"
|
|---|
| 36 | #define TEMP_IDX "tempfile.idx"
|
|---|
| 37 |
|
|---|
| 38 | static FILE *input, *output_idx, *output_snd;
|
|---|
| 39 |
|
|---|
| 40 | static CompressMode gCompMode = kMP3Mode;
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 | void end_of_file(void)
|
|---|
| 44 | {
|
|---|
| 45 | FILE *in;
|
|---|
| 46 | int idx_size = ftell(output_idx);
|
|---|
| 47 | size_t size;
|
|---|
| 48 | char buf[2048];
|
|---|
| 49 |
|
|---|
| 50 | fclose(output_snd);
|
|---|
| 51 | fclose(output_idx);
|
|---|
| 52 |
|
|---|
| 53 | output_idx = fopen(outputName , "wb");
|
|---|
| 54 | writeUint32BE(output_idx, (uint32)idx_size);
|
|---|
| 55 |
|
|---|
| 56 | in = fopen(TEMP_IDX, "rb");
|
|---|
| 57 | while ((size = fread(buf, 1, 2048, in)) > 0) {
|
|---|
| 58 | fwrite(buf, 1, size, output_idx);
|
|---|
| 59 | }
|
|---|
| 60 | fclose(in);
|
|---|
| 61 | in = fopen(TEMP_DAT, "rb");
|
|---|
| 62 | while ((size = fread(buf, 1, 2048, in)) > 0) {
|
|---|
| 63 | fwrite(buf, 1, size, output_idx);
|
|---|
| 64 | }
|
|---|
| 65 | fclose(in);
|
|---|
| 66 | fclose(output_idx);
|
|---|
| 67 | fclose(input);
|
|---|
| 68 |
|
|---|
| 69 | /* And some clean-up :-) */
|
|---|
| 70 | unlink(TEMP_IDX);
|
|---|
| 71 | unlink(TEMP_DAT);
|
|---|
| 72 | unlink(TEMP_RAW);
|
|---|
| 73 | unlink(tempEncoded);
|
|---|
| 74 |
|
|---|
| 75 | exit(-1);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | void append_byte(int size, char buf[])
|
|---|
| 79 | {
|
|---|
| 80 | int i;
|
|---|
| 81 | for (i = 0; i < (size - 1); i++)
|
|---|
| 82 | buf[i] = buf[i + 1];
|
|---|
| 83 | buf[i] = fgetc(input);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | void get_part(void)
|
|---|
| 87 | {
|
|---|
| 88 | FILE *f;
|
|---|
| 89 | uint32 tot_size;
|
|---|
| 90 | char outname[256];
|
|---|
| 91 | int size;
|
|---|
| 92 | char fbuf[2048];
|
|---|
| 93 |
|
|---|
| 94 | char buf[2048];
|
|---|
| 95 | int pos = ftell(input);
|
|---|
| 96 | uint32 tags;
|
|---|
| 97 |
|
|---|
| 98 | /* Scan for the VCTL header */
|
|---|
| 99 | fread(buf, 1, 4, input);
|
|---|
| 100 | /* Note, that both "Sam & Max" demo and floppy disk versions use VTTL */
|
|---|
| 101 | while (memcmp(buf, "VCTL", 4)&&memcmp(buf, "VTTL", 4)) {
|
|---|
| 102 | pos++;
|
|---|
| 103 | append_byte(4, buf);
|
|---|
| 104 | if (feof(input))
|
|---|
| 105 | end_of_file();
|
|---|
| 106 | }
|
|---|
| 107 | tags = readUint32BE(input);
|
|---|
| 108 | if (tags < 8)
|
|---|
| 109 | exit(-1);
|
|---|
| 110 | tags -= 8;
|
|---|
| 111 |
|
|---|
| 112 | writeUint32BE(output_idx, (uint32)pos);
|
|---|
| 113 | writeUint32BE(output_idx, (uint32)ftell(output_snd));
|
|---|
| 114 | writeUint32BE(output_idx, tags);
|
|---|
| 115 | while (tags > 0) {
|
|---|
| 116 | fputc(fgetc(input), output_snd);
|
|---|
| 117 | tags--;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | fread(buf, 1, 8, input);
|
|---|
| 121 | if (!memcmp(buf, "Creative", 8)) {
|
|---|
| 122 | fseek(input, 18, SEEK_CUR);
|
|---|
| 123 | } else if (!memcmp(buf, "VTLK", 4)) {
|
|---|
| 124 | fseek(input, 26, SEEK_CUR);
|
|---|
| 125 | } else {
|
|---|
| 126 | error("Unexpected data encountered");
|
|---|
| 127 | }
|
|---|
| 128 | printf("Voice file found (pos = %d) :", pos);
|
|---|
| 129 |
|
|---|
| 130 | /* Conver the VOC data */
|
|---|
| 131 | extractAndEncodeVOC(TEMP_RAW, input, gCompMode);
|
|---|
| 132 |
|
|---|
| 133 | /* Append the converted data to the master output file */
|
|---|
| 134 | sprintf(outname, tempEncoded);
|
|---|
| 135 | f = fopen(outname, "rb");
|
|---|
| 136 | tot_size = 0;
|
|---|
| 137 | while ((size = fread(fbuf, 1, 2048, f)) > 0) {
|
|---|
| 138 | tot_size += size;
|
|---|
| 139 | fwrite(fbuf, 1, size, output_snd);
|
|---|
| 140 | }
|
|---|
| 141 | fclose(f);
|
|---|
| 142 |
|
|---|
| 143 | writeUint32BE(output_idx, tot_size);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | void showhelp(char *exename)
|
|---|
| 147 | {
|
|---|
| 148 | printf("\nUsage: %s <params> monster.sou\n", exename);
|
|---|
| 149 |
|
|---|
| 150 | printf("\nParams:\n");
|
|---|
| 151 | printf(" --mp3 encode to MP3 format (default)\n");
|
|---|
| 152 | printf(" --vorbis encode to Vorbis format\n");
|
|---|
| 153 | printf(" --flac encode to Flac format\n");
|
|---|
| 154 | printf("(If one of these is specified, it must be the first parameter.)\n");
|
|---|
| 155 |
|
|---|
| 156 | printf("\nMP3 mode params:\n");
|
|---|
| 157 | printf(" -b <rate> <rate> is the target bitrate(ABR)/minimal bitrate(VBR) (default:%d)\n", minBitrDef);
|
|---|
| 158 | printf(" -B <rate> <rate> is the maximum VBR/ABR bitrate (default:%d)\n", maxBitrDef);
|
|---|
| 159 | printf(" --vbr LAME uses the VBR mode (default)\n");
|
|---|
| 160 | printf(" --abr LAME uses the ABR mode\n");
|
|---|
| 161 | printf(" -V <value> specifies the value (0 - 9) of VBR quality (0=best) (default:%d)\n", vbrqualDef);
|
|---|
| 162 | printf(" -q <value> specifies the MPEG algorithm quality (0-9; 0=best) (default:%d)\n", algqualDef);
|
|---|
| 163 | printf(" --silent the output of LAME is hidden (default:disabled)\n");
|
|---|
| 164 |
|
|---|
| 165 | printf("\nVorbis mode params:\n");
|
|---|
| 166 | printf(" -b <rate> <rate> is the nominal bitrate (default:unset)\n");
|
|---|
| 167 | printf(" -m <rate> <rate> is the minimum bitrate (default:unset)\n");
|
|---|
| 168 | printf(" -M <rate> <rate> is the maximum bitrate (default:unset)\n");
|
|---|
| 169 | printf(" -q <value> specifies the value (0 - 10) of VBR quality (10=best) (default:%d)\n", oggqualDef);
|
|---|
| 170 | printf(" --silent the output of oggenc is hidden (default:disabled)\n");
|
|---|
| 171 |
|
|---|
| 172 | printf("\nFlac mode params:\n");
|
|---|
| 173 | printf(" [params] optional arguments passed directly to the encoder\n");
|
|---|
| 174 | printf(" recommended is: --best -b 1152\n");
|
|---|
| 175 |
|
|---|
| 176 | printf("\n --help this help message\n");
|
|---|
| 177 |
|
|---|
| 178 | printf("\n\nIf a parameter is not given the default value is used\n");
|
|---|
| 179 | printf("If using VBR mode for MP3 -b and -B must be multiples of 8; the maximum is 160!\n");
|
|---|
| 180 | exit(2);
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | int main(int argc, char *argv[])
|
|---|
| 184 | {
|
|---|
| 185 | char buf[2048];
|
|---|
| 186 | int i;
|
|---|
| 187 | if (argc < 2)
|
|---|
| 188 | showhelp(argv[0]);
|
|---|
| 189 | /* Compression mode */
|
|---|
| 190 | gCompMode = kMP3Mode;
|
|---|
| 191 | i = 1;
|
|---|
| 192 | if (strcmp(argv[1], "--mp3") == 0) {
|
|---|
| 193 | gCompMode = kMP3Mode;
|
|---|
| 194 | i++;
|
|---|
| 195 | }
|
|---|
| 196 | else if (strcmp(argv[1], "--vorbis") == 0) {
|
|---|
| 197 | gCompMode = kVorbisMode;
|
|---|
| 198 | i++;
|
|---|
| 199 | }
|
|---|
| 200 | else if (strcmp(argv[1], "--flac") == 0) {
|
|---|
| 201 | gCompMode = kFlacMode;
|
|---|
| 202 | i++;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | switch (gCompMode) {
|
|---|
| 206 | case kMP3Mode:
|
|---|
| 207 | outputName = OUTPUT_MP3;
|
|---|
| 208 | tempEncoded = TEMP_MP3;
|
|---|
| 209 | process_mp3_parms(argc, argv, i);
|
|---|
| 210 | break;
|
|---|
| 211 | case kVorbisMode:
|
|---|
| 212 | outputName = OUTPUT_OGG;
|
|---|
| 213 | tempEncoded = TEMP_OGG;
|
|---|
| 214 | process_ogg_parms(argc, argv, i);
|
|---|
| 215 | break;
|
|---|
| 216 | case kFlacMode:
|
|---|
| 217 | outputName = OUTPUT_FLAC;
|
|---|
| 218 | tempEncoded = TEMP_FLAC;
|
|---|
| 219 | process_flac_parms(argc, argv, i);
|
|---|
| 220 | break;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 | i = argc - 1;
|
|---|
| 225 | input = fopen(argv[i], "rb");
|
|---|
| 226 | if (!input) {
|
|---|
| 227 | printf("Cannot open file: %s\n", argv[i]);
|
|---|
| 228 | exit(-1);
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | output_idx = fopen(TEMP_IDX, "wb");
|
|---|
| 232 | if (!output_idx) {
|
|---|
| 233 | printf("Can't open file " TEMP_IDX " for write!\n" );
|
|---|
| 234 | exit(-1);
|
|---|
| 235 | }
|
|---|
| 236 | output_snd = fopen(TEMP_DAT, "wb");
|
|---|
| 237 | if (!output_snd) {
|
|---|
| 238 | printf("Can't open file " TEMP_DAT " for write!\n");
|
|---|
| 239 | exit(-1);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | /* Get the 'SOU ....' header */
|
|---|
| 243 | fread(buf, 1, 8, input);
|
|---|
| 244 | if (strncmp(buf, f_hdr, 8)) {
|
|---|
| 245 | printf("Bad SOU\n");
|
|---|
| 246 | exit(-1);
|
|---|
| 247 | }
|
|---|
| 248 | while (1)
|
|---|
| 249 | get_part();
|
|---|
| 250 | return 0;
|
|---|
| 251 | }
|
|---|