Changeset 357 for libmpc/trunk/mpc2sv8/mpc2sv8.c
- Timestamp:
- 10/01/07 18:54:42 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libmpc/trunk/mpc2sv8/mpc2sv8.c
r353 r357 35 35 #include <mpc/mpcdec.h> 36 36 #include <mpc/minimax.h> 37 #include <getopt.h> 38 39 #include <sys/types.h> 40 #include <dirent.h> 41 #include <libgen.h> 37 42 38 43 #include "../libmpcdec/decoder.h" … … 44 49 #define MPC2SV8_MAJOR 0 45 50 #define MPC2SV8_MINOR 9 46 #define MPC2SV8_BUILD 051 #define MPC2SV8_BUILD 1 47 52 48 53 #define _cat(a,b,c) #a"."#b"."#c … … 82 87 usage(const char *exename) 83 88 { 84 printf("Usage: %s <infile.mpc> <outfile.mpc>\n", exename); 85 } 86 87 int 88 main(int argc, char **argv) 89 { 90 mpc_reader reader; 89 printf("Usage:\n" 90 "%s <infile.mpc> <outfile.mpc>\n" 91 "or\n" 92 "%s <infile_1.mpc> [ <infile_2.mpc> ... <infile_n.mpc> ] <outdir>\n", exename, exename); 93 } 94 95 int convert(char * sv7file, char * sv8file) 96 { 97 mpc_reader reader; 91 98 mpc_demux* demux; 92 99 mpc_streaminfo si; … … 99 106 char buf[TMP_BUF_SIZE]; 100 107 101 printf(About); 102 if(3 != argc) { 103 usage(argv[0]); 104 return 0; 105 } 106 107 err = mpc_reader_init_stdio(&reader, argv[1]); 108 if(err < 0) return !MPC_STATUS_OK; 109 110 demux = mpc_demux_init(&reader); 111 if(!demux) return !MPC_STATUS_OK; 112 mpc_demux_get_info(demux, &si); 108 err = mpc_reader_init_stdio(&reader, sv7file); 109 if(err < 0) return err; 110 111 demux = mpc_demux_init(&reader); 112 if(!demux) { 113 err = !MPC_STATUS_OK; 114 goto READER_ERR; 115 } 116 mpc_demux_get_info(demux, &si); 113 117 114 118 if (si.stream_version >= 8) { 115 fprintf(stderr, "Error : the file \"%s\" is already a sv8 file\n", argv[1]); 116 exit(MPC_STATUS_INVALIDSV); 119 fprintf(stderr, "Error : the file \"%s\" is already a sv8 file\n", sv7file); 120 err = !MPC_STATUS_OK; 121 goto DEMUX_ERR; 117 122 } 118 123 119 124 mpc_encoder_init(&e, si.samples, 6, 1); 120 e.outputFile = fopen( argv[2], "rb" ); 121 if ( e.outputFile != 0 ) { 122 fprintf(stderr, "Error : output file \"%s\" already exists\n", argv[2]); 123 exit(MPC_STATUS_FILE); 124 } 125 e.outputFile = fopen( argv[2], "w+b" ); 125 e.outputFile = fopen( sv8file, "w+b" ); 126 126 e.MS_Channelmode = si.ms; 127 127 128 128 // copy begining of file 129 in_file = fopen(argv[1], "rb"); 130 if(in_file == 0) return !MPC_STATUS_OK; 129 in_file = fopen(sv7file, "rb"); 130 if(in_file == 0) { 131 err = !MPC_STATUS_OK; 132 goto OUT_FILE_ERR; 133 } 131 134 r_size = si.header_position; 132 135 while(r_size) { 133 136 size_t tmp_size = fread(buf, 1, mini(TMP_BUF_SIZE, r_size), in_file); 134 137 if (fwrite(buf, 1, tmp_size, e.outputFile) != tmp_size) { 135 fprintf(stderr, "Error writing to target file : \"%s\"\n", argv[2]); 136 exit(MPC_STATUS_FILE); 138 fprintf(stderr, "Error writing to target file : \"%s\"\n", sv8file); 139 err = MPC_STATUS_FILE; 140 goto IN_FILE_ERR; 137 141 } 138 142 r_size -= tmp_size; … … 143 147 writeMagic(&e); 144 148 writeStreamInfo( &e, si.max_band, si.ms > 0, si.samples, 0, si.sample_freq, 145 149 si.channels); 146 150 si_size = writeBlock(&e, "SH", MPC_TRUE, 0); 147 151 writeGainInfo(&e, si.gain_title, si.peak_title, si.gain_album, si.peak_album); 148 152 si_size = writeBlock(&e, "RG", MPC_FALSE, 0); 149 153 writeEncoderInfo(&e, si.profile, si.pns, si.encoder_version / 100, 150 154 si.encoder_version % 100, 0); 151 155 writeBlock(&e, "EI", MPC_FALSE, 0); 152 156 e.seek_ptr = ftell(e.outputFile); … … 185 189 fseek(e.outputFile, e.seek_ref + 4, SEEK_SET); 186 190 writeStreamInfo( &e, si.max_band, si.ms > 0, demux->d->samples, 0, 187 191 si.sample_freq, si.channels); 188 192 writeBlock(&e, "SH", MPC_TRUE, si_size); 189 193 fseek(e.outputFile, 0, SEEK_END); … … 201 205 } 202 206 207 IN_FILE_ERR: 208 fclose ( in_file ); 209 OUT_FILE_ERR: 203 210 fclose ( e.outputFile ); 204 fclose ( in_file ); 211 mpc_encoder_exit(&e); 212 DEMUX_ERR: 205 213 mpc_demux_exit(demux); 214 READER_ERR: 206 215 mpc_reader_exit_stdio(&reader); 207 mpc_encoder_exit(&e);208 216 209 217 return err; 210 218 } 219 220 mpc_bool_t is_dir(char * dir_path) 221 { 222 DIR * out_dir = opendir(dir_path); 223 if (out_dir != 0) { 224 closedir(out_dir); 225 return MPC_TRUE; 226 } 227 return MPC_FALSE; 228 } 229 230 int 231 main(int argc, char **argv) 232 { 233 int c, i; 234 mpc_bool_t overwrite = MPC_FALSE, use_dir = MPC_FALSE; 235 printf(About); 236 int ret = MPC_STATUS_OK; 237 238 while ((c = getopt(argc , argv, "oh")) != -1) { 239 switch (c) { 240 case 'o': 241 overwrite = MPC_TRUE; 242 break; 243 case 'h': 244 usage(argv[0]); 245 return 0; 246 } 247 } 248 249 use_dir = is_dir(argv[argc - 1]); 250 251 if((argc - optind) < 2 || (use_dir == MPC_FALSE && (argc - optind) > 2)) { 252 usage(argv[0]); 253 return 0; 254 } 255 256 for (i = optind; i < argc - 1; i++) { 257 char * in_file = argv[i]; 258 char * out_file = argv[argc - 1]; 259 if (use_dir == MPC_TRUE) { 260 char * file_name = basename(in_file); 261 out_file = malloc(strlen(file_name) + strlen(argv[argc - 1]) + 2); 262 sprintf(out_file, "%s/%s", argv[argc - 1], file_name); 263 } 264 if (overwrite == MPC_FALSE) { 265 FILE * test_file = fopen( out_file, "rb" ); 266 if ( test_file != 0 ) { 267 fprintf(stderr, "Error : output file \"%s\" already exists\n", out_file); 268 fclose(test_file); 269 continue; 270 } 271 } 272 // FIXME : test if in and out files are the same 273 ret = convert(in_file, out_file); 274 if (use_dir == MPC_TRUE) 275 free(out_file); 276 } 277 278 return ret; 279 }
Note: See TracChangeset
for help on using the changeset viewer.