libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

enc_patch_dictionary.h (3404B)


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