jcphuff.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /*
  2. * jcphuff.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1995-1997, Thomas G. Lane.
  6. * libjpeg-turbo Modifications:
  7. * Copyright (C) 2011, 2015, 2018, D. R. Commander.
  8. * Copyright (C) 2016, 2018, Matthieu Darbois.
  9. * Copyright (C) 2014, Mozilla Corporation.
  10. * For conditions of distribution and use, see the accompanying README.ijg
  11. * file.
  12. *
  13. * This file contains Huffman entropy encoding routines for progressive JPEG.
  14. *
  15. * We do not support output suspension in this module, since the library
  16. * currently does not allow multiple-scan files to be written with output
  17. * suspension.
  18. */
  19. #define JPEG_INTERNALS
  20. #include "jinclude.h"
  21. #include "jpeglib.h"
  22. #include "jsimd.h"
  23. #include "jconfigint.h"
  24. #include <limits.h>
  25. #ifdef HAVE_INTRIN_H
  26. #include <intrin.h>
  27. #ifdef _MSC_VER
  28. #ifdef HAVE_BITSCANFORWARD64
  29. #pragma intrinsic(_BitScanForward64)
  30. #endif
  31. #ifdef HAVE_BITSCANFORWARD
  32. #pragma intrinsic(_BitScanForward)
  33. #endif
  34. #endif
  35. #endif
  36. #ifdef C_PROGRESSIVE_SUPPORTED
  37. /*
  38. * NOTE: If USE_CLZ_INTRINSIC is defined, then clz/bsr instructions will be
  39. * used for bit counting rather than the lookup table. This will reduce the
  40. * memory footprint by 64k, which is important for some mobile applications
  41. * that create many isolated instances of libjpeg-turbo (web browsers, for
  42. * instance.) This may improve performance on some mobile platforms as well.
  43. * This feature is enabled by default only on ARM processors, because some x86
  44. * chips have a slow implementation of bsr, and the use of clz/bsr cannot be
  45. * shown to have a significant performance impact even on the x86 chips that
  46. * have a fast implementation of it. When building for ARMv6, you can
  47. * explicitly disable the use of clz/bsr by adding -mthumb to the compiler
  48. * flags (this defines __thumb__).
  49. */
  50. /* NOTE: Both GCC and Clang define __GNUC__ */
  51. #if defined(__GNUC__) && (defined(__arm__) || defined(__aarch64__))
  52. #if !defined(__thumb__) || defined(__thumb2__)
  53. #define USE_CLZ_INTRINSIC
  54. #endif
  55. #endif
  56. #ifdef USE_CLZ_INTRINSIC
  57. #define JPEG_NBITS_NONZERO(x) (32 - __builtin_clz(x))
  58. #define JPEG_NBITS(x) (x ? JPEG_NBITS_NONZERO(x) : 0)
  59. #else
  60. #include "jpeg_nbits_table.h"
  61. #define JPEG_NBITS(x) (jpeg_nbits_table[x])
  62. #define JPEG_NBITS_NONZERO(x) JPEG_NBITS(x)
  63. #endif
  64. /* Expanded entropy encoder object for progressive Huffman encoding. */
  65. typedef struct {
  66. struct jpeg_entropy_encoder pub; /* public fields */
  67. /* Pointer to routine to prepare data for encode_mcu_AC_first() */
  68. void (*AC_first_prepare) (const JCOEF *block,
  69. const int *jpeg_natural_order_start, int Sl,
  70. int Al, JCOEF *values, size_t *zerobits);
  71. /* Pointer to routine to prepare data for encode_mcu_AC_refine() */
  72. int (*AC_refine_prepare) (const JCOEF *block,
  73. const int *jpeg_natural_order_start, int Sl,
  74. int Al, JCOEF *absvalues, size_t *bits);
  75. /* Mode flag: TRUE for optimization, FALSE for actual data output */
  76. boolean gather_statistics;
  77. /* Bit-level coding status.
  78. * next_output_byte/free_in_buffer are local copies of cinfo->dest fields.
  79. */
  80. JOCTET *next_output_byte; /* => next byte to write in buffer */
  81. size_t free_in_buffer; /* # of byte spaces remaining in buffer */
  82. size_t put_buffer; /* current bit-accumulation buffer */
  83. int put_bits; /* # of bits now in it */
  84. j_compress_ptr cinfo; /* link to cinfo (needed for dump_buffer) */
  85. /* Coding status for DC components */
  86. int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
  87. /* Coding status for AC components */
  88. int ac_tbl_no; /* the table number of the single component */
  89. unsigned int EOBRUN; /* run length of EOBs */
  90. unsigned int BE; /* # of buffered correction bits before MCU */
  91. char *bit_buffer; /* buffer for correction bits (1 per char) */
  92. /* packing correction bits tightly would save some space but cost time... */
  93. unsigned int restarts_to_go; /* MCUs left in this restart interval */
  94. int next_restart_num; /* next restart number to write (0-7) */
  95. /* Pointers to derived tables (these workspaces have image lifespan).
  96. * Since any one scan codes only DC or only AC, we only need one set
  97. * of tables, not one for DC and one for AC.
  98. */
  99. c_derived_tbl *derived_tbls[NUM_HUFF_TBLS];
  100. /* Statistics tables for optimization; again, one set is enough */
  101. long *count_ptrs[NUM_HUFF_TBLS];
  102. } phuff_entropy_encoder;
  103. typedef phuff_entropy_encoder *phuff_entropy_ptr;
  104. /* MAX_CORR_BITS is the number of bits the AC refinement correction-bit
  105. * buffer can hold. Larger sizes may slightly improve compression, but
  106. * 1000 is already well into the realm of overkill.
  107. * The minimum safe size is 64 bits.
  108. */
  109. #define MAX_CORR_BITS 1000 /* Max # of correction bits I can buffer */
  110. /* IRIGHT_SHIFT is like RIGHT_SHIFT, but works on int rather than JLONG.
  111. * We assume that int right shift is unsigned if JLONG right shift is,
  112. * which should be safe.
  113. */
  114. #ifdef RIGHT_SHIFT_IS_UNSIGNED
  115. #define ISHIFT_TEMPS int ishift_temp;
  116. #define IRIGHT_SHIFT(x,shft) \
  117. ((ishift_temp = (x)) < 0 ? \
  118. (ishift_temp >> (shft)) | ((~0) << (16-(shft))) : \
  119. (ishift_temp >> (shft)))
  120. #else
  121. #define ISHIFT_TEMPS
  122. #define IRIGHT_SHIFT(x,shft) ((x) >> (shft))
  123. #endif
  124. #define PAD(v, p) ((v + (p) - 1) & (~((p) - 1)))
  125. /* Forward declarations */
  126. METHODDEF(boolean) encode_mcu_DC_first (j_compress_ptr cinfo,
  127. JBLOCKROW *MCU_data);
  128. METHODDEF(void) encode_mcu_AC_first_prepare
  129. (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
  130. JCOEF *values, size_t *zerobits);
  131. METHODDEF(boolean) encode_mcu_AC_first (j_compress_ptr cinfo,
  132. JBLOCKROW *MCU_data);
  133. METHODDEF(boolean) encode_mcu_DC_refine (j_compress_ptr cinfo,
  134. JBLOCKROW *MCU_data);
  135. METHODDEF(int) encode_mcu_AC_refine_prepare
  136. (const JCOEF *block, const int *jpeg_natural_order_start, int Sl, int Al,
  137. JCOEF *absvalues, size_t *bits);
  138. METHODDEF(boolean) encode_mcu_AC_refine (j_compress_ptr cinfo,
  139. JBLOCKROW *MCU_data);
  140. METHODDEF(void) finish_pass_phuff (j_compress_ptr cinfo);
  141. METHODDEF(void) finish_pass_gather_phuff (j_compress_ptr cinfo);
  142. /* Count bit loop zeroes */
  143. INLINE
  144. METHODDEF(int)
  145. count_zeroes(size_t *x)
  146. {
  147. int result;
  148. #if defined(HAVE_BUILTIN_CTZL)
  149. result = __builtin_ctzl(*x);
  150. *x >>= result;
  151. #elif defined(HAVE_BITSCANFORWARD64)
  152. _BitScanForward64(&result, *x);
  153. *x >>= result;
  154. #elif defined(HAVE_BITSCANFORWARD)
  155. _BitScanForward(&result, *x);
  156. *x >>= result;
  157. #else
  158. result = 0;
  159. while ((*x & 1) == 0) {
  160. ++result;
  161. *x >>= 1;
  162. }
  163. #endif
  164. return result;
  165. }
  166. /*
  167. * Initialize for a Huffman-compressed scan using progressive JPEG.
  168. */
  169. METHODDEF(void)
  170. start_pass_phuff (j_compress_ptr cinfo, boolean gather_statistics)
  171. {
  172. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  173. boolean is_DC_band;
  174. int ci, tbl;
  175. jpeg_component_info *compptr;
  176. entropy->cinfo = cinfo;
  177. entropy->gather_statistics = gather_statistics;
  178. is_DC_band = (cinfo->Ss == 0);
  179. /* We assume jcmaster.c already validated the scan parameters. */
  180. /* Select execution routines */
  181. if (cinfo->Ah == 0) {
  182. if (is_DC_band)
  183. entropy->pub.encode_mcu = encode_mcu_DC_first;
  184. else
  185. entropy->pub.encode_mcu = encode_mcu_AC_first;
  186. if (jsimd_can_encode_mcu_AC_first_prepare())
  187. entropy->AC_first_prepare = jsimd_encode_mcu_AC_first_prepare;
  188. else
  189. entropy->AC_first_prepare = encode_mcu_AC_first_prepare;
  190. } else {
  191. if (is_DC_band)
  192. entropy->pub.encode_mcu = encode_mcu_DC_refine;
  193. else {
  194. entropy->pub.encode_mcu = encode_mcu_AC_refine;
  195. if (jsimd_can_encode_mcu_AC_refine_prepare())
  196. entropy->AC_refine_prepare = jsimd_encode_mcu_AC_refine_prepare;
  197. else
  198. entropy->AC_refine_prepare = encode_mcu_AC_refine_prepare;
  199. /* AC refinement needs a correction bit buffer */
  200. if (entropy->bit_buffer == NULL)
  201. entropy->bit_buffer = (char *)
  202. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  203. MAX_CORR_BITS * sizeof(char));
  204. }
  205. }
  206. if (gather_statistics)
  207. entropy->pub.finish_pass = finish_pass_gather_phuff;
  208. else
  209. entropy->pub.finish_pass = finish_pass_phuff;
  210. /* Only DC coefficients may be interleaved, so cinfo->comps_in_scan = 1
  211. * for AC coefficients.
  212. */
  213. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  214. compptr = cinfo->cur_comp_info[ci];
  215. /* Initialize DC predictions to 0 */
  216. entropy->last_dc_val[ci] = 0;
  217. /* Get table index */
  218. if (is_DC_band) {
  219. if (cinfo->Ah != 0) /* DC refinement needs no table */
  220. continue;
  221. tbl = compptr->dc_tbl_no;
  222. } else {
  223. entropy->ac_tbl_no = tbl = compptr->ac_tbl_no;
  224. }
  225. if (gather_statistics) {
  226. /* Check for invalid table index */
  227. /* (make_c_derived_tbl does this in the other path) */
  228. if (tbl < 0 || tbl >= NUM_HUFF_TBLS)
  229. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tbl);
  230. /* Allocate and zero the statistics tables */
  231. /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
  232. if (entropy->count_ptrs[tbl] == NULL)
  233. entropy->count_ptrs[tbl] = (long *)
  234. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  235. 257 * sizeof(long));
  236. MEMZERO(entropy->count_ptrs[tbl], 257 * sizeof(long));
  237. if (cinfo->master->trellis_passes) {
  238. /* When generating tables for trellis passes, make sure that all */
  239. /* codewords have an assigned length */
  240. int i, j;
  241. for (i = 0; i < 16; i++)
  242. for (j = 0; j < 12; j++)
  243. entropy->count_ptrs[tbl][16 * i + j] = 1;
  244. }
  245. } else {
  246. /* Compute derived values for Huffman table */
  247. /* We may do this more than once for a table, but it's not expensive */
  248. jpeg_make_c_derived_tbl(cinfo, is_DC_band, tbl,
  249. & entropy->derived_tbls[tbl]);
  250. }
  251. }
  252. /* Initialize AC stuff */
  253. entropy->EOBRUN = 0;
  254. entropy->BE = 0;
  255. /* Initialize bit buffer to empty */
  256. entropy->put_buffer = 0;
  257. entropy->put_bits = 0;
  258. /* Initialize restart stuff */
  259. entropy->restarts_to_go = cinfo->restart_interval;
  260. entropy->next_restart_num = 0;
  261. }
  262. /* Outputting bytes to the file.
  263. * NB: these must be called only when actually outputting,
  264. * that is, entropy->gather_statistics == FALSE.
  265. */
  266. /* Emit a byte */
  267. #define emit_byte(entropy, val) { \
  268. *(entropy)->next_output_byte++ = (JOCTET)(val); \
  269. if (--(entropy)->free_in_buffer == 0) \
  270. dump_buffer(entropy); \
  271. }
  272. LOCAL(void)
  273. dump_buffer (phuff_entropy_ptr entropy)
  274. /* Empty the output buffer; we do not support suspension in this module. */
  275. {
  276. struct jpeg_destination_mgr *dest = entropy->cinfo->dest;
  277. if (! (*dest->empty_output_buffer) (entropy->cinfo))
  278. ERREXIT(entropy->cinfo, JERR_CANT_SUSPEND);
  279. /* After a successful buffer dump, must reset buffer pointers */
  280. entropy->next_output_byte = dest->next_output_byte;
  281. entropy->free_in_buffer = dest->free_in_buffer;
  282. }
  283. /* Outputting bits to the file */
  284. /* Only the right 24 bits of put_buffer are used; the valid bits are
  285. * left-justified in this part. At most 16 bits can be passed to emit_bits
  286. * in one call, and we never retain more than 7 bits in put_buffer
  287. * between calls, so 24 bits are sufficient.
  288. */
  289. LOCAL(void)
  290. emit_bits (phuff_entropy_ptr entropy, unsigned int code, int size)
  291. /* Emit some bits, unless we are in gather mode */
  292. {
  293. /* This routine is heavily used, so it's worth coding tightly. */
  294. register size_t put_buffer = (size_t) code;
  295. register int put_bits = entropy->put_bits;
  296. /* if size is 0, caller used an invalid Huffman table entry */
  297. if (size == 0)
  298. ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  299. if (entropy->gather_statistics)
  300. return; /* do nothing if we're only getting stats */
  301. put_buffer &= (((size_t) 1)<<size) - 1; /* mask off any extra bits in code */
  302. put_bits += size; /* new number of bits in buffer */
  303. put_buffer <<= 24 - put_bits; /* align incoming bits */
  304. put_buffer |= entropy->put_buffer; /* and merge with old buffer contents */
  305. while (put_bits >= 8) {
  306. int c = (int) ((put_buffer >> 16) & 0xFF);
  307. emit_byte(entropy, c);
  308. if (c == 0xFF) { /* need to stuff a zero byte? */
  309. emit_byte(entropy, 0);
  310. }
  311. put_buffer <<= 8;
  312. put_bits -= 8;
  313. }
  314. entropy->put_buffer = put_buffer; /* update variables */
  315. entropy->put_bits = put_bits;
  316. }
  317. LOCAL(void)
  318. flush_bits (phuff_entropy_ptr entropy)
  319. {
  320. emit_bits(entropy, 0x7F, 7); /* fill any partial byte with ones */
  321. entropy->put_buffer = 0; /* and reset bit-buffer to empty */
  322. entropy->put_bits = 0;
  323. }
  324. /*
  325. * Emit (or just count) a Huffman symbol.
  326. */
  327. LOCAL(void)
  328. emit_symbol (phuff_entropy_ptr entropy, int tbl_no, int symbol)
  329. {
  330. if (entropy->gather_statistics)
  331. entropy->count_ptrs[tbl_no][symbol]++;
  332. else {
  333. c_derived_tbl *tbl = entropy->derived_tbls[tbl_no];
  334. emit_bits(entropy, tbl->ehufco[symbol], tbl->ehufsi[symbol]);
  335. }
  336. }
  337. /*
  338. * Emit bits from a correction bit buffer.
  339. */
  340. LOCAL(void)
  341. emit_buffered_bits (phuff_entropy_ptr entropy, char *bufstart,
  342. unsigned int nbits)
  343. {
  344. if (entropy->gather_statistics)
  345. return; /* no real work */
  346. while (nbits > 0) {
  347. emit_bits(entropy, (unsigned int) (*bufstart), 1);
  348. bufstart++;
  349. nbits--;
  350. }
  351. }
  352. /*
  353. * Emit any pending EOBRUN symbol.
  354. */
  355. LOCAL(void)
  356. emit_eobrun (phuff_entropy_ptr entropy)
  357. {
  358. register int temp, nbits;
  359. if (entropy->EOBRUN > 0) { /* if there is any pending EOBRUN */
  360. temp = entropy->EOBRUN;
  361. nbits = JPEG_NBITS_NONZERO(temp) - 1;
  362. /* safety check: shouldn't happen given limited correction-bit buffer */
  363. if (nbits > 14)
  364. ERREXIT(entropy->cinfo, JERR_HUFF_MISSING_CODE);
  365. emit_symbol(entropy, entropy->ac_tbl_no, nbits << 4);
  366. if (nbits)
  367. emit_bits(entropy, entropy->EOBRUN, nbits);
  368. entropy->EOBRUN = 0;
  369. /* Emit any buffered correction bits */
  370. emit_buffered_bits(entropy, entropy->bit_buffer, entropy->BE);
  371. entropy->BE = 0;
  372. }
  373. }
  374. /*
  375. * Emit a restart marker & resynchronize predictions.
  376. */
  377. LOCAL(void)
  378. emit_restart (phuff_entropy_ptr entropy, int restart_num)
  379. {
  380. int ci;
  381. emit_eobrun(entropy);
  382. if (! entropy->gather_statistics) {
  383. flush_bits(entropy);
  384. emit_byte(entropy, 0xFF);
  385. emit_byte(entropy, JPEG_RST0 + restart_num);
  386. }
  387. if (entropy->cinfo->Ss == 0) {
  388. /* Re-initialize DC predictions to 0 */
  389. for (ci = 0; ci < entropy->cinfo->comps_in_scan; ci++)
  390. entropy->last_dc_val[ci] = 0;
  391. } else {
  392. /* Re-initialize all AC-related fields to 0 */
  393. entropy->EOBRUN = 0;
  394. entropy->BE = 0;
  395. }
  396. }
  397. /*
  398. * MCU encoding for DC initial scan (either spectral selection,
  399. * or first pass of successive approximation).
  400. */
  401. METHODDEF(boolean)
  402. encode_mcu_DC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  403. {
  404. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  405. register int temp, temp2, temp3;
  406. register int nbits;
  407. int blkn, ci;
  408. int Al = cinfo->Al;
  409. JBLOCKROW block;
  410. jpeg_component_info *compptr;
  411. ISHIFT_TEMPS
  412. entropy->next_output_byte = cinfo->dest->next_output_byte;
  413. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  414. /* Emit restart marker if needed */
  415. if (cinfo->restart_interval)
  416. if (entropy->restarts_to_go == 0)
  417. emit_restart(entropy, entropy->next_restart_num);
  418. /* Encode the MCU data blocks */
  419. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  420. block = MCU_data[blkn];
  421. ci = cinfo->MCU_membership[blkn];
  422. compptr = cinfo->cur_comp_info[ci];
  423. /* Compute the DC value after the required point transform by Al.
  424. * This is simply an arithmetic right shift.
  425. */
  426. temp2 = IRIGHT_SHIFT((int) ((*block)[0]), Al);
  427. /* DC differences are figured on the point-transformed values. */
  428. temp = temp2 - entropy->last_dc_val[ci];
  429. entropy->last_dc_val[ci] = temp2;
  430. /* Encode the DC coefficient difference per section G.1.2.1 */
  431. /* This is a well-known technique for obtaining the absolute value without
  432. * a branch. It is derived from an assembly language technique presented
  433. * in "How to Optimize for the Pentium Processors", Copyright (c) 1996,
  434. * 1997 by Agner Fog.
  435. */
  436. temp3 = temp >> (CHAR_BIT * sizeof(int) - 1);
  437. temp ^= temp3;
  438. temp -= temp3; /* temp is abs value of input */
  439. /* For a negative input, want temp2 = bitwise complement of abs(input) */
  440. temp2 = temp ^ temp3;
  441. /* Find the number of bits needed for the magnitude of the coefficient */
  442. nbits = JPEG_NBITS(temp);
  443. /* Check for out-of-range coefficient values.
  444. * Since we're encoding a difference, the range limit is twice as much.
  445. */
  446. if (nbits > MAX_COEF_BITS+1)
  447. ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  448. /* Count/emit the Huffman-coded symbol for the number of bits */
  449. emit_symbol(entropy, compptr->dc_tbl_no, nbits);
  450. /* Emit that number of bits of the value, if positive, */
  451. /* or the complement of its magnitude, if negative. */
  452. if (nbits) /* emit_bits rejects calls with size 0 */
  453. emit_bits(entropy, (unsigned int) temp2, nbits);
  454. }
  455. cinfo->dest->next_output_byte = entropy->next_output_byte;
  456. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  457. /* Update restart-interval state too */
  458. if (cinfo->restart_interval) {
  459. if (entropy->restarts_to_go == 0) {
  460. entropy->restarts_to_go = cinfo->restart_interval;
  461. entropy->next_restart_num++;
  462. entropy->next_restart_num &= 7;
  463. }
  464. entropy->restarts_to_go--;
  465. }
  466. return TRUE;
  467. }
  468. /*
  469. * Data preparation for encode_mcu_AC_first().
  470. */
  471. #define COMPUTE_ABSVALUES_AC_FIRST(Sl) { \
  472. for (k = 0; k < Sl; k++) { \
  473. temp = block[jpeg_natural_order_start[k]]; \
  474. if (temp == 0) \
  475. continue; \
  476. /* We must apply the point transform by Al. For AC coefficients this \
  477. * is an integer division with rounding towards 0. To do this portably \
  478. * in C, we shift after obtaining the absolute value; so the code is \
  479. * interwoven with finding the abs value (temp) and output bits (temp2). \
  480. */ \
  481. temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \
  482. temp ^= temp2; \
  483. temp -= temp2; /* temp is abs value of input */ \
  484. temp >>= Al; /* apply the point transform */ \
  485. /* Watch out for case that nonzero coef is zero after point transform */ \
  486. if (temp == 0) \
  487. continue; \
  488. /* For a negative coef, want temp2 = bitwise complement of abs(coef) */ \
  489. temp2 ^= temp; \
  490. values[k] = temp; \
  491. values[k + DCTSIZE2] = temp2; \
  492. zerobits |= ((size_t)1U) << k; \
  493. } \
  494. }
  495. METHODDEF(void)
  496. encode_mcu_AC_first_prepare(const JCOEF *block,
  497. const int *jpeg_natural_order_start, int Sl,
  498. int Al, JCOEF *values, size_t *bits)
  499. {
  500. register int k, temp, temp2;
  501. size_t zerobits = 0U;
  502. int Sl0 = Sl;
  503. #if SIZEOF_SIZE_T == 4
  504. if (Sl0 > 32)
  505. Sl0 = 32;
  506. #endif
  507. COMPUTE_ABSVALUES_AC_FIRST(Sl0);
  508. bits[0] = zerobits;
  509. #if SIZEOF_SIZE_T == 4
  510. zerobits = 0U;
  511. if (Sl > 32) {
  512. Sl -= 32;
  513. jpeg_natural_order_start += 32;
  514. values += 32;
  515. COMPUTE_ABSVALUES_AC_FIRST(Sl);
  516. }
  517. bits[1] = zerobits;
  518. #endif
  519. }
  520. /*
  521. * MCU encoding for AC initial scan (either spectral selection,
  522. * or first pass of successive approximation).
  523. */
  524. #define ENCODE_COEFS_AC_FIRST(label) { \
  525. while (zerobits) { \
  526. r = count_zeroes(&zerobits); \
  527. cvalue += r; \
  528. label \
  529. temp = cvalue[0]; \
  530. temp2 = cvalue[DCTSIZE2]; \
  531. \
  532. /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \
  533. while (r > 15) { \
  534. emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \
  535. r -= 16; \
  536. } \
  537. \
  538. /* Find the number of bits needed for the magnitude of the coefficient */ \
  539. nbits = JPEG_NBITS_NONZERO(temp); /* there must be at least one 1 bit */ \
  540. /* Check for out-of-range coefficient values */ \
  541. if (nbits > MAX_COEF_BITS) \
  542. ERREXIT(cinfo, JERR_BAD_DCT_COEF); \
  543. \
  544. /* Count/emit Huffman symbol for run length / number of bits */ \
  545. emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + nbits); \
  546. \
  547. /* Emit that number of bits of the value, if positive, */ \
  548. /* or the complement of its magnitude, if negative. */ \
  549. emit_bits(entropy, (unsigned int)temp2, nbits); \
  550. \
  551. cvalue++; \
  552. zerobits >>= 1; \
  553. } \
  554. }
  555. METHODDEF(boolean)
  556. encode_mcu_AC_first (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  557. {
  558. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  559. register int temp, temp2;
  560. register int nbits, r;
  561. int Sl = cinfo->Se - cinfo->Ss + 1;
  562. int Al = cinfo->Al;
  563. JCOEF values_unaligned[2 * DCTSIZE2 + 15];
  564. JCOEF *values;
  565. const JCOEF *cvalue;
  566. size_t zerobits;
  567. size_t bits[8 / SIZEOF_SIZE_T];
  568. entropy->next_output_byte = cinfo->dest->next_output_byte;
  569. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  570. /* Emit restart marker if needed */
  571. if (cinfo->restart_interval)
  572. if (entropy->restarts_to_go == 0)
  573. emit_restart(entropy, entropy->next_restart_num);
  574. #ifdef WITH_SIMD
  575. cvalue = values = (JCOEF *)PAD((size_t)values_unaligned, 16);
  576. #else
  577. /* Not using SIMD, so alignment is not needed */
  578. cvalue = values = values_unaligned;
  579. #endif
  580. /* Prepare data */
  581. entropy->AC_first_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,
  582. Sl, Al, values, bits);
  583. zerobits = bits[0];
  584. #if SIZEOF_SIZE_T == 4
  585. zerobits |= bits[1];
  586. #endif
  587. /* Emit any pending EOBRUN */
  588. if (zerobits && (entropy->EOBRUN > 0))
  589. emit_eobrun(entropy);
  590. #if SIZEOF_SIZE_T == 4
  591. zerobits = bits[0];
  592. #endif
  593. /* Encode the AC coefficients per section G.1.2.2, fig. G.3 */
  594. ENCODE_COEFS_AC_FIRST((void)0;);
  595. #if SIZEOF_SIZE_T == 4
  596. zerobits = bits[1];
  597. if (zerobits) {
  598. int diff = ((values + DCTSIZE2 / 2) - cvalue);
  599. r = count_zeroes(&zerobits);
  600. r += diff;
  601. cvalue += r;
  602. goto first_iter_ac_first;
  603. }
  604. ENCODE_COEFS_AC_FIRST(first_iter_ac_first:);
  605. #endif
  606. if (cvalue < (values + Sl)) { /* If there are trailing zeroes, */
  607. entropy->EOBRUN++; /* count an EOB */
  608. if (entropy->EOBRUN == 0x7FFF)
  609. emit_eobrun(entropy); /* force it out to avoid overflow */
  610. }
  611. cinfo->dest->next_output_byte = entropy->next_output_byte;
  612. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  613. /* Update restart-interval state too */
  614. if (cinfo->restart_interval) {
  615. if (entropy->restarts_to_go == 0) {
  616. entropy->restarts_to_go = cinfo->restart_interval;
  617. entropy->next_restart_num++;
  618. entropy->next_restart_num &= 7;
  619. }
  620. entropy->restarts_to_go--;
  621. }
  622. return TRUE;
  623. }
  624. /*
  625. * MCU encoding for DC successive approximation refinement scan.
  626. * Note: we assume such scans can be multi-component, although the spec
  627. * is not very clear on the point.
  628. */
  629. METHODDEF(boolean)
  630. encode_mcu_DC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  631. {
  632. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  633. register int temp;
  634. int blkn;
  635. int Al = cinfo->Al;
  636. JBLOCKROW block;
  637. entropy->next_output_byte = cinfo->dest->next_output_byte;
  638. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  639. /* Emit restart marker if needed */
  640. if (cinfo->restart_interval)
  641. if (entropy->restarts_to_go == 0)
  642. emit_restart(entropy, entropy->next_restart_num);
  643. /* Encode the MCU data blocks */
  644. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  645. block = MCU_data[blkn];
  646. /* We simply emit the Al'th bit of the DC coefficient value. */
  647. temp = (*block)[0];
  648. emit_bits(entropy, (unsigned int) (temp >> Al), 1);
  649. }
  650. cinfo->dest->next_output_byte = entropy->next_output_byte;
  651. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  652. /* Update restart-interval state too */
  653. if (cinfo->restart_interval) {
  654. if (entropy->restarts_to_go == 0) {
  655. entropy->restarts_to_go = cinfo->restart_interval;
  656. entropy->next_restart_num++;
  657. entropy->next_restart_num &= 7;
  658. }
  659. entropy->restarts_to_go--;
  660. }
  661. return TRUE;
  662. }
  663. /*
  664. * Data preparation for encode_mcu_AC_refine().
  665. */
  666. #define COMPUTE_ABSVALUES_AC_REFINE(Sl, koffset) { \
  667. /* It is convenient to make a pre-pass to determine the transformed \
  668. * coefficients' absolute values and the EOB position. \
  669. */ \
  670. for (k = 0; k < Sl; k++) { \
  671. temp = block[jpeg_natural_order_start[k]]; \
  672. /* We must apply the point transform by Al. For AC coefficients this \
  673. * is an integer division with rounding towards 0. To do this portably \
  674. * in C, we shift after obtaining the absolute value. \
  675. */ \
  676. temp2 = temp >> (CHAR_BIT * sizeof(int) - 1); \
  677. temp ^= temp2; \
  678. temp -= temp2; /* temp is abs value of input */ \
  679. temp >>= Al; /* apply the point transform */ \
  680. if (temp != 0) { \
  681. zerobits |= ((size_t)1U) << k; \
  682. signbits |= ((size_t)(temp2 + 1)) << k; \
  683. } \
  684. absvalues[k] = (JCOEF)temp; /* save abs value for main pass */ \
  685. if (temp == 1) \
  686. EOB = k + koffset; /* EOB = index of last newly-nonzero coef */ \
  687. } \
  688. }
  689. METHODDEF(int)
  690. encode_mcu_AC_refine_prepare(const JCOEF *block,
  691. const int *jpeg_natural_order_start, int Sl,
  692. int Al, JCOEF *absvalues, size_t *bits)
  693. {
  694. register int k, temp, temp2;
  695. int EOB = 0;
  696. size_t zerobits = 0U, signbits = 0U;
  697. int Sl0 = Sl;
  698. #if SIZEOF_SIZE_T == 4
  699. if (Sl0 > 32)
  700. Sl0 = 32;
  701. #endif
  702. COMPUTE_ABSVALUES_AC_REFINE(Sl0, 0);
  703. bits[0] = zerobits;
  704. #if SIZEOF_SIZE_T == 8
  705. bits[1] = signbits;
  706. #else
  707. bits[2] = signbits;
  708. zerobits = 0U;
  709. signbits = 0U;
  710. if (Sl > 32) {
  711. Sl -= 32;
  712. jpeg_natural_order_start += 32;
  713. absvalues += 32;
  714. COMPUTE_ABSVALUES_AC_REFINE(Sl, 32);
  715. }
  716. bits[1] = zerobits;
  717. bits[3] = signbits;
  718. #endif
  719. return EOB;
  720. }
  721. /*
  722. * MCU encoding for AC successive approximation refinement scan.
  723. */
  724. #define ENCODE_COEFS_AC_REFINE(label) { \
  725. while (zerobits) { \
  726. int idx = count_zeroes(&zerobits); \
  727. r += idx; \
  728. cabsvalue += idx; \
  729. signbits >>= idx; \
  730. label \
  731. /* Emit any required ZRLs, but not if they can be folded into EOB */ \
  732. while (r > 15 && (cabsvalue <= EOBPTR)) { \
  733. /* emit any pending EOBRUN and the BE correction bits */ \
  734. emit_eobrun(entropy); \
  735. /* Emit ZRL */ \
  736. emit_symbol(entropy, entropy->ac_tbl_no, 0xF0); \
  737. r -= 16; \
  738. /* Emit buffered correction bits that must be associated with ZRL */ \
  739. emit_buffered_bits(entropy, BR_buffer, BR); \
  740. BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \
  741. BR = 0; \
  742. } \
  743. \
  744. temp = *cabsvalue++; \
  745. \
  746. /* If the coef was previously nonzero, it only needs a correction bit. \
  747. * NOTE: a straight translation of the spec's figure G.7 would suggest \
  748. * that we also need to test r > 15. But if r > 15, we can only get here \
  749. * if k > EOB, which implies that this coefficient is not 1. \
  750. */ \
  751. if (temp > 1) { \
  752. /* The correction bit is the next bit of the absolute value. */ \
  753. BR_buffer[BR++] = (char)(temp & 1); \
  754. signbits >>= 1; \
  755. zerobits >>= 1; \
  756. continue; \
  757. } \
  758. \
  759. /* Emit any pending EOBRUN and the BE correction bits */ \
  760. emit_eobrun(entropy); \
  761. \
  762. /* Count/emit Huffman symbol for run length / number of bits */ \
  763. emit_symbol(entropy, entropy->ac_tbl_no, (r << 4) + 1); \
  764. \
  765. /* Emit output bit for newly-nonzero coef */ \
  766. temp = signbits & 1; /* ((*block)[jpeg_natural_order_start[k]] < 0) ? 0 : 1 */ \
  767. emit_bits(entropy, (unsigned int)temp, 1); \
  768. \
  769. /* Emit buffered correction bits that must be associated with this code */ \
  770. emit_buffered_bits(entropy, BR_buffer, BR); \
  771. BR_buffer = entropy->bit_buffer; /* BE bits are gone now */ \
  772. BR = 0; \
  773. r = 0; /* reset zero run length */ \
  774. signbits >>= 1; \
  775. zerobits >>= 1; \
  776. } \
  777. }
  778. METHODDEF(boolean)
  779. encode_mcu_AC_refine (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  780. {
  781. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  782. register int temp, r;
  783. char *BR_buffer;
  784. unsigned int BR;
  785. int Sl = cinfo->Se - cinfo->Ss + 1;
  786. int Al = cinfo->Al;
  787. JCOEF absvalues_unaligned[DCTSIZE2 + 15];
  788. JCOEF *absvalues;
  789. const JCOEF *cabsvalue, *EOBPTR;
  790. size_t zerobits, signbits;
  791. size_t bits[16 / SIZEOF_SIZE_T];
  792. entropy->next_output_byte = cinfo->dest->next_output_byte;
  793. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  794. /* Emit restart marker if needed */
  795. if (cinfo->restart_interval)
  796. if (entropy->restarts_to_go == 0)
  797. emit_restart(entropy, entropy->next_restart_num);
  798. #ifdef WITH_SIMD
  799. cabsvalue = absvalues = (JCOEF *)PAD((size_t)absvalues_unaligned, 16);
  800. #else
  801. /* Not using SIMD, so alignment is not needed */
  802. cabsvalue = absvalues = absvalues_unaligned;
  803. #endif
  804. /* Prepare data */
  805. EOBPTR = absvalues +
  806. entropy->AC_refine_prepare(MCU_data[0][0], jpeg_natural_order + cinfo->Ss,
  807. Sl, Al, absvalues, bits);
  808. /* Encode the AC coefficients per section G.1.2.3, fig. G.7 */
  809. r = 0; /* r = run length of zeros */
  810. BR = 0; /* BR = count of buffered bits added now */
  811. BR_buffer = entropy->bit_buffer + entropy->BE; /* Append bits to buffer */
  812. zerobits = bits[0];
  813. #if SIZEOF_SIZE_T == 8
  814. signbits = bits[1];
  815. #else
  816. signbits = bits[2];
  817. #endif
  818. ENCODE_COEFS_AC_REFINE((void)0;);
  819. #if SIZEOF_SIZE_T == 4
  820. zerobits = bits[1];
  821. signbits = bits[3];
  822. if (zerobits) {
  823. int diff = ((absvalues + DCTSIZE2 / 2) - cabsvalue);
  824. int idx = count_zeroes(&zerobits);
  825. signbits >>= idx;
  826. idx += diff;
  827. r += idx;
  828. cabsvalue += idx;
  829. goto first_iter_ac_refine;
  830. }
  831. ENCODE_COEFS_AC_REFINE(first_iter_ac_refine:);
  832. #endif
  833. r |= (int)((absvalues + Sl) - cabsvalue);
  834. if (r > 0 || BR > 0) { /* If there are trailing zeroes, */
  835. entropy->EOBRUN++; /* count an EOB */
  836. entropy->BE += BR; /* concat my correction bits to older ones */
  837. /* We force out the EOB if we risk either:
  838. * 1. overflow of the EOB counter;
  839. * 2. overflow of the correction bit buffer during the next MCU.
  840. */
  841. if (entropy->EOBRUN == 0x7FFF ||
  842. entropy->BE > (MAX_CORR_BITS - DCTSIZE2 + 1))
  843. emit_eobrun(entropy);
  844. }
  845. cinfo->dest->next_output_byte = entropy->next_output_byte;
  846. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  847. /* Update restart-interval state too */
  848. if (cinfo->restart_interval) {
  849. if (entropy->restarts_to_go == 0) {
  850. entropy->restarts_to_go = cinfo->restart_interval;
  851. entropy->next_restart_num++;
  852. entropy->next_restart_num &= 7;
  853. }
  854. entropy->restarts_to_go--;
  855. }
  856. return TRUE;
  857. }
  858. /*
  859. * Finish up at the end of a Huffman-compressed progressive scan.
  860. */
  861. METHODDEF(void)
  862. finish_pass_phuff (j_compress_ptr cinfo)
  863. {
  864. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  865. entropy->next_output_byte = cinfo->dest->next_output_byte;
  866. entropy->free_in_buffer = cinfo->dest->free_in_buffer;
  867. /* Flush out any buffered data */
  868. emit_eobrun(entropy);
  869. flush_bits(entropy);
  870. cinfo->dest->next_output_byte = entropy->next_output_byte;
  871. cinfo->dest->free_in_buffer = entropy->free_in_buffer;
  872. }
  873. /*
  874. * Finish up a statistics-gathering pass and create the new Huffman tables.
  875. */
  876. METHODDEF(void)
  877. finish_pass_gather_phuff (j_compress_ptr cinfo)
  878. {
  879. phuff_entropy_ptr entropy = (phuff_entropy_ptr) cinfo->entropy;
  880. boolean is_DC_band;
  881. int ci, tbl;
  882. jpeg_component_info *compptr;
  883. JHUFF_TBL **htblptr;
  884. boolean did[NUM_HUFF_TBLS];
  885. /* Flush out buffered data (all we care about is counting the EOB symbol) */
  886. emit_eobrun(entropy);
  887. is_DC_band = (cinfo->Ss == 0);
  888. /* It's important not to apply jpeg_gen_optimal_table more than once
  889. * per table, because it clobbers the input frequency counts!
  890. */
  891. MEMZERO(did, sizeof(did));
  892. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  893. compptr = cinfo->cur_comp_info[ci];
  894. if (is_DC_band) {
  895. if (cinfo->Ah != 0) /* DC refinement needs no table */
  896. continue;
  897. tbl = compptr->dc_tbl_no;
  898. } else {
  899. tbl = compptr->ac_tbl_no;
  900. }
  901. if (! did[tbl]) {
  902. if (is_DC_band)
  903. htblptr = & cinfo->dc_huff_tbl_ptrs[tbl];
  904. else
  905. htblptr = & cinfo->ac_huff_tbl_ptrs[tbl];
  906. if (*htblptr == NULL)
  907. *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
  908. jpeg_gen_optimal_table(cinfo, *htblptr, entropy->count_ptrs[tbl]);
  909. did[tbl] = TRUE;
  910. }
  911. }
  912. }
  913. /*
  914. * Module initialization routine for progressive Huffman entropy encoding.
  915. */
  916. GLOBAL(void)
  917. jinit_phuff_encoder (j_compress_ptr cinfo)
  918. {
  919. phuff_entropy_ptr entropy;
  920. int i;
  921. entropy = (phuff_entropy_ptr)
  922. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  923. sizeof(phuff_entropy_encoder));
  924. cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;
  925. entropy->pub.start_pass = start_pass_phuff;
  926. /* Mark tables unallocated */
  927. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  928. entropy->derived_tbls[i] = NULL;
  929. entropy->count_ptrs[i] = NULL;
  930. }
  931. entropy->bit_buffer = NULL; /* needed only in AC refinement scan */
  932. }
  933. #endif /* C_PROGRESSIVE_SUPPORTED */