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