Josh Coalson | 26560dd | 2001-02-08 00:38:41 +0000 | [diff] [blame] | 1 | /* libFLAC - Free Lossless Audio Codec library |
Josh Coalson | 305ae2e | 2002-01-26 17:36:39 +0000 | [diff] [blame] | 2 | * Copyright (C) 2000,2001,2002 Josh Coalson |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public |
| 15 | * License along with this library; if not, write to the |
| 16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | * Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> /* for malloc() */ |
Josh Coalson | ec1af65 | 2001-04-01 05:34:16 +0000 | [diff] [blame] | 22 | #include <string.h> /* for memset/memcpy() */ |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 23 | #include "FLAC/assert.h" |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 24 | #include "protected/stream_decoder.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 25 | #include "private/bitbuffer.h" |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 26 | #include "private/cpu.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 27 | #include "private/crc.h" |
| 28 | #include "private/fixed.h" |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 29 | #include "private/format.h" |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 30 | #include "private/lpc.h" |
| 31 | |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 32 | #ifdef max |
| 33 | #undef max |
| 34 | #endif |
| 35 | #define max(a,b) ((a)>(b)?(a):(b)) |
| 36 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 37 | /*********************************************************************** |
| 38 | * |
| 39 | * Private static data |
| 40 | * |
| 41 | ***********************************************************************/ |
| 42 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 43 | static FLAC__byte ID3V2_TAG_[3] = { 'I', 'D', '3' }; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 44 | |
| 45 | /*********************************************************************** |
| 46 | * |
| 47 | * Private class method prototypes |
| 48 | * |
| 49 | ***********************************************************************/ |
| 50 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 51 | static void set_defaults_(FLAC__StreamDecoder *decoder); |
| 52 | static FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels); |
| 53 | static FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id); |
| 54 | static FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder); |
| 55 | static FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder); |
| 56 | static FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder); |
| 57 | static FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder); |
| 58 | static FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame); |
| 59 | static FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder); |
| 60 | static FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps); |
| 61 | static FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps); |
| 62 | static FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order); |
| 63 | static FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order); |
| 64 | static FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps); |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 65 | 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] | 66 | static FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder); |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 67 | 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] | 68 | |
| 69 | /*********************************************************************** |
| 70 | * |
| 71 | * Private class data |
| 72 | * |
| 73 | ***********************************************************************/ |
| 74 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 75 | typedef struct FLAC__StreamDecoderPrivate { |
Josh Coalson | 681c293 | 2002-08-01 08:19:37 +0000 | [diff] [blame] | 76 | FLAC__StreamDecoderReadCallback read_callback; |
| 77 | FLAC__StreamDecoderWriteCallback write_callback; |
| 78 | FLAC__StreamDecoderMetadataCallback metadata_callback; |
| 79 | FLAC__StreamDecoderErrorCallback error_callback; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 80 | 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 | b6a3219 | 2002-10-04 05:27:49 +0000 | [diff] [blame] | 81 | 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 | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 82 | 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 | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 83 | void *client_data; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 84 | FLAC__BitBuffer *input; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 85 | FLAC__int32 *output[FLAC__MAX_CHANNELS]; |
| 86 | FLAC__int32 *residual[FLAC__MAX_CHANNELS]; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 87 | FLAC__EntropyCodingMethod_PartitionedRiceContents partitioned_rice_contents[FLAC__MAX_CHANNELS]; |
Josh Coalson | 1e1509f | 2001-03-23 20:20:43 +0000 | [diff] [blame] | 88 | unsigned output_capacity, output_channels; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 89 | FLAC__uint32 last_frame_number; |
| 90 | FLAC__uint64 samples_decoded; |
| 91 | FLAC__bool has_stream_info, has_seek_table; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 92 | FLAC__StreamMetadata stream_info; |
| 93 | FLAC__StreamMetadata seek_table; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 94 | FLAC__bool metadata_filter[FLAC__METADATA_TYPE_VORBIS_COMMENT+1]; |
| 95 | FLAC__byte *metadata_filter_ids; |
| 96 | 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] | 97 | FLAC__Frame frame; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 98 | FLAC__bool cached; /* true if there is a byte in lookahead */ |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 99 | FLAC__CPUInfo cpuinfo; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 100 | FLAC__byte header_warmup[2]; /* contains the sync code and reserved bits */ |
| 101 | FLAC__byte lookahead; /* temp storage when we need to look ahead one byte in the stream */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 102 | } FLAC__StreamDecoderPrivate; |
| 103 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 104 | /*********************************************************************** |
| 105 | * |
| 106 | * Public static class data |
| 107 | * |
| 108 | ***********************************************************************/ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 109 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 110 | FLAC_API const char * const FLAC__StreamDecoderStateString[] = { |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 111 | "FLAC__STREAM_DECODER_SEARCH_FOR_METADATA", |
| 112 | "FLAC__STREAM_DECODER_READ_METADATA", |
| 113 | "FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC", |
| 114 | "FLAC__STREAM_DECODER_READ_FRAME", |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 115 | "FLAC__STREAM_DECODER_END_OF_STREAM", |
| 116 | "FLAC__STREAM_DECODER_ABORTED", |
| 117 | "FLAC__STREAM_DECODER_UNPARSEABLE_STREAM", |
| 118 | "FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR", |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 119 | "FLAC__STREAM_DECODER_ALREADY_INITIALIZED", |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 120 | "FLAC__STREAM_DECODER_INVALID_CALLBACK", |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 121 | "FLAC__STREAM_DECODER_UNINITIALIZED" |
| 122 | }; |
| 123 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 124 | FLAC_API const char * const FLAC__StreamDecoderReadStatusString[] = { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 125 | "FLAC__STREAM_DECODER_READ_STATUS_CONTINUE", |
| 126 | "FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM", |
| 127 | "FLAC__STREAM_DECODER_READ_STATUS_ABORT" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 130 | FLAC_API const char * const FLAC__StreamDecoderWriteStatusString[] = { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 131 | "FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE", |
| 132 | "FLAC__STREAM_DECODER_WRITE_STATUS_ABORT" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 135 | FLAC_API const char * const FLAC__StreamDecoderErrorStatusString[] = { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 136 | "FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC", |
| 137 | "FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER", |
| 138 | "FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH" |
Josh Coalson | cbf595f | 2000-12-22 22:35:33 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 141 | /*********************************************************************** |
| 142 | * |
| 143 | * Class constructor/destructor |
| 144 | * |
| 145 | ***********************************************************************/ |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 146 | FLAC_API FLAC__StreamDecoder *FLAC__stream_decoder_new() |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 147 | { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 148 | FLAC__StreamDecoder *decoder; |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 149 | unsigned i; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 150 | |
| 151 | FLAC__ASSERT(sizeof(int) >= 4); /* we want to die right away if this is not true */ |
| 152 | |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 153 | decoder = (FLAC__StreamDecoder*)calloc(1, sizeof(FLAC__StreamDecoder)); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 154 | if(decoder == 0) { |
| 155 | return 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 156 | } |
Josh Coalson | a7e4fb2 | 2002-08-03 22:05:11 +0000 | [diff] [blame] | 157 | |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 158 | decoder->protected_ = (FLAC__StreamDecoderProtected*)calloc(1, sizeof(FLAC__StreamDecoderProtected)); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 159 | if(decoder->protected_ == 0) { |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 160 | free(decoder); |
| 161 | return 0; |
| 162 | } |
Josh Coalson | a7e4fb2 | 2002-08-03 22:05:11 +0000 | [diff] [blame] | 163 | |
Josh Coalson | ea7155f | 2002-10-18 05:49:19 +0000 | [diff] [blame] | 164 | decoder->private_ = (FLAC__StreamDecoderPrivate*)calloc(1, sizeof(FLAC__StreamDecoderPrivate)); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 165 | if(decoder->private_ == 0) { |
| 166 | free(decoder->protected_); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 167 | free(decoder); |
| 168 | return 0; |
| 169 | } |
Josh Coalson | a7e4fb2 | 2002-08-03 22:05:11 +0000 | [diff] [blame] | 170 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 171 | decoder->private_->input = FLAC__bitbuffer_new(); |
| 172 | if(decoder->private_->input == 0) { |
| 173 | free(decoder->private_); |
| 174 | free(decoder->protected_); |
| 175 | free(decoder); |
| 176 | return 0; |
| 177 | } |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 178 | |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 179 | decoder->private_->metadata_filter_ids_capacity = 16; |
| 180 | if(0 == (decoder->private_->metadata_filter_ids = malloc((FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8) * decoder->private_->metadata_filter_ids_capacity))) { |
| 181 | FLAC__bitbuffer_delete(decoder->private_->input); |
| 182 | free(decoder->private_); |
| 183 | free(decoder->protected_); |
| 184 | free(decoder); |
| 185 | return 0; |
| 186 | } |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 187 | |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 188 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
| 189 | decoder->private_->output[i] = 0; |
| 190 | decoder->private_->residual[i] = 0; |
| 191 | } |
| 192 | |
| 193 | decoder->private_->output_capacity = 0; |
| 194 | decoder->private_->output_channels = 0; |
| 195 | decoder->private_->has_seek_table = false; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 196 | |
| 197 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) |
| 198 | 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] | 199 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 200 | set_defaults_(decoder); |
Josh Coalson | 6b02a75 | 2002-05-10 06:42:02 +0000 | [diff] [blame] | 201 | |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 202 | decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; |
| 203 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 204 | return decoder; |
| 205 | } |
| 206 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 207 | FLAC_API void FLAC__stream_decoder_delete(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 208 | { |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 209 | unsigned i; |
| 210 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 211 | FLAC__ASSERT(0 != decoder); |
| 212 | FLAC__ASSERT(0 != decoder->protected_); |
| 213 | FLAC__ASSERT(0 != decoder->private_); |
| 214 | FLAC__ASSERT(0 != decoder->private_->input); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 215 | |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 216 | FLAC__stream_decoder_finish(decoder); |
| 217 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 218 | if(0 != decoder->private_->metadata_filter_ids) |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 219 | free(decoder->private_->metadata_filter_ids); |
| 220 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 221 | FLAC__bitbuffer_delete(decoder->private_->input); |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 222 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 223 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) |
| 224 | 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] | 225 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 226 | free(decoder->private_); |
| 227 | free(decoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 228 | free(decoder); |
| 229 | } |
| 230 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 231 | /*********************************************************************** |
| 232 | * |
| 233 | * Public class methods |
| 234 | * |
| 235 | ***********************************************************************/ |
| 236 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 237 | FLAC_API FLAC__StreamDecoderState FLAC__stream_decoder_init(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 238 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 239 | FLAC__ASSERT(0 != decoder); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 240 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 241 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 242 | return decoder->protected_->state = FLAC__STREAM_DECODER_ALREADY_INITIALIZED; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 243 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 244 | if(0 == decoder->private_->read_callback || 0 == decoder->private_->write_callback || 0 == decoder->private_->metadata_callback || 0 == decoder->private_->error_callback) |
| 245 | return decoder->protected_->state = FLAC__STREAM_DECODER_INVALID_CALLBACK; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 246 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 247 | if(!FLAC__bitbuffer_init(decoder->private_->input)) |
| 248 | return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 249 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 250 | decoder->private_->last_frame_number = 0; |
| 251 | decoder->private_->samples_decoded = 0; |
| 252 | decoder->private_->has_stream_info = false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 253 | decoder->private_->cached = false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 254 | |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 255 | /* |
| 256 | * get the CPU info and set the function pointers |
| 257 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 258 | FLAC__cpu_info(&decoder->private_->cpuinfo); |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 259 | /* first default to the non-asm routines */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 260 | decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal; |
Josh Coalson | b6a3219 | 2002-10-04 05:27:49 +0000 | [diff] [blame] | 261 | decoder->private_->local_lpc_restore_signal_64bit = FLAC__lpc_restore_signal_wide; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 262 | decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal; |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 263 | /* now override with asm where appropriate */ |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 264 | #ifndef FLAC__NO_ASM |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 265 | if(decoder->private_->cpuinfo.use_asm) { |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 266 | #ifdef FLAC__CPU_IA32 |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 267 | FLAC__ASSERT(decoder->private_->cpuinfo.type == FLAC__CPUINFO_TYPE_IA32); |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 268 | #ifdef FLAC__HAS_NASM |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 269 | if(decoder->private_->cpuinfo.data.ia32.mmx) { |
| 270 | decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32; |
| 271 | decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32_mmx; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 272 | } |
| 273 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 274 | decoder->private_->local_lpc_restore_signal = FLAC__lpc_restore_signal_asm_ia32; |
| 275 | decoder->private_->local_lpc_restore_signal_16bit = FLAC__lpc_restore_signal_asm_ia32; |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 276 | } |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 277 | #endif |
Josh Coalson | 034d38e | 2001-05-24 19:29:30 +0000 | [diff] [blame] | 278 | #endif |
Josh Coalson | 021ad3b | 2001-07-18 00:25:52 +0000 | [diff] [blame] | 279 | } |
Josh Coalson | a3f7c2c | 2001-05-25 00:04:45 +0000 | [diff] [blame] | 280 | #endif |
Josh Coalson | b985bbc | 2001-05-23 21:59:52 +0000 | [diff] [blame] | 281 | |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 282 | if(!FLAC__stream_decoder_reset(decoder)) |
| 283 | return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 284 | |
| 285 | return decoder->protected_->state; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 288 | FLAC_API void FLAC__stream_decoder_finish(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 289 | { |
| 290 | unsigned i; |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 291 | FLAC__ASSERT(0 != decoder); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 292 | if(decoder->protected_->state == FLAC__STREAM_DECODER_UNINITIALIZED) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 293 | return; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 294 | if(decoder->private_->has_seek_table) { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 295 | FLAC__ASSERT(0 != decoder->private_->seek_table.data.seek_table.points); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 296 | free(decoder->private_->seek_table.data.seek_table.points); |
| 297 | decoder->private_->seek_table.data.seek_table.points = 0; |
Josh Coalson | bc86950 | 2002-06-14 06:36:16 +0000 | [diff] [blame] | 298 | decoder->private_->has_seek_table = false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 299 | } |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 300 | FLAC__bitbuffer_free(decoder->private_->input); |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 301 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 302 | /* WATCHOUT: |
| 303 | * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the |
| 304 | * output arrays have a buffer of up to 3 zeroes in front |
| 305 | * (at negative indices) for alignment purposes; we use 4 |
| 306 | * to keep the data well-aligned. |
| 307 | */ |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 308 | if(0 != decoder->private_->output[i]) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 309 | free(decoder->private_->output[i]-4); |
| 310 | decoder->private_->output[i] = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 311 | } |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 312 | if(0 != decoder->private_->residual[i]) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 313 | free(decoder->private_->residual[i]); |
| 314 | decoder->private_->residual[i] = 0; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 317 | decoder->private_->output_capacity = 0; |
| 318 | decoder->private_->output_channels = 0; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 319 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 320 | set_defaults_(decoder); |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 321 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 322 | decoder->protected_->state = FLAC__STREAM_DECODER_UNINITIALIZED; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 325 | 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] | 326 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 327 | FLAC__ASSERT(0 != decoder); |
| 328 | FLAC__ASSERT(0 != decoder->private_); |
| 329 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 330 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 331 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 332 | decoder->private_->read_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 333 | return true; |
| 334 | } |
| 335 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 336 | 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] | 337 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 338 | FLAC__ASSERT(0 != decoder); |
| 339 | FLAC__ASSERT(0 != decoder->private_); |
| 340 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 341 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 342 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 343 | decoder->private_->write_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 344 | return true; |
| 345 | } |
| 346 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 347 | 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] | 348 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 349 | FLAC__ASSERT(0 != decoder); |
| 350 | FLAC__ASSERT(0 != decoder->private_); |
| 351 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 352 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 353 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 354 | decoder->private_->metadata_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 355 | return true; |
| 356 | } |
| 357 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 358 | 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] | 359 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 360 | FLAC__ASSERT(0 != decoder); |
| 361 | FLAC__ASSERT(0 != decoder->private_); |
| 362 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 363 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 364 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 365 | decoder->private_->error_callback = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 366 | return true; |
| 367 | } |
| 368 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 369 | 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] | 370 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 371 | FLAC__ASSERT(0 != decoder); |
| 372 | FLAC__ASSERT(0 != decoder->private_); |
| 373 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 374 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 375 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 376 | decoder->private_->client_data = value; |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 377 | return true; |
| 378 | } |
| 379 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 380 | 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] | 381 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 382 | FLAC__ASSERT(0 != decoder); |
| 383 | FLAC__ASSERT(0 != decoder->private_); |
| 384 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 385 | FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 386 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 387 | return false; |
| 388 | decoder->private_->metadata_filter[type] = true; |
| 389 | if(type == FLAC__METADATA_TYPE_APPLICATION) |
| 390 | decoder->private_->metadata_filter_ids_count = 0; |
| 391 | return true; |
| 392 | } |
| 393 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 394 | 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] | 395 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 396 | FLAC__ASSERT(0 != decoder); |
| 397 | FLAC__ASSERT(0 != decoder->private_); |
| 398 | FLAC__ASSERT(0 != decoder->protected_); |
| 399 | FLAC__ASSERT(0 != id); |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 400 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 401 | return false; |
| 402 | |
| 403 | if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) |
| 404 | return true; |
| 405 | |
| 406 | FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); |
| 407 | |
| 408 | if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { |
| 409 | if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) |
| 410 | return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 411 | decoder->private_->metadata_filter_ids_capacity *= 2; |
| 412 | } |
| 413 | |
| 414 | 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)); |
| 415 | decoder->private_->metadata_filter_ids_count++; |
| 416 | |
| 417 | return true; |
| 418 | } |
| 419 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 420 | 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] | 421 | { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 422 | unsigned i; |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 423 | FLAC__ASSERT(0 != decoder); |
| 424 | FLAC__ASSERT(0 != decoder->private_); |
| 425 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 426 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 427 | return false; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 428 | for(i = 0; i < sizeof(decoder->private_->metadata_filter) / sizeof(decoder->private_->metadata_filter[0]); i++) |
| 429 | decoder->private_->metadata_filter[i] = true; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 430 | decoder->private_->metadata_filter_ids_count = 0; |
| 431 | return true; |
| 432 | } |
| 433 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 434 | 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] | 435 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 436 | FLAC__ASSERT(0 != decoder); |
| 437 | FLAC__ASSERT(0 != decoder->private_); |
| 438 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 439 | FLAC__ASSERT(type <= FLAC__METADATA_TYPE_VORBIS_COMMENT); |
| 440 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 441 | return false; |
| 442 | decoder->private_->metadata_filter[type] = false; |
| 443 | if(type == FLAC__METADATA_TYPE_APPLICATION) |
| 444 | decoder->private_->metadata_filter_ids_count = 0; |
| 445 | return true; |
| 446 | } |
| 447 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 448 | 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] | 449 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 450 | FLAC__ASSERT(0 != decoder); |
| 451 | FLAC__ASSERT(0 != decoder->private_); |
| 452 | FLAC__ASSERT(0 != decoder->protected_); |
| 453 | FLAC__ASSERT(0 != id); |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 454 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 455 | return false; |
| 456 | |
| 457 | if(!decoder->private_->metadata_filter[FLAC__METADATA_TYPE_APPLICATION]) |
| 458 | return true; |
| 459 | |
| 460 | FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids); |
| 461 | |
| 462 | if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) { |
| 463 | if(0 == (decoder->private_->metadata_filter_ids = realloc(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity * 2))) |
| 464 | return decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 465 | decoder->private_->metadata_filter_ids_capacity *= 2; |
| 466 | } |
| 467 | |
| 468 | 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)); |
| 469 | decoder->private_->metadata_filter_ids_count++; |
| 470 | |
| 471 | return true; |
| 472 | } |
| 473 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 474 | 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] | 475 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 476 | FLAC__ASSERT(0 != decoder); |
| 477 | FLAC__ASSERT(0 != decoder->private_); |
| 478 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 479 | if(decoder->protected_->state != FLAC__STREAM_DECODER_UNINITIALIZED) |
| 480 | return false; |
| 481 | memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); |
| 482 | decoder->private_->metadata_filter_ids_count = 0; |
| 483 | return true; |
| 484 | } |
| 485 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 486 | 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] | 487 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 488 | FLAC__ASSERT(0 != decoder); |
| 489 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 490 | return decoder->protected_->state; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 493 | FLAC_API unsigned FLAC__stream_decoder_get_channels(const FLAC__StreamDecoder *decoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 494 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 495 | FLAC__ASSERT(0 != decoder); |
| 496 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 497 | return decoder->protected_->channels; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 500 | 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] | 501 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 502 | FLAC__ASSERT(0 != decoder); |
| 503 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 504 | return decoder->protected_->channel_assignment; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 507 | 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] | 508 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 509 | FLAC__ASSERT(0 != decoder); |
| 510 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 511 | return decoder->protected_->bits_per_sample; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 514 | 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] | 515 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 516 | FLAC__ASSERT(0 != decoder); |
| 517 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 518 | return decoder->protected_->sample_rate; |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 521 | FLAC_API unsigned FLAC__stream_decoder_get_blocksize(const FLAC__StreamDecoder *decoder) |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 522 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 523 | FLAC__ASSERT(0 != decoder); |
| 524 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 525 | return decoder->protected_->blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 528 | FLAC_API FLAC__bool FLAC__stream_decoder_flush(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 529 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 530 | FLAC__ASSERT(0 != decoder); |
| 531 | FLAC__ASSERT(0 != decoder->private_); |
| 532 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 533 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 534 | if(!FLAC__bitbuffer_clear(decoder->private_->input)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 535 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 536 | return false; |
| 537 | } |
Josh Coalson | b312f28 | 2001-11-01 05:23:48 +0000 | [diff] [blame] | 538 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 539 | |
| 540 | return true; |
| 541 | } |
| 542 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 543 | FLAC_API FLAC__bool FLAC__stream_decoder_reset(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 544 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 545 | FLAC__ASSERT(0 != decoder); |
| 546 | FLAC__ASSERT(0 != decoder->private_); |
| 547 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 548 | |
| 549 | if(!FLAC__stream_decoder_flush(decoder)) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 550 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 551 | return false; |
| 552 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 553 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_METADATA; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 554 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 555 | decoder->private_->samples_decoded = 0; |
Josh Coalson | 671fadb | 2001-04-17 17:32:01 +0000 | [diff] [blame] | 556 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 557 | return true; |
| 558 | } |
| 559 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 560 | FLAC_API FLAC__bool FLAC__stream_decoder_process_single(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 561 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 562 | FLAC__bool got_a_frame; |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 563 | FLAC__ASSERT(0 != decoder); |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 564 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 565 | |
| 566 | while(1) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 567 | switch(decoder->protected_->state) { |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 568 | case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 569 | if(!find_metadata_(decoder)) |
| 570 | return false; /* above function sets the status for us */ |
| 571 | break; |
| 572 | case FLAC__STREAM_DECODER_READ_METADATA: |
| 573 | if(!read_metadata_(decoder)) |
| 574 | return false; /* above function sets the status for us */ |
| 575 | else |
| 576 | return true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 577 | case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 578 | if(!frame_sync_(decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 579 | return true; /* above function sets the status for us */ |
| 580 | break; |
| 581 | case FLAC__STREAM_DECODER_READ_FRAME: |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 582 | if(!read_frame_(decoder, &got_a_frame)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 583 | return false; /* above function sets the status for us */ |
| 584 | if(got_a_frame) |
| 585 | return true; /* above function sets the status for us */ |
| 586 | break; |
| 587 | case FLAC__STREAM_DECODER_END_OF_STREAM: |
Josh Coalson | 93df7e0 | 2002-07-24 06:08:37 +0000 | [diff] [blame] | 588 | case FLAC__STREAM_DECODER_ABORTED: |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 589 | return true; |
| 590 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 591 | FLAC__ASSERT(0); |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 592 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 597 | 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] | 598 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 599 | FLAC__ASSERT(0 != decoder); |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 600 | FLAC__ASSERT(0 != decoder->protected_); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 601 | |
| 602 | while(1) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 603 | switch(decoder->protected_->state) { |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 604 | case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 605 | if(!find_metadata_(decoder)) |
| 606 | return false; /* above function sets the status for us */ |
| 607 | break; |
| 608 | case FLAC__STREAM_DECODER_READ_METADATA: |
| 609 | if(!read_metadata_(decoder)) |
| 610 | return false; /* above function sets the status for us */ |
| 611 | break; |
| 612 | case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC: |
| 613 | case FLAC__STREAM_DECODER_READ_FRAME: |
| 614 | case FLAC__STREAM_DECODER_END_OF_STREAM: |
| 615 | case FLAC__STREAM_DECODER_ABORTED: |
| 616 | return true; |
| 617 | default: |
| 618 | FLAC__ASSERT(0); |
| 619 | return false; |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | |
Josh Coalson | 6afed9f | 2002-10-16 22:29:47 +0000 | [diff] [blame] | 624 | 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] | 625 | { |
| 626 | FLAC__bool dummy; |
| 627 | FLAC__ASSERT(0 != decoder); |
| 628 | FLAC__ASSERT(0 != decoder->protected_); |
| 629 | |
| 630 | while(1) { |
| 631 | switch(decoder->protected_->state) { |
| 632 | case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA: |
| 633 | if(!find_metadata_(decoder)) |
| 634 | return false; /* above function sets the status for us */ |
| 635 | break; |
| 636 | case FLAC__STREAM_DECODER_READ_METADATA: |
| 637 | if(!read_metadata_(decoder)) |
| 638 | return false; /* above function sets the status for us */ |
| 639 | break; |
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 | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 645 | if(!read_frame_(decoder, &dummy)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 646 | return false; /* above function sets the status for us */ |
| 647 | break; |
| 648 | case FLAC__STREAM_DECODER_END_OF_STREAM: |
Josh Coalson | 93df7e0 | 2002-07-24 06:08:37 +0000 | [diff] [blame] | 649 | case FLAC__STREAM_DECODER_ABORTED: |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 650 | return true; |
| 651 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 652 | FLAC__ASSERT(0); |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 653 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | } |
| 657 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 658 | /*********************************************************************** |
| 659 | * |
| 660 | * Protected class methods |
| 661 | * |
| 662 | ***********************************************************************/ |
| 663 | |
Josh Coalson | 00e5387 | 2001-06-16 07:32:25 +0000 | [diff] [blame] | 664 | unsigned FLAC__stream_decoder_get_input_bytes_unconsumed(const FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 665 | { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 666 | FLAC__ASSERT(0 != decoder); |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 667 | return FLAC__bitbuffer_get_input_bytes_unconsumed(decoder->private_->input); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Josh Coalson | 0a15c14 | 2001-06-13 17:59:57 +0000 | [diff] [blame] | 670 | /*********************************************************************** |
| 671 | * |
| 672 | * Private class methods |
| 673 | * |
| 674 | ***********************************************************************/ |
| 675 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 676 | void set_defaults_(FLAC__StreamDecoder *decoder) |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 677 | { |
| 678 | decoder->private_->read_callback = 0; |
| 679 | decoder->private_->write_callback = 0; |
| 680 | decoder->private_->metadata_callback = 0; |
| 681 | decoder->private_->error_callback = 0; |
| 682 | decoder->private_->client_data = 0; |
| 683 | |
| 684 | memset(decoder->private_->metadata_filter, 0, sizeof(decoder->private_->metadata_filter)); |
| 685 | decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO] = true; |
| 686 | decoder->private_->metadata_filter_ids_count = 0; |
| 687 | } |
| 688 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 689 | FLAC__bool allocate_output_(FLAC__StreamDecoder *decoder, unsigned size, unsigned channels) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 690 | { |
| 691 | unsigned i; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 692 | FLAC__int32 *tmp; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 693 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 694 | if(size <= decoder->private_->output_capacity && channels <= decoder->private_->output_channels) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 695 | return true; |
| 696 | |
Josh Coalson | 5dcb57d | 2002-09-04 07:59:02 +0000 | [diff] [blame] | 697 | /* 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] | 698 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 699 | for(i = 0; i < FLAC__MAX_CHANNELS; i++) { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 700 | if(0 != decoder->private_->output[i]) { |
Josh Coalson | 2c718a2 | 2002-10-21 06:43:48 +0000 | [diff] [blame] | 701 | free(decoder->private_->output[i]-4); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 702 | decoder->private_->output[i] = 0; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 703 | } |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 704 | if(0 != decoder->private_->residual[i]) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 705 | free(decoder->private_->residual[i]); |
| 706 | decoder->private_->residual[i] = 0; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 707 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Josh Coalson | 1e1509f | 2001-03-23 20:20:43 +0000 | [diff] [blame] | 710 | for(i = 0; i < channels; i++) { |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 711 | /* WATCHOUT: |
| 712 | * FLAC__lpc_restore_signal_asm_ia32_mmx() requires that the |
| 713 | * output arrays have a buffer of up to 3 zeroes in front |
| 714 | * (at negative indices) for alignment purposes; we use 4 |
| 715 | * to keep the data well-aligned. |
| 716 | */ |
Josh Coalson | 63d9957 | 2001-07-12 21:26:57 +0000 | [diff] [blame] | 717 | tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*(size+4)); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 718 | if(tmp == 0) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 719 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 720 | return false; |
| 721 | } |
Josh Coalson | 63d9957 | 2001-07-12 21:26:57 +0000 | [diff] [blame] | 722 | memset(tmp, 0, sizeof(FLAC__int32)*4); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 723 | decoder->private_->output[i] = tmp + 4; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 724 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 725 | tmp = (FLAC__int32*)malloc(sizeof(FLAC__int32)*size); |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 726 | if(tmp == 0) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 727 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 728 | return false; |
| 729 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 730 | decoder->private_->residual[i] = tmp; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 731 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 732 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 733 | decoder->private_->output_capacity = size; |
| 734 | decoder->private_->output_channels = channels; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 735 | |
| 736 | return true; |
| 737 | } |
| 738 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 739 | FLAC__bool has_id_filtered_(FLAC__StreamDecoder *decoder, FLAC__byte *id) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 740 | { |
| 741 | unsigned i; |
| 742 | |
| 743 | FLAC__ASSERT(0 != decoder); |
| 744 | FLAC__ASSERT(0 != decoder->private_); |
| 745 | |
| 746 | for(i = 0; i < decoder->private_->metadata_filter_ids_count; i++) |
| 747 | if(0 == memcmp(decoder->private_->metadata_filter_ids + i * (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8), id, (FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8))) |
| 748 | return true; |
| 749 | |
| 750 | return false; |
| 751 | } |
| 752 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 753 | FLAC__bool find_metadata_(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 754 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 755 | FLAC__uint32 x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 756 | unsigned i, id; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 757 | FLAC__bool first = true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 758 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 759 | FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 760 | |
| 761 | for(i = id = 0; i < 4; ) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 762 | if(decoder->private_->cached) { |
| 763 | x = (FLAC__uint32)decoder->private_->lookahead; |
| 764 | decoder->private_->cached = false; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 765 | } |
| 766 | else { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 767 | 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] | 768 | return false; /* the read_callback_ sets the state for us */ |
| 769 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 770 | if(x == FLAC__STREAM_SYNC_STRING[i]) { |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 771 | first = true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 772 | i++; |
| 773 | id = 0; |
| 774 | continue; |
| 775 | } |
| 776 | if(x == ID3V2_TAG_[id]) { |
| 777 | id++; |
| 778 | i = 0; |
| 779 | if(id == 3) { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 780 | if(!skip_id3v2_tag_(decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 781 | return false; /* the read_callback_ sets the state for us */ |
| 782 | } |
| 783 | continue; |
| 784 | } |
| 785 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 786 | decoder->private_->header_warmup[0] = (FLAC__byte)x; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 787 | 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] | 788 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 789 | |
| 790 | /* 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 */ |
| 791 | /* else we have to check if the second byte is the end of a sync code */ |
| 792 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 793 | decoder->private_->lookahead = (FLAC__byte)x; |
| 794 | decoder->private_->cached = true; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 795 | } |
| 796 | 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] | 797 | decoder->private_->header_warmup[1] = (FLAC__byte)x; |
| 798 | decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 799 | return true; |
| 800 | } |
| 801 | } |
| 802 | i = 0; |
| 803 | if(first) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 804 | 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] | 805 | first = false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 809 | decoder->protected_->state = FLAC__STREAM_DECODER_READ_METADATA; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 810 | return true; |
| 811 | } |
| 812 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 813 | FLAC__bool read_metadata_(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 814 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 815 | FLAC__uint32 i, x, last_block, type, length; |
| 816 | FLAC__uint64 xx; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 817 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 818 | FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 819 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 820 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &last_block, FLAC__STREAM_METADATA_IS_LAST_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 821 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 822 | 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] | 823 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 824 | 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] | 825 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 7298392 | 2001-02-23 21:03:21 +0000 | [diff] [blame] | 826 | if(type == FLAC__METADATA_TYPE_STREAMINFO) { |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 827 | unsigned used_bits = 0; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 828 | decoder->private_->stream_info.type = type; |
| 829 | decoder->private_->stream_info.is_last = last_block; |
| 830 | decoder->private_->stream_info.length = length; |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 831 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 832 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 833 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 834 | decoder->private_->stream_info.data.stream_info.min_blocksize = x; |
Josh Coalson | 7298392 | 2001-02-23 21:03:21 +0000 | [diff] [blame] | 835 | used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_BLOCK_SIZE_LEN; |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 836 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 837 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 838 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 839 | decoder->private_->stream_info.data.stream_info.max_blocksize = x; |
Josh Coalson | 7298392 | 2001-02-23 21:03:21 +0000 | [diff] [blame] | 840 | used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_BLOCK_SIZE_LEN; |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 841 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 842 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 843 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 844 | decoder->private_->stream_info.data.stream_info.min_framesize = x; |
Josh Coalson | 7298392 | 2001-02-23 21:03:21 +0000 | [diff] [blame] | 845 | used_bits += FLAC__STREAM_METADATA_STREAMINFO_MIN_FRAME_SIZE_LEN; |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 846 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 847 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 848 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 849 | decoder->private_->stream_info.data.stream_info.max_framesize = x; |
Josh Coalson | 7298392 | 2001-02-23 21:03:21 +0000 | [diff] [blame] | 850 | used_bits += FLAC__STREAM_METADATA_STREAMINFO_MAX_FRAME_SIZE_LEN; |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 851 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 852 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 853 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 854 | decoder->private_->stream_info.data.stream_info.sample_rate = x; |
Josh Coalson | 7298392 | 2001-02-23 21:03:21 +0000 | [diff] [blame] | 855 | used_bits += FLAC__STREAM_METADATA_STREAMINFO_SAMPLE_RATE_LEN; |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 856 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 857 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 858 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 859 | decoder->private_->stream_info.data.stream_info.channels = x+1; |
Josh Coalson | 7298392 | 2001-02-23 21:03:21 +0000 | [diff] [blame] | 860 | used_bits += FLAC__STREAM_METADATA_STREAMINFO_CHANNELS_LEN; |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 861 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 862 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN, read_callback_, decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 863 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 864 | decoder->private_->stream_info.data.stream_info.bits_per_sample = x+1; |
Josh Coalson | 7298392 | 2001-02-23 21:03:21 +0000 | [diff] [blame] | 865 | used_bits += FLAC__STREAM_METADATA_STREAMINFO_BITS_PER_SAMPLE_LEN; |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 866 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 867 | 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)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 868 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 7298392 | 2001-02-23 21:03:21 +0000 | [diff] [blame] | 869 | used_bits += FLAC__STREAM_METADATA_STREAMINFO_TOTAL_SAMPLES_LEN; |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 870 | |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 871 | if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, decoder->private_->stream_info.data.stream_info.md5sum, 16, read_callback_, decoder)) |
| 872 | return false; /* the read_callback_ sets the state for us */ |
| 873 | used_bits += 16*8; |
Josh Coalson | fa37f1c | 2001-01-12 23:55:11 +0000 | [diff] [blame] | 874 | |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 875 | /* skip the rest of the block */ |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 876 | FLAC__ASSERT(used_bits % 8 == 0); |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 877 | length -= (used_bits / 8); |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 878 | if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder)) |
| 879 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 841e27e | 2001-01-03 00:26:42 +0000 | [diff] [blame] | 880 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 881 | decoder->private_->has_stream_info = true; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 882 | if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_STREAMINFO]) |
| 883 | 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] | 884 | } |
| 885 | else if(type == FLAC__METADATA_TYPE_SEEKTABLE) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 886 | decoder->private_->seek_table.type = type; |
| 887 | decoder->private_->seek_table.is_last = last_block; |
| 888 | decoder->private_->seek_table.length = length; |
Josh Coalson | c076ba5 | 2001-04-05 21:32:54 +0000 | [diff] [blame] | 889 | |
Josh Coalson | f145bbd | 2002-05-04 17:37:51 +0000 | [diff] [blame] | 890 | decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH; |
Josh Coalson | c076ba5 | 2001-04-05 21:32:54 +0000 | [diff] [blame] | 891 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 892 | if(0 == (decoder->private_->seek_table.data.seek_table.points = (FLAC__StreamMetadata_SeekPoint*)malloc(decoder->private_->seek_table.data.seek_table.num_points * sizeof(FLAC__StreamMetadata_SeekPoint)))) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 893 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
Josh Coalson | c076ba5 | 2001-04-05 21:32:54 +0000 | [diff] [blame] | 894 | return false; |
| 895 | } |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 896 | for(i = 0; i < decoder->private_->seek_table.data.seek_table.num_points; i++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 897 | if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_SAMPLE_NUMBER_LEN, read_callback_, decoder)) |
Josh Coalson | c076ba5 | 2001-04-05 21:32:54 +0000 | [diff] [blame] | 898 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 899 | decoder->private_->seek_table.data.seek_table.points[i].sample_number = xx; |
Josh Coalson | c076ba5 | 2001-04-05 21:32:54 +0000 | [diff] [blame] | 900 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 901 | if(!FLAC__bitbuffer_read_raw_uint64(decoder->private_->input, &xx, FLAC__STREAM_METADATA_SEEKPOINT_STREAM_OFFSET_LEN, read_callback_, decoder)) |
Josh Coalson | c076ba5 | 2001-04-05 21:32:54 +0000 | [diff] [blame] | 902 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 903 | decoder->private_->seek_table.data.seek_table.points[i].stream_offset = xx; |
Josh Coalson | c076ba5 | 2001-04-05 21:32:54 +0000 | [diff] [blame] | 904 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 905 | if(!FLAC__bitbuffer_read_raw_uint32(decoder->private_->input, &x, FLAC__STREAM_METADATA_SEEKPOINT_FRAME_SAMPLES_LEN, read_callback_, decoder)) |
Josh Coalson | c076ba5 | 2001-04-05 21:32:54 +0000 | [diff] [blame] | 906 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 907 | decoder->private_->seek_table.data.seek_table.points[i].frame_samples = x; |
Josh Coalson | c076ba5 | 2001-04-05 21:32:54 +0000 | [diff] [blame] | 908 | } |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 909 | length -= (decoder->private_->seek_table.data.seek_table.num_points * FLAC__STREAM_METADATA_SEEKPOINT_LENGTH); |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 910 | /* if there is a partial point left, skip over it */ |
| 911 | if(length > 0) { |
| 912 | /*@@@ do an error_callback() here? there's an argument for either way */ |
| 913 | if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, length, read_callback_, decoder)) |
| 914 | return false; /* the read_callback_ sets the state for us */ |
| 915 | } |
| 916 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 917 | decoder->private_->has_seek_table = true; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 918 | if(decoder->private_->metadata_filter[FLAC__METADATA_TYPE_SEEKTABLE]) |
| 919 | 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] | 920 | } |
| 921 | else { |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 922 | FLAC__bool skip_it = !decoder->private_->metadata_filter[type]; |
| 923 | unsigned real_length = length; |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 924 | FLAC__StreamMetadata block; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 925 | |
| 926 | block.is_last = last_block; |
| 927 | block.type = type; |
| 928 | block.length = length; |
| 929 | |
Josh Coalson | 8400e43 | 2002-05-09 05:41:35 +0000 | [diff] [blame] | 930 | if(type == FLAC__METADATA_TYPE_APPLICATION) { |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 931 | if(!FLAC__bitbuffer_read_byte_block_aligned(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] | 932 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 933 | |
| 934 | real_length -= FLAC__STREAM_METADATA_APPLICATION_ID_LEN/8; |
| 935 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 936 | 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] | 937 | skip_it = !skip_it; |
| 938 | } |
| 939 | |
| 940 | if(skip_it) { |
| 941 | if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder)) |
| 942 | return false; /* the read_callback_ sets the state for us */ |
| 943 | } |
| 944 | else { |
| 945 | switch(type) { |
| 946 | case FLAC__METADATA_TYPE_PADDING: |
| 947 | /* skip the padding bytes */ |
| 948 | if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, real_length, read_callback_, decoder)) |
| 949 | return false; /* the read_callback_ sets the state for us */ |
| 950 | break; |
| 951 | case FLAC__METADATA_TYPE_APPLICATION: |
| 952 | /* remember, we read the ID already */ |
Josh Coalson | bda92f2 | 2002-05-31 06:24:03 +0000 | [diff] [blame] | 953 | if(real_length > 0) { |
| 954 | if(0 == (block.data.application.data = malloc(real_length))) { |
| 955 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 956 | return false; |
| 957 | } |
| 958 | if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.application.data, real_length, read_callback_, decoder)) |
| 959 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 960 | } |
Josh Coalson | bda92f2 | 2002-05-31 06:24:03 +0000 | [diff] [blame] | 961 | else |
| 962 | block.data.application.data = 0; |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 963 | break; |
| 964 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 965 | /* read vendor string */ |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 966 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); |
| 967 | if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.vendor_string.length, read_callback_, decoder)) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 968 | return false; /* the read_callback_ sets the state for us */ |
| 969 | if(block.data.vorbis_comment.vendor_string.length > 0) { |
| 970 | if(0 == (block.data.vorbis_comment.vendor_string.entry = malloc(block.data.vorbis_comment.vendor_string.length))) { |
| 971 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 972 | return false; |
| 973 | } |
| 974 | if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.vorbis_comment.vendor_string.entry, block.data.vorbis_comment.vendor_string.length, read_callback_, decoder)) |
| 975 | return false; /* the read_callback_ sets the state for us */ |
| 976 | } |
| 977 | else |
| 978 | block.data.vorbis_comment.vendor_string.entry = 0; |
| 979 | |
| 980 | /* read num comments */ |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 981 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN == 32); |
| 982 | if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.num_comments, read_callback_, decoder)) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 983 | return false; /* the read_callback_ sets the state for us */ |
| 984 | |
| 985 | /* read comments */ |
| 986 | if(block.data.vorbis_comment.num_comments > 0) { |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 987 | if(0 == (block.data.vorbis_comment.comments = malloc(block.data.vorbis_comment.num_comments * sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) { |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 988 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 989 | return false; |
| 990 | } |
| 991 | for(i = 0; i < block.data.vorbis_comment.num_comments; i++) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 992 | FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN == 32); |
| 993 | if(!FLAC__bitbuffer_read_raw_uint32_little_endian(decoder->private_->input, &block.data.vorbis_comment.comments[i].length, read_callback_, decoder)) |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 994 | return false; /* the read_callback_ sets the state for us */ |
| 995 | if(block.data.vorbis_comment.comments[i].length > 0) { |
| 996 | if(0 == (block.data.vorbis_comment.comments[i].entry = malloc(block.data.vorbis_comment.comments[i].length))) { |
| 997 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 998 | return false; |
| 999 | } |
| 1000 | if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, block.data.vorbis_comment.comments[i].entry, block.data.vorbis_comment.comments[i].length, read_callback_, decoder)) |
| 1001 | return false; /* the read_callback_ sets the state for us */ |
| 1002 | } |
| 1003 | else |
| 1004 | block.data.vorbis_comment.comments[i].entry = 0; |
| 1005 | } |
| 1006 | } |
| 1007 | else { |
| 1008 | block.data.vorbis_comment.comments = 0; |
| 1009 | } |
| 1010 | break; |
| 1011 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 1012 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 1013 | default: |
| 1014 | FLAC__ASSERT(0); |
| 1015 | } |
| 1016 | decoder->private_->metadata_callback(decoder, &block, decoder->private_->client_data); |
| 1017 | |
| 1018 | /* now we have to free any malloc'ed data in the block */ |
| 1019 | switch(type) { |
| 1020 | case FLAC__METADATA_TYPE_PADDING: |
| 1021 | break; |
| 1022 | case FLAC__METADATA_TYPE_APPLICATION: |
| 1023 | if(0 != block.data.application.data) |
| 1024 | free(block.data.application.data); |
| 1025 | break; |
| 1026 | case FLAC__METADATA_TYPE_VORBIS_COMMENT: |
| 1027 | if(0 != block.data.vorbis_comment.vendor_string.entry) |
| 1028 | free(block.data.vorbis_comment.vendor_string.entry); |
| 1029 | if(block.data.vorbis_comment.num_comments > 0) |
| 1030 | for(i = 0; i < block.data.vorbis_comment.num_comments; i++) |
| 1031 | if(0 != block.data.vorbis_comment.comments[i].entry) |
| 1032 | free(block.data.vorbis_comment.comments[i].entry); |
| 1033 | if(0 != block.data.vorbis_comment.comments) |
| 1034 | free(block.data.vorbis_comment.comments); |
| 1035 | break; |
| 1036 | case FLAC__METADATA_TYPE_STREAMINFO: |
| 1037 | case FLAC__METADATA_TYPE_SEEKTABLE: |
| 1038 | default: |
| 1039 | FLAC__ASSERT(0); |
| 1040 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | if(last_block) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1045 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1046 | |
| 1047 | return true; |
| 1048 | } |
| 1049 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1050 | FLAC__bool skip_id3v2_tag_(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1051 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1052 | FLAC__uint32 x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1053 | unsigned i, skip; |
| 1054 | |
| 1055 | /* skip the version and flags bytes */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1056 | 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] | 1057 | return false; /* the read_callback_ sets the state for us */ |
| 1058 | /* get the size (in bytes) to skip */ |
| 1059 | skip = 0; |
| 1060 | for(i = 0; i < 4; i++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1061 | 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] | 1062 | return false; /* the read_callback_ sets the state for us */ |
| 1063 | skip <<= 7; |
| 1064 | skip |= (x & 0x7f); |
| 1065 | } |
| 1066 | /* skip the rest of the tag */ |
Josh Coalson | bf9325f | 2002-05-07 05:30:27 +0000 | [diff] [blame] | 1067 | if(!FLAC__bitbuffer_read_byte_block_aligned(decoder->private_->input, 0, skip, read_callback_, decoder)) |
| 1068 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1069 | return true; |
| 1070 | } |
| 1071 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1072 | FLAC__bool frame_sync_(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1073 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1074 | FLAC__uint32 x; |
| 1075 | FLAC__bool first = true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1076 | |
| 1077 | /* If we know the total number of samples in the stream, stop if we've read that many. */ |
| 1078 | /* 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] | 1079 | if(decoder->private_->has_stream_info && decoder->private_->stream_info.data.stream_info.total_samples) { |
| 1080 | if(decoder->private_->samples_decoded >= decoder->private_->stream_info.data.stream_info.total_samples) { |
| 1081 | decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1082 | return true; |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | /* make sure we're byte aligned */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1087 | if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) { |
| 1088 | 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] | 1089 | return false; /* the read_callback_ sets the state for us */ |
| 1090 | } |
| 1091 | |
| 1092 | while(1) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1093 | if(decoder->private_->cached) { |
| 1094 | x = (FLAC__uint32)decoder->private_->lookahead; |
| 1095 | decoder->private_->cached = false; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1096 | } |
| 1097 | else { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1098 | 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] | 1099 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1100 | } |
| 1101 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1102 | decoder->private_->header_warmup[0] = (FLAC__byte)x; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1103 | 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] | 1104 | return false; /* the read_callback_ sets the state for us */ |
| 1105 | |
| 1106 | /* 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 */ |
| 1107 | /* else we have to check if the second byte is the end of a sync code */ |
| 1108 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1109 | decoder->private_->lookahead = (FLAC__byte)x; |
| 1110 | decoder->private_->cached = true; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1111 | } |
| 1112 | 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] | 1113 | decoder->private_->header_warmup[1] = (FLAC__byte)x; |
| 1114 | decoder->protected_->state = FLAC__STREAM_DECODER_READ_FRAME; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1115 | return true; |
| 1116 | } |
| 1117 | } |
| 1118 | if(first) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1119 | 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] | 1120 | first = false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | return true; |
| 1125 | } |
| 1126 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1127 | FLAC__bool read_frame_(FLAC__StreamDecoder *decoder, FLAC__bool *got_a_frame) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1128 | { |
| 1129 | unsigned channel; |
| 1130 | unsigned i; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1131 | FLAC__int32 mid, side, left, right; |
| 1132 | FLAC__uint16 frame_crc; /* the one we calculate from the input stream */ |
| 1133 | FLAC__uint32 x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1134 | |
| 1135 | *got_a_frame = false; |
| 1136 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1137 | /* init the CRC */ |
| 1138 | frame_crc = 0; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1139 | FLAC__CRC16_UPDATE(decoder->private_->header_warmup[0], frame_crc); |
| 1140 | FLAC__CRC16_UPDATE(decoder->private_->header_warmup[1], frame_crc); |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1141 | FLAC__bitbuffer_reset_read_crc16(decoder->private_->input, frame_crc); |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1142 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1143 | if(!read_frame_header_(decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1144 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1145 | if(decoder->protected_->state == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1146 | return true; |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1147 | 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] | 1148 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1149 | for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1150 | /* |
| 1151 | * first figure the correct bits-per-sample of the subframe |
| 1152 | */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1153 | unsigned bps = decoder->private_->frame.header.bits_per_sample; |
| 1154 | switch(decoder->private_->frame.header.channel_assignment) { |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1155 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 1156 | /* no adjustment needed */ |
| 1157 | break; |
| 1158 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1159 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1160 | if(channel == 1) |
| 1161 | bps++; |
| 1162 | break; |
| 1163 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1164 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1165 | if(channel == 0) |
| 1166 | bps++; |
| 1167 | break; |
| 1168 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1169 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1170 | if(channel == 1) |
| 1171 | bps++; |
| 1172 | break; |
| 1173 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1174 | FLAC__ASSERT(0); |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1175 | } |
| 1176 | /* |
| 1177 | * now read it |
| 1178 | */ |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1179 | if(!read_subframe_(decoder, channel, bps)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1180 | return false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1181 | if(decoder->protected_->state != FLAC__STREAM_DECODER_READ_FRAME) { |
| 1182 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1183 | return true; |
| 1184 | } |
| 1185 | } |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1186 | if(!read_zero_padding_(decoder)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1187 | return false; |
| 1188 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1189 | /* |
| 1190 | * Read the frame CRC-16 from the footer and check |
| 1191 | */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1192 | frame_crc = FLAC__bitbuffer_get_read_crc16(decoder->private_->input); |
| 1193 | 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] | 1194 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1195 | if(frame_crc == (FLAC__uint16)x) { |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1196 | /* Undo any special channel coding */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1197 | switch(decoder->private_->frame.header.channel_assignment) { |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1198 | case FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT: |
| 1199 | /* do nothing */ |
| 1200 | break; |
| 1201 | case FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1202 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 1203 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 1204 | decoder->private_->output[1][i] = decoder->private_->output[0][i] - decoder->private_->output[1][i]; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1205 | break; |
| 1206 | case FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1207 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 1208 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 1209 | decoder->private_->output[0][i] += decoder->private_->output[1][i]; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1210 | break; |
| 1211 | case FLAC__CHANNEL_ASSIGNMENT_MID_SIDE: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1212 | FLAC__ASSERT(decoder->private_->frame.header.channels == 2); |
| 1213 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { |
| 1214 | mid = decoder->private_->output[0][i]; |
| 1215 | side = decoder->private_->output[1][i]; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1216 | mid <<= 1; |
| 1217 | if(side & 1) /* i.e. if 'side' is odd... */ |
| 1218 | mid++; |
| 1219 | left = mid + side; |
| 1220 | right = mid - side; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1221 | decoder->private_->output[0][i] = left >> 1; |
| 1222 | decoder->private_->output[1][i] = right >> 1; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1223 | } |
| 1224 | break; |
| 1225 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1226 | FLAC__ASSERT(0); |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1227 | break; |
| 1228 | } |
| 1229 | } |
| 1230 | else { |
| 1231 | /* Bad frame, emit error and zero the output signal */ |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1232 | decoder->private_->error_callback(decoder, FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH, decoder->private_->client_data); |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1233 | for(channel = 0; channel < decoder->private_->frame.header.channels; channel++) { |
| 1234 | memset(decoder->private_->output[channel], 0, sizeof(FLAC__int32) * decoder->private_->frame.header.blocksize); |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1235 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | *got_a_frame = true; |
| 1239 | |
| 1240 | /* put the latest values into the public section of the decoder instance */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1241 | decoder->protected_->channels = decoder->private_->frame.header.channels; |
| 1242 | decoder->protected_->channel_assignment = decoder->private_->frame.header.channel_assignment; |
| 1243 | decoder->protected_->bits_per_sample = decoder->private_->frame.header.bits_per_sample; |
| 1244 | decoder->protected_->sample_rate = decoder->private_->frame.header.sample_rate; |
| 1245 | decoder->protected_->blocksize = decoder->private_->frame.header.blocksize; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1246 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1247 | FLAC__ASSERT(decoder->private_->frame.header.number_type == FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER); |
| 1248 | 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] | 1249 | |
| 1250 | /* write it */ |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 1251 | 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) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1252 | return false; |
| 1253 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1254 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1255 | return true; |
| 1256 | } |
| 1257 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1258 | FLAC__bool read_frame_header_(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1259 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1260 | FLAC__uint32 x; |
| 1261 | FLAC__uint64 xx; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1262 | unsigned i, blocksize_hint = 0, sample_rate_hint = 0; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1263 | 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] | 1264 | unsigned raw_header_len; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1265 | FLAC__bool is_unparseable = false; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1266 | 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); |
| 1267 | 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] | 1268 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1269 | FLAC__ASSERT(FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1270 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1271 | /* init the raw header with the saved bits from synchronization */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1272 | raw_header[0] = decoder->private_->header_warmup[0]; |
| 1273 | raw_header[1] = decoder->private_->header_warmup[1]; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1274 | raw_header_len = 2; |
| 1275 | |
| 1276 | /* |
| 1277 | * check to make sure that the reserved bits are 0 |
| 1278 | */ |
| 1279 | if(raw_header[1] & 0x03) { /* MAGIC NUMBER */ |
| 1280 | is_unparseable = true; |
| 1281 | } |
| 1282 | |
| 1283 | /* |
| 1284 | * Note that along the way as we read the header, we look for a sync |
| 1285 | * code inside. If we find one it would indicate that our original |
| 1286 | * sync was bad since there cannot be a sync code in a valid header. |
| 1287 | */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1288 | |
| 1289 | /* |
| 1290 | * read in the raw header as bytes so we can CRC it, and parse it on the way |
| 1291 | */ |
| 1292 | for(i = 0; i < 2; i++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1293 | 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] | 1294 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1295 | if(x == 0xff) { /* MAGIC NUMBER for the first 8 frame sync bits */ |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1296 | /* 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] | 1297 | decoder->private_->lookahead = (FLAC__byte)x; |
| 1298 | decoder->private_->cached = true; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1299 | 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] | 1300 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1301 | return true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1302 | } |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1303 | raw_header[raw_header_len++] = (FLAC__byte)x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1304 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1305 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1306 | switch(x = raw_header[2] >> 4) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1307 | case 0: |
Josh Coalson | 7531b00 | 2001-04-14 00:24:32 +0000 | [diff] [blame] | 1308 | if(is_known_fixed_blocksize_stream) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1309 | 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] | 1310 | else |
| 1311 | is_unparseable = true; |
| 1312 | break; |
| 1313 | case 1: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1314 | decoder->private_->frame.header.blocksize = 192; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1315 | break; |
| 1316 | case 2: |
| 1317 | case 3: |
| 1318 | case 4: |
| 1319 | case 5: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1320 | decoder->private_->frame.header.blocksize = 576 << (x-2); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1321 | break; |
| 1322 | case 6: |
| 1323 | case 7: |
| 1324 | blocksize_hint = x; |
| 1325 | break; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1326 | case 8: |
| 1327 | case 9: |
| 1328 | case 10: |
| 1329 | case 11: |
| 1330 | case 12: |
| 1331 | case 13: |
| 1332 | case 14: |
| 1333 | case 15: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1334 | decoder->private_->frame.header.blocksize = 256 << (x-8); |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1335 | break; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1336 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1337 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1338 | break; |
| 1339 | } |
| 1340 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1341 | switch(x = raw_header[2] & 0x0f) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1342 | case 0: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1343 | if(decoder->private_->has_stream_info) |
| 1344 | 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] | 1345 | else |
| 1346 | is_unparseable = true; |
| 1347 | break; |
| 1348 | case 1: |
| 1349 | case 2: |
| 1350 | case 3: |
| 1351 | is_unparseable = true; |
| 1352 | break; |
| 1353 | case 4: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1354 | decoder->private_->frame.header.sample_rate = 8000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1355 | break; |
| 1356 | case 5: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1357 | decoder->private_->frame.header.sample_rate = 16000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1358 | break; |
| 1359 | case 6: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1360 | decoder->private_->frame.header.sample_rate = 22050; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1361 | break; |
| 1362 | case 7: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1363 | decoder->private_->frame.header.sample_rate = 24000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1364 | break; |
| 1365 | case 8: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1366 | decoder->private_->frame.header.sample_rate = 32000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1367 | break; |
| 1368 | case 9: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1369 | decoder->private_->frame.header.sample_rate = 44100; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1370 | break; |
| 1371 | case 10: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1372 | decoder->private_->frame.header.sample_rate = 48000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1373 | break; |
| 1374 | case 11: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1375 | decoder->private_->frame.header.sample_rate = 96000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1376 | break; |
| 1377 | case 12: |
| 1378 | case 13: |
| 1379 | case 14: |
| 1380 | sample_rate_hint = x; |
| 1381 | break; |
| 1382 | case 15: |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1383 | 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] | 1384 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1385 | return true; |
| 1386 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1387 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1390 | x = (unsigned)(raw_header[3] >> 4); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1391 | if(x & 8) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1392 | decoder->private_->frame.header.channels = 2; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1393 | switch(x & 7) { |
| 1394 | case 0: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1395 | decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1396 | break; |
| 1397 | case 1: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1398 | decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1399 | break; |
| 1400 | case 2: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1401 | decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1402 | break; |
| 1403 | default: |
| 1404 | is_unparseable = true; |
| 1405 | break; |
| 1406 | } |
| 1407 | } |
| 1408 | else { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1409 | decoder->private_->frame.header.channels = (unsigned)x + 1; |
| 1410 | decoder->private_->frame.header.channel_assignment = FLAC__CHANNEL_ASSIGNMENT_INDEPENDENT; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1413 | switch(x = (unsigned)(raw_header[3] & 0x0e) >> 1) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1414 | case 0: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1415 | if(decoder->private_->has_stream_info) |
| 1416 | 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] | 1417 | else |
| 1418 | is_unparseable = true; |
| 1419 | break; |
| 1420 | case 1: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1421 | decoder->private_->frame.header.bits_per_sample = 8; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1422 | break; |
| 1423 | case 2: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1424 | decoder->private_->frame.header.bits_per_sample = 12; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1425 | break; |
| 1426 | case 4: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1427 | decoder->private_->frame.header.bits_per_sample = 16; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1428 | break; |
| 1429 | case 5: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1430 | decoder->private_->frame.header.bits_per_sample = 20; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1431 | break; |
| 1432 | case 6: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1433 | decoder->private_->frame.header.bits_per_sample = 24; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1434 | break; |
| 1435 | case 3: |
| 1436 | case 7: |
| 1437 | is_unparseable = true; |
| 1438 | break; |
| 1439 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1440 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1441 | break; |
| 1442 | } |
| 1443 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1444 | if(raw_header[3] & 0x01) { /* this should be a zero padding bit */ |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1445 | 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] | 1446 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1447 | return true; |
| 1448 | } |
| 1449 | |
Josh Coalson | 7531b00 | 2001-04-14 00:24:32 +0000 | [diff] [blame] | 1450 | if(blocksize_hint && is_known_variable_blocksize_stream) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1451 | if(!FLAC__bitbuffer_read_utf8_uint64(decoder->private_->input, &xx, read_callback_, decoder, raw_header, &raw_header_len)) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1452 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1453 | if(xx == 0xffffffffffffffff) { /* i.e. non-UTF8 code... */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1454 | decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ |
| 1455 | decoder->private_->cached = true; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1456 | 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] | 1457 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1458 | return true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1459 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1460 | decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; |
| 1461 | decoder->private_->frame.header.number.sample_number = xx; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1462 | } |
| 1463 | else { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1464 | 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] | 1465 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1466 | if(x == 0xffffffff) { /* i.e. non-UTF8 code... */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1467 | decoder->private_->lookahead = raw_header[raw_header_len-1]; /* back up as much as we can */ |
| 1468 | decoder->private_->cached = true; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1469 | 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] | 1470 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1471 | return true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1472 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1473 | decoder->private_->last_frame_number = x; |
| 1474 | if(decoder->private_->has_stream_info) { |
| 1475 | decoder->private_->frame.header.number_type = FLAC__FRAME_NUMBER_TYPE_SAMPLE_NUMBER; |
| 1476 | decoder->private_->frame.header.number.sample_number = (FLAC__int64)decoder->private_->stream_info.data.stream_info.min_blocksize * (FLAC__int64)x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1477 | } |
| 1478 | else { |
| 1479 | is_unparseable = true; |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | if(blocksize_hint) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1484 | 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] | 1485 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1486 | raw_header[raw_header_len++] = (FLAC__byte)x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1487 | if(blocksize_hint == 7) { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1488 | FLAC__uint32 _x; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1489 | 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] | 1490 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1491 | raw_header[raw_header_len++] = (FLAC__byte)_x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1492 | x = (x << 8) | _x; |
| 1493 | } |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1494 | decoder->private_->frame.header.blocksize = x+1; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | if(sample_rate_hint) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1498 | 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] | 1499 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1500 | raw_header[raw_header_len++] = (FLAC__byte)x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1501 | if(sample_rate_hint != 12) { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1502 | FLAC__uint32 _x; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1503 | 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] | 1504 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1505 | raw_header[raw_header_len++] = (FLAC__byte)_x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1506 | x = (x << 8) | _x; |
| 1507 | } |
| 1508 | if(sample_rate_hint == 12) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1509 | decoder->private_->frame.header.sample_rate = x*1000; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1510 | else if(sample_rate_hint == 13) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1511 | decoder->private_->frame.header.sample_rate = x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1512 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1513 | decoder->private_->frame.header.sample_rate = x*10; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1516 | /* read the CRC-8 byte */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1517 | 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] | 1518 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1519 | crc8 = (FLAC__byte)x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1520 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1521 | if(FLAC__crc8(raw_header, raw_header_len) != crc8) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1522 | 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] | 1523 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1524 | return true; |
| 1525 | } |
| 1526 | |
| 1527 | if(is_unparseable) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1528 | decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1529 | return false; |
| 1530 | } |
| 1531 | |
| 1532 | return true; |
| 1533 | } |
| 1534 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1535 | FLAC__bool read_subframe_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1536 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1537 | FLAC__uint32 x; |
| 1538 | FLAC__bool wasted_bits; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1539 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1540 | 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] | 1541 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1542 | |
| 1543 | wasted_bits = (x & 1); |
| 1544 | x &= 0xfe; |
| 1545 | |
| 1546 | if(wasted_bits) { |
| 1547 | unsigned u; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1548 | 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] | 1549 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1550 | decoder->private_->frame.subframes[channel].wasted_bits = u+1; |
| 1551 | bps -= decoder->private_->frame.subframes[channel].wasted_bits; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1552 | } |
| 1553 | else |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1554 | decoder->private_->frame.subframes[channel].wasted_bits = 0; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1555 | |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1556 | /* |
| 1557 | * Lots of magic numbers here |
| 1558 | */ |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1559 | if(x & 0x80) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1560 | 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] | 1561 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1562 | return true; |
| 1563 | } |
| 1564 | else if(x == 0) { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1565 | if(!read_subframe_constant_(decoder, channel, bps)) |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1566 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1567 | } |
| 1568 | else if(x == 2) { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1569 | if(!read_subframe_verbatim_(decoder, channel, bps)) |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1570 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1571 | } |
| 1572 | else if(x < 16) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1573 | decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1574 | return false; |
| 1575 | } |
| 1576 | else if(x <= 24) { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1577 | if(!read_subframe_fixed_(decoder, channel, bps, (x>>1)&7)) |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1578 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1579 | } |
| 1580 | else if(x < 64) { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1581 | decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1582 | return false; |
| 1583 | } |
| 1584 | else { |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1585 | if(!read_subframe_lpc_(decoder, channel, bps, ((x>>1)&31)+1)) |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1586 | return false; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1587 | } |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1588 | |
| 1589 | if(wasted_bits) { |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1590 | unsigned i; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1591 | x = decoder->private_->frame.subframes[channel].wasted_bits; |
| 1592 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
| 1593 | decoder->private_->output[channel][i] <<= x; |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1594 | } |
Josh Coalson | 859bc54 | 2001-03-27 22:22:27 +0000 | [diff] [blame] | 1595 | |
Josh Coalson | 215af57 | 2001-03-27 01:15:58 +0000 | [diff] [blame] | 1596 | return true; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1599 | FLAC__bool read_subframe_constant_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1600 | { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1601 | FLAC__Subframe_Constant *subframe = &decoder->private_->frame.subframes[channel].data.constant; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1602 | FLAC__int32 x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1603 | unsigned i; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1604 | FLAC__int32 *output = decoder->private_->output[channel]; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1605 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1606 | decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_CONSTANT; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1607 | |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1608 | 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] | 1609 | return false; /* the read_callback_ sets the state for us */ |
| 1610 | |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1611 | subframe->value = x; |
| 1612 | |
| 1613 | /* decode the subframe */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1614 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1615 | output[i] = x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1616 | |
| 1617 | return true; |
| 1618 | } |
| 1619 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1620 | FLAC__bool read_subframe_fixed_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1621 | { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1622 | FLAC__Subframe_Fixed *subframe = &decoder->private_->frame.subframes[channel].data.fixed; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1623 | FLAC__int32 i32; |
| 1624 | FLAC__uint32 u32; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1625 | unsigned u; |
| 1626 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1627 | decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_FIXED; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1628 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1629 | subframe->residual = decoder->private_->residual[channel]; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1630 | subframe->order = order; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1631 | |
| 1632 | /* read warm-up samples */ |
| 1633 | for(u = 0; u < order; u++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1634 | 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] | 1635 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1636 | subframe->warmup[u] = i32; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
| 1639 | /* read entropy coding method info */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1640 | 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] | 1641 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1642 | subframe->entropy_coding_method.type = u32; |
| 1643 | switch(subframe->entropy_coding_method.type) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1644 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1645 | 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] | 1646 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1647 | subframe->entropy_coding_method.data.partitioned_rice.order = u32; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 1648 | 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] | 1649 | break; |
| 1650 | default: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1651 | decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1652 | return false; |
| 1653 | } |
| 1654 | |
| 1655 | /* read residual */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1656 | switch(subframe->entropy_coding_method.type) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1657 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 1658 | 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] | 1659 | return false; |
| 1660 | break; |
| 1661 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1662 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | /* decode the subframe */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1666 | memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order); |
| 1667 | FLAC__fixed_restore_signal(decoder->private_->residual[channel], decoder->private_->frame.header.blocksize-order, order, decoder->private_->output[channel]+order); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1668 | |
| 1669 | return true; |
| 1670 | } |
| 1671 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1672 | FLAC__bool read_subframe_lpc_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps, const unsigned order) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1673 | { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1674 | FLAC__Subframe_LPC *subframe = &decoder->private_->frame.subframes[channel].data.lpc; |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1675 | FLAC__int32 i32; |
| 1676 | FLAC__uint32 u32; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1677 | unsigned u; |
| 1678 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1679 | decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_LPC; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1680 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1681 | subframe->residual = decoder->private_->residual[channel]; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1682 | subframe->order = order; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1683 | |
| 1684 | /* read warm-up samples */ |
| 1685 | for(u = 0; u < order; u++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1686 | 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] | 1687 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1688 | subframe->warmup[u] = i32; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | /* read qlp coeff precision */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1692 | 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] | 1693 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 3372cc0 | 2001-04-13 18:44:46 +0000 | [diff] [blame] | 1694 | if(u32 == (1u << FLAC__SUBFRAME_LPC_QLP_COEFF_PRECISION_LEN) - 1) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1695 | 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] | 1696 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1697 | return true; |
| 1698 | } |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1699 | subframe->qlp_coeff_precision = u32+1; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1700 | |
| 1701 | /* read qlp shift */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1702 | 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] | 1703 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1704 | subframe->quantization_level = i32; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1705 | |
| 1706 | /* read quantized lp coefficiencts */ |
| 1707 | for(u = 0; u < order; u++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1708 | 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] | 1709 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1710 | subframe->qlp_coeff[u] = i32; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | /* read entropy coding method info */ |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1714 | 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] | 1715 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1716 | subframe->entropy_coding_method.type = u32; |
| 1717 | switch(subframe->entropy_coding_method.type) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1718 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1719 | 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] | 1720 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1721 | subframe->entropy_coding_method.data.partitioned_rice.order = u32; |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 1722 | 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] | 1723 | break; |
| 1724 | default: |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1725 | decoder->protected_->state = FLAC__STREAM_DECODER_UNPARSEABLE_STREAM; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1726 | return false; |
| 1727 | } |
| 1728 | |
| 1729 | /* read residual */ |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1730 | switch(subframe->entropy_coding_method.type) { |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1731 | case FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE: |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 1732 | 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] | 1733 | return false; |
| 1734 | break; |
| 1735 | default: |
Josh Coalson | 1b68982 | 2001-05-31 20:11:02 +0000 | [diff] [blame] | 1736 | FLAC__ASSERT(0); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
| 1739 | /* decode the subframe */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1740 | memcpy(decoder->private_->output[channel], subframe->warmup, sizeof(FLAC__int32) * order); |
Josh Coalson | fb9d18f | 2002-10-21 07:04:07 +0000 | [diff] [blame^] | 1741 | if(bps + subframe->qlp_coeff_precision + FLAC__bitmath_ilog2(order) <= 32) |
| 1742 | if(bps <= 16 && subframe->qlp_coeff_precision <= 16) |
| 1743 | 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); |
| 1744 | else |
| 1745 | 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 | b6a3219 | 2002-10-04 05:27:49 +0000 | [diff] [blame] | 1746 | else |
| 1747 | 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); |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1748 | |
| 1749 | return true; |
| 1750 | } |
| 1751 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1752 | FLAC__bool read_subframe_verbatim_(FLAC__StreamDecoder *decoder, unsigned channel, unsigned bps) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1753 | { |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1754 | FLAC__Subframe_Verbatim *subframe = &decoder->private_->frame.subframes[channel].data.verbatim; |
| 1755 | FLAC__int32 x, *residual = decoder->private_->residual[channel]; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1756 | unsigned i; |
| 1757 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1758 | decoder->private_->frame.subframes[channel].type = FLAC__SUBFRAME_TYPE_VERBATIM; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1759 | |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1760 | subframe->data = residual; |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1761 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1762 | for(i = 0; i < decoder->private_->frame.header.blocksize; i++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1763 | 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] | 1764 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | b5e60e5 | 2001-01-28 09:27:27 +0000 | [diff] [blame] | 1765 | residual[i] = x; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Josh Coalson | 6dcea51 | 2001-01-23 23:07:36 +0000 | [diff] [blame] | 1768 | /* decode the subframe */ |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1769 | 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] | 1770 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1771 | return true; |
| 1772 | } |
| 1773 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 1774 | 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] | 1775 | { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1776 | FLAC__uint32 rice_parameter; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1777 | int i; |
| 1778 | unsigned partition, sample, u; |
| 1779 | const unsigned partitions = 1u << partition_order; |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1780 | 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] | 1781 | |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 1782 | 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] | 1783 | decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR; |
| 1784 | return false; |
| 1785 | } |
Josh Coalson | b7023aa | 2002-08-17 15:23:43 +0000 | [diff] [blame] | 1786 | |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1787 | sample = 0; |
| 1788 | for(partition = 0; partition < partitions; partition++) { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1789 | 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] | 1790 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 1791 | partitioned_rice_contents->parameters[partition] = rice_parameter; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1792 | if(rice_parameter < FLAC__ENTROPY_CODING_METHOD_PARTITIONED_RICE_ESCAPE_PARAMETER) { |
Josh Coalson | bb6712e | 2001-04-24 22:54:07 +0000 | [diff] [blame] | 1793 | #ifdef FLAC__SYMMETRIC_RICE |
Josh Coalson | b28559a | 2002-04-25 05:21:09 +0000 | [diff] [blame] | 1794 | 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] | 1795 | if(!FLAC__bitbuffer_read_symmetric_rice_signed(decoder->private_->input, &i, rice_parameter, read_callback_, decoder)) |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1796 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1797 | residual[sample] = i; |
| 1798 | } |
Josh Coalson | b28559a | 2002-04-25 05:21:09 +0000 | [diff] [blame] | 1799 | #else |
| 1800 | u = (partition_order == 0 || partition > 0)? partition_samples : partition_samples - predictor_order; |
| 1801 | if(!FLAC__bitbuffer_read_rice_signed_block(decoder->private_->input, residual + sample, u, rice_parameter, read_callback_, decoder)) |
| 1802 | return false; /* the read_callback_ sets the state for us */ |
| 1803 | sample += u; |
| 1804 | #endif |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1805 | } |
| 1806 | else { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1807 | 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] | 1808 | return false; /* the read_callback_ sets the state for us */ |
Josh Coalson | a37ba46 | 2002-08-19 21:36:39 +0000 | [diff] [blame] | 1809 | partitioned_rice_contents->raw_bits[partition] = rice_parameter; |
Josh Coalson | 2051dd4 | 2001-04-12 22:22:34 +0000 | [diff] [blame] | 1810 | 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] | 1811 | 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] | 1812 | return false; /* the read_callback_ sets the state for us */ |
| 1813 | residual[sample] = i; |
| 1814 | } |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | return true; |
| 1819 | } |
| 1820 | |
Josh Coalson | 570db86 | 2002-07-31 06:58:16 +0000 | [diff] [blame] | 1821 | FLAC__bool read_zero_padding_(FLAC__StreamDecoder *decoder) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1822 | { |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1823 | if(!FLAC__bitbuffer_is_consumed_byte_aligned(decoder->private_->input)) { |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1824 | FLAC__uint32 zero = 0; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1825 | 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] | 1826 | return false; /* the read_callback_ sets the state for us */ |
| 1827 | if(zero != 0) { |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1828 | 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] | 1829 | decoder->protected_->state = FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | return true; |
| 1833 | } |
| 1834 | |
Josh Coalson | 77e3f31 | 2001-06-23 03:03:24 +0000 | [diff] [blame] | 1835 | FLAC__bool read_callback_(FLAC__byte buffer[], unsigned *bytes, void *client_data) |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1836 | { |
| 1837 | FLAC__StreamDecoder *decoder = (FLAC__StreamDecoder *)client_data; |
| 1838 | FLAC__StreamDecoderReadStatus status; |
Josh Coalson | aec256b | 2002-03-12 16:19:54 +0000 | [diff] [blame] | 1839 | |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1840 | status = decoder->private_->read_callback(decoder, buffer, bytes, decoder->private_->client_data); |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1841 | if(status == FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1842 | decoder->protected_->state = FLAC__STREAM_DECODER_END_OF_STREAM; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1843 | else if(status == FLAC__STREAM_DECODER_READ_STATUS_ABORT) |
Josh Coalson | fa697a9 | 2001-08-16 20:07:29 +0000 | [diff] [blame] | 1844 | decoder->protected_->state = FLAC__STREAM_DECODER_ABORTED; |
Josh Coalson | 090f8f7 | 2002-06-04 05:53:04 +0000 | [diff] [blame] | 1845 | return status == FLAC__STREAM_DECODER_READ_STATUS_CONTINUE; |
Josh Coalson | bb7f6b9 | 2000-12-10 04:09:52 +0000 | [diff] [blame] | 1846 | } |