enc_modular.h (4833B)
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 #ifndef LIB_JXL_ENC_MODULAR_H_ 7 #define LIB_JXL_ENC_MODULAR_H_ 8 9 #include <jxl/cms_interface.h> 10 11 #include <cstddef> 12 #include <cstdint> 13 #include <vector> 14 15 #include "lib/jxl/base/compiler_specific.h" 16 #include "lib/jxl/base/data_parallel.h" 17 #include "lib/jxl/base/status.h" 18 #include "lib/jxl/dec_modular.h" 19 #include "lib/jxl/enc_ans.h" 20 #include "lib/jxl/enc_bit_writer.h" 21 #include "lib/jxl/enc_cache.h" 22 #include "lib/jxl/enc_params.h" 23 #include "lib/jxl/frame_dimensions.h" 24 #include "lib/jxl/frame_header.h" 25 #include "lib/jxl/image.h" 26 #include "lib/jxl/image_metadata.h" 27 #include "lib/jxl/modular/encoding/dec_ma.h" 28 #include "lib/jxl/modular/encoding/encoding.h" 29 #include "lib/jxl/modular/modular_image.h" 30 #include "lib/jxl/modular/options.h" 31 #include "lib/jxl/quant_weights.h" 32 33 namespace jxl { 34 35 struct AuxOut; 36 37 class ModularFrameEncoder { 38 public: 39 ModularFrameEncoder(const FrameHeader& frame_header, 40 const CompressParams& cparams_orig, bool streaming_mode); 41 Status ComputeEncodingData( 42 const FrameHeader& frame_header, const ImageMetadata& metadata, 43 Image3F* JXL_RESTRICT color, const std::vector<ImageF>& extra_channels, 44 const Rect& group_rect, const FrameDimensions& patch_dim, 45 const Rect& frame_area_rect, PassesEncoderState* JXL_RESTRICT enc_state, 46 const JxlCmsInterface& cms, ThreadPool* pool, AuxOut* aux_out, 47 bool do_color); 48 Status ComputeTree(ThreadPool* pool); 49 Status ComputeTokens(ThreadPool* pool); 50 // Encodes global info (tree + histograms) in the `writer`. 51 Status EncodeGlobalInfo(bool streaming_mode, BitWriter* writer, 52 AuxOut* aux_out); 53 // Encodes a specific modular image (identified by `stream`) in the `writer`, 54 // assigning bits to the provided `layer`. 55 Status EncodeStream(BitWriter* writer, AuxOut* aux_out, size_t layer, 56 const ModularStreamId& stream); 57 58 void ClearStreamData(const ModularStreamId& stream); 59 void ClearModularStreamData(); 60 size_t ComputeStreamingAbsoluteAcGroupId( 61 size_t dc_group_id, size_t ac_group_id, 62 const FrameDimensions& patch_dim) const; 63 64 // Creates a modular image for a given DC group of VarDCT mode. `dc` is the 65 // input DC image, not quantized; the group is specified by `group_index`, and 66 // `nl_dc` decides whether to apply a near-lossless processing to the DC or 67 // not. 68 Status AddVarDCTDC(const FrameHeader& frame_header, const Image3F& dc, 69 const Rect& r, size_t group_index, bool nl_dc, 70 PassesEncoderState* enc_state, bool jpeg_transcode); 71 // Creates a modular image for the AC metadata of the given group 72 // (`group_index`). 73 Status AddACMetadata(const Rect& r, size_t group_index, bool jpeg_transcode, 74 PassesEncoderState* enc_state); 75 // Encodes a RAW quantization table in `writer`. If `modular_frame_encoder` is 76 // null, the quantization table in `encoding` is used, with dimensions `size_x 77 // x size_y`. Otherwise, the table with ID `idx` is encoded from the given 78 // `modular_frame_encoder`. 79 static Status EncodeQuantTable(size_t size_x, size_t size_y, 80 BitWriter* writer, 81 const QuantEncoding& encoding, size_t idx, 82 ModularFrameEncoder* modular_frame_encoder); 83 // Stores a quantization table for future usage with `EncodeQuantTable`. 84 Status AddQuantTable(size_t size_x, size_t size_y, 85 const QuantEncoding& encoding, size_t idx); 86 87 std::vector<size_t> ac_metadata_size; 88 std::vector<uint8_t> extra_dc_precision; 89 90 private: 91 Status PrepareStreamParams(const Rect& rect, const CompressParams& cparams, 92 int minShift, int maxShift, 93 const ModularStreamId& stream, bool do_color, 94 bool groupwise); 95 std::vector<Image> stream_images_; 96 std::vector<ModularOptions> stream_options_; 97 std::vector<uint32_t> quants_; 98 99 Tree tree_; 100 std::vector<std::vector<Token>> tree_tokens_; 101 std::vector<GroupHeader> stream_headers_; 102 std::vector<std::vector<Token>> tokens_; 103 EntropyEncodingData code_; 104 std::vector<uint8_t> context_map_; 105 FrameDimensions frame_dim_; 106 CompressParams cparams_; 107 std::vector<size_t> tree_splits_; 108 std::vector<std::vector<uint32_t>> gi_channel_; 109 std::vector<size_t> image_widths_; 110 Predictor delta_pred_ = Predictor::Average4; 111 112 struct GroupParams { 113 Rect rect; 114 int minShift; 115 int maxShift; 116 ModularStreamId id; 117 }; 118 std::vector<GroupParams> stream_params_; 119 }; 120 121 } // namespace jxl 122 123 #endif // LIB_JXL_ENC_MODULAR_H_