File: | libs/libsndfile/src/ALAC/alac_decoder.c |
Location: | line 221, column 6 |
Description: | Value stored to 'mixRes' is never read |
1 | /* |
2 | * Copyright (c) 2011 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2012-2013 Erik de Castro Lopo <erikd@mega-nerd.com> |
4 | * |
5 | * @APPLE_APACHE_LICENSE_HEADER_START@ |
6 | * |
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | * you may not use this file except in compliance with the License. |
9 | * You may obtain a copy of the License at |
10 | * |
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | * |
13 | * Unless required by applicable law or agreed to in writing, software |
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | * See the License for the specific language governing permissions and |
17 | * limitations under the License. |
18 | * |
19 | * @APPLE_APACHE_LICENSE_HEADER_END@ |
20 | */ |
21 | |
22 | /* |
23 | File: ALACDecoder.cpp |
24 | */ |
25 | |
26 | #include <stdlib.h> |
27 | #include <stddef.h> |
28 | #include <string.h> |
29 | |
30 | #include "alac_codec.h" |
31 | |
32 | #include "dplib.h" |
33 | #include "aglib.h" |
34 | #include "matrixlib.h" |
35 | |
36 | #include "ALACBitUtilities.h" |
37 | #include "EndianPortable.h" |
38 | |
39 | typedef enum |
40 | { false = 0, |
41 | true = 1 |
42 | } bool ; |
43 | |
44 | // constants/data |
45 | const uint32_t kMaxBitDepth = 32; // max allowed bit depth is 32 |
46 | |
47 | |
48 | // prototypes |
49 | static int32_t alac_fill_element (struct BitBuffer * bits) ; |
50 | static int32_t alac_data_stream_element (struct BitBuffer * bits) ; |
51 | |
52 | static void Zero32( int32_t * buffer, uint32_t numItems, uint32_t stride ); |
53 | |
54 | |
55 | /* |
56 | Init() |
57 | - initialize the decoder with the given configuration |
58 | */ |
59 | int32_t |
60 | alac_decoder_init (ALAC_DECODER *p, void * inMagicCookie, uint32_t inMagicCookieSize) |
61 | { |
62 | int32_t status = ALAC_noErr; |
63 | ALACSpecificConfig theConfig; |
64 | uint8_t * theActualCookie = (uint8_t *)inMagicCookie; |
65 | uint32_t theCookieBytesRemaining = inMagicCookieSize; |
66 | |
67 | // For historical reasons the decoder needs to be resilient to magic cookies vended by older encoders. |
68 | // As specified in the ALACMagicCookieDescription.txt document, there may be additional data encapsulating |
69 | // the ALACSpecificConfig. This would consist of format ('frma') and 'alac' atoms which precede the |
70 | // ALACSpecificConfig. |
71 | // See ALACMagicCookieDescription.txt for additional documentation concerning the 'magic cookie' |
72 | |
73 | // skip format ('frma') atom if present |
74 | if (theActualCookie[4] == 'f' && theActualCookie[5] == 'r' && theActualCookie[6] == 'm' && theActualCookie[7] == 'a') |
75 | { |
76 | theActualCookie += 12; |
77 | theCookieBytesRemaining -= 12; |
78 | } |
79 | |
80 | // skip 'alac' atom header if present |
81 | if (theActualCookie[4] == 'a' && theActualCookie[5] == 'l' && theActualCookie[6] == 'a' && theActualCookie[7] == 'c') |
82 | { |
83 | theActualCookie += 12; |
84 | theCookieBytesRemaining -= 12; |
85 | } |
86 | |
87 | // read the ALACSpecificConfig |
88 | if (theCookieBytesRemaining >= sizeof(ALACSpecificConfig)) |
89 | { |
90 | theConfig.frameLength = psf_get_be32 (theActualCookie, offsetof (ALACSpecificConfig, frameLength)__builtin_offsetof(ALACSpecificConfig, frameLength)) ; |
91 | |
92 | if (theConfig.frameLength > ALAC_FRAME_LENGTH4096) |
93 | return fALAC_FrameLengthError ; |
94 | |
95 | theConfig.compatibleVersion = theActualCookie [offsetof (ALACSpecificConfig, compatibleVersion)__builtin_offsetof(ALACSpecificConfig, compatibleVersion)] ; |
96 | theConfig.bitDepth = theActualCookie [offsetof (ALACSpecificConfig, bitDepth)__builtin_offsetof(ALACSpecificConfig, bitDepth)] ; |
97 | theConfig.pb = theActualCookie [offsetof (ALACSpecificConfig, pb)__builtin_offsetof(ALACSpecificConfig, pb)] ; |
98 | theConfig.mb = theActualCookie [offsetof (ALACSpecificConfig, mb)__builtin_offsetof(ALACSpecificConfig, mb)] ; |
99 | theConfig.kb = theActualCookie [offsetof (ALACSpecificConfig, kb)__builtin_offsetof(ALACSpecificConfig, kb)] ; |
100 | theConfig.numChannels = theActualCookie [offsetof (ALACSpecificConfig, numChannels)__builtin_offsetof(ALACSpecificConfig, numChannels)] ; |
101 | theConfig.maxRun = psf_get_be16 (theActualCookie, offsetof (ALACSpecificConfig, maxRun)__builtin_offsetof(ALACSpecificConfig, maxRun)) ; |
102 | theConfig.maxFrameBytes = psf_get_be32 (theActualCookie, offsetof (ALACSpecificConfig, maxFrameBytes)__builtin_offsetof(ALACSpecificConfig, maxFrameBytes)) ; |
103 | theConfig.avgBitRate = psf_get_be32 (theActualCookie, offsetof (ALACSpecificConfig, avgBitRate)__builtin_offsetof(ALACSpecificConfig, avgBitRate)) ; |
104 | theConfig.sampleRate = psf_get_be32 (theActualCookie, offsetof (ALACSpecificConfig, sampleRate)__builtin_offsetof(ALACSpecificConfig, sampleRate)) ; |
105 | |
106 | p->mConfig = theConfig; |
107 | |
108 | RequireAction( p->mConfig.compatibleVersion <= kALACVersion, return kALAC_ParamError; )if (!(p->mConfig.compatibleVersion <= kALACVersion)) { return kALAC_ParamError; }; |
109 | |
110 | RequireAction( (p->mMixBufferU != NULL) && (p->mMixBufferV != NULL) && (p->mPredictor != NULL),if (!((p->mMixBufferU != ((void*)0)) && (p->mMixBufferV != ((void*)0)) && (p->mPredictor != ((void*)0)))) { status = kALAC_MemFullError; goto Exit; } |
111 | status = kALAC_MemFullError; goto Exit; )if (!((p->mMixBufferU != ((void*)0)) && (p->mMixBufferV != ((void*)0)) && (p->mPredictor != ((void*)0)))) { status = kALAC_MemFullError; goto Exit; }; |
112 | } |
113 | else |
114 | { |
115 | status = kALAC_ParamError; |
116 | } |
117 | |
118 | // skip to Channel Layout Info |
119 | // theActualCookie += sizeof(ALACSpecificConfig); |
120 | |
121 | // Currently, the Channel Layout Info portion of the magic cookie (as defined in the |
122 | // ALACMagicCookieDescription.txt document) is unused by the decoder. |
123 | |
124 | Exit: |
125 | return status; |
126 | } |
127 | |
128 | /* |
129 | Decode() |
130 | - the decoded samples are interleaved into the output buffer in the order they arrive in |
131 | the bitstream |
132 | */ |
133 | int32_t |
134 | alac_decode (ALAC_DECODER *p, struct BitBuffer * bits, int32_t * sampleBuffer, uint32_t numSamples, uint32_t numChannels, uint32_t * outNumSamples) |
135 | { |
136 | BitBuffer shiftBits; |
137 | uint32_t bits1, bits2; |
138 | uint8_t tag; |
139 | uint8_t elementInstanceTag; |
140 | AGParamRec agParams; |
141 | uint32_t channelIndex; |
142 | int16_t coefsU[32]; // max possible size is 32 although NUMCOEPAIRS is the current limit |
143 | int16_t coefsV[32]; |
144 | uint8_t numU, numV; |
145 | uint8_t mixBits; |
146 | int8_t mixRes; |
147 | uint16_t unusedHeader; |
148 | uint8_t escapeFlag; |
149 | uint32_t chanBits; |
150 | uint8_t bytesShifted; |
151 | uint32_t shift; |
152 | uint8_t modeU, modeV; |
153 | uint32_t denShiftU, denShiftV; |
154 | uint16_t pbFactorU, pbFactorV; |
155 | uint16_t pb; |
156 | int32_t * out32; |
157 | uint8_t headerByte; |
158 | uint8_t partialFrame; |
159 | uint32_t extraBits; |
160 | int32_t val; |
161 | uint32_t i, j; |
162 | int32_t status; |
163 | |
164 | RequireAction( (bits != NULL) && (sampleBuffer != NULL) && (outNumSamples != NULL), return kALAC_ParamError; )if (!((bits != ((void*)0)) && (sampleBuffer != ((void *)0)) && (outNumSamples != ((void*)0)))) { return kALAC_ParamError ; }; |
165 | RequireAction( numChannels > 0, return kALAC_ParamError; )if (!(numChannels > 0)) { return kALAC_ParamError; }; |
166 | |
167 | p->mActiveElements = 0; |
168 | channelIndex = 0; |
169 | |
170 | status = ALAC_noErr; |
171 | *outNumSamples = numSamples; |
172 | |
173 | while ( status == ALAC_noErr ) |
174 | { |
175 | // bail if we ran off the end of the buffer |
176 | RequireAction( bits->cur < bits->end, status = kALAC_ParamError; goto Exit; )if (!(bits->cur < bits->end)) { status = kALAC_ParamError ; goto Exit; }; |
177 | |
178 | // copy global decode params for this element |
179 | pb = p->mConfig.pb; |
180 | |
181 | // read element tag |
182 | tag = BitBufferReadSmall( bits, 3 ); |
183 | switch ( tag ) |
184 | { |
185 | case ID_SCE: |
186 | case ID_LFE: |
187 | { |
188 | // mono/LFE channel |
189 | elementInstanceTag = BitBufferReadSmall( bits, 4 ); |
190 | p->mActiveElements |= (1u << elementInstanceTag); |
191 | |
192 | // read the 12 unused header bits |
193 | unusedHeader = (uint16_t) BitBufferRead( bits, 12 ); |
194 | RequireAction( unusedHeader == 0, status = kALAC_ParamError; goto Exit; )if (!(unusedHeader == 0)) { status = kALAC_ParamError; goto Exit ; }; |
195 | |
196 | // read the 1-bit "partial frame" flag, 2-bit "shift-off" flag & 1-bit "escape" flag |
197 | headerByte = (uint8_t) BitBufferRead( bits, 4 ); |
198 | |
199 | partialFrame = headerByte >> 3; |
200 | |
201 | bytesShifted = (headerByte >> 1) & 0x3u; |
202 | RequireAction( bytesShifted != 3, status = kALAC_ParamError; goto Exit; )if (!(bytesShifted != 3)) { status = kALAC_ParamError; goto Exit ; }; |
203 | |
204 | shift = bytesShifted * 8; |
205 | |
206 | escapeFlag = headerByte & 0x1; |
207 | |
208 | chanBits = p->mConfig.bitDepth - (bytesShifted * 8); |
209 | |
210 | // check for partial frame to override requested numSamples |
211 | if ( partialFrame != 0 ) |
212 | { |
213 | numSamples = BitBufferRead( bits, 16 ) << 16; |
214 | numSamples |= BitBufferRead( bits, 16 ); |
215 | } |
216 | |
217 | if ( escapeFlag == 0 ) |
218 | { |
219 | // compressed frame, read rest of parameters |
220 | mixBits = (uint8_t) BitBufferRead( bits, 8 ); |
221 | mixRes = (int8_t) BitBufferRead( bits, 8 ); |
Value stored to 'mixRes' is never read | |
222 | //Assert( (mixBits == 0) && (mixRes == 0) ); // no mixing for mono |
223 | |
224 | headerByte = (uint8_t) BitBufferRead( bits, 8 ); |
225 | modeU = headerByte >> 4; |
226 | denShiftU = headerByte & 0xfu; |
227 | |
228 | headerByte = (uint8_t) BitBufferRead( bits, 8 ); |
229 | pbFactorU = headerByte >> 5; |
230 | numU = headerByte & 0x1fu; |
231 | |
232 | for ( i = 0; i < numU; i++ ) |
233 | coefsU[i] = (int16_t) BitBufferRead( bits, 16 ); |
234 | |
235 | // if shift active, skip the the shift buffer but remember where it starts |
236 | if ( bytesShifted != 0 ) |
237 | { |
238 | shiftBits = *bits; |
239 | BitBufferAdvance( bits, (bytesShifted * 8) * numSamples ); |
240 | } |
241 | |
242 | // decompress |
243 | set_ag_params( &agParams, p->mConfig.mb, (pb * pbFactorU) / 4, p->mConfig.kb, numSamples, numSamples, p->mConfig.maxRun ); |
244 | status = dyn_decomp( &agParams, bits, p->mPredictor, numSamples, chanBits, &bits1 ); |
245 | RequireNoErr( status, goto Exit; )if ((status)) { goto Exit; }; |
246 | |
247 | if ( modeU == 0 ) |
248 | { |
249 | unpc_block( p->mPredictor, p->mMixBufferU, numSamples, &coefsU[0], numU, chanBits, denShiftU ); |
250 | } |
251 | else |
252 | { |
253 | // the special "numActive == 31" mode can be done in-place |
254 | unpc_block( p->mPredictor, p->mPredictor, numSamples, NULL((void*)0), 31, chanBits, 0 ); |
255 | unpc_block( p->mPredictor, p->mMixBufferU, numSamples, &coefsU[0], numU, chanBits, denShiftU ); |
256 | } |
257 | } |
258 | else |
259 | { |
260 | //Assert( bytesShifted == 0 ); |
261 | |
262 | // uncompressed frame, copy data into the mix buffer to use common output code |
263 | shift = 32 - chanBits; |
264 | if ( chanBits <= 16 ) |
265 | { |
266 | for ( i = 0; i < numSamples; i++ ) |
267 | { |
268 | val = (int32_t) BitBufferRead( bits, (uint8_t) chanBits ); |
269 | val = (val << shift) >> shift; |
270 | p->mMixBufferU[i] = val; |
271 | } |
272 | } |
273 | else |
274 | { |
275 | // BitBufferRead() can't read more than 16 bits at a time so break up the reads |
276 | extraBits = chanBits - 16; |
277 | for ( i = 0; i < numSamples; i++ ) |
278 | { |
279 | val = (int32_t) BitBufferRead( bits, 16 ); |
280 | val = (val << 16) >> shift; |
281 | p->mMixBufferU[i] = val | BitBufferRead( bits, (uint8_t) extraBits ); |
282 | } |
283 | } |
284 | |
285 | mixBits = mixRes = 0; |
286 | bits1 = chanBits * numSamples; |
287 | bytesShifted = 0; |
288 | } |
289 | |
290 | // now read the shifted values into the shift buffer |
291 | if ( bytesShifted != 0 ) |
292 | { |
293 | shift = bytesShifted * 8; |
294 | //Assert( shift <= 16 ); |
295 | |
296 | for ( i = 0; i < numSamples; i++ ) |
297 | p->mShiftBuffer[i] = (uint16_t) BitBufferRead( &shiftBits, (uint8_t) shift ); |
298 | } |
299 | |
300 | // convert 32-bit integers into output buffer |
301 | switch ( p->mConfig.bitDepth ) |
302 | { |
303 | case 16: |
304 | out32 = sampleBuffer + channelIndex; |
305 | for ( i = 0, j = 0; i < numSamples; i++, j += numChannels ) |
306 | out32[j] = p->mMixBufferU[i] << 16; |
307 | break; |
308 | case 20: |
309 | out32 = sampleBuffer + channelIndex; |
310 | copyPredictorTo20( p->mMixBufferU, out32, numChannels, numSamples ); |
311 | break; |
312 | case 24: |
313 | out32 = sampleBuffer + channelIndex; |
314 | if ( bytesShifted != 0 ) |
315 | copyPredictorTo24Shift( p->mMixBufferU, p->mShiftBuffer, out32, numChannels, numSamples, bytesShifted ); |
316 | else |
317 | copyPredictorTo24( p->mMixBufferU, out32, numChannels, numSamples ); |
318 | break; |
319 | case 32: |
320 | out32 = sampleBuffer + channelIndex; |
321 | if ( bytesShifted != 0 ) |
322 | copyPredictorTo32Shift( p->mMixBufferU, p->mShiftBuffer, out32, numChannels, numSamples, bytesShifted ); |
323 | else |
324 | copyPredictorTo32( p->mMixBufferU, out32, numChannels, numSamples); |
325 | break; |
326 | } |
327 | |
328 | channelIndex += 1; |
329 | *outNumSamples = numSamples; |
330 | break; |
331 | } |
332 | |
333 | case ID_CPE: |
334 | { |
335 | // if decoding this pair would take us over the max channels limit, bail |
336 | if ( (channelIndex + 2) > numChannels ) |
337 | goto NoMoreChannels; |
338 | |
339 | // stereo channel pair |
340 | elementInstanceTag = BitBufferReadSmall( bits, 4 ); |
341 | p->mActiveElements |= (1u << elementInstanceTag); |
342 | |
343 | // read the 12 unused header bits |
344 | unusedHeader = (uint16_t) BitBufferRead( bits, 12 ); |
345 | RequireAction( unusedHeader == 0, status = kALAC_ParamError; goto Exit; )if (!(unusedHeader == 0)) { status = kALAC_ParamError; goto Exit ; }; |
346 | |
347 | // read the 1-bit "partial frame" flag, 2-bit "shift-off" flag & 1-bit "escape" flag |
348 | headerByte = (uint8_t) BitBufferRead( bits, 4 ); |
349 | |
350 | partialFrame = headerByte >> 3; |
351 | |
352 | bytesShifted = (headerByte >> 1) & 0x3u; |
353 | RequireAction( bytesShifted != 3, status = kALAC_ParamError; goto Exit; )if (!(bytesShifted != 3)) { status = kALAC_ParamError; goto Exit ; }; |
354 | |
355 | shift = bytesShifted * 8; |
356 | |
357 | escapeFlag = headerByte & 0x1; |
358 | |
359 | chanBits = p->mConfig.bitDepth - (bytesShifted * 8) + 1; |
360 | |
361 | // check for partial frame length to override requested numSamples |
362 | if ( partialFrame != 0 ) |
363 | { |
364 | numSamples = BitBufferRead( bits, 16 ) << 16; |
365 | numSamples |= BitBufferRead( bits, 16 ); |
366 | } |
367 | |
368 | if ( escapeFlag == 0 ) |
369 | { |
370 | // compressed frame, read rest of parameters |
371 | mixBits = (uint8_t) BitBufferRead( bits, 8 ); |
372 | mixRes = (int8_t) BitBufferRead( bits, 8 ); |
373 | |
374 | headerByte = (uint8_t) BitBufferRead( bits, 8 ); |
375 | modeU = headerByte >> 4; |
376 | denShiftU = headerByte & 0xfu; |
377 | |
378 | headerByte = (uint8_t) BitBufferRead( bits, 8 ); |
379 | pbFactorU = headerByte >> 5; |
380 | numU = headerByte & 0x1fu; |
381 | for ( i = 0; i < numU; i++ ) |
382 | coefsU[i] = (int16_t) BitBufferRead( bits, 16 ); |
383 | |
384 | headerByte = (uint8_t) BitBufferRead( bits, 8 ); |
385 | modeV = headerByte >> 4; |
386 | denShiftV = headerByte & 0xfu; |
387 | |
388 | headerByte = (uint8_t) BitBufferRead( bits, 8 ); |
389 | pbFactorV = headerByte >> 5; |
390 | numV = headerByte & 0x1fu; |
391 | for ( i = 0; i < numV; i++ ) |
392 | coefsV[i] = (int16_t) BitBufferRead( bits, 16 ); |
393 | |
394 | // if shift active, skip the interleaved shifted values but remember where they start |
395 | if ( bytesShifted != 0 ) |
396 | { |
397 | shiftBits = *bits; |
398 | BitBufferAdvance( bits, (bytesShifted * 8) * 2 * numSamples ); |
399 | } |
400 | |
401 | // decompress and run predictor for "left" channel |
402 | set_ag_params( &agParams, p->mConfig.mb, (pb * pbFactorU) / 4, p->mConfig.kb, numSamples, numSamples, p->mConfig.maxRun ); |
403 | status = dyn_decomp( &agParams, bits, p->mPredictor, numSamples, chanBits, &bits1 ); |
404 | RequireNoErr( status, goto Exit; )if ((status)) { goto Exit; }; |
405 | |
406 | if ( modeU == 0 ) |
407 | { |
408 | unpc_block( p->mPredictor, p->mMixBufferU, numSamples, &coefsU[0], numU, chanBits, denShiftU ); |
409 | } |
410 | else |
411 | { |
412 | // the special "numActive == 31" mode can be done in-place |
413 | unpc_block( p->mPredictor, p->mPredictor, numSamples, NULL((void*)0), 31, chanBits, 0 ); |
414 | unpc_block( p->mPredictor, p->mMixBufferU, numSamples, &coefsU[0], numU, chanBits, denShiftU ); |
415 | } |
416 | |
417 | // decompress and run predictor for "right" channel |
418 | set_ag_params( &agParams, p->mConfig.mb, (pb * pbFactorV) / 4, p->mConfig.kb, numSamples, numSamples, p->mConfig.maxRun ); |
419 | status = dyn_decomp( &agParams, bits, p->mPredictor, numSamples, chanBits, &bits2 ); |
420 | RequireNoErr( status, goto Exit; )if ((status)) { goto Exit; }; |
421 | |
422 | if ( modeV == 0 ) |
423 | { |
424 | unpc_block( p->mPredictor, p->mMixBufferV, numSamples, &coefsV[0], numV, chanBits, denShiftV ); |
425 | } |
426 | else |
427 | { |
428 | // the special "numActive == 31" mode can be done in-place |
429 | unpc_block( p->mPredictor, p->mPredictor, numSamples, NULL((void*)0), 31, chanBits, 0 ); |
430 | unpc_block( p->mPredictor, p->mMixBufferV, numSamples, &coefsV[0], numV, chanBits, denShiftV ); |
431 | } |
432 | } |
433 | else |
434 | { |
435 | //Assert( bytesShifted == 0 ); |
436 | |
437 | // uncompressed frame, copy data into the mix buffers to use common output code |
438 | chanBits = p->mConfig.bitDepth; |
439 | shift = 32 - chanBits; |
440 | if ( chanBits <= 16 ) |
441 | { |
442 | for ( i = 0; i < numSamples; i++ ) |
443 | { |
444 | val = (int32_t) BitBufferRead( bits, (uint8_t) chanBits ); |
445 | val = (val << shift) >> shift; |
446 | p->mMixBufferU[i] = val; |
447 | |
448 | val = (int32_t) BitBufferRead( bits, (uint8_t) chanBits ); |
449 | val = (val << shift) >> shift; |
450 | p->mMixBufferV[i] = val; |
451 | } |
452 | } |
453 | else |
454 | { |
455 | // BitBufferRead() can't read more than 16 bits at a time so break up the reads |
456 | extraBits = chanBits - 16; |
457 | for ( i = 0; i < numSamples; i++ ) |
458 | { |
459 | val = (int32_t) BitBufferRead( bits, 16 ); |
460 | val = (val << 16) >> shift; |
461 | p->mMixBufferU[i] = val | BitBufferRead( bits, (uint8_t)extraBits ); |
462 | |
463 | val = (int32_t) BitBufferRead( bits, 16 ); |
464 | val = (val << 16) >> shift; |
465 | p->mMixBufferV[i] = val | BitBufferRead( bits, (uint8_t)extraBits ); |
466 | } |
467 | } |
468 | |
469 | bits1 = chanBits * numSamples; |
470 | bits2 = chanBits * numSamples; |
471 | mixBits = mixRes = 0; |
472 | bytesShifted = 0; |
473 | } |
474 | |
475 | // now read the shifted values into the shift buffer |
476 | if ( bytesShifted != 0 ) |
477 | { |
478 | shift = bytesShifted * 8; |
479 | //Assert( shift <= 16 ); |
480 | |
481 | for ( i = 0; i < (numSamples * 2); i += 2 ) |
482 | { |
483 | p->mShiftBuffer[i + 0] = (uint16_t) BitBufferRead( &shiftBits, (uint8_t) shift ); |
484 | p->mShiftBuffer[i + 1] = (uint16_t) BitBufferRead( &shiftBits, (uint8_t) shift ); |
485 | } |
486 | } |
487 | |
488 | // un-mix the data and convert to output format |
489 | // - note that mixRes = 0 means just interleave so we use that path for uncompressed frames |
490 | switch ( p->mConfig.bitDepth ) |
491 | { |
492 | case 16: |
493 | out32 = sampleBuffer + channelIndex; |
494 | unmix16( p->mMixBufferU, p->mMixBufferV, out32, numChannels, numSamples, mixBits, mixRes ); |
495 | break; |
496 | case 20: |
497 | out32 = sampleBuffer + channelIndex; |
498 | unmix20( p->mMixBufferU, p->mMixBufferV, out32, numChannels, numSamples, mixBits, mixRes ); |
499 | break; |
500 | case 24: |
501 | out32 = sampleBuffer + channelIndex; |
502 | unmix24( p->mMixBufferU, p->mMixBufferV, out32, numChannels, numSamples, |
503 | mixBits, mixRes, p->mShiftBuffer, bytesShifted ); |
504 | break; |
505 | case 32: |
506 | out32 = sampleBuffer + channelIndex; |
507 | unmix32( p->mMixBufferU, p->mMixBufferV, out32, numChannels, numSamples, |
508 | mixBits, mixRes, p->mShiftBuffer, bytesShifted ); |
509 | break; |
510 | } |
511 | |
512 | channelIndex += 2; |
513 | *outNumSamples = numSamples; |
514 | break; |
515 | } |
516 | |
517 | case ID_CCE: |
518 | case ID_PCE: |
519 | { |
520 | // unsupported element, bail |
521 | //AssertNoErr( tag ); |
522 | status = kALAC_ParamError; |
523 | break; |
524 | } |
525 | |
526 | case ID_DSE: |
527 | { |
528 | // data stream element -- parse but ignore |
529 | status = alac_data_stream_element (bits) ; |
530 | break; |
531 | } |
532 | |
533 | case ID_FIL: |
534 | { |
535 | // fill element -- parse but ignore |
536 | status = alac_fill_element (bits) ; |
537 | break; |
538 | } |
539 | |
540 | case ID_END: |
541 | { |
542 | // frame end, all done so byte align the frame and check for overruns |
543 | BitBufferByteAlign( bits, false ); |
544 | //Assert( bits->cur == bits->end ); |
545 | goto Exit; |
546 | } |
547 | } |
548 | |
549 | #if 0 // ! DEBUG |
550 | // if we've decoded all of our channels, bail (but not in debug b/c we want to know if we're seeing bad bits) |
551 | // - this also protects us if the config does not match the bitstream or crap data bits follow the audio bits |
552 | if ( channelIndex >= numChannels ) |
553 | break; |
554 | #endif |
555 | } |
556 | |
557 | NoMoreChannels: |
558 | |
559 | // if we get here and haven't decoded all of the requested channels, fill the remaining channels with zeros |
560 | for ( ; channelIndex < numChannels; channelIndex++ ) |
561 | { |
562 | int32_t * fill32 = sampleBuffer + channelIndex; |
563 | Zero32( fill32, numSamples, numChannels ); |
564 | } |
565 | |
566 | Exit: |
567 | return status; |
568 | } |
569 | |
570 | #if PRAGMA_MARK0 |
571 | #pragma mark - |
572 | #endif |
573 | |
574 | /* |
575 | FillElement() |
576 | - they're just filler so we don't need 'em |
577 | */ |
578 | static int32_t |
579 | alac_fill_element (struct BitBuffer * bits) |
580 | { |
581 | int16_t count; |
582 | |
583 | // 4-bit count or (4-bit + 8-bit count) if 4-bit count == 15 |
584 | // - plus this weird -1 thing I still don't fully understand |
585 | count = BitBufferReadSmall( bits, 4 ); |
586 | if ( count == 15 ) |
587 | count += (int16_t) BitBufferReadSmall( bits, 8 ) - 1; |
588 | |
589 | BitBufferAdvance( bits, count * 8 ); |
590 | |
591 | RequireAction( bits->cur <= bits->end, return kALAC_ParamError; )if (!(bits->cur <= bits->end)) { return kALAC_ParamError ; }; |
592 | |
593 | return ALAC_noErr; |
594 | } |
595 | |
596 | /* |
597 | DataStreamElement() |
598 | - we don't care about data stream elements so just skip them |
599 | */ |
600 | static int32_t |
601 | alac_data_stream_element (struct BitBuffer * bits) |
602 | { |
603 | int32_t data_byte_align_flag; |
604 | uint16_t count; |
605 | |
606 | // the tag associates this data stream element with a given audio element |
607 | |
608 | /* element_instance_tag = */ BitBufferReadSmall( bits, 4 ); |
609 | |
610 | data_byte_align_flag = BitBufferReadOne( bits ); |
611 | |
612 | // 8-bit count or (8-bit + 8-bit count) if 8-bit count == 255 |
613 | count = BitBufferReadSmall( bits, 8 ); |
614 | if ( count == 255 ) |
615 | count += BitBufferReadSmall( bits, 8 ); |
616 | |
617 | // the align flag means the bitstream should be byte-aligned before reading the following data bytes |
618 | if ( data_byte_align_flag ) |
619 | BitBufferByteAlign( bits, false ); |
620 | |
621 | // skip the data bytes |
622 | BitBufferAdvance( bits, count * 8 ); |
623 | |
624 | RequireAction( bits->cur <= bits->end, return kALAC_ParamError; )if (!(bits->cur <= bits->end)) { return kALAC_ParamError ; }; |
625 | |
626 | return ALAC_noErr; |
627 | } |
628 | |
629 | /* |
630 | ZeroN() |
631 | - helper routines to clear out output channel buffers when decoding fewer channels than requested |
632 | */ |
633 | static void Zero32( int32_t * buffer, uint32_t numItems, uint32_t stride ) |
634 | { |
635 | uint32_t indx; |
636 | |
637 | if ( stride == 1 ) |
638 | { |
639 | memset( buffer, 0, numItems * sizeof(int32_t) ); |
640 | } |
641 | else |
642 | { |
643 | for ( indx = 0; indx < (numItems * stride); indx += stride ) |
644 | buffer[indx] = 0; |
645 | } |
646 | } |