Home | History | Annotate | Download | only in decoder
      1 /******************************************************************************
      2 *
      3 * Copyright (C) 2012 Ittiam Systems Pvt Ltd, Bangalore
      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 /**
     19 *******************************************************************************
     20 * @file
     21 *  ihevcd_parse_headers.c
     22 *
     23 * @brief
     24 *  Contains functions for parsing headers
     25 *
     26 * @author
     27 *  Harish
     28 *
     29 * @par List of Functions:
     30 *
     31 * @remarks
     32 *  None
     33 *
     34 *******************************************************************************
     35 */
     36 
     37 /*****************************************************************************/
     38 /* File Includes                                                             */
     39 /*****************************************************************************/
     40 #include <stdio.h>
     41 #include <stddef.h>
     42 #include <stdlib.h>
     43 #include <string.h>
     44 #include <assert.h>
     45 
     46 #include "ihevc_typedefs.h"
     47 #include "iv.h"
     48 #include "ivd.h"
     49 #include "ihevcd_cxa.h"
     50 
     51 #include "ihevc_defs.h"
     52 #include "ihevc_debug.h"
     53 #include "ihevc_defs.h"
     54 #include "ihevc_structs.h"
     55 #include "ihevc_buf_mgr.h"
     56 #include "ihevc_dpb_mgr.h"
     57 #include "ihevc_macros.h"
     58 #include "ihevc_platform_macros.h"
     59 #include "ihevc_cabac_tables.h"
     60 #include "ihevc_common_tables.h"
     61 #include "ihevc_quant_tables.h"
     62 
     63 #include "ihevcd_trace.h"
     64 #include "ihevcd_defs.h"
     65 #include "ihevcd_function_selector.h"
     66 #include "ihevcd_structs.h"
     67 #include "ihevcd_error.h"
     68 #include "ihevcd_debug.h"
     69 #include "ihevcd_nal.h"
     70 #include "ihevcd_bitstream.h"
     71 #include "ihevcd_parse_headers.h"
     72 #include "ihevcd_ref_list.h"
     73 
     74 #define COPY_DEFAULT_SCALING_LIST(pi2_scaling_mat)                                                                                      \
     75 {                                                                                                                                       \
     76     WORD32 scaling_mat_offset[]={0, 16, 32, 48, 64, 80, 96, 160, 224, 288, 352, 416, 480, 736, 992, 1248, 1504, 1760, 2016, 3040};      \
     77                                                                                                                                         \
     78     /* scaling matrix for 4x4 */                                                                                                        \
     79     memcpy(pi2_scaling_mat, gi2_flat_scale_mat_32x32, 6*16*sizeof(WORD16));                                                             \
     80 /* scaling matrix for 8x8 */                                                                                                            \
     81     memcpy(pi2_scaling_mat + scaling_mat_offset[6], gi2_intra_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
     82     memcpy(pi2_scaling_mat + scaling_mat_offset[7], gi2_intra_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
     83     memcpy(pi2_scaling_mat + scaling_mat_offset[8], gi2_intra_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
     84     memcpy(pi2_scaling_mat + scaling_mat_offset[9], gi2_inter_default_scale_mat_8x8, 64*sizeof(WORD16));                                \
     85     memcpy(pi2_scaling_mat + scaling_mat_offset[10], gi2_inter_default_scale_mat_8x8, 64*sizeof(WORD16));                               \
     86     memcpy(pi2_scaling_mat + scaling_mat_offset[11], gi2_inter_default_scale_mat_8x8, 64*sizeof(WORD16));                               \
     87     /* scaling matrix for 16x16 */                                                                                                      \
     88     memcpy(pi2_scaling_mat + scaling_mat_offset[12], gi2_intra_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
     89     memcpy(pi2_scaling_mat + scaling_mat_offset[13], gi2_intra_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
     90     memcpy(pi2_scaling_mat + scaling_mat_offset[14], gi2_intra_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
     91     memcpy(pi2_scaling_mat + scaling_mat_offset[15], gi2_inter_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
     92     memcpy(pi2_scaling_mat + scaling_mat_offset[16], gi2_inter_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
     93     memcpy(pi2_scaling_mat + scaling_mat_offset[17], gi2_inter_default_scale_mat_16x16, 256*sizeof(WORD16));                            \
     94     /* scaling matrix for 32x32 */                                                                                                      \
     95     memcpy(pi2_scaling_mat + scaling_mat_offset[18], gi2_intra_default_scale_mat_32x32, 1024*sizeof(WORD16));                           \
     96     memcpy(pi2_scaling_mat + scaling_mat_offset[19], gi2_inter_default_scale_mat_32x32, 1024*sizeof(WORD16));                           \
     97 }
     98 
     99 #define COPY_FLAT_SCALING_LIST(pi2_scaling_mat)                                                                                         \
    100 {                                                                                                                                       \
    101     WORD32 scaling_mat_offset[]={0, 16, 32, 48, 64, 80, 96, 160, 224, 288, 352, 416, 480, 736, 992, 1248, 1504, 1760, 2016, 3040};      \
    102                                                                                                                                         \
    103     /* scaling matrix for 4x4 */                                                                                                        \
    104     memcpy(pi2_scaling_mat, gi2_flat_scale_mat_32x32, 6*16*sizeof(WORD16));                                                             \
    105     /* scaling matrix for 8x8 */                                                                                                        \
    106     memcpy(pi2_scaling_mat + scaling_mat_offset[6], gi2_flat_scale_mat_32x32, 6*64*sizeof(WORD16));                                     \
    107     /* scaling matrix for 16x16 */                                                                                                      \
    108     memcpy(pi2_scaling_mat + scaling_mat_offset[12], gi2_flat_scale_mat_32x32, 3*256*sizeof(WORD16));                                   \
    109     memcpy(pi2_scaling_mat + scaling_mat_offset[15], gi2_flat_scale_mat_32x32, 3*256*sizeof(WORD16));                                   \
    110     /* scaling matrix for 32x32 */                                                                                                      \
    111     memcpy(pi2_scaling_mat + scaling_mat_offset[18], gi2_flat_scale_mat_32x32, 1024*sizeof(WORD16));                                    \
    112     memcpy(pi2_scaling_mat + scaling_mat_offset[19], gi2_flat_scale_mat_32x32, 1024*sizeof(WORD16));                                    \
    113 }
    114 
    115 /* Function declarations */
    116 
    117 /**
    118 *******************************************************************************
    119 *
    120 * @brief
    121 *  Parses Prediction weight table syntax
    122 *
    123 * @par Description:
    124 *  Parse Prediction weight table syntax as per Section: 7.3.8.4
    125 *
    126 * @param[in] ps_bitstrm
    127 *  Pointer to bitstream context
    128 *
    129 * @param[in] ps_sps
    130 *  Current SPS
    131 *
    132 * @param[in] ps_pps
    133 *  Current PPS
    134 *
    135 * @param[in] ps_slice_hdr
    136 *  Current Slice header
    137 *
    138 * @returns  Error code from IHEVCD_ERROR_T
    139 *
    140 * @remarks
    141 *
    142 *
    143 *******************************************************************************
    144 */
    145 
    146 WORD32 ihevcd_parse_pred_wt_ofst(bitstrm_t *ps_bitstrm,
    147                                  sps_t *ps_sps,
    148                                  pps_t *ps_pps,
    149                                  slice_header_t *ps_slice_hdr)
    150 {
    151     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    152     WORD32 value;
    153     WORD32 i;
    154 
    155     pred_wt_ofst_t *ps_wt_ofst = &ps_slice_hdr->s_wt_ofst;
    156     UNUSED(ps_pps);
    157 
    158     UEV_PARSE("luma_log2_weight_denom", value, ps_bitstrm);
    159     ps_wt_ofst->i1_luma_log2_weight_denom = value;
    160 
    161     if(ps_sps->i1_chroma_format_idc != 0)
    162     {
    163         SEV_PARSE("delta_chroma_log2_weight_denom", value, ps_bitstrm);
    164         ps_wt_ofst->i1_chroma_log2_weight_denom = ps_wt_ofst->i1_luma_log2_weight_denom + value;
    165     }
    166 
    167     for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
    168     {
    169         BITS_PARSE("luma_weight_l0_flag[ i ]", value, ps_bitstrm, 1);
    170         ps_wt_ofst->i1_luma_weight_l0_flag[i] = value;
    171     }
    172 
    173 
    174 
    175     if(ps_sps->i1_chroma_format_idc != 0)
    176     {
    177         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
    178         {
    179             BITS_PARSE("chroma_weight_l0_flag[ i ]", value, ps_bitstrm, 1);
    180             ps_wt_ofst->i1_chroma_weight_l0_flag[i] = value;
    181         }
    182     }
    183     else
    184     {
    185         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
    186         {
    187             ps_wt_ofst->i1_chroma_weight_l0_flag[i] = 0;
    188         }
    189     }
    190 
    191 
    192     for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l0_active; i++)
    193     {
    194         if(ps_wt_ofst->i1_luma_weight_l0_flag[i])
    195         {
    196             SEV_PARSE("delta_luma_weight_l0[ i ]", value, ps_bitstrm);
    197 
    198 
    199             ps_wt_ofst->i2_luma_weight_l0[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom) + value;
    200 
    201             SEV_PARSE("luma_offset_l0[ i ]", value, ps_bitstrm);
    202             ps_wt_ofst->i2_luma_offset_l0[i] = value;
    203 
    204         }
    205         else
    206         {
    207             ps_wt_ofst->i2_luma_weight_l0[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom);
    208             ps_wt_ofst->i2_luma_offset_l0[i] = 0;
    209         }
    210         if(ps_wt_ofst->i1_chroma_weight_l0_flag[i])
    211         {
    212             WORD32 ofst;
    213             WORD32 shift = (1 << (BIT_DEPTH_CHROMA - 1));
    214             SEV_PARSE("delta_chroma_weight_l0[ i ][ j ]", value, ps_bitstrm);
    215             ps_wt_ofst->i2_chroma_weight_l0_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;
    216 
    217 
    218             SEV_PARSE("delta_chroma_offset_l0[ i ][ j ]", value, ps_bitstrm);
    219             ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l0_cb[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
    220             ofst = value - ofst + shift;
    221 
    222             ps_wt_ofst->i2_chroma_offset_l0_cb[i] = CLIP_S8(ofst);
    223 
    224             SEV_PARSE("delta_chroma_weight_l0[ i ][ j ]", value, ps_bitstrm);
    225             ps_wt_ofst->i2_chroma_weight_l0_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;
    226 
    227 
    228             SEV_PARSE("delta_chroma_offset_l0[ i ][ j ]", value, ps_bitstrm);
    229             ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l0_cr[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
    230             ofst = value - ofst + shift;
    231 
    232             ps_wt_ofst->i2_chroma_offset_l0_cr[i] = CLIP_S8(ofst);
    233 
    234         }
    235         else
    236         {
    237             ps_wt_ofst->i2_chroma_weight_l0_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
    238             ps_wt_ofst->i2_chroma_weight_l0_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
    239 
    240             ps_wt_ofst->i2_chroma_offset_l0_cb[i] = 0;
    241             ps_wt_ofst->i2_chroma_offset_l0_cr[i] = 0;
    242         }
    243     }
    244     if(BSLICE == ps_slice_hdr->i1_slice_type)
    245     {
    246         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
    247         {
    248             BITS_PARSE("luma_weight_l1_flag[ i ]", value, ps_bitstrm, 1);
    249             ps_wt_ofst->i1_luma_weight_l1_flag[i] = value;
    250         }
    251 
    252         if(ps_sps->i1_chroma_format_idc != 0)
    253         {
    254             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
    255             {
    256                 BITS_PARSE("chroma_weight_l1_flag[ i ]", value, ps_bitstrm, 1);
    257                 ps_wt_ofst->i1_chroma_weight_l1_flag[i] = value;
    258             }
    259         }
    260         else
    261         {
    262             for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
    263             {
    264                 ps_wt_ofst->i1_chroma_weight_l1_flag[i] = 0;
    265             }
    266         }
    267 
    268         for(i = 0; i < ps_slice_hdr->i1_num_ref_idx_l1_active; i++)
    269         {
    270             if(ps_wt_ofst->i1_luma_weight_l1_flag[i])
    271             {
    272                 SEV_PARSE("delta_luma_weight_l1[ i ]", value, ps_bitstrm);
    273 
    274 
    275                 ps_wt_ofst->i2_luma_weight_l1[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom) + value;
    276 
    277                 SEV_PARSE("luma_offset_l1[ i ]", value, ps_bitstrm);
    278                 ps_wt_ofst->i2_luma_offset_l1[i] = value;
    279 
    280             }
    281             else
    282             {
    283                 ps_wt_ofst->i2_luma_weight_l1[i] = (1 << ps_wt_ofst->i1_luma_log2_weight_denom);
    284                 ps_wt_ofst->i2_luma_offset_l1[i] = 0;
    285             }
    286 
    287             if(ps_wt_ofst->i1_chroma_weight_l1_flag[i])
    288             {
    289                 WORD32 ofst;
    290                 WORD32 shift = (1 << (BIT_DEPTH_CHROMA - 1));
    291                 SEV_PARSE("delta_chroma_weight_l1[ i ][ j ]", value, ps_bitstrm);
    292                 ps_wt_ofst->i2_chroma_weight_l1_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;;
    293 
    294 
    295                 SEV_PARSE("delta_chroma_offset_l1[ i ][ j ]", value, ps_bitstrm);
    296                 ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l1_cb[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
    297                 ofst = value - ofst + shift;
    298 
    299                 ps_wt_ofst->i2_chroma_offset_l1_cb[i] = CLIP_S8(ofst);;
    300 
    301                 SEV_PARSE("delta_chroma_weight_l1[ i ][ j ]", value, ps_bitstrm);
    302                 ps_wt_ofst->i2_chroma_weight_l1_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom) + value;
    303 
    304 
    305                 SEV_PARSE("delta_chroma_offset_l1[ i ][ j ]", value, ps_bitstrm);
    306                 ofst = ((shift * ps_wt_ofst->i2_chroma_weight_l1_cr[i]) >> ps_wt_ofst->i1_chroma_log2_weight_denom);
    307                 ofst = value - ofst + shift;
    308 
    309                 ps_wt_ofst->i2_chroma_offset_l1_cr[i] = CLIP_S8(ofst);;
    310 
    311             }
    312             else
    313             {
    314                 ps_wt_ofst->i2_chroma_weight_l1_cb[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
    315                 ps_wt_ofst->i2_chroma_weight_l1_cr[i] = (1 << ps_wt_ofst->i1_chroma_log2_weight_denom);
    316 
    317                 ps_wt_ofst->i2_chroma_offset_l1_cb[i] = 0;
    318                 ps_wt_ofst->i2_chroma_offset_l1_cr[i] = 0;
    319 
    320             }
    321         }
    322     }
    323     return ret;
    324 }
    325 
    326 /**
    327 *******************************************************************************
    328 *
    329 * @brief
    330 *  Parses short term reference picture set
    331 *
    332 * @par   Description
    333 *  Parses short term reference picture set as per section 7.3.8.2.
    334 * Can be called by either SPS or Slice header parsing modules.
    335 *
    336 * @param[in] ps_bitstrm
    337 *  Pointer to bitstream structure
    338 *
    339 * @param[out] ps_stref_picset_base
    340 *  Pointer to first short term ref pic set structure
    341 *
    342 * @param[in] num_short_term_ref_pic_sets
    343 *  Number of short term reference pic sets
    344 *
    345 * @param[in] idx
    346 *  Current short term ref pic set id
    347 *
    348 * @returns Error code from IHEVCD_ERROR_T
    349 *
    350 * @remarks
    351 *
    352 *******************************************************************************
    353 */
    354 IHEVCD_ERROR_T ihevcd_short_term_ref_pic_set(bitstrm_t *ps_bitstrm,
    355                                              stref_picset_t *ps_stref_picset_base,
    356                                              WORD32 num_short_term_ref_pic_sets,
    357                                              WORD32 idx,
    358                                              stref_picset_t *ps_stref_picset)
    359 {
    360     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    361     WORD32 value;
    362     stref_picset_t *ps_stref_picset_ref;
    363     WORD32 delta_idx, delta_rps;
    364     WORD32 r_idx;
    365     WORD32 i;
    366     WORD32 j, k, temp;
    367     if(idx > 0)
    368     {
    369         BITS_PARSE("inter_ref_pic_set_prediction_flag", value, ps_bitstrm, 1);
    370         ps_stref_picset->i1_inter_ref_pic_set_prediction_flag = value;
    371     }
    372     else
    373         ps_stref_picset->i1_inter_ref_pic_set_prediction_flag = 0;
    374 
    375     if(ps_stref_picset->i1_inter_ref_pic_set_prediction_flag)
    376     {
    377         WORD32 delta_rps_sign;
    378         WORD32 abs_delta_rps;
    379         WORD32 num_neg_pics = 0;
    380         WORD32 num_pos_pics = 0;
    381         WORD32 num_pics = 0;
    382 
    383         if(idx == num_short_term_ref_pic_sets)
    384         {
    385             UEV_PARSE("delta_idx_minus1", value, ps_bitstrm);
    386             delta_idx = value + 1;
    387         }
    388         else
    389         {
    390             delta_idx = 1;
    391         }
    392         r_idx = idx - delta_idx;
    393         r_idx = CLIP3(r_idx, 0, idx - 1);
    394 
    395         ps_stref_picset_ref = ps_stref_picset_base + r_idx;
    396 
    397         BITS_PARSE("delta_rps_sign", value, ps_bitstrm, 1);
    398         delta_rps_sign = value;
    399 
    400         UEV_PARSE("abs_delta_rps_minus1", value, ps_bitstrm);
    401         abs_delta_rps = value + 1;
    402 
    403         delta_rps = (1 - 2 * delta_rps_sign) * (abs_delta_rps);
    404 
    405 
    406 
    407         for(i = 0; i <= ps_stref_picset_ref->i1_num_delta_pocs; i++)
    408         {
    409             WORD32 ref_idc;
    410 
    411             /*****************************************************************/
    412             /* ref_idc is parsed as below                                    */
    413             /* bits "1" ref_idc 1                                            */
    414             /* bits "01" ref_idc 2                                           */
    415             /* bits "00" ref_idc 0                                           */
    416             /*****************************************************************/
    417             BITS_PARSE("used_by_curr_pic_flag", value, ps_bitstrm, 1);
    418             ref_idc = value;
    419             ps_stref_picset->ai1_used[num_pics] = value;
    420             /* If ref_idc is zero check for next bit */
    421             if(0 == ref_idc)
    422             {
    423                 BITS_PARSE("use_delta_flag", value, ps_bitstrm, 1);
    424                 ref_idc = value << 1;
    425             }
    426             if((ref_idc == 1) || (ref_idc == 2))
    427             {
    428                 WORD32 delta_poc;
    429                 delta_poc = delta_rps;
    430                 delta_poc +=
    431                                 ((i < ps_stref_picset_ref->i1_num_delta_pocs) ?
    432                                 ps_stref_picset_ref->ai2_delta_poc[i] :
    433                                 0);
    434 
    435                 ps_stref_picset->ai2_delta_poc[num_pics] = delta_poc;
    436 
    437                 if(delta_poc < 0)
    438                 {
    439                     num_neg_pics++;
    440                 }
    441                 else
    442                 {
    443                     num_pos_pics++;
    444                 }
    445                 num_pics++;
    446             }
    447             ps_stref_picset->ai1_ref_idc[i] = ref_idc;
    448         }
    449 
    450         num_neg_pics = CLIP3(num_neg_pics, 0, MAX_DPB_SIZE - 1);
    451         num_pos_pics = CLIP3(num_pos_pics, 0, (MAX_DPB_SIZE - 1 - num_neg_pics));
    452         num_pics = num_neg_pics + num_pos_pics;
    453 
    454         ps_stref_picset->i1_num_ref_idc =
    455                         ps_stref_picset_ref->i1_num_delta_pocs + 1;
    456         ps_stref_picset->i1_num_delta_pocs = num_pics;
    457         ps_stref_picset->i1_num_pos_pics = num_pos_pics;
    458         ps_stref_picset->i1_num_neg_pics = num_neg_pics;
    459 
    460 
    461         for(j = 1; j < num_pics; j++)
    462         {
    463             WORD32 delta_poc = ps_stref_picset->ai2_delta_poc[j];
    464             WORD8 i1_used = ps_stref_picset->ai1_used[j];
    465             for(k = j - 1; k >= 0; k--)
    466             {
    467                 temp = ps_stref_picset->ai2_delta_poc[k];
    468                 if(delta_poc < temp)
    469                 {
    470                     ps_stref_picset->ai2_delta_poc[k + 1] = temp;
    471                     ps_stref_picset->ai1_used[k + 1] = ps_stref_picset->ai1_used[k];
    472                     ps_stref_picset->ai2_delta_poc[k] = delta_poc;
    473                     ps_stref_picset->ai1_used[k] = i1_used;
    474                 }
    475             }
    476         }
    477         // flip the negative values to largest first
    478         for(j = 0, k = num_neg_pics - 1; j < num_neg_pics >> 1; j++, k--)
    479         {
    480             WORD32 delta_poc = ps_stref_picset->ai2_delta_poc[j];
    481             WORD8 i1_used = ps_stref_picset->ai1_used[j];
    482             ps_stref_picset->ai2_delta_poc[j] = ps_stref_picset->ai2_delta_poc[k];
    483             ps_stref_picset->ai1_used[j] = ps_stref_picset->ai1_used[k];
    484             ps_stref_picset->ai2_delta_poc[k] = delta_poc;
    485             ps_stref_picset->ai1_used[k] = i1_used;
    486         }
    487 
    488     }
    489     else
    490     {
    491         WORD32 prev_poc = 0;
    492         WORD32 poc;
    493 
    494         UEV_PARSE("num_negative_pics", value, ps_bitstrm);
    495         ps_stref_picset->i1_num_neg_pics = value;
    496         ps_stref_picset->i1_num_neg_pics = CLIP3(ps_stref_picset->i1_num_neg_pics,
    497                                                  0,
    498                                                  MAX_DPB_SIZE - 1);
    499 
    500         UEV_PARSE("num_positive_pics", value, ps_bitstrm);
    501         ps_stref_picset->i1_num_pos_pics = value;
    502         ps_stref_picset->i1_num_pos_pics = CLIP3(ps_stref_picset->i1_num_pos_pics,
    503                                                  0,
    504                                                  (MAX_DPB_SIZE - 1 - ps_stref_picset->i1_num_neg_pics));
    505 
    506         ps_stref_picset->i1_num_delta_pocs =
    507                         ps_stref_picset->i1_num_neg_pics +
    508                         ps_stref_picset->i1_num_pos_pics;
    509 
    510 
    511         for(i = 0; i < ps_stref_picset->i1_num_neg_pics; i++)
    512         {
    513             UEV_PARSE("delta_poc_s0_minus1", value, ps_bitstrm);
    514             poc = prev_poc - (value + 1);
    515             prev_poc = poc;
    516             ps_stref_picset->ai2_delta_poc[i] = poc;
    517 
    518             BITS_PARSE("used_by_curr_pic_s0_flag", value, ps_bitstrm, 1);
    519             ps_stref_picset->ai1_used[i] = value;
    520 
    521         }
    522         prev_poc = 0;
    523         for(i = ps_stref_picset->i1_num_neg_pics;
    524                         i < ps_stref_picset->i1_num_delta_pocs;
    525                         i++)
    526         {
    527             UEV_PARSE("delta_poc_s1_minus1", value, ps_bitstrm);
    528             poc = prev_poc + (value + 1);
    529             prev_poc = poc;
    530             ps_stref_picset->ai2_delta_poc[i] = poc;
    531 
    532             BITS_PARSE("used_by_curr_pic_s1_flag", value, ps_bitstrm, 1);
    533             ps_stref_picset->ai1_used[i] = value;
    534 
    535         }
    536 
    537     }
    538 
    539     return ret;
    540 }
    541 
    542 
    543 static WORD32 ihevcd_parse_sub_layer_hrd_parameters(bitstrm_t *ps_bitstrm,
    544                                                     sub_lyr_hrd_params_t *ps_sub_layer_hrd_params,
    545                                                     WORD32 cpb_cnt,
    546                                                     WORD32 sub_pic_cpb_params_present_flag)
    547 {
    548     WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    549     WORD32 i;
    550 
    551     for(i = 0; i <= cpb_cnt; i++)
    552     {
    553         UEV_PARSE("bit_rate_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_bit_rate_value_minus1[i], ps_bitstrm);
    554         UEV_PARSE("cpb_size_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_cpb_size_value_minus1[i], ps_bitstrm);
    555 
    556         if(sub_pic_cpb_params_present_flag)
    557         {
    558             UEV_PARSE("cpb_size_du_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_cpb_size_du_value_minus1[i], ps_bitstrm);
    559             UEV_PARSE("bit_rate_du_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_bit_rate_du_value_minus1[i], ps_bitstrm);
    560         }
    561         BITS_PARSE("cbr_flag[ i ]", ps_sub_layer_hrd_params->au1_cbr_flag[i], ps_bitstrm, 1);
    562     }
    563 
    564     return ret;
    565 }
    566 
    567 
    568 static WORD32 ihevcd_parse_hrd_parameters(bitstrm_t *ps_bitstrm,
    569                                           hrd_params_t *ps_hrd,
    570                                           WORD32 common_info_present_flag,
    571                                           WORD32 max_num_sub_layers_minus1)
    572 {
    573     WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    574     WORD32 i;
    575 
    576     ps_hrd->u1_nal_hrd_parameters_present_flag = 0;
    577     ps_hrd->u1_vcl_hrd_parameters_present_flag = 0;
    578 
    579     ps_hrd->u1_sub_pic_cpb_params_present_flag = 0;
    580 
    581     ps_hrd->u1_tick_divisor_minus2 = 0;
    582     ps_hrd->u1_du_cpb_removal_delay_increment_length_minus1 = 0;
    583     ps_hrd->u1_sub_pic_cpb_params_in_pic_timing_sei_flag = 0;
    584     ps_hrd->u1_dpb_output_delay_du_length_minus1 = 0;
    585 
    586     ps_hrd->u4_bit_rate_scale = 0;
    587     ps_hrd->u4_cpb_size_scale = 0;
    588     ps_hrd->u4_cpb_size_du_scale = 0;
    589 
    590     ps_hrd->u1_initial_cpb_removal_delay_length_minus1 = 23;
    591     ps_hrd->u1_au_cpb_removal_delay_length_minus1 = 23;
    592     ps_hrd->u1_dpb_output_delay_length_minus1 = 23;
    593 
    594     if(common_info_present_flag)
    595     {
    596         BITS_PARSE("nal_hrd_parameters_present_flag", ps_hrd->u1_nal_hrd_parameters_present_flag, ps_bitstrm, 1);
    597         BITS_PARSE("vcl_hrd_parameters_present_flag", ps_hrd->u1_vcl_hrd_parameters_present_flag, ps_bitstrm, 1);
    598 
    599         if(ps_hrd->u1_nal_hrd_parameters_present_flag  ||  ps_hrd->u1_vcl_hrd_parameters_present_flag)
    600         {
    601             BITS_PARSE("sub_pic_cpb_params_present_flag", ps_hrd->u1_sub_pic_cpb_params_present_flag, ps_bitstrm, 1);
    602             if(ps_hrd->u1_sub_pic_cpb_params_present_flag)
    603             {
    604                 BITS_PARSE("tick_divisor_minus2", ps_hrd->u1_tick_divisor_minus2, ps_bitstrm, 8);
    605                 BITS_PARSE("du_cpb_removal_delay_increment_length_minus1", ps_hrd->u1_du_cpb_removal_delay_increment_length_minus1, ps_bitstrm, 5);
    606                 BITS_PARSE("sub_pic_cpb_params_in_pic_timing_sei_flag", ps_hrd->u1_sub_pic_cpb_params_in_pic_timing_sei_flag, ps_bitstrm, 1);
    607                 BITS_PARSE("dpb_output_delay_du_length_minus1", ps_hrd->u1_dpb_output_delay_du_length_minus1, ps_bitstrm, 5);
    608             }
    609 
    610             BITS_PARSE("bit_rate_scale", ps_hrd->u4_bit_rate_scale, ps_bitstrm, 4);
    611             BITS_PARSE("cpb_size_scale", ps_hrd->u4_cpb_size_scale, ps_bitstrm, 4);
    612             if(ps_hrd->u1_sub_pic_cpb_params_present_flag)
    613                 BITS_PARSE("cpb_size_du_scale", ps_hrd->u4_cpb_size_du_scale, ps_bitstrm, 4);
    614 
    615             BITS_PARSE("initial_cpb_removal_delay_length_minus1", ps_hrd->u1_initial_cpb_removal_delay_length_minus1, ps_bitstrm, 5);
    616             BITS_PARSE("au_cpb_removal_delay_length_minus1", ps_hrd->u1_au_cpb_removal_delay_length_minus1, ps_bitstrm, 5);
    617             BITS_PARSE("dpb_output_delay_length_minus1", ps_hrd->u1_dpb_output_delay_length_minus1, ps_bitstrm, 5);
    618         }
    619     }
    620 
    621 
    622     for(i = 0; i <= max_num_sub_layers_minus1; i++)
    623     {
    624         BITS_PARSE("fixed_pic_rate_general_flag[ i ]", ps_hrd->au1_fixed_pic_rate_general_flag[i], ps_bitstrm, 1);
    625 
    626         ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i] = 1;
    627         ps_hrd->au1_elemental_duration_in_tc_minus1[i] = 0;
    628         ps_hrd->au1_low_delay_hrd_flag[i] = 0;
    629         ps_hrd->au1_cpb_cnt_minus1[i] = 0;
    630 
    631         if(!ps_hrd->au1_fixed_pic_rate_general_flag[i])
    632             BITS_PARSE("fixed_pic_rate_within_cvs_flag[ i ]", ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i], ps_bitstrm, 1);
    633 
    634         if(ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i])
    635         {
    636             UEV_PARSE("elemental_duration_in_tc_minus1[ i ]", ps_hrd->au1_elemental_duration_in_tc_minus1[i], ps_bitstrm);
    637         }
    638         else
    639         {
    640             BITS_PARSE("low_delay_hrd_flag[ i ]", ps_hrd->au1_low_delay_hrd_flag[i], ps_bitstrm, 1);
    641         }
    642 
    643         if(!ps_hrd->au1_low_delay_hrd_flag[i])
    644             UEV_PARSE("cpb_cnt_minus1[ i ]", ps_hrd->au1_cpb_cnt_minus1[i], ps_bitstrm);
    645 
    646         if(ps_hrd->au1_cpb_cnt_minus1[i] >= (MAX_CPB_CNT - 1))
    647             return IHEVCD_INVALID_PARAMETER;
    648 
    649         if(ps_hrd->u1_nal_hrd_parameters_present_flag)
    650             ihevcd_parse_sub_layer_hrd_parameters(ps_bitstrm,
    651                                                   &ps_hrd->as_sub_layer_hrd_params[i],
    652                                                   ps_hrd->au1_cpb_cnt_minus1[i],
    653                                                   ps_hrd->u1_sub_pic_cpb_params_present_flag);
    654 
    655         if(ps_hrd->u1_vcl_hrd_parameters_present_flag)
    656             ihevcd_parse_sub_layer_hrd_parameters(ps_bitstrm,
    657                                                   &ps_hrd->as_sub_layer_hrd_params[i],
    658                                                   ps_hrd->au1_cpb_cnt_minus1[i],
    659                                                   ps_hrd->u1_sub_pic_cpb_params_present_flag);
    660     }
    661 
    662     return ret;
    663 }
    664 
    665 
    666 static WORD32 ihevcd_parse_vui_parameters(bitstrm_t *ps_bitstrm,
    667                                           vui_t *ps_vui,
    668                                           WORD32 sps_max_sub_layers_minus1)
    669 {
    670     WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    671     UWORD16 u2_sar_width = 0;
    672     UWORD16 u2_sar_height = 0;
    673 
    674     BITS_PARSE("aspect_ratio_info_present_flag", ps_vui->u1_aspect_ratio_info_present_flag, ps_bitstrm, 1);
    675 
    676     ps_vui->u1_aspect_ratio_idc = SAR_UNUSED;
    677     u2_sar_width = 0;
    678     u2_sar_height = 0;
    679     if(ps_vui->u1_aspect_ratio_info_present_flag)
    680     {
    681         BITS_PARSE("aspect_ratio_idc", ps_vui->u1_aspect_ratio_idc, ps_bitstrm, 8);
    682         switch(ps_vui->u1_aspect_ratio_idc)
    683         {
    684             case SAR_1_1:
    685                 u2_sar_width = 1;
    686                 u2_sar_height = 1;
    687                 break;
    688             case SAR_12_11:
    689                 u2_sar_width = 12;
    690                 u2_sar_height = 11;
    691                 break;
    692             case SAR_10_11:
    693                 u2_sar_width = 10;
    694                 u2_sar_height = 11;
    695                 break;
    696             case SAR_16_11:
    697                 u2_sar_width = 16;
    698                 u2_sar_height = 11;
    699                 break;
    700             case SAR_40_33:
    701                 u2_sar_width = 40;
    702                 u2_sar_height = 33;
    703                 break;
    704             case SAR_24_11:
    705                 u2_sar_width = 24;
    706                 u2_sar_height = 11;
    707                 break;
    708             case SAR_20_11:
    709                 u2_sar_width = 20;
    710                 u2_sar_height = 11;
    711                 break;
    712             case SAR_32_11:
    713                 u2_sar_width = 32;
    714                 u2_sar_height = 11;
    715                 break;
    716             case SAR_80_33:
    717                 u2_sar_width = 80;
    718                 u2_sar_height = 33;
    719                 break;
    720             case SAR_18_11:
    721                 u2_sar_width = 18;
    722                 u2_sar_height = 11;
    723                 break;
    724             case SAR_15_11:
    725                 u2_sar_width = 15;
    726                 u2_sar_height = 11;
    727                 break;
    728             case SAR_64_33:
    729                 u2_sar_width = 64;
    730                 u2_sar_height = 33;
    731                 break;
    732             case SAR_160_99:
    733                 u2_sar_width = 160;
    734                 u2_sar_height = 99;
    735                 break;
    736             case SAR_4_3:
    737                 u2_sar_width = 4;
    738                 u2_sar_height = 3;
    739                 break;
    740             case SAR_3_2:
    741                 u2_sar_width = 3;
    742                 u2_sar_height = 2;
    743                 break;
    744             case SAR_2_1:
    745                 u2_sar_width = 2;
    746                 u2_sar_height = 1;
    747                 break;
    748             case EXTENDED_SAR:
    749                 BITS_PARSE("sar_width", u2_sar_width, ps_bitstrm, 16);
    750                 BITS_PARSE("sar_height", u2_sar_height, ps_bitstrm, 16);
    751                 break;
    752             default:
    753                 u2_sar_width = 0;
    754                 u2_sar_height = 0;
    755                 break;
    756         }
    757     }
    758 
    759     ps_vui->u2_sar_width    = u2_sar_width;
    760     ps_vui->u2_sar_height   = u2_sar_height;
    761 
    762     BITS_PARSE("overscan_info_present_flag", ps_vui->u1_overscan_info_present_flag, ps_bitstrm, 1);
    763     ps_vui->u1_overscan_appropriate_flag = 0;
    764     if(ps_vui->u1_overscan_info_present_flag)
    765         BITS_PARSE("overscan_appropriate_flag", ps_vui->u1_overscan_appropriate_flag, ps_bitstrm, 1);
    766 
    767     BITS_PARSE("video_signal_type_present_flag", ps_vui->u1_video_signal_type_present_flag, ps_bitstrm, 1);
    768     ps_vui->u1_video_format = VID_FMT_UNSPECIFIED;
    769     ps_vui->u1_video_full_range_flag = 0;
    770     ps_vui->u1_colour_description_present_flag = 0;
    771     ps_vui->u1_colour_primaries = 2;
    772     ps_vui->u1_transfer_characteristics = 2;
    773     ps_vui->u1_matrix_coefficients = 2;
    774 
    775     if(ps_vui->u1_video_signal_type_present_flag)
    776     {
    777         BITS_PARSE("video_format", ps_vui->u1_video_format, ps_bitstrm, 3);
    778         BITS_PARSE("video_full_range_flag", ps_vui->u1_video_full_range_flag, ps_bitstrm, 1);
    779         BITS_PARSE("colour_description_present_flag", ps_vui->u1_colour_description_present_flag, ps_bitstrm, 1);
    780         if(ps_vui->u1_colour_description_present_flag)
    781         {
    782             BITS_PARSE("colour_primaries", ps_vui->u1_colour_primaries, ps_bitstrm, 8);
    783             BITS_PARSE("transfer_characteristics", ps_vui->u1_transfer_characteristics, ps_bitstrm, 8);
    784             BITS_PARSE("matrix_coeffs", ps_vui->u1_matrix_coefficients, ps_bitstrm, 8);
    785         }
    786     }
    787 
    788     BITS_PARSE("chroma_loc_info_present_flag", ps_vui->u1_chroma_loc_info_present_flag, ps_bitstrm, 1);
    789     ps_vui->u1_chroma_sample_loc_type_top_field = 0;
    790     ps_vui->u1_chroma_sample_loc_type_bottom_field = 0;
    791     if(ps_vui->u1_chroma_loc_info_present_flag)
    792     {
    793         UEV_PARSE("chroma_sample_loc_type_top_field", ps_vui->u1_chroma_sample_loc_type_top_field, ps_bitstrm);
    794         UEV_PARSE("chroma_sample_loc_type_bottom_field", ps_vui->u1_chroma_sample_loc_type_bottom_field, ps_bitstrm);
    795     }
    796 
    797     BITS_PARSE("neutral_chroma_indication_flag", ps_vui->u1_neutral_chroma_indication_flag, ps_bitstrm, 1);
    798     BITS_PARSE("field_seq_flag", ps_vui->u1_field_seq_flag, ps_bitstrm, 1);
    799     BITS_PARSE("frame_field_info_present_flag", ps_vui->u1_frame_field_info_present_flag, ps_bitstrm, 1);
    800     BITS_PARSE("default_display_window_flag", ps_vui->u1_default_display_window_flag, ps_bitstrm, 1);
    801     ps_vui->u4_def_disp_win_left_offset = 0;
    802     ps_vui->u4_def_disp_win_right_offset = 0;
    803     ps_vui->u4_def_disp_win_top_offset = 0;
    804     ps_vui->u4_def_disp_win_bottom_offset = 0;
    805     if(ps_vui->u1_default_display_window_flag)
    806     {
    807         UEV_PARSE("def_disp_win_left_offset", ps_vui->u4_def_disp_win_left_offset, ps_bitstrm);
    808         UEV_PARSE("def_disp_win_right_offset", ps_vui->u4_def_disp_win_right_offset, ps_bitstrm);
    809         UEV_PARSE("def_disp_win_top_offset", ps_vui->u4_def_disp_win_top_offset, ps_bitstrm);
    810         UEV_PARSE("def_disp_win_bottom_offset", ps_vui->u4_def_disp_win_bottom_offset, ps_bitstrm);
    811     }
    812 
    813     BITS_PARSE("vui_timing_info_present_flag", ps_vui->u1_vui_timing_info_present_flag, ps_bitstrm, 1);
    814     if(ps_vui->u1_vui_timing_info_present_flag)
    815     {
    816         BITS_PARSE("vui_num_units_in_tick", ps_vui->u4_vui_num_units_in_tick, ps_bitstrm, 32);
    817         BITS_PARSE("vui_time_scale", ps_vui->u4_vui_time_scale, ps_bitstrm, 32);
    818         BITS_PARSE("vui_poc_proportional_to_timing_flag", ps_vui->u1_poc_proportional_to_timing_flag, ps_bitstrm, 1);
    819         if(ps_vui->u1_poc_proportional_to_timing_flag)
    820             UEV_PARSE("vui_num_ticks_poc_diff_one_minus1", ps_vui->u1_num_ticks_poc_diff_one_minus1, ps_bitstrm);
    821 
    822         BITS_PARSE("vui_hrd_parameters_present_flag", ps_vui->u1_vui_hrd_parameters_present_flag, ps_bitstrm, 1);
    823         if(ps_vui->u1_vui_hrd_parameters_present_flag)
    824         {
    825             ret = ihevcd_parse_hrd_parameters(ps_bitstrm, &ps_vui->s_vui_hrd_parameters, 1, sps_max_sub_layers_minus1);
    826             RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
    827         }
    828     }
    829 
    830     BITS_PARSE("bitstream_restriction_flag", ps_vui->u1_bitstream_restriction_flag, ps_bitstrm, 1);
    831     ps_vui->u1_tiles_fixed_structure_flag = 0;
    832     ps_vui->u1_motion_vectors_over_pic_boundaries_flag = 1;
    833     ps_vui->u1_restricted_ref_pic_lists_flag = 0;
    834     ps_vui->u4_min_spatial_segmentation_idc = 0;
    835     ps_vui->u1_max_bytes_per_pic_denom = 2;
    836     ps_vui->u1_max_bits_per_mincu_denom = 1;
    837     ps_vui->u1_log2_max_mv_length_horizontal = 15;
    838     ps_vui->u1_log2_max_mv_length_vertical = 15;
    839     if(ps_vui->u1_bitstream_restriction_flag)
    840     {
    841         BITS_PARSE("tiles_fixed_structure_flag", ps_vui->u1_tiles_fixed_structure_flag, ps_bitstrm, 1);
    842         BITS_PARSE("motion_vectors_over_pic_boundaries_flag", ps_vui->u1_motion_vectors_over_pic_boundaries_flag, ps_bitstrm, 1);
    843         BITS_PARSE("restricted_ref_pic_lists_flag", ps_vui->u1_restricted_ref_pic_lists_flag, ps_bitstrm, 1);
    844 
    845         UEV_PARSE("min_spatial_segmentation_idc", ps_vui->u4_min_spatial_segmentation_idc, ps_bitstrm);
    846         UEV_PARSE("max_bytes_per_pic_denom", ps_vui->u1_max_bytes_per_pic_denom, ps_bitstrm);
    847         UEV_PARSE("max_bits_per_min_cu_denom", ps_vui->u1_max_bits_per_mincu_denom, ps_bitstrm);
    848         UEV_PARSE("log2_max_mv_length_horizontal", ps_vui->u1_log2_max_mv_length_horizontal, ps_bitstrm);
    849         UEV_PARSE("log2_max_mv_length_vertical", ps_vui->u1_log2_max_mv_length_vertical, ps_bitstrm);
    850     }
    851 
    852     return ret;
    853 }
    854 
    855 /**
    856 *******************************************************************************
    857 *
    858 * @brief
    859 *  Parses profile tier and level info for either general layer of sub_layer
    860 *
    861 * @par   Description
    862 *  Parses profile tier and level info for either general layer of sub_layer
    863 * as per section 7.3.3
    864 *
    865 * Since the same function is called for parsing general_profile and
    866 * sub_layer_profile etc, variables do not specify whether the syntax is
    867 * for general or sub_layer. Similarly trace functions also do not differentiate
    868 *
    869 * @param[in] ps_bitstrm
    870 *  Pointer to bitstream structure
    871 *
    872 * @param[out] ps_ptl
    873 *  Pointer to profile, tier level structure
    874 *
    875 * @returns Error code from IHEVCD_ERROR_T
    876 *
    877 * @remarks
    878 *
    879 *******************************************************************************
    880 */
    881 
    882 static IHEVCD_ERROR_T ihevcd_parse_profile_tier_level_layer(bitstrm_t *ps_bitstrm,
    883                                                             profile_tier_lvl_t *ps_ptl)
    884 {
    885     WORD32 value;
    886     WORD32 i;
    887     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    888 
    889     BITS_PARSE("XXX_profile_space[]", value, ps_bitstrm, 2);
    890     ps_ptl->i1_profile_space = value;
    891 
    892     BITS_PARSE("XXX_tier_flag[]", value, ps_bitstrm, 1);
    893     ps_ptl->i1_tier_flag = value;
    894 
    895     BITS_PARSE("XXX_profile_idc[]", value, ps_bitstrm, 5);
    896     ps_ptl->i1_profile_idc = value;
    897 
    898     for(i = 0; i < MAX_PROFILE_COMPATBLTY; i++)
    899     {
    900         BITS_PARSE("XXX_profile_compatibility_flag[][j]", value, ps_bitstrm, 1);
    901         ps_ptl->ai1_profile_compatibility_flag[i] = value;
    902     }
    903 
    904     BITS_PARSE("general_progressive_source_flag", value, ps_bitstrm, 1);
    905     ps_ptl->i1_general_progressive_source_flag = value;
    906 
    907     BITS_PARSE("general_interlaced_source_flag", value, ps_bitstrm, 1);
    908     ps_ptl->i1_general_interlaced_source_flag = value;
    909 
    910     BITS_PARSE("general_non_packed_constraint_flag", value, ps_bitstrm, 1);
    911     ps_ptl->i1_general_non_packed_constraint_flag = value;
    912 
    913     BITS_PARSE("general_frame_only_constraint_flag", value, ps_bitstrm, 1);
    914     ps_ptl->i1_frame_only_constraint_flag = value;
    915 
    916     BITS_PARSE("XXX_reserved_zero_44bits[0..15]", value, ps_bitstrm, 16);
    917 
    918     BITS_PARSE("XXX_reserved_zero_44bits[16..31]", value, ps_bitstrm, 16);
    919 
    920     BITS_PARSE("XXX_reserved_zero_44bits[32..43]", value, ps_bitstrm, 12);
    921     return ret;
    922 }
    923 
    924 
    925 /**
    926 *******************************************************************************
    927 *
    928 * @brief
    929 *  Parses profile tier and level info
    930 *
    931 * @par   Description
    932 *  Parses profile tier and level info as per section 7.3.3
    933 * Called during VPS and SPS parsing
    934 * calls ihevcd_parse_profile_tier_level() for general layer and each sub_layers
    935 *
    936 * @param[in] ps_bitstrm
    937 *  Pointer to bitstream structure
    938 *
    939 * @param[out] ps_ptl
    940 *  Pointer to structure that contains profile, tier level for each layers
    941 *
    942 * @param[in] profile_present
    943 *  Flag to indicate if profile data is present
    944 *
    945 * @param[in] max_num_sub_layers
    946 *  Number of sub layers present
    947 *
    948 * @returns Error code from IHEVCD_ERROR_T
    949 *
    950 * @remarks
    951 *
    952 *******************************************************************************
    953 */
    954 
    955 static IHEVCD_ERROR_T ihevcd_profile_tier_level(bitstrm_t *ps_bitstrm,
    956                                                 profile_tier_lvl_info_t *ps_ptl,
    957                                                 WORD32 profile_present,
    958                                                 WORD32 max_num_sub_layers)
    959 {
    960     WORD32 value;
    961     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    962     WORD32 i;
    963 
    964     if(profile_present)
    965     {
    966         ret = ihevcd_parse_profile_tier_level_layer(ps_bitstrm, &ps_ptl->s_ptl_gen);
    967         if((IHEVCD_ERROR_T)IHEVCD_SUCCESS != ret)
    968         {
    969             return ret;
    970         }
    971     }
    972 
    973     BITS_PARSE("general_level_idc", value, ps_bitstrm, 8);
    974     ps_ptl->s_ptl_gen.u1_level_idc = value;
    975 
    976 
    977     for(i = 0; i < max_num_sub_layers; i++)
    978     {
    979         BITS_PARSE("sub_layer_profile_present_flag[i]", value, ps_bitstrm, 1);
    980         ps_ptl->ai1_sub_layer_profile_present_flag[i] = value;
    981 
    982         BITS_PARSE("sub_layer_level_present_flag[i]", value, ps_bitstrm, 1);
    983         ps_ptl->ai1_sub_layer_level_present_flag[i] = value;
    984     }
    985 
    986     if(max_num_sub_layers > 0)
    987     {
    988         for(i = max_num_sub_layers; i < 8; i++)
    989         {
    990             BITS_PARSE("reserved_zero_2bits", value, ps_bitstrm, 2);
    991         }
    992     }
    993 
    994     for(i = 0; i < max_num_sub_layers; i++)
    995     {
    996         if(ps_ptl->ai1_sub_layer_profile_present_flag[i])
    997         {
    998             ret = ihevcd_parse_profile_tier_level_layer(ps_bitstrm,
    999                                                         &ps_ptl->as_ptl_sub[i]);
   1000         }
   1001         if(ps_ptl->ai1_sub_layer_level_present_flag[i])
   1002         {
   1003             BITS_PARSE("sub_layer_level_idc[i]", value, ps_bitstrm, 8);
   1004             ps_ptl->as_ptl_sub[i].u1_level_idc = value;
   1005 
   1006         }
   1007     }
   1008 
   1009 
   1010 
   1011     return ret;
   1012 }
   1013 
   1014 /**
   1015 *******************************************************************************
   1016 *
   1017 * @brief
   1018 *  Parses Scaling List Data syntax
   1019 *
   1020 * @par Description:
   1021 *  Parses Scaling List Data syntax as per Section: 7.3.6
   1022 *
   1023 * @param[in] ps_codec
   1024 *  Pointer to codec context
   1025 *
   1026 * @returns  Error code from IHEVCD_ERROR_T
   1027 *
   1028 * @remarks
   1029 *
   1030 *
   1031 *******************************************************************************
   1032 */
   1033 IHEVCD_ERROR_T  ihevcd_scaling_list_data(codec_t *ps_codec, WORD16 *pi2_scaling_mat)
   1034 {
   1035     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   1036     WORD32 size_id;
   1037     WORD32 matrix_id;
   1038     WORD32 value, dc_value = 0;
   1039     WORD32 next_coef;
   1040     WORD32 coef_num;
   1041     WORD32 i, j, offset;
   1042     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
   1043     WORD16 *pi2_scaling_mat_offset;
   1044     WORD32 scaling_mat_offset[] = { 0, 16, 32, 48, 64, 80, 96, 160, 224, 288, 352, 416, 480, 736, 992, 1248, 1504, 1760, 2016, 3040 };
   1045     UWORD8 *scan_table;
   1046 
   1047     for(size_id = 0; size_id < 4; size_id++)
   1048     {
   1049         for(matrix_id = 0; matrix_id < ((size_id == 3) ? 2 : 6); matrix_id++)
   1050         {
   1051             WORD32 scaling_list_pred_mode_flag;
   1052             WORD32 scaling_list_delta_coef;
   1053             BITS_PARSE("scaling_list_pred_mode_flag", scaling_list_pred_mode_flag, ps_bitstrm, 1);
   1054 
   1055             offset = size_id * 6 + matrix_id;
   1056             pi2_scaling_mat_offset = pi2_scaling_mat + scaling_mat_offset[offset];
   1057 
   1058             if(!scaling_list_pred_mode_flag)
   1059             {
   1060                 WORD32 num_elements;
   1061                 UEV_PARSE("scaling_list_pred_matrix_id_delta", value,
   1062                           ps_bitstrm);
   1063                 value = CLIP3(value, 0, matrix_id);
   1064 
   1065                 num_elements = (1 << (4 + (size_id << 1)));
   1066                 if(0 != value)
   1067                     memcpy(pi2_scaling_mat_offset, pi2_scaling_mat_offset - value * num_elements, num_elements * sizeof(WORD16));
   1068             }
   1069             else
   1070             {
   1071                 next_coef = 8;
   1072                 coef_num = MIN(64, (1 << (4 + (size_id << 1))));
   1073 
   1074                 if(size_id > 1)
   1075                 {
   1076                     SEV_PARSE("scaling_list_dc_coef_minus8", value,
   1077                               ps_bitstrm);
   1078 
   1079                     next_coef = value + 8;
   1080                     dc_value = next_coef;
   1081                 }
   1082                 if(size_id < 2)
   1083                 {
   1084                     scan_table = (UWORD8 *)gapv_ihevc_invscan[size_id + 1];
   1085 
   1086                     for(i = 0; i < coef_num; i++)
   1087                     {
   1088                         SEV_PARSE("scaling_list_delta_coef",
   1089                                   scaling_list_delta_coef, ps_bitstrm);
   1090                         next_coef = (next_coef + scaling_list_delta_coef + 256)
   1091                                         % 256;
   1092                         pi2_scaling_mat_offset[scan_table[i]] = next_coef;
   1093                     }
   1094                 }
   1095                 else if(size_id == 2)
   1096                 {
   1097                     scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
   1098 
   1099                     for(i = 0; i < coef_num; i++)
   1100                     {
   1101                         SEV_PARSE("scaling_list_delta_coef",
   1102                                   scaling_list_delta_coef, ps_bitstrm);
   1103                         next_coef = (next_coef + scaling_list_delta_coef + 256)
   1104                                         % 256;
   1105 
   1106                         offset = scan_table[i];
   1107                         offset = (offset >> 3) * 16 * 2 + (offset & 0x7) * 2;
   1108                         pi2_scaling_mat_offset[offset] = next_coef;
   1109                         pi2_scaling_mat_offset[offset + 1] = next_coef;
   1110                         pi2_scaling_mat_offset[offset + 16] = next_coef;
   1111                         pi2_scaling_mat_offset[offset + 16 + 1] = next_coef;
   1112                     }
   1113                     pi2_scaling_mat_offset[0] = dc_value;
   1114                 }
   1115                 else
   1116                 {
   1117                     scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
   1118 
   1119                     for(i = 0; i < coef_num; i++)
   1120                     {
   1121                         SEV_PARSE("scaling_list_delta_coef",
   1122                                   scaling_list_delta_coef, ps_bitstrm);
   1123                         next_coef = (next_coef + scaling_list_delta_coef + 256)
   1124                                         % 256;
   1125 
   1126                         offset = scan_table[i];
   1127                         offset = (offset >> 3) * 32 * 4 + (offset & 0x7) * 4;
   1128 
   1129                         for(j = 0; j < 4; j++)
   1130                         {
   1131                             pi2_scaling_mat_offset[offset + j * 32] = next_coef;
   1132                             pi2_scaling_mat_offset[offset + 1 + j * 32] = next_coef;
   1133                             pi2_scaling_mat_offset[offset + 2 + j * 32] = next_coef;
   1134                             pi2_scaling_mat_offset[offset + 3 + j * 32] = next_coef;
   1135                         }
   1136                         pi2_scaling_mat_offset[0] = dc_value;
   1137                     }
   1138                 }
   1139             }
   1140         }
   1141     }
   1142 
   1143     return ret;
   1144 }
   1145 
   1146 /**
   1147 *******************************************************************************
   1148 *
   1149 * @brief
   1150 *  Parses VPS (Video Parameter Set)
   1151 *
   1152 * @par Description:
   1153 *  Parse Video Parameter Set as per Section 7.3.2.1
   1154 * update vps structure corresponding to vps ID
   1155 * Till parsing VPS id, the elements are stored in local variables and are copied
   1156 * later
   1157 *
   1158 * @param[in] ps_codec
   1159 *  Pointer to codec context.
   1160 *
   1161 * @returns Error code from IHEVCD_ERROR_T
   1162 *
   1163 * @remarks
   1164 *
   1165 *
   1166 *******************************************************************************
   1167 */
   1168 IHEVCD_ERROR_T ihevcd_parse_vps(codec_t *ps_codec)
   1169 {
   1170     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   1171     WORD32 i;
   1172     WORD32 value;
   1173     WORD32 vps_id;
   1174     vps_t *ps_vps;
   1175     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
   1176     BITS_PARSE("vps_video_parameter_set_id", value, ps_bitstrm, 4);
   1177     vps_id = value;
   1178 
   1179     if(vps_id >= MAX_VPS_CNT)
   1180     {
   1181         ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_VPS_ID;
   1182         return IHEVCD_UNSUPPORTED_VPS_ID;
   1183     }
   1184 
   1185 
   1186     ps_vps = (ps_codec->s_parse.ps_vps_base + vps_id);
   1187 
   1188     ps_vps->i1_vps_id = vps_id;
   1189 
   1190     BITS_PARSE("vps_reserved_three_2bits", value, ps_bitstrm, 2);
   1191     ASSERT(value == 3);
   1192 
   1193     BITS_PARSE("vps_max_layers_minus1", value, ps_bitstrm, 6);
   1194     //ps_vps->i1_vps_max_layers = value + 1;
   1195 
   1196 
   1197 
   1198     BITS_PARSE("vps_max_sub_layers_minus1", value, ps_bitstrm, 3);
   1199     ps_vps->i1_vps_max_sub_layers = value + 1;
   1200 
   1201     ASSERT(ps_vps->i1_vps_max_sub_layers < VPS_MAX_SUB_LAYERS);
   1202 
   1203     BITS_PARSE("vps_temporal_id_nesting_flag", value, ps_bitstrm, 1);
   1204     ps_vps->i1_vps_temporal_id_nesting_flag = value;
   1205 
   1206     BITS_PARSE("vps_reserved_ffff_16bits", value, ps_bitstrm, 16);
   1207     ASSERT(value == 0xFFFF);
   1208     // profile_and_level( 1, vps_max_sub_layers_minus1 )
   1209     ret = ihevcd_profile_tier_level(ps_bitstrm, &(ps_vps->s_ptl),
   1210                                     1, (ps_vps->i1_vps_max_sub_layers - 1));
   1211 
   1212     BITS_PARSE("vps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
   1213     ps_vps->i1_sub_layer_ordering_info_present_flag = value;
   1214     i = (ps_vps->i1_sub_layer_ordering_info_present_flag ?
   1215                     0 : (ps_vps->i1_vps_max_sub_layers - 1));
   1216     for(; i < ps_vps->i1_vps_max_sub_layers; i++)
   1217     {
   1218         UEV_PARSE("vps_max_dec_pic_buffering[i]", value, ps_bitstrm);
   1219         ps_vps->ai1_vps_max_dec_pic_buffering[i] = value;
   1220 
   1221         /* vps_num_reorder_pics (no max) used in print in order to match with HM */
   1222         UEV_PARSE("vps_num_reorder_pics[i]", value, ps_bitstrm);
   1223         ps_vps->ai1_vps_max_num_reorder_pics[i] = value;
   1224 
   1225         UEV_PARSE("vps_max_latency_increase[i]", value, ps_bitstrm);
   1226         ps_vps->ai1_vps_max_latency_increase[i] = value;
   1227     }
   1228 
   1229 
   1230 
   1231     BITS_PARSE("vps_max_layer_id", value, ps_bitstrm, 6);
   1232     //ps_vps->i1_vps_max_layer_id  = value;
   1233 
   1234     UEV_PARSE("vps_num_layer_sets_minus1", value, ps_bitstrm);
   1235     //ps_vps->i1_vps_num_layer_sets  = value + 1;
   1236 
   1237     BITS_PARSE("vps_timing_info_present_flag", value, ps_bitstrm, 1);
   1238     //ps_vps->i1_vps_timing_info_present_flag  = value;
   1239 
   1240 
   1241 
   1242     return ret;
   1243 }
   1244 
   1245 /**
   1246 *******************************************************************************
   1247 *
   1248 * @brief
   1249 *  Parses SPS (Sequence Parameter Set)
   1250 * sequence_parameter_set_rbsp()
   1251 *
   1252 * @par Description:
   1253 *  Parse Sequence Parameter Set as per section  Section: 7.3.2.2
   1254 * The sps is written to a temporary buffer and copied later to the
   1255 * appropriate location
   1256 *
   1257 * @param[in] ps_codec
   1258 *  Pointer to codec context
   1259 *
   1260 * @returns Error code from IHEVCD_ERROR_T
   1261 *
   1262 * @remarks
   1263 *
   1264 *
   1265 *******************************************************************************
   1266 */
   1267 IHEVCD_ERROR_T ihevcd_parse_sps(codec_t *ps_codec)
   1268 {
   1269     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   1270     WORD32 value;
   1271 
   1272     WORD32 i;
   1273     WORD32 vps_id;
   1274     WORD32 sps_max_sub_layers;
   1275     WORD32 sps_id;
   1276     WORD32 sps_temporal_id_nesting_flag;
   1277     sps_t *ps_sps;
   1278     profile_tier_lvl_info_t s_ptl;
   1279     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
   1280     WORD32 ctb_log2_size_y = 0;
   1281 
   1282 
   1283     BITS_PARSE("video_parameter_set_id", value, ps_bitstrm, 4);
   1284     vps_id = value;
   1285     vps_id = CLIP3(vps_id, 0, MAX_VPS_CNT - 1);
   1286 
   1287     BITS_PARSE("sps_max_sub_layers_minus1", value, ps_bitstrm, 3);
   1288     sps_max_sub_layers = value + 1;
   1289     sps_max_sub_layers = CLIP3(sps_max_sub_layers, 1, 7);
   1290 
   1291     BITS_PARSE("sps_temporal_id_nesting_flag", value, ps_bitstrm, 1);
   1292     sps_temporal_id_nesting_flag = value;
   1293 
   1294     //profile_and_level( 1, sps_max_sub_layers_minus1 )
   1295     ret = ihevcd_profile_tier_level(ps_bitstrm, &(s_ptl), 1,
   1296                                     (sps_max_sub_layers - 1));
   1297 
   1298     UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
   1299     sps_id = value;
   1300 
   1301     if((sps_id >= MAX_SPS_CNT) || (sps_id < 0))
   1302     {
   1303         if(ps_codec->i4_sps_done)
   1304             return IHEVCD_UNSUPPORTED_SPS_ID;
   1305         else
   1306             sps_id = 0;
   1307     }
   1308 
   1309 
   1310     ps_sps = (ps_codec->s_parse.ps_sps_base + MAX_SPS_CNT - 1);
   1311     /* Reset SPS to zero */
   1312     {
   1313         WORD16 *pi2_scaling_mat = ps_sps->pi2_scaling_mat;
   1314         memset(ps_sps, 0, sizeof(sps_t));
   1315         ps_sps->pi2_scaling_mat = pi2_scaling_mat;
   1316     }
   1317     ps_sps->i1_sps_id = sps_id;
   1318     ps_sps->i1_vps_id = vps_id;
   1319     ps_sps->i1_sps_max_sub_layers = sps_max_sub_layers;
   1320     ps_sps->i1_sps_temporal_id_nesting_flag = sps_temporal_id_nesting_flag;
   1321     /* This is used only during initialization to get reorder count etc */
   1322     ps_codec->i4_sps_id = sps_id;
   1323     memcpy(&ps_sps->s_ptl, &s_ptl, sizeof(profile_tier_lvl_info_t));
   1324 
   1325     UEV_PARSE("chroma_format_idc", value, ps_bitstrm);
   1326     ps_sps->i1_chroma_format_idc = value;
   1327 
   1328     if(ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_YUV420)
   1329     {
   1330         ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
   1331         return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
   1332     }
   1333 
   1334     if(CHROMA_FMT_IDC_YUV444_PLANES == ps_sps->i1_chroma_format_idc)
   1335     {
   1336         BITS_PARSE("separate_colour_plane_flag", value, ps_bitstrm, 1);
   1337         ps_sps->i1_separate_colour_plane_flag = value;
   1338     }
   1339     else
   1340     {
   1341         ps_sps->i1_separate_colour_plane_flag = 0;
   1342     }
   1343 
   1344     UEV_PARSE("pic_width_in_luma_samples", value, ps_bitstrm);
   1345     ps_sps->i2_pic_width_in_luma_samples = value;
   1346 
   1347     UEV_PARSE("pic_height_in_luma_samples", value, ps_bitstrm);
   1348     ps_sps->i2_pic_height_in_luma_samples = value;
   1349 
   1350     if((0 >= ps_sps->i2_pic_width_in_luma_samples) || (0 >= ps_sps->i2_pic_height_in_luma_samples))
   1351         return IHEVCD_INVALID_PARAMETER;
   1352 
   1353     /* i2_pic_width_in_luma_samples and i2_pic_height_in_luma_samples
   1354        should be multiples of min_cb_size. Here these are aligned to 8,
   1355        i.e. smallest CB size */
   1356     ps_sps->i2_pic_width_in_luma_samples = ALIGN8(ps_sps->i2_pic_width_in_luma_samples);
   1357     ps_sps->i2_pic_height_in_luma_samples = ALIGN8(ps_sps->i2_pic_height_in_luma_samples);
   1358 
   1359     BITS_PARSE("pic_cropping_flag", value, ps_bitstrm, 1);
   1360     ps_sps->i1_pic_cropping_flag = value;
   1361 
   1362     if(ps_sps->i1_pic_cropping_flag)
   1363     {
   1364 
   1365         UEV_PARSE("pic_crop_left_offset", value, ps_bitstrm);
   1366         if (value >= ps_sps->i2_pic_width_in_luma_samples)
   1367         {
   1368             return IHEVCD_INVALID_PARAMETER;
   1369         }
   1370         ps_sps->i2_pic_crop_left_offset = value;
   1371 
   1372         UEV_PARSE("pic_crop_right_offset", value, ps_bitstrm);
   1373         if (value >= ps_sps->i2_pic_width_in_luma_samples)
   1374         {
   1375             return IHEVCD_INVALID_PARAMETER;
   1376         }
   1377         ps_sps->i2_pic_crop_right_offset = value;
   1378 
   1379         UEV_PARSE("pic_crop_top_offset", value, ps_bitstrm);
   1380         if (value >= ps_sps->i2_pic_height_in_luma_samples)
   1381         {
   1382             return IHEVCD_INVALID_PARAMETER;
   1383         }
   1384         ps_sps->i2_pic_crop_top_offset = value;
   1385 
   1386         UEV_PARSE("pic_crop_bottom_offset", value, ps_bitstrm);
   1387         if (value >= ps_sps->i2_pic_height_in_luma_samples)
   1388         {
   1389             return IHEVCD_INVALID_PARAMETER;
   1390         }
   1391         ps_sps->i2_pic_crop_bottom_offset = value;
   1392     }
   1393     else
   1394     {
   1395         ps_sps->i2_pic_crop_left_offset = 0;
   1396         ps_sps->i2_pic_crop_right_offset = 0;
   1397         ps_sps->i2_pic_crop_top_offset = 0;
   1398         ps_sps->i2_pic_crop_bottom_offset = 0;
   1399     }
   1400 
   1401 
   1402     UEV_PARSE("bit_depth_luma_minus8", value, ps_bitstrm);
   1403     if(0 != value)
   1404         return IHEVCD_UNSUPPORTED_BIT_DEPTH;
   1405 
   1406     UEV_PARSE("bit_depth_chroma_minus8", value, ps_bitstrm);
   1407     if(0 != value)
   1408         return IHEVCD_UNSUPPORTED_BIT_DEPTH;
   1409 
   1410     UEV_PARSE("log2_max_pic_order_cnt_lsb_minus4", value, ps_bitstrm);
   1411     ps_sps->i1_log2_max_pic_order_cnt_lsb = value + 4;
   1412 
   1413     BITS_PARSE("sps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
   1414     ps_sps->i1_sps_sub_layer_ordering_info_present_flag = value;
   1415 
   1416 
   1417     i = (ps_sps->i1_sps_sub_layer_ordering_info_present_flag ? 0 : (ps_sps->i1_sps_max_sub_layers - 1));
   1418     for(; i < ps_sps->i1_sps_max_sub_layers; i++)
   1419     {
   1420         UEV_PARSE("max_dec_pic_buffering", value, ps_bitstrm);
   1421         ps_sps->ai1_sps_max_dec_pic_buffering[i] = value + 1;
   1422 
   1423         if(ps_sps->ai1_sps_max_dec_pic_buffering[i] > MAX_DPB_SIZE)
   1424         {
   1425             return IHEVCD_INVALID_PARAMETER;
   1426         }
   1427 
   1428         UEV_PARSE("num_reorder_pics", value, ps_bitstrm);
   1429         ps_sps->ai1_sps_max_num_reorder_pics[i] = value;
   1430 
   1431         if(ps_sps->ai1_sps_max_num_reorder_pics[i] > ps_sps->ai1_sps_max_dec_pic_buffering[i])
   1432         {
   1433             return IHEVCD_INVALID_PARAMETER;
   1434         }
   1435 
   1436         UEV_PARSE("max_latency_increase", value, ps_bitstrm);
   1437         ps_sps->ai1_sps_max_latency_increase[i] = value;
   1438     }
   1439 
   1440     /* Check if sps_max_dec_pic_buffering or sps_max_num_reorder_pics
   1441        has changed */
   1442     if(0 != ps_codec->i4_first_pic_done)
   1443     {
   1444         sps_t *ps_sps_old = ps_codec->s_parse.ps_sps;
   1445         if(ps_sps_old->ai1_sps_max_dec_pic_buffering[ps_sps_old->i1_sps_max_sub_layers - 1] !=
   1446                     ps_sps->ai1_sps_max_dec_pic_buffering[ps_sps->i1_sps_max_sub_layers - 1])
   1447         {
   1448             ps_codec->i4_reset_flag = 1;
   1449             return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
   1450         }
   1451 
   1452         if(ps_sps_old->ai1_sps_max_num_reorder_pics[ps_sps_old->i1_sps_max_sub_layers - 1] !=
   1453                     ps_sps->ai1_sps_max_num_reorder_pics[ps_sps->i1_sps_max_sub_layers - 1])
   1454         {
   1455             ps_codec->i4_reset_flag = 1;
   1456             return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
   1457         }
   1458     }
   1459 
   1460     UEV_PARSE("log2_min_coding_block_size_minus3", value, ps_bitstrm);
   1461     ps_sps->i1_log2_min_coding_block_size = value + 3;
   1462 
   1463     UEV_PARSE("log2_diff_max_min_coding_block_size", value, ps_bitstrm);
   1464     ps_sps->i1_log2_diff_max_min_coding_block_size = value;
   1465 
   1466     ctb_log2_size_y = ps_sps->i1_log2_min_coding_block_size + ps_sps->i1_log2_diff_max_min_coding_block_size;
   1467 
   1468     UEV_PARSE("log2_min_transform_block_size_minus2", value, ps_bitstrm);
   1469     ps_sps->i1_log2_min_transform_block_size = value + 2;
   1470 
   1471     UEV_PARSE("log2_diff_max_min_transform_block_size", value, ps_bitstrm);
   1472     ps_sps->i1_log2_diff_max_min_transform_block_size = value;
   1473 
   1474     ps_sps->i1_log2_max_transform_block_size = ps_sps->i1_log2_min_transform_block_size +
   1475                     ps_sps->i1_log2_diff_max_min_transform_block_size;
   1476 
   1477     if ((ps_sps->i1_log2_max_transform_block_size < 0) ||
   1478                     (ps_sps->i1_log2_max_transform_block_size > MIN(ctb_log2_size_y, 5)))
   1479     {
   1480         return IHEVCD_INVALID_PARAMETER;
   1481     }
   1482 
   1483     ps_sps->i1_log2_ctb_size = ps_sps->i1_log2_min_coding_block_size +
   1484                     ps_sps->i1_log2_diff_max_min_coding_block_size;
   1485 
   1486     if((ps_sps->i1_log2_min_coding_block_size < 3) ||
   1487                     (ps_sps->i1_log2_min_transform_block_size < 2) ||
   1488                     (ps_sps->i1_log2_diff_max_min_transform_block_size < 0) ||
   1489                     (ps_sps->i1_log2_max_transform_block_size > ps_sps->i1_log2_ctb_size) ||
   1490                     (ps_sps->i1_log2_ctb_size < 4) ||
   1491                     (ps_sps->i1_log2_ctb_size > 6))
   1492     {
   1493         return IHEVCD_INVALID_PARAMETER;
   1494     }
   1495 
   1496     ps_sps->i1_log2_min_pcm_coding_block_size = 0;
   1497     ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = 0;
   1498 
   1499     UEV_PARSE("max_transform_hierarchy_depth_inter", value, ps_bitstrm);
   1500     ps_sps->i1_max_transform_hierarchy_depth_inter = value;
   1501 
   1502     UEV_PARSE("max_transform_hierarchy_depth_intra", value, ps_bitstrm);
   1503     ps_sps->i1_max_transform_hierarchy_depth_intra = value;
   1504 
   1505     /* String has a d (enabled) in order to match with HM */
   1506     BITS_PARSE("scaling_list_enabled_flag", value, ps_bitstrm, 1);
   1507     ps_sps->i1_scaling_list_enable_flag = value;
   1508 
   1509     if(ps_sps->i1_scaling_list_enable_flag)
   1510     {
   1511         COPY_DEFAULT_SCALING_LIST(ps_sps->pi2_scaling_mat);
   1512         BITS_PARSE("sps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
   1513         ps_sps->i1_sps_scaling_list_data_present_flag = value;
   1514 
   1515         if(ps_sps->i1_sps_scaling_list_data_present_flag)
   1516             ihevcd_scaling_list_data(ps_codec, ps_sps->pi2_scaling_mat);
   1517     }
   1518     else
   1519     {
   1520         COPY_FLAT_SCALING_LIST(ps_sps->pi2_scaling_mat);
   1521     }
   1522     /* String is asymmetric_motion_partitions_enabled_flag instead of amp_enabled_flag in order to match with HM */
   1523     BITS_PARSE("asymmetric_motion_partitions_enabled_flag", value, ps_bitstrm, 1);
   1524     ps_sps->i1_amp_enabled_flag = value;
   1525 
   1526     BITS_PARSE("sample_adaptive_offset_enabled_flag", value, ps_bitstrm, 1);
   1527     ps_sps->i1_sample_adaptive_offset_enabled_flag = value;
   1528 
   1529     BITS_PARSE("pcm_enabled_flag", value, ps_bitstrm, 1);
   1530     ps_sps->i1_pcm_enabled_flag = value;
   1531 
   1532     if(ps_sps->i1_pcm_enabled_flag)
   1533     {
   1534         BITS_PARSE("pcm_sample_bit_depth_luma", value, ps_bitstrm, 4);
   1535         ps_sps->i1_pcm_sample_bit_depth_luma = value + 1;
   1536 
   1537         BITS_PARSE("pcm_sample_bit_depth_chroma", value, ps_bitstrm, 4);
   1538         ps_sps->i1_pcm_sample_bit_depth_chroma = value + 1;
   1539 
   1540         UEV_PARSE("log2_min_pcm_coding_block_size_minus3", value, ps_bitstrm);
   1541         ps_sps->i1_log2_min_pcm_coding_block_size = value + 3;
   1542 
   1543         UEV_PARSE("log2_diff_max_min_pcm_coding_block_size", value, ps_bitstrm);
   1544         ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = value;
   1545         BITS_PARSE("pcm_loop_filter_disable_flag", value, ps_bitstrm, 1);
   1546         ps_sps->i1_pcm_loop_filter_disable_flag = value;
   1547 
   1548     }
   1549     UEV_PARSE("num_short_term_ref_pic_sets", value, ps_bitstrm);
   1550     ps_sps->i1_num_short_term_ref_pic_sets = value;
   1551 
   1552     ps_sps->i1_num_short_term_ref_pic_sets = CLIP3(ps_sps->i1_num_short_term_ref_pic_sets, 0, MAX_STREF_PICS_SPS);
   1553 
   1554     for(i = 0; i < ps_sps->i1_num_short_term_ref_pic_sets; i++)
   1555         ihevcd_short_term_ref_pic_set(ps_bitstrm, &ps_sps->as_stref_picset[0], ps_sps->i1_num_short_term_ref_pic_sets, i, &ps_sps->as_stref_picset[i]);
   1556 
   1557     BITS_PARSE("long_term_ref_pics_present_flag", value, ps_bitstrm, 1);
   1558     ps_sps->i1_long_term_ref_pics_present_flag = value;
   1559 
   1560     if(ps_sps->i1_long_term_ref_pics_present_flag)
   1561     {
   1562         UEV_PARSE("num_long_term_ref_pics_sps", value, ps_bitstrm);
   1563         ps_sps->i1_num_long_term_ref_pics_sps = value;
   1564 
   1565         for(i = 0; i < ps_sps->i1_num_long_term_ref_pics_sps; i++)
   1566         {
   1567             BITS_PARSE("lt_ref_pic_poc_lsb_sps[ i ]", value, ps_bitstrm, ps_sps->i1_log2_max_pic_order_cnt_lsb);
   1568             ps_sps->au2_lt_ref_pic_poc_lsb_sps[i] = value;
   1569 
   1570             BITS_PARSE("used_by_curr_pic_lt_sps_flag[ i ]", value, ps_bitstrm, 1);
   1571             ps_sps->ai1_used_by_curr_pic_lt_sps_flag[i] = value;
   1572         }
   1573     }
   1574 
   1575     BITS_PARSE("sps_temporal_mvp_enable_flag", value, ps_bitstrm, 1);
   1576     ps_sps->i1_sps_temporal_mvp_enable_flag = value;
   1577 
   1578     /* Print matches HM 8-2 */
   1579     BITS_PARSE("sps_strong_intra_smoothing_enable_flag", value, ps_bitstrm, 1);
   1580     ps_sps->i1_strong_intra_smoothing_enable_flag = value;
   1581 
   1582     BITS_PARSE("vui_parameters_present_flag", value, ps_bitstrm, 1);
   1583     ps_sps->i1_vui_parameters_present_flag = value;
   1584 
   1585     if(ps_sps->i1_vui_parameters_present_flag)
   1586     {
   1587         ret = ihevcd_parse_vui_parameters(ps_bitstrm,
   1588                                           &ps_sps->s_vui_parameters,
   1589                                           ps_sps->i1_sps_max_sub_layers - 1);
   1590         RETURN_IF((ret != (IHEVCD_ERROR_T)IHEVCD_SUCCESS), ret);
   1591     }
   1592 
   1593     BITS_PARSE("sps_extension_flag", value, ps_bitstrm, 1);
   1594 
   1595     if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
   1596     {
   1597         return IHEVCD_INVALID_PARAMETER;
   1598     }
   1599 
   1600     {
   1601         WORD32 numerator;
   1602         WORD32 ceil_offset;
   1603 
   1604         ceil_offset = (1 << ps_sps->i1_log2_ctb_size) - 1;
   1605         numerator = ps_sps->i2_pic_width_in_luma_samples;
   1606 
   1607         ps_sps->i2_pic_wd_in_ctb = ((numerator + ceil_offset) /
   1608                         (1 << ps_sps->i1_log2_ctb_size));
   1609 
   1610         numerator = ps_sps->i2_pic_height_in_luma_samples;
   1611         ps_sps->i2_pic_ht_in_ctb = ((numerator + ceil_offset) /
   1612                         (1 << ps_sps->i1_log2_ctb_size));
   1613 
   1614         ps_sps->i4_pic_size_in_ctb = ps_sps->i2_pic_ht_in_ctb *
   1615                         ps_sps->i2_pic_wd_in_ctb;
   1616 
   1617         if(0 == ps_codec->i4_sps_done)
   1618             ps_codec->s_parse.i4_next_ctb_indx = ps_sps->i4_pic_size_in_ctb;
   1619 
   1620         numerator = ps_sps->i2_pic_width_in_luma_samples;
   1621         ps_sps->i2_pic_wd_in_min_cb = numerator  /
   1622                         (1 << ps_sps->i1_log2_min_coding_block_size);
   1623 
   1624         numerator = ps_sps->i2_pic_height_in_luma_samples;
   1625         ps_sps->i2_pic_ht_in_min_cb = numerator  /
   1626                         (1 << ps_sps->i1_log2_min_coding_block_size);
   1627     }
   1628     if((0 != ps_codec->i4_first_pic_done) &&
   1629                     ((ps_codec->i4_wd != ps_sps->i2_pic_width_in_luma_samples) ||
   1630                     (ps_codec->i4_ht != ps_sps->i2_pic_height_in_luma_samples)))
   1631     {
   1632         ps_codec->i4_reset_flag = 1;
   1633         return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
   1634     }
   1635 
   1636     if((ps_sps->i2_pic_width_in_luma_samples > MAX_WD) ||
   1637                     ((ps_sps->i2_pic_width_in_luma_samples * ps_sps->i2_pic_height_in_luma_samples) >
   1638                     (MAX_WD * MAX_HT)))
   1639     {
   1640         return (IHEVCD_ERROR_T)IVD_STREAM_WIDTH_HEIGHT_NOT_SUPPORTED;
   1641     }
   1642 
   1643     /* Update display width and display height */
   1644     {
   1645         WORD32 disp_wd, disp_ht;
   1646         WORD32 crop_unit_x, crop_unit_y;
   1647         crop_unit_x = 1;
   1648         crop_unit_y = 1;
   1649 
   1650         if(CHROMA_FMT_IDC_YUV420 == ps_sps->i1_chroma_format_idc)
   1651         {
   1652             crop_unit_x = 2;
   1653             crop_unit_y = 2;
   1654         }
   1655 
   1656         disp_wd = ps_sps->i2_pic_width_in_luma_samples;
   1657         disp_wd -= ps_sps->i2_pic_crop_left_offset * crop_unit_x;
   1658         disp_wd -= ps_sps->i2_pic_crop_right_offset * crop_unit_x;
   1659 
   1660 
   1661         disp_ht = ps_sps->i2_pic_height_in_luma_samples;
   1662         disp_ht -= ps_sps->i2_pic_crop_top_offset * crop_unit_y;
   1663         disp_ht -= ps_sps->i2_pic_crop_bottom_offset * crop_unit_y;
   1664 
   1665         if((0 >= disp_wd) || (0 >= disp_ht))
   1666             return IHEVCD_INVALID_PARAMETER;
   1667 
   1668         ps_codec->i4_disp_wd = disp_wd;
   1669         ps_codec->i4_disp_ht = disp_ht;
   1670 
   1671 
   1672         ps_codec->i4_wd = ps_sps->i2_pic_width_in_luma_samples;
   1673         ps_codec->i4_ht = ps_sps->i2_pic_height_in_luma_samples;
   1674 
   1675         {
   1676             WORD32 ref_strd;
   1677             ref_strd = ALIGN32(ps_sps->i2_pic_width_in_luma_samples + PAD_WD);
   1678             if(ps_codec->i4_strd < ref_strd)
   1679             {
   1680                 ps_codec->i4_strd = ref_strd;
   1681             }
   1682         }
   1683 
   1684         if(0 == ps_codec->i4_share_disp_buf)
   1685         {
   1686             if(ps_codec->i4_disp_strd < ps_codec->i4_disp_wd)
   1687             {
   1688                 ps_codec->i4_disp_strd = ps_codec->i4_disp_wd;
   1689             }
   1690         }
   1691         else
   1692         {
   1693             if(ps_codec->i4_disp_strd < ps_codec->i4_strd)
   1694             {
   1695                 ps_codec->i4_disp_strd = ps_codec->i4_strd;
   1696             }
   1697         }
   1698     }
   1699 
   1700     ps_codec->i4_sps_done = 1;
   1701     return ret;
   1702 }
   1703 
   1704 
   1705 void ihevcd_unmark_pps(codec_t *ps_codec, WORD32 sps_id)
   1706 {
   1707     WORD32 pps_id = 0;
   1708     pps_t *ps_pps = ps_codec->ps_pps_base;
   1709 
   1710     for(pps_id = 0; pps_id < MAX_PPS_CNT - 1; pps_id++, ps_pps++)
   1711     {
   1712         if((ps_pps->i1_pps_valid) &&
   1713                         (ps_pps->i1_sps_id == sps_id))
   1714             ps_pps->i1_pps_valid = 0;
   1715     }
   1716 }
   1717 
   1718 
   1719 void ihevcd_copy_sps(codec_t *ps_codec, WORD32 sps_id, WORD32 sps_id_ref)
   1720 {
   1721     sps_t *ps_sps, *ps_sps_ref;
   1722     WORD16 *pi2_scaling_mat_backup;
   1723     WORD32 scaling_mat_size;
   1724 
   1725     SCALING_MAT_SIZE(scaling_mat_size);
   1726     ps_sps_ref = ps_codec->ps_sps_base + sps_id_ref;
   1727     ps_sps = ps_codec->ps_sps_base + sps_id;
   1728 
   1729     if(ps_sps->i1_sps_valid)
   1730     {
   1731         if((ps_sps->i1_log2_ctb_size != ps_sps_ref->i1_log2_ctb_size) ||
   1732                         (ps_sps->i2_pic_wd_in_ctb != ps_sps_ref->i2_pic_wd_in_ctb) ||
   1733                         (ps_sps->i2_pic_ht_in_ctb != ps_sps_ref->i2_pic_ht_in_ctb))
   1734         {
   1735             ihevcd_unmark_pps(ps_codec, sps_id);
   1736         }
   1737     }
   1738 
   1739     pi2_scaling_mat_backup = ps_sps->pi2_scaling_mat;
   1740 
   1741     memcpy(ps_sps, ps_sps_ref, sizeof(sps_t));
   1742     ps_sps->pi2_scaling_mat = pi2_scaling_mat_backup;
   1743     memcpy(ps_sps->pi2_scaling_mat, ps_sps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
   1744     ps_sps->i1_sps_valid = 1;
   1745 
   1746     ps_codec->s_parse.ps_sps = ps_sps;
   1747 }
   1748 
   1749 
   1750 /**
   1751 *******************************************************************************
   1752 *
   1753 * @brief
   1754 *  Parses PPS (Picture Parameter Set)
   1755 *
   1756 * @par Description:
   1757 *  Parse Picture Parameter Set as per section  Section: 7.3.2.3
   1758 * The pps is written to a temporary buffer and copied later to the
   1759 * appropriate location
   1760 *
   1761 * @param[in] ps_codec
   1762 *  Pointer to codec context
   1763 *
   1764 * @returns Error code from IHEVCD_ERROR_T
   1765 *
   1766 * @remarks
   1767 *
   1768 *
   1769 *******************************************************************************
   1770 */
   1771 IHEVCD_ERROR_T ihevcd_parse_pps(codec_t *ps_codec)
   1772 {
   1773     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   1774     WORD32 value;
   1775     WORD32 pps_id;
   1776 
   1777     pps_t *ps_pps;
   1778     sps_t *ps_sps;
   1779     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
   1780 
   1781 
   1782     if(0 == ps_codec->i4_sps_done)
   1783         return IHEVCD_INVALID_HEADER;
   1784 
   1785     UEV_PARSE("pic_parameter_set_id", value, ps_bitstrm);
   1786 
   1787     pps_id = value;
   1788     if((pps_id >= MAX_PPS_CNT) || (pps_id < 0))
   1789     {
   1790         if(ps_codec->i4_pps_done)
   1791             return IHEVCD_UNSUPPORTED_PPS_ID;
   1792         else
   1793             pps_id = 0;
   1794     }
   1795 
   1796 
   1797     ps_pps = (ps_codec->s_parse.ps_pps_base + MAX_PPS_CNT - 1);
   1798 
   1799     ps_pps->i1_pps_id = pps_id;
   1800 
   1801     UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
   1802     ps_pps->i1_sps_id = value;
   1803     ps_pps->i1_sps_id = CLIP3(ps_pps->i1_sps_id, 0, MAX_SPS_CNT - 2);
   1804 
   1805     ps_sps = (ps_codec->s_parse.ps_sps_base + ps_pps->i1_sps_id);
   1806 
   1807     /* If the SPS that is being referred to has not been parsed,
   1808      * copy an existing SPS to the current location */
   1809     if(0 == ps_sps->i1_sps_valid)
   1810     {
   1811         return IHEVCD_INVALID_HEADER;
   1812 
   1813 /*
   1814         sps_t *ps_sps_ref = ps_codec->ps_sps_base;
   1815         while(0 == ps_sps_ref->i1_sps_valid)
   1816             ps_sps_ref++;
   1817         ihevcd_copy_sps(ps_codec, ps_pps->i1_sps_id, ps_sps_ref->i1_sps_id);
   1818 */
   1819     }
   1820 
   1821     BITS_PARSE("dependent_slices_enabled_flag", value, ps_bitstrm, 1);
   1822     ps_pps->i1_dependent_slice_enabled_flag = value;
   1823 
   1824     BITS_PARSE("output_flag_present_flag", value, ps_bitstrm, 1);
   1825     ps_pps->i1_output_flag_present_flag = value;
   1826 
   1827     BITS_PARSE("num_extra_slice_header_bits", value, ps_bitstrm, 3);
   1828     ps_pps->i1_num_extra_slice_header_bits = value;
   1829 
   1830 
   1831     BITS_PARSE("sign_data_hiding_flag", value, ps_bitstrm, 1);
   1832     ps_pps->i1_sign_data_hiding_flag = value;
   1833 
   1834     BITS_PARSE("cabac_init_present_flag", value, ps_bitstrm, 1);
   1835     ps_pps->i1_cabac_init_present_flag = value;
   1836 
   1837     UEV_PARSE("num_ref_idx_l0_default_active_minus1", value, ps_bitstrm);
   1838     ps_pps->i1_num_ref_idx_l0_default_active = value + 1;
   1839 
   1840     UEV_PARSE("num_ref_idx_l1_default_active_minus1", value, ps_bitstrm);
   1841     ps_pps->i1_num_ref_idx_l1_default_active = value + 1;
   1842 
   1843     SEV_PARSE("pic_init_qp_minus26", value, ps_bitstrm);
   1844     ps_pps->i1_pic_init_qp = value + 26;
   1845 
   1846     BITS_PARSE("constrained_intra_pred_flag", value, ps_bitstrm, 1);
   1847     ps_pps->i1_constrained_intra_pred_flag = value;
   1848 
   1849     BITS_PARSE("transform_skip_enabled_flag", value, ps_bitstrm, 1);
   1850     ps_pps->i1_transform_skip_enabled_flag = value;
   1851 
   1852     BITS_PARSE("cu_qp_delta_enabled_flag", value, ps_bitstrm, 1);
   1853     ps_pps->i1_cu_qp_delta_enabled_flag = value;
   1854 
   1855     if(ps_pps->i1_cu_qp_delta_enabled_flag)
   1856     {
   1857         UEV_PARSE("diff_cu_qp_delta_depth", value, ps_bitstrm);
   1858         ps_pps->i1_diff_cu_qp_delta_depth = value;
   1859     }
   1860     else
   1861     {
   1862         ps_pps->i1_diff_cu_qp_delta_depth = 0;
   1863     }
   1864     ps_pps->i1_log2_min_cu_qp_delta_size = ps_sps->i1_log2_ctb_size - ps_pps->i1_diff_cu_qp_delta_depth;
   1865     /* Print different */
   1866     SEV_PARSE("cb_qp_offset", value, ps_bitstrm);
   1867     ps_pps->i1_pic_cb_qp_offset = value;
   1868 
   1869     /* Print different */
   1870     SEV_PARSE("cr_qp_offset", value, ps_bitstrm);
   1871     ps_pps->i1_pic_cr_qp_offset = value;
   1872 
   1873     /* Print different */
   1874     BITS_PARSE("slicelevel_chroma_qp_flag", value, ps_bitstrm, 1);
   1875     ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag = value;
   1876 
   1877     BITS_PARSE("weighted_pred_flag", value, ps_bitstrm, 1);
   1878     ps_pps->i1_weighted_pred_flag = value;
   1879 
   1880     BITS_PARSE("weighted_bipred_flag", value, ps_bitstrm, 1);
   1881     ps_pps->i1_weighted_bipred_flag = value;
   1882 
   1883     BITS_PARSE("transquant_bypass_enable_flag", value, ps_bitstrm, 1);
   1884     ps_pps->i1_transquant_bypass_enable_flag = value;
   1885 
   1886     BITS_PARSE("tiles_enabled_flag", value, ps_bitstrm, 1);
   1887     ps_pps->i1_tiles_enabled_flag = value;
   1888 
   1889     BITS_PARSE("entropy_coding_sync_enabled_flag", value, ps_bitstrm, 1);
   1890     ps_pps->i1_entropy_coding_sync_enabled_flag = value;
   1891 
   1892     ps_pps->i1_loop_filter_across_tiles_enabled_flag = 0;
   1893     if(ps_pps->i1_tiles_enabled_flag)
   1894     {
   1895         WORD32 wd = ALIGN64(ps_codec->i4_wd);
   1896         WORD32 ht = ALIGN64(ps_codec->i4_ht);
   1897 
   1898         WORD32 max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
   1899         WORD32 max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
   1900 
   1901         UEV_PARSE("num_tile_columns_minus1", value, ps_bitstrm);
   1902         ps_pps->i1_num_tile_columns = value + 1;
   1903 
   1904         UEV_PARSE("num_tile_rows_minus1", value, ps_bitstrm);
   1905         ps_pps->i1_num_tile_rows = value + 1;
   1906 
   1907         if((ps_pps->i1_num_tile_columns < 1) ||
   1908                         (ps_pps->i1_num_tile_columns > max_tile_cols) ||
   1909                         (ps_pps->i1_num_tile_rows < 1) ||
   1910                         (ps_pps->i1_num_tile_rows > max_tile_rows))
   1911             return IHEVCD_INVALID_HEADER;
   1912 
   1913         BITS_PARSE("uniform_spacing_flag", value, ps_bitstrm, 1);
   1914         ps_pps->i1_uniform_spacing_flag = value;
   1915 
   1916 
   1917         {
   1918 
   1919             WORD32 start;
   1920             WORD32 i, j;
   1921 
   1922 
   1923             start = 0;
   1924             for(i = 0; i < ps_pps->i1_num_tile_columns; i++)
   1925             {
   1926                 tile_t *ps_tile;
   1927                 if(!ps_pps->i1_uniform_spacing_flag)
   1928                 {
   1929                     if(i < (ps_pps->i1_num_tile_columns - 1))
   1930                     {
   1931                         UEV_PARSE("column_width_minus1[ i ]", value, ps_bitstrm);
   1932                         value += 1;
   1933                     }
   1934                     else
   1935                     {
   1936                         value = ps_sps->i2_pic_wd_in_ctb - start;
   1937                     }
   1938                 }
   1939                 else
   1940                 {
   1941                     value = ((i + 1) * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns -
   1942                                     (i * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns;
   1943                 }
   1944 
   1945                 for(j = 0; j < ps_pps->i1_num_tile_rows; j++)
   1946                 {
   1947                     ps_tile = ps_pps->ps_tile + j * ps_pps->i1_num_tile_columns + i;
   1948                     ps_tile->u1_pos_x = start;
   1949                     ps_tile->u2_wd = value;
   1950                 }
   1951                 start += value;
   1952 
   1953                 if((start > ps_sps->i2_pic_wd_in_ctb) ||
   1954                                 (value <= 0))
   1955                     return IHEVCD_INVALID_HEADER;
   1956             }
   1957 
   1958             start = 0;
   1959             for(i = 0; i < (ps_pps->i1_num_tile_rows); i++)
   1960             {
   1961                 tile_t *ps_tile;
   1962                 if(!ps_pps->i1_uniform_spacing_flag)
   1963                 {
   1964                     if(i < (ps_pps->i1_num_tile_rows - 1))
   1965                     {
   1966 
   1967                         UEV_PARSE("row_height_minus1[ i ]", value, ps_bitstrm);
   1968                         value += 1;
   1969                     }
   1970                     else
   1971                     {
   1972                         value = ps_sps->i2_pic_ht_in_ctb - start;
   1973                     }
   1974                 }
   1975                 else
   1976                 {
   1977                     value = ((i + 1) * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows -
   1978                                     (i * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows;
   1979                 }
   1980 
   1981                 for(j = 0; j < ps_pps->i1_num_tile_columns; j++)
   1982                 {
   1983                     ps_tile = ps_pps->ps_tile + i * ps_pps->i1_num_tile_columns + j;
   1984                     ps_tile->u1_pos_y = start;
   1985                     ps_tile->u2_ht = value;
   1986                 }
   1987                 start += value;
   1988 
   1989                 if((start > ps_sps->i2_pic_ht_in_ctb) ||
   1990                                 (value <= 0))
   1991                     return IHEVCD_INVALID_HEADER;
   1992             }
   1993         }
   1994 
   1995 
   1996         BITS_PARSE("loop_filter_across_tiles_enabled_flag", value, ps_bitstrm, 1);
   1997         ps_pps->i1_loop_filter_across_tiles_enabled_flag = value;
   1998 
   1999     }
   2000     else
   2001     {
   2002         /* If tiles are not present, set first tile in each PPS to have tile
   2003         width and height equal to picture width and height */
   2004         ps_pps->i1_num_tile_columns = 1;
   2005         ps_pps->i1_num_tile_rows = 1;
   2006         ps_pps->i1_uniform_spacing_flag = 1;
   2007 
   2008         ps_pps->ps_tile->u1_pos_x = 0;
   2009         ps_pps->ps_tile->u1_pos_y = 0;
   2010         ps_pps->ps_tile->u2_wd = ps_sps->i2_pic_wd_in_ctb;
   2011         ps_pps->ps_tile->u2_ht = ps_sps->i2_pic_ht_in_ctb;
   2012     }
   2013 
   2014     BITS_PARSE("loop_filter_across_slices_enabled_flag", value, ps_bitstrm, 1);
   2015     ps_pps->i1_loop_filter_across_slices_enabled_flag = value;
   2016 
   2017     BITS_PARSE("deblocking_filter_control_present_flag", value, ps_bitstrm, 1);
   2018     ps_pps->i1_deblocking_filter_control_present_flag = value;
   2019 
   2020     /* Default values */
   2021     ps_pps->i1_pic_disable_deblocking_filter_flag = 0;
   2022     ps_pps->i1_deblocking_filter_override_enabled_flag = 0;
   2023     ps_pps->i1_beta_offset_div2 = 0;
   2024     ps_pps->i1_tc_offset_div2 = 0;
   2025 
   2026     if(ps_pps->i1_deblocking_filter_control_present_flag)
   2027     {
   2028 
   2029         BITS_PARSE("deblocking_filter_override_enabled_flag", value, ps_bitstrm, 1);
   2030         ps_pps->i1_deblocking_filter_override_enabled_flag = value;
   2031 
   2032         BITS_PARSE("pic_disable_deblocking_filter_flag", value, ps_bitstrm, 1);
   2033         ps_pps->i1_pic_disable_deblocking_filter_flag = value;
   2034 
   2035         if(!ps_pps->i1_pic_disable_deblocking_filter_flag)
   2036         {
   2037 
   2038             SEV_PARSE("pps_beta_offset_div2", value, ps_bitstrm);
   2039             ps_pps->i1_beta_offset_div2 = value;
   2040 
   2041             SEV_PARSE("pps_tc_offset_div2", value, ps_bitstrm);
   2042             ps_pps->i1_tc_offset_div2 = value;
   2043 
   2044         }
   2045     }
   2046 
   2047     BITS_PARSE("pps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
   2048     ps_pps->i1_pps_scaling_list_data_present_flag = value;
   2049 
   2050     if(ps_pps->i1_pps_scaling_list_data_present_flag)
   2051     {
   2052         COPY_DEFAULT_SCALING_LIST(ps_pps->pi2_scaling_mat);
   2053         ihevcd_scaling_list_data(ps_codec, ps_pps->pi2_scaling_mat);
   2054     }
   2055 
   2056     BITS_PARSE("lists_modification_present_flag", value, ps_bitstrm, 1);
   2057     ps_pps->i1_lists_modification_present_flag = value;
   2058     UEV_PARSE("log2_parallel_merge_level_minus2", value, ps_bitstrm);
   2059     ps_pps->i1_log2_parallel_merge_level = value + 2;
   2060 
   2061     BITS_PARSE("slice_header_extension_present_flag", value, ps_bitstrm, 1);
   2062     ps_pps->i1_slice_header_extension_present_flag = value;
   2063     /* Not present in HM */
   2064     BITS_PARSE("pps_extension_flag", value, ps_bitstrm, 1);
   2065 
   2066     if((UWORD8 *)ps_bitstrm->pu4_buf > ps_bitstrm->pu1_buf_max)
   2067         return IHEVCD_INVALID_PARAMETER;
   2068 
   2069     ps_codec->i4_pps_done = 1;
   2070     return ret;
   2071 }
   2072 
   2073 
   2074 void ihevcd_copy_pps(codec_t *ps_codec, WORD32 pps_id, WORD32 pps_id_ref)
   2075 {
   2076     pps_t *ps_pps, *ps_pps_ref;
   2077     WORD16 *pi2_scaling_mat_backup;
   2078     WORD32 scaling_mat_size;
   2079     tile_t *ps_tile_backup;
   2080     WORD32 max_tile_cols, max_tile_rows;
   2081     WORD32 wd, ht;
   2082     wd = ALIGN64(ps_codec->i4_wd);
   2083     ht = ALIGN64(ps_codec->i4_ht);
   2084 
   2085     SCALING_MAT_SIZE(scaling_mat_size);
   2086     max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
   2087     max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
   2088 
   2089     ps_pps_ref = ps_codec->ps_pps_base + pps_id_ref;
   2090     ps_pps = ps_codec->ps_pps_base + pps_id;
   2091 
   2092     pi2_scaling_mat_backup = ps_pps->pi2_scaling_mat;
   2093     ps_tile_backup = ps_pps->ps_tile;
   2094 
   2095     memcpy(ps_pps, ps_pps_ref, sizeof(pps_t));
   2096     ps_pps->pi2_scaling_mat = pi2_scaling_mat_backup;
   2097     ps_pps->ps_tile = ps_tile_backup;
   2098     memcpy(ps_pps->pi2_scaling_mat, ps_pps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
   2099     memcpy(ps_pps->ps_tile, ps_pps_ref->ps_tile, max_tile_cols * max_tile_rows * sizeof(tile_t));
   2100 
   2101     ps_pps->i1_pps_valid = 1;
   2102 
   2103     ps_codec->s_parse.ps_pps = ps_pps;
   2104 }
   2105 
   2106 
   2107 IHEVCD_ERROR_T ihevcd_parse_buffering_period_sei(codec_t *ps_codec,
   2108                                                  sps_t *ps_sps)
   2109 {
   2110     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
   2111     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
   2112     UWORD32 value;
   2113     vui_t *ps_vui;
   2114     buf_period_sei_params_t *ps_buf_period_sei_params;
   2115     UWORD32 i;
   2116     hrd_params_t *ps_vui_hdr;
   2117     UWORD32 u4_cpb_cnt;
   2118 
   2119     ps_vui = &ps_sps->s_vui_parameters;
   2120     ps_vui_hdr = &ps_vui->s_vui_hrd_parameters;
   2121 
   2122     ps_buf_period_sei_params = &ps_parse->s_sei_params.s_buf_period_sei_params;
   2123 
   2124     ps_parse->s_sei_params.i1_buf_period_params_present_flag = 1;
   2125 
   2126     UEV_PARSE("bp_seq_parameter_set_id", value, ps_bitstrm);
   2127     ps_buf_period_sei_params->u1_bp_seq_parameter_set_id = value;
   2128 
   2129     if(!ps_vui_hdr->u1_sub_pic_cpb_params_present_flag)
   2130     {
   2131         BITS_PARSE("irap_cpb_params_present_flag", value, ps_bitstrm, 1);
   2132         ps_buf_period_sei_params->u1_rap_cpb_params_present_flag = value;
   2133     }
   2134 
   2135     if(ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
   2136     {
   2137         BITS_PARSE("cpb_delay_offset",
   2138                    value,
   2139                    ps_bitstrm,
   2140                    (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1
   2141                                    + 1));
   2142         ps_buf_period_sei_params->u4_cpb_delay_offset = value;
   2143 
   2144         BITS_PARSE("dpb_delay_offset",
   2145                    value,
   2146                    ps_bitstrm,
   2147                    (ps_vui_hdr->u1_dpb_output_delay_length_minus1
   2148                                    + 1));
   2149         ps_buf_period_sei_params->u4_dpb_delay_offset = value;
   2150     }
   2151     else
   2152     {
   2153         ps_buf_period_sei_params->u4_cpb_delay_offset = 0;
   2154         ps_buf_period_sei_params->u4_dpb_delay_offset = 0;
   2155     }
   2156 
   2157     BITS_PARSE("concatenation_flag", value, ps_bitstrm, 1);
   2158     ps_buf_period_sei_params->u1_concatenation_flag = value;
   2159 
   2160     BITS_PARSE("au_cpb_removal_delay_delta_minus1",
   2161                value,
   2162                ps_bitstrm,
   2163                (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1
   2164                                + 1));
   2165     ps_buf_period_sei_params->u4_au_cpb_removal_delay_delta_minus1 = value;
   2166 
   2167     if(ps_vui_hdr->u1_nal_hrd_parameters_present_flag)
   2168     {
   2169         u4_cpb_cnt = ps_vui_hdr->au1_cpb_cnt_minus1[0];
   2170 
   2171         for(i = 0; i <= u4_cpb_cnt; i++)
   2172         {
   2173             BITS_PARSE("nal_initial_cpb_removal_delay[i]",
   2174                        value,
   2175                        ps_bitstrm,
   2176                        (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
   2177                                        + 1));
   2178             ps_buf_period_sei_params->au4_nal_initial_cpb_removal_delay[i] =
   2179                             value;
   2180 
   2181             BITS_PARSE("nal_initial_cpb_removal_delay_offset",
   2182                        value,
   2183                        ps_bitstrm,
   2184                        (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
   2185                                        + 1));
   2186             ps_buf_period_sei_params->au4_nal_initial_cpb_removal_delay_offset[i] =
   2187                             value;
   2188 
   2189             if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
   2190                             || ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
   2191             {
   2192                 BITS_PARSE("nal_initial_alt_cpb_removal_delay[i]",
   2193                            value,
   2194                            ps_bitstrm,
   2195                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
   2196                                            + 1));
   2197                 ps_buf_period_sei_params->au4_nal_initial_alt_cpb_removal_delay[i] =
   2198                                 value;
   2199 
   2200                 BITS_PARSE("nal_initial_alt_cpb_removal_delay_offset",
   2201                            value,
   2202                            ps_bitstrm,
   2203                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
   2204                                            + 1));
   2205                 ps_buf_period_sei_params->au4_nal_initial_alt_cpb_removal_delay_offset[i] =
   2206                                 value;
   2207             }
   2208         }
   2209     }
   2210 
   2211     if(ps_vui_hdr->u1_vcl_hrd_parameters_present_flag)
   2212     {
   2213         u4_cpb_cnt = ps_vui_hdr->au1_cpb_cnt_minus1[0];
   2214 
   2215         for(i = 0; i <= u4_cpb_cnt; i++)
   2216         {
   2217             BITS_PARSE("vcl_initial_cpb_removal_delay[i]",
   2218                        value,
   2219                        ps_bitstrm,
   2220                        (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
   2221                                        + 1));
   2222             ps_buf_period_sei_params->au4_vcl_initial_cpb_removal_delay[i] =
   2223                             value;
   2224 
   2225             BITS_PARSE("vcl_initial_cpb_removal_delay_offset",
   2226                        value,
   2227                        ps_bitstrm,
   2228                        (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
   2229                                        + 1));
   2230             ps_buf_period_sei_params->au4_vcl_initial_cpb_removal_delay_offset[i] =
   2231                             value;
   2232 
   2233             if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
   2234                             || ps_buf_period_sei_params->u1_rap_cpb_params_present_flag)
   2235             {
   2236                 BITS_PARSE("vcl_initial_alt_cpb_removal_delay[i]",
   2237                            value,
   2238                            ps_bitstrm,
   2239                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
   2240                                            + 1));
   2241                 ps_buf_period_sei_params->au4_vcl_initial_alt_cpb_removal_delay[i] =
   2242                                 value;
   2243 
   2244                 BITS_PARSE("vcl_initial_alt_cpb_removal_delay_offset",
   2245                            value,
   2246                            ps_bitstrm,
   2247                            (ps_vui_hdr->u1_initial_cpb_removal_delay_length_minus1
   2248                                            + 1));
   2249                 ps_buf_period_sei_params->au4_vcl_initial_alt_cpb_removal_delay_offset[i] =
   2250                                 value;
   2251             }
   2252         }
   2253     }
   2254 
   2255     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   2256 }
   2257 
   2258 IHEVCD_ERROR_T ihevcd_parse_pic_timing_sei(codec_t *ps_codec, sps_t *ps_sps)
   2259 {
   2260     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
   2261     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
   2262     UWORD32 value;
   2263     vui_t *ps_vui;
   2264     UWORD32 i;
   2265     hrd_params_t *ps_vui_hdr;
   2266     UWORD32 u4_cpb_dpb_delays_present_flag = 0;
   2267     pic_timing_sei_params_t *ps_pic_timing;
   2268 
   2269     ps_pic_timing = &ps_parse->s_sei_params.s_pic_timing_sei_params;
   2270     ps_vui = &ps_sps->s_vui_parameters;
   2271     ps_vui_hdr = &ps_vui->s_vui_hrd_parameters;
   2272     ps_parse->s_sei_params.i1_pic_timing_params_present_flag = 1;
   2273     if(ps_vui->u1_frame_field_info_present_flag)
   2274     {
   2275         BITS_PARSE("pic_struct", value, ps_bitstrm, 4);
   2276         ps_pic_timing->u4_pic_struct = value;
   2277 
   2278         BITS_PARSE("source_scan_type", value, ps_bitstrm, 2);
   2279         ps_pic_timing->u4_source_scan_type = value;
   2280 
   2281         BITS_PARSE("duplicate_flag", value, ps_bitstrm, 1);
   2282         ps_pic_timing->u1_duplicate_flag = value;
   2283     }
   2284 
   2285     if(ps_vui_hdr->u1_nal_hrd_parameters_present_flag
   2286                     || ps_vui_hdr->u1_vcl_hrd_parameters_present_flag)
   2287     {
   2288         u4_cpb_dpb_delays_present_flag = 1;
   2289     }
   2290     else
   2291     {
   2292         u4_cpb_dpb_delays_present_flag = 0;
   2293     }
   2294 
   2295     if(u4_cpb_dpb_delays_present_flag)
   2296     {
   2297         BITS_PARSE("au_cpb_removal_delay_minus1", value, ps_bitstrm,
   2298                    (ps_vui_hdr->u1_au_cpb_removal_delay_length_minus1 + 1));
   2299         ps_pic_timing->u4_au_cpb_removal_delay_minus1 = value;
   2300 
   2301         BITS_PARSE("pic_dpb_output_delay", value, ps_bitstrm,
   2302                    (ps_vui_hdr->u1_dpb_output_delay_length_minus1 + 1));
   2303         ps_pic_timing->u4_pic_dpb_output_delay = value;
   2304 
   2305         if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag)
   2306         {
   2307             BITS_PARSE("pic_dpb_output_du_delay", value, ps_bitstrm,
   2308                        (ps_vui_hdr->u1_dpb_output_delay_du_length_minus1 + 1));
   2309             ps_pic_timing->u4_pic_dpb_output_du_delay = value;
   2310         }
   2311 
   2312         if(ps_vui_hdr->u1_sub_pic_cpb_params_present_flag
   2313                         && ps_vui_hdr->u1_sub_pic_cpb_params_in_pic_timing_sei_flag)
   2314         {
   2315             UEV_PARSE("num_decoding_units_minus1", value, ps_bitstrm);
   2316             ps_pic_timing->u4_num_decoding_units_minus1 = value;
   2317 
   2318             BITS_PARSE("du_common_cpb_removal_delay_flag", value, ps_bitstrm, 1);
   2319             ps_pic_timing->u1_du_common_cpb_removal_delay_flag = value;
   2320 
   2321             if(ps_pic_timing->u1_du_common_cpb_removal_delay_flag)
   2322             {
   2323                 BITS_PARSE("du_common_cpb_removal_delay_increment_minus1",
   2324                            value,
   2325                            ps_bitstrm,
   2326                            (ps_vui_hdr->u1_du_cpb_removal_delay_increment_length_minus1
   2327                                            + 1));
   2328                 ps_pic_timing->u4_du_common_cpb_removal_delay_increment_minus1 =
   2329                                 value;
   2330             }
   2331 
   2332             for(i = 0; i <= ps_pic_timing->u4_num_decoding_units_minus1; i++)
   2333             {
   2334                 UEV_PARSE("num_nalus_in_du_minus1", value, ps_bitstrm);
   2335                 ps_pic_timing->au4_num_nalus_in_du_minus1[i] = value;
   2336 
   2337                 if((!ps_pic_timing->u1_du_common_cpb_removal_delay_flag)
   2338                                 && (i < ps_pic_timing->u4_num_decoding_units_minus1))
   2339                 {
   2340                     BITS_PARSE("du_common_cpb_removal_delay_increment_minus1",
   2341                                value,
   2342                                ps_bitstrm,
   2343                                (ps_vui_hdr->u1_du_cpb_removal_delay_increment_length_minus1
   2344                                                + 1));
   2345                     ps_pic_timing->au4_du_cpb_removal_delay_increment_minus1[i] =
   2346                                     value;
   2347                 }
   2348             }
   2349         }
   2350     }
   2351 
   2352     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   2353 }
   2354 
   2355 IHEVCD_ERROR_T ihevcd_parse_time_code_sei(codec_t *ps_codec)
   2356 {
   2357     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
   2358     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
   2359     UWORD32 value;
   2360     time_code_t *ps_time_code;
   2361     WORD32 i;
   2362 
   2363     ps_parse->s_sei_params.i1_time_code_present_flag = 1;
   2364     ps_time_code = &ps_parse->s_sei_params.s_time_code;
   2365 
   2366     BITS_PARSE("num_clock_ts", value, ps_bitstrm, 2);
   2367     ps_time_code->u1_num_clock_ts = value;
   2368 
   2369     for(i = 0; i < ps_time_code->u1_num_clock_ts; i++)
   2370     {
   2371         BITS_PARSE("clock_timestamp_flag[i]", value, ps_bitstrm, 1);
   2372         ps_time_code->au1_clock_timestamp_flag[i] = value;
   2373 
   2374         if(ps_time_code->au1_clock_timestamp_flag[i])
   2375         {
   2376             BITS_PARSE("units_field_based_flag[i]", value, ps_bitstrm, 1);
   2377             ps_time_code->au1_units_field_based_flag[i] = value;
   2378 
   2379             BITS_PARSE("counting_type[i]", value, ps_bitstrm, 5);
   2380             ps_time_code->au1_counting_type[i] = value;
   2381 
   2382             BITS_PARSE("full_timestamp_flag[i]", value, ps_bitstrm, 1);
   2383             ps_time_code->au1_full_timestamp_flag[i] = value;
   2384 
   2385             BITS_PARSE("discontinuity_flag[i]", value, ps_bitstrm, 1);
   2386             ps_time_code->au1_discontinuity_flag[i] = value;
   2387 
   2388             BITS_PARSE("cnt_dropped_flag[i]", value, ps_bitstrm, 1);
   2389             ps_time_code->au1_cnt_dropped_flag[i] = value;
   2390 
   2391             BITS_PARSE("n_frames[i]", value, ps_bitstrm, 9);
   2392             ps_time_code->au2_n_frames[i] = value;
   2393 
   2394             if(ps_time_code->au1_full_timestamp_flag[i])
   2395             {
   2396                 BITS_PARSE("seconds_value[i]", value, ps_bitstrm, 6);
   2397                 ps_time_code->au1_seconds_value[i] = value;
   2398 
   2399                 BITS_PARSE("minutes_value[i]", value, ps_bitstrm, 6);
   2400                 ps_time_code->au1_minutes_value[i] = value;
   2401 
   2402                 BITS_PARSE("hours_value[i]", value, ps_bitstrm, 5);
   2403                 ps_time_code->au1_hours_value[i] = value;
   2404             }
   2405             else
   2406             {
   2407                 BITS_PARSE("seconds_flag[i]", value, ps_bitstrm, 1);
   2408                 ps_time_code->au1_seconds_flag[i] = value;
   2409 
   2410                 if(ps_time_code->au1_seconds_flag[i])
   2411                 {
   2412                     BITS_PARSE("seconds_value[i]", value, ps_bitstrm, 6);
   2413                     ps_time_code->au1_seconds_value[i] = value;
   2414 
   2415                     BITS_PARSE("minutes_flag[i]", value, ps_bitstrm, 1);
   2416                     ps_time_code->au1_minutes_flag[i] = value;
   2417 
   2418                     if(ps_time_code->au1_minutes_flag[i])
   2419                     {
   2420                         BITS_PARSE("minutes_value[i]", value, ps_bitstrm, 6);
   2421                         ps_time_code->au1_minutes_value[i] = value;
   2422 
   2423                         BITS_PARSE("hours_flag[i]", value, ps_bitstrm, 1);
   2424                         ps_time_code->au1_hours_flag[i] = value;
   2425 
   2426                         if(ps_time_code->au1_hours_flag[i])
   2427                         {
   2428                             BITS_PARSE("hours_value[i]", value, ps_bitstrm, 5);
   2429                             ps_time_code->au1_hours_value[i] = value;
   2430                         }
   2431                     }
   2432                 }
   2433             }
   2434 
   2435             BITS_PARSE("time_offset_length[i]", value, ps_bitstrm, 5);
   2436             ps_time_code->au1_time_offset_length[i] = value;
   2437 
   2438             if(ps_time_code->au1_time_offset_length[i] > 0)
   2439             {
   2440                 BITS_PARSE("time_offset_value[i]", value, ps_bitstrm,
   2441                            ps_time_code->au1_time_offset_length[i]);
   2442                 ps_time_code->au1_time_offset_value[i] = value;
   2443             }
   2444             else
   2445             {
   2446                 ps_time_code->au1_time_offset_value[i] = 0;
   2447             }
   2448         }
   2449     }
   2450 
   2451     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   2452 }
   2453 
   2454 IHEVCD_ERROR_T ihevcd_parse_mastering_disp_params_sei(codec_t *ps_codec)
   2455 {
   2456     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
   2457     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
   2458     UWORD32 value;
   2459     mastering_dis_col_vol_sei_params_t *ps_mastering_dis_col_vol;
   2460     WORD32 i;
   2461 
   2462     ps_parse->s_sei_params.i4_sei_mastering_disp_colour_vol_params_present_flags = 1;
   2463 
   2464     ps_mastering_dis_col_vol = &ps_parse->s_sei_params.s_mastering_dis_col_vol_sei_params;
   2465 
   2466     for(i = 0; i < 3; i++)
   2467     {
   2468         BITS_PARSE("display_primaries_x[c]", value, ps_bitstrm, 16);
   2469         ps_mastering_dis_col_vol->au2_display_primaries_x[i] = value;
   2470 
   2471         BITS_PARSE("display_primaries_y[c]", value, ps_bitstrm, 16);
   2472         ps_mastering_dis_col_vol->au2_display_primaries_y[i] = value;
   2473     }
   2474 
   2475     BITS_PARSE("white_point_x", value, ps_bitstrm, 16);
   2476     ps_mastering_dis_col_vol->u2_white_point_x = value;
   2477 
   2478     BITS_PARSE("white_point_y", value, ps_bitstrm, 16);
   2479     ps_mastering_dis_col_vol->u2_white_point_y = value;
   2480 
   2481     BITS_PARSE("max_display_mastering_luminance", value, ps_bitstrm, 32);
   2482     ps_mastering_dis_col_vol->u4_max_display_mastering_luminance = value;
   2483 
   2484     BITS_PARSE("min_display_mastering_luminance", value, ps_bitstrm, 32);
   2485     ps_mastering_dis_col_vol->u4_min_display_mastering_luminance = value;
   2486 
   2487     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   2488 }
   2489 
   2490 IHEVCD_ERROR_T ihevcd_parse_user_data_registered_itu_t_t35(codec_t *ps_codec,
   2491                                                            UWORD32 u4_payload_size)
   2492 {
   2493     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
   2494     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
   2495     UWORD32 value;
   2496     user_data_registered_itu_t_t35_t *ps_user_data_registered_itu_t_t35;
   2497     UWORD32 i;
   2498     UWORD32 j = 0;
   2499 
   2500     ps_parse->s_sei_params.i1_user_data_registered_present_flag = 1;
   2501     ps_user_data_registered_itu_t_t35 =
   2502                     &ps_parse->s_sei_params.as_user_data_registered_itu_t_t35[ps_parse->s_sei_params.i4_sei_user_data_cnt];
   2503     ps_parse->s_sei_params.i4_sei_user_data_cnt++;
   2504 
   2505     ps_user_data_registered_itu_t_t35->i4_payload_size = u4_payload_size;
   2506 
   2507     if(u4_payload_size > MAX_USERDATA_PAYLOAD)
   2508     {
   2509         u4_payload_size = MAX_USERDATA_PAYLOAD;
   2510     }
   2511 
   2512     ps_user_data_registered_itu_t_t35->i4_valid_payload_size = u4_payload_size;
   2513 
   2514     BITS_PARSE("itu_t_t35_country_code", value, ps_bitstrm, 8);
   2515     ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code = value;
   2516 
   2517     if(0xFF != ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code)
   2518     {
   2519         i = 1;
   2520     }
   2521     else
   2522     {
   2523         BITS_PARSE("itu_t_t35_country_code_extension_byte", value, ps_bitstrm,
   2524                    8);
   2525         ps_user_data_registered_itu_t_t35->u1_itu_t_t35_country_code_extension_byte =
   2526                         value;
   2527 
   2528         i = 2;
   2529     }
   2530 
   2531     do
   2532     {
   2533         BITS_PARSE("itu_t_t35_payload_byte", value, ps_bitstrm, 8);
   2534         ps_user_data_registered_itu_t_t35->u1_itu_t_t35_payload_byte[j++] =
   2535                         value;
   2536 
   2537         i++;
   2538     }while(i < u4_payload_size);
   2539 
   2540     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   2541 }
   2542 
   2543 void ihevcd_parse_sei_payload(codec_t *ps_codec,
   2544                               UWORD32 u4_payload_type,
   2545                               UWORD32 u4_payload_size,
   2546                               WORD8 i1_nal_type)
   2547 {
   2548     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
   2549     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
   2550     WORD32 payload_bits_remaining = 0;
   2551     sps_t *ps_sps;
   2552 
   2553     UWORD32 i;
   2554 
   2555     for(i = 0; i < MAX_SPS_CNT; i++)
   2556     {
   2557         ps_sps = ps_codec->ps_sps_base + i;
   2558         if(ps_sps->i1_sps_valid)
   2559         {
   2560             break;
   2561         }
   2562     }
   2563     if(NULL == ps_sps)
   2564     {
   2565         return;
   2566     }
   2567 
   2568     if(NAL_PREFIX_SEI == i1_nal_type)
   2569     {
   2570         switch(u4_payload_type)
   2571         {
   2572             case SEI_BUFFERING_PERIOD:
   2573                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
   2574                 ihevcd_parse_buffering_period_sei(ps_codec, ps_sps);
   2575                 break;
   2576 
   2577             case SEI_PICTURE_TIMING:
   2578                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
   2579                 ihevcd_parse_pic_timing_sei(ps_codec, ps_sps);
   2580                 break;
   2581 
   2582             case SEI_TIME_CODE:
   2583                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
   2584                 ihevcd_parse_time_code_sei(ps_codec);
   2585                 break;
   2586 
   2587             case SEI_MASTERING_DISPLAY_COLOUR_VOLUME:
   2588                 ps_parse->s_sei_params.i4_sei_mastering_disp_colour_vol_params_present_flags = 1;
   2589                 ihevcd_parse_mastering_disp_params_sei(ps_codec);
   2590                 break;
   2591 
   2592             case SEI_USER_DATA_REGISTERED_ITU_T_T35:
   2593                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
   2594                 ihevcd_parse_user_data_registered_itu_t_t35(ps_codec,
   2595                                                             u4_payload_size);
   2596                 break;
   2597 
   2598             default:
   2599                 for(i = 0; i < u4_payload_size; i++)
   2600                 {
   2601                     ihevcd_bits_flush(ps_bitstrm, 8);
   2602                 }
   2603                 break;
   2604         }
   2605     }
   2606     else /* NAL_SUFFIX_SEI */
   2607     {
   2608         switch(u4_payload_type)
   2609         {
   2610             case SEI_USER_DATA_REGISTERED_ITU_T_T35:
   2611                 ps_parse->s_sei_params.i1_sei_parameters_present_flag = 1;
   2612                 ihevcd_parse_user_data_registered_itu_t_t35(ps_codec,
   2613                                                             u4_payload_size);
   2614                 break;
   2615 
   2616             default:
   2617                 for(i = 0; i < u4_payload_size; i++)
   2618                 {
   2619                     ihevcd_bits_flush(ps_bitstrm, 8);
   2620                 }
   2621                 break;
   2622         }
   2623     }
   2624 
   2625     /**
   2626      * By definition the underlying bitstream terminates in a byte-aligned manner.
   2627      * 1. Extract all bar the last MIN(bitsremaining,nine) bits as reserved_payload_extension_data
   2628      * 2. Examine the final 8 bits to determine the payload_bit_equal_to_one marker
   2629      * 3. Extract the remainingreserved_payload_extension_data bits.
   2630      *
   2631      * If there are fewer than 9 bits available, extract them.
   2632      */
   2633 
   2634     payload_bits_remaining = ihevcd_bits_num_bits_remaining(ps_bitstrm);
   2635     if(payload_bits_remaining) /* more_data_in_payload() */
   2636     {
   2637         WORD32 final_bits;
   2638         WORD32 final_payload_bits = 0;
   2639         WORD32 mask = 0xFF;
   2640         UWORD32 u4_dummy;
   2641         UWORD32 u4_reserved_payload_extension_data;
   2642         UNUSED(u4_dummy);
   2643         UNUSED(u4_reserved_payload_extension_data);
   2644 
   2645         while(payload_bits_remaining > 9)
   2646         {
   2647             BITS_PARSE("reserved_payload_extension_data",
   2648                        u4_reserved_payload_extension_data, ps_bitstrm, 1);
   2649             payload_bits_remaining--;
   2650         }
   2651 
   2652         final_bits = ihevcd_bits_nxt(ps_bitstrm, payload_bits_remaining);
   2653 
   2654         while(final_bits & (mask >> final_payload_bits))
   2655         {
   2656             final_payload_bits++;
   2657             continue;
   2658         }
   2659 
   2660         while(payload_bits_remaining > (9 - final_payload_bits))
   2661         {
   2662             BITS_PARSE("reserved_payload_extension_data",
   2663                        u4_reserved_payload_extension_data, ps_bitstrm, 1);
   2664             payload_bits_remaining--;
   2665         }
   2666 
   2667         BITS_PARSE("payload_bit_equal_to_one", u4_dummy, ps_bitstrm, 1);
   2668         payload_bits_remaining--;
   2669         while(payload_bits_remaining)
   2670         {
   2671             BITS_PARSE("payload_bit_equal_to_zero", u4_dummy, ps_bitstrm, 1);
   2672             payload_bits_remaining--;
   2673         }
   2674     }
   2675 
   2676     return;
   2677 }
   2678 
   2679 IHEVCD_ERROR_T ihevcd_read_rbsp_trailing_bits(codec_t *ps_codec,
   2680                                               UWORD32 u4_bits_left)
   2681 {
   2682     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
   2683     UWORD32 value;
   2684     WORD32 cnt = 0;
   2685     BITS_PARSE("rbsp_stop_one_bit", value, &ps_parse->s_bitstrm, 1);
   2686     u4_bits_left--;
   2687     if(value != 1)
   2688     {
   2689         return (IHEVCD_ERROR_T)IHEVCD_FAIL;
   2690     }
   2691     while(u4_bits_left)
   2692     {
   2693         BITS_PARSE("rbsp_alignment_zero_bit", value, &ps_parse->s_bitstrm, 1);
   2694         u4_bits_left--;
   2695         cnt++;
   2696     }
   2697     ASSERT(cnt < 8);
   2698 
   2699     return (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   2700 }
   2701 /**
   2702 *******************************************************************************
   2703 *
   2704 * @brief
   2705 *  Parses SEI (Supplemental Enhancement Information)
   2706 *
   2707 * @par Description:
   2708 *  Parses SEI (Supplemental Enhancement Information) as per Section: 7.3.7
   2709 *
   2710 * @param[in] ps_codec
   2711 *  Pointer to codec context
   2712 *
   2713 * @returns Error code from IHEVCD_ERROR_T
   2714 *
   2715 * @remarks
   2716 *
   2717 *
   2718 *******************************************************************************
   2719 */
   2720 IHEVCD_ERROR_T ihevcd_parse_sei(codec_t *ps_codec, nal_header_t *ps_nal)
   2721 {
   2722     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   2723     parse_ctxt_t *ps_parse = &ps_codec->s_parse;
   2724     UWORD32 u4_payload_type = 0, u4_last_payload_type_byte = 0;
   2725     UWORD32 u4_payload_size = 0, u4_last_payload_size_byte = 0;
   2726     UWORD32 value;
   2727     bitstrm_t *ps_bitstrm = &ps_parse->s_bitstrm;
   2728     UWORD32 u4_bits_left;
   2729 
   2730     u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
   2731 
   2732     while(u4_bits_left > 8)
   2733     {
   2734         while(ihevcd_bits_nxt(ps_bitstrm, 8) == 0xFF)
   2735         {
   2736             ihevcd_bits_flush(ps_bitstrm, 8); /* equal to 0xFF */
   2737             u4_payload_type += 255;
   2738         }
   2739 
   2740         BITS_PARSE("last_payload_type_byte", value, ps_bitstrm, 8);
   2741         u4_last_payload_type_byte = value;
   2742 
   2743         u4_payload_type += u4_last_payload_type_byte;
   2744 
   2745         while(ihevcd_bits_nxt(ps_bitstrm, 8) == 0xFF)
   2746         {
   2747             ihevcd_bits_flush(ps_bitstrm, 8); /* equal to 0xFF */
   2748             u4_payload_size += 255;
   2749         }
   2750 
   2751         BITS_PARSE("last_payload_size_byte", value, ps_bitstrm, 8);
   2752         u4_last_payload_size_byte = value;
   2753 
   2754         u4_payload_size += u4_last_payload_size_byte;
   2755         u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
   2756         u4_payload_size = MIN(u4_payload_size, u4_bits_left / 8);
   2757         ihevcd_parse_sei_payload(ps_codec, u4_payload_type, u4_payload_size,
   2758                                  ps_nal->i1_nal_unit_type);
   2759 
   2760         /* Calculate the bits left in the current payload */
   2761         u4_bits_left = ihevcd_bits_num_bits_remaining(ps_bitstrm);
   2762     }
   2763 
   2764     // read rbsp_trailing_bits
   2765     if(u4_bits_left)
   2766     {
   2767         ihevcd_read_rbsp_trailing_bits(ps_codec, u4_bits_left);
   2768     }
   2769 
   2770     return ret;
   2771 }
   2772 
   2773 /**
   2774 *******************************************************************************
   2775 *
   2776 * @brief
   2777 *  Parses Access unit delimiter
   2778 *
   2779 * @par Description:
   2780 *  Parses Access unit delimiter as per section  Section: 7.3.2.5
   2781 *
   2782 * @param[in] ps_codec
   2783 *  Pointer to codec context
   2784 *
   2785 * @returns Error code from IHEVCD_ERROR_T
   2786 *
   2787 * @remarks
   2788 *
   2789 *
   2790 *******************************************************************************
   2791 */
   2792 WORD32 ihevcd_parse_aud(codec_t *ps_codec)
   2793 {
   2794     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   2795     UNUSED(ps_codec);
   2796     return ret;
   2797 }
   2798 
   2799 WORD32 ihevcd_extend_sign_bit(WORD32 value, WORD32 num_bits)
   2800 {
   2801     WORD32 ret_value = value;
   2802     if(value >> (num_bits - 1))
   2803     {
   2804         ret_value |= (0xFFFFFFFF << num_bits);
   2805     }
   2806     return ret_value;
   2807 }
   2808 
   2809 /**
   2810 *******************************************************************************
   2811 *
   2812 * @brief
   2813 *  Calculate POC of the current slice
   2814 *
   2815 * @par Description:
   2816 *  Calculates the current POC using the previous POC lsb and previous POC msb
   2817 *
   2818 * @param[in] ps_codec
   2819 *  Pointer to codec context
   2820 *
   2821 * @param[in] i1_pic_order_cnt_lsb
   2822 *  Current POC lsb
   2823 *
   2824 * @returns  Current absolute POC
   2825 *
   2826 * @remarks
   2827 *
   2828 *
   2829 *******************************************************************************
   2830 */
   2831 
   2832 WORD32 ihevcd_calc_poc(codec_t *ps_codec, nal_header_t *ps_nal, WORD8 i1_log2_max_poc_lsb, WORD32 i2_poc_lsb)
   2833 {
   2834     WORD32 i4_abs_poc, i4_poc_msb;
   2835     WORD32 max_poc_lsb;
   2836     WORD8 i1_nal_unit_type = ps_nal->i1_nal_unit_type;
   2837     max_poc_lsb = (1 << i1_log2_max_poc_lsb);
   2838 
   2839     if((!ps_codec->i4_first_pic_done) && (!ps_codec->i4_pic_present))
   2840         ps_codec->i4_prev_poc_msb = -2 * max_poc_lsb;
   2841 
   2842     if(NAL_IDR_N_LP == i1_nal_unit_type
   2843                     || NAL_IDR_W_LP == i1_nal_unit_type
   2844                     || NAL_BLA_N_LP == i1_nal_unit_type
   2845                     || NAL_BLA_W_DLP == i1_nal_unit_type
   2846                     || NAL_BLA_W_LP == i1_nal_unit_type
   2847                     || (NAL_CRA == i1_nal_unit_type && !ps_codec->i4_first_pic_done))
   2848     {
   2849         i4_poc_msb = ps_codec->i4_prev_poc_msb + 2 * max_poc_lsb;
   2850         ps_codec->i4_prev_poc_lsb = 0;
   2851         ps_codec->i4_max_prev_poc_lsb = 0;
   2852 //        ps_codec->i4_prev_poc_msb = 0;
   2853     }
   2854     else
   2855     {
   2856 
   2857         if((i2_poc_lsb < ps_codec->i4_prev_poc_lsb)
   2858                         && ((ps_codec->i4_prev_poc_lsb - i2_poc_lsb) >= max_poc_lsb / 2))
   2859         {
   2860             i4_poc_msb = ps_codec->i4_prev_poc_msb + max_poc_lsb;
   2861         }
   2862         else if((i2_poc_lsb > ps_codec->i4_prev_poc_lsb)
   2863                         && ((i2_poc_lsb - ps_codec->i4_prev_poc_lsb) > max_poc_lsb / 2))
   2864         {
   2865             i4_poc_msb = ps_codec->i4_prev_poc_msb - max_poc_lsb;
   2866         }
   2867         else
   2868         {
   2869             i4_poc_msb = ps_codec->i4_prev_poc_msb;
   2870         }
   2871 
   2872 
   2873     }
   2874 
   2875     i4_abs_poc = i4_poc_msb + i2_poc_lsb;
   2876     ps_codec->i4_max_prev_poc_lsb = MAX(ps_codec->i4_max_prev_poc_lsb, i2_poc_lsb);
   2877 
   2878     {
   2879         WORD32 is_reference_nal = ((i1_nal_unit_type <= NAL_RSV_VCL_R15) && (i1_nal_unit_type % 2 != 0)) || ((i1_nal_unit_type >= NAL_BLA_W_LP) && (i1_nal_unit_type <= NAL_RSV_RAP_VCL23));
   2880         WORD32 update_prev_poc = ((is_reference_nal) && ((i1_nal_unit_type < NAL_RADL_N) || (i1_nal_unit_type > NAL_RASL_R)));
   2881 
   2882         if((0 == ps_nal->i1_nuh_temporal_id) &&
   2883                         (update_prev_poc))
   2884         {
   2885             ps_codec->i4_prev_poc_lsb = i2_poc_lsb;
   2886             ps_codec->i4_prev_poc_msb = i4_poc_msb;
   2887         }
   2888     }
   2889 
   2890     return i4_abs_poc;
   2891 }
   2892 
   2893 
   2894 void ihevcd_copy_slice_hdr(codec_t *ps_codec, WORD32 slice_idx, WORD32 slice_idx_ref)
   2895 {
   2896     slice_header_t *ps_slice_hdr, *ps_slice_hdr_ref;
   2897     WORD32 *pu4_entry_offset_backup;
   2898 
   2899     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base + slice_idx;
   2900     ps_slice_hdr_ref = ps_codec->s_parse.ps_slice_hdr_base + slice_idx_ref;
   2901 
   2902     pu4_entry_offset_backup = ps_slice_hdr->pu4_entry_point_offset;
   2903     memcpy(ps_slice_hdr, ps_slice_hdr_ref, sizeof(slice_header_t));
   2904     ps_slice_hdr->pu4_entry_point_offset = pu4_entry_offset_backup;
   2905 }
   2906 
   2907 
   2908 
   2909