Coverage Report

Created: 2025-12-31 07:53

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
47.4k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
47.4k
  size_t cx = acs.covered_blocks_x();
31
47.4k
  size_t cy = acs.covered_blocks_y();
32
47.4k
  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
47.4k
  size_t xs = cx / cy;
38
47.4k
  size_t xsm = xs - 1;
39
47.4k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
47.4k
  size_t cur = cx * cy;
42
1.07M
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
28.9M
    for (size_t j = 0; j <= i; j++) {
44
27.9M
      size_t x = j;
45
27.9M
      size_t y = i - j;
46
27.9M
      if (i % 2) std::swap(x, y);
47
27.9M
      if ((y & xsm) != 0) continue;
48
21.2M
      y >>= xss;
49
21.2M
      size_t val = 0;
50
21.2M
      if (x < cx && y < cy) {
51
647k
        val = y * cx + x;
52
20.5M
      } else {
53
20.5M
        val = cur++;
54
20.5M
      }
55
21.2M
      if (is_lut) {
56
1.17M
        out[y * cx * kBlockDim + x] = val;
57
20.0M
      } else {
58
20.0M
        out[val] = y * cx * kBlockDim + x;
59
20.0M
      }
60
21.2M
    }
61
1.03M
  }
62
  // Second half
63
1.03M
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
983k
    size_t i = ip - 1;
65
27.9M
    for (size_t j = 0; j <= i; j++) {
66
26.9M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
26.9M
      size_t y = cx * kBlockDim - 1 - j;
68
26.9M
      if (i % 2) std::swap(x, y);
69
26.9M
      if ((y & xsm) != 0) continue;
70
20.1M
      y >>= xss;
71
20.1M
      size_t val = cur++;
72
20.1M
      if (is_lut) {
73
1.02M
        out[y * cx * kBlockDim + x] = val;
74
19.1M
      } else {
75
19.1M
        out[val] = y * cx * kBlockDim + x;
76
19.1M
      }
77
20.1M
    }
78
983k
  }
79
47.4k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<false>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
36.5k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
36.5k
  size_t cx = acs.covered_blocks_x();
31
36.5k
  size_t cy = acs.covered_blocks_y();
32
36.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
36.5k
  size_t xs = cx / cy;
38
36.5k
  size_t xsm = xs - 1;
39
36.5k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
36.5k
  size_t cur = cx * cy;
42
915k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
27.2M
    for (size_t j = 0; j <= i; j++) {
44
26.4M
      size_t x = j;
45
26.4M
      size_t y = i - j;
46
26.4M
      if (i % 2) std::swap(x, y);
47
26.4M
      if ((y & xsm) != 0) continue;
48
20.0M
      y >>= xss;
49
20.0M
      size_t val = 0;
50
20.0M
      if (x < cx && y < cy) {
51
612k
        val = y * cx + x;
52
19.4M
      } else {
53
19.4M
        val = cur++;
54
19.4M
      }
55
20.0M
      if (is_lut) {
56
0
        out[y * cx * kBlockDim + x] = val;
57
20.0M
      } else {
58
20.0M
        out[val] = y * cx * kBlockDim + x;
59
20.0M
      }
60
20.0M
    }
61
879k
  }
62
  // Second half
63
879k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
842k
    size_t i = ip - 1;
65
26.3M
    for (size_t j = 0; j <= i; j++) {
66
25.5M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
25.5M
      size_t y = cx * kBlockDim - 1 - j;
68
25.5M
      if (i % 2) std::swap(x, y);
69
25.5M
      if ((y & xsm) != 0) continue;
70
19.1M
      y >>= xss;
71
19.1M
      size_t val = cur++;
72
19.1M
      if (is_lut) {
73
0
        out[y * cx * kBlockDim + x] = val;
74
19.1M
      } else {
75
19.1M
        out[val] = y * cx * kBlockDim + x;
76
19.1M
      }
77
19.1M
    }
78
842k
  }
79
36.5k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<true>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
10.8k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
10.8k
  size_t cx = acs.covered_blocks_x();
31
10.8k
  size_t cy = acs.covered_blocks_y();
32
10.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
10.8k
  size_t xs = cx / cy;
38
10.8k
  size_t xsm = xs - 1;
39
10.8k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
10.8k
  size_t cur = cx * cy;
42
162k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
1.69M
    for (size_t j = 0; j <= i; j++) {
44
1.54M
      size_t x = j;
45
1.54M
      size_t y = i - j;
46
1.54M
      if (i % 2) std::swap(x, y);
47
1.54M
      if ((y & xsm) != 0) continue;
48
1.17M
      y >>= xss;
49
1.17M
      size_t val = 0;
50
1.17M
      if (x < cx && y < cy) {
51
34.3k
        val = y * cx + x;
52
1.14M
      } else {
53
1.14M
        val = cur++;
54
1.14M
      }
55
1.17M
      if (is_lut) {
56
1.17M
        out[y * cx * kBlockDim + x] = val;
57
1.17M
      } else {
58
0
        out[val] = y * cx * kBlockDim + x;
59
0
      }
60
1.17M
    }
61
151k
  }
62
  // Second half
63
151k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
141k
    size_t i = ip - 1;
65
1.53M
    for (size_t j = 0; j <= i; j++) {
66
1.38M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
1.38M
      size_t y = cx * kBlockDim - 1 - j;
68
1.38M
      if (i % 2) std::swap(x, y);
69
1.38M
      if ((y & xsm) != 0) continue;
70
1.02M
      y >>= xss;
71
1.02M
      size_t val = cur++;
72
1.02M
      if (is_lut) {
73
1.02M
        out[y * cx * kBlockDim + x] = val;
74
1.02M
      } else {
75
0
        out[val] = y * cx * kBlockDim + x;
76
0
      }
77
1.02M
    }
78
141k
  }
79
10.8k
}
80
81
36.5k
void AcStrategy::ComputeNaturalCoeffOrder(coeff_order_t* order) const {
82
36.5k
  CoeffOrderAndLut</*is_lut=*/false>(*this, order);
83
36.5k
}
84
10.8k
void AcStrategy::ComputeNaturalCoeffOrderLut(coeff_order_t* lut) const {
85
10.8k
  CoeffOrderAndLut</*is_lut=*/true>(*this, lut);
86
10.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
135k
    JxlMemoryManager* memory_manager, size_t xsize, size_t ysize) {
96
135k
  AcStrategyImage img;
97
135k
  JXL_ASSIGN_OR_RETURN(img.layers_,
98
135k
                       ImageB::Create(memory_manager, xsize, ysize));
99
135k
  img.row_ = img.layers_.Row(0);
100
135k
  img.stride_ = img.layers_.PixelsPerRow();
101
135k
  return img;
102
135k
}
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