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  **************************************************************************
     23  * \file ih264d_parse_islice.c
     24  *
     25  * \brief
     26  *    Contains routines that decode a I slice type
     27  *
     28  * Detailed_description
     29  *
     30  * \date
     31  *    07/07/2003
     32  *
     33  * \author  NS
     34  **************************************************************************
     35  */
     36 #include "ih264d_error_handler.h"
     37 #include "ih264d_debug.h"
     38 #include <string.h>
     39 #include "ih264d_bitstrm.h"
     40 #include "ih264d_defs.h"
     41 #include "ih264d_debug.h"
     42 #include "ih264d_tables.h"
     43 #include "ih264d_structs.h"
     44 #include "ih264d_defs.h"
     45 #include "ih264d_parse_cavlc.h"
     46 #include "ih264d_mb_utils.h"
     47 #include "ih264d_deblocking.h"
     48 #include "ih264d_cabac.h"
     49 #include "ih264d_parse_cabac.h"
     50 #include "ih264d_parse_mb_header.h"
     51 #include "ih264d_parse_slice.h"
     52 #include "ih264d_process_pslice.h"
     53 #include "ih264d_process_intra_mb.h"
     54 #include "ih264d_parse_islice.h"
     55 #include "ih264d_error_handler.h"
     56 #include "ih264d_mvpred.h"
     57 #include "ih264d_defs.h"
     58 #include "ih264d_thread_parse_decode.h"
     59 #include "ithread.h"
     60 #include "ih264d_parse_mb_header.h"
     61 #include "assert.h"
     62 #include "ih264d_utils.h"
     63 #include "ih264d_format_conv.h"
     64 
     65 void ih264d_init_cabac_contexts(UWORD8 u1_slice_type, dec_struct_t * ps_dec);
     66 
     67 void ih264d_itrans_recon_luma_dc(dec_struct_t *ps_dec,
     68                                  WORD16* pi2_src,
     69                                  WORD16* pi2_coeff_block,
     70                                  const UWORD16 *pu2_weigh_mat);
     71 
     72 
     73 
     74 /*!
     75  **************************************************************************
     76  * \if Function name : ParseIMb \endif
     77  *
     78  * \brief
     79  *    This function parses CAVLC syntax of a I MB. If 16x16 Luma DC transform
     80  *    is also done here. Transformed Luma DC values are copied in their
     81  *    0th pixel location of corrosponding CoeffBlock.
     82  *
     83  * \return
     84  *    0 on Success and Error code otherwise
     85  **************************************************************************
     86  */
     87 WORD32 ih264d_parse_imb_cavlc(dec_struct_t * ps_dec,
     88                               dec_mb_info_t * ps_cur_mb_info,
     89                               UWORD8 u1_mb_num,
     90                               UWORD8 u1_mb_type)
     91 {
     92     WORD32 i4_delta_qp;
     93     UWORD32 u4_temp;
     94     UWORD32 ui_is_top_mb_available;
     95     UWORD32 ui_is_left_mb_available;
     96     UWORD32 u4_cbp;
     97     UWORD32 u4_offset;
     98     UWORD32 *pu4_bitstrm_buf;
     99     WORD32 ret;
    100 
    101     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
    102     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
    103     UNUSED(u1_mb_num);
    104     ps_cur_mb_info->u1_tran_form8x8 = 0;
    105     ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
    106 
    107     ps_cur_mb_info->u1_yuv_dc_block_flag = 0;
    108 
    109     u4_temp = ps_dec->u1_mb_ngbr_availablity;
    110     ui_is_top_mb_available = BOOLEAN(u4_temp & TOP_MB_AVAILABLE_MASK);
    111     ui_is_left_mb_available = BOOLEAN(u4_temp & LEFT_MB_AVAILABLE_MASK);
    112 
    113     pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
    114 
    115     if(u1_mb_type == I_4x4_MB)
    116     {
    117         ps_cur_mb_info->ps_curmb->u1_mb_type = I_4x4_MB;
    118         u4_offset = 0;
    119 
    120         /*--------------------------------------------------------------------*/
    121         /* Read transform_size_8x8_flag if present                            */
    122         /*--------------------------------------------------------------------*/
    123         if(ps_dec->s_high_profile.u1_transform8x8_present)
    124         {
    125             ps_cur_mb_info->u1_tran_form8x8 = ih264d_get_bit_h264(ps_bitstrm);
    126             COPYTHECONTEXT("transform_size_8x8_flag", ps_cur_mb_info->u1_tran_form8x8);
    127             ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = ps_cur_mb_info->u1_tran_form8x8;
    128         }
    129 
    130         /*--------------------------------------------------------------------*/
    131         /* Read the IntraPrediction modes for LUMA                            */
    132         /*--------------------------------------------------------------------*/
    133         if (!ps_cur_mb_info->u1_tran_form8x8)
    134         {
    135             UWORD8 *pu1_temp;
    136             ih264d_read_intra_pred_modes(ps_dec,
    137                                           ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data),
    138                                           ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data+16),
    139                                           ps_cur_mb_info->u1_tran_form8x8);
    140             pu1_temp = (UWORD8 *)ps_dec->pv_parse_tu_coeff_data;
    141             pu1_temp += 32;
    142             ps_dec->pv_parse_tu_coeff_data = (void *)pu1_temp;
    143         }
    144         else
    145         {
    146             UWORD8 *pu1_temp;
    147             ih264d_read_intra_pred_modes(ps_dec,
    148                                           ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data),
    149                                           ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data+4),
    150                                           ps_cur_mb_info->u1_tran_form8x8);
    151             pu1_temp = (UWORD8 *)ps_dec->pv_parse_tu_coeff_data;
    152             pu1_temp += 8;
    153             ps_dec->pv_parse_tu_coeff_data = (void *)pu1_temp;
    154         }
    155         /*--------------------------------------------------------------------*/
    156         /* Read the IntraPrediction mode for CHROMA                           */
    157         /*--------------------------------------------------------------------*/
    158 //Inlined ih264d_uev
    159         {
    160             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
    161             UWORD32 u4_word, u4_ldz, u4_temp;
    162 
    163             /***************************************************************/
    164             /* Find leading zeros in next 32 bits                          */
    165             /***************************************************************/
    166             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
    167             u4_ldz = CLZ(u4_word);
    168             /* Flush the ps_bitstrm */
    169             u4_bitstream_offset += (u4_ldz + 1);
    170             /* Read the suffix from the ps_bitstrm */
    171             u4_word = 0;
    172             if(u4_ldz)
    173             {
    174                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
    175                         u4_ldz);
    176             }
    177             *pu4_bitstrm_ofst = u4_bitstream_offset;
    178             u4_temp = ((1 << u4_ldz) + u4_word - 1);
    179             if(u4_temp > 3)
    180             {
    181                 return ERROR_CHROMA_PRED_MODE;
    182             }
    183             ps_cur_mb_info->u1_chroma_pred_mode = u4_temp;
    184             COPYTHECONTEXT("intra_chroma_pred_mode", ps_cur_mb_info->u1_chroma_pred_mode);
    185         }
    186         /*--------------------------------------------------------------------*/
    187         /* Read the Coded block pattern                                       */
    188         /*--------------------------------------------------------------------*/
    189         {
    190             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
    191             UWORD32 u4_word, u4_ldz;
    192 
    193             /***************************************************************/
    194             /* Find leading zeros in next 32 bits                          */
    195             /***************************************************************/
    196             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
    197             u4_ldz = CLZ(u4_word);
    198             /* Flush the ps_bitstrm */
    199             u4_bitstream_offset += (u4_ldz + 1);
    200             /* Read the suffix from the ps_bitstrm */
    201             u4_word = 0;
    202             if(u4_ldz)
    203             {
    204                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
    205                         u4_ldz);
    206             }
    207             *pu4_bitstrm_ofst = u4_bitstream_offset;
    208             u4_cbp = ((1 << u4_ldz) + u4_word - 1);
    209         }
    210         if(u4_cbp > 47)
    211         {
    212             return ERROR_CBP;
    213         }
    214 
    215         u4_cbp = gau1_ih264d_cbp_table[u4_cbp][0];
    216         COPYTHECONTEXT("coded_block_pattern", u1_cbp);
    217         ps_cur_mb_info->u1_cbp = u4_cbp;
    218 
    219         /*--------------------------------------------------------------------*/
    220         /* Read mb_qp_delta                                                   */
    221         /*--------------------------------------------------------------------*/
    222         if(ps_cur_mb_info->u1_cbp)
    223         {
    224             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
    225             UWORD32 u4_word, u4_ldz, u4_abs_val;
    226 
    227             /***************************************************************/
    228             /* Find leading zeros in next 32 bits                          */
    229             /***************************************************************/
    230             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
    231             u4_ldz = CLZ(u4_word);
    232 
    233             /* Flush the ps_bitstrm */
    234             u4_bitstream_offset += (u4_ldz + 1);
    235 
    236             /* Read the suffix from the ps_bitstrm */
    237             u4_word = 0;
    238             if(u4_ldz)
    239             {
    240                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
    241                         u4_ldz);
    242             }
    243 
    244             *pu4_bitstrm_ofst = u4_bitstream_offset;
    245             u4_abs_val = ((1 << u4_ldz) + u4_word) >> 1;
    246 
    247             if(u4_word & 0x1)
    248             {
    249                 i4_delta_qp = (-(WORD32)u4_abs_val);
    250             }
    251             else
    252             {
    253                 i4_delta_qp = (u4_abs_val);
    254             }
    255 
    256             if((i4_delta_qp < -26) || (i4_delta_qp > 25))
    257             {
    258                 return ERROR_INV_RANGE_QP_T;
    259             }
    260 
    261             COPYTHECONTEXT("mb_qp_delta", i1_delta_qp);
    262             if(i4_delta_qp != 0)
    263             {
    264                 ret = ih264d_update_qp(ps_dec, (WORD8)i4_delta_qp);
    265                 if(ret != OK)
    266                     return ret;
    267             }
    268         }
    269 
    270     }
    271     else
    272     {
    273         u4_offset = 1;
    274         ps_cur_mb_info->ps_curmb->u1_mb_type = I_16x16_MB;
    275         /*-------------------------------------------------------------------*/
    276         /* Read the IntraPrediction mode for CHROMA                          */
    277         /*-------------------------------------------------------------------*/
    278         {
    279             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
    280             UWORD32 u4_word, u4_ldz;
    281 
    282             /***************************************************************/
    283             /* Find leading zeros in next 32 bits                          */
    284             /***************************************************************/
    285             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
    286             u4_ldz = CLZ(u4_word);
    287             /* Flush the ps_bitstrm */
    288             u4_bitstream_offset += (u4_ldz + 1);
    289             /* Read the suffix from the ps_bitstrm */
    290             u4_word = 0;
    291             if(u4_ldz)
    292             {
    293                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
    294                         u4_ldz);
    295             }
    296             *pu4_bitstrm_ofst = u4_bitstream_offset;
    297             u4_temp = ((1 << u4_ldz) + u4_word - 1);
    298 
    299 //Inlined ih264d_uev
    300 
    301             if(u4_temp > 3)
    302             {
    303                 return ERROR_CHROMA_PRED_MODE;
    304             }
    305             ps_cur_mb_info->u1_chroma_pred_mode = u4_temp;
    306             COPYTHECONTEXT("intra_chroma_pred_mode", ps_cur_mb_info->u1_chroma_pred_mode);
    307         }
    308         /*-------------------------------------------------------------------*/
    309         /* Read the Coded block pattern                                      */
    310         /*-------------------------------------------------------------------*/
    311         u4_cbp = gau1_ih264d_cbp_tab[(u1_mb_type - 1) >> 2];
    312         ps_cur_mb_info->u1_cbp = u4_cbp;
    313 
    314         /*-------------------------------------------------------------------*/
    315         /* Read mb_qp_delta                                                  */
    316         /*-------------------------------------------------------------------*/
    317         {
    318             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
    319             UWORD32 u4_word, u4_ldz, u4_abs_val;
    320 
    321             /***************************************************************/
    322             /* Find leading zeros in next 32 bits                          */
    323             /***************************************************************/
    324             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
    325             u4_ldz = CLZ(u4_word);
    326 
    327             /* Flush the ps_bitstrm */
    328             u4_bitstream_offset += (u4_ldz + 1);
    329 
    330             /* Read the suffix from the ps_bitstrm */
    331             u4_word = 0;
    332             if(u4_ldz)
    333                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
    334                         u4_ldz);
    335 
    336             *pu4_bitstrm_ofst = u4_bitstream_offset;
    337             u4_abs_val = ((1 << u4_ldz) + u4_word) >> 1;
    338 
    339             if(u4_word & 0x1)
    340                 i4_delta_qp = (-(WORD32)u4_abs_val);
    341             else
    342                 i4_delta_qp = (u4_abs_val);
    343 
    344             if((i4_delta_qp < -26) || (i4_delta_qp > 25))
    345                 return ERROR_INV_RANGE_QP_T;
    346 
    347         }
    348 //inlinined ih264d_sev
    349         COPYTHECONTEXT("Delta quant", i1_delta_qp);
    350 
    351         if(i4_delta_qp != 0)
    352         {
    353             ret = ih264d_update_qp(ps_dec, (WORD8)i4_delta_qp);
    354             if(ret != OK)
    355                 return ret;
    356         }
    357 
    358         {
    359             WORD16 i_scaleFactor;
    360             UWORD32 ui_N = 0;
    361             WORD16 *pi2_scale_matrix_ptr;
    362             /*******************************************************************/
    363             /* for luma DC coefficients the scaling is done during the parsing */
    364             /* to preserve the precision                                       */
    365             /*******************************************************************/
    366             if(ps_dec->s_high_profile.u1_scaling_present)
    367             {
    368                 pi2_scale_matrix_ptr =
    369                                 ps_dec->s_high_profile.i2_scalinglist4x4[0];
    370             }
    371             else
    372             {
    373                 i_scaleFactor = 16;
    374                 pi2_scale_matrix_ptr = &i_scaleFactor;
    375             }
    376 
    377             /*---------------------------------------------------------------*/
    378             /* Decode DC coefficients                                        */
    379             /*---------------------------------------------------------------*/
    380             /*---------------------------------------------------------------*/
    381             /* Calculation of N                                              */
    382             /*---------------------------------------------------------------*/
    383             if(ui_is_left_mb_available)
    384             {
    385 
    386                 if(ui_is_top_mb_available)
    387                 {
    388                     ui_N = ((ps_cur_mb_info->ps_top_mb->pu1_nnz_y[0]
    389                                     + ps_dec->pu1_left_nnz_y[0] + 1) >> 1);
    390                 }
    391                 else
    392                 {
    393                     ui_N = ps_dec->pu1_left_nnz_y[0];
    394                 }
    395             }
    396             else if(ui_is_top_mb_available)
    397             {
    398                 ui_N = ps_cur_mb_info->ps_top_mb->pu1_nnz_y[0];
    399             }
    400 
    401             {
    402                 WORD16 pi2_dc_coef[16];
    403                 WORD32 pi4_tmp[16];
    404                 tu_sblk4x4_coeff_data_t *ps_tu_4x4 =
    405                                 (tu_sblk4x4_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
    406                 WORD16 *pi2_coeff_block =
    407                                 (WORD16 *)ps_dec->pv_parse_tu_coeff_data;
    408                 UWORD32 u4_num_coeff;
    409                 ps_tu_4x4->u2_sig_coeff_map = 0;
    410 
    411                 ret = ps_dec->pf_cavlc_parse4x4coeff[(ui_N > 7)](pi2_dc_coef, 0, ui_N,
    412                                                                  ps_dec, &u4_num_coeff);
    413                 if(ret != OK)
    414                     return ret;
    415 
    416                 if(EXCEED_OFFSET(ps_bitstrm))
    417                     return ERROR_EOB_TERMINATE_T;
    418                 if(ps_tu_4x4->u2_sig_coeff_map)
    419                 {
    420                     memset(pi2_dc_coef,0,sizeof(pi2_dc_coef));
    421                     ih264d_unpack_coeff4x4_dc_4x4blk(ps_tu_4x4,
    422                                                      pi2_dc_coef,
    423                                                      ps_dec->pu1_inv_scan);
    424 
    425                     PROFILE_DISABLE_IQ_IT_RECON()
    426                     ps_dec->pf_ihadamard_scaling_4x4(pi2_dc_coef,
    427                                                      pi2_coeff_block,
    428                                                      ps_dec->pu2_quant_scale_y,
    429                                                      (UWORD16 *)pi2_scale_matrix_ptr,
    430                                                      ps_dec->u1_qp_y_div6,
    431                                                      pi4_tmp);
    432                     pi2_coeff_block += 16;
    433                     ps_dec->pv_parse_tu_coeff_data = (void *)pi2_coeff_block;
    434                     SET_BIT(ps_cur_mb_info->u1_yuv_dc_block_flag,0);
    435                 }
    436 
    437             }
    438         }
    439     }
    440 
    441 
    442     if(u4_cbp)
    443     {
    444 
    445         ret = ih264d_parse_residual4x4_cavlc(ps_dec, ps_cur_mb_info,
    446                                        (UWORD8)u4_offset);
    447         if(ret != OK)
    448             return ret;
    449         if(EXCEED_OFFSET(ps_bitstrm))
    450             return ERROR_EOB_TERMINATE_T;
    451 
    452         /* Store Left Mb NNZ and TOP chroma NNZ */
    453     }
    454     else
    455     {
    456         ps_cur_mb_info->u1_qp_div6 = ps_dec->u1_qp_y_div6;
    457         ps_cur_mb_info->u1_qpc_div6 = ps_dec->u1_qp_u_div6;
    458         ps_cur_mb_info->u1_qpcr_div6 = ps_dec->u1_qp_v_div6;
    459         ps_cur_mb_info->u1_qp_rem6 = ps_dec->u1_qp_y_rem6;
    460         ps_cur_mb_info->u1_qpc_rem6 = ps_dec->u1_qp_u_rem6;
    461         ps_cur_mb_info->u1_qpcr_rem6 = ps_dec->u1_qp_v_rem6;
    462         ih264d_update_nnz_for_skipmb(ps_dec, ps_cur_mb_info, CAVLC);
    463     }
    464 
    465     return OK;
    466 }
    467 
    468 /*!
    469  **************************************************************************
    470  * \if Function name : ParseIMbCab \endif
    471  *
    472  * \brief
    473  *    This function parses CABAC syntax of a I MB. If 16x16 Luma DC transform
    474  *    is also done here. Transformed Luma DC values are copied in their
    475  *    0th pixel location of corrosponding CoeffBlock.
    476  *
    477  * \return
    478  *    0 on Success and Error code otherwise
    479  **************************************************************************
    480  */
    481 WORD32 ih264d_parse_imb_cabac(dec_struct_t * ps_dec,
    482                               dec_mb_info_t * ps_cur_mb_info,
    483                               UWORD8 u1_mb_type)
    484 {
    485     WORD8 i1_delta_qp;
    486     UWORD8 u1_cbp;
    487     UWORD8 u1_offset;
    488     /* Variables for handling Cabac contexts */
    489     ctxt_inc_mb_info_t *p_curr_ctxt = ps_dec->ps_curr_ctxt_mb_info;
    490     ctxt_inc_mb_info_t *ps_left_ctxt = ps_dec->p_left_ctxt_mb_info;
    491     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
    492     bin_ctxt_model_t *p_bin_ctxt;
    493 
    494     UWORD8 u1_intra_chrom_pred_mode;
    495     UWORD8 u1_dc_block_flag = 0;
    496     WORD32 ret;
    497 
    498     ps_cur_mb_info->u1_yuv_dc_block_flag = 0;
    499 
    500     if(ps_left_ctxt == ps_dec->ps_def_ctxt_mb_info)
    501     {
    502         ps_dec->pu1_left_yuv_dc_csbp[0] = 0xf;
    503     }
    504 
    505     if(ps_dec->ps_cur_slice->u1_slice_type != I_SLICE)
    506     {
    507         WORD32 *pi4_buf;
    508         WORD8 *pi1_buf;
    509         MEMSET_16BYTES(&ps_dec->pu1_left_mv_ctxt_inc[0][0], 0);
    510         *((UWORD32 *)ps_dec->pi1_left_ref_idx_ctxt_inc) = 0;
    511         MEMSET_16BYTES(p_curr_ctxt->u1_mv, 0);
    512         memset(p_curr_ctxt->i1_ref_idx, 0, 4);
    513     }
    514 
    515     if(u1_mb_type == I_4x4_MB)
    516     {
    517         ps_cur_mb_info->ps_curmb->u1_mb_type = I_4x4_MB;
    518         p_curr_ctxt->u1_mb_type = CAB_I4x4;
    519         u1_offset = 0;
    520 
    521         ps_cur_mb_info->u1_tran_form8x8 = 0;
    522         ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
    523 
    524         /*--------------------------------------------------------------------*/
    525         /* Read transform_size_8x8_flag if present                            */
    526         /*--------------------------------------------------------------------*/
    527         if(ps_dec->s_high_profile.u1_transform8x8_present)
    528         {
    529             ps_cur_mb_info->u1_tran_form8x8 = ih264d_parse_transform8x8flag_cabac(
    530                             ps_dec, ps_cur_mb_info);
    531             COPYTHECONTEXT("transform_size_8x8_flag", ps_cur_mb_info->u1_tran_form8x8);
    532             p_curr_ctxt->u1_transform8x8_ctxt = ps_cur_mb_info->u1_tran_form8x8;
    533             ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = ps_cur_mb_info->u1_tran_form8x8;
    534         }
    535         else
    536         {
    537             p_curr_ctxt->u1_transform8x8_ctxt = 0;
    538         }
    539 
    540         /*--------------------------------------------------------------------*/
    541         /* Read the IntraPrediction modes for LUMA                            */
    542         /*--------------------------------------------------------------------*/
    543         if (!ps_cur_mb_info->u1_tran_form8x8)
    544         {
    545             UWORD8 *pu1_temp;
    546             ih264d_read_intra_pred_modes_cabac(
    547                             ps_dec,
    548                             ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data),
    549                             ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data+16),
    550                             ps_cur_mb_info->u1_tran_form8x8);
    551             pu1_temp = (UWORD8 *)ps_dec->pv_parse_tu_coeff_data;
    552             pu1_temp += 32;
    553             ps_dec->pv_parse_tu_coeff_data = (void *)pu1_temp;
    554         }
    555         else
    556         {
    557             UWORD8 *pu1_temp;
    558             ih264d_read_intra_pred_modes_cabac(
    559                             ps_dec,
    560                             ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data),
    561                             ((UWORD8 *)ps_dec->pv_parse_tu_coeff_data+4),
    562                             ps_cur_mb_info->u1_tran_form8x8);
    563             pu1_temp = (UWORD8 *)ps_dec->pv_parse_tu_coeff_data;
    564             pu1_temp += 8;
    565             ps_dec->pv_parse_tu_coeff_data = (void *)pu1_temp;
    566         }
    567         /*--------------------------------------------------------------------*/
    568         /* Read the IntraPrediction mode for CHROMA                           */
    569         /*--------------------------------------------------------------------*/
    570         u1_intra_chrom_pred_mode = ih264d_parse_chroma_pred_mode_cabac(ps_dec);
    571         COPYTHECONTEXT("intra_chroma_pred_mode", u1_intra_chrom_pred_mode);
    572         p_curr_ctxt->u1_intra_chroma_pred_mode = ps_cur_mb_info->u1_chroma_pred_mode =
    573                         u1_intra_chrom_pred_mode;
    574 
    575         /*--------------------------------------------------------------------*/
    576         /* Read the Coded block pattern                                       */
    577         /*--------------------------------------------------------------------*/
    578         u1_cbp = ih264d_parse_ctx_cbp_cabac(ps_dec);
    579         COPYTHECONTEXT("coded_block_pattern", u1_cbp);
    580         ps_cur_mb_info->u1_cbp = u1_cbp;
    581         p_curr_ctxt->u1_cbp = u1_cbp;
    582 
    583         /*--------------------------------------------------------------------*/
    584         /* Read mb_qp_delta                                                   */
    585         /*--------------------------------------------------------------------*/
    586         if(ps_cur_mb_info->u1_cbp)
    587         {
    588             ret = ih264d_parse_mb_qp_delta_cabac(ps_dec, &i1_delta_qp);
    589             if(ret != OK)
    590                 return ret;
    591             COPYTHECONTEXT("mb_qp_delta", i1_delta_qp);
    592             if(i1_delta_qp != 0)
    593             {
    594                 ret = ih264d_update_qp(ps_dec, i1_delta_qp);
    595                 if(ret != OK)
    596                     return ret;
    597             }
    598         }
    599         else
    600             ps_dec->i1_prev_mb_qp_delta = 0;
    601         p_curr_ctxt->u1_yuv_dc_csbp &= 0xFE;
    602     }
    603     else
    604     {
    605         u1_offset = 1;
    606         ps_cur_mb_info->ps_curmb->u1_mb_type = I_16x16_MB;
    607         p_curr_ctxt->u1_mb_type = CAB_I16x16;
    608         ps_cur_mb_info->u1_tran_form8x8 = 0;
    609         p_curr_ctxt->u1_transform8x8_ctxt = 0;
    610         ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
    611         /*--------------------------------------------------------------------*/
    612         /* Read the IntraPrediction mode for CHROMA                           */
    613         /*--------------------------------------------------------------------*/
    614         u1_intra_chrom_pred_mode = ih264d_parse_chroma_pred_mode_cabac(ps_dec);
    615         if(u1_intra_chrom_pred_mode > 3)
    616             return ERROR_CHROMA_PRED_MODE;
    617 
    618         COPYTHECONTEXT("Chroma intra_chroma_pred_mode pred mode", u1_intra_chrom_pred_mode);
    619         p_curr_ctxt->u1_intra_chroma_pred_mode = ps_cur_mb_info->u1_chroma_pred_mode =
    620                         u1_intra_chrom_pred_mode;
    621 
    622         /*--------------------------------------------------------------------*/
    623         /* Read the Coded block pattern                                       */
    624         /*--------------------------------------------------------------------*/
    625         u1_cbp = gau1_ih264d_cbp_tab[(u1_mb_type - 1) >> 2];
    626         ps_cur_mb_info->u1_cbp = u1_cbp;
    627         p_curr_ctxt->u1_cbp = u1_cbp;
    628 
    629         /*--------------------------------------------------------------------*/
    630         /* Read mb_qp_delta                                                   */
    631         /*--------------------------------------------------------------------*/
    632         ret = ih264d_parse_mb_qp_delta_cabac(ps_dec, &i1_delta_qp);
    633         if(ret != OK)
    634             return ret;
    635         COPYTHECONTEXT("mb_qp_delta", i1_delta_qp);
    636         if(i1_delta_qp != 0)
    637         {
    638             ret = ih264d_update_qp(ps_dec, i1_delta_qp);
    639             if(ret != OK)
    640                 return ret;
    641         }
    642 
    643         {
    644             WORD16 i_scaleFactor;
    645             WORD16* pi2_scale_matrix_ptr;
    646             /*******************************************************************/
    647             /* for luma DC coefficients the scaling is done during the parsing */
    648             /* to preserve the precision                                       */
    649             /*******************************************************************/
    650             if(ps_dec->s_high_profile.u1_scaling_present)
    651             {
    652                 pi2_scale_matrix_ptr =
    653                                 ps_dec->s_high_profile.i2_scalinglist4x4[0];
    654 
    655             }
    656             else
    657             {
    658                 i_scaleFactor = 16;
    659                 pi2_scale_matrix_ptr = &i_scaleFactor;
    660             }
    661             {
    662                 ctxt_inc_mb_info_t *ps_top_ctxt = ps_dec->p_top_ctxt_mb_info;
    663                 UWORD8 uc_a, uc_b;
    664                 UWORD32 u4_ctx_inc;
    665 
    666                 INC_SYM_COUNT(&(ps_dec->s_cab_dec_env));
    667 
    668                 /* if MbAddrN not available then CondTermN = 1 */
    669                 uc_b = ((ps_top_ctxt->u1_yuv_dc_csbp) & 0x01);
    670 
    671                 /* if MbAddrN not available then CondTermN = 1 */
    672                 uc_a = ((ps_dec->pu1_left_yuv_dc_csbp[0]) & 0x01);
    673 
    674                 u4_ctx_inc = (uc_a + (uc_b << 1));
    675 
    676                 {
    677                     WORD16 pi2_dc_coef[16];
    678                     tu_sblk4x4_coeff_data_t *ps_tu_4x4 =
    679                                     (tu_sblk4x4_coeff_data_t *)ps_dec->pv_parse_tu_coeff_data;
    680                     WORD16 *pi2_coeff_block =
    681                                     (WORD16 *)ps_dec->pv_parse_tu_coeff_data;
    682 
    683                     p_bin_ctxt = (ps_dec->p_cbf_t[LUMA_DC_CTXCAT]) + u4_ctx_inc;
    684 
    685                     u1_dc_block_flag =
    686                                     ih264d_read_coeff4x4_cabac(ps_bitstrm,
    687                                                     LUMA_DC_CTXCAT,
    688                                                     ps_dec->p_significant_coeff_flag_t[LUMA_DC_CTXCAT],
    689                                                     ps_dec, p_bin_ctxt);
    690 
    691                     /* Store coded_block_flag */
    692                     p_curr_ctxt->u1_yuv_dc_csbp &= 0xFE;
    693                     p_curr_ctxt->u1_yuv_dc_csbp |= u1_dc_block_flag;
    694                     if(u1_dc_block_flag)
    695                     {
    696                         WORD32 pi4_tmp[16];
    697                         memset(pi2_dc_coef,0,sizeof(pi2_dc_coef));
    698                         ih264d_unpack_coeff4x4_dc_4x4blk(ps_tu_4x4,
    699                                                          pi2_dc_coef,
    700                                                          ps_dec->pu1_inv_scan);
    701 
    702                         PROFILE_DISABLE_IQ_IT_RECON()
    703                         ps_dec->pf_ihadamard_scaling_4x4(pi2_dc_coef,
    704                                                          pi2_coeff_block,
    705                                                          ps_dec->pu2_quant_scale_y,
    706                                                          (UWORD16 *)pi2_scale_matrix_ptr,
    707                                                          ps_dec->u1_qp_y_div6,
    708                                                          pi4_tmp);
    709                         pi2_coeff_block += 16;
    710                         ps_dec->pv_parse_tu_coeff_data = (void *)pi2_coeff_block;
    711                         SET_BIT(ps_cur_mb_info->u1_yuv_dc_block_flag,0);
    712                     }
    713 
    714                 }
    715 
    716             }
    717         }
    718     }
    719 
    720     ps_dec->pu1_left_yuv_dc_csbp[0] &= 0x6;
    721     ps_dec->pu1_left_yuv_dc_csbp[0] |= u1_dc_block_flag;
    722 
    723     ih264d_parse_residual4x4_cabac(ps_dec, ps_cur_mb_info, u1_offset);
    724     if(EXCEED_OFFSET(ps_bitstrm))
    725         return ERROR_EOB_TERMINATE_T;
    726     return OK;
    727 }
    728 
    729 /*****************************************************************************/
    730 /*                                                                           */
    731 /*  Function Name : ih264d_parse_islice_data_cavlc                                  */
    732 /*                                                                           */
    733 /*  Description   : This function parses cabac syntax of a inter slice on    */
    734 /*                  N MB basis.                                              */
    735 /*                                                                           */
    736 /*  Inputs        : ps_dec                                                   */
    737 /*                  sliceparams                                              */
    738 /*                  firstMbInSlice                                           */
    739 /*                                                                           */
    740 /*  Processing    : 1. After parsing syntax for N MBs those N MBs are        */
    741 /*                     decoded till the end of slice.                        */
    742 /*                                                                           */
    743 /*  Returns       : 0                                                        */
    744 /*                                                                           */
    745 /*  Issues        : <List any issues or problems with this function>         */
    746 /*                                                                           */
    747 /*  Revision History:                                                        */
    748 /*                                                                           */
    749 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    750 /*         24 06 2005   ARNY            Draft                                */
    751 /*                                                                           */
    752 /*****************************************************************************/
    753 WORD32 ih264d_parse_islice_data_cavlc(dec_struct_t * ps_dec,
    754                                       dec_slice_params_t * ps_slice,
    755                                       UWORD16 u2_first_mb_in_slice)
    756 {
    757     UWORD8 uc_more_data_flag;
    758     UWORD8 u1_num_mbs, u1_mb_idx;
    759     dec_mb_info_t *ps_cur_mb_info;
    760     deblk_mb_t *ps_cur_deblk_mb;
    761     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
    762     UWORD32 *pu4_bitstrm_ofst = &ps_bitstrm->u4_ofst;
    763     UWORD32 *pu4_bitstrm_buf = ps_bitstrm->pu4_buffer;
    764     UWORD16 i2_pic_wdin_mbs = ps_dec->u2_frm_wd_in_mbs;
    765     WORD16 i2_cur_mb_addr;
    766     UWORD8 u1_mbaff;
    767     UWORD8 u1_num_mbs_next, u1_end_of_row, u1_tfr_n_mb;
    768     WORD32 ret = OK;
    769 
    770     ps_dec->u1_qp = ps_slice->u1_slice_qp;
    771     ih264d_update_qp(ps_dec, 0);
    772     u1_mbaff = ps_slice->u1_mbaff_frame_flag;
    773 
    774     /* initializations */
    775     u1_mb_idx = ps_dec->u1_mb_idx;
    776     u1_num_mbs = u1_mb_idx;
    777 
    778     uc_more_data_flag = 1;
    779     i2_cur_mb_addr = u2_first_mb_in_slice << u1_mbaff;
    780 
    781     do
    782     {
    783         UWORD8 u1_mb_type;
    784 
    785         ps_dec->pv_prev_mb_parse_tu_coeff_data = ps_dec->pv_parse_tu_coeff_data;
    786 
    787         if(i2_cur_mb_addr > ps_dec->ps_cur_sps->u2_max_mb_addr)
    788         {
    789             break;
    790         }
    791 
    792         ps_cur_mb_info = ps_dec->ps_nmb_info + u1_num_mbs;
    793         ps_dec->u4_num_mbs_cur_nmb = u1_num_mbs;
    794         ps_dec->u4_num_pmbair = (u1_num_mbs >> u1_mbaff);
    795 
    796         ps_cur_mb_info->u1_end_of_slice = 0;
    797 
    798         /***************************************************************/
    799         /* Get the required information for decoding of MB             */
    800         /* mb_x, mb_y , neighbour availablity,                         */
    801         /***************************************************************/
    802         ps_dec->pf_get_mb_info(ps_dec, i2_cur_mb_addr, ps_cur_mb_info, 0);
    803 
    804         /***************************************************************/
    805         /* Set the deblocking parameters for this MB                   */
    806         /***************************************************************/
    807         ps_cur_deblk_mb = ps_dec->ps_deblk_mbn + u1_num_mbs;
    808 
    809         if(ps_dec->u4_app_disable_deblk_frm == 0)
    810             ih264d_set_deblocking_parameters(ps_cur_deblk_mb, ps_slice,
    811                                              ps_dec->u1_mb_ngbr_availablity,
    812                                              ps_dec->u1_cur_mb_fld_dec_flag);
    813 
    814         ps_cur_deblk_mb->u1_mb_type = ps_cur_deblk_mb->u1_mb_type | D_INTRA_MB;
    815 
    816         /**************************************************************/
    817         /* Macroblock Layer Begins, Decode the u1_mb_type                */
    818         /**************************************************************/
    819 //Inlined ih264d_uev
    820         {
    821             UWORD32 u4_bitstream_offset = *pu4_bitstrm_ofst;
    822             UWORD32 u4_word, u4_ldz, u4_temp;
    823 
    824             /***************************************************************/
    825             /* Find leading zeros in next 32 bits                          */
    826             /***************************************************************/
    827             NEXTBITS_32(u4_word, u4_bitstream_offset, pu4_bitstrm_buf);
    828             u4_ldz = CLZ(u4_word);
    829             /* Flush the ps_bitstrm */
    830             u4_bitstream_offset += (u4_ldz + 1);
    831             /* Read the suffix from the ps_bitstrm */
    832             u4_word = 0;
    833             if(u4_ldz)
    834                 GETBITS(u4_word, u4_bitstream_offset, pu4_bitstrm_buf,
    835                         u4_ldz);
    836             *pu4_bitstrm_ofst = u4_bitstream_offset;
    837             u4_temp = ((1 << u4_ldz) + u4_word - 1);
    838             if(u4_temp > 25)
    839                 return ERROR_MB_TYPE;
    840             u1_mb_type = u4_temp;
    841 
    842         }
    843 //Inlined ih264d_uev
    844         ps_cur_mb_info->u1_mb_type = u1_mb_type;
    845         COPYTHECONTEXT("u1_mb_type", u1_mb_type);
    846 
    847         /**************************************************************/
    848         /* Parse Macroblock data                                      */
    849         /**************************************************************/
    850         if(25 == u1_mb_type)
    851         {
    852             /* I_PCM_MB */
    853             ps_cur_mb_info->ps_curmb->u1_mb_type = I_PCM_MB;
    854             ret = ih264d_parse_ipcm_mb(ps_dec, ps_cur_mb_info, u1_num_mbs);
    855             if(ret != OK)
    856                 return ret;
    857             ps_cur_deblk_mb->u1_mb_qp = 0;
    858         }
    859         else
    860         {
    861             ret = ih264d_parse_imb_cavlc(ps_dec, ps_cur_mb_info, u1_num_mbs, u1_mb_type);
    862             if(ret != OK)
    863                 return ret;
    864             ps_cur_deblk_mb->u1_mb_qp = ps_dec->u1_qp;
    865         }
    866 
    867         uc_more_data_flag = MORE_RBSP_DATA(ps_bitstrm);
    868 
    869         if(u1_mbaff)
    870         {
    871             ih264d_update_mbaff_left_nnz(ps_dec, ps_cur_mb_info);
    872             if(!uc_more_data_flag && (0 == (i2_cur_mb_addr & 1)))
    873             {
    874                 return ERROR_EOB_FLUSHBITS_T;
    875             }
    876         }
    877         /**************************************************************/
    878         /* Get next Macroblock address                                */
    879         /**************************************************************/
    880 
    881         i2_cur_mb_addr++;
    882 
    883 
    884         /* Store the colocated information */
    885         {
    886             mv_pred_t *ps_mv_nmb_start = ps_dec->ps_mv_cur + (u1_num_mbs << 4);
    887 
    888             mv_pred_t s_mvPred =
    889                 {
    890                     { 0, 0, 0, 0 },
    891                       { -1, -1 }, 0, 0};
    892             ih264d_rep_mv_colz(ps_dec, &s_mvPred, ps_mv_nmb_start, 0,
    893                                (UWORD8)(ps_dec->u1_cur_mb_fld_dec_flag << 1), 4,
    894                                4);
    895         }
    896 
    897         /*if num _cores is set to 3,compute bs will be done in another thread*/
    898         if(ps_dec->u4_num_cores < 3)
    899         {
    900             if(ps_dec->u4_app_disable_deblk_frm == 0)
    901                 ps_dec->pf_compute_bs(ps_dec, ps_cur_mb_info,
    902                                      (UWORD16)(u1_num_mbs >> u1_mbaff));
    903         }
    904         u1_num_mbs++;
    905 
    906         /****************************************************************/
    907         /* Check for End Of Row                                         */
    908         /****************************************************************/
    909         u1_num_mbs_next = i2_pic_wdin_mbs - ps_dec->u2_mbx - 1;
    910         u1_end_of_row = (!u1_num_mbs_next) && (!(u1_mbaff && (u1_num_mbs & 0x01)));
    911         u1_tfr_n_mb = (u1_num_mbs == ps_dec->u1_recon_mb_grp) || u1_end_of_row
    912                         || (!uc_more_data_flag);
    913         ps_cur_mb_info->u1_end_of_slice = (!uc_more_data_flag);
    914 
    915         /*H264_DEC_DEBUG_PRINT("Pic: %d Mb_X=%d Mb_Y=%d",
    916          ps_slice->i4_poc >> ps_slice->u1_field_pic_flag,
    917          ps_dec->u2_mbx,ps_dec->u2_mby + (1 - ps_cur_mb_info->u1_topmb));
    918          H264_DEC_DEBUG_PRINT("u1_tfr_n_mb || (!uc_more_data_flag): %d", u1_tfr_n_mb || (!uc_more_data_flag));*/
    919         if(u1_tfr_n_mb || (!uc_more_data_flag))
    920         {
    921 
    922             if(ps_dec->u1_separate_parse)
    923             {
    924                 ih264d_parse_tfr_nmb(ps_dec, u1_mb_idx, u1_num_mbs,
    925                                      u1_num_mbs_next, u1_tfr_n_mb, u1_end_of_row);
    926                 ps_dec->ps_nmb_info +=  u1_num_mbs;
    927             }
    928             else
    929             {
    930                 ih264d_decode_recon_tfr_nmb(ps_dec, u1_mb_idx, u1_num_mbs,
    931                                             u1_num_mbs_next, u1_tfr_n_mb,
    932                                             u1_end_of_row);
    933             }
    934             ps_dec->u2_total_mbs_coded += u1_num_mbs;
    935             if(u1_tfr_n_mb)
    936                 u1_num_mbs = 0;
    937             u1_mb_idx = u1_num_mbs;
    938             ps_dec->u1_mb_idx = u1_num_mbs;
    939 
    940         }
    941     }
    942     while(uc_more_data_flag);
    943 
    944     ps_dec->u4_num_mbs_cur_nmb = 0;
    945     ps_dec->ps_cur_slice->u4_mbs_in_slice = i2_cur_mb_addr
    946 
    947                         - (u2_first_mb_in_slice << u1_mbaff);
    948 
    949     return ret;
    950 }
    951 
    952 /*****************************************************************************/
    953 /*                                                                           */
    954 /*  Function Name : ih264d_parse_islice_data_cabac                                  */
    955 /*                                                                           */
    956 /*  Description   : This function parses cabac syntax of a inter slice on    */
    957 /*                  N MB basis.                                              */
    958 /*                                                                           */
    959 /*  Inputs        : ps_dec                                                   */
    960 /*                  sliceparams                                              */
    961 /*                  firstMbInSlice                                           */
    962 /*                                                                           */
    963 /*  Processing    : 1. After parsing syntax for N MBs those N MBs are        */
    964 /*                     decoded till the end of slice.                        */
    965 /*                                                                           */
    966 /*  Returns       : 0                                                        */
    967 /*                                                                           */
    968 /*  Issues        : <List any issues or problems with this function>         */
    969 /*                                                                           */
    970 /*  Revision History:                                                        */
    971 /*                                                                           */
    972 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
    973 /*         24 06 2005   ARNY            Draft                                */
    974 /*                                                                           */
    975 /*****************************************************************************/
    976 WORD32 ih264d_parse_islice_data_cabac(dec_struct_t * ps_dec,
    977                                       dec_slice_params_t * ps_slice,
    978                                       UWORD16 u2_first_mb_in_slice)
    979 {
    980     UWORD8 uc_more_data_flag;
    981     UWORD8 u1_num_mbs, u1_mb_idx;
    982     dec_mb_info_t *ps_cur_mb_info;
    983     deblk_mb_t *ps_cur_deblk_mb;
    984 
    985     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
    986     UWORD16 i2_pic_wdin_mbs = ps_dec->u2_frm_wd_in_mbs;
    987     WORD16 i2_cur_mb_addr;
    988     UWORD8 u1_mbaff;
    989     UWORD8 u1_num_mbs_next, u1_end_of_row, u1_tfr_n_mb;
    990     WORD32 ret = OK;
    991 
    992     ps_dec->u1_qp = ps_slice->u1_slice_qp;
    993     ih264d_update_qp(ps_dec, 0);
    994     u1_mbaff = ps_slice->u1_mbaff_frame_flag;
    995 
    996     if(ps_bitstrm->u4_ofst & 0x07)
    997     {
    998         ps_bitstrm->u4_ofst += 8;
    999         ps_bitstrm->u4_ofst &= 0xFFFFFFF8;
   1000     }
   1001     ret = ih264d_init_cabac_dec_envirnoment(&(ps_dec->s_cab_dec_env), ps_bitstrm);
   1002     if(ret != OK)
   1003         return ret;
   1004     ih264d_init_cabac_contexts(I_SLICE, ps_dec);
   1005 
   1006     ps_dec->i1_prev_mb_qp_delta = 0;
   1007 
   1008     /* initializations */
   1009     u1_mb_idx = ps_dec->u1_mb_idx;
   1010     u1_num_mbs = u1_mb_idx;
   1011 
   1012     uc_more_data_flag = 1;
   1013     i2_cur_mb_addr = u2_first_mb_in_slice << u1_mbaff;
   1014     do
   1015     {
   1016         UWORD16 u2_mbx;
   1017 
   1018         ps_dec->pv_prev_mb_parse_tu_coeff_data = ps_dec->pv_parse_tu_coeff_data;
   1019 
   1020         if(i2_cur_mb_addr > ps_dec->ps_cur_sps->u2_max_mb_addr)
   1021         {
   1022             break;
   1023         }
   1024 
   1025         {
   1026             UWORD8 u1_mb_type;
   1027 
   1028             ps_cur_mb_info = ps_dec->ps_nmb_info + u1_num_mbs;
   1029             ps_dec->u4_num_mbs_cur_nmb = u1_num_mbs;
   1030             ps_dec->u4_num_pmbair = (u1_num_mbs >> u1_mbaff);
   1031 
   1032             ps_cur_mb_info->u1_end_of_slice = 0;
   1033 
   1034             /***************************************************************/
   1035             /* Get the required information for decoding of MB                  */
   1036             /* mb_x, mb_y , neighbour availablity,                              */
   1037             /***************************************************************/
   1038             ps_dec->pf_get_mb_info(ps_dec, i2_cur_mb_addr, ps_cur_mb_info, 0);
   1039             u2_mbx = ps_dec->u2_mbx;
   1040 
   1041             /*********************************************************************/
   1042             /* initialize u1_tran_form8x8 to zero to aviod uninitialized accesses */
   1043             /*********************************************************************/
   1044             ps_cur_mb_info->u1_tran_form8x8 = 0;
   1045             ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
   1046 
   1047             /***************************************************************/
   1048             /* Set the deblocking parameters for this MB                   */
   1049             /***************************************************************/
   1050             ps_cur_deblk_mb = ps_dec->ps_deblk_mbn + u1_num_mbs;
   1051             if(ps_dec->u4_app_disable_deblk_frm == 0)
   1052                 ih264d_set_deblocking_parameters(
   1053                                 ps_cur_deblk_mb, ps_slice,
   1054                                 ps_dec->u1_mb_ngbr_availablity,
   1055                                 ps_dec->u1_cur_mb_fld_dec_flag);
   1056 
   1057             ps_cur_deblk_mb->u1_mb_type = ps_cur_deblk_mb->u1_mb_type
   1058                             | D_INTRA_MB;
   1059 
   1060             /* Macroblock Layer Begins */
   1061             /* Decode the u1_mb_type */
   1062             u1_mb_type = ih264d_parse_mb_type_intra_cabac(0, ps_dec);
   1063             if(u1_mb_type > 25)
   1064                 return ERROR_MB_TYPE;
   1065             ps_cur_mb_info->u1_mb_type = u1_mb_type;
   1066             COPYTHECONTEXT("u1_mb_type", u1_mb_type);
   1067 
   1068             /* Parse Macroblock Data */
   1069             if(25 == u1_mb_type)
   1070             {
   1071                 /* I_PCM_MB */
   1072                 ps_cur_mb_info->ps_curmb->u1_mb_type = I_PCM_MB;
   1073                 ret = ih264d_parse_ipcm_mb(ps_dec, ps_cur_mb_info, u1_num_mbs);
   1074                 if(ret != OK)
   1075                     return ret;
   1076                 ps_cur_deblk_mb->u1_mb_qp = 0;
   1077             }
   1078             else
   1079             {
   1080                 ret = ih264d_parse_imb_cabac(ps_dec, ps_cur_mb_info, u1_mb_type);
   1081                 if(ret != OK)
   1082                     return ret;
   1083                 ps_cur_deblk_mb->u1_mb_qp = ps_dec->u1_qp;
   1084             }
   1085 
   1086             if(u1_mbaff)
   1087             {
   1088                 ih264d_update_mbaff_left_nnz(ps_dec, ps_cur_mb_info);
   1089             }
   1090 
   1091 
   1092             if(ps_cur_mb_info->u1_topmb && u1_mbaff)
   1093                 uc_more_data_flag = 1;
   1094             else
   1095             {
   1096                 uc_more_data_flag = ih264d_decode_terminate(&ps_dec->s_cab_dec_env,
   1097                                                           ps_bitstrm);
   1098                 uc_more_data_flag = !uc_more_data_flag;
   1099                 COPYTHECONTEXT("Decode Sliceterm",!uc_more_data_flag);
   1100             }
   1101 
   1102             if(u1_mbaff)
   1103             {
   1104                 if(!uc_more_data_flag && (0 == (i2_cur_mb_addr & 1)))
   1105                 {
   1106                     return ERROR_EOB_FLUSHBITS_T;
   1107                 }
   1108             }
   1109             /* Next macroblock information */
   1110             i2_cur_mb_addr++;
   1111             /* Store the colocated information */
   1112             {
   1113 
   1114                 mv_pred_t *ps_mv_nmb_start = ps_dec->ps_mv_cur + (u1_num_mbs << 4);
   1115                 mv_pred_t s_mvPred =
   1116                     {
   1117                         { 0, 0, 0, 0 },
   1118                           { -1, -1 }, 0, 0};
   1119                 ih264d_rep_mv_colz(
   1120                                 ps_dec, &s_mvPred, ps_mv_nmb_start, 0,
   1121                                 (UWORD8)(ps_dec->u1_cur_mb_fld_dec_flag << 1),
   1122                                 4, 4);
   1123             }
   1124             /*if num _cores is set to 3,compute bs will be done in another thread*/
   1125             if(ps_dec->u4_num_cores < 3)
   1126             {
   1127                 if(ps_dec->u4_app_disable_deblk_frm == 0)
   1128                     ps_dec->pf_compute_bs(ps_dec, ps_cur_mb_info,
   1129                                          (UWORD16)(u1_num_mbs >> u1_mbaff));
   1130             }
   1131             u1_num_mbs++;
   1132 
   1133         }
   1134 
   1135         /****************************************************************/
   1136         /* Check for End Of Row                                         */
   1137         /****************************************************************/
   1138         u1_num_mbs_next = i2_pic_wdin_mbs - u2_mbx - 1;
   1139         u1_end_of_row = (!u1_num_mbs_next) && (!(u1_mbaff && (u1_num_mbs & 0x01)));
   1140         u1_tfr_n_mb = (u1_num_mbs == ps_dec->u1_recon_mb_grp) || u1_end_of_row
   1141                         || (!uc_more_data_flag);
   1142         ps_cur_mb_info->u1_end_of_slice = (!uc_more_data_flag);
   1143 
   1144         if(u1_tfr_n_mb || (!uc_more_data_flag))
   1145         {
   1146 
   1147 
   1148             if(ps_dec->u1_separate_parse)
   1149             {
   1150                 ih264d_parse_tfr_nmb(ps_dec, u1_mb_idx, u1_num_mbs,
   1151                                      u1_num_mbs_next, u1_tfr_n_mb, u1_end_of_row);
   1152                 ps_dec->ps_nmb_info +=  u1_num_mbs;
   1153             }
   1154             else
   1155             {
   1156                 ih264d_decode_recon_tfr_nmb(ps_dec, u1_mb_idx, u1_num_mbs,
   1157                                             u1_num_mbs_next, u1_tfr_n_mb,
   1158                                             u1_end_of_row);
   1159             }
   1160             ps_dec->u2_total_mbs_coded += u1_num_mbs;
   1161             if(u1_tfr_n_mb)
   1162                 u1_num_mbs = 0;
   1163             u1_mb_idx = u1_num_mbs;
   1164             ps_dec->u1_mb_idx = u1_num_mbs;
   1165 
   1166         }
   1167     }
   1168     while(uc_more_data_flag);
   1169 
   1170     ps_dec->u4_num_mbs_cur_nmb = 0;
   1171     ps_dec->ps_cur_slice->u4_mbs_in_slice = i2_cur_mb_addr
   1172 
   1173                         - (u2_first_mb_in_slice << u1_mbaff);
   1174 
   1175     return ret;
   1176 }
   1177 
   1178 /*****************************************************************************/
   1179 /*                                                                           */
   1180 /*  Function Name : ih264d_parse_ipcm_mb                                       */
   1181 /*                                                                           */
   1182 /*  Description   : This function decodes the pixel values of I_PCM Mb.      */
   1183 /*                                                                           */
   1184 /*  Inputs        : ps_dec,  ps_cur_mb_info and mb number                          */
   1185 /*                                                                           */
   1186 /*  Description   : This function reads the luma and chroma pixels directly  */
   1187 /*                  from the bitstream when the mbtype is I_PCM and stores   */
   1188 /*                  them in recon buffer. If the entropy coding mode is      */
   1189 /*                  cabac, decoding engine is re-initialized. The nnzs and   */
   1190 /*                  cabac contexts are appropriately modified.               */
   1191 /*  Returns       : void                                                     */
   1192 /*                                                                           */
   1193 /*  Revision History:                                                        */
   1194 /*         DD MM YYYY   Author(s)       Changes (Describe the changes made)  */
   1195 /*         13 07 2002   Jay                                                  */
   1196 /*                                                                           */
   1197 /*****************************************************************************/
   1198 
   1199 WORD32 ih264d_parse_ipcm_mb(dec_struct_t * ps_dec,
   1200                           dec_mb_info_t *ps_cur_mb_info,
   1201                           UWORD8 u1_mbNum)
   1202 {
   1203     dec_bit_stream_t * const ps_bitstrm = ps_dec->ps_bitstrm;
   1204     UWORD8 u1_mbaff = ps_dec->ps_cur_slice->u1_mbaff_frame_flag;
   1205     UWORD8 *pu1_y, *pu1_u, *pu1_v;
   1206     WORD32 ret;
   1207 
   1208     UWORD32 u4_rec_width_y, u4_rec_width_uv;
   1209     UWORD32 u1_num_mb_pair;
   1210     UWORD8 u1_x, u1_y;
   1211     /* CHANGED CODE */
   1212     tfr_ctxt_t *ps_frame_buf;
   1213     UWORD8 u1_mb_field_decoding_flag;
   1214     UWORD32 *pu4_buf;
   1215     UWORD8 *pu1_buf;
   1216     /* CHANGED CODE */
   1217 
   1218     if(ps_dec->u1_separate_parse)
   1219     {
   1220         ps_frame_buf = &ps_dec->s_tran_addrecon_parse;
   1221     }
   1222     else
   1223     {
   1224         ps_frame_buf = &ps_dec->s_tran_addrecon;
   1225     }
   1226     /* align bistream to byte boundary. */
   1227     /* pcm_alignment_zero_bit discarded */
   1228     /* For XX GotoByteBoundary */
   1229     if(ps_bitstrm->u4_ofst & 0x07)
   1230     {
   1231         ps_bitstrm->u4_ofst += 8;
   1232         ps_bitstrm->u4_ofst &= 0xFFFFFFF8;
   1233     }
   1234 
   1235     /*  Store left Nnz as 16 for each 4x4 blk */
   1236 
   1237     pu1_buf = ps_dec->pu1_left_nnz_y;
   1238     pu4_buf = (UWORD32 *)pu1_buf;
   1239     *pu4_buf = 0x10101010;
   1240     pu1_buf = ps_cur_mb_info->ps_curmb->pu1_nnz_y;
   1241     pu4_buf = (UWORD32 *)pu1_buf;
   1242     *pu4_buf = 0x10101010;
   1243     pu1_buf = ps_cur_mb_info->ps_curmb->pu1_nnz_uv;
   1244     pu4_buf = (UWORD32 *)pu1_buf;
   1245     *pu4_buf = 0x10101010;
   1246     pu1_buf = ps_dec->pu1_left_nnz_uv;
   1247     pu4_buf = (UWORD32 *)pu1_buf;
   1248     *pu4_buf = 0x10101010;
   1249     ps_cur_mb_info->u1_cbp = 0xff;
   1250 
   1251     ps_dec->i1_prev_mb_qp_delta = 0;
   1252     /* Get neighbour MB's */
   1253     u1_num_mb_pair = (u1_mbNum >> u1_mbaff);
   1254 
   1255     /*****************************************************************************/
   1256     /* calculate the RECON buffer YUV pointers for the PCM data                  */
   1257     /*****************************************************************************/
   1258     /* CHANGED CODE  */
   1259     u1_mb_field_decoding_flag = ps_cur_mb_info->u1_mb_field_decodingflag;
   1260     pu1_y = ps_frame_buf->pu1_dest_y + (u1_num_mb_pair << 4);
   1261     pu1_u = ps_frame_buf->pu1_dest_u + (u1_num_mb_pair << 4);
   1262     pu1_v = pu1_u + 1;
   1263 
   1264     u4_rec_width_y = ps_dec->u2_frm_wd_y << u1_mb_field_decoding_flag;
   1265     u4_rec_width_uv = ps_dec->u2_frm_wd_uv << u1_mb_field_decoding_flag;
   1266     /* CHANGED CODE  */
   1267 
   1268     if(u1_mbaff)
   1269     {
   1270         UWORD8 u1_top_mb;
   1271 
   1272         u1_top_mb = ps_cur_mb_info->u1_topmb;
   1273 
   1274         if(u1_top_mb == 0)
   1275         {
   1276             pu1_y += (u1_mb_field_decoding_flag ?
   1277                             (u4_rec_width_y >> 1) : (u4_rec_width_y << 4));
   1278             pu1_u += (u1_mb_field_decoding_flag ?
   1279                             (u4_rec_width_uv) : (u4_rec_width_uv << 4));
   1280             pu1_v = pu1_u + 1;
   1281         }
   1282     }
   1283 
   1284     /* Read Luma samples */
   1285     for(u1_y = 0; u1_y < 16; u1_y++)
   1286     {
   1287         for(u1_x = 0; u1_x < 16; u1_x++)
   1288             pu1_y[u1_x] = ih264d_get_bits_h264(ps_bitstrm, 8);
   1289 
   1290         pu1_y += u4_rec_width_y;
   1291     }
   1292 
   1293     /* Read Chroma samples */
   1294     for(u1_y = 0; u1_y < 8; u1_y++)
   1295     {
   1296         for(u1_x = 0; u1_x < 8; u1_x++)
   1297             pu1_u[u1_x * YUV420SP_FACTOR] = ih264d_get_bits_h264(ps_bitstrm, 8);
   1298 
   1299         pu1_u += u4_rec_width_uv;
   1300     }
   1301 
   1302     for(u1_y = 0; u1_y < 8; u1_y++)
   1303     {
   1304         for(u1_x = 0; u1_x < 8; u1_x++)
   1305             pu1_v[u1_x * YUV420SP_FACTOR] = ih264d_get_bits_h264(ps_bitstrm, 8);
   1306 
   1307         pu1_v += u4_rec_width_uv;
   1308     }
   1309 
   1310     if(CABAC == ps_dec->ps_cur_pps->u1_entropy_coding_mode)
   1311     {
   1312         UWORD32 *pu4_buf;
   1313         UWORD8 *pu1_buf;
   1314         ctxt_inc_mb_info_t *p_curr_ctxt = ps_dec->ps_curr_ctxt_mb_info;
   1315         /* Re-initialize the cabac decoding engine. */
   1316         ret = ih264d_init_cabac_dec_envirnoment(&(ps_dec->s_cab_dec_env), ps_bitstrm);
   1317         if(ret != OK)
   1318             return ret;
   1319         /* update the cabac contetxs */
   1320         p_curr_ctxt->u1_mb_type = CAB_I_PCM;
   1321         p_curr_ctxt->u1_cbp = 47;
   1322         p_curr_ctxt->u1_intra_chroma_pred_mode = 0;
   1323         p_curr_ctxt->u1_transform8x8_ctxt = 0;
   1324         ps_cur_mb_info->ps_curmb->u1_tran_form8x8 = 0;
   1325 
   1326         pu1_buf = ps_dec->pu1_left_nnz_y;
   1327         pu4_buf = (UWORD32 *)pu1_buf;
   1328         *pu4_buf = 0x01010101;
   1329 
   1330         pu1_buf = ps_cur_mb_info->ps_curmb->pu1_nnz_y;
   1331         pu4_buf = (UWORD32 *)pu1_buf;
   1332         *pu4_buf = 0x01010101;
   1333 
   1334         pu1_buf = ps_cur_mb_info->ps_curmb->pu1_nnz_uv;
   1335         pu4_buf = (UWORD32 *)pu1_buf;
   1336         *pu4_buf = 0x01010101;
   1337 
   1338         pu1_buf = ps_dec->pu1_left_nnz_uv;
   1339         pu4_buf = (UWORD32 *)pu1_buf;
   1340         *pu4_buf = 0x01010101;
   1341 
   1342         p_curr_ctxt->u1_yuv_dc_csbp = 0x7;
   1343         ps_dec->pu1_left_yuv_dc_csbp[0] = 0x7;
   1344         if(ps_dec->ps_cur_slice->u1_slice_type != I_SLICE)
   1345         {
   1346 
   1347             MEMSET_16BYTES(&ps_dec->pu1_left_mv_ctxt_inc[0][0], 0);
   1348             memset(ps_dec->pi1_left_ref_idx_ctxt_inc, 0, 4);
   1349             MEMSET_16BYTES(p_curr_ctxt->u1_mv, 0);
   1350             memset(p_curr_ctxt->i1_ref_idx, 0, 4);
   1351 
   1352         }
   1353     }
   1354     return OK;
   1355 }
   1356 
   1357 /*!
   1358  **************************************************************************
   1359  * \if Function name : ih264d_decode_islice \endif
   1360  *
   1361  * \brief
   1362  *    Decodes an I Slice
   1363  *
   1364  *
   1365  * \return
   1366  *    0 on Success and Error code otherwise
   1367  **************************************************************************
   1368  */
   1369 WORD32 ih264d_parse_islice(dec_struct_t *ps_dec,
   1370                             UWORD16 u2_first_mb_in_slice)
   1371 {
   1372     dec_pic_params_t * ps_pps = ps_dec->ps_cur_pps;
   1373     dec_slice_params_t * ps_slice = ps_dec->ps_cur_slice;
   1374     UWORD32 *pu4_bitstrm_buf = ps_dec->ps_bitstrm->pu4_buffer;
   1375     UWORD32 *pu4_bitstrm_ofst = &ps_dec->ps_bitstrm->u4_ofst;
   1376     UWORD32 u4_temp;
   1377     WORD32 i_temp;
   1378     WORD32 ret;
   1379 
   1380     /*--------------------------------------------------------------------*/
   1381     /* Read remaining contents of the slice header                        */
   1382     /*--------------------------------------------------------------------*/
   1383     /* dec_ref_pic_marking function */
   1384     /* G050 */
   1385     if(ps_slice->u1_nal_ref_idc != 0)
   1386     {
   1387         if(!ps_dec->ps_dpb_cmds->u1_dpb_commands_read)
   1388         {
   1389             i_temp = ih264d_read_mmco_commands(ps_dec);
   1390             if (i_temp < 0)
   1391             {
   1392                 return ERROR_DBP_MANAGER_T;
   1393             }
   1394             ps_dec->u4_bitoffset = i_temp;
   1395         }
   1396         else
   1397             ps_dec->ps_bitstrm->u4_ofst += ps_dec->u4_bitoffset;
   1398     }
   1399     /* G050 */
   1400 
   1401     /* Read slice_qp_delta */
   1402     i_temp = ps_pps->u1_pic_init_qp
   1403                     + ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
   1404     if((i_temp < 0) || (i_temp > 51))
   1405         return ERROR_INV_RANGE_QP_T;
   1406     ps_slice->u1_slice_qp = i_temp;
   1407     COPYTHECONTEXT("SH: slice_qp_delta",
   1408                     ps_slice->u1_slice_qp - ps_pps->u1_pic_init_qp);
   1409 
   1410     if(ps_pps->u1_deblocking_filter_parameters_present_flag == 1)
   1411     {
   1412         u4_temp = ih264d_uev(pu4_bitstrm_ofst, pu4_bitstrm_buf);
   1413         COPYTHECONTEXT("SH: disable_deblocking_filter_idc", u4_temp);
   1414 
   1415         if(u4_temp > SLICE_BOUNDARY_DBLK_DISABLED)
   1416         {
   1417             return ERROR_INV_SLICE_HDR_T;
   1418         }
   1419         ps_slice->u1_disable_dblk_filter_idc = u4_temp;
   1420         if(u4_temp != 1)
   1421         {
   1422             i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf)
   1423                             << 1;
   1424             if((MIN_DBLK_FIL_OFF > i_temp) || (i_temp > MAX_DBLK_FIL_OFF))
   1425             {
   1426                 return ERROR_INV_SLICE_HDR_T;
   1427             }
   1428             ps_slice->i1_slice_alpha_c0_offset = i_temp;
   1429             COPYTHECONTEXT("SH: slice_alpha_c0_offset_div2",
   1430                             ps_slice->i1_slice_alpha_c0_offset >> 1);
   1431 
   1432             i_temp = ih264d_sev(pu4_bitstrm_ofst, pu4_bitstrm_buf)
   1433                             << 1;
   1434             if((MIN_DBLK_FIL_OFF > i_temp) || (i_temp > MAX_DBLK_FIL_OFF))
   1435             {
   1436                 return ERROR_INV_SLICE_HDR_T;
   1437             }
   1438             ps_slice->i1_slice_beta_offset = i_temp;
   1439             COPYTHECONTEXT("SH: slice_beta_offset_div2",
   1440                             ps_slice->i1_slice_beta_offset >> 1);
   1441 
   1442         }
   1443         else
   1444         {
   1445             ps_slice->i1_slice_alpha_c0_offset = 0;
   1446             ps_slice->i1_slice_beta_offset = 0;
   1447         }
   1448     }
   1449     else
   1450     {
   1451         ps_slice->u1_disable_dblk_filter_idc = 0;
   1452         ps_slice->i1_slice_alpha_c0_offset = 0;
   1453         ps_slice->i1_slice_beta_offset = 0;
   1454     }
   1455 
   1456     /* Initialization to check if number of motion vector per 2 Mbs */
   1457     /* are exceeding the range or not */
   1458     ps_dec->u2_mv_2mb[0] = 0;
   1459     ps_dec->u2_mv_2mb[1] = 0;
   1460 
   1461 
   1462     /*set slice header cone to 2 ,to indicate  correct header*/
   1463     ps_dec->u1_slice_header_done = 2;
   1464 
   1465     if(ps_pps->u1_entropy_coding_mode)
   1466     {
   1467         SWITCHOFFTRACE; SWITCHONTRACECABAC;
   1468         if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag)
   1469         {
   1470             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cabac_mbaff;
   1471         }
   1472         else
   1473             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cabac_nonmbaff;
   1474 
   1475         ret = ih264d_parse_islice_data_cabac(ps_dec, ps_slice,
   1476                                              u2_first_mb_in_slice);
   1477         if(ret != OK)
   1478             return ret;
   1479         SWITCHONTRACE; SWITCHOFFTRACECABAC;
   1480     }
   1481     else
   1482     {
   1483         if(ps_dec->ps_cur_slice->u1_mbaff_frame_flag)
   1484         {
   1485             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cavlc_mbaff;
   1486         }
   1487         else
   1488             ps_dec->pf_get_mb_info = ih264d_get_mb_info_cavlc_nonmbaff;
   1489         ret = ih264d_parse_islice_data_cavlc(ps_dec, ps_slice,
   1490                                        u2_first_mb_in_slice);
   1491         if(ret != OK)
   1492             return ret;
   1493     }
   1494 
   1495     return OK;
   1496 }
   1497