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 **************************************************************************
23 * \file ih264d_bitstrm.c
24 *
25 * \brief
26 *    Bitstream parsing routines
27 *
28 * \date
29 *    20/11/2002
30 *
31 * \author  AI
32 **************************************************************************
33 */
34
35#include <stdlib.h>
36#include "ih264_typedefs.h"
37#include "ih264_macros.h"
38#include "ih264_platform_macros.h"
39#include "ih264d_bitstrm.h"
40#include "ih264d_error_handler.h"
41
42#include "ih264d_debug.h"
43#include "ih264d_tables.h"
44#include "ih264d_structs.h"
45
46/*!
47 **************************************************************************
48 * \if Function name : ih264d_get_bit_h264 \endif
49 *
50 * \brief
51 *    Read one bit from the bitstream.
52 *
53 *   This is a Bitstream processing function. It reads the
54 *   bit currently pointed by the bit pointer in the
55 *   buffer and advances the pointer by one. It returns
56 *   the bit (0 or 1) in the form of an unsigned integer.
57 *
58 * \return
59 *    Returns the next bit (0 or 1) in the bitstream.
60 *
61 **************************************************************************
62 */
63UWORD8 ih264d_get_bit_h264(dec_bit_stream_t *ps_stream)
64{
65    UWORD32 u4_code;
66
67    GETBIT(u4_code, ps_stream->u4_ofst, ps_stream->pu4_buffer);
68    return (u4_code);
69}
70
71/*!
72 **************************************************************************
73 * \if Function name : ih264d_get_bits_h264 \endif
74 *
75 * \brief
76 *    Read specified number of bits from the bitstream.
77 *
78 *   This is a Bitstream processing function. It reads the
79 *   number specified number of bits from the current bit
80 *   position and advances the bit and byte pointers
81 *   appropriately.
82 *
83 * \return
84 *    An unsigned 32 bit integer with its least significant bits
85 *    containing the bits in order of their occurence in the bitstream.
86 *
87 **************************************************************************
88 */
89
90UWORD32 ih264d_get_bits_h264(dec_bit_stream_t *ps_bitstrm, UWORD32 u4_num_bits)
91{
92    UWORD32 u4_code = 0;
93    if(u4_num_bits)
94        GETBITS(u4_code, ps_bitstrm->u4_ofst, ps_bitstrm->pu4_buffer, u4_num_bits);
95    return (u4_code);
96}
97
98/*!
99 **************************************************************************
100 * \if Function name : ih264d_next_bits_h264 \endif
101 *
102 * \brief
103 *    Peek specified number of bits from the bitstream.
104 *
105 *   This is a Bitstream processing function. It gets the
106 *   specified number of bits from the buffer without
107 *   altering the current pointers. It is equivalent to
108 *   next_bits() function in the standard.
109 *
110 * \return
111 *    An unsigned 32 bit integer with its least significant bits
112 *    containing the bits in order of their occurence in the bitstream.
113 **************************************************************************
114 */
115UWORD32 ih264d_next_bits_h264(dec_bit_stream_t *ps_bitstrm, UWORD32 u4_num_bits)
116{
117    UWORD32 u4_word_off = (ps_bitstrm->u4_ofst >> 5);
118    UWORD32 u4_bit_off = ps_bitstrm->u4_ofst & 0x1F;
119    UWORD32 *pu4_bitstream = ps_bitstrm->pu4_buffer;
120    UWORD32 u4_bits = pu4_bitstream[u4_word_off++] << u4_bit_off;
121
122    /*************************************************************************/
123    /* Test if number of bits to be read exceeds the number of bits in the   */
124    /* current word. If yes, read from the next word of the buffer, The bits */
125    /* from both the words are concatenated to get next 32 bits in 'u4_bits' */
126    /*************************************************************************/
127    if(u4_bit_off > (INT_IN_BITS - u4_num_bits))
128        u4_bits |= (pu4_bitstream[u4_word_off] >> (INT_IN_BITS - u4_bit_off));
129
130    return ((u4_bits >> (INT_IN_BITS - u4_num_bits)));
131}
132
133/*!
134 **************************************************************************
135 * \if Function name : ih264d_flush_bits_h264 \endif
136 *
137 * \brief
138 *    Flush specified number of bits from the bitstream.
139 *
140 *   This function flushes the specified number of bits (marks
141 *   as read) from the buffer.
142 *
143 * \return
144 *     A 8 bit unsigned integer with value
145 *    '1' on successful flush
146 *    '0' on failure.
147 *
148 **************************************************************************
149 */
150WORD32 ih264d_flush_bits_h264(dec_bit_stream_t *ps_bitstrm, WORD32 u4_num_bits)
151{
152    ps_bitstrm->u4_ofst += u4_num_bits;
153
154    if(ps_bitstrm->u4_ofst > ps_bitstrm->u4_max_ofst)
155    {
156        return ERROR_EOB_FLUSHBITS_T;
157    }
158    return OK;
159}
160
161/*!
162 **************************************************************************
163 * \if Function name : ih264d_check_byte_aligned \endif
164 *
165 * \brief
166 *    Checks whether the bit ps_bitstrm u4_ofst is at byte boundary.
167 *
168 * \param ps_bitstrm : Pointer to bitstream
169 *
170 * \return
171 *    Returns 1 if bit ps_bitstrm u4_ofst is at byte alligned position else zero.
172 **************************************************************************
173 */
174
175UWORD8 ih264d_check_byte_aligned(dec_bit_stream_t * ps_bitstrm)
176{
177    if(ps_bitstrm->u4_ofst & 0x07)
178        return (0);
179    else
180        return (1);
181}
182