Coverage Report

Created: 2026-10-28 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/ac_strategy.cc
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
#include "lib/jxl/ac_strategy.h"
7
8
#include <jxl/memory_manager.h>
9
10
#include <algorithm>
11
#include <cstdint>
12
#include <cstring>
13
#include <utility>
14
15
#include "lib/jxl/base/bits.h"
16
#include "lib/jxl/base/compiler_specific.h"
17
#include "lib/jxl/base/status.h"
18
#include "lib/jxl/coeff_order_fwd.h"
19
#include "lib/jxl/frame_dimensions.h"
20
#include "lib/jxl/image.h"
21
22
namespace jxl {
23
24
// Tries to generalize zig-zag order to non-square blocks. Surprisingly, in
25
// square block frequency along the (i + j == const) diagonals is roughly the
26
// same. For historical reasons, consecutive diagonals are traversed
27
// in alternating directions - so called "zig-zag" (or "snake") order.
28
template <bool is_lut>
29
152k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
152k
  size_t cx = acs.covered_blocks_x();
31
152k
  size_t cy = acs.covered_blocks_y();
32
152k
  CoefficientLayout(&cy, &cx);
33
34
  // CoefficientLayout ensures cx >= cy.
35
  // We compute the zigzag order for a cx x cx block, then discard all the
36
  // lines that are not multiple of the ratio between cx and cy.
37
152k
  size_t xs = cx / cy;
38
152k
  size_t xsm = xs - 1;
39
152k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
152k
  size_t cur = cx * cy;
42
2.99M
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
55.6M
    for (size_t j = 0; j <= i; j++) {
44
52.8M
      size_t x = j;
45
52.8M
      size_t y = i - j;
46
52.8M
      if (i % 2) std::swap(x, y);
47
52.8M
      if ((y & xsm) != 0) continue;
48
40.4M
      y >>= xss;
49
40.4M
      size_t val = 0;
50
40.4M
      if (x < cx && y < cy) {
51
1.21M
        val = y * cx + x;
52
39.1M
      } else {
53
39.1M
        val = cur++;
54
39.1M
      }
55
40.4M
      if (is_lut) {
56
1.62M
        out[y * cx * kBlockDim + x] = val;
57
38.7M
      } else {
58
38.7M
        out[val] = y * cx * kBlockDim + x;
59
38.7M
      }
60
40.4M
    }
61
2.84M
  }
62
  // Second half
63
2.84M
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
2.69M
    size_t i = ip - 1;
65
52.6M
    for (size_t j = 0; j <= i; j++) {
66
49.9M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
49.9M
      size_t y = cx * kBlockDim - 1 - j;
68
49.9M
      if (i % 2) std::swap(x, y);
69
49.9M
      if ((y & xsm) != 0) continue;
70
37.5M
      y >>= xss;
71
37.5M
      size_t val = cur++;
72
37.5M
      if (is_lut) {
73
1.42M
        out[y * cx * kBlockDim + x] = val;
74
36.1M
      } else {
75
36.1M
        out[val] = y * cx * kBlockDim + x;
76
36.1M
      }
77
37.5M
    }
78
2.69M
  }
79
152k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<false>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
138k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
138k
  size_t cx = acs.covered_blocks_x();
31
138k
  size_t cy = acs.covered_blocks_y();
32
138k
  CoefficientLayout(&cy, &cx);
33
34
  // CoefficientLayout ensures cx >= cy.
35
  // We compute the zigzag order for a cx x cx block, then discard all the
36
  // lines that are not multiple of the ratio between cx and cy.
37
138k
  size_t xs = cx / cy;
38
138k
  size_t xsm = xs - 1;
39
138k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
138k
  size_t cur = cx * cy;
42
2.78M
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
53.4M
    for (size_t j = 0; j <= i; j++) {
44
50.7M
      size_t x = j;
45
50.7M
      size_t y = i - j;
46
50.7M
      if (i % 2) std::swap(x, y);
47
50.7M
      if ((y & xsm) != 0) continue;
48
38.7M
      y >>= xss;
49
38.7M
      size_t val = 0;
50
38.7M
      if (x < cx && y < cy) {
51
1.17M
        val = y * cx + x;
52
37.6M
      } else {
53
37.6M
        val = cur++;
54
37.6M
      }
55
38.7M
      if (is_lut) {
56
0
        out[y * cx * kBlockDim + x] = val;
57
38.7M
      } else {
58
38.7M
        out[val] = y * cx * kBlockDim + x;
59
38.7M
      }
60
38.7M
    }
61
2.65M
  }
62
  // Second half
63
2.65M
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
2.51M
    size_t i = ip - 1;
65
50.6M
    for (size_t j = 0; j <= i; j++) {
66
48.1M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
48.1M
      size_t y = cx * kBlockDim - 1 - j;
68
48.1M
      if (i % 2) std::swap(x, y);
69
48.1M
      if ((y & xsm) != 0) continue;
70
36.1M
      y >>= xss;
71
36.1M
      size_t val = cur++;
72
36.1M
      if (is_lut) {
73
0
        out[y * cx * kBlockDim + x] = val;
74
36.1M
      } else {
75
36.1M
        out[val] = y * cx * kBlockDim + x;
76
36.1M
      }
77
36.1M
    }
78
2.51M
  }
79
138k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<true>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
13.8k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
13.8k
  size_t cx = acs.covered_blocks_x();
31
13.8k
  size_t cy = acs.covered_blocks_y();
32
13.8k
  CoefficientLayout(&cy, &cx);
33
34
  // CoefficientLayout ensures cx >= cy.
35
  // We compute the zigzag order for a cx x cx block, then discard all the
36
  // lines that are not multiple of the ratio between cx and cy.
37
13.8k
  size_t xs = cx / cy;
38
13.8k
  size_t xsm = xs - 1;
39
13.8k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
13.8k
  size_t cur = cx * cy;
42
209k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
2.19M
    for (size_t j = 0; j <= i; j++) {
44
2.00M
      size_t x = j;
45
2.00M
      size_t y = i - j;
46
2.00M
      if (i % 2) std::swap(x, y);
47
2.00M
      if ((y & xsm) != 0) continue;
48
1.62M
      y >>= xss;
49
1.62M
      size_t val = 0;
50
1.62M
      if (x < cx && y < cy) {
51
47.6k
        val = y * cx + x;
52
1.57M
      } else {
53
1.57M
        val = cur++;
54
1.57M
      }
55
1.62M
      if (is_lut) {
56
1.62M
        out[y * cx * kBlockDim + x] = val;
57
1.62M
      } else {
58
0
        out[val] = y * cx * kBlockDim + x;
59
0
      }
60
1.62M
    }
61
195k
  }
62
  // Second half
63
195k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
181k
    size_t i = ip - 1;
65
1.99M
    for (size_t j = 0; j <= i; j++) {
66
1.80M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
1.80M
      size_t y = cx * kBlockDim - 1 - j;
68
1.80M
      if (i % 2) std::swap(x, y);
69
1.80M
      if ((y & xsm) != 0) continue;
70
1.42M
      y >>= xss;
71
1.42M
      size_t val = cur++;
72
1.42M
      if (is_lut) {
73
1.42M
        out[y * cx * kBlockDim + x] = val;
74
1.42M
      } else {
75
0
        out[val] = y * cx * kBlockDim + x;
76
0
      }
77
1.42M
    }
78
181k
  }
79
13.8k
}
80
81
138k
void AcStrategy::ComputeNaturalCoeffOrder(coeff_order_t* order) const {
82
138k
  CoeffOrderAndLut</*is_lut=*/false>(*this, order);
83
138k
}
84
13.8k
void AcStrategy::ComputeNaturalCoeffOrderLut(coeff_order_t* lut) const {
85
13.8k
  CoeffOrderAndLut</*is_lut=*/true>(*this, lut);
86
13.8k
}
87
88
#if JXL_CXX_LANG < JXL_CXX_17
89
constexpr size_t AcStrategy::kMaxCoeffBlocks;
90
constexpr size_t AcStrategy::kMaxBlockDim;
91
constexpr size_t AcStrategy::kMaxCoeffArea;
92
#endif
93
94
StatusOr<AcStrategyImage> AcStrategyImage::Create(
95
293k
    JxlMemoryManager* memory_manager, size_t xsize, size_t ysize) {
96
293k
  AcStrategyImage img;
97
293k
  JXL_ASSIGN_OR_RETURN(img.layers_,
98
293k
                       ImageB::Create(memory_manager, xsize, ysize));
99
293k
  img.row_ = img.layers_.Row(0);
100
293k
  img.stride_ = img.layers_.PixelsPerRow();
101
293k
  return img;
102
293k
}
103
104
0
size_t AcStrategyImage::CountBlocks(AcStrategyType type) const {
105
0
  size_t ret = 0;
106
0
  for (size_t y = 0; y < layers_.ysize(); y++) {
107
0
    const uint8_t* JXL_RESTRICT row = layers_.ConstRow(y);
108
0
    for (size_t x = 0; x < layers_.xsize(); x++) {
109
0
      if (row[x] == ((static_cast<uint8_t>(type) << 1) | 1)) ret++;
110
0
    }
111
0
  }
112
0
  return ret;
113
0
}
114
115
}  // namespace jxl