entcode.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (c) 2001-2011 Timothy B. Terriberry
  2. Copyright (c) 2008-2009 Xiph.Org Foundation */
  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 COPYRIGHT OWNER
  16. OR 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. #include "opus_types.h"
  25. #include "opus_defines.h"
  26. #if !defined(_entcode_H)
  27. # define _entcode_H (1)
  28. # include <limits.h>
  29. # include <stddef.h>
  30. # include "ecintrin.h"
  31. extern const opus_uint32 SMALL_DIV_TABLE[129];
  32. #ifdef OPUS_ARM_ASM
  33. #define USE_SMALL_DIV_TABLE
  34. #endif
  35. /*OPT: ec_window must be at least 32 bits, but if you have fast arithmetic on a
  36. larger type, you can speed up the decoder by using it here.*/
  37. typedef opus_uint32 ec_window;
  38. typedef struct ec_ctx ec_ctx;
  39. typedef struct ec_ctx ec_enc;
  40. typedef struct ec_ctx ec_dec;
  41. # define EC_WINDOW_SIZE ((int)sizeof(ec_window)*CHAR_BIT)
  42. /*The number of bits to use for the range-coded part of unsigned integers.*/
  43. # define EC_UINT_BITS (8)
  44. /*The resolution of fractional-precision bit usage measurements, i.e.,
  45. 3 => 1/8th bits.*/
  46. # define BITRES 3
  47. /*The entropy encoder/decoder context.
  48. We use the same structure for both, so that common functions like ec_tell()
  49. can be used on either one.*/
  50. struct ec_ctx{
  51. /*Buffered input/output.*/
  52. unsigned char *buf;
  53. /*The size of the buffer.*/
  54. opus_uint32 storage;
  55. /*The offset at which the last byte containing raw bits was read/written.*/
  56. opus_uint32 end_offs;
  57. /*Bits that will be read from/written at the end.*/
  58. ec_window end_window;
  59. /*Number of valid bits in end_window.*/
  60. int nend_bits;
  61. /*The total number of whole bits read/written.
  62. This does not include partial bits currently in the range coder.*/
  63. int nbits_total;
  64. /*The offset at which the next range coder byte will be read/written.*/
  65. opus_uint32 offs;
  66. /*The number of values in the current range.*/
  67. opus_uint32 rng;
  68. /*In the decoder: the difference between the top of the current range and
  69. the input value, minus one.
  70. In the encoder: the low end of the current range.*/
  71. opus_uint32 val;
  72. /*In the decoder: the saved normalization factor from ec_decode().
  73. In the encoder: the number of oustanding carry propagating symbols.*/
  74. opus_uint32 ext;
  75. /*A buffered input/output symbol, awaiting carry propagation.*/
  76. int rem;
  77. /*Nonzero if an error occurred.*/
  78. int error;
  79. };
  80. static OPUS_INLINE opus_uint32 ec_range_bytes(ec_ctx *_this){
  81. return _this->offs;
  82. }
  83. static OPUS_INLINE unsigned char *ec_get_buffer(ec_ctx *_this){
  84. return _this->buf;
  85. }
  86. static OPUS_INLINE int ec_get_error(ec_ctx *_this){
  87. return _this->error;
  88. }
  89. /*Returns the number of bits "used" by the encoded or decoded symbols so far.
  90. This same number can be computed in either the encoder or the decoder, and is
  91. suitable for making coding decisions.
  92. Return: The number of bits.
  93. This will always be slightly larger than the exact value (e.g., all
  94. rounding error is in the positive direction).*/
  95. static OPUS_INLINE int ec_tell(ec_ctx *_this){
  96. return _this->nbits_total-EC_ILOG(_this->rng);
  97. }
  98. /*Returns the number of bits "used" by the encoded or decoded symbols so far.
  99. This same number can be computed in either the encoder or the decoder, and is
  100. suitable for making coding decisions.
  101. Return: The number of bits scaled by 2**BITRES.
  102. This will always be slightly larger than the exact value (e.g., all
  103. rounding error is in the positive direction).*/
  104. opus_uint32 ec_tell_frac(ec_ctx *_this);
  105. /* Tested exhaustively for all n and for 1<=d<=256 */
  106. static OPUS_INLINE opus_uint32 celt_udiv(opus_uint32 n, opus_uint32 d) {
  107. celt_sig_assert(d>0);
  108. #ifdef USE_SMALL_DIV_TABLE
  109. if (d>256)
  110. return n/d;
  111. else {
  112. opus_uint32 t, q;
  113. t = EC_ILOG(d&-d);
  114. q = (opus_uint64)SMALL_DIV_TABLE[d>>t]*(n>>(t-1))>>32;
  115. return q+(n-q*d >= d);
  116. }
  117. #else
  118. return n/d;
  119. #endif
  120. }
  121. static OPUS_INLINE opus_int32 celt_sudiv(opus_int32 n, opus_int32 d) {
  122. celt_sig_assert(d>0);
  123. #ifdef USE_SMALL_DIV_TABLE
  124. if (n<0)
  125. return -(opus_int32)celt_udiv(-n, d);
  126. else
  127. return celt_udiv(n, d);
  128. #else
  129. return n/d;
  130. #endif
  131. }
  132. #endif