jchuff.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. * jchuff.c
  3. *
  4. * Copyright (C) 1991-1997, 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. if (gather_statistics) {
  107. #ifdef ENTROPY_OPT_SUPPORTED
  108. /* Check for invalid table indexes */
  109. /* (make_c_derived_tbl does this in the other path) */
  110. if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
  111. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
  112. if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
  113. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
  114. /* Allocate and zero the statistics tables */
  115. /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
  116. if (entropy->dc_count_ptrs[dctbl] == NULL)
  117. entropy->dc_count_ptrs[dctbl] = (long *)
  118. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  119. 257 * SIZEOF(long));
  120. MEMZERO(entropy->dc_count_ptrs[dctbl], 257 * SIZEOF(long));
  121. if (entropy->ac_count_ptrs[actbl] == NULL)
  122. entropy->ac_count_ptrs[actbl] = (long *)
  123. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  124. 257 * SIZEOF(long));
  125. MEMZERO(entropy->ac_count_ptrs[actbl], 257 * SIZEOF(long));
  126. #endif
  127. } else {
  128. /* Compute derived values for Huffman tables */
  129. /* We may do this more than once for a table, but it's not expensive */
  130. jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
  131. & entropy->dc_derived_tbls[dctbl]);
  132. jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
  133. & entropy->ac_derived_tbls[actbl]);
  134. }
  135. /* Initialize DC predictions to 0 */
  136. entropy->saved.last_dc_val[ci] = 0;
  137. }
  138. /* Initialize bit buffer to empty */
  139. entropy->saved.put_buffer = 0;
  140. entropy->saved.put_bits = 0;
  141. /* Initialize restart stuff */
  142. entropy->restarts_to_go = cinfo->restart_interval;
  143. entropy->next_restart_num = 0;
  144. }
  145. /*
  146. * Compute the derived values for a Huffman table.
  147. * This routine also performs some validation checks on the table.
  148. *
  149. * Note this is also used by jcphuff.c.
  150. */
  151. GLOBAL(void)
  152. jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno,
  153. c_derived_tbl ** pdtbl)
  154. {
  155. JHUFF_TBL *htbl;
  156. c_derived_tbl *dtbl;
  157. int p, i, l, lastp, si, maxsymbol;
  158. char huffsize[257];
  159. unsigned int huffcode[257];
  160. unsigned int code;
  161. /* Note that huffsize[] and huffcode[] are filled in code-length order,
  162. * paralleling the order of the symbols themselves in htbl->huffval[].
  163. */
  164. /* Find the input Huffman table */
  165. if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
  166. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  167. htbl =
  168. isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
  169. if (htbl == NULL)
  170. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  171. /* Allocate a workspace if we haven't already done so. */
  172. if (*pdtbl == NULL)
  173. *pdtbl = (c_derived_tbl *)
  174. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  175. SIZEOF(c_derived_tbl));
  176. dtbl = *pdtbl;
  177. /* Figure C.1: make table of Huffman code length for each symbol */
  178. p = 0;
  179. for (l = 1; l <= 16; l++) {
  180. i = (int) htbl->bits[l];
  181. if (i < 0 || p + i > 256) /* protect against table overrun */
  182. ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  183. while (i--)
  184. huffsize[p++] = (char) l;
  185. }
  186. huffsize[p] = 0;
  187. lastp = p;
  188. /* Figure C.2: generate the codes themselves */
  189. /* We also validate that the counts represent a legal Huffman code tree. */
  190. code = 0;
  191. si = huffsize[0];
  192. p = 0;
  193. while (huffsize[p]) {
  194. while (((int) huffsize[p]) == si) {
  195. huffcode[p++] = code;
  196. code++;
  197. }
  198. /* code is now 1 more than the last code used for codelength si; but
  199. * it must still fit in si bits, since no code is allowed to be all ones.
  200. */
  201. if (((INT32) code) >= (((INT32) 1) << si))
  202. ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  203. code <<= 1;
  204. si++;
  205. }
  206. /* Figure C.3: generate encoding tables */
  207. /* These are code and size indexed by symbol value */
  208. /* Set all codeless symbols to have code length 0;
  209. * this lets us detect duplicate VAL entries here, and later
  210. * allows emit_bits to detect any attempt to emit such symbols.
  211. */
  212. MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi));
  213. /* This is also a convenient place to check for out-of-range
  214. * and duplicated VAL entries. We allow 0..255 for AC symbols
  215. * but only 0..15 for DC. (We could constrain them further
  216. * based on data depth and mode, but this seems enough.)
  217. */
  218. maxsymbol = isDC ? 15 : 255;
  219. for (p = 0; p < lastp; p++) {
  220. i = htbl->huffval[p];
  221. if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
  222. ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  223. dtbl->ehufco[i] = huffcode[p];
  224. dtbl->ehufsi[i] = huffsize[p];
  225. }
  226. }
  227. /* Outputting bytes to the file */
  228. /* Emit a byte, taking 'action' if must suspend. */
  229. #define emit_byte(state,val,action) \
  230. { *(state)->next_output_byte++ = (JOCTET) (val); \
  231. if (--(state)->free_in_buffer == 0) \
  232. if (! dump_buffer(state)) \
  233. { action; } }
  234. LOCAL(boolean)
  235. dump_buffer (working_state * state)
  236. /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
  237. {
  238. struct jpeg_destination_mgr * dest = state->cinfo->dest;
  239. if (! (*dest->empty_output_buffer) (state->cinfo))
  240. return FALSE;
  241. /* After a successful buffer dump, must reset buffer pointers */
  242. state->next_output_byte = dest->next_output_byte;
  243. state->free_in_buffer = dest->free_in_buffer;
  244. return TRUE;
  245. }
  246. /* Outputting bits to the file */
  247. /* Only the right 24 bits of put_buffer are used; the valid bits are
  248. * left-justified in this part. At most 16 bits can be passed to emit_bits
  249. * in one call, and we never retain more than 7 bits in put_buffer
  250. * between calls, so 24 bits are sufficient.
  251. */
  252. INLINE
  253. LOCAL(boolean)
  254. emit_bits (working_state * state, unsigned int code, int size)
  255. /* Emit some bits; return TRUE if successful, FALSE if must suspend */
  256. {
  257. /* This routine is heavily used, so it's worth coding tightly. */
  258. register INT32 put_buffer = (INT32) code;
  259. register int put_bits = state->cur.put_bits;
  260. /* if size is 0, caller used an invalid Huffman table entry */
  261. if (size == 0)
  262. ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);
  263. put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
  264. put_bits += size; /* new number of bits in buffer */
  265. put_buffer <<= 24 - put_bits; /* align incoming bits */
  266. put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
  267. while (put_bits >= 8) {
  268. int c = (int) ((put_buffer >> 16) & 0xFF);
  269. emit_byte(state, c, return FALSE);
  270. if (c == 0xFF) { /* need to stuff a zero byte? */
  271. emit_byte(state, 0, return FALSE);
  272. }
  273. put_buffer <<= 8;
  274. put_bits -= 8;
  275. }
  276. state->cur.put_buffer = put_buffer; /* update state variables */
  277. state->cur.put_bits = put_bits;
  278. return TRUE;
  279. }
  280. LOCAL(boolean)
  281. flush_bits (working_state * state)
  282. {
  283. if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
  284. return FALSE;
  285. state->cur.put_buffer = 0; /* and reset bit-buffer to empty */
  286. state->cur.put_bits = 0;
  287. return TRUE;
  288. }
  289. /* Encode a single block's worth of coefficients */
  290. LOCAL(boolean)
  291. encode_one_block (working_state * state, JCOEFPTR block, int last_dc_val,
  292. c_derived_tbl *dctbl, c_derived_tbl *actbl)
  293. {
  294. register int temp, temp2;
  295. register int nbits;
  296. register int k, r, i;
  297. /* Encode the DC coefficient difference per section F.1.2.1 */
  298. temp = temp2 = block[0] - last_dc_val;
  299. if (temp < 0) {
  300. temp = -temp; /* temp is abs value of input */
  301. /* For a negative input, want temp2 = bitwise complement of abs(input) */
  302. /* This code assumes we are on a two's complement machine */
  303. temp2--;
  304. }
  305. /* Find the number of bits needed for the magnitude of the coefficient */
  306. nbits = 0;
  307. while (temp) {
  308. nbits++;
  309. temp >>= 1;
  310. }
  311. /* Check for out-of-range coefficient values.
  312. * Since we're encoding a difference, the range limit is twice as much.
  313. */
  314. if (nbits > MAX_COEF_BITS+1)
  315. ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
  316. /* Emit the Huffman-coded symbol for the number of bits */
  317. if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
  318. return FALSE;
  319. /* Emit that number of bits of the value, if positive, */
  320. /* or the complement of its magnitude, if negative. */
  321. if (nbits) /* emit_bits rejects calls with size 0 */
  322. if (! emit_bits(state, (unsigned int) temp2, nbits))
  323. return FALSE;
  324. /* Encode the AC coefficients per section F.1.2.2 */
  325. r = 0; /* r = run length of zeros */
  326. for (k = 1; k < DCTSIZE2; k++) {
  327. if ((temp = block[jpeg_natural_order[k]]) == 0) {
  328. r++;
  329. } else {
  330. /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  331. while (r > 15) {
  332. if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
  333. return FALSE;
  334. r -= 16;
  335. }
  336. temp2 = temp;
  337. if (temp < 0) {
  338. temp = -temp; /* temp is abs value of input */
  339. /* This code assumes we are on a two's complement machine */
  340. temp2--;
  341. }
  342. /* Find the number of bits needed for the magnitude of the coefficient */
  343. nbits = 1; /* there must be at least one 1 bit */
  344. while ((temp >>= 1))
  345. nbits++;
  346. /* Check for out-of-range coefficient values */
  347. if (nbits > MAX_COEF_BITS)
  348. ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
  349. /* Emit Huffman symbol for run length / number of bits */
  350. i = (r << 4) + nbits;
  351. if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
  352. return FALSE;
  353. /* Emit that number of bits of the value, if positive, */
  354. /* or the complement of its magnitude, if negative. */
  355. if (! emit_bits(state, (unsigned int) temp2, nbits))
  356. return FALSE;
  357. r = 0;
  358. }
  359. }
  360. /* If the last coef(s) were zero, emit an end-of-block code */
  361. if (r > 0)
  362. if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
  363. return FALSE;
  364. return TRUE;
  365. }
  366. /*
  367. * Emit a restart marker & resynchronize predictions.
  368. */
  369. LOCAL(boolean)
  370. emit_restart (working_state * state, int restart_num)
  371. {
  372. int ci;
  373. if (! flush_bits(state))
  374. return FALSE;
  375. emit_byte(state, 0xFF, return FALSE);
  376. emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
  377. /* Re-initialize DC predictions to 0 */
  378. for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
  379. state->cur.last_dc_val[ci] = 0;
  380. /* The restart counter is not updated until we successfully write the MCU. */
  381. return TRUE;
  382. }
  383. /*
  384. * Encode and output one MCU's worth of Huffman-compressed coefficients.
  385. */
  386. METHODDEF(boolean)
  387. encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  388. {
  389. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  390. working_state state;
  391. int blkn, ci;
  392. jpeg_component_info * compptr;
  393. /* Load up working state */
  394. state.next_output_byte = cinfo->dest->next_output_byte;
  395. state.free_in_buffer = cinfo->dest->free_in_buffer;
  396. ASSIGN_STATE(state.cur, entropy->saved);
  397. state.cinfo = cinfo;
  398. /* Emit restart marker if needed */
  399. if (cinfo->restart_interval) {
  400. if (entropy->restarts_to_go == 0)
  401. if (! emit_restart(&state, entropy->next_restart_num))
  402. return FALSE;
  403. }
  404. /* Encode the MCU data blocks */
  405. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  406. ci = cinfo->MCU_membership[blkn];
  407. compptr = cinfo->cur_comp_info[ci];
  408. if (! encode_one_block(&state,
  409. MCU_data[blkn][0], state.cur.last_dc_val[ci],
  410. entropy->dc_derived_tbls[compptr->dc_tbl_no],
  411. entropy->ac_derived_tbls[compptr->ac_tbl_no]))
  412. return FALSE;
  413. /* Update last_dc_val */
  414. state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
  415. }
  416. /* Completed MCU, so update state */
  417. cinfo->dest->next_output_byte = state.next_output_byte;
  418. cinfo->dest->free_in_buffer = state.free_in_buffer;
  419. ASSIGN_STATE(entropy->saved, state.cur);
  420. /* Update restart-interval state too */
  421. if (cinfo->restart_interval) {
  422. if (entropy->restarts_to_go == 0) {
  423. entropy->restarts_to_go = cinfo->restart_interval;
  424. entropy->next_restart_num++;
  425. entropy->next_restart_num &= 7;
  426. }
  427. entropy->restarts_to_go--;
  428. }
  429. return TRUE;
  430. }
  431. /*
  432. * Finish up at the end of a Huffman-compressed scan.
  433. */
  434. METHODDEF(void)
  435. finish_pass_huff (j_compress_ptr cinfo)
  436. {
  437. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  438. working_state state;
  439. /* Load up working state ... flush_bits needs it */
  440. state.next_output_byte = cinfo->dest->next_output_byte;
  441. state.free_in_buffer = cinfo->dest->free_in_buffer;
  442. ASSIGN_STATE(state.cur, entropy->saved);
  443. state.cinfo = cinfo;
  444. /* Flush out the last data */
  445. if (! flush_bits(&state))
  446. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  447. /* Update state */
  448. cinfo->dest->next_output_byte = state.next_output_byte;
  449. cinfo->dest->free_in_buffer = state.free_in_buffer;
  450. ASSIGN_STATE(entropy->saved, state.cur);
  451. }
  452. /*
  453. * Huffman coding optimization.
  454. *
  455. * We first scan the supplied data and count the number of uses of each symbol
  456. * that is to be Huffman-coded. (This process MUST agree with the code above.)
  457. * Then we build a Huffman coding tree for the observed counts.
  458. * Symbols which are not needed at all for the particular image are not
  459. * assigned any code, which saves space in the DHT marker as well as in
  460. * the compressed data.
  461. */
  462. #ifdef ENTROPY_OPT_SUPPORTED
  463. /* Process a single block's worth of coefficients */
  464. LOCAL(void)
  465. htest_one_block (j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
  466. long dc_counts[], long ac_counts[])
  467. {
  468. register int temp;
  469. register int nbits;
  470. register int k, r;
  471. /* Encode the DC coefficient difference per section F.1.2.1 */
  472. temp = block[0] - last_dc_val;
  473. if (temp < 0)
  474. temp = -temp;
  475. /* Find the number of bits needed for the magnitude of the coefficient */
  476. nbits = 0;
  477. while (temp) {
  478. nbits++;
  479. temp >>= 1;
  480. }
  481. /* Check for out-of-range coefficient values.
  482. * Since we're encoding a difference, the range limit is twice as much.
  483. */
  484. if (nbits > MAX_COEF_BITS+1)
  485. ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  486. /* Count the Huffman symbol for the number of bits */
  487. dc_counts[nbits]++;
  488. /* Encode the AC coefficients per section F.1.2.2 */
  489. r = 0; /* r = run length of zeros */
  490. for (k = 1; k < DCTSIZE2; k++) {
  491. if ((temp = block[jpeg_natural_order[k]]) == 0) {
  492. r++;
  493. } else {
  494. /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  495. while (r > 15) {
  496. ac_counts[0xF0]++;
  497. r -= 16;
  498. }
  499. /* Find the number of bits needed for the magnitude of the coefficient */
  500. if (temp < 0)
  501. temp = -temp;
  502. /* Find the number of bits needed for the magnitude of the coefficient */
  503. nbits = 1; /* there must be at least one 1 bit */
  504. while ((temp >>= 1))
  505. nbits++;
  506. /* Check for out-of-range coefficient values */
  507. if (nbits > MAX_COEF_BITS)
  508. ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  509. /* Count Huffman symbol for run length / number of bits */
  510. ac_counts[(r << 4) + nbits]++;
  511. r = 0;
  512. }
  513. }
  514. /* If the last coef(s) were zero, emit an end-of-block code */
  515. if (r > 0)
  516. ac_counts[0]++;
  517. }
  518. /*
  519. * Trial-encode one MCU's worth of Huffman-compressed coefficients.
  520. * No data is actually output, so no suspension return is possible.
  521. */
  522. METHODDEF(boolean)
  523. encode_mcu_gather (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  524. {
  525. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  526. int blkn, ci;
  527. jpeg_component_info * compptr;
  528. /* Take care of restart intervals if needed */
  529. if (cinfo->restart_interval) {
  530. if (entropy->restarts_to_go == 0) {
  531. /* Re-initialize DC predictions to 0 */
  532. for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  533. entropy->saved.last_dc_val[ci] = 0;
  534. /* Update restart state */
  535. entropy->restarts_to_go = cinfo->restart_interval;
  536. }
  537. entropy->restarts_to_go--;
  538. }
  539. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  540. ci = cinfo->MCU_membership[blkn];
  541. compptr = cinfo->cur_comp_info[ci];
  542. htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
  543. entropy->dc_count_ptrs[compptr->dc_tbl_no],
  544. entropy->ac_count_ptrs[compptr->ac_tbl_no]);
  545. entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
  546. }
  547. return TRUE;
  548. }
  549. /*
  550. * Generate the best Huffman code table for the given counts, fill htbl.
  551. * Note this is also used by jcphuff.c.
  552. *
  553. * The JPEG standard requires that no symbol be assigned a codeword of all
  554. * one bits (so that padding bits added at the end of a compressed segment
  555. * can't look like a valid code). Because of the canonical ordering of
  556. * codewords, this just means that there must be an unused slot in the
  557. * longest codeword length category. Section K.2 of the JPEG spec suggests
  558. * reserving such a slot by pretending that symbol 256 is a valid symbol
  559. * with count 1. In theory that's not optimal; giving it count zero but
  560. * including it in the symbol set anyway should give a better Huffman code.
  561. * But the theoretically better code actually seems to come out worse in
  562. * practice, because it produces more all-ones bytes (which incur stuffed
  563. * zero bytes in the final file). In any case the difference is tiny.
  564. *
  565. * The JPEG standard requires Huffman codes to be no more than 16 bits long.
  566. * If some symbols have a very small but nonzero probability, the Huffman tree
  567. * must be adjusted to meet the code length restriction. We currently use
  568. * the adjustment method suggested in JPEG section K.2. This method is *not*
  569. * optimal; it may not choose the best possible limited-length code. But
  570. * typically only very-low-frequency symbols will be given less-than-optimal
  571. * lengths, so the code is almost optimal. Experimental comparisons against
  572. * an optimal limited-length-code algorithm indicate that the difference is
  573. * microscopic --- usually less than a hundredth of a percent of total size.
  574. * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
  575. */
  576. GLOBAL(void)
  577. jpeg_gen_optimal_table (j_compress_ptr cinfo, JHUFF_TBL * htbl, long freq[])
  578. {
  579. #define MAX_CLEN 32 /* assumed maximum initial code length */
  580. UINT8 bits[MAX_CLEN+1]; /* bits[k] = # of symbols with code length k */
  581. int codesize[257]; /* codesize[k] = code length of symbol k */
  582. int others[257]; /* next symbol in current branch of tree */
  583. int c1, c2;
  584. int p, i, j;
  585. long v;
  586. /* This algorithm is explained in section K.2 of the JPEG standard */
  587. MEMZERO(bits, SIZEOF(bits));
  588. MEMZERO(codesize, SIZEOF(codesize));
  589. for (i = 0; i < 257; i++)
  590. others[i] = -1; /* init links to empty */
  591. freq[256] = 1; /* make sure 256 has a nonzero count */
  592. /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
  593. * that no real symbol is given code-value of all ones, because 256
  594. * will be placed last in the largest codeword category.
  595. */
  596. /* Huffman's basic algorithm to assign optimal code lengths to symbols */
  597. for (;;) {
  598. /* Find the smallest nonzero frequency, set c1 = its symbol */
  599. /* In case of ties, take the larger symbol number */
  600. c1 = -1;
  601. v = 1000000000L;
  602. for (i = 0; i <= 256; i++) {
  603. if (freq[i] && freq[i] <= v) {
  604. v = freq[i];
  605. c1 = i;
  606. }
  607. }
  608. /* Find the next smallest nonzero frequency, set c2 = its symbol */
  609. /* In case of ties, take the larger symbol number */
  610. c2 = -1;
  611. v = 1000000000L;
  612. for (i = 0; i <= 256; i++) {
  613. if (freq[i] && freq[i] <= v && i != c1) {
  614. v = freq[i];
  615. c2 = i;
  616. }
  617. }
  618. /* Done if we've merged everything into one frequency */
  619. if (c2 < 0)
  620. break;
  621. /* Else merge the two counts/trees */
  622. freq[c1] += freq[c2];
  623. freq[c2] = 0;
  624. /* Increment the codesize of everything in c1's tree branch */
  625. codesize[c1]++;
  626. while (others[c1] >= 0) {
  627. c1 = others[c1];
  628. codesize[c1]++;
  629. }
  630. others[c1] = c2; /* chain c2 onto c1's tree branch */
  631. /* Increment the codesize of everything in c2's tree branch */
  632. codesize[c2]++;
  633. while (others[c2] >= 0) {
  634. c2 = others[c2];
  635. codesize[c2]++;
  636. }
  637. }
  638. /* Now count the number of symbols of each code length */
  639. for (i = 0; i <= 256; i++) {
  640. if (codesize[i]) {
  641. /* The JPEG standard seems to think that this can't happen, */
  642. /* but I'm paranoid... */
  643. if (codesize[i] > MAX_CLEN)
  644. ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
  645. bits[codesize[i]]++;
  646. }
  647. }
  648. /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
  649. * Huffman procedure assigned any such lengths, we must adjust the coding.
  650. * Here is what the JPEG spec says about how this next bit works:
  651. * Since symbols are paired for the longest Huffman code, the symbols are
  652. * removed from this length category two at a time. The prefix for the pair
  653. * (which is one bit shorter) is allocated to one of the pair; then,
  654. * skipping the BITS entry for that prefix length, a code word from the next
  655. * shortest nonzero BITS entry is converted into a prefix for two code words
  656. * one bit longer.
  657. */
  658. for (i = MAX_CLEN; i > 16; i--) {
  659. while (bits[i] > 0) {
  660. j = i - 2; /* find length of new prefix to be used */
  661. while (bits[j] == 0)
  662. j--;
  663. bits[i] -= 2; /* remove two symbols */
  664. bits[i-1]++; /* one goes in this length */
  665. bits[j+1] += 2; /* two new symbols in this length */
  666. bits[j]--; /* symbol of this length is now a prefix */
  667. }
  668. }
  669. /* Remove the count for the pseudo-symbol 256 from the largest codelength */
  670. while (bits[i] == 0) /* find largest codelength still in use */
  671. i--;
  672. bits[i]--;
  673. /* Return final symbol counts (only for lengths 0..16) */
  674. MEMCOPY(htbl->bits, bits, SIZEOF(htbl->bits));
  675. /* Return a list of the symbols sorted by code length */
  676. /* It's not real clear to me why we don't need to consider the codelength
  677. * changes made above, but the JPEG spec seems to think this works.
  678. */
  679. p = 0;
  680. for (i = 1; i <= MAX_CLEN; i++) {
  681. for (j = 0; j <= 255; j++) {
  682. if (codesize[j] == i) {
  683. htbl->huffval[p] = (UINT8) j;
  684. p++;
  685. }
  686. }
  687. }
  688. /* Set sent_table FALSE so updated table will be written to JPEG file. */
  689. htbl->sent_table = FALSE;
  690. }
  691. /*
  692. * Finish up a statistics-gathering pass and create the new Huffman tables.
  693. */
  694. METHODDEF(void)
  695. finish_pass_gather (j_compress_ptr cinfo)
  696. {
  697. huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  698. int ci, dctbl, actbl;
  699. jpeg_component_info * compptr;
  700. JHUFF_TBL **htblptr;
  701. boolean did_dc[NUM_HUFF_TBLS];
  702. boolean did_ac[NUM_HUFF_TBLS];
  703. /* It's important not to apply jpeg_gen_optimal_table more than once
  704. * per table, because it clobbers the input frequency counts!
  705. */
  706. MEMZERO(did_dc, SIZEOF(did_dc));
  707. MEMZERO(did_ac, SIZEOF(did_ac));
  708. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  709. compptr = cinfo->cur_comp_info[ci];
  710. dctbl = compptr->dc_tbl_no;
  711. actbl = compptr->ac_tbl_no;
  712. if (! did_dc[dctbl]) {
  713. htblptr = & cinfo->dc_huff_tbl_ptrs[dctbl];
  714. if (*htblptr == NULL)
  715. *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  716. jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
  717. did_dc[dctbl] = TRUE;
  718. }
  719. if (! did_ac[actbl]) {
  720. htblptr = & cinfo->ac_huff_tbl_ptrs[actbl];
  721. if (*htblptr == NULL)
  722. *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  723. jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
  724. did_ac[actbl] = TRUE;
  725. }
  726. }
  727. }
  728. #endif /* ENTROPY_OPT_SUPPORTED */
  729. /*
  730. * Module initialization routine for Huffman entropy encoding.
  731. */
  732. GLOBAL(void)
  733. jinit_huff_encoder (j_compress_ptr cinfo)
  734. {
  735. huff_entropy_ptr entropy;
  736. int i;
  737. entropy = (huff_entropy_ptr)
  738. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  739. SIZEOF(huff_entropy_encoder));
  740. cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  741. entropy->pub.start_pass = start_pass_huff;
  742. /* Mark tables unallocated */
  743. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  744. entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
  745. #ifdef ENTROPY_OPT_SUPPORTED
  746. entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
  747. #endif
  748. }
  749. }