/src/brunsli/c/dec/state_internal.h
Line | Count | Source |
1 | | // Copyright (c) Google LLC 2020 |
2 | | // |
3 | | // Use of this source code is governed by an MIT-style |
4 | | // license that can be found in the LICENSE file or at |
5 | | // https://opensource.org/licenses/MIT. |
6 | | |
7 | | #ifndef BRUNSLI_DEC_STATE_INTERNAL_H_ |
8 | | #define BRUNSLI_DEC_STATE_INTERNAL_H_ |
9 | | |
10 | | #include <array> |
11 | | #include <memory> |
12 | | #include <string> |
13 | | #include <vector> |
14 | | |
15 | | #include "../common/context.h" |
16 | | #include "../common/lehmer_code.h" |
17 | | #include <brunsli/status.h> |
18 | | #include <brunsli/types.h> |
19 | | #include "./ans_decode.h" |
20 | | #include "./arith_decode.h" |
21 | | #include "./bit_reader.h" |
22 | | #include "./brunsli_input.h" |
23 | | #include "./huffman_decode.h" |
24 | | #include <brunsli/jpeg_data_writer.h> |
25 | | #include "./serialization_state.h" |
26 | | #include "./state.h" |
27 | | |
28 | | struct BrotliDecoderStateStruct; |
29 | | |
30 | | namespace brunsli { |
31 | | |
32 | | struct HuffmanDecodingData; |
33 | | |
34 | | namespace internal { |
35 | | namespace dec { |
36 | | |
37 | | struct AcDcState { |
38 | | int next_mcu_y = 0; |
39 | | size_t next_component = 0; |
40 | | int next_iy = 0; |
41 | | int next_x = 0; |
42 | | bool ac_coeffs_order_decoded = false; |
43 | | |
44 | | std::vector<ComponentState> ac; |
45 | | std::vector<ComponentStateDC> dc; |
46 | | }; |
47 | | |
48 | | // Aid for section / subsection parsing. |
49 | | struct SectionState { |
50 | | // Current value tag and type. |
51 | | size_t tag = 0; |
52 | | // True, if section is entered. |
53 | | bool is_active = false; |
54 | | // True, if "message" is actually "section", not a primitive value. |
55 | | bool is_section = false; |
56 | | |
57 | | // Encountered tags tracker. |
58 | | uint32_t tags_met = 0; |
59 | | |
60 | | // Remaining section length. Actual only when outside of workflow. |
61 | | size_t remaining = 0; |
62 | | |
63 | | // Position in current input, for which |remaining| was actual. |
64 | | size_t milestone = 0; |
65 | | |
66 | | // Projected section end, given enough input is provided. |
67 | | // |projected_end| == |milestone| + |provided| |
68 | | size_t projected_end = 0; |
69 | | }; |
70 | | |
71 | | // Fields used for "Header" section parsing. |
72 | | struct HeaderState { |
73 | | enum Stage { |
74 | | // Check section tag. |
75 | | READ_TAG, |
76 | | // Read section length. |
77 | | ENTER_SECTION, |
78 | | // Read value marker. |
79 | | ITEM_READ_TAG, |
80 | | // Read subsection length. |
81 | | ITEM_ENTER_SECTION, |
82 | | // Skip subsection payload. |
83 | | ITEM_SKIP_CONTENTS, |
84 | | // Read value. |
85 | | ITEM_READ_VALUE, |
86 | | // Verify values and apply to decoder state |
87 | | FINALE, |
88 | | // Finish section decoding. |
89 | | DONE |
90 | | }; |
91 | | |
92 | | size_t stage = READ_TAG; |
93 | | |
94 | | // Subsection properties. |
95 | | SectionState section; |
96 | | // Length of subsection remaining to skip. |
97 | | size_t remaining_skip_length = 0; |
98 | | |
99 | | // Collected data (values). |
100 | | std::array<size_t, 16> varint_values; |
101 | | }; |
102 | | |
103 | | // Fields used for "Fallback" section parsing. |
104 | | struct FallbackState { |
105 | | enum Stage { |
106 | | // Check section tag. |
107 | | READ_TAG, |
108 | | // Read section length. |
109 | | ENTER_SECTION, |
110 | | // Copy "original JPEG" contents to internal storage (if necessary). |
111 | | READ_CONTENTS, |
112 | | // Finish section decoding. |
113 | | DONE |
114 | | }; |
115 | | |
116 | | size_t stage = READ_TAG; |
117 | | |
118 | | // Storage for original JPEG contents. |
119 | | std::vector<uint8_t> storage; |
120 | | }; |
121 | | |
122 | | // Fields used for section header parsing. |
123 | | struct SectionHeaderState { |
124 | | enum Stage { |
125 | | // Check section tag. |
126 | | READ_TAG, |
127 | | // Read (unused) value. |
128 | | READ_VALUE, |
129 | | // Read section length. |
130 | | ENTER_SECTION, |
131 | | // Finish section header decoding. |
132 | | DONE |
133 | | }; |
134 | | |
135 | | size_t stage = READ_TAG; |
136 | | }; |
137 | | |
138 | | enum class MetadataDecompressionStage { |
139 | | // Initial state in which it is decided which one of 3 processing variants to |
140 | | // use. |
141 | | INITIAL, |
142 | | // Read the length of uncompressed payload. |
143 | | READ_LENGTH, |
144 | | // Continuing as stream-decompressing/-parsing of Brotli-compressed metadata. |
145 | | DECOMPRESSING, |
146 | | // Parsing is finished, no further processing expected. |
147 | | DONE, |
148 | | }; |
149 | | |
150 | | struct MetadataState { |
151 | | enum Stage { |
152 | | // Parse sequence type. |
153 | | READ_MARKER, |
154 | | // Dump the remaining of metadata to tail sequence. |
155 | | READ_TAIL, |
156 | | // Parse second byte of 2-byte sequence. |
157 | | READ_CODE, |
158 | | // Parse multi-byte sequence length. |
159 | | READ_LENGTH_HI, |
160 | | READ_LENGTH_LO, |
161 | | // Parse multi-byte sequence. |
162 | | READ_MULTIBYTE, |
163 | | }; |
164 | | |
165 | | size_t short_marker_count = 0; |
166 | | uint8_t marker; |
167 | | uint8_t length_hi; |
168 | | size_t remaining_multibyte_length; |
169 | | std::vector<uint8_t>* multibyte_sink; |
170 | | size_t stage = READ_MARKER; |
171 | | |
172 | | BrotliDecoderStateStruct* brotli = nullptr; |
173 | | size_t metadata_size; |
174 | | size_t decompressed_size = 0; |
175 | | BrunsliStatus result = BRUNSLI_DECOMPRESSION_ERROR; |
176 | | MetadataDecompressionStage decompression_stage = |
177 | | MetadataDecompressionStage::INITIAL; |
178 | | |
179 | | ~MetadataState(); |
180 | | |
181 | 48 | bool CanFinish() { return (stage == READ_MARKER) || (stage == READ_TAIL); } |
182 | | }; |
183 | | |
184 | | /** |
185 | | * Fits both DecodeVarint and DecodeLimitedVarint workflows. |
186 | | * |
187 | | * TODO(eustas): we could turn those methods back to stateless, |
188 | | * when "mark / rewind" utilities are added to BrunsliBitReader, and outer |
189 | | * parsing workflow supports input buffering. |
190 | | */ |
191 | | struct VarintState { |
192 | | enum Stage { |
193 | | INIT, |
194 | | READ_CONTINUATION, |
195 | | READ_DATA |
196 | | }; |
197 | | |
198 | | Stage stage = INIT; |
199 | | size_t value; |
200 | | size_t i; |
201 | | }; |
202 | | |
203 | | struct JpegInternalsState { |
204 | | enum Stage { |
205 | | INIT = 0, |
206 | | READ_MARKERS, |
207 | | READ_DRI, |
208 | | |
209 | | DECODE_HUFFMAN_MASK = 0x10, |
210 | | READ_HUFFMAN_LAST, |
211 | | READ_HUFFMAN_SIMPLE, |
212 | | READ_HUFFMAN_MAX_LEN, |
213 | | READ_HUFFMAN_COUNT, |
214 | | READ_HUFFMAN_PERMUTATION, |
215 | | HUFFMAN_UPDATE, |
216 | | |
217 | | PREPARE_READ_SCANS = 0x20, |
218 | | |
219 | | DECODE_SCAN_MASK = 0x40, |
220 | | READ_SCAN_COMMON, |
221 | | READ_SCAN_COMPONENT, |
222 | | READ_SCAN_RESET_POINT_CONTINUATION, |
223 | | READ_SCAN_RESET_POINT_DATA, |
224 | | READ_SCAN_ZERO_RUN_CONTINUATION, |
225 | | READ_SCAN_ZERO_RUN_DATA, |
226 | | |
227 | | READ_NUM_QUANT = 0x80, |
228 | | READ_QUANT, |
229 | | READ_COMP_ID_SCHEME, |
230 | | READ_COMP_ID, |
231 | | READ_NUM_PADDING_BITS, |
232 | | READ_PADDING_BITS, |
233 | | |
234 | | ITERATE_MARKERS, |
235 | | READ_INTERMARKER_LENGTH, |
236 | | READ_INTERMARKER_DATA, |
237 | | |
238 | | DONE |
239 | | }; |
240 | | |
241 | | Stage stage = INIT; |
242 | | |
243 | | bool have_dri = false; |
244 | | size_t num_scans = 0; |
245 | | size_t dht_count = 0; |
246 | | |
247 | | BrunsliBitReader br; |
248 | | size_t is_known_last_huffman_code; |
249 | | size_t terminal_huffman_code_count = 0; |
250 | | bool is_dc_table; |
251 | | size_t total_count; |
252 | | size_t space; |
253 | | size_t max_len; |
254 | | size_t max_count; |
255 | | size_t i; |
256 | | PermutationCoder p; |
257 | | VarintState varint; |
258 | | |
259 | | size_t j; |
260 | | int last_block_idx; |
261 | | int last_num; |
262 | | |
263 | | size_t num_padding_bits; |
264 | | size_t intermarker_length; |
265 | | }; |
266 | | |
267 | | struct QuantDataState { |
268 | | enum Stage { |
269 | | INIT, |
270 | | |
271 | | READ_NUM_QUANT, |
272 | | |
273 | | READ_STOCK, |
274 | | READ_Q_FACTOR, |
275 | | READ_DIFF_IS_ZERO, |
276 | | READ_DIFF_SIGN, |
277 | | READ_DIFF, |
278 | | APPLY_DIFF, |
279 | | UPDATE, |
280 | | |
281 | | READ_QUANT_IDX, |
282 | | |
283 | | FINISH |
284 | | }; |
285 | | |
286 | | Stage stage = INIT; |
287 | | |
288 | | BrunsliBitReader br; |
289 | | size_t i; |
290 | | size_t j; |
291 | | uint8_t data_precision; |
292 | | VarintState vs; |
293 | | int delta; |
294 | | int sign; |
295 | | std::vector<uint8_t> predictor; |
296 | | }; |
297 | | |
298 | | struct HistogramDataState { |
299 | | enum Stage { |
300 | | INIT, |
301 | | |
302 | | READ_SCHEME, |
303 | | READ_NUM_HISTOGRAMS, |
304 | | READ_CONTEXT_MAP_CODE, |
305 | | READ_CONTEXT_MAP, |
306 | | READ_HISTOGRAMS, |
307 | | |
308 | | SKIP_CONTENT, |
309 | | |
310 | | DONE |
311 | | }; |
312 | | |
313 | | Stage stage = INIT; |
314 | | |
315 | | BrunsliBitReader br; |
316 | | size_t max_run_length_prefix; |
317 | | std::unique_ptr<HuffmanDecodingData> entropy; |
318 | | size_t i; |
319 | | std::vector<uint32_t> counts; |
320 | | Arena<HuffmanCode> arena; |
321 | | }; |
322 | | |
323 | | struct Buffer { |
324 | | size_t data_len = 0; |
325 | | size_t borrowed_len; |
326 | | std::vector<uint8_t> data; |
327 | | |
328 | | const uint8_t* external_data; |
329 | | size_t external_pos; |
330 | | size_t external_len; |
331 | | }; |
332 | | |
333 | | struct InternalState { |
334 | | /* Parsing */ |
335 | | |
336 | | AcDcState ac_dc; |
337 | | SectionState section; |
338 | | |
339 | | // Sections. |
340 | | HeaderState header; |
341 | | FallbackState fallback; |
342 | | SectionHeaderState section_header; |
343 | | MetadataState metadata; |
344 | | JpegInternalsState internals; |
345 | | QuantDataState quant; |
346 | | HistogramDataState histogram; |
347 | | |
348 | | // "JPEGDecodingState" storage. |
349 | | std::vector<uint8_t> context_map_; |
350 | | std::vector<ANSDecodingData> entropy_codes_; |
351 | | std::vector<std::vector<uint8_t>> block_state_; |
352 | | |
353 | | bool is_meta_warm = false; |
354 | | |
355 | | // For "estimate peak memory". |
356 | | bool shallow_histograms = false; |
357 | | size_t num_contexts = 0; |
358 | | size_t num_histograms = 0; |
359 | | |
360 | | // Sub-decoders. |
361 | | bool subdecoders_initialized = false; |
362 | | ANSDecoder ans_decoder; |
363 | | BitSource bit_reader; |
364 | | BinaryArithmeticDecoder arith_decoder; |
365 | | |
366 | | BrunsliStatus result = BRUNSLI_OK; |
367 | | |
368 | | Stage last_stage = Stage::ERROR; |
369 | | |
370 | | Buffer buffer; |
371 | | |
372 | | /* Serialization */ |
373 | | |
374 | | SerializationState serialization; |
375 | | }; |
376 | | |
377 | | } // namespace dec |
378 | | } // namespace internal |
379 | | } // namespace brunsli |
380 | | |
381 | | #endif // BRUNSLI_DEC_STATE_INTERNAL_H_ |