test_unit_entropy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /* Copyright (c) 2007-2011 Xiph.Org Foundation, Mozilla Corporation,
  2. Gregory Maxwell
  3. Written by Jean-Marc Valin, Gregory Maxwell, and Timothy B. Terriberry */
  4. /*
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. - Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. - Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  16. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  17. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  21. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <math.h>
  31. #include <time.h>
  32. #include "entcode.h"
  33. #include "entenc.h"
  34. #include "entdec.h"
  35. #include <string.h>
  36. #include "entenc.c"
  37. #include "entdec.c"
  38. #include "entcode.c"
  39. #ifndef M_LOG2E
  40. # define M_LOG2E 1.4426950408889634074
  41. #endif
  42. #define DATA_SIZE 10000000
  43. #define DATA_SIZE2 10000
  44. int main(int _argc,char **_argv){
  45. ec_enc enc;
  46. ec_dec dec;
  47. long nbits;
  48. long nbits2;
  49. double entropy;
  50. int ft;
  51. int ftb;
  52. int sz;
  53. int i;
  54. int ret;
  55. unsigned int sym;
  56. unsigned int seed;
  57. unsigned char *ptr;
  58. const char *env_seed;
  59. ret=0;
  60. entropy=0;
  61. if (_argc > 2) {
  62. fprintf(stderr, "Usage: %s [<seed>]\n", _argv[0]);
  63. return 1;
  64. }
  65. env_seed = getenv("SEED");
  66. if (_argc > 1)
  67. seed = atoi(_argv[1]);
  68. else if (env_seed)
  69. seed = atoi(env_seed);
  70. else
  71. seed = time(NULL);
  72. /*Testing encoding of raw bit values.*/
  73. ptr = (unsigned char *)malloc(DATA_SIZE);
  74. ec_enc_init(&enc,ptr, DATA_SIZE);
  75. for(ft=2;ft<1024;ft++){
  76. for(i=0;i<ft;i++){
  77. entropy+=log(ft)*M_LOG2E;
  78. ec_enc_uint(&enc,i,ft);
  79. }
  80. }
  81. /*Testing encoding of raw bit values.*/
  82. for(ftb=1;ftb<16;ftb++){
  83. for(i=0;i<(1<<ftb);i++){
  84. entropy+=ftb;
  85. nbits=ec_tell(&enc);
  86. ec_enc_bits(&enc,i,ftb);
  87. nbits2=ec_tell(&enc);
  88. if(nbits2-nbits!=ftb){
  89. fprintf(stderr,"Used %li bits to encode %i bits directly.\n",
  90. nbits2-nbits,ftb);
  91. ret=-1;
  92. }
  93. }
  94. }
  95. nbits=ec_tell_frac(&enc);
  96. ec_enc_done(&enc);
  97. fprintf(stderr,
  98. "Encoded %0.2lf bits of entropy to %0.2lf bits (%0.3lf%% wasted).\n",
  99. entropy,ldexp(nbits,-3),100*(nbits-ldexp(entropy,3))/nbits);
  100. fprintf(stderr,"Packed to %li bytes.\n",(long)ec_range_bytes(&enc));
  101. ec_dec_init(&dec,ptr,DATA_SIZE);
  102. for(ft=2;ft<1024;ft++){
  103. for(i=0;i<ft;i++){
  104. sym=ec_dec_uint(&dec,ft);
  105. if(sym!=(unsigned)i){
  106. fprintf(stderr,"Decoded %i instead of %i with ft of %i.\n",sym,i,ft);
  107. ret=-1;
  108. }
  109. }
  110. }
  111. for(ftb=1;ftb<16;ftb++){
  112. for(i=0;i<(1<<ftb);i++){
  113. sym=ec_dec_bits(&dec,ftb);
  114. if(sym!=(unsigned)i){
  115. fprintf(stderr,"Decoded %i instead of %i with ftb of %i.\n",sym,i,ftb);
  116. ret=-1;
  117. }
  118. }
  119. }
  120. nbits2=ec_tell_frac(&dec);
  121. if(nbits!=nbits2){
  122. fprintf(stderr,
  123. "Reported number of bits used was %0.2lf, should be %0.2lf.\n",
  124. ldexp(nbits2,-3),ldexp(nbits,-3));
  125. ret=-1;
  126. }
  127. /*Testing an encoder bust prefers range coder data over raw bits.
  128. This isn't a general guarantee, will only work for data that is buffered in
  129. the encoder state and not yet stored in the user buffer, and should never
  130. get used in practice.
  131. It's mostly here for code coverage completeness.*/
  132. /*Start with a 16-bit buffer.*/
  133. ec_enc_init(&enc,ptr,2);
  134. /*Write 7 raw bits.*/
  135. ec_enc_bits(&enc,0x55,7);
  136. /*Write 12.3 bits of range coder data.*/
  137. ec_enc_uint(&enc,1,2);
  138. ec_enc_uint(&enc,1,3);
  139. ec_enc_uint(&enc,1,4);
  140. ec_enc_uint(&enc,1,5);
  141. ec_enc_uint(&enc,2,6);
  142. ec_enc_uint(&enc,6,7);
  143. ec_enc_done(&enc);
  144. ec_dec_init(&dec,ptr,2);
  145. if(!enc.error
  146. /*The raw bits should have been overwritten by the range coder data.*/
  147. ||ec_dec_bits(&dec,7)!=0x05
  148. /*And all the range coder data should have been encoded correctly.*/
  149. ||ec_dec_uint(&dec,2)!=1
  150. ||ec_dec_uint(&dec,3)!=1
  151. ||ec_dec_uint(&dec,4)!=1
  152. ||ec_dec_uint(&dec,5)!=1
  153. ||ec_dec_uint(&dec,6)!=2
  154. ||ec_dec_uint(&dec,7)!=6){
  155. fprintf(stderr,"Encoder bust overwrote range coder data with raw bits.\n");
  156. ret=-1;
  157. }
  158. srand(seed);
  159. fprintf(stderr,"Testing random streams... Random seed: %u (%.4X)\n", seed, rand() % 65536);
  160. for(i=0;i<409600;i++){
  161. unsigned *data;
  162. unsigned *tell;
  163. unsigned tell_bits;
  164. int j;
  165. int zeros;
  166. ft=rand()/((RAND_MAX>>(rand()%11U))+1U)+10;
  167. sz=rand()/((RAND_MAX>>(rand()%9U))+1U);
  168. data=(unsigned *)malloc(sz*sizeof(*data));
  169. tell=(unsigned *)malloc((sz+1)*sizeof(*tell));
  170. ec_enc_init(&enc,ptr,DATA_SIZE2);
  171. zeros = rand()%13==0;
  172. tell[0]=ec_tell_frac(&enc);
  173. for(j=0;j<sz;j++){
  174. if (zeros)
  175. data[j]=0;
  176. else
  177. data[j]=rand()%ft;
  178. ec_enc_uint(&enc,data[j],ft);
  179. tell[j+1]=ec_tell_frac(&enc);
  180. }
  181. if (rand()%2==0)
  182. while(ec_tell(&enc)%8 != 0)
  183. ec_enc_uint(&enc, rand()%2, 2);
  184. tell_bits = ec_tell(&enc);
  185. ec_enc_done(&enc);
  186. if(tell_bits!=(unsigned)ec_tell(&enc)){
  187. fprintf(stderr,"ec_tell() changed after ec_enc_done(): %i instead of %i (Random seed: %u)\n",
  188. ec_tell(&enc),tell_bits,seed);
  189. ret=-1;
  190. }
  191. if ((tell_bits+7)/8 < ec_range_bytes(&enc))
  192. {
  193. fprintf (stderr, "ec_tell() lied, there's %i bytes instead of %d (Random seed: %u)\n",
  194. ec_range_bytes(&enc), (tell_bits+7)/8,seed);
  195. ret=-1;
  196. }
  197. ec_dec_init(&dec,ptr,DATA_SIZE2);
  198. if(ec_tell_frac(&dec)!=tell[0]){
  199. fprintf(stderr,
  200. "Tell mismatch between encoder and decoder at symbol %i: %i instead of %i (Random seed: %u).\n",
  201. 0,ec_tell_frac(&dec),tell[0],seed);
  202. }
  203. for(j=0;j<sz;j++){
  204. sym=ec_dec_uint(&dec,ft);
  205. if(sym!=data[j]){
  206. fprintf(stderr,
  207. "Decoded %i instead of %i with ft of %i at position %i of %i (Random seed: %u).\n",
  208. sym,data[j],ft,j,sz,seed);
  209. ret=-1;
  210. }
  211. if(ec_tell_frac(&dec)!=tell[j+1]){
  212. fprintf(stderr,
  213. "Tell mismatch between encoder and decoder at symbol %i: %i instead of %i (Random seed: %u).\n",
  214. j+1,ec_tell_frac(&dec),tell[j+1],seed);
  215. }
  216. }
  217. free(tell);
  218. free(data);
  219. }
  220. /*Test compatibility between multiple different encode/decode routines.*/
  221. for(i=0;i<409600;i++){
  222. unsigned *logp1;
  223. unsigned *data;
  224. unsigned *tell;
  225. unsigned *enc_method;
  226. int j;
  227. sz=rand()/((RAND_MAX>>(rand()%9U))+1U);
  228. logp1=(unsigned *)malloc(sz*sizeof(*logp1));
  229. data=(unsigned *)malloc(sz*sizeof(*data));
  230. tell=(unsigned *)malloc((sz+1)*sizeof(*tell));
  231. enc_method=(unsigned *)malloc(sz*sizeof(*enc_method));
  232. ec_enc_init(&enc,ptr,DATA_SIZE2);
  233. tell[0]=ec_tell_frac(&enc);
  234. for(j=0;j<sz;j++){
  235. data[j]=rand()/((RAND_MAX>>1)+1);
  236. logp1[j]=(rand()%15)+1;
  237. enc_method[j]=rand()/((RAND_MAX>>2)+1);
  238. switch(enc_method[j]){
  239. case 0:{
  240. ec_encode(&enc,data[j]?(1<<logp1[j])-1:0,
  241. (1<<logp1[j])-(data[j]?0:1),1<<logp1[j]);
  242. }break;
  243. case 1:{
  244. ec_encode_bin(&enc,data[j]?(1<<logp1[j])-1:0,
  245. (1<<logp1[j])-(data[j]?0:1),logp1[j]);
  246. }break;
  247. case 2:{
  248. ec_enc_bit_logp(&enc,data[j],logp1[j]);
  249. }break;
  250. case 3:{
  251. unsigned char icdf[2];
  252. icdf[0]=1;
  253. icdf[1]=0;
  254. ec_enc_icdf(&enc,data[j],icdf,logp1[j]);
  255. }break;
  256. }
  257. tell[j+1]=ec_tell_frac(&enc);
  258. }
  259. ec_enc_done(&enc);
  260. if((ec_tell(&enc)+7U)/8U<ec_range_bytes(&enc)){
  261. fprintf(stderr,"tell() lied, there's %i bytes instead of %d (Random seed: %u)\n",
  262. ec_range_bytes(&enc),(ec_tell(&enc)+7)/8,seed);
  263. ret=-1;
  264. }
  265. ec_dec_init(&dec,ptr,DATA_SIZE2);
  266. if(ec_tell_frac(&dec)!=tell[0]){
  267. fprintf(stderr,
  268. "Tell mismatch between encoder and decoder at symbol %i: %i instead of %i (Random seed: %u).\n",
  269. 0,ec_tell_frac(&dec),tell[0],seed);
  270. }
  271. for(j=0;j<sz;j++){
  272. int fs;
  273. int dec_method;
  274. dec_method=rand()/((RAND_MAX>>2)+1);
  275. switch(dec_method){
  276. case 0:{
  277. fs=ec_decode(&dec,1<<logp1[j]);
  278. sym=fs>=(1<<logp1[j])-1;
  279. ec_dec_update(&dec,sym?(1<<logp1[j])-1:0,
  280. (1<<logp1[j])-(sym?0:1),1<<logp1[j]);
  281. }break;
  282. case 1:{
  283. fs=ec_decode_bin(&dec,logp1[j]);
  284. sym=fs>=(1<<logp1[j])-1;
  285. ec_dec_update(&dec,sym?(1<<logp1[j])-1:0,
  286. (1<<logp1[j])-(sym?0:1),1<<logp1[j]);
  287. }break;
  288. case 2:{
  289. sym=ec_dec_bit_logp(&dec,logp1[j]);
  290. }break;
  291. case 3:{
  292. unsigned char icdf[2];
  293. icdf[0]=1;
  294. icdf[1]=0;
  295. sym=ec_dec_icdf(&dec,icdf,logp1[j]);
  296. }break;
  297. }
  298. if(sym!=data[j]){
  299. fprintf(stderr,
  300. "Decoded %i instead of %i with logp1 of %i at position %i of %i (Random seed: %u).\n",
  301. sym,data[j],logp1[j],j,sz,seed);
  302. fprintf(stderr,"Encoding method: %i, decoding method: %i\n",
  303. enc_method[j],dec_method);
  304. ret=-1;
  305. }
  306. if(ec_tell_frac(&dec)!=tell[j+1]){
  307. fprintf(stderr,
  308. "Tell mismatch between encoder and decoder at symbol %i: %i instead of %i (Random seed: %u).\n",
  309. j+1,ec_tell_frac(&dec),tell[j+1],seed);
  310. }
  311. }
  312. free(enc_method);
  313. free(tell);
  314. free(data);
  315. free(logp1);
  316. }
  317. ec_enc_init(&enc,ptr,DATA_SIZE2);
  318. ec_enc_bit_logp(&enc,0,1);
  319. ec_enc_bit_logp(&enc,0,1);
  320. ec_enc_bit_logp(&enc,0,1);
  321. ec_enc_bit_logp(&enc,0,1);
  322. ec_enc_bit_logp(&enc,0,2);
  323. ec_enc_patch_initial_bits(&enc,3,2);
  324. if(enc.error){
  325. fprintf(stderr,"patch_initial_bits failed");
  326. ret=-1;
  327. }
  328. ec_enc_patch_initial_bits(&enc,0,5);
  329. if(!enc.error){
  330. fprintf(stderr,"patch_initial_bits didn't fail when it should have");
  331. ret=-1;
  332. }
  333. ec_enc_done(&enc);
  334. if(ec_range_bytes(&enc)!=1||ptr[0]!=192){
  335. fprintf(stderr,"Got %d when expecting 192 for patch_initial_bits",ptr[0]);
  336. ret=-1;
  337. }
  338. ec_enc_init(&enc,ptr,DATA_SIZE2);
  339. ec_enc_bit_logp(&enc,0,1);
  340. ec_enc_bit_logp(&enc,0,1);
  341. ec_enc_bit_logp(&enc,1,6);
  342. ec_enc_bit_logp(&enc,0,2);
  343. ec_enc_patch_initial_bits(&enc,0,2);
  344. if(enc.error){
  345. fprintf(stderr,"patch_initial_bits failed");
  346. ret=-1;
  347. }
  348. ec_enc_done(&enc);
  349. if(ec_range_bytes(&enc)!=2||ptr[0]!=63){
  350. fprintf(stderr,"Got %d when expecting 63 for patch_initial_bits",ptr[0]);
  351. ret=-1;
  352. }
  353. ec_enc_init(&enc,ptr,2);
  354. ec_enc_bit_logp(&enc,0,2);
  355. for(i=0;i<48;i++){
  356. ec_enc_bits(&enc,0,1);
  357. }
  358. ec_enc_done(&enc);
  359. if(!enc.error){
  360. fprintf(stderr,"Raw bits overfill didn't fail when it should have");
  361. ret=-1;
  362. }
  363. ec_enc_init(&enc,ptr,2);
  364. for(i=0;i<17;i++){
  365. ec_enc_bits(&enc,0,1);
  366. }
  367. ec_enc_done(&enc);
  368. if(!enc.error){
  369. fprintf(stderr,"17 raw bits encoded in two bytes");
  370. ret=-1;
  371. }
  372. free(ptr);
  373. return ret;
  374. }