Coverage Report

Created: 2025-07-16 07:53

/src/libjxl/lib/jxl/entropy_coder.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
#include "lib/jxl/entropy_coder.h"
7
8
#include <jxl/memory_manager.h>
9
10
#include <cstdint>
11
#include <vector>
12
13
#include "lib/jxl/ac_context.h"
14
#include "lib/jxl/base/compiler_specific.h"
15
#include "lib/jxl/base/status.h"
16
#include "lib/jxl/coeff_order.h"
17
#include "lib/jxl/coeff_order_fwd.h"
18
#include "lib/jxl/dec_bit_reader.h"
19
#include "lib/jxl/dec_context_map.h"
20
#include "lib/jxl/fields.h"
21
#include "lib/jxl/pack_signed.h"
22
23
namespace jxl {
24
25
Status DecodeBlockCtxMap(JxlMemoryManager* memory_manager, BitReader* br,
26
6.07k
                         BlockCtxMap* block_ctx_map) {
27
6.07k
  auto& dct = block_ctx_map->dc_thresholds;
28
6.07k
  auto& qft = block_ctx_map->qf_thresholds;
29
6.07k
  auto& ctx_map = block_ctx_map->ctx_map;
30
6.07k
  bool is_default = static_cast<bool>(br->ReadFixedBits<1>());
31
6.07k
  if (is_default) {
32
6.07k
    *block_ctx_map = BlockCtxMap();
33
6.07k
    return true;
34
6.07k
  }
35
1
  block_ctx_map->num_dc_ctxs = 1;
36
3
  for (int j : {0, 1, 2}) {
37
3
    dct[j].resize(br->ReadFixedBits<4>());
38
3
    block_ctx_map->num_dc_ctxs *= dct[j].size() + 1;
39
4
    for (int& i : dct[j]) {
40
4
      i = UnpackSigned(U32Coder::Read(kDCThresholdDist, br));
41
4
    }
42
3
  }
43
1
  qft.resize(br->ReadFixedBits<4>());
44
2
  for (uint32_t& i : qft) {
45
2
    i = U32Coder::Read(kQFThresholdDist, br) + 1;
46
2
  }
47
48
1
  if (block_ctx_map->num_dc_ctxs * (qft.size() + 1) > 64) {
49
0
    return JXL_FAILURE("Invalid block context map: too big");
50
0
  }
51
52
1
  ctx_map.resize(3 * kNumOrders * block_ctx_map->num_dc_ctxs *
53
1
                 (qft.size() + 1));
54
1
  JXL_RETURN_IF_ERROR(
55
1
      DecodeContextMap(memory_manager, &ctx_map, &block_ctx_map->num_ctxs, br));
56
1
  if (block_ctx_map->num_ctxs > 16) {
57
0
    return JXL_FAILURE("Invalid block context map: too many distinct contexts");
58
0
  }
59
1
  return true;
60
1
}
61
62
#if JXL_CXX_LANG < JXL_CXX_17
63
constexpr uint8_t BlockCtxMap::kDefaultCtxMap[];  // from ac_context.h
64
#endif
65
66
}  // namespace jxl