1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
6 #ifndef LIB_JPEGLI_DECODE_INTERNAL_H_
7 #define LIB_JPEGLI_DECODE_INTERNAL_H_
10 #include <sys/types.h>
14 #include "lib/jpegli/common.h"
15 #include "lib/jpegli/common_internal.h"
16 #include "lib/jpegli/huffman.h"
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
;
26 typedef int16_t coeff_t
;
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
];
33 coeff_t coeffs
[D_MAX_BLOCKS_IN_MCU
* DCTSIZE2
];
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
{
42 // Input handling state.
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_
;
50 // Coefficient buffers
51 jvirt_barray_ptr
* coef_arrays
;
52 JBLOCKARRAY coeff_rows
[jpegli::kMaxComponents
];
55 // Marker data processing state.
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
73 // Fields defined by SOF marker.
75 int h_factor
[jpegli::kMaxComponents
];
76 int v_factor
[jpegli::kMaxComponents
];
78 // Initialized at strat of frame.
79 uint16_t scan_progression_
[jpegli::kMaxComponents
][DCTSIZE2
];
86 size_t mcu_rows_per_iMCU_row_
;
87 jpegli::coeff_t last_dc_coeff_
[jpegli::kMaxComponents
];
90 int next_restart_marker_
;
92 jpegli::MCUCodingState mcu_
;
97 int output_passes_done_
;
98 JpegliDataType output_data_type_
= JPEGLI_TYPE_UINT8
;
99 bool swap_endianness_
= false;
101 bool need_context_rows_
;
103 int min_scaled_dct_size
;
104 int scaled_dct_size
[jpegli::kMaxComponents
];
106 size_t raw_height_
[jpegli::kMaxComponents
];
107 jpegli::RowBuffer
<float> raw_output_
[jpegli::kMaxComponents
];
108 jpegli::RowBuffer
<float> render_output_
[jpegli::kMaxComponents
];
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
);
115 void (*color_transform
)(float* row
[jpegli::kMaxComponents
], size_t len
);
117 float* idct_scratch_
;
118 float* upsample_scratch_
;
119 uint8_t* output_scratch_
;
120 int16_t* smoothing_scratch_
;
122 // 1 = 1pass, 2 = 2pass, 3 = external
125 int num_colors_
[jpegli::kMaxComponents
];
126 uint8_t* colormap_lut_
;
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
];
136 // Per channel and per frequency statistics about the number of nonzeros and
137 // the sum of coefficient absolute values, used in dequantization bias
141 size_t num_processed_blocks_
[jpegli::kMaxComponents
];
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
;
151 #endif // LIB_JPEGLI_DECODE_INTERNAL_H_