summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libopus/celt/entcode.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libopus/celt/entcode.c')
-rw-r--r--lib/rbcodec/codecs/libopus/celt/entcode.c88
1 files changed, 88 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libopus/celt/entcode.c b/lib/rbcodec/codecs/libopus/celt/entcode.c
new file mode 100644
index 0000000000..80e64fefaa
--- /dev/null
+++ b/lib/rbcodec/codecs/libopus/celt/entcode.c
@@ -0,0 +1,88 @@
1/* Copyright (c) 2001-2011 Timothy B. Terriberry
2*/
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifdef HAVE_CONFIG_H
29#include "opus_config.h"
30#endif
31
32#include "entcode.h"
33#include "arch.h"
34
35#if !defined(EC_CLZ)
36int ec_ilog(opus_uint32 _v){
37 /*On a Pentium M, this branchless version tested as the fastest on
38 1,000,000,000 random 32-bit integers, edging out a similar version with
39 branches, and a 256-entry LUT version.*/
40 int ret;
41 int m;
42 ret=!!_v;
43 m=!!(_v&0xFFFF0000)<<4;
44 _v>>=m;
45 ret|=m;
46 m=!!(_v&0xFF00)<<3;
47 _v>>=m;
48 ret|=m;
49 m=!!(_v&0xF0)<<2;
50 _v>>=m;
51 ret|=m;
52 m=!!(_v&0xC)<<1;
53 _v>>=m;
54 ret|=m;
55 ret+=!!(_v&0x2);
56 return ret;
57}
58#endif
59
60opus_uint32 ec_tell_frac(ec_ctx *_this){
61 opus_uint32 nbits;
62 opus_uint32 r;
63 int l;
64 int i;
65 /*To handle the non-integral number of bits still left in the encoder/decoder
66 state, we compute the worst-case number of bits of val that must be
67 encoded to ensure that the value is inside the range for any possible
68 subsequent bits.
69 The computation here is independent of val itself (the decoder does not
70 even track that value), even though the real number of bits used after
71 ec_enc_done() may be 1 smaller if rng is a power of two and the
72 corresponding trailing bits of val are all zeros.
73 If we did try to track that special case, then coding a value with a
74 probability of 1/(1<<n) might sometimes appear to use more than n bits.
75 This may help explain the surprising result that a newly initialized
76 encoder or decoder claims to have used 1 bit.*/
77 nbits=_this->nbits_total<<BITRES;
78 l=EC_ILOG(_this->rng);
79 r=_this->rng>>(l-16);
80 for(i=BITRES;i-->0;){
81 int b;
82 r=r*r>>15;
83 b=(int)(r>>16);
84 l=l<<1|b;
85 r>>=b;
86 }
87 return nbits-l;
88}