Coverage Report

Created: 2025-11-16 07:22

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
37.5k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
37.5k
  size_t cx = acs.covered_blocks_x();
31
37.5k
  size_t cy = acs.covered_blocks_y();
32
37.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
37.5k
  size_t xs = cx / cy;
38
37.5k
  size_t xsm = xs - 1;
39
37.5k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
37.5k
  size_t cur = cx * cy;
42
875k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
25.2M
    for (size_t j = 0; j <= i; j++) {
44
24.4M
      size_t x = j;
45
24.4M
      size_t y = i - j;
46
24.4M
      if (i % 2) std::swap(x, y);
47
24.4M
      if ((y & xsm) != 0) continue;
48
18.4M
      y >>= xss;
49
18.4M
      size_t val = 0;
50
18.4M
      if (x < cx && y < cy) {
51
563k
        val = y * cx + x;
52
17.8M
      } else {
53
17.8M
        val = cur++;
54
17.8M
      }
55
18.4M
      if (is_lut) {
56
782k
        out[y * cx * kBlockDim + x] = val;
57
17.6M
      } else {
58
17.6M
        out[val] = y * cx * kBlockDim + x;
59
17.6M
      }
60
18.4M
    }
61
837k
  }
62
  // Second half
63
837k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
800k
    size_t i = ip - 1;
65
24.3M
    for (size_t j = 0; j <= i; j++) {
66
23.5M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
23.5M
      size_t y = cx * kBlockDim - 1 - j;
68
23.5M
      if (i % 2) std::swap(x, y);
69
23.5M
      if ((y & xsm) != 0) continue;
70
17.6M
      y >>= xss;
71
17.6M
      size_t val = cur++;
72
17.6M
      if (is_lut) {
73
674k
        out[y * cx * kBlockDim + x] = val;
74
16.9M
      } else {
75
16.9M
        out[val] = y * cx * kBlockDim + x;
76
16.9M
      }
77
17.6M
    }
78
800k
  }
79
37.5k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<false>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
29.5k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
29.5k
  size_t cx = acs.covered_blocks_x();
31
29.5k
  size_t cy = acs.covered_blocks_y();
32
29.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
29.5k
  size_t xs = cx / cy;
38
29.5k
  size_t xsm = xs - 1;
39
29.5k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
29.5k
  size_t cur = cx * cy;
42
758k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
24.0M
    for (size_t j = 0; j <= i; j++) {
44
23.3M
      size_t x = j;
45
23.3M
      size_t y = i - j;
46
23.3M
      if (i % 2) std::swap(x, y);
47
23.3M
      if ((y & xsm) != 0) continue;
48
17.6M
      y >>= xss;
49
17.6M
      size_t val = 0;
50
17.6M
      if (x < cx && y < cy) {
51
540k
        val = y * cx + x;
52
17.1M
      } else {
53
17.1M
        val = cur++;
54
17.1M
      }
55
17.6M
      if (is_lut) {
56
0
        out[y * cx * kBlockDim + x] = val;
57
17.6M
      } else {
58
17.6M
        out[val] = y * cx * kBlockDim + x;
59
17.6M
      }
60
17.6M
    }
61
729k
  }
62
  // Second half
63
729k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
699k
    size_t i = ip - 1;
65
23.3M
    for (size_t j = 0; j <= i; j++) {
66
22.6M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
22.6M
      size_t y = cx * kBlockDim - 1 - j;
68
22.6M
      if (i % 2) std::swap(x, y);
69
22.6M
      if ((y & xsm) != 0) continue;
70
16.9M
      y >>= xss;
71
16.9M
      size_t val = cur++;
72
16.9M
      if (is_lut) {
73
0
        out[y * cx * kBlockDim + x] = val;
74
16.9M
      } else {
75
16.9M
        out[val] = y * cx * kBlockDim + x;
76
16.9M
      }
77
16.9M
    }
78
699k
  }
79
29.5k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<true>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
8.04k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
8.04k
  size_t cx = acs.covered_blocks_x();
31
8.04k
  size_t cy = acs.covered_blocks_y();
32
8.04k
  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.04k
  size_t xs = cx / cy;
38
8.04k
  size_t xsm = xs - 1;
39
8.04k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
8.04k
  size_t cur = cx * cy;
42
116k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
1.15M
    for (size_t j = 0; j <= i; j++) {
44
1.05M
      size_t x = j;
45
1.05M
      size_t y = i - j;
46
1.05M
      if (i % 2) std::swap(x, y);
47
1.05M
      if ((y & xsm) != 0) continue;
48
782k
      y >>= xss;
49
782k
      size_t val = 0;
50
782k
      if (x < cx && y < cy) {
51
22.7k
        val = y * cx + x;
52
760k
      } else {
53
760k
        val = cur++;
54
760k
      }
55
782k
      if (is_lut) {
56
782k
        out[y * cx * kBlockDim + x] = val;
57
782k
      } else {
58
0
        out[val] = y * cx * kBlockDim + x;
59
0
      }
60
782k
    }
61
108k
  }
62
  // Second half
63
108k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
100k
    size_t i = ip - 1;
65
1.04M
    for (size_t j = 0; j <= i; j++) {
66
942k
      size_t x = cx * kBlockDim - 1 - (i - j);
67
942k
      size_t y = cx * kBlockDim - 1 - j;
68
942k
      if (i % 2) std::swap(x, y);
69
942k
      if ((y & xsm) != 0) continue;
70
674k
      y >>= xss;
71
674k
      size_t val = cur++;
72
674k
      if (is_lut) {
73
674k
        out[y * cx * kBlockDim + x] = val;
74
674k
      } else {
75
0
        out[val] = y * cx * kBlockDim + x;
76
0
      }
77
674k
    }
78
100k
  }
79
8.04k
}
80
81
29.5k
void AcStrategy::ComputeNaturalCoeffOrder(coeff_order_t* order) const {
82
29.5k
  CoeffOrderAndLut</*is_lut=*/false>(*this, order);
83
29.5k
}
84
8.04k
void AcStrategy::ComputeNaturalCoeffOrderLut(coeff_order_t* lut) const {
85
8.04k
  CoeffOrderAndLut</*is_lut=*/true>(*this, lut);
86
8.04k
}
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
125k
    JxlMemoryManager* memory_manager, size_t xsize, size_t ysize) {
96
125k
  AcStrategyImage img;
97
125k
  JXL_ASSIGN_OR_RETURN(img.layers_,
98
125k
                       ImageB::Create(memory_manager, xsize, ysize));
99
125k
  img.row_ = img.layers_.Row(0);
100
125k
  img.stride_ = img.layers_.PixelsPerRow();
101
125k
  return img;
102
125k
}
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