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
21 /*******************************************************************************
22 $Revision: #1 $
23 ******************************************************************************/
24
25 /**
26 @file
27 Functions for manipulating input bitstreams.
28
29 @ingroup codec_internal
30 */
31
32 /**
33 @addtogroup codec_internal
34 @{
35 */
36
37 #include "oi_assert.h"
38 #include "oi_bitstream.h"
39 #include "oi_stddefs.h"
40
OI_BITSTREAM_ReadInit(OI_BITSTREAM * bs,const OI_BYTE * buffer)41 PRIVATE void OI_BITSTREAM_ReadInit(OI_BITSTREAM* bs, const OI_BYTE* buffer) {
42 bs->value = 0;
43 bs->ptr.r = buffer;
44 bs->bitPtr = 32;
45 }
46
OI_BITSTREAM_ReadUINT(OI_BITSTREAM * bs,OI_UINT bits)47 PRIVATE uint32_t OI_BITSTREAM_ReadUINT(OI_BITSTREAM* bs, OI_UINT bits) {
48 uint32_t result;
49
50 OI_BITSTREAM_READUINT(result, bits, bs->ptr.r, bs->value, bs->bitPtr);
51
52 return result;
53 }
54
OI_BITSTREAM_ReadUINT4Aligned(OI_BITSTREAM * bs)55 PRIVATE uint8_t OI_BITSTREAM_ReadUINT4Aligned(OI_BITSTREAM* bs) {
56 uint32_t result;
57
58 OI_ASSERT(bs->bitPtr < 16);
59 OI_ASSERT(bs->bitPtr % 4 == 0);
60
61 if (bs->bitPtr == 8) {
62 result = bs->value << 8;
63 bs->bitPtr = 12;
64 } else {
65 result = bs->value << 12;
66 bs->value = (bs->value << 8) | *bs->ptr.r++;
67 bs->bitPtr = 8;
68 }
69 result >>= 28;
70 OI_ASSERT(result < (1u << 4));
71 return (uint8_t)result;
72 }
73
OI_BITSTREAM_ReadUINT8Aligned(OI_BITSTREAM * bs)74 PRIVATE uint8_t OI_BITSTREAM_ReadUINT8Aligned(OI_BITSTREAM* bs) {
75 uint32_t result;
76 OI_ASSERT(bs->bitPtr == 8);
77
78 result = bs->value >> 16;
79 bs->value = (bs->value << 8) | *bs->ptr.r++;
80
81 return (uint8_t)result;
82 }
83
84 /**
85 @}
86 */
87