/* 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.
*/
/* Frame index box 'jxli' will start with Varint() for NF: has type Varint(): number of frames listed in the index. TNUM: has type u32: numerator of tick unit. TDEN: has type u32: denominator of tick unit. Value 0 means the file is ill-formed. per frame i listed: OFFi: has type Varint(): 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. Ti: has type Varint(): 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. Fi: has type Varint(): 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.
All the frames listed in jxli are keyframes and the first frame is present in the list. There shall be either zero or one Frame Index boxes in a JPEG XL file. The offsets OFFi per frame are given as bytes in the codestream, not as bytes in the file format using the box structure. This means if JPEG XL Partial Codestream boxes are used, the offset is counted within the concatenated codestream, bytes from box headers or non-codestream boxes are not counted.
*/
typedefstruct JxlEncoderFrameIndexBoxStruct { // We always need to record the first frame entry, so presence of the // first entry alone is not an indication if it was requested to be // stored. bool index_box_requested_through_api = false;
// 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 duration, bool to_be_indexed) { // We call AddFrame to every frame. // Recording the first frame is required by the standard. // Knowing the last frame is required, since the last indexed frame // needs to know how many frames until the end. // To be able to tell how many frames there are between each index // entry we just record every frame here. if (entries.size() == 1) { if (OFFi == entries[0].OFFi) { // API use for the first frame, let's clear the already recorded first // frame.
entries.clear();
}
}
JxlEncoderFrameIndexBoxEntry e;
e.to_be_indexed = to_be_indexed;
e.OFFi = OFFi;
e.duration = duration;
entries.push_back(e);
}
} JxlEncoderFrameIndexBox;
// The encoder options (such as quality, compression speed, ...) for a single // frame, but not encoder-wide options such as box-related options. typedefstruct JxlEncoderFrameSettingsValuesStruct { // lossless is a separate setting from cparams because it is a combination // setting that overrides multiple settings inside of cparams. bool lossless;
CompressParams cparams;
JxlFrameHeader header;
std::vector<JxlBlendInfo> extra_channel_blend_info;
std::string frame_name;
JxlBitDepth image_bit_depth; bool frame_index_box = false;
jxl::AuxOut* aux_out = nullptr;
} JxlEncoderFrameSettingsValues;
typedef std::array<uint8_t, 4> BoxType;
// Utility function that makes a BoxType from a string literal. The string must // have 4 characters, a 5th null termination character is optional.
constexpr BoxType MakeBoxType(constchar* type) { return BoxType(
{{static_cast<uint8_t>(type[0]), static_cast<uint8_t>(type[1]), static_cast<uint8_t>(type[2]), static_cast<uint8_t>(type[3])}});
}
// Common adapter for an existing frame input source or a whole-image input // buffer or a parsed JPEG file. class JxlEncoderChunkedFrameAdapter { public:
JxlEncoderChunkedFrameAdapter(size_t xs, size_t ys, size_t num_extra_channels)
: xsize(xs), ysize(ys), channels_(1 + num_extra_channels) {}
void ReleaseCurrentData(constvoid* buffer) { // No dynamic memory is allocated in GetColorChannelDataAt or // GetExtraChannelDataAt. Therefore, no cleanup is required here.
}
using FJXLFrameUniquePtr =
std::unique_ptr<JxlFastLosslessFrameState,
decltype(&JxlFastLosslessFreeFrameState)>;
// Either a frame, or a box, not both. // Can also be a FJXL frame. struct JxlEncoderQueuedInput { explicit JxlEncoderQueuedInput(const JxlMemoryManager& memory_manager)
: frame(nullptr, jxl::MemoryManagerDeleteHelper(&memory_manager)),
box(nullptr, jxl::MemoryManagerDeleteHelper(&memory_manager)) {}
MemoryManagerUniquePtr<JxlEncoderQueuedFrame> frame;
MemoryManagerUniquePtr<JxlEncoderQueuedBox> box;
FJXLFrameUniquePtr fast_lossless_frame = {nullptr,
JxlFastLosslessFreeFrameState};
};
// Caller can never overwrite a previously-written buffer. Asking for a buffer // with `min_size` such that `position + min_size` overlaps with a // previously-written buffer is invalid.
jxl::StatusOr<JxlOutputProcessorBuffer> GetBuffer(size_t min_size,
size_t requested_size = 0);
struct InternalBuffer { explicit InternalBuffer(JxlMemoryManager* memory_manager)
: owned_data(memory_manager) {
JXL_DASSERT(memory_manager != nullptr);
} // Bytes in the range `[output_position_ - start_of_the_buffer, // written_bytes)` need to be flushed out.
size_t written_bytes = 0; // If data has been buffered, it is stored in `owned_data`.
jxl::PaddedBytes owned_data;
};
// Invariant: `internal_buffers_` does not contain chunks that are entirely // below the output position.
std::map<size_t, InternalBuffer> internal_buffers_;
uint8_t** next_out_ = nullptr;
size_t* avail_out_ = nullptr; // Where the next GetBuffer call will write bytes to.
size_t position_ = 0; // The position of the last SetFinalizedPosition call.
size_t finalized_position_ = 0; // Either the position of the `external_output_processor_` or the position // `next_out_` points to.
size_t output_position_ = 0;
// Internal use only struct, can only be initialized correctly by // JxlEncoderCreate. struct JxlEncoderStruct {
JxlEncoderStruct() : output_processor(&memory_manager) {}
JxlMemoryManager memory_manager;
jxl::MemoryManagerUniquePtr<jxl::ThreadPool> thread_pool{
nullptr, jxl::MemoryManagerDeleteHelper(&memory_manager)};
std::vector<jxl::MemoryManagerUniquePtr<JxlEncoderFrameSettings>>
encoder_options;
// How many codestream bytes have been written, i.e., // content of jxlc and jxlp boxes. Frame index box jxli // requires position indices to point to codestream bytes, // so we need to keep track of the total of flushed or queue // codestream bytes. These bytes may be in a single jxlc box // or across multiple jxlp boxes.
size_t codestream_bytes_written_end_of_frame;
jxl::JxlEncoderFrameIndexBox frame_index_box;
JxlCmsInterface cms; bool cms_set;
// Force using the container even if not needed bool use_container; // User declared they will add metadata boxes bool use_boxes;
// TODO(lode): move level into jxl::CompressParams since some C++ // implementation decisions should be based on it: level 10 allows more // features to be used. bool store_jpeg_metadata;
int32_t codestream_level;
jxl::CodecMetadata metadata;
std::vector<uint8_t> jpeg_metadata;
// Encoder wrote a jxlp (partial codestream) box, so any next codestream // parts must also be written in jxlp boxes, a single jxlc box cannot be // used. The counter is used for the 4-byte jxlp box index header.
size_t jxlp_counter;
// Wrote any output at all, so wrote the data before the first user added // frame or box, such as signature, basic info, ICC profile or jpeg // reconstruction box. bool wrote_bytes;
// `write_box` must never seek before the position the output wrapper was at // the moment of the call, and must leave the output wrapper such that its // position is one byte past the end of the written box. template <typename WriteBox>
jxl::Status AppendBox(const jxl::BoxType& type, bool unbounded,
size_t box_max_size, const WriteBox& write_box);
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.