Coverage Report

Created: 2025-06-16 07:00

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