Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/vvenc/source/Lib/CommonLib/AffineGradientSearch.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
/**
43
 * \file
44
 * \brief Declaration of AffineGradientSearch class
45
 */
46
47
#pragma once
48
49
#include "CommonDef.h"
50
#include "TypeDef.h"
51
52
namespace vvenc {
53
  //! \ingroup CommonLib
54
  //! \{
55
56
#if defined(TARGET_SIMD_X86) && ENABLE_SIMD_OPT_AFFINE_ME
57
using namespace x86_simd;
58
#endif
59
60
#if defined(TARGET_SIMD_ARM) && ENABLE_SIMD_OPT_AFFINE_ME
61
using namespace arm_simd;
62
#endif
63
64
  // The normal equations of the affine motion search from sums per 4x4 block (used by the AVX2 and NEON kernels).
65
  //
66
  // Per pixel the scalar version (AffineGradientSearch::xEqualCoeffComputer) adds the products iC[p] * iC[q] and
67
  // iC[p] * 8 * residue, where every iC[p] is a sum of terms coef * cx^i * cy^j * d with d one of the two gradients
68
  // and cx = 4 * ( x / 4 ) + 2, cy = 4 * ( y / 4 ) + 2: constant within each 4x4 block. So every matrix entry is a
69
  // weighted sum of five per-block sums, S = sum of d0 * d0, d0 * d1, d1 * d1, d0 * residue and d1 * residue, weighted
70
  // by cx^i * cy^j with i + j <= 2. The kernels gather, for each S over all blocks of the CU, its sums weighted by 1, cx
71
  // and cx^2, and running sums over the bands of 4 rows for the cy weights (EqCoeffBlockTotals); the matrix follows from
72
  // them here. All sums are exact integers, so the result is identical to the per-pixel sums of the scalar version.
73
74
  enum EqCoeffSum { SUM_D0D0, SUM_D0D1, SUM_D1D1, SUM_D0R, SUM_D1R, NUM_EQ_COEFF_SUMS };
75
  static constexpr int NUM_EQ_COEFF_POWERS = 6;   // cx^i * cy^j for (i, j) = (0,0) (1,0) (2,0) (0,1) (1,1) (0,2)
76
77
  static inline int eqCoeffPower( int i, int j )
78
0
  {
79
0
    return j == 0 ? i : ( j == 1 ? 3 + i : 5 );
80
0
  }
Unexecuted instantiation: vvencimpl.cpp:vvenc::eqCoeffPower(int, int)
Unexecuted instantiation: EncLib.cpp:vvenc::eqCoeffPower(int, int)
Unexecuted instantiation: CuTree.cpp:vvenc::eqCoeffPower(int, int)
Unexecuted instantiation: EncGOP.cpp:vvenc::eqCoeffPower(int, int)
Unexecuted instantiation: EncPicture.cpp:vvenc::eqCoeffPower(int, int)
Unexecuted instantiation: EncSlice.cpp:vvenc::eqCoeffPower(int, int)
Unexecuted instantiation: InterSearch.cpp:vvenc::eqCoeffPower(int, int)
Unexecuted instantiation: AffineGradientSearch.cpp:vvenc::eqCoeffPower(int, int)
Unexecuted instantiation: EncCu.cpp:vvenc::eqCoeffPower(int, int)
Unexecuted instantiation: EncModeCtrl.cpp:vvenc::eqCoeffPower(int, int)
Unexecuted instantiation: IntraSearch.cpp:vvenc::eqCoeffPower(int, int)
81
82
  // One term coef * cx^i * cy^j * d of an iC[p], with d the gradient grad (0 or 1). An iC[p] has one or two terms; a
83
  // missing second term has coef 0, so every product of terms runs over two by two terms with fixed bounds (which the
84
  // compiler unrolls and folds) and the missing ones add 0.
85
  struct EqCoeffTerm  { int coef, i, j, grad; };
86
  struct EqCoeffTerms { EqCoeffTerm term[2]; };
87
88
  // Per sum S, with B_b the sums S of band b (rows 4 b to 4 b + 3) of N bands:
89
  //   sum, sumCx, sumCx2  S over all blocks, weighted by 1, cx and cx^2;
90
  //   run, runCx          sum_b ( N - b ) B_b, unweighted and weighted by cx;
91
  //   run2                sum_b ( N - b )( N - b + 1 ) / 2 B_b.
92
  // A kernel gets run and runCx by adding its totals so far after each band, and run2 by adding run after each band.
93
  struct EqCoeffBlockTotals
94
  {
95
    int64_t sum[NUM_EQ_COEFF_SUMS], sumCx[NUM_EQ_COEFF_SUMS], sumCx2[NUM_EQ_COEFF_SUMS];
96
    int64_t run[NUM_EQ_COEFF_SUMS], runCx[NUM_EQ_COEFF_SUMS], run2[NUM_EQ_COEFF_SUMS];
97
  };
98
99
  template<bool b6Param>
100
  static inline void eqCoeffMatrixFromBlockTotals( const EqCoeffBlockTotals& t, const int64_t numBands, int64_t ( *pEqualCoeff )[7] )
101
  {
102
    // weighted[s][power]: the sum over the block of cx^i * cy^j * S. From the running sums:
103
    //   sum_b ( N - b ) B_b = run, sum_b ( N - b )^2 B_b = 2 run2 - run, hence
104
    //   sum_b b B_b = N sum - run and sum_b b^2 B_b = N^2 sum - 2 N run + 2 run2 - run,
105
    // and cy = 4 b + 2, cy^2 = 16 b^2 + 16 b + 4. All exact in 64 bits (below 2^55 for 128x128 and 14-bit inputs).
106
    int64_t weighted[NUM_EQ_COEFF_SUMS][NUM_EQ_COEFF_POWERS];
107
    for( int s = 0; s < NUM_EQ_COEFF_SUMS; s++ )
108
    {
109
      const int64_t sumB   = numBands * t.sum[s] - t.run[s];      // sum_b b B_b
110
      const int64_t sumBCx = numBands * t.sumCx[s] - t.runCx[s];  // sum_b b B_b, weighted by cx
111
      const int64_t sumB2  = numBands * numBands * t.sum[s] - 2 * numBands * t.run[s] + 2 * t.run2[s] - t.run[s];
112
      weighted[s][eqCoeffPower( 0, 0 )] = t.sum[s];
113
      weighted[s][eqCoeffPower( 1, 0 )] = t.sumCx[s];
114
      weighted[s][eqCoeffPower( 2, 0 )] = t.sumCx2[s];
115
      weighted[s][eqCoeffPower( 0, 1 )] = 4 * sumB + 2 * t.sum[s];
116
      weighted[s][eqCoeffPower( 1, 1 )] = 4 * sumBCx + 2 * t.sumCx[s];
117
      weighted[s][eqCoeffPower( 0, 2 )] = 16 * sumB2 + 16 * sumB + 4 * t.sum[s];
118
    }
119
120
    // the iC[p] of the scalar version as terms: 6 parameters iC = { d0, cx d0, d1, cx d1, cy d0, cy d1 },
121
    // 4 parameters iC = { d0, cx d0 + cy d1, d1, cy d0 - cx d1 }
122
    static constexpr int n = b6Param ? 6 : 4;
123
    static constexpr EqCoeffTerms terms6[6] = { { { { 1, 0, 0, 0 } } }, { { { 1, 1, 0, 0 } } }, { { { 1, 0, 0, 1 } } },
124
                                                { { { 1, 1, 0, 1 } } }, { { { 1, 0, 1, 0 } } }, { { { 1, 0, 1, 1 } } } };
125
    static constexpr EqCoeffTerms terms4[4] = { { { { 1, 0, 0, 0 } } }, { { { 1, 1, 0, 0 }, { 1, 0, 1, 1 } } },
126
                                                { { { 1, 0, 0, 1 } } }, { { { 1, 0, 1, 0 }, { -1, 1, 0, 1 } } } };
127
    const EqCoeffTerms* terms = b6Param ? terms6 : terms4;
128
    static constexpr EqCoeffSum gradProduct[2][2] = { { SUM_D0D0, SUM_D0D1 }, { SUM_D0D1, SUM_D1D1 } };
129
130
    for( int col = 0; col < n; col++ )
131
    {
132
      for( int row = col; row < n; row++ )
133
      {
134
        int64_t sum = 0;
135
        for( int a = 0; a < 2; a++ )
136
        {
137
          const EqCoeffTerm& tc = terms[col].term[a];
138
          for( int b = 0; b < 2; b++ )
139
          {
140
            const EqCoeffTerm& tr = terms[row].term[b];
141
            sum += tc.coef * tr.coef * weighted[gradProduct[tc.grad][tr.grad]][eqCoeffPower( tc.i + tr.i, tc.j + tr.j )];
142
          }
143
        }
144
        // as the previous SIMD versions: the entry and its mirror both take the sum of the upper entry
145
        pEqualCoeff[col + 1][row] += sum;
146
        pEqualCoeff[row + 1][col]  = pEqualCoeff[col + 1][row];
147
      }
148
      int64_t sum = 0;
149
      for( int a = 0; a < 2; a++ )
150
      {
151
        const EqCoeffTerm& tc = terms[col].term[a];
152
        sum += tc.coef * weighted[tc.grad == 0 ? SUM_D0R : SUM_D1R][eqCoeffPower( tc.i, tc.j )];
153
      }
154
      pEqualCoeff[col + 1][n] += sum * 8;   // the scalar version's residue * (1 << 3)
155
    }
156
  }
157
158
  class AffineGradientSearch
159
  {
160
  public:
161
    void  (*m_HorizontalSobelFilter)  (Pel* const pPred, const int predStride, Pel *const pDerivate,   const int derivateBufStride, const int width, const int height);
162
    void  (*m_VerticalSobelFilter)    (Pel* const pPred, const int predStride, Pel *const pDerivate,   const int derivateBufStride, const int width, const int height);
163
    void  (*m_EqualCoeffComputer[2])  (Pel* const pResi, const int resiStride, Pel **const ppDerivate, const int derivateBufStride, const int width, const int height, int64_t(*pEqualCoeff)[7]);
164
165
    static void xHorizontalSobelFilter( Pel* const pPred, const int predStride, Pel *const pDerivate,   const int derivateBufStride, const int width, const int height);
166
    static void xVerticalSobelFilter  ( Pel* const pPred, const int predStride, Pel *const pDerivate,   const int derivateBufStride, const int width, const int height);
167
    template<bool b6Param>
168
    static void xEqualCoeffComputer   ( Pel* const pResi, const int resiStride, Pel **const ppDerivate, const int derivateBufStride, const int width, const int height, int64_t(*pEqualCoeff)[7]);
169
170
    AffineGradientSearch( bool enableOpt = true );
171
5.72k
    ~AffineGradientSearch() {}
172
173
#if defined(TARGET_SIMD_X86) && ENABLE_SIMD_OPT_AFFINE_ME
174
    void initAffineGradientSearchX86();
175
    template <X86_VEXT vext>
176
    void _initAffineGradientSearchX86();
177
#endif
178
179
#if defined(TARGET_SIMD_ARM) && ENABLE_SIMD_OPT_AFFINE_ME
180
    void initAffineGradientSearchARM();
181
    template <ARM_VEXT vext>
182
    void _initAffineGradientSearchARM();
183
#endif
184
  };
185
186
} // namespace vvenc
187
188
//! \}
189