Coverage Report

Created: 2026-10-17 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvenc/source/Lib/CommonLib/SampleAdaptiveOffset.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) 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
43
44
/** \file     SampleAdaptiveOffset.cpp
45
    \brief    sample adaptive offset class
46
*/
47
48
#include "SampleAdaptiveOffset.h"
49
#include "UnitTools.h"
50
#include "UnitPartitioner.h"
51
#include "CodingStructure.h"
52
#include "dtrace_codingstruct.h"
53
#include "dtrace_buffer.h"
54
55
#include <string.h>
56
#include <stdlib.h>
57
#include <math.h>
58
59
//! \ingroup CommonLib
60
//! \{
61
62
namespace vvenc {
63
64
void offsetBlock_core(const int channelBitDepth, const ClpRng& clpRng, int typeIdx, int* offset, int startIdx, 
65
                      const Pel* srcBlk, Pel* resBlk, ptrdiff_t srcStride, ptrdiff_t resStride, int width, int height, 
66
                      uint8_t availMask, std::vector<int8_t> &signLineBuf1, std::vector<int8_t> &signLineBuf2)
67
32
{
68
32
  int x, y, startX, startY, endX, endY, edgeType;
69
32
  int firstLineStartX, firstLineEndX, lastLineStartX, lastLineEndX;
70
32
  int8_t signLeft, signRight, signDown;
71
72
32
  const Pel* srcLine = srcBlk;
73
32
  Pel* resLine = resBlk;
74
75
32
  switch (typeIdx)
76
32
  {
77
0
  case SAO_TYPE_EO_0:
78
0
  {
79
0
    offset += 2;
80
0
    startX = availMask&LeftAvail ? 0 : 1;
81
0
    endX = availMask&RightAvail ? width : (width - 1);
82
0
    for (y = 0; y < height; y++)
83
0
    {
84
0
      signLeft = (int8_t)sgn(srcLine[startX] - srcLine[startX - 1]);
85
0
      for (x = startX; x < endX; x++)
86
0
      {
87
0
        signRight = (int8_t)sgn(srcLine[x] - srcLine[x + 1]);
88
0
        edgeType = signRight + signLeft;
89
0
        signLeft = -signRight;
90
91
0
        resLine[x] = ClipPel<int>(srcLine[x] + offset[edgeType], clpRng);
92
0
      }
93
0
      srcLine += srcStride;
94
0
      resLine += resStride;
95
0
    }
96
97
0
  }
98
0
  break;
99
0
  case SAO_TYPE_EO_90:
100
0
  {
101
0
    offset += 2;
102
0
    int8_t *signUpLine = &signLineBuf1[0];
103
104
0
    startY = availMask&AboveAvail ? 0 : 1;
105
0
    endY = availMask&BelowAvail ? height : height - 1;
106
0
    if (!(availMask&AboveAvail))
107
0
    {
108
0
      srcLine += srcStride;
109
0
      resLine += resStride;
110
0
    }
111
112
0
    const Pel* srcLineAbove = srcLine - srcStride;
113
0
    for (x = 0; x < width; x++)
114
0
    {
115
0
      signUpLine[x] = (int8_t)sgn(srcLine[x] - srcLineAbove[x]);
116
0
    }
117
118
0
    const Pel* srcLineBelow;
119
0
    for (y = startY; y < endY; y++)
120
0
    {
121
0
      srcLineBelow = srcLine + srcStride;
122
123
0
      for (x = 0; x < width; x++)
124
0
      {
125
0
        signDown = (int8_t)sgn(srcLine[x] - srcLineBelow[x]);
126
0
        edgeType = signDown + signUpLine[x];
127
0
        signUpLine[x] = -signDown;
128
129
0
        resLine[x] = ClipPel<int>(srcLine[x] + offset[edgeType], clpRng);
130
0
      }
131
0
      srcLine += srcStride;
132
0
      resLine += resStride;
133
0
    }
134
135
0
  }
136
0
  break;
137
0
  case SAO_TYPE_EO_135:
138
0
  {
139
0
    offset += 2;
140
0
    int8_t *signUpLine, *signDownLine, *signTmpLine;
141
142
0
    signUpLine = &signLineBuf1[0];
143
0
    signDownLine = &signLineBuf2[0];
144
145
0
    startX = availMask&LeftAvail ? 0 : 1;
146
0
    endX = availMask&RightAvail ? width : (width - 1);
147
148
    //prepare 2nd line's upper sign
149
0
    const Pel* srcLineBelow = srcLine + srcStride;
150
0
    for (x = startX; x < endX + 1; x++)
151
0
    {
152
0
      signUpLine[x] = (int8_t)sgn(srcLineBelow[x] - srcLine[x - 1]);
153
0
    }
154
155
    //1st line
156
0
    const Pel* srcLineAbove = srcLine - srcStride;
157
0
    firstLineStartX = availMask&AboveLeftAvail ? 0 : 1;
158
0
    firstLineEndX = availMask&AboveAvail ? endX : 1;
159
0
    for (x = firstLineStartX; x < firstLineEndX; x++)
160
0
    {
161
0
      edgeType = sgn(srcLine[x] - srcLineAbove[x - 1]) - signUpLine[x + 1];
162
163
0
      resLine[x] = ClipPel<int>(srcLine[x] + offset[edgeType], clpRng);
164
0
    }
165
0
    srcLine += srcStride;
166
0
    resLine += resStride;
167
168
169
    //middle lines
170
0
    for (y = 1; y < height - 1; y++)
171
0
    {
172
0
      srcLineBelow = srcLine + srcStride;
173
174
0
      for (x = startX; x < endX; x++)
175
0
      {
176
0
        signDown = (int8_t)sgn(srcLine[x] - srcLineBelow[x + 1]);
177
0
        edgeType = signDown + signUpLine[x];
178
0
        resLine[x] = ClipPel<int>(srcLine[x] + offset[edgeType], clpRng);
179
180
0
        signDownLine[x + 1] = -signDown;
181
0
      }
182
0
      signDownLine[startX] = (int8_t)sgn(srcLineBelow[startX] - srcLine[startX - 1]);
183
184
0
      signTmpLine = signUpLine;
185
0
      signUpLine = signDownLine;
186
0
      signDownLine = signTmpLine;
187
188
0
      srcLine += srcStride;
189
0
      resLine += resStride;
190
0
    }
191
192
    //last line
193
0
    srcLineBelow = srcLine + srcStride;
194
0
    lastLineStartX = availMask&BelowAvail ? startX : (width - 1);
195
0
    lastLineEndX = availMask&BelowRightAvail ? width : (width - 1);
196
0
    for (x = lastLineStartX; x < lastLineEndX; x++)
197
0
    {
198
0
      edgeType = sgn(srcLine[x] - srcLineBelow[x + 1]) + signUpLine[x];
199
0
      resLine[x] = ClipPel<int>(srcLine[x] + offset[edgeType], clpRng);
200
201
0
    }
202
0
  }
203
0
  break;
204
0
  case SAO_TYPE_EO_45:
205
0
  {
206
0
    offset += 2;
207
0
    int8_t *signUpLine = &signLineBuf1[1];
208
209
0
    startX = availMask&LeftAvail ? 0 : 1;
210
0
    endX = availMask&RightAvail ? width : (width - 1);
211
212
    //prepare 2nd line upper sign
213
0
    const Pel* srcLineBelow = srcLine + srcStride;
214
0
    for (x = startX - 1; x < endX; x++)
215
0
    {
216
0
      signUpLine[x] = (int8_t)sgn(srcLineBelow[x] - srcLine[x + 1]);
217
0
    }
218
219
220
    //first line
221
0
    const Pel* srcLineAbove = srcLine - srcStride;
222
0
    firstLineStartX = availMask&AboveAvail ? startX : (width - 1);
223
0
    firstLineEndX = availMask&AboveRightAvail ? width : (width - 1);
224
0
    for (x = firstLineStartX; x < firstLineEndX; x++)
225
0
    {
226
0
      edgeType = sgn(srcLine[x] - srcLineAbove[x + 1]) - signUpLine[x - 1];
227
0
      resLine[x] = ClipPel<int>(srcLine[x] + offset[edgeType], clpRng);
228
0
    }
229
0
    srcLine += srcStride;
230
0
    resLine += resStride;
231
232
    //middle lines
233
0
    for (y = 1; y < height - 1; y++)
234
0
    {
235
0
      srcLineBelow = srcLine + srcStride;
236
237
0
      for (x = startX; x < endX; x++)
238
0
      {
239
0
        signDown = (int8_t)sgn(srcLine[x] - srcLineBelow[x - 1]);
240
0
        edgeType = signDown + signUpLine[x];
241
0
        resLine[x] = ClipPel<int>(srcLine[x] + offset[edgeType], clpRng);
242
0
        signUpLine[x - 1] = -signDown;
243
0
      }
244
0
      signUpLine[endX - 1] = (int8_t)sgn(srcLineBelow[endX - 1] - srcLine[endX]);
245
0
      srcLine += srcStride;
246
0
      resLine += resStride;
247
0
    }
248
249
    //last line
250
0
    srcLineBelow = srcLine + srcStride;
251
0
    lastLineStartX = availMask&BelowLeftAvail ? 0 : 1;
252
0
    lastLineEndX = availMask&BelowAvail ? endX : 1;
253
0
    for (x = lastLineStartX; x < lastLineEndX; x++)
254
0
    {
255
0
      edgeType = sgn(srcLine[x] - srcLineBelow[x - 1]) + signUpLine[x];
256
0
      resLine[x] = ClipPel<int>(srcLine[x] + offset[edgeType], clpRng);
257
258
0
    }
259
0
  }
260
0
  break;
261
32
  case SAO_TYPE_BO:
262
32
  {
263
32
    const int shiftBits = channelBitDepth - NUM_SAO_BO_CLASSES_LOG2;
264
2.35k
    for (y = 0; y < height; y++)
265
2.32k
    {
266
148k
      for (x = 0; x < width; x++)
267
146k
      {
268
146k
        resLine[x] = ClipPel<int>(srcLine[x] + offset[srcLine[x] >> shiftBits], clpRng);
269
146k
      }
270
2.32k
      srcLine += srcStride;
271
2.32k
      resLine += resStride;
272
2.32k
    }
273
32
  }
274
32
  break;
275
0
  default:
276
0
  {
277
0
    THROW("Not a supported SAO types\n");
278
0
  }
279
32
  }
280
32
}
281
void calcSaoStatisticsEo0_Core(int width,int startX,int endX,int endY,Pel*  srcLine,Pel*  orgLine,int srcStride,int orgStride,int64_t  *count, int64_t *diff)
282
10.5k
{
283
10.5k
  int iNaRight=width-endX;
284
10.5k
  srcLine      = srcLine + startX;
285
10.5k
  orgLine      =orgLine + startX;
286
10.5k
  int iNaWidth = startX + iNaRight;
287
10.5k
  int i,j;
288
10.5k
  diff +=2;
289
10.5k
  count+=2;
290
608k
  for ( i = 0; i < endY; i++ )
291
598k
  {
292
598k
    int iSignLeft = sgn( *srcLine - *(srcLine - 1) );
293
37.9M
    for ( j = 0; j < width - iNaWidth; j++, srcLine++, orgLine++ )
294
37.3M
    {
295
37.3M
      int iSignRight       = sgn( *srcLine - *(srcLine + 1) );
296
37.3M
      int iType            = iSignLeft + iSignRight;
297
37.3M
      iSignLeft            = -1 * iSignRight;
298
37.3M
      diff[iType]  += (*orgLine - *srcLine);
299
37.3M
      count[iType] += 1;
300
37.3M
    }
301
598k
    srcLine += srcStride - ( width - iNaWidth );
302
598k
    orgLine += orgStride - ( width - iNaWidth );
303
598k
  }
304
10.5k
}
305
void calcSaoStatisticsEo90_Core(int width,int endX,int startY,int endY,Pel*  srcLine,Pel*  orgLine,int srcStride,int orgStride,int64_t  *count, int64_t *diff,int8_t *signUpLine)
306
10.5k
{
307
10.5k
  diff +=2;
308
10.5k
  count+=2;
309
10.5k
  int x,y,edgeType;
310
10.5k
  Pel* srcLineAbove = srcLine - srcStride;
311
10.5k
  int8_t signDown;
312
593k
  for (x=0; x<endX; x++)
313
583k
  {
314
583k
    signUpLine[x] = (int8_t)sgn(srcLine[x] - srcLineAbove[x]);
315
583k
  }
316
10.5k
  Pel* srcLineBelow;
317
597k
  for (y=startY; y<endY; y++)
318
587k
  {
319
587k
    srcLineBelow = srcLine + srcStride;
320
38.0M
    for (x=0; x<endX; x++)
321
37.4M
    {
322
37.4M
      signDown  = (int8_t)sgn(srcLine[x] - srcLineBelow[x]);
323
37.4M
      edgeType  = signDown + signUpLine[x];
324
37.4M
      signUpLine[x]= -signDown;
325
37.4M
      diff [edgeType] += (orgLine[x] - srcLine[x]);
326
37.4M
      count[edgeType] ++;
327
37.4M
    }
328
587k
    srcLine += srcStride;
329
587k
    orgLine += orgStride;
330
587k
  }
331
10.5k
}
332
333
334
void calcSaoStatisticsEo135_Core(int width,int startX,int endX,int endY,Pel*  srcLine,Pel*  orgLine,int srcStride,int orgStride,int64_t  *count, int64_t *diff,int8_t *signUpLine,int8_t *signDownLine)
335
10.5k
{
336
10.5k
  int x,y,edgeType;
337
10.5k
  int8_t signDown;
338
10.5k
  int8_t *signTmpLine;
339
10.5k
  Pel* srcLineBelow = srcLine + srcStride;
340
  //middle lines
341
592k
   for (y=1; y<endY; y++)
342
582k
   {
343
582k
     srcLineBelow = srcLine + srcStride;
344
37.0M
     for (x=startX; x<endX; x++)
345
36.5M
     {
346
36.5M
       signDown = (int8_t)sgn(srcLine[x] - srcLineBelow[x+1]);
347
36.5M
       edgeType = signDown + signUpLine[x];
348
36.5M
       diff [edgeType] += (orgLine[x] - srcLine[x]);
349
36.5M
       count[edgeType] ++;
350
36.5M
       signDownLine[x+1] = -signDown;
351
36.5M
     }
352
582k
     signDownLine[startX] = (int8_t)sgn(srcLineBelow[startX] - srcLine[startX-1]);
353
582k
     signTmpLine  = signUpLine;
354
582k
     signUpLine   = signDownLine;
355
582k
     signDownLine = signTmpLine;
356
582k
     srcLine += srcStride;
357
582k
     orgLine += orgStride;
358
582k
   }
359
10.5k
}
360
void calcSaoStatisticsEo45_Core(int width,int startX,int endX,int endY,Pel*  srcLine,Pel*  orgLine,int srcStride,int orgStride,int64_t  *count, int64_t *diff,int8_t *signUpLine)
361
10.5k
{
362
10.5k
  int x,y,edgeType;
363
10.5k
  int8_t signDown;
364
10.5k
  Pel* srcLineBelow = srcLine + srcStride;
365
  //middle lines
366
592k
  for (y=1; y<endY; y++)
367
582k
  {
368
582k
    srcLineBelow = srcLine + srcStride;
369
370
37.0M
    for(x=startX; x<endX; x++)
371
36.5M
    {
372
36.5M
      signDown = (int8_t)sgn(srcLine[x] - srcLineBelow[x-1]);
373
36.5M
      edgeType = signDown + signUpLine[x];
374
36.5M
      diff [edgeType] += (orgLine[x] - srcLine[x]);
375
36.5M
      count[edgeType] ++;
376
36.5M
      signUpLine[x-1] = -signDown;
377
36.5M
    }
378
582k
    signUpLine[endX-1] = (int8_t)sgn(srcLineBelow[endX-1] - srcLine[endX]);
379
582k
    srcLine  += srcStride;
380
582k
    orgLine  += orgStride;
381
582k
  }
382
10.5k
}
383
void calcSaoStatisticsBo_Core(int width,int endX,int endY,Pel*  srcLine,Pel*  orgLine,int srcStride,int orgStride,int channelBitDepth, int64_t *count,int64_t  *diff)
384
10.5k
{
385
10.5k
  int x,y;
386
10.5k
  int startX=0;
387
10.5k
  int shiftBits = channelBitDepth - NUM_SAO_BO_CLASSES_LOG2;
388
609k
  for (y=0; y< endY; y++)
389
598k
  {
390
38.6M
    for (x=startX; x< endX; x++)
391
38.0M
    {
392
38.0M
      int bandIdx= srcLine[x] >> shiftBits;
393
38.0M
      diff [bandIdx] += (orgLine[x] - srcLine[x]);
394
38.0M
      count[bandIdx] ++;
395
38.0M
    }
396
598k
    srcLine += srcStride;
397
598k
    orgLine += orgStride;
398
598k
  }
399
10.5k
}
400
401
void SAOOffset::reset()
402
185k
{
403
185k
  modeIdc = SAO_MODE_OFF;
404
185k
  typeIdc = -1;
405
185k
  typeAuxInfo = -1;
406
185k
  ::memset(offset, 0, sizeof(int)* MAX_NUM_SAO_CLASSES);
407
185k
}
408
409
void SAOBlkParam::reset()
410
29.1k
{
411
116k
  for(int compIdx = 0; compIdx < MAX_NUM_COMP; compIdx++)
412
87.5k
  {
413
87.5k
    SAOOffsets[compIdx].reset();
414
87.5k
  }
415
29.1k
}
416
417
SampleAdaptiveOffset::SampleAdaptiveOffset( bool enableOpt )
418
8.15k
{
419
8.15k
  offsetBlock = offsetBlock_core;
420
8.15k
  calcSaoStatisticsEo90 = calcSaoStatisticsEo90_Core;
421
8.15k
  calcSaoStatisticsEo135 = calcSaoStatisticsEo135_Core;
422
8.15k
  calcSaoStatisticsEo45 = calcSaoStatisticsEo45_Core;
423
8.15k
  calcSaoStatisticsEo0 = calcSaoStatisticsEo0_Core;
424
8.15k
  calcSaoStatisticsBo = calcSaoStatisticsBo_Core;
425
426
8.15k
  if( enableOpt )
427
8.15k
  {
428
#if ENABLE_SIMD_OPT_SAO && defined( TARGET_SIMD_X86 )
429
    initSampleAdaptiveOffsetX86();
430
#endif
431
#if ENABLE_SIMD_OPT_SAO && defined( TARGET_SIMD_ARM )
432
    initSampleAdaptiveOffsetARM();
433
#endif
434
8.15k
  }
435
8.15k
}
436
437
438
SampleAdaptiveOffset::~SampleAdaptiveOffset()
439
8.15k
{
440
8.15k
  m_signLineBuf1.clear();
441
8.15k
  m_signLineBuf2.clear();
442
8.15k
}
443
444
void SampleAdaptiveOffset::init( ChromaFormat format, uint32_t maxCUWidth, uint32_t maxCUHeight, uint32_t lumaBitShift, uint32_t chromaBitShift )
445
8.15k
{
446
  //bit-depth related
447
32.6k
  for(int compIdx = 0; compIdx < MAX_NUM_COMP; compIdx++)
448
24.4k
  {
449
24.4k
    m_offsetStepLog2  [compIdx] = isLuma(ComponentID(compIdx))? lumaBitShift : chromaBitShift;
450
24.4k
  }
451
8.15k
  m_numberOfComponents = getNumberValidComponents(format);
452
453
8.15k
  size_t lineBufferSize = std::max( maxCUWidth, maxCUHeight ) + 1;
454
8.15k
  if( m_signLineBuf1.size() < lineBufferSize )
455
8.15k
  {
456
8.15k
    m_signLineBuf1.resize( lineBufferSize );
457
8.15k
    m_signLineBuf2.resize( lineBufferSize );
458
8.15k
  }
459
8.15k
}
460
461
void SampleAdaptiveOffset::invertQuantOffsets(ComponentID compIdx, int typeIdc, int typeAuxInfo, int* dstOffsets, int* srcOffsets)
462
52.5k
{
463
52.5k
  int codedOffset[MAX_NUM_SAO_CLASSES];
464
465
52.5k
  ::memcpy(codedOffset, srcOffsets, sizeof(int)*MAX_NUM_SAO_CLASSES);
466
52.5k
  ::memset(dstOffsets, 0, sizeof(int)*MAX_NUM_SAO_CLASSES);
467
468
52.5k
  if(typeIdc == SAO_TYPE_START_BO)
469
10.5k
  {
470
52.6k
    for(int i=0; i< 4; i++)
471
42.1k
    {
472
42.1k
      dstOffsets[(typeAuxInfo+ i)%NUM_SAO_BO_CLASSES] = codedOffset[(typeAuxInfo+ i)%NUM_SAO_BO_CLASSES]*(1<<m_offsetStepLog2[compIdx]);
473
42.1k
    }
474
10.5k
  }
475
42.0k
  else //EO
476
42.0k
  {
477
252k
    for(int i=0; i< NUM_SAO_EO_CLASSES; i++)
478
210k
    {
479
210k
      dstOffsets[i] = codedOffset[i] *(1<<m_offsetStepLog2[compIdx]);
480
210k
    }
481
42.0k
    CHECK(dstOffsets[SAO_CLASS_EO_PLAIN] != 0, "EO offset is not '0'"); //keep EO plain offset as zero
482
42.0k
  }
483
484
52.5k
}
485
486
int SampleAdaptiveOffset::getMergeList(CodingStructure& cs, int ctuRsAddr, SAOBlkParam* blkParams, SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES])
487
3.50k
{
488
3.50k
  const PreCalcValues& pcv = *cs.pcv;
489
490
3.50k
  int ctuX = ctuRsAddr % pcv.widthInCtus;
491
3.50k
  int ctuY = ctuRsAddr / pcv.widthInCtus;
492
3.50k
  const CodingUnit& cu = *cs.getCU(Position(ctuX*pcv.maxCUSize, ctuY*pcv.maxCUSize), CH_L, TREE_D);
493
3.50k
  int mergedCTUPos;
494
3.50k
  int numValidMergeCandidates = 0;
495
496
10.5k
  for(int mergeType=0; mergeType< NUM_SAO_MERGE_TYPES; mergeType++)
497
7.00k
  {
498
7.00k
    SAOBlkParam* mergeCandidate = NULL;
499
500
7.00k
    switch(mergeType)
501
7.00k
    {
502
3.50k
    case SAO_MERGE_ABOVE:
503
3.50k
      {
504
3.50k
        if(ctuY > 0)
505
1.58k
        {
506
1.58k
          mergedCTUPos = ctuRsAddr- pcv.widthInCtus;
507
1.58k
          if(cs.getCURestricted(Position(ctuX*pcv.maxCUSize, (ctuY-1)*pcv.maxCUSize), cu, cu.chType))
508
1.58k
          {
509
1.58k
            mergeCandidate = &(blkParams[mergedCTUPos]);
510
1.58k
          }
511
1.58k
        }
512
3.50k
      }
513
3.50k
      break;
514
3.50k
    case SAO_MERGE_LEFT:
515
3.50k
      {
516
3.50k
        if(ctuX > 0)
517
1.46k
        {
518
1.46k
          mergedCTUPos = ctuRsAddr- 1;
519
1.46k
          if(cs.getCURestricted(Position((ctuX-1)*pcv.maxCUSize, ctuY*pcv.maxCUSize), cu, cu.chType))
520
1.46k
          {
521
1.46k
            mergeCandidate = &(blkParams[mergedCTUPos]);
522
1.46k
          }
523
1.46k
        }
524
3.50k
      }
525
3.50k
      break;
526
0
    default:
527
0
      {
528
0
        THROW("not a supported merge type");
529
0
      }
530
7.00k
    }
531
532
7.00k
    mergeList[mergeType]=mergeCandidate;
533
7.00k
    if (mergeCandidate != NULL)
534
3.04k
    {
535
3.04k
      numValidMergeCandidates++;
536
3.04k
    }
537
7.00k
  }
538
539
3.50k
  return numValidMergeCandidates;
540
3.50k
}
541
542
543
void SampleAdaptiveOffset::reconstructBlkSAOParam(SAOBlkParam& recParam, SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES])
544
3.50k
{
545
3.50k
  const int numberOfComponents = m_numberOfComponents;
546
14.0k
  for(int compIdx = 0; compIdx < numberOfComponents; compIdx++)
547
10.5k
  {
548
10.5k
    const ComponentID component = ComponentID(compIdx);
549
10.5k
    SAOOffset& offsetParam = recParam[component];
550
551
10.5k
    if(offsetParam.modeIdc == SAO_MODE_OFF)
552
3.45k
    {
553
3.45k
      continue;
554
3.45k
    }
555
556
7.05k
    switch(offsetParam.modeIdc)
557
7.05k
    {
558
21
    case SAO_MODE_NEW:
559
21
      {
560
21
        invertQuantOffsets(component, offsetParam.typeIdc, offsetParam.typeAuxInfo, offsetParam.offset, offsetParam.offset);
561
21
      }
562
21
      break;
563
7.03k
    case SAO_MODE_MERGE:
564
7.03k
      {
565
7.03k
        SAOBlkParam* mergeTarget = mergeList[offsetParam.typeIdc];
566
7.03k
        CHECK(mergeTarget == NULL, "Merge target does not exist");
567
568
7.03k
        offsetParam = (*mergeTarget)[component];
569
7.03k
      }
570
0
      break;
571
0
    default:
572
0
      {
573
0
        THROW("Not a supported mode");
574
7.03k
      }
575
7.05k
    }
576
7.05k
  }
577
3.50k
}
578
579
void SampleAdaptiveOffset::xReconstructBlkSAOParams(CodingStructure& cs, SAOBlkParam* saoBlkParams)
580
0
{
581
0
  for(uint32_t compIdx = 0; compIdx < MAX_NUM_COMP; compIdx++)
582
0
  {
583
0
    m_picSAOEnabled[compIdx] = false;
584
0
  }
585
586
0
  const uint32_t numberOfComponents = getNumberValidComponents(cs.pcv->chrFormat);
587
588
0
  for(int ctuRsAddr=0; ctuRsAddr< cs.pcv->sizeInCtus; ctuRsAddr++)
589
0
  {
590
0
    SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES] = { NULL };
591
0
    getMergeList(cs, ctuRsAddr, saoBlkParams, mergeList);
592
593
0
    reconstructBlkSAOParam(saoBlkParams[ctuRsAddr], mergeList);
594
595
0
    for(uint32_t compIdx = 0; compIdx < numberOfComponents; compIdx++)
596
0
    {
597
0
      if(saoBlkParams[ctuRsAddr][compIdx].modeIdc != SAO_MODE_OFF)
598
0
      {
599
0
        m_picSAOEnabled[compIdx] = true;
600
0
      }
601
0
    }
602
0
  }
603
0
}
604
605
void SampleAdaptiveOffset::offsetCTU( const UnitArea& area, const CPelUnitBuf& srcCtu, PelUnitBuf& res, SAOBlkParam& saoblkParam, CodingStructure& cs)
606
3.50k
{
607
3.50k
  const uint32_t numberOfComponents = getNumberValidComponents( area.chromaFormat );
608
3.50k
  bool bAllOff=true;
609
14.0k
  for( uint32_t compIdx = 0; compIdx < numberOfComponents; compIdx++)
610
10.5k
  {
611
10.5k
    if (saoblkParam[compIdx].modeIdc != SAO_MODE_OFF)
612
32
    {
613
32
      bAllOff=false;
614
32
    }
615
10.5k
  }
616
3.50k
  if (bAllOff)
617
3.47k
  {
618
3.47k
    return;
619
3.47k
  }
620
621
31
  uint8_t availMask;
622
  //block boundary availability
623
31
  deriveLoopFilterBoundaryAvailibility(cs, area.Y(), availMask);
624
625
31
  const size_t lineBufferSize = area.Y().width + 1;
626
31
  if (m_signLineBuf1.size() < lineBufferSize)
627
0
  {
628
0
    m_signLineBuf1.resize(lineBufferSize);
629
0
    m_signLineBuf2.resize(lineBufferSize);
630
0
  }
631
632
124
  for(int compIdx = 0; compIdx < numberOfComponents; compIdx++)
633
93
  {
634
93
    const ComponentID compID = ComponentID(compIdx);
635
93
    const CompArea& compArea = area.block(compID);
636
93
    SAOOffset& ctbOffset     = saoblkParam[compIdx];
637
638
93
    if(ctbOffset.modeIdc != SAO_MODE_OFF)
639
32
    {
640
32
      int  srcStride    = srcCtu.get(compID).stride;
641
32
      const Pel* srcBlk = srcCtu.get(compID).buf;
642
32
      int  resStride    = res.get(compID).stride;
643
32
      Pel* resBlk       = res.get(compID).bufAt(compArea);
644
645
32
      offsetBlock( cs.sps->bitDepths[toChannelType(compID)],
646
32
                   cs.slice->clpRngs[compID],
647
32
                   ctbOffset.typeIdc, ctbOffset.offset, ctbOffset.typeAuxInfo
648
32
                  , srcBlk, resBlk, srcStride, resStride, compArea.width, compArea.height, availMask
649
32
                  , m_signLineBuf1, m_signLineBuf2 );
650
32
    }
651
93
  } //compIdx
652
31
}
653
654
void SampleAdaptiveOffset::deriveLoopFilterBoundaryAvailibility(CodingStructure& cs, const Position& pos, uint8_t& availMask ) const
655
31
{
656
31
  const int cuSize = cs.pcv->maxCUSize;
657
31
  CodingUnit *cuLeft, *cuRight, *cuAbove, *cuBelow, *cuAboveLeft, *cuAboveRight, *cuBelowLeft, *cuBelowRight;
658
31
  const CodingUnit *cuCurr = cs.getCU( pos, CH_L, TREE_D );
659
660
31
  if (!cs.pps->getSubPicFromCU(*cuCurr).loopFilterAcrossSubPicEnabled)
661
0
  {
662
0
    THROW("no support");
663
0
  }
664
665
31
  if( !cs.pps->loopFilterAcrossSlicesEnabled || !cs.pps->loopFilterAcrossTilesEnabled )
666
0
  {
667
0
    const int ctuX = pos.x >> cs.pcv->maxCUSizeLog2;
668
0
    const int ctuY = pos.y >> cs.pcv->maxCUSizeLog2;
669
0
    const PPS* pps = cs.slice->pps;
670
0
    const int Xmax = pps->pcv->widthInCtus  - 1;
671
0
    const int Ymax = pps->pcv->heightInCtus - 1;
672
0
    cuLeft       = ctuX > 0                   && pps->canFilterCtuBdry( ctuX, ctuY, -1, 0 ) ? cs.getCU( pos.offset( -cuSize, 0       ), CH_L, TREE_D ): nullptr;
673
0
    cuRight      = ctuX < Xmax                && pps->canFilterCtuBdry( ctuX, ctuY,  1, 0 ) ? cs.getCU( pos.offset( cuSize , 0       ), CH_L, TREE_D ): nullptr;
674
0
    cuAbove      =                ctuY > 0    && pps->canFilterCtuBdry( ctuX, ctuY,  0,-1 ) ? cs.getCU( pos.offset( 0      , -cuSize ), CH_L, TREE_D ): nullptr;
675
0
    cuBelow      =                ctuY < Ymax && pps->canFilterCtuBdry( ctuX, ctuY,  0, 1 ) ? cs.getCU( pos.offset( 0      , cuSize  ), CH_L, TREE_D ): nullptr;
676
0
    cuAboveLeft  = ctuX > 0    && ctuY > 0    && pps->canFilterCtuBdry( ctuX, ctuY, -1,-1 ) ? cs.getCU( pos.offset( -cuSize, -cuSize ), CH_L, TREE_D ): nullptr;
677
0
    cuAboveRight = ctuX < Xmax && ctuY > 0    && pps->canFilterCtuBdry( ctuX, ctuY,  1,-1 ) ? cs.getCU( pos.offset( cuSize , -cuSize ), CH_L, TREE_D ): nullptr;
678
0
    cuBelowLeft  = ctuX > 0    && ctuY < Ymax && pps->canFilterCtuBdry( ctuX, ctuY, -1, 1 ) ? cs.getCU( pos.offset( -cuSize, cuSize  ), CH_L, TREE_D ): nullptr;
679
0
    cuBelowRight = ctuX < Xmax && ctuY < Ymax && pps->canFilterCtuBdry( ctuX, ctuY,  1, 1 ) ? cs.getCU( pos.offset( cuSize , cuSize  ), CH_L, TREE_D ): nullptr;
680
0
  }
681
31
  else
682
31
  {
683
31
    cuLeft       = cs.getCU( pos.offset( -cuSize,       0 ), CH_L, TREE_D );
684
31
    cuRight      = cs.getCU( pos.offset(  cuSize,       0 ), CH_L, TREE_D );
685
31
    cuAbove      = cs.getCU( pos.offset(       0, -cuSize ), CH_L, TREE_D );
686
31
    cuBelow      = cs.getCU( pos.offset(       0,  cuSize ), CH_L, TREE_D );
687
31
    cuAboveLeft  = cs.getCU( pos.offset( -cuSize, -cuSize ), CH_L, TREE_D );
688
31
    cuAboveRight = cs.getCU( pos.offset(  cuSize, -cuSize ), CH_L, TREE_D );
689
31
    cuBelowLeft  = cs.getCU( pos.offset( -cuSize,  cuSize ), CH_L, TREE_D );
690
31
    cuBelowRight = cs.getCU( pos.offset(  cuSize,  cuSize ), CH_L, TREE_D );
691
31
  }
692
31
  availMask = 0;
693
694
  // check cross slice flags
695
31
  if( cs.pps->loopFilterAcrossSlicesEnabled )
696
31
  {
697
31
    availMask |= (cuLeft       != NULL) ? LeftAvail : 0;
698
31
    availMask |= (cuAbove      != NULL) ? AboveAvail : 0;
699
31
    availMask |= (cuRight      != NULL) ? RightAvail : 0;
700
31
    availMask |= (cuBelow      != NULL) ? BelowAvail : 0;
701
31
    availMask |= (cuAboveLeft  != NULL) ? AboveLeftAvail : 0;
702
31
    availMask |= (cuBelowRight != NULL) ? BelowRightAvail : 0;
703
31
    availMask |= (cuAboveRight != NULL) ? AboveRightAvail : 0;
704
31
    availMask |= (cuBelowLeft  != NULL) ? BelowLeftAvail : 0;
705
31
  }
706
0
  else
707
0
  {
708
0
    availMask |= ((cuLeft       != NULL) && CU::isSameSlice(*cuCurr, *cuLeft) ) ? LeftAvail : 0;
709
0
    availMask |= ((cuAbove      != NULL) && CU::isSameSlice(*cuCurr, *cuAbove) ) ? AboveAvail : 0;
710
0
    availMask |= ((cuRight      != NULL) && CU::isSameSlice(*cuCurr, *cuRight)) ? RightAvail : 0;
711
0
    availMask |= ((cuBelow      != NULL) && CU::isSameSlice(*cuCurr, *cuBelow) ) ? BelowAvail : 0;
712
0
    availMask |= ((cuAboveLeft  != NULL) && CU::isSameSlice(*cuCurr, *cuAboveLeft)) ?  AboveLeftAvail : 0;
713
0
    availMask |= ((cuBelowRight != NULL) && CU::isSameSlice(*cuCurr, *cuBelowRight) ) ? BelowRightAvail : 0;
714
0
    availMask |= ((cuAboveRight != NULL) && CU::isSameSlice(*cuCurr, *cuAboveRight) ) ? AboveRightAvail : 0;
715
0
    availMask |= ( (cuBelowLeft != NULL) && CU::isSameSlice(*cuCurr, *cuBelowLeft) ) ? BelowLeftAvail : 0;
716
0
  }
717
718
  // check cross tile flags
719
31
  if (!cs.pps->loopFilterAcrossTilesEnabled)
720
0
  {
721
0
    uint8_t availMaskTile = 0;
722
0
    availMaskTile |= (availMask&LeftAvail       && CU::isSameTile(*cuCurr, *cuLeft)) ? LeftAvail : 0;
723
0
    availMaskTile |= (availMask&AboveAvail      && CU::isSameTile(*cuCurr, *cuAbove)) ? AboveAvail : 0;
724
0
    availMaskTile |= (availMask&RightAvail      && CU::isSameTile(*cuCurr, *cuRight)) ? RightAvail : 0;
725
0
    availMaskTile |= (availMask&BelowAvail      && CU::isSameTile(*cuCurr, *cuBelow)) ? BelowAvail : 0;
726
0
    availMaskTile |= (availMask&AboveLeftAvail  && CU::isSameTile(*cuCurr, *cuAboveLeft)) ? AboveLeftAvail : 0;
727
0
    availMaskTile |= (availMask&AboveRightAvail &&CU::isSameTile(*cuCurr, *cuAboveRight)) ? AboveRightAvail : 0;
728
0
    availMaskTile |= (availMask&BelowLeftAvail  && CU::isSameTile(*cuCurr, *cuBelowLeft)) ? BelowLeftAvail : 0;
729
0
    availMaskTile |= (availMask&BelowRightAvail && CU::isSameTile(*cuCurr, *cuBelowRight)) ? BelowRightAvail : 0;
730
0
    availMask = availMaskTile;
731
0
  }
732
31
}
733
734
} // namespace vvenc
735
736
//! \}
737