summaryrefslogtreecommitdiffstats
path: root/decoder/ih264d_bitstrm.c
blob: fd41bc6c705adbd235afa099598c52d316dfe4f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/******************************************************************************
 *
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *****************************************************************************
 * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
*/

/*!
 **************************************************************************
 * \file ih264d_bitstrm.c
 *
 * \brief
 *    Bitstream parsing routines
 *
 * \date
 *    20/11/2002
 *
 * \author  AI
 **************************************************************************
 */

#include <stdlib.h>
#include "ih264_typedefs.h"
#include "ih264_macros.h"
#include "ih264_platform_macros.h"
#include "ih264d_bitstrm.h"
#include "ih264d_error_handler.h"

#include "ih264d_debug.h"
#include "ih264d_tables.h"
#include "ih264d_structs.h"

/*!
 **************************************************************************
 * \if Function name : ih264d_get_bit_h264 \endif
 *
 * \brief
 *    Read one bit from the bitstream.
 *
 *   This is a Bitstream processing function. It reads the
 *   bit currently pointed by the bit pointer in the
 *   buffer and advances the pointer by one. It returns
 *   the bit (0 or 1) in the form of an unsigned integer.
 *
 * \return
 *    Returns the next bit (0 or 1) in the bitstream.
 *
 **************************************************************************
 */
UWORD8 ih264d_get_bit_h264(dec_bit_stream_t *ps_stream)
{
    UWORD32 u4_code;

    GETBIT(u4_code, ps_stream->u4_ofst, ps_stream->pu4_buffer);
    return (u4_code);
}

/*!
 **************************************************************************
 * \if Function name : ih264d_get_bits_h264 \endif
 *
 * \brief
 *    Read specified number of bits from the bitstream.
 *
 *   This is a Bitstream processing function. It reads the
 *   number specified number of bits from the current bit
 *   position and advances the bit and byte pointers
 *   appropriately.
 *
 * \return
 *    An unsigned 32 bit integer with its least significant bits
 *    containing the bits in order of their occurence in the bitstream.
 *
 **************************************************************************
 */

UWORD32 ih264d_get_bits_h264(dec_bit_stream_t *ps_bitstrm, UWORD32 u4_num_bits)
{
    UWORD32 u4_code = 0;
    if(u4_num_bits)
        GETBITS(u4_code, ps_bitstrm->u4_ofst, ps_bitstrm->pu4_buffer, u4_num_bits);
    return (u4_code);
}

/*!
 **************************************************************************
 * \if Function name : ih264d_next_bits_h264 \endif
 *
 * \brief
 *    Peek specified number of bits from the bitstream.
 *
 *   This is a Bitstream processing function. It gets the
 *   specified number of bits from the buffer without
 *   altering the current pointers. It is equivalent to
 *   next_bits() function in the standard.
 *
 * \return
 *    An unsigned 32 bit integer with its least significant bits
 *    containing the bits in order of their occurence in the bitstream.
 **************************************************************************
 */
UWORD32 ih264d_next_bits_h264(dec_bit_stream_t *ps_bitstrm, UWORD32 u4_num_bits)
{
    UWORD32 u4_word_off = (ps_bitstrm->u4_ofst >> 5);
    UWORD32 u4_bit_off = ps_bitstrm->u4_ofst & 0x1F;
    UWORD32 *pu4_bitstream = ps_bitstrm->pu4_buffer;
    UWORD32 u4_bits = pu4_bitstream[u4_word_off++] << u4_bit_off;

    /*************************************************************************/
    /* Test if number of bits to be read exceeds the number of bits in the   */
    /* current word. If yes, read from the next word of the buffer, The bits */
    /* from both the words are concatenated to get next 32 bits in 'u4_bits' */
    /*************************************************************************/
    if(u4_bit_off > (INT_IN_BITS - u4_num_bits))
        u4_bits |= (pu4_bitstream[u4_word_off] >> (INT_IN_BITS - u4_bit_off));

    return ((u4_bits >> (INT_IN_BITS - u4_num_bits)));
}

/*!
 **************************************************************************
 * \if Function name : ih264d_flush_bits_h264 \endif
 *
 * \brief
 *    Flush specified number of bits from the bitstream.
 *
 *   This function flushes the specified number of bits (marks
 *   as read) from the buffer.
 *
 * \return
 *     A 8 bit unsigned integer with value
 *    '1' on successful flush
 *    '0' on failure.
 *
 **************************************************************************
 */
WORD32 ih264d_flush_bits_h264(dec_bit_stream_t *ps_bitstrm, WORD32 u4_num_bits)
{
    ps_bitstrm->u4_ofst += u4_num_bits;

    if(ps_bitstrm->u4_ofst > ps_bitstrm->u4_max_ofst)
    {
        return ERROR_EOB_FLUSHBITS_T;
    }
    return OK;
}

/*!
 **************************************************************************
 * \if Function name : ih264d_check_byte_aligned \endif
 *
 * \brief
 *    Checks whether the bit ps_bitstrm u4_ofst is at byte boundary.
 *
 * \param ps_bitstrm : Pointer to bitstream
 *
 * \return
 *    Returns 1 if bit ps_bitstrm u4_ofst is at byte alligned position else zero.
 **************************************************************************
 */

UWORD8 ih264d_check_byte_aligned(dec_bit_stream_t * ps_bitstrm)
{
    if(ps_bitstrm->u4_ofst & 0x07)
        return (0);
    else
        return (1);
}