1 /******************************************************************************
2 *
3 * Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
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 /**
19 *******************************************************************************
20 * @file
21 *  ihevcd_bitps_bitstrm.h
22 *
23 * @brief
24 *  Header for bitps_bitstrm access functions
25 *
26 * @author
27 *  Harish
28 *
29 * @par List of Functions:
30 *
31 * @remarks
32 *  None
33 *
34 *******************************************************************************
35 */
36 
37 #ifndef _IHEVCD_BITSTREAM_H_
38 #define _IHEVCD_BITSTREAM_H_
39 /**
40  *  @brief  defines the maximum number of bits in a bitstream word
41  */
42 #define WORD_SIZE         32
43 /**
44  *  @brief  Twice the WORD_SIZE
45  */
46 #define DBL_WORD_SIZE     (2 * (WORD_SIZE))
47 
48 /**
49  *  @brief  WORD_SIZE - 1
50  */
51 #define WORD_SIZE_MINUS1  (WORD_SIZE - 1)
52 
53 /**
54 ******************************************************************************
55 * @brief Macro used to copy elements in bistream structure to local variables.
56 ******************************************************************************
57 */
58 
59 #define GET_STREAM(m_ps_bitstrm, m_pu4_buf, m_u4_bit_ofst,  \
60                   m_u4_cur_word, m_u4_nxt_word)             \
61 {                                                           \
62     m_pu4_buf            = m_ps_bitstrm->pu4_buf;           \
63     m_u4_bit_ofst        = m_ps_bitstrm->u4_bit_ofst;       \
64     m_u4_cur_word        = m_ps_bitstrm->u4_cur_word;       \
65     m_u4_nxt_word        = m_ps_bitstrm->u4_nxt_word;       \
66 }
67 
68 /**
69 ******************************************************************************
70 * @brief Macro used to copy local variables to elements in bistream structure.
71 ******************************************************************************
72 */
73 #define SET_STREAM(m_ps_bitstrm, m_pu4_buf, m_u4_bit_ofst,  \
74                   m_u4_cur_word, m_u4_nxt_word)             \
75 {                                                           \
76     m_ps_bitstrm->pu4_buf       = m_pu4_buf;                \
77     m_ps_bitstrm->u4_bit_ofst   = m_u4_bit_ofst;            \
78     m_ps_bitstrm->u4_cur_word   = m_u4_cur_word;            \
79     m_ps_bitstrm->u4_nxt_word   = m_u4_nxt_word;            \
80 }
81 
82 
83 
84 /**
85 ******************************************************************************
86 * @brief  Snoop next m_cnt bits without updating offsets or buffer increments.
87 * Data is not consumed in this call
88 ******************************************************************************
89 */
90 #define BITS_NXT(m_u4_bits, m_pu4_buf, m_u4_bit_ofst,       \
91                  m_u4_cur_word, m_u4_nxt_word, m_cnt)       \
92 {                                                           \
93     m_u4_bits = (m_u4_cur_word << m_u4_bit_ofst)  >>        \
94                               (WORD_SIZE - m_cnt);          \
95                                                             \
96     if(m_u4_bit_ofst > (WORD_SIZE - m_cnt))                 \
97     {                                                       \
98         m_u4_bits |= SHR(m_u4_nxt_word,                     \
99                    (WORD_SIZE + WORD_SIZE - m_cnt           \
100                           - m_u4_bit_ofst));                \
101     }                                                       \
102 }
103 
104 
105 /**
106 ******************************************************************************
107 *  @brief Snoop next 32 bits without updating offsets or buffer increments.
108 * Data is not consumed in this call
109 ******************************************************************************
110 */
111 #define BITS_NXT32(m_u4_bits, m_pu4_buf, m_u4_bit_ofst,             \
112                  m_u4_cur_word, m_u4_nxt_word)                      \
113 {                                                                   \
114     m_u4_bits = (m_u4_cur_word << m_u4_bit_ofst);                   \
115                                                                     \
116     m_u4_bits |= SHR(m_u4_nxt_word, (WORD_SIZE - m_u4_bit_ofst));   \
117 }
118 
119 
120 /**
121 ******************************************************************************
122 *  @brief  Flush m_u4_bits and updated the buffer pointer.
123 * Data is consumed
124 ******************************************************************************
125 */
126 #define BITS_FLUSH(m_pu4_buf, m_u4_bit_ofst, m_u4_cur_word, \
127                     m_u4_nxt_word, m_cnt)                   \
128 {                                                           \
129     UWORD32 temp;                                           \
130                                                             \
131     m_u4_bit_ofst += m_cnt;                                 \
132     if( m_u4_bit_ofst >=   WORD_SIZE )                      \
133     {                                                       \
134         m_u4_cur_word  = m_u4_nxt_word;                     \
135         /* Getting the next word */                         \
136         temp = *(m_pu4_buf++);                              \
137                                                             \
138         m_u4_bit_ofst -= WORD_SIZE;                         \
139         /* Swapping little endian to big endian conversion*/\
140         m_u4_nxt_word = ITT_BIG_ENDIAN(temp);                   \
141     }                                                       \
142 }
143 /**
144 ******************************************************************************
145 *  @brief Get m_cnt number of bits and update bffer pointers and offset.
146 * Data is consumed
147 ******************************************************************************
148 */
149 #define BITS_GET(m_u4_bits, m_pu4_buf, m_u4_bit_ofst,           \
150                           m_u4_cur_word,m_u4_nxt_word, m_cnt)   \
151 {                                                               \
152     m_u4_bits = (m_u4_cur_word << m_u4_bit_ofst)                \
153                              >> (WORD_SIZE - m_cnt);            \
154     m_u4_bit_ofst += m_cnt;                                     \
155     if(m_u4_bit_ofst > WORD_SIZE)                               \
156     {                                                           \
157         m_u4_bits |= SHR(m_u4_nxt_word,                         \
158                      (DBL_WORD_SIZE - m_u4_bit_ofst));          \
159     }                                                           \
160                                                                 \
161     if( m_u4_bit_ofst >=   WORD_SIZE )                          \
162     {                                                           \
163         UWORD32 pu4_word_tmp;                                   \
164         m_u4_cur_word  = m_u4_nxt_word;                         \
165         /* Getting the next word */                             \
166         pu4_word_tmp = *(m_pu4_buf++);                          \
167                                                                 \
168         m_u4_bit_ofst -= WORD_SIZE;                             \
169         /* Swapping little endian to big endian conversion*/    \
170         m_u4_nxt_word  = ITT_BIG_ENDIAN(pu4_word_tmp);              \
171     }                                                           \
172 }
173 
174 /**
175 ******************************************************************************
176 *  @brief Get 1 bit and update buffer pointers and offset.
177 * Data is consumed
178 ******************************************************************************
179 */
180 
181 #define BIT_GET(m_u4_bits,m_pu4_buf,m_u4_bit_ofst,              \
182                           m_u4_cur_word,m_u4_nxt_word)          \
183 {                                                               \
184     m_u4_bits = (m_u4_cur_word << m_u4_bit_ofst)                \
185                              >> (WORD_SIZE_MINUS1);             \
186     m_u4_bit_ofst++;                                            \
187                                                                 \
188     if(m_u4_bit_ofst ==  WORD_SIZE)                             \
189     {                                                           \
190         UWORD32 pu4_word_tmp;                                   \
191         m_u4_cur_word  = m_u4_nxt_word;                         \
192         /* Getting the next word */                             \
193         pu4_word_tmp = *m_pu4_buf++;                            \
194                                                                 \
195         m_u4_bit_ofst = 0;                                      \
196         /* Swapping little endian to big endian conversion*/    \
197         m_u4_nxt_word  = ITT_BIG_ENDIAN(pu4_word_tmp);              \
198     }                                                           \
199 }
200 
201 void ihevcd_bits_init(bitstrm_t *ps_bitstrm,
202                       UWORD8 *pu1_buf,
203                       UWORD32 u4_numbytes);
204 void ihevcd_bits_flush(bitstrm_t *ps_bitstrm, UWORD32 u4_numbits);
205 
206 void ihevcd_bits_flush_to_byte_boundary(bitstrm_t *ps_bitstrm);
207 
208 UWORD32 ihevcd_bits_nxt(bitstrm_t *ps_bitstrm, UWORD32 u4_numbits);
209 
210 UWORD32 ihevcd_bits_nxt32(bitstrm_t *ps_bitstrm, UWORD32 u4_numbits);
211 
212 
213 UWORD32 ihevcd_bits_get(bitstrm_t *ps_bitstrm, UWORD32 u4_numbits);
214 
215 UWORD32  ihevcd_bits_num_bits_remaining(bitstrm_t *ps_bitstrm);
216 
217 
218 UWORD32  ihevcd_bits_num_bits_consumed(bitstrm_t *ps_bitstrm);
219 
220 UWORD32 ihevcd_uev(bitstrm_t *ps_bitstrm);
221 
222 WORD32 ihevcd_sev(bitstrm_t *ps_bitstrm);
223 
224 void ihevcd_bits_seek(bitstrm_t *ps_bitstrm, WORD32 numbits);
225 
226 #endif /* _IHEVCD_BITSTREAM_H_ */
227