Coverage Report

Created: 2025-08-12 07:37

/src/libjxl/lib/jxl/enc_context_map.cc
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
// Library to encode the context map.
7
8
#include "lib/jxl/enc_context_map.h"
9
10
#include <jxl/memory_manager.h>
11
#include <jxl/types.h>
12
13
#include <algorithm>
14
#include <cstddef>
15
#include <cstdint>
16
#include <vector>
17
18
#include "lib/jxl/ac_context.h"
19
#include "lib/jxl/base/bits.h"
20
#include "lib/jxl/base/status.h"
21
#include "lib/jxl/enc_ans.h"
22
#include "lib/jxl/enc_ans_params.h"
23
#include "lib/jxl/enc_aux_out.h"
24
#include "lib/jxl/entropy_coder.h"
25
#include "lib/jxl/fields.h"
26
#include "lib/jxl/pack_signed.h"
27
28
namespace jxl {
29
30
namespace {
31
32
212k
size_t IndexOf(const std::vector<uint8_t>& v, uint8_t value) {
33
212k
  size_t i = 0;
34
2.20M
  for (; i < v.size(); ++i) {
35
2.20M
    if (v[i] == value) return i;
36
2.20M
  }
37
0
  return i;
38
212k
}
39
40
212k
void MoveToFront(std::vector<uint8_t>* v, size_t index) {
41
212k
  uint8_t value = (*v)[index];
42
2.20M
  for (size_t i = index; i != 0; --i) {
43
1.99M
    (*v)[i] = (*v)[i - 1];
44
1.99M
  }
45
212k
  (*v)[0] = value;
46
212k
}
47
48
832
std::vector<uint8_t> MoveToFrontTransform(const std::vector<uint8_t>& v) {
49
832
  if (v.empty()) return v;
50
832
  uint8_t max_value = *std::max_element(v.begin(), v.end());
51
832
  std::vector<uint8_t> mtf(max_value + 1);
52
15.2k
  for (size_t i = 0; i <= max_value; ++i) mtf[i] = i;
53
832
  std::vector<uint8_t> result(v.size());
54
212k
  for (size_t i = 0; i < v.size(); ++i) {
55
212k
    size_t index = IndexOf(mtf, v[i]);
56
212k
    JXL_DASSERT(index < mtf.size());
57
212k
    result[i] = static_cast<uint8_t>(index);
58
212k
    MoveToFront(&mtf, index);
59
212k
  }
60
832
  return result;
61
832
}
62
63
}  // namespace
64
65
Status EncodeContextMap(const std::vector<uint8_t>& context_map,
66
                        size_t num_histograms, BitWriter* writer,
67
1.05k
                        LayerType layer, AuxOut* aux_out) {
68
1.05k
  if (num_histograms == 1) {
69
    // Simple code
70
227
    writer->Write(1, 1);
71
    // 0 bits per entry.
72
227
    writer->Write(2, 0);
73
227
    return true;
74
227
  }
75
76
832
  JxlMemoryManager* memory_manager = writer->memory_manager();
77
832
  std::vector<uint8_t> transformed_symbols = MoveToFrontTransform(context_map);
78
832
  std::vector<std::vector<Token>> tokens(1);
79
832
  std::vector<std::vector<Token>> mtf_tokens(1);
80
212k
  for (const uint8_t& ctx : context_map) {
81
212k
    tokens[0].emplace_back(0, ctx);
82
212k
  }
83
212k
  for (const uint8_t& sym : transformed_symbols) {
84
212k
    mtf_tokens[0].emplace_back(0, sym);
85
212k
  }
86
832
  HistogramParams params;
87
832
  params.uint_method = HistogramParams::HybridUintMethod::kContextMap;
88
832
  size_t ans_cost;
89
832
  size_t mtf_cost;
90
832
  {
91
832
    EntropyEncodingData codes;
92
832
    JXL_ASSIGN_OR_RETURN(
93
832
        ans_cost, BuildAndEncodeHistograms(memory_manager, params, 1, tokens,
94
832
                                           &codes, nullptr, LayerType::Header,
95
832
                                           /*aux_out*/ nullptr));
96
832
  }
97
0
  {
98
832
    EntropyEncodingData codes;
99
832
    JXL_ASSIGN_OR_RETURN(
100
832
        mtf_cost, BuildAndEncodeHistograms(
101
832
                      memory_manager, params, 1, mtf_tokens, &codes, nullptr,
102
832
                      LayerType::Header, /*aux_out*/ nullptr));
103
832
  }
104
0
  bool use_mtf = mtf_cost < ans_cost;
105
  // Rebuild token list.
106
832
  tokens[0].clear();
107
212k
  for (size_t i = 0; i < transformed_symbols.size(); i++) {
108
212k
    tokens[0].emplace_back(0,
109
212k
                           use_mtf ? transformed_symbols[i] : context_map[i]);
110
212k
  }
111
832
  size_t entry_bits = CeilLog2Nonzero(num_histograms);
112
832
  size_t simple_cost = entry_bits * context_map.size();
113
832
  if (entry_bits < 4 && simple_cost < ans_cost && simple_cost < mtf_cost) {
114
382
    JXL_RETURN_IF_ERROR(writer->WithMaxBits(
115
382
        3 + entry_bits * context_map.size(), layer, aux_out, [&] {
116
382
          writer->Write(1, 1);
117
382
          writer->Write(2, entry_bits);
118
382
          for (uint8_t entry : context_map) {
119
382
            writer->Write(entry_bits, entry);
120
382
          }
121
382
          return true;
122
382
        }));
123
450
  } else {
124
450
    JXL_RETURN_IF_ERROR(writer->WithMaxBits(
125
450
        2 + tokens[0].size() * 24, layer, aux_out, [&]() -> Status {
126
450
          writer->Write(1, 0);
127
450
          writer->Write(1, TO_JXL_BOOL(use_mtf));  // Use/don't use MTF.
128
450
          EntropyEncodingData codes;
129
450
          JXL_ASSIGN_OR_RETURN(
130
450
              size_t cost,
131
450
              BuildAndEncodeHistograms(memory_manager, params, 1, tokens,
132
450
                                       &codes, writer, layer, aux_out));
133
450
          (void)cost;
134
450
          WriteTokens(tokens[0], codes, 0, writer);
135
450
          return true;
136
450
        }));
137
450
  }
138
832
  return true;
139
832
}
140
141
Status EncodeBlockCtxMap(const BlockCtxMap& block_ctx_map, BitWriter* writer,
142
162
                         AuxOut* aux_out) {
143
162
  const auto& dct = block_ctx_map.dc_thresholds;
144
162
  const auto& qft = block_ctx_map.qf_thresholds;
145
162
  const auto& ctx_map = block_ctx_map.ctx_map;
146
162
  return writer->WithMaxBits(
147
162
      (dct[0].size() + dct[1].size() + dct[2].size() + qft.size()) * 34 + 1 +
148
162
          4 + 4 + ctx_map.size() * 10 + 1024,
149
162
      LayerType::Ac, aux_out, [&]() -> Status {
150
162
        if (dct[0].empty() && dct[1].empty() && dct[2].empty() && qft.empty() &&
151
162
            ctx_map.size() == 21 &&
152
162
            std::equal(ctx_map.begin(), ctx_map.end(),
153
0
                       BlockCtxMap::kDefaultCtxMap)) {
154
0
          writer->Write(1, 1);  // default
155
0
          return true;
156
0
        }
157
162
        writer->Write(1, 0);
158
486
        for (int j : {0, 1, 2}) {
159
486
          writer->Write(4, dct[j].size());
160
486
          for (int i : dct[j]) {
161
0
            JXL_RETURN_IF_ERROR(
162
0
                U32Coder::Write(kDCThresholdDist, PackSigned(i), writer));
163
0
          }
164
486
        }
165
162
        writer->Write(4, qft.size());
166
162
        for (uint32_t i : qft) {
167
0
          JXL_RETURN_IF_ERROR(U32Coder::Write(kQFThresholdDist, i - 1, writer));
168
0
        }
169
162
        JXL_RETURN_IF_ERROR(EncodeContextMap(ctx_map, block_ctx_map.num_ctxs,
170
162
                                             writer, LayerType::Ac, aux_out));
171
162
        return true;
172
162
      });
173
162
}
174
175
}  // namespace jxl