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.

846 lines
25 KiB

  1. /*
  2. * jchuff.c
  3. *
  4. * Copyright (C) 1991-1995, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains Huffman entropy encoding routines.
  9. *
  10. * Much of the complexity here has to do with supporting output suspension.
  11. * If the data destination module demands suspension, we want to be able to
  12. * back up to the start of the current MCU. To do this, we copy state
  13. * variables into local working storage, and update them back to the
  14. * permanent JPEG objects only upon successful completion of an MCU.
  15. */
  16. #define JPEG_INTERNALS
  17. #include "jinclude.h"
  18. #include "jpeglib.h"
  19. #include "jchuff.h" /* Declarations shared with jcphuff.c */
  20. /* Expanded entropy encoder object for Huffman encoding.
  21. *
  22. * The savable_state subrecord contains fields that change within an MCU,
  23. * but must not be updated permanently until we complete the MCU.
  24. */
  25. typedef struct {
  26. INT32 put_buffer; /* current bit-accumulation buffer */
  27. int put_bits; /* # of bits now in it */
  28. int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
  29. } savable_state;
  30. /* This macro is to work around compilers with missing or broken
  31. * structure assignment. You'll need to fix this code if you have
  32. * such a compiler and you change MAX_COMPS_IN_SCAN.
  33. */
  34. #ifndef NO_STRUCT_ASSIGN
  35. #define ASSIGN_STATE(dest,src) ((dest) = (src))
  36. #else
  37. #if MAX_COMPS_IN_SCAN == 4
  38. #define ASSIGN_STATE(dest,src) \
  39. ((dest).put_buffer = (src).put_buffer, \
  40. (dest).put_bits = (src).put_bits, \
  41. (dest).last_dc_val[0] = (src).last_dc_val[0], \
  42. (dest).last_dc_val[1] = (src).last_dc_val[1], \
  43. (dest).last_dc_val[2] = (src).last_dc_val[2], \
  44. (dest).last_dc_val[3] = (src).last_dc_val[3])
  45. #endif
  46. #endif
  47. typedef struct {
  48. struct jpeg_entropy_encoder pub; /* public fields */
  49. savable_state saved; /* Bit buffer & DC state at start of MCU */
  50. /* These fields are NOT loaded into local working state. */
  51. unsigned int restarts_to_go; /* MCUs left in this restart interval */
  52. int next_restart_num; /* next restart number to write (0-7) */
  53. /* Pointers to derived tables (these workspaces have image lifespan) */
  54. c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS];
  55. c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];
  56. #ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
  57. long * dc_count_ptrs[NUM_HUFF_TBLS];
  58. long * ac_count_ptrs[NUM_HUFF_TBLS];
  59. #endif
  60. } huff_entropy_encoder;
  61. typedef huff_entropy_encoder * huff_entropy_ptr;
  62. /* Working state while writing an MCU.
  63. * This struct contains all the fields that are needed by subroutines.
  64. */
  65. typedef struct {
  66. JOCTET * next_output_byte; /* => next byte to write in buffer */
  67. size_t free_in_buffer; /* # of byte spaces remaining in buffer */
  68. savable_state cur; /* Current bit buffer & DC state */
  69. j_compress_ptr cinfo; /* dump_buffer needs access to this */
  70. } working_state;
  71. /* Forward declarations */
  72. METHODDEF boolean encode_mcu_huff JPP((j_compress_ptr cinfo,
  73. JBLOCKROW *MCU_data));
  74. METHODDEF void finish_pass_huff JPP((j_compress_ptr cinfo));
  75. #ifdef ENTROPY_OPT_SUPPORTED
  76. METHODDEF boolean encode_mcu_gather JPP((j_compress_ptr cinfo,
  77. JBLOCKROW *MCU_data));
  78. METHODDEF void finish_pass_gather JPP((j_compress_ptr cinfo));
  79. #endif
  80. /*
  81. * Initialize for a Huffman-compressed scan.
  82. * If gather_statistics is TRUE, we do not output anything during the scan,
  83. * just count the Huffman symbols used and generate Huffman code tables.
  84. */
  85. METHODDEF void
  86. start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics)
  87. {
  88. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  89. int ci, dctbl, actbl;
  90. jpeg_component_info * compptr;
  91. if (gather_statistics) {
  92. #ifdef ENTROPY_OPT_SUPPORTED
  93. entropy->pub.encode_mcu = encode_mcu_gather;
  94. entropy->pub.finish_pass = finish_pass_gather;
  95. #else
  96. ERREXIT(cinfo, JERR_NOT_COMPILED);
  97. #endif
  98. } else {
  99. entropy->pub.encode_mcu = encode_mcu_huff;
  100. entropy->pub.finish_pass = finish_pass_huff;
  101. }
  102. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  103. compptr = cinfo->cur_comp_info[ci];
  104. dctbl = compptr->dc_tbl_no;
  105. actbl = compptr->ac_tbl_no;
  106. /* Make sure requested tables are present */
  107. /* (In gather mode, tables need not be allocated yet) */
  108. if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS ||
  109. (cinfo->dc_huff_tbl_ptrs[dctbl] == NULL && !gather_statistics))
  110. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
  111. if (actbl < 0 || actbl >= NUM_HUFF_TBLS ||
  112. (cinfo->ac_huff_tbl_ptrs[actbl] == NULL && !gather_statistics))
  113. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
  114. if (gather_statistics) {
  115. #ifdef ENTROPY_OPT_SUPPORTED
  116. /* Allocate and zero the statistics tables */
  117. /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
  118. if (entropy->dc_count_ptrs[dctbl] == NULL)
  119. entropy->dc_count_ptrs[dctbl] = (long *)
  120. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  121. 257 * SIZEOF(long));
  122. MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
  123. if (entropy->ac_count_ptrs[actbl] == NULL)
  124. entropy->ac_count_ptrs[actbl] = (long *)
  125. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  126. 257 * SIZEOF(long));
  127. MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
  128. #endif
  129. } else {
  130. /* Compute derived values for Huffman tables */
  131. /* We may do this more than once for a table, but it's not expensive */
  132. jpeg_make_c_derived_tbl(cinfo, cinfo->dc_huff_tbl_ptrs[dctbl],
  133. & entropy->dc_derived_tbls[dctbl]);
  134. jpeg_make_c_derived_tbl(cinfo, cinfo->ac_huff_tbl_ptrs[actbl],
  135. & entropy->ac_derived_tbls[actbl]);
  136. }
  137. /* Initialize DC predictions to 0 */
  138. entropy->saved.last_dc_val[ci] = 0;
  139. }
  140. /* Initialize bit buffer to empty */
  141. entropy->saved.put_buffer = 0;
  142. entropy->saved.put_bits = 0;
  143. /* Initialize restart stuff */
  144. entropy->restarts_to_go = cinfo->restart_interval;
  145. entropy->next_restart_num = 0;
  146. }
  147. /*
  148. * Compute the derived values for a Huffman table.
  149. * Note this is also used by jcphuff.c.
  150. */
  151. GLOBAL void
  152. jpeg_make_c_derived_tbl (j_compress_ptr cinfo, JHUFF_TBL * htbl,
  153. c_derived_tbl ** pdtbl)
  154. {
  155. c_derived_tbl *dtbl;
  156. int p, i, l, lastp, si;
  157. char huffsize[257];
  158. unsigned int huffcode[257];
  159. unsigned int code;
  160. /* Allocate a workspace if we haven't already done so. */
  161. if (*pdtbl == NULL)
  162. *pdtbl = (c_derived_tbl *)
  163. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  164. SIZEOF(c_derived_tbl));
  165. dtbl = *pdtbl;
  166. /* Figure C.1: make table of Huffman code length for each symbol */
  167. /* Note that this is in code-length order. */
  168. p = 0;
  169. for (l = 1; l <= 16; l++) {
  170. for (i = 1; i <= (int) htbl->bits[l]; i++)
  171. huffsize[p++] = (char) l;
  172. }
  173. huffsize[p] = 0;
  174. lastp = p;
  175. /* Figure C.2: generate the codes themselves */
  176. /* Note that this is in code-length order. */
  177. code = 0;
  178. si = huffsize[0];
  179. p = 0;
  180. while (huffsize[p]) {
  181. while (((int) huffsize[p]) == si) {
  182. huffcode[p++] = code;
  183. code++;
  184. }
  185. code <<= 1;
  186. si++;
  187. }
  188. /* Figure C.3: generate encoding tables */
  189. /* These are code and size indexed by symbol value */
  190. /* Set any codeless symbols to have code length 0;
  191. * this allows emit_bits to detect any attempt to emit such symbols.
  192. */
  193. MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
  194. for (p = 0; p < lastp; p++) {
  195. dtbl->ehufco[htbl->huffval[p]] = huffcode[p];
  196. dtbl->ehufsi[htbl->huffval[p]] = huffsize[p];
  197. }
  198. }
  199. /* Outputting bytes to the file */
  200. /* Emit a byte, taking 'action' if must suspend. */
  201. #define emit_byte(state,val,action) \
  202. { *(state)->next_output_byte++ = (JOCTET) (val); \
  203. if (--(state)->free_in_buffer == 0) \
  204. if (! dump_buffer(state)) \
  205. { action; } }
  206. LOCAL boolean
  207. dump_buffer (working_state * state)
  208. /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
  209. {
  210. struct jpeg_destination_mgr * dest = state->cinfo->dest;
  211. if (! (*dest->empty_output_buffer) (state->cinfo))
  212. return FALSE;
  213. /* After a successful buffer dump, must reset buffer pointers */
  214. state->next_output_byte = dest->next_output_byte;
  215. state->free_in_buffer = dest->free_in_buffer;
  216. return TRUE;
  217. }
  218. /* Outputting bits to the file */
  219. /* Only the right 24 bits of put_buffer are used; the valid bits are
  220. * left-justified in this part. At most 16 bits can be passed to emit_bits
  221. * in one call, and we never retain more than 7 bits in put_buffer
  222. * between calls, so 24 bits are sufficient.
  223. */
  224. INLINE
  225. LOCAL boolean
  226. emit_bits (working_state * state, unsigned int code, int size)
  227. /* Emit some bits; return TRUE if successful, FALSE if must suspend */
  228. {
  229. /* This routine is heavily used, so it's worth coding tightly. */
  230. register INT32 put_buffer = (INT32) code;
  231. register int put_bits = state->cur.put_bits;
  232. /* if size is 0, caller used an invalid Huffman table entry */
  233. if (size == 0)
  234. ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
  235. put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
  236. put_bits += size; /* new number of bits in buffer */
  237. put_buffer <<= 24 - put_bits; /* align incoming bits */
  238. put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
  239. while (put_bits >= 8) {
  240. int c = (int) ((put_buffer >> 16) & 0xFF);
  241. emit_byte(state, c, return FALSE);
  242. if (c == 0xFF) { /* need to stuff a zero byte? */
  243. emit_byte(state, 0, return FALSE);
  244. }
  245. put_buffer <<= 8;
  246. put_bits -= 8;
  247. }
  248. state->cur.put_buffer = put_buffer; /* update state variables */
  249. state->cur.put_bits = put_bits;
  250. return TRUE;
  251. }
  252. LOCAL boolean
  253. flush_bits (working_state * state)
  254. {
  255. if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
  256. return FALSE;
  257. state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
  258. state->cur.put_bits = 0;
  259. return TRUE;
  260. }
  261. /* Encode a single block's worth of coefficients */
  262. LOCAL boolean
  263. encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
  264. c_derived_tbl *dctbl, c_derived_tbl *actbl)
  265. {
  266. register int temp, temp2;
  267. register int nbits;
  268. register int k, r, i;
  269. /* Encode the DC coefficient difference per section F.1.2.1 */
  270. temp = temp2 = block[0] - last_dc_val;
  271. if (temp < 0) {
  272. temp = -temp; /* temp is abs value of input */
  273. /* For a negative input, want temp2 = bitwise complement of abs(input) */
  274. /* This code assumes we are on a two's complement machine */
  275. temp2--;
  276. }
  277. /* Find the number of bits needed for the magnitude of the coefficient */
  278. nbits = 0;
  279. while (temp) {
  280. nbits++;
  281. temp >>= 1;
  282. }
  283. /* Emit the Huffman-coded symbol for the number of bits */
  284. if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
  285. return FALSE;
  286. /* Emit that number of bits of the value, if positive, */
  287. /* or the complement of its magnitude, if negative. */
  288. if (nbits) /* emit_bits rejects calls with size 0 */
  289. if (! emit_bits(state, (unsigned int) temp2, nbits))
  290. return FALSE;
  291. /* Encode the AC coefficients per section F.1.2.2 */
  292. r = 0; /* r = run length of zeros */
  293. for (k = 1; k < DCTSIZE2; k++) {
  294. if ((temp = block[jpeg_natural_order[k]]) == 0) {
  295. r++;
  296. } else {
  297. /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  298. while (r > 15) {
  299. if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
  300. return FALSE;
  301. r -= 16;
  302. }
  303. temp2 = temp;
  304. if (temp < 0) {
  305. temp = -temp; /* temp is abs value of input */
  306. /* This code assumes we are on a two's complement machine */
  307. temp2--;
  308. }
  309. /* Find the number of bits needed for the magnitude of the coefficient */
  310. nbits = 1; /* there must be at least one 1 bit */
  311. while ((temp >>= 1))
  312. nbits++;
  313. /* Emit Huffman symbol for run length / number of bits */
  314. i = (r << 4) + nbits;
  315. if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
  316. return FALSE;
  317. /* Emit that number of bits of the value, if positive, */
  318. /* or the complement of its magnitude, if negative. */
  319. if (! emit_bits(state, (unsigned int) temp2, nbits))
  320. return FALSE;
  321. r = 0;
  322. }
  323. }
  324. /* If the last coef(s) were zero, emit an end-of-block code */
  325. if (r > 0)
  326. if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
  327. return FALSE;
  328. return TRUE;
  329. }
  330. /*
  331. * Emit a restart marker & resynchronize predictions.
  332. */
  333. LOCAL boolean
  334. emit_restart (working_state * state, int restart_num)
  335. {
  336. int ci;
  337. if (! flush_bits(state))
  338. return FALSE;
  339. emit_byte(state, 0xFF, return FALSE);
  340. emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
  341. /* Re-initialize DC predictions to 0 */
  342. for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
  343. state->cur.last_dc_val[ci] = 0;
  344. /* The restart counter is not updated until we successfully write the MCU. */
  345. return TRUE;
  346. }
  347. /*
  348. * Encode and output one MCU's worth of Huffman-compressed coefficients.
  349. */
  350. METHODDEF boolean
  351. encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  352. {
  353. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  354. working_state state;
  355. int blkn, ci;
  356. jpeg_component_info * compptr;
  357. /* Load up working state */
  358. state.next_output_byte = cinfo->dest->next_output_byte;
  359. state.free_in_buffer = cinfo->dest->free_in_buffer;
  360. ASSIGN_STATE(state.cur, entropy->saved);
  361. state.cinfo = cinfo;
  362. /* Emit restart marker if needed */
  363. if (cinfo->restart_interval) {
  364. if (entropy->restarts_to_go == 0)
  365. if (! emit_restart(&state, entropy->next_restart_num))
  366. return FALSE;
  367. }
  368. /* Encode the MCU data blocks */
  369. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  370. ci = cinfo->MCU_membership[blkn];
  371. compptr = cinfo->cur_comp_info[ci];
  372. if (! encode_one_block(&state,
  373. MCU_data[blkn][0], state.cur.last_dc_val[ci],
  374. entropy->dc_derived_tbls[compptr->dc_tbl_no],
  375. entropy->ac_derived_tbls[compptr->ac_tbl_no]))
  376. return FALSE;
  377. /* Update last_dc_val */
  378. state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
  379. }
  380. /* Completed MCU, so update state */
  381. cinfo->dest->next_output_byte = state.next_output_byte;
  382. cinfo->dest->free_in_buffer = state.free_in_buffer;
  383. ASSIGN_STATE(entropy->saved, state.cur);
  384. /* Update restart-interval state too */
  385. if (cinfo->restart_interval) {
  386. if (entropy->restarts_to_go == 0) {
  387. entropy->restarts_to_go = cinfo->restart_interval;
  388. entropy->next_restart_num++;
  389. entropy->next_restart_num &= 7;
  390. }
  391. entropy->restarts_to_go--;
  392. }
  393. return TRUE;
  394. }
  395. /*
  396. * Finish up at the end of a Huffman-compressed scan.
  397. */
  398. METHODDEF void
  399. finish_pass_huff (j_compress_ptr cinfo)
  400. {
  401. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  402. working_state state;
  403. /* Load up working state ... flush_bits needs it */
  404. state.next_output_byte = cinfo->dest->next_output_byte;
  405. state.free_in_buffer = cinfo->dest->free_in_buffer;
  406. ASSIGN_STATE(state.cur, entropy->saved);
  407. state.cinfo = cinfo;
  408. /* Flush out the last data */
  409. if (! flush_bits(&state))
  410. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  411. /* Update state */
  412. cinfo->dest->next_output_byte = state.next_output_byte;
  413. cinfo->dest->free_in_buffer = state.free_in_buffer;
  414. ASSIGN_STATE(entropy->saved, state.cur);
  415. }
  416. /*
  417. * Huffman coding optimization.
  418. *
  419. * This actually is optimization, in the sense that we find the best possible
  420. * Huffman table(s) for the given data. We first scan the supplied data and
  421. * count the number of uses of each symbol that is to be Huffman-coded.
  422. * (This process must agree with the code above.) Then we build an
  423. * optimal Huffman coding tree for the observed counts.
  424. *
  425. * The JPEG standard requires Huffman codes to be no more than 16 bits long.
  426. * If some symbols have a very small but nonzero probability, the Huffman tree
  427. * must be adjusted to meet the code length restriction. We currently use
  428. * the adjustment method suggested in the JPEG spec. This method is *not*
  429. * optimal; it may not choose the best possible limited-length code. But
  430. * since the symbols involved are infrequently used, it's not clear that
  431. * going to extra trouble is worthwhile.
  432. */
  433. #ifdef ENTROPY_OPT_SUPPORTED
  434. /* Process a single block's worth of coefficients */
  435. LOCAL void
  436. htest_one_block (JCOEFPTR block, int last_dc_val,
  437. long dc_counts[], long ac_counts[])
  438. {
  439. register int temp;
  440. register int nbits;
  441. register int k, r;
  442. /* Encode the DC coefficient difference per section F.1.2.1 */
  443. temp = block[0] - last_dc_val;
  444. if (temp < 0)
  445. temp = -temp;
  446. /* Find the number of bits needed for the magnitude of the coefficient */
  447. nbits = 0;
  448. while (temp) {
  449. nbits++;
  450. temp >>= 1;
  451. }
  452. /* Count the Huffman symbol for the number of bits */
  453. dc_counts[nbits]++;
  454. /* Encode the AC coefficients per section F.1.2.2 */
  455. r = 0; /* r = run length of zeros */
  456. for (k = 1; k < DCTSIZE2; k++) {
  457. if ((temp = block[jpeg_natural_order[k]]) == 0) {
  458. r++;
  459. } else {
  460. /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  461. while (r > 15) {
  462. ac_counts[0xF0]++;
  463. r -= 16;
  464. }
  465. /* Find the number of bits needed for the magnitude of the coefficient */
  466. if (temp < 0)
  467. temp = -temp;
  468. /* Find the number of bits needed for the magnitude of the coefficient */
  469. nbits = 1; /* there must be at least one 1 bit */
  470. while ((temp >>= 1))
  471. nbits++;
  472. /* Count Huffman symbol for run length / number of bits */
  473. ac_counts[(r << 4) + nbits]++;
  474. r = 0;
  475. }
  476. }
  477. /* If the last coef(s) were zero, emit an end-of-block code */
  478. if (r > 0)
  479. ac_counts[0]++;
  480. }
  481. /*
  482. * Trial-encode one MCU's worth of Huffman-compressed coefficients.
  483. * No data is actually output, so no suspension return is possible.
  484. */
  485. METHODDEF boolean
  486. encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  487. {
  488. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  489. int blkn, ci;
  490. jpeg_component_info * compptr;
  491. /* Take care of restart intervals if needed */
  492. if (cinfo->restart_interval) {
  493. if (entropy->restarts_to_go == 0) {
  494. /* Re-initialize DC predictions to 0 */
  495. for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  496. entropy->saved.last_dc_val[ci] = 0;
  497. /* Update restart state */
  498. entropy->restarts_to_go = cinfo->restart_interval;
  499. }
  500. entropy->restarts_to_go--;
  501. }
  502. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  503. ci = cinfo->MCU_membership[blkn];
  504. compptr = cinfo->cur_comp_info[ci];
  505. htest_one_block(MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
  506. entropy->dc_count_ptrs[compptr->dc_tbl_no],
  507. entropy->ac_count_ptrs[compptr->ac_tbl_no]);
  508. entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
  509. }
  510. return TRUE;
  511. }
  512. /*
  513. * Generate the optimal coding for the given counts, fill htbl.
  514. * Note this is also used by jcphuff.c.
  515. */
  516. GLOBAL void
  517. jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
  518. {
  519. #define MAX_CLEN 32 /* assumed maximum initial code length */
  520. UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
  521. int codesize[257]; /* codesize[k] = code length of symbol k */
  522. int others[257]; /* next symbol in current branch of tree */
  523. int c1, c2;
  524. int p, i, j;
  525. long v;
  526. /* This algorithm is explained in section K.2 of the JPEG standard */
  527. MEMZERO(bits, SIZEOF(bits));
  528. MEMZERO(codesize, SIZEOF(codesize));
  529. for (i = 0; i < 257; i++)
  530. others[i] = -1; /* init links to empty */
  531. freq[256] = 1; /* make sure there is a nonzero count */
  532. /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
  533. * that no real symbol is given code-value of all ones, because 256
  534. * will be placed in the largest codeword category.
  535. */
  536. /* Huffman's basic algorithm to assign optimal code lengths to symbols */
  537. for (;;) {
  538. /* Find the smallest nonzero frequency, set c1 = its symbol */
  539. /* In case of ties, take the larger symbol number */
  540. c1 = -1;
  541. v = 1000000000L;
  542. for (i = 0; i <= 256; i++) {
  543. if (freq[i] && freq[i] <= v) {
  544. v = freq[i];
  545. c1 = i;
  546. }
  547. }
  548. /* Find the next smallest nonzero frequency, set c2 = its symbol */
  549. /* In case of ties, take the larger symbol number */
  550. c2 = -1;
  551. v = 1000000000L;
  552. for (i = 0; i <= 256; i++) {
  553. if (freq[i] && freq[i] <= v && i != c1) {
  554. v = freq[i];
  555. c2 = i;
  556. }
  557. }
  558. /* Done if we've merged everything into one frequency */
  559. if (c2 < 0)
  560. break;
  561. /* Else merge the two counts/trees */
  562. freq[c1] += freq[c2];
  563. freq[c2] = 0;
  564. /* Increment the codesize of everything in c1's tree branch */
  565. codesize[c1]++;
  566. while (others[c1] >= 0) {
  567. c1 = others[c1];
  568. codesize[c1]++;
  569. }
  570. others[c1] = c2; /* chain c2 onto c1's tree branch */
  571. /* Increment the codesize of everything in c2's tree branch */
  572. codesize[c2]++;
  573. while (others[c2] >= 0) {
  574. c2 = others[c2];
  575. codesize[c2]++;
  576. }
  577. }
  578. /* Now count the number of symbols of each code length */
  579. for (i = 0; i <= 256; i++) {
  580. if (codesize[i]) {
  581. /* The JPEG standard seems to think that this can't happen, */
  582. /* but I'm paranoid... */
  583. if (codesize[i] > MAX_CLEN)
  584. ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
  585. bits[codesize[i]]++;
  586. }
  587. }
  588. /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
  589. * Huffman procedure assigned any such lengths, we must adjust the coding.
  590. * Here is what the JPEG spec says about how this next bit works:
  591. * Since symbols are paired for the longest Huffman code, the symbols are
  592. * removed from this length category two at a time. The prefix for the pair
  593. * (which is one bit shorter) is allocated to one of the pair; then,
  594. * skipping the BITS entry for that prefix length, a code word from the next
  595. * shortest nonzero BITS entry is converted into a prefix for two code words
  596. * one bit longer.
  597. */
  598. for (i = MAX_CLEN; i > 16; i--) {
  599. while (bits[i] > 0) {
  600. j = i - 2; /* find length of new prefix to be used */
  601. while (bits[j] == 0)
  602. j--;
  603. bits[i] -= 2; /* remove two symbols */
  604. bits[i-1]++; /* one goes in this length */
  605. bits[j+1] += 2; /* two new symbols in this length */
  606. bits[j]--; /* symbol of this length is now a prefix */
  607. }
  608. }
  609. /* Remove the count for the pseudo-symbol 256 from the largest codelength */
  610. while (bits[i] == 0) /* find largest codelength still in use */
  611. i--;
  612. bits[i]--;
  613. /* Return final symbol counts (only for lengths 0..16) */
  614. MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
  615. /* Return a list of the symbols sorted by code length */
  616. /* It's not real clear to me why we don't need to consider the codelength
  617. * changes made above, but the JPEG spec seems to think this works.
  618. */
  619. p = 0;
  620. for (i = 1; i <= MAX_CLEN; i++) {
  621. for (j = 0; j <= 255; j++) {
  622. if (codesize[j] == i) {
  623. htbl->huffval[p] = (UINT8) j;
  624. p++;
  625. }
  626. }
  627. }
  628. /* Set sent_table FALSE so updated table will be written to JPEG file. */
  629. htbl->sent_table = FALSE;
  630. }
  631. /*
  632. * Finish up a statistics-gathering pass and create the new Huffman tables.
  633. */
  634. METHODDEF void
  635. finish_pass_gather (j_compress_ptr cinfo)
  636. {
  637. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  638. int ci, dctbl, actbl;
  639. jpeg_component_info * compptr;
  640. JHUFF_TBL **htblptr;
  641. boolean did_dc[NUM_HUFF_TBLS];
  642. boolean did_ac[NUM_HUFF_TBLS];
  643. /* It's important not to apply jpeg_gen_optimal_table more than once
  644. * per table, because it clobbers the input frequency counts!
  645. */
  646. MEMZERO(did_dc, SIZEOF(did_dc));
  647. MEMZERO(did_ac, SIZEOF(did_ac));
  648. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  649. compptr = cinfo->cur_comp_info[ci];
  650. dctbl = compptr->dc_tbl_no;
  651. actbl = compptr->ac_tbl_no;
  652. if (! did_dc[dctbl]) {
  653. htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
  654. if (*htblptr == NULL)
  655. *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  656. jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
  657. did_dc[dctbl] = TRUE;
  658. }
  659. if (! did_ac[actbl]) {
  660. htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
  661. if (*htblptr == NULL)
  662. *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  663. jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
  664. did_ac[actbl] = TRUE;
  665. }
  666. }
  667. }
  668. #endif /* ENTROPY_OPT_SUPPORTED */
  669. /*
  670. * Module initialization routine for Huffman entropy encoding.
  671. */
  672. GLOBAL void
  673. jinit_huff_encoder (j_compress_ptr cinfo)
  674. {
  675. huff_entropy_ptr entropy;
  676. int i;
  677. entropy = (huff_entropy_ptr)
  678. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  679. SIZEOF(huff_entropy_encoder));
  680. cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  681. entropy->pub.start_pass = start_pass_huff;
  682. /* Mark tables unallocated */
  683. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  684. entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
  685. #ifdef ENTROPY_OPT_SUPPORTED
  686. entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
  687. #endif
  688. }
  689. }