blob: 751235fe3f60075d0be20213a0aaab0687cb1534 [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.*/
Thomas Davies736ddef2017-11-09 09:46:08 +0000150static void od_ec_encode_q15(od_ec_enc *enc, unsigned fl, unsigned fh, int s,
151 int nsyms) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500152 od_ec_window l;
153 unsigned r;
154 unsigned u;
155 unsigned v;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500156 l = enc->low;
157 r = enc->rng;
158 OD_ASSERT(32768U <= r);
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800159 OD_ASSERT(fh < fl);
160 OD_ASSERT(fl <= 32768U);
Thomas Davies736ddef2017-11-09 09:46:08 +0000161 const int N = nsyms - 1;
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800162 if (fl < 32768U) {
Thomas Davies736ddef2017-11-09 09:46:08 +0000163 u = ((r >> 8) * (uint32_t)(fl >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT)) +
164 EC_MIN_PROB * (N - (s - 1));
165 v = ((r >> 8) * (uint32_t)(fh >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT)) +
166 EC_MIN_PROB * (N - (s + 0));
Timothy B. Terriberryb1c57602017-02-16 10:53:39 -0800167 l += r - u;
168 r = u - v;
169 } else {
Thomas Davies736ddef2017-11-09 09:46:08 +0000170 r -= ((r >> 8) * (uint32_t)(fh >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT)) +
171 EC_MIN_PROB * (N - (s + 0));
Timothy B. Terriberryb1c57602017-02-16 10:53:39 -0800172 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500173 od_ec_enc_normalize(enc, l, r);
174#if OD_MEASURE_EC_OVERHEAD
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800175 enc->entropy -= OD_LOG2((double)(OD_ICDF(fh) - OD_ICDF(fl)) / 32768.);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500176 enc->nb_symbols++;
177#endif
178}
179
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800180/*Encode a single binary value.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500181 val: The value to encode (0 or 1).
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700182 f: The probability that the val is one, scaled by 32768.*/
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800183void od_ec_encode_bool_q15(od_ec_enc *enc, int val, unsigned f) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500184 od_ec_window l;
185 unsigned r;
186 unsigned v;
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800187 OD_ASSERT(0 < f);
188 OD_ASSERT(f < 32768U);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500189 l = enc->low;
190 r = enc->rng;
191 OD_ASSERT(32768U <= r);
Thomas Davies736ddef2017-11-09 09:46:08 +0000192 v = ((r >> 8) * (uint32_t)(f >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT));
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800193 if (val) l += r - v;
194 r = val ? v : r - v;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500195 od_ec_enc_normalize(enc, l, r);
196#if OD_MEASURE_EC_OVERHEAD
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800197 enc->entropy -=
198 OD_LOG2((double)(val ? 32768 - OD_ICDF(f) : OD_ICDF(f)) / 32768.);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500199 enc->nb_symbols++;
200#endif
201}
202
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500203/*Encodes a symbol given a cumulative distribution function (CDF) table in Q15.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500204 s: The index of the symbol to encode.
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700205 icdf: 32768 minus the CDF, such that symbol s falls in the range
206 [s > 0 ? (32768 - icdf[s - 1]) : 0, 32768 - icdf[s]).
207 The values must be monotonically decreasing, and icdf[nsyms - 1] must
208 be 0.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500209 nsyms: The number of symbols in the alphabet.
210 This should be at most 16.*/
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700211void od_ec_encode_cdf_q15(od_ec_enc *enc, int s, const uint16_t *icdf,
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500212 int nsyms) {
213 (void)nsyms;
214 OD_ASSERT(s >= 0);
215 OD_ASSERT(s < nsyms);
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700216 OD_ASSERT(icdf[nsyms - 1] == OD_ICDF(32768U));
Thomas Davies736ddef2017-11-09 09:46:08 +0000217 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 -0500218}
219
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500220/*Overwrites a few bits at the very start of an existing stream, after they
221 have already been encoded.
222 This makes it possible to have a few flags up front, where it is easy for
223 decoders to access them without parsing the whole stream, even if their
224 values are not determined until late in the encoding process, without having
225 to buffer all the intermediate symbols in the encoder.
226 In order for this to work, at least nbits bits must have already been encoded
227 using probabilities that are an exact power of two.
228 The encoder can verify the number of encoded bits is sufficient, but cannot
229 check this latter condition.
230 val: The bits to encode (in the least nbits significant bits).
231 They will be decoded in order from most-significant to least.
232 nbits: The number of bits to overwrite.
233 This must be no more than 8.*/
234void od_ec_enc_patch_initial_bits(od_ec_enc *enc, unsigned val, int nbits) {
235 int shift;
236 unsigned mask;
237 OD_ASSERT(nbits >= 0);
238 OD_ASSERT(nbits <= 8);
239 OD_ASSERT(val < 1U << nbits);
240 shift = 8 - nbits;
241 mask = ((1U << nbits) - 1) << shift;
242 if (enc->offs > 0) {
243 /*The first byte has been finalized.*/
244 enc->precarry_buf[0] =
245 (uint16_t)((enc->precarry_buf[0] & ~mask) | val << shift);
246 } else if (9 + enc->cnt + (enc->rng == 0x8000) > nbits) {
247 /*The first byte has yet to be output.*/
248 enc->low = (enc->low & ~((od_ec_window)mask << (16 + enc->cnt))) |
249 (od_ec_window)val << (16 + enc->cnt + shift);
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700250 } else {
251 /*The encoder hasn't even encoded _nbits of data yet.*/
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500252 enc->error = -1;
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700253 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500254}
255
256#if OD_MEASURE_EC_OVERHEAD
257#include <stdio.h>
258#endif
259
260/*Indicates that there are no more symbols to encode.
261 All remaining output bytes are flushed to the output buffer.
262 od_ec_enc_reset() should be called before using the encoder again.
263 bytes: Returns the size of the encoded data in the returned buffer.
264 Return: A pointer to the start of the final buffer, or NULL if there was an
265 encoding error.*/
266unsigned char *od_ec_enc_done(od_ec_enc *enc, uint32_t *nbytes) {
267 unsigned char *out;
268 uint32_t storage;
269 uint16_t *buf;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800270 uint32_t offs;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500271 uint32_t end_offs;
272 int nend_bits;
273 od_ec_window m;
274 od_ec_window e;
275 od_ec_window l;
276 unsigned r;
277 int c;
278 int s;
279 if (enc->error) return NULL;
280#if OD_MEASURE_EC_OVERHEAD
281 {
282 uint32_t tell;
283 /* Don't count the 1 bit we lose to raw bits as overhead. */
284 tell = od_ec_enc_tell(enc) - 1;
285 fprintf(stderr, "overhead: %f%%\n",
286 100 * (tell - enc->entropy) / enc->entropy);
287 fprintf(stderr, "efficiency: %f bits/symbol\n",
288 (double)tell / enc->nb_symbols);
289 }
290#endif
291 /*We output the minimum number of bits that ensures that the symbols encoded
292 thus far will be decoded correctly regardless of the bits that follow.*/
293 l = enc->low;
294 r = enc->rng;
295 c = enc->cnt;
296 s = 9;
297 m = 0x7FFF;
298 e = (l + m) & ~m;
299 while ((e | m) >= l + r) {
300 s++;
301 m >>= 1;
302 e = (l + m) & ~m;
303 }
304 s += c;
305 offs = enc->offs;
306 buf = enc->precarry_buf;
307 if (s > 0) {
308 unsigned n;
309 storage = enc->precarry_storage;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800310 if (offs + ((s + 7) >> 3) > storage) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500311 storage = storage * 2 + ((s + 7) >> 3);
312 buf = (uint16_t *)realloc(buf, sizeof(*buf) * storage);
313 if (buf == NULL) {
314 enc->error = -1;
315 return NULL;
316 }
317 enc->precarry_buf = buf;
318 enc->precarry_storage = storage;
319 }
320 n = (1 << (c + 16)) - 1;
321 do {
Yaowu Xu88cbc582016-11-16 15:18:13 -0800322 OD_ASSERT(offs < storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500323 buf[offs++] = (uint16_t)(e >> (c + 16));
324 e &= n;
325 s -= 8;
326 c -= 8;
327 n >>= 8;
328 } while (s > 0);
329 }
330 /*Make sure there's enough room for the entropy-coded bits and the raw
331 bits.*/
332 out = enc->buf;
333 storage = enc->storage;
334 end_offs = enc->end_offs;
335 e = enc->end_window;
336 nend_bits = enc->nend_bits;
337 s = -s;
338 c = OD_MAXI((nend_bits - s + 7) >> 3, 0);
339 if (offs + end_offs + c > storage) {
340 storage = offs + end_offs + c;
341 out = (unsigned char *)realloc(out, sizeof(*out) * storage);
342 if (out == NULL) {
343 enc->error = -1;
344 return NULL;
345 }
346 OD_MOVE(out + storage - end_offs, out + enc->storage - end_offs, end_offs);
347 enc->buf = out;
348 enc->storage = storage;
349 }
350 /*If we have buffered raw bits, flush them as well.*/
351 while (nend_bits > s) {
352 OD_ASSERT(end_offs < storage);
353 out[storage - ++end_offs] = (unsigned char)e;
354 e >>= 8;
355 nend_bits -= 8;
356 }
357 *nbytes = offs + end_offs;
358 /*Perform carry propagation.*/
359 OD_ASSERT(offs + end_offs <= storage);
360 out = out + storage - (offs + end_offs);
361 c = 0;
362 end_offs = offs;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800363 while (offs > 0) {
364 offs--;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500365 c = buf[offs] + c;
366 out[offs] = (unsigned char)c;
367 c >>= 8;
368 }
369 /*Add any remaining raw bits to the last byte.
370 There is guaranteed to be enough room, because nend_bits <= s.*/
371 OD_ASSERT(nend_bits <= 0 || end_offs > 0);
372 if (nend_bits > 0) out[end_offs - 1] |= (unsigned char)e;
373 /*Note: Unless there's an allocation error, if you keep encoding into the
374 current buffer and call this function again later, everything will work
375 just fine (you won't get a new packet out, but you will get a single
376 buffer with the new data appended to the old).
377 However, this function is O(N) where N is the amount of data coded so far,
378 so calling it more than once for a given packet is a bad idea.*/
379 return out;
380}
381
382/*Returns the number of bits "used" by the encoded symbols so far.
383 This same number can be computed in either the encoder or the decoder, and is
384 suitable for making coding decisions.
385 Warning: The value returned by this function can decrease compared to an
386 earlier call, even after encoding more data, if there is an encoding error
387 (i.e., a failure to allocate enough space for the output buffer).
388 Return: The number of bits.
389 This will always be slightly larger than the exact value (e.g., all
390 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400391int od_ec_enc_tell(const od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500392 /*The 10 here counteracts the offset of -9 baked into cnt, and adds 1 extra
393 bit, which we reserve for terminating the stream.*/
394 return (enc->offs + enc->end_offs) * 8 + enc->cnt + enc->nend_bits + 10;
395}
396
397/*Returns the number of bits "used" by the encoded symbols so far.
398 This same number can be computed in either the encoder or the decoder, and is
399 suitable for making coding decisions.
400 Warning: The value returned by this function can decrease compared to an
401 earlier call, even after encoding more data, if there is an encoding error
402 (i.e., a failure to allocate enough space for the output buffer).
403 Return: The number of bits scaled by 2**OD_BITRES.
404 This will always be slightly larger than the exact value (e.g., all
405 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400406uint32_t od_ec_enc_tell_frac(const od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500407 return od_ec_tell_frac(od_ec_enc_tell(enc), enc->rng);
408}
409
410/*Saves a entropy coder checkpoint to dst.
411 This allows an encoder to reverse a series of entropy coder
412 decisions if it decides that the information would have been
413 better coded some other way.*/
414void od_ec_enc_checkpoint(od_ec_enc *dst, const od_ec_enc *src) {
415 OD_COPY(dst, src, 1);
416}
417
418/*Restores an entropy coder checkpoint saved by od_ec_enc_checkpoint.
419 This can only be used to restore from checkpoints earlier in the target
420 state's history: you can not switch backwards and forwards or otherwise
421 switch to a state which isn't a casual ancestor of the current state.
422 Restore is also incompatible with patching the initial bits, as the
423 changes will remain in the restored version.*/
424void od_ec_enc_rollback(od_ec_enc *dst, const od_ec_enc *src) {
425 unsigned char *buf;
426 uint32_t storage;
427 uint16_t *precarry_buf;
428 uint32_t precarry_storage;
429 OD_ASSERT(dst->storage >= src->storage);
430 OD_ASSERT(dst->precarry_storage >= src->precarry_storage);
431 buf = dst->buf;
432 storage = dst->storage;
433 precarry_buf = dst->precarry_buf;
434 precarry_storage = dst->precarry_storage;
435 OD_COPY(dst, src, 1);
436 dst->buf = buf;
437 dst->storage = storage;
438 dst->precarry_buf = precarry_buf;
439 dst->precarry_storage = precarry_storage;
440}