Coverage Report

Created: 2026-07-20 07:19

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
26.6k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
26.6k
  size_t cx = acs.covered_blocks_x();
31
26.6k
  size_t cy = acs.covered_blocks_y();
32
26.6k
  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
26.6k
  size_t xs = cx / cy;
38
26.6k
  size_t xsm = xs - 1;
39
26.6k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
26.6k
  size_t cur = cx * cy;
42
519k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
10.5M
    for (size_t j = 0; j <= i; j++) {
44
10.0M
      size_t x = j;
45
10.0M
      size_t y = i - j;
46
10.0M
      if (i % 2) std::swap(x, y);
47
10.0M
      if ((y & xsm) != 0) continue;
48
7.49M
      y >>= xss;
49
7.49M
      size_t val = 0;
50
7.49M
      if (x < cx && y < cy) {
51
226k
        val = y * cx + x;
52
7.26M
      } else {
53
7.26M
        val = cur++;
54
7.26M
      }
55
7.49M
      if (is_lut) {
56
380k
        out[y * cx * kBlockDim + x] = val;
57
7.11M
      } else {
58
7.11M
        out[val] = y * cx * kBlockDim + x;
59
7.11M
      }
60
7.49M
    }
61
492k
  }
62
  // Second half
63
492k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
466k
    size_t i = ip - 1;
65
9.98M
    for (size_t j = 0; j <= i; j++) {
66
9.51M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
9.51M
      size_t y = cx * kBlockDim - 1 - j;
68
9.51M
      if (i % 2) std::swap(x, y);
69
9.51M
      if ((y & xsm) != 0) continue;
70
6.99M
      y >>= xss;
71
6.99M
      size_t val = cur++;
72
6.99M
      if (is_lut) {
73
327k
        out[y * cx * kBlockDim + x] = val;
74
6.67M
      } else {
75
6.67M
        out[val] = y * cx * kBlockDim + x;
76
6.67M
      }
77
6.99M
    }
78
466k
  }
79
26.6k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<false>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
22.5k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
22.5k
  size_t cx = acs.covered_blocks_x();
31
22.5k
  size_t cy = acs.covered_blocks_y();
32
22.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
22.5k
  size_t xs = cx / cy;
38
22.5k
  size_t xsm = xs - 1;
39
22.5k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
22.5k
  size_t cur = cx * cy;
42
462k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
9.96M
    for (size_t j = 0; j <= i; j++) {
44
9.52M
      size_t x = j;
45
9.52M
      size_t y = i - j;
46
9.52M
      if (i % 2) std::swap(x, y);
47
9.52M
      if ((y & xsm) != 0) continue;
48
7.11M
      y >>= xss;
49
7.11M
      size_t val = 0;
50
7.11M
      if (x < cx && y < cy) {
51
215k
        val = y * cx + x;
52
6.89M
      } else {
53
6.89M
        val = cur++;
54
6.89M
      }
55
7.11M
      if (is_lut) {
56
0
        out[y * cx * kBlockDim + x] = val;
57
7.11M
      } else {
58
7.11M
        out[val] = y * cx * kBlockDim + x;
59
7.11M
      }
60
7.11M
    }
61
440k
  }
62
  // Second half
63
440k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
417k
    size_t i = ip - 1;
65
9.49M
    for (size_t j = 0; j <= i; j++) {
66
9.08M
      size_t x = cx * kBlockDim - 1 - (i - j);
67
9.08M
      size_t y = cx * kBlockDim - 1 - j;
68
9.08M
      if (i % 2) std::swap(x, y);
69
9.08M
      if ((y & xsm) != 0) continue;
70
6.67M
      y >>= xss;
71
6.67M
      size_t val = cur++;
72
6.67M
      if (is_lut) {
73
0
        out[y * cx * kBlockDim + x] = val;
74
6.67M
      } else {
75
6.67M
        out[val] = y * cx * kBlockDim + x;
76
6.67M
      }
77
6.67M
    }
78
417k
  }
79
22.5k
}
ac_strategy.cc:void jxl::CoeffOrderAndLut<true>(jxl::AcStrategy, unsigned int*)
Line
Count
Source
29
4.08k
static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) {
30
4.08k
  size_t cx = acs.covered_blocks_x();
31
4.08k
  size_t cy = acs.covered_blocks_y();
32
4.08k
  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
4.08k
  size_t xs = cx / cy;
38
4.08k
  size_t xsm = xs - 1;
39
4.08k
  size_t xss = CeilLog2Nonzero(xs);
40
  // First half of the block
41
4.08k
  size_t cur = cx * cy;
42
56.7k
  for (size_t i = 0; i < cx * kBlockDim; i++) {
43
542k
    for (size_t j = 0; j <= i; j++) {
44
489k
      size_t x = j;
45
489k
      size_t y = i - j;
46
489k
      if (i % 2) std::swap(x, y);
47
489k
      if ((y & xsm) != 0) continue;
48
380k
      y >>= xss;
49
380k
      size_t val = 0;
50
380k
      if (x < cx && y < cy) {
51
11.0k
        val = y * cx + x;
52
369k
      } else {
53
369k
        val = cur++;
54
369k
      }
55
380k
      if (is_lut) {
56
380k
        out[y * cx * kBlockDim + x] = val;
57
380k
      } else {
58
0
        out[val] = y * cx * kBlockDim + x;
59
0
      }
60
380k
    }
61
52.6k
  }
62
  // Second half
63
52.6k
  for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) {
64
48.5k
    size_t i = ip - 1;
65
485k
    for (size_t j = 0; j <= i; j++) {
66
436k
      size_t x = cx * kBlockDim - 1 - (i - j);
67
436k
      size_t y = cx * kBlockDim - 1 - j;
68
436k
      if (i % 2) std::swap(x, y);
69
436k
      if ((y & xsm) != 0) continue;
70
327k
      y >>= xss;
71
327k
      size_t val = cur++;
72
327k
      if (is_lut) {
73
327k
        out[y * cx * kBlockDim + x] = val;
74
327k
      } else {
75
0
        out[val] = y * cx * kBlockDim + x;
76
0
      }
77
327k
    }
78
48.5k
  }
79
4.08k
}
80
81
22.5k
void AcStrategy::ComputeNaturalCoeffOrder(coeff_order_t* order) const {
82
22.5k
  CoeffOrderAndLut</*is_lut=*/false>(*this, order);
83
22.5k
}
84
4.08k
void AcStrategy::ComputeNaturalCoeffOrderLut(coeff_order_t* lut) const {
85
4.08k
  CoeffOrderAndLut</*is_lut=*/true>(*this, lut);
86
4.08k
}
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
41.4k
    JxlMemoryManager* memory_manager, size_t xsize, size_t ysize) {
96
41.4k
  AcStrategyImage img;
97
41.4k
  JXL_ASSIGN_OR_RETURN(img.layers_,
98
41.4k
                       ImageB::Create(memory_manager, xsize, ysize));
99
41.4k
  img.row_ = img.layers_.Row(0);
100
41.4k
  img.stride_ = img.layers_.PixelsPerRow();
101
41.4k
  return img;
102
41.4k
}
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