blob: 8ecb0ce55699997416ae18eab86d86d39035ad86 [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>
Sebastien Alaiwan2209fc02018-03-02 15:38:15 +010018#include <math.h>
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +010019#include <assert.h>
Yaowu Xu931bc2a2016-10-14 13:53:51 -070020#include "aom_dsp/entenc.h"
Thomas Daede837262b2017-11-06 20:07:01 -080021#include "aom_dsp/prob.h"
Nathan E. Egge1078dee2016-03-06 10:59:29 -050022
Sebastien Alaiwan2209fc02018-03-02 15:38:15 +010023#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. Egge1078dee2016-03-06 10:59:29 -050030/*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.*/
60static 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 Alaiwan0d52f122018-03-06 09:54:23 +010066 assert(rng <= 65535U);
Nathan E. Egge1078dee2016-03-06 10:59:29 -050067 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 Alaiwan0d52f122018-03-06 09:54:23 +010096 assert(offs < storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -050097 buf[offs++] = (uint16_t)(low >> c);
98 low &= m;
99 c -= 8;
100 m >>= 8;
101 }
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100102 assert(offs < storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500103 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.*/
115void 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.*/
132void 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.*/
150void od_ec_enc_clear(od_ec_enc *enc) {
151 free(enc->precarry_buf);
152 free(enc->buf);
153}
154
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500155/*Encodes a symbol given its frequency in Q15.
Thomas Daede837262b2017-11-06 20:07:01 -0800156 fl: CDF_PROB_TOP minus the cumulative frequency of all symbols that come
157 before the
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700158 one to be encoded.
Thomas Daede837262b2017-11-06 20:07:01 -0800159 fh: CDF_PROB_TOP minus the cumulative frequency of all symbols up to and
160 including
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700161 the one to be encoded.*/
Thomas Davies736ddef2017-11-09 09:46:08 +0000162static void od_ec_encode_q15(od_ec_enc *enc, unsigned fl, unsigned fh, int s,
163 int nsyms) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500164 od_ec_window l;
165 unsigned r;
166 unsigned u;
167 unsigned v;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500168 l = enc->low;
169 r = enc->rng;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100170 assert(32768U <= r);
171 assert(fh <= fl);
172 assert(fl <= 32768U);
173 assert(7 - EC_PROB_SHIFT - CDF_SHIFT >= 0);
Thomas Davies736ddef2017-11-09 09:46:08 +0000174 const int N = nsyms - 1;
Thomas Daede837262b2017-11-06 20:07:01 -0800175 if (fl < CDF_PROB_TOP) {
176 u = ((r >> 8) * (uint32_t)(fl >> EC_PROB_SHIFT) >>
177 (7 - EC_PROB_SHIFT - CDF_SHIFT)) +
Thomas Davies736ddef2017-11-09 09:46:08 +0000178 EC_MIN_PROB * (N - (s - 1));
Thomas Daede837262b2017-11-06 20:07:01 -0800179 v = ((r >> 8) * (uint32_t)(fh >> EC_PROB_SHIFT) >>
180 (7 - EC_PROB_SHIFT - CDF_SHIFT)) +
Thomas Davies736ddef2017-11-09 09:46:08 +0000181 EC_MIN_PROB * (N - (s + 0));
Timothy B. Terriberryb1c57602017-02-16 10:53:39 -0800182 l += r - u;
183 r = u - v;
184 } else {
Thomas Daede837262b2017-11-06 20:07:01 -0800185 r -= ((r >> 8) * (uint32_t)(fh >> EC_PROB_SHIFT) >>
186 (7 - EC_PROB_SHIFT - CDF_SHIFT)) +
Thomas Davies736ddef2017-11-09 09:46:08 +0000187 EC_MIN_PROB * (N - (s + 0));
Timothy B. Terriberryb1c57602017-02-16 10:53:39 -0800188 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500189 od_ec_enc_normalize(enc, l, r);
190#if OD_MEASURE_EC_OVERHEAD
Thomas Daede837262b2017-11-06 20:07:01 -0800191 enc->entropy -= OD_LOG2((double)(OD_ICDF(fh) - OD_ICDF(fl)) / CDF_PROB_TOP.);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500192 enc->nb_symbols++;
193#endif
194}
195
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800196/*Encode a single binary value.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500197 val: The value to encode (0 or 1).
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700198 f: The probability that the val is one, scaled by 32768.*/
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800199void od_ec_encode_bool_q15(od_ec_enc *enc, int val, unsigned f) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500200 od_ec_window l;
201 unsigned r;
202 unsigned v;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100203 assert(0 < f);
204 assert(f < 32768U);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500205 l = enc->low;
206 r = enc->rng;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100207 assert(32768U <= r);
Thomas Davies736ddef2017-11-09 09:46:08 +0000208 v = ((r >> 8) * (uint32_t)(f >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT));
Jonathan Matthews9ade3942017-11-23 08:44:07 +0000209 v += EC_MIN_PROB;
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800210 if (val) l += r - v;
211 r = val ? v : r - v;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500212 od_ec_enc_normalize(enc, l, r);
213#if OD_MEASURE_EC_OVERHEAD
Thomas Daede837262b2017-11-06 20:07:01 -0800214 enc->entropy -= OD_LOG2((double)(val ? f : (32768 - f)) / 32768.);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500215 enc->nb_symbols++;
216#endif
217}
218
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500219/*Encodes a symbol given a cumulative distribution function (CDF) table in Q15.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500220 s: The index of the symbol to encode.
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700221 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. Egge1078dee2016-03-06 10:59:29 -0500225 nsyms: The number of symbols in the alphabet.
226 This should be at most 16.*/
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700227void od_ec_encode_cdf_q15(od_ec_enc *enc, int s, const uint16_t *icdf,
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500228 int nsyms) {
229 (void)nsyms;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100230 assert(s >= 0);
231 assert(s < nsyms);
232 assert(icdf[nsyms - 1] == OD_ICDF(CDF_PROB_TOP));
Thomas Davies736ddef2017-11-09 09:46:08 +0000233 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 -0500234}
235
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500236/*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.*/
250void od_ec_enc_patch_initial_bits(od_ec_enc *enc, unsigned val, int nbits) {
251 int shift;
252 unsigned mask;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100253 assert(nbits >= 0);
254 assert(nbits <= 8);
255 assert(val < 1U << nbits);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500256 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 Xu931bc2a2016-10-14 13:53:51 -0700266 } else {
267 /*The encoder hasn't even encoded _nbits of data yet.*/
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500268 enc->error = -1;
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700269 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500270}
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.*/
282unsigned 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 Xu88cbc582016-11-16 15:18:13 -0800286 uint32_t offs;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500287 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 Xu88cbc582016-11-16 15:18:13 -0800326 if (offs + ((s + 7) >> 3) > storage) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500327 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 Alaiwan0d52f122018-03-06 09:54:23 +0100338 assert(offs < storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500339 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 Alaiwan0d52f122018-03-06 09:54:23 +0100368 assert(end_offs < storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500369 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 Alaiwan0d52f122018-03-06 09:54:23 +0100375 assert(offs + end_offs <= storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500376 out = out + storage - (offs + end_offs);
377 c = 0;
378 end_offs = offs;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800379 while (offs > 0) {
380 offs--;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500381 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 Alaiwan0d52f122018-03-06 09:54:23 +0100387 assert(nend_bits <= 0 || end_offs > 0);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500388 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. Egge19698a72016-08-18 02:34:53 -0400407int od_ec_enc_tell(const od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500408 /*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 Xu6394f0f2018-01-20 18:50:44 -0800410 return (enc->cnt + 10 + enc->nend_bits) + (enc->offs + enc->end_offs) * 8;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500411}
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. Egge19698a72016-08-18 02:34:53 -0400422uint32_t od_ec_enc_tell_frac(const od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500423 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.*/
430void 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.*/
440void 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 Alaiwan0d52f122018-03-06 09:54:23 +0100445 assert(dst->storage >= src->storage);
446 assert(dst->precarry_storage >= src->precarry_storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500447 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}