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