jcphuff.cpp 28 KB

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