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                 ps_stref_picset->ai1_used[i] = value;
    425                 ref_idc = value << 1;
    426             }
    427             if((ref_idc == 1) || (ref_idc == 2))
    428             {
    429                 WORD32 delta_poc;
    430                 delta_poc = delta_rps;
    431                 delta_poc +=
    432                                 ((i < ps_stref_picset_ref->i1_num_delta_pocs) ?
    433                                 ps_stref_picset_ref->ai2_delta_poc[i] :
    434                                 0);
    435 
    436                 ps_stref_picset->ai2_delta_poc[num_pics] = delta_poc;
    437 
    438                 if(delta_poc < 0)
    439                 {
    440                     num_neg_pics++;
    441                 }
    442                 else
    443                 {
    444                     num_pos_pics++;
    445                 }
    446                 num_pics++;
    447             }
    448             ps_stref_picset->ai1_ref_idc[i] = ref_idc;
    449         }
    450 
    451         num_neg_pics = CLIP3(num_neg_pics, 0, MAX_DPB_SIZE - 1);
    452         num_pos_pics = CLIP3(num_pos_pics, 0, (MAX_DPB_SIZE - 1 - num_neg_pics));
    453         num_pics = num_neg_pics + num_pos_pics;
    454 
    455         ps_stref_picset->i1_num_ref_idc =
    456                         ps_stref_picset_ref->i1_num_delta_pocs + 1;
    457         ps_stref_picset->i1_num_delta_pocs = num_pics;
    458         ps_stref_picset->i1_num_pos_pics = num_pos_pics;
    459         ps_stref_picset->i1_num_neg_pics = num_neg_pics;
    460 
    461 
    462         for(j = 1; j < num_pics; j++)
    463         {
    464             WORD32 delta_poc = ps_stref_picset->ai2_delta_poc[j];
    465             WORD8 i1_used = ps_stref_picset->ai1_used[j];
    466             for(k = j - 1; k >= 0; k--)
    467             {
    468                 temp = ps_stref_picset->ai2_delta_poc[k];
    469                 if(delta_poc < temp)
    470                 {
    471                     ps_stref_picset->ai2_delta_poc[k + 1] = temp;
    472                     ps_stref_picset->ai1_used[k + 1] = ps_stref_picset->ai1_used[k];
    473                     ps_stref_picset->ai2_delta_poc[k] = delta_poc;
    474                     ps_stref_picset->ai1_used[k] = i1_used;
    475                 }
    476             }
    477         }
    478         // flip the negative values to largest first
    479         for(j = 0, k = num_neg_pics - 1; j < num_neg_pics >> 1; j++, k--)
    480         {
    481             WORD32 delta_poc = ps_stref_picset->ai2_delta_poc[j];
    482             WORD8 i1_used = ps_stref_picset->ai1_used[j];
    483             ps_stref_picset->ai2_delta_poc[j] = ps_stref_picset->ai2_delta_poc[k];
    484             ps_stref_picset->ai1_used[j] = ps_stref_picset->ai1_used[k];
    485             ps_stref_picset->ai2_delta_poc[k] = delta_poc;
    486             ps_stref_picset->ai1_used[k] = i1_used;
    487         }
    488 
    489     }
    490     else
    491     {
    492         WORD32 prev_poc = 0;
    493         WORD32 poc;
    494 
    495         UEV_PARSE("num_negative_pics", value, ps_bitstrm);
    496         ps_stref_picset->i1_num_neg_pics = value;
    497         ps_stref_picset->i1_num_neg_pics = CLIP3(ps_stref_picset->i1_num_neg_pics,
    498                                                  0,
    499                                                  MAX_DPB_SIZE - 1);
    500 
    501         UEV_PARSE("num_positive_pics", value, ps_bitstrm);
    502         ps_stref_picset->i1_num_pos_pics = value;
    503         ps_stref_picset->i1_num_pos_pics = CLIP3(ps_stref_picset->i1_num_pos_pics,
    504                                                  0,
    505                                                  (MAX_DPB_SIZE - 1 - ps_stref_picset->i1_num_neg_pics));
    506 
    507         ps_stref_picset->i1_num_delta_pocs =
    508                         ps_stref_picset->i1_num_neg_pics +
    509                         ps_stref_picset->i1_num_pos_pics;
    510 
    511 
    512         for(i = 0; i < ps_stref_picset->i1_num_neg_pics; i++)
    513         {
    514             UEV_PARSE("delta_poc_s0_minus1", value, ps_bitstrm);
    515             poc = prev_poc - (value + 1);
    516             prev_poc = poc;
    517             ps_stref_picset->ai2_delta_poc[i] = poc;
    518 
    519             BITS_PARSE("used_by_curr_pic_s0_flag", value, ps_bitstrm, 1);
    520             ps_stref_picset->ai1_used[i] = value;
    521 
    522         }
    523         prev_poc = 0;
    524         for(i = ps_stref_picset->i1_num_neg_pics;
    525                         i < ps_stref_picset->i1_num_delta_pocs;
    526                         i++)
    527         {
    528             UEV_PARSE("delta_poc_s1_minus1", value, ps_bitstrm);
    529             poc = prev_poc + (value + 1);
    530             prev_poc = poc;
    531             ps_stref_picset->ai2_delta_poc[i] = poc;
    532 
    533             BITS_PARSE("used_by_curr_pic_s1_flag", value, ps_bitstrm, 1);
    534             ps_stref_picset->ai1_used[i] = value;
    535 
    536         }
    537 
    538     }
    539 
    540     return ret;
    541 }
    542 
    543 
    544 static WORD32 ihevcd_parse_sub_layer_hrd_parameters(bitstrm_t *ps_bitstrm,
    545                                                     sub_lyr_hrd_params_t *ps_sub_layer_hrd_params,
    546                                                     WORD32 cpb_cnt,
    547                                                     WORD32 sub_pic_cpb_params_present_flag)
    548 {
    549     WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    550     WORD32 i;
    551 
    552     for(i = 0; i <= cpb_cnt; i++)
    553     {
    554         UEV_PARSE("bit_rate_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_bit_rate_value_minus1[i], ps_bitstrm);
    555         UEV_PARSE("cpb_size_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_cpb_size_value_minus1[i], ps_bitstrm);
    556 
    557         if(sub_pic_cpb_params_present_flag)
    558         {
    559             UEV_PARSE("cpb_size_du_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_cpb_size_du_value_minus1[i], ps_bitstrm);
    560             UEV_PARSE("bit_rate_du_value_minus1[ i ]", ps_sub_layer_hrd_params->au4_bit_rate_du_value_minus1[i], ps_bitstrm);
    561         }
    562         BITS_PARSE("cbr_flag[ i ]", ps_sub_layer_hrd_params->au1_cbr_flag[i], ps_bitstrm, 1);
    563     }
    564 
    565     return ret;
    566 }
    567 
    568 
    569 static WORD32 ihevcd_parse_hrd_parameters(bitstrm_t *ps_bitstrm,
    570                                           hrd_params_t *ps_hrd,
    571                                           WORD32 common_info_present_flag,
    572                                           WORD32 max_num_sub_layers_minus1)
    573 {
    574     WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    575     WORD32 i;
    576 
    577     ps_hrd->u1_nal_hrd_parameters_present_flag = 0;
    578     ps_hrd->u1_vcl_hrd_parameters_present_flag = 0;
    579 
    580     ps_hrd->u1_sub_pic_cpb_params_present_flag = 0;
    581 
    582     ps_hrd->u1_tick_divisor_minus2 = 0;
    583     ps_hrd->u1_du_cpb_removal_delay_increment_length_minus1 = 0;
    584     ps_hrd->u1_sub_pic_cpb_params_in_pic_timing_sei_flag = 0;
    585     ps_hrd->u1_dpb_output_delay_du_length_minus1 = 0;
    586 
    587     ps_hrd->u4_bit_rate_scale = 0;
    588     ps_hrd->u4_cpb_size_scale = 0;
    589     ps_hrd->u4_cpb_size_du_scale = 0;
    590 
    591     ps_hrd->u1_initial_cpb_removal_delay_length_minus1 = 23;
    592     ps_hrd->u1_au_cpb_removal_delay_length_minus1 = 23;
    593     ps_hrd->u1_dpb_output_delay_length_minus1 = 23;
    594 
    595     if(common_info_present_flag)
    596     {
    597         BITS_PARSE("nal_hrd_parameters_present_flag", ps_hrd->u1_nal_hrd_parameters_present_flag, ps_bitstrm, 1);
    598         BITS_PARSE("vcl_hrd_parameters_present_flag", ps_hrd->u1_vcl_hrd_parameters_present_flag, ps_bitstrm, 1);
    599 
    600         if(ps_hrd->u1_nal_hrd_parameters_present_flag  ||  ps_hrd->u1_vcl_hrd_parameters_present_flag)
    601         {
    602             BITS_PARSE("sub_pic_cpb_params_present_flag", ps_hrd->u1_sub_pic_cpb_params_present_flag, ps_bitstrm, 1);
    603             if(ps_hrd->u1_sub_pic_cpb_params_present_flag)
    604             {
    605                 BITS_PARSE("tick_divisor_minus2", ps_hrd->u1_tick_divisor_minus2, ps_bitstrm, 8);
    606                 BITS_PARSE("du_cpb_removal_delay_increment_length_minus1", ps_hrd->u1_du_cpb_removal_delay_increment_length_minus1, ps_bitstrm, 5);
    607                 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);
    608                 BITS_PARSE("dpb_output_delay_du_length_minus1", ps_hrd->u1_dpb_output_delay_du_length_minus1, ps_bitstrm, 5);
    609             }
    610 
    611             BITS_PARSE("bit_rate_scale", ps_hrd->u4_bit_rate_scale, ps_bitstrm, 4);
    612             BITS_PARSE("cpb_size_scale", ps_hrd->u4_cpb_size_scale, ps_bitstrm, 4);
    613             if(ps_hrd->u1_sub_pic_cpb_params_present_flag)
    614                 BITS_PARSE("cpb_size_du_scale", ps_hrd->u4_cpb_size_du_scale, ps_bitstrm, 4);
    615 
    616             BITS_PARSE("initial_cpb_removal_delay_length_minus1", ps_hrd->u1_initial_cpb_removal_delay_length_minus1, ps_bitstrm, 5);
    617             BITS_PARSE("au_cpb_removal_delay_length_minus1", ps_hrd->u1_au_cpb_removal_delay_length_minus1, ps_bitstrm, 5);
    618             BITS_PARSE("dpb_output_delay_length_minus1", ps_hrd->u1_dpb_output_delay_length_minus1, ps_bitstrm, 5);
    619         }
    620     }
    621 
    622 
    623     for(i = 0; i <= max_num_sub_layers_minus1; i++)
    624     {
    625         BITS_PARSE("fixed_pic_rate_general_flag[ i ]", ps_hrd->au1_fixed_pic_rate_general_flag[i], ps_bitstrm, 1);
    626 
    627         ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i] = 1;
    628         ps_hrd->au1_elemental_duration_in_tc_minus1[i] = 0;
    629         ps_hrd->au1_low_delay_hrd_flag[i] = 0;
    630         ps_hrd->au1_cpb_cnt_minus1[i] = 0;
    631 
    632         if(!ps_hrd->au1_fixed_pic_rate_general_flag[i])
    633             BITS_PARSE("fixed_pic_rate_within_cvs_flag[ i ]", ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i], ps_bitstrm, 1);
    634 
    635         if(ps_hrd->au1_fixed_pic_rate_within_cvs_flag[i])
    636         {
    637             UEV_PARSE("elemental_duration_in_tc_minus1[ i ]", ps_hrd->au1_elemental_duration_in_tc_minus1[i], ps_bitstrm);
    638         }
    639         else
    640         {
    641             BITS_PARSE("low_delay_hrd_flag[ i ]", ps_hrd->au1_low_delay_hrd_flag[i], ps_bitstrm, 1);
    642         }
    643 
    644         if(!ps_hrd->au1_low_delay_hrd_flag[i])
    645             UEV_PARSE("cpb_cnt_minus1[ i ]", ps_hrd->au1_cpb_cnt_minus1[i], ps_bitstrm);
    646 
    647         if(ps_hrd->u1_nal_hrd_parameters_present_flag)
    648             ihevcd_parse_sub_layer_hrd_parameters(ps_bitstrm,
    649                                                   &ps_hrd->as_sub_layer_hrd_params[i],
    650                                                   ps_hrd->au1_cpb_cnt_minus1[i],
    651                                                   ps_hrd->u1_sub_pic_cpb_params_present_flag);
    652 
    653         if(ps_hrd->u1_vcl_hrd_parameters_present_flag)
    654             ihevcd_parse_sub_layer_hrd_parameters(ps_bitstrm,
    655                                                   &ps_hrd->as_sub_layer_hrd_params[i],
    656                                                   ps_hrd->au1_cpb_cnt_minus1[i],
    657                                                   ps_hrd->u1_sub_pic_cpb_params_present_flag);
    658     }
    659 
    660     return ret;
    661 }
    662 
    663 
    664 static WORD32 ihevcd_parse_vui_parameters(bitstrm_t *ps_bitstrm,
    665                                           vui_t *ps_vui,
    666                                           WORD32 sps_max_sub_layers_minus1)
    667 {
    668     WORD32 ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    669 
    670     BITS_PARSE("aspect_ratio_info_present_flag", ps_vui->u1_aspect_ratio_info_present_flag, ps_bitstrm, 1);
    671 
    672     ps_vui->u1_aspect_ratio_idc = SAR_UNUSED;
    673     ps_vui->u2_sar_width = 0;
    674     ps_vui->u2_sar_height = 0;
    675     if(ps_vui->u1_aspect_ratio_info_present_flag)
    676     {
    677         BITS_PARSE("aspect_ratio_idc", ps_vui->u1_aspect_ratio_idc, ps_bitstrm, 8);
    678         if(ps_vui->u1_aspect_ratio_idc  ==  EXTENDED_SAR)
    679         {
    680             BITS_PARSE("sar_width", ps_vui->u2_sar_width, ps_bitstrm, 16);
    681             BITS_PARSE("sar_height", ps_vui->u2_sar_height, ps_bitstrm, 16);
    682         }
    683     }
    684 
    685     BITS_PARSE("overscan_info_present_flag", ps_vui->u1_overscan_info_present_flag, ps_bitstrm, 1);
    686     ps_vui->u1_overscan_appropriate_flag = 0;
    687     if(ps_vui->u1_overscan_info_present_flag)
    688         BITS_PARSE("overscan_appropriate_flag", ps_vui->u1_overscan_appropriate_flag, ps_bitstrm, 1);
    689 
    690     BITS_PARSE("video_signal_type_present_flag", ps_vui->u1_video_signal_type_present_flag, ps_bitstrm, 1);
    691     ps_vui->u1_video_format = VID_FMT_UNSPECIFIED;
    692     ps_vui->u1_video_full_range_flag = 0;
    693     ps_vui->u1_colour_description_present_flag = 0;
    694     ps_vui->u1_colour_primaries = 2;
    695     ps_vui->u1_transfer_characteristics = 2;
    696     ps_vui->u1_matrix_coefficients = 2;
    697 
    698     if(ps_vui->u1_video_signal_type_present_flag)
    699     {
    700         BITS_PARSE("video_format", ps_vui->u1_video_format, ps_bitstrm, 3);
    701         BITS_PARSE("video_full_range_flag", ps_vui->u1_video_full_range_flag, ps_bitstrm, 1);
    702         BITS_PARSE("colour_description_present_flag", ps_vui->u1_colour_description_present_flag, ps_bitstrm, 1);
    703         if(ps_vui->u1_colour_description_present_flag)
    704         {
    705             BITS_PARSE("colour_primaries", ps_vui->u1_colour_primaries, ps_bitstrm, 8);
    706             BITS_PARSE("transfer_characteristics", ps_vui->u1_transfer_characteristics, ps_bitstrm, 8);
    707             BITS_PARSE("matrix_coeffs", ps_vui->u1_matrix_coefficients, ps_bitstrm, 8);
    708         }
    709     }
    710 
    711     BITS_PARSE("chroma_loc_info_present_flag", ps_vui->u1_chroma_loc_info_present_flag, ps_bitstrm, 1);
    712     ps_vui->u1_chroma_sample_loc_type_top_field = 0;
    713     ps_vui->u1_chroma_sample_loc_type_bottom_field = 0;
    714     if(ps_vui->u1_chroma_loc_info_present_flag)
    715     {
    716         UEV_PARSE("chroma_sample_loc_type_top_field", ps_vui->u1_chroma_sample_loc_type_top_field, ps_bitstrm);
    717         UEV_PARSE("chroma_sample_loc_type_bottom_field", ps_vui->u1_chroma_sample_loc_type_bottom_field, ps_bitstrm);
    718     }
    719 
    720     BITS_PARSE("neutral_chroma_indication_flag", ps_vui->u1_neutral_chroma_indication_flag, ps_bitstrm, 1);
    721     BITS_PARSE("field_seq_flag", ps_vui->u1_field_seq_flag, ps_bitstrm, 1);
    722     BITS_PARSE("frame_field_info_present_flag", ps_vui->u1_frame_field_info_present_flag, ps_bitstrm, 1);
    723     BITS_PARSE("default_display_window_flag", ps_vui->u1_default_display_window_flag, ps_bitstrm, 1);
    724     ps_vui->u4_def_disp_win_left_offset = 0;
    725     ps_vui->u4_def_disp_win_right_offset = 0;
    726     ps_vui->u4_def_disp_win_top_offset = 0;
    727     ps_vui->u4_def_disp_win_bottom_offset = 0;
    728     if(ps_vui->u1_default_display_window_flag)
    729     {
    730         UEV_PARSE("def_disp_win_left_offset", ps_vui->u4_def_disp_win_left_offset, ps_bitstrm);
    731         UEV_PARSE("def_disp_win_right_offset", ps_vui->u4_def_disp_win_right_offset, ps_bitstrm);
    732         UEV_PARSE("def_disp_win_top_offset", ps_vui->u4_def_disp_win_top_offset, ps_bitstrm);
    733         UEV_PARSE("def_disp_win_bottom_offset", ps_vui->u4_def_disp_win_bottom_offset, ps_bitstrm);
    734     }
    735 
    736     BITS_PARSE("vui_timing_info_present_flag", ps_vui->u1_vui_timing_info_present_flag, ps_bitstrm, 1);
    737     if(ps_vui->u1_vui_timing_info_present_flag)
    738     {
    739         BITS_PARSE("vui_num_units_in_tick", ps_vui->u4_vui_num_units_in_tick, ps_bitstrm, 32);
    740         BITS_PARSE("vui_time_scale", ps_vui->u4_vui_time_scale, ps_bitstrm, 32);
    741         BITS_PARSE("vui_poc_proportional_to_timing_flag", ps_vui->u1_poc_proportional_to_timing_flag, ps_bitstrm, 1);
    742         if(ps_vui->u1_poc_proportional_to_timing_flag)
    743             UEV_PARSE("vui_num_ticks_poc_diff_one_minus1", ps_vui->u1_num_ticks_poc_diff_one_minus1, ps_bitstrm);
    744 
    745         BITS_PARSE("vui_hrd_parameters_present_flag", ps_vui->u1_vui_hrd_parameters_present_flag, ps_bitstrm, 1);
    746         if(ps_vui->u1_vui_hrd_parameters_present_flag)
    747             ihevcd_parse_hrd_parameters(ps_bitstrm, &ps_vui->s_vui_hrd_parameters, 1, sps_max_sub_layers_minus1);
    748     }
    749 
    750     BITS_PARSE("bitstream_restriction_flag", ps_vui->u1_bitstream_restriction_flag, ps_bitstrm, 1);
    751     ps_vui->u1_tiles_fixed_structure_flag = 0;
    752     ps_vui->u1_motion_vectors_over_pic_boundaries_flag = 1;
    753     ps_vui->u1_restricted_ref_pic_lists_flag = 0;
    754     ps_vui->u4_min_spatial_segmentation_idc = 0;
    755     ps_vui->u1_max_bytes_per_pic_denom = 2;
    756     ps_vui->u1_max_bits_per_mincu_denom = 1;
    757     ps_vui->u1_log2_max_mv_length_horizontal = 15;
    758     ps_vui->u1_log2_max_mv_length_vertical = 15;
    759     if(ps_vui->u1_bitstream_restriction_flag)
    760     {
    761         BITS_PARSE("tiles_fixed_structure_flag", ps_vui->u1_tiles_fixed_structure_flag, ps_bitstrm, 1);
    762         BITS_PARSE("motion_vectors_over_pic_boundaries_flag", ps_vui->u1_motion_vectors_over_pic_boundaries_flag, ps_bitstrm, 1);
    763         BITS_PARSE("restricted_ref_pic_lists_flag", ps_vui->u1_restricted_ref_pic_lists_flag, ps_bitstrm, 1);
    764 
    765         UEV_PARSE("min_spatial_segmentation_idc", ps_vui->u4_min_spatial_segmentation_idc, ps_bitstrm);
    766         UEV_PARSE("max_bytes_per_pic_denom", ps_vui->u1_max_bytes_per_pic_denom, ps_bitstrm);
    767         UEV_PARSE("max_bits_per_min_cu_denom", ps_vui->u1_max_bits_per_mincu_denom, ps_bitstrm);
    768         UEV_PARSE("log2_max_mv_length_horizontal", ps_vui->u1_log2_max_mv_length_horizontal, ps_bitstrm);
    769         UEV_PARSE("log2_max_mv_length_vertical", ps_vui->u1_log2_max_mv_length_vertical, ps_bitstrm);
    770     }
    771 
    772     return ret;
    773 }
    774 
    775 /**
    776 *******************************************************************************
    777 *
    778 * @brief
    779 *  Parses profile tier and level info for either general layer of sub_layer
    780 *
    781 * @par   Description
    782 *  Parses profile tier and level info for either general layer of sub_layer
    783 * as per section 7.3.3
    784 *
    785 * Since the same function is called for parsing general_profile and
    786 * sub_layer_profile etc, variables do not specify whether the syntax is
    787 * for general or sub_layer. Similarly trace functions also do not differentiate
    788 *
    789 * @param[in] ps_bitstrm
    790 *  Pointer to bitstream structure
    791 *
    792 * @param[out] ps_ptl
    793 *  Pointer to profile, tier level structure
    794 *
    795 * @returns Error code from IHEVCD_ERROR_T
    796 *
    797 * @remarks
    798 *
    799 *******************************************************************************
    800 */
    801 
    802 static IHEVCD_ERROR_T ihevcd_parse_profile_tier_level_layer(bitstrm_t *ps_bitstrm,
    803                                                             profile_tier_lvl_t *ps_ptl)
    804 {
    805     WORD32 value;
    806     WORD32 i;
    807     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    808 
    809     BITS_PARSE("XXX_profile_space[]", value, ps_bitstrm, 2);
    810     ps_ptl->i1_profile_space = value;
    811 
    812     BITS_PARSE("XXX_tier_flag[]", value, ps_bitstrm, 1);
    813     ps_ptl->i1_tier_flag = value;
    814 
    815     BITS_PARSE("XXX_profile_idc[]", value, ps_bitstrm, 5);
    816     ps_ptl->i1_profile_idc = value;
    817 
    818     for(i = 0; i < MAX_PROFILE_COMPATBLTY; i++)
    819     {
    820         BITS_PARSE("XXX_profile_compatibility_flag[][j]", value, ps_bitstrm, 1);
    821         ps_ptl->ai1_profile_compatibility_flag[i] = value;
    822     }
    823 
    824     BITS_PARSE("general_progressive_source_flag", value, ps_bitstrm, 1);
    825     ps_ptl->i1_general_progressive_source_flag = value;
    826 
    827     BITS_PARSE("general_interlaced_source_flag", value, ps_bitstrm, 1);
    828     ps_ptl->i1_general_progressive_source_flag = value;
    829 
    830     BITS_PARSE("general_non_packed_constraint_flag", value, ps_bitstrm, 1);
    831     ps_ptl->i1_general_progressive_source_flag = value;
    832 
    833     BITS_PARSE("general_frame_only_constraint_flag", value, ps_bitstrm, 1);
    834     ps_ptl->i1_general_progressive_source_flag = value;
    835 
    836     BITS_PARSE("XXX_reserved_zero_44bits[0..15]", value, ps_bitstrm, 16);
    837 
    838     BITS_PARSE("XXX_reserved_zero_44bits[16..31]", value, ps_bitstrm, 16);
    839 
    840     BITS_PARSE("XXX_reserved_zero_44bits[32..43]", value, ps_bitstrm, 12);
    841     return ret;
    842 }
    843 
    844 
    845 /**
    846 *******************************************************************************
    847 *
    848 * @brief
    849 *  Parses profile tier and level info
    850 *
    851 * @par   Description
    852 *  Parses profile tier and level info as per section 7.3.3
    853 * Called during VPS and SPS parsing
    854 * calls ihevcd_parse_profile_tier_level() for general layer and each sub_layers
    855 *
    856 * @param[in] ps_bitstrm
    857 *  Pointer to bitstream structure
    858 *
    859 * @param[out] ps_ptl
    860 *  Pointer to structure that contains profile, tier level for each layers
    861 *
    862 * @param[in] profile_present
    863 *  Flag to indicate if profile data is present
    864 *
    865 * @param[in] max_num_sub_layers
    866 *  Number of sub layers present
    867 *
    868 * @returns Error code from IHEVCD_ERROR_T
    869 *
    870 * @remarks
    871 *
    872 *******************************************************************************
    873 */
    874 
    875 static IHEVCD_ERROR_T ihevcd_profile_tier_level(bitstrm_t *ps_bitstrm,
    876                                                 profile_tier_lvl_info_t *ps_ptl,
    877                                                 WORD32 profile_present,
    878                                                 WORD32 max_num_sub_layers)
    879 {
    880     WORD32 value;
    881     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    882     WORD32 i;
    883 
    884     if(profile_present)
    885     {
    886         ret = ihevcd_parse_profile_tier_level_layer(ps_bitstrm, &ps_ptl->s_ptl_gen);
    887     }
    888 
    889     BITS_PARSE("general_level_idc", value, ps_bitstrm, 8);
    890     ps_ptl->s_ptl_gen.u1_level_idc = value;
    891 
    892 
    893     for(i = 0; i < max_num_sub_layers; i++)
    894     {
    895         BITS_PARSE("sub_layer_profile_present_flag[i]", value, ps_bitstrm, 1);
    896         ps_ptl->ai1_sub_layer_profile_present_flag[i] = value;
    897 
    898         BITS_PARSE("sub_layer_level_present_flag[i]", value, ps_bitstrm, 1);
    899         ps_ptl->ai1_sub_layer_level_present_flag[i] = value;
    900     }
    901 
    902     if(max_num_sub_layers > 0)
    903     {
    904         for(i = max_num_sub_layers; i < 8; i++)
    905         {
    906             BITS_PARSE("reserved_zero_2bits", value, ps_bitstrm, 2);
    907         }
    908     }
    909 
    910     for(i = 0; i < max_num_sub_layers; i++)
    911     {
    912         if(ps_ptl->ai1_sub_layer_profile_present_flag[i])
    913         {
    914             ret = ihevcd_parse_profile_tier_level_layer(ps_bitstrm,
    915                                                         &ps_ptl->as_ptl_sub[i]);
    916         }
    917         if(ps_ptl->ai1_sub_layer_level_present_flag[i])
    918         {
    919             BITS_PARSE("sub_layer_level_idc[i]", value, ps_bitstrm, 8);
    920             ps_ptl->as_ptl_sub[i].u1_level_idc = value;
    921 
    922         }
    923     }
    924 
    925 
    926 
    927     return ret;
    928 }
    929 
    930 /**
    931 *******************************************************************************
    932 *
    933 * @brief
    934 *  Parses Scaling List Data syntax
    935 *
    936 * @par Description:
    937 *  Parses Scaling List Data syntax as per Section: 7.3.6
    938 *
    939 * @param[in] ps_codec
    940 *  Pointer to codec context
    941 *
    942 * @returns  Error code from IHEVCD_ERROR_T
    943 *
    944 * @remarks
    945 *
    946 *
    947 *******************************************************************************
    948 */
    949 IHEVCD_ERROR_T  ihevcd_scaling_list_data(codec_t *ps_codec, WORD16 *pi2_scaling_mat)
    950 {
    951     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    952     WORD32 size_id;
    953     WORD32 matrix_id;
    954     WORD32 value, dc_value = 0;
    955     WORD32 next_coef;
    956     WORD32 coef_num;
    957     WORD32 i, j, offset;
    958     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
    959     WORD16 *pi2_scaling_mat_offset;
    960     WORD32 scaling_mat_offset[] = { 0, 16, 32, 48, 64, 80, 96, 160, 224, 288, 352, 416, 480, 736, 992, 1248, 1504, 1760, 2016, 3040 };
    961     UWORD8 *scan_table;
    962 
    963     for(size_id = 0; size_id < 4; size_id++)
    964     {
    965         for(matrix_id = 0; matrix_id < ((size_id == 3) ? 2 : 6); matrix_id++)
    966         {
    967             WORD32 scaling_list_pred_mode_flag;
    968             WORD32 scaling_list_delta_coef;
    969             BITS_PARSE("scaling_list_pred_mode_flag", scaling_list_pred_mode_flag, ps_bitstrm, 1);
    970 
    971             offset = size_id * 6 + matrix_id;
    972             pi2_scaling_mat_offset = pi2_scaling_mat + scaling_mat_offset[offset];
    973 
    974             if(!scaling_list_pred_mode_flag)
    975             {
    976                 WORD32 num_elements;
    977                 UEV_PARSE("scaling_list_pred_matrix_id_delta", value,
    978                           ps_bitstrm);
    979                 value = CLIP3(value, 0, matrix_id);
    980 
    981                 num_elements = (1 << (4 + (size_id << 1)));
    982                 if(0 != value)
    983                     memcpy(pi2_scaling_mat_offset, pi2_scaling_mat_offset - value * num_elements, num_elements * sizeof(WORD16));
    984             }
    985             else
    986             {
    987                 next_coef = 8;
    988                 coef_num = MIN(64, (1 << (4 + (size_id << 1))));
    989 
    990                 if(size_id > 1)
    991                 {
    992                     SEV_PARSE("scaling_list_dc_coef_minus8", value,
    993                               ps_bitstrm);
    994 
    995                     next_coef = value + 8;
    996                     dc_value = next_coef;
    997                 }
    998                 if(size_id < 2)
    999                 {
   1000                     scan_table = (UWORD8 *)gapv_ihevc_invscan[size_id + 1];
   1001 
   1002                     for(i = 0; i < coef_num; i++)
   1003                     {
   1004                         SEV_PARSE("scaling_list_delta_coef",
   1005                                   scaling_list_delta_coef, ps_bitstrm);
   1006                         next_coef = (next_coef + scaling_list_delta_coef + 256)
   1007                                         % 256;
   1008                         pi2_scaling_mat_offset[scan_table[i]] = next_coef;
   1009                     }
   1010                 }
   1011                 else if(size_id == 2)
   1012                 {
   1013                     scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
   1014 
   1015                     for(i = 0; i < coef_num; i++)
   1016                     {
   1017                         SEV_PARSE("scaling_list_delta_coef",
   1018                                   scaling_list_delta_coef, ps_bitstrm);
   1019                         next_coef = (next_coef + scaling_list_delta_coef + 256)
   1020                                         % 256;
   1021 
   1022                         offset = scan_table[i];
   1023                         offset = (offset >> 3) * 16 * 2 + (offset & 0x7) * 2;
   1024                         pi2_scaling_mat_offset[offset] = next_coef;
   1025                         pi2_scaling_mat_offset[offset + 1] = next_coef;
   1026                         pi2_scaling_mat_offset[offset + 16] = next_coef;
   1027                         pi2_scaling_mat_offset[offset + 16 + 1] = next_coef;
   1028                     }
   1029                     pi2_scaling_mat_offset[0] = dc_value;
   1030                 }
   1031                 else
   1032                 {
   1033                     scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
   1034 
   1035                     for(i = 0; i < coef_num; i++)
   1036                     {
   1037                         SEV_PARSE("scaling_list_delta_coef",
   1038                                   scaling_list_delta_coef, ps_bitstrm);
   1039                         next_coef = (next_coef + scaling_list_delta_coef + 256)
   1040                                         % 256;
   1041 
   1042                         offset = scan_table[i];
   1043                         offset = (offset >> 3) * 32 * 4 + (offset & 0x7) * 4;
   1044 
   1045                         for(j = 0; j < 4; j++)
   1046                         {
   1047                             pi2_scaling_mat_offset[offset + j * 32] = next_coef;
   1048                             pi2_scaling_mat_offset[offset + 1 + j * 32] = next_coef;
   1049                             pi2_scaling_mat_offset[offset + 2 + j * 32] = next_coef;
   1050                             pi2_scaling_mat_offset[offset + 3 + j * 32] = next_coef;
   1051                         }
   1052                         pi2_scaling_mat_offset[0] = dc_value;
   1053                     }
   1054                 }
   1055             }
   1056         }
   1057     }
   1058 
   1059     return ret;
   1060 }
   1061 
   1062 /**
   1063 *******************************************************************************
   1064 *
   1065 * @brief
   1066 *  Parses VPS (Video Parameter Set)
   1067 *
   1068 * @par Description:
   1069 *  Parse Video Parameter Set as per Section 7.3.2.1
   1070 * update vps structure corresponding to vps ID
   1071 * Till parsing VPS id, the elements are stored in local variables and are copied
   1072 * later
   1073 *
   1074 * @param[in] ps_codec
   1075 *  Pointer to codec context.
   1076 *
   1077 * @returns Error code from IHEVCD_ERROR_T
   1078 *
   1079 * @remarks
   1080 *
   1081 *
   1082 *******************************************************************************
   1083 */
   1084 IHEVCD_ERROR_T ihevcd_parse_vps(codec_t *ps_codec)
   1085 {
   1086     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   1087     WORD32 i;
   1088     WORD32 value;
   1089     WORD32 vps_id;
   1090     vps_t *ps_vps;
   1091     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
   1092     BITS_PARSE("vps_video_parameter_set_id", value, ps_bitstrm, 4);
   1093     vps_id = value;
   1094 
   1095     if(vps_id >= MAX_VPS_CNT)
   1096     {
   1097         ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_VPS_ID;
   1098         return IHEVCD_UNSUPPORTED_VPS_ID;
   1099     }
   1100 
   1101 
   1102     ps_vps = (ps_codec->s_parse.ps_vps_base + vps_id);
   1103 
   1104     ps_vps->i1_vps_id = vps_id;
   1105 
   1106     BITS_PARSE("vps_reserved_three_2bits", value, ps_bitstrm, 2);
   1107     ASSERT(value == 3);
   1108 
   1109     BITS_PARSE("vps_max_layers_minus1", value, ps_bitstrm, 6);
   1110     //ps_vps->i1_vps_max_layers = value + 1;
   1111 
   1112 
   1113 
   1114     BITS_PARSE("vps_max_sub_layers_minus1", value, ps_bitstrm, 3);
   1115     ps_vps->i1_vps_max_sub_layers = value + 1;
   1116 
   1117     ASSERT(ps_vps->i1_vps_max_sub_layers < VPS_MAX_SUB_LAYERS);
   1118 
   1119     BITS_PARSE("vps_temporal_id_nesting_flag", value, ps_bitstrm, 1);
   1120     ps_vps->i1_vps_temporal_id_nesting_flag = value;
   1121 
   1122     BITS_PARSE("vps_reserved_ffff_16bits", value, ps_bitstrm, 16);
   1123     ASSERT(value == 0xFFFF);
   1124     // profile_and_level( 1, vps_max_sub_layers_minus1 )
   1125     ret = ihevcd_profile_tier_level(ps_bitstrm, &(ps_vps->s_ptl),
   1126                                     1, (ps_vps->i1_vps_max_sub_layers - 1));
   1127 
   1128     BITS_PARSE("vps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
   1129     ps_vps->i1_sub_layer_ordering_info_present_flag = value;
   1130     i = (ps_vps->i1_sub_layer_ordering_info_present_flag ?
   1131                     0 : (ps_vps->i1_vps_max_sub_layers - 1));
   1132     for(; i < ps_vps->i1_vps_max_sub_layers; i++)
   1133     {
   1134         UEV_PARSE("vps_max_dec_pic_buffering[i]", value, ps_bitstrm);
   1135         ps_vps->ai1_vps_max_dec_pic_buffering[i] = value;
   1136 
   1137         /* vps_num_reorder_pics (no max) used in print in order to match with HM */
   1138         UEV_PARSE("vps_num_reorder_pics[i]", value, ps_bitstrm);
   1139         ps_vps->ai1_vps_max_num_reorder_pics[i] = value;
   1140 
   1141         UEV_PARSE("vps_max_latency_increase[i]", value, ps_bitstrm);
   1142         ps_vps->ai1_vps_max_latency_increase[i] = value;
   1143     }
   1144 
   1145 
   1146 
   1147     BITS_PARSE("vps_max_layer_id", value, ps_bitstrm, 6);
   1148     //ps_vps->i1_vps_max_layer_id  = value;
   1149 
   1150     UEV_PARSE("vps_num_layer_sets_minus1", value, ps_bitstrm);
   1151     //ps_vps->i1_vps_num_layer_sets  = value + 1;
   1152 
   1153     BITS_PARSE("vps_timing_info_present_flag", value, ps_bitstrm, 1);
   1154     //ps_vps->i1_vps_timing_info_present_flag  = value;
   1155 
   1156 
   1157 
   1158     return ret;
   1159 }
   1160 
   1161 /**
   1162 *******************************************************************************
   1163 *
   1164 * @brief
   1165 *  Parses SPS (Sequence Parameter Set)
   1166 * sequence_parameter_set_rbsp()
   1167 *
   1168 * @par Description:
   1169 *  Parse Sequence Parameter Set as per section  Section: 7.3.2.2
   1170 * The sps is written to a temporary buffer and copied later to the
   1171 * appropriate location
   1172 *
   1173 * @param[in] ps_codec
   1174 *  Pointer to codec context
   1175 *
   1176 * @returns Error code from IHEVCD_ERROR_T
   1177 *
   1178 * @remarks
   1179 *
   1180 *
   1181 *******************************************************************************
   1182 */
   1183 IHEVCD_ERROR_T ihevcd_parse_sps(codec_t *ps_codec)
   1184 {
   1185     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   1186     WORD32 value;
   1187 
   1188     WORD32 i;
   1189     WORD32 vps_id;
   1190     WORD32 sps_max_sub_layers;
   1191     WORD32 sps_id;
   1192     WORD32 sps_temporal_id_nesting_flag;
   1193     sps_t *ps_sps;
   1194     profile_tier_lvl_info_t s_ptl;
   1195     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
   1196 
   1197 
   1198     BITS_PARSE("video_parameter_set_id", value, ps_bitstrm, 4);
   1199     vps_id = value;
   1200     vps_id = CLIP3(vps_id, 0, MAX_VPS_CNT - 1);
   1201 
   1202     BITS_PARSE("sps_max_sub_layers_minus1", value, ps_bitstrm, 3);
   1203     sps_max_sub_layers = value + 1;
   1204     sps_max_sub_layers = CLIP3(sps_max_sub_layers, 1, 7);
   1205 
   1206     BITS_PARSE("sps_temporal_id_nesting_flag", value, ps_bitstrm, 1);
   1207     sps_temporal_id_nesting_flag = value;
   1208 
   1209     //profile_and_level( 1, sps_max_sub_layers_minus1 )
   1210     ret = ihevcd_profile_tier_level(ps_bitstrm, &(s_ptl), 1,
   1211                                     (sps_max_sub_layers - 1));
   1212 
   1213     UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
   1214     sps_id = value;
   1215 
   1216     if((sps_id >= MAX_SPS_CNT) || (sps_id < 0))
   1217     {
   1218         if(ps_codec->i4_sps_done)
   1219             return IHEVCD_UNSUPPORTED_SPS_ID;
   1220         else
   1221             sps_id = 0;
   1222     }
   1223 
   1224 
   1225     ps_sps = (ps_codec->s_parse.ps_sps_base + MAX_SPS_CNT - 1);
   1226     ps_sps->i1_sps_id = sps_id;
   1227     ps_sps->i1_vps_id = vps_id;
   1228     ps_sps->i1_sps_max_sub_layers = sps_max_sub_layers;
   1229     ps_sps->i1_sps_temporal_id_nesting_flag = sps_temporal_id_nesting_flag;
   1230     /* This is used only during initialization to get reorder count etc */
   1231     ps_codec->i4_sps_id = sps_id;
   1232     memcpy(&ps_sps->s_ptl, &s_ptl, sizeof(profile_tier_lvl_info_t));
   1233 
   1234     UEV_PARSE("chroma_format_idc", value, ps_bitstrm);
   1235     ps_sps->i1_chroma_format_idc = value;
   1236 
   1237     if(ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_YUV420)
   1238     {
   1239         ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
   1240         return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
   1241     }
   1242 
   1243     if(CHROMA_FMT_IDC_YUV444_PLANES == ps_sps->i1_chroma_format_idc)
   1244     {
   1245         BITS_PARSE("separate_colour_plane_flag", value, ps_bitstrm, 1);
   1246         ps_sps->i1_separate_colour_plane_flag = value;
   1247     }
   1248     else
   1249     {
   1250         ps_sps->i1_separate_colour_plane_flag = 0;
   1251     }
   1252 
   1253     UEV_PARSE("pic_width_in_luma_samples", value, ps_bitstrm);
   1254     ps_sps->i2_pic_width_in_luma_samples = value;
   1255 
   1256     UEV_PARSE("pic_height_in_luma_samples", value, ps_bitstrm);
   1257     ps_sps->i2_pic_height_in_luma_samples = value;
   1258 
   1259     if((0 >= ps_sps->i2_pic_width_in_luma_samples) || (0 >= ps_sps->i2_pic_height_in_luma_samples))
   1260         return IHEVCD_INVALID_PARAMETER;
   1261 
   1262     /* i2_pic_width_in_luma_samples and i2_pic_height_in_luma_samples
   1263        should be multiples of min_cb_size. Here these are aligned to 8,
   1264        i.e. smallest CB size */
   1265     ps_sps->i2_pic_width_in_luma_samples = ALIGN8(ps_sps->i2_pic_width_in_luma_samples);
   1266     ps_sps->i2_pic_height_in_luma_samples = ALIGN8(ps_sps->i2_pic_height_in_luma_samples);
   1267 
   1268     BITS_PARSE("pic_cropping_flag", value, ps_bitstrm, 1);
   1269     ps_sps->i1_pic_cropping_flag = value;
   1270 
   1271     if(ps_sps->i1_pic_cropping_flag)
   1272     {
   1273 
   1274         UEV_PARSE("pic_crop_left_offset", value, ps_bitstrm);
   1275         ps_sps->i2_pic_crop_left_offset = value;
   1276 
   1277         UEV_PARSE("pic_crop_right_offset", value, ps_bitstrm);
   1278         ps_sps->i2_pic_crop_right_offset = value;
   1279 
   1280         UEV_PARSE("pic_crop_top_offset", value, ps_bitstrm);
   1281         ps_sps->i2_pic_crop_top_offset = value;
   1282 
   1283         UEV_PARSE("pic_crop_bottom_offset", value, ps_bitstrm);
   1284         ps_sps->i2_pic_crop_bottom_offset = value;
   1285     }
   1286     else
   1287     {
   1288         ps_sps->i2_pic_crop_left_offset = 0;
   1289         ps_sps->i2_pic_crop_right_offset = 0;
   1290         ps_sps->i2_pic_crop_top_offset = 0;
   1291         ps_sps->i2_pic_crop_bottom_offset = 0;
   1292     }
   1293 
   1294 
   1295     UEV_PARSE("bit_depth_luma_minus8", value, ps_bitstrm);
   1296     if(0 != value)
   1297         return IHEVCD_UNSUPPORTED_BIT_DEPTH;
   1298 
   1299     UEV_PARSE("bit_depth_chroma_minus8", value, ps_bitstrm);
   1300     if(0 != value)
   1301         return IHEVCD_UNSUPPORTED_BIT_DEPTH;
   1302 
   1303     UEV_PARSE("log2_max_pic_order_cnt_lsb_minus4", value, ps_bitstrm);
   1304     ps_sps->i1_log2_max_pic_order_cnt_lsb = value + 4;
   1305 
   1306     BITS_PARSE("sps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
   1307     ps_sps->i1_sps_sub_layer_ordering_info_present_flag = value;
   1308 
   1309 
   1310     i = (ps_sps->i1_sps_sub_layer_ordering_info_present_flag ? 0 : (ps_sps->i1_sps_max_sub_layers - 1));
   1311     for(; i < ps_sps->i1_sps_max_sub_layers; i++)
   1312     {
   1313         UEV_PARSE("max_dec_pic_buffering", value, ps_bitstrm);
   1314         ps_sps->ai1_sps_max_dec_pic_buffering[i] = value + 1;
   1315 
   1316         UEV_PARSE("num_reorder_pics", value, ps_bitstrm);
   1317         ps_sps->ai1_sps_max_num_reorder_pics[i] = value;
   1318 
   1319         UEV_PARSE("max_latency_increase", value, ps_bitstrm);
   1320         ps_sps->ai1_sps_max_latency_increase[i] = value;
   1321     }
   1322     UEV_PARSE("log2_min_coding_block_size_minus3", value, ps_bitstrm);
   1323     ps_sps->i1_log2_min_coding_block_size = value + 3;
   1324 
   1325     UEV_PARSE("log2_diff_max_min_coding_block_size", value, ps_bitstrm);
   1326     ps_sps->i1_log2_diff_max_min_coding_block_size = value;
   1327 
   1328     UEV_PARSE("log2_min_transform_block_size_minus2", value, ps_bitstrm);
   1329     ps_sps->i1_log2_min_transform_block_size = value + 2;
   1330 
   1331     UEV_PARSE("log2_diff_max_min_transform_block_size", value, ps_bitstrm);
   1332     ps_sps->i1_log2_diff_max_min_transform_block_size = value;
   1333 
   1334     ps_sps->i1_log2_max_transform_block_size = ps_sps->i1_log2_min_transform_block_size +
   1335                     ps_sps->i1_log2_diff_max_min_transform_block_size;
   1336 
   1337     ps_sps->i1_log2_ctb_size = ps_sps->i1_log2_min_coding_block_size +
   1338                     ps_sps->i1_log2_diff_max_min_coding_block_size;
   1339 
   1340     if((ps_sps->i1_log2_min_coding_block_size < 3) ||
   1341                     (ps_sps->i1_log2_min_transform_block_size < 2) ||
   1342                     (ps_sps->i1_log2_diff_max_min_transform_block_size < 0) ||
   1343                     (ps_sps->i1_log2_max_transform_block_size > ps_sps->i1_log2_ctb_size) ||
   1344                     (ps_sps->i1_log2_ctb_size < 4) ||
   1345                     (ps_sps->i1_log2_ctb_size > 6))
   1346     {
   1347         return IHEVCD_INVALID_PARAMETER;
   1348     }
   1349 
   1350     ps_sps->i1_log2_min_pcm_coding_block_size = 0;
   1351     ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = 0;
   1352 
   1353     UEV_PARSE("max_transform_hierarchy_depth_inter", value, ps_bitstrm);
   1354     ps_sps->i1_max_transform_hierarchy_depth_inter = value;
   1355 
   1356     UEV_PARSE("max_transform_hierarchy_depth_intra", value, ps_bitstrm);
   1357     ps_sps->i1_max_transform_hierarchy_depth_intra = value;
   1358 
   1359     /* String has a d (enabled) in order to match with HM */
   1360     BITS_PARSE("scaling_list_enabled_flag", value, ps_bitstrm, 1);
   1361     ps_sps->i1_scaling_list_enable_flag = value;
   1362 
   1363     if(ps_sps->i1_scaling_list_enable_flag)
   1364     {
   1365         COPY_DEFAULT_SCALING_LIST(ps_sps->pi2_scaling_mat);
   1366         BITS_PARSE("sps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
   1367         ps_sps->i1_sps_scaling_list_data_present_flag = value;
   1368 
   1369         if(ps_sps->i1_sps_scaling_list_data_present_flag)
   1370             ihevcd_scaling_list_data(ps_codec, ps_sps->pi2_scaling_mat);
   1371     }
   1372     else
   1373     {
   1374         COPY_FLAT_SCALING_LIST(ps_sps->pi2_scaling_mat);
   1375     }
   1376     /* String is asymmetric_motion_partitions_enabled_flag instead of amp_enabled_flag in order to match with HM */
   1377     BITS_PARSE("asymmetric_motion_partitions_enabled_flag", value, ps_bitstrm, 1);
   1378     ps_sps->i1_amp_enabled_flag = value;
   1379 
   1380     BITS_PARSE("sample_adaptive_offset_enabled_flag", value, ps_bitstrm, 1);
   1381     ps_sps->i1_sample_adaptive_offset_enabled_flag = value;
   1382 
   1383     BITS_PARSE("pcm_enabled_flag", value, ps_bitstrm, 1);
   1384     ps_sps->i1_pcm_enabled_flag = value;
   1385 
   1386     if(ps_sps->i1_pcm_enabled_flag)
   1387     {
   1388         BITS_PARSE("pcm_sample_bit_depth_luma", value, ps_bitstrm, 4);
   1389         ps_sps->i1_pcm_sample_bit_depth_luma = value + 1;
   1390 
   1391         BITS_PARSE("pcm_sample_bit_depth_chroma", value, ps_bitstrm, 4);
   1392         ps_sps->i1_pcm_sample_bit_depth_chroma = value + 1;
   1393 
   1394         UEV_PARSE("log2_min_pcm_coding_block_size_minus3", value, ps_bitstrm);
   1395         ps_sps->i1_log2_min_pcm_coding_block_size = value + 3;
   1396 
   1397         UEV_PARSE("log2_diff_max_min_pcm_coding_block_size", value, ps_bitstrm);
   1398         ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = value;
   1399         BITS_PARSE("pcm_loop_filter_disable_flag", value, ps_bitstrm, 1);
   1400         ps_sps->i1_pcm_loop_filter_disable_flag = value;
   1401 
   1402     }
   1403     UEV_PARSE("num_short_term_ref_pic_sets", value, ps_bitstrm);
   1404     ps_sps->i1_num_short_term_ref_pic_sets = value;
   1405 
   1406     ps_sps->i1_num_short_term_ref_pic_sets = CLIP3(ps_sps->i1_num_short_term_ref_pic_sets, 0, MAX_STREF_PICS_SPS);
   1407 
   1408     for(i = 0; i < ps_sps->i1_num_short_term_ref_pic_sets; i++)
   1409         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]);
   1410 
   1411     BITS_PARSE("long_term_ref_pics_present_flag", value, ps_bitstrm, 1);
   1412     ps_sps->i1_long_term_ref_pics_present_flag = value;
   1413 
   1414     if(ps_sps->i1_long_term_ref_pics_present_flag)
   1415     {
   1416         UEV_PARSE("num_long_term_ref_pics_sps", value, ps_bitstrm);
   1417         ps_sps->i1_num_long_term_ref_pics_sps = value;
   1418 
   1419         for(i = 0; i < ps_sps->i1_num_long_term_ref_pics_sps; i++)
   1420         {
   1421             BITS_PARSE("lt_ref_pic_poc_lsb_sps[ i ]", value, ps_bitstrm, ps_sps->i1_log2_max_pic_order_cnt_lsb);
   1422             ps_sps->ai1_lt_ref_pic_poc_lsb_sps[i] = value;
   1423 
   1424             BITS_PARSE("used_by_curr_pic_lt_sps_flag[ i ]", value, ps_bitstrm, 1);
   1425             ps_sps->ai1_used_by_curr_pic_lt_sps_flag[i] = value;
   1426         }
   1427     }
   1428 
   1429     BITS_PARSE("sps_temporal_mvp_enable_flag", value, ps_bitstrm, 1);
   1430     ps_sps->i1_sps_temporal_mvp_enable_flag = value;
   1431 
   1432     /* Print matches HM 8-2 */
   1433     BITS_PARSE("sps_strong_intra_smoothing_enable_flag", value, ps_bitstrm, 1);
   1434     ps_sps->i1_strong_intra_smoothing_enable_flag = value;
   1435 
   1436     BITS_PARSE("vui_parameters_present_flag", value, ps_bitstrm, 1);
   1437     ps_sps->i1_vui_parameters_present_flag = value;
   1438 
   1439     if(ps_sps->i1_vui_parameters_present_flag)
   1440         ihevcd_parse_vui_parameters(ps_bitstrm,
   1441                                     &ps_sps->s_vui_parameters,
   1442                                     ps_sps->i1_sps_max_sub_layers - 1);
   1443 
   1444     BITS_PARSE("sps_extension_flag", value, ps_bitstrm, 1);
   1445 
   1446 
   1447     {
   1448         WORD32 numerator;
   1449         WORD32 ceil_offset;
   1450 
   1451         ceil_offset = (1 << ps_sps->i1_log2_ctb_size) - 1;
   1452         numerator = ps_sps->i2_pic_width_in_luma_samples;
   1453 
   1454         ps_sps->i2_pic_wd_in_ctb = ((numerator + ceil_offset) /
   1455                         (1 << ps_sps->i1_log2_ctb_size));
   1456 
   1457         numerator = ps_sps->i2_pic_height_in_luma_samples;
   1458         ps_sps->i2_pic_ht_in_ctb = ((numerator + ceil_offset) /
   1459                         (1 << ps_sps->i1_log2_ctb_size));
   1460 
   1461         ps_sps->i4_pic_size_in_ctb = ps_sps->i2_pic_ht_in_ctb *
   1462                         ps_sps->i2_pic_wd_in_ctb;
   1463 
   1464         if(0 == ps_codec->i4_sps_done)
   1465             ps_codec->s_parse.i4_next_ctb_indx = ps_sps->i4_pic_size_in_ctb;
   1466 
   1467         numerator = ps_sps->i2_pic_width_in_luma_samples;
   1468         ps_sps->i2_pic_wd_in_min_cb = numerator  /
   1469                         (1 << ps_sps->i1_log2_min_coding_block_size);
   1470 
   1471         numerator = ps_sps->i2_pic_height_in_luma_samples;
   1472         ps_sps->i2_pic_ht_in_min_cb = numerator  /
   1473                         (1 << ps_sps->i1_log2_min_coding_block_size);
   1474     }
   1475     if((0 != ps_codec->i4_first_pic_done) &&
   1476                     ((ps_codec->i4_wd != ps_sps->i2_pic_width_in_luma_samples) ||
   1477                     (ps_codec->i4_ht != ps_sps->i2_pic_height_in_luma_samples)))
   1478     {
   1479         ps_codec->i4_reset_flag = 1;
   1480         return (IHEVCD_ERROR_T)IVD_RES_CHANGED;
   1481     }
   1482 
   1483     /* Update display width and display height */
   1484     {
   1485         WORD32 disp_wd, disp_ht;
   1486         WORD32 crop_unit_x, crop_unit_y;
   1487         crop_unit_x = 1;
   1488         crop_unit_y = 1;
   1489 
   1490         if(CHROMA_FMT_IDC_YUV420 == ps_sps->i1_chroma_format_idc)
   1491         {
   1492             crop_unit_x = 2;
   1493             crop_unit_y = 2;
   1494         }
   1495 
   1496         disp_wd = ps_sps->i2_pic_width_in_luma_samples;
   1497         disp_wd -= ps_sps->i2_pic_crop_left_offset * crop_unit_x;
   1498         disp_wd -= ps_sps->i2_pic_crop_right_offset * crop_unit_x;
   1499 
   1500 
   1501         disp_ht = ps_sps->i2_pic_height_in_luma_samples;
   1502         disp_ht -= ps_sps->i2_pic_crop_top_offset * crop_unit_y;
   1503         disp_ht -= ps_sps->i2_pic_crop_bottom_offset * crop_unit_y;
   1504 
   1505         if((0 >= disp_wd) || (0 >= disp_ht))
   1506             return IHEVCD_INVALID_PARAMETER;
   1507 
   1508         ps_codec->i4_disp_wd = disp_wd;
   1509         ps_codec->i4_disp_ht = disp_ht;
   1510 
   1511 
   1512         ps_codec->i4_wd = ps_sps->i2_pic_width_in_luma_samples;
   1513         ps_codec->i4_ht = ps_sps->i2_pic_height_in_luma_samples;
   1514 
   1515         {
   1516             WORD32 ref_strd;
   1517             ref_strd = ALIGN32(ps_sps->i2_pic_width_in_luma_samples + PAD_WD);
   1518             if(ps_codec->i4_strd < ref_strd)
   1519             {
   1520                 ps_codec->i4_strd = ref_strd;
   1521             }
   1522         }
   1523 
   1524         if(0 == ps_codec->i4_share_disp_buf)
   1525         {
   1526             if(ps_codec->i4_disp_strd < ps_codec->i4_disp_wd)
   1527             {
   1528                 ps_codec->i4_disp_strd = ps_codec->i4_disp_wd;
   1529             }
   1530         }
   1531         else
   1532         {
   1533             if(ps_codec->i4_disp_strd < ps_codec->i4_strd)
   1534             {
   1535                 ps_codec->i4_disp_strd = ps_codec->i4_strd;
   1536             }
   1537         }
   1538     }
   1539 
   1540     ps_codec->i4_sps_done = 1;
   1541     return ret;
   1542 }
   1543 
   1544 
   1545 void ihevcd_unmark_pps(codec_t *ps_codec, WORD32 sps_id)
   1546 {
   1547     WORD32 pps_id = 0;
   1548     pps_t *ps_pps = ps_codec->ps_pps_base;
   1549 
   1550     for(pps_id = 0; pps_id < MAX_PPS_CNT - 1; pps_id++, ps_pps++)
   1551     {
   1552         if((ps_pps->i1_pps_valid) &&
   1553                         (ps_pps->i1_sps_id == sps_id))
   1554             ps_pps->i1_pps_valid = 0;
   1555     }
   1556 }
   1557 
   1558 
   1559 void ihevcd_copy_sps(codec_t *ps_codec, WORD32 sps_id, WORD32 sps_id_ref)
   1560 {
   1561     sps_t *ps_sps, *ps_sps_ref;
   1562     WORD16 *pi2_scaling_mat_backup;
   1563     WORD32 scaling_mat_size;
   1564 
   1565     SCALING_MAT_SIZE(scaling_mat_size);
   1566     ps_sps_ref = ps_codec->ps_sps_base + sps_id_ref;
   1567     ps_sps = ps_codec->ps_sps_base + sps_id;
   1568 
   1569     if(ps_sps->i1_sps_valid)
   1570     {
   1571         if((ps_sps->i1_log2_ctb_size != ps_sps_ref->i1_log2_ctb_size) ||
   1572                         (ps_sps->i2_pic_wd_in_ctb != ps_sps_ref->i2_pic_wd_in_ctb) ||
   1573                         (ps_sps->i2_pic_ht_in_ctb != ps_sps_ref->i2_pic_ht_in_ctb))
   1574         {
   1575             ihevcd_unmark_pps(ps_codec, sps_id);
   1576         }
   1577     }
   1578 
   1579     pi2_scaling_mat_backup = ps_sps->pi2_scaling_mat;
   1580 
   1581     memcpy(ps_sps, ps_sps_ref, sizeof(sps_t));
   1582     ps_sps->pi2_scaling_mat = pi2_scaling_mat_backup;
   1583     memcpy(ps_sps->pi2_scaling_mat, ps_sps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
   1584     ps_sps->i1_sps_valid = 1;
   1585 
   1586     ps_codec->s_parse.ps_sps = ps_sps;
   1587 }
   1588 
   1589 
   1590 /**
   1591 *******************************************************************************
   1592 *
   1593 * @brief
   1594 *  Parses PPS (Picture Parameter Set)
   1595 *
   1596 * @par Description:
   1597 *  Parse Picture Parameter Set as per section  Section: 7.3.2.3
   1598 * The pps is written to a temporary buffer and copied later to the
   1599 * appropriate location
   1600 *
   1601 * @param[in] ps_codec
   1602 *  Pointer to codec context
   1603 *
   1604 * @returns Error code from IHEVCD_ERROR_T
   1605 *
   1606 * @remarks
   1607 *
   1608 *
   1609 *******************************************************************************
   1610 */
   1611 IHEVCD_ERROR_T ihevcd_parse_pps(codec_t *ps_codec)
   1612 {
   1613     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   1614     WORD32 value;
   1615     WORD32 pps_id;
   1616 
   1617     pps_t *ps_pps;
   1618     sps_t *ps_sps;
   1619     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
   1620 
   1621 
   1622     if(0 == ps_codec->i4_sps_done)
   1623         return IHEVCD_INVALID_HEADER;
   1624 
   1625     UEV_PARSE("pic_parameter_set_id", value, ps_bitstrm);
   1626 
   1627     pps_id = value;
   1628     if((pps_id >= MAX_PPS_CNT) || (pps_id < 0))
   1629     {
   1630         if(ps_codec->i4_pps_done)
   1631             return IHEVCD_UNSUPPORTED_PPS_ID;
   1632         else
   1633             pps_id = 0;
   1634     }
   1635 
   1636 
   1637     ps_pps = (ps_codec->s_parse.ps_pps_base + MAX_PPS_CNT - 1);
   1638 
   1639     ps_pps->i1_pps_id = pps_id;
   1640 
   1641     UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
   1642     ps_pps->i1_sps_id = value;
   1643     ps_pps->i1_sps_id = CLIP3(ps_pps->i1_sps_id, 0, MAX_SPS_CNT - 2);
   1644 
   1645     ps_sps = (ps_codec->s_parse.ps_sps_base + ps_pps->i1_sps_id);
   1646 
   1647     /* If the SPS that is being referred to has not been parsed,
   1648      * copy an existing SPS to the current location */
   1649     if(0 == ps_sps->i1_sps_valid)
   1650     {
   1651         return IHEVCD_INVALID_HEADER;
   1652 
   1653 /*
   1654         sps_t *ps_sps_ref = ps_codec->ps_sps_base;
   1655         while(0 == ps_sps_ref->i1_sps_valid)
   1656             ps_sps_ref++;
   1657         ihevcd_copy_sps(ps_codec, ps_pps->i1_sps_id, ps_sps_ref->i1_sps_id);
   1658 */
   1659     }
   1660 
   1661     BITS_PARSE("dependent_slices_enabled_flag", value, ps_bitstrm, 1);
   1662     ps_pps->i1_dependent_slice_enabled_flag = value;
   1663 
   1664     BITS_PARSE("output_flag_present_flag", value, ps_bitstrm, 1);
   1665     ps_pps->i1_output_flag_present_flag = value;
   1666 
   1667     BITS_PARSE("num_extra_slice_header_bits", value, ps_bitstrm, 3);
   1668     ps_pps->i1_num_extra_slice_header_bits = value;
   1669 
   1670 
   1671     BITS_PARSE("sign_data_hiding_flag", value, ps_bitstrm, 1);
   1672     ps_pps->i1_sign_data_hiding_flag = value;
   1673 
   1674     BITS_PARSE("cabac_init_present_flag", value, ps_bitstrm, 1);
   1675     ps_pps->i1_cabac_init_present_flag = value;
   1676 
   1677     UEV_PARSE("num_ref_idx_l0_default_active_minus1", value, ps_bitstrm);
   1678     ps_pps->i1_num_ref_idx_l0_default_active = value + 1;
   1679 
   1680     UEV_PARSE("num_ref_idx_l1_default_active_minus1", value, ps_bitstrm);
   1681     ps_pps->i1_num_ref_idx_l1_default_active = value + 1;
   1682 
   1683     SEV_PARSE("pic_init_qp_minus26", value, ps_bitstrm);
   1684     ps_pps->i1_pic_init_qp = value + 26;
   1685 
   1686     BITS_PARSE("constrained_intra_pred_flag", value, ps_bitstrm, 1);
   1687     ps_pps->i1_constrained_intra_pred_flag = value;
   1688 
   1689     BITS_PARSE("transform_skip_enabled_flag", value, ps_bitstrm, 1);
   1690     ps_pps->i1_transform_skip_enabled_flag = value;
   1691 
   1692     BITS_PARSE("cu_qp_delta_enabled_flag", value, ps_bitstrm, 1);
   1693     ps_pps->i1_cu_qp_delta_enabled_flag = value;
   1694 
   1695     if(ps_pps->i1_cu_qp_delta_enabled_flag)
   1696     {
   1697         UEV_PARSE("diff_cu_qp_delta_depth", value, ps_bitstrm);
   1698         ps_pps->i1_diff_cu_qp_delta_depth = value;
   1699     }
   1700     else
   1701     {
   1702         ps_pps->i1_diff_cu_qp_delta_depth = 0;
   1703     }
   1704     ps_pps->i1_log2_min_cu_qp_delta_size = ps_sps->i1_log2_ctb_size - ps_pps->i1_diff_cu_qp_delta_depth;
   1705     /* Print different */
   1706     SEV_PARSE("cb_qp_offset", value, ps_bitstrm);
   1707     ps_pps->i1_pic_cb_qp_offset = value;
   1708 
   1709     /* Print different */
   1710     SEV_PARSE("cr_qp_offset", value, ps_bitstrm);
   1711     ps_pps->i1_pic_cr_qp_offset = value;
   1712 
   1713     /* Print different */
   1714     BITS_PARSE("slicelevel_chroma_qp_flag", value, ps_bitstrm, 1);
   1715     ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag = value;
   1716 
   1717     BITS_PARSE("weighted_pred_flag", value, ps_bitstrm, 1);
   1718     ps_pps->i1_weighted_pred_flag = value;
   1719 
   1720     BITS_PARSE("weighted_bipred_flag", value, ps_bitstrm, 1);
   1721     ps_pps->i1_weighted_bipred_flag = value;
   1722 
   1723     BITS_PARSE("transquant_bypass_enable_flag", value, ps_bitstrm, 1);
   1724     ps_pps->i1_transquant_bypass_enable_flag = value;
   1725 
   1726     BITS_PARSE("tiles_enabled_flag", value, ps_bitstrm, 1);
   1727     ps_pps->i1_tiles_enabled_flag = value;
   1728 
   1729     BITS_PARSE("entropy_coding_sync_enabled_flag", value, ps_bitstrm, 1);
   1730     ps_pps->i1_entropy_coding_sync_enabled_flag = value;
   1731 
   1732     ps_pps->i1_loop_filter_across_tiles_enabled_flag = 0;
   1733     if(ps_pps->i1_tiles_enabled_flag)
   1734     {
   1735         UEV_PARSE("num_tile_columns_minus1", value, ps_bitstrm);
   1736         ps_pps->i1_num_tile_columns = value + 1;
   1737 
   1738         UEV_PARSE("num_tile_rows_minus1", value, ps_bitstrm);
   1739         ps_pps->i1_num_tile_rows = value + 1;
   1740 
   1741         if((ps_pps->i1_num_tile_columns < 1) ||
   1742                         (ps_pps->i1_num_tile_columns > ps_sps->i2_pic_wd_in_ctb) ||
   1743                         (ps_pps->i1_num_tile_rows < 1) ||
   1744                         (ps_pps->i1_num_tile_rows > ps_sps->i2_pic_ht_in_ctb))
   1745             return IHEVCD_INVALID_HEADER;
   1746 
   1747         BITS_PARSE("uniform_spacing_flag", value, ps_bitstrm, 1);
   1748         ps_pps->i1_uniform_spacing_flag = value;
   1749 
   1750 
   1751         {
   1752 
   1753             WORD32 start;
   1754             WORD32 i, j;
   1755 
   1756 
   1757             start = 0;
   1758             for(i = 0; i < ps_pps->i1_num_tile_columns; i++)
   1759             {
   1760                 tile_t *ps_tile;
   1761                 if(!ps_pps->i1_uniform_spacing_flag)
   1762                 {
   1763                     if(i < (ps_pps->i1_num_tile_columns - 1))
   1764                     {
   1765                         UEV_PARSE("column_width_minus1[ i ]", value, ps_bitstrm);
   1766                         value += 1;
   1767                     }
   1768                     else
   1769                     {
   1770                         value = ps_sps->i2_pic_wd_in_ctb - start;
   1771                     }
   1772                 }
   1773                 else
   1774                 {
   1775                     value = ((i + 1) * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns -
   1776                                     (i * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns;
   1777                 }
   1778 
   1779                 for(j = 0; j < ps_pps->i1_num_tile_rows; j++)
   1780                 {
   1781                     ps_tile = ps_pps->ps_tile + j * ps_pps->i1_num_tile_columns + i;
   1782                     ps_tile->u1_pos_x = start;
   1783                     ps_tile->u2_wd = value;
   1784                 }
   1785                 start += value;
   1786 
   1787                 if((start > ps_sps->i2_pic_wd_in_ctb) ||
   1788                                 (value <= 0))
   1789                     return IHEVCD_INVALID_HEADER;
   1790             }
   1791 
   1792             start = 0;
   1793             for(i = 0; i < (ps_pps->i1_num_tile_rows); i++)
   1794             {
   1795                 tile_t *ps_tile;
   1796                 if(!ps_pps->i1_uniform_spacing_flag)
   1797                 {
   1798                     if(i < (ps_pps->i1_num_tile_rows - 1))
   1799                     {
   1800 
   1801                         UEV_PARSE("row_height_minus1[ i ]", value, ps_bitstrm);
   1802                         value += 1;
   1803                     }
   1804                     else
   1805                     {
   1806                         value = ps_sps->i2_pic_ht_in_ctb - start;
   1807                     }
   1808                 }
   1809                 else
   1810                 {
   1811                     value = ((i + 1) * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows -
   1812                                     (i * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows;
   1813                 }
   1814 
   1815                 for(j = 0; j < ps_pps->i1_num_tile_columns; j++)
   1816                 {
   1817                     ps_tile = ps_pps->ps_tile + i * ps_pps->i1_num_tile_columns + j;
   1818                     ps_tile->u1_pos_y = start;
   1819                     ps_tile->u2_ht = value;
   1820                 }
   1821                 start += value;
   1822 
   1823                 if((start > ps_sps->i2_pic_ht_in_ctb) ||
   1824                                 (value <= 0))
   1825                     return IHEVCD_INVALID_HEADER;
   1826             }
   1827         }
   1828 
   1829 
   1830         BITS_PARSE("loop_filter_across_tiles_enabled_flag", value, ps_bitstrm, 1);
   1831         ps_pps->i1_loop_filter_across_tiles_enabled_flag = value;
   1832 
   1833     }
   1834     else
   1835     {
   1836         /* If tiles are not present, set first tile in each PPS to have tile
   1837         width and height equal to picture width and height */
   1838         ps_pps->i1_num_tile_columns = 1;
   1839         ps_pps->i1_num_tile_rows = 1;
   1840         ps_pps->i1_uniform_spacing_flag = 1;
   1841 
   1842         ps_pps->ps_tile->u1_pos_x = 0;
   1843         ps_pps->ps_tile->u1_pos_y = 0;
   1844         ps_pps->ps_tile->u2_wd = ps_sps->i2_pic_wd_in_ctb;
   1845         ps_pps->ps_tile->u2_ht = ps_sps->i2_pic_ht_in_ctb;
   1846     }
   1847 
   1848     BITS_PARSE("loop_filter_across_slices_enabled_flag", value, ps_bitstrm, 1);
   1849     ps_pps->i1_loop_filter_across_slices_enabled_flag = value;
   1850 
   1851     BITS_PARSE("deblocking_filter_control_present_flag", value, ps_bitstrm, 1);
   1852     ps_pps->i1_deblocking_filter_control_present_flag = value;
   1853 
   1854     /* Default values */
   1855     ps_pps->i1_pic_disable_deblocking_filter_flag = 0;
   1856     ps_pps->i1_deblocking_filter_override_enabled_flag = 0;
   1857     ps_pps->i1_beta_offset_div2 = 0;
   1858     ps_pps->i1_tc_offset_div2 = 0;
   1859 
   1860     if(ps_pps->i1_deblocking_filter_control_present_flag)
   1861     {
   1862 
   1863         BITS_PARSE("deblocking_filter_override_enabled_flag", value, ps_bitstrm, 1);
   1864         ps_pps->i1_deblocking_filter_override_enabled_flag = value;
   1865 
   1866         BITS_PARSE("pic_disable_deblocking_filter_flag", value, ps_bitstrm, 1);
   1867         ps_pps->i1_pic_disable_deblocking_filter_flag = value;
   1868 
   1869         if(!ps_pps->i1_pic_disable_deblocking_filter_flag)
   1870         {
   1871 
   1872             SEV_PARSE("pps_beta_offset_div2", value, ps_bitstrm);
   1873             ps_pps->i1_beta_offset_div2 = value;
   1874 
   1875             SEV_PARSE("pps_tc_offset_div2", value, ps_bitstrm);
   1876             ps_pps->i1_tc_offset_div2 = value;
   1877 
   1878         }
   1879     }
   1880 
   1881     BITS_PARSE("pps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
   1882     ps_pps->i1_pps_scaling_list_data_present_flag = value;
   1883 
   1884     if(ps_pps->i1_pps_scaling_list_data_present_flag)
   1885     {
   1886         COPY_DEFAULT_SCALING_LIST(ps_pps->pi2_scaling_mat);
   1887         ihevcd_scaling_list_data(ps_codec, ps_pps->pi2_scaling_mat);
   1888     }
   1889 
   1890     BITS_PARSE("lists_modification_present_flag", value, ps_bitstrm, 1);
   1891     ps_pps->i1_lists_modification_present_flag = value;
   1892     UEV_PARSE("log2_parallel_merge_level_minus2", value, ps_bitstrm);
   1893     ps_pps->i1_log2_parallel_merge_level = value + 2;
   1894 
   1895     BITS_PARSE("slice_header_extension_present_flag", value, ps_bitstrm, 1);
   1896     ps_pps->i1_slice_header_extension_present_flag = value;
   1897     /* Not present in HM */
   1898     BITS_PARSE("pps_extension_flag", value, ps_bitstrm, 1);
   1899 
   1900     ps_codec->i4_pps_done = 1;
   1901     return ret;
   1902 }
   1903 
   1904 
   1905 void ihevcd_copy_pps(codec_t *ps_codec, WORD32 pps_id, WORD32 pps_id_ref)
   1906 {
   1907     pps_t *ps_pps, *ps_pps_ref;
   1908     WORD16 *pi2_scaling_mat_backup;
   1909     WORD32 scaling_mat_size;
   1910     tile_t *ps_tile_backup;
   1911     WORD32 max_tile_cols, max_tile_rows;
   1912     WORD32 wd, ht;
   1913     wd = ALIGN64(ps_codec->i4_wd);
   1914     ht = ALIGN64(ps_codec->i4_ht);
   1915 
   1916     SCALING_MAT_SIZE(scaling_mat_size);
   1917     max_tile_cols = (wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
   1918     max_tile_rows = (ht + MIN_TILE_HT - 1) / MIN_TILE_HT;
   1919 
   1920     ps_pps_ref = ps_codec->ps_pps_base + pps_id_ref;
   1921     ps_pps = ps_codec->ps_pps_base + pps_id;
   1922 
   1923     pi2_scaling_mat_backup = ps_pps->pi2_scaling_mat;
   1924     ps_tile_backup = ps_pps->ps_tile;
   1925 
   1926     memcpy(ps_pps, ps_pps_ref, sizeof(pps_t));
   1927     ps_pps->pi2_scaling_mat = pi2_scaling_mat_backup;
   1928     ps_pps->ps_tile = ps_tile_backup;
   1929     memcpy(ps_pps->pi2_scaling_mat, ps_pps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
   1930     memcpy(ps_pps->ps_tile, ps_pps_ref->ps_tile, max_tile_cols * max_tile_rows * sizeof(tile_t));
   1931 
   1932     ps_pps->i1_pps_valid = 1;
   1933 
   1934     ps_codec->s_parse.ps_pps = ps_pps;
   1935 }
   1936 
   1937 
   1938 
   1939 /**
   1940 *******************************************************************************
   1941 *
   1942 * @brief
   1943 *  Parses SEI (Supplemental Enhancement Information)
   1944 *
   1945 * @par Description:
   1946 *  Parses SEI (Supplemental Enhancement Information) as per Section: 7.3.7
   1947 *
   1948 * @param[in] ps_codec
   1949 *  Pointer to codec context
   1950 *
   1951 * @returns Error code from IHEVCD_ERROR_T
   1952 *
   1953 * @remarks
   1954 *
   1955 *
   1956 *******************************************************************************
   1957 */
   1958 IHEVCD_ERROR_T ihevcd_parse_sei(codec_t *ps_codec)
   1959 {
   1960     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   1961     UNUSED(ps_codec);
   1962     return ret;
   1963 }
   1964 
   1965 /**
   1966 *******************************************************************************
   1967 *
   1968 * @brief
   1969 *  Parses Access unit delimiter
   1970 *
   1971 * @par Description:
   1972 *  Parses Access unit delimiter as per section  Section: 7.3.2.5
   1973 *
   1974 * @param[in] ps_codec
   1975 *  Pointer to codec context
   1976 *
   1977 * @returns Error code from IHEVCD_ERROR_T
   1978 *
   1979 * @remarks
   1980 *
   1981 *
   1982 *******************************************************************************
   1983 */
   1984 WORD32 ihevcd_parse_aud(codec_t *ps_codec)
   1985 {
   1986     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   1987     UNUSED(ps_codec);
   1988     return ret;
   1989 }
   1990 
   1991 WORD32 ihevcd_extend_sign_bit(WORD32 value, WORD32 num_bits)
   1992 {
   1993     WORD32 ret_value = value;
   1994     if(value >> (num_bits - 1))
   1995     {
   1996         ret_value |= (0xFFFFFFFF << num_bits);
   1997     }
   1998     return ret_value;
   1999 }
   2000 
   2001 /**
   2002 *******************************************************************************
   2003 *
   2004 * @brief
   2005 *  Calculate POC of the current slice
   2006 *
   2007 * @par Description:
   2008 *  Calculates the current POC using the previous POC lsb and previous POC msb
   2009 *
   2010 * @param[in] ps_codec
   2011 *  Pointer to codec context
   2012 *
   2013 * @param[in] i1_pic_order_cnt_lsb
   2014 *  Current POC lsb
   2015 *
   2016 * @returns  Current absolute POC
   2017 *
   2018 * @remarks
   2019 *
   2020 *
   2021 *******************************************************************************
   2022 */
   2023 
   2024 WORD32 ihevcd_calc_poc(codec_t *ps_codec, nal_header_t *ps_nal, WORD8 i1_log2_max_poc_lsb, WORD32 i2_poc_lsb)
   2025 {
   2026     WORD32 i4_abs_poc, i4_poc_msb;
   2027     WORD32 max_poc_lsb;
   2028     WORD8 i1_nal_unit_type = ps_nal->i1_nal_unit_type;
   2029     max_poc_lsb = (1 << i1_log2_max_poc_lsb);
   2030 
   2031     if((!ps_codec->i4_first_pic_done) && (!ps_codec->i4_pic_present))
   2032         ps_codec->i4_prev_poc_msb = -2 * max_poc_lsb;
   2033 
   2034     if(NAL_IDR_N_LP == i1_nal_unit_type
   2035                     || NAL_IDR_W_LP == i1_nal_unit_type
   2036                     || NAL_BLA_N_LP == i1_nal_unit_type
   2037                     || NAL_BLA_W_DLP == i1_nal_unit_type
   2038                     || NAL_BLA_W_LP == i1_nal_unit_type
   2039                     || (NAL_CRA == i1_nal_unit_type && !ps_codec->i4_first_pic_done))
   2040     {
   2041         i4_poc_msb = ps_codec->i4_prev_poc_msb + 2 * max_poc_lsb;
   2042         ps_codec->i4_prev_poc_lsb = 0;
   2043         ps_codec->i4_max_prev_poc_lsb = 0;
   2044 //        ps_codec->i4_prev_poc_msb = 0;
   2045     }
   2046     else
   2047     {
   2048 
   2049         if((i2_poc_lsb < ps_codec->i4_prev_poc_lsb)
   2050                         && ((ps_codec->i4_prev_poc_lsb - i2_poc_lsb) >= max_poc_lsb / 2))
   2051         {
   2052             i4_poc_msb = ps_codec->i4_prev_poc_msb + max_poc_lsb;
   2053         }
   2054         else if((i2_poc_lsb > ps_codec->i4_prev_poc_lsb)
   2055                         && ((i2_poc_lsb - ps_codec->i4_prev_poc_lsb) > max_poc_lsb / 2))
   2056         {
   2057             i4_poc_msb = ps_codec->i4_prev_poc_msb - max_poc_lsb;
   2058         }
   2059         else
   2060         {
   2061             i4_poc_msb = ps_codec->i4_prev_poc_msb;
   2062         }
   2063 
   2064 
   2065     }
   2066 
   2067     i4_abs_poc = i4_poc_msb + i2_poc_lsb;
   2068     ps_codec->i4_max_prev_poc_lsb = MAX(ps_codec->i4_max_prev_poc_lsb, i2_poc_lsb);
   2069 
   2070     {
   2071         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));
   2072         WORD32 update_prev_poc = ((is_reference_nal) && ((i1_nal_unit_type < NAL_RADL_N) || (i1_nal_unit_type > NAL_RASL_R)));
   2073 
   2074         if((0 == ps_nal->i1_nuh_temporal_id) &&
   2075                         (update_prev_poc))
   2076         {
   2077             ps_codec->i4_prev_poc_lsb = i2_poc_lsb;
   2078             ps_codec->i4_prev_poc_msb = i4_poc_msb;
   2079         }
   2080     }
   2081 
   2082     return i4_abs_poc;
   2083 }
   2084 
   2085 
   2086 void ihevcd_copy_slice_hdr(codec_t *ps_codec, WORD32 slice_idx, WORD32 slice_idx_ref)
   2087 {
   2088     slice_header_t *ps_slice_hdr, *ps_slice_hdr_ref;
   2089     WORD32 *pu4_entry_offset_backup;
   2090 
   2091     ps_slice_hdr = ps_codec->s_parse.ps_slice_hdr_base + slice_idx;
   2092     ps_slice_hdr_ref = ps_codec->s_parse.ps_slice_hdr_base + slice_idx_ref;
   2093 
   2094     pu4_entry_offset_backup = ps_slice_hdr->pu4_entry_point_offset;
   2095     memcpy(ps_slice_hdr, ps_slice_hdr_ref, sizeof(slice_header_t));
   2096     ps_slice_hdr->pu4_entry_point_offset = pu4_entry_offset_backup;
   2097 }
   2098 
   2099 
   2100 
   2101