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