Coverage Report

Created: 2025-06-22 08:04

/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
24.2k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
25
24.2k
  size_t cx = acs.covered_blocks_x();
26
24.2k
  size_t cy = acs.covered_blocks_y();
27
24.2k
  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
24.2k
  size_t xs = cx / cy;
33
24.2k
  size_t xsm = xs - 1;
34
24.2k
  size_t xss = CeilLog2Nonzero(xs);
35
  // First half of the block
36
24.2k
  size_t cur = cx * cy;
37
337k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
38
5.95M
    for (size_t j = 0; j <= i; j++) {
39
5.64M
      size_t x = j;
40
5.64M
      size_t y = i - j;
41
5.64M
      if (i % 2) std::swap(x, y);
42
5.64M
      if ((y & xsm) != 0) continue;
43
4.09M
      y >>= xss;
44
4.09M
      size_t val = 0;
45
4.09M
      if (x < cx && y < cy) {
46
123k
        val = y * cx + x;
47
3.97M
      } else {
48
3.97M
        val = cur++;
49
3.97M
      }
50
4.09M
      if (is_lut) {
51
0
        out[y * cx * kBlockDim + x] = val;
52
4.09M
      } else {
53
4.09M
        out[val] = y * cx * kBlockDim + x;
54
4.09M
      }
55
4.09M
    }
56
312k
  }
57
  // Second half
58
312k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
59
288k
    size_t i = ip - 1;
60
5.61M
    for (size_t j = 0; j <= i; j++) {
61
5.32M
      size_t x = cx * kBlockDim - 1 - (i - j);
62
5.32M
      size_t y = cx * kBlockDim - 1 - j;
63
5.32M
      if (i % 2) std::swap(x, y);
64
5.32M
      if ((y & xsm) != 0) continue;
65
3.78M
      y >>= xss;
66
3.78M
      size_t val = cur++;
67
3.78M
      if (is_lut) {
68
0
        out[y * cx * kBlockDim + x] = val;
69
3.78M
      } else {
70
3.78M
        out[val] = y * cx * kBlockDim + x;
71
3.78M
      }
72
3.78M
    }
73
288k
  }
74
24.2k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<false>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
24
24.2k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
25
24.2k
  size_t cx = acs.covered_blocks_x();
26
24.2k
  size_t cy = acs.covered_blocks_y();
27
24.2k
  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
24.2k
  size_t xs = cx / cy;
33
24.2k
  size_t xsm = xs - 1;
34
24.2k
  size_t xss = CeilLog2Nonzero(xs);
35
  // First half of the block
36
24.2k
  size_t cur = cx * cy;
37
337k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
38
5.95M
    for (size_t j = 0; j <= i; j++) {
39
5.64M
      size_t x = j;
40
5.64M
      size_t y = i - j;
41
5.64M
      if (i % 2) std::swap(x, y);
42
5.64M
      if ((y & xsm) != 0) continue;
43
4.09M
      y >>= xss;
44
4.09M
      size_t val = 0;
45
4.09M
      if (x < cx && y < cy) {
46
123k
        val = y * cx + x;
47
3.97M
      } else {
48
3.97M
        val = cur++;
49
3.97M
      }
50
4.09M
      if (is_lut) {
51
0
        out[y * cx * kBlockDim + x] = val;
52
4.09M
      } else {
53
4.09M
        out[val] = y * cx * kBlockDim + x;
54
4.09M
      }
55
4.09M
    }
56
312k
  }
57
  // Second half
58
312k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
59
288k
    size_t i = ip - 1;
60
5.61M
    for (size_t j = 0; j <= i; j++) {
61
5.32M
      size_t x = cx * kBlockDim - 1 - (i - j);
62
5.32M
      size_t y = cx * kBlockDim - 1 - j;
63
5.32M
      if (i % 2) std::swap(x, y);
64
5.32M
      if ((y & xsm) != 0) continue;
65
3.78M
      y >>= xss;
66
3.78M
      size_t val = cur++;
67
3.78M
      if (is_lut) {
68
0
        out[y * cx * kBlockDim + x] = val;
69
3.78M
      } else {
70
3.78M
        out[val] = y * cx * kBlockDim + x;
71
3.78M
      }
72
3.78M
    }
73
288k
  }
74
24.2k
}
Unexecuted instantiation: ac_strategy.cc:void jxl::CoeffOrderAndLut<true>(jxl::AcStrategy, unsigned int*)
75
76
24.2k
void AcStrategy::ComputeNaturalCoeffOrder(coeff_order_t* order) const {
77
24.2k
  CoeffOrderAndLut</*is_lut=*/false>(*this, order);
78
24.2k
}
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
43.9k
    JxlMemoryManager* memory_manager, size_t xsize, size_t ysize) {
91
43.9k
  AcStrategyImage img;
92
43.9k
  JXL_ASSIGN_OR_RETURN(img.layers_,
93
43.9k
                       ImageB::Create(memory_manager, xsize, ysize));
94
43.9k
  img.row_ = img.layers_.Row(0);
95
43.9k
  img.stride_ = img.layers_.PixelsPerRow();
96
43.9k
  return img;
97
43.9k
}
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