1 /* flac - Command-line FLAC encoder/decoder
2 * Copyright (C) 2002,2003,2004 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
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.
24 #include "FLAC/assert.h"
29 static FLAC__bool local__parse_uint64_(const char *s, FLAC__uint64 *value)
37 while('\0' != (c = *s++))
38 if(c >= '0' && c <= '9')
39 ret = ret * 10 + (c - '0');
47 static FLAC__bool local__parse_timecode_(const char *s, double *value)
53 /* parse [0-9][0-9]*: */
55 if(c >= '0' && c <= '9')
59 while(':' != (c = *s++)) {
60 if(c >= '0' && c <= '9')
61 i = i * 10 + (c - '0');
65 ret = (double)i * 60.;
67 /* parse [0-9]*[.]?[0-9]* i.e. a sign-less rational number */
68 if(strspn(s, "1234567890.") != strlen(s))
71 const char *p = strchr(s, '.');
72 if(p && 0 != strchr(++p, '.'))
81 static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *track, unsigned *index)
83 FLAC__bool got_track = false, got_index = false;
84 unsigned t = 0, i = 0;
87 while(end? s < end : *s != '\0') {
89 if(c >= '0' && c <= '9') {
90 t = t * 10 + (c - '0');
98 while(end? s < end : *s != '\0') {
100 if(c >= '0' && c <= '9') {
101 i = i * 10 + (c - '0');
109 return got_track && got_index;
113 * @@@ this only works with sorted cuesheets (the spec strongly recommends but
114 * does not require sorted cuesheets). but if it's not sorted, picking a
115 * nearest cue point has no significance.
117 static FLAC__uint64 local__find_closest_cue_(const FLAC__StreamMetadata_CueSheet *cuesheet, unsigned track, unsigned index, FLAC__uint64 total_samples, FLAC__bool look_forward)
121 for(t = 0; t < (int)cuesheet->num_tracks; t++)
122 for(i = 0; i < (int)cuesheet->tracks[t].num_indices; i++)
123 if(cuesheet->tracks[t].number > track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number >= index))
124 return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset;
125 return total_samples;
128 for(t = (int)cuesheet->num_tracks - 1; t >= 0; t--)
129 for(i = (int)cuesheet->tracks[t].num_indices - 1; i >= 0; i--)
130 if(cuesheet->tracks[t].number < track || (cuesheet->tracks[t].number == track && cuesheet->tracks[t].indices[i].number <= index))
131 return cuesheet->tracks[t].offset + cuesheet->tracks[t].indices[i].offset;
136 #ifdef FLAC__VALGRIND_TESTING
137 size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
139 size_t ret = fwrite(ptr, size, nmemb, stream);
146 FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec)
149 FLAC__bool is_negative = false;
151 FLAC__ASSERT(0 != spec);
153 spec->is_relative = false;
154 spec->value_is_samples = true;
155 spec->value.samples = 0;
160 spec->is_relative = true;
163 else if(s[0] == '+') {
164 spec->is_relative = true;
168 if(local__parse_uint64_(s, &val)) {
169 spec->value_is_samples = true;
170 spec->value.samples = (FLAC__int64)val;
172 spec->value.samples = -(spec->value.samples);
176 if(!local__parse_timecode_(s, &d))
178 spec->value_is_samples = false;
179 spec->value.seconds = d;
181 spec->value.seconds = -(spec->value.seconds);
188 void flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification *spec, unsigned sample_rate)
190 FLAC__ASSERT(0 != spec);
191 if(!spec->value_is_samples) {
192 spec->value.samples = (FLAC__int64)(spec->value.seconds * (double)sample_rate);
193 spec->value_is_samples = true;
197 FLAC__bool flac__utils_parse_cue_specification(const char *s, utils__CueSpecification *spec)
199 const char *start = s, *end = 0;
201 FLAC__ASSERT(0 != spec);
203 spec->has_start_point = spec->has_end_point = false;
216 if(!local__parse_cue_(start, s, &spec->start_track, &spec->start_index))
218 spec->has_start_point = true;
222 if(!local__parse_cue_(end, 0, &spec->end_track, &spec->end_index))
224 spec->has_end_point = true;
230 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)
232 FLAC__ASSERT(0 != cue_spec);
233 FLAC__ASSERT(0 != cuesheet);
234 FLAC__ASSERT(0 != total_samples);
235 FLAC__ASSERT(0 != skip_spec);
236 FLAC__ASSERT(0 != until_spec);
238 skip_spec->is_relative = false;
239 skip_spec->value_is_samples = true;
241 until_spec->is_relative = false;
242 until_spec->value_is_samples = true;
244 if(cue_spec->has_start_point)
245 skip_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->start_track, cue_spec->start_index, total_samples, /*look_forward=*/false);
247 skip_spec->value.samples = 0;
249 if(cue_spec->has_end_point)
250 until_spec->value.samples = local__find_closest_cue_(cuesheet, cue_spec->end_track, cue_spec->end_index, total_samples, /*look_forward=*/true);
252 until_spec->value.samples = total_samples;