initial import, still more work to be done...
[flac.git] / src / libOggFLAC / stream_decoder.c
1 /* libOggFLAC - Free Lossless Audio Codec + Ogg library
2  * Copyright (C) 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 <stdlib.h>
21 #include "FLAC/assert.h"
22 #include "protected/stream_decoder.h"
23
24 /***********************************************************************
25  *
26  * Private class method prototypes
27  *
28  ***********************************************************************/
29
30 static void set_defaults_(FLAC__StreamDecoder *decoder);
31
32
33 /***********************************************************************
34  *
35  * Private class data
36  *
37  ***********************************************************************/
38
39 typedef struct FLAC__StreamDecoderPrivate {
40         FLAC__StreamDecoderReadCallback read_callback;
41         FLAC__StreamDecoderWriteCallback write_callback;
42         FLAC__StreamDecoderMetadataCallback metadata_callback;
43         FLAC__StreamDecoderErrorCallback error_callback;
44         void *client_data;
45 } FLAC__StreamDecoderPrivate;
46
47 /***********************************************************************
48  *
49  * Public static class data
50  *
51  ***********************************************************************/
52
53 const char * const FLAC__StreamDecoderStateString[] = {
54         "OggFLAC__STREAM_DECODER_OK",
55         "OggFLAC__STREAM_DECODER_FLAC_STREAM_DECODER_ERROR",
56         "OggFLAC__STREAM_DECODER_INVALID_CALLBACK",
57         "OggFLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
58         "OggFLAC__STREAM_DECODER_ALREADY_INITIALIZED",
59         "OggFLAC__STREAM_DECODER_UNINITIALIZED"
60 };
61
62
63 /***********************************************************************
64  *
65  * Class constructor/destructor
66  *
67  ***********************************************************************/
68 FLAC__StreamDecoder *FLAC__stream_decoder_new()
69 {
70         FLAC__StreamDecoder *decoder;
71         unsigned i;
72
73         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
74
75         decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
76         if(decoder == 0) {
77                 return 0;
78         }
79         memset(decoder, 0, sizeof(FLAC__StreamDecoder));
80
81         decoder->protected_ = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
82         if(decoder->protected_ == 0) {
83                 free(decoder);
84                 return 0;
85         }
86         memset(decoder->protected_, 0, sizeof(FLAC__StreamDecoderProtected));
87
88         decoder->private_ = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
89         if(decoder->private_ == 0) {
90                 free(decoder->protected_);
91                 free(decoder);
92                 return 0;
93         }
94         memset(decoder->private_, 0, sizeof(FLAC__StreamDecoderPrivate));
95
96         decoder->private_->input = FLAC__bitbuffer_new();
97         if(decoder->private_->input == 0) {
98                 free(decoder->private_);
99                 free(decoder->protected_);
100                 free(decoder);
101                 return 0;
102         }
103
104         decoder->private_->metadata_filter_ids_capacity = 16;
105         if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
106                 FLAC__bitbuffer_delete(decoder->private_->input);
107                 free(decoder->private_);
108                 free(decoder->protected_);
109                 free(decoder);
110                 return 0;
111         }
112
113         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
114                 decoder->private_->output[i] = 0;
115                 decoder->private_->residual[i] = 0;
116         }
117
118         decoder->private_->output_capacity = 0;
119         decoder->private_->output_channels = 0;
120         decoder->private_->has_seek_table = false;
121
122         set_defaults_(decoder);
123
124         decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
125
126         return decoder;
127 }
128
129 void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
130 {
131         FLAC__ASSERT(0 != decoder);
132         FLAC__ASSERT(0 != decoder->protected_);
133         FLAC__ASSERT(0 != decoder->private_);
134         FLAC__ASSERT(0 != decoder->private_->input);
135
136         FLAC__stream_decoder_finish(decoder);
137
138         if(0 != decoder->private_->metadata_filter_ids)
139                 free(decoder->private_->metadata_filter_ids);
140
141         FLAC__bitbuffer_delete(decoder->private_->input);
142         free(decoder->private_);
143         free(decoder->protected_);
144         free(decoder);
145 }
146
147 /***********************************************************************
148  *
149  * Public class methods
150  *
151  ***********************************************************************/
152
153 FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
154 {
155         FLAC__ASSERT(0 != decoder);
156
157         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
158                 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
159
160         if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
161                 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
162
163         if(!FLAC__bitbuffer_init(decoder->private_->input))
164                 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
165
166         decoder->private_->last_frame_number = 0;
167         decoder->private_->samples_decoded = 0;
168         decoder->private_->has_stream_info = false;
169         decoder->private_->cached = false;
170
171         /*
172          * get the CPU info and set the function pointers
173          */
174         FLAC__cpu_info(&decoder->private_->cpuinfo);
175         /* first default to the non-asm routines */
176         decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
177         decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
178         /* now override with asm where appropriate */
179 #ifndef FLAC__NO_ASM
180         if(decoder->private_->cpuinfo.use_asm) {
181 #ifdef FLAC__CPU_IA32
182                 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
183 #ifdef FLAC__HAS_NASM
184                 if(decoder->private_->cpuinfo.data.ia32.mmx) {
185                         decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
186                         decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
187                 }
188                 else {
189                         decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
190                         decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
191                 }
192 #endif
193 #endif
194         }
195 #endif
196
197         if(!FLAC__stream_decoder_reset(decoder))
198                 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
199
200         return decoder->protected_->state;
201 }
202
203 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
204 {
205         unsigned i;
206         FLAC__ASSERT(0 != decoder);
207         if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
208                 return;
209         if(decoder->private_->has_seek_table) {
210                 FLAC__ASSERT(0 != decoder->private_->seek_table.data.seek_table.points);
211                 free(decoder->private_->seek_table.data.seek_table.points);
212                 decoder->private_->seek_table.data.seek_table.points = 0;
213                 decoder->private_->has_seek_table = false;
214         }
215         FLAC__bitbuffer_free(decoder->private_->input);
216         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
217                 /* WATCHOUT: FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the output arrays have a buffer of up to 3 zeroes in front (at negative indices) for alignment purposes; we use 4 to keep the data well-aligned. */
218                 if(0 != decoder->private_->output[i]) {
219                         free(decoder->private_->output[i]-4);
220                         decoder->private_->output[i] = 0;
221                 }
222                 if(0 != decoder->private_->residual[i]) {
223                         free(decoder->private_->residual[i]);
224                         decoder->private_->residual[i] = 0;
225                 }
226         }
227         decoder->private_->output_capacity = 0;
228         decoder->private_->output_channels = 0;
229
230         set_defaults_(decoder);
231
232         decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
233 }
234
235 FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
236 {
237         FLAC__ASSERT(0 != decoder);
238         FLAC__ASSERT(0 != decoder->private_);
239         FLAC__ASSERT(0 != decoder->protected_);
240         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
241                 return false;
242         decoder->private_->read_callback = value;
243         return true;
244 }
245
246 FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
247 {
248         FLAC__ASSERT(0 != decoder);
249         FLAC__ASSERT(0 != decoder->private_);
250         FLAC__ASSERT(0 != decoder->protected_);
251         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
252                 return false;
253         decoder->private_->write_callback = value;
254         return true;
255 }
256
257 FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
258 {
259         FLAC__ASSERT(0 != decoder);
260         FLAC__ASSERT(0 != decoder->private_);
261         FLAC__ASSERT(0 != decoder->protected_);
262         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
263                 return false;
264         decoder->private_->metadata_callback = value;
265         return true;
266 }
267
268 FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
269 {
270         FLAC__ASSERT(0 != decoder);
271         FLAC__ASSERT(0 != decoder->private_);
272         FLAC__ASSERT(0 != decoder->protected_);
273         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
274                 return false;
275         decoder->private_->error_callback = value;
276         return true;
277 }
278
279 FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
280 {
281         FLAC__ASSERT(0 != decoder);
282         FLAC__ASSERT(0 != decoder->private_);
283         FLAC__ASSERT(0 != decoder->protected_);
284         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
285                 return false;
286         decoder->private_->client_data = value;
287         return true;
288 }
289
290 FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
291 {
292         FLAC__ASSERT(0 != decoder);
293         FLAC__ASSERT(0 != decoder->private_);
294         FLAC__ASSERT(0 != decoder->protected_);
295         FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
296         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
297                 return false;
298         decoder->private_->metadata_filter[type] = true;
299         if(type == FLAC__METADATA_TYPE_APPLICATION)
300                 decoder->private_->metadata_filter_ids_count = 0;
301         return true;
302 }
303
304 FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
305 {
306         FLAC__ASSERT(0 != decoder);
307         FLAC__ASSERT(0 != decoder->private_);
308         FLAC__ASSERT(0 != decoder->protected_);
309         FLAC__ASSERT(0 != id);
310         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
311                 return false;
312
313         if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
314                 return true;
315
316         FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
317
318         if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
319                 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
320                         return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
321                 decoder->private_->metadata_filter_ids_capacity *= 2;
322         }
323
324         memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
325         decoder->private_->metadata_filter_ids_count++;
326
327         return true;
328 }
329
330 FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
331 {
332         unsigned i;
333         FLAC__ASSERT(0 != decoder);
334         FLAC__ASSERT(0 != decoder->private_);
335         FLAC__ASSERT(0 != decoder->protected_);
336         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
337                 return false;
338         for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
339                 decoder->private_->metadata_filter[i] = true;
340         decoder->private_->metadata_filter_ids_count = 0;
341         return true;
342 }
343
344 FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
345 {
346         FLAC__ASSERT(0 != decoder);
347         FLAC__ASSERT(0 != decoder->private_);
348         FLAC__ASSERT(0 != decoder->protected_);
349         FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT);
350         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
351                 return false;
352         decoder->private_->metadata_filter[type] = false;
353         if(type == FLAC__METADATA_TYPE_APPLICATION)
354                 decoder->private_->metadata_filter_ids_count = 0;
355         return true;
356 }
357
358 FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
359 {
360         FLAC__ASSERT(0 != decoder);
361         FLAC__ASSERT(0 != decoder->private_);
362         FLAC__ASSERT(0 != decoder->protected_);
363         FLAC__ASSERT(0 != id);
364         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
365                 return false;
366
367         if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
368                 return true;
369
370         FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
371
372         if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
373                 if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2)))
374                         return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
375                 decoder->private_->metadata_filter_ids_capacity *= 2;
376         }
377
378         memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
379         decoder->private_->metadata_filter_ids_count++;
380
381         return true;
382 }
383
384 FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
385 {
386         FLAC__ASSERT(0 != decoder);
387         FLAC__ASSERT(0 != decoder->private_);
388         FLAC__ASSERT(0 != decoder->protected_);
389         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
390                 return false;
391         memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
392         decoder->private_->metadata_filter_ids_count = 0;
393         return true;
394 }
395
396 FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
397 {
398         FLAC__ASSERT(0 != decoder);
399         FLAC__ASSERT(0 != decoder->protected_);
400         return decoder->protected_->state;
401 }
402
403 unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
404 {
405         FLAC__ASSERT(0 != decoder);
406         FLAC__ASSERT(0 != decoder->protected_);
407         return decoder->protected_->channels;
408 }
409
410 FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
411 {
412         FLAC__ASSERT(0 != decoder);
413         FLAC__ASSERT(0 != decoder->protected_);
414         return decoder->protected_->channel_assignment;
415 }
416
417 unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
418 {
419         FLAC__ASSERT(0 != decoder);
420         FLAC__ASSERT(0 != decoder->protected_);
421         return decoder->protected_->bits_per_sample;
422 }
423
424 unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
425 {
426         FLAC__ASSERT(0 != decoder);
427         FLAC__ASSERT(0 != decoder->protected_);
428         return decoder->protected_->sample_rate;
429 }
430
431 unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
432 {
433         FLAC__ASSERT(0 != decoder);
434         FLAC__ASSERT(0 != decoder->protected_);
435         return decoder->protected_->blocksize;
436 }
437
438 FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
439 {
440         FLAC__ASSERT(0 != decoder);
441         FLAC__ASSERT(0 != decoder->private_);
442         FLAC__ASSERT(0 != decoder->protected_);
443
444         if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
445                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
446                 return false;
447         }
448         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
449
450         return true;
451 }
452
453 FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
454 {
455         FLAC__ASSERT(0 != decoder);
456         FLAC__ASSERT(0 != decoder->private_);
457         FLAC__ASSERT(0 != decoder->protected_);
458
459         if(!FLAC__stream_decoder_flush(decoder)) {
460                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
461                 return false;
462         }
463         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
464
465         decoder->private_->samples_decoded = 0;
466
467         return true;
468 }
469
470 FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
471 {
472         FLAC__bool got_a_frame;
473         FLAC__ASSERT(0 != decoder);
474         FLAC__ASSERT(0 != decoder->protected_);
475
476         while(1) {
477                 switch(decoder->protected_->state) {
478                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
479                                 if(!find_metadata_(decoder))
480                                         return false; /* above function sets the status for us */
481                                 break;
482                         case FLAC__STREAM_DECODER_READ_METADATA:
483                                 if(!read_metadata_(decoder))
484                                         return false; /* above function sets the status for us */
485                                 else
486                                         return true;
487                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
488                                 if(!frame_sync_(decoder))
489                                         return true; /* above function sets the status for us */
490                                 break;
491                         case FLAC__STREAM_DECODER_READ_FRAME:
492                                 if(!read_frame_(decoder, &got_a_frame))
493                                         return false; /* above function sets the status for us */
494                                 if(got_a_frame)
495                                         return true; /* above function sets the status for us */
496                                 break;
497                         case FLAC__STREAM_DECODER_END_OF_STREAM:
498                         case FLAC__STREAM_DECODER_ABORTED:
499                                 return true;
500                         default:
501                                 FLAC__ASSERT(0);
502                                 return false;
503                 }
504         }
505 }
506
507 FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
508 {
509         FLAC__ASSERT(0 != decoder);
510         FLAC__ASSERT(0 != decoder->protected_);
511
512         while(1) {
513                 switch(decoder->protected_->state) {
514                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
515                                 if(!find_metadata_(decoder))
516                                         return false; /* above function sets the status for us */
517                                 break;
518                         case FLAC__STREAM_DECODER_READ_METADATA:
519                                 if(!read_metadata_(decoder))
520                                         return false; /* above function sets the status for us */
521                                 break;
522                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
523                         case FLAC__STREAM_DECODER_READ_FRAME:
524                         case FLAC__STREAM_DECODER_END_OF_STREAM:
525                         case FLAC__STREAM_DECODER_ABORTED:
526                                 return true;
527                         default:
528                                 FLAC__ASSERT(0);
529                                 return false;
530                 }
531         }
532 }
533
534 FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
535 {
536         FLAC__bool dummy;
537         FLAC__ASSERT(0 != decoder);
538         FLAC__ASSERT(0 != decoder->protected_);
539
540         while(1) {
541                 switch(decoder->protected_->state) {
542                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
543                                 if(!find_metadata_(decoder))
544                                         return false; /* above function sets the status for us */
545                                 break;
546                         case FLAC__STREAM_DECODER_READ_METADATA:
547                                 if(!read_metadata_(decoder))
548                                         return false; /* above function sets the status for us */
549                                 break;
550                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
551                                 if(!frame_sync_(decoder))
552                                         return true; /* above function sets the status for us */
553                                 break;
554                         case FLAC__STREAM_DECODER_READ_FRAME:
555                                 if(!read_frame_(decoder, &dummy))
556                                         return false; /* above function sets the status for us */
557                                 break;
558                         case FLAC__STREAM_DECODER_END_OF_STREAM:
559                         case FLAC__STREAM_DECODER_ABORTED:
560                                 return true;
561                         default:
562                                 FLAC__ASSERT(0);
563                                 return false;
564                 }
565         }
566 }
567
568 /***********************************************************************
569  *
570  * Protected class methods
571  *
572  ***********************************************************************/
573
574 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
575 {
576         FLAC__ASSERT(0 != decoder);
577         return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
578 }
579
580 /***********************************************************************
581  *
582  * Private class methods
583  *
584  ***********************************************************************/
585
586 void set_defaults_(FLAC__StreamDecoder *decoder)
587 {
588         decoder->private_->read_callback = 0;
589         decoder->private_->write_callback = 0;
590         decoder->private_->metadata_callback = 0;
591         decoder->private_->error_callback = 0;
592         decoder->private_->client_data = 0;
593
594         memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
595         decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
596         decoder->private_->metadata_filter_ids_count = 0;
597 }