Cabac optimizations

Optimized cabac functions for writing bypass bins.
Removed unused return from cabac flush function.
Removed the macro REV_NBITS.

Bug: 22860270

Change-Id: Iece82797f2f45a35281817a2b77a7c7fe4e02bd1
This commit is contained in:
Doney Alex 2015-06-30 17:56:42 +05:30 committed by Marco Nelissen
parent eddf6a33fe
commit 0574be65f4
3 changed files with 29 additions and 72 deletions

View file

@ -111,30 +111,29 @@
* *
******************************************************************************* *******************************************************************************
*/ */
UWORD32 ih264e_cabac_UEGk0_binarization(WORD16 i2_sufs, WORD8 *pi1_bins_len) UWORD32 ih264e_cabac_UEGk0_binarization(WORD16 i2_sufs, WORD8 *pi1_bins_len)
{ {
UWORD32 u4_bins; WORD32 unary_length;
WORD32 i4_len; UWORD32 u4_sufs_shiftk_plus1, u4_egk, u4_unary_bins;
WORD16 x, y;
x = i2_sufs + 1; u4_sufs_shiftk_plus1 = i2_sufs + 1;
i4_len = CLZ(x);
i4_len = 31 - i4_len;
y = 1 << i4_len;
y = y - 1;
i2_sufs = i2_sufs - y;
u4_bins = y << 1;
u4_bins = u4_bins << i4_len;
u4_bins = u4_bins + i2_sufs;
REV(u4_bins, u4_bins); unary_length = (32 - CLZ(u4_sufs_shiftk_plus1) + (0 == u4_sufs_shiftk_plus1));
u4_bins = u4_bins >> (31 - 2 * i4_len);
(*pi1_bins_len) = 2 * i4_len + 1;
return (u4_bins); /* unary code with (unary_length-1) '1's and terminating '0' bin */
u4_unary_bins = (1 << unary_length) - 2;
/* insert the symbol prefix of (unary length - 1) bins */
u4_egk = (u4_unary_bins << (unary_length - 1))
| (u4_sufs_shiftk_plus1 & ((1 << (unary_length - 1)) - 1));
/* length of the code = 2 *(unary_length - 1) + 1 + k */
*pi1_bins_len = (2 * unary_length) - 1;
return (u4_egk);
} }
/** /**
******************************************************************************* *******************************************************************************
* *
@ -236,14 +235,14 @@ void ih264e_get_cabac_context(entropy_ctxt_t *ps_ent_ctxt, WORD32 u4_mb_type)
* @param[in] ps_cabac_ctxt * @param[in] ps_cabac_ctxt
* pointer to cabac context (handle) * pointer to cabac context (handle)
* *
* @returns success or failure error code * @returns none
* *
* @remarks * @remarks
* None * None
* *
******************************************************************************* *******************************************************************************
*/ */
WORD32 ih264e_cabac_flush(cabac_ctxt_t *ps_cabac_ctxt) void ih264e_cabac_flush(cabac_ctxt_t *ps_cabac_ctxt)
{ {
/* bit stream ptr */ /* bit stream ptr */
@ -267,17 +266,6 @@ WORD32 ih264e_cabac_flush(cabac_ctxt_t *ps_cabac_ctxt)
WORD32 bits_left; WORD32 bits_left;
WORD32 rem_bits; WORD32 rem_bits;
/*********************************************************************/
/* Bitstream overflow check */
/* NOTE: corner case of epb bytes (max 2 for 32bit word) not handled */
/*********************************************************************/
if ((u4_strm_buf_offset + u4_out_standing_bytes + 1)
>= ps_stream->u4_max_strm_size)
{
/* return without corrupting the buffer beyond its size */
return (IH264E_BITSTREAM_BUFFER_OVERFLOW);
}
if (carry) if (carry)
{ {
/* CORNER CASE: if the previous data is 0x000003, then EPB will be inserted /* CORNER CASE: if the previous data is 0x000003, then EPB will be inserted
@ -336,7 +324,6 @@ WORD32 ih264e_cabac_flush(cabac_ctxt_t *ps_cabac_ctxt)
ps_stream->u4_cur_word = 0; ps_stream->u4_cur_word = 0;
ps_stream->i4_bits_left_in_cw = WORD_SIZE; ps_stream->i4_bits_left_in_cw = WORD_SIZE;
return (IH264E_SUCCESS);
} }
} }
@ -763,7 +750,6 @@ void ih264e_cabac_encode_bypass_bins(cabac_ctxt_t *ps_cabac, UWORD32 u4_bins,
UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range; UWORD32 u4_range = ps_cab_enc_env->u4_code_int_range;
WORD32 next_byte; WORD32 next_byte;
UWORD32 rev_next_byte;
/* Sanity checks */ /* Sanity checks */
ASSERT((num_bins < 33) && (num_bins > 0)); ASSERT((num_bins < 33) && (num_bins > 0));
@ -777,14 +763,11 @@ void ih264e_cabac_encode_bypass_bins(cabac_ctxt_t *ps_cabac, UWORD32 u4_bins,
{ {
num_bins -= 8; num_bins -= 8;
/* extract the leading 8 bins */ next_byte = (u4_bins >> (num_bins)) & 0xff;
next_byte = (u4_bins) & 0xff;
u4_bins >>= 8;
REV_NBITS(next_byte, 8, rev_next_byte);
/* L = (L << 8) + (R * next_byte) */ /* L = (L << 8) + (R * next_byte) */
ps_cab_enc_env->u4_code_int_low <<= 8; ps_cab_enc_env->u4_code_int_low <<= 8;
ps_cab_enc_env->u4_code_int_low += (rev_next_byte * u4_range); ps_cab_enc_env->u4_code_int_low += (next_byte * u4_range);
ps_cab_enc_env->u4_bits_gen += 8; ps_cab_enc_env->u4_bits_gen += 8;
if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS) if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
@ -797,10 +780,8 @@ void ih264e_cabac_encode_bypass_bins(cabac_ctxt_t *ps_cabac, UWORD32 u4_bins,
/* Update low with remaining bins and return */ /* Update low with remaining bins and return */
next_byte = (u4_bins & ((1 << num_bins) - 1)); next_byte = (u4_bins & ((1 << num_bins) - 1));
REV_NBITS(next_byte, num_bins, rev_next_byte);
ps_cab_enc_env->u4_code_int_low <<= num_bins; ps_cab_enc_env->u4_code_int_low <<= num_bins;
ps_cab_enc_env->u4_code_int_low += (rev_next_byte * u4_range); ps_cab_enc_env->u4_code_int_low += (next_byte * u4_range);
ps_cab_enc_env->u4_bits_gen += num_bins; ps_cab_enc_env->u4_bits_gen += num_bins;
if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS) if (ps_cab_enc_env->u4_bits_gen > CABAC_BITS)
@ -810,10 +791,3 @@ void ih264e_cabac_encode_bypass_bins(cabac_ctxt_t *ps_cabac, UWORD32 u4_bins,
} }
} }

View file

@ -47,24 +47,6 @@
#define CABAC_BITS 9 #define CABAC_BITS 9
/**
******************************************************************************
* @macro Count number of bits set
******************************************************************************
*/
#define REV_NBITS(word, size, rev_word) \
{ \
WORD32 i; \
rev_word = 0; \
for (i = 0; i < (size); i++) \
{ \
UWORD32 bit = ((word) >> i) & 1; \
rev_word += (1 << ((size) - i - 1)) * bit; \
} \
} \
/** /**
****************************************************************************** ******************************************************************************
* @macro Reverse bits in an unsigned integer * @macro Reverse bits in an unsigned integer
@ -201,14 +183,14 @@ void ih264e_get_cabac_context(entropy_ctxt_t *ps_ent_ctxt, WORD32 u4_mb_type);
* @param[in] ps_cabac_ctxt * @param[in] ps_cabac_ctxt
* pointer to cabac context (handle) * pointer to cabac context (handle)
* *
* @returns success or failure error code * @returns none
* *
* @remarks * @remarks
* None * None
* *
******************************************************************************* *******************************************************************************
*/ */
WORD32 ih264e_cabac_flush(cabac_ctxt_t *ps_cabac_ctxt); void ih264e_cabac_flush(cabac_ctxt_t *ps_cabac_ctxt);
/** /**

View file

@ -856,7 +856,7 @@ static void ih264e_cabac_write_coeff4x4(WORD16 *pi2_res_block, UWORD8 u1_nnz,
} }
/* encode coeff_sign_flag[i] */ /* encode coeff_sign_flag[i] */
u1_sign = ((*pi16_coeffs) < 0) ? 1 : 0; u1_sign = ((*pi16_coeffs) < 0) ? 1 : 0;
ih264e_cabac_encode_bypass_bins(ps_cabac_ctxt, u1_sign, 1); ih264e_cabac_encode_bypass_bin(ps_cabac_ctxt, u1_sign);
i = CLZ(u4_sig_coeff); i = CLZ(u4_sig_coeff);
i = 31 - i; i = 31 - i;
pi16_coeffs--; pi16_coeffs--;
@ -1375,7 +1375,7 @@ static void ih264e_cabac_enc_ctx_mvd(WORD16 u1_mvd, UWORD32 u4_ctx_idx_offset,
{ {
if (i2_sufs >= (1 << k)) if (i2_sufs >= (1 << k))
{ {
u4_bins = (u4_bins | (1 << i1_bins_len)); u4_bins = (u4_bins | (1 << (31 - i1_bins_len)));
i1_bins_len++; i1_bins_len++;
i2_sufs = i2_sufs - (1 << k); i2_sufs = i2_sufs - (1 << k);
k++; k++;
@ -1386,12 +1386,13 @@ static void ih264e_cabac_enc_ctx_mvd(WORD16 u1_mvd, UWORD32 u4_ctx_idx_offset,
while (k--) while (k--)
{ {
u1_bin = ((i2_sufs >> k) & 0x01); u1_bin = ((i2_sufs >> k) & 0x01);
u4_bins = (u4_bins | (u1_bin << i1_bins_len)); u4_bins = (u4_bins | (u1_bin << (31 - i1_bins_len)));
i1_bins_len++; i1_bins_len++;
} }
break; break;
} }
} }
u4_bins >>= (32 - i1_bins_len);
ih264e_cabac_encode_bypass_bins(ps_cabac_ctxt, u4_bins, ih264e_cabac_encode_bypass_bins(ps_cabac_ctxt, u4_bins,
i1_bins_len); i1_bins_len);
} }
@ -1428,9 +1429,9 @@ static void ih264e_cabac_enc_ctx_mvd(WORD16 u1_mvd, UWORD32 u4_ctx_idx_offset,
} }
/* sign bit, uses EncodeBypass */ /* sign bit, uses EncodeBypass */
if (u1_mvd > 0) if (u1_mvd > 0)
ih264e_cabac_encode_bypass_bins(ps_cabac_ctxt, 0, 1); ih264e_cabac_encode_bypass_bin(ps_cabac_ctxt, 0);
else else
ih264e_cabac_encode_bypass_bins(ps_cabac_ctxt, 1, 1); ih264e_cabac_encode_bypass_bin(ps_cabac_ctxt, 1);
} }
} }