ref: 33d16d816f127e2ce5a927d7d93d04a52b04a888
parent: c0351e1b470bbf16f147c4f7133dc47f23c7efe4
author: Rostislav Pehlivanov <[email protected]>
date: Fri Oct 26 16:33:15 EDT 2018
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.
--- a/src/decode.c
+++ b/src/decode.c
@@ -406,7 +406,7 @@
// find reused cache entries
int i = 0;
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];
const int n_used_cache = i;
@@ -470,13 +470,13 @@
uint16_t *const pal = f->frame_thread.pass ?
f->frame_thread.pal[((t->by >> 1) + (t->bx & 1)) * (f->b4_stride >> 1) +
((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);
int prev = pal[0] = msac_decode_bools(&ts->msac, f->cur.p.p.bpc);
const int max = (1 << f->cur.p.p.bpc) - 1;
for (int i = 1; i < b->pal_sz[1]; i++) {
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;
}
} else {
@@ -865,7 +865,7 @@
delta_q = msac_decode_bools(&ts->msac, n_bits) + 1 + (1 << n_bits);
}
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;
}
ts->last_qidx = iclip(ts->last_qidx + delta_q, 1, 255);
@@ -887,7 +887,7 @@
1 + (1 << n_bits);
}
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 *= 1 << f->frame_hdr.delta.lf.res_log2;
}
@@ -1497,7 +1497,7 @@
} else {
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)
printf("Post-seg/wedge[%d,wedge_idx=%d,sign=%d]: r=%d\n",
b->comp_type == COMP_INTER_WEDGE,
--- a/src/msac.c
+++ b/src/msac.c
@@ -1,12 +1,28 @@
/*
- * 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
- * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
- * 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
- * Media Patent License 1.0 was not distributed with this source code in the
- * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 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"
@@ -18,269 +34,101 @@
#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
-/*OPT: od_ec_window must be at least 32 bits, but if you have fast arithmetic
- on a larger type, you can speed up the decoder by using it here.*/
-typedef uint32_t od_ec_window;
+#define EC_WIN_SIZE (sizeof(ec_win) << 3)
-#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) {
- int s;
- od_ec_window dif;
- int16_t cnt;
- const unsigned char *bptr;
- const unsigned char *end;
- dif = dec->dif;
- cnt = dec->cnt;
- bptr = dec->bptr;
- end = dec->end;
- s = OD_EC_WINDOW_SIZE - 9 - (cnt + 15);
- for (; s >= 0 && bptr < end; s -= 8, bptr++) {
- 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;
+static inline void ctx_refill(MsacContext *s) {
+ const uint8_t *buf_pos = s->buf_pos;
+ const uint8_t *buf_end = s->buf_end;
+ int c = EC_WIN_SIZE - s->cnt - 24;
+ ec_win dif = s->dif;
+ while (c >= 0 && buf_pos < buf_end) {
+ dif ^= ((ec_win)*buf_pos++) << c;
+ c -= 8;
+ }
+ s->dif = dif;
+ s->cnt = EC_WIN_SIZE - c - 24;
+ s->buf_pos = buf_pos;
}
-/*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;
+/* 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. */
+static inline void ctx_norm(MsacContext *s, ec_win dif, uint32_t rng) {
+ const uint16_t d = 15 - (31 ^ clz(rng));
+ assert(rng <= 65535U);
+ s->cnt -= d;
+ s->dif = ((dif + 1) << d) - 1; /* Shift in 1s in the LSBs */
+ s->rng = rng << d;
+ if (s->cnt < 0)
+ ctx_refill(s);
}
-/*Initializes the decoder.
- buf: The input buffer to use.
- Return: 0 on success, or a negative value on error.*/
-static void od_ec_dec_init(od_ec_dec *dec, const unsigned char *buf,
- uint32_t storage) {
- dec->buf = buf;
- dec->tell_offs = 10 - (OD_EC_WINDOW_SIZE - 8);
- dec->end = buf + storage;
- dec->bptr = buf;
- dec->dif = ((od_ec_window)1 << (OD_EC_WINDOW_SIZE - 1)) - 1;
- dec->rng = 0x8000;
- dec->cnt = -15;
- dec->error = 0;
- od_ec_dec_refill(dec);
-}
+/* Decodes a symbol given an inverse cumulative distribution function (CDF)
+ * table in Q15. */
+unsigned msac_decode_symbol(MsacContext *const s, const uint16_t *const cdf,
+ const unsigned n_symbols)
+{
+ ec_win u, v = s->rng, r = s->rng >> 8;
+ const ec_win c = s->dif >> (EC_WIN_SIZE - 16);
+ unsigned ret = 0;
-/*Decode a single binary value.
- f: The probability that the bit is one, scaled by 32768.
- Return: The value decoded (0 or 1).*/
-static int od_ec_decode_bool_q15(od_ec_dec *dec, unsigned f) {
- od_ec_window dif;
- od_ec_window vw;
- unsigned r;
- unsigned r_new;
- 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);
-}
+ assert(!cdf[n_symbols - 1]);
-/*Decodes a symbol given an inverse cumulative distribution function (CDF)
- 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;
+ do {
+ u = v;
+ v = r * (cdf[ret++] >> EC_PROB_SHIFT);
+ v >>= 7 - EC_PROB_SHIFT;
+ v += EC_MIN_PROB * (n_symbols - ret);
+ } while (c < v);
- 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] >> 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);
-}
+ assert(u <= s->rng);
-void msac_init(MsacContext *const c,
- const uint8_t *const data, const size_t sz)
-{
- od_ec_dec_init(c, data, sz);
+ ctx_norm(s, s->dif - (v << (EC_WIN_SIZE - 16)), u - v);
+ return ret - 1;
}
-unsigned msac_decode_symbol(MsacContext *const c, const uint16_t *const cdf,
- const unsigned n_symbols)
-{
- return od_ec_decode_cdf_q15(c, cdf, n_symbols);
+/* Decode a single binary value.
+ * 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_bool(MsacContext *const c, const unsigned cdf) {
- return od_ec_decode_bool_q15(c, cdf);
-}
-
unsigned msac_decode_bools(MsacContext *const c, const unsigned l) {
int v = 0;
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;
}
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 a = 0;
int b = k;
- while ((2U << b) < n) {
- if (!msac_decode_bool(c, 128 << 7)) break;
+ while ((2 << b) < n) {
+ if (!msac_decode_bool(c, EC_BOOL_EPROB)) break;
b = k + i++;
a = (1 << b);
}
const unsigned v = msac_decode_bools(c, b) + a;
- return ref * 2U <= n ? inv_recenter(ref, v) :
- n - 1 - inv_recenter(n - 1 - ref, v);
+ return ref * 2 <= n ? inv_recenter(ref, v) :
+ n - 1 - inv_recenter(n - 1 - ref, v);
}
int msac_decode_uniform(MsacContext *const c, const unsigned n) {
@@ -287,14 +135,14 @@
assert(n > 0);
const int l = ulog2(n) + 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);
- 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) {
int rate;
- unsigned i, tmp;
+ unsigned i;
static const int nsymbs2speed[17] = {
0, 0, 1, 1, 2, 2, 2, 2, 2,
@@ -302,17 +150,23 @@
};
assert(nsymbs < 17);
rate = 3 + (cdf[nsymbs] > 15) + (cdf[nsymbs] > 31) + nsymbs2speed[nsymbs];
- tmp = 32768U;
- // Single loop (faster)
- for (i = 0; i < nsymbs - 1; ++i) {
- tmp = (i == val) ? 0 : tmp;
- if (tmp < cdf[i]) {
- cdf[i] -= ((cdf[i] - tmp) >> rate);
- } else {
- cdf[i] += ((tmp - cdf[i]) >> rate);
- }
- }
+ for (i = 0; i < val; ++i)
+ cdf[i] += (32768 - cdf[i]) >> rate;
+ for (i = val; i < nsymbs - 1; i++)
+ cdf[i] -= cdf[i] >> rate;
+
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);
}
--- a/src/msac.h
+++ b/src/msac.h
@@ -1,12 +1,28 @@
/*
- * 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
- * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
- * 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
- * Media Patent License 1.0 was not distributed with this source code in the
- * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * 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__
@@ -15,21 +31,26 @@
#include <stdint.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 {
- const uint8_t *buf, *end, *bptr;
- int32_t tell_offs;
- uint32_t dif;
+ const uint8_t *buf_pos;
+ const uint8_t *buf_end;
+ ec_win dif;
uint16_t rng;
- int16_t cnt;
- int error;
+ int cnt;
} MsacContext;
+#define EC_PROB_SHIFT 6
+#define EC_BOOL_EPROB 256
+
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);
-unsigned msac_decode_bool(MsacContext *c, unsigned cdf);
+unsigned msac_decode_bool(MsacContext *s, unsigned f);
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);
void update_cdf(uint16_t *cdf, unsigned val, unsigned nsymbs);
@@ -45,7 +66,7 @@
static inline unsigned msac_decode_bool_adapt(MsacContext *const c,
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] };
update_cdf(bak_cdf, bit, 2);
cdf[0] = bak_cdf[0];
--- a/src/recon_tmpl.c
+++ b/src/recon_tmpl.c
@@ -49,8 +49,8 @@
int len = 0;
unsigned val = 1;
- while (!msac_decode_bool(msac, 128 << 7) && len < 32) len++;
- while (len--) val = (val << 1) | msac_decode_bool(msac, 128 << 7);
+ while (!msac_decode_bool(msac, EC_BOOL_EPROB) && len < 32) len++;
+ while (len--) val = (val << 1) | msac_decode_bool(msac, EC_BOOL_EPROB);
return val - 1;
}
@@ -151,7 +151,7 @@
unsigned mask = eob >> 1;
if (eob_hi_bit) eob |= mask;
for (mask >>= 1; mask; mask >>= 1) {
- const int eob_bit = msac_decode_bool(&ts->msac, 128 << 7);
+ const int eob_bit = msac_decode_bool(&ts->msac, EC_BOOL_EPROB);
if (eob_bit) eob |= mask;
}
if (dbg)
@@ -226,7 +226,7 @@
dc_sign = sign ? 0 : 2;
dq = (dq_tbl[0] * qm_tbl[0] + 16) >> 5;
} else {
- sign = msac_decode_bool(&ts->msac, 128 << 7);
+ sign = msac_decode_bool(&ts->msac, EC_BOOL_EPROB);
if (dbg)
printf("Post-sign[%d=%d=%d]: r=%d\n", i, rc, sign, ts->msac.rng);
dq = (dq_tbl[1] * qm_tbl[rc] + 16) >> 5;