1*77c1e3ccSAndroid Build Coastguard Worker /*
2*77c1e3ccSAndroid Build Coastguard Worker * Copyright (c) 2001-2016, Alliance for Open Media. All rights reserved.
3*77c1e3ccSAndroid Build Coastguard Worker *
4*77c1e3ccSAndroid Build Coastguard Worker * This source code is subject to the terms of the BSD 2 Clause License and
5*77c1e3ccSAndroid Build Coastguard Worker * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6*77c1e3ccSAndroid Build Coastguard Worker * was not distributed with this source code in the LICENSE file, you can
7*77c1e3ccSAndroid Build Coastguard Worker * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8*77c1e3ccSAndroid Build Coastguard Worker * Media Patent License 1.0 was not distributed with this source code in the
9*77c1e3ccSAndroid Build Coastguard Worker * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10*77c1e3ccSAndroid Build Coastguard Worker */
11*77c1e3ccSAndroid Build Coastguard Worker
12*77c1e3ccSAndroid Build Coastguard Worker #include "aom_dsp/entcode.h"
13*77c1e3ccSAndroid Build Coastguard Worker
14*77c1e3ccSAndroid Build Coastguard Worker /*Given the current total integer number of bits used and the current value of
15*77c1e3ccSAndroid Build Coastguard Worker rng, computes the fraction number of bits used to OD_BITRES precision.
16*77c1e3ccSAndroid Build Coastguard Worker This is used by od_ec_enc_tell_frac() and od_ec_dec_tell_frac().
17*77c1e3ccSAndroid Build Coastguard Worker nbits_total: The number of whole bits currently used, i.e., the value
18*77c1e3ccSAndroid Build Coastguard Worker returned by od_ec_enc_tell() or od_ec_dec_tell().
19*77c1e3ccSAndroid Build Coastguard Worker rng: The current value of rng from either the encoder or decoder state.
20*77c1e3ccSAndroid Build Coastguard Worker Return: The number of bits scaled by 2**OD_BITRES.
21*77c1e3ccSAndroid Build Coastguard Worker This will always be slightly larger than the exact value (e.g., all
22*77c1e3ccSAndroid Build Coastguard Worker rounding error is in the positive direction).*/
od_ec_tell_frac(uint32_t nbits_total,uint32_t rng)23*77c1e3ccSAndroid Build Coastguard Worker uint32_t od_ec_tell_frac(uint32_t nbits_total, uint32_t rng) {
24*77c1e3ccSAndroid Build Coastguard Worker uint32_t nbits;
25*77c1e3ccSAndroid Build Coastguard Worker int l;
26*77c1e3ccSAndroid Build Coastguard Worker int i;
27*77c1e3ccSAndroid Build Coastguard Worker /*To handle the non-integral number of bits still left in the encoder/decoder
28*77c1e3ccSAndroid Build Coastguard Worker state, we compute the worst-case number of bits of val that must be
29*77c1e3ccSAndroid Build Coastguard Worker encoded to ensure that the value is inside the range for any possible
30*77c1e3ccSAndroid Build Coastguard Worker subsequent bits.
31*77c1e3ccSAndroid Build Coastguard Worker The computation here is independent of val itself (the decoder does not
32*77c1e3ccSAndroid Build Coastguard Worker even track that value), even though the real number of bits used after
33*77c1e3ccSAndroid Build Coastguard Worker od_ec_enc_done() may be 1 smaller if rng is a power of two and the
34*77c1e3ccSAndroid Build Coastguard Worker corresponding trailing bits of val are all zeros.
35*77c1e3ccSAndroid Build Coastguard Worker If we did try to track that special case, then coding a value with a
36*77c1e3ccSAndroid Build Coastguard Worker probability of 1/(1 << n) might sometimes appear to use more than n bits.
37*77c1e3ccSAndroid Build Coastguard Worker This may help explain the surprising result that a newly initialized
38*77c1e3ccSAndroid Build Coastguard Worker encoder or decoder claims to have used 1 bit.*/
39*77c1e3ccSAndroid Build Coastguard Worker nbits = nbits_total << OD_BITRES;
40*77c1e3ccSAndroid Build Coastguard Worker l = 0;
41*77c1e3ccSAndroid Build Coastguard Worker for (i = OD_BITRES; i-- > 0;) {
42*77c1e3ccSAndroid Build Coastguard Worker int b;
43*77c1e3ccSAndroid Build Coastguard Worker rng = rng * rng >> 15;
44*77c1e3ccSAndroid Build Coastguard Worker b = (int)(rng >> 16);
45*77c1e3ccSAndroid Build Coastguard Worker l = l << 1 | b;
46*77c1e3ccSAndroid Build Coastguard Worker rng >>= b;
47*77c1e3ccSAndroid Build Coastguard Worker }
48*77c1e3ccSAndroid Build Coastguard Worker return nbits - l;
49*77c1e3ccSAndroid Build Coastguard Worker }
50