entcode.c 3.0 KB

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