summaryrefslogtreecommitdiffstats
path: root/decoder/ih264d_nal.c
blob: 48450c89aa4445834e11ad7a94f1112e4a0baa3e (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/******************************************************************************
 *
 * 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_nal.c
 *
 *  \brief  NAL parsing routines
 *
 *  Detailed_description
 *
 *  \author
 *         - AI  19 11 2002  Creation
 **************************************************************************
 */
#include "ih264d_bitstrm.h"
#include "ih264d_defs.h"
#include "ih264_typedefs.h"
#include "ih264_macros.h"
#include "ih264_platform_macros.h"
#include "ih264d_defs.h"
#define NUM_OF_ZERO_BYTES_BEFORE_START_CODE 2
#define EMULATION_PREVENTION_BYTE           0x03

#define NAL_FIRST_BYTE_SIZE 1

/*!
 **************************************************************************
 * \if Function name : ih264d_find_start_code \endif
 *
 * \brief
 *    This function searches for the Start Code Prefix.
 *
 * \param pu1_buf : Pointer to char buffer which contains bitstream.
 * \param u4_cur_pos : Current position in the buffer.
 * \param u4_max_ofst : Number of bytes in Buffer.
 * \param pu4_length_of_start_code  : Poiter to length of Start Code.
 *
 * \return
 *    Returns 0 on success and -1 on error.
 *
 **************************************************************************
 */
#define START_CODE_NOT_FOUND    -1
#define END_OF_STREAM_BUFFER    -2
#define END_OF_STREAM           -1

void ih264d_check_if_aud(UWORD8 *pu1_buf,
                         UWORD32 u4_cur_pos,
                         UWORD32 u4_max_ofst,
                         UWORD32 *pu4_next_is_aud)
{
    UWORD8 u1_first_byte, u1_nal_unit_type;
    if(u4_cur_pos + 1 < u4_max_ofst)
    {
        u1_first_byte = pu1_buf[u4_cur_pos + 1];
        u1_nal_unit_type = NAL_UNIT_TYPE(u1_first_byte);

        if(u1_nal_unit_type == ACCESS_UNIT_DELIMITER_RBSP)
        {
            *pu4_next_is_aud = 1;
        }
    }

}
WORD32 ih264d_find_start_code(UWORD8 *pu1_buf,
                              UWORD32 u4_cur_pos,
                              UWORD32 u4_max_ofst,
                              UWORD32 *pu4_length_of_start_code,
                              UWORD32 *pu4_next_is_aud)
{
    WORD32 zero_byte_cnt = 0;
    UWORD32 ui_curPosTemp;

    *pu4_length_of_start_code = 0;
    /*Find first start code */
    while(u4_cur_pos < u4_max_ofst)
    {
        if(pu1_buf[u4_cur_pos] == 0)
            zero_byte_cnt++;
        else if(pu1_buf[u4_cur_pos]
                        == 0x01 && zero_byte_cnt >= NUM_OF_ZERO_BYTES_BEFORE_START_CODE)
        {
            /* Found the start code */
            u4_cur_pos++;
            break;
        }
        else
        {
            zero_byte_cnt = 0;
        }
        u4_cur_pos++;
    }
    /*Find Next Start Code */
    *pu4_length_of_start_code = u4_cur_pos;
    zero_byte_cnt = 0;
    ui_curPosTemp = u4_cur_pos;
    while(u4_cur_pos < u4_max_ofst)
    {

        if(pu1_buf[u4_cur_pos] == 0)
            zero_byte_cnt++;
        else if(pu1_buf[u4_cur_pos]
                        == 0x01 && zero_byte_cnt >= NUM_OF_ZERO_BYTES_BEFORE_START_CODE)
        {
            /* Found the start code */
            ih264d_check_if_aud(pu1_buf, u4_cur_pos, u4_max_ofst,
                                pu4_next_is_aud);
            return (u4_cur_pos - zero_byte_cnt - ui_curPosTemp);
        }
        else
        {
            zero_byte_cnt = 0;
        }
        u4_cur_pos++;
    }

    return (u4_cur_pos - zero_byte_cnt - ui_curPosTemp); //(START_CODE_NOT_FOUND);
}

/*!
 **************************************************************************
 * \if Function name : ih264d_get_next_nal_unit \endif
 *
 * \brief
 *    This function reads one NAl unit.
 *
 * \param ps_nalStream : Poiter to NalUnitStream structure.
 * \param ps_nalUnit : Pointer to NalUnit.
 *
 * \return
 *    Returns 0 on success and -1 on error.
 *
 **************************************************************************
 */
WORD32 ih264d_get_next_nal_unit(UWORD8 *pu1_buf,
                                UWORD32 u4_cur_pos,
                                UWORD32 u4_max_ofst,
                                UWORD32 *pu4_length_of_start_code)
{

    WORD32 i_length_of_nal_unit = 0;
    UWORD32 u4_next_is_aud;

    /* NAL Thread starts */

    ih264d_find_start_code(pu1_buf, u4_cur_pos, u4_max_ofst,
                           pu4_length_of_start_code, &u4_next_is_aud);

    return (i_length_of_nal_unit);
}

/*!
 **************************************************************************
 * \if Function name : ih264d_process_nal_unit \endif
 *
 * \brief
 *    This function removes emulation byte "0x03" from bitstream (EBSP to RBSP).
 *    It also converts bytestream format into 32 bit little-endian format.
 *
 * \param ps_bitstrm : Poiter to dec_bit_stream_t structure.
 * \param pu1_nal_unit  : Pointer to char buffer of NalUnit.
 * \param u4_numbytes_in_nal_unit : Number bytes in NalUnit buffer.
 *
 * \return
 *    Returns number of bytes in RBSP ps_bitstrm.
 *
 * \note
 *    This function is same as nal_unit() of 7.3.1. Apart from nal_unit()
 *    implementation it converts char buffer into 32 bit Buffer. This
 *    facilitates efficient access of bitstream. This has been done taking
 *    into account present processor architectures.
 *
 **************************************************************************
 */
WORD32 ih264d_process_nal_unit(dec_bit_stream_t *ps_bitstrm,
                            UWORD8 *pu1_nal_unit,
                            UWORD32 u4_numbytes_in_nal_unit)
{
    UWORD32 u4_num_bytes_in_rbsp;
    UWORD8 u1_cur_byte;
    WORD32 i = 0;
    WORD8 c_count;
    UWORD32 ui_word;
    UWORD32 *puc_bitstream_buffer = (UWORD32*)pu1_nal_unit;
    ps_bitstrm->pu4_buffer = puc_bitstream_buffer;

    /*--------------------------------------------------------------------*/
    /* First Byte of the NAL Unit                                         */
    /*--------------------------------------------------------------------*/

    ui_word = *pu1_nal_unit++;

    /*--------------------------------------------------------------------*/
    /* Convertion of the EBSP to RBSP                                     */
    /* ie Remove the emulation_prevention_byte [equal to 0x03]            */
    /*--------------------------------------------------------------------*/
    u4_num_bytes_in_rbsp = 0;
    c_count = 0;

//first iteration

    u1_cur_byte = *pu1_nal_unit++;

    ui_word = ((ui_word << 8) | u1_cur_byte);

    c_count++;
    if(u1_cur_byte != 0x00)
        c_count = 0;

//second iteration

    u1_cur_byte = *pu1_nal_unit++;

    ui_word = ((ui_word << 8) | u1_cur_byte);
    u4_num_bytes_in_rbsp = 2;

    c_count++;
    if(u1_cur_byte != 0x00)
        c_count = 0;

    if(u4_numbytes_in_nal_unit > 2)
    {
        i = ((u4_numbytes_in_nal_unit - 3));
    }

    for(; i > 8; i -= 4)
    {

// loop 0
        u1_cur_byte = *pu1_nal_unit++;

        if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE
                        && u1_cur_byte == EMULATION_PREVENTION_BYTE)
        {
            c_count = 0;
            u1_cur_byte = *pu1_nal_unit++;
            i--;
        }

        ui_word = ((ui_word << 8) | u1_cur_byte);
        *puc_bitstream_buffer = ui_word;
        puc_bitstream_buffer++;
        c_count++;
        if(u1_cur_byte != 0x00)
            c_count = 0;

// loop 1
        u1_cur_byte = *pu1_nal_unit++;

        if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE
                        && u1_cur_byte == EMULATION_PREVENTION_BYTE)
        {
            c_count = 0;
            u1_cur_byte = *pu1_nal_unit++;
            i--;
        }
        ui_word = ((ui_word << 8) | u1_cur_byte);

        c_count++;
        if(u1_cur_byte != 0x00)
            c_count = 0;

// loop 2
        u1_cur_byte = *pu1_nal_unit++;

        if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE
                        && u1_cur_byte == EMULATION_PREVENTION_BYTE)
        {
            c_count = 0;
            u1_cur_byte = *pu1_nal_unit++;
            i--;
        }

        ui_word = ((ui_word << 8) | u1_cur_byte);

        c_count++;
        if(u1_cur_byte != 0x00)
            c_count = 0;

// loop 3
        u1_cur_byte = *pu1_nal_unit++;

        if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE
                        && u1_cur_byte == EMULATION_PREVENTION_BYTE)
        {
            c_count = 0;
            u1_cur_byte = *pu1_nal_unit++;
            i--;
        }

        ui_word = ((ui_word << 8) | u1_cur_byte);

        c_count++;
        if(u1_cur_byte != 0x00)
            c_count = 0;

        u4_num_bytes_in_rbsp += 4;

    }

    for(; i > 0; i--)
    {
        u1_cur_byte = *pu1_nal_unit++;

        if(c_count == NUM_OF_ZERO_BYTES_BEFORE_START_CODE
                        && u1_cur_byte == EMULATION_PREVENTION_BYTE)
        {
            c_count = 0;
            i--;
            u1_cur_byte = *pu1_nal_unit++;
        }

        ui_word = ((ui_word << 8) | u1_cur_byte);
        u4_num_bytes_in_rbsp++;

        if((u4_num_bytes_in_rbsp & 0x03) == 0x03)
        {
            *puc_bitstream_buffer = ui_word;
            puc_bitstream_buffer++;
        }
        c_count++;
        if(u1_cur_byte != 0x00)
            c_count = 0;

    }

    *puc_bitstream_buffer = (ui_word
                    << ((3 - (((u4_num_bytes_in_rbsp << 30) >> 30))) << 3));
    ps_bitstrm->u4_ofst = 0;
    ps_bitstrm->u4_max_ofst = ((u4_num_bytes_in_rbsp + NAL_FIRST_BYTE_SIZE) << 3);

    return (u4_num_bytes_in_rbsp);
}


/*!
 **************************************************************************
 * \if Function name : ih264d_rbsp_to_sodb \endif
 *
 * \brief
 *    This function converts RBSP to SODB.
 *
 * \param ps_bitstrm : Poiter to dec_bit_stream_t structure.
 *
 * \return
 *    None.
 *
 **************************************************************************
 */
void ih264d_rbsp_to_sodb(dec_bit_stream_t *ps_bitstrm)
{
    UWORD32 ui_lastWord;
    UWORD32 ui_word;
    UWORD8 uc_lastByte;
    WORD8 i;

    ui_lastWord = (ps_bitstrm->u4_max_ofst >> 5);
    i = (ps_bitstrm->u4_max_ofst >> 3) & 0x03;

    if(i)
    {
        ui_word = ps_bitstrm->pu4_buffer[ui_lastWord];
        uc_lastByte = ((ui_word << ((i - 1) << 3)) >> 24);
    }
    else
    {
        ui_word = ps_bitstrm->pu4_buffer[ui_lastWord - 1];
        uc_lastByte = ((ui_word << 24) >> 24);
    }
    /*--------------------------------------------------------------------*/
    /* Find out the rbsp_stop_bit position in the last byte of rbsp       */
    /*--------------------------------------------------------------------*/
    for(i = 0; (i < 8) && !CHECKBIT(uc_lastByte, i); ++i)
        ;
    ps_bitstrm->u4_max_ofst = ps_bitstrm->u4_max_ofst - (i + 1);
}