add --cue option to flac, and tests and documentation
[platform/upstream/flac.git] / src / flac / utils.c
1 /* flac - Command-line FLAC encoder/decoder
2  * Copyright (C) 2002,2003,2004  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
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.
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "utils.h"
24 #include "FLAC/assert.h"
25 #include <math.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 static FLAC__bool local__parse_uint64_(const char *s, FLAC__uint64 *value)
30 {
31         FLAC__uint64 ret = 0;
32         char c;
33
34         if(*s == '\0')
35                 return false;
36
37         while('\0' != (c = *s++))
38                 if(c >= '0' && c <= '9')
39                         ret = ret * 10 + (c - '0');
40                 else
41                         return false;
42
43         *value = ret;
44         return true;
45 }
46
47 static FLAC__bool local__parse_timecode_(const char *s, double *value)
48 {
49         double ret;
50         unsigned i;
51         char c;
52
53         /* parse [0-9][0-9]*: */
54         c = *s++;
55         if(c >= '0' && c <= '9')
56                 i = (c - '0');
57         else
58                 return false;
59         while(':' != (c = *s++)) {
60                 if(c >= '0' && c <= '9')
61                         i = i * 10 + (c - '0');
62                 else
63                         return false;
64         }
65         ret = (double)i * 60.;
66
67         /* parse [0-9]*[.]?[0-9]* i.e. a sign-less rational number */
68         if(strspn(s, "1234567890.") != strlen(s))
69                 return false;
70         {
71                 const char *p = strchr(s, '.');
72                 if(p && 0 != strchr(++p, '.'))
73                         return false;
74         }
75         ret += atof(s);
76
77         *value = ret;
78         return true;
79 }
80
81 static FLAC__bool local__parse_cue_(const char *s, const char *end, unsigned *track, unsigned *index)
82 {
83         FLAC__bool got_track = false, got_index = false;
84         unsigned t = 0, i = 0;
85         char c;
86
87         while(end? s < end : *s != '\0') {
88                 c = *s++;
89                 if(c >= '0' && c <= '9') {
90                         t = t * 10 + (c - '0');
91                         got_track = true;
92                 }
93                 else if(c == '.')
94                         break;
95                 else
96                         return false;
97         }
98         while(end? s < end : *s != '\0') {
99                 c = *s++;
100                 if(c >= '0' && c <= '9') {
101                         i = i * 10 + (c - '0');
102                         got_index = true;
103                 }
104                 else
105                         return false;
106         }
107         *track = t;
108         *index = i;
109         return got_track && got_index;
110 }
111
112 /*
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.
116  */
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)
118 {
119         int t, i;
120         if(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;
126         }
127         else {
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;
132                 return 0;
133         }
134 }
135
136 #ifdef FLAC__VALGRIND_TESTING
137 size_t flac__utils_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
138 {
139         size_t ret = fwrite(ptr, size, nmemb, stream);
140         if(!ferror(stream))
141                 fflush(stream);
142         return ret;
143 }
144 #endif
145
146 FLAC__bool flac__utils_parse_skip_until_specification(const char *s, utils__SkipUntilSpecification *spec)
147 {
148         FLAC__uint64 val;
149         FLAC__bool is_negative = false;
150
151         FLAC__ASSERT(0 != spec);
152
153         spec->is_relative = false;
154         spec->value_is_samples = true;
155         spec->value.samples = 0;
156
157         if(0 != s) {
158                 if(s[0] == '-') {
159                         is_negative = true;
160                         spec->is_relative = true;
161                         s++;
162                 }
163                 else if(s[0] == '+') {
164                         spec->is_relative = true;
165                         s++;
166                 }
167
168                 if(local__parse_uint64_(s, &val)) {
169                         spec->value_is_samples = true;
170                         spec->value.samples = (FLAC__int64)val;
171                         if(is_negative)
172                                 spec->value.samples = -(spec->value.samples);
173                 }
174                 else {
175                         double d;
176                         if(!local__parse_timecode_(s, &d))
177                                 return false;
178                         spec->value_is_samples = false;
179                         spec->value.seconds = d;
180                         if(is_negative)
181                                 spec->value.seconds = -(spec->value.seconds);
182                 }
183         }
184
185         return true;
186 }
187
188 void flac__utils_canonicalize_skip_until_specification(utils__SkipUntilSpecification *spec, unsigned sample_rate)
189 {
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;
194         }
195 }
196
197 FLAC__bool flac__utils_parse_cue_specification(const char *s, utils__CueSpecification *spec)
198 {
199         const char *start = s, *end = 0;
200
201         FLAC__ASSERT(0 != spec);
202
203         spec->has_start_point = spec->has_end_point = false;
204
205         s = strchr(s, '-');
206
207         if(0 != s) {
208                 if(s == start)
209                         start = 0;
210                 end = s+1;
211                 if(*end == '\0')
212                         end = 0;
213         }
214
215         if(start) {
216                 if(!local__parse_cue_(start, s, &spec->start_track, &spec->start_index))
217                         return false;
218                 spec->has_start_point = true;
219         }
220
221         if(end) {
222                 if(!local__parse_cue_(end, 0, &spec->end_track, &spec->end_index))
223                         return false;
224                 spec->has_end_point = true;
225         }
226
227         return true;
228 }
229
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)
231 {
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);
237
238         skip_spec->is_relative = false;
239         skip_spec->value_is_samples = true;
240
241         until_spec->is_relative = false;
242         until_spec->value_is_samples = true;
243
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);
246         else
247                 skip_spec->value.samples = 0;
248
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);
251         else
252                 until_spec->value.samples = total_samples;
253 }