rename some struct members to avoid C++ 'private' and 'protected' keywords
[platform/upstream/flac.git] / src / libFLAC / stream_decoder.c
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000,2001  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 memset/memcpy() */
23 #include "FLAC/assert.h"
24 #include "protected/stream_decoder.h"
25 #include "private/bitbuffer.h"
26 #include "private/cpu.h"
27 #include "private/crc.h"
28 #include "private/fixed.h"
29 #include "private/lpc.h"
30
31 /***********************************************************************
32  *
33  * Private static data
34  *
35  ***********************************************************************/
36
37 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
38
39 /***********************************************************************
40  *
41  * Private class method prototypes
42  *
43  ***********************************************************************/
44
45 static FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
46 static FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
47 static FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
48 static FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
49 static FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
50 static FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame);
51 static FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
52 static FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
53 static FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
54 static FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
55 static FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order);
56 static FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps);
57 static FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual);
58 static FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
59 static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
60
61 /***********************************************************************
62  *
63  * Private class data
64  *
65  ***********************************************************************/
66
67 typedef struct FLAC__StreamDecoderPrivate {
68         FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data);
69         FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data);
70         void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
71         void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
72         void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
73         void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
74         void *client_data;
75         FLAC__BitBuffer input;
76         FLAC__int32 *output[FLAC__MAX_CHANNELS];
77         FLAC__int32 *residual[FLAC__MAX_CHANNELS];
78         unsigned output_capacity, output_channels;
79         FLAC__uint32 last_frame_number;
80         FLAC__uint64 samples_decoded;
81         FLAC__bool has_stream_info, has_seek_table;
82         FLAC__StreamMetaData stream_info;
83         FLAC__StreamMetaData seek_table;
84         FLAC__Frame frame;
85         FLAC__bool cached; /* true if there is a byte in lookahead */
86         FLAC__CPUInfo cpuinfo;
87         FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
88         FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
89 } FLAC__StreamDecoderPrivate;
90
91 /***********************************************************************
92  *
93  * Public static class data
94  *
95  ***********************************************************************/
96
97 const char *FLAC__StreamDecoderStateString[] = {
98         "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
99         "FLAC__STREAM_DECODER_READ_METADATA",
100         "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
101         "FLAC__STREAM_DECODER_READ_FRAME",
102         "FLAC__STREAM_DECODER_END_OF_STREAM",
103         "FLAC__STREAM_DECODER_ABORTED",
104         "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
105         "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
106         "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
107         "FLAC__STREAM_DECODER_INVALID_CALLBACK",
108         "FLAC__STREAM_DECODER_UNINITIALIZED"
109 };
110
111 const char *FLAC__StreamDecoderReadStatusString[] = {
112         "FLAC__STREAM_DECODER_READ_CONTINUE",
113         "FLAC__STREAM_DECODER_READ_END_OF_STREAM",
114         "FLAC__STREAM_DECODER_READ_ABORT"
115 };
116
117 const char *FLAC__StreamDecoderWriteStatusString[] = {
118         "FLAC__STREAM_DECODER_WRITE_CONTINUE",
119         "FLAC__STREAM_DECODER_WRITE_ABORT"
120 };
121
122 const char *FLAC__StreamDecoderErrorStatusString[] = {
123         "FLAC__STREAM_DECODER_ERROR_LOST_SYNC",
124         "FLAC__STREAM_DECODER_ERROR_BAD_HEADER",
125         "FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH"
126 };
127
128 /***********************************************************************
129  *
130  * Class constructor/destructor
131  *
132  ***********************************************************************/
133 FLAC__StreamDecoder *FLAC__stream_decoder_new()
134 {
135         FLAC__StreamDecoder *decoder;
136
137         FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
138
139         decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
140         if(decoder == 0) {
141                 return 0;
142         }
143         decoder->protected_ = (FLAC__StreamDecoderProtected*)malloc(sizeof(FLAC__StreamDecoderProtected));
144         if(decoder->protected_ == 0) {
145                 free(decoder);
146                 return 0;
147         }
148         decoder->private_ = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
149         if(decoder->private_ == 0) {
150                 free(decoder->protected_);
151                 free(decoder);
152                 return 0;
153         }
154
155         decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
156
157         decoder->private_->read_callback = 0;
158         decoder->private_->write_callback = 0;
159         decoder->private_->metadata_callback = 0;
160         decoder->private_->error_callback = 0;
161         decoder->private_->client_data = 0;
162
163         return decoder;
164 }
165
166 void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
167 {
168         FLAC__ASSERT(decoder != 0);
169         FLAC__ASSERT(decoder->protected_ != 0);
170         FLAC__ASSERT(decoder->private_ != 0);
171
172         free(decoder->private_);
173         free(decoder->protected_);
174         free(decoder);
175 }
176
177 /***********************************************************************
178  *
179  * Public class methods
180  *
181  ***********************************************************************/
182
183 FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
184 {
185         unsigned i;
186
187         FLAC__ASSERT(decoder != 0);
188
189         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
190                 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
191
192         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
193
194         if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
195                 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
196
197         FLAC__bitbuffer_init(&decoder->private_->input);
198
199         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
200                 decoder->private_->output[i] = 0;
201                 decoder->private_->residual[i] = 0;
202         }
203
204         decoder->private_->output_capacity = 0;
205         decoder->private_->output_channels = 0;
206         decoder->private_->last_frame_number = 0;
207         decoder->private_->samples_decoded = 0;
208         decoder->private_->has_stream_info = false;
209         decoder->private_->has_seek_table = false;
210         decoder->private_->cached = false;
211
212         /*
213          * get the CPU info and set the function pointers
214          */
215         FLAC__cpu_info(&decoder->private_->cpuinfo);
216         /* first default to the non-asm routines */
217         decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
218         decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
219         /* now override with asm where appropriate */
220 #ifndef FLAC__NO_ASM
221         if(decoder->private_->cpuinfo.use_asm) {
222 #ifdef FLAC__CPU_IA32
223                 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
224 #ifdef FLAC__HAS_NASM
225                 if(decoder->private_->cpuinfo.data.ia32.mmx) {
226                         decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
227                         decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
228                 }
229                 else {
230                         decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
231                         decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
232                 }
233 #endif
234 #endif
235         }
236 #endif
237
238         return decoder->protected_->state;
239 }
240
241 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
242 {
243         unsigned i;
244         FLAC__ASSERT(decoder != 0);
245         if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
246                 return;
247         if(decoder->private_->has_seek_table) {
248                 free(decoder->private_->seek_table.data.seek_table.points);
249                 decoder->private_->seek_table.data.seek_table.points = 0;
250         }
251         FLAC__bitbuffer_free(&decoder->private_->input);
252         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
253                 /* 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. */
254                 if(decoder->private_->output[i] != 0) {
255                         free(decoder->private_->output[i]-4);
256                         decoder->private_->output[i] = 0;
257                 }
258                 if(decoder->private_->residual[i] != 0) {
259                         free(decoder->private_->residual[i]);
260                         decoder->private_->residual[i] = 0;
261                 }
262         }
263         decoder->private_->output_capacity = 0;
264         decoder->private_->output_channels = 0;
265         decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
266 }
267
268 FLAC__bool FLAC__stream_decoder_set_read_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadStatus (*value)(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data))
269 {
270         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
271                 return false;
272         decoder->private_->read_callback = value;
273         return true;
274 }
275
276 FLAC__bool FLAC__stream_decoder_set_write_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteStatus (*value)(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data))
277 {
278         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
279                 return false;
280         decoder->private_->write_callback = value;
281         return true;
282 }
283
284 FLAC__bool FLAC__stream_decoder_set_metadata_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data))
285 {
286         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
287                 return false;
288         decoder->private_->metadata_callback = value;
289         return true;
290 }
291
292 FLAC__bool FLAC__stream_decoder_set_error_callback(const FLAC__StreamDecoder *decoder, void (*value)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data))
293 {
294         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
295                 return false;
296         decoder->private_->error_callback = value;
297         return true;
298 }
299
300 FLAC__bool FLAC__stream_decoder_set_client_data(const FLAC__StreamDecoder *decoder, void *value)
301 {
302         if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
303                 return false;
304         decoder->private_->client_data = value;
305         return true;
306 }
307
308 FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
309 {
310         return decoder->protected_->state;
311 }
312
313 unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
314 {
315         return decoder->protected_->channels;
316 }
317
318 FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
319 {
320         return decoder->protected_->channel_assignment;
321 }
322
323 unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
324 {
325         return decoder->protected_->bits_per_sample;
326 }
327
328 unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
329 {
330         return decoder->protected_->sample_rate;
331 }
332
333 unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
334 {
335         return decoder->protected_->blocksize;
336 }
337
338 FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
339 {
340         FLAC__ASSERT(decoder != 0);
341
342         if(!FLAC__bitbuffer_clear(&decoder->private_->input)) {
343                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
344                 return false;
345         }
346
347         return true;
348 }
349
350 FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
351 {
352         FLAC__ASSERT(decoder != 0);
353
354         if(!FLAC__stream_decoder_flush(decoder)) {
355                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
356                 return false;
357         }
358         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
359
360         decoder->private_->samples_decoded = 0;
361
362         return true;
363 }
364
365 FLAC__bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
366 {
367         FLAC__bool dummy;
368         FLAC__ASSERT(decoder != 0);
369
370         if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
371                 return true;
372
373         FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
374
375         if(!FLAC__stream_decoder_reset(decoder)) {
376                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
377                 return false;
378         }
379
380         while(1) {
381                 switch(decoder->protected_->state) {
382                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
383                                 if(!stream_decoder_find_metadata_(decoder))
384                                         return false; /* above function sets the status for us */
385                                 break;
386                         case FLAC__STREAM_DECODER_READ_METADATA:
387                                 if(!stream_decoder_read_metadata_(decoder))
388                                         return false; /* above function sets the status for us */
389                                 break;
390                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
391                                 if(!stream_decoder_frame_sync_(decoder))
392                                         return true; /* above function sets the status for us */
393                                 break;
394                         case FLAC__STREAM_DECODER_READ_FRAME:
395                                 if(!stream_decoder_read_frame_(decoder, &dummy))
396                                         return false; /* above function sets the status for us */
397                                 break;
398                         case FLAC__STREAM_DECODER_END_OF_STREAM:
399                                 return true;
400                         default:
401                                 FLAC__ASSERT(0);
402                 }
403         }
404 }
405
406 FLAC__bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
407 {
408         FLAC__ASSERT(decoder != 0);
409
410         if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
411                 return true;
412
413         FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
414
415         if(!FLAC__stream_decoder_reset(decoder)) {
416                 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
417                 return false;
418         }
419
420         while(1) {
421                 switch(decoder->protected_->state) {
422                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
423                                 if(!stream_decoder_find_metadata_(decoder))
424                                         return false; /* above function sets the status for us */
425                                 break;
426                         case FLAC__STREAM_DECODER_READ_METADATA:
427                                 if(!stream_decoder_read_metadata_(decoder))
428                                         return false; /* above function sets the status for us */
429                                 break;
430                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
431                                 return true;
432                                 break;
433                         case FLAC__STREAM_DECODER_END_OF_STREAM:
434                                 return true;
435                         default:
436                                 FLAC__ASSERT(0);
437                 }
438         }
439 }
440
441 FLAC__bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
442 {
443         FLAC__bool got_a_frame;
444         FLAC__ASSERT(decoder != 0);
445
446         if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
447                 return true;
448
449         FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
450
451         while(1) {
452                 switch(decoder->protected_->state) {
453                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
454                                 if(!stream_decoder_frame_sync_(decoder))
455                                         return true; /* above function sets the status for us */
456                                 break;
457                         case FLAC__STREAM_DECODER_READ_FRAME:
458                                 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
459                                         return false; /* above function sets the status for us */
460                                 if(got_a_frame)
461                                         return true; /* above function sets the status for us */
462                                 break;
463                         case FLAC__STREAM_DECODER_END_OF_STREAM:
464                                 return true;
465                         default:
466                                 FLAC__ASSERT(0);
467                 }
468         }
469 }
470
471 FLAC__bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
472 {
473         FLAC__bool dummy;
474         FLAC__ASSERT(decoder != 0);
475
476         if(decoder->protected_->state == FLAC__STREAM_DECODER_END_OF_STREAM)
477                 return true;
478
479         FLAC__ASSERT(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
480
481         while(1) {
482                 switch(decoder->protected_->state) {
483                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
484                                 if(!stream_decoder_frame_sync_(decoder))
485                                         return true; /* above function sets the status for us */
486                                 break;
487                         case FLAC__STREAM_DECODER_READ_FRAME:
488                                 if(!stream_decoder_read_frame_(decoder, &dummy))
489                                         return false; /* above function sets the status for us */
490                                 break;
491                         case FLAC__STREAM_DECODER_END_OF_STREAM:
492                                 return true;
493                         default:
494                                 FLAC__ASSERT(0);
495                 }
496         }
497 }
498
499 /***********************************************************************
500  *
501  * Protected class methods
502  *
503  ***********************************************************************/
504
505 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
506 {
507         FLAC__ASSERT(decoder != 0);
508         return decoder->private_->input.bytes - decoder->private_->input.consumed_bytes;
509 }
510
511 /***********************************************************************
512  *
513  * Private class methods
514  *
515  ***********************************************************************/
516
517 FLAC__bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
518 {
519         unsigned i;
520         FLAC__int32 *tmp;
521
522         if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
523                 return true;
524
525         /* @@@ should change to use realloc() */
526
527         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
528                 if(decoder->private_->output[i] != 0) {
529                         free(decoder->private_->output[i]);
530                         decoder->private_->output[i] = 0;
531                 }
532                 if(decoder->private_->residual[i] != 0) {
533                         free(decoder->private_->residual[i]);
534                         decoder->private_->residual[i] = 0;
535                 }
536         }
537
538         for(i = 0; i < channels; i++) {
539                 /* 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. */
540                 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
541                 if(tmp == 0) {
542                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
543                         return false;
544                 }
545                 memset(tmp, 0, sizeof(FLAC__int32)*4);
546                 decoder->private_->output[i] = tmp + 4;
547
548                 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size);
549                 if(tmp == 0) {
550                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
551                         return false;
552                 }
553                 decoder->private_->residual[i] = tmp;
554         }
555
556         decoder->private_->output_capacity = size;
557         decoder->private_->output_channels = channels;
558
559         return true;
560 }
561
562 FLAC__bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
563 {
564         FLAC__uint32 x;
565         unsigned i, id;
566         FLAC__bool first = true;
567
568         FLAC__ASSERT(decoder->private_->input.consumed_bits == 0); /* make sure we're byte aligned */
569
570         for(i = id = 0; i < 4; ) {
571                 if(decoder->private_->cached) {
572                         x = (FLAC__uint32)decoder->private_->lookahead;
573                         decoder->private_->cached = false;
574                 }
575                 else {
576                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
577                                 return false; /* the read_callback_ sets the state for us */
578                 }
579                 if(x == FLAC__STREAM_SYNC_STRING[i]) {
580                         first = true;
581                         i++;
582                         id = 0;
583                         continue;
584                 }
585                 if(x == ID3V2_TAG_[id]) {
586                         id++;
587                         i = 0;
588                         if(id == 3) {
589                                 if(!stream_decoder_skip_id3v2_tag_(decoder))
590                                         return false; /* the read_callback_ sets the state for us */
591                         }
592                         continue;
593                 }
594                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
595                         decoder->private_->header_warmup[0] = (FLAC__byte)x;
596                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
597                                 return false; /* the read_callback_ sets the state for us */
598
599                         /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
600                         /* else we have to check if the second byte is the end of a sync code */
601                         if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
602                                 decoder->private_->lookahead = (FLAC__byte)x;
603                                 decoder->private_->cached = true;
604                         }
605                         else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
606                                 decoder->private_->header_warmup[1] = (FLAC__byte)x;
607                                 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
608                                 return true;
609                         }
610                 }
611                 i = 0;
612                 if(first) {
613                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
614                         first = false;
615                 }
616         }
617
618         decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
619         return true;
620 }
621
622 FLAC__bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
623 {
624         FLAC__uint32 i, x, last_block, type, length;
625         FLAC__uint64 xx;
626
627         FLAC__ASSERT(decoder->private_->input.consumed_bits == 0); /* make sure we're byte aligned */
628
629         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
630                 return false; /* the read_callback_ sets the state for us */
631         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
632                 return false; /* the read_callback_ sets the state for us */
633         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
634                 return false; /* the read_callback_ sets the state for us */
635         if(type == FLAC__METADATA_TYPE_STREAMINFO) {
636                 unsigned used_bits = 0;
637                 decoder->private_->stream_info.type = type;
638                 decoder->private_->stream_info.is_last = last_block;
639                 decoder->private_->stream_info.length = length;
640
641                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
642                         return false; /* the read_callback_ sets the state for us */
643                 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
644                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
645
646                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
647                         return false; /* the read_callback_ sets the state for us */
648                 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
649                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
650
651                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
652                         return false; /* the read_callback_ sets the state for us */
653                 decoder->private_->stream_info.data.stream_info.min_framesize = x;
654                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
655
656                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
657                         return false; /* the read_callback_ sets the state for us */
658                 decoder->private_->stream_info.data.stream_info.max_framesize = x;
659                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
660
661                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
662                         return false; /* the read_callback_ sets the state for us */
663                 decoder->private_->stream_info.data.stream_info.sample_rate = x;
664                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
665
666                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
667                         return false; /* the read_callback_ sets the state for us */
668                 decoder->private_->stream_info.data.stream_info.channels = x+1;
669                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
670
671                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
672                         return false; /* the read_callback_ sets the state for us */
673                 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
674                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
675
676                 if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
677                         return false; /* the read_callback_ sets the state for us */
678                 used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
679
680                 for(i = 0; i < 16; i++) {
681                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
682                                 return false; /* the read_callback_ sets the state for us */
683                         decoder->private_->stream_info.data.stream_info.md5sum[i] = (FLAC__byte)x;
684                 }
685                 used_bits += i*8;
686
687                 /* skip the rest of the block */
688                 FLAC__ASSERT(used_bits % 8 == 0);
689                 length -= (used_bits / 8);
690                 for(i = 0; i < length; i++) {
691                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
692                                 return false; /* the read_callback_ sets the state for us */
693                 }
694
695                 decoder->private_->has_stream_info = true;
696                 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
697         }
698         else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
699                 unsigned real_points;
700
701                 decoder->private_->seek_table.type = type;
702                 decoder->private_->seek_table.is_last = last_block;
703                 decoder->private_->seek_table.length = length;
704
705                 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LEN;
706
707                 if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetaData_SeekPoint*)malloc(decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetaData_SeekPoint)))) {
708                         decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
709                         return false;
710                 }
711                 for(i = real_points = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
712                         if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
713                                 return false; /* the read_callback_ sets the state for us */
714                         decoder->private_->seek_table.data.seek_table.points[real_points].sample_number = xx;
715
716                         if(!FLAC__bitbuffer_read_raw_uint64(&decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
717                                 return false; /* the read_callback_ sets the state for us */
718                         decoder->private_->seek_table.data.seek_table.points[real_points].stream_offset = xx;
719
720                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
721                                 return false; /* the read_callback_ sets the state for us */
722                         decoder->private_->seek_table.data.seek_table.points[real_points].frame_samples = x;
723
724                         if(decoder->private_->seek_table.data.seek_table.points[real_points].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER)
725                                 real_points++;
726                 }
727                 decoder->private_->seek_table.data.seek_table.num_points = real_points;
728
729                 decoder->private_->has_seek_table = true;
730                 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
731         }
732         else {
733                 /* skip other metadata blocks */
734                 for(i = 0; i < length; i++) {
735                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
736                                 return false; /* the read_callback_ sets the state for us */
737                 }
738         }
739
740         if(last_block)
741                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
742
743         return true;
744 }
745
746 FLAC__bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
747 {
748         FLAC__uint32 x;
749         unsigned i, skip;
750
751         /* skip the version and flags bytes */
752         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 24, read_callback_, decoder))
753                 return false; /* the read_callback_ sets the state for us */
754         /* get the size (in bytes) to skip */
755         skip = 0;
756         for(i = 0; i < 4; i++) {
757                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
758                         return false; /* the read_callback_ sets the state for us */
759                 skip <<= 7;
760                 skip |= (x & 0x7f);
761         }
762         /* skip the rest of the tag */
763         for(i = 0; i < skip; i++) {
764                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
765                         return false; /* the read_callback_ sets the state for us */
766         }
767         return true;
768 }
769
770 FLAC__bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
771 {
772         FLAC__uint32 x;
773         FLAC__bool first = true;
774
775         /* If we know the total number of samples in the stream, stop if we've read that many. */
776         /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
777         if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
778                 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
779                         decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
780                         return true;
781                 }
782         }
783
784         /* make sure we're byte aligned */
785         if(decoder->private_->input.consumed_bits != 0) {
786                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8-decoder->private_->input.consumed_bits, read_callback_, decoder))
787                         return false; /* the read_callback_ sets the state for us */
788         }
789
790         while(1) {
791                 if(decoder->private_->cached) {
792                         x = (FLAC__uint32)decoder->private_->lookahead;
793                         decoder->private_->cached = false;
794                 }
795                 else {
796                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
797                                 return false; /* the read_callback_ sets the state for us */
798                 }
799                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
800                         decoder->private_->header_warmup[0] = (FLAC__byte)x;
801                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
802                                 return false; /* the read_callback_ sets the state for us */
803
804                         /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
805                         /* else we have to check if the second byte is the end of a sync code */
806                         if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
807                                 decoder->private_->lookahead = (FLAC__byte)x;
808                                 decoder->private_->cached = true;
809                         }
810                         else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
811                                 decoder->private_->header_warmup[1] = (FLAC__byte)x;
812                                 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
813                                 return true;
814                         }
815                 }
816                 if(first) {
817                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
818                         first = 0;
819                 }
820         }
821
822         return true;
823 }
824
825 FLAC__bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame)
826 {
827         unsigned channel;
828         unsigned i;
829         FLAC__int32 mid, side, left, right;
830         FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
831         FLAC__uint32 x;
832
833         *got_a_frame = false;
834
835         /* init the CRC */
836         frame_crc = 0;
837         FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
838         FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
839         FLAC__bitbuffer_init_read_crc16(&decoder->private_->input, frame_crc);
840
841         if(!stream_decoder_read_frame_header_(decoder))
842                 return false;
843         if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
844                 return true;
845         if(!stream_decoder_allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
846                 return false;
847         for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
848                 /*
849                  * first figure the correct bits-per-sample of the subframe
850                  */
851                 unsigned bps = decoder->private_->frame.header.bits_per_sample;
852                 switch(decoder->private_->frame.header.channel_assignment) {
853                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
854                                 /* no adjustment needed */
855                                 break;
856                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
857                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
858                                 if(channel == 1)
859                                         bps++;
860                                 break;
861                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
862                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
863                                 if(channel == 0)
864                                         bps++;
865                                 break;
866                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
867                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
868                                 if(channel == 1)
869                                         bps++;
870                                 break;
871                         default:
872                                 FLAC__ASSERT(0);
873                 }
874                 /*
875                  * now read it
876                  */
877                 if(!stream_decoder_read_subframe_(decoder, channel, bps))
878                         return false;
879                 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
880                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
881                         return true;
882                 }
883         }
884         if(!stream_decoder_read_zero_padding_(decoder))
885                 return false;
886
887         /*
888          * Read the frame CRC-16 from the footer and check
889          */
890         frame_crc = decoder->private_->input.read_crc16;
891         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
892                 return false; /* the read_callback_ sets the state for us */
893         if(frame_crc == (FLAC__uint16)x) {
894                 /* Undo any special channel coding */
895                 switch(decoder->private_->frame.header.channel_assignment) {
896                         case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
897                                 /* do nothing */
898                                 break;
899                         case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
900                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
901                                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
902                                         decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
903                                 break;
904                         case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
905                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
906                                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
907                                         decoder->private_->output[0][i] += decoder->private_->output[1][i];
908                                 break;
909                         case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
910                                 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
911                                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
912                                         mid = decoder->private_->output[0][i];
913                                         side = decoder->private_->output[1][i];
914                                         mid <<= 1;
915                                         if(side & 1) /* i.e. if 'side' is odd... */
916                                                 mid++;
917                                         left = mid + side;
918                                         right = mid - side;
919                                         decoder->private_->output[0][i] = left >> 1;
920                                         decoder->private_->output[1][i] = right >> 1;
921                                 }
922                                 break;
923                         default:
924                                 FLAC__ASSERT(0);
925                                 break;
926                 }
927         }
928         else {
929                 /* Bad frame, emit error and zero the output signal */
930                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_FRAME_CRC_MISMATCH, decoder->private_->client_data);
931                 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
932                         memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
933                 }
934         }
935
936         *got_a_frame = true;
937
938         /* put the latest values into the public section of the decoder instance */
939         decoder->protected_->channels = decoder->private_->frame.header.channels;
940         decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
941         decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
942         decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
943         decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
944
945         FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
946         decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
947
948         /* write it */
949         /* NOTE: some versions of GCC can't figure out const-ness right and will give you an 'incompatible pointer type' warning on arg 3 here: */
950         if(decoder->private_->write_callback(decoder, &decoder->private_->frame, decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
951                 return false;
952
953         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
954         return true;
955 }
956
957 FLAC__bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
958 {
959         FLAC__uint32 x;
960         FLAC__uint64 xx;
961         unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
962         FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
963         unsigned raw_header_len;
964         FLAC__bool is_unparseable = false;
965         const FLAC__bool is_known_variable_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize);
966         const FLAC__bool is_known_fixed_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
967
968         FLAC__ASSERT(decoder->private_->input.consumed_bits == 0); /* make sure we're byte aligned */
969
970         /* init the raw header with the saved bits from synchronization */
971         raw_header[0] = decoder->private_->header_warmup[0];
972         raw_header[1] = decoder->private_->header_warmup[1];
973         raw_header_len = 2;
974
975         /*
976          * check to make sure that the reserved bits are 0
977          */
978         if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
979                 is_unparseable = true;
980         }
981
982         /*
983          * Note that along the way as we read the header, we look for a sync
984          * code inside.  If we find one it would indicate that our original
985          * sync was bad since there cannot be a sync code in a valid header.
986          */
987
988         /*
989          * read in the raw header as bytes so we can CRC it, and parse it on the way
990          */
991         for(i = 0; i < 2; i++) {
992                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
993                         return false; /* the read_callback_ sets the state for us */
994                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
995                         /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
996                         decoder->private_->lookahead = (FLAC__byte)x;
997                         decoder->private_->cached = true;
998                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
999                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1000                         return true;
1001                 }
1002                 raw_header[raw_header_len++] = (FLAC__byte)x;
1003         }
1004
1005         switch(x = raw_header[2] >> 4) {
1006                 case 0:
1007                         if(is_known_fixed_blocksize_stream)
1008                                 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
1009                         else
1010                                 is_unparseable = true;
1011                         break;
1012                 case 1:
1013                         decoder->private_->frame.header.blocksize = 192;
1014                         break;
1015                 case 2:
1016                 case 3:
1017                 case 4:
1018                 case 5:
1019                         decoder->private_->frame.header.blocksize = 576 << (x-2);
1020                         break;
1021                 case 6:
1022                 case 7:
1023                         blocksize_hint = x;
1024                         break;
1025                 case 8:
1026                 case 9:
1027                 case 10:
1028                 case 11:
1029                 case 12:
1030                 case 13:
1031                 case 14:
1032                 case 15:
1033                         decoder->private_->frame.header.blocksize = 256 << (x-8);
1034                         break;
1035                 default:
1036                         FLAC__ASSERT(0);
1037                         break;
1038         }
1039
1040         switch(x = raw_header[2] & 0x0f) {
1041                 case 0:
1042                         if(decoder->private_->has_stream_info)
1043                                 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
1044                         else
1045                                 is_unparseable = true;
1046                         break;
1047                 case 1:
1048                 case 2:
1049                 case 3:
1050                         is_unparseable = true;
1051                         break;
1052                 case 4:
1053                         decoder->private_->frame.header.sample_rate = 8000;
1054                         break;
1055                 case 5:
1056                         decoder->private_->frame.header.sample_rate = 16000;
1057                         break;
1058                 case 6:
1059                         decoder->private_->frame.header.sample_rate = 22050;
1060                         break;
1061                 case 7:
1062                         decoder->private_->frame.header.sample_rate = 24000;
1063                         break;
1064                 case 8:
1065                         decoder->private_->frame.header.sample_rate = 32000;
1066                         break;
1067                 case 9:
1068                         decoder->private_->frame.header.sample_rate = 44100;
1069                         break;
1070                 case 10:
1071                         decoder->private_->frame.header.sample_rate = 48000;
1072                         break;
1073                 case 11:
1074                         decoder->private_->frame.header.sample_rate = 96000;
1075                         break;
1076                 case 12:
1077                 case 13:
1078                 case 14:
1079                         sample_rate_hint = x;
1080                         break;
1081                 case 15:
1082                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1083                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1084                         return true;
1085                 default:
1086                         FLAC__ASSERT(0);
1087         }
1088
1089         x = (unsigned)(raw_header[3] >> 4);
1090         if(x & 8) {
1091                 decoder->private_->frame.header.channels = 2;
1092                 switch(x & 7) {
1093                         case 0:
1094                                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
1095                                 break;
1096                         case 1:
1097                                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
1098                                 break;
1099                         case 2:
1100                                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
1101                                 break;
1102                         default:
1103                                 is_unparseable = true;
1104                                 break;
1105                 }
1106         }
1107         else {
1108                 decoder->private_->frame.header.channels = (unsigned)x + 1;
1109                 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
1110         }
1111
1112         switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
1113                 case 0:
1114                         if(decoder->private_->has_stream_info)
1115                                 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
1116                         else
1117                                 is_unparseable = true;
1118                         break;
1119                 case 1:
1120                         decoder->private_->frame.header.bits_per_sample = 8;
1121                         break;
1122                 case 2:
1123                         decoder->private_->frame.header.bits_per_sample = 12;
1124                         break;
1125                 case 4:
1126                         decoder->private_->frame.header.bits_per_sample = 16;
1127                         break;
1128                 case 5:
1129                         decoder->private_->frame.header.bits_per_sample = 20;
1130                         break;
1131                 case 6:
1132                         decoder->private_->frame.header.bits_per_sample = 24;
1133                         break;
1134                 case 3:
1135                 case 7:
1136                         is_unparseable = true;
1137                         break;
1138                 default:
1139                         FLAC__ASSERT(0);
1140                         break;
1141         }
1142
1143         if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
1144                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1145                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1146                 return true;
1147         }
1148
1149         if(blocksize_hint && is_known_variable_blocksize_stream) {
1150                 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1151                         return false; /* the read_callback_ sets the state for us */
1152                 if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */
1153                         decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1154                         decoder->private_->cached = true;
1155                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1156                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1157                         return true;
1158                 }
1159                 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1160                 decoder->private_->frame.header.number.sample_number = xx;
1161         }
1162         else {
1163                 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
1164                         return false; /* the read_callback_ sets the state for us */
1165                 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
1166                         decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1167                         decoder->private_->cached = true;
1168                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1169                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1170                         return true;
1171                 }
1172                 decoder->private_->last_frame_number = x;
1173                 if(decoder->private_->has_stream_info) {
1174                         decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1175                         decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x;
1176                 }
1177                 else {
1178                         is_unparseable = true;
1179                 }
1180         }
1181
1182         if(blocksize_hint) {
1183                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
1184                         return false; /* the read_callback_ sets the state for us */
1185                 raw_header[raw_header_len++] = (FLAC__byte)x;
1186                 if(blocksize_hint == 7) {
1187                         FLAC__uint32 _x;
1188                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &_x, 8, read_callback_, decoder))
1189                                 return false; /* the read_callback_ sets the state for us */
1190                         raw_header[raw_header_len++] = (FLAC__byte)_x;
1191                         x = (x << 8) | _x;
1192                 }
1193                 decoder->private_->frame.header.blocksize = x+1;
1194         }
1195
1196         if(sample_rate_hint) {
1197                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
1198                         return false; /* the read_callback_ sets the state for us */
1199                 raw_header[raw_header_len++] = (FLAC__byte)x;
1200                 if(sample_rate_hint != 12) {
1201                         FLAC__uint32 _x;
1202                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &_x, 8, read_callback_, decoder))
1203                                 return false; /* the read_callback_ sets the state for us */
1204                         raw_header[raw_header_len++] = (FLAC__byte)_x;
1205                         x = (x << 8) | _x;
1206                 }
1207                 if(sample_rate_hint == 12)
1208                         decoder->private_->frame.header.sample_rate = x*1000;
1209                 else if(sample_rate_hint == 13)
1210                         decoder->private_->frame.header.sample_rate = x;
1211                 else
1212                         decoder->private_->frame.header.sample_rate = x*10;
1213         }
1214
1215         /* read the CRC-8 byte */
1216         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder))
1217                 return false; /* the read_callback_ sets the state for us */
1218         crc8 = (FLAC__byte)x;
1219
1220         if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
1221                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_BAD_HEADER, decoder->private_->client_data);
1222                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1223                 return true;
1224         }
1225
1226         if(is_unparseable) {
1227                 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1228                 return false;
1229         }
1230
1231         return true;
1232 }
1233
1234 FLAC__bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1235 {
1236         FLAC__uint32 x;
1237         FLAC__bool wasted_bits;
1238
1239         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
1240                 return false; /* the read_callback_ sets the state for us */
1241
1242         wasted_bits = (x & 1);
1243         x &= 0xfe;
1244
1245         if(wasted_bits) {
1246                 unsigned u;
1247                 if(!FLAC__bitbuffer_read_unary_unsigned(&decoder->private_->input, &u, read_callback_, decoder))
1248                         return false; /* the read_callback_ sets the state for us */
1249                 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1250                 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
1251         }
1252         else
1253                 decoder->private_->frame.subframes[channel].wasted_bits = 0;
1254
1255         /*
1256          * Lots of magic numbers here
1257          */
1258         if(x & 0x80) {
1259                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1260                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1261                 return true;
1262         }
1263         else if(x == 0) {
1264                 if(!stream_decoder_read_subframe_constant_(decoder, channel, bps))
1265                         return false;
1266         }
1267         else if(x == 2) {
1268                 if(!stream_decoder_read_subframe_verbatim_(decoder, channel, bps))
1269                         return false;
1270         }
1271         else if(x < 16) {
1272                 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1273                 return false;
1274         }
1275         else if(x <= 24) {
1276                 if(!stream_decoder_read_subframe_fixed_(decoder, channel, bps, (x>>1)&7))
1277                         return false;
1278         }
1279         else if(x < 64) {
1280                 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1281                 return false;
1282         }
1283         else {
1284                 if(!stream_decoder_read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1))
1285                         return false;
1286         }
1287
1288         if(wasted_bits) {
1289                 unsigned i;
1290                 x = decoder->private_->frame.subframes[channel].wasted_bits;
1291                 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1292                         decoder->private_->output[channel][i] <<= x;
1293         }
1294
1295         return true;
1296 }
1297
1298 FLAC__bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1299 {
1300         FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
1301         FLAC__int32 x;
1302         unsigned i;
1303         FLAC__int32 *output = decoder->private_->output[channel];
1304
1305         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
1306
1307         if(!FLAC__bitbuffer_read_raw_int32(&decoder->private_->input, &x, bps, read_callback_, decoder))
1308                 return false; /* the read_callback_ sets the state for us */
1309
1310         subframe->value = x;
1311
1312         /* decode the subframe */
1313         for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1314                 output[i] = x;
1315
1316         return true;
1317 }
1318
1319 FLAC__bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1320 {
1321         FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
1322         FLAC__int32 i32;
1323         FLAC__uint32 u32;
1324         unsigned u;
1325
1326         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
1327
1328         subframe->residual = decoder->private_->residual[channel];
1329         subframe->order = order;
1330
1331         /* read warm-up samples */
1332         for(u = 0; u < order; u++) {
1333                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private_->input, &i32, bps, read_callback_, decoder))
1334                         return false; /* the read_callback_ sets the state for us */
1335                 subframe->warmup[u] = i32;
1336         }
1337
1338         /* read entropy coding method info */
1339         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1340                 return false; /* the read_callback_ sets the state for us */
1341         subframe->entropy_coding_method.type = u32;
1342         switch(subframe->entropy_coding_method.type) {
1343                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1344                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1345                                 return false; /* the read_callback_ sets the state for us */
1346                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1347                         break;
1348                 default:
1349                         decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1350                         return false;
1351         }
1352
1353         /* read residual */
1354         switch(subframe->entropy_coding_method.type) {
1355                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1356                         if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1357                                 return false;
1358                         break;
1359                 default:
1360                         FLAC__ASSERT(0);
1361         }
1362
1363         /* decode the subframe */
1364         memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1365         FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1366
1367         return true;
1368 }
1369
1370 FLAC__bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order)
1371 {
1372         FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
1373         FLAC__int32 i32;
1374         FLAC__uint32 u32;
1375         unsigned u;
1376
1377         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
1378
1379         subframe->residual = decoder->private_->residual[channel];
1380         subframe->order = order;
1381
1382         /* read warm-up samples */
1383         for(u = 0; u < order; u++) {
1384                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private_->input, &i32, bps, read_callback_, decoder))
1385                         return false; /* the read_callback_ sets the state for us */
1386                 subframe->warmup[u] = i32;
1387         }
1388
1389         /* read qlp coeff precision */
1390         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1391                 return false; /* the read_callback_ sets the state for us */
1392         if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
1393                 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1394                 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1395                 return true;
1396         }
1397         subframe->qlp_coeff_precision = u32+1;
1398
1399         /* read qlp shift */
1400         if(!FLAC__bitbuffer_read_raw_int32(&decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1401                 return false; /* the read_callback_ sets the state for us */
1402         subframe->quantization_level = i32;
1403
1404         /* read quantized lp coefficiencts */
1405         for(u = 0; u < order; u++) {
1406                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
1407                         return false; /* the read_callback_ sets the state for us */
1408                 subframe->qlp_coeff[u] = i32;
1409         }
1410
1411         /* read entropy coding method info */
1412         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1413                 return false; /* the read_callback_ sets the state for us */
1414         subframe->entropy_coding_method.type = u32;
1415         switch(subframe->entropy_coding_method.type) {
1416                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1417                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1418                                 return false; /* the read_callback_ sets the state for us */
1419                         subframe->entropy_coding_method.data.partitioned_rice.order = u32;
1420                         break;
1421                 default:
1422                         decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1423                         return false;
1424         }
1425
1426         /* read residual */
1427         switch(subframe->entropy_coding_method.type) {
1428                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1429                         if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, &subframe->entropy_coding_method.data.partitioned_rice, decoder->private_->residual[channel]))
1430                                 return false;
1431                         break;
1432                 default:
1433                         FLAC__ASSERT(0);
1434         }
1435
1436         /* decode the subframe */
1437         memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1438         if(bps <= 16 && subframe->qlp_coeff_precision <= 16)
1439                 decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
1440         else
1441                 decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
1442
1443         return true;
1444 }
1445
1446 FLAC__bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps)
1447 {
1448         FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
1449         FLAC__int32 x, *residual = decoder->private_->residual[channel];
1450         unsigned i;
1451
1452         decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
1453
1454         subframe->data = residual;
1455
1456         for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1457                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private_->input, &x, bps, read_callback_, decoder))
1458                         return false; /* the read_callback_ sets the state for us */
1459                 residual[i] = x;
1460         }
1461
1462         /* decode the subframe */
1463         memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1464
1465         return true;
1466 }
1467
1468 FLAC__bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, FLAC__EntropyCodingMethod_PartitionedRice *partitioned_rice, FLAC__int32 *residual)
1469 {
1470         FLAC__uint32 rice_parameter;
1471         int i;
1472         unsigned partition, sample, u;
1473         const unsigned partition_order = partitioned_rice->order;
1474         const unsigned partitions = 1u << partition_order;
1475         const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
1476
1477         sample = 0;
1478         for(partition = 0; partition < partitions; partition++) {
1479                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1480                         return false; /* the read_callback_ sets the state for us */
1481                 partitioned_rice->parameters[partition] = rice_parameter;
1482                 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
1483                         for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1484 #ifdef FLAC__SYMMETRIC_RICE
1485                                 if(!FLAC__bitbuffer_read_symmetric_rice_signed(&decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1486                                         return false; /* the read_callback_ sets the state for us */
1487 #else
1488                                 if(!FLAC__bitbuffer_read_rice_signed(&decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1489                                         return false; /* the read_callback_ sets the state for us */
1490 #endif
1491                                 residual[sample] = i;
1492                         }
1493                 }
1494                 else {
1495                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
1496                                 return false; /* the read_callback_ sets the state for us */
1497                         partitioned_rice->raw_bits[partition] = rice_parameter;
1498                         for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1499                                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
1500                                         return false; /* the read_callback_ sets the state for us */
1501                                 residual[sample] = i;
1502                         }
1503                 }
1504         }
1505
1506         return true;
1507 }
1508
1509 FLAC__bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1510 {
1511         if(decoder->private_->input.consumed_bits != 0) {
1512                 FLAC__uint32 zero = 0;
1513                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->private_->input, &zero, 8-decoder->private_->input.consumed_bits, read_callback_, decoder))
1514                         return false; /* the read_callback_ sets the state for us */
1515                 if(zero != 0) {
1516                         decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->private_->client_data);
1517                         decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1518                 }
1519         }
1520         return true;
1521 }
1522
1523 FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
1524 {
1525         FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1526         FLAC__StreamDecoderReadStatus status;
1527         status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
1528         if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1529                 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1530         else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1531                 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
1532         return status == FLAC__STREAM_DECODER_READ_CONTINUE;
1533 }