encoder pre-emphasis now in 16-bits
[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_bits64(ec_enc *_this,ec_uint64 _fl,int _ftb){
81   if(_ftb>32){
82     ec_enc_bits(_this,(ec_uint32)(_fl>>32),_ftb-32);
83     _ftb=32;
84     _fl&=0xFFFFFFFF;
85   }
86   ec_enc_bits(_this,(ec_uint32)_fl,_ftb);
87 }
88
89 void ec_enc_uint(ec_enc *_this,ec_uint32 _fl,ec_uint32 _ft){
90   unsigned  ft;
91   unsigned  fl;
92   int       ftb;
93   _ft--;
94   ftb=EC_ILOG(_ft)&-!!_ft;
95   if(ftb>EC_UNIT_BITS){
96     ftb-=EC_UNIT_BITS;
97     ft=(_ft>>ftb)+1;
98     fl=(unsigned)(_fl>>ftb);
99     ec_encode(_this,fl,fl+1,ft);
100     ec_enc_bits(_this,_fl,ftb);
101   } else {
102     ec_encode(_this,_fl,_fl+1,_ft+1);
103   }
104 }
105
106 void ec_enc_uint64(ec_enc *_this,ec_uint64 _fl,ec_uint64 _ft){
107   unsigned  ft;
108   unsigned  fl;
109   int       ftb;
110   _ft--;
111   ftb=EC_ILOG64(_ft)&-!!_ft;
112   if(ftb>EC_UNIT_BITS){
113     ftb-=EC_UNIT_BITS;
114     ft=(unsigned)(_ft>>ftb)+1;
115     fl=(unsigned)(_fl>>ftb);
116     ec_encode(_this,fl,fl+1,ft);
117     ec_enc_bits64(_this,_fl,ftb);
118   } else {
119     ec_encode(_this,_fl,_fl+1,_ft+1);
120   }
121 }