54dd853a2d09e294e2e18abf0b565865e625ff70
[flac.git] / src / share / grabbag / cuesheet.c
1 /* grabbag - Convenience lib for various routines common to several tools
2  * Copyright (C) 2002  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "share/grabbag.h"
20 #include "FLAC/assert.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 unsigned grabbag__cuesheet_msf_to_frame(unsigned minutes, unsigned seconds, unsigned frames)
26 {
27         return ((minutes * 60) + seconds) * 75 + frames;
28 }
29
30 void grabbag__cuesheet_frame_to_msf(unsigned frame, unsigned *minutes, unsigned *seconds, unsigned *frames)
31 {
32         *frames = frame % 75;
33         frame /= 75;
34         *seconds = frame % 60;
35         frame /= 60;
36         *minutes = frame;
37 }
38
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)
41 {
42         int ret = 0;
43         char c;
44
45         if(*s == '\0')
46                 return -1;
47
48         while('\0' != (c = *s++))
49                 if(c >= '0' && c <= '9')
50                         ret = ret * 10 + (c - '0');
51                 else
52                         return -1;
53
54         return ret;
55 }
56
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)
59 {
60         FLAC__int64 ret = 0;
61         char c;
62
63         if(*s == '\0')
64                 return -1;
65
66         while('\0' != (c = *s++))
67                 if(c >= '0' && c <= '9')
68                         ret = ret * 10 + (c - '0');
69                 else
70                         return -1;
71
72         return ret;
73 }
74
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
77  */
78 static FLAC__int64 local__parse_msf_(const char *s)
79 {
80         FLAC__int64 ret, field;
81         char c;
82
83         c = *s++;
84         if(c >= '0' && c <= '9')
85                 field = (c - '0');
86         else
87                 return -1;
88         while(':' != (c = *s++)) {
89                 if(c >= '0' && c <= '9')
90                         field = field * 10 + (c - '0');
91                 else
92                         return -1;
93         }
94
95         ret = field * 60 * 44100;
96
97         c = *s++;
98         if(c >= '0' && c <= '9')
99                 field = (c - '0');
100         else
101                 return -1;
102         if(':' != (c = *s++)) {
103                 if(c >= '0' && c <= '9') {
104                         field = field * 10 + (c - '0');
105                         c = *s++;
106                         if(c != ':')
107                                 return -1;
108                 }
109                 else
110                         return -1;
111         }
112
113         if(field >= 60)
114                 return -1;
115
116         ret += field * 44100;
117
118         c = *s++;
119         if(c >= '0' && c <= '9')
120                 field = (c - '0');
121         else
122                 return -1;
123         if('\0' != (c = *s++)) {
124                 if(c >= '0' && c <= '9') {
125                         field = field * 10 + (c - '0');
126                         c = *s++;
127                 }
128                 else
129                         return -1;
130         }
131
132         if(c != '\0')
133                 return -1;
134
135         if(field >= 75)
136                 return -1;
137
138         ret += field * (44100 / 75);
139
140         return ret;
141 }
142
143 static char *local__get_field_(char **s)
144 {
145         char *p;
146
147         FLAC__ASSERT(0 != s);
148
149         if(0 == *s)
150                 return 0;
151
152         /* skip leading whitespace */
153         while(**s && 0 != strchr(" \t\r\n", **s))
154                 (*s)++;
155
156         if(**s == 0)
157                 *s = 0;
158
159         p = *s;
160
161         if(p) {
162                 while(**s && 0 == strchr(" \t\r\n", **s))
163                         (*s)++;
164                 if(**s) {
165                         **s = '\0';
166                         (*s)++;
167                 }
168                 else
169                         *s = 0;
170         }
171
172         return p;
173 }
174
175 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)
176 {
177 #if defined _MSC_VER || defined __MINGW32__
178 #define FLAC__STRCASECMP stricmp
179 #else
180 #define FLAC__STRCASECMP strcasecmp
181 #endif
182         char buffer[4096], *line, *field;
183         unsigned linelen, forced_leadout_track_num = 0;
184         FLAC__uint64 forced_leadout_track_offset = 0;
185         int in_track_num = -1, in_index_num = -1;
186         FLAC__bool disc_has_catalog = false, track_has_flags, track_has_isrc, has_forced_leadout = false;
187         FLAC__StreamMetadata_CueSheet *cs = &cuesheet->data.cue_sheet;
188
189         cs->lead_in = is_cdda? 2 * 44100 /* The default lead-in size for CD-DA */ : 0;
190
191         while(0 != fgets(buffer, sizeof(buffer), file)) {
192                 (*last_line_read)++;
193                 line = buffer;
194
195                 linelen = strlen(line);
196                 if(line[linelen-1] != '\n') {
197                         *error_message = "line too long";
198                         return false;
199                 }
200
201                 if(0 != (field = local__get_field_(&line))) {
202                         if(0 == FLAC__STRCASECMP(field, "CATALOG")) {
203                                 if(disc_has_catalog) {
204                                         *error_message = "found multiple CATALOG commands";
205                                         return false;
206                                 }
207                                 if(0 == (field = local__get_field_(&line))) {
208                                         *error_message = "CATALOG is missing catalog number";
209                                         return false;
210                                 }
211                                 if(strlen(field) >= sizeof(cs->media_catalog_number)) {
212                                         *error_message = "CATALOG number is to long";
213                                         return false;
214                                 }
215                                 if(is_cdda && (strlen(field) != 13 || strspn(field, "0123456789") != 13)) {
216                                         *error_message = "CD-DA CATALOG number must be 13 decimal digits";
217                                         return false;
218                                 }
219                                 strcpy(cs->media_catalog_number, field);
220                                 disc_has_catalog = true;
221                         }
222                         else if(0 == FLAC__STRCASECMP(field, "FLAGS")) {
223                                 if(track_has_flags) {
224                                         *error_message = "found multiple FLAGS commands";
225                                         return false;
226                                 }
227                                 if(in_track_num < 0 || in_index_num >= 0) {
228                                         *error_message = "FLAGS command must come after TRACK but before INDEX";
229                                         return false;
230                                 }
231                                 while(0 != (field = local__get_field_(&line))) {
232                                         if(0 == FLAC__STRCASECMP(field, "PRE"))
233                                                 cs->tracks[cs->num_tracks-1].pre_emphasis = 1;
234                                 }
235                                 track_has_flags = true;
236                         }
237                         else if(0 == FLAC__STRCASECMP(field, "INDEX")) {
238                                 FLAC__int64 xx;
239                                 FLAC__StreamMetadata_CueSheet_Track *track = &cs->tracks[cs->num_tracks-1];
240                                 if(in_track_num < 0) {
241                                         *error_message = "found INDEX before any TRACK";
242                                         return false;
243                                 }
244                                 if(0 == (field = local__get_field_(&line))) {
245                                         *error_message = "INDEX is missing index number";
246                                         return false;
247                                 }
248                                 in_index_num = local__parse_int_(field);
249                                 if(in_index_num < 0) {
250                                         *error_message = "INDEX has invalid index number";
251                                         return false;
252                                 }
253                                 FLAC__ASSERT(cs->num_tracks > 0);
254                                 if(track->num_indices == 0) {
255                                         /* it's the first index point of the track */
256                                         if(in_index_num > 1) {
257                                                 *error_message = "first INDEX number of a TRACK must be 0 or 1";
258                                                 return false;
259                                         }
260                                 }
261                                 else {
262                                         if(in_index_num != track->indices[track->num_indices-1].number + 1) {
263                                                 *error_message = "INDEX numbers must be sequential";
264                                                 return false;
265                                         }
266                                 }
267                                 if(is_cdda && in_index_num > 99) {
268                                         *error_message = "CD-DA INDEX number must be between 0 and 99, inclusive";
269                                         return false;
270                                 }
271                                 /*@@@@ search for duplicate track number? */
272                                 if(0 == (field = local__get_field_(&line))) {
273                                         *error_message = "INDEX is missing an offset after the index number";
274                                         return false;
275                                 }
276                                 xx = local__parse_msf_(field);
277                                 if(xx < 0) {
278                                         if(is_cdda) {
279                                                 *error_message = "illegal INDEX offset (not of the form MM:SS:FF)";
280                                                 return false;
281                                         }
282                                         xx = local__parse_int64_(field);
283                                         if(xx < 0) {
284                                                 *error_message = "illegal INDEX offset";
285                                                 return false;
286                                         }
287                                 }
288                                 if(is_cdda && cs->num_tracks == 1 && cs->tracks[0].num_indices == 0 && xx != 0) {
289                                         *error_message = "first INDEX of first TRACK must have an offset of 00:00:00";
290                                         return false;
291                                 }
292                                 if(is_cdda && track->num_indices > 0 && (FLAC__uint64)xx <= track->indices[track->num_indices-1].offset) {
293                                         *error_message = "CD-DA INDEX offsets must increase in time";
294                                         return false;
295                                 }
296                                 /* fill in track offset if it's the first index of the track */
297                                 if(track->num_indices == 0)
298                                         track->offset = (FLAC__uint64)xx;
299                                 if(is_cdda && cs->num_tracks > 1) {
300                                         const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-2];
301                                         if((FLAC__uint64)xx <= prev->offset + prev->indices[prev->num_indices-1].offset) {
302                                                 *error_message = "CD-DA INDEX offsets must increase in time";
303                                                 return false;
304                                         }
305                                 }
306                                 if(!FLAC__metadata_object_cuesheet_track_insert_blank_index(cuesheet, cs->num_tracks-1, track->num_indices)) {
307                                         *error_message = "memory allocation error";
308                                         return false;
309                                 }
310                                 track->indices[track->num_indices-1].offset = (FLAC__uint64)xx - track->offset;
311                                 track->indices[track->num_indices-1].number = in_index_num;
312                         }
313                         else if(0 == FLAC__STRCASECMP(field, "ISRC")) {
314                                 if(track_has_isrc) {
315                                         *error_message = "found multiple ISRC commands";
316                                         return false;
317                                 }
318                                 if(in_track_num < 0 || in_index_num >= 0) {
319                                         *error_message = "FLAGS command must come after TRACK but before INDEX";
320                                         return false;
321                                 }
322                                 if(strlen(field) != 12 || strspn(field, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") < 5 || strspn(field+5, "1234567890") != 7) {
323                                         *error_message = "invalid ISRC number";
324                                         return false;
325                                 }
326                                 strcpy(cs->tracks[cs->num_tracks-1].isrc, field);
327                                 track_has_isrc = true;
328                         }
329                         else if(0 == FLAC__STRCASECMP(field, "TRACK")) {
330                                 if(cs->num_tracks > 0) {
331                                         const FLAC__StreamMetadata_CueSheet_Track *prev = &cs->tracks[cs->num_tracks-1];
332                                         if(
333                                                 prev->num_indices == 0 ||
334                                                 (
335                                                         is_cdda &&
336                                                         (
337                                                                 (prev->num_indices == 1 && prev->indices[0].number != 1) ||
338                                                                 (prev->num_indices == 2 && prev->indices[0].number != 1 && prev->indices[1].number != 1)
339                                                         )
340                                                 )
341                                         ) {
342                                                 *error_message = is_cdda?
343                                                         "previous TRACK must specify at least one INDEX 01" :
344                                                         "previous TRACK must specify at least one INDEX";
345                                                 return false;
346                                         }
347                                 }
348                                 if(0 == (field = local__get_field_(&line))) {
349                                         *error_message = "TRACK is missing track number";
350                                         return false;
351                                 }
352                                 in_track_num = local__parse_int_(field);
353                                 if(in_track_num < 0) {
354                                         *error_message = "TRACK has invalid track number";
355                                         return false;
356                                 }
357                                 if(in_track_num == 0) {
358                                         *error_message = "TRACK number must be greater than 0";
359                                         return false;
360                                 }
361                                 if(is_cdda && in_track_num > 99) {
362                                         *error_message = "CD-DA TRACK number must be between 1 and 99, inclusive";
363                                         return false;
364                                 }
365                                 if(is_cdda && cs->num_tracks > 0 && in_track_num != cs->tracks[cs->num_tracks-1].number + 1) {
366                                         *error_message = "CD-DA TRACK numbers must be sequential";
367                                         return false;
368                                 }
369                                 /*@@@@ search for duplicate track number? */
370                                 if(0 == (field = local__get_field_(&line))) {
371                                         *error_message = "TRACK is missing a track type after the track number";
372                                         return false;
373                                 }
374                                 if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
375                                         *error_message = "memory allocation error";
376                                         return false;
377                                 }
378                                 cs->tracks[cs->num_tracks-1].number = in_track_num;
379                                 cs->tracks[cs->num_tracks-1].type = (0 == FLAC__STRCASECMP(field, "AUDIO"))? 0 : 1; /*@@@@ should we be more strict with the value here? */
380                                 in_index_num = -1;
381                                 track_has_flags = false;
382                                 track_has_isrc = false;
383                         }
384                         else if(0 == FLAC__STRCASECMP(field, "REM")) {
385                                 if(0 != (field = local__get_field_(&line))) {
386                                         if(0 == strcmp(field, "FLAC__lead-in")) {
387                                                 FLAC__int64 xx;
388                                                 if(0 == (field = local__get_field_(&line))) {
389                                                         *error_message = "FLAC__lead-in is missing offset";
390                                                         return false;
391                                                 }
392                                                 xx = local__parse_int64_(field);
393                                                 if(xx < 0) {
394                                                         *error_message = "illegal FLAC__lead-in offset";
395                                                         return false;
396                                                 }
397                                                 if(is_cdda && xx % 588 != 0) {
398                                                         *error_message = "illegal CD-DA FLAC__lead-in offset, must be even multiple of 588 samples";
399                                                         return false;
400                                                 }
401                                                 cs->lead_in = (FLAC__uint64)xx;
402                                         }
403                                         else if(0 == strcmp(field, "FLAC__lead-out")) {
404                                                 int track_num;
405                                                 FLAC__int64 offset;
406                                                 if(has_forced_leadout) {
407                                                         *error_message = "multiple FLAC__lead-out commands";
408                                                         return false;
409                                                 }
410                                                 if(0 == (field = local__get_field_(&line))) {
411                                                         *error_message = "FLAC__lead-out is missing track number";
412                                                         return false;
413                                                 }
414                                                 track_num = local__parse_int_(field);
415                                                 if(track_num < 0) {
416                                                         *error_message = "illegal FLAC__lead-out track number";
417                                                         return false;
418                                                 }
419                                                 forced_leadout_track_num = (unsigned)track_num;
420                                                 /*@@@@ search for duplicate track number? */
421                                                 if(0 == (field = local__get_field_(&line))) {
422                                                         *error_message = "FLAC__lead-out is missing offset";
423                                                         return false;
424                                                 }
425                                                 offset = local__parse_int64_(field);
426                                                 if(offset < 0) {
427                                                         *error_message = "illegal FLAC__lead-out offset";
428                                                         return false;
429                                                 }
430                                                 forced_leadout_track_offset = (FLAC__uint64)offset;
431                                                 if(forced_leadout_track_offset != lead_out_offset) {
432                                                         *error_message = "FLAC__lead-out offset does not match end-of-stream offset";
433                                                         return false;
434                                                 }
435                                                 has_forced_leadout = true;
436                                         }
437                                 }
438                         }
439                 }
440         }
441
442         if(cs->num_tracks == 0) {
443                 *error_message = "there must be at least one TRACK command";
444                 return false;
445         }
446
447         if(!has_forced_leadout) {
448                 forced_leadout_track_num = is_cdda? 170 : cs->num_tracks;
449                 forced_leadout_track_offset = lead_out_offset;
450         }
451         if(!FLAC__metadata_object_cuesheet_insert_blank_track(cuesheet, cs->num_tracks)) {
452                 *error_message = "memory allocation error";
453                 return false;
454         }
455         cs->tracks[cs->num_tracks-1].number = forced_leadout_track_num;
456         cs->tracks[cs->num_tracks-1].offset = forced_leadout_track_offset;
457
458         if(!feof(file)) {
459                 *error_message = "read error";
460                 return false;
461         }
462         return true;
463 #undef FLAC__STRCASECMP
464 }
465
466 FLAC__StreamMetadata *grabbag__cuesheet_parse(FILE *file, const char **error_message, unsigned *last_line_read, FLAC__bool is_cdda, FLAC__uint64 lead_out_offset)
467 {
468         FLAC__StreamMetadata *cuesheet;
469
470         FLAC__ASSERT(0 != file);
471         FLAC__ASSERT(0 != error_message);
472         FLAC__ASSERT(0 != last_line_read);
473
474         *last_line_read = 0;
475         cuesheet = FLAC__metadata_object_new(FLAC__METADATA_TYPE_CUESHEET);
476
477         if(0 == cuesheet) {
478                 *error_message = "memory allocation error";
479                 return 0;
480         }
481
482         if(!local__cuesheet_parse_(file, error_message, last_line_read, cuesheet, is_cdda, lead_out_offset)) {
483                 FLAC__metadata_object_delete(cuesheet);
484                 return 0;
485         }
486
487         return cuesheet;
488 }
489
490 void grabbag__cuesheet_emit(FILE *file, const FLAC__StreamMetadata *cuesheet, const char *file_reference, FLAC__bool is_cdda)
491 {
492         const FLAC__StreamMetadata_CueSheet *cs;
493         unsigned track_num, index_num;
494
495         FLAC__ASSERT(0 != file);
496         FLAC__ASSERT(0 != cuesheet);
497         FLAC__ASSERT(cuesheet->type == FLAC__METADATA_TYPE_CUESHEET);
498
499         cs = &cuesheet->data.cue_sheet;
500
501         if(*(cs->media_catalog_number))
502                 fprintf(file, "CATALOG %s\n", cs->media_catalog_number);
503         fprintf(file, "FILE %s\n", file_reference);
504
505         for(track_num = 0; track_num < cs->num_tracks-1; track_num++) {
506                 const FLAC__StreamMetadata_CueSheet_Track *track = cs->tracks + track_num;
507
508                 fprintf(file, "  TRACK %02u %s\n", (unsigned)track->number, track->type == 0? "AUDIO" : "DATA");
509
510                 if(track->pre_emphasis)
511                         fprintf(file, "    FLAGS PRE\n");
512                 if(*(track->isrc))
513                         fprintf(file, "    ISRC %s\n", track->isrc);
514
515                 for(index_num = 0; index_num < track->num_indices; index_num++) {
516                         const FLAC__StreamMetadata_CueSheet_Index *index = track->indices + index_num;
517
518                         fprintf(file, "    INDEX %02u ", (unsigned)index->number);
519                         if(is_cdda) {
520                                 const unsigned logical_frame = (track->offset + index->offset) / (44100 / 75);
521                                 unsigned m, s, f;
522                                 grabbag__cuesheet_frame_to_msf(logical_frame, &m, &s, &f);
523                                 fprintf(file, "%02u:%02u:%02u\n", m, s, f);
524                         }
525                         else
526                                 fprintf(file, "%llu\n", track->offset + index->offset);
527                 }
528         }
529
530         fprintf(file, "REM FLAC__lead-in %llu\n", cs->lead_in);
531         fprintf(file, "REM FLAC__lead-out %u %llu\n", (unsigned)cs->tracks[track_num].number, cs->tracks[track_num].offset);
532 }