blob: ab74ea04aee70d9c9ca599149d63286e8360fb7d [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) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500133 enc->offs = 0;
134 enc->low = 0;
135 enc->rng = 0x8000;
136 /*This is initialized to -9 so that it crosses zero after we've accumulated
137 one byte + one carry bit.*/
138 enc->cnt = -9;
139 enc->error = 0;
140#if OD_MEASURE_EC_OVERHEAD
141 enc->entropy = 0;
142 enc->nb_symbols = 0;
143#endif
144}
145
146/*Frees the buffers used by the encoder.*/
147void od_ec_enc_clear(od_ec_enc *enc) {
148 free(enc->precarry_buf);
149 free(enc->buf);
150}
151
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500152/*Encodes a symbol given its frequency in Q15.
Thomas Daede837262b2017-11-06 20:07:01 -0800153 fl: CDF_PROB_TOP minus the cumulative frequency of all symbols that come
154 before the
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700155 one to be encoded.
Thomas Daede837262b2017-11-06 20:07:01 -0800156 fh: CDF_PROB_TOP minus the cumulative frequency of all symbols up to and
157 including
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700158 the one to be encoded.*/
Thomas Davies736ddef2017-11-09 09:46:08 +0000159static void od_ec_encode_q15(od_ec_enc *enc, unsigned fl, unsigned fh, int s,
160 int nsyms) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500161 od_ec_window l;
162 unsigned r;
163 unsigned u;
164 unsigned v;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500165 l = enc->low;
166 r = enc->rng;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100167 assert(32768U <= r);
168 assert(fh <= fl);
169 assert(fl <= 32768U);
170 assert(7 - EC_PROB_SHIFT - CDF_SHIFT >= 0);
Thomas Davies736ddef2017-11-09 09:46:08 +0000171 const int N = nsyms - 1;
Thomas Daede837262b2017-11-06 20:07:01 -0800172 if (fl < CDF_PROB_TOP) {
173 u = ((r >> 8) * (uint32_t)(fl >> EC_PROB_SHIFT) >>
174 (7 - EC_PROB_SHIFT - CDF_SHIFT)) +
Thomas Davies736ddef2017-11-09 09:46:08 +0000175 EC_MIN_PROB * (N - (s - 1));
Thomas Daede837262b2017-11-06 20:07:01 -0800176 v = ((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 l += r - u;
180 r = u - v;
181 } else {
Thomas Daede837262b2017-11-06 20:07:01 -0800182 r -= ((r >> 8) * (uint32_t)(fh >> EC_PROB_SHIFT) >>
183 (7 - EC_PROB_SHIFT - CDF_SHIFT)) +
Thomas Davies736ddef2017-11-09 09:46:08 +0000184 EC_MIN_PROB * (N - (s + 0));
Timothy B. Terriberryb1c57602017-02-16 10:53:39 -0800185 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500186 od_ec_enc_normalize(enc, l, r);
187#if OD_MEASURE_EC_OVERHEAD
Thomas Daede837262b2017-11-06 20:07:01 -0800188 enc->entropy -= OD_LOG2((double)(OD_ICDF(fh) - OD_ICDF(fl)) / CDF_PROB_TOP.);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500189 enc->nb_symbols++;
190#endif
191}
192
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800193/*Encode a single binary value.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500194 val: The value to encode (0 or 1).
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700195 f: The probability that the val is one, scaled by 32768.*/
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800196void od_ec_encode_bool_q15(od_ec_enc *enc, int val, unsigned f) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500197 od_ec_window l;
198 unsigned r;
199 unsigned v;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100200 assert(0 < f);
201 assert(f < 32768U);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500202 l = enc->low;
203 r = enc->rng;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100204 assert(32768U <= r);
Thomas Davies736ddef2017-11-09 09:46:08 +0000205 v = ((r >> 8) * (uint32_t)(f >> EC_PROB_SHIFT) >> (7 - EC_PROB_SHIFT));
Jonathan Matthews9ade3942017-11-23 08:44:07 +0000206 v += EC_MIN_PROB;
Timothy B. Terriberry41b4f752017-03-07 17:45:30 -0800207 if (val) l += r - v;
208 r = val ? v : r - v;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500209 od_ec_enc_normalize(enc, l, r);
210#if OD_MEASURE_EC_OVERHEAD
Thomas Daede837262b2017-11-06 20:07:01 -0800211 enc->entropy -= OD_LOG2((double)(val ? f : (32768 - f)) / 32768.);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500212 enc->nb_symbols++;
213#endif
214}
215
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500216/*Encodes a symbol given a cumulative distribution function (CDF) table in Q15.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500217 s: The index of the symbol to encode.
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700218 icdf: 32768 minus the CDF, such that symbol s falls in the range
219 [s > 0 ? (32768 - icdf[s - 1]) : 0, 32768 - icdf[s]).
220 The values must be monotonically decreasing, and icdf[nsyms - 1] must
221 be 0.
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500222 nsyms: The number of symbols in the alphabet.
223 This should be at most 16.*/
Timothy B. Terriberryf9ef4f62017-08-25 11:24:18 -0700224void od_ec_encode_cdf_q15(od_ec_enc *enc, int s, const uint16_t *icdf,
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500225 int nsyms) {
226 (void)nsyms;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100227 assert(s >= 0);
228 assert(s < nsyms);
229 assert(icdf[nsyms - 1] == OD_ICDF(CDF_PROB_TOP));
Thomas Davies736ddef2017-11-09 09:46:08 +0000230 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 -0500231}
232
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500233/*Overwrites a few bits at the very start of an existing stream, after they
234 have already been encoded.
235 This makes it possible to have a few flags up front, where it is easy for
236 decoders to access them without parsing the whole stream, even if their
237 values are not determined until late in the encoding process, without having
238 to buffer all the intermediate symbols in the encoder.
239 In order for this to work, at least nbits bits must have already been encoded
240 using probabilities that are an exact power of two.
241 The encoder can verify the number of encoded bits is sufficient, but cannot
242 check this latter condition.
243 val: The bits to encode (in the least nbits significant bits).
244 They will be decoded in order from most-significant to least.
245 nbits: The number of bits to overwrite.
246 This must be no more than 8.*/
247void od_ec_enc_patch_initial_bits(od_ec_enc *enc, unsigned val, int nbits) {
248 int shift;
249 unsigned mask;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100250 assert(nbits >= 0);
251 assert(nbits <= 8);
252 assert(val < 1U << nbits);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500253 shift = 8 - nbits;
254 mask = ((1U << nbits) - 1) << shift;
255 if (enc->offs > 0) {
256 /*The first byte has been finalized.*/
257 enc->precarry_buf[0] =
258 (uint16_t)((enc->precarry_buf[0] & ~mask) | val << shift);
259 } else if (9 + enc->cnt + (enc->rng == 0x8000) > nbits) {
260 /*The first byte has yet to be output.*/
261 enc->low = (enc->low & ~((od_ec_window)mask << (16 + enc->cnt))) |
262 (od_ec_window)val << (16 + enc->cnt + shift);
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700263 } else {
264 /*The encoder hasn't even encoded _nbits of data yet.*/
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500265 enc->error = -1;
Yaowu Xu931bc2a2016-10-14 13:53:51 -0700266 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500267}
268
269#if OD_MEASURE_EC_OVERHEAD
270#include <stdio.h>
271#endif
272
273/*Indicates that there are no more symbols to encode.
274 All remaining output bytes are flushed to the output buffer.
275 od_ec_enc_reset() should be called before using the encoder again.
276 bytes: Returns the size of the encoded data in the returned buffer.
277 Return: A pointer to the start of the final buffer, or NULL if there was an
278 encoding error.*/
279unsigned char *od_ec_enc_done(od_ec_enc *enc, uint32_t *nbytes) {
280 unsigned char *out;
281 uint32_t storage;
282 uint16_t *buf;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800283 uint32_t offs;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500284 od_ec_window m;
285 od_ec_window e;
286 od_ec_window l;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500287 int c;
288 int s;
289 if (enc->error) return NULL;
290#if OD_MEASURE_EC_OVERHEAD
291 {
292 uint32_t tell;
293 /* Don't count the 1 bit we lose to raw bits as overhead. */
294 tell = od_ec_enc_tell(enc) - 1;
295 fprintf(stderr, "overhead: %f%%\n",
296 100 * (tell - enc->entropy) / enc->entropy);
297 fprintf(stderr, "efficiency: %f bits/symbol\n",
298 (double)tell / enc->nb_symbols);
299 }
300#endif
301 /*We output the minimum number of bits that ensures that the symbols encoded
302 thus far will be decoded correctly regardless of the bits that follow.*/
303 l = enc->low;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500304 c = enc->cnt;
Frank Bossenebcf3e52018-03-27 12:19:20 -0400305 s = 10;
306 m = 0x3FFF;
Frank Bossendf46b222018-03-27 14:53:51 -0400307 e = ((l + m) & ~m) | (m + 1);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500308 s += c;
309 offs = enc->offs;
310 buf = enc->precarry_buf;
311 if (s > 0) {
312 unsigned n;
313 storage = enc->precarry_storage;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800314 if (offs + ((s + 7) >> 3) > storage) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500315 storage = storage * 2 + ((s + 7) >> 3);
316 buf = (uint16_t *)realloc(buf, sizeof(*buf) * storage);
317 if (buf == NULL) {
318 enc->error = -1;
319 return NULL;
320 }
321 enc->precarry_buf = buf;
322 enc->precarry_storage = storage;
323 }
324 n = (1 << (c + 16)) - 1;
325 do {
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100326 assert(offs < storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500327 buf[offs++] = (uint16_t)(e >> (c + 16));
328 e &= n;
329 s -= 8;
330 c -= 8;
331 n >>= 8;
332 } while (s > 0);
333 }
Wan-Teh Chang33bc54b2018-05-08 14:32:13 -0700334 /*Make sure there's enough room for the entropy-coded bits.*/
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500335 out = enc->buf;
336 storage = enc->storage;
Wan-Teh Chang33bc54b2018-05-08 14:32:13 -0700337 c = OD_MAXI((s + 7) >> 3, 0);
338 if (offs + c > storage) {
339 storage = offs + c;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500340 out = (unsigned char *)realloc(out, sizeof(*out) * storage);
341 if (out == NULL) {
342 enc->error = -1;
343 return NULL;
344 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500345 enc->buf = out;
346 enc->storage = storage;
347 }
Wan-Teh Chang33bc54b2018-05-08 14:32:13 -0700348 *nbytes = offs;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500349 /*Perform carry propagation.*/
Wan-Teh Chang33bc54b2018-05-08 14:32:13 -0700350 assert(offs <= storage);
351 out = out + storage - offs;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500352 c = 0;
Yaowu Xu88cbc582016-11-16 15:18:13 -0800353 while (offs > 0) {
354 offs--;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500355 c = buf[offs] + c;
356 out[offs] = (unsigned char)c;
357 c >>= 8;
358 }
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500359 /*Note: Unless there's an allocation error, if you keep encoding into the
360 current buffer and call this function again later, everything will work
361 just fine (you won't get a new packet out, but you will get a single
362 buffer with the new data appended to the old).
363 However, this function is O(N) where N is the amount of data coded so far,
364 so calling it more than once for a given packet is a bad idea.*/
365 return out;
366}
367
368/*Returns the number of bits "used" by the encoded symbols so far.
369 This same number can be computed in either the encoder or the decoder, and is
370 suitable for making coding decisions.
371 Warning: The value returned by this function can decrease compared to an
372 earlier call, even after encoding more data, if there is an encoding error
373 (i.e., a failure to allocate enough space for the output buffer).
374 Return: The number of bits.
375 This will always be slightly larger than the exact value (e.g., all
376 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400377int od_ec_enc_tell(const od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500378 /*The 10 here counteracts the offset of -9 baked into cnt, and adds 1 extra
379 bit, which we reserve for terminating the stream.*/
Wan-Teh Chang33bc54b2018-05-08 14:32:13 -0700380 return (enc->cnt + 10) + enc->offs * 8;
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500381}
382
383/*Returns the number of bits "used" by the encoded symbols so far.
384 This same number can be computed in either the encoder or the decoder, and is
385 suitable for making coding decisions.
386 Warning: The value returned by this function can decrease compared to an
387 earlier call, even after encoding more data, if there is an encoding error
388 (i.e., a failure to allocate enough space for the output buffer).
389 Return: The number of bits scaled by 2**OD_BITRES.
390 This will always be slightly larger than the exact value (e.g., all
391 rounding error is in the positive direction).*/
Nathan E. Egge19698a72016-08-18 02:34:53 -0400392uint32_t od_ec_enc_tell_frac(const od_ec_enc *enc) {
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500393 return od_ec_tell_frac(od_ec_enc_tell(enc), enc->rng);
394}
395
396/*Saves a entropy coder checkpoint to dst.
397 This allows an encoder to reverse a series of entropy coder
398 decisions if it decides that the information would have been
399 better coded some other way.*/
400void od_ec_enc_checkpoint(od_ec_enc *dst, const od_ec_enc *src) {
401 OD_COPY(dst, src, 1);
402}
403
404/*Restores an entropy coder checkpoint saved by od_ec_enc_checkpoint.
405 This can only be used to restore from checkpoints earlier in the target
406 state's history: you can not switch backwards and forwards or otherwise
407 switch to a state which isn't a casual ancestor of the current state.
408 Restore is also incompatible with patching the initial bits, as the
409 changes will remain in the restored version.*/
410void od_ec_enc_rollback(od_ec_enc *dst, const od_ec_enc *src) {
411 unsigned char *buf;
412 uint32_t storage;
413 uint16_t *precarry_buf;
414 uint32_t precarry_storage;
Sebastien Alaiwan0d52f122018-03-06 09:54:23 +0100415 assert(dst->storage >= src->storage);
416 assert(dst->precarry_storage >= src->precarry_storage);
Nathan E. Egge1078dee2016-03-06 10:59:29 -0500417 buf = dst->buf;
418 storage = dst->storage;
419 precarry_buf = dst->precarry_buf;
420 precarry_storage = dst->precarry_storage;
421 OD_COPY(dst, src, 1);
422 dst->buf = buf;
423 dst->storage = storage;
424 dst->precarry_buf = precarry_buf;
425 dst->precarry_storage = precarry_storage;
426}