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