|
715
|
1 /* libFLAC - Free Lossless Audio Codec library
|
|
|
2 * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
|
|
|
3 *
|
|
|
4 * Redistribution and use in source and binary forms, with or without
|
|
|
5 * modification, are permitted provided that the following conditions
|
|
|
6 * are met:
|
|
|
7 *
|
|
|
8 * - Redistributions of source code must retain the above copyright
|
|
|
9 * notice, this list of conditions and the following disclaimer.
|
|
|
10 *
|
|
|
11 * - 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.
|
|
|
30 */
|
|
|
31
|
|
721
|
32 #define USE_VFS 1
|
|
|
33 #include <audacious/vfs.h>
|
|
|
34
|
|
715
|
35 #if HAVE_CONFIG_H
|
|
|
36 # include <config.h>
|
|
|
37 #endif
|
|
|
38
|
|
|
39 #if defined _MSC_VER || defined __MINGW32__
|
|
|
40 #include <io.h> /* for _setmode() */
|
|
|
41 #include <fcntl.h> /* for _O_BINARY */
|
|
|
42 #endif
|
|
|
43 #if defined __CYGWIN__ || defined __EMX__
|
|
|
44 #include <io.h> /* for setmode(), O_BINARY */
|
|
|
45 #include <fcntl.h> /* for _O_BINARY */
|
|
|
46 #endif
|
|
|
47 #include <stdio.h>
|
|
|
48 #include <stdlib.h> /* for malloc() */
|
|
|
49 #include <string.h> /* for memset/memcpy() */
|
|
|
50 #include <sys/stat.h> /* for stat() */
|
|
|
51 #include <sys/types.h> /* for off_t */
|
|
721
|
52 #if defined USE_VFS || defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__
|
|
715
|
53 #if _MSC_VER <= 1600 || defined __BORLANDC__ /* @@@ [2G limit] */
|
|
|
54 #define fseeko fseek
|
|
|
55 #define ftello ftell
|
|
|
56 #endif
|
|
|
57 #endif
|
|
|
58 #include "FLAC/assert.h"
|
|
|
59 #include "protected/stream_decoder.h"
|
|
|
60 #include "private/bitreader.h"
|
|
|
61 #include "private/bitmath.h"
|
|
|
62 #include "private/cpu.h"
|
|
|
63 #include "private/crc.h"
|
|
|
64 #include "private/fixed.h"
|
|
|
65 #include "private/format.h"
|
|
|
66 #include "private/lpc.h"
|
|
|
67 #include "private/md5.h"
|
|
|
68 #include "private/memory.h"
|
|
|
69
|
|
|
70 #ifdef max
|
|
|
71 #undef max
|
|
|
72 #endif
|
|
|
73 #define max(a,b) ((a)>(b)?(a):(b))
|
|
|
74
|
|
|
75 /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */
|
|
|
76 #ifdef _MSC_VER
|
|
|
77 #define FLAC__U64L(x) x
|
|
|
78 #else
|
|
|
79 #define FLAC__U64L(x) x##LLU
|
|
|
80 #endif
|
|
|
81
|
|
|
82
|
|
|
83 /* technically this should be in an "export.c" but this is convenient enough */
|
|
|
84 FLAC_API int FLAC_API_SUPPORTS_OGG_FLAC =
|
|
|
85 #if FLAC__HAS_OGG
|
|
|
86 1
|
|
|
87 #else
|
|
|
88 0
|
|
|
89 #endif
|
|
|
90 ;
|
|
|
91
|
|
|
92
|
|
|
93 /***********************************************************************
|
|
|
94 *
|
|
|
95 * Private static data
|
|
|
96 *
|
|
|
97 ***********************************************************************/
|
|
|
98
|
|
|
99 static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' };
|
|
|
100
|
|
|
101 /***********************************************************************
|
|
|
102 *
|
|
|
103 * Private class method prototypes
|
|
|
104 *
|
|
|
105 ***********************************************************************/
|
|
|
106
|
|
|
107 static void set_defaults_(FLAC__StreamDecoder *decoder);
|
|
721
|
108 #ifndef USE_VFS
|
|
715
|
109 static FILE *get_binary_stdin_(void);
|
|
721
|
110 #endif
|
|
715
|
111 static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels);
|
|
|
112 static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id);
|
|
|
113 static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder);
|
|
|
114 static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder);
|
|
|
115 static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
|
|
|
116 static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length);
|
|
|
117 static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj);
|
|
|
118 static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj);
|
|
|
119 static FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj);
|
|
|
120 static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder);
|
|
|
121 static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder);
|
|
|
122 static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode);
|
|
|
123 static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder);
|
|
|
124 static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
|
|
|
125 static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
|
|
|
126 static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
|
|
|
127 static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode);
|
|
|
128 static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode);
|
|
|
129 static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual);
|
|
|
130 static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder);
|
|
|
131 static FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data);
|
|
|
132 #if FLAC__HAS_OGG
|
|
|
133 static FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes);
|
|
|
134 static FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
|
|
|
135 #endif
|
|
|
136 static FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
|
|
|
137 static void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status);
|
|
|
138 static FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
|
|
|
139 #if FLAC__HAS_OGG
|
|
|
140 static FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample);
|
|
|
141 #endif
|
|
|
142 static FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data);
|
|
|
143 static FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data);
|
|
|
144 static FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data);
|
|
|
145 static FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data);
|
|
|
146 static FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data);
|
|
|
147
|
|
|
148 /***********************************************************************
|
|
|
149 *
|
|
|
150 * Private class data
|
|
|
151 *
|
|
|
152 ***********************************************************************/
|
|
|
153
|
|
|
154 typedef struct FLAC__StreamDecoderPrivate {
|
|
|
155 #if FLAC__HAS_OGG
|
|
|
156 FLAC__bool is_ogg;
|
|
|
157 #endif
|
|
|
158 FLAC__StreamDecoderReadCallback read_callback;
|
|
|
159 FLAC__StreamDecoderSeekCallback seek_callback;
|
|
|
160 FLAC__StreamDecoderTellCallback tell_callback;
|
|
|
161 FLAC__StreamDecoderLengthCallback length_callback;
|
|
|
162 FLAC__StreamDecoderEofCallback eof_callback;
|
|
|
163 FLAC__StreamDecoderWriteCallback write_callback;
|
|
|
164 FLAC__StreamDecoderMetadataCallback metadata_callback;
|
|
|
165 FLAC__StreamDecoderErrorCallback error_callback;
|
|
|
166 /* generic 32-bit datapath: */
|
|
|
167 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[]);
|
|
|
168 /* generic 64-bit datapath: */
|
|
|
169 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[]);
|
|
|
170 /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */
|
|
|
171 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[]);
|
|
|
172 /* 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: */
|
|
|
173 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[]);
|
|
|
174 void *client_data;
|
|
721
|
175 VFSFile *file; /* only used if FLAC__stream_decoder_init_file()/FLAC__stream_decoder_init_file() called, else NULL */
|
|
715
|
176 FLAC__BitReader *input;
|
|
|
177 FLAC__int32 *output[FLAC__MAX_CHANNELS];
|
|
|
178 FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */
|
|
|
179 FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS];
|
|
|
180 unsigned output_capacity, output_channels;
|
|
|
181 FLAC__uint32 last_frame_number;
|
|
|
182 FLAC__uint32 last_block_size;
|
|
|
183 FLAC__uint64 samples_decoded;
|
|
|
184 FLAC__bool has_stream_info, has_seek_table;
|
|
|
185 FLAC__StreamMetadata stream_info;
|
|
|
186 FLAC__StreamMetadata seek_table;
|
|
|
187 FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */
|
|
|
188 FLAC__byte *metadata_filter_ids;
|
|
|
189 unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */
|
|
|
190 FLAC__Frame frame;
|
|
|
191 FLAC__bool cached; /* true if there is a byte in lookahead */
|
|
|
192 FLAC__CPUInfo cpuinfo;
|
|
|
193 FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */
|
|
|
194 FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */
|
|
|
195 /* unaligned (original) pointers to allocated data */
|
|
|
196 FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS];
|
|
|
197 FLAC__bool do_md5_checking; /* initially gets protected_->md5_checking but is turned off after a seek or if the metadata has a zero MD5 */
|
|
|
198 FLAC__bool internal_reset_hack; /* used only during init() so we can call reset to set up the decoder without rewinding the input */
|
|
|
199 FLAC__bool is_seeking;
|
|
|
200 struct FLAC__MD5Context md5context;
|
|
|
201 FLAC__byte computed_md5sum[16]; /* this is the sum we computed from the decoded data */
|
|
|
202 /* (the rest of these are only used for seeking) */
|
|
|
203 FLAC__Frame last_frame; /* holds the info of the last frame we seeked to */
|
|
|
204 FLAC__uint64 first_frame_offset; /* hint to the seek routine of where in the stream the first audio frame starts */
|
|
|
205 FLAC__uint64 target_sample;
|
|
|
206 unsigned unparseable_frame_count; /* used to tell whether we're decoding a future version of FLAC or just got a bad sync */
|
|
|
207 #if FLAC__HAS_OGG
|
|
|
208 FLAC__bool got_a_frame; /* hack needed in Ogg FLAC seek routine to check when process_single() actually writes a frame */
|
|
|
209 #endif
|
|
|
210 } FLAC__StreamDecoderPrivate;
|
|
|
211
|
|
|
212 /***********************************************************************
|
|
|
213 *
|
|
|
214 * Public static class data
|
|
|
215 *
|
|
|
216 ***********************************************************************/
|
|
|
217
|
|
|
218 FLAC_API const char * const FLAC__StreamDecoderStateString[] = {
|
|
|
219 "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA",
|
|
|
220 "FLAC__STREAM_DECODER_READ_METADATA",
|
|
|
221 "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC",
|
|
|
222 "FLAC__STREAM_DECODER_READ_FRAME",
|
|
|
223 "FLAC__STREAM_DECODER_END_OF_STREAM",
|
|
|
224 "FLAC__STREAM_DECODER_OGG_ERROR",
|
|
|
225 "FLAC__STREAM_DECODER_SEEK_ERROR",
|
|
|
226 "FLAC__STREAM_DECODER_ABORTED",
|
|
|
227 "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR",
|
|
|
228 "FLAC__STREAM_DECODER_UNINITIALIZED"
|
|
|
229 };
|
|
|
230
|
|
|
231 FLAC_API const char * const FLAC__StreamDecoderInitStatusString[] = {
|
|
|
232 "FLAC__STREAM_DECODER_INIT_STATUS_OK",
|
|
|
233 "FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER",
|
|
|
234 "FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS",
|
|
|
235 "FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR",
|
|
|
236 "FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE",
|
|
|
237 "FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED"
|
|
|
238 };
|
|
|
239
|
|
|
240 FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = {
|
|
|
241 "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE",
|
|
|
242 "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM",
|
|
|
243 "FLAC__STREAM_DECODER_READ_STATUS_ABORT"
|
|
|
244 };
|
|
|
245
|
|
|
246 FLAC_API const char * const FLAC__StreamDecoderSeekStatusString[] = {
|
|
|
247 "FLAC__STREAM_DECODER_SEEK_STATUS_OK",
|
|
|
248 "FLAC__STREAM_DECODER_SEEK_STATUS_ERROR",
|
|
|
249 "FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED"
|
|
|
250 };
|
|
|
251
|
|
|
252 FLAC_API const char * const FLAC__StreamDecoderTellStatusString[] = {
|
|
|
253 "FLAC__STREAM_DECODER_TELL_STATUS_OK",
|
|
|
254 "FLAC__STREAM_DECODER_TELL_STATUS_ERROR",
|
|
|
255 "FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED"
|
|
|
256 };
|
|
|
257
|
|
|
258 FLAC_API const char * const FLAC__StreamDecoderLengthStatusString[] = {
|
|
|
259 "FLAC__STREAM_DECODER_LENGTH_STATUS_OK",
|
|
|
260 "FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR",
|
|
|
261 "FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED"
|
|
|
262 };
|
|
|
263
|
|
|
264 FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = {
|
|
|
265 "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE",
|
|
|
266 "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT"
|
|
|
267 };
|
|
|
268
|
|
|
269 FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = {
|
|
|
270 "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC",
|
|
|
271 "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER",
|
|
|
272 "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH",
|
|
|
273 "FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM"
|
|
|
274 };
|
|
|
275
|
|
|
276 /***********************************************************************
|
|
|
277 *
|
|
|
278 * Class constructor/destructor
|
|
|
279 *
|
|
|
280 ***********************************************************************/
|
|
|
281 FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new(void)
|
|
|
282 {
|
|
|
283 FLAC__StreamDecoder *decoder;
|
|
|
284 unsigned i;
|
|
|
285
|
|
|
286 FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */
|
|
|
287
|
|
|
288 decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder));
|
|
|
289 if(decoder == 0) {
|
|
|
290 return 0;
|
|
|
291 }
|
|
|
292
|
|
|
293 decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected));
|
|
|
294 if(decoder->protected_ == 0) {
|
|
|
295 free(decoder);
|
|
|
296 return 0;
|
|
|
297 }
|
|
|
298
|
|
|
299 decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate));
|
|
|
300 if(decoder->private_ == 0) {
|
|
|
301 free(decoder->protected_);
|
|
|
302 free(decoder);
|
|
|
303 return 0;
|
|
|
304 }
|
|
|
305
|
|
|
306 decoder->private_->input = FLAC__bitreader_new();
|
|
|
307 if(decoder->private_->input == 0) {
|
|
|
308 free(decoder->private_);
|
|
|
309 free(decoder->protected_);
|
|
|
310 free(decoder);
|
|
|
311 return 0;
|
|
|
312 }
|
|
|
313
|
|
|
314 decoder->private_->metadata_filter_ids_capacity = 16;
|
|
|
315 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) {
|
|
|
316 FLAC__bitreader_delete(decoder->private_->input);
|
|
|
317 free(decoder->private_);
|
|
|
318 free(decoder->protected_);
|
|
|
319 free(decoder);
|
|
|
320 return 0;
|
|
|
321 }
|
|
|
322
|
|
|
323 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
|
|
|
324 decoder->private_->output[i] = 0;
|
|
|
325 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
|
|
|
326 }
|
|
|
327
|
|
|
328 decoder->private_->output_capacity = 0;
|
|
|
329 decoder->private_->output_channels = 0;
|
|
|
330 decoder->private_->has_seek_table = false;
|
|
|
331
|
|
|
332 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
|
|
|
333 FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]);
|
|
|
334
|
|
|
335 decoder->private_->file = 0;
|
|
|
336
|
|
|
337 set_defaults_(decoder);
|
|
|
338
|
|
|
339 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
|
|
|
340
|
|
|
341 return decoder;
|
|
|
342 }
|
|
|
343
|
|
|
344 FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder)
|
|
|
345 {
|
|
|
346 unsigned i;
|
|
|
347
|
|
|
348 FLAC__ASSERT(0 != decoder);
|
|
|
349 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
350 FLAC__ASSERT(0 != decoder->private_);
|
|
|
351 FLAC__ASSERT(0 != decoder->private_->input);
|
|
|
352
|
|
|
353 (void)FLAC__stream_decoder_finish(decoder);
|
|
|
354
|
|
|
355 if(0 != decoder->private_->metadata_filter_ids)
|
|
|
356 free(decoder->private_->metadata_filter_ids);
|
|
|
357
|
|
|
358 FLAC__bitreader_delete(decoder->private_->input);
|
|
|
359
|
|
|
360 for(i = 0; i < FLAC__MAX_CHANNELS; i++)
|
|
|
361 FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]);
|
|
|
362
|
|
|
363 free(decoder->private_);
|
|
|
364 free(decoder->protected_);
|
|
|
365 free(decoder);
|
|
|
366 }
|
|
|
367
|
|
|
368 /***********************************************************************
|
|
|
369 *
|
|
|
370 * Public class methods
|
|
|
371 *
|
|
|
372 ***********************************************************************/
|
|
|
373
|
|
|
374 static FLAC__StreamDecoderInitStatus init_stream_internal_(
|
|
|
375 FLAC__StreamDecoder *decoder,
|
|
|
376 FLAC__StreamDecoderReadCallback read_callback,
|
|
|
377 FLAC__StreamDecoderSeekCallback seek_callback,
|
|
|
378 FLAC__StreamDecoderTellCallback tell_callback,
|
|
|
379 FLAC__StreamDecoderLengthCallback length_callback,
|
|
|
380 FLAC__StreamDecoderEofCallback eof_callback,
|
|
|
381 FLAC__StreamDecoderWriteCallback write_callback,
|
|
|
382 FLAC__StreamDecoderMetadataCallback metadata_callback,
|
|
|
383 FLAC__StreamDecoderErrorCallback error_callback,
|
|
|
384 void *client_data,
|
|
|
385 FLAC__bool is_ogg
|
|
|
386 )
|
|
|
387 {
|
|
|
388 FLAC__ASSERT(0 != decoder);
|
|
|
389
|
|
|
390 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
|
|
|
391 return FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
|
|
|
392
|
|
|
393 #if !FLAC__HAS_OGG
|
|
|
394 if(is_ogg)
|
|
|
395 return FLAC__STREAM_DECODER_INIT_STATUS_UNSUPPORTED_CONTAINER;
|
|
|
396 #endif
|
|
|
397
|
|
|
398 if(
|
|
|
399 0 == read_callback ||
|
|
|
400 0 == write_callback ||
|
|
|
401 0 == error_callback ||
|
|
|
402 (seek_callback && (0 == tell_callback || 0 == length_callback || 0 == eof_callback))
|
|
|
403 )
|
|
|
404 return FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
|
|
|
405
|
|
|
406 #if FLAC__HAS_OGG
|
|
|
407 decoder->private_->is_ogg = is_ogg;
|
|
|
408 if(is_ogg && !FLAC__ogg_decoder_aspect_init(&decoder->protected_->ogg_decoder_aspect))
|
|
|
409 return decoder->protected_->state = FLAC__STREAM_DECODER_OGG_ERROR;
|
|
|
410 #endif
|
|
|
411
|
|
|
412 /*
|
|
|
413 * get the CPU info and set the function pointers
|
|
|
414 */
|
|
|
415 FLAC__cpu_info(&decoder->private_->cpuinfo);
|
|
|
416 /* first default to the non-asm routines */
|
|
|
417 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal;
|
|
|
418 decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide;
|
|
|
419 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal;
|
|
|
420 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal;
|
|
|
421 /* now override with asm where appropriate */
|
|
|
422 #ifndef FLAC__NO_ASM
|
|
|
423 if(decoder->private_->cpuinfo.use_asm) {
|
|
|
424 #ifdef FLAC__CPU_IA32
|
|
|
425 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32);
|
|
|
426 #ifdef FLAC__HAS_NASM
|
|
|
427 if(decoder->private_->cpuinfo.data.ia32.mmx) {
|
|
|
428 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
|
|
|
429 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx;
|
|
|
430 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx;
|
|
|
431 }
|
|
|
432 else {
|
|
|
433 decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32;
|
|
|
434 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32;
|
|
|
435 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32;
|
|
|
436 }
|
|
|
437 #endif
|
|
|
438 #elif defined FLAC__CPU_PPC
|
|
|
439 FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC);
|
|
|
440 if(decoder->private_->cpuinfo.data.ppc.altivec) {
|
|
|
441 decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16;
|
|
|
442 decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8;
|
|
|
443 }
|
|
|
444 #endif
|
|
|
445 }
|
|
|
446 #endif
|
|
|
447
|
|
|
448 /* from here on, errors are fatal */
|
|
|
449
|
|
|
450 if(!FLAC__bitreader_init(decoder->private_->input, read_callback_, decoder)) {
|
|
|
451 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
452 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
|
|
|
453 }
|
|
|
454
|
|
|
455 decoder->private_->read_callback = read_callback;
|
|
|
456 decoder->private_->seek_callback = seek_callback;
|
|
|
457 decoder->private_->tell_callback = tell_callback;
|
|
|
458 decoder->private_->length_callback = length_callback;
|
|
|
459 decoder->private_->eof_callback = eof_callback;
|
|
|
460 decoder->private_->write_callback = write_callback;
|
|
|
461 decoder->private_->metadata_callback = metadata_callback;
|
|
|
462 decoder->private_->error_callback = error_callback;
|
|
|
463 decoder->private_->client_data = client_data;
|
|
|
464 decoder->private_->last_frame_number = 0;
|
|
|
465 decoder->private_->last_block_size = 0;
|
|
|
466 decoder->private_->samples_decoded = 0;
|
|
|
467 decoder->private_->has_stream_info = false;
|
|
|
468 decoder->private_->cached = false;
|
|
|
469
|
|
|
470 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
|
|
|
471 decoder->private_->is_seeking = false;
|
|
|
472
|
|
|
473 decoder->private_->internal_reset_hack = true; /* so the following reset does not try to rewind the input */
|
|
|
474 if(!FLAC__stream_decoder_reset(decoder)) {
|
|
|
475 /* above call sets the state for us */
|
|
|
476 return FLAC__STREAM_DECODER_INIT_STATUS_MEMORY_ALLOCATION_ERROR;
|
|
|
477 }
|
|
|
478
|
|
|
479 return FLAC__STREAM_DECODER_INIT_STATUS_OK;
|
|
|
480 }
|
|
|
481
|
|
|
482 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_stream(
|
|
|
483 FLAC__StreamDecoder *decoder,
|
|
|
484 FLAC__StreamDecoderReadCallback read_callback,
|
|
|
485 FLAC__StreamDecoderSeekCallback seek_callback,
|
|
|
486 FLAC__StreamDecoderTellCallback tell_callback,
|
|
|
487 FLAC__StreamDecoderLengthCallback length_callback,
|
|
|
488 FLAC__StreamDecoderEofCallback eof_callback,
|
|
|
489 FLAC__StreamDecoderWriteCallback write_callback,
|
|
|
490 FLAC__StreamDecoderMetadataCallback metadata_callback,
|
|
|
491 FLAC__StreamDecoderErrorCallback error_callback,
|
|
|
492 void *client_data
|
|
|
493 )
|
|
|
494 {
|
|
|
495 return init_stream_internal_(
|
|
|
496 decoder,
|
|
|
497 read_callback,
|
|
|
498 seek_callback,
|
|
|
499 tell_callback,
|
|
|
500 length_callback,
|
|
|
501 eof_callback,
|
|
|
502 write_callback,
|
|
|
503 metadata_callback,
|
|
|
504 error_callback,
|
|
|
505 client_data,
|
|
|
506 /*is_ogg=*/false
|
|
|
507 );
|
|
|
508 }
|
|
|
509
|
|
|
510 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_stream(
|
|
|
511 FLAC__StreamDecoder *decoder,
|
|
|
512 FLAC__StreamDecoderReadCallback read_callback,
|
|
|
513 FLAC__StreamDecoderSeekCallback seek_callback,
|
|
|
514 FLAC__StreamDecoderTellCallback tell_callback,
|
|
|
515 FLAC__StreamDecoderLengthCallback length_callback,
|
|
|
516 FLAC__StreamDecoderEofCallback eof_callback,
|
|
|
517 FLAC__StreamDecoderWriteCallback write_callback,
|
|
|
518 FLAC__StreamDecoderMetadataCallback metadata_callback,
|
|
|
519 FLAC__StreamDecoderErrorCallback error_callback,
|
|
|
520 void *client_data
|
|
|
521 )
|
|
|
522 {
|
|
|
523 return init_stream_internal_(
|
|
|
524 decoder,
|
|
|
525 read_callback,
|
|
|
526 seek_callback,
|
|
|
527 tell_callback,
|
|
|
528 length_callback,
|
|
|
529 eof_callback,
|
|
|
530 write_callback,
|
|
|
531 metadata_callback,
|
|
|
532 error_callback,
|
|
|
533 client_data,
|
|
|
534 /*is_ogg=*/true
|
|
|
535 );
|
|
|
536 }
|
|
|
537
|
|
|
538 static FLAC__StreamDecoderInitStatus init_FILE_internal_(
|
|
|
539 FLAC__StreamDecoder *decoder,
|
|
721
|
540 VFSFile *file,
|
|
715
|
541 FLAC__StreamDecoderWriteCallback write_callback,
|
|
|
542 FLAC__StreamDecoderMetadataCallback metadata_callback,
|
|
|
543 FLAC__StreamDecoderErrorCallback error_callback,
|
|
|
544 void *client_data,
|
|
|
545 FLAC__bool is_ogg
|
|
|
546 )
|
|
|
547 {
|
|
|
548 FLAC__ASSERT(0 != decoder);
|
|
|
549 FLAC__ASSERT(0 != file);
|
|
|
550
|
|
|
551 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
|
|
|
552 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
|
|
|
553
|
|
|
554 if(0 == write_callback || 0 == error_callback)
|
|
|
555 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
|
|
|
556
|
|
|
557 /*
|
|
|
558 * To make sure that our file does not go unclosed after an error, we
|
|
|
559 * must assign the FILE pointer before any further error can occur in
|
|
|
560 * this routine.
|
|
|
561 */
|
|
721
|
562 #ifndef USE_VFS
|
|
715
|
563 if(file == stdin)
|
|
|
564 file = get_binary_stdin_(); /* just to be safe */
|
|
721
|
565 #endif
|
|
715
|
566
|
|
|
567 decoder->private_->file = file;
|
|
|
568
|
|
|
569 return init_stream_internal_(
|
|
|
570 decoder,
|
|
|
571 file_read_callback_,
|
|
721
|
572 #ifdef USE_VFS
|
|
|
573 file_seek_callback_,
|
|
|
574 file_tell_callback_,
|
|
|
575 file_length_callback_,
|
|
|
576 #else
|
|
715
|
577 decoder->private_->file == stdin? 0: file_seek_callback_,
|
|
|
578 decoder->private_->file == stdin? 0: file_tell_callback_,
|
|
|
579 decoder->private_->file == stdin? 0: file_length_callback_,
|
|
721
|
580 #endif
|
|
715
|
581 file_eof_callback_,
|
|
|
582 write_callback,
|
|
|
583 metadata_callback,
|
|
|
584 error_callback,
|
|
|
585 client_data,
|
|
|
586 is_ogg
|
|
|
587 );
|
|
|
588 }
|
|
|
589
|
|
|
590 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_FILE(
|
|
|
591 FLAC__StreamDecoder *decoder,
|
|
721
|
592 VFSFile *file,
|
|
715
|
593 FLAC__StreamDecoderWriteCallback write_callback,
|
|
|
594 FLAC__StreamDecoderMetadataCallback metadata_callback,
|
|
|
595 FLAC__StreamDecoderErrorCallback error_callback,
|
|
|
596 void *client_data
|
|
|
597 )
|
|
|
598 {
|
|
|
599 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
|
|
|
600 }
|
|
|
601
|
|
|
602 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_FILE(
|
|
|
603 FLAC__StreamDecoder *decoder,
|
|
721
|
604 VFSFile *file,
|
|
715
|
605 FLAC__StreamDecoderWriteCallback write_callback,
|
|
|
606 FLAC__StreamDecoderMetadataCallback metadata_callback,
|
|
|
607 FLAC__StreamDecoderErrorCallback error_callback,
|
|
|
608 void *client_data
|
|
|
609 )
|
|
|
610 {
|
|
|
611 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
|
|
|
612 }
|
|
|
613
|
|
|
614 static FLAC__StreamDecoderInitStatus init_file_internal_(
|
|
|
615 FLAC__StreamDecoder *decoder,
|
|
|
616 const char *filename,
|
|
|
617 FLAC__StreamDecoderWriteCallback write_callback,
|
|
|
618 FLAC__StreamDecoderMetadataCallback metadata_callback,
|
|
|
619 FLAC__StreamDecoderErrorCallback error_callback,
|
|
|
620 void *client_data,
|
|
|
621 FLAC__bool is_ogg
|
|
|
622 )
|
|
|
623 {
|
|
721
|
624 VFSFile *file;
|
|
715
|
625
|
|
|
626 FLAC__ASSERT(0 != decoder);
|
|
|
627
|
|
|
628 /*
|
|
|
629 * To make sure that our file does not go unclosed after an error, we
|
|
|
630 * have to do the same entrance checks here that are later performed
|
|
|
631 * in FLAC__stream_decoder_init_FILE() before the FILE* is assigned.
|
|
|
632 */
|
|
|
633 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
|
|
|
634 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_ALREADY_INITIALIZED;
|
|
|
635
|
|
|
636 if(0 == write_callback || 0 == error_callback)
|
|
|
637 return decoder->protected_->state = FLAC__STREAM_DECODER_INIT_STATUS_INVALID_CALLBACKS;
|
|
|
638
|
|
721
|
639 #ifdef USE_VFS
|
|
|
640 file = vfs_fopen(filename, "rb");
|
|
|
641 #else
|
|
715
|
642 file = filename? fopen(filename, "rb") : stdin;
|
|
721
|
643 #endif
|
|
715
|
644
|
|
|
645 if(0 == file)
|
|
|
646 return FLAC__STREAM_DECODER_INIT_STATUS_ERROR_OPENING_FILE;
|
|
|
647
|
|
|
648 return init_FILE_internal_(decoder, file, write_callback, metadata_callback, error_callback, client_data, is_ogg);
|
|
|
649 }
|
|
|
650
|
|
|
651 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_file(
|
|
|
652 FLAC__StreamDecoder *decoder,
|
|
|
653 const char *filename,
|
|
|
654 FLAC__StreamDecoderWriteCallback write_callback,
|
|
|
655 FLAC__StreamDecoderMetadataCallback metadata_callback,
|
|
|
656 FLAC__StreamDecoderErrorCallback error_callback,
|
|
|
657 void *client_data
|
|
|
658 )
|
|
|
659 {
|
|
|
660 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/false);
|
|
|
661 }
|
|
|
662
|
|
|
663 FLAC_API FLAC__StreamDecoderInitStatus FLAC__stream_decoder_init_ogg_file(
|
|
|
664 FLAC__StreamDecoder *decoder,
|
|
|
665 const char *filename,
|
|
|
666 FLAC__StreamDecoderWriteCallback write_callback,
|
|
|
667 FLAC__StreamDecoderMetadataCallback metadata_callback,
|
|
|
668 FLAC__StreamDecoderErrorCallback error_callback,
|
|
|
669 void *client_data
|
|
|
670 )
|
|
|
671 {
|
|
|
672 return init_file_internal_(decoder, filename, write_callback, metadata_callback, error_callback, client_data, /*is_ogg=*/true);
|
|
|
673 }
|
|
|
674
|
|
|
675 FLAC_API FLAC__bool FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder)
|
|
|
676 {
|
|
|
677 FLAC__bool md5_failed = false;
|
|
|
678 unsigned i;
|
|
|
679
|
|
|
680 FLAC__ASSERT(0 != decoder);
|
|
|
681 FLAC__ASSERT(0 != decoder->private_);
|
|
|
682 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
683
|
|
|
684 if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED)
|
|
|
685 return true;
|
|
|
686
|
|
|
687 /* see the comment in FLAC__seekable_stream_decoder_reset() as to why we
|
|
|
688 * always call FLAC__MD5Final()
|
|
|
689 */
|
|
|
690 FLAC__MD5Final(decoder->private_->computed_md5sum, &decoder->private_->md5context);
|
|
|
691
|
|
|
692 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
|
|
|
693 free(decoder->private_->seek_table.data.seek_table.points);
|
|
|
694 decoder->private_->seek_table.data.seek_table.points = 0;
|
|
|
695 decoder->private_->has_seek_table = false;
|
|
|
696 }
|
|
|
697 FLAC__bitreader_free(decoder->private_->input);
|
|
|
698 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
|
|
|
699 /* WATCHOUT:
|
|
|
700 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
|
|
|
701 * output arrays have a buffer of up to 3 zeroes in front
|
|
|
702 * (at negative indices) for alignment purposes; we use 4
|
|
|
703 * to keep the data well-aligned.
|
|
|
704 */
|
|
|
705 if(0 != decoder->private_->output[i]) {
|
|
|
706 free(decoder->private_->output[i]-4);
|
|
|
707 decoder->private_->output[i] = 0;
|
|
|
708 }
|
|
|
709 if(0 != decoder->private_->residual_unaligned[i]) {
|
|
|
710 free(decoder->private_->residual_unaligned[i]);
|
|
|
711 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
|
|
|
712 }
|
|
|
713 }
|
|
|
714 decoder->private_->output_capacity = 0;
|
|
|
715 decoder->private_->output_channels = 0;
|
|
|
716
|
|
|
717 #if FLAC__HAS_OGG
|
|
|
718 if(decoder->private_->is_ogg)
|
|
|
719 FLAC__ogg_decoder_aspect_finish(&decoder->protected_->ogg_decoder_aspect);
|
|
|
720 #endif
|
|
|
721
|
|
|
722 if(0 != decoder->private_->file) {
|
|
721
|
723 #ifndef USE_VFS
|
|
715
|
724 if(decoder->private_->file != stdin)
|
|
|
725 fclose(decoder->private_->file);
|
|
1151
|
726 #else
|
|
|
727 vfs_fclose(decoder->private_->file);
|
|
721
|
728 #endif
|
|
715
|
729 decoder->private_->file = 0;
|
|
|
730 }
|
|
|
731
|
|
|
732 if(decoder->private_->do_md5_checking) {
|
|
|
733 if(memcmp(decoder->private_->stream_info.data.stream_info.md5sum, decoder->private_->computed_md5sum, 16))
|
|
|
734 md5_failed = true;
|
|
|
735 }
|
|
|
736 decoder->private_->is_seeking = false;
|
|
|
737
|
|
|
738 set_defaults_(decoder);
|
|
|
739
|
|
|
740 decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED;
|
|
|
741
|
|
|
742 return !md5_failed;
|
|
|
743 }
|
|
|
744
|
|
|
745 FLAC_API FLAC__bool FLAC__stream_decoder_set_ogg_serial_number(FLAC__StreamDecoder *decoder, long value)
|
|
|
746 {
|
|
|
747 FLAC__ASSERT(0 != decoder);
|
|
|
748 FLAC__ASSERT(0 != decoder->private_);
|
|
|
749 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
750 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
|
|
|
751 return false;
|
|
|
752 #if FLAC__HAS_OGG
|
|
|
753 /* can't check decoder->private_->is_ogg since that's not set until init time */
|
|
|
754 FLAC__ogg_decoder_aspect_set_serial_number(&decoder->protected_->ogg_decoder_aspect, value);
|
|
|
755 return true;
|
|
|
756 #else
|
|
|
757 (void)value;
|
|
|
758 return false;
|
|
|
759 #endif
|
|
|
760 }
|
|
|
761
|
|
|
762 FLAC_API FLAC__bool FLAC__stream_decoder_set_md5_checking(FLAC__StreamDecoder *decoder, FLAC__bool value)
|
|
|
763 {
|
|
|
764 FLAC__ASSERT(0 != decoder);
|
|
|
765 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
766 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
|
|
|
767 return false;
|
|
|
768 decoder->protected_->md5_checking = value;
|
|
|
769 return true;
|
|
|
770 }
|
|
|
771
|
|
|
772 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
|
|
|
773 {
|
|
|
774 FLAC__ASSERT(0 != decoder);
|
|
|
775 FLAC__ASSERT(0 != decoder->private_);
|
|
|
776 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
777 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
|
|
|
778 /* double protection */
|
|
|
779 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
|
|
|
780 return false;
|
|
|
781 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
|
|
|
782 return false;
|
|
|
783 decoder->private_->metadata_filter[type] = true;
|
|
|
784 if(type == FLAC__METADATA_TYPE_APPLICATION)
|
|
|
785 decoder->private_->metadata_filter_ids_count = 0;
|
|
|
786 return true;
|
|
|
787 }
|
|
|
788
|
|
|
789 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
|
|
|
790 {
|
|
|
791 FLAC__ASSERT(0 != decoder);
|
|
|
792 FLAC__ASSERT(0 != decoder->private_);
|
|
|
793 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
794 FLAC__ASSERT(0 != id);
|
|
|
795 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
|
|
|
796 return false;
|
|
|
797
|
|
|
798 if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
|
|
|
799 return true;
|
|
|
800
|
|
|
801 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
|
|
|
802
|
|
|
803 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
|
|
|
804 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
|
|
|
805 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
806 return false;
|
|
|
807 }
|
|
|
808 decoder->private_->metadata_filter_ids_capacity *= 2;
|
|
|
809 }
|
|
|
810
|
|
|
811 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));
|
|
|
812 decoder->private_->metadata_filter_ids_count++;
|
|
|
813
|
|
|
814 return true;
|
|
|
815 }
|
|
|
816
|
|
|
817 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder)
|
|
|
818 {
|
|
|
819 unsigned i;
|
|
|
820 FLAC__ASSERT(0 != decoder);
|
|
|
821 FLAC__ASSERT(0 != decoder->private_);
|
|
|
822 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
823 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
|
|
|
824 return false;
|
|
|
825 for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++)
|
|
|
826 decoder->private_->metadata_filter[i] = true;
|
|
|
827 decoder->private_->metadata_filter_ids_count = 0;
|
|
|
828 return true;
|
|
|
829 }
|
|
|
830
|
|
|
831 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type)
|
|
|
832 {
|
|
|
833 FLAC__ASSERT(0 != decoder);
|
|
|
834 FLAC__ASSERT(0 != decoder->private_);
|
|
|
835 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
836 FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE);
|
|
|
837 /* double protection */
|
|
|
838 if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE)
|
|
|
839 return false;
|
|
|
840 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
|
|
|
841 return false;
|
|
|
842 decoder->private_->metadata_filter[type] = false;
|
|
|
843 if(type == FLAC__METADATA_TYPE_APPLICATION)
|
|
|
844 decoder->private_->metadata_filter_ids_count = 0;
|
|
|
845 return true;
|
|
|
846 }
|
|
|
847
|
|
|
848 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4])
|
|
|
849 {
|
|
|
850 FLAC__ASSERT(0 != decoder);
|
|
|
851 FLAC__ASSERT(0 != decoder->private_);
|
|
|
852 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
853 FLAC__ASSERT(0 != id);
|
|
|
854 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
|
|
|
855 return false;
|
|
|
856
|
|
|
857 if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION])
|
|
|
858 return true;
|
|
|
859
|
|
|
860 FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
|
|
|
861
|
|
|
862 if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
|
|
|
863 if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) {
|
|
|
864 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
865 return false;
|
|
|
866 }
|
|
|
867 decoder->private_->metadata_filter_ids_capacity *= 2;
|
|
|
868 }
|
|
|
869
|
|
|
870 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));
|
|
|
871 decoder->private_->metadata_filter_ids_count++;
|
|
|
872
|
|
|
873 return true;
|
|
|
874 }
|
|
|
875
|
|
|
876 FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder)
|
|
|
877 {
|
|
|
878 FLAC__ASSERT(0 != decoder);
|
|
|
879 FLAC__ASSERT(0 != decoder->private_);
|
|
|
880 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
881 if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED)
|
|
|
882 return false;
|
|
|
883 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
|
|
|
884 decoder->private_->metadata_filter_ids_count = 0;
|
|
|
885 return true;
|
|
|
886 }
|
|
|
887
|
|
|
888 FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder)
|
|
|
889 {
|
|
|
890 FLAC__ASSERT(0 != decoder);
|
|
|
891 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
892 return decoder->protected_->state;
|
|
|
893 }
|
|
|
894
|
|
|
895 FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder)
|
|
|
896 {
|
|
|
897 return FLAC__StreamDecoderStateString[decoder->protected_->state];
|
|
|
898 }
|
|
|
899
|
|
|
900 FLAC_API FLAC__bool FLAC__stream_decoder_get_md5_checking(const FLAC__StreamDecoder *decoder)
|
|
|
901 {
|
|
|
902 FLAC__ASSERT(0 != decoder);
|
|
|
903 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
904 return decoder->protected_->md5_checking;
|
|
|
905 }
|
|
|
906
|
|
|
907 FLAC_API FLAC__uint64 FLAC__stream_decoder_get_total_samples(const FLAC__StreamDecoder *decoder)
|
|
|
908 {
|
|
|
909 FLAC__ASSERT(0 != decoder);
|
|
|
910 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
911 return decoder->private_->has_stream_info? decoder->private_->stream_info.data.stream_info.total_samples : 0;
|
|
|
912 }
|
|
|
913
|
|
|
914 FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder)
|
|
|
915 {
|
|
|
916 FLAC__ASSERT(0 != decoder);
|
|
|
917 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
918 return decoder->protected_->channels;
|
|
|
919 }
|
|
|
920
|
|
|
921 FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder)
|
|
|
922 {
|
|
|
923 FLAC__ASSERT(0 != decoder);
|
|
|
924 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
925 return decoder->protected_->channel_assignment;
|
|
|
926 }
|
|
|
927
|
|
|
928 FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder)
|
|
|
929 {
|
|
|
930 FLAC__ASSERT(0 != decoder);
|
|
|
931 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
932 return decoder->protected_->bits_per_sample;
|
|
|
933 }
|
|
|
934
|
|
|
935 FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder)
|
|
|
936 {
|
|
|
937 FLAC__ASSERT(0 != decoder);
|
|
|
938 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
939 return decoder->protected_->sample_rate;
|
|
|
940 }
|
|
|
941
|
|
|
942 FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder)
|
|
|
943 {
|
|
|
944 FLAC__ASSERT(0 != decoder);
|
|
|
945 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
946 return decoder->protected_->blocksize;
|
|
|
947 }
|
|
|
948
|
|
|
949 FLAC_API FLAC__bool FLAC__stream_decoder_get_decode_position(const FLAC__StreamDecoder *decoder, FLAC__uint64 *position)
|
|
|
950 {
|
|
|
951 FLAC__ASSERT(0 != decoder);
|
|
|
952 FLAC__ASSERT(0 != decoder->private_);
|
|
|
953 FLAC__ASSERT(0 != position);
|
|
|
954
|
|
|
955 #if FLAC__HAS_OGG
|
|
|
956 if(decoder->private_->is_ogg)
|
|
|
957 return false;
|
|
|
958 #endif
|
|
|
959 if(0 == decoder->private_->tell_callback)
|
|
|
960 return false;
|
|
|
961 if(decoder->private_->tell_callback(decoder, position, decoder->private_->client_data) != FLAC__STREAM_DECODER_TELL_STATUS_OK)
|
|
|
962 return false;
|
|
|
963 /* should never happen since all FLAC frames and metadata blocks are byte aligned, but check just in case */
|
|
|
964 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input))
|
|
|
965 return false;
|
|
|
966 FLAC__ASSERT(*position >= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder));
|
|
|
967 *position -= FLAC__stream_decoder_get_input_bytes_unconsumed(decoder);
|
|
|
968 return true;
|
|
|
969 }
|
|
|
970
|
|
|
971 FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder)
|
|
|
972 {
|
|
|
973 FLAC__ASSERT(0 != decoder);
|
|
|
974 FLAC__ASSERT(0 != decoder->private_);
|
|
|
975 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
976
|
|
|
977 decoder->private_->samples_decoded = 0;
|
|
|
978 decoder->private_->do_md5_checking = false;
|
|
|
979
|
|
|
980 #if FLAC__HAS_OGG
|
|
|
981 if(decoder->private_->is_ogg)
|
|
|
982 FLAC__ogg_decoder_aspect_flush(&decoder->protected_->ogg_decoder_aspect);
|
|
|
983 #endif
|
|
|
984
|
|
|
985 if(!FLAC__bitreader_clear(decoder->private_->input)) {
|
|
|
986 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
987 return false;
|
|
|
988 }
|
|
|
989 decoder->private_->last_frame_number = 0;
|
|
|
990 decoder->private_->last_block_size = 0;
|
|
|
991 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
992
|
|
|
993 return true;
|
|
|
994 }
|
|
|
995
|
|
|
996 FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder)
|
|
|
997 {
|
|
|
998 FLAC__ASSERT(0 != decoder);
|
|
|
999 FLAC__ASSERT(0 != decoder->private_);
|
|
|
1000 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
1001
|
|
|
1002 if(!FLAC__stream_decoder_flush(decoder)) {
|
|
|
1003 /* above call sets the state for us */
|
|
|
1004 return false;
|
|
|
1005 }
|
|
|
1006
|
|
|
1007 #if FLAC__HAS_OGG
|
|
|
1008 /*@@@ could go in !internal_reset_hack block below */
|
|
|
1009 if(decoder->private_->is_ogg)
|
|
|
1010 FLAC__ogg_decoder_aspect_reset(&decoder->protected_->ogg_decoder_aspect);
|
|
|
1011 #endif
|
|
|
1012
|
|
|
1013 /* Rewind if necessary. If FLAC__stream_decoder_init() is calling us,
|
|
|
1014 * (internal_reset_hack) don't try to rewind since we are already at
|
|
|
1015 * the beginning of the stream and don't want to fail if the input is
|
|
|
1016 * not seekable.
|
|
|
1017 */
|
|
|
1018 if(!decoder->private_->internal_reset_hack) {
|
|
721
|
1019 #ifndef USE_VFS
|
|
715
|
1020 if(decoder->private_->file == stdin)
|
|
|
1021 return false; /* can't rewind stdin, reset fails */
|
|
721
|
1022 #endif
|
|
715
|
1023 if(decoder->private_->seek_callback && decoder->private_->seek_callback(decoder, 0, decoder->private_->client_data) == FLAC__STREAM_DECODER_SEEK_STATUS_ERROR)
|
|
|
1024 return false; /* seekable and seek fails, reset fails */
|
|
|
1025 }
|
|
|
1026 else
|
|
|
1027 decoder->private_->internal_reset_hack = false;
|
|
|
1028
|
|
|
1029 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA;
|
|
|
1030
|
|
|
1031 decoder->private_->has_stream_info = false;
|
|
|
1032 if(decoder->private_->has_seek_table && 0 != decoder->private_->seek_table.data.seek_table.points) {
|
|
|
1033 free(decoder->private_->seek_table.data.seek_table.points);
|
|
|
1034 decoder->private_->seek_table.data.seek_table.points = 0;
|
|
|
1035 decoder->private_->has_seek_table = false;
|
|
|
1036 }
|
|
|
1037 decoder->private_->do_md5_checking = decoder->protected_->md5_checking;
|
|
|
1038
|
|
|
1039 /* We initialize the FLAC__MD5Context even though we may never use it. This
|
|
|
1040 * is because md5 checking may be turned on to start and then turned off if
|
|
|
1041 * a seek occurs. So we init the context here and finalize it in
|
|
|
1042 * FLAC__stream_decoder_finish() to make sure things are always cleaned up
|
|
|
1043 * properly.
|
|
|
1044 */
|
|
|
1045 FLAC__MD5Init(&decoder->private_->md5context);
|
|
|
1046
|
|
|
1047 decoder->private_->first_frame_offset = 0;
|
|
|
1048 decoder->private_->unparseable_frame_count = 0;
|
|
|
1049
|
|
|
1050 return true;
|
|
|
1051 }
|
|
|
1052
|
|
|
1053 FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder)
|
|
|
1054 {
|
|
|
1055 FLAC__bool got_a_frame;
|
|
|
1056 FLAC__ASSERT(0 != decoder);
|
|
|
1057 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
1058
|
|
|
1059 while(1) {
|
|
|
1060 switch(decoder->protected_->state) {
|
|
|
1061 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
|
|
|
1062 if(!find_metadata_(decoder))
|
|
|
1063 return false; /* above function sets the status for us */
|
|
|
1064 break;
|
|
|
1065 case FLAC__STREAM_DECODER_READ_METADATA:
|
|
|
1066 if(!read_metadata_(decoder))
|
|
|
1067 return false; /* above function sets the status for us */
|
|
|
1068 else
|
|
|
1069 return true;
|
|
|
1070 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
|
|
|
1071 if(!frame_sync_(decoder))
|
|
|
1072 return true; /* above function sets the status for us */
|
|
|
1073 break;
|
|
|
1074 case FLAC__STREAM_DECODER_READ_FRAME:
|
|
|
1075 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true))
|
|
|
1076 return false; /* above function sets the status for us */
|
|
|
1077 if(got_a_frame)
|
|
|
1078 return true; /* above function sets the status for us */
|
|
|
1079 break;
|
|
|
1080 case FLAC__STREAM_DECODER_END_OF_STREAM:
|
|
|
1081 case FLAC__STREAM_DECODER_ABORTED:
|
|
|
1082 return true;
|
|
|
1083 default:
|
|
|
1084 FLAC__ASSERT(0);
|
|
|
1085 return false;
|
|
|
1086 }
|
|
|
1087 }
|
|
|
1088 }
|
|
|
1089
|
|
|
1090 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder)
|
|
|
1091 {
|
|
|
1092 FLAC__ASSERT(0 != decoder);
|
|
|
1093 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
1094
|
|
|
1095 while(1) {
|
|
|
1096 switch(decoder->protected_->state) {
|
|
|
1097 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
|
|
|
1098 if(!find_metadata_(decoder))
|
|
|
1099 return false; /* above function sets the status for us */
|
|
|
1100 break;
|
|
|
1101 case FLAC__STREAM_DECODER_READ_METADATA:
|
|
|
1102 if(!read_metadata_(decoder))
|
|
|
1103 return false; /* above function sets the status for us */
|
|
|
1104 break;
|
|
|
1105 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
|
|
|
1106 case FLAC__STREAM_DECODER_READ_FRAME:
|
|
|
1107 case FLAC__STREAM_DECODER_END_OF_STREAM:
|
|
|
1108 case FLAC__STREAM_DECODER_ABORTED:
|
|
|
1109 return true;
|
|
|
1110 default:
|
|
|
1111 FLAC__ASSERT(0);
|
|
|
1112 return false;
|
|
|
1113 }
|
|
|
1114 }
|
|
|
1115 }
|
|
|
1116
|
|
|
1117 FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder)
|
|
|
1118 {
|
|
|
1119 FLAC__bool dummy;
|
|
|
1120 FLAC__ASSERT(0 != decoder);
|
|
|
1121 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
1122
|
|
|
1123 while(1) {
|
|
|
1124 switch(decoder->protected_->state) {
|
|
|
1125 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
|
|
|
1126 if(!find_metadata_(decoder))
|
|
|
1127 return false; /* above function sets the status for us */
|
|
|
1128 break;
|
|
|
1129 case FLAC__STREAM_DECODER_READ_METADATA:
|
|
|
1130 if(!read_metadata_(decoder))
|
|
|
1131 return false; /* above function sets the status for us */
|
|
|
1132 break;
|
|
|
1133 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
|
|
|
1134 if(!frame_sync_(decoder))
|
|
|
1135 return true; /* above function sets the status for us */
|
|
|
1136 break;
|
|
|
1137 case FLAC__STREAM_DECODER_READ_FRAME:
|
|
|
1138 if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true))
|
|
|
1139 return false; /* above function sets the status for us */
|
|
|
1140 break;
|
|
|
1141 case FLAC__STREAM_DECODER_END_OF_STREAM:
|
|
|
1142 case FLAC__STREAM_DECODER_ABORTED:
|
|
|
1143 return true;
|
|
|
1144 default:
|
|
|
1145 FLAC__ASSERT(0);
|
|
|
1146 return false;
|
|
|
1147 }
|
|
|
1148 }
|
|
|
1149 }
|
|
|
1150
|
|
|
1151 FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder)
|
|
|
1152 {
|
|
|
1153 FLAC__bool got_a_frame;
|
|
|
1154 FLAC__ASSERT(0 != decoder);
|
|
|
1155 FLAC__ASSERT(0 != decoder->protected_);
|
|
|
1156
|
|
|
1157 while(1) {
|
|
|
1158 switch(decoder->protected_->state) {
|
|
|
1159 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
|
|
|
1160 case FLAC__STREAM_DECODER_READ_METADATA:
|
|
|
1161 return false; /* above function sets the status for us */
|
|
|
1162 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
|
|
|
1163 if(!frame_sync_(decoder))
|
|
|
1164 return true; /* above function sets the status for us */
|
|
|
1165 break;
|
|
|
1166 case FLAC__STREAM_DECODER_READ_FRAME:
|
|
|
1167 if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false))
|
|
|
1168 return false; /* above function sets the status for us */
|
|
|
1169 if(got_a_frame)
|
|
|
1170 return true; /* above function sets the status for us */
|
|
|
1171 break;
|
|
|
1172 case FLAC__STREAM_DECODER_END_OF_STREAM:
|
|
|
1173 case FLAC__STREAM_DECODER_ABORTED:
|
|
|
1174 return true;
|
|
|
1175 default:
|
|
|
1176 FLAC__ASSERT(0);
|
|
|
1177 return false;
|
|
|
1178 }
|
|
|
1179 }
|
|
|
1180 }
|
|
|
1181
|
|
|
1182 FLAC_API FLAC__bool FLAC__stream_decoder_seek_absolute(FLAC__StreamDecoder *decoder, FLAC__uint64 sample)
|
|
|
1183 {
|
|
|
1184 FLAC__uint64 length;
|
|
|
1185
|
|
|
1186 FLAC__ASSERT(0 != decoder);
|
|
|
1187
|
|
|
1188 if(
|
|
|
1189 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_METADATA &&
|
|
|
1190 decoder->protected_->state != FLAC__STREAM_DECODER_READ_METADATA &&
|
|
|
1191 decoder->protected_->state != FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC &&
|
|
|
1192 decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME &&
|
|
|
1193 decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM
|
|
|
1194 )
|
|
|
1195 return false;
|
|
|
1196
|
|
|
1197 if(0 == decoder->private_->seek_callback)
|
|
|
1198 return false;
|
|
|
1199
|
|
|
1200 FLAC__ASSERT(decoder->private_->seek_callback);
|
|
|
1201 FLAC__ASSERT(decoder->private_->tell_callback);
|
|
|
1202 FLAC__ASSERT(decoder->private_->length_callback);
|
|
|
1203 FLAC__ASSERT(decoder->private_->eof_callback);
|
|
|
1204
|
|
|
1205 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder))
|
|
|
1206 return false;
|
|
|
1207
|
|
|
1208 decoder->private_->is_seeking = true;
|
|
|
1209
|
|
|
1210 /* turn off md5 checking if a seek is attempted */
|
|
|
1211 decoder->private_->do_md5_checking = false;
|
|
|
1212
|
|
|
1213 /* get the file length (currently our algorithm needs to know the length so it's also an error to get FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED) */
|
|
|
1214 if(decoder->private_->length_callback(decoder, &length, decoder->private_->client_data) != FLAC__STREAM_DECODER_LENGTH_STATUS_OK) {
|
|
|
1215 decoder->private_->is_seeking = false;
|
|
|
1216 return false;
|
|
|
1217 }
|
|
|
1218
|
|
|
1219 /* if we haven't finished processing the metadata yet, do that so we have the STREAMINFO, SEEK_TABLE, and first_frame_offset */
|
|
|
1220 if(
|
|
|
1221 decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_METADATA ||
|
|
|
1222 decoder->protected_->state == FLAC__STREAM_DECODER_READ_METADATA
|
|
|
1223 ) {
|
|
|
1224 if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder)) {
|
|
|
1225 /* above call sets the state for us */
|
|
|
1226 decoder->private_->is_seeking = false;
|
|
|
1227 return false;
|
|
|
1228 }
|
|
|
1229 /* check this again in case we didn't know total_samples the first time */
|
|
|
1230 if(FLAC__stream_decoder_get_total_samples(decoder) > 0 && sample >= FLAC__stream_decoder_get_total_samples(decoder)) {
|
|
|
1231 decoder->private_->is_seeking = false;
|
|
|
1232 return false;
|
|
|
1233 }
|
|
|
1234 }
|
|
|
1235
|
|
|
1236 {
|
|
|
1237 const FLAC__bool ok =
|
|
|
1238 #if FLAC__HAS_OGG
|
|
|
1239 decoder->private_->is_ogg?
|
|
|
1240 seek_to_absolute_sample_ogg_(decoder, length, sample) :
|
|
|
1241 #endif
|
|
|
1242 seek_to_absolute_sample_(decoder, length, sample)
|
|
|
1243 ;
|
|
|
1244 decoder->private_->is_seeking = false;
|
|
|
1245 return ok;
|
|
|
1246 }
|
|
|
1247 }
|
|
|
1248
|
|
|
1249 /***********************************************************************
|
|
|
1250 *
|
|
|
1251 * Protected class methods
|
|
|
1252 *
|
|
|
1253 ***********************************************************************/
|
|
|
1254
|
|
|
1255 unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder)
|
|
|
1256 {
|
|
|
1257 FLAC__ASSERT(0 != decoder);
|
|
|
1258 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
|
|
|
1259 FLAC__ASSERT(!(FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) & 7));
|
|
|
1260 return FLAC__bitreader_get_input_bits_unconsumed(decoder->private_->input) / 8;
|
|
|
1261 }
|
|
|
1262
|
|
|
1263 /***********************************************************************
|
|
|
1264 *
|
|
|
1265 * Private class methods
|
|
|
1266 *
|
|
|
1267 ***********************************************************************/
|
|
|
1268
|
|
|
1269 void set_defaults_(FLAC__StreamDecoder *decoder)
|
|
|
1270 {
|
|
|
1271 #if FLAC__HAS_OGG
|
|
|
1272 decoder->private_->is_ogg = false;
|
|
|
1273 #endif
|
|
|
1274 decoder->private_->read_callback = 0;
|
|
|
1275 decoder->private_->seek_callback = 0;
|
|
|
1276 decoder->private_->tell_callback = 0;
|
|
|
1277 decoder->private_->length_callback = 0;
|
|
|
1278 decoder->private_->eof_callback = 0;
|
|
|
1279 decoder->private_->write_callback = 0;
|
|
|
1280 decoder->private_->metadata_callback = 0;
|
|
|
1281 decoder->private_->error_callback = 0;
|
|
|
1282 decoder->private_->client_data = 0;
|
|
|
1283
|
|
|
1284 memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter));
|
|
|
1285 decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true;
|
|
|
1286 decoder->private_->metadata_filter_ids_count = 0;
|
|
|
1287
|
|
|
1288 decoder->protected_->md5_checking = false;
|
|
|
1289
|
|
|
1290 #if FLAC__HAS_OGG
|
|
|
1291 FLAC__ogg_decoder_aspect_set_defaults(&decoder->protected_->ogg_decoder_aspect);
|
|
|
1292 #endif
|
|
|
1293 }
|
|
|
1294
|
|
|
1295 /*
|
|
|
1296 * This will forcibly set stdin to binary mode (for OSes that require it)
|
|
|
1297 */
|
|
721
|
1298 #ifndef USE_VFS
|
|
715
|
1299 FILE *get_binary_stdin_(void)
|
|
|
1300 {
|
|
|
1301 /* if something breaks here it is probably due to the presence or
|
|
|
1302 * absence of an underscore before the identifiers 'setmode',
|
|
|
1303 * 'fileno', and/or 'O_BINARY'; check your system header files.
|
|
|
1304 */
|
|
|
1305 #if defined _MSC_VER || defined __MINGW32__
|
|
|
1306 _setmode(_fileno(stdin), _O_BINARY);
|
|
|
1307 #elif defined __CYGWIN__ || defined __EMX__
|
|
|
1308 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
|
|
|
1309 setmode(_fileno(stdin), _O_BINARY);
|
|
|
1310 #endif
|
|
|
1311
|
|
|
1312 return stdin;
|
|
|
1313 }
|
|
721
|
1314 #endif
|
|
715
|
1315
|
|
|
1316 FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels)
|
|
|
1317 {
|
|
|
1318 unsigned i;
|
|
|
1319 FLAC__int32 *tmp;
|
|
|
1320
|
|
|
1321 if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels)
|
|
|
1322 return true;
|
|
|
1323
|
|
|
1324 /* simply using realloc() is not practical because the number of channels may change mid-stream */
|
|
|
1325
|
|
|
1326 for(i = 0; i < FLAC__MAX_CHANNELS; i++) {
|
|
|
1327 if(0 != decoder->private_->output[i]) {
|
|
|
1328 free(decoder->private_->output[i]-4);
|
|
|
1329 decoder->private_->output[i] = 0;
|
|
|
1330 }
|
|
|
1331 if(0 != decoder->private_->residual_unaligned[i]) {
|
|
|
1332 free(decoder->private_->residual_unaligned[i]);
|
|
|
1333 decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0;
|
|
|
1334 }
|
|
|
1335 }
|
|
|
1336
|
|
|
1337 for(i = 0; i < channels; i++) {
|
|
|
1338 /* WATCHOUT:
|
|
|
1339 * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the
|
|
|
1340 * output arrays have a buffer of up to 3 zeroes in front
|
|
|
1341 * (at negative indices) for alignment purposes; we use 4
|
|
|
1342 * to keep the data well-aligned.
|
|
|
1343 */
|
|
|
1344 tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4));
|
|
|
1345 if(tmp == 0) {
|
|
|
1346 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1347 return false;
|
|
|
1348 }
|
|
|
1349 memset(tmp, 0, sizeof(FLAC__int32)*4);
|
|
|
1350 decoder->private_->output[i] = tmp + 4;
|
|
|
1351
|
|
|
1352 /* WATCHOUT:
|
|
|
1353 * minimum of quadword alignment for PPC vector optimizations is REQUIRED:
|
|
|
1354 */
|
|
|
1355 if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) {
|
|
|
1356 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1357 return false;
|
|
|
1358 }
|
|
|
1359 }
|
|
|
1360
|
|
|
1361 decoder->private_->output_capacity = size;
|
|
|
1362 decoder->private_->output_channels = channels;
|
|
|
1363
|
|
|
1364 return true;
|
|
|
1365 }
|
|
|
1366
|
|
|
1367 FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id)
|
|
|
1368 {
|
|
|
1369 unsigned i;
|
|
|
1370
|
|
|
1371 FLAC__ASSERT(0 != decoder);
|
|
|
1372 FLAC__ASSERT(0 != decoder->private_);
|
|
|
1373
|
|
|
1374 for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++)
|
|
|
1375 if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)))
|
|
|
1376 return true;
|
|
|
1377
|
|
|
1378 return false;
|
|
|
1379 }
|
|
|
1380
|
|
|
1381 FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder)
|
|
|
1382 {
|
|
|
1383 FLAC__uint32 x;
|
|
|
1384 unsigned i, id;
|
|
|
1385 FLAC__bool first = true;
|
|
|
1386
|
|
|
1387 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
|
|
|
1388
|
|
|
1389 for(i = id = 0; i < 4; ) {
|
|
|
1390 if(decoder->private_->cached) {
|
|
|
1391 x = (FLAC__uint32)decoder->private_->lookahead;
|
|
|
1392 decoder->private_->cached = false;
|
|
|
1393 }
|
|
|
1394 else {
|
|
|
1395 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
|
|
|
1396 return false; /* read_callback_ sets the state for us */
|
|
|
1397 }
|
|
|
1398 if(x == FLAC__STREAM_SYNC_STRING[i]) {
|
|
|
1399 first = true;
|
|
|
1400 i++;
|
|
|
1401 id = 0;
|
|
|
1402 continue;
|
|
|
1403 }
|
|
|
1404 if(x == ID3V2_TAG_[id]) {
|
|
|
1405 id++;
|
|
|
1406 i = 0;
|
|
|
1407 if(id == 3) {
|
|
|
1408 if(!skip_id3v2_tag_(decoder))
|
|
|
1409 return false; /* skip_id3v2_tag_ sets the state for us */
|
|
|
1410 }
|
|
|
1411 continue;
|
|
|
1412 }
|
|
|
1413 id = 0;
|
|
|
1414 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
|
|
|
1415 decoder->private_->header_warmup[0] = (FLAC__byte)x;
|
|
|
1416 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
|
|
|
1417 return false; /* read_callback_ sets the state for us */
|
|
|
1418
|
|
|
1419 /* 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 */
|
|
|
1420 /* else we have to check if the second byte is the end of a sync code */
|
|
|
1421 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
|
|
|
1422 decoder->private_->lookahead = (FLAC__byte)x;
|
|
|
1423 decoder->private_->cached = true;
|
|
|
1424 }
|
|
|
1425 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
|
|
|
1426 decoder->private_->header_warmup[1] = (FLAC__byte)x;
|
|
|
1427 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
|
|
|
1428 return true;
|
|
|
1429 }
|
|
|
1430 }
|
|
|
1431 i = 0;
|
|
|
1432 if(first) {
|
|
|
1433 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
|
|
|
1434 first = false;
|
|
|
1435 }
|
|
|
1436 }
|
|
|
1437
|
|
|
1438 decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA;
|
|
|
1439 return true;
|
|
|
1440 }
|
|
|
1441
|
|
|
1442 FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder)
|
|
|
1443 {
|
|
|
1444 FLAC__bool is_last;
|
|
|
1445 FLAC__uint32 i, x, type, length;
|
|
|
1446
|
|
|
1447 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
|
|
|
1448
|
|
|
1449 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN))
|
|
|
1450 return false; /* read_callback_ sets the state for us */
|
|
|
1451 is_last = x? true : false;
|
|
|
1452
|
|
|
1453 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN))
|
|
|
1454 return false; /* read_callback_ sets the state for us */
|
|
|
1455
|
|
|
1456 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN))
|
|
|
1457 return false; /* read_callback_ sets the state for us */
|
|
|
1458
|
|
|
1459 if(type == FLAC__METADATA_TYPE_STREAMINFO) {
|
|
|
1460 if(!read_metadata_streaminfo_(decoder, is_last, length))
|
|
|
1461 return false;
|
|
|
1462
|
|
|
1463 decoder->private_->has_stream_info = true;
|
|
|
1464 if(0 == memcmp(decoder->private_->stream_info.data.stream_info.md5sum, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 16))
|
|
|
1465 decoder->private_->do_md5_checking = false;
|
|
|
1466 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] && decoder->private_->metadata_callback)
|
|
|
1467 decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data);
|
|
|
1468 }
|
|
|
1469 else if(type == FLAC__METADATA_TYPE_SEEKTABLE) {
|
|
|
1470 if(!read_metadata_seektable_(decoder, is_last, length))
|
|
|
1471 return false;
|
|
|
1472
|
|
|
1473 decoder->private_->has_seek_table = true;
|
|
|
1474 if(!decoder->private_->is_seeking && decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE] && decoder->private_->metadata_callback)
|
|
|
1475 decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data);
|
|
|
1476 }
|
|
|
1477 else {
|
|
|
1478 FLAC__bool skip_it = !decoder->private_->metadata_filter[type];
|
|
|
1479 unsigned real_length = length;
|
|
|
1480 FLAC__StreamMetadata block;
|
|
|
1481
|
|
|
1482 block.is_last = is_last;
|
|
|
1483 block.type = (FLAC__MetadataType)type;
|
|
|
1484 block.length = length;
|
|
|
1485
|
|
|
1486 if(type == FLAC__METADATA_TYPE_APPLICATION) {
|
|
|
1487 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))
|
|
|
1488 return false; /* read_callback_ sets the state for us */
|
|
|
1489
|
|
|
1490 real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8;
|
|
|
1491
|
|
|
1492 if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id))
|
|
|
1493 skip_it = !skip_it;
|
|
|
1494 }
|
|
|
1495
|
|
|
1496 if(skip_it) {
|
|
|
1497 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
|
|
|
1498 return false; /* read_callback_ sets the state for us */
|
|
|
1499 }
|
|
|
1500 else {
|
|
|
1501 switch(type) {
|
|
|
1502 case FLAC__METADATA_TYPE_PADDING:
|
|
|
1503 /* skip the padding bytes */
|
|
|
1504 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, real_length))
|
|
|
1505 return false; /* read_callback_ sets the state for us */
|
|
|
1506 break;
|
|
|
1507 case FLAC__METADATA_TYPE_APPLICATION:
|
|
|
1508 /* remember, we read the ID already */
|
|
|
1509 if(real_length > 0) {
|
|
|
1510 if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) {
|
|
|
1511 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1512 return false;
|
|
|
1513 }
|
|
|
1514 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length))
|
|
|
1515 return false; /* read_callback_ sets the state for us */
|
|
|
1516 }
|
|
|
1517 else
|
|
|
1518 block.data.application.data = 0;
|
|
|
1519 break;
|
|
|
1520 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
|
|
|
1521 if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment))
|
|
|
1522 return false;
|
|
|
1523 break;
|
|
|
1524 case FLAC__METADATA_TYPE_CUESHEET:
|
|
|
1525 if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet))
|
|
|
1526 return false;
|
|
|
1527 break;
|
|
|
1528 case FLAC__METADATA_TYPE_PICTURE:
|
|
|
1529 if(!read_metadata_picture_(decoder, &block.data.picture))
|
|
|
1530 return false;
|
|
|
1531 break;
|
|
|
1532 case FLAC__METADATA_TYPE_STREAMINFO:
|
|
|
1533 case FLAC__METADATA_TYPE_SEEKTABLE:
|
|
|
1534 FLAC__ASSERT(0);
|
|
|
1535 break;
|
|
|
1536 default:
|
|
|
1537 if(real_length > 0) {
|
|
|
1538 if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) {
|
|
|
1539 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1540 return false;
|
|
|
1541 }
|
|
|
1542 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length))
|
|
|
1543 return false; /* read_callback_ sets the state for us */
|
|
|
1544 }
|
|
|
1545 else
|
|
|
1546 block.data.unknown.data = 0;
|
|
|
1547 break;
|
|
|
1548 }
|
|
|
1549 if(!decoder->private_->is_seeking && decoder->private_->metadata_callback)
|
|
|
1550 decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data);
|
|
|
1551
|
|
|
1552 /* now we have to free any malloc'ed data in the block */
|
|
|
1553 switch(type) {
|
|
|
1554 case FLAC__METADATA_TYPE_PADDING:
|
|
|
1555 break;
|
|
|
1556 case FLAC__METADATA_TYPE_APPLICATION:
|
|
|
1557 if(0 != block.data.application.data)
|
|
|
1558 free(block.data.application.data);
|
|
|
1559 break;
|
|
|
1560 case FLAC__METADATA_TYPE_VORBIS_COMMENT:
|
|
|
1561 if(0 != block.data.vorbis_comment.vendor_string.entry)
|
|
|
1562 free(block.data.vorbis_comment.vendor_string.entry);
|
|
|
1563 if(block.data.vorbis_comment.num_comments > 0)
|
|
|
1564 for(i = 0; i < block.data.vorbis_comment.num_comments; i++)
|
|
|
1565 if(0 != block.data.vorbis_comment.comments[i].entry)
|
|
|
1566 free(block.data.vorbis_comment.comments[i].entry);
|
|
|
1567 if(0 != block.data.vorbis_comment.comments)
|
|
|
1568 free(block.data.vorbis_comment.comments);
|
|
|
1569 break;
|
|
|
1570 case FLAC__METADATA_TYPE_CUESHEET:
|
|
|
1571 if(block.data.cue_sheet.num_tracks > 0)
|
|
|
1572 for(i = 0; i < block.data.cue_sheet.num_tracks; i++)
|
|
|
1573 if(0 != block.data.cue_sheet.tracks[i].indices)
|
|
|
1574 free(block.data.cue_sheet.tracks[i].indices);
|
|
|
1575 if(0 != block.data.cue_sheet.tracks)
|
|
|
1576 free(block.data.cue_sheet.tracks);
|
|
|
1577 break;
|
|
|
1578 case FLAC__METADATA_TYPE_PICTURE:
|
|
|
1579 if(0 != block.data.picture.mime_type)
|
|
|
1580 free(block.data.picture.mime_type);
|
|
|
1581 if(0 != block.data.picture.description)
|
|
|
1582 free(block.data.picture.description);
|
|
|
1583 if(0 != block.data.picture.data)
|
|
|
1584 free(block.data.picture.data);
|
|
|
1585 break;
|
|
|
1586 case FLAC__METADATA_TYPE_STREAMINFO:
|
|
|
1587 case FLAC__METADATA_TYPE_SEEKTABLE:
|
|
|
1588 FLAC__ASSERT(0);
|
|
|
1589 default:
|
|
|
1590 if(0 != block.data.unknown.data)
|
|
|
1591 free(block.data.unknown.data);
|
|
|
1592 break;
|
|
|
1593 }
|
|
|
1594 }
|
|
|
1595 }
|
|
|
1596
|
|
|
1597 if(is_last) {
|
|
|
1598 /* if this fails, it's OK, it's just a hint for the seek routine */
|
|
|
1599 if(!FLAC__stream_decoder_get_decode_position(decoder, &decoder->private_->first_frame_offset))
|
|
|
1600 decoder->private_->first_frame_offset = 0;
|
|
|
1601 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
1602 }
|
|
|
1603
|
|
|
1604 return true;
|
|
|
1605 }
|
|
|
1606
|
|
|
1607 FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
|
|
|
1608 {
|
|
|
1609 FLAC__uint32 x;
|
|
|
1610 unsigned bits, used_bits = 0;
|
|
|
1611
|
|
|
1612 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
|
|
|
1613
|
|
|
1614 decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO;
|
|
|
1615 decoder->private_->stream_info.is_last = is_last;
|
|
|
1616 decoder->private_->stream_info.length = length;
|
|
|
1617
|
|
|
1618 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN;
|
|
|
1619 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, bits))
|
|
|
1620 return false; /* read_callback_ sets the state for us */
|
|
|
1621 decoder->private_->stream_info.data.stream_info.min_blocksize = x;
|
|
|
1622 used_bits += bits;
|
|
|
1623
|
|
|
1624 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN;
|
|
|
1625 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN))
|
|
|
1626 return false; /* read_callback_ sets the state for us */
|
|
|
1627 decoder->private_->stream_info.data.stream_info.max_blocksize = x;
|
|
|
1628 used_bits += bits;
|
|
|
1629
|
|
|
1630 bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN;
|
|
|
1631 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN))
|
|
|
1632 return false; /* read_callback_ sets the state for us */
|
|
|
1633 decoder->private_->stream_info.data.stream_info.min_framesize = x;
|
|
|
1634 used_bits += bits;
|
|
|
1635
|
|
|
1636 bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN;
|
|
|
1637 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN))
|
|
|
1638 return false; /* read_callback_ sets the state for us */
|
|
|
1639 decoder->private_->stream_info.data.stream_info.max_framesize = x;
|
|
|
1640 used_bits += bits;
|
|
|
1641
|
|
|
1642 bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN;
|
|
|
1643 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN))
|
|
|
1644 return false; /* read_callback_ sets the state for us */
|
|
|
1645 decoder->private_->stream_info.data.stream_info.sample_rate = x;
|
|
|
1646 used_bits += bits;
|
|
|
1647
|
|
|
1648 bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN;
|
|
|
1649 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN))
|
|
|
1650 return false; /* read_callback_ sets the state for us */
|
|
|
1651 decoder->private_->stream_info.data.stream_info.channels = x+1;
|
|
|
1652 used_bits += bits;
|
|
|
1653
|
|
|
1654 bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN;
|
|
|
1655 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN))
|
|
|
1656 return false; /* read_callback_ sets the state for us */
|
|
|
1657 decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1;
|
|
|
1658 used_bits += bits;
|
|
|
1659
|
|
|
1660 bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN;
|
|
|
1661 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN))
|
|
|
1662 return false; /* read_callback_ sets the state for us */
|
|
|
1663 used_bits += bits;
|
|
|
1664
|
|
|
1665 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16))
|
|
|
1666 return false; /* read_callback_ sets the state for us */
|
|
|
1667 used_bits += 16*8;
|
|
|
1668
|
|
|
1669 /* skip the rest of the block */
|
|
|
1670 FLAC__ASSERT(used_bits % 8 == 0);
|
|
|
1671 length -= (used_bits / 8);
|
|
|
1672 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
|
|
|
1673 return false; /* read_callback_ sets the state for us */
|
|
|
1674
|
|
|
1675 return true;
|
|
|
1676 }
|
|
|
1677
|
|
|
1678 FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length)
|
|
|
1679 {
|
|
|
1680 FLAC__uint32 i, x;
|
|
|
1681 FLAC__uint64 xx;
|
|
|
1682
|
|
|
1683 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
|
|
|
1684
|
|
|
1685 decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE;
|
|
|
1686 decoder->private_->seek_table.is_last = is_last;
|
|
|
1687 decoder->private_->seek_table.length = length;
|
|
|
1688
|
|
|
1689 decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
|
|
|
1690
|
|
|
1691 /* use realloc since we may pass through here several times (e.g. after seeking) */
|
|
|
1692 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)))) {
|
|
|
1693 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1694 return false;
|
|
|
1695 }
|
|
|
1696 for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) {
|
|
|
1697 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN))
|
|
|
1698 return false; /* read_callback_ sets the state for us */
|
|
|
1699 decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx;
|
|
|
1700
|
|
|
1701 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN))
|
|
|
1702 return false; /* read_callback_ sets the state for us */
|
|
|
1703 decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx;
|
|
|
1704
|
|
|
1705 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN))
|
|
|
1706 return false; /* read_callback_ sets the state for us */
|
|
|
1707 decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x;
|
|
|
1708 }
|
|
|
1709 length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH);
|
|
|
1710 /* if there is a partial point left, skip over it */
|
|
|
1711 if(length > 0) {
|
|
|
1712 /*@@@ do a send_error_to_client_() here? there's an argument for either way */
|
|
|
1713 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, length))
|
|
|
1714 return false; /* read_callback_ sets the state for us */
|
|
|
1715 }
|
|
|
1716
|
|
|
1717 return true;
|
|
|
1718 }
|
|
|
1719
|
|
|
1720 FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj)
|
|
|
1721 {
|
|
|
1722 FLAC__uint32 i;
|
|
|
1723
|
|
|
1724 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
|
|
|
1725
|
|
|
1726 /* read vendor string */
|
|
|
1727 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
|
|
|
1728 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length))
|
|
|
1729 return false; /* read_callback_ sets the state for us */
|
|
|
1730 if(obj->vendor_string.length > 0) {
|
|
|
1731 if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length+1))) {
|
|
|
1732 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1733 return false;
|
|
|
1734 }
|
|
|
1735 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length))
|
|
|
1736 return false; /* read_callback_ sets the state for us */
|
|
|
1737 obj->vendor_string.entry[obj->vendor_string.length] = '\0';
|
|
|
1738 }
|
|
|
1739 else
|
|
|
1740 obj->vendor_string.entry = 0;
|
|
|
1741
|
|
|
1742 /* read num comments */
|
|
|
1743 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32);
|
|
|
1744 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->num_comments))
|
|
|
1745 return false; /* read_callback_ sets the state for us */
|
|
|
1746
|
|
|
1747 /* read comments */
|
|
|
1748 if(obj->num_comments > 0) {
|
|
|
1749 if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
|
|
|
1750 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1751 return false;
|
|
|
1752 }
|
|
|
1753 for(i = 0; i < obj->num_comments; i++) {
|
|
|
1754 FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32);
|
|
|
1755 if(!FLAC__bitreader_read_uint32_little_endian(decoder->private_->input, &obj->comments[i].length))
|
|
|
1756 return false; /* read_callback_ sets the state for us */
|
|
|
1757 if(obj->comments[i].length > 0) {
|
|
|
1758 if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length+1))) {
|
|
|
1759 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1760 return false;
|
|
|
1761 }
|
|
|
1762 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length))
|
|
|
1763 return false; /* read_callback_ sets the state for us */
|
|
|
1764 obj->comments[i].entry[obj->comments[i].length] = '\0';
|
|
|
1765 }
|
|
|
1766 else
|
|
|
1767 obj->comments[i].entry = 0;
|
|
|
1768 }
|
|
|
1769 }
|
|
|
1770 else {
|
|
|
1771 obj->comments = 0;
|
|
|
1772 }
|
|
|
1773
|
|
|
1774 return true;
|
|
|
1775 }
|
|
|
1776
|
|
|
1777 FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj)
|
|
|
1778 {
|
|
|
1779 FLAC__uint32 i, j, x;
|
|
|
1780
|
|
|
1781 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
|
|
|
1782
|
|
|
1783 memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet));
|
|
|
1784
|
|
|
1785 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
|
|
|
1786 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8))
|
|
|
1787 return false; /* read_callback_ sets the state for us */
|
|
|
1788
|
|
|
1789 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN))
|
|
|
1790 return false; /* read_callback_ sets the state for us */
|
|
|
1791
|
|
|
1792 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN))
|
|
|
1793 return false; /* read_callback_ sets the state for us */
|
|
|
1794 obj->is_cd = x? true : false;
|
|
|
1795
|
|
|
1796 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN))
|
|
|
1797 return false; /* read_callback_ sets the state for us */
|
|
|
1798
|
|
|
1799 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN))
|
|
|
1800 return false; /* read_callback_ sets the state for us */
|
|
|
1801 obj->num_tracks = x;
|
|
|
1802
|
|
|
1803 if(obj->num_tracks > 0) {
|
|
|
1804 if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) {
|
|
|
1805 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1806 return false;
|
|
|
1807 }
|
|
|
1808 for(i = 0; i < obj->num_tracks; i++) {
|
|
|
1809 FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i];
|
|
|
1810 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN))
|
|
|
1811 return false; /* read_callback_ sets the state for us */
|
|
|
1812
|
|
|
1813 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN))
|
|
|
1814 return false; /* read_callback_ sets the state for us */
|
|
|
1815 track->number = (FLAC__byte)x;
|
|
|
1816
|
|
|
1817 FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
|
|
|
1818 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8))
|
|
|
1819 return false; /* read_callback_ sets the state for us */
|
|
|
1820
|
|
|
1821 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN))
|
|
|
1822 return false; /* read_callback_ sets the state for us */
|
|
|
1823 track->type = x;
|
|
|
1824
|
|
|
1825 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN))
|
|
|
1826 return false; /* read_callback_ sets the state for us */
|
|
|
1827 track->pre_emphasis = x;
|
|
|
1828
|
|
|
1829 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN))
|
|
|
1830 return false; /* read_callback_ sets the state for us */
|
|
|
1831
|
|
|
1832 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN))
|
|
|
1833 return false; /* read_callback_ sets the state for us */
|
|
|
1834 track->num_indices = (FLAC__byte)x;
|
|
|
1835
|
|
|
1836 if(track->num_indices > 0) {
|
|
|
1837 if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) {
|
|
|
1838 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1839 return false;
|
|
|
1840 }
|
|
|
1841 for(j = 0; j < track->num_indices; j++) {
|
|
|
1842 FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j];
|
|
|
1843 if(!FLAC__bitreader_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN))
|
|
|
1844 return false; /* read_callback_ sets the state for us */
|
|
|
1845
|
|
|
1846 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN))
|
|
|
1847 return false; /* read_callback_ sets the state for us */
|
|
|
1848 index->number = (FLAC__byte)x;
|
|
|
1849
|
|
|
1850 if(!FLAC__bitreader_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN))
|
|
|
1851 return false; /* read_callback_ sets the state for us */
|
|
|
1852 }
|
|
|
1853 }
|
|
|
1854 }
|
|
|
1855 }
|
|
|
1856
|
|
|
1857 return true;
|
|
|
1858 }
|
|
|
1859
|
|
|
1860 FLAC__bool read_metadata_picture_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_Picture *obj)
|
|
|
1861 {
|
|
|
1862 FLAC__uint32 len;
|
|
|
1863
|
|
|
1864 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
|
|
|
1865
|
|
|
1866 /* read type */
|
|
|
1867 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->type, FLAC__STREAM_METADATA_PICTURE_TYPE_LEN))
|
|
|
1868 return false; /* read_callback_ sets the state for us */
|
|
|
1869
|
|
|
1870 /* read MIME type */
|
|
|
1871 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN))
|
|
|
1872 return false; /* read_callback_ sets the state for us */
|
|
|
1873 if(0 == (obj->mime_type = (char*)malloc(len+1))) {
|
|
|
1874 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1875 return false;
|
|
|
1876 }
|
|
|
1877 if(len > 0) {
|
|
|
1878 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->mime_type, len))
|
|
|
1879 return false; /* read_callback_ sets the state for us */
|
|
|
1880 }
|
|
|
1881 obj->mime_type[len] = '\0';
|
|
|
1882
|
|
|
1883 /* read description */
|
|
|
1884 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN))
|
|
|
1885 return false; /* read_callback_ sets the state for us */
|
|
|
1886 if(0 == (obj->description = (FLAC__byte*)malloc(len+1))) {
|
|
|
1887 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1888 return false;
|
|
|
1889 }
|
|
|
1890 if(len > 0) {
|
|
|
1891 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->description, len))
|
|
|
1892 return false; /* read_callback_ sets the state for us */
|
|
|
1893 }
|
|
|
1894 obj->description[len] = '\0';
|
|
|
1895
|
|
|
1896 /* read width */
|
|
|
1897 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->width, FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN))
|
|
|
1898 return false; /* read_callback_ sets the state for us */
|
|
|
1899
|
|
|
1900 /* read height */
|
|
|
1901 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->height, FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN))
|
|
|
1902 return false; /* read_callback_ sets the state for us */
|
|
|
1903
|
|
|
1904 /* read depth */
|
|
|
1905 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->depth, FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN))
|
|
|
1906 return false; /* read_callback_ sets the state for us */
|
|
|
1907
|
|
|
1908 /* read colors */
|
|
|
1909 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &obj->colors, FLAC__STREAM_METADATA_PICTURE_COLORS_LEN))
|
|
|
1910 return false; /* read_callback_ sets the state for us */
|
|
|
1911
|
|
|
1912 /* read data */
|
|
|
1913 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &(obj->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN))
|
|
|
1914 return false; /* read_callback_ sets the state for us */
|
|
|
1915 if(0 == (obj->data = (FLAC__byte*)malloc(obj->data_length))) {
|
|
|
1916 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
1917 return false;
|
|
|
1918 }
|
|
|
1919 if(obj->data_length > 0) {
|
|
|
1920 if(!FLAC__bitreader_read_byte_block_aligned_no_crc(decoder->private_->input, obj->data, obj->data_length))
|
|
|
1921 return false; /* read_callback_ sets the state for us */
|
|
|
1922 }
|
|
|
1923
|
|
|
1924 return true;
|
|
|
1925 }
|
|
|
1926
|
|
|
1927 FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder)
|
|
|
1928 {
|
|
|
1929 FLAC__uint32 x;
|
|
|
1930 unsigned i, skip;
|
|
|
1931
|
|
|
1932 /* skip the version and flags bytes */
|
|
|
1933 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 24))
|
|
|
1934 return false; /* read_callback_ sets the state for us */
|
|
|
1935 /* get the size (in bytes) to skip */
|
|
|
1936 skip = 0;
|
|
|
1937 for(i = 0; i < 4; i++) {
|
|
|
1938 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
|
|
|
1939 return false; /* read_callback_ sets the state for us */
|
|
|
1940 skip <<= 7;
|
|
|
1941 skip |= (x & 0x7f);
|
|
|
1942 }
|
|
|
1943 /* skip the rest of the tag */
|
|
|
1944 if(!FLAC__bitreader_skip_byte_block_aligned_no_crc(decoder->private_->input, skip))
|
|
|
1945 return false; /* read_callback_ sets the state for us */
|
|
|
1946 return true;
|
|
|
1947 }
|
|
|
1948
|
|
|
1949 FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder)
|
|
|
1950 {
|
|
|
1951 FLAC__uint32 x;
|
|
|
1952 FLAC__bool first = true;
|
|
|
1953
|
|
|
1954 /* If we know the total number of samples in the stream, stop if we've read that many. */
|
|
|
1955 /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */
|
|
|
1956 if(FLAC__stream_decoder_get_total_samples(decoder) > 0) {
|
|
|
1957 if(decoder->private_->samples_decoded >= FLAC__stream_decoder_get_total_samples(decoder)) {
|
|
|
1958 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
|
|
|
1959 return true;
|
|
|
1960 }
|
|
|
1961 }
|
|
|
1962
|
|
|
1963 /* make sure we're byte aligned */
|
|
|
1964 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
|
|
|
1965 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
|
|
|
1966 return false; /* read_callback_ sets the state for us */
|
|
|
1967 }
|
|
|
1968
|
|
|
1969 while(1) {
|
|
|
1970 if(decoder->private_->cached) {
|
|
|
1971 x = (FLAC__uint32)decoder->private_->lookahead;
|
|
|
1972 decoder->private_->cached = false;
|
|
|
1973 }
|
|
|
1974 else {
|
|
|
1975 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
|
|
|
1976 return false; /* read_callback_ sets the state for us */
|
|
|
1977 }
|
|
|
1978 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
|
|
|
1979 decoder->private_->header_warmup[0] = (FLAC__byte)x;
|
|
|
1980 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
|
|
|
1981 return false; /* read_callback_ sets the state for us */
|
|
|
1982
|
|
|
1983 /* 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 */
|
|
|
1984 /* else we have to check if the second byte is the end of a sync code */
|
|
|
1985 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
|
|
|
1986 decoder->private_->lookahead = (FLAC__byte)x;
|
|
|
1987 decoder->private_->cached = true;
|
|
|
1988 }
|
|
|
1989 else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */
|
|
|
1990 decoder->private_->header_warmup[1] = (FLAC__byte)x;
|
|
|
1991 decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME;
|
|
|
1992 return true;
|
|
|
1993 }
|
|
|
1994 }
|
|
|
1995 if(first) {
|
|
|
1996 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
|
|
|
1997 first = false;
|
|
|
1998 }
|
|
|
1999 }
|
|
|
2000
|
|
|
2001 return true;
|
|
|
2002 }
|
|
|
2003
|
|
|
2004 FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode)
|
|
|
2005 {
|
|
|
2006 unsigned channel;
|
|
|
2007 unsigned i;
|
|
|
2008 FLAC__int32 mid, side, left, right;
|
|
|
2009 unsigned frame_crc; /* the one we calculate from the input stream */
|
|
|
2010 FLAC__uint32 x;
|
|
|
2011
|
|
|
2012 *got_a_frame = false;
|
|
|
2013
|
|
|
2014 /* init the CRC */
|
|
|
2015 frame_crc = 0;
|
|
|
2016 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc);
|
|
|
2017 frame_crc = FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc);
|
|
|
2018 FLAC__bitreader_reset_read_crc16(decoder->private_->input, (FLAC__uint16)frame_crc);
|
|
|
2019
|
|
|
2020 if(!read_frame_header_(decoder))
|
|
|
2021 return false;
|
|
|
2022 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means we didn't sync on a valid header */
|
|
|
2023 return true;
|
|
|
2024 if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels))
|
|
|
2025 return false;
|
|
|
2026 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
|
|
|
2027 /*
|
|
|
2028 * first figure the correct bits-per-sample of the subframe
|
|
|
2029 */
|
|
|
2030 unsigned bps = decoder->private_->frame.header.bits_per_sample;
|
|
|
2031 switch(decoder->private_->frame.header.channel_assignment) {
|
|
|
2032 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
|
|
|
2033 /* no adjustment needed */
|
|
|
2034 break;
|
|
|
2035 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
|
|
|
2036 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
|
|
|
2037 if(channel == 1)
|
|
|
2038 bps++;
|
|
|
2039 break;
|
|
|
2040 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
|
|
|
2041 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
|
|
|
2042 if(channel == 0)
|
|
|
2043 bps++;
|
|
|
2044 break;
|
|
|
2045 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
|
|
|
2046 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
|
|
|
2047 if(channel == 1)
|
|
|
2048 bps++;
|
|
|
2049 break;
|
|
|
2050 default:
|
|
|
2051 FLAC__ASSERT(0);
|
|
|
2052 }
|
|
|
2053 /*
|
|
|
2054 * now read it
|
|
|
2055 */
|
|
|
2056 if(!read_subframe_(decoder, channel, bps, do_full_decode))
|
|
|
2057 return false;
|
|
|
2058 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
|
|
|
2059 return true;
|
|
|
2060 }
|
|
|
2061 if(!read_zero_padding_(decoder))
|
|
|
2062 return false;
|
|
|
2063
|
|
|
2064 /*
|
|
|
2065 * Read the frame CRC-16 from the footer and check
|
|
|
2066 */
|
|
|
2067 frame_crc = FLAC__bitreader_get_read_crc16(decoder->private_->input);
|
|
|
2068 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN))
|
|
|
2069 return false; /* read_callback_ sets the state for us */
|
|
|
2070 if(frame_crc == x) {
|
|
|
2071 if(do_full_decode) {
|
|
|
2072 /* Undo any special channel coding */
|
|
|
2073 switch(decoder->private_->frame.header.channel_assignment) {
|
|
|
2074 case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT:
|
|
|
2075 /* do nothing */
|
|
|
2076 break;
|
|
|
2077 case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE:
|
|
|
2078 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
|
|
|
2079 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
|
|
|
2080 decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i];
|
|
|
2081 break;
|
|
|
2082 case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE:
|
|
|
2083 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
|
|
|
2084 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
|
|
|
2085 decoder->private_->output[0][i] += decoder->private_->output[1][i];
|
|
|
2086 break;
|
|
|
2087 case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE:
|
|
|
2088 FLAC__ASSERT(decoder->private_->frame.header.channels == 2);
|
|
|
2089 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
|
|
|
2090 mid = decoder->private_->output[0][i];
|
|
|
2091 side = decoder->private_->output[1][i];
|
|
|
2092 mid <<= 1;
|
|
|
2093 if(side & 1) /* i.e. if 'side' is odd... */
|
|
|
2094 mid++;
|
|
|
2095 left = mid + side;
|
|
|
2096 right = mid - side;
|
|
|
2097 decoder->private_->output[0][i] = left >> 1;
|
|
|
2098 decoder->private_->output[1][i] = right >> 1;
|
|
|
2099 }
|
|
|
2100 break;
|
|
|
2101 default:
|
|
|
2102 FLAC__ASSERT(0);
|
|
|
2103 break;
|
|
|
2104 }
|
|
|
2105 }
|
|
|
2106 }
|
|
|
2107 else {
|
|
|
2108 /* Bad frame, emit error and zero the output signal */
|
|
|
2109 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH);
|
|
|
2110 if(do_full_decode) {
|
|
|
2111 for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) {
|
|
|
2112 memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
|
|
|
2113 }
|
|
|
2114 }
|
|
|
2115 }
|
|
|
2116
|
|
|
2117 *got_a_frame = true;
|
|
|
2118
|
|
|
2119 /* put the latest values into the public section of the decoder instance */
|
|
|
2120 decoder->protected_->channels = decoder->private_->frame.header.channels;
|
|
|
2121 decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment;
|
|
|
2122 decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample;
|
|
|
2123 decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate;
|
|
|
2124 decoder->protected_->blocksize = decoder->private_->frame.header.blocksize;
|
|
|
2125
|
|
|
2126 FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
|
|
|
2127 decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize;
|
|
|
2128
|
|
|
2129 /* write it */
|
|
|
2130 if(do_full_decode) {
|
|
|
2131 if(write_audio_frame_to_client_(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE)
|
|
|
2132 return false;
|
|
|
2133 }
|
|
|
2134
|
|
|
2135 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2136 return true;
|
|
|
2137 }
|
|
|
2138
|
|
|
2139 FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder)
|
|
|
2140 {
|
|
|
2141 FLAC__uint32 x;
|
|
|
2142 FLAC__uint64 xx;
|
|
|
2143 unsigned i, blocksize_hint = 0, sample_rate_hint = 0;
|
|
|
2144 FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */
|
|
|
2145 unsigned raw_header_len;
|
|
|
2146 FLAC__bool is_unparseable = false;
|
|
|
2147 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);
|
|
|
2148 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);
|
|
|
2149
|
|
|
2150 FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
|
|
|
2151
|
|
|
2152 /* init the raw header with the saved bits from synchronization */
|
|
|
2153 raw_header[0] = decoder->private_->header_warmup[0];
|
|
|
2154 raw_header[1] = decoder->private_->header_warmup[1];
|
|
|
2155 raw_header_len = 2;
|
|
|
2156
|
|
|
2157 /*
|
|
|
2158 * check to make sure that the reserved bits are 0
|
|
|
2159 */
|
|
|
2160 if(raw_header[1] & 0x03) { /* MAGIC NUMBER */
|
|
|
2161 is_unparseable = true;
|
|
|
2162 }
|
|
|
2163
|
|
|
2164 /*
|
|
|
2165 * Note that along the way as we read the header, we look for a sync
|
|
|
2166 * code inside. If we find one it would indicate that our original
|
|
|
2167 * sync was bad since there cannot be a sync code in a valid header.
|
|
|
2168 */
|
|
|
2169
|
|
|
2170 /*
|
|
|
2171 * read in the raw header as bytes so we can CRC it, and parse it on the way
|
|
|
2172 */
|
|
|
2173 for(i = 0; i < 2; i++) {
|
|
|
2174 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
|
|
|
2175 return false; /* read_callback_ sets the state for us */
|
|
|
2176 if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */
|
|
|
2177 /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */
|
|
|
2178 decoder->private_->lookahead = (FLAC__byte)x;
|
|
|
2179 decoder->private_->cached = true;
|
|
|
2180 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
|
|
|
2181 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2182 return true;
|
|
|
2183 }
|
|
|
2184 raw_header[raw_header_len++] = (FLAC__byte)x;
|
|
|
2185 }
|
|
|
2186
|
|
|
2187 switch(x = raw_header[2] >> 4) {
|
|
|
2188 case 0:
|
|
|
2189 if(is_known_fixed_blocksize_stream)
|
|
|
2190 decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
|
|
|
2191 else
|
|
|
2192 is_unparseable = true;
|
|
|
2193 break;
|
|
|
2194 case 1:
|
|
|
2195 decoder->private_->frame.header.blocksize = 192;
|
|
|
2196 break;
|
|
|
2197 case 2:
|
|
|
2198 case 3:
|
|
|
2199 case 4:
|
|
|
2200 case 5:
|
|
|
2201 decoder->private_->frame.header.blocksize = 576 << (x-2);
|
|
|
2202 break;
|
|
|
2203 case 6:
|
|
|
2204 case 7:
|
|
|
2205 blocksize_hint = x;
|
|
|
2206 break;
|
|
|
2207 case 8:
|
|
|
2208 case 9:
|
|
|
2209 case 10:
|
|
|
2210 case 11:
|
|
|
2211 case 12:
|
|
|
2212 case 13:
|
|
|
2213 case 14:
|
|
|
2214 case 15:
|
|
|
2215 decoder->private_->frame.header.blocksize = 256 << (x-8);
|
|
|
2216 break;
|
|
|
2217 default:
|
|
|
2218 FLAC__ASSERT(0);
|
|
|
2219 break;
|
|
|
2220 }
|
|
|
2221
|
|
|
2222 switch(x = raw_header[2] & 0x0f) {
|
|
|
2223 case 0:
|
|
|
2224 if(decoder->private_->has_stream_info)
|
|
|
2225 decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate;
|
|
|
2226 else
|
|
|
2227 is_unparseable = true;
|
|
|
2228 break;
|
|
|
2229 case 1:
|
|
|
2230 case 2:
|
|
|
2231 case 3:
|
|
|
2232 is_unparseable = true;
|
|
|
2233 break;
|
|
|
2234 case 4:
|
|
|
2235 decoder->private_->frame.header.sample_rate = 8000;
|
|
|
2236 break;
|
|
|
2237 case 5:
|
|
|
2238 decoder->private_->frame.header.sample_rate = 16000;
|
|
|
2239 break;
|
|
|
2240 case 6:
|
|
|
2241 decoder->private_->frame.header.sample_rate = 22050;
|
|
|
2242 break;
|
|
|
2243 case 7:
|
|
|
2244 decoder->private_->frame.header.sample_rate = 24000;
|
|
|
2245 break;
|
|
|
2246 case 8:
|
|
|
2247 decoder->private_->frame.header.sample_rate = 32000;
|
|
|
2248 break;
|
|
|
2249 case 9:
|
|
|
2250 decoder->private_->frame.header.sample_rate = 44100;
|
|
|
2251 break;
|
|
|
2252 case 10:
|
|
|
2253 decoder->private_->frame.header.sample_rate = 48000;
|
|
|
2254 break;
|
|
|
2255 case 11:
|
|
|
2256 decoder->private_->frame.header.sample_rate = 96000;
|
|
|
2257 break;
|
|
|
2258 case 12:
|
|
|
2259 case 13:
|
|
|
2260 case 14:
|
|
|
2261 sample_rate_hint = x;
|
|
|
2262 break;
|
|
|
2263 case 15:
|
|
|
2264 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
|
|
|
2265 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2266 return true;
|
|
|
2267 default:
|
|
|
2268 FLAC__ASSERT(0);
|
|
|
2269 }
|
|
|
2270
|
|
|
2271 x = (unsigned)(raw_header[3] >> 4);
|
|
|
2272 if(x & 8) {
|
|
|
2273 decoder->private_->frame.header.channels = 2;
|
|
|
2274 switch(x & 7) {
|
|
|
2275 case 0:
|
|
|
2276 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
|
|
|
2277 break;
|
|
|
2278 case 1:
|
|
|
2279 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
|
|
|
2280 break;
|
|
|
2281 case 2:
|
|
|
2282 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
|
|
|
2283 break;
|
|
|
2284 default:
|
|
|
2285 is_unparseable = true;
|
|
|
2286 break;
|
|
|
2287 }
|
|
|
2288 }
|
|
|
2289 else {
|
|
|
2290 decoder->private_->frame.header.channels = (unsigned)x + 1;
|
|
|
2291 decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT;
|
|
|
2292 }
|
|
|
2293
|
|
|
2294 switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) {
|
|
|
2295 case 0:
|
|
|
2296 if(decoder->private_->has_stream_info)
|
|
|
2297 decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample;
|
|
|
2298 else
|
|
|
2299 is_unparseable = true;
|
|
|
2300 break;
|
|
|
2301 case 1:
|
|
|
2302 decoder->private_->frame.header.bits_per_sample = 8;
|
|
|
2303 break;
|
|
|
2304 case 2:
|
|
|
2305 decoder->private_->frame.header.bits_per_sample = 12;
|
|
|
2306 break;
|
|
|
2307 case 4:
|
|
|
2308 decoder->private_->frame.header.bits_per_sample = 16;
|
|
|
2309 break;
|
|
|
2310 case 5:
|
|
|
2311 decoder->private_->frame.header.bits_per_sample = 20;
|
|
|
2312 break;
|
|
|
2313 case 6:
|
|
|
2314 decoder->private_->frame.header.bits_per_sample = 24;
|
|
|
2315 break;
|
|
|
2316 case 3:
|
|
|
2317 case 7:
|
|
|
2318 is_unparseable = true;
|
|
|
2319 break;
|
|
|
2320 default:
|
|
|
2321 FLAC__ASSERT(0);
|
|
|
2322 break;
|
|
|
2323 }
|
|
|
2324
|
|
|
2325 if(raw_header[3] & 0x01) { /* this should be a zero padding bit */
|
|
|
2326 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
|
|
|
2327 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2328 return true;
|
|
|
2329 }
|
|
|
2330
|
|
|
2331 /*
|
|
|
2332 * Now we get to the regrettable consequences of not knowing for sure
|
|
|
2333 * whether we got a frame number or a sample number. There are no
|
|
|
2334 * encoders that do variable-blocksize encoding so unless we know from
|
|
|
2335 * the STREAMINFO that it is variable-blocksize we will assume it is
|
|
|
2336 * fixed-blocksize. The trouble comes when we have no STREAMINFO; again
|
|
|
2337 * we will guess that is fixed-blocksize. Where this can go wrong: 1) a
|
|
|
2338 * variable-blocksize stream with no STREAMINFO; 2) a fixed-blocksize
|
|
|
2339 * stream that was edited such that one or more frames before or
|
|
|
2340 * including this one do not have the same number of samples as the
|
|
|
2341 * STREAMINFO's min and max blocksize.
|
|
|
2342 */
|
|
|
2343 if(is_known_variable_blocksize_stream) {
|
|
|
2344 if(blocksize_hint) {
|
|
|
2345 if(!FLAC__bitreader_read_utf8_uint64(decoder->private_->input, &xx, raw_header, &raw_header_len))
|
|
|
2346 return false; /* read_callback_ sets the state for us */
|
|
|
2347 if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */
|
|
|
2348 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
|
|
|
2349 decoder->private_->cached = true;
|
|
|
2350 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
|
|
|
2351 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2352 return true;
|
|
|
2353 }
|
|
|
2354 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
|
|
|
2355 decoder->private_->frame.header.number.sample_number = xx;
|
|
|
2356 }
|
|
|
2357 else
|
|
|
2358 is_unparseable = true;
|
|
|
2359 }
|
|
|
2360 else {
|
|
|
2361 if(!FLAC__bitreader_read_utf8_uint32(decoder->private_->input, &x, raw_header, &raw_header_len))
|
|
|
2362 return false; /* read_callback_ sets the state for us */
|
|
|
2363 if(x == 0xffffffff) { /* i.e. non-UTF8 code... */
|
|
|
2364 decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */
|
|
|
2365 decoder->private_->cached = true;
|
|
|
2366 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
|
|
|
2367 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2368 return true;
|
|
|
2369 }
|
|
|
2370 decoder->private_->last_frame_number = x;
|
|
|
2371 decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER;
|
|
|
2372 if(decoder->private_->has_stream_info) {
|
|
|
2373 FLAC__ASSERT(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize);
|
|
|
2374 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x;
|
|
|
2375 decoder->private_->last_block_size = decoder->private_->frame.header.blocksize;
|
|
|
2376 }
|
|
|
2377 else if(blocksize_hint) {
|
|
|
2378 if(decoder->private_->last_block_size)
|
|
|
2379 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->last_block_size * (FLAC__uint64)x;
|
|
|
2380 else
|
|
|
2381 is_unparseable = true;
|
|
|
2382 }
|
|
|
2383 else {
|
|
|
2384 decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x;
|
|
|
2385 decoder->private_->last_block_size = decoder->private_->frame.header.blocksize;
|
|
|
2386 }
|
|
|
2387 }
|
|
|
2388
|
|
|
2389 if(blocksize_hint) {
|
|
|
2390 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
|
|
|
2391 return false; /* read_callback_ sets the state for us */
|
|
|
2392 raw_header[raw_header_len++] = (FLAC__byte)x;
|
|
|
2393 if(blocksize_hint == 7) {
|
|
|
2394 FLAC__uint32 _x;
|
|
|
2395 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
|
|
|
2396 return false; /* read_callback_ sets the state for us */
|
|
|
2397 raw_header[raw_header_len++] = (FLAC__byte)_x;
|
|
|
2398 x = (x << 8) | _x;
|
|
|
2399 }
|
|
|
2400 decoder->private_->frame.header.blocksize = x+1;
|
|
|
2401 }
|
|
|
2402
|
|
|
2403 if(sample_rate_hint) {
|
|
|
2404 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
|
|
|
2405 return false; /* read_callback_ sets the state for us */
|
|
|
2406 raw_header[raw_header_len++] = (FLAC__byte)x;
|
|
|
2407 if(sample_rate_hint != 12) {
|
|
|
2408 FLAC__uint32 _x;
|
|
|
2409 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &_x, 8))
|
|
|
2410 return false; /* read_callback_ sets the state for us */
|
|
|
2411 raw_header[raw_header_len++] = (FLAC__byte)_x;
|
|
|
2412 x = (x << 8) | _x;
|
|
|
2413 }
|
|
|
2414 if(sample_rate_hint == 12)
|
|
|
2415 decoder->private_->frame.header.sample_rate = x*1000;
|
|
|
2416 else if(sample_rate_hint == 13)
|
|
|
2417 decoder->private_->frame.header.sample_rate = x;
|
|
|
2418 else
|
|
|
2419 decoder->private_->frame.header.sample_rate = x*10;
|
|
|
2420 }
|
|
|
2421
|
|
|
2422 /* read the CRC-8 byte */
|
|
|
2423 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8))
|
|
|
2424 return false; /* read_callback_ sets the state for us */
|
|
|
2425 crc8 = (FLAC__byte)x;
|
|
|
2426
|
|
|
2427 if(FLAC__crc8(raw_header, raw_header_len) != crc8) {
|
|
|
2428 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER);
|
|
|
2429 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2430 return true;
|
|
|
2431 }
|
|
|
2432
|
|
|
2433 if(is_unparseable) {
|
|
|
2434 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
|
|
|
2435 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2436 return true;
|
|
|
2437 }
|
|
|
2438
|
|
|
2439 return true;
|
|
|
2440 }
|
|
|
2441
|
|
|
2442 FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
|
|
|
2443 {
|
|
|
2444 FLAC__uint32 x;
|
|
|
2445 FLAC__bool wasted_bits;
|
|
|
2446 unsigned i;
|
|
|
2447
|
|
|
2448 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &x, 8)) /* MAGIC NUMBER */
|
|
|
2449 return false; /* read_callback_ sets the state for us */
|
|
|
2450
|
|
|
2451 wasted_bits = (x & 1);
|
|
|
2452 x &= 0xfe;
|
|
|
2453
|
|
|
2454 if(wasted_bits) {
|
|
|
2455 unsigned u;
|
|
|
2456 if(!FLAC__bitreader_read_unary_unsigned(decoder->private_->input, &u))
|
|
|
2457 return false; /* read_callback_ sets the state for us */
|
|
|
2458 decoder->private_->frame.subframes[channel].wasted_bits = u+1;
|
|
|
2459 bps -= decoder->private_->frame.subframes[channel].wasted_bits;
|
|
|
2460 }
|
|
|
2461 else
|
|
|
2462 decoder->private_->frame.subframes[channel].wasted_bits = 0;
|
|
|
2463
|
|
|
2464 /*
|
|
|
2465 * Lots of magic numbers here
|
|
|
2466 */
|
|
|
2467 if(x & 0x80) {
|
|
|
2468 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
|
|
|
2469 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2470 return true;
|
|
|
2471 }
|
|
|
2472 else if(x == 0) {
|
|
|
2473 if(!read_subframe_constant_(decoder, channel, bps, do_full_decode))
|
|
|
2474 return false;
|
|
|
2475 }
|
|
|
2476 else if(x == 2) {
|
|
|
2477 if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode))
|
|
|
2478 return false;
|
|
|
2479 }
|
|
|
2480 else if(x < 16) {
|
|
|
2481 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
|
|
|
2482 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2483 return true;
|
|
|
2484 }
|
|
|
2485 else if(x <= 24) {
|
|
|
2486 if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode))
|
|
|
2487 return false;
|
|
|
2488 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
|
|
|
2489 return true;
|
|
|
2490 }
|
|
|
2491 else if(x < 64) {
|
|
|
2492 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
|
|
|
2493 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2494 return true;
|
|
|
2495 }
|
|
|
2496 else {
|
|
|
2497 if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode))
|
|
|
2498 return false;
|
|
|
2499 if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) /* means bad sync or got corruption */
|
|
|
2500 return true;
|
|
|
2501 }
|
|
|
2502
|
|
|
2503 if(wasted_bits && do_full_decode) {
|
|
|
2504 x = decoder->private_->frame.subframes[channel].wasted_bits;
|
|
|
2505 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
|
|
|
2506 decoder->private_->output[channel][i] <<= x;
|
|
|
2507 }
|
|
|
2508
|
|
|
2509 return true;
|
|
|
2510 }
|
|
|
2511
|
|
|
2512 FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
|
|
|
2513 {
|
|
|
2514 FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant;
|
|
|
2515 FLAC__int32 x;
|
|
|
2516 unsigned i;
|
|
|
2517 FLAC__int32 *output = decoder->private_->output[channel];
|
|
|
2518
|
|
|
2519 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT;
|
|
|
2520
|
|
|
2521 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
|
|
|
2522 return false; /* read_callback_ sets the state for us */
|
|
|
2523
|
|
|
2524 subframe->value = x;
|
|
|
2525
|
|
|
2526 /* decode the subframe */
|
|
|
2527 if(do_full_decode) {
|
|
|
2528 for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
|
|
|
2529 output[i] = x;
|
|
|
2530 }
|
|
|
2531
|
|
|
2532 return true;
|
|
|
2533 }
|
|
|
2534
|
|
|
2535 FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
|
|
|
2536 {
|
|
|
2537 FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed;
|
|
|
2538 FLAC__int32 i32;
|
|
|
2539 FLAC__uint32 u32;
|
|
|
2540 unsigned u;
|
|
|
2541
|
|
|
2542 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED;
|
|
|
2543
|
|
|
2544 subframe->residual = decoder->private_->residual[channel];
|
|
|
2545 subframe->order = order;
|
|
|
2546
|
|
|
2547 /* read warm-up samples */
|
|
|
2548 for(u = 0; u < order; u++) {
|
|
|
2549 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
|
|
|
2550 return false; /* read_callback_ sets the state for us */
|
|
|
2551 subframe->warmup[u] = i32;
|
|
|
2552 }
|
|
|
2553
|
|
|
2554 /* read entropy coding method info */
|
|
|
2555 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
|
|
|
2556 return false; /* read_callback_ sets the state for us */
|
|
|
2557 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
|
|
|
2558 switch(subframe->entropy_coding_method.type) {
|
|
|
2559 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
|
|
|
2560 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
|
|
|
2561 return false; /* read_callback_ sets the state for us */
|
|
|
2562 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
|
|
|
2563 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
|
|
|
2564 break;
|
|
|
2565 default:
|
|
|
2566 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
|
|
|
2567 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2568 return true;
|
|
|
2569 }
|
|
|
2570
|
|
|
2571 /* read residual */
|
|
|
2572 switch(subframe->entropy_coding_method.type) {
|
|
|
2573 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
|
|
|
2574 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]))
|
|
|
2575 return false;
|
|
|
2576 break;
|
|
|
2577 default:
|
|
|
2578 FLAC__ASSERT(0);
|
|
|
2579 }
|
|
|
2580
|
|
|
2581 /* decode the subframe */
|
|
|
2582 if(do_full_decode) {
|
|
|
2583 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
|
|
|
2584 FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order);
|
|
|
2585 }
|
|
|
2586
|
|
|
2587 return true;
|
|
|
2588 }
|
|
|
2589
|
|
|
2590 FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode)
|
|
|
2591 {
|
|
|
2592 FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc;
|
|
|
2593 FLAC__int32 i32;
|
|
|
2594 FLAC__uint32 u32;
|
|
|
2595 unsigned u;
|
|
|
2596
|
|
|
2597 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC;
|
|
|
2598
|
|
|
2599 subframe->residual = decoder->private_->residual[channel];
|
|
|
2600 subframe->order = order;
|
|
|
2601
|
|
|
2602 /* read warm-up samples */
|
|
|
2603 for(u = 0; u < order; u++) {
|
|
|
2604 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, bps))
|
|
|
2605 return false; /* read_callback_ sets the state for us */
|
|
|
2606 subframe->warmup[u] = i32;
|
|
|
2607 }
|
|
|
2608
|
|
|
2609 /* read qlp coeff precision */
|
|
|
2610 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN))
|
|
|
2611 return false; /* read_callback_ sets the state for us */
|
|
|
2612 if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) {
|
|
|
2613 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
|
|
|
2614 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2615 return true;
|
|
|
2616 }
|
|
|
2617 subframe->qlp_coeff_precision = u32+1;
|
|
|
2618
|
|
|
2619 /* read qlp shift */
|
|
|
2620 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN))
|
|
|
2621 return false; /* read_callback_ sets the state for us */
|
|
|
2622 subframe->quantization_level = i32;
|
|
|
2623
|
|
|
2624 /* read quantized lp coefficiencts */
|
|
|
2625 for(u = 0; u < order; u++) {
|
|
|
2626 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision))
|
|
|
2627 return false; /* read_callback_ sets the state for us */
|
|
|
2628 subframe->qlp_coeff[u] = i32;
|
|
|
2629 }
|
|
|
2630
|
|
|
2631 /* read entropy coding method info */
|
|
|
2632 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN))
|
|
|
2633 return false; /* read_callback_ sets the state for us */
|
|
|
2634 subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32;
|
|
|
2635 switch(subframe->entropy_coding_method.type) {
|
|
|
2636 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
|
|
|
2637 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN))
|
|
|
2638 return false; /* read_callback_ sets the state for us */
|
|
|
2639 subframe->entropy_coding_method.data.partitioned_rice.order = u32;
|
|
|
2640 subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel];
|
|
|
2641 break;
|
|
|
2642 default:
|
|
|
2643 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM);
|
|
|
2644 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2645 return true;
|
|
|
2646 }
|
|
|
2647
|
|
|
2648 /* read residual */
|
|
|
2649 switch(subframe->entropy_coding_method.type) {
|
|
|
2650 case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE:
|
|
|
2651 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]))
|
|
|
2652 return false;
|
|
|
2653 break;
|
|
|
2654 default:
|
|
|
2655 FLAC__ASSERT(0);
|
|
|
2656 }
|
|
|
2657
|
|
|
2658 /* decode the subframe */
|
|
|
2659 if(do_full_decode) {
|
|
|
2660 memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order);
|
|
|
2661 if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32)
|
|
|
2662 if(bps <= 16 && subframe->qlp_coeff_precision <= 16) {
|
|
|
2663 if(order <= 8)
|
|
|
2664 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);
|
|
|
2665 else
|
|
|
2666 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);
|
|
|
2667 }
|
|
|
2668 else
|
|
|
2669 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);
|
|
|
2670 else
|
|
|
2671 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);
|
|
|
2672 }
|
|
|
2673
|
|
|
2674 return true;
|
|
|
2675 }
|
|
|
2676
|
|
|
2677 FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode)
|
|
|
2678 {
|
|
|
2679 FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim;
|
|
|
2680 FLAC__int32 x, *residual = decoder->private_->residual[channel];
|
|
|
2681 unsigned i;
|
|
|
2682
|
|
|
2683 decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM;
|
|
|
2684
|
|
|
2685 subframe->data = residual;
|
|
|
2686
|
|
|
2687 for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
|
|
|
2688 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &x, bps))
|
|
|
2689 return false; /* read_callback_ sets the state for us */
|
|
|
2690 residual[i] = x;
|
|
|
2691 }
|
|
|
2692
|
|
|
2693 /* decode the subframe */
|
|
|
2694 if(do_full_decode)
|
|
|
2695 memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize);
|
|
|
2696
|
|
|
2697 return true;
|
|
|
2698 }
|
|
|
2699
|
|
|
2700 FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual)
|
|
|
2701 {
|
|
|
2702 FLAC__uint32 rice_parameter;
|
|
|
2703 int i;
|
|
|
2704 unsigned partition, sample, u;
|
|
|
2705 const unsigned partitions = 1u << partition_order;
|
|
|
2706 const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order;
|
|
|
2707
|
|
|
2708 /* sanity checks */
|
|
|
2709 if(partition_order == 0) {
|
|
|
2710 if(decoder->private_->frame.header.blocksize < predictor_order) {
|
|
|
2711 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
|
|
|
2712 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2713 return true;
|
|
|
2714 }
|
|
|
2715 }
|
|
|
2716 else {
|
|
|
2717 if(partition_samples < predictor_order) {
|
|
|
2718 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
|
|
|
2719 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2720 return true;
|
|
|
2721 }
|
|
|
2722 }
|
|
|
2723
|
|
|
2724 if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) {
|
|
|
2725 decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
|
|
|
2726 return false;
|
|
|
2727 }
|
|
|
2728
|
|
|
2729 sample = 0;
|
|
|
2730 for(partition = 0; partition < partitions; partition++) {
|
|
|
2731 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN))
|
|
|
2732 return false; /* read_callback_ sets the state for us */
|
|
|
2733 partitioned_rice_contents->parameters[partition] = rice_parameter;
|
|
|
2734 if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) {
|
|
|
2735 u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order;
|
|
|
2736 if(!FLAC__bitreader_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter))
|
|
|
2737 return false; /* read_callback_ sets the state for us */
|
|
|
2738 sample += u;
|
|
|
2739 }
|
|
|
2740 else {
|
|
|
2741 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN))
|
|
|
2742 return false; /* read_callback_ sets the state for us */
|
|
|
2743 partitioned_rice_contents->raw_bits[partition] = rice_parameter;
|
|
|
2744 for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) {
|
|
|
2745 if(!FLAC__bitreader_read_raw_int32(decoder->private_->input, &i, rice_parameter))
|
|
|
2746 return false; /* read_callback_ sets the state for us */
|
|
|
2747 residual[sample] = i;
|
|
|
2748 }
|
|
|
2749 }
|
|
|
2750 }
|
|
|
2751
|
|
|
2752 return true;
|
|
|
2753 }
|
|
|
2754
|
|
|
2755 FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder)
|
|
|
2756 {
|
|
|
2757 if(!FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input)) {
|
|
|
2758 FLAC__uint32 zero = 0;
|
|
|
2759 if(!FLAC__bitreader_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitreader_bits_left_for_byte_alignment(decoder->private_->input)))
|
|
|
2760 return false; /* read_callback_ sets the state for us */
|
|
|
2761 if(zero != 0) {
|
|
|
2762 send_error_to_client_(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC);
|
|
|
2763 decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC;
|
|
|
2764 }
|
|
|
2765 }
|
|
|
2766 return true;
|
|
|
2767 }
|
|
|
2768
|
|
|
2769 FLAC__bool read_callback_(FLAC__byte buffer[], size_t *bytes, void *client_data)
|
|
|
2770 {
|
|
|
2771 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data;
|
|
|
2772
|
|
|
2773 if(
|
|
|
2774 #if FLAC__HAS_OGG
|
|
|
2775 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
|
|
|
2776 !decoder->private_->is_ogg &&
|
|
|
2777 #endif
|
|
|
2778 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
|
|
|
2779 ) {
|
|
|
2780 *bytes = 0;
|
|
|
2781 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
|
|
|
2782 return false;
|
|
|
2783 }
|
|
|
2784 else if(*bytes > 0) {
|
|
|
2785 /* While seeking, it is possible for our seek to land in the
|
|
|
2786 * middle of audio data that looks exactly like a frame header
|
|
|
2787 * from a future version of an encoder. When that happens, our
|
|
|
2788 * error callback will get an
|
|
|
2789 * FLAC__STREAM_DECODER_UNPARSEABLE_STREAM and increment its
|
|
|
2790 * unparseable_frame_count. But there is a remote possibility
|
|
|
2791 * that it is properly synced at such a "future-codec frame",
|
|
|
2792 * so to make sure, we wait to see many "unparseable" errors in
|
|
|
2793 * a row before bailing out.
|
|
|
2794 */
|
|
|
2795 if(decoder->private_->is_seeking && decoder->private_->unparseable_frame_count > 20) {
|
|
|
2796 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
|
|
|
2797 return false;
|
|
|
2798 }
|
|
|
2799 else {
|
|
|
2800 const FLAC__StreamDecoderReadStatus status =
|
|
|
2801 #if FLAC__HAS_OGG
|
|
|
2802 decoder->private_->is_ogg?
|
|
|
2803 read_callback_ogg_aspect_(decoder, buffer, bytes) :
|
|
|
2804 #endif
|
|
|
2805 decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data)
|
|
|
2806 ;
|
|
|
2807 if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) {
|
|
|
2808 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
|
|
|
2809 return false;
|
|
|
2810 }
|
|
|
2811 else if(*bytes == 0) {
|
|
|
2812 if(
|
|
|
2813 status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM ||
|
|
|
2814 (
|
|
|
2815 #if FLAC__HAS_OGG
|
|
|
2816 /* see [1] HACK NOTE below for why we don't call the eof_callback when decoding Ogg FLAC */
|
|
|
2817 !decoder->private_->is_ogg &&
|
|
|
2818 #endif
|
|
|
2819 decoder->private_->eof_callback && decoder->private_->eof_callback(decoder, decoder->private_->client_data)
|
|
|
2820 )
|
|
|
2821 ) {
|
|
|
2822 decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM;
|
|
|
2823 return false;
|
|
|
2824 }
|
|
|
2825 else
|
|
|
2826 return true;
|
|
|
2827 }
|
|
|
2828 else
|
|
|
2829 return true;
|
|
|
2830 }
|
|
|
2831 }
|
|
|
2832 else {
|
|
|
2833 /* abort to avoid a deadlock */
|
|
|
2834 decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED;
|
|
|
2835 return false;
|
|
|
2836 }
|
|
|
2837 /* [1] @@@ HACK NOTE: The end-of-stream checking has to be hacked around
|
|
|
2838 * for Ogg FLAC. This is because the ogg decoder aspect can lose sync
|
|
|
2839 * and at the same time hit the end of the stream (for example, seeking
|
|
|
2840 * to a point that is after the beginning of the last Ogg page). There
|
|
|
2841 * is no way to report an Ogg sync loss through the callbacks (see note
|
|
|
2842 * in read_callback_ogg_aspect_()) so it returns CONTINUE with *bytes==0.
|
|
|
2843 * So to keep the decoder from stopping at this point we gate the call
|
|
|
2844 * to the eof_callback and let the Ogg decoder aspect set the
|
|
|
2845 * end-of-stream state when it is needed.
|
|
|
2846 */
|
|
|
2847 }
|
|
|
2848
|
|
|
2849 #if FLAC__HAS_OGG
|
|
|
2850 FLAC__StreamDecoderReadStatus read_callback_ogg_aspect_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes)
|
|
|
2851 {
|
|
|
2852 switch(FLAC__ogg_decoder_aspect_read_callback_wrapper(&decoder->protected_->ogg_decoder_aspect, buffer, bytes, read_callback_proxy_, decoder, decoder->private_->client_data)) {
|
|
|
2853 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK:
|
|
|
2854 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
|
|
|
2855 /* we don't really have a way to handle lost sync via read
|
|
|
2856 * callback so we'll let it pass and let the underlying
|
|
|
2857 * FLAC decoder catch the error
|
|
|
2858 */
|
|
|
2859 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_LOST_SYNC:
|
|
|
2860 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
|
|
|
2861 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM:
|
|
|
2862 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
|
|
|
2863 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_NOT_FLAC:
|
|
|
2864 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_UNSUPPORTED_MAPPING_VERSION:
|
|
|
2865 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT:
|
|
|
2866 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_ERROR:
|
|
|
2867 case FLAC__OGG_DECODER_ASPECT_READ_STATUS_MEMORY_ALLOCATION_ERROR:
|
|
|
2868 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
|
|
|
2869 default:
|
|
|
2870 FLAC__ASSERT(0);
|
|
|
2871 /* double protection */
|
|
|
2872 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
|
|
|
2873 }
|
|
|
2874 }
|
|
|
2875
|
|
|
2876 FLAC__OggDecoderAspectReadStatus read_callback_proxy_(const void *void_decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
|
|
|
2877 {
|
|
|
2878 FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder*)void_decoder;
|
|
|
2879
|
|
|
2880 switch(decoder->private_->read_callback(decoder, buffer, bytes, client_data)) {
|
|
|
2881 case FLAC__STREAM_DECODER_READ_STATUS_CONTINUE:
|
|
|
2882 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_OK;
|
|
|
2883 case FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM:
|
|
|
2884 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_END_OF_STREAM;
|
|
|
2885 case FLAC__STREAM_DECODER_READ_STATUS_ABORT:
|
|
|
2886 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
|
|
|
2887 default:
|
|
|
2888 /* double protection: */
|
|
|
2889 FLAC__ASSERT(0);
|
|
|
2890 return FLAC__OGG_DECODER_ASPECT_READ_STATUS_ABORT;
|
|
|
2891 }
|
|
|
2892 }
|
|
|
2893 #endif
|
|
|
2894
|
|
|
2895 FLAC__StreamDecoderWriteStatus write_audio_frame_to_client_(FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[])
|
|
|
2896 {
|
|
|
2897 if(decoder->private_->is_seeking) {
|
|
|
2898 FLAC__uint64 this_frame_sample = frame->header.number.sample_number;
|
|
|
2899 FLAC__uint64 next_frame_sample = this_frame_sample + (FLAC__uint64)frame->header.blocksize;
|
|
|
2900 FLAC__uint64 target_sample = decoder->private_->target_sample;
|
|
|
2901
|
|
|
2902 FLAC__ASSERT(frame->header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
|
|
|
2903
|
|
|
2904 #if FLAC__HAS_OGG
|
|
|
2905 decoder->private_->got_a_frame = true;
|
|
|
2906 #endif
|
|
|
2907 decoder->private_->last_frame = *frame; /* save the frame */
|
|
|
2908 if(this_frame_sample <= target_sample && target_sample < next_frame_sample) { /* we hit our target frame */
|
|
|
2909 unsigned delta = (unsigned)(target_sample - this_frame_sample);
|
|
|
2910 /* kick out of seek mode */
|
|
|
2911 decoder->private_->is_seeking = false;
|
|
|
2912 /* shift out the samples before target_sample */
|
|
|
2913 if(delta > 0) {
|
|
|
2914 unsigned channel;
|
|
|
2915 const FLAC__int32 *newbuffer[FLAC__MAX_CHANNELS];
|
|
|
2916 for(channel = 0; channel < frame->header.channels; channel++)
|
|
|
2917 newbuffer[channel] = buffer[channel] + delta;
|
|
|
2918 decoder->private_->last_frame.header.blocksize -= delta;
|
|
|
2919 decoder->private_->last_frame.header.number.sample_number += (FLAC__uint64)delta;
|
|
|
2920 /* write the relevant samples */
|
|
|
2921 return decoder->private_->write_callback(decoder, &decoder->private_->last_frame, newbuffer, decoder->private_->client_data);
|
|
|
2922 }
|
|
|
2923 else {
|
|
|
2924 /* write the relevant samples */
|
|
|
2925 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
|
|
|
2926 }
|
|
|
2927 }
|
|
|
2928 else {
|
|
|
2929 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
|
2930 }
|
|
|
2931 }
|
|
|
2932 else {
|
|
|
2933 /*
|
|
|
2934 * If we never got STREAMINFO, turn off MD5 checking to save
|
|
|
2935 * cycles since we don't have a sum to compare to anyway
|
|
|
2936 */
|
|
|
2937 if(!decoder->private_->has_stream_info)
|
|
|
2938 decoder->private_->do_md5_checking = false;
|
|
|
2939 if(decoder->private_->do_md5_checking) {
|
|
|
2940 if(!FLAC__MD5Accumulate(&decoder->private_->md5context, buffer, frame->header.channels, frame->header.blocksize, (frame->header.bits_per_sample+7) / 8))
|
|
|
2941 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
|
|
|
2942 }
|
|
|
2943 return decoder->private_->write_callback(decoder, frame, buffer, decoder->private_->client_data);
|
|
|
2944 }
|
|
|
2945 }
|
|
|
2946
|
|
|
2947 void send_error_to_client_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status)
|
|
|
2948 {
|
|
|
2949 if(!decoder->private_->is_seeking)
|
|
|
2950 decoder->private_->error_callback(decoder, status, decoder->private_->client_data);
|
|
|
2951 else if(status == FLAC__STREAM_DECODER_ERROR_STATUS_UNPARSEABLE_STREAM)
|
|
|
2952 decoder->private_->unparseable_frame_count++;
|
|
|
2953 }
|
|
|
2954
|
|
|
2955 FLAC__bool seek_to_absolute_sample_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
|
|
|
2956 {
|
|
|
2957 FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
|
|
|
2958 FLAC__int64 pos = -1;
|
|
|
2959 int i;
|
|
|
2960 unsigned approx_bytes_per_frame;
|
|
|
2961 FLAC__bool first_seek = true;
|
|
|
2962 const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
|
|
|
2963 const unsigned min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
|
|
|
2964 const unsigned max_blocksize = decoder->private_->stream_info.data.stream_info.max_blocksize;
|
|
|
2965 const unsigned max_framesize = decoder->private_->stream_info.data.stream_info.max_framesize;
|
|
|
2966 const unsigned min_framesize = decoder->private_->stream_info.data.stream_info.min_framesize;
|
|
|
2967 /* take these from the current frame in case they've changed mid-stream */
|
|
|
2968 unsigned channels = FLAC__stream_decoder_get_channels(decoder);
|
|
|
2969 unsigned bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
|
|
|
2970 const FLAC__StreamMetadata_SeekTable *seek_table = decoder->private_->has_seek_table? &decoder->private_->seek_table.data.seek_table : 0;
|
|
|
2971
|
|
|
2972 /* use values from stream info if we didn't decode a frame */
|
|
|
2973 if(channels == 0)
|
|
|
2974 channels = decoder->private_->stream_info.data.stream_info.channels;
|
|
|
2975 if(bps == 0)
|
|
|
2976 bps = decoder->private_->stream_info.data.stream_info.bits_per_sample;
|
|
|
2977
|
|
|
2978 /* we are just guessing here */
|
|
|
2979 if(max_framesize > 0)
|
|
|
2980 approx_bytes_per_frame = (max_framesize + min_framesize) / 2 + 1;
|
|
|
2981
|
|
|
2982 /*
|
|
|
2983 * Check if it's a known fixed-blocksize stream. Note that though
|
|
|
2984 * the spec doesn't allow zeroes in the STREAMINFO block, we may
|
|
|
2985 * never get a STREAMINFO block when decoding so the value of
|
|
|
2986 * min_blocksize might be zero.
|
|
|
2987 */
|
|
|
2988 else if(min_blocksize == max_blocksize && min_blocksize > 0) {
|
|
|
2989 /* note there are no () around 'bps/8' to keep precision up since it's an integer calulation */
|
|
|
2990 approx_bytes_per_frame = min_blocksize * channels * bps/8 + 64;
|
|
|
2991 }
|
|
|
2992 else
|
|
|
2993 approx_bytes_per_frame = 4096 * channels * bps/8 + 64;
|
|
|
2994
|
|
|
2995 /*
|
|
|
2996 * First, we set an upper and lower bound on where in the
|
|
|
2997 * stream we will search. For now we assume the worst case
|
|
|
2998 * scenario, which is our best guess at the beginning of
|
|
|
2999 * the first frame and end of the stream.
|
|
|
3000 */
|
|
|
3001 lower_bound = first_frame_offset;
|
|
|
3002 lower_bound_sample = 0;
|
|
|
3003 upper_bound = stream_length;
|
|
|
3004 upper_bound_sample = total_samples > 0 ? total_samples : target_sample;
|
|
|
3005 if(upper_bound_sample == 0)
|
|
|
3006 upper_bound_sample = 1;
|
|
|
3007
|
|
|
3008 /*
|
|
|
3009 * Now we refine the bounds if we have a seektable with
|
|
|
3010 * suitable points. Note that according to the spec they
|
|
|
3011 * must be ordered by ascending sample number.
|
|
|
3012 */
|
|
|
3013 if(seek_table) {
|
|
|
3014 /* find the closest seek point <= target_sample, if it exists */
|
|
|
3015 for(i = (int)seek_table->num_points - 1; i >= 0; i--) {
|
|
|
3016 if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && seek_table->points[i].sample_number <= target_sample)
|
|
|
3017 break;
|
|
|
3018 }
|
|
|
3019 if(i >= 0) { /* i.e. we found a suitable seek point... */
|
|
|
3020 lower_bound = first_frame_offset + seek_table->points[i].stream_offset;
|
|
|
3021 lower_bound_sample = seek_table->points[i].sample_number;
|
|
|
3022 }
|
|
|
3023
|
|
|
3024 /* find the closest seek point > target_sample, if it exists */
|
|
|
3025 for(i = 0; i < (int)seek_table->num_points; i++) {
|
|
|
3026 if(seek_table->points[i].sample_number != FLAC__STREAM_METADATA_SEEKPOINT_PLACEHOLDER && seek_table->points[i].sample_number > target_sample)
|
|
|
3027 break;
|
|
|
3028 }
|
|
|
3029 if(i < (int)seek_table->num_points) { /* i.e. we found a suitable seek point... */
|
|
|
3030 upper_bound = first_frame_offset + seek_table->points[i].stream_offset;
|
|
|
3031 upper_bound_sample = seek_table->points[i].sample_number;
|
|
|
3032 }
|
|
|
3033 }
|
|
|
3034
|
|
|
3035 decoder->private_->target_sample = target_sample;
|
|
|
3036 while(1) {
|
|
|
3037 /* check if the bounds are still ok */
|
|
|
3038 if (lower_bound_sample >= upper_bound_sample || lower_bound > upper_bound) {
|
|
|
3039 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
|
|
|
3040 return false;
|
|
|
3041 }
|
|
|
3042 #ifndef FLAC__INTEGER_ONLY_LIBRARY
|
|
|
3043 #if defined _MSC_VER || defined __MINGW32__
|
|
|
3044 /* with VC++ you have to spoon feed it the casting */
|
|
|
3045 pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(FLAC__int64)(target_sample - lower_bound_sample) / (FLAC__double)(FLAC__int64)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(FLAC__int64)(upper_bound - lower_bound)) - approx_bytes_per_frame;
|
|
|
3046 #else
|
|
|
3047 pos = (FLAC__int64)lower_bound + (FLAC__int64)((FLAC__double)(target_sample - lower_bound_sample) / (FLAC__double)(upper_bound_sample - lower_bound_sample) * (FLAC__double)(upper_bound - lower_bound)) - approx_bytes_per_frame;
|
|
|
3048 #endif
|
|
|
3049 #else
|
|
|
3050 /* a little less accurate: */
|
|
|
3051 if(upper_bound - lower_bound < 0xffffffff)
|
|
|
3052 pos = (FLAC__int64)lower_bound + (FLAC__int64)(((target_sample - lower_bound_sample) * (upper_bound - lower_bound)) / (upper_bound_sample - lower_bound_sample)) - approx_bytes_per_frame;
|
|
|
3053 else /* @@@ WATCHOUT, ~2TB limit */
|
|
|
3054 pos = (FLAC__int64)lower_bound + (FLAC__int64)((((target_sample - lower_bound_sample)>>8) * ((upper_bound - lower_bound)>>8)) / ((upper_bound_sample - lower_bound_sample)>>16)) - approx_bytes_per_frame;
|
|
|
3055 #endif
|
|
|
3056 if(pos >= (FLAC__int64)upper_bound)
|
|
|
3057 pos = (FLAC__int64)upper_bound - 1;
|
|
|
3058 if(pos < (FLAC__int64)lower_bound)
|
|
|
3059 pos = (FLAC__int64)lower_bound;
|
|
|
3060 if(decoder->private_->seek_callback(decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
|
|
|
3061 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
|
|
|
3062 return false;
|
|
|
3063 }
|
|
|
3064 if(!FLAC__stream_decoder_flush(decoder)) {
|
|
|
3065 /* above call sets the state for us */
|
|
|
3066 return false;
|
|
|
3067 }
|
|
|
3068 /* Now we need to get a frame. First we need to reset our
|
|
|
3069 * unparseable_frame_count; if we get too many unparseable
|
|
|
3070 * frames in a row, the read callback will return
|
|
|
3071 * FLAC__STREAM_DECODER_READ_STATUS_ABORT, causing
|
|
|
3072 * FLAC__stream_decoder_process_single() to return false.
|
|
|
3073 */
|
|
|
3074 decoder->private_->unparseable_frame_count = 0;
|
|
|
3075 if(!FLAC__stream_decoder_process_single(decoder)) {
|
|
|
3076 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
|
|
|
3077 return false;
|
|
|
3078 }
|
|
|
3079 /* our write callback will change the state when it gets to the target frame */
|
|
|
3080 /* actually, we could have got_a_frame if our decoder is at FLAC__STREAM_DECODER_END_OF_STREAM so we need to check for that also */
|
|
|
3081 #if 0
|
|
|
3082 /*@@@@@@ used to be the following; not clear if the check for end of stream is needed anymore */
|
|
|
3083 if(decoder->protected_->state != FLAC__SEEKABLE_STREAM_DECODER_SEEKING && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM)
|
|
|
3084 break;
|
|
|
3085 #endif
|
|
|
3086 if(!decoder->private_->is_seeking) {
|
|
|
3087 break;
|
|
|
3088 }
|
|
|
3089 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
|
|
|
3090
|
|
|
3091 if (!decoder->private_->samples_decoded || (this_frame_sample + decoder->private_->last_frame.header.blocksize >= upper_bound_sample && !first_seek)) {
|
|
|
3092 if (pos == (FLAC__int64)lower_bound) {
|
|
|
3093 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
|
|
|
3094 return false;
|
|
|
3095 }
|
|
|
3096 /* our last move backwards wasn't big enough, try again */
|
|
|
3097 approx_bytes_per_frame *= 2;
|
|
|
3098 continue;
|
|
|
3099 }
|
|
|
3100 /* allow one seek over upper bound, required for streams with unknown total_samples */
|
|
|
3101 first_seek = false;
|
|
|
3102
|
|
|
3103 /* make sure we are not seeking in corrupted stream */
|
|
|
3104 if (this_frame_sample < lower_bound_sample) {
|
|
|
3105 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
|
|
|
3106 return false;
|
|
|
3107 }
|
|
|
3108
|
|
|
3109 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
|
|
|
3110
|
|
|
3111 /* we need to narrow the search */
|
|
|
3112 if(target_sample < this_frame_sample) {
|
|
|
3113 upper_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
|
|
|
3114 if(!FLAC__stream_decoder_get_decode_position(decoder, &upper_bound)) {
|
|
|
3115 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
|
|
|
3116 return false;
|
|
|
3117 }
|
|
|
3118 approx_bytes_per_frame = (unsigned)(2 * (upper_bound - pos) / 3 + 16);
|
|
|
3119 }
|
|
|
3120 else {
|
|
|
3121 /* target_sample >= this_frame_sample + this frame's blocksize */
|
|
|
3122
|
|
|
3123 lower_bound_sample = this_frame_sample + decoder->private_->last_frame.header.blocksize;
|
|
|
3124 if(!FLAC__stream_decoder_get_decode_position(decoder, &lower_bound)) {
|
|
|
3125 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
|
|
|
3126 return false;
|
|
|
3127 }
|
|
|
3128 approx_bytes_per_frame = (unsigned)(2 * (lower_bound - pos) / 3 + 16);
|
|
|
3129 }
|
|
|
3130 }
|
|
|
3131
|
|
|
3132 return true;
|
|
|
3133 }
|
|
|
3134
|
|
|
3135 #if FLAC__HAS_OGG
|
|
|
3136 FLAC__bool seek_to_absolute_sample_ogg_(FLAC__StreamDecoder *decoder, FLAC__uint64 stream_length, FLAC__uint64 target_sample)
|
|
|
3137 {
|
|
|
3138 FLAC__uint64 left_pos = 0, right_pos = stream_length;
|
|
|
3139 FLAC__uint64 left_sample = 0, right_sample = FLAC__stream_decoder_get_total_samples(decoder);
|
|
|
3140 FLAC__uint64 this_frame_sample = 0; /* only initialized to avoid compiler warning */
|
|
|
3141 FLAC__uint64 pos = 0; /* only initialized to avoid compiler warning */
|
|
|
3142 FLAC__bool did_a_seek;
|
|
|
3143 unsigned iteration = 0;
|
|
|
3144
|
|
|
3145 /* In the first iterations, we will calculate the target byte position
|
|
|
3146 * by the distance from the target sample to left_sample and
|
|
|
3147 * right_sample (let's call it "proportional search"). After that, we
|
|
|
3148 * will switch to binary search.
|
|
|
3149 */
|
|
|
3150 unsigned BINARY_SEARCH_AFTER_ITERATION = 2;
|
|
|
3151
|
|
|
3152 /* We will switch to a linear search once our current sample is less
|
|
|
3153 * than this number of samples ahead of the target sample
|
|
|
3154 */
|
|
|
3155 static const FLAC__uint64 LINEAR_SEARCH_WITHIN_SAMPLES = FLAC__MAX_BLOCK_SIZE * 2;
|
|
|
3156
|
|
|
3157 /* If the total number of samples is unknown, use a large value, and
|
|
|
3158 * force binary search immediately.
|
|
|
3159 */
|
|
|
3160 if(right_sample == 0) {
|
|
|
3161 right_sample = (FLAC__uint64)(-1);
|
|
|
3162 BINARY_SEARCH_AFTER_ITERATION = 0;
|
|
|
3163 }
|
|
|
3164
|
|
|
3165 decoder->private_->target_sample = target_sample;
|
|
|
3166 for( ; ; iteration++) {
|
|
|
3167 if (iteration == 0 || this_frame_sample > target_sample || target_sample - this_frame_sample > LINEAR_SEARCH_WITHIN_SAMPLES) {
|
|
|
3168 if (iteration >= BINARY_SEARCH_AFTER_ITERATION) {
|
|
|
3169 pos = (right_pos + left_pos) / 2;
|
|
|
3170 }
|
|
|
3171 else {
|
|
|
3172 #ifndef FLAC__INTEGER_ONLY_LIBRARY
|
|
|
3173 #if defined _MSC_VER || defined __MINGW32__
|
|
|
3174 /* with MSVC you have to spoon feed it the casting */
|
|
|
3175 pos = (FLAC__uint64)((FLAC__double)(FLAC__int64)(target_sample - left_sample) / (FLAC__double)(FLAC__int64)(right_sample - left_sample) * (FLAC__double)(FLAC__int64)(right_pos - left_pos));
|
|
|
3176 #else
|
|
|
3177 pos = (FLAC__uint64)((FLAC__double)(target_sample - left_sample) / (FLAC__double)(right_sample - left_sample) * (FLAC__double)(right_pos - left_pos));
|
|
|
3178 #endif
|
|
|
3179 #else
|
|
|
3180 /* a little less accurate: */
|
|
|
3181 if ((target_sample-left_sample <= 0xffffffff) && (right_pos-left_pos <= 0xffffffff))
|
|
|
3182 pos = (FLAC__int64)(((target_sample-left_sample) * (right_pos-left_pos)) / (right_sample-left_sample));
|
|
|
3183 else /* @@@ WATCHOUT, ~2TB limit */
|
|
|
3184 pos = (FLAC__int64)((((target_sample-left_sample)>>8) * ((right_pos-left_pos)>>8)) / ((right_sample-left_sample)>>16));
|
|
|
3185 #endif
|
|
|
3186 /* @@@ TODO: might want to limit pos to some distance
|
|
|
3187 * before EOF, to make sure we land before the last frame,
|
|
|
3188 * thereby getting a this_frame_sample and so having a better
|
|
|
3189 * estimate. @@@@@@DELETE:this would also mostly (or totally if we could
|
|
|
3190 * be sure to land before the last frame) avoid the
|
|
|
3191 * end-of-stream case we have to check later.
|
|
|
3192 */
|
|
|
3193 }
|
|
|
3194
|
|
|
3195 /* physical seek */
|
|
|
3196 if(decoder->private_->seek_callback((FLAC__StreamDecoder*)decoder, (FLAC__uint64)pos, decoder->private_->client_data) != FLAC__STREAM_DECODER_SEEK_STATUS_OK) {
|
|
|
3197 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
|
|
|
3198 return false;
|
|
|
3199 }
|
|
|
3200 if(!FLAC__stream_decoder_flush(decoder)) {
|
|
|
3201 /* above call sets the state for us */
|
|
|
3202 return false;
|
|
|
3203 }
|
|
|
3204 did_a_seek = true;
|
|
|
3205 }
|
|
|
3206 else
|
|
|
3207 did_a_seek = false;
|
|
|
3208
|
|
|
3209 decoder->private_->got_a_frame = false;
|
|
|
3210 if(!FLAC__stream_decoder_process_single(decoder)) {
|
|
|
3211 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
|
|
|
3212 return false;
|
|
|
3213 }
|
|
|
3214 if(!decoder->private_->got_a_frame) {
|
|
|
3215 if(did_a_seek) {
|
|
|
3216 /* this can happen if we seek to a point after the last frame; we drop
|
|
|
3217 * to binary search right away in this case to avoid any wasted
|
|
|
3218 * iterations of proportional search.
|
|
|
3219 */
|
|
|
3220 right_pos = pos;
|
|
|
3221 BINARY_SEARCH_AFTER_ITERATION = 0;
|
|
|
3222 }
|
|
|
3223 else {
|
|
|
3224 /* this can probably only happen if total_samples is unknown and the
|
|
|
3225 * target_sample is past the end of the stream
|
|
|
3226 */
|
|
|
3227 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
|
|
|
3228 return false;
|
|
|
3229 }
|
|
|
3230 }
|
|
|
3231 /* our write callback will change the state when it gets to the target frame */
|
|
|
3232 else if(!decoder->private_->is_seeking/*@@@@@@ && decoder->protected_->state != FLAC__STREAM_DECODER_END_OF_STREAM*/) {
|
|
|
3233 break;
|
|
|
3234 }
|
|
|
3235 else {
|
|
|
3236 this_frame_sample = decoder->private_->last_frame.header.number.sample_number;
|
|
|
3237 FLAC__ASSERT(decoder->private_->last_frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER);
|
|
|
3238
|
|
|
3239 if (did_a_seek) {
|
|
|
3240 if (this_frame_sample <= target_sample) {
|
|
|
3241 /* The 'equal' case should not happen, since
|
|
|
3242 * FLAC__stream_decoder_process_single()
|
|
|
3243 * should recognize that it has hit the
|
|
|
3244 * target sample and we would exit through
|
|
|
3245 * the 'break' above.
|
|
|
3246 */
|
|
|
3247 FLAC__ASSERT(this_frame_sample != target_sample);
|
|
|
3248
|
|
|
3249 left_sample = this_frame_sample;
|
|
|
3250 /* sanity check to avoid infinite loop */
|
|
|
3251 if (left_pos == pos) {
|
|
|
3252 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
|
|
|
3253 return false;
|
|
|
3254 }
|
|
|
3255 left_pos = pos;
|
|
|
3256 }
|
|
|
3257 else if(this_frame_sample > target_sample) {
|
|
|
3258 right_sample = this_frame_sample;
|
|
|
3259 /* sanity check to avoid infinite loop */
|
|
|
3260 if (right_pos == pos) {
|
|
|
3261 decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
|
|
|
3262 return false;
|
|
|
3263 }
|
|
|
3264 right_pos = pos;
|
|
|
3265 }
|
|
|
3266 }
|
|
|
3267 }
|
|
|
3268 }
|
|
|
3269
|
|
|
3270 return true;
|
|
|
3271 }
|
|
|
3272 #endif
|
|
|
3273
|
|
|
3274 FLAC__StreamDecoderReadStatus file_read_callback_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
|
|
|
3275 {
|
|
|
3276 (void)client_data;
|
|
|
3277
|
|
|
3278 if(*bytes > 0) {
|
|
721
|
3279 *bytes = vfs_fread(buffer, sizeof(FLAC__byte), *bytes, decoder->private_->file);
|
|
|
3280 #ifndef USE_VFS
|
|
715
|
3281 if(ferror(decoder->private_->file))
|
|
|
3282 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
|
|
721
|
3283 else
|
|
|
3284 #endif
|
|
|
3285 if(*bytes == 0)
|
|
715
|
3286 return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
|
|
|
3287 else
|
|
|
3288 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
|
|
|
3289 }
|
|
|
3290 else
|
|
|
3291 return FLAC__STREAM_DECODER_READ_STATUS_ABORT; /* abort to avoid a deadlock */
|
|
|
3292 }
|
|
|
3293
|
|
|
3294 FLAC__StreamDecoderSeekStatus file_seek_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
|
|
|
3295 {
|
|
|
3296 (void)client_data;
|
|
|
3297
|
|
721
|
3298 #ifndef USE_VFS
|
|
715
|
3299 if(decoder->private_->file == stdin)
|
|
|
3300 return FLAC__STREAM_DECODER_SEEK_STATUS_UNSUPPORTED;
|
|
721
|
3301 else
|
|
|
3302 #endif
|
|
|
3303 if(vfs_fseek(decoder->private_->file, (off_t)absolute_byte_offset, SEEK_SET) < 0)
|
|
715
|
3304 return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
|
|
|
3305 else
|
|
|
3306 return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
|
|
|
3307 }
|
|
|
3308
|
|
|
3309 FLAC__StreamDecoderTellStatus file_tell_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
|
|
|
3310 {
|
|
|
3311 off_t pos;
|
|
|
3312 (void)client_data;
|
|
|
3313
|
|
721
|
3314 #ifndef USE_VFS
|
|
715
|
3315 if(decoder->private_->file == stdin)
|
|
|
3316 return FLAC__STREAM_DECODER_TELL_STATUS_UNSUPPORTED;
|
|
721
|
3317 else
|
|
|
3318 #endif
|
|
|
3319 if((pos = vfs_ftell(decoder->private_->file)) < 0)
|
|
715
|
3320 return FLAC__STREAM_DECODER_TELL_STATUS_ERROR;
|
|
|
3321 else {
|
|
|
3322 *absolute_byte_offset = (FLAC__uint64)pos;
|
|
|
3323 return FLAC__STREAM_DECODER_TELL_STATUS_OK;
|
|
|
3324 }
|
|
|
3325 }
|
|
|
3326
|
|
|
3327 FLAC__StreamDecoderLengthStatus file_length_callback_(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
|
|
|
3328 {
|
|
|
3329 struct stat filestats;
|
|
|
3330 (void)client_data;
|
|
|
3331
|
|
721
|
3332 #ifndef USE_VFS
|
|
715
|
3333 if(decoder->private_->file == stdin)
|
|
|
3334 return FLAC__STREAM_DECODER_LENGTH_STATUS_UNSUPPORTED;
|
|
|
3335 else if(fstat(fileno(decoder->private_->file), &filestats) != 0)
|
|
|
3336 return FLAC__STREAM_DECODER_LENGTH_STATUS_ERROR;
|
|
|
3337 else {
|
|
721
|
3338 #endif
|
|
715
|
3339 *stream_length = (FLAC__uint64)filestats.st_size;
|
|
|
3340 return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
|
|
721
|
3341 #ifndef USE_VFS
|
|
715
|
3342 }
|
|
721
|
3343 #endif
|
|
715
|
3344 }
|
|
|
3345
|
|
|
3346 FLAC__bool file_eof_callback_(const FLAC__StreamDecoder *decoder, void *client_data)
|
|
|
3347 {
|
|
|
3348 (void)client_data;
|
|
|
3349
|
|
721
|
3350 return vfs_feof(decoder->private_->file)? true : false;
|
|
715
|
3351 }
|