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