1 /******************************************************************************
2  *
3  *  Copyright (C) 2014 The Android Open Source Project
4  *  Copyright 2003 - 2004 Open Interface North America, Inc. All rights reserved.
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  ******************************************************************************/
19 #ifndef _OI_BITSTREAM_H
20 #define _OI_BITSTREAM_H
21 
22 /**********************************************************************************
23   $Revision: #1 $
24 ***********************************************************************************/
25 
26 
27 /**
28 @file
29 Function prototypes and macro definitions for manipulating input and output
30 bitstreams.
31 
32 @ingroup codec_internal
33 */
34 
35 /**
36 @addtogroup codec_internal
37 @{
38 */
39 
40 #include "oi_codec_sbc_private.h"
41 #include "oi_stddefs.h"
42 
43 INLINE void OI_BITSTREAM_ReadInit(OI_BITSTREAM *bs, const OI_BYTE *buffer);
44 
45 INLINE void OI_BITSTREAM_WriteInit(OI_BITSTREAM *bs, OI_BYTE *buffer);
46 
47 INLINE OI_UINT32 OI_BITSTREAM_ReadUINT(OI_BITSTREAM *bs, OI_UINT bits);
48 
49 INLINE OI_UINT8 OI_BITSTREAM_ReadUINT4Aligned(OI_BITSTREAM *bs);
50 
51 INLINE OI_UINT8 OI_BITSTREAM_ReadUINT8Aligned(OI_BITSTREAM *bs);
52 
53 INLINE void OI_BITSTREAM_WriteUINT(OI_BITSTREAM *bs,
54                                    OI_UINT16 value,
55                                    OI_UINT bits);
56 
57 /*
58  * Use knowledge that the bitstream is aligned to optimize the write of a byte
59  */
60 PRIVATE void OI_BITSTREAM_WriteUINT8Aligned(OI_BITSTREAM *bs,
61         OI_UINT8 datum);
62 
63 /*
64  * Use knowledge that the bitstream is aligned to optimize the write pair of nibbles
65  */
66 PRIVATE void OI_BITSTREAM_Write2xUINT4Aligned(OI_BITSTREAM *bs,
67         OI_UINT8 datum1,
68         OI_UINT8 datum2);
69 
70 /** Internally the bitstream looks ahead in the stream. When
71  * OI_SBC_ReadScalefactors() goes to temporarily break the abstraction, it will
72  * need to know where the "logical" pointer is in the stream.
73  */
74 #define OI_BITSTREAM_GetWritePtr(bs) ((bs)->ptr.w - 3)
75 #define OI_BITSTREAM_GetReadPtr(bs) ((bs)->ptr.r - 3)
76 
77 /** This is declared here as a macro because decoder.c breaks the bitsream
78  * encapsulation for efficiency reasons.
79  */
80 #define OI_BITSTREAM_READUINT(result, bits, ptr, value, bitPtr) \
81 do { \
82     OI_ASSERT((bits) <= 16); \
83     OI_ASSERT((bitPtr) < 16); \
84     OI_ASSERT((bitPtr) >= 8); \
85     \
86     result = (value) << (bitPtr); \
87     result >>= 32 - (bits); \
88     \
89     bitPtr += (bits); \
90     while (bitPtr >= 16) { \
91         value = ((value) << 8) | *ptr++; \
92         bitPtr -= 8; \
93     } \
94     OI_ASSERT((bits == 0) || (result < (1u << (bits)))); \
95 } while (0)
96 
97 
98 #define OI_BITSTREAM_WRITEUINT(ptr, value, bitPtr, datum, bits) \
99 do {\
100     bitPtr -= bits;\
101     value |= datum << bitPtr;\
102     \
103     while (bitPtr <= 16) {\
104         bitPtr += 8;\
105         *ptr++ = (OI_UINT8)(value >> 24);\
106         value <<= 8;\
107     }\
108 } while (0)
109 
110 #define OI_BITSTREAM_WRITEFLUSH(ptr, value, bitPtr) \
111 do {\
112     while (bitPtr < 32) {\
113         bitPtr += 8;\
114         *ptr++ = (OI_UINT8)(value >> 24);\
115         value <<= 8;\
116     }\
117 } while (0)
118 
119 /**
120 @}
121 */
122 
123 #endif /* _OI_BITSTREAM_H */
124