Coverage Report

Created: 2025-06-22 08:04

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