summaryrefslogtreecommitdiffstats
path: root/decoder/ih264d_sei.c
blob: 800f2c9c20dd0e6652c7ecfd49d4bc9ad4a45bf5 (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
/******************************************************************************
 *
 * 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         : ih264d_sei.c                                                */
/*                                                                           */
/*  Description       : This file contains routines to parse SEI NAL's       */
/*                                                                           */
/*  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)  */
/*         25 05 2005   NS              Draft                                */
/*                                                                           */
/*****************************************************************************/

#include "ih264_typedefs.h"
#include "ih264_macros.h"
#include "ih264_platform_macros.h"
#include "ih264d_sei.h"
#include "ih264d_bitstrm.h"
#include "ih264d_structs.h"
#include "ih264d_error_handler.h"
#include "ih264d_vui.h"
#include "ih264d_parse_cavlc.h"
#include "ih264d_defs.h"

/*****************************************************************************/
/*                                                                           */
/*  Function Name : ih264d_parse_buffering_period                                   */
/*                                                                           */
/*  Description   : This function parses SEI message buffering_period        */
/*  Inputs        : ps_buf_prd pointer to struct buf_period_t                  */
/*                  ps_bitstrm    Bitstream                                */
/*  Globals       : None                                                     */
/*  Processing    : Parses SEI payload buffering period.                     */
/*  Outputs       : None                                                     */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        : Not implemented fully                                    */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         06 05 2002   NS              Draft                                */
/*                                                                           */
/*****************************************************************************/

WORD32 ih264d_parse_buffering_period(buf_period_t *ps_buf_prd,
                                     dec_bit_stream_t *ps_bitstrm,
                                     dec_struct_t *ps_dec)
{
    UWORD8 u1_seq_parameter_set_id;
    dec_seq_params_t *ps_seq;
    UWORD8 u1_nal_hrd_present, u1_vcl_hrd_present;
    UWORD32 i;
    UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
    UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
    UNUSED(ps_buf_prd);
    u1_seq_parameter_set_id = ih264d_uev(pu4_bitstrm_ofst,
                                         pu4_bitstrm_buf);
    if(u1_seq_parameter_set_id >= MAX_NUM_SEQ_PARAMS)
        return ERROR_INVALID_SEQ_PARAM;
    ps_seq = &ps_dec->ps_sps[u1_seq_parameter_set_id];
    if(TRUE != ps_seq->u1_is_valid)
        return (-1);

    ps_dec->ps_sei->u1_seq_param_set_id = u1_seq_parameter_set_id;
    ps_dec->ps_cur_sps = ps_seq;
    if(FALSE == ps_seq->u1_is_valid)
        return ERROR_INVALID_SEQ_PARAM;
    if(1 == ps_seq->u1_vui_parameters_present_flag)
    {
        u1_nal_hrd_present = ps_seq->s_vui.u1_nal_hrd_params_present;
        if(u1_nal_hrd_present)
        {
            for(i = 0; i < ps_seq->s_vui.s_nal_hrd.u4_cpb_cnt; i++)
            {
                ih264d_get_bits_h264(
                                ps_bitstrm,
                                ps_seq->s_vui.s_nal_hrd.u1_initial_cpb_removal_delay);
                ih264d_get_bits_h264(
                                ps_bitstrm,
                                ps_seq->s_vui.s_nal_hrd.u1_initial_cpb_removal_delay);
            }
        }

        u1_vcl_hrd_present = ps_seq->s_vui.u1_vcl_hrd_params_present;
        if(u1_vcl_hrd_present)
        {
            for(i = 0; i < ps_seq->s_vui.s_vcl_hrd.u4_cpb_cnt; i++)
            {
                ih264d_get_bits_h264(
                                ps_bitstrm,
                                ps_seq->s_vui.s_vcl_hrd.u1_initial_cpb_removal_delay);
                ih264d_get_bits_h264(
                                ps_bitstrm,
                                ps_seq->s_vui.s_vcl_hrd.u1_initial_cpb_removal_delay);
            }
        }
    }
    return OK;
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : ih264d_parse_pic_timing                                         */
/*                                                                           */
/*  Description   : This function parses SEI message pic_timing              */
/*  Inputs        : ps_bitstrm    Bitstream                                */
/*                  ps_dec          Poniter decoder context                  */
/*                  ui4_payload_size pay load i4_size                           */
/*  Globals       : None                                                     */
/*  Processing    : Parses SEI payload picture timing                        */
/*  Outputs       : None                                                     */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        : Not implemented fully                                    */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         06 05 2002   NS              Draft                                */
/*                                                                           */
/*****************************************************************************/
WORD32 ih264d_parse_pic_timing(dec_bit_stream_t *ps_bitstrm,
                               dec_struct_t *ps_dec,
                               UWORD32 ui4_payload_size)
{
    sei *ps_sei;
    vui_t *ps_vu4;
    UWORD8 u1_cpb_dpb_present;
    UWORD8 u1_pic_struct_present_flag;
    UWORD32 u4_start_offset, u4_bits_consumed;
    UWORD8 u1_cpb_removal_delay_length, u1_dpb_output_delay_length;

    ps_sei = (sei *)ps_dec->ps_sei;
    ps_vu4 = &ps_dec->ps_cur_sps->s_vui;

    u1_cpb_dpb_present = ps_vu4->u1_vcl_hrd_params_present
                    + ps_vu4->u1_nal_hrd_params_present;

    if(ps_vu4->u1_vcl_hrd_params_present)
    {
        u1_cpb_removal_delay_length =
                        ps_vu4->s_vcl_hrd.u1_cpb_removal_delay_length;
        u1_dpb_output_delay_length =
                        ps_vu4->s_vcl_hrd.u1_dpb_output_delay_length;
    }
    else if(ps_vu4->u1_nal_hrd_params_present)
    {
        u1_cpb_removal_delay_length =
                        ps_vu4->s_nal_hrd.u1_cpb_removal_delay_length;
        u1_dpb_output_delay_length =
                        ps_vu4->s_nal_hrd.u1_dpb_output_delay_length;
    }
    else
    {
        u1_cpb_removal_delay_length = 24;
        u1_dpb_output_delay_length = 24;

    }

    u4_start_offset = ps_bitstrm->u4_ofst;
    if(u1_cpb_dpb_present)
    {
        ih264d_get_bits_h264(ps_bitstrm, u1_cpb_removal_delay_length);
        ih264d_get_bits_h264(ps_bitstrm, u1_dpb_output_delay_length);
    }

    u1_pic_struct_present_flag = ps_vu4->u1_pic_struct_present_flag;
    if(u1_pic_struct_present_flag)
    {
        ps_sei->u1_pic_struct = ih264d_get_bits_h264(ps_bitstrm, 4);
        ps_dec->u1_pic_struct_copy = ps_sei->u1_pic_struct;
        ps_sei->u1_is_valid = 1;
    }
    u4_bits_consumed = ps_bitstrm->u4_ofst - u4_start_offset;
    ih264d_flush_bits_h264(ps_bitstrm,
                           (ui4_payload_size << 3) - u4_bits_consumed);

    return (0);
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : ih264d_parse_recovery_point                                     */
/*                                                                           */
/*  Description   : This function parses SEI message recovery point          */
/*  Inputs        : ps_bitstrm    Bitstream                                */
/*                  ps_dec          Poniter decoder context                  */
/*                  ui4_payload_size pay load i4_size                           */
/*  Globals       : None                                                     */
/*  Processing    : Parses SEI payload picture timing                        */
/*  Outputs       : None                                                     */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        : Not implemented fully                                    */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         06 05 2002   NS              Draft                                */
/*                                                                           */
/*****************************************************************************/
WORD32 ih264d_parse_recovery_point(dec_bit_stream_t *ps_bitstrm,
                                   dec_struct_t *ps_dec,
                                   UWORD32 ui4_payload_size)
{
    sei *ps_sei = ps_dec->ps_sei;
    dec_err_status_t *ps_err = ps_dec->ps_dec_err_status;
    UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
    UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
    UNUSED(ui4_payload_size);
    ps_sei->u2_recovery_frame_cnt = ih264d_uev(pu4_bitstrm_ofst,
                                               pu4_bitstrm_buf);
    ps_err->u4_frm_sei_sync = ps_err->u4_cur_frm
                    + ps_sei->u2_recovery_frame_cnt;
    ps_sei->u1_exact_match_flag = ih264d_get_bit_h264(ps_bitstrm);
    ps_sei->u1_broken_link_flag = ih264d_get_bit_h264(ps_bitstrm);
    ps_sei->u1_changing_slice_grp_idc = ih264d_get_bits_h264(ps_bitstrm, 2);

    return (0);
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : ih264d_parse_sei_payload                                        */
/*                                                                           */
/*  Description   : This function parses SEI pay loads. Currently it's       */
/*                  implemented partially.                                   */
/*  Inputs        : ps_bitstrm    Bitstream                                */
/*                  ui4_payload_type  SEI payload type                       */
/*                  ui4_payload_size  SEI payload i4_size                       */
/*  Globals       : None                                                     */
/*  Processing    : Parses SEI payloads units and stores the info            */
/*  Outputs       : None                                                     */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        : Not implemented fully                                    */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         06 05 2002   NS              Draft                                */
/*                                                                           */
/*****************************************************************************/

WORD32 ih264d_parse_sei_payload(dec_bit_stream_t *ps_bitstrm,
                                UWORD32 ui4_payload_type,
                                UWORD32 ui4_payload_size,
                                dec_struct_t *ps_dec)
{
    sei *ps_sei;
    WORD32 i4_status = 0;
    ps_sei = (sei *)ps_dec->ps_sei;
    switch(ui4_payload_type)
    {
        case SEI_BUF_PERIOD:

            i4_status = ih264d_parse_buffering_period(&ps_sei->s_buf_period,
                                                      ps_bitstrm, ps_dec);
            /*if(i4_status != OK)
                return i4_status;*/
            break;
        case SEI_PIC_TIMING:
            if(NULL == ps_dec->ps_cur_sps)
                ih264d_flush_bits_h264(ps_bitstrm, (ui4_payload_size << 3));
            else
                ih264d_parse_pic_timing(ps_bitstrm, ps_dec,
                                        ui4_payload_size);
            break;
        case SEI_RECOVERY_PT:
            ih264d_parse_recovery_point(ps_bitstrm, ps_dec,
                                        ui4_payload_size);
            break;
        default:
            ih264d_flush_bits_h264(ps_bitstrm, (ui4_payload_size << 3));
            break;
    }
    return (i4_status);
}

/*****************************************************************************/
/*                                                                           */
/*  Function Name : ih264d_parse_sei_message                                        */
/*                                                                           */
/*  Description   : This function is parses and decode SEI. Currently it's   */
/*                  not implemented fully.                                   */
/*  Inputs        : ps_dec    Decoder parameters                       */
/*                  ps_bitstrm    Bitstream                                */
/*  Globals       : None                                                     */
/*  Processing    : Parses SEI NAL units and stores the info                 */
/*  Outputs       : None                                                     */
/*  Returns       : None                                                     */
/*                                                                           */
/*  Issues        : Not implemented fully                                    */
/*                                                                           */
/*  Revision History:                                                        */
/*                                                                           */
/*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
/*         06 05 2002   NS              Draft                                */
/*                                                                           */
/*****************************************************************************/

WORD32 ih264d_parse_sei_message(dec_struct_t *ps_dec,
                                dec_bit_stream_t *ps_bitstrm)
{
    UWORD32 ui4_payload_type, ui4_payload_size;
    UWORD32 u4_bits;
    WORD32 i4_status = 0;

    do
    {
        ui4_payload_type = 0;

        u4_bits = ih264d_get_bits_h264(ps_bitstrm, 8);
        while(0xff == u4_bits)
        {
            u4_bits = ih264d_get_bits_h264(ps_bitstrm, 8);
            ui4_payload_type += 255;
        }
        ui4_payload_type += u4_bits;

        ui4_payload_size = 0;
        u4_bits = ih264d_get_bits_h264(ps_bitstrm, 8);
        while(0xff == u4_bits)
        {
            u4_bits = ih264d_get_bits_h264(ps_bitstrm, 8);
            ui4_payload_size += 255;
        }
        ui4_payload_size += u4_bits;

        i4_status = ih264d_parse_sei_payload(ps_bitstrm, ui4_payload_type,
                                             ui4_payload_size, ps_dec);
        if(i4_status == -1)
        {
            i4_status = 0;
            break;
        }

        if(i4_status != OK)
            return i4_status;

        if(ih264d_check_byte_aligned(ps_bitstrm) == 0)
        {
            u4_bits = ih264d_get_bit_h264(ps_bitstrm);
            if(0 == u4_bits)
            {
                H264_DEC_DEBUG_PRINT("\nError in parsing SEI message");
            }
            while(0 == ih264d_check_byte_aligned(ps_bitstrm))
            {
                u4_bits = ih264d_get_bit_h264(ps_bitstrm);
                if(u4_bits)
                {
                    H264_DEC_DEBUG_PRINT("\nError in parsing SEI message");
                }
            }
        }
    }
    while(ps_bitstrm->u4_ofst < ps_bitstrm->u4_max_ofst);
    return (i4_status);
}