Source code of Windows XP (NT5)
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.

832 lines
24 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jcphuff.c
  5. *
  6. * Copyright (C) 1995-1996, Thomas G. Lane.
  7. * This file is part of the Independent JPEG Group's software.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. * This file contains Huffman entropy encoding routines for progressive JPEG.
  11. *
  12. * We do not support output suspension in this module, since the library
  13. * currently does not allow multiple-scan files to be written with output
  14. * suspension.
  15. */
  16. #define JPEG_INTERNALS
  17. #include "jinclude.h"
  18. #include "jpeglib.h"
  19. #include "jchuff.h" /* Declarations shared with jchuff.c */
  20. #ifdef C_PROGRESSIVE_SUPPORTED
  21. /* Expanded entropy encoder object for progressive Huffman encoding. */
  22. typedef struct {
  23. struct jpeg_entropy_encoder pub; /* public fields */
  24. /* Mode flag: TRUE for optimization, FALSE for actual data output */
  25. boolean gather_statistics;
  26. /* Bit-level coding status.
  27. * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
  28. */
  29. JOCTET * next_output_byte; /* => next byte to write in buffer */
  30. size_t free_in_buffer; /* # of byte spaces remaining in buffer */
  31. INT32 put_buffer; /* current bit-accumulation buffer */
  32. int put_bits; /* # of bits now in it */
  33. j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */
  34. /* Coding status for DC components */
  35. int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
  36. /* Coding status for AC components */
  37. int ac_tbl_no; /* the table number of the single component */
  38. unsigned int EOBRUN; /* run length of EOBs */
  39. unsigned int BE; /* # of buffered correction bits before MCU */
  40. char * bit_buffer; /* buffer for correction bits (1 per char) */
  41. /* packing correction bits tightly would save some space but cost time... */
  42. unsigned int restarts_to_go; /* MCUs left in this restart interval */
  43. int next_restart_num; /* next restart number to write (0-7) */
  44. /* Pointers to derived tables (these workspaces have image lifespan).
  45. * Since any one scan codes only DC or only AC, we only need one set
  46. * of tables, not one for DC and one for AC.
  47. */
  48. c_derived_tbl * derived_tbls[NUM_HUFF_TBLS];
  49. /* Statistics tables for optimization; again, one set is enough */
  50. long * count_ptrs[NUM_HUFF_TBLS];
  51. } phuff_entropy_encoder;
  52. typedef phuff_entropy_encoder * phuff_entropy_ptr;
  53. /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
  54. * buffer can hold. Larger sizes may slightly improve compression, but
  55. * 1000 is already well into the realm of overkill.
  56. * The minimum safe size is 64 bits.
  57. */
  58. #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
  59. /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than INT32.
  60. * We assume that int right shift is unsigned if INT32 right shift is,
  61. * which should be safe.
  62. */
  63. #ifdef RIGHT_SHIFT_IS_UNSIGNED
  64. #define ISHIFT_TEMPS int ishift_temp;
  65. #define IRIGHT_SHIFT(x,shft) \
  66. ((ishift_temp = (x)) < 0 ? \
  67. (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
  68. (ishift_temp >> (shft)))
  69. #else
  70. #define ISHIFT_TEMPS
  71. #define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
  72. #endif
  73. /* Forward declarations */
  74. METHODDEF(boolean) encode_mcu_DC_first JPP((j_compress_ptr cinfo,
  75. JBLOCKROW *MCU_data));
  76. METHODDEF(boolean) encode_mcu_AC_first JPP((j_compress_ptr cinfo,
  77. JBLOCKROW *MCU_data));
  78. METHODDEF(boolean) encode_mcu_DC_refine JPP((j_compress_ptr cinfo,
  79. JBLOCKROW *MCU_data));
  80. METHODDEF(boolean) encode_mcu_AC_refine JPP((j_compress_ptr cinfo,
  81. JBLOCKROW *MCU_data));
  82. METHODDEF(void) finish_pass_phuff JPP((j_compress_ptr cinfo));
  83. METHODDEF(void) finish_pass_gather_phuff JPP((j_compress_ptr cinfo));
  84. /*
  85. * Initialize for a Huffman-compressed scan using progressive JPEG.
  86. */
  87. METHODDEF(void)
  88. start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
  89. {
  90. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  91. boolean is_DC_band;
  92. int ci, tbl;
  93. jpeg_component_info * compptr;
  94. entropy->cinfo = cinfo;
  95. entropy->gather_statistics = gather_statistics;
  96. is_DC_band = (cinfo->Ss == 0);
  97. /* We assume jcmaster.c already validated the scan parameters. */
  98. /* Select execution routines */
  99. if (cinfo->Ah == 0) {
  100. if (is_DC_band)
  101. entropy->pub.encode_mcu = encode_mcu_DC_first;
  102. else
  103. entropy->pub.encode_mcu = encode_mcu_AC_first;
  104. } else {
  105. if (is_DC_band)
  106. entropy->pub.encode_mcu = encode_mcu_DC_refine;
  107. else {
  108. entropy->pub.encode_mcu = encode_mcu_AC_refine;
  109. /* AC refinement needs a correction bit buffer */
  110. if (entropy->bit_buffer == NULL)
  111. entropy->bit_buffer = (char *)
  112. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  113. MAX_CORR_BITS * SIZEOF(char));
  114. }
  115. }
  116. if (gather_statistics)
  117. entropy->pub.finish_pass = finish_pass_gather_phuff;
  118. else
  119. entropy->pub.finish_pass = finish_pass_phuff;
  120. /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
  121. * for AC coefficients.
  122. */
  123. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  124. compptr = cinfo->cur_comp_info[ci];
  125. /* Initialize DC predictions to 0 */
  126. entropy->last_dc_val[ci] = 0;
  127. /* Make sure requested tables are present */
  128. /* (In gather mode, tables need not be allocated yet) */
  129. if (is_DC_band) {
  130. if (cinfo->Ah != 0) /* DC refinement needs no table */
  131. continue;
  132. tbl = compptr->dc_tbl_no;
  133. if (tbl < 0 || tbl >= NUM_HUFF_TBLS ||
  134. (cinfo->dc_huff_tbl_ptrs[tbl] == NULL && !gather_statistics))
  135. ERREXIT1(cinfo,JERR_NO_HUFF_TABLE, tbl);
  136. } else {
  137. entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
  138. if (tbl < 0 || tbl >= NUM_HUFF_TBLS ||
  139. (cinfo->ac_huff_tbl_ptrs[tbl] == NULL && !gather_statistics))
  140. ERREXIT1(cinfo,JERR_NO_HUFF_TABLE, tbl);
  141. }
  142. if (gather_statistics) {
  143. /* Allocate and zero the statistics tables */
  144. /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
  145. if (entropy->count_ptrs[tbl] == NULL)
  146. entropy->count_ptrs[tbl] = (long *)
  147. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  148. 257 * SIZEOF(long));
  149. MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
  150. } else {
  151. /* Compute derived values for Huffman tables */
  152. /* We may do this more than once for a table, but it's not expensive */
  153. if (is_DC_band)
  154. jpeg_make_c_derived_tbl(cinfo, cinfo->dc_huff_tbl_ptrs[tbl],
  155. & entropy->derived_tbls[tbl]);
  156. else
  157. jpeg_make_c_derived_tbl(cinfo, cinfo->ac_huff_tbl_ptrs[tbl],
  158. & entropy->derived_tbls[tbl]);
  159. }
  160. }
  161. /* Initialize AC stuff */
  162. entropy->EOBRUN = 0;
  163. entropy->BE = 0;
  164. /* Initialize bit buffer to empty */
  165. entropy->put_buffer = 0;
  166. entropy->put_bits = 0;
  167. /* Initialize restart stuff */
  168. entropy->restarts_to_go = cinfo->restart_interval;
  169. entropy->next_restart_num = 0;
  170. }
  171. /* Outputting bytes to the file.
  172. * NB: these must be called only when actually outputting,
  173. * that is, entropy->gather_statistics == FALSE.
  174. */
  175. /* Emit a byte */
  176. #define emit_byte(entropy,val) \
  177. { *(entropy)->next_output_byte++ = (JOCTET) (val); \
  178. if (--(entropy)->free_in_buffer == 0) \
  179. dump_buffer(entropy); }
  180. LOCAL(void)
  181. dump_buffer (phuff_entropy_ptr entropy)
  182. /* Empty the output buffer; we do not support suspension in this module. */
  183. {
  184. struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
  185. if (! (*dest->empty_output_buffer) (entropy->cinfo))
  186. ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
  187. /* After a successful buffer dump, must reset buffer pointers */
  188. entropy->next_output_byte = dest->next_output_byte;
  189. entropy->free_in_buffer = dest->free_in_buffer;
  190. }
  191. /* Outputting bits to the file */
  192. /* Only the right 24 bits of put_buffer are used; the valid bits are
  193. * left-justified in this part. At most 16 bits can be passed to emit_bits
  194. * in one call, and we never retain more than 7 bits in put_buffer
  195. * between calls, so 24 bits are sufficient.
  196. */
  197. INLINE
  198. LOCAL(void)
  199. emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
  200. /* Emit some bits, unless we are in gather mode */
  201. {
  202. /* This routine is heavily used, so it's worth coding tightly. */
  203. register INT32 put_buffer = (INT32) code;
  204. register int put_bits = entropy->put_bits;
  205. /* if size is 0, caller used an invalid Huffman table entry */
  206. if (size == 0)
  207. ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  208. if (entropy->gather_statistics)
  209. return; /* do nothing if we're only getting stats */
  210. put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
  211. put_bits += size; /* new number of bits in buffer */
  212. put_buffer <<= 24 - put_bits; /* align incoming bits */
  213. put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
  214. while (put_bits >= 8) {
  215. int c = (int) ((put_buffer >> 16) & 0xFF);
  216. emit_byte(entropy, c);
  217. if (c == 0xFF) { /* need to stuff a zero byte? */
  218. emit_byte(entropy, 0);
  219. }
  220. put_buffer <<= 8;
  221. put_bits -= 8;
  222. }
  223. entropy->put_buffer = put_buffer; /* update variables */
  224. entropy->put_bits = put_bits;
  225. }
  226. LOCAL(void)
  227. flush_bits (phuff_entropy_ptr entropy)
  228. {
  229. emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
  230. entropy->put_buffer = 0; /* and reset bit-buffer to empty */
  231. entropy->put_bits = 0;
  232. }
  233. /*
  234. * Emit (or just count) a Huffman symbol.
  235. */
  236. INLINE
  237. LOCAL(void)
  238. emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
  239. {
  240. if (entropy->gather_statistics)
  241. entropy->count_ptrs[tbl_no][symbol]++;
  242. else {
  243. c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
  244. emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
  245. }
  246. }
  247. /*
  248. * Emit bits from a correction bit buffer.
  249. */
  250. LOCAL(void)
  251. emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
  252. unsigned int nbits)
  253. {
  254. if (entropy->gather_statistics)
  255. return; /* no real work */
  256. while (nbits > 0) {
  257. emit_bits(entropy, (unsigned int) (*bufstart), 1);
  258. bufstart++;
  259. nbits--;
  260. }
  261. }
  262. /*
  263. * Emit any pending EOBRUN symbol.
  264. */
  265. LOCAL(void)
  266. emit_eobrun (phuff_entropy_ptr entropy)
  267. {
  268. register int temp, nbits;
  269. if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
  270. temp = entropy->EOBRUN;
  271. nbits = 0;
  272. while ((temp >>= 1))
  273. nbits++;
  274. emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
  275. if (nbits)
  276. emit_bits(entropy, entropy->EOBRUN, nbits);
  277. entropy->EOBRUN = 0;
  278. /* Emit any buffered correction bits */
  279. emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
  280. entropy->BE = 0;
  281. }
  282. }
  283. /*
  284. * Emit a restart marker & resynchronize predictions.
  285. */
  286. LOCAL(void)
  287. emit_restart (phuff_entropy_ptr entropy, int restart_num)
  288. {
  289. int ci;
  290. emit_eobrun(entropy);
  291. if (! entropy->gather_statistics) {
  292. flush_bits(entropy);
  293. emit_byte(entropy, 0xFF);
  294. emit_byte(entropy, JPEG_RST0 + restart_num);
  295. }
  296. if (entropy->cinfo->Ss == 0) {
  297. /* Re-initialize DC predictions to 0 */
  298. for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
  299. entropy->last_dc_val[ci] = 0;
  300. } else {
  301. /* Re-initialize all AC-related fields to 0 */
  302. entropy->EOBRUN = 0;
  303. entropy->BE = 0;
  304. }
  305. }
  306. /*
  307. * MCU encoding for DC initial scan (either spectral selection,
  308. * or first pass of successive approximation).
  309. */
  310. METHODDEF(boolean)
  311. encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  312. {
  313. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  314. register int temp, temp2;
  315. register int nbits;
  316. int blkn, ci;
  317. int Al = cinfo->Al;
  318. JBLOCKROW block;
  319. jpeg_component_info * compptr;
  320. ISHIFT_TEMPS
  321. entropy->next_output_byte = cinfo->dest->next_output_byte;
  322. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  323. /* Emit restart marker if needed */
  324. if (cinfo->restart_interval)
  325. if (entropy->restarts_to_go == 0)
  326. emit_restart(entropy, entropy->next_restart_num);
  327. /* Encode the MCU data blocks */
  328. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  329. block = MCU_data[blkn];
  330. ci = cinfo->MCU_membership[blkn];
  331. compptr = cinfo->cur_comp_info[ci];
  332. /* Compute the DC value after the required point transform by Al.
  333. * This is simply an arithmetic right shift.
  334. */
  335. temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
  336. /* DC differences are figured on the point-transformed values. */
  337. temp = temp2 - entropy->last_dc_val[ci];
  338. entropy->last_dc_val[ci] = temp2;
  339. /* Encode the DC coefficient difference per section G.1.2.1 */
  340. temp2 = temp;
  341. if (temp < 0) {
  342. temp = -temp; /* temp is abs value of input */
  343. /* For a negative input, want temp2 = bitwise complement of abs(input) */
  344. /* This code assumes we are on a two's complement machine */
  345. temp2--;
  346. }
  347. /* Find the number of bits needed for the magnitude of the coefficient */
  348. nbits = 0;
  349. while (temp) {
  350. nbits++;
  351. temp >>= 1;
  352. }
  353. /* Count/emit the Huffman-coded symbol for the number of bits */
  354. emit_symbol(entropy, compptr->dc_tbl_no, nbits);
  355. /* Emit that number of bits of the value, if positive, */
  356. /* or the complement of its magnitude, if negative. */
  357. if (nbits) /* emit_bits rejects calls with size 0 */
  358. emit_bits(entropy, (unsigned int) temp2, nbits);
  359. }
  360. cinfo->dest->next_output_byte = entropy->next_output_byte;
  361. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  362. /* Update restart-interval state too */
  363. if (cinfo->restart_interval) {
  364. if (entropy->restarts_to_go == 0) {
  365. entropy->restarts_to_go = cinfo->restart_interval;
  366. entropy->next_restart_num++;
  367. entropy->next_restart_num &= 7;
  368. }
  369. entropy->restarts_to_go--;
  370. }
  371. return TRUE;
  372. }
  373. /*
  374. * MCU encoding for AC initial scan (either spectral selection,
  375. * or first pass of successive approximation).
  376. */
  377. METHODDEF(boolean)
  378. encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  379. {
  380. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  381. register int temp, temp2;
  382. register int nbits;
  383. register int r, k;
  384. int Se = cinfo->Se;
  385. int Al = cinfo->Al;
  386. JBLOCKROW block;
  387. entropy->next_output_byte = cinfo->dest->next_output_byte;
  388. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  389. /* Emit restart marker if needed */
  390. if (cinfo->restart_interval)
  391. if (entropy->restarts_to_go == 0)
  392. emit_restart(entropy, entropy->next_restart_num);
  393. /* Encode the MCU data block */
  394. block = MCU_data[0];
  395. /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
  396. r = 0; /* r = run length of zeros */
  397. for (k = cinfo->Ss; k <= Se; k++) {
  398. if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
  399. r++;
  400. continue;
  401. }
  402. /* We must apply the point transform by Al. For AC coefficients this
  403. * is an integer division with rounding towards 0. To do this portably
  404. * in C, we shift after obtaining the absolute value; so the code is
  405. * interwoven with finding the abs value (temp) and output bits (temp2).
  406. */
  407. if (temp < 0) {
  408. temp = -temp; /* temp is abs value of input */
  409. temp >>= Al; /* apply the point transform */
  410. /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
  411. temp2 = ~temp;
  412. } else {
  413. temp >>= Al; /* apply the point transform */
  414. temp2 = temp;
  415. }
  416. /* Watch out for case that nonzero coef is zero after point transform */
  417. if (temp == 0) {
  418. r++;
  419. continue;
  420. }
  421. /* Emit any pending EOBRUN */
  422. if (entropy->EOBRUN > 0)
  423. emit_eobrun(entropy);
  424. /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  425. while (r > 15) {
  426. emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
  427. r -= 16;
  428. }
  429. /* Find the number of bits needed for the magnitude of the coefficient */
  430. nbits = 1; /* there must be at least one 1 bit */
  431. while ((temp >>= 1))
  432. nbits++;
  433. /* Count/emit Huffman symbol for run length / number of bits */
  434. emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
  435. /* Emit that number of bits of the value, if positive, */
  436. /* or the complement of its magnitude, if negative. */
  437. emit_bits(entropy, (unsigned int) temp2, nbits);
  438. r = 0; /* reset zero run length */
  439. }
  440. if (r > 0) { /* If there are trailing zeroes, */
  441. entropy->EOBRUN++; /* count an EOB */
  442. if (entropy->EOBRUN == 0x7FFF)
  443. emit_eobrun(entropy); /* force it out to avoid overflow */
  444. }
  445. cinfo->dest->next_output_byte = entropy->next_output_byte;
  446. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  447. /* Update restart-interval state too */
  448. if (cinfo->restart_interval) {
  449. if (entropy->restarts_to_go == 0) {
  450. entropy->restarts_to_go = cinfo->restart_interval;
  451. entropy->next_restart_num++;
  452. entropy->next_restart_num &= 7;
  453. }
  454. entropy->restarts_to_go--;
  455. }
  456. return TRUE;
  457. }
  458. /*
  459. * MCU encoding for DC successive approximation refinement scan.
  460. * Note: we assume such scans can be multi-component, although the spec
  461. * is not very clear on the point.
  462. */
  463. METHODDEF(boolean)
  464. encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  465. {
  466. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  467. register int temp;
  468. int blkn;
  469. int Al = cinfo->Al;
  470. JBLOCKROW block;
  471. entropy->next_output_byte = cinfo->dest->next_output_byte;
  472. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  473. /* Emit restart marker if needed */
  474. if (cinfo->restart_interval)
  475. if (entropy->restarts_to_go == 0)
  476. emit_restart(entropy, entropy->next_restart_num);
  477. /* Encode the MCU data blocks */
  478. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  479. block = MCU_data[blkn];
  480. /* We simply emit the Al'th bit of the DC coefficient value. */
  481. temp = (*block)[0];
  482. emit_bits(entropy, (unsigned int) (temp >> Al), 1);
  483. }
  484. cinfo->dest->next_output_byte = entropy->next_output_byte;
  485. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  486. /* Update restart-interval state too */
  487. if (cinfo->restart_interval) {
  488. if (entropy->restarts_to_go == 0) {
  489. entropy->restarts_to_go = cinfo->restart_interval;
  490. entropy->next_restart_num++;
  491. entropy->next_restart_num &= 7;
  492. }
  493. entropy->restarts_to_go--;
  494. }
  495. return TRUE;
  496. }
  497. /*
  498. * MCU encoding for AC successive approximation refinement scan.
  499. */
  500. METHODDEF(boolean)
  501. encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  502. {
  503. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  504. register int temp;
  505. register int r, k;
  506. int EOB;
  507. char *BR_buffer;
  508. unsigned int BR;
  509. int Se = cinfo->Se;
  510. int Al = cinfo->Al;
  511. JBLOCKROW block;
  512. int absvalues[DCTSIZE2];
  513. entropy->next_output_byte = cinfo->dest->next_output_byte;
  514. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  515. /* Emit restart marker if needed */
  516. if (cinfo->restart_interval)
  517. if (entropy->restarts_to_go == 0)
  518. emit_restart(entropy, entropy->next_restart_num);
  519. /* Encode the MCU data block */
  520. block = MCU_data[0];
  521. /* It is convenient to make a pre-pass to determine the transformed
  522. * coefficients' absolute values and the EOB position.
  523. */
  524. EOB = 0;
  525. for (k = cinfo->Ss; k <= Se; k++) {
  526. temp = (*block)[jpeg_natural_order[k]];
  527. /* We must apply the point transform by Al. For AC coefficients this
  528. * is an integer division with rounding towards 0. To do this portably
  529. * in C, we shift after obtaining the absolute value.
  530. */
  531. if (temp < 0)
  532. temp = -temp; /* temp is abs value of input */
  533. temp >>= Al; /* apply the point transform */
  534. absvalues[k] = temp; /* save abs value for main pass */
  535. if (temp == 1)
  536. EOB = k; /* EOB = index of last newly-nonzero coef */
  537. }
  538. /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
  539. r = 0; /* r = run length of zeros */
  540. BR = 0; /* BR = count of buffered bits added now */
  541. BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
  542. for (k = cinfo->Ss; k <= Se; k++) {
  543. if ((temp = absvalues[k]) == 0) {
  544. r++;
  545. continue;
  546. }
  547. /* Emit any required ZRLs, but not if they can be folded into EOB */
  548. while (r > 15 && k <= EOB) {
  549. /* emit any pending EOBRUN and the BE correction bits */
  550. emit_eobrun(entropy);
  551. /* Emit ZRL */
  552. emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
  553. r -= 16;
  554. /* Emit buffered correction bits that must be associated with ZRL */
  555. emit_buffered_bits(entropy, BR_buffer, BR);
  556. BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
  557. BR = 0;
  558. }
  559. /* If the coef was previously nonzero, it only needs a correction bit.
  560. * NOTE: a straight translation of the spec's figure G.7 would suggest
  561. * that we also need to test r > 15. But if r > 15, we can only get here
  562. * if k > EOB, which implies that this coefficient is not 1.
  563. */
  564. if (temp > 1) {
  565. /* The correction bit is the next bit of the absolute value. */
  566. BR_buffer[BR++] = (char) (temp & 1);
  567. continue;
  568. }
  569. /* Emit any pending EOBRUN and the BE correction bits */
  570. emit_eobrun(entropy);
  571. /* Count/emit Huffman symbol for run length / number of bits */
  572. emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
  573. /* Emit output bit for newly-nonzero coef */
  574. temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
  575. emit_bits(entropy, (unsigned int) temp, 1);
  576. /* Emit buffered correction bits that must be associated with this code */
  577. emit_buffered_bits(entropy, BR_buffer, BR);
  578. BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
  579. BR = 0;
  580. r = 0; /* reset zero run length */
  581. }
  582. if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
  583. entropy->EOBRUN++; /* count an EOB */
  584. entropy->BE += BR; /* concat my correction bits to older ones */
  585. /* We force out the EOB if we risk either:
  586. * 1. overflow of the EOB counter;
  587. * 2. overflow of the correction bit buffer during the next MCU.
  588. */
  589. if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
  590. emit_eobrun(entropy);
  591. }
  592. cinfo->dest->next_output_byte = entropy->next_output_byte;
  593. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  594. /* Update restart-interval state too */
  595. if (cinfo->restart_interval) {
  596. if (entropy->restarts_to_go == 0) {
  597. entropy->restarts_to_go = cinfo->restart_interval;
  598. entropy->next_restart_num++;
  599. entropy->next_restart_num &= 7;
  600. }
  601. entropy->restarts_to_go--;
  602. }
  603. return TRUE;
  604. }
  605. /*
  606. * Finish up at the end of a Huffman-compressed progressive scan.
  607. */
  608. METHODDEF(void)
  609. finish_pass_phuff (j_compress_ptr cinfo)
  610. {
  611. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  612. entropy->next_output_byte = cinfo->dest->next_output_byte;
  613. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  614. /* Flush out any buffered data */
  615. emit_eobrun(entropy);
  616. flush_bits(entropy);
  617. cinfo->dest->next_output_byte = entropy->next_output_byte;
  618. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  619. }
  620. /*
  621. * Finish up a statistics-gathering pass and create the new Huffman tables.
  622. */
  623. METHODDEF(void)
  624. finish_pass_gather_phuff (j_compress_ptr cinfo)
  625. {
  626. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  627. boolean is_DC_band;
  628. int ci, tbl;
  629. jpeg_component_info * compptr;
  630. JHUFF_TBL **htblptr;
  631. boolean did[NUM_HUFF_TBLS];
  632. /* Flush out buffered data (all we care about is counting the EOB symbol) */
  633. emit_eobrun(entropy);
  634. is_DC_band = (cinfo->Ss == 0);
  635. /* It's important not to apply jpeg_gen_optimal_table more than once
  636. * per table, because it clobbers the input frequency counts!
  637. */
  638. MEMZERO(did, SIZEOF(did));
  639. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  640. compptr = cinfo->cur_comp_info[ci];
  641. if (is_DC_band) {
  642. if (cinfo->Ah != 0) /* DC refinement needs no table */
  643. continue;
  644. tbl = compptr->dc_tbl_no;
  645. } else {
  646. tbl = compptr->ac_tbl_no;
  647. }
  648. if (! did[tbl]) {
  649. if (is_DC_band)
  650. htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
  651. else
  652. htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
  653. if (*htblptr == NULL)
  654. *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  655. jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
  656. did[tbl] = TRUE;
  657. }
  658. }
  659. }
  660. /*
  661. * Module initialization routine for progressive Huffman entropy encoding.
  662. */
  663. GLOBAL(void)
  664. jinit_phuff_encoder (j_compress_ptr cinfo)
  665. {
  666. phuff_entropy_ptr entropy;
  667. int i;
  668. entropy = (phuff_entropy_ptr)
  669. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  670. SIZEOF(phuff_entropy_encoder));
  671. cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  672. entropy->pub.start_pass = start_pass_phuff;
  673. /* Mark tables unallocated */
  674. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  675. entropy->derived_tbls[i] = NULL;
  676. entropy->count_ptrs[i] = NULL;
  677. }
  678. entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
  679. }
  680. #endif /* C_PROGRESSIVE_SUPPORTED */