blob: 49b176cd8076422ded66af24e6f45b2b96a330c0 [file] [log] [blame]
Yushin Cho77bba8d2016-11-04 16:36:56 -07001/*
2 * Copyright (c) 2001-2016, Alliance for Open Media. All rights reserved
3 *
4 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10 */
Nathan E. Egge1078dee2016-03-06 10:59:29 -050011
12#ifdef HAVE_CONFIG_H
Yaowu Xu931bc2a2016-10-14 13:53:51 -070013#include "./config.h"
Nathan E. Egge1078dee2016-03-06 10:59:29 -050014#endif
15
Yaowu Xu931bc2a2016-10-14 13:53:51 -070016#include "aom_dsp/entdec.h"
Nathan E. Egge1078dee2016-03-06 10:59:29 -050017
18/*A range decoder.
19 This is an entropy decoder based upon \cite{Mar79}, which is itself a
20 rediscovery of the FIFO arithmetic code introduced by \cite{Pas76}.
21 It is very similar to arithmetic encoding, except that encoding is done with
22 digits in any base, instead of with bits, and so it is faster when using
23 larger bases (i.e.: a byte).
24 The author claims an average waste of $\frac{1}{2}\log_b(2b)$ bits, where $b$
25 is the base, longer than the theoretical optimum, but to my knowledge there
26 is no published justification for this claim.
27 This only seems true when using near-infinite precision arithmetic so that
28 the process is carried out with no rounding errors.
29
30 An excellent description of implementation details is available at
31 http://www.arturocampos.com/ac_range.html
32 A recent work \cite{MNW98} which proposes several changes to arithmetic
33 encoding for efficiency actually re-discovers many of the principles
34 behind range encoding, and presents a good theoretical analysis of them.
35
36 End of stream is handled by writing out the smallest number of bits that
37 ensures that the stream will be correctly decoded regardless of the value of
38 any subsequent bits.
39 od_ec_dec_tell() can be used to determine how many bits were needed to decode
40 all the symbols thus far; other data can be packed in the remaining bits of
41 the input buffer.
42 @PHDTHESIS{Pas76,
43 author="Richard Clark Pasco",
44 title="Source coding algorithms for fast data compression",
45 school="Dept. of Electrical Engineering, Stanford University",
46 address="Stanford, CA",
47 month=May,
48 year=1976,
49 URL="http://www.richpasco.org/scaffdc.pdf"
50 }
51 @INPROCEEDINGS{Mar79,
52 author="Martin, G.N.N.",
53 title="Range encoding: an algorithm for removing redundancy from a digitised
54 message",
55 booktitle="Video & Data Recording Conference",
56 year=1979,
57 address="Southampton",
58 month=Jul,
59 URL="http://www.compressconsult.com/rangecoder/rngcod.pdf.gz"
60 }
61 @ARTICLE{MNW98,
62 author="Alistair Moffat and Radford Neal and Ian H. Witten",
63 title="Arithmetic Coding Revisited",
64 journal="{ACM} Transactions on Information Systems",
65 year=1998,
66 volume=16,
67 number=3,
68 pages="256--294",
69 month=Jul,
70 URL="http://researchcommons.waikato.ac.nz/bitstream/handle/10289/78/content.pdf"
71 }*/
72
Nathan E. Egge1078dee2016-03-06 10:59:29 -050073/*This is meant to be a large, positive constant that can still be efficiently
74 loaded as an immediate (on platforms like ARM, for example).
75 Even relatively modest values like 100 would work fine.*/
76#define OD_EC_LOTS_OF_BITS (0x4000)
77
78static void od_ec_dec_refill(od_ec_dec *dec) {
79 int s;
80 od_ec_window dif;
81 int16_t cnt;
82 const unsigned char *bptr;
83 const unsigned char *end;
84 dif = dec->dif;
85 cnt = dec->cnt;
86 bptr = dec->bptr;
87 end = dec->end;
88 s = OD_EC_WINDOW_SIZE - 9 - (cnt + 15);
89 for (; s >= 0 && bptr < end; s -= 8, bptr++) {
90 OD_ASSERT(s <= OD_EC_WINDOW_SIZE - 8);
Timothy B. Terriberry881f1092017-03-07 20:03:09 -080091 dif ^= (od_ec_window)bptr[0] << s;
Nathan E. Egge1078dee2016-03-06 10:59:29 -050092 cnt += 8;
93 }
94 if (bptr >= end) {
95 dec->tell_offs += OD_EC_LOTS_OF_BITS - cnt;
96 cnt = OD_EC_LOTS_OF_BITS;
97 }
98 dec->dif = dif;
99 dec->cnt = cnt;
100 dec->bptr = bptr;
101}
102
103/*Takes updated dif and range values, renormalizes them so that
104 32768 <= rng < 65536 (reading more bytes from the stream into dif if
105 necessary), and stores them back in the decoder context.
106 dif: The new value of dif.
107 rng: The new value of the range.
108 ret: The value to return.
109 Return: ret.
110 This allows the compiler to jump to this function via a tail-call.*/
Michael Bebenita63b44c42016-08-23 16:03:39 -0700111static int od_ec_dec_normalize(od_ec_dec *dec, od_ec_window dif, unsigned rng,
112 int ret) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500113 int d;
114 OD_ASSERT(rng <= 65535U);
115 d = 16 - OD_ILOG_NZ(rng);
116 dec->cnt -= d;
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800117#if CONFIG_EC_SMALLMUL
118 /*This is equivalent to shifting in 1's instead of 0's.*/
119 dec->dif = ((dif + 1) << d) - 1;
120#else
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500121 dec->dif = dif << d;
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800122#endif
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500123 dec->rng = rng << d;
124 if (dec->cnt < 0) od_ec_dec_refill(dec);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500125 return ret;
126}
127
128/*Initializes the decoder.
129 buf: The input buffer to use.
130 Return: 0 on success, or a negative value on error.*/
131void od_ec_dec_init(od_ec_dec *dec, const unsigned char *buf,
132 uint32_t storage) {
133 dec->buf = buf;
134 dec->eptr = buf + storage;
135 dec->end_window = 0;
136 dec->nend_bits = 0;
137 dec->tell_offs = 10 - (OD_EC_WINDOW_SIZE - 8);
138 dec->end = buf + storage;
139 dec->bptr = buf;
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800140#if CONFIG_EC_SMALLMUL
141 dec->dif = ((od_ec_window)1 << (OD_EC_WINDOW_SIZE - 1)) - 1;
142#else
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500143 dec->dif = 0;
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800144#endif
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500145 dec->rng = 0x8000;
146 dec->cnt = -15;
147 dec->error = 0;
148 od_ec_dec_refill(dec);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500149}
150
Timothy B. Terriberryead52872017-03-07 20:27:34 -0800151/*Decode a single binary value.
152 {EC_SMALLMUL} f: The probability that the bit is one, scaled by 32768.
153 {else} f: The probability that the bit is zero, scaled by 32768.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500154 Return: The value decoded (0 or 1).*/
Timothy B. Terriberryead52872017-03-07 20:27:34 -0800155int od_ec_decode_bool_q15(od_ec_dec *dec, unsigned f) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500156 od_ec_window dif;
157 od_ec_window vw;
158 unsigned r;
Nathan E. Egge5357dca2016-09-09 14:21:56 -0400159 unsigned r_new;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500160 unsigned v;
161 int ret;
Timothy B. Terriberryead52872017-03-07 20:27:34 -0800162 OD_ASSERT(0 < f);
163 OD_ASSERT(f < 32768U);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500164 dif = dec->dif;
165 r = dec->rng;
166 OD_ASSERT(dif >> (OD_EC_WINDOW_SIZE - 16) < r);
167 OD_ASSERT(32768U <= r);
Timothy B. Terriberryb1c57602017-02-16 10:53:39 -0800168#if CONFIG_EC_SMALLMUL
Timothy B. Terriberryead52872017-03-07 20:27:34 -0800169 v = (r >> 8) * (uint32_t)f >> 7;
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800170 vw = (od_ec_window)v << (OD_EC_WINDOW_SIZE - 16);
171 ret = 1;
172 r_new = v;
173 if (dif >= vw) {
174 r_new = r - v;
175 dif -= vw;
176 ret = 0;
177 }
Timothy B. Terriberryb1c57602017-02-16 10:53:39 -0800178#else
Timothy B. Terriberryead52872017-03-07 20:27:34 -0800179 v = f * (uint32_t)r >> 15;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500180 vw = (od_ec_window)v << (OD_EC_WINDOW_SIZE - 16);
Nathan E. Egge5357dca2016-09-09 14:21:56 -0400181 ret = 0;
182 r_new = v;
183 if (dif >= vw) {
184 r_new = r - v;
185 dif -= vw;
186 ret = 1;
187 }
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800188#endif
Nathan E. Egge5357dca2016-09-09 14:21:56 -0400189 return od_ec_dec_normalize(dec, dif, r_new, ret);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500190}
191
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500192/*Decodes a symbol given a cumulative distribution function (CDF) table in Q15.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500193 cdf: The CDF, such that symbol s falls in the range
194 [s > 0 ? cdf[s - 1] : 0, cdf[s]).
195 The values must be monotonically non-increasing, and cdf[nsyms - 1]
196 must be 32768.
Timothy B. Terriberryead52872017-03-07 20:27:34 -0800197 {EC_SMALLMUL}: The CDF contains 32768 minus those values.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500198 nsyms: The number of symbols in the alphabet.
199 This should be at most 16.
200 Return: The decoded symbol s.*/
Michael Bebenita63b44c42016-08-23 16:03:39 -0700201int od_ec_decode_cdf_q15(od_ec_dec *dec, const uint16_t *cdf, int nsyms) {
Timothy B. Terriberry561eb7c2017-03-07 18:06:44 -0800202 od_ec_window dif;
203 unsigned r;
204 unsigned c;
205 unsigned u;
206 unsigned v;
207 int ret;
208 (void)nsyms;
209 dif = dec->dif;
210 r = dec->rng;
211 OD_ASSERT(dif >> (OD_EC_WINDOW_SIZE - 16) < r);
Timothy B. Terriberryead52872017-03-07 20:27:34 -0800212 OD_ASSERT(cdf[nsyms - 1] == OD_ICDF(32768U));
Timothy B. Terriberry561eb7c2017-03-07 18:06:44 -0800213 OD_ASSERT(32768U <= r);
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800214#if CONFIG_EC_SMALLMUL
215 c = (unsigned)(dif >> (OD_EC_WINDOW_SIZE - 16));
216 v = r;
217 ret = -1;
218 do {
219 u = v;
Timothy B. Terriberryead52872017-03-07 20:27:34 -0800220 v = (r >> 8) * (uint32_t)cdf[++ret] >> 7;
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800221 } while (c < v);
222 OD_ASSERT(v < u);
223 OD_ASSERT(u <= r);
224 r = u - v;
225 dif -= (od_ec_window)v << (OD_EC_WINDOW_SIZE - 16);
226#else
Timothy B. Terriberry561eb7c2017-03-07 18:06:44 -0800227 c = (unsigned)(dif >> (OD_EC_WINDOW_SIZE - 16));
228 v = 0;
229 ret = -1;
230 do {
231 u = v;
232 v = cdf[++ret] * (uint32_t)r >> 15;
233 } while (v <= c);
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800234 OD_ASSERT(u < v);
Timothy B. Terriberry561eb7c2017-03-07 18:06:44 -0800235 OD_ASSERT(v <= r);
236 r = v - u;
237 dif -= (od_ec_window)u << (OD_EC_WINDOW_SIZE - 16);
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800238#endif
Timothy B. Terriberry561eb7c2017-03-07 18:06:44 -0800239 return od_ec_dec_normalize(dec, dif, r, ret);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500240}
241
Nathan E. Egge24f1a902017-02-14 13:29:44 -0500242#if CONFIG_RAWBITS
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500243/*Extracts a sequence of raw bits from the stream.
244 The bits must have been encoded with od_ec_enc_bits().
245 ftb: The number of bits to extract.
246 This must be between 0 and 25, inclusive.
247 Return: The decoded bits.*/
Yushin Cho77bba8d2016-11-04 16:36:56 -0700248uint32_t od_ec_dec_bits_(od_ec_dec *dec, unsigned ftb) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500249 od_ec_window window;
250 int available;
251 uint32_t ret;
252 OD_ASSERT(ftb <= 25);
253 window = dec->end_window;
254 available = dec->nend_bits;
255 if ((unsigned)available < ftb) {
256 const unsigned char *buf;
257 const unsigned char *eptr;
258 buf = dec->buf;
259 eptr = dec->eptr;
260 OD_ASSERT(available <= OD_EC_WINDOW_SIZE - 8);
261 do {
262 if (eptr <= buf) {
263 dec->tell_offs += OD_EC_LOTS_OF_BITS - available;
264 available = OD_EC_LOTS_OF_BITS;
265 break;
266 }
267 window |= (od_ec_window) * --eptr << available;
268 available += 8;
269 } while (available <= OD_EC_WINDOW_SIZE - 8);
270 dec->eptr = eptr;
271 }
272 ret = (uint32_t)window & (((uint32_t)1 << ftb) - 1);
273 window >>= ftb;
274 available -= ftb;
275 dec->end_window = window;
276 dec->nend_bits = available;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500277 return ret;
278}
Nathan E. Egge24f1a902017-02-14 13:29:44 -0500279#endif
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500280
281/*Returns the number of bits "used" by the decoded symbols so far.
282 This same number can be computed in either the encoder or the decoder, and is
283 suitable for making coding decisions.
284 Return: The number of bits.
285 This will always be slightly larger than the exact value (e.g., all
286 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400287int od_ec_dec_tell(const od_ec_dec *dec) {
Yaowu Xufebe9b02016-11-09 10:00:31 -0800288 return (int)(((dec->end - dec->eptr) + (dec->bptr - dec->buf)) * 8 -
289 dec->cnt - dec->nend_bits + dec->tell_offs);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500290}
291
292/*Returns the number of bits "used" by the decoded symbols so far.
293 This same number can be computed in either the encoder or the decoder, and is
294 suitable for making coding decisions.
295 Return: The number of bits scaled by 2**OD_BITRES.
296 This will always be slightly larger than the exact value (e.g., all
297 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400298uint32_t od_ec_dec_tell_frac(const od_ec_dec *dec) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500299 return od_ec_tell_frac(od_ec_dec_tell(dec), dec->rng);
300}