jcphuff.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. INLINE
  189. LOCAL(void)
  190. emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
  191. /* Emit some bits, unless we are in gather mode */
  192. {
  193. /* This routine is heavily used, so it's worth coding tightly. */
  194. register INT32 put_buffer = (INT32) code;
  195. register int put_bits = entropy->put_bits;
  196. /* if size is 0, caller used an invalid Huffman table entry */
  197. if (size == 0)
  198. ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  199. if (entropy->gather_statistics)
  200. return; /* do nothing if we're only getting stats */
  201. put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
  202. put_bits += size; /* new number of bits in buffer */
  203. put_buffer <<= 24 - put_bits; /* align incoming bits */
  204. put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
  205. while (put_bits >= 8) {
  206. int c = (int) ((put_buffer >> 16) & 0xFF);
  207. emit_byte(entropy, c);
  208. if (c == 0xFF) { /* need to stuff a zero byte? */
  209. emit_byte(entropy, 0);
  210. }
  211. put_buffer <<= 8;
  212. put_bits -= 8;
  213. }
  214. entropy->put_buffer = put_buffer; /* update variables */
  215. entropy->put_bits = put_bits;
  216. }
  217. LOCAL(void)
  218. flush_bits (phuff_entropy_ptr entropy)
  219. {
  220. emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
  221. entropy->put_buffer = 0; /* and reset bit-buffer to empty */
  222. entropy->put_bits = 0;
  223. }
  224. /*
  225. * Emit (or just count) a Huffman symbol.
  226. */
  227. INLINE
  228. LOCAL(void)
  229. emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
  230. {
  231. if (entropy->gather_statistics)
  232. entropy->count_ptrs[tbl_no][symbol]++;
  233. else {
  234. c_derived_tbl * tbl = entropy->derived_tbls[tbl_no];
  235. emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
  236. }
  237. }
  238. /*
  239. * Emit bits from a correction bit buffer.
  240. */
  241. LOCAL(void)
  242. emit_buffered_bits (phuff_entropy_ptr entropy, char * bufstart,
  243. unsigned int nbits)
  244. {
  245. if (entropy->gather_statistics)
  246. return; /* no real work */
  247. while (nbits > 0) {
  248. emit_bits(entropy, (unsigned int) (*bufstart), 1);
  249. bufstart++;
  250. nbits--;
  251. }
  252. }
  253. /*
  254. * Emit any pending EOBRUN symbol.
  255. */
  256. LOCAL(void)
  257. emit_eobrun (phuff_entropy_ptr entropy)
  258. {
  259. register int temp, nbits;
  260. if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
  261. temp = entropy->EOBRUN;
  262. nbits = 0;
  263. while ((temp >>= 1))
  264. nbits++;
  265. /* safety check: shouldn't happen given limited correction-bit buffer */
  266. if (nbits > 14)
  267. ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  268. emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
  269. if (nbits)
  270. emit_bits(entropy, entropy->EOBRUN, nbits);
  271. entropy->EOBRUN = 0;
  272. /* Emit any buffered correction bits */
  273. emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
  274. entropy->BE = 0;
  275. }
  276. }
  277. /*
  278. * Emit a restart marker & resynchronize predictions.
  279. */
  280. LOCAL(void)
  281. emit_restart (phuff_entropy_ptr entropy, int restart_num)
  282. {
  283. int ci;
  284. emit_eobrun(entropy);
  285. if (! entropy->gather_statistics) {
  286. flush_bits(entropy);
  287. emit_byte(entropy, 0xFF);
  288. emit_byte(entropy, JPEG_RST0 + restart_num);
  289. }
  290. if (entropy->cinfo->Ss == 0) {
  291. /* Re-initialize DC predictions to 0 */
  292. for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
  293. entropy->last_dc_val[ci] = 0;
  294. } else {
  295. /* Re-initialize all AC-related fields to 0 */
  296. entropy->EOBRUN = 0;
  297. entropy->BE = 0;
  298. }
  299. }
  300. /*
  301. * MCU encoding for DC initial scan (either spectral selection,
  302. * or first pass of successive approximation).
  303. */
  304. METHODDEF(boolean)
  305. encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  306. {
  307. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  308. register int temp, temp2;
  309. register int nbits;
  310. int blkn, ci;
  311. int Al = cinfo->Al;
  312. JBLOCKROW block;
  313. jpeg_component_info * compptr;
  314. ISHIFT_TEMPS
  315. entropy->next_output_byte = cinfo->dest->next_output_byte;
  316. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  317. /* Emit restart marker if needed */
  318. if (cinfo->restart_interval)
  319. if (entropy->restarts_to_go == 0)
  320. emit_restart(entropy, entropy->next_restart_num);
  321. /* Encode the MCU data blocks */
  322. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  323. block = MCU_data[blkn];
  324. ci = cinfo->MCU_membership[blkn];
  325. compptr = cinfo->cur_comp_info[ci];
  326. /* Compute the DC value after the required point transform by Al.
  327. * This is simply an arithmetic right shift.
  328. */
  329. temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
  330. /* DC differences are figured on the point-transformed values. */
  331. temp = temp2 - entropy->last_dc_val[ci];
  332. entropy->last_dc_val[ci] = temp2;
  333. /* Encode the DC coefficient difference per section G.1.2.1 */
  334. temp2 = temp;
  335. if (temp < 0) {
  336. temp = -temp; /* temp is abs value of input */
  337. /* For a negative input, want temp2 = bitwise complement of abs(input) */
  338. /* This code assumes we are on a two's complement machine */
  339. temp2--;
  340. }
  341. /* Find the number of bits needed for the magnitude of the coefficient */
  342. nbits = 0;
  343. while (temp) {
  344. nbits++;
  345. temp >>= 1;
  346. }
  347. /* Check for out-of-range coefficient values.
  348. * Since we're encoding a difference, the range limit is twice as much.
  349. */
  350. if (nbits > MAX_COEF_BITS+1)
  351. ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  352. /* Count/emit the Huffman-coded symbol for the number of bits */
  353. emit_symbol(entropy, compptr->dc_tbl_no, nbits);
  354. /* Emit that number of bits of the value, if positive, */
  355. /* or the complement of its magnitude, if negative. */
  356. if (nbits) /* emit_bits rejects calls with size 0 */
  357. emit_bits(entropy, (unsigned int) temp2, nbits);
  358. }
  359. cinfo->dest->next_output_byte = entropy->next_output_byte;
  360. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  361. /* Update restart-interval state too */
  362. if (cinfo->restart_interval) {
  363. if (entropy->restarts_to_go == 0) {
  364. entropy->restarts_to_go = cinfo->restart_interval;
  365. entropy->next_restart_num++;
  366. entropy->next_restart_num &= 7;
  367. }
  368. entropy->restarts_to_go--;
  369. }
  370. return TRUE;
  371. }
  372. /*
  373. * MCU encoding for AC initial scan (either spectral selection,
  374. * or first pass of successive approximation).
  375. */
  376. METHODDEF(boolean)
  377. encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  378. {
  379. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  380. register int temp, temp2;
  381. register int nbits;
  382. register int r, k;
  383. int Se = cinfo->Se;
  384. int Al = cinfo->Al;
  385. JBLOCKROW block;
  386. entropy->next_output_byte = cinfo->dest->next_output_byte;
  387. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  388. /* Emit restart marker if needed */
  389. if (cinfo->restart_interval)
  390. if (entropy->restarts_to_go == 0)
  391. emit_restart(entropy, entropy->next_restart_num);
  392. /* Encode the MCU data block */
  393. block = MCU_data[0];
  394. /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
  395. r = 0; /* r = run length of zeros */
  396. for (k = cinfo->Ss; k <= Se; k++) {
  397. if ((temp = (*block)[jpeg_natural_order[k]]) == 0) {
  398. r++;
  399. continue;
  400. }
  401. /* We must apply the point transform by Al. For AC coefficients this
  402. * is an integer division with rounding towards 0. To do this portably
  403. * in C, we shift after obtaining the absolute value; so the code is
  404. * interwoven with finding the abs value (temp) and output bits (temp2).
  405. */
  406. if (temp < 0) {
  407. temp = -temp; /* temp is abs value of input */
  408. temp >>= Al; /* apply the point transform */
  409. /* For a negative coef, want temp2 = bitwise complement of abs(coef) */
  410. temp2 = ~temp;
  411. } else {
  412. temp >>= Al; /* apply the point transform */
  413. temp2 = temp;
  414. }
  415. /* Watch out for case that nonzero coef is zero after point transform */
  416. if (temp == 0) {
  417. r++;
  418. continue;
  419. }
  420. /* Emit any pending EOBRUN */
  421. if (entropy->EOBRUN > 0)
  422. emit_eobrun(entropy);
  423. /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  424. while (r > 15) {
  425. emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
  426. r -= 16;
  427. }
  428. /* Find the number of bits needed for the magnitude of the coefficient */
  429. nbits = 1; /* there must be at least one 1 bit */
  430. while ((temp >>= 1))
  431. nbits++;
  432. /* Check for out-of-range coefficient values */
  433. if (nbits > MAX_COEF_BITS)
  434. ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  435. /* Count/emit Huffman symbol for run length / number of bits */
  436. emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits);
  437. /* Emit that number of bits of the value, if positive, */
  438. /* or the complement of its magnitude, if negative. */
  439. emit_bits(entropy, (unsigned int) temp2, nbits);
  440. r = 0; /* reset zero run length */
  441. }
  442. if (r > 0) { /* If there are trailing zeroes, */
  443. entropy->EOBRUN++; /* count an EOB */
  444. if (entropy->EOBRUN == 0x7FFF)
  445. emit_eobrun(entropy); /* force it out to avoid overflow */
  446. }
  447. cinfo->dest->next_output_byte = entropy->next_output_byte;
  448. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  449. /* Update restart-interval state too */
  450. if (cinfo->restart_interval) {
  451. if (entropy->restarts_to_go == 0) {
  452. entropy->restarts_to_go = cinfo->restart_interval;
  453. entropy->next_restart_num++;
  454. entropy->next_restart_num &= 7;
  455. }
  456. entropy->restarts_to_go--;
  457. }
  458. return TRUE;
  459. }
  460. /*
  461. * MCU encoding for DC successive approximation refinement scan.
  462. * Note: we assume such scans can be multi-component, although the spec
  463. * is not very clear on the point.
  464. */
  465. METHODDEF(boolean)
  466. encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  467. {
  468. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  469. register int temp;
  470. int blkn;
  471. int Al = cinfo->Al;
  472. JBLOCKROW block;
  473. entropy->next_output_byte = cinfo->dest->next_output_byte;
  474. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  475. /* Emit restart marker if needed */
  476. if (cinfo->restart_interval)
  477. if (entropy->restarts_to_go == 0)
  478. emit_restart(entropy, entropy->next_restart_num);
  479. /* Encode the MCU data blocks */
  480. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  481. block = MCU_data[blkn];
  482. /* We simply emit the Al'th bit of the DC coefficient value. */
  483. temp = (*block)[0];
  484. emit_bits(entropy, (unsigned int) (temp >> Al), 1);
  485. }
  486. cinfo->dest->next_output_byte = entropy->next_output_byte;
  487. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  488. /* Update restart-interval state too */
  489. if (cinfo->restart_interval) {
  490. if (entropy->restarts_to_go == 0) {
  491. entropy->restarts_to_go = cinfo->restart_interval;
  492. entropy->next_restart_num++;
  493. entropy->next_restart_num &= 7;
  494. }
  495. entropy->restarts_to_go--;
  496. }
  497. return TRUE;
  498. }
  499. /*
  500. * MCU encoding for AC successive approximation refinement scan.
  501. */
  502. METHODDEF(boolean)
  503. encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  504. {
  505. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  506. register int temp;
  507. register int r, k;
  508. int EOB;
  509. char *BR_buffer;
  510. unsigned int BR;
  511. int Se = cinfo->Se;
  512. int Al = cinfo->Al;
  513. JBLOCKROW block;
  514. int absvalues[DCTSIZE2];
  515. entropy->next_output_byte = cinfo->dest->next_output_byte;
  516. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  517. /* Emit restart marker if needed */
  518. if (cinfo->restart_interval)
  519. if (entropy->restarts_to_go == 0)
  520. emit_restart(entropy, entropy->next_restart_num);
  521. /* Encode the MCU data block */
  522. block = MCU_data[0];
  523. /* It is convenient to make a pre-pass to determine the transformed
  524. * coefficients' absolute values and the EOB position.
  525. */
  526. EOB = 0;
  527. for (k = cinfo->Ss; k <= Se; k++) {
  528. temp = (*block)[jpeg_natural_order[k]];
  529. /* We must apply the point transform by Al. For AC coefficients this
  530. * is an integer division with rounding towards 0. To do this portably
  531. * in C, we shift after obtaining the absolute value.
  532. */
  533. if (temp < 0)
  534. temp = -temp; /* temp is abs value of input */
  535. temp >>= Al; /* apply the point transform */
  536. absvalues[k] = temp; /* save abs value for main pass */
  537. if (temp == 1)
  538. EOB = k; /* EOB = index of last newly-nonzero coef */
  539. }
  540. /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
  541. r = 0; /* r = run length of zeros */
  542. BR = 0; /* BR = count of buffered bits added now */
  543. BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
  544. for (k = cinfo->Ss; k <= Se; k++) {
  545. if ((temp = absvalues[k]) == 0) {
  546. r++;
  547. continue;
  548. }
  549. /* Emit any required ZRLs, but not if they can be folded into EOB */
  550. while (r > 15 && k <= EOB) {
  551. /* emit any pending EOBRUN and the BE correction bits */
  552. emit_eobrun(entropy);
  553. /* Emit ZRL */
  554. emit_symbol(entropy, entropy->ac_tbl_no, 0xF0);
  555. r -= 16;
  556. /* Emit buffered correction bits that must be associated with ZRL */
  557. emit_buffered_bits(entropy, BR_buffer, BR);
  558. BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
  559. BR = 0;
  560. }
  561. /* If the coef was previously nonzero, it only needs a correction bit.
  562. * NOTE: a straight translation of the spec's figure G.7 would suggest
  563. * that we also need to test r > 15. But if r > 15, we can only get here
  564. * if k > EOB, which implies that this coefficient is not 1.
  565. */
  566. if (temp > 1) {
  567. /* The correction bit is the next bit of the absolute value. */
  568. BR_buffer[BR++] = (char) (temp & 1);
  569. continue;
  570. }
  571. /* Emit any pending EOBRUN and the BE correction bits */
  572. emit_eobrun(entropy);
  573. /* Count/emit Huffman symbol for run length / number of bits */
  574. emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1);
  575. /* Emit output bit for newly-nonzero coef */
  576. temp = ((*block)[jpeg_natural_order[k]] < 0) ? 0 : 1;
  577. emit_bits(entropy, (unsigned int) temp, 1);
  578. /* Emit buffered correction bits that must be associated with this code */
  579. emit_buffered_bits(entropy, BR_buffer, BR);
  580. BR_buffer = entropy->bit_buffer; /* BE bits are gone now */
  581. BR = 0;
  582. r = 0; /* reset zero run length */
  583. }
  584. if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
  585. entropy->EOBRUN++; /* count an EOB */
  586. entropy->BE += BR; /* concat my correction bits to older ones */
  587. /* We force out the EOB if we risk either:
  588. * 1. overflow of the EOB counter;
  589. * 2. overflow of the correction bit buffer during the next MCU.
  590. */
  591. if (entropy->EOBRUN == 0x7FFF || entropy->BE > (MAX_CORR_BITS-DCTSIZE2+1))
  592. emit_eobrun(entropy);
  593. }
  594. cinfo->dest->next_output_byte = entropy->next_output_byte;
  595. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  596. /* Update restart-interval state too */
  597. if (cinfo->restart_interval) {
  598. if (entropy->restarts_to_go == 0) {
  599. entropy->restarts_to_go = cinfo->restart_interval;
  600. entropy->next_restart_num++;
  601. entropy->next_restart_num &= 7;
  602. }
  603. entropy->restarts_to_go--;
  604. }
  605. return TRUE;
  606. }
  607. /*
  608. * Finish up at the end of a Huffman-compressed progressive scan.
  609. */
  610. METHODDEF(void)
  611. finish_pass_phuff (j_compress_ptr cinfo)
  612. {
  613. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  614. entropy->next_output_byte = cinfo->dest->next_output_byte;
  615. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  616. /* Flush out any buffered data */
  617. emit_eobrun(entropy);
  618. flush_bits(entropy);
  619. cinfo->dest->next_output_byte = entropy->next_output_byte;
  620. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  621. }
  622. /*
  623. * Finish up a statistics-gathering pass and create the new Huffman tables.
  624. */
  625. METHODDEF(void)
  626. finish_pass_gather_phuff (j_compress_ptr cinfo)
  627. {
  628. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  629. boolean is_DC_band;
  630. int ci, tbl;
  631. jpeg_component_info * compptr;
  632. JHUFF_TBL **htblptr;
  633. boolean did[NUM_HUFF_TBLS];
  634. /* Flush out buffered data (all we care about is counting the EOB symbol) */
  635. emit_eobrun(entropy);
  636. is_DC_band = (cinfo->Ss == 0);
  637. /* It's important not to apply jpeg_gen_optimal_table more than once
  638. * per table, because it clobbers the input frequency counts!
  639. */
  640. MEMZERO(did, SIZEOF(did));
  641. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  642. compptr = cinfo->cur_comp_info[ci];
  643. if (is_DC_band) {
  644. if (cinfo->Ah != 0) /* DC refinement needs no table */
  645. continue;
  646. tbl = compptr->dc_tbl_no;
  647. } else {
  648. tbl = compptr->ac_tbl_no;
  649. }
  650. if (! did[tbl]) {
  651. if (is_DC_band)
  652. htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
  653. else
  654. htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
  655. if (*htblptr == NULL)
  656. *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  657. jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
  658. did[tbl] = TRUE;
  659. }
  660. }
  661. }
  662. /*
  663. * Module initialization routine for progressive Huffman entropy encoding.
  664. */
  665. GLOBAL(void)
  666. jinit_phuff_encoder (j_compress_ptr cinfo)
  667. {
  668. phuff_entropy_ptr entropy;
  669. int i;
  670. entropy = (phuff_entropy_ptr)
  671. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  672. SIZEOF(phuff_entropy_encoder));
  673. cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  674. entropy->pub.start_pass = start_pass_phuff;
  675. /* Mark tables unallocated */
  676. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  677. entropy->derived_tbls[i] = NULL;
  678. entropy->count_ptrs[i] = NULL;
  679. }
  680. entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
  681. }
  682. #endif /* C_PROGRESSIVE_SUPPORTED */