Coverage Report

Created: 2026-07-25 07:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/vvenc/source/Lib/CommonLib/Buffer.h
Line
Count
Source
1
/* -----------------------------------------------------------------------------
2
The copyright in this software is being made available under the Clear BSD
3
License, included below. No patent rights, trademark rights and/or 
4
other Intellectual Property Rights other than the copyrights concerning 
5
the Software are granted under this license.
6
7
The Clear BSD License
8
9
Copyright (c) 2019-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVenC Authors.
10
All rights reserved.
11
12
Redistribution and use in source and binary forms, with or without modification,
13
are permitted (subject to the limitations in the disclaimer below) provided that
14
the following conditions are met:
15
16
     * Redistributions of source code must retain the above copyright notice,
17
     this list of conditions and the following disclaimer.
18
19
     * Redistributions in binary form must reproduce the above copyright
20
     notice, this list of conditions and the following disclaimer in the
21
     documentation and/or other materials provided with the distribution.
22
23
     * Neither the name of the copyright holder nor the names of its
24
     contributors may be used to endorse or promote products derived from this
25
     software without specific prior written permission.
26
27
NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
28
THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
29
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
31
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
36
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
POSSIBILITY OF SUCH DAMAGE.
39
40
41
------------------------------------------------------------------------------------------- */
42
/** \file     Buffer.h
43
 *  \brief    Low-overhead class describing 2D memory layout
44
 */
45
46
#pragma once
47
48
#ifndef __IN_UNIT_H__
49
#error "Include Unit.h not Buffer.h"
50
#endif
51
52
#include "Common.h"
53
#include "CommonDef.h"
54
#include "MotionInfo.h"
55
56
#include <string.h>
57
#include <type_traits>
58
#include <typeinfo>
59
60
#include "vvenc/vvenc.h"
61
62
//! \ingroup CommonLib
63
//! \{
64
65
struct vvencYUVBuffer;
66
67
namespace vvenc {
68
69
#if ENABLE_SIMD_OPT_BUFFER && defined(TARGET_SIMD_X86)
70
using namespace x86_simd;
71
#endif
72
73
#if ENABLE_SIMD_OPT_BUFFER && defined(TARGET_SIMD_ARM)
74
using namespace arm_simd;
75
#endif
76
77
// ---------------------------------------------------------------------------
78
// AreaBuf struct
79
// ---------------------------------------------------------------------------
80
81
struct PelBufferOps
82
{
83
  PelBufferOps();
84
  void initPelBufOps( bool enableOpt = true );
85
86
0
#define INCX( ptr, stride ) { ptr++; }
87
0
#define INCY( ptr, stride ) { ptr += ( stride ); }
88
0
#define OFFSETX( ptr, stride, x ) { ptr += ( x ); }
89
0
#define OFFSETY( ptr, stride, y ) { ptr += ( y ) * ( stride ); }
90
0
#define OFFSET( ptr, stride, x, y ) { ptr += ( x ) + ( y ) * ( stride ); }
91
#define GET_OFFSETX( ptr, stride, x ) ( ( ptr ) + ( x ) )
92
#define GET_OFFSETY( ptr, stride, y ) ( ( ptr ) + ( y ) * ( stride ) )
93
0
#define GET_OFFSET( ptr, stride, x, y ) ( ( ptr ) + ( x ) + ( y ) * ( stride ) ) // need in loopFilter.cpp + some ARM files
94
95
  void ( *roundGeo )      ( const Pel* src, Pel* dest, const int numSamples, unsigned rshift, int offset, const ClpRng &clpRng);
96
  void ( *addAvg )        ( const Pel* src0, const Pel* src1, Pel* dst, int numsamples, unsigned shift, int offset, const ClpRng& clpRng );
97
  void ( *reco  )         ( const Pel* src0, const Pel* src1, Pel* dst, int numSamples, const ClpRng& clpRng );
98
  void ( *copyClip )      ( const Pel* src0,                  Pel* dst, int numSamples, const ClpRng& clpRng );
99
  void ( *addAvg4 )       ( const Pel* src0, int src0Stride, const Pel* src1, int src1Stride, Pel* dst, int dstStride, int width, int height,       unsigned shift, int offset, const ClpRng& clpRng );
100
  void ( *addAvg8 )       ( const Pel* src0, int src0Stride, const Pel* src1, int src1Stride, Pel* dst, int dstStride, int width, int height,       unsigned shift, int offset, const ClpRng& clpRng );
101
  void ( *addAvg16 )      ( const Pel* src0, int src0Stride, const Pel* src1, int src1Stride, Pel* dst, int dstStride, int width, int height,       unsigned shift, int offset, const ClpRng& clpRng );
102
  void ( *sub4 )          ( const Pel* src0, int src0Stride, const Pel* src1, int src1Stride, Pel* dst, int dstStride, int width, int height );
103
  void ( *sub8 )          ( const Pel* src0, int src0Stride, const Pel* src1, int src1Stride, Pel* dst, int dstStride, int width, int height );
104
  void ( *wghtAvg4 )      ( const Pel* src0, int src0Stride, const Pel* src1, int src1Stride, Pel *dst, int dstStride, int width, int height,       unsigned shift, int offset, int w0, int w1, const ClpRng& clpRng );
105
  void ( *wghtAvg8 )      ( const Pel* src0, int src0Stride, const Pel* src1, int src1Stride, Pel *dst, int dstStride, int width, int height,       unsigned shift, int offset, int w0, int w1, const ClpRng& clpRng );
106
  void ( *copyClip4 )     ( const Pel* src0, int src0Stride,                                  Pel* dst, int dstStride, int width, int height,                                   const ClpRng& clpRng );
107
  void ( *copyClip8 )     ( const Pel* src0, int src0Stride,                                  Pel* dst, int dstStride, int width, int height,                                   const ClpRng& clpRng );
108
  void ( *reco4 )         ( const Pel* src0, int src0Stride, const Pel* src1, int src1Stride, Pel* dst, int dstStride, int width, int height,                                   const ClpRng& clpRng );
109
  void ( *reco8 )         ( const Pel* src0, int src0Stride, const Pel* src1, int src1Stride, Pel* dst, int dstStride, int width, int height,                                   const ClpRng& clpRng );
110
  void ( *linTf4 )        ( const Pel* src0, int src0Stride,                                  Pel* dst, int dstStride, int width, int height, int scale, unsigned shift, int offset, const ClpRng& clpRng, bool bClip );
111
  void ( *linTf8 )        ( const Pel* src0, int src0Stride,                                  Pel* dst, int dstStride, int width, int height, int scale, unsigned shift, int offset, const ClpRng& clpRng, bool bClip );
112
  void ( *copyBuffer )    ( const char* src, int srcStride, char* dst, int dstStride, int width, int height );
113
  void ( *removeHighFreq8)( Pel* src0, int src0Stride, const Pel* src1, int src1Stride, int width, int height);
114
  void ( *removeHighFreq4)( Pel* src0, int src0Stride, const Pel* src1, int src1Stride, int width, int height);
115
  void ( *transpose4x4 )  ( const Pel* src,  int srcStride, Pel* dst, int dstStride );
116
  void ( *transpose8x8 )  ( const Pel* src,  int srcStride, Pel* dst, int dstStride );
117
  void ( *roundIntVector) ( int* v, int size, unsigned int nShift, const int dmvLimit);
118
  void ( *mipMatrixMul_4_4)( Pel* res, const Pel* input, const uint8_t* weight, const int maxVal, const int offset, bool transpose );
119
  void ( *mipMatrixMul_8_4)( Pel* res, const Pel* input, const uint8_t* weight, const int maxVal, const int offset, bool transpose );
120
  void ( *mipMatrixMul_8_8)( Pel* res, const Pel* input, const uint8_t* weight, const int maxVal, const int offset, bool transpose );
121
  void ( *weightCiip)     ( Pel* res, const Pel* intra, const int numSamples, int numIntra );
122
  void ( *applyLut )      ( const Pel* src, const ptrdiff_t srcStride, Pel* dst, ptrdiff_t dstStride, int width, int height, const Pel* lut );
123
  void ( *fillPtrMap )    ( void** ptrMap, const ptrdiff_t mapStride, int width, int height, void* val );
124
  uint64_t ( *AvgHighPassWithDownsampling )    ( const int width, const int height, const Pel* pSrc, const int iSrcStride);
125
  uint64_t ( *AvgHighPass )    ( const int width, const int height, const Pel* pSrc, const int iSrcStride);
126
  uint64_t ( *AvgHighPassWithDownsamplingDiff1st ) (const int width, const int height, const Pel* pSrc,const Pel* pSrcM1, const int iSrcStride, const int iSrcM1Stride);
127
  uint64_t ( *AvgHighPassWithDownsamplingDiff2nd) (const int width,const int height,const Pel* pSrc,const Pel* pSM1,const Pel* pS21,const int iSrcStride,const int iSM1Stride,const int iSM2Stride);
128
  uint64_t ( *HDHighPass) (const int width, const int height,const Pel*  pSrc,const Pel* pSM1,const int iSrcStride,const int iSM1Stride);
129
  uint64_t ( *HDHighPass2)  (const int width, const int height,const Pel*  pSrc,const Pel* pSM1,const Pel* pSM2,const int iSrcStride,const int iSM1Stride,const int iSM2Stride);
130
131
private:
132
  bool isInitSIMDDone = false;
133
134
#if ENABLE_SIMD_OPT_BUFFER && defined(TARGET_SIMD_X86)
135
  void initPelBufOpsX86();
136
  template<X86_VEXT vext>
137
  void _initPelBufOpsX86();
138
#endif
139
140
#if ENABLE_SIMD_OPT_BUFFER && defined( TARGET_SIMD_ARM )
141
  void initPelBufOpsARM();
142
  template<ARM_VEXT vext>
143
  void _initPelBufOpsARM();
144
#endif
145
};
146
147
extern PelBufferOps g_pelBufOP;
148
149
template<typename T>
150
struct AreaBuf : public Size
151
{
152
  T*        buf;
153
  int       stride;
154
  // the proper type causes awful lot of errors
155
156
0
  AreaBuf()                                                                               : Size(),                  buf( NULL ), stride( 0 )          { }
Unexecuted instantiation: vvenc::AreaBuf<short>::AreaBuf()
Unexecuted instantiation: vvenc::AreaBuf<short const>::AreaBuf()
Unexecuted instantiation: vvenc::AreaBuf<int>::AreaBuf()
Unexecuted instantiation: vvenc::AreaBuf<vvenc::MotionInfo>::AreaBuf()
157
0
  AreaBuf( T *_buf, const Size& size )                                                    : Size( size ),            buf( _buf ), stride( size.width ) { }
Unexecuted instantiation: vvenc::AreaBuf<vvenc::LoopFilterParam>::AreaBuf(vvenc::LoopFilterParam*, vvenc::Size const&)
Unexecuted instantiation: vvenc::AreaBuf<vvenc::LoopFilterParam const>::AreaBuf(vvenc::LoopFilterParam const*, vvenc::Size const&)
Unexecuted instantiation: vvenc::AreaBuf<short const>::AreaBuf(short const*, vvenc::Size const&)
Unexecuted instantiation: vvenc::AreaBuf<short>::AreaBuf(short*, vvenc::Size const&)
Unexecuted instantiation: vvenc::AreaBuf<int>::AreaBuf(int*, vvenc::Size const&)
Unexecuted instantiation: vvenc::AreaBuf<vvenc::MotionInfo const>::AreaBuf(vvenc::MotionInfo const*, vvenc::Size const&)
Unexecuted instantiation: vvenc::AreaBuf<vvenc::MotionInfo>::AreaBuf(vvenc::MotionInfo*, vvenc::Size const&)
158
0
  AreaBuf( T *_buf, const int& _stride, const Size& size )                                : Size( size ),            buf( _buf ), stride( _stride )    { }
Unexecuted instantiation: vvenc::AreaBuf<short>::AreaBuf(short*, int const&, vvenc::Size const&)
Unexecuted instantiation: vvenc::AreaBuf<short const>::AreaBuf(short const*, int const&, vvenc::Size const&)
Unexecuted instantiation: vvenc::AreaBuf<vvenc::MotionInfo>::AreaBuf(vvenc::MotionInfo*, int const&, vvenc::Size const&)
Unexecuted instantiation: vvenc::AreaBuf<vvenc::LoopFilterParam>::AreaBuf(vvenc::LoopFilterParam*, int const&, vvenc::Size const&)
159
0
  AreaBuf( T *_buf, const SizeType& _width, const SizeType& _height )                     : Size( _width, _height ), buf( _buf ), stride( _width )     { }
Unexecuted instantiation: vvenc::AreaBuf<short>::AreaBuf(short*, unsigned int const&, unsigned int const&)
Unexecuted instantiation: vvenc::AreaBuf<short const>::AreaBuf(short const*, unsigned int const&, unsigned int const&)
160
0
  AreaBuf( T *_buf, const int& _stride, const SizeType& _width, const SizeType& _height ) : Size( _width, _height ), buf( _buf ), stride( _stride )    { }
Unexecuted instantiation: vvenc::AreaBuf<short>::AreaBuf(short*, int const&, unsigned int const&, unsigned int const&)
Unexecuted instantiation: vvenc::AreaBuf<short const>::AreaBuf(short const*, int const&, unsigned int const&, unsigned int const&)
161
162
  AreaBuf( const AreaBuf& )  = default;
163
  AreaBuf(       AreaBuf&& ) = default;
164
  AreaBuf& operator=( const AreaBuf& )  = default;
165
  AreaBuf& operator=(       AreaBuf&& ) = default;
166
167
  // conversion from AreaBuf<const T> to AreaBuf<T>
168
  template<bool T_IS_CONST = std::is_const<T>::value>
169
0
  AreaBuf( const AreaBuf<typename std::remove_const_t<T>>& other, std::enable_if_t<T_IS_CONST>* = 0) : Size( other ), buf( other.buf ), stride( other.stride ) { }
Unexecuted instantiation: vvenc::AreaBuf<short const>::AreaBuf<true>(vvenc::AreaBuf<short> const&, std::__1::enable_if<true, void>::type*)
Unexecuted instantiation: vvenc::AreaBuf<vvenc::MotionInfo const>::AreaBuf<true>(vvenc::AreaBuf<vvenc::MotionInfo> const&, std::__1::enable_if<true, void>::type*)
Unexecuted instantiation: vvenc::AreaBuf<int const>::AreaBuf<true>(vvenc::AreaBuf<int> const&, std::__1::enable_if<true, void>::type*)
170
171
  void fill                 ( const T &val );
172
  void memset               ( const int val );
173
174
  void copyFrom             ( const AreaBuf<const T>& other );
175
  void reconstruct          ( const AreaBuf<const T>& pred, const AreaBuf<const T>& resi, const ClpRng& clpRng);
176
  void copyClip             ( const AreaBuf<const T>& src, const ClpRng& clpRng);
177
178
  void subtract             ( const AreaBuf<const T>& minuend, const AreaBuf<const T>& subtrahend );
179
  void calcVarianceSplit    ( const AreaBuf<const T>& Org, const uint32_t  size,int& varh,int& varv ) const;
180
  void extendBorderPel      ( unsigned marginX, unsigned marginY );
181
182
  void addAvg               ( const AreaBuf<const T>& other1, const AreaBuf<const T>& other2, const ClpRng& clpRng );
183
  T    getAvg               () const;
184
  void padBorderPel         ( unsigned marginX, unsigned marginY, int dir );
185
  void addWeightedAvg       ( const AreaBuf<const T>& other1, const AreaBuf<const T>& other2, const ClpRng& clpRng, const int8_t BcwIdx );
186
  void removeHighFreq       ( const AreaBuf<const T>& other, const bool bClip, const ClpRng& clpRng);
187
  void extendBorderPelTop   ( int x, int size, int margin );
188
  void extendBorderPelBot   ( int x, int size, int margin );
189
  void extendBorderPelLft   ( int y, int size, int margin );
190
  void extendBorderPelRgt   ( int y, int size, int margin );
191
192
  void linearTransform      ( const int scale, const unsigned shift, const int offset, bool bClip, const ClpRng& clpRng );
193
194
  void transposedFrom       ( const AreaBuf<const T>& other );
195
  void weightCiip           ( const AreaBuf<const T>& intra, const int numIntra );
196
197
  void rspSignal            ( const Pel* pLUT );
198
  void rspSignal            ( const AreaBuf<const T>& other, const Pel* pLUT );
199
  void scaleSignal          ( const int scale, const bool dir , const ClpRng& clpRng);
200
  bool compare              ( const AreaBuf<const T>& other ) const;
201
202
0
        T& at( const int& x, const int& y )          { return buf[y * stride + x]; }
Unexecuted instantiation: vvenc::AreaBuf<short>::at(int const&, int const&)
Unexecuted instantiation: vvenc::AreaBuf<vvenc::MotionInfo>::at(int const&, int const&)
Unexecuted instantiation: vvenc::AreaBuf<int>::at(int const&, int const&)
203
0
  const T& at( const int& x, const int& y ) const    { return buf[y * stride + x]; }
Unexecuted instantiation: vvenc::AreaBuf<vvenc::MotionInfo const>::at(int const&, int const&) const
Unexecuted instantiation: vvenc::AreaBuf<short const>::at(int const&, int const&) const
Unexecuted instantiation: vvenc::AreaBuf<short>::at(int const&, int const&) const
Unexecuted instantiation: vvenc::AreaBuf<int const>::at(int const&, int const&) const
204
205
0
        T& at( const Position& pos )                 { return buf[pos.y * stride + pos.x]; }
Unexecuted instantiation: vvenc::AreaBuf<vvenc::MotionInfo>::at(vvenc::Position const&)
Unexecuted instantiation: vvenc::AreaBuf<short const>::at(vvenc::Position const&)
Unexecuted instantiation: vvenc::AreaBuf<short>::at(vvenc::Position const&)
Unexecuted instantiation: vvenc::AreaBuf<vvenc::LoopFilterParam>::at(vvenc::Position const&)
206
0
  const T& at( const Position& pos ) const           { return buf[pos.y * stride + pos.x]; }
Unexecuted instantiation: vvenc::AreaBuf<short const>::at(vvenc::Position const&) const
Unexecuted instantiation: vvenc::AreaBuf<short>::at(vvenc::Position const&) const
207
208
209
0
        T* bufAt( const int& x, const int& y )       { return &at( x, y ); }
210
0
  const T* bufAt( const int& x, const int& y ) const { return &at( x, y ); }
Unexecuted instantiation: vvenc::AreaBuf<short const>::bufAt(int const&, int const&) const
Unexecuted instantiation: vvenc::AreaBuf<short>::bufAt(int const&, int const&) const
211
212
0
        T* bufAt( const Position& pos )              { return &at( pos ); }
Unexecuted instantiation: vvenc::AreaBuf<vvenc::MotionInfo>::bufAt(vvenc::Position const&)
Unexecuted instantiation: vvenc::AreaBuf<short const>::bufAt(vvenc::Position const&)
Unexecuted instantiation: vvenc::AreaBuf<short>::bufAt(vvenc::Position const&)
Unexecuted instantiation: vvenc::AreaBuf<vvenc::LoopFilterParam>::bufAt(vvenc::Position const&)
213
0
  const T* bufAt( const Position& pos ) const        { return &at( pos ); }
Unexecuted instantiation: vvenc::AreaBuf<short const>::bufAt(vvenc::Position const&) const
Unexecuted instantiation: vvenc::AreaBuf<short>::bufAt(vvenc::Position const&) const
214
215
  AreaBuf<      T> subBuf( const Area& area )                                                         { return AreaBuf<      T>( bufAt( area ), stride, area   ); }
216
  AreaBuf<const T> subBuf( const Area& area )                                                   const { return AreaBuf<const T>( bufAt( area ), stride, area   ); }
217
0
  AreaBuf<      T> subBuf( const Position& pos, const Size& size )                                    { return AreaBuf<      T>( bufAt( pos  ), stride, size   ); }
Unexecuted instantiation: vvenc::AreaBuf<vvenc::MotionInfo>::subBuf(vvenc::Position const&, vvenc::Size const&)
Unexecuted instantiation: vvenc::AreaBuf<short>::subBuf(vvenc::Position const&, vvenc::Size const&)
Unexecuted instantiation: vvenc::AreaBuf<vvenc::LoopFilterParam>::subBuf(vvenc::Position const&, vvenc::Size const&)
218
0
  AreaBuf<const T> subBuf( const Position& pos, const Size& size )                              const { return AreaBuf<const T>( bufAt( pos  ), stride, size   ); }
Unexecuted instantiation: vvenc::AreaBuf<short const>::subBuf(vvenc::Position const&, vvenc::Size const&) const
Unexecuted instantiation: vvenc::AreaBuf<short>::subBuf(vvenc::Position const&, vvenc::Size const&) const
219
0
  AreaBuf<      T> subBuf( const int& x, const int& y, const unsigned& _w, const unsigned& _h )       { return AreaBuf<      T>( bufAt( x, y ), stride, _w, _h ); }
220
  AreaBuf<const T> subBuf( const int& x, const int& y, const unsigned& _w, const unsigned& _h ) const { return AreaBuf<const T>( bufAt( x, y ), stride, _w, _h ); }
221
};
222
223
typedef AreaBuf<      Pel>  PelBuf;
224
typedef AreaBuf<const Pel> CPelBuf;
225
226
typedef AreaBuf<      TCoeff>  CoeffBuf;
227
typedef AreaBuf<const TCoeff> CCoeffBuf;
228
229
typedef AreaBuf<      TCoeffSig>  CoeffSigBuf;
230
typedef AreaBuf<const TCoeffSig> CCoeffSigBuf;
231
232
typedef AreaBuf<      MotionInfo>  MotionBuf;
233
typedef AreaBuf<const MotionInfo> CMotionBuf;
234
235
typedef AreaBuf<      TCoeff>  PLTescapeBuf;
236
typedef AreaBuf<const TCoeff> CPLTescapeBuf;
237
238
typedef AreaBuf<      bool>  PLTtypeBuf;
239
typedef AreaBuf<const bool> CPLTtypeBuf;
240
241
typedef AreaBuf<      LoopFilterParam>  LFPBuf;
242
typedef AreaBuf<const LoopFilterParam> CLFPBuf;
243
244
0
#define SIZE_AWARE_PER_EL_OP( OP, INC )                     \
245
0
if( ( width & 7 ) == 0 )                                    \
246
0
{                                                           \
247
0
  for( int y = 0; y < height; y++ )                         \
248
0
  {                                                         \
249
0
    for( int x = 0; x < width; x += 8 )                     \
250
0
    {                                                       \
251
0
      OP( x + 0 );                                          \
252
0
      OP( x + 1 );                                          \
253
0
      OP( x + 2 );                                          \
254
0
      OP( x + 3 );                                          \
255
0
      OP( x + 4 );                                          \
256
0
      OP( x + 5 );                                          \
257
0
      OP( x + 6 );                                          \
258
0
      OP( x + 7 );                                          \
259
0
    }                                                       \
260
0
                                                            \
261
0
    INC;                                                    \
262
0
  }                                                         \
263
0
}                                                           \
264
0
else if( ( width & 3 ) == 0 )                               \
265
0
{                                                           \
266
0
  for( int y = 0; y < height; y++ )                         \
267
0
  {                                                         \
268
0
    for( int x = 0; x < width; x += 4 )                     \
269
0
    {                                                       \
270
0
      OP( x + 0 );                                          \
271
0
      OP( x + 1 );                                          \
272
0
      OP( x + 2 );                                          \
273
0
      OP( x + 3 );                                          \
274
0
    }                                                       \
275
0
                                                            \
276
0
    INC;                                                    \
277
0
  }                                                         \
278
0
}                                                           \
279
0
else if( ( width & 1 ) == 0 )                               \
280
0
{                                                           \
281
0
  for( int y = 0; y < height; y++ )                         \
282
0
  {                                                         \
283
0
    for( int x = 0; x < width; x += 2 )                     \
284
0
    {                                                       \
285
0
      OP( x + 0 );                                          \
286
0
      OP( x + 1 );                                          \
287
0
    }                                                       \
288
0
                                                            \
289
0
    INC;                                                    \
290
0
  }                                                         \
291
0
}                                                           \
292
0
else                                                        \
293
0
{                                                           \
294
0
  for( int y = 0; y < height; y++ )                         \
295
0
  {                                                         \
296
0
    for( int x = 0; x < width; x++ )                        \
297
0
    {                                                       \
298
0
      OP( x );                                              \
299
0
    }                                                       \
300
0
                                                            \
301
0
    INC;                                                    \
302
0
  }                                                         \
303
0
}
304
305
306
template<typename T>
307
T AreaBuf <T> ::getAvg() const
308
0
{
309
0
  const T* src = buf;
310
0
  int64_t  acc = 0;
311
312
0
#define AVG_INC      src += stride
313
0
#define AVG_OP(ADDR) acc += src[ADDR]
314
0
  SIZE_AWARE_PER_EL_OP(AVG_OP, AVG_INC);
315
0
#undef AVG_INC
316
0
#undef AVG_OP
317
318
0
  return T ((acc + (area() >> 1)) / area());
319
0
}
Unexecuted instantiation: vvenc::AreaBuf<short const>::getAvg() const
Unexecuted instantiation: vvenc::AreaBuf<short>::getAvg() const
320
321
template<>
322
void AreaBuf<MotionInfo>::fill( const MotionInfo& val );
323
324
template<typename T>
325
void AreaBuf<T>::fill(const T &val)
326
0
{
327
0
  if( T( 0 ) == val )
328
0
  {
329
0
    GCC_WARNING_DISABLE_class_memaccess
330
0
    if( width == stride )
331
0
    {
332
0
      ::memset( buf, 0, width * height * sizeof( T ) );
333
0
    }
334
0
    else
335
0
    {
336
0
      T* dest = buf;
337
0
      size_t line = width * sizeof( T );
338
339
0
      for( unsigned y = 0; y < height; y++ )
340
0
      {
341
0
        ::memset( dest, 0, line );
342
343
0
        dest += stride;
344
0
      }
345
0
    }
346
0
    GCC_WARNING_RESET
347
0
  }
348
0
  else
349
0
  {
350
0
    if( width == stride )
351
0
    {
352
0
      std::fill_n( buf, width * height, val );
353
0
    }
354
0
    else
355
0
    {
356
0
      T* dest = buf;
357
358
0
      for( int y = 0; y < height; y++, dest += stride )
359
0
      {
360
0
        std::fill_n( dest, width, val );
361
0
      }
362
0
    }
363
0
  }
364
0
}
365
366
template<typename T>
367
void AreaBuf<T>::memset( const int val )
368
0
{
369
0
  GCC_WARNING_DISABLE_class_memaccess
370
0
  if( width == stride )
371
0
  {
372
0
    ::memset( buf, val, width * height * sizeof( T ) );
373
0
  }
374
0
  else
375
0
  {
376
0
    T* dest = buf;
377
0
    size_t line = width * sizeof( T );
378
379
0
    for( int y = 0; y < height; y++, dest += stride )
380
0
    {
381
0
      ::memset( dest, val, line );
382
0
    }
383
0
  }
384
0
  GCC_WARNING_RESET
385
0
}
Unexecuted instantiation: vvenc::AreaBuf<short>::memset(int)
Unexecuted instantiation: vvenc::AreaBuf<vvenc::MotionInfo>::memset(int)
Unexecuted instantiation: vvenc::AreaBuf<vvenc::LoopFilterParam>::memset(int)
386
387
template<typename T>
388
void AreaBuf<T>::copyFrom( const AreaBuf<const T>& other )
389
0
{
390
#if !defined(__GNUC__) || __GNUC__ > 5
391
  static_assert( std::is_trivially_copyable<T>::value, "Type T is not trivially_copyable" );
392
#endif
393
394
0
  g_pelBufOP.copyBuffer( ( const char* ) other.buf, other.stride * sizeof( T ), ( char* ) buf, stride * sizeof( T ), width * sizeof( T ), height );
395
0
}
Unexecuted instantiation: vvenc::AreaBuf<vvenc::MotionInfo>::copyFrom(vvenc::AreaBuf<vvenc::MotionInfo const> const&)
Unexecuted instantiation: vvenc::AreaBuf<short>::copyFrom(vvenc::AreaBuf<short const> const&)
396
397
template<typename T>
398
void AreaBuf<T>::subtract( const AreaBuf<const T>& minuend, const AreaBuf<const T>& subtrahend )
399
{
400
  THROW( "Type not supported" );
401
}
402
403
template<>
404
void AreaBuf<Pel>::subtract( const AreaBuf<const Pel>& minuend, const AreaBuf<const Pel>& subtrahend );
405
406
template<typename T>
407
void AreaBuf<T>::calcVarianceSplit( const AreaBuf<const T>& Org, const uint32_t  size,int& varh,int& varv ) const 
408
{
409
  THROW( "Type not supported" );
410
}
411
412
template<>
413
void AreaBuf<const Pel>::calcVarianceSplit( const AreaBuf<const Pel>& Org, const uint32_t  size, int& varh,int&varv ) const;
414
415
template<typename T>
416
void AreaBuf<T>::copyClip( const AreaBuf<const T>& src, const ClpRng& clpRng )
417
{
418
  THROW( "Type not supported" );
419
}
420
421
template<>
422
void AreaBuf<Pel>::copyClip( const AreaBuf<const Pel>& src, const ClpRng& clpRng );
423
424
template<typename T>
425
void AreaBuf<T>::reconstruct( const AreaBuf<const T>& pred, const AreaBuf<const T>& resi, const ClpRng& clpRng )
426
{
427
  THROW( "Type not supported" );
428
}
429
430
template<>
431
void AreaBuf<Pel>::reconstruct( const AreaBuf<const Pel>& pred, const AreaBuf<const Pel>& resi, const ClpRng& clpRng );
432
433
template<>
434
void AreaBuf<Pel>::rspSignal( const AreaBuf<const Pel>& other, const Pel* pLUT);
435
436
template<typename T>
437
void AreaBuf<T>::rspSignal( const AreaBuf<const T>& other, const Pel* pLUT)
438
{
439
  THROW( "Type not supported" );
440
}
441
442
443
template<typename T>
444
void AreaBuf<T>::addAvg( const AreaBuf<const T>& other1, const AreaBuf<const T>& other2, const ClpRng& clpRng )
445
{
446
  THROW( "Type not supported" );
447
}
448
449
template<>
450
void AreaBuf<Pel>::addAvg( const AreaBuf<const Pel>& other1, const AreaBuf<const Pel>& other2, const ClpRng& clpRng );
451
452
template<typename T>
453
void AreaBuf<T>::linearTransform( const int scale, const unsigned shift, const int offset, bool bClip, const ClpRng& clpRng )
454
{
455
  THROW( "Type not supported" );
456
}
457
458
template<>
459
void AreaBuf<Pel>::linearTransform( const int scale, const unsigned shift, const int offset, bool bClip, const ClpRng& clpRng );
460
461
template<typename T>
462
void AreaBuf<T>::removeHighFreq( const AreaBuf<const T>& other, const bool bClip, const ClpRng& clpRng )
463
0
{
464
0
  const T*  src       = other.buf;
465
0
  const int srcStride = other.stride;
466
467
0
        T*  dst       = buf;
468
0
  const int dstStride = stride;
469
470
0
#if ENABLE_SIMD_OPT_BCW
471
0
  if (!bClip)
472
0
  {
473
0
    if(!(width & 7))
474
0
      g_pelBufOP.removeHighFreq8(dst, dstStride, src, srcStride, width, height);
475
0
    else if (!(width & 3))
476
0
      g_pelBufOP.removeHighFreq4(dst, dstStride, src, srcStride, width, height);
477
0
    else
478
0
      CHECK(true, "Not supported");
479
0
  }
480
0
  else
481
0
  {
482
0
#endif
483
484
0
#define REM_HF_INC  \
485
0
  src += srcStride; \
486
0
  dst += dstStride; \
487
0
488
0
#define REM_HF_OP_CLIP( ADDR ) dst[ADDR] = ClipPel<T>( 2 * dst[ADDR] - src[ADDR], clpRng )
489
0
#define REM_HF_OP( ADDR )      dst[ADDR] =             2 * dst[ADDR] - src[ADDR]
490
491
0
  if( bClip )
492
0
  {
493
0
    SIZE_AWARE_PER_EL_OP( REM_HF_OP_CLIP, REM_HF_INC );
494
0
  }
495
0
  else
496
0
  {
497
0
    SIZE_AWARE_PER_EL_OP( REM_HF_OP,      REM_HF_INC );
498
0
  }
499
500
0
#undef REM_HF_INC
501
0
#undef REM_HF_OP
502
0
#undef REM_HF_OP_CLIP
503
504
0
#if ENABLE_SIMD_OPT_BCW
505
0
  }
506
0
#endif
507
0
}
508
509
510
template<typename T>
511
void AreaBuf<T>::extendBorderPelLft( int y, int size, int margin )
512
0
{
513
0
  T* p = buf + y * stride;
514
0
  int h = size + y;
515
516
  // do left and right margins
517
0
  for (; y < h; y++)
518
0
  {
519
0
    for (int x = 0; x < margin; x++)
520
0
    {
521
0
      *(p - margin + x) = p[0];
522
0
    }
523
0
    p += stride;
524
0
  }
525
0
}
526
527
template<typename T>
528
void AreaBuf<T>::extendBorderPelTop( int x, int size, int margin )
529
0
{
530
0
  T* p = buf + x;
531
532
  // p is now (-marginX, 0)
533
0
  for (int y = 0; y < margin; y++)
534
0
  {
535
0
    ::memcpy(p - (y + 1) * stride, p, sizeof(T) * size);
536
0
  }
537
0
}
538
539
template<typename T>
540
void AreaBuf<T>::extendBorderPelRgt( int y, int size, int margin )
541
0
{
542
0
  T* p = buf + y * stride;
543
0
  int h = size + y;
544
545
  // do left and right margins
546
0
  for( ; y < h; y++)
547
0
  {
548
0
    for (int x = 0; x < margin; x++)
549
0
    {
550
0
      p[width + x] = p[width - 1];
551
0
    }
552
0
    p += stride;
553
0
  }
554
0
}
555
556
template<typename T>
557
void AreaBuf<T>::extendBorderPelBot( int x, int size, int margin )
558
0
{
559
0
  T* p = buf + (height-1)*stride + x;
560
561
  // p is now the (-margin, height-1)
562
0
  for (int y = 0; y < margin; y++)
563
0
  {
564
0
    ::memcpy(p + (y + 1) * stride, p, sizeof(T) * size);
565
0
  }
566
0
}
567
568
template<typename T>
569
void AreaBuf<T>::extendBorderPel(unsigned marginX, unsigned marginY)
570
0
{
571
0
  T* p = buf;
572
0
  int h = height;
573
0
  int w = width;
574
0
  int s = stride;
575
576
0
  CHECK((w + 2 * marginX) > s, "Size of buffer too small to extend");
577
  // do left and right margins
578
0
  for (int y = 0; y < h; y++)
579
0
  {
580
0
    for (int x = 0; x < marginX; x++)
581
0
    {
582
0
      *(p - marginX + x) = p[0];
583
0
      p[w + x] = p[w - 1];
584
0
    }
585
0
    p += s;
586
0
  }
587
588
  // p is now the (0,height) (bottom left of image within bigger picture
589
0
  p -= (s + marginX);
590
  // p is now the (-margin, height-1)
591
0
  for (int y = 0; y < marginY; y++)
592
0
  {
593
0
    ::memcpy(p + (y + 1) * s, p, sizeof(T) * (w + (marginX << 1)));
594
0
  }
595
596
  // p is still (-marginX, height-1)
597
0
  p -= ((h - 1) * s);
598
  // p is now (-marginX, 0)
599
0
  for (int y = 0; y < marginY; y++)
600
0
  {
601
0
    ::memcpy(p - (y + 1) * s, p, sizeof(T) * (w + (marginX << 1)));
602
0
  }
603
0
}
604
605
template<typename T>
606
void AreaBuf<T>::padBorderPel( unsigned marginX, unsigned marginY, int dir )
607
0
{
608
0
  T*  p = buf;
609
0
  int s = stride;
610
0
  int h = height;
611
0
  int w = width;
612
613
0
  CHECK( w  > s, "Size of buffer too small to extend" );
614
615
  // top-left margin
616
0
  if ( dir == 1 )
617
0
  {
618
0
    for( int y = 0; y < marginY; y++ )
619
0
    {
620
0
      for( int x = 0; x < marginX; x++ )
621
0
      {
622
0
        p[x] = p[marginX];
623
0
      }
624
0
      p += s;
625
0
    }
626
0
  }
627
628
  // bottom-right margin
629
0
  if ( dir == 2 )
630
0
  {
631
0
    p = buf + s * ( h - marginY ) + w - marginX;
632
633
0
    for( int y = 0; y < marginY; y++ )
634
0
    {
635
0
      for( int x = 0; x < marginX; x++ )
636
0
      {
637
0
        p[x] = p[-1];
638
0
      }
639
0
      p += s;
640
0
    }
641
0
  }
642
0
}
643
644
645
#if ENABLE_SIMD_OPT_BUFFER
646
template<> void AreaBuf<Pel>::transposedFrom( const AreaBuf<const Pel>& other );
647
#endif
648
649
template<typename T>
650
void AreaBuf<T>::transposedFrom( const AreaBuf<const T>& other )
651
{
652
  CHECK( width * height != other.width * other.height, "Incompatible size" );
653
654
        T* dst  =       buf;
655
  const T* src  = other.buf;
656
  width         = other.height;
657
  height        = other.width;
658
  stride        = stride < width ? width : stride;
659
660
  for( unsigned y = 0; y < other.height; y++ )
661
  {
662
    for( unsigned x = 0; x < other.width; x++ )
663
    {
664
      dst[y + x*stride] = src[x + y*other.stride];
665
    }
666
  }
667
}
668
669
template<typename T>
670
bool AreaBuf <T> ::compare( const AreaBuf<const T>& other ) const
671
{
672
        T* mine   =       buf;
673
  const T* theirs = other.buf;
674
  if( width != other.width) return false;
675
  if( height != other.height) return false;
676
677
  for( unsigned y = 0; y < other.height; y++ )
678
  {
679
    for( unsigned x = 0; x < other.width; x++ )
680
    {
681
      if( mine[x + y*stride] != theirs[x+y*other.stride])
682
      {
683
        return false;
684
      }
685
    }
686
  }
687
  return true;
688
}
689
690
#ifndef DONT_UNDEF_SIZE_AWARE_PER_EL_OP
691
#undef SIZE_AWARE_PER_EL_OP
692
#endif // !DONT_UNDEF_SIZE_AWARE_PER_EL_OP
693
694
// ---------------------------------------------------------------------------
695
// UnitBuf struct
696
// ---------------------------------------------------------------------------
697
698
struct UnitArea;
699
700
template<typename T>
701
struct UnitBuf
702
{
703
  typedef static_vector<AreaBuf<T>,       MAX_NUM_COMP> UnitBufBuffers;
704
  typedef static_vector<AreaBuf<const T>, MAX_NUM_COMP> ConstUnitBufBuffers;
705
706
  ChromaFormat chromaFormat;
707
  UnitBufBuffers bufs;
708
709
0
  UnitBuf() : chromaFormat( NUM_CHROMA_FORMAT ) { }
Unexecuted instantiation: vvenc::UnitBuf<short>::UnitBuf()
Unexecuted instantiation: vvenc::UnitBuf<short const>::UnitBuf()
710
  UnitBuf( const ChromaFormat _chromaFormat, const UnitBufBuffers&  _bufs ) : chromaFormat( _chromaFormat ), bufs( _bufs ) { }
711
  UnitBuf( const ChromaFormat _chromaFormat,       UnitBufBuffers&& _bufs ) : chromaFormat( _chromaFormat ), bufs( std::forward<UnitBufBuffers>( _bufs ) ) { }
712
0
  UnitBuf( const ChromaFormat _chromaFormat, const AreaBuf<T>&  blkY )      : chromaFormat( _chromaFormat ), bufs{ blkY } { }
713
0
  UnitBuf( const ChromaFormat _chromaFormat,       AreaBuf<T>&& blkY )      : chromaFormat( _chromaFormat ), bufs{ std::forward<AreaBuf<T> >(blkY) } { }
Unexecuted instantiation: vvenc::UnitBuf<short>::UnitBuf(vvencChromaFormat, vvenc::AreaBuf<short>&&)
Unexecuted instantiation: vvenc::UnitBuf<short const>::UnitBuf(vvencChromaFormat, vvenc::AreaBuf<short const>&&)
714
0
  UnitBuf( const ChromaFormat _chromaFormat, const AreaBuf<T>&  blkY, const AreaBuf<T>&  blkCb, const AreaBuf<T>  &blkCr ) : chromaFormat( _chromaFormat ), bufs{ blkY, blkCb, blkCr } { }
715
0
  UnitBuf( const ChromaFormat _chromaFormat,       AreaBuf<T>&& blkY,       AreaBuf<T>&& blkCb,       AreaBuf<T> &&blkCr ) : chromaFormat( _chromaFormat ), bufs{ std::forward<AreaBuf<T> >(blkY), std::forward<AreaBuf<T> >(blkCb), std::forward<AreaBuf<T> >(blkCr) } { }
Unexecuted instantiation: vvenc::UnitBuf<short>::UnitBuf(vvencChromaFormat, vvenc::AreaBuf<short>&&, vvenc::AreaBuf<short>&&, vvenc::AreaBuf<short>&&)
Unexecuted instantiation: vvenc::UnitBuf<short const>::UnitBuf(vvencChromaFormat, vvenc::AreaBuf<short const>&&, vvenc::AreaBuf<short const>&&, vvenc::AreaBuf<short const>&&)
716
717
0
  UnitBuf( const UnitBuf& other )  = default;
718
  UnitBuf(       UnitBuf&& other ) = default;
719
  UnitBuf& operator=( const UnitBuf& other )  = default;
720
  UnitBuf& operator=(       UnitBuf&& other ) = default;
721
722
  // conversion from UnitBuf<const T> to UnitBuf<T>
723
  template<bool T_IS_COST = std::is_const<T>::value>
724
0
  UnitBuf( const UnitBuf<typename std::remove_const<T>::type>& other, std::enable_if<T_IS_COST>* = 0 ) : chromaFormat( other.chromaFormat ), bufs( other.bufs.begin(), other.bufs.end() ) { }
725
726
0
        AreaBuf<T>& get( const ComponentID comp )        { return bufs[comp]; }
Unexecuted instantiation: vvenc::UnitBuf<short>::get(vvenc::ComponentID)
Unexecuted instantiation: vvenc::UnitBuf<short const>::get(vvenc::ComponentID)
727
0
  const AreaBuf<T>& get( const ComponentID comp )  const { return bufs[comp]; }
Unexecuted instantiation: vvenc::UnitBuf<short>::get(vvenc::ComponentID) const
Unexecuted instantiation: vvenc::UnitBuf<short const>::get(vvenc::ComponentID) const
728
729
0
        AreaBuf<T>& Y()        { return bufs[0]; }
Unexecuted instantiation: vvenc::UnitBuf<short>::Y()
Unexecuted instantiation: vvenc::UnitBuf<short const>::Y()
730
0
  const AreaBuf<T>& Y()  const { return bufs[0]; }
731
0
        AreaBuf<T>& Cb()       { return bufs[1]; }
732
  const AreaBuf<T>& Cb() const { return bufs[1]; }
733
0
        AreaBuf<T>& Cr()       { return bufs[2]; }
734
  const AreaBuf<T>& Cr() const { return bufs[2]; }
735
0
  bool valid          () const { return bufs.size() != 0; }
736
737
  void fill                 ( const T& val );
738
  void copyFrom             ( const UnitBuf<const T> &other, const bool luma = true, const bool chroma = true );
739
  void reconstruct          ( const UnitBuf<const T>& pred, const UnitBuf<const T>& resi, const ClpRngs& clpRngs );
740
  void copyClip             ( const UnitBuf<const T> &src, const ClpRngs& clpRngs, const bool lumaOnly = false, const bool chromaOnly = false );
741
  void subtract             ( const UnitBuf<const T>& minuend, const UnitBuf<const T>& subtrahend );
742
  void addAvg               ( const UnitBuf<const T>& other1, const UnitBuf<const T>& other2, const ClpRngs& clpRngs, const bool chromaOnly = false, const bool lumaOnly = false);
743
  void addWeightedAvg       ( const UnitBuf<const T>& other1, const UnitBuf<const T>& other2, const ClpRngs& clpRngs, const uint8_t BcwIdx = BCW_DEFAULT, const bool chromaOnly = false, const bool lumaOnly = false);
744
  void padBorderPel         ( unsigned margin, int dir );
745
  void extendBorderPel      ( unsigned margin, bool scale = false);
746
  void extendBorderPelTop   ( int x, int size, int margin );
747
  void extendBorderPelBot   ( int x, int size, int margin );
748
  void extendBorderPelLft   ( int y, int size, int margin );
749
  void extendBorderPelRgt   ( int y, int size, int margin );
750
751
  void removeHighFreq       ( const UnitBuf<const T>& other, const bool bClip, const ClpRngs& clpRngs);
752
753
        UnitBuf<      T> subBuf (const UnitArea& subArea);
754
  const UnitBuf<const T> subBuf (const UnitArea& subArea) const;
755
};
756
757
typedef UnitBuf<      Pel>  PelUnitBuf;
758
typedef UnitBuf<const Pel> CPelUnitBuf;
759
760
typedef UnitBuf<      TCoeff>  CoeffUnitBuf;
761
typedef UnitBuf<const TCoeff> CCoeffUnitBuf;
762
763
template<typename T>
764
void UnitBuf<T>::fill( const T &val )
765
0
{
766
0
  for( int i = 0; i < bufs.size(); i++ )
767
0
  {
768
0
    bufs[i].fill( val );
769
0
  }
770
0
}
771
772
773
template<typename T>
774
void UnitBuf<T>::copyFrom(const UnitBuf<const T> &other, const bool luma, const bool chroma)
775
0
{
776
0
  CHECK( chromaFormat != other.chromaFormat, "Incompatible formats" );
777
0
  CHECK( !luma && !chroma, "At least one channel has to be selected" );
778
779
0
  const size_t compStart = luma   ? 0           : 1;
780
0
  const size_t compEnd   = chroma ? bufs.size() : 1;
781
782
0
  for( size_t i = compStart; i < compEnd; i++ )
783
0
  {
784
0
    if( bufs[ i ].buf != nullptr && other.bufs[ i ].buf != nullptr )
785
0
      bufs[i].copyFrom( other.bufs[i] );
786
0
  }
787
0
}
788
789
template<typename T>
790
void UnitBuf<T>::subtract( const UnitBuf<const T>& minuend, const UnitBuf<const T>& subtrahend )
791
0
{
792
0
  CHECK( chromaFormat != minuend.chromaFormat, "Incompatible formats" );
793
0
  CHECK( chromaFormat != subtrahend.chromaFormat, "Incompatible formats");
794
795
0
  for( int i = 0; i < bufs.size(); i++ )
796
0
  {
797
0
    bufs[i].subtract( minuend.bufs[i], subtrahend.bufs[i] );
798
0
  }
799
0
}
800
801
template<typename T>
802
void UnitBuf<T>::copyClip(const UnitBuf<const T> &src, const ClpRngs &clpRngs, const bool lumaOnly, const bool chromaOnly)
803
0
{
804
0
  CHECK( chromaFormat != src.chromaFormat, "Incompatible formats" );
805
806
0
  CHECK(lumaOnly && chromaOnly, "Not allowed to have both lumaOnly and chromaOnly selected");
807
0
  const int compStart = chromaOnly ? 1 : 0;
808
0
  const int compEnd   = lumaOnly   ? 1 : ( int ) bufs.size();
809
0
  for( int i = compStart; i < compEnd; i++ )
810
0
  {
811
0
    bufs[i].copyClip( src.bufs[i], clpRngs[i] );
812
0
  }
813
0
}
814
815
template<typename T>
816
void UnitBuf<T>::reconstruct(const UnitBuf<const T>& pred, const UnitBuf<const T>& resi, const ClpRngs& clpRngs)
817
0
{
818
0
  CHECK( chromaFormat != pred.chromaFormat, "Incompatible formats" );
819
0
  CHECK( chromaFormat != resi.chromaFormat, "Incompatible formats" );
820
821
0
  for( int i = 0; i < bufs.size(); i++ )
822
0
  {
823
0
    bufs[i].reconstruct( pred.bufs[i], resi.bufs[i], clpRngs[i] );
824
0
  }
825
0
}
826
827
template<typename T>
828
void UnitBuf<T>::addAvg(const UnitBuf<const T>& other1, const UnitBuf<const T>& other2, const ClpRngs& clpRngs, const bool chromaOnly /* = false */, const bool lumaOnly /* = false */)
829
0
{
830
0
  const int istart = chromaOnly ? 1 : 0;
831
0
  const int iend   = lumaOnly   ? 1 : ( int ) bufs.size();
832
833
0
  CHECK( lumaOnly && chromaOnly, "should not happen" );
834
835
0
  for( int i = istart; i < iend; i++)
836
0
  {
837
0
    bufs[i].addAvg( other1.bufs[i], other2.bufs[i], clpRngs[i]);
838
0
  }
839
0
}
840
841
template<typename T>
842
void UnitBuf<T>::addWeightedAvg(const UnitBuf<const T>& other1, const UnitBuf<const T>& other2, const ClpRngs& clpRngs, const uint8_t BcwIdx /* = BCW_DEFAULT */, const bool chromaOnly /* = false */, const bool lumaOnly /* = false */)
843
0
{
844
0
  const int istart = chromaOnly ? 1 : 0;
845
0
  const int iend   = lumaOnly   ? 1 : ( int ) bufs.size();
846
847
0
  CHECK( lumaOnly && chromaOnly, "should not happen" );
848
849
0
  for ( int i = istart; i < iend; i++)
850
0
  {
851
0
    bufs[i].addWeightedAvg(other1.bufs[i], other2.bufs[i], clpRngs[i], BcwIdx);
852
0
  }
853
0
}
854
855
template<typename T>
856
void UnitBuf<T>::extendBorderPelTop   ( int x, int size, int margin )
857
0
{
858
0
  for( int i = 0; i < bufs.size(); i++ )
859
0
  {
860
0
    int csx = getComponentScaleX(ComponentID(i), chromaFormat);
861
0
    int csy = getComponentScaleY(ComponentID(i), chromaFormat);
862
0
    bufs[i].extendBorderPelTop( x>>csx, size>>csx, margin>>csy );
863
0
  }
864
0
}
865
866
template<typename T>
867
void UnitBuf<T>::extendBorderPelBot   ( int x, int size, int margin )
868
0
{
869
0
  for( int i = 0; i < bufs.size(); i++ )
870
0
  {
871
0
    int csx = getComponentScaleX(ComponentID(i), chromaFormat);
872
0
    int csy = getComponentScaleY(ComponentID(i), chromaFormat);
873
0
    bufs[i].extendBorderPelBot( x>>csx, size>>csx, margin>>csy );
874
0
  }
875
0
}
876
877
template<typename T>
878
void UnitBuf<T>::extendBorderPelLft   ( int y, int size, int margin )
879
0
{
880
0
  for( int i = 0; i < bufs.size(); i++ )
881
0
  {
882
0
    int csx = getComponentScaleX(ComponentID(i), chromaFormat);
883
0
    int csy = getComponentScaleY(ComponentID(i), chromaFormat);
884
0
    bufs[i].extendBorderPelLft( y>>csy, size>>csy, margin>>csx );
885
0
  }
886
0
}
887
888
template<typename T>
889
void UnitBuf<T>::extendBorderPelRgt   ( int y, int size, int margin )
890
0
{
891
0
  for( int i = 0; i < bufs.size(); i++ )
892
0
  {
893
0
    int csx = getComponentScaleX(ComponentID(i), chromaFormat);
894
0
    int csy = getComponentScaleY(ComponentID(i), chromaFormat);
895
0
    bufs[i].extendBorderPelRgt( y>>csy, size>>csy, margin>>csx );
896
0
  }
897
0
}
898
899
template<typename T>
900
void UnitBuf<T>::extendBorderPel( unsigned margin, bool scaleMargin )
901
0
{
902
0
  if( ! scaleMargin )
903
0
  {
904
0
    for( int i = 0; i < bufs.size(); i++ )
905
0
    {
906
0
      bufs[i].extendBorderPel( margin, margin );
907
0
    }
908
0
  }
909
0
  else
910
0
  {
911
0
    for ( int i = 0; i < bufs.size(); i++)
912
0
    {
913
0
      bufs[i].extendBorderPel(margin >> getComponentScaleX(ComponentID(i), chromaFormat), margin >> getComponentScaleY(ComponentID(i), chromaFormat));
914
0
    }
915
0
  }
916
0
}
917
918
template<typename T>
919
void UnitBuf<T>::padBorderPel( unsigned margin, int dir )
920
0
{
921
0
  for( int i = 0; i < bufs.size(); i++ )
922
0
  {
923
0
    bufs[i].padBorderPel( margin >> getComponentScaleX( ComponentID( i ), chromaFormat ), margin >> getComponentScaleY( ComponentID( i ), chromaFormat ), dir );
924
0
  }
925
0
}
926
927
template<typename T>
928
void UnitBuf<T>::removeHighFreq( const UnitBuf<const T>& other, const bool bClip, const ClpRngs& clpRngs)
929
0
{
930
0
  bufs[0].removeHighFreq(other.bufs[0], bClip, clpRngs[0]);
931
0
}
932
933
template<typename T>
934
UnitBuf<T> UnitBuf<T>::subBuf( const UnitArea& subArea )
935
0
{
936
0
  UnitBuf<T> subBuf;
937
0
  subBuf.chromaFormat = chromaFormat;
938
0
  unsigned blockIdx = 0;
939
940
0
  for( auto &subAreaBuf : bufs )
941
0
  {
942
0
    subBuf.bufs.push_back( subAreaBuf.subBuf( subArea.blocks[blockIdx].pos(), subArea.blocks[blockIdx].size() ) );
943
0
    blockIdx++;
944
0
  }
945
946
0
  return subBuf;
947
0
}
948
949
950
template<typename T>
951
const UnitBuf<const T> UnitBuf<T>::subBuf( const UnitArea& subArea ) const
952
0
{
953
0
  UnitBuf<const T> subBuf;
954
0
  subBuf.chromaFormat = chromaFormat;
955
0
  unsigned blockIdx = 0;
956
957
0
  for( const auto &subAreaBuf : bufs )
958
0
  {
959
0
    subBuf.bufs.push_back( subAreaBuf.subBuf( subArea.blocks[blockIdx].pos(), subArea.blocks[blockIdx].size() ) );
960
0
    blockIdx++;
961
0
  }
962
963
0
  return subBuf;
964
0
}
965
966
// ---------------------------------------------------------------------------
967
// PelStorage struct (PelUnitBuf which allocates its own memory)
968
// ---------------------------------------------------------------------------
969
970
struct UnitArea;
971
struct CompArea;
972
973
struct PelStorage : public PelUnitBuf
974
{
975
  PelStorage();
976
  ~PelStorage();
977
978
  void swap( PelStorage& other );
979
  void createFromBuf( PelUnitBuf buf );
980
  void takeOwnership( PelStorage& other );
981
  void create( const UnitArea& _unit );
982
  void create( const ChromaFormat &_chromaFormat, const Area& _area );
983
  void create( const ChromaFormat &_chromaFormat, const Area& _area, const unsigned _maxCUSize, const unsigned _margin = 0, const unsigned _alignment = 0, const bool _scaleChromaMargin = true );
984
  void destroy();
985
  void compactResize( const UnitArea& area );
986
987
         PelBuf getBuf( const CompArea& blk );
988
  const CPelBuf getBuf( const CompArea& blk ) const;
989
990
         PelBuf getBuf( const ComponentID CompID );
991
  const CPelBuf getBuf( const ComponentID CompID ) const;
992
993
         PelUnitBuf getBuf( const UnitArea& unit );
994
  const CPelUnitBuf getBuf( const UnitArea& unit ) const;
995
0
               Pel* getOrigin( const int id ) const { return m_origin[id]; }
996
997
         PelUnitBuf getBuf(const int strY, const int strCb, const int strCr, const UnitArea& unit);
998
  const CPelUnitBuf getBuf(const int strY, const int strCb, const int strCr, const UnitArea& unit) const;
999
1000
         PelUnitBuf getBufPart( const UnitArea& unit );
1001
  const CPelUnitBuf getBufPart( const UnitArea& unit ) const;
1002
1003
         PelUnitBuf getCompactBuf(const UnitArea& unit);
1004
  const CPelUnitBuf getCompactBuf(const UnitArea& unit) const;
1005
1006
         PelBuf     getCompactBuf(const CompArea& blk);
1007
  const CPelBuf     getCompactBuf(const CompArea& blk) const;
1008
1009
private:
1010
1011
  UnitArea m_maxArea;
1012
  Pel* m_origin[MAX_NUM_COMP];
1013
};
1014
1015
struct CompStorage : public PelBuf
1016
{
1017
0
  ~CompStorage() { if( valid() ) delete[] m_memory; }
1018
1019
  void compactResize( const Size& size )
1020
0
  {
1021
0
    CHECK( size.area() > m_allocSize, "Resizing causes buffer overflow!" );
1022
0
    Size::operator=( size );
1023
0
    stride = size.width;
1024
0
  }
1025
  void create( const Size& size )
1026
0
  {
1027
0
    CHECK( m_memory, "Trying to re-create an already initialized buffer" );
1028
0
    m_allocSize = size.area();
1029
0
    m_memory = new Pel[m_allocSize];
1030
0
    PelBuf::operator=( PelBuf( m_memory, size ) );
1031
0
  }
1032
  void destroy()
1033
0
  {
1034
0
    if( valid() ) delete[] m_memory;
1035
0
    m_memory    = nullptr;
1036
0
    m_allocSize = 0;
1037
0
  }
1038
0
  bool valid() { return m_memory != nullptr; }
1039
private:
1040
  ptrdiff_t m_allocSize = 0;
1041
  Pel*      m_memory    = nullptr;
1042
};
1043
1044
template<int NumEntries>
1045
struct SortedPelUnitBufs
1046
{
1047
  void create(ChromaFormat cform, int maxWidth, int maxHeight)
1048
0
  {
1049
0
    m_pacBufs.resize(NumEntries + 1);
1050
0
    m_acStorage.resize(NumEntries + 1);
1051
0
    for (size_t i = 0; i <= NumEntries; i++)
1052
0
    {
1053
0
      m_acStorage[i].create(cform, Area(0, 0, maxWidth, maxHeight));
1054
0
    }
1055
0
  }
1056
1057
  void destroy()
1058
0
  {
1059
0
    for (size_t i = 0; i <= NumEntries; i++)
1060
0
    {
1061
0
      m_acStorage[i].destroy();
1062
0
    }
1063
0
  }
1064
1065
  void reset()
1066
  {
1067
    m_sortedList.clear();
1068
  }
1069
1070
  void reduceTo(int numModes)
1071
0
  {
1072
0
    CHECK( numModes > m_sortedList.size(), "not enough buffers");
1073
0
    m_sortedList.resize(numModes);
1074
0
  }
1075
1076
  void prepare( const UnitArea& ua, int numModes)
1077
0
  {
1078
0
    CHECK( numModes > NumEntries, "not enough buffers");
1079
0
    m_sortedList.resize(numModes);
1080
0
    for (size_t i = 0; i < numModes; i++)
1081
0
    {
1082
0
      m_pacBufs[i] = m_acStorage[i].getCompactBuf(ua);
1083
0
      m_sortedList[i] = &m_pacBufs[i];
1084
0
    }
1085
1086
0
    m_pacBufs[numModes] = m_acStorage[numModes].getCompactBuf(ua);
1087
0
    m_TestBuf = &m_pacBufs[numModes];
1088
0
  }
1089
1090
0
  PelUnitBuf* getBufFromSortedList( int idx)              const { return m_sortedList.size() > idx ? m_sortedList[idx]: nullptr; }
1091
0
  PelBuf&     getTestBuf          ( ComponentID compId)   const { return m_TestBuf->bufs[compId]; }
1092
0
  PelUnitBuf& getTestBuf          ()                      const { return *m_TestBuf; }
1093
1094
  void swap( unsigned pos1, unsigned pos2 )
1095
0
  {
1096
0
    CHECK( pos1 >= m_sortedList.size(), "index out of range" );
1097
0
    CHECK( pos2 >= m_sortedList.size(), "index out of range" );
1098
0
    std::swap(m_sortedList[pos1], m_sortedList[pos2]);
1099
0
  }
1100
1101
  void insert(int insertPos, int RdListSize)
1102
0
  {
1103
0
    if (insertPos != -1)
1104
0
    {
1105
0
      for (int i = RdListSize - 1; i > insertPos; i--)
1106
0
      {
1107
0
        std::swap(m_sortedList[i - 1], m_sortedList[i]);
1108
0
      }
1109
0
      std::swap(m_TestBuf, m_sortedList[insertPos]);
1110
0
    }
1111
0
  }
1112
1113
private:
1114
  PelUnitBuf*                             m_TestBuf;
1115
  static_vector<PelUnitBuf*,NumEntries>   m_sortedList;
1116
  static_vector<PelUnitBuf, NumEntries+1> m_pacBufs;
1117
  static_vector<PelStorage, NumEntries+1> m_acStorage;
1118
};
1119
1120
struct Window;
1121
1122
void copyPadToPelUnitBuf( PelUnitBuf pelUnitBuf, const vvencYUVBuffer& yuvBuffer, const ChromaFormat& chFmt );
1123
//void setupPelUnitBuf( const YUVBuffer& yuvBuffer, PelUnitBuf& pelUnitBuf, const ChromaFormat& chFmt );
1124
void setupYuvBuffer ( const PelUnitBuf& pelUnitBuf, vvencYUVBuffer& yuvBuffer, const Window* confWindow );
1125
1126
} // namespace vvenc
1127
1128
//! \}
1129