Home | History | Annotate | Download | only in decoder
      1 /******************************************************************************
      2  *
      3  * Copyright (C) 2015 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at:
      8  *
      9  * http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  *****************************************************************************
     18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
     19 */
     20 /*!
     21  **************************************************************************
     22  * \file ih264d_parse_bslice.c
     23  *
     24  * \brief
     25  *    Contains routines that decode a I slice type
     26  *
     27  * Detailed_description
     28  *
     29  * \date
     30  *    07/07/2003
     31  *
     32  * \author  NS
     33  **************************************************************************
     34  */
     35 
     36 #include <string.h>
     37 #include "ih264d_bitstrm.h"
     38 #include "ih264d_defs.h"
     39 #include "ih264d_debug.h"
     40 #include "ih264d_tables.h"
     41 #include "ih264d_structs.h"
     42 #include "ih264d_defs.h"
     43 #include "ih264d_parse_cavlc.h"
     44 #include "ih264d_mb_utils.h"
     45 #include "ih264d_parse_slice.h"
     46 #include "ih264d_process_intra_mb.h"
     47 #include "ih264d_mvpred.h"
     48 #include "ih264d_parse_islice.h"
     49 #include "ih264d_inter_pred.h"
     50 #include "ih264d_process_pslice.h"
     51 #include "ih264d_process_bslice.h"
     52 #include "ih264d_deblocking.h"
     53 #include "ih264d_cabac.h"
     54 #include "ih264d_parse_mb_header.h"
     55 #include "ih264d_error_handler.h"
     56 #include "ih264d_mvpred.h"
     57 #include "ih264d_cabac.h"
     58 #include "ih264d_utils.h"
     59 
     60 void ih264d_init_cabac_contexts(UWORD8 u1_slice_type, dec_struct_t * ps_dec);
     61 
     62 /*!
     63  **************************************************************************
     64  * \if Function name : ParseMb_SubMb_PredBCav\endif
     65  *
     66  * \brief
     67  *    Implements sub_mb_pred() of 7.3.5.2. & mb_pred() of 7.3.5.1
     68  *
     69  * \return
     70  *    None.
     71  *
     72  **************************************************************************
     73  */
     74 WORD32 ih264d_parse_bmb_non_direct_cavlc(dec_struct_t * ps_dec,
     75                                        dec_mb_info_t * ps_cur_mb_info,
     76                                        UWORD8 u1_mb_num,
     77                                        UWORD8 u1_num_mbsNby2)
     78 {
     79     dec_bit_stream_t * ps_bitstrm = ps_dec->ps_bitstrm;
     80     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
     81     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
     82     UWORD8 * pu1_sub_mb_pred_modes = (UWORD8 *)(gau1_ih264d_submb_pred_modes) + 4;
     83     const UWORD8 (*pu1_mb_pred_modes)[32] =
     84                     (const UWORD8 (*)[32])gau1_ih264d_mb_pred_modes;
     85     const UWORD8 * pu1_num_mb_part = (const UWORD8 *)gau1_ih264d_num_mb_part;
     86     const UWORD8 * pu1_sub_mb_mc_mode = (const UWORD8 *)(gau1_ih264d_submb_mc_mode)
     87                     + 4;
     88 
     89     parse_pmbarams_t * ps_parse_mb_data = ps_dec->ps_parse_mb_data
     90                     + u1_num_mbsNby2;
     91     UWORD8 * pu1_col_info = ps_parse_mb_data->u1_col_info;
     92     WORD8 (*pi1_ref_idx)[MAX_REFIDX_INFO_PER_MB] = ps_parse_mb_data->i1_ref_idx;
     93     UWORD8 u1_mb_type = ps_cur_mb_info->u1_mb_type;
     94     UWORD8 u1_mb_mc_mode, u1_num_mb_part, u1_sub_mb = !(u1_mb_type ^ B_8x8);
     95     UWORD32 u4_mb_mc_mode = 0, u4_mb_pred_mode = 0;
     96     WORD32 ret;
     97 
     98     if(u1_sub_mb)
     99     {
    100         UWORD8 uc_i;
    101         u1_mb_mc_mode = 0;
    102         u1_num_mb_part = 4;
    103         /* Reading the subMB type */
    104         for(uc_i = 0; uc_i < 4; uc_i++)
    105         {
    106 
    107             UWORD32 ui_sub_mb_mode;
    108 
    109 //Inlined ih264d_uev
    110             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
    111             UWORD32 u4_word, u4_ldz;
    112 
    113             /***************************************************************/
    114             /* Find leading zeros in next 32 bits                          */
    115             /***************************************************************/
    116             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
    117             u4_ldz = CLZ(u4_word);
    118             /* Flush the ps_bitstrm */
    119             u4_bitstream_offset += (u4_ldz + 1);
    120             /* Read the suffix from the ps_bitstrm */
    121             u4_word = 0;
    122             if(u4_ldz)
    123                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
    124                         u4_ldz);
    125             *pu4_bitstrm_ofst = u4_bitstream_offset;
    126             ui_sub_mb_mode = ((1 << u4_ldz) + u4_word - 1);
    127 //Inlined ih264d_uev
    128 
    129             if(ui_sub_mb_mode > 12)
    130                 return ERROR_SUB_MB_TYPE;
    131             else
    132             {
    133                 UWORD8 u1_subMbPredMode = pu1_sub_mb_pred_modes[ui_sub_mb_mode];
    134                 u4_mb_mc_mode = (u4_mb_mc_mode << 8)
    135                                 | pu1_sub_mb_mc_mode[ui_sub_mb_mode];
    136                 u4_mb_pred_mode = (u4_mb_pred_mode << 8) | u1_subMbPredMode;
    137                 pi1_ref_idx[0][uc_i] = ((u1_subMbPredMode & PRED_L0) - 1) >> 1;
    138                 pi1_ref_idx[1][uc_i] = ((u1_subMbPredMode & PRED_L1) - 1) >> 1;
    139                 COPYTHECONTEXT("sub_mb_type", u1_subMbPredMode);
    140             }
    141             /* Storing collocated Mb and SubMb mode information */
    142             *pu1_col_info++ = ((PRED_8x8) << 6)
    143                             | ((pu1_sub_mb_mc_mode[ui_sub_mb_mode] << 4));
    144             if(ui_sub_mb_mode != B_DIRECT_8x8)
    145             {
    146                 if(ui_sub_mb_mode > B_BI_8x8)
    147                 {
    148                     ps_dec->s_high_profile.u1_no_submb_part_size_lt8x8_flag = 0;
    149                 }
    150             }
    151             else if(!ps_dec->s_high_profile.u1_direct_8x8_inference_flag)
    152             {
    153                 ps_dec->s_high_profile.u1_no_submb_part_size_lt8x8_flag = 0;
    154             }
    155         }
    156     }
    157     else
    158     {
    159         UWORD8 u1_mb_pred_mode_idx = 5 + u1_mb_type;
    160         UWORD8 u1_mb_pred_mode_part0 = pu1_mb_pred_modes[0][u1_mb_pred_mode_idx];
    161         UWORD8 u1_mb_pred_mode_part1 = pu1_mb_pred_modes[1][u1_mb_pred_mode_idx];
    162         u1_mb_mc_mode = ps_cur_mb_info->u1_mb_mc_mode;
    163         u1_num_mb_part = pu1_num_mb_part[u1_mb_mc_mode];
    164 
    165         pi1_ref_idx[0][0] = ((u1_mb_pred_mode_part0 & PRED_L0) - 1) >> 1;
    166         pi1_ref_idx[1][0] = ((u1_mb_pred_mode_part0 & PRED_L1) - 1) >> 1;
    167         pi1_ref_idx[0][1] = ((u1_mb_pred_mode_part1 & PRED_L0) - 1) >> 1;
    168         pi1_ref_idx[1][1] = ((u1_mb_pred_mode_part1 & PRED_L1) - 1) >> 1;
    169 
    170         u4_mb_pred_mode = (u1_mb_pred_mode_part0 << 8) | u1_mb_pred_mode_part1;
    171         u4_mb_mc_mode = u1_mb_mc_mode | (u1_mb_mc_mode << 8);
    172         u4_mb_mc_mode <<= 16;
    173         u4_mb_pred_mode <<= 16;
    174 
    175         /* Storing collocated Mb and SubMb mode information */
    176         *pu1_col_info++ = (u1_mb_mc_mode << 6);
    177         if(u1_mb_mc_mode)
    178             *pu1_col_info++ = (u1_mb_mc_mode << 6);
    179     }
    180 
    181     {
    182         UWORD8 u1_mbaff = ps_dec->ps_cur_slice->u1_mbaff_frame_flag;
    183         UWORD8 uc_field = ps_cur_mb_info->u1_mb_field_decodingflag;
    184         UWORD8 *pu1_num_ref_idx_lx_active =
    185                         ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active;
    186         const UWORD8 u1_mbaff_field = (u1_mbaff & uc_field);
    187         UWORD8 u4_num_ref_idx_lx_active;
    188 
    189         u4_num_ref_idx_lx_active = (pu1_num_ref_idx_lx_active[0]
    190                         << u1_mbaff_field) - 1;
    191 
    192         if(u4_num_ref_idx_lx_active)
    193         {
    194             if(1 == u4_num_ref_idx_lx_active)
    195                 ih264d_parse_bmb_ref_index_cavlc_range1(
    196                                 u1_num_mb_part, ps_bitstrm, pi1_ref_idx[0],
    197                                 u4_num_ref_idx_lx_active);
    198             else
    199             {
    200                 ret = ih264d_parse_bmb_ref_index_cavlc(u1_num_mb_part, ps_bitstrm,
    201                                                  pi1_ref_idx[0],
    202                                                  u4_num_ref_idx_lx_active);
    203                 if(ret != OK)
    204                     return ret;
    205             }
    206         }
    207 
    208         u4_num_ref_idx_lx_active = (pu1_num_ref_idx_lx_active[1]
    209                         << u1_mbaff_field) - 1;
    210 
    211         if(u4_num_ref_idx_lx_active)
    212         {
    213             if(1 == u4_num_ref_idx_lx_active)
    214                 ih264d_parse_bmb_ref_index_cavlc_range1(
    215                                 u1_num_mb_part, ps_bitstrm, pi1_ref_idx[1],
    216                                 u4_num_ref_idx_lx_active);
    217             else
    218             {
    219                 ret = ih264d_parse_bmb_ref_index_cavlc(u1_num_mb_part, ps_bitstrm,
    220                                                  pi1_ref_idx[1],
    221                                                  u4_num_ref_idx_lx_active);
    222                 if(ret != OK)
    223                     return ret;
    224             }
    225         }
    226     }
    227 
    228     /* Read MotionVectors */
    229     {
    230         const UWORD8 * pu1_top_left_sub_mb_indx;
    231 
    232         const UWORD8 * pu1_sub_mb_indx_mod =
    233                         (const UWORD8 *)(gau1_ih264d_submb_indx_mod)
    234                                         + (u1_sub_mb * 6);
    235         const UWORD8 * pu1_sub_mb_partw = (const UWORD8 *)gau1_ih264d_submb_partw;
    236         const UWORD8 * pu1_sub_mb_parth = (const UWORD8 *)gau1_ih264d_submb_parth;
    237         const UWORD8 * pu1_num_sub_mb_part =
    238                         (const UWORD8 *)gau1_ih264d_num_submb_part;
    239         const UWORD8 * pu1_mb_partw = (const UWORD8 *)gau1_ih264d_mb_partw;
    240         const UWORD8 * pu1_mb_parth = (const UWORD8 *)gau1_ih264d_mb_parth;
    241         UWORD8 u1_p_idx = 0, u1_num_submb_part, uc_lx;
    242         parse_part_params_t * ps_part;
    243         mv_pred_t *ps_mv_start = ps_dec->ps_mv_cur + (u1_mb_num << 4);
    244         UWORD8 u1_mb_part_wd, u1_mb_part_ht;
    245 
    246         /* Initialisations */
    247         ps_part = ps_dec->ps_part;
    248         /* Default Initialization for Non subMb Case Mode */
    249         u1_mb_part_wd = pu1_mb_partw[u1_mb_mc_mode];
    250         u1_mb_part_ht = pu1_mb_parth[u1_mb_mc_mode];
    251         u1_num_submb_part = 1;
    252 
    253         /* Decoding the MV for the subMB */
    254         for(uc_lx = 0; uc_lx < 2; uc_lx++)
    255         {
    256             UWORD8 u1_sub_mb_num = 0, u1_pred_mode, uc_i;
    257             UWORD32 u4_mb_mc_mode_tmp = u4_mb_mc_mode;
    258             UWORD32 u4_mb_pred_mode_tmp = u4_mb_pred_mode;
    259             UWORD16 u2_sub_mb_num = 0x028A; // for sub mb case
    260             UWORD8 u1_b2 = uc_lx << 1;
    261             u1_pred_mode = (uc_lx) ? PRED_L1 : PRED_L0;
    262             pu1_top_left_sub_mb_indx = pu1_sub_mb_indx_mod + (u1_mb_mc_mode << 1);
    263 
    264             for(uc_i = 0; uc_i < u1_num_mb_part; uc_i++)
    265             {
    266                 UWORD8 u1_mb_mc_mode, uc_j;
    267                 UWORD8 i1_pred = u4_mb_pred_mode_tmp >> 24;
    268                 u1_mb_mc_mode = u4_mb_mc_mode_tmp >> 24;
    269                 u4_mb_pred_mode_tmp <<= 8;
    270                 u4_mb_mc_mode_tmp <<= 8;
    271                 /* subMb prediction mode */
    272                 if(u1_sub_mb)
    273                 {
    274 
    275                     u1_mb_part_wd = pu1_sub_mb_partw[u1_mb_mc_mode];
    276                     u1_mb_part_ht = pu1_sub_mb_parth[u1_mb_mc_mode];
    277                     u1_sub_mb_num = u2_sub_mb_num >> 12;
    278                     u1_num_submb_part = pu1_num_sub_mb_part[u1_mb_mc_mode];
    279                     pu1_top_left_sub_mb_indx = pu1_sub_mb_indx_mod
    280                                     + (u1_mb_mc_mode << 1);
    281                     u2_sub_mb_num <<= 4;
    282                 }
    283                 for(uc_j = 0; uc_j < u1_num_submb_part;
    284                                 uc_j++, pu1_top_left_sub_mb_indx++)
    285                 {
    286                     mv_pred_t * ps_mv;
    287                     u1_sub_mb_num = u1_sub_mb_num + *pu1_top_left_sub_mb_indx;
    288                     ps_mv = ps_mv_start + u1_sub_mb_num;
    289 
    290                     /* Storing Info for partitions, writing only once */
    291                     if(uc_lx)
    292                     {
    293                         ps_part->u1_is_direct = (!i1_pred);
    294                         ps_part->u1_pred_mode = i1_pred;
    295                         ps_part->u1_sub_mb_num = u1_sub_mb_num;
    296                         ps_part->u1_partheight = u1_mb_part_ht;
    297                         ps_part->u1_partwidth = u1_mb_part_wd;
    298                         /* Increment partition Index */
    299                         u1_p_idx++;
    300                         ps_part++;
    301                     }
    302 
    303                     if(i1_pred & u1_pred_mode)
    304                     {
    305                         WORD16 i2_mvx, i2_mvy;
    306 
    307 //inlining ih264d_sev
    308                         {
    309                             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
    310                             UWORD32 u4_word, u4_ldz, u4_abs_val;
    311 
    312                             /***************************************************************/
    313                             /* Find leading zeros in next 32 bits                          */
    314                             /***************************************************************/
    315                             NEXTBITS_32(u4_word, u4_bitstream_offset,
    316                                         pu4_bitstrm_buf);
    317                             u4_ldz = CLZ(u4_word);
    318 
    319                             /* Flush the ps_bitstrm */
    320                             u4_bitstream_offset += (u4_ldz + 1);
    321 
    322                             /* Read the suffix from the ps_bitstrm */
    323                             u4_word = 0;
    324                             if(u4_ldz)
    325                                 GETBITS(u4_word, u4_bitstream_offset,
    326                                         pu4_bitstrm_buf, u4_ldz);
    327 
    328                             *pu4_bitstrm_ofst = u4_bitstream_offset;
    329                             u4_abs_val = ((1 << u4_ldz) + u4_word) >> 1;
    330 
    331                             if(u4_word & 0x1)
    332                                 i2_mvx = (-(WORD32)u4_abs_val);
    333                             else
    334                                 i2_mvx = (u4_abs_val);
    335                         }
    336 //inlinined ih264d_sev
    337 
    338 //inlining ih264d_sev
    339                         {
    340                             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
    341                             UWORD32 u4_word, u4_ldz, u4_abs_val;
    342 
    343                             /***************************************************************/
    344                             /* Find leading zeros in next 32 bits                          */
    345                             /***************************************************************/
    346                             NEXTBITS_32(u4_word, u4_bitstream_offset,
    347                                         pu4_bitstrm_buf);
    348                             u4_ldz = CLZ(u4_word);
    349 
    350                             /* Flush the ps_bitstrm */
    351                             u4_bitstream_offset += (u4_ldz + 1);
    352 
    353                             /* Read the suffix from the ps_bitstrm */
    354                             u4_word = 0;
    355                             if(u4_ldz)
    356                                 GETBITS(u4_word, u4_bitstream_offset,
    357                                         pu4_bitstrm_buf, u4_ldz);
    358 
    359                             *pu4_bitstrm_ofst = u4_bitstream_offset;
    360                             u4_abs_val = ((1 << u4_ldz) + u4_word) >> 1;
    361 
    362                             if(u4_word & 0x1)
    363                                 i2_mvy = (-(WORD32)u4_abs_val);
    364                             else
    365                                 i2_mvy = (u4_abs_val);
    366                         }
    367 //inlinined ih264d_sev
    368 
    369                         /* Storing Mv residuals */
    370                         ps_mv->i2_mv[u1_b2] = i2_mvx;
    371                         ps_mv->i2_mv[u1_b2 + 1] = i2_mvy;
    372                     }
    373                 }
    374             }
    375         }
    376         /* write back to the scratch partition info */
    377         ps_dec->ps_part = ps_part;
    378         ps_parse_mb_data->u1_num_part = u1_sub_mb ? u1_p_idx : u1_num_mb_part;
    379 
    380     }
    381     return OK;
    382 }
    383 
    384 /*!
    385  **************************************************************************
    386  * \if Function name : ParseMb_SubMb_PredBCab\endif
    387  *
    388  * \brief
    389  *    Implements sub_mb_pred() of 7.3.5.2. & mb_pred() of 7.3.5.1
    390  *
    391  * \return
    392  *    None.
    393  *
    394  **************************************************************************
    395  */
    396 
    397 WORD32 ih264d_parse_bmb_non_direct_cabac(dec_struct_t * ps_dec,
    398                                        dec_mb_info_t * ps_cur_mb_info,
    399                                        UWORD8 u1_mb_num,
    400                                        UWORD8 u1_num_mbsNby2)
    401 {
    402     /* Loads from ps_dec */
    403     decoding_envirnoment_t * ps_cab_env = &ps_dec->s_cab_dec_env;
    404     dec_bit_stream_t * ps_bitstrm = ps_dec->ps_bitstrm;
    405     ctxt_inc_mb_info_t *p_curr_ctxt = ps_dec->ps_curr_ctxt_mb_info;
    406     parse_pmbarams_t * ps_parse_mb_data = ps_dec->ps_parse_mb_data
    407                     + u1_num_mbsNby2;
    408 
    409     /* table pointer loads */
    410     const UWORD8 * pu1_sub_mb_pred_modes = (UWORD8 *)(gau1_ih264d_submb_pred_modes)
    411                     + 4;
    412     const UWORD8 (*pu1_mb_pred_modes)[32] =
    413                     (const UWORD8 (*)[32])gau1_ih264d_mb_pred_modes;
    414     const UWORD8 *pu1_num_mb_part = (const UWORD8 *)gau1_ih264d_num_mb_part;
    415     const UWORD8 *pu1_sub_mb_mc_mode = (UWORD8 *)(gau1_ih264d_submb_mc_mode) + 4;
    416 
    417     const UWORD8 u1_mb_type = ps_cur_mb_info->u1_mb_type;
    418     UWORD8 * pu1_col_info = ps_parse_mb_data->u1_col_info;
    419     WORD8 *pi1_ref_idx_l0 = &ps_parse_mb_data->i1_ref_idx[0][0];
    420     WORD8 *pi1_ref_idx_l1 = &ps_parse_mb_data->i1_ref_idx[1][0];
    421     UWORD8 u1_dec_ref_l0, u1_dec_ref_l1;
    422 
    423     UWORD8 u1_num_mb_part, u1_mb_mc_mode, u1_sub_mb, u1_mbpred_mode = 5
    424                     + u1_mb_type;
    425     UWORD32 u4_mb_mc_mode = 0, u4_mb_pred_mode = 0;
    426     WORD32 ret;
    427 
    428     p_curr_ctxt->u1_mb_type = CAB_NON_BD16x16;
    429     u1_sub_mb = !(u1_mb_type ^ B_8x8);
    430 
    431     {
    432         UWORD8 u1_mbaff = ps_dec->ps_cur_slice->u1_mbaff_frame_flag;
    433         UWORD8 *pu1_num_ref_idx_lx_active =
    434                         ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active;
    435         UWORD8 uc_field = ps_cur_mb_info->u1_mb_field_decodingflag;
    436         UWORD8 u1_mbaff_field = (u1_mbaff & uc_field);
    437         u1_dec_ref_l0 = (pu1_num_ref_idx_lx_active[0] << u1_mbaff_field) - 1;
    438         u1_dec_ref_l1 = (pu1_num_ref_idx_lx_active[1] << u1_mbaff_field) - 1;
    439     }
    440 
    441     if(u1_sub_mb)
    442     {
    443         const UWORD8 u1_colz = ((PRED_8x8) << 6);
    444         UWORD8 uc_i;
    445         u1_mb_mc_mode = 0;
    446         u1_num_mb_part = 4;
    447         /* Reading the subMB type */
    448         for(uc_i = 0; uc_i < 4; uc_i++)
    449         {
    450             UWORD8 u1_sub_mb_mode, u1_subMbPredModes;
    451             u1_sub_mb_mode = ih264d_parse_submb_type_cabac(
    452                             1, ps_cab_env, ps_bitstrm,
    453                             ps_dec->p_sub_mb_type_t);
    454 
    455             if(u1_sub_mb_mode > 12)
    456                 return ERROR_SUB_MB_TYPE;
    457 
    458             u1_subMbPredModes = pu1_sub_mb_pred_modes[u1_sub_mb_mode];
    459             u4_mb_mc_mode = (u4_mb_mc_mode << 8) | pu1_sub_mb_mc_mode[u1_sub_mb_mode];
    460             u4_mb_pred_mode = (u4_mb_pred_mode << 8) | u1_subMbPredModes;
    461             *pi1_ref_idx_l0++ =
    462                             (u1_subMbPredModes & PRED_L0) ? u1_dec_ref_l0 : -1;
    463             *pi1_ref_idx_l1++ =
    464                             (u1_subMbPredModes & PRED_L1) ? u1_dec_ref_l1 : -1;
    465             COPYTHECONTEXT("sub_mb_type", u1_sub_mb_mode);
    466             /* Storing collocated Mb and SubMb mode information */
    467             *pu1_col_info++ =
    468                             (u1_colz | (pu1_sub_mb_mc_mode[u1_sub_mb_mode] << 4));
    469             if(u1_sub_mb_mode != B_DIRECT_8x8)
    470             {
    471                 if(u1_sub_mb_mode > B_BI_8x8)
    472                 {
    473                     ps_dec->s_high_profile.u1_no_submb_part_size_lt8x8_flag = 0;
    474                 }
    475             }
    476             else if(!ps_dec->s_high_profile.u1_direct_8x8_inference_flag)
    477             {
    478                 ps_dec->s_high_profile.u1_no_submb_part_size_lt8x8_flag = 0;
    479             }
    480         }
    481         pi1_ref_idx_l0 -= 4;
    482         pi1_ref_idx_l1 -= 4;
    483     }
    484     else
    485     {
    486         UWORD8 u1_mb_pred_mode_part0 = pu1_mb_pred_modes[0][u1_mbpred_mode];
    487         UWORD8 u1_mb_pred_mode_part1 = pu1_mb_pred_modes[1][u1_mbpred_mode];
    488         u1_mb_mc_mode = ps_cur_mb_info->u1_mb_mc_mode;
    489         u1_num_mb_part = pu1_num_mb_part[u1_mb_mc_mode];
    490         /* Storing collocated Mb and SubMb mode information */
    491         *pu1_col_info++ = (u1_mb_mc_mode << 6);
    492         if(u1_mb_mc_mode)
    493             *pu1_col_info++ = (u1_mb_mc_mode << 6);
    494         u4_mb_mc_mode = u1_mb_mc_mode | (u1_mb_mc_mode << 8);
    495         u4_mb_mc_mode <<= 16;
    496         u4_mb_pred_mode = ((u1_mb_pred_mode_part0 << 8) | u1_mb_pred_mode_part1) << 16;
    497 
    498         *pi1_ref_idx_l0++ = (u1_mb_pred_mode_part0 & PRED_L0) ? u1_dec_ref_l0 : -1;
    499         *pi1_ref_idx_l0-- = (u1_mb_pred_mode_part1 & PRED_L0) ? u1_dec_ref_l0 : -1;
    500         *pi1_ref_idx_l1++ = (u1_mb_pred_mode_part0 & PRED_L1) ? u1_dec_ref_l1 : -1;
    501         *pi1_ref_idx_l1-- = (u1_mb_pred_mode_part1 & PRED_L1) ? u1_dec_ref_l1 : -1;
    502     }
    503     {
    504         WORD8 *pi1_lft_cxt = ps_dec->pi1_left_ref_idx_ctxt_inc;
    505         WORD8 *pi1_top_cxt = p_curr_ctxt->i1_ref_idx;
    506 
    507         ret = ih264d_parse_ref_idx_cabac(u1_num_mb_part, 0, u1_dec_ref_l0,
    508                                    u1_mb_mc_mode, pi1_ref_idx_l0, pi1_lft_cxt,
    509                                    pi1_top_cxt, ps_cab_env, ps_bitstrm,
    510                                    ps_dec->p_ref_idx_t);
    511         if(ret != OK)
    512             return ret;
    513 
    514         ret = ih264d_parse_ref_idx_cabac(u1_num_mb_part, 2, u1_dec_ref_l1,
    515                                    u1_mb_mc_mode, pi1_ref_idx_l1, pi1_lft_cxt,
    516                                    pi1_top_cxt, ps_cab_env, ps_bitstrm,
    517                                    ps_dec->p_ref_idx_t);
    518         if(ret != OK)
    519             return ret;
    520     }
    521     /* Read MotionVectors */
    522     {
    523         const UWORD8 *pu1_top_left_sub_mb_indx;
    524         UWORD8 uc_j, uc_lx;
    525         UWORD8 u1_mb_part_wd, u1_mb_part_ht;
    526 
    527         const UWORD8 *pu1_sub_mb_indx_mod =
    528                         (const UWORD8 *)gau1_ih264d_submb_indx_mod
    529                                         + (u1_sub_mb * 6);
    530         const UWORD8 *pu1_sub_mb_partw = (const UWORD8 *)gau1_ih264d_submb_partw;
    531         const UWORD8 *pu1_sub_mb_parth = (const UWORD8 *)gau1_ih264d_submb_parth;
    532         const UWORD8 *pu1_num_sub_mb_part =
    533                         (const UWORD8 *)gau1_ih264d_num_submb_part;
    534         const UWORD8 *pu1_mb_partw = (const UWORD8 *)gau1_ih264d_mb_partw;
    535         const UWORD8 *pu1_mb_parth = (const UWORD8 *)gau1_ih264d_mb_parth;
    536 
    537         UWORD8 u1_p_idx = 0;
    538         UWORD8 u1_num_submb_part;
    539         parse_part_params_t *ps_part;
    540         /* Initialisations */
    541         mv_pred_t *ps_mv_start = ps_dec->ps_mv_cur + (u1_mb_num << 4);
    542         ps_part = ps_dec->ps_part;
    543 
    544         /* Default initialization for non subMb case */
    545         u1_mb_part_wd = pu1_mb_partw[u1_mb_mc_mode];
    546         u1_mb_part_ht = pu1_mb_parth[u1_mb_mc_mode];
    547         u1_num_submb_part = 1;
    548 
    549         /* Decoding the MV for the subMB */
    550         for(uc_lx = 0; uc_lx < 2; uc_lx++)
    551         {
    552             UWORD8 u1_sub_mb_num = 0;
    553             UWORD32 u4_mb_pred_mode_tmp = u4_mb_pred_mode;
    554             UWORD32 u4_mb_mc_mode_tmp = u4_mb_mc_mode;
    555             UWORD8 u1_mb_mc_mode_1, u1_pred_mode, uc_i;
    556             UWORD16 u2_sub_mb_num = 0x028A;
    557             UWORD8 u1_b2 = uc_lx << 1;
    558             u1_pred_mode = (uc_lx) ? PRED_L1 : PRED_L0;
    559             /* Default for Cabac */
    560             pu1_top_left_sub_mb_indx = pu1_sub_mb_indx_mod + (u1_mb_mc_mode << 1);
    561             for(uc_i = 0; uc_i < u1_num_mb_part; uc_i++)
    562             {
    563 
    564                 WORD8 i1_pred = (UWORD8)(u4_mb_pred_mode_tmp >> 24);
    565                 u1_mb_mc_mode_1 = (UWORD8)(u4_mb_mc_mode_tmp >> 24);
    566                 u4_mb_pred_mode_tmp <<= 8;
    567                 u4_mb_mc_mode_tmp <<= 8;
    568 
    569                 /* subMb prediction mode */
    570                 if(u1_sub_mb)
    571                 {
    572                     u1_mb_part_wd = pu1_sub_mb_partw[u1_mb_mc_mode_1];
    573                     u1_mb_part_ht = pu1_sub_mb_parth[u1_mb_mc_mode_1];
    574                     u1_sub_mb_num = u2_sub_mb_num >> 12;
    575                     pu1_top_left_sub_mb_indx = pu1_sub_mb_indx_mod + (u1_mb_mc_mode_1 << 1);
    576                     u1_num_submb_part = pu1_num_sub_mb_part[u1_mb_mc_mode_1];
    577                     u2_sub_mb_num = u2_sub_mb_num << 4;
    578                 }
    579 
    580                 for(uc_j = 0; uc_j < u1_num_submb_part;
    581                                 uc_j++, pu1_top_left_sub_mb_indx++)
    582                 {
    583                     mv_pred_t *ps_mv;
    584                     u1_sub_mb_num = u1_sub_mb_num + *pu1_top_left_sub_mb_indx;
    585                     ps_mv = ps_mv_start + u1_sub_mb_num;
    586 
    587                     /* Storing Info for partitions, writing only once */
    588                     if(uc_lx)
    589                     {
    590                         ps_part->u1_is_direct = (!i1_pred);
    591                         ps_part->u1_pred_mode = i1_pred;
    592                         ps_part->u1_sub_mb_num = u1_sub_mb_num;
    593                         ps_part->u1_partheight = u1_mb_part_ht;
    594                         ps_part->u1_partwidth = u1_mb_part_wd;
    595 
    596                         /* Increment partition Index */
    597                         u1_p_idx++;
    598                         ps_part++;
    599                     }
    600 
    601                     ih264d_get_mvd_cabac(u1_sub_mb_num, u1_b2, u1_mb_part_wd,
    602                                          u1_mb_part_ht,
    603                                          (UWORD8)(i1_pred & u1_pred_mode), ps_dec,
    604                                          ps_mv);
    605                 }
    606             }
    607         }
    608         /* write back to the scratch partition info */
    609 
    610         ps_dec->ps_part = ps_part;
    611         ps_parse_mb_data->u1_num_part = u1_sub_mb ? u1_p_idx : u1_num_mb_part;
    612 
    613     }
    614 
    615     return OK;
    616 }
    617 
    618 /*!
    619  **************************************************************************
    620  * \if Function name : ih264d_parse_bmb_cabac \endif
    621  *
    622  * \brief
    623  *    This function parses CABAC syntax of a B MB.
    624  *
    625  * \return
    626  *    0 on Success and Error code otherwise
    627  **************************************************************************
    628  */
    629 WORD32 ih264d_parse_bmb_cabac(dec_struct_t * ps_dec,
    630                               dec_mb_info_t * ps_cur_mb_info,
    631                               UWORD8 u1_mb_num,
    632                               UWORD8 u1_num_mbsNby2)
    633 {
    634     UWORD8 u1_cbp;
    635     deblk_mb_t * ps_cur_deblk_mb = ps_dec->ps_deblk_mbn + u1_mb_num;
    636     const UWORD8 *puc_mb_mc_mode = (const UWORD8 *)gau1_ih264d_mb_mc_mode;
    637     UWORD8 u1_mb_type = ps_cur_mb_info->u1_mb_type;
    638     ctxt_inc_mb_info_t *p_curr_ctxt = ps_dec->ps_curr_ctxt_mb_info;
    639 
    640     WORD32 ret;
    641     UWORD8 u1_Bdirect_tranform_read = 1;
    642     ps_dec->s_high_profile.u1_no_submb_part_size_lt8x8_flag = 1;
    643 
    644     ps_cur_mb_info->u1_mb_mc_mode = puc_mb_mc_mode[5 + u1_mb_type];
    645 
    646     ps_cur_mb_info->u1_yuv_dc_block_flag = 0;
    647 
    648     ps_cur_deblk_mb->u1_mb_type |= D_B_SLICE;
    649     if(u1_mb_type != B_DIRECT)
    650     {
    651         ret = ih264d_parse_bmb_non_direct_cabac(ps_dec, ps_cur_mb_info, u1_mb_num,
    652                                           u1_num_mbsNby2);
    653         if(ret != OK)
    654             return ret;
    655     }
    656     else
    657     {
    658 
    659         /************ STORING PARTITION INFO ***********/
    660         parse_part_params_t * ps_part_info;
    661         ps_part_info = ps_dec->ps_part;
    662         ps_part_info->u1_is_direct = PART_DIRECT_16x16;
    663         ps_part_info->u1_sub_mb_num = 0;
    664         ps_dec->ps_part++;
    665         p_curr_ctxt->u1_mb_type = CAB_BD16x16;
    666 
    667         MEMSET_16BYTES(&ps_dec->pu1_left_mv_ctxt_inc[0][0], 0);
    668         memset(ps_dec->pi1_left_ref_idx_ctxt_inc, 0, 4);
    669         MEMSET_16BYTES(p_curr_ctxt->u1_mv, 0);
    670         memset(p_curr_ctxt->i1_ref_idx, 0, 4);
    671 
    672         /* check whether transform8x8 u4_flag to be read or not */
    673         u1_Bdirect_tranform_read =
    674                         ps_dec->s_high_profile.u1_direct_8x8_inference_flag;
    675     }
    676 
    677     /* Read the Coded block pattern */
    678     u1_cbp = (WORD8)ih264d_parse_ctx_cbp_cabac(ps_dec);
    679     p_curr_ctxt->u1_cbp = u1_cbp;
    680     ps_cur_mb_info->u1_cbp = u1_cbp;
    681 
    682     if(u1_cbp > 47)
    683         return ERROR_CBP;
    684 
    685     COPYTHECONTEXT("coded_block_pattern", u1_cbp);
    686 
    687     ps_cur_mb_info->u1_tran_form8x8 = 0;
    688     ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
    689 
    690     if((ps_dec->s_high_profile.u1_transform8x8_present) && (u1_cbp & (0xf))
    691                     && (ps_dec->s_high_profile.u1_no_submb_part_size_lt8x8_flag)
    692                     && (u1_Bdirect_tranform_read))
    693     {
    694         ps_cur_mb_info->u1_tran_form8x8 = ih264d_parse_transform8x8flag_cabac(
    695                         ps_dec, ps_cur_mb_info);
    696         COPYTHECONTEXT("transform_size_8x8_flag", ps_cur_mb_info->u1_tran_form8x8);
    697 
    698         ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = ps_cur_mb_info->u1_tran_form8x8;
    699         p_curr_ctxt->u1_transform8x8_ctxt = ps_cur_mb_info->u1_tran_form8x8;
    700     }
    701     else
    702     {
    703         p_curr_ctxt->u1_transform8x8_ctxt = 0;
    704     }
    705 
    706     p_curr_ctxt->u1_intra_chroma_pred_mode = 0;
    707     p_curr_ctxt->u1_yuv_dc_csbp &= 0xFE;
    708     ps_dec->pu1_left_yuv_dc_csbp[0] &= 0x6;
    709 
    710     /* Read mb_qp_delta */
    711     if(u1_cbp)
    712     {
    713         WORD8 c_temp;
    714         ret = ih264d_parse_mb_qp_delta_cabac(ps_dec, &c_temp);
    715         if(ret != OK)
    716             return ret;
    717         COPYTHECONTEXT("mb_qp_delta", c_temp);
    718         if(c_temp)
    719         {
    720             ret = ih264d_update_qp(ps_dec, c_temp);
    721             if(ret != OK)
    722                 return ret;
    723         }
    724     }
    725     else
    726         ps_dec->i1_prev_mb_qp_delta = 0;
    727 
    728     ih264d_parse_residual4x4_cabac(ps_dec, ps_cur_mb_info, 0);
    729     if(EXCEED_OFFSET(ps_dec->ps_bitstrm))
    730         return ERROR_EOB_TERMINATE_T;
    731     return OK;
    732 }
    733 /*!
    734  **************************************************************************
    735  * \if Function name : ih264d_parse_bmb_cavlc \endif
    736  *
    737  * \brief
    738  *    This function parses CAVLC syntax of a B MB.
    739  *
    740  * \return
    741  *    0 on Success and Error code otherwise
    742  **************************************************************************
    743  */
    744 WORD32 ih264d_parse_bmb_cavlc(dec_struct_t * ps_dec,
    745                               dec_mb_info_t * ps_cur_mb_info,
    746                               UWORD8 u1_mb_num,
    747                               UWORD8 u1_num_mbsNby2)
    748 {
    749     UWORD32 u4_cbp;
    750     deblk_mb_t * ps_cur_deblk_mb = ps_dec->ps_deblk_mbn + u1_mb_num;
    751     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
    752     UWORD32 * pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
    753     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
    754     const UWORD8 *puc_mb_mc_mode = (const UWORD8 *)gau1_ih264d_mb_mc_mode;
    755     UWORD8 u1_mb_type = ps_cur_mb_info->u1_mb_type;
    756 
    757     WORD32 ret;
    758     UWORD8 u1_Bdirect_tranform_read = 1;
    759     ps_dec->s_high_profile.u1_no_submb_part_size_lt8x8_flag = 1;
    760     ps_cur_mb_info->u1_tran_form8x8 = 0;
    761     ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
    762 
    763     ps_cur_mb_info->u1_yuv_dc_block_flag = 0;
    764 
    765     ps_cur_mb_info->u1_mb_mc_mode = puc_mb_mc_mode[5 + u1_mb_type];
    766 
    767     ps_cur_deblk_mb->u1_mb_type |= D_B_SLICE;
    768     if(u1_mb_type != B_DIRECT)
    769     {
    770         ret = ih264d_parse_bmb_non_direct_cavlc(ps_dec, ps_cur_mb_info, u1_mb_num,
    771                                           u1_num_mbsNby2);
    772         if(ret != OK)
    773             return ret;
    774     }
    775     else
    776     {
    777         /************ STORING PARTITION INFO ***********/
    778         parse_part_params_t * ps_part_info;
    779         ps_part_info = ps_dec->ps_part;
    780         ps_part_info->u1_is_direct = PART_DIRECT_16x16;
    781         ps_part_info->u1_sub_mb_num = 0;
    782         ps_dec->ps_part++;
    783         /* check whether transform8x8 u4_flag to be read or not */
    784         u1_Bdirect_tranform_read =
    785                         ps_dec->s_high_profile.u1_direct_8x8_inference_flag;
    786     }
    787 
    788     /* Read the Coded block pattern */
    789     {
    790         const UWORD8 * puc_CbpInter = gau1_ih264d_cbp_inter;
    791 //Inlined ih264d_uev
    792         UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
    793         UWORD32 u4_word, u4_ldz;
    794 
    795         /***************************************************************/
    796         /* Find leading zeros in next 32 bits                          */
    797         /***************************************************************/
    798         NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
    799         u4_ldz = CLZ(u4_word);
    800         /* Flush the ps_bitstrm */
    801         u4_bitstream_offset += (u4_ldz + 1);
    802         /* Read the suffix from the ps_bitstrm */
    803         u4_word = 0;
    804         if(u4_ldz)
    805             GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf, u4_ldz);
    806         *pu4_bitstrm_ofst = u4_bitstream_offset;
    807         u4_cbp = ((1 << u4_ldz) + u4_word - 1);
    808 //Inlined ih264d_uev
    809         if(u4_cbp > 47)
    810             return ERROR_CBP;
    811         u4_cbp = puc_CbpInter[u4_cbp];
    812 
    813         if((ps_dec->s_high_profile.u1_transform8x8_present) && (u4_cbp & (0xf))
    814                         && (ps_dec->s_high_profile.u1_no_submb_part_size_lt8x8_flag)
    815                         && (u1_Bdirect_tranform_read))
    816         {
    817             ps_cur_mb_info->u1_tran_form8x8 = ih264d_get_bit_h264(ps_bitstrm);
    818             COPYTHECONTEXT("transform_size_8x8_flag", ps_cur_mb_info->u1_tran_form8x8);
    819             ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = ps_cur_mb_info->u1_tran_form8x8;
    820         }
    821 
    822     }
    823 
    824     COPYTHECONTEXT("coded_block_pattern", u4_cbp);
    825     ps_cur_mb_info->u1_cbp = u4_cbp;
    826 
    827     /* Read mb_qp_delta */
    828     if(u4_cbp)
    829     {
    830         WORD32 i_temp;
    831 //inlining ih264d_sev
    832 
    833         UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
    834         UWORD32 u4_word, u4_ldz, u4_abs_val;
    835 
    836         /***************************************************************/
    837         /* Find leading zeros in next 32 bits                          */
    838         /***************************************************************/
    839         NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
    840         u4_ldz = CLZ(u4_word);
    841 
    842         /* Flush the ps_bitstrm */
    843         u4_bitstream_offset += (u4_ldz + 1);
    844 
    845         /* Read the suffix from the ps_bitstrm */
    846         u4_word = 0;
    847         if(u4_ldz)
    848             GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf, u4_ldz);
    849 
    850         *pu4_bitstrm_ofst = u4_bitstream_offset;
    851         u4_abs_val = ((1 << u4_ldz) + u4_word) >> 1;
    852 
    853         if(u4_word & 0x1)
    854             i_temp = (-(WORD32)u4_abs_val);
    855         else
    856             i_temp = (u4_abs_val);
    857 
    858         if(i_temp < -26 || i_temp > 25)
    859             return ERROR_INV_RANGE_QP_T;
    860 //inlinined ih264d_sev
    861         COPYTHECONTEXT("mb_qp_delta", i_temp);
    862         if(i_temp)
    863         {
    864             ret = ih264d_update_qp(ps_dec, (WORD8)i_temp);
    865             if(ret != OK)
    866                 return ret;
    867         }
    868 
    869         ret = ih264d_parse_residual4x4_cavlc(ps_dec, ps_cur_mb_info, 0);
    870         if(ret != OK)
    871             return ret;
    872         if(EXCEED_OFFSET(ps_bitstrm))
    873             return ERROR_EOB_TERMINATE_T;
    874     }
    875     else
    876     {
    877         ps_dec->i1_prev_mb_qp_delta = 0;
    878         ih264d_update_nnz_for_skipmb(ps_dec, ps_cur_mb_info, CAVLC);
    879     }
    880 
    881     return OK;
    882 }
    883 
    884 WORD32 ih264d_mv_pred_ref_tfr_nby2_bmb(dec_struct_t * ps_dec,
    885                                      UWORD8 u1_mb_idx,
    886                                      UWORD8 u1_num_mbs)
    887 {
    888     parse_pmbarams_t * ps_mb_part_info;
    889     parse_part_params_t * ps_part;
    890     mv_pred_t *ps_mv_nmb, *ps_mv_nmb_start, *ps_mv_ntop, *ps_mv_ntop_start;
    891     pic_buffer_t * ps_ref_frame;
    892     UWORD8 u1_direct_mode_width;
    893     UWORD8 i, j;
    894     dec_mb_info_t * ps_cur_mb_info;
    895     const UWORD8 u1_mbaff = ps_dec->ps_cur_slice->u1_mbaff_frame_flag;
    896     UWORD8 u1_field;
    897     WORD32 ret = 0;
    898 
    899     ps_dec->i4_submb_ofst -= (u1_num_mbs - u1_mb_idx) << 4;
    900     ps_mb_part_info = ps_dec->ps_parse_mb_data;
    901     ps_part = ps_dec->ps_parse_part_params;
    902 
    903     /* N/2 Mb MvPred and Transfer Setup Loop */
    904     for(i = u1_mb_idx; i < u1_num_mbs; i++, ps_mb_part_info++)
    905     {
    906         UWORD8 u1_colz = 0;
    907         ps_dec->i4_submb_ofst += SUB_BLK_SIZE;
    908         /* Restore the slice scratch MbX and MbY context */
    909         ps_cur_mb_info = ps_dec->ps_nmb_info + i;
    910 
    911 
    912         u1_field = ps_cur_mb_info->u1_mb_field_decodingflag;
    913 
    914         ps_mv_nmb_start = ps_dec->ps_mv_cur + (i << 4);
    915         ps_dec->u2_mbx = ps_cur_mb_info->u2_mbx;
    916         ps_dec->u2_mby = ps_cur_mb_info->u2_mby;
    917         ps_dec->u1_currB_type = 0;
    918         ps_dec->u2_mv_2mb[i & 0x1] = 0;
    919 
    920         /* Look for MV Prediction and Reference Transfer in Non-I Mbs */
    921         if(!ps_mb_part_info->u1_isI_mb)
    922         {
    923             UWORD8 u1_blk_no;
    924             WORD16 i1_ref_idx, i1_ref_idx1;
    925             UWORD8 u1_pred_mode;
    926             UWORD8 u1_sub_mb_x, u1_sub_mb_y, u1_sub_mb_num;
    927             UWORD8 u1_lx, u1_lx_start, u1_lxend, u1_tmp_lx;
    928             UWORD8 u1_num_part, u1_num_ref, u1_wd, u1_ht;
    929             UWORD32 *pu4_wt_offst;
    930             UWORD8 u1_scale_ref, u4_bot_mb;
    931             deblk_mb_t * ps_cur_deblk_mb = ps_dec->ps_deblk_mbn + i;
    932             WORD8 (*pi1_ref_idx)[MAX_REFIDX_INFO_PER_MB] =
    933                             ps_mb_part_info->i1_ref_idx;
    934             WORD8 *pi1_ref_idx0 = pi1_ref_idx[0],
    935                             *pi1_ref_idx1 = pi1_ref_idx[1];
    936             UWORD32 **ppu4_wt_ofst = ps_mb_part_info->pu4_wt_offst;
    937 
    938             /* MB Level initialisations */
    939             ps_dec->u4_num_pmbair = i >> u1_mbaff;
    940             ps_dec->u1_mb_idx_mv = i;
    941 
    942             /* CHANGED CODE */
    943             ps_mv_ntop_start = ps_mv_nmb_start
    944                             - (ps_dec->u2_frm_wd_in_mbs << (4 + u1_mbaff)) + 12;
    945 
    946             u1_num_part = ps_mb_part_info->u1_num_part;
    947             ps_cur_deblk_mb->u1_mb_type |= (u1_num_part > 1) << 1;
    948             u1_direct_mode_width = (1 == ps_mb_part_info->u1_num_part) ? 16 : 8;
    949 
    950 
    951             ps_cur_mb_info->u4_pred_info_pkd_idx = ps_dec->u4_pred_info_pkd_idx;
    952             ps_cur_mb_info->u1_num_pred_parts = 0;
    953 
    954             /****************************************************/
    955             /* weighted u4_ofst pointer calculations, this loop  */
    956             /* runs maximum 4 times, even in direct cases       */
    957             /****************************************************/
    958             u1_scale_ref = u1_mbaff & ps_cur_mb_info->u1_mb_field_decodingflag;
    959             u4_bot_mb = 1 - ps_cur_mb_info->u1_topmb;
    960             if(ps_dec->ps_cur_pps->u1_wted_bipred_idc)
    961             {
    962                 u1_num_ref = MIN(u1_num_part, 4);
    963                 if(PART_DIRECT_16x16 != ps_part->u1_is_direct)
    964                 {
    965                     for(u1_blk_no = 0; u1_blk_no < u1_num_ref; u1_blk_no++)
    966                     {
    967                         i1_ref_idx = MAX(pi1_ref_idx0[u1_blk_no], 0);
    968                         if(u1_scale_ref)
    969                             i1_ref_idx >>= 1;
    970                         i1_ref_idx *=
    971                                         ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1];
    972                         if(u1_scale_ref)
    973                             i1_ref_idx +=
    974                                             (MAX(pi1_ref_idx1[u1_blk_no], 0)
    975                                                             >> 1);
    976                         else
    977                             i1_ref_idx += MAX(pi1_ref_idx1[u1_blk_no], 0);
    978                         pu4_wt_offst = (UWORD32*)&ps_dec->pu4_wt_ofsts[2
    979                                         * X3(i1_ref_idx)];
    980 
    981                         if(pi1_ref_idx0[u1_blk_no] < 0)
    982                             pu4_wt_offst += 1;
    983 
    984                         ppu4_wt_ofst[u1_blk_no] = pu4_wt_offst;
    985                         if(u1_scale_ref
    986                                         && (ps_dec->ps_cur_pps->u1_wted_bipred_idc
    987                                                         == 2))
    988                         {
    989                             i1_ref_idx = MAX(pi1_ref_idx0[u1_blk_no], 0);
    990                             i1_ref_idx *=
    991                                             (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1]
    992                                                             << 1);
    993                             i1_ref_idx += MAX(pi1_ref_idx1[u1_blk_no], 0);
    994                             if(u4_bot_mb)
    995                             {
    996                                 i1_ref_idx +=
    997                                                 (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0]
    998                                                                 << 1)
    999                                                                 * (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1]
   1000                                                                                 << 1);
   1001                             }
   1002                             pu4_wt_offst = (UWORD32*)&ps_dec->pu4_mbaff_wt_mat[2
   1003                                             * X3(i1_ref_idx)];
   1004                             ppu4_wt_ofst[u1_blk_no] = pu4_wt_offst;
   1005                         }
   1006                     }
   1007                 }
   1008             }
   1009 
   1010             /**************************************************/
   1011             /* Loop on Partitions                             */
   1012             /* direct mode is reflected as a single partition */
   1013             /**************************************************/
   1014             for(j = 0; j < u1_num_part; j++, ps_part++)
   1015             {
   1016                 u1_sub_mb_num = ps_part->u1_sub_mb_num;
   1017                 ps_dec->u1_sub_mb_num = u1_sub_mb_num;
   1018 
   1019                 if(PART_NOT_DIRECT != ps_part->u1_is_direct)
   1020                 {
   1021                     /**************************************************/
   1022                     /* Direct Mode, Call DecodeSpatial/TemporalDirect */
   1023                     /* only (those will in turn call FormMbPartInfo)  */
   1024                     /**************************************************/
   1025                     ret = ps_dec->ps_cur_slice->pf_decodeDirect(ps_dec,
   1026                                                                 u1_direct_mode_width,
   1027                                                                 ps_cur_mb_info, i);
   1028                     if(ret != OK)
   1029                         return ret;
   1030                     ps_cur_deblk_mb->u1_mb_type |= (ps_dec->u1_currB_type << 1);
   1031 
   1032                 }
   1033                 else
   1034                 {
   1035                     mv_pred_t s_mvPred;
   1036                     /**************************************************/
   1037                     /* Non Direct Mode, Call Motion Vector Predictor  */
   1038                     /* and FormMbpartInfo                             */
   1039                     /**************************************************/
   1040                     u1_sub_mb_x = u1_sub_mb_num & 0x03;
   1041                     u1_sub_mb_y = u1_sub_mb_num >> 2;
   1042                     u1_blk_no =
   1043                                     (u1_num_part < 4) ?
   1044                                                     j :
   1045                                                     (((u1_sub_mb_y >> 1) << 1)
   1046                                                                     + (u1_sub_mb_x
   1047                                                                                     >> 1));
   1048 
   1049                     ps_mv_ntop = ps_mv_ntop_start + u1_sub_mb_x;
   1050                     ps_mv_nmb = ps_mv_nmb_start + u1_sub_mb_num;
   1051 
   1052                     u1_pred_mode = ps_part->u1_pred_mode;
   1053                     u1_wd = ps_part->u1_partwidth;
   1054                     u1_ht = ps_part->u1_partheight;
   1055 
   1056                     u1_lx_start = 0;
   1057                     u1_lxend = 2;
   1058                     if( PRED_L0 == u1_pred_mode)
   1059                     {
   1060                         s_mvPred.i2_mv[2] = 0;
   1061                         s_mvPred.i2_mv[3] = 0;
   1062                         u1_lxend = 1;
   1063                     }
   1064                     if( PRED_L1 == u1_pred_mode)
   1065                     {
   1066                         s_mvPred.i2_mv[0] = 0;
   1067                         s_mvPred.i2_mv[1] = 0;
   1068                         u1_lx_start = 1;
   1069                     }
   1070 
   1071                     /* Populate the colpic info and reference frames */
   1072                     s_mvPred.i1_ref_frame[0] = pi1_ref_idx0[u1_blk_no];
   1073                     s_mvPred.i1_ref_frame[1] = pi1_ref_idx1[u1_blk_no];
   1074 
   1075                     ps_dec->pf_mvpred(ps_dec, ps_cur_mb_info, ps_mv_nmb, ps_mv_ntop,
   1076                                       &s_mvPred, u1_sub_mb_num, u1_wd,
   1077                                       u1_lx_start, u1_lxend,
   1078                                       ps_cur_mb_info->u1_mb_mc_mode);
   1079 
   1080                     /**********************************************************/
   1081                     /* Loop on number of predictors, 1 Each for Forw Backw    */
   1082                     /* Loop 2 times for BiDirect mode                         */
   1083                     /**********************************************************/
   1084                     for(u1_lx = u1_lx_start; u1_lx < u1_lxend; u1_lx++)
   1085                     {
   1086                         WORD16 i2_mv_x, i2_mv_y;
   1087 
   1088                         /********************************************************/
   1089                         /* Predict Mv                                           */
   1090                         /* Add Mv Residuals and store back                      */
   1091                         /********************************************************/
   1092                         i1_ref_idx = s_mvPred.i1_ref_frame[u1_lx];
   1093                         u1_tmp_lx = (u1_lx << 1);
   1094 
   1095                         i2_mv_x = ps_mv_nmb->i2_mv[u1_tmp_lx];
   1096                         i2_mv_y = ps_mv_nmb->i2_mv[u1_tmp_lx + 1];
   1097 
   1098                         i2_mv_x += s_mvPred.i2_mv[u1_tmp_lx];
   1099                         i2_mv_y += s_mvPred.i2_mv[u1_tmp_lx + 1];
   1100                         s_mvPred.i2_mv[u1_tmp_lx] = i2_mv_x;
   1101                         s_mvPred.i2_mv[u1_tmp_lx + 1] = i2_mv_y;
   1102 
   1103                         /********************************************************/
   1104                         /* Transfer setup call                                  */
   1105                         /* convert RefIdx if it is MbAff                        */
   1106                         /* Pass Weight Offset and refFrame                      */
   1107                         /********************************************************/
   1108                         i1_ref_idx1 = i1_ref_idx >> u1_scale_ref;
   1109                         if(u1_scale_ref && ((i1_ref_idx & 0x01) != u4_bot_mb))
   1110                             i1_ref_idx1 += MAX_REF_BUFS;
   1111                         ps_ref_frame =
   1112                                         ps_dec->ps_ref_pic_buf_lx[u1_lx][i1_ref_idx1];
   1113 
   1114                         /* Storing Colocated-Zero u4_flag */
   1115                         if(u1_lx == u1_lx_start)
   1116                         {
   1117                             /* Fill colocated info in MvPred structure */
   1118                             s_mvPred.u1_col_ref_pic_idx =
   1119                                             ps_ref_frame->u1_mv_buf_id;
   1120                             s_mvPred.u1_pic_type = ps_ref_frame->u1_pic_type;
   1121 
   1122                             /* Calculating colocated zero information */
   1123                             u1_colz =
   1124                                             (u1_field << 1)
   1125                                                             | ((i1_ref_idx == 0)
   1126                                                                             && (ABS(i2_mv_x)
   1127                                                                                             <= 1)
   1128                                                                             && (ABS(i2_mv_y)
   1129                                                                                             <= 1));
   1130                             u1_colz |= ps_mb_part_info->u1_col_info[u1_blk_no];
   1131                         }
   1132 
   1133                         pu4_wt_offst = ppu4_wt_ofst[u1_blk_no];
   1134                         {
   1135                             pred_info_pkd_t *ps_pred_pkd;
   1136                            WORD16 i2_mv[2];
   1137 
   1138                            i2_mv[0] = i2_mv_x;
   1139                            i2_mv[1] = i2_mv_y;
   1140 
   1141                            ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
   1142                         ih264d_fill_pred_info(i2_mv,u1_wd,u1_ht,u1_sub_mb_num,u1_pred_mode,
   1143                                         ps_pred_pkd,ps_ref_frame->u1_pic_buf_id,i1_ref_idx,pu4_wt_offst,
   1144                                         ps_ref_frame->u1_pic_type);
   1145                         ps_dec->u4_pred_info_pkd_idx++;
   1146                         ps_cur_mb_info->u1_num_pred_parts++;
   1147 
   1148 
   1149                         }
   1150 
   1151                     }
   1152                     ih264d_rep_mv_colz(ps_dec, &s_mvPred, ps_mv_nmb,
   1153                                        u1_sub_mb_num, u1_colz, u1_ht,
   1154                                        u1_wd);
   1155                 }
   1156             }
   1157 
   1158         }
   1159         else
   1160         {
   1161             /* Set zero values in case of Intra Mbs */
   1162             mv_pred_t s_mvPred =
   1163                 {
   1164                     { 0, 0, 0, 0 },
   1165                       { -1, -1 }, 0, 0};
   1166             /* Storing colocated zero information */
   1167             ih264d_rep_mv_colz(ps_dec, &s_mvPred, ps_mv_nmb_start, 0,
   1168                                (UWORD8)(u1_field << 1), 4, 4);
   1169         }
   1170 
   1171         /*if num _cores is set to 3 ,compute bs will be done in another thread*/
   1172         if(ps_dec->u4_num_cores < 3)
   1173         {
   1174             if(ps_dec->u4_app_disable_deblk_frm == 0)
   1175                 ps_dec->pf_compute_bs(ps_dec, ps_cur_mb_info,
   1176                                      (UWORD16)(i >> u1_mbaff));
   1177         }
   1178     }
   1179     return OK;
   1180 }
   1181 /*!
   1182  **************************************************************************
   1183  * \if Function name : ih264d_get_implicit_weights \endif
   1184  *
   1185  * \brief
   1186  *    Calculates Implicit Weights.
   1187  *
   1188  * \return
   1189  *    None
   1190  *
   1191  **************************************************************************
   1192  */
   1193 void ih264d_get_implicit_weights(dec_struct_t *ps_dec)
   1194 {
   1195     UWORD32 *pu4_iwt_ofst;
   1196     UWORD8 i, j;
   1197     struct pic_buffer_t *ps_pic_buff0, *ps_pic_buff1;
   1198     WORD16 i2_dist_scale_factor;
   1199     WORD16 i16_tb, i16_td, i16_tx;
   1200     WORD32 i4_tb, i4_td;
   1201     WORD32 i4_poc0, i4_poc1;
   1202     UWORD32 ui_temp0, ui_temp1;
   1203     UWORD8 uc_num_ref_idx_l0_active, uc_num_ref_idx_l1_active;
   1204 
   1205     pu4_iwt_ofst = ps_dec->pu4_wts_ofsts_mat;
   1206     uc_num_ref_idx_l0_active =
   1207                     ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0];
   1208     uc_num_ref_idx_l1_active =
   1209                     ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1];
   1210 
   1211     for(i = 0; i < uc_num_ref_idx_l0_active; i++)
   1212     {
   1213         ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][i];
   1214         i4_poc0 = ps_pic_buff0->i4_avg_poc;
   1215         for(j = 0; j < uc_num_ref_idx_l1_active; j++)
   1216         {
   1217             ps_pic_buff1 = ps_dec->ps_ref_pic_buf_lx[1][j];
   1218             i4_poc1 = ps_pic_buff1->i4_avg_poc;
   1219 
   1220             if(i4_poc1 != i4_poc0)
   1221             {
   1222                 i4_tb = ps_dec->ps_cur_pic->i4_poc - i4_poc0;
   1223                 i16_tb = CLIP_S8(i4_tb);
   1224                 i4_td = i4_poc1 - i4_poc0;
   1225                 i16_td = CLIP_S8(i4_td);
   1226                 i16_tx = (16384 + ABS(SIGN_POW2_DIV(i16_td, 1))) / i16_td;
   1227                 i2_dist_scale_factor = CLIP_S11(
   1228                                             (((i16_tb * i16_tx) + 32) >> 6));
   1229 
   1230                 if(/*((u4_poc1 - u4_poc0) == 0) ||*/
   1231                 (!(ps_pic_buff1->u1_is_short && ps_pic_buff0->u1_is_short))
   1232                                 || ((i2_dist_scale_factor >> 2) < -64)
   1233                                 || ((i2_dist_scale_factor >> 2) > 128))
   1234                 {
   1235                     /* same for forward and backward, wt=32 and Offset = 0 */
   1236                     ui_temp0 = 0x00000020;
   1237                     ui_temp1 = 0x00000020;
   1238                 }
   1239                 else
   1240                 {
   1241                     ui_temp0 = 64 - (i2_dist_scale_factor >> 2);
   1242                     ui_temp1 = (i2_dist_scale_factor >> 2);
   1243                 }
   1244             }
   1245             else
   1246             {
   1247                 ui_temp0 = 0x00000020;
   1248                 ui_temp1 = 0x00000020;
   1249             }
   1250             pu4_iwt_ofst[0] = pu4_iwt_ofst[2] = pu4_iwt_ofst[4] = ui_temp0;
   1251             pu4_iwt_ofst[1] = pu4_iwt_ofst[3] = pu4_iwt_ofst[5] = ui_temp1;
   1252             pu4_iwt_ofst += 6;
   1253         }
   1254     }
   1255     if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag)
   1256     {
   1257         UWORD8 k;
   1258         WORD32 i4_cur_poc = ps_dec->ps_cur_pic->i4_top_field_order_cnt;
   1259         UWORD32* pu4_wt_mat = ps_dec->pu4_mbaff_wt_mat;
   1260         /* Form the Implicit Weighted prediction matrix for field MBs also */
   1261         for(k = 0; k < 2; k++)
   1262         {
   1263             for(i = 0; i < (uc_num_ref_idx_l0_active << 1); i++)
   1264             {
   1265                 UWORD16 u2_l0_idx;
   1266 
   1267                 /*u2_l0_idx = (i >= uc_num_ref_idx_l0_active)
   1268                  ?(MAX_REF_BUFS + i - uc_num_ref_idx_l0_active) : (i) ;*/
   1269 
   1270                 u2_l0_idx = i >> 1;
   1271                 if((i & 0x01) != k)
   1272                 {
   1273                     u2_l0_idx += MAX_REF_BUFS;
   1274                 }
   1275                 ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][u2_l0_idx];
   1276                 i4_poc0 = ps_pic_buff0->i4_poc;
   1277                 for(j = 0; j < (uc_num_ref_idx_l1_active << 1); j++)
   1278                 {
   1279                     UWORD16 u2_l1_idx;
   1280                     /*u2_l1_idx = (j >= uc_num_ref_idx_l1_active)
   1281                      ? (MAX_REF_BUFS + j - uc_num_ref_idx_l1_active ) : (j) ;*/
   1282 
   1283                     u2_l1_idx = j >> 1;
   1284                     if((j & 0x01) != k)
   1285                     {
   1286                         u2_l1_idx += MAX_REF_BUFS;
   1287                     }
   1288                     ps_pic_buff1 = ps_dec->ps_ref_pic_buf_lx[1][u2_l1_idx];
   1289                     i4_poc1 = ps_pic_buff1->i4_poc;
   1290                     if(i4_poc1 != i4_poc0)
   1291                     {
   1292                         i4_tb = i4_cur_poc - i4_poc0;
   1293                         i16_tb = CLIP_S8(i4_tb);
   1294                         i4_td = i4_poc1 - i4_poc0;
   1295                         i16_td = CLIP_S8(i4_td);
   1296                         i16_tx = (16384 + ABS(SIGN_POW2_DIV(i16_td, 1)))
   1297                                         / i16_td;
   1298                         i2_dist_scale_factor = CLIP_S11(
   1299                                                     (((i16_tb * i16_tx) + 32) >> 6));
   1300 
   1301                         if(/*((u4_poc1 - u4_poc0) == 0) ||*/
   1302                         (!(ps_pic_buff1->u1_is_short && ps_pic_buff0->u1_is_short))
   1303                                         || ((i2_dist_scale_factor >> 2) < -64)
   1304                                         || ((i2_dist_scale_factor >> 2) > 128))
   1305                         {
   1306                             /* same for forward and backward, wt=32 and Offset = 0 */
   1307                             ui_temp0 = 0x00000020;
   1308                             ui_temp1 = 0x00000020;
   1309                         }
   1310                         else
   1311                         {
   1312                             ui_temp0 = 64 - (i2_dist_scale_factor >> 2);
   1313                             ui_temp1 = (i2_dist_scale_factor >> 2);
   1314                         }
   1315                     }
   1316                     else
   1317                     {
   1318                         ui_temp0 = 0x00000020;
   1319                         ui_temp1 = 0x00000020;
   1320                     }
   1321                     /* Store in the weight matrix */
   1322                     *pu4_wt_mat++ = ui_temp0;
   1323                     *pu4_wt_mat++ = ui_temp1;
   1324                     *pu4_wt_mat++ = ui_temp0;
   1325                     *pu4_wt_mat++ = ui_temp1;
   1326                     *pu4_wt_mat++ = ui_temp0;
   1327                     *pu4_wt_mat++ = ui_temp1;
   1328 
   1329                 }
   1330             }
   1331             i4_cur_poc = ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
   1332         }
   1333     }
   1334 }
   1335 
   1336 /*!
   1337  **************************************************************************
   1338  * \if Function name : ih264d_decode_bslice \endif
   1339  *
   1340  * \brief
   1341  *    Decodes a B Slice
   1342  *
   1343  *
   1344  * \return
   1345  *    0 on Success and Error code otherwise
   1346  **************************************************************************
   1347  */
   1348 WORD32 ih264d_parse_bslice(dec_struct_t * ps_dec, UWORD16 u2_first_mb_in_slice)
   1349 {
   1350     dec_pic_params_t * ps_pps = ps_dec->ps_cur_pps;
   1351     dec_slice_params_t * ps_slice = ps_dec->ps_cur_slice;
   1352     dec_bit_stream_t * ps_bitstrm = ps_dec->ps_bitstrm;
   1353     UWORD8 u1_ref_idx_re_flag_lx;
   1354     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
   1355     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
   1356 
   1357     UWORD32 u4_temp, ui_temp1;
   1358     WORD32 i_temp;
   1359     WORD32 ret;
   1360 
   1361     /*--------------------------------------------------------------------*/
   1362     /* Read remaining contents of the slice header                        */
   1363     /*--------------------------------------------------------------------*/
   1364     {
   1365         WORD8 *pi1_buf;
   1366         WORD16 *pi2_mv = ps_dec->s_default_mv_pred.i2_mv;
   1367         WORD32 *pi4_mv = (WORD32*)pi2_mv;
   1368         WORD16 *pi16_refFrame;
   1369         pi1_buf = ps_dec->s_default_mv_pred.i1_ref_frame;
   1370         pi16_refFrame = (WORD16*)pi1_buf;
   1371         *pi4_mv = 0;
   1372         *(pi4_mv + 1) = 0;
   1373         *pi16_refFrame = OUT_OF_RANGE_REF;
   1374         ps_dec->s_default_mv_pred.u1_col_ref_pic_idx = (UWORD8)-1;
   1375         ps_dec->s_default_mv_pred.u1_pic_type = (UWORD8)-1;
   1376     }
   1377 
   1378     ps_slice->u1_num_ref_idx_active_override_flag = ih264d_get_bit_h264(
   1379                     ps_bitstrm);
   1380     COPYTHECONTEXT("SH: num_ref_idx_override_flag",
   1381                     ps_slice->u1_num_ref_idx_active_override_flag);
   1382 
   1383     u4_temp = ps_dec->ps_cur_pps->u1_num_ref_idx_lx_active[0];
   1384     ui_temp1 = ps_dec->ps_cur_pps->u1_num_ref_idx_lx_active[1];
   1385     if(ps_slice->u1_num_ref_idx_active_override_flag)
   1386     {
   1387         u4_temp = 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
   1388         COPYTHECONTEXT("SH: num_ref_idx_l0_active_minus1",
   1389                         u4_temp - 1);
   1390         ui_temp1 = 1 + ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
   1391         COPYTHECONTEXT("SH: num_ref_idx_l1_active_minus1",
   1392                         ui_temp1 - 1);
   1393     }
   1394 
   1395     {
   1396         UWORD8 u1_max_ref_idx = MAX_FRAMES;
   1397         if(ps_slice->u1_field_pic_flag)
   1398         {
   1399             u1_max_ref_idx = MAX_FRAMES << 1;
   1400         }
   1401         if((u4_temp > u1_max_ref_idx) || (ui_temp1 > u1_max_ref_idx)
   1402                         || (u4_temp < 1) || (ui_temp1 < 1))
   1403         {
   1404             return ERROR_NUM_REF;
   1405         }
   1406         ps_slice->u1_num_ref_idx_lx_active[0] = u4_temp;
   1407         ps_slice->u1_num_ref_idx_lx_active[1] = ui_temp1;
   1408     }
   1409 
   1410 
   1411     ih264d_init_ref_idx_lx_b(ps_dec);
   1412     /* Store the value for future slices in the same picture */
   1413     ps_dec->u1_num_ref_idx_lx_active_prev =
   1414                     ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0];
   1415 
   1416     u1_ref_idx_re_flag_lx = ih264d_get_bit_h264(ps_bitstrm);
   1417     COPYTHECONTEXT("SH: ref_pic_list_reordering_flag_l0",u1_ref_idx_re_flag_lx);
   1418 
   1419     /* Modified temporarily */
   1420     if(u1_ref_idx_re_flag_lx)
   1421     {
   1422         WORD8 ret;
   1423         ps_dec->ps_ref_pic_buf_lx[0] = ps_dec->ps_dpb_mgr->ps_mod_dpb[0];
   1424         ret = ih264d_ref_idx_reordering(ps_dec, 0);
   1425         if(ret == -1)
   1426             return ERROR_REFIDX_ORDER_T;
   1427     }
   1428     else
   1429         ps_dec->ps_ref_pic_buf_lx[0] = ps_dec->ps_dpb_mgr->ps_init_dpb[0];
   1430 
   1431     u1_ref_idx_re_flag_lx = ih264d_get_bit_h264(ps_bitstrm);
   1432     COPYTHECONTEXT("SH: ref_pic_list_reordering_flag_l1",u1_ref_idx_re_flag_lx);
   1433 
   1434     /* Modified temporarily */
   1435     if(u1_ref_idx_re_flag_lx)
   1436     {
   1437         WORD8 ret;
   1438         ps_dec->ps_ref_pic_buf_lx[1] = ps_dec->ps_dpb_mgr->ps_mod_dpb[1];
   1439         ret = ih264d_ref_idx_reordering(ps_dec, 1);
   1440         if(ret == -1)
   1441             return ERROR_REFIDX_ORDER_T;
   1442     }
   1443     else
   1444         ps_dec->ps_ref_pic_buf_lx[1] = ps_dec->ps_dpb_mgr->ps_init_dpb[1];
   1445 
   1446     /* Create refIdx to POC mapping */
   1447     {
   1448         void **ppv_map_ref_idx_to_poc_lx;
   1449         WORD8 idx;
   1450         struct pic_buffer_t *ps_pic;
   1451 
   1452         ppv_map_ref_idx_to_poc_lx = ps_dec->ppv_map_ref_idx_to_poc + FRM_LIST_L0;
   1453         ppv_map_ref_idx_to_poc_lx[0] = 0;
   1454         ppv_map_ref_idx_to_poc_lx++;
   1455         for(idx = 0; idx < ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0];
   1456                         idx++)
   1457         {
   1458             ps_pic = ps_dec->ps_ref_pic_buf_lx[0][idx];
   1459             ppv_map_ref_idx_to_poc_lx[idx] = (ps_pic->pu1_buf1);
   1460         }
   1461 
   1462         ppv_map_ref_idx_to_poc_lx = ps_dec->ppv_map_ref_idx_to_poc + FRM_LIST_L1;
   1463 
   1464         ppv_map_ref_idx_to_poc_lx[0] = 0;
   1465         ppv_map_ref_idx_to_poc_lx++;
   1466         for(idx = 0; idx < ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1];
   1467                         idx++)
   1468         {
   1469             ps_pic = ps_dec->ps_ref_pic_buf_lx[1][idx];
   1470             ppv_map_ref_idx_to_poc_lx[idx] = (ps_pic->pu1_buf1);
   1471         }
   1472 
   1473         if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag)
   1474         {
   1475             void **ppv_map_ref_idx_to_poc_lx_t, **ppv_map_ref_idx_to_poc_lx_b;
   1476 
   1477             ppv_map_ref_idx_to_poc_lx_t = ps_dec->ppv_map_ref_idx_to_poc
   1478                             + TOP_LIST_FLD_L0;
   1479             ppv_map_ref_idx_to_poc_lx_b = ps_dec->ppv_map_ref_idx_to_poc
   1480                             + BOT_LIST_FLD_L0;
   1481 
   1482             ppv_map_ref_idx_to_poc_lx_t[0] = 0;
   1483             ppv_map_ref_idx_to_poc_lx_t++;
   1484             ppv_map_ref_idx_to_poc_lx_b[0] = 0;
   1485             ppv_map_ref_idx_to_poc_lx_b++;
   1486             for(idx = 0; idx < ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0];
   1487                             idx++)
   1488             {
   1489                 ps_pic = ps_dec->ps_ref_pic_buf_lx[0][idx];
   1490                 ppv_map_ref_idx_to_poc_lx_t[0] = (ps_pic->pu1_buf1);
   1491                 ppv_map_ref_idx_to_poc_lx_b[1] = (ps_pic->pu1_buf1);
   1492 
   1493                 ppv_map_ref_idx_to_poc_lx_b[0] = (ps_pic->pu1_buf1) + 1;
   1494                 ppv_map_ref_idx_to_poc_lx_t[1] = (ps_pic->pu1_buf1) + 1;
   1495 
   1496                 ppv_map_ref_idx_to_poc_lx_t += 2;
   1497                 ppv_map_ref_idx_to_poc_lx_b += 2;
   1498             }
   1499 
   1500             ppv_map_ref_idx_to_poc_lx_t = ps_dec->ppv_map_ref_idx_to_poc
   1501                             + TOP_LIST_FLD_L1;
   1502             ppv_map_ref_idx_to_poc_lx_b = ps_dec->ppv_map_ref_idx_to_poc
   1503                             + BOT_LIST_FLD_L1;
   1504 
   1505             ppv_map_ref_idx_to_poc_lx_t[0] = 0;
   1506             ppv_map_ref_idx_to_poc_lx_t++;
   1507             ppv_map_ref_idx_to_poc_lx_b[0] = 0;
   1508             ppv_map_ref_idx_to_poc_lx_b++;
   1509             for(idx = 0; idx < ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1];
   1510                             idx++)
   1511             {
   1512                 UWORD8 u1_tmp_idx = idx << 1;
   1513                 ps_pic = ps_dec->ps_ref_pic_buf_lx[1][idx];
   1514                 ppv_map_ref_idx_to_poc_lx_t[u1_tmp_idx] = (ps_pic->pu1_buf1);
   1515                 ppv_map_ref_idx_to_poc_lx_b[u1_tmp_idx + 1] = (ps_pic->pu1_buf1);
   1516 
   1517                 ppv_map_ref_idx_to_poc_lx_b[u1_tmp_idx] = (ps_pic->pu1_buf1) + 1;
   1518                 ppv_map_ref_idx_to_poc_lx_t[u1_tmp_idx + 1] = (ps_pic->pu1_buf1) + 1;
   1519 
   1520             }
   1521         }
   1522 
   1523         if(ps_dec->u4_num_cores >= 3)
   1524         {
   1525             WORD32 num_entries;
   1526             WORD32 size;
   1527             num_entries = MAX_FRAMES;
   1528             if((1 >= ps_dec->ps_cur_sps->u1_num_ref_frames) &&
   1529                 (0 == ps_dec->i4_display_delay))
   1530             {
   1531                 num_entries = 1;
   1532             }
   1533 
   1534             num_entries = ((2 * num_entries) + 1);
   1535             num_entries *= 2;
   1536 
   1537             size = num_entries * sizeof(void *);
   1538             size += PAD_MAP_IDX_POC * sizeof(void *);
   1539 
   1540             memcpy((void *)ps_dec->ps_parse_cur_slice->ppv_map_ref_idx_to_poc,
   1541                ps_dec->ppv_map_ref_idx_to_poc,
   1542                size);
   1543         }
   1544 
   1545     }
   1546 
   1547     if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag
   1548                     && (ps_dec->ps_cur_slice->u1_field_pic_flag == 0))
   1549     {
   1550         ih264d_convert_frm_mbaff_list(ps_dec);
   1551     }
   1552 
   1553     if(ps_pps->u1_wted_bipred_idc == 1)
   1554     {
   1555         ret = ih264d_parse_pred_weight_table(ps_slice, ps_bitstrm);
   1556         if(ret != OK)
   1557             return ret;
   1558         ih264d_form_pred_weight_matrix(ps_dec);
   1559         ps_dec->pu4_wt_ofsts = ps_dec->pu4_wts_ofsts_mat;
   1560     }
   1561     else if(ps_pps->u1_wted_bipred_idc == 2)
   1562     {
   1563         /* Implicit Weighted prediction */
   1564         ps_slice->u2_log2Y_crwd = 0x0505;
   1565         ps_dec->pu4_wt_ofsts = ps_dec->pu4_wts_ofsts_mat;
   1566         ih264d_get_implicit_weights(ps_dec);
   1567     }
   1568     else
   1569         ps_dec->ps_cur_slice->u2_log2Y_crwd = 0;
   1570 
   1571     ps_dec->ps_parse_cur_slice->u2_log2Y_crwd =
   1572                     ps_dec->ps_cur_slice->u2_log2Y_crwd;
   1573 
   1574     /* G050 */
   1575     if(ps_slice->u1_nal_ref_idc != 0)
   1576     {
   1577         if(!ps_dec->ps_dpb_cmds->u1_dpb_commands_read)
   1578         {
   1579             i_temp = ih264d_read_mmco_commands(ps_dec);
   1580             if (i_temp < 0)
   1581             {
   1582                 return ERROR_DBP_MANAGER_T;
   1583             }
   1584             ps_dec->u4_bitoffset = i_temp;
   1585         }
   1586         else
   1587             ps_bitstrm->u4_ofst += ps_dec->u4_bitoffset;
   1588     }
   1589     /* G050 */
   1590 
   1591     if(ps_pps->u1_entropy_coding_mode == CABAC)
   1592     {
   1593         u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
   1594         if(u4_temp > MAX_CABAC_INIT_IDC)
   1595         {
   1596             return ERROR_INV_SLICE_HDR_T;
   1597         }
   1598         ps_slice->u1_cabac_init_idc = u4_temp;
   1599         COPYTHECONTEXT("SH: cabac_init_idc",ps_slice->u1_cabac_init_idc);
   1600     }
   1601 
   1602     /* Read slice_qp_delta */
   1603     i_temp = ps_pps->u1_pic_init_qp
   1604                     + ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
   1605     if((i_temp < 0) || (i_temp > 51))
   1606     {
   1607         return ERROR_INV_RANGE_QP_T;
   1608     }
   1609     ps_slice->u1_slice_qp = i_temp;
   1610     COPYTHECONTEXT("SH: slice_qp_delta",
   1611                     (WORD8)(ps_slice->u1_slice_qp - ps_pps->u1_pic_init_qp));
   1612 
   1613     if(ps_pps->u1_deblocking_filter_parameters_present_flag == 1)
   1614     {
   1615         u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
   1616         if(u4_temp > SLICE_BOUNDARY_DBLK_DISABLED)
   1617         {
   1618             return ERROR_INV_SLICE_HDR_T;
   1619         } COPYTHECONTEXT("SH: disable_deblocking_filter_idc", u4_temp);
   1620         ps_slice->u1_disable_dblk_filter_idc = u4_temp;
   1621         if(u4_temp != 1)
   1622         {
   1623             i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf)
   1624                             << 1;
   1625             if((MIN_DBLK_FIL_OFF > i_temp) || (i_temp > MAX_DBLK_FIL_OFF))
   1626             {
   1627                 return ERROR_INV_SLICE_HDR_T;
   1628             }
   1629             ps_slice->i1_slice_alpha_c0_offset = i_temp;
   1630             COPYTHECONTEXT("SH: slice_alpha_c0_offset_div2",
   1631                             ps_slice->i1_slice_alpha_c0_offset >> 1);
   1632 
   1633             i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf)
   1634                             << 1;
   1635             if((MIN_DBLK_FIL_OFF > i_temp) || (i_temp > MAX_DBLK_FIL_OFF))
   1636             {
   1637                 return ERROR_INV_SLICE_HDR_T;
   1638             }
   1639             ps_slice->i1_slice_beta_offset = i_temp;
   1640             COPYTHECONTEXT("SH: slice_beta_offset_div2",
   1641                             ps_slice->i1_slice_beta_offset >> 1);
   1642 
   1643         }
   1644         else
   1645         {
   1646             ps_slice->i1_slice_alpha_c0_offset = 0;
   1647             ps_slice->i1_slice_beta_offset = 0;
   1648         }
   1649     }
   1650     else
   1651     {
   1652         ps_slice->u1_disable_dblk_filter_idc = 0;
   1653         ps_slice->i1_slice_alpha_c0_offset = 0;
   1654         ps_slice->i1_slice_beta_offset = 0;
   1655     }
   1656 
   1657     ps_dec->u1_slice_header_done = 2;
   1658 
   1659     if(ps_pps->u1_entropy_coding_mode)
   1660     {
   1661         SWITCHOFFTRACE; SWITCHONTRACECABAC;
   1662         ps_dec->pf_parse_inter_slice = ih264d_parse_inter_slice_data_cabac;
   1663         ps_dec->pf_parse_inter_mb = ih264d_parse_bmb_cabac;
   1664         ih264d_init_cabac_contexts(B_SLICE, ps_dec);
   1665 
   1666         if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag)
   1667             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cabac_mbaff;
   1668         else
   1669             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cabac_nonmbaff;
   1670     }
   1671     else
   1672     {
   1673         SWITCHONTRACE; SWITCHOFFTRACECABAC;
   1674         ps_dec->pf_parse_inter_slice = ih264d_parse_inter_slice_data_cavlc;
   1675         ps_dec->pf_parse_inter_mb = ih264d_parse_bmb_cavlc;
   1676         if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag)
   1677             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cavlc_mbaff;
   1678         else
   1679             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cavlc_nonmbaff;
   1680     }
   1681 
   1682     ret = ih264d_cal_col_pic(ps_dec);
   1683     if(ret != OK)
   1684         return ret;
   1685     ps_dec->u1_B = 1;
   1686     ps_dec->pf_mvpred_ref_tfr_nby2mb = ih264d_mv_pred_ref_tfr_nby2_bmb;
   1687     ret = ps_dec->pf_parse_inter_slice(ps_dec, ps_slice, u2_first_mb_in_slice);
   1688     if(ret != OK)
   1689         return ret;
   1690     return OK;
   1691 }
   1692 
   1693