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.

829 lines
24 KiB

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