Moved the content of libentcode into libcelt to reduce dependencies,
[opus.git] / libcelt / entenc.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include "entenc.h"
4
5
6
7 #define EC_BUFFER_INCREMENT (256)
8
9 void ec_byte_writeinit(ec_byte_buffer *_b){
10   _b->ptr=_b->buf=malloc(EC_BUFFER_INCREMENT);
11   _b->storage=EC_BUFFER_INCREMENT;
12 }
13
14 void ec_byte_writetrunc(ec_byte_buffer *_b,long _bytes){
15   _b->ptr=_b->buf+_bytes;
16 }
17
18 void ec_byte_write1(ec_byte_buffer *_b,unsigned _value){
19   ptrdiff_t endbyte;
20   endbyte=_b->ptr-_b->buf;
21   if(endbyte>=_b->storage){
22     _b->buf=realloc(_b->buf,_b->storage+EC_BUFFER_INCREMENT);
23     _b->storage+=EC_BUFFER_INCREMENT;
24     _b->ptr=_b->buf+endbyte;
25   }
26   *(_b->ptr++)=(unsigned char)_value;
27 }
28
29 void ec_byte_write4(ec_byte_buffer *_b,ec_uint32 _value){
30   ptrdiff_t endbyte;
31   endbyte=_b->ptr-_b->buf;
32   if(endbyte+4>_b->storage){
33     _b->buf=realloc(_b->buf,_b->storage+EC_BUFFER_INCREMENT);
34     _b->storage+=EC_BUFFER_INCREMENT;
35     _b->ptr=_b->buf+endbyte;
36   }
37   *(_b->ptr++)=(unsigned char)_value;
38   _value>>=8;
39   *(_b->ptr++)=(unsigned char)_value;
40   _value>>=8;
41   *(_b->ptr++)=(unsigned char)_value;
42   _value>>=8;
43   *(_b->ptr++)=(unsigned char)_value;
44 }
45
46 void ec_byte_writecopy(ec_byte_buffer *_b,void *_source,long _bytes){
47   ptrdiff_t endbyte;
48   endbyte=_b->ptr-_b->buf;
49   if(endbyte+_bytes>_b->storage){
50     _b->storage=endbyte+_bytes+EC_BUFFER_INCREMENT;
51     _b->buf=realloc(_b->buf,_b->storage);
52     _b->ptr=_b->buf+endbyte;
53   }
54   memmove(_b->ptr,_source,_bytes);
55   _b->ptr+=_bytes;
56 }
57
58 void ec_byte_writeclear(ec_byte_buffer *_b){
59   free(_b->buf);
60 }
61
62
63
64 void ec_enc_bits(ec_enc *_this,ec_uint32 _fl,int _ftb){
65   unsigned fl;
66   unsigned ft;
67   while(_ftb>EC_UNIT_BITS){
68     _ftb-=EC_UNIT_BITS;
69     fl=(unsigned)(_fl>>_ftb)&EC_UNIT_MASK;
70     ec_encode(_this,fl,fl+1,EC_UNIT_MASK+1);
71   }
72   ft=1<<_ftb;
73   fl=(unsigned)_fl&ft-1;
74   ec_encode(_this,fl,fl+1,ft);
75 }
76
77 void ec_enc_bits64(ec_enc *_this,ec_uint64 _fl,int _ftb){
78   if(_ftb>32){
79     ec_enc_bits(_this,(ec_uint32)(_fl>>32),_ftb-32);
80     _ftb=32;
81     _fl&=0xFFFFFFFF;
82   }
83   ec_enc_bits(_this,(ec_uint32)_fl,_ftb);
84 }
85
86 void ec_enc_uint(ec_enc *_this,ec_uint32 _fl,ec_uint32 _ft){
87   ec_uint32 mask;
88   unsigned  ft;
89   unsigned  fl;
90   int       ftb;
91   _ft--;
92   ftb=EC_ILOG(_ft)&-!!_ft;
93   while(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     if(fl<ft-1){
99       ec_enc_bits(_this,_fl,ftb);
100       return;
101     }
102     mask=((ec_uint32)1<<ftb)-1;
103     _fl=_fl&mask;
104     _ft=_ft&mask;
105   }
106   ec_encode(_this,_fl,_fl+1,_ft+1);
107 }
108
109 void ec_enc_uint64(ec_enc *_this,ec_uint64 _fl,ec_uint64 _ft){
110   ec_uint64 mask;
111   unsigned  ft;
112   unsigned  fl;
113   int       ftb;
114   _ft--;
115   ftb=EC_ILOG64(_ft)&-!!_ft;
116   while(ftb>EC_UNIT_BITS){
117     ftb-=EC_UNIT_BITS;
118     ft=(unsigned)(_ft>>ftb)+1;
119     fl=(unsigned)(_fl>>ftb);
120     ec_encode(_this,fl,fl+1,ft);
121     if(fl<ft-1){
122       ec_enc_bits64(_this,_fl,ftb);
123       return;
124     }
125     mask=((ec_uint64)1<<ftb)-1;
126     _fl=_fl&mask;
127     _ft=_ft&mask;
128   }
129   ec_encode(_this,_fl,_fl+1,_ft+1);
130 }