Coverage Report

Created: 2026-05-24 07:45

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
125k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
125k
  size_t cx = acs.covered_blocks_x();
31
125k
  size_t cy = acs.covered_blocks_y();
32
125k
  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
125k
  size_t xs = cx / cy;
38
125k
  size_t xsm = xs - 1;
39
125k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
125k
  size_t cur = cx * cy;
42
2.53M
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
48.9M
    for (size_t j = 0; j <= i; j++) {
44
46.5M
      size_t x = j;
45
46.5M
      size_t y = i - j;
46
46.5M
      if (i % 2) std::swap(x, y);
47
46.5M
      if ((y & xsm) != 0) continue;
48
36.0M
      y >>= xss;
49
36.0M
      size_t val = 0;
50
36.0M
      if (x < cx && y < cy) {
51
1.08M
        val = y * cx + x;
52
34.9M
      } else {
53
34.9M
        val = cur++;
54
34.9M
      }
55
36.0M
      if (is_lut) {
56
1.06M
        out[y * cx * kBlockDim + x] = val;
57
35.0M
      } else {
58
35.0M
        out[val] = y * cx * kBlockDim + x;
59
35.0M
      }
60
36.0M
    }
61
2.40M
  }
62
  // Second half
63
2.40M
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
2.28M
    size_t i = ip - 1;
65
46.3M
    for (size_t j = 0; j <= i; j++) {
66
44.0M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
44.0M
      size_t y = cx * kBlockDim - 1 - j;
68
44.0M
      if (i % 2) std::swap(x, y);
69
44.0M
      if ((y & xsm) != 0) continue;
70
33.6M
      y >>= xss;
71
33.6M
      size_t val = cur++;
72
33.6M
      if (is_lut) {
73
930k
        out[y * cx * kBlockDim + x] = val;
74
32.7M
      } else {
75
32.7M
        out[val] = y * cx * kBlockDim + x;
76
32.7M
      }
77
33.6M
    }
78
2.28M
  }
79
125k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<false>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
115k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
115k
  size_t cx = acs.covered_blocks_x();
31
115k
  size_t cy = acs.covered_blocks_y();
32
115k
  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
115k
  size_t xs = cx / cy;
38
115k
  size_t xsm = xs - 1;
39
115k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
115k
  size_t cur = cx * cy;
42
2.39M
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
47.4M
    for (size_t j = 0; j <= i; j++) {
44
45.2M
      size_t x = j;
45
45.2M
      size_t y = i - j;
46
45.2M
      if (i % 2) std::swap(x, y);
47
45.2M
      if ((y & xsm) != 0) continue;
48
35.0M
      y >>= xss;
49
35.0M
      size_t val = 0;
50
35.0M
      if (x < cx && y < cy) {
51
1.05M
        val = y * cx + x;
52
33.9M
      } else {
53
33.9M
        val = cur++;
54
33.9M
      }
55
35.0M
      if (is_lut) {
56
0
        out[y * cx * kBlockDim + x] = val;
57
35.0M
      } else {
58
35.0M
        out[val] = y * cx * kBlockDim + x;
59
35.0M
      }
60
35.0M
    }
61
2.27M
  }
62
  // Second half
63
2.27M
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
2.16M
    size_t i = ip - 1;
65
45.1M
    for (size_t j = 0; j <= i; j++) {
66
42.9M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
42.9M
      size_t y = cx * kBlockDim - 1 - j;
68
42.9M
      if (i % 2) std::swap(x, y);
69
42.9M
      if ((y & xsm) != 0) continue;
70
32.7M
      y >>= xss;
71
32.7M
      size_t val = cur++;
72
32.7M
      if (is_lut) {
73
0
        out[y * cx * kBlockDim + x] = val;
74
32.7M
      } else {
75
32.7M
        out[val] = y * cx * kBlockDim + x;
76
32.7M
      }
77
32.7M
    }
78
2.16M
  }
79
115k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<true>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
9.81k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
9.81k
  size_t cx = acs.covered_blocks_x();
31
9.81k
  size_t cy = acs.covered_blocks_y();
32
9.81k
  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
9.81k
  size_t xs = cx / cy;
38
9.81k
  size_t xsm = xs - 1;
39
9.81k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
9.81k
  size_t cur = cx * cy;
42
141k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
1.41M
    for (size_t j = 0; j <= i; j++) {
44
1.28M
      size_t x = j;
45
1.28M
      size_t y = i - j;
46
1.28M
      if (i % 2) std::swap(x, y);
47
1.28M
      if ((y & xsm) != 0) continue;
48
1.06M
      y >>= xss;
49
1.06M
      size_t val = 0;
50
1.06M
      if (x < cx && y < cy) {
51
31.1k
        val = y * cx + x;
52
1.03M
      } else {
53
1.03M
        val = cur++;
54
1.03M
      }
55
1.06M
      if (is_lut) {
56
1.06M
        out[y * cx * kBlockDim + x] = val;
57
1.06M
      } else {
58
0
        out[val] = y * cx * kBlockDim + x;
59
0
      }
60
1.06M
    }
61
131k
  }
62
  // Second half
63
131k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
121k
    size_t i = ip - 1;
65
1.27M
    for (size_t j = 0; j <= i; j++) {
66
1.15M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
1.15M
      size_t y = cx * kBlockDim - 1 - j;
68
1.15M
      if (i % 2) std::swap(x, y);
69
1.15M
      if ((y & xsm) != 0) continue;
70
930k
      y >>= xss;
71
930k
      size_t val = cur++;
72
930k
      if (is_lut) {
73
930k
        out[y * cx * kBlockDim + x] = val;
74
930k
      } else {
75
0
        out[val] = y * cx * kBlockDim + x;
76
0
      }
77
930k
    }
78
121k
  }
79
9.81k
}
80
81
115k
void AcStrategy::ComputeNaturalCoeffOrder(coeff_order_t* order) const {
82
115k
  CoeffOrderAndLut</*is_lut=*/false>(*this, order);
83
115k
}
84
9.81k
void AcStrategy::ComputeNaturalCoeffOrderLut(coeff_order_t* lut) const {
85
9.81k
  CoeffOrderAndLut</*is_lut=*/true>(*this, lut);
86
9.81k
}
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
210k
    JxlMemoryManager* memory_manager, size_t xsize, size_t ysize) {
96
210k
  AcStrategyImage img;
97
210k
  JXL_ASSIGN_OR_RETURN(img.layers_,
98
210k
                       ImageB::Create(memory_manager, xsize, ysize));
99
210k
  img.row_ = img.layers_.Row(0);
100
210k
  img.stride_ = img.layers_.PixelsPerRow();
101
210k
  return img;
102
210k
}
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