summaryrefslogtreecommitdiff
path: root/decoder/impeg2d_bitstream.c
blob: 57a9e2f61ce7d47c41fcf1596112a50be2064628 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/******************************************************************************
 *
 * 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 Name         : impeg2d_bitstream.c                                  */
/*                                                                           */
/*  Description       : This file contains all the necessary examples to     */
/*                      establish a consistent use of Ittiam C coding        */
/*                      standards (based on Indian Hill C Standards)         */
/*                                                                           */
/*  List of Functions : <List the functions defined in this file>            */
/*                                                                           */
/*  Issues / Problems : None                                                 */
/*                                                                           */
/*  Revision History  :                                                      */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         10 01 2005   Ittiam          Draft                                */
/*                                                                           */
/*****************************************************************************/
#include <stdlib.h>

#include "iv_datatypedef.h"
#include "impeg2_defs.h"
#include "impeg2_platform_macros.h"
#include "impeg2_macros.h"
#include "impeg2d_bitstream.h"

#define BIT(val,bit)      (UWORD16)(((val) >> (bit)) & 0x1)
/******************************************************************************
*
*  Function Name    : impeg2d_bit_stream_init
*
*  Description      : This is a Bitstream initialising function.
*  Arguments        :
*  stream           : Pointer to the Bitstream.
*  byteBuf          : Address of the buffer
*  size             : Size of the buffer in bytes
*
*  Values Returned  : None
*******************************************************************************/
void impeg2d_bit_stream_init(stream_t *ps_stream,
                             UWORD8 *pu1_byte_buf,
                             UWORD32 u4_max_offset)
{
    UWORD8      *pu1_byte_buff;
    UWORD32     *pu4_word_buf;
    size_t     u4_byte_addr;
    UWORD32     u4_temp1,u4_temp2;

    /* Set parameters of the stream structure.Associate the structure with
       the file */
    ps_stream->pv_bs_buf           = pu1_byte_buf;
    ps_stream->u4_offset              = 0;

    /* Take care of unaligned address and create
       nearest greater aligned address */
    pu1_byte_buff               = (UWORD8 *)pu1_byte_buf;
    u4_byte_addr                = (size_t)pu1_byte_buff;

    if((u4_byte_addr & 3) == 1)
    {
        u4_temp1                = ((UWORD32)(*pu1_byte_buff++)) << 8;
        u4_temp1                += ((UWORD32)(*pu1_byte_buff++)) << 16;
        u4_temp1                += ((UWORD32)(*pu1_byte_buff++)) << 24;

        pu4_word_buf            = (UWORD32 *)pu1_byte_buff;

        ps_stream->u4_offset          = 8;
    }
    else if((u4_byte_addr & 3) == 2)
    {
        u4_temp1                = ((UWORD32)(*pu1_byte_buff++)) << 16;
        u4_temp1                += ((UWORD32)(*pu1_byte_buff++)) << 24;

        pu4_word_buf            = (UWORD32 *)pu1_byte_buff;

        ps_stream->u4_offset          = 16;
    }
    else if((u4_byte_addr & 3) == 3)
    {
        u4_temp1                = (((UWORD32)(*pu1_byte_buff++)) << 24);

        pu4_word_buf            = (UWORD32 *)pu1_byte_buff;

        ps_stream->u4_offset          = 24;
    }
    else
    {
        pu4_word_buf            = (UWORD32 *)pu1_byte_buff;

        u4_temp1                = *pu4_word_buf++;
        ps_stream->u4_offset          = 0;
    }

    /* convert the endian ness from Little endian to Big endian so that bits
       are in proper order from MSB to LSB */
    CONV_LE_TO_BE(u4_temp2,u4_temp1)

    /* Read One more word for buf nxt */
    u4_temp1                    = *pu4_word_buf++;
    ps_stream->u4_buf              = u4_temp2;

    CONV_LE_TO_BE(u4_temp2,u4_temp1)

    ps_stream->u4_buf_nxt          = u4_temp2;

    ps_stream->pu4_buf_aligned      = pu4_word_buf;


    ps_stream->u4_max_offset        = (u4_max_offset << 3) + ps_stream->u4_offset;

    return;
}



/******************************************************************************
*
*  Function Name    : impeg2d_bit_stream_get_bit
*
*  Description      : 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.
*  Arguments        :
*  stream           : Pointer to the Bitstream.
*
*  Values Returned  : The bit read(0/1)
*******************************************************************************/
INLINE UWORD8 impeg2d_bit_stream_get_bit(stream_t *ps_stream)
{
    UWORD32     u4_bit,u4_offset,u4_temp;
    UWORD32     u4_curr_bit;

    u4_offset               = ps_stream->u4_offset;
    u4_curr_bit             = u4_offset & 0x1F;
    u4_bit                  = ps_stream->u4_buf;

    /* Move the current bit read from the current word to the
       least significant bit positions of 'c'.*/
    u4_bit                  >>= BITS_IN_INT - u4_curr_bit - 1;

    u4_offset++;

    /* If the last bit of the last word of the buffer has been read update
       the currrent buf with next, and read next buf from bit stream buffer */
    if (u4_curr_bit == 31)
    {
        ps_stream->u4_buf      = ps_stream->u4_buf_nxt;

        if (ps_stream->u4_offset < ps_stream->u4_max_offset)
        {
            u4_temp             = *(ps_stream->pu4_buf_aligned)++;
            CONV_LE_TO_BE(ps_stream->u4_buf_nxt,u4_temp)
        }
    }
    ps_stream->u4_offset          = u4_offset;

    return (u4_bit & 0x1);
}
/******************************************************************************
*
*  Function Name    : impeg2d_bit_stream_flush
*
*  Description      : This is a Bitstream processing function. It
*                     advances the bit and byte pointers appropriately
*
*  Arguments        :
*  ctxt             : Pointer to the Bitstream.
*  numBits          : No of bits to be read
*
*  Values Returned  : None
*******************************************************************************/
INLINE void impeg2d_bit_stream_flush(void* pv_ctxt, UWORD32 u4_no_of_bits)
{
    stream_t *ps_stream = (stream_t *)pv_ctxt;


    if (ps_stream->u4_offset <= ps_stream->u4_max_offset)
    {
        /* We have to flush the bytes even if the offset is equal to the maximum
         * offset. This will ensure that a stream with an error exactly at the
         * offset will not get stuck in an infinite loop - If we do not flush
         * these bytes, then we keep feeding the erroneous bits.
         */
        FLUSH_BITS(ps_stream->u4_offset,ps_stream->u4_buf,ps_stream->u4_buf_nxt,u4_no_of_bits,ps_stream->pu4_buf_aligned)
    }
    return;
}
/******************************************************************************
*
*  Function Name    : impeg2d_bit_stream_flush_to_byte_boundary
*
*  Description      : This is a Bitstream processing function.It advances
*                     the bit and byte pointers to next byte boundary
*
*  Arguments        :
*  stream           : Pointer to the Bitstream.
*  NoOfBits         : No of bits to be read
*
*  Values Returned  : The bits read (upto 32 bits maximum) starting from the
*                     least significant bit and going towards most significant
*                     bit in the order of their occurence.
*******************************************************************************/
INLINE void impeg2d_bit_stream_flush_to_byte_boundary(void* pv_ctxt)
{
    UWORD8 u1_bit_offset;
    stream_t *ps_stream = (stream_t *)pv_ctxt;

    u1_bit_offset = (ps_stream->u4_offset) & 0x7;


    /* if it is not byte aligned make it byte aligned*/
    if(u1_bit_offset != 0)
    {
        impeg2d_bit_stream_flush(ps_stream,(8 - u1_bit_offset));
    }



}


/******************************************************************************
*
*  Function Name    : ibits_next
*
*  Description      : This is a Bitstream processing function.It gets the
*                     specified number of bits from the buffer without
*                     altering the current pointers. It is used mainly to
*                     check for some specific pattern of bits like start
*                     code. This is equivalent to next_bits() function
*                     defined in MPEG-4 Visual Standard Definition of functions
*
*  Arguments        :
*  ctxt             : Pointer to the Bitstream.
*  numBits          : No of bits to be read
*
*  Values Returned  : The bits read (upto 32 bits maximum) starting from the
*                     least significant bit and going towards most significant
*                     bit in the order of their occurence.
*******************************************************************************/
INLINE UWORD32 impeg2d_bit_stream_nxt( stream_t  *ps_stream, WORD32 i4_no_of_bits)
{
    UWORD32     u4_bits,u4_offset,u4_temp;
    UWORD8      u4_bit_ptr;

    ASSERT(i4_no_of_bits > 0);

    u4_offset               = ps_stream->u4_offset;
    u4_bit_ptr              = u4_offset & 0x1F;
    u4_bits                 = ps_stream->u4_buf << u4_bit_ptr;

    u4_bit_ptr              += i4_no_of_bits;
    if(32 < u4_bit_ptr)
    {
        /*  Read bits from the next word if necessary */
        u4_temp             = ps_stream->u4_buf_nxt;
        u4_bit_ptr          &= (BITS_IN_INT - 1);

        u4_temp             = (u4_temp >> (BITS_IN_INT - u4_bit_ptr));

        /* u4_temp consists of bits,if any that had to be read from the next word
           of the buffer.The bits read from both the words are concatenated and
           moved to the least significant positions of 'u4_bits'*/
        u4_bits = (u4_bits >> (32 - i4_no_of_bits)) | u4_temp;
    }
    else
    {
        u4_bits = (u4_bits >> (32 - i4_no_of_bits));
    }

    return (u4_bits);
}
/******************************************************************************
*
*  Function Name    : impeg2d_bit_stream_get
*
*  Description      : This is a Bitstream processing function. It reads a
*                     specified number of bits from the current bit
*                     position and advances the bit and byte pointers
*                     appropriately
*  Arguments        :
*  ctxt             : Pointer to the Bitstream.
*  numBits          : No of bits to be read
*
*  Values Returned  : The bits read (upto 32 bits maximum) starting from the
*                     least significant bit and going towards most significant
*                     bit in the order of their occurence.
*******************************************************************************/

INLINE UWORD32 impeg2d_bit_stream_get(void* pv_ctxt, UWORD32 u4_num_bits)
{
    UWORD32 u4_next_bits = impeg2d_bit_stream_nxt(pv_ctxt, u4_num_bits);
    impeg2d_bit_stream_flush(pv_ctxt, u4_num_bits);
    return(u4_next_bits);
}



/******************************************************************************
*
*  Function Name    : impeg2d_bit_stream_num_bits_read
*
*  Description      : This is a Bitstream processing function. It reads a
*                     specified number of bits from the current bit
*                     position and advances the bit and byte pointers
*                     appropriately
*  Arguments        :
*  ctxt             : Pointer to the Bitstream.
*  numBits          : No of bits to be read
*
*  Values Returned  : The bits read (upto 16 bits maximum) starting from the
*                     least significant bit and going towards most significant
*                     bit in the order of their occurence.
*******************************************************************************/
INLINE UWORD32 impeg2d_bit_stream_num_bits_read(void* pv_ctxt)
{
    stream_t *u4_no_of_bitsstream = (stream_t *)pv_ctxt;
    size_t     u4_temp;
    UWORD32     u4_bits_read;
    u4_temp         = (size_t)(u4_no_of_bitsstream->pv_bs_buf);
    u4_temp         &= 0x3;
    u4_bits_read         = (u4_no_of_bitsstream->u4_offset - (u4_temp << 3));

    return(u4_bits_read);

}