Fix ectest to not check a case which isn't guaranteed to work, and which we don't...
[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 #include "arch.h"
8
9
10 #define EC_BUFFER_INCREMENT (256)
11
12 void ec_byte_writeinit_buffer(ec_byte_buffer *_b, unsigned char *_buf, long _size){
13   _b->ptr=_b->buf=_buf;
14   _b->storage=_size;
15   _b->resizable = 0;
16 }
17
18 void ec_byte_writeinit(ec_byte_buffer *_b){
19   _b->ptr=_b->buf=celt_alloc(EC_BUFFER_INCREMENT*sizeof(char));
20   _b->storage=EC_BUFFER_INCREMENT;
21   _b->resizable = 1;
22 }
23
24 void ec_byte_writetrunc(ec_byte_buffer *_b,long _bytes){
25   _b->ptr=_b->buf+_bytes;
26 }
27
28 void ec_byte_write1(ec_byte_buffer *_b,unsigned _value){
29   ptrdiff_t endbyte;
30   endbyte=_b->ptr-_b->buf;
31   if(endbyte>=_b->storage){
32     if (_b->resizable){
33       _b->buf=celt_realloc(_b->buf,(_b->storage+EC_BUFFER_INCREMENT)*sizeof(char));
34       _b->storage+=EC_BUFFER_INCREMENT;
35       _b->ptr=_b->buf+endbyte;
36     } else {
37       celt_fatal("range encoder overflow\n");
38     }
39   }
40   *(_b->ptr++)=(unsigned char)_value;
41 }
42
43 void ec_byte_write4(ec_byte_buffer *_b,ec_uint32 _value){
44   ptrdiff_t endbyte;
45   endbyte=_b->ptr-_b->buf;
46   if(endbyte+4>_b->storage){
47     if (_b->resizable){
48       _b->buf=celt_realloc(_b->buf,(_b->storage+EC_BUFFER_INCREMENT)*sizeof(char));
49       _b->storage+=EC_BUFFER_INCREMENT;
50       _b->ptr=_b->buf+endbyte;
51     } else {
52       celt_fatal("range encoder overflow\n");
53     }
54   }
55   *(_b->ptr++)=(unsigned char)_value;
56   _value>>=8;
57   *(_b->ptr++)=(unsigned char)_value;
58   _value>>=8;
59   *(_b->ptr++)=(unsigned char)_value;
60   _value>>=8;
61   *(_b->ptr++)=(unsigned char)_value;
62 }
63
64 void ec_byte_writecopy(ec_byte_buffer *_b,void *_source,long _bytes){
65   ptrdiff_t endbyte;
66   endbyte=_b->ptr-_b->buf;
67   if(endbyte+_bytes>_b->storage){
68     if (_b->resizable){
69       _b->storage=endbyte+_bytes+EC_BUFFER_INCREMENT;
70       _b->buf=celt_realloc(_b->buf,_b->storage*sizeof(char));
71       _b->ptr=_b->buf+endbyte;
72     } else {
73       celt_fatal("range encoder overflow\n");
74     }
75   }
76   memmove(_b->ptr,_source,_bytes);
77   _b->ptr+=_bytes;
78 }
79
80 void ec_byte_writeclear(ec_byte_buffer *_b){
81   celt_free(_b->buf);
82 }
83
84
85
86 void ec_enc_bits(ec_enc *_this,ec_uint32 _fl,int _ftb){
87   unsigned fl;
88   unsigned ft;
89   while(_ftb>EC_UNIT_BITS){
90     _ftb-=EC_UNIT_BITS;
91     fl=(unsigned)(_fl>>_ftb)&EC_UNIT_MASK;
92     ec_encode_bin(_this,fl,fl+1,EC_UNIT_BITS);
93   }
94   ft=1<<_ftb;
95   fl=(unsigned)_fl&ft-1;
96   ec_encode_bin(_this,fl,fl+1,_ftb);
97 }
98
99 void ec_enc_uint(ec_enc *_this,ec_uint32 _fl,ec_uint32 _ft){
100   unsigned  ft;
101   unsigned  fl;
102   int       ftb;
103   /*In order to optimize EC_ILOG(), it is undefined for the value 0.*/
104   celt_assert(_ft>1);
105   _ft--;
106   ftb=EC_ILOG(_ft);
107   if(ftb>EC_UNIT_BITS){
108     ftb-=EC_UNIT_BITS;
109     ft=(_ft>>ftb)+1;
110     fl=(unsigned)(_fl>>ftb);
111     ec_encode(_this,fl,fl+1,ft);
112     ec_enc_bits(_this,_fl,ftb);
113   } else {
114     ec_encode(_this,_fl,_fl+1,_ft+1);
115   }
116 }
117