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 | |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
Sebastien Alaiwan | 2209fc0 | 2018-03-02 15:38:15 +0100 | [diff] [blame] | 18 | #include <math.h> |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 19 | #include <assert.h> |
Yaowu Xu | 931bc2a | 2016-10-14 13:53:51 -0700 | [diff] [blame] | 20 | #include "aom_dsp/entenc.h" |
Thomas Daede | 837262b | 2017-11-06 20:07:01 -0800 | [diff] [blame] | 21 | #include "aom_dsp/prob.h" |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 22 | |
Sebastien Alaiwan | 2209fc0 | 2018-03-02 15:38:15 +0100 | [diff] [blame] | 23 | #if OD_MEASURE_EC_OVERHEAD |
| 24 | #if !defined(M_LOG2E) |
| 25 | #define M_LOG2E (1.4426950408889634073599246810019) |
| 26 | #endif |
| 27 | #define OD_LOG2(x) (M_LOG2E * log(x)) |
| 28 | #endif // OD_MEASURE_EC_OVERHEAD |
| 29 | |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 30 | /*A range encoder. |
| 31 | See entdec.c and the references for implementation details \cite{Mar79,MNW98}. |
| 32 | |
| 33 | @INPROCEEDINGS{Mar79, |
| 34 | author="Martin, G.N.N.", |
| 35 | title="Range encoding: an algorithm for removing redundancy from a digitised |
| 36 | message", |
| 37 | booktitle="Video \& Data Recording Conference", |
| 38 | year=1979, |
| 39 | address="Southampton", |
| 40 | month=Jul, |
| 41 | URL="http://www.compressconsult.com/rangecoder/rngcod.pdf.gz" |
| 42 | } |
| 43 | @ARTICLE{MNW98, |
| 44 | author="Alistair Moffat and Radford Neal and Ian H. Witten", |
| 45 | title="Arithmetic Coding Revisited", |
| 46 | journal="{ACM} Transactions on Information Systems", |
| 47 | year=1998, |
| 48 | volume=16, |
| 49 | number=3, |
| 50 | pages="256--294", |
| 51 | month=Jul, |
| 52 | URL="http://researchcommons.waikato.ac.nz/bitstream/handle/10289/78/content.pdf" |
| 53 | }*/ |
| 54 | |
| 55 | /*Takes updated low and range values, renormalizes them so that |
| 56 | 32768 <= rng < 65536 (flushing bytes from low to the pre-carry buffer if |
| 57 | necessary), and stores them back in the encoder context. |
| 58 | low: The new value of low. |
| 59 | rng: The new value of the range.*/ |
| 60 | static void od_ec_enc_normalize(od_ec_enc *enc, od_ec_window low, |
| 61 | unsigned rng) { |
| 62 | int d; |
| 63 | int c; |
| 64 | int s; |
| 65 | c = enc->cnt; |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 66 | assert(rng <= 65535U); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 67 | d = 16 - OD_ILOG_NZ(rng); |
| 68 | s = c + d; |
| 69 | /*TODO: Right now we flush every time we have at least one byte available. |
| 70 | Instead we should use an od_ec_window and flush right before we're about to |
| 71 | shift bits off the end of the window. |
| 72 | For a 32-bit window this is about the same amount of work, but for a 64-bit |
| 73 | window it should be a fair win.*/ |
| 74 | if (s >= 0) { |
| 75 | uint16_t *buf; |
| 76 | uint32_t storage; |
| 77 | uint32_t offs; |
| 78 | unsigned m; |
| 79 | buf = enc->precarry_buf; |
| 80 | storage = enc->precarry_storage; |
| 81 | offs = enc->offs; |
| 82 | if (offs + 2 > storage) { |
| 83 | storage = 2 * storage + 2; |
| 84 | buf = (uint16_t *)realloc(buf, sizeof(*buf) * storage); |
| 85 | if (buf == NULL) { |
| 86 | enc->error = -1; |
| 87 | enc->offs = 0; |
| 88 | return; |
| 89 | } |
| 90 | enc->precarry_buf = buf; |
| 91 | enc->precarry_storage = storage; |
| 92 | } |
| 93 | c += 16; |
| 94 | m = (1 << c) - 1; |
| 95 | if (s >= 8) { |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 96 | assert(offs < storage); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 97 | buf[offs++] = (uint16_t)(low >> c); |
| 98 | low &= m; |
| 99 | c -= 8; |
| 100 | m >>= 8; |
| 101 | } |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 102 | assert(offs < storage); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 103 | buf[offs++] = (uint16_t)(low >> c); |
| 104 | s = c + d - 24; |
| 105 | low &= m; |
| 106 | enc->offs = offs; |
| 107 | } |
| 108 | enc->low = low << d; |
| 109 | enc->rng = rng << d; |
| 110 | enc->cnt = s; |
| 111 | } |
| 112 | |
| 113 | /*Initializes the encoder. |
| 114 | size: The initial size of the buffer, in bytes.*/ |
| 115 | void od_ec_enc_init(od_ec_enc *enc, uint32_t size) { |
| 116 | od_ec_enc_reset(enc); |
| 117 | enc->buf = (unsigned char *)malloc(sizeof(*enc->buf) * size); |
| 118 | enc->storage = size; |
| 119 | if (size > 0 && enc->buf == NULL) { |
| 120 | enc->storage = 0; |
| 121 | enc->error = -1; |
| 122 | } |
| 123 | enc->precarry_buf = (uint16_t *)malloc(sizeof(*enc->precarry_buf) * size); |
| 124 | enc->precarry_storage = size; |
| 125 | if (size > 0 && enc->precarry_buf == NULL) { |
| 126 | enc->precarry_storage = 0; |
| 127 | enc->error = -1; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /*Reinitializes the encoder.*/ |
| 132 | void od_ec_enc_reset(od_ec_enc *enc) { |
| 133 | enc->end_offs = 0; |
| 134 | enc->end_window = 0; |
| 135 | enc->nend_bits = 0; |
| 136 | enc->offs = 0; |
| 137 | enc->low = 0; |
| 138 | enc->rng = 0x8000; |
| 139 | /*This is initialized to -9 so that it crosses zero after we've accumulated |
| 140 | one byte + one carry bit.*/ |
| 141 | enc->cnt = -9; |
| 142 | enc->error = 0; |
| 143 | #if OD_MEASURE_EC_OVERHEAD |
| 144 | enc->entropy = 0; |
| 145 | enc->nb_symbols = 0; |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | /*Frees the buffers used by the encoder.*/ |
| 150 | void od_ec_enc_clear(od_ec_enc *enc) { |
| 151 | free(enc->precarry_buf); |
| 152 | free(enc->buf); |
| 153 | } |
| 154 | |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 155 | /*Encodes a symbol given its frequency in Q15. |
Thomas Daede | 837262b | 2017-11-06 20:07:01 -0800 | [diff] [blame] | 156 | fl: CDF_PROB_TOP minus the cumulative frequency of all symbols that come |
| 157 | before the |
Timothy B. Terriberry | f9ef4f6 | 2017-08-25 11:24:18 -0700 | [diff] [blame] | 158 | one to be encoded. |
Thomas Daede | 837262b | 2017-11-06 20:07:01 -0800 | [diff] [blame] | 159 | fh: CDF_PROB_TOP minus the cumulative frequency of all symbols up to and |
| 160 | including |
Timothy B. Terriberry | f9ef4f6 | 2017-08-25 11:24:18 -0700 | [diff] [blame] | 161 | the one to be encoded.*/ |
Thomas Davies | 736ddef | 2017-11-09 09:46:08 +0000 | [diff] [blame] | 162 | static void od_ec_encode_q15(od_ec_enc *enc, unsigned fl, unsigned fh, int s, |
| 163 | int nsyms) { |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 164 | od_ec_window l; |
| 165 | unsigned r; |
| 166 | unsigned u; |
| 167 | unsigned v; |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 168 | l = enc->low; |
| 169 | r = enc->rng; |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 170 | assert(32768U <= r); |
| 171 | assert(fh <= fl); |
| 172 | assert(fl <= 32768U); |
| 173 | assert(7 - EC_PROB_SHIFT - CDF_SHIFT >= 0); |
Thomas Davies | 736ddef | 2017-11-09 09:46:08 +0000 | [diff] [blame] | 174 | const int N = nsyms - 1; |
Thomas Daede | 837262b | 2017-11-06 20:07:01 -0800 | [diff] [blame] | 175 | if (fl < CDF_PROB_TOP) { |
| 176 | u = ((r >> 8) * (uint32_t)(fl >> EC_PROB_SHIFT) >> |
| 177 | (7 - EC_PROB_SHIFT - CDF_SHIFT)) + |
Thomas Davies | 736ddef | 2017-11-09 09:46:08 +0000 | [diff] [blame] | 178 | EC_MIN_PROB * (N - (s - 1)); |
Thomas Daede | 837262b | 2017-11-06 20:07:01 -0800 | [diff] [blame] | 179 | v = ((r >> 8) * (uint32_t)(fh >> EC_PROB_SHIFT) >> |
| 180 | (7 - EC_PROB_SHIFT - CDF_SHIFT)) + |
Thomas Davies | 736ddef | 2017-11-09 09:46:08 +0000 | [diff] [blame] | 181 | EC_MIN_PROB * (N - (s + 0)); |
Timothy B. Terriberry | b1c5760 | 2017-02-16 10:53:39 -0800 | [diff] [blame] | 182 | l += r - u; |
| 183 | r = u - v; |
| 184 | } else { |
Thomas Daede | 837262b | 2017-11-06 20:07:01 -0800 | [diff] [blame] | 185 | r -= ((r >> 8) * (uint32_t)(fh >> EC_PROB_SHIFT) >> |
| 186 | (7 - EC_PROB_SHIFT - CDF_SHIFT)) + |
Thomas Davies | 736ddef | 2017-11-09 09:46:08 +0000 | [diff] [blame] | 187 | EC_MIN_PROB * (N - (s + 0)); |
Timothy B. Terriberry | b1c5760 | 2017-02-16 10:53:39 -0800 | [diff] [blame] | 188 | } |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 189 | od_ec_enc_normalize(enc, l, r); |
| 190 | #if OD_MEASURE_EC_OVERHEAD |
Thomas Daede | 837262b | 2017-11-06 20:07:01 -0800 | [diff] [blame] | 191 | enc->entropy -= OD_LOG2((double)(OD_ICDF(fh) - OD_ICDF(fl)) / CDF_PROB_TOP.); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 192 | enc->nb_symbols++; |
| 193 | #endif |
| 194 | } |
| 195 | |
Timothy B. Terriberry | 41b4f75 | 2017-03-07 17:45:30 -0800 | [diff] [blame] | 196 | /*Encode a single binary value. |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 197 | val: The value to encode (0 or 1). |
Timothy B. Terriberry | f9ef4f6 | 2017-08-25 11:24:18 -0700 | [diff] [blame] | 198 | f: The probability that the val is one, scaled by 32768.*/ |
Timothy B. Terriberry | 41b4f75 | 2017-03-07 17:45:30 -0800 | [diff] [blame] | 199 | void od_ec_encode_bool_q15(od_ec_enc *enc, int val, unsigned f) { |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 200 | od_ec_window l; |
| 201 | unsigned r; |
| 202 | unsigned v; |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 203 | assert(0 < f); |
| 204 | assert(f < 32768U); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 205 | l = enc->low; |
| 206 | r = enc->rng; |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 207 | assert(32768U <= r); |
Thomas Davies | 736ddef | 2017-11-09 09:46:08 +0000 | [diff] [blame] | 208 | 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] | 209 | v += EC_MIN_PROB; |
Timothy B. Terriberry | 41b4f75 | 2017-03-07 17:45:30 -0800 | [diff] [blame] | 210 | if (val) l += r - v; |
| 211 | r = val ? v : r - v; |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 212 | od_ec_enc_normalize(enc, l, r); |
| 213 | #if OD_MEASURE_EC_OVERHEAD |
Thomas Daede | 837262b | 2017-11-06 20:07:01 -0800 | [diff] [blame] | 214 | enc->entropy -= OD_LOG2((double)(val ? f : (32768 - f)) / 32768.); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 215 | enc->nb_symbols++; |
| 216 | #endif |
| 217 | } |
| 218 | |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 219 | /*Encodes a symbol given a cumulative distribution function (CDF) table in Q15. |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 220 | s: The index of the symbol to encode. |
Timothy B. Terriberry | f9ef4f6 | 2017-08-25 11:24:18 -0700 | [diff] [blame] | 221 | icdf: 32768 minus the CDF, such that symbol s falls in the range |
| 222 | [s > 0 ? (32768 - icdf[s - 1]) : 0, 32768 - icdf[s]). |
| 223 | The values must be monotonically decreasing, and icdf[nsyms - 1] must |
| 224 | be 0. |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 225 | nsyms: The number of symbols in the alphabet. |
| 226 | This should be at most 16.*/ |
Timothy B. Terriberry | f9ef4f6 | 2017-08-25 11:24:18 -0700 | [diff] [blame] | 227 | void od_ec_encode_cdf_q15(od_ec_enc *enc, int s, const uint16_t *icdf, |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 228 | int nsyms) { |
| 229 | (void)nsyms; |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 230 | assert(s >= 0); |
| 231 | assert(s < nsyms); |
| 232 | assert(icdf[nsyms - 1] == OD_ICDF(CDF_PROB_TOP)); |
Thomas Davies | 736ddef | 2017-11-09 09:46:08 +0000 | [diff] [blame] | 233 | od_ec_encode_q15(enc, s > 0 ? icdf[s - 1] : OD_ICDF(0), icdf[s], s, nsyms); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 234 | } |
| 235 | |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 236 | /*Overwrites a few bits at the very start of an existing stream, after they |
| 237 | have already been encoded. |
| 238 | This makes it possible to have a few flags up front, where it is easy for |
| 239 | decoders to access them without parsing the whole stream, even if their |
| 240 | values are not determined until late in the encoding process, without having |
| 241 | to buffer all the intermediate symbols in the encoder. |
| 242 | In order for this to work, at least nbits bits must have already been encoded |
| 243 | using probabilities that are an exact power of two. |
| 244 | The encoder can verify the number of encoded bits is sufficient, but cannot |
| 245 | check this latter condition. |
| 246 | val: The bits to encode (in the least nbits significant bits). |
| 247 | They will be decoded in order from most-significant to least. |
| 248 | nbits: The number of bits to overwrite. |
| 249 | This must be no more than 8.*/ |
| 250 | void od_ec_enc_patch_initial_bits(od_ec_enc *enc, unsigned val, int nbits) { |
| 251 | int shift; |
| 252 | unsigned mask; |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 253 | assert(nbits >= 0); |
| 254 | assert(nbits <= 8); |
| 255 | assert(val < 1U << nbits); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 256 | shift = 8 - nbits; |
| 257 | mask = ((1U << nbits) - 1) << shift; |
| 258 | if (enc->offs > 0) { |
| 259 | /*The first byte has been finalized.*/ |
| 260 | enc->precarry_buf[0] = |
| 261 | (uint16_t)((enc->precarry_buf[0] & ~mask) | val << shift); |
| 262 | } else if (9 + enc->cnt + (enc->rng == 0x8000) > nbits) { |
| 263 | /*The first byte has yet to be output.*/ |
| 264 | enc->low = (enc->low & ~((od_ec_window)mask << (16 + enc->cnt))) | |
| 265 | (od_ec_window)val << (16 + enc->cnt + shift); |
Yaowu Xu | 931bc2a | 2016-10-14 13:53:51 -0700 | [diff] [blame] | 266 | } else { |
| 267 | /*The encoder hasn't even encoded _nbits of data yet.*/ |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 268 | enc->error = -1; |
Yaowu Xu | 931bc2a | 2016-10-14 13:53:51 -0700 | [diff] [blame] | 269 | } |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | #if OD_MEASURE_EC_OVERHEAD |
| 273 | #include <stdio.h> |
| 274 | #endif |
| 275 | |
| 276 | /*Indicates that there are no more symbols to encode. |
| 277 | All remaining output bytes are flushed to the output buffer. |
| 278 | od_ec_enc_reset() should be called before using the encoder again. |
| 279 | bytes: Returns the size of the encoded data in the returned buffer. |
| 280 | Return: A pointer to the start of the final buffer, or NULL if there was an |
| 281 | encoding error.*/ |
| 282 | unsigned char *od_ec_enc_done(od_ec_enc *enc, uint32_t *nbytes) { |
| 283 | unsigned char *out; |
| 284 | uint32_t storage; |
| 285 | uint16_t *buf; |
Yaowu Xu | 88cbc58 | 2016-11-16 15:18:13 -0800 | [diff] [blame] | 286 | uint32_t offs; |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 287 | uint32_t end_offs; |
| 288 | int nend_bits; |
| 289 | od_ec_window m; |
| 290 | od_ec_window e; |
| 291 | od_ec_window l; |
| 292 | unsigned r; |
| 293 | int c; |
| 294 | int s; |
| 295 | if (enc->error) return NULL; |
| 296 | #if OD_MEASURE_EC_OVERHEAD |
| 297 | { |
| 298 | uint32_t tell; |
| 299 | /* Don't count the 1 bit we lose to raw bits as overhead. */ |
| 300 | tell = od_ec_enc_tell(enc) - 1; |
| 301 | fprintf(stderr, "overhead: %f%%\n", |
| 302 | 100 * (tell - enc->entropy) / enc->entropy); |
| 303 | fprintf(stderr, "efficiency: %f bits/symbol\n", |
| 304 | (double)tell / enc->nb_symbols); |
| 305 | } |
| 306 | #endif |
| 307 | /*We output the minimum number of bits that ensures that the symbols encoded |
| 308 | thus far will be decoded correctly regardless of the bits that follow.*/ |
| 309 | l = enc->low; |
| 310 | r = enc->rng; |
| 311 | c = enc->cnt; |
| 312 | s = 9; |
| 313 | m = 0x7FFF; |
| 314 | e = (l + m) & ~m; |
| 315 | while ((e | m) >= l + r) { |
| 316 | s++; |
| 317 | m >>= 1; |
| 318 | e = (l + m) & ~m; |
| 319 | } |
| 320 | s += c; |
| 321 | offs = enc->offs; |
| 322 | buf = enc->precarry_buf; |
| 323 | if (s > 0) { |
| 324 | unsigned n; |
| 325 | storage = enc->precarry_storage; |
Yaowu Xu | 88cbc58 | 2016-11-16 15:18:13 -0800 | [diff] [blame] | 326 | if (offs + ((s + 7) >> 3) > storage) { |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 327 | storage = storage * 2 + ((s + 7) >> 3); |
| 328 | buf = (uint16_t *)realloc(buf, sizeof(*buf) * storage); |
| 329 | if (buf == NULL) { |
| 330 | enc->error = -1; |
| 331 | return NULL; |
| 332 | } |
| 333 | enc->precarry_buf = buf; |
| 334 | enc->precarry_storage = storage; |
| 335 | } |
| 336 | n = (1 << (c + 16)) - 1; |
| 337 | do { |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 338 | assert(offs < storage); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 339 | buf[offs++] = (uint16_t)(e >> (c + 16)); |
| 340 | e &= n; |
| 341 | s -= 8; |
| 342 | c -= 8; |
| 343 | n >>= 8; |
| 344 | } while (s > 0); |
| 345 | } |
| 346 | /*Make sure there's enough room for the entropy-coded bits and the raw |
| 347 | bits.*/ |
| 348 | out = enc->buf; |
| 349 | storage = enc->storage; |
| 350 | end_offs = enc->end_offs; |
| 351 | e = enc->end_window; |
| 352 | nend_bits = enc->nend_bits; |
| 353 | s = -s; |
| 354 | c = OD_MAXI((nend_bits - s + 7) >> 3, 0); |
| 355 | if (offs + end_offs + c > storage) { |
| 356 | storage = offs + end_offs + c; |
| 357 | out = (unsigned char *)realloc(out, sizeof(*out) * storage); |
| 358 | if (out == NULL) { |
| 359 | enc->error = -1; |
| 360 | return NULL; |
| 361 | } |
| 362 | OD_MOVE(out + storage - end_offs, out + enc->storage - end_offs, end_offs); |
| 363 | enc->buf = out; |
| 364 | enc->storage = storage; |
| 365 | } |
| 366 | /*If we have buffered raw bits, flush them as well.*/ |
| 367 | while (nend_bits > s) { |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 368 | assert(end_offs < storage); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 369 | out[storage - ++end_offs] = (unsigned char)e; |
| 370 | e >>= 8; |
| 371 | nend_bits -= 8; |
| 372 | } |
| 373 | *nbytes = offs + end_offs; |
| 374 | /*Perform carry propagation.*/ |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 375 | assert(offs + end_offs <= storage); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 376 | out = out + storage - (offs + end_offs); |
| 377 | c = 0; |
| 378 | end_offs = offs; |
Yaowu Xu | 88cbc58 | 2016-11-16 15:18:13 -0800 | [diff] [blame] | 379 | while (offs > 0) { |
| 380 | offs--; |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 381 | c = buf[offs] + c; |
| 382 | out[offs] = (unsigned char)c; |
| 383 | c >>= 8; |
| 384 | } |
| 385 | /*Add any remaining raw bits to the last byte. |
| 386 | There is guaranteed to be enough room, because nend_bits <= s.*/ |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 387 | assert(nend_bits <= 0 || end_offs > 0); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 388 | if (nend_bits > 0) out[end_offs - 1] |= (unsigned char)e; |
| 389 | /*Note: Unless there's an allocation error, if you keep encoding into the |
| 390 | current buffer and call this function again later, everything will work |
| 391 | just fine (you won't get a new packet out, but you will get a single |
| 392 | buffer with the new data appended to the old). |
| 393 | However, this function is O(N) where N is the amount of data coded so far, |
| 394 | so calling it more than once for a given packet is a bad idea.*/ |
| 395 | return out; |
| 396 | } |
| 397 | |
| 398 | /*Returns the number of bits "used" by the encoded symbols so far. |
| 399 | This same number can be computed in either the encoder or the decoder, and is |
| 400 | suitable for making coding decisions. |
| 401 | Warning: The value returned by this function can decrease compared to an |
| 402 | earlier call, even after encoding more data, if there is an encoding error |
| 403 | (i.e., a failure to allocate enough space for the output buffer). |
| 404 | Return: The number of bits. |
| 405 | This will always be slightly larger than the exact value (e.g., all |
| 406 | rounding error is in the positive direction).*/ |
Nathan E. Egge | 19698a7 | 2016-08-18 02:34:53 -0400 | [diff] [blame] | 407 | int od_ec_enc_tell(const od_ec_enc *enc) { |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 408 | /*The 10 here counteracts the offset of -9 baked into cnt, and adds 1 extra |
| 409 | bit, which we reserve for terminating the stream.*/ |
Yaowu Xu | 6394f0f | 2018-01-20 18:50:44 -0800 | [diff] [blame] | 410 | return (enc->cnt + 10 + enc->nend_bits) + (enc->offs + enc->end_offs) * 8; |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /*Returns the number of bits "used" by the encoded symbols so far. |
| 414 | This same number can be computed in either the encoder or the decoder, and is |
| 415 | suitable for making coding decisions. |
| 416 | Warning: The value returned by this function can decrease compared to an |
| 417 | earlier call, even after encoding more data, if there is an encoding error |
| 418 | (i.e., a failure to allocate enough space for the output buffer). |
| 419 | Return: The number of bits scaled by 2**OD_BITRES. |
| 420 | This will always be slightly larger than the exact value (e.g., all |
| 421 | rounding error is in the positive direction).*/ |
Nathan E. Egge | 19698a7 | 2016-08-18 02:34:53 -0400 | [diff] [blame] | 422 | uint32_t od_ec_enc_tell_frac(const od_ec_enc *enc) { |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 423 | return od_ec_tell_frac(od_ec_enc_tell(enc), enc->rng); |
| 424 | } |
| 425 | |
| 426 | /*Saves a entropy coder checkpoint to dst. |
| 427 | This allows an encoder to reverse a series of entropy coder |
| 428 | decisions if it decides that the information would have been |
| 429 | better coded some other way.*/ |
| 430 | void od_ec_enc_checkpoint(od_ec_enc *dst, const od_ec_enc *src) { |
| 431 | OD_COPY(dst, src, 1); |
| 432 | } |
| 433 | |
| 434 | /*Restores an entropy coder checkpoint saved by od_ec_enc_checkpoint. |
| 435 | This can only be used to restore from checkpoints earlier in the target |
| 436 | state's history: you can not switch backwards and forwards or otherwise |
| 437 | switch to a state which isn't a casual ancestor of the current state. |
| 438 | Restore is also incompatible with patching the initial bits, as the |
| 439 | changes will remain in the restored version.*/ |
| 440 | void od_ec_enc_rollback(od_ec_enc *dst, const od_ec_enc *src) { |
| 441 | unsigned char *buf; |
| 442 | uint32_t storage; |
| 443 | uint16_t *precarry_buf; |
| 444 | uint32_t precarry_storage; |
Sebastien Alaiwan | 0d52f12 | 2018-03-06 09:54:23 +0100 | [diff] [blame] | 445 | assert(dst->storage >= src->storage); |
| 446 | assert(dst->precarry_storage >= src->precarry_storage); |
Nathan E. Egge | 1078dee | 2016-03-06 10:59:29 -0500 | [diff] [blame] | 447 | buf = dst->buf; |
| 448 | storage = dst->storage; |
| 449 | precarry_buf = dst->precarry_buf; |
| 450 | precarry_storage = dst->precarry_storage; |
| 451 | OD_COPY(dst, src, 1); |
| 452 | dst->buf = buf; |
| 453 | dst->storage = storage; |
| 454 | dst->precarry_buf = precarry_buf; |
| 455 | dst->precarry_storage = precarry_storage; |
| 456 | } |