blob: 2fd4493eabd75cd0c164bc4bb375a4a24e721c06 [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
Nathan E. Egge1078dee2016-03-06 10:59:29 -050012#include <stdlib.h>
13#include <string.h>
Sebastien Alaiwan2209fc02018-03-02 15:38:15 +010014#include <math.h>
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +010015#include <assert.h>
Yaowu Xu931bc2a2016-10-14 13:53:51 -070016#include "aom_dsp/entenc.h"
Thomas Daede837262b2017-11-06 20:07:01 -080017#include "aom_dsp/prob.h"
Nathan E. Egge1078dee2016-03-06 10:59:29 -050018
Sebastien Alaiwan2209fc02018-03-02 15:38:15 +010019#if OD_MEASURE_EC_OVERHEAD
20#if !defined(M_LOG2E)
21#define M_LOG2E (1.4426950408889634073599246810019)
22#endif
23#define OD_LOG2(x) (M_LOG2E * log(x))
24#endif // OD_MEASURE_EC_OVERHEAD
25
Nathan E. Egge1078dee2016-03-06 10:59:29 -050026/*A range encoder.
27 See entdec.c and the references for implementation details \cite{Mar79,MNW98}.
28
29 @INPROCEEDINGS{Mar79,
30 author="Martin, G.N.N.",
31 title="Range encoding: an algorithm for removing redundancy from a digitised
32 message",
33 booktitle="Video \& Data Recording Conference",
34 year=1979,
35 address="Southampton",
36 month=Jul,
37 URL="http://www.compressconsult.com/rangecoder/rngcod.pdf.gz"
38 }
39 @ARTICLE{MNW98,
40 author="Alistair Moffat and Radford Neal and Ian H. Witten",
41 title="Arithmetic Coding Revisited",
42 journal="{ACM} Transactions on Information Systems",
43 year=1998,
44 volume=16,
45 number=3,
46 pages="256--294",
47 month=Jul,
48 URL="http://researchcommons.waikato.ac.nz/bitstream/handle/10289/78/content.pdf"
49 }*/
50
51/*Takes updated low and range values, renormalizes them so that
52 32768 <= rng < 65536 (flushing bytes from low to the pre-carry buffer if
53 necessary), and stores them back in the encoder context.
54 low: The new value of low.
55 rng: The new value of the range.*/
56static void od_ec_enc_normalize(od_ec_enc *enc, od_ec_window low,
57 unsigned rng) {
58 int d;
59 int c;
60 int s;
61 c = enc->cnt;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +010062 assert(rng <= 65535U);
Wan-Teh Changed997ab2018-08-14 12:35:17 -070063 /*The number of leading zeros in the 16-bit binary representation of rng.*/
Nathan E. Egge1078dee2016-03-06 10:59:29 -050064 d = 16 - OD_ILOG_NZ(rng);
65 s = c + d;
66 /*TODO: Right now we flush every time we have at least one byte available.
67 Instead we should use an od_ec_window and flush right before we're about to
68 shift bits off the end of the window.
69 For a 32-bit window this is about the same amount of work, but for a 64-bit
70 window it should be a fair win.*/
71 if (s >= 0) {
72 uint16_t *buf;
73 uint32_t storage;
74 uint32_t offs;
75 unsigned m;
76 buf = enc->precarry_buf;
77 storage = enc->precarry_storage;
78 offs = enc->offs;
79 if (offs + 2 > storage) {
80 storage = 2 * storage + 2;
81 buf = (uint16_t *)realloc(buf, sizeof(*buf) * storage);
82 if (buf == NULL) {
83 enc->error = -1;
84 enc->offs = 0;
85 return;
86 }
87 enc->precarry_buf = buf;
88 enc->precarry_storage = storage;
89 }
90 c += 16;
91 m = (1 << c) - 1;
92 if (s >= 8) {
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +010093 assert(offs < storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -050094 buf[offs++] = (uint16_t)(low >> c);
95 low &= m;
96 c -= 8;
97 m >>= 8;
98 }
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +010099 assert(offs < storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500100 buf[offs++] = (uint16_t)(low >> c);
101 s = c + d - 24;
102 low &= m;
103 enc->offs = offs;
104 }
105 enc->low = low << d;
106 enc->rng = rng << d;
107 enc->cnt = s;
108}
109
110/*Initializes the encoder.
111 size: The initial size of the buffer, in bytes.*/
112void od_ec_enc_init(od_ec_enc *enc, uint32_t size) {
113 od_ec_enc_reset(enc);
114 enc->buf = (unsigned char *)malloc(sizeof(*enc->buf) * size);
115 enc->storage = size;
116 if (size > 0 && enc->buf == NULL) {
117 enc->storage = 0;
118 enc->error = -1;
119 }
120 enc->precarry_buf = (uint16_t *)malloc(sizeof(*enc->precarry_buf) * size);
121 enc->precarry_storage = size;
122 if (size > 0 && enc->precarry_buf == NULL) {
123 enc->precarry_storage = 0;
124 enc->error = -1;
125 }
126}
127
128/*Reinitializes the encoder.*/
129void od_ec_enc_reset(od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500130 enc->offs = 0;
131 enc->low = 0;
132 enc->rng = 0x8000;
133 /*This is initialized to -9 so that it crosses zero after we've accumulated
134 one byte + one carry bit.*/
135 enc->cnt = -9;
136 enc->error = 0;
137#if OD_MEASURE_EC_OVERHEAD
138 enc->entropy = 0;
139 enc->nb_symbols = 0;
140#endif
141}
142
143/*Frees the buffers used by the encoder.*/
144void od_ec_enc_clear(od_ec_enc *enc) {
145 free(enc->precarry_buf);
146 free(enc->buf);
147}
148
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500149/*Encodes a symbol given its frequency in Q15.
Thomas Daede837262b2017-11-06 20:07:01 -0800150 fl: CDF_PROB_TOP minus the cumulative frequency of all symbols that come
151 before the
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700152 one to be encoded.
Thomas Daede837262b2017-11-06 20:07:01 -0800153 fh: CDF_PROB_TOP minus the cumulative frequency of all symbols up to and
154 including
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700155 the one to be encoded.*/
Thomas Davies736ddef2017-11-09 09:46:08 +0000156static void od_ec_encode_q15(od_ec_enc *enc, unsigned fl, unsigned fh, int s,
157 int nsyms) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500158 od_ec_window l;
159 unsigned r;
160 unsigned u;
161 unsigned v;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500162 l = enc->low;
163 r = enc->rng;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100164 assert(32768U <= r);
165 assert(fh <= fl);
166 assert(fl <= 32768U);
167 assert(7 - EC_PROB_SHIFT - CDF_SHIFT >= 0);
Thomas Davies736ddef2017-11-09 09:46:08 +0000168 const int N = nsyms - 1;
Thomas Daede837262b2017-11-06 20:07:01 -0800169 if (fl < CDF_PROB_TOP) {
170 u = ((r >> 8) * (uint32_t)(fl >> EC_PROB_SHIFT) >>
171 (7 - EC_PROB_SHIFT - CDF_SHIFT)) +
Thomas Davies736ddef2017-11-09 09:46:08 +0000172 EC_MIN_PROB * (N - (s - 1));
Thomas Daede837262b2017-11-06 20:07:01 -0800173 v = ((r >> 8) * (uint32_t)(fh >> EC_PROB_SHIFT) >>
174 (7 - EC_PROB_SHIFT - CDF_SHIFT)) +
Thomas Davies736ddef2017-11-09 09:46:08 +0000175 EC_MIN_PROB * (N - (s + 0));
Timothy B. Terriberryb1c57602017-02-16 10:53:39 -0800176 l += r - u;
177 r = u - v;
178 } else {
Thomas Daede837262b2017-11-06 20:07:01 -0800179 r -= ((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 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500183 od_ec_enc_normalize(enc, l, r);
184#if OD_MEASURE_EC_OVERHEAD
Thomas Daede837262b2017-11-06 20:07:01 -0800185 enc->entropy -= OD_LOG2((double)(OD_ICDF(fh) - OD_ICDF(fl)) / CDF_PROB_TOP.);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500186 enc->nb_symbols++;
187#endif
188}
189
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800190/*Encode a single binary value.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500191 val: The value to encode (0 or 1).
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700192 f: The probability that the val is one, scaled by 32768.*/
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800193void od_ec_encode_bool_q15(od_ec_enc *enc, int val, unsigned f) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500194 od_ec_window l;
195 unsigned r;
196 unsigned v;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100197 assert(0 < f);
198 assert(f < 32768U);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500199 l = enc->low;
200 r = enc->rng;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100201 assert(32768U <= r);
Thomas Davies736ddef2017-11-09 09:46:08 +0000202 v = ((r >> 8) * (uint32_t)(f >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT));
Jonathan Matthews9ade3942017-11-23 08:44:07 +0000203 v += EC_MIN_PROB;
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800204 if (val) l += r - v;
205 r = val ? v : r - v;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500206 od_ec_enc_normalize(enc, l, r);
207#if OD_MEASURE_EC_OVERHEAD
Thomas Daede837262b2017-11-06 20:07:01 -0800208 enc->entropy -= OD_LOG2((double)(val ? f : (32768 - f)) / 32768.);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500209 enc->nb_symbols++;
210#endif
211}
212
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500213/*Encodes a symbol given a cumulative distribution function (CDF) table in Q15.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500214 s: The index of the symbol to encode.
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700215 icdf: 32768 minus the CDF, such that symbol s falls in the range
216 [s > 0 ? (32768 - icdf[s - 1]) : 0, 32768 - icdf[s]).
217 The values must be monotonically decreasing, and icdf[nsyms - 1] must
218 be 0.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500219 nsyms: The number of symbols in the alphabet.
220 This should be at most 16.*/
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700221void od_ec_encode_cdf_q15(od_ec_enc *enc, int s, const uint16_t *icdf,
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500222 int nsyms) {
223 (void)nsyms;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100224 assert(s >= 0);
225 assert(s < nsyms);
226 assert(icdf[nsyms - 1] == OD_ICDF(CDF_PROB_TOP));
Thomas Davies736ddef2017-11-09 09:46:08 +0000227 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 -0500228}
229
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500230/*Overwrites a few bits at the very start of an existing stream, after they
231 have already been encoded.
232 This makes it possible to have a few flags up front, where it is easy for
233 decoders to access them without parsing the whole stream, even if their
234 values are not determined until late in the encoding process, without having
235 to buffer all the intermediate symbols in the encoder.
236 In order for this to work, at least nbits bits must have already been encoded
237 using probabilities that are an exact power of two.
238 The encoder can verify the number of encoded bits is sufficient, but cannot
239 check this latter condition.
240 val: The bits to encode (in the least nbits significant bits).
241 They will be decoded in order from most-significant to least.
242 nbits: The number of bits to overwrite.
243 This must be no more than 8.*/
244void od_ec_enc_patch_initial_bits(od_ec_enc *enc, unsigned val, int nbits) {
245 int shift;
246 unsigned mask;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100247 assert(nbits >= 0);
248 assert(nbits <= 8);
249 assert(val < 1U << nbits);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500250 shift = 8 - nbits;
251 mask = ((1U << nbits) - 1) << shift;
252 if (enc->offs > 0) {
253 /*The first byte has been finalized.*/
254 enc->precarry_buf[0] =
255 (uint16_t)((enc->precarry_buf[0] & ~mask) | val << shift);
256 } else if (9 + enc->cnt + (enc->rng == 0x8000) > nbits) {
257 /*The first byte has yet to be output.*/
258 enc->low = (enc->low & ~((od_ec_window)mask << (16 + enc->cnt))) |
259 (od_ec_window)val << (16 + enc->cnt + shift);
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700260 } else {
261 /*The encoder hasn't even encoded _nbits of data yet.*/
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500262 enc->error = -1;
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700263 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500264}
265
266#if OD_MEASURE_EC_OVERHEAD
267#include <stdio.h>
268#endif
269
270/*Indicates that there are no more symbols to encode.
271 All remaining output bytes are flushed to the output buffer.
272 od_ec_enc_reset() should be called before using the encoder again.
273 bytes: Returns the size of the encoded data in the returned buffer.
274 Return: A pointer to the start of the final buffer, or NULL if there was an
275 encoding error.*/
276unsigned char *od_ec_enc_done(od_ec_enc *enc, uint32_t *nbytes) {
277 unsigned char *out;
278 uint32_t storage;
279 uint16_t *buf;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800280 uint32_t offs;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500281 od_ec_window m;
282 od_ec_window e;
283 od_ec_window l;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500284 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;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500301 c = enc->cnt;
Frank Bossenebcf3e52018-03-27 12:19:20 -0400302 s = 10;
303 m = 0x3FFF;
Frank Bossendf46b222018-03-27 14:53:51 -0400304 e = ((l + m) & ~m) | (m + 1);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500305 s += c;
306 offs = enc->offs;
307 buf = enc->precarry_buf;
308 if (s > 0) {
309 unsigned n;
310 storage = enc->precarry_storage;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800311 if (offs + ((s + 7) >> 3) > storage) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500312 storage = storage * 2 + ((s + 7) >> 3);
313 buf = (uint16_t *)realloc(buf, sizeof(*buf) * storage);
314 if (buf == NULL) {
315 enc->error = -1;
316 return NULL;
317 }
318 enc->precarry_buf = buf;
319 enc->precarry_storage = storage;
320 }
321 n = (1 << (c + 16)) - 1;
322 do {
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100323 assert(offs < storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500324 buf[offs++] = (uint16_t)(e >> (c + 16));
325 e &= n;
326 s -= 8;
327 c -= 8;
328 n >>= 8;
329 } while (s > 0);
330 }
Wan-Teh Chang33bc54b2018-05-08 14:32:13 -0700331 /*Make sure there's enough room for the entropy-coded bits.*/
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500332 out = enc->buf;
333 storage = enc->storage;
Wan-Teh Chang33bc54b2018-05-08 14:32:13 -0700334 c = OD_MAXI((s + 7) >> 3, 0);
335 if (offs + c > storage) {
336 storage = offs + c;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500337 out = (unsigned char *)realloc(out, sizeof(*out) * storage);
338 if (out == NULL) {
339 enc->error = -1;
340 return NULL;
341 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500342 enc->buf = out;
343 enc->storage = storage;
344 }
Wan-Teh Chang33bc54b2018-05-08 14:32:13 -0700345 *nbytes = offs;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500346 /*Perform carry propagation.*/
Wan-Teh Chang33bc54b2018-05-08 14:32:13 -0700347 assert(offs <= storage);
348 out = out + storage - offs;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500349 c = 0;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800350 while (offs > 0) {
351 offs--;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500352 c = buf[offs] + c;
353 out[offs] = (unsigned char)c;
354 c >>= 8;
355 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500356 /*Note: Unless there's an allocation error, if you keep encoding into the
357 current buffer and call this function again later, everything will work
358 just fine (you won't get a new packet out, but you will get a single
359 buffer with the new data appended to the old).
360 However, this function is O(N) where N is the amount of data coded so far,
361 so calling it more than once for a given packet is a bad idea.*/
362 return out;
363}
364
365/*Returns the number of bits "used" by the encoded symbols so far.
366 This same number can be computed in either the encoder or the decoder, and is
367 suitable for making coding decisions.
368 Warning: The value returned by this function can decrease compared to an
369 earlier call, even after encoding more data, if there is an encoding error
370 (i.e., a failure to allocate enough space for the output buffer).
371 Return: The number of bits.
372 This will always be slightly larger than the exact value (e.g., all
373 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400374int od_ec_enc_tell(const od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500375 /*The 10 here counteracts the offset of -9 baked into cnt, and adds 1 extra
376 bit, which we reserve for terminating the stream.*/
Wan-Teh Chang33bc54b2018-05-08 14:32:13 -0700377 return (enc->cnt + 10) + enc->offs * 8;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500378}
379
380/*Returns the number of bits "used" by the encoded symbols so far.
381 This same number can be computed in either the encoder or the decoder, and is
382 suitable for making coding decisions.
383 Warning: The value returned by this function can decrease compared to an
384 earlier call, even after encoding more data, if there is an encoding error
385 (i.e., a failure to allocate enough space for the output buffer).
386 Return: The number of bits scaled by 2**OD_BITRES.
387 This will always be slightly larger than the exact value (e.g., all
388 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400389uint32_t od_ec_enc_tell_frac(const od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500390 return od_ec_tell_frac(od_ec_enc_tell(enc), enc->rng);
391}
392
393/*Saves a entropy coder checkpoint to dst.
394 This allows an encoder to reverse a series of entropy coder
395 decisions if it decides that the information would have been
396 better coded some other way.*/
397void od_ec_enc_checkpoint(od_ec_enc *dst, const od_ec_enc *src) {
398 OD_COPY(dst, src, 1);
399}
400
401/*Restores an entropy coder checkpoint saved by od_ec_enc_checkpoint.
402 This can only be used to restore from checkpoints earlier in the target
403 state's history: you can not switch backwards and forwards or otherwise
404 switch to a state which isn't a casual ancestor of the current state.
405 Restore is also incompatible with patching the initial bits, as the
406 changes will remain in the restored version.*/
407void od_ec_enc_rollback(od_ec_enc *dst, const od_ec_enc *src) {
408 unsigned char *buf;
409 uint32_t storage;
410 uint16_t *precarry_buf;
411 uint32_t precarry_storage;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100412 assert(dst->storage >= src->storage);
413 assert(dst->precarry_storage >= src->precarry_storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500414 buf = dst->buf;
415 storage = dst->storage;
416 precarry_buf = dst->precarry_buf;
417 precarry_storage = dst->precarry_storage;
418 OD_COPY(dst, src, 1);
419 dst->buf = buf;
420 dst->storage = storage;
421 dst->precarry_buf = precarry_buf;
422 dst->precarry_storage = precarry_storage;
423}