1 /* grabbag - Convenience lib for various routines common to several tools
2 * Copyright (C) 2002,2003,2004 Josh Coalson
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.
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.
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.
19 #include "share/grabbag.h"
20 #include "FLAC/assert.h"
25 unsigned grabbag__cuesheet_msf_to_frame(unsigned minutes, unsigned seconds, unsigned frames)
27 return ((minutes * 60) + seconds) * 75 + frames;
30 void grabbag__cuesheet_frame_to_msf(unsigned frame, unsigned *minutes, unsigned *seconds, unsigned *frames)
34 *seconds = frame % 60;
39 /* since we only care about values >= 0 or error, returns < 0 for any illegal string, else value */
40 static int local__parse_int_(const char *s)
48 while('\0' != (c = *s++))
49 if(c >= '0' && c <= '9')
50 ret = ret * 10 + (c - '0');
57 /* since we only care about values >= 0 or error, returns < 0 for any illegal string, else value */
58 static FLAC__int64 local__parse_int64_(const char *s)
66 while('\0' != (c = *s++))
67 if(c >= '0' && c <= '9')
68 ret = ret * 10 + (c - '0');
75 /* accept '[0-9]+:[0-9][0-9]?:[0-9][0-9]?', but max second of 59 and max frame of 74, e.g. 0:0:0, 123:45:67
76 * return sample number or <0 for error
78 static FLAC__int64 local__parse_msf_(const char *s)
80 FLAC__int64 ret, field;
84 if(c >= '0' && c <= '9')
88 while(':' != (c = *s++)) {
89 if(c >= '0' && c <= '9')
90 field = field * 10 + (c - '0');
95 ret = field * 60 * 44100;
98 if(c >= '0' && c <= '9')
102 if(':' != (c = *s++)) {
103 if(c >= '0' && c <= '9') {
104 field = field * 10 + (c - '0');
116 ret += field * 44100;
119 if(c >= '0' && c <= '9')
123 if('\0' != (c = *s++)) {
124 if(c >= '0' && c <= '9') {
125 field = field * 10 + (c - '0');
138 ret += field * (44100 / 75);
143 static char *local__get_field_(char **s, FLAC__bool allow_quotes)
145 FLAC__bool has_quote = false;
148 FLAC__ASSERT(0 != s);
153 /* skip leading whitespace */
154 while(**s && 0 != strchr(" \t\r\n", **s))
162 if(allow_quotes && (**s == '"')) {
174 *s = strchr(*s, '\"');
175 /* if there is no matching end quote, it's an error */
184 while(**s && 0 == strchr(" \t\r\n", **s))
197 static FLAC__bool local__cuesheet_parse_(FILE *file, const char **error_message, unsigned *last_line_read, FLAC__StreamMetadata *cuesheet, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
199 #if defined _MSC_VER || defined __MINGW32__
200 #define FLAC__STRCASECMP stricmp
202 #define FLAC__STRCASECMP strcasecmp
204 char buffer[4096], *line, *field;
205 unsigned linelen, forced_leadout_track_num = 0;
206 FLAC__uint64 forced_leadout_track_offset = 0;
207 int in_track_num = -1, in_index_num = -1;
208 FLAC__bool disc_has_catalog = false, track_has_flags = false, track_has_isrc = false, has_forced_leadout = false;
209 FLAC__StreamMetadata_CueSheet *cs = &cuesheet->data.cue_sheet;
211 cs->lead_in = is_cdda? 2 * 44100 /* The default lead-in size for CD-DA */ : 0;
214 while(0 != fgets(buffer, sizeof(buffer), file)) {
218 linelen = strlen(line);
219 if(line[linelen-1] != '\n') {
220 *error_message = "line too long";
224 if(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
225 if(0 == FLAC__STRCASECMP(field, "CATALOG")) {
226 if(disc_has_catalog) {
227 *error_message = "found multiple CATALOG commands";
230 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/true))) {
231 *error_message = "CATALOG is missing catalog number";
234 if(strlen(field) >= sizeof(cs->media_catalog_number)) {
235 *error_message = "CATALOG number is too long";
238 if(is_cdda && (strlen(field) != 13 || strspn(field, "0123456789") != 13)) {
239 *error_message = "CD-DA CATALOG number must be 13 decimal digits";
242 strcpy(cs->media_catalog_number, field);
243 disc_has_catalog = true;
245 else if(0 == FLAC__STRCASECMP(field, "FLAGS")) {
246 if(track_has_flags) {
247 *error_message = "found multiple FLAGS commands";
250 if(in_track_num < 0 || in_index_num >= 0) {
251 *error_message = "FLAGS command must come after TRACK but before INDEX";
254 while(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
255 if(0 == FLAC__STRCASECMP(field, "PRE"))
256 cs->tracks[cs->num_tracks-1].pre_emphasis = 1;
258 track_has_flags = true;
260 else if(0 == FLAC__STRCASECMP(field, "INDEX")) {
262 FLAC__StreamMetadata_CueSheet_Track *track = &cs->tracks[cs->num_tracks-1];
263 if(in_track_num < 0) {
264 *error_message = "found INDEX before any TRACK";
267 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
268 *error_message = "INDEX is missing index number";
271 in_index_num = local__parse_int_(field);
272 if(in_index_num < 0) {
273 *error_message = "INDEX has invalid index number";
276 FLAC__ASSERT(cs->num_tracks > 0);
277 if(track->num_indices == 0) {
278 /* it's the first index point of the track */
279 if(in_index_num > 1) {
280 *error_message = "first INDEX number of a TRACK must be 0 or 1";
285 if(in_index_num != track->indices[track->num_indices-1].number + 1) {
286 *error_message = "INDEX numbers must be sequential";
290 if(is_cdda && in_index_num > 99) {
291 *error_message = "CD-DA INDEX number must be between 0 and 99, inclusive";
294 /*@@@ search for duplicate track number? */
295 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
296 *error_message = "INDEX is missing an offset after the index number";
299 xx = local__parse_msf_(field);
302 *error_message = "illegal INDEX offset (not of the form MM:SS:FF)";
305 xx = local__parse_int64_(field);
307 *error_message = "illegal INDEX offset";
311 if(is_cdda && cs->num_tracks == 1 && cs->tracks[0].num_indices == 0 && xx != 0) {
312 *error_message = "first INDEX of first TRACK must have an offset of 00:00:00";
315 if(is_cdda && track->num_indices > 0 && (FLAC__uint64)xx <= track->indices[track->num_indices-1].offset) {
316 *error_message = "CD-DA INDEX offsets must increase in time";
319 /* fill in track offset if it's the first index of the track */
320 if(track->num_indices == 0)
321 track->offset = (FLAC__uint64)xx;
322 if(is_cdda && cs->num_tracks > 1) {
323 const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-2];
324 if((FLAC__uint64)xx <= prev->offset + prev->indices[prev->num_indices-1].offset) {
325 *error_message = "CD-DA INDEX offsets must increase in time";
329 if(!FLAC__metadata_object_cuesheet_track_insert_blank_index(cuesheet, cs->num_tracks-1, track->num_indices)) {
330 *error_message = "memory allocation error";
333 track->indices[track->num_indices-1].offset = (FLAC__uint64)xx - track->offset;
334 track->indices[track->num_indices-1].number = in_index_num;
336 else if(0 == FLAC__STRCASECMP(field, "ISRC")) {
339 *error_message = "found multiple ISRC commands";
342 if(in_track_num < 0 || in_index_num >= 0) {
343 *error_message = "ISRC command must come after TRACK but before INDEX";
346 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
347 *error_message = "ISRC is missing ISRC number";
350 /* strip out dashes */
351 for(l = r = field; *r; r++) {
356 if(strlen(field) != 12 || strspn(field, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") < 5 || strspn(field+5, "1234567890") != 7) {
357 *error_message = "invalid ISRC number";
360 strcpy(cs->tracks[cs->num_tracks-1].isrc, field);
361 track_has_isrc = true;
363 else if(0 == FLAC__STRCASECMP(field, "TRACK")) {
364 if(cs->num_tracks > 0) {
365 const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
367 prev->num_indices == 0 ||
371 (prev->num_indices == 1 && prev->indices[0].number != 1) ||
372 (prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
376 *error_message = is_cdda?
377 "previous TRACK must specify at least one INDEX 01" :
378 "previous TRACK must specify at least one INDEX";
382 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
383 *error_message = "TRACK is missing track number";
386 in_track_num = local__parse_int_(field);
387 if(in_track_num < 0) {
388 *error_message = "TRACK has invalid track number";
391 if(in_track_num == 0) {
392 *error_message = "TRACK number must be greater than 0";
395 if(is_cdda && in_track_num > 99) {
396 *error_message = "CD-DA TRACK number must be between 1 and 99, inclusive";
399 if(is_cdda && cs->num_tracks > 0 && in_track_num != cs->tracks[cs->num_tracks-1].number + 1) {
400 *error_message = "CD-DA TRACK numbers must be sequential";
403 /*@@@ search for duplicate track number? */
404 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
405 *error_message = "TRACK is missing a track type after the track number";
408 if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
409 *error_message = "memory allocation error";
412 cs->tracks[cs->num_tracks-1].number = in_track_num;
413 cs->tracks[cs->num_tracks-1].type = (0 == FLAC__STRCASECMP(field, "AUDIO"))? 0 : 1; /*@@@ should we be more strict with the value here? */
415 track_has_flags = false;
416 track_has_isrc = false;
418 else if(0 == FLAC__STRCASECMP(field, "REM")) {
419 if(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
420 if(0 == strcmp(field, "FLAC__lead-in")) {
422 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
423 *error_message = "FLAC__lead-in is missing offset";
426 xx = local__parse_int64_(field);
428 *error_message = "illegal FLAC__lead-in offset";
431 if(is_cdda && xx % 588 != 0) {
432 *error_message = "illegal CD-DA FLAC__lead-in offset, must be even multiple of 588 samples";
435 cs->lead_in = (FLAC__uint64)xx;
437 else if(0 == strcmp(field, "FLAC__lead-out")) {
440 if(has_forced_leadout) {
441 *error_message = "multiple FLAC__lead-out commands";
444 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
445 *error_message = "FLAC__lead-out is missing track number";
448 track_num = local__parse_int_(field);
450 *error_message = "illegal FLAC__lead-out track number";
453 forced_leadout_track_num = (unsigned)track_num;
454 /*@@@ search for duplicate track number? */
455 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
456 *error_message = "FLAC__lead-out is missing offset";
459 offset = local__parse_int64_(field);
461 *error_message = "illegal FLAC__lead-out offset";
464 forced_leadout_track_offset = (FLAC__uint64)offset;
465 if(forced_leadout_track_offset != lead_out_offset) {
466 *error_message = "FLAC__lead-out offset does not match end-of-stream offset";
469 has_forced_leadout = true;
476 if(cs->num_tracks == 0) {
477 *error_message = "there must be at least one TRACK command";
481 const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
483 prev->num_indices == 0 ||
487 (prev->num_indices == 1 && prev->indices[0].number != 1) ||
488 (prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
492 *error_message = is_cdda?
493 "previous TRACK must specify at least one INDEX 01" :
494 "previous TRACK must specify at least one INDEX";
499 if(!has_forced_leadout) {
500 forced_leadout_track_num = is_cdda? 170 : cs->num_tracks;
501 forced_leadout_track_offset = lead_out_offset;
503 if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
504 *error_message = "memory allocation error";
507 cs->tracks[cs->num_tracks-1].number = forced_leadout_track_num;
508 cs->tracks[cs->num_tracks-1].offset = forced_leadout_track_offset;
511 *error_message = "read error";
515 #undef FLAC__STRCASECMP
518 FLAC__StreamMetadata *grabbag__cuesheet_parse(FILE *file, const char **error_message, unsigned *last_line_read, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
520 FLAC__StreamMetadata *cuesheet;
522 FLAC__ASSERT(0 != file);
523 FLAC__ASSERT(0 != error_message);
524 FLAC__ASSERT(0 != last_line_read);
527 cuesheet = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET);
530 *error_message = "memory allocation error";
534 if(!local__cuesheet_parse_(file, error_message, last_line_read, cuesheet, is_cdda, lead_out_offset)) {
535 FLAC__metadata_object_delete(cuesheet);
542 void grabbag__cuesheet_emit(FILE *file, const FLAC__StreamMetadata *cuesheet, const char *file_reference)
544 const FLAC__StreamMetadata_CueSheet *cs;
545 unsigned track_num, index_num;
547 FLAC__ASSERT(0 != file);
548 FLAC__ASSERT(0 != cuesheet);
549 FLAC__ASSERT(cuesheet->type == FLAC__METADATA_TYPE_CUESHEET);
551 cs = &cuesheet->data.cue_sheet;
553 if(*(cs->media_catalog_number))
554 fprintf(file, "CATALOG %s\n", cs->media_catalog_number);
555 fprintf(file, "FILE %s\n", file_reference);
557 for(track_num = 0; track_num < cs->num_tracks-1; track_num++) {
558 const FLAC__StreamMetadata_CueSheet_Track *track = cs->tracks + track_num;
560 fprintf(file, " TRACK %02u %s\n", (unsigned)track->number, track->type == 0? "AUDIO" : "DATA");
562 if(track->pre_emphasis)
563 fprintf(file, " FLAGS PRE\n");
565 fprintf(file, " ISRC %s\n", track->isrc);
567 for(index_num = 0; index_num < track->num_indices; index_num++) {
568 const FLAC__StreamMetadata_CueSheet_Index *index = track->indices + index_num;
570 fprintf(file, " INDEX %02u ", (unsigned)index->number);
572 const unsigned logical_frame = (unsigned)((track->offset + index->offset) / (44100 / 75));
574 grabbag__cuesheet_frame_to_msf(logical_frame, &m, &s, &f);
575 fprintf(file, "%02u:%02u:%02u\n", m, s, f);
579 fprintf(file, "%I64u\n", track->offset + index->offset);
581 fprintf(file, "%llu\n", track->offset + index->offset);
587 fprintf(file, "REM FLAC__lead-in %I64u\n", cs->lead_in);
588 fprintf(file, "REM FLAC__lead-out %u %I64u\n", (unsigned)cs->tracks[track_num].number, cs->tracks[track_num].offset);
590 fprintf(file, "REM FLAC__lead-in %llu\n", cs->lead_in);
591 fprintf(file, "REM FLAC__lead-out %u %llu\n", (unsigned)cs->tracks[track_num].number, cs->tracks[track_num].offset);