Yushin Cho | 77bba8d | 2016-11-04 16:36:56 -0700 | [diff] [blame] | 1 | /* |
| 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. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 11 | |
| 12 | #ifdef HAVE_CONFIG_H |
Yaowu Xu | 931bc2a | 2016-10-14 13:53:51 -0700 | [diff] [blame] | 13 | #include "./config.h" |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 14 | #endif |
| 15 | |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 16 | #include <assert.h> |
Yaowu Xu | 931bc2a | 2016-10-14 13:53:51 -0700 | [diff] [blame] | 17 | #include "aom_dsp/entdec.h" |
Thomas Daede | 837262b | 2017-11-06 20:07:01 -0800 | [diff] [blame] | 18 | #include "aom_dsp/prob.h" |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 19 | |
| 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. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 75 | /*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 | |
| 80 | static 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 Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 92 | assert(s <= OD_EC_WINDOW_SIZE - 8); |
Timothy B. Terriberry | 881f109 | 2017-03-07 20:03:09 -0800 | [diff] [blame] | 93 | dif ^= (od_ec_window)bptr[0] << s; |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 94 | 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 Bebenita | 63b44c4 | 2016-08-23 16:03:39 -0700 | [diff] [blame] | 113 | static int od_ec_dec_normalize(od_ec_dec *dec, od_ec_window dif, unsigned rng, |
| 114 | int ret) { |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 115 | int d; |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 116 | assert(rng <= 65535U); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 117 | d = 16 - OD_ILOG_NZ(rng); |
| 118 | dec->cnt -= d; |
Timothy B. Terriberry | 881f109 | 2017-03-07 20:03:09 -0800 | [diff] [blame] | 119 | /*This is equivalent to shifting in 1's instead of 0's.*/ |
| 120 | dec->dif = ((dif + 1) << d) - 1; |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 121 | dec->rng = rng << d; |
| 122 | if (dec->cnt < 0) od_ec_dec_refill(dec); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 123 | 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.*/ |
| 129 | void 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. Terriberry | 881f109 | 2017-03-07 20:03:09 -0800 | [diff] [blame] | 138 | dec->dif = ((od_ec_window)1 << (OD_EC_WINDOW_SIZE - 1)) - 1; |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 139 | dec->rng = 0x8000; |
| 140 | dec->cnt = -15; |
| 141 | dec->error = 0; |
| 142 | od_ec_dec_refill(dec); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Timothy B. Terriberry | ead5287 | 2017-03-07 20:27:34 -0800 | [diff] [blame] | 145 | /*Decode a single binary value. |
Timothy B. Terriberry | f9ef4f6 | 2017-08-25 11:24:18 -0700 | [diff] [blame] | 146 | f: The probability that the bit is one, scaled by 32768. |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 147 | Return: The value decoded (0 or 1).*/ |
Timothy B. Terriberry | ead5287 | 2017-03-07 20:27:34 -0800 | [diff] [blame] | 148 | int od_ec_decode_bool_q15(od_ec_dec *dec, unsigned f) { |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 149 | od_ec_window dif; |
| 150 | od_ec_window vw; |
| 151 | unsigned r; |
Nathan E. Egge | 5357dca | 2016-09-09 14:21:56 -0400 | [diff] [blame] | 152 | unsigned r_new; |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 153 | unsigned v; |
| 154 | int ret; |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 155 | assert(0 < f); |
| 156 | assert(f < 32768U); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 157 | dif = dec->dif; |
| 158 | r = dec->rng; |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 159 | assert(dif >> (OD_EC_WINDOW_SIZE - 16) < r); |
| 160 | assert(32768U <= r); |
Thomas Davies | 736ddef | 2017-11-09 09:46:08 +0000 | [diff] [blame] | 161 | v = ((r >> 8) * (uint32_t)(f >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT)); |
Jonathan Matthews | 9ade394 | 2017-11-23 08:44:07 +0000 | [diff] [blame] | 162 | v += EC_MIN_PROB; |
Timothy B. Terriberry | 881f109 | 2017-03-07 20:03:09 -0800 | [diff] [blame] | 163 | 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. Egge | 5357dca | 2016-09-09 14:21:56 -0400 | [diff] [blame] | 171 | return od_ec_dec_normalize(dec, dif, r_new, ret); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Timothy B. Terriberry | f9ef4f6 | 2017-08-25 11:24:18 -0700 | [diff] [blame] | 174 | /*Decodes a symbol given an inverse cumulative distribution function (CDF) |
| 175 | table in Q15. |
Thomas Daede | 837262b | 2017-11-06 20:07:01 -0800 | [diff] [blame] | 176 | 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. Terriberry | f9ef4f6 | 2017-08-25 11:24:18 -0700 | [diff] [blame] | 178 | The values must be monotonically non-increasing, and icdf[nsyms - 1] |
| 179 | must be 0. |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 180 | nsyms: The number of symbols in the alphabet. |
| 181 | This should be at most 16. |
| 182 | Return: The decoded symbol s.*/ |
Timothy B. Terriberry | f9ef4f6 | 2017-08-25 11:24:18 -0700 | [diff] [blame] | 183 | int od_ec_decode_cdf_q15(od_ec_dec *dec, const uint16_t *icdf, int nsyms) { |
Timothy B. Terriberry | 561eb7c | 2017-03-07 18:06:44 -0800 | [diff] [blame] | 184 | 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 Davies | 736ddef | 2017-11-09 09:46:08 +0000 | [diff] [blame] | 193 | const int N = nsyms - 1; |
| 194 | |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 195 | 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. Terriberry | 881f109 | 2017-03-07 20:03:09 -0800 | [diff] [blame] | 199 | c = (unsigned)(dif >> (OD_EC_WINDOW_SIZE - 16)); |
| 200 | v = r; |
| 201 | ret = -1; |
| 202 | do { |
| 203 | u = v; |
Thomas Davies | 736ddef | 2017-11-09 09:46:08 +0000 | [diff] [blame] | 204 | v = ((r >> 8) * (uint32_t)(icdf[++ret] >> EC_PROB_SHIFT) >> |
Thomas Daede | 837262b | 2017-11-06 20:07:01 -0800 | [diff] [blame] | 205 | (7 - EC_PROB_SHIFT - CDF_SHIFT)); |
Thomas Davies | 736ddef | 2017-11-09 09:46:08 +0000 | [diff] [blame] | 206 | v += EC_MIN_PROB * (N - ret); |
Timothy B. Terriberry | 881f109 | 2017-03-07 20:03:09 -0800 | [diff] [blame] | 207 | } while (c < v); |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 208 | assert(v < u); |
| 209 | assert(u <= r); |
Timothy B. Terriberry | 881f109 | 2017-03-07 20:03:09 -0800 | [diff] [blame] | 210 | r = u - v; |
| 211 | dif -= (od_ec_window)v << (OD_EC_WINDOW_SIZE - 16); |
Timothy B. Terriberry | 561eb7c | 2017-03-07 18:06:44 -0800 | [diff] [blame] | 212 | return od_ec_dec_normalize(dec, dif, r, ret); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 213 | } |
| 214 | |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 215 | /*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. Egge | 19698a7 | 2016-08-18 02:34:53 -0400 | [diff] [blame] | 221 | int od_ec_dec_tell(const od_ec_dec *dec) { |
Yaowu Xu | febe9b0 | 2016-11-09 10:00:31 -0800 | [diff] [blame] | 222 | return (int)(((dec->end - dec->eptr) + (dec->bptr - dec->buf)) * 8 - |
| 223 | dec->cnt - dec->nend_bits + dec->tell_offs); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 224 | } |
| 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. Egge | 19698a7 | 2016-08-18 02:34:53 -0400 | [diff] [blame] | 232 | uint32_t od_ec_dec_tell_frac(const od_ec_dec *dec) { |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 233 | return od_ec_tell_frac(od_ec_dec_tell(dec), dec->rng); |
| 234 | } |