Path: blob/master/thirdparty/libjpeg-turbo/src/jdhuff.h
9904 views
/*1* jdhuff.h2*3* This file was part of the Independent JPEG Group's software:4* Copyright (C) 1991-1997, Thomas G. Lane.5* Lossless JPEG Modifications:6* Copyright (C) 1999, Ken Murchison.7* libjpeg-turbo Modifications:8* Copyright (C) 2010-2011, 2015-2016, 2021, D. R. Commander.9* Copyright (C) 2018, Matthias Räncker.10* For conditions of distribution and use, see the accompanying README.ijg11* file.12*13* This file contains declarations for Huffman entropy decoding routines14* that are shared between the sequential decoder (jdhuff.c), the progressive15* decoder (jdphuff.c), and the lossless decoder (jdlhuff.c). No other modules16* need to see these.17*/1819#include "jconfigint.h"202122/* Derived data constructed for each Huffman table */2324#define HUFF_LOOKAHEAD 8 /* # of bits of lookahead */2526typedef struct {27/* Basic tables: (element [0] of each array is unused) */28JLONG maxcode[18]; /* largest code of length k (-1 if none) */29/* (maxcode[17] is a sentinel to ensure jpeg_huff_decode terminates) */30JLONG valoffset[18]; /* huffval[] offset for codes of length k */31/* valoffset[k] = huffval[] index of 1st symbol of code length k, less32* the smallest code of length k; so given a code of length k, the33* corresponding symbol is huffval[code + valoffset[k]]34*/3536/* Link to public Huffman table (needed only in jpeg_huff_decode) */37JHUFF_TBL *pub;3839/* Lookahead table: indexed by the next HUFF_LOOKAHEAD bits of40* the input data stream. If the next Huffman code is no more41* than HUFF_LOOKAHEAD bits long, we can obtain its length and42* the corresponding symbol directly from this tables.43*44* The lower 8 bits of each table entry contain the number of45* bits in the corresponding Huffman code, or HUFF_LOOKAHEAD + 146* if too long. The next 8 bits of each entry contain the47* symbol.48*/49int lookup[1 << HUFF_LOOKAHEAD];50} d_derived_tbl;5152/* Expand a Huffman table definition into the derived format */53EXTERN(void) jpeg_make_d_derived_tbl(j_decompress_ptr cinfo, boolean isDC,54int tblno, d_derived_tbl **pdtbl);555657/*58* Fetching the next N bits from the input stream is a time-critical operation59* for the Huffman decoders. We implement it with a combination of inline60* macros and out-of-line subroutines. Note that N (the number of bits61* demanded at one time) never exceeds 15 for JPEG use.62*63* We read source bytes into get_buffer and dole out bits as needed.64* If get_buffer already contains enough bits, they are fetched in-line65* by the macros CHECK_BIT_BUFFER and GET_BITS. When there aren't enough66* bits, jpeg_fill_bit_buffer is called; it will attempt to fill get_buffer67* as full as possible (not just to the number of bits needed; this68* prefetching reduces the overhead cost of calling jpeg_fill_bit_buffer).69* Note that jpeg_fill_bit_buffer may return FALSE to indicate suspension.70* On TRUE return, jpeg_fill_bit_buffer guarantees that get_buffer contains71* at least the requested number of bits --- dummy zeroes are inserted if72* necessary.73*/7475#if !defined(_WIN32) && !defined(SIZEOF_SIZE_T)76#error Cannot determine word size77#endif7879#if SIZEOF_SIZE_T == 8 || defined(_WIN64)8081typedef size_t bit_buf_type; /* type of bit-extraction buffer */82#define BIT_BUF_SIZE 64 /* size of buffer in bits */8384#elif defined(__x86_64__) && defined(__ILP32__)8586typedef unsigned long long bit_buf_type; /* type of bit-extraction buffer */87#define BIT_BUF_SIZE 64 /* size of buffer in bits */8889#else9091typedef unsigned long bit_buf_type; /* type of bit-extraction buffer */92#define BIT_BUF_SIZE 32 /* size of buffer in bits */9394#endif9596/* If long is > 32 bits on your machine, and shifting/masking longs is97* reasonably fast, making bit_buf_type be long and setting BIT_BUF_SIZE98* appropriately should be a win. Unfortunately we can't define the size99* with something like #define BIT_BUF_SIZE (sizeof(bit_buf_type)*8)100* because not all machines measure sizeof in 8-bit bytes.101*/102103typedef struct { /* Bitreading state saved across MCUs */104bit_buf_type get_buffer; /* current bit-extraction buffer */105int bits_left; /* # of unused bits in it */106} bitread_perm_state;107108typedef struct { /* Bitreading working state within an MCU */109/* Current data source location */110/* We need a copy, rather than munging the original, in case of suspension */111const JOCTET *next_input_byte; /* => next byte to read from source */112size_t bytes_in_buffer; /* # of bytes remaining in source buffer */113/* Bit input buffer --- note these values are kept in register variables,114* not in this struct, inside the inner loops.115*/116bit_buf_type get_buffer; /* current bit-extraction buffer */117int bits_left; /* # of unused bits in it */118/* Pointer needed by jpeg_fill_bit_buffer. */119j_decompress_ptr cinfo; /* back link to decompress master record */120} bitread_working_state;121122/* Macros to declare and load/save bitread local variables. */123#define BITREAD_STATE_VARS \124register bit_buf_type get_buffer; \125register int bits_left; \126bitread_working_state br_state127128#define BITREAD_LOAD_STATE(cinfop, permstate) \129br_state.cinfo = cinfop; \130br_state.next_input_byte = cinfop->src->next_input_byte; \131br_state.bytes_in_buffer = cinfop->src->bytes_in_buffer; \132get_buffer = permstate.get_buffer; \133bits_left = permstate.bits_left;134135#define BITREAD_SAVE_STATE(cinfop, permstate) \136cinfop->src->next_input_byte = br_state.next_input_byte; \137cinfop->src->bytes_in_buffer = br_state.bytes_in_buffer; \138permstate.get_buffer = get_buffer; \139permstate.bits_left = bits_left140141/*142* These macros provide the in-line portion of bit fetching.143* Use CHECK_BIT_BUFFER to ensure there are N bits in get_buffer144* before using GET_BITS, PEEK_BITS, or DROP_BITS.145* The variables get_buffer and bits_left are assumed to be locals,146* but the state struct might not be (jpeg_huff_decode needs this).147* CHECK_BIT_BUFFER(state, n, action);148* Ensure there are N bits in get_buffer; if suspend, take action.149* val = GET_BITS(n);150* Fetch next N bits.151* val = PEEK_BITS(n);152* Fetch next N bits without removing them from the buffer.153* DROP_BITS(n);154* Discard next N bits.155* The value N should be a simple variable, not an expression, because it156* is evaluated multiple times.157*/158159#define CHECK_BIT_BUFFER(state, nbits, action) { \160if (bits_left < (nbits)) { \161if (!jpeg_fill_bit_buffer(&(state), get_buffer, bits_left, nbits)) \162{ action; } \163get_buffer = (state).get_buffer; bits_left = (state).bits_left; \164} \165}166167#define GET_BITS(nbits) \168(((int)(get_buffer >> (bits_left -= (nbits)))) & ((1 << (nbits)) - 1))169170#define PEEK_BITS(nbits) \171(((int)(get_buffer >> (bits_left - (nbits)))) & ((1 << (nbits)) - 1))172173#define DROP_BITS(nbits) \174(bits_left -= (nbits))175176/* Load up the bit buffer to a depth of at least nbits */177EXTERN(boolean) jpeg_fill_bit_buffer(bitread_working_state *state,178register bit_buf_type get_buffer,179register int bits_left, int nbits);180181182/*183* Code for extracting next Huffman-coded symbol from input bit stream.184* Again, this is time-critical and we make the main paths be macros.185*186* We use a lookahead table to process codes of up to HUFF_LOOKAHEAD bits187* without looping. Usually, more than 95% of the Huffman codes will be 8188* or fewer bits long. The few overlength codes are handled with a loop,189* which need not be inline code.190*191* Notes about the HUFF_DECODE macro:192* 1. Near the end of the data segment, we may fail to get enough bits193* for a lookahead. In that case, we do it the hard way.194* 2. If the lookahead table contains no entry, the next code must be195* more than HUFF_LOOKAHEAD bits long.196* 3. jpeg_huff_decode returns -1 if forced to suspend.197*/198199#define HUFF_DECODE(result, state, htbl, failaction, slowlabel) { \200register int nb, look; \201if (bits_left < HUFF_LOOKAHEAD) { \202if (!jpeg_fill_bit_buffer(&state, get_buffer, bits_left, 0)) \203{ failaction; } \204get_buffer = state.get_buffer; bits_left = state.bits_left; \205if (bits_left < HUFF_LOOKAHEAD) { \206nb = 1; goto slowlabel; \207} \208} \209look = PEEK_BITS(HUFF_LOOKAHEAD); \210if ((nb = (htbl->lookup[look] >> HUFF_LOOKAHEAD)) <= HUFF_LOOKAHEAD) { \211DROP_BITS(nb); \212result = htbl->lookup[look] & ((1 << HUFF_LOOKAHEAD) - 1); \213} else { \214slowlabel: \215if ((result = \216jpeg_huff_decode(&state, get_buffer, bits_left, htbl, nb)) < 0) \217{ failaction; } \218get_buffer = state.get_buffer; bits_left = state.bits_left; \219} \220}221222#define HUFF_DECODE_FAST(s, nb, htbl) \223FILL_BIT_BUFFER_FAST; \224s = PEEK_BITS(HUFF_LOOKAHEAD); \225s = htbl->lookup[s]; \226nb = s >> HUFF_LOOKAHEAD; \227/* Pre-execute the common case of nb <= HUFF_LOOKAHEAD */ \228DROP_BITS(nb); \229s = s & ((1 << HUFF_LOOKAHEAD) - 1); \230if (nb > HUFF_LOOKAHEAD) { \231/* Equivalent of jpeg_huff_decode() */ \232/* Don't use GET_BITS() here because we don't want to modify bits_left */ \233s = (get_buffer >> bits_left) & ((1 << (nb)) - 1); \234while (s > htbl->maxcode[nb]) { \235s <<= 1; \236s |= GET_BITS(1); \237nb++; \238} \239if (nb > 16) \240s = 0; \241else \242s = htbl->pub->huffval[(int)(s + htbl->valoffset[nb]) & 0xFF]; \243}244245/* Out-of-line case for Huffman code fetching */246EXTERN(int) jpeg_huff_decode(bitread_working_state *state,247register bit_buf_type get_buffer,248register int bits_left, d_derived_tbl *htbl,249int min_bits);250251252