1 /* Copyright (c) 2001-2008 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 
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 
15    - Neither the name of the Xiph.org Foundation nor the names of its
16    contributors may be used to endorse or promote products derived from
17    this software without specific prior written permission.
18 
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include "os_support.h"
37 #include "entenc.h"
38 #include "arch.h"
39 
40 
41 #define EC_BUFFER_INCREMENT (256)
42 
ec_byte_writeinit_buffer(ec_byte_buffer * _b,unsigned char * _buf,long _size)43 void ec_byte_writeinit_buffer(ec_byte_buffer *_b, unsigned char *_buf, long _size){
44   _b->ptr=_b->buf=_buf;
45   _b->end_ptr=_b->buf+_size-1;
46   _b->storage=_size;
47 }
48 
ec_byte_shrink(ec_byte_buffer * _b,long _size)49 void ec_byte_shrink(ec_byte_buffer *_b, long _size){
50    _b->end_ptr=_b->buf+_size-1;
51    _b->storage=_size;
52 }
53 
ec_byte_write1(ec_byte_buffer * _b,unsigned _value)54 void ec_byte_write1(ec_byte_buffer *_b,unsigned _value){
55   ptrdiff_t endbyte;
56   endbyte=_b->ptr-_b->buf;
57   if(endbyte>=_b->storage){
58     celt_fatal("range encoder overflow\n");
59   }
60   *(_b->ptr++)=(unsigned char)_value;
61 }
62 
ec_byte_write_at_end(ec_byte_buffer * _b,unsigned _value)63 void ec_byte_write_at_end(ec_byte_buffer *_b,unsigned _value){
64   if (_b->end_ptr < _b->ptr)
65   {
66     celt_fatal("byte buffer collision");
67   }
68   *(_b->end_ptr--)=(unsigned char)_value;
69 }
70 
71 
ec_enc_bits(ec_enc * _this,ec_uint32 _fl,int _ftb)72 void ec_enc_bits(ec_enc *_this,ec_uint32 _fl,int _ftb){
73   unsigned fl;
74   unsigned ft;
75   while(_ftb>EC_UNIT_BITS){
76     _ftb-=EC_UNIT_BITS;
77     fl=(unsigned)(_fl>>_ftb)&EC_UNIT_MASK;
78     ec_encode_raw(_this,fl,fl+1,EC_UNIT_BITS);
79   }
80   ft=1<<_ftb;
81   fl=(unsigned)_fl&ft-1;
82   ec_encode_raw(_this,fl,fl+1,_ftb);
83 }
84 
ec_enc_uint(ec_enc * _this,ec_uint32 _fl,ec_uint32 _ft)85 void ec_enc_uint(ec_enc *_this,ec_uint32 _fl,ec_uint32 _ft){
86   unsigned  ft;
87   unsigned  fl;
88   int       ftb;
89   /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/
90   celt_assert(_ft>1);
91   _ft--;
92   ftb=EC_ILOG(_ft);
93   if(ftb>EC_UNIT_BITS){
94     ftb-=EC_UNIT_BITS;
95     ft=(_ft>>ftb)+1;
96     fl=(unsigned)(_fl>>ftb);
97     ec_encode(_this,fl,fl+1,ft);
98     ec_enc_bits(_this,_fl,ftb);
99   } else {
100     ec_encode(_this,_fl,_fl+1,_ft+1);
101   }
102 }
103 
104