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