Home | History | Annotate | Download | only in decoder
      1 /******************************************************************************
      2  *
      3  * Copyright (C) 2015 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at:
      8  *
      9  * http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  *****************************************************************************
     18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
     19 */
     20 
     21 /*****************************************************************************/
     22 /*                                                                           */
     23 /*  File Name         : ih264d_sei.c                                                */
     24 /*                                                                           */
     25 /*  Description       : This file contains routines to parse SEI NAL's       */
     26 /*                                                                           */
     27 /*  List of Functions : <List the functions defined in this file>            */
     28 /*                                                                           */
     29 /*  Issues / Problems : None                                                 */
     30 /*                                                                           */
     31 /*  Revision History  :                                                      */
     32 /*                                                                           */
     33 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
     34 /*         25 05 2005   NS              Draft                                */
     35 /*                                                                           */
     36 /*****************************************************************************/
     37 
     38 #include "ih264_typedefs.h"
     39 #include "ih264_macros.h"
     40 #include "ih264_platform_macros.h"
     41 #include "ih264d_sei.h"
     42 #include "ih264d_bitstrm.h"
     43 #include "ih264d_structs.h"
     44 #include "ih264d_error_handler.h"
     45 #include "ih264d_vui.h"
     46 #include "ih264d_parse_cavlc.h"
     47 #include "ih264d_defs.h"
     48 
     49 /*****************************************************************************/
     50 /*                                                                           */
     51 /*  Function Name : ih264d_parse_buffering_period                                   */
     52 /*                                                                           */
     53 /*  Description   : This function parses SEI message buffering_period        */
     54 /*  Inputs        : ps_buf_prd pointer to struct buf_period_t                  */
     55 /*                  ps_bitstrm    Bitstream                                */
     56 /*  Globals       : None                                                     */
     57 /*  Processing    : Parses SEI payload buffering period.                     */
     58 /*  Outputs       : None                                                     */
     59 /*  Returns       : None                                                     */
     60 /*                                                                           */
     61 /*  Issues        : Not implemented fully                                    */
     62 /*                                                                           */
     63 /*  Revision History:                                                        */
     64 /*                                                                           */
     65 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
     66 /*         06 05 2002   NS              Draft                                */
     67 /*                                                                           */
     68 /*****************************************************************************/
     69 
     70 WORD32 ih264d_parse_buffering_period(buf_period_t *ps_buf_prd,
     71                                      dec_bit_stream_t *ps_bitstrm,
     72                                      dec_struct_t *ps_dec)
     73 {
     74     UWORD8 u1_seq_parameter_set_id;
     75     dec_seq_params_t *ps_seq;
     76     UWORD8 u1_nal_hrd_present, u1_vcl_hrd_present;
     77     UWORD32 i;
     78     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
     79     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
     80     UNUSED(ps_buf_prd);
     81     u1_seq_parameter_set_id = ih264d_uev(pu4_bitstrm_ofst,
     82                                          pu4_bitstrm_buf);
     83     if(u1_seq_parameter_set_id >= MAX_NUM_SEQ_PARAMS)
     84         return ERROR_INVALID_SEQ_PARAM;
     85     ps_seq = &ps_dec->ps_sps[u1_seq_parameter_set_id];
     86     if(TRUE != ps_seq->u1_is_valid)
     87         return (-1);
     88 
     89     ps_dec->ps_sei->u1_seq_param_set_id = u1_seq_parameter_set_id;
     90     ps_dec->ps_cur_sps = ps_seq;
     91     if(FALSE == ps_seq->u1_is_valid)
     92         return ERROR_INVALID_SEQ_PARAM;
     93     if(1 == ps_seq->u1_vui_parameters_present_flag)
     94     {
     95         u1_nal_hrd_present = ps_seq->s_vui.u1_nal_hrd_params_present;
     96         if(u1_nal_hrd_present)
     97         {
     98             for(i = 0; i < ps_seq->s_vui.s_nal_hrd.u4_cpb_cnt; i++)
     99             {
    100                 ih264d_get_bits_h264(
    101                                 ps_bitstrm,
    102                                 ps_seq->s_vui.s_nal_hrd.u1_initial_cpb_removal_delay);
    103                 ih264d_get_bits_h264(
    104                                 ps_bitstrm,
    105                                 ps_seq->s_vui.s_nal_hrd.u1_initial_cpb_removal_delay);
    106             }
    107         }
    108 
    109         u1_vcl_hrd_present = ps_seq->s_vui.u1_vcl_hrd_params_present;
    110         if(u1_vcl_hrd_present)
    111         {
    112             for(i = 0; i < ps_seq->s_vui.s_vcl_hrd.u4_cpb_cnt; i++)
    113             {
    114                 ih264d_get_bits_h264(
    115                                 ps_bitstrm,
    116                                 ps_seq->s_vui.s_vcl_hrd.u1_initial_cpb_removal_delay);
    117                 ih264d_get_bits_h264(
    118                                 ps_bitstrm,
    119                                 ps_seq->s_vui.s_vcl_hrd.u1_initial_cpb_removal_delay);
    120             }
    121         }
    122     }
    123     return OK;
    124 }
    125 
    126 /*****************************************************************************/
    127 /*                                                                           */
    128 /*  Function Name : ih264d_parse_pic_timing                                         */
    129 /*                                                                           */
    130 /*  Description   : This function parses SEI message pic_timing              */
    131 /*  Inputs        : ps_bitstrm    Bitstream                                */
    132 /*                  ps_dec          Poniter decoder context                  */
    133 /*                  ui4_payload_size pay load i4_size                           */
    134 /*  Globals       : None                                                     */
    135 /*  Processing    : Parses SEI payload picture timing                        */
    136 /*  Outputs       : None                                                     */
    137 /*  Returns       : None                                                     */
    138 /*                                                                           */
    139 /*  Issues        : Not implemented fully                                    */
    140 /*                                                                           */
    141 /*  Revision History:                                                        */
    142 /*                                                                           */
    143 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    144 /*         06 05 2002   NS              Draft                                */
    145 /*                                                                           */
    146 /*****************************************************************************/
    147 WORD32 ih264d_parse_pic_timing(dec_bit_stream_t *ps_bitstrm,
    148                                dec_struct_t *ps_dec,
    149                                UWORD32 ui4_payload_size)
    150 {
    151     sei *ps_sei;
    152     vui_t *ps_vu4;
    153     UWORD8 u1_cpb_dpb_present;
    154     UWORD8 u1_pic_struct_present_flag;
    155     UWORD32 u4_start_offset, u4_bits_consumed;
    156     UWORD8 u1_cpb_removal_delay_length, u1_dpb_output_delay_length;
    157 
    158     ps_sei = (sei *)ps_dec->ps_sei;
    159     ps_vu4 = &ps_dec->ps_cur_sps->s_vui;
    160 
    161     u1_cpb_dpb_present = ps_vu4->u1_vcl_hrd_params_present
    162                     + ps_vu4->u1_nal_hrd_params_present;
    163 
    164     if(ps_vu4->u1_vcl_hrd_params_present)
    165     {
    166         u1_cpb_removal_delay_length =
    167                         ps_vu4->s_vcl_hrd.u1_cpb_removal_delay_length;
    168         u1_dpb_output_delay_length =
    169                         ps_vu4->s_vcl_hrd.u1_dpb_output_delay_length;
    170     }
    171     else if(ps_vu4->u1_nal_hrd_params_present)
    172     {
    173         u1_cpb_removal_delay_length =
    174                         ps_vu4->s_nal_hrd.u1_cpb_removal_delay_length;
    175         u1_dpb_output_delay_length =
    176                         ps_vu4->s_nal_hrd.u1_dpb_output_delay_length;
    177     }
    178     else
    179     {
    180         u1_cpb_removal_delay_length = 24;
    181         u1_dpb_output_delay_length = 24;
    182 
    183     }
    184 
    185     u4_start_offset = ps_bitstrm->u4_ofst;
    186     if(u1_cpb_dpb_present)
    187     {
    188         ih264d_get_bits_h264(ps_bitstrm, u1_cpb_removal_delay_length);
    189         ih264d_get_bits_h264(ps_bitstrm, u1_dpb_output_delay_length);
    190     }
    191 
    192     u1_pic_struct_present_flag = ps_vu4->u1_pic_struct_present_flag;
    193     if(u1_pic_struct_present_flag)
    194     {
    195         ps_sei->u1_pic_struct = ih264d_get_bits_h264(ps_bitstrm, 4);
    196         ps_dec->u1_pic_struct_copy = ps_sei->u1_pic_struct;
    197         ps_sei->u1_is_valid = 1;
    198     }
    199     u4_bits_consumed = ps_bitstrm->u4_ofst - u4_start_offset;
    200 
    201     if((ui4_payload_size << 3) < u4_bits_consumed)
    202         return ERROR_CORRUPTED_SLICE;
    203 
    204     ih264d_flush_bits_h264(ps_bitstrm,
    205                            (ui4_payload_size << 3) - u4_bits_consumed);
    206 
    207     return (0);
    208 }
    209 
    210 /*****************************************************************************/
    211 /*                                                                           */
    212 /*  Function Name : ih264d_parse_recovery_point                                     */
    213 /*                                                                           */
    214 /*  Description   : This function parses SEI message recovery point          */
    215 /*  Inputs        : ps_bitstrm    Bitstream                                */
    216 /*                  ps_dec          Poniter decoder context                  */
    217 /*                  ui4_payload_size pay load i4_size                           */
    218 /*  Globals       : None                                                     */
    219 /*  Processing    : Parses SEI payload picture timing                        */
    220 /*  Outputs       : None                                                     */
    221 /*  Returns       : None                                                     */
    222 /*                                                                           */
    223 /*  Issues        : Not implemented fully                                    */
    224 /*                                                                           */
    225 /*  Revision History:                                                        */
    226 /*                                                                           */
    227 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    228 /*         06 05 2002   NS              Draft                                */
    229 /*                                                                           */
    230 /*****************************************************************************/
    231 WORD32 ih264d_parse_recovery_point(dec_bit_stream_t *ps_bitstrm,
    232                                    dec_struct_t *ps_dec,
    233                                    UWORD32 ui4_payload_size)
    234 {
    235     sei *ps_sei = ps_dec->ps_sei;
    236     dec_err_status_t *ps_err = ps_dec->ps_dec_err_status;
    237     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
    238     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
    239     UNUSED(ui4_payload_size);
    240     ps_sei->u2_recovery_frame_cnt = ih264d_uev(pu4_bitstrm_ofst,
    241                                                pu4_bitstrm_buf);
    242     ps_err->u4_frm_sei_sync = ps_err->u4_cur_frm
    243                     + ps_sei->u2_recovery_frame_cnt;
    244     ps_sei->u1_exact_match_flag = ih264d_get_bit_h264(ps_bitstrm);
    245     ps_sei->u1_broken_link_flag = ih264d_get_bit_h264(ps_bitstrm);
    246     ps_sei->u1_changing_slice_grp_idc = ih264d_get_bits_h264(ps_bitstrm, 2);
    247 
    248     return (0);
    249 }
    250 
    251 /*****************************************************************************/
    252 /*                                                                           */
    253 /*  Function Name : ih264d_parse_sei_payload                                        */
    254 /*                                                                           */
    255 /*  Description   : This function parses SEI pay loads. Currently it's       */
    256 /*                  implemented partially.                                   */
    257 /*  Inputs        : ps_bitstrm    Bitstream                                */
    258 /*                  ui4_payload_type  SEI payload type                       */
    259 /*                  ui4_payload_size  SEI payload i4_size                       */
    260 /*  Globals       : None                                                     */
    261 /*  Processing    : Parses SEI payloads units and stores the info            */
    262 /*  Outputs       : None                                                     */
    263 /*  Returns       : None                                                     */
    264 /*                                                                           */
    265 /*  Issues        : Not implemented fully                                    */
    266 /*                                                                           */
    267 /*  Revision History:                                                        */
    268 /*                                                                           */
    269 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    270 /*         06 05 2002   NS              Draft                                */
    271 /*                                                                           */
    272 /*****************************************************************************/
    273 
    274 WORD32 ih264d_parse_sei_payload(dec_bit_stream_t *ps_bitstrm,
    275                                 UWORD32 ui4_payload_type,
    276                                 UWORD32 ui4_payload_size,
    277                                 dec_struct_t *ps_dec)
    278 {
    279     sei *ps_sei;
    280     WORD32 i4_status = 0;
    281     ps_sei = (sei *)ps_dec->ps_sei;
    282 
    283     if(ui4_payload_size == 0)
    284         return -1;
    285 
    286     switch(ui4_payload_type)
    287     {
    288         case SEI_BUF_PERIOD:
    289 
    290             i4_status = ih264d_parse_buffering_period(&ps_sei->s_buf_period,
    291                                                       ps_bitstrm, ps_dec);
    292             break;
    293         case SEI_PIC_TIMING:
    294             if(NULL == ps_dec->ps_cur_sps)
    295                 i4_status = ih264d_flush_bits_h264(ps_bitstrm, (ui4_payload_size << 3));
    296             else
    297                 i4_status = ih264d_parse_pic_timing(ps_bitstrm, ps_dec,
    298                                         ui4_payload_size);
    299             break;
    300         case SEI_RECOVERY_PT:
    301             i4_status = ih264d_parse_recovery_point(ps_bitstrm, ps_dec,
    302                                         ui4_payload_size);
    303             break;
    304         default:
    305             i4_status = ih264d_flush_bits_h264(ps_bitstrm, (ui4_payload_size << 3));
    306             break;
    307     }
    308     return (i4_status);
    309 }
    310 
    311 /*****************************************************************************/
    312 /*                                                                           */
    313 /*  Function Name : ih264d_parse_sei_message                                        */
    314 /*                                                                           */
    315 /*  Description   : This function is parses and decode SEI. Currently it's   */
    316 /*                  not implemented fully.                                   */
    317 /*  Inputs        : ps_dec    Decoder parameters                       */
    318 /*                  ps_bitstrm    Bitstream                                */
    319 /*  Globals       : None                                                     */
    320 /*  Processing    : Parses SEI NAL units and stores the info                 */
    321 /*  Outputs       : None                                                     */
    322 /*  Returns       : None                                                     */
    323 /*                                                                           */
    324 /*  Issues        : Not implemented fully                                    */
    325 /*                                                                           */
    326 /*  Revision History:                                                        */
    327 /*                                                                           */
    328 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    329 /*         06 05 2002   NS              Draft                                */
    330 /*                                                                           */
    331 /*****************************************************************************/
    332 
    333 WORD32 ih264d_parse_sei_message(dec_struct_t *ps_dec,
    334                                 dec_bit_stream_t *ps_bitstrm)
    335 {
    336     UWORD32 ui4_payload_type, ui4_payload_size;
    337     UWORD32 u4_bits;
    338     WORD32 i4_status = 0;
    339 
    340     do
    341     {
    342         ui4_payload_type = 0;
    343 
    344         u4_bits = ih264d_get_bits_h264(ps_bitstrm, 8);
    345         while(0xff == u4_bits && !EXCEED_OFFSET(ps_bitstrm))
    346         {
    347             u4_bits = ih264d_get_bits_h264(ps_bitstrm, 8);
    348             ui4_payload_type += 255;
    349         }
    350         ui4_payload_type += u4_bits;
    351 
    352         ui4_payload_size = 0;
    353         u4_bits = ih264d_get_bits_h264(ps_bitstrm, 8);
    354         while(0xff == u4_bits && !EXCEED_OFFSET(ps_bitstrm))
    355         {
    356             u4_bits = ih264d_get_bits_h264(ps_bitstrm, 8);
    357             ui4_payload_size += 255;
    358         }
    359         ui4_payload_size += u4_bits;
    360 
    361         i4_status = ih264d_parse_sei_payload(ps_bitstrm, ui4_payload_type,
    362                                              ui4_payload_size, ps_dec);
    363         if(i4_status != OK)
    364             return i4_status;
    365 
    366         if(ih264d_check_byte_aligned(ps_bitstrm) == 0)
    367         {
    368             u4_bits = ih264d_get_bit_h264(ps_bitstrm);
    369             if(0 == u4_bits)
    370             {
    371                 H264_DEC_DEBUG_PRINT("\nError in parsing SEI message");
    372             }
    373             while(0 == ih264d_check_byte_aligned(ps_bitstrm)
    374                             && !EXCEED_OFFSET(ps_bitstrm))
    375             {
    376                 u4_bits = ih264d_get_bit_h264(ps_bitstrm);
    377                 if(u4_bits)
    378                 {
    379                     H264_DEC_DEBUG_PRINT("\nError in parsing SEI message");
    380                 }
    381             }
    382         }
    383     }
    384     while(ps_bitstrm->u4_ofst < ps_bitstrm->u4_max_ofst);
    385     return (i4_status);
    386 }
    387 
    388