Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

806 lines
17 KiB

  1. /* File: sv_h261_huffman.c */
  2. /*****************************************************************************
  3. ** Copyright (c) Digital Equipment Corporation, 1995, 1997 **
  4. ** **
  5. ** All Rights Reserved. Unpublished rights reserved under the copyright **
  6. ** laws of the United States. **
  7. ** **
  8. ** The software contained on this media is proprietary to and embodies **
  9. ** the confidential technology of Digital Equipment Corporation. **
  10. ** Possession, use, duplication or dissemination of the software and **
  11. ** media is authorized only pursuant to a valid written license from **
  12. ** Digital Equipment Corporation. **
  13. ** **
  14. ** RESTRICTED RIGHTS LEGEND Use, duplication, or disclosure by the U.S. **
  15. ** Government is subject to restrictions as set forth in Subparagraph **
  16. ** (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, as applicable. **
  17. ******************************************************************************/
  18. /*************************************************************
  19. This file contains the Huffman routines.
  20. *************************************************************/
  21. /*
  22. #define _SLIBDEBUG_
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "SV.h"
  28. #include "sv_intrn.h"
  29. #include "sv_h261.h"
  30. #include "proto.h"
  31. #ifdef _SLIBDEBUG_
  32. #define _DEBUG_ 0 /* detailed debuging statements */
  33. #define _VERBOSE_ 0 /* show progress */
  34. #define _VERIFY_ 1 /* verify correct operation */
  35. #define _WARN_ 0 /* warnings about strange behavior */
  36. #endif
  37. static DHUFF *MakeDhuff();
  38. static EHUFF *MakeEhuff(int n);
  39. static void LoadETable(int *array, EHUFF *table);
  40. static void LoadDTable(int *array, DHUFF *table);
  41. static int GetNextState(DHUFF *huff);
  42. static void DestroyDhuff(DHUFF **huff);
  43. static void DestroyEhuff(EHUFF **huff);
  44. static void AddCode(int n, int code, int value, DHUFF *huff);
  45. /* Actual Tables */
  46. #define GetLeft(sval,huff) ((huff->state[(sval)] >> 16) & 0xffff)
  47. #define GetRight(sval,huff) (huff->state[(sval)] & 0xffff)
  48. #define SetLeft(number,sval,huff) huff->state[(sval)]=\
  49. (((huff->state[(sval)]) & 0xffff)|(number<<16));
  50. #define SetRight(number,sval,huff) huff->state[(sval)]=\
  51. (((huff->state[(sval)]) & 0xffff0000)|(number));
  52. #define EmptyState 0xffff
  53. #define Numberp(value) ((value & 0x8000) ? 1 : 0)
  54. #define MakeHVal(value) (value | 0x8000)
  55. #define GetHVal(value) (value & 0x7fff)
  56. int MTypeCoeff[] = {
  57. 0,4,1,
  58. 1,7,1,
  59. 2,1,1,
  60. 3,5,1,
  61. 4,9,1,
  62. 5,8,1,
  63. 6,10,1,
  64. 7,3,1,
  65. 8,2,1,
  66. 9,6,1,
  67. -1,-1};
  68. int MBACoeff[] = {
  69. 1,1,1,
  70. 2,3,3,
  71. 3,3,2,
  72. 4,4,3,
  73. 5,4,2,
  74. 6,5,3,
  75. 7,5,2,
  76. 8,7,7,
  77. 9,7,6,
  78. 10,8,11,
  79. 11,8,10,
  80. 12,8,9,
  81. 13,8,8,
  82. 14,8,7,
  83. 15,8,6,
  84. 16,10,23,
  85. 17,10,22,
  86. 18,10,21,
  87. 19,10,20,
  88. 20,10,19,
  89. 21,10,18,
  90. 22,11,35,
  91. 23,11,34,
  92. 24,11,33,
  93. 25,11,32,
  94. 26,11,31,
  95. 27,11,30,
  96. 28,11,29,
  97. 29,11,28,
  98. 30,11,27,
  99. 31,11,26,
  100. 32,11,25,
  101. 33,11,24,
  102. 34,11,15, /* Stuffing */
  103. 35,16,1, /* Start */
  104. -1,-1};
  105. int MVDCoeff[] = {
  106. 16,11,25,
  107. 17,11,27,
  108. 18,11,29,
  109. 19,11,31,
  110. 20,11,33,
  111. 21,11,35,
  112. 22,10,19,
  113. 23,10,21,
  114. 24,10,23,
  115. 25,8,7,
  116. 26,8,9,
  117. 27,8,11,
  118. 28,7,7,
  119. 29,5,3,
  120. 30,4,3,
  121. 31,3,3,
  122. 0,1,1,
  123. 1,3,2,
  124. 2,4,2,
  125. 3,5,2,
  126. 4,7,6,
  127. 5,8,10,
  128. 6,8,8,
  129. 7,8,6,
  130. 8,10,22,
  131. 9,10,20,
  132. 10,10,18,
  133. 11,11,34,
  134. 12,11,32,
  135. 13,11,30,
  136. 14,11,28,
  137. 15,11,26,
  138. -1,-1};
  139. int CBPCoeff[] = {
  140. 60,3,7,
  141. 4,4,13,
  142. 8,4,12,
  143. 16,4,11,
  144. 32,4,10,
  145. 12,5,19,
  146. 48,5,18,
  147. 20,5,17,
  148. 40,5,16,
  149. 28,5,15,
  150. 44,5,14,
  151. 52,5,13,
  152. 56,5,12,
  153. 1,5,11,
  154. 61,5,10,
  155. 2,5,9,
  156. 62,5,8,
  157. 24,6,15,
  158. 36,6,14,
  159. 3,6,13,
  160. 63,6,12,
  161. 5,7,23,
  162. 9,7,22,
  163. 17,7,21,
  164. 33,7,20,
  165. 6,7,19,
  166. 10,7,18,
  167. 18,7,17,
  168. 34,7,16,
  169. 7,8,31,
  170. 11,8,30,
  171. 19,8,29,
  172. 35,8,28,
  173. 13,8,27,
  174. 49,8,26,
  175. 21,8,25,
  176. 41,8,24,
  177. 14,8,23,
  178. 50,8,22,
  179. 22,8,21,
  180. 42,8,20,
  181. 15,8,19,
  182. 51,8,18,
  183. 23,8,17,
  184. 43,8,16,
  185. 25,8,15,
  186. 37,8,14,
  187. 26,8,13,
  188. 38,8,12,
  189. 29,8,11,
  190. 45,8,10,
  191. 53,8,9,
  192. 57,8,8,
  193. 30,8,7,
  194. 46,8,6,
  195. 54,8,5,
  196. 58,8,4,
  197. 31,9,7,
  198. 47,9,6,
  199. 55,9,5,
  200. 59,9,4,
  201. 27,9,3,
  202. 39,9,2,
  203. -1,-1};
  204. int TCoeff1[] = {
  205. 0,2,2, /* EOF */
  206. 1,2,3, /* Not First Coef */
  207. 2,4,4,
  208. 3,5,5,
  209. 4,7,6,
  210. 5,8,38,
  211. 6,8,33,
  212. 7,10,10,
  213. 8,12,29,
  214. 9,12,24,
  215. 10,12,19,
  216. 11,12,16,
  217. 12,13,26,
  218. 13,13,25,
  219. 14,13,24,
  220. 15,13,23,
  221. 257,3,3,
  222. 258,6,6,
  223. 259,8,37,
  224. 260,10,12,
  225. 261,12,27,
  226. 262,13,22,
  227. 263,13,21,
  228. 513,4,5,
  229. 514,7,4,
  230. 515,10,11,
  231. 516,12,20,
  232. 517,13,20,
  233. 769,5,7,
  234. 770,8,36,
  235. 771,12,28,
  236. 772,13,19,
  237. 1025,5,6,
  238. 1026,10,15,
  239. 1027,12,18,
  240. 1281,6,7,
  241. 1282,10,9,
  242. 1283,13,18,
  243. 1537,6,5,
  244. 1538,12,30,
  245. 1793,6,4,
  246. 1794,12,21,
  247. 2049,7,7,
  248. 2050,12,17,
  249. 2305,7,5,
  250. 2306,13,17,
  251. 2561,8,39,
  252. 2562,13,16,
  253. 2817,8,35,
  254. 3073,8,34,
  255. 3329,8,32,
  256. 3585,10,14,
  257. 3841,10,13,
  258. 4097,10,8,
  259. 4353,12,31,
  260. 4609,12,26,
  261. 4865,12,25,
  262. 5121,12,23,
  263. 5377,12,22,
  264. 5633,13,31,
  265. 5889,13,30,
  266. 6145,13,29,
  267. 6401,13,28,
  268. 6657,13,27,
  269. 6913,6,1, /* Escape */
  270. -1,-1
  271. };
  272. /* Excludes EOB */
  273. int TCoeff2[] = {
  274. 1,1,1, /* First Coef */
  275. 2,4,4,
  276. 3,5,5,
  277. 4,7,6,
  278. 5,8,38,
  279. 6,8,33,
  280. 7,10,10,
  281. 8,12,29,
  282. 9,12,24,
  283. 10,12,19,
  284. 11,12,16,
  285. 12,13,26,
  286. 13,13,25,
  287. 14,13,24,
  288. 15,13,23,
  289. 257,3,3,
  290. 258,6,6,
  291. 259,8,37,
  292. 260,10,12,
  293. 261,12,27,
  294. 262,13,22,
  295. 263,13,21,
  296. 513,4,5,
  297. 514,7,4,
  298. 515,10,11,
  299. 516,12,20,
  300. 517,13,20,
  301. 769,5,7,
  302. 770,8,36,
  303. 771,12,28,
  304. 772,13,19,
  305. 1025,5,6,
  306. 1026,10,15,
  307. 1027,12,18,
  308. 1281,6,7,
  309. 1282,10,9,
  310. 1283,13,18,
  311. 1537,6,5,
  312. 1538,12,30,
  313. 1793,6,4,
  314. 1794,12,21,
  315. 2049,7,7,
  316. 2050,12,17,
  317. 2305,7,5,
  318. 2306,13,17,
  319. 2561,8,39,
  320. 2562,13,16,
  321. 2817,8,35,
  322. 3073,8,34,
  323. 3329,8,32,
  324. 3585,10,14,
  325. 3841,10,13,
  326. 4097,10,8,
  327. 4353,12,31,
  328. 4609,12,26,
  329. 4865,12,25,
  330. 5121,12,23,
  331. 5377,12,22,
  332. 5633,13,31,
  333. 5889,13,30,
  334. 6145,13,29,
  335. 6401,13,28,
  336. 6657,13,27,
  337. 6913,6,1, /* Escape */
  338. -1,-1
  339. };
  340. /*
  341. * Function: inithuff()
  342. * Purpose: Initializes all of the Huffman structures to the
  343. * appropriate values. It must be called before any of
  344. * the tables are used.
  345. */
  346. void sv_H261HuffInit(SvH261Info_t *H261)
  347. {
  348. H261->NumberBitsCoded = 0;
  349. H261->MBADHuff = MakeDhuff();
  350. H261->MVDDHuff = MakeDhuff();
  351. H261->CBPDHuff = MakeDhuff();
  352. H261->T1DHuff = MakeDhuff();
  353. H261->T2DHuff = MakeDhuff();
  354. H261->T3DHuff = MakeDhuff();
  355. H261->MBAEHuff = MakeEhuff(40);
  356. H261->MVDEHuff = MakeEhuff(40);
  357. H261->CBPEHuff = MakeEhuff(70);
  358. H261->T1EHuff = MakeEhuff(8192);
  359. H261->T2EHuff = MakeEhuff(8192);
  360. H261->T3EHuff = MakeEhuff(20);
  361. LoadDTable(MBACoeff,H261->MBADHuff);
  362. LoadETable(MBACoeff,H261->MBAEHuff);
  363. LoadDTable(MVDCoeff,H261->MVDDHuff);
  364. LoadETable(MVDCoeff,H261->MVDEHuff);
  365. LoadDTable(CBPCoeff,H261->CBPDHuff);
  366. LoadETable(CBPCoeff,H261->CBPEHuff);
  367. LoadDTable(TCoeff1,H261->T1DHuff);
  368. LoadETable(TCoeff1,H261->T1EHuff);
  369. LoadDTable(TCoeff2,H261->T2DHuff);
  370. LoadETable(TCoeff2,H261->T2EHuff);
  371. LoadDTable(MTypeCoeff,H261->T3DHuff);
  372. LoadETable(MTypeCoeff,H261->T3EHuff);
  373. }
  374. /*
  375. * Function: freehuff()
  376. * Purpose: Frees all memory allocated for the Huffman structures.
  377. */
  378. void sv_H261HuffFree(SvH261Info_t *H261)
  379. {
  380. DestroyDhuff(&H261->MBADHuff);
  381. DestroyDhuff(&H261->MVDDHuff);
  382. DestroyDhuff(&H261->CBPDHuff);
  383. DestroyDhuff(&H261->T1DHuff);
  384. DestroyDhuff(&H261->T2DHuff);
  385. DestroyDhuff(&H261->T3DHuff);
  386. DestroyEhuff(&H261->MBAEHuff);
  387. DestroyEhuff(&H261->MVDEHuff);
  388. DestroyEhuff(&H261->CBPEHuff);
  389. DestroyEhuff(&H261->T1EHuff);
  390. DestroyEhuff(&H261->T2EHuff);
  391. DestroyEhuff(&H261->T3EHuff);
  392. }
  393. /*
  394. ** Function: MakeDhuff()
  395. ** Purpose: Constructs a decoder Huffman table and returns
  396. ** the structure.
  397. */
  398. static DHUFF *MakeDhuff()
  399. {
  400. int i;
  401. DHUFF *temp;
  402. _SlibDebug(_DEBUG_, printf("MakeDhuff()\n") );
  403. temp = (DHUFF *)ScAlloc(sizeof(DHUFF));
  404. temp->NumberStates=1;
  405. for(i=0; i<512; i++)
  406. temp->state[i] = -1;
  407. return(temp);
  408. }
  409. static void DestroyDhuff(DHUFF **huff)
  410. {
  411. if (huff)
  412. {
  413. ScFree(*huff);
  414. *huff=NULL;
  415. }
  416. }
  417. /*
  418. ** Function: MakeEhuff()
  419. ** Purpose: Constructs an encoder huff with a designated table-size.
  420. ** This table-size, n, is used for the lookup of Huffman values,
  421. ** and must represent the largest positive Huffman value.
  422. */
  423. static EHUFF *MakeEhuff(int n)
  424. {
  425. int i;
  426. EHUFF *temp;
  427. _SlibDebug(_DEBUG_, printf("MakeEhuff()\n") );
  428. temp = (EHUFF *)ScAlloc(sizeof(EHUFF));
  429. temp->n = n;
  430. temp->Hlen = (int *)ScAlloc(n*sizeof(int));
  431. temp->Hcode = (int *)ScAlloc(n*sizeof(int));
  432. for(i=0; i<n; i++)
  433. {
  434. temp->Hlen[i] = -1;
  435. temp->Hcode[i] = -1;
  436. }
  437. return(temp);
  438. }
  439. static void DestroyEhuff(EHUFF **huff)
  440. {
  441. if (huff)
  442. {
  443. if ((*huff)->Hlen)
  444. ScFree((*huff)->Hlen);
  445. if ((*huff)->Hcode)
  446. ScFree((*huff)->Hcode);
  447. ScFree(*huff);
  448. *huff=NULL;
  449. }
  450. }
  451. /*
  452. ** Function: LoadETable()
  453. ** Purpose: Used to load an array into an encoder table. The
  454. ** array is grouped in triplets and the first negative value
  455. ** signals the end of the table.
  456. */
  457. static void LoadETable(int *array, EHUFF *table)
  458. {
  459. _SlibDebug(_DEBUG_, printf("LoadETable()\n") );
  460. while(*array>=0)
  461. {
  462. if (*array>table->n)
  463. {
  464. printf("Table overflow.\n");
  465. }
  466. table->Hlen[*array] = array[1];
  467. table->Hcode[*array] = (int )array[2];
  468. array+=3;
  469. }
  470. }
  471. /*
  472. ** Function: LoadDHUFF()
  473. ** Purpose: Used to load an array into the DHUFF structure. The
  474. ** array consists of trios of Huffman definitions, the
  475. ** first one the value, the next one the size, and the
  476. ** third one the code.
  477. */
  478. static void LoadDTable(int *array, DHUFF *table)
  479. {
  480. _SlibDebug(_DEBUG_, printf("LoadDTable()\n") );
  481. while(*array>=0)
  482. {
  483. AddCode(array[1],array[2],array[0],table);
  484. array+=3;
  485. }
  486. }
  487. /*
  488. ** Function: GetNextState()
  489. ** Returns the next free state of the decoder Huffman
  490. ** structure. It no longer exits an error upon overflow.
  491. */
  492. static int GetNextState(DHUFF *huff)
  493. {
  494. _SlibDebug(_DEBUG_, printf("GetNextState()\n") );
  495. /*
  496. if (huff->NumberStates==512)
  497. {
  498. _SlibDebug(_DEBUG_, printf("Overflow\n") );
  499. exit(ERROR_BOUNDS);
  500. }
  501. */
  502. return(huff->NumberStates++);
  503. }
  504. /*
  505. ** Function: sv_H261HuffEncode()
  506. ** Purpose: Encodes a value according to a designated encoder Huffman
  507. ** table out to the stream. It returns the number of bits
  508. ** written to the stream and a zero on error.
  509. */
  510. int sv_H261HuffEncode(SvH261Info_t *H261, ScBitstream_t *bs, int val, EHUFF *huff)
  511. {
  512. _SlibDebug(_DEBUG_, printf("Encode(val=%d)\n", val) );
  513. if (val < 0)
  514. {
  515. _SlibDebug(_DEBUG_, printf("Encode() Out of bounds val: %d.\n",val) );
  516. return(0);
  517. }
  518. else if (val>=huff->n)
  519. return(0); /* No serious error, can occur with some values */
  520. else if (huff->Hlen[val]<0)
  521. return(0); /* No serious error: can pass thru by alerting routine.*/
  522. else
  523. {
  524. _SlibDebug(_DEBUG_,
  525. printf("Encode() Value: %d|%x Length: %d Code: %d\n",
  526. val,val,huff->Hlen[val],huff->Hcode[val]) );
  527. H261->NumberBitsCoded+=huff->Hlen[val];
  528. ScBSPutBits(bs, huff->Hcode[val], huff->Hlen[val]);
  529. return(huff->Hlen[val]);
  530. }
  531. }
  532. /*
  533. ** Function: sv_H261HuffDecode()
  534. ** Purpose: Returns an integer read off the stream using the designated
  535. ** Huffman structure.
  536. */
  537. #if 1
  538. int sv_H261HuffDecode(SvH261Info_t *H261, ScBitstream_t *bs, DHUFF *huff)
  539. {
  540. register int State=0, bits;
  541. register unsigned short cb;
  542. _SlibDebug(_DEBUG_, printf("Decode()\n") );
  543. cb = (unsigned short)ScBSPeekBits(bs, 16);
  544. if (bs->EOI)
  545. return(0);
  546. if ((State = nextstate(huff, State, 0x8000)) & 0x8000) {
  547. bits=1;
  548. State = (State == 0xffff) ? 0 : State & 0x7fff;
  549. } else if ((State = nextstate(huff, State, 0x4000)) & 0x8000) {
  550. bits=2;
  551. State = (State == 0xffff) ? 0 : State & 0x7fff;
  552. } else if ((State = nextstate(huff, State, 0x2000)) & 0x8000) {
  553. bits=3;
  554. State = (State == 0xffff) ? 0 : State & 0x7fff;
  555. } else if ((State = nextstate(huff, State, 0x1000)) & 0x8000) {
  556. bits=4;
  557. State = (State == 0xffff) ? 0 : State & 0x7fff;
  558. } else if ((State = nextstate(huff, State, 0x0800)) & 0x8000) {
  559. bits=5;
  560. State = (State == 0xffff) ? 0 : State & 0x7fff;
  561. } else if ((State = nextstate(huff, State, 0x0400)) & 0x8000) {
  562. bits=6;
  563. State = (State == 0xffff) ? 0 : State & 0x7fff;
  564. } else if ((State = nextstate(huff, State, 0x0200)) & 0x8000) {
  565. bits=7;
  566. State = (State == 0xffff) ? 0 : State & 0x7fff;
  567. } else if ((State = nextstate(huff, State, 0x0100)) & 0x8000) {
  568. bits=8;
  569. State = (State == 0xffff) ? 0 : State & 0x7fff;
  570. } else if ((State = nextstate(huff, State, 0x0080)) & 0x8000) {
  571. bits=9;
  572. State = (State == 0xffff) ? 0 : State & 0x7fff;
  573. } else if ((State = nextstate(huff, State, 0x0040)) & 0x8000) {
  574. bits=10;
  575. State = (State == 0xffff) ? 0 : State & 0x7fff;
  576. } else if ((State = nextstate(huff, State, 0x0020)) & 0x8000) {
  577. bits=11;
  578. State = (State == 0xffff) ? 0 : State & 0x7fff;
  579. } else if ((State = nextstate(huff, State, 0x0010)) & 0x8000) {
  580. bits=12;
  581. State = (State == 0xffff) ? 0 : State & 0x7fff;
  582. } else if ((State = nextstate(huff, State, 0x0008)) & 0x8000) {
  583. bits=13;
  584. State = (State == 0xffff) ? 0 : State & 0x7fff;
  585. } else if ((State = nextstate(huff, State, 0x0004)) & 0x8000) {
  586. bits=14;
  587. State = (State == 0xffff) ? 0 : State & 0x7fff;
  588. } else if ((State = nextstate(huff, State, 0x0002)) & 0x8000) {
  589. bits=15;
  590. State = (State == 0xffff) ? 0 : State & 0x7fff;
  591. } else if ((State = nextstate(huff, State, 0x0001)) & 0x8000) {
  592. bits=16;
  593. State = (State == 0xffff) ? 0 : State & 0x7fff;
  594. }
  595. ScBSSkipBits(bs, bits);
  596. return(State);
  597. }
  598. #else
  599. int sv_H261HuffDecode(SvH261Info_t *H261, ScBitstream_t *bs, DHUFF *huff)
  600. {
  601. int Next,cb;
  602. int CurrentState=0;
  603. _SlibDebug(_DEBUG_, printf("Decode()\n") );
  604. while(1)
  605. {
  606. cb = ScBSGetBit(bs);
  607. if (bs->EOI)
  608. return(0);
  609. Next = cb ? GetLeft(CurrentState,huff) : GetRight(CurrentState,huff);
  610. if (Next == EmptyState)
  611. return(0);
  612. else if (Numberp(Next))
  613. return(GetHVal(Next));
  614. else
  615. CurrentState = Next;
  616. }
  617. }
  618. #endif
  619. /*
  620. ** Function: AddCode()
  621. ** Purpose: Adds a Huffman code to the decoder structure. It is called
  622. ** everytime a new Huffman code is to be defined. This function
  623. ** exits when an invalid code is attempted to be placed in
  624. ** the structure.
  625. */
  626. static void AddCode(int n, int code, int value, DHUFF *huff)
  627. {
  628. int i,Next;
  629. int CurrentState=0;
  630. _SlibDebug(_DEBUG_, printf("AddCode()\n") );
  631. if (value < 0)
  632. return;
  633. for(i=n-1;i>0;i--)
  634. {
  635. if (code & (1 << i))
  636. {
  637. Next = GetLeft(CurrentState,huff);
  638. if (Next == EmptyState)
  639. {
  640. Next = GetNextState(huff);
  641. SetLeft(Next,CurrentState,huff);
  642. CurrentState = Next;
  643. }
  644. else /* if (Numberp(Next))
  645. {
  646. printf("Bad Value/State match:\n");
  647. printf("Length: %d Code: %d Value: %d\n",
  648. n,code,value);
  649. exit(ERROR_BOUNDS);
  650. }
  651. else
  652. */
  653. {
  654. CurrentState = Next;
  655. }
  656. }
  657. else
  658. {
  659. Next = GetRight(CurrentState,huff);
  660. if (Next == EmptyState)
  661. {
  662. Next = GetNextState(huff);
  663. SetRight(Next,CurrentState,huff);
  664. CurrentState = Next;
  665. }
  666. else /* if (Numberp(Next))
  667. {
  668. printf("Bad Value/State match:\n");
  669. printf("Length: %d Code: %d Value: %d\n",
  670. n,code,value);
  671. exit(ERROR_BOUNDS);
  672. }
  673. else
  674. */
  675. {
  676. CurrentState = Next;
  677. }
  678. }
  679. }
  680. if (code & 1)
  681. {
  682. Next = GetLeft(CurrentState,huff);
  683. /* if (Next != EmptyState)
  684. {
  685. printf("Overflow on Huffman Table: Nonunique prefix.\n");
  686. printf("Length: %d Code: %d|%x Value: %d|%x\n",
  687. n,code,code,value,value);
  688. exit(ERROR_BOUNDS);
  689. }
  690. */
  691. SetLeft(MakeHVal(value),CurrentState,huff);
  692. }
  693. else
  694. {
  695. Next = GetRight(CurrentState,huff);
  696. /* if (Next != EmptyState)
  697. {
  698. printf("Overflow on Huffman Table: Nonunique prefix.\n");
  699. printf("Length: %d Code: %d|%x Value: %d|%x\n",
  700. n,code,code,value,value);
  701. exit(ERROR_BOUNDS);
  702. }
  703. */
  704. SetRight(MakeHVal(value),CurrentState,huff);
  705. }
  706. }
  707. /*
  708. ** Function: PrintDHUFF()
  709. ** Purpose: Prints out the decoder Huffman structure that is passed
  710. ** into it.
  711. */
  712. void PrintDhuff(DHUFF *huff)
  713. {
  714. int i;
  715. printf("Modified Huffman Decoding Structure: %p\n",huff);
  716. printf("Number of states %d\n",huff->NumberStates);
  717. for(i=0;i<huff->NumberStates;i++)
  718. {
  719. printf("State: %d Left State: %x Right State: %x\n",
  720. i,
  721. GetLeft(i,huff),
  722. GetRight(i,huff));
  723. }
  724. }
  725. /*
  726. ** Function: PrintEhuff()
  727. ** Purpose: Prints the encoder Huffman structure passed into it.
  728. */
  729. void PrintEhuff(EHUFF *huff)
  730. {
  731. BEGIN("PrintEhuff");
  732. int i;
  733. printf("Modified Huffman Encoding Structure: %p\n",huff);
  734. printf("Number of values %d\n",huff->n);
  735. for(i=0;i<huff->n;i++)
  736. {
  737. if (huff->Hlen[i]>=0)
  738. {
  739. printf("Value: %x Length: %d Code: %x\n",
  740. i,huff->Hlen[i],huff->Hcode[i]);
  741. }
  742. }
  743. }
  744. /*
  745. ** Function: PrintTable()
  746. ** Purpose: Prints out 256 elements in a nice byte ordered fashion.
  747. */
  748. void PrintTable(int *table)
  749. {
  750. int i,j;
  751. for(i=0;i<16;i++)
  752. {
  753. for(j=0;j<16;j++)
  754. {
  755. printf("%2x ",*(table++));
  756. }
  757. printf("\n");
  758. }
  759. }
  760.