summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/on2/h264dec/omxdl/arm_neon/src/armCOMM_Bitstream.c
blob: 9ef9319d3056336525f7c5d4adcd77a4bfb9e1f3 (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
/**
 * 
 * File Name:  armCOMM_Bitstream.c
 * OpenMAX DL: v1.0.2
 * Revision:   9641
 * Date:       Thursday, February 7, 2008
 * 
 * (c) Copyright 2007-2008 ARM Limited. All Rights Reserved.
 * 
 * 
 *
 * Defines bitstream encode and decode functions common to all codecs
 */

#include "omxtypes.h"
#include "armCOMM.h"
#include "armCOMM_Bitstream.h"

/***************************************
 * Fixed bit length Decode
 ***************************************/

/**
 * Function: armLookAheadBits()
 *
 * Description:
 * Get the next N bits from the bitstream without advancing the bitstream pointer
 *
 * Parameters:
 * [in]     **ppBitStream
 * [in]     *pOffset
 * [in]     N=1...32
 *
 * Returns  Value
 */

OMX_U32 armLookAheadBits(const OMX_U8 **ppBitStream, OMX_INT *pOffset, OMX_INT N)
{
    const OMX_U8 *pBitStream = *ppBitStream;
    OMX_INT Offset = *pOffset;
    OMX_U32 Value;

    armAssert(Offset>=0 && Offset<=7);
    armAssert(N>=1 && N<=32);

    /* Read next 32 bits from stream */
    Value = (pBitStream[0] << 24 ) | ( pBitStream[1] << 16)  | (pBitStream[2] << 8 ) | (pBitStream[3]) ;
    Value = (Value << Offset ) | (pBitStream[4] >> (8-Offset));

    /* Return N bits */
    return Value >> (32-N);
}


/**
 * Function: armGetBits()
 *
 * Description:
 * Read N bits from the bitstream
 *    
 * Parameters:
 * [in]     *ppBitStream
 * [in]     *pOffset
 * [in]     N=1..32
 *
 * [out]    *ppBitStream
 * [out]    *pOffset
 * Returns  Value
 */


OMX_U32 armGetBits(const OMX_U8 **ppBitStream, OMX_INT *pOffset, OMX_INT N)
{
    const OMX_U8 *pBitStream = *ppBitStream;
    OMX_INT Offset = *pOffset;
    OMX_U32 Value;
    
    if(N == 0)
    {
      return 0;
    }

    armAssert(Offset>=0 && Offset<=7);
    armAssert(N>=1 && N<=32);

    /* Read next 32 bits from stream */
    Value = (pBitStream[0] << 24 ) | ( pBitStream[1] << 16)  | (pBitStream[2] << 8 ) | (pBitStream[3]) ;
    Value = (Value << Offset ) | (pBitStream[4] >> (8-Offset));

    /* Advance bitstream pointer by N bits */
    Offset += N;
    *ppBitStream = pBitStream + (Offset>>3);
    *pOffset = Offset & 7;

    /* Return N bits */
    return Value >> (32-N);
}

/**
 * Function: armByteAlign()
 *
 * Description:
 * Align the pointer *ppBitStream to the next byte boundary
 *
 * Parameters:
 * [in]     *ppBitStream
 * [in]     *pOffset
 *
 * [out]    *ppBitStream
 * [out]    *pOffset
 *
 **/
 
OMXVoid armByteAlign(const OMX_U8 **ppBitStream,OMX_INT *pOffset)
{
    if(*pOffset > 0)
    {
        *ppBitStream += 1;
        *pOffset = 0;
    }    
}

/** 
 * Function: armSkipBits()
 *
 * Description:
 * Skip N bits from the value at *ppBitStream
 *
 * Parameters:
 * [in]     *ppBitStream
 * [in]     *pOffset
 * [in]     N
 *
 * [out]    *ppBitStream
 * [out]    *pOffset
 *
 **/


OMXVoid armSkipBits(const OMX_U8 **ppBitStream,OMX_INT *pOffset,OMX_INT N)
{
    OMX_INT Offset = *pOffset;
    const OMX_U8 *pBitStream = *ppBitStream;
   
    /* Advance bitstream pointer by N bits */
    Offset += N;
    *ppBitStream = pBitStream + (Offset>>3);
    *pOffset = Offset & 7;
}

/***************************************
 * Variable bit length Decode
 ***************************************/

/**
 * Function: armUnPackVLC32()
 *
 * Description:
 * Variable length decode of variable length symbol (max size 32 bits) read from
 * the bit stream pointed by *ppBitStream at *pOffset by using the table
 * pointed by pCodeBook
 * 
 * Parameters:
 * [in]     *pBitStream
 * [in]     *pOffset
 * [in]     pCodeBook
 * 
 * [out]    *pBitStream
 * [out]    *pOffset
 *
 * Returns : Code Book Index if successfull. 
 *         : ARM_NO_CODEBOOK_INDEX = -1 if search fails.
 **/
#ifndef C_OPTIMIZED_IMPLEMENTATION 

OMX_U16 armUnPackVLC32(
    const OMX_U8 **ppBitStream,
    OMX_INT *pOffset,
    const ARM_VLC32 *pCodeBook
)
{    
    const OMX_U8 *pBitStream = *ppBitStream;
    OMX_INT Offset = *pOffset;
    OMX_U32 Value;
    OMX_INT Index;
        
    armAssert(Offset>=0 && Offset<=7);

    /* Read next 32 bits from stream */
    Value = (pBitStream[0] << 24 ) | ( pBitStream[1] << 16)  | (pBitStream[2] << 8 ) | (pBitStream[3]) ;
    Value = (Value << Offset ) | (pBitStream[4] >> (8-Offset));

    /* Search through the codebook */    
    for (Index=0; pCodeBook->codeLen != 0; Index++)
    {
        if (pCodeBook->codeWord == (Value >> (32 - pCodeBook->codeLen)))
        {
            Offset       = Offset + pCodeBook->codeLen;
            *ppBitStream = pBitStream + (Offset >> 3) ;
            *pOffset     = Offset & 7;
            
            return Index;
        }        
        pCodeBook++;
    }

    /* No code match found */
    return ARM_NO_CODEBOOK_INDEX;
}

#endif

/***************************************
 * Fixed bit length Encode
 ***************************************/

/**
 * Function: armPackBits
 *
 * Description:
 * Pack a VLC code word into the bitstream
 *
 * Remarks:
 *
 * Parameters:
 * [in] ppBitStream     pointer to the pointer to the current byte 
 *                      in the bit stream.
 * [in] pOffset         pointer to the bit position in the byte 
 *                      pointed by *ppBitStream. Valid within 0
 *                      to 7.
 * [in] codeWord        Code word that need to be inserted in to the
 *                          bitstream
 * [in] codeLength      Length of the code word valid range 1...32
 *
 * [out] ppBitStream    *ppBitStream is updated after the block is encoded,
 *                          so that it points to the current byte in the bit
 *                          stream buffer.
 * [out] pBitOffset     *pBitOffset is updated so that it points to the
 *                          current bit position in the byte pointed by
 *                          *ppBitStream.
 *
 * Return Value:
 * Standard OMX_RESULT result. See enumeration for possible result codes.
 *
 */
 
OMXResult armPackBits (
    OMX_U8  **ppBitStream, 
    OMX_INT *pOffset,
    OMX_U32 codeWord, 
    OMX_INT codeLength 
)
{
    OMX_U8  *pBitStream = *ppBitStream;
    OMX_INT Offset = *pOffset;
    OMX_U32 Value;
        
    /* checking argument validity */
    armRetArgErrIf(Offset < 0, OMX_Sts_BadArgErr);
    armRetArgErrIf(Offset > 7, OMX_Sts_BadArgErr);
    armRetArgErrIf(codeLength < 1, OMX_Sts_BadArgErr);
    armRetArgErrIf(codeLength > 32, OMX_Sts_BadArgErr);

    /* Prepare the first byte */
    codeWord = codeWord << (32-codeLength);
    Value = (pBitStream[0] >> (8-Offset)) << (8-Offset);
    Value = Value | (codeWord >> (24+Offset));

    /* Write out whole bytes */
    while (8-Offset <= codeLength)
    {
        *pBitStream++ = (OMX_U8)Value;
        codeWord   = codeWord  << (8-Offset);
        codeLength = codeLength - (8-Offset);
        Offset = 0;
        Value = codeWord >> 24;
    }

    /* Write out final partial byte */
    *pBitStream  = (OMX_U8)Value;
    *ppBitStream = pBitStream;
    *pOffset = Offset + codeLength;
    
    return  OMX_Sts_NoErr;
}
 
/***************************************
 * Variable bit length Encode
 ***************************************/

/**
 * Function: armPackVLC32
 *
 * Description:
 * Pack a VLC code word into the bitstream
 *
 * Remarks:
 *
 * Parameters:
 * [in]	ppBitStream		pointer to the pointer to the current byte 
 *                      in the bit stream.
 * [in]	pBitOffset	    pointer to the bit position in the byte 
 *                      pointed by *ppBitStream. Valid within 0
 *                      to 7.
 * [in]	 code     		VLC code word that need to be inserted in to the
 *                      bitstream
 *
 * [out] ppBitStream	*ppBitStream is updated after the block is encoded,
 *	                    so that it points to the current byte in the bit
 *						stream buffer.
 * [out] pBitOffset		*pBitOffset is updated so that it points to the
 *						current bit position in the byte pointed by
 *						*ppBitStream.
 *
 * Return Value:
 * Standard OMX_RESULT result. See enumeration for possible result codes.
 *
 */
 
OMXResult armPackVLC32 (
    OMX_U8 **ppBitStream, 
    OMX_INT *pBitOffset,
    ARM_VLC32 code 
)
{
    return (armPackBits(ppBitStream, pBitOffset, code.codeWord, code.codeLen));
}

/*End of File*/