/src/libjxl/lib/jxl/decode.cc
Line | Count | Source (jump to first uncovered line) |
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 <jxl/cms_interface.h> |
7 | | #include <jxl/codestream_header.h> |
8 | | #include <jxl/color_encoding.h> |
9 | | #include <jxl/decode.h> |
10 | | #include <jxl/jxl_export.h> |
11 | | #include <jxl/memory_manager.h> |
12 | | #include <jxl/parallel_runner.h> |
13 | | #include <jxl/types.h> |
14 | | #include <jxl/version.h> |
15 | | |
16 | | #include <algorithm> |
17 | | #include <array> |
18 | | #include <cstddef> |
19 | | #include <cstdint> |
20 | | #include <cstring> |
21 | | #include <functional> |
22 | | #include <memory> |
23 | | #include <utility> |
24 | | #include <vector> |
25 | | |
26 | | #include "lib/jxl/base/byte_order.h" |
27 | | #include "lib/jxl/base/common.h" |
28 | | #include "lib/jxl/base/compiler_specific.h" |
29 | | #include "lib/jxl/base/data_parallel.h" |
30 | | #include "lib/jxl/base/span.h" |
31 | | #include "lib/jxl/base/status.h" |
32 | | #include "lib/jxl/color_encoding_internal.h" |
33 | | #include "lib/jxl/dec_bit_reader.h" |
34 | | #include "lib/jxl/dec_cache.h" |
35 | | #include "lib/jxl/image_metadata.h" |
36 | | #include "lib/jxl/jpeg/jpeg_data.h" |
37 | | #include "lib/jxl/padded_bytes.h" |
38 | | |
39 | | // JPEGXL_ENABLE_BOXES, JPEGXL_ENABLE_TRANSCODE_JPEG |
40 | | |
41 | | #if JPEGXL_ENABLE_BOXES || JPEGXL_ENABLE_TRANSCODE_JPEG |
42 | | #include "lib/jxl/box_content_decoder.h" |
43 | | #endif |
44 | | #include "lib/jxl/dec_frame.h" |
45 | | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
46 | | #include "lib/jxl/decode_to_jpeg.h" |
47 | | #endif |
48 | | #include "lib/jxl/fields.h" |
49 | | #include "lib/jxl/frame_dimensions.h" |
50 | | #include "lib/jxl/frame_header.h" |
51 | | #include "lib/jxl/headers.h" |
52 | | #include "lib/jxl/icc_codec.h" |
53 | | #include "lib/jxl/image_bundle.h" |
54 | | #include "lib/jxl/memory_manager_internal.h" |
55 | | |
56 | | namespace { |
57 | | |
58 | | // Checks if a + b > size, taking possible integer overflow into account. |
59 | 37.3k | bool OutOfBounds(size_t a, size_t b, size_t size) { |
60 | 37.3k | size_t pos = a + b; |
61 | 37.3k | if (pos > size) return true; |
62 | 35.9k | if (pos < a) return true; // overflow happened |
63 | 35.9k | return false; |
64 | 35.9k | } |
65 | | |
66 | 14.3k | JXL_INLINE size_t InitialBasicInfoSizeHint() { |
67 | | // Amount of bytes before the start of the codestream in the container format, |
68 | | // assuming that the codestream is the first box after the signature and |
69 | | // filetype boxes. 12 bytes signature box + 20 bytes filetype box + 16 bytes |
70 | | // codestream box length + name + optional XLBox length. |
71 | 14.3k | const size_t container_header_size = 48; |
72 | | |
73 | | // Worst-case amount of bytes for basic info of the JPEG XL codestream header, |
74 | | // that is all information up to and including extra_channel_bits. Up to |
75 | | // around 2 bytes signature + 8 bytes SizeHeader + 31 bytes ColorEncoding + 4 |
76 | | // bytes rest of ImageMetadata + 5 bytes part of ImageMetadata2. |
77 | | // TODO(lode): recompute and update this value when alpha_bits is moved to |
78 | | // extra channels info. |
79 | 14.3k | const size_t max_codestream_basic_info_size = 50; |
80 | | |
81 | 14.3k | return container_header_size + max_codestream_basic_info_size; |
82 | 14.3k | } |
83 | | |
84 | | // Debug-printing failure macro similar to JXL_FAILURE, but for the status code |
85 | | // JXL_DEC_ERROR |
86 | | #if (JXL_CRASH_ON_ERROR) |
87 | | #define JXL_API_ERROR(format, ...) \ |
88 | | (::jxl::Debug(("%s:%d: " format "\n"), __FILE__, __LINE__, ##__VA_ARGS__), \ |
89 | | ::jxl::Abort(), JXL_DEC_ERROR) |
90 | | #else // JXL_CRASH_ON_ERROR |
91 | | #define JXL_API_ERROR(format, ...) \ |
92 | 12.7k | (((JXL_IS_DEBUG_BUILD) && \ |
93 | 12.7k | ::jxl::Debug(("%s:%d: " format "\n"), __FILE__, __LINE__, ##__VA_ARGS__)), \ |
94 | 12.7k | JXL_DEC_ERROR) |
95 | | #endif // JXL_CRASH_ON_ERROR |
96 | | |
97 | | // Error caused by bad input (invalid file) rather than incorrect API usage. |
98 | | // For now there is no way to distinguish these two types of errors yet. |
99 | 12.7k | #define JXL_INPUT_ERROR(format, ...) JXL_API_ERROR(format, ##__VA_ARGS__) |
100 | | |
101 | 184k | JxlDecoderStatus ConvertStatus(JxlDecoderStatus status) { return status; } |
102 | | |
103 | 82.3k | JxlDecoderStatus ConvertStatus(jxl::Status status) { |
104 | 82.3k | return status ? JXL_DEC_SUCCESS : JXL_DEC_ERROR; |
105 | 82.3k | } |
106 | | |
107 | | #define JXL_API_RETURN_IF_ERROR(expr) \ |
108 | 267k | { \ |
109 | 267k | JxlDecoderStatus status_ = ConvertStatus(expr); \ |
110 | 267k | if (status_ != JXL_DEC_SUCCESS) return status_; \ |
111 | 267k | } |
112 | | |
113 | 28.7k | JxlSignature ReadSignature(const uint8_t* buf, size_t len, size_t* pos) { |
114 | 28.7k | if (*pos >= len) return JXL_SIG_NOT_ENOUGH_BYTES; |
115 | | |
116 | 12.3k | buf += *pos; |
117 | 12.3k | len -= *pos; |
118 | | |
119 | | // JPEG XL codestream: 0xff 0x0a |
120 | 12.3k | if (len >= 1 && buf[0] == 0xff) { |
121 | 12.1k | if (len < 2) { |
122 | 0 | return JXL_SIG_NOT_ENOUGH_BYTES; |
123 | 12.1k | } else if (buf[1] == jxl::kCodestreamMarker) { |
124 | 12.1k | *pos += 2; |
125 | 12.1k | return JXL_SIG_CODESTREAM; |
126 | 12.1k | } else { |
127 | 0 | return JXL_SIG_INVALID; |
128 | 0 | } |
129 | 12.1k | } |
130 | | |
131 | | // JPEG XL container |
132 | 166 | if (len >= 1 && buf[0] == 0) { |
133 | 164 | if (len < 12) { |
134 | 0 | return JXL_SIG_NOT_ENOUGH_BYTES; |
135 | 164 | } else if (buf[1] == 0 && buf[2] == 0 && buf[3] == 0xC && buf[4] == 'J' && |
136 | 164 | buf[5] == 'X' && buf[6] == 'L' && buf[7] == ' ' && |
137 | 164 | buf[8] == 0xD && buf[9] == 0xA && buf[10] == 0x87 && |
138 | 164 | buf[11] == 0xA) { |
139 | 163 | *pos += 12; |
140 | 163 | return JXL_SIG_CONTAINER; |
141 | 163 | } else { |
142 | 1 | return JXL_SIG_INVALID; |
143 | 1 | } |
144 | 164 | } |
145 | | |
146 | 2 | return JXL_SIG_INVALID; |
147 | 166 | } |
148 | | |
149 | | } // namespace |
150 | | |
151 | 30 | uint32_t JxlDecoderVersion(void) { |
152 | 30 | return JPEGXL_MAJOR_VERSION * 1000000 + JPEGXL_MINOR_VERSION * 1000 + |
153 | 30 | JPEGXL_PATCH_VERSION; |
154 | 30 | } |
155 | | |
156 | 28.7k | JxlSignature JxlSignatureCheck(const uint8_t* buf, size_t len) { |
157 | 28.7k | size_t pos = 0; |
158 | 28.7k | return ReadSignature(buf, len, &pos); |
159 | 28.7k | } |
160 | | |
161 | | namespace { |
162 | | |
163 | 12.5k | size_t BitsPerChannel(JxlDataType data_type) { |
164 | 12.5k | switch (data_type) { |
165 | 11.2k | case JXL_TYPE_UINT8: |
166 | 11.2k | return 8; |
167 | 1.26k | case JXL_TYPE_UINT16: |
168 | 1.26k | return 16; |
169 | 43 | case JXL_TYPE_FLOAT: |
170 | 43 | return 32; |
171 | 0 | case JXL_TYPE_FLOAT16: |
172 | 0 | return 16; |
173 | 0 | default: |
174 | 0 | return 0; // signals unhandled JxlDataType |
175 | 12.5k | } |
176 | 12.5k | } |
177 | | |
178 | | template <typename T> |
179 | | uint32_t GetBitDepth(JxlBitDepth bit_depth, const T& metadata, |
180 | 4.39k | JxlPixelFormat format) { |
181 | 4.39k | if (bit_depth.type == JXL_BIT_DEPTH_FROM_PIXEL_FORMAT) { |
182 | 4.39k | return BitsPerChannel(format.data_type); |
183 | 4.39k | } else if (bit_depth.type == JXL_BIT_DEPTH_FROM_CODESTREAM) { |
184 | 0 | return metadata.bit_depth.bits_per_sample; |
185 | 0 | } else if (bit_depth.type == JXL_BIT_DEPTH_CUSTOM) { |
186 | 0 | return bit_depth.bits_per_sample; |
187 | 0 | } |
188 | 0 | return 0; |
189 | 4.39k | } decode.cc:unsigned int (anonymous namespace)::GetBitDepth<jxl::ImageMetadata>(JxlBitDepth, jxl::ImageMetadata const&, JxlPixelFormat) Line | Count | Source | 180 | 4.39k | JxlPixelFormat format) { | 181 | 4.39k | if (bit_depth.type == JXL_BIT_DEPTH_FROM_PIXEL_FORMAT) { | 182 | 4.39k | return BitsPerChannel(format.data_type); | 183 | 4.39k | } else if (bit_depth.type == JXL_BIT_DEPTH_FROM_CODESTREAM) { | 184 | 0 | return metadata.bit_depth.bits_per_sample; | 185 | 0 | } else if (bit_depth.type == JXL_BIT_DEPTH_CUSTOM) { | 186 | 0 | return bit_depth.bits_per_sample; | 187 | 0 | } | 188 | 0 | return 0; | 189 | 4.39k | } |
Unexecuted instantiation: decode.cc:unsigned int (anonymous namespace)::GetBitDepth<jxl::ExtraChannelInfo>(JxlBitDepth, jxl::ExtraChannelInfo const&, JxlPixelFormat) |
190 | | |
191 | | enum class DecoderStage : uint32_t { |
192 | | kInited, // Decoder created, no JxlDecoderProcessInput called yet |
193 | | kStarted, // Running JxlDecoderProcessInput calls |
194 | | kCodestreamFinished, // Codestream done, but other boxes could still occur. |
195 | | // This stage can also occur before having seen the |
196 | | // entire codestream if the user didn't subscribe to any |
197 | | // codestream events at all, e.g. only to box events, |
198 | | // or, the user only subscribed to basic info, and only |
199 | | // the header of the codestream was parsed. |
200 | | kError, // Error occurred, decoder object no longer usable |
201 | | }; |
202 | | |
203 | | enum class FrameStage : uint32_t { |
204 | | kHeader, // Must parse frame header. |
205 | | kTOC, // Must parse TOC |
206 | | kFull, // Must parse full pixels |
207 | | }; |
208 | | |
209 | | enum class BoxStage : uint32_t { |
210 | | kHeader, // Parsing box header of the next box, or start of non-container |
211 | | // stream |
212 | | kFtyp, // The ftyp box |
213 | | kSkip, // Box whose contents are skipped |
214 | | kCodestream, // Handling codestream box contents, or non-container stream |
215 | | kPartialCodestream, // Handling the extra header of partial codestream box |
216 | | kJpegRecon, // Handling jpeg reconstruction box |
217 | | }; |
218 | | |
219 | | enum class JpegReconStage : uint32_t { |
220 | | kNone, // Not outputting |
221 | | kSettingMetadata, // Ready to output, must set metadata to the jpeg_data |
222 | | kOutputting, // Currently outputting the JPEG bytes |
223 | | }; |
224 | | |
225 | | // For each internal frame, which storage locations it references, and which |
226 | | // storage locations it is stored in, using the bit mask as defined in |
227 | | // FrameDecoder::References and FrameDecoder::SaveAs. |
228 | | struct FrameRef { |
229 | | int reference; |
230 | | int saved_as; |
231 | | }; |
232 | | |
233 | | /* |
234 | | Given list of frame references to storage slots, and storage slots in which this |
235 | | frame is saved, computes which frames are required to decode the frame at the |
236 | | given index and any frames after it. The frames on which this depends are |
237 | | returned as a vector of their indices, in no particular order. The given index |
238 | | must be smaller than saved_as.size(), and references.size() must equal |
239 | | saved_as.size(). Any frames beyond saved_as and references are considered |
240 | | unknown future frames and must be treated as if something depends on them. |
241 | | */ |
242 | | std::vector<size_t> GetFrameDependencies(size_t index, |
243 | 0 | const std::vector<FrameRef>& refs) { |
244 | 0 | JXL_DASSERT(index < refs.size()); |
245 | |
|
246 | 0 | std::vector<size_t> result; |
247 | |
|
248 | 0 | constexpr size_t kNumStorage = 8; |
249 | | |
250 | | // value which indicates nothing is stored in this storage slot |
251 | 0 | const size_t invalid = refs.size(); |
252 | | // for each of the 8 storage slots, a vector that translates frame index to |
253 | | // frame stored in this storage slot at this point, that is, the last |
254 | | // frame that was stored in this slot before or at this index. |
255 | 0 | std::array<std::vector<size_t>, kNumStorage> storage; |
256 | 0 | for (size_t s = 0; s < kNumStorage; ++s) { |
257 | 0 | storage[s].resize(refs.size()); |
258 | 0 | int mask = 1 << s; |
259 | 0 | size_t id = invalid; |
260 | 0 | for (size_t i = 0; i < refs.size(); ++i) { |
261 | 0 | if (refs[i].saved_as & mask) { |
262 | 0 | id = i; |
263 | 0 | } |
264 | 0 | storage[s][i] = id; |
265 | 0 | } |
266 | 0 | } |
267 | |
|
268 | 0 | std::vector<char> seen(index + 1, 0); |
269 | 0 | std::vector<size_t> stack; |
270 | 0 | stack.push_back(index); |
271 | 0 | seen[index] = 1; |
272 | | |
273 | | // For frames after index, assume they can depend on any of the 8 storage |
274 | | // slots, so push the frame for each stored reference to the stack and result. |
275 | | // All frames after index are treated as having unknown references and with |
276 | | // the possibility that there are more frames after the last known. |
277 | | // TODO(lode): take values of saved_as and references after index, and a |
278 | | // input flag indicating if they are all frames of the image, to further |
279 | | // optimize this. |
280 | 0 | for (size_t s = 0; s < kNumStorage; ++s) { |
281 | 0 | size_t frame_ref = storage[s][index]; |
282 | 0 | if (frame_ref == invalid) continue; |
283 | 0 | if (seen[frame_ref]) continue; |
284 | 0 | stack.push_back(frame_ref); |
285 | 0 | seen[frame_ref] = 1; |
286 | 0 | result.push_back(frame_ref); |
287 | 0 | } |
288 | |
|
289 | 0 | while (!stack.empty()) { |
290 | 0 | size_t frame_index = stack.back(); |
291 | 0 | stack.pop_back(); |
292 | 0 | if (frame_index == 0) continue; // first frame cannot have references |
293 | 0 | for (size_t s = 0; s < kNumStorage; ++s) { |
294 | 0 | int mask = 1 << s; |
295 | 0 | if (!(refs[frame_index].reference & mask)) continue; |
296 | 0 | size_t frame_ref = storage[s][frame_index - 1]; |
297 | 0 | if (frame_ref == invalid) continue; |
298 | 0 | if (seen[frame_ref]) continue; |
299 | 0 | stack.push_back(frame_ref); |
300 | 0 | seen[frame_ref] = 1; |
301 | 0 | result.push_back(frame_ref); |
302 | 0 | } |
303 | 0 | } |
304 | |
|
305 | 0 | return result; |
306 | 0 | } |
307 | | |
308 | | // Parameters for user-requested extra channel output. |
309 | | struct ExtraChannelOutput { |
310 | | JxlPixelFormat format; |
311 | | void* buffer; |
312 | | size_t buffer_size; |
313 | | }; |
314 | | |
315 | | } // namespace |
316 | | |
317 | | namespace jxl { |
318 | | |
319 | | struct JxlDecoderFrameIndexBoxEntry { |
320 | | // OFFi: offset of start byte of this frame compared to start |
321 | | // byte of previous frame from this index in the JPEG XL codestream. For the |
322 | | // first frame, this is the offset from the first byte of the JPEG XL |
323 | | // codestream. |
324 | | uint64_t OFFi; |
325 | | // Ti: duration in ticks between the start of this frame and |
326 | | // the start of the next frame in the index. If this is the last frame in the |
327 | | // index, this is the duration in ticks between the start of this frame and |
328 | | // the end of the stream. A tick lasts TNUM / TDEN seconds. |
329 | | uint32_t Ti; |
330 | | // Fi: amount of frames the next frame in the index occurs |
331 | | // after this frame. If this is the last frame in the index, this is the |
332 | | // amount of frames after this frame in the remainder of the stream. Only |
333 | | // frames that are presented by the decoder are counted for this purpose, this |
334 | | // excludes frames that are not intended for display but for compositing with |
335 | | // other frames, such as frames that aren't the last frame with a duration of |
336 | | // 0 ticks. |
337 | | uint32_t Fi; |
338 | | }; |
339 | | |
340 | | struct JxlDecoderFrameIndexBox { |
341 | 0 | int64_t NF() const { return entries.size(); } |
342 | | int32_t TNUM = 1; |
343 | | int32_t TDEN = 1000; |
344 | | |
345 | | std::vector<JxlDecoderFrameIndexBoxEntry> entries; |
346 | | |
347 | | // That way we can ensure that every index box will have the first frame. |
348 | | // If the API user decides to mark it as an indexed frame, we call |
349 | | // the AddFrame again, this time with requested. |
350 | 0 | void AddFrame(uint64_t OFFi, uint32_t Ti, uint32_t Fi) { |
351 | 0 | JxlDecoderFrameIndexBoxEntry e; |
352 | 0 | e.OFFi = OFFi; |
353 | 0 | e.Ti = Ti; |
354 | 0 | e.Fi = Fi; |
355 | 0 | entries.push_back(e); |
356 | 0 | } |
357 | | }; |
358 | | |
359 | | } // namespace jxl |
360 | | |
361 | | // NOLINTNEXTLINE(clang-analyzer-optin.performance.Padding) |
362 | | struct JxlDecoder { |
363 | 14.3k | JxlDecoder() = default; |
364 | | |
365 | | JxlMemoryManager memory_manager; |
366 | | std::unique_ptr<jxl::ThreadPool> thread_pool; |
367 | | |
368 | | DecoderStage stage; |
369 | | |
370 | | // Status of progression, internal. |
371 | | bool got_signature; |
372 | | // Indicates we know that we've seen the last codestream box: either this |
373 | | // was a jxlc box, or a jxlp box that has its index indicated as last by |
374 | | // having its most significant bit set, or no boxes are used at all. This |
375 | | // does not indicate the full codestream has already been seen, only the |
376 | | // last box of it has been initiated. |
377 | | bool last_codestream_seen; |
378 | | bool got_codestream_signature; |
379 | | bool got_basic_info; |
380 | | bool got_transform_data; // To skip everything before ICC. |
381 | | bool got_all_headers; // Codestream metadata headers. |
382 | | bool post_headers; // Already decoding pixels. |
383 | | std::unique_ptr<jxl::ICCReader> icc_reader; |
384 | | jxl::JxlDecoderFrameIndexBox frame_index_box; |
385 | | // This means either we actually got the preview image, or determined we |
386 | | // cannot get it or there is none. |
387 | | bool got_preview_image; |
388 | | bool preview_frame; |
389 | | |
390 | | // Position of next_in in the original file including box format if present |
391 | | // (as opposed to position in the codestream) |
392 | | size_t file_pos; |
393 | | |
394 | | size_t box_contents_begin; |
395 | | size_t box_contents_end; |
396 | | size_t box_contents_size; |
397 | | size_t box_size; |
398 | | size_t header_size; |
399 | | // Either a final box that runs until EOF, or the case of no container format |
400 | | // at all. |
401 | | bool box_contents_unbounded; |
402 | | |
403 | | JxlBoxType box_type; |
404 | | JxlBoxType box_decoded_type; // Underlying type for brob boxes |
405 | | // Set to true right after a JXL_DEC_BOX event only. |
406 | | bool box_event; |
407 | | bool decompress_boxes; |
408 | | |
409 | | bool box_out_buffer_set; |
410 | | // Whether the out buffer is set for the current box, if the user did not yet |
411 | | // release the buffer while the next box is encountered, this will be set to |
412 | | // false. If this is false, no JXL_DEC_NEED_MORE_INPUT is emitted |
413 | | // (irrespective of the value of box_out_buffer_set), because not setting |
414 | | // output indicates the user does not wish the data of this box. |
415 | | bool box_out_buffer_set_current_box; |
416 | | uint8_t* box_out_buffer; |
417 | | size_t box_out_buffer_size; |
418 | | // which byte of the full box content the start of the out buffer points to |
419 | | size_t box_out_buffer_begin; |
420 | | // which byte of box_out_buffer to write to next |
421 | | size_t box_out_buffer_pos; |
422 | | |
423 | | // Settings |
424 | | bool keep_orientation; |
425 | | bool unpremul_alpha; |
426 | | bool render_spotcolors; |
427 | | bool coalescing; |
428 | | float desired_intensity_target; |
429 | | |
430 | | // Bitfield, for which informative events (JXL_DEC_BASIC_INFO, etc...) the |
431 | | // decoder returns a status. By default, do not return for any of the events, |
432 | | // only return when the decoder cannot continue because it needs more input or |
433 | | // output data. |
434 | | int events_wanted; |
435 | | int orig_events_wanted; |
436 | | |
437 | | // Fields for reading the basic info from the header. |
438 | | size_t basic_info_size_hint; |
439 | | bool have_container; |
440 | | size_t box_count; |
441 | | |
442 | | // The level of progressive detail in frame decoding. |
443 | | JxlProgressiveDetail prog_detail = kDC; |
444 | | // The progressive detail of the current frame. |
445 | | JxlProgressiveDetail frame_prog_detail; |
446 | | // The intended downsampling ratio for the current progression step. |
447 | | size_t downsampling_target; |
448 | | |
449 | | // Set to true if either an image out buffer or an image out callback was set. |
450 | | bool image_out_buffer_set; |
451 | | |
452 | | // Owned by the caller, buffer for preview or full resolution image. |
453 | | void* image_out_buffer; |
454 | | JxlImageOutInitCallback image_out_init_callback; |
455 | | JxlImageOutRunCallback image_out_run_callback; |
456 | | JxlImageOutDestroyCallback image_out_destroy_callback; |
457 | | void* image_out_init_opaque; |
458 | | struct SimpleImageOutCallback { |
459 | | JxlImageOutCallback callback; |
460 | | void* opaque; |
461 | | }; |
462 | | SimpleImageOutCallback simple_image_out_callback; |
463 | | |
464 | | size_t image_out_size; |
465 | | |
466 | | JxlPixelFormat image_out_format; |
467 | | JxlBitDepth image_out_bit_depth; |
468 | | |
469 | | // For extra channels. Empty if no extra channels are requested, and they are |
470 | | // reset each frame |
471 | | std::vector<ExtraChannelOutput> extra_channel_output; |
472 | | |
473 | | jxl::CodecMetadata metadata; |
474 | | // Same as metadata.m, except for the color_encoding, which is set to the |
475 | | // output encoding. |
476 | | jxl::ImageMetadata image_metadata; |
477 | | std::unique_ptr<jxl::ImageBundle> ib; |
478 | | |
479 | | std::unique_ptr<jxl::PassesDecoderState> passes_state; |
480 | | std::unique_ptr<jxl::FrameDecoder> frame_dec; |
481 | | size_t next_section; |
482 | | std::vector<char> section_processed; |
483 | | |
484 | | // headers and TOC for the current frame. When got_toc is true, this is |
485 | | // always the frame header of the last frame of the current still series, |
486 | | // that is, the displayed frame. |
487 | | std::unique_ptr<jxl::FrameHeader> frame_header; |
488 | | |
489 | | size_t remaining_frame_size; |
490 | | FrameStage frame_stage; |
491 | | bool dc_frame_progression_done; |
492 | | // The currently processed frame is the last of the current composite still, |
493 | | // and so must be returned as pixels |
494 | | bool is_last_of_still; |
495 | | // The currently processed frame is the last of the codestream |
496 | | bool is_last_total; |
497 | | // How many frames to skip. |
498 | | size_t skip_frames; |
499 | | // Skipping the current frame. May be false if skip_frames was just set to |
500 | | // a positive value while already processing a current frame, then |
501 | | // skipping_frame will be enabled only for the next frame. |
502 | | bool skipping_frame; |
503 | | |
504 | | // Amount of internal frames and external frames started. External frames are |
505 | | // user-visible frames, internal frames includes all external frames and |
506 | | // also invisible frames such as patches, blending-only and dc_level frames. |
507 | | size_t internal_frames; |
508 | | size_t external_frames; |
509 | | |
510 | | std::vector<FrameRef> frame_refs; |
511 | | |
512 | | // Translates external frame index to internal frame index. The external |
513 | | // index is the index of user-visible frames. The internal index can be larger |
514 | | // since non-visible frames (such as frames with patches, ...) are included. |
515 | | std::vector<size_t> frame_external_to_internal; |
516 | | |
517 | | // Whether the frame with internal index is required to decode the frame |
518 | | // being skipped to or any frames after that. If no skipping is active, |
519 | | // this vector is ignored. If the current internal frame index is beyond this |
520 | | // vector, it must be treated as a required frame. |
521 | | std::vector<char> frame_required; |
522 | | |
523 | | // Codestream input data is copied here temporarily when the decoder needs |
524 | | // more input bytes to process the next part of the stream. We copy the input |
525 | | // data in order to be able to release it all through the API it when |
526 | | // returning JXL_DEC_NEED_MORE_INPUT. |
527 | | std::vector<uint8_t> codestream_copy; |
528 | | // Number of bytes at the end of codestream_copy that were not yet consumed |
529 | | // by calling AdvanceInput(). |
530 | | size_t codestream_unconsumed; |
531 | | // Position in the codestream_copy vector that the decoder already finished |
532 | | // processing. It can be greater than the current size of codestream_copy in |
533 | | // case where the decoder skips some parts of the frame that were not yet |
534 | | // provided. |
535 | | size_t codestream_pos; |
536 | | // Number of bits after codestream_pos that were already processed. |
537 | | size_t codestream_bits_ahead; |
538 | | |
539 | | BoxStage box_stage; |
540 | | |
541 | | #if JPEGXL_ENABLE_BOXES |
542 | | jxl::JxlBoxContentDecoder box_content_decoder; |
543 | | #endif |
544 | | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
545 | | jxl::JxlToJpegDecoder jpeg_decoder; |
546 | | // Decodes Exif or XMP metadata for JPEG reconstruction |
547 | | jxl::JxlBoxContentDecoder metadata_decoder; |
548 | | std::vector<uint8_t> exif_metadata; |
549 | | std::vector<uint8_t> xmp_metadata; |
550 | | // must store JPEG reconstruction metadata from the current box |
551 | | // 0 = not stored, 1 = currently storing, 2 = finished |
552 | | int store_exif; |
553 | | int store_xmp; |
554 | | size_t recon_out_buffer_pos; |
555 | | size_t recon_exif_size; // Expected exif size as read from the jbrd box |
556 | | size_t recon_xmp_size; // Expected exif size as read from the jbrd box |
557 | | JpegReconStage recon_output_jpeg; |
558 | | |
559 | 962 | bool JbrdNeedMoreBoxes() const { |
560 | | // jbrd box wants exif but exif box not yet seen |
561 | 962 | if (store_exif < 2 && recon_exif_size > 0) return true; |
562 | | // jbrd box wants xmp but xmp box not yet seen |
563 | 962 | if (store_xmp < 2 && recon_xmp_size > 0) return true; |
564 | 962 | return false; |
565 | 962 | } |
566 | | #endif |
567 | | |
568 | | const uint8_t* next_in; |
569 | | size_t avail_in; |
570 | | bool input_closed; |
571 | | |
572 | 175k | void AdvanceInput(size_t size) { |
573 | 175k | JXL_DASSERT(avail_in >= size); |
574 | 175k | next_in += size; |
575 | 175k | avail_in -= size; |
576 | 175k | file_pos += size; |
577 | 175k | } |
578 | | |
579 | 243k | size_t AvailableCodestream() const { |
580 | 243k | size_t avail_codestream = avail_in; |
581 | 243k | if (!box_contents_unbounded) { |
582 | 872 | avail_codestream = |
583 | 872 | std::min<size_t>(avail_codestream, box_contents_end - file_pos); |
584 | 872 | } |
585 | 243k | return avail_codestream; |
586 | 243k | } |
587 | | |
588 | 117k | void AdvanceCodestream(size_t size) { |
589 | 117k | size_t avail_codestream = AvailableCodestream(); |
590 | 117k | if (codestream_copy.empty()) { |
591 | 116k | if (size <= avail_codestream) { |
592 | 116k | AdvanceInput(size); |
593 | 116k | } else { |
594 | 112 | codestream_pos = size - avail_codestream; |
595 | 112 | AdvanceInput(avail_codestream); |
596 | 112 | } |
597 | 116k | } else { |
598 | 548 | codestream_pos += size; |
599 | 548 | if (codestream_pos + codestream_unconsumed >= codestream_copy.size()) { |
600 | 7 | size_t advance = std::min( |
601 | 7 | codestream_unconsumed, |
602 | 7 | codestream_unconsumed + codestream_pos - codestream_copy.size()); |
603 | 7 | AdvanceInput(advance); |
604 | 7 | codestream_pos -= std::min(codestream_pos, codestream_copy.size()); |
605 | 7 | codestream_unconsumed = 0; |
606 | 7 | codestream_copy.clear(); |
607 | 7 | } |
608 | 548 | } |
609 | 117k | } |
610 | | |
611 | 10.4k | JxlDecoderStatus RequestMoreInput() { |
612 | 10.4k | if (codestream_copy.empty()) { |
613 | 5.83k | size_t avail_codestream = AvailableCodestream(); |
614 | 5.83k | codestream_copy.insert(codestream_copy.end(), next_in, |
615 | 5.83k | next_in + avail_codestream); |
616 | 5.83k | AdvanceInput(avail_codestream); |
617 | 5.83k | } else { |
618 | 4.62k | AdvanceInput(codestream_unconsumed); |
619 | 4.62k | codestream_unconsumed = 0; |
620 | 4.62k | } |
621 | 10.4k | return JXL_DEC_NEED_MORE_INPUT; |
622 | 10.4k | } |
623 | | |
624 | 120k | JxlDecoderStatus GetCodestreamInput(jxl::Span<const uint8_t>* span) { |
625 | 120k | if (codestream_copy.empty() && codestream_pos > 0) { |
626 | 224 | size_t avail_codestream = AvailableCodestream(); |
627 | 224 | size_t skip = std::min<size_t>(codestream_pos, avail_codestream); |
628 | 224 | AdvanceInput(skip); |
629 | 224 | codestream_pos -= skip; |
630 | 224 | if (codestream_pos > 0) { |
631 | 224 | return RequestMoreInput(); |
632 | 224 | } |
633 | 224 | } |
634 | 120k | if (codestream_pos > codestream_copy.size()) { |
635 | 0 | return JXL_API_ERROR("Internal: codestream_pos > codestream_copy.size()"); |
636 | 0 | } |
637 | 120k | if (codestream_unconsumed > codestream_copy.size()) { |
638 | 0 | return JXL_API_ERROR( |
639 | 0 | "Internal: codestream_unconsumed > codestream_copy.size()"); |
640 | 0 | } |
641 | 120k | size_t avail_codestream = AvailableCodestream(); |
642 | 120k | if (codestream_copy.empty()) { |
643 | 115k | if (avail_codestream == 0) { |
644 | 1.02k | return RequestMoreInput(); |
645 | 1.02k | } |
646 | 114k | *span = jxl::Bytes(next_in, avail_codestream); |
647 | 114k | return JXL_DEC_SUCCESS; |
648 | 115k | } else { |
649 | 4.63k | codestream_copy.insert(codestream_copy.end(), |
650 | 4.63k | next_in + codestream_unconsumed, |
651 | 4.63k | next_in + avail_codestream); |
652 | 4.63k | codestream_unconsumed = avail_codestream; |
653 | 4.63k | *span = jxl::Bytes(codestream_copy.data() + codestream_pos, |
654 | 4.63k | codestream_copy.size() - codestream_pos); |
655 | 4.63k | return JXL_DEC_SUCCESS; |
656 | 4.63k | } |
657 | 120k | } |
658 | | |
659 | | // Whether the decoder can use more codestream input for a purpose it needs. |
660 | | // This returns false if the user didn't subscribe to any events that |
661 | | // require the codestream (e.g. only subscribed to metadata boxes), or all |
662 | | // parts of the codestream that are subscribed to (e.g. only basic info) have |
663 | | // already occurred. |
664 | 467 | bool CanUseMoreCodestreamInput() const { |
665 | | // The decoder can set this to finished early if all relevant events were |
666 | | // processed, so this check works. |
667 | 467 | return stage != DecoderStage::kCodestreamFinished; |
668 | 467 | } |
669 | | }; |
670 | | |
671 | | namespace { |
672 | | |
673 | 49.9k | bool CheckSizeLimit(JxlDecoder* dec, size_t xsize, size_t ysize) { |
674 | 49.9k | if (xsize == 0 || ysize == 0) return true; |
675 | 49.9k | size_t padded_xsize = jxl::DivCeil(xsize, 32) * 32; |
676 | 49.9k | if (padded_xsize < xsize) return false; // overflow |
677 | 49.9k | size_t num_pixels = padded_xsize * ysize; |
678 | 49.9k | if (num_pixels / padded_xsize != ysize) return false; // overflow |
679 | 49.9k | return true; |
680 | 49.9k | } |
681 | | |
682 | | } // namespace |
683 | | |
684 | | // Resets the state that must be reset for both Rewind and Reset |
685 | 14.3k | void JxlDecoderRewindDecodingState(JxlDecoder* dec) { |
686 | 14.3k | dec->stage = DecoderStage::kInited; |
687 | 14.3k | dec->got_signature = false; |
688 | 14.3k | dec->last_codestream_seen = false; |
689 | 14.3k | dec->got_codestream_signature = false; |
690 | 14.3k | dec->got_basic_info = false; |
691 | 14.3k | dec->got_transform_data = false; |
692 | 14.3k | dec->got_all_headers = false; |
693 | 14.3k | dec->post_headers = false; |
694 | 14.3k | if (dec->icc_reader) dec->icc_reader->Reset(); |
695 | 14.3k | dec->got_preview_image = false; |
696 | 14.3k | dec->preview_frame = false; |
697 | 14.3k | dec->file_pos = 0; |
698 | 14.3k | dec->box_contents_begin = 0; |
699 | 14.3k | dec->box_contents_end = 0; |
700 | 14.3k | dec->box_contents_size = 0; |
701 | 14.3k | dec->box_size = 0; |
702 | 14.3k | dec->header_size = 0; |
703 | 14.3k | dec->box_contents_unbounded = false; |
704 | 14.3k | memset(dec->box_type, 0, sizeof(dec->box_type)); |
705 | 14.3k | memset(dec->box_decoded_type, 0, sizeof(dec->box_decoded_type)); |
706 | 14.3k | dec->box_event = false; |
707 | 14.3k | dec->box_stage = BoxStage::kHeader; |
708 | 14.3k | dec->box_out_buffer_set = false; |
709 | 14.3k | dec->box_out_buffer_set_current_box = false; |
710 | 14.3k | dec->box_out_buffer = nullptr; |
711 | 14.3k | dec->box_out_buffer_size = 0; |
712 | 14.3k | dec->box_out_buffer_begin = 0; |
713 | 14.3k | dec->box_out_buffer_pos = 0; |
714 | | |
715 | 14.3k | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
716 | 14.3k | dec->exif_metadata.clear(); |
717 | 14.3k | dec->xmp_metadata.clear(); |
718 | 14.3k | dec->store_exif = 0; |
719 | 14.3k | dec->store_xmp = 0; |
720 | 14.3k | dec->recon_out_buffer_pos = 0; |
721 | 14.3k | dec->recon_exif_size = 0; |
722 | 14.3k | dec->recon_xmp_size = 0; |
723 | 14.3k | dec->recon_output_jpeg = JpegReconStage::kNone; |
724 | 14.3k | #endif |
725 | | |
726 | 14.3k | dec->events_wanted = dec->orig_events_wanted; |
727 | 14.3k | dec->basic_info_size_hint = InitialBasicInfoSizeHint(); |
728 | 14.3k | dec->have_container = false; |
729 | 14.3k | dec->box_count = 0; |
730 | 14.3k | dec->downsampling_target = 8; |
731 | 14.3k | dec->image_out_buffer_set = false; |
732 | 14.3k | dec->image_out_buffer = nullptr; |
733 | 14.3k | dec->image_out_init_callback = nullptr; |
734 | 14.3k | dec->image_out_run_callback = nullptr; |
735 | 14.3k | dec->image_out_destroy_callback = nullptr; |
736 | 14.3k | dec->image_out_init_opaque = nullptr; |
737 | 14.3k | dec->image_out_size = 0; |
738 | 14.3k | dec->image_out_bit_depth.type = JXL_BIT_DEPTH_FROM_PIXEL_FORMAT; |
739 | 14.3k | dec->extra_channel_output.clear(); |
740 | 14.3k | dec->next_in = nullptr; |
741 | 14.3k | dec->avail_in = 0; |
742 | 14.3k | dec->input_closed = false; |
743 | | |
744 | 14.3k | dec->passes_state.reset(); |
745 | 14.3k | dec->frame_dec.reset(); |
746 | 14.3k | dec->next_section = 0; |
747 | 14.3k | dec->section_processed.clear(); |
748 | | |
749 | 14.3k | dec->ib.reset(); |
750 | 14.3k | dec->metadata = jxl::CodecMetadata(); |
751 | 14.3k | dec->image_metadata = dec->metadata.m; |
752 | 14.3k | dec->frame_header = jxl::make_unique<jxl::FrameHeader>(&dec->metadata); |
753 | | |
754 | 14.3k | dec->codestream_copy.clear(); |
755 | 14.3k | dec->codestream_unconsumed = 0; |
756 | 14.3k | dec->codestream_pos = 0; |
757 | 14.3k | dec->codestream_bits_ahead = 0; |
758 | | |
759 | 14.3k | dec->frame_stage = FrameStage::kHeader; |
760 | 14.3k | dec->remaining_frame_size = 0; |
761 | 14.3k | dec->is_last_of_still = false; |
762 | 14.3k | dec->is_last_total = false; |
763 | 14.3k | dec->skip_frames = 0; |
764 | 14.3k | dec->skipping_frame = false; |
765 | 14.3k | dec->internal_frames = 0; |
766 | 14.3k | dec->external_frames = 0; |
767 | 14.3k | } |
768 | | |
769 | 14.3k | void JxlDecoderReset(JxlDecoder* dec) { |
770 | 14.3k | JxlDecoderRewindDecodingState(dec); |
771 | | |
772 | 14.3k | dec->thread_pool.reset(); |
773 | 14.3k | dec->keep_orientation = false; |
774 | 14.3k | dec->unpremul_alpha = false; |
775 | 14.3k | dec->render_spotcolors = true; |
776 | 14.3k | dec->coalescing = true; |
777 | 14.3k | dec->desired_intensity_target = 0; |
778 | 14.3k | dec->orig_events_wanted = 0; |
779 | 14.3k | dec->events_wanted = 0; |
780 | 14.3k | dec->frame_refs.clear(); |
781 | 14.3k | dec->frame_external_to_internal.clear(); |
782 | 14.3k | dec->frame_required.clear(); |
783 | 14.3k | dec->decompress_boxes = false; |
784 | 14.3k | } |
785 | | |
786 | 14.3k | JxlDecoder* JxlDecoderCreate(const JxlMemoryManager* memory_manager) { |
787 | 14.3k | JxlMemoryManager local_memory_manager; |
788 | 14.3k | if (!jxl::MemoryManagerInit(&local_memory_manager, memory_manager)) |
789 | 0 | return nullptr; |
790 | | |
791 | 14.3k | void* alloc = |
792 | 14.3k | jxl::MemoryManagerAlloc(&local_memory_manager, sizeof(JxlDecoder)); |
793 | 14.3k | if (!alloc) return nullptr; |
794 | | // Placement new constructor on allocated memory |
795 | 14.3k | JxlDecoder* dec = new (alloc) JxlDecoder(); |
796 | 14.3k | dec->memory_manager = local_memory_manager; |
797 | | |
798 | 14.3k | JxlDecoderReset(dec); |
799 | | |
800 | 14.3k | return dec; |
801 | 14.3k | } |
802 | | |
803 | 14.3k | void JxlDecoderDestroy(JxlDecoder* dec) { |
804 | 14.3k | if (dec) { |
805 | 14.3k | JxlMemoryManager local_memory_manager = dec->memory_manager; |
806 | | // Call destructor directly since custom free function is used. |
807 | 14.3k | dec->~JxlDecoder(); |
808 | 14.3k | jxl::MemoryManagerFree(&local_memory_manager, dec); |
809 | 14.3k | } |
810 | 14.3k | } |
811 | | |
812 | 0 | void JxlDecoderRewind(JxlDecoder* dec) { JxlDecoderRewindDecodingState(dec); } |
813 | | |
814 | 0 | void JxlDecoderSkipFrames(JxlDecoder* dec, size_t amount) { |
815 | | // Increment amount, rather than set it: making the amount smaller is |
816 | | // impossible because the decoder may already have skipped frames required to |
817 | | // decode earlier frames, and making the amount larger compared to an existing |
818 | | // amount is impossible because if JxlDecoderSkipFrames is called in the |
819 | | // middle of already skipping frames, the user cannot know how many frames |
820 | | // have already been skipped internally so far so an absolute value cannot |
821 | | // be defined. |
822 | 0 | dec->skip_frames += amount; |
823 | |
|
824 | 0 | dec->frame_required.clear(); |
825 | 0 | size_t next_frame = dec->external_frames + dec->skip_frames; |
826 | | |
827 | | // A frame that has been seen before a rewind |
828 | 0 | if (next_frame < dec->frame_external_to_internal.size()) { |
829 | 0 | size_t internal_index = dec->frame_external_to_internal[next_frame]; |
830 | 0 | if (internal_index < dec->frame_refs.size()) { |
831 | 0 | std::vector<size_t> deps = |
832 | 0 | GetFrameDependencies(internal_index, dec->frame_refs); |
833 | |
|
834 | 0 | dec->frame_required.resize(internal_index + 1, 0); |
835 | 0 | for (size_t idx : deps) { |
836 | 0 | if (idx < dec->frame_required.size()) { |
837 | 0 | dec->frame_required[idx] = 1; |
838 | 0 | } else { |
839 | 0 | JXL_DEBUG_ABORT("Unreachable"); |
840 | 0 | } |
841 | 0 | } |
842 | 0 | } |
843 | 0 | } |
844 | 0 | } |
845 | | |
846 | 0 | JxlDecoderStatus JxlDecoderSkipCurrentFrame(JxlDecoder* dec) { |
847 | 0 | if (dec->frame_stage != FrameStage::kFull) { |
848 | 0 | return JXL_API_ERROR("JxlDecoderSkipCurrentFrame called at the wrong time"); |
849 | 0 | } |
850 | 0 | JXL_DASSERT(dec->frame_dec); |
851 | 0 | dec->frame_stage = FrameStage::kHeader; |
852 | 0 | dec->AdvanceCodestream(dec->remaining_frame_size); |
853 | 0 | if (dec->is_last_of_still) { |
854 | 0 | dec->image_out_buffer_set = false; |
855 | 0 | } |
856 | 0 | return JXL_DEC_SUCCESS; |
857 | 0 | } |
858 | | |
859 | | JXL_EXPORT JxlDecoderStatus |
860 | | JxlDecoderSetParallelRunner(JxlDecoder* dec, JxlParallelRunner parallel_runner, |
861 | 11.5k | void* parallel_runner_opaque) { |
862 | 11.5k | if (dec->stage != DecoderStage::kInited) { |
863 | 0 | return JXL_API_ERROR( |
864 | 0 | "JxlDecoderSetParallelRunner must be called before starting"); |
865 | 0 | } |
866 | 11.5k | dec->thread_pool = jxl::make_unique<jxl::ThreadPool>(parallel_runner, |
867 | 11.5k | parallel_runner_opaque); |
868 | 11.5k | return JXL_DEC_SUCCESS; |
869 | 11.5k | } |
870 | | |
871 | 0 | size_t JxlDecoderSizeHintBasicInfo(const JxlDecoder* dec) { |
872 | 0 | if (dec->got_basic_info) return 0; |
873 | 0 | return dec->basic_info_size_hint; |
874 | 0 | } |
875 | | |
876 | 14.3k | JxlDecoderStatus JxlDecoderSubscribeEvents(JxlDecoder* dec, int events_wanted) { |
877 | 14.3k | if (dec->stage != DecoderStage::kInited) { |
878 | 0 | return JXL_DEC_ERROR; // Cannot subscribe to events after having started. |
879 | 0 | } |
880 | 14.3k | if (events_wanted & 63) { |
881 | 0 | return JXL_DEC_ERROR; // Can only subscribe to informative events. |
882 | 0 | } |
883 | 14.3k | dec->events_wanted = events_wanted; |
884 | 14.3k | dec->orig_events_wanted = events_wanted; |
885 | 14.3k | return JXL_DEC_SUCCESS; |
886 | 14.3k | } |
887 | | |
888 | | JxlDecoderStatus JxlDecoderSetKeepOrientation(JxlDecoder* dec, |
889 | 14.3k | JXL_BOOL skip_reorientation) { |
890 | 14.3k | if (dec->stage != DecoderStage::kInited) { |
891 | 0 | return JXL_API_ERROR("Must set keep_orientation option before starting"); |
892 | 0 | } |
893 | 14.3k | dec->keep_orientation = FROM_JXL_BOOL(skip_reorientation); |
894 | 14.3k | return JXL_DEC_SUCCESS; |
895 | 14.3k | } |
896 | | |
897 | | JxlDecoderStatus JxlDecoderSetUnpremultiplyAlpha(JxlDecoder* dec, |
898 | 14.3k | JXL_BOOL unpremul_alpha) { |
899 | 14.3k | if (dec->stage != DecoderStage::kInited) { |
900 | 0 | return JXL_API_ERROR("Must set unpremul_alpha option before starting"); |
901 | 0 | } |
902 | 14.3k | dec->unpremul_alpha = FROM_JXL_BOOL(unpremul_alpha); |
903 | 14.3k | return JXL_DEC_SUCCESS; |
904 | 14.3k | } |
905 | | |
906 | | JxlDecoderStatus JxlDecoderSetRenderSpotcolors(JxlDecoder* dec, |
907 | 0 | JXL_BOOL render_spotcolors) { |
908 | 0 | if (dec->stage != DecoderStage::kInited) { |
909 | 0 | return JXL_API_ERROR("Must set render_spotcolors option before starting"); |
910 | 0 | } |
911 | 0 | dec->render_spotcolors = FROM_JXL_BOOL(render_spotcolors); |
912 | 0 | return JXL_DEC_SUCCESS; |
913 | 0 | } |
914 | | |
915 | 0 | JxlDecoderStatus JxlDecoderSetCoalescing(JxlDecoder* dec, JXL_BOOL coalescing) { |
916 | 0 | if (dec->stage != DecoderStage::kInited) { |
917 | 0 | return JXL_API_ERROR("Must set coalescing option before starting"); |
918 | 0 | } |
919 | 0 | dec->coalescing = FROM_JXL_BOOL(coalescing); |
920 | 0 | return JXL_DEC_SUCCESS; |
921 | 0 | } |
922 | | |
923 | | namespace { |
924 | | // helper function to get the dimensions of the current image buffer |
925 | 12.5k | void GetCurrentDimensions(const JxlDecoder* dec, size_t& xsize, size_t& ysize) { |
926 | 12.5k | if (dec->frame_header->nonserialized_is_preview) { |
927 | 0 | xsize = dec->metadata.oriented_preview_xsize(dec->keep_orientation); |
928 | 0 | ysize = dec->metadata.oriented_preview_ysize(dec->keep_orientation); |
929 | 0 | return; |
930 | 0 | } |
931 | 12.5k | xsize = dec->metadata.oriented_xsize(dec->keep_orientation); |
932 | 12.5k | ysize = dec->metadata.oriented_ysize(dec->keep_orientation); |
933 | 12.5k | if (!dec->coalescing) { |
934 | 0 | const auto frame_dim = dec->frame_header->ToFrameDimensions(); |
935 | 0 | xsize = frame_dim.xsize_upsampled; |
936 | 0 | ysize = frame_dim.ysize_upsampled; |
937 | 0 | if (!dec->keep_orientation && |
938 | 0 | static_cast<int>(dec->metadata.m.GetOrientation()) > 4) { |
939 | 0 | std::swap(xsize, ysize); |
940 | 0 | } |
941 | 0 | } |
942 | 12.5k | } |
943 | | } // namespace |
944 | | |
945 | | namespace jxl { |
946 | | namespace { |
947 | | |
948 | | // Returns JXL_DEC_SUCCESS if the full bundle was successfully read, status |
949 | | // indicating either error or need more input otherwise. |
950 | | template <class T> |
951 | | JxlDecoderStatus ReadBundle(JxlDecoder* dec, Span<const uint8_t> data, |
952 | 37.6k | BitReader* reader, T* JXL_RESTRICT t) { |
953 | | // Use a copy of the bit reader because CanRead advances bits. |
954 | 37.6k | BitReader reader2(data); |
955 | 37.6k | reader2.SkipBits(reader->TotalBitsConsumed()); |
956 | 37.6k | bool can_read = Bundle::CanRead(&reader2, t); |
957 | 37.6k | JXL_API_RETURN_IF_ERROR(reader2.Close()); |
958 | | |
959 | 37.6k | if (!can_read) { |
960 | 1.99k | return dec->RequestMoreInput(); |
961 | 1.99k | } |
962 | 35.7k | if (!Bundle::Read(reader, t)) { |
963 | 175 | return JXL_DEC_ERROR; |
964 | 175 | } |
965 | 35.5k | return JXL_DEC_SUCCESS; |
966 | 35.7k | } decode.cc:JxlDecoderStatus jxl::(anonymous namespace)::ReadBundle<jxl::SizeHeader>(JxlDecoder*, jxl::Span<unsigned char const>, jxl::BitReader*, jxl::SizeHeader*) Line | Count | Source | 952 | 13.0k | BitReader* reader, T* JXL_RESTRICT t) { | 953 | | // Use a copy of the bit reader because CanRead advances bits. | 954 | 13.0k | BitReader reader2(data); | 955 | 13.0k | reader2.SkipBits(reader->TotalBitsConsumed()); | 956 | 13.0k | bool can_read = Bundle::CanRead(&reader2, t); | 957 | 13.0k | JXL_API_RETURN_IF_ERROR(reader2.Close()); | 958 | | | 959 | 13.0k | if (!can_read) { | 960 | 30 | return dec->RequestMoreInput(); | 961 | 30 | } | 962 | 13.0k | if (!Bundle::Read(reader, t)) { | 963 | 0 | return JXL_DEC_ERROR; | 964 | 0 | } | 965 | 13.0k | return JXL_DEC_SUCCESS; | 966 | 13.0k | } |
decode.cc:JxlDecoderStatus jxl::(anonymous namespace)::ReadBundle<jxl::ImageMetadata>(JxlDecoder*, jxl::Span<unsigned char const>, jxl::BitReader*, jxl::ImageMetadata*) Line | Count | Source | 952 | 13.0k | BitReader* reader, T* JXL_RESTRICT t) { | 953 | | // Use a copy of the bit reader because CanRead advances bits. | 954 | 13.0k | BitReader reader2(data); | 955 | 13.0k | reader2.SkipBits(reader->TotalBitsConsumed()); | 956 | 13.0k | bool can_read = Bundle::CanRead(&reader2, t); | 957 | 13.0k | JXL_API_RETURN_IF_ERROR(reader2.Close()); | 958 | | | 959 | 13.0k | if (!can_read) { | 960 | 1.50k | return dec->RequestMoreInput(); | 961 | 1.50k | } | 962 | 11.5k | if (!Bundle::Read(reader, t)) { | 963 | 172 | return JXL_DEC_ERROR; | 964 | 172 | } | 965 | 11.3k | return JXL_DEC_SUCCESS; | 966 | 11.5k | } |
decode.cc:JxlDecoderStatus jxl::(anonymous namespace)::ReadBundle<jxl::CustomTransformData>(JxlDecoder*, jxl::Span<unsigned char const>, jxl::BitReader*, jxl::CustomTransformData*) Line | Count | Source | 952 | 11.5k | BitReader* reader, T* JXL_RESTRICT t) { | 953 | | // Use a copy of the bit reader because CanRead advances bits. | 954 | 11.5k | BitReader reader2(data); | 955 | 11.5k | reader2.SkipBits(reader->TotalBitsConsumed()); | 956 | 11.5k | bool can_read = Bundle::CanRead(&reader2, t); | 957 | 11.5k | JXL_API_RETURN_IF_ERROR(reader2.Close()); | 958 | | | 959 | 11.5k | if (!can_read) { | 960 | 465 | return dec->RequestMoreInput(); | 961 | 465 | } | 962 | 11.1k | if (!Bundle::Read(reader, t)) { | 963 | 3 | return JXL_DEC_ERROR; | 964 | 3 | } | 965 | 11.1k | return JXL_DEC_SUCCESS; | 966 | 11.1k | } |
|
967 | | |
968 | | std::unique_ptr<BitReader, std::function<void(BitReader*)>> GetBitReader( |
969 | 80.7k | Span<const uint8_t> span) { |
970 | 80.7k | BitReader* reader = new BitReader(span); |
971 | 80.7k | return std::unique_ptr<BitReader, std::function<void(BitReader*)>>( |
972 | 80.7k | reader, [](BitReader* reader) { |
973 | | // We can't allow Close to abort the program if the reader is out of |
974 | | // bounds, or all return paths in the code, even those that already |
975 | | // return failure, would have to manually call AllReadsWithinBounds(). |
976 | | // Invalid JXL codestream should not cause program to quit. |
977 | 80.7k | (void)reader->AllReadsWithinBounds(); |
978 | 80.7k | (void)reader->Close(); |
979 | 80.7k | delete reader; |
980 | 80.7k | }); |
981 | 80.7k | } |
982 | | |
983 | 13.0k | JxlDecoderStatus JxlDecoderReadBasicInfo(JxlDecoder* dec) { |
984 | 13.0k | if (!dec->got_codestream_signature) { |
985 | | // Check and skip the codestream signature |
986 | 12.2k | Span<const uint8_t> span; |
987 | 12.2k | JXL_API_RETURN_IF_ERROR(dec->GetCodestreamInput(&span)); |
988 | 12.2k | if (span.size() < 2) { |
989 | 0 | return dec->RequestMoreInput(); |
990 | 0 | } |
991 | 12.2k | if (span.data()[0] != 0xff || span.data()[1] != jxl::kCodestreamMarker) { |
992 | 5 | return JXL_INPUT_ERROR("invalid signature"); |
993 | 5 | } |
994 | 12.2k | dec->got_codestream_signature = true; |
995 | 12.2k | dec->AdvanceCodestream(2); |
996 | 12.2k | } |
997 | | |
998 | 13.0k | Span<const uint8_t> span; |
999 | 13.0k | JXL_API_RETURN_IF_ERROR(dec->GetCodestreamInput(&span)); |
1000 | 13.0k | auto reader = GetBitReader(span); |
1001 | 13.0k | JXL_API_RETURN_IF_ERROR( |
1002 | 13.0k | ReadBundle(dec, span, reader.get(), &dec->metadata.size)); |
1003 | 13.0k | JXL_API_RETURN_IF_ERROR( |
1004 | 13.0k | ReadBundle(dec, span, reader.get(), &dec->metadata.m)); |
1005 | 11.3k | size_t total_bits = reader->TotalBitsConsumed(); |
1006 | 11.3k | dec->AdvanceCodestream(total_bits / jxl::kBitsPerByte); |
1007 | 11.3k | dec->codestream_bits_ahead = total_bits % jxl::kBitsPerByte; |
1008 | 11.3k | dec->got_basic_info = true; |
1009 | 11.3k | dec->basic_info_size_hint = 0; |
1010 | 11.3k | dec->image_metadata = dec->metadata.m; |
1011 | 11.3k | JXL_DEBUG_V(2, "Decoded BasicInfo: %s", dec->metadata.DebugString().c_str()); |
1012 | | |
1013 | 11.3k | if (!CheckSizeLimit(dec, dec->metadata.size.xsize(), |
1014 | 11.3k | dec->metadata.size.ysize())) { |
1015 | 0 | return JXL_INPUT_ERROR("image is too large"); |
1016 | 0 | } |
1017 | | |
1018 | 11.3k | return JXL_DEC_SUCCESS; |
1019 | 11.3k | } |
1020 | | |
1021 | | // Reads all codestream headers (but not frame headers) |
1022 | 12.4k | JxlDecoderStatus JxlDecoderReadAllHeaders(JxlDecoder* dec) { |
1023 | 12.4k | if (!dec->got_transform_data) { |
1024 | 11.6k | Span<const uint8_t> span; |
1025 | 11.6k | JXL_API_RETURN_IF_ERROR(dec->GetCodestreamInput(&span)); |
1026 | 11.5k | auto reader = GetBitReader(span); |
1027 | 11.5k | reader->SkipBits(dec->codestream_bits_ahead); |
1028 | 11.5k | dec->metadata.transform_data.nonserialized_xyb_encoded = |
1029 | 11.5k | dec->metadata.m.xyb_encoded; |
1030 | 11.5k | JXL_API_RETURN_IF_ERROR( |
1031 | 11.5k | ReadBundle(dec, span, reader.get(), &dec->metadata.transform_data)); |
1032 | 11.1k | size_t total_bits = reader->TotalBitsConsumed(); |
1033 | 11.1k | dec->AdvanceCodestream(total_bits / jxl::kBitsPerByte); |
1034 | 11.1k | dec->codestream_bits_ahead = total_bits % jxl::kBitsPerByte; |
1035 | 11.1k | dec->got_transform_data = true; |
1036 | 11.1k | } |
1037 | | |
1038 | 11.9k | Span<const uint8_t> span; |
1039 | 11.9k | JXL_API_RETURN_IF_ERROR(dec->GetCodestreamInput(&span)); |
1040 | 11.8k | auto reader = GetBitReader(span); |
1041 | 11.8k | reader->SkipBits(dec->codestream_bits_ahead); |
1042 | | |
1043 | 11.8k | if (dec->metadata.m.color_encoding.WantICC()) { |
1044 | 2.45k | jxl::Status status = dec->icc_reader->Init(reader.get()); |
1045 | | // Always check AllReadsWithinBounds, not all the C++ decoder implementation |
1046 | | // handles reader out of bounds correctly yet (e.g. context map). Not |
1047 | | // checking AllReadsWithinBounds can cause reader->Close() to trigger an |
1048 | | // assert, but we don't want library to quit program for invalid codestream. |
1049 | 2.45k | if (!reader->AllReadsWithinBounds() || |
1050 | 2.45k | status.code() == StatusCode::kNotEnoughBytes) { |
1051 | 1.15k | return dec->RequestMoreInput(); |
1052 | 1.15k | } |
1053 | 1.29k | if (!status) { |
1054 | | // Other non-successful status is an error |
1055 | 224 | return JXL_DEC_ERROR; |
1056 | 224 | } |
1057 | 1.07k | PaddedBytes decoded_icc{&dec->memory_manager}; |
1058 | 1.07k | status = dec->icc_reader->Process(reader.get(), &decoded_icc); |
1059 | 1.07k | if (status.code() == StatusCode::kNotEnoughBytes) { |
1060 | 404 | return dec->RequestMoreInput(); |
1061 | 404 | } |
1062 | 670 | if (!status) { |
1063 | | // Other non-successful status is an error |
1064 | 659 | return JXL_DEC_ERROR; |
1065 | 659 | } |
1066 | 11 | if (decoded_icc.empty()) { |
1067 | 2 | return JXL_DEC_ERROR; |
1068 | 2 | } |
1069 | 9 | IccBytes icc; |
1070 | 9 | Bytes(decoded_icc).AppendTo(icc); |
1071 | 9 | dec->metadata.m.color_encoding.SetICCRaw(std::move(icc)); |
1072 | 9 | } |
1073 | | |
1074 | 9.45k | dec->got_all_headers = true; |
1075 | 9.45k | JXL_API_RETURN_IF_ERROR(reader->JumpToByteBoundary()); |
1076 | | |
1077 | 9.41k | dec->AdvanceCodestream(reader->TotalBitsConsumed() / jxl::kBitsPerByte); |
1078 | 9.41k | dec->codestream_bits_ahead = 0; |
1079 | | |
1080 | 9.41k | if (!dec->passes_state) { |
1081 | 9.41k | dec->passes_state = |
1082 | 9.41k | jxl::make_unique<jxl::PassesDecoderState>(&dec->memory_manager); |
1083 | 9.41k | } |
1084 | | |
1085 | 9.41k | JXL_API_RETURN_IF_ERROR( |
1086 | 9.41k | dec->passes_state->output_encoding_info.SetFromMetadata(dec->metadata)); |
1087 | 9.41k | if (dec->desired_intensity_target > 0) { |
1088 | 0 | dec->passes_state->output_encoding_info.desired_intensity_target = |
1089 | 0 | dec->desired_intensity_target; |
1090 | 0 | } |
1091 | 9.41k | dec->image_metadata = dec->metadata.m; |
1092 | | |
1093 | 9.41k | return JXL_DEC_SUCCESS; |
1094 | 9.41k | } |
1095 | | |
1096 | 26.4k | JxlDecoderStatus JxlDecoderProcessSections(JxlDecoder* dec) { |
1097 | 26.4k | Span<const uint8_t> span; |
1098 | 26.4k | JXL_API_RETURN_IF_ERROR(dec->GetCodestreamInput(&span)); |
1099 | 26.3k | const auto& toc = dec->frame_dec->Toc(); |
1100 | 26.3k | size_t pos = 0; |
1101 | 26.3k | std::vector<jxl::FrameDecoder::SectionInfo> section_info; |
1102 | 26.3k | std::vector<jxl::FrameDecoder::SectionStatus> section_status; |
1103 | 61.4k | for (size_t i = dec->next_section; i < toc.size(); ++i) { |
1104 | 36.5k | if (dec->section_processed[i]) { |
1105 | 3 | pos += toc[i].size; |
1106 | 3 | continue; |
1107 | 3 | } |
1108 | 36.5k | size_t id = toc[i].id; |
1109 | 36.5k | size_t size = toc[i].size; |
1110 | 36.5k | if (OutOfBounds(pos, size, span.size())) { |
1111 | 1.41k | break; |
1112 | 1.41k | } |
1113 | 35.0k | auto* br = new jxl::BitReader(jxl::Bytes(span.data() + pos, size)); |
1114 | 35.0k | section_info.emplace_back(jxl::FrameDecoder::SectionInfo{br, id, i}); |
1115 | 35.0k | section_status.emplace_back(); |
1116 | 35.0k | pos += size; |
1117 | 35.0k | } |
1118 | 26.3k | jxl::Status status = dec->frame_dec->ProcessSections( |
1119 | 26.3k | section_info.data(), section_info.size(), section_status.data()); |
1120 | 26.3k | bool out_of_bounds = false; |
1121 | 26.3k | bool has_error = false; |
1122 | 35.0k | for (const auto& info : section_info) { |
1123 | 35.0k | if (!info.br->AllReadsWithinBounds()) { |
1124 | | // Mark out of bounds section, but keep closing and deleting the next |
1125 | | // ones as well. |
1126 | 3.65k | out_of_bounds = true; |
1127 | 3.65k | } |
1128 | 35.0k | if (!info.br->Close()) has_error = true; |
1129 | 35.0k | delete info.br; |
1130 | 35.0k | } |
1131 | 26.3k | if (has_error) { |
1132 | 0 | return JXL_INPUT_ERROR("internal: bit-reader failed to close"); |
1133 | 0 | } |
1134 | 26.3k | if (out_of_bounds) { |
1135 | | // If any bit reader indicates out of bounds, it's an error, not just |
1136 | | // needing more input, since we ensure only bit readers containing |
1137 | | // a complete section are provided to the FrameDecoder. |
1138 | 3.59k | return JXL_INPUT_ERROR("frame out of bounds"); |
1139 | 3.59k | } |
1140 | 22.7k | if (!status) { |
1141 | 903 | return JXL_INPUT_ERROR("frame processing failed"); |
1142 | 903 | } |
1143 | 49.6k | for (size_t i = 0; i < section_status.size(); ++i) { |
1144 | 27.8k | auto s_status = section_status[i]; |
1145 | 27.8k | if (s_status == jxl::FrameDecoder::kDone) { |
1146 | 27.7k | dec->section_processed[section_info[i].index] = 1; |
1147 | 27.7k | } else if (s_status != jxl::FrameDecoder::kSkipped) { |
1148 | 0 | return JXL_INPUT_ERROR("unexpected section status"); |
1149 | 0 | } |
1150 | 27.8k | } |
1151 | 21.8k | size_t completed_prefix_bytes = 0; |
1152 | 49.5k | while (dec->next_section < dec->section_processed.size() && |
1153 | 49.5k | dec->section_processed[dec->next_section] == 1) { |
1154 | 27.7k | completed_prefix_bytes += toc[dec->next_section].size; |
1155 | 27.7k | ++dec->next_section; |
1156 | 27.7k | } |
1157 | 21.8k | dec->remaining_frame_size -= completed_prefix_bytes; |
1158 | 21.8k | dec->AdvanceCodestream(completed_prefix_bytes); |
1159 | 21.8k | return JXL_DEC_SUCCESS; |
1160 | 21.8k | } |
1161 | | |
1162 | | // TODO(eustas): no CodecInOut -> no image size reinforcement -> possible OOM. |
1163 | 46.5k | JxlDecoderStatus JxlDecoderProcessCodestream(JxlDecoder* dec) { |
1164 | | // If no parallel runner is set, use the default |
1165 | | // TODO(lode): move this initialization to an appropriate location once the |
1166 | | // runner is used to decode pixels. |
1167 | 46.5k | if (!dec->thread_pool) { |
1168 | 2.71k | dec->thread_pool = jxl::make_unique<jxl::ThreadPool>(nullptr, nullptr); |
1169 | 2.71k | } |
1170 | | |
1171 | | // No matter what events are wanted, the basic info is always required. |
1172 | 46.5k | if (!dec->got_basic_info) { |
1173 | 13.0k | JxlDecoderStatus status = JxlDecoderReadBasicInfo(dec); |
1174 | 13.0k | if (status != JXL_DEC_SUCCESS) return status; |
1175 | 13.0k | } |
1176 | | |
1177 | 44.8k | if (dec->events_wanted & JXL_DEC_BASIC_INFO) { |
1178 | 11.3k | dec->events_wanted &= ~JXL_DEC_BASIC_INFO; |
1179 | 11.3k | return JXL_DEC_BASIC_INFO; |
1180 | 11.3k | } |
1181 | | |
1182 | 33.4k | if (!dec->events_wanted) { |
1183 | 0 | dec->stage = DecoderStage::kCodestreamFinished; |
1184 | 0 | return JXL_DEC_SUCCESS; |
1185 | 0 | } |
1186 | | |
1187 | 33.4k | if (!dec->icc_reader) { |
1188 | 11.3k | dec->icc_reader = jxl::make_unique<ICCReader>(&dec->memory_manager); |
1189 | 11.3k | } |
1190 | | |
1191 | 33.4k | if (!dec->got_all_headers) { |
1192 | 12.4k | JxlDecoderStatus status = JxlDecoderReadAllHeaders(dec); |
1193 | 12.4k | if (status != JXL_DEC_SUCCESS) return status; |
1194 | 12.4k | } |
1195 | | |
1196 | 30.4k | if (dec->events_wanted & JXL_DEC_COLOR_ENCODING) { |
1197 | 8.10k | dec->events_wanted &= ~JXL_DEC_COLOR_ENCODING; |
1198 | 8.10k | return JXL_DEC_COLOR_ENCODING; |
1199 | 8.10k | } |
1200 | | |
1201 | 22.3k | if (!dec->events_wanted) { |
1202 | 0 | dec->stage = DecoderStage::kCodestreamFinished; |
1203 | 0 | return JXL_DEC_SUCCESS; |
1204 | 0 | } |
1205 | | |
1206 | 22.3k | dec->post_headers = true; |
1207 | | |
1208 | 22.3k | if (!dec->got_preview_image && dec->metadata.m.have_preview) { |
1209 | 136 | dec->preview_frame = true; |
1210 | 136 | } |
1211 | | |
1212 | | // Handle frames |
1213 | 55.0k | for (;;) { |
1214 | 55.0k | bool parse_frames = |
1215 | 55.0k | (dec->events_wanted & |
1216 | 55.0k | (JXL_DEC_PREVIEW_IMAGE | JXL_DEC_FRAME | JXL_DEC_FULL_IMAGE)); |
1217 | 55.0k | if (!parse_frames) { |
1218 | 0 | break; |
1219 | 0 | } |
1220 | 55.0k | if (dec->frame_stage == FrameStage::kHeader && dec->is_last_total) { |
1221 | 418 | break; |
1222 | 418 | } |
1223 | 54.6k | if (dec->frame_stage == FrameStage::kHeader) { |
1224 | 45.2k | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1225 | 45.2k | if (dec->recon_output_jpeg == JpegReconStage::kSettingMetadata || |
1226 | 45.2k | dec->recon_output_jpeg == JpegReconStage::kOutputting) { |
1227 | | // The image bundle contains the JPEG reconstruction frame, but the |
1228 | | // decoder is still waiting to decode an EXIF or XMP box. It's not |
1229 | | // implemented to decode additional frames during this, and a JPEG |
1230 | | // reconstruction image should have only one frame. |
1231 | 0 | return JXL_API_ERROR( |
1232 | 0 | "cannot decode a next frame after JPEG reconstruction frame"); |
1233 | 0 | } |
1234 | 45.2k | #endif |
1235 | 45.2k | if (!dec->ib) { |
1236 | 29.6k | dec->ib = jxl::make_unique<jxl::ImageBundle>(&dec->memory_manager, |
1237 | 29.6k | &dec->image_metadata); |
1238 | 29.6k | } |
1239 | 45.2k | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1240 | | // If JPEG reconstruction is wanted and possible, set the jpeg_data of |
1241 | | // the ImageBundle. |
1242 | 45.2k | if (!dec->jpeg_decoder.SetImageBundleJpegData(dec->ib.get())) |
1243 | 0 | return JXL_DEC_ERROR; |
1244 | 45.2k | #endif |
1245 | 45.2k | dec->frame_dec = jxl::make_unique<FrameDecoder>( |
1246 | 45.2k | dec->passes_state.get(), dec->metadata, dec->thread_pool.get(), |
1247 | 45.2k | /*use_slow_rendering_pipeline=*/false); |
1248 | 45.2k | dec->frame_header = jxl::make_unique<FrameHeader>(&dec->metadata); |
1249 | 45.2k | Span<const uint8_t> span; |
1250 | 45.2k | JXL_API_RETURN_IF_ERROR(dec->GetCodestreamInput(&span)); |
1251 | 44.1k | auto reader = GetBitReader(span); |
1252 | 44.1k | jxl::Status status = dec->frame_dec->InitFrame( |
1253 | 44.1k | reader.get(), dec->ib.get(), dec->preview_frame); |
1254 | 44.1k | if (!reader->AllReadsWithinBounds() || |
1255 | 44.1k | status.code() == StatusCode::kNotEnoughBytes) { |
1256 | 4.52k | return dec->RequestMoreInput(); |
1257 | 39.6k | } else if (!status) { |
1258 | 1.02k | return JXL_INPUT_ERROR("invalid frame header"); |
1259 | 1.02k | } |
1260 | 38.6k | dec->AdvanceCodestream(reader->TotalBitsConsumed() / kBitsPerByte); |
1261 | 38.6k | *dec->frame_header = dec->frame_dec->GetFrameHeader(); |
1262 | 38.6k | jxl::FrameDimensions frame_dim = dec->frame_header->ToFrameDimensions(); |
1263 | 38.6k | if (!CheckSizeLimit(dec, frame_dim.xsize_upsampled_padded, |
1264 | 38.6k | frame_dim.ysize_upsampled_padded)) { |
1265 | 0 | return JXL_INPUT_ERROR("frame is too large"); |
1266 | 0 | } |
1267 | 38.6k | int output_type = |
1268 | 38.6k | dec->preview_frame ? JXL_DEC_PREVIEW_IMAGE : JXL_DEC_FULL_IMAGE; |
1269 | 38.6k | bool output_needed = ((dec->events_wanted & output_type) != 0); |
1270 | 38.6k | if (output_needed) { |
1271 | 25.8k | JXL_API_RETURN_IF_ERROR(dec->frame_dec->InitFrameOutput()); |
1272 | 25.8k | } |
1273 | 38.6k | dec->remaining_frame_size = dec->frame_dec->SumSectionSizes(); |
1274 | | |
1275 | 38.6k | dec->frame_stage = FrameStage::kTOC; |
1276 | 38.6k | if (dec->preview_frame) { |
1277 | 75 | if (!(dec->events_wanted & JXL_DEC_PREVIEW_IMAGE)) { |
1278 | 75 | dec->frame_stage = FrameStage::kHeader; |
1279 | 75 | dec->AdvanceCodestream(dec->remaining_frame_size); |
1280 | 75 | dec->got_preview_image = true; |
1281 | 75 | dec->preview_frame = false; |
1282 | 75 | } |
1283 | 75 | continue; |
1284 | 75 | } |
1285 | | |
1286 | 38.5k | int saved_as = FrameDecoder::SavedAs(*dec->frame_header); |
1287 | | // is last in entire codestream |
1288 | 38.5k | dec->is_last_total = dec->frame_header->is_last; |
1289 | | // is last of current still |
1290 | 38.5k | dec->is_last_of_still = |
1291 | 38.5k | dec->is_last_total || dec->frame_header->animation_frame.duration > 0; |
1292 | | // is kRegularFrame and coalescing is disabled |
1293 | 38.5k | dec->is_last_of_still |= |
1294 | 38.5k | (!dec->coalescing && |
1295 | 38.5k | dec->frame_header->frame_type == FrameType::kRegularFrame); |
1296 | 38.5k | const size_t internal_frame_index = dec->internal_frames; |
1297 | 38.5k | const size_t external_frame_index = dec->external_frames; |
1298 | 38.5k | if (dec->is_last_of_still) dec->external_frames++; |
1299 | 38.5k | dec->internal_frames++; |
1300 | | |
1301 | 38.5k | if (dec->skip_frames > 0) { |
1302 | 0 | dec->skipping_frame = true; |
1303 | 0 | if (dec->is_last_of_still) { |
1304 | 0 | dec->skip_frames--; |
1305 | 0 | } |
1306 | 38.5k | } else { |
1307 | 38.5k | dec->skipping_frame = false; |
1308 | 38.5k | } |
1309 | | |
1310 | 38.5k | if (external_frame_index >= dec->frame_external_to_internal.size()) { |
1311 | 7.87k | dec->frame_external_to_internal.push_back(internal_frame_index); |
1312 | 7.87k | if (dec->frame_external_to_internal.size() != |
1313 | 7.87k | external_frame_index + 1) { |
1314 | 0 | return JXL_API_ERROR("internal"); |
1315 | 0 | } |
1316 | 7.87k | } |
1317 | | |
1318 | 38.5k | if (internal_frame_index >= dec->frame_refs.size()) { |
1319 | | // add the value 0xff (which means all references) to new slots: we only |
1320 | | // know the references of the frame at FinalizeFrame, and fill in the |
1321 | | // correct values there. As long as this information is not known, the |
1322 | | // worst case where the frame depends on all storage slots is assumed. |
1323 | 38.5k | dec->frame_refs.emplace_back(FrameRef{0xFF, saved_as}); |
1324 | 38.5k | if (dec->frame_refs.size() != internal_frame_index + 1) { |
1325 | 0 | return JXL_API_ERROR("internal"); |
1326 | 0 | } |
1327 | 38.5k | } |
1328 | | |
1329 | 38.5k | if (dec->skipping_frame) { |
1330 | | // Whether this frame could be referenced by any future frame: either |
1331 | | // because it's a frame saved for blending or patches, or because it's |
1332 | | // a DC frame. |
1333 | 0 | bool referenceable = |
1334 | 0 | dec->frame_header->CanBeReferenced() || |
1335 | 0 | dec->frame_header->frame_type == FrameType::kDCFrame; |
1336 | 0 | if (internal_frame_index < dec->frame_required.size() && |
1337 | 0 | !dec->frame_required[internal_frame_index]) { |
1338 | 0 | referenceable = false; |
1339 | 0 | } |
1340 | 0 | if (!referenceable) { |
1341 | | // Skip all decoding for this frame, since the user is skipping this |
1342 | | // frame and no future frames can reference it. |
1343 | 0 | dec->frame_stage = FrameStage::kHeader; |
1344 | 0 | dec->AdvanceCodestream(dec->remaining_frame_size); |
1345 | 0 | continue; |
1346 | 0 | } |
1347 | 0 | } |
1348 | | |
1349 | 38.5k | if ((dec->events_wanted & JXL_DEC_FRAME) && dec->is_last_of_still) { |
1350 | | // Only return this for the last of a series of stills: patches frames |
1351 | | // etc... before this one do not contain the correct information such |
1352 | | // as animation timing, ... |
1353 | 4.55k | if (!dec->skipping_frame) { |
1354 | 4.55k | return JXL_DEC_FRAME; |
1355 | 4.55k | } |
1356 | 4.55k | } |
1357 | 38.5k | } |
1358 | | |
1359 | 43.3k | if (dec->frame_stage == FrameStage::kTOC) { |
1360 | 38.5k | dec->frame_dec->SetRenderSpotcolors(dec->render_spotcolors); |
1361 | 38.5k | dec->frame_dec->SetCoalescing(dec->coalescing); |
1362 | | |
1363 | 38.5k | if (!dec->preview_frame && |
1364 | 38.5k | (dec->events_wanted & JXL_DEC_FRAME_PROGRESSION)) { |
1365 | 0 | dec->frame_prog_detail = |
1366 | 0 | dec->frame_dec->SetPauseAtProgressive(dec->prog_detail); |
1367 | 38.5k | } else { |
1368 | 38.5k | dec->frame_prog_detail = JxlProgressiveDetail::kFrames; |
1369 | 38.5k | } |
1370 | 38.5k | dec->dc_frame_progression_done = false; |
1371 | | |
1372 | 38.5k | dec->next_section = 0; |
1373 | 38.5k | dec->section_processed.clear(); |
1374 | 38.5k | dec->section_processed.resize(dec->frame_dec->Toc().size(), 0); |
1375 | | |
1376 | | // If we don't need pixels, we can skip actually decoding the frames. |
1377 | 38.5k | if (dec->preview_frame || (dec->events_wanted & JXL_DEC_FULL_IMAGE)) { |
1378 | 25.8k | dec->frame_stage = FrameStage::kFull; |
1379 | 25.8k | } else if (!dec->is_last_total) { |
1380 | 12.6k | dec->frame_stage = FrameStage::kHeader; |
1381 | 12.6k | dec->AdvanceCodestream(dec->remaining_frame_size); |
1382 | 12.6k | continue; |
1383 | 12.6k | } else { |
1384 | 55 | break; |
1385 | 55 | } |
1386 | 38.5k | } |
1387 | | |
1388 | 30.6k | if (dec->frame_stage == FrameStage::kFull) { |
1389 | 30.6k | if (!dec->image_out_buffer_set) { |
1390 | 26.2k | if (dec->preview_frame) { |
1391 | 0 | return JXL_DEC_NEED_PREVIEW_OUT_BUFFER; |
1392 | 0 | } |
1393 | 26.2k | if ( |
1394 | 26.2k | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1395 | 26.2k | (!dec->jpeg_decoder.IsOutputSet() || |
1396 | 26.2k | dec->ib->jpeg_data == nullptr) && |
1397 | 26.2k | #endif |
1398 | 26.2k | dec->is_last_of_still && !dec->skipping_frame) { |
1399 | | // TODO(lode): remove the dec->is_last_of_still condition if the |
1400 | | // frame decoder needs the image buffer as working space for decoding |
1401 | | // non-visible or blending frames too |
1402 | 4.23k | return JXL_DEC_NEED_IMAGE_OUT_BUFFER; |
1403 | 4.23k | } |
1404 | 26.2k | } |
1405 | | |
1406 | 26.4k | if (dec->image_out_buffer_set) { |
1407 | 4.39k | size_t xsize; |
1408 | 4.39k | size_t ysize; |
1409 | 4.39k | GetCurrentDimensions(dec, xsize, ysize); |
1410 | 4.39k | size_t bits_per_sample = GetBitDepth( |
1411 | 4.39k | dec->image_out_bit_depth, dec->metadata.m, dec->image_out_format); |
1412 | 4.39k | dec->frame_dec->SetImageOutput( |
1413 | 4.39k | PixelCallback{ |
1414 | 4.39k | dec->image_out_init_callback, dec->image_out_run_callback, |
1415 | 4.39k | dec->image_out_destroy_callback, dec->image_out_init_opaque}, |
1416 | 4.39k | reinterpret_cast<uint8_t*>(dec->image_out_buffer), |
1417 | 4.39k | dec->image_out_size, xsize, ysize, dec->image_out_format, |
1418 | 4.39k | bits_per_sample, dec->unpremul_alpha, !dec->keep_orientation); |
1419 | 4.39k | for (size_t i = 0; i < dec->extra_channel_output.size(); ++i) { |
1420 | 0 | const auto& extra = dec->extra_channel_output[i]; |
1421 | 0 | size_t ec_bits_per_sample = |
1422 | 0 | GetBitDepth(dec->image_out_bit_depth, |
1423 | 0 | dec->metadata.m.extra_channel_info[i], extra.format); |
1424 | 0 | dec->frame_dec->AddExtraChannelOutput(extra.buffer, extra.buffer_size, |
1425 | 0 | xsize, extra.format, |
1426 | 0 | ec_bits_per_sample); |
1427 | 0 | } |
1428 | 4.39k | } |
1429 | | |
1430 | 26.4k | size_t next_num_passes_to_pause = dec->frame_dec->NextNumPassesToPause(); |
1431 | | |
1432 | 26.4k | JXL_API_RETURN_IF_ERROR(JxlDecoderProcessSections(dec)); |
1433 | | |
1434 | 21.8k | bool all_sections_done = dec->frame_dec->HasDecodedAll(); |
1435 | 21.8k | bool got_dc_only = !all_sections_done && dec->frame_dec->HasDecodedDC(); |
1436 | | |
1437 | 21.8k | if (dec->frame_prog_detail >= JxlProgressiveDetail::kDC && |
1438 | 21.8k | !dec->dc_frame_progression_done && got_dc_only) { |
1439 | 0 | dec->dc_frame_progression_done = true; |
1440 | 0 | dec->downsampling_target = 8; |
1441 | 0 | return JXL_DEC_FRAME_PROGRESSION; |
1442 | 0 | } |
1443 | | |
1444 | 21.8k | bool new_progression_step_done = |
1445 | 21.8k | dec->frame_dec->NumCompletePasses() >= next_num_passes_to_pause; |
1446 | | |
1447 | 21.8k | if (!all_sections_done && |
1448 | 21.8k | dec->frame_prog_detail >= JxlProgressiveDetail::kLastPasses && |
1449 | 21.8k | new_progression_step_done) { |
1450 | 0 | dec->downsampling_target = |
1451 | 0 | dec->frame_header->passes.GetDownsamplingTargetForCompletedPasses( |
1452 | 0 | dec->frame_dec->NumCompletePasses()); |
1453 | 0 | return JXL_DEC_FRAME_PROGRESSION; |
1454 | 0 | } |
1455 | | |
1456 | 21.8k | if (!all_sections_done) { |
1457 | | // Not all sections have been processed yet |
1458 | 1.12k | return dec->RequestMoreInput(); |
1459 | 1.12k | } |
1460 | | |
1461 | 20.6k | if (!dec->preview_frame) { |
1462 | 20.6k | size_t internal_index = dec->internal_frames - 1; |
1463 | 20.6k | if (dec->frame_refs.size() <= internal_index) { |
1464 | 0 | return JXL_API_ERROR("internal"); |
1465 | 0 | } |
1466 | | // Always fill this in, even if it was already written, it could be that |
1467 | | // this frame was skipped before and set to 255, while only now we know |
1468 | | // the true value. |
1469 | 20.6k | dec->frame_refs[internal_index].reference = |
1470 | 20.6k | dec->frame_dec->References(); |
1471 | 20.6k | } |
1472 | | |
1473 | 20.6k | if (!dec->frame_dec->FinalizeFrame()) { |
1474 | 0 | return JXL_INPUT_ERROR("decoding frame failed"); |
1475 | 0 | } |
1476 | 20.6k | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1477 | | // If jpeg output was requested, we merely return the JXL_DEC_FULL_IMAGE |
1478 | | // status without outputting pixels. |
1479 | 20.6k | if (dec->jpeg_decoder.IsOutputSet() && dec->ib->jpeg_data != nullptr) { |
1480 | 0 | dec->frame_stage = FrameStage::kHeader; |
1481 | 0 | dec->recon_output_jpeg = JpegReconStage::kSettingMetadata; |
1482 | 0 | return JXL_DEC_FULL_IMAGE; |
1483 | 0 | } |
1484 | 20.6k | #endif |
1485 | 20.6k | if (dec->preview_frame || dec->is_last_of_still) { |
1486 | 742 | dec->image_out_buffer_set = false; |
1487 | 742 | dec->extra_channel_output.clear(); |
1488 | 742 | } |
1489 | 20.6k | } |
1490 | | |
1491 | 20.6k | dec->frame_stage = FrameStage::kHeader; |
1492 | | |
1493 | | // The pixels have been output or are not needed, do not keep them in |
1494 | | // memory here. |
1495 | 20.6k | dec->ib.reset(); |
1496 | 20.6k | if (dec->preview_frame) { |
1497 | 0 | dec->got_preview_image = true; |
1498 | 0 | dec->preview_frame = false; |
1499 | 0 | dec->events_wanted &= ~JXL_DEC_PREVIEW_IMAGE; |
1500 | 0 | return JXL_DEC_PREVIEW_IMAGE; |
1501 | 20.6k | } else if (dec->is_last_of_still && |
1502 | 20.6k | (dec->events_wanted & JXL_DEC_FULL_IMAGE) && |
1503 | 20.6k | !dec->skipping_frame) { |
1504 | 742 | return JXL_DEC_FULL_IMAGE; |
1505 | 742 | } |
1506 | 20.6k | } |
1507 | | |
1508 | 473 | dec->stage = DecoderStage::kCodestreamFinished; |
1509 | | // Return success, this means there is nothing more to do. |
1510 | 473 | return JXL_DEC_SUCCESS; |
1511 | 22.3k | } |
1512 | | |
1513 | | } // namespace |
1514 | | } // namespace jxl |
1515 | | |
1516 | | JxlDecoderStatus JxlDecoderSetInput(JxlDecoder* dec, const uint8_t* data, |
1517 | 12.3k | size_t size) { |
1518 | 12.3k | if (dec->next_in) { |
1519 | 0 | return JXL_API_ERROR("already set input, use JxlDecoderReleaseInput first"); |
1520 | 0 | } |
1521 | 12.3k | if (dec->input_closed) { |
1522 | 0 | return JXL_API_ERROR("input already closed"); |
1523 | 0 | } |
1524 | | |
1525 | 12.3k | dec->next_in = data; |
1526 | 12.3k | dec->avail_in = size; |
1527 | 12.3k | return JXL_DEC_SUCCESS; |
1528 | 12.3k | } |
1529 | | |
1530 | 19.5k | size_t JxlDecoderReleaseInput(JxlDecoder* dec) { |
1531 | 19.5k | size_t result = dec->avail_in; |
1532 | 19.5k | dec->next_in = nullptr; |
1533 | 19.5k | dec->avail_in = 0; |
1534 | 19.5k | return result; |
1535 | 19.5k | } |
1536 | | |
1537 | 7.13k | void JxlDecoderCloseInput(JxlDecoder* dec) { dec->input_closed = true; } |
1538 | | |
1539 | | JxlDecoderStatus JxlDecoderSetJPEGBuffer(JxlDecoder* dec, uint8_t* data, |
1540 | 0 | size_t size) { |
1541 | 0 | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1542 | | // JPEG reconstruction buffer can only set and updated before or during the |
1543 | | // first frame, the reconstruction box refers to the first frame and in |
1544 | | // theory multi-frame images should not be used with a jbrd box. |
1545 | 0 | if (dec->internal_frames > 1) { |
1546 | 0 | return JXL_API_ERROR("JPEG reconstruction only works for the first frame"); |
1547 | 0 | } |
1548 | 0 | if (dec->jpeg_decoder.IsOutputSet()) { |
1549 | 0 | return JXL_API_ERROR("Already set JPEG buffer"); |
1550 | 0 | } |
1551 | 0 | return dec->jpeg_decoder.SetOutputBuffer(data, size); |
1552 | | #else |
1553 | | return JXL_API_ERROR("JPEG reconstruction is not supported."); |
1554 | | #endif |
1555 | 0 | } |
1556 | | |
1557 | 0 | size_t JxlDecoderReleaseJPEGBuffer(JxlDecoder* dec) { |
1558 | 0 | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1559 | 0 | return dec->jpeg_decoder.ReleaseOutputBuffer(); |
1560 | | #else |
1561 | | return JXL_API_ERROR("JPEG reconstruction is not supported."); |
1562 | | #endif |
1563 | 0 | } |
1564 | | |
1565 | | // Parses the header of the box, outputting the 4-character type and the box |
1566 | | // size, including header size, as stored in the box header. |
1567 | | // @param in current input bytes. |
1568 | | // @param size available input size. |
1569 | | // @param pos position in the input, must begin at the header of the box. |
1570 | | // @param file_pos position of pos since the start of the JXL file, rather than |
1571 | | // the current input, used for integer overflow checking. |
1572 | | // @param type the output box type. |
1573 | | // @param box_size output the total box size, including header, in bytes, or 0 |
1574 | | // if it's a final unbounded box. |
1575 | | // @param header_size output size of the box header. |
1576 | | // @return JXL_DEC_SUCCESS if the box header was fully parsed. In that case the |
1577 | | // parsing position must be incremented by header_size bytes. |
1578 | | // JXL_DEC_NEED_MORE_INPUT if not enough input bytes available, in that case |
1579 | | // header_size indicates a lower bound for the known size the header has to be |
1580 | | // at least. JXL_DEC_ERROR if the box header is invalid. |
1581 | | static JxlDecoderStatus ParseBoxHeader(const uint8_t* in, size_t size, |
1582 | | size_t pos, size_t file_pos, |
1583 | | JxlBoxType type, uint64_t* box_size, |
1584 | 801 | uint64_t* header_size) { |
1585 | 801 | if (OutOfBounds(pos, 8, size)) { |
1586 | 7 | *header_size = 8; |
1587 | 7 | return JXL_DEC_NEED_MORE_INPUT; |
1588 | 7 | } |
1589 | 794 | size_t box_start = pos; |
1590 | | // Box size, including this header itself. |
1591 | 794 | *box_size = LoadBE32(in + pos); |
1592 | 794 | pos += 4; |
1593 | 794 | memcpy(type, in + pos, 4); |
1594 | 794 | pos += 4; |
1595 | 794 | if (*box_size == 1) { |
1596 | 57 | *header_size = 16; |
1597 | 57 | if (OutOfBounds(pos, 8, size)) return JXL_DEC_NEED_MORE_INPUT; |
1598 | 56 | *box_size = LoadBE64(in + pos); |
1599 | 56 | pos += 8; |
1600 | 56 | } |
1601 | 793 | *header_size = pos - box_start; |
1602 | 793 | if (*box_size > 0 && *box_size < *header_size) { |
1603 | 1 | return JXL_INPUT_ERROR("invalid box size"); |
1604 | 1 | } |
1605 | 792 | if (file_pos + *box_size < file_pos) { |
1606 | 1 | return JXL_INPUT_ERROR("Box size overflow"); |
1607 | 1 | } |
1608 | 791 | return JXL_DEC_SUCCESS; |
1609 | 792 | } |
1610 | | |
1611 | | // This includes handling the codestream if it is not a box-based jxl file. |
1612 | 47.1k | static JxlDecoderStatus HandleBoxes(JxlDecoder* dec) { |
1613 | | // Box handling loop |
1614 | 60.3k | for (;;) { |
1615 | 60.3k | if (dec->box_stage != BoxStage::kHeader) { |
1616 | 47.3k | dec->AdvanceInput(dec->header_size); |
1617 | 47.3k | dec->header_size = 0; |
1618 | 47.3k | #if JPEGXL_ENABLE_BOXES |
1619 | 47.3k | if ((dec->events_wanted & JXL_DEC_BOX) && |
1620 | 47.3k | dec->box_out_buffer_set_current_box) { |
1621 | 73 | uint8_t* next_out = dec->box_out_buffer + dec->box_out_buffer_pos; |
1622 | 73 | size_t avail_out = dec->box_out_buffer_size - dec->box_out_buffer_pos; |
1623 | | |
1624 | 73 | JxlDecoderStatus box_result = dec->box_content_decoder.Process( |
1625 | 73 | dec->next_in, dec->avail_in, |
1626 | 73 | dec->file_pos - dec->box_contents_begin, &next_out, &avail_out); |
1627 | 73 | size_t produced = |
1628 | 73 | next_out - (dec->box_out_buffer + dec->box_out_buffer_pos); |
1629 | 73 | dec->box_out_buffer_pos += produced; |
1630 | | |
1631 | 73 | if (box_result == JXL_DEC_BOX_COMPLETE && |
1632 | 73 | !(dec->events_wanted & JXL_DEC_BOX_COMPLETE)) { |
1633 | 67 | box_result = JXL_DEC_SUCCESS; |
1634 | 67 | } |
1635 | | |
1636 | | // Don't return JXL_DEC_NEED_MORE_INPUT: the box stages below, instead, |
1637 | | // handle the input progression, and the above only outputs the part of |
1638 | | // the box seen so far. |
1639 | 73 | if (box_result != JXL_DEC_SUCCESS && |
1640 | 73 | box_result != JXL_DEC_NEED_MORE_INPUT) { |
1641 | 0 | return box_result; |
1642 | 0 | } |
1643 | 73 | } |
1644 | 47.3k | #endif |
1645 | 47.3k | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1646 | 47.3k | if (dec->store_exif == 1 || dec->store_xmp == 1) { |
1647 | 0 | std::vector<uint8_t>& metadata = |
1648 | 0 | (dec->store_exif == 1) ? dec->exif_metadata : dec->xmp_metadata; |
1649 | 0 | for (;;) { |
1650 | 0 | if (metadata.empty()) metadata.resize(64); |
1651 | 0 | uint8_t* orig_next_out = metadata.data() + dec->recon_out_buffer_pos; |
1652 | 0 | uint8_t* next_out = orig_next_out; |
1653 | 0 | size_t avail_out = metadata.size() - dec->recon_out_buffer_pos; |
1654 | 0 | JxlDecoderStatus box_result = dec->metadata_decoder.Process( |
1655 | 0 | dec->next_in, dec->avail_in, |
1656 | 0 | dec->file_pos - dec->box_contents_begin, &next_out, &avail_out); |
1657 | 0 | size_t produced = next_out - orig_next_out; |
1658 | 0 | dec->recon_out_buffer_pos += produced; |
1659 | 0 | if (box_result == JXL_DEC_BOX_NEED_MORE_OUTPUT) { |
1660 | 0 | metadata.resize(metadata.size() * 2); |
1661 | 0 | } else if (box_result == JXL_DEC_NEED_MORE_INPUT) { |
1662 | 0 | break; // box stage handling below will handle this instead |
1663 | 0 | } else if (box_result == JXL_DEC_BOX_COMPLETE) { |
1664 | 0 | size_t needed_size = (dec->store_exif == 1) ? dec->recon_exif_size |
1665 | 0 | : dec->recon_xmp_size; |
1666 | 0 | if (dec->box_contents_unbounded && |
1667 | 0 | dec->recon_out_buffer_pos < needed_size) { |
1668 | | // Unbounded box, but we know the expected size due to the jbrd |
1669 | | // box's data. Treat this as the JXL_DEC_NEED_MORE_INPUT case. |
1670 | 0 | break; |
1671 | 0 | } else { |
1672 | 0 | metadata.resize(dec->recon_out_buffer_pos); |
1673 | 0 | if (dec->store_exif == 1) dec->store_exif = 2; |
1674 | 0 | if (dec->store_xmp == 1) dec->store_xmp = 2; |
1675 | 0 | break; |
1676 | 0 | } |
1677 | 0 | } else { |
1678 | | // error |
1679 | 0 | return box_result; |
1680 | 0 | } |
1681 | 0 | } |
1682 | 0 | } |
1683 | 47.3k | #endif |
1684 | 47.3k | } |
1685 | 60.3k | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1686 | 60.3k | if (dec->recon_output_jpeg == JpegReconStage::kSettingMetadata && |
1687 | 60.3k | !dec->JbrdNeedMoreBoxes()) { |
1688 | 0 | jxl::jpeg::JPEGData* jpeg_data = dec->ib->jpeg_data.get(); |
1689 | 0 | if (dec->recon_exif_size) { |
1690 | 0 | JxlDecoderStatus status = jxl::JxlToJpegDecoder::SetExif( |
1691 | 0 | dec->exif_metadata.data(), dec->exif_metadata.size(), jpeg_data); |
1692 | 0 | if (status != JXL_DEC_SUCCESS) return status; |
1693 | 0 | } |
1694 | 0 | if (dec->recon_xmp_size) { |
1695 | 0 | JxlDecoderStatus status = jxl::JxlToJpegDecoder::SetXmp( |
1696 | 0 | dec->xmp_metadata.data(), dec->xmp_metadata.size(), jpeg_data); |
1697 | 0 | if (status != JXL_DEC_SUCCESS) return status; |
1698 | 0 | } |
1699 | 0 | dec->recon_output_jpeg = JpegReconStage::kOutputting; |
1700 | 0 | } |
1701 | | |
1702 | 60.3k | if (dec->recon_output_jpeg == JpegReconStage::kOutputting && |
1703 | 60.3k | !dec->JbrdNeedMoreBoxes()) { |
1704 | 0 | JxlDecoderStatus status = |
1705 | 0 | dec->jpeg_decoder.WriteOutput(*dec->ib->jpeg_data); |
1706 | 0 | if (status != JXL_DEC_SUCCESS) return status; |
1707 | 0 | dec->recon_output_jpeg = JpegReconStage::kNone; |
1708 | 0 | dec->ib.reset(); |
1709 | 0 | if (dec->events_wanted & JXL_DEC_FULL_IMAGE) { |
1710 | | // Return the full image event here now, this may be delayed if this |
1711 | | // could only be done after decoding an exif or xmp box after the |
1712 | | // codestream. |
1713 | 0 | return JXL_DEC_FULL_IMAGE; |
1714 | 0 | } |
1715 | 0 | } |
1716 | 60.3k | #endif |
1717 | | |
1718 | 60.3k | if (dec->box_stage == BoxStage::kHeader) { |
1719 | 13.0k | if (!dec->have_container) { |
1720 | 12.1k | if (dec->stage == DecoderStage::kCodestreamFinished) |
1721 | 0 | return JXL_DEC_SUCCESS; |
1722 | 12.1k | dec->box_stage = BoxStage::kCodestream; |
1723 | 12.1k | dec->box_contents_unbounded = true; |
1724 | 12.1k | continue; |
1725 | 12.1k | } |
1726 | 814 | if (dec->avail_in == 0) { |
1727 | 12 | if (dec->stage != DecoderStage::kCodestreamFinished) { |
1728 | | // Not yet seen (all) codestream boxes. |
1729 | 5 | return JXL_DEC_NEED_MORE_INPUT; |
1730 | 5 | } |
1731 | 7 | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1732 | 7 | if (dec->JbrdNeedMoreBoxes()) { |
1733 | 0 | return JXL_DEC_NEED_MORE_INPUT; |
1734 | 0 | } |
1735 | 7 | #endif |
1736 | 7 | if (dec->input_closed) { |
1737 | 6 | return JXL_DEC_SUCCESS; |
1738 | 6 | } |
1739 | 1 | if (!(dec->events_wanted & JXL_DEC_BOX)) { |
1740 | | // All codestream and jbrd metadata boxes finished, and no individual |
1741 | | // boxes requested by user, so no need to request any more input. |
1742 | | // This returns success for backwards compatibility, when |
1743 | | // JxlDecoderCloseInput and JXL_DEC_BOX did not exist, as well |
1744 | | // as for efficiency. |
1745 | 0 | return JXL_DEC_SUCCESS; |
1746 | 0 | } |
1747 | | // Even though we are exactly at a box end, there still may be more |
1748 | | // boxes. The user may call JxlDecoderCloseInput to indicate the input |
1749 | | // is finished and get success instead. |
1750 | 1 | return JXL_DEC_NEED_MORE_INPUT; |
1751 | 1 | } |
1752 | | |
1753 | 802 | bool boxed_codestream_done = |
1754 | 802 | ((dec->events_wanted & JXL_DEC_BOX) && |
1755 | 802 | dec->stage == DecoderStage::kCodestreamFinished && |
1756 | 802 | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1757 | 802 | !dec->JbrdNeedMoreBoxes() && |
1758 | 802 | #endif |
1759 | 802 | dec->last_codestream_seen); |
1760 | 802 | if (boxed_codestream_done && dec->avail_in >= 2 && |
1761 | 802 | dec->next_in[0] == 0xff && |
1762 | 802 | dec->next_in[1] == jxl::kCodestreamMarker) { |
1763 | | // We detected the start of the next naked codestream, so we can return |
1764 | | // success here. |
1765 | 1 | return JXL_DEC_SUCCESS; |
1766 | 1 | } |
1767 | | |
1768 | 801 | uint64_t box_size; |
1769 | 801 | uint64_t header_size; |
1770 | 801 | JxlDecoderStatus status = |
1771 | 801 | ParseBoxHeader(dec->next_in, dec->avail_in, 0, dec->file_pos, |
1772 | 801 | dec->box_type, &box_size, &header_size); |
1773 | 801 | if (status != JXL_DEC_SUCCESS) { |
1774 | 10 | if (status == JXL_DEC_NEED_MORE_INPUT) { |
1775 | 8 | dec->basic_info_size_hint = |
1776 | 8 | InitialBasicInfoSizeHint() + header_size - dec->file_pos; |
1777 | 8 | } |
1778 | 10 | return status; |
1779 | 10 | } |
1780 | 791 | if (memcmp(dec->box_type, "brob", 4) == 0) { |
1781 | 1 | if (dec->avail_in < header_size + 4) { |
1782 | 0 | return JXL_DEC_NEED_MORE_INPUT; |
1783 | 0 | } |
1784 | 1 | memcpy(dec->box_decoded_type, dec->next_in + header_size, |
1785 | 1 | sizeof(dec->box_decoded_type)); |
1786 | 790 | } else { |
1787 | 790 | memcpy(dec->box_decoded_type, dec->box_type, |
1788 | 790 | sizeof(dec->box_decoded_type)); |
1789 | 790 | } |
1790 | | |
1791 | | // Box order validity checks |
1792 | | // The signature box at box_count == 1 is not checked here since that's |
1793 | | // already done at the beginning. |
1794 | 791 | dec->box_count++; |
1795 | 791 | if (boxed_codestream_done && memcmp(dec->box_type, "JXL ", 4) == 0) { |
1796 | | // We detected the start of the next boxed stream, so we can return |
1797 | | // success here. |
1798 | 0 | return JXL_DEC_SUCCESS; |
1799 | 0 | } |
1800 | 791 | if (dec->box_count == 2 && memcmp(dec->box_type, "ftyp", 4) != 0) { |
1801 | 50 | return JXL_INPUT_ERROR("the second box must be the ftyp box"); |
1802 | 50 | } |
1803 | 741 | if (memcmp(dec->box_type, "ftyp", 4) == 0 && dec->box_count != 2) { |
1804 | 0 | return JXL_INPUT_ERROR("the ftyp box must come second"); |
1805 | 0 | } |
1806 | | |
1807 | 741 | dec->box_contents_unbounded = (box_size == 0); |
1808 | 741 | dec->box_contents_begin = dec->file_pos + header_size; |
1809 | 741 | dec->box_contents_end = |
1810 | 741 | dec->box_contents_unbounded ? 0 : (dec->file_pos + box_size); |
1811 | 741 | dec->box_contents_size = |
1812 | 741 | dec->box_contents_unbounded ? 0 : (box_size - header_size); |
1813 | 741 | dec->box_size = box_size; |
1814 | 741 | dec->header_size = header_size; |
1815 | 741 | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1816 | 741 | if (dec->orig_events_wanted & JXL_DEC_JPEG_RECONSTRUCTION) { |
1817 | | // Initiate storing of Exif or XMP data for JPEG reconstruction |
1818 | 0 | if (dec->store_exif == 0 && |
1819 | 0 | memcmp(dec->box_decoded_type, "Exif", 4) == 0) { |
1820 | 0 | dec->store_exif = 1; |
1821 | 0 | dec->recon_out_buffer_pos = 0; |
1822 | 0 | } |
1823 | 0 | if (dec->store_xmp == 0 && |
1824 | 0 | memcmp(dec->box_decoded_type, "xml ", 4) == 0) { |
1825 | 0 | dec->store_xmp = 1; |
1826 | 0 | dec->recon_out_buffer_pos = 0; |
1827 | 0 | } |
1828 | 0 | } |
1829 | 741 | #endif |
1830 | 741 | #if JPEGXL_ENABLE_BOXES |
1831 | 741 | if (dec->events_wanted & JXL_DEC_BOX) { |
1832 | 741 | bool decompress = |
1833 | 741 | dec->decompress_boxes && memcmp(dec->box_type, "brob", 4) == 0; |
1834 | 741 | dec->box_content_decoder.StartBox( |
1835 | 741 | decompress, dec->box_contents_unbounded, dec->box_contents_size); |
1836 | 741 | } |
1837 | 741 | #endif |
1838 | 741 | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1839 | 741 | if (dec->store_exif == 1 || dec->store_xmp == 1) { |
1840 | 0 | bool brob = memcmp(dec->box_type, "brob", 4) == 0; |
1841 | 0 | dec->metadata_decoder.StartBox(brob, dec->box_contents_unbounded, |
1842 | 0 | dec->box_contents_size); |
1843 | 0 | } |
1844 | 741 | #endif |
1845 | 741 | if (memcmp(dec->box_type, "ftyp", 4) == 0) { |
1846 | 108 | dec->box_stage = BoxStage::kFtyp; |
1847 | 633 | } else if (memcmp(dec->box_type, "jxlc", 4) == 0) { |
1848 | 2 | if (dec->last_codestream_seen) { |
1849 | 0 | return JXL_INPUT_ERROR("there can only be one jxlc box"); |
1850 | 0 | } |
1851 | 2 | dec->last_codestream_seen = true; |
1852 | 2 | dec->box_stage = BoxStage::kCodestream; |
1853 | 631 | } else if (memcmp(dec->box_type, "jxlp", 4) == 0) { |
1854 | 249 | dec->box_stage = BoxStage::kPartialCodestream; |
1855 | 249 | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1856 | 382 | } else if ((dec->orig_events_wanted & JXL_DEC_JPEG_RECONSTRUCTION) && |
1857 | 382 | memcmp(dec->box_type, "jbrd", 4) == 0) { |
1858 | 0 | if (!(dec->events_wanted & JXL_DEC_JPEG_RECONSTRUCTION)) { |
1859 | 0 | return JXL_INPUT_ERROR( |
1860 | 0 | "multiple JPEG reconstruction boxes not supported"); |
1861 | 0 | } |
1862 | 0 | dec->box_stage = BoxStage::kJpegRecon; |
1863 | 0 | #endif |
1864 | 382 | } else { |
1865 | 382 | dec->box_stage = BoxStage::kSkip; |
1866 | 382 | } |
1867 | | |
1868 | 741 | if (dec->events_wanted & JXL_DEC_BOX) { |
1869 | 741 | dec->box_event = true; |
1870 | 741 | dec->box_out_buffer_set_current_box = false; |
1871 | 741 | return JXL_DEC_BOX; |
1872 | 741 | } |
1873 | 47.3k | } else if (dec->box_stage == BoxStage::kFtyp) { |
1874 | 110 | if (dec->box_contents_size < 12) { |
1875 | 0 | return JXL_INPUT_ERROR("file type box too small"); |
1876 | 0 | } |
1877 | 110 | if (dec->avail_in < 4) return JXL_DEC_NEED_MORE_INPUT; |
1878 | 104 | if (memcmp(dec->next_in, "jxl ", 4) != 0) { |
1879 | 11 | return JXL_INPUT_ERROR("file type box major brand must be \"jxl \""); |
1880 | 11 | } |
1881 | 93 | dec->AdvanceInput(4); |
1882 | 93 | dec->box_stage = BoxStage::kSkip; |
1883 | 47.2k | } else if (dec->box_stage == BoxStage::kPartialCodestream) { |
1884 | 249 | if (dec->last_codestream_seen) { |
1885 | 0 | return JXL_INPUT_ERROR("cannot have jxlp box after last jxlp box"); |
1886 | 0 | } |
1887 | | // TODO(lode): error if box is unbounded but last bit not set |
1888 | 249 | if (dec->avail_in < 4) return JXL_DEC_NEED_MORE_INPUT; |
1889 | 249 | if (!dec->box_contents_unbounded && dec->box_contents_size < 4) { |
1890 | 0 | return JXL_INPUT_ERROR("jxlp box too small to contain index"); |
1891 | 0 | } |
1892 | 249 | size_t jxlp_index = LoadBE32(dec->next_in); |
1893 | | // The high bit of jxlp_index indicates whether this is the last |
1894 | | // jxlp box. |
1895 | 249 | if (jxlp_index & 0x80000000) { |
1896 | 32 | dec->last_codestream_seen = true; |
1897 | 32 | } |
1898 | 249 | dec->AdvanceInput(4); |
1899 | 249 | dec->box_stage = BoxStage::kCodestream; |
1900 | 47.0k | } else if (dec->box_stage == BoxStage::kCodestream) { |
1901 | 46.5k | JxlDecoderStatus status = jxl::JxlDecoderProcessCodestream(dec); |
1902 | 46.5k | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1903 | 46.5k | if (status == JXL_DEC_FULL_IMAGE) { |
1904 | 742 | if (dec->recon_output_jpeg != JpegReconStage::kNone) { |
1905 | 0 | continue; |
1906 | 0 | } |
1907 | 742 | } |
1908 | 46.5k | #endif |
1909 | 46.5k | if (status == JXL_DEC_NEED_MORE_INPUT) { |
1910 | 10.4k | if (dec->file_pos == dec->box_contents_end && |
1911 | 10.4k | !dec->box_contents_unbounded) { |
1912 | 217 | dec->box_stage = BoxStage::kHeader; |
1913 | 217 | continue; |
1914 | 217 | } |
1915 | 10.4k | } |
1916 | | |
1917 | 46.3k | if (status == JXL_DEC_SUCCESS) { |
1918 | 473 | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1919 | 473 | if (dec->JbrdNeedMoreBoxes()) { |
1920 | 0 | dec->box_stage = BoxStage::kSkip; |
1921 | 0 | continue; |
1922 | 0 | } |
1923 | 473 | #endif |
1924 | 473 | if (dec->box_contents_unbounded) { |
1925 | | // Last box reached and codestream done, nothing more to do. |
1926 | 460 | break; |
1927 | 460 | } |
1928 | 13 | if (dec->events_wanted & JXL_DEC_BOX) { |
1929 | | // Codestream done, but there may be more other boxes. |
1930 | 13 | dec->box_stage = BoxStage::kSkip; |
1931 | 13 | continue; |
1932 | 13 | } |
1933 | 13 | } |
1934 | 45.8k | return status; |
1935 | 46.3k | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
1936 | 46.3k | } else if (dec->box_stage == BoxStage::kJpegRecon) { |
1937 | 0 | if (!dec->jpeg_decoder.IsParsingBox()) { |
1938 | | // This is a new JPEG reconstruction metadata box. |
1939 | 0 | dec->jpeg_decoder.StartBox(dec->box_contents_unbounded, |
1940 | 0 | dec->box_contents_size); |
1941 | 0 | } |
1942 | 0 | const uint8_t* next_in = dec->next_in; |
1943 | 0 | size_t avail_in = dec->avail_in; |
1944 | 0 | JxlDecoderStatus recon_result = |
1945 | 0 | dec->jpeg_decoder.Process(&next_in, &avail_in); |
1946 | 0 | size_t consumed = next_in - dec->next_in; |
1947 | 0 | dec->AdvanceInput(consumed); |
1948 | 0 | if (recon_result == JXL_DEC_JPEG_RECONSTRUCTION) { |
1949 | 0 | jxl::jpeg::JPEGData* jpeg_data = dec->jpeg_decoder.GetJpegData(); |
1950 | 0 | size_t num_exif = jxl::JxlToJpegDecoder::NumExifMarkers(*jpeg_data); |
1951 | 0 | size_t num_xmp = jxl::JxlToJpegDecoder::NumXmpMarkers(*jpeg_data); |
1952 | 0 | if (num_exif) { |
1953 | 0 | if (num_exif > 1) { |
1954 | 0 | return JXL_INPUT_ERROR( |
1955 | 0 | "multiple exif markers for JPEG reconstruction not supported"); |
1956 | 0 | } |
1957 | 0 | if (JXL_DEC_SUCCESS != jxl::JxlToJpegDecoder::ExifBoxContentSize( |
1958 | 0 | *jpeg_data, &dec->recon_exif_size)) { |
1959 | 0 | return JXL_INPUT_ERROR("invalid jbrd exif size"); |
1960 | 0 | } |
1961 | 0 | } |
1962 | 0 | if (num_xmp) { |
1963 | 0 | if (num_xmp > 1) { |
1964 | 0 | return JXL_INPUT_ERROR( |
1965 | 0 | "multiple XMP markers for JPEG reconstruction not supported"); |
1966 | 0 | } |
1967 | 0 | if (JXL_DEC_SUCCESS != jxl::JxlToJpegDecoder::XmlBoxContentSize( |
1968 | 0 | *jpeg_data, &dec->recon_xmp_size)) { |
1969 | 0 | return JXL_INPUT_ERROR("invalid jbrd XMP size"); |
1970 | 0 | } |
1971 | 0 | } |
1972 | | |
1973 | 0 | dec->box_stage = BoxStage::kHeader; |
1974 | | // If successful JPEG reconstruction, return the success if the user |
1975 | | // cares about it, otherwise continue. |
1976 | 0 | if (dec->events_wanted & JXL_DEC_JPEG_RECONSTRUCTION) { |
1977 | 0 | dec->events_wanted &= ~JXL_DEC_JPEG_RECONSTRUCTION; |
1978 | 0 | return JXL_DEC_JPEG_RECONSTRUCTION; |
1979 | 0 | } |
1980 | 0 | } else { |
1981 | | // If anything else, return the result. |
1982 | 0 | return recon_result; |
1983 | 0 | } |
1984 | 0 | #endif |
1985 | 454 | } else if (dec->box_stage == BoxStage::kSkip) { |
1986 | 454 | if (dec->box_contents_unbounded) { |
1987 | 0 | if (dec->input_closed) { |
1988 | 0 | return JXL_DEC_SUCCESS; |
1989 | 0 | } |
1990 | 0 | if (!(dec->box_out_buffer_set)) { |
1991 | | // An unbounded box is always the last box. Not requesting box data, |
1992 | | // so return success even if JxlDecoderCloseInput was not called for |
1993 | | // backwards compatibility as well as efficiency since this box is |
1994 | | // being skipped. |
1995 | 0 | return JXL_DEC_SUCCESS; |
1996 | 0 | } |
1997 | | // Arbitrarily more bytes may follow, only JxlDecoderCloseInput can |
1998 | | // mark the end. |
1999 | 0 | dec->AdvanceInput(dec->avail_in); |
2000 | 0 | return JXL_DEC_NEED_MORE_INPUT; |
2001 | 0 | } |
2002 | | // Amount of remaining bytes in the box that is being skipped. |
2003 | 454 | size_t remaining = dec->box_contents_end - dec->file_pos; |
2004 | 454 | if (dec->avail_in < remaining) { |
2005 | | // Indicate how many more bytes needed starting from next_in. |
2006 | 30 | dec->basic_info_size_hint = |
2007 | 30 | InitialBasicInfoSizeHint() + dec->box_contents_end - dec->file_pos; |
2008 | | // Don't have the full box yet, skip all we have so far |
2009 | 30 | dec->AdvanceInput(dec->avail_in); |
2010 | 30 | return JXL_DEC_NEED_MORE_INPUT; |
2011 | 424 | } else { |
2012 | | // Full box available, skip all its remaining bytes |
2013 | 424 | dec->AdvanceInput(remaining); |
2014 | 424 | dec->box_stage = BoxStage::kHeader; |
2015 | 424 | } |
2016 | 454 | } else { |
2017 | 0 | JXL_DEBUG_ABORT("Unreachable"); |
2018 | 0 | } |
2019 | 60.3k | } |
2020 | 460 | return JXL_DEC_SUCCESS; |
2021 | 47.1k | } |
2022 | | |
2023 | 63.5k | JxlDecoderStatus JxlDecoderProcessInput(JxlDecoder* dec) { |
2024 | 63.5k | if (dec->stage == DecoderStage::kInited) { |
2025 | 14.3k | dec->stage = DecoderStage::kStarted; |
2026 | 14.3k | } |
2027 | 63.5k | if (dec->stage == DecoderStage::kError) { |
2028 | 0 | return JXL_API_ERROR( |
2029 | 0 | "Cannot keep using decoder after it encountered an error, use " |
2030 | 0 | "JxlDecoderReset to reset it"); |
2031 | 0 | } |
2032 | | |
2033 | 63.5k | if (!dec->got_signature) { |
2034 | 28.7k | JxlSignature sig = JxlSignatureCheck(dec->next_in, dec->avail_in); |
2035 | 28.7k | if (sig == JXL_SIG_INVALID) return JXL_INPUT_ERROR("invalid signature"); |
2036 | 28.7k | if (sig == JXL_SIG_NOT_ENOUGH_BYTES) { |
2037 | 16.3k | if (dec->input_closed) { |
2038 | 1.99k | return JXL_INPUT_ERROR("file too small for signature"); |
2039 | 1.99k | } |
2040 | 14.3k | return JXL_DEC_NEED_MORE_INPUT; |
2041 | 16.3k | } |
2042 | | |
2043 | 12.3k | dec->got_signature = true; |
2044 | | |
2045 | 12.3k | if (sig == JXL_SIG_CONTAINER) { |
2046 | 163 | dec->have_container = true; |
2047 | 12.1k | } else { |
2048 | 12.1k | dec->last_codestream_seen = true; |
2049 | 12.1k | } |
2050 | 12.3k | } |
2051 | | |
2052 | 47.1k | JxlDecoderStatus status = HandleBoxes(dec); |
2053 | | |
2054 | 47.1k | if (status == JXL_DEC_NEED_MORE_INPUT && dec->input_closed) { |
2055 | 5.14k | return JXL_INPUT_ERROR("premature end of input"); |
2056 | 5.14k | } |
2057 | | |
2058 | | // Even if the box handling returns success, certain types of |
2059 | | // data may be missing. |
2060 | 42.0k | if (status == JXL_DEC_SUCCESS) { |
2061 | 467 | if (dec->CanUseMoreCodestreamInput()) { |
2062 | 0 | return JXL_INPUT_ERROR("codestream never finished"); |
2063 | 0 | } |
2064 | 467 | #if JPEGXL_ENABLE_TRANSCODE_JPEG |
2065 | 467 | if (dec->JbrdNeedMoreBoxes()) { |
2066 | 0 | return JXL_INPUT_ERROR("missing metadata boxes for jpeg reconstruction"); |
2067 | 0 | } |
2068 | 467 | #endif |
2069 | 467 | } |
2070 | | |
2071 | 42.0k | return status; |
2072 | 42.0k | } |
2073 | | |
2074 | | // To ensure ABI forward-compatibility, this struct has a constant size. |
2075 | | static_assert(sizeof(JxlBasicInfo) == 204, |
2076 | | "JxlBasicInfo struct size should remain constant"); |
2077 | | |
2078 | | JxlDecoderStatus JxlDecoderGetBasicInfo(const JxlDecoder* dec, |
2079 | 11.3k | JxlBasicInfo* info) { |
2080 | 11.3k | if (!dec->got_basic_info) return JXL_DEC_NEED_MORE_INPUT; |
2081 | | |
2082 | 11.3k | if (info) { |
2083 | 11.3k | memset(info, 0, sizeof(*info)); |
2084 | | |
2085 | 11.3k | const jxl::ImageMetadata& meta = dec->metadata.m; |
2086 | | |
2087 | 11.3k | info->have_container = TO_JXL_BOOL(dec->have_container); |
2088 | 11.3k | info->xsize = dec->metadata.size.xsize(); |
2089 | 11.3k | info->ysize = dec->metadata.size.ysize(); |
2090 | 11.3k | info->uses_original_profile = TO_JXL_BOOL(!meta.xyb_encoded); |
2091 | | |
2092 | 11.3k | info->bits_per_sample = meta.bit_depth.bits_per_sample; |
2093 | 11.3k | info->exponent_bits_per_sample = meta.bit_depth.exponent_bits_per_sample; |
2094 | | |
2095 | 11.3k | info->have_preview = TO_JXL_BOOL(meta.have_preview); |
2096 | 11.3k | info->have_animation = TO_JXL_BOOL(meta.have_animation); |
2097 | 11.3k | info->orientation = static_cast<JxlOrientation>(meta.orientation); |
2098 | | |
2099 | 11.3k | if (!dec->keep_orientation) { |
2100 | 0 | if (info->orientation >= JXL_ORIENT_TRANSPOSE) { |
2101 | 0 | std::swap(info->xsize, info->ysize); |
2102 | 0 | } |
2103 | 0 | info->orientation = JXL_ORIENT_IDENTITY; |
2104 | 0 | } |
2105 | | |
2106 | 11.3k | info->intensity_target = meta.IntensityTarget(); |
2107 | 11.3k | if (dec->desired_intensity_target > 0) { |
2108 | 0 | info->intensity_target = dec->desired_intensity_target; |
2109 | 0 | } |
2110 | 11.3k | info->min_nits = meta.tone_mapping.min_nits; |
2111 | 11.3k | info->relative_to_max_display = |
2112 | 11.3k | TO_JXL_BOOL(meta.tone_mapping.relative_to_max_display); |
2113 | 11.3k | info->linear_below = meta.tone_mapping.linear_below; |
2114 | | |
2115 | 11.3k | const jxl::ExtraChannelInfo* alpha = meta.Find(jxl::ExtraChannel::kAlpha); |
2116 | 11.3k | if (alpha != nullptr) { |
2117 | 1.38k | info->alpha_bits = alpha->bit_depth.bits_per_sample; |
2118 | 1.38k | info->alpha_exponent_bits = alpha->bit_depth.exponent_bits_per_sample; |
2119 | 1.38k | info->alpha_premultiplied = TO_JXL_BOOL(alpha->alpha_associated); |
2120 | 9.97k | } else { |
2121 | 9.97k | info->alpha_bits = 0; |
2122 | 9.97k | info->alpha_exponent_bits = 0; |
2123 | 9.97k | info->alpha_premultiplied = 0; |
2124 | 9.97k | } |
2125 | | |
2126 | 11.3k | info->num_color_channels = |
2127 | 11.3k | meta.color_encoding.GetColorSpace() == jxl::ColorSpace::kGray ? 1 : 3; |
2128 | | |
2129 | 11.3k | info->num_extra_channels = meta.num_extra_channels; |
2130 | | |
2131 | 11.3k | if (info->have_preview) { |
2132 | 145 | info->preview.xsize = dec->metadata.m.preview_size.xsize(); |
2133 | 145 | info->preview.ysize = dec->metadata.m.preview_size.ysize(); |
2134 | 145 | } |
2135 | | |
2136 | 11.3k | if (info->have_animation) { |
2137 | 236 | info->animation.tps_numerator = dec->metadata.m.animation.tps_numerator; |
2138 | 236 | info->animation.tps_denominator = |
2139 | 236 | dec->metadata.m.animation.tps_denominator; |
2140 | 236 | info->animation.num_loops = dec->metadata.m.animation.num_loops; |
2141 | 236 | info->animation.have_timecodes = |
2142 | 236 | TO_JXL_BOOL(dec->metadata.m.animation.have_timecodes); |
2143 | 236 | } |
2144 | | |
2145 | 11.3k | if (meta.have_intrinsic_size) { |
2146 | 127 | info->intrinsic_xsize = dec->metadata.m.intrinsic_size.xsize(); |
2147 | 127 | info->intrinsic_ysize = dec->metadata.m.intrinsic_size.ysize(); |
2148 | 11.2k | } else { |
2149 | 11.2k | info->intrinsic_xsize = info->xsize; |
2150 | 11.2k | info->intrinsic_ysize = info->ysize; |
2151 | 11.2k | } |
2152 | 11.3k | } |
2153 | | |
2154 | 11.3k | return JXL_DEC_SUCCESS; |
2155 | 11.3k | } |
2156 | | |
2157 | | JxlDecoderStatus JxlDecoderGetExtraChannelInfo(const JxlDecoder* dec, |
2158 | | size_t index, |
2159 | 0 | JxlExtraChannelInfo* info) { |
2160 | 0 | if (!dec->got_basic_info) return JXL_DEC_NEED_MORE_INPUT; |
2161 | | |
2162 | 0 | const std::vector<jxl::ExtraChannelInfo>& channels = |
2163 | 0 | dec->metadata.m.extra_channel_info; |
2164 | |
|
2165 | 0 | if (index >= channels.size()) return JXL_DEC_ERROR; // out of bounds |
2166 | 0 | const jxl::ExtraChannelInfo& channel = channels[index]; |
2167 | |
|
2168 | 0 | info->type = static_cast<JxlExtraChannelType>(channel.type); |
2169 | 0 | info->bits_per_sample = channel.bit_depth.bits_per_sample; |
2170 | 0 | info->exponent_bits_per_sample = |
2171 | 0 | channel.bit_depth.floating_point_sample |
2172 | 0 | ? channel.bit_depth.exponent_bits_per_sample |
2173 | 0 | : 0; |
2174 | 0 | info->dim_shift = channel.dim_shift; |
2175 | 0 | info->name_length = channel.name.size(); |
2176 | 0 | info->alpha_premultiplied = TO_JXL_BOOL(channel.alpha_associated); |
2177 | 0 | info->spot_color[0] = channel.spot_color[0]; |
2178 | 0 | info->spot_color[1] = channel.spot_color[1]; |
2179 | 0 | info->spot_color[2] = channel.spot_color[2]; |
2180 | 0 | info->spot_color[3] = channel.spot_color[3]; |
2181 | 0 | info->cfa_channel = channel.cfa_channel; |
2182 | |
|
2183 | 0 | return JXL_DEC_SUCCESS; |
2184 | 0 | } |
2185 | | |
2186 | | JxlDecoderStatus JxlDecoderGetExtraChannelName(const JxlDecoder* dec, |
2187 | | size_t index, char* name, |
2188 | 0 | size_t size) { |
2189 | 0 | if (!dec->got_basic_info) return JXL_DEC_NEED_MORE_INPUT; |
2190 | | |
2191 | 0 | const std::vector<jxl::ExtraChannelInfo>& channels = |
2192 | 0 | dec->metadata.m.extra_channel_info; |
2193 | |
|
2194 | 0 | if (index >= channels.size()) return JXL_DEC_ERROR; // out of bounds |
2195 | 0 | const jxl::ExtraChannelInfo& channel = channels[index]; |
2196 | | |
2197 | | // Also need null-termination character |
2198 | 0 | if (channel.name.size() + 1 > size) return JXL_DEC_ERROR; |
2199 | | |
2200 | 0 | memcpy(name, channel.name.c_str(), channel.name.size() + 1); |
2201 | |
|
2202 | 0 | return JXL_DEC_SUCCESS; |
2203 | 0 | } |
2204 | | |
2205 | | namespace { |
2206 | | |
2207 | | // Gets the jxl::ColorEncoding for the desired target, and checks errors. |
2208 | | // Returns the object regardless of whether the actual color space is in ICC, |
2209 | | // but ensures that if the color encoding is not the encoding from the |
2210 | | // codestream header metadata, it cannot require ICC profile. |
2211 | | JxlDecoderStatus GetColorEncodingForTarget( |
2212 | | const JxlDecoder* dec, JxlColorProfileTarget target, |
2213 | 32.4k | const jxl::ColorEncoding** encoding) { |
2214 | 32.4k | if (!dec->got_all_headers) return JXL_DEC_NEED_MORE_INPUT; |
2215 | 32.4k | *encoding = nullptr; |
2216 | 32.4k | if (target == JXL_COLOR_PROFILE_TARGET_DATA && dec->metadata.m.xyb_encoded) { |
2217 | 7.41k | *encoding = &dec->passes_state->output_encoding_info.color_encoding; |
2218 | 24.9k | } else { |
2219 | 24.9k | *encoding = &dec->metadata.m.color_encoding; |
2220 | 24.9k | } |
2221 | 32.4k | return JXL_DEC_SUCCESS; |
2222 | 32.4k | } |
2223 | | } // namespace |
2224 | | |
2225 | | JxlDecoderStatus JxlDecoderGetColorAsEncodedProfile( |
2226 | | const JxlDecoder* dec, JxlColorProfileTarget target, |
2227 | 8.10k | JxlColorEncoding* color_encoding) { |
2228 | 8.10k | const jxl::ColorEncoding* jxl_color_encoding = nullptr; |
2229 | 8.10k | JxlDecoderStatus status = |
2230 | 8.10k | GetColorEncodingForTarget(dec, target, &jxl_color_encoding); |
2231 | 8.10k | if (status) return status; |
2232 | | |
2233 | 8.10k | if (jxl_color_encoding->WantICC()) |
2234 | 1 | return JXL_DEC_ERROR; // Indicate no encoded profile available. |
2235 | | |
2236 | 8.10k | if (color_encoding) { |
2237 | 8.10k | *color_encoding = jxl_color_encoding->ToExternal(); |
2238 | 8.10k | } |
2239 | | |
2240 | 8.10k | return JXL_DEC_SUCCESS; |
2241 | 8.10k | } |
2242 | | |
2243 | | JxlDecoderStatus JxlDecoderGetICCProfileSize(const JxlDecoder* dec, |
2244 | | JxlColorProfileTarget target, |
2245 | 16.2k | size_t* size) { |
2246 | 16.2k | const jxl::ColorEncoding* jxl_color_encoding = nullptr; |
2247 | 16.2k | JxlDecoderStatus status = |
2248 | 16.2k | GetColorEncodingForTarget(dec, target, &jxl_color_encoding); |
2249 | 16.2k | if (status != JXL_DEC_SUCCESS) return status; |
2250 | | |
2251 | 16.2k | if (jxl_color_encoding->WantICC()) { |
2252 | 7 | jxl::ColorSpace color_space = |
2253 | 7 | dec->metadata.m.color_encoding.GetColorSpace(); |
2254 | 7 | if (color_space == jxl::ColorSpace::kUnknown || |
2255 | 7 | color_space == jxl::ColorSpace::kXYB) { |
2256 | | // This indicates there's no ICC profile available |
2257 | | // TODO(lode): for the XYB case, do we want to craft an ICC profile that |
2258 | | // represents XYB as an RGB profile? It may be possible, but not with |
2259 | | // only 1D transfer functions. |
2260 | 3 | return JXL_DEC_ERROR; |
2261 | 3 | } |
2262 | 7 | } |
2263 | | |
2264 | 16.2k | if (size) { |
2265 | 16.2k | *size = jxl_color_encoding->ICC().size(); |
2266 | 16.2k | } |
2267 | | |
2268 | 16.2k | return JXL_DEC_SUCCESS; |
2269 | 16.2k | } |
2270 | | |
2271 | | JxlDecoderStatus JxlDecoderGetColorAsICCProfile(const JxlDecoder* dec, |
2272 | | JxlColorProfileTarget target, |
2273 | | uint8_t* icc_profile, |
2274 | 8.10k | size_t size) { |
2275 | 8.10k | size_t wanted_size; |
2276 | | // This also checks the NEED_MORE_INPUT and the unknown/xyb cases |
2277 | 8.10k | JxlDecoderStatus status = |
2278 | 8.10k | JxlDecoderGetICCProfileSize(dec, target, &wanted_size); |
2279 | 8.10k | if (status != JXL_DEC_SUCCESS) return status; |
2280 | 8.10k | if (size < wanted_size) return JXL_API_ERROR("ICC profile output too small"); |
2281 | | |
2282 | 8.10k | const jxl::ColorEncoding* jxl_color_encoding = nullptr; |
2283 | 8.10k | status = GetColorEncodingForTarget(dec, target, &jxl_color_encoding); |
2284 | 8.10k | if (status != JXL_DEC_SUCCESS) return status; |
2285 | | |
2286 | 8.10k | memcpy(icc_profile, jxl_color_encoding->ICC().data(), |
2287 | 8.10k | jxl_color_encoding->ICC().size()); |
2288 | | |
2289 | 8.10k | return JXL_DEC_SUCCESS; |
2290 | 8.10k | } |
2291 | | |
2292 | | namespace { |
2293 | | |
2294 | | // Returns the amount of bits needed for getting memory buffer size, and does |
2295 | | // all error checking required for size checking and format validity. |
2296 | | JxlDecoderStatus PrepareSizeCheck(const JxlDecoder* dec, |
2297 | 8.17k | const JxlPixelFormat* format, size_t* bits) { |
2298 | 8.17k | if (!dec->got_basic_info) { |
2299 | | // Don't know image dimensions yet, cannot check for valid size. |
2300 | 0 | return JXL_DEC_NEED_MORE_INPUT; |
2301 | 0 | } |
2302 | 8.17k | if (!dec->coalescing && |
2303 | 8.17k | (!dec->frame_header || dec->frame_stage == FrameStage::kHeader)) { |
2304 | 0 | return JXL_API_ERROR("Don't know frame dimensions yet"); |
2305 | 0 | } |
2306 | 8.17k | if (format->num_channels > 4) { |
2307 | 0 | return JXL_API_ERROR("More than 4 channels not supported"); |
2308 | 0 | } |
2309 | | |
2310 | 8.17k | *bits = BitsPerChannel(format->data_type); |
2311 | | |
2312 | 8.17k | if (*bits == 0) { |
2313 | 0 | return JXL_API_ERROR("Invalid/unsupported data type"); |
2314 | 0 | } |
2315 | | |
2316 | 8.17k | return JXL_DEC_SUCCESS; |
2317 | 8.17k | } |
2318 | | |
2319 | | } // namespace |
2320 | | |
2321 | 0 | size_t JxlDecoderGetIntendedDownsamplingRatio(JxlDecoder* dec) { |
2322 | 0 | return dec->downsampling_target; |
2323 | 0 | } |
2324 | | |
2325 | 0 | JxlDecoderStatus JxlDecoderFlushImage(JxlDecoder* dec) { |
2326 | 0 | if (!dec->image_out_buffer_set) return JXL_DEC_ERROR; |
2327 | 0 | if (dec->frame_stage != FrameStage::kFull) { |
2328 | 0 | return JXL_DEC_ERROR; |
2329 | 0 | } |
2330 | 0 | JXL_DASSERT(dec->frame_dec); |
2331 | 0 | if (!dec->frame_dec->HasDecodedDC()) { |
2332 | | // FrameDecoder::Flush currently requires DC to have been decoded already |
2333 | | // to work correctly. |
2334 | 0 | return JXL_DEC_ERROR; |
2335 | 0 | } |
2336 | | |
2337 | 0 | if (!dec->frame_dec->Flush()) { |
2338 | 0 | return JXL_DEC_ERROR; |
2339 | 0 | } |
2340 | | |
2341 | 0 | return JXL_DEC_SUCCESS; |
2342 | 0 | } |
2343 | | |
2344 | | JXL_EXPORT JxlDecoderStatus JxlDecoderSetCms(JxlDecoder* dec, |
2345 | 0 | const JxlCmsInterface cms) { |
2346 | 0 | if (!dec->passes_state) { |
2347 | 0 | dec->passes_state = |
2348 | 0 | jxl::make_unique<jxl::PassesDecoderState>(&dec->memory_manager); |
2349 | 0 | } |
2350 | 0 | dec->passes_state->output_encoding_info.color_management_system = cms; |
2351 | 0 | dec->passes_state->output_encoding_info.cms_set = true; |
2352 | 0 | return JXL_DEC_SUCCESS; |
2353 | 0 | } |
2354 | | |
2355 | | static JxlDecoderStatus GetMinSize(const JxlDecoder* dec, |
2356 | | const JxlPixelFormat* format, |
2357 | | size_t num_channels, size_t* min_size, |
2358 | 8.17k | bool preview) { |
2359 | 8.17k | size_t bits; |
2360 | 8.17k | JxlDecoderStatus status = PrepareSizeCheck(dec, format, &bits); |
2361 | 8.17k | if (status != JXL_DEC_SUCCESS) return status; |
2362 | 8.17k | size_t xsize; |
2363 | 8.17k | size_t ysize; |
2364 | 8.17k | if (preview) { |
2365 | 0 | xsize = dec->metadata.oriented_preview_xsize(dec->keep_orientation); |
2366 | 0 | ysize = dec->metadata.oriented_preview_ysize(dec->keep_orientation); |
2367 | 8.17k | } else { |
2368 | 8.17k | GetCurrentDimensions(dec, xsize, ysize); |
2369 | 8.17k | } |
2370 | 8.17k | if (num_channels == 0) num_channels = format->num_channels; |
2371 | 8.17k | size_t row_size = |
2372 | 8.17k | jxl::DivCeil(xsize * num_channels * bits, jxl::kBitsPerByte); |
2373 | 8.17k | size_t last_row_size = row_size; |
2374 | 8.17k | if (format->align > 1) { |
2375 | 0 | row_size = jxl::DivCeil(row_size, format->align) * format->align; |
2376 | 0 | } |
2377 | 8.17k | *min_size = row_size * (ysize - 1) + last_row_size; |
2378 | 8.17k | return JXL_DEC_SUCCESS; |
2379 | 8.17k | } |
2380 | | |
2381 | | JXL_EXPORT JxlDecoderStatus JxlDecoderPreviewOutBufferSize( |
2382 | 0 | const JxlDecoder* dec, const JxlPixelFormat* format, size_t* size) { |
2383 | 0 | if (format->num_channels < 3 && |
2384 | 0 | !dec->image_metadata.color_encoding.IsGray()) { |
2385 | 0 | return JXL_API_ERROR("Number of channels is too low for color output"); |
2386 | 0 | } |
2387 | 0 | return GetMinSize(dec, format, 0, size, true); |
2388 | 0 | } |
2389 | | |
2390 | | JXL_EXPORT JxlDecoderStatus JxlDecoderSetPreviewOutBuffer( |
2391 | 0 | JxlDecoder* dec, const JxlPixelFormat* format, void* buffer, size_t size) { |
2392 | 0 | if (!dec->got_basic_info || !dec->metadata.m.have_preview || |
2393 | 0 | !(dec->orig_events_wanted & JXL_DEC_PREVIEW_IMAGE)) { |
2394 | 0 | return JXL_API_ERROR("No preview out buffer needed at this time"); |
2395 | 0 | } |
2396 | 0 | if (format->num_channels < 3 && |
2397 | 0 | !dec->image_metadata.color_encoding.IsGray()) { |
2398 | 0 | return JXL_API_ERROR("Number of channels is too low for color output"); |
2399 | 0 | } |
2400 | | |
2401 | 0 | size_t min_size; |
2402 | | // This also checks whether the format is valid and supported and basic info |
2403 | | // is available. |
2404 | 0 | JxlDecoderStatus status = |
2405 | 0 | JxlDecoderPreviewOutBufferSize(dec, format, &min_size); |
2406 | 0 | if (status != JXL_DEC_SUCCESS) return status; |
2407 | | |
2408 | 0 | if (size < min_size) return JXL_DEC_ERROR; |
2409 | | |
2410 | 0 | dec->image_out_buffer_set = true; |
2411 | 0 | dec->image_out_buffer = buffer; |
2412 | 0 | dec->image_out_size = size; |
2413 | 0 | dec->image_out_format = *format; |
2414 | |
|
2415 | 0 | return JXL_DEC_SUCCESS; |
2416 | 0 | } |
2417 | | |
2418 | | JXL_EXPORT JxlDecoderStatus JxlDecoderImageOutBufferSize( |
2419 | 8.17k | const JxlDecoder* dec, const JxlPixelFormat* format, size_t* size) { |
2420 | 8.17k | if (format->num_channels < 3 && |
2421 | 8.17k | !dec->image_metadata.color_encoding.IsGray()) { |
2422 | 0 | return JXL_API_ERROR("Number of channels is too low for color output"); |
2423 | 0 | } |
2424 | | |
2425 | 8.17k | return GetMinSize(dec, format, 0, size, false); |
2426 | 8.17k | } |
2427 | | |
2428 | | JxlDecoderStatus JxlDecoderSetImageOutBuffer(JxlDecoder* dec, |
2429 | | const JxlPixelFormat* format, |
2430 | 4.23k | void* buffer, size_t size) { |
2431 | 4.23k | if (!dec->got_basic_info || !(dec->orig_events_wanted & JXL_DEC_FULL_IMAGE)) { |
2432 | 0 | return JXL_API_ERROR("No image out buffer needed at this time"); |
2433 | 0 | } |
2434 | 4.23k | if (dec->image_out_buffer_set && !!dec->image_out_run_callback) { |
2435 | 0 | return JXL_API_ERROR( |
2436 | 0 | "Cannot change from image out callback to image out buffer"); |
2437 | 0 | } |
2438 | 4.23k | if (format->num_channels < 3 && |
2439 | 4.23k | !dec->image_metadata.color_encoding.IsGray()) { |
2440 | 0 | return JXL_API_ERROR("Number of channels is too low for color output"); |
2441 | 0 | } |
2442 | 4.23k | size_t min_size; |
2443 | | // This also checks whether the format is valid and supported and basic info |
2444 | | // is available. |
2445 | 4.23k | JxlDecoderStatus status = |
2446 | 4.23k | JxlDecoderImageOutBufferSize(dec, format, &min_size); |
2447 | 4.23k | if (status != JXL_DEC_SUCCESS) return status; |
2448 | | |
2449 | 4.23k | if (size < min_size) return JXL_DEC_ERROR; |
2450 | | |
2451 | 4.23k | dec->image_out_buffer_set = true; |
2452 | 4.23k | dec->image_out_buffer = buffer; |
2453 | 4.23k | dec->image_out_size = size; |
2454 | 4.23k | dec->image_out_format = *format; |
2455 | | |
2456 | 4.23k | return JXL_DEC_SUCCESS; |
2457 | 4.23k | } |
2458 | | |
2459 | | JxlDecoderStatus JxlDecoderExtraChannelBufferSize(const JxlDecoder* dec, |
2460 | | const JxlPixelFormat* format, |
2461 | | size_t* size, |
2462 | 0 | uint32_t index) { |
2463 | 0 | if (!dec->got_basic_info || !(dec->orig_events_wanted & JXL_DEC_FULL_IMAGE)) { |
2464 | 0 | return JXL_API_ERROR("No extra channel buffer needed at this time"); |
2465 | 0 | } |
2466 | | |
2467 | 0 | if (index >= dec->metadata.m.num_extra_channels) { |
2468 | 0 | return JXL_API_ERROR("Invalid extra channel index"); |
2469 | 0 | } |
2470 | | |
2471 | 0 | return GetMinSize(dec, format, 1, size, false); |
2472 | 0 | } |
2473 | | |
2474 | | JxlDecoderStatus JxlDecoderSetExtraChannelBuffer(JxlDecoder* dec, |
2475 | | const JxlPixelFormat* format, |
2476 | | void* buffer, size_t size, |
2477 | 0 | uint32_t index) { |
2478 | 0 | size_t min_size; |
2479 | | // This also checks whether the format and index are valid and supported and |
2480 | | // basic info is available. |
2481 | 0 | JxlDecoderStatus status = |
2482 | 0 | JxlDecoderExtraChannelBufferSize(dec, format, &min_size, index); |
2483 | 0 | if (status != JXL_DEC_SUCCESS) return status; |
2484 | | |
2485 | 0 | if (size < min_size) return JXL_DEC_ERROR; |
2486 | | |
2487 | 0 | if (dec->extra_channel_output.size() <= index) { |
2488 | 0 | dec->extra_channel_output.resize(dec->metadata.m.num_extra_channels, |
2489 | 0 | {{}, nullptr, 0}); |
2490 | 0 | } |
2491 | | // Guaranteed correct thanks to check in JxlDecoderExtraChannelBufferSize. |
2492 | 0 | JXL_DASSERT(dec->extra_channel_output.size() > index); |
2493 | |
|
2494 | 0 | dec->extra_channel_output[index].format = *format; |
2495 | 0 | dec->extra_channel_output[index].format.num_channels = 1; |
2496 | 0 | dec->extra_channel_output[index].buffer = buffer; |
2497 | 0 | dec->extra_channel_output[index].buffer_size = size; |
2498 | |
|
2499 | 0 | return JXL_DEC_SUCCESS; |
2500 | 0 | } |
2501 | | |
2502 | | JxlDecoderStatus JxlDecoderSetImageOutCallback(JxlDecoder* dec, |
2503 | | const JxlPixelFormat* format, |
2504 | | JxlImageOutCallback callback, |
2505 | 0 | void* opaque) { |
2506 | 0 | dec->simple_image_out_callback.callback = callback; |
2507 | 0 | dec->simple_image_out_callback.opaque = opaque; |
2508 | 0 | const auto init_callback = |
2509 | 0 | +[](void* init_opaque, size_t num_threads, size_t num_pixels_per_thread) { |
2510 | | // No initialization to do, just reuse init_opaque as run_opaque. |
2511 | 0 | return init_opaque; |
2512 | 0 | }; |
2513 | 0 | const auto run_callback = |
2514 | 0 | +[](void* run_opaque, size_t thread_id, size_t x, size_t y, |
2515 | 0 | size_t num_pixels, const void* pixels) { |
2516 | 0 | const auto* const simple_callback = |
2517 | 0 | static_cast<const JxlDecoder::SimpleImageOutCallback*>(run_opaque); |
2518 | 0 | simple_callback->callback(simple_callback->opaque, x, y, num_pixels, |
2519 | 0 | pixels); |
2520 | 0 | }; |
2521 | 0 | const auto destroy_callback = +[](void* run_opaque) {}; |
2522 | 0 | return JxlDecoderSetMultithreadedImageOutCallback( |
2523 | 0 | dec, format, init_callback, run_callback, |
2524 | 0 | /*destroy_callback=*/destroy_callback, &dec->simple_image_out_callback); |
2525 | 0 | } |
2526 | | |
2527 | | JxlDecoderStatus JxlDecoderSetMultithreadedImageOutCallback( |
2528 | | JxlDecoder* dec, const JxlPixelFormat* format, |
2529 | | JxlImageOutInitCallback init_callback, JxlImageOutRunCallback run_callback, |
2530 | 0 | JxlImageOutDestroyCallback destroy_callback, void* init_opaque) { |
2531 | 0 | if (dec->image_out_buffer_set && !!dec->image_out_buffer) { |
2532 | 0 | return JXL_API_ERROR( |
2533 | 0 | "Cannot change from image out buffer to image out callback"); |
2534 | 0 | } |
2535 | | |
2536 | 0 | if (init_callback == nullptr || run_callback == nullptr || |
2537 | 0 | destroy_callback == nullptr) { |
2538 | 0 | return JXL_API_ERROR("All callbacks are required"); |
2539 | 0 | } |
2540 | | |
2541 | | // Perform error checking for invalid format. |
2542 | 0 | size_t bits_sink; |
2543 | 0 | JxlDecoderStatus status = PrepareSizeCheck(dec, format, &bits_sink); |
2544 | 0 | if (status != JXL_DEC_SUCCESS) return status; |
2545 | | |
2546 | 0 | dec->image_out_buffer_set = true; |
2547 | 0 | dec->image_out_init_callback = init_callback; |
2548 | 0 | dec->image_out_run_callback = run_callback; |
2549 | 0 | dec->image_out_destroy_callback = destroy_callback; |
2550 | 0 | dec->image_out_init_opaque = init_opaque; |
2551 | 0 | dec->image_out_format = *format; |
2552 | |
|
2553 | 0 | return JXL_DEC_SUCCESS; |
2554 | 0 | } |
2555 | | |
2556 | | JxlDecoderStatus JxlDecoderGetFrameHeader(const JxlDecoder* dec, |
2557 | 0 | JxlFrameHeader* header) { |
2558 | 0 | if (!dec->frame_header || dec->frame_stage == FrameStage::kHeader) { |
2559 | 0 | return JXL_API_ERROR("no frame header available"); |
2560 | 0 | } |
2561 | 0 | const auto& metadata = dec->metadata.m; |
2562 | 0 | memset(header, 0, sizeof(*header)); |
2563 | 0 | if (metadata.have_animation) { |
2564 | 0 | header->duration = dec->frame_header->animation_frame.duration; |
2565 | 0 | if (metadata.animation.have_timecodes) { |
2566 | 0 | header->timecode = dec->frame_header->animation_frame.timecode; |
2567 | 0 | } |
2568 | 0 | } |
2569 | 0 | header->name_length = dec->frame_header->name.size(); |
2570 | 0 | header->is_last = TO_JXL_BOOL(dec->frame_header->is_last); |
2571 | 0 | size_t xsize; |
2572 | 0 | size_t ysize; |
2573 | 0 | GetCurrentDimensions(dec, xsize, ysize); |
2574 | 0 | header->layer_info.xsize = xsize; |
2575 | 0 | header->layer_info.ysize = ysize; |
2576 | 0 | if (!dec->coalescing && dec->frame_header->custom_size_or_origin) { |
2577 | 0 | header->layer_info.crop_x0 = dec->frame_header->frame_origin.x0; |
2578 | 0 | header->layer_info.crop_y0 = dec->frame_header->frame_origin.y0; |
2579 | 0 | header->layer_info.have_crop = JXL_TRUE; |
2580 | 0 | } else { |
2581 | 0 | header->layer_info.crop_x0 = 0; |
2582 | 0 | header->layer_info.crop_y0 = 0; |
2583 | 0 | header->layer_info.have_crop = JXL_FALSE; |
2584 | 0 | } |
2585 | 0 | if (!dec->keep_orientation && !dec->coalescing) { |
2586 | | // orient the crop offset |
2587 | 0 | size_t W = dec->metadata.oriented_xsize(false); |
2588 | 0 | size_t H = dec->metadata.oriented_ysize(false); |
2589 | 0 | if (metadata.orientation > 4) { |
2590 | 0 | std::swap(header->layer_info.crop_x0, header->layer_info.crop_y0); |
2591 | 0 | } |
2592 | 0 | size_t o = (metadata.orientation - 1) & 3; |
2593 | 0 | if (o > 0 && o < 3) { |
2594 | 0 | header->layer_info.crop_x0 = W - xsize - header->layer_info.crop_x0; |
2595 | 0 | } |
2596 | 0 | if (o > 1) { |
2597 | 0 | header->layer_info.crop_y0 = H - ysize - header->layer_info.crop_y0; |
2598 | 0 | } |
2599 | 0 | } |
2600 | 0 | if (dec->coalescing) { |
2601 | 0 | header->layer_info.blend_info.blendmode = JXL_BLEND_REPLACE; |
2602 | 0 | header->layer_info.blend_info.source = 0; |
2603 | 0 | header->layer_info.blend_info.alpha = 0; |
2604 | 0 | header->layer_info.blend_info.clamp = JXL_FALSE; |
2605 | 0 | header->layer_info.save_as_reference = 0; |
2606 | 0 | } else { |
2607 | 0 | header->layer_info.blend_info.blendmode = |
2608 | 0 | static_cast<JxlBlendMode>(dec->frame_header->blending_info.mode); |
2609 | 0 | header->layer_info.blend_info.source = |
2610 | 0 | dec->frame_header->blending_info.source; |
2611 | 0 | header->layer_info.blend_info.alpha = |
2612 | 0 | dec->frame_header->blending_info.alpha_channel; |
2613 | 0 | header->layer_info.blend_info.clamp = |
2614 | 0 | TO_JXL_BOOL(dec->frame_header->blending_info.clamp); |
2615 | 0 | header->layer_info.save_as_reference = dec->frame_header->save_as_reference; |
2616 | 0 | } |
2617 | 0 | return JXL_DEC_SUCCESS; |
2618 | 0 | } |
2619 | | |
2620 | | JxlDecoderStatus JxlDecoderGetExtraChannelBlendInfo(const JxlDecoder* dec, |
2621 | | size_t index, |
2622 | 0 | JxlBlendInfo* blend_info) { |
2623 | 0 | if (!dec->frame_header || dec->frame_stage == FrameStage::kHeader) { |
2624 | 0 | return JXL_API_ERROR("no frame header available"); |
2625 | 0 | } |
2626 | 0 | const auto& metadata = dec->metadata.m; |
2627 | 0 | if (index >= metadata.num_extra_channels) { |
2628 | 0 | return JXL_API_ERROR("Invalid extra channel index"); |
2629 | 0 | } |
2630 | 0 | blend_info->blendmode = static_cast<JxlBlendMode>( |
2631 | 0 | dec->frame_header->extra_channel_blending_info[index].mode); |
2632 | 0 | blend_info->source = |
2633 | 0 | dec->frame_header->extra_channel_blending_info[index].source; |
2634 | 0 | blend_info->alpha = |
2635 | 0 | dec->frame_header->extra_channel_blending_info[index].alpha_channel; |
2636 | 0 | blend_info->clamp = |
2637 | 0 | TO_JXL_BOOL(dec->frame_header->extra_channel_blending_info[index].clamp); |
2638 | 0 | return JXL_DEC_SUCCESS; |
2639 | 0 | } |
2640 | | |
2641 | | JxlDecoderStatus JxlDecoderGetFrameName(const JxlDecoder* dec, char* name, |
2642 | 0 | size_t size) { |
2643 | 0 | if (!dec->frame_header || dec->frame_stage == FrameStage::kHeader) { |
2644 | 0 | return JXL_API_ERROR("no frame header available"); |
2645 | 0 | } |
2646 | 0 | if (size < dec->frame_header->name.size() + 1) { |
2647 | 0 | return JXL_API_ERROR("too small frame name output buffer"); |
2648 | 0 | } |
2649 | 0 | memcpy(name, dec->frame_header->name.c_str(), |
2650 | 0 | dec->frame_header->name.size() + 1); |
2651 | |
|
2652 | 0 | return JXL_DEC_SUCCESS; |
2653 | 0 | } |
2654 | | |
2655 | | JxlDecoderStatus JxlDecoderSetPreferredColorProfile( |
2656 | 0 | JxlDecoder* dec, const JxlColorEncoding* color_encoding) { |
2657 | 0 | return JxlDecoderSetOutputColorProfile(dec, color_encoding, |
2658 | 0 | /*icc_data=*/nullptr, /*icc_size=*/0); |
2659 | 0 | } |
2660 | | |
2661 | | JxlDecoderStatus JxlDecoderSetOutputColorProfile( |
2662 | | JxlDecoder* dec, const JxlColorEncoding* color_encoding, |
2663 | 0 | const uint8_t* icc_data, size_t icc_size) { |
2664 | 0 | if ((color_encoding != nullptr) && (icc_data != nullptr)) { |
2665 | 0 | return JXL_API_ERROR("cannot set both color_encoding and icc_data"); |
2666 | 0 | } |
2667 | 0 | if ((color_encoding == nullptr) && (icc_data == nullptr)) { |
2668 | 0 | return JXL_API_ERROR("one of color_encoding and icc_data must be set"); |
2669 | 0 | } |
2670 | 0 | if (!dec->got_all_headers) { |
2671 | 0 | return JXL_API_ERROR("color info not yet available"); |
2672 | 0 | } |
2673 | 0 | if (dec->post_headers) { |
2674 | 0 | return JXL_API_ERROR("too late to set the color encoding"); |
2675 | 0 | } |
2676 | 0 | if ((!dec->passes_state->output_encoding_info.cms_set) && |
2677 | 0 | (icc_data != nullptr)) { |
2678 | 0 | return JXL_API_ERROR( |
2679 | 0 | "must set color management system via JxlDecoderSetCms"); |
2680 | 0 | } |
2681 | 0 | auto& output_encoding = dec->passes_state->output_encoding_info; |
2682 | 0 | if (color_encoding) { |
2683 | 0 | if (dec->image_metadata.color_encoding.IsGray() && |
2684 | 0 | color_encoding->color_space != JXL_COLOR_SPACE_GRAY && |
2685 | 0 | dec->image_out_buffer_set && dec->image_out_format.num_channels < 3) { |
2686 | 0 | return JXL_API_ERROR("Number of channels is too low for color output"); |
2687 | 0 | } |
2688 | 0 | if (color_encoding->color_space == JXL_COLOR_SPACE_UNKNOWN) { |
2689 | 0 | return JXL_API_ERROR("Unknown output colorspace"); |
2690 | 0 | } |
2691 | 0 | jxl::ColorEncoding c_out; |
2692 | 0 | JXL_API_RETURN_IF_ERROR(c_out.FromExternal(*color_encoding)); |
2693 | 0 | JXL_API_RETURN_IF_ERROR(!c_out.ICC().empty()); |
2694 | 0 | if (!c_out.SameColorEncoding(output_encoding.color_encoding)) { |
2695 | 0 | JXL_API_RETURN_IF_ERROR(output_encoding.MaybeSetColorEncoding(c_out)); |
2696 | 0 | dec->image_metadata.color_encoding = output_encoding.color_encoding; |
2697 | 0 | } |
2698 | 0 | return JXL_DEC_SUCCESS; |
2699 | 0 | } |
2700 | | // icc_data != nullptr |
2701 | | // TODO(firsching): implement setting output color profile from icc_data. |
2702 | 0 | jxl::ColorEncoding c_dst; |
2703 | 0 | std::vector<uint8_t> padded_icc; |
2704 | 0 | padded_icc.assign(icc_data, icc_data + icc_size); |
2705 | 0 | if (!c_dst.SetICC(std::move(padded_icc), |
2706 | 0 | &output_encoding.color_management_system)) { |
2707 | 0 | return JXL_API_ERROR( |
2708 | 0 | "setting output color profile from icc_data not yet implemented."); |
2709 | 0 | } |
2710 | 0 | JXL_API_RETURN_IF_ERROR( |
2711 | 0 | static_cast<int>(output_encoding.MaybeSetColorEncoding(c_dst))); |
2712 | |
|
2713 | 0 | return JXL_DEC_SUCCESS; |
2714 | 0 | } |
2715 | | |
2716 | | JxlDecoderStatus JxlDecoderSetDesiredIntensityTarget( |
2717 | 0 | JxlDecoder* dec, float desired_intensity_target) { |
2718 | 0 | if (desired_intensity_target < 0) { |
2719 | 0 | return JXL_API_ERROR("negative intensity target requested"); |
2720 | 0 | } |
2721 | 0 | dec->desired_intensity_target = desired_intensity_target; |
2722 | 0 | return JXL_DEC_SUCCESS; |
2723 | 0 | } |
2724 | | |
2725 | | JxlDecoderStatus JxlDecoderSetBoxBuffer(JxlDecoder* dec, uint8_t* data, |
2726 | 70 | size_t size) { |
2727 | 70 | if (dec->box_out_buffer_set) { |
2728 | 0 | return JXL_API_ERROR("must release box buffer before setting it again"); |
2729 | 0 | } |
2730 | 70 | if (!dec->box_event) { |
2731 | 0 | return JXL_API_ERROR("can only set box buffer after box event"); |
2732 | 0 | } |
2733 | | |
2734 | 70 | dec->box_out_buffer_set = true; |
2735 | 70 | dec->box_out_buffer_set_current_box = true; |
2736 | 70 | dec->box_out_buffer = data; |
2737 | 70 | dec->box_out_buffer_size = size; |
2738 | 70 | dec->box_out_buffer_pos = 0; |
2739 | 70 | return JXL_DEC_SUCCESS; |
2740 | 70 | } |
2741 | | |
2742 | 15.0k | size_t JxlDecoderReleaseBoxBuffer(JxlDecoder* dec) { |
2743 | 15.0k | if (!dec->box_out_buffer_set) { |
2744 | 15.0k | return 0; |
2745 | 15.0k | } |
2746 | 70 | size_t result = dec->box_out_buffer_size - dec->box_out_buffer_pos; |
2747 | 70 | dec->box_out_buffer_set = false; |
2748 | 70 | dec->box_out_buffer = nullptr; |
2749 | 70 | dec->box_out_buffer_size = 0; |
2750 | 70 | if (!dec->box_out_buffer_set_current_box) { |
2751 | 67 | dec->box_out_buffer_begin = 0; |
2752 | 67 | } else { |
2753 | 3 | dec->box_out_buffer_begin += dec->box_out_buffer_pos; |
2754 | 3 | } |
2755 | 70 | dec->box_out_buffer_set_current_box = false; |
2756 | 70 | return result; |
2757 | 15.0k | } |
2758 | | |
2759 | | JxlDecoderStatus JxlDecoderSetDecompressBoxes(JxlDecoder* dec, |
2760 | 0 | JXL_BOOL decompress) { |
2761 | | // TODO(lode): return error if libbrotli is not compiled in the jxl decoding |
2762 | | // library |
2763 | 0 | dec->decompress_boxes = FROM_JXL_BOOL(decompress); |
2764 | 0 | return JXL_DEC_SUCCESS; |
2765 | 0 | } |
2766 | | |
2767 | | JxlDecoderStatus JxlDecoderGetBoxType(JxlDecoder* dec, JxlBoxType type, |
2768 | 741 | JXL_BOOL decompressed) { |
2769 | 741 | if (!dec->box_event) { |
2770 | 0 | return JXL_API_ERROR("can only get box info after JXL_DEC_BOX event"); |
2771 | 0 | } |
2772 | 741 | if (decompressed) { |
2773 | 0 | memcpy(type, dec->box_decoded_type, sizeof(dec->box_decoded_type)); |
2774 | 741 | } else { |
2775 | 741 | memcpy(type, dec->box_type, sizeof(dec->box_type)); |
2776 | 741 | } |
2777 | | |
2778 | 741 | return JXL_DEC_SUCCESS; |
2779 | 741 | } |
2780 | | |
2781 | | JxlDecoderStatus JxlDecoderGetBoxSizeRaw(const JxlDecoder* dec, |
2782 | 741 | uint64_t* size) { |
2783 | 741 | if (!dec->box_event) { |
2784 | 0 | return JXL_API_ERROR("can only get box info after JXL_DEC_BOX event"); |
2785 | 0 | } |
2786 | 741 | if (size) { |
2787 | 741 | *size = dec->box_size; |
2788 | 741 | } |
2789 | 741 | return JXL_DEC_SUCCESS; |
2790 | 741 | } |
2791 | | |
2792 | | JxlDecoderStatus JxlDecoderGetBoxSizeContents(const JxlDecoder* dec, |
2793 | 0 | uint64_t* size) { |
2794 | 0 | if (!dec->box_event) { |
2795 | 0 | return JXL_API_ERROR("can only get box info after JXL_DEC_BOX event"); |
2796 | 0 | } |
2797 | 0 | if (size) { |
2798 | 0 | *size = dec->box_contents_size; |
2799 | 0 | } |
2800 | 0 | return JXL_DEC_SUCCESS; |
2801 | 0 | } |
2802 | | |
2803 | | JxlDecoderStatus JxlDecoderSetProgressiveDetail(JxlDecoder* dec, |
2804 | 0 | JxlProgressiveDetail detail) { |
2805 | 0 | if (detail != kDC && detail != kLastPasses && detail != kPasses) { |
2806 | 0 | return JXL_API_ERROR( |
2807 | 0 | "Values other than kDC (%d), kLastPasses (%d) and kPasses (%d), " |
2808 | 0 | "like %d are not implemented.", |
2809 | 0 | kDC, kLastPasses, kPasses, detail); |
2810 | 0 | } |
2811 | 0 | dec->prog_detail = detail; |
2812 | 0 | return JXL_DEC_SUCCESS; |
2813 | 0 | } |
2814 | | |
2815 | | namespace { |
2816 | | |
2817 | | template <typename T> |
2818 | | JxlDecoderStatus VerifyOutputBitDepth(JxlBitDepth bit_depth, const T& metadata, |
2819 | 0 | JxlPixelFormat format) { |
2820 | 0 | uint32_t bits_per_sample = GetBitDepth(bit_depth, metadata, format); |
2821 | 0 | if (bits_per_sample == 0) return JXL_API_ERROR("Invalid output bit depth"); |
2822 | 0 | if (format.data_type == JXL_TYPE_UINT8 && bits_per_sample > 8) { |
2823 | 0 | return JXL_API_ERROR("Invalid bit depth %u for uint8 output", |
2824 | 0 | bits_per_sample); |
2825 | 0 | } else if (format.data_type == JXL_TYPE_UINT16 && bits_per_sample > 16) { |
2826 | 0 | return JXL_API_ERROR("Invalid bit depth %u for uint16 output", |
2827 | 0 | bits_per_sample); |
2828 | 0 | } |
2829 | 0 | return JXL_DEC_SUCCESS; |
2830 | 0 | } |
2831 | | |
2832 | | } // namespace |
2833 | | |
2834 | | JxlDecoderStatus JxlDecoderSetImageOutBitDepth(JxlDecoder* dec, |
2835 | 0 | const JxlBitDepth* bit_depth) { |
2836 | 0 | if (!dec->image_out_buffer_set) { |
2837 | 0 | return JXL_API_ERROR("No image out buffer was set."); |
2838 | 0 | } |
2839 | 0 | JXL_API_RETURN_IF_ERROR( |
2840 | 0 | VerifyOutputBitDepth(*bit_depth, dec->metadata.m, dec->image_out_format)); |
2841 | 0 | dec->image_out_bit_depth = *bit_depth; |
2842 | 0 | return JXL_DEC_SUCCESS; |
2843 | 0 | } |