blob: ca7b819f75ccb80bfbb35d42baa0f34a6523ea9e [file] [log] [blame]
Josh Coalson26560dd2001-02-08 00:38:41 +00001/* libFLAC - Free Lossless Audio Codec library
Josh Coalsona78fac62005-01-25 04:17:55 +00002 * Copyright (C) 2000,2001,2002,2003,2004,2005 Josh Coalson
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00003 *
Josh Coalsonafd81072003-01-31 23:34:56 +00004 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00007 *
Josh Coalsonafd81072003-01-31 23:34:56 +00008 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000010 *
Josh Coalsonafd81072003-01-31 23:34:56 +000011 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000030 */
31
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000032#include <stdio.h>
33#include <stdlib.h> /* for malloc() */
Josh Coalsonec1af652001-04-01 05:34:16 +000034#include <string.h> /* for memset/memcpy() */
Josh Coalson1b689822001-05-31 20:11:02 +000035#include "FLAC/assert.h"
Josh Coalson0a15c142001-06-13 17:59:57 +000036#include "protected/stream_decoder.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000037#include "private/bitbuffer.h"
Josh Coalson72582912002-10-26 04:33:49 +000038#include "private/bitmath.h"
Josh Coalsonb985bbc2001-05-23 21:59:52 +000039#include "private/cpu.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000040#include "private/crc.h"
41#include "private/fixed.h"
Josh Coalsonb7023aa2002-08-17 15:23:43 +000042#include "private/format.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000043#include "private/lpc.h"
Josh Coalson3aadd102004-07-27 01:13:16 +000044#include "private/memory.h"
Josh Coalsonbb7f6b92000-12-10 04:09:52 +000045
Josh Coalson5e31be12002-12-04 07:07:35 +000046#ifdef HAVE_CONFIG_H
47#include <config.h>
48#endif
49
Josh Coalsonb7023aa2002-08-17 15:23:43 +000050#ifdef max
51#undef max
52#endif
53#define max(a,b) ((a)>(b)?(a):(b))
54
Josh Coalson49623ea2004-07-29 06:28:59 +000055/* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
56#ifdef _MSC_VER
57#define FLAC__U64L(x) x
58#else
59#define FLAC__U64L(x) x##LLU
60#endif
61
Josh Coalson0a15c142001-06-13 17:59:57 +000062/***********************************************************************
63 *
64 * Private static data
65 *
66 ***********************************************************************/
67
Josh Coalson77e3f312001-06-23 03:03:24 +000068static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
Josh Coalson0a15c142001-06-13 17:59:57 +000069
70/***********************************************************************
71 *
72 * Private class method prototypes
73 *
74 ***********************************************************************/
75
Josh Coalson570db862002-07-31 06:58:16 +000076static void set_defaults_(FLAC__StreamDecoder *decoder);
77static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
78static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
79static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
80static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
Josh Coalson5f349562002-11-14 04:58:42 +000081static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
82static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
83static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
Josh Coalsone4869382002-11-15 05:41:48 +000084static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
Josh Coalson570db862002-07-31 06:58:16 +000085static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
86static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
Josh Coalson47f51b12004-07-16 00:53:38 +000087static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
Josh Coalson570db862002-07-31 06:58:16 +000088static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
Josh Coalson47f51b12004-07-16 00:53:38 +000089static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
90static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
91static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
92static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
93static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
Josh Coalsona37ba462002-08-19 21:36:39 +000094static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual);
Josh Coalson570db862002-07-31 06:58:16 +000095static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
Josh Coalson77e3f312001-06-23 03:03:24 +000096static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data);
Josh Coalson0a15c142001-06-13 17:59:57 +000097
98/***********************************************************************
99 *
100 * Private class data
101 *
102 ***********************************************************************/
103
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000104typedef struct FLAC__StreamDecoderPrivate {
Josh Coalson681c2932002-08-01 08:19:37 +0000105 FLAC__StreamDecoderReadCallback read_callback;
106 FLAC__StreamDecoderWriteCallback write_callback;
107 FLAC__StreamDecoderMetadataCallback metadata_callback;
108 FLAC__StreamDecoderErrorCallback error_callback;
Josh Coalson3aadd102004-07-27 01:13:16 +0000109 /* generic 32-bit datapath: */
Josh Coalson77e3f312001-06-23 03:03:24 +0000110 void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
Josh Coalson3aadd102004-07-27 01:13:16 +0000111 /* generic 64-bit datapath: */
Josh Coalsonb6a32192002-10-04 05:27:49 +0000112 void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
Josh Coalson3aadd102004-07-27 01:13:16 +0000113 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */
Josh Coalson77e3f312001-06-23 03:03:24 +0000114 void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
Josh Coalson3aadd102004-07-27 01:13:16 +0000115 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */
116 void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000117 void *client_data;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000118 FLAC__BitBuffer *input;
Josh Coalson77e3f312001-06-23 03:03:24 +0000119 FLAC__int32 *output[FLAC__MAX_CHANNELS];
Josh Coalson3aadd102004-07-27 01:13:16 +0000120 FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
Josh Coalsona37ba462002-08-19 21:36:39 +0000121 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
Josh Coalson1e1509f2001-03-23 20:20:43 +0000122 unsigned output_capacity, output_channels;
Josh Coalson77e3f312001-06-23 03:03:24 +0000123 FLAC__uint32 last_frame_number;
Josh Coalsonba056082005-01-24 00:20:49 +0000124 FLAC__uint32 last_block_size;
Josh Coalson77e3f312001-06-23 03:03:24 +0000125 FLAC__uint64 samples_decoded;
126 FLAC__bool has_stream_info, has_seek_table;
Josh Coalsoncc682512002-06-08 04:53:42 +0000127 FLAC__StreamMetadata stream_info;
128 FLAC__StreamMetadata seek_table;
Josh Coalsond0609472003-01-10 05:37:13 +0000129 FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000130 FLAC__byte *metadata_filter_ids;
131 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
Josh Coalson6dcea512001-01-23 23:07:36 +0000132 FLAC__Frame frame;
Josh Coalson77e3f312001-06-23 03:03:24 +0000133 FLAC__bool cached; /* true if there is a byte in lookahead */
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000134 FLAC__CPUInfo cpuinfo;
Josh Coalson77e3f312001-06-23 03:03:24 +0000135 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
136 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
Josh Coalson3aadd102004-07-27 01:13:16 +0000137 /* unaligned (original) pointers to allocated data */
138 FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000139} FLAC__StreamDecoderPrivate;
140
Josh Coalson0a15c142001-06-13 17:59:57 +0000141/***********************************************************************
142 *
143 * Public static class data
144 *
145 ***********************************************************************/
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000146
Josh Coalson6afed9f2002-10-16 22:29:47 +0000147FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000148 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
149 "FLAC__STREAM_DECODER_READ_METADATA",
150 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
151 "FLAC__STREAM_DECODER_READ_FRAME",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000152 "FLAC__STREAM_DECODER_END_OF_STREAM",
153 "FLAC__STREAM_DECODER_ABORTED",
154 "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM",
155 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
Josh Coalson0a15c142001-06-13 17:59:57 +0000156 "FLAC__STREAM_DECODER_ALREADY_INITIALIZED",
Josh Coalson00e53872001-06-16 07:32:25 +0000157 "FLAC__STREAM_DECODER_INVALID_CALLBACK",
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000158 "FLAC__STREAM_DECODER_UNINITIALIZED"
159};
160
Josh Coalson6afed9f2002-10-16 22:29:47 +0000161FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
Josh Coalson090f8f72002-06-04 05:53:04 +0000162 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
163 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
164 "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000165};
166
Josh Coalson6afed9f2002-10-16 22:29:47 +0000167FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
Josh Coalson090f8f72002-06-04 05:53:04 +0000168 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
169 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000170};
171
Josh Coalson6afed9f2002-10-16 22:29:47 +0000172FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
Josh Coalson090f8f72002-06-04 05:53:04 +0000173 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
174 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
175 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH"
Josh Coalsoncbf595f2000-12-22 22:35:33 +0000176};
177
Josh Coalson0a15c142001-06-13 17:59:57 +0000178/***********************************************************************
179 *
180 * Class constructor/destructor
181 *
182 ***********************************************************************/
Josh Coalson6afed9f2002-10-16 22:29:47 +0000183FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new()
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000184{
Josh Coalson0a15c142001-06-13 17:59:57 +0000185 FLAC__StreamDecoder *decoder;
Josh Coalsonbc869502002-06-14 06:36:16 +0000186 unsigned i;
Josh Coalson0a15c142001-06-13 17:59:57 +0000187
188 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
189
Josh Coalsonea7155f2002-10-18 05:49:19 +0000190 decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
Josh Coalson0a15c142001-06-13 17:59:57 +0000191 if(decoder == 0) {
192 return 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000193 }
Josh Coalsona7e4fb22002-08-03 22:05:11 +0000194
Josh Coalsonea7155f2002-10-18 05:49:19 +0000195 decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000196 if(decoder->protected_ == 0) {
Josh Coalson0a15c142001-06-13 17:59:57 +0000197 free(decoder);
198 return 0;
199 }
Josh Coalsona7e4fb22002-08-03 22:05:11 +0000200
Josh Coalsonea7155f2002-10-18 05:49:19 +0000201 decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
Josh Coalsonfa697a92001-08-16 20:07:29 +0000202 if(decoder->private_ == 0) {
203 free(decoder->protected_);
Josh Coalson0a15c142001-06-13 17:59:57 +0000204 free(decoder);
205 return 0;
206 }
Josh Coalsona7e4fb22002-08-03 22:05:11 +0000207
Josh Coalsonaec256b2002-03-12 16:19:54 +0000208 decoder->private_->input = FLAC__bitbuffer_new();
209 if(decoder->private_->input == 0) {
210 free(decoder->private_);
211 free(decoder->protected_);
212 free(decoder);
213 return 0;
214 }
Josh Coalson0a15c142001-06-13 17:59:57 +0000215
Josh Coalson6b02a752002-05-10 06:42:02 +0000216 decoder->private_->metadata_filter_ids_capacity = 16;
Josh Coalson5e3fcb22002-11-06 07:11:26 +0000217 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
Josh Coalson6b02a752002-05-10 06:42:02 +0000218 FLAC__bitbuffer_delete(decoder->private_->input);
219 free(decoder->private_);
220 free(decoder->protected_);
221 free(decoder);
222 return 0;
223 }
Josh Coalson090f8f72002-06-04 05:53:04 +0000224
Josh Coalsonbc869502002-06-14 06:36:16 +0000225 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
226 decoder->private_->output[i] = 0;
Josh Coalson3aadd102004-07-27 01:13:16 +0000227 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
Josh Coalsonbc869502002-06-14 06:36:16 +0000228 }
229
230 decoder->private_->output_capacity = 0;
231 decoder->private_->output_channels = 0;
232 decoder->private_->has_seek_table = false;
Josh Coalsona37ba462002-08-19 21:36:39 +0000233
234 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
235 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
Josh Coalsonbc869502002-06-14 06:36:16 +0000236
Josh Coalson570db862002-07-31 06:58:16 +0000237 set_defaults_(decoder);
Josh Coalson6b02a752002-05-10 06:42:02 +0000238
Josh Coalsonbc869502002-06-14 06:36:16 +0000239 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
240
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000241 return decoder;
242}
243
Josh Coalson6afed9f2002-10-16 22:29:47 +0000244FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000245{
Josh Coalsona37ba462002-08-19 21:36:39 +0000246 unsigned i;
247
Josh Coalson570db862002-07-31 06:58:16 +0000248 FLAC__ASSERT(0 != decoder);
249 FLAC__ASSERT(0 != decoder->protected_);
250 FLAC__ASSERT(0 != decoder->private_);
251 FLAC__ASSERT(0 != decoder->private_->input);
Josh Coalson0a15c142001-06-13 17:59:57 +0000252
Josh Coalsonbc869502002-06-14 06:36:16 +0000253 FLAC__stream_decoder_finish(decoder);
254
Josh Coalson570db862002-07-31 06:58:16 +0000255 if(0 != decoder->private_->metadata_filter_ids)
Josh Coalson090f8f72002-06-04 05:53:04 +0000256 free(decoder->private_->metadata_filter_ids);
257
Josh Coalsonaec256b2002-03-12 16:19:54 +0000258 FLAC__bitbuffer_delete(decoder->private_->input);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000259
Josh Coalsona37ba462002-08-19 21:36:39 +0000260 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
261 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000262
Josh Coalsonfa697a92001-08-16 20:07:29 +0000263 free(decoder->private_);
264 free(decoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000265 free(decoder);
266}
267
Josh Coalson0a15c142001-06-13 17:59:57 +0000268/***********************************************************************
269 *
270 * Public class methods
271 *
272 ***********************************************************************/
273
Josh Coalson6afed9f2002-10-16 22:29:47 +0000274FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000275{
Josh Coalson570db862002-07-31 06:58:16 +0000276 FLAC__ASSERT(0 != decoder);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000277
Josh Coalsonfa697a92001-08-16 20:07:29 +0000278 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
279 return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000280
Josh Coalsonfa697a92001-08-16 20:07:29 +0000281 if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback)
282 return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000283
Josh Coalsonaec256b2002-03-12 16:19:54 +0000284 if(!FLAC__bitbuffer_init(decoder->private_->input))
285 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000286
Josh Coalsonfa697a92001-08-16 20:07:29 +0000287 decoder->private_->last_frame_number = 0;
Josh Coalsonba056082005-01-24 00:20:49 +0000288 decoder->private_->last_block_size = 0;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000289 decoder->private_->samples_decoded = 0;
290 decoder->private_->has_stream_info = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000291 decoder->private_->cached = false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000292
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000293 /*
294 * get the CPU info and set the function pointers
295 */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000296 FLAC__cpu_info(&decoder->private_->cpuinfo);
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000297 /* first default to the non-asm routines */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000298 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
Josh Coalsonb6a32192002-10-04 05:27:49 +0000299 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000300 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
Josh Coalson3aadd102004-07-27 01:13:16 +0000301 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000302 /* now override with asm where appropriate */
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000303#ifndef FLAC__NO_ASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000304 if(decoder->private_->cpuinfo.use_asm) {
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000305#ifdef FLAC__CPU_IA32
Josh Coalsonfa697a92001-08-16 20:07:29 +0000306 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
Josh Coalson034d38e2001-05-24 19:29:30 +0000307#ifdef FLAC__HAS_NASM
Josh Coalsonfa697a92001-08-16 20:07:29 +0000308 if(decoder->private_->cpuinfo.data.ia32.mmx) {
309 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
310 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
Josh Coalson3aadd102004-07-27 01:13:16 +0000311 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000312 }
313 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000314 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
315 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
Josh Coalson3aadd102004-07-27 01:13:16 +0000316 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
Josh Coalson021ad3b2001-07-18 00:25:52 +0000317 }
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000318#endif
Josh Coalson3aadd102004-07-27 01:13:16 +0000319#elif defined FLAC__CPU_PPC
320 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC);
321 if(decoder->private_->cpuinfo.data.ppc.altivec) {
322 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16;
323 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8;
324 }
Josh Coalson034d38e2001-05-24 19:29:30 +0000325#endif
Josh Coalson021ad3b2001-07-18 00:25:52 +0000326 }
Josh Coalsona3f7c2c2001-05-25 00:04:45 +0000327#endif
Josh Coalsonb985bbc2001-05-23 21:59:52 +0000328
Josh Coalsoncfdfc822002-08-02 06:12:36 +0000329 if(!FLAC__stream_decoder_reset(decoder))
330 return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
331
332 return decoder->protected_->state;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000333}
334
Josh Coalson6afed9f2002-10-16 22:29:47 +0000335FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000336{
337 unsigned i;
Josh Coalson570db862002-07-31 06:58:16 +0000338 FLAC__ASSERT(0 != decoder);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000339 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000340 return;
Josh Coalson4fa90592002-12-04 07:01:37 +0000341 if(0 != decoder->private_->seek_table.data.seek_table.points) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000342 free(decoder->private_->seek_table.data.seek_table.points);
343 decoder->private_->seek_table.data.seek_table.points = 0;
Josh Coalsonbc869502002-06-14 06:36:16 +0000344 decoder->private_->has_seek_table = false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000345 }
Josh Coalsonaec256b2002-03-12 16:19:54 +0000346 FLAC__bitbuffer_free(decoder->private_->input);
Josh Coalson0a15c142001-06-13 17:59:57 +0000347 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000348 /* WATCHOUT:
349 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
350 * output arrays have a buffer of up to 3 zeroes in front
351 * (at negative indices) for alignment purposes; we use 4
352 * to keep the data well-aligned.
353 */
Josh Coalson570db862002-07-31 06:58:16 +0000354 if(0 != decoder->private_->output[i]) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000355 free(decoder->private_->output[i]-4);
356 decoder->private_->output[i] = 0;
Josh Coalson0a15c142001-06-13 17:59:57 +0000357 }
Josh Coalson3aadd102004-07-27 01:13:16 +0000358 if(0 != decoder->private_->residual_unaligned[i]) {
359 free(decoder->private_->residual_unaligned[i]);
360 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
Josh Coalson0a15c142001-06-13 17:59:57 +0000361 }
362 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000363 decoder->private_->output_capacity = 0;
364 decoder->private_->output_channels = 0;
Josh Coalson090f8f72002-06-04 05:53:04 +0000365
Josh Coalson570db862002-07-31 06:58:16 +0000366 set_defaults_(decoder);
Josh Coalson090f8f72002-06-04 05:53:04 +0000367
Josh Coalsonfa697a92001-08-16 20:07:29 +0000368 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
Josh Coalson0a15c142001-06-13 17:59:57 +0000369}
370
Josh Coalson6afed9f2002-10-16 22:29:47 +0000371FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +0000372{
Josh Coalson570db862002-07-31 06:58:16 +0000373 FLAC__ASSERT(0 != decoder);
374 FLAC__ASSERT(0 != decoder->private_);
375 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000376 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000377 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000378 decoder->private_->read_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000379 return true;
380}
381
Josh Coalson6afed9f2002-10-16 22:29:47 +0000382FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +0000383{
Josh Coalson570db862002-07-31 06:58:16 +0000384 FLAC__ASSERT(0 != decoder);
385 FLAC__ASSERT(0 != decoder->private_);
386 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000387 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000388 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000389 decoder->private_->write_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000390 return true;
391}
392
Josh Coalson6afed9f2002-10-16 22:29:47 +0000393FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +0000394{
Josh Coalson570db862002-07-31 06:58:16 +0000395 FLAC__ASSERT(0 != decoder);
396 FLAC__ASSERT(0 != decoder->private_);
397 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000398 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000399 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000400 decoder->private_->metadata_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000401 return true;
402}
403
Josh Coalson6afed9f2002-10-16 22:29:47 +0000404FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value)
Josh Coalson00e53872001-06-16 07:32:25 +0000405{
Josh Coalson570db862002-07-31 06:58:16 +0000406 FLAC__ASSERT(0 != decoder);
407 FLAC__ASSERT(0 != decoder->private_);
408 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000409 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000410 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000411 decoder->private_->error_callback = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000412 return true;
413}
414
Josh Coalson6afed9f2002-10-16 22:29:47 +0000415FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value)
Josh Coalson00e53872001-06-16 07:32:25 +0000416{
Josh Coalson570db862002-07-31 06:58:16 +0000417 FLAC__ASSERT(0 != decoder);
418 FLAC__ASSERT(0 != decoder->private_);
419 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000420 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
Josh Coalson00e53872001-06-16 07:32:25 +0000421 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +0000422 decoder->private_->client_data = value;
Josh Coalson00e53872001-06-16 07:32:25 +0000423 return true;
424}
425
Josh Coalson6afed9f2002-10-16 22:29:47 +0000426FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000427{
Josh Coalson570db862002-07-31 06:58:16 +0000428 FLAC__ASSERT(0 != decoder);
429 FLAC__ASSERT(0 != decoder->private_);
430 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson1cb23412004-07-22 01:32:00 +0000431 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
Josh Coalsond0609472003-01-10 05:37:13 +0000432 /* double protection */
Josh Coalson1cb23412004-07-22 01:32:00 +0000433 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
Josh Coalsond0609472003-01-10 05:37:13 +0000434 return false;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000435 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
436 return false;
437 decoder->private_->metadata_filter[type] = true;
438 if(type == FLAC__METADATA_TYPE_APPLICATION)
439 decoder->private_->metadata_filter_ids_count = 0;
440 return true;
441}
442
Josh Coalson6afed9f2002-10-16 22:29:47 +0000443FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000444{
Josh Coalson570db862002-07-31 06:58:16 +0000445 FLAC__ASSERT(0 != decoder);
446 FLAC__ASSERT(0 != decoder->private_);
447 FLAC__ASSERT(0 != decoder->protected_);
448 FLAC__ASSERT(0 != id);
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000449 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
450 return false;
451
452 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
453 return true;
454
455 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
456
457 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
Josh Coalsond9264962005-09-03 01:41:11 +0000458 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
459 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
460 return false;
461 }
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000462 decoder->private_->metadata_filter_ids_capacity *= 2;
463 }
464
465 memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
466 decoder->private_->metadata_filter_ids_count++;
467
468 return true;
469}
470
Josh Coalson6afed9f2002-10-16 22:29:47 +0000471FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000472{
Josh Coalson090f8f72002-06-04 05:53:04 +0000473 unsigned i;
Josh Coalson570db862002-07-31 06:58:16 +0000474 FLAC__ASSERT(0 != decoder);
475 FLAC__ASSERT(0 != decoder->private_);
476 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000477 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
478 return false;
Josh Coalson090f8f72002-06-04 05:53:04 +0000479 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
480 decoder->private_->metadata_filter[i] = true;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000481 decoder->private_->metadata_filter_ids_count = 0;
482 return true;
483}
484
Josh Coalson6afed9f2002-10-16 22:29:47 +0000485FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000486{
Josh Coalson570db862002-07-31 06:58:16 +0000487 FLAC__ASSERT(0 != decoder);
488 FLAC__ASSERT(0 != decoder->private_);
489 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalson1cb23412004-07-22 01:32:00 +0000490 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
Josh Coalsond0609472003-01-10 05:37:13 +0000491 /* double protection */
Josh Coalson1cb23412004-07-22 01:32:00 +0000492 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
Josh Coalsond0609472003-01-10 05:37:13 +0000493 return false;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000494 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
495 return false;
496 decoder->private_->metadata_filter[type] = false;
497 if(type == FLAC__METADATA_TYPE_APPLICATION)
498 decoder->private_->metadata_filter_ids_count = 0;
499 return true;
500}
501
Josh Coalson6afed9f2002-10-16 22:29:47 +0000502FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000503{
Josh Coalson570db862002-07-31 06:58:16 +0000504 FLAC__ASSERT(0 != decoder);
505 FLAC__ASSERT(0 != decoder->private_);
506 FLAC__ASSERT(0 != decoder->protected_);
507 FLAC__ASSERT(0 != id);
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000508 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
509 return false;
510
511 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
512 return true;
513
514 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
515
516 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
Josh Coalsond9264962005-09-03 01:41:11 +0000517 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
518 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
519 return false;
520 }
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000521 decoder->private_->metadata_filter_ids_capacity *= 2;
522 }
523
524 memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8));
525 decoder->private_->metadata_filter_ids_count++;
526
527 return true;
528}
529
Josh Coalson6afed9f2002-10-16 22:29:47 +0000530FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000531{
Josh Coalson570db862002-07-31 06:58:16 +0000532 FLAC__ASSERT(0 != decoder);
533 FLAC__ASSERT(0 != decoder->private_);
534 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000535 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
536 return false;
537 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
538 decoder->private_->metadata_filter_ids_count = 0;
539 return true;
540}
541
Josh Coalson6afed9f2002-10-16 22:29:47 +0000542FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000543{
Josh Coalson570db862002-07-31 06:58:16 +0000544 FLAC__ASSERT(0 != decoder);
545 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000546 return decoder->protected_->state;
Josh Coalson0a15c142001-06-13 17:59:57 +0000547}
548
Josh Coalsond36b3462003-09-24 22:08:00 +0000549FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
550{
551 return FLAC__StreamDecoderStateString[decoder->protected_->state];
552}
553
Josh Coalson6afed9f2002-10-16 22:29:47 +0000554FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000555{
Josh Coalson570db862002-07-31 06:58:16 +0000556 FLAC__ASSERT(0 != decoder);
557 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000558 return decoder->protected_->channels;
Josh Coalson0a15c142001-06-13 17:59:57 +0000559}
560
Josh Coalson6afed9f2002-10-16 22:29:47 +0000561FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000562{
Josh Coalson570db862002-07-31 06:58:16 +0000563 FLAC__ASSERT(0 != decoder);
564 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000565 return decoder->protected_->channel_assignment;
Josh Coalson0a15c142001-06-13 17:59:57 +0000566}
567
Josh Coalson6afed9f2002-10-16 22:29:47 +0000568FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000569{
Josh Coalson570db862002-07-31 06:58:16 +0000570 FLAC__ASSERT(0 != decoder);
571 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000572 return decoder->protected_->bits_per_sample;
Josh Coalson0a15c142001-06-13 17:59:57 +0000573}
574
Josh Coalson6afed9f2002-10-16 22:29:47 +0000575FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000576{
Josh Coalson570db862002-07-31 06:58:16 +0000577 FLAC__ASSERT(0 != decoder);
578 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000579 return decoder->protected_->sample_rate;
Josh Coalson0a15c142001-06-13 17:59:57 +0000580}
581
Josh Coalson6afed9f2002-10-16 22:29:47 +0000582FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
Josh Coalson0a15c142001-06-13 17:59:57 +0000583{
Josh Coalson570db862002-07-31 06:58:16 +0000584 FLAC__ASSERT(0 != decoder);
585 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000586 return decoder->protected_->blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000587}
588
Josh Coalson6afed9f2002-10-16 22:29:47 +0000589FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000590{
Josh Coalson570db862002-07-31 06:58:16 +0000591 FLAC__ASSERT(0 != decoder);
592 FLAC__ASSERT(0 != decoder->private_);
593 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000594
Josh Coalsonaec256b2002-03-12 16:19:54 +0000595 if(!FLAC__bitbuffer_clear(decoder->private_->input)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000596 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000597 return false;
598 }
Josh Coalsonba056082005-01-24 00:20:49 +0000599 decoder->private_->last_frame_number = 0;
600 decoder->private_->last_block_size = 0;
Josh Coalsonb312f282001-11-01 05:23:48 +0000601 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000602
603 return true;
604}
605
Josh Coalson6afed9f2002-10-16 22:29:47 +0000606FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000607{
Josh Coalson570db862002-07-31 06:58:16 +0000608 FLAC__ASSERT(0 != decoder);
609 FLAC__ASSERT(0 != decoder->private_);
610 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000611
612 if(!FLAC__stream_decoder_flush(decoder)) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000613 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000614 return false;
615 }
Josh Coalsonfa697a92001-08-16 20:07:29 +0000616 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000617
Josh Coalsonfa697a92001-08-16 20:07:29 +0000618 decoder->private_->samples_decoded = 0;
Josh Coalson671fadb2001-04-17 17:32:01 +0000619
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000620 return true;
621}
622
Josh Coalson6afed9f2002-10-16 22:29:47 +0000623FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000624{
Josh Coalson77e3f312001-06-23 03:03:24 +0000625 FLAC__bool got_a_frame;
Josh Coalson570db862002-07-31 06:58:16 +0000626 FLAC__ASSERT(0 != decoder);
Josh Coalsoncfdfc822002-08-02 06:12:36 +0000627 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000628
629 while(1) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000630 switch(decoder->protected_->state) {
Josh Coalsoncfdfc822002-08-02 06:12:36 +0000631 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
632 if(!find_metadata_(decoder))
633 return false; /* above function sets the status for us */
634 break;
635 case FLAC__STREAM_DECODER_READ_METADATA:
636 if(!read_metadata_(decoder))
637 return false; /* above function sets the status for us */
638 else
639 return true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000640 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
Josh Coalson570db862002-07-31 06:58:16 +0000641 if(!frame_sync_(decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000642 return true; /* above function sets the status for us */
643 break;
644 case FLAC__STREAM_DECODER_READ_FRAME:
Josh Coalson47f51b12004-07-16 00:53:38 +0000645 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000646 return false; /* above function sets the status for us */
647 if(got_a_frame)
648 return true; /* above function sets the status for us */
649 break;
650 case FLAC__STREAM_DECODER_END_OF_STREAM:
Josh Coalson93df7e02002-07-24 06:08:37 +0000651 case FLAC__STREAM_DECODER_ABORTED:
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000652 return true;
653 default:
Josh Coalson1b689822001-05-31 20:11:02 +0000654 FLAC__ASSERT(0);
Josh Coalsoncfdfc822002-08-02 06:12:36 +0000655 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000656 }
657 }
658}
659
Josh Coalson6afed9f2002-10-16 22:29:47 +0000660FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000661{
Josh Coalson570db862002-07-31 06:58:16 +0000662 FLAC__ASSERT(0 != decoder);
Josh Coalsoncfdfc822002-08-02 06:12:36 +0000663 FLAC__ASSERT(0 != decoder->protected_);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000664
665 while(1) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000666 switch(decoder->protected_->state) {
Josh Coalsoncfdfc822002-08-02 06:12:36 +0000667 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
668 if(!find_metadata_(decoder))
669 return false; /* above function sets the status for us */
670 break;
671 case FLAC__STREAM_DECODER_READ_METADATA:
672 if(!read_metadata_(decoder))
673 return false; /* above function sets the status for us */
674 break;
675 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
676 case FLAC__STREAM_DECODER_READ_FRAME:
677 case FLAC__STREAM_DECODER_END_OF_STREAM:
678 case FLAC__STREAM_DECODER_ABORTED:
679 return true;
680 default:
681 FLAC__ASSERT(0);
682 return false;
683 }
684 }
685}
686
Josh Coalson6afed9f2002-10-16 22:29:47 +0000687FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
Josh Coalsoncfdfc822002-08-02 06:12:36 +0000688{
689 FLAC__bool dummy;
690 FLAC__ASSERT(0 != decoder);
691 FLAC__ASSERT(0 != decoder->protected_);
692
693 while(1) {
694 switch(decoder->protected_->state) {
695 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
696 if(!find_metadata_(decoder))
697 return false; /* above function sets the status for us */
698 break;
699 case FLAC__STREAM_DECODER_READ_METADATA:
700 if(!read_metadata_(decoder))
701 return false; /* above function sets the status for us */
702 break;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000703 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
Josh Coalson570db862002-07-31 06:58:16 +0000704 if(!frame_sync_(decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000705 return true; /* above function sets the status for us */
706 break;
707 case FLAC__STREAM_DECODER_READ_FRAME:
Josh Coalson47f51b12004-07-16 00:53:38 +0000708 if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000709 return false; /* above function sets the status for us */
710 break;
711 case FLAC__STREAM_DECODER_END_OF_STREAM:
Josh Coalson93df7e02002-07-24 06:08:37 +0000712 case FLAC__STREAM_DECODER_ABORTED:
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000713 return true;
714 default:
Josh Coalson1b689822001-05-31 20:11:02 +0000715 FLAC__ASSERT(0);
Josh Coalsoncfdfc822002-08-02 06:12:36 +0000716 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000717 }
718 }
719}
720
Josh Coalson47f51b12004-07-16 00:53:38 +0000721FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
722{
723 FLAC__bool got_a_frame;
724 FLAC__ASSERT(0 != decoder);
725 FLAC__ASSERT(0 != decoder->protected_);
726
727 while(1) {
728 switch(decoder->protected_->state) {
729 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
730 case FLAC__STREAM_DECODER_READ_METADATA:
731 return false; /* above function sets the status for us */
732 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
733 if(!frame_sync_(decoder))
734 return true; /* above function sets the status for us */
735 break;
736 case FLAC__STREAM_DECODER_READ_FRAME:
737 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
738 return false; /* above function sets the status for us */
739 if(got_a_frame)
740 return true; /* above function sets the status for us */
741 break;
742 case FLAC__STREAM_DECODER_END_OF_STREAM:
743 case FLAC__STREAM_DECODER_ABORTED:
744 return true;
745 default:
746 FLAC__ASSERT(0);
747 return false;
748 }
749 }
750}
751
Josh Coalson0a15c142001-06-13 17:59:57 +0000752/***********************************************************************
753 *
754 * Protected class methods
755 *
756 ***********************************************************************/
757
Josh Coalson00e53872001-06-16 07:32:25 +0000758unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000759{
Josh Coalson570db862002-07-31 06:58:16 +0000760 FLAC__ASSERT(0 != decoder);
Josh Coalsonaec256b2002-03-12 16:19:54 +0000761 return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000762}
763
Josh Coalson0a15c142001-06-13 17:59:57 +0000764/***********************************************************************
765 *
766 * Private class methods
767 *
768 ***********************************************************************/
769
Josh Coalson570db862002-07-31 06:58:16 +0000770void set_defaults_(FLAC__StreamDecoder *decoder)
Josh Coalson090f8f72002-06-04 05:53:04 +0000771{
772 decoder->private_->read_callback = 0;
773 decoder->private_->write_callback = 0;
774 decoder->private_->metadata_callback = 0;
775 decoder->private_->error_callback = 0;
776 decoder->private_->client_data = 0;
777
778 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
779 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
780 decoder->private_->metadata_filter_ids_count = 0;
781}
782
Josh Coalson570db862002-07-31 06:58:16 +0000783FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000784{
785 unsigned i;
Josh Coalson77e3f312001-06-23 03:03:24 +0000786 FLAC__int32 *tmp;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000787
Josh Coalsonfa697a92001-08-16 20:07:29 +0000788 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000789 return true;
790
Josh Coalson5dcb57d2002-09-04 07:59:02 +0000791 /* simply using realloc() is not practical because the number of channels may change mid-stream */
Josh Coalson6dcea512001-01-23 23:07:36 +0000792
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000793 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
Josh Coalson570db862002-07-31 06:58:16 +0000794 if(0 != decoder->private_->output[i]) {
Josh Coalson2c718a22002-10-21 06:43:48 +0000795 free(decoder->private_->output[i]-4);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000796 decoder->private_->output[i] = 0;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000797 }
Josh Coalson3aadd102004-07-27 01:13:16 +0000798 if(0 != decoder->private_->residual_unaligned[i]) {
799 free(decoder->private_->residual_unaligned[i]);
800 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
Josh Coalson6dcea512001-01-23 23:07:36 +0000801 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000802 }
803
Josh Coalson1e1509f2001-03-23 20:20:43 +0000804 for(i = 0; i < channels; i++) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +0000805 /* WATCHOUT:
806 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
807 * output arrays have a buffer of up to 3 zeroes in front
808 * (at negative indices) for alignment purposes; we use 4
809 * to keep the data well-aligned.
810 */
Josh Coalson63d99572001-07-12 21:26:57 +0000811 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000812 if(tmp == 0) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000813 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000814 return false;
815 }
Josh Coalson63d99572001-07-12 21:26:57 +0000816 memset(tmp, 0, sizeof(FLAC__int32)*4);
Josh Coalsonfa697a92001-08-16 20:07:29 +0000817 decoder->private_->output[i] = tmp + 4;
Josh Coalson6dcea512001-01-23 23:07:36 +0000818
Josh Coalson3aadd102004-07-27 01:13:16 +0000819 /* WATCHOUT:
820 * minimum of quadword alignment for PPC vector optimizations is REQUIRED:
821 */
822 if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000823 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
Josh Coalson6dcea512001-01-23 23:07:36 +0000824 return false;
825 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000826 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000827
Josh Coalsonfa697a92001-08-16 20:07:29 +0000828 decoder->private_->output_capacity = size;
829 decoder->private_->output_channels = channels;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000830
831 return true;
832}
833
Josh Coalson570db862002-07-31 06:58:16 +0000834FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000835{
836 unsigned i;
837
838 FLAC__ASSERT(0 != decoder);
839 FLAC__ASSERT(0 != decoder->private_);
840
841 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
842 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
843 return true;
844
845 return false;
846}
847
Josh Coalson570db862002-07-31 06:58:16 +0000848FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000849{
Josh Coalson77e3f312001-06-23 03:03:24 +0000850 FLAC__uint32 x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000851 unsigned i, id;
Josh Coalson77e3f312001-06-23 03:03:24 +0000852 FLAC__bool first = true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000853
Josh Coalsonaec256b2002-03-12 16:19:54 +0000854 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000855
856 for(i = id = 0; i < 4; ) {
Josh Coalsonfa697a92001-08-16 20:07:29 +0000857 if(decoder->private_->cached) {
858 x = (FLAC__uint32)decoder->private_->lookahead;
859 decoder->private_->cached = false;
Josh Coalson215af572001-03-27 01:15:58 +0000860 }
861 else {
Josh Coalsonaec256b2002-03-12 16:19:54 +0000862 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson215af572001-03-27 01:15:58 +0000863 return false; /* the read_callback_ sets the state for us */
864 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000865 if(x == FLAC__STREAM_SYNC_STRING[i]) {
Josh Coalson215af572001-03-27 01:15:58 +0000866 first = true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000867 i++;
868 id = 0;
869 continue;
870 }
871 if(x == ID3V2_TAG_[id]) {
872 id++;
873 i = 0;
874 if(id == 3) {
Josh Coalson570db862002-07-31 06:58:16 +0000875 if(!skip_id3v2_tag_(decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000876 return false; /* the read_callback_ sets the state for us */
877 }
878 continue;
879 }
880 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000881 decoder->private_->header_warmup[0] = (FLAC__byte)x;
Josh Coalsonaec256b2002-03-12 16:19:54 +0000882 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000883 return false; /* the read_callback_ sets the state for us */
Josh Coalson215af572001-03-27 01:15:58 +0000884
885 /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
886 /* else we have to check if the second byte is the end of a sync code */
887 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000888 decoder->private_->lookahead = (FLAC__byte)x;
889 decoder->private_->cached = true;
Josh Coalson215af572001-03-27 01:15:58 +0000890 }
891 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
Josh Coalsonfa697a92001-08-16 20:07:29 +0000892 decoder->private_->header_warmup[1] = (FLAC__byte)x;
893 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000894 return true;
895 }
896 }
897 i = 0;
898 if(first) {
Josh Coalson090f8f72002-06-04 05:53:04 +0000899 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
Josh Coalson215af572001-03-27 01:15:58 +0000900 first = false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000901 }
902 }
903
Josh Coalsonfa697a92001-08-16 20:07:29 +0000904 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000905 return true;
906}
907
Josh Coalson570db862002-07-31 06:58:16 +0000908FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000909{
Josh Coalson5f349562002-11-14 04:58:42 +0000910 FLAC__bool is_last;
911 FLAC__uint32 i, x, type, length;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000912
Josh Coalsonaec256b2002-03-12 16:19:54 +0000913 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000914
Josh Coalson5f349562002-11-14 04:58:42 +0000915 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000916 return false; /* the read_callback_ sets the state for us */
Josh Coalson5f349562002-11-14 04:58:42 +0000917 is_last = x? true : false;
918
Josh Coalsonaec256b2002-03-12 16:19:54 +0000919 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000920 return false; /* the read_callback_ sets the state for us */
Josh Coalson5f349562002-11-14 04:58:42 +0000921
Josh Coalsonaec256b2002-03-12 16:19:54 +0000922 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000923 return false; /* the read_callback_ sets the state for us */
Josh Coalson5f349562002-11-14 04:58:42 +0000924
Josh Coalson72983922001-02-23 21:03:21 +0000925 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
Josh Coalson5f349562002-11-14 04:58:42 +0000926 if(!read_metadata_streaminfo_(decoder, is_last, length))
927 return false;
Josh Coalson841e27e2001-01-03 00:26:42 +0000928
Josh Coalsonfa697a92001-08-16 20:07:29 +0000929 decoder->private_->has_stream_info = true;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000930 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO])
931 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
Josh Coalsonc076ba52001-04-05 21:32:54 +0000932 }
933 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
Josh Coalson5f349562002-11-14 04:58:42 +0000934 if(!read_metadata_seektable_(decoder, is_last, length))
Josh Coalsonc076ba52001-04-05 21:32:54 +0000935 return false;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000936
Josh Coalsonfa697a92001-08-16 20:07:29 +0000937 decoder->private_->has_seek_table = true;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000938 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE])
939 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000940 }
941 else {
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000942 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
943 unsigned real_length = length;
Josh Coalsoncc682512002-06-08 04:53:42 +0000944 FLAC__StreamMetadata block;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000945
Josh Coalson5f349562002-11-14 04:58:42 +0000946 block.is_last = is_last;
Josh Coalson5e3fcb22002-11-06 07:11:26 +0000947 block.type = (FLAC__MetadataType)type;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000948 block.length = length;
949
Josh Coalson8400e432002-05-09 05:41:35 +0000950 if(type == FLAC__METADATA_TYPE_APPLICATION) {
Josh Coalsone4869382002-11-15 05:41:48 +0000951 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +0000952 return false; /* the read_callback_ sets the state for us */
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000953
954 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
955
Josh Coalson570db862002-07-31 06:58:16 +0000956 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000957 skip_it = !skip_it;
958 }
959
960 if(skip_it) {
Josh Coalsone4869382002-11-15 05:41:48 +0000961 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000962 return false; /* the read_callback_ sets the state for us */
963 }
964 else {
965 switch(type) {
966 case FLAC__METADATA_TYPE_PADDING:
967 /* skip the padding bytes */
Josh Coalsone4869382002-11-15 05:41:48 +0000968 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder))
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000969 return false; /* the read_callback_ sets the state for us */
970 break;
971 case FLAC__METADATA_TYPE_APPLICATION:
972 /* remember, we read the ID already */
Josh Coalsonbda92f22002-05-31 06:24:03 +0000973 if(real_length > 0) {
Josh Coalson5e3fcb22002-11-06 07:11:26 +0000974 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
Josh Coalsonbda92f22002-05-31 06:24:03 +0000975 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
976 return false;
977 }
Josh Coalsone4869382002-11-15 05:41:48 +0000978 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder))
Josh Coalsonbda92f22002-05-31 06:24:03 +0000979 return false; /* the read_callback_ sets the state for us */
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000980 }
Josh Coalsonbda92f22002-05-31 06:24:03 +0000981 else
982 block.data.application.data = 0;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000983 break;
984 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
Josh Coalson5f349562002-11-14 04:58:42 +0000985 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
986 return false;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000987 break;
Josh Coalsone4869382002-11-15 05:41:48 +0000988 case FLAC__METADATA_TYPE_CUESHEET:
989 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
990 return false;
991 break;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000992 case FLAC__METADATA_TYPE_STREAMINFO:
993 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalsonfb48f242003-01-11 06:25:42 +0000994 FLAC__ASSERT(0);
Josh Coalsond0609472003-01-10 05:37:13 +0000995 break;
Josh Coalsonbf9325f2002-05-07 05:30:27 +0000996 default:
Josh Coalsond0609472003-01-10 05:37:13 +0000997 if(real_length > 0) {
998 if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
999 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1000 return false;
1001 }
1002 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length, read_callback_, decoder))
1003 return false; /* the read_callback_ sets the state for us */
1004 }
1005 else
1006 block.data.unknown.data = 0;
1007 break;
Josh Coalsonbf9325f2002-05-07 05:30:27 +00001008 }
1009 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
1010
1011 /* now we have to free any malloc'ed data in the block */
1012 switch(type) {
1013 case FLAC__METADATA_TYPE_PADDING:
1014 break;
1015 case FLAC__METADATA_TYPE_APPLICATION:
1016 if(0 != block.data.application.data)
1017 free(block.data.application.data);
1018 break;
1019 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
1020 if(0 != block.data.vorbis_comment.vendor_string.entry)
1021 free(block.data.vorbis_comment.vendor_string.entry);
1022 if(block.data.vorbis_comment.num_comments > 0)
1023 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
1024 if(0 != block.data.vorbis_comment.comments[i].entry)
1025 free(block.data.vorbis_comment.comments[i].entry);
1026 if(0 != block.data.vorbis_comment.comments)
1027 free(block.data.vorbis_comment.comments);
1028 break;
Josh Coalsone4869382002-11-15 05:41:48 +00001029 case FLAC__METADATA_TYPE_CUESHEET:
1030 if(block.data.cue_sheet.num_tracks > 0)
1031 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
1032 if(0 != block.data.cue_sheet.tracks[i].indices)
1033 free(block.data.cue_sheet.tracks[i].indices);
1034 if(0 != block.data.cue_sheet.tracks)
1035 free(block.data.cue_sheet.tracks);
1036 break;
Josh Coalsonbf9325f2002-05-07 05:30:27 +00001037 case FLAC__METADATA_TYPE_STREAMINFO:
1038 case FLAC__METADATA_TYPE_SEEKTABLE:
Josh Coalsonbf9325f2002-05-07 05:30:27 +00001039 FLAC__ASSERT(0);
Josh Coalsond0609472003-01-10 05:37:13 +00001040 default:
1041 if(0 != block.data.unknown.data)
1042 free(block.data.unknown.data);
1043 break;
Josh Coalsonbf9325f2002-05-07 05:30:27 +00001044 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001045 }
1046 }
1047
Josh Coalson5f349562002-11-14 04:58:42 +00001048 if(is_last)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001049 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001050
1051 return true;
1052}
1053
Josh Coalson5f349562002-11-14 04:58:42 +00001054FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1055{
1056 FLAC__uint32 x;
1057 unsigned bits, used_bits = 0;
1058
1059 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1060
1061 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
1062 decoder->private_->stream_info.is_last = is_last;
1063 decoder->private_->stream_info.length = length;
1064
1065 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
1066 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder))
1067 return false; /* the read_callback_ sets the state for us */
1068 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
1069 used_bits += bits;
1070
1071 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
1072 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder))
1073 return false; /* the read_callback_ sets the state for us */
1074 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
1075 used_bits += bits;
1076
1077 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
1078 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder))
1079 return false; /* the read_callback_ sets the state for us */
1080 decoder->private_->stream_info.data.stream_info.min_framesize = x;
1081 used_bits += bits;
1082
1083 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
1084 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder))
1085 return false; /* the read_callback_ sets the state for us */
1086 decoder->private_->stream_info.data.stream_info.max_framesize = x;
1087 used_bits += bits;
1088
1089 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
1090 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder))
1091 return false; /* the read_callback_ sets the state for us */
1092 decoder->private_->stream_info.data.stream_info.sample_rate = x;
1093 used_bits += bits;
1094
1095 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
1096 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder))
1097 return false; /* the read_callback_ sets the state for us */
1098 decoder->private_->stream_info.data.stream_info.channels = x+1;
1099 used_bits += bits;
1100
1101 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
1102 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder))
1103 return false; /* the read_callback_ sets the state for us */
1104 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
1105 used_bits += bits;
1106
1107 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
1108 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder))
1109 return false; /* the read_callback_ sets the state for us */
1110 used_bits += bits;
1111
Josh Coalsone4869382002-11-15 05:41:48 +00001112 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder))
Josh Coalson5f349562002-11-14 04:58:42 +00001113 return false; /* the read_callback_ sets the state for us */
1114 used_bits += 16*8;
1115
1116 /* skip the rest of the block */
1117 FLAC__ASSERT(used_bits % 8 == 0);
1118 length -= (used_bits / 8);
Josh Coalsone4869382002-11-15 05:41:48 +00001119 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
Josh Coalson5f349562002-11-14 04:58:42 +00001120 return false; /* the read_callback_ sets the state for us */
1121
1122 return true;
1123}
1124
1125FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
1126{
1127 FLAC__uint32 i, x;
1128 FLAC__uint64 xx;
1129
1130 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1131
1132 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
1133 decoder->private_->seek_table.is_last = is_last;
1134 decoder->private_->seek_table.length = length;
1135
1136 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
1137
Josh Coalson4fa90592002-12-04 07:01:37 +00001138 /* use realloc since we may pass through here several times (e.g. after seeking) */
1139 if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) {
Josh Coalson5f349562002-11-14 04:58:42 +00001140 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1141 return false;
1142 }
1143 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
1144 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder))
1145 return false; /* the read_callback_ sets the state for us */
1146 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
1147
1148 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder))
1149 return false; /* the read_callback_ sets the state for us */
1150 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
1151
1152 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder))
1153 return false; /* the read_callback_ sets the state for us */
1154 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
1155 }
1156 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
1157 /* if there is a partial point left, skip over it */
1158 if(length > 0) {
1159 /*@@@ do an error_callback() here? there's an argument for either way */
Josh Coalsone4869382002-11-15 05:41:48 +00001160 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder))
Josh Coalson5f349562002-11-14 04:58:42 +00001161 return false; /* the read_callback_ sets the state for us */
1162 }
1163
1164 return true;
1165}
1166
1167FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
1168{
1169 FLAC__uint32 i;
1170
1171 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1172
1173 /* read vendor string */
1174 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1175 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder))
1176 return false; /* the read_callback_ sets the state for us */
1177 if(obj->vendor_string.length > 0) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001178 if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length+1))) {
Josh Coalson5f349562002-11-14 04:58:42 +00001179 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1180 return false;
1181 }
Josh Coalsone4869382002-11-15 05:41:48 +00001182 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder))
Josh Coalson5f349562002-11-14 04:58:42 +00001183 return false; /* the read_callback_ sets the state for us */
Josh Coalsondef597e2004-12-30 00:59:30 +00001184 obj->vendor_string.entry[obj->vendor_string.length] = '\0';
Josh Coalson5f349562002-11-14 04:58:42 +00001185 }
1186 else
1187 obj->vendor_string.entry = 0;
1188
1189 /* read num comments */
1190 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
1191 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder))
1192 return false; /* the read_callback_ sets the state for us */
1193
1194 /* read comments */
1195 if(obj->num_comments > 0) {
1196 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
1197 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1198 return false;
1199 }
1200 for(i = 0; i < obj->num_comments; i++) {
1201 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
1202 if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder))
1203 return false; /* the read_callback_ sets the state for us */
1204 if(obj->comments[i].length > 0) {
Josh Coalsondef597e2004-12-30 00:59:30 +00001205 if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length+1))) {
Josh Coalson5f349562002-11-14 04:58:42 +00001206 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1207 return false;
1208 }
Josh Coalsone4869382002-11-15 05:41:48 +00001209 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder))
Josh Coalson5f349562002-11-14 04:58:42 +00001210 return false; /* the read_callback_ sets the state for us */
Josh Coalsondef597e2004-12-30 00:59:30 +00001211 obj->comments[i].entry[obj->comments[i].length] = '\0';
Josh Coalson5f349562002-11-14 04:58:42 +00001212 }
1213 else
1214 obj->comments[i].entry = 0;
1215 }
1216 }
1217 else {
1218 obj->comments = 0;
1219 }
1220
1221 return true;
1222}
1223
Josh Coalsone4869382002-11-15 05:41:48 +00001224FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
1225{
1226 FLAC__uint32 i, j, x;
1227
1228 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
1229
1230 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
1231
1232 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
Josh Coalson27e13542003-05-20 00:01:49 +00001233 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8, read_callback_, decoder))
Josh Coalsone4869382002-11-15 05:41:48 +00001234 return false; /* the read_callback_ sets the state for us */
1235
1236 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder))
1237 return false; /* the read_callback_ sets the state for us */
1238
Josh Coalson8f0c71b2002-12-05 06:37:46 +00001239 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN, read_callback_, decoder))
1240 return false; /* the read_callback_ sets the state for us */
1241 obj->is_cd = x? true : false;
1242
Josh Coalson38162d52002-11-16 06:31:54 +00001243 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder))
1244 return false; /* the read_callback_ sets the state for us */
1245
Josh Coalsone4869382002-11-15 05:41:48 +00001246 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder))
1247 return false; /* the read_callback_ sets the state for us */
1248 obj->num_tracks = x;
1249
1250 if(obj->num_tracks > 0) {
1251 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
1252 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1253 return false;
1254 }
1255 for(i = 0; i < obj->num_tracks; i++) {
1256 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
1257 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder))
1258 return false; /* the read_callback_ sets the state for us */
1259
1260 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder))
1261 return false; /* the read_callback_ sets the state for us */
Josh Coalsona0d54542002-11-15 19:32:57 +00001262 track->number = (FLAC__byte)x;
Josh Coalsone4869382002-11-15 05:41:48 +00001263
1264 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
Josh Coalson27e13542003-05-20 00:01:49 +00001265 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8, read_callback_, decoder))
Josh Coalsone4869382002-11-15 05:41:48 +00001266 return false; /* the read_callback_ sets the state for us */
1267
1268 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder))
1269 return false; /* the read_callback_ sets the state for us */
1270 track->type = x;
1271
1272 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder))
1273 return false; /* the read_callback_ sets the state for us */
1274 track->pre_emphasis = x;
1275
1276 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder))
1277 return false; /* the read_callback_ sets the state for us */
1278
1279 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder))
1280 return false; /* the read_callback_ sets the state for us */
Josh Coalsona0d54542002-11-15 19:32:57 +00001281 track->num_indices = (FLAC__byte)x;
Josh Coalsone4869382002-11-15 05:41:48 +00001282
1283 if(track->num_indices > 0) {
1284 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
1285 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
1286 return false;
1287 }
1288 for(j = 0; j < track->num_indices; j++) {
1289 FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
1290 if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder))
1291 return false; /* the read_callback_ sets the state for us */
1292
1293 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder))
1294 return false; /* the read_callback_ sets the state for us */
Josh Coalsona0d54542002-11-15 19:32:57 +00001295 index->number = (FLAC__byte)x;
Josh Coalsone4869382002-11-15 05:41:48 +00001296
1297 if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder))
1298 return false; /* the read_callback_ sets the state for us */
1299 }
1300 }
1301 }
1302 }
1303
1304 return true;
1305}
1306
Josh Coalson570db862002-07-31 06:58:16 +00001307FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001308{
Josh Coalson77e3f312001-06-23 03:03:24 +00001309 FLAC__uint32 x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001310 unsigned i, skip;
1311
1312 /* skip the version and flags bytes */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001313 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001314 return false; /* the read_callback_ sets the state for us */
1315 /* get the size (in bytes) to skip */
1316 skip = 0;
1317 for(i = 0; i < 4; i++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001318 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001319 return false; /* the read_callback_ sets the state for us */
1320 skip <<= 7;
1321 skip |= (x & 0x7f);
1322 }
1323 /* skip the rest of the tag */
Josh Coalsone4869382002-11-15 05:41:48 +00001324 if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder))
Josh Coalsonbf9325f2002-05-07 05:30:27 +00001325 return false; /* the read_callback_ sets the state for us */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001326 return true;
1327}
1328
Josh Coalson570db862002-07-31 06:58:16 +00001329FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001330{
Josh Coalson77e3f312001-06-23 03:03:24 +00001331 FLAC__uint32 x;
1332 FLAC__bool first = true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001333
1334 /* If we know the total number of samples in the stream, stop if we've read that many. */
1335 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001336 if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) {
1337 if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) {
1338 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001339 return true;
1340 }
1341 }
1342
1343 /* make sure we're byte aligned */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001344 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
1345 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001346 return false; /* the read_callback_ sets the state for us */
1347 }
1348
1349 while(1) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001350 if(decoder->private_->cached) {
1351 x = (FLAC__uint32)decoder->private_->lookahead;
1352 decoder->private_->cached = false;
Josh Coalson215af572001-03-27 01:15:58 +00001353 }
1354 else {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001355 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001356 return false; /* the read_callback_ sets the state for us */
Josh Coalson215af572001-03-27 01:15:58 +00001357 }
1358 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001359 decoder->private_->header_warmup[0] = (FLAC__byte)x;
Josh Coalsonaec256b2002-03-12 16:19:54 +00001360 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalson215af572001-03-27 01:15:58 +00001361 return false; /* the read_callback_ sets the state for us */
1362
1363 /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */
1364 /* else we have to check if the second byte is the end of a sync code */
1365 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001366 decoder->private_->lookahead = (FLAC__byte)x;
1367 decoder->private_->cached = true;
Josh Coalson215af572001-03-27 01:15:58 +00001368 }
1369 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001370 decoder->private_->header_warmup[1] = (FLAC__byte)x;
1371 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001372 return true;
1373 }
1374 }
1375 if(first) {
Josh Coalson090f8f72002-06-04 05:53:04 +00001376 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
Josh Coalson38c68542002-02-12 22:58:59 +00001377 first = false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001378 }
1379 }
1380
1381 return true;
1382}
1383
Josh Coalson47f51b12004-07-16 00:53:38 +00001384FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001385{
1386 unsigned channel;
1387 unsigned i;
Josh Coalson77e3f312001-06-23 03:03:24 +00001388 FLAC__int32 mid, side, left, right;
1389 FLAC__uint16 frame_crc; /* the one we calculate from the input stream */
1390 FLAC__uint32 x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001391
1392 *got_a_frame = false;
1393
Josh Coalson215af572001-03-27 01:15:58 +00001394 /* init the CRC */
1395 frame_crc = 0;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001396 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
1397 FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
Josh Coalsonaec256b2002-03-12 16:19:54 +00001398 FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc);
Josh Coalson215af572001-03-27 01:15:58 +00001399
Josh Coalson570db862002-07-31 06:58:16 +00001400 if(!read_frame_header_(decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001401 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001402 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001403 return true;
Josh Coalson570db862002-07-31 06:58:16 +00001404 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001405 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001406 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001407 /*
1408 * first figure the correct bits-per-sample of the subframe
1409 */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001410 unsigned bps = decoder->private_->frame.header.bits_per_sample;
1411 switch(decoder->private_->frame.header.channel_assignment) {
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001412 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1413 /* no adjustment needed */
1414 break;
1415 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001416 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001417 if(channel == 1)
1418 bps++;
1419 break;
1420 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001421 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001422 if(channel == 0)
1423 bps++;
1424 break;
1425 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001426 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001427 if(channel == 1)
1428 bps++;
1429 break;
1430 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001431 FLAC__ASSERT(0);
Josh Coalsonb5e60e52001-01-28 09:27:27 +00001432 }
1433 /*
1434 * now read it
1435 */
Josh Coalson47f51b12004-07-16 00:53:38 +00001436 if(!read_subframe_(decoder, channel, bps, do_full_decode))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001437 return false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001438 if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) {
1439 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001440 return true;
1441 }
1442 }
Josh Coalson570db862002-07-31 06:58:16 +00001443 if(!read_zero_padding_(decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001444 return false;
1445
Josh Coalson215af572001-03-27 01:15:58 +00001446 /*
1447 * Read the frame CRC-16 from the footer and check
1448 */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001449 frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input);
1450 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder))
Josh Coalson215af572001-03-27 01:15:58 +00001451 return false; /* the read_callback_ sets the state for us */
Josh Coalson77e3f312001-06-23 03:03:24 +00001452 if(frame_crc == (FLAC__uint16)x) {
Josh Coalson47f51b12004-07-16 00:53:38 +00001453 if(do_full_decode) {
1454 /* Undo any special channel coding */
1455 switch(decoder->private_->frame.header.channel_assignment) {
1456 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
1457 /* do nothing */
1458 break;
1459 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
1460 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1461 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1462 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
1463 break;
1464 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
1465 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1466 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1467 decoder->private_->output[0][i] += decoder->private_->output[1][i];
1468 break;
1469 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
1470 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
1471 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
1472 mid = decoder->private_->output[0][i];
1473 side = decoder->private_->output[1][i];
1474 mid <<= 1;
1475 if(side & 1) /* i.e. if 'side' is odd... */
1476 mid++;
1477 left = mid + side;
1478 right = mid - side;
1479 decoder->private_->output[0][i] = left >> 1;
1480 decoder->private_->output[1][i] = right >> 1;
1481 }
1482 break;
1483 default:
1484 FLAC__ASSERT(0);
1485 break;
1486 }
Josh Coalson215af572001-03-27 01:15:58 +00001487 }
1488 }
1489 else {
1490 /* Bad frame, emit error and zero the output signal */
Josh Coalson090f8f72002-06-04 05:53:04 +00001491 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data);
Josh Coalson47f51b12004-07-16 00:53:38 +00001492 if(do_full_decode) {
1493 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
1494 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
1495 }
Josh Coalson215af572001-03-27 01:15:58 +00001496 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001497 }
1498
1499 *got_a_frame = true;
1500
1501 /* put the latest values into the public section of the decoder instance */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001502 decoder->protected_->channels = decoder->private_->frame.header.channels;
1503 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
1504 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
1505 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
1506 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001507
Josh Coalsonfa697a92001-08-16 20:07:29 +00001508 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
1509 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001510
1511 /* write it */
Josh Coalson47f51b12004-07-16 00:53:38 +00001512 if(do_full_decode) {
1513 if(decoder->private_->write_callback(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
1514 return false;
1515 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001516
Josh Coalsonfa697a92001-08-16 20:07:29 +00001517 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001518 return true;
1519}
1520
Josh Coalson570db862002-07-31 06:58:16 +00001521FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001522{
Josh Coalson77e3f312001-06-23 03:03:24 +00001523 FLAC__uint32 x;
1524 FLAC__uint64 xx;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001525 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
Josh Coalson77e3f312001-06-23 03:03:24 +00001526 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001527 unsigned raw_header_len;
Josh Coalson77e3f312001-06-23 03:03:24 +00001528 FLAC__bool is_unparseable = false;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001529 const FLAC__bool is_known_variable_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize);
1530 const FLAC__bool is_known_fixed_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001531
Josh Coalsonaec256b2002-03-12 16:19:54 +00001532 FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input));
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001533
Josh Coalson215af572001-03-27 01:15:58 +00001534 /* init the raw header with the saved bits from synchronization */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001535 raw_header[0] = decoder->private_->header_warmup[0];
1536 raw_header[1] = decoder->private_->header_warmup[1];
Josh Coalson215af572001-03-27 01:15:58 +00001537 raw_header_len = 2;
1538
1539 /*
1540 * check to make sure that the reserved bits are 0
1541 */
1542 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
1543 is_unparseable = true;
1544 }
1545
1546 /*
1547 * Note that along the way as we read the header, we look for a sync
1548 * code inside. If we find one it would indicate that our original
1549 * sync was bad since there cannot be a sync code in a valid header.
1550 */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001551
1552 /*
1553 * read in the raw header as bytes so we can CRC it, and parse it on the way
1554 */
1555 for(i = 0; i < 2; i++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001556 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001557 return false; /* the read_callback_ sets the state for us */
Josh Coalson215af572001-03-27 01:15:58 +00001558 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001559 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001560 decoder->private_->lookahead = (FLAC__byte)x;
1561 decoder->private_->cached = true;
Josh Coalson090f8f72002-06-04 05:53:04 +00001562 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001563 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson215af572001-03-27 01:15:58 +00001564 return true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001565 }
Josh Coalson77e3f312001-06-23 03:03:24 +00001566 raw_header[raw_header_len++] = (FLAC__byte)x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001567 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001568
Josh Coalson215af572001-03-27 01:15:58 +00001569 switch(x = raw_header[2] >> 4) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001570 case 0:
Josh Coalson7531b002001-04-14 00:24:32 +00001571 if(is_known_fixed_blocksize_stream)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001572 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001573 else
1574 is_unparseable = true;
1575 break;
1576 case 1:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001577 decoder->private_->frame.header.blocksize = 192;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001578 break;
1579 case 2:
1580 case 3:
1581 case 4:
1582 case 5:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001583 decoder->private_->frame.header.blocksize = 576 << (x-2);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001584 break;
1585 case 6:
1586 case 7:
1587 blocksize_hint = x;
1588 break;
Josh Coalson215af572001-03-27 01:15:58 +00001589 case 8:
1590 case 9:
1591 case 10:
1592 case 11:
1593 case 12:
1594 case 13:
1595 case 14:
1596 case 15:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001597 decoder->private_->frame.header.blocksize = 256 << (x-8);
Josh Coalson215af572001-03-27 01:15:58 +00001598 break;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001599 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001600 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001601 break;
1602 }
1603
Josh Coalson215af572001-03-27 01:15:58 +00001604 switch(x = raw_header[2] & 0x0f) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001605 case 0:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001606 if(decoder->private_->has_stream_info)
1607 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001608 else
1609 is_unparseable = true;
1610 break;
1611 case 1:
1612 case 2:
1613 case 3:
1614 is_unparseable = true;
1615 break;
1616 case 4:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001617 decoder->private_->frame.header.sample_rate = 8000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001618 break;
1619 case 5:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001620 decoder->private_->frame.header.sample_rate = 16000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001621 break;
1622 case 6:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001623 decoder->private_->frame.header.sample_rate = 22050;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001624 break;
1625 case 7:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001626 decoder->private_->frame.header.sample_rate = 24000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001627 break;
1628 case 8:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001629 decoder->private_->frame.header.sample_rate = 32000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001630 break;
1631 case 9:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001632 decoder->private_->frame.header.sample_rate = 44100;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001633 break;
1634 case 10:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001635 decoder->private_->frame.header.sample_rate = 48000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001636 break;
1637 case 11:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001638 decoder->private_->frame.header.sample_rate = 96000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001639 break;
1640 case 12:
1641 case 13:
1642 case 14:
1643 sample_rate_hint = x;
1644 break;
1645 case 15:
Josh Coalson090f8f72002-06-04 05:53:04 +00001646 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001647 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001648 return true;
1649 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001650 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001651 }
1652
Josh Coalson215af572001-03-27 01:15:58 +00001653 x = (unsigned)(raw_header[3] >> 4);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001654 if(x & 8) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001655 decoder->private_->frame.header.channels = 2;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001656 switch(x & 7) {
1657 case 0:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001658 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001659 break;
1660 case 1:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001661 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001662 break;
1663 case 2:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001664 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001665 break;
1666 default:
1667 is_unparseable = true;
1668 break;
1669 }
1670 }
1671 else {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001672 decoder->private_->frame.header.channels = (unsigned)x + 1;
1673 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001674 }
1675
Josh Coalson215af572001-03-27 01:15:58 +00001676 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001677 case 0:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001678 if(decoder->private_->has_stream_info)
1679 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001680 else
1681 is_unparseable = true;
1682 break;
1683 case 1:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001684 decoder->private_->frame.header.bits_per_sample = 8;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001685 break;
1686 case 2:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001687 decoder->private_->frame.header.bits_per_sample = 12;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001688 break;
1689 case 4:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001690 decoder->private_->frame.header.bits_per_sample = 16;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001691 break;
1692 case 5:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001693 decoder->private_->frame.header.bits_per_sample = 20;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001694 break;
1695 case 6:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001696 decoder->private_->frame.header.bits_per_sample = 24;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001697 break;
1698 case 3:
1699 case 7:
1700 is_unparseable = true;
1701 break;
1702 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001703 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001704 break;
1705 }
1706
Josh Coalson215af572001-03-27 01:15:58 +00001707 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
Josh Coalson090f8f72002-06-04 05:53:04 +00001708 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001709 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001710 return true;
1711 }
1712
Josh Coalsonba056082005-01-24 00:20:49 +00001713 /*
1714 * Now we get to the regrettable consequences of not knowing for sure
1715 * whether we got a frame number or a sample number. There are no
1716 * encoders that do variable-blocksize encoding so unless we know from
1717 * the STREAMINFO that it is variable-blocksize we will assume it is
1718 * fixed-blocksize. The trouble comes when we have no STREAMINFO; again
1719 * we will guess that is fixed-blocksize. Where this can go wrong: 1) a
1720 * variable-blocksize stream with no STREAMINFO; 2) a fixed-blocksize
1721 * stream that was edited such that one or more frames before or
1722 * including this one do not have the same number of samples as the
1723 * STREAMINFO's min and max blocksize.
1724 */
1725 if(is_known_variable_blocksize_stream) {
1726 if(blocksize_hint) {
1727 if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len))
1728 return false; /* the read_callback_ sets the state for us */
1729 if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
1730 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1731 decoder->private_->cached = true;
1732 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
1733 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
1734 return true;
1735 }
1736 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
1737 decoder->private_->frame.header.number.sample_number = xx;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001738 }
Josh Coalsonba056082005-01-24 00:20:49 +00001739 else
1740 is_unparseable = true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001741 }
1742 else {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001743 if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001744 return false; /* the read_callback_ sets the state for us */
Josh Coalson215af572001-03-27 01:15:58 +00001745 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001746 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
1747 decoder->private_->cached = true;
Josh Coalson090f8f72002-06-04 05:53:04 +00001748 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001749 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalson215af572001-03-27 01:15:58 +00001750 return true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001751 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001752 decoder->private_->last_frame_number = x;
Josh Coalson2eed0b92003-11-27 18:15:17 +00001753 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
Josh Coalsonba056082005-01-24 00:20:49 +00001754 if(decoder->private_->has_stream_info) {
1755 FLAC__ASSERT(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
1756 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
Josh Coalson77d8e7f2005-01-30 18:17:11 +00001757 decoder->private_->last_block_size = decoder->private_->frame.header.blocksize;
Josh Coalsonba056082005-01-24 00:20:49 +00001758 }
1759 else if(blocksize_hint) {
1760 if(decoder->private_->last_block_size)
1761 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->last_block_size * (FLAC__uint64)x;
Josh Coalson2eed0b92003-11-27 18:15:17 +00001762 else
1763 is_unparseable = true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001764 }
Josh Coalsonba056082005-01-24 00:20:49 +00001765 else {
1766 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
Josh Coalson77d8e7f2005-01-30 18:17:11 +00001767 decoder->private_->last_block_size = decoder->private_->frame.header.blocksize;
Josh Coalsonba056082005-01-24 00:20:49 +00001768 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001769 }
1770
1771 if(blocksize_hint) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001772 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001773 return false; /* the read_callback_ sets the state for us */
Josh Coalson77e3f312001-06-23 03:03:24 +00001774 raw_header[raw_header_len++] = (FLAC__byte)x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001775 if(blocksize_hint == 7) {
Josh Coalson77e3f312001-06-23 03:03:24 +00001776 FLAC__uint32 _x;
Josh Coalsonaec256b2002-03-12 16:19:54 +00001777 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001778 return false; /* the read_callback_ sets the state for us */
Josh Coalson77e3f312001-06-23 03:03:24 +00001779 raw_header[raw_header_len++] = (FLAC__byte)_x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001780 x = (x << 8) | _x;
1781 }
Josh Coalsonfa697a92001-08-16 20:07:29 +00001782 decoder->private_->frame.header.blocksize = x+1;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001783 }
1784
1785 if(sample_rate_hint) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001786 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001787 return false; /* the read_callback_ sets the state for us */
Josh Coalson77e3f312001-06-23 03:03:24 +00001788 raw_header[raw_header_len++] = (FLAC__byte)x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001789 if(sample_rate_hint != 12) {
Josh Coalson77e3f312001-06-23 03:03:24 +00001790 FLAC__uint32 _x;
Josh Coalsonaec256b2002-03-12 16:19:54 +00001791 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001792 return false; /* the read_callback_ sets the state for us */
Josh Coalson77e3f312001-06-23 03:03:24 +00001793 raw_header[raw_header_len++] = (FLAC__byte)_x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001794 x = (x << 8) | _x;
1795 }
1796 if(sample_rate_hint == 12)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001797 decoder->private_->frame.header.sample_rate = x*1000;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001798 else if(sample_rate_hint == 13)
Josh Coalsonfa697a92001-08-16 20:07:29 +00001799 decoder->private_->frame.header.sample_rate = x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001800 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00001801 decoder->private_->frame.header.sample_rate = x*10;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001802 }
1803
Josh Coalson215af572001-03-27 01:15:58 +00001804 /* read the CRC-8 byte */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001805 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001806 return false; /* the read_callback_ sets the state for us */
Josh Coalson77e3f312001-06-23 03:03:24 +00001807 crc8 = (FLAC__byte)x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001808
Josh Coalson215af572001-03-27 01:15:58 +00001809 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
Josh Coalson090f8f72002-06-04 05:53:04 +00001810 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001811 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001812 return true;
1813 }
1814
1815 if(is_unparseable) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001816 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001817 return false;
1818 }
1819
1820 return true;
1821}
1822
Josh Coalson47f51b12004-07-16 00:53:38 +00001823FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001824{
Josh Coalson77e3f312001-06-23 03:03:24 +00001825 FLAC__uint32 x;
1826 FLAC__bool wasted_bits;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001827
Josh Coalsonaec256b2002-03-12 16:19:54 +00001828 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001829 return false; /* the read_callback_ sets the state for us */
Josh Coalson215af572001-03-27 01:15:58 +00001830
1831 wasted_bits = (x & 1);
1832 x &= 0xfe;
1833
1834 if(wasted_bits) {
1835 unsigned u;
Josh Coalsonaec256b2002-03-12 16:19:54 +00001836 if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder))
Josh Coalson215af572001-03-27 01:15:58 +00001837 return false; /* the read_callback_ sets the state for us */
Josh Coalsonfa697a92001-08-16 20:07:29 +00001838 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
1839 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
Josh Coalson215af572001-03-27 01:15:58 +00001840 }
1841 else
Josh Coalsonfa697a92001-08-16 20:07:29 +00001842 decoder->private_->frame.subframes[channel].wasted_bits = 0;
Josh Coalson215af572001-03-27 01:15:58 +00001843
Josh Coalson859bc542001-03-27 22:22:27 +00001844 /*
1845 * Lots of magic numbers here
1846 */
Josh Coalson215af572001-03-27 01:15:58 +00001847 if(x & 0x80) {
Josh Coalson090f8f72002-06-04 05:53:04 +00001848 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001849 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001850 return true;
1851 }
1852 else if(x == 0) {
Josh Coalson47f51b12004-07-16 00:53:38 +00001853 if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
Josh Coalson215af572001-03-27 01:15:58 +00001854 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001855 }
1856 else if(x == 2) {
Josh Coalson47f51b12004-07-16 00:53:38 +00001857 if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
Josh Coalson215af572001-03-27 01:15:58 +00001858 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001859 }
1860 else if(x < 16) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001861 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001862 return false;
1863 }
1864 else if(x <= 24) {
Josh Coalson47f51b12004-07-16 00:53:38 +00001865 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
Josh Coalson215af572001-03-27 01:15:58 +00001866 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001867 }
1868 else if(x < 64) {
Josh Coalsonfa697a92001-08-16 20:07:29 +00001869 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001870 return false;
1871 }
1872 else {
Josh Coalson47f51b12004-07-16 00:53:38 +00001873 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
Josh Coalson215af572001-03-27 01:15:58 +00001874 return false;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001875 }
Josh Coalson215af572001-03-27 01:15:58 +00001876
Josh Coalson47f51b12004-07-16 00:53:38 +00001877 if(wasted_bits && do_full_decode) {
Josh Coalson859bc542001-03-27 22:22:27 +00001878 unsigned i;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001879 x = decoder->private_->frame.subframes[channel].wasted_bits;
1880 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1881 decoder->private_->output[channel][i] <<= x;
Josh Coalson215af572001-03-27 01:15:58 +00001882 }
Josh Coalson859bc542001-03-27 22:22:27 +00001883
Josh Coalson215af572001-03-27 01:15:58 +00001884 return true;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001885}
1886
Josh Coalson47f51b12004-07-16 00:53:38 +00001887FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001888{
Josh Coalsonfa697a92001-08-16 20:07:29 +00001889 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
Josh Coalson77e3f312001-06-23 03:03:24 +00001890 FLAC__int32 x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001891 unsigned i;
Josh Coalsonfa697a92001-08-16 20:07:29 +00001892 FLAC__int32 *output = decoder->private_->output[channel];
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001893
Josh Coalsonfa697a92001-08-16 20:07:29 +00001894 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
Josh Coalson6dcea512001-01-23 23:07:36 +00001895
Josh Coalsonaec256b2002-03-12 16:19:54 +00001896 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001897 return false; /* the read_callback_ sets the state for us */
1898
Josh Coalson6dcea512001-01-23 23:07:36 +00001899 subframe->value = x;
1900
1901 /* decode the subframe */
Josh Coalson47f51b12004-07-16 00:53:38 +00001902 if(do_full_decode) {
1903 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
1904 output[i] = x;
1905 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001906
1907 return true;
1908}
1909
Josh Coalson47f51b12004-07-16 00:53:38 +00001910FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001911{
Josh Coalsonfa697a92001-08-16 20:07:29 +00001912 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
Josh Coalson77e3f312001-06-23 03:03:24 +00001913 FLAC__int32 i32;
1914 FLAC__uint32 u32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001915 unsigned u;
1916
Josh Coalsonfa697a92001-08-16 20:07:29 +00001917 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
Josh Coalson6dcea512001-01-23 23:07:36 +00001918
Josh Coalsonfa697a92001-08-16 20:07:29 +00001919 subframe->residual = decoder->private_->residual[channel];
Josh Coalson6dcea512001-01-23 23:07:36 +00001920 subframe->order = order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001921
1922 /* read warm-up samples */
1923 for(u = 0; u < order; u++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001924 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001925 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00001926 subframe->warmup[u] = i32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001927 }
1928
1929 /* read entropy coding method info */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001930 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001931 return false; /* the read_callback_ sets the state for us */
Josh Coalson5e3fcb22002-11-06 07:11:26 +00001932 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
Josh Coalson6dcea512001-01-23 23:07:36 +00001933 switch(subframe->entropy_coding_method.type) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001934 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
Josh Coalsonaec256b2002-03-12 16:19:54 +00001935 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001936 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00001937 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
Josh Coalsona37ba462002-08-19 21:36:39 +00001938 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001939 break;
1940 default:
Josh Coalsonfa697a92001-08-16 20:07:29 +00001941 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001942 return false;
1943 }
1944
1945 /* read residual */
Josh Coalson6dcea512001-01-23 23:07:36 +00001946 switch(subframe->entropy_coding_method.type) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001947 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
Josh Coalsona37ba462002-08-19 21:36:39 +00001948 if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel]))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001949 return false;
1950 break;
1951 default:
Josh Coalson1b689822001-05-31 20:11:02 +00001952 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001953 }
1954
1955 /* decode the subframe */
Josh Coalson47f51b12004-07-16 00:53:38 +00001956 if(do_full_decode) {
1957 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
1958 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
1959 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001960
1961 return true;
1962}
1963
Josh Coalson47f51b12004-07-16 00:53:38 +00001964FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001965{
Josh Coalsonfa697a92001-08-16 20:07:29 +00001966 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
Josh Coalson77e3f312001-06-23 03:03:24 +00001967 FLAC__int32 i32;
1968 FLAC__uint32 u32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001969 unsigned u;
1970
Josh Coalsonfa697a92001-08-16 20:07:29 +00001971 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
Josh Coalson6dcea512001-01-23 23:07:36 +00001972
Josh Coalsonfa697a92001-08-16 20:07:29 +00001973 subframe->residual = decoder->private_->residual[channel];
Josh Coalson6dcea512001-01-23 23:07:36 +00001974 subframe->order = order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001975
1976 /* read warm-up samples */
1977 for(u = 0; u < order; u++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00001978 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001979 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00001980 subframe->warmup[u] = i32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001981 }
1982
1983 /* read qlp coeff precision */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001984 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001985 return false; /* the read_callback_ sets the state for us */
Josh Coalson3372cc02001-04-13 18:44:46 +00001986 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
Josh Coalson090f8f72002-06-04 05:53:04 +00001987 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
Josh Coalsonfa697a92001-08-16 20:07:29 +00001988 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001989 return true;
1990 }
Josh Coalson6dcea512001-01-23 23:07:36 +00001991 subframe->qlp_coeff_precision = u32+1;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001992
1993 /* read qlp shift */
Josh Coalsonaec256b2002-03-12 16:19:54 +00001994 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001995 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00001996 subframe->quantization_level = i32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00001997
1998 /* read quantized lp coefficiencts */
1999 for(u = 0; u < order; u++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00002000 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002001 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00002002 subframe->qlp_coeff[u] = i32;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002003 }
2004
2005 /* read entropy coding method info */
Josh Coalsonaec256b2002-03-12 16:19:54 +00002006 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002007 return false; /* the read_callback_ sets the state for us */
Josh Coalson5e3fcb22002-11-06 07:11:26 +00002008 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
Josh Coalson6dcea512001-01-23 23:07:36 +00002009 switch(subframe->entropy_coding_method.type) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002010 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
Josh Coalsonaec256b2002-03-12 16:19:54 +00002011 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002012 return false; /* the read_callback_ sets the state for us */
Josh Coalson6dcea512001-01-23 23:07:36 +00002013 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
Josh Coalsona37ba462002-08-19 21:36:39 +00002014 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002015 break;
2016 default:
Josh Coalsonfa697a92001-08-16 20:07:29 +00002017 decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002018 return false;
2019 }
2020
2021 /* read residual */
Josh Coalson6dcea512001-01-23 23:07:36 +00002022 switch(subframe->entropy_coding_method.type) {
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002023 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
Josh Coalsona37ba462002-08-19 21:36:39 +00002024 if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel]))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002025 return false;
2026 break;
2027 default:
Josh Coalson1b689822001-05-31 20:11:02 +00002028 FLAC__ASSERT(0);
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002029 }
2030
2031 /* decode the subframe */
Josh Coalson47f51b12004-07-16 00:53:38 +00002032 if(do_full_decode) {
2033 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
2034 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
Josh Coalson3aadd102004-07-27 01:13:16 +00002035 if(bps <= 16 && subframe->qlp_coeff_precision <= 16) {
2036 if(order <= 8)
2037 decoder->private_->local_lpc_restore_signal_16bit_order8(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
2038 else
2039 decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
2040 }
Josh Coalson47f51b12004-07-16 00:53:38 +00002041 else
2042 decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
Josh Coalsonfb9d18f2002-10-21 07:04:07 +00002043 else
Josh Coalson47f51b12004-07-16 00:53:38 +00002044 decoder->private_->local_lpc_restore_signal_64bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order);
2045 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002046
2047 return true;
2048}
2049
Josh Coalson47f51b12004-07-16 00:53:38 +00002050FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002051{
Josh Coalsonfa697a92001-08-16 20:07:29 +00002052 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
2053 FLAC__int32 x, *residual = decoder->private_->residual[channel];
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002054 unsigned i;
2055
Josh Coalsonfa697a92001-08-16 20:07:29 +00002056 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
Josh Coalson6dcea512001-01-23 23:07:36 +00002057
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002058 subframe->data = residual;
Josh Coalson6dcea512001-01-23 23:07:36 +00002059
Josh Coalsonfa697a92001-08-16 20:07:29 +00002060 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00002061 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002062 return false; /* the read_callback_ sets the state for us */
Josh Coalsonb5e60e52001-01-28 09:27:27 +00002063 residual[i] = x;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002064 }
2065
Josh Coalson6dcea512001-01-23 23:07:36 +00002066 /* decode the subframe */
Josh Coalson47f51b12004-07-16 00:53:38 +00002067 if(do_full_decode)
2068 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
Josh Coalson6dcea512001-01-23 23:07:36 +00002069
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002070 return true;
2071}
2072
Josh Coalsona37ba462002-08-19 21:36:39 +00002073FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002074{
Josh Coalson77e3f312001-06-23 03:03:24 +00002075 FLAC__uint32 rice_parameter;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002076 int i;
2077 unsigned partition, sample, u;
2078 const unsigned partitions = 1u << partition_order;
Josh Coalsonfa697a92001-08-16 20:07:29 +00002079 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002080
Josh Coalson607fc842004-10-06 00:56:51 +00002081 /* sanity checks */
2082 if(partition_order == 0) {
2083 if(decoder->private_->frame.header.blocksize < predictor_order) {
2084 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
2085 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2086 return true;
2087 }
2088 }
2089 else {
2090 if(partition_samples < predictor_order) {
2091 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
2092 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
2093 return true;
2094 }
2095 }
2096
Josh Coalsona37ba462002-08-19 21:36:39 +00002097 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002098 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
2099 return false;
2100 }
Josh Coalsonb7023aa2002-08-17 15:23:43 +00002101
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002102 sample = 0;
2103 for(partition = 0; partition < partitions; partition++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00002104 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002105 return false; /* the read_callback_ sets the state for us */
Josh Coalsona37ba462002-08-19 21:36:39 +00002106 partitioned_rice_contents->parameters[partition] = rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00002107 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
Josh Coalsonb28559a2002-04-25 05:21:09 +00002108 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
2109 if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder))
2110 return false; /* the read_callback_ sets the state for us */
2111 sample += u;
Josh Coalson2051dd42001-04-12 22:22:34 +00002112 }
2113 else {
Josh Coalsonaec256b2002-03-12 16:19:54 +00002114 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder))
Josh Coalson2051dd42001-04-12 22:22:34 +00002115 return false; /* the read_callback_ sets the state for us */
Josh Coalsona37ba462002-08-19 21:36:39 +00002116 partitioned_rice_contents->raw_bits[partition] = rice_parameter;
Josh Coalson2051dd42001-04-12 22:22:34 +00002117 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
Josh Coalsonaec256b2002-03-12 16:19:54 +00002118 if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder))
Josh Coalson2051dd42001-04-12 22:22:34 +00002119 return false; /* the read_callback_ sets the state for us */
2120 residual[sample] = i;
2121 }
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002122 }
2123 }
2124
2125 return true;
2126}
2127
Josh Coalson570db862002-07-31 06:58:16 +00002128FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002129{
Josh Coalsonaec256b2002-03-12 16:19:54 +00002130 if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) {
Josh Coalson77e3f312001-06-23 03:03:24 +00002131 FLAC__uint32 zero = 0;
Josh Coalsonaec256b2002-03-12 16:19:54 +00002132 if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder))
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002133 return false; /* the read_callback_ sets the state for us */
2134 if(zero != 0) {
Josh Coalson090f8f72002-06-04 05:53:04 +00002135 decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data);
Josh Coalsonfa697a92001-08-16 20:07:29 +00002136 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002137 }
2138 }
2139 return true;
2140}
2141
Josh Coalson77e3f312001-06-23 03:03:24 +00002142FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data)
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002143{
2144 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
2145 FLAC__StreamDecoderReadStatus status;
Josh Coalsonaec256b2002-03-12 16:19:54 +00002146
Josh Coalsonfa697a92001-08-16 20:07:29 +00002147 status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data);
Josh Coalson090f8f72002-06-04 05:53:04 +00002148 if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM)
Josh Coalsonfa697a92001-08-16 20:07:29 +00002149 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
Josh Coalson090f8f72002-06-04 05:53:04 +00002150 else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT)
Josh Coalsonfa697a92001-08-16 20:07:29 +00002151 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
Josh Coalson090f8f72002-06-04 05:53:04 +00002152 return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
Josh Coalsonbb7f6b92000-12-10 04:09:52 +00002153}