Commit 45d4fde6 authored by Nathan Egge's avatar Nathan Egge Committed by Ronald S. Bultje

Add msac_decode_bool_equi() function

When decoding an equi-probable bit (e.g. prob = 1/2) we can simplify the
decode function.
parent 25020403
...@@ -413,7 +413,7 @@ static void read_pal_plane(Dav1dTileContext *const t, Av1Block *const b, ...@@ -413,7 +413,7 @@ static void read_pal_plane(Dav1dTileContext *const t, Av1Block *const b,
// find reused cache entries // find reused cache entries
int i = 0; int i = 0;
for (int n = 0; n < n_cache && i < pal_sz; n++) for (int n = 0; n < n_cache && i < pal_sz; n++)
if (msac_decode_bool(&ts->msac, EC_BOOL_EPROB)) if (msac_decode_bool_equi(&ts->msac))
used_cache[i++] = cache[n]; used_cache[i++] = cache[n];
const int n_used_cache = i; const int n_used_cache = i;
...@@ -477,13 +477,13 @@ static void read_pal_uv(Dav1dTileContext *const t, Av1Block *const b, ...@@ -477,13 +477,13 @@ static void read_pal_uv(Dav1dTileContext *const t, Av1Block *const b,
uint16_t *const pal = f->frame_thread.pass ? uint16_t *const pal = f->frame_thread.pass ?
f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) + f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
((t->bx >> 1) + (t->by & 1))][2] : t->pal[2]; ((t->bx >> 1) + (t->by & 1))][2] : t->pal[2];
if (msac_decode_bool(&ts->msac, EC_BOOL_EPROB)) { if (msac_decode_bool_equi(&ts->msac)) {
const int bits = f->cur.p.bpc - 4 + msac_decode_bools(&ts->msac, 2); const int bits = f->cur.p.bpc - 4 + msac_decode_bools(&ts->msac, 2);
int prev = pal[0] = msac_decode_bools(&ts->msac, f->cur.p.bpc); int prev = pal[0] = msac_decode_bools(&ts->msac, f->cur.p.bpc);
const int max = (1 << f->cur.p.bpc) - 1; const int max = (1 << f->cur.p.bpc) - 1;
for (int i = 1; i < b->pal_sz[1]; i++) { for (int i = 1; i < b->pal_sz[1]; i++) {
int delta = msac_decode_bools(&ts->msac, bits); int delta = msac_decode_bools(&ts->msac, bits);
if (delta && msac_decode_bool(&ts->msac, EC_BOOL_EPROB)) delta = -delta; if (delta && msac_decode_bool_equi(&ts->msac)) delta = -delta;
prev = pal[i] = (prev + delta) & max; prev = pal[i] = (prev + delta) & max;
} }
} else { } else {
...@@ -927,7 +927,7 @@ static int decode_b(Dav1dTileContext *const t, ...@@ -927,7 +927,7 @@ static int decode_b(Dav1dTileContext *const t,
delta_q = msac_decode_bools(&ts->msac, n_bits) + 1 + (1 << n_bits); delta_q = msac_decode_bools(&ts->msac, n_bits) + 1 + (1 << n_bits);
} }
if (delta_q) { if (delta_q) {
if (msac_decode_bool(&ts->msac, EC_BOOL_EPROB)) delta_q = -delta_q; if (msac_decode_bool_equi(&ts->msac)) delta_q = -delta_q;
delta_q *= 1 << f->frame_hdr->delta.q.res_log2; delta_q *= 1 << f->frame_hdr->delta.q.res_log2;
} }
ts->last_qidx = iclip(ts->last_qidx + delta_q, 1, 255); ts->last_qidx = iclip(ts->last_qidx + delta_q, 1, 255);
...@@ -949,7 +949,7 @@ static int decode_b(Dav1dTileContext *const t, ...@@ -949,7 +949,7 @@ static int decode_b(Dav1dTileContext *const t,
1 + (1 << n_bits); 1 + (1 << n_bits);
} }
if (delta_lf) { if (delta_lf) {
if (msac_decode_bool(&ts->msac, EC_BOOL_EPROB)) if (msac_decode_bool_equi(&ts->msac))
delta_lf = -delta_lf; delta_lf = -delta_lf;
delta_lf *= 1 << f->frame_hdr->delta.lf.res_log2; delta_lf *= 1 << f->frame_hdr->delta.lf.res_log2;
} }
...@@ -1572,7 +1572,7 @@ static int decode_b(Dav1dTileContext *const t, ...@@ -1572,7 +1572,7 @@ static int decode_b(Dav1dTileContext *const t,
} else { } else {
b->comp_type = COMP_INTER_SEG; b->comp_type = COMP_INTER_SEG;
} }
b->mask_sign = msac_decode_bool(&ts->msac, EC_BOOL_EPROB); b->mask_sign = msac_decode_bool_equi(&ts->msac);
if (DEBUG_BLOCK_INFO) if (DEBUG_BLOCK_INFO)
printf("Post-seg/wedge[%d,wedge_idx=%d,sign=%d]: r=%d\n", printf("Post-seg/wedge[%d,wedge_idx=%d,sign=%d]: r=%d\n",
b->comp_type == COMP_INTER_WEDGE, b->comp_type == COMP_INTER_WEDGE,
......
...@@ -91,6 +91,22 @@ unsigned msac_decode_symbol(MsacContext *const s, const uint16_t *const cdf, ...@@ -91,6 +91,22 @@ unsigned msac_decode_symbol(MsacContext *const s, const uint16_t *const cdf,
return ret - 1; return ret - 1;
} }
unsigned msac_decode_bool_equi(MsacContext *const s) {
ec_win v, vw, dif = s->dif;
uint16_t r = s->rng;
unsigned ret;
assert((dif >> (EC_WIN_SIZE - 16)) < r);
// When the probability is 1/2, f = 16384 >> EC_PROB_SHIFT = 256 and we can
// replace the multiply with a simple shift.
v = ((r >> 8) << 7) + EC_MIN_PROB;
vw = v << (EC_WIN_SIZE - 16);
ret = dif >= vw;
dif -= ret*vw;
v += ret*(r - 2*v);
ctx_norm(s, dif, v);
return !ret;
}
/* Decode a single binary value. /* Decode a single binary value.
* f: The probability that the bit is one * f: The probability that the bit is one
* Return: The value decoded (0 or 1). */ * Return: The value decoded (0 or 1). */
...@@ -111,7 +127,7 @@ unsigned msac_decode_bool(MsacContext *const s, const unsigned f) { ...@@ -111,7 +127,7 @@ unsigned msac_decode_bool(MsacContext *const s, const unsigned f) {
unsigned msac_decode_bools(MsacContext *const c, const unsigned l) { unsigned msac_decode_bools(MsacContext *const c, const unsigned l) {
int v = 0; int v = 0;
for (int n = (int) l - 1; n >= 0; n--) for (int n = (int) l - 1; n >= 0; n--)
v = (v << 1) | msac_decode_bool(c, EC_BOOL_EPROB); v = (v << 1) | msac_decode_bool_equi(c);
return v; return v;
} }
...@@ -122,7 +138,7 @@ int msac_decode_subexp(MsacContext *const c, const int ref, ...@@ -122,7 +138,7 @@ int msac_decode_subexp(MsacContext *const c, const int ref,
int a = 0; int a = 0;
int b = k; int b = k;
while ((2 << b) < n) { while ((2 << b) < n) {
if (!msac_decode_bool(c, EC_BOOL_EPROB)) break; if (!msac_decode_bool_equi(c)) break;
b = k + i++; b = k + i++;
a = (1 << b); a = (1 << b);
} }
...@@ -137,7 +153,7 @@ int msac_decode_uniform(MsacContext *const c, const unsigned n) { ...@@ -137,7 +153,7 @@ int msac_decode_uniform(MsacContext *const c, const unsigned n) {
assert(l > 1); assert(l > 1);
const unsigned m = (1 << l) - n; const unsigned m = (1 << l) - n;
const unsigned v = msac_decode_bools(c, l - 1); const unsigned v = msac_decode_bools(c, l - 1);
return v < m ? v : (v << 1) - m + msac_decode_bool(c, EC_BOOL_EPROB); return v < m ? v : (v << 1) - m + msac_decode_bool_equi(c);
} }
static void update_cdf(uint16_t *const cdf, const unsigned val, static void update_cdf(uint16_t *const cdf, const unsigned val,
......
...@@ -44,13 +44,13 @@ typedef struct MsacContext { ...@@ -44,13 +44,13 @@ typedef struct MsacContext {
} MsacContext; } MsacContext;
#define EC_PROB_SHIFT 6 #define EC_PROB_SHIFT 6
#define EC_BOOL_EPROB 256
void msac_init(MsacContext *c, const uint8_t *data, size_t sz, int disable_cdf_update_flag); void msac_init(MsacContext *c, const uint8_t *data, size_t sz, int disable_cdf_update_flag);
unsigned msac_decode_symbol(MsacContext *s, const uint16_t *cdf, unsigned msac_decode_symbol(MsacContext *s, const uint16_t *cdf,
const unsigned n_symbols); const unsigned n_symbols);
unsigned msac_decode_symbol_adapt(MsacContext *s, uint16_t *cdf, unsigned msac_decode_symbol_adapt(MsacContext *s, uint16_t *cdf,
const unsigned n_symbols); const unsigned n_symbols);
unsigned msac_decode_bool_equi(MsacContext *const s);
unsigned msac_decode_bool(MsacContext *s, unsigned f); unsigned msac_decode_bool(MsacContext *s, unsigned f);
unsigned msac_decode_bool_adapt(MsacContext *s, uint16_t *cdf); unsigned msac_decode_bool_adapt(MsacContext *s, uint16_t *cdf);
unsigned msac_decode_bools(MsacContext *c, unsigned l); unsigned msac_decode_bools(MsacContext *c, unsigned l);
......
...@@ -50,8 +50,8 @@ static unsigned read_golomb(MsacContext *const msac) { ...@@ -50,8 +50,8 @@ static unsigned read_golomb(MsacContext *const msac) {
int len = 0; int len = 0;
unsigned val = 1; unsigned val = 1;
while (!msac_decode_bool(msac, EC_BOOL_EPROB) && len < 32) len++; while (!msac_decode_bool_equi(msac) && len < 32) len++;
while (len--) val = (val << 1) | msac_decode_bool(msac, EC_BOOL_EPROB); while (len--) val = (val << 1) | msac_decode_bool_equi(msac);
return val - 1; return val - 1;
} }
...@@ -152,7 +152,7 @@ static int decode_coefs(Dav1dTileContext *const t, ...@@ -152,7 +152,7 @@ static int decode_coefs(Dav1dTileContext *const t,
unsigned mask = eob >> 1; unsigned mask = eob >> 1;
if (eob_hi_bit) eob |= mask; if (eob_hi_bit) eob |= mask;
for (mask >>= 1; mask; mask >>= 1) { for (mask >>= 1; mask; mask >>= 1) {
const int eob_bit = msac_decode_bool(&ts->msac, EC_BOOL_EPROB); const int eob_bit = msac_decode_bool_equi(&ts->msac);
if (eob_bit) eob |= mask; if (eob_bit) eob |= mask;
} }
if (dbg) if (dbg)
...@@ -231,7 +231,7 @@ static int decode_coefs(Dav1dTileContext *const t, ...@@ -231,7 +231,7 @@ static int decode_coefs(Dav1dTileContext *const t,
dc_sign = sign ? 0 : 2; dc_sign = sign ? 0 : 2;
dq = (dq_tbl[0] * qm_tbl[0] + 16) >> 5; dq = (dq_tbl[0] * qm_tbl[0] + 16) >> 5;
} else { } else {
sign = msac_decode_bool(&ts->msac, EC_BOOL_EPROB); sign = msac_decode_bool_equi(&ts->msac);
if (dbg) if (dbg)
printf("Post-sign[%d=%d=%d]: r=%d\n", i, rc, sign, ts->msac.rng); printf("Post-sign[%d=%d=%d]: r=%d\n", i, rc, sign, ts->msac.rng);
dq = (dq_tbl[1] * qm_tbl[rc] + 16) >> 5; dq = (dq_tbl[1] * qm_tbl[rc] + 16) >> 5;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment