1 /* grabbag - Convenience lib for various routines common to several tools
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2016 Xiph.Org Foundation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "FLAC/assert.h"
28 #include "share/compat.h"
29 #include "share/grabbag.h"
30 #include "share/safe_str.h"
32 unsigned grabbag__cuesheet_msf_to_frame(unsigned minutes, unsigned seconds, unsigned frames)
34 return ((minutes * 60) + seconds) * 75 + frames;
37 void grabbag__cuesheet_frame_to_msf(unsigned frame, unsigned *minutes, unsigned *seconds, unsigned *frames)
41 *seconds = frame % 60;
46 /* since we only care about values >= 0 or error, returns < 0 for any illegal string, else value */
47 static int local__parse_int_(const char *s)
55 while('\0' != (c = *s++))
56 if(c >= '0' && c <= '9')
57 ret = ret * 10 + (c - '0');
64 /* since we only care about values >= 0 or error, returns < 0 for any illegal string, else value */
65 static FLAC__int64 local__parse_int64_(const char *s)
73 while('\0' != (c = *s++))
74 if(c >= '0' && c <= '9')
75 ret = ret * 10 + (c - '0');
82 /* 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
83 * return sample number or <0 for error
84 * WATCHOUT: if sample rate is not evenly divisible by 75, the resulting sample number will be approximate
86 static FLAC__int64 local__parse_msf_(const char *s, unsigned sample_rate)
88 FLAC__int64 ret, field;
92 if(c >= '0' && c <= '9')
96 while(':' != (c = *s++)) {
97 if(c >= '0' && c <= '9')
98 field = field * 10 + (c - '0');
103 ret = field * 60 * sample_rate;
106 if(c >= '0' && c <= '9')
110 if(':' != (c = *s++)) {
111 if(c >= '0' && c <= '9') {
112 field = field * 10 + (c - '0');
124 ret += field * sample_rate;
127 if(c >= '0' && c <= '9')
131 if('\0' != (c = *s++)) {
132 if(c >= '0' && c <= '9') {
133 field = field * 10 + (c - '0');
146 ret += field * (sample_rate / 75);
151 /* 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
152 * return sample number or <0 for error
153 * WATCHOUT: depending on the sample rate, the resulting sample number may be approximate with fractional seconds
155 static FLAC__int64 local__parse_ms_(const char *s, unsigned sample_rate)
157 FLAC__int64 ret, field;
162 if(c >= '0' && c <= '9')
166 while(':' != (c = *s++)) {
167 if(c >= '0' && c <= '9')
168 field = field * 10 + (c - '0');
173 ret = field * 60 * sample_rate;
175 s++; /* skip the ':' */
176 if(strspn(s, "0123456789.") != strlen(s))
181 if(x < 0.0 || x >= 60.0)
184 ret += (FLAC__int64)(x * sample_rate);
189 static char *local__get_field_(char **s, FLAC__bool allow_quotes)
191 FLAC__bool has_quote = false;
194 FLAC__ASSERT(0 != s);
199 /* skip leading whitespace */
200 while(**s && 0 != strchr(" \t\r\n", **s))
208 if(allow_quotes && (**s == '"')) {
220 *s = strchr(*s, '\"');
221 /* if there is no matching end quote, it's an error */
230 while(**s && 0 == strchr(" \t\r\n", **s))
243 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)
245 char buffer[4096], *line, *field;
246 unsigned forced_leadout_track_num = 0;
247 FLAC__uint64 forced_leadout_track_offset = 0;
248 int in_track_num = -1, in_index_num = -1;
249 FLAC__bool disc_has_catalog = false, track_has_flags = false, track_has_isrc = false, has_forced_leadout = false;
250 FLAC__StreamMetadata_CueSheet *cs = &cuesheet->data.cue_sheet;
252 FLAC__ASSERT(!is_cdda || sample_rate == 44100);
253 /* double protection */
254 if(is_cdda && sample_rate != 44100) {
255 *error_message = "CD-DA cuesheet only allowed with 44.1kHz sample rate";
259 cs->lead_in = is_cdda? 2 * 44100 /* The default lead-in size for CD-DA */ : 0;
262 while(0 != fgets(buffer, sizeof(buffer), file)) {
267 size_t linelen = strlen(line);
268 if((linelen == sizeof(buffer)-1) && line[linelen-1] != '\n') {
269 *error_message = "line too long";
274 if(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
275 if(0 == FLAC__STRCASECMP(field, "CATALOG")) {
276 if(disc_has_catalog) {
277 *error_message = "found multiple CATALOG commands";
280 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/true))) {
281 *error_message = "CATALOG is missing catalog number";
284 if(strlen(field) >= sizeof(cs->media_catalog_number)) {
285 *error_message = "CATALOG number is too long";
288 if(is_cdda && (strlen(field) != 13 || strspn(field, "0123456789") != 13)) {
289 *error_message = "CD-DA CATALOG number must be 13 decimal digits";
292 safe_strncpy(cs->media_catalog_number, field, sizeof(cs->media_catalog_number));
293 disc_has_catalog = true;
295 else if(0 == FLAC__STRCASECMP(field, "FLAGS")) {
296 if(track_has_flags) {
297 *error_message = "found multiple FLAGS commands";
300 if(in_track_num < 0 || in_index_num >= 0) {
301 *error_message = "FLAGS command must come after TRACK but before INDEX";
304 while(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
305 if(0 == FLAC__STRCASECMP(field, "PRE"))
306 cs->tracks[cs->num_tracks-1].pre_emphasis = 1;
308 track_has_flags = true;
310 else if(0 == FLAC__STRCASECMP(field, "INDEX")) {
312 FLAC__StreamMetadata_CueSheet_Track *track = &cs->tracks[cs->num_tracks-1];
313 if(in_track_num < 0) {
314 *error_message = "found INDEX before any TRACK";
317 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
318 *error_message = "INDEX is missing index number";
321 in_index_num = local__parse_int_(field);
322 if(in_index_num < 0) {
323 *error_message = "INDEX has invalid index number";
326 FLAC__ASSERT(cs->num_tracks > 0);
327 if(track->num_indices == 0) {
328 /* it's the first index point of the track */
329 if(in_index_num > 1) {
330 *error_message = "first INDEX number of a TRACK must be 0 or 1";
335 if(in_index_num != track->indices[track->num_indices-1].number + 1) {
336 *error_message = "INDEX numbers must be sequential";
340 if(is_cdda && in_index_num > 99) {
341 *error_message = "CD-DA INDEX number must be between 0 and 99, inclusive";
344 /*@@@ search for duplicate track number? */
345 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
346 *error_message = "INDEX is missing an offset after the index number";
349 /* first parse as minute:second:frame format */
350 xx = local__parse_msf_(field, sample_rate);
352 /* CD-DA must use only MM:SS:FF format */
354 *error_message = "illegal INDEX offset (not of the form MM:SS:FF)";
357 /* as an extension for non-CD-DA we allow MM:SS.SS or raw sample number */
358 xx = local__parse_ms_(field, sample_rate);
360 xx = local__parse_int64_(field);
362 *error_message = "illegal INDEX offset";
367 else if(sample_rate % 75 && xx) {
368 /* only sample zero is exact */
369 *error_message = "illegal INDEX offset (MM:SS:FF form not allowed if sample rate is not a multiple of 75)";
372 if(is_cdda && cs->num_tracks == 1 && cs->tracks[0].num_indices == 0 && xx != 0) {
373 *error_message = "first INDEX of first TRACK must have an offset of 00:00:00";
376 if(is_cdda && track->num_indices > 0 && (FLAC__uint64)xx <= track->indices[track->num_indices-1].offset) {
377 *error_message = "CD-DA INDEX offsets must increase in time";
380 /* fill in track offset if it's the first index of the track */
381 if(track->num_indices == 0)
382 track->offset = (FLAC__uint64)xx;
383 if(is_cdda && cs->num_tracks > 1) {
384 const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-2];
385 if((FLAC__uint64)xx <= prev->offset + prev->indices[prev->num_indices-1].offset) {
386 *error_message = "CD-DA INDEX offsets must increase in time";
390 if(!FLAC__metadata_object_cuesheet_track_insert_blank_index(cuesheet, cs->num_tracks-1, track->num_indices)) {
391 *error_message = "memory allocation error";
394 track->indices[track->num_indices-1].offset = (FLAC__uint64)xx - track->offset;
395 track->indices[track->num_indices-1].number = in_index_num;
397 else if(0 == FLAC__STRCASECMP(field, "ISRC")) {
400 *error_message = "found multiple ISRC commands";
403 if(in_track_num < 0 || in_index_num >= 0) {
404 *error_message = "ISRC command must come after TRACK but before INDEX";
407 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/true))) {
408 *error_message = "ISRC is missing ISRC number";
411 /* strip out dashes */
412 for(l = r = field; *r; r++) {
417 if(strlen(field) != 12 || strspn(field, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") < 5 || strspn(field+5, "1234567890") != 7) {
418 *error_message = "invalid ISRC number";
421 safe_strncpy(cs->tracks[cs->num_tracks-1].isrc, field, sizeof(cs->tracks[cs->num_tracks-1].isrc));
422 track_has_isrc = true;
424 else if(0 == FLAC__STRCASECMP(field, "TRACK")) {
425 if(cs->num_tracks > 0) {
426 const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
428 prev->num_indices == 0 ||
432 (prev->num_indices == 1 && prev->indices[0].number != 1) ||
433 (prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
437 *error_message = is_cdda?
438 "previous TRACK must specify at least one INDEX 01" :
439 "previous TRACK must specify at least one INDEX";
443 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
444 *error_message = "TRACK is missing track number";
447 in_track_num = local__parse_int_(field);
448 if(in_track_num < 0) {
449 *error_message = "TRACK has invalid track number";
452 if(in_track_num == 0) {
453 *error_message = "TRACK number must be greater than 0";
457 if(in_track_num > 99) {
458 *error_message = "CD-DA TRACK number must be between 1 and 99, inclusive";
463 if(in_track_num == 255) {
464 *error_message = "TRACK number 255 is reserved for the lead-out";
467 else if(in_track_num > 255) {
468 *error_message = "TRACK number must be between 1 and 254, inclusive";
472 if(is_cdda && cs->num_tracks > 0 && in_track_num != cs->tracks[cs->num_tracks-1].number + 1) {
473 *error_message = "CD-DA TRACK numbers must be sequential";
476 /*@@@ search for duplicate track number? */
477 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
478 *error_message = "TRACK is missing a track type after the track number";
481 if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
482 *error_message = "memory allocation error";
485 cs->tracks[cs->num_tracks-1].number = in_track_num;
486 cs->tracks[cs->num_tracks-1].type = (0 == FLAC__STRCASECMP(field, "AUDIO"))? 0 : 1; /*@@@ should we be more strict with the value here? */
488 track_has_flags = false;
489 track_has_isrc = false;
491 else if(0 == FLAC__STRCASECMP(field, "REM")) {
492 if(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
493 if(0 == strcmp(field, "FLAC__lead-in")) {
495 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
496 *error_message = "FLAC__lead-in is missing offset";
499 xx = local__parse_int64_(field);
501 *error_message = "illegal FLAC__lead-in offset";
504 if(is_cdda && xx % 588 != 0) {
505 *error_message = "illegal CD-DA FLAC__lead-in offset, must be even multiple of 588 samples";
508 cs->lead_in = (FLAC__uint64)xx;
510 else if(0 == strcmp(field, "FLAC__lead-out")) {
513 if(has_forced_leadout) {
514 *error_message = "multiple FLAC__lead-out commands";
517 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
518 *error_message = "FLAC__lead-out is missing track number";
521 track_num = local__parse_int_(field);
523 *error_message = "illegal FLAC__lead-out track number";
526 forced_leadout_track_num = (unsigned)track_num;
527 /*@@@ search for duplicate track number? */
528 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
529 *error_message = "FLAC__lead-out is missing offset";
532 offset = local__parse_int64_(field);
534 *error_message = "illegal FLAC__lead-out offset";
537 forced_leadout_track_offset = (FLAC__uint64)offset;
538 if(forced_leadout_track_offset != lead_out_offset) {
539 *error_message = "FLAC__lead-out offset does not match end-of-stream offset";
542 has_forced_leadout = true;
549 if(cs->num_tracks == 0) {
550 *error_message = "there must be at least one TRACK command";
554 const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
556 prev->num_indices == 0 ||
560 (prev->num_indices == 1 && prev->indices[0].number != 1) ||
561 (prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
565 *error_message = is_cdda?
566 "previous TRACK must specify at least one INDEX 01" :
567 "previous TRACK must specify at least one INDEX";
572 if(!has_forced_leadout) {
573 forced_leadout_track_num = is_cdda? 170 : 255;
574 forced_leadout_track_offset = lead_out_offset;
576 if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
577 *error_message = "memory allocation error";
580 cs->tracks[cs->num_tracks-1].number = forced_leadout_track_num;
581 cs->tracks[cs->num_tracks-1].offset = forced_leadout_track_offset;
584 *error_message = "read error";
590 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)
592 FLAC__StreamMetadata *cuesheet;
594 FLAC__ASSERT(0 != file);
595 FLAC__ASSERT(0 != error_message);
596 FLAC__ASSERT(0 != last_line_read);
599 cuesheet = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET);
602 *error_message = "memory allocation error";
606 if(!local__cuesheet_parse_(file, error_message, last_line_read, cuesheet, sample_rate, is_cdda, lead_out_offset)) {
607 FLAC__metadata_object_delete(cuesheet);
614 void grabbag__cuesheet_emit(FILE *file, const FLAC__StreamMetadata *cuesheet, const char *file_reference)
616 const FLAC__StreamMetadata_CueSheet *cs;
617 unsigned track_num, index_num;
619 FLAC__ASSERT(0 != file);
620 FLAC__ASSERT(0 != cuesheet);
621 FLAC__ASSERT(cuesheet->type == FLAC__METADATA_TYPE_CUESHEET);
623 cs = &cuesheet->data.cue_sheet;
625 if(*(cs->media_catalog_number))
626 fprintf(file, "CATALOG %s\n", cs->media_catalog_number);
627 fprintf(file, "FILE %s\n", file_reference);
629 for(track_num = 0; track_num < cs->num_tracks-1; track_num++) {
630 const FLAC__StreamMetadata_CueSheet_Track *track = cs->tracks + track_num;
632 fprintf(file, " TRACK %02u %s\n", (unsigned)track->number, track->type == 0? "AUDIO" : "DATA");
634 if(track->pre_emphasis)
635 fprintf(file, " FLAGS PRE\n");
637 fprintf(file, " ISRC %s\n", track->isrc);
639 for(index_num = 0; index_num < track->num_indices; index_num++) {
640 const FLAC__StreamMetadata_CueSheet_Index *indx = track->indices + index_num;
642 fprintf(file, " INDEX %02u ", (unsigned)indx->number);
644 const unsigned logical_frame = (unsigned)((track->offset + indx->offset) / (44100 / 75));
646 grabbag__cuesheet_frame_to_msf(logical_frame, &m, &s, &f);
647 fprintf(file, "%02u:%02u:%02u\n", m, s, f);
650 fprintf(file, "%" PRIu64 "\n", (track->offset + indx->offset));
654 fprintf(file, "REM FLAC__lead-in %" PRIu64 "\n", cs->lead_in);
655 fprintf(file, "REM FLAC__lead-out %u %" PRIu64 "\n", (unsigned)cs->tracks[track_num].number, cs->tracks[track_num].offset);