| Jean-Marc Valin | 9a0bba1 | 2008-02-20 14:08:50 +1100 | [diff] [blame] | 1 | #ifdef HAVE_CONFIG_H |
| 2 | #include "config.h" |
| 3 | #endif |
| 4 | |
| tterribe | 7e3293f | 2008-01-23 23:04:43 +0000 | [diff] [blame] | 5 | #include <stdlib.h> |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 6 | #include <stdio.h> |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 7 | #include <math.h> |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 8 | #include "bitrenc.h" |
| Jean-Marc Valin | 6238bc0 | 2008-01-28 22:28:54 +1100 | [diff] [blame] | 9 | #include "entcode.h" |
| 10 | #include "entenc.h" |
| 11 | #include "entdec.h" |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 12 | |
| 13 | int main(int _argc,char **_argv){ |
| 14 | ec_byte_buffer buf; |
| 15 | ec_enc enc; |
| 16 | ec_dec dec; |
| Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 17 | ec_uint64 sym64; |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 18 | long nbits; |
| tterribe | 3eff11d | 2008-01-11 05:51:49 +0000 | [diff] [blame] | 19 | long nbits2; |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 20 | double entropy; |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 21 | int ft; |
| 22 | int ftb; |
| 23 | int sym; |
| 24 | int sz; |
| 25 | int s; |
| 26 | int i; |
| Jean-Marc Valin | 3df6e27 | 2008-02-20 15:08:08 +1100 | [diff] [blame^] | 27 | int ret; |
| 28 | ret=0; |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 29 | entropy=0; |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 30 | /*Testing encoding of raw bit values.*/ |
| 31 | ec_byte_writeinit(&buf); |
| 32 | ec_enc_init(&enc,&buf); |
| 33 | for(ft=0;ft<1024;ft++){ |
| 34 | for(i=0;i<ft;i++){ |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 35 | entropy+=log(ft)*M_LOG2E; |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 36 | ec_enc_uint(&enc,i,ft); |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 37 | entropy+=log(ft)*M_LOG2E+30; |
| Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 38 | ec_enc_uint64(&enc,(ec_uint64)i<<30|i,(ec_uint64)ft<<30); |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | /*Testing encoding of raw bit values.*/ |
| 42 | for(ftb=0;ftb<16;ftb++){ |
| 43 | for(i=0;i<(1<<ftb);i++){ |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 44 | entropy+=ftb; |
| tterribe | 3eff11d | 2008-01-11 05:51:49 +0000 | [diff] [blame] | 45 | nbits=ec_enc_tell(&enc,0); |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 46 | ec_enc_bits(&enc,i,ftb); |
| tterribe | 3eff11d | 2008-01-11 05:51:49 +0000 | [diff] [blame] | 47 | nbits2=ec_enc_tell(&enc,0); |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 48 | if(nbits2-nbits!=ftb){ |
| 49 | fprintf(stderr,"Used %li bits to encode %i bits directly.\n", |
| 50 | nbits2-nbits,ftb); |
| Jean-Marc Valin | 3df6e27 | 2008-02-20 15:08:08 +1100 | [diff] [blame^] | 51 | ret=-1; |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 52 | } |
| 53 | entropy+=ftb+30; |
| 54 | nbits=nbits2; |
| Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 55 | ec_enc_bits64(&enc,(ec_uint64)i<<30|i,ftb+30); |
| tterribe | 3eff11d | 2008-01-11 05:51:49 +0000 | [diff] [blame] | 56 | nbits2=ec_enc_tell(&enc,0); |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 57 | if(nbits2-nbits!=ftb+30){ |
| 58 | fprintf(stderr,"Used %li bits to encode %i bits directly.\n", |
| 59 | nbits2-nbits,ftb+30); |
| Jean-Marc Valin | 3df6e27 | 2008-02-20 15:08:08 +1100 | [diff] [blame^] | 60 | ret=-1; |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 61 | } |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 62 | } |
| 63 | } |
| tterribe | 3eff11d | 2008-01-11 05:51:49 +0000 | [diff] [blame] | 64 | nbits=ec_enc_tell(&enc,4); |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 65 | ec_enc_done(&enc); |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 66 | fprintf(stderr, |
| tterribe | fad779c | 2008-01-11 05:12:17 +0000 | [diff] [blame] | 67 | "Encoded %0.2lf bits of entropy to %0.2lf bits (%0.3lf%% wasted).\n", |
| 68 | entropy,ldexp(nbits,-4),100*(nbits-ldexp(entropy,4))/nbits); |
| tterribe | 06390d0 | 2008-01-11 03:13:50 +0000 | [diff] [blame] | 69 | fprintf(stderr,"Packed to %li bytes.\n",(long)(buf.ptr-buf.buf)); |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 70 | ec_byte_readinit(&buf,ec_byte_get_buffer(&buf),ec_byte_bytes(&buf)); |
| 71 | ec_dec_init(&dec,&buf); |
| 72 | for(ft=0;ft<1024;ft++){ |
| 73 | for(i=0;i<ft;i++){ |
| 74 | sym=ec_dec_uint(&dec,ft); |
| 75 | if(sym!=i){ |
| 76 | fprintf(stderr,"Decoded %i instead of %i with ft of %i.\n",sym,i,ft); |
| Jean-Marc Valin | 3df6e27 | 2008-02-20 15:08:08 +1100 | [diff] [blame^] | 77 | ret=-1; |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 78 | } |
| Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 79 | sym64=ec_dec_uint64(&dec,(ec_uint64)ft<<30); |
| 80 | if(sym64!=((ec_uint64)i<<30|i)){ |
| 81 | fprintf(stderr,"Decoded %lli instead of %lli with ft of %lli.\n",sym64, |
| 82 | (ec_uint64)i<<30|i,(ec_uint64)ft<<30); |
| Jean-Marc Valin | 3df6e27 | 2008-02-20 15:08:08 +1100 | [diff] [blame^] | 83 | ret=-1; |
| Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 84 | } |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | for(ftb=0;ftb<16;ftb++){ |
| 88 | for(i=0;i<(1<<ftb);i++){ |
| 89 | sym=ec_dec_bits(&dec,ftb); |
| 90 | if(sym!=i){ |
| 91 | fprintf(stderr,"Decoded %i instead of %i with ftb of %i.\n",sym,i,ftb); |
| Jean-Marc Valin | 3df6e27 | 2008-02-20 15:08:08 +1100 | [diff] [blame^] | 92 | ret=-1; |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 93 | } |
| Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 94 | sym64=ec_dec_bits64(&dec,ftb+30); |
| 95 | if(sym64!=((ec_uint64)i<<30|i)){ |
| 96 | fprintf(stderr,"Decoded %lli instead of %lli with ftb of %i.\n", |
| 97 | sym64,(ec_uint64)i<<30|i,ftb+30); |
| Jean-Marc Valin | 3df6e27 | 2008-02-20 15:08:08 +1100 | [diff] [blame^] | 98 | ret=-1; |
| Timothy B. Terriberry | f13fea7 | 2007-12-11 13:25:57 +1100 | [diff] [blame] | 99 | } |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 100 | } |
| 101 | } |
| tterribe | 3eff11d | 2008-01-11 05:51:49 +0000 | [diff] [blame] | 102 | nbits2=ec_dec_tell(&dec,4); |
| 103 | if(nbits!=nbits2){ |
| 104 | fprintf(stderr, |
| 105 | "Reported number of bits used was %0.2lf, should be %0.2lf.\n", |
| 106 | ldexp(nbits2,-4),ldexp(nbits,-4)); |
| Jean-Marc Valin | 3df6e27 | 2008-02-20 15:08:08 +1100 | [diff] [blame^] | 107 | ret=-1; |
| tterribe | 3eff11d | 2008-01-11 05:51:49 +0000 | [diff] [blame] | 108 | } |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 109 | ec_byte_writeclear(&buf); |
| tterribe | 7e3293f | 2008-01-23 23:04:43 +0000 | [diff] [blame] | 110 | fprintf(stderr,"Testing random streams...\n"); |
| 111 | srand(0); |
| Jean-Marc Valin | 2991af5 | 2008-02-19 21:02:43 +1100 | [diff] [blame] | 112 | for(i=0;i<409600;i++){ |
| tterribe | 7e3293f | 2008-01-23 23:04:43 +0000 | [diff] [blame] | 113 | unsigned *data; |
| 114 | int j; |
| Jean-Marc Valin | cb0956d | 2008-02-07 20:21:57 +1100 | [diff] [blame] | 115 | int tell_bits; |
| 116 | int zeros; |
| Jean-Marc Valin | 2991af5 | 2008-02-19 21:02:43 +1100 | [diff] [blame] | 117 | ft=rand()/((RAND_MAX>>(rand()%11))+1)+10; |
| 118 | sz=rand()/((RAND_MAX>>(rand()%9))+1); |
| tterribe | 7e3293f | 2008-01-23 23:04:43 +0000 | [diff] [blame] | 119 | data=(unsigned *)malloc(sz*sizeof(*data)); |
| 120 | ec_byte_writeinit(&buf); |
| 121 | ec_enc_init(&enc,&buf); |
| Jean-Marc Valin | cb0956d | 2008-02-07 20:21:57 +1100 | [diff] [blame] | 122 | zeros = rand()%13==0; |
| tterribe | 7e3293f | 2008-01-23 23:04:43 +0000 | [diff] [blame] | 123 | for(j=0;j<sz;j++){ |
| Jean-Marc Valin | cb0956d | 2008-02-07 20:21:57 +1100 | [diff] [blame] | 124 | if (zeros) |
| 125 | data[j]=0; |
| 126 | else |
| 127 | data[j]=rand()%ft; |
| tterribe | 7e3293f | 2008-01-23 23:04:43 +0000 | [diff] [blame] | 128 | ec_enc_uint(&enc,data[j],ft); |
| 129 | } |
| Jean-Marc Valin | 2991af5 | 2008-02-19 21:02:43 +1100 | [diff] [blame] | 130 | if (rand()%2==0) |
| 131 | while(ec_enc_tell(&enc, 0)%8 != 0) |
| 132 | ec_enc_uint(&enc, rand()%2, 2); |
| Jean-Marc Valin | cb0956d | 2008-02-07 20:21:57 +1100 | [diff] [blame] | 133 | tell_bits = ec_enc_tell(&enc, 0); |
| tterribe | 7e3293f | 2008-01-23 23:04:43 +0000 | [diff] [blame] | 134 | ec_enc_done(&enc); |
| Jean-Marc Valin | b375670 | 2008-02-08 11:50:17 +1100 | [diff] [blame] | 135 | if ((tell_bits+7)/8 < ec_byte_bytes(&buf)) |
| 136 | { |
| 137 | fprintf (stderr, "tell() lied, there's %d bytes instead of %d\n", |
| 138 | ec_byte_bytes(&buf), (tell_bits+7)/8); |
| Jean-Marc Valin | 3df6e27 | 2008-02-20 15:08:08 +1100 | [diff] [blame^] | 139 | ret=-1; |
| Jean-Marc Valin | b375670 | 2008-02-08 11:50:17 +1100 | [diff] [blame] | 140 | } |
| Jean-Marc Valin | cb0956d | 2008-02-07 20:21:57 +1100 | [diff] [blame] | 141 | tell_bits -= 8*ec_byte_bytes(&buf); |
| tterribe | 7e3293f | 2008-01-23 23:04:43 +0000 | [diff] [blame] | 142 | ec_byte_readinit(&buf,ec_byte_get_buffer(&buf),ec_byte_bytes(&buf)); |
| 143 | ec_dec_init(&dec,&buf); |
| 144 | for(j=0;j<sz;j++){ |
| 145 | sym=ec_dec_uint(&dec,ft); |
| 146 | if(sym!=data[j]){ |
| 147 | fprintf(stderr, |
| 148 | "Decoded %i instead of %i with ft of %i at position %i of %i.\n", |
| 149 | sym,data[j],ft,j,sz); |
| Jean-Marc Valin | 3df6e27 | 2008-02-20 15:08:08 +1100 | [diff] [blame^] | 150 | ret=-1; |
| tterribe | 7e3293f | 2008-01-23 23:04:43 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | ec_byte_writeclear(&buf); |
| tterribe | bab4fb1 | 2008-01-23 23:10:28 +0000 | [diff] [blame] | 154 | free(data); |
| tterribe | 7e3293f | 2008-01-23 23:04:43 +0000 | [diff] [blame] | 155 | } |
| Jean-Marc Valin | 3df6e27 | 2008-02-20 15:08:08 +1100 | [diff] [blame^] | 156 | return ret; |
| Timothy B. Terriberry | 2ec8d9e | 2007-12-06 15:09:53 +1100 | [diff] [blame] | 157 | } |