4b36141b74d2cd6177b7258d83fed76ee446ab5d
[platform/upstream/flac.git] / src / flac / utils.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 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 <math.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "utils.h"
29 #include "FLAC/assert.h"
30 #include "FLAC/metadata.h"
31 #include "share/compat.h"
32
33 const char *CHANNEL_MASK_TAG = "WAVEFORMATEXTENSIBLE_CHANNEL_MASK";
34
35 int flac__utils_verbosity_ = 2;
36
37 static FLAC__bool local__parse_uint64_(const char *s, FLAC__uint64 *value)
38 {
39         FLAC__uint64 ret = 0;
40         char c;
41
42         if(*s == '\0')
43                 return false;
44
45         while('\0' != (c = *s++))
46                 if(c >= '0' && c <= '9')
47                         ret = ret * 10 + (c - '0');
48                 else
49                         return false;
50
51         *value = ret;
52         return true;
53 }
54
55 static FLAC__bool local__parse_timecode_(const char *s, double *value)
56 {
57         double ret;
58         unsigned i;
59         char c, *endptr;
60
61         /* parse [0-9][0-9]*: */
62         c = *s++;
63         if(c >= '0' && c <= '9')
64                 i = (c - '0');
65         else
66                 return false;
67         while(':' != (c = *s++)) {
68                 if(c >= '0' && c <= '9')
69                         i = i * 10 + (c - '0');
70                 else
71                         return false;
72         }
73         ret = (double)i * 60.;
74
75         /* parse [0-9]*[.,]?[0-9]* i.e. a sign-less rational number (. or , OK for fractional seconds, to support different locales) */
76         if(strspn(s, "1234567890.,") != strlen(s))
77                 return false;
78         ret += strtod(s, &endptr);
79         if (endptr == s || *endptr)
80                 return false;
81
82         *value = ret;
83         return true;
84 }
85
86 static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *track, unsigned *index)
87 {
88         FLAC__bool got_track = false, got_index = false;
89         unsigned t = 0, i = 0;
90         char c;
91
92         while(end? s < end : *s != '\0') {
93                 c = *s++;
94                 if(c >= '0' && c <= '9') {
95                         t = t * 10 + (c - '0');
96                         got_track = true;
97                 }
98                 else if(c == '.')
99                         break;
100                 else
101                         return false;
102         }
103         while(end? s < end : *s != '\0') {
104                 c = *s++;
105                 if(c >= '0' && c <= '9') {
106                         i = i * 10 + (c - '0');
107                         got_index = true;
108                 }
109                 else
110                         return false;
111         }
112         *track = t;
113         *index = i;
114         return got_track && got_index;
115 }
116
117 /*
118  * this only works with sorted cuesheets (the spec strongly recommends but
119  * does not require sorted cuesheets).  but if it's not sorted, picking a
120  * nearest cue point has no significance.
121  */
122 static FLAC__uint64 local__find_closest_cue_(const FLAC__StreamMetadata_CueSheet *cuesheet, unsigned track, unsigned index, FLAC__uint64 total_samples, FLAC__bool look_forward)
123 {
124         int t, i;
125         if(look_forward) {
126                 for(t = 0; t < (int)cuesheet->num_tracks; t++)
127                         for(i = 0; i < (int)cuesheet->tracks[t].num_indices; i++)
128                                 if(cuesheet->tracks[t].number > track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number >= index))
129                                         return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset;
130                 return total_samples;
131         }
132         else {
133                 for(t = (int)cuesheet->num_tracks - 1; t >= 0; t--)
134                         for(i = (int)cuesheet->tracks[t].num_indices - 1; i >= 0; i--)
135                                 if(cuesheet->tracks[t].number < track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number <= index))
136                                         return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset;
137                 return 0;
138         }
139 }
140
141 void flac__utils_printf(FILE *stream, int level, const char *format, ...)
142 {
143         if(flac__utils_verbosity_ >= level) {
144                 va_list args;
145
146                 FLAC__ASSERT(0 != format);
147
148                 va_start(args, format);
149
150                 (void) vfprintf(stream, format, args);
151
152                 va_end(args);
153
154 #ifdef _MSC_VER
155                 if(stream == stderr)
156                         fflush(stream); /* for some reason stderr is buffered in at least some if not all MSC libs */
157 #endif
158         }
159 }
160
161 #ifdef FLAC__VALGRIND_TESTING
162 size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
163 {
164         size_t ret = fwrite(ptr, size, nmemb, stream);
165         if(!ferror(stream))
166                 fflush(stream);
167         return ret;
168 }
169 #endif
170
171 FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec)
172 {
173         FLAC__uint64 val;
174         FLAC__bool is_negative = false;
175
176         FLAC__ASSERT(0 != spec);
177
178         spec->is_relative = false;
179         spec->value_is_samples = true;
180         spec->value.samples = 0;
181
182         if(0 != s) {
183                 if(s[0] == '-') {
184                         is_negative = true;
185                         spec->is_relative = true;
186                         s++;
187                 }
188                 else if(s[0] == '+') {
189                         spec->is_relative = true;
190                         s++;
191                 }
192
193                 if(local__parse_uint64_(s, &val)) {
194                         spec->value_is_samples = true;
195                         spec->value.samples = (FLAC__int64)val;
196                         if(is_negative)
197                                 spec->value.samples = -(spec->value.samples);
198                 }
199                 else {
200                         double d;
201                         if(!local__parse_timecode_(s, &d))
202                                 return false;
203                         spec->value_is_samples = false;
204                         spec->value.seconds = d;
205                         if(is_negative)
206                                 spec->value.seconds = -(spec->value.seconds);
207                 }
208         }
209
210         return true;
211 }
212
213 void flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification *spec, unsigned sample_rate)
214 {
215         FLAC__ASSERT(0 != spec);
216         if(!spec->value_is_samples) {
217                 spec->value.samples = (FLAC__int64)(spec->value.seconds * (double)sample_rate);
218                 spec->value_is_samples = true;
219         }
220 }
221
222 FLAC__bool flac__utils_parse_cue_specification(const char *s, utils__CueSpecification *spec)
223 {
224         const char *start = s, *end = 0;
225
226         FLAC__ASSERT(0 != spec);
227
228         spec->has_start_point = spec->has_end_point = false;
229
230         s = strchr(s, '-');
231
232         if(0 != s) {
233                 if(s == start)
234                         start = 0;
235                 end = s+1;
236                 if(*end == '\0')
237                         end = 0;
238         }
239
240         if(start) {
241                 if(!local__parse_cue_(start, s, &spec->start_track, &spec->start_index))
242                         return false;
243                 spec->has_start_point = true;
244         }
245
246         if(end) {
247                 if(!local__parse_cue_(end, 0, &spec->end_track, &spec->end_index))
248                         return false;
249                 spec->has_end_point = true;
250         }
251
252         return true;
253 }
254
255 void flac__utils_canonicalize_cue_specification(const utils__CueSpecification *cue_spec, const FLAC__StreamMetadata_CueSheet *cuesheet, FLAC__uint64 total_samples, utils__SkipUntilSpecification *skip_spec, utils__SkipUntilSpecification *until_spec)
256 {
257         FLAC__ASSERT(0 != cue_spec);
258         FLAC__ASSERT(0 != cuesheet);
259         FLAC__ASSERT(0 != total_samples);
260         FLAC__ASSERT(0 != skip_spec);
261         FLAC__ASSERT(0 != until_spec);
262
263         skip_spec->is_relative = false;
264         skip_spec->value_is_samples = true;
265
266         until_spec->is_relative = false;
267         until_spec->value_is_samples = true;
268
269         if(cue_spec->has_start_point)
270                 skip_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->start_track, cue_spec->start_index, total_samples, /*look_forward=*/false);
271         else
272                 skip_spec->value.samples = 0;
273
274         if(cue_spec->has_end_point)
275                 until_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->end_track, cue_spec->end_index, total_samples, /*look_forward=*/true);
276         else
277                 until_spec->value.samples = total_samples;
278 }
279
280 FLAC__bool flac__utils_set_channel_mask_tag(FLAC__StreamMetadata *object, FLAC__uint32 channel_mask)
281 {
282         FLAC__StreamMetadata_VorbisComment_Entry entry = { 0, 0 };
283         char tag[128];
284
285         FLAC__ASSERT(object);
286         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
287         FLAC__ASSERT(strlen(CHANNEL_MASK_TAG+1+2+16+1) <= sizeof(tag)); /* +1 for =, +2 for 0x, +16 for digits, +1 for NUL */
288         entry.entry = (FLAC__byte*)tag;
289         if((entry.length = flac_snprintf(tag, sizeof(tag), "%s=0x%04X", CHANNEL_MASK_TAG, (unsigned)channel_mask)) >= sizeof(tag))
290                 return false;
291         if(!FLAC__metadata_object_vorbiscomment_replace_comment(object, entry, /*all=*/true, /*copy=*/true))
292                 return false;
293         return true;
294 }
295
296 FLAC__bool flac__utils_get_channel_mask_tag(const FLAC__StreamMetadata *object, FLAC__uint32 *channel_mask)
297 {
298         int offset;
299         unsigned val;
300         char *p;
301         FLAC__ASSERT(object);
302         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
303         if(0 > (offset = FLAC__metadata_object_vorbiscomment_find_entry_from(object, /*offset=*/0, CHANNEL_MASK_TAG)))
304                 return false;
305         if(object->data.vorbis_comment.comments[offset].length < strlen(CHANNEL_MASK_TAG)+4)
306                 return false;
307         if(0 == (p = strchr((const char *)object->data.vorbis_comment.comments[offset].entry, '='))) /* should never happen, but just in case */
308                 return false;
309         if(strncmp(p, "=0x", 3))
310                 return false;
311         if(sscanf(p+3, "%x", &val) != 1)
312                 return false;
313         *channel_mask = val;
314         return true;
315 }