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