965ab59595d0e3200c98ebc0663f6d62414d3199
[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 #ifndef _WIN32
33 #include <wchar.h>
34 #include <sys/ioctl.h>
35 #endif
36
37 const char *CHANNEL_MASK_TAG = "WAVEFORMATEXTENSIBLE_CHANNEL_MASK";
38
39 int flac__utils_verbosity_ = 2;
40
41 static FLAC__bool local__parse_uint64_(const char *s, FLAC__uint64 *value)
42 {
43         FLAC__uint64 ret = 0;
44         char c;
45
46         if(*s == '\0')
47                 return false;
48
49         while('\0' != (c = *s++))
50                 if(c >= '0' && c <= '9')
51                         ret = ret * 10 + (c - '0');
52                 else
53                         return false;
54
55         *value = ret;
56         return true;
57 }
58
59 static FLAC__bool local__parse_timecode_(const char *s, double *value)
60 {
61         double ret;
62         unsigned i;
63         char c, *endptr;
64
65         /* parse [0-9][0-9]*: */
66         c = *s++;
67         if(c >= '0' && c <= '9')
68                 i = (c - '0');
69         else
70                 return false;
71         while(':' != (c = *s++)) {
72                 if(c >= '0' && c <= '9')
73                         i = i * 10 + (c - '0');
74                 else
75                         return false;
76         }
77         ret = (double)i * 60.;
78
79         /* parse [0-9]*[.,]?[0-9]* i.e. a sign-less rational number (. or , OK for fractional seconds, to support different locales) */
80         if(strspn(s, "1234567890.,") != strlen(s))
81                 return false;
82         ret += strtod(s, &endptr);
83         if (endptr == s || *endptr)
84                 return false;
85
86         *value = ret;
87         return true;
88 }
89
90 static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *track, unsigned *indx)
91 {
92         FLAC__bool got_track = false, got_index = false;
93         unsigned t = 0, i = 0;
94         char c;
95
96         while(end? s < end : *s != '\0') {
97                 c = *s++;
98                 if(c >= '0' && c <= '9') {
99                         t = t * 10 + (c - '0');
100                         got_track = true;
101                 }
102                 else if(c == '.')
103                         break;
104                 else
105                         return false;
106         }
107         while(end? s < end : *s != '\0') {
108                 c = *s++;
109                 if(c >= '0' && c <= '9') {
110                         i = i * 10 + (c - '0');
111                         got_index = true;
112                 }
113                 else
114                         return false;
115         }
116         *track = t;
117         *indx = i;
118         return got_track && got_index;
119 }
120
121 /*
122  * this only works with sorted cuesheets (the spec strongly recommends but
123  * does not require sorted cuesheets).  but if it's not sorted, picking a
124  * nearest cue point has no significance.
125  */
126 static FLAC__uint64 local__find_closest_cue_(const FLAC__StreamMetadata_CueSheet *cuesheet, unsigned track, unsigned indx, FLAC__uint64 total_samples, FLAC__bool look_forward)
127 {
128         int t, i;
129         if(look_forward) {
130                 for(t = 0; t < (int)cuesheet->num_tracks; t++)
131                         for(i = 0; i < (int)cuesheet->tracks[t].num_indices; i++)
132                                 if(cuesheet->tracks[t].number > track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number >= indx))
133                                         return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset;
134                 return total_samples;
135         }
136         else {
137                 for(t = (int)cuesheet->num_tracks - 1; t >= 0; t--)
138                         for(i = (int)cuesheet->tracks[t].num_indices - 1; i >= 0; i--)
139                                 if(cuesheet->tracks[t].number < track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number <= indx))
140                                         return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset;
141                 return 0;
142         }
143 }
144
145 void flac__utils_printf(FILE *stream, int level, const char *format, ...)
146 {
147         if(flac__utils_verbosity_ >= level) {
148                 va_list args;
149
150                 FLAC__ASSERT(0 != format);
151
152                 va_start(args, format);
153
154                 (void) flac_vfprintf(stream, format, args);
155
156                 va_end(args);
157
158 #ifdef _MSC_VER
159                 if(stream == stderr)
160                         fflush(stream); /* for some reason stderr is buffered in at least some if not all MSC libs */
161 #endif
162         }
163 }
164
165 /* variables and functions for console status output */
166 static FLAC__bool is_name_printed;
167 static int stats_char_count = 0;
168 static int console_width;
169 static int console_chars_left;
170
171 int get_console_width()
172 {
173         int width = 80;
174 #ifdef _WIN32
175         width = win_get_console_width();
176 #else
177         struct winsize w;
178         if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) width = w.ws_col;
179 #endif
180         return width;
181 }
182
183 size_t strlen_console(const char *text)
184 {
185 #ifdef _WIN32
186         return strlen_utf8(text);
187 #else
188         size_t len;
189         wchar_t *wtmp;
190
191         len = strlen(text)+1;
192         wtmp = (wchar_t *)malloc(len*sizeof(wchar_t));
193         if (wtmp == NULL) return len-1;
194         mbstowcs(wtmp, text, len);
195         len = wcswidth(wtmp, len);
196         free(wtmp);
197
198         return len;
199 #endif
200 }
201
202 void stats_new_file()
203 {
204         is_name_printed = false;
205 }
206
207 void stats_clear()
208 {
209         while (stats_char_count > 0 && stats_char_count--)
210                 fprintf(stderr, "\b");
211 }
212
213 void stats_print_name(int level, const char *name)
214 {
215         int len;
216
217         if (flac__utils_verbosity_ >= level) {
218                 stats_clear();
219                 if(is_name_printed) return;
220
221                 console_width = get_console_width();
222                 len = strlen_console(name)+2;
223                 console_chars_left = console_width  - (len % console_width);
224                 flac_fprintf(stderr, "%s: ", name);
225                 is_name_printed = true;
226         }
227 }
228
229 void stats_print_info(int level, const char *format, ...)
230 {
231         char tmp[80];
232         int len, cleared_len;
233
234         if (flac__utils_verbosity_ >= level) {
235                 va_list args;
236                 va_start(args, format);
237                 len = vsnprintf(tmp, sizeof(tmp), format, args);
238                 va_end(args);
239                 if (len < 0 || len == sizeof(tmp)) {
240                         tmp[sizeof(tmp)-1] = '\0';
241                         len = sizeof(tmp)-1;
242                 }
243                 cleared_len = stats_char_count;
244                 stats_clear();
245                 if (len >= console_chars_left) {
246                         while (cleared_len > 0 && cleared_len--) fprintf(stderr, " ");
247                         fprintf(stderr, "\n");
248                         console_chars_left = console_width;
249                 }
250                 stats_char_count = fprintf(stderr, "%s", tmp);
251         }
252 }
253
254 #ifdef FLAC__VALGRIND_TESTING
255 size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
256 {
257         size_t ret = fwrite(ptr, size, nmemb, stream);
258         if(!ferror(stream))
259                 fflush(stream);
260         return ret;
261 }
262 #endif
263
264 FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec)
265 {
266         FLAC__uint64 val;
267         FLAC__bool is_negative = false;
268
269         FLAC__ASSERT(0 != spec);
270
271         spec->is_relative = false;
272         spec->value_is_samples = true;
273         spec->value.samples = 0;
274
275         if(0 != s) {
276                 if(s[0] == '-') {
277                         is_negative = true;
278                         spec->is_relative = true;
279                         s++;
280                 }
281                 else if(s[0] == '+') {
282                         spec->is_relative = true;
283                         s++;
284                 }
285
286                 if(local__parse_uint64_(s, &val)) {
287                         spec->value_is_samples = true;
288                         spec->value.samples = (FLAC__int64)val;
289                         if(is_negative)
290                                 spec->value.samples = -(spec->value.samples);
291                 }
292                 else {
293                         double d;
294                         if(!local__parse_timecode_(s, &d))
295                                 return false;
296                         spec->value_is_samples = false;
297                         spec->value.seconds = d;
298                         if(is_negative)
299                                 spec->value.seconds = -(spec->value.seconds);
300                 }
301         }
302
303         return true;
304 }
305
306 void flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification *spec, unsigned sample_rate)
307 {
308         FLAC__ASSERT(0 != spec);
309         if(!spec->value_is_samples) {
310                 spec->value.samples = (FLAC__int64)(spec->value.seconds * (double)sample_rate);
311                 spec->value_is_samples = true;
312         }
313 }
314
315 FLAC__bool flac__utils_parse_cue_specification(const char *s, utils__CueSpecification *spec)
316 {
317         const char *start = s, *end = 0;
318
319         FLAC__ASSERT(0 != spec);
320
321         spec->has_start_point = spec->has_end_point = false;
322
323         s = strchr(s, '-');
324
325         if(0 != s) {
326                 if(s == start)
327                         start = 0;
328                 end = s+1;
329                 if(*end == '\0')
330                         end = 0;
331         }
332
333         if(start) {
334                 if(!local__parse_cue_(start, s, &spec->start_track, &spec->start_index))
335                         return false;
336                 spec->has_start_point = true;
337         }
338
339         if(end) {
340                 if(!local__parse_cue_(end, 0, &spec->end_track, &spec->end_index))
341                         return false;
342                 spec->has_end_point = true;
343         }
344
345         return true;
346 }
347
348 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)
349 {
350         FLAC__ASSERT(0 != cue_spec);
351         FLAC__ASSERT(0 != cuesheet);
352         FLAC__ASSERT(0 != total_samples);
353         FLAC__ASSERT(0 != skip_spec);
354         FLAC__ASSERT(0 != until_spec);
355
356         skip_spec->is_relative = false;
357         skip_spec->value_is_samples = true;
358
359         until_spec->is_relative = false;
360         until_spec->value_is_samples = true;
361
362         if(cue_spec->has_start_point)
363                 skip_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->start_track, cue_spec->start_index, total_samples, /*look_forward=*/false);
364         else
365                 skip_spec->value.samples = 0;
366
367         if(cue_spec->has_end_point)
368                 until_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->end_track, cue_spec->end_index, total_samples, /*look_forward=*/true);
369         else
370                 until_spec->value.samples = total_samples;
371 }
372
373 FLAC__bool flac__utils_set_channel_mask_tag(FLAC__StreamMetadata *object, FLAC__uint32 channel_mask)
374 {
375         FLAC__StreamMetadata_VorbisComment_Entry entry = { 0, 0 };
376         char tag[128];
377
378         FLAC__ASSERT(object);
379         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
380         FLAC__ASSERT(strlen(CHANNEL_MASK_TAG+1+2+16+1) <= sizeof(tag)); /* +1 for =, +2 for 0x, +16 for digits, +1 for NUL */
381         entry.entry = (FLAC__byte*)tag;
382         if((entry.length = flac_snprintf(tag, sizeof(tag), "%s=0x%04X", CHANNEL_MASK_TAG, (unsigned)channel_mask)) >= sizeof(tag))
383                 return false;
384         if(!FLAC__metadata_object_vorbiscomment_replace_comment(object, entry, /*all=*/true, /*copy=*/true))
385                 return false;
386         return true;
387 }
388
389 FLAC__bool flac__utils_get_channel_mask_tag(const FLAC__StreamMetadata *object, FLAC__uint32 *channel_mask)
390 {
391         int offset;
392         unsigned val;
393         char *p;
394         FLAC__ASSERT(object);
395         FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
396         if(0 > (offset = FLAC__metadata_object_vorbiscomment_find_entry_from(object, /*offset=*/0, CHANNEL_MASK_TAG)))
397                 return false;
398         if(object->data.vorbis_comment.comments[offset].length < strlen(CHANNEL_MASK_TAG)+4)
399                 return false;
400         if(0 == (p = strchr((const char *)object->data.vorbis_comment.comments[offset].entry, '='))) /* should never happen, but just in case */
401                 return false;
402         if(strncmp(p, "=0x", 3))
403                 return false;
404         if(sscanf(p+3, "%x", &val) != 1)
405                 return false;
406         *channel_mask = val;
407         return true;
408 }