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"
33 const char *CHANNEL_MASK_TAG = "WAVEFORMATEXTENSIBLE_CHANNEL_MASK";
35 int flac__utils_verbosity_ = 2;
37 static FLAC__bool local__parse_uint64_(const char *s, FLAC__uint64 *value)
45 while('\0' != (c = *s++))
46 if(c >= '0' && c <= '9')
47 ret = ret * 10 + (c - '0');
55 static FLAC__bool local__parse_timecode_(const char *s, double *value)
61 /* parse [0-9][0-9]*: */
63 if(c >= '0' && c <= '9')
67 while(':' != (c = *s++)) {
68 if(c >= '0' && c <= '9')
69 i = i * 10 + (c - '0');
73 ret = (double)i * 60.;
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))
78 ret += strtod(s, &endptr);
79 if (endptr == s || *endptr)
86 static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *track, unsigned *index)
88 FLAC__bool got_track = false, got_index = false;
89 unsigned t = 0, i = 0;
92 while(end? s < end : *s != '\0') {
94 if(c >= '0' && c <= '9') {
95 t = t * 10 + (c - '0');
103 while(end? s < end : *s != '\0') {
105 if(c >= '0' && c <= '9') {
106 i = i * 10 + (c - '0');
114 return got_track && got_index;
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.
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)
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;
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;
141 void flac__utils_printf(FILE *stream, int level, const char *format, ...)
143 if(flac__utils_verbosity_ >= level) {
146 FLAC__ASSERT(0 != format);
148 va_start(args, format);
150 (void) vfprintf(stream, format, args);
156 fflush(stream); /* for some reason stderr is buffered in at least some if not all MSC libs */
161 #ifdef FLAC__VALGRIND_TESTING
162 size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
164 size_t ret = fwrite(ptr, size, nmemb, stream);
171 FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec)
174 FLAC__bool is_negative = false;
176 FLAC__ASSERT(0 != spec);
178 spec->is_relative = false;
179 spec->value_is_samples = true;
180 spec->value.samples = 0;
185 spec->is_relative = true;
188 else if(s[0] == '+') {
189 spec->is_relative = true;
193 if(local__parse_uint64_(s, &val)) {
194 spec->value_is_samples = true;
195 spec->value.samples = (FLAC__int64)val;
197 spec->value.samples = -(spec->value.samples);
201 if(!local__parse_timecode_(s, &d))
203 spec->value_is_samples = false;
204 spec->value.seconds = d;
206 spec->value.seconds = -(spec->value.seconds);
213 void flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification *spec, unsigned sample_rate)
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;
222 FLAC__bool flac__utils_parse_cue_specification(const char *s, utils__CueSpecification *spec)
224 const char *start = s, *end = 0;
226 FLAC__ASSERT(0 != spec);
228 spec->has_start_point = spec->has_end_point = false;
241 if(!local__parse_cue_(start, s, &spec->start_track, &spec->start_index))
243 spec->has_start_point = true;
247 if(!local__parse_cue_(end, 0, &spec->end_track, &spec->end_index))
249 spec->has_end_point = true;
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)
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);
263 skip_spec->is_relative = false;
264 skip_spec->value_is_samples = true;
266 until_spec->is_relative = false;
267 until_spec->value_is_samples = true;
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);
272 skip_spec->value.samples = 0;
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);
277 until_spec->value.samples = total_samples;
280 FLAC__bool flac__utils_set_channel_mask_tag(FLAC__StreamMetadata *object, FLAC__uint32 channel_mask)
282 FLAC__StreamMetadata_VorbisComment_Entry entry = { 0, 0 };
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))
291 if(!FLAC__metadata_object_vorbiscomment_replace_comment(object, entry, /*all=*/true, /*copy=*/true))
296 FLAC__bool flac__utils_get_channel_mask_tag(const FLAC__StreamMetadata *object, FLAC__uint32 *channel_mask)
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)))
305 if(object->data.vorbis_comment.comments[offset].length < strlen(CHANNEL_MASK_TAG)+4)
307 if(0 == (p = strchr((const char *)object->data.vorbis_comment.comments[offset].entry, '='))) /* should never happen, but just in case */
309 if(strncmp(p, "=0x", 3))
311 if(sscanf(p+3, "%x", &val) != 1)