Josh Coalson | 26560dd | 2001-02-08 00:38:41 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Josh Coalson | a78fac6 | 2005-01-25 04:17:55 +0000 | [diff] [blame] | 2 | * Copyright (C) 2000,2001,2002,2003,2004,2005 Josh Coalson |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 7 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 10 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 11 | * - Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * - Neither the name of the Xiph.org Foundation nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> /* for malloc() */ |
Josh Coalson | ec1af65 | 2001-04-01 05:34:16 +0000 | [diff] [blame] | 34 | #include <string.h> /* for memset/memcpy() */ |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 35 | #include "FLAC/assert.h" |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 36 | #include "protected/stream_decoder.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 37 | #include "private/bitbuffer.h" |
Josh Coalson | 7258291 | 2002-10-26 04:33:49 +0000 | [diff] [blame] | 38 | #include "private/bitmath.h" |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 39 | #include "private/cpu.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 40 | #include "private/crc.h" |
| 41 | #include "private/fixed.h" |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 42 | #include "private/format.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 43 | #include "private/lpc.h" |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 44 | #include "private/memory.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 45 | |
Josh Coalson | 5e31be1 | 2002-12-04 07:07:35 +0000 | [diff] [blame] | 46 | #ifdef HAVE_CONFIG_H |
| 47 | #include <config.h> |
| 48 | #endif |
| 49 | |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 50 | #ifdef max |
| 51 | #undef max |
| 52 | #endif |
| 53 | #define max(a,b) ((a)>(b)?(a):(b)) |
| 54 | |
Josh Coalson | 49623ea | 2004-07-29 06:28:59 +0000 | [diff] [blame] | 55 | /* adjust for compilers that can't understand using LLU suffix for uint64_t literals */ |
| 56 | #ifdef _MSC_VER |
| 57 | #define FLAC__U64L(x) x |
| 58 | #else |
| 59 | #define FLAC__U64L(x) x##LLU |
| 60 | #endif |
| 61 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 62 | /*********************************************************************** |
| 63 | * |
| 64 | * Private static data |
| 65 | * |
| 66 | ***********************************************************************/ |
| 67 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 68 | static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' }; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 69 | |
| 70 | /*********************************************************************** |
| 71 | * |
| 72 | * Private class method prototypes |
| 73 | * |
| 74 | ***********************************************************************/ |
| 75 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 76 | static void set_defaults_(FLAC__StreamDecoder *decoder); |
| 77 | static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels); |
| 78 | static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id); |
| 79 | static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder); |
| 80 | static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder); |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 81 | static FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length); |
| 82 | static FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length); |
| 83 | static FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj); |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 84 | static FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj); |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 85 | static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder); |
| 86 | static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder); |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 87 | static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode); |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 88 | static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder); |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 89 | static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); |
| 90 | static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); |
| 91 | static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode); |
| 92 | static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode); |
| 93 | static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode); |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 94 | static FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual); |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 95 | static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder); |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 96 | static FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 97 | |
| 98 | /*********************************************************************** |
| 99 | * |
| 100 | * Private class data |
| 101 | * |
| 102 | ***********************************************************************/ |
| 103 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 104 | typedef struct FLAC__StreamDecoderPrivate { |
Josh Coalson | 681c293 | 2002-08-01 08:19:37 +0000 | [diff] [blame] | 105 | FLAC__StreamDecoderReadCallback read_callback; |
| 106 | FLAC__StreamDecoderWriteCallback write_callback; |
| 107 | FLAC__StreamDecoderMetadataCallback metadata_callback; |
| 108 | FLAC__StreamDecoderErrorCallback error_callback; |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 109 | /* generic 32-bit datapath: */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 110 | void (*local_lpc_restore_signal)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 111 | /* generic 64-bit datapath: */ |
Josh Coalson | b6a3219 | 2002-10-04 05:27:49 +0000 | [diff] [blame] | 112 | void (*local_lpc_restore_signal_64bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 113 | /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit): */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 114 | void (*local_lpc_restore_signal_16bit)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 115 | /* for use when the signal is <= 16 bits-per-sample, or <= 15 bits-per-sample on a side channel (which requires 1 extra bit), AND order <= 8: */ |
| 116 | void (*local_lpc_restore_signal_16bit_order8)(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 117 | void *client_data; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 118 | FLAC__BitBuffer *input; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 119 | FLAC__int32 *output[FLAC__MAX_CHANNELS]; |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 120 | FLAC__int32 *residual[FLAC__MAX_CHANNELS]; /* WATCHOUT: these are the aligned pointers; the real pointers that should be free()'d are residual_unaligned[] below */ |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 121 | FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS]; |
Josh Coalson | 1e1509f | 2001-03-23 20:20:43 +0000 | [diff] [blame] | 122 | unsigned output_capacity, output_channels; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 123 | FLAC__uint32 last_frame_number; |
Josh Coalson | ba05608 | 2005-01-24 00:20:49 +0000 | [diff] [blame] | 124 | FLAC__uint32 last_block_size; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 125 | FLAC__uint64 samples_decoded; |
| 126 | FLAC__bool has_stream_info, has_seek_table; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 127 | FLAC__StreamMetadata stream_info; |
| 128 | FLAC__StreamMetadata seek_table; |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 129 | FLAC__bool metadata_filter[128]; /* MAGIC number 128 == total number of metadata block types == 1 << 7 */ |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 130 | FLAC__byte *metadata_filter_ids; |
| 131 | unsigned metadata_filter_ids_count, metadata_filter_ids_capacity; /* units for both are IDs, not bytes */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 132 | FLAC__Frame frame; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 133 | FLAC__bool cached; /* true if there is a byte in lookahead */ |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 134 | FLAC__CPUInfo cpuinfo; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 135 | FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */ |
| 136 | FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */ |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 137 | /* unaligned (original) pointers to allocated data */ |
| 138 | FLAC__int32 *residual_unaligned[FLAC__MAX_CHANNELS]; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 139 | } FLAC__StreamDecoderPrivate; |
| 140 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 141 | /*********************************************************************** |
| 142 | * |
| 143 | * Public static class data |
| 144 | * |
| 145 | ***********************************************************************/ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 146 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 147 | FLAC_API const char * const FLAC__StreamDecoderStateString[] = { |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 148 | "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA", |
| 149 | "FLAC__STREAM_DECODER_READ_METADATA", |
| 150 | "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC", |
| 151 | "FLAC__STREAM_DECODER_READ_FRAME", |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 152 | "FLAC__STREAM_DECODER_END_OF_STREAM", |
| 153 | "FLAC__STREAM_DECODER_ABORTED", |
| 154 | "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM", |
| 155 | "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 156 | "FLAC__STREAM_DECODER_ALREADY_INITIALIZED", |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 157 | "FLAC__STREAM_DECODER_INVALID_CALLBACK", |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 158 | "FLAC__STREAM_DECODER_UNINITIALIZED" |
| 159 | }; |
| 160 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 161 | FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 162 | "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE", |
| 163 | "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM", |
| 164 | "FLAC__STREAM_DECODER_READ_STATUS_ABORT" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 167 | FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 168 | "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE", |
| 169 | "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 172 | FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 173 | "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC", |
| 174 | "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER", |
| 175 | "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 178 | /*********************************************************************** |
| 179 | * |
| 180 | * Class constructor/destructor |
| 181 | * |
| 182 | ***********************************************************************/ |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 183 | FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new() |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 184 | { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 185 | FLAC__StreamDecoder *decoder; |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 186 | unsigned i; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 187 | |
| 188 | FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */ |
| 189 | |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 190 | decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder)); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 191 | if(decoder == 0) { |
| 192 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 193 | } |
Josh Coalson | a7e4fb2 | 2002-08-03 22:05:11 +0000 | [diff] [blame] | 194 | |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 195 | decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected)); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 196 | if(decoder->protected_ == 0) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 197 | free(decoder); |
| 198 | return 0; |
| 199 | } |
Josh Coalson | a7e4fb2 | 2002-08-03 22:05:11 +0000 | [diff] [blame] | 200 | |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 201 | decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate)); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 202 | if(decoder->private_ == 0) { |
| 203 | free(decoder->protected_); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 204 | free(decoder); |
| 205 | return 0; |
| 206 | } |
Josh Coalson | a7e4fb2 | 2002-08-03 22:05:11 +0000 | [diff] [blame] | 207 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 208 | decoder->private_->input = FLAC__bitbuffer_new(); |
| 209 | if(decoder->private_->input == 0) { |
| 210 | free(decoder->private_); |
| 211 | free(decoder->protected_); |
| 212 | free(decoder); |
| 213 | return 0; |
| 214 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 215 | |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 216 | decoder->private_->metadata_filter_ids_capacity = 16; |
Josh Coalson | 5e3fcb2 | 2002-11-06 07:11:26 +0000 | [diff] [blame] | 217 | if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) { |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 218 | FLAC__bitbuffer_delete(decoder->private_->input); |
| 219 | free(decoder->private_); |
| 220 | free(decoder->protected_); |
| 221 | free(decoder); |
| 222 | return 0; |
| 223 | } |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 224 | |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 225 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 226 | decoder->private_->output[i] = 0; |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 227 | decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | decoder->private_->output_capacity = 0; |
| 231 | decoder->private_->output_channels = 0; |
| 232 | decoder->private_->has_seek_table = false; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 233 | |
| 234 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) |
| 235 | FLAC__format_entropy_coding_method_partitioned_rice_contents_init(&decoder->private_->partitioned_rice_contents[i]); |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 236 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 237 | set_defaults_(decoder); |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 238 | |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 239 | decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; |
| 240 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 241 | return decoder; |
| 242 | } |
| 243 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 244 | FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 245 | { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 246 | unsigned i; |
| 247 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 248 | FLAC__ASSERT(0 != decoder); |
| 249 | FLAC__ASSERT(0 != decoder->protected_); |
| 250 | FLAC__ASSERT(0 != decoder->private_); |
| 251 | FLAC__ASSERT(0 != decoder->private_->input); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 252 | |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 253 | FLAC__stream_decoder_finish(decoder); |
| 254 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 255 | if(0 != decoder->private_->metadata_filter_ids) |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 256 | free(decoder->private_->metadata_filter_ids); |
| 257 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 258 | FLAC__bitbuffer_delete(decoder->private_->input); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 259 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 260 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) |
| 261 | FLAC__format_entropy_coding_method_partitioned_rice_contents_clear(&decoder->private_->partitioned_rice_contents[i]); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 262 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 263 | free(decoder->private_); |
| 264 | free(decoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 265 | free(decoder); |
| 266 | } |
| 267 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 268 | /*********************************************************************** |
| 269 | * |
| 270 | * Public class methods |
| 271 | * |
| 272 | ***********************************************************************/ |
| 273 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 274 | FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 275 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 276 | FLAC__ASSERT(0 != decoder); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 277 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 278 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 279 | return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 280 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 281 | if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback) |
| 282 | return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 283 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 284 | if(!FLAC__bitbuffer_init(decoder->private_->input)) |
| 285 | return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 286 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 287 | decoder->private_->last_frame_number = 0; |
Josh Coalson | ba05608 | 2005-01-24 00:20:49 +0000 | [diff] [blame] | 288 | decoder->private_->last_block_size = 0; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 289 | decoder->private_->samples_decoded = 0; |
| 290 | decoder->private_->has_stream_info = false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 291 | decoder->private_->cached = false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 292 | |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 293 | /* |
| 294 | * get the CPU info and set the function pointers |
| 295 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 296 | FLAC__cpu_info(&decoder->private_->cpuinfo); |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 297 | /* first default to the non-asm routines */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 298 | decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal; |
Josh Coalson | b6a3219 | 2002-10-04 05:27:49 +0000 | [diff] [blame] | 299 | decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 300 | decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal; |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 301 | decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal; |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 302 | /* now override with asm where appropriate */ |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 303 | #ifndef FLAC__NO_ASM |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 304 | if(decoder->private_->cpuinfo.use_asm) { |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 305 | #ifdef FLAC__CPU_IA32 |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 306 | FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 307 | #ifdef FLAC__HAS_NASM |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 308 | if(decoder->private_->cpuinfo.data.ia32.mmx) { |
| 309 | decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32; |
| 310 | decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx; |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 311 | decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32_mmx; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 312 | } |
| 313 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 314 | decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32; |
| 315 | decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32; |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 316 | decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ia32; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 317 | } |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 318 | #endif |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 319 | #elif defined FLAC__CPU_PPC |
| 320 | FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_PPC); |
| 321 | if(decoder->private_->cpuinfo.data.ppc.altivec) { |
| 322 | decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ppc_altivec_16; |
| 323 | decoder->private_->local_lpc_restore_signal_16bit_order8 = FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8; |
| 324 | } |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 325 | #endif |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 326 | } |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 327 | #endif |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 328 | |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 329 | if(!FLAC__stream_decoder_reset(decoder)) |
| 330 | return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 331 | |
| 332 | return decoder->protected_->state; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 335 | FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 336 | { |
| 337 | unsigned i; |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 338 | FLAC__ASSERT(0 != decoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 339 | if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 340 | return; |
Josh Coalson | 4fa9059 | 2002-12-04 07:01:37 +0000 | [diff] [blame] | 341 | if(0 != decoder->private_->seek_table.data.seek_table.points) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 342 | free(decoder->private_->seek_table.data.seek_table.points); |
| 343 | decoder->private_->seek_table.data.seek_table.points = 0; |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 344 | decoder->private_->has_seek_table = false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 345 | } |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 346 | FLAC__bitbuffer_free(decoder->private_->input); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 347 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 348 | /* WATCHOUT: |
| 349 | * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the |
| 350 | * output arrays have a buffer of up to 3 zeroes in front |
| 351 | * (at negative indices) for alignment purposes; we use 4 |
| 352 | * to keep the data well-aligned. |
| 353 | */ |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 354 | if(0 != decoder->private_->output[i]) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 355 | free(decoder->private_->output[i]-4); |
| 356 | decoder->private_->output[i] = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 357 | } |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 358 | if(0 != decoder->private_->residual_unaligned[i]) { |
| 359 | free(decoder->private_->residual_unaligned[i]); |
| 360 | decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 363 | decoder->private_->output_capacity = 0; |
| 364 | decoder->private_->output_channels = 0; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 365 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 366 | set_defaults_(decoder); |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 367 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 368 | decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 371 | FLAC_API FLAC__bool FLAC__stream_decoder_set_read_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderReadCallback value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 372 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 373 | FLAC__ASSERT(0 != decoder); |
| 374 | FLAC__ASSERT(0 != decoder->private_); |
| 375 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 376 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 377 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 378 | decoder->private_->read_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 379 | return true; |
| 380 | } |
| 381 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 382 | FLAC_API FLAC__bool FLAC__stream_decoder_set_write_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderWriteCallback value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 383 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 384 | FLAC__ASSERT(0 != decoder); |
| 385 | FLAC__ASSERT(0 != decoder->private_); |
| 386 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 387 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 388 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 389 | decoder->private_->write_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 390 | return true; |
| 391 | } |
| 392 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 393 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderMetadataCallback value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 394 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 395 | FLAC__ASSERT(0 != decoder); |
| 396 | FLAC__ASSERT(0 != decoder->private_); |
| 397 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 398 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 399 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 400 | decoder->private_->metadata_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 401 | return true; |
| 402 | } |
| 403 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 404 | FLAC_API FLAC__bool FLAC__stream_decoder_set_error_callback(FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorCallback value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 405 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 406 | FLAC__ASSERT(0 != decoder); |
| 407 | FLAC__ASSERT(0 != decoder->private_); |
| 408 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 409 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 410 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 411 | decoder->private_->error_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 412 | return true; |
| 413 | } |
| 414 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 415 | FLAC_API FLAC__bool FLAC__stream_decoder_set_client_data(FLAC__StreamDecoder *decoder, void *value) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 416 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 417 | FLAC__ASSERT(0 != decoder); |
| 418 | FLAC__ASSERT(0 != decoder->private_); |
| 419 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 420 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 421 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 422 | decoder->private_->client_data = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 423 | return true; |
| 424 | } |
| 425 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 426 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond(FLAC__StreamDecoder *decoder, FLAC__MetadataType type) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 427 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 428 | FLAC__ASSERT(0 != decoder); |
| 429 | FLAC__ASSERT(0 != decoder->private_); |
| 430 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | 1cb2341 | 2004-07-22 01:32:00 +0000 | [diff] [blame] | 431 | FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE); |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 432 | /* double protection */ |
Josh Coalson | 1cb2341 | 2004-07-22 01:32:00 +0000 | [diff] [blame] | 433 | if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE) |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 434 | return false; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 435 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 436 | return false; |
| 437 | decoder->private_->metadata_filter[type] = true; |
| 438 | if(type == FLAC__METADATA_TYPE_APPLICATION) |
| 439 | decoder->private_->metadata_filter_ids_count = 0; |
| 440 | return true; |
| 441 | } |
| 442 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 443 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 444 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 445 | FLAC__ASSERT(0 != decoder); |
| 446 | FLAC__ASSERT(0 != decoder->private_); |
| 447 | FLAC__ASSERT(0 != decoder->protected_); |
| 448 | FLAC__ASSERT(0 != id); |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 449 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 450 | return false; |
| 451 | |
| 452 | if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) |
| 453 | return true; |
| 454 | |
| 455 | FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); |
| 456 | |
| 457 | if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { |
Josh Coalson | d926496 | 2005-09-03 01:41:11 +0000 | [diff] [blame] | 458 | if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) { |
| 459 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 460 | return false; |
| 461 | } |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 462 | decoder->private_->metadata_filter_ids_capacity *= 2; |
| 463 | } |
| 464 | |
| 465 | memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)); |
| 466 | decoder->private_->metadata_filter_ids_count++; |
| 467 | |
| 468 | return true; |
| 469 | } |
| 470 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 471 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_respond_all(FLAC__StreamDecoder *decoder) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 472 | { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 473 | unsigned i; |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 474 | FLAC__ASSERT(0 != decoder); |
| 475 | FLAC__ASSERT(0 != decoder->private_); |
| 476 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 477 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 478 | return false; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 479 | for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++) |
| 480 | decoder->private_->metadata_filter[i] = true; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 481 | decoder->private_->metadata_filter_ids_count = 0; |
| 482 | return true; |
| 483 | } |
| 484 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 485 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore(FLAC__StreamDecoder *decoder, FLAC__MetadataType type) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 486 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 487 | FLAC__ASSERT(0 != decoder); |
| 488 | FLAC__ASSERT(0 != decoder->private_); |
| 489 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | 1cb2341 | 2004-07-22 01:32:00 +0000 | [diff] [blame] | 490 | FLAC__ASSERT((unsigned)type <= FLAC__MAX_METADATA_TYPE_CODE); |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 491 | /* double protection */ |
Josh Coalson | 1cb2341 | 2004-07-22 01:32:00 +0000 | [diff] [blame] | 492 | if((unsigned)type > FLAC__MAX_METADATA_TYPE_CODE) |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 493 | return false; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 494 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 495 | return false; |
| 496 | decoder->private_->metadata_filter[type] = false; |
| 497 | if(type == FLAC__METADATA_TYPE_APPLICATION) |
| 498 | decoder->private_->metadata_filter_ids_count = 0; |
| 499 | return true; |
| 500 | } |
| 501 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 502 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_application(FLAC__StreamDecoder *decoder, const FLAC__byte id[4]) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 503 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 504 | FLAC__ASSERT(0 != decoder); |
| 505 | FLAC__ASSERT(0 != decoder->private_); |
| 506 | FLAC__ASSERT(0 != decoder->protected_); |
| 507 | FLAC__ASSERT(0 != id); |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 508 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 509 | return false; |
| 510 | |
| 511 | if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) |
| 512 | return true; |
| 513 | |
| 514 | FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); |
| 515 | |
| 516 | if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { |
Josh Coalson | d926496 | 2005-09-03 01:41:11 +0000 | [diff] [blame] | 517 | if(0 == (decoder->private_->metadata_filter_ids = (FLAC__byte*)realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) { |
| 518 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 519 | return false; |
| 520 | } |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 521 | decoder->private_->metadata_filter_ids_capacity *= 2; |
| 522 | } |
| 523 | |
| 524 | memcpy(decoder->private_->metadata_filter_ids + decoder->private_->metadata_filter_ids_count * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8)); |
| 525 | decoder->private_->metadata_filter_ids_count++; |
| 526 | |
| 527 | return true; |
| 528 | } |
| 529 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 530 | FLAC_API FLAC__bool FLAC__stream_decoder_set_metadata_ignore_all(FLAC__StreamDecoder *decoder) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 531 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 532 | FLAC__ASSERT(0 != decoder); |
| 533 | FLAC__ASSERT(0 != decoder->private_); |
| 534 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 535 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 536 | return false; |
| 537 | memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); |
| 538 | decoder->private_->metadata_filter_ids_count = 0; |
| 539 | return true; |
| 540 | } |
| 541 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 542 | FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_get_state(const FLAC__StreamDecoder *decoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 543 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 544 | FLAC__ASSERT(0 != decoder); |
| 545 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 546 | return decoder->protected_->state; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Josh Coalson | d36b346 | 2003-09-24 22:08:00 +0000 | [diff] [blame] | 549 | FLAC_API const char *FLAC__stream_decoder_get_resolved_state_string(const FLAC__StreamDecoder *decoder) |
| 550 | { |
| 551 | return FLAC__StreamDecoderStateString[decoder->protected_->state]; |
| 552 | } |
| 553 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 554 | FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 555 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 556 | FLAC__ASSERT(0 != decoder); |
| 557 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 558 | return decoder->protected_->channels; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 561 | FLAC_API FLAC__ChannelAssignment FLAC__stream_decoder_get_channel_assignment(const FLAC__StreamDecoder *decoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 562 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 563 | FLAC__ASSERT(0 != decoder); |
| 564 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 565 | return decoder->protected_->channel_assignment; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 568 | FLAC_API unsigned FLAC__stream_decoder_get_bits_per_sample(const FLAC__StreamDecoder *decoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 569 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 570 | FLAC__ASSERT(0 != decoder); |
| 571 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 572 | return decoder->protected_->bits_per_sample; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 575 | FLAC_API unsigned FLAC__stream_decoder_get_sample_rate(const FLAC__StreamDecoder *decoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 576 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 577 | FLAC__ASSERT(0 != decoder); |
| 578 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 579 | return decoder->protected_->sample_rate; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 582 | FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 583 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 584 | FLAC__ASSERT(0 != decoder); |
| 585 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 586 | return decoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 589 | FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 590 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 591 | FLAC__ASSERT(0 != decoder); |
| 592 | FLAC__ASSERT(0 != decoder->private_); |
| 593 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 594 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 595 | if(!FLAC__bitbuffer_clear(decoder->private_->input)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 596 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 597 | return false; |
| 598 | } |
Josh Coalson | ba05608 | 2005-01-24 00:20:49 +0000 | [diff] [blame] | 599 | decoder->private_->last_frame_number = 0; |
| 600 | decoder->private_->last_block_size = 0; |
Josh Coalson | b312f28 | 2001-11-01 05:23:48 +0000 | [diff] [blame] | 601 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 602 | |
| 603 | return true; |
| 604 | } |
| 605 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 606 | FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 607 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 608 | FLAC__ASSERT(0 != decoder); |
| 609 | FLAC__ASSERT(0 != decoder->private_); |
| 610 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 611 | |
| 612 | if(!FLAC__stream_decoder_flush(decoder)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 613 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 614 | return false; |
| 615 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 616 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 617 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 618 | decoder->private_->samples_decoded = 0; |
Josh Coalson | 671fadb | 2001-04-17 17:32:01 +0000 | [diff] [blame] | 619 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 620 | return true; |
| 621 | } |
| 622 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 623 | FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 624 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 625 | FLAC__bool got_a_frame; |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 626 | FLAC__ASSERT(0 != decoder); |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 627 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 628 | |
| 629 | while(1) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 630 | switch(decoder->protected_->state) { |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 631 | case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 632 | if(!find_metadata_(decoder)) |
| 633 | return false; /* above function sets the status for us */ |
| 634 | break; |
| 635 | case FLAC__STREAM_DECODER_READ_METADATA: |
| 636 | if(!read_metadata_(decoder)) |
| 637 | return false; /* above function sets the status for us */ |
| 638 | else |
| 639 | return true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 640 | case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 641 | if(!frame_sync_(decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 642 | return true; /* above function sets the status for us */ |
| 643 | break; |
| 644 | case FLAC__STREAM_DECODER_READ_FRAME: |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 645 | if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/true)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 646 | return false; /* above function sets the status for us */ |
| 647 | if(got_a_frame) |
| 648 | return true; /* above function sets the status for us */ |
| 649 | break; |
| 650 | case FLAC__STREAM_DECODER_END_OF_STREAM: |
Josh Coalson | 93df7e0 | 2002-07-24 06:08:37 +0000 | [diff] [blame] | 651 | case FLAC__STREAM_DECODER_ABORTED: |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 652 | return true; |
| 653 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 654 | FLAC__ASSERT(0); |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 655 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | } |
| 659 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 660 | FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_metadata(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 661 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 662 | FLAC__ASSERT(0 != decoder); |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 663 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 664 | |
| 665 | while(1) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 666 | switch(decoder->protected_->state) { |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 667 | case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 668 | if(!find_metadata_(decoder)) |
| 669 | return false; /* above function sets the status for us */ |
| 670 | break; |
| 671 | case FLAC__STREAM_DECODER_READ_METADATA: |
| 672 | if(!read_metadata_(decoder)) |
| 673 | return false; /* above function sets the status for us */ |
| 674 | break; |
| 675 | case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
| 676 | case FLAC__STREAM_DECODER_READ_FRAME: |
| 677 | case FLAC__STREAM_DECODER_END_OF_STREAM: |
| 678 | case FLAC__STREAM_DECODER_ABORTED: |
| 679 | return true; |
| 680 | default: |
| 681 | FLAC__ASSERT(0); |
| 682 | return false; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 687 | FLAC_API FLAC__bool FLAC__stream_decoder_process_until_end_of_stream(FLAC__StreamDecoder *decoder) |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 688 | { |
| 689 | FLAC__bool dummy; |
| 690 | FLAC__ASSERT(0 != decoder); |
| 691 | FLAC__ASSERT(0 != decoder->protected_); |
| 692 | |
| 693 | while(1) { |
| 694 | switch(decoder->protected_->state) { |
| 695 | case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 696 | if(!find_metadata_(decoder)) |
| 697 | return false; /* above function sets the status for us */ |
| 698 | break; |
| 699 | case FLAC__STREAM_DECODER_READ_METADATA: |
| 700 | if(!read_metadata_(decoder)) |
| 701 | return false; /* above function sets the status for us */ |
| 702 | break; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 703 | case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 704 | if(!frame_sync_(decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 705 | return true; /* above function sets the status for us */ |
| 706 | break; |
| 707 | case FLAC__STREAM_DECODER_READ_FRAME: |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 708 | if(!read_frame_(decoder, &dummy, /*do_full_decode=*/true)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 709 | return false; /* above function sets the status for us */ |
| 710 | break; |
| 711 | case FLAC__STREAM_DECODER_END_OF_STREAM: |
Josh Coalson | 93df7e0 | 2002-07-24 06:08:37 +0000 | [diff] [blame] | 712 | case FLAC__STREAM_DECODER_ABORTED: |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 713 | return true; |
| 714 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 715 | FLAC__ASSERT(0); |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 716 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |
| 720 | |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 721 | FLAC_API FLAC__bool FLAC__stream_decoder_skip_single_frame(FLAC__StreamDecoder *decoder) |
| 722 | { |
| 723 | FLAC__bool got_a_frame; |
| 724 | FLAC__ASSERT(0 != decoder); |
| 725 | FLAC__ASSERT(0 != decoder->protected_); |
| 726 | |
| 727 | while(1) { |
| 728 | switch(decoder->protected_->state) { |
| 729 | case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 730 | case FLAC__STREAM_DECODER_READ_METADATA: |
| 731 | return false; /* above function sets the status for us */ |
| 732 | case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
| 733 | if(!frame_sync_(decoder)) |
| 734 | return true; /* above function sets the status for us */ |
| 735 | break; |
| 736 | case FLAC__STREAM_DECODER_READ_FRAME: |
| 737 | if(!read_frame_(decoder, &got_a_frame, /*do_full_decode=*/false)) |
| 738 | return false; /* above function sets the status for us */ |
| 739 | if(got_a_frame) |
| 740 | return true; /* above function sets the status for us */ |
| 741 | break; |
| 742 | case FLAC__STREAM_DECODER_END_OF_STREAM: |
| 743 | case FLAC__STREAM_DECODER_ABORTED: |
| 744 | return true; |
| 745 | default: |
| 746 | FLAC__ASSERT(0); |
| 747 | return false; |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 752 | /*********************************************************************** |
| 753 | * |
| 754 | * Protected class methods |
| 755 | * |
| 756 | ***********************************************************************/ |
| 757 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 758 | unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 759 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 760 | FLAC__ASSERT(0 != decoder); |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 761 | return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 764 | /*********************************************************************** |
| 765 | * |
| 766 | * Private class methods |
| 767 | * |
| 768 | ***********************************************************************/ |
| 769 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 770 | void set_defaults_(FLAC__StreamDecoder *decoder) |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 771 | { |
| 772 | decoder->private_->read_callback = 0; |
| 773 | decoder->private_->write_callback = 0; |
| 774 | decoder->private_->metadata_callback = 0; |
| 775 | decoder->private_->error_callback = 0; |
| 776 | decoder->private_->client_data = 0; |
| 777 | |
| 778 | memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); |
| 779 | decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true; |
| 780 | decoder->private_->metadata_filter_ids_count = 0; |
| 781 | } |
| 782 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 783 | FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 784 | { |
| 785 | unsigned i; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 786 | FLAC__int32 *tmp; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 787 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 788 | if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 789 | return true; |
| 790 | |
Josh Coalson | 5dcb57d | 2002-09-04 07:59:02 +0000 | [diff] [blame] | 791 | /* simply using realloc() is not practical because the number of channels may change mid-stream */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 792 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 793 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 794 | if(0 != decoder->private_->output[i]) { |
Josh Coalson | 2c718a2 | 2002-10-21 06:43:48 +0000 | [diff] [blame] | 795 | free(decoder->private_->output[i]-4); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 796 | decoder->private_->output[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 797 | } |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 798 | if(0 != decoder->private_->residual_unaligned[i]) { |
| 799 | free(decoder->private_->residual_unaligned[i]); |
| 800 | decoder->private_->residual_unaligned[i] = decoder->private_->residual[i] = 0; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 801 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Josh Coalson | 1e1509f | 2001-03-23 20:20:43 +0000 | [diff] [blame] | 804 | for(i = 0; i < channels; i++) { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 805 | /* WATCHOUT: |
| 806 | * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the |
| 807 | * output arrays have a buffer of up to 3 zeroes in front |
| 808 | * (at negative indices) for alignment purposes; we use 4 |
| 809 | * to keep the data well-aligned. |
| 810 | */ |
Josh Coalson | 63d9957 | 2001-07-12 21:26:57 +0000 | [diff] [blame] | 811 | tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4)); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 812 | if(tmp == 0) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 813 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 814 | return false; |
| 815 | } |
Josh Coalson | 63d9957 | 2001-07-12 21:26:57 +0000 | [diff] [blame] | 816 | memset(tmp, 0, sizeof(FLAC__int32)*4); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 817 | decoder->private_->output[i] = tmp + 4; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 818 | |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 819 | /* WATCHOUT: |
| 820 | * minimum of quadword alignment for PPC vector optimizations is REQUIRED: |
| 821 | */ |
| 822 | if(!FLAC__memory_alloc_aligned_int32_array(size, &decoder->private_->residual_unaligned[i], &decoder->private_->residual[i])) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 823 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 824 | return false; |
| 825 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 826 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 827 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 828 | decoder->private_->output_capacity = size; |
| 829 | decoder->private_->output_channels = channels; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 830 | |
| 831 | return true; |
| 832 | } |
| 833 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 834 | FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 835 | { |
| 836 | unsigned i; |
| 837 | |
| 838 | FLAC__ASSERT(0 != decoder); |
| 839 | FLAC__ASSERT(0 != decoder->private_); |
| 840 | |
| 841 | for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++) |
| 842 | if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))) |
| 843 | return true; |
| 844 | |
| 845 | return false; |
| 846 | } |
| 847 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 848 | FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 849 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 850 | FLAC__uint32 x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 851 | unsigned i, id; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 852 | FLAC__bool first = true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 853 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 854 | FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 855 | |
| 856 | for(i = id = 0; i < 4; ) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 857 | if(decoder->private_->cached) { |
| 858 | x = (FLAC__uint32)decoder->private_->lookahead; |
| 859 | decoder->private_->cached = false; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 860 | } |
| 861 | else { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 862 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 863 | return false; /* the read_callback_ sets the state for us */ |
| 864 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 865 | if(x == FLAC__STREAM_SYNC_STRING[i]) { |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 866 | first = true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 867 | i++; |
| 868 | id = 0; |
| 869 | continue; |
| 870 | } |
| 871 | if(x == ID3V2_TAG_[id]) { |
| 872 | id++; |
| 873 | i = 0; |
| 874 | if(id == 3) { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 875 | if(!skip_id3v2_tag_(decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 876 | return false; /* the read_callback_ sets the state for us */ |
| 877 | } |
| 878 | continue; |
| 879 | } |
| 880 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 881 | decoder->private_->header_warmup[0] = (FLAC__byte)x; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 882 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 883 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 884 | |
| 885 | /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */ |
| 886 | /* else we have to check if the second byte is the end of a sync code */ |
| 887 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 888 | decoder->private_->lookahead = (FLAC__byte)x; |
| 889 | decoder->private_->cached = true; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 890 | } |
| 891 | else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 892 | decoder->private_->header_warmup[1] = (FLAC__byte)x; |
| 893 | decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 894 | return true; |
| 895 | } |
| 896 | } |
| 897 | i = 0; |
| 898 | if(first) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 899 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 900 | first = false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 901 | } |
| 902 | } |
| 903 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 904 | decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 905 | return true; |
| 906 | } |
| 907 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 908 | FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 909 | { |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 910 | FLAC__bool is_last; |
| 911 | FLAC__uint32 i, x, type, length; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 912 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 913 | FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 914 | |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 915 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 916 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 917 | is_last = x? true : false; |
| 918 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 919 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &type, FLAC__STREAM_METADATA_TYPE_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 920 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 921 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 922 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &length, FLAC__STREAM_METADATA_LENGTH_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 923 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 924 | |
Josh Coalson | 7298392 | 2001-02-23 21:03:21 +0000 | [diff] [blame] | 925 | if(type == FLAC__METADATA_TYPE_STREAMINFO) { |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 926 | if(!read_metadata_streaminfo_(decoder, is_last, length)) |
| 927 | return false; |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 928 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 929 | decoder->private_->has_stream_info = true; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 930 | if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO]) |
| 931 | decoder->private_->metadata_callback(decoder, &decoder->private_->stream_info, decoder->private_->client_data); |
Josh Coalson | c076ba5 | 2001-04-05 21:32:54 +0000 | [diff] [blame] | 932 | } |
| 933 | else if(type == FLAC__METADATA_TYPE_SEEKTABLE) { |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 934 | if(!read_metadata_seektable_(decoder, is_last, length)) |
Josh Coalson | c076ba5 | 2001-04-05 21:32:54 +0000 | [diff] [blame] | 935 | return false; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 936 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 937 | decoder->private_->has_seek_table = true; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 938 | if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE]) |
| 939 | decoder->private_->metadata_callback(decoder, &decoder->private_->seek_table, decoder->private_->client_data); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 940 | } |
| 941 | else { |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 942 | FLAC__bool skip_it = !decoder->private_->metadata_filter[type]; |
| 943 | unsigned real_length = length; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 944 | FLAC__StreamMetadata block; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 945 | |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 946 | block.is_last = is_last; |
Josh Coalson | 5e3fcb2 | 2002-11-06 07:11:26 +0000 | [diff] [blame] | 947 | block.type = (FLAC__MetadataType)type; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 948 | block.length = length; |
| 949 | |
Josh Coalson | 8400e43 | 2002-05-09 05:41:35 +0000 | [diff] [blame] | 950 | if(type == FLAC__METADATA_TYPE_APPLICATION) { |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 951 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.id, FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 952 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 953 | |
| 954 | real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8; |
| 955 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 956 | if(decoder->private_->metadata_filter_ids_count > 0 && has_id_filtered_(decoder, block.data.application.id)) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 957 | skip_it = !skip_it; |
| 958 | } |
| 959 | |
| 960 | if(skip_it) { |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 961 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder)) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 962 | return false; /* the read_callback_ sets the state for us */ |
| 963 | } |
| 964 | else { |
| 965 | switch(type) { |
| 966 | case FLAC__METADATA_TYPE_PADDING: |
| 967 | /* skip the padding bytes */ |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 968 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, real_length, read_callback_, decoder)) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 969 | return false; /* the read_callback_ sets the state for us */ |
| 970 | break; |
| 971 | case FLAC__METADATA_TYPE_APPLICATION: |
| 972 | /* remember, we read the ID already */ |
Josh Coalson | bda92f2 | 2002-05-31 06:24:03 +0000 | [diff] [blame] | 973 | if(real_length > 0) { |
Josh Coalson | 5e3fcb2 | 2002-11-06 07:11:26 +0000 | [diff] [blame] | 974 | if(0 == (block.data.application.data = (FLAC__byte*)malloc(real_length))) { |
Josh Coalson | bda92f2 | 2002-05-31 06:24:03 +0000 | [diff] [blame] | 975 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 976 | return false; |
| 977 | } |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 978 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder)) |
Josh Coalson | bda92f2 | 2002-05-31 06:24:03 +0000 | [diff] [blame] | 979 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 980 | } |
Josh Coalson | bda92f2 | 2002-05-31 06:24:03 +0000 | [diff] [blame] | 981 | else |
| 982 | block.data.application.data = 0; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 983 | break; |
| 984 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 985 | if(!read_metadata_vorbiscomment_(decoder, &block.data.vorbis_comment)) |
| 986 | return false; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 987 | break; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 988 | case FLAC__METADATA_TYPE_CUESHEET: |
| 989 | if(!read_metadata_cuesheet_(decoder, &block.data.cue_sheet)) |
| 990 | return false; |
| 991 | break; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 992 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 993 | case FLAC__METADATA_TYPE_SEEKTABLE: |
Josh Coalson | fb48f24 | 2003-01-11 06:25:42 +0000 | [diff] [blame] | 994 | FLAC__ASSERT(0); |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 995 | break; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 996 | default: |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 997 | if(real_length > 0) { |
| 998 | if(0 == (block.data.unknown.data = (FLAC__byte*)malloc(real_length))) { |
| 999 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1000 | return false; |
| 1001 | } |
| 1002 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, block.data.unknown.data, real_length, read_callback_, decoder)) |
| 1003 | return false; /* the read_callback_ sets the state for us */ |
| 1004 | } |
| 1005 | else |
| 1006 | block.data.unknown.data = 0; |
| 1007 | break; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 1008 | } |
| 1009 | decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data); |
| 1010 | |
| 1011 | /* now we have to free any malloc'ed data in the block */ |
| 1012 | switch(type) { |
| 1013 | case FLAC__METADATA_TYPE_PADDING: |
| 1014 | break; |
| 1015 | case FLAC__METADATA_TYPE_APPLICATION: |
| 1016 | if(0 != block.data.application.data) |
| 1017 | free(block.data.application.data); |
| 1018 | break; |
| 1019 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 1020 | if(0 != block.data.vorbis_comment.vendor_string.entry) |
| 1021 | free(block.data.vorbis_comment.vendor_string.entry); |
| 1022 | if(block.data.vorbis_comment.num_comments > 0) |
| 1023 | for(i = 0; i < block.data.vorbis_comment.num_comments; i++) |
| 1024 | if(0 != block.data.vorbis_comment.comments[i].entry) |
| 1025 | free(block.data.vorbis_comment.comments[i].entry); |
| 1026 | if(0 != block.data.vorbis_comment.comments) |
| 1027 | free(block.data.vorbis_comment.comments); |
| 1028 | break; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1029 | case FLAC__METADATA_TYPE_CUESHEET: |
| 1030 | if(block.data.cue_sheet.num_tracks > 0) |
| 1031 | for(i = 0; i < block.data.cue_sheet.num_tracks; i++) |
| 1032 | if(0 != block.data.cue_sheet.tracks[i].indices) |
| 1033 | free(block.data.cue_sheet.tracks[i].indices); |
| 1034 | if(0 != block.data.cue_sheet.tracks) |
| 1035 | free(block.data.cue_sheet.tracks); |
| 1036 | break; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 1037 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 1038 | case FLAC__METADATA_TYPE_SEEKTABLE: |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 1039 | FLAC__ASSERT(0); |
Josh Coalson | d060947 | 2003-01-10 05:37:13 +0000 | [diff] [blame] | 1040 | default: |
| 1041 | if(0 != block.data.unknown.data) |
| 1042 | free(block.data.unknown.data); |
| 1043 | break; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 1044 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1045 | } |
| 1046 | } |
| 1047 | |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 1048 | if(is_last) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1049 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1050 | |
| 1051 | return true; |
| 1052 | } |
| 1053 | |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 1054 | FLAC__bool read_metadata_streaminfo_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length) |
| 1055 | { |
| 1056 | FLAC__uint32 x; |
| 1057 | unsigned bits, used_bits = 0; |
| 1058 | |
| 1059 | FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); |
| 1060 | |
| 1061 | decoder->private_->stream_info.type = FLAC__METADATA_TYPE_STREAMINFO; |
| 1062 | decoder->private_->stream_info.is_last = is_last; |
| 1063 | decoder->private_->stream_info.length = length; |
| 1064 | |
| 1065 | bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; |
| 1066 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, bits, read_callback_, decoder)) |
| 1067 | return false; /* the read_callback_ sets the state for us */ |
| 1068 | decoder->private_->stream_info.data.stream_info.min_blocksize = x; |
| 1069 | used_bits += bits; |
| 1070 | |
| 1071 | bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; |
| 1072 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder)) |
| 1073 | return false; /* the read_callback_ sets the state for us */ |
| 1074 | decoder->private_->stream_info.data.stream_info.max_blocksize = x; |
| 1075 | used_bits += bits; |
| 1076 | |
| 1077 | bits = FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; |
| 1078 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder)) |
| 1079 | return false; /* the read_callback_ sets the state for us */ |
| 1080 | decoder->private_->stream_info.data.stream_info.min_framesize = x; |
| 1081 | used_bits += bits; |
| 1082 | |
| 1083 | bits = FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; |
| 1084 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder)) |
| 1085 | return false; /* the read_callback_ sets the state for us */ |
| 1086 | decoder->private_->stream_info.data.stream_info.max_framesize = x; |
| 1087 | used_bits += bits; |
| 1088 | |
| 1089 | bits = FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; |
| 1090 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder)) |
| 1091 | return false; /* the read_callback_ sets the state for us */ |
| 1092 | decoder->private_->stream_info.data.stream_info.sample_rate = x; |
| 1093 | used_bits += bits; |
| 1094 | |
| 1095 | bits = FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; |
| 1096 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder)) |
| 1097 | return false; /* the read_callback_ sets the state for us */ |
| 1098 | decoder->private_->stream_info.data.stream_info.channels = x+1; |
| 1099 | used_bits += bits; |
| 1100 | |
| 1101 | bits = FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; |
| 1102 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder)) |
| 1103 | return false; /* the read_callback_ sets the state for us */ |
| 1104 | decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1; |
| 1105 | used_bits += bits; |
| 1106 | |
| 1107 | bits = FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; |
| 1108 | if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &decoder->private_->stream_info.data.stream_info.total_samples, FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN, read_callback_, decoder)) |
| 1109 | return false; /* the read_callback_ sets the state for us */ |
| 1110 | used_bits += bits; |
| 1111 | |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1112 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder)) |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 1113 | return false; /* the read_callback_ sets the state for us */ |
| 1114 | used_bits += 16*8; |
| 1115 | |
| 1116 | /* skip the rest of the block */ |
| 1117 | FLAC__ASSERT(used_bits % 8 == 0); |
| 1118 | length -= (used_bits / 8); |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1119 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder)) |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 1120 | return false; /* the read_callback_ sets the state for us */ |
| 1121 | |
| 1122 | return true; |
| 1123 | } |
| 1124 | |
| 1125 | FLAC__bool read_metadata_seektable_(FLAC__StreamDecoder *decoder, FLAC__bool is_last, unsigned length) |
| 1126 | { |
| 1127 | FLAC__uint32 i, x; |
| 1128 | FLAC__uint64 xx; |
| 1129 | |
| 1130 | FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); |
| 1131 | |
| 1132 | decoder->private_->seek_table.type = FLAC__METADATA_TYPE_SEEKTABLE; |
| 1133 | decoder->private_->seek_table.is_last = is_last; |
| 1134 | decoder->private_->seek_table.length = length; |
| 1135 | |
| 1136 | decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; |
| 1137 | |
Josh Coalson | 4fa9059 | 2002-12-04 07:01:37 +0000 | [diff] [blame] | 1138 | /* use realloc since we may pass through here several times (e.g. after seeking) */ |
| 1139 | if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)realloc(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) { |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 1140 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1141 | return false; |
| 1142 | } |
| 1143 | for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) { |
| 1144 | if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder)) |
| 1145 | return false; /* the read_callback_ sets the state for us */ |
| 1146 | decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx; |
| 1147 | |
| 1148 | if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder)) |
| 1149 | return false; /* the read_callback_ sets the state for us */ |
| 1150 | decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx; |
| 1151 | |
| 1152 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder)) |
| 1153 | return false; /* the read_callback_ sets the state for us */ |
| 1154 | decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x; |
| 1155 | } |
| 1156 | length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH); |
| 1157 | /* if there is a partial point left, skip over it */ |
| 1158 | if(length > 0) { |
| 1159 | /*@@@ do an error_callback() here? there's an argument for either way */ |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1160 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, length, read_callback_, decoder)) |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 1161 | return false; /* the read_callback_ sets the state for us */ |
| 1162 | } |
| 1163 | |
| 1164 | return true; |
| 1165 | } |
| 1166 | |
| 1167 | FLAC__bool read_metadata_vorbiscomment_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_VorbisComment *obj) |
| 1168 | { |
| 1169 | FLAC__uint32 i; |
| 1170 | |
| 1171 | FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); |
| 1172 | |
| 1173 | /* read vendor string */ |
| 1174 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); |
| 1175 | if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->vendor_string.length, read_callback_, decoder)) |
| 1176 | return false; /* the read_callback_ sets the state for us */ |
| 1177 | if(obj->vendor_string.length > 0) { |
Josh Coalson | def597e | 2004-12-30 00:59:30 +0000 | [diff] [blame] | 1178 | if(0 == (obj->vendor_string.entry = (FLAC__byte*)malloc(obj->vendor_string.length+1))) { |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 1179 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1180 | return false; |
| 1181 | } |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1182 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->vendor_string.entry, obj->vendor_string.length, read_callback_, decoder)) |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 1183 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | def597e | 2004-12-30 00:59:30 +0000 | [diff] [blame] | 1184 | obj->vendor_string.entry[obj->vendor_string.length] = '\0'; |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 1185 | } |
| 1186 | else |
| 1187 | obj->vendor_string.entry = 0; |
| 1188 | |
| 1189 | /* read num comments */ |
| 1190 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32); |
| 1191 | if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->num_comments, read_callback_, decoder)) |
| 1192 | return false; /* the read_callback_ sets the state for us */ |
| 1193 | |
| 1194 | /* read comments */ |
| 1195 | if(obj->num_comments > 0) { |
| 1196 | if(0 == (obj->comments = (FLAC__StreamMetadata_VorbisComment_Entry*)malloc(obj->num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) { |
| 1197 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1198 | return false; |
| 1199 | } |
| 1200 | for(i = 0; i < obj->num_comments; i++) { |
| 1201 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); |
| 1202 | if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &obj->comments[i].length, read_callback_, decoder)) |
| 1203 | return false; /* the read_callback_ sets the state for us */ |
| 1204 | if(obj->comments[i].length > 0) { |
Josh Coalson | def597e | 2004-12-30 00:59:30 +0000 | [diff] [blame] | 1205 | if(0 == (obj->comments[i].entry = (FLAC__byte*)malloc(obj->comments[i].length+1))) { |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 1206 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1207 | return false; |
| 1208 | } |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1209 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, obj->comments[i].entry, obj->comments[i].length, read_callback_, decoder)) |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 1210 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | def597e | 2004-12-30 00:59:30 +0000 | [diff] [blame] | 1211 | obj->comments[i].entry[obj->comments[i].length] = '\0'; |
Josh Coalson | 5f34956 | 2002-11-14 04:58:42 +0000 | [diff] [blame] | 1212 | } |
| 1213 | else |
| 1214 | obj->comments[i].entry = 0; |
| 1215 | } |
| 1216 | } |
| 1217 | else { |
| 1218 | obj->comments = 0; |
| 1219 | } |
| 1220 | |
| 1221 | return true; |
| 1222 | } |
| 1223 | |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1224 | FLAC__bool read_metadata_cuesheet_(FLAC__StreamDecoder *decoder, FLAC__StreamMetadata_CueSheet *obj) |
| 1225 | { |
| 1226 | FLAC__uint32 i, j, x; |
| 1227 | |
| 1228 | FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); |
| 1229 | |
| 1230 | memset(obj, 0, sizeof(FLAC__StreamMetadata_CueSheet)); |
| 1231 | |
| 1232 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0); |
Josh Coalson | 27e1354 | 2003-05-20 00:01:49 +0000 | [diff] [blame] | 1233 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)obj->media_catalog_number, FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN/8, read_callback_, decoder)) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1234 | return false; /* the read_callback_ sets the state for us */ |
| 1235 | |
| 1236 | if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &obj->lead_in, FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN, read_callback_, decoder)) |
| 1237 | return false; /* the read_callback_ sets the state for us */ |
| 1238 | |
Josh Coalson | 8f0c71b | 2002-12-05 06:37:46 +0000 | [diff] [blame] | 1239 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN, read_callback_, decoder)) |
| 1240 | return false; /* the read_callback_ sets the state for us */ |
| 1241 | obj->is_cd = x? true : false; |
| 1242 | |
Josh Coalson | 38162d5 | 2002-11-16 06:31:54 +0000 | [diff] [blame] | 1243 | if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN, read_callback_, decoder)) |
| 1244 | return false; /* the read_callback_ sets the state for us */ |
| 1245 | |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1246 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN, read_callback_, decoder)) |
| 1247 | return false; /* the read_callback_ sets the state for us */ |
| 1248 | obj->num_tracks = x; |
| 1249 | |
| 1250 | if(obj->num_tracks > 0) { |
| 1251 | if(0 == (obj->tracks = (FLAC__StreamMetadata_CueSheet_Track*)calloc(obj->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track)))) { |
| 1252 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1253 | return false; |
| 1254 | } |
| 1255 | for(i = 0; i < obj->num_tracks; i++) { |
| 1256 | FLAC__StreamMetadata_CueSheet_Track *track = &obj->tracks[i]; |
| 1257 | if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &track->offset, FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN, read_callback_, decoder)) |
| 1258 | return false; /* the read_callback_ sets the state for us */ |
| 1259 | |
| 1260 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN, read_callback_, decoder)) |
| 1261 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | a0d5454 | 2002-11-15 19:32:57 +0000 | [diff] [blame] | 1262 | track->number = (FLAC__byte)x; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1263 | |
| 1264 | FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0); |
Josh Coalson | 27e1354 | 2003-05-20 00:01:49 +0000 | [diff] [blame] | 1265 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, (FLAC__byte*)track->isrc, FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN/8, read_callback_, decoder)) |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1266 | return false; /* the read_callback_ sets the state for us */ |
| 1267 | |
| 1268 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN, read_callback_, decoder)) |
| 1269 | return false; /* the read_callback_ sets the state for us */ |
| 1270 | track->type = x; |
| 1271 | |
| 1272 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN, read_callback_, decoder)) |
| 1273 | return false; /* the read_callback_ sets the state for us */ |
| 1274 | track->pre_emphasis = x; |
| 1275 | |
| 1276 | if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN, read_callback_, decoder)) |
| 1277 | return false; /* the read_callback_ sets the state for us */ |
| 1278 | |
| 1279 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN, read_callback_, decoder)) |
| 1280 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | a0d5454 | 2002-11-15 19:32:57 +0000 | [diff] [blame] | 1281 | track->num_indices = (FLAC__byte)x; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1282 | |
| 1283 | if(track->num_indices > 0) { |
| 1284 | if(0 == (track->indices = (FLAC__StreamMetadata_CueSheet_Index*)calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index)))) { |
| 1285 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1286 | return false; |
| 1287 | } |
| 1288 | for(j = 0; j < track->num_indices; j++) { |
| 1289 | FLAC__StreamMetadata_CueSheet_Index *index = &track->indices[j]; |
| 1290 | if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &index->offset, FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN, read_callback_, decoder)) |
| 1291 | return false; /* the read_callback_ sets the state for us */ |
| 1292 | |
| 1293 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN, read_callback_, decoder)) |
| 1294 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | a0d5454 | 2002-11-15 19:32:57 +0000 | [diff] [blame] | 1295 | index->number = (FLAC__byte)x; |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1296 | |
| 1297 | if(!FLAC__bitbuffer_skip_bits_no_crc(decoder->private_->input, FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN, read_callback_, decoder)) |
| 1298 | return false; /* the read_callback_ sets the state for us */ |
| 1299 | } |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | return true; |
| 1305 | } |
| 1306 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1307 | FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1308 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1309 | FLAC__uint32 x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1310 | unsigned i, skip; |
| 1311 | |
| 1312 | /* skip the version and flags bytes */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1313 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 24, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1314 | return false; /* the read_callback_ sets the state for us */ |
| 1315 | /* get the size (in bytes) to skip */ |
| 1316 | skip = 0; |
| 1317 | for(i = 0; i < 4; i++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1318 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1319 | return false; /* the read_callback_ sets the state for us */ |
| 1320 | skip <<= 7; |
| 1321 | skip |= (x & 0x7f); |
| 1322 | } |
| 1323 | /* skip the rest of the tag */ |
Josh Coalson | e486938 | 2002-11-15 05:41:48 +0000 | [diff] [blame] | 1324 | if(!FLAC__bitbuffer_read_byte_block_aligned_no_crc(decoder->private_->input, 0, skip, read_callback_, decoder)) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 1325 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1326 | return true; |
| 1327 | } |
| 1328 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1329 | FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1330 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1331 | FLAC__uint32 x; |
| 1332 | FLAC__bool first = true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1333 | |
| 1334 | /* If we know the total number of samples in the stream, stop if we've read that many. */ |
| 1335 | /* This will stop us, for example, from wasting time trying to sync on an ID3V1 tag. */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1336 | if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) { |
| 1337 | if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) { |
| 1338 | decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1339 | return true; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | /* make sure we're byte aligned */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1344 | if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) { |
| 1345 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1346 | return false; /* the read_callback_ sets the state for us */ |
| 1347 | } |
| 1348 | |
| 1349 | while(1) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1350 | if(decoder->private_->cached) { |
| 1351 | x = (FLAC__uint32)decoder->private_->lookahead; |
| 1352 | decoder->private_->cached = false; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1353 | } |
| 1354 | else { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1355 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1356 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1357 | } |
| 1358 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1359 | decoder->private_->header_warmup[0] = (FLAC__byte)x; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1360 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1361 | return false; /* the read_callback_ sets the state for us */ |
| 1362 | |
| 1363 | /* we have to check if we just read two 0xff's in a row; the second may actually be the beginning of the sync code */ |
| 1364 | /* else we have to check if the second byte is the end of a sync code */ |
| 1365 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1366 | decoder->private_->lookahead = (FLAC__byte)x; |
| 1367 | decoder->private_->cached = true; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1368 | } |
| 1369 | else if(x >> 2 == 0x3e) { /* MAGIC NUMBER for the last 6 sync bits */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1370 | decoder->private_->header_warmup[1] = (FLAC__byte)x; |
| 1371 | decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1372 | return true; |
| 1373 | } |
| 1374 | } |
| 1375 | if(first) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1376 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); |
Josh Coalson | 38c6854 | 2002-02-12 22:58:59 +0000 | [diff] [blame] | 1377 | first = false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | return true; |
| 1382 | } |
| 1383 | |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1384 | FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame, FLAC__bool do_full_decode) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1385 | { |
| 1386 | unsigned channel; |
| 1387 | unsigned i; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1388 | FLAC__int32 mid, side, left, right; |
| 1389 | FLAC__uint16 frame_crc; /* the one we calculate from the input stream */ |
| 1390 | FLAC__uint32 x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1391 | |
| 1392 | *got_a_frame = false; |
| 1393 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1394 | /* init the CRC */ |
| 1395 | frame_crc = 0; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1396 | FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc); |
| 1397 | FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc); |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1398 | FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc); |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1399 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1400 | if(!read_frame_header_(decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1401 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1402 | if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1403 | return true; |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1404 | if(!allocate_output_(decoder, decoder->private_->frame.header.blocksize, decoder->private_->frame.header.channels)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1405 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1406 | for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1407 | /* |
| 1408 | * first figure the correct bits-per-sample of the subframe |
| 1409 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1410 | unsigned bps = decoder->private_->frame.header.bits_per_sample; |
| 1411 | switch(decoder->private_->frame.header.channel_assignment) { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1412 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 1413 | /* no adjustment needed */ |
| 1414 | break; |
| 1415 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1416 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1417 | if(channel == 1) |
| 1418 | bps++; |
| 1419 | break; |
| 1420 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1421 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1422 | if(channel == 0) |
| 1423 | bps++; |
| 1424 | break; |
| 1425 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1426 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1427 | if(channel == 1) |
| 1428 | bps++; |
| 1429 | break; |
| 1430 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1431 | FLAC__ASSERT(0); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1432 | } |
| 1433 | /* |
| 1434 | * now read it |
| 1435 | */ |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1436 | if(!read_subframe_(decoder, channel, bps, do_full_decode)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1437 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1438 | if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) { |
| 1439 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1440 | return true; |
| 1441 | } |
| 1442 | } |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1443 | if(!read_zero_padding_(decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1444 | return false; |
| 1445 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1446 | /* |
| 1447 | * Read the frame CRC-16 from the footer and check |
| 1448 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1449 | frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input); |
| 1450 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__FRAME_FOOTER_CRC_LEN, read_callback_, decoder)) |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1451 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1452 | if(frame_crc == (FLAC__uint16)x) { |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1453 | if(do_full_decode) { |
| 1454 | /* Undo any special channel coding */ |
| 1455 | switch(decoder->private_->frame.header.channel_assignment) { |
| 1456 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 1457 | /* do nothing */ |
| 1458 | break; |
| 1459 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
| 1460 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 1461 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 1462 | decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i]; |
| 1463 | break; |
| 1464 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
| 1465 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 1466 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 1467 | decoder->private_->output[0][i] += decoder->private_->output[1][i]; |
| 1468 | break; |
| 1469 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
| 1470 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 1471 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { |
| 1472 | mid = decoder->private_->output[0][i]; |
| 1473 | side = decoder->private_->output[1][i]; |
| 1474 | mid <<= 1; |
| 1475 | if(side & 1) /* i.e. if 'side' is odd... */ |
| 1476 | mid++; |
| 1477 | left = mid + side; |
| 1478 | right = mid - side; |
| 1479 | decoder->private_->output[0][i] = left >> 1; |
| 1480 | decoder->private_->output[1][i] = right >> 1; |
| 1481 | } |
| 1482 | break; |
| 1483 | default: |
| 1484 | FLAC__ASSERT(0); |
| 1485 | break; |
| 1486 | } |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1487 | } |
| 1488 | } |
| 1489 | else { |
| 1490 | /* Bad frame, emit error and zero the output signal */ |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1491 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data); |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1492 | if(do_full_decode) { |
| 1493 | for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { |
| 1494 | memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize); |
| 1495 | } |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1496 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| 1499 | *got_a_frame = true; |
| 1500 | |
| 1501 | /* put the latest values into the public section of the decoder instance */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1502 | decoder->protected_->channels = decoder->private_->frame.header.channels; |
| 1503 | decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment; |
| 1504 | decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample; |
| 1505 | decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate; |
| 1506 | decoder->protected_->blocksize = decoder->private_->frame.header.blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1507 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1508 | FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 1509 | decoder->private_->samples_decoded = decoder->private_->frame.header.number.sample_number + decoder->private_->frame.header.blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1510 | |
| 1511 | /* write it */ |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1512 | if(do_full_decode) { |
| 1513 | if(decoder->private_->write_callback(decoder, &decoder->private_->frame, (const FLAC__int32 * const *)decoder->private_->output, decoder->private_->client_data) != FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE) |
| 1514 | return false; |
| 1515 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1516 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1517 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1518 | return true; |
| 1519 | } |
| 1520 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1521 | FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1522 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1523 | FLAC__uint32 x; |
| 1524 | FLAC__uint64 xx; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1525 | unsigned i, blocksize_hint = 0, sample_rate_hint = 0; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1526 | FLAC__byte crc8, raw_header[16]; /* MAGIC NUMBER based on the maximum frame header size, including CRC */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1527 | unsigned raw_header_len; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1528 | FLAC__bool is_unparseable = false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1529 | const FLAC__bool is_known_variable_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize != decoder->private_->stream_info.data.stream_info.max_blocksize); |
| 1530 | const FLAC__bool is_known_fixed_blocksize_stream = (decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1531 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1532 | FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1533 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1534 | /* init the raw header with the saved bits from synchronization */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1535 | raw_header[0] = decoder->private_->header_warmup[0]; |
| 1536 | raw_header[1] = decoder->private_->header_warmup[1]; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1537 | raw_header_len = 2; |
| 1538 | |
| 1539 | /* |
| 1540 | * check to make sure that the reserved bits are 0 |
| 1541 | */ |
| 1542 | if(raw_header[1] & 0x03) { /* MAGIC NUMBER */ |
| 1543 | is_unparseable = true; |
| 1544 | } |
| 1545 | |
| 1546 | /* |
| 1547 | * Note that along the way as we read the header, we look for a sync |
| 1548 | * code inside. If we find one it would indicate that our original |
| 1549 | * sync was bad since there cannot be a sync code in a valid header. |
| 1550 | */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1551 | |
| 1552 | /* |
| 1553 | * read in the raw header as bytes so we can CRC it, and parse it on the way |
| 1554 | */ |
| 1555 | for(i = 0; i < 2; i++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1556 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1557 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1558 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1559 | /* if we get here it means our original sync was erroneous since the sync code cannot appear in the header */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1560 | decoder->private_->lookahead = (FLAC__byte)x; |
| 1561 | decoder->private_->cached = true; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1562 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1563 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1564 | return true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1565 | } |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1566 | raw_header[raw_header_len++] = (FLAC__byte)x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1567 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1568 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1569 | switch(x = raw_header[2] >> 4) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1570 | case 0: |
Josh Coalson | 7531b00 | 2001-04-14 00:24:32 +0000 | [diff] [blame] | 1571 | if(is_known_fixed_blocksize_stream) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1572 | decoder->private_->frame.header.blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1573 | else |
| 1574 | is_unparseable = true; |
| 1575 | break; |
| 1576 | case 1: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1577 | decoder->private_->frame.header.blocksize = 192; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1578 | break; |
| 1579 | case 2: |
| 1580 | case 3: |
| 1581 | case 4: |
| 1582 | case 5: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1583 | decoder->private_->frame.header.blocksize = 576 << (x-2); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1584 | break; |
| 1585 | case 6: |
| 1586 | case 7: |
| 1587 | blocksize_hint = x; |
| 1588 | break; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1589 | case 8: |
| 1590 | case 9: |
| 1591 | case 10: |
| 1592 | case 11: |
| 1593 | case 12: |
| 1594 | case 13: |
| 1595 | case 14: |
| 1596 | case 15: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1597 | decoder->private_->frame.header.blocksize = 256 << (x-8); |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1598 | break; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1599 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1600 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1601 | break; |
| 1602 | } |
| 1603 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1604 | switch(x = raw_header[2] & 0x0f) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1605 | case 0: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1606 | if(decoder->private_->has_stream_info) |
| 1607 | decoder->private_->frame.header.sample_rate = decoder->private_->stream_info.data.stream_info.sample_rate; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1608 | else |
| 1609 | is_unparseable = true; |
| 1610 | break; |
| 1611 | case 1: |
| 1612 | case 2: |
| 1613 | case 3: |
| 1614 | is_unparseable = true; |
| 1615 | break; |
| 1616 | case 4: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1617 | decoder->private_->frame.header.sample_rate = 8000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1618 | break; |
| 1619 | case 5: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1620 | decoder->private_->frame.header.sample_rate = 16000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1621 | break; |
| 1622 | case 6: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1623 | decoder->private_->frame.header.sample_rate = 22050; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1624 | break; |
| 1625 | case 7: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1626 | decoder->private_->frame.header.sample_rate = 24000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1627 | break; |
| 1628 | case 8: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1629 | decoder->private_->frame.header.sample_rate = 32000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1630 | break; |
| 1631 | case 9: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1632 | decoder->private_->frame.header.sample_rate = 44100; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1633 | break; |
| 1634 | case 10: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1635 | decoder->private_->frame.header.sample_rate = 48000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1636 | break; |
| 1637 | case 11: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1638 | decoder->private_->frame.header.sample_rate = 96000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1639 | break; |
| 1640 | case 12: |
| 1641 | case 13: |
| 1642 | case 14: |
| 1643 | sample_rate_hint = x; |
| 1644 | break; |
| 1645 | case 15: |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1646 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1647 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1648 | return true; |
| 1649 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1650 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1653 | x = (unsigned)(raw_header[3] >> 4); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1654 | if(x & 8) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1655 | decoder->private_->frame.header.channels = 2; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1656 | switch(x & 7) { |
| 1657 | case 0: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1658 | decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1659 | break; |
| 1660 | case 1: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1661 | decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1662 | break; |
| 1663 | case 2: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1664 | decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1665 | break; |
| 1666 | default: |
| 1667 | is_unparseable = true; |
| 1668 | break; |
| 1669 | } |
| 1670 | } |
| 1671 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1672 | decoder->private_->frame.header.channels = (unsigned)x + 1; |
| 1673 | decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1676 | switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1677 | case 0: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1678 | if(decoder->private_->has_stream_info) |
| 1679 | decoder->private_->frame.header.bits_per_sample = decoder->private_->stream_info.data.stream_info.bits_per_sample; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1680 | else |
| 1681 | is_unparseable = true; |
| 1682 | break; |
| 1683 | case 1: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1684 | decoder->private_->frame.header.bits_per_sample = 8; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1685 | break; |
| 1686 | case 2: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1687 | decoder->private_->frame.header.bits_per_sample = 12; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1688 | break; |
| 1689 | case 4: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1690 | decoder->private_->frame.header.bits_per_sample = 16; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1691 | break; |
| 1692 | case 5: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1693 | decoder->private_->frame.header.bits_per_sample = 20; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1694 | break; |
| 1695 | case 6: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1696 | decoder->private_->frame.header.bits_per_sample = 24; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1697 | break; |
| 1698 | case 3: |
| 1699 | case 7: |
| 1700 | is_unparseable = true; |
| 1701 | break; |
| 1702 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1703 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1704 | break; |
| 1705 | } |
| 1706 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1707 | if(raw_header[3] & 0x01) { /* this should be a zero padding bit */ |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1708 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1709 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1710 | return true; |
| 1711 | } |
| 1712 | |
Josh Coalson | ba05608 | 2005-01-24 00:20:49 +0000 | [diff] [blame] | 1713 | /* |
| 1714 | * Now we get to the regrettable consequences of not knowing for sure |
| 1715 | * whether we got a frame number or a sample number. There are no |
| 1716 | * encoders that do variable-blocksize encoding so unless we know from |
| 1717 | * the STREAMINFO that it is variable-blocksize we will assume it is |
| 1718 | * fixed-blocksize. The trouble comes when we have no STREAMINFO; again |
| 1719 | * we will guess that is fixed-blocksize. Where this can go wrong: 1) a |
| 1720 | * variable-blocksize stream with no STREAMINFO; 2) a fixed-blocksize |
| 1721 | * stream that was edited such that one or more frames before or |
| 1722 | * including this one do not have the same number of samples as the |
| 1723 | * STREAMINFO's min and max blocksize. |
| 1724 | */ |
| 1725 | if(is_known_variable_blocksize_stream) { |
| 1726 | if(blocksize_hint) { |
| 1727 | if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len)) |
| 1728 | return false; /* the read_callback_ sets the state for us */ |
| 1729 | if(xx == FLAC__U64L(0xffffffffffffffff)) { /* i.e. non-UTF8 code... */ |
| 1730 | decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ |
| 1731 | decoder->private_->cached = true; |
| 1732 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data); |
| 1733 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 1734 | return true; |
| 1735 | } |
| 1736 | decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; |
| 1737 | decoder->private_->frame.header.number.sample_number = xx; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1738 | } |
Josh Coalson | ba05608 | 2005-01-24 00:20:49 +0000 | [diff] [blame] | 1739 | else |
| 1740 | is_unparseable = true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1741 | } |
| 1742 | else { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1743 | if(!FLAC__bitbuffer_read_utf8_uint32(decoder->private_->input, &x, read_callback_, decoder, raw_header, &raw_header_len)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1744 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1745 | if(x == 0xffffffff) { /* i.e. non-UTF8 code... */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1746 | decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ |
| 1747 | decoder->private_->cached = true; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1748 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1749 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1750 | return true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1751 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1752 | decoder->private_->last_frame_number = x; |
Josh Coalson | 2eed0b9 | 2003-11-27 18:15:17 +0000 | [diff] [blame] | 1753 | decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; |
Josh Coalson | ba05608 | 2005-01-24 00:20:49 +0000 | [diff] [blame] | 1754 | if(decoder->private_->has_stream_info) { |
| 1755 | FLAC__ASSERT(decoder->private_->stream_info.data.stream_info.min_blocksize == decoder->private_->stream_info.data.stream_info.max_blocksize); |
| 1756 | decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__uint64)x; |
Josh Coalson | 77d8e7f | 2005-01-30 18:17:11 +0000 | [diff] [blame] | 1757 | decoder->private_->last_block_size = decoder->private_->frame.header.blocksize; |
Josh Coalson | ba05608 | 2005-01-24 00:20:49 +0000 | [diff] [blame] | 1758 | } |
| 1759 | else if(blocksize_hint) { |
| 1760 | if(decoder->private_->last_block_size) |
| 1761 | decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->last_block_size * (FLAC__uint64)x; |
Josh Coalson | 2eed0b9 | 2003-11-27 18:15:17 +0000 | [diff] [blame] | 1762 | else |
| 1763 | is_unparseable = true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1764 | } |
Josh Coalson | ba05608 | 2005-01-24 00:20:49 +0000 | [diff] [blame] | 1765 | else { |
| 1766 | decoder->private_->frame.header.number.sample_number = (FLAC__uint64)decoder->private_->frame.header.blocksize * (FLAC__uint64)x; |
Josh Coalson | 77d8e7f | 2005-01-30 18:17:11 +0000 | [diff] [blame] | 1767 | decoder->private_->last_block_size = decoder->private_->frame.header.blocksize; |
Josh Coalson | ba05608 | 2005-01-24 00:20:49 +0000 | [diff] [blame] | 1768 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | if(blocksize_hint) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1772 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1773 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1774 | raw_header[raw_header_len++] = (FLAC__byte)x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1775 | if(blocksize_hint == 7) { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1776 | FLAC__uint32 _x; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1777 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1778 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1779 | raw_header[raw_header_len++] = (FLAC__byte)_x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1780 | x = (x << 8) | _x; |
| 1781 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1782 | decoder->private_->frame.header.blocksize = x+1; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | if(sample_rate_hint) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1786 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1787 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1788 | raw_header[raw_header_len++] = (FLAC__byte)x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1789 | if(sample_rate_hint != 12) { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1790 | FLAC__uint32 _x; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1791 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &_x, 8, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1792 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1793 | raw_header[raw_header_len++] = (FLAC__byte)_x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1794 | x = (x << 8) | _x; |
| 1795 | } |
| 1796 | if(sample_rate_hint == 12) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1797 | decoder->private_->frame.header.sample_rate = x*1000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1798 | else if(sample_rate_hint == 13) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1799 | decoder->private_->frame.header.sample_rate = x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1800 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1801 | decoder->private_->frame.header.sample_rate = x*10; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1804 | /* read the CRC-8 byte */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1805 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1806 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1807 | crc8 = (FLAC__byte)x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1808 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1809 | if(FLAC__crc8(raw_header, raw_header_len) != crc8) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1810 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER, decoder->private_->client_data); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1811 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1812 | return true; |
| 1813 | } |
| 1814 | |
| 1815 | if(is_unparseable) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1816 | decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1817 | return false; |
| 1818 | } |
| 1819 | |
| 1820 | return true; |
| 1821 | } |
| 1822 | |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1823 | FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1824 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1825 | FLAC__uint32 x; |
| 1826 | FLAC__bool wasted_bits; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1827 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1828 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, 8, read_callback_, decoder)) /* MAGIC NUMBER */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1829 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1830 | |
| 1831 | wasted_bits = (x & 1); |
| 1832 | x &= 0xfe; |
| 1833 | |
| 1834 | if(wasted_bits) { |
| 1835 | unsigned u; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1836 | if(!FLAC__bitbuffer_read_unary_unsigned(decoder->private_->input, &u, read_callback_, decoder)) |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1837 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1838 | decoder->private_->frame.subframes[channel].wasted_bits = u+1; |
| 1839 | bps -= decoder->private_->frame.subframes[channel].wasted_bits; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1840 | } |
| 1841 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1842 | decoder->private_->frame.subframes[channel].wasted_bits = 0; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1843 | |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1844 | /* |
| 1845 | * Lots of magic numbers here |
| 1846 | */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1847 | if(x & 0x80) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1848 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1849 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1850 | return true; |
| 1851 | } |
| 1852 | else if(x == 0) { |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1853 | if(!read_subframe_constant_(decoder, channel, bps, do_full_decode)) |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1854 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1855 | } |
| 1856 | else if(x == 2) { |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1857 | if(!read_subframe_verbatim_(decoder, channel, bps, do_full_decode)) |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1858 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1859 | } |
| 1860 | else if(x < 16) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1861 | decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1862 | return false; |
| 1863 | } |
| 1864 | else if(x <= 24) { |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1865 | if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7, do_full_decode)) |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1866 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1867 | } |
| 1868 | else if(x < 64) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1869 | decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1870 | return false; |
| 1871 | } |
| 1872 | else { |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1873 | if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1, do_full_decode)) |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1874 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1875 | } |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1876 | |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1877 | if(wasted_bits && do_full_decode) { |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1878 | unsigned i; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1879 | x = decoder->private_->frame.subframes[channel].wasted_bits; |
| 1880 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 1881 | decoder->private_->output[channel][i] <<= x; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1882 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1883 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1884 | return true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1887 | FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1888 | { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1889 | FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1890 | FLAC__int32 x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1891 | unsigned i; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1892 | FLAC__int32 *output = decoder->private_->output[channel]; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1893 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1894 | decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1895 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1896 | if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1897 | return false; /* the read_callback_ sets the state for us */ |
| 1898 | |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1899 | subframe->value = x; |
| 1900 | |
| 1901 | /* decode the subframe */ |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1902 | if(do_full_decode) { |
| 1903 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 1904 | output[i] = x; |
| 1905 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1906 | |
| 1907 | return true; |
| 1908 | } |
| 1909 | |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1910 | FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1911 | { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1912 | FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1913 | FLAC__int32 i32; |
| 1914 | FLAC__uint32 u32; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1915 | unsigned u; |
| 1916 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1917 | decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1918 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1919 | subframe->residual = decoder->private_->residual[channel]; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1920 | subframe->order = order; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1921 | |
| 1922 | /* read warm-up samples */ |
| 1923 | for(u = 0; u < order; u++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1924 | if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1925 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1926 | subframe->warmup[u] = i32; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | /* read entropy coding method info */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1930 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1931 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 5e3fcb2 | 2002-11-06 07:11:26 +0000 | [diff] [blame] | 1932 | subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1933 | switch(subframe->entropy_coding_method.type) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1934 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1935 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1936 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1937 | subframe->entropy_coding_method.data.partitioned_rice.order = u32; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 1938 | subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel]; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1939 | break; |
| 1940 | default: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1941 | decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1942 | return false; |
| 1943 | } |
| 1944 | |
| 1945 | /* read residual */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1946 | switch(subframe->entropy_coding_method.type) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1947 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 1948 | if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel])) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1949 | return false; |
| 1950 | break; |
| 1951 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1952 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | /* decode the subframe */ |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1956 | if(do_full_decode) { |
| 1957 | memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order); |
| 1958 | FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order); |
| 1959 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1960 | |
| 1961 | return true; |
| 1962 | } |
| 1963 | |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 1964 | FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order, FLAC__bool do_full_decode) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1965 | { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1966 | FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1967 | FLAC__int32 i32; |
| 1968 | FLAC__uint32 u32; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1969 | unsigned u; |
| 1970 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1971 | decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1972 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1973 | subframe->residual = decoder->private_->residual[channel]; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1974 | subframe->order = order; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1975 | |
| 1976 | /* read warm-up samples */ |
| 1977 | for(u = 0; u < order; u++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1978 | if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, bps, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1979 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1980 | subframe->warmup[u] = i32; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | /* read qlp coeff precision */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1984 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1985 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 3372cc0 | 2001-04-13 18:44:46 +0000 | [diff] [blame] | 1986 | if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1987 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1988 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1989 | return true; |
| 1990 | } |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1991 | subframe->qlp_coeff_precision = u32+1; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1992 | |
| 1993 | /* read qlp shift */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1994 | if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, FLAC__SUBFRAME_LPC_QLP_SHIFT_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1995 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1996 | subframe->quantization_level = i32; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1997 | |
| 1998 | /* read quantized lp coefficiencts */ |
| 1999 | for(u = 0; u < order; u++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2000 | if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i32, subframe->qlp_coeff_precision, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2001 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 2002 | subframe->qlp_coeff[u] = i32; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | /* read entropy coding method info */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2006 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_TYPE_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2007 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 5e3fcb2 | 2002-11-06 07:11:26 +0000 | [diff] [blame] | 2008 | subframe->entropy_coding_method.type = (FLAC__EntropyCodingMethodType)u32; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 2009 | switch(subframe->entropy_coding_method.type) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2010 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2011 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &u32, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ORDER_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2012 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 2013 | subframe->entropy_coding_method.data.partitioned_rice.order = u32; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2014 | subframe->entropy_coding_method.data.partitioned_rice.contents = &decoder->private_->partitioned_rice_contents[channel]; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2015 | break; |
| 2016 | default: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2017 | decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2018 | return false; |
| 2019 | } |
| 2020 | |
| 2021 | /* read residual */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 2022 | switch(subframe->entropy_coding_method.type) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2023 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2024 | if(!read_residual_partitioned_rice_(decoder, order, subframe->entropy_coding_method.data.partitioned_rice.order, &decoder->private_->partitioned_rice_contents[channel], decoder->private_->residual[channel])) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2025 | return false; |
| 2026 | break; |
| 2027 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 2028 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | /* decode the subframe */ |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 2032 | if(do_full_decode) { |
| 2033 | memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order); |
| 2034 | if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32) |
Josh Coalson | 3aadd10 | 2004-07-27 01:13:16 +0000 | [diff] [blame] | 2035 | if(bps <= 16 && subframe->qlp_coeff_precision <= 16) { |
| 2036 | if(order <= 8) |
| 2037 | decoder->private_->local_lpc_restore_signal_16bit_order8(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); |
| 2038 | else |
| 2039 | decoder->private_->local_lpc_restore_signal_16bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); |
| 2040 | } |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 2041 | else |
| 2042 | decoder->private_->local_lpc_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); |
Josh Coalson | fb9d18f | 2002-10-21 07:04:07 +0000 | [diff] [blame] | 2043 | else |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 2044 | decoder->private_->local_lpc_restore_signal_64bit(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, subframe->qlp_coeff, order, subframe->quantization_level, decoder->private_->output[channel]+order); |
| 2045 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2046 | |
| 2047 | return true; |
| 2048 | } |
| 2049 | |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 2050 | FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, FLAC__bool do_full_decode) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2051 | { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2052 | FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim; |
| 2053 | FLAC__int32 x, *residual = decoder->private_->residual[channel]; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2054 | unsigned i; |
| 2055 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2056 | decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 2057 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2058 | subframe->data = residual; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 2059 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2060 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2061 | if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &x, bps, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2062 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 2063 | residual[i] = x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 2066 | /* decode the subframe */ |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 2067 | if(do_full_decode) |
| 2068 | memcpy(decoder->private_->output[channel], subframe->data, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize); |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 2069 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2070 | return true; |
| 2071 | } |
| 2072 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2073 | FLAC__bool read_residual_partitioned_rice_(FLAC__StreamDecoder *decoder, unsigned predictor_order, unsigned partition_order, FLAC__EntropyCodingMethod_PartitionedRiceContents *partitioned_rice_contents, FLAC__int32 *residual) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2074 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2075 | FLAC__uint32 rice_parameter; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2076 | int i; |
| 2077 | unsigned partition, sample, u; |
| 2078 | const unsigned partitions = 1u << partition_order; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2079 | const unsigned partition_samples = partition_order > 0? decoder->private_->frame.header.blocksize >> partition_order : decoder->private_->frame.header.blocksize - predictor_order; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2080 | |
Josh Coalson | 607fc84 | 2004-10-06 00:56:51 +0000 | [diff] [blame] | 2081 | /* sanity checks */ |
| 2082 | if(partition_order == 0) { |
| 2083 | if(decoder->private_->frame.header.blocksize < predictor_order) { |
| 2084 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); |
| 2085 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2086 | return true; |
| 2087 | } |
| 2088 | } |
| 2089 | else { |
| 2090 | if(partition_samples < predictor_order) { |
| 2091 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); |
| 2092 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
| 2093 | return true; |
| 2094 | } |
| 2095 | } |
| 2096 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2097 | if(!FLAC__format_entropy_coding_method_partitioned_rice_contents_ensure_size(partitioned_rice_contents, max(6, partition_order))) { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2098 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 2099 | return false; |
| 2100 | } |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 2101 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2102 | sample = 0; |
| 2103 | for(partition = 0; partition < partitions; partition++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2104 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_PARAMETER_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2105 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2106 | partitioned_rice_contents->parameters[partition] = rice_parameter; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2107 | if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | b28559a | 2002-04-25 05:21:09 +0000 | [diff] [blame] | 2108 | u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order; |
| 2109 | if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder)) |
| 2110 | return false; /* the read_callback_ sets the state for us */ |
| 2111 | sample += u; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2112 | } |
| 2113 | else { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2114 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &rice_parameter, FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_RAW_LEN, read_callback_, decoder)) |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2115 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 2116 | partitioned_rice_contents->raw_bits[partition] = rice_parameter; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2117 | for(u = (partition_order == 0 || partition > 0)? 0 : predictor_order; u < partition_samples; u++, sample++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2118 | if(!FLAC__bitbuffer_read_raw_int32(decoder->private_->input, &i, rice_parameter, read_callback_, decoder)) |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 2119 | return false; /* the read_callback_ sets the state for us */ |
| 2120 | residual[sample] = i; |
| 2121 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | return true; |
| 2126 | } |
| 2127 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 2128 | FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2129 | { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2130 | if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2131 | FLAC__uint32 zero = 0; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2132 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &zero, FLAC__bitbuffer_bits_left_for_byte_alignment(decoder->private_->input), read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2133 | return false; /* the read_callback_ sets the state for us */ |
| 2134 | if(zero != 0) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 2135 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC, decoder->private_->client_data); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2136 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2137 | } |
| 2138 | } |
| 2139 | return true; |
| 2140 | } |
| 2141 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 2142 | FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2143 | { |
| 2144 | FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data; |
| 2145 | FLAC__StreamDecoderReadStatus status; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 2146 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2147 | status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data); |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 2148 | if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2149 | decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 2150 | else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 2151 | decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 2152 | return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 2153 | } |