Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvdec/source/Lib/CommonLib/Buffer.cpp
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) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC 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
43
/** \file     Buffer.cpp
44
 *  \brief    Low-overhead class describing 2D memory layout
45
 */
46
47
#define DONT_UNDEF_SIZE_AWARE_PER_EL_OP
48
49
// unit needs to come first due to a forward declaration
50
51
#include "Unit.h"
52
#include "Buffer.h"
53
#include "InterpolationFilter.h"
54
#include "Picture.h"
55
#include "Slice.h"
56
57
#include <memory>
58
59
#if ENABLE_SIMD_OPT_BUFFER && defined( TARGET_SIMD_X86 )
60
#include "CommonDefX86.h"
61
#endif
62
63
namespace vvdec
64
{
65
66
template< typename T >
67
void addAvgCore( const T* src1, ptrdiff_t src1Stride, const T* src2, ptrdiff_t src2Stride, T* dest, ptrdiff_t dstStride, int width, int height, int rshift, int offset, const ClpRng& clpRng )
68
0
{
69
0
#define ADD_AVG_CORE_OP( ADDR ) dest[ADDR] = ClipPel( rightShift( ( src1[ADDR] + src2[ADDR] + offset ), rshift ), clpRng )
70
0
#define ADD_AVG_CORE_INC    \
71
0
  src1 += src1Stride;       \
72
0
  src2 += src2Stride;       \
73
0
  dest +=  dstStride;       \
74
0
75
0
  SIZE_AWARE_PER_EL_OP( ADD_AVG_CORE_OP, ADD_AVG_CORE_INC );
76
77
0
#undef ADD_AVG_CORE_OP
78
0
#undef ADD_AVG_CORE_INC
79
0
}
80
81
template<typename T>
82
void reconstructCore( const T* src1, ptrdiff_t src1Stride, const T* src2, ptrdiff_t src2Stride, T* dest, ptrdiff_t dstStride, int width, int height, const ClpRng& clpRng )
83
0
{
84
0
#define RECO_CORE_OP( ADDR ) dest[ADDR] = ClipPel( src1[ADDR] + src2[ADDR], clpRng )
85
0
#define RECO_CORE_INC     \
86
0
  src1 += src1Stride;     \
87
0
  src2 += src2Stride;     \
88
0
  dest +=  dstStride;     \
89
0
90
0
  SIZE_AWARE_PER_EL_OP( RECO_CORE_OP, RECO_CORE_INC );
91
92
0
#undef RECO_CORE_OP
93
0
#undef RECO_CORE_INC
94
0
}
95
96
97
template<typename T>
98
void linTfCore( const T* src, ptrdiff_t srcStride, Pel *dst, ptrdiff_t dstStride, int width, int height, int scale, int shift, int offset, const ClpRng& clpRng, bool bClip )
99
0
{
100
0
#define LINTF_CORE_OP( ADDR ) dst[ADDR] = ( Pel ) bClip ? ClipPel( rightShift( scale * src[ADDR], shift ) + offset, clpRng ) : ( rightShift( scale * src[ADDR], shift ) + offset )
101
0
#define LINTF_CORE_INC  \
102
0
  src += srcStride;     \
103
0
  dst += dstStride;     \
104
0
105
0
  SIZE_AWARE_PER_EL_OP( LINTF_CORE_OP, LINTF_CORE_INC );
106
107
0
#undef LINTF_CORE_OP
108
0
#undef LINTF_CORE_INC
109
0
}
110
111
template<typename T>
112
void transpose4x4Core( const Pel* src, ptrdiff_t srcStride, Pel* dst, ptrdiff_t dstStride )
113
0
{
114
0
  for( int i = 0; i < 4; i++ )
115
0
  {
116
0
    for( int j = 0; j < 4; j++ )
117
0
    {
118
0
      dst[j * dstStride] = src[j];
119
0
    }
120
121
0
    dst++;
122
0
    src += srcStride;
123
0
  }
124
0
}
125
126
template<typename T>
127
void transpose8x8Core( const Pel* src, ptrdiff_t srcStride, Pel* dst, ptrdiff_t dstStride )
128
0
{
129
0
  for( int i = 0; i < 8; i++ )
130
0
  {
131
0
    for( int j = 0; j < 8; j++ )
132
0
    {
133
0
      dst[j * dstStride] = src[j];
134
0
    }
135
136
0
    dst++;
137
0
    src += srcStride;
138
0
  }
139
0
}
140
141
template<typename T>
142
void copyClipCore( const T* src, ptrdiff_t srcStride, Pel *dst, ptrdiff_t dstStride, int width, int height, const ClpRng& clpRng )
143
{
144
#define RECO_OP( ADDR ) dst[ADDR] = ClipPel( src[ADDR], clpRng )
145
#define RECO_INC      \
146
    src += srcStride; \
147
    dst += dstStride; \
148
149
  SIZE_AWARE_PER_EL_OP( RECO_OP, RECO_INC );
150
151
#undef RECO_OP
152
#undef RECO_INC
153
}
154
155
template<typename T>
156
void addWeightedAvgCore( const T* src1, ptrdiff_t src1Stride, const T* src2, ptrdiff_t src2Stride, T* dest, ptrdiff_t destStride, int width, int height, int rshift, int offset, int w0, int w1, const ClpRng& clpRng )
157
0
{
158
0
#define ADD_WGHT_AVG_OP( ADDR ) dest[ADDR] = ClipPel( rightShift( ( src1[ADDR]*w0 + src2[ADDR]*w1 + offset ), rshift ), clpRng )
159
0
#define ADD_WGHT_AVG_INC     \
160
0
    src1 += src1Stride; \
161
0
    src2 += src2Stride; \
162
0
    dest += destStride; \
163
0
164
0
  SIZE_AWARE_PER_EL_OP( ADD_WGHT_AVG_OP, ADD_WGHT_AVG_INC );
165
166
0
#undef ADD_WGHT_AVG_OP
167
0
#undef ADD_WGHT_AVG_INC
168
0
}
169
170
void copyBufferCore( const char *src, ptrdiff_t srcStride, char *dst, ptrdiff_t dstStride, int width, int height )
171
0
{
172
#if ENABLE_SIMD_OPT_BUFFER && defined( TARGET_SIMD_X86 )
173
  _mm_prefetch( (const char *) ( src ),             _MM_HINT_T0 );
174
  _mm_prefetch( (const char *) ( src + srcStride ), _MM_HINT_T0 );
175
  _mm_prefetch( (const char *) ( dst ),             _MM_HINT_T0 );
176
  _mm_prefetch( (const char *) ( dst + dstStride ), _MM_HINT_T0 );
177
178
#endif
179
0
  if( width == srcStride && width == dstStride )
180
0
  {
181
0
    memcpy( dst, src, width * height );
182
0
    return;
183
0
  }
184
185
0
  for( int i = 0; i < height; i++ )
186
0
  {
187
#if ENABLE_SIMD_OPT_BUFFER && defined( TARGET_SIMD_X86 )
188
    _mm_prefetch( (const char *) ( src + srcStride ), _MM_HINT_T0 );
189
    _mm_prefetch( (const char *) ( dst + dstStride ), _MM_HINT_T0 );
190
191
#endif
192
0
    memcpy( dst, src, width );
193
194
0
    src += srcStride;
195
0
    dst += dstStride;
196
0
  }
197
0
}
198
199
void applyLutCore( Pel* ptr, ptrdiff_t ptrStride, int width, int height, const Pel* lut )
200
0
{
201
  //    const auto rsp_sgnl_op  = [=, &dst]( int ADDR ){ dst[ADDR] = lut[dst[ADDR]]; };
202
  //    const auto rsp_sgnl_inc = [=, &dst]            { dst += stride;              };
203
204
  //    size_aware_pel_op( rsp_sgnl_op, rsp_sgnl_inc, width, height );
205
206
0
    #define RSP_SGNL_OP( ADDR ) ptr[ADDR] = lut[ptr[ADDR]]
207
0
    #define RSP_SGNL_INC        ptr      += ptrStride;
208
209
0
      SIZE_AWARE_PER_EL_OP( RSP_SGNL_OP, RSP_SGNL_INC )
210
211
0
    #undef RSP_SGNL_OP
212
0
    #undef RSP_SGNL_INC
213
    
214
0
    return;
215
0
}
216
217
// LMCS chroma residual scaling: the residual, clipped to the bit depth, scaled by scale / 2^CSCALE_FP_PREC with
218
// rounding of its magnitude
219
void scaleSignalCore( Pel* ptr, ptrdiff_t ptrStride, int width, int height, const int scale, const int bd )
220
0
{
221
0
  Pel* dst = ptr;
222
0
  Pel* src = ptr;
223
0
  int sign, absval;
224
0
  int maxAbsclipBD = ( 1 << bd ) - 1;
225
226
0
  for (int y = 0; y < height; y++)
227
0
  {
228
0
    for (int x = 0; x < width; x++)
229
0
    {
230
0
      src[x] = Clip3<Pel>( -maxAbsclipBD - 1, maxAbsclipBD, src[x] );
231
0
      sign   = src[x] >= 0 ? 1 : -1;
232
0
      absval = sign * src[x];
233
234
0
      int val = sign * ((absval * scale + (1 << (CSCALE_FP_PREC - 1))) >> CSCALE_FP_PREC);
235
236
0
      if( sizeof( Pel ) == 2 ) // avoid overflow when storing data
237
0
      {
238
0
          val = Clip3<int>(-32768, 32767, val);
239
0
      }
240
0
      dst[x] = (Pel)val;
241
0
    }
242
0
    dst += ptrStride;
243
0
    src += ptrStride;
244
0
  }
245
0
}
246
247
void fillN_CuCore( CodingUnit** ptr, ptrdiff_t ptrStride, int width, int height, CodingUnit* cuPtr )
248
0
{
249
0
  if( width == ptrStride )
250
0
  {
251
0
    std::fill_n( ptr, width * height, cuPtr );
252
0
  }
253
0
  else
254
0
  {
255
0
    CodingUnit** dst = ptr;
256
257
0
    for( int y = 0; y < height; y++, dst += ptrStride )
258
0
    {
259
0
      std::fill_n( dst, width, cuPtr );
260
0
    }
261
0
  }
262
0
}
263
264
void fillN_MotionInfoCore( MotionInfo* ptr, ptrdiff_t ptrStride, int width, int height, const MotionInfo& val )
265
0
{
266
0
  if( width == ptrStride )
267
0
  {
268
0
    std::fill_n( ptr, width * height, val );
269
0
  }
270
0
  else
271
0
  {
272
0
    MotionInfo* dst = ptr;
273
274
0
    for( int y = 0; y < height; y++, dst += ptrStride )
275
0
    {
276
0
      std::fill_n( dst, width, val );
277
0
    }
278
0
  }
279
0
}
280
281
void sampleRateConvCore( const std::pair<int, int> scalingRatio, const std::pair<int, int> compScale,
282
                         const Pel* orgSrc, const ptrdiff_t orgStride, const int orgWidth, const int orgHeight,
283
                         const int beforeScaleLeftOffset, const int beforeScaleTopOffset,
284
                         Pel* scaledSrc, const ptrdiff_t scaledStride, const int scaledWidth, const int scaledHeight,
285
                         const int afterScaleLeftOffset, const int afterScaleTopOffset,
286
                         const int bitDepth, const bool useLumaFilter,
287
                         const bool horCollocatedPositionFlag, const bool verCollocatedPositionFlag )
288
0
{
289
0
  if( orgWidth == scaledWidth && orgHeight == scaledHeight && scalingRatio == SCALE_1X && !beforeScaleLeftOffset && !beforeScaleTopOffset && !afterScaleLeftOffset && !afterScaleTopOffset )
290
0
  {
291
0
    g_pelBufOP.copyBuffer( ( const char * ) orgSrc, orgStride * sizeof( Pel ), ( char* ) scaledSrc, scaledStride * sizeof( Pel ), orgWidth * sizeof( Pel ), orgHeight );
292
293
0
    return;
294
0
  }
295
296
0
  const TFilterCoeff* filterHor = useLumaFilter ? &InterpolationFilter::m_lumaFilter[0][0] : &InterpolationFilter::m_chromaFilter[0][0];
297
0
  const TFilterCoeff* filterVer = useLumaFilter ? &InterpolationFilter::m_lumaFilter[0][0] : &InterpolationFilter::m_chromaFilter[0][0];
298
299
0
  const int numFracPositions  = useLumaFilter ? 15 : 31;
300
0
  const int numFracShift      = useLumaFilter ? 4 : 5;
301
0
  const int posShiftX         = SCALE_RATIO_BITS - numFracShift + compScale.first;
302
0
  const int posShiftY         = SCALE_RATIO_BITS - numFracShift + compScale.second;
303
0
  int addX  = (1 << (posShiftX - 1)) + (beforeScaleLeftOffset << SCALE_RATIO_BITS) + ((int( 1 - horCollocatedPositionFlag ) * 8 * (scalingRatio.first - SCALE_1X.first) + (1 << (2 + compScale.first))) >> (3 + compScale.first));
304
0
  int addY  = (1 << (posShiftY - 1)) + (beforeScaleTopOffset << SCALE_RATIO_BITS) + ((int( 1 - verCollocatedPositionFlag ) * 8 * (scalingRatio.second - SCALE_1X.second) + (1 << (2 + compScale.second))) >> (3 + compScale.second));
305
306
0
  const int filterLength = useLumaFilter ? NTAPS_LUMA : NTAPS_CHROMA;
307
0
  const int log2Norm     = 12;
308
309
0
  CHECK( bitDepth > 17, "Overflow may happen!" );
310
311
0
  const int maxVal = (1 << bitDepth) - 1;
312
0
  std::unique_ptr<int[]> buf( new int[orgHeight * scaledWidth] );
313
314
0
  for( int i = 0; i < scaledWidth; i++ )
315
0
  {
316
0
    const Pel* org = orgSrc;
317
0
    int refPos = int( ((int64_t(i << compScale.first) - afterScaleLeftOffset) * scalingRatio.first + addX) >> posShiftX );
318
0
    int integer = refPos >> numFracShift;
319
0
    int frac = refPos & numFracPositions;
320
0
    int* tmp = buf.get() + i;
321
322
0
    for( int j = 0; j < orgHeight; j++ )
323
0
    {
324
0
      int sum = 0;
325
0
      const TFilterCoeff* f = filterHor + frac * filterLength;
326
327
0
      for( int k = 0; k < filterLength; k++ )
328
0
      {
329
0
        int xInt = std::min<int>( std::max( 0, integer + k - filterLength / 2 + 1 ), orgWidth - 1 );
330
0
        sum += f[k] * org[xInt]; // postpone horizontal filtering gain removal after vertical filtering
331
0
      }
332
333
0
      *tmp = sum;
334
335
0
      tmp += scaledWidth;
336
0
      org += orgStride;
337
0
    }
338
0
  }
339
340
0
  Pel* dst = scaledSrc;
341
342
0
  for( int j = 0; j < scaledHeight; j++ )
343
0
  {
344
0
    int refPos = int( ((int64_t(j << compScale.second) - afterScaleTopOffset) * scalingRatio.second + addY) >> posShiftY );
345
0
    int integer = refPos >> numFracShift;
346
0
    int frac = refPos & numFracPositions;
347
348
0
    for( int i = 0; i < scaledWidth; i++ )
349
0
    {
350
0
      int sum = 0;
351
0
      int* tmp = buf.get() + i;
352
0
      const TFilterCoeff* f = filterVer + frac * filterLength;
353
354
0
      for( int k = 0; k < filterLength; k++ )
355
0
      {
356
0
        int yInt = std::min<int>( std::max( 0, integer + k - filterLength / 2 + 1 ), orgHeight - 1 );
357
0
        sum += f[k] * tmp[yInt * scaledWidth];
358
0
      }
359
360
0
      dst[i] = std::min<int>( std::max( 0, (sum + (1 << (log2Norm - 1))) >> log2Norm ), maxVal );
361
0
    }
362
363
0
    dst += scaledStride;
364
0
  }
365
0
}
366
367
void rspFwdCore( Pel* ptr, ptrdiff_t ptrStride, int width, int height, const int bd, const Pel OrgCW, const Pel* LmcsPivot, const Pel* ScaleCoeff, const Pel* InputPivot )
368
0
{
369
0
  int idxY;
370
0
  int shift = getLog2( OrgCW );
371
372
  //    const auto rsp_sgnl_op  = [=, &dst]( int ADDR ){ idxY = ( dst[ADDR] >> shift ); dst[ADDR] = static_cast<Pel>( ClipBD<int>( LmcsPivot[idxY] + ( ( ScaleCoeff[idxY] * ( dst[ADDR] - InputPivot[idxY] ) + ( 1 << 10 ) ) >> 11 ), bd ) ); };
373
  //    const auto rsp_sgnl_inc = [=, &dst]            { dst += stride; };
374
375
  //    size_aware_pel_op( rsp_sgnl_op, rsp_sgnl_inc, width, height );
376
377
0
#define RSP_FWD_OP( ADDR ) { idxY = ( ptr[ADDR] >> shift ); ptr[ADDR] = static_cast<Pel>( ClipBD<int>( LmcsPivot[idxY] + ( ( ScaleCoeff[idxY] * ( ptr[ADDR] - InputPivot[idxY] ) + ( 1 << 10 ) ) >> 11 ), bd ) ); }
378
0
#define RSP_FWD_INC        ptr      += ptrStride;
379
380
0
  SIZE_AWARE_PER_EL_OP( RSP_FWD_OP, RSP_FWD_INC )
381
382
0
#undef RSP_FWD_OP
383
0
#undef RSP_FWD_INC
384
0
}
385
386
PelBufferOps::PelBufferOps()
387
14
{
388
14
  addAvg4  = addAvgCore<Pel>;
389
14
  addAvg8  = addAvgCore<Pel>;
390
14
  addAvg16 = addAvgCore<Pel>;
391
392
14
  reco4 = reconstructCore<Pel>;
393
14
  reco8 = reconstructCore<Pel>;
394
395
14
  linTf4 = linTfCore<Pel>;
396
14
  linTf8 = linTfCore<Pel>;
397
398
14
  wghtAvg4 = addWeightedAvgCore<Pel>;
399
14
  wghtAvg8 = addWeightedAvgCore<Pel>;
400
401
14
  copyBuffer = copyBufferCore;
402
403
14
  transpose4x4 = transpose4x4Core<Pel>;
404
14
  transpose8x8 = transpose8x8Core<Pel>;
405
406
14
  scaleSignal = scaleSignalCore;
407
408
14
  applyLut = applyLutCore;
409
14
  rspFwd   = rspFwdCore;
410
14
  rspBcw   = nullptr;
411
412
14
  fillN_CU         = fillN_CuCore;
413
14
  fillN_MotionInfo = fillN_MotionInfoCore;
414
415
14
  sampleRateConv = sampleRateConvCore;
416
14
}
417
418
PelBufferOps g_pelBufOP = PelBufferOps();
419
420
template<>
421
void AreaBuf<Pel>::addWeightedAvg(const AreaBuf<const Pel> &other1, const AreaBuf<const Pel> &other2, const ClpRng& clpRng, const int8_t bcwIdx)
422
0
{
423
0
  const int8_t w0 = getBcwWeight(bcwIdx, REF_PIC_LIST_0);
424
0
  const int8_t w1 = getBcwWeight(bcwIdx, REF_PIC_LIST_1);
425
0
  const int8_t log2WeightBase = g_BcwLog2WeightBase;
426
0
  const Pel* src0 = other1.buf;
427
0
  const Pel* src2 = other2.buf;
428
0
  Pel* dest = buf;
429
430
0
  const ptrdiff_t src1Stride = other1.stride;
431
0
  const ptrdiff_t src2Stride = other2.stride;
432
0
  const ptrdiff_t destStride = stride;
433
0
  const int clipbd    = clpRng.bd;
434
0
  const int shiftNum  = std::max<int>( 2, ( IF_INTERNAL_PREC - clipbd ) ) + log2WeightBase;
435
0
  const int offset    = ( 1 << ( shiftNum - 1 ) ) + ( IF_INTERNAL_OFFS << log2WeightBase );
436
437
0
  if( ( width & 7 ) == 0 )
438
0
  {
439
0
    g_pelBufOP.wghtAvg8( src0, src1Stride, src2, src2Stride, dest, destStride, width, height, shiftNum, offset, w0, w1, clpRng );
440
0
  }
441
0
  else if( ( width & 3 ) == 0 )
442
0
  {
443
0
    g_pelBufOP.wghtAvg4( src0, src1Stride, src2, src2Stride, dest, destStride, width, height, shiftNum, offset, w0, w1, clpRng );
444
0
  }
445
0
  else
446
0
  {
447
0
#define ADD_AVG_OP( ADDR ) dest[ADDR] = ClipPel( rightShift( ( src0[ADDR]*w0 + src2[ADDR]*w1 + offset ), shiftNum ), clpRng )
448
0
#define ADD_AVG_INC     \
449
0
    src0 += src1Stride; \
450
0
    src2 += src2Stride; \
451
0
    dest += destStride; \
452
0
453
0
    SIZE_AWARE_PER_EL_OP( ADD_AVG_OP, ADD_AVG_INC );
454
455
0
#undef ADD_AVG_OP
456
0
#undef ADD_AVG_INC
457
0
  }
458
0
}
459
460
template<>
461
void AreaBuf<Pel>::scaleSignal(const int scale, const ClpRng& clpRng)
462
0
{
463
0
  g_pelBufOP.scaleSignal( buf, stride, width, height, scale, clpRng.bd );
464
0
}
465
466
template<>
467
void AreaBuf<Pel>::addAvg( const AreaBuf<const Pel> &other1, const AreaBuf<const Pel> &other2, const ClpRng& clpRng)
468
0
{
469
0
  const Pel* src0 = other1.buf;
470
0
  const Pel* src2 = other2.buf;
471
0
        Pel* dest =        buf;
472
473
0
  const ptrdiff_t src1Stride = other1.stride;
474
0
  const ptrdiff_t src2Stride = other2.stride;
475
0
  const ptrdiff_t destStride =        stride;
476
0
  const int       clipbd     = clpRng.bd;
477
0
  const int       shiftNum   = std::max<int>(2, (IF_INTERNAL_PREC - clipbd)) + 1;
478
0
  const int       offset     = (1 << (shiftNum - 1)) + 2 * IF_INTERNAL_OFFS;
479
480
0
  if( ( width & 15 ) == 0 )
481
0
  {
482
0
    g_pelBufOP.addAvg16( src0, src1Stride, src2, src2Stride, dest, destStride, width, height, shiftNum, offset, clpRng );
483
0
  }
484
0
  else if( ( width & 7 ) == 0 )
485
0
  {
486
0
    g_pelBufOP.addAvg8( src0, src1Stride, src2, src2Stride, dest, destStride, width, height, shiftNum, offset, clpRng );
487
0
  }
488
0
  else if( ( width & 3 ) == 0 )
489
0
  {
490
0
    g_pelBufOP.addAvg4( src0, src1Stride, src2, src2Stride, dest, destStride, width, height, shiftNum, offset, clpRng );
491
0
  }
492
0
  else
493
0
  {
494
0
#define ADD_AVG_OP( ADDR ) dest[ADDR] = ClipPel( rightShift( ( src0[ADDR] + src2[ADDR] + offset ), shiftNum ), clpRng )
495
0
#define ADD_AVG_INC     \
496
0
    src0 += src1Stride; \
497
0
    src2 += src2Stride; \
498
0
    dest += destStride; \
499
0
500
0
    SIZE_AWARE_PER_EL_OP( ADD_AVG_OP, ADD_AVG_INC );
501
502
0
#undef ADD_AVG_OP
503
0
#undef ADD_AVG_INC
504
0
  }
505
0
}
506
507
template<>
508
void AreaBuf<Pel>::reconstruct( const AreaBuf<const Pel> &pred, const AreaBuf<const Pel> &resi, const ClpRng& clpRng )
509
0
{
510
0
  const Pel* src1 = pred.buf;
511
0
  const Pel* src2 = resi.buf;
512
0
        Pel* dest =      buf;
513
514
0
  const ptrdiff_t src1Stride = pred.stride;
515
0
  const ptrdiff_t src2Stride = resi.stride;
516
0
  const ptrdiff_t destStride =      stride;
517
518
0
  if( ( width & 7 ) == 0 )
519
0
  {
520
0
    g_pelBufOP.reco8( src1, src1Stride, src2, src2Stride, dest, destStride, width, height, clpRng );
521
0
  }
522
0
  else if( ( width & 3 ) == 0 )
523
0
  {
524
0
    g_pelBufOP.reco4( src1, src1Stride, src2, src2Stride, dest, destStride, width, height, clpRng );
525
0
  }
526
0
  else
527
0
  {
528
0
#define RECO_OP( ADDR ) dest[ADDR] = ClipPel( src1[ADDR] + src2[ADDR], clpRng )
529
0
#define RECO_INC        \
530
0
    src1 += src1Stride; \
531
0
    src2 += src2Stride; \
532
0
    dest += destStride; \
533
0
534
0
    SIZE_AWARE_PER_EL_OP( RECO_OP, RECO_INC );
535
536
0
#undef RECO_OP
537
0
#undef RECO_INC
538
0
  }
539
0
}
540
541
template<>
542
void AreaBuf<Pel>::linearTransform( const int scale, const int shift, const int offset, bool bClip, const ClpRng& clpRng )
543
0
{
544
0
  const Pel* src = buf;
545
0
        Pel* dst = buf;
546
547
0
  if( width == 1 )
548
0
  {
549
0
    THROW_FATAL( "Blocks of width = 1 not supported" );
550
0
  }
551
0
  else if( ( width & 7 ) == 0 )
552
0
  {
553
0
    g_pelBufOP.linTf8( src, stride, dst, stride, width, height, scale, shift, offset, clpRng, bClip );
554
0
  }
555
0
  else if( ( width & 3 ) == 0 )
556
0
  {
557
0
    g_pelBufOP.linTf4( src, stride, dst, stride, width, height, scale, shift, offset, clpRng, bClip );
558
0
  }
559
0
  else
560
0
  {
561
0
#define LINTF_OP( ADDR ) dst[ADDR] = ( Pel ) bClip ? ClipPel( rightShift( scale * src[ADDR], shift ) + offset, clpRng ) : ( rightShift( scale * src[ADDR], shift ) + offset )
562
0
#define LINTF_INC        \
563
0
    src += stride;       \
564
0
    dst += stride;       \
565
0
566
0
    SIZE_AWARE_PER_EL_OP( LINTF_OP, LINTF_INC );
567
568
0
#undef RECO_OP
569
0
#undef RECO_INC
570
0
  }
571
0
}
572
573
#if ENABLE_SIMD_OPT_BUFFER && defined(TARGET_SIMD_X86)
574
template<>
575
void AreaBuf<Pel>::transposedFrom( const AreaBuf<const Pel> &other )
576
{
577
  CHECK( width != other.height || height != other.width, "Incompatible size" );
578
579
  if( ( ( width | height ) & 7 ) == 0 )
580
  {
581
    const Pel* src = other.buf;
582
583
    for( unsigned y = 0; y < other.height; y += 8 )
584
    {
585
      Pel* dst = buf + y;
586
587
      for( unsigned x = 0; x < other.width; x += 8 )
588
      {
589
        g_pelBufOP.transpose8x8( &src[x], other.stride, dst, stride );
590
591
        dst += 8 * stride;
592
      }
593
594
      src += 8 * other.stride;
595
    }
596
  }
597
  else if( ( ( width | height ) & 3 ) == 0 )
598
  {
599
    const Pel* src = other.buf;
600
601
    for( unsigned y = 0; y < other.height; y += 4 )
602
    {
603
      Pel* dst = buf + y;
604
605
      for( unsigned x = 0; x < other.width; x += 4 )
606
      {
607
        g_pelBufOP.transpose4x4( &src[x], other.stride, dst, stride );
608
609
        dst += 4 * stride;
610
      }
611
612
      src += 4 * other.stride;
613
    }
614
  }
615
  else
616
  {
617
          Pel* dst =       buf;
618
    const Pel* src = other.buf;
619
    width          = other.height;
620
    height         = other.width;
621
    stride         = stride < width ? width : stride;
622
623
    for( unsigned y = 0; y < other.height; y++ )
624
    {
625
      for( unsigned x = 0; x < other.width; x++ )
626
      {
627
        dst[y + x*stride] = src[x + y * other.stride];
628
      }
629
    }
630
  }
631
}
632
#endif
633
634
template<>
635
void AreaBuf<MotionInfo>::fill( const MotionInfo& val )
636
0
{
637
0
  g_pelBufOP.fillN_MotionInfo( buf, stride, width, height, val );
638
0
}
639
640
PelStorage::PelStorage()
641
3.54k
{
642
14.1k
  for( uint32_t i = 0; i < MAX_NUM_COMPONENT; i++ )
643
10.6k
  {
644
10.6k
    m_origin[i]        = nullptr;
645
10.6k
    m_allocator[i]     = nullptr;
646
10.6k
  }
647
3.54k
}
648
649
PelStorage::~PelStorage()
650
3.54k
{
651
3.54k
  try
652
3.54k
  {
653
3.54k
    destroy();
654
3.54k
  }
655
3.54k
  catch( Exception& e )   // this is a destructor, so we don't throw here
656
3.54k
  {
657
0
    msg( ERROR, "PelStorage::~PelStorage: caught exception while releasing the buffers: %s\n", e.what() );
658
0
  }
659
3.54k
  catch( ... )
660
3.54k
  {
661
0
    msg( ERROR, "PelStorage::~PelStorage: caught unknown exception while releasing the buffers\n" );
662
0
  }
663
3.54k
}
664
665
void PelStorage::create( const UnitArea &_UnitArea )
666
0
{
667
0
  create( _UnitArea.chromaFormat, _UnitArea.blocks[0] );
668
0
}
669
670
PelStorage::CompBufSize PelStorage::calcCompBufSize( const Size& _size, const unsigned _maxCUSize, const unsigned _margin, const unsigned _alignmentByte, const bool _scaleChromaMargin, const unsigned scaleX, const unsigned scaleY )
671
200
{
672
200
  uint64_t extHeight = _size.height;
673
200
  uint64_t extWidth  = _size.width;
674
675
200
  if( _maxCUSize )
676
200
  {
677
200
    extHeight = ( ( extHeight + _maxCUSize - 1 ) / _maxCUSize ) * _maxCUSize;
678
200
    extWidth  = ( ( extWidth  + _maxCUSize - 1 ) / _maxCUSize ) * _maxCUSize;
679
200
  }
680
681
200
  const unsigned _alignment = _alignmentByte / sizeof( Pel );
682
683
200
  unsigned ymargin = _margin >> (_scaleChromaMargin ? scaleY : 0);
684
200
  unsigned xmargin = _margin >> (_scaleChromaMargin ? scaleX : 0);
685
686
200
  if( _alignment && xmargin )
687
200
  {
688
200
    xmargin = ( ( xmargin + _alignment - 1 ) / _alignment ) * _alignment;
689
200
  }
690
691
200
  uint64_t totalWidth  = ( extWidth  >> scaleX ) + 2 * xmargin;
692
200
  uint64_t totalHeight = ( extHeight >> scaleY ) + 2 * ymargin;
693
694
200
  if( _alignment )
695
200
  {
696
    // make sure buffer lines are align
697
200
    totalWidth = ( ( totalWidth + _alignment - 1 ) / _alignment ) * _alignment;
698
200
  }
699
700
200
#if ENABLE_SIMD_OPT_INTER
701
200
  const uint64_t area = totalWidth * totalHeight + 1; // +1 for the extra Pel overread in prefetchPad_SSE, in case reading from the very bottom right of the picture
702
#else
703
  const uint64_t area = totalWidth * totalHeight;
704
#endif
705
  // buffer sizes and offsets are computed in 32 bit
706
200
  CHECK( area > INT32_MAX / sizeof( Pel ), "Buffer size " << _size.width << "x" << _size.height << " too large" );
707
708
199
  return { Size{ SizeType( totalWidth ), SizeType( totalHeight ) }, xmargin, ymargin, size_t( area ) };
709
200
}
710
711
void PelStorage::create( const ChromaFormat _chromaFormat, const Size& _size, const unsigned _maxCUSize, const unsigned _margin, const unsigned _alignmentByte, const bool _scaleChromaMargin, const UserAllocator* userAlloc )
712
0
{
713
0
  CHECK( !bufs.empty(), "Trying to re-create an already initialized buffer" );
714
0
  CHECK( _alignmentByte && _alignmentByte != MEMORY_ALIGN_DEF_SIZE, "Unsupported alignment" );
715
716
0
  chromaFormat = _chromaFormat;
717
718
0
  const uint32_t numCh = getNumberValidComponents( _chromaFormat );
719
720
0
  for( uint32_t i = 0; i < numCh; i++ )
721
0
  {
722
0
    const ComponentID compID = ComponentID( i );
723
0
    const unsigned scaleX = getComponentScaleX( compID, _chromaFormat );
724
0
    const unsigned scaleY = getComponentScaleY( compID, _chromaFormat );
725
726
0
    const CompBufSize bufSize = calcCompBufSize( _size, _maxCUSize, _margin, _alignmentByte, _scaleChromaMargin, scaleX, scaleY );
727
0
    CHECK( !bufSize.area, "Trying to create a buffer with zero area" );
728
729
0
    m_origSi[i] = bufSize.total;
730
0
    if( userAlloc && userAlloc->enabled )
731
0
    {
732
0
      m_origin[i] = ( Pel* ) userAlloc->create( userAlloc->opaque, (vvdecComponentType)i, uint32_t( sizeof(Pel)*bufSize.area ), MEMORY_ALIGN_DEF_SIZE, &m_allocator[i] );
733
0
      CHECK( m_origin[i] == nullptr, "external allocator callback failed (returned NULL)." );
734
0
      m_externAllocator = true;
735
0
      m_userAlloc       = userAlloc;
736
0
    }
737
0
    else
738
0
    {
739
0
      m_origin[i] = ( Pel* ) xMalloc( Pel, bufSize.area );
740
0
    }
741
0
    Pel* topLeft = m_origin[i] + bufSize.total.width * bufSize.ymargin + bufSize.xmargin;
742
0
    bufs.push_back( PelBuf( topLeft, bufSize.total.width, _size.width >> scaleX, _size.height >> scaleY ) );
743
0
  }
744
0
}
745
746
void PelStorage::createFromBuf( PelUnitBuf buf )
747
0
{
748
0
  chromaFormat = buf.chromaFormat;
749
750
0
  const uint32_t numCh = getNumberValidComponents( chromaFormat );
751
752
0
  bufs.resize(numCh);
753
754
0
  for( uint32_t i = 0; i < numCh; i++ )
755
0
  {
756
0
    PelBuf cPelBuf = buf.get( ComponentID( i ) );
757
0
    bufs[i] = PelBuf( cPelBuf.bufAt( 0, 0 ), cPelBuf.stride, cPelBuf.width, cPelBuf.height );
758
0
  }
759
0
}
760
761
void PelStorage::swap( PelStorage& other )
762
0
{
763
0
  const uint32_t numCh = getNumberValidComponents( chromaFormat );
764
765
0
  for( uint32_t i = 0; i < numCh; i++ )
766
0
  {
767
    // check this otherwise it would turn out to get very weird
768
0
    CHECK( chromaFormat                   != other.chromaFormat                  , "Incompatible formats" );
769
0
    CHECK( get( ComponentID( i ) )        != other.get( ComponentID( i ) )       , "Incompatible formats" );
770
0
    CHECK( get( ComponentID( i ) ).stride != other.get( ComponentID( i ) ).stride, "Incompatible formats" );
771
772
0
    std::swap( bufs[i].buf,    other.bufs[i].buf );
773
0
    std::swap( bufs[i].stride, other.bufs[i].stride );
774
0
    std::swap( m_origin[i],    other.m_origin[i] );
775
0
    std::swap( m_allocator[i], other.m_allocator[i] );
776
0
  }
777
0
  std::swap( m_externAllocator, other.m_externAllocator );
778
0
  std::swap( m_userAlloc,       other.m_userAlloc );
779
0
}
780
781
void PelStorage::destroy()
782
3.54k
{
783
3.54k
  chromaFormat = NUM_CHROMA_FORMAT;
784
14.1k
  for( uint32_t i = 0; i < MAX_NUM_COMPONENT; i++ )
785
10.6k
  {
786
10.6k
    if( m_origin[i] )
787
0
    {
788
0
      if ( !m_externAllocator )
789
0
      {
790
0
        xFree( m_origin[i] );
791
0
      }
792
0
      else if( m_allocator[i])
793
0
      {
794
0
        CHECK( m_userAlloc->unref == nullptr, "vvdecUnrefBufferCallback not valid, cannot unref picture buffer" )
795
0
        m_userAlloc->unref( m_userAlloc->opaque, m_allocator[i] );
796
0
      }
797
0
      m_origin[i] = nullptr;
798
0
    }
799
10.6k
  }
800
  // clear() keeps the elements, which would still point into the freed planes
801
3.54k
  for( auto& buf: bufs )
802
0
  {
803
0
    buf = PelBuf();
804
0
  }
805
3.54k
  bufs.clear();
806
3.54k
}
807
808
PelBuf PelStorage::getBuf( const ComponentID CompID )
809
0
{
810
0
  return bufs[CompID];
811
0
}
812
813
const CPelBuf PelStorage::getBuf( const ComponentID CompID ) const
814
0
{
815
0
  return bufs[CompID];
816
0
}
817
818
PelBuf PelStorage::getBuf( const CompArea &blk )
819
0
{
820
0
  const PelBuf& r = bufs[blk.compID()];
821
822
0
  CHECKD( rsAddr( blk.bottomRight(), r.stride ) >= ( ( r.height - 1 ) * r.stride + r.width ), "Trying to access a buf outside of bound!" );
823
824
0
  return PelBuf( r.buf + rsAddr( blk, r.stride ), r.stride, blk );
825
0
}
826
827
const CPelBuf PelStorage::getBuf( const CompArea &blk ) const
828
0
{
829
0
  const PelBuf& r = bufs[blk.compID()];
830
0
  return CPelBuf( r.buf + rsAddr( blk, r.stride ), r.stride, blk );
831
0
}
832
833
PelUnitBuf PelStorage::getBuf( const UnitArea &unit )
834
0
{
835
0
  return ( chromaFormat == CHROMA_400 ) ? PelUnitBuf( chromaFormat, getBuf( unit.Y() ) ) : PelUnitBuf( chromaFormat, getBuf( unit.Y() ), getBuf( unit.Cb() ), getBuf( unit.Cr() ) );
836
0
}
837
838
const CPelUnitBuf PelStorage::getBuf( const UnitArea &unit ) const
839
0
{
840
0
  return ( chromaFormat == CHROMA_400 ) ? CPelUnitBuf( chromaFormat, getBuf( unit.Y() ) ) : CPelUnitBuf( chromaFormat, getBuf( unit.Y() ), getBuf( unit.Cb() ), getBuf( unit.Cr() ) );
841
0
}
842
843
template<>
844
void UnitBuf<Pel>::colorSpaceConvert( const UnitBuf<Pel> &other, const ClpRng& clpRng )
845
0
{
846
0
  const Pel* pOrg0 = bufs[COMPONENT_Y ].buf;
847
0
  const Pel* pOrg1 = bufs[COMPONENT_Cb].buf;
848
0
  const Pel* pOrg2 = bufs[COMPONENT_Cr].buf;
849
0
  const ptrdiff_t strideOrg = bufs[COMPONENT_Y ].stride;
850
851
0
  Pel* pDst0 = other.bufs[COMPONENT_Y ].buf;
852
0
  Pel* pDst1 = other.bufs[COMPONENT_Cb].buf;
853
0
  Pel* pDst2 = other.bufs[COMPONENT_Cr].buf;
854
0
  const ptrdiff_t strideDst = other.bufs[COMPONENT_Y ].stride;
855
856
0
  int width  = bufs[COMPONENT_Y].width;
857
0
  int height = bufs[COMPONENT_Y].height;
858
0
  int maxAbsclipBD = (1 << (clpRng.bd + 1)) - 1;
859
0
  int y0, cg, co;
860
861
0
  CHECKD( bufs[COMPONENT_Y].stride != bufs[COMPONENT_Cb].stride || bufs[COMPONENT_Y].stride != bufs[COMPONENT_Cr].stride, "unequal stride for 444 content" );
862
0
  CHECKD( other.bufs[COMPONENT_Y].stride != other.bufs[COMPONENT_Cb].stride || other.bufs[COMPONENT_Y].stride != other.bufs[COMPONENT_Cr].stride, "unequal stride for 444 content" );
863
0
  CHECKD( bufs[COMPONENT_Y].width != other.bufs[COMPONENT_Y].width || bufs[COMPONENT_Y].height != other.bufs[COMPONENT_Y].height, "unequal block size" );
864
865
0
  for( int y = 0; y < height; y++ )
866
0
  {
867
0
    for( int x = 0; x < width; x++ )
868
0
    {
869
0
      y0 = pOrg0[x];
870
0
      cg = pOrg1[x];
871
0
      co = pOrg2[x];
872
873
0
      y0 = Clip3((-maxAbsclipBD - 1), maxAbsclipBD, y0);
874
0
      cg = Clip3((-maxAbsclipBD - 1), maxAbsclipBD, cg);
875
0
      co = Clip3((-maxAbsclipBD - 1), maxAbsclipBD, co);
876
877
0
      int t = y0 - (cg >> 1);
878
0
      pDst0[x] = cg + t;
879
0
      pDst1[x] = t - (co >> 1);
880
0
      pDst2[x] = co + pDst1[x];
881
0
    }
882
883
0
    pOrg0 += strideOrg;
884
0
    pOrg1 += strideOrg;
885
0
    pOrg2 += strideOrg;
886
0
    pDst0 += strideDst;
887
0
    pDst1 += strideDst;
888
0
    pDst2 += strideDst;
889
0
  }
890
0
}
891
892
template void UnitBuf<Pel>::writeToFile( std::string filename ) const;
893
894
template<typename T>
895
void UnitBuf<T>::writeToFile( std::string filename ) const
896
0
{
897
0
  FILE* f = fopen( filename.c_str(), "w" );
898
0
  CHECK_FATAL( f == nullptr, "writeToFile() cannot open file for writing" )
899
900
0
  for( auto& b: bufs )
901
0
  {
902
0
    for( unsigned y = 0; y < b.height; y++ )
903
0
    {
904
0
      fwrite( b.bufAt( 0, y ), sizeof( T ), b.width, f );
905
0
    }
906
0
  }
907
908
0
  fclose( f );
909
0
}
910
911
}