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