Commit 33d16d81 authored by Rostislav Pehlivanov's avatar Rostislav Pehlivanov Committed by Jean-Baptiste Kempf

Rewrite msac.c

This rewrites msac.c to the point of there being no libaom project
code left, hence changing the license of the file to the dav1d
project's license.

The rewrite much simplifies and optimizes entropy decoding.
Some encoder specific code also remained, such as tell_offs, to
tell the fractional amount of bits left, which the decoder does
not need.

ctx_refill is much simpler and has a tighter loop with less
instructions, which on some CPUs can actually be ran in one cycle.
The old mechanism which checked to see if the buffer reached the
end to disable calling ctx_refill is gone, as all it saved was
a mostly well predicted branch.
The optimizations regarading this function enabled us to use
an ec_win of 64 bits whilst improving performance. This was not
possible with the old needlessly robust system.

Some msac-specific API changes were made - msac_decode_bool now
takes a scaled value directly rather than doing scaling itself.
This saves a shift in most use cases as the function is mainly
used to read equiprobable bools rather than ones with specific
probabilities.

There's still room for optimizations, mainly in that update_cdf
could be SIMD'd. This commit prepares for some of them by
moving the init function at the bottom of the file.

Overall decoder speedup seems to be around 3%-5%, specific on
bitrate and encoder as expected.
parent c0351e1b
...@@ -406,7 +406,7 @@ static void read_pal_plane(Dav1dTileContext *const t, Av1Block *const b, ...@@ -406,7 +406,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, 128 << 7)) if (msac_decode_bool(&ts->msac, EC_BOOL_EPROB))
used_cache[i++] = cache[n]; used_cache[i++] = cache[n];
const int n_used_cache = i; const int n_used_cache = i;
...@@ -470,13 +470,13 @@ static void read_pal_uv(Dav1dTileContext *const t, Av1Block *const b, ...@@ -470,13 +470,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, 128 << 7)) { if (msac_decode_bool(&ts->msac, EC_BOOL_EPROB)) {
const int bits = f->cur.p.p.bpc - 4 + msac_decode_bools(&ts->msac, 2); const int bits = f->cur.p.p.bpc - 4 + msac_decode_bools(&ts->msac, 2);
int prev = pal[0] = msac_decode_bools(&ts->msac, f->cur.p.p.bpc); int prev = pal[0] = msac_decode_bools(&ts->msac, f->cur.p.p.bpc);
const int max = (1 << f->cur.p.p.bpc) - 1; const int max = (1 << f->cur.p.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, 128 << 7)) delta = -delta; if (delta && msac_decode_bool(&ts->msac, EC_BOOL_EPROB)) delta = -delta;
prev = pal[i] = (prev + delta) & max; prev = pal[i] = (prev + delta) & max;
} }
} else { } else {
...@@ -865,7 +865,7 @@ static int decode_b(Dav1dTileContext *const t, ...@@ -865,7 +865,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, 128 << 7)) delta_q = -delta_q; if (msac_decode_bool(&ts->msac, EC_BOOL_EPROB)) 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);
...@@ -887,7 +887,7 @@ static int decode_b(Dav1dTileContext *const t, ...@@ -887,7 +887,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, 128 << 7)) if (msac_decode_bool(&ts->msac, EC_BOOL_EPROB))
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;
} }
...@@ -1497,7 +1497,7 @@ static int decode_b(Dav1dTileContext *const t, ...@@ -1497,7 +1497,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, 128 << 7); b->mask_sign = msac_decode_bool(&ts->msac, EC_BOOL_EPROB);
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,
......
/* /*
* Copyright (c) 2001-2016, Alliance for Open Media. All rights reserved * Copyright © 2018, VideoLAN and dav1d authors
* Copyright © 2018, Two Orioles, LLC
* All rights reserved.
* *
* This source code is subject to the terms of the BSD 2 Clause License and * Redistribution and use in source and binary forms, with or without
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License * modification, are permitted provided that the following conditions are met:
* was not distributed with this source code in the LICENSE file, you can *
* obtain it at www.aomedia.org/license/software. If the Alliance for Open * 1. Redistributions of source code must retain the above copyright notice, this
* Media Patent License 1.0 was not distributed with this source code in the * list of conditions and the following disclaimer.
* PATENTS file, you can obtain it at www.aomedia.org/license/patent. *
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "config.h" #include "config.h"
...@@ -18,283 +34,115 @@ ...@@ -18,283 +34,115 @@
#include "src/msac.h" #include "src/msac.h"
typedef MsacContext od_ec_dec;
//#define CDF_SIZE(x) ((x) + 1)
#define CDF_PROB_BITS 15
#define CDF_PROB_TOP (1 << CDF_PROB_BITS)
//#define CDF_INIT_TOP 32768
#define CDF_SHIFT (15 - CDF_PROB_BITS)
#define OD_CLZ0 (1)
#define OD_CLZ(x) (-get_msb(x))
#define OD_ILOG_NZ(x) (OD_CLZ0 - OD_CLZ(x))
static inline int get_msb(unsigned int n) {
assert(n != 0);
return 31 ^ clz(n);
}
#define EC_PROB_SHIFT 6
#define EC_MIN_PROB 4 // must be <= (1<<EC_PROB_SHIFT)/16 #define EC_MIN_PROB 4 // must be <= (1<<EC_PROB_SHIFT)/16
/*OPT: od_ec_window must be at least 32 bits, but if you have fast arithmetic #define EC_WIN_SIZE (sizeof(ec_win) << 3)
on a larger type, you can speed up the decoder by using it here.*/
typedef uint32_t od_ec_window;
#define OD_EC_WINDOW_SIZE ((int)sizeof(od_ec_window) * CHAR_BIT)
/*The resolution of fractional-precision bit usage measurements, i.e.,
3 => 1/8th bits.*/
#define OD_BITRES (3)
#define OD_ICDF AOM_ICDF
#define AOM_ICDF(a) (32768-(a))
/*A range decoder.
This is an entropy decoder based upon \cite{Mar79}, which is itself a
rediscovery of the FIFO arithmetic code introduced by \cite{Pas76}.
It is very similar to arithmetic encoding, except that encoding is done with
digits in any base, instead of with bits, and so it is faster when using
larger bases (i.e.: a byte).
The author claims an average waste of $\frac{1}{2}\log_b(2b)$ bits, where $b$
is the base, longer than the theoretical optimum, but to my knowledge there
is no published justification for this claim.
This only seems true when using near-infinite precision arithmetic so that
the process is carried out with no rounding errors.
An excellent description of implementation details is available at
http://www.arturocampos.com/ac_range.html
A recent work \cite{MNW98} which proposes several changes to arithmetic
encoding for efficiency actually re-discovers many of the principles
behind range encoding, and presents a good theoretical analysis of them.
End of stream is handled by writing out the smallest number of bits that
ensures that the stream will be correctly decoded regardless of the value of
any subsequent bits.
od_ec_dec_tell() can be used to determine how many bits were needed to decode
all the symbols thus far; other data can be packed in the remaining bits of
the input buffer.
@PHDTHESIS{Pas76,
author="Richard Clark Pasco",
title="Source coding algorithms for fast data compression",
school="Dept. of Electrical Engineering, Stanford University",
address="Stanford, CA",
month=May,
year=1976,
URL="http://www.richpasco.org/scaffdc.pdf"
}
@INPROCEEDINGS{Mar79,
author="Martin, G.N.N.",
title="Range encoding: an algorithm for removing redundancy from a digitised
message",
booktitle="Video & Data Recording Conference",
year=1979,
address="Southampton",
month=Jul,
URL="http://www.compressconsult.com/rangecoder/rngcod.pdf.gz"
}
@ARTICLE{MNW98,
author="Alistair Moffat and Radford Neal and Ian H. Witten",
title="Arithmetic Coding Revisited",
journal="{ACM} Transactions on Information Systems",
year=1998,
volume=16,
number=3,
pages="256--294",
month=Jul,
URL="http://researchcommons.waikato.ac.nz/bitstream/handle/10289/78/content.pdf"
}*/
/*This is meant to be a large, positive constant that can still be efficiently
loaded as an immediate (on platforms like ARM, for example).
Even relatively modest values like 100 would work fine.*/
#define OD_EC_LOTS_OF_BITS (0x4000)
static void od_ec_dec_refill(od_ec_dec *dec) { static inline void ctx_refill(MsacContext *s) {
int s; const uint8_t *buf_pos = s->buf_pos;
od_ec_window dif; const uint8_t *buf_end = s->buf_end;
int16_t cnt; int c = EC_WIN_SIZE - s->cnt - 24;
const unsigned char *bptr; ec_win dif = s->dif;
const unsigned char *end; while (c >= 0 && buf_pos < buf_end) {
dif = dec->dif; dif ^= ((ec_win)*buf_pos++) << c;
cnt = dec->cnt; c -= 8;
bptr = dec->bptr; }
end = dec->end; s->dif = dif;
s = OD_EC_WINDOW_SIZE - 9 - (cnt + 15); s->cnt = EC_WIN_SIZE - c - 24;
for (; s >= 0 && bptr < end; s -= 8, bptr++) { s->buf_pos = buf_pos;
assert(s <= OD_EC_WINDOW_SIZE - 8);
dif ^= (od_ec_window)bptr[0] << s;
cnt += 8;
}
if (bptr >= end) {
dec->tell_offs += OD_EC_LOTS_OF_BITS - cnt;
cnt = OD_EC_LOTS_OF_BITS;
}
dec->dif = dif;
dec->cnt = cnt;
dec->bptr = bptr;
}
/*Takes updated dif and range values, renormalizes them so that
32768 <= rng < 65536 (reading more bytes from the stream into dif if
necessary), and stores them back in the decoder context.
dif: The new value of dif.
rng: The new value of the range.
ret: The value to return.
Return: ret.
This allows the compiler to jump to this function via a tail-call.*/
static int od_ec_dec_normalize(od_ec_dec *dec, od_ec_window dif, unsigned rng,
int ret) {
int d;
assert(rng <= 65535U);
d = 16 - OD_ILOG_NZ(rng);
dec->cnt -= d;
/*This is equivalent to shifting in 1's instead of 0's.*/
dec->dif = ((dif + 1) << d) - 1;
dec->rng = rng << d;
if (dec->cnt < 0) od_ec_dec_refill(dec);
return ret;
} }
/*Initializes the decoder. /* Takes updated dif and range values, renormalizes them so that
buf: The input buffer to use. * 32768 <= rng < 65536 (reading more bytes from the stream into dif if
Return: 0 on success, or a negative value on error.*/ * necessary), and stores them back in the decoder context.
static void od_ec_dec_init(od_ec_dec *dec, const unsigned char *buf, * dif: The new value of dif.
uint32_t storage) { * rng: The new value of the range. */
dec->buf = buf; static inline void ctx_norm(MsacContext *s, ec_win dif, uint32_t rng) {
dec->tell_offs = 10 - (OD_EC_WINDOW_SIZE - 8); const uint16_t d = 15 - (31 ^ clz(rng));
dec->end = buf + storage; assert(rng <= 65535U);
dec->bptr = buf; s->cnt -= d;
dec->dif = ((od_ec_window)1 << (OD_EC_WINDOW_SIZE - 1)) - 1; s->dif = ((dif + 1) << d) - 1; /* Shift in 1s in the LSBs */
dec->rng = 0x8000; s->rng = rng << d;
dec->cnt = -15; if (s->cnt < 0)
dec->error = 0; ctx_refill(s);
od_ec_dec_refill(dec);
} }
/*Decode a single binary value. /* Decodes a symbol given an inverse cumulative distribution function (CDF)
f: The probability that the bit is one, scaled by 32768. * table in Q15. */
Return: The value decoded (0 or 1).*/ unsigned msac_decode_symbol(MsacContext *const s, const uint16_t *const cdf,
static int od_ec_decode_bool_q15(od_ec_dec *dec, unsigned f) { const unsigned n_symbols)
od_ec_window dif; {
od_ec_window vw; ec_win u, v = s->rng, r = s->rng >> 8;
unsigned r; const ec_win c = s->dif >> (EC_WIN_SIZE - 16);
unsigned r_new; unsigned ret = 0;
unsigned v;
int ret;
assert(0 < f);
assert(f < 32768U);
dif = dec->dif;
r = dec->rng;
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;
if (dif >= vw) {
r_new = r - v;
dif -= vw;
ret = 0;
}
return od_ec_dec_normalize(dec, dif, r_new, ret);
}
/*Decodes a symbol given an inverse cumulative distribution function (CDF) assert(!cdf[n_symbols - 1]);
table in Q15.
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.
This should be at most 16.
Return: The decoded symbol s.*/
static int od_ec_decode_cdf_q15(od_ec_dec *dec, const uint16_t *icdf, int nsyms) {
od_ec_window dif;
unsigned r;
unsigned c;
unsigned u;
unsigned v;
int ret;
(void)nsyms;
dif = dec->dif;
r = dec->rng;
const int N = nsyms - 1;
assert(dif >> (OD_EC_WINDOW_SIZE - 16) < r); do {
assert(icdf[nsyms - 1] == OD_ICDF(CDF_PROB_TOP)); u = v;
assert(32768U <= r); v = r * (cdf[ret++] >> EC_PROB_SHIFT);
assert(7 - EC_PROB_SHIFT - CDF_SHIFT >= 0); v >>= 7 - EC_PROB_SHIFT;
c = (unsigned)(dif >> (OD_EC_WINDOW_SIZE - 16)); v += EC_MIN_PROB * (n_symbols - ret);
v = r; } while (c < v);
ret = -1;
do {
u = v;
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);
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);
}
void msac_init(MsacContext *const c, assert(u <= s->rng);
const uint8_t *const data, const size_t sz)
{
od_ec_dec_init(c, data, sz);
}
unsigned msac_decode_symbol(MsacContext *const c, const uint16_t *const cdf, ctx_norm(s, s->dif - (v << (EC_WIN_SIZE - 16)), u - v);
const unsigned n_symbols) return ret - 1;
{
return od_ec_decode_cdf_q15(c, cdf, n_symbols);
} }
unsigned msac_decode_bool(MsacContext *const c, const unsigned cdf) { /* Decode a single binary value.
return od_ec_decode_bool_q15(c, cdf); * f: The probability that the bit is one
* Return: The value decoded (0 or 1). */
unsigned msac_decode_bool(MsacContext *const s, const unsigned f) {
ec_win v, vw, dif = s->dif;
uint16_t r = s->rng;
unsigned ret;
assert((dif >> (EC_WIN_SIZE - 16)) < r);
v = ((r >> 8) * f >> (7 - EC_PROB_SHIFT)) + 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;
} }
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, 128 << 7); v = (v << 1) | msac_decode_bool(c, EC_BOOL_EPROB);
return v; return v;
} }
int msac_decode_subexp(MsacContext *const c, const int ref, int msac_decode_subexp(MsacContext *const c, const int ref,
const unsigned n, const unsigned k) const int n, const unsigned k)
{ {
int i = 0; int i = 0;
int a = 0; int a = 0;
int b = k; int b = k;
while ((2U << b) < n) { while ((2 << b) < n) {
if (!msac_decode_bool(c, 128 << 7)) break; if (!msac_decode_bool(c, EC_BOOL_EPROB)) break;
b = k + i++; b = k + i++;
a = (1 << b); a = (1 << b);
} }
const unsigned v = msac_decode_bools(c, b) + a; const unsigned v = msac_decode_bools(c, b) + a;
return ref * 2U <= n ? inv_recenter(ref, v) : return ref * 2 <= n ? inv_recenter(ref, v) :
n - 1 - inv_recenter(n - 1 - ref, v); n - 1 - inv_recenter(n - 1 - ref, v);
} }
int msac_decode_uniform(MsacContext *const c, const unsigned n) { int msac_decode_uniform(MsacContext *const c, const unsigned n) {
assert(n > 0); assert(n > 0);
const int l = ulog2(n) + 1; const int l = ulog2(n) + 1;
assert(l > 1); assert(l > 1);
const unsigned m = (1U << 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, 128 << 7); return v < m ? v : (v << 1) - m + msac_decode_bool(c, EC_BOOL_EPROB);
} }
void update_cdf(uint16_t *cdf, unsigned val, unsigned nsymbs) { void update_cdf(uint16_t *cdf, unsigned val, unsigned nsymbs) {
int rate; int rate;
unsigned i, tmp; unsigned i;
static const int nsymbs2speed[17] = { static const int nsymbs2speed[17] = {
0, 0, 1, 1, 2, 2, 2, 2, 2, 0, 0, 1, 1, 2, 2, 2, 2, 2,
...@@ -302,17 +150,23 @@ void update_cdf(uint16_t *cdf, unsigned val, unsigned nsymbs) { ...@@ -302,17 +150,23 @@ void update_cdf(uint16_t *cdf, unsigned val, unsigned nsymbs) {
}; };
assert(nsymbs < 17); assert(nsymbs < 17);
rate = 3 + (cdf[nsymbs] > 15) + (cdf[nsymbs] > 31) + nsymbs2speed[nsymbs]; rate = 3 + (cdf[nsymbs] > 15) + (cdf[nsymbs] > 31) + nsymbs2speed[nsymbs];
tmp = 32768U;
// Single loop (faster) for (i = 0; i < val; ++i)
for (i = 0; i < nsymbs - 1; ++i) { cdf[i] += (32768 - cdf[i]) >> rate;
tmp = (i == val) ? 0 : tmp;
if (tmp < cdf[i]) { for (i = val; i < nsymbs - 1; i++)
cdf[i] -= ((cdf[i] - tmp) >> rate); cdf[i] -= cdf[i] >> rate;
} else {
cdf[i] += ((tmp - cdf[i]) >> rate);
}
}
cdf[nsymbs] += (cdf[nsymbs] < 32); cdf[nsymbs] += (cdf[nsymbs] < 32);
} }
void msac_init(MsacContext *const s, const uint8_t *const data,
const size_t sz)
{
s->buf_pos = data;
s->buf_end = data + sz;
s->dif = ((ec_win)1 << (EC_WIN_SIZE - 1)) - 1;
s->rng = 0x8000;
s->cnt = -15;
ctx_refill(s);
}
/* /*
* Copyright (c) 2001-2016, Alliance for Open Media. All rights reserved * Copyright © 2018, VideoLAN and dav1d authors
* Copyright © 2018, Two Orioles, LLC
* All rights reserved.
* *
* This source code is subject to the terms of the BSD 2 Clause License and * Redistribution and use in source and binary forms, with or without
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License * modification, are permitted provided that the following conditions are met:
* was not distributed with this source code in the LICENSE file, you can *
* obtain it at www.aomedia.org/license/software. If the Alliance for Open * 1. Redistributions of source code must retain the above copyright notice, this
* Media Patent License 1.0 was not distributed with this source code in the * list of conditions and the following disclaimer.
* PATENTS file, you can obtain it at www.aomedia.org/license/patent. *
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef __DAV1D_SRC_MSAC_H__ #ifndef __DAV1D_SRC_MSAC_H__
...@@ -15,21 +31,26 @@ ...@@ -15,21 +31,26 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
/* Using uint32_t should be faster on 32 bit systems, in theory, maybe */
typedef uint64_t ec_win;
typedef struct MsacContext { typedef struct MsacContext {
const uint8_t *buf, *end, *bptr; const uint8_t *buf_pos;
int32_t tell_offs; const uint8_t *buf_end;
uint32_t dif; ec_win dif;
uint16_t rng; uint16_t rng;
int16_t cnt; int cnt;
int error;
} MsacContext; } MsacContext;
#define EC_PROB_SHIFT 6
#define EC_BOOL_EPROB 256
void msac_init(MsacContext *c, const uint8_t *data, size_t sz); void msac_init(MsacContext *c, const uint8_t *data, size_t sz);
unsigned msac_decode_symbol(MsacContext *c, 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_bool(MsacContext *c, unsigned cdf); unsigned msac_decode_bool(MsacContext *s, unsigned f);
unsigned msac_decode_bools(MsacContext *c, unsigned l); unsigned msac_decode_bools(MsacContext *c, unsigned l);
int msac_decode_subexp(MsacContext *c, int ref, unsigned n, unsigned k); int msac_decode_subexp(MsacContext *c, int ref, int n, unsigned k);
int msac_decode_uniform(MsacContext *c, unsigned n); int msac_decode_uniform(MsacContext *c, unsigned n);
void update_cdf(uint16_t *cdf, unsigned val, unsigned nsymbs); void update_cdf(uint16_t *cdf, unsigned val, unsigned nsymbs);
...@@ -45,7 +66,7 @@ static inline unsigned msac_decode_symbol_adapt(MsacContext *const c, ...@@ -45,7 +66,7 @@ static inline unsigned msac_decode_symbol_adapt(MsacContext *const c,
static inline unsigned msac_decode_bool_adapt(MsacContext *const c, static inline unsigned msac_decode_bool_adapt(MsacContext *const c,
uint16_t *const cdf) uint16_t *const cdf)
{ {
const unsigned bit = msac_decode_bool(c, *cdf); const unsigned bit = msac_decode_bool(c, *cdf >> EC_PROB_SHIFT);
uint16_t bak_cdf[3] = { cdf[0], 0, cdf[1] }; uint16_t bak_cdf[3] = { cdf[0], 0, cdf[1] };
update_cdf(bak_cdf, bit, 2); update_cdf(bak_cdf, bit, 2);
cdf[0] = bak_cdf[0]; cdf[0] = bak_cdf[0];
......
...@@ -49,8 +49,8 @@ static unsigned read_golomb(MsacContext *const msac) { ...@@ -49,8 +49,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, 128 << 7) && len < 32) len++; while (!msac_decode_bool(msac, EC_BOOL_EPROB) && len < 32) len++;
while (len--) val = (val << 1) | msac_decode_bool(msac, 128 << 7); while (len--) val = (val << 1) | msac_decode_bool(msac, EC_BOOL_EPROB);
return val - 1; return val - 1;
}