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