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