xref: /aosp_15_r20/external/libavc/decoder/ih264d_bitstrm.h (revision 495ae853bb871d1e5a258cb02c2cc13cde8ddb9a)
1*495ae853SAndroid Build Coastguard Worker /******************************************************************************
2*495ae853SAndroid Build Coastguard Worker  *
3*495ae853SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
4*495ae853SAndroid Build Coastguard Worker  *
5*495ae853SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
6*495ae853SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
7*495ae853SAndroid Build Coastguard Worker  * You may obtain a copy of the License at:
8*495ae853SAndroid Build Coastguard Worker  *
9*495ae853SAndroid Build Coastguard Worker  * http://www.apache.org/licenses/LICENSE-2.0
10*495ae853SAndroid Build Coastguard Worker  *
11*495ae853SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
12*495ae853SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
13*495ae853SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*495ae853SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
15*495ae853SAndroid Build Coastguard Worker  * limitations under the License.
16*495ae853SAndroid Build Coastguard Worker  *
17*495ae853SAndroid Build Coastguard Worker  *****************************************************************************
18*495ae853SAndroid Build Coastguard Worker  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
19*495ae853SAndroid Build Coastguard Worker */
20*495ae853SAndroid Build Coastguard Worker 
21*495ae853SAndroid Build Coastguard Worker #ifndef _IH264D_BITSTRM_H_
22*495ae853SAndroid Build Coastguard Worker #define _IH264D_BITSTRM_H_
23*495ae853SAndroid Build Coastguard Worker /*!
24*495ae853SAndroid Build Coastguard Worker  *************************************************************************
25*495ae853SAndroid Build Coastguard Worker  * \file ih264d_bitstrm.h
26*495ae853SAndroid Build Coastguard Worker  *
27*495ae853SAndroid Build Coastguard Worker  * \brief
28*495ae853SAndroid Build Coastguard Worker  *  Contains all the declarations of bitstream reading routines
29*495ae853SAndroid Build Coastguard Worker  *
30*495ae853SAndroid Build Coastguard Worker  * \date
31*495ae853SAndroid Build Coastguard Worker  *    20/11/2002
32*495ae853SAndroid Build Coastguard Worker  *
33*495ae853SAndroid Build Coastguard Worker  * \author  AI
34*495ae853SAndroid Build Coastguard Worker  *************************************************************************
35*495ae853SAndroid Build Coastguard Worker  */
36*495ae853SAndroid Build Coastguard Worker 
37*495ae853SAndroid Build Coastguard Worker /* Includes */
38*495ae853SAndroid Build Coastguard Worker #include <stdio.h>
39*495ae853SAndroid Build Coastguard Worker #include <stdlib.h>
40*495ae853SAndroid Build Coastguard Worker #include "ih264_typedefs.h"
41*495ae853SAndroid Build Coastguard Worker #include "ih264_macros.h"
42*495ae853SAndroid Build Coastguard Worker #include "ih264_platform_macros.h"
43*495ae853SAndroid Build Coastguard Worker 
44*495ae853SAndroid Build Coastguard Worker #define INT_IN_BYTES        4
45*495ae853SAndroid Build Coastguard Worker #define INT_IN_BITS         32
46*495ae853SAndroid Build Coastguard Worker 
47*495ae853SAndroid Build Coastguard Worker /* Based on level 1.2 of baseline profile */
48*495ae853SAndroid Build Coastguard Worker /* 396[MAX_FS] * 128 * 1.5 [ChromaFormatParameter] / sizeof(UWORD32)
49*495ae853SAndroid Build Coastguard Worker  i.e  396 * 128 * 1.5 / 4 = 19008 */
50*495ae853SAndroid Build Coastguard Worker /* Based on level 3 of main profile */
51*495ae853SAndroid Build Coastguard Worker /* 1620[MAX_FS] * 128 * 1.5 [ChromaFormatParameter] / sizeof(UWORD32)
52*495ae853SAndroid Build Coastguard Worker  i.e  1620 * 128 * 1.5 / 4= 77760 */
53*495ae853SAndroid Build Coastguard Worker #define SIZE_OF_BUFFER      77760
54*495ae853SAndroid Build Coastguard Worker 
55*495ae853SAndroid Build Coastguard Worker /* Structure for the ps_bitstrm */
56*495ae853SAndroid Build Coastguard Worker typedef struct
57*495ae853SAndroid Build Coastguard Worker {
58*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_ofst; /* Offset in the buffer for the current bit */
59*495ae853SAndroid Build Coastguard Worker     UWORD32 *pu4_buffer; /* Bitstream Buffer  */
60*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_max_ofst; /* points to first bit beyond the buffer */
61*495ae853SAndroid Build Coastguard Worker     void * pv_codec_handle; /* For Error Handling */
62*495ae853SAndroid Build Coastguard Worker } dec_bit_stream_t;
63*495ae853SAndroid Build Coastguard Worker 
64*495ae853SAndroid Build Coastguard Worker /* To read the next bit */
65*495ae853SAndroid Build Coastguard Worker UWORD8 ih264d_get_bit_h264(dec_bit_stream_t *);
66*495ae853SAndroid Build Coastguard Worker 
67*495ae853SAndroid Build Coastguard Worker /* To read the next specified number of bits */
68*495ae853SAndroid Build Coastguard Worker UWORD32 ih264d_get_bits_h264(dec_bit_stream_t *, UWORD32);
69*495ae853SAndroid Build Coastguard Worker 
70*495ae853SAndroid Build Coastguard Worker /* To see the next specified number of bits */
71*495ae853SAndroid Build Coastguard Worker UWORD32 ih264d_next_bits_h264(dec_bit_stream_t *, UWORD32);
72*495ae853SAndroid Build Coastguard Worker 
73*495ae853SAndroid Build Coastguard Worker /* To flush a specified number of bits*/
74*495ae853SAndroid Build Coastguard Worker WORD32 ih264d_flush_bits_h264(dec_bit_stream_t *, WORD32);
75*495ae853SAndroid Build Coastguard Worker 
76*495ae853SAndroid Build Coastguard Worker /*!
77*495ae853SAndroid Build Coastguard Worker  **************************************************************************
78*495ae853SAndroid Build Coastguard Worker  * \if Function name : MoreRbspData \endif
79*495ae853SAndroid Build Coastguard Worker  *
80*495ae853SAndroid Build Coastguard Worker  * \brief
81*495ae853SAndroid Build Coastguard Worker  *    Determines whether there is more data in RBSP or not.
82*495ae853SAndroid Build Coastguard Worker  *
83*495ae853SAndroid Build Coastguard Worker  * \param ps_bitstrm : Pointer to bitstream
84*495ae853SAndroid Build Coastguard Worker  *
85*495ae853SAndroid Build Coastguard Worker  * \return
86*495ae853SAndroid Build Coastguard Worker  *    Returns 1 if there is more data in RBSP before rbsp_trailing_bits().
87*495ae853SAndroid Build Coastguard Worker  *    Otherwise it returns FALSE.
88*495ae853SAndroid Build Coastguard Worker  **************************************************************************
89*495ae853SAndroid Build Coastguard Worker  */
90*495ae853SAndroid Build Coastguard Worker 
91*495ae853SAndroid Build Coastguard Worker 
92*495ae853SAndroid Build Coastguard Worker #define EXCEED_OFFSET(ps_bitstrm) \
93*495ae853SAndroid Build Coastguard Worker   (ps_bitstrm->u4_ofst > ps_bitstrm->u4_max_ofst)
94*495ae853SAndroid Build Coastguard Worker #define CHECK_BITS_SUFFICIENT(ps_bitstrm, bits_to_read) \
95*495ae853SAndroid Build Coastguard Worker   (ps_bitstrm->u4_ofst + bits_to_read <= ps_bitstrm->u4_max_ofst)
96*495ae853SAndroid Build Coastguard Worker #define MORE_RBSP_DATA(ps_bitstrm) \
97*495ae853SAndroid Build Coastguard Worker     CHECK_BITS_SUFFICIENT(ps_bitstrm, 1)
98*495ae853SAndroid Build Coastguard Worker 
99*495ae853SAndroid Build Coastguard Worker void GoToByteBoundary(dec_bit_stream_t * ps_bitstrm);
100*495ae853SAndroid Build Coastguard Worker UWORD8 ih264d_check_byte_aligned(dec_bit_stream_t * ps_bitstrm);
101*495ae853SAndroid Build Coastguard Worker 
102*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
103*495ae853SAndroid Build Coastguard Worker /* Define a macro for inlining of GETBIT:                                    */
104*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
105*495ae853SAndroid Build Coastguard Worker #define   GETBIT(u4_code, u4_offset, pu4_bitstream)                         \
106*495ae853SAndroid Build Coastguard Worker {                                                                           \
107*495ae853SAndroid Build Coastguard Worker     UWORD32 *pu4_buf =  (pu4_bitstream);                                    \
108*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_word_off = ((u4_offset) >> 5);                               \
109*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_bit_off = (u4_offset) & 0x1F;                                \
110*495ae853SAndroid Build Coastguard Worker     u4_code = pu4_buf[u4_word_off] << u4_bit_off;                           \
111*495ae853SAndroid Build Coastguard Worker     (u4_offset)++;                                                          \
112*495ae853SAndroid Build Coastguard Worker     u4_code = (u4_code >> 31);                                              \
113*495ae853SAndroid Build Coastguard Worker }
114*495ae853SAndroid Build Coastguard Worker 
115*495ae853SAndroid Build Coastguard Worker 
116*495ae853SAndroid Build Coastguard Worker 
117*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
118*495ae853SAndroid Build Coastguard Worker /* Define a macro for inlining of GETBITS: u4_no_bits shall not exceed 32    */
119*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
120*495ae853SAndroid Build Coastguard Worker #define     GETBITS(u4_code, u4_offset, pu4_bitstream, u4_no_bits)          \
121*495ae853SAndroid Build Coastguard Worker {                                                                           \
122*495ae853SAndroid Build Coastguard Worker     UWORD32 *pu4_buf =  (pu4_bitstream);                                    \
123*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_word_off = ((u4_offset) >> 5);                               \
124*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_bit_off = (u4_offset) & 0x1F;                                \
125*495ae853SAndroid Build Coastguard Worker     u4_code = pu4_buf[u4_word_off++] << u4_bit_off;                         \
126*495ae853SAndroid Build Coastguard Worker                                                                             \
127*495ae853SAndroid Build Coastguard Worker     if(u4_bit_off)                                                          \
128*495ae853SAndroid Build Coastguard Worker         u4_code |= (pu4_buf[u4_word_off] >> (INT_IN_BITS - u4_bit_off));    \
129*495ae853SAndroid Build Coastguard Worker     u4_code = u4_code >> (INT_IN_BITS - u4_no_bits);                        \
130*495ae853SAndroid Build Coastguard Worker     (u4_offset) += u4_no_bits;                                              \
131*495ae853SAndroid Build Coastguard Worker }                                                                           \
132*495ae853SAndroid Build Coastguard Worker                                                                             \
133*495ae853SAndroid Build Coastguard Worker 
134*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
135*495ae853SAndroid Build Coastguard Worker /* Define a macro for inlining of NEXTBITS                                   */
136*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
137*495ae853SAndroid Build Coastguard Worker #define     NEXTBITS(u4_word, u4_offset, pu4_bitstream, u4_no_bits)         \
138*495ae853SAndroid Build Coastguard Worker {                                                                           \
139*495ae853SAndroid Build Coastguard Worker     UWORD32 *pu4_buf =  (pu4_bitstream);                                    \
140*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_word_off = ((u4_offset) >> 5);                               \
141*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_bit_off = (u4_offset) & 0x1F;                                \
142*495ae853SAndroid Build Coastguard Worker     u4_word = pu4_buf[u4_word_off++] << u4_bit_off;                         \
143*495ae853SAndroid Build Coastguard Worker     if(u4_bit_off)                                                          \
144*495ae853SAndroid Build Coastguard Worker         u4_word |= (pu4_buf[u4_word_off] >> (INT_IN_BITS - u4_bit_off));    \
145*495ae853SAndroid Build Coastguard Worker     u4_word = u4_word >> (INT_IN_BITS - u4_no_bits);                        \
146*495ae853SAndroid Build Coastguard Worker }
147*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
148*495ae853SAndroid Build Coastguard Worker /* Define a macro for inlining of NEXTBITS_32                                */
149*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
150*495ae853SAndroid Build Coastguard Worker #define     NEXTBITS_32(u4_word, u4_offset, pu4_bitstream)                  \
151*495ae853SAndroid Build Coastguard Worker {                                                                           \
152*495ae853SAndroid Build Coastguard Worker     UWORD32 *pu4_buf =  (pu4_bitstream);                                    \
153*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_word_off = ((u4_offset) >> 5);                               \
154*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_bit_off = (u4_offset) & 0x1F;                                \
155*495ae853SAndroid Build Coastguard Worker                                                                             \
156*495ae853SAndroid Build Coastguard Worker     u4_word = pu4_buf[u4_word_off++] << u4_bit_off;                         \
157*495ae853SAndroid Build Coastguard Worker     if(u4_bit_off)                                                          \
158*495ae853SAndroid Build Coastguard Worker     u4_word |= (pu4_buf[u4_word_off] >> (INT_IN_BITS - u4_bit_off));        \
159*495ae853SAndroid Build Coastguard Worker }
160*495ae853SAndroid Build Coastguard Worker 
161*495ae853SAndroid Build Coastguard Worker 
162*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
163*495ae853SAndroid Build Coastguard Worker /* Define a macro for inlining of FIND_ONE_IN_STREAM_32                      */
164*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
165*495ae853SAndroid Build Coastguard Worker #define   FIND_ONE_IN_STREAM_32(u4_ldz, u4_offset, pu4_bitstream)           \
166*495ae853SAndroid Build Coastguard Worker {                                                                           \
167*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_word;                                                        \
168*495ae853SAndroid Build Coastguard Worker     NEXTBITS_32(u4_word, u4_offset, pu4_bitstream);                         \
169*495ae853SAndroid Build Coastguard Worker     u4_ldz = CLZ(u4_word);                                     \
170*495ae853SAndroid Build Coastguard Worker     (u4_offset) += (u4_ldz + 1);                                            \
171*495ae853SAndroid Build Coastguard Worker }
172*495ae853SAndroid Build Coastguard Worker 
173*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
174*495ae853SAndroid Build Coastguard Worker /* Define a macro for inlining of FIND_ONE_IN_STREAM_LEN                     */
175*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
176*495ae853SAndroid Build Coastguard Worker #define   FIND_ONE_IN_STREAM_LEN(u4_ldz, u4_offset, pu4_bitstream, u4_len)  \
177*495ae853SAndroid Build Coastguard Worker {                                                                           \
178*495ae853SAndroid Build Coastguard Worker     UWORD32 u4_word;                                                        \
179*495ae853SAndroid Build Coastguard Worker     NEXTBITS_32(u4_word, u4_offset, pu4_bitstream);                         \
180*495ae853SAndroid Build Coastguard Worker     u4_ldz = CLZ(u4_word);                                     \
181*495ae853SAndroid Build Coastguard Worker     if(u4_ldz < u4_len)                                                     \
182*495ae853SAndroid Build Coastguard Worker     (u4_offset) += (u4_ldz + 1);                                            \
183*495ae853SAndroid Build Coastguard Worker     else                                                                    \
184*495ae853SAndroid Build Coastguard Worker     {                                                                       \
185*495ae853SAndroid Build Coastguard Worker         u4_ldz = u4_len;                                                    \
186*495ae853SAndroid Build Coastguard Worker         (u4_offset) += u4_ldz;                                              \
187*495ae853SAndroid Build Coastguard Worker     }                                                                       \
188*495ae853SAndroid Build Coastguard Worker }
189*495ae853SAndroid Build Coastguard Worker 
190*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
191*495ae853SAndroid Build Coastguard Worker /* Define a macro for inlining of FLUSHBITS                                  */
192*495ae853SAndroid Build Coastguard Worker /*****************************************************************************/
193*495ae853SAndroid Build Coastguard Worker #define   FLUSHBITS(u4_offset, u4_no_bits)                                  \
194*495ae853SAndroid Build Coastguard Worker {                                                                           \
195*495ae853SAndroid Build Coastguard Worker         (u4_offset) += (u4_no_bits);                                        \
196*495ae853SAndroid Build Coastguard Worker }
197*495ae853SAndroid Build Coastguard Worker 
198*495ae853SAndroid Build Coastguard Worker #endif  /* _BITSTREAM_H_ */
199