1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
20 Filename: buf_getbits.c
22 ------------------------------------------------------------------------------
29 ------------------------------------------------------------------------------
30 INPUT AND OUTPUT DEFINITIONS
32 Arguments: hBitBuf Handle to Bitbuffer
33 n Number of bits to read
38 ------------------------------------------------------------------------------
41 Reads n bits from Bitbuffer
43 ------------------------------------------------------------------------------
47 ------------------------------------------------------------------------------
50 SC 29 Software Copyright Licencing Disclaimer:
52 This software module was originally developed by
58 in the course of development of the ISO/IEC 13818-7 and ISO/IEC 14496-3
59 standards for reference purposes and its performance may not have been
60 optimized. This software module is an implementation of one or more tools as
61 specified by the ISO/IEC 13818-7 and ISO/IEC 14496-3 standards.
62 ISO/IEC gives users free license to this software module or modifications
63 thereof for use in products claiming conformance to audiovisual and
64 image-coding related ITU Recommendations and/or ISO/IEC International
65 Standards. ISO/IEC gives users the same free license to this software module or
66 modifications thereof for research purposes and further ISO/IEC standardisation.
67 Those intending to use this software module in products are advised that its
68 use may infringe existing patents. ISO/IEC have no liability for use of this
69 software module or modifications thereof. Copyright is not released for
70 products that do not conform to audiovisual and image-coding related ITU
71 Recommendations and/or ISO/IEC International Standards.
72 The original developer retains full right to modify and use the code for its
73 own purpose, assign or donate the code to a third party and to inhibit third
74 parties from using the code for products that do not conform to audiovisual and
75 image-coding related ITU Recommendations and/or ISO/IEC International Standards.
76 This copyright notice must be included in all copies or derivative works.
77 Copyright (c) ISO/IEC 2002.
79 ------------------------------------------------------------------------------
82 ------------------------------------------------------------------------------
86 /*----------------------------------------------------------------------------
88 ----------------------------------------------------------------------------*/
92 #include "buf_getbits.h"
94 /*----------------------------------------------------------------------------
96 ; Define module specific macros here
97 ----------------------------------------------------------------------------*/
100 /*----------------------------------------------------------------------------
102 ; Include all pre-processor statements here. Include conditional
103 ; compile variables also.
104 ----------------------------------------------------------------------------*/
106 /*----------------------------------------------------------------------------
107 ; LOCAL FUNCTION DEFINITIONS
108 ; Function Prototype declaration
109 ----------------------------------------------------------------------------*/
111 /*----------------------------------------------------------------------------
112 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
113 ; Variable declaration - defined here and used outside this module
114 ----------------------------------------------------------------------------*/
116 /*----------------------------------------------------------------------------
117 ; EXTERNAL FUNCTION REFERENCES
118 ; Declare functions defined elsewhere and referenced in this module
119 ----------------------------------------------------------------------------*/
121 /*----------------------------------------------------------------------------
122 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
123 ; Declare variables used in this module but defined elsewhere
124 ----------------------------------------------------------------------------*/
126 /*----------------------------------------------------------------------------
128 ----------------------------------------------------------------------------*/
130 UInt32 buf_getbits(BIT_BUFFER * hBitBuf, Int32 n)
133 /* read bits from MSB side */
134 if (hBitBuf->buffered_bits <= 16)
136 hBitBuf->buffer_word = (hBitBuf->buffer_word << 16) | (*(hBitBuf->char_ptr++) << 8);
137 hBitBuf->buffer_word |= *(hBitBuf->char_ptr++);
138 hBitBuf->buffered_bits += 16;
141 hBitBuf->buffered_bits -= n;
142 hBitBuf->nrBitsRead += n;
144 return ((hBitBuf->buffer_word >> hBitBuf->buffered_bits) & ((1 << n) - 1));
149 UInt32 buf_get_1bit(BIT_BUFFER * hBitBuf)
152 /* read bits from MSB side */
153 if (hBitBuf->buffered_bits <= 16)
155 hBitBuf->buffer_word = (hBitBuf->buffer_word << 16) | (*(hBitBuf->char_ptr++) << 8);
156 hBitBuf->buffer_word |= *(hBitBuf->char_ptr++);
157 hBitBuf->buffered_bits += 16;
160 hBitBuf->buffered_bits--;
161 hBitBuf->nrBitsRead++;
163 return ((hBitBuf->buffer_word >> hBitBuf->buffered_bits) & 1);