entenc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #if defined(HAVE_CONFIG_H)
  25. # include "config.h"
  26. #endif
  27. #include "os_support.h"
  28. #include "arch.h"
  29. #include "entenc.h"
  30. #include "mfrngcod.h"
  31. /*A range encoder.
  32. See entdec.c and the references for implementation details \cite{Mar79,MNW98}.
  33. @INPROCEEDINGS{Mar79,
  34. author="Martin, G.N.N.",
  35. title="Range encoding: an algorithm for removing redundancy from a digitised
  36. message",
  37. booktitle="Video \& Data Recording Conference",
  38. year=1979,
  39. address="Southampton",
  40. month=Jul
  41. }
  42. @ARTICLE{MNW98,
  43. author="Alistair Moffat and Radford Neal and Ian H. Witten",
  44. title="Arithmetic Coding Revisited",
  45. journal="{ACM} Transactions on Information Systems",
  46. year=1998,
  47. volume=16,
  48. number=3,
  49. pages="256--294",
  50. month=Jul,
  51. URL="http://www.stanford.edu/class/ee398/handouts/papers/Moffat98ArithmCoding.pdf"
  52. }*/
  53. static int ec_write_byte(ec_enc *_this,unsigned _value){
  54. if(_this->offs+_this->end_offs>=_this->storage)return -1;
  55. _this->buf[_this->offs++]=(unsigned char)_value;
  56. return 0;
  57. }
  58. static int ec_write_byte_at_end(ec_enc *_this,unsigned _value){
  59. if(_this->offs+_this->end_offs>=_this->storage)return -1;
  60. _this->buf[_this->storage-++(_this->end_offs)]=(unsigned char)_value;
  61. return 0;
  62. }
  63. /*Outputs a symbol, with a carry bit.
  64. If there is a potential to propagate a carry over several symbols, they are
  65. buffered until it can be determined whether or not an actual carry will
  66. occur.
  67. If the counter for the buffered symbols overflows, then the stream becomes
  68. undecodable.
  69. This gives a theoretical limit of a few billion symbols in a single packet on
  70. 32-bit systems.
  71. The alternative is to truncate the range in order to force a carry, but
  72. requires similar carry tracking in the decoder, needlessly slowing it down.*/
  73. static void ec_enc_carry_out(ec_enc *_this,int _c){
  74. if(_c!=EC_SYM_MAX){
  75. /*No further carry propagation possible, flush buffer.*/
  76. int carry;
  77. carry=_c>>EC_SYM_BITS;
  78. /*Don't output a byte on the first write.
  79. This compare should be taken care of by branch-prediction thereafter.*/
  80. if(_this->rem>=0)_this->error|=ec_write_byte(_this,_this->rem+carry);
  81. if(_this->ext>0){
  82. unsigned sym;
  83. sym=(EC_SYM_MAX+carry)&EC_SYM_MAX;
  84. do _this->error|=ec_write_byte(_this,sym);
  85. while(--(_this->ext)>0);
  86. }
  87. _this->rem=_c&EC_SYM_MAX;
  88. }
  89. else _this->ext++;
  90. }
  91. static OPUS_INLINE void ec_enc_normalize(ec_enc *_this){
  92. /*If the range is too small, output some bits and rescale it.*/
  93. while(_this->rng<=EC_CODE_BOT){
  94. ec_enc_carry_out(_this,(int)(_this->val>>EC_CODE_SHIFT));
  95. /*Move the next-to-high-order symbol into the high-order position.*/
  96. _this->val=(_this->val<<EC_SYM_BITS)&(EC_CODE_TOP-1);
  97. _this->rng<<=EC_SYM_BITS;
  98. _this->nbits_total+=EC_SYM_BITS;
  99. }
  100. }
  101. void ec_enc_init(ec_enc *_this,unsigned char *_buf,opus_uint32 _size){
  102. _this->buf=_buf;
  103. _this->end_offs=0;
  104. _this->end_window=0;
  105. _this->nend_bits=0;
  106. /*This is the offset from which ec_tell() will subtract partial bits.*/
  107. _this->nbits_total=EC_CODE_BITS+1;
  108. _this->offs=0;
  109. _this->rng=EC_CODE_TOP;
  110. _this->rem=-1;
  111. _this->val=0;
  112. _this->ext=0;
  113. _this->storage=_size;
  114. _this->error=0;
  115. }
  116. void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft){
  117. opus_uint32 r;
  118. r=celt_udiv(_this->rng,_ft);
  119. if(_fl>0){
  120. _this->val+=_this->rng-IMUL32(r,(_ft-_fl));
  121. _this->rng=IMUL32(r,(_fh-_fl));
  122. }
  123. else _this->rng-=IMUL32(r,(_ft-_fh));
  124. ec_enc_normalize(_this);
  125. }
  126. void ec_encode_bin(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _bits){
  127. opus_uint32 r;
  128. r=_this->rng>>_bits;
  129. if(_fl>0){
  130. _this->val+=_this->rng-IMUL32(r,((1U<<_bits)-_fl));
  131. _this->rng=IMUL32(r,(_fh-_fl));
  132. }
  133. else _this->rng-=IMUL32(r,((1U<<_bits)-_fh));
  134. ec_enc_normalize(_this);
  135. }
  136. /*The probability of having a "one" is 1/(1<<_logp).*/
  137. void ec_enc_bit_logp(ec_enc *_this,int _val,unsigned _logp){
  138. opus_uint32 r;
  139. opus_uint32 s;
  140. opus_uint32 l;
  141. r=_this->rng;
  142. l=_this->val;
  143. s=r>>_logp;
  144. r-=s;
  145. if(_val)_this->val=l+r;
  146. _this->rng=_val?s:r;
  147. ec_enc_normalize(_this);
  148. }
  149. void ec_enc_icdf(ec_enc *_this,int _s,const unsigned char *_icdf,unsigned _ftb){
  150. opus_uint32 r;
  151. r=_this->rng>>_ftb;
  152. if(_s>0){
  153. _this->val+=_this->rng-IMUL32(r,_icdf[_s-1]);
  154. _this->rng=IMUL32(r,_icdf[_s-1]-_icdf[_s]);
  155. }
  156. else _this->rng-=IMUL32(r,_icdf[_s]);
  157. ec_enc_normalize(_this);
  158. }
  159. void ec_enc_uint(ec_enc *_this,opus_uint32 _fl,opus_uint32 _ft){
  160. unsigned ft;
  161. unsigned fl;
  162. int ftb;
  163. /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/
  164. celt_assert(_ft>1);
  165. _ft--;
  166. ftb=EC_ILOG(_ft);
  167. if(ftb>EC_UINT_BITS){
  168. ftb-=EC_UINT_BITS;
  169. ft=(_ft>>ftb)+1;
  170. fl=(unsigned)(_fl>>ftb);
  171. ec_encode(_this,fl,fl+1,ft);
  172. ec_enc_bits(_this,_fl&(((opus_uint32)1<<ftb)-1U),ftb);
  173. }
  174. else ec_encode(_this,_fl,_fl+1,_ft+1);
  175. }
  176. void ec_enc_bits(ec_enc *_this,opus_uint32 _fl,unsigned _bits){
  177. ec_window window;
  178. int used;
  179. window=_this->end_window;
  180. used=_this->nend_bits;
  181. celt_assert(_bits>0);
  182. if(used+_bits>EC_WINDOW_SIZE){
  183. do{
  184. _this->error|=ec_write_byte_at_end(_this,(unsigned)window&EC_SYM_MAX);
  185. window>>=EC_SYM_BITS;
  186. used-=EC_SYM_BITS;
  187. }
  188. while(used>=EC_SYM_BITS);
  189. }
  190. window|=(ec_window)_fl<<used;
  191. used+=_bits;
  192. _this->end_window=window;
  193. _this->nend_bits=used;
  194. _this->nbits_total+=_bits;
  195. }
  196. void ec_enc_patch_initial_bits(ec_enc *_this,unsigned _val,unsigned _nbits){
  197. int shift;
  198. unsigned mask;
  199. celt_assert(_nbits<=EC_SYM_BITS);
  200. shift=EC_SYM_BITS-_nbits;
  201. mask=((1<<_nbits)-1)<<shift;
  202. if(_this->offs>0){
  203. /*The first byte has been finalized.*/
  204. _this->buf[0]=(unsigned char)((_this->buf[0]&~mask)|_val<<shift);
  205. }
  206. else if(_this->rem>=0){
  207. /*The first byte is still awaiting carry propagation.*/
  208. _this->rem=(_this->rem&~mask)|_val<<shift;
  209. }
  210. else if(_this->rng<=(EC_CODE_TOP>>_nbits)){
  211. /*The renormalization loop has never been run.*/
  212. _this->val=(_this->val&~((opus_uint32)mask<<EC_CODE_SHIFT))|
  213. (opus_uint32)_val<<(EC_CODE_SHIFT+shift);
  214. }
  215. /*The encoder hasn't even encoded _nbits of data yet.*/
  216. else _this->error=-1;
  217. }
  218. void ec_enc_shrink(ec_enc *_this,opus_uint32 _size){
  219. celt_assert(_this->offs+_this->end_offs<=_size);
  220. OPUS_MOVE(_this->buf+_size-_this->end_offs,
  221. _this->buf+_this->storage-_this->end_offs,_this->end_offs);
  222. _this->storage=_size;
  223. }
  224. void ec_enc_done(ec_enc *_this){
  225. ec_window window;
  226. int used;
  227. opus_uint32 msk;
  228. opus_uint32 end;
  229. int l;
  230. /*We output the minimum number of bits that ensures that the symbols encoded
  231. thus far will be decoded correctly regardless of the bits that follow.*/
  232. l=EC_CODE_BITS-EC_ILOG(_this->rng);
  233. msk=(EC_CODE_TOP-1)>>l;
  234. end=(_this->val+msk)&~msk;
  235. if((end|msk)>=_this->val+_this->rng){
  236. l++;
  237. msk>>=1;
  238. end=(_this->val+msk)&~msk;
  239. }
  240. while(l>0){
  241. ec_enc_carry_out(_this,(int)(end>>EC_CODE_SHIFT));
  242. end=(end<<EC_SYM_BITS)&(EC_CODE_TOP-1);
  243. l-=EC_SYM_BITS;
  244. }
  245. /*If we have a buffered byte flush it into the output buffer.*/
  246. if(_this->rem>=0||_this->ext>0)ec_enc_carry_out(_this,0);
  247. /*If we have buffered extra bits, flush them as well.*/
  248. window=_this->end_window;
  249. used=_this->nend_bits;
  250. while(used>=EC_SYM_BITS){
  251. _this->error|=ec_write_byte_at_end(_this,(unsigned)window&EC_SYM_MAX);
  252. window>>=EC_SYM_BITS;
  253. used-=EC_SYM_BITS;
  254. }
  255. /*Clear any excess space and add any remaining extra bits to the last byte.*/
  256. if(!_this->error){
  257. OPUS_CLEAR(_this->buf+_this->offs,
  258. _this->storage-_this->offs-_this->end_offs);
  259. if(used>0){
  260. /*If there's no range coder data at all, give up.*/
  261. if(_this->end_offs>=_this->storage)_this->error=-1;
  262. else{
  263. l=-l;
  264. /*If we've busted, don't add too many extra bits to the last byte; it
  265. would corrupt the range coder data, and that's more important.*/
  266. if(_this->offs+_this->end_offs>=_this->storage&&l<used){
  267. window&=(1<<l)-1;
  268. _this->error=-1;
  269. }
  270. _this->buf[_this->storage-_this->end_offs-1]|=(unsigned char)window;
  271. }
  272. }
  273. }
  274. }