/src/libjxl/lib/jxl/enc_patch_dictionary.h
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 | | #ifndef LIB_JXL_ENC_PATCH_DICTIONARY_H_ |
7 | | #define LIB_JXL_ENC_PATCH_DICTIONARY_H_ |
8 | | |
9 | | // Chooses reference patches, and avoids encoding them once per occurrence. |
10 | | |
11 | | #include <jxl/cms_interface.h> |
12 | | |
13 | | #include <cstddef> |
14 | | #include <cstdint> |
15 | | #include <cstring> |
16 | | #include <utility> |
17 | | #include <vector> |
18 | | |
19 | | #include "lib/jxl/base/compiler_specific.h" |
20 | | #include "lib/jxl/base/data_parallel.h" |
21 | | #include "lib/jxl/base/status.h" |
22 | | #include "lib/jxl/dec_patch_dictionary.h" |
23 | | #include "lib/jxl/enc_bit_writer.h" |
24 | | #include "lib/jxl/enc_cache.h" |
25 | | #include "lib/jxl/enc_params.h" |
26 | | #include "lib/jxl/image.h" |
27 | | |
28 | | namespace jxl { |
29 | | |
30 | | struct AuxOut; |
31 | | enum class LayerType : uint8_t; |
32 | | |
33 | | constexpr size_t kMaxPatchSize = 32; |
34 | | |
35 | | struct QuantizedPatch { |
36 | | size_t xsize; |
37 | | size_t ysize; |
38 | 756k | QuantizedPatch() { |
39 | 3.02M | for (size_t i = 0; i < 3; i++) { |
40 | 2.26M | pixels[i].resize(kMaxPatchSize * kMaxPatchSize); |
41 | 2.26M | fpixels[i].resize(kMaxPatchSize * kMaxPatchSize); |
42 | 2.26M | } |
43 | 756k | } |
44 | | std::vector<int8_t> pixels[3] = {}; |
45 | | // Not compared. Used only to retrieve original pixels to construct the |
46 | | // reference image. |
47 | | std::vector<float> fpixels[3] = {}; |
48 | 754k | bool operator==(const QuantizedPatch& other) const { |
49 | 754k | if (xsize != other.xsize) return false; |
50 | 753k | if (ysize != other.ysize) return false; |
51 | 2.96M | for (size_t c = 0; c < 3; c++) { |
52 | 2.23M | if (memcmp(pixels[c].data(), other.pixels[c].data(), |
53 | 2.23M | sizeof(int8_t) * xsize * ysize) != 0) |
54 | 18.2k | return false; |
55 | 2.23M | } |
56 | 732k | return true; |
57 | 750k | } |
58 | | |
59 | 21.2M | bool operator<(const QuantizedPatch& other) const { |
60 | 21.2M | if (xsize != other.xsize) return xsize < other.xsize; |
61 | 19.3M | if (ysize != other.ysize) return ysize < other.ysize; |
62 | 65.9M | for (size_t c = 0; c < 3; c++) { |
63 | 50.5M | int cmp = memcmp(pixels[c].data(), other.pixels[c].data(), |
64 | 50.5M | sizeof(int8_t) * xsize * ysize); |
65 | 50.5M | if (cmp > 0) return false; |
66 | 49.7M | if (cmp < 0) return true; |
67 | 49.7M | } |
68 | 15.4M | return false; |
69 | 17.8M | } |
70 | | }; |
71 | | |
72 | | // Pair (patch, vector of occurrences). |
73 | | using PatchInfo = |
74 | | std::pair<QuantizedPatch, std::vector<std::pair<uint32_t, uint32_t>>>; |
75 | | |
76 | | // Friend class of PatchDictionary. |
77 | | class PatchDictionaryEncoder { |
78 | | public: |
79 | | // Only call if HasAny(). |
80 | | static Status Encode(const PatchDictionary& pdic, BitWriter* writer, |
81 | | LayerType layer, AuxOut* aux_out); |
82 | | |
83 | | static void SetPositions(PatchDictionary* pdic, |
84 | | std::vector<PatchPosition> positions, |
85 | | std::vector<PatchReferencePosition> ref_positions, |
86 | | std::vector<PatchBlending> blendings, |
87 | 97 | size_t blendings_stride) { |
88 | 97 | pdic->positions_ = std::move(positions); |
89 | 97 | pdic->ref_positions_ = std::move(ref_positions); |
90 | 97 | pdic->blendings_ = std::move(blendings); |
91 | 97 | pdic->blendings_stride_ = blendings_stride; |
92 | 97 | pdic->ComputePatchTree(); |
93 | 97 | } |
94 | | |
95 | | static Status SubtractFrom(const PatchDictionary& pdic, Image3F* opsin); |
96 | | }; |
97 | | |
98 | | Status FindBestPatchDictionary(const Image3F& opsin, |
99 | | PassesEncoderState* JXL_RESTRICT state, |
100 | | const JxlCmsInterface& cms, ThreadPool* pool, |
101 | | AuxOut* aux_out, bool is_xyb = true); |
102 | | |
103 | | Status RoundtripPatchFrame(Image3F* reference_frame, |
104 | | PassesEncoderState* JXL_RESTRICT state, int idx, |
105 | | CompressParams& cparams, const JxlCmsInterface& cms, |
106 | | ThreadPool* pool, AuxOut* aux_out, bool subtract); |
107 | | |
108 | | } // namespace jxl |
109 | | |
110 | | #endif // LIB_JXL_ENC_PATCH_DICTIONARY_H_ |