blob: b8c4dc04745d7a884195665a5abd8adc76633bf6 [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
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500145/*Encodes a symbol given its frequency in Q15.
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700146 fl: 32768 minus the cumulative frequency of all symbols that come before the
147 one to be encoded.
148 fh: 32768 minus the cumulative frequency of all symbols up to and including
149 the one to be encoded.*/
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500150static void od_ec_encode_q15(od_ec_enc *enc, unsigned fl, unsigned fh) {
151 od_ec_window l;
152 unsigned r;
153 unsigned u;
154 unsigned v;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500155 l = enc->low;
156 r = enc->rng;
157 OD_ASSERT(32768U <= r);
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800158 OD_ASSERT(fh < fl);
159 OD_ASSERT(fl <= 32768U);
160 if (fl < 32768U) {
161 u = (r >> 8) * (uint32_t)fl >> 7;
162 v = (r >> 8) * (uint32_t)fh >> 7;
Timothy B. Terriberryb1c57602017-02-16 10:53:39 -0800163 l += r - u;
164 r = u - v;
165 } else {
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800166 r -= (r >> 8) * (uint32_t)fh >> 7;
Timothy B. Terriberryb1c57602017-02-16 10:53:39 -0800167 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500168 od_ec_enc_normalize(enc, l, r);
169#if OD_MEASURE_EC_OVERHEAD
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800170 enc->entropy -= OD_LOG2((double)(OD_ICDF(fh) - OD_ICDF(fl)) / 32768.);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500171 enc->nb_symbols++;
172#endif
173}
174
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800175/*Encode a single binary value.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500176 val: The value to encode (0 or 1).
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700177 f: The probability that the val is one, scaled by 32768.*/
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800178void od_ec_encode_bool_q15(od_ec_enc *enc, int val, unsigned f) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500179 od_ec_window l;
180 unsigned r;
181 unsigned v;
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800182 OD_ASSERT(0 < f);
183 OD_ASSERT(f < 32768U);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500184 l = enc->low;
185 r = enc->rng;
186 OD_ASSERT(32768U <= r);
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800187 v = (r >> 8) * (uint32_t)f >> 7;
188 if (val) l += r - v;
189 r = val ? v : r - v;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500190 od_ec_enc_normalize(enc, l, r);
191#if OD_MEASURE_EC_OVERHEAD
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800192 enc->entropy -=
193 OD_LOG2((double)(val ? 32768 - OD_ICDF(f) : OD_ICDF(f)) / 32768.);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500194 enc->nb_symbols++;
195#endif
196}
197
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500198/*Encodes a symbol given a cumulative distribution function (CDF) table in Q15.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500199 s: The index of the symbol to encode.
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700200 icdf: 32768 minus the CDF, such that symbol s falls in the range
201 [s > 0 ? (32768 - icdf[s - 1]) : 0, 32768 - icdf[s]).
202 The values must be monotonically decreasing, and icdf[nsyms - 1] must
203 be 0.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500204 nsyms: The number of symbols in the alphabet.
205 This should be at most 16.*/
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700206void od_ec_encode_cdf_q15(od_ec_enc *enc, int s, const uint16_t *icdf,
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500207 int nsyms) {
208 (void)nsyms;
209 OD_ASSERT(s >= 0);
210 OD_ASSERT(s < nsyms);
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700211 OD_ASSERT(icdf[nsyms - 1] == OD_ICDF(32768U));
212 od_ec_encode_q15(enc, s > 0 ? icdf[s - 1] : OD_ICDF(0), icdf[s]);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500213}
214
Nathan E. Egge24f1a902017-02-14 13:29:44 -0500215#if CONFIG_RAWBITS
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500216/*Encodes a sequence of raw bits in the stream.
217 fl: The bits to encode.
218 ftb: The number of bits to encode.
219 This must be between 0 and 25, inclusive.*/
220void od_ec_enc_bits(od_ec_enc *enc, uint32_t fl, unsigned ftb) {
221 od_ec_window end_window;
222 int nend_bits;
223 OD_ASSERT(ftb <= 25);
224 OD_ASSERT(fl < (uint32_t)1 << ftb);
225#if OD_MEASURE_EC_OVERHEAD
226 enc->entropy += ftb;
227#endif
228 end_window = enc->end_window;
229 nend_bits = enc->nend_bits;
230 if (nend_bits + ftb > OD_EC_WINDOW_SIZE) {
231 unsigned char *buf;
232 uint32_t storage;
233 uint32_t end_offs;
234 buf = enc->buf;
235 storage = enc->storage;
236 end_offs = enc->end_offs;
237 if (end_offs + (OD_EC_WINDOW_SIZE >> 3) >= storage) {
238 unsigned char *new_buf;
239 uint32_t new_storage;
240 new_storage = 2 * storage + (OD_EC_WINDOW_SIZE >> 3);
241 new_buf = (unsigned char *)malloc(sizeof(*new_buf) * new_storage);
242 if (new_buf == NULL) {
243 enc->error = -1;
244 enc->end_offs = 0;
245 return;
246 }
247 OD_COPY(new_buf + new_storage - end_offs, buf + storage - end_offs,
248 end_offs);
249 storage = new_storage;
250 free(buf);
251 enc->buf = buf = new_buf;
252 enc->storage = storage;
253 }
254 do {
255 OD_ASSERT(end_offs < storage);
256 buf[storage - ++end_offs] = (unsigned char)end_window;
257 end_window >>= 8;
258 nend_bits -= 8;
259 } while (nend_bits >= 8);
260 enc->end_offs = end_offs;
261 }
262 OD_ASSERT(nend_bits + ftb <= OD_EC_WINDOW_SIZE);
263 end_window |= (od_ec_window)fl << nend_bits;
264 nend_bits += ftb;
265 enc->end_window = end_window;
266 enc->nend_bits = nend_bits;
267}
Nathan E. Egge24f1a902017-02-14 13:29:44 -0500268#endif
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500269
270/*Overwrites a few bits at the very start of an existing stream, after they
271 have already been encoded.
272 This makes it possible to have a few flags up front, where it is easy for
273 decoders to access them without parsing the whole stream, even if their
274 values are not determined until late in the encoding process, without having
275 to buffer all the intermediate symbols in the encoder.
276 In order for this to work, at least nbits bits must have already been encoded
277 using probabilities that are an exact power of two.
278 The encoder can verify the number of encoded bits is sufficient, but cannot
279 check this latter condition.
280 val: The bits to encode (in the least nbits significant bits).
281 They will be decoded in order from most-significant to least.
282 nbits: The number of bits to overwrite.
283 This must be no more than 8.*/
284void od_ec_enc_patch_initial_bits(od_ec_enc *enc, unsigned val, int nbits) {
285 int shift;
286 unsigned mask;
287 OD_ASSERT(nbits >= 0);
288 OD_ASSERT(nbits <= 8);
289 OD_ASSERT(val < 1U << nbits);
290 shift = 8 - nbits;
291 mask = ((1U << nbits) - 1) << shift;
292 if (enc->offs > 0) {
293 /*The first byte has been finalized.*/
294 enc->precarry_buf[0] =
295 (uint16_t)((enc->precarry_buf[0] & ~mask) | val << shift);
296 } else if (9 + enc->cnt + (enc->rng == 0x8000) > nbits) {
297 /*The first byte has yet to be output.*/
298 enc->low = (enc->low & ~((od_ec_window)mask << (16 + enc->cnt))) |
299 (od_ec_window)val << (16 + enc->cnt + shift);
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700300 } else {
301 /*The encoder hasn't even encoded _nbits of data yet.*/
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500302 enc->error = -1;
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700303 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500304}
305
306#if OD_MEASURE_EC_OVERHEAD
307#include <stdio.h>
308#endif
309
310/*Indicates that there are no more symbols to encode.
311 All remaining output bytes are flushed to the output buffer.
312 od_ec_enc_reset() should be called before using the encoder again.
313 bytes: Returns the size of the encoded data in the returned buffer.
314 Return: A pointer to the start of the final buffer, or NULL if there was an
315 encoding error.*/
316unsigned char *od_ec_enc_done(od_ec_enc *enc, uint32_t *nbytes) {
317 unsigned char *out;
318 uint32_t storage;
319 uint16_t *buf;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800320 uint32_t offs;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500321 uint32_t end_offs;
322 int nend_bits;
323 od_ec_window m;
324 od_ec_window e;
325 od_ec_window l;
326 unsigned r;
327 int c;
328 int s;
329 if (enc->error) return NULL;
330#if OD_MEASURE_EC_OVERHEAD
331 {
332 uint32_t tell;
333 /* Don't count the 1 bit we lose to raw bits as overhead. */
334 tell = od_ec_enc_tell(enc) - 1;
335 fprintf(stderr, "overhead: %f%%\n",
336 100 * (tell - enc->entropy) / enc->entropy);
337 fprintf(stderr, "efficiency: %f bits/symbol\n",
338 (double)tell / enc->nb_symbols);
339 }
340#endif
341 /*We output the minimum number of bits that ensures that the symbols encoded
342 thus far will be decoded correctly regardless of the bits that follow.*/
343 l = enc->low;
344 r = enc->rng;
345 c = enc->cnt;
346 s = 9;
347 m = 0x7FFF;
348 e = (l + m) & ~m;
349 while ((e | m) >= l + r) {
350 s++;
351 m >>= 1;
352 e = (l + m) & ~m;
353 }
354 s += c;
355 offs = enc->offs;
356 buf = enc->precarry_buf;
357 if (s > 0) {
358 unsigned n;
359 storage = enc->precarry_storage;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800360 if (offs + ((s + 7) >> 3) > storage) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500361 storage = storage * 2 + ((s + 7) >> 3);
362 buf = (uint16_t *)realloc(buf, sizeof(*buf) * storage);
363 if (buf == NULL) {
364 enc->error = -1;
365 return NULL;
366 }
367 enc->precarry_buf = buf;
368 enc->precarry_storage = storage;
369 }
370 n = (1 << (c + 16)) - 1;
371 do {
Yaowu Xu88cbc582016-11-16 15:18:13 -0800372 OD_ASSERT(offs < storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500373 buf[offs++] = (uint16_t)(e >> (c + 16));
374 e &= n;
375 s -= 8;
376 c -= 8;
377 n >>= 8;
378 } while (s > 0);
379 }
380 /*Make sure there's enough room for the entropy-coded bits and the raw
381 bits.*/
382 out = enc->buf;
383 storage = enc->storage;
384 end_offs = enc->end_offs;
385 e = enc->end_window;
386 nend_bits = enc->nend_bits;
387 s = -s;
388 c = OD_MAXI((nend_bits - s + 7) >> 3, 0);
389 if (offs + end_offs + c > storage) {
390 storage = offs + end_offs + c;
391 out = (unsigned char *)realloc(out, sizeof(*out) * storage);
392 if (out == NULL) {
393 enc->error = -1;
394 return NULL;
395 }
396 OD_MOVE(out + storage - end_offs, out + enc->storage - end_offs, end_offs);
397 enc->buf = out;
398 enc->storage = storage;
399 }
400 /*If we have buffered raw bits, flush them as well.*/
401 while (nend_bits > s) {
402 OD_ASSERT(end_offs < storage);
403 out[storage - ++end_offs] = (unsigned char)e;
404 e >>= 8;
405 nend_bits -= 8;
406 }
407 *nbytes = offs + end_offs;
408 /*Perform carry propagation.*/
409 OD_ASSERT(offs + end_offs <= storage);
410 out = out + storage - (offs + end_offs);
411 c = 0;
412 end_offs = offs;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800413 while (offs > 0) {
414 offs--;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500415 c = buf[offs] + c;
416 out[offs] = (unsigned char)c;
417 c >>= 8;
418 }
419 /*Add any remaining raw bits to the last byte.
420 There is guaranteed to be enough room, because nend_bits <= s.*/
421 OD_ASSERT(nend_bits <= 0 || end_offs > 0);
422 if (nend_bits > 0) out[end_offs - 1] |= (unsigned char)e;
423 /*Note: Unless there's an allocation error, if you keep encoding into the
424 current buffer and call this function again later, everything will work
425 just fine (you won't get a new packet out, but you will get a single
426 buffer with the new data appended to the old).
427 However, this function is O(N) where N is the amount of data coded so far,
428 so calling it more than once for a given packet is a bad idea.*/
429 return out;
430}
431
432/*Returns the number of bits "used" by the encoded symbols so far.
433 This same number can be computed in either the encoder or the decoder, and is
434 suitable for making coding decisions.
435 Warning: The value returned by this function can decrease compared to an
436 earlier call, even after encoding more data, if there is an encoding error
437 (i.e., a failure to allocate enough space for the output buffer).
438 Return: The number of bits.
439 This will always be slightly larger than the exact value (e.g., all
440 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400441int od_ec_enc_tell(const od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500442 /*The 10 here counteracts the offset of -9 baked into cnt, and adds 1 extra
443 bit, which we reserve for terminating the stream.*/
444 return (enc->offs + enc->end_offs) * 8 + enc->cnt + enc->nend_bits + 10;
445}
446
447/*Returns the number of bits "used" by the encoded symbols so far.
448 This same number can be computed in either the encoder or the decoder, and is
449 suitable for making coding decisions.
450 Warning: The value returned by this function can decrease compared to an
451 earlier call, even after encoding more data, if there is an encoding error
452 (i.e., a failure to allocate enough space for the output buffer).
453 Return: The number of bits scaled by 2**OD_BITRES.
454 This will always be slightly larger than the exact value (e.g., all
455 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400456uint32_t od_ec_enc_tell_frac(const od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500457 return od_ec_tell_frac(od_ec_enc_tell(enc), enc->rng);
458}
459
460/*Saves a entropy coder checkpoint to dst.
461 This allows an encoder to reverse a series of entropy coder
462 decisions if it decides that the information would have been
463 better coded some other way.*/
464void od_ec_enc_checkpoint(od_ec_enc *dst, const od_ec_enc *src) {
465 OD_COPY(dst, src, 1);
466}
467
468/*Restores an entropy coder checkpoint saved by od_ec_enc_checkpoint.
469 This can only be used to restore from checkpoints earlier in the target
470 state's history: you can not switch backwards and forwards or otherwise
471 switch to a state which isn't a casual ancestor of the current state.
472 Restore is also incompatible with patching the initial bits, as the
473 changes will remain in the restored version.*/
474void od_ec_enc_rollback(od_ec_enc *dst, const od_ec_enc *src) {
475 unsigned char *buf;
476 uint32_t storage;
477 uint16_t *precarry_buf;
478 uint32_t precarry_storage;
479 OD_ASSERT(dst->storage >= src->storage);
480 OD_ASSERT(dst->precarry_storage >= src->precarry_storage);
481 buf = dst->buf;
482 storage = dst->storage;
483 precarry_buf = dst->precarry_buf;
484 precarry_storage = dst->precarry_storage;
485 OD_COPY(dst, src, 1);
486 dst->buf = buf;
487 dst->storage = storage;
488 dst->precarry_buf = precarry_buf;
489 dst->precarry_storage = precarry_storage;
490}