Coverage Report

Created: 2025-07-16 07:53

/src/libjxl/lib/jxl/ac_strategy.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/ac_strategy.h"
7
8
#include <jxl/memory_manager.h>
9
10
#include <algorithm>
11
#include <cstring>
12
#include <utility>
13
14
#include "lib/jxl/base/bits.h"
15
#include "lib/jxl/base/compiler_specific.h"
16
17
namespace jxl {
18
19
// Tries to generalize zig-zag order to non-square blocks. Surprisingly, in
20
// square block frequency along the (i + j == const) diagonals is roughly the
21
// same. For historical reasons, consecutive diagonals are traversed
22
// in alternating directions - so called "zig-zag" (or "snake") order.
23
template <bool is_lut>
24
15.1k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
25
15.1k
  size_t cx = acs.covered_blocks_x();
26
15.1k
  size_t cy = acs.covered_blocks_y();
27
15.1k
  CoefficientLayout(&cy, &cx);
28
29
  // CoefficientLayout ensures cx >= cy.
30
  // We compute the zigzag order for a cx x cx block, then discard all the
31
  // lines that are not multiple of the ratio between cx and cy.
32
15.1k
  size_t xs = cx / cy;
33
15.1k
  size_t xsm = xs - 1;
34
15.1k
  size_t xss = CeilLog2Nonzero(xs);
35
  // First half of the block
36
15.1k
  size_t cur = cx * cy;
37
184k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
38
1.32M
    for (size_t j = 0; j <= i; j++) {
39
1.15M
      size_t x = j;
40
1.15M
      size_t y = i - j;
41
1.15M
      if (i % 2) std::swap(x, y);
42
1.15M
      if ((y & xsm) != 0) continue;
43
763k
      y >>= xss;
44
763k
      size_t val = 0;
45
763k
      if (x < cx && y < cy) {
46
21.2k
        val = y * cx + x;
47
742k
      } else {
48
742k
        val = cur++;
49
742k
      }
50
763k
      if (is_lut) {
51
0
        out[y * cx * kBlockDim + x] = val;
52
763k
      } else {
53
763k
        out[val] = y * cx * kBlockDim + x;
54
763k
      }
55
763k
    }
56
169k
  }
57
  // Second half
58
169k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
59
154k
    size_t i = ip - 1;
60
1.13M
    for (size_t j = 0; j <= i; j++) {
61
982k
      size_t x = cx * kBlockDim - 1 - (i - j);
62
982k
      size_t y = cx * kBlockDim - 1 - j;
63
982k
      if (i % 2) std::swap(x, y);
64
982k
      if ((y & xsm) != 0) continue;
65
594k
      y >>= xss;
66
594k
      size_t val = cur++;
67
594k
      if (is_lut) {
68
0
        out[y * cx * kBlockDim + x] = val;
69
594k
      } else {
70
594k
        out[val] = y * cx * kBlockDim + x;
71
594k
      }
72
594k
    }
73
154k
  }
74
15.1k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<false>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
24
15.1k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
25
15.1k
  size_t cx = acs.covered_blocks_x();
26
15.1k
  size_t cy = acs.covered_blocks_y();
27
15.1k
  CoefficientLayout(&cy, &cx);
28
29
  // CoefficientLayout ensures cx >= cy.
30
  // We compute the zigzag order for a cx x cx block, then discard all the
31
  // lines that are not multiple of the ratio between cx and cy.
32
15.1k
  size_t xs = cx / cy;
33
15.1k
  size_t xsm = xs - 1;
34
15.1k
  size_t xss = CeilLog2Nonzero(xs);
35
  // First half of the block
36
15.1k
  size_t cur = cx * cy;
37
184k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
38
1.32M
    for (size_t j = 0; j <= i; j++) {
39
1.15M
      size_t x = j;
40
1.15M
      size_t y = i - j;
41
1.15M
      if (i % 2) std::swap(x, y);
42
1.15M
      if ((y & xsm) != 0) continue;
43
763k
      y >>= xss;
44
763k
      size_t val = 0;
45
763k
      if (x < cx && y < cy) {
46
21.2k
        val = y * cx + x;
47
742k
      } else {
48
742k
        val = cur++;
49
742k
      }
50
763k
      if (is_lut) {
51
0
        out[y * cx * kBlockDim + x] = val;
52
763k
      } else {
53
763k
        out[val] = y * cx * kBlockDim + x;
54
763k
      }
55
763k
    }
56
169k
  }
57
  // Second half
58
169k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
59
154k
    size_t i = ip - 1;
60
1.13M
    for (size_t j = 0; j <= i; j++) {
61
982k
      size_t x = cx * kBlockDim - 1 - (i - j);
62
982k
      size_t y = cx * kBlockDim - 1 - j;
63
982k
      if (i % 2) std::swap(x, y);
64
982k
      if ((y & xsm) != 0) continue;
65
594k
      y >>= xss;
66
594k
      size_t val = cur++;
67
594k
      if (is_lut) {
68
0
        out[y * cx * kBlockDim + x] = val;
69
594k
      } else {
70
594k
        out[val] = y * cx * kBlockDim + x;
71
594k
      }
72
594k
    }
73
154k
  }
74
15.1k
}
Unexecuted instantiation: ac_strategy.cc:void jxl::CoeffOrderAndLut<true>(jxl::AcStrategy, unsigned int*)
75
76
15.1k
void AcStrategy::ComputeNaturalCoeffOrder(coeff_order_t* order) const {
77
15.1k
  CoeffOrderAndLut</*is_lut=*/false>(*this, order);
78
15.1k
}
79
0
void AcStrategy::ComputeNaturalCoeffOrderLut(coeff_order_t* lut) const {
80
0
  CoeffOrderAndLut</*is_lut=*/true>(*this, lut);
81
0
}
82
83
#if JXL_CXX_LANG < JXL_CXX_17
84
constexpr size_t AcStrategy::kMaxCoeffBlocks;
85
constexpr size_t AcStrategy::kMaxBlockDim;
86
constexpr size_t AcStrategy::kMaxCoeffArea;
87
#endif
88
89
StatusOr<AcStrategyImage> AcStrategyImage::Create(
90
12.0k
    JxlMemoryManager* memory_manager, size_t xsize, size_t ysize) {
91
12.0k
  AcStrategyImage img;
92
12.0k
  JXL_ASSIGN_OR_RETURN(img.layers_,
93
12.0k
                       ImageB::Create(memory_manager, xsize, ysize));
94
12.0k
  img.row_ = img.layers_.Row(0);
95
12.0k
  img.stride_ = img.layers_.PixelsPerRow();
96
12.0k
  return img;
97
12.0k
}
98
99
0
size_t AcStrategyImage::CountBlocks(AcStrategyType type) const {
100
0
  size_t ret = 0;
101
0
  for (size_t y = 0; y < layers_.ysize(); y++) {
102
0
    const uint8_t* JXL_RESTRICT row = layers_.ConstRow(y);
103
0
    for (size_t x = 0; x < layers_.xsize(); x++) {
104
0
      if (row[x] == ((static_cast<uint8_t>(type) << 1) | 1)) ret++;
105
0
    }
106
0
  }
107
0
  return ret;
108
0
}
109
110
}  // namespace jxl