media/libopus/celt/entcode.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial