00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00032 #include "splt.h"
00033
00034 #include "mp3.h"
00035 #include "mp3_silence.h"
00036 #include "mp3_utils.h"
00037
00038
00039
00040
00045 static const char *splt_mp3_chan[] =
00046 {
00047 "Mono",
00048 "Dual Mono",
00049 "Joint Stereo",
00050 "Stereo",
00051 "?"
00052 };
00053
00057 static const unsigned long splt_mp3_crctab[256] = {
00058 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba,
00059 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
00060 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
00061 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
00062 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
00063 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
00064 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec,
00065 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
00066 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
00067 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
00068 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940,
00069 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
00070 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116,
00071 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
00072 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
00073 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
00074 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a,
00075 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
00076 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818,
00077 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
00078 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
00079 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
00080 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c,
00081 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
00082 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
00083 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
00084 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
00085 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
00086 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086,
00087 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
00088 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4,
00089 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
00090 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
00091 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
00092 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
00093 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
00094 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe,
00095 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
00096 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
00097 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
00098 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252,
00099 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
00100 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60,
00101 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
00102 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
00103 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
00104 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04,
00105 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
00106 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a,
00107 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
00108 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
00109 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
00110 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e,
00111 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
00112 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
00113 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
00114 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
00115 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
00116 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0,
00117 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
00118 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6,
00119 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
00120 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
00121 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d,
00122 };
00123
00124 static void splt_mp3_save_end_point(splt_state *state, splt_mp3_state *mp3state,
00125 int save_end_point, off_t end)
00126 {
00127 mp3state->end_non_zero = end;
00128
00129 if (save_end_point)
00130 {
00131 mp3state->end = end;
00132 }
00133 else
00134 {
00135 mp3state->end = 0;
00136 }
00137 }
00138
00146 static FILE *splt_mp3_open_file_read(splt_state *state, const char *filename,
00147 int *error)
00148 {
00149 FILE *file_input = NULL;
00150
00151 if (filename != NULL && ((strcmp(filename,"-") == 0) ||
00152 (strcmp(filename,"m-") == 0)))
00153 {
00154 file_input = stdin;
00155 #ifdef __WIN32__
00156 _setmode(fileno(file_input), _O_BINARY);
00157 #endif
00158 }
00159 else
00160 {
00161 file_input = splt_io_fopen(filename, "rb");
00162 if (file_input == NULL)
00163 {
00164 splt_e_set_strerror_msg_with_data(state, filename);
00165 *error = SPLT_ERROR_CANNOT_OPEN_FILE;
00166 }
00167 }
00168
00169 return file_input;
00170 }
00171
00175 static FILE *splt_mp3_open_file_write(splt_state *state, const char *output_fname, int *error)
00176 {
00177 FILE *file_output = NULL;
00178
00179
00180 if (strcmp(output_fname, "-")==0)
00181 {
00182 file_output = stdout;
00183 #ifdef __WIN32__
00184 _setmode(fileno(file_output), _O_BINARY);
00185 #endif
00186 }
00187 else
00188 {
00189 if (!(file_output = splt_io_fopen(output_fname, "wb+")))
00190 {
00191 splt_e_set_strerror_msg_with_data(state, output_fname);
00192 *error = SPLT_ERROR_CANNOT_OPEN_DEST_FILE;
00193 }
00194 }
00195
00196 return file_output;
00197 }
00198
00199
00200
00201
00203 static unsigned long splt_mp3_c_crc(splt_state *state,
00204 FILE *in, off_t begin, off_t end, int *error)
00205 {
00206 register unsigned long crc;
00207 int c;
00208
00209 crc = 0xFFFFFFFF;
00210
00211 if (fseeko(in, begin, SEEK_SET) == -1)
00212 {
00213 splt_e_set_strerror_msg_with_data(state, splt_t_get_filename_to_split(state));
00214 *error = SPLT_ERROR_SEEKING_FILE;
00215 return 0;
00216 }
00217
00218 while(begin++ < end)
00219 {
00220 c = fgetc(in);
00221 crc = ((crc >> 8) & 0x00FFFFFF) ^ splt_mp3_crctab[(crc ^ c) & 0xFF];
00222 }
00223
00224 return (crc ^ 0xFFFFFFFF);
00225 }
00226
00231 static void splt_mp3_state_free(splt_state *state)
00232 {
00233 splt_mp3_state *mp3state = state->codec;
00234
00235 if (mp3state)
00236 {
00237 if (mp3state->mp3file.xingbuffer)
00238 {
00239 free(mp3state->mp3file.xingbuffer);
00240 mp3state->mp3file.xingbuffer = NULL;
00241 }
00242
00243
00244 free(mp3state);
00245 state->codec = NULL;
00246 }
00247 }
00248
00249
00250
00251
00260 static int splt_mp3_getid3v1_offset(FILE *file_input)
00261 {
00262 if (fseeko(file_input, (off_t) -128, SEEK_END)==-1)
00263 {
00264 return 0;
00265 }
00266
00267 if (fgetc(file_input)=='T')
00268 if (fgetc(file_input)=='A')
00269 if (fgetc(file_input)=='G')
00270 return -128;
00271
00272 return 0;
00273 }
00274
00283 static off_t splt_mp3_getid3v2_end_offset(FILE *in, off_t start)
00284 {
00285 unsigned long oword = 0;
00286 if (fseeko(in, start, SEEK_SET)==-1)
00287 {
00288 return 0;
00289 }
00290
00291 if (fgetc(in)=='I')
00292 if (fgetc(in)=='D')
00293 if (fgetc(in)=='3')
00294 {
00295 int i;
00296 if (fseeko(in, (off_t) 3, SEEK_CUR)==-1)
00297 {
00298 return 0;
00299 }
00300
00301 for (i=0; i<4; i++)
00302 {
00303 oword = (oword << 7) | fgetc(in);
00304 }
00305
00306 return (off_t) (oword);
00307 }
00308
00309 return 0;
00310 }
00311
00312
00313 #ifndef NO_ID3TAG
00314
00316 static id3_byte_t *splt_mp3_get_id3v2_tag_bytes(FILE *file, id3_length_t *length)
00317 {
00318 id3_byte_t *bytes = NULL;
00319 *length = 0;
00320
00321 off_t id3v2_end_offset = splt_mp3_getid3v2_end_offset(file, 0);
00322
00323 if (id3v2_end_offset != 0)
00324 {
00325 size_t id3v2_size = (size_t) (id3v2_end_offset + 10);
00326
00327 rewind(file);
00328 bytes = splt_io_fread(file, 1, id3v2_size);
00329
00330 if (! bytes)
00331 {
00332 return NULL;
00333 }
00334
00335 *length = (unsigned long) id3v2_size;
00336 }
00337
00338 return bytes;
00339 }
00340
00341 static id3_byte_t *splt_mp3_get_id3v1_tag_bytes(FILE *file, id3_length_t *length)
00342 {
00343 id3_byte_t *bytes = NULL;
00344 *length = 0;
00345
00346 off_t id3v1_offset = splt_mp3_getid3v1_offset(file);
00347
00348 if (id3v1_offset != 0)
00349 {
00350 if (fseeko(file, id3v1_offset, SEEK_END) !=-1)
00351 {
00352 bytes = malloc(sizeof(unsigned char) * 128);
00353
00354 if (! bytes)
00355 {
00356 return NULL;
00357 }
00358
00359 if (fread(bytes, 1 , 128, file) != 128)
00360 {
00361 if (bytes)
00362 {
00363 free(bytes);
00364 bytes = NULL;
00365 return NULL;
00366 }
00367 }
00368 else
00369 {
00370 *length = (unsigned long) 128;
00371 }
00372 }
00373 }
00374
00375 return bytes;
00376 }
00377
00387 static id3_byte_t *splt_mp3_get_id3_tag_bytes(splt_state *state, const char *filename,
00388 id3_length_t *length, int *error, int *tags_version)
00389 {
00390 *length = 0;
00391 id3_byte_t *bytes = NULL;
00392
00393 FILE *file = splt_io_fopen(filename, "rb");
00394
00395 if (! file)
00396 {
00397 splt_e_set_strerror_msg_with_data(state, filename);
00398 *error = SPLT_ERROR_CANNOT_OPEN_FILE;
00399 goto end;
00400 }
00401 else
00402 {
00403 id3_length_t id3v1_length = 0;
00404 id3_byte_t *id3v1_bytes = splt_mp3_get_id3v1_tag_bytes(file, &id3v1_length);
00405
00406 id3_length_t id3v2_length = 0;
00407 id3_byte_t *id3v2_bytes = splt_mp3_get_id3v2_tag_bytes(file, &id3v2_length);
00408
00409 if (id3v2_bytes)
00410 {
00411 *tags_version = 2;
00412 bytes = id3v2_bytes;
00413 *length = id3v2_length;
00414
00415 if (id3v1_bytes)
00416 {
00417 *tags_version = 12;
00418 free(id3v1_bytes);
00419 id3v1_bytes = NULL;
00420 }
00421 }
00422 else if (id3v1_bytes)
00423 {
00424 *tags_version = 1;
00425 bytes = id3v1_bytes;
00426 *length = id3v1_length;
00427 }
00428 }
00429
00430 end:
00431 if (file)
00432 {
00433 if (fclose(file) != 0)
00434 {
00435 if (bytes)
00436 {
00437 free(bytes);
00438 bytes = NULL;
00439 }
00440 return NULL;
00441 }
00442 }
00443
00444 return bytes;
00445 }
00446
00448 static int splt_mp3_put_original_libid3_frame(splt_state *state,
00449 const struct id3_tag *id3tag, const char *frame_type, int id_type)
00450 {
00451 struct id3_frame *frame = NULL;
00452 union id3_field *field = NULL;
00453 id3_ucs4_t *ucs4 = NULL;
00454 id3_utf8_t *tag_value = NULL;
00455
00456 int err = SPLT_OK;
00457
00458 frame = id3_tag_findframe(id3tag, frame_type,0);
00459 if (frame != NULL)
00460 {
00461 if (id_type == SPLT_MP3_ID3_COMMENT)
00462 {
00463 field = id3_frame_field(frame, 3);
00464 ucs4 = (id3_ucs4_t *) id3_field_getfullstring(field);
00465 }
00466 else
00467 {
00468 field = id3_frame_field(frame, 1);
00469 ucs4 = (id3_ucs4_t *) id3_field_getstrings(field,0);
00470 }
00471 if (ucs4 != NULL)
00472 {
00473 tag_value = id3_ucs4_utf8duplicate(ucs4);
00474
00475 if (tag_value != NULL)
00476 {
00477 switch (id_type)
00478 {
00479 case SPLT_MP3_ID3_ALBUM:
00480 err = splt_tu_set_original_tags_field(state,SPLT_TAGS_ALBUM, tag_value);
00481 break;
00482 case SPLT_MP3_ID3_ARTIST:
00483 err = splt_tu_set_original_tags_field(state,SPLT_TAGS_ARTIST, tag_value);
00484 break;
00485 case SPLT_MP3_ID3_TITLE:
00486 if (strcmp(frame_type,ID3_FRAME_TITLE) == 0)
00487 {
00488 err = splt_tu_set_original_tags_field(state,SPLT_TAGS_TITLE, tag_value);
00489 }
00490 break;
00491 case SPLT_MP3_ID3_YEAR:
00492 err = splt_tu_set_original_tags_field(state,SPLT_TAGS_YEAR, tag_value);
00493 break;
00494 case SPLT_MP3_ID3_TRACK:
00495 ;
00496 int track = atoi((char *)tag_value);
00497 err = splt_tu_set_original_tags_field(state,SPLT_TAGS_TRACK, &track);
00498 break;
00499 case SPLT_MP3_ID3_COMMENT:
00500 err = splt_tu_set_original_tags_field(state,SPLT_TAGS_COMMENT, tag_value);
00501 break;
00502 case SPLT_MP3_ID3_GENRE:
00503 ;
00504 char *genre = (char *)tag_value;
00505
00506 int id3v1 = atoi(genre);
00507 if ((id3v1 != 0) &&
00508 (id3v1 < SPLT_ID3V1_NUMBER_OF_GENRES) &&
00509 (state->original_tags.tags.genre == NULL))
00510 {
00511 err = splt_tu_set_original_tags_field(state, SPLT_TAGS_GENRE, splt_id3v1_genres[id3v1]);
00512 }
00513 else if (strcmp(genre, "0") == 0)
00514 {
00515 err = splt_tu_set_original_tags_field(state, SPLT_TAGS_GENRE, SPLT_UNDEFINED_GENRE);
00516 }
00517 else
00518 {
00519 err = splt_tu_set_original_tags_field(state, SPLT_TAGS_GENRE, genre);
00520 }
00521 break;
00522 default:
00523 break;
00524 }
00525 free(tag_value);
00526 tag_value = NULL;
00527 }
00528 else
00529 {
00530 err = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00531 }
00532 }
00533 }
00534
00535 return err;
00536 }
00537
00539 #define MP3_VERIFY_ERROR() \
00540 if (err != SPLT_OK) \
00541 { \
00542 *tag_error = err; \
00543 goto end; \
00544 };
00545
00551 static void splt_mp3_get_original_tags(const char *filename,
00552 splt_state *state, int *tag_error)
00553 {
00554 int err = SPLT_OK;
00555
00556
00557 struct id3_tag *id3tag = NULL;
00558
00559
00560 id3_length_t id3_tag_length = 0;
00561 int tags_version = 0;
00562 id3_byte_t *id3_tag_bytes =
00563 splt_mp3_get_id3_tag_bytes(state, filename, &id3_tag_length, tag_error,
00564 &tags_version);
00565
00566 if (*tag_error >= 0)
00567 {
00568 if (id3_tag_bytes)
00569 {
00570 id3tag = id3_tag_parse(id3_tag_bytes, id3_tag_length);
00571
00572 if (id3tag)
00573 {
00574 err = splt_tu_set_original_tags_field(state,SPLT_TAGS_VERSION, &tags_version);
00575 MP3_VERIFY_ERROR();
00576 err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_ARTIST,
00577 SPLT_MP3_ID3_ARTIST);
00578 MP3_VERIFY_ERROR();
00579 err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_ALBUM,
00580 SPLT_MP3_ID3_ALBUM);
00581 MP3_VERIFY_ERROR();
00582 err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_TITLE,
00583 SPLT_MP3_ID3_TITLE);
00584 MP3_VERIFY_ERROR();
00585 err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_YEAR,
00586 SPLT_MP3_ID3_YEAR);
00587 MP3_VERIFY_ERROR();
00588 err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_GENRE,
00589 SPLT_MP3_ID3_GENRE);
00590 MP3_VERIFY_ERROR();
00591 err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_COMMENT,
00592 SPLT_MP3_ID3_COMMENT);
00593 MP3_VERIFY_ERROR();
00594 err = splt_mp3_put_original_libid3_frame(state,id3tag,ID3_FRAME_TRACK,
00595 SPLT_MP3_ID3_TRACK);
00596 MP3_VERIFY_ERROR();
00597
00598 id3_tag_delete(id3tag);
00599 }
00600
00601 tag_bytes_and_size *bytes_and_size = malloc(sizeof(tag_bytes_and_size));
00602 if (bytes_and_size == NULL)
00603 {
00604 err = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00605 MP3_VERIFY_ERROR();
00606 }
00607
00608 bytes_and_size->tag_bytes = id3_tag_bytes;
00609 bytes_and_size->tag_length = id3_tag_length;
00610
00611 splt_tu_set_original_tags_data(state, bytes_and_size);
00612 }
00613
00614 end:
00615 ;
00616 }
00617 else if (id3_tag_bytes)
00618 {
00619 free(id3_tag_bytes);
00620 id3_tag_bytes = NULL;
00621 }
00622 }
00623
00626 static void splt_mp3_delete_existing_frames(struct id3_tag *id, const char *frame_type)
00627 {
00628 struct id3_frame *frame = NULL;
00629 while ((frame = id3_tag_findframe(id, frame_type, 0)))
00630 {
00631 id3_tag_detachframe(id, frame);
00632 id3_frame_delete(frame);
00633 }
00634 }
00635
00636 static void splt_mp3_put_libid3_frame_in_tag_with_content(struct id3_tag *id,
00637 const char *frame_type, int field_number, const char *content, int *error)
00638 {
00639 struct id3_frame *id_frame = NULL;
00640 id3_ucs4_t *field_content = NULL;
00641 union id3_field *id_field = NULL;
00642
00643 if (content)
00644 {
00645 splt_mp3_delete_existing_frames(id, frame_type);
00646
00647 id_frame = id3_frame_new(frame_type);
00648 if (!id_frame)
00649 {
00650 goto error;
00651 }
00652
00653 id_field = id3_frame_field(id_frame, field_number);
00654
00655 id3_field_settextencoding(id3_frame_field(id_frame, 0),
00656 ID3_FIELD_TEXTENCODING_UTF_16);
00657
00658 field_content = id3_utf8_ucs4duplicate((signed char *)content);
00659 if (! field_content)
00660 {
00661 goto error;
00662 }
00663
00664
00665 if (field_number == 1)
00666 {
00667 if (id3_field_addstring(id_field, field_content) == -1)
00668 {
00669 goto error;
00670 }
00671 }
00672
00673 else if (field_number == 3)
00674 {
00675 if (id3_field_setfullstring(id_field, field_content) == -1)
00676 {
00677 goto error;
00678 }
00679 }
00680
00681 free(field_content);
00682 field_content = NULL;
00683
00684 if (id3_tag_attachframe(id, id_frame) == -1)
00685 {
00686 goto error;
00687 }
00688
00689 id3_frame_delete(id_frame);
00690 }
00691
00692 return;
00693
00694 error:
00695 *error = SPLT_ERROR_LIBID3;
00696 if (id_frame)
00697 {
00698 id3_frame_delete(id_frame);
00699 }
00700 if (field_content)
00701 {
00702 free(field_content);
00703 field_content = NULL;
00704 }
00705
00706 return;
00707 }
00708
00709 static char *splt_mp3_build_libid3tag(const char *title, const char *artist,
00710 const char *album, const char *year, const char *genre,
00711 const char *comment, int track, int set_original_tags,
00712 int *error, unsigned long *number_of_bytes, int tags_version,
00713 splt_state *state)
00714 {
00715 struct id3_tag *id = NULL;
00716
00717 tag_bytes_and_size *bytes_and_size =
00718 (tag_bytes_and_size *) splt_tu_get_original_tags_data(state);
00719
00720 if (set_original_tags && bytes_and_size)
00721 {
00722 id = id3_tag_parse(bytes_and_size->tag_bytes, bytes_and_size->tag_length);
00723 }
00724 else
00725 {
00726 id = id3_tag_new();
00727 }
00728
00729 id3_byte_t *bytes = NULL;
00730 id3_length_t bytes_length = 0;
00731
00732 if (tags_version == 1)
00733 {
00734 id3_tag_options(id, ID3_TAG_OPTION_ID3V1, ID3_TAG_OPTION_ID3V1);
00735 }
00736 else
00737 {
00738
00739
00740 id3_tag_options(id, ID3_TAG_OPTION_CRC, 0);
00741 id3_tag_options(id, ID3_TAG_OPTION_COMPRESSION, 0);
00742 }
00743
00744 splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_TITLE, 1, title, error);
00745 if (*error < 0) { goto error; }
00746 splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_ARTIST, 1, artist, error);
00747 if (*error < 0) { goto error; }
00748 splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_ALBUM, 1, album, error);
00749 if (*error < 0) { goto error; }
00750 splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_YEAR, 1, year, error);
00751 if (*error < 0) { goto error; }
00752 splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_COMMENT, 3, comment, error);
00753 if (*error < 0) { goto error; }
00754 if (track != -1)
00755 {
00756 char track_str[255] = { '\0' };
00757 snprintf(track_str,254,"%d",track);
00758 splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_TRACK, 1,
00759 track_str, error);
00760 if (*error < 0) { goto error; }
00761 }
00762
00763 splt_mp3_put_libid3_frame_in_tag_with_content(id, ID3_FRAME_GENRE, 1, genre, error);
00764 if (*error < 0) { goto error; }
00765
00766
00767 bytes_length = id3_tag_render(id, NULL);
00768
00769 if (bytes_length > 0)
00770 {
00771
00772 bytes = malloc(sizeof(id3_byte_t) * bytes_length);
00773 if (!bytes)
00774 {
00775 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00776 goto error;
00777 }
00778 memset(bytes, '\0', sizeof(id3_byte_t) * bytes_length);
00779
00780 bytes_length = id3_tag_render(id, bytes);
00781
00782 *number_of_bytes = (unsigned long) bytes_length;
00783 }
00784
00785 id3_tag_delete(id);
00786
00787 return (char *) bytes;
00788
00789 error:
00790 id3_tag_delete(id);
00791 *number_of_bytes = 0;
00792 if (bytes)
00793 {
00794 free(bytes);
00795 bytes = NULL;
00796 }
00797
00798 return NULL;
00799 }
00800
00801 #else
00802
00804 static const char unsigned splt_mp3_id3v1_genre_mapping[SPLT_ID3V1_NUMBER_OF_GENRES] =
00805 { 0x00,
00806
00807 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
00808 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14,
00809 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
00810 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
00811 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
00812 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C,
00813 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46,
00814 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
00815
00816 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
00817 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63,
00818 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D,
00819 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
00820 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D,
00821
00822 0xFF };
00823
00825 static unsigned char splt_mp3_get_id3v1_mapping(const char *genre_string)
00826 {
00827 if (genre_string == NULL)
00828 {
00829 return 0xFF;
00830 }
00831
00832 int i = 0;
00833 for (i = 0; i < SPLT_ID3V1_NUMBER_OF_GENRES; i++)
00834 {
00835 if (strncmp(genre_string, splt_id3v1_genres[i], strlen(genre_string)) == 0)
00836 {
00837 return splt_mp3_id3v1_genre_mapping[i];
00838 }
00839 }
00840
00841 return 0xFF;
00842 }
00843
00850 static char *splt_mp3_build_simple_id3v1(const char *title, const char *artist,
00851 const char *album, const char *year, const char *genre,
00852 const char *comment, int track, int *error, unsigned long *number_of_bytes)
00853 {
00854 char *id = NULL;
00855 char buffer[30] = { '\0' };
00856 int j = 3,i = 0;
00857
00858 if ((id = malloc(sizeof(char) * 128)) != NULL)
00859 {
00860 memset(id,'\0',128);
00861
00862 strncpy(id, SPLT_MP3_TAG, 4);
00863
00864 memset(buffer, '\0', 30);
00865 if (title!=NULL) strncpy(buffer, title, 30);
00866 for (i=0; i<30; i++) id[j++]=buffer[i];
00867
00868 memset(buffer, '\0', 30);
00869 if (artist!=NULL) strncpy(buffer, artist, 30);
00870 for (i=0; i<30; i++) id[j++]=buffer[i];
00871
00872 memset(buffer, '\0', 30);
00873 if (album!=NULL) strncpy(buffer, album, 30);
00874 for (i=0; i<30; i++) id[j++]=buffer[i];
00875
00876 memset(buffer, '\0', 30);
00877 if (year!=NULL) strncpy(buffer, year, 4);
00878 for (i=0; i<4; i++) id[j++]=buffer[i];
00879
00880 memset(buffer, '\0', 30);
00881 if (comment!=NULL) strncpy(buffer, comment, 30);
00882 for (i=0; i<30; i++)
00883 {
00884 id[j++]=buffer[i];
00885 }
00886
00887 if (track != -1)
00888 {
00889 if (track != 0x00)
00890 {
00891 id[j-1] = (char) track;
00892 }
00893 }
00894 id[j] = (char) splt_mp3_get_id3v1_mapping(genre);
00895 }
00896 else
00897 {
00898 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
00899 return NULL;
00900 }
00901
00902 *number_of_bytes = 128;
00903
00904 return id;
00905 }
00906 #endif
00907
00914 static char *splt_mp3_build_id3_tags(splt_state *state,
00915 const char *title, const char *artist,
00916 const char *album, const char *year, const char *genre,
00917 const char *comment, int track, int set_original_tags,
00918 int *error, unsigned long *number_of_bytes, int version)
00919 {
00920 char *id = NULL;
00921
00922 #ifdef NO_ID3TAG
00923 if (version == 1)
00924 {
00925 splt_d_print_debug(state,"Setting ID3v1 tags without libid3tag\n");
00926 id = splt_mp3_build_simple_id3v1(title, artist, album, year, genre, comment, track,
00927 error, number_of_bytes);
00928 }
00929 #else
00930 if (version == 1)
00931 {
00932 splt_d_print_debug(state,"Setting ID3v1 tags with libid3tag\n");
00933 id = splt_mp3_build_libid3tag(title, artist, album, year, genre, comment, track,
00934 set_original_tags, error, number_of_bytes, 1, state);
00935 }
00936 else
00937 {
00938 splt_d_print_debug(state,"Setting ID3v2 tags with libid3tag\n");
00939 id = splt_mp3_build_libid3tag(title, artist, album, year, genre, comment, track,
00940 set_original_tags, error, number_of_bytes, 2, state);
00941 }
00942 #endif
00943
00944 return id;
00945 }
00946
00953 static char *splt_mp3_build_tags(const char *filename, splt_state *state, int *error,
00954 unsigned long *number_of_bytes, int id3_version)
00955 {
00956 char *id3_data = NULL;
00957
00958 if (splt_o_get_int_option(state,SPLT_OPT_TAGS) != SPLT_NO_TAGS)
00959 {
00960 splt_tags *tags = splt_tu_get_current_tags(state);
00961
00962 if (tags)
00963 {
00964 char *artist_or_performer = splt_tu_get_artist_or_performer_ptr(tags);
00965 id3_data = splt_mp3_build_id3_tags(state,
00966 tags->title, artist_or_performer, tags->album,
00967 tags->year, tags->genre, tags->comment,
00968 tags->track, tags->set_original_tags ,error, number_of_bytes, id3_version);
00969 }
00970 }
00971
00972 return id3_data;
00973 }
00974
00976 int splt_mp3_write_id3v1_tags(splt_state *state, FILE *file_output,
00977 const char *output_fname)
00978 {
00979 const char *filename = splt_t_get_filename_to_split(state);
00980 unsigned long number_of_bytes = 0;
00981 int error = SPLT_OK;
00982
00983 char *id3_tags = splt_mp3_build_tags(filename, state, &error, &number_of_bytes, 1);
00984
00985 if ((error >= 0) && (id3_tags) && (number_of_bytes > 0))
00986 {
00987 if (file_output)
00988 {
00989 if (fseeko(file_output, splt_mp3_getid3v1_offset(file_output), SEEK_END)!=-1)
00990 {
00991 if (splt_io_fwrite(state, id3_tags, 1, number_of_bytes, file_output) < number_of_bytes)
00992 {
00993 splt_e_set_error_data(state, output_fname);
00994 error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
00995 }
00996 }
00997 else
00998 {
00999 splt_e_set_strerror_msg_with_data(state, output_fname);
01000 error = SPLT_ERROR_SEEKING_FILE;
01001 }
01002 }
01003 }
01004
01005 if (id3_tags)
01006 {
01007 free(id3_tags);
01008 id3_tags = NULL;
01009 }
01010
01011 return error;
01012 }
01013
01014 #ifndef NO_ID3TAG
01015 int splt_mp3_write_id3v2_tags(splt_state *state, FILE *file_output,
01016 const char *output_fname, off_t *end_offset)
01017 {
01018 const char *filename = splt_t_get_filename_to_split(state);
01019 unsigned long number_of_bytes = 0;
01020 int error = SPLT_OK;
01021
01022 char *id3_tags = splt_mp3_build_tags(filename, state, &error, &number_of_bytes, 2);
01023
01024 if ((error >= 0) && (id3_tags) && (number_of_bytes > 0))
01025 {
01026 if (splt_io_fwrite(state, id3_tags, 1, number_of_bytes, file_output) < number_of_bytes)
01027 {
01028 splt_e_set_error_data(state, output_fname);
01029 error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
01030 }
01031 else
01032 {
01033 if (end_offset != NULL)
01034 {
01035 *end_offset = number_of_bytes;
01036 }
01037 }
01038 }
01039
01040 if (id3_tags)
01041 {
01042 free(id3_tags);
01043 id3_tags = NULL;
01044 }
01045
01046 return error;
01047 }
01048 #endif
01049
01051 int splt_mp3_get_output_tags_version(splt_state *state)
01052 {
01053 #ifdef NO_ID3TAG
01054 splt_d_print_debug(state,"Output tags version is ID3v1 without libid3tag\n");
01055 return 1;
01056 #else
01057 int original_tags_version = state->original_tags.tags.tags_version;
01058 int force_tags_version = splt_o_get_int_option(state, SPLT_OPT_FORCE_TAGS_VERSION);
01059
01060 int output_tags_version = original_tags_version;
01061 if (force_tags_version != 0)
01062 {
01063 output_tags_version = force_tags_version;
01064 }
01065
01066 if ((output_tags_version == 0) &&
01067 (splt_o_get_int_option(state, SPLT_OPT_TAGS) == SPLT_CURRENT_TAGS))
01068 {
01069 char *filename = splt_t_get_filename_to_split(state);
01070 if (strcmp(filename, "-") != 0)
01071 {
01072 output_tags_version = 12;
01073 }
01074 }
01075
01076 splt_d_print_debug(state,"Output tags version is ID3v _%d_\n", output_tags_version);
01077
01078 return output_tags_version;
01079 #endif
01080 }
01081
01082
01083
01084
01093 static splt_mp3_state *splt_mp3_info(FILE *file_input, splt_state *state,
01094 int framemode, int *error)
01095 {
01096 splt_mp3_state *mp3state = state->codec;
01097
01098 int prev = -1;
01099 long len;
01100
01101 if ((mp3state = malloc(sizeof(splt_mp3_state)))==NULL)
01102 {
01103 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
01104 return NULL;
01105 }
01106 memset(mp3state, 0x0, sizeof(splt_mp3_state));
01107
01108 char *filename = splt_t_get_filename_to_split(state);
01109
01110
01111 mp3state->syncdetect = 0;
01112
01113
01114 mp3state->frames = 1;
01115 mp3state->end = 0;
01116 mp3state->end_non_zero = 0;
01117 mp3state->first = 1;
01118 mp3state->file_input = file_input;
01119 mp3state->framemode = framemode;
01120 mp3state->headw = 0;
01121 mp3state->mp3file.xing = 0;
01122 mp3state->mp3file.xing_offset = 0;
01123 mp3state->mp3file.xingbuffer = NULL;
01124
01125 mp3state->mp3file.len = splt_io_get_file_length(state, file_input, filename, error);
01126 splt_t_set_total_time(state, 0);
01127 mp3state->data_ptr = NULL;
01128 mp3state->data_len = 0;
01129 mp3state->buf_len = 0;
01130 mp3state->bytes = 0;
01131
01132
01133 splt_mp3_init_stream_frame(mp3state);
01134 mad_synth_init(&mp3state->synth);
01135
01136 mad_timer_reset(&mp3state->timer);
01137
01138
01139 do
01140 {
01141 int ret = splt_mp3_get_frame(mp3state);
01142
01143 if (ret == -2)
01144 {
01145 splt_e_set_error_data(state,filename);
01146 *error = SPLT_ERROR_INVALID;
01147 goto function_end;
01148 }
01149
01150 if ((prev == 0) &&
01151 ((ret == 0) || (mp3state->stream.error == MAD_ERROR_BUFLEN)))
01152 {
01153 break;
01154 }
01155
01156
01157 if (ret == 0)
01158 {
01159
01160 mp3state->data_ptr = (unsigned char *) mp3state->stream.this_frame;
01161
01162 if(mp3state->stream.next_frame!=NULL)
01163 {
01164 mp3state->data_len = (long) (mp3state->stream.next_frame - mp3state->stream.this_frame);
01165 }
01166
01167 if (mp3state->stream.anc_bitlen > 64)
01168 {
01169 int tag = 0;
01170 struct mad_bitptr ptr = mp3state->stream.anc_ptr;
01171 struct mad_bitptr start = ptr;
01172
01173 unsigned long xing_word = mad_bit_read(&ptr, 32);
01174 if ((xing_word==SPLT_MP3_XING_MAGIC) ||
01175 (xing_word==SPLT_MP3_INFO_MAGIC))
01176 {
01177 tag = 1;
01178 }
01179
01180 else
01181 {
01182 if (xing_word == ((SPLT_MP3_XING_MAGIC << 16) & 0xffffffffL)
01183 || xing_word == ((SPLT_MP3_INFO_MAGIC << 16) & 0xffffffffL))
01184 {
01185 ptr = start;
01186 mad_bit_skip(&ptr, 16);
01187 tag = 1;
01188 }
01189 }
01190
01191
01192 if (tag)
01193 {
01194 xing_word = mad_bit_read(&ptr, 32);
01195 if (xing_word & SPLT_MP3_XING_FRAMES)
01196 {
01197 mad_timer_t total;
01198 mp3state->frames = mad_bit_read(&ptr, 32);
01199 total = mp3state->frame.header.duration;
01200 mad_timer_multiply(&total, mp3state->frames);
01201 float total_time_milliseconds = (float) mad_timer_count(total, MAD_UNITS_MILLISECONDS);
01202 total_time_milliseconds /= 10.f;
01203 splt_t_set_total_time(state, (long) ceilf(total_time_milliseconds));
01204 }
01205
01206 if (xing_word & SPLT_MP3_XING_BYTES)
01207 {
01208 if (mp3state->mp3file.len == 0)
01209 mp3state->mp3file.len = mad_bit_read(&ptr, 32);
01210 }
01211
01212 if (splt_o_get_int_option(state, SPLT_OPT_XING))
01213 {
01214 mp3state->mp3file.xing = mp3state->data_len;
01215
01216 if ((mp3state->mp3file.xingbuffer =
01217 malloc(mp3state->mp3file.xing))==NULL)
01218 {
01219 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
01220 goto function_end;
01221 }
01222
01223 memcpy(mp3state->mp3file.xingbuffer, mp3state->data_ptr,
01224 mp3state->mp3file.xing);
01225 mp3state->mp3file.xing_offset = splt_mp3_xing_info_off(mp3state);
01226 }
01227
01228 splt_o_set_int_option(state, SPLT_OPT_FRAME_MODE, SPLT_TRUE);
01229 mp3state->framemode = 1;
01230
01231 if (!splt_o_messages_locked(state))
01232 {
01233 if (!splt_o_get_iopt(state, SPLT_INTERNAL_FRAME_MODE_ENABLED))
01234 {
01235 int split_mode =
01236 splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE);
01237
01238 if (split_mode != SPLT_OPTION_WRAP_MODE &&
01239 split_mode != SPLT_OPTION_ERROR_MODE)
01240 {
01241 splt_c_put_info_message_to_client(state,
01242 _(" info: found Xing or Info header. Switching to frame mode... \n"));
01243 splt_o_set_iopt(state, SPLT_INTERNAL_FRAME_MODE_ENABLED, SPLT_TRUE);
01244 }
01245 }
01246 }
01247 continue;
01248 }
01249 }
01250 }
01251
01252 prev = ret;
01253 } while (1);
01254
01255 len = (long) (mp3state->buf_len - (mp3state->data_ptr - mp3state->inputBuffer));
01256
01257 if (len < 0)
01258 {
01259 splt_e_set_error_data(state,filename);
01260 *error = SPLT_ERROR_INVALID;
01261 goto function_end;
01262 }
01263
01264
01265 mp3state->mp3file.firsth = (off_t) (mp3state->bytes - len);
01266 splt_d_print_debug(state, "mp3 firsth bytes = %ld\n", mp3state->bytes);
01267 splt_d_print_debug(state, "mp3 firsth len = %ld\n", len);
01268 mp3state->bytes = mp3state->mp3file.firsth;
01269 mp3state->headw =
01270 (unsigned long) ((mp3state->data_ptr[0] << 24) |
01271 (mp3state->data_ptr[1] << 16) |
01272 (mp3state->data_ptr[2] << 8) | (mp3state->data_ptr[3]));
01273 mp3state->mp3file.mpgid = (int) ((mp3state->headw >> 19)&1);
01274 mp3state->mp3file.layer = mp3state->frame.header.layer;
01275
01276 mp3state->mp3file.freq = mp3state->frame.header.samplerate;
01277 mp3state->mp3file.bitrate = mp3state->frame.header.bitrate/SPLT_MP3_BYTE;
01278
01279 mp3state->mp3file.firsthead =
01280 splt_mp3_makehead(mp3state->headw, mp3state->mp3file, mp3state->mp3file.firsthead, mp3state->mp3file.firsth);
01281
01282 mp3state->mp3file.fps = (float) (mp3state->mp3file.freq*(2-mp3state->mp3file.mpgid));
01283 mp3state->mp3file.fps /= SPLT_MP3_PCM;
01284
01285
01286 switch(mp3state->frame.header.mode)
01287 {
01288 case MAD_MODE_SINGLE_CHANNEL:
01289 mp3state->mp3file.channels = 0;
01290 break;
01291 case MAD_MODE_DUAL_CHANNEL:
01292 mp3state->mp3file.channels = 1;
01293 break;
01294 case MAD_MODE_JOINT_STEREO:
01295 mp3state->mp3file.channels = 2;
01296 break;
01297 case MAD_MODE_STEREO:
01298 mp3state->mp3file.channels = 3;
01299 break;
01300 default:
01301 mp3state->mp3file.channels = 4;
01302 break;
01303 }
01304
01305
01306
01307 if (splt_t_get_total_time(state) == 0)
01308 {
01309 if (mp3state->mp3file.len > 0)
01310 {
01311 long temp = (long)
01312 (((double)(mp3state->mp3file.len - mp3state->mp3file.firsth)
01313 / (double)mp3state->mp3file.bitrate) * 100.0);
01314
01315 splt_t_set_total_time(state, temp);
01316 }
01317 }
01318
01319 function_end:
01320
01321
01322
01323 mad_synth_finish(&mp3state->synth);
01324
01325 return mp3state;
01326 }
01327
01329 static void splt_mp3_end(splt_state *state, int *error)
01330 {
01331 splt_mp3_state *mp3state = state->codec;
01332 if (mp3state)
01333 {
01334 splt_mp3_finish_stream_frame(mp3state);
01335 if (mp3state->file_input)
01336 {
01337 if (mp3state->file_input != stdin)
01338 {
01339 if (fclose(mp3state->file_input) != 0)
01340 {
01341 splt_e_set_strerror_msg_with_data(state, splt_t_get_filename_to_split(state));
01342 *error = SPLT_ERROR_CANNOT_CLOSE_FILE;
01343 }
01344 }
01345 mp3state->file_input = NULL;
01346 }
01347
01348 splt_mp3_state_free(state);
01349 }
01350 state->codec = NULL;
01351 }
01352
01354 static void splt_mp3_get_info(splt_state *state, FILE *file_input, int *error)
01355 {
01356
01357
01358
01359 state->codec = splt_mp3_info(file_input, state,
01360 splt_o_get_int_option(state,SPLT_OPT_FRAME_MODE), error);
01361
01362 if ((*error < 0) || (state->codec == NULL))
01363 {
01364 if (state->codec != NULL)
01365 {
01366 splt_mp3_end(state, error);
01367 }
01368 return;
01369 }
01370
01371 else
01372 {
01373 if ((! splt_o_messages_locked(state)) &&
01374 (splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_WRAP_MODE) &&
01375 (splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_ERROR_MODE))
01376 {
01377 splt_mp3_state *mp3state = state->codec;
01378 struct splt_mp3 *mfile = &mp3state->mp3file;
01379
01380 char mpeg_infos[1024] = { '\0' };
01381 snprintf(mpeg_infos, 1024, _(" info: MPEG %d Layer %d - %d Hz - %s"),
01382 (2-mfile->mpgid), mfile->layer, mfile->freq, splt_mp3_chan[mfile->channels]);
01383
01384 char frame_mode_infos[256] = { '\0' };
01385 if (mp3state->framemode)
01386 {
01387 if (splt_o_get_int_option(state, SPLT_OPT_INPUT_NOT_SEEKABLE))
01388 {
01389 snprintf(frame_mode_infos, 255, _(" - FRAME MODE NS"));
01390 }
01391 else
01392 {
01393 snprintf(frame_mode_infos, 255, _(" - FRAME MODE"));
01394 }
01395 }
01396 else if (splt_o_get_int_option(state, SPLT_OPT_INPUT_NOT_SEEKABLE))
01397 {
01398 snprintf(frame_mode_infos, 255, _(" - NS - %d Kb/s"),
01399 mfile->bitrate * SPLT_MP3_BYTE / 1000);
01400 }
01401 else
01402 {
01403 snprintf(frame_mode_infos, 255, _(" - %d Kb/s"),
01404 mfile->bitrate * SPLT_MP3_BYTE / 1000);
01405 }
01406
01407 char total_time[256] = { '\0' };
01408 int total_seconds = (int) (splt_t_get_total_time(state) / 100);
01409 int minutes = total_seconds / 60;
01410 int seconds = total_seconds % 60;
01411 snprintf(total_time,255, _(" - Total time: %dm.%02ds"), minutes, seconds%60);
01412
01413 splt_c_put_info_message_to_client(state,
01414 "%s%s%s\n", mpeg_infos, frame_mode_infos, total_time);
01415 }
01416 }
01417 }
01418
01419
01420
01421
01422 static off_t splt_mp3_write_data_ptr(splt_state *state, const char *filename,
01423 const char *output_fname, FILE *file_output, int *error)
01424 {
01425 splt_mp3_state *mp3state = state->codec;
01426
01427 long len = (long) (mp3state->inputBuffer + mp3state->buf_len - mp3state->data_ptr);
01428
01429 if (len < 0)
01430 {
01431 splt_e_set_error_data(state, filename);
01432 *error = SPLT_ERROR_WHILE_READING_FILE;
01433 return len;
01434 }
01435
01436 if (splt_io_fwrite(state, mp3state->data_ptr, 1, len, file_output) < len)
01437 {
01438 splt_e_set_error_data(state,output_fname);
01439 *error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
01440 return len;
01441 }
01442
01443 mp3state->data_len = 0;
01444
01445 return len;
01446 }
01447
01448
01461 static int splt_mp3_simple_split(splt_state *state, const char *output_fname,
01462 off_t begin, off_t end, int do_write_tags, short write_first_frame)
01463 {
01464 splt_d_print_debug(state,"Mp3 simple split on output _%s_\n", output_fname);
01465 splt_d_print_debug(state,"Mp3 simple split offset begin is _%ld_\n", begin);
01466 splt_d_print_debug(state,"Mp3 simple split offset end is _%ld_\n", end);
01467
01468 splt_mp3_state *mp3state = state->codec;
01469
01470 int error = SPLT_OK_SPLIT;
01471
01472 FILE *file_output = NULL;
01473 off_t position = 0;
01474 unsigned char buffer[SPLT_MP3_READBSIZE] = { '\0' };
01475 long readed = 0;
01476
01477 off_t temp_end = 0;
01478
01479 long start = begin;
01480 int split_mode = splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE);
01481
01482 splt_c_put_progress_text(state, SPLT_PROGRESS_CREATE);
01483
01484 char *filename = splt_t_get_filename_to_split(state);
01485
01486 position = ftello(mp3state->file_input);
01487
01488 if (fseeko(mp3state->file_input, begin, SEEK_SET)==-1)
01489 {
01490 return SPLT_ERROR_BEGIN_OUT_OF_FILE;
01491 }
01492
01493
01494 off_t st_size;
01495 char *fname_to_split = splt_t_get_filename_to_split(state);
01496 if(splt_io_stat(fname_to_split, NULL, &st_size) == 0)
01497 {
01498 mp3state->end2 = st_size;
01499 }
01500 else
01501 {
01502 splt_e_set_strerror_msg_with_data(state, fname_to_split);
01503 return SPLT_ERROR_CANNOT_OPEN_FILE;
01504 }
01505
01506 if (! splt_o_get_int_option(state, SPLT_OPT_PRETEND_TO_SPLIT))
01507 {
01508 file_output = splt_mp3_open_file_write(state, output_fname, &error);
01509 if (error < 0) { return error; }
01510 }
01511
01512 int output_tags_version = splt_mp3_get_output_tags_version(state);
01513
01514 #ifndef NO_ID3TAG
01515
01516 if (do_write_tags && (output_tags_version == 2 || output_tags_version == 12))
01517 {
01518 int err = SPLT_OK;
01519 if ((err = splt_mp3_write_id3v2_tags(state, file_output,
01520 output_fname, NULL)) < 0)
01521 {
01522 error = err;
01523 goto function_end;
01524 }
01525 }
01526 #endif
01527
01528 if (mp3state->mp3file.xing != 0)
01529 {
01530 if (splt_o_get_int_option(state, SPLT_OPT_XING))
01531 {
01532
01533 if (state->options.split_mode != SPLT_OPTION_ERROR_MODE)
01534 {
01535 if (splt_io_fwrite(state, mp3state->mp3file.xingbuffer, 1,
01536 mp3state->mp3file.xing, file_output) < mp3state->mp3file.xing)
01537 {
01538 splt_e_set_error_data(state, output_fname);
01539 error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
01540 goto function_end;
01541 }
01542 }
01543 }
01544 }
01545
01546 if (write_first_frame)
01547 {
01548 splt_mp3_write_data_ptr(state, filename, output_fname, file_output, &error);
01549 if (error < 0) { goto function_end; }
01550 }
01551
01552 while (!feof(mp3state->file_input))
01553 {
01554 readed = SPLT_MP3_READBSIZE;
01555 if (end != -1)
01556 {
01557 if (begin >= end)
01558 {
01559 break;
01560 }
01561 if ((end - begin) < SPLT_MP3_READBSIZE)
01562 {
01563 readed = end - begin;
01564 }
01565 }
01566
01567 if ((readed = fread(buffer, 1, readed, mp3state->file_input))==-1)
01568 {
01569 break;
01570 }
01571
01572 if (splt_io_fwrite(state, buffer, 1, readed, file_output) < readed)
01573 {
01574 splt_e_set_error_data(state,output_fname);
01575 error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
01576 goto function_end;
01577 }
01578 begin += readed;
01579
01580
01581 if ((split_mode == SPLT_OPTION_WRAP_MODE) ||
01582 (split_mode == SPLT_OPTION_ERROR_MODE) ||
01583 ((split_mode == SPLT_OPTION_NORMAL_MODE)
01584 && (!splt_o_get_int_option(state, SPLT_OPT_AUTO_ADJUST))
01585 && (!splt_o_get_int_option(state, SPLT_OPT_FRAME_MODE))))
01586 {
01587 temp_end = end;
01588
01589 if (end == -1)
01590 {
01591 temp_end = mp3state->end2;
01592 }
01593
01594 splt_c_update_progress(state,(double)(begin-start),
01595 (double)(temp_end-start),1,0,
01596 SPLT_DEFAULT_PROGRESS_RATE);
01597 }
01598 else
01599 {
01600
01601 if (splt_o_get_int_option(state, SPLT_OPT_AUTO_ADJUST))
01602 {
01603 splt_c_update_progress(state,(double)(begin-start),
01604 (double)(end-start),
01605 2,0.5, SPLT_DEFAULT_PROGRESS_RATE);
01606 }
01607 else
01608 {
01609 if (splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE)
01610 == SPLT_OPTION_TIME_MODE)
01611 {
01612 temp_end = end;
01613
01614 if (end == -1)
01615 {
01616 temp_end = mp3state->end2;
01617 }
01618
01619
01620 if (splt_o_get_int_option(state, SPLT_OPT_FRAME_MODE))
01621 {
01622 splt_c_update_progress(state,(double)(begin-start),
01623 (double)(temp_end-start),
01624 2,0.5, SPLT_DEFAULT_PROGRESS_RATE);
01625 }
01626 else
01627 {
01628 splt_c_update_progress(state,(double)(begin-start),
01629 (double)(temp_end-start),
01630 1,0, SPLT_DEFAULT_PROGRESS_RATE);
01631 }
01632 }
01633 else
01634 {
01635 splt_c_update_progress(state,(double)(begin-start),
01636 (double)(end-start),
01637 2,0.5, SPLT_DEFAULT_PROGRESS_RATE);
01638 }
01639 }
01640 }
01641 }
01642
01643
01644 if (do_write_tags && (output_tags_version == 1 || output_tags_version == 12))
01645 {
01646 int err = SPLT_OK;
01647 if ((err = splt_mp3_write_id3v1_tags(state, file_output, output_fname)) < 0)
01648 {
01649 error = err;
01650 goto function_end;
01651 }
01652 }
01653
01654 if (fseeko(mp3state->file_input, position, SEEK_SET)==-1)
01655 {
01656 splt_e_set_strerror_msg_with_data(state, filename);
01657 goto function_end;
01658 }
01659
01660 function_end:
01661 if (file_output)
01662 {
01663 if (file_output != stdout)
01664 {
01665 if (fclose(file_output) != 0)
01666 {
01667 splt_e_set_strerror_msg_with_data(state, filename);
01668 return SPLT_ERROR_CANNOT_CLOSE_FILE;
01669 }
01670 }
01671 file_output = NULL;
01672 }
01673
01674 return error;
01675 }
01676
01692 static double splt_mp3_split(const char *output_fname, splt_state *state,
01693 double fbegin_sec, double fend_sec, int *error, int save_end_point)
01694 {
01695 splt_d_print_debug(state,"Mp3 split...\n");
01696 splt_d_print_debug(state,"Output filename is _%s_\n", output_fname);
01697 splt_d_print_debug(state,"Begin position is _%lf_\n", fbegin_sec);
01698 splt_d_print_debug(state,"End position is _%lf_\n", fend_sec);
01699
01700 splt_mp3_state *mp3state = state->codec;
01701
01702 int adjustoption = splt_o_get_int_option(state, SPLT_OPT_PARAM_GAP);
01703 short seekable = ! splt_o_get_int_option(state, SPLT_OPT_INPUT_NOT_SEEKABLE);
01704 float threshold = splt_o_get_float_option(state, SPLT_OPT_PARAM_THRESHOLD);
01705
01706 short fend_sec_is_not_eof =
01707 !splt_u_fend_sec_is_bigger_than_total_time(state, fend_sec);
01708
01709 short eof=0, check_bitrate=0;
01710
01711 char *filename = splt_t_get_filename_to_split(state);
01712
01713 FILE *file_output = NULL;
01714 short writing = 0, finished=0;
01715 unsigned long fbegin=0;
01716 off_t wrote = 0;
01717 long len = 0;
01718
01719 unsigned long stopped_frames = 0;
01720 int progress_adjust_val = 2;
01721
01722 if (adjustoption)
01723 {
01724 progress_adjust_val = 4;
01725 }
01726
01727 splt_c_put_progress_text(state,SPLT_PROGRESS_CREATE);
01728
01729 double sec_end_time = fend_sec;
01730
01731
01732 if (!seekable)
01733 {
01734 if (! splt_o_get_int_option(state, SPLT_OPT_PRETEND_TO_SPLIT))
01735 {
01736 file_output = splt_mp3_open_file_write(state, output_fname, error);
01737 if (*error < 0) { return sec_end_time; };
01738 }
01739
01740 int output_tags_version = splt_mp3_get_output_tags_version(state);
01741
01742 off_t id3v2_end_offset = 0;
01743 #ifndef NO_ID3TAG
01744
01745 if (output_tags_version == 2 || output_tags_version == 12)
01746 {
01747 int err = SPLT_OK;
01748 if ((err = splt_mp3_write_id3v2_tags(state, file_output,
01749 output_fname, &id3v2_end_offset)) < 0)
01750 {
01751 *error = err;
01752 goto bloc_end;
01753 }
01754 }
01755 #endif
01756
01757
01758 if (mp3state->framemode)
01759 {
01760 splt_d_print_debug(state,"Starting not seekable mp3 frame mode...\n");
01761
01762 long begin_c, end_c, time;
01763
01764 begin_c = (long) (fbegin_sec * 100);
01765 if (fend_sec > 0)
01766 {
01767 end_c = (long) (fend_sec * 100);
01768 }
01769 else
01770 {
01771 end_c = 0;
01772 }
01773 time = 0;
01774
01775 do
01776 {
01777
01778 if (!writing && (time >= begin_c))
01779 {
01780 writing = 1;
01781 fbegin = mp3state->frames;
01782
01783 if (mp3state->mp3file.xing > 0)
01784 {
01785 wrote = splt_io_fwrite(state, mp3state->mp3file.xingbuffer,
01786 1, mp3state->mp3file.xing, file_output);
01787 if (wrote < mp3state->mp3file.xing)
01788 {
01789 splt_e_set_error_data(state,output_fname);
01790 *error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
01791 goto bloc_end;
01792 }
01793 }
01794 }
01795
01796
01797 if (writing)
01798 {
01799 if (mp3state->data_len > 0)
01800 {
01801 if ((len = splt_io_fwrite(state, mp3state->data_ptr, 1, mp3state->data_len, file_output))
01802 < mp3state->data_len)
01803 {
01804 splt_e_set_error_data(state,output_fname);
01805 *error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
01806 goto bloc_end;
01807 }
01808 wrote = (off_t) (wrote + len);
01809 mp3state->data_len = 0;
01810 }
01811
01812 if ((end_c > 0) && (time >= end_c))
01813 {
01814 finished = 1;
01815 }
01816 if (eof || finished)
01817 {
01818 finished = 1;
01819 if (eof) { *error = SPLT_OK_SPLIT_EOF; }
01820 break;
01821 }
01822 }
01823
01824
01825 if (splt_o_get_int_option(state,SPLT_OPT_SPLIT_MODE)
01826 == SPLT_OPTION_TIME_MODE)
01827 {
01828 splt_c_update_progress(state,(double)(time-begin_c),
01829 (double)(end_c-begin_c),1,0,
01830 SPLT_DEFAULT_PROGRESS_RATE);
01831 }
01832 else
01833 {
01834 splt_c_update_progress(state,(double)(time),
01835 (double)(end_c),1,0,
01836 SPLT_DEFAULT_PROGRESS_RATE);
01837 }
01838
01839 int mad_err = SPLT_OK;
01840 switch (splt_mp3_get_valid_frame(state, &mad_err))
01841 {
01842 case 1:
01843 mad_timer_add(&mp3state->timer, mp3state->frame.header.duration);
01844 mp3state->frames++;
01845 time = (unsigned long) mad_timer_count(mp3state->timer, MAD_UNITS_CENTISECONDS);
01846 break;
01847 case 0:
01848 break;
01849 case -1:
01850 eof = 1;
01851 *error = SPLT_OK_SPLIT_EOF;
01852 break;
01853 case -3:
01854
01855 *error = mad_err;
01856 goto bloc_end;
01857 break;
01858 default:
01859 break;
01860 }
01861
01862 } while (!finished);
01863 }
01864
01865 else
01866 {
01867 splt_d_print_debug(state,"Starting mp3 not seekable non frame mode...\n");
01868
01869 off_t begin = 0, end = 0;
01870 if (fend_sec_is_not_eof)
01871 {
01872 end = (off_t) (fend_sec * mp3state->mp3file.bitrate + mp3state->mp3file.firsth);
01873 }
01874 else
01875 {
01876 end = -1;
01877 }
01878
01879
01880 if (mp3state->end == 0)
01881 {
01882 begin = (off_t) (fbegin_sec * mp3state->mp3file.bitrate + mp3state->mp3file.firsth);
01883
01884 if ((mp3state->bytes == begin) && (mp3state->data_len > 0))
01885 {
01886 wrote += splt_mp3_write_data_ptr(state, filename, output_fname, file_output, error);
01887 if (*error < 0) { goto bloc_end; }
01888 }
01889 else
01890 {
01891 while (mp3state->bytes < begin)
01892 {
01893 off_t to_read;
01894 if (feof(mp3state->file_input))
01895 {
01896 *error = SPLT_ERROR_BEGIN_OUT_OF_FILE;
01897 goto bloc_end;
01898 }
01899 to_read = (begin - mp3state->bytes);
01900 if (to_read > SPLT_MAD_BSIZE)
01901 to_read = SPLT_MAD_BSIZE;
01902 if ((mp3state->data_len = fread(mp3state->inputBuffer, 1, to_read, mp3state->file_input))<=0)
01903 {
01904 *error = SPLT_ERROR_BEGIN_OUT_OF_FILE;
01905 goto bloc_end;
01906 }
01907 mp3state->bytes+=mp3state->data_len;
01908 }
01909
01910 int mad_err = SPLT_OK;
01911 splt_mp3_init_stream_frame(mp3state);
01912 switch (splt_mp3_get_valid_frame(state, &mad_err))
01913 {
01914 case 1:
01915 len = (long) (mp3state->inputBuffer + mp3state->buf_len - mp3state->data_ptr);
01916 if (len < 0)
01917 {
01918 splt_e_set_error_data(state,filename);
01919 *error = SPLT_ERROR_WHILE_READING_FILE;
01920 goto bloc_end;
01921 }
01922 if (splt_io_fwrite(state, mp3state->data_ptr, 1, len, file_output) < len)
01923 {
01924 splt_e_set_error_data(state,output_fname);
01925 *error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
01926 goto bloc_end;
01927 }
01928 wrote = (off_t) (wrote + len);
01929 mp3state->data_len = 0;
01930 break;
01931 case 0:
01932 break;
01933 case -1:
01934 eof = 1;
01935 *error = SPLT_ERROR_BEGIN_OUT_OF_FILE;
01936 break;
01937 case -3:
01938
01939 *error = mad_err;
01940 goto bloc_end;
01941 break;
01942 default:
01943 break;
01944 }
01945 }
01946 }
01947
01948 else
01949 {
01950 len = (long) (mp3state->inputBuffer + mp3state->buf_len - mp3state->data_ptr);
01951 if (len < 0)
01952 {
01953 splt_e_set_error_data(state,filename);
01954 *error = SPLT_ERROR_WHILE_READING_FILE;
01955 goto bloc_end;
01956 }
01957 if (splt_io_fwrite(state, mp3state->data_ptr, 1, len, file_output) < len)
01958 {
01959 splt_e_set_error_data(state,output_fname);
01960 *error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
01961 goto bloc_end;
01962 }
01963 wrote = (off_t) (wrote + len);
01964 mp3state->data_len = 0;
01965 begin = mp3state->end;
01966 }
01967
01968 long split_begin_point = mp3state->bytes;
01969
01970 while (!eof)
01971 {
01972 off_t to_read = SPLT_MAD_BSIZE;
01973 if (end > 0)
01974 {
01975 to_read = (end - mp3state->bytes);
01976 if (to_read <= 0)
01977 {
01978 break;
01979 }
01980 if (to_read > SPLT_MAD_BSIZE)
01981 to_read = SPLT_MAD_BSIZE;
01982 }
01983
01984 if (feof(mp3state->file_input) ||
01985 ((mp3state->data_len =
01986 fread(mp3state->inputBuffer, 1, to_read, mp3state->file_input))<=0))
01987 {
01988 eof = 1;
01989 *error = SPLT_OK_SPLIT_EOF;
01990 break;
01991 }
01992
01993 if (splt_io_fwrite(state, mp3state->inputBuffer, 1,
01994 mp3state->data_len, file_output) < mp3state->data_len)
01995 {
01996 splt_e_set_error_data(state,output_fname);
01997 *error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
01998 goto bloc_end;
01999 }
02000
02001 mp3state->bytes += mp3state->data_len;
02002
02003 splt_c_update_progress(state, (double) (mp3state->bytes-split_begin_point),
02004 (double)(end-split_begin_point), 1,0,SPLT_DEFAULT_PROGRESS_RATE);
02005 }
02006
02007 splt_mp3_save_end_point(state, mp3state, save_end_point, end);
02008
02009 if (!eof)
02010 {
02011
02012
02013 int mad_err = SPLT_OK;
02014 splt_mp3_init_stream_frame(mp3state);
02015 switch (splt_mp3_get_valid_frame(state, &mad_err))
02016 {
02017 case 1:
02018 len = (long) (mp3state->data_ptr - mp3state->inputBuffer);
02019 if (len < 0)
02020 {
02021 splt_e_set_error_data(state,filename);
02022 *error = SPLT_ERROR_WHILE_READING_FILE;
02023 goto bloc_end;
02024 }
02025 if (splt_io_fwrite(state, mp3state->inputBuffer, 1, len, file_output) < len)
02026 {
02027 splt_e_set_error_data(state,output_fname);
02028 *error = SPLT_ERROR_CANT_WRITE_TO_OUTPUT_FILE;
02029 goto bloc_end;
02030 }
02031 break;
02032 case 0:
02033 break;
02034 case -1:
02035 eof = 1;
02036 *error = SPLT_OK_SPLIT_EOF;
02037 break;
02038 case -3:
02039 *error = mad_err;
02040 goto bloc_end;
02041 break;
02042 default:
02043 break;
02044 }
02045 }
02046 }
02047
02048
02049 if (file_output)
02050 {
02051 if (mp3state->mp3file.xing > 0)
02052 {
02053 if (fseeko(file_output, mp3state->mp3file.xing_offset+4+id3v2_end_offset, SEEK_SET)!=-1)
02054 {
02055 unsigned long headw = (unsigned long) (mp3state->frames - fbegin + 1);
02056 fputc((headw >> 24) & 0xFF, file_output);
02057 fputc((headw >> 16) & 0xFF, file_output);
02058 fputc((headw >> 8) & 0xFF, file_output);
02059 fputc((headw >> 0) & 0xFF, file_output);
02060 headw = (unsigned long) (wrote);
02061 fputc((headw >> 24) & 0xFF, file_output);
02062 fputc((headw >> 16) & 0xFF, file_output);
02063 fputc((headw >> 8) & 0xFF, file_output);
02064 fputc((headw >> 0) & 0xFF, file_output);
02065 }
02066 else
02067 {
02068 splt_e_set_strerror_msg_with_data(state, output_fname);
02069 *error = SPLT_ERROR_SEEKING_FILE;
02070 goto bloc_end;
02071 }
02072 }
02073
02074
02075 if (output_tags_version == 1 || output_tags_version == 12)
02076 {
02077 int err = SPLT_OK;
02078 if ((err = splt_mp3_write_id3v1_tags(state, file_output, output_fname)) < 0)
02079 {
02080 *error = err;
02081 goto bloc_end;
02082 }
02083 }
02084 }
02085
02086 bloc_end:
02087 if (file_output)
02088 {
02089 if (file_output != stdout)
02090 {
02091 if (fclose(file_output) != 0)
02092 {
02093 splt_e_set_strerror_msg_with_data(state, output_fname);
02094 *error = SPLT_ERROR_CANNOT_CLOSE_FILE;
02095 }
02096 }
02097 }
02098 file_output = NULL;
02099
02100 if (*error == SPLT_OK) { *error = SPLT_OK_SPLIT; }
02101
02102 return sec_end_time;
02103 }
02104
02105 else
02106 {
02107 short write_first_frame = SPLT_FALSE;
02108 off_t begin = 0, end = 0;
02109
02110 if (mp3state->framemode)
02111 {
02112 splt_d_print_debug(state,"Starting seekable mp3 frame mode...\n");
02113
02114 unsigned long fbegin, fend, adjust;
02115 fbegin = fend = adjust = 0;
02116
02117
02118 fbegin = fbegin_sec * mp3state->mp3file.fps;
02119
02120 if (fend_sec_is_not_eof)
02121 {
02122
02123 if (adjustoption)
02124 {
02125 if (fend_sec_is_not_eof)
02126 {
02127 float adj = (float) (adjustoption);
02128 float len = (fend_sec - fbegin_sec);
02129 if (adj > len)
02130 {
02131 adj = len;
02132 }
02133 if (fend_sec > adj)
02134 {
02135 fend_sec -= adj;
02136 }
02137 adjust = (unsigned long) (adj * 100.f);
02138 }
02139 else
02140 {
02141 adjust = 0;
02142 }
02143 }
02144
02145
02146 fend = (unsigned long) ceilf(fend_sec * mp3state->mp3file.fps);
02147 }
02148 else
02149 {
02150 fend = 0xFFFFFFFF;
02151 }
02152
02153 splt_d_print_debug(state,"Finding begin...\n");
02154
02155 if (mp3state->end == 0)
02156 {
02157 if (mp3state->first)
02158 {
02159 mp3state->h.ptr = mp3state->mp3file.firsthead.ptr;
02160 mp3state->h.framesize = mp3state->mp3file.firsthead.framesize;
02161 begin = mp3state->mp3file.firsthead.ptr;
02162 mp3state->first = 0;
02163 }
02164
02165 splt_c_put_progress_text(state,SPLT_PROGRESS_PREPARE);
02166
02167
02168 if (mp3state->frames >= fbegin && mp3state->end_non_zero != 0)
02169 {
02170 begin = mp3state->end_non_zero;
02171 }
02172
02173
02174 while (mp3state->frames < fbegin)
02175 {
02176 begin = splt_mp3_findhead(mp3state, mp3state->h.ptr + mp3state->h.framesize);
02177 if (begin == -1) { *error = SPLT_ERROR_BEGIN_OUT_OF_FILE; goto bloc_end2; }
02178
02179
02180 if ((begin!=mp3state->h.ptr + mp3state->h.framesize)&&(state->syncerrors>=0))
02181 {
02182 state->syncerrors++;
02183 }
02184 if ((mp3state->syncdetect)&&(state->syncerrors> SPLT_MAXSYNC))
02185 {
02186 splt_mp3_checksync(mp3state);
02187 }
02188
02189 mp3state->h = splt_mp3_makehead(mp3state->headw, mp3state->mp3file, mp3state->h, begin);
02190 mp3state->frames++;
02191
02192
02193
02194 if (adjustoption)
02195 {
02196 splt_c_update_progress(state,(double)(mp3state->frames),
02197 (double)fend, 8,
02198 0,SPLT_DEFAULT_PROGRESS_RATE);
02199 }
02200 else
02201 {
02202 splt_c_update_progress(state,(double)(mp3state->frames),
02203 (double)fend,progress_adjust_val,
02204 0,SPLT_DEFAULT_PROGRESS_RATE);
02205 }
02206 }
02207 }
02208 else
02209 {
02210 begin = mp3state->end;
02211 }
02212
02213 splt_d_print_debug(state,"Begin is _%ld_\n", begin);
02214
02215 if (mp3state->mp3file.len > 0)
02216 {
02217 if (begin >= mp3state->mp3file.len)
02218 {
02219 *error = SPLT_ERROR_BEGIN_OUT_OF_FILE;
02220 goto bloc_end2;
02221 }
02222 }
02223
02224 splt_c_put_progress_text(state,SPLT_PROGRESS_PREPARE);
02225
02226 long int frames_begin = mp3state->frames;
02227
02228 while (mp3state->frames <= fend)
02229 {
02230 mp3state->frames++;
02231 end = splt_mp3_findhead(mp3state, mp3state->h.ptr + mp3state->h.framesize);
02232 if (end == -1)
02233 {
02234 end = mp3state->h.ptr + mp3state->h.framesize;
02235 eof=1;
02236 *error = SPLT_OK_SPLIT_EOF;
02237 break;
02238 }
02239
02240
02241 if ((end != mp3state->h.ptr + mp3state->h.framesize)&&(state->syncerrors>=0))
02242 {
02243 state->syncerrors++;
02244 }
02245 if ((mp3state->syncdetect)&&(state->syncerrors>SPLT_MAXSYNC))
02246 {
02247 splt_mp3_checksync(mp3state);
02248 }
02249
02250 mp3state->h = splt_mp3_makehead (mp3state->headw, mp3state->mp3file, mp3state->h, end);
02251
02252
02253
02254
02255 int split_mode = splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE);
02256 if (((split_mode == SPLT_OPTION_TIME_MODE) ||
02257 (split_mode == SPLT_OPTION_SILENCE_MODE) ||
02258 (split_mode == SPLT_OPTION_TRIM_SILENCE_MODE))
02259 && (!splt_o_get_int_option(state,SPLT_OPT_AUTO_ADJUST)))
02260 {
02261 splt_c_update_progress(state, (double)(mp3state->frames-fbegin),
02262 (double)(fend-fbegin), progress_adjust_val,
02263 0, SPLT_DEFAULT_PROGRESS_RATE);
02264 }
02265 else
02266 {
02267 if (adjustoption)
02268 {
02269 if(adjust)
02270 {
02271 if (split_mode == SPLT_OPTION_TIME_MODE)
02272 {
02273 splt_c_update_progress(state,
02274 (double)(mp3state->frames-frames_begin),
02275 (double)(fend-frames_begin),
02276 4,0,SPLT_DEFAULT_PROGRESS_RATE);
02277 }
02278 else
02279 {
02280 splt_c_update_progress(state,
02281 (double)(mp3state->frames-frames_begin),
02282 (double)(fend-frames_begin),
02283 8,1/(float)8,SPLT_DEFAULT_PROGRESS_RATE);
02284 }
02285 }
02286 }
02287 else
02288 {
02289 splt_c_update_progress(state,
02290 (double)(mp3state->frames-stopped_frames),
02291 (double)(fend-stopped_frames),
02292 progress_adjust_val,
02293 0,SPLT_DEFAULT_PROGRESS_RATE);
02294 }
02295 }
02296
02297
02298 if ((adjust) && (mp3state->frames >= fend))
02299 {
02300 int silence_points_found =
02301 splt_mp3_scan_silence(state, end, 2 * adjust, threshold, 0.f, 0, error,
02302 splt_scan_silence_processor);
02303
02304 if (silence_points_found == -1)
02305 {
02306 goto bloc_end2;
02307 }
02308 else if (silence_points_found > 0)
02309 {
02310 adjust = (unsigned long) (splt_siu_silence_position(state->silence_list, mp3state->off)
02311 * mp3state->mp3file.fps);
02312 }
02313 else
02314 {
02315 adjust = (unsigned long) (adjustoption * mp3state->mp3file.fps);
02316 }
02317 fend += adjust;
02318 end = splt_mp3_findhead(mp3state, end);
02319
02320 sec_end_time = mp3state->frames / mp3state->mp3file.fps;
02321
02322 splt_siu_ssplit_free(&state->silence_list);
02323 adjust=0;
02324
02325 splt_c_put_progress_text(state,SPLT_PROGRESS_PREPARE);
02326 stopped_frames = mp3state->frames;
02327 }
02328 }
02329
02330 splt_mp3_save_end_point(state, mp3state, save_end_point, end);
02331
02332
02333 if (mp3state->mp3file.xing > 0)
02334 {
02335 unsigned long headw;
02336 headw = (unsigned long) (mp3state->frames - fbegin + 1);
02337 mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+4] = (headw >> 24) & 0xFF;
02338 mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+5] = (headw >> 16) & 0xFF;
02339 mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+6] = (headw >> 8) & 0xFF;
02340 mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+7] = headw & 0xFF;
02341
02342 if (end == -1)
02343 {
02344 end = mp3state->mp3file.len;
02345 }
02346 headw = (unsigned long) (end - begin + mp3state->mp3file.xing);
02347 mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+8] = (headw >> 24) & 0xFF;
02348 mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+9] = (headw >> 16) & 0xFF;
02349 mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+10] = (headw >> 8) & 0xFF;
02350 mp3state->mp3file.xingbuffer[mp3state->mp3file.xing_offset+11] = headw & 0xFF;
02351 }
02352 }
02353 else
02354
02355 {
02356 splt_d_print_debug(state,"Starting mp3 seekable non frame mode...\n");
02357
02358 long first_frame_offset = mp3state->inputBuffer + mp3state->buf_len - mp3state->data_ptr;
02359
02360
02361 if (mp3state->end == 0)
02362 {
02363 begin = (off_t) (fbegin_sec * mp3state->mp3file.bitrate + mp3state->mp3file.firsth);
02364
02365 if ((mp3state->bytes == begin) && (mp3state->data_len > 0))
02366 {
02367 write_first_frame = SPLT_TRUE;
02368 begin += first_frame_offset;
02369 }
02370 else
02371 {
02372 begin = splt_mp3_findvalidhead(mp3state, begin);
02373 }
02374
02375 if (begin == -1)
02376 {
02377 *error = SPLT_ERROR_BEGIN_OUT_OF_FILE;
02378 goto bloc_end2;
02379 }
02380 if (splt_mp3_tabsel_123[1 - mp3state->mp3file.mpgid][mp3state->mp3file.layer-1][splt_mp3_c_bitrate(mp3state->headw)] !=
02381 mp3state->mp3file.firsthead.bitrate)
02382 {
02383 check_bitrate = 1;
02384 }
02385 }
02386
02387 else
02388 {
02389 begin = mp3state->end;
02390 }
02391
02392 if (fend_sec_is_not_eof)
02393 {
02394 end = (off_t) (fend_sec * mp3state->mp3file.bitrate + mp3state->mp3file.firsth);
02395 if (write_first_frame)
02396 {
02397 end += first_frame_offset;
02398 }
02399
02400
02401 end = splt_mp3_findvalidhead(mp3state, end);
02402 if (splt_mp3_tabsel_123[1 - mp3state->mp3file.mpgid][mp3state->mp3file.layer-1][splt_mp3_c_bitrate(mp3state->headw)] !=
02403 mp3state->mp3file.firsthead.bitrate)
02404 check_bitrate = 1;
02405 }
02406 else
02407 {
02408 end = -1;
02409 }
02410
02411 splt_mp3_save_end_point(state, mp3state, save_end_point, end);
02412 }
02413
02414
02415 int err = splt_mp3_simple_split(state, output_fname, begin, end,
02416 SPLT_TRUE, write_first_frame);
02417 if (err < 0) { *error = err; }
02418
02419 if (!save_end_point)
02420 {
02421 if (splt_o_get_long_option(state, SPLT_OPT_OVERLAP_TIME) > 0)
02422 {
02423 mp3state->frames = 1;
02424 mp3state->first = 1;
02425 }
02426 }
02427 }
02428
02429 if (check_bitrate)
02430 {
02431 *error = SPLT_MIGHT_BE_VBR;
02432 }
02433
02434 if (*error == SPLT_OK) { *error = SPLT_OK_SPLIT; }
02435
02436 bloc_end2:
02437
02438 return sec_end_time;
02439 }
02440
02441
02442
02443
02446 static off_t splt_mp3_adjustsync(splt_mp3_state *mp3state, off_t begin, off_t end)
02447 {
02448 off_t position;
02449 position = begin;
02450 if (fseeko(mp3state->file_input, position, SEEK_SET)==-1)
02451 {
02452 return (off_t) (-1);
02453 }
02454
02455
02456 while (position++ < end)
02457 {
02458 if (fgetc(mp3state->file_input)=='T') {
02459 if (fgetc(mp3state->file_input)=='A') {
02460 if (fgetc(mp3state->file_input)=='G')
02461 return (position + 127);
02462 else position++;
02463 }
02464 if (fseeko(mp3state->file_input, -1, SEEK_CUR) == -1)
02465 {
02466 return (off_t) (-1);
02467 }
02468 }
02469 }
02470
02471 position = begin;
02472
02473 if (fseeko(mp3state->file_input, position, SEEK_SET)==-1)
02474 {
02475 return (off_t) (-1);
02476 }
02477
02478
02479 while (position++ < end)
02480 {
02481 if (fgetc(mp3state->file_input)=='I')
02482 {
02483 if (fgetc(mp3state->file_input)=='D')
02484 {
02485 if (fgetc(mp3state->file_input)=='3')
02486 {
02487 return (position - 1);
02488 }
02489 else
02490 {
02491 position++;
02492 }
02493 }
02494 if(fseeko(mp3state->file_input, -1, SEEK_CUR)==-1)
02495 {
02496 return (off_t) (-1);
02497 }
02498 }
02499 }
02500
02501 return end;
02502 }
02503
02505 static void splt_mp3_syncerror_search(splt_state *state, int *error)
02506 {
02507 off_t offset = 0;
02508 char *filename = splt_t_get_filename_to_split(state);
02509 int sync_err = SPLT_OK;
02510
02511 splt_mp3_state *mp3state = state->codec;
02512
02513 splt_c_put_progress_text(state,SPLT_PROGRESS_SEARCH_SYNC);
02514
02515 mp3state->h.ptr = mp3state->mp3file.firsthead.ptr;
02516 mp3state->h.framesize = mp3state->mp3file.firsthead.framesize;
02517
02518
02519 off_t st_size;
02520 if(splt_io_stat(filename, NULL, &st_size) == 0)
02521 {
02522
02523 sync_err = splt_se_serrors_append_point(state, 0);
02524 if (sync_err != SPLT_OK)
02525 {
02526 *error = sync_err;
02527 return;
02528 }
02529
02530
02531 while (state->serrors->serrors_points_num < SPLT_MAXSYNC)
02532 {
02533 offset = splt_mp3_findhead(mp3state, mp3state->h.ptr + mp3state->h.framesize);
02534 if (offset==-1)
02535 {
02536 break;
02537 }
02538
02539 if (offset != mp3state->h.ptr + mp3state->h.framesize)
02540 {
02541 off_t serror_point =
02542 splt_mp3_adjustsync(mp3state, mp3state->h.ptr, offset);
02543
02544 sync_err = splt_se_serrors_append_point(state, serror_point);
02545 if (sync_err != SPLT_OK)
02546 {
02547 *error = sync_err;
02548 return;
02549 }
02550 offset = splt_mp3_findvalidhead(mp3state, serror_point);
02551 if (splt_io_get_word(mp3state->file_input, offset, SEEK_SET, &mp3state->headw) == -1)
02552 {
02553 *error = SPLT_ERR_SYNC;
02554 return;
02555 }
02556 }
02557
02558 mp3state->h = splt_mp3_makehead (mp3state->headw, mp3state->mp3file,
02559 mp3state->h, offset);
02560
02561 if (splt_t_split_is_canceled(state))
02562 {
02563 *error = SPLT_SPLIT_CANCELLED;
02564 return;
02565 }
02566
02567
02568 splt_c_update_progress(state,(double)(offset),
02569 (double)(st_size),1,0,
02570 SPLT_DEFAULT_PROGRESS_RATE);
02571 }
02572 }
02573 else
02574 {
02575 splt_e_set_strerror_msg_with_data(state, filename);
02576 *error = SPLT_ERROR_CANNOT_OPEN_FILE;
02577 return;
02578 }
02579
02580 if (state->serrors->serrors_points_num == 0)
02581 {
02582 *error = SPLT_ERR_NO_SYNC_FOUND;
02583 return;
02584 }
02585
02586 if (state->serrors->serrors_points_num == SPLT_MAXSYNC)
02587 {
02588 *error = SPLT_ERR_TOO_MANY_SYNC_ERR;
02589 return;
02590 }
02591
02592
02593 sync_err = splt_se_serrors_append_point(state, LONG_MAX);
02594 if (sync_err != SPLT_OK)
02595 {
02596 *error = sync_err;
02597 return;
02598 }
02599
02600 *error = SPLT_SYNC_OK;
02601
02602 return;
02603 }
02604
02605
02606
02607
02609 static const unsigned char splt_mp3_albumwraphead[22] =
02610 {
02611 0xa, 0x23, 0x54, 0x49, 0x54, 0x32, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x41, 0x6c, 0x62, 0x75, 0x6d, 0x57, 0x72, 0x61, 0x70,
02612 };
02613
02619 static void splt_mp3_dewrap(int listonly, const char *dir, int *error, splt_state *state)
02620 {
02621 if (listonly)
02622 {
02623 *error = SPLT_OK;
02624 }
02625 else
02626 {
02627 *error = SPLT_DEWRAP_OK;
02628 }
02629
02630
02631 short albumwrap=0, mp3wrap=0;
02632
02633 int wrapfiles=0, i, j, k=0;
02634 unsigned char c;
02635 char filename[2048] = { '\0' };
02636 off_t begin=0, end=0, len = 0, id3offset = 0;
02637 char junk[2048] = { '\0' };
02638 char *file_to_dewrap = splt_t_get_filename_to_split(state);
02639
02640
02641 if (*error < 0)
02642 {
02643 return;
02644 }
02645 else
02646 {
02647 splt_mp3_state *mp3state = state->codec;
02648
02649 if (*error >= 0)
02650 {
02651 len = splt_io_get_file_length(state, mp3state->file_input, file_to_dewrap, error);
02652 if (error < 0) { return; }
02653
02654 id3offset = splt_mp3_getid3v2_end_offset(mp3state->file_input, 0);
02655
02656
02657 if (fseeko(mp3state->file_input, id3offset, SEEK_SET)==-1)
02658 {
02659 *error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
02660 return;
02661 }
02662
02663 splt_d_print_debug(state,"Searching for wrap string...\n");
02664
02665
02666
02667 for (i=0; i<16384; i++)
02668 {
02669 if (feof(mp3state->file_input))
02670 {
02671 *error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
02672 return;
02673 }
02674 if ((id3offset = ftello(mp3state->file_input))==-1)
02675 {
02676 *error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
02677 return;
02678 }
02679 if (fgetc(mp3state->file_input)=='W')
02680 if (fgetc(mp3state->file_input)=='R')
02681 if (fgetc(mp3state->file_input)=='A')
02682 if (fgetc(mp3state->file_input)=='P')
02683 {
02684 mp3wrap = 1;
02685 break;
02686 }
02687 }
02688
02689
02690
02691 if (!mp3wrap && (id3offset!=0))
02692 {
02693 if (fseeko(mp3state->file_input, (off_t) 8, SEEK_SET)==-1)
02694 {
02695 *error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
02696 return;
02697 }
02698 albumwrap = 1;
02699 for (i=0; i<22; i++)
02700 {
02701 if (splt_mp3_albumwraphead[i]!=fgetc(mp3state->file_input))
02702 {
02703 albumwrap = 0;
02704 break;
02705 }
02706 }
02707 }
02708
02709
02710 if (albumwrap || mp3wrap)
02711 {
02712 splt_d_print_debug(state,"Effective dewrap...\n");
02713
02714
02715 if (mp3wrap) {
02716 splt_d_print_debug(state,"Mp3 mp3wrap check...\n");
02717 short indexver;
02718
02719
02720 char major_v = fgetc(mp3state->file_input);
02721 char minor_v = fgetc(mp3state->file_input);
02722
02723 splt_c_put_info_message_to_client(state,
02724 _(" Detected file created with: Mp3Wrap v. %c.%c\n"),
02725 major_v,minor_v);
02726
02727 indexver = fgetc(mp3state->file_input);
02728 if (indexver > SPLT_MP3_INDEXVERSION)
02729 {
02730 *error = SPLT_DEWRAP_ERR_VERSION_OLD;
02731 return;
02732 }
02733 wrapfiles = (int) fgetc(mp3state->file_input);
02734 if (feof(mp3state->file_input))
02735 {
02736 *error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
02737 return;
02738 }
02739
02740 if (indexver > 0x0)
02741 {
02742 unsigned long crc = 0, fcrc = 0;
02743 if (splt_io_get_word(mp3state->file_input, 0, SEEK_CUR, &fcrc)==-1)
02744 {
02745 *error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
02746 return;
02747 }
02748
02749
02750 if (! splt_o_get_int_option(state, SPLT_OPT_QUIET_MODE))
02751 {
02752 begin = ftello(mp3state->file_input);
02753 if (fseeko(mp3state->file_input,
02754 splt_mp3_getid3v1_offset(mp3state->file_input), SEEK_END)==-1)
02755 {
02756 splt_e_set_strerror_msg_with_data(state, file_to_dewrap);
02757 *error = SPLT_ERROR_SEEKING_FILE;
02758 return;
02759 }
02760 end = ftello(mp3state->file_input);
02761 splt_c_put_info_message_to_client(state,
02762 _(" Check for file integrity: calculating CRC please wait... "));
02763 crc = splt_mp3_c_crc(state, mp3state->file_input, begin, end, error);
02764 if (*error < 0)
02765 {
02766 return;
02767 }
02768 if (crc != fcrc)
02769 {
02770
02771
02772
02773
02774
02775
02776 *error = SPLT_ERROR_CRC_FAILED;
02777 return;
02778 }
02779 else
02780 {
02781 splt_c_put_info_message_to_client(state, _(" OK\n"));
02782 }
02783 if (fseeko(mp3state->file_input, begin, SEEK_SET)==-1)
02784 {
02785 splt_e_set_strerror_msg_with_data(state, file_to_dewrap);
02786 *error = SPLT_ERROR_SEEKING_FILE;
02787 return;
02788 }
02789 }
02790 }
02791 }
02792
02793
02794
02795 if (albumwrap)
02796 {
02797 splt_d_print_debug(state,"Mp3 albumwrap check...\n");
02798
02799 splt_c_put_info_message_to_client(state,
02800 _(" Detected file created with: AlbumWrap\n"));
02801
02802 if (fseeko(mp3state->file_input, (off_t) 0x52d, SEEK_SET)==-1)
02803 {
02804 *error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
02805 return;
02806 }
02807 i = 0;
02808 while (((c=fgetc(mp3state->file_input))!=0x20) &&(i<2048))
02809 junk[i++] = c;
02810 junk[i] = '\0';
02811 wrapfiles = atoi (junk);
02812 }
02813 if (wrapfiles<=0)
02814 {
02815 *error = SPLT_DEWRAP_ERR_NO_FILE_OR_BAD_INDEX;
02816 return;
02817 }
02818
02819
02820 state->split.splitnumber = wrapfiles+1;
02821
02822 splt_c_put_info_message_to_client(state, _(" Total files: %d\n"),wrapfiles);
02823
02824
02825 for (i=0; i<wrapfiles; i++)
02826 {
02827 if (!splt_t_split_is_canceled(state))
02828 {
02829
02830 splt_t_set_current_split(state, i+1);
02831
02832
02833
02834 if (i==0)
02835 {
02836
02837 if (mp3wrap)
02838 {
02839 unsigned long w;
02840 if (splt_io_get_word (mp3state->file_input, 0, SEEK_CUR, &w)==-1)
02841 {
02842 splt_e_set_error_data(state,file_to_dewrap);
02843 *error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
02844 return;
02845 }
02846 begin = (off_t) (w + id3offset);
02847 }
02848
02849
02850 if (albumwrap)
02851 {
02852 if (fseeko (mp3state->file_input,
02853 (off_t) SPLT_MP3_ABWINDEXOFFSET, SEEK_SET)==-1)
02854 {
02855 splt_e_set_error_data(state,file_to_dewrap);
02856 *error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
02857 return;
02858 }
02859 j = 0;
02860 while ((c=fgetc(mp3state->file_input))!='[')
02861 if (j++ > 32)
02862 {
02863 splt_e_set_error_data(state,file_to_dewrap);
02864 *error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
02865 return;
02866 }
02867 if (fseeko(mp3state->file_input, (off_t) 3, SEEK_CUR)==-1)
02868 {
02869 splt_e_set_error_data(state,file_to_dewrap);
02870 *error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
02871 return;
02872 }
02873 j = 0;
02874 while ((j<2048) && ((c = fgetc(mp3state->file_input))!='['))
02875 if (c!='.') junk[j++] = c;
02876 else k = j;
02877 junk[j] = '\0';
02878 begin = (off_t) atol (junk);
02879 k = j - k;
02880 if (k<4)
02881 {
02882 for (j=0; j<(4-k); j++)
02883 {
02884 begin = begin * 10;
02885 }
02886 }
02887 }
02888 }
02889 else
02890 {
02891 begin = end;
02892 }
02893
02894
02895 if (mp3wrap)
02896 {
02897 unsigned long w;
02898 j = 0;
02899 do
02900 {
02901 c = fgetc(mp3state->file_input);
02902
02903 if (c==SPLT_NDIRCHAR)
02904 {
02905 c=SPLT_DIRCHAR;
02906 }
02907 filename[j++] = c;
02908 } while ((c!=0x00)&&(j<2048));
02909
02910 if (splt_io_get_word (mp3state->file_input, 0, SEEK_CUR, &w) == -1)
02911 {
02912 splt_e_set_error_data(state,file_to_dewrap);
02913 *error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
02914 return;
02915 }
02916
02917 end = (off_t) (w + id3offset);
02918
02919
02920
02921
02922 if (!listonly && (!dir || dir[0] == '\0'))
02923 {
02924 memset(junk, 0x00, 2048);
02925 char *ptr = filename;
02926 while (((ptr = strchr(ptr, SPLT_DIRCHAR))!=NULL)&&((ptr-filename)<2048))
02927 {
02928 ptr++;
02929 strncpy(junk, filename, ptr-filename);
02930 if (! splt_io_check_if_directory(junk))
02931 {
02932 if ((splt_io_mkdir(state, junk)) == -1)
02933 {
02934 *error = SPLT_ERROR_CANNOT_CREATE_DIRECTORY;
02935 splt_e_set_strerror_msg_with_data(state, junk);
02936 return;
02937 }
02938 }
02939 }
02940 }
02941 }
02942
02943
02944
02945 if (albumwrap)
02946 {
02947 if (i<wrapfiles-1)
02948 {
02949 if (fseeko (mp3state->file_input,
02950 (off_t) (SPLT_MP3_ABWINDEXOFFSET + (i * SPLT_MP3_ABWLEN)), SEEK_SET)==-1)
02951 {
02952 splt_e_set_error_data(state,file_to_dewrap);
02953 *error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
02954 return;
02955 }
02956 j = 0;
02957 while ((j<2048) && ((c = fgetc(mp3state->file_input))!='['))
02958 if (c!='.') junk[j++] = c;
02959 else k = j;
02960 junk[j] = '\0';
02961 end = (off_t) atol (junk);
02962 k = j - k;
02963 if (k<4)
02964 for (j=0; j<(4-k); j++)
02965 end = end * 10;
02966 end += begin;
02967 }
02968 else end = len;
02969
02970 if (fseeko (mp3state->file_input,
02971 (off_t) (SPLT_MP3_ABWINDEXOFFSET + (i*SPLT_MP3_ABWLEN)), SEEK_SET)==-1)
02972 {
02973 splt_e_set_error_data(state,file_to_dewrap);
02974 *error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
02975 }
02976 j = 0;
02977 while ((c=fgetc(mp3state->file_input))!='[')
02978 if (j++ > 32)
02979 {
02980 splt_e_set_error_data(state,file_to_dewrap);
02981 *error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
02982 return;
02983 }
02984 if (fseeko (mp3state->file_input, (off_t) 3, SEEK_CUR)==-1)
02985 {
02986 splt_e_set_error_data(state,file_to_dewrap);
02987 *error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
02988 return;
02989 }
02990 j = 0;
02991 while ((c=fgetc(mp3state->file_input))!='[')
02992 if (j++ > 32)
02993 {
02994 splt_e_set_error_data(state,file_to_dewrap);
02995 *error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
02996 return;
02997 }
02998 if (fseeko(mp3state->file_input, (off_t) 3, SEEK_CUR)==-1)
02999 {
03000 splt_e_set_error_data(state,file_to_dewrap);
03001 *error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
03002 return;
03003 }
03004 j = 0;
03005 while (j<=400)
03006 filename[j++] = fgetc(mp3state->file_input);
03007 for (j=400; j>0; j--) {
03008 if (filename[j]==0x20)
03009 filename[j]='\0';
03010 else break;
03011 }
03012 filename[j+1] = '\0';
03013 }
03014
03015 splt_d_print_debug(state,"Found the file _%s_\n", filename);
03016 splt_d_print_debug(state,"Cut the dirchar");
03017
03018
03019 char str_temp[4];
03020 snprintf(str_temp,4,"%c%c",'.',SPLT_DIRCHAR);
03021 if (strstr(filename,str_temp) != NULL)
03022 {
03023 char *filename2 = strdup(filename);
03024 if (!filename2)
03025 {
03026 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
03027 return;
03028 }
03029 else
03030 {
03031 snprintf(filename,2048, "%s", filename2+2);
03032 free(filename2);
03033 filename2 = NULL;
03034 }
03035 }
03036
03037 if (feof(mp3state->file_input))
03038 {
03039 splt_e_set_error_data(state,file_to_dewrap);
03040 *error = SPLT_DEWRAP_ERR_FILE_DAMAGED_INCOMPLETE;
03041 return;
03042 }
03043
03044
03045
03046 if (listonly)
03047 {
03048 splt_d_print_debug(state,"Only list wrapped files\n");
03049
03050 int put_file_error = SPLT_OK;
03051 put_file_error = splt_w_wrap_put_file(state, wrapfiles, i, filename);
03052 if (put_file_error != SPLT_OK)
03053 {
03054 *error = put_file_error;
03055 return;
03056 }
03057 }
03058
03059
03060 else
03061 {
03062 splt_d_print_debug(state,"Split wrapped file\n");
03063
03064 int ret = 0;
03065
03066
03067 if (dir && (dir[0] != '\0'))
03068 {
03069 char temp[2048] = { '\0' };
03070 strncpy(temp, filename, 2048);
03071 char *ptr = temp;
03072
03073 if ((ptr = strrchr(temp,SPLT_DIRCHAR)) == NULL)
03074 {
03075 ptr = temp;
03076 }
03077 else
03078 {
03079 if (ptr-temp > 0)
03080 {
03081 ptr++;
03082 }
03083 }
03084
03085 if (strcmp(dir,str_temp) == 0)
03086 {
03087 snprintf(filename, 2048,"%s%s", dir, ptr);
03088 }
03089 else
03090 {
03091 if (dir[strlen(dir)-1] == SPLT_DIRCHAR)
03092 {
03093 snprintf(filename, 2048,"%s%s", dir, ptr);
03094 }
03095 else
03096 {
03097 snprintf(filename, 2048,"%s%c%s", dir, SPLT_DIRCHAR, ptr);
03098 }
03099 }
03100 splt_d_print_debug(state,"wrap dir _%s_\n", dir);
03101 splt_d_print_debug(state,"wrap after dir _%s_\n", ptr);
03102 }
03103
03104
03105 if (mp3state->mp3file.xingbuffer)
03106 {
03107 free(mp3state->mp3file.xingbuffer);
03108 mp3state->mp3file.xingbuffer = NULL;
03109 }
03110 mp3state->mp3file.xing = 0;
03111
03112 int append_err = SPLT_OK;
03113 append_err = splt_sp_append_splitpoint(state,0,
03114 splt_su_get_fname_without_path(filename), SPLT_SPLITPOINT);
03115 if (append_err != SPLT_OK)
03116 {
03117 *error = append_err;
03118 return;
03119 }
03120
03121
03122 int cut_err = splt_sp_cut_splitpoint_extension(state,i);
03123 if (cut_err != SPLT_OK)
03124 {
03125 *error = cut_err;
03126 return;
03127 }
03128
03129
03130 ret = splt_mp3_simple_split(state, filename, begin, end,
03131 SPLT_FALSE, SPLT_FALSE);
03132
03133
03134 if (ret >= 0)
03135 {
03136 ret = splt_c_put_split_file(state, filename);
03137 if (ret < 0) { *error = ret; }
03138 }
03139 else
03140 {
03141 *error = ret;
03142 }
03143 }
03144 }
03145 }
03146 }
03147 else
03148 {
03149 *error = SPLT_DEWRAP_ERR_FILE_NOT_WRAPED_DAMAGED;
03150 return;
03151 }
03152 }
03153 }
03154 }
03155
03157 void splt_mp3_init(splt_state *state, int *error)
03158 {
03159 FILE *file_input = NULL;
03160 char *filename = splt_t_get_filename_to_split(state);
03161
03162 state->syncerrors = 0;
03163
03164
03165 if ((file_input = splt_mp3_open_file_read(state, filename, error)) != NULL)
03166 {
03167 splt_mp3_get_info(state, file_input, error);
03168
03169 if (*error >= 0)
03170 {
03171 splt_mp3_state *mp3state = state->codec;
03172 mp3state->off = splt_o_get_float_option(state,SPLT_OPT_PARAM_OFFSET);
03173
03174
03175 if (splt_t_get_total_time(state) > 0)
03176 {
03177 mp3state->frames = 1;
03178 }
03179 }
03180 }
03181 }
03182
03195 void splt_pl_set_plugin_info(splt_plugin_info *info, int *error)
03196 {
03197 float plugin_version = 1.0;
03198
03199
03200 info->version = plugin_version;
03201
03202
03203 info->name = malloc(sizeof(char) * 40);
03204 if (info->name != NULL)
03205 {
03206 snprintf(info->name, 39, "mp3 (libmad)");
03207 }
03208 else
03209 {
03210 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
03211 return;
03212 }
03213
03214
03215 info->extension = malloc(sizeof(char) * (strlen(SPLT_MP3EXT)+2));
03216 if (info->extension != NULL)
03217 {
03218 snprintf(info->extension, strlen(SPLT_MP3EXT)+1, SPLT_MP3EXT);
03219 }
03220 else
03221 {
03222 *error = SPLT_ERROR_CANNOT_ALLOCATE_MEMORY;
03223 return;
03224 }
03225
03226 info->upper_extension = splt_su_convert(info->extension, SPLT_TO_UPPERCASE, error);
03227 }
03228
03230 void splt_pl_init(splt_state *state, int *error)
03231 {
03232 if (splt_io_input_is_stdin(state))
03233 {
03234 char *filename = splt_t_get_filename_to_split(state);
03235 if (filename[1] == '\0')
03236 {
03237 splt_c_put_info_message_to_client(state,
03238 _(" warning: stdin '-' is supposed to be mp3 stream.\n"));
03239 }
03240 }
03241
03242 splt_mp3_init(state, error);
03243 }
03244
03246 void splt_pl_end(splt_state *state, int *error)
03247 {
03248
03249
03250 if ((splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_SILENCE_MODE)
03251 && (splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_TRIM_SILENCE_MODE)
03252 && (splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_ERROR_MODE)
03253 && (splt_o_get_int_option(state, SPLT_OPT_SPLIT_MODE) != SPLT_OPTION_WRAP_MODE))
03254 {
03255 if (splt_o_get_int_option(state, SPLT_OPT_FRAME_MODE))
03256 {
03257 if (*error >= 0)
03258 {
03259 splt_mp3_state *mp3state = state->codec;
03260
03261
03262 if (mp3state->frames != 1)
03263 {
03264 splt_c_put_info_message_to_client(state,
03265 _(" Processed %lu frames - Sync errors: %lu\n"),
03266 mp3state->frames, state->syncerrors);
03267 }
03268 }
03269 }
03270 }
03271 splt_mp3_end(state, error);
03272 }
03273
03275 int splt_pl_check_plugin_is_for_file(splt_state *state, int *error)
03276 {
03277 char *filename = splt_t_get_filename_to_split(state);
03278
03279 if (filename != NULL && ((strcmp(filename,"-") == 0) ||
03280 (strcmp(filename,"m-") == 0)))
03281 {
03282 return SPLT_TRUE;
03283 }
03284
03285 int is_mp3 = SPLT_FALSE;
03286
03287 splt_o_lock_messages(state);
03288 splt_mp3_init(state, error);
03289 splt_o_unlock_messages(state);
03290 if (*error >= 0)
03291 {
03292 splt_mp3_state *mp3state = state->codec;
03293 if (mp3state)
03294 {
03295 is_mp3 = SPLT_TRUE;
03296 }
03297 }
03298 splt_mp3_end(state, error);
03299
03300 return is_mp3;
03301 }
03302
03304 void splt_pl_search_syncerrors(splt_state *state, int *error)
03305 {
03306
03307 splt_mp3_syncerror_search(state, error);
03308 }
03309
03311 void splt_pl_dewrap(splt_state *state, int listonly, const char *dir, int *error)
03312 {
03313 splt_w_wrap_free(state);
03314 splt_mp3_dewrap(listonly, dir, error, state);
03315 }
03316
03317 double splt_pl_split(splt_state *state, const char *final_fname,
03318 double begin_point, double end_point, int *error, int save_end_point)
03319 {
03320 return splt_mp3_split(final_fname, state, begin_point, end_point, error, save_end_point);
03321 }
03322
03324 int splt_pl_simple_split(splt_state *state, char *output_fname, off_t begin, off_t end)
03325 {
03326 int error = SPLT_OK;
03327
03328 splt_mp3_state *mp3state = state->codec;
03329
03330
03331 if (splt_t_get_total_time(state) > 0)
03332 {
03333 mp3state->frames = 1;
03334 }
03335
03336
03337 error = splt_mp3_simple_split(state, output_fname, begin, end,
03338 SPLT_FALSE, SPLT_FALSE);
03339
03340 return error;
03341 }
03342
03344 int splt_pl_scan_silence(splt_state *state, int *error)
03345 {
03346 float offset = splt_o_get_float_option(state,SPLT_OPT_PARAM_OFFSET);
03347 float threshold = splt_o_get_float_option(state, SPLT_OPT_PARAM_THRESHOLD);
03348 float min_length = splt_o_get_float_option(state, SPLT_OPT_PARAM_MIN_LENGTH);
03349
03350 splt_mp3_state *mp3state = state->codec;
03351 mp3state->off = offset;
03352
03353 int found = splt_mp3_scan_silence(state, mp3state->mp3file.firsthead.ptr, 0,
03354 threshold, min_length, 1, error, splt_scan_silence_processor);
03355 if (*error < 0) { return -1; }
03356
03357 return found;
03358 }
03359
03361 int splt_pl_scan_trim_silence(splt_state *state, int *error)
03362 {
03363 float threshold = splt_o_get_float_option(state, SPLT_OPT_PARAM_THRESHOLD);
03364
03365 splt_mp3_state *mp3state = state->codec;
03366
03367 int found = splt_mp3_scan_silence(state, mp3state->mp3file.firsthead.ptr, 0,
03368 threshold, 0, 1, error, splt_trim_silence_processor);
03369 if (*error < 0) { return -1; }
03370
03371 return found;
03372 }
03373
03375 void splt_pl_set_original_tags(splt_state *state, int *error)
03376 {
03377 splt_d_print_debug(state, "Getting original tags ...");
03378 #ifndef NO_ID3TAG
03379 splt_d_print_debug(state, "Taking original ID3 tags from file using libid3tag ...\n");
03380 splt_mp3_get_original_tags(splt_t_get_filename_to_split(state), state, error);
03381 #else
03382 splt_d_print_debug(state, "Warning ! NO_ID3TAG");
03383
03384 #endif
03385 }
03386
03387 void splt_pl_clear_original_tags(splt_original_tags *original_tags)
03388 {
03389 #ifndef NO_ID3TAG
03390 tag_bytes_and_size *bytes_and_size = (tag_bytes_and_size *) original_tags->all_original_tags;
03391
03392 if (!bytes_and_size) {
03393 return;
03394 }
03395
03396 if (bytes_and_size->tag_bytes)
03397 {
03398 free(bytes_and_size->tag_bytes);
03399 bytes_and_size->tag_bytes = NULL;
03400 }
03401
03402 bytes_and_size->tag_length = 0;
03403
03404 free(original_tags->all_original_tags);
03405 original_tags->all_original_tags = NULL;
03406 #endif
03407 }
03408