jchuff.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /*
  2. * jchuff.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1997, Thomas G. Lane.
  6. * libjpeg-turbo Modifications:
  7. * Copyright (C) 2009-2011, 2014-2016, 2018-2022, D. R. Commander.
  8. * Copyright (C) 2015, Matthieu Darbois.
  9. * Copyright (C) 2018, Matthias Räncker.
  10. * Copyright (C) 2020, Arm Limited.
  11. * For conditions of distribution and use, see the accompanying README.ijg
  12. * file.
  13. *
  14. * This file contains Huffman entropy encoding routines.
  15. *
  16. * Much of the complexity here has to do with supporting output suspension.
  17. * If the data destination module demands suspension, we want to be able to
  18. * back up to the start of the current MCU. To do this, we copy state
  19. * variables into local working storage, and update them back to the
  20. * permanent JPEG objects only upon successful completion of an MCU.
  21. *
  22. * NOTE: All referenced figures are from
  23. * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994.
  24. */
  25. #define JPEG_INTERNALS
  26. #include "jinclude.h"
  27. #include "jpeglib.h"
  28. #include "jsimd.h"
  29. #include "jconfigint.h"
  30. #include <limits.h>
  31. /*
  32. * NOTE: If USE_CLZ_INTRINSIC is defined, then clz/bsr instructions will be
  33. * used for bit counting rather than the lookup table. This will reduce the
  34. * memory footprint by 64k, which is important for some mobile applications
  35. * that create many isolated instances of libjpeg-turbo (web browsers, for
  36. * instance.) This may improve performance on some mobile platforms as well.
  37. * This feature is enabled by default only on Arm processors, because some x86
  38. * chips have a slow implementation of bsr, and the use of clz/bsr cannot be
  39. * shown to have a significant performance impact even on the x86 chips that
  40. * have a fast implementation of it. When building for Armv6, you can
  41. * explicitly disable the use of clz/bsr by adding -mthumb to the compiler
  42. * flags (this defines __thumb__).
  43. */
  44. /* NOTE: Both GCC and Clang define __GNUC__ */
  45. #if (defined(__GNUC__) && (defined(__arm__) || defined(__aarch64__))) || \
  46. defined(_M_ARM) || defined(_M_ARM64)
  47. #if !defined(__thumb__) || defined(__thumb2__)
  48. #define USE_CLZ_INTRINSIC
  49. #endif
  50. #endif
  51. #ifdef USE_CLZ_INTRINSIC
  52. #if defined(_MSC_VER) && !defined(__clang__)
  53. #define JPEG_NBITS_NONZERO(x) (32 - _CountLeadingZeros(x))
  54. #else
  55. #define JPEG_NBITS_NONZERO(x) (32 - __builtin_clz(x))
  56. #endif
  57. #define JPEG_NBITS(x) (x ? JPEG_NBITS_NONZERO(x) : 0)
  58. #else
  59. #include "jpeg_nbits_table.h"
  60. #define JPEG_NBITS(x) (jpeg_nbits_table[x])
  61. #define JPEG_NBITS_NONZERO(x) JPEG_NBITS(x)
  62. #endif
  63. /* Expanded entropy encoder object for Huffman encoding.
  64. *
  65. * The savable_state subrecord contains fields that change within an MCU,
  66. * but must not be updated permanently until we complete the MCU.
  67. */
  68. #if defined(__x86_64__) && defined(__ILP32__)
  69. typedef unsigned long long bit_buf_type;
  70. #else
  71. typedef size_t bit_buf_type;
  72. #endif
  73. /* NOTE: The more optimal Huffman encoding algorithm is only used by the
  74. * intrinsics implementation of the Arm Neon SIMD extensions, which is why we
  75. * retain the old Huffman encoder behavior when using the GAS implementation.
  76. */
  77. #if defined(WITH_SIMD) && !(defined(__arm__) || defined(__aarch64__) || \
  78. defined(_M_ARM) || defined(_M_ARM64))
  79. typedef unsigned long long simd_bit_buf_type;
  80. #else
  81. typedef bit_buf_type simd_bit_buf_type;
  82. #endif
  83. #if (defined(SIZEOF_SIZE_T) && SIZEOF_SIZE_T == 8) || defined(_WIN64) || \
  84. (defined(__x86_64__) && defined(__ILP32__))
  85. #define BIT_BUF_SIZE 64
  86. #elif (defined(SIZEOF_SIZE_T) && SIZEOF_SIZE_T == 4) || defined(_WIN32)
  87. #define BIT_BUF_SIZE 32
  88. #else
  89. #error Cannot determine word size
  90. #endif
  91. #define SIMD_BIT_BUF_SIZE (sizeof(simd_bit_buf_type) * 8)
  92. typedef struct {
  93. union {
  94. bit_buf_type c;
  95. simd_bit_buf_type simd;
  96. } put_buffer; /* current bit accumulation buffer */
  97. int free_bits; /* # of bits available in it */
  98. /* (Neon GAS: # of bits now in it) */
  99. int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
  100. } savable_state;
  101. typedef struct {
  102. struct jpeg_entropy_encoder pub; /* public fields */
  103. savable_state saved; /* Bit buffer & DC state at start of MCU */
  104. /* These fields are NOT loaded into local working state. */
  105. unsigned int restarts_to_go; /* MCUs left in this restart interval */
  106. int next_restart_num; /* next restart number to write (0-7) */
  107. /* Pointers to derived tables (these workspaces have image lifespan) */
  108. c_derived_tbl *dc_derived_tbls[NUM_HUFF_TBLS];
  109. c_derived_tbl *ac_derived_tbls[NUM_HUFF_TBLS];
  110. #ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */
  111. long *dc_count_ptrs[NUM_HUFF_TBLS];
  112. long *ac_count_ptrs[NUM_HUFF_TBLS];
  113. #endif
  114. int simd;
  115. } huff_entropy_encoder;
  116. typedef huff_entropy_encoder *huff_entropy_ptr;
  117. /* Working state while writing an MCU.
  118. * This struct contains all the fields that are needed by subroutines.
  119. */
  120. typedef struct {
  121. JOCTET *next_output_byte; /* => next byte to write in buffer */
  122. size_t free_in_buffer; /* # of byte spaces remaining in buffer */
  123. savable_state cur; /* Current bit buffer & DC state */
  124. j_compress_ptr cinfo; /* dump_buffer needs access to this */
  125. int simd;
  126. } working_state;
  127. /* Forward declarations */
  128. METHODDEF(boolean) encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data);
  129. METHODDEF(void) finish_pass_huff(j_compress_ptr cinfo);
  130. #ifdef ENTROPY_OPT_SUPPORTED
  131. METHODDEF(boolean) encode_mcu_gather(j_compress_ptr cinfo,
  132. JBLOCKROW *MCU_data);
  133. METHODDEF(void) finish_pass_gather(j_compress_ptr cinfo);
  134. #endif
  135. /*
  136. * Initialize for a Huffman-compressed scan.
  137. * If gather_statistics is TRUE, we do not output anything during the scan,
  138. * just count the Huffman symbols used and generate Huffman code tables.
  139. */
  140. METHODDEF(void)
  141. start_pass_huff(j_compress_ptr cinfo, boolean gather_statistics)
  142. {
  143. huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
  144. int ci, dctbl, actbl;
  145. jpeg_component_info *compptr;
  146. if (gather_statistics) {
  147. #ifdef ENTROPY_OPT_SUPPORTED
  148. entropy->pub.encode_mcu = encode_mcu_gather;
  149. entropy->pub.finish_pass = finish_pass_gather;
  150. #else
  151. ERREXIT(cinfo, JERR_NOT_COMPILED);
  152. #endif
  153. } else {
  154. entropy->pub.encode_mcu = encode_mcu_huff;
  155. entropy->pub.finish_pass = finish_pass_huff;
  156. }
  157. entropy->simd = jsimd_can_huff_encode_one_block();
  158. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  159. compptr = cinfo->cur_comp_info[ci];
  160. dctbl = compptr->dc_tbl_no;
  161. actbl = compptr->ac_tbl_no;
  162. if (gather_statistics) {
  163. #ifdef ENTROPY_OPT_SUPPORTED
  164. /* Check for invalid table indexes */
  165. /* (make_c_derived_tbl does this in the other path) */
  166. if (dctbl < 0 || dctbl >= NUM_HUFF_TBLS)
  167. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dctbl);
  168. if (actbl < 0 || actbl >= NUM_HUFF_TBLS)
  169. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, actbl);
  170. /* Allocate and zero the statistics tables */
  171. /* Note that jpeg_gen_optimal_table expects 257 entries in each table! */
  172. if (entropy->dc_count_ptrs[dctbl] == NULL)
  173. entropy->dc_count_ptrs[dctbl] = (long *)
  174. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  175. 257 * sizeof(long));
  176. memset(entropy->dc_count_ptrs[dctbl], 0, 257 * sizeof(long));
  177. if (entropy->ac_count_ptrs[actbl] == NULL)
  178. entropy->ac_count_ptrs[actbl] = (long *)
  179. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  180. 257 * sizeof(long));
  181. memset(entropy->ac_count_ptrs[actbl], 0, 257 * sizeof(long));
  182. #endif
  183. } else {
  184. /* Compute derived values for Huffman tables */
  185. /* We may do this more than once for a table, but it's not expensive */
  186. jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl,
  187. &entropy->dc_derived_tbls[dctbl]);
  188. jpeg_make_c_derived_tbl(cinfo, FALSE, actbl,
  189. &entropy->ac_derived_tbls[actbl]);
  190. }
  191. /* Initialize DC predictions to 0 */
  192. entropy->saved.last_dc_val[ci] = 0;
  193. }
  194. /* Initialize bit buffer to empty */
  195. if (entropy->simd) {
  196. entropy->saved.put_buffer.simd = 0;
  197. #if defined(__aarch64__) && !defined(NEON_INTRINSICS)
  198. entropy->saved.free_bits = 0;
  199. #else
  200. entropy->saved.free_bits = SIMD_BIT_BUF_SIZE;
  201. #endif
  202. } else {
  203. entropy->saved.put_buffer.c = 0;
  204. entropy->saved.free_bits = BIT_BUF_SIZE;
  205. }
  206. /* Initialize restart stuff */
  207. entropy->restarts_to_go = cinfo->restart_interval;
  208. entropy->next_restart_num = 0;
  209. }
  210. /*
  211. * Compute the derived values for a Huffman table.
  212. * This routine also performs some validation checks on the table.
  213. *
  214. * Note this is also used by jcphuff.c.
  215. */
  216. GLOBAL(void)
  217. jpeg_make_c_derived_tbl(j_compress_ptr cinfo, boolean isDC, int tblno,
  218. c_derived_tbl **pdtbl)
  219. {
  220. JHUFF_TBL *htbl;
  221. c_derived_tbl *dtbl;
  222. int p, i, l, lastp, si, maxsymbol;
  223. char huffsize[257];
  224. unsigned int huffcode[257];
  225. unsigned int code;
  226. /* Note that huffsize[] and huffcode[] are filled in code-length order,
  227. * paralleling the order of the symbols themselves in htbl->huffval[].
  228. */
  229. /* Find the input Huffman table */
  230. if (tblno < 0 || tblno >= NUM_HUFF_TBLS)
  231. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  232. htbl =
  233. isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno];
  234. if (htbl == NULL)
  235. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno);
  236. /* Allocate a workspace if we haven't already done so. */
  237. if (*pdtbl == NULL)
  238. *pdtbl = (c_derived_tbl *)
  239. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  240. sizeof(c_derived_tbl));
  241. dtbl = *pdtbl;
  242. /* Figure C.1: make table of Huffman code length for each symbol */
  243. p = 0;
  244. for (l = 1; l <= 16; l++) {
  245. i = (int)htbl->bits[l];
  246. if (i < 0 || p + i > 256) /* protect against table overrun */
  247. ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  248. while (i--)
  249. huffsize[p++] = (char)l;
  250. }
  251. huffsize[p] = 0;
  252. lastp = p;
  253. /* Figure C.2: generate the codes themselves */
  254. /* We also validate that the counts represent a legal Huffman code tree. */
  255. code = 0;
  256. si = huffsize[0];
  257. p = 0;
  258. while (huffsize[p]) {
  259. while (((int)huffsize[p]) == si) {
  260. huffcode[p++] = code;
  261. code++;
  262. }
  263. /* code is now 1 more than the last code used for codelength si; but
  264. * it must still fit in si bits, since no code is allowed to be all ones.
  265. */
  266. if (((JLONG)code) >= (((JLONG)1) << si))
  267. ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  268. code <<= 1;
  269. si++;
  270. }
  271. /* Figure C.3: generate encoding tables */
  272. /* These are code and size indexed by symbol value */
  273. /* Set all codeless symbols to have code length 0;
  274. * this lets us detect duplicate VAL entries here, and later
  275. * allows emit_bits to detect any attempt to emit such symbols.
  276. */
  277. memset(dtbl->ehufco, 0, sizeof(dtbl->ehufco));
  278. memset(dtbl->ehufsi, 0, sizeof(dtbl->ehufsi));
  279. /* This is also a convenient place to check for out-of-range
  280. * and duplicated VAL entries. We allow 0..255 for AC symbols
  281. * but only 0..15 for DC. (We could constrain them further
  282. * based on data depth and mode, but this seems enough.)
  283. */
  284. maxsymbol = isDC ? 15 : 255;
  285. for (p = 0; p < lastp; p++) {
  286. i = htbl->huffval[p];
  287. if (i < 0 || i > maxsymbol || dtbl->ehufsi[i])
  288. ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
  289. dtbl->ehufco[i] = huffcode[p];
  290. dtbl->ehufsi[i] = huffsize[p];
  291. }
  292. }
  293. /* Outputting bytes to the file */
  294. /* Emit a byte, taking 'action' if must suspend. */
  295. #define emit_byte(state, val, action) { \
  296. *(state)->next_output_byte++ = (JOCTET)(val); \
  297. if (--(state)->free_in_buffer == 0) \
  298. if (!dump_buffer(state)) \
  299. { action; } \
  300. }
  301. LOCAL(boolean)
  302. dump_buffer(working_state *state)
  303. /* Empty the output buffer; return TRUE if successful, FALSE if must suspend */
  304. {
  305. struct jpeg_destination_mgr *dest = state->cinfo->dest;
  306. if (!(*dest->empty_output_buffer) (state->cinfo))
  307. return FALSE;
  308. /* After a successful buffer dump, must reset buffer pointers */
  309. state->next_output_byte = dest->next_output_byte;
  310. state->free_in_buffer = dest->free_in_buffer;
  311. return TRUE;
  312. }
  313. /* Outputting bits to the file */
  314. /* Output byte b and, speculatively, an additional 0 byte. 0xFF must be
  315. * encoded as 0xFF 0x00, so the output buffer pointer is advanced by 2 if the
  316. * byte is 0xFF. Otherwise, the output buffer pointer is advanced by 1, and
  317. * the speculative 0 byte will be overwritten by the next byte.
  318. */
  319. #define EMIT_BYTE(b) { \
  320. buffer[0] = (JOCTET)(b); \
  321. buffer[1] = 0; \
  322. buffer -= -2 + ((JOCTET)(b) < 0xFF); \
  323. }
  324. /* Output the entire bit buffer. If there are no 0xFF bytes in it, then write
  325. * directly to the output buffer. Otherwise, use the EMIT_BYTE() macro to
  326. * encode 0xFF as 0xFF 0x00.
  327. */
  328. #if BIT_BUF_SIZE == 64
  329. #define FLUSH() { \
  330. if (put_buffer & 0x8080808080808080 & ~(put_buffer + 0x0101010101010101)) { \
  331. EMIT_BYTE(put_buffer >> 56) \
  332. EMIT_BYTE(put_buffer >> 48) \
  333. EMIT_BYTE(put_buffer >> 40) \
  334. EMIT_BYTE(put_buffer >> 32) \
  335. EMIT_BYTE(put_buffer >> 24) \
  336. EMIT_BYTE(put_buffer >> 16) \
  337. EMIT_BYTE(put_buffer >> 8) \
  338. EMIT_BYTE(put_buffer ) \
  339. } else { \
  340. buffer[0] = (JOCTET)(put_buffer >> 56); \
  341. buffer[1] = (JOCTET)(put_buffer >> 48); \
  342. buffer[2] = (JOCTET)(put_buffer >> 40); \
  343. buffer[3] = (JOCTET)(put_buffer >> 32); \
  344. buffer[4] = (JOCTET)(put_buffer >> 24); \
  345. buffer[5] = (JOCTET)(put_buffer >> 16); \
  346. buffer[6] = (JOCTET)(put_buffer >> 8); \
  347. buffer[7] = (JOCTET)(put_buffer); \
  348. buffer += 8; \
  349. } \
  350. }
  351. #else
  352. #define FLUSH() { \
  353. if (put_buffer & 0x80808080 & ~(put_buffer + 0x01010101)) { \
  354. EMIT_BYTE(put_buffer >> 24) \
  355. EMIT_BYTE(put_buffer >> 16) \
  356. EMIT_BYTE(put_buffer >> 8) \
  357. EMIT_BYTE(put_buffer ) \
  358. } else { \
  359. buffer[0] = (JOCTET)(put_buffer >> 24); \
  360. buffer[1] = (JOCTET)(put_buffer >> 16); \
  361. buffer[2] = (JOCTET)(put_buffer >> 8); \
  362. buffer[3] = (JOCTET)(put_buffer); \
  363. buffer += 4; \
  364. } \
  365. }
  366. #endif
  367. /* Fill the bit buffer to capacity with the leading bits from code, then output
  368. * the bit buffer and put the remaining bits from code into the bit buffer.
  369. */
  370. #define PUT_AND_FLUSH(code, size) { \
  371. put_buffer = (put_buffer << (size + free_bits)) | (code >> -free_bits); \
  372. FLUSH() \
  373. free_bits += BIT_BUF_SIZE; \
  374. put_buffer = code; \
  375. }
  376. /* Insert code into the bit buffer and output the bit buffer if needed.
  377. * NOTE: We can't flush with free_bits == 0, since the left shift in
  378. * PUT_AND_FLUSH() would have undefined behavior.
  379. */
  380. #define PUT_BITS(code, size) { \
  381. free_bits -= size; \
  382. if (free_bits < 0) \
  383. PUT_AND_FLUSH(code, size) \
  384. else \
  385. put_buffer = (put_buffer << size) | code; \
  386. }
  387. #define PUT_CODE(code, size) { \
  388. temp &= (((JLONG)1) << nbits) - 1; \
  389. temp |= code << nbits; \
  390. nbits += size; \
  391. PUT_BITS(temp, nbits) \
  392. }
  393. /* Although it is exceedingly rare, it is possible for a Huffman-encoded
  394. * coefficient block to be larger than the 128-byte unencoded block. For each
  395. * of the 64 coefficients, PUT_BITS is invoked twice, and each invocation can
  396. * theoretically store 16 bits (for a maximum of 2048 bits or 256 bytes per
  397. * encoded block.) If, for instance, one artificially sets the AC
  398. * coefficients to alternating values of 32767 and -32768 (using the JPEG
  399. * scanning order-- 1, 8, 16, etc.), then this will produce an encoded block
  400. * larger than 200 bytes.
  401. */
  402. #define BUFSIZE (DCTSIZE2 * 8)
  403. #define LOAD_BUFFER() { \
  404. if (state->free_in_buffer < BUFSIZE) { \
  405. localbuf = 1; \
  406. buffer = _buffer; \
  407. } else \
  408. buffer = state->next_output_byte; \
  409. }
  410. #define STORE_BUFFER() { \
  411. if (localbuf) { \
  412. size_t bytes, bytestocopy; \
  413. bytes = buffer - _buffer; \
  414. buffer = _buffer; \
  415. while (bytes > 0) { \
  416. bytestocopy = MIN(bytes, state->free_in_buffer); \
  417. memcpy(state->next_output_byte, buffer, bytestocopy); \
  418. state->next_output_byte += bytestocopy; \
  419. buffer += bytestocopy; \
  420. state->free_in_buffer -= bytestocopy; \
  421. if (state->free_in_buffer == 0) \
  422. if (!dump_buffer(state)) return FALSE; \
  423. bytes -= bytestocopy; \
  424. } \
  425. } else { \
  426. state->free_in_buffer -= (buffer - state->next_output_byte); \
  427. state->next_output_byte = buffer; \
  428. } \
  429. }
  430. LOCAL(boolean)
  431. flush_bits(working_state *state)
  432. {
  433. JOCTET _buffer[BUFSIZE], *buffer, temp;
  434. simd_bit_buf_type put_buffer; int put_bits;
  435. int localbuf = 0;
  436. if (state->simd) {
  437. #if defined(__aarch64__) && !defined(NEON_INTRINSICS)
  438. put_bits = state->cur.free_bits;
  439. #else
  440. put_bits = SIMD_BIT_BUF_SIZE - state->cur.free_bits;
  441. #endif
  442. put_buffer = state->cur.put_buffer.simd;
  443. } else {
  444. put_bits = BIT_BUF_SIZE - state->cur.free_bits;
  445. put_buffer = state->cur.put_buffer.c;
  446. }
  447. LOAD_BUFFER()
  448. while (put_bits >= 8) {
  449. put_bits -= 8;
  450. temp = (JOCTET)(put_buffer >> put_bits);
  451. EMIT_BYTE(temp)
  452. }
  453. if (put_bits) {
  454. /* fill partial byte with ones */
  455. temp = (JOCTET)((put_buffer << (8 - put_bits)) | (0xFF >> put_bits));
  456. EMIT_BYTE(temp)
  457. }
  458. if (state->simd) { /* and reset bit buffer to empty */
  459. state->cur.put_buffer.simd = 0;
  460. #if defined(__aarch64__) && !defined(NEON_INTRINSICS)
  461. state->cur.free_bits = 0;
  462. #else
  463. state->cur.free_bits = SIMD_BIT_BUF_SIZE;
  464. #endif
  465. } else {
  466. state->cur.put_buffer.c = 0;
  467. state->cur.free_bits = BIT_BUF_SIZE;
  468. }
  469. STORE_BUFFER()
  470. return TRUE;
  471. }
  472. /* Encode a single block's worth of coefficients */
  473. LOCAL(boolean)
  474. encode_one_block_simd(working_state *state, JCOEFPTR block, int last_dc_val,
  475. c_derived_tbl *dctbl, c_derived_tbl *actbl)
  476. {
  477. JOCTET _buffer[BUFSIZE], *buffer;
  478. int localbuf = 0;
  479. LOAD_BUFFER()
  480. buffer = jsimd_huff_encode_one_block(state, buffer, block, last_dc_val,
  481. dctbl, actbl);
  482. STORE_BUFFER()
  483. return TRUE;
  484. }
  485. LOCAL(boolean)
  486. encode_one_block(working_state *state, JCOEFPTR block, int last_dc_val,
  487. c_derived_tbl *dctbl, c_derived_tbl *actbl)
  488. {
  489. int temp, nbits, free_bits;
  490. bit_buf_type put_buffer;
  491. JOCTET _buffer[BUFSIZE], *buffer;
  492. int localbuf = 0;
  493. free_bits = state->cur.free_bits;
  494. put_buffer = state->cur.put_buffer.c;
  495. LOAD_BUFFER()
  496. /* Encode the DC coefficient difference per section F.1.2.1 */
  497. temp = block[0] - last_dc_val;
  498. /* This is a well-known technique for obtaining the absolute value without a
  499. * branch. It is derived from an assembly language technique presented in
  500. * "How to Optimize for the Pentium Processors", Copyright (c) 1996, 1997 by
  501. * Agner Fog. This code assumes we are on a two's complement machine.
  502. */
  503. nbits = temp >> (CHAR_BIT * sizeof(int) - 1);
  504. temp += nbits;
  505. nbits ^= temp;
  506. /* Find the number of bits needed for the magnitude of the coefficient */
  507. nbits = JPEG_NBITS(nbits);
  508. /* Emit the Huffman-coded symbol for the number of bits.
  509. * Emit that number of bits of the value, if positive,
  510. * or the complement of its magnitude, if negative.
  511. */
  512. PUT_CODE(dctbl->ehufco[nbits], dctbl->ehufsi[nbits])
  513. /* Encode the AC coefficients per section F.1.2.2 */
  514. {
  515. int r = 0; /* r = run length of zeros */
  516. /* Manually unroll the k loop to eliminate the counter variable. This
  517. * improves performance greatly on systems with a limited number of
  518. * registers (such as x86.)
  519. */
  520. #define kloop(jpeg_natural_order_of_k) { \
  521. if ((temp = block[jpeg_natural_order_of_k]) == 0) { \
  522. r += 16; \
  523. } else { \
  524. /* Branch-less absolute value, bitwise complement, etc., same as above */ \
  525. nbits = temp >> (CHAR_BIT * sizeof(int) - 1); \
  526. temp += nbits; \
  527. nbits ^= temp; \
  528. nbits = JPEG_NBITS_NONZERO(nbits); \
  529. /* if run length > 15, must emit special run-length-16 codes (0xF0) */ \
  530. while (r >= 16 * 16) { \
  531. r -= 16 * 16; \
  532. PUT_BITS(actbl->ehufco[0xf0], actbl->ehufsi[0xf0]) \
  533. } \
  534. /* Emit Huffman symbol for run length / number of bits */ \
  535. r += nbits; \
  536. PUT_CODE(actbl->ehufco[r], actbl->ehufsi[r]) \
  537. r = 0; \
  538. } \
  539. }
  540. /* One iteration for each value in jpeg_natural_order[] */
  541. kloop(1); kloop(8); kloop(16); kloop(9); kloop(2); kloop(3);
  542. kloop(10); kloop(17); kloop(24); kloop(32); kloop(25); kloop(18);
  543. kloop(11); kloop(4); kloop(5); kloop(12); kloop(19); kloop(26);
  544. kloop(33); kloop(40); kloop(48); kloop(41); kloop(34); kloop(27);
  545. kloop(20); kloop(13); kloop(6); kloop(7); kloop(14); kloop(21);
  546. kloop(28); kloop(35); kloop(42); kloop(49); kloop(56); kloop(57);
  547. kloop(50); kloop(43); kloop(36); kloop(29); kloop(22); kloop(15);
  548. kloop(23); kloop(30); kloop(37); kloop(44); kloop(51); kloop(58);
  549. kloop(59); kloop(52); kloop(45); kloop(38); kloop(31); kloop(39);
  550. kloop(46); kloop(53); kloop(60); kloop(61); kloop(54); kloop(47);
  551. kloop(55); kloop(62); kloop(63);
  552. /* If the last coef(s) were zero, emit an end-of-block code */
  553. if (r > 0) {
  554. PUT_BITS(actbl->ehufco[0], actbl->ehufsi[0])
  555. }
  556. }
  557. state->cur.put_buffer.c = put_buffer;
  558. state->cur.free_bits = free_bits;
  559. STORE_BUFFER()
  560. return TRUE;
  561. }
  562. /*
  563. * Emit a restart marker & resynchronize predictions.
  564. */
  565. LOCAL(boolean)
  566. emit_restart(working_state *state, int restart_num)
  567. {
  568. int ci;
  569. if (!flush_bits(state))
  570. return FALSE;
  571. emit_byte(state, 0xFF, return FALSE);
  572. emit_byte(state, JPEG_RST0 + restart_num, return FALSE);
  573. /* Re-initialize DC predictions to 0 */
  574. for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
  575. state->cur.last_dc_val[ci] = 0;
  576. /* The restart counter is not updated until we successfully write the MCU. */
  577. return TRUE;
  578. }
  579. /*
  580. * Encode and output one MCU's worth of Huffman-compressed coefficients.
  581. */
  582. METHODDEF(boolean)
  583. encode_mcu_huff(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  584. {
  585. huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
  586. working_state state;
  587. int blkn, ci;
  588. jpeg_component_info *compptr;
  589. /* Load up working state */
  590. state.next_output_byte = cinfo->dest->next_output_byte;
  591. state.free_in_buffer = cinfo->dest->free_in_buffer;
  592. state.cur = entropy->saved;
  593. state.cinfo = cinfo;
  594. state.simd = entropy->simd;
  595. /* Emit restart marker if needed */
  596. if (cinfo->restart_interval) {
  597. if (entropy->restarts_to_go == 0)
  598. if (!emit_restart(&state, entropy->next_restart_num))
  599. return FALSE;
  600. }
  601. /* Encode the MCU data blocks */
  602. if (entropy->simd) {
  603. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  604. ci = cinfo->MCU_membership[blkn];
  605. compptr = cinfo->cur_comp_info[ci];
  606. if (!encode_one_block_simd(&state,
  607. MCU_data[blkn][0], state.cur.last_dc_val[ci],
  608. entropy->dc_derived_tbls[compptr->dc_tbl_no],
  609. entropy->ac_derived_tbls[compptr->ac_tbl_no]))
  610. return FALSE;
  611. /* Update last_dc_val */
  612. state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
  613. }
  614. } else {
  615. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  616. ci = cinfo->MCU_membership[blkn];
  617. compptr = cinfo->cur_comp_info[ci];
  618. if (!encode_one_block(&state,
  619. MCU_data[blkn][0], state.cur.last_dc_val[ci],
  620. entropy->dc_derived_tbls[compptr->dc_tbl_no],
  621. entropy->ac_derived_tbls[compptr->ac_tbl_no]))
  622. return FALSE;
  623. /* Update last_dc_val */
  624. state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
  625. }
  626. }
  627. /* Completed MCU, so update state */
  628. cinfo->dest->next_output_byte = state.next_output_byte;
  629. cinfo->dest->free_in_buffer = state.free_in_buffer;
  630. entropy->saved = state.cur;
  631. /* Update restart-interval state too */
  632. if (cinfo->restart_interval) {
  633. if (entropy->restarts_to_go == 0) {
  634. entropy->restarts_to_go = cinfo->restart_interval;
  635. entropy->next_restart_num++;
  636. entropy->next_restart_num &= 7;
  637. }
  638. entropy->restarts_to_go--;
  639. }
  640. return TRUE;
  641. }
  642. /*
  643. * Finish up at the end of a Huffman-compressed scan.
  644. */
  645. METHODDEF(void)
  646. finish_pass_huff(j_compress_ptr cinfo)
  647. {
  648. huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
  649. working_state state;
  650. /* Load up working state ... flush_bits needs it */
  651. state.next_output_byte = cinfo->dest->next_output_byte;
  652. state.free_in_buffer = cinfo->dest->free_in_buffer;
  653. state.cur = entropy->saved;
  654. state.cinfo = cinfo;
  655. state.simd = entropy->simd;
  656. /* Flush out the last data */
  657. if (!flush_bits(&state))
  658. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  659. /* Update state */
  660. cinfo->dest->next_output_byte = state.next_output_byte;
  661. cinfo->dest->free_in_buffer = state.free_in_buffer;
  662. entropy->saved = state.cur;
  663. }
  664. /*
  665. * Huffman coding optimization.
  666. *
  667. * We first scan the supplied data and count the number of uses of each symbol
  668. * that is to be Huffman-coded. (This process MUST agree with the code above.)
  669. * Then we build a Huffman coding tree for the observed counts.
  670. * Symbols which are not needed at all for the particular image are not
  671. * assigned any code, which saves space in the DHT marker as well as in
  672. * the compressed data.
  673. */
  674. #ifdef ENTROPY_OPT_SUPPORTED
  675. /* Process a single block's worth of coefficients */
  676. LOCAL(void)
  677. htest_one_block(j_compress_ptr cinfo, JCOEFPTR block, int last_dc_val,
  678. long dc_counts[], long ac_counts[])
  679. {
  680. register int temp;
  681. register int nbits;
  682. register int k, r;
  683. /* Encode the DC coefficient difference per section F.1.2.1 */
  684. temp = block[0] - last_dc_val;
  685. if (temp < 0)
  686. temp = -temp;
  687. /* Find the number of bits needed for the magnitude of the coefficient */
  688. nbits = 0;
  689. while (temp) {
  690. nbits++;
  691. temp >>= 1;
  692. }
  693. /* Check for out-of-range coefficient values.
  694. * Since we're encoding a difference, the range limit is twice as much.
  695. */
  696. if (nbits > MAX_COEF_BITS + 1)
  697. ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  698. /* Count the Huffman symbol for the number of bits */
  699. dc_counts[nbits]++;
  700. /* Encode the AC coefficients per section F.1.2.2 */
  701. r = 0; /* r = run length of zeros */
  702. for (k = 1; k < DCTSIZE2; k++) {
  703. if ((temp = block[jpeg_natural_order[k]]) == 0) {
  704. r++;
  705. } else {
  706. /* if run length > 15, must emit special run-length-16 codes (0xF0) */
  707. while (r > 15) {
  708. ac_counts[0xF0]++;
  709. r -= 16;
  710. }
  711. /* Find the number of bits needed for the magnitude of the coefficient */
  712. if (temp < 0)
  713. temp = -temp;
  714. /* Find the number of bits needed for the magnitude of the coefficient */
  715. nbits = 1; /* there must be at least one 1 bit */
  716. while ((temp >>= 1))
  717. nbits++;
  718. /* Check for out-of-range coefficient values */
  719. if (nbits > MAX_COEF_BITS)
  720. ERREXIT(cinfo, JERR_BAD_DCT_COEF);
  721. /* Count Huffman symbol for run length / number of bits */
  722. ac_counts[(r << 4) + nbits]++;
  723. r = 0;
  724. }
  725. }
  726. /* If the last coef(s) were zero, emit an end-of-block code */
  727. if (r > 0)
  728. ac_counts[0]++;
  729. }
  730. /*
  731. * Trial-encode one MCU's worth of Huffman-compressed coefficients.
  732. * No data is actually output, so no suspension return is possible.
  733. */
  734. METHODDEF(boolean)
  735. encode_mcu_gather(j_compress_ptr cinfo, JBLOCKROW *MCU_data)
  736. {
  737. huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
  738. int blkn, ci;
  739. jpeg_component_info *compptr;
  740. /* Take care of restart intervals if needed */
  741. if (cinfo->restart_interval) {
  742. if (entropy->restarts_to_go == 0) {
  743. /* Re-initialize DC predictions to 0 */
  744. for (ci = 0; ci < cinfo->comps_in_scan; ci++)
  745. entropy->saved.last_dc_val[ci] = 0;
  746. /* Update restart state */
  747. entropy->restarts_to_go = cinfo->restart_interval;
  748. }
  749. entropy->restarts_to_go--;
  750. }
  751. for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
  752. ci = cinfo->MCU_membership[blkn];
  753. compptr = cinfo->cur_comp_info[ci];
  754. htest_one_block(cinfo, MCU_data[blkn][0], entropy->saved.last_dc_val[ci],
  755. entropy->dc_count_ptrs[compptr->dc_tbl_no],
  756. entropy->ac_count_ptrs[compptr->ac_tbl_no]);
  757. entropy->saved.last_dc_val[ci] = MCU_data[blkn][0][0];
  758. }
  759. return TRUE;
  760. }
  761. /*
  762. * Generate the best Huffman code table for the given counts, fill htbl.
  763. * Note this is also used by jcphuff.c.
  764. *
  765. * The JPEG standard requires that no symbol be assigned a codeword of all
  766. * one bits (so that padding bits added at the end of a compressed segment
  767. * can't look like a valid code). Because of the canonical ordering of
  768. * codewords, this just means that there must be an unused slot in the
  769. * longest codeword length category. Annex K (Clause K.2) of
  770. * Rec. ITU-T T.81 (1992) | ISO/IEC 10918-1:1994 suggests reserving such a slot
  771. * by pretending that symbol 256 is a valid symbol with count 1. In theory
  772. * that's not optimal; giving it count zero but including it in the symbol set
  773. * anyway should give a better Huffman code. But the theoretically better code
  774. * actually seems to come out worse in practice, because it produces more
  775. * all-ones bytes (which incur stuffed zero bytes in the final file). In any
  776. * case the difference is tiny.
  777. *
  778. * The JPEG standard requires Huffman codes to be no more than 16 bits long.
  779. * If some symbols have a very small but nonzero probability, the Huffman tree
  780. * must be adjusted to meet the code length restriction. We currently use
  781. * the adjustment method suggested in JPEG section K.2. This method is *not*
  782. * optimal; it may not choose the best possible limited-length code. But
  783. * typically only very-low-frequency symbols will be given less-than-optimal
  784. * lengths, so the code is almost optimal. Experimental comparisons against
  785. * an optimal limited-length-code algorithm indicate that the difference is
  786. * microscopic --- usually less than a hundredth of a percent of total size.
  787. * So the extra complexity of an optimal algorithm doesn't seem worthwhile.
  788. */
  789. GLOBAL(void)
  790. jpeg_gen_optimal_table(j_compress_ptr cinfo, JHUFF_TBL *htbl, long freq[])
  791. {
  792. #define MAX_CLEN 32 /* assumed maximum initial code length */
  793. UINT8 bits[MAX_CLEN + 1]; /* bits[k] = # of symbols with code length k */
  794. int codesize[257]; /* codesize[k] = code length of symbol k */
  795. int others[257]; /* next symbol in current branch of tree */
  796. int c1, c2;
  797. int p, i, j;
  798. long v;
  799. /* This algorithm is explained in section K.2 of the JPEG standard */
  800. memset(bits, 0, sizeof(bits));
  801. memset(codesize, 0, sizeof(codesize));
  802. for (i = 0; i < 257; i++)
  803. others[i] = -1; /* init links to empty */
  804. freq[256] = 1; /* make sure 256 has a nonzero count */
  805. /* Including the pseudo-symbol 256 in the Huffman procedure guarantees
  806. * that no real symbol is given code-value of all ones, because 256
  807. * will be placed last in the largest codeword category.
  808. */
  809. /* Huffman's basic algorithm to assign optimal code lengths to symbols */
  810. for (;;) {
  811. /* Find the smallest nonzero frequency, set c1 = its symbol */
  812. /* In case of ties, take the larger symbol number */
  813. c1 = -1;
  814. v = 1000000000L;
  815. for (i = 0; i <= 256; i++) {
  816. if (freq[i] && freq[i] <= v) {
  817. v = freq[i];
  818. c1 = i;
  819. }
  820. }
  821. /* Find the next smallest nonzero frequency, set c2 = its symbol */
  822. /* In case of ties, take the larger symbol number */
  823. c2 = -1;
  824. v = 1000000000L;
  825. for (i = 0; i <= 256; i++) {
  826. if (freq[i] && freq[i] <= v && i != c1) {
  827. v = freq[i];
  828. c2 = i;
  829. }
  830. }
  831. /* Done if we've merged everything into one frequency */
  832. if (c2 < 0)
  833. break;
  834. /* Else merge the two counts/trees */
  835. freq[c1] += freq[c2];
  836. freq[c2] = 0;
  837. /* Increment the codesize of everything in c1's tree branch */
  838. codesize[c1]++;
  839. while (others[c1] >= 0) {
  840. c1 = others[c1];
  841. codesize[c1]++;
  842. }
  843. others[c1] = c2; /* chain c2 onto c1's tree branch */
  844. /* Increment the codesize of everything in c2's tree branch */
  845. codesize[c2]++;
  846. while (others[c2] >= 0) {
  847. c2 = others[c2];
  848. codesize[c2]++;
  849. }
  850. }
  851. /* Now count the number of symbols of each code length */
  852. for (i = 0; i <= 256; i++) {
  853. if (codesize[i]) {
  854. /* The JPEG standard seems to think that this can't happen, */
  855. /* but I'm paranoid... */
  856. if (codesize[i] > MAX_CLEN)
  857. ERREXIT(cinfo, JERR_HUFF_CLEN_OVERFLOW);
  858. bits[codesize[i]]++;
  859. }
  860. }
  861. /* JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
  862. * Huffman procedure assigned any such lengths, we must adjust the coding.
  863. * Here is what Rec. ITU-T T.81 | ISO/IEC 10918-1 says about how this next
  864. * bit works: Since symbols are paired for the longest Huffman code, the
  865. * symbols are removed from this length category two at a time. The prefix
  866. * for the pair (which is one bit shorter) is allocated to one of the pair;
  867. * then, skipping the BITS entry for that prefix length, a code word from the
  868. * next shortest nonzero BITS entry is converted into a prefix for two code
  869. * words one bit longer.
  870. */
  871. for (i = MAX_CLEN; i > 16; i--) {
  872. while (bits[i] > 0) {
  873. j = i - 2; /* find length of new prefix to be used */
  874. while (bits[j] == 0)
  875. j--;
  876. bits[i] -= 2; /* remove two symbols */
  877. bits[i - 1]++; /* one goes in this length */
  878. bits[j + 1] += 2; /* two new symbols in this length */
  879. bits[j]--; /* symbol of this length is now a prefix */
  880. }
  881. }
  882. /* Remove the count for the pseudo-symbol 256 from the largest codelength */
  883. while (bits[i] == 0) /* find largest codelength still in use */
  884. i--;
  885. bits[i]--;
  886. /* Return final symbol counts (only for lengths 0..16) */
  887. memcpy(htbl->bits, bits, sizeof(htbl->bits));
  888. /* Return a list of the symbols sorted by code length */
  889. /* It's not real clear to me why we don't need to consider the codelength
  890. * changes made above, but Rec. ITU-T T.81 | ISO/IEC 10918-1 seems to think
  891. * this works.
  892. */
  893. p = 0;
  894. for (i = 1; i <= MAX_CLEN; i++) {
  895. for (j = 0; j <= 255; j++) {
  896. if (codesize[j] == i) {
  897. htbl->huffval[p] = (UINT8)j;
  898. p++;
  899. }
  900. }
  901. }
  902. /* Set sent_table FALSE so updated table will be written to JPEG file. */
  903. htbl->sent_table = FALSE;
  904. }
  905. /*
  906. * Finish up a statistics-gathering pass and create the new Huffman tables.
  907. */
  908. METHODDEF(void)
  909. finish_pass_gather(j_compress_ptr cinfo)
  910. {
  911. huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy;
  912. int ci, dctbl, actbl;
  913. jpeg_component_info *compptr;
  914. JHUFF_TBL **htblptr;
  915. boolean did_dc[NUM_HUFF_TBLS];
  916. boolean did_ac[NUM_HUFF_TBLS];
  917. /* It's important not to apply jpeg_gen_optimal_table more than once
  918. * per table, because it clobbers the input frequency counts!
  919. */
  920. memset(did_dc, 0, sizeof(did_dc));
  921. memset(did_ac, 0, sizeof(did_ac));
  922. for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  923. compptr = cinfo->cur_comp_info[ci];
  924. dctbl = compptr->dc_tbl_no;
  925. actbl = compptr->ac_tbl_no;
  926. if (!did_dc[dctbl]) {
  927. htblptr = &cinfo->dc_huff_tbl_ptrs[dctbl];
  928. if (*htblptr == NULL)
  929. *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);
  930. jpeg_gen_optimal_table(cinfo, *htblptr, entropy->dc_count_ptrs[dctbl]);
  931. did_dc[dctbl] = TRUE;
  932. }
  933. if (!did_ac[actbl]) {
  934. htblptr = &cinfo->ac_huff_tbl_ptrs[actbl];
  935. if (*htblptr == NULL)
  936. *htblptr = jpeg_alloc_huff_table((j_common_ptr)cinfo);
  937. jpeg_gen_optimal_table(cinfo, *htblptr, entropy->ac_count_ptrs[actbl]);
  938. did_ac[actbl] = TRUE;
  939. }
  940. }
  941. }
  942. #endif /* ENTROPY_OPT_SUPPORTED */
  943. /*
  944. * Module initialization routine for Huffman entropy encoding.
  945. */
  946. GLOBAL(void)
  947. jinit_huff_encoder(j_compress_ptr cinfo)
  948. {
  949. huff_entropy_ptr entropy;
  950. int i;
  951. entropy = (huff_entropy_ptr)
  952. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  953. sizeof(huff_entropy_encoder));
  954. cinfo->entropy = (struct jpeg_entropy_encoder *)entropy;
  955. entropy->pub.start_pass = start_pass_huff;
  956. /* Mark tables unallocated */
  957. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  958. entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL;
  959. #ifdef ENTROPY_OPT_SUPPORTED
  960. entropy->dc_count_ptrs[i] = entropy->ac_count_ptrs[i] = NULL;
  961. #endif
  962. }
  963. }