decode_internal.h (4542B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #ifndef LIB_JPEGLI_DECODE_INTERNAL_H_ 7 #define LIB_JPEGLI_DECODE_INTERNAL_H_ 8 9 #include <stdint.h> 10 #include <sys/types.h> 11 12 #include <vector> 13 14 #include "lib/jpegli/common.h" 15 #include "lib/jpegli/common_internal.h" 16 #include "lib/jpegli/huffman.h" 17 18 namespace jpegli { 19 20 static constexpr int kNeedMoreInput = 100; 21 static constexpr int kHandleRestart = 101; 22 static constexpr int kHandleMarkerProcessor = 102; 23 static constexpr int kProcessNextMarker = 103; 24 static constexpr size_t kAllHuffLutSize = NUM_HUFF_TBLS * kJpegHuffmanLutSize; 25 26 typedef int16_t coeff_t; 27 28 // State of the decoder that has to be saved before decoding one MCU in case 29 // we run out of the bitstream. 30 struct MCUCodingState { 31 coeff_t last_dc_coeff[kMaxComponents]; 32 int eobrun; 33 coeff_t coeffs[D_MAX_BLOCKS_IN_MCU * DCTSIZE2]; 34 }; 35 36 } // namespace jpegli 37 38 // Use this forward-declared libjpeg struct to hold all our private variables. 39 // TODO(szabadka) Remove variables that have a corresponding version in cinfo. 40 struct jpeg_decomp_master { 41 // 42 // Input handling state. 43 // 44 std::vector<uint8_t> input_buffer_; 45 size_t input_buffer_pos_; 46 // Number of bits after codestream_pos_ that were already processed. 47 size_t codestream_bits_ahead_; 48 bool streaming_mode_; 49 50 // Coefficient buffers 51 jvirt_barray_ptr* coef_arrays; 52 JBLOCKARRAY coeff_rows[jpegli::kMaxComponents]; 53 54 // 55 // Marker data processing state. 56 // 57 bool found_soi_; 58 bool found_dri_; 59 bool found_sof_; 60 bool found_eoi_; 61 size_t icc_index_; 62 size_t icc_total_; 63 std::vector<uint8_t> icc_profile_; 64 jpegli::HuffmanTableEntry dc_huff_lut_[jpegli::kAllHuffLutSize]; 65 jpegli::HuffmanTableEntry ac_huff_lut_[jpegli::kAllHuffLutSize]; 66 uint8_t markers_to_save_[32]; 67 jpeg_marker_parser_method app_marker_parsers[16]; 68 jpeg_marker_parser_method com_marker_parser; 69 // Whether this jpeg has multiple scans (progressive or non-interleaved 70 // sequential). 71 bool is_multiscan_; 72 73 // Fields defined by SOF marker. 74 size_t iMCU_cols_; 75 int h_factor[jpegli::kMaxComponents]; 76 int v_factor[jpegli::kMaxComponents]; 77 78 // Initialized at strat of frame. 79 uint16_t scan_progression_[jpegli::kMaxComponents][DCTSIZE2]; 80 81 // 82 // Per scan state. 83 // 84 size_t scan_mcu_row_; 85 size_t scan_mcu_col_; 86 size_t mcu_rows_per_iMCU_row_; 87 jpegli::coeff_t last_dc_coeff_[jpegli::kMaxComponents]; 88 int eobrun_; 89 int restarts_to_go_; 90 int next_restart_marker_; 91 92 jpegli::MCUCodingState mcu_; 93 94 // 95 // Rendering state. 96 // 97 int output_passes_done_; 98 JpegliDataType output_data_type_ = JPEGLI_TYPE_UINT8; 99 bool swap_endianness_ = false; 100 size_t xoffset_; 101 bool need_context_rows_; 102 103 int min_scaled_dct_size; 104 int scaled_dct_size[jpegli::kMaxComponents]; 105 106 size_t raw_height_[jpegli::kMaxComponents]; 107 jpegli::RowBuffer<float> raw_output_[jpegli::kMaxComponents]; 108 jpegli::RowBuffer<float> render_output_[jpegli::kMaxComponents]; 109 110 void (*inverse_transform[jpegli::kMaxComponents])( 111 const int16_t* JXL_RESTRICT qblock, const float* JXL_RESTRICT dequant, 112 const float* JXL_RESTRICT biases, float* JXL_RESTRICT scratch_space, 113 float* JXL_RESTRICT output, size_t output_stride, size_t dctsize); 114 115 void (*color_transform)(float* row[jpegli::kMaxComponents], size_t len); 116 117 float* idct_scratch_; 118 float* upsample_scratch_; 119 uint8_t* output_scratch_; 120 int16_t* smoothing_scratch_; 121 float* dequant_; 122 // 1 = 1pass, 2 = 2pass, 3 = external 123 int quant_mode_; 124 int quant_pass_; 125 int num_colors_[jpegli::kMaxComponents]; 126 uint8_t* colormap_lut_; 127 uint8_t* pixels_; 128 JSAMPARRAY scanlines_; 129 std::vector<std::vector<uint8_t>> candidate_lists_; 130 bool regenerate_inverse_colormap_; 131 float* dither_[jpegli::kMaxComponents]; 132 float* error_row_[2 * jpegli::kMaxComponents]; 133 size_t dither_size_; 134 size_t dither_mask_; 135 136 // Per channel and per frequency statistics about the number of nonzeros and 137 // the sum of coefficient absolute values, used in dequantization bias 138 // computation. 139 int* nonzeros_; 140 int* sumabs_; 141 size_t num_processed_blocks_[jpegli::kMaxComponents]; 142 float* biases_; 143 #define SAVED_COEFS 10 144 // This holds the coef_bits of the scan before the current scan, 145 // i.e. the bottom half when rendering incomplete scans. 146 int (*coef_bits_latch)[SAVED_COEFS]; 147 int (*prev_coef_bits_latch)[SAVED_COEFS]; 148 bool apply_smoothing; 149 }; 150 151 #endif // LIB_JPEGLI_DECODE_INTERNAL_H_