Add oc_enc_tell to report an estimate of the number of bits used so far.
[opus.git] / libentcode / ectest.c
1 #include <stdio.h>
2 #include <math.h>
3 #include "probenc.h"
4 #include "probdec.h"
5 #include "bitrenc.h"
6
7 int main(int _argc,char **_argv){
8   ec_byte_buffer buf;
9   ec_enc         enc;
10   ec_dec         dec;
11   ec_probmod     mod;
12   ec_uint64      sym64;
13   long           nbits;
14   double         entropy;
15   int            ft;
16   int            ftb;
17   int            sym;
18   int            sz;
19   int            s;
20   int            i;
21   entropy=0;
22   /*Testing encoding of raw bit values.*/
23   ec_byte_writeinit(&buf);
24   ec_enc_init(&enc,&buf);
25   for(ft=0;ft<1024;ft++){
26     for(i=0;i<ft;i++){
27       entropy+=log(ft)*M_LOG2E;
28       ec_enc_uint(&enc,i,ft);
29       entropy+=log(ft)*M_LOG2E+30;
30       ec_enc_uint64(&enc,(ec_uint64)i<<30|i,(ec_uint64)ft<<30);
31     }
32   }
33   /*Testing encoding of raw bit values.*/
34   for(ftb=0;ftb<16;ftb++){
35     for(i=0;i<(1<<ftb);i++){
36       long nbits;
37       long nbits2;
38       entropy+=ftb;
39       nbits=ec_enc_tell(&enc);
40       ec_enc_bits(&enc,i,ftb);
41       nbits2=ec_enc_tell(&enc);
42       if(nbits2-nbits!=ftb){
43         fprintf(stderr,"Used %li bits to encode %i bits directly.\n",
44          nbits2-nbits,ftb);
45       }
46       entropy+=ftb+30;
47       nbits=nbits2;
48       ec_enc_bits64(&enc,(ec_uint64)i<<30|i,ftb+30);
49       nbits2=ec_enc_tell(&enc);
50       if(nbits2-nbits!=ftb+30){
51         fprintf(stderr,"Used %li bits to encode %i bits directly.\n",
52          nbits2-nbits,ftb+30);
53       }
54     }
55   }
56   for(sz=1;sz<256;sz++){
57     ec_probmod_init_full(&mod,sz,1,sz+(sz>>1),NULL);
58     for(i=0;i<sz;i++){
59       s=((unsigned)(i*45678901+7))%sz;
60       entropy+=(log(mod.ft)-log(ec_bitree_get_freq(mod.bitree,s)))*M_LOG2E;
61       ec_probmod_write(&mod,&enc,s);
62     }
63     ec_probmod_clear(&mod);
64   }
65   for(sz=11;sz<256;sz++){
66     ec_probmod_init_full(&mod,sz,1,sz+(sz>>1),NULL);
67     for(i=0;i<sz;i++){
68       s=((unsigned)(i*45678901+7))%sz;
69       entropy+=(log(ec_bitree_get_cumul(mod.bitree,EC_MINI(s+6,sz))-
70        ec_bitree_get_cumul(mod.bitree,EC_MAXI(s-5,0)))-
71        log(ec_bitree_get_freq(mod.bitree,s)))*M_LOG2E;
72       ec_probmod_write_range(&mod,&enc,s,EC_MAXI(s-5,0),EC_MINI(s+6,sz));
73     }
74     ec_probmod_clear(&mod);
75   }
76   nbits=ec_enc_tell(&enc);
77   ec_enc_done(&enc);
78   fprintf(stderr,
79    "Encoded %0.2lf bits of entropy to %li bits (%0.3lf%% wasted).\n",
80    entropy,nbits,100*(nbits-entropy)/nbits);
81   fprintf(stderr,"Packed to %li bytes.\n",(long)(buf.ptr-buf.buf));
82   ec_byte_readinit(&buf,ec_byte_get_buffer(&buf),ec_byte_bytes(&buf));
83   ec_dec_init(&dec,&buf);
84   for(ft=0;ft<1024;ft++){
85     for(i=0;i<ft;i++){
86       sym=ec_dec_uint(&dec,ft);
87       if(sym!=i){
88         fprintf(stderr,"Decoded %i instead of %i with ft of %i.\n",sym,i,ft);
89         return -1;
90       }
91       sym64=ec_dec_uint64(&dec,(ec_uint64)ft<<30);
92       if(sym64!=((ec_uint64)i<<30|i)){
93         fprintf(stderr,"Decoded %lli instead of %lli with ft of %lli.\n",sym64,
94          (ec_uint64)i<<30|i,(ec_uint64)ft<<30);
95       }
96     }
97   }
98   for(ftb=0;ftb<16;ftb++){
99     for(i=0;i<(1<<ftb);i++){
100       sym=ec_dec_bits(&dec,ftb);
101       if(sym!=i){
102         fprintf(stderr,"Decoded %i instead of %i with ftb of %i.\n",sym,i,ftb);
103         return -1;
104       }
105       sym64=ec_dec_bits64(&dec,ftb+30);
106       if(sym64!=((ec_uint64)i<<30|i)){
107         fprintf(stderr,"Decoded %lli instead of %lli with ftb of %i.\n",
108          sym64,(ec_uint64)i<<30|i,ftb+30);
109       }
110     }
111   }
112   for(sz=1;sz<256;sz++){
113     ec_probmod_init_full(&mod,sz,1,sz+(sz>>1),NULL);
114     for(i=0;i<sz;i++){
115       s=((unsigned)(i*45678901+7))%sz;
116       sym=ec_probmod_read(&mod,&dec);
117       if(sym!=s){
118         fprintf(stderr,"Decoded %i instead of %i with sz of %i.\n",sym,s,sz);
119         return -1;
120       }
121     }
122     ec_probmod_clear(&mod);
123   }
124   for(sz=11;sz<256;sz++){
125     ec_probmod_init_full(&mod,sz,1,sz+(sz>>1),NULL);
126     for(i=0;i<sz;i++){
127       s=((unsigned)(i*45678901+7))%sz;
128       sym=ec_probmod_read_range(&mod,&dec,EC_MAXI(s-5,0),EC_MINI(s+6,sz));
129       if(sym!=s){
130         fprintf(stderr,"Decoded %i instead of %i with sz of %i.\n",sym,s,sz);
131         return -1;
132       }
133     }
134     ec_probmod_clear(&mod);
135   }
136   ec_byte_writeclear(&buf);
137   fprintf(stderr,"All tests passed.\n");
138   return 0;
139 }