Coverage Report

Created: 2026-02-14 07:42

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
46.5k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
46.5k
  size_t cx = acs.covered_blocks_x();
31
46.5k
  size_t cy = acs.covered_blocks_y();
32
46.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
46.5k
  size_t xs = cx / cy;
38
46.5k
  size_t xsm = xs - 1;
39
46.5k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
46.5k
  size_t cur = cx * cy;
42
1.00M
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
31.6M
    for (size_t j = 0; j <= i; j++) {
44
30.7M
      size_t x = j;
45
30.7M
      size_t y = i - j;
46
30.7M
      if (i % 2) std::swap(x, y);
47
30.7M
      if ((y & xsm) != 0) continue;
48
24.4M
      y >>= xss;
49
24.4M
      size_t val = 0;
50
24.4M
      if (x < cx && y < cy) {
51
750k
        val = y * cx + x;
52
23.7M
      } else {
53
23.7M
        val = cur++;
54
23.7M
      }
55
24.4M
      if (is_lut) {
56
60.0k
        out[y * cx * kBlockDim + x] = val;
57
24.4M
      } else {
58
24.4M
        out[val] = y * cx * kBlockDim + x;
59
24.4M
      }
60
24.4M
    }
61
960k
  }
62
  // Second half
63
960k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
914k
    size_t i = ip - 1;
65
30.6M
    for (size_t j = 0; j <= i; j++) {
66
29.7M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
29.7M
      size_t y = cx * kBlockDim - 1 - j;
68
29.7M
      if (i % 2) std::swap(x, y);
69
29.7M
      if ((y & xsm) != 0) continue;
70
23.5M
      y >>= xss;
71
23.5M
      size_t val = cur++;
72
23.5M
      if (is_lut) {
73
48.9k
        out[y * cx * kBlockDim + x] = val;
74
23.4M
      } else {
75
23.4M
        out[val] = y * cx * kBlockDim + x;
76
23.4M
      }
77
23.5M
    }
78
914k
  }
79
46.5k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<false>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
45.5k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
45.5k
  size_t cx = acs.covered_blocks_x();
31
45.5k
  size_t cy = acs.covered_blocks_y();
32
45.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
45.5k
  size_t xs = cx / cy;
38
45.5k
  size_t xsm = xs - 1;
39
45.5k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
45.5k
  size_t cur = cx * cy;
42
995k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
31.5M
    for (size_t j = 0; j <= i; j++) {
44
30.6M
      size_t x = j;
45
30.6M
      size_t y = i - j;
46
30.6M
      if (i % 2) std::swap(x, y);
47
30.6M
      if ((y & xsm) != 0) continue;
48
24.4M
      y >>= xss;
49
24.4M
      size_t val = 0;
50
24.4M
      if (x < cx && y < cy) {
51
748k
        val = y * cx + x;
52
23.6M
      } else {
53
23.6M
        val = cur++;
54
23.6M
      }
55
24.4M
      if (is_lut) {
56
0
        out[y * cx * kBlockDim + x] = val;
57
24.4M
      } else {
58
24.4M
        out[val] = y * cx * kBlockDim + x;
59
24.4M
      }
60
24.4M
    }
61
949k
  }
62
  // Second half
63
949k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
904k
    size_t i = ip - 1;
65
30.5M
    for (size_t j = 0; j <= i; j++) {
66
29.6M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
29.6M
      size_t y = cx * kBlockDim - 1 - j;
68
29.6M
      if (i % 2) std::swap(x, y);
69
29.6M
      if ((y & xsm) != 0) continue;
70
23.4M
      y >>= xss;
71
23.4M
      size_t val = cur++;
72
23.4M
      if (is_lut) {
73
0
        out[y * cx * kBlockDim + x] = val;
74
23.4M
      } else {
75
23.4M
        out[val] = y * cx * kBlockDim + x;
76
23.4M
      }
77
23.4M
    }
78
904k
  }
79
45.5k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<true>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
1.06k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
1.06k
  size_t cx = acs.covered_blocks_x();
31
1.06k
  size_t cy = acs.covered_blocks_y();
32
1.06k
  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
1.06k
  size_t xs = cx / cy;
38
1.06k
  size_t xsm = xs - 1;
39
1.06k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
1.06k
  size_t cur = cx * cy;
42
12.1k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
91.7k
    for (size_t j = 0; j <= i; j++) {
44
80.6k
      size_t x = j;
45
80.6k
      size_t y = i - j;
46
80.6k
      if (i % 2) std::swap(x, y);
47
80.6k
      if ((y & xsm) != 0) continue;
48
60.0k
      y >>= xss;
49
60.0k
      size_t val = 0;
50
60.0k
      if (x < cx && y < cy) {
51
1.70k
        val = y * cx + x;
52
58.3k
      } else {
53
58.3k
        val = cur++;
54
58.3k
      }
55
60.0k
      if (is_lut) {
56
60.0k
        out[y * cx * kBlockDim + x] = val;
57
60.0k
      } else {
58
0
        out[val] = y * cx * kBlockDim + x;
59
0
      }
60
60.0k
    }
61
11.0k
  }
62
  // Second half
63
11.0k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
10.0k
    size_t i = ip - 1;
65
79.5k
    for (size_t j = 0; j <= i; j++) {
66
69.5k
      size_t x = cx * kBlockDim - 1 - (i - j);
67
69.5k
      size_t y = cx * kBlockDim - 1 - j;
68
69.5k
      if (i % 2) std::swap(x, y);
69
69.5k
      if ((y & xsm) != 0) continue;
70
48.9k
      y >>= xss;
71
48.9k
      size_t val = cur++;
72
48.9k
      if (is_lut) {
73
48.9k
        out[y * cx * kBlockDim + x] = val;
74
48.9k
      } else {
75
0
        out[val] = y * cx * kBlockDim + x;
76
0
      }
77
48.9k
    }
78
10.0k
  }
79
1.06k
}
80
81
45.5k
void AcStrategy::ComputeNaturalCoeffOrder(coeff_order_t* order) const {
82
45.5k
  CoeffOrderAndLut</*is_lut=*/false>(*this, order);
83
45.5k
}
84
1.06k
void AcStrategy::ComputeNaturalCoeffOrderLut(coeff_order_t* lut) const {
85
1.06k
  CoeffOrderAndLut</*is_lut=*/true>(*this, lut);
86
1.06k
}
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
100k
    JxlMemoryManager* memory_manager, size_t xsize, size_t ysize) {
96
100k
  AcStrategyImage img;
97
100k
  JXL_ASSIGN_OR_RETURN(img.layers_,
98
100k
                       ImageB::Create(memory_manager, xsize, ysize));
99
100k
  img.row_ = img.layers_.Row(0);
100
100k
  img.stride_ = img.layers_.PixelsPerRow();
101
100k
  return img;
102
100k
}
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