tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

enc_patch_dictionary.h (3656B)


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