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