hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jdhuff.c |
| 3 | * |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 4 | * This file was part of the Independent JPEG Group's software: |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 5 | * Copyright (C) 1991-1997, Thomas G. Lane. |
noel@chromium.org | 3395bcc | 2014-04-14 06:56:00 +0000 | [diff] [blame] | 6 | * libjpeg-turbo Modifications: |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 7 | * Copyright (C) 2009-2011, 2016, 2018-2019, D. R. Commander. |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 8 | * Copyright (C) 2018, Matthias Räncker. |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 9 | * For conditions of distribution and use, see the accompanying README.ijg |
| 10 | * file. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 11 | * |
| 12 | * This file contains Huffman entropy decoding routines. |
| 13 | * |
| 14 | * Much of the complexity here has to do with supporting input suspension. |
| 15 | * If the data source module demands suspension, we want to be able to back |
| 16 | * up to the start of the current MCU. To do this, we copy state variables |
| 17 | * into local working storage, and update them back to the permanent |
| 18 | * storage only upon successful completion of an MCU. |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 19 | * |
| 20 | * NOTE: All referenced figures are from |
| 21 | * Recommendation ITU-T T.81 (1992) | ISO/IEC 10918-1:1994. |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 24 | #define JPEG_INTERNALS |
| 25 | #include "jinclude.h" |
| 26 | #include "jpeglib.h" |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 27 | #include "jdhuff.h" /* Declarations shared with jdphuff.c */ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 28 | #include "jpegcomp.h" |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 29 | #include "jstdhuff.c" |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | /* |
| 33 | * Expanded entropy decoder object for Huffman decoding. |
| 34 | * |
| 35 | * The savable_state subrecord contains fields that change within an MCU, |
| 36 | * but must not be updated permanently until we complete the MCU. |
| 37 | */ |
| 38 | |
| 39 | typedef struct { |
| 40 | int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */ |
| 41 | } savable_state; |
| 42 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 43 | typedef struct { |
| 44 | struct jpeg_entropy_decoder pub; /* public fields */ |
| 45 | |
| 46 | /* These fields are loaded into local variables at start of each MCU. |
| 47 | * In case of suspension, we exit WITHOUT updating them. |
| 48 | */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 49 | bitread_perm_state bitstate; /* Bit buffer at start of MCU */ |
| 50 | savable_state saved; /* Other state at start of MCU */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 51 | |
| 52 | /* These fields are NOT loaded into local working state. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 53 | unsigned int restarts_to_go; /* MCUs left in this restart interval */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 54 | |
| 55 | /* Pointers to derived tables (these workspaces have image lifespan) */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 56 | d_derived_tbl *dc_derived_tbls[NUM_HUFF_TBLS]; |
| 57 | d_derived_tbl *ac_derived_tbls[NUM_HUFF_TBLS]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 58 | |
| 59 | /* Precalculated info set up by start_pass for use in decode_mcu: */ |
| 60 | |
| 61 | /* Pointers to derived tables to be used for each block within an MCU */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 62 | d_derived_tbl *dc_cur_tbls[D_MAX_BLOCKS_IN_MCU]; |
| 63 | d_derived_tbl *ac_cur_tbls[D_MAX_BLOCKS_IN_MCU]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 64 | /* Whether we care about the DC and AC coefficient values for each block */ |
| 65 | boolean dc_needed[D_MAX_BLOCKS_IN_MCU]; |
| 66 | boolean ac_needed[D_MAX_BLOCKS_IN_MCU]; |
| 67 | } huff_entropy_decoder; |
| 68 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 69 | typedef huff_entropy_decoder *huff_entropy_ptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | /* |
| 73 | * Initialize for a Huffman-compressed scan. |
| 74 | */ |
| 75 | |
| 76 | METHODDEF(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 77 | start_pass_huff_decoder(j_decompress_ptr cinfo) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 78 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 79 | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 80 | int ci, blkn, dctbl, actbl; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 81 | d_derived_tbl **pdtbl; |
| 82 | jpeg_component_info *compptr; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 83 | |
| 84 | /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG. |
| 85 | * This ought to be an error condition, but we make it a warning because |
| 86 | * there are some baseline files out there with all zeroes in these bytes. |
| 87 | */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 88 | if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2 - 1 || |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 89 | cinfo->Ah != 0 || cinfo->Al != 0) |
| 90 | WARNMS(cinfo, JWRN_NOT_SEQUENTIAL); |
| 91 | |
| 92 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 93 | compptr = cinfo->cur_comp_info[ci]; |
| 94 | dctbl = compptr->dc_tbl_no; |
| 95 | actbl = compptr->ac_tbl_no; |
| 96 | /* Compute derived values for Huffman tables */ |
| 97 | /* We may do this more than once for a table, but it's not expensive */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 98 | pdtbl = (d_derived_tbl **)(entropy->dc_derived_tbls) + dctbl; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 99 | jpeg_make_d_derived_tbl(cinfo, TRUE, dctbl, pdtbl); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 100 | pdtbl = (d_derived_tbl **)(entropy->ac_derived_tbls) + actbl; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 101 | jpeg_make_d_derived_tbl(cinfo, FALSE, actbl, pdtbl); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 102 | /* Initialize DC predictions to 0 */ |
| 103 | entropy->saved.last_dc_val[ci] = 0; |
| 104 | } |
| 105 | |
| 106 | /* Precalculate decoding info for each block in an MCU of this scan */ |
| 107 | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
| 108 | ci = cinfo->MCU_membership[blkn]; |
| 109 | compptr = cinfo->cur_comp_info[ci]; |
| 110 | /* Precalculate which table to use for each block */ |
| 111 | entropy->dc_cur_tbls[blkn] = entropy->dc_derived_tbls[compptr->dc_tbl_no]; |
| 112 | entropy->ac_cur_tbls[blkn] = entropy->ac_derived_tbls[compptr->ac_tbl_no]; |
| 113 | /* Decide whether we really care about the coefficient values */ |
| 114 | if (compptr->component_needed) { |
| 115 | entropy->dc_needed[blkn] = TRUE; |
| 116 | /* we don't need the ACs if producing a 1/8th-size image */ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 117 | entropy->ac_needed[blkn] = (compptr->_DCT_scaled_size > 1); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 118 | } else { |
| 119 | entropy->dc_needed[blkn] = entropy->ac_needed[blkn] = FALSE; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /* Initialize bitread state variables */ |
| 124 | entropy->bitstate.bits_left = 0; |
| 125 | entropy->bitstate.get_buffer = 0; /* unnecessary, but keeps Purify quiet */ |
| 126 | entropy->pub.insufficient_data = FALSE; |
| 127 | |
| 128 | /* Initialize restart counter */ |
| 129 | entropy->restarts_to_go = cinfo->restart_interval; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /* |
| 134 | * Compute the derived values for a Huffman table. |
| 135 | * This routine also performs some validation checks on the table. |
| 136 | * |
| 137 | * Note this is also used by jdphuff.c. |
| 138 | */ |
| 139 | |
| 140 | GLOBAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 141 | jpeg_make_d_derived_tbl(j_decompress_ptr cinfo, boolean isDC, int tblno, |
| 142 | d_derived_tbl **pdtbl) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 143 | { |
| 144 | JHUFF_TBL *htbl; |
| 145 | d_derived_tbl *dtbl; |
| 146 | int p, i, l, si, numsymbols; |
| 147 | int lookbits, ctr; |
| 148 | char huffsize[257]; |
| 149 | unsigned int huffcode[257]; |
| 150 | unsigned int code; |
| 151 | |
| 152 | /* Note that huffsize[] and huffcode[] are filled in code-length order, |
| 153 | * paralleling the order of the symbols themselves in htbl->huffval[]. |
| 154 | */ |
| 155 | |
| 156 | /* Find the input Huffman table */ |
| 157 | if (tblno < 0 || tblno >= NUM_HUFF_TBLS) |
| 158 | ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); |
| 159 | htbl = |
| 160 | isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; |
| 161 | if (htbl == NULL) |
| 162 | ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); |
| 163 | |
| 164 | /* Allocate a workspace if we haven't already done so. */ |
| 165 | if (*pdtbl == NULL) |
| 166 | *pdtbl = (d_derived_tbl *) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 167 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 168 | sizeof(d_derived_tbl)); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 169 | dtbl = *pdtbl; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 170 | dtbl->pub = htbl; /* fill in back link */ |
| 171 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 172 | /* Figure C.1: make table of Huffman code length for each symbol */ |
| 173 | |
| 174 | p = 0; |
| 175 | for (l = 1; l <= 16; l++) { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 176 | i = (int)htbl->bits[l]; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 177 | if (i < 0 || p + i > 256) /* protect against table overrun */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 178 | ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
| 179 | while (i--) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 180 | huffsize[p++] = (char)l; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 181 | } |
| 182 | huffsize[p] = 0; |
| 183 | numsymbols = p; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 184 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 185 | /* Figure C.2: generate the codes themselves */ |
| 186 | /* We also validate that the counts represent a legal Huffman code tree. */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 187 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 188 | code = 0; |
| 189 | si = huffsize[0]; |
| 190 | p = 0; |
| 191 | while (huffsize[p]) { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 192 | while (((int)huffsize[p]) == si) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 193 | huffcode[p++] = code; |
| 194 | code++; |
| 195 | } |
| 196 | /* code is now 1 more than the last code used for codelength si; but |
| 197 | * it must still fit in si bits, since no code is allowed to be all ones. |
| 198 | */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 199 | if (((JLONG)code) >= (((JLONG)1) << si)) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 200 | ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
| 201 | code <<= 1; |
| 202 | si++; |
| 203 | } |
| 204 | |
| 205 | /* Figure F.15: generate decoding tables for bit-sequential decoding */ |
| 206 | |
| 207 | p = 0; |
| 208 | for (l = 1; l <= 16; l++) { |
| 209 | if (htbl->bits[l]) { |
| 210 | /* valoffset[l] = huffval[] index of 1st symbol of code length l, |
| 211 | * minus the minimum code of length l |
| 212 | */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 213 | dtbl->valoffset[l] = (JLONG)p - (JLONG)huffcode[p]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 214 | p += htbl->bits[l]; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 215 | dtbl->maxcode[l] = huffcode[p - 1]; /* maximum code of length l */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 216 | } else { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 217 | dtbl->maxcode[l] = -1; /* -1 if no codes of this length */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | dtbl->valoffset[17] = 0; |
| 221 | dtbl->maxcode[17] = 0xFFFFFL; /* ensures jpeg_huff_decode terminates */ |
| 222 | |
| 223 | /* Compute lookahead tables to speed up decoding. |
| 224 | * First we set all the table entries to 0, indicating "too long"; |
| 225 | * then we iterate through the Huffman codes that are short enough and |
| 226 | * fill in all the entries that correspond to bit sequences starting |
| 227 | * with that code. |
| 228 | */ |
| 229 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 230 | for (i = 0; i < (1 << HUFF_LOOKAHEAD); i++) |
| 231 | dtbl->lookup[i] = (HUFF_LOOKAHEAD + 1) << HUFF_LOOKAHEAD; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 232 | |
| 233 | p = 0; |
| 234 | for (l = 1; l <= HUFF_LOOKAHEAD; l++) { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 235 | for (i = 1; i <= (int)htbl->bits[l]; i++, p++) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 236 | /* l = current code's length, p = its index in huffcode[] & huffval[]. */ |
| 237 | /* Generate left-justified code followed by all possible bit sequences */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 238 | lookbits = huffcode[p] << (HUFF_LOOKAHEAD - l); |
| 239 | for (ctr = 1 << (HUFF_LOOKAHEAD - l); ctr > 0; ctr--) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 240 | dtbl->lookup[lookbits] = (l << HUFF_LOOKAHEAD) | htbl->huffval[p]; |
| 241 | lookbits++; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* Validate symbols as being reasonable. |
| 247 | * For AC tables, we make no check, but accept all byte values 0..255. |
| 248 | * For DC tables, we require the symbols to be in range 0..15. |
| 249 | * (Tighter bounds could be applied depending on the data depth and mode, |
| 250 | * but this is sufficient to ensure safe decoding.) |
| 251 | */ |
| 252 | if (isDC) { |
| 253 | for (i = 0; i < numsymbols; i++) { |
| 254 | int sym = htbl->huffval[i]; |
| 255 | if (sym < 0 || sym > 15) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 256 | ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /* |
| 263 | * Out-of-line code for bit fetching (shared with jdphuff.c). |
| 264 | * See jdhuff.h for info about usage. |
| 265 | * Note: current values of get_buffer and bits_left are passed as parameters, |
| 266 | * but are returned in the corresponding fields of the state struct. |
| 267 | * |
| 268 | * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width |
| 269 | * of get_buffer to be used. (On machines with wider words, an even larger |
| 270 | * buffer could be used.) However, on some machines 32-bit shifts are |
| 271 | * quite slow and take time proportional to the number of places shifted. |
| 272 | * (This is true with most PC compilers, for instance.) In this case it may |
| 273 | * be a win to set MIN_GET_BITS to the minimum value of 15. This reduces the |
| 274 | * average shift distance at the cost of more calls to jpeg_fill_bit_buffer. |
| 275 | */ |
| 276 | |
| 277 | #ifdef SLOW_SHIFT_32 |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 278 | #define MIN_GET_BITS 15 /* minimum allowable value */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 279 | #else |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 280 | #define MIN_GET_BITS (BIT_BUF_SIZE - 7) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 281 | #endif |
| 282 | |
| 283 | |
| 284 | GLOBAL(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 285 | jpeg_fill_bit_buffer(bitread_working_state *state, |
| 286 | register bit_buf_type get_buffer, register int bits_left, |
| 287 | int nbits) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 288 | /* Load up the bit buffer to a depth of at least nbits */ |
| 289 | { |
| 290 | /* Copy heavily used state fields into locals (hopefully registers) */ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 291 | register const JOCTET *next_input_byte = state->next_input_byte; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 292 | register size_t bytes_in_buffer = state->bytes_in_buffer; |
| 293 | j_decompress_ptr cinfo = state->cinfo; |
| 294 | |
| 295 | /* Attempt to load at least MIN_GET_BITS bits into get_buffer. */ |
| 296 | /* (It is assumed that no request will be for more than that many bits.) */ |
| 297 | /* We fail to do so only if we hit a marker or are forced to suspend. */ |
| 298 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 299 | if (cinfo->unread_marker == 0) { /* cannot advance past a marker */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 300 | while (bits_left < MIN_GET_BITS) { |
| 301 | register int c; |
| 302 | |
| 303 | /* Attempt to read a byte */ |
| 304 | if (bytes_in_buffer == 0) { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 305 | if (!(*cinfo->src->fill_input_buffer) (cinfo)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 306 | return FALSE; |
| 307 | next_input_byte = cinfo->src->next_input_byte; |
| 308 | bytes_in_buffer = cinfo->src->bytes_in_buffer; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 309 | } |
| 310 | bytes_in_buffer--; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 311 | c = *next_input_byte++; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 312 | |
| 313 | /* If it's 0xFF, check and discard stuffed zero byte */ |
| 314 | if (c == 0xFF) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 315 | /* Loop here to discard any padding FF's on terminating marker, |
| 316 | * so that we can save a valid unread_marker value. NOTE: we will |
| 317 | * accept multiple FF's followed by a 0 as meaning a single FF data |
| 318 | * byte. This data pattern is not valid according to the standard. |
| 319 | */ |
| 320 | do { |
| 321 | if (bytes_in_buffer == 0) { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 322 | if (!(*cinfo->src->fill_input_buffer) (cinfo)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 323 | return FALSE; |
| 324 | next_input_byte = cinfo->src->next_input_byte; |
| 325 | bytes_in_buffer = cinfo->src->bytes_in_buffer; |
| 326 | } |
| 327 | bytes_in_buffer--; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 328 | c = *next_input_byte++; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 329 | } while (c == 0xFF); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 330 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 331 | if (c == 0) { |
| 332 | /* Found FF/00, which represents an FF data byte */ |
| 333 | c = 0xFF; |
| 334 | } else { |
| 335 | /* Oops, it's actually a marker indicating end of compressed data. |
| 336 | * Save the marker code for later use. |
| 337 | * Fine point: it might appear that we should save the marker into |
| 338 | * bitread working state, not straight into permanent state. But |
| 339 | * once we have hit a marker, we cannot need to suspend within the |
| 340 | * current MCU, because we will read no more bytes from the data |
| 341 | * source. So it is OK to update permanent state right away. |
| 342 | */ |
| 343 | cinfo->unread_marker = c; |
| 344 | /* See if we need to insert some fake zero bits. */ |
| 345 | goto no_more_bytes; |
| 346 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* OK, load c into get_buffer */ |
| 350 | get_buffer = (get_buffer << 8) | c; |
| 351 | bits_left += 8; |
| 352 | } /* end while */ |
| 353 | } else { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 354 | no_more_bytes: |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 355 | /* We get here if we've read the marker that terminates the compressed |
| 356 | * data segment. There should be enough bits in the buffer register |
| 357 | * to satisfy the request; if so, no problem. |
| 358 | */ |
| 359 | if (nbits > bits_left) { |
| 360 | /* Uh-oh. Report corrupted data to user and stuff zeroes into |
| 361 | * the data stream, so that we can produce some kind of image. |
| 362 | * We use a nonvolatile flag to ensure that only one warning message |
| 363 | * appears per data segment. |
| 364 | */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 365 | if (!cinfo->entropy->insufficient_data) { |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 366 | WARNMS(cinfo, JWRN_HIT_MARKER); |
| 367 | cinfo->entropy->insufficient_data = TRUE; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 368 | } |
| 369 | /* Fill the buffer with zero bits */ |
| 370 | get_buffer <<= MIN_GET_BITS - bits_left; |
| 371 | bits_left = MIN_GET_BITS; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | /* Unload the local registers */ |
| 376 | state->next_input_byte = next_input_byte; |
| 377 | state->bytes_in_buffer = bytes_in_buffer; |
| 378 | state->get_buffer = get_buffer; |
| 379 | state->bits_left = bits_left; |
| 380 | |
| 381 | return TRUE; |
| 382 | } |
| 383 | |
| 384 | |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 385 | /* Macro version of the above, which performs much better but does not |
| 386 | handle markers. We have to hand off any blocks with markers to the |
| 387 | slower routines. */ |
| 388 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 389 | #define GET_BYTE { \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 390 | register int c0, c1; \ |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 391 | c0 = *buffer++; \ |
| 392 | c1 = *buffer; \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 393 | /* Pre-execute most common case */ \ |
| 394 | get_buffer = (get_buffer << 8) | c0; \ |
| 395 | bits_left += 8; \ |
| 396 | if (c0 == 0xFF) { \ |
| 397 | /* Pre-execute case of FF/00, which represents an FF data byte */ \ |
| 398 | buffer++; \ |
| 399 | if (c1 != 0) { \ |
| 400 | /* Oops, it's actually a marker indicating end of compressed data. */ \ |
| 401 | cinfo->unread_marker = c1; \ |
| 402 | /* Back out pre-execution and fill the buffer with zero bits */ \ |
| 403 | buffer -= 2; \ |
| 404 | get_buffer &= ~0xFF; \ |
| 405 | } \ |
| 406 | } \ |
| 407 | } |
| 408 | |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 409 | #if SIZEOF_SIZE_T == 8 || defined(_WIN64) || (defined(__x86_64__) && defined(__ILP32__)) |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 410 | |
| 411 | /* Pre-fetch 48 bytes, because the holding register is 64-bit */ |
| 412 | #define FILL_BIT_BUFFER_FAST \ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 413 | if (bits_left <= 16) { \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 414 | GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE GET_BYTE \ |
| 415 | } |
| 416 | |
| 417 | #else |
| 418 | |
| 419 | /* Pre-fetch 16 bytes, because the holding register is 32-bit */ |
| 420 | #define FILL_BIT_BUFFER_FAST \ |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 421 | if (bits_left <= 16) { \ |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 422 | GET_BYTE GET_BYTE \ |
| 423 | } |
| 424 | |
| 425 | #endif |
| 426 | |
| 427 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 428 | /* |
| 429 | * Out-of-line code for Huffman code decoding. |
| 430 | * See jdhuff.h for info about usage. |
| 431 | */ |
| 432 | |
| 433 | GLOBAL(int) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 434 | jpeg_huff_decode(bitread_working_state *state, |
| 435 | register bit_buf_type get_buffer, register int bits_left, |
| 436 | d_derived_tbl *htbl, int min_bits) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 437 | { |
| 438 | register int l = min_bits; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 439 | register JLONG code; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 440 | |
| 441 | /* HUFF_DECODE has determined that the code is at least min_bits */ |
| 442 | /* bits long, so fetch that many bits in one swoop. */ |
| 443 | |
| 444 | CHECK_BIT_BUFFER(*state, l, return -1); |
| 445 | code = GET_BITS(l); |
| 446 | |
| 447 | /* Collect the rest of the Huffman code one bit at a time. */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 448 | /* This is per Figure F.16. */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 449 | |
| 450 | while (code > htbl->maxcode[l]) { |
| 451 | code <<= 1; |
| 452 | CHECK_BIT_BUFFER(*state, 1, return -1); |
| 453 | code |= GET_BITS(1); |
| 454 | l++; |
| 455 | } |
| 456 | |
| 457 | /* Unload the local registers */ |
| 458 | state->get_buffer = get_buffer; |
| 459 | state->bits_left = bits_left; |
| 460 | |
| 461 | /* With garbage input we may reach the sentinel value l = 17. */ |
| 462 | |
| 463 | if (l > 16) { |
| 464 | WARNMS(state->cinfo, JWRN_HUFF_BAD_CODE); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 465 | return 0; /* fake a zero as the safest result */ |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 468 | return htbl->pub->huffval[(int)(code + htbl->valoffset[l])]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | |
| 472 | /* |
| 473 | * Figure F.12: extend sign bit. |
| 474 | * On some machines, a shift and add will be faster than a table lookup. |
| 475 | */ |
| 476 | |
| 477 | #define AVOID_TABLES |
| 478 | #ifdef AVOID_TABLES |
| 479 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 480 | #define NEG_1 ((unsigned int)-1) |
| 481 | #define HUFF_EXTEND(x, s) \ |
| 482 | ((x) + ((((x) - (1 << ((s) - 1))) >> 31) & (((NEG_1) << (s)) + 1))) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 483 | |
| 484 | #else |
| 485 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 486 | #define HUFF_EXTEND(x, s) \ |
| 487 | ((x) < extend_test[s] ? (x) + extend_offset[s] : (x)) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 488 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 489 | static const int extend_test[16] = { /* entry n is 2**(n-1) */ |
| 490 | 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, |
| 491 | 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 |
| 492 | }; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 493 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 494 | static const int extend_offset[16] = { /* entry n is (-1 << n) + 1 */ |
| 495 | 0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1, ((-1) << 4) + 1, |
| 496 | ((-1) << 5) + 1, ((-1) << 6) + 1, ((-1) << 7) + 1, ((-1) << 8) + 1, |
| 497 | ((-1) << 9) + 1, ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1, |
| 498 | ((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1 |
| 499 | }; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 500 | |
| 501 | #endif /* AVOID_TABLES */ |
| 502 | |
| 503 | |
| 504 | /* |
| 505 | * Check for a restart marker & resynchronize decoder. |
| 506 | * Returns FALSE if must suspend. |
| 507 | */ |
| 508 | |
| 509 | LOCAL(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 510 | process_restart(j_decompress_ptr cinfo) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 511 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 512 | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 513 | int ci; |
| 514 | |
| 515 | /* Throw away any unused bits remaining in bit buffer; */ |
| 516 | /* include any full bytes in next_marker's count of discarded bytes */ |
| 517 | cinfo->marker->discarded_bytes += entropy->bitstate.bits_left / 8; |
| 518 | entropy->bitstate.bits_left = 0; |
| 519 | |
| 520 | /* Advance past the RSTn marker */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 521 | if (!(*cinfo->marker->read_restart_marker) (cinfo)) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 522 | return FALSE; |
| 523 | |
| 524 | /* Re-initialize DC predictions to 0 */ |
| 525 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) |
| 526 | entropy->saved.last_dc_val[ci] = 0; |
| 527 | |
| 528 | /* Reset restart counter */ |
| 529 | entropy->restarts_to_go = cinfo->restart_interval; |
| 530 | |
| 531 | /* Reset out-of-data flag, unless read_restart_marker left us smack up |
| 532 | * against a marker. In that case we will end up treating the next data |
| 533 | * segment as empty, and we can avoid producing bogus output pixels by |
| 534 | * leaving the flag set. |
| 535 | */ |
| 536 | if (cinfo->unread_marker == 0) |
| 537 | entropy->pub.insufficient_data = FALSE; |
| 538 | |
| 539 | return TRUE; |
| 540 | } |
| 541 | |
| 542 | |
| 543 | LOCAL(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 544 | decode_mcu_slow(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 545 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 546 | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 547 | BITREAD_STATE_VARS; |
| 548 | int blkn; |
| 549 | savable_state state; |
| 550 | /* Outer loop handles each block in the MCU */ |
| 551 | |
| 552 | /* Load up working state */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 553 | BITREAD_LOAD_STATE(cinfo, entropy->bitstate); |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 554 | state = entropy->saved; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 555 | |
| 556 | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
Aaron Gable | c9c8755 | 2015-08-03 09:34:32 -0700 | [diff] [blame] | 557 | JBLOCKROW block = MCU_data ? MCU_data[blkn] : NULL; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 558 | d_derived_tbl *dctbl = entropy->dc_cur_tbls[blkn]; |
| 559 | d_derived_tbl *actbl = entropy->ac_cur_tbls[blkn]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 560 | register int s, k, r; |
| 561 | |
| 562 | /* Decode a single block's worth of coefficients */ |
| 563 | |
| 564 | /* Section F.2.2.1: decode the DC coefficient difference */ |
| 565 | HUFF_DECODE(s, br_state, dctbl, return FALSE, label1); |
| 566 | if (s) { |
| 567 | CHECK_BIT_BUFFER(br_state, s, return FALSE); |
| 568 | r = GET_BITS(s); |
| 569 | s = HUFF_EXTEND(r, s); |
| 570 | } |
| 571 | |
| 572 | if (entropy->dc_needed[blkn]) { |
| 573 | /* Convert DC difference to actual value, update last_dc_val */ |
| 574 | int ci = cinfo->MCU_membership[blkn]; |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 575 | /* This is really just |
| 576 | * s += state.last_dc_val[ci]; |
| 577 | * It is written this way in order to shut up UBSan. |
| 578 | */ |
| 579 | s = (int)((unsigned int)s + (unsigned int)state.last_dc_val[ci]); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 580 | state.last_dc_val[ci] = s; |
Aaron Gable | c9c8755 | 2015-08-03 09:34:32 -0700 | [diff] [blame] | 581 | if (block) { |
| 582 | /* Output the DC coefficient (assumes jpeg_natural_order[0] = 0) */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 583 | (*block)[0] = (JCOEF)s; |
Aaron Gable | c9c8755 | 2015-08-03 09:34:32 -0700 | [diff] [blame] | 584 | } |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Aaron Gable | c9c8755 | 2015-08-03 09:34:32 -0700 | [diff] [blame] | 587 | if (entropy->ac_needed[blkn] && block) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 588 | |
| 589 | /* Section F.2.2.2: decode the AC coefficients */ |
| 590 | /* Since zeroes are skipped, output area must be cleared beforehand */ |
| 591 | for (k = 1; k < DCTSIZE2; k++) { |
| 592 | HUFF_DECODE(s, br_state, actbl, return FALSE, label2); |
| 593 | |
| 594 | r = s >> 4; |
| 595 | s &= 15; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 596 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 597 | if (s) { |
| 598 | k += r; |
| 599 | CHECK_BIT_BUFFER(br_state, s, return FALSE); |
| 600 | r = GET_BITS(s); |
| 601 | s = HUFF_EXTEND(r, s); |
| 602 | /* Output coefficient in natural (dezigzagged) order. |
| 603 | * Note: the extra entries in jpeg_natural_order[] will save us |
| 604 | * if k >= DCTSIZE2, which could happen if the data is corrupted. |
| 605 | */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 606 | (*block)[jpeg_natural_order[k]] = (JCOEF)s; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 607 | } else { |
| 608 | if (r != 15) |
| 609 | break; |
| 610 | k += 15; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | } else { |
| 615 | |
| 616 | /* Section F.2.2.2: decode the AC coefficients */ |
| 617 | /* In this path we just discard the values */ |
| 618 | for (k = 1; k < DCTSIZE2; k++) { |
| 619 | HUFF_DECODE(s, br_state, actbl, return FALSE, label3); |
| 620 | |
| 621 | r = s >> 4; |
| 622 | s &= 15; |
| 623 | |
| 624 | if (s) { |
| 625 | k += r; |
| 626 | CHECK_BIT_BUFFER(br_state, s, return FALSE); |
| 627 | DROP_BITS(s); |
| 628 | } else { |
| 629 | if (r != 15) |
| 630 | break; |
| 631 | k += 15; |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | /* Completed MCU, so update state */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 638 | BITREAD_SAVE_STATE(cinfo, entropy->bitstate); |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 639 | entropy->saved = state; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 640 | return TRUE; |
| 641 | } |
| 642 | |
| 643 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 644 | LOCAL(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 645 | decode_mcu_fast(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 646 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 647 | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 648 | BITREAD_STATE_VARS; |
| 649 | JOCTET *buffer; |
| 650 | int blkn; |
| 651 | savable_state state; |
| 652 | /* Outer loop handles each block in the MCU */ |
| 653 | |
| 654 | /* Load up working state */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 655 | BITREAD_LOAD_STATE(cinfo, entropy->bitstate); |
| 656 | buffer = (JOCTET *)br_state.next_input_byte; |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 657 | state = entropy->saved; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 658 | |
| 659 | for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) { |
Aaron Gable | 631e2dd | 2015-08-11 13:03:18 -0700 | [diff] [blame] | 660 | JBLOCKROW block = MCU_data ? MCU_data[blkn] : NULL; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 661 | d_derived_tbl *dctbl = entropy->dc_cur_tbls[blkn]; |
| 662 | d_derived_tbl *actbl = entropy->ac_cur_tbls[blkn]; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 663 | register int s, k, r, l; |
| 664 | |
noel@chromium.org | 8ee9bdd | 2015-04-27 11:27:28 +0000 | [diff] [blame] | 665 | HUFF_DECODE_FAST(s, l, dctbl, slow_decode_mcu); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 666 | if (s) { |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 667 | FILL_BIT_BUFFER_FAST |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 668 | r = GET_BITS(s); |
| 669 | s = HUFF_EXTEND(r, s); |
| 670 | } |
| 671 | |
| 672 | if (entropy->dc_needed[blkn]) { |
| 673 | int ci = cinfo->MCU_membership[blkn]; |
Jonathan Wright | db870df | 2020-08-05 11:42:22 +0100 | [diff] [blame] | 674 | s = (int)((unsigned int)s + (unsigned int)state.last_dc_val[ci]); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 675 | state.last_dc_val[ci] = s; |
Aaron Gable | c9c8755 | 2015-08-03 09:34:32 -0700 | [diff] [blame] | 676 | if (block) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 677 | (*block)[0] = (JCOEF)s; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Aaron Gable | c9c8755 | 2015-08-03 09:34:32 -0700 | [diff] [blame] | 680 | if (entropy->ac_needed[blkn] && block) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 681 | |
| 682 | for (k = 1; k < DCTSIZE2; k++) { |
noel@chromium.org | 8ee9bdd | 2015-04-27 11:27:28 +0000 | [diff] [blame] | 683 | HUFF_DECODE_FAST(s, l, actbl, slow_decode_mcu); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 684 | r = s >> 4; |
| 685 | s &= 15; |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 686 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 687 | if (s) { |
| 688 | k += r; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 689 | FILL_BIT_BUFFER_FAST |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 690 | r = GET_BITS(s); |
| 691 | s = HUFF_EXTEND(r, s); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 692 | (*block)[jpeg_natural_order[k]] = (JCOEF)s; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 693 | } else { |
| 694 | if (r != 15) break; |
| 695 | k += 15; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | } else { |
| 700 | |
| 701 | for (k = 1; k < DCTSIZE2; k++) { |
noel@chromium.org | 8ee9bdd | 2015-04-27 11:27:28 +0000 | [diff] [blame] | 702 | HUFF_DECODE_FAST(s, l, actbl, slow_decode_mcu); |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 703 | r = s >> 4; |
| 704 | s &= 15; |
| 705 | |
| 706 | if (s) { |
| 707 | k += r; |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 708 | FILL_BIT_BUFFER_FAST |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 709 | DROP_BITS(s); |
| 710 | } else { |
| 711 | if (r != 15) break; |
| 712 | k += 15; |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 718 | if (cinfo->unread_marker != 0) { |
noel@chromium.org | 8ee9bdd | 2015-04-27 11:27:28 +0000 | [diff] [blame] | 719 | slow_decode_mcu: |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 720 | cinfo->unread_marker = 0; |
| 721 | return FALSE; |
| 722 | } |
| 723 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 724 | br_state.bytes_in_buffer -= (buffer - br_state.next_input_byte); |
| 725 | br_state.next_input_byte = buffer; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 726 | BITREAD_SAVE_STATE(cinfo, entropy->bitstate); |
Jonathan Wright | bbb8282 | 2020-11-25 13:36:43 +0000 | [diff] [blame] | 727 | entropy->saved = state; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 728 | return TRUE; |
| 729 | } |
| 730 | |
| 731 | |
| 732 | /* |
| 733 | * Decode and return one MCU's worth of Huffman-compressed coefficients. |
| 734 | * The coefficients are reordered from zigzag order into natural array order, |
| 735 | * but are not dequantized. |
| 736 | * |
| 737 | * The i'th block of the MCU is stored into the block pointed to by |
| 738 | * MCU_data[i]. WE ASSUME THIS AREA HAS BEEN ZEROED BY THE CALLER. |
| 739 | * (Wholesale zeroing is usually a little faster than retail...) |
| 740 | * |
| 741 | * Returns FALSE if data source requested suspension. In that case no |
| 742 | * changes have been made to permanent state. (Exception: some output |
| 743 | * coefficients may already have been assigned. This is harmless for |
| 744 | * this module, since we'll just re-assign them on the next call.) |
| 745 | */ |
| 746 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 747 | #define BUFSIZE (DCTSIZE2 * 8) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 748 | |
| 749 | METHODDEF(boolean) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 750 | decode_mcu(j_decompress_ptr cinfo, JBLOCKROW *MCU_data) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 751 | { |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 752 | huff_entropy_ptr entropy = (huff_entropy_ptr)cinfo->entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 753 | int usefast = 1; |
| 754 | |
| 755 | /* Process restart marker if needed; may have to suspend */ |
| 756 | if (cinfo->restart_interval) { |
| 757 | if (entropy->restarts_to_go == 0) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 758 | if (!process_restart(cinfo)) |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 759 | return FALSE; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 760 | usefast = 0; |
| 761 | } |
| 762 | |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 763 | if (cinfo->src->bytes_in_buffer < BUFSIZE * (size_t)cinfo->blocks_in_MCU || |
| 764 | cinfo->unread_marker != 0) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 765 | usefast = 0; |
| 766 | |
| 767 | /* If we've run out of data, just leave the MCU set to zeroes. |
| 768 | * This way, we return uniform gray for the remainder of the segment. |
| 769 | */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 770 | if (!entropy->pub.insufficient_data) { |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 771 | |
| 772 | if (usefast) { |
hbono@chromium.org | 9862697 | 2011-08-03 03:13:08 +0000 | [diff] [blame] | 773 | if (!decode_mcu_fast(cinfo, MCU_data)) goto use_slow; |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 774 | } else { |
| 775 | use_slow: |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 776 | if (!decode_mcu_slow(cinfo, MCU_data)) return FALSE; |
| 777 | } |
| 778 | |
| 779 | } |
| 780 | |
| 781 | /* Account for restart interval (no-op if not using restarts) */ |
| 782 | entropy->restarts_to_go--; |
| 783 | |
| 784 | return TRUE; |
| 785 | } |
| 786 | |
| 787 | |
| 788 | /* |
| 789 | * Module initialization routine for Huffman entropy decoding. |
| 790 | */ |
| 791 | |
| 792 | GLOBAL(void) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 793 | jinit_huff_decoder(j_decompress_ptr cinfo) |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 794 | { |
| 795 | huff_entropy_ptr entropy; |
| 796 | int i; |
| 797 | |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 798 | /* Motion JPEG frames typically do not include the Huffman tables if they |
| 799 | are the default tables. Thus, if the tables are not set by the time |
| 800 | the Huffman decoder is initialized (usually within the body of |
| 801 | jpeg_start_decompress()), we set them to default values. */ |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 802 | std_huff_tables((j_common_ptr)cinfo); |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 803 | |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 804 | entropy = (huff_entropy_ptr) |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 805 | (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, |
Tom Hudson | 0d47d2d | 2016-05-04 13:22:56 -0400 | [diff] [blame] | 806 | sizeof(huff_entropy_decoder)); |
Chris Blume | cca8c4d | 2019-03-01 01:09:50 -0800 | [diff] [blame] | 807 | cinfo->entropy = (struct jpeg_entropy_decoder *)entropy; |
hbono@chromium.org | f0c4f33 | 2010-11-01 05:14:55 +0000 | [diff] [blame] | 808 | entropy->pub.start_pass = start_pass_huff_decoder; |
| 809 | entropy->pub.decode_mcu = decode_mcu; |
| 810 | |
| 811 | /* Mark tables unallocated */ |
| 812 | for (i = 0; i < NUM_HUFF_TBLS; i++) { |
| 813 | entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; |
| 814 | } |
| 815 | } |