10f814b4222af3d679140a49ce39750036d93636
[platform/upstream/flac.git] / src / metaflac / operations_shorthand_cuesheet.c
1 /* metaflac - Command-line FLAC metadata editor
2  * Copyright (C) 2001,2002,2003,2004,2005,2006,2007,2008,2009  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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <errno.h>
24 #include <string.h>
25 #include "options.h"
26 #include "utils.h"
27 #include "FLAC/assert.h"
28 #include "share/grabbag.h"
29 #include "share/compat.h"
30 #include "operations_shorthand.h"
31
32 static FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, unsigned sample_rate, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link);
33 static FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename);
34
35 FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata_Chain *chain, const Operation *operation, FLAC__bool *needs_write)
36 {
37         FLAC__bool ok = true;
38         FLAC__StreamMetadata *cuesheet = 0;
39         FLAC__Metadata_Iterator *iterator = FLAC__metadata_iterator_new();
40         FLAC__uint64 lead_out_offset = 0;
41         FLAC__bool is_cdda = false;
42         unsigned sample_rate = 0;
43
44         if(0 == iterator)
45                 die("out of memory allocating iterator");
46
47         FLAC__metadata_iterator_init(iterator, chain);
48
49         do {
50                 FLAC__StreamMetadata *block = FLAC__metadata_iterator_get_block(iterator);
51                 if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
52                         lead_out_offset = block->data.stream_info.total_samples;
53                         if(lead_out_offset == 0) {
54                                 flac_fprintf(stderr, "%s: ERROR: FLAC file must have total_samples set in STREAMINFO in order to import/export cuesheet\n", filename);
55                                 FLAC__metadata_iterator_delete(iterator);
56                                 return false;
57                         }
58                         sample_rate = block->data.stream_info.sample_rate;
59                         is_cdda = (block->data.stream_info.channels == 1 || block->data.stream_info.channels == 2) && (block->data.stream_info.bits_per_sample == 16) && (sample_rate == 44100);
60                 }
61                 else if(block->type == FLAC__METADATA_TYPE_CUESHEET)
62                         cuesheet = block;
63         } while(FLAC__metadata_iterator_next(iterator));
64
65         if(lead_out_offset == 0) {
66                 flac_fprintf(stderr, "%s: ERROR: FLAC stream has no STREAMINFO block\n", filename);
67                 FLAC__metadata_iterator_delete(iterator);
68                 return false;
69         }
70
71         switch(operation->type) {
72                 case OP__IMPORT_CUESHEET_FROM:
73                         if(0 != cuesheet) {
74                                 flac_fprintf(stderr, "%s: ERROR: FLAC file already has CUESHEET block\n", filename);
75                                 ok = false;
76                         }
77                         else {
78                                 ok = import_cs_from(filename, &cuesheet, operation->argument.import_cuesheet_from.filename, needs_write, lead_out_offset, sample_rate, is_cdda, operation->argument.import_cuesheet_from.add_seekpoint_link);
79                                 if(ok) {
80                                         /* append CUESHEET block */
81                                         while(FLAC__metadata_iterator_next(iterator))
82                                                 ;
83                                         if(!FLAC__metadata_iterator_insert_block_after(iterator, cuesheet)) {
84                                                 print_error_with_chain_status(chain, "%s: ERROR: adding new CUESHEET block to metadata", filename);
85                                                 FLAC__metadata_object_delete(cuesheet);
86                                                 ok = false;
87                                         }
88                                 }
89                         }
90                         break;
91                 case OP__EXPORT_CUESHEET_TO:
92                         if(0 == cuesheet) {
93                                 flac_fprintf(stderr, "%s: ERROR: FLAC file has no CUESHEET block\n", filename);
94                                 ok = false;
95                         }
96                         else
97                                 ok = export_cs_to(filename, cuesheet, operation->argument.filename.value);
98                         break;
99                 default:
100                         ok = false;
101                         FLAC__ASSERT(0);
102                         break;
103         };
104
105         FLAC__metadata_iterator_delete(iterator);
106         return ok;
107 }
108
109 /*
110  * local routines
111  */
112
113 FLAC__bool import_cs_from(const char *filename, FLAC__StreamMetadata **cuesheet, const char *cs_filename, FLAC__bool *needs_write, FLAC__uint64 lead_out_offset, unsigned sample_rate, FLAC__bool is_cdda, Argument_AddSeekpoint *add_seekpoint_link)
114 {
115         FILE *f;
116         const char *error_message;
117         char **seekpoint_specification = add_seekpoint_link? &(add_seekpoint_link->specification) : 0;
118         unsigned last_line_read;
119
120         if(0 == cs_filename || strlen(cs_filename) == 0) {
121                 flac_fprintf(stderr, "%s: ERROR: empty import file name\n", filename);
122                 return false;
123         }
124         if(0 == strcmp(cs_filename, "-"))
125                 f = stdin;
126         else
127                 f = flac_fopen(cs_filename, "r");
128
129         if(0 == f) {
130                 flac_fprintf(stderr, "%s: ERROR: can't open import file %s: %s\n", filename, cs_filename, strerror(errno));
131                 return false;
132         }
133
134         *cuesheet = grabbag__cuesheet_parse(f, &error_message, &last_line_read, sample_rate, is_cdda, lead_out_offset);
135
136         if(f != stdin)
137                 fclose(f);
138
139         if(0 == *cuesheet) {
140                 flac_fprintf(stderr, "%s: ERROR: while parsing cuesheet \"%s\" on line %u: %s\n", filename, cs_filename, last_line_read, error_message);
141                 return false;
142         }
143
144         if(!FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/false, &error_message)) {
145                 flac_fprintf(stderr, "%s: ERROR parsing cuesheet \"%s\": %s\n", filename, cs_filename, error_message);
146                 return false;
147         }
148
149         /* if we're expecting CDDA, warn about non-compliance */
150         if(is_cdda && !FLAC__format_cuesheet_is_legal(&(*cuesheet)->data.cue_sheet, /*check_cd_da_subset=*/true, &error_message)) {
151                 flac_fprintf(stderr, "%s: WARNING cuesheet \"%s\" is not audio CD compliant: %s\n", filename, cs_filename, error_message);
152                 (*cuesheet)->data.cue_sheet.is_cd = false;
153         }
154
155         /* add seekpoints for each index point if required */
156         if(0 != seekpoint_specification) {
157                 char spec[128];
158                 unsigned track, index;
159                 const FLAC__StreamMetadata_CueSheet *cs = &(*cuesheet)->data.cue_sheet;
160                 if(0 == *seekpoint_specification)
161                         *seekpoint_specification = local_strdup("");
162                 for(track = 0; track < cs->num_tracks; track++) {
163                         const FLAC__StreamMetadata_CueSheet_Track *tr = cs->tracks+track;
164                         for(index = 0; index < tr->num_indices; index++) {
165                                 flac_snprintf(spec, sizeof (spec), "%" PRIu64 ";", (tr->offset + tr->indices[index].offset));
166                                 local_strcat(seekpoint_specification, spec);
167                         }
168                 }
169         }
170
171         *needs_write = true;
172         return true;
173 }
174
175 FLAC__bool export_cs_to(const char *filename, const FLAC__StreamMetadata *cuesheet, const char *cs_filename)
176 {
177         FILE *f;
178         char *ref = 0;
179         size_t reflen;
180
181         if(0 == cs_filename || strlen(cs_filename) == 0) {
182                 flac_fprintf(stderr, "%s: ERROR: empty export file name\n", filename);
183                 return false;
184         }
185         if(0 == strcmp(cs_filename, "-"))
186                 f = stdout;
187         else
188                 f = flac_fopen(cs_filename, "w");
189
190         if(0 == f) {
191                 flac_fprintf(stderr, "%s: ERROR: can't open export file %s: %s\n", filename, cs_filename, strerror(errno));
192                 return false;
193         }
194
195         reflen = strlen(filename) + 7 + 1;
196         if(0 == (ref = malloc(reflen))) {
197                 flac_fprintf(stderr, "%s: ERROR: allocating memory\n", filename);
198                 if(f != stdout)
199                         fclose(f);
200                 return false;
201         }
202
203         flac_snprintf(ref, reflen, "\"%s\" FLAC", filename);
204
205         grabbag__cuesheet_emit(f, cuesheet, ref);
206
207         free(ref);
208
209         if(f != stdout)
210                 fclose(f);
211
212         return true;
213 }