1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2013 Xiph.Org Foundation
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "FLAC/assert.h"
31 #include "FLAC/metadata.h"
32 #include "share/compat.h"
38 #ifdef GWINSZ_IN_SYS_IOCTL
39 # include <sys/ioctl.h>
43 const char *CHANNEL_MASK_TAG = "WAVEFORMATEXTENSIBLE_CHANNEL_MASK";
45 int flac__utils_verbosity_ = 2;
47 static FLAC__bool local__parse_uint64_(const char *s, FLAC__uint64 *value)
55 while('\0' != (c = *s++))
56 if(c >= '0' && c <= '9')
57 ret = ret * 10 + (c - '0');
65 static FLAC__bool local__parse_timecode_(const char *s, double *value)
71 /* parse [0-9][0-9]*: */
73 if(c >= '0' && c <= '9')
77 while(':' != (c = *s++)) {
78 if(c >= '0' && c <= '9')
79 i = i * 10 + (c - '0');
83 ret = (double)i * 60.;
85 /* parse [0-9]*[.,]?[0-9]* i.e. a sign-less rational number (. or , OK for fractional seconds, to support different locales) */
86 if(strspn(s, "1234567890.,") != strlen(s))
88 ret += strtod(s, &endptr);
89 if (endptr == s || *endptr)
96 static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *track, unsigned *indx)
98 FLAC__bool got_track = false, got_index = false;
99 unsigned t = 0, i = 0;
102 while(end? s < end : *s != '\0') {
104 if(c >= '0' && c <= '9') {
105 t = t * 10 + (c - '0');
113 while(end? s < end : *s != '\0') {
115 if(c >= '0' && c <= '9') {
116 i = i * 10 + (c - '0');
124 return got_track && got_index;
128 * this only works with sorted cuesheets (the spec strongly recommends but
129 * does not require sorted cuesheets). but if it's not sorted, picking a
130 * nearest cue point has no significance.
132 static FLAC__uint64 local__find_closest_cue_(const FLAC__StreamMetadata_CueSheet *cuesheet, unsigned track, unsigned indx, FLAC__uint64 total_samples, FLAC__bool look_forward)
136 for(t = 0; t < (int)cuesheet->num_tracks; t++)
137 for(i = 0; i < (int)cuesheet->tracks[t].num_indices; i++)
138 if(cuesheet->tracks[t].number > track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number >= indx))
139 return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset;
140 return total_samples;
143 for(t = (int)cuesheet->num_tracks - 1; t >= 0; t--)
144 for(i = (int)cuesheet->tracks[t].num_indices - 1; i >= 0; i--)
145 if(cuesheet->tracks[t].number < track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number <= indx))
146 return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset;
151 void flac__utils_printf(FILE *stream, int level, const char *format, ...)
153 if(flac__utils_verbosity_ >= level) {
156 FLAC__ASSERT(0 != format);
158 va_start(args, format);
160 (void) flac_vfprintf(stream, format, args);
166 fflush(stream); /* for some reason stderr is buffered in at least some if not all MSC libs */
171 /* variables and functions for console status output */
172 static FLAC__bool is_name_printed;
173 static int stats_char_count = 0;
174 static int console_width;
175 static int console_chars_left;
177 int get_console_width(void)
181 width = win_get_console_width();
182 #elif defined __EMX__
186 #elif !defined __ANDROID__
188 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) width = w.ws_col;
193 size_t strlen_console(const char *text)
196 return strlen_utf8(text);
201 len = strlen(text)+1;
202 wtmp = (wchar_t *)malloc(len*sizeof(wchar_t));
203 if (wtmp == NULL) return len-1;
204 mbstowcs(wtmp, text, len);
205 len = wcswidth(wtmp, len);
212 void stats_new_file(void)
214 is_name_printed = false;
217 void stats_clear(void)
219 while (stats_char_count > 0 && stats_char_count--)
220 fprintf(stderr, "\b");
223 void stats_print_name(int level, const char *name)
227 if (flac__utils_verbosity_ >= level) {
229 if(is_name_printed) return;
231 console_width = get_console_width();
232 len = strlen_console(name)+2;
233 console_chars_left = console_width - (len % console_width);
234 flac_fprintf(stderr, "%s: ", name);
235 is_name_printed = true;
239 void stats_print_info(int level, const char *format, ...)
244 if (flac__utils_verbosity_ >= level) {
246 va_start(args, format);
247 len = vsnprintf(tmp, sizeof(tmp), format, args);
249 if (len < 0 || len == sizeof(tmp)) {
250 tmp[sizeof(tmp)-1] = '\0';
254 if (len >= console_chars_left) {
255 clear_len = console_chars_left;
256 while (clear_len > 0 && clear_len--) fprintf(stderr, " ");
257 fprintf(stderr, "\n");
258 console_chars_left = console_width;
260 stats_char_count = fprintf(stderr, "%s", tmp);
264 #ifdef FLAC__VALGRIND_TESTING
265 size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
267 size_t ret = fwrite(ptr, size, nmemb, stream);
274 FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec)
277 FLAC__bool is_negative = false;
279 FLAC__ASSERT(0 != spec);
281 spec->is_relative = false;
282 spec->value_is_samples = true;
283 spec->value.samples = 0;
288 spec->is_relative = true;
291 else if(s[0] == '+') {
292 spec->is_relative = true;
296 if(local__parse_uint64_(s, &val)) {
297 spec->value_is_samples = true;
298 spec->value.samples = (FLAC__int64)val;
300 spec->value.samples = -(spec->value.samples);
304 if(!local__parse_timecode_(s, &d))
306 spec->value_is_samples = false;
307 spec->value.seconds = d;
309 spec->value.seconds = -(spec->value.seconds);
316 void flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification *spec, unsigned sample_rate)
318 FLAC__ASSERT(0 != spec);
319 if(!spec->value_is_samples) {
320 spec->value.samples = (FLAC__int64)(spec->value.seconds * (double)sample_rate);
321 spec->value_is_samples = true;
325 FLAC__bool flac__utils_parse_cue_specification(const char *s, utils__CueSpecification *spec)
327 const char *start = s, *end = 0;
329 FLAC__ASSERT(0 != spec);
331 spec->has_start_point = spec->has_end_point = false;
344 if(!local__parse_cue_(start, s, &spec->start_track, &spec->start_index))
346 spec->has_start_point = true;
350 if(!local__parse_cue_(end, 0, &spec->end_track, &spec->end_index))
352 spec->has_end_point = true;
358 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)
360 FLAC__ASSERT(0 != cue_spec);
361 FLAC__ASSERT(0 != cuesheet);
362 FLAC__ASSERT(0 != total_samples);
363 FLAC__ASSERT(0 != skip_spec);
364 FLAC__ASSERT(0 != until_spec);
366 skip_spec->is_relative = false;
367 skip_spec->value_is_samples = true;
369 until_spec->is_relative = false;
370 until_spec->value_is_samples = true;
372 if(cue_spec->has_start_point)
373 skip_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->start_track, cue_spec->start_index, total_samples, /*look_forward=*/false);
375 skip_spec->value.samples = 0;
377 if(cue_spec->has_end_point)
378 until_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->end_track, cue_spec->end_index, total_samples, /*look_forward=*/true);
380 until_spec->value.samples = total_samples;
383 FLAC__bool flac__utils_set_channel_mask_tag(FLAC__StreamMetadata *object, FLAC__uint32 channel_mask)
385 FLAC__StreamMetadata_VorbisComment_Entry entry = { 0, 0 };
388 FLAC__ASSERT(object);
389 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
390 FLAC__ASSERT(strlen(CHANNEL_MASK_TAG+1+2+16+1) <= sizeof(tag)); /* +1 for =, +2 for 0x, +16 for digits, +1 for NUL */
391 entry.entry = (FLAC__byte*)tag;
392 if((entry.length = flac_snprintf(tag, sizeof(tag), "%s=0x%04X", CHANNEL_MASK_TAG, (unsigned)channel_mask)) >= sizeof(tag))
394 if(!FLAC__metadata_object_vorbiscomment_replace_comment(object, entry, /*all=*/true, /*copy=*/true))
399 FLAC__bool flac__utils_get_channel_mask_tag(const FLAC__StreamMetadata *object, FLAC__uint32 *channel_mask)
404 FLAC__ASSERT(object);
405 FLAC__ASSERT(object->type == FLAC__METADATA_TYPE_VORBIS_COMMENT);
406 if(0 > (offset = FLAC__metadata_object_vorbiscomment_find_entry_from(object, /*offset=*/0, CHANNEL_MASK_TAG)))
408 if(object->data.vorbis_comment.comments[offset].length < strlen(CHANNEL_MASK_TAG)+4)
410 if(0 == (p = strchr((const char *)object->data.vorbis_comment.comments[offset].entry, '='))) /* should never happen, but just in case */
412 if(strncmp(p, "=0x", 3))
414 if(sscanf(p+3, "%x", &val) != 1)