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