1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2001-2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include <assert.h>
13*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/entdec.h"
14*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/prob.h"
15*77c1e3ccSAndroid Build Coastguard Worker
16*77c1e3ccSAndroid Build Coastguard Worker /*A range decoder.
17*77c1e3ccSAndroid Build Coastguard Worker This is an entropy decoder based upon \cite{Mar79}, which is itself a
18*77c1e3ccSAndroid Build Coastguard Worker rediscovery of the FIFO arithmetic code introduced by \cite{Pas76}.
19*77c1e3ccSAndroid Build Coastguard Worker It is very similar to arithmetic encoding, except that encoding is done with
20*77c1e3ccSAndroid Build Coastguard Worker digits in any base, instead of with bits, and so it is faster when using
21*77c1e3ccSAndroid Build Coastguard Worker larger bases (i.e.: a byte).
22*77c1e3ccSAndroid Build Coastguard Worker The author claims an average waste of $\frac{1}{2}\log_b(2b)$ bits, where $b$
23*77c1e3ccSAndroid Build Coastguard Worker is the base, longer than the theoretical optimum, but to my knowledge there
24*77c1e3ccSAndroid Build Coastguard Worker is no published justification for this claim.
25*77c1e3ccSAndroid Build Coastguard Worker This only seems true when using near-infinite precision arithmetic so that
26*77c1e3ccSAndroid Build Coastguard Worker the process is carried out with no rounding errors.
27*77c1e3ccSAndroid Build Coastguard Worker
28*77c1e3ccSAndroid Build Coastguard Worker An excellent description of implementation details is available at
29*77c1e3ccSAndroid Build Coastguard Worker http://www.arturocampos.com/ac_range.html
30*77c1e3ccSAndroid Build Coastguard Worker A recent work \cite{MNW98} which proposes several changes to arithmetic
31*77c1e3ccSAndroid Build Coastguard Worker encoding for efficiency actually re-discovers many of the principles
32*77c1e3ccSAndroid Build Coastguard Worker behind range encoding, and presents a good theoretical analysis of them.
33*77c1e3ccSAndroid Build Coastguard Worker
34*77c1e3ccSAndroid Build Coastguard Worker End of stream is handled by writing out the smallest number of bits that
35*77c1e3ccSAndroid Build Coastguard Worker ensures that the stream will be correctly decoded regardless of the value of
36*77c1e3ccSAndroid Build Coastguard Worker any subsequent bits.
37*77c1e3ccSAndroid Build Coastguard Worker od_ec_dec_tell() can be used to determine how many bits were needed to decode
38*77c1e3ccSAndroid Build Coastguard Worker all the symbols thus far; other data can be packed in the remaining bits of
39*77c1e3ccSAndroid Build Coastguard Worker the input buffer.
40*77c1e3ccSAndroid Build Coastguard Worker @PHDTHESIS{Pas76,
41*77c1e3ccSAndroid Build Coastguard Worker author="Richard Clark Pasco",
42*77c1e3ccSAndroid Build Coastguard Worker title="Source coding algorithms for fast data compression",
43*77c1e3ccSAndroid Build Coastguard Worker school="Dept. of Electrical Engineering, Stanford University",
44*77c1e3ccSAndroid Build Coastguard Worker address="Stanford, CA",
45*77c1e3ccSAndroid Build Coastguard Worker month=May,
46*77c1e3ccSAndroid Build Coastguard Worker year=1976,
47*77c1e3ccSAndroid Build Coastguard Worker URL="http://www.richpasco.org/scaffdc.pdf"
48*77c1e3ccSAndroid Build Coastguard Worker }
49*77c1e3ccSAndroid Build Coastguard Worker @INPROCEEDINGS{Mar79,
50*77c1e3ccSAndroid Build Coastguard Worker author="Martin, G.N.N.",
51*77c1e3ccSAndroid Build Coastguard Worker title="Range encoding: an algorithm for removing redundancy from a digitised
52*77c1e3ccSAndroid Build Coastguard Worker message",
53*77c1e3ccSAndroid Build Coastguard Worker booktitle="Video & Data Recording Conference",
54*77c1e3ccSAndroid Build Coastguard Worker year=1979,
55*77c1e3ccSAndroid Build Coastguard Worker address="Southampton",
56*77c1e3ccSAndroid Build Coastguard Worker month=Jul,
57*77c1e3ccSAndroid Build Coastguard Worker URL="http://www.compressconsult.com/rangecoder/rngcod.pdf.gz"
58*77c1e3ccSAndroid Build Coastguard Worker }
59*77c1e3ccSAndroid Build Coastguard Worker @ARTICLE{MNW98,
60*77c1e3ccSAndroid Build Coastguard Worker author="Alistair Moffat and Radford Neal and Ian H. Witten",
61*77c1e3ccSAndroid Build Coastguard Worker title="Arithmetic Coding Revisited",
62*77c1e3ccSAndroid Build Coastguard Worker journal="{ACM} Transactions on Information Systems",
63*77c1e3ccSAndroid Build Coastguard Worker year=1998,
64*77c1e3ccSAndroid Build Coastguard Worker volume=16,
65*77c1e3ccSAndroid Build Coastguard Worker number=3,
66*77c1e3ccSAndroid Build Coastguard Worker pages="256--294",
67*77c1e3ccSAndroid Build Coastguard Worker month=Jul,
68*77c1e3ccSAndroid Build Coastguard Worker URL="http://researchcommons.waikato.ac.nz/bitstream/handle/10289/78/content.pdf"
69*77c1e3ccSAndroid Build Coastguard Worker }*/
70*77c1e3ccSAndroid Build Coastguard Worker
71*77c1e3ccSAndroid Build Coastguard Worker /*This is meant to be a large, positive constant that can still be efficiently
72*77c1e3ccSAndroid Build Coastguard Worker loaded as an immediate (on platforms like ARM, for example).
73*77c1e3ccSAndroid Build Coastguard Worker Even relatively modest values like 100 would work fine.*/
74*77c1e3ccSAndroid Build Coastguard Worker #define OD_EC_LOTS_OF_BITS (0x4000)
75*77c1e3ccSAndroid Build Coastguard Worker
76*77c1e3ccSAndroid Build Coastguard Worker /*The return value of od_ec_dec_tell does not change across an od_ec_dec_refill
77*77c1e3ccSAndroid Build Coastguard Worker call.*/
od_ec_dec_refill(od_ec_dec * dec)78*77c1e3ccSAndroid Build Coastguard Worker static void od_ec_dec_refill(od_ec_dec *dec) {
79*77c1e3ccSAndroid Build Coastguard Worker int s;
80*77c1e3ccSAndroid Build Coastguard Worker od_ec_window dif;
81*77c1e3ccSAndroid Build Coastguard Worker int16_t cnt;
82*77c1e3ccSAndroid Build Coastguard Worker const unsigned char *bptr;
83*77c1e3ccSAndroid Build Coastguard Worker const unsigned char *end;
84*77c1e3ccSAndroid Build Coastguard Worker dif = dec->dif;
85*77c1e3ccSAndroid Build Coastguard Worker cnt = dec->cnt;
86*77c1e3ccSAndroid Build Coastguard Worker bptr = dec->bptr;
87*77c1e3ccSAndroid Build Coastguard Worker end = dec->end;
88*77c1e3ccSAndroid Build Coastguard Worker s = OD_EC_WINDOW_SIZE - 9 - (cnt + 15);
89*77c1e3ccSAndroid Build Coastguard Worker for (; s >= 0 && bptr < end; s -= 8, bptr++) {
90*77c1e3ccSAndroid Build Coastguard Worker /*Each time a byte is inserted into the window (dif), bptr advances and cnt
91*77c1e3ccSAndroid Build Coastguard Worker is incremented by 8, so the total number of consumed bits (the return
92*77c1e3ccSAndroid Build Coastguard Worker value of od_ec_dec_tell) does not change.*/
93*77c1e3ccSAndroid Build Coastguard Worker assert(s <= OD_EC_WINDOW_SIZE - 8);
94*77c1e3ccSAndroid Build Coastguard Worker dif ^= (od_ec_window)bptr[0] << s;
95*77c1e3ccSAndroid Build Coastguard Worker cnt += 8;
96*77c1e3ccSAndroid Build Coastguard Worker }
97*77c1e3ccSAndroid Build Coastguard Worker if (bptr >= end) {
98*77c1e3ccSAndroid Build Coastguard Worker /*We've reached the end of the buffer. It is perfectly valid for us to need
99*77c1e3ccSAndroid Build Coastguard Worker to fill the window with additional bits past the end of the buffer (and
100*77c1e3ccSAndroid Build Coastguard Worker this happens in normal operation). These bits should all just be taken
101*77c1e3ccSAndroid Build Coastguard Worker as zero. But we cannot increment bptr past 'end' (this is undefined
102*77c1e3ccSAndroid Build Coastguard Worker behavior), so we start to increment dec->tell_offs. We also don't want
103*77c1e3ccSAndroid Build Coastguard Worker to keep testing bptr against 'end', so we set cnt to OD_EC_LOTS_OF_BITS
104*77c1e3ccSAndroid Build Coastguard Worker and adjust dec->tell_offs so that the total number of unconsumed bits in
105*77c1e3ccSAndroid Build Coastguard Worker the window (dec->cnt - dec->tell_offs) does not change. This effectively
106*77c1e3ccSAndroid Build Coastguard Worker puts lots of zero bits into the window, and means we won't try to refill
107*77c1e3ccSAndroid Build Coastguard Worker it from the buffer for a very long time (at which point we'll put lots
108*77c1e3ccSAndroid Build Coastguard Worker of zero bits into the window again).*/
109*77c1e3ccSAndroid Build Coastguard Worker dec->tell_offs += OD_EC_LOTS_OF_BITS - cnt;
110*77c1e3ccSAndroid Build Coastguard Worker cnt = OD_EC_LOTS_OF_BITS;
111*77c1e3ccSAndroid Build Coastguard Worker }
112*77c1e3ccSAndroid Build Coastguard Worker dec->dif = dif;
113*77c1e3ccSAndroid Build Coastguard Worker dec->cnt = cnt;
114*77c1e3ccSAndroid Build Coastguard Worker dec->bptr = bptr;
115*77c1e3ccSAndroid Build Coastguard Worker }
116*77c1e3ccSAndroid Build Coastguard Worker
117*77c1e3ccSAndroid Build Coastguard Worker /*Takes updated dif and range values, renormalizes them so that
118*77c1e3ccSAndroid Build Coastguard Worker 32768 <= rng < 65536 (reading more bytes from the stream into dif if
119*77c1e3ccSAndroid Build Coastguard Worker necessary), and stores them back in the decoder context.
120*77c1e3ccSAndroid Build Coastguard Worker dif: The new value of dif.
121*77c1e3ccSAndroid Build Coastguard Worker rng: The new value of the range.
122*77c1e3ccSAndroid Build Coastguard Worker ret: The value to return.
123*77c1e3ccSAndroid Build Coastguard Worker Return: ret.
124*77c1e3ccSAndroid Build Coastguard Worker This allows the compiler to jump to this function via a tail-call.*/
od_ec_dec_normalize(od_ec_dec * dec,od_ec_window dif,unsigned rng,int ret)125*77c1e3ccSAndroid Build Coastguard Worker static int od_ec_dec_normalize(od_ec_dec *dec, od_ec_window dif, unsigned rng,
126*77c1e3ccSAndroid Build Coastguard Worker int ret) {
127*77c1e3ccSAndroid Build Coastguard Worker int d;
128*77c1e3ccSAndroid Build Coastguard Worker assert(rng <= 65535U);
129*77c1e3ccSAndroid Build Coastguard Worker /*The number of leading zeros in the 16-bit binary representation of rng.*/
130*77c1e3ccSAndroid Build Coastguard Worker d = 16 - OD_ILOG_NZ(rng);
131*77c1e3ccSAndroid Build Coastguard Worker /*d bits in dec->dif are consumed.*/
132*77c1e3ccSAndroid Build Coastguard Worker dec->cnt -= d;
133*77c1e3ccSAndroid Build Coastguard Worker /*This is equivalent to shifting in 1's instead of 0's.*/
134*77c1e3ccSAndroid Build Coastguard Worker dec->dif = ((dif + 1) << d) - 1;
135*77c1e3ccSAndroid Build Coastguard Worker dec->rng = rng << d;
136*77c1e3ccSAndroid Build Coastguard Worker if (dec->cnt < 0) od_ec_dec_refill(dec);
137*77c1e3ccSAndroid Build Coastguard Worker return ret;
138*77c1e3ccSAndroid Build Coastguard Worker }
139*77c1e3ccSAndroid Build Coastguard Worker
140*77c1e3ccSAndroid Build Coastguard Worker /*Initializes the decoder.
141*77c1e3ccSAndroid Build Coastguard Worker buf: The input buffer to use.
142*77c1e3ccSAndroid Build Coastguard Worker storage: The size in bytes of the input buffer.*/
od_ec_dec_init(od_ec_dec * dec,const unsigned char * buf,uint32_t storage)143*77c1e3ccSAndroid Build Coastguard Worker void od_ec_dec_init(od_ec_dec *dec, const unsigned char *buf,
144*77c1e3ccSAndroid Build Coastguard Worker uint32_t storage) {
145*77c1e3ccSAndroid Build Coastguard Worker dec->buf = buf;
146*77c1e3ccSAndroid Build Coastguard Worker dec->tell_offs = 10 - (OD_EC_WINDOW_SIZE - 8);
147*77c1e3ccSAndroid Build Coastguard Worker dec->end = buf + storage;
148*77c1e3ccSAndroid Build Coastguard Worker dec->bptr = buf;
149*77c1e3ccSAndroid Build Coastguard Worker dec->dif = ((od_ec_window)1 << (OD_EC_WINDOW_SIZE - 1)) - 1;
150*77c1e3ccSAndroid Build Coastguard Worker dec->rng = 0x8000;
151*77c1e3ccSAndroid Build Coastguard Worker dec->cnt = -15;
152*77c1e3ccSAndroid Build Coastguard Worker od_ec_dec_refill(dec);
153*77c1e3ccSAndroid Build Coastguard Worker }
154*77c1e3ccSAndroid Build Coastguard Worker
155*77c1e3ccSAndroid Build Coastguard Worker /*Decode a single binary value.
156*77c1e3ccSAndroid Build Coastguard Worker f: The probability that the bit is one, scaled by 32768.
157*77c1e3ccSAndroid Build Coastguard Worker Return: The value decoded (0 or 1).*/
od_ec_decode_bool_q15(od_ec_dec * dec,unsigned f)158*77c1e3ccSAndroid Build Coastguard Worker int od_ec_decode_bool_q15(od_ec_dec *dec, unsigned f) {
159*77c1e3ccSAndroid Build Coastguard Worker od_ec_window dif;
160*77c1e3ccSAndroid Build Coastguard Worker od_ec_window vw;
161*77c1e3ccSAndroid Build Coastguard Worker unsigned r;
162*77c1e3ccSAndroid Build Coastguard Worker unsigned r_new;
163*77c1e3ccSAndroid Build Coastguard Worker unsigned v;
164*77c1e3ccSAndroid Build Coastguard Worker int ret;
165*77c1e3ccSAndroid Build Coastguard Worker assert(0 < f);
166*77c1e3ccSAndroid Build Coastguard Worker assert(f < 32768U);
167*77c1e3ccSAndroid Build Coastguard Worker dif = dec->dif;
168*77c1e3ccSAndroid Build Coastguard Worker r = dec->rng;
169*77c1e3ccSAndroid Build Coastguard Worker assert(dif >> (OD_EC_WINDOW_SIZE - 16) < r);
170*77c1e3ccSAndroid Build Coastguard Worker assert(32768U <= r);
171*77c1e3ccSAndroid Build Coastguard Worker v = ((r >> 8) * (uint32_t)(f >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT));
172*77c1e3ccSAndroid Build Coastguard Worker v += EC_MIN_PROB;
173*77c1e3ccSAndroid Build Coastguard Worker vw = (od_ec_window)v << (OD_EC_WINDOW_SIZE - 16);
174*77c1e3ccSAndroid Build Coastguard Worker ret = 1;
175*77c1e3ccSAndroid Build Coastguard Worker r_new = v;
176*77c1e3ccSAndroid Build Coastguard Worker if (dif >= vw) {
177*77c1e3ccSAndroid Build Coastguard Worker r_new = r - v;
178*77c1e3ccSAndroid Build Coastguard Worker dif -= vw;
179*77c1e3ccSAndroid Build Coastguard Worker ret = 0;
180*77c1e3ccSAndroid Build Coastguard Worker }
181*77c1e3ccSAndroid Build Coastguard Worker return od_ec_dec_normalize(dec, dif, r_new, ret);
182*77c1e3ccSAndroid Build Coastguard Worker }
183*77c1e3ccSAndroid Build Coastguard Worker
184*77c1e3ccSAndroid Build Coastguard Worker /*Decodes a symbol given an inverse cumulative distribution function (CDF)
185*77c1e3ccSAndroid Build Coastguard Worker table in Q15.
186*77c1e3ccSAndroid Build Coastguard Worker icdf: CDF_PROB_TOP minus the CDF, such that symbol s falls in the range
187*77c1e3ccSAndroid Build Coastguard Worker [s > 0 ? (CDF_PROB_TOP - icdf[s - 1]) : 0, CDF_PROB_TOP - icdf[s]).
188*77c1e3ccSAndroid Build Coastguard Worker The values must be monotonically non-increasing, and icdf[nsyms - 1]
189*77c1e3ccSAndroid Build Coastguard Worker must be 0.
190*77c1e3ccSAndroid Build Coastguard Worker nsyms: The number of symbols in the alphabet.
191*77c1e3ccSAndroid Build Coastguard Worker This should be at most 16.
192*77c1e3ccSAndroid Build Coastguard Worker Return: The decoded symbol s.*/
od_ec_decode_cdf_q15(od_ec_dec * dec,const uint16_t * icdf,int nsyms)193*77c1e3ccSAndroid Build Coastguard Worker int od_ec_decode_cdf_q15(od_ec_dec *dec, const uint16_t *icdf, int nsyms) {
194*77c1e3ccSAndroid Build Coastguard Worker od_ec_window dif;
195*77c1e3ccSAndroid Build Coastguard Worker unsigned r;
196*77c1e3ccSAndroid Build Coastguard Worker unsigned c;
197*77c1e3ccSAndroid Build Coastguard Worker unsigned u;
198*77c1e3ccSAndroid Build Coastguard Worker unsigned v;
199*77c1e3ccSAndroid Build Coastguard Worker int ret;
200*77c1e3ccSAndroid Build Coastguard Worker (void)nsyms;
201*77c1e3ccSAndroid Build Coastguard Worker dif = dec->dif;
202*77c1e3ccSAndroid Build Coastguard Worker r = dec->rng;
203*77c1e3ccSAndroid Build Coastguard Worker const int N = nsyms - 1;
204*77c1e3ccSAndroid Build Coastguard Worker
205*77c1e3ccSAndroid Build Coastguard Worker assert(dif >> (OD_EC_WINDOW_SIZE - 16) < r);
206*77c1e3ccSAndroid Build Coastguard Worker assert(icdf[nsyms - 1] == OD_ICDF(CDF_PROB_TOP));
207*77c1e3ccSAndroid Build Coastguard Worker assert(32768U <= r);
208*77c1e3ccSAndroid Build Coastguard Worker assert(7 - EC_PROB_SHIFT >= 0);
209*77c1e3ccSAndroid Build Coastguard Worker c = (unsigned)(dif >> (OD_EC_WINDOW_SIZE - 16));
210*77c1e3ccSAndroid Build Coastguard Worker v = r;
211*77c1e3ccSAndroid Build Coastguard Worker ret = -1;
212*77c1e3ccSAndroid Build Coastguard Worker do {
213*77c1e3ccSAndroid Build Coastguard Worker u = v;
214*77c1e3ccSAndroid Build Coastguard Worker v = ((r >> 8) * (uint32_t)(icdf[++ret] >> EC_PROB_SHIFT) >>
215*77c1e3ccSAndroid Build Coastguard Worker (7 - EC_PROB_SHIFT));
216*77c1e3ccSAndroid Build Coastguard Worker v += EC_MIN_PROB * (N - ret);
217*77c1e3ccSAndroid Build Coastguard Worker } while (c < v);
218*77c1e3ccSAndroid Build Coastguard Worker assert(v < u);
219*77c1e3ccSAndroid Build Coastguard Worker assert(u <= r);
220*77c1e3ccSAndroid Build Coastguard Worker r = u - v;
221*77c1e3ccSAndroid Build Coastguard Worker dif -= (od_ec_window)v << (OD_EC_WINDOW_SIZE - 16);
222*77c1e3ccSAndroid Build Coastguard Worker return od_ec_dec_normalize(dec, dif, r, ret);
223*77c1e3ccSAndroid Build Coastguard Worker }
224*77c1e3ccSAndroid Build Coastguard Worker
225*77c1e3ccSAndroid Build Coastguard Worker /*Returns the number of bits "used" by the decoded symbols so far.
226*77c1e3ccSAndroid Build Coastguard Worker This same number can be computed in either the encoder or the decoder, and is
227*77c1e3ccSAndroid Build Coastguard Worker suitable for making coding decisions.
228*77c1e3ccSAndroid Build Coastguard Worker Return: The number of bits.
229*77c1e3ccSAndroid Build Coastguard Worker This will always be slightly larger than the exact value (e.g., all
230*77c1e3ccSAndroid Build Coastguard Worker rounding error is in the positive direction).*/
od_ec_dec_tell(const od_ec_dec * dec)231*77c1e3ccSAndroid Build Coastguard Worker int od_ec_dec_tell(const od_ec_dec *dec) {
232*77c1e3ccSAndroid Build Coastguard Worker /*There is a window of bits stored in dec->dif. The difference
233*77c1e3ccSAndroid Build Coastguard Worker (dec->bptr - dec->buf) tells us how many bytes have been read into this
234*77c1e3ccSAndroid Build Coastguard Worker window. The difference (dec->cnt - dec->tell_offs) tells us how many of
235*77c1e3ccSAndroid Build Coastguard Worker the bits in that window remain unconsumed.*/
236*77c1e3ccSAndroid Build Coastguard Worker return (int)((dec->bptr - dec->buf) * 8 - dec->cnt + dec->tell_offs);
237*77c1e3ccSAndroid Build Coastguard Worker }
238*77c1e3ccSAndroid Build Coastguard Worker
239*77c1e3ccSAndroid Build Coastguard Worker /*Returns the number of bits "used" by the decoded symbols so far.
240*77c1e3ccSAndroid Build Coastguard Worker This same number can be computed in either the encoder or the decoder, and is
241*77c1e3ccSAndroid Build Coastguard Worker suitable for making coding decisions.
242*77c1e3ccSAndroid Build Coastguard Worker Return: The number of bits scaled by 2**OD_BITRES.
243*77c1e3ccSAndroid Build Coastguard Worker This will always be slightly larger than the exact value (e.g., all
244*77c1e3ccSAndroid Build Coastguard Worker rounding error is in the positive direction).*/
od_ec_dec_tell_frac(const od_ec_dec * dec)245*77c1e3ccSAndroid Build Coastguard Worker uint32_t od_ec_dec_tell_frac(const od_ec_dec *dec) {
246*77c1e3ccSAndroid Build Coastguard Worker return od_ec_tell_frac(od_ec_dec_tell(dec), dec->rng);
247*77c1e3ccSAndroid Build Coastguard Worker }
248