1 /****************************************************************************** 2 * 3 * Copyright 2014 The Android Open Source Project 4 * Copyright 2003 - 2004 Open Interface North America, Inc. All rights 5 * reserved. 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 ******************************************************************************/ 20 #ifndef _OI_BITSTREAM_H 21 #define _OI_BITSTREAM_H 22 23 /******************************************************************************* 24 $Revision: #1 $ 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 uint32_t OI_BITSTREAM_ReadUINT(OI_BITSTREAM* bs, OI_UINT bits); 48 49 INLINE uint8_t OI_BITSTREAM_ReadUINT4Aligned(OI_BITSTREAM* bs); 50 51 INLINE uint8_t OI_BITSTREAM_ReadUINT8Aligned(OI_BITSTREAM* bs); 52 53 INLINE void OI_BITSTREAM_WriteUINT(OI_BITSTREAM* bs, uint16_t value, OI_UINT bits); 54 55 /* 56 * Use knowledge that the bitstream is aligned to optimize the write of a byte 57 */ 58 PRIVATE void OI_BITSTREAM_WriteUINT8Aligned(OI_BITSTREAM* bs, uint8_t datum); 59 60 /* 61 * Use knowledge that the bitstream is aligned to optimize the writing of a 62 * pair of nibbles. 63 */ 64 PRIVATE void OI_BITSTREAM_Write2xUINT4Aligned(OI_BITSTREAM* bs, uint8_t datum1, uint8_t datum2); 65 66 /** Internally the bitstream looks ahead in the stream. When 67 * OI_SBC_ReadScalefactors() goes to temporarily break the abstraction, it will 68 * need to know where the "logical" pointer is in the stream. 69 */ 70 #define OI_BITSTREAM_GetWritePtr(bs) ((bs)->ptr.w - 3) 71 #define OI_BITSTREAM_GetReadPtr(bs) ((bs)->ptr.r - 3) 72 73 /** This is declared here as a macro because decoder.c breaks the bitsream 74 * encapsulation for efficiency reasons. 75 */ 76 #define OI_BITSTREAM_READUINT(result, bits, ptr, value, bitPtr) \ 77 do { \ 78 OI_ASSERT((bits) <= 16); \ 79 OI_ASSERT((bitPtr) < 32); \ 80 OI_ASSERT((bitPtr) >= 0); \ 81 \ 82 while ((bitPtr + bits) > 32) { \ 83 (value) = ((value) << 8) | *(ptr)++; \ 84 (bitPtr) -= 8; \ 85 } \ 86 \ 87 (result) = (value) << (bitPtr); \ 88 (result) >>= 32 - (bits); \ 89 \ 90 (bitPtr) += (bits); \ 91 OI_ASSERT(((bits) == 0) || ((result) < (1u << (bits)))); \ 92 } while (0) 93 94 #define OI_BITSTREAM_WRITEUINT(ptr, value, bitPtr, datum, bits) \ 95 do { \ 96 (bitPtr) -= (bits); \ 97 (value) |= (datum) << (bitPtr); \ 98 \ 99 while ((bitPtr) <= 16) { \ 100 (bitPtr) += 8; \ 101 *(ptr)++ = (uint8_t)((value) >> 24); \ 102 (value) <<= 8; \ 103 } \ 104 } while (0) 105 106 #define OI_BITSTREAM_WRITEFLUSH(ptr, value, bitPtr) \ 107 do { \ 108 while ((bitPtr) < 32) { \ 109 (bitPtr) += 8; \ 110 *(ptr)++ = (uint8_t)((value) >> 24); \ 111 (value) <<= 8; \ 112 } \ 113 } while (0) 114 115 /** 116 @} 117 */ 118 119 #endif /* _OI_BITSTREAM_H */ 120