jcphuff.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /*
  2. * jcphuff.c
  3. *
  4. * Copyright (C) 1995-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 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. /* Get table index */
  126. if (is_DC_band) {
  127. if (cinfo->Ah != 0) /* DC refinement needs no table */
  128. continue;
  129. tbl = compptr->dc_tbl_no;
  130. } else {
  131. entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
  132. }
  133. if (gather_statistics) {
  134. /* Check for invalid table index */
  135. /* (make_c_derived_tbl does this in the other path) */
  136. if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
  137. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
  138. /* Allocate and zero the statistics tables */
  139. /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
  140. if (entropy->count_ptrs[tbl] == NULL)
  141. entropy->count_ptrs[tbl] = (long *)
  142. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  143. 257 * SIZEOF(long));
  144. MEMZERO(entropy->count_ptrs[tbl], 257 * SIZEOF(long));
  145. } else {
  146. /* Compute derived values for Huffman table */
  147. /* We may do this more than once for a table, but it's not expensive */
  148. jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
  149. & entropy->derived_tbls[tbl]);
  150. }
  151. }
  152. /* Initialize AC stuff */
  153. entropy->EOBRUN = 0;
  154. entropy->BE = 0;
  155. /* Initialize bit buffer to empty */
  156. entropy->put_buffer = 0;
  157. entropy->put_bits = 0;
  158. /* Initialize restart stuff */
  159. entropy->restarts_to_go = cinfo->restart_interval;
  160. entropy->next_restart_num = 0;
  161. }
  162. /* Outputting bytes to the file.
  163. * NB: these must be called only when actually outputting,
  164. * that is, entropy->gather_statistics == FALSE.
  165. */
  166. /* Emit a byte */
  167. #define emit_byte(entropy,val) \
  168. { *(entropy)->next_output_byte++ = (JOCTET) (val); \
  169. if (--(entropy)->free_in_buffer == 0) \
  170. dump_buffer(entropy); }
  171. LOCAL(void)
  172. dump_buffer (phuff_entropy_ptr entropy)
  173. /* Empty the output buffer; we do not support suspension in this module. */
  174. {
  175. struct jpeg_destination_mgr * dest = entropy->cinfo->dest;
  176. if (! (*dest->empty_output_buffer) (entropy->cinfo))
  177. ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
  178. /* After a successful buffer dump, must reset buffer pointers */
  179. entropy->next_output_byte = dest->next_output_byte;
  180. entropy->free_in_buffer = dest->free_in_buffer;
  181. }
  182. /* Outputting bits to the file */
  183. /* Only the right 24 bits of put_buffer are used; the valid bits are
  184. * left-justified in this part. At most 16 bits can be passed to emit_bits
  185. * in one call, and we never retain more than 7 bits in put_buffer
  186. * between calls, so 24 bits are sufficient.
  187. */
  188. LOCAL(void)
  189. emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
  190. /* Emit some bits, unless we are in gather mode */
  191. {
  192. /* This routine is heavily used, so it's worth coding tightly. */
  193. register INT32 put_buffer = (INT32) code;
  194. register int put_bits = entropy->put_bits;
  195. /* if size is 0, caller used an invalid Huffman table entry */
  196. if (size == 0)
  197. ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  198. if (entropy->gather_statistics)
  199. return; /* do nothing if we're only getting stats */
  200. put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
  201. put_bits += size; /* new number of bits in buffer */
  202. put_buffer <<= 24 - put_bits; /* align incoming bits */
  203. put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
  204. while (put_bits >= 8) {
  205. int c = (int) ((put_buffer >> 16) & 0xFF);
  206. emit_byte(entropy, c);
  207. if (c == 0xFF) { /* need to stuff a zero byte? */
  208. emit_byte(entropy, 0);
  209. }
  210. put_buffer <<= 8;
  211. put_bits -= 8;
  212. }
  213. entropy->put_buffer = put_buffer; /* update variables */
  214. entropy->put_bits = put_bits;
  215. }
  216. LOCAL(void)
  217. flush_bits (phuff_entropy_ptr entropy)
  218. {
  219. emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
  220. entropy->put_buffer = 0; /* and reset bit-buffer to empty */
  221. entropy->put_bits = 0;
  222. }
  223. /*
  224. * Emit (or just count) a Huffman symbol.
  225. */
  226. LOCAL(void)
  227. emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
  228. {
  229. if (entropy->gather_statistics)
  230. entropy->count_ptrs[tbl_no][symbol]++;
  231. else {
  232. c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
  233. emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
  234. }
  235. }
  236. /*
  237. * Emit bits from a correction bit buffer.
  238. */
  239. LOCAL(void)
  240. emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
  241. unsigned int nbits)
  242. {
  243. if (entropy->gather_statistics)
  244. return; /* no real work */
  245. while (nbits > 0) {
  246. emit_bits(entropy, (unsigned int) (*bufstart), 1);
  247. bufstart++;
  248. nbits--;
  249. }
  250. }
  251. /*
  252. * Emit any pending EOBRUN symbol.
  253. */
  254. LOCAL(void)
  255. emit_eobrun (phuff_entropy_ptr entropy)
  256. {
  257. register int temp, nbits;
  258. if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
  259. temp = entropy->EOBRUN;
  260. nbits = 0;
  261. while ((temp >>= 1))
  262. nbits++;
  263. /* safety check: shouldn't happen given limited correction-bit buffer */
  264. if (nbits > 14)
  265. ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  266. emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
  267. if (nbits)
  268. emit_bits(entropy, entropy->EOBRUN, nbits);
  269. entropy->EOBRUN = 0;
  270. /* Emit any buffered correction bits */
  271. emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
  272. entropy->BE = 0;
  273. }
  274. }
  275. /*
  276. * Emit a restart marker & resynchronize predictions.
  277. */
  278. LOCAL(void)
  279. emit_restart (phuff_entropy_ptr entropy, int restart_num)
  280. {
  281. int ci;
  282. emit_eobrun(entropy);
  283. if (! entropy->gather_statistics) {
  284. flush_bits(entropy);
  285. emit_byte(entropy, 0xFF);
  286. emit_byte(entropy, JPEG_RST0 + restart_num);
  287. }
  288. if (entropy->cinfo->Ss == 0) {
  289. /* Re-initialize DC predictions to 0 */
  290. for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
  291. entropy->last_dc_val[ci] = 0;
  292. } else {
  293. /* Re-initialize all AC-related fields to 0 */
  294. entropy->EOBRUN = 0;
  295. entropy->BE = 0;
  296. }
  297. }
  298. /*
  299. * MCU encoding for DC initial scan (either spectral selection,
  300. * or first pass of successive approximation).
  301. */
  302. METHODDEF(boolean)
  303. encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  304. {
  305. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  306. register int temp, temp2;
  307. register int nbits;
  308. int blkn, ci;
  309. int Al = cinfo->Al;
  310. JBLOCKROW block;
  311. jpeg_component_info * compptr;
  312. ISHIFT_TEMPS
  313. entropy->next_output_byte = cinfo->dest->next_output_byte;
  314. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  315. /* Emit restart marker if needed */
  316. if (cinfo->restart_interval)
  317. if (entropy->restarts_to_go == 0)
  318. emit_restart(entropy, entropy->next_restart_num);
  319. /* Encode the MCU data blocks */
  320. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  321. block = MCU_data[blkn];
  322. ci = cinfo->MCU_membership[blkn];
  323. compptr = cinfo->cur_comp_info[ci];
  324. /* Compute the DC value after the required point transform by Al.
  325. * This is simply an arithmetic right shift.
  326. */
  327. temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
  328. /* DC differences are figured on the point-transformed values. */
  329. temp = temp2 - entropy->last_dc_val[ci];
  330. entropy->last_dc_val[ci] = temp2;
  331. /* Encode the DC coefficient difference per section G.1.2.1 */
  332. temp2 = temp;
  333. if (temp < 0) {
  334. temp = -temp; /* temp is abs value of input */
  335. /* For a negative input, want temp2 = bitwise complement of abs(input) */
  336. /* This code assumes we are on a two's complement machine */
  337. temp2--;
  338. }
  339. /* Find the number of bits needed for the magnitude of the coefficient */
  340. nbits = 0;
  341. while (temp) {
  342. nbits++;
  343. temp >>= 1;
  344. }
  345. /* Check for out-of-range coefficient values.
  346. * Since we're encoding a difference, the range limit is twice as much.
  347. */
  348. if (nbits > MAX_COEF_BITS+1)
  349. ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  350. /* Count/emit the Huffman-coded symbol for the number of bits */
  351. emit_symbol(entropy, compptr->dc_tbl_no, nbits);
  352. /* Emit that number of bits of the value, if positive, */
  353. /* or the complement of its magnitude, if negative. */
  354. if (nbits) /* emit_bits rejects calls with size 0 */
  355. emit_bits(entropy, (unsigned int) temp2, nbits);
  356. }
  357. cinfo->dest->next_output_byte = entropy->next_output_byte;
  358. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  359. /* Update restart-interval state too */
  360. if (cinfo->restart_interval) {
  361. if (entropy->restarts_to_go == 0) {
  362. entropy->restarts_to_go = cinfo->restart_interval;
  363. entropy->next_restart_num++;
  364. entropy->next_restart_num &= 7;
  365. }
  366. entropy->restarts_to_go--;
  367. }
  368. return TRUE;
  369. }
  370. /*
  371. * MCU encoding for AC initial scan (either spectral selection,
  372. * or first pass of successive approximation).
  373. */
  374. METHODDEF(boolean)
  375. encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  376. {
  377. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  378. register int temp, temp2;
  379. register int nbits;
  380. register int r, k;
  381. int Se = cinfo->Se;
  382. int Al = cinfo->Al;
  383. JBLOCKROW block;
  384. entropy->next_output_byte = cinfo->dest->next_output_byte;
  385. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  386. /* Emit restart marker if needed */
  387. if (cinfo->restart_interval)
  388. if (entropy->restarts_to_go == 0)
  389. emit_restart(entropy, entropy->next_restart_num);
  390. /* Encode the MCU data block */
  391. block = MCU_data[0];
  392. /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
  393. r = 0; /* r = run length of zeros */
  394. for (k = cinfo->Ss; k <= Se; k++) {
  395. if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
  396. r++;
  397. continue;
  398. }
  399. /* We must apply the point transform by Al. For AC coefficients this
  400. * is an integer division with rounding towards 0. To do this portably
  401. * in C, we shift after obtaining the absolute value; so the code is
  402. * interwoven with finding the abs value (temp) and output bits (temp2).
  403. */
  404. if (temp < 0) {
  405. temp = -temp; /* temp is abs value of input */
  406. temp >>= Al; /* apply the point transform */
  407. /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
  408. temp2 = ~temp;
  409. } else {
  410. temp >>= Al; /* apply the point transform */
  411. temp2 = temp;
  412. }
  413. /* Watch out for case that nonzero coef is zero after point transform */
  414. if (temp == 0) {
  415. r++;
  416. continue;
  417. }
  418. /* Emit any pending EOBRUN */
  419. if (entropy->EOBRUN > 0)
  420. emit_eobrun(entropy);
  421. /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  422. while (r > 15) {
  423. emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
  424. r -= 16;
  425. }
  426. /* Find the number of bits needed for the magnitude of the coefficient */
  427. nbits = 1; /* there must be at least one 1 bit */
  428. while ((temp >>= 1))
  429. nbits++;
  430. /* Check for out-of-range coefficient values */
  431. if (nbits > MAX_COEF_BITS)
  432. ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  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 */