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     if(ps_vui->u1_video_signal_type_present_flag)
    695     {
    696         BITS_PARSE("video_format", ps_vui->u1_video_format, ps_bitstrm, 3);
    697         BITS_PARSE("video_full_range_flag", ps_vui->u1_video_full_range_flag, ps_bitstrm, 1);
    698         BITS_PARSE("colour_description_present_flag", ps_vui->u1_colour_description_present_flag, ps_bitstrm, 1);
    699         ps_vui->u1_colour_primaries = 2;
    700         ps_vui->u1_transfer_characteristics = 2;
    701         if(ps_vui->u1_colour_description_present_flag)
    702         {
    703             BITS_PARSE("colour_primaries", ps_vui->u1_colour_primaries, ps_bitstrm, 8);
    704             BITS_PARSE("transfer_characteristics", ps_vui->u1_transfer_characteristics, ps_bitstrm, 8);
    705             BITS_PARSE("matrix_coeffs", ps_vui->u1_matrix_coefficients, ps_bitstrm, 8);
    706         }
    707     }
    708 
    709     BITS_PARSE("chroma_loc_info_present_flag", ps_vui->u1_chroma_loc_info_present_flag, ps_bitstrm, 1);
    710     ps_vui->u1_chroma_sample_loc_type_top_field = 0;
    711     ps_vui->u1_chroma_sample_loc_type_bottom_field = 0;
    712     if(ps_vui->u1_chroma_loc_info_present_flag)
    713     {
    714         UEV_PARSE("chroma_sample_loc_type_top_field", ps_vui->u1_chroma_sample_loc_type_top_field, ps_bitstrm);
    715         UEV_PARSE("chroma_sample_loc_type_bottom_field", ps_vui->u1_chroma_sample_loc_type_bottom_field, ps_bitstrm);
    716     }
    717 
    718     BITS_PARSE("neutral_chroma_indication_flag", ps_vui->u1_neutral_chroma_indication_flag, ps_bitstrm, 1);
    719     BITS_PARSE("field_seq_flag", ps_vui->u1_field_seq_flag, ps_bitstrm, 1);
    720     BITS_PARSE("frame_field_info_present_flag", ps_vui->u1_frame_field_info_present_flag, ps_bitstrm, 1);
    721     BITS_PARSE("default_display_window_flag", ps_vui->u1_default_display_window_flag, ps_bitstrm, 1);
    722     ps_vui->u4_def_disp_win_left_offset = 0;
    723     ps_vui->u4_def_disp_win_right_offset = 0;
    724     ps_vui->u4_def_disp_win_top_offset = 0;
    725     ps_vui->u4_def_disp_win_bottom_offset = 0;
    726     if(ps_vui->u1_default_display_window_flag)
    727     {
    728         UEV_PARSE("def_disp_win_left_offset", ps_vui->u4_def_disp_win_left_offset, ps_bitstrm);
    729         UEV_PARSE("def_disp_win_right_offset", ps_vui->u4_def_disp_win_right_offset, ps_bitstrm);
    730         UEV_PARSE("def_disp_win_top_offset", ps_vui->u4_def_disp_win_top_offset, ps_bitstrm);
    731         UEV_PARSE("def_disp_win_bottom_offset", ps_vui->u4_def_disp_win_bottom_offset, ps_bitstrm);
    732     }
    733 
    734     BITS_PARSE("vui_timing_info_present_flag", ps_vui->u1_vui_timing_info_present_flag, ps_bitstrm, 1);
    735     if(ps_vui->u1_vui_timing_info_present_flag)
    736     {
    737         BITS_PARSE("vui_num_units_in_tick", ps_vui->u4_vui_num_units_in_tick, ps_bitstrm, 32);
    738         BITS_PARSE("vui_time_scale", ps_vui->u4_vui_time_scale, ps_bitstrm, 32);
    739         BITS_PARSE("vui_poc_proportional_to_timing_flag", ps_vui->u1_poc_proportional_to_timing_flag, ps_bitstrm, 1);
    740         if(ps_vui->u1_poc_proportional_to_timing_flag)
    741             UEV_PARSE("vui_num_ticks_poc_diff_one_minus1", ps_vui->u1_num_ticks_poc_diff_one_minus1, ps_bitstrm);
    742 
    743         BITS_PARSE("vui_hrd_parameters_present_flag", ps_vui->u1_vui_hrd_parameters_present_flag, ps_bitstrm, 1);
    744         if(ps_vui->u1_vui_hrd_parameters_present_flag)
    745             ihevcd_parse_hrd_parameters(ps_bitstrm, &ps_vui->s_vui_hrd_parameters, 1, sps_max_sub_layers_minus1);
    746     }
    747 
    748     BITS_PARSE("bitstream_restriction_flag", ps_vui->u1_bitstream_restriction_flag, ps_bitstrm, 1);
    749     ps_vui->u1_tiles_fixed_structure_flag = 0;
    750     ps_vui->u1_motion_vectors_over_pic_boundaries_flag = 1;
    751     ps_vui->u1_restricted_ref_pic_lists_flag = 0;
    752     ps_vui->u4_min_spatial_segmentation_idc = 0;
    753     ps_vui->u1_max_bytes_per_pic_denom = 2;
    754     ps_vui->u1_max_bits_per_mincu_denom = 1;
    755     ps_vui->u1_log2_max_mv_length_horizontal = 15;
    756     ps_vui->u1_log2_max_mv_length_vertical = 15;
    757     if(ps_vui->u1_bitstream_restriction_flag)
    758     {
    759         BITS_PARSE("tiles_fixed_structure_flag", ps_vui->u1_tiles_fixed_structure_flag, ps_bitstrm, 1);
    760         BITS_PARSE("motion_vectors_over_pic_boundaries_flag", ps_vui->u1_motion_vectors_over_pic_boundaries_flag, ps_bitstrm, 1);
    761         BITS_PARSE("restricted_ref_pic_lists_flag", ps_vui->u1_restricted_ref_pic_lists_flag, ps_bitstrm, 1);
    762 
    763         UEV_PARSE("min_spatial_segmentation_idc", ps_vui->u4_min_spatial_segmentation_idc, ps_bitstrm);
    764         UEV_PARSE("max_bytes_per_pic_denom", ps_vui->u1_max_bytes_per_pic_denom, ps_bitstrm);
    765         UEV_PARSE("max_bits_per_min_cu_denom", ps_vui->u1_max_bits_per_mincu_denom, ps_bitstrm);
    766         UEV_PARSE("log2_max_mv_length_horizontal", ps_vui->u1_log2_max_mv_length_horizontal, ps_bitstrm);
    767         UEV_PARSE("log2_max_mv_length_vertical", ps_vui->u1_log2_max_mv_length_vertical, ps_bitstrm);
    768     }
    769 
    770     return ret;
    771 }
    772 
    773 /**
    774 *******************************************************************************
    775 *
    776 * @brief
    777 *  Parses profile tier and level info for either general layer of sub_layer
    778 *
    779 * @par   Description
    780 *  Parses profile tier and level info for either general layer of sub_layer
    781 * as per section 7.3.3
    782 *
    783 * Since the same function is called for parsing general_profile and
    784 * sub_layer_profile etc, variables do not specify whether the syntax is
    785 * for general or sub_layer. Similarly trace functions also do not differentiate
    786 *
    787 * @param[in] ps_bitstrm
    788 *  Pointer to bitstream structure
    789 *
    790 * @param[out] ps_ptl
    791 *  Pointer to profile, tier level structure
    792 *
    793 * @returns Error code from IHEVCD_ERROR_T
    794 *
    795 * @remarks
    796 *
    797 *******************************************************************************
    798 */
    799 
    800 static IHEVCD_ERROR_T ihevcd_parse_profile_tier_level_layer(bitstrm_t *ps_bitstrm,
    801                                                             profile_tier_lvl_t *ps_ptl)
    802 {
    803     WORD32 value;
    804     WORD32 i;
    805     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    806 
    807     BITS_PARSE("XXX_profile_space[]", value, ps_bitstrm, 2);
    808     ps_ptl->i1_profile_space = value;
    809 
    810     BITS_PARSE("XXX_tier_flag[]", value, ps_bitstrm, 1);
    811     ps_ptl->i1_tier_flag = value;
    812 
    813     BITS_PARSE("XXX_profile_idc[]", value, ps_bitstrm, 5);
    814     ps_ptl->i1_profile_idc = value;
    815 
    816     for(i = 0; i < MAX_PROFILE_COMPATBLTY; i++)
    817     {
    818         BITS_PARSE("XXX_profile_compatibility_flag[][j]", value, ps_bitstrm, 1);
    819         ps_ptl->ai1_profile_compatibility_flag[i] = value;
    820     }
    821 
    822     BITS_PARSE("general_progressive_source_flag", value, ps_bitstrm, 1);
    823     ps_ptl->i1_general_progressive_source_flag = value;
    824 
    825     BITS_PARSE("general_interlaced_source_flag", value, ps_bitstrm, 1);
    826     ps_ptl->i1_general_progressive_source_flag = value;
    827 
    828     BITS_PARSE("general_non_packed_constraint_flag", value, ps_bitstrm, 1);
    829     ps_ptl->i1_general_progressive_source_flag = value;
    830 
    831     BITS_PARSE("general_frame_only_constraint_flag", value, ps_bitstrm, 1);
    832     ps_ptl->i1_general_progressive_source_flag = value;
    833 
    834     BITS_PARSE("XXX_reserved_zero_44bits[0..15]", value, ps_bitstrm, 16);
    835 
    836     BITS_PARSE("XXX_reserved_zero_44bits[16..31]", value, ps_bitstrm, 16);
    837 
    838     BITS_PARSE("XXX_reserved_zero_44bits[32..43]", value, ps_bitstrm, 12);
    839     return ret;
    840 }
    841 
    842 
    843 /**
    844 *******************************************************************************
    845 *
    846 * @brief
    847 *  Parses profile tier and level info
    848 *
    849 * @par   Description
    850 *  Parses profile tier and level info as per section 7.3.3
    851 * Called during VPS and SPS parsing
    852 * calls ihevcd_parse_profile_tier_level() for general layer and each sub_layers
    853 *
    854 * @param[in] ps_bitstrm
    855 *  Pointer to bitstream structure
    856 *
    857 * @param[out] ps_ptl
    858 *  Pointer to structure that contains profile, tier level for each layers
    859 *
    860 * @param[in] profile_present
    861 *  Flag to indicate if profile data is present
    862 *
    863 * @param[in] max_num_sub_layers
    864 *  Number of sub layers present
    865 *
    866 * @returns Error code from IHEVCD_ERROR_T
    867 *
    868 * @remarks
    869 *
    870 *******************************************************************************
    871 */
    872 
    873 static IHEVCD_ERROR_T ihevcd_profile_tier_level(bitstrm_t *ps_bitstrm,
    874                                                 profile_tier_lvl_info_t *ps_ptl,
    875                                                 WORD32 profile_present,
    876                                                 WORD32 max_num_sub_layers)
    877 {
    878     WORD32 value;
    879     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    880     WORD32 i;
    881 
    882     if(profile_present)
    883     {
    884         ret = ihevcd_parse_profile_tier_level_layer(ps_bitstrm, &ps_ptl->s_ptl_gen);
    885     }
    886 
    887     BITS_PARSE("general_level_idc", value, ps_bitstrm, 8);
    888     ps_ptl->s_ptl_gen.u1_level_idc = value;
    889 
    890 
    891     for(i = 0; i < max_num_sub_layers; i++)
    892     {
    893         BITS_PARSE("sub_layer_profile_present_flag[i]", value, ps_bitstrm, 1);
    894         ps_ptl->ai1_sub_layer_profile_present_flag[i] = value;
    895 
    896         BITS_PARSE("sub_layer_level_present_flag[i]", value, ps_bitstrm, 1);
    897         ps_ptl->ai1_sub_layer_level_present_flag[i] = value;
    898     }
    899 
    900     if(max_num_sub_layers > 0)
    901     {
    902         for(i = max_num_sub_layers; i < 8; i++)
    903         {
    904             BITS_PARSE("reserved_zero_2bits", value, ps_bitstrm, 2);
    905         }
    906     }
    907 
    908     for(i = 0; i < max_num_sub_layers; i++)
    909     {
    910         if(ps_ptl->ai1_sub_layer_profile_present_flag[i])
    911         {
    912             ret = ihevcd_parse_profile_tier_level_layer(ps_bitstrm,
    913                                                         &ps_ptl->as_ptl_sub[i]);
    914         }
    915         if(ps_ptl->ai1_sub_layer_level_present_flag[i])
    916         {
    917             BITS_PARSE("sub_layer_level_idc[i]", value, ps_bitstrm, 8);
    918             ps_ptl->as_ptl_sub[i].u1_level_idc = value;
    919 
    920         }
    921     }
    922 
    923 
    924 
    925     return ret;
    926 }
    927 
    928 /**
    929 *******************************************************************************
    930 *
    931 * @brief
    932 *  Parses Scaling List Data syntax
    933 *
    934 * @par Description:
    935 *  Parses Scaling List Data syntax as per Section: 7.3.6
    936 *
    937 * @param[in] ps_codec
    938 *  Pointer to codec context
    939 *
    940 * @returns  Error code from IHEVCD_ERROR_T
    941 *
    942 * @remarks
    943 *
    944 *
    945 *******************************************************************************
    946 */
    947 IHEVCD_ERROR_T  ihevcd_scaling_list_data(codec_t *ps_codec, WORD16 *pi2_scaling_mat)
    948 {
    949     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
    950     WORD32 size_id;
    951     WORD32 matrix_id;
    952     WORD32 value, dc_value = 0;
    953     WORD32 next_coef;
    954     WORD32 coef_num;
    955     WORD32 i, j, offset;
    956     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
    957     WORD16 *pi2_scaling_mat_offset;
    958     WORD32 scaling_mat_offset[] = { 0, 16, 32, 48, 64, 80, 96, 160, 224, 288, 352, 416, 480, 736, 992, 1248, 1504, 1760, 2016, 3040 };
    959     UWORD8 *scan_table;
    960 
    961     for(size_id = 0; size_id < 4; size_id++)
    962     {
    963         for(matrix_id = 0; matrix_id < ((size_id == 3) ? 2 : 6); matrix_id++)
    964         {
    965             WORD32 scaling_list_pred_mode_flag;
    966             WORD32 scaling_list_delta_coef;
    967             BITS_PARSE("scaling_list_pred_mode_flag", scaling_list_pred_mode_flag, ps_bitstrm, 1);
    968 
    969             offset = size_id * 6 + matrix_id;
    970             pi2_scaling_mat_offset = pi2_scaling_mat + scaling_mat_offset[offset];
    971 
    972             if(!scaling_list_pred_mode_flag)
    973             {
    974                 WORD32 num_elements;
    975                 UEV_PARSE("scaling_list_pred_matrix_id_delta", value,
    976                           ps_bitstrm);
    977                 value = CLIP3(value, 0, matrix_id);
    978 
    979                 num_elements = (1 << (4 + (size_id << 1)));
    980                 if(0 != value)
    981                     memcpy(pi2_scaling_mat_offset, pi2_scaling_mat_offset - value * num_elements, num_elements * sizeof(WORD16));
    982             }
    983             else
    984             {
    985                 next_coef = 8;
    986                 coef_num = MIN(64, (1 << (4 + (size_id << 1))));
    987 
    988                 if(size_id > 1)
    989                 {
    990                     SEV_PARSE("scaling_list_dc_coef_minus8", value,
    991                               ps_bitstrm);
    992 
    993                     next_coef = value + 8;
    994                     dc_value = next_coef;
    995                 }
    996                 if(size_id < 2)
    997                 {
    998                     scan_table = (UWORD8 *)gapv_ihevc_invscan[size_id + 1];
    999 
   1000                     for(i = 0; i < coef_num; i++)
   1001                     {
   1002                         SEV_PARSE("scaling_list_delta_coef",
   1003                                   scaling_list_delta_coef, ps_bitstrm);
   1004                         next_coef = (next_coef + scaling_list_delta_coef + 256)
   1005                                         % 256;
   1006                         pi2_scaling_mat_offset[scan_table[i]] = next_coef;
   1007                     }
   1008                 }
   1009                 else if(size_id == 2)
   1010                 {
   1011                     scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
   1012 
   1013                     for(i = 0; i < coef_num; i++)
   1014                     {
   1015                         SEV_PARSE("scaling_list_delta_coef",
   1016                                   scaling_list_delta_coef, ps_bitstrm);
   1017                         next_coef = (next_coef + scaling_list_delta_coef + 256)
   1018                                         % 256;
   1019 
   1020                         offset = scan_table[i];
   1021                         offset = (offset >> 3) * 16 * 2 + (offset & 0x7) * 2;
   1022                         pi2_scaling_mat_offset[offset] = next_coef;
   1023                         pi2_scaling_mat_offset[offset + 1] = next_coef;
   1024                         pi2_scaling_mat_offset[offset + 16] = next_coef;
   1025                         pi2_scaling_mat_offset[offset + 16 + 1] = next_coef;
   1026                     }
   1027                     pi2_scaling_mat_offset[0] = dc_value;
   1028                 }
   1029                 else
   1030                 {
   1031                     scan_table = (UWORD8 *)gapv_ihevc_invscan[2];
   1032 
   1033                     for(i = 0; i < coef_num; i++)
   1034                     {
   1035                         SEV_PARSE("scaling_list_delta_coef",
   1036                                   scaling_list_delta_coef, ps_bitstrm);
   1037                         next_coef = (next_coef + scaling_list_delta_coef + 256)
   1038                                         % 256;
   1039 
   1040                         offset = scan_table[i];
   1041                         offset = (offset >> 3) * 32 * 4 + (offset & 0x7) * 4;
   1042 
   1043                         for(j = 0; j < 4; j++)
   1044                         {
   1045                             pi2_scaling_mat_offset[offset + j * 32] = next_coef;
   1046                             pi2_scaling_mat_offset[offset + 1 + j * 32] = next_coef;
   1047                             pi2_scaling_mat_offset[offset + 2 + j * 32] = next_coef;
   1048                             pi2_scaling_mat_offset[offset + 3 + j * 32] = next_coef;
   1049                         }
   1050                         pi2_scaling_mat_offset[0] = dc_value;
   1051                     }
   1052                 }
   1053             }
   1054         }
   1055     }
   1056 
   1057     return ret;
   1058 }
   1059 
   1060 /**
   1061 *******************************************************************************
   1062 *
   1063 * @brief
   1064 *  Parses VPS (Video Parameter Set)
   1065 *
   1066 * @par Description:
   1067 *  Parse Video Parameter Set as per Section 7.3.2.1
   1068 * update vps structure corresponding to vps ID
   1069 * Till parsing VPS id, the elements are stored in local variables and are copied
   1070 * later
   1071 *
   1072 * @param[in] ps_codec
   1073 *  Pointer to codec context.
   1074 *
   1075 * @returns Error code from IHEVCD_ERROR_T
   1076 *
   1077 * @remarks
   1078 *
   1079 *
   1080 *******************************************************************************
   1081 */
   1082 IHEVCD_ERROR_T ihevcd_parse_vps(codec_t *ps_codec)
   1083 {
   1084     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   1085     WORD32 i;
   1086     WORD32 value;
   1087     WORD32 vps_id;
   1088     vps_t *ps_vps;
   1089     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
   1090     BITS_PARSE("vps_video_parameter_set_id", value, ps_bitstrm, 4);
   1091     vps_id = value;
   1092 
   1093     if(vps_id >= MAX_VPS_CNT)
   1094     {
   1095         ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_VPS_ID;
   1096         return IHEVCD_UNSUPPORTED_VPS_ID;
   1097     }
   1098 
   1099 
   1100     ps_vps = (ps_codec->s_parse.ps_vps_base + vps_id);
   1101 
   1102     ps_vps->i1_vps_id = vps_id;
   1103 
   1104     BITS_PARSE("vps_reserved_three_2bits", value, ps_bitstrm, 2);
   1105     ASSERT(value == 3);
   1106 
   1107     BITS_PARSE("vps_max_layers_minus1", value, ps_bitstrm, 6);
   1108     //ps_vps->i1_vps_max_layers = value + 1;
   1109 
   1110 
   1111 
   1112     BITS_PARSE("vps_max_sub_layers_minus1", value, ps_bitstrm, 3);
   1113     ps_vps->i1_vps_max_sub_layers = value + 1;
   1114 
   1115     ASSERT(ps_vps->i1_vps_max_sub_layers < VPS_MAX_SUB_LAYERS);
   1116 
   1117     BITS_PARSE("vps_temporal_id_nesting_flag", value, ps_bitstrm, 1);
   1118     ps_vps->i1_vps_temporal_id_nesting_flag = value;
   1119 
   1120     BITS_PARSE("vps_reserved_ffff_16bits", value, ps_bitstrm, 16);
   1121     ASSERT(value == 0xFFFF);
   1122     // profile_and_level( 1, vps_max_sub_layers_minus1 )
   1123     ret = ihevcd_profile_tier_level(ps_bitstrm, &(ps_vps->s_ptl),
   1124                                     1, (ps_vps->i1_vps_max_sub_layers - 1));
   1125 
   1126     BITS_PARSE("vps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
   1127     ps_vps->i1_sub_layer_ordering_info_present_flag = value;
   1128     i = (ps_vps->i1_sub_layer_ordering_info_present_flag ?
   1129                     0 : (ps_vps->i1_vps_max_sub_layers - 1));
   1130     for(; i < ps_vps->i1_vps_max_sub_layers; i++)
   1131     {
   1132         UEV_PARSE("vps_max_dec_pic_buffering[i]", value, ps_bitstrm);
   1133         ps_vps->ai1_vps_max_dec_pic_buffering[i] = value;
   1134 
   1135         /* vps_num_reorder_pics (no max) used in print in order to match with HM */
   1136         UEV_PARSE("vps_num_reorder_pics[i]", value, ps_bitstrm);
   1137         ps_vps->ai1_vps_max_num_reorder_pics[i] = value;
   1138 
   1139         UEV_PARSE("vps_max_latency_increase[i]", value, ps_bitstrm);
   1140         ps_vps->ai1_vps_max_latency_increase[i] = value;
   1141     }
   1142 
   1143 
   1144 
   1145     BITS_PARSE("vps_max_layer_id", value, ps_bitstrm, 6);
   1146     //ps_vps->i1_vps_max_layer_id  = value;
   1147 
   1148     UEV_PARSE("vps_num_layer_sets_minus1", value, ps_bitstrm);
   1149     //ps_vps->i1_vps_num_layer_sets  = value + 1;
   1150 
   1151     BITS_PARSE("vps_timing_info_present_flag", value, ps_bitstrm, 1);
   1152     //ps_vps->i1_vps_timing_info_present_flag  = value;
   1153 
   1154 
   1155 
   1156     return ret;
   1157 }
   1158 
   1159 /**
   1160 *******************************************************************************
   1161 *
   1162 * @brief
   1163 *  Parses SPS (Sequence Parameter Set)
   1164 * sequence_parameter_set_rbsp()
   1165 *
   1166 * @par Description:
   1167 *  Parse Sequence Parameter Set as per section  Section: 7.3.2.2
   1168 * The sps is written to a temporary buffer and copied later to the
   1169 * appropriate location
   1170 *
   1171 * @param[in] ps_codec
   1172 *  Pointer to codec context
   1173 *
   1174 * @returns Error code from IHEVCD_ERROR_T
   1175 *
   1176 * @remarks
   1177 *
   1178 *
   1179 *******************************************************************************
   1180 */
   1181 IHEVCD_ERROR_T ihevcd_parse_sps(codec_t *ps_codec)
   1182 {
   1183     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   1184     WORD32 value;
   1185 
   1186     WORD32 i;
   1187     WORD32 vps_id;
   1188     WORD32 sps_max_sub_layers;
   1189     WORD32 sps_id;
   1190     WORD32 sps_temporal_id_nesting_flag;
   1191     sps_t *ps_sps;
   1192     profile_tier_lvl_info_t s_ptl;
   1193     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
   1194 
   1195 
   1196     BITS_PARSE("video_parameter_set_id", value, ps_bitstrm, 4);
   1197     vps_id = value;
   1198     vps_id = CLIP3(vps_id, 0, MAX_VPS_CNT - 1);
   1199 
   1200     BITS_PARSE("sps_max_sub_layers_minus1", value, ps_bitstrm, 3);
   1201     sps_max_sub_layers = value + 1;
   1202     sps_max_sub_layers = CLIP3(sps_max_sub_layers, 1, 7);
   1203 
   1204     BITS_PARSE("sps_temporal_id_nesting_flag", value, ps_bitstrm, 1);
   1205     sps_temporal_id_nesting_flag = value;
   1206 
   1207     //profile_and_level( 1, sps_max_sub_layers_minus1 )
   1208     ret = ihevcd_profile_tier_level(ps_bitstrm, &(s_ptl), 1,
   1209                                     (sps_max_sub_layers - 1));
   1210 
   1211     UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
   1212     sps_id = value;
   1213 
   1214     if((sps_id >= MAX_SPS_CNT) || (sps_id < 0))
   1215     {
   1216         if(ps_codec->i4_sps_done)
   1217             return IHEVCD_UNSUPPORTED_SPS_ID;
   1218         else
   1219             sps_id = 0;
   1220     }
   1221 
   1222 
   1223     ps_sps = (ps_codec->s_parse.ps_sps_base + MAX_SPS_CNT - 1);
   1224     ps_sps->i1_sps_id = sps_id;
   1225     ps_sps->i1_vps_id = vps_id;
   1226     ps_sps->i1_sps_max_sub_layers = sps_max_sub_layers;
   1227     ps_sps->i1_sps_temporal_id_nesting_flag = sps_temporal_id_nesting_flag;
   1228     /* This is used only during initialization to get reorder count etc */
   1229     ps_codec->i4_sps_id = sps_id;
   1230     memcpy(&ps_sps->s_ptl, &s_ptl, sizeof(profile_tier_lvl_info_t));
   1231 
   1232     UEV_PARSE("chroma_format_idc", value, ps_bitstrm);
   1233     ps_sps->i1_chroma_format_idc = value;
   1234 
   1235     if(ps_sps->i1_chroma_format_idc != CHROMA_FMT_IDC_YUV420)
   1236     {
   1237         ps_codec->s_parse.i4_error_code = IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
   1238         return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_CHROMA_FMT_IDC;
   1239     }
   1240 
   1241     if(CHROMA_FMT_IDC_YUV444_PLANES == ps_sps->i1_chroma_format_idc)
   1242     {
   1243         BITS_PARSE("separate_colour_plane_flag", value, ps_bitstrm, 1);
   1244         ps_sps->i1_separate_colour_plane_flag = value;
   1245     }
   1246     else
   1247     {
   1248         ps_sps->i1_separate_colour_plane_flag = 0;
   1249     }
   1250 
   1251     UEV_PARSE("pic_width_in_luma_samples", value, ps_bitstrm);
   1252     ps_sps->i2_pic_width_in_luma_samples = value;
   1253 
   1254     UEV_PARSE("pic_height_in_luma_samples", value, ps_bitstrm);
   1255     ps_sps->i2_pic_height_in_luma_samples = value;
   1256 
   1257     if((0 >= ps_sps->i2_pic_width_in_luma_samples) || (0 >= ps_sps->i2_pic_height_in_luma_samples))
   1258         return IHEVCD_INVALID_PARAMETER;
   1259 
   1260     if((ps_sps->i2_pic_width_in_luma_samples > ps_codec->i4_max_wd) ||
   1261        (ps_sps->i2_pic_width_in_luma_samples * ps_sps->i2_pic_height_in_luma_samples >
   1262                        ps_codec->i4_max_wd * ps_codec->i4_max_ht) ||
   1263        (ps_sps->i2_pic_height_in_luma_samples > MAX(ps_codec->i4_max_wd, ps_codec->i4_max_ht)))
   1264     {
   1265         ps_codec->i4_new_max_wd = ps_sps->i2_pic_width_in_luma_samples;
   1266         ps_codec->i4_new_max_ht = ps_sps->i2_pic_height_in_luma_samples;
   1267         return (IHEVCD_ERROR_T)IHEVCD_UNSUPPORTED_DIMENSIONS;
   1268     }
   1269 
   1270     BITS_PARSE("pic_cropping_flag", value, ps_bitstrm, 1);
   1271     ps_sps->i1_pic_cropping_flag = value;
   1272 
   1273     if(ps_sps->i1_pic_cropping_flag)
   1274     {
   1275 
   1276         UEV_PARSE("pic_crop_left_offset", value, ps_bitstrm);
   1277         ps_sps->i2_pic_crop_left_offset = value;
   1278 
   1279         UEV_PARSE("pic_crop_right_offset", value, ps_bitstrm);
   1280         ps_sps->i2_pic_crop_right_offset = value;
   1281 
   1282         UEV_PARSE("pic_crop_top_offset", value, ps_bitstrm);
   1283         ps_sps->i2_pic_crop_top_offset = value;
   1284 
   1285         UEV_PARSE("pic_crop_bottom_offset", value, ps_bitstrm);
   1286         ps_sps->i2_pic_crop_bottom_offset = value;
   1287     }
   1288     else
   1289     {
   1290         ps_sps->i2_pic_crop_left_offset = 0;
   1291         ps_sps->i2_pic_crop_right_offset = 0;
   1292         ps_sps->i2_pic_crop_top_offset = 0;
   1293         ps_sps->i2_pic_crop_bottom_offset = 0;
   1294     }
   1295 
   1296 
   1297     UEV_PARSE("bit_depth_luma_minus8", value, ps_bitstrm);
   1298     if(0 != value)
   1299         return IHEVCD_UNSUPPORTED_BIT_DEPTH;
   1300 
   1301     UEV_PARSE("bit_depth_chroma_minus8", value, ps_bitstrm);
   1302     if(0 != value)
   1303         return IHEVCD_UNSUPPORTED_BIT_DEPTH;
   1304 
   1305     UEV_PARSE("log2_max_pic_order_cnt_lsb_minus4", value, ps_bitstrm);
   1306     ps_sps->i1_log2_max_pic_order_cnt_lsb = value + 4;
   1307 
   1308     BITS_PARSE("sps_sub_layer_ordering_info_present_flag", value, ps_bitstrm, 1);
   1309     ps_sps->i1_sps_sub_layer_ordering_info_present_flag = value;
   1310 
   1311 
   1312     i = (ps_sps->i1_sps_sub_layer_ordering_info_present_flag ? 0 : (ps_sps->i1_sps_max_sub_layers - 1));
   1313     for(; i < ps_sps->i1_sps_max_sub_layers; i++)
   1314     {
   1315         UEV_PARSE("max_dec_pic_buffering", value, ps_bitstrm);
   1316         ps_sps->ai1_sps_max_dec_pic_buffering[i] = value + 1;
   1317 
   1318         UEV_PARSE("num_reorder_pics", value, ps_bitstrm);
   1319         ps_sps->ai1_sps_max_num_reorder_pics[i] = value;
   1320 
   1321         UEV_PARSE("max_latency_increase", value, ps_bitstrm);
   1322         ps_sps->ai1_sps_max_latency_increase[i] = value;
   1323     }
   1324     UEV_PARSE("log2_min_coding_block_size_minus3", value, ps_bitstrm);
   1325     ps_sps->i1_log2_min_coding_block_size = value + 3;
   1326 
   1327     UEV_PARSE("log2_diff_max_min_coding_block_size", value, ps_bitstrm);
   1328     ps_sps->i1_log2_diff_max_min_coding_block_size = value;
   1329 
   1330     UEV_PARSE("log2_min_transform_block_size_minus2", value, ps_bitstrm);
   1331     ps_sps->i1_log2_min_transform_block_size = value + 2;
   1332 
   1333     UEV_PARSE("log2_diff_max_min_transform_block_size", value, ps_bitstrm);
   1334     ps_sps->i1_log2_diff_max_min_transform_block_size = value;
   1335 
   1336     ps_sps->i1_log2_max_transform_block_size = ps_sps->i1_log2_min_transform_block_size +
   1337                     ps_sps->i1_log2_diff_max_min_transform_block_size;
   1338 
   1339     ps_sps->i1_log2_ctb_size = ps_sps->i1_log2_min_coding_block_size +
   1340                     ps_sps->i1_log2_diff_max_min_coding_block_size;
   1341 
   1342     if((ps_sps->i1_log2_min_coding_block_size < 3) ||
   1343                     (ps_sps->i1_log2_min_transform_block_size < 2) ||
   1344                     (ps_sps->i1_log2_diff_max_min_transform_block_size < 0) ||
   1345                     (ps_sps->i1_log2_max_transform_block_size > ps_sps->i1_log2_ctb_size) ||
   1346                     (ps_sps->i1_log2_ctb_size < 4) ||
   1347                     (ps_sps->i1_log2_ctb_size > 6))
   1348     {
   1349         return IHEVCD_INVALID_PARAMETER;
   1350     }
   1351 
   1352     ps_sps->i1_log2_min_pcm_coding_block_size = 0;
   1353     ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = 0;
   1354 
   1355     UEV_PARSE("max_transform_hierarchy_depth_inter", value, ps_bitstrm);
   1356     ps_sps->i1_max_transform_hierarchy_depth_inter = value;
   1357 
   1358     UEV_PARSE("max_transform_hierarchy_depth_intra", value, ps_bitstrm);
   1359     ps_sps->i1_max_transform_hierarchy_depth_intra = value;
   1360 
   1361     /* String has a d (enabled) in order to match with HM */
   1362     BITS_PARSE("scaling_list_enabled_flag", value, ps_bitstrm, 1);
   1363     ps_sps->i1_scaling_list_enable_flag = value;
   1364 
   1365     if(ps_sps->i1_scaling_list_enable_flag)
   1366     {
   1367         COPY_DEFAULT_SCALING_LIST(ps_sps->pi2_scaling_mat);
   1368         BITS_PARSE("sps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
   1369         ps_sps->i1_sps_scaling_list_data_present_flag = value;
   1370 
   1371         if(ps_sps->i1_sps_scaling_list_data_present_flag)
   1372             ihevcd_scaling_list_data(ps_codec, ps_sps->pi2_scaling_mat);
   1373     }
   1374     else
   1375     {
   1376         COPY_FLAT_SCALING_LIST(ps_sps->pi2_scaling_mat);
   1377     }
   1378     /* String is asymmetric_motion_partitions_enabled_flag instead of amp_enabled_flag in order to match with HM */
   1379     BITS_PARSE("asymmetric_motion_partitions_enabled_flag", value, ps_bitstrm, 1);
   1380     ps_sps->i1_amp_enabled_flag = value;
   1381 
   1382     BITS_PARSE("sample_adaptive_offset_enabled_flag", value, ps_bitstrm, 1);
   1383     ps_sps->i1_sample_adaptive_offset_enabled_flag = value;
   1384 
   1385     BITS_PARSE("pcm_enabled_flag", value, ps_bitstrm, 1);
   1386     ps_sps->i1_pcm_enabled_flag = value;
   1387 
   1388     if(ps_sps->i1_pcm_enabled_flag)
   1389     {
   1390         BITS_PARSE("pcm_sample_bit_depth_luma", value, ps_bitstrm, 4);
   1391         ps_sps->i1_pcm_sample_bit_depth_luma = value + 1;
   1392 
   1393         BITS_PARSE("pcm_sample_bit_depth_chroma", value, ps_bitstrm, 4);
   1394         ps_sps->i1_pcm_sample_bit_depth_chroma = value + 1;
   1395 
   1396         UEV_PARSE("log2_min_pcm_coding_block_size_minus3", value, ps_bitstrm);
   1397         ps_sps->i1_log2_min_pcm_coding_block_size = value + 3;
   1398 
   1399         UEV_PARSE("log2_diff_max_min_pcm_coding_block_size", value, ps_bitstrm);
   1400         ps_sps->i1_log2_diff_max_min_pcm_coding_block_size = value;
   1401         BITS_PARSE("pcm_loop_filter_disable_flag", value, ps_bitstrm, 1);
   1402         ps_sps->i1_pcm_loop_filter_disable_flag = value;
   1403 
   1404     }
   1405     UEV_PARSE("num_short_term_ref_pic_sets", value, ps_bitstrm);
   1406     ps_sps->i1_num_short_term_ref_pic_sets = value;
   1407 
   1408     ps_sps->i1_num_short_term_ref_pic_sets = CLIP3(ps_sps->i1_num_short_term_ref_pic_sets, 0, MAX_STREF_PICS_SPS);
   1409 
   1410     for(i = 0; i < ps_sps->i1_num_short_term_ref_pic_sets; i++)
   1411         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]);
   1412 
   1413     BITS_PARSE("long_term_ref_pics_present_flag", value, ps_bitstrm, 1);
   1414     ps_sps->i1_long_term_ref_pics_present_flag = value;
   1415 
   1416     if(ps_sps->i1_long_term_ref_pics_present_flag)
   1417     {
   1418         UEV_PARSE("num_long_term_ref_pics_sps", value, ps_bitstrm);
   1419         ps_sps->i1_num_long_term_ref_pics_sps = value;
   1420 
   1421         for(i = 0; i < ps_sps->i1_num_long_term_ref_pics_sps; i++)
   1422         {
   1423             BITS_PARSE("lt_ref_pic_poc_lsb_sps[ i ]", value, ps_bitstrm, ps_sps->i1_log2_max_pic_order_cnt_lsb);
   1424             ps_sps->ai1_lt_ref_pic_poc_lsb_sps[i] = value;
   1425 
   1426             BITS_PARSE("used_by_curr_pic_lt_sps_flag[ i ]", value, ps_bitstrm, 1);
   1427             ps_sps->ai1_used_by_curr_pic_lt_sps_flag[i] = value;
   1428         }
   1429     }
   1430 
   1431     BITS_PARSE("sps_temporal_mvp_enable_flag", value, ps_bitstrm, 1);
   1432     ps_sps->i1_sps_temporal_mvp_enable_flag = value;
   1433 
   1434     /* Print matches HM 8-2 */
   1435     BITS_PARSE("sps_strong_intra_smoothing_enable_flag", value, ps_bitstrm, 1);
   1436     ps_sps->i1_strong_intra_smoothing_enable_flag = value;
   1437 
   1438     BITS_PARSE("vui_parameters_present_flag", value, ps_bitstrm, 1);
   1439     ps_sps->i1_vui_parameters_present_flag = value;
   1440 
   1441     if(ps_sps->i1_vui_parameters_present_flag)
   1442         ihevcd_parse_vui_parameters(ps_bitstrm,
   1443                                     &ps_sps->s_vui_parameters,
   1444                                     ps_sps->i1_sps_max_sub_layers - 1);
   1445 
   1446     BITS_PARSE("sps_extension_flag", value, ps_bitstrm, 1);
   1447 
   1448 
   1449     {
   1450         WORD32 numerator;
   1451         WORD32 ceil_offset;
   1452 
   1453         ceil_offset = (1 << ps_sps->i1_log2_ctb_size) - 1;
   1454         numerator = ps_sps->i2_pic_width_in_luma_samples;
   1455 
   1456         ps_sps->i2_pic_wd_in_ctb = ((numerator + ceil_offset) /
   1457                         (1 << ps_sps->i1_log2_ctb_size));
   1458 
   1459         numerator = ps_sps->i2_pic_height_in_luma_samples;
   1460         ps_sps->i2_pic_ht_in_ctb = ((numerator + ceil_offset) /
   1461                         (1 << ps_sps->i1_log2_ctb_size));
   1462 
   1463         ps_sps->i4_pic_size_in_ctb = ps_sps->i2_pic_ht_in_ctb *
   1464                         ps_sps->i2_pic_wd_in_ctb;
   1465 
   1466         if(0 == ps_codec->i4_sps_done)
   1467             ps_codec->s_parse.i4_next_ctb_indx = ps_sps->i4_pic_size_in_ctb;
   1468 
   1469         numerator = ps_sps->i2_pic_width_in_luma_samples;
   1470         ps_sps->i2_pic_wd_in_min_cb = numerator  /
   1471                         (1 << ps_sps->i1_log2_min_coding_block_size);
   1472 
   1473         numerator = ps_sps->i2_pic_height_in_luma_samples;
   1474         ps_sps->i2_pic_ht_in_min_cb = numerator  /
   1475                         (1 << ps_sps->i1_log2_min_coding_block_size);
   1476     }
   1477     if((0 != ps_codec->i4_first_pic_done) &&
   1478                     ((ps_codec->i4_wd != ps_sps->i2_pic_width_in_luma_samples) ||
   1479                     (ps_codec->i4_ht != ps_sps->i2_pic_height_in_luma_samples)))
   1480     {
   1481         ps_codec->i4_reset_flag = 1;
   1482         ps_codec->i4_error_code = IVD_RES_CHANGED;
   1483         return (IHEVCD_ERROR_T)IHEVCD_FAIL;
   1484     }
   1485 
   1486     /* Update display width and display height */
   1487     {
   1488         WORD32 disp_wd, disp_ht;
   1489         WORD32 crop_unit_x, crop_unit_y;
   1490         crop_unit_x = 1;
   1491         crop_unit_y = 1;
   1492 
   1493         if(CHROMA_FMT_IDC_YUV420 == ps_sps->i1_chroma_format_idc)
   1494         {
   1495             crop_unit_x = 2;
   1496             crop_unit_y = 2;
   1497         }
   1498 
   1499         disp_wd = ps_sps->i2_pic_width_in_luma_samples;
   1500         disp_wd -= ps_sps->i2_pic_crop_left_offset * crop_unit_x;
   1501         disp_wd -= ps_sps->i2_pic_crop_right_offset * crop_unit_x;
   1502 
   1503 
   1504         disp_ht = ps_sps->i2_pic_height_in_luma_samples;
   1505         disp_ht -= ps_sps->i2_pic_crop_top_offset * crop_unit_y;
   1506         disp_ht -= ps_sps->i2_pic_crop_bottom_offset * crop_unit_y;
   1507 
   1508         if((0 >= disp_wd) || (0 >= disp_ht))
   1509             return IHEVCD_INVALID_PARAMETER;
   1510 
   1511         ps_codec->i4_disp_wd = disp_wd;
   1512         ps_codec->i4_disp_ht = disp_ht;
   1513 
   1514 
   1515         ps_codec->i4_wd = ps_sps->i2_pic_width_in_luma_samples;
   1516         ps_codec->i4_ht = ps_sps->i2_pic_height_in_luma_samples;
   1517 
   1518         {
   1519             WORD32 ref_strd;
   1520             ref_strd = ALIGN32(ps_sps->i2_pic_width_in_luma_samples + PAD_WD);
   1521             if(ps_codec->i4_strd < ref_strd)
   1522             {
   1523                 ps_codec->i4_strd = ref_strd;
   1524             }
   1525         }
   1526 
   1527         if(0 == ps_codec->i4_share_disp_buf)
   1528         {
   1529             if(ps_codec->i4_disp_strd < ps_codec->i4_disp_wd)
   1530             {
   1531                 ps_codec->i4_disp_strd = ps_codec->i4_disp_wd;
   1532             }
   1533         }
   1534         else
   1535         {
   1536             if(ps_codec->i4_disp_strd < ps_codec->i4_strd)
   1537             {
   1538                 ps_codec->i4_disp_strd = ps_codec->i4_strd;
   1539             }
   1540         }
   1541     }
   1542 
   1543     ps_codec->i4_sps_done = 1;
   1544     return ret;
   1545 }
   1546 
   1547 
   1548 void ihevcd_unmark_pps(codec_t *ps_codec, WORD32 sps_id)
   1549 {
   1550     WORD32 pps_id = 0;
   1551     pps_t *ps_pps = ps_codec->ps_pps_base;
   1552 
   1553     for(pps_id = 0; pps_id < MAX_PPS_CNT - 1; pps_id++, ps_pps++)
   1554     {
   1555         if((ps_pps->i1_pps_valid) &&
   1556                         (ps_pps->i1_sps_id == sps_id))
   1557             ps_pps->i1_pps_valid = 0;
   1558     }
   1559 }
   1560 
   1561 
   1562 void ihevcd_copy_sps(codec_t *ps_codec, WORD32 sps_id, WORD32 sps_id_ref)
   1563 {
   1564     sps_t *ps_sps, *ps_sps_ref;
   1565     WORD16 *pi2_scaling_mat_backup;
   1566     WORD32 scaling_mat_size;
   1567 
   1568     SCALING_MAT_SIZE(scaling_mat_size);
   1569     ps_sps_ref = ps_codec->ps_sps_base + sps_id_ref;
   1570     ps_sps = ps_codec->ps_sps_base + sps_id;
   1571 
   1572     if(ps_sps->i1_sps_valid)
   1573     {
   1574         if((ps_sps->i1_log2_ctb_size != ps_sps_ref->i1_log2_ctb_size) ||
   1575                         (ps_sps->i2_pic_wd_in_ctb != ps_sps_ref->i2_pic_wd_in_ctb) ||
   1576                         (ps_sps->i2_pic_ht_in_ctb != ps_sps_ref->i2_pic_ht_in_ctb))
   1577         {
   1578             ihevcd_unmark_pps(ps_codec, sps_id);
   1579         }
   1580     }
   1581 
   1582     pi2_scaling_mat_backup = ps_sps->pi2_scaling_mat;
   1583 
   1584     memcpy(ps_sps, ps_sps_ref, sizeof(sps_t));
   1585     ps_sps->pi2_scaling_mat = pi2_scaling_mat_backup;
   1586     memcpy(ps_sps->pi2_scaling_mat, ps_sps_ref->pi2_scaling_mat, scaling_mat_size * sizeof(WORD16));
   1587     ps_sps->i1_sps_valid = 1;
   1588 
   1589     ps_codec->s_parse.ps_sps = ps_sps;
   1590 }
   1591 
   1592 
   1593 /**
   1594 *******************************************************************************
   1595 *
   1596 * @brief
   1597 *  Parses PPS (Picture Parameter Set)
   1598 *
   1599 * @par Description:
   1600 *  Parse Picture Parameter Set as per section  Section: 7.3.2.3
   1601 * The pps is written to a temporary buffer and copied later to the
   1602 * appropriate location
   1603 *
   1604 * @param[in] ps_codec
   1605 *  Pointer to codec context
   1606 *
   1607 * @returns Error code from IHEVCD_ERROR_T
   1608 *
   1609 * @remarks
   1610 *
   1611 *
   1612 *******************************************************************************
   1613 */
   1614 IHEVCD_ERROR_T ihevcd_parse_pps(codec_t *ps_codec)
   1615 {
   1616     IHEVCD_ERROR_T ret = (IHEVCD_ERROR_T)IHEVCD_SUCCESS;
   1617     WORD32 value;
   1618     WORD32 pps_id;
   1619 
   1620     pps_t *ps_pps;
   1621     sps_t *ps_sps;
   1622     bitstrm_t *ps_bitstrm = &ps_codec->s_parse.s_bitstrm;
   1623 
   1624 
   1625     if(0 == ps_codec->i4_sps_done)
   1626         return IHEVCD_INVALID_HEADER;
   1627 
   1628     UEV_PARSE("pic_parameter_set_id", value, ps_bitstrm);
   1629 
   1630     pps_id = value;
   1631     if((pps_id >= MAX_PPS_CNT) || (pps_id < 0))
   1632     {
   1633         if(ps_codec->i4_pps_done)
   1634             return IHEVCD_UNSUPPORTED_PPS_ID;
   1635         else
   1636             pps_id = 0;
   1637     }
   1638 
   1639 
   1640     ps_pps = (ps_codec->s_parse.ps_pps_base + MAX_PPS_CNT - 1);
   1641 
   1642     ps_pps->i1_pps_id = pps_id;
   1643 
   1644     UEV_PARSE("seq_parameter_set_id", value, ps_bitstrm);
   1645     ps_pps->i1_sps_id = value;
   1646     ps_pps->i1_sps_id = CLIP3(ps_pps->i1_sps_id, 0, MAX_SPS_CNT - 2);
   1647 
   1648     ps_sps = (ps_codec->s_parse.ps_sps_base + ps_pps->i1_sps_id);
   1649 
   1650     /* If the SPS that is being referred to has not been parsed,
   1651      * copy an existing SPS to the current location */
   1652     if(0 == ps_sps->i1_sps_valid)
   1653     {
   1654         return IHEVCD_INVALID_HEADER;
   1655 
   1656 /*
   1657         sps_t *ps_sps_ref = ps_codec->ps_sps_base;
   1658         while(0 == ps_sps_ref->i1_sps_valid)
   1659             ps_sps_ref++;
   1660         ihevcd_copy_sps(ps_codec, ps_pps->i1_sps_id, ps_sps_ref->i1_sps_id);
   1661 */
   1662     }
   1663 
   1664     BITS_PARSE("dependent_slices_enabled_flag", value, ps_bitstrm, 1);
   1665     ps_pps->i1_dependent_slice_enabled_flag = value;
   1666 
   1667     BITS_PARSE("output_flag_present_flag", value, ps_bitstrm, 1);
   1668     ps_pps->i1_output_flag_present_flag = value;
   1669 
   1670     BITS_PARSE("num_extra_slice_header_bits", value, ps_bitstrm, 3);
   1671     ps_pps->i1_num_extra_slice_header_bits = value;
   1672 
   1673 
   1674     BITS_PARSE("sign_data_hiding_flag", value, ps_bitstrm, 1);
   1675     ps_pps->i1_sign_data_hiding_flag = value;
   1676 
   1677     BITS_PARSE("cabac_init_present_flag", value, ps_bitstrm, 1);
   1678     ps_pps->i1_cabac_init_present_flag = value;
   1679 
   1680     UEV_PARSE("num_ref_idx_l0_default_active_minus1", value, ps_bitstrm);
   1681     ps_pps->i1_num_ref_idx_l0_default_active = value + 1;
   1682 
   1683     UEV_PARSE("num_ref_idx_l1_default_active_minus1", value, ps_bitstrm);
   1684     ps_pps->i1_num_ref_idx_l1_default_active = value + 1;
   1685 
   1686     SEV_PARSE("pic_init_qp_minus26", value, ps_bitstrm);
   1687     ps_pps->i1_pic_init_qp = value + 26;
   1688 
   1689     BITS_PARSE("constrained_intra_pred_flag", value, ps_bitstrm, 1);
   1690     ps_pps->i1_constrained_intra_pred_flag = value;
   1691 
   1692     BITS_PARSE("transform_skip_enabled_flag", value, ps_bitstrm, 1);
   1693     ps_pps->i1_transform_skip_enabled_flag = value;
   1694 
   1695     BITS_PARSE("cu_qp_delta_enabled_flag", value, ps_bitstrm, 1);
   1696     ps_pps->i1_cu_qp_delta_enabled_flag = value;
   1697 
   1698     if(ps_pps->i1_cu_qp_delta_enabled_flag)
   1699     {
   1700         UEV_PARSE("diff_cu_qp_delta_depth", value, ps_bitstrm);
   1701         ps_pps->i1_diff_cu_qp_delta_depth = value;
   1702     }
   1703     else
   1704     {
   1705         ps_pps->i1_diff_cu_qp_delta_depth = 0;
   1706     }
   1707     ps_pps->i1_log2_min_cu_qp_delta_size = ps_sps->i1_log2_ctb_size - ps_pps->i1_diff_cu_qp_delta_depth;
   1708     /* Print different */
   1709     SEV_PARSE("cb_qp_offset", value, ps_bitstrm);
   1710     ps_pps->i1_pic_cb_qp_offset = value;
   1711 
   1712     /* Print different */
   1713     SEV_PARSE("cr_qp_offset", value, ps_bitstrm);
   1714     ps_pps->i1_pic_cr_qp_offset = value;
   1715 
   1716     /* Print different */
   1717     BITS_PARSE("slicelevel_chroma_qp_flag", value, ps_bitstrm, 1);
   1718     ps_pps->i1_pic_slice_level_chroma_qp_offsets_present_flag = value;
   1719 
   1720     BITS_PARSE("weighted_pred_flag", value, ps_bitstrm, 1);
   1721     ps_pps->i1_weighted_pred_flag = value;
   1722 
   1723     BITS_PARSE("weighted_bipred_flag", value, ps_bitstrm, 1);
   1724     ps_pps->i1_weighted_bipred_flag = value;
   1725 
   1726     BITS_PARSE("transquant_bypass_enable_flag", value, ps_bitstrm, 1);
   1727     ps_pps->i1_transquant_bypass_enable_flag = value;
   1728 
   1729     BITS_PARSE("tiles_enabled_flag", value, ps_bitstrm, 1);
   1730     ps_pps->i1_tiles_enabled_flag = value;
   1731 
   1732     BITS_PARSE("entropy_coding_sync_enabled_flag", value, ps_bitstrm, 1);
   1733     ps_pps->i1_entropy_coding_sync_enabled_flag = value;
   1734 
   1735     ps_pps->i1_loop_filter_across_tiles_enabled_flag = 0;
   1736     if(ps_pps->i1_tiles_enabled_flag)
   1737     {
   1738         UEV_PARSE("num_tile_columns_minus1", value, ps_bitstrm);
   1739         ps_pps->i1_num_tile_columns = value + 1;
   1740 
   1741         UEV_PARSE("num_tile_rows_minus1", value, ps_bitstrm);
   1742         ps_pps->i1_num_tile_rows = value + 1;
   1743 
   1744         if((ps_pps->i1_num_tile_columns < 1) ||
   1745                         (ps_pps->i1_num_tile_columns > ps_sps->i2_pic_wd_in_ctb) ||
   1746                         (ps_pps->i1_num_tile_rows < 1) ||
   1747                         (ps_pps->i1_num_tile_rows > ps_sps->i2_pic_ht_in_ctb))
   1748             return IHEVCD_INVALID_HEADER;
   1749 
   1750         BITS_PARSE("uniform_spacing_flag", value, ps_bitstrm, 1);
   1751         ps_pps->i1_uniform_spacing_flag = value;
   1752 
   1753 
   1754         {
   1755 
   1756             WORD32 start;
   1757             WORD32 i, j;
   1758 
   1759 
   1760             start = 0;
   1761             for(i = 0; i < ps_pps->i1_num_tile_columns; i++)
   1762             {
   1763                 tile_t *ps_tile;
   1764                 if(!ps_pps->i1_uniform_spacing_flag)
   1765                 {
   1766                     if(i < (ps_pps->i1_num_tile_columns - 1))
   1767                     {
   1768                         UEV_PARSE("column_width_minus1[ i ]", value, ps_bitstrm);
   1769                         value += 1;
   1770                     }
   1771                     else
   1772                     {
   1773                         value = ps_sps->i2_pic_wd_in_ctb - start;
   1774                     }
   1775                 }
   1776                 else
   1777                 {
   1778                     value = ((i + 1) * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns -
   1779                                     (i * ps_sps->i2_pic_wd_in_ctb) / ps_pps->i1_num_tile_columns;
   1780                 }
   1781 
   1782                 for(j = 0; j < ps_pps->i1_num_tile_rows; j++)
   1783                 {
   1784                     ps_tile = ps_pps->ps_tile + j * ps_pps->i1_num_tile_columns + i;
   1785                     ps_tile->u1_pos_x = start;
   1786                     ps_tile->u2_wd = value;
   1787                 }
   1788                 start += value;
   1789 
   1790                 if((start > ps_sps->i2_pic_wd_in_ctb) ||
   1791                                 (value <= 0))
   1792                     return IHEVCD_INVALID_HEADER;
   1793             }
   1794 
   1795             start = 0;
   1796             for(i = 0; i < (ps_pps->i1_num_tile_rows); i++)
   1797             {
   1798                 tile_t *ps_tile;
   1799                 if(!ps_pps->i1_uniform_spacing_flag)
   1800                 {
   1801                     if(i < (ps_pps->i1_num_tile_rows - 1))
   1802                     {
   1803 
   1804                         UEV_PARSE("row_height_minus1[ i ]", value, ps_bitstrm);
   1805                         value += 1;
   1806                     }
   1807                     else
   1808                     {
   1809                         value = ps_sps->i2_pic_ht_in_ctb - start;
   1810                     }
   1811                 }
   1812                 else
   1813                 {
   1814                     value = ((i + 1) * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows -
   1815                                     (i * ps_sps->i2_pic_ht_in_ctb) / ps_pps->i1_num_tile_rows;
   1816                 }
   1817 
   1818                 for(j = 0; j < ps_pps->i1_num_tile_columns; j++)
   1819                 {
   1820                     ps_tile = ps_pps->ps_tile + i * ps_pps->i1_num_tile_columns + j;
   1821                     ps_tile->u1_pos_y = start;
   1822                     ps_tile->u2_ht = value;
   1823                 }
   1824                 start += value;
   1825 
   1826                 if((start > ps_sps->i2_pic_ht_in_ctb) ||
   1827                                 (value <= 0))
   1828                     return IHEVCD_INVALID_HEADER;
   1829             }
   1830         }
   1831 
   1832 
   1833         BITS_PARSE("loop_filter_across_tiles_enabled_flag", value, ps_bitstrm, 1);
   1834         ps_pps->i1_loop_filter_across_tiles_enabled_flag = value;
   1835 
   1836     }
   1837     else
   1838     {
   1839         /* If tiles are not present, set first tile in each PPS to have tile
   1840         width and height equal to picture width and height */
   1841         ps_pps->i1_num_tile_columns = 1;
   1842         ps_pps->i1_num_tile_rows = 1;
   1843         ps_pps->i1_uniform_spacing_flag = 1;
   1844 
   1845         ps_pps->ps_tile->u1_pos_x = 0;
   1846         ps_pps->ps_tile->u1_pos_y = 0;
   1847         ps_pps->ps_tile->u2_wd = ps_sps->i2_pic_wd_in_ctb;
   1848         ps_pps->ps_tile->u2_ht = ps_sps->i2_pic_ht_in_ctb;
   1849     }
   1850 
   1851     BITS_PARSE("loop_filter_across_slices_enabled_flag", value, ps_bitstrm, 1);
   1852     ps_pps->i1_loop_filter_across_slices_enabled_flag = value;
   1853 
   1854     BITS_PARSE("deblocking_filter_control_present_flag", value, ps_bitstrm, 1);
   1855     ps_pps->i1_deblocking_filter_control_present_flag = value;
   1856 
   1857     /* Default values */
   1858     ps_pps->i1_pic_disable_deblocking_filter_flag = 0;
   1859     ps_pps->i1_deblocking_filter_override_enabled_flag = 0;
   1860     ps_pps->i1_beta_offset_div2 = 0;
   1861     ps_pps->i1_tc_offset_div2 = 0;
   1862 
   1863     if(ps_pps->i1_deblocking_filter_control_present_flag)
   1864     {
   1865 
   1866         BITS_PARSE("deblocking_filter_override_enabled_flag", value, ps_bitstrm, 1);
   1867         ps_pps->i1_deblocking_filter_override_enabled_flag = value;
   1868 
   1869         BITS_PARSE("pic_disable_deblocking_filter_flag", value, ps_bitstrm, 1);
   1870         ps_pps->i1_pic_disable_deblocking_filter_flag = value;
   1871 
   1872         if(!ps_pps->i1_pic_disable_deblocking_filter_flag)
   1873         {
   1874 
   1875             SEV_PARSE("pps_beta_offset_div2", value, ps_bitstrm);
   1876             ps_pps->i1_beta_offset_div2 = value;
   1877 
   1878             SEV_PARSE("pps_tc_offset_div2", value, ps_bitstrm);
   1879             ps_pps->i1_tc_offset_div2 = value;
   1880 
   1881         }
   1882     }
   1883 
   1884     BITS_PARSE("pps_scaling_list_data_present_flag", value, ps_bitstrm, 1);
   1885     ps_pps->i1_pps_scaling_list_data_present_flag = value;
   1886 
   1887     if(ps_pps->i1_pps_scaling_list_data_present_flag)
   1888     {
   1889         COPY_DEFAULT_SCALING_LIST(ps_pps->pi2_scaling_mat);
   1890         ihevcd_scaling_list_data(ps_codec, ps_pps->pi2_scaling_mat);
   1891     }
   1892 
   1893     BITS_PARSE("lists_modification_present_flag", value, ps_bitstrm, 1);
   1894     ps_pps->i1_lists_modification_present_flag = value;
   1895     UEV_PARSE("log2_parallel_merge_level_minus2", value, ps_bitstrm);
   1896     ps_pps->i1_log2_parallel_merge_level = value + 2;
   1897 
   1898     BITS_PARSE("slice_header_extension_present_flag", value, ps_bitstrm, 1);
   1899     ps_pps->i1_slice_header_extension_present_flag = value;
   1900     /* Not present in HM */
   1901     BITS_PARSE("pps_extension_flag", value, ps_bitstrm, 1);
   1902 
   1903     ps_codec->i4_pps_done = 1;
   1904     return ret;
   1905 }
   1906 
   1907 
   1908 void ihevcd_copy_pps(codec_t *ps_codec, WORD32 pps_id, WORD32 pps_id_ref)
   1909 {
   1910     pps_t *ps_pps, *ps_pps_ref;
   1911     WORD16 *pi2_scaling_mat_backup;
   1912     WORD32 scaling_mat_size;
   1913     tile_t *ps_tile_backup;
   1914     WORD32 max_tile_cols, max_tile_rows;
   1915 
   1916     SCALING_MAT_SIZE(scaling_mat_size);
   1917     max_tile_cols = (ps_codec->i4_max_wd + MIN_TILE_WD - 1) / MIN_TILE_WD;
   1918     max_tile_rows = (ps_codec->i4_max_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