iljpgdehuff.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these librararies and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $TOG: iljpgdehuff.c /main/5 1999/10/14 13:19:16 mgreess $ */
  24. /**---------------------------------------------------------------------
  25. ***
  26. *** (c)Copyright 1992 Hewlett-Packard Co.
  27. ***
  28. *** RESTRICTED RIGHTS LEGEND
  29. *** Use, duplication, or disclosure by the U.S. Government is subject to
  30. *** restrictions as set forth in sub-paragraph (c)(1)(ii) of the Rights in
  31. *** Technical Data and Computer Software clause in DFARS 252.227-7013.
  32. *** Hewlett-Packard Company
  33. *** 3000 Hanover Street
  34. *** Palo Alto, CA 94304 U.S.A.
  35. *** Rights for non-DOD U.S. Government Departments and Agencies are as set
  36. *** forth in FAR 52.227-19(c)(1,2).
  37. ***
  38. ***-------------------------------------------------------------------*/
  39. #include <stdlib.h>
  40. #include "iljpgdecodeint.h"
  41. /*
  42. utilities for fast table-based huffman decoder
  43. NOTES
  44. Author: G. Seroussi, HPL, PaloAlto. May 21, 1992
  45. Modifications: V. Bhaskaran, HPL, PaloAlto. May 29, 1992.
  46. email: bhaskara@hplvab.hpl.hp.com
  47. */
  48. typedef struct tree_node {
  49. int c;
  50. struct tree_node *left;
  51. struct tree_node *right;
  52. } tree_node;
  53. typedef int LOOKUP;
  54. struct table_set {
  55. LOOKUP *lookup_symb;
  56. LOOKUP *lookup_len;
  57. tree_node *tree_nodes;
  58. };
  59. typedef unsigned int BITBUF;
  60. #define LOOKUP_BITS 9
  61. #define FOUND_EOI 0x80
  62. #define ABSIZE 256 /* max size of alphabet for Huffman encoding */
  63. #define NOLEN 0
  64. #define BLOCK_COEFFS 64 /* number of coefficients per block */
  65. #define NOLEAF (BITBUF)(-1)
  66. #define BITBUFSIZE (8*sizeof(BITBUF))
  67. #define SHIFTBUF ( BITBUFSIZE - 8 )
  68. #define BUFMSB ( ((BITBUF) 1) << ( BITBUFSIZE - 1 ) )
  69. /* Data private to this file. Pointed to by iljpgDecodePrivRec.pHuffPriv */
  70. typedef struct {
  71. unsigned int bitbuf;
  72. int bitsleft;
  73. int resetDone;
  74. struct table_set table_set_dc[4];
  75. struct table_set table_set_ac[4];
  76. } iljpgDehuffRec, *iljpgDehuffPtr;
  77. /* Characteristc vector of the upper 4x4 block in zig-zag order */
  78. static int fourx4_table[] =
  79. { 1, 1, 1, 1, 1, 1, 1, 1,
  80. 1, 1, 0, 1, 1, 1, 0, 0,
  81. 0, 1, 1, 0, 0, 0, 0, 0,
  82. 1, 0, 0, 0, 0, 0, 0, 0,
  83. 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, 0, 0, 0, 0, 0, 0, 0,
  85. 0, 0, 0, 0, 0, 0, 0, 0,
  86. 0, 0, 0, 0, 0, 0, 0, 0
  87. };
  88. /* Build a lookup table and a partial binary tree for the Huffman dictionary.
  89. Return absize of table; = 0 if tables are invalid.
  90. */
  91. /* The lookup tables are used by the Huffman decoding engine as follows:
  92. a fixed number N of bits are read from the encoded stream (N is
  93. given by the "lookup_bits" parameter). Let P denote the binary
  94. number formed by the N bits read.
  95. P is used as an index into two lookup tables: lookup_symb[]
  96. and lookup_len[]. If P uniquely identifies a Huffman code (i.e. if its
  97. N-bit pattern contains a Huffman code as a prefix), then the encoded
  98. symbol is given by lookup_symb[P], and the length of the corresponding
  99. Huffman code is given by lookup_len[P]. The latter is used to determine
  100. how many bits from the input stream were "used".
  101. If P does not uniquely identify a Huffman code (i.e. it is a prefix of
  102. a Huffman code), then lookup_len[P] = NOLEN (currently set to 0),
  103. and lookup_symb[P] is an index into an array of tree nodes, giving the
  104. Huffman subtree determined by the prefix P. The rest of the Huffman
  105. code is obtained by traversing this subtree according to the input bits
  106. following P in the encoded stream.
  107. Notice that a given Huffman code may have many entries in the lookup
  108. tables. For example, if N=9 and 01 is a Huffman code,
  109. then all the entries with indices 01XXXXXXX will contain the same
  110. symbol (the one represented by the code 01) and length.
  111. */
  112. static int build_intermediate_tables (
  113. iljpgPtr bits,
  114. iljpgPtr vals,
  115. int max_absize,
  116. int *hufvals, /* must be of length max_absize+1; all zeros */
  117. int *hufcodes, /* ditto */
  118. int *huflen /* ditto */
  119. )
  120. {
  121. long int i, j, k, code, si;
  122. int absize;
  123. int length, sumLengths;
  124. /* generate code lengths: return 0 (error) if sum > 256 */
  125. for (i = 0, k = 0, sumLengths = 0; i < 16; i++) {
  126. length = bits[i];
  127. if ((sumLengths += length) > max_absize)
  128. return 0; /* table too long; return error */
  129. for (j = 0; j < length; j++)
  130. huflen[k++] = i+1;
  131. }
  132. /* generate values until huflen[i] == 0 - at least one is, because array
  133. is "max_absize+1" entries long and zeroed, and above set at most max_absize.
  134. */
  135. for ( i=0; huflen[i]; i++ )
  136. hufvals[i] = vals[i];
  137. absize = i; /* actual number of Huffman-encoded symbols */
  138. /* now generate codes using the JPEG document procedure */
  139. k=0;
  140. code = 0;
  141. si = huflen[0];
  142. while (1) {
  143. do {
  144. hufcodes[k] = code;
  145. code ++;
  146. k ++;
  147. }
  148. while ( huflen[k] == si );
  149. if ( huflen[k] == 0 )
  150. break; /* done */
  151. do {
  152. code <<= 1;
  153. si ++;
  154. }
  155. while ( huflen[k] != si );
  156. }
  157. return absize;
  158. }
  159. static iljpgError build_huffman_tables (
  160. iljpgPtr bits, /* "Bits" table as specified in the JPEG standard */
  161. iljpgPtr vals, /* "Vals" table as specified in the JPEG standard */
  162. int lookup_bits, /* # of Huffman bits that are looked up in the tables */
  163. struct table_set *table_setp /* pointer to a struct of pointers to lookup tables */
  164. )
  165. {
  166. int next_node = 0, /* index to next available tree node */
  167. c, len, s, i, j, n;
  168. long numnodes, /* max number nodes in Huffman tree */
  169. lookup_index,
  170. lookup_size,
  171. code, mask;
  172. tree_node *tree_nodes, /* pointer to pool of tree nodes */
  173. *nodep, *leafp, **followp; /* misc aux. tree pointers */
  174. LOOKUP *lookup_symb, *lookup_len; /* pointers to lookup tables */
  175. int absize; /* actual number of Huffman-encoded symbols */
  176. int *hufvals, *hufcodes, *huflen; /* intermediate tables */
  177. iljpgError error;
  178. /* Init all ptrs to allocated data to null. If any allocation fails
  179. deallocate all and return malloc error.
  180. */
  181. hufvals = hufcodes = huflen = (int *)NULL;
  182. lookup_symb = lookup_len = (LOOKUP *)NULL;
  183. tree_nodes = (tree_node *)NULL;
  184. error = ILJPG_ERROR_DECODE_MALLOC; /* assumed malloc error for now */
  185. if (!(hufvals = (int *)ILJPG_MALLOC_ZERO ((ABSIZE+1) * sizeof(int))))
  186. goto BuildTablesError;
  187. if (!(hufcodes = (int *)ILJPG_MALLOC_ZERO ((ABSIZE+1) * sizeof(int))))
  188. goto BuildTablesError;
  189. if (!(huflen = (int *)ILJPG_MALLOC_ZERO ((ABSIZE+1) * sizeof(int))))
  190. goto BuildTablesError;
  191. absize = build_intermediate_tables(bits, vals, ABSIZE, hufvals,
  192. hufcodes, huflen);
  193. if (absize <= 0) {
  194. error = ILJPG_ERROR_DECODE_DCAC_TABLE;
  195. goto BuildTablesError;
  196. }
  197. lookup_size = ( 1L << lookup_bits );
  198. /* allocate lookup tables */
  199. if (!(lookup_symb = (LOOKUP *)ILJPG_MALLOC_ZERO (lookup_size * sizeof(LOOKUP))))
  200. goto BuildTablesError;
  201. if (!(lookup_len = (LOOKUP *)ILJPG_MALLOC_ZERO (lookup_size * sizeof(LOOKUP))))
  202. goto BuildTablesError;
  203. /* allocate tree nodes */
  204. numnodes = 2*absize + 1; /* the max number of leaves is absize+1, since
  205. a leave is created for code 111...111 even
  206. though JPEG doesn't use it */
  207. if (!(tree_nodes = (tree_node *)ILJPG_MALLOC_ZERO (numnodes * sizeof(tree_node))))
  208. goto BuildTablesError;
  209. /* initialize lookup tables */
  210. for ( i=0; i<lookup_size; i++ ) {
  211. lookup_symb[i] = -1;
  212. lookup_len[i] = NOLEN;
  213. }
  214. /* now go thru the Huffman table, build a lookup table,
  215. and also parts of the Huffman tree for codes that
  216. are not fully in the lookup table */
  217. for ( s = 0; s < absize; s++ ) {
  218. c = hufvals[s];
  219. code = hufcodes[s];
  220. len = huflen[s];
  221. if ( len == NOLEN ) /* no entry for this character */
  222. continue;
  223. if ( len <= lookup_bits ) { /* enter in lookup table */
  224. lookup_index = code << ( lookup_bits - len );
  225. /* number of entries for this code */
  226. n = 1 << ( lookup_bits - len );
  227. for ( j=0; j<n; j++ ) {
  228. lookup_symb[ lookup_index + j ] = c;
  229. lookup_len[ lookup_index + j ] = len;
  230. }
  231. }
  232. else {
  233. /* build the part of the Huffman tree for the
  234. end of this code */
  235. lookup_index = code >> ( len - lookup_bits );
  236. /* check that the lookup entry is not occupied by
  237. a full coded symb */
  238. if ( lookup_len[lookup_index] != NOLEN ) {
  239. #if DEBUG
  240. fprintf(stderr,"*** ERROR: lookup_len[%ld] = %d\n",lookup_index,lookup_len[lookup_index]);
  241. #endif
  242. error = ILJPG_ERROR_DECODE_INTERNAL;
  243. goto BuildTablesError;
  244. }
  245. /* see if the prefix is already in the table */
  246. if ( (j = lookup_symb[lookup_index]) != -1 ) {
  247. /* it's there */
  248. nodep = tree_nodes + j; /* get tree pointer */
  249. }
  250. else {
  251. /* create a tree node */
  252. /* check that there are available nodes
  253. (this should never fail) */
  254. if ( next_node >= numnodes ) {
  255. #if DEBUG
  256. fprintf(stderr,"build_huffman_tables: *** ERROR: ran out of tree nodes (a)\n");
  257. #endif
  258. error = ILJPG_ERROR_DECODE_INTERNAL;
  259. goto BuildTablesError;
  260. }
  261. lookup_symb[lookup_index] = next_node;
  262. nodep = tree_nodes + next_node;
  263. nodep->left = NULL;
  264. nodep->right = NULL;
  265. next_node ++;
  266. lookup_len[lookup_index] = NOLEN;
  267. }
  268. mask = (1L<<(len - lookup_bits - 1));
  269. /* now go down the tree branch, building as needed */
  270. for ( i = lookup_bits; i < len; i++ ) {
  271. followp = (code & mask) ? &nodep->left : &nodep->right;
  272. if ( *followp == NULL ) {
  273. /* build a new node */
  274. /* check that there are available nodes
  275. (this should never fail) */
  276. if ( next_node >= numnodes ) {
  277. #if DEBUG
  278. fprintf(stderr,"build_huffman_tables: *** ERROR: ran out of tree nodes (b)\n");
  279. #endif
  280. error = ILJPG_ERROR_DECODE_INTERNAL;
  281. goto BuildTablesError;
  282. }
  283. *followp = &tree_nodes[next_node++];
  284. leafp = *followp; /* remember leaf */
  285. (*followp)->left = NULL;
  286. (*followp)->right= NULL;
  287. (*followp)->c = -1;
  288. }
  289. nodep = *followp;
  290. mask >>= 1;
  291. }
  292. leafp->c = c; /* write symbol in leaf */
  293. }
  294. }
  295. /* return pointers to lookup tables */
  296. table_setp->lookup_symb = lookup_symb;
  297. table_setp->lookup_len = lookup_len;
  298. table_setp->tree_nodes = tree_nodes;
  299. /* deallocate temporary data and return success */
  300. ILJPG_FREE (hufvals);
  301. ILJPG_FREE (hufcodes);
  302. ILJPG_FREE (huflen);
  303. return 0;
  304. /* Goto point on error: deallocate all non-null and return "error". */
  305. BuildTablesError:
  306. if (lookup_symb) ILJPG_FREE (lookup_symb);
  307. if (lookup_len) ILJPG_FREE (lookup_len);
  308. if (tree_nodes) ILJPG_FREE (tree_nodes);
  309. if (hufvals) ILJPG_FREE (hufvals);
  310. if (hufcodes) ILJPG_FREE (hufcodes);
  311. if (huflen) ILJPG_FREE (huflen);
  312. return error;
  313. }
  314. /* -------------------- _iljpgDehuffInit -------------------------- */
  315. /* Called by iljpgDecodeInit() to init for Huffman decoding.
  316. */
  317. ILJPG_PRIVATE_EXTERN
  318. iljpgError _iljpgDehuffInit (
  319. iljpgDecodePrivPtr pPriv
  320. )
  321. {
  322. iljpgDehuffPtr pHuffPriv;
  323. iljpgDataPtr pData;
  324. iljpgPtr pTable;
  325. iljpgError error;
  326. int i;
  327. /* Allocate Huffman private area and point to it in decode private */
  328. pData = pPriv->pData;
  329. pHuffPriv = (iljpgDehuffPtr)ILJPG_MALLOC_ZERO (sizeof (iljpgDehuffRec));
  330. if (!pHuffPriv)
  331. return ILJPG_ERROR_DECODE_MALLOC;
  332. pPriv->pHuffPriv = (iljpgPtr)pHuffPriv;
  333. /* Clear buffer and count of input bits, and flag that reset was done */
  334. pHuffPriv->bitbuf = 0;
  335. pHuffPriv->bitsleft = 0;
  336. pHuffPriv->resetDone = 0;
  337. /* Build lookup tables from DC/AC tables from caller (*pPriv->pData) */
  338. for (i = 0; i < 4; i++) {
  339. if ((pTable = pData->DCTables[i])) {
  340. if ((error = build_huffman_tables (pTable, (pTable+16), LOOKUP_BITS,
  341. &pHuffPriv->table_set_dc[i])))
  342. return error;
  343. }
  344. if ((pTable = pData->ACTables[i])) {
  345. if ((error = build_huffman_tables (pTable, (pTable+16), LOOKUP_BITS,
  346. &pHuffPriv->table_set_ac[i])))
  347. return error;
  348. }
  349. }
  350. return 0;
  351. }
  352. /* -------------------- _iljpgDehuffCleanup -------------------------- */
  353. /* Called by iljpgDecodeCleanup() to cleanup after Huffman decoding.
  354. */
  355. ILJPG_PRIVATE_EXTERN
  356. iljpgError _iljpgDehuffCleanup (
  357. iljpgDecodePrivPtr pPriv
  358. )
  359. {
  360. iljpgDehuffPtr pHuffPriv;
  361. int i;
  362. /* Free the Huffman decode private data including lookup tables */
  363. pHuffPriv = (iljpgDehuffPtr)pPriv->pHuffPriv;
  364. if (pHuffPriv) {
  365. for (i = 0; i < 4; i++) {
  366. if (pHuffPriv->table_set_dc[i].lookup_symb)
  367. ILJPG_FREE (pHuffPriv->table_set_dc[i].lookup_symb);
  368. if (pHuffPriv->table_set_dc[i].lookup_len)
  369. ILJPG_FREE (pHuffPriv->table_set_dc[i].lookup_len);
  370. if (pHuffPriv->table_set_dc[i].tree_nodes)
  371. ILJPG_FREE (pHuffPriv->table_set_dc[i].tree_nodes);
  372. if (pHuffPriv->table_set_ac[i].lookup_symb)
  373. ILJPG_FREE (pHuffPriv->table_set_ac[i].lookup_symb);
  374. if (pHuffPriv->table_set_ac[i].lookup_len)
  375. ILJPG_FREE (pHuffPriv->table_set_ac[i].lookup_len);
  376. if (pHuffPriv->table_set_ac[i].tree_nodes)
  377. ILJPG_FREE (pHuffPriv->table_set_ac[i].tree_nodes);
  378. }
  379. ILJPG_FREE (pHuffPriv);
  380. }
  381. return 0;
  382. }
  383. /* -------------------- _iljpgDehuffReset -------------------------- */
  384. /* Reset for Huffman decoding, when a restart marker is seen or at the
  385. beginning of a strip, which implicitly is a restart.
  386. */
  387. ILJPG_PRIVATE_EXTERN
  388. iljpgError _iljpgDehuffReset (
  389. iljpgDecodePrivPtr pPriv
  390. )
  391. {
  392. iljpgDehuffPtr pHuffPriv;
  393. int comp;
  394. /* Clear buffer and count of input bits */
  395. pHuffPriv = (iljpgDehuffPtr)pPriv->pHuffPriv;
  396. pHuffPriv->bitbuf = 0;
  397. pHuffPriv->bitsleft = 0;
  398. /* Flag that reset done, to signal DehuffExecute to eat restart marker */
  399. pHuffPriv->resetDone = 1;
  400. /* Clear set lastDC to 0, as per JPEG spec on a restart marker */
  401. for (comp = 0; comp < pPriv->pData->nComps; comp++)
  402. pPriv->compData[comp].lastDC = 0;
  403. return 0;
  404. }
  405. /* -------------------- _iljpgDehuffExecute -------------------------- */
  406. /* Decode 64 bytes of huffman bits from "stream" into "mb", for component
  407. index "comp". Return a code (e.g. HUFF_FULL) for what is non-zero in the
  408. block to "*pBlockType".
  409. */
  410. ILJPG_PRIVATE_EXTERN
  411. iljpgError _iljpgDehuffExecute (
  412. iljpgDecodePrivPtr pPriv,
  413. ILJPG_DECODE_STREAM stream,
  414. int comp,
  415. int *mb,
  416. unsigned int *pBlockType /* RETURNED */
  417. )
  418. {
  419. iljpgDehuffPtr pHuffPriv;
  420. int coeff_ct = 0;
  421. #define is_dc (!coeff_ct) /* First coefficient is DC */
  422. int delta;
  423. int len, zrun;
  424. register BITBUF bitbuf, ch, c;
  425. register int bitsleft;
  426. iljpgError error = 0; /* assume no error */
  427. int index;
  428. int is_fourx4 = 1, is_dconly = 0;
  429. int coeff;
  430. tree_node *nodep;
  431. struct table_set *pTableSet;
  432. LOOKUP *lookup_symb, *lookup_len;
  433. tree_node *tree_nodes;
  434. BITBUF markerValue = 0; /* 0 or code for marker seen */
  435. /* macros for outputting coefficients to a memory buffer */
  436. # define WRITE_COEFF(x) (*mb++ = (int)x)
  437. # define WRITE_ZRUN(n) ( mb += n )
  438. /* Get the next byte from stream into "_byte". If a 0xff,
  439. eat next char ifit is a null, otherwise it is a marker:
  440. if a restart marker ignore it; else store in markerValue, which if
  441. != 0 when this is invoked means a marker has already been seen (error).
  442. If an end-of-data error, set markerValue to non-zero.
  443. Thus any invocation of this macro after end-of-data or
  444. a non-restart marker seen is an error - but the first one is allowed.
  445. Apparently the code here fetches one byte ahead.
  446. */
  447. # define DECODE_GET_BYTE(_byte) { \
  448. if (markerValue) { \
  449. error = ILJPG_ERROR_DECODE_DATA; \
  450. goto out_mainloop; \
  451. } \
  452. if (!ILJPG_DECODE_GET_BYTE (stream, _byte, error)) { \
  453. if (error == ILJPG_ERROR_DECODE_EOD) { \
  454. markerValue = 1; \
  455. _byte = 0xff; \
  456. error = 0; \
  457. } \
  458. else goto out_mainloop; \
  459. } \
  460. else if (_byte == 0xff) { \
  461. if (!ILJPG_DECODE_GET_BYTE (stream, markerValue, error)) { \
  462. if (error == ILJPG_ERROR_DECODE_EOD) { \
  463. markerValue = 1; \
  464. error = 0; \
  465. } \
  466. else goto out_mainloop; \
  467. } \
  468. if ((markerValue & ~7) == ILJPGM_RST0) \
  469. markerValue = 0; \
  470. } \
  471. }
  472. /* clear output buffer */
  473. memset(mb, 0, BLOCK_COEFFS*sizeof(int));
  474. /* Get bit buffer and count from private */
  475. pHuffPriv = (iljpgDehuffPtr)pPriv->pHuffPriv;
  476. bitbuf = pHuffPriv->bitbuf;
  477. bitsleft = pHuffPriv->bitsleft;
  478. /* If a reset was just done, eat the next marker if present and get
  479. next byte (which can't be a marker or error!); set into bitbuf.
  480. */
  481. if (pHuffPriv->resetDone) {
  482. pHuffPriv->resetDone = 0;
  483. if (!ILJPG_DECODE_GET_BYTE (stream, ch, error))
  484. return error; /* must be more bytes after restart */
  485. if (ch == 0xff) { /* marker or 0xff,0 = 0xff */
  486. if (!ILJPG_DECODE_GET_BYTE (stream, ch, error))
  487. return error;
  488. if (ch == 0)
  489. ch = 0xff; /* ff,0 => ff in data stream */
  490. else if ((ch & ~7) == ILJPGM_RST0) {
  491. /* Restart marker; get another byte, or two if ff */
  492. if (!ILJPG_DECODE_GET_BYTE (stream, ch, error))
  493. return error;
  494. if (ch == 0xff) {
  495. if (!ILJPG_DECODE_GET_BYTE (stream, ch, error))
  496. return error;
  497. if (ch == 0)
  498. ch = 0xff; /* ff,0 => ff in data stream */
  499. }
  500. }
  501. else return ILJPG_ERROR_DECODE_DATA; /* other marker; error */
  502. }
  503. bitbuf = ( ch << SHIFTBUF );
  504. bitsleft = 8; /* one byte; bitsleft 0 after restart */
  505. }
  506. /* Point to DC tables for this component ("comp"). */
  507. pTableSet = &pHuffPriv->table_set_dc[pPriv->pData->comp[comp].DCTableIndex];
  508. lookup_symb = pTableSet->lookup_symb;
  509. lookup_len = pTableSet->lookup_len;
  510. tree_nodes = pTableSet->tree_nodes;
  511. while ( coeff_ct < BLOCK_COEFFS ) {
  512. while ( bitsleft < LOOKUP_BITS ) {
  513. DECODE_GET_BYTE (ch)
  514. ch <<= (SHIFTBUF - bitsleft);
  515. bitbuf += ch;
  516. bitsleft += 8;
  517. }
  518. index = bitbuf >> ( BITBUFSIZE - LOOKUP_BITS );
  519. ch = *(lookup_symb + index);
  520. len = *(lookup_len + index);
  521. if ( len != NOLEN ) { /* can get value from lookup table */
  522. bitbuf <<= len;
  523. bitsleft -= len;
  524. }
  525. else { /* has to go down the tree */
  526. bitbuf <<= LOOKUP_BITS;
  527. bitsleft -= LOOKUP_BITS;
  528. /* point to a node in the tree */
  529. nodep = tree_nodes + ch;
  530. while ( 1 ) {
  531. if ( !bitsleft ) {
  532. DECODE_GET_BYTE (ch)
  533. bitbuf = ( ch << SHIFTBUF );
  534. bitsleft = 8;
  535. }
  536. if ( bitbuf & BUFMSB ) /* next bit is a one */
  537. nodep = nodep->left;
  538. else
  539. nodep = nodep->right;
  540. bitbuf <<= 1;
  541. bitsleft --;
  542. /* if nodep is null, error in data */
  543. if (!nodep)
  544. return ILJPG_ERROR_DECODE_DATA;
  545. if ( (ch = nodep->c) != NOLEAF ) {
  546. break;
  547. }
  548. }
  549. }
  550. if ( ! is_dc ) {
  551. /* Handling of AC coefficients */
  552. /* at this point, ch contains RRRRSSSS, where
  553. RRRR = length of a run of zero coeffs.
  554. SSSS = number of the "bin" containing the next coeff.
  555. */
  556. /* first handle RRRR: */
  557. zrun = ch >> 4;
  558. if ( zrun ) {
  559. WRITE_ZRUN(zrun);
  560. coeff_ct += zrun;
  561. ch &= 0x0f;
  562. }
  563. }
  564. else { /* we've done a DC case: set pointers for next
  565. coefficient to AC tables. */
  566. pTableSet = &pHuffPriv->table_set_ac
  567. [pPriv->pData->comp[comp].ACTableIndex];
  568. lookup_symb = pTableSet->lookup_symb;
  569. lookup_len = pTableSet->lookup_len;
  570. tree_nodes = pTableSet->tree_nodes;
  571. }
  572. /* rest of AC case is identical to DC case */
  573. /* at this point, ch contains the "bin" number:
  574. read that number of bits from the input stream */
  575. if ( ch ) {
  576. /* SSSS is not zero */
  577. while ( bitsleft < ch ) {
  578. DECODE_GET_BYTE (c)
  579. bitbuf = bitbuf +
  580. ( c << (SHIFTBUF - bitsleft) );
  581. bitsleft += 8;
  582. }
  583. /* the desired number is now at the MS part of bitbuf */
  584. /* NOTE: Following code should be broken into a big
  585. SWITCH statement, with one case per value of ch
  586. between 1 and 11 */
  587. /* shift to lower part */
  588. coeff = bitbuf >> (BITBUFSIZE - ch);
  589. if ( (bitbuf & BUFMSB) == 0 ) /* leading bit is a zero */
  590. coeff -= (( 1L << ch ) - 1 );
  591. bitbuf <<= ch;
  592. bitsleft -= ch;
  593. WRITE_COEFF(coeff); /* write a nonzero coefficient */
  594. /* check if the nonzero coefficient is inside the
  595. upper left 4x4 sub-block */
  596. if ( is_fourx4 )
  597. is_fourx4 &= fourx4_table[ coeff_ct ];
  598. coeff_ct ++;
  599. }
  600. else {
  601. /* SSSS is zero */
  602. if ( !is_dc && zrun == 0 ) {
  603. /* we got RRRRSSSS = 00000000, pad rest of the
  604. block with zeroes */
  605. delta = BLOCK_COEFFS-coeff_ct;
  606. WRITE_ZRUN(delta);
  607. coeff_ct += delta;
  608. is_dconly = ( delta == BLOCK_COEFFS-1);
  609. }
  610. else {
  611. /* write an isolated zero coefficient */
  612. WRITE_ZRUN(1);
  613. coeff_ct ++;
  614. }
  615. }
  616. }
  617. /* Goto point for exiting: "error" must contain current error code */
  618. out_mainloop:
  619. /* Store bit buffer and count into private */
  620. pHuffPriv->bitbuf = bitbuf;
  621. pHuffPriv->bitsleft = bitsleft;
  622. if ( is_dconly )
  623. *pBlockType = HUFF_DC_ONLY;
  624. else if ( is_fourx4 )
  625. *pBlockType = HUFF_FOURX4;
  626. else *pBlockType = HUFF_FULL;
  627. return error;
  628. }