Home | History | Annotate | Download | only in decoder
      1 /******************************************************************************
      2  *
      3  * Copyright (C) 2015 The Android Open Source Project
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at:
      8  *
      9  * http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  *
     17  *****************************************************************************
     18  * Originally developed and contributed by Ittiam Systems Pvt. Ltd, Bangalore
     19 */
     20 /*!
     21  **************************************************************************
     22  * \file ih264d_parse_slice.c
     23  *
     24  * \brief
     25  *    Contains routines that decodes a slice NAL unit
     26  *
     27  * \date
     28  *    19/12/2002
     29  *
     30  * \author  AI
     31  **************************************************************************
     32  */
     33 #include <string.h>
     34 #include "ih264_typedefs.h"
     35 #include "ih264_macros.h"
     36 #include "ih264_platform_macros.h"
     37 #include "ithread.h"
     38 #include "ih264d_structs.h"
     39 #include "ih264d_debug.h"
     40 #include "ih264d_bitstrm.h"
     41 #include "ih264d_parse_mb_header.h"
     42 #include "ih264d_process_bslice.h"
     43 #include "ih264d_process_pslice.h"
     44 #include "ih264d_parse_cavlc.h"
     45 #include "ih264d_utils.h"
     46 #include "ih264d_deblocking.h"
     47 #include "ih264d_defs.h"
     48 #include "ih264d_error_handler.h"
     49 #include "ih264d_tables.h"
     50 #include "ih264d_defs.h"
     51 #include "ih264d_mem_request.h"
     52 #include "ih264d_parse_islice.h"
     53 #include "ih264d_parse_slice.h"
     54 #include "ih264d_mvpred.h"
     55 #include "ih264d_mb_utils.h"
     56 
     57 #include "ih264d_defs.h"
     58 #include "ih264d_quant_scaling.h"
     59 
     60 #include "ih264d_inter_pred.h"
     61 
     62 #include "ih264d_sei.h"
     63 #include "ih264d.h"
     64 #include "ih264_error.h"
     65 #include "ih264_disp_mgr.h"
     66 #include "ih264_buf_mgr.h"
     67 
     68 #include "ih264d_thread_parse_decode.h"
     69 #include "ih264d_thread_compute_bs.h"
     70 #include "ih264d_dpb_manager.h"
     71 #include <assert.h>
     72 #include "ih264d_parse_islice.h"
     73 #define RET_LAST_SKIP  0x80000000
     74 
     75 /*!
     76  **************************************************************************
     77  * \if Function name : ih264d_form_pred_weight_matrix \endif
     78  *
     79  * \brief
     80  *    Forms pred weight matrix.
     81  *
     82  * \return
     83  *    None
     84  *
     85  **************************************************************************
     86  */
     87 
     88 void ih264d_form_pred_weight_matrix(dec_struct_t *ps_dec)
     89 {
     90     dec_slice_params_t *ps_cur_slice;
     91     UWORD8 uc_num_ref_idx_l0_active, uc_num_ref_idx_l1_active;
     92     UWORD8 i, j;
     93     UWORD32 *pu4_mat_iwt_ofst;
     94     UWORD16 i2_idx;
     95     UWORD32 *pui32_weight_offset_l0, *pui32_weight_offset_l1;
     96     UWORD32 u4_temp;
     97 
     98     ps_cur_slice = ps_dec->ps_cur_slice;
     99     uc_num_ref_idx_l0_active = ps_cur_slice->u1_num_ref_idx_lx_active[0];
    100     uc_num_ref_idx_l1_active = ps_cur_slice->u1_num_ref_idx_lx_active[1];
    101 
    102     pu4_mat_iwt_ofst = ps_dec->pu4_wts_ofsts_mat;
    103 
    104     if(ps_cur_slice->u1_slice_type == B_SLICE)
    105     {
    106         for(i = 0; i < uc_num_ref_idx_l0_active; i++)
    107         {
    108             pui32_weight_offset_l0 = ps_cur_slice->u4_wt_ofst_lx[0][i];
    109             for(j = 0; j < uc_num_ref_idx_l1_active; j++)
    110             {
    111                 pui32_weight_offset_l1 = ps_cur_slice->u4_wt_ofst_lx[1][j];
    112                 i2_idx = i * uc_num_ref_idx_l0_active + j;
    113                 i2_idx = X3(i2_idx);
    114                 /*        u4_temp = (pui32_weight_offset_l0[0] | (pui32_weight_offset_l1[0] << 16));
    115                  pu4_mat_iwt_ofst[0] = u4_temp;
    116                  u4_temp = (pui32_weight_offset_l0[1] | (pui32_weight_offset_l1[1] << 16));
    117                  pu4_mat_iwt_ofst[1] = u4_temp;
    118                  u4_temp = (pui32_weight_offset_l0[2] | (pui32_weight_offset_l1[2] << 16));
    119                  pu4_mat_iwt_ofst[2] = u4_temp;
    120                  pu4_mat_iwt_ofst += 3;*/
    121                 pu4_mat_iwt_ofst[0] = pui32_weight_offset_l0[0];
    122                 pu4_mat_iwt_ofst[1] = pui32_weight_offset_l1[0];
    123                 pu4_mat_iwt_ofst[2] = pui32_weight_offset_l0[1];
    124                 pu4_mat_iwt_ofst[3] = pui32_weight_offset_l1[1];
    125                 pu4_mat_iwt_ofst[4] = pui32_weight_offset_l0[2];
    126                 pu4_mat_iwt_ofst[5] = pui32_weight_offset_l1[2];
    127                 pu4_mat_iwt_ofst += 6;
    128             }
    129         }
    130     }
    131     else
    132     {
    133         for(i = 0; i < uc_num_ref_idx_l0_active; i++)
    134         {
    135             pui32_weight_offset_l0 = ps_cur_slice->u4_wt_ofst_lx[0][i];
    136             i2_idx = X3(i);
    137             u4_temp = (UWORD32)pui32_weight_offset_l0[0];
    138             pu4_mat_iwt_ofst[0] = u4_temp;
    139             u4_temp = (UWORD32)pui32_weight_offset_l0[1];
    140             pu4_mat_iwt_ofst[2] = u4_temp;
    141             u4_temp = (UWORD32)pui32_weight_offset_l0[2];
    142             pu4_mat_iwt_ofst[4] = u4_temp;
    143             pu4_mat_iwt_ofst += 6;
    144         }
    145     }
    146 }
    147 
    148 
    149 /*!
    150  **************************************************************************
    151  * \if Function name :  init_firstSliceParam \endif
    152  *
    153  * \brief
    154  *    Initialize the Parameter required for all the slices for a picture
    155  *
    156  * \return           : Nothing
    157  *
    158  **************************************************************************
    159  */
    160 
    161 WORD32 ih264d_start_of_pic(dec_struct_t *ps_dec,
    162                          WORD32 i4_poc,
    163                          pocstruct_t *ps_temp_poc,
    164                          UWORD16 u2_frame_num,
    165                          dec_pic_params_t *ps_pps)
    166 {
    167     pocstruct_t *ps_prev_poc = &ps_dec->s_cur_pic_poc;
    168     pocstruct_t *ps_cur_poc = ps_temp_poc;
    169 
    170     pic_buffer_t *pic_buf;
    171 
    172     ivd_video_decode_op_t * ps_dec_output =
    173                     (ivd_video_decode_op_t *)ps_dec->pv_dec_out;
    174     dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
    175     dec_seq_params_t *ps_seq = ps_pps->ps_sps;
    176     UWORD8 u1_bottom_field_flag = ps_cur_slice->u1_bottom_field_flag;
    177     UWORD8 u1_field_pic_flag = ps_cur_slice->u1_field_pic_flag;
    178     /* high profile related declarations */
    179     high_profile_tools_t s_high_profile;
    180     WORD32 ret;
    181 
    182     H264_MUTEX_LOCK(&ps_dec->process_disp_mutex);
    183 
    184     ps_prev_poc->i4_pic_order_cnt_lsb = ps_cur_poc->i4_pic_order_cnt_lsb;
    185     ps_prev_poc->i4_pic_order_cnt_msb = ps_cur_poc->i4_pic_order_cnt_msb;
    186     ps_prev_poc->i4_delta_pic_order_cnt_bottom =
    187                     ps_cur_poc->i4_delta_pic_order_cnt_bottom;
    188     ps_prev_poc->i4_delta_pic_order_cnt[0] =
    189                     ps_cur_poc->i4_delta_pic_order_cnt[0];
    190     ps_prev_poc->i4_delta_pic_order_cnt[1] =
    191                     ps_cur_poc->i4_delta_pic_order_cnt[1];
    192     ps_prev_poc->u1_bot_field = ps_dec->ps_cur_slice->u1_bottom_field_flag;
    193     ps_prev_poc->i4_prev_frame_num_ofst = ps_cur_poc->i4_prev_frame_num_ofst;
    194     ps_prev_poc->u2_frame_num = u2_frame_num;
    195     ps_dec->i1_prev_mb_qp_delta = 0;
    196     ps_dec->i1_next_ctxt_idx = 0;
    197 
    198 
    199     ps_dec->u4_nmb_deblk = 0;
    200     if(ps_dec->u4_num_cores == 1)
    201        ps_dec->u4_nmb_deblk = 1;
    202 
    203 
    204 
    205     if(ps_seq->u1_mb_aff_flag == 1)
    206     {
    207         ps_dec->u4_nmb_deblk = 0;
    208         if(ps_dec->u4_num_cores > 2)
    209             ps_dec->u4_num_cores = 2;
    210     }
    211 
    212         ps_dec->u4_use_intrapred_line_copy = 0;
    213 
    214 
    215 
    216     if (ps_seq->u1_mb_aff_flag == 0)
    217     {
    218         ps_dec->u4_use_intrapred_line_copy = 1;
    219     }
    220 
    221     ps_dec->u4_app_disable_deblk_frm = 0;
    222     /* If degrade is enabled, set the degrade flags appropriately */
    223     if(ps_dec->i4_degrade_type && ps_dec->i4_degrade_pics)
    224     {
    225         WORD32 degrade_pic;
    226         ps_dec->i4_degrade_pic_cnt++;
    227         degrade_pic = 0;
    228 
    229         /* If degrade is to be done in all frames, then do not check further */
    230         switch(ps_dec->i4_degrade_pics)
    231         {
    232             case 4:
    233             {
    234                 degrade_pic = 1;
    235                 break;
    236             }
    237             case 3:
    238             {
    239                 if(ps_cur_slice->u1_slice_type != I_SLICE)
    240                     degrade_pic = 1;
    241 
    242                 break;
    243             }
    244             case 2:
    245             {
    246 
    247                 /* If pic count hits non-degrade interval or it is an islice, then do not degrade */
    248                 if((ps_cur_slice->u1_slice_type != I_SLICE)
    249                                 && (ps_dec->i4_degrade_pic_cnt
    250                                                 != ps_dec->i4_nondegrade_interval))
    251                     degrade_pic = 1;
    252 
    253                 break;
    254             }
    255             case 1:
    256             {
    257                 /* Check if the current picture is non-ref */
    258                 if(0 == ps_cur_slice->u1_nal_ref_idc)
    259                 {
    260                     degrade_pic = 1;
    261                 }
    262                 break;
    263             }
    264 
    265         }
    266         if(degrade_pic)
    267         {
    268             if(ps_dec->i4_degrade_type & 0x2)
    269                 ps_dec->u4_app_disable_deblk_frm = 1;
    270 
    271             /* MC degrading is done only for non-ref pictures */
    272             if(0 == ps_cur_slice->u1_nal_ref_idc)
    273             {
    274                 if(ps_dec->i4_degrade_type & 0x4)
    275                     ps_dec->i4_mv_frac_mask = 0;
    276 
    277                 if(ps_dec->i4_degrade_type & 0x8)
    278                     ps_dec->i4_mv_frac_mask = 0;
    279             }
    280         }
    281         else
    282             ps_dec->i4_degrade_pic_cnt = 0;
    283     }
    284 
    285     {
    286         dec_err_status_t * ps_err = ps_dec->ps_dec_err_status;
    287         if(ps_dec->u1_sl_typ_5_9
    288                         && ((ps_cur_slice->u1_slice_type == I_SLICE)
    289                                         || (ps_cur_slice->u1_slice_type
    290                                                         == SI_SLICE)))
    291             ps_err->u1_cur_pic_type = PIC_TYPE_I;
    292         else
    293             ps_err->u1_cur_pic_type = PIC_TYPE_UNKNOWN;
    294 
    295         if(ps_err->u1_pic_aud_i == PIC_TYPE_I)
    296         {
    297             ps_err->u1_cur_pic_type = PIC_TYPE_I;
    298             ps_err->u1_pic_aud_i = PIC_TYPE_UNKNOWN;
    299         }
    300 
    301         if(ps_cur_slice->u1_nal_unit_type == IDR_SLICE_NAL)
    302         {
    303             if(ps_err->u1_err_flag)
    304                 ih264d_reset_ref_bufs(ps_dec->ps_dpb_mgr);
    305             ps_err->u1_err_flag = ACCEPT_ALL_PICS;
    306         }
    307     }
    308 
    309     if(ps_dec->u1_init_dec_flag && ps_dec->s_prev_seq_params.u1_eoseq_pending)
    310     {
    311         /* Reset the decoder picture buffers */
    312         WORD32 j;
    313         for(j = 0; j < MAX_DISP_BUFS_NEW; j++)
    314         {
    315 
    316             ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
    317                                   j,
    318                                   BUF_MGR_REF);
    319             ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
    320                                   ps_dec->au1_pic_buf_id_mv_buf_id_map[j],
    321                                   BUF_MGR_REF);
    322             ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
    323                                   j,
    324                                   BUF_MGR_IO);
    325         }
    326 
    327         /* reset the decoder structure parameters related to buffer handling */
    328         ps_dec->u1_second_field = 0;
    329         ps_dec->i4_cur_display_seq = 0;
    330 
    331         /********************************************************************/
    332         /* indicate in the decoder output i4_status that some frames are being */
    333         /* dropped, so that it resets timestamp and wait for a new sequence */
    334         /********************************************************************/
    335 
    336         ps_dec->s_prev_seq_params.u1_eoseq_pending = 0;
    337     }
    338     ret = ih264d_init_pic(ps_dec, u2_frame_num, i4_poc, ps_pps);
    339     if(ret != OK)
    340         return ret;
    341 
    342     ps_dec->pv_parse_tu_coeff_data = ps_dec->pv_pic_tu_coeff_data;
    343     ps_dec->pv_proc_tu_coeff_data  = ps_dec->pv_pic_tu_coeff_data;
    344     ps_dec->ps_nmb_info = ps_dec->ps_frm_mb_info;
    345     if(ps_dec->u1_separate_parse)
    346     {
    347         UWORD16 pic_wd;
    348         UWORD16 pic_ht;
    349         UWORD32 num_mbs;
    350 
    351         pic_wd = ps_dec->u2_pic_wd;
    352         pic_ht = ps_dec->u2_pic_ht;
    353         num_mbs = (pic_wd * pic_ht) >> 8;
    354 
    355         if(ps_dec->pu1_dec_mb_map)
    356         {
    357             memset((void *)ps_dec->pu1_dec_mb_map, 0, num_mbs);
    358         }
    359 
    360         if(ps_dec->pu1_recon_mb_map)
    361         {
    362 
    363             memset((void *)ps_dec->pu1_recon_mb_map, 0, num_mbs);
    364         }
    365 
    366         if(ps_dec->pu2_slice_num_map)
    367         {
    368             memset((void *)ps_dec->pu2_slice_num_map, 0,
    369                    (num_mbs * sizeof(UWORD16)));
    370         }
    371 
    372     }
    373 
    374     ps_dec->ps_parse_cur_slice = &(ps_dec->ps_dec_slice_buf[0]);
    375     ps_dec->ps_decode_cur_slice = &(ps_dec->ps_dec_slice_buf[0]);
    376     ps_dec->ps_computebs_cur_slice = &(ps_dec->ps_dec_slice_buf[0]);
    377     ps_dec->u2_cur_slice_num = 0;
    378 
    379     /* Initialize all the HP toolsets to zero */
    380     ps_dec->s_high_profile.u1_scaling_present = 0;
    381     ps_dec->s_high_profile.u1_transform8x8_present = 0;
    382 
    383     /* Get Next Free Picture */
    384     if(1 == ps_dec->u4_share_disp_buf)
    385     {
    386         UWORD32 i;
    387         /* Free any buffer that is in the queue to be freed */
    388         for(i = 0; i < MAX_DISP_BUFS_NEW; i++)
    389         {
    390             if(0 == ps_dec->u4_disp_buf_to_be_freed[i])
    391                 continue;
    392             ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr, i,
    393             BUF_MGR_IO);
    394             ps_dec->u4_disp_buf_to_be_freed[i] = 0;
    395             ps_dec->u4_disp_buf_mapping[i] = 0;
    396 
    397         }
    398     }
    399     if(!(u1_field_pic_flag && 0 != ps_dec->u1_top_bottom_decoded)) //ps_dec->u1_second_field))
    400     {
    401         pic_buffer_t *ps_cur_pic;
    402         WORD32 cur_pic_buf_id, cur_mv_buf_id;
    403         col_mv_buf_t *ps_col_mv;
    404         while(1)
    405         {
    406             ps_cur_pic = (pic_buffer_t *)ih264_buf_mgr_get_next_free(
    407                             (buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
    408                             &cur_pic_buf_id);
    409             if(ps_cur_pic == NULL)
    410             {
    411                 ps_dec->i4_error_code = ERROR_UNAVAIL_PICBUF_T;
    412                 return ERROR_UNAVAIL_PICBUF_T;
    413             }
    414             if(0 == ps_dec->u4_disp_buf_mapping[cur_pic_buf_id])
    415             {
    416                 break;
    417             }
    418 
    419         }
    420         ps_col_mv = (col_mv_buf_t *)ih264_buf_mgr_get_next_free((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
    421                                                                &cur_mv_buf_id);
    422         if(ps_col_mv == NULL)
    423         {
    424             ps_dec->i4_error_code = ERROR_UNAVAIL_MVBUF_T;
    425             return ERROR_UNAVAIL_MVBUF_T;
    426         }
    427 
    428         ps_dec->ps_cur_pic = ps_cur_pic;
    429         ps_dec->u1_pic_buf_id = cur_pic_buf_id;
    430         ps_cur_pic->u4_ts = ps_dec->u4_ts;
    431 
    432 
    433         ps_cur_pic->u1_mv_buf_id = cur_mv_buf_id;
    434         ps_dec->au1_pic_buf_id_mv_buf_id_map[cur_pic_buf_id] = cur_mv_buf_id;
    435 
    436         ps_cur_pic->pu1_col_zero_flag = (UWORD8 *)ps_col_mv->pv_col_zero_flag;
    437         ps_cur_pic->ps_mv = (mv_pred_t *)ps_col_mv->pv_mv;
    438         ps_dec->au1_pic_buf_ref_flag[cur_pic_buf_id] = 0;
    439         if(ps_dec->u1_first_slice_in_stream)
    440         {
    441             /*make first entry of list0 point to cur pic,so that if first Islice is in error, ref pic struct will have valid entries*/
    442             ps_dec->ps_ref_pic_buf_lx[0] = ps_dec->ps_dpb_mgr->ps_init_dpb[0];
    443             *(ps_dec->ps_dpb_mgr->ps_init_dpb[0][0]) = *ps_cur_pic;
    444             /* Initialize for field reference as well */
    445             *(ps_dec->ps_dpb_mgr->ps_init_dpb[0][MAX_REF_BUFS]) = *ps_cur_pic;
    446         }
    447 
    448         if(!ps_dec->ps_cur_pic)
    449         {
    450             WORD32 j;
    451             H264_DEC_DEBUG_PRINT("------- Display Buffers Reset --------\n");
    452             for(j = 0; j < MAX_DISP_BUFS_NEW; j++)
    453             {
    454 
    455                 ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
    456                                       j,
    457                                       BUF_MGR_REF);
    458                 ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
    459                                       ps_dec->au1_pic_buf_id_mv_buf_id_map[j],
    460                                       BUF_MGR_REF);
    461                 ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
    462                                       j,
    463                                       BUF_MGR_IO);
    464             }
    465 
    466             ps_dec->i4_cur_display_seq = 0;
    467             ps_dec->i4_prev_max_display_seq = 0;
    468             ps_dec->i4_max_poc = 0;
    469 
    470             ps_cur_pic = (pic_buffer_t *)ih264_buf_mgr_get_next_free(
    471                             (buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
    472                             &cur_pic_buf_id);
    473             if(ps_cur_pic == NULL)
    474             {
    475                 ps_dec->i4_error_code = ERROR_UNAVAIL_PICBUF_T;
    476                 return ERROR_UNAVAIL_PICBUF_T;
    477             }
    478 
    479             ps_col_mv = (col_mv_buf_t *)ih264_buf_mgr_get_next_free((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
    480                                                                    &cur_mv_buf_id);
    481             if(ps_col_mv == NULL)
    482             {
    483                 ps_dec->i4_error_code = ERROR_UNAVAIL_MVBUF_T;
    484                 return ERROR_UNAVAIL_MVBUF_T;
    485             }
    486 
    487             ps_dec->ps_cur_pic = ps_cur_pic;
    488             ps_dec->u1_pic_buf_id = cur_pic_buf_id;
    489             ps_cur_pic->u4_ts = ps_dec->u4_ts;
    490             ps_dec->apv_buf_id_pic_buf_map[cur_pic_buf_id] = (void *)ps_cur_pic;
    491 
    492             ps_cur_pic->u1_mv_buf_id = cur_mv_buf_id;
    493             ps_dec->au1_pic_buf_id_mv_buf_id_map[cur_pic_buf_id] = cur_mv_buf_id;
    494 
    495             ps_cur_pic->pu1_col_zero_flag = (UWORD8 *)ps_col_mv->pv_col_zero_flag;
    496             ps_cur_pic->ps_mv = (mv_pred_t *)ps_col_mv->pv_mv;
    497             ps_dec->au1_pic_buf_ref_flag[cur_pic_buf_id] = 0;
    498 
    499         }
    500 
    501         ps_dec->ps_cur_pic->u1_picturetype = u1_field_pic_flag;
    502         ps_dec->ps_cur_pic->u4_pack_slc_typ = SKIP_NONE;
    503         H264_DEC_DEBUG_PRINT("got a buffer\n");
    504     }
    505     else
    506     {
    507         H264_DEC_DEBUG_PRINT("did not get a buffer\n");
    508     }
    509 
    510     ps_dec->u4_pic_buf_got = 1;
    511 
    512     ps_dec->ps_cur_pic->i4_poc = i4_poc;
    513     ps_dec->ps_cur_pic->i4_frame_num = u2_frame_num;
    514     ps_dec->ps_cur_pic->i4_pic_num = u2_frame_num;
    515     ps_dec->ps_cur_pic->i4_top_field_order_cnt = ps_pps->i4_top_field_order_cnt;
    516     ps_dec->ps_cur_pic->i4_bottom_field_order_cnt =
    517                     ps_pps->i4_bottom_field_order_cnt;
    518     ps_dec->ps_cur_pic->i4_avg_poc = ps_pps->i4_avg_poc;
    519     ps_dec->ps_cur_pic->u4_time_stamp = ps_dec->u4_pts;
    520 
    521     ps_dec->s_cur_pic = *(ps_dec->ps_cur_pic);
    522     if(u1_field_pic_flag && u1_bottom_field_flag)
    523     {
    524         WORD32 i4_temp_poc;
    525         WORD32 i4_top_field_order_poc, i4_bot_field_order_poc;
    526         /* Point to odd lines, since it's bottom field */
    527         ps_dec->s_cur_pic.pu1_buf1 += ps_dec->s_cur_pic.u2_frm_wd_y;
    528         ps_dec->s_cur_pic.pu1_buf2 += ps_dec->s_cur_pic.u2_frm_wd_uv;
    529         ps_dec->s_cur_pic.pu1_buf3 += ps_dec->s_cur_pic.u2_frm_wd_uv;
    530         ps_dec->s_cur_pic.ps_mv +=
    531                         ((ps_dec->u2_pic_ht * ps_dec->u2_pic_wd) >> 5);
    532         ps_dec->s_cur_pic.pu1_col_zero_flag += ((ps_dec->u2_pic_ht
    533                         * ps_dec->u2_pic_wd) >> 5);
    534         ps_dec->ps_cur_pic->u1_picturetype |= BOT_FLD;
    535         i4_top_field_order_poc = ps_dec->ps_cur_pic->i4_top_field_order_cnt;
    536         i4_bot_field_order_poc = ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
    537         i4_temp_poc = MIN(i4_top_field_order_poc,
    538                                  i4_bot_field_order_poc);
    539         ps_dec->ps_cur_pic->i4_avg_poc = i4_temp_poc;
    540     }
    541 
    542     ps_cur_slice->u1_mbaff_frame_flag = ps_seq->u1_mb_aff_flag
    543                     && (!u1_field_pic_flag);
    544 
    545     ps_dec->ps_cur_pic->u1_picturetype |= (ps_cur_slice->u1_mbaff_frame_flag
    546                     << 2);
    547 
    548     ps_dec->ps_cur_mb_row = ps_dec->ps_nbr_mb_row; //[0];
    549     //Increment by 2 ,so that left mb (mbaff decrements by 2)  will always be valid
    550     ps_dec->ps_cur_mb_row += 2;
    551     ps_dec->ps_top_mb_row = ps_dec->ps_nbr_mb_row;
    552     ps_dec->ps_top_mb_row += ((ps_dec->u2_frm_wd_in_mbs + 2) << (1 - ps_dec->ps_cur_sps->u1_frame_mbs_only_flag));
    553     //Increment by 2 ,so that left mb (mbaff decrements by 2)  will always be valid
    554     ps_dec->ps_top_mb_row += 2;
    555 
    556     /* CHANGED CODE */
    557     ps_dec->ps_mv_cur = ps_dec->s_cur_pic.ps_mv;
    558     ps_dec->ps_mv_top = ps_dec->ps_mv_top_p[0];
    559     /* CHANGED CODE */
    560     ps_dec->u1_mv_top_p = 0;
    561     ps_dec->u1_mb_idx = 0;
    562     /* CHANGED CODE */
    563     ps_dec->ps_mv_left = ps_dec->s_cur_pic.ps_mv;
    564     ps_dec->u2_total_mbs_coded = 0;
    565     ps_dec->i4_submb_ofst = -(SUB_BLK_SIZE);
    566     ps_dec->u4_pred_info_idx = 0;
    567     ps_dec->u4_pred_info_pkd_idx = 0;
    568     ps_dec->u4_dma_buf_idx = 0;
    569     ps_dec->ps_mv = ps_dec->s_cur_pic.ps_mv;
    570     ps_dec->ps_mv_bank_cur = ps_dec->s_cur_pic.ps_mv;
    571     ps_dec->pu1_col_zero_flag = ps_dec->s_cur_pic.pu1_col_zero_flag;
    572     ps_dec->ps_part = ps_dec->ps_parse_part_params;
    573     ps_dec->i2_prev_slice_mbx = -1;
    574     ps_dec->i2_prev_slice_mby = 0;
    575     ps_dec->u2_mv_2mb[0] = 0;
    576     ps_dec->u2_mv_2mb[1] = 0;
    577     ps_dec->u1_last_pic_not_decoded = 0;
    578 
    579     ps_dec->u2_cur_slice_num_dec_thread = 0;
    580     ps_dec->u2_cur_slice_num_bs = 0;
    581     ps_dec->u4_intra_pred_line_ofst = 0;
    582     ps_dec->pu1_cur_y_intra_pred_line = ps_dec->pu1_y_intra_pred_line;
    583     ps_dec->pu1_cur_u_intra_pred_line = ps_dec->pu1_u_intra_pred_line;
    584     ps_dec->pu1_cur_v_intra_pred_line = ps_dec->pu1_v_intra_pred_line;
    585 
    586     ps_dec->pu1_cur_y_intra_pred_line_base = ps_dec->pu1_y_intra_pred_line;
    587     ps_dec->pu1_cur_u_intra_pred_line_base = ps_dec->pu1_u_intra_pred_line;
    588     ps_dec->pu1_cur_v_intra_pred_line_base = ps_dec->pu1_v_intra_pred_line;
    589 
    590 
    591 
    592 
    593 
    594     ps_dec->pu1_prev_y_intra_pred_line = ps_dec->pu1_y_intra_pred_line
    595                     + (ps_dec->u2_frm_wd_in_mbs * MB_SIZE);
    596 
    597     ps_dec->pu1_prev_u_intra_pred_line = ps_dec->pu1_u_intra_pred_line
    598                     + ps_dec->u2_frm_wd_in_mbs * BLK8x8SIZE * YUV420SP_FACTOR;
    599     ps_dec->pu1_prev_v_intra_pred_line = ps_dec->pu1_v_intra_pred_line
    600                     + ps_dec->u2_frm_wd_in_mbs * BLK8x8SIZE;
    601 
    602     ps_dec->ps_deblk_mbn = ps_dec->ps_deblk_pic;
    603     /* Initialize The Function Pointer Depending Upon the Entropy and MbAff Flag */
    604     {
    605         if(ps_cur_slice->u1_mbaff_frame_flag)
    606         {
    607             ps_dec->pf_compute_bs = ih264d_compute_bs_mbaff;
    608             ps_dec->pf_mvpred = ih264d_mvpred_mbaff;
    609         }
    610         else
    611         {
    612             ps_dec->pf_compute_bs = ih264d_compute_bs_non_mbaff;
    613             ps_dec->u1_cur_mb_fld_dec_flag = ps_cur_slice->u1_field_pic_flag;
    614         }
    615     }
    616     /* Set up the Parameter for DMA transfer */
    617     {
    618         UWORD8 u1_field_pic_flag = ps_dec->ps_cur_slice->u1_field_pic_flag;
    619 
    620         UWORD8 u1_mbaff = ps_cur_slice->u1_mbaff_frame_flag;
    621 
    622         UWORD8 uc_lastmbs = (((ps_dec->u2_pic_wd) >> 4)
    623                         % (ps_dec->u1_recon_mb_grp >> u1_mbaff));
    624         UWORD16 ui16_lastmbs_widthY =
    625                         (uc_lastmbs ? (uc_lastmbs << 4) : ((ps_dec->u1_recon_mb_grp
    626                                         >> u1_mbaff) << 4));
    627         UWORD16 ui16_lastmbs_widthUV =
    628                         uc_lastmbs ? (uc_lastmbs << 3) : ((ps_dec->u1_recon_mb_grp
    629                                         >> u1_mbaff) << 3);
    630 
    631         ps_dec->s_tran_addrecon.pu1_dest_y = ps_dec->s_cur_pic.pu1_buf1;
    632         ps_dec->s_tran_addrecon.pu1_dest_u = ps_dec->s_cur_pic.pu1_buf2;
    633         ps_dec->s_tran_addrecon.pu1_dest_v = ps_dec->s_cur_pic.pu1_buf3;
    634 
    635         ps_dec->s_tran_addrecon.u2_frm_wd_y = ps_dec->u2_frm_wd_y
    636                         << u1_field_pic_flag;
    637         ps_dec->s_tran_addrecon.u2_frm_wd_uv = ps_dec->u2_frm_wd_uv
    638                         << u1_field_pic_flag;
    639 
    640         if(u1_field_pic_flag)
    641         {
    642             ui16_lastmbs_widthY += ps_dec->u2_frm_wd_y;
    643             ui16_lastmbs_widthUV += ps_dec->u2_frm_wd_uv;
    644         }
    645 
    646         /* Normal Increment of Pointer */
    647         ps_dec->s_tran_addrecon.u4_inc_y[0] = ((ps_dec->u1_recon_mb_grp << 4)
    648                         >> u1_mbaff);
    649         ps_dec->s_tran_addrecon.u4_inc_uv[0] = ((ps_dec->u1_recon_mb_grp << 4)
    650                         >> u1_mbaff);
    651 
    652         /* End of Row Increment */
    653         ps_dec->s_tran_addrecon.u4_inc_y[1] = (ui16_lastmbs_widthY
    654                         + (PAD_LEN_Y_H << 1)
    655                         + ps_dec->s_tran_addrecon.u2_frm_wd_y
    656                                         * ((15 << u1_mbaff) + u1_mbaff));
    657         ps_dec->s_tran_addrecon.u4_inc_uv[1] = (ui16_lastmbs_widthUV
    658                         + (PAD_LEN_UV_H << 2)
    659                         + ps_dec->s_tran_addrecon.u2_frm_wd_uv
    660                                         * ((15 << u1_mbaff) + u1_mbaff));
    661 
    662         /* Assign picture numbers to each frame/field  */
    663         /* only once per picture.                      */
    664         ih264d_assign_pic_num(ps_dec);
    665         ps_dec->s_tran_addrecon.u2_mv_top_left_inc = (ps_dec->u1_recon_mb_grp
    666                         << 2) - 1 - (u1_mbaff << 2);
    667         ps_dec->s_tran_addrecon.u2_mv_left_inc = ((ps_dec->u1_recon_mb_grp
    668                         >> u1_mbaff) - 1) << (4 + u1_mbaff);
    669     }
    670     /**********************************************************************/
    671     /* High profile related initialization at pictrue level               */
    672     /**********************************************************************/
    673     if(ps_seq->u1_profile_idc == HIGH_PROFILE_IDC)
    674     {
    675         if((ps_seq->i4_seq_scaling_matrix_present_flag)
    676                         || (ps_pps->i4_pic_scaling_matrix_present_flag))
    677         {
    678             ih264d_form_scaling_matrix_picture(ps_seq, ps_pps, ps_dec);
    679             ps_dec->s_high_profile.u1_scaling_present = 1;
    680         }
    681         else
    682         {
    683             ih264d_form_default_scaling_matrix(ps_dec);
    684         }
    685 
    686         if(ps_pps->i4_transform_8x8_mode_flag)
    687         {
    688             ps_dec->s_high_profile.u1_transform8x8_present = 1;
    689         }
    690     }
    691     else
    692     {
    693         ih264d_form_default_scaling_matrix(ps_dec);
    694     }
    695 
    696     /* required while reading the transform_size_8x8 u4_flag */
    697     ps_dec->s_high_profile.u1_direct_8x8_inference_flag =
    698                     ps_seq->u1_direct_8x8_inference_flag;
    699     ps_dec->s_high_profile.s_cavlc_ctxt = ps_dec->s_cavlc_ctxt;
    700 
    701     ps_dec->i1_recon_in_thread3_flag = 1;
    702     ps_dec->ps_frame_buf_ip_recon = &ps_dec->s_tran_addrecon;
    703     if(ps_dec->u1_separate_parse)
    704     {
    705         memcpy(&ps_dec->s_tran_addrecon_parse, &ps_dec->s_tran_addrecon,
    706                sizeof(tfr_ctxt_t));
    707         if(ps_dec->u4_num_cores >= 3 && ps_dec->i1_recon_in_thread3_flag)
    708         {
    709             memcpy(&ps_dec->s_tran_iprecon, &ps_dec->s_tran_addrecon,
    710                    sizeof(tfr_ctxt_t));
    711             ps_dec->ps_frame_buf_ip_recon = &ps_dec->s_tran_iprecon;
    712         }
    713     }
    714 
    715 
    716     ih264d_init_deblk_tfr_ctxt(ps_dec,&(ps_dec->s_pad_mgr), &(ps_dec->s_tran_addrecon),
    717                                ps_dec->u2_frm_wd_in_mbs, 0);
    718 
    719     ps_dec->ps_cur_deblk_mb = ps_dec->ps_deblk_pic;
    720     ps_dec->u4_cur_deblk_mb_num = 0;
    721 
    722     ps_dec->u4_deblk_mb_x = 0;
    723     ps_dec->u4_deblk_mb_y = 0;
    724     ps_dec->pu4_wt_ofsts = ps_dec->pu4_wts_ofsts_mat;
    725 
    726     H264_MUTEX_UNLOCK(&ps_dec->process_disp_mutex);
    727     return OK;
    728 }
    729 
    730 /*!
    731  **************************************************************************
    732  * \if Function name :  ih264d_deblock_display \endif
    733  *
    734  * \brief            :  The function callls the deblocking routine and manages
    735  :  the Recon buffers and displays .
    736  * \return           :  Nothing
    737  *
    738  **************************************************************************
    739  */
    740 WORD32 ih264d_end_of_pic_dispbuf_mgr(dec_struct_t * ps_dec)
    741 {
    742     dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
    743     UWORD8 u1_num_of_users = 0;
    744     WORD32 ret;
    745 
    746     H264_MUTEX_LOCK(&ps_dec->process_disp_mutex);
    747     if(1)
    748     {
    749 
    750         {
    751             ih264d_delete_nonref_nondisplay_pics(ps_dec->ps_dpb_mgr);
    752             if(ps_cur_slice->u1_mmco_equalto5
    753                             || (ps_cur_slice->u1_nal_unit_type == IDR_SLICE_NAL))
    754             {
    755                 ps_dec->ps_cur_pic->i4_poc = 0;
    756                 if(ps_dec->u2_total_mbs_coded
    757                                 == (ps_dec->ps_cur_sps->u2_max_mb_addr + 1))
    758                     ih264d_reset_ref_bufs(ps_dec->ps_dpb_mgr);
    759                 ih264d_release_display_bufs(ps_dec);
    760             }
    761             if(IVD_DECODE_FRAME_OUT != ps_dec->e_frm_out_mode)
    762             {
    763                 ret = ih264d_assign_display_seq(ps_dec);
    764                 if(ret != OK)
    765                     return ret;
    766             }
    767         }
    768 
    769         if(ps_cur_slice->u1_nal_ref_idc)
    770         {
    771             /* Mark pic buf as needed for reference */
    772             ih264_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
    773                                      ps_dec->u1_pic_buf_id,
    774                                      BUF_MGR_REF);
    775             /* Mark mv buf as needed for reference */
    776             ih264_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
    777                                      ps_dec->au1_pic_buf_id_mv_buf_id_map[ps_dec->u1_pic_buf_id],
    778                                      BUF_MGR_REF);
    779             ps_dec->au1_pic_buf_ref_flag[ps_dec->u1_pic_buf_id] = 1;
    780         }
    781 
    782         /* 420 consumer */
    783         /* Increment the number of users by 1 for display based upon */
    784         /*the SEEK KEY FRAME control sent to decoder                 */
    785         if(((0 == ps_dec->u1_last_pic_not_decoded)
    786                         && (0
    787                                         == (ps_dec->ps_cur_pic->u4_pack_slc_typ
    788                                                         & ps_dec->u4_skip_frm_mask)))
    789                         || (ps_cur_slice->u1_nal_unit_type == IDR_SLICE_NAL))
    790         {
    791             /* Mark pic buf as needed for display */
    792             ih264_buf_mgr_set_status((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
    793                                      ps_dec->u1_pic_buf_id,
    794                                      BUF_MGR_IO);
    795 
    796         }
    797 
    798         if(!ps_cur_slice->u1_field_pic_flag
    799                         || ((TOP_FIELD_ONLY | BOT_FIELD_ONLY)
    800                                         != ps_dec->u1_top_bottom_decoded))
    801         {
    802             pic_buffer_t *ps_cur_pic = ps_dec->ps_cur_pic;
    803             ps_cur_pic->u2_disp_width = ps_dec->u2_disp_width;
    804             ps_cur_pic->u2_disp_height = ps_dec->u2_disp_height >> 1;
    805 
    806             ps_cur_pic->u2_crop_offset_y = ps_dec->u2_crop_offset_y;
    807             ps_cur_pic->u2_crop_offset_uv = ps_dec->u2_crop_offset_uv;
    808             ps_cur_pic->u1_pic_type = 0;
    809 
    810             ret = ih264d_insert_pic_in_display_list(
    811                             ps_dec->ps_dpb_mgr,
    812                             ps_dec->u1_pic_buf_id,
    813                             ps_dec->i4_prev_max_display_seq
    814                                             + ps_dec->ps_cur_pic->i4_poc,
    815                             ps_dec->ps_cur_pic->i4_frame_num);
    816             if(ret != OK)
    817                 return ret;
    818 
    819             {
    820                 ivd_video_decode_op_t * ps_dec_output =
    821                                 (ivd_video_decode_op_t *)ps_dec->pv_dec_out;
    822 
    823                 ps_dec_output->u4_frame_decoded_flag = 1;
    824             }
    825             if(ps_dec->au1_pic_buf_ref_flag[ps_dec->u1_pic_buf_id] == 0)
    826             {
    827                 ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
    828                                       ps_dec->au1_pic_buf_id_mv_buf_id_map[ps_dec->u1_pic_buf_id],
    829                                       BUF_MGR_REF);
    830                 ps_dec->au1_pic_buf_ref_flag[ps_dec->u1_pic_buf_id] = 0;
    831 
    832             }
    833         }
    834         else
    835         {
    836             H264_DEC_DEBUG_PRINT("pic not inserted display %d %d\n",
    837                                  ps_cur_slice->u1_field_pic_flag,
    838                                  ps_dec->u1_second_field);
    839         }
    840 
    841         if(!ps_cur_slice->u1_field_pic_flag
    842                         || ((TOP_FIELD_ONLY | BOT_FIELD_ONLY)
    843                                         == ps_dec->u1_top_bottom_decoded))
    844         {
    845             if(IVD_DECODE_FRAME_OUT == ps_dec->e_frm_out_mode)
    846             {
    847                 ret = ih264d_assign_display_seq(ps_dec);
    848                 if(ret != OK)
    849                     return ret;
    850             }
    851         }
    852     }
    853 
    854     H264_MUTEX_UNLOCK(&ps_dec->process_disp_mutex);
    855 
    856     return OK;
    857 }
    858 
    859 void ih264d_err_pic_dispbuf_mgr(dec_struct_t *ps_dec)
    860 {
    861     dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
    862     ivd_video_decode_op_t * ps_dec_output =
    863                     (ivd_video_decode_op_t *)ps_dec->pv_dec_out;
    864 
    865     ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
    866                           ps_dec->u1_pic_buf_id,
    867                           BUF_MGR_REF);
    868     ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_mv_buf_mgr,
    869                           ps_dec->au1_pic_buf_id_mv_buf_id_map[ps_dec->u1_pic_buf_id],
    870                           BUF_MGR_REF);
    871     ih264_buf_mgr_release((buf_mgr_t *)ps_dec->pv_pic_buf_mgr,
    872                           ps_dec->u1_pic_buf_id,
    873                           BUF_MGR_IO);
    874 }
    875 
    876 void ih264d_deblock_picture(void *ptr)
    877 {
    878     dec_struct_t *ps_dec = (dec_struct_t *)ptr;
    879 
    880     {
    881         /*Deblock picture only if all the mb's in the frame have been decoded*/
    882         if(ps_dec->u1_pic_decode_done == 1)
    883         {
    884             if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag
    885                             || ps_dec->ps_cur_slice->u1_field_pic_flag)
    886             {
    887                 ps_dec->p_DeblockPicture[ps_dec->ps_cur_slice->u1_mbaff_frame_flag](
    888                                 ps_dec);
    889             }
    890             else
    891 
    892             {
    893 
    894                 ih264d_deblock_picture_progressive(ps_dec);
    895             }
    896 
    897         }
    898     }
    899 
    900 }
    901 
    902 /*!
    903  **************************************************************************
    904  * \if Function name :  ih264d_deblock_display \endif
    905  *
    906  * \brief            :  The function callls the deblocking routine and manages
    907  :  the Recon buffers and displays .
    908  * \return           :  Nothing
    909  *
    910  **************************************************************************
    911  */
    912 WORD32 ih264d_deblock_display(dec_struct_t *ps_dec)
    913 {
    914     WORD32 ret;
    915     /* Call deblocking */
    916     ih264d_deblock_picture(ps_dec);
    917 
    918     ret = ih264d_end_of_pic_dispbuf_mgr(ps_dec);
    919     if(ret != OK)
    920         return ret;
    921 
    922     return OK;
    923 }
    924 
    925 /*
    926  *!
    927  **************************************************************************
    928  * \if Function name : EndofPoc \endif
    929  *
    930  * \brief
    931  *    EndofPoc Processing
    932  *
    933  * \return
    934  *    0 on Success and Error code otherwise
    935  **************************************************************************
    936  */
    937 
    938 WORD32 ih264d_end_of_pic(dec_struct_t *ps_dec,
    939                        UWORD8 u1_is_idr_slice,
    940                        UWORD16 u2_frame_num)
    941 {
    942     dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
    943     WORD32 ret;
    944 
    945     ps_dec->u2_mbx = 0xffff;
    946     ps_dec->u2_mby = 0;
    947     {
    948         dec_err_status_t * ps_err = ps_dec->ps_dec_err_status;
    949         if(ps_err->u1_err_flag & REJECT_CUR_PIC)
    950         {
    951             ih264d_err_pic_dispbuf_mgr(ps_dec);
    952             return ERROR_NEW_FRAME_EXPECTED;
    953         }
    954     }
    955 
    956     H264_MUTEX_LOCK(&ps_dec->process_disp_mutex);
    957     ret = ih264d_end_of_pic_processing(ps_dec);
    958     if(ret != OK)
    959         return ret;
    960     ps_dec->u2_total_mbs_coded = 0;
    961     /*--------------------------------------------------------------------*/
    962     /* ih264d_decode_pic_order_cnt - calculate the Pic Order Cnt                    */
    963     /* Needed to detect end of picture                                    */
    964     /*--------------------------------------------------------------------*/
    965     {
    966         pocstruct_t *ps_prev_poc = &ps_dec->s_prev_pic_poc;
    967         pocstruct_t *ps_cur_poc = &ps_dec->s_cur_pic_poc;
    968         if((0 == u1_is_idr_slice) && ps_cur_slice->u1_nal_ref_idc)
    969             ps_dec->u2_prev_ref_frame_num = ps_cur_slice->u2_frame_num;
    970 
    971         if(u1_is_idr_slice || ps_cur_slice->u1_mmco_equalto5)
    972             ps_dec->u2_prev_ref_frame_num = 0;
    973 
    974         if(ps_dec->ps_cur_sps->u1_gaps_in_frame_num_value_allowed_flag)
    975         {
    976             ret = ih264d_decode_gaps_in_frame_num(ps_dec, u2_frame_num);
    977             if(ret != OK)
    978                 return ret;
    979         }
    980 
    981         ps_prev_poc->i4_prev_frame_num_ofst = ps_cur_poc->i4_prev_frame_num_ofst;
    982         ps_prev_poc->u2_frame_num = ps_cur_poc->u2_frame_num;
    983         ps_prev_poc->u1_mmco_equalto5 = ps_cur_slice->u1_mmco_equalto5;
    984         if(ps_cur_slice->u1_nal_ref_idc)
    985         {
    986             ps_prev_poc->i4_pic_order_cnt_lsb = ps_cur_poc->i4_pic_order_cnt_lsb;
    987             ps_prev_poc->i4_pic_order_cnt_msb = ps_cur_poc->i4_pic_order_cnt_msb;
    988             ps_prev_poc->i4_delta_pic_order_cnt_bottom =
    989                             ps_cur_poc->i4_delta_pic_order_cnt_bottom;
    990             ps_prev_poc->i4_delta_pic_order_cnt[0] =
    991                             ps_cur_poc->i4_delta_pic_order_cnt[0];
    992             ps_prev_poc->i4_delta_pic_order_cnt[1] =
    993                             ps_cur_poc->i4_delta_pic_order_cnt[1];
    994             ps_prev_poc->u1_bot_field = ps_cur_poc->u1_bot_field;
    995         }
    996     }
    997 
    998     H264_MUTEX_UNLOCK(&ps_dec->process_disp_mutex);
    999 
   1000     return OK;
   1001 }
   1002 
   1003 /*!
   1004  **************************************************************************
   1005  * \if Function name : DecodeSlice \endif
   1006  *
   1007  * \brief
   1008  *    Parses a slice
   1009  *
   1010  * \return
   1011  *    0 on Success and Error code otherwise
   1012  **************************************************************************
   1013  */
   1014 
   1015 WORD32 ih264d_parse_decode_slice(UWORD8 u1_is_idr_slice,
   1016                                  UWORD8 u1_nal_ref_idc,
   1017                                  dec_struct_t *ps_dec /* Decoder parameters */
   1018                                  )
   1019 {
   1020     dec_bit_stream_t * ps_bitstrm = ps_dec->ps_bitstrm;
   1021     dec_pic_params_t *ps_pps;
   1022     dec_seq_params_t *ps_seq;
   1023     dec_slice_params_t *ps_cur_slice = ps_dec->ps_cur_slice;
   1024     pocstruct_t s_tmp_poc;
   1025     WORD32 i_delta_poc[2];
   1026     WORD32 i4_poc = 0;
   1027     UWORD16 u2_first_mb_in_slice, u2_frame_num;
   1028     UWORD8 u1_field_pic_flag, u1_redundant_pic_cnt = 0, u1_slice_type;
   1029     UWORD32 u4_idr_pic_id = 0;
   1030     UWORD8 u1_bottom_field_flag, u1_pic_order_cnt_type;
   1031 
   1032     UWORD8 u1_nal_unit_type;
   1033     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
   1034     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
   1035     WORD8 i1_is_end_of_poc;
   1036 
   1037     WORD32 ret, end_of_frame;
   1038     WORD32 prev_slice_err, num_mb_skipped;
   1039     UWORD8 u1_mbaff;
   1040     pocstruct_t *ps_cur_poc;
   1041 
   1042     UWORD32 u4_temp;
   1043     WORD32 i_temp;
   1044     UWORD32 u4_call_end_of_pic = 0;
   1045 
   1046     /* read FirstMbInSlice  and slice type*/
   1047     ps_dec->ps_dpb_cmds->u1_dpb_commands_read_slc = 0;
   1048     u2_first_mb_in_slice = ih264d_uev(pu4_bitstrm_ofst,
   1049                                      pu4_bitstrm_buf);
   1050     if(u2_first_mb_in_slice
   1051                     > (ps_dec->u2_frm_ht_in_mbs * ps_dec->u2_frm_wd_in_mbs))
   1052     {
   1053 
   1054         return ERROR_CORRUPTED_SLICE;
   1055     }
   1056 
   1057     /*we currently don not support ASO*/
   1058     if(((u2_first_mb_in_slice << ps_cur_slice->u1_mbaff_frame_flag)
   1059                     <= ps_dec->u2_cur_mb_addr) && (ps_dec->u2_cur_mb_addr != 0)
   1060                     && (ps_dec->u4_first_slice_in_pic != 0))
   1061     {
   1062         return ERROR_CORRUPTED_SLICE;
   1063     }
   1064 
   1065     COPYTHECONTEXT("SH: first_mb_in_slice",u2_first_mb_in_slice);
   1066 
   1067     u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
   1068 
   1069     if(u4_temp > 9)
   1070         return ERROR_INV_SLC_TYPE_T;
   1071 
   1072     u1_slice_type = u4_temp;
   1073     COPYTHECONTEXT("SH: slice_type",(u1_slice_type));
   1074     ps_dec->u1_sl_typ_5_9 = 0;
   1075     /* Find Out the Slice Type is 5 to 9 or not then Set the Flag   */
   1076     /* u1_sl_typ_5_9 = 1 .Which tells that all the slices in the Pic*/
   1077     /* will be of same type of current                            */
   1078     if(u1_slice_type > 4)
   1079     {
   1080         u1_slice_type -= 5;
   1081         ps_dec->u1_sl_typ_5_9 = 1;
   1082     }
   1083 
   1084     {
   1085         UWORD32 skip;
   1086 
   1087         if((ps_dec->i4_app_skip_mode == IVD_SKIP_PB)
   1088                         || (ps_dec->i4_dec_skip_mode == IVD_SKIP_PB))
   1089         {
   1090             UWORD32 u4_bit_stream_offset = 0;
   1091 
   1092             if(ps_dec->u1_nal_unit_type == IDR_SLICE_NAL)
   1093             {
   1094                 skip = 0;
   1095 
   1096                 ps_dec->i4_dec_skip_mode = IVD_SKIP_NONE;
   1097             }
   1098             else if((I_SLICE == u1_slice_type)
   1099                             && (1 >= ps_dec->ps_cur_sps->u1_num_ref_frames))
   1100             {
   1101                 skip = 0;
   1102 
   1103                 ps_dec->i4_dec_skip_mode = IVD_SKIP_NONE;
   1104             }
   1105             else
   1106             {
   1107                 skip = 1;
   1108             }
   1109 
   1110             /* If one frame worth of data is already skipped, do not skip the next one */
   1111             if((0 == u2_first_mb_in_slice) && (1 == ps_dec->u4_prev_nal_skipped))
   1112             {
   1113                 skip = 0;
   1114             }
   1115 
   1116             if(skip)
   1117             {
   1118                 ps_dec->u4_prev_nal_skipped = 1;
   1119                 ps_dec->i4_dec_skip_mode = IVD_SKIP_PB;
   1120                 return 0;
   1121             }
   1122             else
   1123             {
   1124                 /* If the previous NAL was skipped, then
   1125                  do not process that buffer in this call.
   1126                  Return to app and process it in the next call.
   1127                  This is necessary to handle cases where I/IDR is not complete in
   1128                  the current buffer and application intends to fill the remaining part of the bitstream
   1129                  later. This ensures we process only frame worth of data in every call */
   1130                 if(1 == ps_dec->u4_prev_nal_skipped)
   1131                 {
   1132                     ps_dec->u4_return_to_app = 1;
   1133                     return 0;
   1134                 }
   1135             }
   1136         }
   1137 
   1138     }
   1139 
   1140     u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
   1141     if(u4_temp & MASK_ERR_PIC_SET_ID)
   1142         return ERROR_INV_SPS_PPS_T;
   1143     /* discard slice if pic param is invalid */
   1144     COPYTHECONTEXT("SH: pic_parameter_set_id", u4_temp);
   1145     ps_pps = &ps_dec->ps_pps[u4_temp];
   1146     if(FALSE == ps_pps->u1_is_valid)
   1147     {
   1148         return ERROR_INV_SPS_PPS_T;
   1149     }
   1150     ps_seq = ps_pps->ps_sps;
   1151     if(!ps_seq)
   1152         return ERROR_INV_SPS_PPS_T;
   1153     if(FALSE == ps_seq->u1_is_valid)
   1154         return ERROR_INV_SPS_PPS_T;
   1155 
   1156     /* Get the frame num */
   1157     u2_frame_num = ih264d_get_bits_h264(ps_bitstrm,
   1158                                          ps_seq->u1_bits_in_frm_num);
   1159 //    H264_DEC_DEBUG_PRINT("FRAME %d First MB in slice: %d\n", u2_frame_num, u2_first_mb_in_slice);
   1160 
   1161     COPYTHECONTEXT("SH: frame_num", u2_frame_num);
   1162 //    H264_DEC_DEBUG_PRINT("Second field: %d frame num: %d prv_frame_num: %d \n", ps_dec->u1_second_field, u2_frame_num, ps_dec->u2_prv_frame_num);
   1163 
   1164     /* Get the field related flags  */
   1165     if(!ps_seq->u1_frame_mbs_only_flag)
   1166     {
   1167 
   1168         u1_field_pic_flag = ih264d_get_bit_h264(ps_bitstrm);
   1169         COPYTHECONTEXT("SH: field_pic_flag", u1_field_pic_flag);
   1170         u1_bottom_field_flag = 0;
   1171 
   1172         if(u1_field_pic_flag)
   1173         {
   1174             ps_dec->pu1_inv_scan = (UWORD8 *)gau1_ih264d_inv_scan_fld;
   1175             u1_bottom_field_flag = ih264d_get_bit_h264(ps_bitstrm);
   1176             COPYTHECONTEXT("SH: bottom_field_flag", u1_bottom_field_flag);
   1177 
   1178         }
   1179         else
   1180         {
   1181             ps_dec->pu1_inv_scan = (UWORD8 *)gau1_ih264d_inv_scan;
   1182         }
   1183     }
   1184     else
   1185     {
   1186         u1_field_pic_flag = 0;
   1187         u1_bottom_field_flag = 0;
   1188 
   1189         ps_dec->pu1_inv_scan = (UWORD8 *)gau1_ih264d_inv_scan;
   1190     }
   1191 
   1192     u1_nal_unit_type = SLICE_NAL;
   1193     if(u1_is_idr_slice)
   1194     {
   1195         if(0 == u1_field_pic_flag)
   1196         {
   1197             ps_dec->u1_top_bottom_decoded = TOP_FIELD_ONLY | BOT_FIELD_ONLY;
   1198         }
   1199         u1_nal_unit_type = IDR_SLICE_NAL;
   1200         u4_idr_pic_id = ih264d_uev(pu4_bitstrm_ofst,
   1201                                    pu4_bitstrm_buf);
   1202         if(u4_idr_pic_id > 65535)
   1203             return ERROR_INV_SPS_PPS_T;
   1204         COPYTHECONTEXT("SH:  ", u4_idr_pic_id);
   1205     }
   1206 
   1207     /* read delta pic order count information*/
   1208     i_delta_poc[0] = i_delta_poc[1] = 0;
   1209     s_tmp_poc.i4_pic_order_cnt_lsb = 0;
   1210     s_tmp_poc.i4_delta_pic_order_cnt_bottom = 0;
   1211     u1_pic_order_cnt_type = ps_seq->u1_pic_order_cnt_type;
   1212     if(u1_pic_order_cnt_type == 0)
   1213     {
   1214         i_temp = ih264d_get_bits_h264(
   1215                         ps_bitstrm,
   1216                         ps_seq->u1_log2_max_pic_order_cnt_lsb_minus);
   1217         if(i_temp < 0 || i_temp >= ps_seq->i4_max_pic_order_cntLsb)
   1218             return ERROR_INV_SPS_PPS_T;
   1219         s_tmp_poc.i4_pic_order_cnt_lsb = i_temp;
   1220         COPYTHECONTEXT("SH: pic_order_cnt_lsb", s_tmp_poc.i4_pic_order_cnt_lsb);
   1221 
   1222         if((ps_pps->u1_pic_order_present_flag == 1) && (!u1_field_pic_flag))
   1223         {
   1224             s_tmp_poc.i4_delta_pic_order_cnt_bottom = ih264d_sev(
   1225                             pu4_bitstrm_ofst, pu4_bitstrm_buf);
   1226             //if(s_tmp_poc.i4_delta_pic_order_cnt_bottom > ps_seq->i4_max_pic_order_cntLsb)
   1227             COPYTHECONTEXT("SH: delta_pic_order_cnt_bottom",
   1228                             s_tmp_poc.i4_delta_pic_order_cnt_bottom);
   1229         }
   1230     }
   1231 
   1232     s_tmp_poc.i4_delta_pic_order_cnt[0] = 0;
   1233     s_tmp_poc.i4_delta_pic_order_cnt[1] = 0;
   1234     if(u1_pic_order_cnt_type == 1
   1235                     && (!ps_seq->u1_delta_pic_order_always_zero_flag))
   1236     {
   1237         s_tmp_poc.i4_delta_pic_order_cnt[0] = ih264d_sev(pu4_bitstrm_ofst,
   1238                                                          pu4_bitstrm_buf);
   1239         COPYTHECONTEXT("SH: delta_pic_order_cnt[0]",
   1240                         s_tmp_poc.i4_delta_pic_order_cnt[0]);
   1241 
   1242         if(ps_pps->u1_pic_order_present_flag && !u1_field_pic_flag)
   1243         {
   1244             s_tmp_poc.i4_delta_pic_order_cnt[1] = ih264d_sev(
   1245                             pu4_bitstrm_ofst, pu4_bitstrm_buf);
   1246             COPYTHECONTEXT("SH: delta_pic_order_cnt[1]",
   1247                             s_tmp_poc.i4_delta_pic_order_cnt[1]);
   1248         }
   1249     }
   1250 
   1251     if(ps_pps->u1_redundant_pic_cnt_present_flag)
   1252     {
   1253         u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
   1254         if(u4_temp > MAX_REDUNDANT_PIC_CNT)
   1255             return ERROR_INV_SPS_PPS_T;
   1256         u1_redundant_pic_cnt = u4_temp;
   1257         COPYTHECONTEXT("SH: redundant_pic_cnt", u1_redundant_pic_cnt);
   1258     }
   1259 
   1260     /*--------------------------------------------------------------------*/
   1261     /* Check if the slice is part of new picture                          */
   1262     /*--------------------------------------------------------------------*/
   1263     i1_is_end_of_poc = 0;
   1264     if(!ps_dec->u1_first_slice_in_stream)
   1265     {
   1266         i1_is_end_of_poc = ih264d_is_end_of_pic(u2_frame_num, u1_nal_ref_idc,
   1267                                             &s_tmp_poc, &ps_dec->s_cur_pic_poc,
   1268                                             ps_cur_slice, u1_pic_order_cnt_type,
   1269                                             u1_nal_unit_type, u4_idr_pic_id,
   1270                                             u1_field_pic_flag,
   1271                                             u1_bottom_field_flag);
   1272 
   1273         /* since we support only Full frame decode, every new process should
   1274          * process a new pic
   1275          */
   1276         if((ps_dec->u4_first_slice_in_pic == 2) && (i1_is_end_of_poc == 0))
   1277         {
   1278             /* if it is the first slice is process call ,it should be a new frame. If it is not
   1279              * reject current pic and dont add it to dpb
   1280              */
   1281             ps_dec->ps_dec_err_status->u1_err_flag |= REJECT_CUR_PIC;
   1282             i1_is_end_of_poc = 1;
   1283         }
   1284         else
   1285         {
   1286             /* reset REJECT_CUR_PIC */
   1287             ps_dec->ps_dec_err_status->u1_err_flag &= MASK_REJECT_CUR_PIC;
   1288         }
   1289     }
   1290 
   1291     /*--------------------------------------------------------------------*/
   1292     /* Check for error in slice and parse the missing/corrupted MB's      */
   1293     /* as skip-MB's in an inserted P-slice                                */
   1294     /*--------------------------------------------------------------------*/
   1295     u1_mbaff = ps_seq->u1_mb_aff_flag && (!u1_field_pic_flag);
   1296     prev_slice_err = 0;
   1297 
   1298     if(i1_is_end_of_poc || ps_dec->u1_first_slice_in_stream)
   1299     {
   1300         if(u2_frame_num != ps_dec->u2_prv_frame_num
   1301                && ps_dec->u1_top_bottom_decoded != 0
   1302                    && ps_dec->u1_top_bottom_decoded
   1303                        != (TOP_FIELD_ONLY | BOT_FIELD_ONLY))
   1304         {
   1305             ps_dec->u1_dangling_field = 1;
   1306             if(ps_dec->u4_first_slice_in_pic)
   1307             {
   1308                 // first slice - dangling field
   1309                 prev_slice_err = 1;
   1310             }
   1311             else
   1312             {
   1313                 // last slice - dangling field
   1314                 prev_slice_err = 2;
   1315             }
   1316 
   1317             if(ps_dec->u1_top_bottom_decoded ==TOP_FIELD_ONLY)
   1318                 ps_cur_slice->u1_bottom_field_flag = 1;
   1319             else
   1320                 ps_cur_slice->u1_bottom_field_flag = 0;
   1321 
   1322             num_mb_skipped = (ps_dec->u2_frm_ht_in_mbs * ps_dec->u2_frm_wd_in_mbs)
   1323                     - ps_dec->u2_total_mbs_coded;
   1324             ps_cur_poc = &ps_dec->s_cur_pic_poc;
   1325 
   1326             u1_is_idr_slice = ps_cur_slice->u1_nal_unit_type == IDR_SLICE_NAL;
   1327         }
   1328         else if(ps_dec->u4_first_slice_in_pic == 2)
   1329         {
   1330             if(u2_first_mb_in_slice > 0)
   1331             {
   1332                 // first slice - missing/header corruption
   1333                 prev_slice_err = 1;
   1334                 num_mb_skipped = u2_first_mb_in_slice << u1_mbaff;
   1335                 ps_cur_poc = &s_tmp_poc;
   1336 
   1337                 // initializing slice parameters
   1338                 ps_cur_slice->u4_idr_pic_id = u4_idr_pic_id;
   1339                 ps_cur_slice->u1_field_pic_flag = u1_field_pic_flag;
   1340                 ps_cur_slice->u1_bottom_field_flag = u1_bottom_field_flag;
   1341                 ps_cur_slice->i4_pic_order_cnt_lsb =
   1342                         s_tmp_poc.i4_pic_order_cnt_lsb;
   1343                 ps_cur_slice->u1_nal_unit_type = u1_nal_unit_type;
   1344                 ps_cur_slice->u1_redundant_pic_cnt = u1_redundant_pic_cnt;
   1345                 ps_cur_slice->u1_nal_ref_idc = u1_nal_ref_idc;
   1346                 ps_cur_slice->u1_pic_order_cnt_type = u1_pic_order_cnt_type;
   1347             }
   1348         }
   1349         else
   1350         {
   1351 
   1352             if(ps_dec->u4_first_slice_in_pic)
   1353             {
   1354                 /* if valid slice header is not decoded do start of pic processing
   1355                  * since in the current process call, frame num is not updated in the slice structure yet
   1356                  * ih264d_is_end_of_pic is checked with valid frame num of previous process call,
   1357                  * although i1_is_end_of_poc is set there could be  more slices in the frame,
   1358                  * so conceal only till cur slice */
   1359                 prev_slice_err = 1;
   1360                 num_mb_skipped = u2_first_mb_in_slice << u1_mbaff;
   1361             }
   1362             else
   1363             {
   1364                 /* since i1_is_end_of_poc is set ,means new frame num is encountered. so conceal the current frame
   1365                  * completely */
   1366                 prev_slice_err = 2;
   1367                 num_mb_skipped = (ps_dec->u2_frm_ht_in_mbs * ps_dec->u2_frm_wd_in_mbs)
   1368                         - ps_dec->u2_total_mbs_coded;
   1369             }
   1370             ps_cur_poc = &s_tmp_poc;
   1371         }
   1372     }
   1373     else
   1374     {
   1375         if((u2_first_mb_in_slice << u1_mbaff) > ps_dec->u2_total_mbs_coded)
   1376         {
   1377             // previous slice - missing/corruption
   1378             prev_slice_err = 2;
   1379             num_mb_skipped = (u2_first_mb_in_slice << u1_mbaff)
   1380                     - ps_dec->u2_total_mbs_coded;
   1381             ps_cur_poc = &s_tmp_poc;
   1382         }
   1383         else if((u2_first_mb_in_slice << u1_mbaff) < ps_dec->u2_total_mbs_coded)
   1384         {
   1385             return ERROR_CORRUPTED_SLICE;
   1386         }
   1387     }
   1388 
   1389     if(prev_slice_err)
   1390     {
   1391         ret = ih264d_mark_err_slice_skip(ps_dec, num_mb_skipped, u1_is_idr_slice, u2_frame_num, ps_cur_poc, prev_slice_err);
   1392 
   1393         if(ps_dec->u1_dangling_field == 1)
   1394         {
   1395             ps_dec->u1_second_field = 1 - ps_dec->u1_second_field;
   1396             ps_cur_slice->u1_bottom_field_flag = u1_bottom_field_flag;
   1397             ps_dec->u2_prv_frame_num = u2_frame_num;
   1398             ps_dec->u1_first_slice_in_stream = 0;
   1399             return ERROR_DANGLING_FIELD_IN_PIC;
   1400         }
   1401 
   1402         if(prev_slice_err == 2)
   1403         {
   1404             ps_dec->u1_first_slice_in_stream = 0;
   1405             return ERROR_INCOMPLETE_FRAME;
   1406         }
   1407 
   1408         if(ps_dec->u2_total_mbs_coded
   1409                 >= ps_dec->u2_frm_ht_in_mbs * ps_dec->u2_frm_wd_in_mbs)
   1410         {
   1411             /* return if all MBs in frame are parsed*/
   1412             ps_dec->u1_first_slice_in_stream = 0;
   1413             return ERROR_IN_LAST_SLICE_OF_PIC;
   1414         }
   1415 
   1416         if(ps_dec->ps_dec_err_status->u1_err_flag & REJECT_CUR_PIC)
   1417         {
   1418             ih264d_err_pic_dispbuf_mgr(ps_dec);
   1419             return ERROR_NEW_FRAME_EXPECTED;
   1420         }
   1421 
   1422         if(ret != OK)
   1423             return ret;
   1424 
   1425         i1_is_end_of_poc = 0;
   1426     }
   1427 
   1428     if (ps_dec->u4_first_slice_in_pic == 0)
   1429     {
   1430         ps_dec->ps_parse_cur_slice++;
   1431         ps_dec->u2_cur_slice_num++;
   1432     }
   1433 
   1434     // in the case of single core increment ps_decode_cur_slice
   1435     if((ps_dec->u1_separate_parse == 0) && (ps_dec->u4_first_slice_in_pic == 0))
   1436     {
   1437         ps_dec->ps_decode_cur_slice++;
   1438     }
   1439     ps_dec->u1_slice_header_done = 0;
   1440 
   1441     /*--------------------------------------------------------------------*/
   1442     /* If the slice is part of new picture, do End of Pic processing.     */
   1443     /*--------------------------------------------------------------------*/
   1444     if(!ps_dec->u1_first_slice_in_stream)
   1445     {
   1446         UWORD8 uc_mbs_exceed = 0;
   1447 
   1448         if(ps_dec->u2_total_mbs_coded
   1449                         == (ps_dec->ps_cur_sps->u2_max_mb_addr + 1))
   1450         {
   1451             /*u2_total_mbs_coded is forced  to u2_max_mb_addr+ 1 at the end of decode ,so
   1452              ,if it is first slice in pic dont consider u2_total_mbs_coded to detect new picture */
   1453             if(ps_dec->u4_first_slice_in_pic == 0)
   1454                 uc_mbs_exceed = 1;
   1455         }
   1456 
   1457         if(i1_is_end_of_poc || uc_mbs_exceed)
   1458         {
   1459 
   1460             if(1 == ps_dec->u1_last_pic_not_decoded)
   1461             {
   1462                 ret = ih264d_end_of_pic_dispbuf_mgr(ps_dec);
   1463 
   1464                 if(ret != OK)
   1465                     return ret;
   1466 
   1467                 ret = ih264d_end_of_pic(ps_dec, u1_is_idr_slice, u2_frame_num);
   1468                 if(ret != OK)
   1469                     return ret;
   1470 #if WIN32
   1471                 H264_DEC_DEBUG_PRINT(" ------ PIC SKIPPED ------\n");
   1472 #endif
   1473                 return RET_LAST_SKIP;
   1474             }
   1475             else
   1476             {
   1477                 ret = ih264d_end_of_pic(ps_dec, u1_is_idr_slice, u2_frame_num);
   1478                 if(ret != OK)
   1479                     return ret;
   1480             }
   1481 
   1482         }
   1483     }
   1484 
   1485     if(u1_field_pic_flag)
   1486     {
   1487         ps_dec->u2_prv_frame_num = u2_frame_num;
   1488     }
   1489 
   1490     if(ps_cur_slice->u1_mmco_equalto5)
   1491     {
   1492         WORD32 i4_temp_poc;
   1493         WORD32 i4_top_field_order_poc, i4_bot_field_order_poc;
   1494 
   1495         if(!ps_cur_slice->u1_field_pic_flag) // or a complementary field pair
   1496         {
   1497             i4_top_field_order_poc = ps_dec->ps_cur_pic->i4_top_field_order_cnt;
   1498             i4_bot_field_order_poc =
   1499                             ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
   1500             i4_temp_poc = MIN(i4_top_field_order_poc,
   1501                                      i4_bot_field_order_poc);
   1502         }
   1503         else if(!ps_cur_slice->u1_bottom_field_flag)
   1504             i4_temp_poc = ps_dec->ps_cur_pic->i4_top_field_order_cnt;
   1505         else
   1506             i4_temp_poc = ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
   1507 
   1508         ps_dec->ps_cur_pic->i4_top_field_order_cnt = i4_temp_poc
   1509                         - ps_dec->ps_cur_pic->i4_top_field_order_cnt;
   1510         ps_dec->ps_cur_pic->i4_bottom_field_order_cnt = i4_temp_poc
   1511                         - ps_dec->ps_cur_pic->i4_bottom_field_order_cnt;
   1512         ps_dec->ps_cur_pic->i4_poc = i4_temp_poc;
   1513         ps_dec->ps_cur_pic->i4_avg_poc = i4_temp_poc;
   1514     }
   1515     if(ps_dec->u4_first_slice_in_pic == 2)
   1516     {
   1517         ret = ih264d_decode_pic_order_cnt(u1_is_idr_slice, u2_frame_num,
   1518                                           &ps_dec->s_prev_pic_poc,
   1519                                           &s_tmp_poc, ps_cur_slice, ps_pps,
   1520                                           u1_nal_ref_idc,
   1521                                           u1_bottom_field_flag,
   1522                                           u1_field_pic_flag, &i4_poc);
   1523         if(ret != OK)
   1524             return ret;
   1525         /* Display seq no calculations */
   1526         if(i4_poc >= ps_dec->i4_max_poc)
   1527             ps_dec->i4_max_poc = i4_poc;
   1528         /* IDR Picture or POC wrap around */
   1529         if(i4_poc == 0)
   1530         {
   1531             ps_dec->i4_prev_max_display_seq = ps_dec->i4_prev_max_display_seq
   1532                             + ps_dec->i4_max_poc
   1533                             + ps_dec->u1_max_dec_frame_buffering + 1;
   1534             ps_dec->i4_max_poc = 0;
   1535         }
   1536     }
   1537 
   1538     /*--------------------------------------------------------------------*/
   1539     /* Copy the values read from the bitstream to the slice header and then*/
   1540     /* If the slice is first slice in picture, then do Start of Picture   */
   1541     /* processing.                                                        */
   1542     /*--------------------------------------------------------------------*/
   1543     ps_cur_slice->i4_delta_pic_order_cnt[0] = i_delta_poc[0];
   1544     ps_cur_slice->i4_delta_pic_order_cnt[1] = i_delta_poc[1];
   1545     ps_cur_slice->u4_idr_pic_id = u4_idr_pic_id;
   1546     ps_cur_slice->u2_first_mb_in_slice = u2_first_mb_in_slice;
   1547     ps_cur_slice->u1_field_pic_flag = u1_field_pic_flag;
   1548     ps_cur_slice->u1_bottom_field_flag = u1_bottom_field_flag;
   1549     ps_cur_slice->u1_slice_type = u1_slice_type;
   1550     ps_cur_slice->i4_pic_order_cnt_lsb = s_tmp_poc.i4_pic_order_cnt_lsb;
   1551 
   1552     ps_cur_slice->u1_nal_unit_type = u1_nal_unit_type;
   1553     ps_cur_slice->u1_redundant_pic_cnt = u1_redundant_pic_cnt;
   1554     ps_cur_slice->u1_nal_ref_idc = u1_nal_ref_idc;
   1555     ps_cur_slice->u1_pic_order_cnt_type = u1_pic_order_cnt_type;
   1556 
   1557     if(ps_seq->u1_frame_mbs_only_flag)
   1558         ps_cur_slice->u1_direct_8x8_inference_flag =
   1559                         ps_seq->u1_direct_8x8_inference_flag;
   1560     else
   1561         ps_cur_slice->u1_direct_8x8_inference_flag = 1;
   1562 
   1563     if(u1_slice_type == B_SLICE)
   1564     {
   1565         ps_cur_slice->u1_direct_spatial_mv_pred_flag = ih264d_get_bit_h264(
   1566                         ps_bitstrm);
   1567         COPYTHECONTEXT("SH: direct_spatial_mv_pred_flag",
   1568                         ps_cur_slice->u1_direct_spatial_mv_pred_flag);
   1569 
   1570         if(ps_cur_slice->u1_direct_spatial_mv_pred_flag)
   1571             ps_cur_slice->pf_decodeDirect = ih264d_decode_spatial_direct;
   1572         else
   1573             ps_cur_slice->pf_decodeDirect = ih264d_decode_temporal_direct;
   1574         if(!((ps_pps->ps_sps->u1_mb_aff_flag) && (!u1_field_pic_flag)))
   1575             ps_dec->pf_mvpred = ih264d_mvpred_nonmbaffB;
   1576     }
   1577     else
   1578     {
   1579         if(!((ps_pps->ps_sps->u1_mb_aff_flag) && (!u1_field_pic_flag)))
   1580             ps_dec->pf_mvpred = ih264d_mvpred_nonmbaff;
   1581     }
   1582 
   1583     if(ps_dec->u4_first_slice_in_pic == 2)
   1584     {
   1585         if(u2_first_mb_in_slice == 0)
   1586         {
   1587             ret = ih264d_start_of_pic(ps_dec, i4_poc, &s_tmp_poc, u2_frame_num, ps_pps);
   1588             if(ret != OK)
   1589                 return ret;
   1590         }
   1591 
   1592         ps_dec->u4_output_present = 0;
   1593 
   1594         {
   1595             ih264d_get_next_display_field(ps_dec,
   1596                                           ps_dec->ps_out_buffer,
   1597                                           &(ps_dec->s_disp_op));
   1598             /* If error code is non-zero then there is no buffer available for display,
   1599              hence avoid format conversion */
   1600 
   1601             if(0 != ps_dec->s_disp_op.u4_error_code)
   1602             {
   1603                 ps_dec->u4_fmt_conv_cur_row = ps_dec->s_disp_frame_info.u4_y_ht;
   1604             }
   1605             else
   1606                 ps_dec->u4_output_present = 1;
   1607         }
   1608         if(ps_dec->u1_separate_parse == 1)
   1609         {
   1610             if(ps_dec->u4_dec_thread_created == 0)
   1611             {
   1612                 ithread_create(ps_dec->pv_dec_thread_handle, NULL,
   1613                                (void *)ih264d_decode_picture_thread,
   1614                                (void *)ps_dec);
   1615 
   1616                 ps_dec->u4_dec_thread_created = 1;
   1617             }
   1618 
   1619             if((ps_dec->u4_num_cores == 3) &&
   1620                             ((ps_dec->u4_app_disable_deblk_frm == 0) || ps_dec->i1_recon_in_thread3_flag)
   1621                             && (ps_dec->u4_bs_deblk_thread_created == 0))
   1622             {
   1623                 ps_dec->u4_start_recon_deblk = 0;
   1624                 ithread_create(ps_dec->pv_bs_deblk_thread_handle, NULL,
   1625                                (void *)ih264d_recon_deblk_thread,
   1626                                (void *)ps_dec);
   1627                 ps_dec->u4_bs_deblk_thread_created = 1;
   1628             }
   1629         }
   1630 
   1631     }
   1632 
   1633     /* INITIALIZATION of fn ptrs for MC and formMbPartInfo functions */
   1634     {
   1635         UWORD8 uc_nofield_nombaff;
   1636 
   1637 
   1638 
   1639         uc_nofield_nombaff = ((ps_dec->ps_cur_slice->u1_field_pic_flag == 0)
   1640                         && (ps_dec->ps_cur_slice->u1_mbaff_frame_flag == 0)
   1641                         && (u1_slice_type != B_SLICE)
   1642                         && (ps_dec->ps_cur_pps->u1_wted_pred_flag == 0));
   1643 
   1644         /* Initialise MC and formMbPartInfo fn ptrs one time based on profile_idc */
   1645 
   1646         if(uc_nofield_nombaff)
   1647         {
   1648             ps_dec->p_form_mb_part_info = ih264d_form_mb_part_info_bp;
   1649             ps_dec->p_motion_compensate = ih264d_motion_compensate_bp;
   1650         }
   1651         else
   1652         {
   1653             ps_dec->p_form_mb_part_info = ih264d_form_mb_part_info_mp;
   1654             ps_dec->p_motion_compensate = ih264d_motion_compensate_mp;
   1655         }
   1656 
   1657 
   1658     }
   1659 
   1660     /*
   1661      * Decide whether to decode the current picture or not
   1662      */
   1663     {
   1664         dec_err_status_t * ps_err = ps_dec->ps_dec_err_status;
   1665         if(ps_err->u4_frm_sei_sync == u2_frame_num)
   1666         {
   1667             ps_err->u1_err_flag = ACCEPT_ALL_PICS;
   1668             ps_err->u4_frm_sei_sync = SYNC_FRM_DEFAULT;
   1669         }
   1670         ps_err->u4_cur_frm = u2_frame_num;
   1671     }
   1672 
   1673     /* Decision for decoding if the picture is to be skipped */
   1674     {
   1675         WORD32 i4_skip_b_pic, i4_skip_p_pic;
   1676 
   1677         i4_skip_b_pic = (ps_dec->u4_skip_frm_mask & B_SLC_BIT)
   1678                         && (B_SLICE == u1_slice_type) && (0 == u1_nal_ref_idc);
   1679 
   1680         i4_skip_p_pic = (ps_dec->u4_skip_frm_mask & P_SLC_BIT)
   1681                         && (P_SLICE == u1_slice_type) && (0 == u1_nal_ref_idc);
   1682 
   1683         /**************************************************************/
   1684         /* Skip the B picture if skip mask is set for B picture and   */
   1685         /* Current B picture is a non reference B picture or there is */
   1686         /* no user for reference B picture                            */
   1687         /**************************************************************/
   1688         if(i4_skip_b_pic)
   1689         {
   1690             ps_dec->ps_cur_pic->u4_pack_slc_typ |= B_SLC_BIT;
   1691             /* Don't decode the picture in SKIP-B mode if that picture is B */
   1692             /* and also it is not to be used as a reference picture         */
   1693             ps_dec->u1_last_pic_not_decoded = 1;
   1694 
   1695             return OK;
   1696         }
   1697         /**************************************************************/
   1698         /* Skip the P picture if skip mask is set for P picture and   */
   1699         /* Current P picture is a non reference P picture or there is */
   1700         /* no user for reference P picture                            */
   1701         /**************************************************************/
   1702         if(i4_skip_p_pic)
   1703         {
   1704             ps_dec->ps_cur_pic->u4_pack_slc_typ |= P_SLC_BIT;
   1705             /* Don't decode the picture in SKIP-P mode if that picture is P */
   1706             /* and also it is not to be used as a reference picture         */
   1707             ps_dec->u1_last_pic_not_decoded = 1;
   1708 
   1709             return OK;
   1710         }
   1711     }
   1712 
   1713     {
   1714         UWORD16 u2_mb_x, u2_mb_y;
   1715 
   1716         ps_dec->i4_submb_ofst = ((u2_first_mb_in_slice
   1717                         << ps_cur_slice->u1_mbaff_frame_flag) * SUB_BLK_SIZE)
   1718                         - SUB_BLK_SIZE;
   1719         if(u2_first_mb_in_slice)
   1720         {
   1721             UWORD8 u1_mb_aff;
   1722             UWORD8 u1_field_pic;
   1723             UWORD16 u2_frm_wd_in_mbs;
   1724             u2_frm_wd_in_mbs = ps_seq->u2_frm_wd_in_mbs;
   1725             u1_mb_aff = ps_cur_slice->u1_mbaff_frame_flag;
   1726             u1_field_pic = ps_cur_slice->u1_field_pic_flag;
   1727 
   1728             {
   1729                 UWORD32 x_offset;
   1730                 UWORD32 y_offset;
   1731                 UWORD32 u4_frame_stride;
   1732                 tfr_ctxt_t *ps_trns_addr; // = &ps_dec->s_tran_addrecon_parse;
   1733 
   1734                 if(ps_dec->u1_separate_parse)
   1735                 {
   1736                     ps_trns_addr = &ps_dec->s_tran_addrecon_parse;
   1737                 }
   1738                 else
   1739                 {
   1740                     ps_trns_addr = &ps_dec->s_tran_addrecon;
   1741                 }
   1742                 u2_mb_x = MOD(u2_first_mb_in_slice, u2_frm_wd_in_mbs);
   1743                 u2_mb_y = DIV(u2_first_mb_in_slice, u2_frm_wd_in_mbs);
   1744 
   1745                 u2_mb_y <<= u1_mb_aff;
   1746 
   1747                 if((u2_mb_x > u2_frm_wd_in_mbs - 1)
   1748                                 || (u2_mb_y > ps_dec->u2_frm_ht_in_mbs - 1))
   1749                 {
   1750                     return ERROR_CORRUPTED_SLICE;
   1751                 }
   1752 
   1753                 u4_frame_stride = ps_dec->u2_frm_wd_y << u1_field_pic;
   1754                 x_offset = u2_mb_x << 4;
   1755                 y_offset = (u2_mb_y * u4_frame_stride) << 4;
   1756 
   1757                 ps_trns_addr->pu1_dest_y = ps_dec->s_cur_pic.pu1_buf1 + x_offset
   1758                                 + y_offset;
   1759 
   1760                 u4_frame_stride = ps_dec->u2_frm_wd_uv << u1_field_pic;
   1761                 x_offset >>= 1;
   1762                 y_offset = (u2_mb_y * u4_frame_stride) << 3;
   1763 
   1764                 x_offset *= YUV420SP_FACTOR;
   1765 
   1766                 ps_trns_addr->pu1_dest_u = ps_dec->s_cur_pic.pu1_buf2 + x_offset
   1767                                 + y_offset;
   1768                 ps_trns_addr->pu1_dest_v = ps_dec->s_cur_pic.pu1_buf3 + x_offset
   1769                                 + y_offset;
   1770 
   1771                 ps_trns_addr->pu1_mb_y = ps_trns_addr->pu1_dest_y;
   1772                 ps_trns_addr->pu1_mb_u = ps_trns_addr->pu1_dest_u;
   1773                 ps_trns_addr->pu1_mb_v = ps_trns_addr->pu1_dest_v;
   1774 
   1775 
   1776                 // assign the deblock structure pointers to start of slice
   1777                 if(ps_dec->u1_separate_parse == 1)
   1778                 {
   1779                     ps_dec->ps_deblk_mbn = ps_dec->ps_deblk_pic
   1780                                     + (u2_first_mb_in_slice << u1_mb_aff);
   1781                 }
   1782                 else
   1783                 {
   1784                         ps_dec->ps_deblk_mbn = ps_dec->ps_deblk_pic
   1785                                         + (u2_first_mb_in_slice << u1_mb_aff);
   1786                 }
   1787 
   1788                 ps_dec->u2_cur_mb_addr = (u2_first_mb_in_slice << u1_mb_aff);
   1789 
   1790                 ps_dec->ps_mv_cur = ps_dec->s_cur_pic.ps_mv
   1791                                 + ((u2_first_mb_in_slice << u1_mb_aff) << 4);
   1792             }
   1793         }
   1794         else
   1795         {
   1796             tfr_ctxt_t *ps_trns_addr;
   1797 
   1798             if(ps_dec->u1_separate_parse)
   1799             {
   1800                 ps_trns_addr = &ps_dec->s_tran_addrecon_parse;
   1801             }
   1802             else
   1803             {
   1804                 ps_trns_addr = &ps_dec->s_tran_addrecon;
   1805             }
   1806 
   1807             u2_mb_x = 0xffff;
   1808             u2_mb_y = 0;
   1809             // assign the deblock structure pointers to start of slice
   1810             ps_dec->u2_cur_mb_addr = 0;
   1811             ps_dec->ps_deblk_mbn = ps_dec->ps_deblk_pic;
   1812             ps_dec->ps_mv_cur = ps_dec->s_cur_pic.ps_mv;
   1813             ps_trns_addr->pu1_dest_y = ps_dec->s_cur_pic.pu1_buf1;
   1814             ps_trns_addr->pu1_dest_u = ps_dec->s_cur_pic.pu1_buf2;
   1815             ps_trns_addr->pu1_dest_v = ps_dec->s_cur_pic.pu1_buf3;
   1816 
   1817             ps_trns_addr->pu1_mb_y = ps_trns_addr->pu1_dest_y;
   1818             ps_trns_addr->pu1_mb_u = ps_trns_addr->pu1_dest_u;
   1819             ps_trns_addr->pu1_mb_v = ps_trns_addr->pu1_dest_v;
   1820 
   1821         }
   1822 
   1823         ps_dec->ps_part = ps_dec->ps_parse_part_params;
   1824 
   1825         ps_dec->u2_mbx =
   1826                         (MOD(u2_first_mb_in_slice - 1, ps_seq->u2_frm_wd_in_mbs));
   1827         ps_dec->u2_mby =
   1828                         (DIV(u2_first_mb_in_slice - 1, ps_seq->u2_frm_wd_in_mbs));
   1829         ps_dec->u2_mby <<= ps_cur_slice->u1_mbaff_frame_flag;
   1830         ps_dec->i2_prev_slice_mbx = ps_dec->u2_mbx;
   1831         ps_dec->i2_prev_slice_mby = ps_dec->u2_mby;
   1832     }
   1833 
   1834     /* RBSP stop bit is used for CABAC decoding*/
   1835     ps_bitstrm->u4_max_ofst += ps_dec->ps_cur_pps->u1_entropy_coding_mode;
   1836 
   1837     ps_dec->u1_B = (u1_slice_type == B_SLICE);
   1838     ps_dec->u4_next_mb_skip = 0;
   1839 
   1840     ps_dec->ps_parse_cur_slice->u4_first_mb_in_slice =
   1841                     ps_dec->ps_cur_slice->u2_first_mb_in_slice;
   1842     ps_dec->ps_parse_cur_slice->slice_type =
   1843                     ps_dec->ps_cur_slice->u1_slice_type;
   1844 
   1845 
   1846     ps_dec->u4_start_recon_deblk = 1;
   1847     {
   1848         WORD32 num_entries;
   1849         WORD32 size;
   1850         UWORD8 *pu1_buf;
   1851 
   1852         num_entries = MAX_FRAMES;
   1853         if((1 >= ps_dec->ps_cur_sps->u1_num_ref_frames) &&
   1854             (0 == ps_dec->i4_display_delay))
   1855         {
   1856             num_entries = 1;
   1857         }
   1858         num_entries = ((2 * num_entries) + 1);
   1859         if(BASE_PROFILE_IDC != ps_dec->ps_cur_sps->u1_profile_idc)
   1860         {
   1861             num_entries *= 2;
   1862         }
   1863 
   1864         size = num_entries * sizeof(void *);
   1865         size += PAD_MAP_IDX_POC * sizeof(void *);
   1866 
   1867         pu1_buf = (UWORD8 *)ps_dec->pv_map_ref_idx_to_poc_buf;
   1868         pu1_buf += size * ps_dec->u2_cur_slice_num;
   1869         ps_dec->ps_parse_cur_slice->ppv_map_ref_idx_to_poc = ( void *)pu1_buf;
   1870     }
   1871 
   1872     if(ps_dec->u1_separate_parse)
   1873     {
   1874         ps_dec->ps_parse_cur_slice->pv_tu_coeff_data_start = ps_dec->pv_parse_tu_coeff_data;
   1875     }
   1876     else
   1877     {
   1878         ps_dec->pv_proc_tu_coeff_data = ps_dec->pv_parse_tu_coeff_data;
   1879     }
   1880 
   1881     if(u1_slice_type == I_SLICE)
   1882     {
   1883         ps_dec->ps_cur_pic->u4_pack_slc_typ |= I_SLC_BIT;
   1884 
   1885         ret = ih264d_parse_islice(ps_dec, u2_first_mb_in_slice);
   1886 
   1887         if(ps_dec->i4_pic_type != B_SLICE && ps_dec->i4_pic_type != P_SLICE)
   1888             ps_dec->i4_pic_type = I_SLICE;
   1889 
   1890     }
   1891     else if(u1_slice_type == P_SLICE)
   1892     {
   1893         ps_dec->ps_cur_pic->u4_pack_slc_typ |= P_SLC_BIT;
   1894         ret = ih264d_parse_pslice(ps_dec, u2_first_mb_in_slice);
   1895         ps_dec->u1_pr_sl_type = u1_slice_type;
   1896         if(ps_dec->i4_pic_type != B_SLICE)
   1897             ps_dec->i4_pic_type = P_SLICE;
   1898     }
   1899     else if(u1_slice_type == B_SLICE)
   1900     {
   1901         ps_dec->ps_cur_pic->u4_pack_slc_typ |= B_SLC_BIT;
   1902         ret = ih264d_parse_bslice(ps_dec, u2_first_mb_in_slice);
   1903         ps_dec->u1_pr_sl_type = u1_slice_type;
   1904         ps_dec->i4_pic_type = B_SLICE;
   1905     }
   1906     else
   1907         return ERROR_INV_SLC_TYPE_T;
   1908 
   1909     if(ps_dec->u1_slice_header_done)
   1910     {
   1911         /* set to zero to indicate a valid slice has been decoded */
   1912         /* first slice header successfully decoded */
   1913         ps_dec->u4_first_slice_in_pic = 0;
   1914         ps_dec->u1_first_slice_in_stream = 0;
   1915     }
   1916 
   1917     if(ret != OK)
   1918         return ret;
   1919 
   1920     /* storing last Mb X and MbY of the slice */
   1921     ps_dec->i2_prev_slice_mbx = ps_dec->u2_mbx;
   1922     ps_dec->i2_prev_slice_mby = ps_dec->u2_mby;
   1923 
   1924     /* End of Picture detection */
   1925 
   1926     if(ps_dec->u2_total_mbs_coded >= (ps_seq->u2_max_mb_addr + 1))
   1927     {
   1928         ps_dec->u1_pic_decode_done = 1;
   1929 
   1930     }
   1931 
   1932     {
   1933         dec_err_status_t * ps_err = ps_dec->ps_dec_err_status;
   1934         if((ps_err->u1_err_flag & REJECT_PB_PICS)
   1935                         && (ps_err->u1_cur_pic_type == PIC_TYPE_I))
   1936         {
   1937             ps_err->u1_err_flag = ACCEPT_ALL_PICS;
   1938         }
   1939     }
   1940 
   1941     PRINT_BIN_BIT_RATIO(ps_dec)
   1942 
   1943     return ret;
   1944 }
   1945 
   1946