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_process_bslice.c
     23  *
     24  * \brief
     25  *    Contains routines that decode B slice type
     26  *
     27  * Detailed_description
     28  *
     29  * \date
     30  *    21/12/2002
     31  *
     32  * \author  NS
     33  **************************************************************************
     34  */
     35 #include "ih264_typedefs.h"
     36 #include "ih264_macros.h"
     37 #include "ih264_platform_macros.h"
     38 
     39 #include <string.h>
     40 #include "ih264d_structs.h"
     41 #include "ih264d_bitstrm.h"
     42 #include "ih264d_parse_cavlc.h"
     43 #include "ih264d_mb_utils.h"
     44 #include "ih264d_mvpred.h"
     45 #include "ih264d_inter_pred.h"
     46 #include "ih264d_process_pslice.h"
     47 #include "ih264d_error_handler.h"
     48 #include "ih264d_tables.h"
     49 #include "ih264d_parse_slice.h"
     50 #include "ih264d_process_pslice.h"
     51 #include "ih264d_process_bslice.h"
     52 #include "ih264d_tables.h"
     53 #include "ih264d_parse_islice.h"
     54 #include "ih264d_mvpred.h"
     55 
     56 void ih264d_init_cabac_contexts(UWORD8 u1_slice_type, dec_struct_t * ps_dec);
     57 //UWORD32 g_hits = 0;
     58 //UWORD32 g_miss = 0;
     59 /*!
     60  **************************************************************************
     61  * \if Function name : ih264d_decode_spatial_direct \endif
     62  *
     63  * \brief
     64  *    Decodes spatial direct mode.
     65  *
     66  * \return
     67  *    None.
     68  *    Arunoday T
     69  **************************************************************************
     70  */
     71 WORD32 ih264d_decode_spatial_direct(dec_struct_t * ps_dec,
     72                                     UWORD8 u1_wd_x,
     73                                     dec_mb_info_t * ps_cur_mb_info,
     74                                     UWORD8 u1_mb_num)
     75 {
     76     mv_pred_t s_mv_pred, *ps_mv;
     77     UWORD8 u1_col_zero_flag, u1_sub_mb_num, u1_direct_zero_pred_flag = 0;
     78     UWORD8 u1_mbaff = ps_dec->ps_cur_slice->u1_mbaff_frame_flag;
     79     mv_pred_t *ps_mv_ntop_start;
     80     mv_pred_t *ps_mv_nmb_start = ps_dec->ps_mv_cur + (u1_mb_num << 4);
     81     UWORD8 partition_size, sub_partition, u1_mb_partw, u1_mb_parth;
     82     UWORD8 i;
     83     WORD8 i1_pred, i1_ref_frame0, i1_ref_frame1;
     84     struct pic_buffer_t *ps_ref_frame = NULL, *ps_col_pic, *ps_pic_buff0 = NULL,
     85                     *ps_pic_buff1 = NULL;
     86 
     87     UWORD8 u1_zero_pred_cond_f, u1_zero_pred_cond_b;
     88     WORD16 i2_def_mv[2], i2_spat_pred_mv[4], *pi2_final_mv0, *pi2_final_mv1;
     89     UWORD16 ui2_mask_fwd = 0, ui2_mask_bwd = 0, u2_mask = 0;
     90     UWORD32 *pui32_weight_ofsts = NULL;
     91     directmv_t s_mvdirect;
     92     UWORD8 u1_colz;
     93     UWORD8 u1_final_ref_idx = 0;
     94     const UWORD8 *pu1_mb_parth = (const UWORD8 *)gau1_ih264d_mb_parth;
     95     const UWORD8 *pu1_mb_partw = (const UWORD8 *)gau1_ih264d_mb_partw;
     96     const UWORD16 sub_mask_table[] =
     97         { 0x33, 0x3, 0x11, 0x1 };
     98     const UWORD16 mask_table[] =
     99         { 0xffff, /*16x16 NA */
    100           0xff, /* 16x8*/
    101           0x3333, /* 8x16*/
    102           0x33 };/* 8x8*/
    103     mv_pred_t s_temp_mv_pred;
    104     WORD32 ret = 0;
    105 
    106     /* CHANGED CODE */
    107     ps_mv_ntop_start = ps_dec->ps_mv_cur + (u1_mb_num << 4)
    108                     - (ps_dec->u2_frm_wd_in_mbs << (4 + u1_mbaff)) + 12;
    109 
    110     /* assign default values for MotionVector as zero */
    111     i2_def_mv[0] = 0;
    112     i2_def_mv[1] = 0;
    113 
    114     u1_direct_zero_pred_flag = ps_dec->pf_mvpred(ps_dec, ps_cur_mb_info, ps_mv_nmb_start,
    115                                               ps_mv_ntop_start, &s_mv_pred, 0, 4,
    116                                               0, 1, B_DIRECT_SPATIAL);
    117 
    118     i2_spat_pred_mv[0] = s_mv_pred.i2_mv[0];
    119     i2_spat_pred_mv[1] = s_mv_pred.i2_mv[1];
    120     i2_spat_pred_mv[2] = s_mv_pred.i2_mv[2];
    121     i2_spat_pred_mv[3] = s_mv_pred.i2_mv[3];
    122 
    123     i1_ref_frame0 = s_mv_pred.i1_ref_frame[0];
    124     i1_ref_frame1 = s_mv_pred.i1_ref_frame[1];
    125 
    126     i1_ref_frame0 = (i1_ref_frame0 < 0) ? -1 : i1_ref_frame0;
    127     i1_ref_frame1 = (i1_ref_frame1 < 0) ? -1 : i1_ref_frame1;
    128 
    129     i1_pred = 0;
    130 
    131     {
    132         WORD8 u1_ref_idx, u1_ref_idx1;
    133         UWORD32 uc_Idx, uc_Idx1;
    134         UWORD8 u1_scale_ref = (ps_dec->ps_cur_slice->u1_mbaff_frame_flag
    135                         && ps_cur_mb_info->u1_mb_field_decodingflag);
    136         u1_final_ref_idx = i1_ref_frame0;
    137         if(i1_ref_frame0 >= 0)
    138         {
    139             /* convert RefIdx if it is MbAff */
    140             u1_ref_idx = i1_ref_frame0;
    141             u1_ref_idx1 = i1_ref_frame0;
    142             if(u1_scale_ref)
    143             {
    144                 u1_ref_idx1 = u1_ref_idx >> 1;
    145                 if((u1_ref_idx & 0x01) != (1 - ps_cur_mb_info->u1_topmb))
    146                     u1_ref_idx1 += MAX_REF_BUFS;
    147             }
    148             /* If i1_ref_frame0 < 0 then refIdxCol is obtained from ps_pic_buff1 */
    149             ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][u1_ref_idx1];
    150             ps_ref_frame = ps_pic_buff0;
    151             i1_pred = PRED_L0;
    152         }
    153 
    154         if(i1_ref_frame1 >= 0)
    155         {
    156             /* convert RefIdx if it is MbAff */
    157             u1_ref_idx = i1_ref_frame1;
    158             u1_ref_idx1 = i1_ref_frame1;
    159             if(u1_scale_ref)
    160             {
    161                 u1_ref_idx1 = u1_ref_idx >> 1;
    162                 if((u1_ref_idx & 0x01) != (1 - ps_cur_mb_info->u1_topmb))
    163                     u1_ref_idx1 += MAX_REF_BUFS;
    164             }
    165             ps_pic_buff1 = ps_dec->ps_ref_pic_buf_lx[1][u1_ref_idx1];
    166             i1_pred = i1_pred | PRED_L1;
    167         }
    168         if(i1_ref_frame0 < 0)
    169         {
    170             ps_ref_frame = ps_pic_buff1;
    171             u1_final_ref_idx = i1_ref_frame1;
    172         }
    173 
    174         u1_zero_pred_cond_f = (u1_direct_zero_pred_flag) || (i1_ref_frame0 < 0);
    175         u1_zero_pred_cond_b = (u1_direct_zero_pred_flag) || (i1_ref_frame1 < 0);
    176 
    177         if(ps_dec->ps_cur_pps->u1_wted_bipred_idc)
    178         {
    179             uc_Idx = ((i1_ref_frame0 < 1) ? 0 : i1_ref_frame0)
    180                             * ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1];
    181             if(u1_scale_ref)
    182                 uc_Idx >>= 1;
    183             uc_Idx1 = (i1_ref_frame1 < 0) ? 0 : i1_ref_frame1;
    184             uc_Idx += (u1_scale_ref) ? (uc_Idx1 >> 1) : uc_Idx1;
    185             pui32_weight_ofsts =
    186                             (UWORD32*)&ps_dec->pu4_wt_ofsts[2 * X3(uc_Idx)];
    187 
    188             if(i1_ref_frame0 < 0)
    189                 pui32_weight_ofsts += 1;
    190 
    191             if(u1_scale_ref && (ps_dec->ps_cur_pps->u1_wted_bipred_idc == 2))
    192             {
    193                 WORD16 i2_ref_idx;
    194                 i2_ref_idx = MAX(i1_ref_frame0, 0);
    195                 i2_ref_idx *= (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1]
    196                                 << 1);
    197                 i2_ref_idx += MAX(i1_ref_frame1, 0);
    198                 if(!ps_cur_mb_info->u1_topmb)
    199                     i2_ref_idx +=
    200                                     (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0]
    201                                                     << 1)
    202                                                     * (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1]
    203                                                                     << 1);
    204                 pui32_weight_ofsts = (UWORD32*)&ps_dec->pu4_mbaff_wt_mat[2
    205                                 * X3(i2_ref_idx)];
    206             }
    207         }
    208     }
    209 
    210     s_temp_mv_pred.i1_ref_frame[0] = i1_ref_frame0;
    211     s_temp_mv_pred.i1_ref_frame[1] = i1_ref_frame1;
    212     s_temp_mv_pred.u1_col_ref_pic_idx = ps_ref_frame->u1_mv_buf_id;
    213     s_temp_mv_pred.u1_pic_type = ps_ref_frame->u1_pic_type;
    214 
    215     /**********************************************************************/
    216     /* Call the function which gets the number of partitions and          */
    217     /* partition info of colocated Mb                                     */
    218     /**********************************************************************/
    219 
    220     ps_dec->pf_parse_mvdirect(ps_dec, ps_dec->ps_col_pic, &s_mvdirect, u1_wd_x,
    221                            ps_dec->i4_submb_ofst, ps_cur_mb_info);
    222     ps_col_pic = ps_dec->ps_col_pic;
    223     if((s_mvdirect.u1_col_zeroflag_change == 0) || u1_direct_zero_pred_flag)
    224     {
    225         WORD16 i2_mv_x, i2_mv_y, i2_mvX1, i2_mvY1;
    226         /* Most probable case */
    227         u1_col_zero_flag = *(ps_col_pic->pu1_col_zero_flag
    228                         + s_mvdirect.i4_mv_indices[0]);
    229         u1_col_zero_flag = u1_col_zero_flag & 0x01;
    230 
    231         if(u1_zero_pred_cond_f || ((i1_ref_frame0 == 0) && (u1_col_zero_flag == 1)))
    232         {
    233             i2_mv_x = 0;
    234             i2_mv_y = 0;
    235         }
    236         else
    237         {
    238             i2_mv_x = i2_spat_pred_mv[0];
    239             i2_mv_y = i2_spat_pred_mv[1];
    240 
    241         }
    242 
    243         if(u1_zero_pred_cond_b || ((i1_ref_frame1 == 0) && (u1_col_zero_flag == 1)))
    244         {
    245             i2_mvX1 = 0;
    246             i2_mvY1 = 0;
    247         }
    248         else
    249         {
    250             i2_mvX1 = i2_spat_pred_mv[2];
    251             i2_mvY1 = i2_spat_pred_mv[3];
    252         }
    253 
    254         u1_sub_mb_num = ps_dec->u1_sub_mb_num;
    255         u1_mb_partw = (u1_wd_x >> 2);
    256 
    257 
    258         if(i1_ref_frame0 >= 0)
    259         {
    260             {
    261                pred_info_pkd_t *ps_pred_pkd;
    262                WORD16 i2_mv[2];
    263                WORD8 i1_ref_idx= 0;
    264 
    265                i2_mv[0] = i2_mv_x;
    266                i2_mv[1] = i2_mv_y;
    267 
    268                ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
    269             ih264d_fill_pred_info(i2_mv,u1_mb_partw,u1_mb_partw,u1_sub_mb_num,i1_pred,
    270                             ps_pred_pkd,ps_pic_buff0->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
    271                             ps_pic_buff0->u1_pic_type);
    272             ps_dec->u4_pred_info_pkd_idx++;
    273             ps_cur_mb_info->u1_num_pred_parts++;
    274 
    275 
    276             }
    277 
    278         }
    279 
    280         if(i1_ref_frame1 >= 0)
    281         {
    282             {
    283                 pred_info_pkd_t *ps_pred_pkd;
    284                WORD16 i2_mv[2];
    285                WORD8 i1_ref_idx= 0;
    286 
    287                i2_mv[0] = i2_mvX1;
    288                i2_mv[1] = i2_mvY1;
    289 
    290                ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
    291             ih264d_fill_pred_info(i2_mv,u1_mb_partw,u1_mb_partw,u1_sub_mb_num,i1_pred,
    292                             ps_pred_pkd,ps_pic_buff1->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
    293                             ps_pic_buff1->u1_pic_type);
    294             ps_dec->u4_pred_info_pkd_idx++;
    295             ps_cur_mb_info->u1_num_pred_parts++;
    296 
    297 
    298             }
    299         }
    300 
    301 
    302         /* Replication optimisation */
    303         s_temp_mv_pred.i2_mv[0] = i2_mv_x;
    304         s_temp_mv_pred.i2_mv[1] = i2_mv_y;
    305         s_temp_mv_pred.i2_mv[2] = i2_mvX1;
    306         s_temp_mv_pred.i2_mv[3] = i2_mvY1;
    307 
    308         /* Calculating colocated zero information */
    309         {
    310             /*************************************/
    311             /* If(bit2 and bit3 set)             */
    312             /* then                              */
    313             /*  (bit0 and bit1) => submmbmode    */
    314             /*  (bit2 and bit3) => mbmode        */
    315             /* else                              */
    316             /*  (bit0 and bit1) => mbmode        */
    317             /*************************************/
    318             /*UWORD8 u1_packed_mb_sub_mb_mode = sub_partition ?
    319              (s_mvdirect.i1_partitionsize[0]) : ((s_mvdirect.i1_partitionsize[0]) << 2);*/
    320             UWORD8 u1_packed_mb_sub_mb_mode = (u1_mb_partw == 2) ? 0x03 : 0;
    321 
    322             if(i1_ref_frame0 < 0)
    323             {
    324                 i2_mv_x = i2_mvX1;
    325                 i2_mv_y = i2_mvY1;
    326             }
    327 
    328             /* Change from left shift 4 to 6 - Varun */
    329             u1_colz = (ps_cur_mb_info->u1_mb_field_decodingflag << 1)
    330                             | ((u1_final_ref_idx == 0) && (ABS(i2_mv_x) <= 1)
    331                                             && (ABS(i2_mv_y) <= 1));
    332             u1_colz |= (u1_packed_mb_sub_mb_mode << 6);
    333         }
    334         ps_mv = ps_mv_nmb_start + u1_sub_mb_num;
    335         ih264d_rep_mv_colz(ps_dec, &s_temp_mv_pred, ps_mv, u1_sub_mb_num, u1_colz,
    336                            u1_mb_partw, u1_mb_partw);
    337         if(u1_wd_x == MB_SIZE)
    338             ps_dec->u1_currB_type = 0;
    339 
    340 
    341 
    342         return OK;
    343     }
    344     /***************************************************************************/
    345     /* If present MB is 16x16 and the partition of colocated Mb is >= PRED_8x8 */
    346     /* i.e 8x8 or less than 8x8 partitions then set up DMA for (0,0) and       */
    347     /* spatially predicted motion vector and do the multiplexing after         */
    348     /* motion compensation                                                     */
    349     /***************************************************************************/
    350 
    351 
    352     if((u1_wd_x == MB_SIZE) && (s_mvdirect.i1_num_partitions > 2))
    353     {
    354         ps_cur_mb_info->u1_Mux = 1;
    355         if(i1_ref_frame0 >= 0)
    356         {
    357 
    358             {
    359                 pred_info_pkd_t *ps_pred_pkd;
    360                WORD8 i1_ref_idx= 0;
    361 
    362                ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
    363             ih264d_fill_pred_info(&(i2_spat_pred_mv[0]),4,4,0,i1_pred,
    364                             ps_pred_pkd,ps_pic_buff0->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
    365                             ps_pic_buff0->u1_pic_type);
    366             ps_dec->u4_pred_info_pkd_idx++;
    367             ps_cur_mb_info->u1_num_pred_parts++;
    368 
    369 
    370             }
    371 
    372             /******    (0,0) Motion vectors DMA     *****/
    373             {
    374                 pred_info_pkd_t *ps_pred_pkd;
    375                WORD16 i2_mv[2];
    376                WORD8 i1_ref_idx= 0;
    377 
    378                i2_mv[0] = 0;
    379                i2_mv[1] = 0;
    380 
    381                ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
    382             ih264d_fill_pred_info(i2_mv,4,4,0,i1_pred,
    383                             ps_pred_pkd,ps_pic_buff0->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
    384                             ps_pic_buff0->u1_pic_type);
    385             ps_dec->u4_pred_info_pkd_idx++;
    386             ps_cur_mb_info->u1_num_pred_parts++;
    387 
    388 
    389             }
    390         }
    391         if(i1_ref_frame1 >= 0)
    392         {
    393             {
    394                 pred_info_pkd_t *ps_pred_pkd;
    395                WORD16 i2_mv[2];
    396                WORD8 i1_ref_idx= 0;
    397 
    398                ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
    399             ih264d_fill_pred_info(&(i2_spat_pred_mv[2]),4,4,0,i1_pred,
    400                             ps_pred_pkd,ps_pic_buff1->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
    401                             ps_pic_buff1->u1_pic_type);
    402             ps_dec->u4_pred_info_pkd_idx++;
    403             ps_cur_mb_info->u1_num_pred_parts++;
    404 
    405 
    406             }
    407 
    408             /******    (0,0) Motion vectors DMA     *****/
    409 
    410             {
    411                 pred_info_pkd_t *ps_pred_pkd;
    412                WORD16 i2_mv[2];
    413                WORD8 i1_ref_idx= 0;
    414 
    415                i2_mv[0] = 0;
    416                i2_mv[1] = 0;
    417 
    418                ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
    419             ih264d_fill_pred_info(i2_mv,4,4,0,i1_pred,
    420                             ps_pred_pkd,ps_pic_buff1->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
    421                             ps_pic_buff1->u1_pic_type);
    422             ps_dec->u4_pred_info_pkd_idx++;
    423             ps_cur_mb_info->u1_num_pred_parts++;
    424 
    425 
    426             }
    427         }
    428     }
    429 
    430     /*u1_col = *(ps_col_pic->pu1_col_zero_flag + s_mvdirect.i4_mv_indices[0]);
    431      u1_col &= 1;
    432      u1_init = 0;*/
    433 
    434     for(i = 0; i < s_mvdirect.i1_num_partitions; i++)
    435     {
    436         partition_size = s_mvdirect.i1_partitionsize[i];
    437         u1_sub_mb_num = s_mvdirect.i1_submb_num[i];
    438 
    439         sub_partition = partition_size >> 2;
    440         partition_size &= 0x3;
    441         u1_mb_partw = pu1_mb_partw[partition_size];
    442         u1_mb_parth = pu1_mb_parth[partition_size];
    443         u2_mask = mask_table[partition_size];
    444         if(sub_partition != 0)
    445         {
    446             u1_mb_partw >>= 1;
    447             u1_mb_parth >>= 1;
    448             u2_mask = sub_mask_table[partition_size];
    449         }
    450 
    451         u1_col_zero_flag = *(ps_col_pic->pu1_col_zero_flag
    452                         + s_mvdirect.i4_mv_indices[i]);
    453         u1_col_zero_flag = u1_col_zero_flag & 0x01;
    454 
    455         /*if(u1_col != u1_col_zero_flag)
    456          u1_init = 1;*/
    457 
    458         if(u1_zero_pred_cond_f || ((i1_ref_frame0 == 0) && (u1_col_zero_flag == 1)))
    459         {
    460             pi2_final_mv0 = &i2_def_mv[0];
    461             ui2_mask_fwd |= (u2_mask << u1_sub_mb_num);
    462         }
    463         else
    464             pi2_final_mv0 = &i2_spat_pred_mv[0];
    465 
    466         if(u1_zero_pred_cond_b || ((i1_ref_frame1 == 0) && (u1_col_zero_flag == 1)))
    467         {
    468             pi2_final_mv1 = &i2_def_mv[0];
    469             ui2_mask_bwd |= (u2_mask << u1_sub_mb_num);
    470         }
    471         else
    472             pi2_final_mv1 = &i2_spat_pred_mv[2];
    473 
    474         if(ps_cur_mb_info->u1_Mux != 1)
    475         {
    476             /*u1_sub_mb_x = u1_sub_mb_num & 0x03;
    477              uc_sub_mb_y = (u1_sub_mb_num >> 2);*/
    478             if(i1_ref_frame0 >= 0)
    479             {
    480 
    481                 {
    482                     pred_info_pkd_t *ps_pred_pkd;
    483                    WORD8 i1_ref_idx= 0;
    484 
    485                    ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
    486                 ih264d_fill_pred_info(pi2_final_mv0,u1_mb_partw,u1_mb_parth,u1_sub_mb_num,i1_pred,
    487                                 ps_pred_pkd,ps_pic_buff0->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
    488                                 ps_pic_buff0->u1_pic_type);
    489                 ps_dec->u4_pred_info_pkd_idx++;
    490                 ps_cur_mb_info->u1_num_pred_parts++;
    491 
    492 
    493                 }
    494 
    495             }
    496 
    497             if(i1_ref_frame1 >= 0)
    498             {
    499                 {
    500                     pred_info_pkd_t *ps_pred_pkd;
    501                    WORD8 i1_ref_idx= 0;
    502 
    503                    ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
    504                 ih264d_fill_pred_info(pi2_final_mv1,u1_mb_partw,u1_mb_parth,u1_sub_mb_num,i1_pred,
    505                                 ps_pred_pkd,ps_pic_buff1->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
    506                                 ps_pic_buff1->u1_pic_type);
    507                 ps_dec->u4_pred_info_pkd_idx++;
    508                 ps_cur_mb_info->u1_num_pred_parts++;
    509 
    510 
    511                 }
    512             }
    513         }
    514 
    515         /* Replication optimisation */
    516         s_temp_mv_pred.i2_mv[0] = pi2_final_mv0[0];
    517         s_temp_mv_pred.i2_mv[1] = pi2_final_mv0[1];
    518         s_temp_mv_pred.i2_mv[2] = pi2_final_mv1[0];
    519         s_temp_mv_pred.i2_mv[3] = pi2_final_mv1[1];
    520 
    521         /* Calculating colocated zero information */
    522         {
    523             WORD16 i2_mv_x = 0, i2_mv_y = 0;
    524             /*************************************/
    525             /* If(bit2 and bit3 set)             */
    526             /* then                              */
    527             /*  (bit0 and bit1) => submmbmode    */
    528             /*  (bit2 and bit3) => mbmode        */
    529             /* else                              */
    530             /*  (bit0 and bit1) => mbmode        */
    531             /*************************************/
    532             UWORD8 u1_packed_mb_sub_mb_mode =
    533                             sub_partition ? (s_mvdirect.i1_partitionsize[i]) : ((s_mvdirect.i1_partitionsize[i])
    534                                                             << 2);
    535 
    536             if(i1_ref_frame0 >= 0)
    537             {
    538                 i2_mv_x = pi2_final_mv0[0];
    539                 i2_mv_y = pi2_final_mv0[1];
    540             }
    541             else
    542             {
    543                 i2_mv_x = pi2_final_mv1[0];
    544                 i2_mv_y = pi2_final_mv1[1];
    545             }
    546 
    547             u1_colz = (ps_cur_mb_info->u1_mb_field_decodingflag << 1)
    548                             | ((u1_final_ref_idx == 0) && (ABS(i2_mv_x) <= 1)
    549                                             && (ABS(i2_mv_y) <= 1));
    550             u1_colz |= (u1_packed_mb_sub_mb_mode << 4);
    551         }
    552         ps_mv = ps_mv_nmb_start + u1_sub_mb_num;
    553         ih264d_rep_mv_colz(ps_dec, &s_temp_mv_pred, ps_mv, u1_sub_mb_num, u1_colz,
    554                            u1_mb_parth, u1_mb_partw);
    555     }
    556     i = 0;
    557     if(i1_ref_frame0 >= 0)
    558         ps_cur_mb_info->u2_mask[i++] = ui2_mask_fwd;
    559     if(i1_ref_frame1 >= 0)
    560         ps_cur_mb_info->u2_mask[i] = ui2_mask_bwd;
    561 
    562     /*if(u1_init)
    563      H264_DEC_DEBUG_PRINT("hit\n");
    564      else
    565      H264_DEC_DEBUG_PRINT("miss\n");*/
    566 
    567     return OK;
    568 }
    569 
    570 /*!
    571  **************************************************************************
    572  * \if Function name : ih264d_decode_temporal_direct \endif
    573  *
    574  * \brief
    575  *    Decodes temporal direct mode.
    576  *
    577  * \return
    578  *    None.
    579  *
    580  **************************************************************************
    581  */
    582 WORD32 ih264d_decode_temporal_direct(dec_struct_t * ps_dec,
    583                                      UWORD8 u1_wd_x,
    584                                      dec_mb_info_t * ps_cur_mb_info,
    585                                      UWORD8 u1_mb_num)
    586 {
    587     struct pic_buffer_t *ps_pic_buff0, *ps_pic_buff1, *ps_col_pic;
    588     mv_pred_t *ps_mv, s_temp_mv_pred;
    589     UWORD8 u1_sub_mb_num;
    590     UWORD8 u1_mbaff = ps_dec->ps_cur_slice->u1_mbaff_frame_flag;
    591     WORD16 i2_mv_x0, i2_mv_y0, i2_mv_x1, i2_mv_y1;
    592     UWORD8 u1_mb_partw, u1_mb_parth;
    593     UWORD8 i, partition_size, sub_partition;
    594     UWORD32 *pui32_weight_ofsts = NULL;
    595     directmv_t s_mvdirect;
    596     const UWORD8 *pu1_mb_parth = (const UWORD8 *)gau1_ih264d_mb_parth;
    597     const UWORD8 *pu1_mb_partw = (const UWORD8 *)gau1_ih264d_mb_partw;
    598     WORD8 c_refFrm0, c_refFrm1;
    599     UWORD8 u1_ref_idx0, u1_is_cur_mb_fld;
    600     WORD32 pic0_poc, pic1_poc, cur_poc;
    601     WORD32 ret = 0;
    602 
    603     u1_is_cur_mb_fld = ps_cur_mb_info->u1_mb_field_decodingflag;
    604     ps_pic_buff1 = ps_dec->ps_ref_pic_buf_lx[1][0];
    605 
    606     /**********************************************************************/
    607     /* Call the function which gets the number of partitions and          */
    608     /* partition info of colocated Mb                                     */
    609     /**********************************************************************/
    610     ps_dec->pf_parse_mvdirect(ps_dec, ps_dec->ps_col_pic, &s_mvdirect, u1_wd_x,
    611                            ps_dec->i4_submb_ofst, ps_cur_mb_info);
    612     ps_col_pic = ps_dec->ps_col_pic;
    613 
    614     for(i = 0; i < s_mvdirect.i1_num_partitions; i++)
    615     {
    616         UWORD8 u1_colz;
    617         partition_size = s_mvdirect.i1_partitionsize[i];
    618         u1_sub_mb_num = s_mvdirect.i1_submb_num[i];
    619         ps_mv = ps_col_pic->ps_mv + s_mvdirect.i4_mv_indices[i];
    620 
    621         /* This should be removed to catch unitialized memory read */
    622         u1_ref_idx0 = 0;
    623 
    624         sub_partition = partition_size >> 2;
    625         partition_size &= 0x3;
    626         u1_mb_partw = pu1_mb_partw[partition_size];
    627         u1_mb_parth = pu1_mb_parth[partition_size];
    628         if(sub_partition != 0)
    629         {
    630             u1_mb_partw >>= 1;
    631             u1_mb_parth >>= 1;
    632         }
    633         c_refFrm0 = ps_mv->i1_ref_frame[0];
    634         c_refFrm1 = ps_mv->i1_ref_frame[1];
    635 
    636         if((c_refFrm0 == -1) && (c_refFrm1 == -1))
    637         {
    638             u1_ref_idx0 = 0;
    639             ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][0];
    640             if(u1_mbaff && u1_is_cur_mb_fld)
    641             {
    642                 if(ps_cur_mb_info->u1_topmb)
    643                 {
    644                     pic0_poc = ps_pic_buff0->i4_top_field_order_cnt;
    645                     pic1_poc = ps_pic_buff1->i4_top_field_order_cnt;
    646                     cur_poc = ps_dec->ps_cur_pic->i4_top_field_order_cnt;
    647                 }
    648                 else
    649                 {
    650                     pic1_poc = ps_pic_buff1->i4_bottom_field_order_cnt;
    651                     cur_poc = ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
    652                     ps_pic_buff1 = ps_dec->ps_ref_pic_buf_lx[1][MAX_REF_BUFS];
    653                     pic0_poc = ps_pic_buff0->i4_bottom_field_order_cnt;
    654                     ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][MAX_REF_BUFS];
    655                 }
    656             }
    657             else
    658             {
    659                 pic0_poc = ps_pic_buff0->i4_avg_poc;
    660                 pic1_poc = ps_pic_buff1->i4_avg_poc;
    661                 cur_poc = ps_dec->ps_cur_pic->i4_poc;
    662             }
    663         }
    664         else
    665         {
    666             UWORD8 uc_i, u1_num_frw_ref_pics;
    667             UWORD8 buf_id, u1_pic_type;
    668             buf_id = ps_mv->u1_col_ref_pic_idx;
    669             u1_pic_type = ps_mv->u1_pic_type;
    670             if(ps_dec->ps_cur_slice->u1_field_pic_flag)
    671             {
    672                 if(s_mvdirect.u1_vert_mv_scale == FRM_TO_FLD)
    673                 {
    674                     u1_pic_type = TOP_FLD;
    675                     if(ps_dec->ps_cur_slice->u1_bottom_field_flag)
    676                         u1_pic_type = BOT_FLD;
    677                 }
    678             }
    679             u1_num_frw_ref_pics =
    680                             ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0];
    681 
    682             for(uc_i = 0; uc_i < u1_num_frw_ref_pics; uc_i++)
    683             {
    684                 if(ps_dec->ps_cur_slice->u1_field_pic_flag)
    685                 {
    686                     if(ps_dec->ps_ref_pic_buf_lx[0][uc_i]->u1_mv_buf_id == buf_id)
    687                     {
    688                         if(ps_dec->ps_ref_pic_buf_lx[0][uc_i]->u1_pic_type
    689                                         == u1_pic_type)
    690                         {
    691                             u1_ref_idx0 = uc_i;
    692                             break;
    693                         }
    694                     }
    695                 }
    696                 else
    697                 {
    698                     if(ps_dec->ps_ref_pic_buf_lx[0][uc_i]->u1_mv_buf_id == buf_id)
    699                     {
    700                         u1_ref_idx0 = uc_i;
    701                         break;
    702                     }
    703                 }
    704             }
    705 
    706             ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][u1_ref_idx0];
    707             ps_pic_buff1 = ps_dec->ps_ref_pic_buf_lx[1][0];
    708 
    709             if(u1_mbaff && u1_is_cur_mb_fld)
    710             {
    711                 pic0_poc = ps_pic_buff0->i4_top_field_order_cnt;
    712                 u1_ref_idx0 <<= 1;
    713                 if(s_mvdirect.u1_vert_mv_scale == ONE_TO_ONE)
    714                 {
    715                     if(u1_pic_type == BOT_FLD)
    716                     {
    717                         pic0_poc = ps_pic_buff0->i4_bottom_field_order_cnt;
    718                         ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][(u1_ref_idx0
    719                                         >> 1) + MAX_REF_BUFS];
    720                         if(ps_cur_mb_info->u1_topmb)
    721                             u1_ref_idx0++;
    722                     }
    723                     else
    724                     {
    725                         if(1 - ps_cur_mb_info->u1_topmb)
    726                             u1_ref_idx0++;
    727                     }
    728                 }
    729                 if(s_mvdirect.u1_vert_mv_scale == FRM_TO_FLD)
    730                 {
    731                     if(1 - ps_cur_mb_info->u1_topmb)
    732                     {
    733                         pic0_poc = ps_pic_buff0->i4_bottom_field_order_cnt;
    734                         ps_pic_buff0 = ps_dec->ps_ref_pic_buf_lx[0][(u1_ref_idx0
    735                                         >> 1) + MAX_REF_BUFS];
    736                     }
    737                 }
    738                 if(ps_cur_mb_info->u1_topmb)
    739                 {
    740                     pic1_poc = ps_pic_buff1->i4_top_field_order_cnt;
    741                     cur_poc = ps_dec->ps_cur_pic->i4_top_field_order_cnt;
    742                 }
    743                 else
    744                 {
    745                     pic1_poc = ps_pic_buff1->i4_bottom_field_order_cnt;
    746                     cur_poc = ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
    747                     ps_pic_buff1 = ps_dec->ps_ref_pic_buf_lx[1][MAX_REF_BUFS];
    748                 }
    749             }
    750             else
    751             {
    752                 pic0_poc = ps_pic_buff0->i4_avg_poc;
    753                 pic1_poc = ps_pic_buff1->i4_avg_poc;
    754                 cur_poc = ps_dec->ps_cur_pic->i4_poc;
    755             }
    756         }
    757         {
    758             WORD16 i16_td;
    759             WORD32 diff;
    760             if(c_refFrm0 >= 0)
    761             {
    762                 i2_mv_x0 = ps_mv->i2_mv[0];
    763                 i2_mv_y0 = ps_mv->i2_mv[1];
    764             }
    765             else if(c_refFrm1 >= 0)
    766             {
    767                 i2_mv_x0 = ps_mv->i2_mv[2];
    768                 i2_mv_y0 = ps_mv->i2_mv[3];
    769             }
    770             else
    771             {
    772                 i2_mv_x0 = 0;
    773                 i2_mv_y0 = 0;
    774             }
    775             /* If FRM_TO_FLD or FLD_TO_FRM scale the "y" component of the colocated Mv*/
    776             if(s_mvdirect.u1_vert_mv_scale == FRM_TO_FLD)
    777             {
    778                 i2_mv_y0 /= 2;
    779             }
    780             else if(s_mvdirect.u1_vert_mv_scale == FLD_TO_FRM)
    781             {
    782                 i2_mv_y0 *= 2;
    783             }
    784 
    785             diff = pic1_poc - pic0_poc;
    786             i16_td = CLIP_S8(diff);
    787             if((ps_pic_buff0->u1_is_short == 0) || (i16_td == 0))
    788             {
    789                 i2_mv_x1 = 0;
    790                 i2_mv_y1 = 0;
    791             }
    792             else
    793             {
    794                 WORD16 i16_tb, i16_tx, i2_dist_scale_factor, i16_temp;
    795 
    796                 diff = cur_poc - pic0_poc;
    797                 i16_tb = CLIP_S8(diff);
    798 
    799                 i16_tx = (16384 + ABS(SIGN_POW2_DIV(i16_td, 1))) / i16_td;
    800                 i2_dist_scale_factor = CLIP_S11(
    801                                             (((i16_tb * i16_tx) + 32) >> 6));
    802                 i16_temp = (i2_mv_x0 * i2_dist_scale_factor + 128) >> 8;
    803                 i2_mv_x1 = i16_temp - i2_mv_x0;
    804                 i2_mv_x0 = i16_temp;
    805 
    806                 i16_temp = (i2_mv_y0 * i2_dist_scale_factor + 128) >> 8;
    807                 i2_mv_y1 = i16_temp - i2_mv_y0;
    808                 i2_mv_y0 = i16_temp;
    809             }
    810             {
    811                 mv_pred_t *ps_mv;
    812 
    813                 /*u1_sub_mb_x = u1_sub_mb_num & 0x03;
    814                  uc_sub_mb_y = u1_sub_mb_num >> 2;*/
    815                 if(ps_dec->ps_cur_pps->u1_wted_bipred_idc)
    816                 {
    817                     UWORD8 u1_idx =
    818                                     u1_ref_idx0
    819                                                     * ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1];
    820                     UWORD8 u1_scale_ref = u1_mbaff && u1_is_cur_mb_fld;
    821                     if(u1_scale_ref)
    822                         u1_idx >>= 1;
    823                     pui32_weight_ofsts = (UWORD32*)&ps_dec->pu4_wt_ofsts[2
    824                                     * X3(u1_idx)];
    825                     if(u1_scale_ref
    826                                     && (ps_dec->ps_cur_pps->u1_wted_bipred_idc
    827                                                     == 2))
    828                     {
    829                         WORD16 i2_ref_idx;
    830                         i2_ref_idx = u1_ref_idx0;
    831                         i2_ref_idx *=
    832                                         (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1]
    833                                                         << 1);
    834                         if(!ps_cur_mb_info->u1_topmb)
    835                             i2_ref_idx +=
    836                                             (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0]
    837                                                             << 1)
    838                                                             * (ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1]
    839                                                                             << 1);
    840                         pui32_weight_ofsts =
    841                                         (UWORD32*)&ps_dec->pu4_mbaff_wt_mat[2
    842                                                         * X3(i2_ref_idx)];
    843                     }
    844                 }
    845                 {
    846                     pred_info_pkd_t *ps_pred_pkd;
    847                    WORD16 i2_mv[2];
    848                    WORD8 i1_ref_idx= 0;
    849 
    850                    i2_mv[0] = i2_mv_x0;
    851                    i2_mv[1] = i2_mv_y0;
    852 
    853                    ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
    854                 ih264d_fill_pred_info(i2_mv,u1_mb_partw,u1_mb_parth,u1_sub_mb_num,PRED_L0 | PRED_L1,
    855                                 ps_pred_pkd,ps_pic_buff0->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
    856                                 ps_pic_buff0->u1_pic_type);
    857                 ps_dec->u4_pred_info_pkd_idx++;
    858                 ps_cur_mb_info->u1_num_pred_parts++;
    859 
    860 
    861                 }
    862                 {
    863                    pred_info_pkd_t *ps_pred_pkd;
    864                    WORD16 i2_mv[2];
    865                    WORD8 i1_ref_idx= 0;
    866 
    867                    i2_mv[0] = i2_mv_x1;
    868                    i2_mv[1] = i2_mv_y1;
    869 
    870                    ps_pred_pkd = ps_dec->ps_pred_pkd + ps_dec->u4_pred_info_pkd_idx;
    871                 ih264d_fill_pred_info(i2_mv,u1_mb_partw,u1_mb_parth,u1_sub_mb_num,PRED_L0 | PRED_L1,
    872                                 ps_pred_pkd,ps_pic_buff1->u1_pic_buf_id,i1_ref_idx,pui32_weight_ofsts,
    873                                 ps_pic_buff1->u1_pic_type);
    874                 ps_dec->u4_pred_info_pkd_idx++;
    875                 ps_cur_mb_info->u1_num_pred_parts++;
    876 
    877 
    878                 }
    879 
    880                 /* Replication optimisation */
    881                 s_temp_mv_pred.i2_mv[0] = i2_mv_x0;
    882                 s_temp_mv_pred.i2_mv[1] = i2_mv_y0;
    883                 s_temp_mv_pred.i2_mv[2] = i2_mv_x1;
    884                 s_temp_mv_pred.i2_mv[3] = i2_mv_y1;
    885                 s_temp_mv_pred.i1_ref_frame[0] = u1_ref_idx0;
    886                 s_temp_mv_pred.i1_ref_frame[1] = 0;
    887                 s_temp_mv_pred.u1_col_ref_pic_idx = ps_pic_buff0->u1_mv_buf_id;
    888                 s_temp_mv_pred.u1_pic_type = ps_pic_buff0->u1_pic_type;
    889                 ps_mv = ps_dec->ps_mv_cur + (u1_mb_num << 4) + u1_sub_mb_num;
    890 
    891                 {
    892                     WORD16 i2_mv_x = 0, i2_mv_y = 0;
    893                     UWORD8 u1_packed_mb_sub_mb_mode =
    894                                     sub_partition ? (s_mvdirect.i1_partitionsize[i]) : ((s_mvdirect.i1_partitionsize[i])
    895                                                                     << 2);
    896 
    897                     if(c_refFrm0 >= 0)
    898                     {
    899                         i2_mv_x = i2_mv_x0;
    900                         i2_mv_y = i2_mv_y0;
    901                     }
    902                     else
    903                     {
    904                         i2_mv_x = i2_mv_x1;
    905                         i2_mv_y = i2_mv_y1;
    906                     }
    907 
    908                     u1_colz =
    909                                     (ps_cur_mb_info->u1_mb_field_decodingflag << 1)
    910                                                     | ((u1_ref_idx0 == 0)
    911                                                                     && (ABS(i2_mv_x)
    912                                                                                     <= 1)
    913                                                                     && (ABS(i2_mv_y)
    914                                                                                     <= 1));
    915                     u1_colz |= (u1_packed_mb_sub_mb_mode << 4);
    916                 }
    917                 ih264d_rep_mv_colz(ps_dec, &s_temp_mv_pred, ps_mv, u1_sub_mb_num,
    918                                    u1_colz, u1_mb_parth, u1_mb_partw);
    919             }
    920         }
    921     }
    922     /* return value set to UWORD8 to make it homogeneous  */
    923     /* with decodespatialdirect                           */
    924     return OK;
    925 }
    926 
    927 void ih264d_convert_frm_to_fld_list(struct pic_buffer_t *ps_ref_pic_buf_lx,
    928                                     UWORD8 *pu1_L0,
    929                                     dec_struct_t *ps_dec,
    930                                     UWORD8 u1_num_short_term_bufs)
    931 {
    932     UWORD8 uc_count = *pu1_L0, i, uc_l1, uc_lx, j;
    933     struct pic_buffer_t *ps_ref_lx[2], *ps_ref_pic_lx;
    934     UWORD8 u1_bottom_field_flag;
    935     dec_slice_params_t *ps_cur_slice;
    936     UWORD8 u1_ref[2], u1_fld[2], u1_same_fld, u1_op_fld;
    937     UWORD32 ui_half_num_of_sub_mbs;
    938 
    939     uc_l1 = 0;
    940     uc_lx = 0;
    941     ps_cur_slice = ps_dec->ps_cur_slice;
    942     ps_ref_pic_lx = ps_ref_pic_buf_lx - MAX_REF_BUFS;
    943     ps_ref_lx[0] = ps_ref_pic_buf_lx;
    944     ps_ref_lx[1] = ps_ref_pic_buf_lx;
    945     u1_bottom_field_flag = ps_cur_slice->u1_bottom_field_flag;
    946     ui_half_num_of_sub_mbs = ((ps_dec->u2_pic_ht * ps_dec->u2_pic_wd) >> 5);
    947     if(u1_bottom_field_flag)
    948     {
    949         u1_ref[0] = BOT_REF;
    950         u1_ref[1] = TOP_REF;
    951         u1_fld[0] = BOT_FLD;
    952         u1_fld[1] = TOP_FLD;
    953         u1_same_fld = BOT_FLD;
    954         u1_op_fld = TOP_FLD;
    955     }
    956     else
    957     {
    958         u1_ref[0] = TOP_REF;
    959         u1_ref[1] = BOT_REF;
    960         u1_fld[0] = TOP_FLD;
    961         u1_fld[1] = BOT_FLD;
    962         u1_same_fld = TOP_FLD;
    963         u1_op_fld = BOT_FLD;
    964     }
    965 
    966     /* Create the field list starting with all the short term     */
    967     /* frames followed by all the long term frames. No long term  */
    968     /* reference field should have a list idx less than a short   */
    969     /* term reference field during initiailization.               */
    970 
    971     for(j = 0; j < 2; j++)
    972     {
    973         i = ((j == 0) ? 0 : u1_num_short_term_bufs);
    974         uc_count = ((j == 0) ? u1_num_short_term_bufs : *pu1_L0);
    975         for(; i < uc_count; i++, ps_ref_lx[0]++)
    976         {
    977             /* Search field of same parity in Frame list */
    978             if((ps_ref_lx[0]->u1_pic_type & u1_ref[0])) // || ((ps_ref_lx[0]->u1_picturetype & 0x3) == 0))
    979             {
    980                 /* Insert PIC of same parity in RefPicList */
    981                 ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_lx, ps_ref_lx[0]);
    982                 ps_ref_pic_lx->i4_pic_num = (ps_ref_pic_lx->i4_pic_num * 2 + 1);
    983                 ps_ref_pic_lx->u1_long_term_pic_num =
    984                                 (ps_ref_pic_lx->u1_long_term_frm_idx * 2 + 1);
    985                 ps_ref_pic_lx->u1_pic_type = u1_same_fld;
    986                 if(u1_fld[0] & BOT_FLD)
    987                 {
    988                     ps_ref_pic_lx->u1_pic_type = BOT_FLD;
    989                     ps_ref_pic_lx->pu1_buf1 += ps_ref_pic_lx->u2_frm_wd_y;
    990                     ps_ref_pic_lx->pu1_buf2 += ps_ref_pic_lx->u2_frm_wd_uv;
    991                     ps_ref_pic_lx->pu1_buf3 += ps_ref_pic_lx->u2_frm_wd_uv;
    992                     if(ps_ref_pic_lx->u1_picturetype & 0x3)
    993                     {
    994                         ps_ref_pic_lx->pu1_col_zero_flag += ui_half_num_of_sub_mbs;
    995                         ps_ref_pic_lx->ps_mv += ui_half_num_of_sub_mbs;
    996                     }
    997                     ps_ref_pic_lx->i4_poc =
    998                                     ps_ref_pic_lx->i4_bottom_field_order_cnt;
    999                     ps_ref_pic_lx->i4_avg_poc =
   1000                                     ps_ref_pic_lx->i4_bottom_field_order_cnt;
   1001                 }
   1002                 else
   1003                 {
   1004                     ps_ref_pic_lx->u1_pic_type = TOP_FLD;
   1005                     ps_ref_pic_lx->i4_poc = ps_ref_pic_lx->i4_top_field_order_cnt;
   1006                     ps_ref_pic_lx->i4_avg_poc =
   1007                                     ps_ref_pic_lx->i4_top_field_order_cnt;
   1008                 }
   1009 
   1010                 ps_ref_pic_lx++;
   1011                 uc_lx++;
   1012                 /* Find field of opposite parity */
   1013                 if(uc_l1 < uc_count && ps_ref_lx[1])
   1014                 {
   1015                     while(!(ps_ref_lx[1]->u1_pic_type & u1_ref[1]))
   1016                     {
   1017                         ps_ref_lx[1]++;
   1018                         uc_l1++;
   1019                         if(uc_l1 >= uc_count)
   1020                             ps_ref_lx[1] = 0;
   1021                         if(!ps_ref_lx[1])
   1022                             break;
   1023                     }
   1024 
   1025                     if(ps_ref_lx[1])
   1026                     {
   1027                         uc_l1++;
   1028                         ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_lx,
   1029                                                            ps_ref_lx[1]);
   1030                         ps_ref_pic_lx->u1_pic_type = u1_op_fld;
   1031                         ps_ref_pic_lx->i4_pic_num = (ps_ref_pic_lx->i4_pic_num * 2);
   1032                         ps_ref_pic_lx->u1_long_term_pic_num =
   1033                                         (ps_ref_pic_lx->u1_long_term_frm_idx * 2);
   1034                         if(u1_fld[1] & BOT_FLD)
   1035                         {
   1036                             ps_ref_pic_lx->u1_pic_type = BOT_FLD;
   1037                             ps_ref_pic_lx->pu1_buf1 += ps_ref_pic_lx->u2_frm_wd_y;
   1038                             ps_ref_pic_lx->pu1_buf2 += ps_ref_pic_lx->u2_frm_wd_uv;
   1039                             ps_ref_pic_lx->pu1_buf3 += ps_ref_pic_lx->u2_frm_wd_uv;
   1040                             if(ps_ref_pic_lx->u1_picturetype & 0x3)
   1041                             {
   1042                                 ps_ref_pic_lx->pu1_col_zero_flag +=
   1043                                                 ui_half_num_of_sub_mbs;
   1044                                 ps_ref_pic_lx->ps_mv += ui_half_num_of_sub_mbs;
   1045                             }
   1046                             ps_ref_pic_lx->i4_poc =
   1047                                             ps_ref_pic_lx->i4_bottom_field_order_cnt;
   1048                             ps_ref_pic_lx->i4_avg_poc =
   1049                                             ps_ref_pic_lx->i4_bottom_field_order_cnt;
   1050                         }
   1051                         else
   1052                         {
   1053                             ps_ref_pic_lx->u1_pic_type = TOP_FLD;
   1054                             ps_ref_pic_lx->i4_poc =
   1055                                             ps_ref_pic_lx->i4_top_field_order_cnt;
   1056                             ps_ref_pic_lx->i4_avg_poc =
   1057                                             ps_ref_pic_lx->i4_top_field_order_cnt;
   1058                         }
   1059                         ps_ref_pic_lx++;
   1060                         uc_lx++;
   1061                         ps_ref_lx[1]++;
   1062                     }
   1063                 }
   1064             }
   1065         }
   1066 
   1067         /* Same parity fields are over, now insert left over opposite parity fields */
   1068         /** Added  if(ps_ref_lx[1]) for error checks */
   1069         if(ps_ref_lx[1])
   1070         {
   1071             for(; uc_l1 < uc_count; uc_l1++)
   1072             {
   1073                 if(ps_ref_lx[1]->u1_pic_type & u1_ref[1])
   1074                 {
   1075                     /* Insert PIC of opposite parity in RefPicList */
   1076                     ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_lx,
   1077                                                        ps_ref_lx[1]);
   1078                     ps_ref_pic_lx->u1_pic_type = u1_op_fld;
   1079                     ps_ref_pic_lx->i4_pic_num = (ps_ref_pic_lx->i4_pic_num * 2);
   1080                     ps_ref_pic_lx->u1_long_term_pic_num =
   1081                                     (ps_ref_pic_lx->u1_long_term_frm_idx * 2);
   1082                     if(u1_op_fld == BOT_FLD)
   1083                     {
   1084                         ps_ref_pic_lx->u1_pic_type = BOT_FLD;
   1085                         ps_ref_pic_lx->pu1_buf1 += ps_ref_pic_lx->u2_frm_wd_y;
   1086                         ps_ref_pic_lx->pu1_buf2 += ps_ref_pic_lx->u2_frm_wd_uv;
   1087                         ps_ref_pic_lx->pu1_buf3 += ps_ref_pic_lx->u2_frm_wd_uv;
   1088                         if(ps_ref_pic_lx->u1_picturetype & 0x3)
   1089                         {
   1090                             ps_ref_pic_lx->pu1_col_zero_flag +=
   1091                                             ui_half_num_of_sub_mbs;
   1092                             ps_ref_pic_lx->ps_mv += ui_half_num_of_sub_mbs;
   1093                         }
   1094                         ps_ref_pic_lx->i4_poc =
   1095                                         ps_ref_pic_lx->i4_bottom_field_order_cnt;
   1096                         ps_ref_pic_lx->i4_avg_poc =
   1097                                         ps_ref_pic_lx->i4_bottom_field_order_cnt;
   1098                     }
   1099                     else
   1100                     {
   1101                         ps_ref_pic_lx->i4_poc =
   1102                                         ps_ref_pic_lx->i4_top_field_order_cnt;
   1103                         ps_ref_pic_lx->i4_avg_poc =
   1104                                         ps_ref_pic_lx->i4_top_field_order_cnt;
   1105                     }
   1106                     ps_ref_pic_lx++;
   1107                     uc_lx++;
   1108                     ps_ref_lx[1]++;
   1109                 }
   1110             }
   1111         }
   1112     }
   1113     *pu1_L0 = uc_lx;
   1114 }
   1115 
   1116 void ih264d_convert_frm_mbaff_list(dec_struct_t *ps_dec)
   1117 {
   1118     struct pic_buffer_t **ps_ref_pic_lx;
   1119     UWORD8 u1_max_ref_idx, idx;
   1120     UWORD16 u2_frm_wd_y, u2_frm_wd_uv;
   1121     struct pic_buffer_t **ps_ref_pic_buf_lx;
   1122     UWORD32 u4_half_num_of_sub_mbs = ((ps_dec->u2_pic_ht * ps_dec->u2_pic_wd) >> 5);
   1123 
   1124     ps_ref_pic_buf_lx = ps_dec->ps_ref_pic_buf_lx[0];
   1125     ps_ref_pic_lx = ps_dec->ps_ref_pic_buf_lx[0];
   1126     u1_max_ref_idx = ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[0];
   1127     for(idx = 0; idx < u1_max_ref_idx; idx++)
   1128     {
   1129         ps_ref_pic_lx[idx]->u1_pic_type = TOP_FLD;
   1130         ps_ref_pic_lx[idx]->i4_poc = ps_ref_pic_lx[idx]->i4_top_field_order_cnt;
   1131 
   1132     }
   1133     u2_frm_wd_y = ps_dec->u2_frm_wd_y;
   1134     u2_frm_wd_uv = ps_dec->u2_frm_wd_uv;
   1135 
   1136     for(idx = 0; idx < u1_max_ref_idx; idx++)
   1137     {
   1138         *ps_ref_pic_lx[idx + MAX_REF_BUFS] = *ps_ref_pic_buf_lx[idx];
   1139         ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_buf1 =
   1140                         ps_ref_pic_buf_lx[idx]->pu1_buf1 + u2_frm_wd_y;
   1141         ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_buf2 =
   1142                         ps_ref_pic_buf_lx[idx]->pu1_buf2 + u2_frm_wd_uv;
   1143         ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_buf3 =
   1144                         ps_ref_pic_buf_lx[idx]->pu1_buf3 + u2_frm_wd_uv;
   1145 
   1146         ps_ref_pic_lx[idx + MAX_REF_BUFS]->u1_pic_type = BOT_FLD;
   1147         ps_ref_pic_lx[idx + MAX_REF_BUFS]->i4_poc =
   1148                         ps_ref_pic_buf_lx[idx]->i4_bottom_field_order_cnt;
   1149         if(ps_ref_pic_buf_lx[idx]->u1_picturetype & 0x3)
   1150         {
   1151             ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_col_zero_flag =
   1152                             ps_ref_pic_buf_lx[idx]->pu1_col_zero_flag
   1153                                             + u4_half_num_of_sub_mbs;
   1154             ps_ref_pic_lx[idx + MAX_REF_BUFS]->ps_mv =
   1155                             ps_ref_pic_buf_lx[idx]->ps_mv + u4_half_num_of_sub_mbs;
   1156         }
   1157     }
   1158 
   1159     if(ps_dec->u1_B)
   1160     {
   1161         ps_ref_pic_buf_lx = ps_dec->ps_ref_pic_buf_lx[1];
   1162         ps_ref_pic_lx = ps_dec->ps_ref_pic_buf_lx[1];
   1163         u1_max_ref_idx = ps_dec->ps_cur_slice->u1_num_ref_idx_lx_active[1];
   1164         for(idx = 0; idx < u1_max_ref_idx; idx++)
   1165         {
   1166             ps_ref_pic_lx[idx]->u1_pic_type = TOP_FLD;
   1167             ps_ref_pic_lx[idx]->i4_poc = ps_ref_pic_lx[idx]->i4_top_field_order_cnt;
   1168 
   1169         }
   1170 
   1171         for(idx = 0; idx < u1_max_ref_idx; idx++)
   1172         {
   1173             *ps_ref_pic_lx[idx + MAX_REF_BUFS] = *ps_ref_pic_buf_lx[idx];
   1174             ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_buf1 =
   1175                             ps_ref_pic_buf_lx[idx]->pu1_buf1 + u2_frm_wd_y;
   1176             ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_buf2 =
   1177                             ps_ref_pic_buf_lx[idx]->pu1_buf2 + u2_frm_wd_uv;
   1178             ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_buf3 =
   1179                             ps_ref_pic_buf_lx[idx]->pu1_buf3 + u2_frm_wd_uv;
   1180             ps_ref_pic_lx[idx + MAX_REF_BUFS]->u1_pic_type = BOT_FLD;
   1181             ps_ref_pic_lx[idx + MAX_REF_BUFS]->i4_poc =
   1182                             ps_ref_pic_buf_lx[idx]->i4_bottom_field_order_cnt;
   1183 
   1184             if(ps_ref_pic_buf_lx[idx]->u1_picturetype & 0x3)
   1185             {
   1186                 ps_ref_pic_lx[idx + MAX_REF_BUFS]->pu1_col_zero_flag =
   1187                                 ps_ref_pic_buf_lx[idx]->pu1_col_zero_flag
   1188                                                 + u4_half_num_of_sub_mbs;
   1189                 ps_ref_pic_lx[idx + MAX_REF_BUFS]->ps_mv =
   1190                                 ps_ref_pic_buf_lx[idx]->ps_mv
   1191                                                 + u4_half_num_of_sub_mbs;
   1192             }
   1193         }
   1194     }
   1195 }
   1196 /*!
   1197  **************************************************************************
   1198  * \if Function name : ih264d_init_ref_idx_lx_b \endif
   1199  *
   1200  * \brief
   1201  *    Initializes forward and backward refernce lists for B slice decoding.
   1202  *
   1203  *
   1204  * \return
   1205  *    0 on Success and Error code otherwise
   1206  **************************************************************************
   1207  */
   1208 void ih264d_init_ref_idx_lx_b(dec_struct_t *ps_dec)
   1209 {
   1210     struct pic_buffer_t *ps_ref_pic_buf_lx;
   1211     dpb_manager_t *ps_dpb_mgr;
   1212     struct dpb_info_t *ps_next_dpb;
   1213     WORD32 i_cur_poc, i_max_st_poc, i_min_st_poc, i_ref_poc, i_temp_poc;
   1214     WORD8 i;
   1215     UWORD8 u1_max_lt_index, u1_min_lt_index;
   1216     UWORD32 u4_lt_index;
   1217     UWORD8 u1_field_pic_flag;
   1218     dec_slice_params_t *ps_cur_slice;
   1219     UWORD8 u1_L0, u1_L1;
   1220     UWORD8 u1_num_short_term_bufs;
   1221     UWORD8 u1_max_ref_idx_l0, u1_max_ref_idx_l1;
   1222 
   1223     ps_cur_slice = ps_dec->ps_cur_slice;
   1224     u1_field_pic_flag = ps_cur_slice->u1_field_pic_flag;
   1225     u1_max_ref_idx_l0 = ps_cur_slice->u1_num_ref_idx_lx_active[0]
   1226                     << u1_field_pic_flag;
   1227     u1_max_ref_idx_l1 = ps_cur_slice->u1_num_ref_idx_lx_active[1]
   1228                     << u1_field_pic_flag;
   1229 
   1230     ps_dpb_mgr = ps_dec->ps_dpb_mgr;
   1231     /* Get the current POC */
   1232     i_cur_poc = ps_dec->ps_cur_pic->i4_poc;
   1233 
   1234     /* Get MaxStPOC,MinStPOC,MaxLt,MinLt */
   1235     i_max_st_poc = i_cur_poc;
   1236     i_min_st_poc = i_cur_poc;
   1237     u1_max_lt_index = MAX_REF_BUFS + 1;
   1238     u1_min_lt_index = MAX_REF_BUFS + 1;
   1239     /* Start from ST head */
   1240     ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
   1241     for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
   1242     {
   1243         i_ref_poc = ps_next_dpb->ps_pic_buf->i4_poc;
   1244         if(i_ref_poc < i_cur_poc)
   1245         {
   1246             /* RefPic Buf POC is before Current POC in display order */
   1247             i_min_st_poc = MIN(i_min_st_poc, i_ref_poc);
   1248         }
   1249         else
   1250         {
   1251             /* RefPic Buf POC is after Current POC in display order */
   1252             i_max_st_poc = MAX(i_max_st_poc, i_ref_poc);
   1253         }
   1254 
   1255         /* Chase the next link */
   1256         ps_next_dpb = ps_next_dpb->ps_prev_short;
   1257     }
   1258 
   1259     /* Start from LT head */
   1260     ps_next_dpb = ps_dpb_mgr->ps_dpb_ht_head;
   1261     if(ps_next_dpb)
   1262     {
   1263         u1_max_lt_index = ps_next_dpb->u1_lt_idx;
   1264         u1_min_lt_index = ps_next_dpb->u1_lt_idx;
   1265     }
   1266     for(i = 0; i < ps_dpb_mgr->u1_num_lt_ref_bufs; i++)
   1267     {
   1268         u4_lt_index = ps_next_dpb->u1_lt_idx;
   1269         u1_max_lt_index = (UWORD8)(MAX(u1_max_lt_index, u4_lt_index));
   1270         u1_min_lt_index = (UWORD8)(MIN(u1_min_lt_index, u4_lt_index));
   1271 
   1272         /* Chase the next link */
   1273         ps_next_dpb = ps_next_dpb->ps_prev_long;
   1274     }
   1275 
   1276     /* 1. Initialize refIdxL0 */
   1277     u1_L0 = 0;
   1278     if(u1_field_pic_flag)
   1279     {
   1280         ps_ref_pic_buf_lx = ps_dpb_mgr->ps_init_dpb[0][0];
   1281         ps_ref_pic_buf_lx += MAX_REF_BUFS;
   1282         i_temp_poc = i_cur_poc;
   1283     }
   1284     else
   1285     {
   1286         ps_ref_pic_buf_lx = ps_dpb_mgr->ps_init_dpb[0][0];
   1287         i_temp_poc = i_cur_poc - 1;
   1288     }
   1289     /* Arrange all short term buffers in output order as given by POC */
   1290     /* 1.1 Arrange POC's less than CurrPOC in the descending POC order starting
   1291      from (CurrPOC - 1)*/
   1292     for(; i_temp_poc >= i_min_st_poc; i_temp_poc--)
   1293     {
   1294         /* Start from ST head */
   1295         ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
   1296         for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
   1297         {
   1298             if((WORD32)ps_next_dpb->ps_pic_buf->i4_poc == i_temp_poc)
   1299             {
   1300                 /* Copy info in pic buffer */
   1301                 ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_buf_lx,
   1302                                                    ps_next_dpb->ps_pic_buf);
   1303                 ps_ref_pic_buf_lx++;
   1304                 u1_L0++;
   1305                 break;
   1306             }
   1307             ps_next_dpb = ps_next_dpb->ps_prev_short;
   1308         }
   1309     }
   1310 
   1311     {
   1312         /* 1.2. Arrange POC's more than CurrPOC in the ascending POC order starting
   1313          from (CurrPOC + 1)*/
   1314         for(i_temp_poc = i_cur_poc + 1; i_temp_poc <= i_max_st_poc; i_temp_poc++)
   1315         {
   1316             /* Start from ST head */
   1317             ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
   1318             for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
   1319             {
   1320                 if((WORD32)ps_next_dpb->ps_pic_buf->i4_poc == i_temp_poc)
   1321                 {
   1322                     ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_buf_lx,
   1323                                                        ps_next_dpb->ps_pic_buf);
   1324                     ps_ref_pic_buf_lx++;
   1325                     u1_L0++;
   1326                     break;
   1327                 }
   1328                 ps_next_dpb = ps_next_dpb->ps_prev_short;
   1329             }
   1330         }
   1331     }
   1332 
   1333     /* 1.3 Arrange all Long term buffers in ascending order, in LongtermIndex */
   1334     /* Start from ST head */
   1335 
   1336     u1_num_short_term_bufs = u1_L0;
   1337     for(u4_lt_index = u1_min_lt_index; u4_lt_index <= u1_max_lt_index; u4_lt_index++)
   1338     {
   1339         ps_next_dpb = ps_dpb_mgr->ps_dpb_ht_head;
   1340         for(i = 0; i < ps_dpb_mgr->u1_num_lt_ref_bufs; i++)
   1341         {
   1342             if(ps_next_dpb->u1_lt_idx == u4_lt_index)
   1343             {
   1344                 ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_buf_lx,
   1345                                                    ps_next_dpb->ps_pic_buf);
   1346                 ps_ref_pic_buf_lx->u1_long_term_pic_num =
   1347                                 ps_ref_pic_buf_lx->u1_long_term_frm_idx;
   1348 
   1349                 ps_ref_pic_buf_lx++;
   1350                 u1_L0++;
   1351                 break;
   1352             }
   1353             ps_next_dpb = ps_next_dpb->ps_prev_long;
   1354         }
   1355     }
   1356 
   1357     if(u1_field_pic_flag)
   1358     {
   1359         /* Initialize the rest of the entries in the */
   1360         /* reference list to handle of errors        */
   1361         {
   1362             UWORD8 u1_i;
   1363             pic_buffer_t *ps_ref_pic;
   1364 
   1365             ps_ref_pic = ps_dpb_mgr->ps_init_dpb[0][0] + MAX_REF_BUFS;
   1366 
   1367             if(NULL == ps_ref_pic->pu1_buf1)
   1368             {
   1369                 ps_ref_pic = ps_dec->ps_cur_pic;
   1370             }
   1371             for(u1_i = u1_L0; u1_i < u1_max_ref_idx_l0; u1_i++)
   1372             {
   1373                 *ps_ref_pic_buf_lx = *ps_ref_pic;
   1374                 ps_ref_pic_buf_lx++;
   1375             }
   1376         }
   1377         ih264d_convert_frm_to_fld_list(
   1378                         ps_dpb_mgr->ps_init_dpb[0][0] + MAX_REF_BUFS, &u1_L0,
   1379                         ps_dec, u1_num_short_term_bufs);
   1380 
   1381         ps_ref_pic_buf_lx = ps_dpb_mgr->ps_init_dpb[0][0] + u1_L0;
   1382     }
   1383 
   1384     ps_dec->ps_cur_slice->u1_initial_list_size[0] = u1_L0;
   1385 
   1386     /* Initialize the rest of the entries in the */
   1387     /* reference list to handle of errors        */
   1388     {
   1389         UWORD8 u1_i;
   1390         pic_buffer_t *ps_ref_pic;
   1391 
   1392         ps_ref_pic = ps_dpb_mgr->ps_init_dpb[0][0];
   1393 
   1394         if(NULL == ps_ref_pic->pu1_buf1)
   1395         {
   1396             ps_ref_pic = ps_dec->ps_cur_pic;
   1397         }
   1398         for(u1_i = u1_L0; u1_i < u1_max_ref_idx_l0; u1_i++)
   1399         {
   1400             *ps_ref_pic_buf_lx = *ps_ref_pic;
   1401             ps_ref_pic_buf_lx++;
   1402         }
   1403     }
   1404     {
   1405         /* 2. Initialize refIdxL1 */
   1406         u1_L1 = 0;
   1407         if(u1_field_pic_flag)
   1408         {
   1409             ps_ref_pic_buf_lx = ps_dpb_mgr->ps_init_dpb[1][0] + MAX_REF_BUFS;
   1410         }
   1411         else
   1412         {
   1413             ps_ref_pic_buf_lx = ps_dpb_mgr->ps_init_dpb[1][0];
   1414         }
   1415 
   1416         /* 2.1. Arrange POC's more than CurrPOC in the ascending POC order starting
   1417          from (CurrPOC + 1)*/
   1418         for(i_temp_poc = i_cur_poc + 1; i_temp_poc <= i_max_st_poc; i_temp_poc++)
   1419         {
   1420             /* Start from ST head */
   1421             ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
   1422             for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
   1423             {
   1424                 if((WORD32)ps_next_dpb->ps_pic_buf->i4_poc == i_temp_poc)
   1425                 {
   1426                     ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_buf_lx,
   1427                                                        ps_next_dpb->ps_pic_buf);
   1428                     ps_ref_pic_buf_lx++;
   1429                     u1_L1++;
   1430                     break;
   1431                 }
   1432                 ps_next_dpb = ps_next_dpb->ps_prev_short;
   1433             }
   1434         }
   1435 
   1436         if(u1_field_pic_flag)
   1437         {
   1438             i_temp_poc = i_cur_poc;
   1439         }
   1440         else
   1441         {
   1442             i_temp_poc = i_cur_poc - 1;
   1443         }
   1444 
   1445         /* Arrange all short term buffers in output order as given by POC */
   1446         /* 2.2 Arrange POC's less than CurrPOC in the descending POC order starting
   1447          from (CurrPOC - 1)*/
   1448         for(; i_temp_poc >= i_min_st_poc; i_temp_poc--)
   1449         {
   1450             /* Start from ST head */
   1451             ps_next_dpb = ps_dpb_mgr->ps_dpb_st_head;
   1452             for(i = 0; i < ps_dpb_mgr->u1_num_st_ref_bufs; i++)
   1453             {
   1454                 if((WORD32)ps_next_dpb->ps_pic_buf->i4_poc == i_temp_poc)
   1455                 {
   1456                     ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_buf_lx,
   1457                                                        ps_next_dpb->ps_pic_buf);
   1458                     ps_ref_pic_buf_lx++;
   1459                     u1_L1++;
   1460                     break;
   1461                 }
   1462                 ps_next_dpb = ps_next_dpb->ps_prev_short;
   1463             }
   1464         }
   1465 
   1466         /* 2.3 Arrange all Long term buffers in ascending order, in LongtermIndex */
   1467         /* Start from ST head */
   1468         u1_num_short_term_bufs = u1_L1;
   1469 
   1470         for(u4_lt_index = u1_min_lt_index; u4_lt_index <= u1_max_lt_index;
   1471                         u4_lt_index++)
   1472         {
   1473             ps_next_dpb = ps_dpb_mgr->ps_dpb_ht_head;
   1474             for(i = 0; i < ps_dpb_mgr->u1_num_lt_ref_bufs; i++)
   1475             {
   1476                 if(ps_next_dpb->u1_lt_idx == u4_lt_index)
   1477                 {
   1478                     ih264d_insert_pic_in_ref_pic_listx(ps_ref_pic_buf_lx,
   1479                                                        ps_next_dpb->ps_pic_buf);
   1480                     ps_ref_pic_buf_lx->u1_long_term_pic_num =
   1481                                     ps_ref_pic_buf_lx->u1_long_term_frm_idx;
   1482                     ps_ref_pic_buf_lx++;
   1483                     u1_L1++;
   1484                     break;
   1485                 }
   1486                 ps_next_dpb = ps_next_dpb->ps_prev_long;
   1487             }
   1488         }
   1489 
   1490         if(u1_field_pic_flag)
   1491         {
   1492             /* Initialize the rest of the entries in the */
   1493             /* reference list to handle of errors        */
   1494             {
   1495                 UWORD8 u1_i;
   1496                 pic_buffer_t *ps_ref_pic;
   1497 
   1498                 ps_ref_pic = ps_dpb_mgr->ps_init_dpb[0][0] + MAX_REF_BUFS;
   1499 
   1500                 if(NULL == ps_ref_pic->pu1_buf1)
   1501                 {
   1502                     ps_ref_pic = ps_dec->ps_cur_pic;
   1503                 }
   1504                 for(u1_i = u1_L1; u1_i < u1_max_ref_idx_l1; u1_i++)
   1505                 {
   1506                     *ps_ref_pic_buf_lx = *ps_ref_pic;
   1507                     ps_ref_pic_buf_lx++;
   1508                 }
   1509             }
   1510 
   1511             ih264d_convert_frm_to_fld_list(
   1512                             ps_dpb_mgr->ps_init_dpb[1][0] + MAX_REF_BUFS,
   1513                             &u1_L1, ps_dec, u1_num_short_term_bufs);
   1514             ps_ref_pic_buf_lx = ps_dpb_mgr->ps_init_dpb[1][0] + u1_L1;
   1515         }
   1516 
   1517         ps_dec->ps_cur_slice->u1_initial_list_size[1] = u1_L1;
   1518 
   1519         /* Initialize the rest of the entries in the */
   1520         /* reference list to handle of errors        */
   1521         {
   1522             UWORD8 u1_i;
   1523             pic_buffer_t *ps_ref_pic;
   1524 
   1525             ps_ref_pic = ps_dpb_mgr->ps_init_dpb[0][0];
   1526 
   1527             if(NULL == ps_ref_pic->pu1_buf1)
   1528             {
   1529                 ps_ref_pic = ps_dec->ps_cur_pic;
   1530             }
   1531             for(u1_i = u1_L1; u1_i < u1_max_ref_idx_l1; u1_i++)
   1532             {
   1533                 *ps_ref_pic_buf_lx = *ps_ref_pic;
   1534                 ps_ref_pic_buf_lx++;
   1535             }
   1536         }
   1537 
   1538         /* If list0 and list 1 ebtries are same then swap the 0th and 1st entry */
   1539         /* of list 1                                                            */
   1540         {
   1541             struct pic_buffer_t *ps_ref_pic1_buf_l0, *ps_ref_pic1_buf_l1;
   1542             struct pic_buffer_t s_ref_pic1_buf_temp;
   1543 
   1544             ps_ref_pic1_buf_l0 = ps_dpb_mgr->ps_init_dpb[0][0];
   1545             ps_ref_pic1_buf_l1 = ps_dpb_mgr->ps_init_dpb[1][0];
   1546 
   1547             if((u1_L0 == u1_L1) && (u1_L0 > 1))
   1548             {
   1549                 WORD32 i_index, i_swap;
   1550 
   1551                 i_swap = 1;
   1552 
   1553                 for(i_index = 0; i_index < u1_L0; i_index++)
   1554                 {
   1555                     if((ps_ref_pic1_buf_l0[i_index]).pu1_buf1
   1556                                     != (ps_ref_pic1_buf_l1[i_index]).pu1_buf1)
   1557                     {
   1558                         i_swap = 0;
   1559                         break;
   1560                     }
   1561                 }
   1562                 if(1 == i_swap)
   1563                 {
   1564                     memcpy(&s_ref_pic1_buf_temp, &ps_ref_pic1_buf_l1[1],
   1565                            sizeof(struct pic_buffer_t));
   1566                     memcpy(&ps_ref_pic1_buf_l1[1], &ps_ref_pic1_buf_l1[0],
   1567                            sizeof(struct pic_buffer_t));
   1568                     memcpy(&ps_ref_pic1_buf_l1[0], &s_ref_pic1_buf_temp,
   1569                            sizeof(struct pic_buffer_t));
   1570                 }
   1571             }
   1572         }
   1573     }
   1574 }
   1575 
   1576 
   1577 
   1578 void ih264d_get_implicit_weights(dec_struct_t *ps_dec);
   1579 
   1580 /*!
   1581  **************************************************************************
   1582  * \if Function name : ih264d_one_to_one \endif
   1583  *
   1584  * \brief
   1585  *    Initializes forward and backward refernce lists for B slice decoding.
   1586  *
   1587  *
   1588  * \return
   1589  *    0 on Success and Error code otherwise
   1590  **************************************************************************
   1591  */
   1592 void ih264d_one_to_one(dec_struct_t *ps_dec,
   1593                        struct pic_buffer_t *ps_col_pic,
   1594                        directmv_t *ps_direct,
   1595                        UWORD8 u1_wd_x,
   1596                        WORD32 u2_sub_mb_ofst,
   1597                        dec_mb_info_t * ps_cur_mb_info)
   1598 {
   1599     UWORD8 *pu1_col_zero_flag_start, u1_col_mb_pred_mode, u1_num_blks, u1_sub_mb_num;
   1600     UWORD8 u1_init_colzero_flag;
   1601     UNUSED(ps_cur_mb_info);
   1602     pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag + u2_sub_mb_ofst;
   1603     u1_col_mb_pred_mode = pu1_col_zero_flag_start[ps_dec->u1_sub_mb_num];
   1604     u1_init_colzero_flag = u1_col_mb_pred_mode & 1;
   1605     u1_col_mb_pred_mode >>= 6;
   1606     ps_direct->u1_vert_mv_scale = ONE_TO_ONE;
   1607     ps_direct->u1_col_zeroflag_change = 0;
   1608 
   1609     if(u1_wd_x == MB_SIZE)
   1610     {
   1611         ps_dec->u1_currB_type = (!!u1_col_mb_pred_mode);
   1612         if(u1_col_mb_pred_mode == PRED_16x16)
   1613         {
   1614             ps_direct->i1_num_partitions = 1;
   1615             ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst;
   1616             ps_direct->i1_submb_num[0] = 0;
   1617             ps_direct->i1_partitionsize[0] = PRED_16x16;
   1618 
   1619             return;
   1620         }
   1621         else if(u1_col_mb_pred_mode < PRED_8x8)
   1622         {
   1623             ps_direct->i1_num_partitions = 2;
   1624             ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst;
   1625             ps_direct->i1_submb_num[0] = 0;
   1626             ps_direct->i1_partitionsize[0] = u1_col_mb_pred_mode;
   1627             u1_sub_mb_num = (u1_col_mb_pred_mode == PRED_16x8) ? 8 : 2;
   1628             ps_direct->i1_submb_num[1] = u1_sub_mb_num;
   1629             ps_direct->i4_mv_indices[1] = u2_sub_mb_ofst
   1630                             + ps_direct->i1_submb_num[1];
   1631             ps_direct->i1_partitionsize[1] = u1_col_mb_pred_mode;
   1632             if((pu1_col_zero_flag_start[u1_sub_mb_num] & 1) != u1_init_colzero_flag)
   1633                 ps_direct->u1_col_zeroflag_change = 1;
   1634             return;
   1635         }
   1636         else
   1637         {
   1638             u1_num_blks = 4;
   1639         }
   1640     }
   1641     else
   1642     {
   1643         u1_num_blks = 1;
   1644     }
   1645 
   1646     {
   1647         const UWORD8 *pu1_top_lt_mb_part_idx;
   1648         UWORD8 u1_col_sub_mb_pred_mode, uc_blk, u1_sub_blk, u1_submb_col = 0;
   1649         UWORD8 u1_num_sub_blks, uc_direct8x8inf, *pu1_col_zero_flag, u1_sub_mb_num;
   1650         const UWORD8 *pu1_num_sub_mb_part =
   1651                         (const UWORD8 *)gau1_ih264d_num_submb_part;
   1652         UWORD8 i1_num_partitions = 0, partition_size;
   1653         WORD32 mv_index;
   1654         const UWORD8 *pu1_top_lt_sub_mb_idx = gau1_ih264d_submb_indx_mod_sp_drct;
   1655 
   1656         u1_sub_mb_num = ps_dec->u1_sub_mb_num;
   1657         uc_direct8x8inf = ps_dec->ps_cur_slice->u1_direct_8x8_inference_flag;
   1658         pu1_top_lt_mb_part_idx = gau1_ih264d_top_left_mb_part_indx_mod
   1659                         + (PRED_8x8 << 1) + 1;
   1660 
   1661         for(uc_blk = 0; uc_blk < u1_num_blks; uc_blk++)
   1662         {
   1663             partition_size = PRED_8x8;
   1664             pu1_top_lt_sub_mb_idx = gau1_ih264d_submb_indx_mod_sp_drct;
   1665             if(uc_direct8x8inf == 1)
   1666             {
   1667                 u1_submb_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
   1668                 mv_index = u2_sub_mb_ofst + u1_submb_col;
   1669                 u1_num_sub_blks = 1;
   1670             }
   1671             else
   1672             {
   1673                 /* colMbPart is either 8x8, 8x4, 4x8, 4x4 */
   1674                 pu1_col_zero_flag = pu1_col_zero_flag_start + u1_sub_mb_num;
   1675                 u1_col_sub_mb_pred_mode = *pu1_col_zero_flag;
   1676                 u1_col_sub_mb_pred_mode = (u1_col_sub_mb_pred_mode & 0x30) >> 4;
   1677                 partition_size = (UWORD8)((u1_col_sub_mb_pred_mode)
   1678                                 | (PRED_8x8 << 2));
   1679                 mv_index = u2_sub_mb_ofst + u1_sub_mb_num;
   1680                 pu1_top_lt_sub_mb_idx += (u1_col_sub_mb_pred_mode << 1);
   1681                 u1_num_sub_blks = pu1_num_sub_mb_part[u1_col_sub_mb_pred_mode];
   1682 
   1683             }
   1684 
   1685             for(u1_sub_blk = 0; u1_sub_blk < u1_num_sub_blks;
   1686                             u1_sub_blk++, pu1_top_lt_sub_mb_idx++)
   1687             {
   1688                 u1_sub_mb_num += *pu1_top_lt_sub_mb_idx;
   1689                 mv_index += *pu1_top_lt_sub_mb_idx;
   1690                 ps_direct->i4_mv_indices[i1_num_partitions] = mv_index;
   1691                 ps_direct->i1_submb_num[i1_num_partitions] = u1_sub_mb_num;
   1692                 ps_direct->i1_partitionsize[i1_num_partitions] = partition_size;
   1693                 i1_num_partitions++;
   1694                 if(!uc_direct8x8inf)
   1695                     u1_submb_col = u1_sub_mb_num;
   1696                 if((pu1_col_zero_flag_start[u1_submb_col] & 1)
   1697                                 != u1_init_colzero_flag)
   1698                     ps_direct->u1_col_zeroflag_change = 1;
   1699             }
   1700             u1_sub_mb_num = *pu1_top_lt_mb_part_idx++;
   1701         }
   1702         ps_direct->i1_num_partitions = i1_num_partitions;
   1703     }
   1704 }
   1705 /*!
   1706  **************************************************************************
   1707  * \if Function name : ih264d_mbaff_cross_pmbair \endif
   1708  *
   1709  * \brief
   1710  *    Initializes forward and backward refernce lists for B slice decoding.
   1711  *
   1712  *
   1713  * \return
   1714  *    0 on Success and Error code otherwise
   1715  **************************************************************************
   1716  */
   1717 void ih264d_mbaff_cross_pmbair(dec_struct_t *ps_dec,
   1718                                struct pic_buffer_t *ps_col_pic,
   1719                                directmv_t *ps_direct,
   1720                                UWORD8 u1_wd_x,
   1721                                WORD32 u2_sub_mb_ofst,
   1722                                dec_mb_info_t * ps_cur_mb_info)
   1723 {
   1724     UWORD8 *pu1_col_zero_flag_start, *pu1_col_zero_flag, u1_sub_mb_num,
   1725                     uc_sub_mb_num_col;
   1726     UWORD8 *pu1_col_zero_flag_right_half;
   1727     WORD32 i4_force_8X8;
   1728     UWORD8 u1_num_blks, u1_col_mb_pred_mode, uc_blk, u1_col_sub_mb_pred_mode,
   1729                     u1_col_sub_mb_pred_mode_rt;
   1730     UWORD8 i1_num_partitions = 0, partition_size;
   1731 
   1732     WORD32 mv_index;
   1733 
   1734     UWORD8 u1_num_sub_blks;
   1735     UWORD8 u1_is_cur_mb_fld, i;
   1736     UWORD8 u1_init_colzero_flag;
   1737 
   1738     u1_is_cur_mb_fld = ps_cur_mb_info->u1_mb_field_decodingflag;
   1739     u1_sub_mb_num = ps_dec->u1_sub_mb_num;
   1740     ps_direct->u1_col_zeroflag_change = 0;
   1741     /*pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag + u2_sub_mb_ofst;
   1742      u1_col_mb_pred_mode = pu1_col_zero_flag_start[u1_sub_mb_num];
   1743      u1_init_colzero_flag = u1_col_mb_pred_mode & 1;
   1744      u1_col_mb_pred_mode >>= 6; */
   1745     if(0 == u1_is_cur_mb_fld)
   1746     {
   1747         ps_direct->u1_vert_mv_scale = FLD_TO_FRM;
   1748         if(u1_wd_x == MB_SIZE)
   1749         {
   1750             pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag
   1751                             + u2_sub_mb_ofst;
   1752             u1_col_mb_pred_mode = pu1_col_zero_flag_start[0];
   1753             u1_init_colzero_flag = u1_col_mb_pred_mode & 1;
   1754             u1_col_mb_pred_mode >>= 6;
   1755 
   1756 
   1757             if(u1_col_mb_pred_mode & 0x2)
   1758             {
   1759                 ps_dec->u1_currB_type = 1;
   1760                 if(u1_col_mb_pred_mode == PRED_8x16)
   1761                 {
   1762                     ps_direct->i1_num_partitions = 2;
   1763                     ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst;
   1764                     ps_direct->i1_submb_num[0] = 0;
   1765                     ps_direct->i1_partitionsize[0] = PRED_8x16;
   1766                     ps_direct->i4_mv_indices[1] = u2_sub_mb_ofst + 2;
   1767                     ps_direct->i1_submb_num[1] = 2;
   1768                     ps_direct->i1_partitionsize[1] = PRED_8x16;
   1769                     if((pu1_col_zero_flag_start[2] & 1) != u1_init_colzero_flag)
   1770                         ps_direct->u1_col_zeroflag_change = 1;
   1771                 }
   1772                 else
   1773                 {
   1774                     pu1_col_zero_flag = pu1_col_zero_flag_start + u1_sub_mb_num;
   1775                     u1_col_sub_mb_pred_mode = (*pu1_col_zero_flag & 0x10);/* 8x4 or 4x4 mode */
   1776 
   1777                     pu1_col_zero_flag_right_half = pu1_col_zero_flag_start
   1778                                     + u1_sub_mb_num + 2;
   1779                     u1_col_sub_mb_pred_mode_rt =
   1780                                     (*pu1_col_zero_flag_right_half & 0x10);/* 8x4 or 4x4 mode */
   1781 
   1782                     i4_force_8X8 = (u1_col_sub_mb_pred_mode)
   1783                                     || (u1_col_sub_mb_pred_mode_rt);
   1784                     if(i4_force_8X8)
   1785                     {
   1786                         u1_num_sub_blks = 2;
   1787                         partition_size = PRED_8x8;
   1788                     }
   1789                     else
   1790                     {
   1791                         partition_size = PRED_8x16;
   1792                         u1_num_sub_blks = 1;
   1793                     }
   1794 
   1795                     for(i = 0; i < 2; i++)
   1796                     {
   1797                         for(uc_blk = 0; uc_blk < u1_num_sub_blks; uc_blk++)
   1798                         {
   1799                             uc_sub_mb_num_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
   1800                             uc_sub_mb_num_col &= 0x7;
   1801                             mv_index = u2_sub_mb_ofst + uc_sub_mb_num_col;
   1802 
   1803                             ps_direct->i4_mv_indices[i1_num_partitions] =
   1804                                             mv_index;
   1805                             ps_direct->i1_submb_num[i1_num_partitions] =
   1806                                             u1_sub_mb_num;
   1807                             ps_direct->i1_partitionsize[i1_num_partitions] =
   1808                                             partition_size;
   1809                             i1_num_partitions++;
   1810                             if((pu1_col_zero_flag_start[uc_sub_mb_num_col] & 1)
   1811                                             != u1_init_colzero_flag)
   1812                                 ps_direct->u1_col_zeroflag_change = 1;
   1813                             u1_sub_mb_num += 8;
   1814                         }
   1815                         u1_sub_mb_num = 2; /* move to second half of Cur MB */
   1816                     }
   1817                     ps_direct->i1_num_partitions = i1_num_partitions;
   1818                     return;
   1819                 }
   1820             }
   1821             else
   1822             {
   1823                 ps_direct->i1_num_partitions = 1;
   1824                 ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst;
   1825                 ps_direct->i1_submb_num[0] = 0;
   1826                 ps_direct->i1_partitionsize[0] = PRED_16x16;
   1827                 ps_dec->u1_currB_type = 0;
   1828                 return;
   1829             }
   1830         }
   1831         else
   1832         {
   1833             uc_sub_mb_num_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
   1834             uc_sub_mb_num_col &= 0x7;
   1835 
   1836             ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst + uc_sub_mb_num_col;
   1837             ps_direct->i1_submb_num[0] = u1_sub_mb_num;
   1838             ps_direct->i1_partitionsize[0] = PRED_8x8;
   1839             ps_direct->i1_num_partitions = 1;
   1840         }
   1841     }
   1842     else
   1843     {
   1844         ps_direct->u1_vert_mv_scale = FRM_TO_FLD;
   1845         pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag + u2_sub_mb_ofst;
   1846         u1_init_colzero_flag = pu1_col_zero_flag_start[0] & 1;
   1847 
   1848         if(u1_wd_x == MB_SIZE)
   1849         {
   1850             UWORD8 u1_submb_col;
   1851             UWORD8 *puc_colZeroFlagStart_bot_mb, uc_colMbPredMode_bot_mb;
   1852 
   1853             pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag
   1854                             + u2_sub_mb_ofst;
   1855             u1_col_mb_pred_mode = pu1_col_zero_flag_start[u1_sub_mb_num] >> 6;
   1856 
   1857             puc_colZeroFlagStart_bot_mb = ps_col_pic->pu1_col_zero_flag
   1858                             + u2_sub_mb_ofst + 16;
   1859             uc_colMbPredMode_bot_mb = puc_colZeroFlagStart_bot_mb[8] >> 6;
   1860 
   1861             i4_force_8X8 = (u1_col_mb_pred_mode & 0x2)
   1862                             || (uc_colMbPredMode_bot_mb & 0x2);
   1863             if(i4_force_8X8)
   1864             {
   1865                 u1_num_blks = 2;
   1866                 partition_size = PRED_8x8;
   1867             }
   1868             else
   1869             {
   1870                 u1_num_blks = 1;
   1871                 partition_size = PRED_16x8;
   1872             }
   1873 
   1874             ps_dec->u1_currB_type = 1;
   1875             /*As this mb is derived from 2 Mbs min no of partitions = 2*/
   1876             for(i = 0; i < 2; i++)
   1877             {
   1878 
   1879                 pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag
   1880                                 + u2_sub_mb_ofst;
   1881                 u1_col_mb_pred_mode = pu1_col_zero_flag_start[u1_sub_mb_num] >> 6;
   1882 
   1883                 for(uc_blk = 0; uc_blk < u1_num_blks; uc_blk++)
   1884                 {
   1885                     u1_submb_col = (u1_sub_mb_num & 0x7) ? 1 : 0;
   1886                     u1_submb_col += u1_sub_mb_num;
   1887                     mv_index = u2_sub_mb_ofst + u1_submb_col;
   1888 
   1889 
   1890                     ps_direct->i4_mv_indices[i1_num_partitions] = mv_index;
   1891                     ps_direct->i1_submb_num[i1_num_partitions] = u1_sub_mb_num;
   1892                     ps_direct->i1_partitionsize[i1_num_partitions] =
   1893                                     partition_size;
   1894                     i1_num_partitions++;
   1895                     if((pu1_col_zero_flag_start[u1_submb_col] & 1)
   1896                                     != u1_init_colzero_flag)
   1897                         ps_direct->u1_col_zeroflag_change = 1;
   1898                     u1_sub_mb_num += 2;
   1899                 }
   1900                 u1_sub_mb_num = 8; /* move to second half of Cur MB */
   1901                 u2_sub_mb_ofst += 16;/* move to next Colocated MB */
   1902             }
   1903             ps_direct->i1_num_partitions = i1_num_partitions;
   1904             return;
   1905         }
   1906         else
   1907         {
   1908             uc_sub_mb_num_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
   1909             uc_sub_mb_num_col &= 0xb;
   1910             u2_sub_mb_ofst += (u1_sub_mb_num >> 3) ? 16 : 0;
   1911 
   1912             ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst + uc_sub_mb_num_col;
   1913             ps_direct->i1_submb_num[0] = u1_sub_mb_num;
   1914             ps_direct->i1_partitionsize[0] = PRED_8x8;
   1915             ps_direct->i1_num_partitions = 1;
   1916             return;
   1917         }
   1918     }
   1919 }
   1920 /*!
   1921  **************************************************************************
   1922  * \if Function name : ih264d_cal_col_pic \endif
   1923  *
   1924  * \brief
   1925  *    Finds the colocated picture.
   1926  *
   1927  *
   1928  * \return
   1929  *    0 on Success and Error code otherwise
   1930  **************************************************************************
   1931  */
   1932 WORD32 ih264d_cal_col_pic(dec_struct_t *ps_dec)
   1933 {
   1934     struct pic_buffer_t* ps_col_pic = ps_dec->ps_col_pic;
   1935     UWORD8 uc_curpictype, uc_colpictype;
   1936     ps_col_pic = ps_dec->ps_ref_pic_buf_lx[1][0];
   1937     uc_curpictype = (ps_dec->ps_cur_pic->u1_picturetype & 0x7);
   1938     uc_colpictype = (ps_col_pic->u1_picturetype & 0x7);
   1939     if(uc_curpictype == FRM_PIC)
   1940     {
   1941         if(uc_colpictype == FRM_PIC)
   1942             ps_dec->pf_parse_mvdirect = ih264d_one_to_one;
   1943         else if(uc_colpictype == COMP_FLD_PAIR)
   1944         {
   1945             ps_dec->pf_parse_mvdirect = ih264d_fld_to_frm;
   1946             if(ps_col_pic->i4_top_field_order_cnt
   1947                             >= ps_col_pic->i4_bottom_field_order_cnt)
   1948             {
   1949                 struct pic_buffer_t* ps_tempPic = ps_col_pic;
   1950                 UWORD32 ui_half_num_of_sub_mbs = ((ps_dec->u2_pic_ht
   1951                                 * ps_dec->u2_pic_wd) >> 5);
   1952                 ps_col_pic = ps_dec->ps_ref_pic_buf_lx[1][MAX_REF_BUFS];
   1953                 /* memcpy ps_tempPic to ps_col_pic */
   1954                 *ps_col_pic = *ps_tempPic;
   1955                 ps_col_pic->pu1_buf1 = ps_tempPic->pu1_buf1
   1956                                 + ps_tempPic->u2_frm_wd_y;
   1957                 ps_col_pic->pu1_buf2 = ps_tempPic->pu1_buf2
   1958                                 + ps_tempPic->u2_frm_wd_uv;
   1959                 ps_col_pic->pu1_buf3 = ps_tempPic->pu1_buf3
   1960                                 + ps_tempPic->u2_frm_wd_uv;
   1961                 ps_col_pic->pu1_col_zero_flag = ps_tempPic->pu1_col_zero_flag
   1962                                 + ui_half_num_of_sub_mbs;
   1963                 ps_col_pic->ps_mv = ps_tempPic->ps_mv + ui_half_num_of_sub_mbs;
   1964 
   1965 
   1966                 ps_col_pic->u1_pic_type = 0;/*complementary reference field pair-refering as frame */
   1967 
   1968 
   1969 
   1970             }
   1971         }
   1972         else
   1973         {
   1974             UWORD32 i4_error_code;
   1975             i4_error_code = ERROR_DBP_MANAGER_T;
   1976 //          i4_error_code |= 1<<IVD_CORRUPTEDDATA;
   1977             return i4_error_code;
   1978         }
   1979     }
   1980     else if(uc_curpictype == AFRM_PIC)
   1981     {
   1982         ps_dec->pf_parse_mvdirect = ih264d_fld_to_mbaff;
   1983     }
   1984     else /* must be a field*/
   1985     {
   1986         if(uc_colpictype == FRM_PIC)
   1987             ps_dec->pf_parse_mvdirect = ih264d_frm_to_fld;
   1988         else if(uc_colpictype == AFRM_PIC)
   1989             ps_dec->pf_parse_mvdirect = ih264d_mbaff_to_fld;
   1990         else
   1991             ps_dec->pf_parse_mvdirect = ih264d_one_to_one;
   1992     }
   1993     ps_dec->ps_col_pic = ps_col_pic;
   1994     return OK;
   1995 }
   1996 
   1997 /*!
   1998  **************************************************************************
   1999  * \if Function name : ih264d_frm_to_fld \endif
   2000  *
   2001  * \brief
   2002  *    Initializes forward and backward refernce lists for B slice decoding.
   2003  *
   2004  *
   2005  * \return
   2006  *    0 on Success and Error code otherwise
   2007  **************************************************************************
   2008  */
   2009 void ih264d_frm_to_fld(dec_struct_t *ps_dec,
   2010                        struct pic_buffer_t *ps_col_pic,
   2011                        directmv_t *ps_direct,
   2012                        UWORD8 u1_wd_x,
   2013                        WORD32 u2_sub_mb_ofst,
   2014                        dec_mb_info_t * ps_cur_mb_info)
   2015 {
   2016     UWORD8 *pu1_col_zero_flag_start, u1_sub_mb_num;
   2017     UWORD8 u1_num_blks, u1_col_mb_pred_mode, uc_blk;
   2018     UWORD8 i1_num_partitions = 0, partition_size, i;
   2019     WORD32 mv_index;
   2020     UWORD32 increment;
   2021     WORD32 i4_force_8X8;
   2022     UNUSED(ps_cur_mb_info);
   2023     ps_direct->u1_col_zeroflag_change = 1;
   2024     ps_direct->u1_vert_mv_scale = FRM_TO_FLD;
   2025     u1_sub_mb_num = ps_dec->u1_sub_mb_num;
   2026 
   2027     /* new calculation specific to this function */
   2028     if((ps_col_pic->u1_picturetype & 0x7) == FRM_PIC)
   2029     {
   2030         UWORD16 u2_frm_wd_in_mbs = ps_dec->u2_frm_wd_in_mbs;
   2031         increment = (u2_frm_wd_in_mbs << 4);
   2032         /*mbAddrCol = mbAddrCol1 */
   2033         u2_sub_mb_ofst = (ps_dec->u2_mbx
   2034                         + (2 * ps_dec->u2_mby * u2_frm_wd_in_mbs)) << 4;
   2035     }
   2036     else
   2037         increment = 16;
   2038 
   2039     if(u1_wd_x == MB_SIZE)
   2040     {
   2041         ps_dec->u1_currB_type = 1;
   2042 
   2043         {
   2044             UWORD8 *puc_colZeroFlagStart_bot_mb, uc_colMbPredMode_bot_mb;
   2045 
   2046             pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag
   2047                             + u2_sub_mb_ofst;
   2048             u1_col_mb_pred_mode = (*pu1_col_zero_flag_start >> 6);
   2049 
   2050             puc_colZeroFlagStart_bot_mb = ps_col_pic->pu1_col_zero_flag
   2051                             + u2_sub_mb_ofst + increment;
   2052             uc_colMbPredMode_bot_mb = (*puc_colZeroFlagStart_bot_mb >> 6);
   2053 
   2054             i4_force_8X8 = (u1_col_mb_pred_mode & 0x2)
   2055                             || (uc_colMbPredMode_bot_mb & 0x2);
   2056 
   2057             if(i4_force_8X8)
   2058             {
   2059                 u1_num_blks = 2;
   2060                 partition_size = PRED_8x8;
   2061             }
   2062             else
   2063             {
   2064                 partition_size = PRED_16x8;
   2065                 u1_num_blks = 1;
   2066             }
   2067         }
   2068 
   2069         /*As this mb is derived from 2 Mbs, min no of partitions = 2*/
   2070         for(i = 0; i < 2; i++)
   2071         {
   2072             for(uc_blk = 0; uc_blk < u1_num_blks; uc_blk++)
   2073             {
   2074                 mv_index = u2_sub_mb_ofst + u1_sub_mb_num;
   2075                 mv_index += (u1_sub_mb_num & 0x7) ? 1 : 0;
   2076 
   2077                 ps_direct->i4_mv_indices[i1_num_partitions] = mv_index;
   2078                 ps_direct->i1_submb_num[i1_num_partitions] = u1_sub_mb_num;
   2079                 ps_direct->i1_partitionsize[i1_num_partitions] = partition_size;
   2080                 i1_num_partitions++;
   2081 
   2082                 u1_sub_mb_num += 2;
   2083             }
   2084             u1_sub_mb_num = 8; /* move to second half of Cur MB */
   2085             u2_sub_mb_ofst += increment;/* move to next Colocated MB */
   2086         }
   2087         ps_direct->i1_num_partitions = i1_num_partitions;
   2088         return;
   2089     }
   2090     else
   2091     {
   2092         UWORD8 u1_sub_mb_num_col;
   2093         u1_sub_mb_num_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
   2094         u1_sub_mb_num_col &= 0xb;
   2095         u2_sub_mb_ofst += (u1_sub_mb_num >> 3) ? increment : 0;
   2096 
   2097         ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst + u1_sub_mb_num_col;
   2098         ps_direct->i1_submb_num[0] = u1_sub_mb_num;
   2099         ps_direct->i1_partitionsize[0] = PRED_8x8;
   2100         ps_direct->i1_num_partitions = 1;
   2101         return;
   2102     }
   2103 }
   2104 /*!
   2105  **************************************************************************
   2106  * \if Function name : ih264d_fld_to_frm \endif
   2107  *
   2108  * \brief
   2109  *    Initializes forward and backward refernce lists for B slice decoding.
   2110  *
   2111  *
   2112  * \return
   2113  *    0 on Success and Error code otherwise
   2114  **************************************************************************
   2115  */
   2116 void ih264d_fld_to_frm(dec_struct_t *ps_dec,
   2117                        struct pic_buffer_t *ps_col_pic,
   2118                        directmv_t *ps_direct,
   2119                        UWORD8 u1_wd_x,
   2120                        WORD32 u2_sub_mb_ofst,
   2121                        dec_mb_info_t * ps_cur_mb_info)
   2122 {
   2123     UWORD8 *pu1_col_zero_flag_start, *pu1_col_zero_flag,
   2124                     *pu1_col_zero_flag_right_half, u1_sub_mb_num, uc_sub_mb_num_col;
   2125     UWORD8 u1_col_mb_pred_mode, uc_blk;
   2126     WORD32 i4_force_8X8;
   2127 
   2128     UNUSED(ps_cur_mb_info);
   2129     ps_direct->u1_vert_mv_scale = FLD_TO_FRM;
   2130     ps_direct->u1_col_zeroflag_change = 1;
   2131     /* new calculation specific to this function for u2_sub_mb_ofst*/
   2132     u2_sub_mb_ofst = (ps_dec->u2_mbx
   2133                     + ((ps_dec->u2_mby >> 1) * ps_dec->u2_frm_wd_in_mbs)) << 4;
   2134     u2_sub_mb_ofst += ((ps_dec->u2_mby & 1) << 3);
   2135 
   2136     if(u1_wd_x == MB_SIZE)
   2137     {
   2138         pu1_col_zero_flag_start = ps_col_pic->pu1_col_zero_flag + u2_sub_mb_ofst;
   2139         u1_col_mb_pred_mode = (*pu1_col_zero_flag_start >> 6);
   2140         ps_dec->u1_currB_type = (!!u1_col_mb_pred_mode);
   2141 
   2142         if(u1_col_mb_pred_mode & 0x2)
   2143         {
   2144             if(u1_col_mb_pred_mode == PRED_8x16)
   2145             {
   2146                 ps_direct->i1_num_partitions = 2;
   2147                 ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst;
   2148                 ps_direct->i1_submb_num[0] = 0;
   2149                 ps_direct->i1_partitionsize[0] = PRED_8x16;
   2150                 ps_direct->i4_mv_indices[1] = u2_sub_mb_ofst + 2;
   2151                 ps_direct->i1_submb_num[1] = 2;
   2152                 ps_direct->i1_partitionsize[1] = PRED_8x16;
   2153             }
   2154             else
   2155             {
   2156                 UWORD8 i1_num_partitions = 0, partition_size;
   2157                 UWORD32 mv_index;
   2158                 UWORD8 u1_num_sub_blks, i, u1_col_sub_mb_pred_mode,
   2159                                 u1_col_sub_mb_pred_mode_rt;
   2160 
   2161                 u1_sub_mb_num = ps_dec->u1_sub_mb_num;
   2162 
   2163                 pu1_col_zero_flag = pu1_col_zero_flag_start + u1_sub_mb_num;
   2164                 u1_col_sub_mb_pred_mode = (*pu1_col_zero_flag & 0x10);/* 8x4 or 4x4 mode */
   2165 
   2166                 pu1_col_zero_flag_right_half = pu1_col_zero_flag_start + u1_sub_mb_num
   2167                                 + 2;
   2168                 u1_col_sub_mb_pred_mode_rt = (*pu1_col_zero_flag_right_half
   2169                                 & 0x10);/* 8x4 or 4x4 mode */
   2170 
   2171                 i4_force_8X8 = (u1_col_sub_mb_pred_mode)
   2172                                 || (u1_col_sub_mb_pred_mode_rt);
   2173                 if(i4_force_8X8)
   2174                 {
   2175                     u1_num_sub_blks = 2;
   2176                     partition_size = PRED_8x8;
   2177                 }
   2178                 else
   2179                 {
   2180                     partition_size = PRED_8x16;
   2181                     u1_num_sub_blks = 1;
   2182                 }
   2183 
   2184                 for(i = 0; i < 2; i++)
   2185                 {
   2186                     for(uc_blk = 0; uc_blk < u1_num_sub_blks; uc_blk++)
   2187                     {
   2188                         uc_sub_mb_num_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
   2189                         uc_sub_mb_num_col &= 0x7;
   2190                         mv_index = u2_sub_mb_ofst + uc_sub_mb_num_col;
   2191 
   2192                         ps_direct->i4_mv_indices[i1_num_partitions] = mv_index;
   2193                         ps_direct->i1_submb_num[i1_num_partitions] =
   2194                                         u1_sub_mb_num;
   2195                         ps_direct->i1_partitionsize[i1_num_partitions] =
   2196                                         partition_size;
   2197                         i1_num_partitions++;
   2198                         u1_sub_mb_num += 8;
   2199                     }
   2200 
   2201                     u1_sub_mb_num = 2; /* move to second half of Cur MB */
   2202 
   2203                 }
   2204                 ps_direct->i1_num_partitions = i1_num_partitions;
   2205                 return;
   2206             }
   2207         }
   2208         else
   2209         {
   2210             ps_direct->i1_num_partitions = 1;
   2211             ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst;
   2212             ps_direct->i1_submb_num[0] = 0;
   2213             ps_direct->i1_partitionsize[0] = PRED_16x16;
   2214             return;
   2215         }
   2216     }
   2217     else
   2218     {
   2219         u1_sub_mb_num = ps_dec->u1_sub_mb_num;
   2220         uc_sub_mb_num_col = u1_sub_mb_num | (u1_sub_mb_num >> 1);
   2221         uc_sub_mb_num_col &= 0x7;
   2222 
   2223         ps_direct->i4_mv_indices[0] = u2_sub_mb_ofst + uc_sub_mb_num_col;
   2224         ps_direct->i1_submb_num[0] = u1_sub_mb_num;
   2225         ps_direct->i1_partitionsize[0] = PRED_8x8;
   2226         ps_direct->i1_num_partitions = 1;
   2227     }
   2228 }
   2229 /*!
   2230  **************************************************************************
   2231  * \if Function name : ih264d_one_to_one \endif
   2232  *
   2233  * \brief
   2234  *    Initializes forward and backward refernce lists for B slice decoding.
   2235  *
   2236  *
   2237  * \return
   2238  *    0 on Success and Error code otherwise
   2239  **************************************************************************
   2240  */
   2241 void ih264d_mbaff_to_fld(dec_struct_t *ps_dec,
   2242                          struct pic_buffer_t *ps_col_pic,
   2243                          directmv_t *ps_direct,
   2244                          UWORD8 u1_wd_x,
   2245                          WORD32 u2_sub_mb_ofst,
   2246                          dec_mb_info_t * ps_cur_mb_info)
   2247 {
   2248     UWORD8* pu1_col_zero_flag, u1_iscol_mb_fld;
   2249     u2_sub_mb_ofst <<= 1;
   2250     pu1_col_zero_flag = ps_col_pic->pu1_col_zero_flag + u2_sub_mb_ofst;
   2251     u1_iscol_mb_fld = (*pu1_col_zero_flag & 0x2) >> 1;
   2252     if(u1_iscol_mb_fld)
   2253     {
   2254         u2_sub_mb_ofst += (ps_dec->ps_cur_slice->u1_bottom_field_flag << 4);
   2255         ih264d_one_to_one(ps_dec, ps_col_pic, ps_direct, u1_wd_x,
   2256                           u2_sub_mb_ofst, ps_cur_mb_info);
   2257     }
   2258     else
   2259         ih264d_frm_to_fld(ps_dec, ps_col_pic, ps_direct, u1_wd_x,
   2260                           u2_sub_mb_ofst, ps_cur_mb_info);
   2261 }
   2262 /*!
   2263  **************************************************************************
   2264  * \if Function name : ih264d_one_to_one \endif
   2265  *
   2266  * \brief
   2267  *    Initializes forward and backward refernce lists for B slice decoding.
   2268  *
   2269  *
   2270  * \return
   2271  *    0 on Success and Error code otherwise
   2272  **************************************************************************
   2273  */
   2274 void ih264d_fld_to_mbaff(dec_struct_t *ps_dec,
   2275                          struct pic_buffer_t *ps_col_pic,
   2276                          directmv_t *ps_direct,
   2277                          UWORD8 u1_wd_x,
   2278                          WORD32 u2_sub_mb_ofst,
   2279                          dec_mb_info_t * ps_cur_mb_info)
   2280 {
   2281     if((ps_col_pic->u1_picturetype & 0x7) == COMP_FLD_PAIR)
   2282     {
   2283         /* first calculate the colocated picture which varies with Mb */
   2284         UWORD8 u1_is_cur_mb_fld;
   2285         u1_is_cur_mb_fld = ps_cur_mb_info->u1_mb_field_decodingflag;
   2286         u2_sub_mb_ofst = (u2_sub_mb_ofst & 0xffe0); /* mbaddrCol5 = curmbaddr/2;*/
   2287         u2_sub_mb_ofst >>= 1;
   2288 
   2289         ps_col_pic = ps_dec->ps_ref_pic_buf_lx[1][0];
   2290         if(u1_is_cur_mb_fld)
   2291         {
   2292             if(1 - ps_cur_mb_info->u1_topmb)
   2293                 ps_col_pic = ps_dec->ps_ref_pic_buf_lx[1][MAX_REF_BUFS];
   2294 
   2295             ih264d_one_to_one(ps_dec, ps_col_pic, ps_direct, u1_wd_x,
   2296                               u2_sub_mb_ofst, ps_cur_mb_info);
   2297         }
   2298         else
   2299         {
   2300 
   2301             if(ABS(ps_col_pic->i4_top_field_order_cnt
   2302                             - ps_dec->ps_cur_pic->i4_poc) >=
   2303                             ABS(ps_dec->ps_cur_pic->i4_poc - ps_col_pic->i4_bottom_field_order_cnt))
   2304             {
   2305                 ps_col_pic = ps_dec->ps_ref_pic_buf_lx[1][MAX_REF_BUFS];
   2306             }
   2307 
   2308             if(ps_cur_mb_info->u1_topmb == 0)
   2309                 u2_sub_mb_ofst += 8;
   2310             ih264d_mbaff_cross_pmbair(ps_dec, ps_col_pic, ps_direct, u1_wd_x,
   2311                                       u2_sub_mb_ofst, ps_cur_mb_info);
   2312         }
   2313         ps_dec->ps_col_pic = ps_col_pic;
   2314     }
   2315     else
   2316     {
   2317         UWORD8* pu1_col_zero_flag = ps_col_pic->pu1_col_zero_flag
   2318                         + u2_sub_mb_ofst;
   2319         UWORD8 temp, u1_is_cur_mb_fld, u1_iscol_mb_fld;
   2320 
   2321         u1_iscol_mb_fld = (*pu1_col_zero_flag & 0x2) >> 1;
   2322         u1_is_cur_mb_fld = ps_cur_mb_info->u1_mb_field_decodingflag;
   2323         temp = (u1_iscol_mb_fld ^ u1_is_cur_mb_fld);
   2324 
   2325         if(temp == 0)
   2326             ih264d_one_to_one(ps_dec, ps_col_pic, ps_direct, u1_wd_x,
   2327                               u2_sub_mb_ofst, ps_cur_mb_info);
   2328         else
   2329         {
   2330             u2_sub_mb_ofst &= 0xffef;
   2331             if(u1_is_cur_mb_fld == 0)
   2332             {
   2333                 if(ABS(ps_col_pic->i4_top_field_order_cnt
   2334                                 - ps_dec->ps_cur_pic->i4_poc) >=
   2335                                 ABS(ps_dec->ps_cur_pic->i4_poc - ps_col_pic->i4_bottom_field_order_cnt))
   2336                 {
   2337                     u2_sub_mb_ofst += 0x10;
   2338                 }
   2339                 if(ps_cur_mb_info->u1_topmb == 0)
   2340                     u2_sub_mb_ofst += 8;
   2341             }
   2342             ih264d_mbaff_cross_pmbair(ps_dec, ps_col_pic, ps_direct, u1_wd_x,
   2343                                       u2_sub_mb_ofst, ps_cur_mb_info);
   2344         }
   2345     }
   2346 }
   2347 
   2348