media/libopus/celt/entcode.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libopus/celt/entcode.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,93 @@
     1.4 +/* Copyright (c) 2001-2011 Timothy B. Terriberry
     1.5 +*/
     1.6 +/*
     1.7 +   Redistribution and use in source and binary forms, with or without
     1.8 +   modification, are permitted provided that the following conditions
     1.9 +   are met:
    1.10 +
    1.11 +   - Redistributions of source code must retain the above copyright
    1.12 +   notice, this list of conditions and the following disclaimer.
    1.13 +
    1.14 +   - Redistributions in binary form must reproduce the above copyright
    1.15 +   notice, this list of conditions and the following disclaimer in the
    1.16 +   documentation and/or other materials provided with the distribution.
    1.17 +
    1.18 +   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    1.19 +   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    1.20 +   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.21 +   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    1.22 +   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    1.23 +   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    1.24 +   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    1.25 +   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    1.26 +   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    1.27 +   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    1.28 +   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.29 +*/
    1.30 +
    1.31 +#ifdef HAVE_CONFIG_H
    1.32 +#include "config.h"
    1.33 +#endif
    1.34 +
    1.35 +#include "entcode.h"
    1.36 +#include "arch.h"
    1.37 +
    1.38 +#if !defined(EC_CLZ)
    1.39 +/*This is a fallback for systems where we don't know how to access
    1.40 +   a BSR or CLZ instruction (see ecintrin.h).
    1.41 +  If you are optimizing Opus on a new platform and it has a native CLZ or
    1.42 +   BZR (e.g. cell, MIPS, x86, etc) then making it available to Opus will be
    1.43 +   an easy performance win.*/
    1.44 +int ec_ilog(opus_uint32 _v){
    1.45 +  /*On a Pentium M, this branchless version tested as the fastest on
    1.46 +     1,000,000,000 random 32-bit integers, edging out a similar version with
    1.47 +     branches, and a 256-entry LUT version.*/
    1.48 +  int ret;
    1.49 +  int m;
    1.50 +  ret=!!_v;
    1.51 +  m=!!(_v&0xFFFF0000)<<4;
    1.52 +  _v>>=m;
    1.53 +  ret|=m;
    1.54 +  m=!!(_v&0xFF00)<<3;
    1.55 +  _v>>=m;
    1.56 +  ret|=m;
    1.57 +  m=!!(_v&0xF0)<<2;
    1.58 +  _v>>=m;
    1.59 +  ret|=m;
    1.60 +  m=!!(_v&0xC)<<1;
    1.61 +  _v>>=m;
    1.62 +  ret|=m;
    1.63 +  ret+=!!(_v&0x2);
    1.64 +  return ret;
    1.65 +}
    1.66 +#endif
    1.67 +
    1.68 +opus_uint32 ec_tell_frac(ec_ctx *_this){
    1.69 +  opus_uint32 nbits;
    1.70 +  opus_uint32 r;
    1.71 +  int         l;
    1.72 +  int         i;
    1.73 +  /*To handle the non-integral number of bits still left in the encoder/decoder
    1.74 +     state, we compute the worst-case number of bits of val that must be
    1.75 +     encoded to ensure that the value is inside the range for any possible
    1.76 +     subsequent bits.
    1.77 +    The computation here is independent of val itself (the decoder does not
    1.78 +     even track that value), even though the real number of bits used after
    1.79 +     ec_enc_done() may be 1 smaller if rng is a power of two and the
    1.80 +     corresponding trailing bits of val are all zeros.
    1.81 +    If we did try to track that special case, then coding a value with a
    1.82 +     probability of 1/(1<<n) might sometimes appear to use more than n bits.
    1.83 +    This may help explain the surprising result that a newly initialized
    1.84 +     encoder or decoder claims to have used 1 bit.*/
    1.85 +  nbits=_this->nbits_total<<BITRES;
    1.86 +  l=EC_ILOG(_this->rng);
    1.87 +  r=_this->rng>>(l-16);
    1.88 +  for(i=BITRES;i-->0;){
    1.89 +    int b;
    1.90 +    r=r*r>>15;
    1.91 +    b=(int)(r>>16);
    1.92 +    l=l<<1|b;
    1.93 +    r>>=b;
    1.94 +  }
    1.95 +  return nbits-l;
    1.96 +}

mercurial