add md5.o
[flac.git] / src / libFLAC / stream_decoder.c
1 /* libFLAC - Free Lossless Audio Coder library
2  * Copyright (C) 2000  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 <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h> /* for malloc() */
23 #include "FLAC/stream_decoder.h"
24 #include "private/bitbuffer.h"
25 #include "private/crc.h"
26 #include "private/fixed.h"
27 #include "private/lpc.h"
28
29 typedef struct FLAC__StreamDecoderPrivate {
30         FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data);
31         FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data);
32         void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data);
33         void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
34         void *client_data;
35         FLAC__BitBuffer input;
36         int32 *output[FLAC__MAX_CHANNELS];
37         int32 *residual;
38         unsigned output_capacity;
39         uint32 last_frame_number;
40         uint64 samples_decoded;
41         bool has_stream_header;
42         FLAC__StreamMetaData stream_header;
43         FLAC__FrameHeader frame_header;
44 } FLAC__StreamDecoderPrivate;
45
46 static byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
47
48 static bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size);
49 static bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder);
50 static bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder);
51 static bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
52 static bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder);
53 static bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame);
54 static bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder);
55 static bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel);
56 static bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel);
57 static bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order);
58 static bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order);
59 static bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel);
60 static bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order);
61 static bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder);
62 static bool read_callback_(byte buffer[], unsigned *bytes, void *client_data);
63
64 const char *FLAC__StreamDecoderStateString[] = {
65         "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
66         "FLAC__STREAM_DECODER_READ_METADATA",
67         "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
68         "FLAC__STREAM_DECODER_READ_FRAME",
69         "FLAC__STREAM_DECODER_RESYNC_IN_HEADER",
70         "FLAC__STREAM_DECODER_END_OF_STREAM",
71         "FLAC__STREAM_DECODER_ABORTED",
72         "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
73         "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
74         "FLAC__STREAM_DECODER_UNINITIALIZED"
75 };
76
77 const char *FLAC__StreamDecoderReadStatusString[] = {
78         "FLAC__STREAM_DECODER_READ_CONTINUE",
79         "FLAC__STREAM_DECODER_READ_END_OF_STREAM",
80         "FLAC__STREAM_DECODER_READ_ABORT"
81 };
82
83 const char *FLAC__StreamDecoderWriteStatusString[] = {
84         "FLAC__STREAM_DECODER_WRITE_CONTINUE",
85         "FLAC__STREAM_DECODER_WRITE_ABORT"
86 };
87
88 const char *FLAC__StreamDecoderErrorStatusString[] = {
89         "FLAC__STREAM_DECODER_ERROR_LOST_SYNC"
90 };
91
92 FLAC__StreamDecoder *FLAC__stream_decoder_get_new_instance()
93 {
94         FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)malloc(sizeof(FLAC__StreamDecoder));
95         if(decoder != 0) {
96                 decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
97                 decoder->guts = 0;
98         }
99         return decoder;
100 }
101
102 void FLAC__stream_decoder_free_instance(FLAC__StreamDecoder *decoder)
103 {
104         free(decoder);
105 }
106
107 FLAC__StreamDecoderState FLAC__stream_decoder_init(
108         FLAC__StreamDecoder *decoder,
109         FLAC__StreamDecoderReadStatus (*read_callback)(const FLAC__StreamDecoder *decoder, byte buffer[], unsigned *bytes, void *client_data),
110         FLAC__StreamDecoderWriteStatus (*write_callback)(const FLAC__StreamDecoder *decoder, const FLAC__FrameHeader *header, const int32 *buffer[], void *client_data),
111         void (*metadata_callback)(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetaData *metadata, void *client_data),
112         void (*error_callback)(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data),
113         void *client_data
114 )
115 {
116         unsigned i;
117
118         assert(sizeof(int) >= 4); /* we want to die right away if this is not true */
119         assert(decoder != 0);
120         assert(read_callback != 0);
121         assert(write_callback != 0);
122         assert(metadata_callback != 0);
123         assert(error_callback != 0);
124         assert(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED);
125         assert(decoder->guts == 0);
126
127         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
128
129         decoder->guts = (FLAC__StreamDecoderPrivate*)malloc(sizeof(FLAC__StreamDecoderPrivate));
130         if(decoder->guts == 0)
131                 return decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
132
133         decoder->guts->read_callback = read_callback;
134         decoder->guts->write_callback = write_callback;
135         decoder->guts->metadata_callback = metadata_callback;
136         decoder->guts->error_callback = error_callback;
137         decoder->guts->client_data = client_data;
138
139         FLAC__bitbuffer_init(&decoder->guts->input);
140
141         for(i = 0; i < FLAC__MAX_CHANNELS; i++)
142                 decoder->guts->output[i] = 0;
143         decoder->guts->residual = 0;
144
145         decoder->guts->output_capacity = 0;
146         decoder->guts->last_frame_number = 0;
147         decoder->guts->samples_decoded = 0;
148         decoder->guts->has_stream_header = false;
149
150         return decoder->state;
151 }
152
153 void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
154 {
155         unsigned i;
156         assert(decoder != 0);
157         if(decoder->state == FLAC__STREAM_DECODER_UNINITIALIZED)
158                 return;
159         if(decoder->guts != 0) {
160                 FLAC__bitbuffer_free(&decoder->guts->input);
161                 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
162                         if(decoder->guts->output[i] != 0) {
163                                 free(decoder->guts->output[i]);
164                                 decoder->guts->output[i] = 0;
165                         }
166                 }
167                 if(decoder->guts->residual != 0) {
168                         free(decoder->guts->residual);
169                         decoder->guts->residual = 0;
170                 }
171                 free(decoder->guts);
172                 decoder->guts = 0;
173         }
174         decoder->state = FLAC__STREAM_DECODER_UNINITIALIZED;
175 }
176
177 bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
178 {
179         assert(decoder != 0);
180
181         if(!FLAC__bitbuffer_clear(&decoder->guts->input)) {
182                 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
183                 return false;
184         }
185
186         return true;
187 }
188
189 bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
190 {
191         assert(decoder != 0);
192
193         if(!FLAC__stream_decoder_flush(decoder)) {
194                 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
195                 return false;
196         }
197         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
198
199         return true;
200 }
201
202 bool FLAC__stream_decoder_process_whole_stream(FLAC__StreamDecoder *decoder)
203 {
204         bool dummy;
205         assert(decoder != 0);
206
207         if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
208                 return true;
209
210         assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
211
212         if(!FLAC__stream_decoder_reset(decoder)) {
213                 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
214                 return false;
215         }
216
217         while(1) {
218                 switch(decoder->state) {
219                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
220                                 if(!stream_decoder_find_metadata_(decoder))
221                                         return false; /* above function sets the status for us */
222                                 break;
223                         case FLAC__STREAM_DECODER_READ_METADATA:
224                                 if(!stream_decoder_read_metadata_(decoder))
225                                         return false; /* above function sets the status for us */
226                                 break;
227                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
228                                 if(!stream_decoder_frame_sync_(decoder))
229                                         return true; /* above function sets the status for us */
230                                 break;
231                         case FLAC__STREAM_DECODER_READ_FRAME:
232                                 if(!stream_decoder_read_frame_(decoder, &dummy))
233                                         return false; /* above function sets the status for us */
234                                 break;
235                         case FLAC__STREAM_DECODER_END_OF_STREAM:
236                                 return true;
237                         default:
238                                 assert(0);
239                 }
240         }
241 }
242
243 bool FLAC__stream_decoder_process_metadata(FLAC__StreamDecoder *decoder)
244 {
245         assert(decoder != 0);
246
247         if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
248                 return true;
249
250         assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA);
251
252         if(!FLAC__stream_decoder_reset(decoder)) {
253                 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
254                 return false;
255         }
256
257         while(1) {
258                 switch(decoder->state) {
259                         case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
260                                 if(!stream_decoder_find_metadata_(decoder))
261                                         return false; /* above function sets the status for us */
262                                 break;
263                         case FLAC__STREAM_DECODER_READ_METADATA:
264                                 if(!stream_decoder_read_metadata_(decoder))
265                                         return false; /* above function sets the status for us */
266                                 break;
267                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
268                                 return true;
269                                 break;
270                         case FLAC__STREAM_DECODER_END_OF_STREAM:
271                                 return true;
272                         default:
273                                 assert(0);
274                 }
275         }
276 }
277
278 bool FLAC__stream_decoder_process_one_frame(FLAC__StreamDecoder *decoder)
279 {
280         bool got_a_frame;
281         assert(decoder != 0);
282
283         if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
284                 return true;
285
286         assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
287
288         while(1) {
289                 switch(decoder->state) {
290                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
291                                 if(!stream_decoder_frame_sync_(decoder))
292                                         return true; /* above function sets the status for us */
293                                 break;
294                         case FLAC__STREAM_DECODER_READ_FRAME:
295                                 if(!stream_decoder_read_frame_(decoder, &got_a_frame))
296                                         return false; /* above function sets the status for us */
297                                 if(got_a_frame)
298                                         return true; /* above function sets the status for us */
299                                 break;
300                         case FLAC__STREAM_DECODER_END_OF_STREAM:
301                                 return true;
302                         default:
303                                 assert(0);
304                 }
305         }
306 }
307
308 bool FLAC__stream_decoder_process_remaining_frames(FLAC__StreamDecoder *decoder)
309 {
310         bool dummy;
311         assert(decoder != 0);
312
313         if(decoder->state == FLAC__STREAM_DECODER_END_OF_STREAM)
314                 return true;
315
316         assert(decoder->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC);
317
318         while(1) {
319                 switch(decoder->state) {
320                         case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
321                                 if(!stream_decoder_frame_sync_(decoder))
322                                         return true; /* above function sets the status for us */
323                                 break;
324                         case FLAC__STREAM_DECODER_READ_FRAME:
325                                 if(!stream_decoder_read_frame_(decoder, &dummy))
326                                         return false; /* above function sets the status for us */
327                                 break;
328                         case FLAC__STREAM_DECODER_END_OF_STREAM:
329                                 return true;
330                         default:
331                                 assert(0);
332                 }
333         }
334 }
335
336 unsigned FLAC__stream_decoder_input_bytes_unconsumed(FLAC__StreamDecoder *decoder)
337 {
338         assert(decoder != 0);
339         return decoder->guts->input.bytes - decoder->guts->input.consumed_bytes;
340 }
341
342 bool stream_decoder_allocate_output_(FLAC__StreamDecoder *decoder, unsigned size)
343 {
344         unsigned i;
345         int32 *tmp;
346
347         if(size <= decoder->guts->output_capacity)
348                 return true;
349
350         for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
351                 if(decoder->guts->output[i] != 0) {
352                         free(decoder->guts->output[i]);
353                         decoder->guts->output[i] = 0;
354                 }
355         }
356         if(decoder->guts->residual != 0) {
357                 free(decoder->guts->residual);
358                 decoder->guts->residual = 0;
359         }
360
361         for(i = 0; i < decoder->guts->frame_header.channels; i++) {
362                 tmp = (int32*)malloc(sizeof(int32)*size);
363                 if(tmp == 0) {
364                         decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
365                         return false;
366                 }
367                 decoder->guts->output[i] = tmp;
368         }
369         tmp = (int32*)malloc(sizeof(int32)*size);
370         if(tmp == 0) {
371                 decoder->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
372                 return false;
373         }
374         decoder->guts->residual = tmp;
375
376         decoder->guts->output_capacity = size;
377
378         return true;
379 }
380
381 bool stream_decoder_find_metadata_(FLAC__StreamDecoder *decoder)
382 {
383         uint32 x;
384         unsigned i, id;
385         bool first = 1;
386
387         assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
388
389         for(i = id = 0; i < 4; ) {
390                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
391                         return false; /* the read_callback_ sets the state for us */
392                 if(x == FLAC__STREAM_SYNC_STRING[i]) {
393                         first = 1;
394                         i++;
395                         id = 0;
396                         continue;
397                 }
398                 if(x == ID3V2_TAG_[id]) {
399                         id++;
400                         i = 0;
401                         if(id == 3) {
402                                 if(!stream_decoder_skip_id3v2_tag_(decoder))
403                                         return false; /* the read_callback_ sets the state for us */
404                         }
405                         continue;
406                 }
407                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
408                         unsigned y;
409                         if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
410                                 return false; /* the read_callback_ sets the state for us */
411                         if(!y) { /* MAGIC NUMBER for the last sync bit */
412                                 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
413                                 return true;
414                         }
415                 }
416                 i = 0;
417                 if(first) {
418                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
419                         first = 0;
420                 }
421         }
422
423         decoder->state = FLAC__STREAM_DECODER_READ_METADATA;
424         return true;
425 }
426
427 bool stream_decoder_read_metadata_(FLAC__StreamDecoder *decoder)
428 {
429         uint32 i, x, last_block, type, length;
430
431         assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
432
433         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
434                 return false; /* the read_callback_ sets the state for us */
435         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
436                 return false; /* the read_callback_ sets the state for us */
437         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
438                 return false; /* the read_callback_ sets the state for us */
439         if(type == FLAC__METADATA_TYPE_ENCODING) {
440                 unsigned used_bits = 0;
441                 decoder->guts->stream_header.type = type;
442                 decoder->guts->stream_header.is_last = last_block;
443                 decoder->guts->stream_header.length = length;
444
445                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN, read_callback_, decoder))
446                         return false; /* the read_callback_ sets the state for us */
447                 decoder->guts->stream_header.data.encoding.min_blocksize = x;
448                 used_bits += FLAC__STREAM_METADATA_ENCODING_MIN_BLOCK_SIZE_LEN;
449
450                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
451                         return false; /* the read_callback_ sets the state for us */
452                 decoder->guts->stream_header.data.encoding.max_blocksize = x;
453                 used_bits += FLAC__STREAM_METADATA_ENCODING_MAX_BLOCK_SIZE_LEN;
454
455                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
456                         return false; /* the read_callback_ sets the state for us */
457                 decoder->guts->stream_header.data.encoding.min_framesize = x;
458                 used_bits += FLAC__STREAM_METADATA_ENCODING_MIN_FRAME_SIZE_LEN;
459
460                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
461                         return false; /* the read_callback_ sets the state for us */
462                 decoder->guts->stream_header.data.encoding.max_framesize = x;
463                 used_bits += FLAC__STREAM_METADATA_ENCODING_MAX_FRAME_SIZE_LEN;
464
465                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN, read_callback_, decoder))
466                         return false; /* the read_callback_ sets the state for us */
467                 decoder->guts->stream_header.data.encoding.sample_rate = x;
468                 used_bits += FLAC__STREAM_METADATA_ENCODING_SAMPLE_RATE_LEN;
469
470                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN, read_callback_, decoder))
471                         return false; /* the read_callback_ sets the state for us */
472                 decoder->guts->stream_header.data.encoding.channels = x+1;
473                 used_bits += FLAC__STREAM_METADATA_ENCODING_CHANNELS_LEN;
474
475                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
476                         return false; /* the read_callback_ sets the state for us */
477                 decoder->guts->stream_header.data.encoding.bits_per_sample = x+1;
478                 used_bits += FLAC__STREAM_METADATA_ENCODING_BITS_PER_SAMPLE_LEN;
479
480                 if(!FLAC__bitbuffer_read_raw_uint64(&decoder->guts->input, &decoder->guts->stream_header.data.encoding.total_samples, FLAC__STREAM_METADATA_ENCODING_TOTAL_SAMPLES_LEN, read_callback_, decoder))
481                         return false; /* the read_callback_ sets the state for us */
482                 used_bits += FLAC__STREAM_METADATA_ENCODING_TOTAL_SAMPLES_LEN;
483
484                 for(i = 0; i < 16; i++) {
485                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
486                                 return false; /* the read_callback_ sets the state for us */
487                         decoder->guts->stream_header.data.encoding.md5sum[i] = (byte)x;
488                 }
489                 used_bits += 128;
490
491                 /* skip the rest of the block */
492                 assert(used_bits % 8 == 0);
493                 length -= (used_bits / 8);
494                 for(i = 0; i < length; i++) {
495                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
496                                 return false; /* the read_callback_ sets the state for us */
497                 }
498
499                 decoder->guts->has_stream_header = true;
500                 decoder->guts->metadata_callback(decoder, &decoder->guts->stream_header, decoder->guts->client_data);
501         }
502         else {
503                 /* skip other metadata blocks */
504                 for(i = 0; i < length; i++) {
505                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
506                                 return false; /* the read_callback_ sets the state for us */
507                 }
508         }
509
510         if(last_block)
511                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
512
513         return true;
514 }
515
516 bool stream_decoder_skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
517 {
518         uint32 x;
519         unsigned i, skip;
520
521         /* skip the version and flags bytes */
522         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 24, read_callback_, decoder))
523                 return false; /* the read_callback_ sets the state for us */
524         /* get the size (in bytes) to skip */
525         skip = 0;
526         for(i = 0; i < 4; i++) {
527                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
528                         return false; /* the read_callback_ sets the state for us */
529                 skip <<= 7;
530                 skip |= (x & 0x7f);
531         }
532         /* skip the rest of the tag */
533         for(i = 0; i < skip; i++) {
534                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
535                         return false; /* the read_callback_ sets the state for us */
536         }
537         return true;
538 }
539
540 bool stream_decoder_frame_sync_(FLAC__StreamDecoder *decoder)
541 {
542         uint32 x;
543         bool first = 1;
544
545         /* If we know the total number of samples in the stream, stop if we've read that many. */
546         /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
547         if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.total_samples) {
548                 if(decoder->guts->samples_decoded >= decoder->guts->stream_header.data.encoding.total_samples) {
549                         decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
550                         return true;
551                 }
552         }
553
554         /* make sure we're byte aligned */
555         if(decoder->guts->input.consumed_bits != 0) {
556                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
557                         return false; /* the read_callback_ sets the state for us */
558         }
559
560         while(1) {
561                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
562                         return false; /* the read_callback_ sets the state for us */
563                 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
564                         unsigned y;
565                         if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
566                                 return false; /* the read_callback_ sets the state for us */
567                         if(!y) { /* MAGIC NUMBER for the last sync bit */
568                                 decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
569                                 return true;
570                         }
571                 }
572                 if(first) {
573                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
574                         first = 0;
575                 }
576         }
577
578         return true;
579 }
580
581 bool stream_decoder_read_frame_(FLAC__StreamDecoder *decoder, bool *got_a_frame)
582 {
583         unsigned channel;
584         unsigned i;
585         int32 mid, side, left, right;
586
587         *got_a_frame = false;
588
589         if(!stream_decoder_read_frame_header_(decoder))
590                 return false;
591         if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
592                 if(decoder->state == FLAC__STREAM_DECODER_RESYNC_IN_HEADER)
593                         decoder->state = FLAC__STREAM_DECODER_READ_FRAME;
594                 else
595                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
596                 return true;
597         }
598         if(!stream_decoder_allocate_output_(decoder, decoder->guts->frame_header.blocksize))
599                 return false;
600         for(channel = 0; channel < decoder->guts->frame_header.channels; channel++) {
601                 if(!stream_decoder_read_subframe_(decoder, channel))
602                         return false;
603                 if(decoder->state != FLAC__STREAM_DECODER_READ_FRAME) {
604                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
605                         return true;
606                 }
607         }
608         if(!stream_decoder_read_zero_padding_(decoder))
609                 return false;
610
611         /* Undo any special channel coding */
612         switch(decoder->guts->frame_header.channel_assignment) {
613                 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
614                         /* do nothing */
615                         break;
616                 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
617                         assert(decoder->guts->frame_header.channels == 2);
618                         for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
619                                 decoder->guts->output[1][i] = decoder->guts->output[0][i] - decoder->guts->output[1][i];
620                         break;
621                 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
622                         assert(decoder->guts->frame_header.channels == 2);
623                         for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
624                                 decoder->guts->output[0][i] += decoder->guts->output[1][i];
625                         break;
626                 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
627                         assert(decoder->guts->frame_header.channels == 2);
628                         for(i = 0; i < decoder->guts->frame_header.blocksize; i++) {
629                                 mid = decoder->guts->output[0][i];
630                                 side = decoder->guts->output[1][i];
631                                 mid <<= 1;
632                                 if(side & 1) /* i.e. if 'side' is odd... */
633                                         mid++;
634                                 left = mid + side;
635                                 right = mid - side;
636                                 decoder->guts->output[0][i] = left >> 1;
637                                 decoder->guts->output[1][i] = right >> 1;
638                         }
639                         break;
640                 default:
641                         assert(0);
642                         break;
643         }
644
645         *got_a_frame = true;
646
647         /* put the latest values into the public section of the decoder instance */
648         decoder->channels = decoder->guts->frame_header.channels;
649         decoder->channel_assignment = decoder->guts->frame_header.channel_assignment;
650         decoder->bits_per_sample = decoder->guts->frame_header.bits_per_sample;
651         decoder->sample_rate = decoder->guts->frame_header.sample_rate;
652         decoder->blocksize = decoder->guts->frame_header.blocksize;
653
654         decoder->guts->samples_decoded += decoder->guts->frame_header.blocksize;
655
656         /* write it */
657         if(decoder->guts->write_callback(decoder, &decoder->guts->frame_header, decoder->guts->output, decoder->guts->client_data) != FLAC__STREAM_DECODER_WRITE_CONTINUE)
658                 return false;
659
660         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
661         return true;
662 }
663
664 bool stream_decoder_read_frame_header_(FLAC__StreamDecoder *decoder)
665 {
666         uint32 x;
667         uint64 xx;
668         unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
669         byte crc, raw_header[15]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
670         unsigned raw_header_len;
671         bool is_unparseable = false;
672
673         assert(decoder->guts->input.consumed_bits == 0); /* make sure we're byte aligned */
674
675         /* init the raw header with the first 8 bits of the sync code */
676         raw_header[0] = 0xff; /* MAGIC NUMBER for the first 8 frame sync bits */
677         raw_header_len = 1;
678
679         /*
680          * read in the raw header as bytes so we can CRC it, and parse it on the way
681          */
682         for(i = 0; i < 2; i++) {
683                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
684                         return false; /* the read_callback_ sets the state for us */
685                 else if(x == 0xff) { /* MAGIC NUMBER for the first part of the sync code */
686                         /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
687                         uint32 y;
688                         if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
689                                 return false; /* the read_callback_ sets the state for us */
690                         if(!y) { /* MAGIC NUMBER for the last sync bit */
691                                 decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
692                                 return true;
693                         }
694                         else {
695                                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
696                                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
697                                 return true;
698                         }
699                 }
700                 raw_header[raw_header_len++] = (byte)x;
701         }
702         assert(!(raw_header[1] & 0x80)); /* last sync bit should be confirmed zero before we get here */
703
704         switch(x = raw_header[1] >> 4) {
705                 case 0:
706                         if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.min_blocksize == decoder->guts->stream_header.data.encoding.max_blocksize) /* i.e. it's a fixed-blocksize stream */
707                                 decoder->guts->frame_header.blocksize = decoder->guts->stream_header.data.encoding.min_blocksize;
708                         else
709                                 is_unparseable = true;
710                         break;
711                 case 1:
712                         decoder->guts->frame_header.blocksize = 192;
713                         break;
714                 case 2:
715                 case 3:
716                 case 4:
717                 case 5:
718                         decoder->guts->frame_header.blocksize = 576 << (x-2);
719                         break;
720                 case 6:
721                 case 7:
722                         blocksize_hint = x;
723                         break;
724                 default:
725                         assert(0);
726                         break;
727         }
728
729         switch(x = raw_header[1] & 0x0f) {
730                 case 0:
731                         if(decoder->guts->has_stream_header)
732                                 decoder->guts->frame_header.sample_rate = decoder->guts->stream_header.data.encoding.sample_rate;
733                         else
734                                 is_unparseable = true;
735                         break;
736                 case 1:
737                 case 2:
738                 case 3:
739                         is_unparseable = true;
740                         break;
741                 case 4:
742                         decoder->guts->frame_header.sample_rate = 8000;
743                         break;
744                 case 5:
745                         decoder->guts->frame_header.sample_rate = 16000;
746                         break;
747                 case 6:
748                         decoder->guts->frame_header.sample_rate = 22050;
749                         break;
750                 case 7:
751                         decoder->guts->frame_header.sample_rate = 24000;
752                         break;
753                 case 8:
754                         decoder->guts->frame_header.sample_rate = 32000;
755                         break;
756                 case 9:
757                         decoder->guts->frame_header.sample_rate = 44100;
758                         break;
759                 case 10:
760                         decoder->guts->frame_header.sample_rate = 48000;
761                         break;
762                 case 11:
763                         decoder->guts->frame_header.sample_rate = 96000;
764                         break;
765                 case 12:
766                 case 13:
767                 case 14:
768                         sample_rate_hint = x;
769                         break;
770                 case 15:
771                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
772                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
773                         return true;
774                 default:
775                         assert(0);
776         }
777
778         x = (unsigned)(raw_header[2] >> 4);
779         if(x & 8) {
780                 decoder->guts->frame_header.channels = 2;
781                 switch(x & 7) {
782                         case 0:
783                                 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
784                                 break;
785                         case 1:
786                                 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
787                                 break;
788                         case 2:
789                                 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
790                                 break;
791                         default:
792                                 is_unparseable = true;
793                                 break;
794                 }
795         }
796         else {
797                 decoder->guts->frame_header.channels = (unsigned)x + 1;
798                 decoder->guts->frame_header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
799         }
800
801         switch(x = (unsigned)(raw_header[2] & 0x0e) >> 1) {
802                 case 0:
803                         if(decoder->guts->has_stream_header)
804                                 decoder->guts->frame_header.bits_per_sample = decoder->guts->stream_header.data.encoding.bits_per_sample;
805                         else
806                                 is_unparseable = true;
807                         break;
808                 case 1:
809                         decoder->guts->frame_header.bits_per_sample = 8;
810                         break;
811                 case 2:
812                         decoder->guts->frame_header.bits_per_sample = 12;
813                         break;
814                 case 4:
815                         decoder->guts->frame_header.bits_per_sample = 16;
816                         break;
817                 case 5:
818                         decoder->guts->frame_header.bits_per_sample = 20;
819                         break;
820                 case 6:
821                         decoder->guts->frame_header.bits_per_sample = 24;
822                         break;
823                 case 3:
824                 case 7:
825                         is_unparseable = true;
826                         break;
827                 default:
828                         assert(0);
829                         break;
830         }
831
832         if(raw_header[2] & 0x01) { /* this should be a zero padding bit */
833                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
834                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
835                 return true;
836         }
837
838         if(blocksize_hint) {
839                 if(!FLAC__bitbuffer_read_utf8_uint64(&decoder->guts->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
840                         return false; /* the read_callback_ sets the state for us */
841                 if(xx == 0xffffffffffffffff) {
842                         if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
843                                 uint32 y;
844                                 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
845                                         return false; /* the read_callback_ sets the state for us */
846                                 if(!y) { /* MAGIC NUMBER for the last sync bit */
847                                         decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
848                                         return true;
849                                 }
850                                 else {
851                                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
852                                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
853                                         return true;
854                                 }
855                         }
856                         else {
857                                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
858                                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
859                                 return true;
860                         }
861                 }
862                 if(decoder->guts->has_stream_header && decoder->guts->stream_header.data.encoding.min_blocksize == decoder->guts->stream_header.data.encoding.max_blocksize) /* i.e. it's a fixed-blocksize stream */
863                         decoder->guts->frame_header.number.sample_number = (uint64)decoder->guts->last_frame_number * (int64)decoder->guts->stream_header.data.encoding.min_blocksize + xx;
864                 else
865                         decoder->guts->frame_header.number.sample_number = xx;
866         }
867         else {
868                 if(!FLAC__bitbuffer_read_utf8_uint32(&decoder->guts->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
869                         return false; /* the read_callback_ sets the state for us */
870                 if(x == 0xffffffff) {
871                         if(raw_header[raw_header_len-1] == 0xff) { /* MAGIC NUMBER for sync code */
872                                 uint32 y;
873                                 if(!FLAC__bitbuffer_peek_bit(&decoder->guts->input, &y, read_callback_, decoder))
874                                         return false; /* the read_callback_ sets the state for us */
875                                 if(!y) { /* MAGIC NUMBER for the last sync bit */
876                                         decoder->state = FLAC__STREAM_DECODER_RESYNC_IN_HEADER;
877                                         return true;
878                                 }
879                                 else {
880                                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
881                                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
882                                         return true;
883                                 }
884                         }
885                         else {
886                                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
887                                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
888                                 return true;
889                         }
890                 }
891                 decoder->guts->last_frame_number = x;
892                 if(decoder->guts->has_stream_header) {
893                         decoder->guts->frame_header.number.sample_number = (int64)decoder->guts->stream_header.data.encoding.min_blocksize * (int64)x;
894                 }
895                 else {
896                         is_unparseable = true;
897                 }
898         }
899
900         if(blocksize_hint) {
901                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
902                         return false; /* the read_callback_ sets the state for us */
903                 raw_header[raw_header_len++] = (byte)x;
904                 if(blocksize_hint == 7) {
905                         uint32 _x;
906                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
907                                 return false; /* the read_callback_ sets the state for us */
908                         raw_header[raw_header_len++] = (byte)_x;
909                         x = (x << 8) | _x;
910                 }
911                 decoder->guts->frame_header.blocksize = x+1;
912         }
913
914         if(sample_rate_hint) {
915                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
916                         return false; /* the read_callback_ sets the state for us */
917                 raw_header[raw_header_len++] = (byte)x;
918                 if(sample_rate_hint != 12) {
919                         uint32 _x;
920                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &_x, 8, read_callback_, decoder))
921                                 return false; /* the read_callback_ sets the state for us */
922                         raw_header[raw_header_len++] = (byte)_x;
923                         x = (x << 8) | _x;
924                 }
925                 if(sample_rate_hint == 12)
926                         decoder->guts->frame_header.sample_rate = x*1000;
927                 else if(sample_rate_hint == 13)
928                         decoder->guts->frame_header.sample_rate = x;
929                 else
930                         decoder->guts->frame_header.sample_rate = x*10;
931         }
932
933         /* read the crc byte */
934         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, 8, read_callback_, decoder))
935                 return false; /* the read_callback_ sets the state for us */
936         crc = (byte)x;
937
938         if(FLAC__crc8(raw_header, raw_header_len) != crc) {
939                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
940                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
941                 return true;
942         }
943
944         if(is_unparseable) {
945                 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
946                 return false;
947         }
948
949         return true;
950 }
951
952 bool stream_decoder_read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel)
953 {
954         uint32 x;
955
956         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &x, FLAC__SUBFRAME_HEADER_TYPE_LEN, read_callback_, decoder))
957                 return false; /* the read_callback_ sets the state for us */
958         if(x & 0x01 || x & 0x80) {
959                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
960                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
961                 return true;
962         }
963         else if(x == 0) {
964                 return stream_decoder_read_subframe_constant_(decoder, channel);
965         }
966         else if(x == 2) {
967                 return stream_decoder_read_subframe_verbatim_(decoder, channel);
968         }
969         else if(x < 16) {
970                 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
971                 return false;
972         }
973         else if(x <= 24) {
974                 return stream_decoder_read_subframe_fixed_(decoder, channel, (x>>1)&7);
975         }
976         else if(x < 64) {
977                 decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
978                 return false;
979         }
980         else {
981                 return stream_decoder_read_subframe_lpc_(decoder, channel, ((x>>1)&31)+1);
982         }
983 }
984
985 bool stream_decoder_read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel)
986 {
987         int32 x;
988         unsigned i;
989
990         if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
991                 return false; /* the read_callback_ sets the state for us */
992
993         for(i = 0; i < decoder->guts->frame_header.blocksize; i++)
994                 decoder->guts->output[channel][i] = x;
995
996         return true;
997 }
998
999 bool stream_decoder_read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order)
1000 {
1001         FLAC__SubframeHeader_Fixed subframe_header;
1002         int32 i32;
1003         uint32 u32;
1004         unsigned u;
1005
1006         subframe_header.order = order;
1007
1008         /* read warm-up samples */
1009         for(u = 0; u < order; u++) {
1010                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
1011                         return false; /* the read_callback_ sets the state for us */
1012                 decoder->guts->output[channel][u] = i32;
1013         }
1014
1015         /* read entropy coding method info */
1016         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1017                 return false; /* the read_callback_ sets the state for us */
1018         subframe_header.entropy_coding_method.type = u32;
1019         switch(subframe_header.entropy_coding_method.type) {
1020                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1021                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1022                                 return false; /* the read_callback_ sets the state for us */
1023                         subframe_header.entropy_coding_method.data.partitioned_rice.order = u32;
1024                         break;
1025                 default:
1026                         decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1027                         return false;
1028         }
1029
1030         /* read residual */
1031         switch(subframe_header.entropy_coding_method.type) {
1032                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1033                         if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe_header.entropy_coding_method.data.partitioned_rice.order))
1034                                 return false;
1035                         break;
1036                 default:
1037                         assert(0);
1038         }
1039
1040         /* decode the subframe */
1041         FLAC__fixed_restore_signal(decoder->guts->residual, decoder->guts->frame_header.blocksize-order, order, decoder->guts->output[channel]+order);
1042
1043         return true;
1044 }
1045
1046 bool stream_decoder_read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, const unsigned order)
1047 {
1048         FLAC__SubframeHeader_LPC subframe_header;
1049         int32 i32;
1050         uint32 u32;
1051         unsigned u;
1052
1053         subframe_header.order = order;
1054
1055         /* read warm-up samples */
1056         for(u = 0; u < order; u++) {
1057                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
1058                         return false; /* the read_callback_ sets the state for us */
1059                 decoder->guts->output[channel][u] = i32;
1060         }
1061
1062         /* read qlp coeff precision */
1063         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__SUBFRAME_HEADER_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
1064                 return false; /* the read_callback_ sets the state for us */
1065         if(u32 == 15) {
1066                 decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1067                 decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1068                 return true;
1069         }
1070         subframe_header.qlp_coeff_precision = u32+1;
1071
1072         /* read qlp shift */
1073         if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, FLAC__SUBFRAME_HEADER_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
1074                 return false; /* the read_callback_ sets the state for us */
1075         subframe_header.quantization_level = i32;
1076
1077         /* read quantized lp coefficiencts */
1078         for(u = 0; u < order; u++) {
1079                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &i32, subframe_header.qlp_coeff_precision, read_callback_, decoder))
1080                         return false; /* the read_callback_ sets the state for us */
1081                 subframe_header.qlp_coeff[u] = i32;
1082         }
1083
1084         /* read entropy coding method info */
1085         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
1086                 return false; /* the read_callback_ sets the state for us */
1087         subframe_header.entropy_coding_method.type = u32;
1088         switch(subframe_header.entropy_coding_method.type) {
1089                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1090                         if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
1091                                 return false; /* the read_callback_ sets the state for us */
1092                         subframe_header.entropy_coding_method.data.partitioned_rice.order = u32;
1093                         break;
1094                 default:
1095                         decoder->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
1096                         return false;
1097         }
1098
1099         /* read residual */
1100         switch(subframe_header.entropy_coding_method.type) {
1101                 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
1102                         if(!stream_decoder_read_residual_partitioned_rice_(decoder, order, subframe_header.entropy_coding_method.data.partitioned_rice.order))
1103                                 return false;
1104                         break;
1105                 default:
1106                         assert(0);
1107         }
1108
1109         /* decode the subframe */
1110         FLAC__lpc_restore_signal(decoder->guts->residual, decoder->guts->frame_header.blocksize-order, subframe_header.qlp_coeff, order, subframe_header.quantization_level, decoder->guts->output[channel]+order);
1111
1112         return true;
1113 }
1114
1115 bool stream_decoder_read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel)
1116 {
1117         int32 x;
1118         unsigned i;
1119
1120         for(i = 0; i < decoder->guts->frame_header.blocksize; i++) {
1121                 if(!FLAC__bitbuffer_read_raw_int32(&decoder->guts->input, &x, decoder->guts->frame_header.bits_per_sample, read_callback_, decoder))
1122                         return false; /* the read_callback_ sets the state for us */
1123                 decoder->guts->output[channel][i] = x;
1124         }
1125
1126         return true;
1127 }
1128
1129 bool stream_decoder_read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order)
1130 {
1131         uint32 rice_parameter;
1132         int i;
1133         unsigned partition, sample, u;
1134         const unsigned partitions = 1u << partition_order;
1135         const unsigned partition_samples = partition_order > 0? decoder->guts->frame_header.blocksize >> partition_order : decoder->guts->frame_header.blocksize - predictor_order;
1136
1137         sample = 0;
1138         for(partition = 0; partition < partitions; partition++) {
1139                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
1140                         return false; /* the read_callback_ sets the state for us */
1141                 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
1142                         if(!FLAC__bitbuffer_read_rice_signed(&decoder->guts->input, &i, rice_parameter, read_callback_, decoder))
1143                                 return false; /* the read_callback_ sets the state for us */
1144                         decoder->guts->residual[sample] = i;
1145                 }
1146         }
1147
1148         return true;
1149 }
1150
1151 bool stream_decoder_read_zero_padding_(FLAC__StreamDecoder *decoder)
1152 {
1153         if(decoder->guts->input.consumed_bits != 0) {
1154                 uint32 zero;
1155                 if(!FLAC__bitbuffer_read_raw_uint32(&decoder->guts->input, &zero, 8-decoder->guts->input.consumed_bits, read_callback_, decoder))
1156                         return false; /* the read_callback_ sets the state for us */
1157                 if(zero != 0) {
1158                         decoder->guts->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_LOST_SYNC, decoder->guts->client_data);
1159                         decoder->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1160                 }
1161         }
1162         return true;
1163 }
1164
1165 bool read_callback_(byte buffer[], unsigned *bytes, void *client_data)
1166 {
1167         FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
1168         FLAC__StreamDecoderReadStatus status;
1169         status = decoder->guts->read_callback(decoder, buffer, bytes, decoder->guts->client_data);
1170         if(status == FLAC__STREAM_DECODER_READ_END_OF_STREAM)
1171                 decoder->state = FLAC__STREAM_DECODER_END_OF_STREAM;
1172         else if(status == FLAC__STREAM_DECODER_READ_ABORT)
1173                 decoder->state = FLAC__STREAM_DECODER_ABORTED;
1174         return status == FLAC__STREAM_DECODER_READ_CONTINUE;
1175 }