Line data Source code
1 : /*
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 : */
11 :
12 : #ifdef HAVE_CONFIG_H
13 : #include "./config.h"
14 : #endif
15 :
16 : #include "aom_dsp/entcode.h"
17 :
18 : /*Given the current total integer number of bits used and the current value of
19 : rng, computes the fraction number of bits used to OD_BITRES precision.
20 : This is used by od_ec_enc_tell_frac() and od_ec_dec_tell_frac().
21 : nbits_total: The number of whole bits currently used, i.e., the value
22 : returned by od_ec_enc_tell() or od_ec_dec_tell().
23 : rng: The current value of rng from either the encoder or decoder state.
24 : Return: The number of bits scaled by 2**OD_BITRES.
25 : This will always be slightly larger than the exact value (e.g., all
26 : rounding error is in the positive direction).*/
27 0 : uint32_t od_ec_tell_frac(uint32_t nbits_total, uint32_t rng) {
28 : uint32_t nbits;
29 : int l;
30 : int i;
31 : /*To handle the non-integral number of bits still left in the encoder/decoder
32 : state, we compute the worst-case number of bits of val that must be
33 : encoded to ensure that the value is inside the range for any possible
34 : subsequent bits.
35 : The computation here is independent of val itself (the decoder does not
36 : even track that value), even though the real number of bits used after
37 : od_ec_enc_done() may be 1 smaller if rng is a power of two and the
38 : corresponding trailing bits of val are all zeros.
39 : If we did try to track that special case, then coding a value with a
40 : probability of 1/(1 << n) might sometimes appear to use more than n bits.
41 : This may help explain the surprising result that a newly initialized
42 : encoder or decoder claims to have used 1 bit.*/
43 0 : nbits = nbits_total << OD_BITRES;
44 0 : l = 0;
45 0 : for (i = OD_BITRES; i-- > 0;) {
46 : int b;
47 0 : rng = rng * rng >> 15;
48 0 : b = (int)(rng >> 16);
49 0 : l = l << 1 | b;
50 0 : rng >>= b;
51 : }
52 0 : return nbits - l;
53 : }
|