blob: b015956fa03d93e0e4de21293cd667a2a08fb02d [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);
91 dif |= (od_ec_window)bptr[0] << s;
92 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;
117 dec->dif = dif << d;
118 dec->rng = rng << d;
119 if (dec->cnt < 0) od_ec_dec_refill(dec);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500120 return ret;
121}
122
123/*Initializes the decoder.
124 buf: The input buffer to use.
125 Return: 0 on success, or a negative value on error.*/
126void od_ec_dec_init(od_ec_dec *dec, const unsigned char *buf,
127 uint32_t storage) {
128 dec->buf = buf;
129 dec->eptr = buf + storage;
130 dec->end_window = 0;
131 dec->nend_bits = 0;
132 dec->tell_offs = 10 - (OD_EC_WINDOW_SIZE - 8);
133 dec->end = buf + storage;
134 dec->bptr = buf;
135 dec->dif = 0;
136 dec->rng = 0x8000;
137 dec->cnt = -15;
138 dec->error = 0;
139 od_ec_dec_refill(dec);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500140}
141
142/*Decode a bit that has an fz/ft probability of being a zero.
143 fz: The probability that the bit is zero, scaled by _ft.
144 ft: The total probability.
145 This must be at least 16384 and no more than 32768.
146 Return: The value decoded (0 or 1).*/
Michael Bebenita63b44c42016-08-23 16:03:39 -0700147int od_ec_decode_bool(od_ec_dec *dec, unsigned fz, unsigned ft) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500148 od_ec_window dif;
149 od_ec_window vw;
150 unsigned r;
151 int s;
152 unsigned v;
153 int ret;
154 OD_ASSERT(0 < fz);
155 OD_ASSERT(fz < ft);
156 OD_ASSERT(16384 <= ft);
157 OD_ASSERT(ft <= 32768U);
158 dif = dec->dif;
159 r = dec->rng;
160 OD_ASSERT(dif >> (OD_EC_WINDOW_SIZE - 16) < r);
161 OD_ASSERT(ft <= r);
162 s = r - ft >= ft;
163 ft <<= s;
164 fz <<= s;
165 OD_ASSERT(r - ft < ft);
166#if OD_EC_REDUCED_OVERHEAD
167 {
168 unsigned d;
169 unsigned e;
170 d = r - ft;
171 e = OD_SUBSATU(2 * d, ft);
172 v = fz + OD_MINI(fz, e) + OD_MINI(OD_SUBSATU(fz, e) >> 1, d);
173 }
174#else
175 v = fz + OD_MINI(fz, r - ft);
176#endif
177 vw = (od_ec_window)v << (OD_EC_WINDOW_SIZE - 16);
178 ret = dif >= vw;
179 if (ret) dif -= vw;
180 r = ret ? r - v : v;
Michael Bebenita63b44c42016-08-23 16:03:39 -0700181 return od_ec_dec_normalize(dec, dif, r, ret);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500182}
183
184/*Decode a bit that has an fz probability of being a zero in Q15.
185 This is a simpler, lower overhead version of od_ec_decode_bool() for use when
186 ft == 32768.
187 To be decoded properly by this function, symbols cannot have been encoded by
188 od_ec_encode(), but must have been encoded with one of the equivalent _q15()
189 or _dyadic() functions instead.
190 fz: The probability that the bit is zero, scaled by 32768.
191 Return: The value decoded (0 or 1).*/
Michael Bebenita63b44c42016-08-23 16:03:39 -0700192int od_ec_decode_bool_q15(od_ec_dec *dec, unsigned fz) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500193 od_ec_window dif;
194 od_ec_window vw;
195 unsigned r;
Nathan E. Egge5357dca2016-09-09 14:21:56 -0400196 unsigned r_new;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500197 unsigned v;
198 int ret;
199 OD_ASSERT(0 < fz);
200 OD_ASSERT(fz < 32768U);
201 dif = dec->dif;
202 r = dec->rng;
203 OD_ASSERT(dif >> (OD_EC_WINDOW_SIZE - 16) < r);
204 OD_ASSERT(32768U <= r);
205 v = fz * (uint32_t)r >> 15;
206 vw = (od_ec_window)v << (OD_EC_WINDOW_SIZE - 16);
Nathan E. Egge5357dca2016-09-09 14:21:56 -0400207 ret = 0;
208 r_new = v;
209 if (dif >= vw) {
210 r_new = r - v;
211 dif -= vw;
212 ret = 1;
213 }
214 return od_ec_dec_normalize(dec, dif, r_new, ret);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500215}
216
217/*Decodes a symbol given a cumulative distribution function (CDF) table.
218 cdf: The CDF, such that symbol s falls in the range
219 [s > 0 ? cdf[s - 1] : 0, cdf[s]).
220 The values must be monotonically non-increasing, and cdf[nsyms - 1]
221 must be at least 16384, and no more than 32768.
222 nsyms: The number of symbols in the alphabet.
223 This should be at most 16.
224 Return: The decoded symbol s.*/
Michael Bebenita63b44c42016-08-23 16:03:39 -0700225int od_ec_decode_cdf(od_ec_dec *dec, const uint16_t *cdf, int nsyms) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500226 od_ec_window dif;
227 unsigned r;
228 unsigned c;
229 unsigned d;
230#if OD_EC_REDUCED_OVERHEAD
231 unsigned e;
232#endif
233 int s;
234 unsigned u;
235 unsigned v;
236 unsigned q;
237 unsigned fl;
238 unsigned fh;
239 unsigned ft;
240 int ret;
241 dif = dec->dif;
242 r = dec->rng;
243 OD_ASSERT(dif >> (OD_EC_WINDOW_SIZE - 16) < r);
244 OD_ASSERT(nsyms > 0);
245 ft = cdf[nsyms - 1];
246 OD_ASSERT(16384 <= ft);
247 OD_ASSERT(ft <= 32768U);
248 OD_ASSERT(ft <= r);
249 s = r - ft >= ft;
250 ft <<= s;
251 d = r - ft;
252 OD_ASSERT(d < ft);
253 c = (unsigned)(dif >> (OD_EC_WINDOW_SIZE - 16));
254 q = OD_MAXI((int)(c >> 1), (int)(c - d));
255#if OD_EC_REDUCED_OVERHEAD
256 e = OD_SUBSATU(2 * d, ft);
257 /*The correctness of this inverse partition function is not obvious, but it
258 was checked exhaustively for all possible values of r, ft, and c.
259 TODO: It should be possible to optimize this better than the compiler,
260 given that we do not care about the accuracy of negative results (as we
261 will not use them).
262 It would also be nice to get rid of the 32-bit dividend, as it requires a
263 32x32->64 bit multiply to invert.*/
264 q = OD_MAXI((int)q, (int)((2 * (int32_t)c + 1 - (int32_t)e) / 3));
265#endif
266 q >>= s;
267 OD_ASSERT(q<ft>> s);
268 fl = 0;
269 ret = 0;
270 for (fh = cdf[ret]; fh <= q; fh = cdf[++ret]) fl = fh;
271 OD_ASSERT(fh <= ft >> s);
272 fl <<= s;
273 fh <<= s;
274#if OD_EC_REDUCED_OVERHEAD
275 u = fl + OD_MINI(fl, e) + OD_MINI(OD_SUBSATU(fl, e) >> 1, d);
276 v = fh + OD_MINI(fh, e) + OD_MINI(OD_SUBSATU(fh, e) >> 1, d);
277#else
278 u = fl + OD_MINI(fl, d);
279 v = fh + OD_MINI(fh, d);
280#endif
281 r = v - u;
282 dif -= (od_ec_window)u << (OD_EC_WINDOW_SIZE - 16);
Michael Bebenita63b44c42016-08-23 16:03:39 -0700283 return od_ec_dec_normalize(dec, dif, r, ret);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500284}
285
286/*Decodes a symbol given a cumulative distribution function (CDF) table.
287 cdf: The CDF, such that symbol s falls in the range
288 [s > 0 ? cdf[s - 1] : 0, cdf[s]).
289 The values must be monotonically non-increasing, and cdf[nsyms - 1]
290 must be at least 2, and no more than 32768.
291 nsyms: The number of symbols in the alphabet.
292 This should be at most 16.
293 Return: The decoded symbol s.*/
Michael Bebenita63b44c42016-08-23 16:03:39 -0700294int od_ec_decode_cdf_unscaled(od_ec_dec *dec, const uint16_t *cdf, int nsyms) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500295 od_ec_window dif;
296 unsigned r;
297 unsigned c;
298 unsigned d;
299#if OD_EC_REDUCED_OVERHEAD
300 unsigned e;
301#endif
302 int s;
303 unsigned u;
304 unsigned v;
305 unsigned q;
306 unsigned fl;
307 unsigned fh;
308 unsigned ft;
309 int ret;
310 dif = dec->dif;
311 r = dec->rng;
312 OD_ASSERT(dif >> (OD_EC_WINDOW_SIZE - 16) < r);
313 OD_ASSERT(nsyms > 0);
314 ft = cdf[nsyms - 1];
315 OD_ASSERT(2 <= ft);
316 OD_ASSERT(ft <= 32768U);
317 s = 15 - OD_ILOG_NZ(ft - 1);
318 ft <<= s;
319 OD_ASSERT(ft <= r);
320 if (r - ft >= ft) {
321 ft <<= 1;
322 s++;
323 }
324 d = r - ft;
325 OD_ASSERT(d < ft);
326 c = (unsigned)(dif >> (OD_EC_WINDOW_SIZE - 16));
327 q = OD_MAXI((int)(c >> 1), (int)(c - d));
328#if OD_EC_REDUCED_OVERHEAD
329 e = OD_SUBSATU(2 * d, ft);
330 /*TODO: See TODO above.*/
331 q = OD_MAXI((int)q, (int)((2 * (int32_t)c + 1 - (int32_t)e) / 3));
332#endif
333 q >>= s;
334 OD_ASSERT(q<ft>> s);
335 fl = 0;
336 ret = 0;
337 for (fh = cdf[ret]; fh <= q; fh = cdf[++ret]) fl = fh;
338 OD_ASSERT(fh <= ft >> s);
339 fl <<= s;
340 fh <<= s;
341#if OD_EC_REDUCED_OVERHEAD
342 u = fl + OD_MINI(fl, e) + OD_MINI(OD_SUBSATU(fl, e) >> 1, d);
343 v = fh + OD_MINI(fh, e) + OD_MINI(OD_SUBSATU(fh, e) >> 1, d);
344#else
345 u = fl + OD_MINI(fl, d);
346 v = fh + OD_MINI(fh, d);
347#endif
348 r = v - u;
349 dif -= (od_ec_window)u << (OD_EC_WINDOW_SIZE - 16);
Michael Bebenita63b44c42016-08-23 16:03:39 -0700350 return od_ec_dec_normalize(dec, dif, r, ret);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500351}
352
353/*Decodes a symbol given a cumulative distribution function (CDF) table that
354 sums to a power of two.
355 This is a simpler, lower overhead version of od_ec_decode_cdf() for use when
356 cdf[nsyms - 1] is a power of two.
357 To be decoded properly by this function, symbols cannot have been encoded by
358 od_ec_encode(), but must have been encoded with one of the equivalent _q15()
359 functions instead.
360 cdf: The CDF, such that symbol s falls in the range
361 [s > 0 ? cdf[s - 1] : 0, cdf[s]).
362 The values must be monotonically non-increasing, and cdf[nsyms - 1]
363 must be exactly 1 << ftb.
364 nsyms: The number of symbols in the alphabet.
365 This should be at most 16.
366 ftb: The number of bits of precision in the cumulative distribution.
367 This must be no more than 15.
368 Return: The decoded symbol s.*/
Michael Bebenita63b44c42016-08-23 16:03:39 -0700369int od_ec_decode_cdf_unscaled_dyadic(od_ec_dec *dec, const uint16_t *cdf,
370 int nsyms, unsigned ftb) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500371 od_ec_window dif;
372 unsigned r;
373 unsigned c;
374 unsigned u;
375 unsigned v;
376 int ret;
377 (void)nsyms;
378 dif = dec->dif;
379 r = dec->rng;
380 OD_ASSERT(dif >> (OD_EC_WINDOW_SIZE - 16) < r);
381 OD_ASSERT(ftb <= 15);
382 OD_ASSERT(cdf[nsyms - 1] == 1U << ftb);
383 OD_ASSERT(32768U <= r);
384 c = (unsigned)(dif >> (OD_EC_WINDOW_SIZE - 16));
385 v = 0;
386 ret = -1;
387 do {
388 u = v;
389 v = cdf[++ret] * (uint32_t)r >> ftb;
390 } while (v <= c);
391 OD_ASSERT(v <= r);
392 r = v - u;
393 dif -= (od_ec_window)u << (OD_EC_WINDOW_SIZE - 16);
Michael Bebenita63b44c42016-08-23 16:03:39 -0700394 return od_ec_dec_normalize(dec, dif, r, ret);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500395}
396
397/*Decodes a symbol given a cumulative distribution function (CDF) table in Q15.
398 This is a simpler, lower overhead version of od_ec_decode_cdf() for use when
399 cdf[nsyms - 1] == 32768.
400 To be decoded properly by this function, symbols cannot have been encoded by
401 od_ec_encode(), but must have been encoded with one of the equivalent _q15()
402 or dyadic() functions instead.
403 cdf: The CDF, such that symbol s falls in the range
404 [s > 0 ? cdf[s - 1] : 0, cdf[s]).
405 The values must be monotonically non-increasing, and cdf[nsyms - 1]
406 must be 32768.
407 nsyms: The number of symbols in the alphabet.
408 This should be at most 16.
409 Return: The decoded symbol s.*/
Michael Bebenita63b44c42016-08-23 16:03:39 -0700410int od_ec_decode_cdf_q15(od_ec_dec *dec, const uint16_t *cdf, int nsyms) {
411 return od_ec_decode_cdf_unscaled_dyadic(dec, cdf, nsyms, 15);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500412}
413
414/*Extracts a raw unsigned integer with a non-power-of-2 range from the stream.
415 The integer must have been encoded with od_ec_enc_uint().
416 ft: The number of integers that can be decoded (one more than the max).
417 This must be at least 2, and no more than 2**29.
418 Return: The decoded bits.*/
Michael Bebenita63b44c42016-08-23 16:03:39 -0700419uint32_t od_ec_dec_uint(od_ec_dec *dec, uint32_t ft) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500420 OD_ASSERT(ft >= 2);
421 OD_ASSERT(ft <= (uint32_t)1 << (25 + OD_EC_UINT_BITS));
422 if (ft > 1U << OD_EC_UINT_BITS) {
423 uint32_t t;
424 int ft1;
425 int ftb;
426 ft--;
427 ftb = OD_ILOG_NZ(ft) - OD_EC_UINT_BITS;
428 ft1 = (int)(ft >> ftb) + 1;
Michael Bebenita63b44c42016-08-23 16:03:39 -0700429 t = od_ec_decode_cdf_q15(dec, OD_UNIFORM_CDF_Q15(ft1), ft1);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700430 t = t << ftb | od_ec_dec_bits(dec, ftb, "");
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500431 if (t <= ft) return t;
432 dec->error = 1;
433 return ft;
434 }
Michael Bebenita63b44c42016-08-23 16:03:39 -0700435 return od_ec_decode_cdf_q15(dec, OD_UNIFORM_CDF_Q15(ft), (int)ft);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500436}
437
438/*Extracts a sequence of raw bits from the stream.
439 The bits must have been encoded with od_ec_enc_bits().
440 ftb: The number of bits to extract.
441 This must be between 0 and 25, inclusive.
442 Return: The decoded bits.*/
Yushin Cho77bba8d2016-11-04 16:36:56 -0700443uint32_t od_ec_dec_bits_(od_ec_dec *dec, unsigned ftb) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500444 od_ec_window window;
445 int available;
446 uint32_t ret;
447 OD_ASSERT(ftb <= 25);
448 window = dec->end_window;
449 available = dec->nend_bits;
450 if ((unsigned)available < ftb) {
451 const unsigned char *buf;
452 const unsigned char *eptr;
453 buf = dec->buf;
454 eptr = dec->eptr;
455 OD_ASSERT(available <= OD_EC_WINDOW_SIZE - 8);
456 do {
457 if (eptr <= buf) {
458 dec->tell_offs += OD_EC_LOTS_OF_BITS - available;
459 available = OD_EC_LOTS_OF_BITS;
460 break;
461 }
462 window |= (od_ec_window) * --eptr << available;
463 available += 8;
464 } while (available <= OD_EC_WINDOW_SIZE - 8);
465 dec->eptr = eptr;
466 }
467 ret = (uint32_t)window & (((uint32_t)1 << ftb) - 1);
468 window >>= ftb;
469 available -= ftb;
470 dec->end_window = window;
471 dec->nend_bits = available;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500472 return ret;
473}
474
475/*Returns the number of bits "used" by the decoded symbols so far.
476 This same number can be computed in either the encoder or the decoder, and is
477 suitable for making coding decisions.
478 Return: The number of bits.
479 This will always be slightly larger than the exact value (e.g., all
480 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400481int od_ec_dec_tell(const od_ec_dec *dec) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500482 return ((dec->end - dec->eptr) + (dec->bptr - dec->buf)) * 8 - dec->cnt -
483 dec->nend_bits + dec->tell_offs;
484}
485
486/*Returns the number of bits "used" by the decoded symbols so far.
487 This same number can be computed in either the encoder or the decoder, and is
488 suitable for making coding decisions.
489 Return: The number of bits scaled by 2**OD_BITRES.
490 This will always be slightly larger than the exact value (e.g., all
491 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400492uint32_t od_ec_dec_tell_frac(const od_ec_dec *dec) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500493 return od_ec_tell_frac(od_ec_dec_tell(dec), dec->rng);
494}