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