entdec.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include <stddef.h>
  28. #include "os_support.h"
  29. #include "arch.h"
  30. #include "entdec.h"
  31. #include "mfrngcod.h"
  32. /*A range decoder.
  33. This is an entropy decoder based upon \cite{Mar79}, which is itself a
  34. rediscovery of the FIFO arithmetic code introduced by \cite{Pas76}.
  35. It is very similar to arithmetic encoding, except that encoding is done with
  36. digits in any base, instead of with bits, and so it is faster when using
  37. larger bases (i.e.: a byte).
  38. The author claims an average waste of $\frac{1}{2}\log_b(2b)$ bits, where $b$
  39. is the base, longer than the theoretical optimum, but to my knowledge there
  40. is no published justification for this claim.
  41. This only seems true when using near-infinite precision arithmetic so that
  42. the process is carried out with no rounding errors.
  43. An excellent description of implementation details is available at
  44. http://www.arturocampos.com/ac_range.html
  45. A recent work \cite{MNW98} which proposes several changes to arithmetic
  46. encoding for efficiency actually re-discovers many of the principles
  47. behind range encoding, and presents a good theoretical analysis of them.
  48. End of stream is handled by writing out the smallest number of bits that
  49. ensures that the stream will be correctly decoded regardless of the value of
  50. any subsequent bits.
  51. ec_tell() can be used to determine how many bits were needed to decode
  52. all the symbols thus far; other data can be packed in the remaining bits of
  53. the input buffer.
  54. @PHDTHESIS{Pas76,
  55. author="Richard Clark Pasco",
  56. title="Source coding algorithms for fast data compression",
  57. school="Dept. of Electrical Engineering, Stanford University",
  58. address="Stanford, CA",
  59. month=May,
  60. year=1976
  61. }
  62. @INPROCEEDINGS{Mar79,
  63. author="Martin, G.N.N.",
  64. title="Range encoding: an algorithm for removing redundancy from a digitised
  65. message",
  66. booktitle="Video & Data Recording Conference",
  67. year=1979,
  68. address="Southampton",
  69. month=Jul
  70. }
  71. @ARTICLE{MNW98,
  72. author="Alistair Moffat and Radford Neal and Ian H. Witten",
  73. title="Arithmetic Coding Revisited",
  74. journal="{ACM} Transactions on Information Systems",
  75. year=1998,
  76. volume=16,
  77. number=3,
  78. pages="256--294",
  79. month=Jul,
  80. URL="http://www.stanford.edu/class/ee398a/handouts/papers/Moffat98ArithmCoding.pdf"
  81. }*/
  82. static int ec_read_byte(ec_dec *_this){
  83. return _this->offs<_this->storage?_this->buf[_this->offs++]:0;
  84. }
  85. static int ec_read_byte_from_end(ec_dec *_this){
  86. return _this->end_offs<_this->storage?
  87. _this->buf[_this->storage-++(_this->end_offs)]:0;
  88. }
  89. /*Normalizes the contents of val and rng so that rng lies entirely in the
  90. high-order symbol.*/
  91. static void ec_dec_normalize(ec_dec *_this){
  92. /*If the range is too small, rescale it and input some bits.*/
  93. while(_this->rng<=EC_CODE_BOT){
  94. int sym;
  95. _this->nbits_total+=EC_SYM_BITS;
  96. _this->rng<<=EC_SYM_BITS;
  97. /*Use up the remaining bits from our last symbol.*/
  98. sym=_this->rem;
  99. /*Read the next value from the input.*/
  100. _this->rem=ec_read_byte(_this);
  101. /*Take the rest of the bits we need from this new symbol.*/
  102. sym=(sym<<EC_SYM_BITS|_this->rem)>>(EC_SYM_BITS-EC_CODE_EXTRA);
  103. /*And subtract them from val, capped to be less than EC_CODE_TOP.*/
  104. _this->val=((_this->val<<EC_SYM_BITS)+(EC_SYM_MAX&~sym))&(EC_CODE_TOP-1);
  105. }
  106. }
  107. void ec_dec_init(ec_dec *_this,unsigned char *_buf,opus_uint32 _storage){
  108. _this->buf=_buf;
  109. _this->storage=_storage;
  110. _this->end_offs=0;
  111. _this->end_window=0;
  112. _this->nend_bits=0;
  113. /*This is the offset from which ec_tell() will subtract partial bits.
  114. The final value after the ec_dec_normalize() call will be the same as in
  115. the encoder, but we have to compensate for the bits that are added there.*/
  116. _this->nbits_total=EC_CODE_BITS+1
  117. -((EC_CODE_BITS-EC_CODE_EXTRA)/EC_SYM_BITS)*EC_SYM_BITS;
  118. _this->offs=0;
  119. _this->rng=1U<<EC_CODE_EXTRA;
  120. _this->rem=ec_read_byte(_this);
  121. _this->val=_this->rng-1-(_this->rem>>(EC_SYM_BITS-EC_CODE_EXTRA));
  122. _this->error=0;
  123. /*Normalize the interval.*/
  124. ec_dec_normalize(_this);
  125. }
  126. unsigned ec_decode(ec_dec *_this,unsigned _ft){
  127. unsigned s;
  128. _this->ext=celt_udiv(_this->rng,_ft);
  129. s=(unsigned)(_this->val/_this->ext);
  130. return _ft-EC_MINI(s+1,_ft);
  131. }
  132. unsigned ec_decode_bin(ec_dec *_this,unsigned _bits){
  133. unsigned s;
  134. _this->ext=_this->rng>>_bits;
  135. s=(unsigned)(_this->val/_this->ext);
  136. return (1U<<_bits)-EC_MINI(s+1U,1U<<_bits);
  137. }
  138. void ec_dec_update(ec_dec *_this,unsigned _fl,unsigned _fh,unsigned _ft){
  139. opus_uint32 s;
  140. s=IMUL32(_this->ext,_ft-_fh);
  141. _this->val-=s;
  142. _this->rng=_fl>0?IMUL32(_this->ext,_fh-_fl):_this->rng-s;
  143. ec_dec_normalize(_this);
  144. }
  145. /*The probability of having a "one" is 1/(1<<_logp).*/
  146. int ec_dec_bit_logp(ec_dec *_this,unsigned _logp){
  147. opus_uint32 r;
  148. opus_uint32 d;
  149. opus_uint32 s;
  150. int ret;
  151. r=_this->rng;
  152. d=_this->val;
  153. s=r>>_logp;
  154. ret=d<s;
  155. if(!ret)_this->val=d-s;
  156. _this->rng=ret?s:r-s;
  157. ec_dec_normalize(_this);
  158. return ret;
  159. }
  160. int ec_dec_icdf(ec_dec *_this,const unsigned char *_icdf,unsigned _ftb){
  161. opus_uint32 r;
  162. opus_uint32 d;
  163. opus_uint32 s;
  164. opus_uint32 t;
  165. int ret;
  166. s=_this->rng;
  167. d=_this->val;
  168. r=s>>_ftb;
  169. ret=-1;
  170. do{
  171. t=s;
  172. s=IMUL32(r,_icdf[++ret]);
  173. }
  174. while(d<s);
  175. _this->val=d-s;
  176. _this->rng=t-s;
  177. ec_dec_normalize(_this);
  178. return ret;
  179. }
  180. opus_uint32 ec_dec_uint(ec_dec *_this,opus_uint32 _ft){
  181. unsigned ft;
  182. unsigned s;
  183. int ftb;
  184. /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/
  185. celt_assert(_ft>1);
  186. _ft--;
  187. ftb=EC_ILOG(_ft);
  188. if(ftb>EC_UINT_BITS){
  189. opus_uint32 t;
  190. ftb-=EC_UINT_BITS;
  191. ft=(unsigned)(_ft>>ftb)+1;
  192. s=ec_decode(_this,ft);
  193. ec_dec_update(_this,s,s+1,ft);
  194. t=(opus_uint32)s<<ftb|ec_dec_bits(_this,ftb);
  195. if(t<=_ft)return t;
  196. _this->error=1;
  197. return _ft;
  198. }
  199. else{
  200. _ft++;
  201. s=ec_decode(_this,(unsigned)_ft);
  202. ec_dec_update(_this,s,s+1,(unsigned)_ft);
  203. return s;
  204. }
  205. }
  206. opus_uint32 ec_dec_bits(ec_dec *_this,unsigned _bits){
  207. ec_window window;
  208. int available;
  209. opus_uint32 ret;
  210. window=_this->end_window;
  211. available=_this->nend_bits;
  212. if((unsigned)available<_bits){
  213. do{
  214. window|=(ec_window)ec_read_byte_from_end(_this)<<available;
  215. available+=EC_SYM_BITS;
  216. }
  217. while(available<=EC_WINDOW_SIZE-EC_SYM_BITS);
  218. }
  219. ret=(opus_uint32)window&(((opus_uint32)1<<_bits)-1U);
  220. window>>=_bits;
  221. available-=_bits;
  222. _this->end_window=window;
  223. _this->nend_bits=available;
  224. _this->nbits_total+=_bits;
  225. return ret;
  226. }