Coverage Report

Created: 2025-12-03 07:28

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
40.2k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
40.2k
  size_t cx = acs.covered_blocks_x();
31
40.2k
  size_t cy = acs.covered_blocks_y();
32
40.2k
  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
40.2k
  size_t xs = cx / cy;
38
40.2k
  size_t xsm = xs - 1;
39
40.2k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
40.2k
  size_t cur = cx * cy;
42
920k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
26.1M
    for (size_t j = 0; j <= i; j++) {
44
25.2M
      size_t x = j;
45
25.2M
      size_t y = i - j;
46
25.2M
      if (i % 2) std::swap(x, y);
47
25.2M
      if ((y & xsm) != 0) continue;
48
19.1M
      y >>= xss;
49
19.1M
      size_t val = 0;
50
19.1M
      if (x < cx && y < cy) {
51
585k
        val = y * cx + x;
52
18.5M
      } else {
53
18.5M
        val = cur++;
54
18.5M
      }
55
19.1M
      if (is_lut) {
56
887k
        out[y * cx * kBlockDim + x] = val;
57
18.2M
      } else {
58
18.2M
        out[val] = y * cx * kBlockDim + x;
59
18.2M
      }
60
19.1M
    }
61
880k
  }
62
  // Second half
63
880k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
840k
    size_t i = ip - 1;
65
25.2M
    for (size_t j = 0; j <= i; j++) {
66
24.3M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
24.3M
      size_t y = cx * kBlockDim - 1 - j;
68
24.3M
      if (i % 2) std::swap(x, y);
69
24.3M
      if ((y & xsm) != 0) continue;
70
18.2M
      y >>= xss;
71
18.2M
      size_t val = cur++;
72
18.2M
      if (is_lut) {
73
768k
        out[y * cx * kBlockDim + x] = val;
74
17.5M
      } else {
75
17.5M
        out[val] = y * cx * kBlockDim + x;
76
17.5M
      }
77
18.2M
    }
78
840k
  }
79
40.2k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<false>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
31.4k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
31.4k
  size_t cx = acs.covered_blocks_x();
31
31.4k
  size_t cy = acs.covered_blocks_y();
32
31.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
31.4k
  size_t xs = cx / cy;
38
31.4k
  size_t xsm = xs - 1;
39
31.4k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
31.4k
  size_t cur = cx * cy;
42
792k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
24.8M
    for (size_t j = 0; j <= i; j++) {
44
24.0M
      size_t x = j;
45
24.0M
      size_t y = i - j;
46
24.0M
      if (i % 2) std::swap(x, y);
47
24.0M
      if ((y & xsm) != 0) continue;
48
18.2M
      y >>= xss;
49
18.2M
      size_t val = 0;
50
18.2M
      if (x < cx && y < cy) {
51
559k
        val = y * cx + x;
52
17.7M
      } else {
53
17.7M
        val = cur++;
54
17.7M
      }
55
18.2M
      if (is_lut) {
56
0
        out[y * cx * kBlockDim + x] = val;
57
18.2M
      } else {
58
18.2M
        out[val] = y * cx * kBlockDim + x;
59
18.2M
      }
60
18.2M
    }
61
760k
  }
62
  // Second half
63
760k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
729k
    size_t i = ip - 1;
65
24.0M
    for (size_t j = 0; j <= i; j++) {
66
23.3M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
23.3M
      size_t y = cx * kBlockDim - 1 - j;
68
23.3M
      if (i % 2) std::swap(x, y);
69
23.3M
      if ((y & xsm) != 0) continue;
70
17.5M
      y >>= xss;
71
17.5M
      size_t val = cur++;
72
17.5M
      if (is_lut) {
73
0
        out[y * cx * kBlockDim + x] = val;
74
17.5M
      } else {
75
17.5M
        out[val] = y * cx * kBlockDim + x;
76
17.5M
      }
77
17.5M
    }
78
729k
  }
79
31.4k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<true>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
8.81k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
8.81k
  size_t cx = acs.covered_blocks_x();
31
8.81k
  size_t cy = acs.covered_blocks_y();
32
8.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
8.81k
  size_t xs = cx / cy;
38
8.81k
  size_t xsm = xs - 1;
39
8.81k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
8.81k
  size_t cur = cx * cy;
42
128k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
1.29M
    for (size_t j = 0; j <= i; j++) {
44
1.17M
      size_t x = j;
45
1.17M
      size_t y = i - j;
46
1.17M
      if (i % 2) std::swap(x, y);
47
1.17M
      if ((y & xsm) != 0) continue;
48
887k
      y >>= xss;
49
887k
      size_t val = 0;
50
887k
      if (x < cx && y < cy) {
51
25.8k
        val = y * cx + x;
52
861k
      } else {
53
861k
        val = cur++;
54
861k
      }
55
887k
      if (is_lut) {
56
887k
        out[y * cx * kBlockDim + x] = val;
57
887k
      } else {
58
0
        out[val] = y * cx * kBlockDim + x;
59
0
      }
60
887k
    }
61
119k
  }
62
  // Second half
63
119k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
110k
    size_t i = ip - 1;
65
1.16M
    for (size_t j = 0; j <= i; j++) {
66
1.05M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
1.05M
      size_t y = cx * kBlockDim - 1 - j;
68
1.05M
      if (i % 2) std::swap(x, y);
69
1.05M
      if ((y & xsm) != 0) continue;
70
768k
      y >>= xss;
71
768k
      size_t val = cur++;
72
768k
      if (is_lut) {
73
768k
        out[y * cx * kBlockDim + x] = val;
74
768k
      } else {
75
0
        out[val] = y * cx * kBlockDim + x;
76
0
      }
77
768k
    }
78
110k
  }
79
8.81k
}
80
81
31.4k
void AcStrategy::ComputeNaturalCoeffOrder(coeff_order_t* order) const {
82
31.4k
  CoeffOrderAndLut</*is_lut=*/false>(*this, order);
83
31.4k
}
84
8.81k
void AcStrategy::ComputeNaturalCoeffOrderLut(coeff_order_t* lut) const {
85
8.81k
  CoeffOrderAndLut</*is_lut=*/true>(*this, lut);
86
8.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
126k
    JxlMemoryManager* memory_manager, size_t xsize, size_t ysize) {
96
126k
  AcStrategyImage img;
97
126k
  JXL_ASSIGN_OR_RETURN(img.layers_,
98
126k
                       ImageB::Create(memory_manager, xsize, ysize));
99
126k
  img.row_ = img.layers_.Row(0);
100
126k
  img.stride_ = img.layers_.PixelsPerRow();
101
126k
  return img;
102
126k
}
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