blob: ad13a72f4dc6b9d7edb1b461d978a9e93287c402 [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
16#include <stdlib.h>
17#include <string.h>
Yaowu Xu931bc2a2016-10-14 13:53:51 -070018#include "aom_dsp/entenc.h"
Nathan E. Egge1078dee2016-03-06 10:59:29 -050019
20/*A range encoder.
21 See entdec.c and the references for implementation details \cite{Mar79,MNW98}.
22
23 @INPROCEEDINGS{Mar79,
24 author="Martin, G.N.N.",
25 title="Range encoding: an algorithm for removing redundancy from a digitised
26 message",
27 booktitle="Video \& Data Recording Conference",
28 year=1979,
29 address="Southampton",
30 month=Jul,
31 URL="http://www.compressconsult.com/rangecoder/rngcod.pdf.gz"
32 }
33 @ARTICLE{MNW98,
34 author="Alistair Moffat and Radford Neal and Ian H. Witten",
35 title="Arithmetic Coding Revisited",
36 journal="{ACM} Transactions on Information Systems",
37 year=1998,
38 volume=16,
39 number=3,
40 pages="256--294",
41 month=Jul,
42 URL="http://researchcommons.waikato.ac.nz/bitstream/handle/10289/78/content.pdf"
43 }*/
44
45/*Takes updated low and range values, renormalizes them so that
46 32768 <= rng < 65536 (flushing bytes from low to the pre-carry buffer if
47 necessary), and stores them back in the encoder context.
48 low: The new value of low.
49 rng: The new value of the range.*/
50static void od_ec_enc_normalize(od_ec_enc *enc, od_ec_window low,
51 unsigned rng) {
52 int d;
53 int c;
54 int s;
55 c = enc->cnt;
56 OD_ASSERT(rng <= 65535U);
57 d = 16 - OD_ILOG_NZ(rng);
58 s = c + d;
59 /*TODO: Right now we flush every time we have at least one byte available.
60 Instead we should use an od_ec_window and flush right before we're about to
61 shift bits off the end of the window.
62 For a 32-bit window this is about the same amount of work, but for a 64-bit
63 window it should be a fair win.*/
64 if (s >= 0) {
65 uint16_t *buf;
66 uint32_t storage;
67 uint32_t offs;
68 unsigned m;
69 buf = enc->precarry_buf;
70 storage = enc->precarry_storage;
71 offs = enc->offs;
72 if (offs + 2 > storage) {
73 storage = 2 * storage + 2;
74 buf = (uint16_t *)realloc(buf, sizeof(*buf) * storage);
75 if (buf == NULL) {
76 enc->error = -1;
77 enc->offs = 0;
78 return;
79 }
80 enc->precarry_buf = buf;
81 enc->precarry_storage = storage;
82 }
83 c += 16;
84 m = (1 << c) - 1;
85 if (s >= 8) {
86 OD_ASSERT(offs < storage);
87 buf[offs++] = (uint16_t)(low >> c);
88 low &= m;
89 c -= 8;
90 m >>= 8;
91 }
92 OD_ASSERT(offs < storage);
93 buf[offs++] = (uint16_t)(low >> c);
94 s = c + d - 24;
95 low &= m;
96 enc->offs = offs;
97 }
98 enc->low = low << d;
99 enc->rng = rng << d;
100 enc->cnt = s;
101}
102
103/*Initializes the encoder.
104 size: The initial size of the buffer, in bytes.*/
105void od_ec_enc_init(od_ec_enc *enc, uint32_t size) {
106 od_ec_enc_reset(enc);
107 enc->buf = (unsigned char *)malloc(sizeof(*enc->buf) * size);
108 enc->storage = size;
109 if (size > 0 && enc->buf == NULL) {
110 enc->storage = 0;
111 enc->error = -1;
112 }
113 enc->precarry_buf = (uint16_t *)malloc(sizeof(*enc->precarry_buf) * size);
114 enc->precarry_storage = size;
115 if (size > 0 && enc->precarry_buf == NULL) {
116 enc->precarry_storage = 0;
117 enc->error = -1;
118 }
119}
120
121/*Reinitializes the encoder.*/
122void od_ec_enc_reset(od_ec_enc *enc) {
123 enc->end_offs = 0;
124 enc->end_window = 0;
125 enc->nend_bits = 0;
126 enc->offs = 0;
127 enc->low = 0;
128 enc->rng = 0x8000;
129 /*This is initialized to -9 so that it crosses zero after we've accumulated
130 one byte + one carry bit.*/
131 enc->cnt = -9;
132 enc->error = 0;
133#if OD_MEASURE_EC_OVERHEAD
134 enc->entropy = 0;
135 enc->nb_symbols = 0;
136#endif
137}
138
139/*Frees the buffers used by the encoder.*/
140void od_ec_enc_clear(od_ec_enc *enc) {
141 free(enc->precarry_buf);
142 free(enc->buf);
143}
144
145/*Encodes a symbol given its scaled frequency information.
146 The frequency information must be discernable by the decoder, assuming it
147 has read only the previous symbols from the stream.
148 You can change the frequency information, or even the entire source alphabet,
149 so long as the decoder can tell from the context of the previously encoded
150 information that it is supposed to do so as well.
151 fl: The cumulative frequency of all symbols that come before the one to be
152 encoded.
153 fh: The cumulative frequency of all symbols up to and including the one to
154 be encoded.
155 Together with fl, this defines the range [fl, fh) in which the decoded
156 value will fall.
157 ft: The sum of the frequencies of all the symbols.
158 This must be at least 16384, and no more than 32768.*/
159static void od_ec_encode(od_ec_enc *enc, unsigned fl, unsigned fh,
160 unsigned ft) {
161 od_ec_window l;
162 unsigned r;
163 int s;
164 unsigned d;
165 unsigned u;
166 unsigned v;
167 OD_ASSERT(fl < fh);
168 OD_ASSERT(fh <= ft);
169 OD_ASSERT(16384 <= ft);
170 OD_ASSERT(ft <= 32768U);
171 l = enc->low;
172 r = enc->rng;
173 OD_ASSERT(ft <= r);
174 s = r - ft >= ft;
175 ft <<= s;
176 fl <<= s;
177 fh <<= s;
178 d = r - ft;
179 OD_ASSERT(d < ft);
180#if OD_EC_REDUCED_OVERHEAD
181 {
182 unsigned e;
183 e = OD_SUBSATU(2 * d, ft);
184 u = fl + OD_MINI(fl, e) + OD_MINI(OD_SUBSATU(fl, e) >> 1, d);
185 v = fh + OD_MINI(fh, e) + OD_MINI(OD_SUBSATU(fh, e) >> 1, d);
186 }
187#else
188 u = fl + OD_MINI(fl, d);
189 v = fh + OD_MINI(fh, d);
190#endif
191 r = v - u;
192 l += u;
193 od_ec_enc_normalize(enc, l, r);
194#if OD_MEASURE_EC_OVERHEAD
195 enc->entropy -= OD_LOG2((double)(fh - fl) / ft);
196 enc->nb_symbols++;
197#endif
198}
199
200/*Encodes a symbol given its frequency in Q15.
201 This is like od_ec_encode() when ft == 32768, but is simpler and has lower
202 overhead.
203 Symbols encoded with this function cannot be properly decoded with
204 od_ec_decode(), and must be decoded with one of the equivalent _q15()
205 functions instead.
206 fl: The cumulative frequency of all symbols that come before the one to be
207 encoded.
208 fh: The cumulative frequency of all symbols up to and including the one to
209 be encoded.*/
210static void od_ec_encode_q15(od_ec_enc *enc, unsigned fl, unsigned fh) {
211 od_ec_window l;
212 unsigned r;
213 unsigned u;
214 unsigned v;
215 OD_ASSERT(fl < fh);
216 OD_ASSERT(fh <= 32768U);
217 l = enc->low;
218 r = enc->rng;
219 OD_ASSERT(32768U <= r);
220 u = fl * (uint32_t)r >> 15;
221 v = fh * (uint32_t)r >> 15;
222 r = v - u;
223 l += u;
224 od_ec_enc_normalize(enc, l, r);
225#if OD_MEASURE_EC_OVERHEAD
226 enc->entropy -= OD_LOG2((double)(fh - fl) / 32768.);
227 enc->nb_symbols++;
228#endif
229}
230
231/*Encodes a symbol given its frequency information with an arbitrary scale.
232 This operates just like od_ec_encode(), but does not require that ft be at
233 least 16384.
234 fl: The cumulative frequency of all symbols that come before the one to be
235 encoded.
236 fh: The cumulative frequency of all symbols up to and including the one to
237 be encoded.
238 ft: The sum of the frequencies of all the symbols.
239 This must be at least 2 and no more than 32768.*/
240static void od_ec_encode_unscaled(od_ec_enc *enc, unsigned fl, unsigned fh,
241 unsigned ft) {
242 int s;
243 OD_ASSERT(fl < fh);
244 OD_ASSERT(fh <= ft);
245 OD_ASSERT(2 <= ft);
246 OD_ASSERT(ft <= 32768U);
247 s = 15 - OD_ILOG_NZ(ft - 1);
248 od_ec_encode(enc, fl << s, fh << s, ft << s);
249}
250
251/*Encode a bit that has an fz/ft probability of being a zero.
252 val: The value to encode (0 or 1).
253 fz: The probability that val is zero, scaled by ft.
254 ft: The total probability.
255 This must be at least 16384 and no more than 32768.*/
256void od_ec_encode_bool(od_ec_enc *enc, int val, unsigned fz, unsigned ft) {
257 od_ec_window l;
258 unsigned r;
259 int s;
260 unsigned v;
261 OD_ASSERT(0 < fz);
262 OD_ASSERT(fz < ft);
263 OD_ASSERT(16384 <= ft);
264 OD_ASSERT(ft <= 32768U);
265 l = enc->low;
266 r = enc->rng;
267 OD_ASSERT(ft <= r);
268 s = r - ft >= ft;
269 ft <<= s;
270 fz <<= s;
271 OD_ASSERT(r - ft < ft);
272#if OD_EC_REDUCED_OVERHEAD
273 {
274 unsigned d;
275 unsigned e;
276 d = r - ft;
277 e = OD_SUBSATU(2 * d, ft);
278 v = fz + OD_MINI(fz, e) + OD_MINI(OD_SUBSATU(fz, e) >> 1, d);
279 }
280#else
281 v = fz + OD_MINI(fz, r - ft);
282#endif
283 if (val) l += v;
284 r = val ? r - v : v;
285 od_ec_enc_normalize(enc, l, r);
286#if OD_MEASURE_EC_OVERHEAD
287 enc->entropy -= OD_LOG2((double)(val ? ft - fz : fz) / ft);
288 enc->nb_symbols++;
289#endif
290}
291
292/*Encode a bit that has an fz probability of being a zero in Q15.
293 This is a simpler, lower overhead version of od_ec_encode_bool() for use when
294 ft == 32768.
295 Symbols encoded with this function cannot be properly decoded with
296 od_ec_decode(), and must be decoded with one of the equivalent _q15()
297 functions instead.
298 val: The value to encode (0 or 1).
299 fz: The probability that val is zero, scaled by 32768.*/
300void od_ec_encode_bool_q15(od_ec_enc *enc, int val, unsigned fz) {
301 od_ec_window l;
302 unsigned r;
303 unsigned v;
304 OD_ASSERT(0 < fz);
305 OD_ASSERT(fz < 32768U);
306 l = enc->low;
307 r = enc->rng;
308 OD_ASSERT(32768U <= r);
309 v = fz * (uint32_t)r >> 15;
310 if (val) l += v;
311 r = val ? r - v : v;
312 od_ec_enc_normalize(enc, l, r);
313#if OD_MEASURE_EC_OVERHEAD
314 enc->entropy -= OD_LOG2((double)(val ? 32768 - fz : fz) / 32768.);
315 enc->nb_symbols++;
316#endif
317}
318
319/*Encodes a symbol given a cumulative distribution function (CDF) table.
320 s: The index of the symbol to encode.
321 cdf: The CDF, such that symbol s falls in the range
322 [s > 0 ? cdf[s - 1] : 0, cdf[s]).
323 The values must be monotonically non-decreasing, and the last value
324 must be at least 16384, and no more than 32768.
325 nsyms: The number of symbols in the alphabet.
326 This should be at most 16.*/
327void od_ec_encode_cdf(od_ec_enc *enc, int s, const uint16_t *cdf, int nsyms) {
328 OD_ASSERT(s >= 0);
329 OD_ASSERT(s < nsyms);
330 od_ec_encode(enc, s > 0 ? cdf[s - 1] : 0, cdf[s], cdf[nsyms - 1]);
331}
332
333/*Encodes a symbol given a cumulative distribution function (CDF) table in Q15.
334 This is a simpler, lower overhead version of od_ec_encode_cdf() for use when
335 cdf[nsyms - 1] == 32768.
336 Symbols encoded with this function cannot be properly decoded with
337 od_ec_decode(), and must be decoded with one of the equivalent _q15()
338 functions instead.
339 s: The index of the symbol to encode.
340 cdf: The CDF, such that symbol s falls in the range
341 [s > 0 ? cdf[s - 1] : 0, cdf[s]).
342 The values must be monotonically non-decreasing, and the last value
343 must be exactly 32768.
344 nsyms: The number of symbols in the alphabet.
345 This should be at most 16.*/
346void od_ec_encode_cdf_q15(od_ec_enc *enc, int s, const uint16_t *cdf,
347 int nsyms) {
348 (void)nsyms;
349 OD_ASSERT(s >= 0);
350 OD_ASSERT(s < nsyms);
351 OD_ASSERT(cdf[nsyms - 1] == 32768U);
352 od_ec_encode_q15(enc, s > 0 ? cdf[s - 1] : 0, cdf[s]);
353}
354
355/*Encodes a symbol given a cumulative distribution function (CDF) table.
356 s: The index of the symbol to encode.
357 cdf: The CDF, such that symbol s falls in the range
358 [s > 0 ? cdf[s - 1] : 0, cdf[s]).
359 The values must be monotonically non-decreasing, and the last value
360 must be at least 2, and no more than 32768.
361 nsyms: The number of symbols in the alphabet.
362 This should be at most 16.*/
363void od_ec_encode_cdf_unscaled(od_ec_enc *enc, int s, const uint16_t *cdf,
364 int nsyms) {
365 OD_ASSERT(s >= 0);
366 OD_ASSERT(s < nsyms);
367 od_ec_encode_unscaled(enc, s > 0 ? cdf[s - 1] : 0, cdf[s], cdf[nsyms - 1]);
368}
369
370/*Equivalent to od_ec_encode_cdf_q15() with the cdf scaled by
371 (1 << (15 - ftb)).
372 s: The index of the symbol to encode.
373 cdf: The CDF, such that symbol s falls in the range
374 [s > 0 ? cdf[s - 1] : 0, cdf[s]).
375 The values must be monotonically non-decreasing, and the last value
376 must be exactly 1 << ftb.
377 nsyms: The number of symbols in the alphabet.
378 This should be at most 16.
379 ftb: The number of bits of precision in the cumulative distribution.
380 This must be no more than 15.*/
381void od_ec_encode_cdf_unscaled_dyadic(od_ec_enc *enc, int s,
382 const uint16_t *cdf, int nsyms,
383 unsigned ftb) {
384 (void)nsyms;
385 OD_ASSERT(s >= 0);
386 OD_ASSERT(s < nsyms);
387 OD_ASSERT(ftb <= 15);
388 OD_ASSERT(cdf[nsyms - 1] == 1U << ftb);
389 od_ec_encode_q15(enc, s > 0 ? cdf[s - 1] << (15 - ftb) : 0,
390 cdf[s] << (15 - ftb));
391}
392
Nathan E. Egge24f1a902017-02-14 13:29:44 -0500393#if CONFIG_RAWBITS
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500394/*Encodes a raw unsigned integer in the stream.
395 fl: The integer to encode.
396 ft: The number of integers that can be encoded (one more than the max).
397 This must be at least 2, and no more than 2**29.*/
398void od_ec_enc_uint(od_ec_enc *enc, uint32_t fl, uint32_t ft) {
399 OD_ASSERT(ft >= 2);
400 OD_ASSERT(fl < ft);
401 OD_ASSERT(ft <= (uint32_t)1 << (25 + OD_EC_UINT_BITS));
402 if (ft > 1U << OD_EC_UINT_BITS) {
403 int ft1;
404 int ftb;
405 ft--;
406 ftb = OD_ILOG_NZ(ft) - OD_EC_UINT_BITS;
407 ft1 = (int)(ft >> ftb) + 1;
408 od_ec_encode_cdf_q15(enc, (int)(fl >> ftb), OD_UNIFORM_CDF_Q15(ft1), ft1);
409 od_ec_enc_bits(enc, fl & (((uint32_t)1 << ftb) - 1), ftb);
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700410 } else {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500411 od_ec_encode_cdf_q15(enc, (int)fl, OD_UNIFORM_CDF_Q15(ft), (int)ft);
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700412 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500413}
414
415/*Encodes a sequence of raw bits in the stream.
416 fl: The bits to encode.
417 ftb: The number of bits to encode.
418 This must be between 0 and 25, inclusive.*/
419void od_ec_enc_bits(od_ec_enc *enc, uint32_t fl, unsigned ftb) {
420 od_ec_window end_window;
421 int nend_bits;
422 OD_ASSERT(ftb <= 25);
423 OD_ASSERT(fl < (uint32_t)1 << ftb);
424#if OD_MEASURE_EC_OVERHEAD
425 enc->entropy += ftb;
426#endif
427 end_window = enc->end_window;
428 nend_bits = enc->nend_bits;
429 if (nend_bits + ftb > OD_EC_WINDOW_SIZE) {
430 unsigned char *buf;
431 uint32_t storage;
432 uint32_t end_offs;
433 buf = enc->buf;
434 storage = enc->storage;
435 end_offs = enc->end_offs;
436 if (end_offs + (OD_EC_WINDOW_SIZE >> 3) >= storage) {
437 unsigned char *new_buf;
438 uint32_t new_storage;
439 new_storage = 2 * storage + (OD_EC_WINDOW_SIZE >> 3);
440 new_buf = (unsigned char *)malloc(sizeof(*new_buf) * new_storage);
441 if (new_buf == NULL) {
442 enc->error = -1;
443 enc->end_offs = 0;
444 return;
445 }
446 OD_COPY(new_buf + new_storage - end_offs, buf + storage - end_offs,
447 end_offs);
448 storage = new_storage;
449 free(buf);
450 enc->buf = buf = new_buf;
451 enc->storage = storage;
452 }
453 do {
454 OD_ASSERT(end_offs < storage);
455 buf[storage - ++end_offs] = (unsigned char)end_window;
456 end_window >>= 8;
457 nend_bits -= 8;
458 } while (nend_bits >= 8);
459 enc->end_offs = end_offs;
460 }
461 OD_ASSERT(nend_bits + ftb <= OD_EC_WINDOW_SIZE);
462 end_window |= (od_ec_window)fl << nend_bits;
463 nend_bits += ftb;
464 enc->end_window = end_window;
465 enc->nend_bits = nend_bits;
466}
Nathan E. Egge24f1a902017-02-14 13:29:44 -0500467#endif
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500468
469/*Overwrites a few bits at the very start of an existing stream, after they
470 have already been encoded.
471 This makes it possible to have a few flags up front, where it is easy for
472 decoders to access them without parsing the whole stream, even if their
473 values are not determined until late in the encoding process, without having
474 to buffer all the intermediate symbols in the encoder.
475 In order for this to work, at least nbits bits must have already been encoded
476 using probabilities that are an exact power of two.
477 The encoder can verify the number of encoded bits is sufficient, but cannot
478 check this latter condition.
479 val: The bits to encode (in the least nbits significant bits).
480 They will be decoded in order from most-significant to least.
481 nbits: The number of bits to overwrite.
482 This must be no more than 8.*/
483void od_ec_enc_patch_initial_bits(od_ec_enc *enc, unsigned val, int nbits) {
484 int shift;
485 unsigned mask;
486 OD_ASSERT(nbits >= 0);
487 OD_ASSERT(nbits <= 8);
488 OD_ASSERT(val < 1U << nbits);
489 shift = 8 - nbits;
490 mask = ((1U << nbits) - 1) << shift;
491 if (enc->offs > 0) {
492 /*The first byte has been finalized.*/
493 enc->precarry_buf[0] =
494 (uint16_t)((enc->precarry_buf[0] & ~mask) | val << shift);
495 } else if (9 + enc->cnt + (enc->rng == 0x8000) > nbits) {
496 /*The first byte has yet to be output.*/
497 enc->low = (enc->low & ~((od_ec_window)mask << (16 + enc->cnt))) |
498 (od_ec_window)val << (16 + enc->cnt + shift);
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700499 } else {
500 /*The encoder hasn't even encoded _nbits of data yet.*/
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500501 enc->error = -1;
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700502 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500503}
504
505#if OD_MEASURE_EC_OVERHEAD
506#include <stdio.h>
507#endif
508
509/*Indicates that there are no more symbols to encode.
510 All remaining output bytes are flushed to the output buffer.
511 od_ec_enc_reset() should be called before using the encoder again.
512 bytes: Returns the size of the encoded data in the returned buffer.
513 Return: A pointer to the start of the final buffer, or NULL if there was an
514 encoding error.*/
515unsigned char *od_ec_enc_done(od_ec_enc *enc, uint32_t *nbytes) {
516 unsigned char *out;
517 uint32_t storage;
518 uint16_t *buf;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800519 uint32_t offs;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500520 uint32_t end_offs;
521 int nend_bits;
522 od_ec_window m;
523 od_ec_window e;
524 od_ec_window l;
525 unsigned r;
526 int c;
527 int s;
528 if (enc->error) return NULL;
529#if OD_MEASURE_EC_OVERHEAD
530 {
531 uint32_t tell;
532 /* Don't count the 1 bit we lose to raw bits as overhead. */
533 tell = od_ec_enc_tell(enc) - 1;
534 fprintf(stderr, "overhead: %f%%\n",
535 100 * (tell - enc->entropy) / enc->entropy);
536 fprintf(stderr, "efficiency: %f bits/symbol\n",
537 (double)tell / enc->nb_symbols);
538 }
539#endif
540 /*We output the minimum number of bits that ensures that the symbols encoded
541 thus far will be decoded correctly regardless of the bits that follow.*/
542 l = enc->low;
543 r = enc->rng;
544 c = enc->cnt;
545 s = 9;
546 m = 0x7FFF;
547 e = (l + m) & ~m;
548 while ((e | m) >= l + r) {
549 s++;
550 m >>= 1;
551 e = (l + m) & ~m;
552 }
553 s += c;
554 offs = enc->offs;
555 buf = enc->precarry_buf;
556 if (s > 0) {
557 unsigned n;
558 storage = enc->precarry_storage;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800559 if (offs + ((s + 7) >> 3) > storage) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500560 storage = storage * 2 + ((s + 7) >> 3);
561 buf = (uint16_t *)realloc(buf, sizeof(*buf) * storage);
562 if (buf == NULL) {
563 enc->error = -1;
564 return NULL;
565 }
566 enc->precarry_buf = buf;
567 enc->precarry_storage = storage;
568 }
569 n = (1 << (c + 16)) - 1;
570 do {
Yaowu Xu88cbc582016-11-16 15:18:13 -0800571 OD_ASSERT(offs < storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500572 buf[offs++] = (uint16_t)(e >> (c + 16));
573 e &= n;
574 s -= 8;
575 c -= 8;
576 n >>= 8;
577 } while (s > 0);
578 }
579 /*Make sure there's enough room for the entropy-coded bits and the raw
580 bits.*/
581 out = enc->buf;
582 storage = enc->storage;
583 end_offs = enc->end_offs;
584 e = enc->end_window;
585 nend_bits = enc->nend_bits;
586 s = -s;
587 c = OD_MAXI((nend_bits - s + 7) >> 3, 0);
588 if (offs + end_offs + c > storage) {
589 storage = offs + end_offs + c;
590 out = (unsigned char *)realloc(out, sizeof(*out) * storage);
591 if (out == NULL) {
592 enc->error = -1;
593 return NULL;
594 }
595 OD_MOVE(out + storage - end_offs, out + enc->storage - end_offs, end_offs);
596 enc->buf = out;
597 enc->storage = storage;
598 }
599 /*If we have buffered raw bits, flush them as well.*/
600 while (nend_bits > s) {
601 OD_ASSERT(end_offs < storage);
602 out[storage - ++end_offs] = (unsigned char)e;
603 e >>= 8;
604 nend_bits -= 8;
605 }
606 *nbytes = offs + end_offs;
607 /*Perform carry propagation.*/
608 OD_ASSERT(offs + end_offs <= storage);
609 out = out + storage - (offs + end_offs);
610 c = 0;
611 end_offs = offs;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800612 while (offs > 0) {
613 offs--;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500614 c = buf[offs] + c;
615 out[offs] = (unsigned char)c;
616 c >>= 8;
617 }
618 /*Add any remaining raw bits to the last byte.
619 There is guaranteed to be enough room, because nend_bits <= s.*/
620 OD_ASSERT(nend_bits <= 0 || end_offs > 0);
621 if (nend_bits > 0) out[end_offs - 1] |= (unsigned char)e;
622 /*Note: Unless there's an allocation error, if you keep encoding into the
623 current buffer and call this function again later, everything will work
624 just fine (you won't get a new packet out, but you will get a single
625 buffer with the new data appended to the old).
626 However, this function is O(N) where N is the amount of data coded so far,
627 so calling it more than once for a given packet is a bad idea.*/
628 return out;
629}
630
631/*Returns the number of bits "used" by the encoded symbols so far.
632 This same number can be computed in either the encoder or the decoder, and is
633 suitable for making coding decisions.
634 Warning: The value returned by this function can decrease compared to an
635 earlier call, even after encoding more data, if there is an encoding error
636 (i.e., a failure to allocate enough space for the output buffer).
637 Return: The number of bits.
638 This will always be slightly larger than the exact value (e.g., all
639 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400640int od_ec_enc_tell(const od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500641 /*The 10 here counteracts the offset of -9 baked into cnt, and adds 1 extra
642 bit, which we reserve for terminating the stream.*/
643 return (enc->offs + enc->end_offs) * 8 + enc->cnt + enc->nend_bits + 10;
644}
645
646/*Returns the number of bits "used" by the encoded symbols so far.
647 This same number can be computed in either the encoder or the decoder, and is
648 suitable for making coding decisions.
649 Warning: The value returned by this function can decrease compared to an
650 earlier call, even after encoding more data, if there is an encoding error
651 (i.e., a failure to allocate enough space for the output buffer).
652 Return: The number of bits scaled by 2**OD_BITRES.
653 This will always be slightly larger than the exact value (e.g., all
654 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400655uint32_t od_ec_enc_tell_frac(const od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500656 return od_ec_tell_frac(od_ec_enc_tell(enc), enc->rng);
657}
658
659/*Saves a entropy coder checkpoint to dst.
660 This allows an encoder to reverse a series of entropy coder
661 decisions if it decides that the information would have been
662 better coded some other way.*/
663void od_ec_enc_checkpoint(od_ec_enc *dst, const od_ec_enc *src) {
664 OD_COPY(dst, src, 1);
665}
666
667/*Restores an entropy coder checkpoint saved by od_ec_enc_checkpoint.
668 This can only be used to restore from checkpoints earlier in the target
669 state's history: you can not switch backwards and forwards or otherwise
670 switch to a state which isn't a casual ancestor of the current state.
671 Restore is also incompatible with patching the initial bits, as the
672 changes will remain in the restored version.*/
673void od_ec_enc_rollback(od_ec_enc *dst, const od_ec_enc *src) {
674 unsigned char *buf;
675 uint32_t storage;
676 uint16_t *precarry_buf;
677 uint32_t precarry_storage;
678 OD_ASSERT(dst->storage >= src->storage);
679 OD_ASSERT(dst->precarry_storage >= src->precarry_storage);
680 buf = dst->buf;
681 storage = dst->storage;
682 precarry_buf = dst->precarry_buf;
683 precarry_storage = dst->precarry_storage;
684 OD_COPY(dst, src, 1);
685 dst->buf = buf;
686 dst->storage = storage;
687 dst->precarry_buf = precarry_buf;
688 dst->precarry_storage = precarry_storage;
689}