jcphuff.cpp 25 KB

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