ec_test.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*Daala video codec
  2. Copyright (c) 2007-2012 Daala project contributors. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. - Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. - Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  13. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  14. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  15. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  17. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  18. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  19. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <limits.h>
  27. #include <string.h>
  28. #define EC_MINI(_a,_b) ((_a)<(_b)?(_a):(_b))
  29. #define EC_MAXI(_a,_b) ((_a)>(_b)?(_a):(_b))
  30. # ifdef OD_GNUC_PREREQ
  31. # if OD_GNUC_PREREQ(3,4)
  32. /*Note the casts to (int) below: this prevents EC_CLZ{32|64}_OFFS from
  33. "upgrading" the type of an entire expression to an (unsigned) size_t.*/
  34. # if INT_MAX>=2147483647
  35. # define EC_CLZ32_OFFS ((int)sizeof(unsigned)*CHAR_BIT)
  36. # define EC_CLZ32(_x) (__builtin_clz(_x))
  37. # elif LONG_MAX>=2147483647L
  38. # define EC_CLZ32_OFFS ((int)sizeof(unsigned long)*CHAR_BIT)
  39. # define EC_CLZ32(_x) (__builtin_clzl(_x))
  40. # endif
  41. # endif
  42. # endif
  43. # if defined(EC_CLZ32)
  44. # define EC_ILOGNZ_32(_v) (EC_CLZ32_OFFS-EC_CLZ32(_v))
  45. # define EC_ILOG_32(_v) (EC_ILOGNZ_32(_v)&-!!(_v))
  46. # else
  47. # error "Need __builtin_clz or equivalent."
  48. # endif
  49. typedef signed char ec_bitree[32];
  50. typedef unsigned char ec_bitree_probs[16];
  51. typedef unsigned short ec_probs[16];
  52. #define EC_TREE_FTB (15)
  53. #define EC_TREE_FT (1<<EC_TREE_FTB)
  54. typedef struct ec_tree ec_tree;
  55. typedef struct ec_code_entry ec_code_entry;
  56. struct ec_tree{
  57. ec_bitree t;
  58. ec_bitree_probs p;
  59. ec_probs f;
  60. int s;
  61. };
  62. struct ec_code_entry{
  63. int tree;
  64. unsigned char v;
  65. unsigned char n;
  66. unsigned char l;
  67. };
  68. static ec_tree *trees;
  69. static int ntrees;
  70. static int ctrees;
  71. static ec_code_entry *entries;
  72. static int nentries;
  73. static int centries;
  74. static int ec_tree_count_leaves(const signed char *_t,const unsigned char *_p,
  75. int _i){
  76. return _i<0?1:ec_tree_count_leaves(_t,_p,_t[2*_i+0])+
  77. ec_tree_count_leaves(_t,_p,_t[2*_i+1]);
  78. }
  79. static void tree_fill_freqs(unsigned short *_f,
  80. const signed char *_t,const unsigned char *_p,int _i,int _p0){
  81. int i0;
  82. int i1;
  83. int l;
  84. int p0;
  85. int p1;
  86. i0=_t[2*_i+0];
  87. p0=_p0*_p[_i]+128>>8;
  88. l=ec_tree_count_leaves(_t,_p,i0);
  89. p0=p0>l?p0:l;
  90. i1=_t[2*_i+1];
  91. l=ec_tree_count_leaves(_t,_p,i1);
  92. p0=p0<_p0-l?p0:_p0-l;
  93. if(i0<0)_f[-i0-1]=p0;
  94. else tree_fill_freqs(_f,_t,_p,i0,p0);
  95. p1=_p0-p0;
  96. if(i1<0)_f[-i1-1]=p1;
  97. else tree_fill_freqs(_f,_t,_p,i1,p1);
  98. }
  99. static void ec_tree_init(ec_tree *_tree,
  100. const signed char *_t,const unsigned char *_p,int _s){
  101. int l;
  102. memcpy(_tree->t,_t,2*_s*sizeof(*_t));
  103. memcpy(_tree->p,_p,_s*sizeof(*_p));
  104. tree_fill_freqs(_tree->f,_t,_p,0,EC_TREE_FT);
  105. for(l=1;l<=_s;l++)_tree->f[l]+=_tree->f[l-1];
  106. for(;l<16;l++)_tree->f[l]=_tree->f[l-1];
  107. _tree->s=_s;
  108. }
  109. static int tree_read(signed char *_t,unsigned char *_p){
  110. int s;
  111. int i;
  112. int j;
  113. i=j=0;
  114. s=1;
  115. do{
  116. int t0;
  117. int t1;
  118. int p0;
  119. if(scanf("%i%i%i",&t0,&t1,&p0)<3)return 0;
  120. if(t0<0)t0=-(++j);
  121. s=t0+1<s?s:t0+1;
  122. if(t1<0)t1=-(++j);
  123. s=t1+1<s?s:t1+1;
  124. _t[2*i+0]=(signed char)t0;
  125. _t[2*i+1]=(signed char)t1;
  126. _p[i]=p0;
  127. }
  128. while(++i<s);
  129. return s;
  130. }
  131. static int entry_read(void){
  132. signed char t[64];
  133. unsigned char p[32];
  134. int s;
  135. int tree;
  136. int v;
  137. int n;
  138. int i;
  139. s=tree_read(t,p);
  140. if(s<=0)return s;
  141. for(tree=0;tree<ntrees;tree++){
  142. if(trees[tree].s==s
  143. &&memcmp(trees[tree].t,t,2*s*sizeof(*t))==0
  144. &&memcmp(trees[tree].p,p,s*sizeof(*p))==0){
  145. break;
  146. }
  147. }
  148. if(tree>=ntrees){
  149. if(ctrees<=ntrees){
  150. ctrees=ctrees<<1|1;
  151. trees=(ec_tree *)realloc(trees,ctrees*sizeof(*trees));
  152. }
  153. ec_tree_init(trees+tree,t,p,s);
  154. ntrees++;
  155. }
  156. if(centries<=nentries){
  157. centries=centries<<1|1;
  158. entries=(ec_code_entry *)realloc(entries,centries*sizeof(*entries));
  159. }
  160. entries[nentries].tree=tree;
  161. if(scanf("%i%i",&v,&n)<2)return 0;
  162. entries[nentries].v=(unsigned char)v;
  163. entries[nentries].n=(unsigned char)n;
  164. for(i=0;n-->0;)i=t[2*i+(v>>n&1)];
  165. entries[nentries].l=-i-1;
  166. nentries++;
  167. return 1;
  168. }
  169. typedef size_t ec_window;
  170. typedef struct ec_enc ec_enc;
  171. typedef struct ec_dec ec_dec;
  172. #define EC_WINDOW_SZ (sizeof(ec_window)*CHAR_BIT)
  173. /*This is meant to be a large, positive constant that can still be efficiently
  174. loaded as an immediate (on platforms like ARM, for example).
  175. Even relatively modest values like 100 would work fine.*/
  176. #define EC_LOTS_OF_BITS (0x4000)
  177. struct ec_enc{
  178. unsigned short *buf;
  179. size_t cbuf;
  180. size_t nbuf;
  181. unsigned low;
  182. short cnt;
  183. unsigned short rng;
  184. };
  185. struct ec_dec{
  186. const unsigned char *buf;
  187. const unsigned char *end;
  188. ec_window dif;
  189. short cnt;
  190. unsigned short rng;
  191. };
  192. #if defined(EC_MULTISYM)
  193. static void ec_enc_init(ec_enc *_this){
  194. _this->buf=NULL;
  195. _this->cbuf=0;
  196. _this->nbuf=0;
  197. _this->low=0;
  198. _this->cnt=-9;
  199. _this->rng=0x8000;
  200. }
  201. static void ec_enc_clear(ec_enc *_this){
  202. free(_this->buf);
  203. }
  204. static int ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ftb){
  205. unsigned l;
  206. unsigned r;
  207. int c;
  208. int s;
  209. unsigned d;
  210. unsigned u;
  211. unsigned v;
  212. #if defined(EC_MULT_FREE)
  213. unsigned ft;
  214. #endif
  215. /*printf("0x%08X %2i 0x%04X [0x%04X,0x%04X) {0x%04X}\n",
  216. _this->low,_this->cnt,_this->rng,_fl,_fh,1<<_ftb);*/
  217. l=_this->low;
  218. c=_this->cnt;
  219. r=_this->rng;
  220. #if defined(EC_MULT_FREE)
  221. ft=1<<_ftb;
  222. s=r>=ft<<1;
  223. if(s)ft<<=1;
  224. _fl<<=s;
  225. _fh<<=s;
  226. d=r-ft;
  227. u=_fl+EC_MINI(_fl,d);
  228. v=_fh+EC_MINI(_fh,d);
  229. #else
  230. u=r*_fl>>_ftb;
  231. v=r*_fh>>_ftb;
  232. #endif
  233. r=v-u;
  234. l+=u;
  235. d=16-EC_ILOGNZ_32(r);
  236. s=c+d;
  237. /*TODO: Right now we flush every time we have at least one byte available.
  238. Instead we should use an ec_window and flush right before we're about to
  239. shift bits off the end of the window.
  240. For a 32-bit window this is about the same amount of work, but for a 64-bit
  241. window it should be a fair win.*/
  242. if(s>=0){
  243. unsigned short *buf;
  244. size_t cbuf;
  245. size_t nbuf;
  246. unsigned m;
  247. buf=_this->buf;
  248. cbuf=_this->cbuf;
  249. nbuf=_this->nbuf;
  250. if(nbuf>=cbuf){
  251. cbuf=(cbuf<<1)+2;
  252. buf=(unsigned short *)realloc(buf,cbuf*sizeof(*buf));
  253. if(buf==NULL)return -ENOMEM;
  254. }
  255. c+=16;
  256. m=(1<<c)-1;
  257. if(s>=8){
  258. buf[nbuf++]=(unsigned short)(l>>c);
  259. l&=m;
  260. c-=8;
  261. m>>=8;
  262. }
  263. buf[nbuf++]=(unsigned short)(l>>c);
  264. s=c+d-24;
  265. l&=m;
  266. _this->buf=buf;
  267. _this->cbuf=cbuf;
  268. _this->nbuf=nbuf;
  269. }
  270. _this->low=l<<d;
  271. _this->cnt=s;
  272. _this->rng=r<<d;
  273. return 0;
  274. }
  275. static int ec_enc_done(ec_enc *_this,unsigned char **_out){
  276. unsigned short *buf;
  277. size_t cbuf;
  278. size_t nbuf;
  279. unsigned char *out;
  280. unsigned mask;
  281. unsigned end;
  282. unsigned l;
  283. unsigned r;
  284. int c;
  285. int d;
  286. int s;
  287. size_t i;
  288. buf=_this->buf;
  289. cbuf=_this->cbuf;
  290. nbuf=_this->nbuf;
  291. l=_this->low;
  292. c=_this->cnt;
  293. r=_this->rng;
  294. /*Figure out the minimum number of bits that ensures that the symbols encoded
  295. thus far will be decoded correctly regardless of the bits that follow.*/
  296. d=0;
  297. mask=0x7FFF;
  298. end=l+mask&~mask;
  299. if((end|mask)>=l+r){
  300. d++;
  301. mask>>=1;
  302. end=l+mask&~mask;
  303. }
  304. /*Flush all remaining bits into the output buffer.*/
  305. s=c+d+8;
  306. if(s>=0){
  307. unsigned m;
  308. if(nbuf>=cbuf){
  309. cbuf=(cbuf<<1)+2;
  310. buf=(unsigned short *)realloc(buf,cbuf*sizeof(*buf));
  311. if(buf==NULL)return -ENOMEM;
  312. }
  313. m=(1<<c+16)-1;
  314. do{
  315. buf[nbuf++]=(unsigned short)(end>>c+16);
  316. end&=m;
  317. s-=8;
  318. c-=8;
  319. m>>=8;
  320. }
  321. while(s>=0);
  322. _this->buf=buf;
  323. _this->cbuf=cbuf;
  324. }
  325. /*Perform carry propagation.*/
  326. out=(unsigned char *)malloc(nbuf*sizeof(*out));
  327. if(out==NULL)return -ENOMEM;
  328. l=0;
  329. for(i=nbuf;i-->0;){
  330. l=buf[i]+l;
  331. out[i]=(unsigned char)l;
  332. l>>=8;
  333. }
  334. *_out=out;
  335. return nbuf;
  336. }
  337. static void ec_dec_init(ec_dec *_this,const unsigned char *_buf,size_t _sz){
  338. const unsigned char *end;
  339. ec_window dif;
  340. int c;
  341. int s;
  342. end=_buf+_sz;
  343. dif=0;
  344. c=-15;
  345. for(s=EC_WINDOW_SZ-9;s>=0;){
  346. if(_buf>=end){
  347. c=EC_LOTS_OF_BITS;
  348. break;
  349. }
  350. c+=8;
  351. dif|=(ec_window)*_buf++<<s;
  352. s-=8;
  353. }
  354. _this->buf=_buf;
  355. _this->end=end;
  356. _this->dif=dif;
  357. _this->cnt=c;
  358. _this->rng=0x8000;
  359. }
  360. static int ec_decode(ec_dec *_this,const unsigned short _f[16],unsigned _ftb){
  361. ec_window dif;
  362. unsigned r;
  363. unsigned d;
  364. int c;
  365. int s;
  366. unsigned u;
  367. unsigned v;
  368. unsigned q;
  369. int l;
  370. #if defined(EC_MULT_FREE)
  371. unsigned fl;
  372. unsigned fh;
  373. unsigned ft;
  374. #endif
  375. dif=_this->dif;
  376. c=_this->cnt;
  377. r=_this->rng;
  378. #if defined(EC_MULT_FREE)
  379. ft=1<<_ftb;
  380. s=r>=ft<<1;
  381. if(s)ft<<=1;
  382. d=r-ft;
  383. q=EC_MAXI((int)(dif>>EC_WINDOW_SZ-16+1),(int)((dif>>EC_WINDOW_SZ-16)-d))>>s;
  384. fl=0;
  385. for(l=0;_f[l]<=q;l++)fl=_f[l];
  386. fh=_f[l];
  387. /*printf("0x%08X %2i 0x%04X [0x%04X,0x%04X) {0x%04X}\n",
  388. (int)(_this->low>>EC_WINDOW_SZ-16),_this->cnt,_this->rng,fl,fh,1<<_ftb);*/
  389. fl<<=s;
  390. fh<<=s;
  391. u=fl+EC_MINI(fl,d);
  392. v=fh+EC_MINI(fh,d);
  393. #else
  394. q=(unsigned)(dif>>EC_WINDOW_SZ-16);
  395. u=0;
  396. v=_f[0]*r>>_ftb;
  397. for(l=0;v<=q;){
  398. u=v;
  399. v=_f[++l]*r>>_ftb;
  400. }
  401. #endif
  402. r=v-u;
  403. dif-=(ec_window)u<<EC_WINDOW_SZ-16;
  404. d=16-EC_ILOGNZ_32(r);
  405. s=c-d;
  406. if(s<0){
  407. const unsigned char *buf;
  408. const unsigned char *end;
  409. buf=_this->buf;
  410. end=_this->end;
  411. for(s=EC_WINDOW_SZ-9-(c+15);s>=0;){
  412. if(buf>=end){
  413. c=EC_LOTS_OF_BITS;
  414. break;
  415. }
  416. c+=8;
  417. dif|=(ec_window)*buf++<<s;
  418. s-=8;
  419. }
  420. s=c-d;
  421. _this->buf=buf;
  422. }
  423. _this->dif=dif<<d;
  424. _this->cnt=s;
  425. _this->rng=r<<d;
  426. return l;
  427. }
  428. #endif
  429. #if defined(EC_BINARY)
  430. static void ec_enc_bool_init(ec_enc *_this){
  431. _this->buf=NULL;
  432. _this->cbuf=0;
  433. _this->nbuf=0;
  434. _this->low=0;
  435. _this->cnt=-24;
  436. _this->rng=255;
  437. }
  438. static void ec_enc_bool_clear(ec_enc *_this){
  439. free(_this->buf);
  440. }
  441. static int ec_encode_bool(ec_enc *_this,int _b,unsigned _p){
  442. unsigned l;
  443. unsigned r;
  444. int c;
  445. unsigned s;
  446. l=_this->low;
  447. c=_this->cnt;
  448. r=_this->rng;
  449. s=1+((r-1)*_p>>8);
  450. /*printf("0x%08X %3i 0x%02X [0x%02X) %i {0x%02X}\n",
  451. _this->low,_this->cnt,_this->rng,s,_b,_p);*/
  452. r=_b?r-s:s;
  453. if(_b)l+=s;
  454. s=8-EC_ILOGNZ_32(r);
  455. r<<=s;
  456. c+=s;
  457. if(c>=0){
  458. unsigned short *buf;
  459. size_t cbuf;
  460. size_t nbuf;
  461. int o;
  462. buf=_this->buf;
  463. cbuf=_this->cbuf;
  464. nbuf=_this->nbuf;
  465. if(nbuf>=cbuf){
  466. cbuf=(cbuf<<1)+1;
  467. buf=(unsigned short *)realloc(buf,cbuf*sizeof(*buf));
  468. if(buf==NULL)return -ENOMEM;
  469. }
  470. o=s-c;
  471. buf[nbuf++]=(unsigned short)(l>>24-o);
  472. l=l<<o&0xFFFFFF;
  473. s=c;
  474. c-=8;
  475. _this->buf=buf;
  476. _this->cbuf=cbuf;
  477. _this->nbuf=nbuf;
  478. }
  479. _this->low=l<<s;
  480. _this->cnt=c;
  481. _this->rng=r;
  482. return 0;
  483. }
  484. static int ec_encode_bool_tree(ec_enc *_this,
  485. const signed char *_t,const unsigned char *_p,int _v,int _n){
  486. int b;
  487. int i;
  488. int ret;
  489. for(i=0;_n-->0;i=_t[2*i+b]){
  490. b=_v>>_n&1;
  491. ret=ec_encode_bool(_this,b,_p[i]);
  492. if(ret<0)break;
  493. }
  494. return ret;
  495. }
  496. static int ec_enc_bool_done(ec_enc *_this,unsigned char **_out){
  497. unsigned short *buf;
  498. size_t nbuf;
  499. unsigned char *out;
  500. unsigned l;
  501. size_t i;
  502. /*Flush all remaining bits into the output buffer.
  503. TODO: Flush minimum amount only; don't update state.
  504. For right now we are copying the VP8 approach.*/
  505. for(i=0;i<32;i++)ec_encode_bool(_this,0,128);
  506. buf=_this->buf;
  507. nbuf=_this->nbuf;
  508. /*Perform carry propagation.*/
  509. out=(unsigned char *)malloc(nbuf*sizeof(*out));
  510. if(out==NULL)return -ENOMEM;
  511. l=0;
  512. for(i=nbuf;i-->0;){
  513. l=buf[i]+l;
  514. out[i]=(unsigned char)l;
  515. l>>=8;
  516. }
  517. *_out=out;
  518. return nbuf;
  519. }
  520. static void ec_dec_bool_init(ec_dec *_this,
  521. const unsigned char *_buf,size_t _sz){
  522. const unsigned char *end;
  523. ec_window dif;
  524. int c;
  525. int s;
  526. end=_buf+_sz;
  527. dif=0;
  528. c=-8;
  529. for(s=EC_WINDOW_SZ-8;s>=0;){
  530. if(_buf>=end){
  531. c=EC_LOTS_OF_BITS;
  532. break;
  533. }
  534. c+=8;
  535. dif|=(ec_window)*_buf++<<s;
  536. s-=8;
  537. }
  538. _this->buf=_buf;
  539. _this->end=end;
  540. _this->dif=dif;
  541. _this->cnt=c;
  542. _this->rng=255;
  543. }
  544. static int ec_decode_bool(ec_dec *_this,unsigned _p){
  545. ec_window d;
  546. ec_window q;
  547. unsigned r;
  548. int c;
  549. int s;
  550. int v;
  551. d=_this->dif;
  552. c=_this->cnt;
  553. r=_this->rng;
  554. s=1+((r-1)*_p>>8);
  555. q=(ec_window)s<<EC_WINDOW_SZ-8;
  556. v=d>=q;
  557. /*printf("0x%08X %3i 0x%02X [0x%02X) %i {0x%02X}\n",
  558. (int)(_this->dif>>EC_WINDOW_SZ-8),_this->cnt,_this->rng,s,v,_p);*/
  559. r=v?r-s:s;
  560. if(v)d-=q;
  561. s=8-EC_ILOGNZ_32(r);
  562. r<<=s;
  563. d<<=s;
  564. c-=s;
  565. if(c<0){
  566. const unsigned char *buf;
  567. const unsigned char *end;
  568. buf=_this->buf;
  569. end=_this->end;
  570. for(s=EC_WINDOW_SZ-8-(c+8);s>=0;){
  571. if(buf>=end){
  572. c=EC_LOTS_OF_BITS;
  573. break;
  574. }
  575. c+=8;
  576. d|=(ec_window)*buf++<<s;
  577. s-=8;
  578. }
  579. _this->buf=buf;
  580. }
  581. _this->dif=d;
  582. _this->cnt=c;
  583. _this->rng=r;
  584. return v;
  585. }
  586. static int ec_decode_bool_tree(ec_dec *_this,
  587. const signed char *_t,const unsigned char *_p){
  588. int i;
  589. i=0;
  590. do i=_t[2*i+ec_decode_bool(_this,_p[i])];
  591. while(i>0);
  592. return -i-1;
  593. }
  594. #endif
  595. int main(void){
  596. ec_enc enc;
  597. ec_dec dec;
  598. unsigned char *out;
  599. int nout;
  600. int ei;
  601. int ret;
  602. int loop;
  603. while(entry_read());
  604. for(loop=0;loop<1024;loop++){
  605. #if defined(EC_MULTISYM)
  606. ec_enc_init(&enc);
  607. for(ei=0;ei<nentries;ei++){
  608. const unsigned short *f;
  609. int l;
  610. f=trees[entries[ei].tree].f;
  611. l=entries[ei].l;
  612. ec_encode(&enc,l>0?f[l-1]:0,f[l],EC_TREE_FTB);
  613. }
  614. nout=ec_enc_done(&enc,&out);
  615. if(nout<0)return EXIT_FAILURE;
  616. /*printf("Encoded to %u bytes.\n",nout);*/
  617. ec_dec_init(&dec,out,nout);
  618. ec_enc_clear(&enc);
  619. ret=EXIT_SUCCESS;
  620. for(ei=0;ei<nentries;ei++){
  621. const unsigned short *f;
  622. int l;
  623. f=trees[entries[ei].tree].f;
  624. l=ec_decode(&dec,f,EC_TREE_FTB);
  625. if(l!=entries[ei].l){
  626. printf("%i should be %i\n",l,entries[ei].l);
  627. ret=EXIT_FAILURE;
  628. }
  629. }
  630. free(out);
  631. #elif defined(EC_BINARY)
  632. ec_enc_bool_init(&enc);
  633. for(ei=0;ei<nentries;ei++){
  634. const signed char *t;
  635. const unsigned char *p;
  636. int v;
  637. int n;
  638. t=trees[entries[ei].tree].t;
  639. p=trees[entries[ei].tree].p;
  640. v=entries[ei].v;
  641. n=entries[ei].n;
  642. ec_encode_bool_tree(&enc,t,p,v,n);
  643. }
  644. nout=ec_enc_bool_done(&enc,&out);
  645. if(nout<0)return EXIT_FAILURE;
  646. /*printf("Encoded to %u bytes.\n",nout);*/
  647. ec_dec_bool_init(&dec,out,nout);
  648. ec_enc_bool_clear(&enc);
  649. ret=EXIT_SUCCESS;
  650. for(ei=0;ei<nentries;ei++){
  651. const signed char *t;
  652. const unsigned char *p;
  653. int l;
  654. t=trees[entries[ei].tree].t;
  655. p=trees[entries[ei].tree].p;
  656. l=ec_decode_bool_tree(&dec,t,p);
  657. if(l!=entries[ei].l){
  658. printf("%i should be %i\n",l,entries[ei].l);
  659. ret=EXIT_FAILURE;
  660. }
  661. }
  662. free(out);
  663. #else
  664. # error "Please define EC_MULTISYM or EC_BINARY"
  665. #endif
  666. }
  667. return ret;
  668. }