/src/libjxl/lib/jxl/dec_group.cc
Line | Count | Source |
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 | | #include "lib/jxl/dec_group.h" |
7 | | |
8 | | #include <jxl/memory_manager.h> |
9 | | |
10 | | #include <algorithm> |
11 | | #include <array> |
12 | | #include <cstdint> |
13 | | #include <cstdio> |
14 | | #include <cstdlib> |
15 | | #include <cstring> |
16 | | #include <memory> |
17 | | #include <utility> |
18 | | #include <vector> |
19 | | |
20 | | #include "lib/jxl/base/compiler_specific.h" |
21 | | #include "lib/jxl/chroma_from_luma.h" |
22 | | #include "lib/jxl/coeff_order_fwd.h" |
23 | | #include "lib/jxl/dct_util.h" |
24 | | #include "lib/jxl/dec_ans.h" |
25 | | #include "lib/jxl/frame_dimensions.h" |
26 | | #include "lib/jxl/frame_header.h" |
27 | | #include "lib/jxl/image.h" |
28 | | #include "lib/jxl/image_ops.h" |
29 | | #include "lib/jxl/jpeg/jpeg_data.h" |
30 | | #include "lib/jxl/render_pipeline/render_pipeline.h" |
31 | | #include "lib/jxl/render_pipeline/render_pipeline_stage.h" |
32 | | |
33 | | #undef HWY_TARGET_INCLUDE |
34 | | #define HWY_TARGET_INCLUDE "lib/jxl/dec_group.cc" |
35 | | #include <hwy/foreach_target.h> |
36 | | #include <hwy/highway.h> |
37 | | |
38 | | #include "lib/jxl/ac_context.h" |
39 | | #include "lib/jxl/ac_strategy.h" |
40 | | #include "lib/jxl/base/bits.h" |
41 | | #include "lib/jxl/base/common.h" |
42 | | #include "lib/jxl/base/printf_macros.h" |
43 | | #include "lib/jxl/base/rect.h" |
44 | | #include "lib/jxl/base/status.h" |
45 | | #include "lib/jxl/coeff_order.h" |
46 | | #include "lib/jxl/common.h" // kMaxNumPasses |
47 | | #include "lib/jxl/dec_cache.h" |
48 | | #include "lib/jxl/dec_transforms-inl.h" |
49 | | #include "lib/jxl/dec_xyb.h" |
50 | | #include "lib/jxl/entropy_coder.h" |
51 | | #include "lib/jxl/quant_weights.h" |
52 | | #include "lib/jxl/quantizer-inl.h" |
53 | | #include "lib/jxl/quantizer.h" |
54 | | |
55 | | #ifndef LIB_JXL_DEC_GROUP_CC |
56 | | #define LIB_JXL_DEC_GROUP_CC |
57 | | namespace jxl { |
58 | | |
59 | | struct AuxOut; |
60 | | |
61 | | // Interface for reading groups for DecodeGroupImpl. |
62 | | class GetBlock { |
63 | | public: |
64 | | virtual void StartRow(size_t by) = 0; |
65 | | virtual Status LoadBlock(size_t bx, size_t by, const AcStrategy& acs, |
66 | | size_t size, size_t log2_covered_blocks, |
67 | | ACPtr block[3], ACType ac_type) = 0; |
68 | 5.10k | virtual ~GetBlock() {} |
69 | | }; |
70 | | |
71 | | // Controls whether DecodeGroupImpl renders to pixels or not. |
72 | | enum DrawMode { |
73 | | // Render to pixels. |
74 | | kDraw = 0, |
75 | | // Don't render to pixels. |
76 | | kDontDraw = 1, |
77 | | }; |
78 | | |
79 | | } // namespace jxl |
80 | | #endif // LIB_JXL_DEC_GROUP_CC |
81 | | |
82 | | HWY_BEFORE_NAMESPACE(); |
83 | | namespace jxl { |
84 | | namespace HWY_NAMESPACE { |
85 | | |
86 | | // These templates are not found via ADL. |
87 | | using hwy::HWY_NAMESPACE::AllFalse; |
88 | | using hwy::HWY_NAMESPACE::Gt; |
89 | | using hwy::HWY_NAMESPACE::Le; |
90 | | using hwy::HWY_NAMESPACE::MaskFromVec; |
91 | | using hwy::HWY_NAMESPACE::Or; |
92 | | using hwy::HWY_NAMESPACE::Rebind; |
93 | | using hwy::HWY_NAMESPACE::ShiftRight; |
94 | | |
95 | | using D = HWY_FULL(float); |
96 | | using DU = HWY_FULL(uint32_t); |
97 | | using DI = HWY_FULL(int32_t); |
98 | | using DI16 = Rebind<int16_t, DI>; |
99 | | using DI16_FULL = HWY_CAPPED(int16_t, kDCTBlockSize); |
100 | | constexpr D d; |
101 | | constexpr DI di; |
102 | | constexpr DI16 di16; |
103 | | constexpr DI16_FULL di16_full; |
104 | | |
105 | | // TODO(veluca): consider SIMDfying. |
106 | 0 | void Transpose8x8InPlace(int32_t* JXL_RESTRICT block) { |
107 | 0 | for (size_t x = 0; x < 8; x++) { |
108 | 0 | for (size_t y = x + 1; y < 8; y++) { |
109 | 0 | std::swap(block[y * 8 + x], block[x * 8 + y]); |
110 | 0 | } |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | | template <ACType ac_type> |
115 | | void DequantLane(Vec<D> scaled_dequant_x, Vec<D> scaled_dequant_y, |
116 | | Vec<D> scaled_dequant_b, |
117 | | const float* JXL_RESTRICT dequant_matrices, size_t size, |
118 | | size_t k, Vec<D> x_cc_mul, Vec<D> b_cc_mul, |
119 | | const float* JXL_RESTRICT biases, ACPtr qblock[3], |
120 | 3.01M | float* JXL_RESTRICT block) { |
121 | 3.01M | const auto x_mul = Mul(Load(d, dequant_matrices + k), scaled_dequant_x); |
122 | 3.01M | const auto y_mul = |
123 | 3.01M | Mul(Load(d, dequant_matrices + size + k), scaled_dequant_y); |
124 | 3.01M | const auto b_mul = |
125 | 3.01M | Mul(Load(d, dequant_matrices + 2 * size + k), scaled_dequant_b); |
126 | | |
127 | 3.01M | Vec<DI> quantized_x_int; |
128 | 3.01M | Vec<DI> quantized_y_int; |
129 | 3.01M | Vec<DI> quantized_b_int; |
130 | 3.01M | if (ac_type == ACType::k16) { |
131 | 2.27M | quantized_x_int = PromoteTo(di, Load(di16, qblock[0].ptr16 + k)); |
132 | 2.27M | quantized_y_int = PromoteTo(di, Load(di16, qblock[1].ptr16 + k)); |
133 | 2.27M | quantized_b_int = PromoteTo(di, Load(di16, qblock[2].ptr16 + k)); |
134 | 2.27M | } else { |
135 | 737k | quantized_x_int = Load(di, qblock[0].ptr32 + k); |
136 | 737k | quantized_y_int = Load(di, qblock[1].ptr32 + k); |
137 | 737k | quantized_b_int = Load(di, qblock[2].ptr32 + k); |
138 | 737k | } |
139 | | |
140 | 3.01M | const auto dequant_x_cc = |
141 | 3.01M | Mul(AdjustQuantBias(di, 0, quantized_x_int, biases), x_mul); |
142 | 3.01M | const auto dequant_y = |
143 | 3.01M | Mul(AdjustQuantBias(di, 1, quantized_y_int, biases), y_mul); |
144 | 3.01M | const auto dequant_b_cc = |
145 | 3.01M | Mul(AdjustQuantBias(di, 2, quantized_b_int, biases), b_mul); |
146 | | |
147 | 3.01M | const auto dequant_x = MulAdd(x_cc_mul, dequant_y, dequant_x_cc); |
148 | 3.01M | const auto dequant_b = MulAdd(b_cc_mul, dequant_y, dequant_b_cc); |
149 | 3.01M | Store(dequant_x, d, block + k); |
150 | 3.01M | Store(dequant_y, d, block + size + k); |
151 | 3.01M | Store(dequant_b, d, block + 2 * size + k); |
152 | 3.01M | } void jxl::N_SCALAR::DequantLane<(jxl::ACType)0>(hwy::N_SCALAR::Vec1<float>, hwy::N_SCALAR::Vec1<float>, hwy::N_SCALAR::Vec1<float>, float const*, unsigned long, unsigned long, hwy::N_SCALAR::Vec1<float>, hwy::N_SCALAR::Vec1<float>, float const*, jxl::ACPtr*, float*) Line | Count | Source | 120 | 2.27M | float* JXL_RESTRICT block) { | 121 | 2.27M | const auto x_mul = Mul(Load(d, dequant_matrices + k), scaled_dequant_x); | 122 | 2.27M | const auto y_mul = | 123 | 2.27M | Mul(Load(d, dequant_matrices + size + k), scaled_dequant_y); | 124 | 2.27M | const auto b_mul = | 125 | 2.27M | Mul(Load(d, dequant_matrices + 2 * size + k), scaled_dequant_b); | 126 | | | 127 | 2.27M | Vec<DI> quantized_x_int; | 128 | 2.27M | Vec<DI> quantized_y_int; | 129 | 2.27M | Vec<DI> quantized_b_int; | 130 | 2.27M | if (ac_type == ACType::k16) { | 131 | 2.27M | quantized_x_int = PromoteTo(di, Load(di16, qblock[0].ptr16 + k)); | 132 | 2.27M | quantized_y_int = PromoteTo(di, Load(di16, qblock[1].ptr16 + k)); | 133 | 2.27M | quantized_b_int = PromoteTo(di, Load(di16, qblock[2].ptr16 + k)); | 134 | 2.27M | } else { | 135 | 0 | quantized_x_int = Load(di, qblock[0].ptr32 + k); | 136 | 0 | quantized_y_int = Load(di, qblock[1].ptr32 + k); | 137 | 0 | quantized_b_int = Load(di, qblock[2].ptr32 + k); | 138 | 0 | } | 139 | | | 140 | 2.27M | const auto dequant_x_cc = | 141 | 2.27M | Mul(AdjustQuantBias(di, 0, quantized_x_int, biases), x_mul); | 142 | 2.27M | const auto dequant_y = | 143 | 2.27M | Mul(AdjustQuantBias(di, 1, quantized_y_int, biases), y_mul); | 144 | 2.27M | const auto dequant_b_cc = | 145 | 2.27M | Mul(AdjustQuantBias(di, 2, quantized_b_int, biases), b_mul); | 146 | | | 147 | 2.27M | const auto dequant_x = MulAdd(x_cc_mul, dequant_y, dequant_x_cc); | 148 | 2.27M | const auto dequant_b = MulAdd(b_cc_mul, dequant_y, dequant_b_cc); | 149 | 2.27M | Store(dequant_x, d, block + k); | 150 | 2.27M | Store(dequant_y, d, block + size + k); | 151 | 2.27M | Store(dequant_b, d, block + 2 * size + k); | 152 | 2.27M | } |
void jxl::N_SCALAR::DequantLane<(jxl::ACType)1>(hwy::N_SCALAR::Vec1<float>, hwy::N_SCALAR::Vec1<float>, hwy::N_SCALAR::Vec1<float>, float const*, unsigned long, unsigned long, hwy::N_SCALAR::Vec1<float>, hwy::N_SCALAR::Vec1<float>, float const*, jxl::ACPtr*, float*) Line | Count | Source | 120 | 737k | float* JXL_RESTRICT block) { | 121 | 737k | const auto x_mul = Mul(Load(d, dequant_matrices + k), scaled_dequant_x); | 122 | 737k | const auto y_mul = | 123 | 737k | Mul(Load(d, dequant_matrices + size + k), scaled_dequant_y); | 124 | 737k | const auto b_mul = | 125 | 737k | Mul(Load(d, dequant_matrices + 2 * size + k), scaled_dequant_b); | 126 | | | 127 | 737k | Vec<DI> quantized_x_int; | 128 | 737k | Vec<DI> quantized_y_int; | 129 | 737k | Vec<DI> quantized_b_int; | 130 | 737k | if (ac_type == ACType::k16) { | 131 | 0 | quantized_x_int = PromoteTo(di, Load(di16, qblock[0].ptr16 + k)); | 132 | 0 | quantized_y_int = PromoteTo(di, Load(di16, qblock[1].ptr16 + k)); | 133 | 0 | quantized_b_int = PromoteTo(di, Load(di16, qblock[2].ptr16 + k)); | 134 | 737k | } else { | 135 | 737k | quantized_x_int = Load(di, qblock[0].ptr32 + k); | 136 | 737k | quantized_y_int = Load(di, qblock[1].ptr32 + k); | 137 | 737k | quantized_b_int = Load(di, qblock[2].ptr32 + k); | 138 | 737k | } | 139 | | | 140 | 737k | const auto dequant_x_cc = | 141 | 737k | Mul(AdjustQuantBias(di, 0, quantized_x_int, biases), x_mul); | 142 | 737k | const auto dequant_y = | 143 | 737k | Mul(AdjustQuantBias(di, 1, quantized_y_int, biases), y_mul); | 144 | 737k | const auto dequant_b_cc = | 145 | 737k | Mul(AdjustQuantBias(di, 2, quantized_b_int, biases), b_mul); | 146 | | | 147 | 737k | const auto dequant_x = MulAdd(x_cc_mul, dequant_y, dequant_x_cc); | 148 | 737k | const auto dequant_b = MulAdd(b_cc_mul, dequant_y, dequant_b_cc); | 149 | 737k | Store(dequant_x, d, block + k); | 150 | 737k | Store(dequant_y, d, block + size + k); | 151 | 737k | Store(dequant_b, d, block + 2 * size + k); | 152 | 737k | } |
|
153 | | |
154 | | template <ACType ac_type> |
155 | | void DequantBlock(float inv_global_scale, int quant, float x_dm_multiplier, |
156 | | float b_dm_multiplier, Vec<D> x_cc_mul, Vec<D> b_cc_mul, |
157 | | AcStrategyType kind, size_t size, const Quantizer& quantizer, |
158 | | size_t covered_blocks, const size_t* sbx, |
159 | | const float* JXL_RESTRICT* JXL_RESTRICT dc_row, |
160 | | size_t dc_stride, const float* JXL_RESTRICT biases, |
161 | | ACPtr qblock[3], float* JXL_RESTRICT block, |
162 | 33.1k | float* JXL_RESTRICT scratch) { |
163 | 33.1k | const auto scaled_dequant_s = inv_global_scale / quant; |
164 | | |
165 | 33.1k | const auto scaled_dequant_x = Set(d, scaled_dequant_s * x_dm_multiplier); |
166 | 33.1k | const auto scaled_dequant_y = Set(d, scaled_dequant_s); |
167 | 33.1k | const auto scaled_dequant_b = Set(d, scaled_dequant_s * b_dm_multiplier); |
168 | | |
169 | 33.1k | const float* dequant_matrices = quantizer.DequantMatrix(kind, 0); |
170 | | |
171 | 3.04M | for (size_t k = 0; k < covered_blocks * kDCTBlockSize; k += Lanes(d)) { |
172 | 3.00M | DequantLane<ac_type>(scaled_dequant_x, scaled_dequant_y, scaled_dequant_b, |
173 | 3.00M | dequant_matrices, size, k, x_cc_mul, b_cc_mul, biases, |
174 | 3.00M | qblock, block); |
175 | 3.00M | } |
176 | 132k | for (size_t c = 0; c < 3; c++) { |
177 | 99.1k | LowestFrequenciesFromDC(kind, dc_row[c] + sbx[c], dc_stride, |
178 | 99.1k | block + c * size, scratch); |
179 | 99.1k | } |
180 | 33.1k | } void jxl::N_SCALAR::DequantBlock<(jxl::ACType)0>(float, int, float, float, hwy::N_SCALAR::Vec1<float>, hwy::N_SCALAR::Vec1<float>, jxl::AcStrategyType, unsigned long, jxl::Quantizer const&, unsigned long, unsigned long const*, float const* restrict*, unsigned long, float const*, jxl::ACPtr*, float*, float*) Line | Count | Source | 162 | 23.3k | float* JXL_RESTRICT scratch) { | 163 | 23.3k | const auto scaled_dequant_s = inv_global_scale / quant; | 164 | | | 165 | 23.3k | const auto scaled_dequant_x = Set(d, scaled_dequant_s * x_dm_multiplier); | 166 | 23.3k | const auto scaled_dequant_y = Set(d, scaled_dequant_s); | 167 | 23.3k | const auto scaled_dequant_b = Set(d, scaled_dequant_s * b_dm_multiplier); | 168 | | | 169 | 23.3k | const float* dequant_matrices = quantizer.DequantMatrix(kind, 0); | 170 | | | 171 | 2.29M | for (size_t k = 0; k < covered_blocks * kDCTBlockSize; k += Lanes(d)) { | 172 | 2.27M | DequantLane<ac_type>(scaled_dequant_x, scaled_dequant_y, scaled_dequant_b, | 173 | 2.27M | dequant_matrices, size, k, x_cc_mul, b_cc_mul, biases, | 174 | 2.27M | qblock, block); | 175 | 2.27M | } | 176 | 93.3k | for (size_t c = 0; c < 3; c++) { | 177 | 69.9k | LowestFrequenciesFromDC(kind, dc_row[c] + sbx[c], dc_stride, | 178 | 69.9k | block + c * size, scratch); | 179 | 69.9k | } | 180 | 23.3k | } |
void jxl::N_SCALAR::DequantBlock<(jxl::ACType)1>(float, int, float, float, hwy::N_SCALAR::Vec1<float>, hwy::N_SCALAR::Vec1<float>, jxl::AcStrategyType, unsigned long, jxl::Quantizer const&, unsigned long, unsigned long const*, float const* restrict*, unsigned long, float const*, jxl::ACPtr*, float*, float*) Line | Count | Source | 162 | 9.82k | float* JXL_RESTRICT scratch) { | 163 | 9.82k | const auto scaled_dequant_s = inv_global_scale / quant; | 164 | | | 165 | 9.82k | const auto scaled_dequant_x = Set(d, scaled_dequant_s * x_dm_multiplier); | 166 | 9.82k | const auto scaled_dequant_y = Set(d, scaled_dequant_s); | 167 | 9.82k | const auto scaled_dequant_b = Set(d, scaled_dequant_s * b_dm_multiplier); | 168 | | | 169 | 9.82k | const float* dequant_matrices = quantizer.DequantMatrix(kind, 0); | 170 | | | 171 | 745k | for (size_t k = 0; k < covered_blocks * kDCTBlockSize; k += Lanes(d)) { | 172 | 735k | DequantLane<ac_type>(scaled_dequant_x, scaled_dequant_y, scaled_dequant_b, | 173 | 735k | dequant_matrices, size, k, x_cc_mul, b_cc_mul, biases, | 174 | 735k | qblock, block); | 175 | 735k | } | 176 | 39.0k | for (size_t c = 0; c < 3; c++) { | 177 | 29.2k | LowestFrequenciesFromDC(kind, dc_row[c] + sbx[c], dc_stride, | 178 | 29.2k | block + c * size, scratch); | 179 | 29.2k | } | 180 | 9.82k | } |
|
181 | | |
182 | | Status DecodeGroupImpl(const FrameHeader& frame_header, |
183 | | GetBlock* JXL_RESTRICT get_block, |
184 | | GroupDecCache* JXL_RESTRICT group_dec_cache, |
185 | | PassesDecoderState* JXL_RESTRICT dec_state, |
186 | | size_t thread, size_t group_idx, |
187 | | RenderPipelineInput& render_pipeline_input, |
188 | 5.10k | jpeg::JPEGData* jpeg_data, DrawMode draw) { |
189 | | // TODO(veluca): investigate cache usage in this function. |
190 | 5.10k | const Rect block_rect = |
191 | 5.10k | dec_state->shared->frame_dim.BlockGroupRect(group_idx); |
192 | 5.10k | const AcStrategyImage& ac_strategy = dec_state->shared->ac_strategy; |
193 | | |
194 | 5.10k | const size_t xsize_blocks = block_rect.xsize(); |
195 | 5.10k | const size_t ysize_blocks = block_rect.ysize(); |
196 | | |
197 | 5.10k | const size_t dc_stride = dec_state->shared->dc->PixelsPerRow(); |
198 | | |
199 | 5.10k | const float inv_global_scale = dec_state->shared->quantizer.InvGlobalScale(); |
200 | | |
201 | 5.10k | const YCbCrChromaSubsampling& cs = frame_header.chroma_subsampling; |
202 | | |
203 | 5.10k | const auto kJpegDctMin = Set(di16_full, -4095); |
204 | 5.10k | const auto kJpegDctMax = Set(di16_full, 4095); |
205 | | |
206 | 5.10k | size_t idct_stride[3]; |
207 | 20.4k | for (size_t c = 0; c < 3; c++) { |
208 | 15.2k | idct_stride[c] = render_pipeline_input.GetBuffer(c).first->PixelsPerRow(); |
209 | 15.2k | } |
210 | | |
211 | 5.10k | HWY_ALIGN int32_t scaled_qtable[64 * 3]; |
212 | | |
213 | 5.10k | ACType ac_type = dec_state->coefficients->Type(); |
214 | 5.10k | auto dequant_block = ac_type == ACType::k16 ? DequantBlock<ACType::k16> |
215 | 5.10k | : DequantBlock<ACType::k32>; |
216 | | // Whether or not coefficients should be stored for future usage, and/or read |
217 | | // from past usage. |
218 | 5.10k | bool accumulate = !dec_state->coefficients->IsEmpty(); |
219 | | // Offset of the current block in the group. |
220 | 5.10k | size_t offset = 0; |
221 | | |
222 | 5.10k | std::array<int, 3> jpeg_c_map; |
223 | 5.10k | bool jpeg_is_gray = false; |
224 | 5.10k | std::array<int, 3> dcoff = {}; |
225 | | |
226 | | // TODO(veluca): all of this should be done only once per image. |
227 | 5.10k | const ColorCorrelation& color_correlation = dec_state->shared->cmap.base(); |
228 | 5.10k | if (jpeg_data) { |
229 | 0 | if (!color_correlation.IsJPEGCompatible()) { |
230 | 0 | return JXL_FAILURE("The CfL map is not JPEG-compatible"); |
231 | 0 | } |
232 | 0 | jpeg_is_gray = (jpeg_data->components.size() == 1); |
233 | 0 | JXL_ENSURE(frame_header.color_transform != ColorTransform::kXYB); |
234 | 0 | jpeg_c_map = JpegOrder(frame_header.color_transform, jpeg_is_gray); |
235 | 0 | const std::vector<QuantEncoding>& qe = |
236 | 0 | dec_state->shared->matrices.encodings(); |
237 | 0 | if (qe.empty() || qe[0].mode != QuantEncoding::Mode::kQuantModeRAW || |
238 | 0 | std::abs(qe[0].qraw.qtable_den - 1.f / (8 * 255)) > 1e-8f) { |
239 | 0 | return JXL_FAILURE( |
240 | 0 | "Quantization table is not a JPEG quantization table."); |
241 | 0 | } |
242 | 0 | JXL_ENSURE(qe[0].qraw.qtable->size() == 3 * 8 * 8); |
243 | 0 | int* qtable = qe[0].qraw.qtable->data(); |
244 | 0 | for (size_t c = 0; c < 3; c++) { |
245 | 0 | if (frame_header.color_transform == ColorTransform::kNone) { |
246 | 0 | dcoff[c] = 1024 / qtable[64 * c]; |
247 | 0 | } |
248 | 0 | for (size_t i = 0; i < 64; i++) { |
249 | | // Transpose the matrix, as it will be used on the transposed block. |
250 | 0 | int num = qtable[64 + i]; |
251 | 0 | int den = qtable[64 * c + i]; |
252 | 0 | if (num <= 0 || den <= 0 || num >= 65536 || den >= 65536) { |
253 | 0 | return JXL_FAILURE("Invalid JPEG quantization table"); |
254 | 0 | } |
255 | 0 | scaled_qtable[64 * c + (i % 8) * 8 + (i / 8)] = |
256 | 0 | (1 << kCFLFixedPointPrecision) * num / den; |
257 | 0 | } |
258 | 0 | } |
259 | 0 | } |
260 | | |
261 | 5.10k | size_t hshift[3] = {cs.HShift(0), cs.HShift(1), cs.HShift(2)}; |
262 | 5.10k | size_t vshift[3] = {cs.VShift(0), cs.VShift(1), cs.VShift(2)}; |
263 | 5.10k | Rect r[3]; |
264 | 20.3k | for (size_t i = 0; i < 3; i++) { |
265 | 15.2k | r[i] = |
266 | 15.2k | Rect(block_rect.x0() >> hshift[i], block_rect.y0() >> vshift[i], |
267 | 15.2k | block_rect.xsize() >> hshift[i], block_rect.ysize() >> vshift[i]); |
268 | 15.2k | if (!r[i].IsInside({0, 0, dec_state->shared->dc->Plane(i).xsize(), |
269 | 15.2k | dec_state->shared->dc->Plane(i).ysize()})) { |
270 | 0 | return JXL_FAILURE("Frame dimensions are too big for the image."); |
271 | 0 | } |
272 | 15.2k | } |
273 | | |
274 | 15.0k | for (size_t by = 0; by < ysize_blocks; ++by) { |
275 | 9.92k | get_block->StartRow(by); |
276 | 9.92k | size_t sby[3] = {by >> vshift[0], by >> vshift[1], by >> vshift[2]}; |
277 | | |
278 | 9.92k | const int32_t* JXL_RESTRICT row_quant = |
279 | 9.92k | block_rect.ConstRow(dec_state->shared->raw_quant_field, by); |
280 | | |
281 | 9.92k | const float* JXL_RESTRICT dc_rows[3] = { |
282 | 9.92k | r[0].ConstPlaneRow(*dec_state->shared->dc, 0, sby[0]), |
283 | 9.92k | r[1].ConstPlaneRow(*dec_state->shared->dc, 1, sby[1]), |
284 | 9.92k | r[2].ConstPlaneRow(*dec_state->shared->dc, 2, sby[2]), |
285 | 9.92k | }; |
286 | | |
287 | 9.92k | const size_t ty = (block_rect.y0() + by) / kColorTileDimInBlocks; |
288 | 9.92k | AcStrategyRow acs_row = ac_strategy.ConstRow(block_rect, by); |
289 | | |
290 | 9.92k | const int8_t* JXL_RESTRICT row_cmap[3] = { |
291 | 9.92k | dec_state->shared->cmap.ytox_map.ConstRow(ty), |
292 | 9.92k | nullptr, |
293 | 9.92k | dec_state->shared->cmap.ytob_map.ConstRow(ty), |
294 | 9.92k | }; |
295 | | |
296 | 9.92k | float* JXL_RESTRICT idct_row[3]; |
297 | 9.92k | int16_t* JXL_RESTRICT jpeg_row[3]; |
298 | 39.6k | for (size_t c = 0; c < 3; c++) { |
299 | 29.7k | const auto& buffer = render_pipeline_input.GetBuffer(c); |
300 | 29.7k | idct_row[c] = buffer.second.Row(buffer.first, sby[c] * kBlockDim); |
301 | 29.7k | if (jpeg_data) { |
302 | 0 | auto& component = jpeg_data->components[jpeg_c_map[c]]; |
303 | 0 | jpeg_row[c] = |
304 | 0 | component.coeffs.data() + |
305 | 0 | (component.width_in_blocks * (r[c].y0() + sby[c]) + r[c].x0()) * |
306 | 0 | kDCTBlockSize; |
307 | 0 | } |
308 | 29.7k | } |
309 | | |
310 | 9.92k | size_t bx = 0; |
311 | 21.2k | for (size_t tx = 0; tx < DivCeil(xsize_blocks, kColorTileDimInBlocks); |
312 | 11.3k | tx++) { |
313 | 11.3k | size_t abs_tx = tx + block_rect.x0() / kColorTileDimInBlocks; |
314 | 11.3k | auto x_cc_mul = Set(d, color_correlation.YtoXRatio(row_cmap[0][abs_tx])); |
315 | 11.3k | auto b_cc_mul = Set(d, color_correlation.YtoBRatio(row_cmap[2][abs_tx])); |
316 | | // Increment bx by llf_x because those iterations would otherwise |
317 | | // immediately continue (!IsFirstBlock). Reduces mispredictions. |
318 | 45.0k | for (; bx < xsize_blocks && bx < (tx + 1) * kColorTileDimInBlocks;) { |
319 | 33.7k | size_t sbx[3] = {bx >> hshift[0], bx >> hshift[1], bx >> hshift[2]}; |
320 | 33.7k | AcStrategy acs = acs_row[bx]; |
321 | 33.7k | const size_t llf_x = acs.covered_blocks_x(); |
322 | | |
323 | | // Can only happen in the second or lower rows of a varblock. |
324 | 33.7k | if (JXL_UNLIKELY(!acs.IsFirstBlock())) { |
325 | 654 | bx += llf_x; |
326 | 654 | continue; |
327 | 654 | } |
328 | 33.0k | const size_t log2_covered_blocks = acs.log2_covered_blocks(); |
329 | | |
330 | 33.0k | const size_t covered_blocks = 1 << log2_covered_blocks; |
331 | 33.0k | const size_t size = covered_blocks * kDCTBlockSize; |
332 | | |
333 | 33.0k | ACPtr qblock[3]; |
334 | 33.0k | if (accumulate) { |
335 | 0 | for (size_t c = 0; c < 3; c++) { |
336 | 0 | qblock[c] = dec_state->coefficients->PlaneRow(c, group_idx, offset); |
337 | 0 | } |
338 | 33.0k | } else { |
339 | | // No point in reading from bitstream without accumulating and not |
340 | | // drawing. |
341 | 33.0k | JXL_ENSURE(draw == kDraw); |
342 | 33.0k | if (ac_type == ACType::k16) { |
343 | 23.3k | memset(group_dec_cache->dec_group_qblock16, 0, |
344 | 23.3k | size * 3 * sizeof(int16_t)); |
345 | 93.3k | for (size_t c = 0; c < 3; c++) { |
346 | 69.9k | qblock[c].ptr16 = group_dec_cache->dec_group_qblock16 + c * size; |
347 | 69.9k | } |
348 | 23.3k | } else { |
349 | 9.76k | memset(group_dec_cache->dec_group_qblock, 0, |
350 | 9.76k | size * 3 * sizeof(int32_t)); |
351 | 39.2k | for (size_t c = 0; c < 3; c++) { |
352 | 29.4k | qblock[c].ptr32 = group_dec_cache->dec_group_qblock + c * size; |
353 | 29.4k | } |
354 | 9.76k | } |
355 | 33.0k | } |
356 | 33.0k | JXL_RETURN_IF_ERROR(get_block->LoadBlock( |
357 | 33.0k | bx, by, acs, size, log2_covered_blocks, qblock, ac_type)); |
358 | 33.0k | offset += size; |
359 | 33.0k | if (draw == kDontDraw) { |
360 | 0 | bx += llf_x; |
361 | 0 | continue; |
362 | 0 | } |
363 | | |
364 | 33.0k | if (JXL_UNLIKELY(jpeg_data)) { |
365 | 0 | if (acs.Strategy() != AcStrategyType::DCT) { |
366 | 0 | return JXL_FAILURE( |
367 | 0 | "Can only decode to JPEG if only DCT-8 is used."); |
368 | 0 | } |
369 | | |
370 | 0 | HWY_ALIGN int32_t transposed_dct_y[64]; |
371 | 0 | for (size_t c : {1, 0, 2}) { |
372 | | // Propagate only Y for grayscale. |
373 | 0 | if (jpeg_is_gray && c != 1) { |
374 | 0 | continue; |
375 | 0 | } |
376 | 0 | if ((sbx[c] << hshift[c] != bx) || (sby[c] << vshift[c] != by)) { |
377 | 0 | continue; |
378 | 0 | } |
379 | 0 | int16_t* JXL_RESTRICT jpeg_pos = |
380 | 0 | jpeg_row[c] + sbx[c] * kDCTBlockSize; |
381 | | // JPEG XL is transposed, JPEG is not. |
382 | 0 | auto* transposed_dct = qblock[c].ptr32; |
383 | 0 | Transpose8x8InPlace(transposed_dct); |
384 | | // No CfL - no need to store the y block converted to integers. |
385 | 0 | if (!cs.Is444() || |
386 | 0 | (row_cmap[0][abs_tx] == 0 && row_cmap[2][abs_tx] == 0)) { |
387 | 0 | for (size_t i = 0; i < 64; i += Lanes(d)) { |
388 | 0 | const auto ini = Load(di, transposed_dct + i); |
389 | 0 | const auto ini16 = DemoteTo(di16, ini); |
390 | 0 | StoreU(ini16, di16, jpeg_pos + i); |
391 | 0 | } |
392 | 0 | } else if (c == 1) { |
393 | | // Y channel: save for restoring X/B, but nothing else to do. |
394 | 0 | for (size_t i = 0; i < 64; i += Lanes(d)) { |
395 | 0 | const auto ini = Load(di, transposed_dct + i); |
396 | 0 | Store(ini, di, transposed_dct_y + i); |
397 | 0 | const auto ini16 = DemoteTo(di16, ini); |
398 | 0 | StoreU(ini16, di16, jpeg_pos + i); |
399 | 0 | } |
400 | 0 | } else { |
401 | | // transposed_dct_y contains the y channel block, transposed. |
402 | 0 | const auto scale = |
403 | 0 | Set(di, ColorCorrelation::RatioJPEG(row_cmap[c][abs_tx])); |
404 | 0 | const auto round = Set(di, 1 << (kCFLFixedPointPrecision - 1)); |
405 | 0 | for (int i = 0; i < 64; i += Lanes(d)) { |
406 | 0 | auto in = Load(di, transposed_dct + i); |
407 | 0 | auto in_y = Load(di, transposed_dct_y + i); |
408 | 0 | auto qt = Load(di, scaled_qtable + c * size + i); |
409 | 0 | auto coeff_scale = ShiftRight<kCFLFixedPointPrecision>( |
410 | 0 | Add(Mul(qt, scale), round)); |
411 | 0 | auto cfl_factor = ShiftRight<kCFLFixedPointPrecision>( |
412 | 0 | Add(Mul(in_y, coeff_scale), round)); |
413 | 0 | StoreU(DemoteTo(di16, Add(in, cfl_factor)), di16, jpeg_pos + i); |
414 | 0 | } |
415 | 0 | } |
416 | 0 | jpeg_pos[0] = |
417 | 0 | Clamp1<float>(dc_rows[c][sbx[c]] - dcoff[c], -2047, 2047); |
418 | 0 | auto overflow = MaskFromVec(Set(di16_full, 0)); |
419 | 0 | auto underflow = MaskFromVec(Set(di16_full, 0)); |
420 | 0 | for (int i = 0; i < 64; i += Lanes(di16_full)) { |
421 | 0 | auto in = LoadU(di16_full, jpeg_pos + i); |
422 | 0 | overflow = Or(overflow, Gt(in, kJpegDctMax)); |
423 | 0 | underflow = Or(underflow, Lt(in, kJpegDctMin)); |
424 | 0 | } |
425 | 0 | if (!AllFalse(di16_full, Or(overflow, underflow))) { |
426 | 0 | return JXL_FAILURE("JPEG DCT coefficients out of range"); |
427 | 0 | } |
428 | 0 | } |
429 | 33.0k | } else { |
430 | 33.0k | HWY_ALIGN float* const block = group_dec_cache->dec_group_block; |
431 | | // Dequantize and add predictions. |
432 | 33.0k | dequant_block( |
433 | 33.0k | inv_global_scale, row_quant[bx], dec_state->x_dm_multiplier, |
434 | 33.0k | dec_state->b_dm_multiplier, x_cc_mul, b_cc_mul, acs.Strategy(), |
435 | 33.0k | size, dec_state->shared->quantizer, |
436 | 33.0k | acs.covered_blocks_y() * acs.covered_blocks_x(), sbx, dc_rows, |
437 | 33.0k | dc_stride, |
438 | 33.0k | dec_state->output_encoding_info.opsin_params.quant_biases, qblock, |
439 | 33.0k | block, group_dec_cache->scratch_space); |
440 | | |
441 | 99.1k | for (size_t c : {1, 0, 2}) { |
442 | 99.1k | if ((sbx[c] << hshift[c] != bx) || (sby[c] << vshift[c] != by)) { |
443 | 0 | continue; |
444 | 0 | } |
445 | | // IDCT |
446 | 99.1k | float* JXL_RESTRICT idct_pos = idct_row[c] + sbx[c] * kBlockDim; |
447 | 99.1k | TransformToPixels(acs.Strategy(), block + c * size, idct_pos, |
448 | 99.1k | idct_stride[c], group_dec_cache->scratch_space); |
449 | 99.1k | } |
450 | 33.0k | } |
451 | 33.0k | bx += llf_x; |
452 | 33.0k | } |
453 | 11.3k | } |
454 | 9.92k | } |
455 | 5.09k | return true; |
456 | 5.10k | } |
457 | | |
458 | | // NOLINTNEXTLINE(google-readability-namespace-comments) |
459 | | } // namespace HWY_NAMESPACE |
460 | | } // namespace jxl |
461 | | HWY_AFTER_NAMESPACE(); |
462 | | |
463 | | #if HWY_ONCE |
464 | | namespace jxl { |
465 | | namespace { |
466 | | // Decode quantized AC coefficients of DCT blocks. |
467 | | // LLF components in the output block will not be modified. |
468 | | template <ACType ac_type, bool uses_lz77> |
469 | | Status DecodeACVarBlock(size_t ctx_offset, size_t log2_covered_blocks, |
470 | | int32_t* JXL_RESTRICT row_nzeros, |
471 | | const int32_t* JXL_RESTRICT row_nzeros_top, |
472 | | size_t nzeros_stride, size_t c, size_t bx, size_t by, |
473 | | size_t lbx, AcStrategy acs, |
474 | | const coeff_order_t* JXL_RESTRICT coeff_order, |
475 | | BitReader* JXL_RESTRICT br, |
476 | | ANSSymbolReader* JXL_RESTRICT decoder, |
477 | | const std::vector<uint8_t>& context_map, |
478 | | const uint8_t* qdc_row, const int32_t* qf_row, |
479 | | const BlockCtxMap& block_ctx_map, ACPtr block, |
480 | 99.4k | size_t shift = 0) { |
481 | | // Equal to number of LLF coefficients. |
482 | 99.4k | const size_t covered_blocks = 1 << log2_covered_blocks; |
483 | 99.4k | const size_t size = covered_blocks * kDCTBlockSize; |
484 | 99.4k | int32_t predicted_nzeros = |
485 | 99.4k | PredictFromTopAndLeft(row_nzeros_top, row_nzeros, bx, 32); |
486 | | |
487 | 99.4k | size_t ord = kStrategyOrder[acs.RawStrategy()]; |
488 | 99.4k | const coeff_order_t* JXL_RESTRICT order = |
489 | 99.4k | &coeff_order[CoeffOrderOffset(ord, c)]; |
490 | | |
491 | 99.4k | size_t block_ctx = block_ctx_map.Context(qdc_row[lbx], qf_row[bx], ord, c); |
492 | 99.4k | const int32_t nzero_ctx = |
493 | 99.4k | block_ctx_map.NonZeroContext(predicted_nzeros, block_ctx) + ctx_offset; |
494 | | |
495 | 99.4k | size_t nzeros = |
496 | 99.4k | decoder->ReadHybridUintInlined<uses_lz77>(nzero_ctx, br, context_map); |
497 | 99.4k | if (nzeros > size - covered_blocks) { |
498 | 6 | return JXL_FAILURE("Invalid AC: nzeros %" PRIuS " too large for %" PRIuS |
499 | 6 | " 8x8 blocks", |
500 | 6 | nzeros, covered_blocks); |
501 | 6 | } |
502 | 201k | for (size_t y = 0; y < acs.covered_blocks_y(); y++) { |
503 | 260k | for (size_t x = 0; x < acs.covered_blocks_x(); x++) { |
504 | 158k | row_nzeros[bx + x + y * nzeros_stride] = |
505 | 158k | (nzeros + covered_blocks - 1) >> log2_covered_blocks; |
506 | 158k | } |
507 | 101k | } |
508 | | |
509 | 99.4k | const size_t histo_offset = |
510 | 99.4k | ctx_offset + block_ctx_map.ZeroDensityContextsOffset(block_ctx); |
511 | | |
512 | 99.4k | size_t prev = (nzeros > size / 16 ? 0 : 1); |
513 | 182k | for (size_t k = covered_blocks; k < size && nzeros != 0; ++k) { |
514 | 82.5k | const size_t ctx = |
515 | 82.5k | histo_offset + ZeroDensityContext(nzeros, k, covered_blocks, |
516 | 82.5k | log2_covered_blocks, prev); |
517 | 82.5k | const size_t u_coeff = |
518 | 82.5k | decoder->ReadHybridUintInlined<uses_lz77>(ctx, br, context_map); |
519 | | // Hand-rolled version of UnpackSigned, shifting before the conversion to |
520 | | // signed integer to avoid undefined behavior of shifting negative numbers. |
521 | 82.5k | const size_t magnitude = u_coeff >> 1; |
522 | 82.5k | const size_t neg_sign = (~u_coeff) & 1; |
523 | 82.5k | const ptrdiff_t coeff = |
524 | 82.5k | static_cast<ptrdiff_t>((magnitude ^ (neg_sign - 1)) << shift); |
525 | 82.5k | if (ac_type == ACType::k16) { |
526 | 37.9k | block.ptr16[order[k]] += coeff; |
527 | 44.6k | } else { |
528 | 44.6k | block.ptr32[order[k]] += coeff; |
529 | 44.6k | } |
530 | 82.5k | prev = static_cast<size_t>(u_coeff != 0); |
531 | 82.5k | nzeros -= prev; |
532 | 82.5k | } |
533 | 99.4k | if (JXL_UNLIKELY(nzeros != 0)) { |
534 | 6 | return JXL_FAILURE("Invalid AC: nzeros at end of block is %" PRIuS |
535 | 6 | ", should be 0. Block (%" PRIuS ", %" PRIuS |
536 | 6 | "), channel %" PRIuS, |
537 | 6 | nzeros, bx, by, c); |
538 | 6 | } |
539 | | |
540 | 99.4k | return true; |
541 | 99.4k | } dec_group.cc:jxl::Status jxl::(anonymous namespace)::DecodeACVarBlock<(jxl::ACType)0, true>(unsigned long, unsigned long, int*, int const*, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, jxl::AcStrategy, unsigned int const*, jxl::BitReader*, jxl::ANSSymbolReader*, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, unsigned char const*, int const*, jxl::BlockCtxMap const&, jxl::ACPtr, unsigned long) Line | Count | Source | 480 | 68.6k | size_t shift = 0) { | 481 | | // Equal to number of LLF coefficients. | 482 | 68.6k | const size_t covered_blocks = 1 << log2_covered_blocks; | 483 | 68.6k | const size_t size = covered_blocks * kDCTBlockSize; | 484 | 68.6k | int32_t predicted_nzeros = | 485 | 68.6k | PredictFromTopAndLeft(row_nzeros_top, row_nzeros, bx, 32); | 486 | | | 487 | 68.6k | size_t ord = kStrategyOrder[acs.RawStrategy()]; | 488 | 68.6k | const coeff_order_t* JXL_RESTRICT order = | 489 | 68.6k | &coeff_order[CoeffOrderOffset(ord, c)]; | 490 | | | 491 | 68.6k | size_t block_ctx = block_ctx_map.Context(qdc_row[lbx], qf_row[bx], ord, c); | 492 | 68.6k | const int32_t nzero_ctx = | 493 | 68.6k | block_ctx_map.NonZeroContext(predicted_nzeros, block_ctx) + ctx_offset; | 494 | | | 495 | 68.6k | size_t nzeros = | 496 | 68.6k | decoder->ReadHybridUintInlined<uses_lz77>(nzero_ctx, br, context_map); | 497 | 68.6k | if (nzeros > size - covered_blocks) { | 498 | 3 | return JXL_FAILURE("Invalid AC: nzeros %" PRIuS " too large for %" PRIuS | 499 | 3 | " 8x8 blocks", | 500 | 3 | nzeros, covered_blocks); | 501 | 3 | } | 502 | 138k | for (size_t y = 0; y < acs.covered_blocks_y(); y++) { | 503 | 172k | for (size_t x = 0; x < acs.covered_blocks_x(); x++) { | 504 | 102k | row_nzeros[bx + x + y * nzeros_stride] = | 505 | 102k | (nzeros + covered_blocks - 1) >> log2_covered_blocks; | 506 | 102k | } | 507 | 70.0k | } | 508 | | | 509 | 68.6k | const size_t histo_offset = | 510 | 68.6k | ctx_offset + block_ctx_map.ZeroDensityContextsOffset(block_ctx); | 511 | | | 512 | 68.6k | size_t prev = (nzeros > size / 16 ? 0 : 1); | 513 | 85.3k | for (size_t k = covered_blocks; k < size && nzeros != 0; ++k) { | 514 | 16.7k | const size_t ctx = | 515 | 16.7k | histo_offset + ZeroDensityContext(nzeros, k, covered_blocks, | 516 | 16.7k | log2_covered_blocks, prev); | 517 | 16.7k | const size_t u_coeff = | 518 | 16.7k | decoder->ReadHybridUintInlined<uses_lz77>(ctx, br, context_map); | 519 | | // Hand-rolled version of UnpackSigned, shifting before the conversion to | 520 | | // signed integer to avoid undefined behavior of shifting negative numbers. | 521 | 16.7k | const size_t magnitude = u_coeff >> 1; | 522 | 16.7k | const size_t neg_sign = (~u_coeff) & 1; | 523 | 16.7k | const ptrdiff_t coeff = | 524 | 16.7k | static_cast<ptrdiff_t>((magnitude ^ (neg_sign - 1)) << shift); | 525 | 16.7k | if (ac_type == ACType::k16) { | 526 | 16.7k | block.ptr16[order[k]] += coeff; | 527 | 16.7k | } else { | 528 | 0 | block.ptr32[order[k]] += coeff; | 529 | 0 | } | 530 | 16.7k | prev = static_cast<size_t>(u_coeff != 0); | 531 | 16.7k | nzeros -= prev; | 532 | 16.7k | } | 533 | 68.6k | if (JXL_UNLIKELY(nzeros != 0)) { | 534 | 0 | return JXL_FAILURE("Invalid AC: nzeros at end of block is %" PRIuS | 535 | 0 | ", should be 0. Block (%" PRIuS ", %" PRIuS | 536 | 0 | "), channel %" PRIuS, | 537 | 0 | nzeros, bx, by, c); | 538 | 0 | } | 539 | | | 540 | 68.6k | return true; | 541 | 68.6k | } |
dec_group.cc:jxl::Status jxl::(anonymous namespace)::DecodeACVarBlock<(jxl::ACType)1, true>(unsigned long, unsigned long, int*, int const*, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, jxl::AcStrategy, unsigned int const*, jxl::BitReader*, jxl::ANSSymbolReader*, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, unsigned char const*, int const*, jxl::BlockCtxMap const&, jxl::ACPtr, unsigned long) Line | Count | Source | 480 | 11.9k | size_t shift = 0) { | 481 | | // Equal to number of LLF coefficients. | 482 | 11.9k | const size_t covered_blocks = 1 << log2_covered_blocks; | 483 | 11.9k | const size_t size = covered_blocks * kDCTBlockSize; | 484 | 11.9k | int32_t predicted_nzeros = | 485 | 11.9k | PredictFromTopAndLeft(row_nzeros_top, row_nzeros, bx, 32); | 486 | | | 487 | 11.9k | size_t ord = kStrategyOrder[acs.RawStrategy()]; | 488 | 11.9k | const coeff_order_t* JXL_RESTRICT order = | 489 | 11.9k | &coeff_order[CoeffOrderOffset(ord, c)]; | 490 | | | 491 | 11.9k | size_t block_ctx = block_ctx_map.Context(qdc_row[lbx], qf_row[bx], ord, c); | 492 | 11.9k | const int32_t nzero_ctx = | 493 | 11.9k | block_ctx_map.NonZeroContext(predicted_nzeros, block_ctx) + ctx_offset; | 494 | | | 495 | 11.9k | size_t nzeros = | 496 | 11.9k | decoder->ReadHybridUintInlined<uses_lz77>(nzero_ctx, br, context_map); | 497 | 11.9k | if (nzeros > size - covered_blocks) { | 498 | 0 | return JXL_FAILURE("Invalid AC: nzeros %" PRIuS " too large for %" PRIuS | 499 | 0 | " 8x8 blocks", | 500 | 0 | nzeros, covered_blocks); | 501 | 0 | } | 502 | 23.8k | for (size_t y = 0; y < acs.covered_blocks_y(); y++) { | 503 | 29.2k | for (size_t x = 0; x < acs.covered_blocks_x(); x++) { | 504 | 17.3k | row_nzeros[bx + x + y * nzeros_stride] = | 505 | 17.3k | (nzeros + covered_blocks - 1) >> log2_covered_blocks; | 506 | 17.3k | } | 507 | 11.9k | } | 508 | | | 509 | 11.9k | const size_t histo_offset = | 510 | 11.9k | ctx_offset + block_ctx_map.ZeroDensityContextsOffset(block_ctx); | 511 | | | 512 | 11.9k | size_t prev = (nzeros > size / 16 ? 0 : 1); | 513 | 11.9k | for (size_t k = covered_blocks; k < size && nzeros != 0; ++k) { | 514 | 0 | const size_t ctx = | 515 | 0 | histo_offset + ZeroDensityContext(nzeros, k, covered_blocks, | 516 | 0 | log2_covered_blocks, prev); | 517 | 0 | const size_t u_coeff = | 518 | 0 | decoder->ReadHybridUintInlined<uses_lz77>(ctx, br, context_map); | 519 | | // Hand-rolled version of UnpackSigned, shifting before the conversion to | 520 | | // signed integer to avoid undefined behavior of shifting negative numbers. | 521 | 0 | const size_t magnitude = u_coeff >> 1; | 522 | 0 | const size_t neg_sign = (~u_coeff) & 1; | 523 | 0 | const ptrdiff_t coeff = | 524 | 0 | static_cast<ptrdiff_t>((magnitude ^ (neg_sign - 1)) << shift); | 525 | 0 | if (ac_type == ACType::k16) { | 526 | 0 | block.ptr16[order[k]] += coeff; | 527 | 0 | } else { | 528 | 0 | block.ptr32[order[k]] += coeff; | 529 | 0 | } | 530 | 0 | prev = static_cast<size_t>(u_coeff != 0); | 531 | 0 | nzeros -= prev; | 532 | 0 | } | 533 | 11.9k | if (JXL_UNLIKELY(nzeros != 0)) { | 534 | 0 | return JXL_FAILURE("Invalid AC: nzeros at end of block is %" PRIuS | 535 | 0 | ", should be 0. Block (%" PRIuS ", %" PRIuS | 536 | 0 | "), channel %" PRIuS, | 537 | 0 | nzeros, bx, by, c); | 538 | 0 | } | 539 | | | 540 | 11.9k | return true; | 541 | 11.9k | } |
dec_group.cc:jxl::Status jxl::(anonymous namespace)::DecodeACVarBlock<(jxl::ACType)0, false>(unsigned long, unsigned long, int*, int const*, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, jxl::AcStrategy, unsigned int const*, jxl::BitReader*, jxl::ANSSymbolReader*, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, unsigned char const*, int const*, jxl::BlockCtxMap const&, jxl::ACPtr, unsigned long) Line | Count | Source | 480 | 1.37k | size_t shift = 0) { | 481 | | // Equal to number of LLF coefficients. | 482 | 1.37k | const size_t covered_blocks = 1 << log2_covered_blocks; | 483 | 1.37k | const size_t size = covered_blocks * kDCTBlockSize; | 484 | 1.37k | int32_t predicted_nzeros = | 485 | 1.37k | PredictFromTopAndLeft(row_nzeros_top, row_nzeros, bx, 32); | 486 | | | 487 | 1.37k | size_t ord = kStrategyOrder[acs.RawStrategy()]; | 488 | 1.37k | const coeff_order_t* JXL_RESTRICT order = | 489 | 1.37k | &coeff_order[CoeffOrderOffset(ord, c)]; | 490 | | | 491 | 1.37k | size_t block_ctx = block_ctx_map.Context(qdc_row[lbx], qf_row[bx], ord, c); | 492 | 1.37k | const int32_t nzero_ctx = | 493 | 1.37k | block_ctx_map.NonZeroContext(predicted_nzeros, block_ctx) + ctx_offset; | 494 | | | 495 | 1.37k | size_t nzeros = | 496 | 1.37k | decoder->ReadHybridUintInlined<uses_lz77>(nzero_ctx, br, context_map); | 497 | 1.37k | if (nzeros > size - covered_blocks) { | 498 | 1 | return JXL_FAILURE("Invalid AC: nzeros %" PRIuS " too large for %" PRIuS | 499 | 1 | " 8x8 blocks", | 500 | 1 | nzeros, covered_blocks); | 501 | 1 | } | 502 | 3.51k | for (size_t y = 0; y < acs.covered_blocks_y(); y++) { | 503 | 6.00k | for (size_t x = 0; x < acs.covered_blocks_x(); x++) { | 504 | 3.85k | row_nzeros[bx + x + y * nzeros_stride] = | 505 | 3.85k | (nzeros + covered_blocks - 1) >> log2_covered_blocks; | 506 | 3.85k | } | 507 | 2.14k | } | 508 | | | 509 | 1.36k | const size_t histo_offset = | 510 | 1.36k | ctx_offset + block_ctx_map.ZeroDensityContextsOffset(block_ctx); | 511 | | | 512 | 1.36k | size_t prev = (nzeros > size / 16 ? 0 : 1); | 513 | 22.5k | for (size_t k = covered_blocks; k < size && nzeros != 0; ++k) { | 514 | 21.1k | const size_t ctx = | 515 | 21.1k | histo_offset + ZeroDensityContext(nzeros, k, covered_blocks, | 516 | 21.1k | log2_covered_blocks, prev); | 517 | 21.1k | const size_t u_coeff = | 518 | 21.1k | decoder->ReadHybridUintInlined<uses_lz77>(ctx, br, context_map); | 519 | | // Hand-rolled version of UnpackSigned, shifting before the conversion to | 520 | | // signed integer to avoid undefined behavior of shifting negative numbers. | 521 | 21.1k | const size_t magnitude = u_coeff >> 1; | 522 | 21.1k | const size_t neg_sign = (~u_coeff) & 1; | 523 | 21.1k | const ptrdiff_t coeff = | 524 | 21.1k | static_cast<ptrdiff_t>((magnitude ^ (neg_sign - 1)) << shift); | 525 | 21.1k | if (ac_type == ACType::k16) { | 526 | 21.1k | block.ptr16[order[k]] += coeff; | 527 | 21.1k | } else { | 528 | 0 | block.ptr32[order[k]] += coeff; | 529 | 0 | } | 530 | 21.1k | prev = static_cast<size_t>(u_coeff != 0); | 531 | 21.1k | nzeros -= prev; | 532 | 21.1k | } | 533 | 1.36k | if (JXL_UNLIKELY(nzeros != 0)) { | 534 | 3 | return JXL_FAILURE("Invalid AC: nzeros at end of block is %" PRIuS | 535 | 3 | ", should be 0. Block (%" PRIuS ", %" PRIuS | 536 | 3 | "), channel %" PRIuS, | 537 | 3 | nzeros, bx, by, c); | 538 | 3 | } | 539 | | | 540 | 1.36k | return true; | 541 | 1.36k | } |
dec_group.cc:jxl::Status jxl::(anonymous namespace)::DecodeACVarBlock<(jxl::ACType)1, false>(unsigned long, unsigned long, int*, int const*, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, jxl::AcStrategy, unsigned int const*, jxl::BitReader*, jxl::ANSSymbolReader*, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, unsigned char const*, int const*, jxl::BlockCtxMap const&, jxl::ACPtr, unsigned long) Line | Count | Source | 480 | 17.5k | size_t shift = 0) { | 481 | | // Equal to number of LLF coefficients. | 482 | 17.5k | const size_t covered_blocks = 1 << log2_covered_blocks; | 483 | 17.5k | const size_t size = covered_blocks * kDCTBlockSize; | 484 | 17.5k | int32_t predicted_nzeros = | 485 | 17.5k | PredictFromTopAndLeft(row_nzeros_top, row_nzeros, bx, 32); | 486 | | | 487 | 17.5k | size_t ord = kStrategyOrder[acs.RawStrategy()]; | 488 | 17.5k | const coeff_order_t* JXL_RESTRICT order = | 489 | 17.5k | &coeff_order[CoeffOrderOffset(ord, c)]; | 490 | | | 491 | 17.5k | size_t block_ctx = block_ctx_map.Context(qdc_row[lbx], qf_row[bx], ord, c); | 492 | 17.5k | const int32_t nzero_ctx = | 493 | 17.5k | block_ctx_map.NonZeroContext(predicted_nzeros, block_ctx) + ctx_offset; | 494 | | | 495 | 17.5k | size_t nzeros = | 496 | 17.5k | decoder->ReadHybridUintInlined<uses_lz77>(nzero_ctx, br, context_map); | 497 | 17.5k | if (nzeros > size - covered_blocks) { | 498 | 2 | return JXL_FAILURE("Invalid AC: nzeros %" PRIuS " too large for %" PRIuS | 499 | 2 | " 8x8 blocks", | 500 | 2 | nzeros, covered_blocks); | 501 | 2 | } | 502 | 35.0k | for (size_t y = 0; y < acs.covered_blocks_y(); y++) { | 503 | 52.4k | for (size_t x = 0; x < acs.covered_blocks_x(); x++) { | 504 | 34.9k | row_nzeros[bx + x + y * nzeros_stride] = | 505 | 34.9k | (nzeros + covered_blocks - 1) >> log2_covered_blocks; | 506 | 34.9k | } | 507 | 17.5k | } | 508 | | | 509 | 17.5k | const size_t histo_offset = | 510 | 17.5k | ctx_offset + block_ctx_map.ZeroDensityContextsOffset(block_ctx); | 511 | | | 512 | 17.5k | size_t prev = (nzeros > size / 16 ? 0 : 1); | 513 | 62.2k | for (size_t k = covered_blocks; k < size && nzeros != 0; ++k) { | 514 | 44.6k | const size_t ctx = | 515 | 44.6k | histo_offset + ZeroDensityContext(nzeros, k, covered_blocks, | 516 | 44.6k | log2_covered_blocks, prev); | 517 | 44.6k | const size_t u_coeff = | 518 | 44.6k | decoder->ReadHybridUintInlined<uses_lz77>(ctx, br, context_map); | 519 | | // Hand-rolled version of UnpackSigned, shifting before the conversion to | 520 | | // signed integer to avoid undefined behavior of shifting negative numbers. | 521 | 44.6k | const size_t magnitude = u_coeff >> 1; | 522 | 44.6k | const size_t neg_sign = (~u_coeff) & 1; | 523 | 44.6k | const ptrdiff_t coeff = | 524 | 44.6k | static_cast<ptrdiff_t>((magnitude ^ (neg_sign - 1)) << shift); | 525 | 44.6k | if (ac_type == ACType::k16) { | 526 | 0 | block.ptr16[order[k]] += coeff; | 527 | 44.6k | } else { | 528 | 44.6k | block.ptr32[order[k]] += coeff; | 529 | 44.6k | } | 530 | 44.6k | prev = static_cast<size_t>(u_coeff != 0); | 531 | 44.6k | nzeros -= prev; | 532 | 44.6k | } | 533 | 17.5k | if (JXL_UNLIKELY(nzeros != 0)) { | 534 | 3 | return JXL_FAILURE("Invalid AC: nzeros at end of block is %" PRIuS | 535 | 3 | ", should be 0. Block (%" PRIuS ", %" PRIuS | 536 | 3 | "), channel %" PRIuS, | 537 | 3 | nzeros, bx, by, c); | 538 | 3 | } | 539 | | | 540 | 17.5k | return true; | 541 | 17.5k | } |
|
542 | | |
543 | | // Structs used by DecodeGroupImpl to get a quantized block. |
544 | | // GetBlockFromBitstream uses ANS decoding (and thus keeps track of row |
545 | | // pointers in row_nzeros), GetBlockFromEncoder simply reads the coefficient |
546 | | // image provided by the encoder. |
547 | | |
548 | | struct GetBlockFromBitstream : public GetBlock { |
549 | 9.92k | void StartRow(size_t by) override { |
550 | 9.92k | qf_row = rect.ConstRow(*qf, by); |
551 | 39.6k | for (size_t c = 0; c < 3; c++) { |
552 | 29.7k | size_t sby = by >> vshift[c]; |
553 | 29.7k | quant_dc_row = quant_dc->ConstRow(rect.y0() + by) + rect.x0(); |
554 | 59.5k | for (size_t i = 0; i < num_passes; i++) { |
555 | 29.7k | row_nzeros[i][c] = group_dec_cache->num_nzeroes[i].PlaneRow(c, sby); |
556 | 29.7k | row_nzeros_top[i][c] = |
557 | 29.7k | sby == 0 |
558 | 29.7k | ? nullptr |
559 | 29.7k | : group_dec_cache->num_nzeroes[i].ConstPlaneRow(c, sby - 1); |
560 | 29.7k | } |
561 | 29.7k | } |
562 | 9.92k | } |
563 | | |
564 | | Status LoadBlock(size_t bx, size_t by, const AcStrategy& acs, size_t size, |
565 | | size_t log2_covered_blocks, ACPtr block[3], |
566 | 33.1k | ACType ac_type) override { |
567 | 33.1k | ; |
568 | 99.4k | for (size_t c : {1, 0, 2}) { |
569 | 99.4k | size_t sbx = bx >> hshift[c]; |
570 | 99.4k | size_t sby = by >> vshift[c]; |
571 | 99.4k | if (JXL_UNLIKELY((sbx << hshift[c] != bx) || (sby << vshift[c] != by))) { |
572 | 0 | continue; |
573 | 0 | } |
574 | | |
575 | 198k | for (size_t pass = 0; JXL_UNLIKELY(pass < num_passes); pass++) { |
576 | 99.4k | auto decode_ac_varblock = |
577 | 99.4k | decoders[pass].UsesLZ77() |
578 | 99.4k | ? (ac_type == ACType::k16 ? DecodeACVarBlock<ACType::k16, 1> |
579 | 80.5k | : DecodeACVarBlock<ACType::k32, 1>) |
580 | 99.4k | : (ac_type == ACType::k16 ? DecodeACVarBlock<ACType::k16, 0> |
581 | 18.9k | : DecodeACVarBlock<ACType::k32, 0>); |
582 | 99.4k | JXL_RETURN_IF_ERROR(decode_ac_varblock( |
583 | 99.4k | ctx_offset[pass], log2_covered_blocks, row_nzeros[pass][c], |
584 | 99.4k | row_nzeros_top[pass][c], nzeros_stride, c, sbx, sby, bx, acs, |
585 | 99.4k | &coeff_orders[pass * coeff_order_size], readers[pass], |
586 | 99.4k | &decoders[pass], context_map[pass], quant_dc_row, qf_row, |
587 | 99.4k | *block_ctx_map, block[c], shift_for_pass[pass])); |
588 | 99.4k | } |
589 | 99.4k | } |
590 | 33.1k | return true; |
591 | 33.1k | } |
592 | | |
593 | | Status Init(const FrameHeader& frame_header, |
594 | | BitReader* JXL_RESTRICT* JXL_RESTRICT readers_, |
595 | | size_t num_passes_, size_t group_idx, size_t histo_selector_bits, |
596 | | const Rect& rect_, GroupDecCache* JXL_RESTRICT group_dec_cache_, |
597 | 5.10k | PassesDecoderState* dec_state, size_t first_pass) { |
598 | 20.4k | for (size_t i = 0; i < 3; i++) { |
599 | 15.3k | hshift[i] = frame_header.chroma_subsampling.HShift(i); |
600 | 15.3k | vshift[i] = frame_header.chroma_subsampling.VShift(i); |
601 | 15.3k | } |
602 | 5.10k | coeff_order_size = dec_state->shared->coeff_order_size; |
603 | 5.10k | coeff_orders = |
604 | 5.10k | dec_state->shared->coeff_orders.data() + first_pass * coeff_order_size; |
605 | 5.10k | context_map = dec_state->context_map.data() + first_pass; |
606 | 5.10k | readers = readers_; |
607 | 5.10k | num_passes = num_passes_; |
608 | 5.10k | shift_for_pass = frame_header.passes.shift + first_pass; |
609 | 5.10k | group_dec_cache = group_dec_cache_; |
610 | 5.10k | rect = rect_; |
611 | 5.10k | block_ctx_map = &dec_state->shared->block_ctx_map; |
612 | 5.10k | qf = &dec_state->shared->raw_quant_field; |
613 | 5.10k | quant_dc = &dec_state->shared->quant_dc; |
614 | | |
615 | 10.2k | for (size_t pass = 0; pass < num_passes; pass++) { |
616 | | // Select which histogram set to use among those of the current pass. |
617 | 5.10k | size_t cur_histogram = 0; |
618 | 5.10k | if (histo_selector_bits != 0) { |
619 | 373 | cur_histogram = readers[pass]->ReadBits(histo_selector_bits); |
620 | 373 | } |
621 | 5.10k | if (cur_histogram >= dec_state->shared->num_histograms) { |
622 | 0 | return JXL_FAILURE("Invalid histogram selector"); |
623 | 0 | } |
624 | 5.10k | ctx_offset[pass] = cur_histogram * block_ctx_map->NumACContexts(); |
625 | | |
626 | 5.10k | JXL_ASSIGN_OR_RETURN( |
627 | 5.10k | decoders[pass], |
628 | 5.10k | ANSSymbolReader::Create(&dec_state->code[pass + first_pass], |
629 | 5.10k | readers[pass])); |
630 | 5.10k | } |
631 | 5.10k | nzeros_stride = group_dec_cache->num_nzeroes[0].PixelsPerRow(); |
632 | 10.2k | for (size_t i = 0; i < num_passes; i++) { |
633 | 5.10k | JXL_ENSURE( |
634 | 5.10k | nzeros_stride == |
635 | 5.10k | static_cast<size_t>(group_dec_cache->num_nzeroes[i].PixelsPerRow())); |
636 | 5.10k | } |
637 | 5.10k | return true; |
638 | 5.10k | } |
639 | | |
640 | | const uint32_t* shift_for_pass = nullptr; // not owned |
641 | | const coeff_order_t* JXL_RESTRICT coeff_orders; |
642 | | size_t coeff_order_size; |
643 | | const std::vector<uint8_t>* JXL_RESTRICT context_map; |
644 | | ANSSymbolReader decoders[kMaxNumPasses]; |
645 | | BitReader* JXL_RESTRICT* JXL_RESTRICT readers; |
646 | | size_t num_passes; |
647 | | size_t ctx_offset[kMaxNumPasses]; |
648 | | size_t nzeros_stride; |
649 | | int32_t* JXL_RESTRICT row_nzeros[kMaxNumPasses][3]; |
650 | | const int32_t* JXL_RESTRICT row_nzeros_top[kMaxNumPasses][3]; |
651 | | GroupDecCache* JXL_RESTRICT group_dec_cache; |
652 | | const BlockCtxMap* block_ctx_map; |
653 | | const ImageI* qf; |
654 | | const ImageB* quant_dc; |
655 | | const int32_t* qf_row; |
656 | | const uint8_t* quant_dc_row; |
657 | | Rect rect; |
658 | | size_t hshift[3], vshift[3]; |
659 | | }; |
660 | | |
661 | | struct GetBlockFromEncoder : public GetBlock { |
662 | 0 | void StartRow(size_t by) override {} |
663 | | |
664 | | Status LoadBlock(size_t bx, size_t by, const AcStrategy& acs, size_t size, |
665 | | size_t log2_covered_blocks, ACPtr block[3], |
666 | 0 | ACType ac_type) override { |
667 | 0 | JXL_ENSURE(ac_type == ACType::k32); |
668 | 0 | for (size_t c = 0; c < 3; c++) { |
669 | | // for each pass |
670 | 0 | for (size_t i = 0; i < quantized_ac->size(); i++) { |
671 | 0 | for (size_t k = 0; k < size; k++) { |
672 | | // TODO(veluca): SIMD. |
673 | 0 | block[c].ptr32[k] += |
674 | 0 | rows[i][c][offset + k] * (1 << shift_for_pass[i]); |
675 | 0 | } |
676 | 0 | } |
677 | 0 | } |
678 | 0 | offset += size; |
679 | 0 | return true; |
680 | 0 | } |
681 | | |
682 | | static StatusOr<GetBlockFromEncoder> Create( |
683 | | const std::vector<std::unique_ptr<ACImage>>& ac, size_t group_idx, |
684 | 0 | const uint32_t* shift_for_pass) { |
685 | 0 | GetBlockFromEncoder result(ac, group_idx, shift_for_pass); |
686 | | // TODO(veluca): not supported with chroma subsampling. |
687 | 0 | for (size_t i = 0; i < ac.size(); i++) { |
688 | 0 | JXL_ENSURE(ac[i]->Type() == ACType::k32); |
689 | 0 | for (size_t c = 0; c < 3; c++) { |
690 | 0 | result.rows[i][c] = ac[i]->PlaneRow(c, group_idx, 0).ptr32; |
691 | 0 | } |
692 | 0 | } |
693 | 0 | return result; |
694 | 0 | } |
695 | | |
696 | | const std::vector<std::unique_ptr<ACImage>>* JXL_RESTRICT quantized_ac; |
697 | | size_t offset = 0; |
698 | | const int32_t* JXL_RESTRICT rows[kMaxNumPasses][3]; |
699 | | const uint32_t* shift_for_pass = nullptr; // not owned |
700 | | |
701 | | private: |
702 | | GetBlockFromEncoder(const std::vector<std::unique_ptr<ACImage>>& ac, |
703 | | size_t group_idx, const uint32_t* shift_for_pass) |
704 | 0 | : quantized_ac(&ac), shift_for_pass(shift_for_pass) {} |
705 | | }; |
706 | | |
707 | | HWY_EXPORT(DecodeGroupImpl); |
708 | | |
709 | | } // namespace |
710 | | |
711 | | Status DecodeGroup(const FrameHeader& frame_header, |
712 | | BitReader* JXL_RESTRICT* JXL_RESTRICT readers, |
713 | | size_t num_passes, size_t group_idx, |
714 | | PassesDecoderState* JXL_RESTRICT dec_state, |
715 | | GroupDecCache* JXL_RESTRICT group_dec_cache, size_t thread, |
716 | | RenderPipelineInput& render_pipeline_input, |
717 | | jpeg::JPEGData* JXL_RESTRICT jpeg_data, size_t first_pass, |
718 | 5.10k | bool force_draw, bool dc_only, bool* should_run_pipeline) { |
719 | 5.10k | JxlMemoryManager* memory_manager = dec_state->memory_manager(); |
720 | 5.10k | DrawMode draw = |
721 | 5.10k | (num_passes + first_pass == frame_header.passes.num_passes) || force_draw |
722 | 5.10k | ? kDraw |
723 | 5.10k | : kDontDraw; |
724 | | |
725 | 5.10k | if (should_run_pipeline) { |
726 | 5.10k | *should_run_pipeline = draw != kDontDraw; |
727 | 5.10k | } |
728 | | |
729 | 5.10k | if (draw == kDraw && num_passes == 0 && first_pass == 0) { |
730 | 0 | JXL_RETURN_IF_ERROR(group_dec_cache->InitDCBufferOnce(memory_manager)); |
731 | 0 | const YCbCrChromaSubsampling& cs = frame_header.chroma_subsampling; |
732 | 0 | for (size_t c : {0, 1, 2}) { |
733 | 0 | size_t hs = cs.HShift(c); |
734 | 0 | size_t vs = cs.VShift(c); |
735 | | // We reuse filter_input_storage here as it is not currently in use. |
736 | 0 | const Rect src_rect_precs = |
737 | 0 | dec_state->shared->frame_dim.BlockGroupRect(group_idx); |
738 | 0 | const Rect src_rect = |
739 | 0 | Rect(src_rect_precs.x0() >> hs, src_rect_precs.y0() >> vs, |
740 | 0 | src_rect_precs.xsize() >> hs, src_rect_precs.ysize() >> vs); |
741 | 0 | const Rect copy_rect(kRenderPipelineXOffset, 2, src_rect.xsize(), |
742 | 0 | src_rect.ysize()); |
743 | 0 | JXL_RETURN_IF_ERROR( |
744 | 0 | CopyImageToWithPadding(src_rect, dec_state->shared->dc->Plane(c), 2, |
745 | 0 | copy_rect, &group_dec_cache->dc_buffer)); |
746 | | // Mirrorpad. Interleaving left and right padding ensures that padding |
747 | | // works out correctly even for images with DC size of 1. |
748 | 0 | for (size_t y = 0; y < src_rect.ysize() + 4; y++) { |
749 | 0 | size_t xend = kRenderPipelineXOffset + |
750 | 0 | (dec_state->shared->dc->Plane(c).xsize() >> hs) - |
751 | 0 | src_rect.x0(); |
752 | 0 | for (size_t ix = 0; ix < 2; ix++) { |
753 | 0 | if (src_rect.x0() == 0) { |
754 | 0 | group_dec_cache->dc_buffer.Row(y)[kRenderPipelineXOffset - ix - 1] = |
755 | 0 | group_dec_cache->dc_buffer.Row(y)[kRenderPipelineXOffset + ix]; |
756 | 0 | } |
757 | 0 | if (src_rect.x0() + src_rect.xsize() + 2 >= |
758 | 0 | (dec_state->shared->dc->xsize() >> hs)) { |
759 | 0 | group_dec_cache->dc_buffer.Row(y)[xend + ix] = |
760 | 0 | group_dec_cache->dc_buffer.Row(y)[xend - ix - 1]; |
761 | 0 | } |
762 | 0 | } |
763 | 0 | } |
764 | 0 | const auto& buffer = render_pipeline_input.GetBuffer(c); |
765 | 0 | Rect dst_rect = buffer.second; |
766 | 0 | ImageF* upsampling_dst = buffer.first; |
767 | 0 | JXL_ENSURE(dst_rect.IsInside(*upsampling_dst)); |
768 | | |
769 | 0 | RenderPipelineStage::RowInfo input_rows(1, std::vector<float*>(5)); |
770 | 0 | RenderPipelineStage::RowInfo output_rows(1, std::vector<float*>(8)); |
771 | 0 | for (size_t y = src_rect.y0(); y < src_rect.y0() + src_rect.ysize(); |
772 | 0 | y++) { |
773 | 0 | for (ptrdiff_t iy = 0; iy < 5; iy++) { |
774 | 0 | input_rows[0][iy] = group_dec_cache->dc_buffer.Row( |
775 | 0 | Mirror(static_cast<ptrdiff_t>(y) + iy - 2, |
776 | 0 | dec_state->shared->dc->Plane(c).ysize() >> vs) + |
777 | 0 | 2 - src_rect.y0()); |
778 | 0 | } |
779 | 0 | for (size_t iy = 0; iy < 8; iy++) { |
780 | 0 | output_rows[0][iy] = |
781 | 0 | dst_rect.Row(upsampling_dst, ((y - src_rect.y0()) << 3) + iy) - |
782 | 0 | kRenderPipelineXOffset; |
783 | 0 | } |
784 | | // Arguments set to 0/nullptr are not used. |
785 | 0 | JXL_RETURN_IF_ERROR(dec_state->upsampler8x->ProcessRow( |
786 | 0 | input_rows, output_rows, |
787 | 0 | /*xextra=*/0, src_rect.xsize(), 0, 0, thread)); |
788 | 0 | } |
789 | 0 | } |
790 | 0 | return true; |
791 | 0 | } |
792 | | |
793 | 5.10k | size_t histo_selector_bits = 0; |
794 | 5.10k | if (dc_only) { |
795 | 0 | JXL_ENSURE(num_passes == 0); |
796 | 5.10k | } else { |
797 | 5.10k | JXL_ENSURE(dec_state->shared->num_histograms > 0); |
798 | 5.10k | histo_selector_bits = CeilLog2Nonzero(dec_state->shared->num_histograms); |
799 | 5.10k | } |
800 | | |
801 | 5.10k | auto get_block = jxl::make_unique<GetBlockFromBitstream>(); |
802 | 5.10k | JXL_RETURN_IF_ERROR(get_block->Init( |
803 | 5.10k | frame_header, readers, num_passes, group_idx, histo_selector_bits, |
804 | 5.10k | dec_state->shared->frame_dim.BlockGroupRect(group_idx), group_dec_cache, |
805 | 5.10k | dec_state, first_pass)); |
806 | | |
807 | 5.10k | JXL_RETURN_IF_ERROR(HWY_DYNAMIC_DISPATCH(DecodeGroupImpl)( |
808 | 5.10k | frame_header, get_block.get(), group_dec_cache, dec_state, thread, |
809 | 5.10k | group_idx, render_pipeline_input, jpeg_data, draw)); |
810 | | |
811 | 10.1k | for (size_t pass = 0; pass < num_passes; pass++) { |
812 | 5.09k | if (!get_block->decoders[pass].CheckANSFinalState()) { |
813 | 0 | return JXL_FAILURE("ANS checksum failure."); |
814 | 0 | } |
815 | 5.09k | } |
816 | 5.09k | return true; |
817 | 5.09k | } |
818 | | |
819 | | Status DecodeGroupForRoundtrip(const FrameHeader& frame_header, |
820 | | const std::vector<std::unique_ptr<ACImage>>& ac, |
821 | | size_t group_idx, |
822 | | PassesDecoderState* JXL_RESTRICT dec_state, |
823 | | GroupDecCache* JXL_RESTRICT group_dec_cache, |
824 | | size_t thread, |
825 | | RenderPipelineInput& render_pipeline_input, |
826 | | jpeg::JPEGData* JXL_RESTRICT jpeg_data, |
827 | 0 | AuxOut* aux_out) { |
828 | 0 | JxlMemoryManager* memory_manager = dec_state->memory_manager(); |
829 | 0 | JXL_ASSIGN_OR_RETURN( |
830 | 0 | GetBlockFromEncoder get_block, |
831 | 0 | GetBlockFromEncoder::Create(ac, group_idx, frame_header.passes.shift)); |
832 | 0 | JXL_RETURN_IF_ERROR(group_dec_cache->InitOnce( |
833 | 0 | memory_manager, |
834 | 0 | /*num_passes=*/0, |
835 | 0 | /*used_acs=*/(1u << AcStrategy::kNumValidStrategies) - 1)); |
836 | | |
837 | 0 | return HWY_DYNAMIC_DISPATCH(DecodeGroupImpl)( |
838 | 0 | frame_header, &get_block, group_dec_cache, dec_state, thread, group_idx, |
839 | 0 | render_pipeline_input, jpeg_data, kDraw); |
840 | 0 | } |
841 | | |
842 | | } // namespace jxl |
843 | | #endif // HWY_ONCE |