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