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