fix status enum, more rigorous setting of defaults, add inherited get methods from...
[platform/upstream/flac.git] / src / libFLAC / seekable_stream_decoder.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001,2002  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h> /* for malloc() */
22 #include <string.h> /* for memcpy()/memcmp() */
23 #include "FLAC/assert.h"
24 #include "protected/seekable_stream_decoder.h"
25 #include "protected/stream_decoder.h"
26 #include "private/md5.h"
27
28 /***********************************************************************
29  *
30  * Private class method prototypes
31  *
32  ***********************************************************************/
33
34 static void seekable_stream_decoder_set_defaults_(FLAC__SeekableStreamDecoder *decoder);
35 static FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
36 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
37 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
38 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
39 static FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
40
41 /***********************************************************************
42  *
43  * Private class data
44  *
45  ***********************************************************************/
46
47 typedef struct FLAC__SeekableStreamDecoderPrivate {
48         FLAC__SeekableStreamDecoderReadStatus (*read_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
49         FLAC__SeekableStreamDecoderSeekStatus (*seek_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
50         FLAC__SeekableStreamDecoderTellStatus (*tell_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
51         FLAC__SeekableStreamDecoderLengthStatus (*length_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
52         FLAC__bool (*eof_callback)(const FLAC__SeekableStreamDecoder *decoder, void *client_data);
53         FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
54         void (*metadata_callback)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
55         void (*error_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
56         void *client_data;
57         FLAC__StreamDecoder *stream_decoder;
58         struct MD5Context md5context;
59         FLAC__byte stored_md5sum[16]; /* this is what is stored in the metadata */
60         FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
61         /* the rest of these are only used for seeking: */
62         FLAC__StreamMetaData_StreamInfo stream_info; /* we keep this around so we can figure out how to seek quickly */
63         const FLAC__StreamMetaData_SeekTable *seek_table; /* we hold a pointer to the stream decoder's seek table for the same reason */
64         FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
65         FLAC__uint64 target_sample;
66 } FLAC__SeekableStreamDecoderPrivate;
67
68 /***********************************************************************
69  *
70  * Public static class data
71  *
72  ***********************************************************************/
73
74 const char *FLAC__SeekableStreamDecoderStateString[] = {
75         "FLAC__SEEKABLE_STREAM_DECODER_OK",
76         "FLAC__SEEKABLE_STREAM_DECODER_SEEKING",
77         "FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM",
78         "FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
79         "FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR",
80         "FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR",
81         "FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR",
82     "FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED",
83     "FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK",
84     "FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED"
85 };
86
87 const char *FLAC__SeekableStreamDecoderReadStatusString[] = {
88         "FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK",
89         "FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_ERROR"
90 };
91
92 const char *FLAC__SeekableStreamDecoderSeekStatusString[] = {
93         "FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK",
94         "FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_ERROR"
95 };
96
97 const char *FLAC__SeekableStreamDecoderTellStatusString[] = {
98         "FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK",
99         "FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_ERROR"
100 };
101
102 const char *FLAC__SeekableStreamDecoderLengthStatusString[] = {
103         "FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK",
104         "FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_ERROR"
105 };
106
107 /***********************************************************************
108  *
109  * Class constructor/destructor
110  *
111  ***********************************************************************/
112
113 FLAC__SeekableStreamDecoder *FLAC__seekable_stream_decoder_new()
114 {
115         FLAC__SeekableStreamDecoder *decoder;
116
117         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
118
119         decoder = (FLAC__SeekableStreamDecoder*)malloc(sizeof(FLAC__SeekableStreamDecoder));
120         if(decoder == 0) {
121                 return 0;
122         }
123         decoder->protected_ = (FLAC__SeekableStreamDecoderProtected*)malloc(sizeof(FLAC__SeekableStreamDecoderProtected));
124         if(decoder->protected_ == 0) {
125                 free(decoder);
126                 return 0;
127         }
128         decoder->private_ = (FLAC__SeekableStreamDecoderPrivate*)malloc(sizeof(FLAC__SeekableStreamDecoderPrivate));
129         if(decoder->private_ == 0) {
130                 free(decoder->protected_);
131                 free(decoder);
132                 return 0;
133         }
134
135         decoder->private_->stream_decoder = FLAC__stream_decoder_new();
136
137         if(0 == decoder->private_->stream_decoder) {
138                 free(decoder->private_);
139                 free(decoder->protected_);
140                 free(decoder);
141                 return 0;
142         }
143
144         seekable_stream_decoder_set_defaults_(decoder);
145
146         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
147
148         return decoder;
149 }
150
151 void FLAC__seekable_stream_decoder_delete(FLAC__SeekableStreamDecoder *decoder)
152 {
153         FLAC__ASSERT(decoder != 0);
154         FLAC__ASSERT(decoder->protected_ != 0);
155         FLAC__ASSERT(decoder->private_ != 0);
156
157         if(decoder->private_->stream_decoder != 0)
158                 FLAC__stream_decoder_delete(decoder->private_->stream_decoder);
159
160         free(decoder->private_);
161         free(decoder->protected_);
162         free(decoder);
163 }
164
165 /***********************************************************************
166  *
167  * Public class methods
168  *
169  ***********************************************************************/
170
171 FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_init(FLAC__SeekableStreamDecoder *decoder)
172 {
173         FLAC__ASSERT(decoder != 0);
174
175         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
176                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED;
177
178         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
179
180         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)
181                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
182
183         if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
184                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
185
186         decoder->private_->seek_table = 0;
187
188         /* We initialize the MD5Context even though we may never use it.  This is
189          * because md5_checking may be turned on to start and then turned off if a
190          * seek occurs.  So we always init the context here and finalize it in
191          * FLAC__seekable_stream_decoder_finish() to make sure things are always
192          * cleaned up properly.
193          */
194         MD5Init(&decoder->private_->md5context);
195
196         FLAC__stream_decoder_set_read_callback(decoder->private_->stream_decoder, read_callback_);
197         FLAC__stream_decoder_set_write_callback(decoder->private_->stream_decoder, write_callback_);
198         FLAC__stream_decoder_set_metadata_callback(decoder->private_->stream_decoder, metadata_callback_);
199         FLAC__stream_decoder_set_error_callback(decoder->private_->stream_decoder, error_callback_);
200         FLAC__stream_decoder_set_client_data(decoder->private_->stream_decoder, decoder);
201
202         if(FLAC__stream_decoder_init(decoder->private_->stream_decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
203                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
204
205         return decoder->protected_->state;
206 }
207
208 FLAC__bool FLAC__seekable_stream_decoder_finish(FLAC__SeekableStreamDecoder *decoder)
209 {
210         FLAC__bool md5_failed = false;
211
212         FLAC__ASSERT(decoder != 0);
213         FLAC__ASSERT(decoder->private_ != 0);
214         FLAC__ASSERT(decoder->protected_ != 0);
215
216         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
217                 return true;
218
219         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
220
221         /* see the comment in FLAC__seekable_stream_decoder_init() as to why we
222          * always call MD5Final()
223          */
224         MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
225
226         FLAC__stream_decoder_finish(decoder->private_->stream_decoder);
227
228         if(decoder->protected_->md5_checking) {
229                 if(memcmp(decoder->private_->stored_md5sum, decoder->private_->computed_md5sum, 16))
230                         md5_failed = true;
231         }
232
233         seekable_stream_decoder_set_defaults_(decoder);
234
235         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
236
237         return !md5_failed;
238 }
239
240 FLAC__bool FLAC__seekable_stream_decoder_set_md5_checking(FLAC__SeekableStreamDecoder *decoder, FLAC__bool value)
241 {
242         FLAC__ASSERT(decoder != 0);
243         FLAC__ASSERT(decoder->protected_ != 0);
244         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
245                 return false;
246         decoder->protected_->md5_checking = value;
247         return true;
248 }
249
250 FLAC__bool FLAC__seekable_stream_decoder_set_read_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderReadStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data))
251 {
252         FLAC__ASSERT(decoder != 0);
253         FLAC__ASSERT(decoder->private_ != 0);
254         FLAC__ASSERT(decoder->protected_ != 0);
255         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
256                 return false;
257         decoder->private_->read_callback = value;
258         return true;
259 }
260
261 FLAC__bool FLAC__seekable_stream_decoder_set_seek_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderSeekStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data))
262 {
263         FLAC__ASSERT(decoder != 0);
264         FLAC__ASSERT(decoder->private_ != 0);
265         FLAC__ASSERT(decoder->protected_ != 0);
266         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
267                 return false;
268         decoder->private_->seek_callback = value;
269         return true;
270 }
271
272 FLAC__bool FLAC__seekable_stream_decoder_set_tell_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderTellStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data))
273 {
274         FLAC__ASSERT(decoder != 0);
275         FLAC__ASSERT(decoder->private_ != 0);
276         FLAC__ASSERT(decoder->protected_ != 0);
277         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
278                 return false;
279         decoder->private_->tell_callback = value;
280         return true;
281 }
282
283 FLAC__bool FLAC__seekable_stream_decoder_set_length_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderLengthStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data))
284 {
285         FLAC__ASSERT(decoder != 0);
286         FLAC__ASSERT(decoder->private_ != 0);
287         FLAC__ASSERT(decoder->protected_ != 0);
288         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
289                 return false;
290         decoder->private_->length_callback = value;
291         return true;
292 }
293
294 FLAC__bool FLAC__seekable_stream_decoder_set_eof_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__bool (*value)(const FLAC__SeekableStreamDecoder *decoder, void *client_data))
295 {
296         FLAC__ASSERT(decoder != 0);
297         FLAC__ASSERT(decoder->private_ != 0);
298         FLAC__ASSERT(decoder->protected_ != 0);
299         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
300                 return false;
301         decoder->private_->eof_callback = value;
302         return true;
303 }
304
305 FLAC__bool FLAC__seekable_stream_decoder_set_write_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))
306 {
307         FLAC__ASSERT(decoder != 0);
308         FLAC__ASSERT(decoder->private_ != 0);
309         FLAC__ASSERT(decoder->protected_ != 0);
310         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
311                 return false;
312         decoder->private_->write_callback = value;
313         return true;
314 }
315
316 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_callback(FLAC__SeekableStreamDecoder *decoder, void (*value)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))
317 {
318         FLAC__ASSERT(decoder != 0);
319         FLAC__ASSERT(decoder->private_ != 0);
320         FLAC__ASSERT(decoder->protected_ != 0);
321         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
322                 return false;
323         decoder->private_->metadata_callback = value;
324         return true;
325 }
326
327 FLAC__bool FLAC__seekable_stream_decoder_set_error_callback(FLAC__SeekableStreamDecoder *decoder, void (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
328 {
329         FLAC__ASSERT(decoder != 0);
330         FLAC__ASSERT(decoder->private_ != 0);
331         FLAC__ASSERT(decoder->protected_ != 0);
332         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
333                 return false;
334         decoder->private_->error_callback = value;
335         return true;
336 }
337
338 FLAC__bool FLAC__seekable_stream_decoder_set_client_data(FLAC__SeekableStreamDecoder *decoder, void *value)
339 {
340         FLAC__ASSERT(decoder != 0);
341         FLAC__ASSERT(decoder->private_ != 0);
342         FLAC__ASSERT(decoder->protected_ != 0);
343         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
344                 return false;
345         decoder->private_->client_data = value;
346         return true;
347 }
348
349 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_respond(FLAC__SeekableStreamDecoder *decoder, FLAC__MetaDataType type)
350 {
351         FLAC__ASSERT(decoder != 0);
352         FLAC__ASSERT(decoder->private_ != 0);
353         FLAC__ASSERT(decoder->protected_ != 0);
354         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
355         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
356                 return false;
357         return FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, type);
358 }
359
360 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_respond_application(FLAC__SeekableStreamDecoder *decoder, const FLAC__byte id[4])
361 {
362         FLAC__ASSERT(decoder != 0);
363         FLAC__ASSERT(decoder->private_ != 0);
364         FLAC__ASSERT(decoder->protected_ != 0);
365         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
366         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
367                 return false;
368         return FLAC__stream_decoder_set_metadata_respond_application(decoder->private_->stream_decoder, id);
369 }
370
371 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_respond_all(FLAC__SeekableStreamDecoder *decoder)
372 {
373         FLAC__ASSERT(decoder != 0);
374         FLAC__ASSERT(decoder->private_ != 0);
375         FLAC__ASSERT(decoder->protected_ != 0);
376         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
377         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
378                 return false;
379         return FLAC__stream_decoder_set_metadata_respond_all(decoder->private_->stream_decoder);
380 }
381
382 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_ignore(FLAC__SeekableStreamDecoder *decoder, FLAC__MetaDataType type)
383 {
384         FLAC__ASSERT(decoder != 0);
385         FLAC__ASSERT(decoder->private_ != 0);
386         FLAC__ASSERT(decoder->protected_ != 0);
387         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
388         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
389                 return false;
390         return FLAC__stream_decoder_set_metadata_ignore(decoder->private_->stream_decoder, type);
391 }
392
393 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_ignore_application(FLAC__SeekableStreamDecoder *decoder, const FLAC__byte id[4])
394 {
395         FLAC__ASSERT(decoder != 0);
396         FLAC__ASSERT(decoder->private_ != 0);
397         FLAC__ASSERT(decoder->protected_ != 0);
398         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
399         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
400                 return false;
401         return FLAC__stream_decoder_set_metadata_ignore_application(decoder->private_->stream_decoder, id);
402 }
403
404 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_ignore_all(FLAC__SeekableStreamDecoder *decoder)
405 {
406         FLAC__ASSERT(decoder != 0);
407         FLAC__ASSERT(decoder->private_ != 0);
408         FLAC__ASSERT(decoder->protected_ != 0);
409         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
410         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
411                 return false;
412         return FLAC__stream_decoder_set_metadata_ignore_all(decoder->private_->stream_decoder);
413 }
414
415 FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_get_state(const FLAC__SeekableStreamDecoder *decoder)
416 {
417         FLAC__ASSERT(decoder != 0);
418         FLAC__ASSERT(decoder->protected_ != 0);
419         return decoder->protected_->state;
420 }
421
422 FLAC__bool FLAC__seekable_stream_decoder_get_md5_checking(const FLAC__SeekableStreamDecoder *decoder)
423 {
424         FLAC__ASSERT(decoder != 0);
425         FLAC__ASSERT(decoder->protected_ != 0);
426         return decoder->protected_->md5_checking;
427 }
428
429 unsigned FLAC__seekable_stream_decoder_get_channels(const FLAC__SeekableStreamDecoder *decoder)
430 {
431     FLAC__ASSERT(decoder != 0);
432     FLAC__ASSERT(decoder->private_ != 0);
433     return FLAC__stream_decoder_get_channels(decoder->private_->stream_decoder);
434 }
435
436 FLAC__ChannelAssignment FLAC__seekable_stream_decoder_get_channel_assignment(const FLAC__SeekableStreamDecoder *decoder)
437 {
438     FLAC__ASSERT(decoder != 0);
439     FLAC__ASSERT(decoder->private_ != 0);
440     return FLAC__stream_decoder_get_channel_assignment(decoder->private_->stream_decoder);
441 }
442
443 unsigned FLAC__seekable_stream_decoder_get_bits_per_sample(const FLAC__SeekableStreamDecoder *decoder)
444 {
445     FLAC__ASSERT(decoder != 0);
446     FLAC__ASSERT(decoder->private_ != 0);
447     return FLAC__stream_decoder_get_bits_per_sample(decoder->private_->stream_decoder);
448 }
449
450 unsigned FLAC__seekable_stream_decoder_get_sample_rate(const FLAC__SeekableStreamDecoder *decoder)
451 {
452     FLAC__ASSERT(decoder != 0);
453     FLAC__ASSERT(decoder->private_ != 0);
454     return FLAC__stream_decoder_get_sample_rate(decoder->private_->stream_decoder);
455 }
456
457 unsigned FLAC__seekable_stream_decoder_get_blocksize(const FLAC__SeekableStreamDecoder *decoder)
458 {
459     FLAC__ASSERT(decoder != 0);
460     FLAC__ASSERT(decoder->private_ != 0);
461     return FLAC__stream_decoder_get_blocksize(decoder->private_->stream_decoder);
462 }
463
464 FLAC__bool FLAC__seekable_stream_decoder_flush(FLAC__SeekableStreamDecoder *decoder)
465 {
466         FLAC__ASSERT(decoder != 0);
467         FLAC__ASSERT(decoder->private_ != 0);
468         FLAC__ASSERT(decoder->protected_ != 0);
469
470         decoder->protected_->md5_checking = false;
471
472         if(!FLAC__stream_decoder_flush(decoder->private_->stream_decoder)) {
473                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
474                 return false;
475         }
476
477         return true;
478 }
479
480 FLAC__bool FLAC__seekable_stream_decoder_reset(FLAC__SeekableStreamDecoder *decoder)
481 {
482         FLAC__ASSERT(decoder != 0);
483         FLAC__ASSERT(decoder->private_ != 0);
484         FLAC__ASSERT(decoder->protected_ != 0);
485
486         if(!FLAC__seekable_stream_decoder_flush(decoder)) {
487                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
488                 return false;
489         }
490
491         if(!FLAC__stream_decoder_reset(decoder->private_->stream_decoder)) {
492                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
493                 return false;
494         }
495
496         decoder->private_->seek_table = 0;
497
498         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
499
500         return true;
501 }
502
503 FLAC__bool FLAC__seekable_stream_decoder_process_whole_stream(FLAC__SeekableStreamDecoder *decoder)
504 {
505         FLAC__bool ret;
506         FLAC__ASSERT(decoder != 0);
507
508         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
509                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
510
511         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
512                 return true;
513
514         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
515
516         ret = FLAC__stream_decoder_process_whole_stream(decoder->private_->stream_decoder);
517         if(!ret)
518                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
519
520         return ret;
521 }
522
523 FLAC__bool FLAC__seekable_stream_decoder_process_metadata(FLAC__SeekableStreamDecoder *decoder)
524 {
525         FLAC__bool ret;
526         FLAC__ASSERT(decoder != 0);
527
528         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
529                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
530
531         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
532                 return true;
533
534         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
535
536         ret = FLAC__stream_decoder_process_metadata(decoder->private_->stream_decoder);
537         if(!ret)
538                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
539
540         return ret;
541 }
542
543 FLAC__bool FLAC__seekable_stream_decoder_process_one_frame(FLAC__SeekableStreamDecoder *decoder)
544 {
545         FLAC__bool ret;
546         FLAC__ASSERT(decoder != 0);
547
548         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
549                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
550
551         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
552                 return true;
553
554         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
555
556         ret = FLAC__stream_decoder_process_one_frame(decoder->private_->stream_decoder);
557         if(!ret)
558                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
559
560         return ret;
561 }
562
563 FLAC__bool FLAC__seekable_stream_decoder_process_remaining_frames(FLAC__SeekableStreamDecoder *decoder)
564 {
565         FLAC__bool ret;
566         FLAC__ASSERT(decoder != 0);
567
568         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
569                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
570
571         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
572                 return true;
573
574         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
575
576         ret = FLAC__stream_decoder_process_remaining_frames(decoder->private_->stream_decoder);
577         if(!ret)
578                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
579
580         return ret;
581 }
582
583 FLAC__bool FLAC__seekable_stream_decoder_seek_absolute(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 sample)
584 {
585         FLAC__uint64 length;
586
587         FLAC__ASSERT(decoder != 0);
588         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK || decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM);
589
590         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEKING;
591
592         /* turn off md5 checking if a seek is attempted */
593         decoder->protected_->md5_checking = false;
594
595         if(!FLAC__stream_decoder_reset(decoder->private_->stream_decoder)) {
596                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
597                 return false;
598         }
599         /* get the file length */
600         if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK) {
601                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
602                 return false;
603         }
604         /* rewind */
605         if(decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {
606                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
607                 return false;
608         }
609         if(!FLAC__stream_decoder_process_metadata(decoder->private_->stream_decoder)) {
610                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
611                 return false;
612         }
613         if(sample > decoder->private_->stream_info.total_samples) {
614                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
615                 return false;
616         }
617
618         return seek_to_absolute_sample_(decoder, length, sample);
619 }
620
621 /***********************************************************************
622  *
623  * Private class methods
624  *
625  ***********************************************************************/
626
627 void seekable_stream_decoder_set_defaults_(FLAC__SeekableStreamDecoder *decoder)
628 {
629         decoder->private_->read_callback = 0;
630         decoder->private_->seek_callback = 0;
631         decoder->private_->tell_callback = 0;
632         decoder->private_->length_callback = 0;
633         decoder->private_->eof_callback = 0;
634         decoder->private_->write_callback = 0;
635         decoder->private_->metadata_callback = 0;
636         decoder->private_->error_callback = 0;
637         decoder->private_->client_data = 0;
638
639         decoder->protected_->md5_checking = false;
640 }
641
642 FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
643 {
644         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
645         (void)decoder;
646         if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {
647                 seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
648                 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
649         }
650         else if(*bytes > 0) {
651                 unsigned bytes_read = *bytes;
652                 if(seekable_stream_decoder->private_->read_callback(seekable_stream_decoder, buffer, &bytes_read, seekable_stream_decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_READ_STATUS_OK) {
653                         seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR;
654                         return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
655                 }
656                 if(bytes_read == 0) {
657                         if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {
658                                 seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
659                                 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
660                         }
661                         else
662                                 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
663                 }
664                 else {
665                         *bytes = bytes_read;
666                         return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
667                 }
668         }
669         else
670                 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
671 }
672
673 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)
674 {
675         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
676         (void)decoder;
677
678         if(seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {
679                 FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
680                 FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
681                 FLAC__uint64 target_sample = seekable_stream_decoder->private_->target_sample;
682
683                 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
684
685                 seekable_stream_decoder->private_->last_frame = *frame; /* save the frame */
686                 if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
687                         unsigned delta = (unsigned)(target_sample - this_frame_sample);
688                         /* kick out of seek mode */
689                         seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
690                         /* shift out the samples before target_sample */
691                         if(delta > 0) {
692                                 unsigned channel;
693                                 const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
694                                 for(channel = 0; channel < frame->header.channels; channel++)
695                                         newbuffer[channel] = buffer[channel] + delta;
696                                 seekable_stream_decoder->private_->last_frame.header.blocksize -= delta;
697                                 seekable_stream_decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
698                                 /* write the relevant samples */
699                                 return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, &seekable_stream_decoder->private_->last_frame, newbuffer, seekable_stream_decoder->private_->client_data);
700                         }
701                         else {
702                                 /* write the relevant samples */
703                                 return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);
704                         }
705                 }
706                 else {
707                         return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
708                 }
709         }
710         else {
711                 if(seekable_stream_decoder->protected_->md5_checking) {
712                         if(!FLAC__MD5Accumulate(&seekable_stream_decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
713                                 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
714                 }
715                 return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);
716         }
717 }
718
719 void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
720 {
721         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
722         (void)decoder;
723
724         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
725                 seekable_stream_decoder->private_->stream_info = metadata->data.stream_info;
726                 /* save the MD5 signature for comparison later */
727                 memcpy(seekable_stream_decoder->private_->stored_md5sum, metadata->data.stream_info.md5sum, 16);
728                 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))
729                         seekable_stream_decoder->protected_->md5_checking = false;
730         }
731         else if(metadata->type == FLAC__METADATA_TYPE_SEEKTABLE) {
732                 seekable_stream_decoder->private_->seek_table = &metadata->data.seek_table;
733         }
734
735         if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING)
736                 seekable_stream_decoder->private_->metadata_callback(seekable_stream_decoder, metadata, seekable_stream_decoder->private_->client_data);
737 }
738
739 void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
740 {
741         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
742         (void)decoder;
743
744         if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING)
745                 seekable_stream_decoder->private_->error_callback(seekable_stream_decoder, status, seekable_stream_decoder->private_->client_data);
746 }
747
748 FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
749 {
750         FLAC__uint64 first_frame_offset, lower_bound, upper_bound;
751         FLAC__int64 pos = -1, last_pos = -1;
752         int i, lower_seek_point = -1, upper_seek_point = -1;
753         unsigned approx_bytes_per_frame;
754         FLAC__uint64 last_frame_sample = 0xffffffffffffffff;
755         FLAC__bool needs_seek;
756         const FLAC__bool is_variable_blocksize_stream = (decoder->private_->stream_info.min_blocksize != decoder->private_->stream_info.max_blocksize);
757
758         /* we are just guessing here, but we want to guess high, not low */
759         if(decoder->private_->stream_info.max_framesize > 0) {
760                 approx_bytes_per_frame = decoder->private_->stream_info.max_framesize;
761         }
762         else if(!is_variable_blocksize_stream) {
763                 /* note there are no () around 'decoder->private_->stream_info.bits_per_sample/8' to keep precision up since it's an integer calulation */
764                 approx_bytes_per_frame = decoder->private_->stream_info.min_blocksize * decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample/8 + 64;
765         }
766         else
767                 approx_bytes_per_frame = 1152 * decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample/8 + 64;
768
769         /*
770          * The stream position is currently at the first frame plus any read
771          * ahead data, so first we get the stream position, then subtract
772          * uncomsumed bytes to get the position of the first frame in the
773          * stream.
774          */
775         if(decoder->private_->tell_callback(decoder, &first_frame_offset, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {
776                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
777                 return false;
778         }
779         FLAC__ASSERT(first_frame_offset >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder));
780         first_frame_offset -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);
781
782         /*
783          * First, we set an upper and lower bound on where in the
784          * stream we will search.  For now we assume the worst case
785          * scenario, which is our best guess at the beginning of
786          * the first and last frames.
787          */
788         lower_bound = first_frame_offset;
789
790         /* calc the upper_bound, beyond which we never want to seek */
791         if(decoder->private_->stream_info.max_framesize > 0)
792                 upper_bound = stream_length - (decoder->private_->stream_info.max_framesize + 128 + 2); /* 128 for a possible ID3V1 tag, 2 for indexing differences */
793         else
794                 upper_bound = stream_length - ((decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample * FLAC__MAX_BLOCK_SIZE) / 8 + 128 + 2);
795
796         /*
797          * Now we refine the bounds if we have a seektable with
798          * suitable points.  Note that according to the spec they
799          * must be ordered by ascending sample number.
800          */
801         if(0 != decoder->private_->seek_table) {
802                 /* find the closest seek point <= target_sample, if it exists */
803                 for(i = (int)decoder->private_->seek_table->num_points - 1; i >= 0; i--) {
804                         if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number <= target_sample)
805                                 break;
806                 }
807                 if(i >= 0) { /* i.e. we found a suitable seek point... */
808                         lower_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;
809                         lower_seek_point = i;
810                 }
811
812                 /* find the closest seek point > target_sample, if it exists */
813                 for(i = 0; i < (int)decoder->private_->seek_table->num_points; i++) {
814                         if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number > target_sample)
815                                 break;
816                 }
817                 if(i < (int)decoder->private_->seek_table->num_points) { /* i.e. we found a suitable seek point... */
818                         upper_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;
819                         upper_seek_point = i;
820                 }
821         }
822
823         /*
824          * Now guess at where within those bounds our target
825          * sample will be.
826          */
827         if(lower_seek_point >= 0) {
828                 /* first see if our sample is within a few frames of the lower seekpoint */
829                 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)) {
830                         pos = (FLAC__int64)lower_bound;
831                 }
832                 else if(upper_seek_point >= 0) {
833                         const FLAC__uint64 target_offset = target_sample - decoder->private_->seek_table->points[lower_seek_point].sample_number;
834                         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;
835                         const FLAC__uint64 range_bytes = upper_bound - lower_bound;
836 #if defined _MSC_VER || defined __MINGW32__
837                         /* with VC++ you have to spoon feed it the casting */
838                         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;
839 #else
840                         pos = (FLAC__int64)lower_bound + (FLAC__int64)((double)target_offset / (double)range_samples * (double)(range_bytes-1)) - approx_bytes_per_frame;
841 #endif
842                 }
843         }
844         if(pos < 0) {
845                 /* We need to use the metadata and the filelength to estimate the position of the frame with the correct sample */
846 #if defined _MSC_VER || defined __MINGW32__
847                 /* with VC++ you have to spoon feed it the casting */
848                 pos = (FLAC__int64)first_frame_offset + (FLAC__int64)((double)(FLAC__int64)target_sample / (double)(FLAC__int64)decoder->private_->stream_info.total_samples * (double)(FLAC__int64)(stream_length-first_frame_offset-1)) - approx_bytes_per_frame;
849 #else
850                 pos = (FLAC__int64)first_frame_offset + (FLAC__int64)((double)target_sample / (double)decoder->private_->stream_info.total_samples * (double)(stream_length-first_frame_offset-1)) - approx_bytes_per_frame;
851 #endif
852         }
853
854         /* clip the position to the bounds, lower bound takes precedence */
855         if(pos >= (FLAC__int64)upper_bound)
856                 pos = (FLAC__int64)upper_bound-1;
857         if(pos < (FLAC__int64)lower_bound)
858                 pos = (FLAC__int64)lower_bound;
859         needs_seek = true;
860
861         decoder->private_->target_sample = target_sample;
862         while(1) {
863                 if(needs_seek) {
864                         if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {
865                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
866                                 return false;
867                         }
868                         if(!FLAC__stream_decoder_flush(decoder->private_->stream_decoder)) {
869                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
870                                 return false;
871                         }
872                 }
873                 if(!FLAC__stream_decoder_process_one_frame(decoder->private_->stream_decoder)) {
874                         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
875                         return false;
876                 }
877                 /* our write callback will change the state when it gets to the target frame */
878                 if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {
879                         break;
880                 }
881                 else { /* we need to narrow the search */
882                         FLAC__uint64 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
883                         FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
884                         if(this_frame_sample == last_frame_sample) {
885                                 /* our last move backwards wasn't big enough */
886                                 pos -= (last_pos - pos);
887                                 needs_seek = true;
888                         }
889                         else {
890                                 if(target_sample < this_frame_sample) {
891                                         last_pos = pos;
892                                         approx_bytes_per_frame = decoder->private_->last_frame.header.blocksize * decoder->private_->last_frame.header.channels * decoder->private_->last_frame.header.bits_per_sample/8 + 64;
893                                         pos -= approx_bytes_per_frame;
894                                         needs_seek = true;
895                                 }
896                                 else { /* target_sample >= this_frame_sample + this frame's blocksize */
897                                         last_pos = pos;
898                                         if(decoder->private_->tell_callback(decoder, (FLAC__uint64*)(&pos), decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {
899                                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
900                                                 return false;
901                                         }
902                                         pos -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);
903                                         needs_seek = false;
904                                 }
905                         }
906                         if(pos < (FLAC__int64)lower_bound)
907                                 pos = (FLAC__int64)lower_bound;
908                         last_frame_sample = this_frame_sample;
909                 }
910         }
911
912         return true;
913 }