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