Coverage Report

Created: 2025-07-23 08:18

/src/libjxl/lib/jxl/enc_ans.h
Line
Count
Source
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_ANS_H_
7
#define LIB_JXL_ENC_ANS_H_
8
9
// Library to encode the ANS population counts to the bit-stream and encode
10
// symbols based on the respective distributions.
11
12
#include <jxl/memory_manager.h>
13
14
#include <cstddef>
15
#include <cstdint>
16
#include <vector>
17
18
#include "lib/jxl/ans_params.h"
19
#include "lib/jxl/base/status.h"
20
#include "lib/jxl/dec_ans.h"
21
#include "lib/jxl/enc_ans_params.h"
22
#include "lib/jxl/enc_bit_writer.h"
23
24
namespace jxl {
25
26
struct AuxOut;
27
enum class LayerType : uint8_t;
28
29
#define USE_MULT_BY_RECIPROCAL
30
31
// precision must be equal to:  #bits(state_) + #bits(freq)
32
199M
#define RECIPROCAL_PRECISION (32 + ANS_LOG_TAB_SIZE)
33
34
// Data structure representing one element of the encoding table built
35
// from a distribution.
36
// TODO(veluca): split this up, or use an union.
37
struct ANSEncSymbolInfo {
38
  // ANS
39
  uint16_t freq_;
40
  std::vector<uint16_t> reverse_map_;
41
#ifdef USE_MULT_BY_RECIPROCAL
42
  uint64_t ifreq_;
43
#endif
44
  // Prefix coding.
45
  uint8_t depth;
46
  uint16_t bits;
47
};
48
49
class ANSCoder {
50
 public:
51
13.9k
  ANSCoder() : state_(ANS_SIGNATURE << 16) {}
52
53
198M
  uint32_t PutSymbol(const ANSEncSymbolInfo& t, uint8_t* nbits) {
54
198M
    uint32_t bits = 0;
55
198M
    *nbits = 0;
56
198M
    if ((state_ >> (32 - ANS_LOG_TAB_SIZE)) >= t.freq_) {
57
21.2M
      bits = state_ & 0xffff;
58
21.2M
      state_ >>= 16;
59
21.2M
      *nbits = 16;
60
21.2M
    }
61
198M
#ifdef USE_MULT_BY_RECIPROCAL
62
    // We use mult-by-reciprocal trick, but that requires 64b calc.
63
198M
    const uint32_t v = (state_ * t.ifreq_) >> RECIPROCAL_PRECISION;
64
198M
    const uint32_t offset = t.reverse_map_[state_ - v * t.freq_];
65
198M
    state_ = (v << ANS_LOG_TAB_SIZE) + offset;
66
#else
67
    state_ = ((state_ / t.freq_) << ANS_LOG_TAB_SIZE) +
68
             t.reverse_map_[state_ % t.freq_];
69
#endif
70
198M
    return bits;
71
198M
  }
72
73
13.9k
  uint32_t GetState() const { return state_; }
74
75
 private:
76
  uint32_t state_;
77
};
78
79
static const int kNumFixedHistograms = 1;
80
81
// Integer to be encoded by an entropy coder, either ANS or Huffman.
82
struct Token {
83
  Token() = default;
84
  Token(uint32_t c, uint32_t value)
85
212M
      : is_lz77_length(false), context(c), value(value) {}
86
  uint32_t is_lz77_length : 1;
87
  uint32_t context : 31;
88
  uint32_t value;
89
};
90
91
struct SizeWriter {
92
  size_t size = 0;
93
89.9M
  void Write(size_t num, size_t bits) { size += num; }
94
};
95
96
struct EntropyEncodingData {
97
  std::vector<std::vector<ANSEncSymbolInfo>> encoding_info;
98
  bool use_prefix_code;
99
  std::vector<HybridUintConfig> uint_config;
100
  size_t log_alpha_size;
101
  LZ77Params lz77;
102
  std::vector<BitWriter> encoded_histograms;
103
  std::vector<uint8_t> context_map;
104
105
  StatusOr<size_t> BuildAndStoreEntropyCodes(
106
      JxlMemoryManager* memory_manager, const HistogramParams& params,
107
      const std::vector<std::vector<Token>>& tokens,
108
      const std::vector<Histogram>& builder, BitWriter* writer, LayerType layer,
109
      AuxOut* aux_out);
110
111
  StatusOr<size_t> BuildAndStoreANSEncodingData(
112
      JxlMemoryManager* memory_manager,
113
      HistogramParams::ANSHistogramStrategy ans_histogram_strategy,
114
      const Histogram& histogram, BitWriter* writer);
115
116
 private:
117
  Status ChooseUintConfigs(JxlMemoryManager* memory_manager,
118
                           const HistogramParams& params,
119
                           const std::vector<std::vector<Token>>& tokens,
120
                           std::vector<Histogram>& clustered_histograms);
121
};
122
123
// Writes the context map to the bitstream and concatenates the individual
124
// histogram bitstreams in codes.encoded_histograms. Used in streaming mode.
125
Status EncodeHistograms(const EntropyEncodingData& codes, BitWriter* writer,
126
                        LayerType layer, AuxOut* aux_out);
127
128
// Apply context clustering, compute histograms and encode them. Returns an
129
// estimate of the total bits used for encoding the stream. If `writer` ==
130
// nullptr, the bit estimate will not take into account the context map (which
131
// does not get written if `num_contexts` == 1).
132
// Returns cost
133
StatusOr<size_t> BuildAndEncodeHistograms(
134
    JxlMemoryManager* memory_manager, const HistogramParams& params,
135
    size_t num_contexts, std::vector<std::vector<Token>>& tokens,
136
    EntropyEncodingData* codes, BitWriter* writer, LayerType layer,
137
    AuxOut* aux_out);
138
139
// Write the tokens to a string.
140
Status WriteTokens(const std::vector<Token>& tokens,
141
                   const EntropyEncodingData& codes, size_t context_offset,
142
                   BitWriter* writer, LayerType layer, AuxOut* aux_out);
143
144
// Same as above, but assumes allotment created by caller.
145
size_t WriteTokens(const std::vector<Token>& tokens,
146
                   const EntropyEncodingData& codes, size_t context_offset,
147
                   BitWriter* writer);
148
149
// Exposed for tests; to be used with Writer=BitWriter only.
150
template <typename Writer>
151
void EncodeUintConfigs(const std::vector<HybridUintConfig>& uint_config,
152
                       Writer* writer, size_t log_alpha_size);
153
extern template void EncodeUintConfigs(const std::vector<HybridUintConfig>&,
154
                                       BitWriter*, size_t);
155
156
// Globally set the option to create fuzzer-friendly ANS streams. Negatively
157
// impacts compression. Not thread-safe.
158
void SetANSFuzzerFriendly(bool ans_fuzzer_friendly);
159
}  // namespace jxl
160
161
#endif  // LIB_JXL_ENC_ANS_H_