/src/libjxl/lib/jxl/ac_strategy.cc
Line | Count | Source (jump to first uncovered line) |
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 | 11.5k | static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) { |
30 | 11.5k | size_t cx = acs.covered_blocks_x(); |
31 | 11.5k | size_t cy = acs.covered_blocks_y(); |
32 | 11.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 | 11.5k | size_t xs = cx / cy; |
38 | 11.5k | size_t xsm = xs - 1; |
39 | 11.5k | size_t xss = CeilLog2Nonzero(xs); |
40 | | // First half of the block |
41 | 11.5k | size_t cur = cx * cy; |
42 | 208k | for (size_t i = 0; i < cx * kBlockDim; i++) { |
43 | 4.53M | for (size_t j = 0; j <= i; j++) { |
44 | 4.33M | size_t x = j; |
45 | 4.33M | size_t y = i - j; |
46 | 4.33M | if (i % 2) std::swap(x, y); |
47 | 4.33M | if ((y & xsm) != 0) continue; |
48 | 3.29M | y >>= xss; |
49 | 3.29M | size_t val = 0; |
50 | 3.29M | if (x < cx && y < cy) { |
51 | 100k | val = y * cx + x; |
52 | 3.19M | } else { |
53 | 3.19M | val = cur++; |
54 | 3.19M | } |
55 | 3.29M | if (is_lut) { |
56 | 78.1k | out[y * cx * kBlockDim + x] = val; |
57 | 3.22M | } else { |
58 | 3.22M | out[val] = y * cx * kBlockDim + x; |
59 | 3.22M | } |
60 | 3.29M | } |
61 | 197k | } |
62 | | // Second half |
63 | 197k | for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) { |
64 | 185k | size_t i = ip - 1; |
65 | 4.32M | for (size_t j = 0; j <= i; j++) { |
66 | 4.13M | size_t x = cx * kBlockDim - 1 - (i - j); |
67 | 4.13M | size_t y = cx * kBlockDim - 1 - j; |
68 | 4.13M | if (i % 2) std::swap(x, y); |
69 | 4.13M | if ((y & xsm) != 0) continue; |
70 | 3.10M | y >>= xss; |
71 | 3.10M | size_t val = cur++; |
72 | 3.10M | if (is_lut) { |
73 | 69.2k | out[y * cx * kBlockDim + x] = val; |
74 | 3.03M | } else { |
75 | 3.03M | out[val] = y * cx * kBlockDim + x; |
76 | 3.03M | } |
77 | 3.10M | } |
78 | 185k | } |
79 | 11.5k | } ac_strategy.cc:void jxl::CoeffOrderAndLut<false>(jxl::AcStrategy, unsigned int*) Line | Count | Source | 29 | 11.0k | static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) { | 30 | 11.0k | size_t cx = acs.covered_blocks_x(); | 31 | 11.0k | size_t cy = acs.covered_blocks_y(); | 32 | 11.0k | 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 | 11.0k | size_t xs = cx / cy; | 38 | 11.0k | size_t xsm = xs - 1; | 39 | 11.0k | size_t xss = CeilLog2Nonzero(xs); | 40 | | // First half of the block | 41 | 11.0k | size_t cur = cx * cy; | 42 | 199k | for (size_t i = 0; i < cx * kBlockDim; i++) { | 43 | 4.42M | for (size_t j = 0; j <= i; j++) { | 44 | 4.23M | size_t x = j; | 45 | 4.23M | size_t y = i - j; | 46 | 4.23M | if (i % 2) std::swap(x, y); | 47 | 4.23M | if ((y & xsm) != 0) continue; | 48 | 3.22M | y >>= xss; | 49 | 3.22M | size_t val = 0; | 50 | 3.22M | if (x < cx && y < cy) { | 51 | 97.7k | val = y * cx + x; | 52 | 3.12M | } else { | 53 | 3.12M | val = cur++; | 54 | 3.12M | } | 55 | 3.22M | if (is_lut) { | 56 | 0 | out[y * cx * kBlockDim + x] = val; | 57 | 3.22M | } else { | 58 | 3.22M | out[val] = y * cx * kBlockDim + x; | 59 | 3.22M | } | 60 | 3.22M | } | 61 | 188k | } | 62 | | // Second half | 63 | 188k | for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) { | 64 | 177k | size_t i = ip - 1; | 65 | 4.22M | for (size_t j = 0; j <= i; j++) { | 66 | 4.04M | size_t x = cx * kBlockDim - 1 - (i - j); | 67 | 4.04M | size_t y = cx * kBlockDim - 1 - j; | 68 | 4.04M | if (i % 2) std::swap(x, y); | 69 | 4.04M | if ((y & xsm) != 0) continue; | 70 | 3.03M | y >>= xss; | 71 | 3.03M | size_t val = cur++; | 72 | 3.03M | if (is_lut) { | 73 | 0 | out[y * cx * kBlockDim + x] = val; | 74 | 3.03M | } else { | 75 | 3.03M | out[val] = y * cx * kBlockDim + x; | 76 | 3.03M | } | 77 | 3.03M | } | 78 | 177k | } | 79 | 11.0k | } |
ac_strategy.cc:void jxl::CoeffOrderAndLut<true>(jxl::AcStrategy, unsigned int*) Line | Count | Source | 29 | 538 | static void CoeffOrderAndLut(AcStrategy acs, coeff_order_t* out) { | 30 | 538 | size_t cx = acs.covered_blocks_x(); | 31 | 538 | size_t cy = acs.covered_blocks_y(); | 32 | 538 | 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 | 538 | size_t xs = cx / cy; | 38 | 538 | size_t xsm = xs - 1; | 39 | 538 | size_t xss = CeilLog2Nonzero(xs); | 40 | | // First half of the block | 41 | 538 | size_t cur = cx * cy; | 42 | 9.36k | for (size_t i = 0; i < cx * kBlockDim; i++) { | 43 | 110k | for (size_t j = 0; j <= i; j++) { | 44 | 101k | size_t x = j; | 45 | 101k | size_t y = i - j; | 46 | 101k | if (i % 2) std::swap(x, y); | 47 | 101k | if ((y & xsm) != 0) continue; | 48 | 78.1k | y >>= xss; | 49 | 78.1k | size_t val = 0; | 50 | 78.1k | if (x < cx && y < cy) { | 51 | 2.30k | val = y * cx + x; | 52 | 75.8k | } else { | 53 | 75.8k | val = cur++; | 54 | 75.8k | } | 55 | 78.1k | if (is_lut) { | 56 | 78.1k | out[y * cx * kBlockDim + x] = val; | 57 | 78.1k | } else { | 58 | 0 | out[val] = y * cx * kBlockDim + x; | 59 | 0 | } | 60 | 78.1k | } | 61 | 8.82k | } | 62 | | // Second half | 63 | 8.82k | for (size_t ip = cx * kBlockDim - 1; ip > 0; ip--) { | 64 | 8.28k | size_t i = ip - 1; | 65 | 100k | for (size_t j = 0; j <= i; j++) { | 66 | 92.5k | size_t x = cx * kBlockDim - 1 - (i - j); | 67 | 92.5k | size_t y = cx * kBlockDim - 1 - j; | 68 | 92.5k | if (i % 2) std::swap(x, y); | 69 | 92.5k | if ((y & xsm) != 0) continue; | 70 | 69.2k | y >>= xss; | 71 | 69.2k | size_t val = cur++; | 72 | 69.2k | if (is_lut) { | 73 | 69.2k | out[y * cx * kBlockDim + x] = val; | 74 | 69.2k | } else { | 75 | 0 | out[val] = y * cx * kBlockDim + x; | 76 | 0 | } | 77 | 69.2k | } | 78 | 8.28k | } | 79 | 538 | } |
|
80 | | |
81 | 11.0k | void AcStrategy::ComputeNaturalCoeffOrder(coeff_order_t* order) const { |
82 | 11.0k | CoeffOrderAndLut</*is_lut=*/false>(*this, order); |
83 | 11.0k | } |
84 | 538 | void AcStrategy::ComputeNaturalCoeffOrderLut(coeff_order_t* lut) const { |
85 | 538 | CoeffOrderAndLut</*is_lut=*/true>(*this, lut); |
86 | 538 | } |
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 | 24.7k | JxlMemoryManager* memory_manager, size_t xsize, size_t ysize) { |
96 | 24.7k | AcStrategyImage img; |
97 | 24.7k | JXL_ASSIGN_OR_RETURN(img.layers_, |
98 | 24.7k | ImageB::Create(memory_manager, xsize, ysize)); |
99 | 24.7k | img.row_ = img.layers_.Row(0); |
100 | 24.7k | img.stride_ = img.layers_.PixelsPerRow(); |
101 | 24.7k | return img; |
102 | 24.7k | } |
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 |