Coverage Report

Created: 2026-03-31 06:56

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
24.3k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
24.3k
  size_t cx = acs.covered_blocks_x();
31
24.3k
  size_t cy = acs.covered_blocks_y();
32
24.3k
  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
24.3k
  size_t xs = cx / cy;
38
24.3k
  size_t xsm = xs - 1;
39
24.3k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
24.3k
  size_t cur = cx * cy;
42
497k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
11.7M
    for (size_t j = 0; j <= i; j++) {
44
11.2M
      size_t x = j;
45
11.2M
      size_t y = i - j;
46
11.2M
      if (i % 2) std::swap(x, y);
47
11.2M
      if ((y & xsm) != 0) continue;
48
8.36M
      y >>= xss;
49
8.36M
      size_t val = 0;
50
8.36M
      if (x < cx && y < cy) {
51
253k
        val = y * cx + x;
52
8.10M
      } else {
53
8.10M
        val = cur++;
54
8.10M
      }
55
8.36M
      if (is_lut) {
56
341k
        out[y * cx * kBlockDim + x] = val;
57
8.01M
      } else {
58
8.01M
        out[val] = y * cx * kBlockDim + x;
59
8.01M
      }
60
8.36M
    }
61
472k
  }
62
  // Second half
63
472k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
448k
    size_t i = ip - 1;
65
11.2M
    for (size_t j = 0; j <= i; j++) {
66
10.7M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
10.7M
      size_t y = cx * kBlockDim - 1 - j;
68
10.7M
      if (i % 2) std::swap(x, y);
69
10.7M
      if ((y & xsm) != 0) continue;
70
7.88M
      y >>= xss;
71
7.88M
      size_t val = cur++;
72
7.88M
      if (is_lut) {
73
293k
        out[y * cx * kBlockDim + x] = val;
74
7.59M
      } else {
75
7.59M
        out[val] = y * cx * kBlockDim + x;
76
7.59M
      }
77
7.88M
    }
78
448k
  }
79
24.3k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<false>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
20.5k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
20.5k
  size_t cx = acs.covered_blocks_x();
31
20.5k
  size_t cy = acs.covered_blocks_y();
32
20.5k
  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
20.5k
  size_t xs = cx / cy;
38
20.5k
  size_t xsm = xs - 1;
39
20.5k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
20.5k
  size_t cur = cx * cy;
42
445k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
11.2M
    for (size_t j = 0; j <= i; j++) {
44
10.8M
      size_t x = j;
45
10.8M
      size_t y = i - j;
46
10.8M
      if (i % 2) std::swap(x, y);
47
10.8M
      if ((y & xsm) != 0) continue;
48
8.01M
      y >>= xss;
49
8.01M
      size_t val = 0;
50
8.01M
      if (x < cx && y < cy) {
51
243k
        val = y * cx + x;
52
7.77M
      } else {
53
7.77M
        val = cur++;
54
7.77M
      }
55
8.01M
      if (is_lut) {
56
0
        out[y * cx * kBlockDim + x] = val;
57
8.01M
      } else {
58
8.01M
        out[val] = y * cx * kBlockDim + x;
59
8.01M
      }
60
8.01M
    }
61
425k
  }
62
  // Second half
63
425k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
404k
    size_t i = ip - 1;
65
10.7M
    for (size_t j = 0; j <= i; j++) {
66
10.3M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
10.3M
      size_t y = cx * kBlockDim - 1 - j;
68
10.3M
      if (i % 2) std::swap(x, y);
69
10.3M
      if ((y & xsm) != 0) continue;
70
7.59M
      y >>= xss;
71
7.59M
      size_t val = cur++;
72
7.59M
      if (is_lut) {
73
0
        out[y * cx * kBlockDim + x] = val;
74
7.59M
      } else {
75
7.59M
        out[val] = y * cx * kBlockDim + x;
76
7.59M
      }
77
7.59M
    }
78
404k
  }
79
20.5k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<true>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
3.76k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
3.76k
  size_t cx = acs.covered_blocks_x();
31
3.76k
  size_t cy = acs.covered_blocks_y();
32
3.76k
  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
3.76k
  size_t xs = cx / cy;
38
3.76k
  size_t xsm = xs - 1;
39
3.76k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
3.76k
  size_t cur = cx * cy;
42
51.5k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
485k
    for (size_t j = 0; j <= i; j++) {
44
438k
      size_t x = j;
45
438k
      size_t y = i - j;
46
438k
      if (i % 2) std::swap(x, y);
47
438k
      if ((y & xsm) != 0) continue;
48
341k
      y >>= xss;
49
341k
      size_t val = 0;
50
341k
      if (x < cx && y < cy) {
51
9.92k
        val = y * cx + x;
52
331k
      } else {
53
331k
        val = cur++;
54
331k
      }
55
341k
      if (is_lut) {
56
341k
        out[y * cx * kBlockDim + x] = val;
57
341k
      } else {
58
0
        out[val] = y * cx * kBlockDim + x;
59
0
      }
60
341k
    }
61
47.7k
  }
62
  // Second half
63
47.7k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
44.0k
    size_t i = ip - 1;
65
434k
    for (size_t j = 0; j <= i; j++) {
66
390k
      size_t x = cx * kBlockDim - 1 - (i - j);
67
390k
      size_t y = cx * kBlockDim - 1 - j;
68
390k
      if (i % 2) std::swap(x, y);
69
390k
      if ((y & xsm) != 0) continue;
70
293k
      y >>= xss;
71
293k
      size_t val = cur++;
72
293k
      if (is_lut) {
73
293k
        out[y * cx * kBlockDim + x] = val;
74
293k
      } else {
75
0
        out[val] = y * cx * kBlockDim + x;
76
0
      }
77
293k
    }
78
44.0k
  }
79
3.76k
}
80
81
20.5k
void AcStrategy::ComputeNaturalCoeffOrder(coeff_order_t* order) const {
82
20.5k
  CoeffOrderAndLut</*is_lut=*/false>(*this, order);
83
20.5k
}
84
3.76k
void AcStrategy::ComputeNaturalCoeffOrderLut(coeff_order_t* lut) const {
85
3.76k
  CoeffOrderAndLut</*is_lut=*/true>(*this, lut);
86
3.76k
}
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
39.1k
    JxlMemoryManager* memory_manager, size_t xsize, size_t ysize) {
96
39.1k
  AcStrategyImage img;
97
39.1k
  JXL_ASSIGN_OR_RETURN(img.layers_,
98
39.1k
                       ImageB::Create(memory_manager, xsize, ysize));
99
39.1k
  img.row_ = img.layers_.Row(0);
100
39.1k
  img.stride_ = img.layers_.PixelsPerRow();
101
39.1k
  return img;
102
39.1k
}
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