fix another possible seeking bug
[flac.git] / src / libFLAC / seekable_stream_decoder.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002,2003,2004  Josh Coalson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * - Neither the name of the Xiph.org Foundation nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <stdio.h>
33 #include <stdlib.h> /* for calloc() */
34 #include <string.h> /* for memcpy()/memcmp() */
35 #include "FLAC/assert.h"
36 #include "protected/seekable_stream_decoder.h"
37 #include "protected/stream_decoder.h"
38 #include "private/md5.h"
39
40 /***********************************************************************
41  *
42  * Private class method prototypes
43  *
44  ***********************************************************************/
45
46 static void set_defaults_(FLAC__SeekableStreamDecoder *decoder);
47 static FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
48 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
49 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
50 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
51 static FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
52
53 /***********************************************************************
54  *
55  * Private class data
56  *
57  ***********************************************************************/
58
59 typedef struct FLAC__SeekableStreamDecoderPrivate {
60         FLAC__SeekableStreamDecoderReadCallback read_callback;
61         FLAC__SeekableStreamDecoderSeekCallback seek_callback;
62         FLAC__SeekableStreamDecoderTellCallback tell_callback;
63         FLAC__SeekableStreamDecoderLengthCallback length_callback;
64         FLAC__SeekableStreamDecoderEofCallback eof_callback;
65         FLAC__SeekableStreamDecoderWriteCallback write_callback;
66         FLAC__SeekableStreamDecoderMetadataCallback metadata_callback;
67         FLAC__SeekableStreamDecoderErrorCallback error_callback;
68         void *client_data;
69         FLAC__StreamDecoder *stream_decoder;
70         FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek */
71         struct MD5Context md5context;
72         FLAC__byte stored_md5sum[16]; /* this is what is stored in the metadata */
73         FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
74         /* the rest of these are only used for seeking: */
75         FLAC__StreamMetadata_StreamInfo stream_info; /* we keep this around so we can figure out how to seek quickly */
76         const FLAC__StreamMetadata_SeekTable *seek_table; /* we hold a pointer to the stream decoder's seek table for the same reason */
77         /* Since we always want to see the STREAMINFO and SEEK_TABLE blocks at this level, we need some extra flags to keep track of whether they should be passed on up through the metadata_callback */
78         FLAC__bool ignore_stream_info_block;
79         FLAC__bool ignore_seek_table_block;
80         FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
81         FLAC__uint64 target_sample;
82 } FLAC__SeekableStreamDecoderPrivate;
83
84 /***********************************************************************
85  *
86  * Public static class data
87  *
88  ***********************************************************************/
89
90 FLAC_API const char * const FLAC__SeekableStreamDecoderStateString[] = {
91         "FLAC__SEEKABLE_STREAM_DECODER_OK",
92         "FLAC__SEEKABLE_STREAM_DECODER_SEEKING",
93         "FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM",
94         "FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
95         "FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR",
96         "FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR",
97         "FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR",
98         "FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED",
99         "FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK",
100         "FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED"
101 };
102
103 FLAC_API const char * const FLAC__SeekableStreamDecoderReadStatusString[] = {
104         "FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK",
105         "FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR"
106 };
107
108 FLAC_API const char * const FLAC__SeekableStreamDecoderSeekStatusString[] = {
109         "FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK",
110         "FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR"
111 };
112
113 FLAC_API const char * const FLAC__SeekableStreamDecoderTellStatusString[] = {
114         "FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK",
115         "FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR"
116 };
117
118 FLAC_API const char * const FLAC__SeekableStreamDecoderLengthStatusString[] = {
119         "FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK",
120         "FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR"
121 };
122
123
124 /***********************************************************************
125  *
126  * Class constructor/destructor
127  *
128  ***********************************************************************/
129
130 FLAC_API FLAC__SeekableStreamDecoder *FLAC__seekable_stream_decoder_new()
131 {
132         FLAC__SeekableStreamDecoder *decoder;
133
134         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
135
136         decoder = (FLAC__SeekableStreamDecoder*)calloc(1, sizeof(FLAC__SeekableStreamDecoder));
137         if(decoder == 0) {
138                 return 0;
139         }
140
141         decoder->protected_ = (FLAC__SeekableStreamDecoderProtected*)calloc(1, sizeof(FLAC__SeekableStreamDecoderProtected));
142         if(decoder->protected_ == 0) {
143                 free(decoder);
144                 return 0;
145         }
146
147         decoder->private_ = (FLAC__SeekableStreamDecoderPrivate*)calloc(1, sizeof(FLAC__SeekableStreamDecoderPrivate));
148         if(decoder->private_ == 0) {
149                 free(decoder->protected_);
150                 free(decoder);
151                 return 0;
152         }
153
154         decoder->private_->stream_decoder = FLAC__stream_decoder_new();
155         if(0 == decoder->private_->stream_decoder) {
156                 free(decoder->private_);
157                 free(decoder->protected_);
158                 free(decoder);
159                 return 0;
160         }
161
162         set_defaults_(decoder);
163
164         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
165
166         return decoder;
167 }
168
169 FLAC_API void FLAC__seekable_stream_decoder_delete(FLAC__SeekableStreamDecoder *decoder)
170 {
171         FLAC__ASSERT(0 != decoder);
172         FLAC__ASSERT(0 != decoder->protected_);
173         FLAC__ASSERT(0 != decoder->private_);
174         FLAC__ASSERT(0 != decoder->private_->stream_decoder);
175
176         (void)FLAC__seekable_stream_decoder_finish(decoder);
177
178         FLAC__stream_decoder_delete(decoder->private_->stream_decoder);
179
180         free(decoder->private_);
181         free(decoder->protected_);
182         free(decoder);
183 }
184
185 /***********************************************************************
186  *
187  * Public class methods
188  *
189  ***********************************************************************/
190
191 FLAC_API FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_init(FLAC__SeekableStreamDecoder *decoder)
192 {
193         FLAC__ASSERT(0 != decoder);
194
195         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
196                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED;
197
198         if(0 == decoder->private_->read_callback || 0 == decoder->private_->seek_callback || 0 == decoder->private_->tell_callback || 0 == decoder->private_->length_callback || 0 == decoder->private_->eof_callback)
199                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
200
201         if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
202                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
203
204         decoder->private_->seek_table = 0;
205
206         decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
207
208         /* We initialize the MD5Context even though we may never use it.  This is
209          * because md5 checking may be turned on to start and then turned off if a
210          * seek occurs.  So we always init the context here and finalize it in
211          * FLAC__seekable_stream_decoder_finish() to make sure things are always
212          * cleaned up properly.
213          */
214         MD5Init(&decoder->private_->md5context);
215
216         FLAC__stream_decoder_set_read_callback(decoder->private_->stream_decoder, read_callback_);
217         FLAC__stream_decoder_set_write_callback(decoder->private_->stream_decoder, write_callback_);
218         FLAC__stream_decoder_set_metadata_callback(decoder->private_->stream_decoder, metadata_callback_);
219         FLAC__stream_decoder_set_error_callback(decoder->private_->stream_decoder, error_callback_);
220         FLAC__stream_decoder_set_client_data(decoder->private_->stream_decoder, decoder);
221
222         /* We always want to see these blocks.  Whether or not we pass them up
223          * through the metadata callback will be determined by flags set in our
224          * implementation of ..._set_metadata_respond/ignore...()
225          */
226         FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, FLAC__METADATA_TYPE_STREAMINFO);
227         FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, FLAC__METADATA_TYPE_SEEKTABLE);
228
229         if(FLAC__stream_decoder_init(decoder->private_->stream_decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
230                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
231
232         return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
233 }
234
235 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_finish(FLAC__SeekableStreamDecoder *decoder)
236 {
237         FLAC__bool md5_failed = false;
238
239         FLAC__ASSERT(0 != decoder);
240         FLAC__ASSERT(0 != decoder->private_);
241         FLAC__ASSERT(0 != decoder->protected_);
242
243         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
244                 return true;
245
246         FLAC__ASSERT(0 != decoder->private_->stream_decoder);
247
248         /* see the comment in FLAC__seekable_stream_decoder_init() as to why we
249          * always call MD5Final()
250          */
251         MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
252
253         FLAC__stream_decoder_finish(decoder->private_->stream_decoder);
254
255         if(decoder->private_->do_md5_checking) {
256                 if(memcmp(decoder->private_->stored_md5sum, decoder->private_->computed_md5sum, 16))
257                         md5_failed = true;
258         }
259
260         set_defaults_(decoder);
261
262         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
263
264         return !md5_failed;
265 }
266
267 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_md5_checking(FLAC__SeekableStreamDecoder *decoder, FLAC__bool value)
268 {
269         FLAC__ASSERT(0 != decoder);
270         FLAC__ASSERT(0 != decoder->protected_);
271         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
272                 return false;
273         decoder->protected_->md5_checking = value;
274         return true;
275 }
276
277 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_read_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderReadCallback value)
278 {
279         FLAC__ASSERT(0 != decoder);
280         FLAC__ASSERT(0 != decoder->private_);
281         FLAC__ASSERT(0 != decoder->protected_);
282         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
283                 return false;
284         decoder->private_->read_callback = value;
285         return true;
286 }
287
288 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_seek_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderSeekCallback value)
289 {
290         FLAC__ASSERT(0 != decoder);
291         FLAC__ASSERT(0 != decoder->private_);
292         FLAC__ASSERT(0 != decoder->protected_);
293         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
294                 return false;
295         decoder->private_->seek_callback = value;
296         return true;
297 }
298
299 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_tell_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderTellCallback value)
300 {
301         FLAC__ASSERT(0 != decoder);
302         FLAC__ASSERT(0 != decoder->private_);
303         FLAC__ASSERT(0 != decoder->protected_);
304         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
305                 return false;
306         decoder->private_->tell_callback = value;
307         return true;
308 }
309
310 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_length_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderLengthCallback value)
311 {
312         FLAC__ASSERT(0 != decoder);
313         FLAC__ASSERT(0 != decoder->private_);
314         FLAC__ASSERT(0 != decoder->protected_);
315         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
316                 return false;
317         decoder->private_->length_callback = value;
318         return true;
319 }
320
321 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_eof_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderEofCallback value)
322 {
323         FLAC__ASSERT(0 != decoder);
324         FLAC__ASSERT(0 != decoder->private_);
325         FLAC__ASSERT(0 != decoder->protected_);
326         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
327                 return false;
328         decoder->private_->eof_callback = value;
329         return true;
330 }
331
332 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_write_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderWriteCallback value)
333 {
334         FLAC__ASSERT(0 != decoder);
335         FLAC__ASSERT(0 != decoder->private_);
336         FLAC__ASSERT(0 != decoder->protected_);
337         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
338                 return false;
339         decoder->private_->write_callback = value;
340         return true;
341 }
342
343 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderMetadataCallback value)
344 {
345         FLAC__ASSERT(0 != decoder);
346         FLAC__ASSERT(0 != decoder->private_);
347         FLAC__ASSERT(0 != decoder->protected_);
348         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
349                 return false;
350         decoder->private_->metadata_callback = value;
351         return true;
352 }
353
354 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_error_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderErrorCallback value)
355 {
356         FLAC__ASSERT(0 != decoder);
357         FLAC__ASSERT(0 != decoder->private_);
358         FLAC__ASSERT(0 != decoder->protected_);
359         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
360                 return false;
361         decoder->private_->error_callback = value;
362         return true;
363 }
364
365 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_client_data(FLAC__SeekableStreamDecoder *decoder, void *value)
366 {
367         FLAC__ASSERT(0 != decoder);
368         FLAC__ASSERT(0 != decoder->private_);
369         FLAC__ASSERT(0 != decoder->protected_);
370         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
371                 return false;
372         decoder->private_->client_data = value;
373         return true;
374 }
375
376 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_respond(FLAC__SeekableStreamDecoder *decoder, FLAC__MetadataType type)
377 {
378         FLAC__ASSERT(0 != decoder);
379         FLAC__ASSERT(0 != decoder->private_);
380         FLAC__ASSERT(0 != decoder->protected_);
381         FLAC__ASSERT(0 != decoder->private_->stream_decoder);
382         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
383                 return false;
384         if(type == FLAC__METADATA_TYPE_STREAMINFO)
385                 decoder->private_->ignore_stream_info_block = false;
386         else if(type == FLAC__METADATA_TYPE_SEEKTABLE)
387                 decoder->private_->ignore_seek_table_block = false;
388         return FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, type);
389 }
390
391 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_respond_application(FLAC__SeekableStreamDecoder *decoder, const FLAC__byte id[4])
392 {
393         FLAC__ASSERT(0 != decoder);
394         FLAC__ASSERT(0 != decoder->private_);
395         FLAC__ASSERT(0 != decoder->protected_);
396         FLAC__ASSERT(0 != decoder->private_->stream_decoder);
397         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
398                 return false;
399         return FLAC__stream_decoder_set_metadata_respond_application(decoder->private_->stream_decoder, id);
400 }
401
402 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_respond_all(FLAC__SeekableStreamDecoder *decoder)
403 {
404         FLAC__ASSERT(0 != decoder);
405         FLAC__ASSERT(0 != decoder->private_);
406         FLAC__ASSERT(0 != decoder->protected_);
407         FLAC__ASSERT(0 != decoder->private_->stream_decoder);
408         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
409                 return false;
410         decoder->private_->ignore_stream_info_block = false;
411         decoder->private_->ignore_seek_table_block = false;
412         return FLAC__stream_decoder_set_metadata_respond_all(decoder->private_->stream_decoder);
413 }
414
415 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_ignore(FLAC__SeekableStreamDecoder *decoder, FLAC__MetadataType type)
416 {
417         FLAC__ASSERT(0 != decoder);
418         FLAC__ASSERT(0 != decoder->private_);
419         FLAC__ASSERT(0 != decoder->protected_);
420         FLAC__ASSERT(0 != decoder->private_->stream_decoder);
421         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
422                 return false;
423         if(type == FLAC__METADATA_TYPE_STREAMINFO) {
424                 decoder->private_->ignore_stream_info_block = true;
425                 return true;
426         }
427         else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
428                 decoder->private_->ignore_seek_table_block = true;
429                 return true;
430         }
431         else
432                 return FLAC__stream_decoder_set_metadata_ignore(decoder->private_->stream_decoder, type);
433 }
434
435 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_ignore_application(FLAC__SeekableStreamDecoder *decoder, const FLAC__byte id[4])
436 {
437         FLAC__ASSERT(0 != decoder);
438         FLAC__ASSERT(0 != decoder->private_);
439         FLAC__ASSERT(0 != decoder->protected_);
440         FLAC__ASSERT(0 != decoder->private_->stream_decoder);
441         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
442                 return false;
443         return FLAC__stream_decoder_set_metadata_ignore_application(decoder->private_->stream_decoder, id);
444 }
445
446 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_set_metadata_ignore_all(FLAC__SeekableStreamDecoder *decoder)
447 {
448         FLAC__ASSERT(0 != decoder);
449         FLAC__ASSERT(0 != decoder->private_);
450         FLAC__ASSERT(0 != decoder->protected_);
451         FLAC__ASSERT(0 != decoder->private_->stream_decoder);
452         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
453                 return false;
454         decoder->private_->ignore_stream_info_block = true;
455         decoder->private_->ignore_seek_table_block = true;
456         return
457                 FLAC__stream_decoder_set_metadata_ignore_all(decoder->private_->stream_decoder) &&
458                 FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, FLAC__METADATA_TYPE_STREAMINFO) &&
459                 FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, FLAC__METADATA_TYPE_SEEKTABLE);
460 }
461
462 FLAC_API FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_get_state(const FLAC__SeekableStreamDecoder *decoder)
463 {
464         FLAC__ASSERT(0 != decoder);
465         FLAC__ASSERT(0 != decoder->protected_);
466         return decoder->protected_->state;
467 }
468
469 FLAC_API FLAC__StreamDecoderState FLAC__seekable_stream_decoder_get_stream_decoder_state(const FLAC__SeekableStreamDecoder *decoder)
470 {
471         FLAC__ASSERT(0 != decoder);
472         FLAC__ASSERT(0 != decoder->private_);
473         return FLAC__stream_decoder_get_state(decoder->private_->stream_decoder);
474 }
475
476 FLAC_API const char *FLAC__seekable_stream_decoder_get_resolved_state_string(const FLAC__SeekableStreamDecoder *decoder)
477 {
478         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR)
479                 return FLAC__SeekableStreamDecoderStateString[decoder->protected_->state];
480         else
481                 return FLAC__stream_decoder_get_resolved_state_string(decoder->private_->stream_decoder);
482 }
483
484 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_get_md5_checking(const FLAC__SeekableStreamDecoder *decoder)
485 {
486         FLAC__ASSERT(0 != decoder);
487         FLAC__ASSERT(0 != decoder->protected_);
488         return decoder->protected_->md5_checking;
489 }
490
491 FLAC_API unsigned FLAC__seekable_stream_decoder_get_channels(const FLAC__SeekableStreamDecoder *decoder)
492 {
493         FLAC__ASSERT(0 != decoder);
494         FLAC__ASSERT(0 != decoder->private_);
495         return FLAC__stream_decoder_get_channels(decoder->private_->stream_decoder);
496 }
497
498 FLAC_API FLAC__ChannelAssignment FLAC__seekable_stream_decoder_get_channel_assignment(const FLAC__SeekableStreamDecoder *decoder)
499 {
500         FLAC__ASSERT(0 != decoder);
501         FLAC__ASSERT(0 != decoder->private_);
502         return FLAC__stream_decoder_get_channel_assignment(decoder->private_->stream_decoder);
503 }
504
505 FLAC_API unsigned FLAC__seekable_stream_decoder_get_bits_per_sample(const FLAC__SeekableStreamDecoder *decoder)
506 {
507         FLAC__ASSERT(0 != decoder);
508         FLAC__ASSERT(0 != decoder->private_);
509         return FLAC__stream_decoder_get_bits_per_sample(decoder->private_->stream_decoder);
510 }
511
512 FLAC_API unsigned FLAC__seekable_stream_decoder_get_sample_rate(const FLAC__SeekableStreamDecoder *decoder)
513 {
514         FLAC__ASSERT(0 != decoder);
515         FLAC__ASSERT(0 != decoder->private_);
516         return FLAC__stream_decoder_get_sample_rate(decoder->private_->stream_decoder);
517 }
518
519 FLAC_API unsigned FLAC__seekable_stream_decoder_get_blocksize(const FLAC__SeekableStreamDecoder *decoder)
520 {
521         FLAC__ASSERT(0 != decoder);
522         FLAC__ASSERT(0 != decoder->private_);
523         return FLAC__stream_decoder_get_blocksize(decoder->private_->stream_decoder);
524 }
525
526 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_get_decode_position(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *position)
527 {
528         FLAC__ASSERT(0 != decoder);
529         FLAC__ASSERT(0 != decoder->private_);
530         FLAC__ASSERT(0 != position);
531
532         if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK)
533                 return false;
534         FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder));
535         *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);
536         return true;
537 }
538
539 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_flush(FLAC__SeekableStreamDecoder *decoder)
540 {
541         FLAC__ASSERT(0 != decoder);
542         FLAC__ASSERT(0 != decoder->private_);
543         FLAC__ASSERT(0 != decoder->protected_);
544
545         decoder->private_->do_md5_checking = false;
546
547         if(!FLAC__stream_decoder_flush(decoder->private_->stream_decoder)) {
548                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
549                 return false;
550         }
551
552         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
553
554         return true;
555 }
556
557 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_reset(FLAC__SeekableStreamDecoder *decoder)
558 {
559         FLAC__ASSERT(0 != decoder);
560         FLAC__ASSERT(0 != decoder->private_);
561         FLAC__ASSERT(0 != decoder->protected_);
562
563         if(!FLAC__seekable_stream_decoder_flush(decoder)) {
564                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
565                 return false;
566         }
567
568         if(!FLAC__stream_decoder_reset(decoder->private_->stream_decoder)) {
569                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
570                 return false;
571         }
572
573         decoder->private_->seek_table = 0;
574
575         decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
576
577         /* We initialize the MD5Context even though we may never use it.  This is
578          * because md5 checking may be turned on to start and then turned off if a
579          * seek occurs.  So we always init the context here and finalize it in
580          * FLAC__seekable_stream_decoder_finish() to make sure things are always
581          * cleaned up properly.
582          */
583         MD5Init(&decoder->private_->md5context);
584
585         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
586
587         return true;
588 }
589
590 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_process_single(FLAC__SeekableStreamDecoder *decoder)
591 {
592         FLAC__bool ret;
593         FLAC__ASSERT(0 != decoder);
594
595         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
596                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
597
598         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
599                 return true;
600
601         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
602
603         ret = FLAC__stream_decoder_process_single(decoder->private_->stream_decoder);
604         if(!ret)
605                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
606
607         return ret;
608 }
609
610 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_process_until_end_of_metadata(FLAC__SeekableStreamDecoder *decoder)
611 {
612         FLAC__bool ret;
613         FLAC__ASSERT(0 != decoder);
614
615         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
616                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
617
618         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
619                 return true;
620
621         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
622
623         ret = FLAC__stream_decoder_process_until_end_of_metadata(decoder->private_->stream_decoder);
624         if(!ret)
625                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
626
627         return ret;
628 }
629
630 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_process_until_end_of_stream(FLAC__SeekableStreamDecoder *decoder)
631 {
632         FLAC__bool ret;
633         FLAC__ASSERT(0 != decoder);
634
635         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
636                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
637
638         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
639                 return true;
640
641         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
642
643         ret = FLAC__stream_decoder_process_until_end_of_stream(decoder->private_->stream_decoder);
644         if(!ret)
645                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
646
647         return ret;
648 }
649
650 FLAC_API FLAC__bool FLAC__seekable_stream_decoder_seek_absolute(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 sample)
651 {
652         FLAC__uint64 length;
653
654         FLAC__ASSERT(0 != decoder);
655         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK || decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM);
656
657         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEKING;
658
659         /* turn off md5 checking if a seek is attempted */
660         decoder->private_->do_md5_checking = false;
661
662         if(!FLAC__stream_decoder_reset(decoder->private_->stream_decoder)) {
663                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
664                 return false;
665         }
666         /* get the file length */
667         if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK) {
668                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
669                 return false;
670         }
671         /* rewind */
672         if(decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {
673                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
674                 return false;
675         }
676         if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder->private_->stream_decoder)) {
677                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
678                 return false;
679         }
680         if(decoder->private_->stream_info.total_samples > 0 && sample >= decoder->private_->stream_info.total_samples) {
681                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
682                 return false;
683         }
684
685         return seek_to_absolute_sample_(decoder, length, sample);
686 }
687
688 /***********************************************************************
689  *
690  * Private class methods
691  *
692  ***********************************************************************/
693
694 void set_defaults_(FLAC__SeekableStreamDecoder *decoder)
695 {
696         decoder->private_->read_callback = 0;
697         decoder->private_->seek_callback = 0;
698         decoder->private_->tell_callback = 0;
699         decoder->private_->length_callback = 0;
700         decoder->private_->eof_callback = 0;
701         decoder->private_->write_callback = 0;
702         decoder->private_->metadata_callback = 0;
703         decoder->private_->error_callback = 0;
704         decoder->private_->client_data = 0;
705         /* WATCHOUT: these should match the default behavior of FLAC__StreamDecoder */
706         decoder->private_->ignore_stream_info_block = false;
707         decoder->private_->ignore_seek_table_block = true;
708
709         decoder->protected_->md5_checking = false;
710 }
711
712 FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
713 {
714         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
715         (void)decoder;
716         if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {
717                 *bytes = 0;
718 #if 0
719 @@@@@@ verify that this is not needed
720                 seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
721 #endif
722                 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
723         }
724         else if(*bytes > 0) {
725                 if(seekable_stream_decoder->private_->read_callback(seekable_stream_decoder, buffer, bytes, seekable_stream_decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK) {
726                         seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR;
727                         return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
728                 }
729                 if(*bytes == 0) {
730                         if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {
731 #if 0
732 @@@@@@ verify that this is not needed
733                                 seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
734 #endif
735                                 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
736                         }
737                         else
738                                 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
739                 }
740                 else {
741                         return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
742                 }
743         }
744         else
745                 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
746 }
747
748 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
749 {
750         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
751         (void)decoder;
752
753         if(seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {
754                 FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
755                 FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
756                 FLAC__uint64 target_sample = seekable_stream_decoder->private_->target_sample;
757
758                 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
759
760                 seekable_stream_decoder->private_->last_frame = *frame; /* save the frame */
761                 if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
762                         unsigned delta = (unsigned)(target_sample - this_frame_sample);
763                         /* kick out of seek mode */
764                         seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
765                         /* shift out the samples before target_sample */
766                         if(delta > 0) {
767                                 unsigned channel;
768                                 const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
769                                 for(channel = 0; channel < frame->header.channels; channel++)
770                                         newbuffer[channel] = buffer[channel] + delta;
771                                 seekable_stream_decoder->private_->last_frame.header.blocksize -= delta;
772                                 seekable_stream_decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
773                                 /* write the relevant samples */
774                                 return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, &seekable_stream_decoder->private_->last_frame, newbuffer, seekable_stream_decoder->private_->client_data);
775                         }
776                         else {
777                                 /* write the relevant samples */
778                                 return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);
779                         }
780                 }
781                 else {
782                         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
783                 }
784         }
785         else {
786                 if(seekable_stream_decoder->private_->do_md5_checking) {
787                         if(!FLAC__MD5Accumulate(&seekable_stream_decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
788                                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
789                 }
790                 return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);
791         }
792 }
793
794 void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
795 {
796         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
797         (void)decoder;
798
799         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
800                 seekable_stream_decoder->private_->stream_info = metadata->data.stream_info;
801                 /* save the MD5 signature for comparison later */
802                 memcpy(seekable_stream_decoder->private_->stored_md5sum, metadata->data.stream_info.md5sum, 16);
803                 if(0 == memcmp(seekable_stream_decoder->private_->stored_md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
804                         seekable_stream_decoder->private_->do_md5_checking = false;
805         }
806         else if(metadata->type == FLAC__METADATA_TYPE_SEEKTABLE) {
807                 seekable_stream_decoder->private_->seek_table = &metadata->data.seek_table;
808         }
809
810         if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {
811                 FLAC__bool ignore_block = false;
812                 if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO && seekable_stream_decoder->private_->ignore_stream_info_block)
813                         ignore_block = true;
814                 else if(metadata->type == FLAC__METADATA_TYPE_SEEKTABLE && seekable_stream_decoder->private_->ignore_seek_table_block)
815                         ignore_block = true;
816                 if(!ignore_block)
817                         seekable_stream_decoder->private_->metadata_callback(seekable_stream_decoder, metadata, seekable_stream_decoder->private_->client_data);
818         }
819 }
820
821 void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
822 {
823         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
824         (void)decoder;
825
826         if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING)
827                 seekable_stream_decoder->private_->error_callback(seekable_stream_decoder, status, seekable_stream_decoder->private_->client_data);
828 }
829
830 FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
831 {
832         FLAC__uint64 first_frame_offset, lower_bound, upper_bound;
833         FLAC__int64 pos = -1, last_pos = -1;
834         int i, lower_seek_point = -1, upper_seek_point = -1;
835         unsigned approx_bytes_per_frame;
836         FLAC__uint64 last_frame_sample = 0xffffffffffffffff;
837         FLAC__bool needs_seek;
838         const FLAC__uint64 total_samples = decoder->private_->stream_info.total_samples;
839         const unsigned min_blocksize = decoder->private_->stream_info.min_blocksize;
840         const unsigned max_blocksize = decoder->private_->stream_info.max_blocksize;
841         const unsigned max_framesize = decoder->private_->stream_info.max_framesize;
842         const unsigned channels = FLAC__seekable_stream_decoder_get_channels(decoder);
843         const unsigned bps = FLAC__seekable_stream_decoder_get_bits_per_sample(decoder);
844
845         /* we are just guessing here, but we want to guess high, not low */
846         if(max_framesize > 0) {
847                 approx_bytes_per_frame = max_framesize;
848         }
849         /*
850          * Check if it's a known fixed-blocksize stream.  Note that though
851          * the spec doesn't allow zeroes in the STREAMINFO block, we may
852          * never get a STREAMINFO block when decoding so the value of
853          * min_blocksize might be zero.
854          */
855         else if(min_blocksize == max_blocksize && min_blocksize > 0) {
856                 /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */
857                 approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
858         }
859         else
860                 approx_bytes_per_frame = 4608 * channels * bps/8 + 64;
861
862         /*
863          * The decode position is currently at the first frame since we
864          * rewound and processed metadata.
865          */
866         if(!FLAC__seekable_stream_decoder_get_decode_position(decoder, &first_frame_offset)) {
867                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
868                 return false;
869         }
870
871         /*
872          * First, we set an upper and lower bound on where in the
873          * stream we will search.  For now we assume the worst case
874          * scenario, which is our best guess at the beginning of
875          * the first and last frames.
876          */
877         lower_bound = first_frame_offset;
878
879         /* calc the upper_bound, beyond which we never want to seek */
880         if(max_framesize > 0)
881                 upper_bound = stream_length - (max_framesize + 128 + 2); /* 128 for a possible ID3V1 tag, 2 for indexing differences */
882         else
883                 upper_bound = stream_length - ((channels * bps * FLAC__MAX_BLOCK_SIZE) / 8 + 128 + 2);
884
885         /*
886          * Now we refine the bounds if we have a seektable with
887          * suitable points.  Note that according to the spec they
888          * must be ordered by ascending sample number.
889          */
890         if(0 != decoder->private_->seek_table) {
891                 /* find the closest seek point <= target_sample, if it exists */
892                 for(i = (int)decoder->private_->seek_table->num_points - 1; i >= 0; i--) {
893                         if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number <= target_sample)
894                                 break;
895                 }
896                 if(i >= 0) { /* i.e. we found a suitable seek point... */
897                         lower_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;
898                         lower_seek_point = i;
899                 }
900
901                 /* find the closest seek point > target_sample, if it exists */
902                 for(i = 0; i < (int)decoder->private_->seek_table->num_points; i++) {
903                         if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number > target_sample)
904                                 break;
905                 }
906                 if(i < (int)decoder->private_->seek_table->num_points) { /* i.e. we found a suitable seek point... */
907                         upper_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;
908                         upper_seek_point = i;
909                 }
910         }
911
912         /*
913          * Now guess at where within those bounds our target
914          * sample will be.
915          */
916         if(lower_seek_point >= 0) {
917                 /* first see if our sample is within a few frames of the lower seekpoint */
918                 if(decoder->private_->seek_table->points[lower_seek_point].sample_number <= target_sample && target_sample < decoder->private_->seek_table->points[lower_seek_point].sample_number + (decoder->private_->seek_table->points[lower_seek_point].frame_samples * 4)) {
919                         pos = (FLAC__int64)lower_bound;
920                 }
921                 else if(upper_seek_point >= 0) {
922                         const FLAC__uint64 target_offset = target_sample - decoder->private_->seek_table->points[lower_seek_point].sample_number;
923                         const FLAC__uint64 range_samples = decoder->private_->seek_table->points[upper_seek_point].sample_number - decoder->private_->seek_table->points[lower_seek_point].sample_number;
924                         const FLAC__uint64 range_bytes = upper_bound - lower_bound;
925 #if defined _MSC_VER || defined __MINGW32__
926                         /* with VC++ you have to spoon feed it the casting */
927                         pos = (FLAC__int64)lower_bound + (FLAC__int64)((double)(FLAC__int64)target_offset / (double)(FLAC__int64)range_samples * (double)(FLAC__int64)(range_bytes-1)) - approx_bytes_per_frame;
928 #else
929                         pos = (FLAC__int64)lower_bound + (FLAC__int64)((double)target_offset / (double)range_samples * (double)(range_bytes-1)) - approx_bytes_per_frame;
930 #endif
931                 }
932         }
933
934         /*
935          * If there's no seek table, we need to use the metadata (if we
936          * have it) and the filelength to estimate the position of the
937          * frame with the correct sample.
938          */
939         if(pos < 0 && total_samples > 0) {
940 #if defined _MSC_VER || defined __MINGW32__
941                 /* with VC++ you have to spoon feed it the casting */
942                 pos = (FLAC__int64)first_frame_offset + (FLAC__int64)((double)(FLAC__int64)target_sample / (double)(FLAC__int64)total_samples * (double)(FLAC__int64)(stream_length-first_frame_offset-1)) - approx_bytes_per_frame;
943 #else
944                 pos = (FLAC__int64)first_frame_offset + (FLAC__int64)((double)target_sample / (double)total_samples * (double)(stream_length-first_frame_offset-1)) - approx_bytes_per_frame;
945 #endif
946         }
947
948         /*
949          * If there's no seek table and total_samples is unknown, we
950          * don't even bother trying to figure out a target, we just use
951          * our current position.
952          */
953         if(pos < 0) {
954                 FLAC__uint64 upos;
955                 if(decoder->private_->tell_callback(decoder, &upos, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {
956                         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
957                         return false;
958                 }
959                 pos = (FLAC__int64)upos;
960                 needs_seek = false;
961         }
962         else
963                 needs_seek = true;
964
965         /* clip the position to the bounds, lower bound takes precedence */
966         if(pos >= (FLAC__int64)upper_bound) {
967                 pos = (FLAC__int64)upper_bound-1;
968                 needs_seek = true;
969         }
970         if(pos < (FLAC__int64)lower_bound) {
971                 pos = (FLAC__int64)lower_bound;
972                 needs_seek = true;
973         }
974
975         decoder->private_->target_sample = target_sample;
976         while(1) {
977                 if(needs_seek) {
978                         if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {
979                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
980                                 return false;
981                         }
982                         if(!FLAC__stream_decoder_flush(decoder->private_->stream_decoder)) {
983                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
984                                 return false;
985                         }
986                 }
987                 /* Now we need to get a frame.  It is possible for our seek
988                  * to land in the middle of audio data that looks exactly like
989                  * a frame header from a future version of an encoder.  When
990                  * that happens, FLAC__stream_decoder_process_single() will
991                  * return false and the state will be
992                  * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM.  But there is a
993                  * remote possibility that it is properly synced at such a
994                  * "future-codec frame", so to make sure, we wait to see
995                  * several "unparseable" errors in a row before bailing out.
996                  */
997                 {
998                         unsigned unparseable_count;
999                         FLAC__bool got_a_frame = false;
1000                         for (unparseable_count = 0; !got_a_frame && unparseable_count < 10; unparseable_count++) {
1001                                 if(FLAC__stream_decoder_process_single(decoder->private_->stream_decoder))
1002                                         got_a_frame = true;
1003                                 else if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_UNPARSEABLE_STREAM)
1004                                         /* try again.  we don't want to flush the decoder since that clears the bitbuffer */
1005                                         decoder->private_->stream_decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1006                                 else /* it's a real error */
1007                                         break;
1008                         }
1009                         if (!got_a_frame) {
1010                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
1011                                 return false;
1012                         }
1013                 }
1014                 /* our write callback will change the state when it gets to the target frame */
1015                 if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {
1016                         break;
1017                 }
1018                 else { /* we need to narrow the search */
1019                         const FLAC__uint64 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
1020                         FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1021                         if(this_frame_sample == last_frame_sample && pos < last_pos) {
1022                                 /* our last move backwards wasn't big enough, double it */
1023                                 pos -= (last_pos - pos);
1024                                 needs_seek = true;
1025                         }
1026                         else {
1027                                 if(target_sample < this_frame_sample) {
1028                                         last_pos = pos;
1029                                         approx_bytes_per_frame = decoder->private_->last_frame.header.blocksize * channels * bps/8 + 64;
1030                                         pos -= approx_bytes_per_frame;
1031                                         needs_seek = true;
1032                                 }
1033                                 else { /* target_sample >= this_frame_sample + this frame's blocksize */
1034                                         FLAC__uint64 upos;
1035                                         if(decoder->private_->tell_callback(decoder, &upos, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {
1036                                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
1037                                                 return false;
1038                                         }
1039                                         last_pos = pos;
1040                                         pos = (FLAC__int64)upos;
1041                                         pos -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);
1042                                         needs_seek = false;
1043                                         /*
1044                                          * if we haven't hit the target frame yet and our position hasn't changed,
1045                                          * it means we're at the end of the stream and the seek target does not exist.
1046                                          */
1047                                         if(last_pos == pos) {
1048                                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
1049                                                 return false;
1050                                         }
1051                                 }
1052                         }
1053                         if(pos < (FLAC__int64)lower_bound)
1054                                 pos = (FLAC__int64)lower_bound;
1055                         last_frame_sample = this_frame_sample;
1056                 }
1057         }
1058
1059         return true;
1060 }