1/*
2 * Copyright (c) 2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_APACHE_LICENSE_HEADER_START@
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License") ;
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *	 http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * @APPLE_APACHE_LICENSE_HEADER_END@
19 */
20
21/*=============================================================================
22	File:		ALACBitUtilities.h
23
24	$NoKeywords: $
25=============================================================================*/
26
27#ifndef __ALACBITUTILITIES_H
28#define __ALACBITUTILITIES_H
29
30#include <stdint.h>
31
32#ifndef MIN
33#define MIN(x, y) 			((x) < (y) ? (x) : (y))
34#endif // MIN
35#ifndef MAX
36#define MAX(x, y) 			((x) > (y) ? (x) : (y))
37#endif // MAX
38
39#define RequireAction(condition, action)		if (! (condition)) { action }
40#define RequireActionSilent(condition, action)	if (! (condition)) { action }
41#define RequireNoErr(condition, action)			if (condition) { action }
42
43enum
44{
45	ALAC_noErr = 0
46} ;
47
48
49typedef enum
50{	ID_SCE = 0,						/* Single Channel Element   */
51	ID_CPE = 1,						/* Channel Pair Element	 */
52	ID_CCE = 2,						/* Coupling Channel Element */
53	ID_LFE = 3,						/* LFE Channel Element	  */
54	ID_DSE = 4,						/* not yet supported		*/
55	ID_PCE = 5,
56	ID_FIL = 6,
57	ID_END = 7
58} ELEMENT_TYPE ;
59
60// types
61typedef struct BitBuffer
62{
63	uint8_t *		cur ;
64	uint8_t *		end ;
65	uint32_t		bitIndex ;
66	uint32_t		byteSize ;
67
68} BitBuffer ;
69
70/*
71	BitBuffer routines
72	- these routines take a fixed size buffer and read/write to it
73	- bounds checking must be done by the client
74*/
75void		BitBufferInit (BitBuffer * bits, uint8_t * buffer, uint32_t byteSize) ;
76uint32_t	BitBufferRead (BitBuffer * bits, uint8_t numBits) ;	// note: cannot read more than 16 bits at a time
77uint8_t		BitBufferReadSmall (BitBuffer * bits, uint8_t numBits) ;
78uint8_t		BitBufferReadOne (BitBuffer * bits) ;
79uint32_t	BitBufferPeek (BitBuffer * bits, uint8_t numBits) ;		// note: cannot read more than 16 bits at a time
80uint32_t	BitBufferPeekOne (BitBuffer * bits) ;
81uint32_t	BitBufferUnpackBERSize (BitBuffer * bits) ;
82uint32_t	BitBufferGetPosition (BitBuffer * bits) ;
83void		BitBufferByteAlign (BitBuffer * bits, int32_t addZeros) ;
84void		BitBufferAdvance (BitBuffer * bits, uint32_t numBits) ;
85void		BitBufferRewind (BitBuffer * bits, uint32_t numBits) ;
86void		BitBufferWrite (BitBuffer * bits, uint32_t value, uint32_t numBits) ;
87void		BitBufferReset (BitBuffer * bits) ;
88
89#endif	/* __BITUTILITIES_H */
90