Removing the 64-bit part of the range coder.
[opus.git] / libcelt / entenc.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include "os_support.h"
6 #include "entenc.h"
7
8
9
10 #define EC_BUFFER_INCREMENT (256)
11
12 void ec_byte_writeinit(ec_byte_buffer *_b){
13   _b->ptr=_b->buf=celt_alloc(EC_BUFFER_INCREMENT*sizeof(char));
14   _b->storage=EC_BUFFER_INCREMENT;
15 }
16
17 void ec_byte_writetrunc(ec_byte_buffer *_b,long _bytes){
18   _b->ptr=_b->buf+_bytes;
19 }
20
21 void ec_byte_write1(ec_byte_buffer *_b,unsigned _value){
22   ptrdiff_t endbyte;
23   endbyte=_b->ptr-_b->buf;
24   if(endbyte>=_b->storage){
25     _b->buf=celt_realloc(_b->buf,(_b->storage+EC_BUFFER_INCREMENT)*sizeof(char));
26     _b->storage+=EC_BUFFER_INCREMENT;
27     _b->ptr=_b->buf+endbyte;
28   }
29   *(_b->ptr++)=(unsigned char)_value;
30 }
31
32 void ec_byte_write4(ec_byte_buffer *_b,ec_uint32 _value){
33   ptrdiff_t endbyte;
34   endbyte=_b->ptr-_b->buf;
35   if(endbyte+4>_b->storage){
36     _b->buf=celt_realloc(_b->buf,(_b->storage+EC_BUFFER_INCREMENT)*sizeof(char));
37     _b->storage+=EC_BUFFER_INCREMENT;
38     _b->ptr=_b->buf+endbyte;
39   }
40   *(_b->ptr++)=(unsigned char)_value;
41   _value>>=8;
42   *(_b->ptr++)=(unsigned char)_value;
43   _value>>=8;
44   *(_b->ptr++)=(unsigned char)_value;
45   _value>>=8;
46   *(_b->ptr++)=(unsigned char)_value;
47 }
48
49 void ec_byte_writecopy(ec_byte_buffer *_b,void *_source,long _bytes){
50   ptrdiff_t endbyte;
51   endbyte=_b->ptr-_b->buf;
52   if(endbyte+_bytes>_b->storage){
53     _b->storage=endbyte+_bytes+EC_BUFFER_INCREMENT;
54     _b->buf=celt_realloc(_b->buf,_b->storage*sizeof(char));
55     _b->ptr=_b->buf+endbyte;
56   }
57   memmove(_b->ptr,_source,_bytes);
58   _b->ptr+=_bytes;
59 }
60
61 void ec_byte_writeclear(ec_byte_buffer *_b){
62   celt_free(_b->buf);
63 }
64
65
66
67 void ec_enc_bits(ec_enc *_this,ec_uint32 _fl,int _ftb){
68   unsigned fl;
69   unsigned ft;
70   while(_ftb>EC_UNIT_BITS){
71     _ftb-=EC_UNIT_BITS;
72     fl=(unsigned)(_fl>>_ftb)&EC_UNIT_MASK;
73     ec_encode_bin(_this,fl,fl+1,EC_UNIT_BITS);
74   }
75   ft=1<<_ftb;
76   fl=(unsigned)_fl&ft-1;
77   ec_encode_bin(_this,fl,fl+1,_ftb);
78 }
79
80 void ec_enc_uint(ec_enc *_this,ec_uint32 _fl,ec_uint32 _ft){
81   unsigned  ft;
82   unsigned  fl;
83   int       ftb;
84   _ft--;
85   ftb=EC_ILOG(_ft)&-!!_ft;
86   if(ftb>EC_UNIT_BITS){
87     ftb-=EC_UNIT_BITS;
88     ft=(_ft>>ftb)+1;
89     fl=(unsigned)(_fl>>ftb);
90     ec_encode(_this,fl,fl+1,ft);
91     ec_enc_bits(_this,_fl,ftb);
92   } else {
93     ec_encode(_this,_fl,_fl+1,_ft+1);
94   }
95 }
96