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