1 /* grabbag - Convenience lib for various routines common to several tools
2 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "share/grabbag.h"
24 #include "FLAC/assert.h"
29 unsigned grabbag__cuesheet_msf_to_frame(unsigned minutes, unsigned seconds, unsigned frames)
31 return ((minutes * 60) + seconds) * 75 + frames;
34 void grabbag__cuesheet_frame_to_msf(unsigned frame, unsigned *minutes, unsigned *seconds, unsigned *frames)
38 *seconds = frame % 60;
43 /* since we only care about values >= 0 or error, returns < 0 for any illegal string, else value */
44 static int local__parse_int_(const char *s)
52 while('\0' != (c = *s++))
53 if(c >= '0' && c <= '9')
54 ret = ret * 10 + (c - '0');
61 /* since we only care about values >= 0 or error, returns < 0 for any illegal string, else value */
62 static FLAC__int64 local__parse_int64_(const char *s)
70 while('\0' != (c = *s++))
71 if(c >= '0' && c <= '9')
72 ret = ret * 10 + (c - '0');
79 /* accept minute:second:frame syntax of '[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
80 * return sample number or <0 for error
81 * WATCHOUT: if sample rate is not evenly divisible by 75, the resulting sample number will be approximate
83 static FLAC__int64 local__parse_msf_(const char *s, unsigned sample_rate)
85 FLAC__int64 ret, field;
89 if(c >= '0' && c <= '9')
93 while(':' != (c = *s++)) {
94 if(c >= '0' && c <= '9')
95 field = field * 10 + (c - '0');
100 ret = field * 60 * sample_rate;
103 if(c >= '0' && c <= '9')
107 if(':' != (c = *s++)) {
108 if(c >= '0' && c <= '9') {
109 field = field * 10 + (c - '0');
121 ret += field * sample_rate;
124 if(c >= '0' && c <= '9')
128 if('\0' != (c = *s++)) {
129 if(c >= '0' && c <= '9') {
130 field = field * 10 + (c - '0');
143 ret += field * (sample_rate / 75);
148 /* accept minute:second syntax of '[0-9]+:[0-9][0-9]?{,.[0-9]+}', but second < 60, e.g. 0:0.0, 3:5, 15:31.731
149 * return sample number or <0 for error
150 * WATCHOUT: depending on the sample rate, the resulting sample number may be approximate with fractional seconds
152 static FLAC__int64 local__parse_ms_(const char *s, unsigned sample_rate)
154 FLAC__int64 ret, field;
159 if(c >= '0' && c <= '9')
163 while(':' != (c = *s++)) {
164 if(c >= '0' && c <= '9')
165 field = field * 10 + (c - '0');
170 ret = field * 60 * sample_rate;
172 s++; /* skip the ':' */
173 if(strspn(s, "0123456789.") != strlen(s))
178 if(x < 0.0 || x >= 60.0)
181 ret += (FLAC__int64)(x * sample_rate);
186 static char *local__get_field_(char **s, FLAC__bool allow_quotes)
188 FLAC__bool has_quote = false;
191 FLAC__ASSERT(0 != s);
196 /* skip leading whitespace */
197 while(**s && 0 != strchr(" \t\r\n", **s))
205 if(allow_quotes && (**s == '"')) {
217 *s = strchr(*s, '\"');
218 /* if there is no matching end quote, it's an error */
227 while(**s && 0 == strchr(" \t\r\n", **s))
240 static FLAC__bool local__cuesheet_parse_(FILE *file, const char **error_message, unsigned *last_line_read, FLAC__StreamMetadata *cuesheet, unsigned sample_rate, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
242 #if defined _MSC_VER || defined __MINGW32__ || defined __EMX__
243 #define FLAC__STRCASECMP stricmp
245 #define FLAC__STRCASECMP strcasecmp
247 char buffer[4096], *line, *field;
248 unsigned forced_leadout_track_num = 0;
249 FLAC__uint64 forced_leadout_track_offset = 0;
250 int in_track_num = -1, in_index_num = -1;
251 FLAC__bool disc_has_catalog = false, track_has_flags = false, track_has_isrc = false, has_forced_leadout = false;
252 FLAC__StreamMetadata_CueSheet *cs = &cuesheet->data.cue_sheet;
254 FLAC__ASSERT(!is_cdda || sample_rate == 44100);
255 /* double protection */
256 if(is_cdda && sample_rate != 44100) {
257 *error_message = "CD-DA cuesheet only allowed with 44.1kHz sample rate";
261 cs->lead_in = is_cdda? 2 * 44100 /* The default lead-in size for CD-DA */ : 0;
264 while(0 != fgets(buffer, sizeof(buffer), file)) {
269 size_t linelen = strlen(line);
270 if((linelen == sizeof(buffer)-1) && line[linelen-1] != '\n') {
271 *error_message = "line too long";
276 if(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
277 if(0 == FLAC__STRCASECMP(field, "CATALOG")) {
278 if(disc_has_catalog) {
279 *error_message = "found multiple CATALOG commands";
282 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/true))) {
283 *error_message = "CATALOG is missing catalog number";
286 if(strlen(field) >= sizeof(cs->media_catalog_number)) {
287 *error_message = "CATALOG number is too long";
290 if(is_cdda && (strlen(field) != 13 || strspn(field, "0123456789") != 13)) {
291 *error_message = "CD-DA CATALOG number must be 13 decimal digits";
294 strcpy(cs->media_catalog_number, field);
295 disc_has_catalog = true;
297 else if(0 == FLAC__STRCASECMP(field, "FLAGS")) {
298 if(track_has_flags) {
299 *error_message = "found multiple FLAGS commands";
302 if(in_track_num < 0 || in_index_num >= 0) {
303 *error_message = "FLAGS command must come after TRACK but before INDEX";
306 while(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
307 if(0 == FLAC__STRCASECMP(field, "PRE"))
308 cs->tracks[cs->num_tracks-1].pre_emphasis = 1;
310 track_has_flags = true;
312 else if(0 == FLAC__STRCASECMP(field, "INDEX")) {
314 FLAC__StreamMetadata_CueSheet_Track *track = &cs->tracks[cs->num_tracks-1];
315 if(in_track_num < 0) {
316 *error_message = "found INDEX before any TRACK";
319 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
320 *error_message = "INDEX is missing index number";
323 in_index_num = local__parse_int_(field);
324 if(in_index_num < 0) {
325 *error_message = "INDEX has invalid index number";
328 FLAC__ASSERT(cs->num_tracks > 0);
329 if(track->num_indices == 0) {
330 /* it's the first index point of the track */
331 if(in_index_num > 1) {
332 *error_message = "first INDEX number of a TRACK must be 0 or 1";
337 if(in_index_num != track->indices[track->num_indices-1].number + 1) {
338 *error_message = "INDEX numbers must be sequential";
342 if(is_cdda && in_index_num > 99) {
343 *error_message = "CD-DA INDEX number must be between 0 and 99, inclusive";
346 /*@@@ search for duplicate track number? */
347 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
348 *error_message = "INDEX is missing an offset after the index number";
351 /* first parse as minute:second:frame format */
352 xx = local__parse_msf_(field, sample_rate);
354 /* CD-DA must use only MM:SS:FF format */
356 *error_message = "illegal INDEX offset (not of the form MM:SS:FF)";
359 /* as an extension for non-CD-DA we allow MM:SS.SS or raw sample number */
360 xx = local__parse_ms_(field, sample_rate);
362 xx = local__parse_int64_(field);
364 *error_message = "illegal INDEX offset";
369 else if(sample_rate % 75) {
370 *error_message = "illegal INDEX offset (MM:SS:FF form not allowed if sample rate is not a multiple of 75)";
373 if(is_cdda && cs->num_tracks == 1 && cs->tracks[0].num_indices == 0 && xx != 0) {
374 *error_message = "first INDEX of first TRACK must have an offset of 00:00:00";
377 if(is_cdda && track->num_indices > 0 && (FLAC__uint64)xx <= track->indices[track->num_indices-1].offset) {
378 *error_message = "CD-DA INDEX offsets must increase in time";
381 /* fill in track offset if it's the first index of the track */
382 if(track->num_indices == 0)
383 track->offset = (FLAC__uint64)xx;
384 if(is_cdda && cs->num_tracks > 1) {
385 const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-2];
386 if((FLAC__uint64)xx <= prev->offset + prev->indices[prev->num_indices-1].offset) {
387 *error_message = "CD-DA INDEX offsets must increase in time";
391 if(!FLAC__metadata_object_cuesheet_track_insert_blank_index(cuesheet, cs->num_tracks-1, track->num_indices)) {
392 *error_message = "memory allocation error";
395 track->indices[track->num_indices-1].offset = (FLAC__uint64)xx - track->offset;
396 track->indices[track->num_indices-1].number = in_index_num;
398 else if(0 == FLAC__STRCASECMP(field, "ISRC")) {
401 *error_message = "found multiple ISRC commands";
404 if(in_track_num < 0 || in_index_num >= 0) {
405 *error_message = "ISRC command must come after TRACK but before INDEX";
408 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
409 *error_message = "ISRC is missing ISRC number";
412 /* strip out dashes */
413 for(l = r = field; *r; r++) {
418 if(strlen(field) != 12 || strspn(field, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") < 5 || strspn(field+5, "1234567890") != 7) {
419 *error_message = "invalid ISRC number";
422 strcpy(cs->tracks[cs->num_tracks-1].isrc, field);
423 track_has_isrc = true;
425 else if(0 == FLAC__STRCASECMP(field, "TRACK")) {
426 if(cs->num_tracks > 0) {
427 const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
429 prev->num_indices == 0 ||
433 (prev->num_indices == 1 && prev->indices[0].number != 1) ||
434 (prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
438 *error_message = is_cdda?
439 "previous TRACK must specify at least one INDEX 01" :
440 "previous TRACK must specify at least one INDEX";
444 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
445 *error_message = "TRACK is missing track number";
448 in_track_num = local__parse_int_(field);
449 if(in_track_num < 0) {
450 *error_message = "TRACK has invalid track number";
453 if(in_track_num == 0) {
454 *error_message = "TRACK number must be greater than 0";
458 if(in_track_num > 99) {
459 *error_message = "CD-DA TRACK number must be between 1 and 99, inclusive";
464 if(in_track_num == 255) {
465 *error_message = "TRACK number 255 is reserved for the lead-out";
468 else if(in_track_num > 255) {
469 *error_message = "TRACK number must be between 1 and 254, inclusive";
473 if(is_cdda && cs->num_tracks > 0 && in_track_num != cs->tracks[cs->num_tracks-1].number + 1) {
474 *error_message = "CD-DA TRACK numbers must be sequential";
477 /*@@@ search for duplicate track number? */
478 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
479 *error_message = "TRACK is missing a track type after the track number";
482 if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
483 *error_message = "memory allocation error";
486 cs->tracks[cs->num_tracks-1].number = in_track_num;
487 cs->tracks[cs->num_tracks-1].type = (0 == FLAC__STRCASECMP(field, "AUDIO"))? 0 : 1; /*@@@ should we be more strict with the value here? */
489 track_has_flags = false;
490 track_has_isrc = false;
492 else if(0 == FLAC__STRCASECMP(field, "REM")) {
493 if(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
494 if(0 == strcmp(field, "FLAC__lead-in")) {
496 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
497 *error_message = "FLAC__lead-in is missing offset";
500 xx = local__parse_int64_(field);
502 *error_message = "illegal FLAC__lead-in offset";
505 if(is_cdda && xx % 588 != 0) {
506 *error_message = "illegal CD-DA FLAC__lead-in offset, must be even multiple of 588 samples";
509 cs->lead_in = (FLAC__uint64)xx;
511 else if(0 == strcmp(field, "FLAC__lead-out")) {
514 if(has_forced_leadout) {
515 *error_message = "multiple FLAC__lead-out commands";
518 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
519 *error_message = "FLAC__lead-out is missing track number";
522 track_num = local__parse_int_(field);
524 *error_message = "illegal FLAC__lead-out track number";
527 forced_leadout_track_num = (unsigned)track_num;
528 /*@@@ search for duplicate track number? */
529 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
530 *error_message = "FLAC__lead-out is missing offset";
533 offset = local__parse_int64_(field);
535 *error_message = "illegal FLAC__lead-out offset";
538 forced_leadout_track_offset = (FLAC__uint64)offset;
539 if(forced_leadout_track_offset != lead_out_offset) {
540 *error_message = "FLAC__lead-out offset does not match end-of-stream offset";
543 has_forced_leadout = true;
550 if(cs->num_tracks == 0) {
551 *error_message = "there must be at least one TRACK command";
555 const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
557 prev->num_indices == 0 ||
561 (prev->num_indices == 1 && prev->indices[0].number != 1) ||
562 (prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
566 *error_message = is_cdda?
567 "previous TRACK must specify at least one INDEX 01" :
568 "previous TRACK must specify at least one INDEX";
573 if(!has_forced_leadout) {
574 forced_leadout_track_num = is_cdda? 170 : 255;
575 forced_leadout_track_offset = lead_out_offset;
577 if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
578 *error_message = "memory allocation error";
581 cs->tracks[cs->num_tracks-1].number = forced_leadout_track_num;
582 cs->tracks[cs->num_tracks-1].offset = forced_leadout_track_offset;
585 *error_message = "read error";
589 #undef FLAC__STRCASECMP
592 FLAC__StreamMetadata *grabbag__cuesheet_parse(FILE *file, const char **error_message, unsigned *last_line_read, unsigned sample_rate, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
594 FLAC__StreamMetadata *cuesheet;
596 FLAC__ASSERT(0 != file);
597 FLAC__ASSERT(0 != error_message);
598 FLAC__ASSERT(0 != last_line_read);
601 cuesheet = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET);
604 *error_message = "memory allocation error";
608 if(!local__cuesheet_parse_(file, error_message, last_line_read, cuesheet, sample_rate, is_cdda, lead_out_offset)) {
609 FLAC__metadata_object_delete(cuesheet);
616 void grabbag__cuesheet_emit(FILE *file, const FLAC__StreamMetadata *cuesheet, const char *file_reference)
618 const FLAC__StreamMetadata_CueSheet *cs;
619 unsigned track_num, index_num;
621 FLAC__ASSERT(0 != file);
622 FLAC__ASSERT(0 != cuesheet);
623 FLAC__ASSERT(cuesheet->type == FLAC__METADATA_TYPE_CUESHEET);
625 cs = &cuesheet->data.cue_sheet;
627 if(*(cs->media_catalog_number))
628 fprintf(file, "CATALOG %s\n", cs->media_catalog_number);
629 fprintf(file, "FILE %s\n", file_reference);
631 for(track_num = 0; track_num < cs->num_tracks-1; track_num++) {
632 const FLAC__StreamMetadata_CueSheet_Track *track = cs->tracks + track_num;
634 fprintf(file, " TRACK %02u %s\n", (unsigned)track->number, track->type == 0? "AUDIO" : "DATA");
636 if(track->pre_emphasis)
637 fprintf(file, " FLAGS PRE\n");
639 fprintf(file, " ISRC %s\n", track->isrc);
641 for(index_num = 0; index_num < track->num_indices; index_num++) {
642 const FLAC__StreamMetadata_CueSheet_Index *index = track->indices + index_num;
644 fprintf(file, " INDEX %02u ", (unsigned)index->number);
646 const unsigned logical_frame = (unsigned)((track->offset + index->offset) / (44100 / 75));
648 grabbag__cuesheet_frame_to_msf(logical_frame, &m, &s, &f);
649 fprintf(file, "%02u:%02u:%02u\n", m, s, f);
653 fprintf(file, "%I64u\n", track->offset + index->offset);
655 fprintf(file, "%llu\n", (unsigned long long)(track->offset + index->offset));
661 fprintf(file, "REM FLAC__lead-in %I64u\n", cs->lead_in);
662 fprintf(file, "REM FLAC__lead-out %u %I64u\n", (unsigned)cs->tracks[track_num].number, cs->tracks[track_num].offset);
664 fprintf(file, "REM FLAC__lead-in %llu\n", (unsigned long long)cs->lead_in);
665 fprintf(file, "REM FLAC__lead-out %u %llu\n", (unsigned)cs->tracks[track_num].number, (unsigned long long)cs->tracks[track_num].offset);