1 /******************************************************************************
2 *
3 * Copyright (C) 2015 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************
18 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19 */
20 /*****************************************************************************/
21 /* */
22 /* File Name : impeg2d_bitstream.c */
23 /* */
24 /* Description : This file contains all the necessary examples to */
25 /* establish a consistent use of Ittiam C coding */
26 /* standards (based on Indian Hill C Standards) */
27 /* */
28 /* List of Functions : <List the functions defined in this file> */
29 /* */
30 /* Issues / Problems : None */
31 /* */
32 /* Revision History : */
33 /* */
34 /* DD MM YYYY Author(s) Changes (Describe the changes made) */
35 /* 10 01 2005 Ittiam Draft */
36 /* */
37 /*****************************************************************************/
38 #include <stdlib.h>
39
40 #include "iv_datatypedef.h"
41 #include "impeg2_defs.h"
42 #include "impeg2_platform_macros.h"
43 #include "impeg2_macros.h"
44 #include "impeg2d_bitstream.h"
45
46 #define BIT(val,bit) (UWORD16)(((val) >> (bit)) & 0x1)
47 /******************************************************************************
48 *
49 * Function Name : impeg2d_bit_stream_init
50 *
51 * Description : This is a Bitstream initialising function.
52 * Arguments :
53 * stream : Pointer to the Bitstream.
54 * byteBuf : Address of the buffer
55 * size : Size of the buffer in bytes
56 *
57 * Values Returned : None
58 *******************************************************************************/
impeg2d_bit_stream_init(stream_t * ps_stream,UWORD8 * pu1_byte_buf,UWORD32 u4_max_offset)59 void impeg2d_bit_stream_init(stream_t *ps_stream,
60 UWORD8 *pu1_byte_buf,
61 UWORD32 u4_max_offset)
62 {
63 UWORD8 *pu1_byte_buff;
64 UWORD32 *pu4_word_buf;
65 size_t u4_byte_addr;
66 UWORD32 u4_temp1,u4_temp2;
67
68 /* Set parameters of the stream structure.Associate the structure with
69 the file */
70 ps_stream->pv_bs_buf = pu1_byte_buf;
71 ps_stream->u4_offset = 0;
72
73 /* Take care of unaligned address and create
74 nearest greater aligned address */
75 pu1_byte_buff = (UWORD8 *)pu1_byte_buf;
76 u4_byte_addr = (size_t)pu1_byte_buff;
77
78 if((u4_byte_addr & 3) == 1)
79 {
80 u4_temp1 = ((UWORD32)(*pu1_byte_buff++)) << 8;
81 u4_temp1 += ((UWORD32)(*pu1_byte_buff++)) << 16;
82 u4_temp1 += ((UWORD32)(*pu1_byte_buff++)) << 24;
83
84 pu4_word_buf = (UWORD32 *)pu1_byte_buff;
85
86 ps_stream->u4_offset = 8;
87 }
88 else if((u4_byte_addr & 3) == 2)
89 {
90 u4_temp1 = ((UWORD32)(*pu1_byte_buff++)) << 16;
91 u4_temp1 += ((UWORD32)(*pu1_byte_buff++)) << 24;
92
93 pu4_word_buf = (UWORD32 *)pu1_byte_buff;
94
95 ps_stream->u4_offset = 16;
96 }
97 else if((u4_byte_addr & 3) == 3)
98 {
99 u4_temp1 = (((UWORD32)(*pu1_byte_buff++)) << 24);
100
101 pu4_word_buf = (UWORD32 *)pu1_byte_buff;
102
103 ps_stream->u4_offset = 24;
104 }
105 else
106 {
107 pu4_word_buf = (UWORD32 *)pu1_byte_buff;
108
109 u4_temp1 = *pu4_word_buf++;
110 ps_stream->u4_offset = 0;
111 }
112
113 /* convert the endian ness from Little endian to Big endian so that bits
114 are in proper order from MSB to LSB */
115 CONV_LE_TO_BE(u4_temp2,u4_temp1)
116
117 /* Read One more word for buf nxt */
118 u4_temp1 = *pu4_word_buf++;
119 ps_stream->u4_buf = u4_temp2;
120
121 CONV_LE_TO_BE(u4_temp2,u4_temp1)
122
123 ps_stream->u4_buf_nxt = u4_temp2;
124
125 ps_stream->pu4_buf_aligned = pu4_word_buf;
126
127
128 ps_stream->u4_max_offset = (u4_max_offset << 3) + ps_stream->u4_offset;
129
130 return;
131 }
132
133
134
135 /******************************************************************************
136 *
137 * Function Name : impeg2d_bit_stream_get_bit
138 *
139 * Description : This is a Bitstream processing function. It reads the
140 * bit currently pointed by the bit pointer in the buffer and
141 * advances the pointer by one.
142 * Arguments :
143 * stream : Pointer to the Bitstream.
144 *
145 * Values Returned : The bit read(0/1)
146 *******************************************************************************/
impeg2d_bit_stream_get_bit(stream_t * ps_stream)147 INLINE UWORD8 impeg2d_bit_stream_get_bit(stream_t *ps_stream)
148 {
149 UWORD32 u4_bit,u4_offset,u4_temp;
150 UWORD32 u4_curr_bit;
151
152 u4_offset = ps_stream->u4_offset;
153 u4_curr_bit = u4_offset & 0x1F;
154 u4_bit = ps_stream->u4_buf;
155
156 /* Move the current bit read from the current word to the
157 least significant bit positions of 'c'.*/
158 u4_bit >>= BITS_IN_INT - u4_curr_bit - 1;
159
160 u4_offset++;
161
162 /* If the last bit of the last word of the buffer has been read update
163 the currrent buf with next, and read next buf from bit stream buffer */
164 if (u4_curr_bit == 31)
165 {
166 ps_stream->u4_buf = ps_stream->u4_buf_nxt;
167 u4_temp = *(ps_stream->pu4_buf_aligned)++;
168
169 CONV_LE_TO_BE(ps_stream->u4_buf_nxt,u4_temp)
170 }
171 ps_stream->u4_offset = u4_offset;
172
173 return (u4_bit & 0x1);
174 }
175 /******************************************************************************
176 *
177 * Function Name : impeg2d_bit_stream_flush
178 *
179 * Description : This is a Bitstream processing function. It
180 * advances the bit and byte pointers appropriately
181 *
182 * Arguments :
183 * ctxt : Pointer to the Bitstream.
184 * numBits : No of bits to be read
185 *
186 * Values Returned : None
187 *******************************************************************************/
impeg2d_bit_stream_flush(void * pv_ctxt,UWORD32 u4_no_of_bits)188 INLINE void impeg2d_bit_stream_flush(void* pv_ctxt, UWORD32 u4_no_of_bits)
189 {
190 stream_t *ps_stream = (stream_t *)pv_ctxt;
191
192 FLUSH_BITS(ps_stream->u4_offset,ps_stream->u4_buf,ps_stream->u4_buf_nxt,u4_no_of_bits,ps_stream->pu4_buf_aligned)
193 return;
194 }
195 /******************************************************************************
196 *
197 * Function Name : impeg2d_bit_stream_flush_to_byte_boundary
198 *
199 * Description : This is a Bitstream processing function.It advances
200 * the bit and byte pointers to next byte boundary
201 *
202 * Arguments :
203 * stream : Pointer to the Bitstream.
204 * NoOfBits : No of bits to be read
205 *
206 * Values Returned : The bits read (upto 32 bits maximum) starting from the
207 * least significant bit and going towards most significant
208 * bit in the order of their occurence.
209 *******************************************************************************/
impeg2d_bit_stream_flush_to_byte_boundary(void * pv_ctxt)210 INLINE void impeg2d_bit_stream_flush_to_byte_boundary(void* pv_ctxt)
211 {
212 UWORD8 u1_bit_offset;
213 stream_t *ps_stream = (stream_t *)pv_ctxt;
214
215 u1_bit_offset = (ps_stream->u4_offset) & 0x7;
216
217
218 /* if it is not byte aligned make it byte aligned*/
219 if(u1_bit_offset != 0)
220 {
221 impeg2d_bit_stream_flush(ps_stream,(8 - u1_bit_offset));
222 }
223
224
225
226 }
227
228
229 /******************************************************************************
230 *
231 * Function Name : ibits_next
232 *
233 * Description : This is a Bitstream processing function.It gets the
234 * specified number of bits from the buffer without
235 * altering the current pointers. It is used mainly to
236 * check for some specific pattern of bits like start
237 * code. This is equivalent to next_bits() function
238 * defined in MPEG-4 Visual Standard Definition of functions
239 *
240 * Arguments :
241 * ctxt : Pointer to the Bitstream.
242 * numBits : No of bits to be read
243 *
244 * Values Returned : The bits read (upto 32 bits maximum) starting from the
245 * least significant bit and going towards most significant
246 * bit in the order of their occurence.
247 *******************************************************************************/
impeg2d_bit_stream_nxt(stream_t * ps_stream,WORD32 i4_no_of_bits)248 INLINE UWORD32 impeg2d_bit_stream_nxt( stream_t *ps_stream, WORD32 i4_no_of_bits)
249 {
250 UWORD32 u4_bits,u4_offset,u4_temp;
251 UWORD8 u4_bit_ptr;
252
253 ASSERT(i4_no_of_bits > 0);
254
255 u4_offset = ps_stream->u4_offset;
256 u4_bit_ptr = u4_offset & 0x1F;
257 u4_bits = ps_stream->u4_buf << u4_bit_ptr;
258
259 u4_bit_ptr += i4_no_of_bits;
260 if(32 < u4_bit_ptr)
261 {
262 /* Read bits from the next word if necessary */
263 u4_temp = ps_stream->u4_buf_nxt;
264 u4_bit_ptr &= (BITS_IN_INT - 1);
265
266 u4_temp = (u4_temp >> (BITS_IN_INT - u4_bit_ptr));
267
268 /* u4_temp consists of bits,if any that had to be read from the next word
269 of the buffer.The bits read from both the words are concatenated and
270 moved to the least significant positions of 'u4_bits'*/
271 u4_bits = (u4_bits >> (32 - i4_no_of_bits)) | u4_temp;
272 }
273 else
274 {
275 u4_bits = (u4_bits >> (32 - i4_no_of_bits));
276 }
277
278 return (u4_bits);
279 }
280 /******************************************************************************
281 *
282 * Function Name : impeg2d_bit_stream_get
283 *
284 * Description : This is a Bitstream processing function. It reads a
285 * specified number of bits from the current bit
286 * position and advances the bit and byte pointers
287 * appropriately
288 * Arguments :
289 * ctxt : Pointer to the Bitstream.
290 * numBits : No of bits to be read
291 *
292 * Values Returned : The bits read (upto 32 bits maximum) starting from the
293 * least significant bit and going towards most significant
294 * bit in the order of their occurence.
295 *******************************************************************************/
296
impeg2d_bit_stream_get(void * pv_ctxt,UWORD32 u4_num_bits)297 INLINE UWORD32 impeg2d_bit_stream_get(void* pv_ctxt, UWORD32 u4_num_bits)
298 {
299 UWORD32 u4_next_bits = impeg2d_bit_stream_nxt(pv_ctxt, u4_num_bits);
300 impeg2d_bit_stream_flush(pv_ctxt, u4_num_bits);
301 return(u4_next_bits);
302 }
303
304
305
306 /******************************************************************************
307 *
308 * Function Name : impeg2d_bit_stream_num_bits_read
309 *
310 * Description : This is a Bitstream processing function. It reads a
311 * specified number of bits from the current bit
312 * position and advances the bit and byte pointers
313 * appropriately
314 * Arguments :
315 * ctxt : Pointer to the Bitstream.
316 * numBits : No of bits to be read
317 *
318 * Values Returned : The bits read (upto 16 bits maximum) starting from the
319 * least significant bit and going towards most significant
320 * bit in the order of their occurence.
321 *******************************************************************************/
impeg2d_bit_stream_num_bits_read(void * pv_ctxt)322 INLINE UWORD32 impeg2d_bit_stream_num_bits_read(void* pv_ctxt)
323 {
324 stream_t *u4_no_of_bitsstream = (stream_t *)pv_ctxt;
325 size_t u4_temp;
326 UWORD32 u4_bits_read;
327 u4_temp = (size_t)(u4_no_of_bitsstream->pv_bs_buf);
328 u4_temp &= 0x3;
329 u4_bits_read = (u4_no_of_bitsstream->u4_offset - (u4_temp << 3));
330
331 return(u4_bits_read);
332
333 }
334
335
336