/* * jdhuff.c * * This file was part of the Independent JPEG Group's software: * Copyright (C) 1991-1997, Thomas G. Lane. * libjpeg-turbo Modifications: * Copyright (C) 2009-2011, 2016, 2018-2019, D. R. Commander. * Copyright (C) 2018, Matthias Räncker. * For conditions of distribution and use, see the accompanying README.ijg * file. * * This file contains Huffman entropy decoding routines. * * Much of the complexity here has to do with supporting input suspension. * If the data source module demands suspension, we want to be able to back * up to the start of the current MCU. To do this, we copy state variables * into local working storage, and update them back to the permanent * storage only upon successful completion of an MCU. * * NOTE: All referenced figures are from * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994. */ #define JPEG_INTERNALS #include "jinclude.h" #include "jpeglib.h" #include "jdhuff.h" /* Declarations shared with jdphuff.c */ #include "jpegcomp.h" #include "jstdhuff.c" /* * Expanded entropy decoder object for Huffman decoding. * * The savable_state subrecord contains fields that change within an MCU, * but must not be updated permanently until we complete the MCU. */ savable_state; huff_entropy_decoder; huff_entropy_ptr; /* * Initialize for a Huffman-compressed scan. */ METHODDEF(void) start_pass_huff_decoder(j_decompress_ptr cinfo) { … } /* * Compute the derived values for a Huffman table. * This routine also performs some validation checks on the table. * * Note this is also used by jdphuff.c. */ GLOBAL(void) jpeg_make_d_derived_tbl(j_decompress_ptr cinfo, boolean isDC, int tblno, d_derived_tbl **pdtbl) { … } /* * Out-of-line code for bit fetching (shared with jdphuff.c). * See jdhuff.h for info about usage. * Note: current values of get_buffer and bits_left are passed as parameters, * but are returned in the corresponding fields of the state struct. * * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width * of get_buffer to be used. (On machines with wider words, an even larger * buffer could be used.) However, on some machines 32-bit shifts are * quite slow and take time proportional to the number of places shifted. * (This is true with most PC compilers, for instance.) In this case it may * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the * average shift distance at the cost of more calls to jpeg_fill_bit_buffer. */ #ifdef SLOW_SHIFT_32 #define MIN_GET_BITS … #else #define MIN_GET_BITS … #endif GLOBAL(boolean) jpeg_fill_bit_buffer(bitread_working_state *state, register bit_buf_type get_buffer, register int bits_left, int nbits) /* Load up the bit buffer to a depth of at least nbits */ { … } /* Macro version of the above, which performs much better but does not handle markers. We have to hand off any blocks with markers to the slower routines. */ #define GET_BYTE … #if SIZEOF_SIZE_T == 8 || defined(_WIN64) || (defined(__x86_64__) && defined(__ILP32__)) /* Pre-fetch 48 bytes, because the holding register is 64-bit */ #define FILL_BIT_BUFFER_FAST … #else /* Pre-fetch 16 bytes, because the holding register is 32-bit */ #define FILL_BIT_BUFFER_FAST … #endif /* * Out-of-line code for Huffman code decoding. * See jdhuff.h for info about usage. */ GLOBAL(int) jpeg_huff_decode(bitread_working_state *state, register bit_buf_type get_buffer, register int bits_left, d_derived_tbl *htbl, int min_bits) { … } /* * Figure F.12: extend sign bit. * On some machines, a shift and add will be faster than a table lookup. */ #define AVOID_TABLES #ifdef AVOID_TABLES #define NEG_1 … #define HUFF_EXTEND(x, s) … #else #define HUFF_EXTEND … static const int extend_test[16] = { /* entry n is 2**(n-1) */ 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 }; static const int extend_offset[16] = { /* entry n is (-1 << n) + 1 */ 0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1, ((-1) << 4) + 1, ((-1) << 5) + 1, ((-1) << 6) + 1, ((-1) << 7) + 1, ((-1) << 8) + 1, ((-1) << 9) + 1, ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1, ((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1 }; #endif /* AVOID_TABLES */ /* * Check for a restart marker & resynchronize decoder. * Returns FALSE if must suspend. */ LOCAL(boolean) process_restart(j_decompress_ptr cinfo) { … } #if defined(__has_feature) #if __has_feature(undefined_behavior_sanitizer) __attribute__((no_sanitize("signed-integer-overflow"), no_sanitize("unsigned-integer-overflow"))) #endif #endif LOCAL(boolean) decode_mcu_slow(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { … } #if defined(__has_feature) #if __has_feature(undefined_behavior_sanitizer) __attribute__((no_sanitize("signed-integer-overflow"), no_sanitize("unsigned-integer-overflow"))) #endif #endif LOCAL(boolean) decode_mcu_fast(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { … } /* * Decode and return one MCU's worth of Huffman-compressed coefficients. * The coefficients are reordered from zigzag order into natural array order, * but are not dequantized. * * The i'th block of the MCU is stored into the block pointed to by * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER. * (Wholesale zeroing is usually a little faster than retail...) * * Returns FALSE if data source requested suspension. In that case no * changes have been made to permanent state. (Exception: some output * coefficients may already have been assigned. This is harmless for * this module, since we'll just re-assign them on the next call.) */ #define BUFSIZE … METHODDEF(boolean) decode_mcu(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) { … } /* * Module initialization routine for Huffman entropy decoding. */ GLOBAL(void) jinit_huff_decoder(j_decompress_ptr cinfo) { … }