1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
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.
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.
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.
29 #include "FLAC/assert.h"
30 #include "FLAC/metadata.h"
31 #include "share/compat.h"
34 #include <sys/ioctl.h>
37 const char *CHANNEL_MASK_TAG = "WAVEFORMATEXTENSIBLE_CHANNEL_MASK";
39 int flac__utils_verbosity_ = 2;
41 static FLAC__bool local__parse_uint64_(const char *s, FLAC__uint64 *value)
49 while('\0' != (c = *s++))
50 if(c >= '0' && c <= '9')
51 ret = ret * 10 + (c - '0');
59 static FLAC__bool local__parse_timecode_(const char *s, double *value)
65 /* parse [0-9][0-9]*: */
67 if(c >= '0' && c <= '9')
71 while(':' != (c = *s++)) {
72 if(c >= '0' && c <= '9')
73 i = i * 10 + (c - '0');
77 ret = (double)i * 60.;
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))
82 ret += strtod(s, &endptr);
83 if (endptr == s || *endptr)
90 static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *track, unsigned *indx)
92 FLAC__bool got_track = false, got_index = false;
93 unsigned t = 0, i = 0;
96 while(end? s < end : *s != '\0') {
98 if(c >= '0' && c <= '9') {
99 t = t * 10 + (c - '0');
107 while(end? s < end : *s != '\0') {
109 if(c >= '0' && c <= '9') {
110 i = i * 10 + (c - '0');
118 return got_track && got_index;
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.
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)
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;
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;
145 void flac__utils_printf(FILE *stream, int level, const char *format, ...)
147 if(flac__utils_verbosity_ >= level) {
150 FLAC__ASSERT(0 != format);
152 va_start(args, format);
154 (void) flac_vfprintf(stream, format, args);
160 fflush(stream); /* for some reason stderr is buffered in at least some if not all MSC libs */
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;
171 int get_console_width(void)
175 width = win_get_console_width();
178 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != -1) width = w.ws_col;
183 size_t strlen_console(const char *text)
186 return strlen_utf8(text);
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);
202 void stats_new_file(void)
204 is_name_printed = false;
207 void stats_clear(void)
209 while (stats_char_count > 0 && stats_char_count--)
210 fprintf(stderr, "\b");
213 void stats_print_name(int level, const char *name)
217 if (flac__utils_verbosity_ >= level) {
219 if(is_name_printed) return;
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;
229 void stats_print_info(int level, const char *format, ...)
232 int len, cleared_len;
234 if (flac__utils_verbosity_ >= level) {
236 va_start(args, format);
237 len = vsnprintf(tmp, sizeof(tmp), format, args);
239 if (len < 0 || len == sizeof(tmp)) {
240 tmp[sizeof(tmp)-1] = '\0';
243 cleared_len = stats_char_count;
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;
250 stats_char_count = fprintf(stderr, "%s", tmp);
254 #ifdef FLAC__VALGRIND_TESTING
255 size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
257 size_t ret = fwrite(ptr, size, nmemb, stream);
264 FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec)
267 FLAC__bool is_negative = false;
269 FLAC__ASSERT(0 != spec);
271 spec->is_relative = false;
272 spec->value_is_samples = true;
273 spec->value.samples = 0;
278 spec->is_relative = true;
281 else if(s[0] == '+') {
282 spec->is_relative = true;
286 if(local__parse_uint64_(s, &val)) {
287 spec->value_is_samples = true;
288 spec->value.samples = (FLAC__int64)val;
290 spec->value.samples = -(spec->value.samples);
294 if(!local__parse_timecode_(s, &d))
296 spec->value_is_samples = false;
297 spec->value.seconds = d;
299 spec->value.seconds = -(spec->value.seconds);
306 void flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification *spec, unsigned sample_rate)
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;
315 FLAC__bool flac__utils_parse_cue_specification(const char *s, utils__CueSpecification *spec)
317 const char *start = s, *end = 0;
319 FLAC__ASSERT(0 != spec);
321 spec->has_start_point = spec->has_end_point = false;
334 if(!local__parse_cue_(start, s, &spec->start_track, &spec->start_index))
336 spec->has_start_point = true;
340 if(!local__parse_cue_(end, 0, &spec->end_track, &spec->end_index))
342 spec->has_end_point = true;
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)
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);
356 skip_spec->is_relative = false;
357 skip_spec->value_is_samples = true;
359 until_spec->is_relative = false;
360 until_spec->value_is_samples = true;
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);
365 skip_spec->value.samples = 0;
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);
370 until_spec->value.samples = total_samples;
373 FLAC__bool flac__utils_set_channel_mask_tag(FLAC__StreamMetadata *object, FLAC__uint32 channel_mask)
375 FLAC__StreamMetadata_VorbisComment_Entry entry = { 0, 0 };
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))
384 if(!FLAC__metadata_object_vorbiscomment_replace_comment(object, entry, /*all=*/true, /*copy=*/true))
389 FLAC__bool flac__utils_get_channel_mask_tag(const FLAC__StreamMetadata *object, FLAC__uint32 *channel_mask)
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)))
398 if(object->data.vorbis_comment.comments[offset].length < strlen(CHANNEL_MASK_TAG)+4)
400 if(0 == (p = strchr((const char *)object->data.vorbis_comment.comments[offset].entry, '='))) /* should never happen, but just in case */
402 if(strncmp(p, "=0x", 3))
404 if(sscanf(p+3, "%x", &val) != 1)