libjxl

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

dct_block-inl.h (3214B)


      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 // Adapters for DCT input/output: from/to contiguous blocks or image rows.
      7 
      8 #if defined(LIB_JXL_DCT_BLOCK_INL_H_) == defined(HWY_TARGET_TOGGLE)
      9 #ifdef LIB_JXL_DCT_BLOCK_INL_H_
     10 #undef LIB_JXL_DCT_BLOCK_INL_H_
     11 #else
     12 #define LIB_JXL_DCT_BLOCK_INL_H_
     13 #endif
     14 
     15 #include <stddef.h>
     16 
     17 #include <hwy/highway.h>
     18 
     19 #include "lib/jxl/base/status.h"
     20 HWY_BEFORE_NAMESPACE();
     21 namespace jxl {
     22 namespace HWY_NAMESPACE {
     23 namespace {
     24 
     25 // These templates are not found via ADL.
     26 using hwy::HWY_NAMESPACE::Vec;
     27 
     28 // Block: (x, y) <-> (N * y + x)
     29 // Lines: (x, y) <-> (stride * y + x)
     30 //
     31 // I.e. Block is a specialization of Lines with fixed stride.
     32 //
     33 // FromXXX should implement Read and Load (Read vector).
     34 // ToXXX should implement Write and Store (Write vector).
     35 
     36 template <size_t N>
     37 using BlockDesc = HWY_CAPPED(float, N);
     38 
     39 // Here and in the following, the SZ template parameter specifies the number of
     40 // values to load/store. Needed because we want to handle 4x4 sub-blocks of
     41 // 16x16 blocks.
     42 class DCTFrom {
     43  public:
     44   DCTFrom(const float* data, size_t stride) : stride_(stride), data_(data) {}
     45 
     46   template <typename D>
     47   HWY_INLINE Vec<D> LoadPart(D /* tag */, const size_t row, size_t i) const {
     48     JXL_DASSERT(Lanes(D()) <= stride_);
     49     // Since these functions are used also for DC, no alignment at all is
     50     // guaranteed in the case of floating blocks.
     51     // TODO(veluca): consider using a different class for DC-to-LF and
     52     // DC-from-LF, or copying DC values to/from a temporary aligned location.
     53     return LoadU(D(), Address(row, i));
     54   }
     55 
     56   HWY_INLINE float Read(const size_t row, const size_t i) const {
     57     return *Address(row, i);
     58   }
     59 
     60   constexpr HWY_INLINE const float* Address(const size_t row,
     61                                             const size_t i) const {
     62     return data_ + row * stride_ + i;
     63   }
     64 
     65   size_t Stride() const { return stride_; }
     66 
     67  private:
     68   size_t stride_;
     69   const float* JXL_RESTRICT data_;
     70 };
     71 
     72 class DCTTo {
     73  public:
     74   DCTTo(float* data, size_t stride) : stride_(stride), data_(data) {}
     75 
     76   template <typename D>
     77   HWY_INLINE void StorePart(D /* tag */, const Vec<D>& v, const size_t row,
     78                             size_t i) const {
     79     JXL_DASSERT(Lanes(D()) <= stride_);
     80     // Since these functions are used also for DC, no alignment at all is
     81     // guaranteed in the case of floating blocks.
     82     // TODO(veluca): consider using a different class for DC-to-LF and
     83     // DC-from-LF, or copying DC values to/from a temporary aligned location.
     84     StoreU(v, D(), Address(row, i));
     85   }
     86 
     87   HWY_INLINE void Write(float v, const size_t row, const size_t i) const {
     88     *Address(row, i) = v;
     89   }
     90 
     91   constexpr HWY_INLINE float* Address(const size_t row, const size_t i) const {
     92     return data_ + row * stride_ + i;
     93   }
     94 
     95   size_t Stride() const { return stride_; }
     96 
     97  private:
     98   size_t stride_;
     99   float* JXL_RESTRICT data_;
    100 };
    101 
    102 }  // namespace
    103 // NOLINTNEXTLINE(google-readability-namespace-comments)
    104 }  // namespace HWY_NAMESPACE
    105 }  // namespace jxl
    106 HWY_AFTER_NAMESPACE();
    107 
    108 #endif  // LIB_JXL_DCT_BLOCK_INL_H_