update copyright date to include 2002
[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 FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
35 static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
36 static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
37 static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
38 static FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
39
40 /***********************************************************************
41  *
42  * Private class data
43  *
44  ***********************************************************************/
45
46 typedef struct FLAC__SeekableStreamDecoderPrivate {
47         FLAC__SeekableStreamDecoderReadStatus (*read_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
48         FLAC__SeekableStreamDecoderSeekStatus (*seek_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
49         FLAC__SeekableStreamDecoderTellStatus (*tell_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
50         FLAC__SeekableStreamDecoderLengthStatus (*length_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
51         FLAC__bool (*eof_callback)(const FLAC__SeekableStreamDecoder *decoder, void *client_data);
52         FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
53         void (*metadata_callback)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
54         void (*error_callback)(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
55         void *client_data;
56         FLAC__StreamDecoder *stream_decoder;
57         struct MD5Context md5context;
58         FLAC__byte stored_md5sum[16]; /* this is what is stored in the metadata */
59         FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
60         /* the rest of these are only used for seeking: */
61         FLAC__StreamMetaData_StreamInfo stream_info; /* we keep this around so we can figure out how to seek quickly */
62         const FLAC__StreamMetaData_SeekTable *seek_table; /* we hold a pointer to the stream decoder's seek table for the same reason */
63         FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
64         FLAC__uint64 target_sample;
65 } FLAC__SeekableStreamDecoderPrivate;
66
67 /***********************************************************************
68  *
69  * Public static class data
70  *
71  ***********************************************************************/
72
73 const char *FLAC__SeekableStreamDecoderStateString[] = {
74         "FLAC__SEEKABLE_STREAM_DECODER_OK",
75         "FLAC__SEEKABLE_STREAM_DECODER_SEEKING",
76         "FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM",
77         "FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
78         "FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR",
79         "FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR",
80         "FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR",
81     "FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED",
82     "FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK",
83     "FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED"
84 };
85
86 const char *FLAC__SeekableStreamDecoderReadStatusString[] = {
87         "FLAC__STREAM_DECODER_READ_STATUS_OK",
88         "FLAC__STREAM_DECODER_READ_STATUS_ERROR"
89 };
90
91 const char *FLAC__SeekableStreamDecoderSeekStatusString[] = {
92         "FLAC__STREAM_DECODER_SEEK_STATUS_OK",
93         "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR"
94 };
95
96 const char *FLAC__SeekableStreamDecoderTellStatusString[] = {
97         "FLAC__STREAM_DECODER_TELL_STATUS_OK",
98         "FLAC__STREAM_DECODER_TELL_STATUS_ERROR"
99 };
100
101 const char *FLAC__SeekableStreamDecoderLengthStatusString[] = {
102         "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
103         "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR"
104 };
105
106 /***********************************************************************
107  *
108  * Class constructor/destructor
109  *
110  ***********************************************************************/
111
112 FLAC__SeekableStreamDecoder *FLAC__seekable_stream_decoder_new()
113 {
114         FLAC__SeekableStreamDecoder *decoder;
115
116         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
117
118         decoder = (FLAC__SeekableStreamDecoder*)malloc(sizeof(FLAC__SeekableStreamDecoder));
119         if(decoder == 0) {
120                 return 0;
121         }
122         decoder->protected_ = (FLAC__SeekableStreamDecoderProtected*)malloc(sizeof(FLAC__SeekableStreamDecoderProtected));
123         if(decoder->protected_ == 0) {
124                 free(decoder);
125                 return 0;
126         }
127         decoder->private_ = (FLAC__SeekableStreamDecoderPrivate*)malloc(sizeof(FLAC__SeekableStreamDecoderPrivate));
128         if(decoder->private_ == 0) {
129                 free(decoder->protected_);
130                 free(decoder);
131                 return 0;
132         }
133
134         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
135
136         decoder->private_->read_callback = 0;
137         decoder->private_->seek_callback = 0;
138         decoder->private_->tell_callback = 0;
139         decoder->private_->length_callback = 0;
140         decoder->private_->eof_callback = 0;
141         decoder->private_->write_callback = 0;
142         decoder->private_->metadata_callback = 0;
143         decoder->private_->error_callback = 0;
144         decoder->private_->client_data = 0;
145
146         return decoder;
147 }
148
149 void FLAC__seekable_stream_decoder_delete(FLAC__SeekableStreamDecoder *decoder)
150 {
151         FLAC__ASSERT(decoder != 0);
152         FLAC__ASSERT(decoder->protected_ != 0);
153         FLAC__ASSERT(decoder->private_ != 0);
154
155         free(decoder->private_);
156         free(decoder->protected_);
157         free(decoder);
158 }
159
160 /***********************************************************************
161  *
162  * Public class methods
163  *
164  ***********************************************************************/
165
166 FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_init(FLAC__SeekableStreamDecoder *decoder)
167 {
168         FLAC__ASSERT(decoder != 0);
169
170         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
171                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED;
172
173         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
174
175         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)
176                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
177
178         if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
179                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
180
181         decoder->private_->stream_decoder = 0;
182         decoder->private_->seek_table = 0;
183
184         /* We initialize the MD5Context even though we may never use it.  This is
185          * because md5_checking may be turned on to start and then turned off if a
186          * seek occurs.  So we always init the context here and finalize it in
187          * FLAC__seekable_stream_decoder_finish() to make sure things are always
188          * cleaned up properly.
189          */
190         MD5Init(&decoder->private_->md5context);
191
192         decoder->private_->stream_decoder = FLAC__stream_decoder_new();
193
194         if(0 == decoder->private_->stream_decoder)
195                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
196
197         FLAC__stream_decoder_set_read_callback(decoder->private_->stream_decoder, read_callback_);
198         FLAC__stream_decoder_set_write_callback(decoder->private_->stream_decoder, write_callback_);
199         FLAC__stream_decoder_set_metadata_callback(decoder->private_->stream_decoder, metadata_callback_);
200         FLAC__stream_decoder_set_error_callback(decoder->private_->stream_decoder, error_callback_);
201         FLAC__stream_decoder_set_client_data(decoder->private_->stream_decoder, decoder);
202
203         if(FLAC__stream_decoder_init(decoder->private_->stream_decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
204                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
205
206         return decoder->protected_->state;
207 }
208
209 FLAC__bool FLAC__seekable_stream_decoder_finish(FLAC__SeekableStreamDecoder *decoder)
210 {
211         FLAC__bool md5_failed = false;
212
213         FLAC__ASSERT(decoder != 0);
214         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
215                 return true;
216         /* see the comment in FLAC__seekable_stream_decoder_init() as to why we
217          * always call MD5Final()
218          */
219         MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
220         if(decoder->private_->stream_decoder != 0) {
221                 FLAC__stream_decoder_finish(decoder->private_->stream_decoder);
222                 FLAC__stream_decoder_delete(decoder->private_->stream_decoder);
223         }
224         if(decoder->protected_->md5_checking) {
225                 if(memcmp(decoder->private_->stored_md5sum, decoder->private_->computed_md5sum, 16))
226                         md5_failed = true;
227         }
228         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
229         return !md5_failed;
230 }
231
232 FLAC__bool FLAC__seekable_stream_decoder_set_md5_checking(const FLAC__SeekableStreamDecoder *decoder, FLAC__bool value)
233 {
234         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
235                 return false;
236         decoder->protected_->md5_checking = value;
237         return true;
238 }
239
240 FLAC__bool FLAC__seekable_stream_decoder_set_read_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderReadStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data))
241 {
242         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
243                 return false;
244         decoder->private_->read_callback = value;
245         return true;
246 }
247
248 FLAC__bool FLAC__seekable_stream_decoder_set_seek_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderSeekStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data))
249 {
250         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
251                 return false;
252         decoder->private_->seek_callback = value;
253         return true;
254 }
255
256 FLAC__bool FLAC__seekable_stream_decoder_set_tell_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderTellStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data))
257 {
258         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
259                 return false;
260         decoder->private_->tell_callback = value;
261         return true;
262 }
263
264 FLAC__bool FLAC__seekable_stream_decoder_set_length_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__SeekableStreamDecoderLengthStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data))
265 {
266         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
267                 return false;
268         decoder->private_->length_callback = value;
269         return true;
270 }
271
272 FLAC__bool FLAC__seekable_stream_decoder_set_eof_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__bool (*value)(const FLAC__SeekableStreamDecoder *decoder, void *client_data))
273 {
274         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
275                 return false;
276         decoder->private_->eof_callback = value;
277         return true;
278 }
279
280 FLAC__bool FLAC__seekable_stream_decoder_set_write_callback(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))
281 {
282         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
283                 return false;
284         decoder->private_->write_callback = value;
285         return true;
286 }
287
288 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_callback(const FLAC__SeekableStreamDecoder *decoder, void (*value)(const FLAC__SeekableStreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))
289 {
290         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
291                 return false;
292         decoder->private_->metadata_callback = value;
293         return true;
294 }
295
296 FLAC__bool FLAC__seekable_stream_decoder_set_error_callback(const FLAC__SeekableStreamDecoder *decoder, void (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
297 {
298         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
299                 return false;
300         decoder->private_->error_callback = value;
301         return true;
302 }
303
304 FLAC__bool FLAC__seekable_stream_decoder_set_client_data(const FLAC__SeekableStreamDecoder *decoder, void *value)
305 {
306         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
307                 return false;
308         decoder->private_->client_data = value;
309         return true;
310 }
311
312 FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_get_state(const FLAC__SeekableStreamDecoder *decoder)
313 {
314         return decoder->protected_->state;
315 }
316
317 FLAC__bool FLAC__seekable_stream_decoder_get_md5_checking(const FLAC__SeekableStreamDecoder *decoder)
318 {
319         return decoder->protected_->md5_checking;
320 }
321
322 FLAC__bool FLAC__seekable_stream_decoder_process_whole_stream(FLAC__SeekableStreamDecoder *decoder)
323 {
324         FLAC__bool ret;
325         FLAC__ASSERT(decoder != 0);
326
327         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
328                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
329
330         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
331                 return true;
332
333         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
334
335         ret = FLAC__stream_decoder_process_whole_stream(decoder->private_->stream_decoder);
336         if(!ret)
337                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
338
339         return ret;
340 }
341
342 FLAC__bool FLAC__seekable_stream_decoder_process_metadata(FLAC__SeekableStreamDecoder *decoder)
343 {
344         FLAC__bool ret;
345         FLAC__ASSERT(decoder != 0);
346
347         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
348                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
349
350         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
351                 return true;
352
353         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
354
355         ret = FLAC__stream_decoder_process_metadata(decoder->private_->stream_decoder);
356         if(!ret)
357                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
358
359         return ret;
360 }
361
362 FLAC__bool FLAC__seekable_stream_decoder_process_one_frame(FLAC__SeekableStreamDecoder *decoder)
363 {
364         FLAC__bool ret;
365         FLAC__ASSERT(decoder != 0);
366
367         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
368                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
369
370         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
371                 return true;
372
373         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
374
375         ret = FLAC__stream_decoder_process_one_frame(decoder->private_->stream_decoder);
376         if(!ret)
377                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
378
379         return ret;
380 }
381
382 FLAC__bool FLAC__seekable_stream_decoder_process_remaining_frames(FLAC__SeekableStreamDecoder *decoder)
383 {
384         FLAC__bool ret;
385         FLAC__ASSERT(decoder != 0);
386
387         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
388                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
389
390         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
391                 return true;
392
393         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
394
395         ret = FLAC__stream_decoder_process_remaining_frames(decoder->private_->stream_decoder);
396         if(!ret)
397                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
398
399         return ret;
400 }
401
402 FLAC__bool FLAC__seekable_stream_decoder_seek_absolute(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 sample)
403 {
404         FLAC__uint64 length;
405
406         FLAC__ASSERT(decoder != 0);
407         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
408
409         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEKING;
410
411         /* turn off md5 checking if a seek is attempted */
412         decoder->protected_->md5_checking = false;
413
414         if(!FLAC__stream_decoder_reset(decoder->private_->stream_decoder)) {
415                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
416                 return false;
417         }
418         /* get the file length */
419         if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK) {
420                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
421                 return false;
422         }
423         /* rewind */
424         if(decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {
425                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
426                 return false;
427         }
428         if(!FLAC__stream_decoder_process_metadata(decoder->private_->stream_decoder)) {
429                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
430                 return false;
431         }
432         if(sample > decoder->private_->stream_info.total_samples) {
433                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
434                 return false;
435         }
436
437         return seek_to_absolute_sample_(decoder, length, sample);
438 }
439
440 /***********************************************************************
441  *
442  * Private class methods
443  *
444  ***********************************************************************/
445
446 FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
447 {
448         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
449         (void)decoder;
450         if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {
451                 seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
452                 return FLAC__STREAM_DECODER_READ_END_OF_STREAM;
453         }
454         else if(*bytes > 0) {
455                 unsigned bytes_read = *bytes;
456                 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) {
457                         seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR;
458                         return FLAC__STREAM_DECODER_READ_ABORT;
459                 }
460                 if(bytes_read == 0) {
461                         if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {
462                                 seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
463                                 return FLAC__STREAM_DECODER_READ_END_OF_STREAM;
464                         }
465                         else
466                                 return FLAC__STREAM_DECODER_READ_CONTINUE;
467                 }
468                 else {
469                         *bytes = bytes_read;
470                         return FLAC__STREAM_DECODER_READ_CONTINUE;
471                 }
472         }
473         else
474                 return FLAC__STREAM_DECODER_READ_ABORT; /* abort to avoid a deadlock */
475 }
476
477 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)
478 {
479         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
480         (void)decoder;
481
482         if(seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {
483                 FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
484                 FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
485                 FLAC__uint64 target_sample = seekable_stream_decoder->private_->target_sample;
486
487                 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
488
489                 seekable_stream_decoder->private_->last_frame = *frame; /* save the frame */
490                 if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
491                         unsigned delta = (unsigned)(target_sample - this_frame_sample);
492                         /* kick out of seek mode */
493                         seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
494                         /* shift out the samples before target_sample */
495                         if(delta > 0) {
496                                 unsigned channel;
497                                 const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
498                                 for(channel = 0; channel < frame->header.channels; channel++)
499                                         newbuffer[channel] = buffer[channel] + delta;
500                                 seekable_stream_decoder->private_->last_frame.header.blocksize -= delta;
501                                 seekable_stream_decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
502                                 /* write the relevant samples */
503                                 return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, &seekable_stream_decoder->private_->last_frame, newbuffer, seekable_stream_decoder->private_->client_data);
504                         }
505                         else {
506                                 /* write the relevant samples */
507                                 return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);
508                         }
509                 }
510                 else {
511                         return FLAC__STREAM_DECODER_WRITE_CONTINUE;
512                 }
513         }
514         else {
515                 if(seekable_stream_decoder->protected_->md5_checking) {
516                         if(!FLAC__MD5Accumulate(&seekable_stream_decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
517                                 return FLAC__STREAM_DECODER_WRITE_ABORT;
518                 }
519                 return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);
520         }
521 }
522
523 void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
524 {
525         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
526         (void)decoder;
527
528         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
529                 seekable_stream_decoder->private_->stream_info = metadata->data.stream_info;
530                 /* save the MD5 signature for comparison later */
531                 memcpy(seekable_stream_decoder->private_->stored_md5sum, metadata->data.stream_info.md5sum, 16);
532                 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))
533                         seekable_stream_decoder->protected_->md5_checking = false;
534         }
535         else if(metadata->type == FLAC__METADATA_TYPE_SEEKTABLE) {
536                 seekable_stream_decoder->private_->seek_table = &metadata->data.seek_table;
537         }
538
539         if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING)
540                 seekable_stream_decoder->private_->metadata_callback(seekable_stream_decoder, metadata, seekable_stream_decoder->private_->client_data);
541 }
542
543 void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
544 {
545         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
546         (void)decoder;
547
548         if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING)
549                 seekable_stream_decoder->private_->error_callback(seekable_stream_decoder, status, seekable_stream_decoder->private_->client_data);
550 }
551
552 FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
553 {
554         FLAC__uint64 first_frame_offset, lower_bound, upper_bound;
555         FLAC__int64 pos = -1, last_pos = -1;
556         int i, lower_seek_point = -1, upper_seek_point = -1;
557         unsigned approx_bytes_per_frame;
558         FLAC__uint64 last_frame_sample = 0xffffffffffffffff;
559         FLAC__bool needs_seek;
560         const FLAC__bool is_variable_blocksize_stream = (decoder->private_->stream_info.min_blocksize != decoder->private_->stream_info.max_blocksize);
561
562         /* we are just guessing here, but we want to guess high, not low */
563         if(decoder->private_->stream_info.max_framesize > 0) {
564                 approx_bytes_per_frame = decoder->private_->stream_info.max_framesize;
565         }
566         else if(!is_variable_blocksize_stream) {
567                 /* note there are no () around 'decoder->private_->stream_info.bits_per_sample/8' to keep precision up since it's an integer calulation */
568                 approx_bytes_per_frame = decoder->private_->stream_info.min_blocksize * decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample/8 + 64;
569         }
570         else
571                 approx_bytes_per_frame = 1152 * decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample/8 + 64;
572
573         /*
574          * The stream position is currently at the first frame plus any read
575          * ahead data, so first we get the stream position, then subtract
576          * uncomsumed bytes to get the position of the first frame in the
577          * stream.
578          */
579         if(decoder->private_->tell_callback(decoder, &first_frame_offset, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {
580                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
581                 return false;
582         }
583         FLAC__ASSERT(first_frame_offset >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder));
584         first_frame_offset -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);
585
586         /*
587          * First, we set an upper and lower bound on where in the
588          * stream we will search.  For now we assume the worst case
589          * scenario, which is our best guess at the beginning of
590          * the first and last frames.
591          */
592         lower_bound = first_frame_offset;
593
594         /* calc the upper_bound, beyond which we never want to seek */
595         if(decoder->private_->stream_info.max_framesize > 0)
596                 upper_bound = stream_length - (decoder->private_->stream_info.max_framesize + 128 + 2); /* 128 for a possible ID3V1 tag, 2 for indexing differences */
597         else
598                 upper_bound = stream_length - ((decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample * FLAC__MAX_BLOCK_SIZE) / 8 + 128 + 2);
599
600         /*
601          * Now we refine the bounds if we have a seektable with
602          * suitable points.  Note that according to the spec they
603          * must be ordered by ascending sample number.
604          */
605         if(0 != decoder->private_->seek_table) {
606                 /* find the closest seek point <= target_sample, if it exists */
607                 for(i = (int)decoder->private_->seek_table->num_points - 1; i >= 0; i--) {
608                         if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number <= target_sample)
609                                 break;
610                 }
611                 if(i >= 0) { /* i.e. we found a suitable seek point... */
612                         lower_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;
613                         lower_seek_point = i;
614                 }
615
616                 /* find the closest seek point > target_sample, if it exists */
617                 for(i = 0; i < (int)decoder->private_->seek_table->num_points; i++) {
618                         if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number > target_sample)
619                                 break;
620                 }
621                 if(i < (int)decoder->private_->seek_table->num_points) { /* i.e. we found a suitable seek point... */
622                         upper_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;
623                         upper_seek_point = i;
624                 }
625         }
626
627         /*
628          * Now guess at where within those bounds our target
629          * sample will be.
630          */
631         if(lower_seek_point >= 0) {
632                 /* first see if our sample is within a few frames of the lower seekpoint */
633                 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)) {
634                         pos = (FLAC__int64)lower_bound;
635                 }
636                 else if(upper_seek_point >= 0) {
637                         const FLAC__uint64 target_offset = target_sample - decoder->private_->seek_table->points[lower_seek_point].sample_number;
638                         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;
639                         const FLAC__uint64 range_bytes = upper_bound - lower_bound;
640 #if defined _MSC_VER || defined __MINGW32__
641                         /* with VC++ you have to spoon feed it the casting */
642                         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;
643 #else
644                         pos = (FLAC__int64)lower_bound + (FLAC__int64)((double)target_offset / (double)range_samples * (double)(range_bytes-1)) - approx_bytes_per_frame;
645 #endif
646                 }
647         }
648         if(pos < 0) {
649                 /* We need to use the metadata and the filelength to estimate the position of the frame with the correct sample */
650 #if defined _MSC_VER || defined __MINGW32__
651                 /* with VC++ you have to spoon feed it the casting */
652                 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;
653 #else
654                 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;
655 #endif
656         }
657
658         /* clip the position to the bounds, lower bound takes precedence */
659         if(pos >= (FLAC__int64)upper_bound)
660                 pos = (FLAC__int64)upper_bound-1;
661         if(pos < (FLAC__int64)lower_bound)
662                 pos = (FLAC__int64)lower_bound;
663         needs_seek = true;
664
665         decoder->private_->target_sample = target_sample;
666         while(1) {
667                 if(needs_seek) {
668                         if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {
669                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
670                                 return false;
671                         }
672                         if(!FLAC__stream_decoder_flush(decoder->private_->stream_decoder)) {
673                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_ERROR;
674                                 return false;
675                         }
676                 }
677                 if(!FLAC__stream_decoder_process_one_frame(decoder->private_->stream_decoder)) {
678                         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
679                         return false;
680                 }
681                 /* our write callback will change the state when it gets to the target frame */
682                 if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {
683                         break;
684                 }
685                 else { /* we need to narrow the search */
686                         FLAC__uint64 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
687                         FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
688                         if(this_frame_sample == last_frame_sample) {
689                                 /* our last move backwards wasn't big enough */
690                                 pos -= (last_pos - pos);
691                                 needs_seek = true;
692                         }
693                         else {
694                                 if(target_sample < this_frame_sample) {
695                                         last_pos = pos;
696                                         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;
697                                         pos -= approx_bytes_per_frame;
698                                         needs_seek = true;
699                                 }
700                                 else { /* target_sample >= this_frame_sample + this frame's blocksize */
701                                         last_pos = pos;
702                                         if(decoder->private_->tell_callback(decoder, (FLAC__uint64*)(&pos), decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {
703                                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
704                                                 return false;
705                                         }
706                                         pos -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);
707                                         needs_seek = false;
708                                 }
709                         }
710                         if(pos < (FLAC__int64)lower_bound)
711                                 pos = (FLAC__int64)lower_bound;
712                         last_frame_sample = this_frame_sample;
713                 }
714         }
715
716         return true;
717 }