diff options
Diffstat (limited to 'third_party/aom/aom_dsp/entdec.c')
-rw-r--r-- | third_party/aom/aom_dsp/entdec.c | 89 |
1 files changed, 26 insertions, 63 deletions
diff --git a/third_party/aom/aom_dsp/entdec.c b/third_party/aom/aom_dsp/entdec.c index 71dad0df6..b8e9078c3 100644 --- a/third_party/aom/aom_dsp/entdec.c +++ b/third_party/aom/aom_dsp/entdec.c @@ -9,11 +9,9 @@ * PATENTS file, you can obtain it at www.aomedia.org/license/patent. */ -#ifdef HAVE_CONFIG_H -#include "./config.h" -#endif - +#include <assert.h> #include "aom_dsp/entdec.h" +#include "aom_dsp/prob.h" /*A range decoder. This is an entropy decoder based upon \cite{Mar79}, which is itself a @@ -75,6 +73,8 @@ Even relatively modest values like 100 would work fine.*/ #define OD_EC_LOTS_OF_BITS (0x4000) +/*The return value of od_ec_dec_tell does not change across an od_ec_dec_refill + call.*/ static void od_ec_dec_refill(od_ec_dec *dec) { int s; od_ec_window dif; @@ -87,7 +87,7 @@ static void od_ec_dec_refill(od_ec_dec *dec) { end = dec->end; s = OD_EC_WINDOW_SIZE - 9 - (cnt + 15); for (; s >= 0 && bptr < end; s -= 8, bptr++) { - OD_ASSERT(s <= OD_EC_WINDOW_SIZE - 8); + assert(s <= OD_EC_WINDOW_SIZE - 8); dif ^= (od_ec_window)bptr[0] << s; cnt += 8; } @@ -111,7 +111,7 @@ static void od_ec_dec_refill(od_ec_dec *dec) { static int od_ec_dec_normalize(od_ec_dec *dec, od_ec_window dif, unsigned rng, int ret) { int d; - OD_ASSERT(rng <= 65535U); + assert(rng <= 65535U); d = 16 - OD_ILOG_NZ(rng); dec->cnt -= d; /*This is equivalent to shifting in 1's instead of 0's.*/ @@ -127,9 +127,6 @@ static int od_ec_dec_normalize(od_ec_dec *dec, od_ec_window dif, unsigned rng, void od_ec_dec_init(od_ec_dec *dec, const unsigned char *buf, uint32_t storage) { dec->buf = buf; - dec->eptr = buf + storage; - dec->end_window = 0; - dec->nend_bits = 0; dec->tell_offs = 10 - (OD_EC_WINDOW_SIZE - 8); dec->end = buf + storage; dec->bptr = buf; @@ -150,13 +147,14 @@ int od_ec_decode_bool_q15(od_ec_dec *dec, unsigned f) { unsigned r_new; unsigned v; int ret; - OD_ASSERT(0 < f); - OD_ASSERT(f < 32768U); + assert(0 < f); + assert(f < 32768U); dif = dec->dif; r = dec->rng; - OD_ASSERT(dif >> (OD_EC_WINDOW_SIZE - 16) < r); - OD_ASSERT(32768U <= r); - v = (r >> 8) * (uint32_t)f >> 7; + assert(dif >> (OD_EC_WINDOW_SIZE - 16) < r); + assert(32768U <= r); + v = ((r >> 8) * (uint32_t)(f >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT)); + v += EC_MIN_PROB; vw = (od_ec_window)v << (OD_EC_WINDOW_SIZE - 16); ret = 1; r_new = v; @@ -170,8 +168,8 @@ int od_ec_decode_bool_q15(od_ec_dec *dec, unsigned f) { /*Decodes a symbol given an inverse cumulative distribution function (CDF) table in Q15. - icdf: 32768 minus the CDF, such that symbol s falls in the range - [s > 0 ? (32768 - icdf[s - 1]) : 0, 32768 - icdf[s]). + icdf: CDF_PROB_TOP minus the CDF, such that symbol s falls in the range + [s > 0 ? (CDF_PROB_TOP - icdf[s - 1]) : 0, CDF_PROB_TOP - icdf[s]). The values must be monotonically non-increasing, and icdf[nsyms - 1] must be 0. nsyms: The number of symbols in the alphabet. @@ -187,62 +185,28 @@ int od_ec_decode_cdf_q15(od_ec_dec *dec, const uint16_t *icdf, int nsyms) { (void)nsyms; dif = dec->dif; r = dec->rng; - OD_ASSERT(dif >> (OD_EC_WINDOW_SIZE - 16) < r); - OD_ASSERT(icdf[nsyms - 1] == OD_ICDF(32768U)); - OD_ASSERT(32768U <= r); + const int N = nsyms - 1; + + assert(dif >> (OD_EC_WINDOW_SIZE - 16) < r); + assert(icdf[nsyms - 1] == OD_ICDF(CDF_PROB_TOP)); + assert(32768U <= r); + assert(7 - EC_PROB_SHIFT - CDF_SHIFT >= 0); c = (unsigned)(dif >> (OD_EC_WINDOW_SIZE - 16)); v = r; ret = -1; do { u = v; - v = (r >> 8) * (uint32_t)icdf[++ret] >> 7; + v = ((r >> 8) * (uint32_t)(icdf[++ret] >> EC_PROB_SHIFT) >> + (7 - EC_PROB_SHIFT - CDF_SHIFT)); + v += EC_MIN_PROB * (N - ret); } while (c < v); - OD_ASSERT(v < u); - OD_ASSERT(u <= r); + assert(v < u); + assert(u <= r); r = u - v; dif -= (od_ec_window)v << (OD_EC_WINDOW_SIZE - 16); return od_ec_dec_normalize(dec, dif, r, ret); } -#if CONFIG_RAWBITS -/*Extracts a sequence of raw bits from the stream. - The bits must have been encoded with od_ec_enc_bits(). - ftb: The number of bits to extract. - This must be between 0 and 25, inclusive. - Return: The decoded bits.*/ -uint32_t od_ec_dec_bits_(od_ec_dec *dec, unsigned ftb) { - od_ec_window window; - int available; - uint32_t ret; - OD_ASSERT(ftb <= 25); - window = dec->end_window; - available = dec->nend_bits; - if ((unsigned)available < ftb) { - const unsigned char *buf; - const unsigned char *eptr; - buf = dec->buf; - eptr = dec->eptr; - OD_ASSERT(available <= OD_EC_WINDOW_SIZE - 8); - do { - if (eptr <= buf) { - dec->tell_offs += OD_EC_LOTS_OF_BITS - available; - available = OD_EC_LOTS_OF_BITS; - break; - } - window |= (od_ec_window) * --eptr << available; - available += 8; - } while (available <= OD_EC_WINDOW_SIZE - 8); - dec->eptr = eptr; - } - ret = (uint32_t)window & (((uint32_t)1 << ftb) - 1); - window >>= ftb; - available -= ftb; - dec->end_window = window; - dec->nend_bits = available; - return ret; -} -#endif - /*Returns the number of bits "used" by the decoded symbols so far. This same number can be computed in either the encoder or the decoder, and is suitable for making coding decisions. @@ -250,8 +214,7 @@ uint32_t od_ec_dec_bits_(od_ec_dec *dec, unsigned ftb) { This will always be slightly larger than the exact value (e.g., all rounding error is in the positive direction).*/ int od_ec_dec_tell(const od_ec_dec *dec) { - return (int)(((dec->end - dec->eptr) + (dec->bptr - dec->buf)) * 8 - - dec->cnt - dec->nend_bits + dec->tell_offs); + return (int)((dec->bptr - dec->buf) * 8 - dec->cnt + dec->tell_offs); } /*Returns the number of bits "used" by the decoded symbols so far. |