blob: acb9d8b9dedd5d38b75a03885d2c6c4605553f1a [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
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +010016#include <assert.h>
Yaowu Xu931bc2a2016-10-14 13:53:51 -070017#include "aom_dsp/entdec.h"
Thomas Daede837262b2017-11-06 20:07:01 -080018#include "aom_dsp/prob.h"
Nathan E. Egge1078dee2016-03-06 10:59:29 -050019
20/*A range decoder.
21 This is an entropy decoder based upon \cite{Mar79}, which is itself a
22 rediscovery of the FIFO arithmetic code introduced by \cite{Pas76}.
23 It is very similar to arithmetic encoding, except that encoding is done with
24 digits in any base, instead of with bits, and so it is faster when using
25 larger bases (i.e.: a byte).
26 The author claims an average waste of $\frac{1}{2}\log_b(2b)$ bits, where $b$
27 is the base, longer than the theoretical optimum, but to my knowledge there
28 is no published justification for this claim.
29 This only seems true when using near-infinite precision arithmetic so that
30 the process is carried out with no rounding errors.
31
32 An excellent description of implementation details is available at
33 http://www.arturocampos.com/ac_range.html
34 A recent work \cite{MNW98} which proposes several changes to arithmetic
35 encoding for efficiency actually re-discovers many of the principles
36 behind range encoding, and presents a good theoretical analysis of them.
37
38 End of stream is handled by writing out the smallest number of bits that
39 ensures that the stream will be correctly decoded regardless of the value of
40 any subsequent bits.
41 od_ec_dec_tell() can be used to determine how many bits were needed to decode
42 all the symbols thus far; other data can be packed in the remaining bits of
43 the input buffer.
44 @PHDTHESIS{Pas76,
45 author="Richard Clark Pasco",
46 title="Source coding algorithms for fast data compression",
47 school="Dept. of Electrical Engineering, Stanford University",
48 address="Stanford, CA",
49 month=May,
50 year=1976,
51 URL="http://www.richpasco.org/scaffdc.pdf"
52 }
53 @INPROCEEDINGS{Mar79,
54 author="Martin, G.N.N.",
55 title="Range encoding: an algorithm for removing redundancy from a digitised
56 message",
57 booktitle="Video & Data Recording Conference",
58 year=1979,
59 address="Southampton",
60 month=Jul,
61 URL="http://www.compressconsult.com/rangecoder/rngcod.pdf.gz"
62 }
63 @ARTICLE{MNW98,
64 author="Alistair Moffat and Radford Neal and Ian H. Witten",
65 title="Arithmetic Coding Revisited",
66 journal="{ACM} Transactions on Information Systems",
67 year=1998,
68 volume=16,
69 number=3,
70 pages="256--294",
71 month=Jul,
72 URL="http://researchcommons.waikato.ac.nz/bitstream/handle/10289/78/content.pdf"
73 }*/
74
Nathan E. Egge1078dee2016-03-06 10:59:29 -050075/*This is meant to be a large, positive constant that can still be efficiently
76 loaded as an immediate (on platforms like ARM, for example).
77 Even relatively modest values like 100 would work fine.*/
78#define OD_EC_LOTS_OF_BITS (0x4000)
79
80static void od_ec_dec_refill(od_ec_dec *dec) {
81 int s;
82 od_ec_window dif;
83 int16_t cnt;
84 const unsigned char *bptr;
85 const unsigned char *end;
86 dif = dec->dif;
87 cnt = dec->cnt;
88 bptr = dec->bptr;
89 end = dec->end;
90 s = OD_EC_WINDOW_SIZE - 9 - (cnt + 15);
91 for (; s >= 0 && bptr < end; s -= 8, bptr++) {
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +010092 assert(s <= OD_EC_WINDOW_SIZE - 8);
Timothy B. Terriberry881f1092017-03-07 20:03:09 -080093 dif ^= (od_ec_window)bptr[0] << s;
Nathan E. Egge1078dee2016-03-06 10:59:29 -050094 cnt += 8;
95 }
96 if (bptr >= end) {
97 dec->tell_offs += OD_EC_LOTS_OF_BITS - cnt;
98 cnt = OD_EC_LOTS_OF_BITS;
99 }
100 dec->dif = dif;
101 dec->cnt = cnt;
102 dec->bptr = bptr;
103}
104
105/*Takes updated dif and range values, renormalizes them so that
106 32768 <= rng < 65536 (reading more bytes from the stream into dif if
107 necessary), and stores them back in the decoder context.
108 dif: The new value of dif.
109 rng: The new value of the range.
110 ret: The value to return.
111 Return: ret.
112 This allows the compiler to jump to this function via a tail-call.*/
Michael Bebenita63b44c42016-08-23 16:03:39 -0700113static int od_ec_dec_normalize(od_ec_dec *dec, od_ec_window dif, unsigned rng,
114 int ret) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500115 int d;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100116 assert(rng <= 65535U);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500117 d = 16 - OD_ILOG_NZ(rng);
118 dec->cnt -= d;
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800119 /*This is equivalent to shifting in 1's instead of 0's.*/
120 dec->dif = ((dif + 1) << d) - 1;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500121 dec->rng = rng << d;
122 if (dec->cnt < 0) od_ec_dec_refill(dec);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500123 return ret;
124}
125
126/*Initializes the decoder.
127 buf: The input buffer to use.
128 Return: 0 on success, or a negative value on error.*/
129void od_ec_dec_init(od_ec_dec *dec, const unsigned char *buf,
130 uint32_t storage) {
131 dec->buf = buf;
132 dec->eptr = buf + storage;
133 dec->end_window = 0;
134 dec->nend_bits = 0;
135 dec->tell_offs = 10 - (OD_EC_WINDOW_SIZE - 8);
136 dec->end = buf + storage;
137 dec->bptr = buf;
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800138 dec->dif = ((od_ec_window)1 << (OD_EC_WINDOW_SIZE - 1)) - 1;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500139 dec->rng = 0x8000;
140 dec->cnt = -15;
141 dec->error = 0;
142 od_ec_dec_refill(dec);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500143}
144
Timothy B. Terriberryead52872017-03-07 20:27:34 -0800145/*Decode a single binary value.
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700146 f: The probability that the bit is one, scaled by 32768.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500147 Return: The value decoded (0 or 1).*/
Timothy B. Terriberryead52872017-03-07 20:27:34 -0800148int od_ec_decode_bool_q15(od_ec_dec *dec, unsigned f) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500149 od_ec_window dif;
150 od_ec_window vw;
151 unsigned r;
Nathan E. Egge5357dca2016-09-09 14:21:56 -0400152 unsigned r_new;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500153 unsigned v;
154 int ret;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100155 assert(0 < f);
156 assert(f < 32768U);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500157 dif = dec->dif;
158 r = dec->rng;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100159 assert(dif >> (OD_EC_WINDOW_SIZE - 16) < r);
160 assert(32768U <= r);
Thomas Davies736ddef2017-11-09 09:46:08 +0000161 v = ((r >> 8) * (uint32_t)(f >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT));
Jonathan Matthews9ade3942017-11-23 08:44:07 +0000162 v += EC_MIN_PROB;
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800163 vw = (od_ec_window)v << (OD_EC_WINDOW_SIZE - 16);
164 ret = 1;
165 r_new = v;
166 if (dif >= vw) {
167 r_new = r - v;
168 dif -= vw;
169 ret = 0;
170 }
Nathan E. Egge5357dca2016-09-09 14:21:56 -0400171 return od_ec_dec_normalize(dec, dif, r_new, ret);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500172}
173
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700174/*Decodes a symbol given an inverse cumulative distribution function (CDF)
175 table in Q15.
Thomas Daede837262b2017-11-06 20:07:01 -0800176 icdf: CDF_PROB_TOP minus the CDF, such that symbol s falls in the range
177 [s > 0 ? (CDF_PROB_TOP - icdf[s - 1]) : 0, CDF_PROB_TOP - icdf[s]).
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700178 The values must be monotonically non-increasing, and icdf[nsyms - 1]
179 must be 0.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500180 nsyms: The number of symbols in the alphabet.
181 This should be at most 16.
182 Return: The decoded symbol s.*/
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700183int od_ec_decode_cdf_q15(od_ec_dec *dec, const uint16_t *icdf, int nsyms) {
Timothy B. Terriberry561eb7c2017-03-07 18:06:44 -0800184 od_ec_window dif;
185 unsigned r;
186 unsigned c;
187 unsigned u;
188 unsigned v;
189 int ret;
190 (void)nsyms;
191 dif = dec->dif;
192 r = dec->rng;
Thomas Davies736ddef2017-11-09 09:46:08 +0000193 const int N = nsyms - 1;
194
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100195 assert(dif >> (OD_EC_WINDOW_SIZE - 16) < r);
196 assert(icdf[nsyms - 1] == OD_ICDF(CDF_PROB_TOP));
197 assert(32768U <= r);
198 assert(7 - EC_PROB_SHIFT - CDF_SHIFT >= 0);
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800199 c = (unsigned)(dif >> (OD_EC_WINDOW_SIZE - 16));
200 v = r;
201 ret = -1;
202 do {
203 u = v;
Thomas Davies736ddef2017-11-09 09:46:08 +0000204 v = ((r >> 8) * (uint32_t)(icdf[++ret] >> EC_PROB_SHIFT) >>
Thomas Daede837262b2017-11-06 20:07:01 -0800205 (7 - EC_PROB_SHIFT - CDF_SHIFT));
Thomas Davies736ddef2017-11-09 09:46:08 +0000206 v += EC_MIN_PROB * (N - ret);
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800207 } while (c < v);
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100208 assert(v < u);
209 assert(u <= r);
Timothy B. Terriberry881f1092017-03-07 20:03:09 -0800210 r = u - v;
211 dif -= (od_ec_window)v << (OD_EC_WINDOW_SIZE - 16);
Timothy B. Terriberry561eb7c2017-03-07 18:06:44 -0800212 return od_ec_dec_normalize(dec, dif, r, ret);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500213}
214
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500215/*Returns the number of bits "used" by the decoded symbols so far.
216 This same number can be computed in either the encoder or the decoder, and is
217 suitable for making coding decisions.
218 Return: The number of bits.
219 This will always be slightly larger than the exact value (e.g., all
220 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400221int od_ec_dec_tell(const od_ec_dec *dec) {
Yaowu Xufebe9b02016-11-09 10:00:31 -0800222 return (int)(((dec->end - dec->eptr) + (dec->bptr - dec->buf)) * 8 -
223 dec->cnt - dec->nend_bits + dec->tell_offs);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500224}
225
226/*Returns the number of bits "used" by the decoded symbols so far.
227 This same number can be computed in either the encoder or the decoder, and is
228 suitable for making coding decisions.
229 Return: The number of bits scaled by 2**OD_BITRES.
230 This will always be slightly larger than the exact value (e.g., all
231 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400232uint32_t od_ec_dec_tell_frac(const od_ec_dec *dec) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500233 return od_ec_tell_frac(od_ec_dec_tell(dec), dec->rng);
234}