Decoder: Fix stack underflow in CAVLC 4x4 parse functions

Bug: 26399350
Change-Id: Id768751672a7b093ab6e53d4fc0b3188d470920e

WORD32 ih264d_cavlc_parse_8x8block_both_available(WORD16 *pi2_coeff_block,
                                                  UWORD32 u4_sub_block_strd,
                                                  UWORD32 u4_isdc,
 dec_struct_t * ps_dec,
                                                  UWORD8 *pu1_top_nnz,
                                                  UWORD8 *pu1_left_nnz,
                                                  UWORD8 u1_tran_form8x8,
                                                  UWORD8 u1_mb_field_decodingflag,
                                                  UWORD32 *pu4_csbp)
{
    UWORD32 u4_num_coeff, u4_n, u4_subblock_coded;
    UWORD32 u4_top0, u4_top1;
    UWORD32 *pu4_dummy;
    WORD32 (**pf_cavlc_parse4x4coeff)(WORD16 *pi2_coeff_block,
                                      UWORD32 u4_isdc,
                                      WORD32 u4_n,
 struct _DecStruct *ps_dec,
                                      UWORD32 *pu4_dummy) =
                                      ps_dec->pf_cavlc_parse4x4coeff;
    UWORD32 u4_idx = 0;
    UWORD8 *puc_temp;
    WORD32 ret;

 *pu4_csbp = 0;
 /* need to change the inverse scan matrices here */
    puc_temp = ps_dec->pu1_inv_scan;

 /*------------------------------------------------------*/
 /* Residual 4x4 decoding: SubBlock 0                    */
 /*------------------------------------------------------*/
 if(u1_tran_form8x8)
 {
 if(!u1_mb_field_decodingflag)
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[0];
 }
 else
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[0];
 }
 }
    u4_n = (pu1_top_nnz[0] + pu1_left_nnz[0] + 1) >> 1;
    ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
                                             u4_n, ps_dec, &u4_num_coeff);
 if(ret != OK)
 return ret;

    u4_top0 = u4_num_coeff;
    u4_subblock_coded = (u4_num_coeff != 0);
    INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);

 /*------------------------------------------------------*/
 /* Residual 4x4 decoding: SubBlock 1                    */
 /*------------------------------------------------------*/
    u4_idx++;
 if(u1_tran_form8x8)
 {
 if(!u1_mb_field_decodingflag)
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[1];
 }
 else
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[1];
 }
 }
 else
 {
        pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
 }
    u4_n = (pu1_top_nnz[1] + u4_num_coeff + 1) >> 1;
    ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
                                             u4_n, ps_dec, &u4_num_coeff);
 if(ret != OK)
 return ret;

    u4_top1 = pu1_left_nnz[0] = u4_num_coeff;
    u4_subblock_coded = (u4_num_coeff != 0);
    INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);

 /*------------------------------------------------------*/
 /* Residual 4x4 decoding: SubBlock 2                    */
 /*------------------------------------------------------*/
    u4_idx += (u4_sub_block_strd - 1);
 if(u1_tran_form8x8)
 {
 if(!u1_mb_field_decodingflag)
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[2];
 }
 else
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[2];
 }
 }
 else
 {
        pi2_coeff_block += ((u4_sub_block_strd - 1) * NUM_COEFFS_IN_4x4BLK);
 }
    u4_n = (u4_top0 + pu1_left_nnz[1] + 1) >> 1;
    ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
                                             u4_n, ps_dec, &u4_num_coeff);
 if(ret != OK)
 return ret;

    pu1_top_nnz[0] = u4_num_coeff;
    u4_subblock_coded = (u4_num_coeff != 0);
    INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);

 /*------------------------------------------------------*/
 /* Residual 4x4 decoding: SubBlock 3                    */
 /*------------------------------------------------------*/
    u4_idx++;
 if(u1_tran_form8x8)
 {
 if(!u1_mb_field_decodingflag)
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[3];
 }
 else
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[3];
 }
 }
 else
 {
        pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
 }
    u4_n = (u4_top1 + u4_num_coeff + 1) >> 1;
    ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
                                             u4_n, ps_dec, &u4_num_coeff);
 if(ret != OK)
 return ret;

    pu1_top_nnz[1] = pu1_left_nnz[1] = u4_num_coeff;
    u4_subblock_coded = (u4_num_coeff != 0);
    INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);

    ps_dec->pu1_inv_scan = puc_temp;

 return OK;
}

WORD32 ih264d_cavlc_parse_8x8block_both_available(WORD16 *pi2_coeff_block,
                                                  UWORD32 u4_sub_block_strd,
                                                  UWORD32 u4_isdc,
 dec_struct_t * ps_dec,
                                                  UWORD8 *pu1_top_nnz,
                                                  UWORD8 *pu1_left_nnz,
                                                  UWORD8 u1_tran_form8x8,
                                                  UWORD8 u1_mb_field_decodingflag,
                                                  UWORD32 *pu4_csbp)
{
    UWORD32 u4_num_coeff, u4_n, u4_subblock_coded;
    UWORD32 u4_top0, u4_top1;
    UWORD32 *pu4_dummy;
    WORD32 (**pf_cavlc_parse4x4coeff)(WORD16 *pi2_coeff_block,
                                      UWORD32 u4_isdc,
                                      WORD32 u4_n,
 struct _DecStruct *ps_dec,
                                      UWORD32 *pu4_dummy) =
                                      ps_dec->pf_cavlc_parse4x4coeff;
    UWORD32 u4_idx = 0;
    UWORD8 *puc_temp;
    WORD32 ret;

 *pu4_csbp = 0;
 /* need to change the inverse scan matrices here */
    puc_temp = ps_dec->pu1_inv_scan;

 /*------------------------------------------------------*/
 /* Residual 4x4 decoding: SubBlock 0                    */
 /*------------------------------------------------------*/
 if(u1_tran_form8x8)
 {
 if(!u1_mb_field_decodingflag)
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[0];
 }
 else
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[0];
 }
 }
    u4_n = (pu1_top_nnz[0] + pu1_left_nnz[0] + 1) >> 1;
    ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
                                             u4_n, ps_dec, &u4_num_coeff);
 if(ret != OK)
 return ret;

    u4_top0 = u4_num_coeff;
    u4_subblock_coded = (u4_num_coeff != 0);
    INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);

 /*------------------------------------------------------*/
 /* Residual 4x4 decoding: SubBlock 1                    */
 /*------------------------------------------------------*/
    u4_idx++;
 if(u1_tran_form8x8)
 {
 if(!u1_mb_field_decodingflag)
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[1];
 }
 else
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[1];
 }
 }
 else
 {
        pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
 }
    u4_n = (pu1_top_nnz[1] + u4_num_coeff + 1) >> 1;
    ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
                                             u4_n, ps_dec, &u4_num_coeff);
 if(ret != OK)
 return ret;

    u4_top1 = pu1_left_nnz[0] = u4_num_coeff;
    u4_subblock_coded = (u4_num_coeff != 0);
    INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);

 /*------------------------------------------------------*/
 /* Residual 4x4 decoding: SubBlock 2                    */
 /*------------------------------------------------------*/
    u4_idx += (u4_sub_block_strd - 1);
 if(u1_tran_form8x8)
 {
 if(!u1_mb_field_decodingflag)
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[2];
 }
 else
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[2];
 }
 }
 else
 {
        pi2_coeff_block += ((u4_sub_block_strd - 1) * NUM_COEFFS_IN_4x4BLK);
 }
    u4_n = (u4_top0 + pu1_left_nnz[1] + 1) >> 1;
    ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
                                             u4_n, ps_dec, &u4_num_coeff);
 if(ret != OK)
 return ret;

    pu1_top_nnz[0] = u4_num_coeff;
    u4_subblock_coded = (u4_num_coeff != 0);
    INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);

 /*------------------------------------------------------*/
 /* Residual 4x4 decoding: SubBlock 3                    */
 /*------------------------------------------------------*/
    u4_idx++;
 if(u1_tran_form8x8)
 {
 if(!u1_mb_field_decodingflag)
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_prog8x8_cavlc[3];
 }
 else
 {
            ps_dec->pu1_inv_scan =
 (UWORD8*)gau1_ih264d_inv_scan_int8x8_cavlc[3];
 }
 }
 else
 {
        pi2_coeff_block += NUM_COEFFS_IN_4x4BLK;
 }
    u4_n = (u4_top1 + u4_num_coeff + 1) >> 1;
    ret = pf_cavlc_parse4x4coeff[(u4_n > 7)](pi2_coeff_block, u4_isdc,
                                             u4_n, ps_dec, &u4_num_coeff);
 if(ret != OK)
 return ret;

    pu1_top_nnz[1] = pu1_left_nnz[1] = u4_num_coeff;
    u4_subblock_coded = (u4_num_coeff != 0);
    INSERT_BIT(*pu4_csbp, u4_idx, u4_subblock_coded);

    ps_dec->pu1_inv_scan = puc_temp;

 return OK;
}
