// Copyright (c) the JPEG XL Project Authors. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file.
// Checks if a + b > size, taking possible integer overflow into account. bool OutOfBounds(size_t a, size_t b, size_t size) {
size_t pos = a + b; if (pos > size) returntrue; if (pos < a) returntrue; // overflow happened returnfalse;
}
JXL_INLINE size_t InitialBasicInfoSizeHint() { // Amount of bytes before the start of the codestream in the container format, // assuming that the codestream is the first box after the signature and // filetype boxes. 12 bytes signature box + 20 bytes filetype box + 16 bytes // codestream box length + name + optional XLBox length. const size_t container_header_size = 48;
// Worst-case amount of bytes for basic info of the JPEG XL codestream header, // that is all information up to and including extra_channel_bits. Up to // around 2 bytes signature + 8 bytes SizeHeader + 31 bytes ColorEncoding + 4 // bytes rest of ImageMetadata + 5 bytes part of ImageMetadata2. // TODO(lode): recompute and update this value when alpha_bits is moved to // extra channels info. const size_t max_codestream_basic_info_size = 50;
// Debug-printing failure macro similar to JXL_FAILURE, but for the status code // JXL_DEC_ERROR #if (JXL_CRASH_ON_ERROR) #define JXL_API_ERROR(format, ...) \
(::jxl::Debug(("%s:%d: " format "\n"), __FILE__, __LINE__, ##__VA_ARGS__), \
::jxl::Abort(), JXL_DEC_ERROR) #else// JXL_CRASH_ON_ERROR #define JXL_API_ERROR(format, ...) \
(((JXL_IS_DEBUG_BUILD) && \
::jxl::Debug(("%s:%d: " format "\n"), __FILE__, __LINE__, ##__VA_ARGS__)), \
JXL_DEC_ERROR) #endif// JXL_CRASH_ON_ERROR
// Error caused by bad input (invalid file) rather than incorrect API usage. // For now there is no way to distinguish these two types of errors yet. #define JXL_INPUT_ERROR(format, ...) JXL_API_ERROR(format, ##__VA_ARGS__)
enumclass DecoderStage : uint32_t {
kInited, // Decoder created, no JxlDecoderProcessInput called yet
kStarted, // Running JxlDecoderProcessInput calls
kCodestreamFinished, // Codestream done, but other boxes could still occur. // This stage can also occur before having seen the // entire codestream if the user didn't subscribe to any // codestream events at all, e.g. only to box events, // or, the user only subscribed to basic info, and only // the header of the codestream was parsed.
kError, // Error occurred, decoder object no longer usable
};
enumclass FrameStage : uint32_t {
kHeader, // Must parse frame header.
kTOC, // Must parse TOC
kFull, // Must parse full pixels
};
enumclass BoxStage : uint32_t {
kHeader, // Parsing box header of the next box, or start of non-container // stream
kFtyp, // The ftyp box
kSkip, // Box whose contents are skipped
kCodestream, // Handling codestream box contents, or non-container stream
kPartialCodestream, // Handling the extra header of partial codestream box
kJpegRecon, // Handling jpeg reconstruction box
};
enumclass JpegReconStage : uint32_t {
kNone, // Not outputting
kSettingMetadata, // Ready to output, must set metadata to the jpeg_data
kOutputting, // Currently outputting the JPEG bytes
};
// For each internal frame, which storage locations it references, and which // storage locations it is stored in, using the bit mask as defined in // FrameDecoder::References and FrameDecoder::SaveAs. typedefstruct FrameRef { int reference; int saved_as;
} FrameRef;
/* Given list of frame references to storage slots, and storage slots in which this frame is saved, computes which frames are required to decode the frame at the given index and any frames after it. The frames on which this depends are returned as a vector of their indices, in no particular order. The given index must be smaller than saved_as.size(), and references.size() must equal saved_as.size(). Any frames beyond saved_as and references are considered unknown future frames and must be treated as if something depends on them.
*/
std::vector<size_t> GetFrameDependencies(size_t index, const std::vector<FrameRef>& refs) {
JXL_DASSERT(index < refs.size());
std::vector<size_t> result;
constexpr size_t kNumStorage = 8;
// value which indicates nothing is stored in this storage slot const size_t invalid = refs.size(); // for each of the 8 storage slots, a vector that translates frame index to // frame stored in this storage slot at this point, that is, the last // frame that was stored in this slot before or at this index.
std::array<std::vector<size_t>, kNumStorage> storage; for (size_t s = 0; s < kNumStorage; ++s) {
storage[s].resize(refs.size()); int mask = 1 << s;
size_t id = invalid; for (size_t i = 0; i < refs.size(); ++i) { if (refs[i].saved_as & mask) {
id = i;
}
storage[s][i] = id;
}
}
// For frames after index, assume they can depend on any of the 8 storage // slots, so push the frame for each stored reference to the stack and result. // All frames after index are treated as having unknown references and with // the possibility that there are more frames after the last known. // TODO(lode): take values of saved_as and references after index, and a // input flag indicating if they are all frames of the image, to further // optimize this. for (size_t s = 0; s < kNumStorage; ++s) {
size_t frame_ref = storage[s][index]; if (frame_ref == invalid) continue; if (seen[frame_ref]) continue;
stack.push_back(frame_ref);
seen[frame_ref] = 1;
result.push_back(frame_ref);
}
while (!stack.empty()) {
size_t frame_index = stack.back();
stack.pop_back(); if (frame_index == 0) continue; // first frame cannot have references for (size_t s = 0; s < kNumStorage; ++s) { int mask = 1 << s; if (!(refs[frame_index].reference & mask)) continue;
size_t frame_ref = storage[s][frame_index - 1]; if (frame_ref == invalid) continue; if (seen[frame_ref]) continue;
stack.push_back(frame_ref);
seen[frame_ref] = 1;
result.push_back(frame_ref);
}
}
return result;
}
// Parameters for user-requested extra channel output. struct ExtraChannelOutput {
JxlPixelFormat format; void* buffer;
size_t buffer_size;
};
} // namespace
namespace jxl {
typedefstruct JxlDecoderFrameIndexBoxEntryStruct { // OFFi: offset of start byte of this frame compared to start // byte of previous frame from this index in the JPEG XL codestream. For the // first frame, this is the offset from the first byte of the JPEG XL // codestream.
uint64_t OFFi; // Ti: duration in ticks between the start of this frame and // the start of the next frame in the index. If this is the last frame in the // index, this is the duration in ticks between the start of this frame and // the end of the stream. A tick lasts TNUM / TDEN seconds.
uint32_t Ti; // Fi: amount of frames the next frame in the index occurs // after this frame. If this is the last frame in the index, this is the // amount of frames after this frame in the remainder of the stream. Only // frames that are presented by the decoder are counted for this purpose, this // excludes frames that are not intended for display but for compositing with // other frames, such as frames that aren't the last frame with a duration of // 0 ticks.
uint32_t Fi;
} JxlDecoderFrameIndexBoxEntry;
// That way we can ensure that every index box will have the first frame. // If the API user decides to mark it as an indexed frame, we call // the AddFrame again, this time with requested. void AddFrame(uint64_t OFFi, uint32_t Ti, uint32_t Fi) {
JxlDecoderFrameIndexBoxEntry e;
e.OFFi = OFFi;
e.Ti = Ti;
e.Fi = Fi;
entries.push_back(e);
}
} JxlDecoderFrameIndexBox;
// Status of progression, internal. bool got_signature; // Indicates we know that we've seen the last codestream box: either this // was a jxlc box, or a jxlp box that has its index indicated as last by // having its most significant bit set, or no boxes are used at all. This // does not indicate the full codestream has already been seen, only the // last box of it has been initiated. bool last_codestream_seen; bool got_codestream_signature; bool got_basic_info; bool got_transform_data; // To skip everything before ICC. bool got_all_headers; // Codestream metadata headers. bool post_headers; // Already decoding pixels.
std::unique_ptr<jxl::ICCReader> icc_reader;
jxl::JxlDecoderFrameIndexBox frame_index_box; // This means either we actually got the preview image, or determined we // cannot get it or there is none. bool got_preview_image; bool preview_frame;
// Position of next_in in the original file including box format if present // (as opposed to position in the codestream)
size_t file_pos;
size_t box_contents_begin;
size_t box_contents_end;
size_t box_contents_size;
size_t box_size;
size_t header_size; // Either a final box that runs until EOF, or the case of no container format // at all. bool box_contents_unbounded;
JxlBoxType box_type;
JxlBoxType box_decoded_type; // Underlying type for brob boxes // Set to true right after a JXL_DEC_BOX event only. bool box_event; bool decompress_boxes;
bool box_out_buffer_set; // Whether the out buffer is set for the current box, if the user did not yet // release the buffer while the next box is encountered, this will be set to // false. If this is false, no JXL_DEC_NEED_MORE_INPUT is emitted // (irrespective of the value of box_out_buffer_set), because not setting // output indicates the user does not wish the data of this box. bool box_out_buffer_set_current_box;
uint8_t* box_out_buffer;
size_t box_out_buffer_size; // which byte of the full box content the start of the out buffer points to
size_t box_out_buffer_begin; // which byte of box_out_buffer to write to next
size_t box_out_buffer_pos;
// Bitfield, for which informative events (JXL_DEC_BASIC_INFO, etc...) the // decoder returns a status. By default, do not return for any of the events, // only return when the decoder cannot continue because it needs more input or // output data. int events_wanted; int orig_events_wanted;
// Fields for reading the basic info from the header.
size_t basic_info_size_hint; bool have_container;
size_t box_count;
// The level of progressive detail in frame decoding.
JxlProgressiveDetail prog_detail = kDC; // The progressive detail of the current frame.
JxlProgressiveDetail frame_prog_detail; // The intended downsampling ratio for the current progression step.
size_t downsampling_target;
// Set to true if either an image out buffer or an image out callback was set. bool image_out_buffer_set;
// Owned by the caller, buffer for preview or full resolution image. void* image_out_buffer;
JxlImageOutInitCallback image_out_init_callback;
JxlImageOutRunCallback image_out_run_callback;
JxlImageOutDestroyCallback image_out_destroy_callback; void* image_out_init_opaque; struct SimpleImageOutCallback {
JxlImageOutCallback callback; void* opaque;
};
SimpleImageOutCallback simple_image_out_callback;
// For extra channels. Empty if no extra channels are requested, and they are // reset each frame
std::vector<ExtraChannelOutput> extra_channel_output;
jxl::CodecMetadata metadata; // Same as metadata.m, except for the color_encoding, which is set to the // output encoding.
jxl::ImageMetadata image_metadata;
std::unique_ptr<jxl::ImageBundle> ib;
// headers and TOC for the current frame. When got_toc is true, this is // always the frame header of the last frame of the current still series, // that is, the displayed frame.
std::unique_ptr<jxl::FrameHeader> frame_header;
size_t remaining_frame_size;
FrameStage frame_stage; bool dc_frame_progression_done; // The currently processed frame is the last of the current composite still, // and so must be returned as pixels bool is_last_of_still; // The currently processed frame is the last of the codestream bool is_last_total; // How many frames to skip.
size_t skip_frames; // Skipping the current frame. May be false if skip_frames was just set to // a positive value while already processing a current frame, then // skipping_frame will be enabled only for the next frame. bool skipping_frame;
// Amount of internal frames and external frames started. External frames are // user-visible frames, internal frames includes all external frames and // also invisible frames such as patches, blending-only and dc_level frames.
size_t internal_frames;
size_t external_frames;
std::vector<FrameRef> frame_refs;
// Translates external frame index to internal frame index. The external // index is the index of user-visible frames. The internal index can be larger // since non-visible frames (such as frames with patches, ...) are included.
std::vector<size_t> frame_external_to_internal;
// Whether the frame with internal index is required to decode the frame // being skipped to or any frames after that. If no skipping is active, // this vector is ignored. If the current internal frame index is beyond this // vector, it must be treated as a required frame.
std::vector<char> frame_required;
// Codestream input data is copied here temporarily when the decoder needs // more input bytes to process the next part of the stream. We copy the input // data in order to be able to release it all through the API it when // returning JXL_DEC_NEED_MORE_INPUT.
std::vector<uint8_t> codestream_copy; // Number of bytes at the end of codestream_copy that were not yet consumed // by calling AdvanceInput().
size_t codestream_unconsumed; // Position in the codestream_copy vector that the decoder already finished // processing. It can be greater than the current size of codestream_copy in // case where the decoder skips some parts of the frame that were not yet // provided.
size_t codestream_pos; // Number of bits after codestream_pos that were already processed.
size_t codestream_bits_ahead;
BoxStage box_stage;
#if JPEGXL_ENABLE_BOXES
jxl::JxlBoxContentDecoder box_content_decoder; #endif #if JPEGXL_ENABLE_TRANSCODE_JPEG
jxl::JxlToJpegDecoder jpeg_decoder; // Decodes Exif or XMP metadata for JPEG reconstruction
jxl::JxlBoxContentDecoder metadata_decoder;
std::vector<uint8_t> exif_metadata;
std::vector<uint8_t> xmp_metadata; // must store JPEG reconstruction metadata from the current box // 0 = not stored, 1 = currently storing, 2 = finished int store_exif; int store_xmp;
size_t recon_out_buffer_pos;
size_t recon_exif_size; // Expected exif size as read from the jbrd box
size_t recon_xmp_size; // Expected exif size as read from the jbrd box
JpegReconStage recon_output_jpeg;
bool JbrdNeedMoreBoxes() const { // jbrd box wants exif but exif box not yet seen if (store_exif < 2 && recon_exif_size > 0) returntrue; // jbrd box wants xmp but xmp box not yet seen if (store_xmp < 2 && recon_xmp_size > 0) returntrue; returnfalse;
} #endif
// Whether the decoder can use more codestream input for a purpose it needs. // This returns false if the user didn't subscribe to any events that // require the codestream (e.g. only subscribed to metadata boxes), or all // parts of the codestream that are subscribed to (e.g. only basic info) have // already occurred. bool CanUseMoreCodestreamInput() const { // The decoder can set this to finished early if all relevant events were // processed, so this check works. return stage != DecoderStage::kCodestreamFinished;
}
};
void* alloc =
jxl::MemoryManagerAlloc(&local_memory_manager, sizeof(JxlDecoder)); if (!alloc) return nullptr; // Placement new constructor on allocated memory
JxlDecoder* dec = new (alloc) JxlDecoder();
dec->memory_manager = local_memory_manager;
JxlDecoderReset(dec);
return dec;
}
void JxlDecoderDestroy(JxlDecoder* dec) { if (dec) {
JxlMemoryManager local_memory_manager = dec->memory_manager; // Call destructor directly since custom free function is used.
dec->~JxlDecoder();
jxl::MemoryManagerFree(&local_memory_manager, dec);
}
}
void JxlDecoderSkipFrames(JxlDecoder* dec, size_t amount) { // Increment amount, rather than set it: making the amount smaller is // impossible because the decoder may already have skipped frames required to // decode earlier frames, and making the amount larger compared to an existing // amount is impossible because if JxlDecoderSkipFrames is called in the // middle of already skipping frames, the user cannot know how many frames // have already been skipped internally so far so an absolute value cannot // be defined.
dec->skip_frames += amount;
// A frame that has been seen before a rewind if (next_frame < dec->frame_external_to_internal.size()) {
size_t internal_index = dec->frame_external_to_internal[next_frame]; if (internal_index < dec->frame_refs.size()) {
std::vector<size_t> deps =
GetFrameDependencies(internal_index, dec->frame_refs);
JxlDecoderStatus JxlDecoderSubscribeEvents(JxlDecoder* dec, int events_wanted) { if (dec->stage != DecoderStage::kInited) { return JXL_DEC_ERROR; // Cannot subscribe to events after having started.
} if (events_wanted & 63) { return JXL_DEC_ERROR; // Can only subscribe to informative events.
}
dec->events_wanted = events_wanted;
dec->orig_events_wanted = events_wanted; return JXL_DEC_SUCCESS;
}
JxlDecoderStatus JxlDecoderSetKeepOrientation(JxlDecoder* dec,
JXL_BOOL skip_reorientation) { if (dec->stage != DecoderStage::kInited) { return JXL_API_ERROR("Must set keep_orientation option before starting");
}
dec->keep_orientation = FROM_JXL_BOOL(skip_reorientation); return JXL_DEC_SUCCESS;
}
JxlDecoderStatus JxlDecoderSetUnpremultiplyAlpha(JxlDecoder* dec,
JXL_BOOL unpremul_alpha) { if (dec->stage != DecoderStage::kInited) { return JXL_API_ERROR("Must set unpremul_alpha option before starting");
}
dec->unpremul_alpha = FROM_JXL_BOOL(unpremul_alpha); return JXL_DEC_SUCCESS;
}
JxlDecoderStatus JxlDecoderSetRenderSpotcolors(JxlDecoder* dec,
JXL_BOOL render_spotcolors) { if (dec->stage != DecoderStage::kInited) { return JXL_API_ERROR("Must set render_spotcolors option before starting");
}
dec->render_spotcolors = FROM_JXL_BOOL(render_spotcolors); return JXL_DEC_SUCCESS;
}
JxlDecoderStatus JxlDecoderSetCoalescing(JxlDecoder* dec, JXL_BOOL coalescing) { if (dec->stage != DecoderStage::kInited) { return JXL_API_ERROR("Must set coalescing option before starting");
}
dec->coalescing = FROM_JXL_BOOL(coalescing); return JXL_DEC_SUCCESS;
}
namespace { // helper function to get the dimensions of the current image buffer void GetCurrentDimensions(const JxlDecoder* dec, size_t& xsize, size_t& ysize) { if (dec->frame_header->nonserialized_is_preview) {
xsize = dec->metadata.oriented_preview_xsize(dec->keep_orientation);
ysize = dec->metadata.oriented_preview_ysize(dec->keep_orientation); return;
}
xsize = dec->metadata.oriented_xsize(dec->keep_orientation);
ysize = dec->metadata.oriented_ysize(dec->keep_orientation); if (!dec->coalescing) { constauto frame_dim = dec->frame_header->ToFrameDimensions();
xsize = frame_dim.xsize_upsampled;
ysize = frame_dim.ysize_upsampled; if (!dec->keep_orientation && static_cast<int>(dec->metadata.m.GetOrientation()) > 4) {
std::swap(xsize, ysize);
}
}
}
} // namespace
namespace jxl { namespace {
// Returns JXL_DEC_SUCCESS if the full bundle was successfully read, status // indicating either error or need more input otherwise. template <class T>
JxlDecoderStatus ReadBundle(JxlDecoder* dec, Span<const uint8_t> data,
BitReader* reader, T* JXL_RESTRICT t) { // Use a copy of the bit reader because CanRead advances bits.
BitReader reader2(data);
reader2.SkipBits(reader->TotalBitsConsumed()); bool can_read = Bundle::CanRead(&reader2, t);
JXL_API_RETURN_IF_ERROR(reader2.Close());
if (!can_read) { return dec->RequestMoreInput();
} if (!Bundle::Read(reader, t)) { return JXL_DEC_ERROR;
} return JXL_DEC_SUCCESS;
}
std::unique_ptr<BitReader, std::function<void(BitReader*)>> GetBitReader(
Span<const uint8_t> span) {
BitReader* reader = new BitReader(span); return std::unique_ptr<BitReader, std::function<void(BitReader*)>>(
reader, [](BitReader* reader) { // We can't allow Close to abort the program if the reader is out of // bounds, or all return paths in the code, even those that already // return failure, would have to manually call AllReadsWithinBounds(). // Invalid JXL codestream should not cause program to quit.
(void)reader->AllReadsWithinBounds();
(void)reader->Close(); delete reader;
});
}
JxlDecoderStatus JxlDecoderReadBasicInfo(JxlDecoder* dec) { if (!dec->got_codestream_signature) { // Check and skip the codestream signature
Span<const uint8_t> span;
JXL_API_RETURN_IF_ERROR(dec->GetCodestreamInput(&span)); if (span.size() < 2) { return dec->RequestMoreInput();
} if (span.data()[0] != 0xff || span.data()[1] != jxl::kCodestreamMarker) { return JXL_INPUT_ERROR("invalid signature");
}
dec->got_codestream_signature = true;
dec->AdvanceCodestream(2);
}
Span<const uint8_t> span;
JXL_API_RETURN_IF_ERROR(dec->GetCodestreamInput(&span)); auto reader = GetBitReader(span);
reader->SkipBits(dec->codestream_bits_ahead);
if (dec->metadata.m.color_encoding.WantICC()) {
jxl::Status status = dec->icc_reader->Init(reader.get()); // Always check AllReadsWithinBounds, not all the C++ decoder implementation // handles reader out of bounds correctly yet (e.g. context map). Not // checking AllReadsWithinBounds can cause reader->Close() to trigger an // assert, but we don't want library to quit program for invalid codestream. if (!reader->AllReadsWithinBounds() ||
status.code() == StatusCode::kNotEnoughBytes) { return dec->RequestMoreInput();
} if (!status) { // Other non-successful status is an error return JXL_DEC_ERROR;
}
PaddedBytes decoded_icc{&dec->memory_manager};
status = dec->icc_reader->Process(reader.get(), &decoded_icc); if (status.code() == StatusCode::kNotEnoughBytes) { return dec->RequestMoreInput();
} if (!status) { // Other non-successful status is an error return JXL_DEC_ERROR;
} if (decoded_icc.empty()) { return JXL_DEC_ERROR;
}
IccBytes icc;
Bytes(decoded_icc).AppendTo(icc);
dec->metadata.m.color_encoding.SetICCRaw(std::move(icc));
}
JxlDecoderStatus JxlDecoderProcessSections(JxlDecoder* dec) {
Span<const uint8_t> span;
JXL_API_RETURN_IF_ERROR(dec->GetCodestreamInput(&span)); constauto& toc = dec->frame_dec->Toc();
size_t pos = 0;
std::vector<jxl::FrameDecoder::SectionInfo> section_info;
std::vector<jxl::FrameDecoder::SectionStatus> section_status; for (size_t i = dec->next_section; i < toc.size(); ++i) { if (dec->section_processed[i]) {
pos += toc[i].size; continue;
}
size_t id = toc[i].id;
size_t size = toc[i].size; if (OutOfBounds(pos, size, span.size())) { break;
} auto* br = new jxl::BitReader(jxl::Bytes(span.data() + pos, size));
section_info.emplace_back(jxl::FrameDecoder::SectionInfo{br, id, i});
section_status.emplace_back();
pos += size;
}
jxl::Status status = dec->frame_dec->ProcessSections(
section_info.data(), section_info.size(), section_status.data()); bool out_of_bounds = false; bool has_error = false; for (constauto& info : section_info) { if (!info.br->AllReadsWithinBounds()) { // Mark out of bounds section, but keep closing and deleting the next // ones as well.
out_of_bounds = true;
} if (!info.br->Close()) has_error = true; delete info.br;
} if (has_error) { return JXL_INPUT_ERROR("internal: bit-reader failed to close");
} if (out_of_bounds) { // If any bit reader indicates out of bounds, it's an error, not just // needing more input, since we ensure only bit readers containing // a complete section are provided to the FrameDecoder. return JXL_INPUT_ERROR("frame out of bounds");
} if (!status) { return JXL_INPUT_ERROR("frame processing failed");
} for (size_t i = 0; i < section_status.size(); ++i) { auto status = section_status[i]; if (status == jxl::FrameDecoder::kDone) {
dec->section_processed[section_info[i].index] = 1;
} elseif (status != jxl::FrameDecoder::kSkipped) { return JXL_INPUT_ERROR("unexpected section status");
}
}
size_t completed_prefix_bytes = 0; while (dec->next_section < dec->section_processed.size() &&
dec->section_processed[dec->next_section] == 1) {
completed_prefix_bytes += toc[dec->next_section].size;
++dec->next_section;
}
dec->remaining_frame_size -= completed_prefix_bytes;
dec->AdvanceCodestream(completed_prefix_bytes); return JXL_DEC_SUCCESS;
}
// TODO(eustas): no CodecInOut -> no image size reinforcement -> possible OOM.
JxlDecoderStatus JxlDecoderProcessCodestream(JxlDecoder* dec) { // If no parallel runner is set, use the default // TODO(lode): move this initialization to an appropriate location once the // runner is used to decode pixels. if (!dec->thread_pool) {
dec->thread_pool = jxl::make_unique<jxl::ThreadPool>(nullptr, nullptr);
}
// No matter what events are wanted, the basic info is always required. if (!dec->got_basic_info) {
JxlDecoderStatus status = JxlDecoderReadBasicInfo(dec); if (status != JXL_DEC_SUCCESS) return status;
}
int saved_as = FrameDecoder::SavedAs(*dec->frame_header); // is last in entire codestream
dec->is_last_total = dec->frame_header->is_last; // is last of current still
dec->is_last_of_still =
dec->is_last_total || dec->frame_header->animation_frame.duration > 0; // is kRegularFrame and coalescing is disabled
dec->is_last_of_still |=
(!dec->coalescing &&
dec->frame_header->frame_type == FrameType::kRegularFrame); const size_t internal_frame_index = dec->internal_frames; const size_t external_frame_index = dec->external_frames; if (dec->is_last_of_still) dec->external_frames++;
dec->internal_frames++;
if (dec->skip_frames > 0) {
dec->skipping_frame = true; if (dec->is_last_of_still) {
dec->skip_frames--;
}
} else {
dec->skipping_frame = false;
}
if (external_frame_index >= dec->frame_external_to_internal.size()) {
dec->frame_external_to_internal.push_back(internal_frame_index); if (dec->frame_external_to_internal.size() !=
external_frame_index + 1) { return JXL_API_ERROR("internal");
}
}
if (internal_frame_index >= dec->frame_refs.size()) { // add the value 0xff (which means all references) to new slots: we only // know the references of the frame at FinalizeFrame, and fill in the // correct values there. As long as this information is not known, the // worst case where the frame depends on all storage slots is assumed.
dec->frame_refs.emplace_back(FrameRef{0xFF, saved_as}); if (dec->frame_refs.size() != internal_frame_index + 1) { return JXL_API_ERROR("internal");
}
}
if (dec->skipping_frame) { // Whether this frame could be referenced by any future frame: either // because it's a frame saved for blending or patches, or because it's // a DC frame. bool referenceable =
dec->frame_header->CanBeReferenced() ||
dec->frame_header->frame_type == FrameType::kDCFrame; if (internal_frame_index < dec->frame_required.size() &&
!dec->frame_required[internal_frame_index]) {
referenceable = false;
} if (!referenceable) { // Skip all decoding for this frame, since the user is skipping this // frame and no future frames can reference it.
dec->frame_stage = FrameStage::kHeader;
dec->AdvanceCodestream(dec->remaining_frame_size); continue;
}
}
if ((dec->events_wanted & JXL_DEC_FRAME) && dec->is_last_of_still) { // Only return this for the last of a series of stills: patches frames // etc... before this one do not contain the correct information such // as animation timing, ... if (!dec->skipping_frame) { return JXL_DEC_FRAME;
}
}
}
if (dec->frame_stage == FrameStage::kTOC) {
dec->frame_dec->SetRenderSpotcolors(dec->render_spotcolors);
dec->frame_dec->SetCoalescing(dec->coalescing);
// If we don't need pixels, we can skip actually decoding the frames. if (dec->preview_frame || (dec->events_wanted & JXL_DEC_FULL_IMAGE)) {
dec->frame_stage = FrameStage::kFull;
} elseif (!dec->is_last_total) {
dec->frame_stage = FrameStage::kHeader;
dec->AdvanceCodestream(dec->remaining_frame_size); continue;
} else { break;
}
}
if (dec->frame_stage == FrameStage::kFull) { if (!dec->image_out_buffer_set) { if (dec->preview_frame) { return JXL_DEC_NEED_PREVIEW_OUT_BUFFER;
} if ( #if JPEGXL_ENABLE_TRANSCODE_JPEG
(!dec->jpeg_decoder.IsOutputSet() ||
dec->ib->jpeg_data == nullptr) && #endif
dec->is_last_of_still && !dec->skipping_frame) { // TODO(lode): remove the dec->is_last_of_still condition if the // frame decoder needs the image buffer as working space for decoding // non-visible or blending frames too return JXL_DEC_NEED_IMAGE_OUT_BUFFER;
}
}
if (!all_sections_done) { // Not all sections have been processed yet return dec->RequestMoreInput();
}
if (!dec->preview_frame) {
size_t internal_index = dec->internal_frames - 1; if (dec->frame_refs.size() <= internal_index) { return JXL_API_ERROR("internal");
} // Always fill this in, even if it was already written, it could be that // this frame was skipped before and set to 255, while only now we know // the true value.
dec->frame_refs[internal_index].reference =
dec->frame_dec->References();
}
if (!dec->frame_dec->FinalizeFrame()) { return JXL_INPUT_ERROR("decoding frame failed");
} #if JPEGXL_ENABLE_TRANSCODE_JPEG // If jpeg output was requested, we merely return the JXL_DEC_FULL_IMAGE // status without outputting pixels. if (dec->jpeg_decoder.IsOutputSet() && dec->ib->jpeg_data != nullptr) {
dec->frame_stage = FrameStage::kHeader;
dec->recon_output_jpeg = JpegReconStage::kSettingMetadata; return JXL_DEC_FULL_IMAGE;
} #endif if (dec->preview_frame || dec->is_last_of_still) {
dec->image_out_buffer_set = false;
dec->extra_channel_output.clear();
}
}
dec->frame_stage = FrameStage::kHeader;
// The pixels have been output or are not needed, do not keep them in // memory here.
dec->ib.reset(); if (dec->preview_frame) {
dec->got_preview_image = true;
dec->preview_frame = false;
dec->events_wanted &= ~JXL_DEC_PREVIEW_IMAGE; return JXL_DEC_PREVIEW_IMAGE;
} elseif (dec->is_last_of_still &&
(dec->events_wanted & JXL_DEC_FULL_IMAGE) &&
!dec->skipping_frame) { return JXL_DEC_FULL_IMAGE;
}
}
dec->stage = DecoderStage::kCodestreamFinished; // Return success, this means there is nothing more to do. return JXL_DEC_SUCCESS;
}
} // namespace
} // namespace jxl
JxlDecoderStatus JxlDecoderSetInput(JxlDecoder* dec, const uint8_t* data,
size_t size) { if (dec->next_in) { return JXL_API_ERROR("already set input, use JxlDecoderReleaseInput first");
} if (dec->input_closed) { return JXL_API_ERROR("input already closed");
}
JxlDecoderStatus JxlDecoderSetJPEGBuffer(JxlDecoder* dec, uint8_t* data,
size_t size) { #if JPEGXL_ENABLE_TRANSCODE_JPEG // JPEG reconstruction buffer can only set and updated before or during the // first frame, the reconstruction box refers to the first frame and in // theory multi-frame images should not be used with a jbrd box. if (dec->internal_frames > 1) { return JXL_API_ERROR("JPEG reconstruction only works for the first frame");
} if (dec->jpeg_decoder.IsOutputSet()) { return JXL_API_ERROR("Already set JPEG buffer");
} return dec->jpeg_decoder.SetOutputBuffer(data, size); #else return JXL_API_ERROR("JPEG reconstruction is not supported."); #endif
}
size_t JxlDecoderReleaseJPEGBuffer(JxlDecoder* dec) { #if JPEGXL_ENABLE_TRANSCODE_JPEG return dec->jpeg_decoder.ReleaseOutputBuffer(); #else return JXL_API_ERROR("JPEG reconstruction is not supported."); #endif
}
// Parses the header of the box, outputting the 4-character type and the box // size, including header size, as stored in the box header. // @param in current input bytes. // @param size available input size. // @param pos position in the input, must begin at the header of the box. // @param file_pos position of pos since the start of the JXL file, rather than // the current input, used for integer overflow checking. // @param type the output box type. // @param box_size output the total box size, including header, in bytes, or 0 // if it's a final unbounded box. // @param header_size output size of the box header. // @return JXL_DEC_SUCCESS if the box header was fully parsed. In that case the // parsing position must be incremented by header_size bytes. // JXL_DEC_NEED_MORE_INPUT if not enough input bytes available, in that case // header_size indicates a lower bound for the known size the header has to be // at least. JXL_DEC_ERROR if the box header is invalid. static JxlDecoderStatus ParseBoxHeader(const uint8_t* in, size_t size,
size_t pos, size_t file_pos,
JxlBoxType type, uint64_t* box_size,
uint64_t* header_size) { if (OutOfBounds(pos, 8, size)) {
*header_size = 8; return JXL_DEC_NEED_MORE_INPUT;
}
size_t box_start = pos; // Box size, including this header itself.
*box_size = LoadBE32(in + pos);
pos += 4;
memcpy(type, in + pos, 4);
pos += 4; if (*box_size == 1) {
*header_size = 16; if (OutOfBounds(pos, 8, size)) return JXL_DEC_NEED_MORE_INPUT;
*box_size = LoadBE64(in + pos);
pos += 8;
}
*header_size = pos - box_start; if (*box_size > 0 && *box_size < *header_size) { return JXL_INPUT_ERROR("invalid box size");
} if (file_pos + *box_size < file_pos) { return JXL_INPUT_ERROR("Box size overflow");
} return JXL_DEC_SUCCESS;
}
// This includes handling the codestream if it is not a box-based jxl file. static JxlDecoderStatus HandleBoxes(JxlDecoder* dec) { // Box handling loop for (;;) { if (dec->box_stage != BoxStage::kHeader) {
dec->AdvanceInput(dec->header_size);
dec->header_size = 0; #if JPEGXL_ENABLE_BOXES if ((dec->events_wanted & JXL_DEC_BOX) &&
dec->box_out_buffer_set_current_box) {
uint8_t* next_out = dec->box_out_buffer + dec->box_out_buffer_pos;
size_t avail_out = dec->box_out_buffer_size - dec->box_out_buffer_pos;
// Don't return JXL_DEC_NEED_MORE_INPUT: the box stages below, instead, // handle the input progression, and the above only outputs the part of // the box seen so far. if (box_result != JXL_DEC_SUCCESS &&
box_result != JXL_DEC_NEED_MORE_INPUT) { return box_result;
}
} #endif #if JPEGXL_ENABLE_TRANSCODE_JPEG if (dec->store_exif == 1 || dec->store_xmp == 1) {
std::vector<uint8_t>& metadata =
(dec->store_exif == 1) ? dec->exif_metadata : dec->xmp_metadata; for (;;) { if (metadata.empty()) metadata.resize(64);
uint8_t* orig_next_out = metadata.data() + dec->recon_out_buffer_pos;
uint8_t* next_out = orig_next_out;
size_t avail_out = metadata.size() - dec->recon_out_buffer_pos;
JxlDecoderStatus box_result = dec->metadata_decoder.Process(
dec->next_in, dec->avail_in,
dec->file_pos - dec->box_contents_begin, &next_out, &avail_out);
size_t produced = next_out - orig_next_out;
dec->recon_out_buffer_pos += produced; if (box_result == JXL_DEC_BOX_NEED_MORE_OUTPUT) {
metadata.resize(metadata.size() * 2);
} elseif (box_result == JXL_DEC_NEED_MORE_INPUT) { break; // box stage handling below will handle this instead
} elseif (box_result == JXL_DEC_BOX_COMPLETE) {
size_t needed_size = (dec->store_exif == 1) ? dec->recon_exif_size
: dec->recon_xmp_size; if (dec->box_contents_unbounded &&
dec->recon_out_buffer_pos < needed_size) { // Unbounded box, but we know the expected size due to the jbrd // box's data. Treat this as the JXL_DEC_NEED_MORE_INPUT case. break;
} else {
metadata.resize(dec->recon_out_buffer_pos); if (dec->store_exif == 1) dec->store_exif = 2; if (dec->store_xmp == 1) dec->store_xmp = 2; break;
}
} else { // error return box_result;
}
}
} #endif
} #if JPEGXL_ENABLE_TRANSCODE_JPEG if (dec->recon_output_jpeg == JpegReconStage::kSettingMetadata &&
!dec->JbrdNeedMoreBoxes()) {
jxl::jpeg::JPEGData* jpeg_data = dec->ib->jpeg_data.get(); if (dec->recon_exif_size) {
JxlDecoderStatus status = jxl::JxlToJpegDecoder::SetExif(
dec->exif_metadata.data(), dec->exif_metadata.size(), jpeg_data); if (status != JXL_DEC_SUCCESS) return status;
} if (dec->recon_xmp_size) {
JxlDecoderStatus status = jxl::JxlToJpegDecoder::SetXmp(
dec->xmp_metadata.data(), dec->xmp_metadata.size(), jpeg_data); if (status != JXL_DEC_SUCCESS) return status;
}
dec->recon_output_jpeg = JpegReconStage::kOutputting;
}
if (dec->recon_output_jpeg == JpegReconStage::kOutputting &&
!dec->JbrdNeedMoreBoxes()) {
JxlDecoderStatus status =
dec->jpeg_decoder.WriteOutput(*dec->ib->jpeg_data); if (status != JXL_DEC_SUCCESS) return status;
dec->recon_output_jpeg = JpegReconStage::kNone;
dec->ib.reset(); if (dec->events_wanted & JXL_DEC_FULL_IMAGE) { // Return the full image event here now, this may be delayed if this // could only be done after decoding an exif or xmp box after the // codestream. return JXL_DEC_FULL_IMAGE;
}
} #endif
if (dec->box_stage == BoxStage::kHeader) { if (!dec->have_container) { if (dec->stage == DecoderStage::kCodestreamFinished) return JXL_DEC_SUCCESS;
dec->box_stage = BoxStage::kCodestream;
dec->box_contents_unbounded = true; continue;
} if (dec->avail_in == 0) { if (dec->stage != DecoderStage::kCodestreamFinished) { // Not yet seen (all) codestream boxes. return JXL_DEC_NEED_MORE_INPUT;
} #if JPEGXL_ENABLE_TRANSCODE_JPEG if (dec->JbrdNeedMoreBoxes()) { return JXL_DEC_NEED_MORE_INPUT;
} #endif if (dec->input_closed) { return JXL_DEC_SUCCESS;
} if (!(dec->events_wanted & JXL_DEC_BOX)) { // All codestream and jbrd metadata boxes finished, and no individual // boxes requested by user, so no need to request any more input. // This returns success for backwards compatibility, when // JxlDecoderCloseInput and JXL_DEC_BOX did not exist, as well // as for efficiency. return JXL_DEC_SUCCESS;
} // Even though we are exactly at a box end, there still may be more // boxes. The user may call JxlDecoderCloseInput to indicate the input // is finished and get success instead. return JXL_DEC_NEED_MORE_INPUT;
}
bool boxed_codestream_done =
((dec->events_wanted & JXL_DEC_BOX) &&
dec->stage == DecoderStage::kCodestreamFinished && #if JPEGXL_ENABLE_TRANSCODE_JPEG
!dec->JbrdNeedMoreBoxes() && #endif
dec->last_codestream_seen); if (boxed_codestream_done && dec->avail_in >= 2 &&
dec->next_in[0] == 0xff &&
dec->next_in[1] == jxl::kCodestreamMarker) { // We detected the start of the next naked codestream, so we can return // success here. return JXL_DEC_SUCCESS;
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.22Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.