Coverage Report

Created: 2025-06-16 07:00

/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
33.4M
#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
1.53k
  ANSCoder() : state_(ANS_SIGNATURE << 16) {}
52
53
33.1M
  uint32_t PutSymbol(const ANSEncSymbolInfo& t, uint8_t* nbits) {
54
33.1M
    uint32_t bits = 0;
55
33.1M
    *nbits = 0;
56
33.1M
    if ((state_ >> (32 - ANS_LOG_TAB_SIZE)) >= t.freq_) {
57
4.47M
      bits = state_ & 0xffff;
58
4.47M
      state_ >>= 16;
59
4.47M
      *nbits = 16;
60
4.47M
    }
61
33.1M
#ifdef USE_MULT_BY_RECIPROCAL
62
    // We use mult-by-reciprocal trick, but that requires 64b calc.
63
33.1M
    const uint32_t v = (state_ * t.ifreq_) >> RECIPROCAL_PRECISION;
64
33.1M
    const uint32_t offset = t.reverse_map_[state_ - v * t.freq_];
65
33.1M
    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
33.1M
    return bits;
71
33.1M
  }
72
73
1.53k
  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
33.8M
      : 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
14.6M
  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(const HistogramParams& params,
118
                           const std::vector<std::vector<Token>>& tokens,
119
                           std::vector<Histogram>& clustered_histograms);
120
};
121
122
// Writes the context map to the bitstream and concatenates the individual
123
// histogram bitstreams in codes.encoded_histograms. Used in streaming mode.
124
Status EncodeHistograms(const EntropyEncodingData& codes, BitWriter* writer,
125
                        LayerType layer, AuxOut* aux_out);
126
127
// Apply context clustering, compute histograms and encode them. Returns an
128
// estimate of the total bits used for encoding the stream. If `writer` ==
129
// nullptr, the bit estimate will not take into account the context map (which
130
// does not get written if `num_contexts` == 1).
131
// Returns cost
132
StatusOr<size_t> BuildAndEncodeHistograms(
133
    JxlMemoryManager* memory_manager, const HistogramParams& params,
134
    size_t num_contexts, std::vector<std::vector<Token>>& tokens,
135
    EntropyEncodingData* codes, BitWriter* writer, LayerType layer,
136
    AuxOut* aux_out);
137
138
// Write the tokens to a string.
139
Status WriteTokens(const std::vector<Token>& tokens,
140
                   const EntropyEncodingData& codes, size_t context_offset,
141
                   BitWriter* writer, LayerType layer, AuxOut* aux_out);
142
143
// Same as above, but assumes allotment created by caller.
144
size_t WriteTokens(const std::vector<Token>& tokens,
145
                   const EntropyEncodingData& codes, size_t context_offset,
146
                   BitWriter* writer);
147
148
// Exposed for tests; to be used with Writer=BitWriter only.
149
template <typename Writer>
150
void EncodeUintConfigs(const std::vector<HybridUintConfig>& uint_config,
151
                       Writer* writer, size_t log_alpha_size);
152
extern template void EncodeUintConfigs(const std::vector<HybridUintConfig>&,
153
                                       BitWriter*, size_t);
154
155
// Globally set the option to create fuzzer-friendly ANS streams. Negatively
156
// impacts compression. Not thread-safe.
157
void SetANSFuzzerFriendly(bool ans_fuzzer_friendly);
158
}  // namespace jxl
159
160
#endif  // LIB_JXL_ENC_ANS_H_