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