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 "share/compat.h"
25 #include "FLAC/assert.h"
30 unsigned grabbag__cuesheet_msf_to_frame(unsigned minutes, unsigned seconds, unsigned frames)
32 return ((minutes * 60) + seconds) * 75 + frames;
35 void grabbag__cuesheet_frame_to_msf(unsigned frame, unsigned *minutes, unsigned *seconds, unsigned *frames)
39 *seconds = frame % 60;
44 /* since we only care about values >= 0 or error, returns < 0 for any illegal string, else value */
45 static int local__parse_int_(const char *s)
53 while('\0' != (c = *s++))
54 if(c >= '0' && c <= '9')
55 ret = ret * 10 + (c - '0');
62 /* since we only care about values >= 0 or error, returns < 0 for any illegal string, else value */
63 static FLAC__int64 local__parse_int64_(const char *s)
71 while('\0' != (c = *s++))
72 if(c >= '0' && c <= '9')
73 ret = ret * 10 + (c - '0');
80 /* 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
81 * return sample number or <0 for error
82 * WATCHOUT: if sample rate is not evenly divisible by 75, the resulting sample number will be approximate
84 static FLAC__int64 local__parse_msf_(const char *s, unsigned sample_rate)
86 FLAC__int64 ret, field;
90 if(c >= '0' && c <= '9')
94 while(':' != (c = *s++)) {
95 if(c >= '0' && c <= '9')
96 field = field * 10 + (c - '0');
101 ret = field * 60 * sample_rate;
104 if(c >= '0' && c <= '9')
108 if(':' != (c = *s++)) {
109 if(c >= '0' && c <= '9') {
110 field = field * 10 + (c - '0');
122 ret += field * sample_rate;
125 if(c >= '0' && c <= '9')
129 if('\0' != (c = *s++)) {
130 if(c >= '0' && c <= '9') {
131 field = field * 10 + (c - '0');
144 ret += field * (sample_rate / 75);
149 /* 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
150 * return sample number or <0 for error
151 * WATCHOUT: depending on the sample rate, the resulting sample number may be approximate with fractional seconds
153 static FLAC__int64 local__parse_ms_(const char *s, unsigned sample_rate)
155 FLAC__int64 ret, field;
160 if(c >= '0' && c <= '9')
164 while(':' != (c = *s++)) {
165 if(c >= '0' && c <= '9')
166 field = field * 10 + (c - '0');
171 ret = field * 60 * sample_rate;
173 s++; /* skip the ':' */
174 if(strspn(s, "0123456789.") != strlen(s))
179 if(x < 0.0 || x >= 60.0)
182 ret += (FLAC__int64)(x * sample_rate);
187 static char *local__get_field_(char **s, FLAC__bool allow_quotes)
189 FLAC__bool has_quote = false;
192 FLAC__ASSERT(0 != s);
197 /* skip leading whitespace */
198 while(**s && 0 != strchr(" \t\r\n", **s))
206 if(allow_quotes && (**s == '"')) {
218 *s = strchr(*s, '\"');
219 /* if there is no matching end quote, it's an error */
228 while(**s && 0 == strchr(" \t\r\n", **s))
241 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)
243 #if defined _MSC_VER || defined __MINGW32__
244 #define FLAC__STRCASECMP stricmp
246 #define FLAC__STRCASECMP strcasecmp
248 char buffer[4096], *line, *field;
249 unsigned forced_leadout_track_num = 0;
250 FLAC__uint64 forced_leadout_track_offset = 0;
251 int in_track_num = -1, in_index_num = -1;
252 FLAC__bool disc_has_catalog = false, track_has_flags = false, track_has_isrc = false, has_forced_leadout = false;
253 FLAC__StreamMetadata_CueSheet *cs = &cuesheet->data.cue_sheet;
255 FLAC__ASSERT(!is_cdda || sample_rate == 44100);
256 /* double protection */
257 if(is_cdda && sample_rate != 44100) {
258 *error_message = "CD-DA cuesheet only allowed with 44.1kHz sample rate";
262 cs->lead_in = is_cdda? 2 * 44100 /* The default lead-in size for CD-DA */ : 0;
265 while(0 != fgets(buffer, sizeof(buffer), file)) {
270 size_t linelen = strlen(line);
271 if((linelen == sizeof(buffer)-1) && line[linelen-1] != '\n') {
272 *error_message = "line too long";
277 if(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
278 if(0 == FLAC__STRCASECMP(field, "CATALOG")) {
279 if(disc_has_catalog) {
280 *error_message = "found multiple CATALOG commands";
283 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/true))) {
284 *error_message = "CATALOG is missing catalog number";
287 if(strlen(field) >= sizeof(cs->media_catalog_number)) {
288 *error_message = "CATALOG number is too long";
291 if(is_cdda && (strlen(field) != 13 || strspn(field, "0123456789") != 13)) {
292 *error_message = "CD-DA CATALOG number must be 13 decimal digits";
295 strcpy(cs->media_catalog_number, field);
296 disc_has_catalog = true;
298 else if(0 == FLAC__STRCASECMP(field, "FLAGS")) {
299 if(track_has_flags) {
300 *error_message = "found multiple FLAGS commands";
303 if(in_track_num < 0 || in_index_num >= 0) {
304 *error_message = "FLAGS command must come after TRACK but before INDEX";
307 while(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
308 if(0 == FLAC__STRCASECMP(field, "PRE"))
309 cs->tracks[cs->num_tracks-1].pre_emphasis = 1;
311 track_has_flags = true;
313 else if(0 == FLAC__STRCASECMP(field, "INDEX")) {
315 FLAC__StreamMetadata_CueSheet_Track *track = &cs->tracks[cs->num_tracks-1];
316 if(in_track_num < 0) {
317 *error_message = "found INDEX before any TRACK";
320 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
321 *error_message = "INDEX is missing index number";
324 in_index_num = local__parse_int_(field);
325 if(in_index_num < 0) {
326 *error_message = "INDEX has invalid index number";
329 FLAC__ASSERT(cs->num_tracks > 0);
330 if(track->num_indices == 0) {
331 /* it's the first index point of the track */
332 if(in_index_num > 1) {
333 *error_message = "first INDEX number of a TRACK must be 0 or 1";
338 if(in_index_num != track->indices[track->num_indices-1].number + 1) {
339 *error_message = "INDEX numbers must be sequential";
343 if(is_cdda && in_index_num > 99) {
344 *error_message = "CD-DA INDEX number must be between 0 and 99, inclusive";
347 /*@@@ search for duplicate track number? */
348 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
349 *error_message = "INDEX is missing an offset after the index number";
352 /* first parse as minute:second:frame format */
353 xx = local__parse_msf_(field, sample_rate);
355 /* CD-DA must use only MM:SS:FF format */
357 *error_message = "illegal INDEX offset (not of the form MM:SS:FF)";
360 /* as an extension for non-CD-DA we allow MM:SS.SS or raw sample number */
361 xx = local__parse_ms_(field, sample_rate);
363 xx = local__parse_int64_(field);
365 *error_message = "illegal INDEX offset";
370 else if(sample_rate % 75 && xx) {
371 /* only sample zero is exact */
372 *error_message = "illegal INDEX offset (MM:SS:FF form not allowed if sample rate is not a multiple of 75)";
375 if(is_cdda && cs->num_tracks == 1 && cs->tracks[0].num_indices == 0 && xx != 0) {
376 *error_message = "first INDEX of first TRACK must have an offset of 00:00:00";
379 if(is_cdda && track->num_indices > 0 && (FLAC__uint64)xx <= track->indices[track->num_indices-1].offset) {
380 *error_message = "CD-DA INDEX offsets must increase in time";
383 /* fill in track offset if it's the first index of the track */
384 if(track->num_indices == 0)
385 track->offset = (FLAC__uint64)xx;
386 if(is_cdda && cs->num_tracks > 1) {
387 const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-2];
388 if((FLAC__uint64)xx <= prev->offset + prev->indices[prev->num_indices-1].offset) {
389 *error_message = "CD-DA INDEX offsets must increase in time";
393 if(!FLAC__metadata_object_cuesheet_track_insert_blank_index(cuesheet, cs->num_tracks-1, track->num_indices)) {
394 *error_message = "memory allocation error";
397 track->indices[track->num_indices-1].offset = (FLAC__uint64)xx - track->offset;
398 track->indices[track->num_indices-1].number = in_index_num;
400 else if(0 == FLAC__STRCASECMP(field, "ISRC")) {
403 *error_message = "found multiple ISRC commands";
406 if(in_track_num < 0 || in_index_num >= 0) {
407 *error_message = "ISRC command must come after TRACK but before INDEX";
410 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
411 *error_message = "ISRC is missing ISRC number";
414 /* strip out dashes */
415 for(l = r = field; *r; r++) {
420 if(strlen(field) != 12 || strspn(field, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") < 5 || strspn(field+5, "1234567890") != 7) {
421 *error_message = "invalid ISRC number";
424 strcpy(cs->tracks[cs->num_tracks-1].isrc, field);
425 track_has_isrc = true;
427 else if(0 == FLAC__STRCASECMP(field, "TRACK")) {
428 if(cs->num_tracks > 0) {
429 const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
431 prev->num_indices == 0 ||
435 (prev->num_indices == 1 && prev->indices[0].number != 1) ||
436 (prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
440 *error_message = is_cdda?
441 "previous TRACK must specify at least one INDEX 01" :
442 "previous TRACK must specify at least one INDEX";
446 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
447 *error_message = "TRACK is missing track number";
450 in_track_num = local__parse_int_(field);
451 if(in_track_num < 0) {
452 *error_message = "TRACK has invalid track number";
455 if(in_track_num == 0) {
456 *error_message = "TRACK number must be greater than 0";
460 if(in_track_num > 99) {
461 *error_message = "CD-DA TRACK number must be between 1 and 99, inclusive";
466 if(in_track_num == 255) {
467 *error_message = "TRACK number 255 is reserved for the lead-out";
470 else if(in_track_num > 255) {
471 *error_message = "TRACK number must be between 1 and 254, inclusive";
475 if(is_cdda && cs->num_tracks > 0 && in_track_num != cs->tracks[cs->num_tracks-1].number + 1) {
476 *error_message = "CD-DA TRACK numbers must be sequential";
479 /*@@@ search for duplicate track number? */
480 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
481 *error_message = "TRACK is missing a track type after the track number";
484 if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
485 *error_message = "memory allocation error";
488 cs->tracks[cs->num_tracks-1].number = in_track_num;
489 cs->tracks[cs->num_tracks-1].type = (0 == FLAC__STRCASECMP(field, "AUDIO"))? 0 : 1; /*@@@ should we be more strict with the value here? */
491 track_has_flags = false;
492 track_has_isrc = false;
494 else if(0 == FLAC__STRCASECMP(field, "REM")) {
495 if(0 != (field = local__get_field_(&line, /*allow_quotes=*/false))) {
496 if(0 == strcmp(field, "FLAC__lead-in")) {
498 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
499 *error_message = "FLAC__lead-in is missing offset";
502 xx = local__parse_int64_(field);
504 *error_message = "illegal FLAC__lead-in offset";
507 if(is_cdda && xx % 588 != 0) {
508 *error_message = "illegal CD-DA FLAC__lead-in offset, must be even multiple of 588 samples";
511 cs->lead_in = (FLAC__uint64)xx;
513 else if(0 == strcmp(field, "FLAC__lead-out")) {
516 if(has_forced_leadout) {
517 *error_message = "multiple FLAC__lead-out commands";
520 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
521 *error_message = "FLAC__lead-out is missing track number";
524 track_num = local__parse_int_(field);
526 *error_message = "illegal FLAC__lead-out track number";
529 forced_leadout_track_num = (unsigned)track_num;
530 /*@@@ search for duplicate track number? */
531 if(0 == (field = local__get_field_(&line, /*allow_quotes=*/false))) {
532 *error_message = "FLAC__lead-out is missing offset";
535 offset = local__parse_int64_(field);
537 *error_message = "illegal FLAC__lead-out offset";
540 forced_leadout_track_offset = (FLAC__uint64)offset;
541 if(forced_leadout_track_offset != lead_out_offset) {
542 *error_message = "FLAC__lead-out offset does not match end-of-stream offset";
545 has_forced_leadout = true;
552 if(cs->num_tracks == 0) {
553 *error_message = "there must be at least one TRACK command";
557 const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
559 prev->num_indices == 0 ||
563 (prev->num_indices == 1 && prev->indices[0].number != 1) ||
564 (prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
568 *error_message = is_cdda?
569 "previous TRACK must specify at least one INDEX 01" :
570 "previous TRACK must specify at least one INDEX";
575 if(!has_forced_leadout) {
576 forced_leadout_track_num = is_cdda? 170 : 255;
577 forced_leadout_track_offset = lead_out_offset;
579 if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
580 *error_message = "memory allocation error";
583 cs->tracks[cs->num_tracks-1].number = forced_leadout_track_num;
584 cs->tracks[cs->num_tracks-1].offset = forced_leadout_track_offset;
587 *error_message = "read error";
591 #undef FLAC__STRCASECMP
594 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)
596 FLAC__StreamMetadata *cuesheet;
598 FLAC__ASSERT(0 != file);
599 FLAC__ASSERT(0 != error_message);
600 FLAC__ASSERT(0 != last_line_read);
603 cuesheet = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET);
606 *error_message = "memory allocation error";
610 if(!local__cuesheet_parse_(file, error_message, last_line_read, cuesheet, sample_rate, is_cdda, lead_out_offset)) {
611 FLAC__metadata_object_delete(cuesheet);
618 void grabbag__cuesheet_emit(FILE *file, const FLAC__StreamMetadata *cuesheet, const char *file_reference)
620 const FLAC__StreamMetadata_CueSheet *cs;
621 unsigned track_num, index_num;
623 FLAC__ASSERT(0 != file);
624 FLAC__ASSERT(0 != cuesheet);
625 FLAC__ASSERT(cuesheet->type == FLAC__METADATA_TYPE_CUESHEET);
627 cs = &cuesheet->data.cue_sheet;
629 if(*(cs->media_catalog_number))
630 fprintf(file, "CATALOG %s\n", cs->media_catalog_number);
631 fprintf(file, "FILE %s\n", file_reference);
633 for(track_num = 0; track_num < cs->num_tracks-1; track_num++) {
634 const FLAC__StreamMetadata_CueSheet_Track *track = cs->tracks + track_num;
636 fprintf(file, " TRACK %02u %s\n", (unsigned)track->number, track->type == 0? "AUDIO" : "DATA");
638 if(track->pre_emphasis)
639 fprintf(file, " FLAGS PRE\n");
641 fprintf(file, " ISRC %s\n", track->isrc);
643 for(index_num = 0; index_num < track->num_indices; index_num++) {
644 const FLAC__StreamMetadata_CueSheet_Index *index = track->indices + index_num;
646 fprintf(file, " INDEX %02u ", (unsigned)index->number);
648 const unsigned logical_frame = (unsigned)((track->offset + index->offset) / (44100 / 75));
650 grabbag__cuesheet_frame_to_msf(logical_frame, &m, &s, &f);
651 fprintf(file, "%02u:%02u:%02u\n", m, s, f);
654 fprintf(file, "%" PRIu64 "\n", (track->offset + index->offset));
658 fprintf(file, "REM FLAC__lead-in %" PRIu64 "\n", cs->lead_in);
659 fprintf(file, "REM FLAC__lead-out %u %" PRIu64 "\n", (unsigned)cs->tracks[track_num].number, cs->tracks[track_num].offset);