fix state enums
[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_DECODER_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->private_->stream_decoder = FLAC__stream_decoder_new();
135
136         if(0 == decoder->private_->stream_decoder) {
137                 free(decoder->private_);
138                 free(decoder->protected_);
139                 free(decoder);
140                 return 0;
141         }
142
143         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
144
145         decoder->private_->read_callback = 0;
146         decoder->private_->seek_callback = 0;
147         decoder->private_->tell_callback = 0;
148         decoder->private_->length_callback = 0;
149         decoder->private_->eof_callback = 0;
150         decoder->private_->write_callback = 0;
151         decoder->private_->metadata_callback = 0;
152         decoder->private_->error_callback = 0;
153         decoder->private_->client_data = 0;
154
155         decoder->protected_->md5_checking = false;
156
157         return decoder;
158 }
159
160 void FLAC__seekable_stream_decoder_delete(FLAC__SeekableStreamDecoder *decoder)
161 {
162         FLAC__ASSERT(decoder != 0);
163         FLAC__ASSERT(decoder->protected_ != 0);
164         FLAC__ASSERT(decoder->private_ != 0);
165
166         if(decoder->private_->stream_decoder != 0)
167                 FLAC__stream_decoder_delete(decoder->private_->stream_decoder);
168
169         free(decoder->private_);
170         free(decoder->protected_);
171         free(decoder);
172 }
173
174 /***********************************************************************
175  *
176  * Public class methods
177  *
178  ***********************************************************************/
179
180 FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_init(FLAC__SeekableStreamDecoder *decoder)
181 {
182         FLAC__ASSERT(decoder != 0);
183
184         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
185                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_ALREADY_INITIALIZED;
186
187         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
188
189         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)
190                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
191
192         if(0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
193                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_INVALID_CALLBACK;
194
195         decoder->private_->seek_table = 0;
196
197         /* We initialize the MD5Context even though we may never use it.  This is
198          * because md5_checking may be turned on to start and then turned off if a
199          * seek occurs.  So we always init the context here and finalize it in
200          * FLAC__seekable_stream_decoder_finish() to make sure things are always
201          * cleaned up properly.
202          */
203         MD5Init(&decoder->private_->md5context);
204
205         FLAC__stream_decoder_set_read_callback(decoder->private_->stream_decoder, read_callback_);
206         FLAC__stream_decoder_set_write_callback(decoder->private_->stream_decoder, write_callback_);
207         FLAC__stream_decoder_set_metadata_callback(decoder->private_->stream_decoder, metadata_callback_);
208         FLAC__stream_decoder_set_error_callback(decoder->private_->stream_decoder, error_callback_);
209         FLAC__stream_decoder_set_client_data(decoder->private_->stream_decoder, decoder);
210
211         if(FLAC__stream_decoder_init(decoder->private_->stream_decoder) != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA)
212                 return decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
213
214         return decoder->protected_->state;
215 }
216
217 FLAC__bool FLAC__seekable_stream_decoder_finish(FLAC__SeekableStreamDecoder *decoder)
218 {
219         FLAC__bool md5_failed = false;
220
221         FLAC__ASSERT(decoder != 0);
222         FLAC__ASSERT(decoder->private_ != 0);
223         FLAC__ASSERT(decoder->protected_ != 0);
224
225         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
226                 return true;
227
228         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
229
230         /* see the comment in FLAC__seekable_stream_decoder_init() as to why we
231          * always call MD5Final()
232          */
233         MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
234
235         FLAC__stream_decoder_finish(decoder->private_->stream_decoder);
236
237         if(decoder->protected_->md5_checking) {
238                 if(memcmp(decoder->private_->stored_md5sum, decoder->private_->computed_md5sum, 16))
239                         md5_failed = true;
240         }
241
242         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED;
243
244         return !md5_failed;
245 }
246
247 FLAC__bool FLAC__seekable_stream_decoder_set_md5_checking(FLAC__SeekableStreamDecoder *decoder, FLAC__bool value)
248 {
249         FLAC__ASSERT(decoder != 0);
250         FLAC__ASSERT(decoder->protected_ != 0);
251         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
252                 return false;
253         decoder->protected_->md5_checking = value;
254         return true;
255 }
256
257 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))
258 {
259         FLAC__ASSERT(decoder != 0);
260         FLAC__ASSERT(decoder->private_ != 0);
261         FLAC__ASSERT(decoder->protected_ != 0);
262         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
263                 return false;
264         decoder->private_->read_callback = value;
265         return true;
266 }
267
268 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))
269 {
270         FLAC__ASSERT(decoder != 0);
271         FLAC__ASSERT(decoder->private_ != 0);
272         FLAC__ASSERT(decoder->protected_ != 0);
273         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
274                 return false;
275         decoder->private_->seek_callback = value;
276         return true;
277 }
278
279 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))
280 {
281         FLAC__ASSERT(decoder != 0);
282         FLAC__ASSERT(decoder->private_ != 0);
283         FLAC__ASSERT(decoder->protected_ != 0);
284         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
285                 return false;
286         decoder->private_->tell_callback = value;
287         return true;
288 }
289
290 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))
291 {
292         FLAC__ASSERT(decoder != 0);
293         FLAC__ASSERT(decoder->private_ != 0);
294         FLAC__ASSERT(decoder->protected_ != 0);
295         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
296                 return false;
297         decoder->private_->length_callback = value;
298         return true;
299 }
300
301 FLAC__bool FLAC__seekable_stream_decoder_set_eof_callback(FLAC__SeekableStreamDecoder *decoder, FLAC__bool (*value)(const FLAC__SeekableStreamDecoder *decoder, void *client_data))
302 {
303         FLAC__ASSERT(decoder != 0);
304         FLAC__ASSERT(decoder->private_ != 0);
305         FLAC__ASSERT(decoder->protected_ != 0);
306         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
307                 return false;
308         decoder->private_->eof_callback = value;
309         return true;
310 }
311
312 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))
313 {
314         FLAC__ASSERT(decoder != 0);
315         FLAC__ASSERT(decoder->private_ != 0);
316         FLAC__ASSERT(decoder->protected_ != 0);
317         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
318                 return false;
319         decoder->private_->write_callback = value;
320         return true;
321 }
322
323 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))
324 {
325         FLAC__ASSERT(decoder != 0);
326         FLAC__ASSERT(decoder->private_ != 0);
327         FLAC__ASSERT(decoder->protected_ != 0);
328         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
329                 return false;
330         decoder->private_->metadata_callback = value;
331         return true;
332 }
333
334 FLAC__bool FLAC__seekable_stream_decoder_set_error_callback(FLAC__SeekableStreamDecoder *decoder, void (*value)(const FLAC__SeekableStreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
335 {
336         FLAC__ASSERT(decoder != 0);
337         FLAC__ASSERT(decoder->private_ != 0);
338         FLAC__ASSERT(decoder->protected_ != 0);
339         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
340                 return false;
341         decoder->private_->error_callback = value;
342         return true;
343 }
344
345 FLAC__bool FLAC__seekable_stream_decoder_set_client_data(FLAC__SeekableStreamDecoder *decoder, void *value)
346 {
347         FLAC__ASSERT(decoder != 0);
348         FLAC__ASSERT(decoder->private_ != 0);
349         FLAC__ASSERT(decoder->protected_ != 0);
350         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
351                 return false;
352         decoder->private_->client_data = value;
353         return true;
354 }
355
356 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_respond(FLAC__SeekableStreamDecoder *decoder, FLAC__MetaDataType type)
357 {
358         FLAC__ASSERT(decoder != 0);
359         FLAC__ASSERT(decoder->private_ != 0);
360         FLAC__ASSERT(decoder->protected_ != 0);
361         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
362         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
363                 return false;
364         return FLAC__stream_decoder_set_metadata_respond(decoder->private_->stream_decoder, type);
365 }
366
367 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_respond_application(FLAC__SeekableStreamDecoder *decoder, const FLAC__byte id[4])
368 {
369         FLAC__ASSERT(decoder != 0);
370         FLAC__ASSERT(decoder->private_ != 0);
371         FLAC__ASSERT(decoder->protected_ != 0);
372         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
373         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
374                 return false;
375         return FLAC__stream_decoder_set_metadata_respond_application(decoder->private_->stream_decoder, id);
376 }
377
378 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_respond_all(FLAC__SeekableStreamDecoder *decoder)
379 {
380         FLAC__ASSERT(decoder != 0);
381         FLAC__ASSERT(decoder->private_ != 0);
382         FLAC__ASSERT(decoder->protected_ != 0);
383         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
384         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
385                 return false;
386         return FLAC__stream_decoder_set_metadata_respond_all(decoder->private_->stream_decoder);
387 }
388
389 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_ignore(FLAC__SeekableStreamDecoder *decoder, FLAC__MetaDataType type)
390 {
391         FLAC__ASSERT(decoder != 0);
392         FLAC__ASSERT(decoder->private_ != 0);
393         FLAC__ASSERT(decoder->protected_ != 0);
394         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
395         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
396                 return false;
397         return FLAC__stream_decoder_set_metadata_ignore(decoder->private_->stream_decoder, type);
398 }
399
400 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_ignore_application(FLAC__SeekableStreamDecoder *decoder, const FLAC__byte id[4])
401 {
402         FLAC__ASSERT(decoder != 0);
403         FLAC__ASSERT(decoder->private_ != 0);
404         FLAC__ASSERT(decoder->protected_ != 0);
405         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
406         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
407                 return false;
408         return FLAC__stream_decoder_set_metadata_ignore_application(decoder->private_->stream_decoder, id);
409 }
410
411 FLAC__bool FLAC__seekable_stream_decoder_set_metadata_ignore_all(FLAC__SeekableStreamDecoder *decoder)
412 {
413         FLAC__ASSERT(decoder != 0);
414         FLAC__ASSERT(decoder->private_ != 0);
415         FLAC__ASSERT(decoder->protected_ != 0);
416         FLAC__ASSERT(decoder->private_->stream_decoder != 0);
417         if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_UNINITIALIZED)
418                 return false;
419         return FLAC__stream_decoder_set_metadata_ignore_all(decoder->private_->stream_decoder);
420 }
421
422 FLAC__SeekableStreamDecoderState FLAC__seekable_stream_decoder_get_state(const FLAC__SeekableStreamDecoder *decoder)
423 {
424         FLAC__ASSERT(decoder != 0);
425         FLAC__ASSERT(decoder->protected_ != 0);
426         return decoder->protected_->state;
427 }
428
429 FLAC__bool FLAC__seekable_stream_decoder_get_md5_checking(const FLAC__SeekableStreamDecoder *decoder)
430 {
431         FLAC__ASSERT(decoder != 0);
432         FLAC__ASSERT(decoder->protected_ != 0);
433         return decoder->protected_->md5_checking;
434 }
435
436 FLAC__bool FLAC__seekable_stream_decoder_process_whole_stream(FLAC__SeekableStreamDecoder *decoder)
437 {
438         FLAC__bool ret;
439         FLAC__ASSERT(decoder != 0);
440
441         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
442                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
443
444         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
445                 return true;
446
447         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
448
449         ret = FLAC__stream_decoder_process_whole_stream(decoder->private_->stream_decoder);
450         if(!ret)
451                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
452
453         return ret;
454 }
455
456 FLAC__bool FLAC__seekable_stream_decoder_process_metadata(FLAC__SeekableStreamDecoder *decoder)
457 {
458         FLAC__bool ret;
459         FLAC__ASSERT(decoder != 0);
460
461         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
462                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
463
464         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
465                 return true;
466
467         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
468
469         ret = FLAC__stream_decoder_process_metadata(decoder->private_->stream_decoder);
470         if(!ret)
471                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
472
473         return ret;
474 }
475
476 FLAC__bool FLAC__seekable_stream_decoder_process_one_frame(FLAC__SeekableStreamDecoder *decoder)
477 {
478         FLAC__bool ret;
479         FLAC__ASSERT(decoder != 0);
480
481         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
482                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
483
484         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
485                 return true;
486
487         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
488
489         ret = FLAC__stream_decoder_process_one_frame(decoder->private_->stream_decoder);
490         if(!ret)
491                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
492
493         return ret;
494 }
495
496 FLAC__bool FLAC__seekable_stream_decoder_process_remaining_frames(FLAC__SeekableStreamDecoder *decoder)
497 {
498         FLAC__bool ret;
499         FLAC__ASSERT(decoder != 0);
500
501         if(decoder->private_->stream_decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
502                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
503
504         if(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM)
505                 return true;
506
507         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK);
508
509         ret = FLAC__stream_decoder_process_remaining_frames(decoder->private_->stream_decoder);
510         if(!ret)
511                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
512
513         return ret;
514 }
515
516 FLAC__bool FLAC__seekable_stream_decoder_seek_absolute(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 sample)
517 {
518         FLAC__uint64 length;
519
520         FLAC__ASSERT(decoder != 0);
521         FLAC__ASSERT(decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_OK || decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM);
522
523         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEKING;
524
525         /* turn off md5 checking if a seek is attempted */
526         decoder->protected_->md5_checking = false;
527
528         if(!FLAC__stream_decoder_reset(decoder->private_->stream_decoder)) {
529                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
530                 return false;
531         }
532         /* get the file length */
533         if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_LENGTH_STATUS_OK) {
534                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
535                 return false;
536         }
537         /* rewind */
538         if(decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {
539                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
540                 return false;
541         }
542         if(!FLAC__stream_decoder_process_metadata(decoder->private_->stream_decoder)) {
543                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
544                 return false;
545         }
546         if(sample > decoder->private_->stream_info.total_samples) {
547                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
548                 return false;
549         }
550
551         return seek_to_absolute_sample_(decoder, length, sample);
552 }
553
554 /***********************************************************************
555  *
556  * Private class methods
557  *
558  ***********************************************************************/
559
560 FLAC__StreamDecoderReadStatus read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
561 {
562         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
563         (void)decoder;
564         if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {
565                 seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
566                 return FLAC__STREAM_DECODER_READ_END_OF_STREAM;
567         }
568         else if(*bytes > 0) {
569                 unsigned bytes_read = *bytes;
570                 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) {
571                         seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_READ_ERROR;
572                         return FLAC__STREAM_DECODER_READ_ABORT;
573                 }
574                 if(bytes_read == 0) {
575                         if(seekable_stream_decoder->private_->eof_callback(seekable_stream_decoder, seekable_stream_decoder->private_->client_data)) {
576                                 seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_END_OF_STREAM;
577                                 return FLAC__STREAM_DECODER_READ_END_OF_STREAM;
578                         }
579                         else
580                                 return FLAC__STREAM_DECODER_READ_CONTINUE;
581                 }
582                 else {
583                         *bytes = bytes_read;
584                         return FLAC__STREAM_DECODER_READ_CONTINUE;
585                 }
586         }
587         else
588                 return FLAC__STREAM_DECODER_READ_ABORT; /* abort to avoid a deadlock */
589 }
590
591 FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data)
592 {
593         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
594         (void)decoder;
595
596         if(seekable_stream_decoder->protected_->state == FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {
597                 FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
598                 FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
599                 FLAC__uint64 target_sample = seekable_stream_decoder->private_->target_sample;
600
601                 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
602
603                 seekable_stream_decoder->private_->last_frame = *frame; /* save the frame */
604                 if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
605                         unsigned delta = (unsigned)(target_sample - this_frame_sample);
606                         /* kick out of seek mode */
607                         seekable_stream_decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_OK;
608                         /* shift out the samples before target_sample */
609                         if(delta > 0) {
610                                 unsigned channel;
611                                 const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
612                                 for(channel = 0; channel < frame->header.channels; channel++)
613                                         newbuffer[channel] = buffer[channel] + delta;
614                                 seekable_stream_decoder->private_->last_frame.header.blocksize -= delta;
615                                 seekable_stream_decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
616                                 /* write the relevant samples */
617                                 return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, &seekable_stream_decoder->private_->last_frame, newbuffer, seekable_stream_decoder->private_->client_data);
618                         }
619                         else {
620                                 /* write the relevant samples */
621                                 return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);
622                         }
623                 }
624                 else {
625                         return FLAC__STREAM_DECODER_WRITE_CONTINUE;
626                 }
627         }
628         else {
629                 if(seekable_stream_decoder->protected_->md5_checking) {
630                         if(!FLAC__MD5Accumulate(&seekable_stream_decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
631                                 return FLAC__STREAM_DECODER_WRITE_ABORT;
632                 }
633                 return seekable_stream_decoder->private_->write_callback(seekable_stream_decoder, frame, buffer, seekable_stream_decoder->private_->client_data);
634         }
635 }
636
637 void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data)
638 {
639         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
640         (void)decoder;
641
642         if(metadata->type == FLAC__METADATA_TYPE_STREAMINFO) {
643                 seekable_stream_decoder->private_->stream_info = metadata->data.stream_info;
644                 /* save the MD5 signature for comparison later */
645                 memcpy(seekable_stream_decoder->private_->stored_md5sum, metadata->data.stream_info.md5sum, 16);
646                 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))
647                         seekable_stream_decoder->protected_->md5_checking = false;
648         }
649         else if(metadata->type == FLAC__METADATA_TYPE_SEEKTABLE) {
650                 seekable_stream_decoder->private_->seek_table = &metadata->data.seek_table;
651         }
652
653         if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING)
654                 seekable_stream_decoder->private_->metadata_callback(seekable_stream_decoder, metadata, seekable_stream_decoder->private_->client_data);
655 }
656
657 void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
658 {
659         FLAC__SeekableStreamDecoder *seekable_stream_decoder = (FLAC__SeekableStreamDecoder *)client_data;
660         (void)decoder;
661
662         if(seekable_stream_decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING)
663                 seekable_stream_decoder->private_->error_callback(seekable_stream_decoder, status, seekable_stream_decoder->private_->client_data);
664 }
665
666 FLAC__bool seek_to_absolute_sample_(FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
667 {
668         FLAC__uint64 first_frame_offset, lower_bound, upper_bound;
669         FLAC__int64 pos = -1, last_pos = -1;
670         int i, lower_seek_point = -1, upper_seek_point = -1;
671         unsigned approx_bytes_per_frame;
672         FLAC__uint64 last_frame_sample = 0xffffffffffffffff;
673         FLAC__bool needs_seek;
674         const FLAC__bool is_variable_blocksize_stream = (decoder->private_->stream_info.min_blocksize != decoder->private_->stream_info.max_blocksize);
675
676         /* we are just guessing here, but we want to guess high, not low */
677         if(decoder->private_->stream_info.max_framesize > 0) {
678                 approx_bytes_per_frame = decoder->private_->stream_info.max_framesize;
679         }
680         else if(!is_variable_blocksize_stream) {
681                 /* note there are no () around 'decoder->private_->stream_info.bits_per_sample/8' to keep precision up since it's an integer calulation */
682                 approx_bytes_per_frame = decoder->private_->stream_info.min_blocksize * decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample/8 + 64;
683         }
684         else
685                 approx_bytes_per_frame = 1152 * decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample/8 + 64;
686
687         /*
688          * The stream position is currently at the first frame plus any read
689          * ahead data, so first we get the stream position, then subtract
690          * uncomsumed bytes to get the position of the first frame in the
691          * stream.
692          */
693         if(decoder->private_->tell_callback(decoder, &first_frame_offset, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {
694                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
695                 return false;
696         }
697         FLAC__ASSERT(first_frame_offset >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder));
698         first_frame_offset -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);
699
700         /*
701          * First, we set an upper and lower bound on where in the
702          * stream we will search.  For now we assume the worst case
703          * scenario, which is our best guess at the beginning of
704          * the first and last frames.
705          */
706         lower_bound = first_frame_offset;
707
708         /* calc the upper_bound, beyond which we never want to seek */
709         if(decoder->private_->stream_info.max_framesize > 0)
710                 upper_bound = stream_length - (decoder->private_->stream_info.max_framesize + 128 + 2); /* 128 for a possible ID3V1 tag, 2 for indexing differences */
711         else
712                 upper_bound = stream_length - ((decoder->private_->stream_info.channels * decoder->private_->stream_info.bits_per_sample * FLAC__MAX_BLOCK_SIZE) / 8 + 128 + 2);
713
714         /*
715          * Now we refine the bounds if we have a seektable with
716          * suitable points.  Note that according to the spec they
717          * must be ordered by ascending sample number.
718          */
719         if(0 != decoder->private_->seek_table) {
720                 /* find the closest seek point <= target_sample, if it exists */
721                 for(i = (int)decoder->private_->seek_table->num_points - 1; i >= 0; i--) {
722                         if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number <= target_sample)
723                                 break;
724                 }
725                 if(i >= 0) { /* i.e. we found a suitable seek point... */
726                         lower_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;
727                         lower_seek_point = i;
728                 }
729
730                 /* find the closest seek point > target_sample, if it exists */
731                 for(i = 0; i < (int)decoder->private_->seek_table->num_points; i++) {
732                         if(decoder->private_->seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && decoder->private_->seek_table->points[i].sample_number > target_sample)
733                                 break;
734                 }
735                 if(i < (int)decoder->private_->seek_table->num_points) { /* i.e. we found a suitable seek point... */
736                         upper_bound = first_frame_offset + decoder->private_->seek_table->points[i].stream_offset;
737                         upper_seek_point = i;
738                 }
739         }
740
741         /*
742          * Now guess at where within those bounds our target
743          * sample will be.
744          */
745         if(lower_seek_point >= 0) {
746                 /* first see if our sample is within a few frames of the lower seekpoint */
747                 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)) {
748                         pos = (FLAC__int64)lower_bound;
749                 }
750                 else if(upper_seek_point >= 0) {
751                         const FLAC__uint64 target_offset = target_sample - decoder->private_->seek_table->points[lower_seek_point].sample_number;
752                         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;
753                         const FLAC__uint64 range_bytes = upper_bound - lower_bound;
754 #if defined _MSC_VER || defined __MINGW32__
755                         /* with VC++ you have to spoon feed it the casting */
756                         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;
757 #else
758                         pos = (FLAC__int64)lower_bound + (FLAC__int64)((double)target_offset / (double)range_samples * (double)(range_bytes-1)) - approx_bytes_per_frame;
759 #endif
760                 }
761         }
762         if(pos < 0) {
763                 /* We need to use the metadata and the filelength to estimate the position of the frame with the correct sample */
764 #if defined _MSC_VER || defined __MINGW32__
765                 /* with VC++ you have to spoon feed it the casting */
766                 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;
767 #else
768                 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;
769 #endif
770         }
771
772         /* clip the position to the bounds, lower bound takes precedence */
773         if(pos >= (FLAC__int64)upper_bound)
774                 pos = (FLAC__int64)upper_bound-1;
775         if(pos < (FLAC__int64)lower_bound)
776                 pos = (FLAC__int64)lower_bound;
777         needs_seek = true;
778
779         decoder->private_->target_sample = target_sample;
780         while(1) {
781                 if(needs_seek) {
782                         if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_SEEK_STATUS_OK) {
783                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
784                                 return false;
785                         }
786                         if(!FLAC__stream_decoder_flush(decoder->private_->stream_decoder)) {
787                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR;
788                                 return false;
789                         }
790                 }
791                 if(!FLAC__stream_decoder_process_one_frame(decoder->private_->stream_decoder)) {
792                         decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
793                         return false;
794                 }
795                 /* our write callback will change the state when it gets to the target frame */
796                 if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING) {
797                         break;
798                 }
799                 else { /* we need to narrow the search */
800                         FLAC__uint64 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
801                         FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
802                         if(this_frame_sample == last_frame_sample) {
803                                 /* our last move backwards wasn't big enough */
804                                 pos -= (last_pos - pos);
805                                 needs_seek = true;
806                         }
807                         else {
808                                 if(target_sample < this_frame_sample) {
809                                         last_pos = pos;
810                                         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;
811                                         pos -= approx_bytes_per_frame;
812                                         needs_seek = true;
813                                 }
814                                 else { /* target_sample >= this_frame_sample + this frame's blocksize */
815                                         last_pos = pos;
816                                         if(decoder->private_->tell_callback(decoder, (FLAC__uint64*)(&pos), decoder->private_->client_data) != FLAC__SEEKABLE_STREAM_DECODER_TELL_STATUS_OK) {
817                                                 decoder->protected_->state = FLAC__SEEKABLE_STREAM_DECODER_SEEK_ERROR;
818                                                 return false;
819                                         }
820                                         pos -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder->private_->stream_decoder);
821                                         needs_seek = false;
822                                 }
823                         }
824                         if(pos < (FLAC__int64)lower_bound)
825                                 pos = (FLAC__int64)lower_bound;
826                         last_frame_sample = this_frame_sample;
827                 }
828         }
829
830         return true;
831 }