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/TrQuant.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     TrQuant.cpp
45
    \brief    transform and quantization class
46
*/
47
48
#include "TrQuant.h"
49
#include "TrQuant_EMT.h"
50
#include "QuantRDOQ.h"
51
#include "DepQuant.h"
52
#include "UnitTools.h"
53
#include "ContextModelling.h"
54
#include "CodingStructure.h"
55
#include "dtrace_buffer.h"
56
#include "TimeProfiler.h"
57
#include "SearchSpaceCounter.h"
58
59
#include <stdlib.h>
60
#include <memory.h>
61
62
//! \ingroup CommonLib
63
//! \{
64
65
namespace vvenc {
66
67
struct coeffGroupRDStats
68
{
69
  int    iNNZbeforePos0;
70
  double d64CodedLevelandDist; // distortion and level cost only
71
  double d64UncodedDist;    // all zero coded block distortion
72
  double d64SigCost;
73
  double d64SigCost_0;
74
};
75
76
FwdTrans *const fastFwdTrans[NUM_TRANS_TYPE][g_numTransformMatrixSizes] =
77
{
78
  { fastForwardDCT2_B2, fastForwardDCT2_B4, fastForwardDCT2_B8, fastForwardDCT2_B16, fastForwardDCT2_B32, fastForwardDCT2_B64 },
79
  { nullptr,            fastForwardDCT8_B4, fastForwardDCT8_B8, fastForwardDCT8_B16, fastForwardDCT8_B32, nullptr },
80
  { nullptr,            fastForwardDST7_B4, fastForwardDST7_B8, fastForwardDST7_B16, fastForwardDST7_B32, nullptr },
81
};
82
83
InvTrans *const fastInvTrans[NUM_TRANS_TYPE][g_numTransformMatrixSizes] =
84
{
85
  { fastInverseDCT2_B2, fastInverseDCT2_B4, fastInverseDCT2_B8, fastInverseDCT2_B16, fastInverseDCT2_B32, fastInverseDCT2_B64 },
86
  { nullptr,            fastInverseDCT8_B4, fastInverseDCT8_B8, fastInverseDCT8_B16, fastInverseDCT8_B32, nullptr },
87
  { nullptr,            fastInverseDST7_B4, fastInverseDST7_B8, fastInverseDST7_B16, fastInverseDST7_B32, nullptr },
88
};
89
90
//! \ingroup CommonLib
91
//! \{
92
93
155M
static inline int64_t square( const int d ) { return d * (int64_t)d; }
94
95
template<int signedMode> std::pair<int64_t,int64_t> fwdTransformCbCr( const PelBuf& resCb, const PelBuf& resCr, PelBuf& resC1, PelBuf& resC2 )
96
490k
{
97
490k
  const Pel*  cb  = resCb.buf;
98
490k
  const Pel*  cr  = resCr.buf;
99
490k
  Pel*        c1  = resC1.buf;
100
490k
  Pel*        c2  = resC2.buf;
101
490k
  int64_t     d1  = 0;
102
490k
  int64_t     d2  = 0;
103
6.34M
  for( SizeType y = 0; y < resCb.height; y++, cb += resCb.stride, cr += resCr.stride, c1 += resC1.stride, c2 += resC2.stride )
104
5.85M
  {
105
83.8M
    for( SizeType x = 0; x < resCb.width; x++ )
106
77.9M
    {
107
77.9M
      int cbx = cb[x], crx = cr[x];
108
77.9M
      if      ( signedMode ==  1 )
109
19.4M
      {
110
19.4M
        c1[x] = Pel( ( 4*cbx + 2*crx ) / 5 );
111
19.4M
        d1   += square( cbx - c1[x] ) + square( crx - (c1[x]>>1) );
112
19.4M
      }
113
58.4M
      else if ( signedMode == -1 )
114
0
      {
115
0
        c1[x] = Pel( ( 4*cbx - 2*crx ) / 5 );
116
0
        d1   += square( cbx - c1[x] ) + square( crx - (-c1[x]>>1) );
117
0
      }
118
58.4M
      else if ( signedMode ==  2 )
119
19.4M
      {
120
19.4M
        c1[x] = Pel( ( cbx + crx ) / 2 );
121
19.4M
        d1   += square( cbx - c1[x] ) + square( crx - c1[x] );
122
19.4M
      }
123
38.9M
      else if ( signedMode == -2 )
124
0
      {
125
0
        c1[x] = Pel( ( cbx - crx ) / 2 );
126
0
        d1   += square( cbx - c1[x] ) + square( crx + c1[x] );
127
0
      }
128
38.9M
      else if ( signedMode ==  3 )
129
19.4M
      {
130
19.4M
        c2[x] = Pel( ( 4*crx + 2*cbx ) / 5 );
131
19.4M
        d1   += square( cbx - (c2[x]>>1) ) + square( crx - c2[x] );
132
19.4M
      }
133
19.4M
      else if ( signedMode == -3 )
134
0
      {
135
0
        c2[x] = Pel( ( 4*crx - 2*cbx ) / 5 );
136
0
        d1   += square( cbx - (-c2[x]>>1) ) + square( crx - c2[x] );
137
0
      }
138
19.4M
      else
139
19.4M
      {
140
19.4M
        d1   += square( cbx );
141
19.4M
        d2   += square( crx );
142
19.4M
      }
143
77.9M
    }
144
5.85M
  }
145
490k
  return std::make_pair(d1,d2);
146
490k
}
std::__1::pair<long, long> vvenc::fwdTransformCbCr<0>(vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
Line
Count
Source
96
122k
{
97
122k
  const Pel*  cb  = resCb.buf;
98
122k
  const Pel*  cr  = resCr.buf;
99
122k
  Pel*        c1  = resC1.buf;
100
122k
  Pel*        c2  = resC2.buf;
101
122k
  int64_t     d1  = 0;
102
122k
  int64_t     d2  = 0;
103
1.58M
  for( SizeType y = 0; y < resCb.height; y++, cb += resCb.stride, cr += resCr.stride, c1 += resC1.stride, c2 += resC2.stride )
104
1.46M
  {
105
20.9M
    for( SizeType x = 0; x < resCb.width; x++ )
106
19.4M
    {
107
19.4M
      int cbx = cb[x], crx = cr[x];
108
19.4M
      if      ( signedMode ==  1 )
109
0
      {
110
0
        c1[x] = Pel( ( 4*cbx + 2*crx ) / 5 );
111
0
        d1   += square( cbx - c1[x] ) + square( crx - (c1[x]>>1) );
112
0
      }
113
19.4M
      else if ( signedMode == -1 )
114
0
      {
115
0
        c1[x] = Pel( ( 4*cbx - 2*crx ) / 5 );
116
0
        d1   += square( cbx - c1[x] ) + square( crx - (-c1[x]>>1) );
117
0
      }
118
19.4M
      else if ( signedMode ==  2 )
119
0
      {
120
0
        c1[x] = Pel( ( cbx + crx ) / 2 );
121
0
        d1   += square( cbx - c1[x] ) + square( crx - c1[x] );
122
0
      }
123
19.4M
      else if ( signedMode == -2 )
124
0
      {
125
0
        c1[x] = Pel( ( cbx - crx ) / 2 );
126
0
        d1   += square( cbx - c1[x] ) + square( crx + c1[x] );
127
0
      }
128
19.4M
      else if ( signedMode ==  3 )
129
0
      {
130
0
        c2[x] = Pel( ( 4*crx + 2*cbx ) / 5 );
131
0
        d1   += square( cbx - (c2[x]>>1) ) + square( crx - c2[x] );
132
0
      }
133
19.4M
      else if ( signedMode == -3 )
134
0
      {
135
0
        c2[x] = Pel( ( 4*crx - 2*cbx ) / 5 );
136
0
        d1   += square( cbx - (-c2[x]>>1) ) + square( crx - c2[x] );
137
0
      }
138
19.4M
      else
139
19.4M
      {
140
19.4M
        d1   += square( cbx );
141
19.4M
        d2   += square( crx );
142
19.4M
      }
143
19.4M
    }
144
1.46M
  }
145
122k
  return std::make_pair(d1,d2);
146
122k
}
std::__1::pair<long, long> vvenc::fwdTransformCbCr<1>(vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
Line
Count
Source
96
122k
{
97
122k
  const Pel*  cb  = resCb.buf;
98
122k
  const Pel*  cr  = resCr.buf;
99
122k
  Pel*        c1  = resC1.buf;
100
122k
  Pel*        c2  = resC2.buf;
101
122k
  int64_t     d1  = 0;
102
122k
  int64_t     d2  = 0;
103
1.58M
  for( SizeType y = 0; y < resCb.height; y++, cb += resCb.stride, cr += resCr.stride, c1 += resC1.stride, c2 += resC2.stride )
104
1.46M
  {
105
20.9M
    for( SizeType x = 0; x < resCb.width; x++ )
106
19.4M
    {
107
19.4M
      int cbx = cb[x], crx = cr[x];
108
19.4M
      if      ( signedMode ==  1 )
109
19.4M
      {
110
19.4M
        c1[x] = Pel( ( 4*cbx + 2*crx ) / 5 );
111
19.4M
        d1   += square( cbx - c1[x] ) + square( crx - (c1[x]>>1) );
112
19.4M
      }
113
0
      else if ( signedMode == -1 )
114
0
      {
115
0
        c1[x] = Pel( ( 4*cbx - 2*crx ) / 5 );
116
0
        d1   += square( cbx - c1[x] ) + square( crx - (-c1[x]>>1) );
117
0
      }
118
0
      else if ( signedMode ==  2 )
119
0
      {
120
0
        c1[x] = Pel( ( cbx + crx ) / 2 );
121
0
        d1   += square( cbx - c1[x] ) + square( crx - c1[x] );
122
0
      }
123
0
      else if ( signedMode == -2 )
124
0
      {
125
0
        c1[x] = Pel( ( cbx - crx ) / 2 );
126
0
        d1   += square( cbx - c1[x] ) + square( crx + c1[x] );
127
0
      }
128
0
      else if ( signedMode ==  3 )
129
0
      {
130
0
        c2[x] = Pel( ( 4*crx + 2*cbx ) / 5 );
131
0
        d1   += square( cbx - (c2[x]>>1) ) + square( crx - c2[x] );
132
0
      }
133
0
      else if ( signedMode == -3 )
134
0
      {
135
0
        c2[x] = Pel( ( 4*crx - 2*cbx ) / 5 );
136
0
        d1   += square( cbx - (-c2[x]>>1) ) + square( crx - c2[x] );
137
0
      }
138
0
      else
139
0
      {
140
0
        d1   += square( cbx );
141
0
        d2   += square( crx );
142
0
      }
143
19.4M
    }
144
1.46M
  }
145
122k
  return std::make_pair(d1,d2);
146
122k
}
Unexecuted instantiation: std::__1::pair<long, long> vvenc::fwdTransformCbCr<-1>(vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
std::__1::pair<long, long> vvenc::fwdTransformCbCr<2>(vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
Line
Count
Source
96
122k
{
97
122k
  const Pel*  cb  = resCb.buf;
98
122k
  const Pel*  cr  = resCr.buf;
99
122k
  Pel*        c1  = resC1.buf;
100
122k
  Pel*        c2  = resC2.buf;
101
122k
  int64_t     d1  = 0;
102
122k
  int64_t     d2  = 0;
103
1.58M
  for( SizeType y = 0; y < resCb.height; y++, cb += resCb.stride, cr += resCr.stride, c1 += resC1.stride, c2 += resC2.stride )
104
1.46M
  {
105
20.9M
    for( SizeType x = 0; x < resCb.width; x++ )
106
19.4M
    {
107
19.4M
      int cbx = cb[x], crx = cr[x];
108
19.4M
      if      ( signedMode ==  1 )
109
0
      {
110
0
        c1[x] = Pel( ( 4*cbx + 2*crx ) / 5 );
111
0
        d1   += square( cbx - c1[x] ) + square( crx - (c1[x]>>1) );
112
0
      }
113
19.4M
      else if ( signedMode == -1 )
114
0
      {
115
0
        c1[x] = Pel( ( 4*cbx - 2*crx ) / 5 );
116
0
        d1   += square( cbx - c1[x] ) + square( crx - (-c1[x]>>1) );
117
0
      }
118
19.4M
      else if ( signedMode ==  2 )
119
19.4M
      {
120
19.4M
        c1[x] = Pel( ( cbx + crx ) / 2 );
121
19.4M
        d1   += square( cbx - c1[x] ) + square( crx - c1[x] );
122
19.4M
      }
123
0
      else if ( signedMode == -2 )
124
0
      {
125
0
        c1[x] = Pel( ( cbx - crx ) / 2 );
126
0
        d1   += square( cbx - c1[x] ) + square( crx + c1[x] );
127
0
      }
128
0
      else if ( signedMode ==  3 )
129
0
      {
130
0
        c2[x] = Pel( ( 4*crx + 2*cbx ) / 5 );
131
0
        d1   += square( cbx - (c2[x]>>1) ) + square( crx - c2[x] );
132
0
      }
133
0
      else if ( signedMode == -3 )
134
0
      {
135
0
        c2[x] = Pel( ( 4*crx - 2*cbx ) / 5 );
136
0
        d1   += square( cbx - (-c2[x]>>1) ) + square( crx - c2[x] );
137
0
      }
138
0
      else
139
0
      {
140
0
        d1   += square( cbx );
141
0
        d2   += square( crx );
142
0
      }
143
19.4M
    }
144
1.46M
  }
145
122k
  return std::make_pair(d1,d2);
146
122k
}
Unexecuted instantiation: std::__1::pair<long, long> vvenc::fwdTransformCbCr<-2>(vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
std::__1::pair<long, long> vvenc::fwdTransformCbCr<3>(vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
Line
Count
Source
96
122k
{
97
122k
  const Pel*  cb  = resCb.buf;
98
122k
  const Pel*  cr  = resCr.buf;
99
122k
  Pel*        c1  = resC1.buf;
100
122k
  Pel*        c2  = resC2.buf;
101
122k
  int64_t     d1  = 0;
102
122k
  int64_t     d2  = 0;
103
1.58M
  for( SizeType y = 0; y < resCb.height; y++, cb += resCb.stride, cr += resCr.stride, c1 += resC1.stride, c2 += resC2.stride )
104
1.46M
  {
105
20.9M
    for( SizeType x = 0; x < resCb.width; x++ )
106
19.4M
    {
107
19.4M
      int cbx = cb[x], crx = cr[x];
108
19.4M
      if      ( signedMode ==  1 )
109
0
      {
110
0
        c1[x] = Pel( ( 4*cbx + 2*crx ) / 5 );
111
0
        d1   += square( cbx - c1[x] ) + square( crx - (c1[x]>>1) );
112
0
      }
113
19.4M
      else if ( signedMode == -1 )
114
0
      {
115
0
        c1[x] = Pel( ( 4*cbx - 2*crx ) / 5 );
116
0
        d1   += square( cbx - c1[x] ) + square( crx - (-c1[x]>>1) );
117
0
      }
118
19.4M
      else if ( signedMode ==  2 )
119
0
      {
120
0
        c1[x] = Pel( ( cbx + crx ) / 2 );
121
0
        d1   += square( cbx - c1[x] ) + square( crx - c1[x] );
122
0
      }
123
19.4M
      else if ( signedMode == -2 )
124
0
      {
125
0
        c1[x] = Pel( ( cbx - crx ) / 2 );
126
0
        d1   += square( cbx - c1[x] ) + square( crx + c1[x] );
127
0
      }
128
19.4M
      else if ( signedMode ==  3 )
129
19.4M
      {
130
19.4M
        c2[x] = Pel( ( 4*crx + 2*cbx ) / 5 );
131
19.4M
        d1   += square( cbx - (c2[x]>>1) ) + square( crx - c2[x] );
132
19.4M
      }
133
0
      else if ( signedMode == -3 )
134
0
      {
135
0
        c2[x] = Pel( ( 4*crx - 2*cbx ) / 5 );
136
0
        d1   += square( cbx - (-c2[x]>>1) ) + square( crx - c2[x] );
137
0
      }
138
0
      else
139
0
      {
140
0
        d1   += square( cbx );
141
0
        d2   += square( crx );
142
0
      }
143
19.4M
    }
144
1.46M
  }
145
122k
  return std::make_pair(d1,d2);
146
122k
}
Unexecuted instantiation: std::__1::pair<long, long> vvenc::fwdTransformCbCr<-3>(vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short> const&, vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
147
148
template<int signedMode> void invTransformCbCr( PelBuf& resCb, PelBuf& resCr )
149
314k
{
150
314k
  Pel*  cb  = resCb.buf;
151
314k
  Pel*  cr  = resCr.buf;
152
4.29M
  for( SizeType y = 0; y < resCb.height; y++, cb += resCb.stride, cr += resCr.stride )
153
3.98M
  {
154
58.2M
    for( SizeType x = 0; x < resCb.width; x++ )
155
54.2M
    {
156
54.2M
      if      ( signedMode ==  1 )  { cr[x] =  cb[x] >> 1;  }
157
54.2M
      else if ( signedMode == -1 )  { cr[x] = -cb[x] >> 1;  }
158
54.2M
      else if ( signedMode ==  2 )  { cr[x] =  cb[x]; }
159
284k
      else if ( signedMode == -2 )  { cr[x] = -cb[x]; }
160
284k
      else if ( signedMode ==  3 )  { cb[x] =  cr[x] >> 1; }
161
0
      else if ( signedMode == -3 )  { cb[x] = -cr[x] >> 1; }
162
54.2M
    }
163
3.98M
  }
164
314k
}
Unexecuted instantiation: void vvenc::invTransformCbCr<0>(vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
Unexecuted instantiation: void vvenc::invTransformCbCr<1>(vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
Unexecuted instantiation: void vvenc::invTransformCbCr<-1>(vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
void vvenc::invTransformCbCr<2>(vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
Line
Count
Source
149
312k
{
150
312k
  Pel*  cb  = resCb.buf;
151
312k
  Pel*  cr  = resCr.buf;
152
4.26M
  for( SizeType y = 0; y < resCb.height; y++, cb += resCb.stride, cr += resCr.stride )
153
3.95M
  {
154
57.9M
    for( SizeType x = 0; x < resCb.width; x++ )
155
53.9M
    {
156
53.9M
      if      ( signedMode ==  1 )  { cr[x] =  cb[x] >> 1;  }
157
53.9M
      else if ( signedMode == -1 )  { cr[x] = -cb[x] >> 1;  }
158
53.9M
      else if ( signedMode ==  2 )  { cr[x] =  cb[x]; }
159
0
      else if ( signedMode == -2 )  { cr[x] = -cb[x]; }
160
0
      else if ( signedMode ==  3 )  { cb[x] =  cr[x] >> 1; }
161
0
      else if ( signedMode == -3 )  { cb[x] = -cr[x] >> 1; }
162
53.9M
    }
163
3.95M
  }
164
312k
}
Unexecuted instantiation: void vvenc::invTransformCbCr<-2>(vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
void vvenc::invTransformCbCr<3>(vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
Line
Count
Source
149
2.23k
{
150
2.23k
  Pel*  cb  = resCb.buf;
151
2.23k
  Pel*  cr  = resCr.buf;
152
27.4k
  for( SizeType y = 0; y < resCb.height; y++, cb += resCb.stride, cr += resCr.stride )
153
25.1k
  {
154
309k
    for( SizeType x = 0; x < resCb.width; x++ )
155
284k
    {
156
284k
      if      ( signedMode ==  1 )  { cr[x] =  cb[x] >> 1;  }
157
284k
      else if ( signedMode == -1 )  { cr[x] = -cb[x] >> 1;  }
158
284k
      else if ( signedMode ==  2 )  { cr[x] =  cb[x]; }
159
284k
      else if ( signedMode == -2 )  { cr[x] = -cb[x]; }
160
284k
      else if ( signedMode ==  3 )  { cb[x] =  cr[x] >> 1; }
161
0
      else if ( signedMode == -3 )  { cb[x] = -cr[x] >> 1; }
162
284k
    }
163
25.1k
  }
164
2.23k
}
Unexecuted instantiation: void vvenc::invTransformCbCr<-3>(vvenc::AreaBuf<short>&, vvenc::AreaBuf<short>&)
165
166
void xFwdLfnstNxNCore(int *src, int *dst, const uint32_t mode, const uint32_t index, const uint32_t size, int zeroOutSize)
167
1.40M
{
168
1.40M
  const int8_t *trMat  = (size > 4) ? g_lfnstFwd8x8[mode][index][0] : g_lfnstFwd4x4[mode][index][0];
169
1.40M
  const int     trSize = (size > 4) ? 48 : 16;
170
1.40M
  int           coef;
171
1.40M
  int *         out = dst;
172
173
21.8M
  for (int j = 0; j < zeroOutSize; j++)
174
20.4M
  {
175
20.4M
    int *         srcPtr   = src;
176
20.4M
    const int8_t *trMatTmp = trMat;
177
20.4M
    coef                   = 0;
178
783M
    for (int i = 0; i < trSize; i++)
179
763M
    {
180
763M
      coef += *srcPtr++ * *trMatTmp++;
181
763M
    }
182
20.4M
    *out++ = (coef + 64) >> 7;
183
20.4M
    trMat += trSize;
184
20.4M
  }
185
186
1.40M
  ::memset(out, 0, (trSize - zeroOutSize) * sizeof(int));
187
1.40M
}
188
189
190
void xInvLfnstNxNCore(int *src, int *dst, const uint32_t mode, const uint32_t index, const uint32_t size, int zeroOutSize)
191
600k
{
192
600k
  int           maxLog2TrDynamicRange = 15;
193
600k
  const TCoeff  outputMinimum         = -(1 << maxLog2TrDynamicRange);
194
600k
  const TCoeff  outputMaximum         = (1 << maxLog2TrDynamicRange) - 1;
195
600k
  const int8_t *trMat                 = (size > 4) ? g_lfnstInv8x8[mode][index][0] : g_lfnstInv4x4[mode][index][0];
196
600k
  const int     trSize                = (size > 4) ? 48 : 16;
197
600k
  int           resi;
198
600k
  int *         out                   = dst;
199
200
22.6M
  for( int j = 0; j < trSize; j++, trMat += 16 )
201
22.0M
  {
202
22.0M
    resi = 0;
203
22.0M
    const int8_t* trMatTmp = trMat;
204
22.0M
    int*          srcPtr   = src;
205
206
335M
    for( int i = 0; i < zeroOutSize; i++ )
207
313M
    {
208
313M
      resi += *srcPtr++ * *trMatTmp++;
209
313M
    }
210
211
22.0M
    *out++ = Clip3( outputMinimum, outputMaximum, ( int ) ( resi + 64 ) >> 7 );
212
22.0M
  }
213
600k
}
214
215
// ====================================================================================================================
216
// TrQuant class member functions
217
// ====================================================================================================================
218
5.72k
TrQuant::TrQuant( bool enableOpt ) : m_scalingListEnabled(false), m_quant( nullptr )
219
5.72k
{
220
  // allocate temporary buffers
221
5.72k
  m_plTempCoeff = ( TCoeff* ) xMalloc( TCoeff, MAX_TB_SIZEY * MAX_TB_SIZEY );
222
5.72k
  m_tmp         = ( TCoeff* ) xMalloc( TCoeff, MAX_TB_SIZEY * MAX_TB_SIZEY );
223
5.72k
  m_blk         = ( TCoeff* ) xMalloc( TCoeff, MAX_TB_SIZEY * MAX_TB_SIZEY );
224
225
40.0k
  for( int i = 0; i < NUM_TRAFO_MODES_MTS; i++ )
226
34.3k
  {
227
34.3k
    m_mtsCoeffs[i] = ( TCoeff* ) xMalloc( TCoeff, MAX_TB_SIZEY * MAX_TB_SIZEY );
228
34.3k
  }
229
230
5.72k
  {
231
5.72k
    m_invICT      = m_invICTMem + maxAbsIctMode;
232
5.72k
    m_invICT[ 0]  = invTransformCbCr< 0>;
233
5.72k
    m_invICT[ 1]  = invTransformCbCr< 1>;
234
5.72k
    m_invICT[-1]  = invTransformCbCr<-1>;
235
5.72k
    m_invICT[ 2]  = invTransformCbCr< 2>;
236
5.72k
    m_invICT[-2]  = invTransformCbCr<-2>;
237
5.72k
    m_invICT[ 3]  = invTransformCbCr< 3>;
238
5.72k
    m_invICT[-3]  = invTransformCbCr<-3>;
239
5.72k
    m_fwdICT      = m_fwdICTMem + maxAbsIctMode;
240
5.72k
    m_fwdICT[ 0]  = fwdTransformCbCr< 0>;
241
5.72k
    m_fwdICT[ 1]  = fwdTransformCbCr< 1>;
242
5.72k
    m_fwdICT[-1]  = fwdTransformCbCr<-1>;
243
5.72k
    m_fwdICT[ 2]  = fwdTransformCbCr< 2>;
244
5.72k
    m_fwdICT[-2]  = fwdTransformCbCr<-2>;
245
5.72k
    m_fwdICT[ 3]  = fwdTransformCbCr< 3>;
246
5.72k
    m_fwdICT[-3]  = fwdTransformCbCr<-3>;
247
5.72k
  }
248
249
5.72k
  m_invLfnstNxN = xInvLfnstNxNCore;
250
5.72k
  m_fwdLfnstNxN = xFwdLfnstNxNCore;
251
252
#if defined( TARGET_SIMD_X86 ) && ENABLE_SIMD_TRAFO
253
  if( enableOpt )
254
  {
255
    initTrQuantX86();
256
  }
257
#endif
258
#if defined( TARGET_SIMD_ARM ) && ENABLE_SIMD_TRAFO
259
  if( enableOpt )
260
  {
261
    initTrQuantARM();
262
  }
263
#endif
264
5.72k
}
265
266
TrQuant::~TrQuant()
267
5.72k
{
268
5.72k
  if( m_quant )
269
5.72k
  {
270
5.72k
    delete m_quant;
271
5.72k
    m_quant = nullptr;
272
5.72k
  }
273
274
  // delete temporary buffers
275
5.72k
  if( m_plTempCoeff )
276
5.72k
  {
277
5.72k
    xFree( m_plTempCoeff );
278
5.72k
    m_plTempCoeff = nullptr;
279
5.72k
  }
280
281
5.72k
  if( m_blk )
282
5.72k
  {
283
5.72k
    xFree( m_blk );
284
5.72k
    m_blk = nullptr;
285
5.72k
  }
286
287
5.72k
  if( m_tmp )
288
5.72k
  {
289
5.72k
    xFree( m_tmp );
290
5.72k
    m_tmp = nullptr;
291
5.72k
  }
292
293
40.0k
  for( int i = 0; i < NUM_TRAFO_MODES_MTS; i++ )
294
34.3k
  {
295
34.3k
     xFree( m_mtsCoeffs[i] );
296
34.3k
  }
297
5.72k
}
298
299
void TrQuant::xDeQuant(const TransformUnit& tu,
300
                             CoeffBuf      &dstCoeff,
301
                       const ComponentID   &compID,
302
                       const QpParam       &cQP)
303
982k
{
304
982k
  PROFILER_SCOPE_AND_STAGE( 1, _TPROF, P_DEQUANT );
305
982k
  m_quant->dequant( tu, dstCoeff, compID, cQP );
306
982k
}
307
308
void TrQuant::init( const Quant* otherQuant,
309
                    const int  rdoq,
310
                    const bool bUseRDOQTS,
311
                    const bool scalingListsEnabled,
312
                    const bool bEnc,
313
                    const int  thrVal,
314
                    const int  sbbSkipThrVal
315
)
316
5.72k
{
317
5.72k
  m_bEnc = bEnc;
318
319
5.72k
  delete m_quant;
320
5.72k
  m_quant = nullptr;
321
322
5.72k
  m_quant = new(std::nothrow) DepQuant( otherQuant, bEnc, scalingListsEnabled );
323
5.72k
  CHECK( !m_quant, "allocation failed" );
324
5.72k
  m_quant->init( rdoq, bUseRDOQTS, thrVal, sbbSkipThrVal );
325
5.72k
}
326
327
328
void TrQuant::invTransformNxN( TransformUnit& tu, const ComponentID compID, PelBuf& pResi, const QpParam& cQP )
329
982k
{
330
982k
  const CompArea& area    = tu.blocks[compID];
331
982k
  const uint32_t uiWidth  = area.width;
332
982k
  const uint32_t uiHeight = area.height;
333
334
982k
  CHECK( uiWidth > tu.cs->sps->getMaxTbSize() || uiHeight > tu.cs->sps->getMaxTbSize(), "Maximal allowed transformation size exceeded!" );
335
336
982k
  {
337
982k
    CoeffBuf tempCoeff = CoeffBuf( m_plTempCoeff, area );
338
982k
    xDeQuant( tu, tempCoeff, compID, cQP );
339
340
982k
    DTRACE_COEFF_BUF( D_TCOEFF, tempCoeff, tu, tu.cu->predMode, compID );
341
342
982k
    if (tu.cs->sps->LFNST)
343
982k
    {
344
982k
      xInvLfnst(tu, compID);
345
982k
    }
346
982k
    if (tu.mtsIdx[compID] == MTS_SKIP)
347
56.9k
    {
348
56.9k
      xITransformSkip(tempCoeff, pResi, tu, compID);
349
56.9k
    }
350
925k
    else
351
925k
    {
352
925k
      xIT(tu, compID, tempCoeff, pResi);
353
925k
    }
354
982k
  }
355
356
  //DTRACE_BLOCK_COEFF(tu.getCoeffs(compID), tu, tu.cu->predMode, compID);
357
982k
  DTRACE_PEL_BUF( D_RESIDUALS, pResi, tu, tu.cu->predMode, compID);
358
982k
}
359
360
std::pair<int64_t,int64_t> TrQuant::fwdTransformICT( const TransformUnit& tu, const PelBuf& resCb, const PelBuf& resCr, PelBuf& resC1, PelBuf& resC2, int jointCbCr )
361
490k
{
362
490k
  CHECK( Size(resCb) != Size(resCr), "resCb and resCr have different sizes" );
363
490k
  CHECK( Size(resCb) != Size(resC1), "resCb and resC1 have different sizes" );
364
490k
  CHECK( Size(resCb) != Size(resC2), "resCb and resC2 have different sizes" );
365
490k
  return (*m_fwdICT[ TU::getICTMode(tu, jointCbCr) ])( resCb, resCr, resC1, resC2 );
366
490k
}
367
368
void TrQuant::invTransformICT( const TransformUnit& tu, PelBuf& resCb, PelBuf& resCr )
369
314k
{
370
314k
  CHECK( Size(resCb) != Size(resCr), "resCb and resCr have different sizes" );
371
314k
  (*m_invICT[ TU::getICTMode(tu) ])( resCb, resCr );
372
314k
}
373
374
std::vector<int> TrQuant::selectICTCandidates( const TransformUnit& tu, CompStorage* resCb, CompStorage* resCr )
375
122k
{
376
122k
  CHECK( !resCb[0].valid() || !resCr[0].valid(), "standard components are not valid" );
377
378
122k
  if( !CU::isIntra( *tu.cu ) )
379
0
  {
380
0
    int cbfMask = 3;
381
0
    fwdTransformICT( tu, resCb[0], resCr[0], resCb[cbfMask], resCr[cbfMask], cbfMask );
382
0
    std::vector<int> cbfMasksToTest;
383
0
    cbfMasksToTest.push_back( cbfMask );
384
0
    return cbfMasksToTest;
385
0
  }
386
387
122k
  std::pair<int64_t,int64_t> pairDist[4];
388
612k
  for( int cbfMask = 0; cbfMask < 4; cbfMask++ )
389
490k
  {
390
490k
    pairDist[cbfMask] = fwdTransformICT( tu, resCb[0], resCr[0], resCb[cbfMask], resCr[cbfMask], cbfMask );
391
490k
  }
392
393
122k
  std::vector<int> cbfMasksToTest;
394
122k
  int64_t minDist1  = std::min<int64_t>( pairDist[0].first, pairDist[0].second );
395
122k
  int64_t minDist2  = std::numeric_limits<int64_t>::max();
396
122k
  int     cbfMask1  = 0;
397
122k
  int     cbfMask2  = 0;
398
122k
  for( int cbfMask : { 1, 2, 3 } )
399
367k
  {
400
367k
    if( pairDist[cbfMask].first < minDist1 )
401
242k
    {
402
242k
      cbfMask2  = cbfMask1; minDist2  = minDist1;
403
242k
      cbfMask1  = cbfMask;  minDist1  = pairDist[cbfMask1].first;
404
242k
    }
405
124k
    else if( pairDist[cbfMask].first < minDist2 )
406
122k
    {
407
122k
      cbfMask2  = cbfMask;  minDist2  = pairDist[cbfMask2].first;
408
122k
    }
409
367k
  }
410
122k
  if( cbfMask1 )
411
122k
  {
412
122k
    cbfMasksToTest.push_back( cbfMask1 );
413
122k
  }
414
122k
  if( cbfMask2 && ( ( minDist2 < (9*minDist1)/8 ) || ( !cbfMask1 && minDist2 < (3*minDist1)/2 ) ) )
415
0
  {
416
0
    cbfMasksToTest.push_back( cbfMask2 );
417
0
  }
418
419
122k
  return cbfMasksToTest;
420
122k
}
421
422
423
424
// ------------------------------------------------------------------------------------------------
425
// Logical transform
426
// ------------------------------------------------------------------------------------------------
427
void TrQuant::xSetTrTypes( const TransformUnit& tu, const ComponentID compID, const int width, const int height, int &trTypeHor, int &trTypeVer )
428
3.85M
{
429
3.85M
  const bool isISP = CU::isIntra(*tu.cu) && tu.cu->ispMode && isLuma(compID);
430
3.85M
  if (isISP && tu.cu->lfnstIdx)
431
21.7k
  {
432
21.7k
    return;
433
21.7k
  }
434
3.83M
  if (!tu.cs->sps->MTS)
435
0
  {
436
0
    return;
437
0
  }
438
3.83M
  if (CU::isIntra(*tu.cu) && isLuma(compID) && ((tu.cs->sps->getUseImplicitMTS() && tu.cu->lfnstIdx == 0 && tu.cu->mipFlag == 0) || tu.cu->ispMode))
439
126k
  {
440
126k
    if (width >= 4 && width <= 16)
441
51.5k
      trTypeHor = DST7;
442
126k
    if (height >= 4 && height <= 16)
443
49.7k
      trTypeVer = DST7;
444
126k
  }
445
3.70M
  else if( tu.cs->sps->MTS && tu.cu->sbtInfo && isLuma(compID)/*isSBT*/ )
446
0
  {
447
0
    const uint8_t sbtIdx = CU::getSbtIdx( tu.cu->sbtInfo );
448
0
    const uint8_t sbtPos = CU::getSbtPos( tu.cu->sbtInfo );
449
450
0
    if( sbtIdx == SBT_VER_HALF || sbtIdx == SBT_VER_QUAD )
451
0
    {
452
0
      assert( tu.lwidth() <= MTS_INTER_MAX_CU_SIZE );
453
0
      if( tu.lheight() > MTS_INTER_MAX_CU_SIZE )
454
0
      {
455
0
        trTypeHor = trTypeVer = DCT2;
456
0
      }
457
0
      else
458
0
      {
459
0
        if( sbtPos == SBT_POS0 )  { trTypeHor = DCT8;  trTypeVer = DST7; }
460
0
        else                      { trTypeHor = DST7;  trTypeVer = DST7; }
461
0
      }
462
0
    }
463
0
    else
464
0
    {
465
0
      assert( tu.lheight() <= MTS_INTER_MAX_CU_SIZE );
466
0
      if( tu.lwidth() > MTS_INTER_MAX_CU_SIZE )
467
0
      {
468
0
        trTypeHor = trTypeVer = DCT2;
469
0
      }
470
0
      else
471
0
      {
472
0
        if( sbtPos == SBT_POS0 )  { trTypeHor = DST7;  trTypeVer = DCT8; }
473
0
        else                      { trTypeHor = DST7;  trTypeVer = DST7; }
474
0
      }
475
0
    }
476
0
  }
477
3.83M
  const bool isExplicitMTS = (CU::isIntra(*tu.cu) ? tu.cs->sps->MTS : tu.cs->sps->MTSInter && CU::isInter(*tu.cu)) && isLuma(compID);
478
3.83M
  if (isExplicitMTS)
479
248k
  {
480
248k
    if (tu.mtsIdx[compID] > MTS_SKIP)
481
0
    {
482
0
      int indHor = (tu.mtsIdx[compID] - MTS_DST7_DST7) & 1;
483
0
      int indVer = (tu.mtsIdx[compID] - MTS_DST7_DST7) >> 1;
484
0
      trTypeHor  = indHor ? DCT8 : DST7;
485
0
      trTypeVer  = indVer ? DCT8 : DST7;
486
0
    }
487
248k
  }
488
3.83M
}
489
490
491
void TrQuant::xT( const TransformUnit& tu, const ComponentID compID, const CPelBuf& resi, CoeffBuf& dstCoeff, const int width, const int height )
492
882k
{
493
882k
  PROFILER_SCOPE_AND_STAGE( 1, _TPROF, P_TRAFO );
494
495
882k
  const unsigned maxLog2TrDynamicRange  = tu.cs->sps->getMaxLog2TrDynamicRange();
496
882k
  const unsigned bitDepth               = tu.cs->sps->bitDepths[toChannelType( compID )];
497
882k
  const int      TRANSFORM_MATRIX_SHIFT = g_transformMatrixShift[TRANSFORM_FORWARD];
498
882k
  const uint32_t transformWidthIndex    = Log2(width ) - 1;  // nLog2WidthMinus1, since transform start from 2-point
499
882k
  const uint32_t transformHeightIndex   = Log2(height) - 1;  // nLog2HeightMinus1, since transform start from 2-point
500
501
882k
  int trTypeHor = DCT2;
502
882k
  int trTypeVer = DCT2;
503
504
882k
  xSetTrTypes( tu, compID, width, height, trTypeHor, trTypeVer );
505
506
882k
  int  skipWidth  = ( trTypeHor != DCT2 && width  == 32 ) ? 16 : width  > JVET_C0024_ZERO_OUT_TH ? width  - JVET_C0024_ZERO_OUT_TH : 0;
507
882k
  int  skipHeight = ( trTypeVer != DCT2 && height == 32 ) ? 16 : height > JVET_C0024_ZERO_OUT_TH ? height - JVET_C0024_ZERO_OUT_TH : 0;
508
509
882k
  if( tu.cu->lfnstIdx )
510
33.5k
  {
511
33.5k
    if ((width == 4 && height > 4) || (width > 4 && height == 4))
512
4.91k
    {
513
4.91k
      skipWidth  = width - 4;
514
4.91k
      skipHeight = height - 4;
515
4.91k
    }
516
28.6k
    else if ((width >= 8 && height >= 8))
517
28.5k
    {
518
28.5k
      skipWidth  = width - 8;
519
28.5k
      skipHeight = height - 8;
520
28.5k
    }
521
33.5k
  }
522
523
882k
  TCoeff* block = m_blk;
524
882k
  TCoeff* tmp   = m_tmp;
525
526
882k
  const Pel* resiBuf    = resi.buf;
527
882k
  const int  resiStride = resi.stride;
528
529
882k
#if ENABLE_SIMD_TRAFO
530
  // the first stage of a 2-D transform may read the residual as it is
531
882k
  const auto fwdPel = width > 1 && height > 1 ? g_tCoeffOps.fastFwdPel[trTypeHor][transformWidthIndex] : nullptr;
532
533
882k
  if( !fwdPel && ( width & 3 ) )
534
0
#endif
535
0
  {
536
0
    for( int y = 0; y < height; y++ )
537
0
    {
538
0
      for( int x = 0; x < width; x++ )
539
0
      {
540
0
        block[( y * width ) + x] = resiBuf[( y * resiStride ) + x];
541
0
      }
542
0
    }
543
0
  }
544
882k
#if ENABLE_SIMD_TRAFO
545
882k
  else if( !fwdPel && ( width & 7 ) )
546
143k
  {
547
143k
    g_tCoeffOps.cpyCoeff4( resiBuf, resiStride, block, width, height );
548
143k
  }
549
739k
  else if( !fwdPel )
550
739k
  {
551
739k
    g_tCoeffOps.cpyCoeff8( resiBuf, resiStride, block, width, height );
552
739k
  }
553
882k
#endif //ENABLE_SIMD_TRAFO
554
555
882k
  if (width > 1 && height > 1)
556
881k
  {
557
881k
    const int shift_1st = ((Log2(width )) + bitDepth + TRANSFORM_MATRIX_SHIFT) - maxLog2TrDynamicRange;
558
881k
    const int shift_2nd =  (Log2(height))            + TRANSFORM_MATRIX_SHIFT;
559
881k
    CHECK( shift_1st < 0, "Negative shift" );
560
881k
    CHECK( shift_2nd < 0, "Negative shift" );
561
881k
#if ENABLE_SIMD_TRAFO
562
881k
    if( fwdPel )
563
0
    {
564
0
      fwdPel( resiBuf, resiStride, tmp, shift_1st, height, skipWidth );
565
0
    }
566
881k
    else
567
881k
#endif
568
881k
    {
569
881k
      fastFwdTrans[trTypeHor][transformWidthIndex](block, tmp, shift_1st, height, 0, skipWidth);
570
881k
    }
571
881k
    fastFwdTrans[trTypeVer][transformHeightIndex](tmp, dstCoeff.buf, shift_2nd, width, skipWidth, skipHeight);
572
    // The kernels leave the rows past the cut-off as they were. The second stage never reads those of the first stage. In the
573
    // coefficients, the RDOQ check reads 32 rows and the comparison of transforms reads blocks up to 32 rows, so 64 rows need no clear.
574
881k
    if( skipHeight && height < 64 )
575
20.8k
    {
576
20.8k
      memset( dstCoeff.buf + width * ( height - skipHeight ), 0, sizeof( TCoeff ) * width * skipHeight );
577
20.8k
    }
578
881k
  }
579
370
  else if (height == 1)   // 1-D horizontal transform
580
376
  {
581
376
    const int shift = ((Log2(width )) + bitDepth + TRANSFORM_MATRIX_SHIFT) - maxLog2TrDynamicRange;
582
376
    CHECK( shift < 0, "Negative shift" );
583
376
    fastFwdTrans[trTypeHor][transformWidthIndex](block, dstCoeff.buf, shift, 1, 0, skipWidth);
584
    // The RDOQ check reads the coefficients past the cut-off of a single row.
585
376
    if( skipWidth )
586
0
    {
587
0
      memset( dstCoeff.buf + width - skipWidth, 0, sizeof( TCoeff ) * skipWidth );
588
0
    }
589
376
  }
590
18.4E
  else   // if (iWidth == 1) //1-D vertical transform
591
18.4E
  {
592
18.4E
    int shift = ((floorLog2(height)) + bitDepth + TRANSFORM_MATRIX_SHIFT) - maxLog2TrDynamicRange;
593
18.4E
    CHECK(shift < 0, "Negative shift");
594
18.4E
    CHECKD((transformHeightIndex < 0), "There is a problem with the height.");
595
18.4E
    fastFwdTrans[trTypeVer][transformHeightIndex](block, dstCoeff.buf, shift, 1, 0, skipHeight);
596
    // The RDOQ check reads the coefficients past the cut-off of a single column up to row 32.
597
18.4E
    if( skipHeight )
598
0
    {
599
0
      memset( dstCoeff.buf + height - skipHeight, 0, sizeof( TCoeff ) * skipHeight );
600
0
    }
601
18.4E
  }
602
882k
}
603
604
605
// How many lines and rows of a coefficient block can hold coefficients, from the last scan position. The groups go by rising
606
// diagonals of the block, and within a diagonal from the bottom left. So all earlier groups lie on lower diagonals or to the left
607
// on this one, and in the first group the coefficients go by diagonals in the same way. Both round up to a multiple of 4, as the
608
// matrix kernels' rows have to be one, and the first stage's lines are the second stage's rows.
609
static inline void getCoeffExtent( const int lastPos, const int width, const int height, int& numLines, int& numRows )
610
325k
{
611
325k
  const ScanElement& last    = getScanOrder( SCAN_GROUPED_4x4, Log2( width ), Log2( height ) )[std::max( lastPos, 0 )];
612
325k
  const uint32_t*    log2Sbb = g_log2SbbSize[Log2( width )][Log2( height )];
613
325k
  const int          groupX  = last.x >> log2Sbb[0];
614
325k
  const int          groupY  = last.y >> log2Sbb[1];
615
325k
  const int          rank    = groupX + groupY;
616
617
325k
  int lines, rows;
618
325k
  if( rank == 0 )
619
319k
  {
620
319k
    lines = std::min<int>( last.x + last.y + 1, 1 << log2Sbb[0] );
621
319k
    rows  = std::min<int>( last.x + last.y + 1, 1 << log2Sbb[1] );
622
319k
  }
623
5.65k
  else
624
5.65k
  {
625
5.65k
    lines = std::max( rank, groupX + 1 ) << log2Sbb[0];
626
5.65k
    rows  = ( rank + 1 ) << log2Sbb[1];
627
5.65k
  }
628
629
325k
  numLines = std::min( width,  ( lines + 3 ) & ~3 );
630
325k
  numRows  = std::min( height, ( rows  + 3 ) & ~3 );
631
325k
}
632
633
void TrQuant::xIT( const TransformUnit& tu, const ComponentID compID, const CCoeffBuf& pCoeff, PelBuf& pResidual )
634
925k
{
635
925k
  PROFILER_SCOPE_AND_STAGE( 1, _TPROF, P_TRAFO );
636
637
925k
  const int      width                  = pCoeff.width;
638
925k
  const int      height                 = pCoeff.height;
639
925k
  const unsigned maxLog2TrDynamicRange  = tu.cs->sps->getMaxLog2TrDynamicRange();
640
925k
  const unsigned bitDepth               = tu.cs->sps->bitDepths[toChannelType( compID )];
641
925k
  const int      TRANSFORM_MATRIX_SHIFT = g_transformMatrixShift[TRANSFORM_INVERSE];
642
925k
  const TCoeff   clipMinimum            = -( 1 << maxLog2TrDynamicRange );
643
925k
  const TCoeff   clipMaximum            =  ( 1 << maxLog2TrDynamicRange ) - 1;
644
925k
  const uint32_t transformWidthIndex    = Log2(width )- 1;                                // nLog2WidthMinus1, since transform start from 2-point
645
925k
  const uint32_t transformHeightIndex   = Log2(height) - 1;                                // nLog2HeightMinus1, since transform start from 2-point
646
647
648
925k
  int trTypeHor = DCT2;
649
925k
  int trTypeVer = DCT2;
650
651
925k
  xSetTrTypes( tu, compID, width, height, trTypeHor, trTypeVer );
652
653
925k
  int skipWidth  = ( trTypeHor != DCT2 && width  == 32 ) ? 16 : width  > JVET_C0024_ZERO_OUT_TH ? width  - JVET_C0024_ZERO_OUT_TH : 0;
654
925k
  int skipHeight = ( trTypeVer != DCT2 && height == 32 ) ? 16 : height > JVET_C0024_ZERO_OUT_TH ? height - JVET_C0024_ZERO_OUT_TH : 0;
655
656
925k
  if (tu.cs->sps->LFNST && tu.cu->lfnstIdx)
657
600k
  {
658
600k
    if ((width == 4 && height > 4) || (width > 4 && height == 4))
659
164k
    {
660
164k
      skipWidth = width - 4;
661
164k
      skipHeight = height - 4;
662
164k
    }
663
435k
    else if ((width >= 8 && height >= 8))
664
388k
    {
665
388k
      skipWidth = width - 8;
666
388k
      skipHeight = height - 8;
667
388k
    }
668
600k
  }
669
670
925k
  TCoeff *block = m_blk;
671
925k
  TCoeff *tmp   = m_tmp;
672
925k
  if (width > 1 && height > 1)   // 2-D transform
673
925k
  {
674
925k
    const int shift_1st =   TRANSFORM_MATRIX_SHIFT + 1; // 1 has been added to shift_1st at the expense of shift_2nd
675
925k
    const int shift_2nd = ( TRANSFORM_MATRIX_SHIFT + maxLog2TrDynamicRange - 1 ) - bitDepth;
676
925k
    CHECK( shift_1st < 0, "Negative shift" );
677
925k
    CHECK( shift_2nd < 0, "Negative shift" );
678
    // LFNST writes its own region, which the zero-out above already gives. Otherwise the coefficients stop at the last scan position.
679
925k
    if( !tu.cu->lfnstIdx )
680
325k
    {
681
325k
      int numLines, numRows;
682
325k
      getCoeffExtent( tu.lastPos[compID], width, height, numLines, numRows );
683
325k
      skipWidth  = std::max( skipWidth , width  - numLines );
684
325k
      skipHeight = std::max( skipHeight, height - numRows  );
685
325k
    }
686
925k
    fastInvTrans[trTypeVer][transformHeightIndex](pCoeff.buf, tmp, shift_1st, width, skipWidth, skipHeight, clipMinimum, clipMaximum);
687
925k
#if ENABLE_SIMD_TRAFO
688
    // the matrix kernels stop at their cutoff, so only the butterflies read the lines the first stage skips
689
925k
    if( width <= 8 && skipWidth )
690
#else
691
    if( skipWidth )
692
#endif
693
149k
    {
694
149k
      memset( tmp + ( width - skipWidth ) * height, 0, skipWidth * height * sizeof( TCoeff ) );
695
149k
    }
696
925k
    fastInvTrans[trTypeHor][transformWidthIndex](tmp, block, shift_2nd, height, 0, skipWidth, clipMinimum, clipMaximum);
697
925k
  }
698
94
  else if (width == 1)   // 1-D vertical transform
699
0
  {
700
0
    int shift = (TRANSFORM_MATRIX_SHIFT + maxLog2TrDynamicRange - 1) - bitDepth;
701
0
    CHECK(shift < 0, "Negative shift");
702
0
    fastInvTrans[trTypeVer][transformHeightIndex](pCoeff.buf, block, shift + 1, 1, 0, skipHeight, clipMinimum, clipMaximum);
703
0
  }
704
94
  else   // if(iHeight == 1) //1-D horizontal transform
705
94
  {
706
94
    const int shift = (TRANSFORM_MATRIX_SHIFT + maxLog2TrDynamicRange - 1) - bitDepth;
707
94
    CHECK(shift < 0, "Negative shift");
708
94
    fastInvTrans[trTypeHor][transformWidthIndex](pCoeff.buf, block, shift + 1, 1, 0, skipWidth, clipMinimum, clipMaximum);
709
94
  }
710
711
925k
#if ENABLE_SIMD_TRAFO
712
925k
  if( width & 3 )
713
0
#endif //ENABLE_SIMD_TRAFO
714
0
  {
715
0
    Pel       *dst    = pResidual.buf;
716
0
    ptrdiff_t  stride = pResidual.stride;
717
718
0
    for( int y = 0; y < height; y++ )
719
0
    {
720
0
      for( int x = 0; x < width; x++ )
721
0
      {
722
0
        dst[x] = ( Pel ) *block++;
723
0
      }
724
725
0
      dst += stride;
726
0
    }
727
0
  }
728
925k
#if ENABLE_SIMD_TRAFO
729
925k
  else if( width & 7 )
730
195k
  {
731
195k
    g_tCoeffOps.cpyResi4( block, pResidual.buf, pResidual.stride, width, height );
732
195k
  }
733
730k
  else
734
730k
  {
735
730k
    g_tCoeffOps.cpyResi8( block, pResidual.buf, pResidual.stride, width, height );
736
730k
  }
737
925k
#endif //ENABLE_SIMD_TRAFO
738
925k
}
739
740
/** Wrapper function between HM interface and core NxN transform skipping
741
 */
742
void TrQuant::xITransformSkip(const CCoeffBuf& pCoeff,
743
  PelBuf& pResidual,
744
  const TransformUnit& tu,
745
  const ComponentID compID)
746
56.9k
{
747
56.9k
  const CompArea& area = tu.blocks[compID];
748
56.9k
  const int width = area.width;
749
56.9k
  const int height = area.height;
750
751
56.9k
#if ENABLE_SIMD_TRAFO
752
56.9k
  if( ( width & 3 ) == 0 )
753
56.9k
  {
754
    // the dequantized coefficients are within 16 bit, where the saturation of the SIMD copies equals the cast below
755
56.9k
    CHECKD( pCoeff.stride != width, "the coefficients have to be stored without padding" );
756
56.9k
    ( width & 7 ? g_tCoeffOps.cpyResi4 : g_tCoeffOps.cpyResi8 )( pCoeff.buf, pResidual.buf, pResidual.stride, width, height );
757
56.9k
    return;
758
56.9k
  }
759
0
#endif
760
761
0
  for (uint32_t y = 0; y < height; y++)
762
0
  {
763
0
    for (uint32_t x = 0; x < width; x++)
764
0
    {
765
0
      pResidual.at(x, y) = Pel(pCoeff.at(x, y));
766
0
    }
767
0
  }
768
0
}
769
770
void TrQuant::xQuant(TransformUnit& tu, const ComponentID compID, const CCoeffBuf& pSrc, TCoeff &uiAbsSum, const QpParam& cQP, const Ctx& ctx)
771
2.37M
{
772
2.37M
  PROFILER_SCOPE_AND_STAGE( 1, _TPROF, P_QUANT );
773
2.37M
  m_quant->quant( tu, compID, pSrc, uiAbsSum, cQP, ctx );
774
#if ENABLE_MEASURE_SEARCH_SPACE
775
776
  g_searchSpaceAcc.addQuant( tu, toChannelType( compID ) );
777
#endif
778
2.37M
}
779
780
781
void TrQuant::transformNxN(TransformUnit &tu, const ComponentID compID, const QpParam &cQP, TCoeff &uiAbsSum, const Ctx &ctx, const bool loadTr, const LfnstPrimary lfnstPrimary)
782
2.37M
{
783
2.37M
        CodingStructure &cs = *tu.cs;
784
2.37M
  const CompArea& rect      = tu.blocks[compID];
785
2.37M
  const uint32_t uiWidth        = rect.width;
786
2.37M
  const uint32_t uiHeight       = rect.height;
787
788
2.37M
  const CPelBuf resiBuf     = cs.getResiBuf(rect);
789
790
2.37M
  if( tu.noResidual )
791
0
  {
792
0
    uiAbsSum = 0;
793
0
    TU::setCbfAtDepth( tu, compID, tu.depth, uiAbsSum > 0 );
794
0
    return;
795
0
  }
796
2.37M
  if (tu.cu->bdpcmM[toChannelType(compID)])
797
117k
  {
798
117k
    tu.mtsIdx[compID] = MTS_SKIP;
799
117k
  }
800
801
2.37M
  uiAbsSum = 0;
802
2.37M
  CHECK( cs.sps->getMaxTbSize() < uiWidth, "Unsupported transformation size" );
803
804
2.37M
  CoeffBuf tempCoeff(loadTr ? m_mtsCoeffs[tu.mtsIdx[compID]] : m_plTempCoeff, rect);
805
2.37M
  if (!loadTr && lfnstPrimary != LFNST_PRIMARY_KNOWN)
806
2.31M
  {
807
2.31M
    DTRACE_PEL_BUF( D_RESIDUALS, resiBuf, tu, tu.cu->predMode, compID );
808
2.31M
    if (tu.mtsIdx[compID] == MTS_SKIP)
809
117k
    {
810
117k
      xTransformSkip(tu, compID, resiBuf, tempCoeff.buf);
811
117k
    }
812
2.19M
    else if( m_keepPrimary )
813
2.04M
    {
814
2.04M
      xTOrKept(tu, compID, resiBuf, tempCoeff, uiWidth, uiHeight);
815
2.04M
    }
816
149k
    else
817
149k
    {
818
149k
      xT(tu, compID, resiBuf, tempCoeff, uiWidth, uiHeight);
819
149k
    }
820
2.31M
  }
821
2.37M
  if (cs.sps->LFNST)
822
2.37M
  {
823
2.37M
    xFwdLfnst(tu, compID, loadTr, lfnstPrimary);
824
2.37M
  }
825
2.37M
  DTRACE_COEFF_BUF( D_TCOEFF, tempCoeff, tu, tu.cu->predMode, compID );
826
827
2.37M
  xQuant( tu, compID, tempCoeff, uiAbsSum, cQP, ctx );
828
829
2.37M
  DTRACE_COEFF_BUF( D_TCOEFF, tu.getCoeffs( compID ), tu, tu.cu->predMode, compID );
830
831
  // set coded block flag (CBF)
832
2.37M
  TU::setCbfAtDepth (tu, compID, tu.depth, uiAbsSum > 0);
833
2.37M
}
834
835
void TrQuant::keepPrimaryCoeffs( const bool keep )
836
785k
{
837
785k
  m_keepPrimary = keep;
838
785k
  for( KeptPrimaryCoeffs& kept : m_keptPrimary )
839
4.71M
  {
840
4.71M
    kept.valid = false;
841
4.71M
  }
842
785k
}
843
844
// The size is a template parameter because compilers inline a memcpy of known size. A call per row would cost about as much
845
// as the transform this is there to save.
846
template<int size>
847
static inline void copyCoeffSquare( TCoeff* dst, const int dstStride, const TCoeff* src, const int srcStride )
848
2.04M
{
849
15.6M
  for( int y = 0; y < size; y++ )
850
13.5M
  {
851
13.5M
    memcpy( dst + y * dstStride, src + y * srcStride, size * sizeof( TCoeff ) );
852
13.5M
  }
853
2.04M
}
TrQuant.cpp:void vvenc::copyCoeffSquare<8>(int*, int, int const*, int)
Line
Count
Source
848
1.34M
{
849
12.1M
  for( int y = 0; y < size; y++ )
850
10.7M
  {
851
10.7M
    memcpy( dst + y * dstStride, src + y * srcStride, size * sizeof( TCoeff ) );
852
10.7M
  }
853
1.34M
}
TrQuant.cpp:void vvenc::copyCoeffSquare<4>(int*, int, int const*, int)
Line
Count
Source
848
700k
{
849
3.50M
  for( int y = 0; y < size; y++ )
850
2.80M
  {
851
2.80M
    memcpy( dst + y * dstStride, src + y * srcStride, size * sizeof( TCoeff ) );
852
2.80M
  }
853
700k
}
854
855
// With an LFNST index the primary transform stops at the coefficients LFNST reads, and these don't depend on the index.
856
// They are kept from the first transform of a residual, with or without LFNST, if the next one has the same transform type.
857
void TrQuant::xTOrKept( const TransformUnit& tu, const ComponentID compID, const CPelBuf& resi, CoeffBuf& dstCoeff, const int width, const int height )
858
2.04M
{
859
2.04M
  int trTypeHor = DCT2;
860
2.04M
  int trTypeVer = DCT2;
861
2.04M
  xSetTrTypes( tu, compID, width, height, trTypeHor, trTypeVer );
862
863
2.04M
  KeptPrimaryCoeffs& kept   = m_keptPrimary[tu.jointCbCr ? MAX_NUM_COMP + tu.jointCbCr - 1 : compID];
864
2.04M
  const int          keptW  = 8;   // kept.coeffs has room for 8 x 8 coefficients
865
2.04M
  const bool         big    = width >= 8 && height >= 8;
866
2.04M
  const int          sbSize = big ? 8 : 4;
867
868
2.04M
  if( tu.cu->lfnstIdx && kept.valid && kept.trTypeHor == trTypeHor && kept.trTypeVer == trTypeVer )
869
1.33M
  {
870
    // Like xT, which doesn't clear the rows past the cut-off of a block of 64 rows.
871
1.33M
    memset( dstCoeff.buf, 0, sizeof( TCoeff ) * width * ( height < 64 ? height : sbSize ) );
872
1.33M
    if( big )
873
870k
    {
874
870k
      copyCoeffSquare<8>( dstCoeff.buf, width, kept.coeffs, keptW );
875
870k
    }
876
466k
    else
877
466k
    {
878
466k
      copyCoeffSquare<4>( dstCoeff.buf, width, kept.coeffs, keptW );
879
466k
    }
880
1.33M
    return;
881
1.33M
  }
882
883
712k
  xT( tu, compID, resi, dstCoeff, width, height );
884
885
712k
  if( big )
886
479k
  {
887
479k
    copyCoeffSquare<8>( kept.coeffs, keptW, dstCoeff.buf, width );
888
479k
  }
889
233k
  else
890
233k
  {
891
233k
    copyCoeffSquare<4>( kept.coeffs, keptW, dstCoeff.buf, width );
892
233k
  }
893
712k
  kept.trTypeHor = trTypeHor;
894
712k
  kept.trTypeVer = trTypeVer;
895
712k
  kept.valid     = true;
896
712k
}
897
898
void TrQuant::checktransformsNxN( TransformUnit &tu, TrModeList *trModes, const int maxCand, const ComponentID compID)
899
20.1k
{
900
20.1k
  CodingStructure &cs     = *tu.cs;
901
20.1k
  const CompArea& rect    = tu.blocks[compID];
902
20.1k
  const uint32_t   width  = rect.width;
903
20.1k
  const uint32_t   height = rect.height;
904
905
20.1k
  const CPelBuf resiBuf = cs.getResiBuf(rect);
906
907
20.1k
  CHECK(cs.sps->getMaxTbSize() < width, "Unsupported transformation size");
908
20.1k
  int                           pos = 0;
909
20.1k
  TrCostList                    trCosts;
910
20.1k
  TrModeList::iterator          it      = trModes->begin();
911
20.1k
  const double                  facBB[] = { 1.2, 1.3, 1.3, 1.4, 1.5 };
912
60.4k
  while (it != trModes->end())
913
40.3k
  {
914
40.3k
    tu.mtsIdx[compID] = it->first;
915
40.3k
    CoeffBuf tempCoeff(m_mtsCoeffs[tu.mtsIdx[compID]], rect);
916
40.3k
    if (tu.noResidual)
917
0
    {
918
0
      int sumAbs = 0;
919
0
      trCosts.push_back(TrCost(sumAbs, pos++));
920
0
      it++;
921
0
      continue;
922
0
    }
923
40.3k
    if (tu.mtsIdx[compID] == MTS_SKIP)
924
20.1k
    {
925
20.1k
      xTransformSkip(tu, compID, resiBuf, tempCoeff.buf);
926
20.1k
    }
927
20.1k
    else
928
20.1k
    {
929
20.1k
      xT(tu, compID, resiBuf, tempCoeff, width, height);
930
20.1k
    }
931
932
40.3k
    int sumAbs = 0;
933
7.09M
    for (int pos = 0; pos < width * height; pos++)
934
7.05M
    {
935
7.05M
      sumAbs += abs(tempCoeff.buf[pos]);
936
7.05M
    }
937
938
40.3k
    double scaleSAD = 1.0;
939
40.3k
    if (tu.mtsIdx[compID] == MTS_SKIP && ((floorLog2(width) + floorLog2(height)) & 1) == 1)
940
8.45k
    {
941
8.45k
      scaleSAD = 1.0 / 1.414213562;   // compensate for not scaling transform skip coefficients by 1/sqrt(2)
942
8.45k
    }
943
40.3k
    if (tu.mtsIdx[compID] == MTS_SKIP)
944
20.1k
    {
945
20.1k
      int trShift = getTransformShift(tu.cu->slice->sps->bitDepths[CH_L], rect.size(), tu.cu->slice->sps->getMaxLog2TrDynamicRange());
946
20.1k
      scaleSAD *= pow(2, trShift);
947
20.1k
    }
948
40.3k
    trCosts.push_back(TrCost(int(std::min<double>(sumAbs * scaleSAD, std::numeric_limits<int>::max())), pos++));
949
40.3k
    it++;
950
40.3k
  }
951
952
20.1k
  int                           numTests = 0;
953
20.1k
  TrCostList::iterator          itC      = trCosts.begin();
954
20.1k
  const double                  fac      = facBB[std::max(0, floorLog2(std::max(width, height)) - 2)];
955
20.1k
  const double                  thr      = fac * trCosts.begin()->first;
956
20.1k
  const double                  thrTS    = trCosts.begin()->first;
957
60.4k
  while (itC != trCosts.end())
958
40.3k
  {
959
40.3k
    const bool testTr               = itC->first <= (trModes->at(itC->second).first == 1 ? thrTS : thr) && numTests <= maxCand;
960
40.3k
    trModes->at(itC->second).second = testTr;
961
40.3k
    numTests += testTr;
962
40.3k
    itC++;
963
40.3k
  }
964
20.1k
}
965
966
uint32_t TrQuant::xGetLFNSTIntraMode( const Area& tuArea, const uint32_t dirMode )
967
2.00M
{
968
2.00M
  if (dirMode < 2)
969
1.12M
  {
970
1.12M
    return dirMode;
971
1.12M
  }
972
973
881k
  static const int modeShift[] = { 0, 6, 10, 12, 14, 15 };
974
975
881k
  const int width  = int(tuArea.width);
976
881k
  const int height = int(tuArea.height);
977
978
881k
  if (width > height && dirMode < 2 + modeShift[floorLog2(width) - floorLog2(height)])
979
302
  {
980
302
    return dirMode + (VDIA_IDX - 1) + (NUM_EXT_LUMA_MODE >> 1);
981
302
  }
982
881k
  else if (height > width && dirMode > VDIA_IDX - modeShift[floorLog2(height) - floorLog2(width)])
983
82.7k
  {
984
82.7k
    return dirMode - (VDIA_IDX + 1) + (NUM_EXT_LUMA_MODE >> 1) + NUM_LUMA_MODE;
985
82.7k
  }
986
987
798k
  return dirMode;
988
881k
}
989
990
991
bool TrQuant::xGetTransposeFlag(uint32_t intraMode)
992
2.00M
{
993
2.00M
  return ((intraMode >= NUM_LUMA_MODE) && (intraMode >= (NUM_LUMA_MODE + (NUM_EXT_LUMA_MODE >> 1))))
994
2.00M
         || ((intraMode < NUM_LUMA_MODE) && (intraMode > DIA_IDX));
995
2.00M
}
996
997
998
void TrQuant::xInvLfnst(const TransformUnit &tu, const ComponentID compID)
999
982k
{
1000
982k
  const CompArea &area     = tu.blocks[compID];
1001
982k
  const uint32_t  width    = area.width;
1002
982k
  const uint32_t  height   = area.height;
1003
982k
  const uint32_t  lfnstIdx = tu.cu->lfnstIdx;
1004
982k
  if (lfnstIdx && tu.mtsIdx[compID] != MTS_SKIP && (CU::isSepTree(*tu.cu) ? true : isLuma(compID)))
1005
600k
  {
1006
600k
    const CodingUnit& cu = *tu.cu;
1007
600k
    CHECKD( tu.cs->getCU( area.pos(), toChannelType( compID ), TREE_D ) != tu.cu, "the TU's CU is not the CU at its position" );
1008
600k
    const bool         whge3 = width >= 8 && height >= 8;
1009
600k
    const ScanElement *scan =
1010
600k
      whge3
1011
600k
        ? g_coefTopLeftDiagScan8x8[Log2(width)] 
1012
600k
        : getScanOrder(SCAN_GROUPED_4x4, Log2(area.width), Log2(area.height));
1013
600k
    uint32_t intraMode = CU::getFinalIntraMode(cu, toChannelType(compID));
1014
1015
600k
    if (CU::isLMCMode( cu.intraDir[toChannelType(compID)]))
1016
99.5k
    {
1017
99.5k
      intraMode = CU::getCoLocatedIntraLumaMode(cu);
1018
99.5k
    }
1019
600k
    if (CU::isMIP(cu, toChannelType(compID)))
1020
5.78k
    {
1021
5.78k
      intraMode = PLANAR_IDX;
1022
5.78k
    }
1023
600k
    CHECK(intraMode >= NUM_INTRA_MODE - 1, "Invalid intra mode");
1024
1025
600k
    if (lfnstIdx < 3)
1026
600k
    {
1027
600k
      if (tu.cu->ispMode && isLuma(compID))
1028
8.42k
      {
1029
8.42k
        intraMode = xGetLFNSTIntraMode(tu.cu->blocks[compID], intraMode);
1030
8.42k
      }
1031
591k
      else
1032
591k
        intraMode = xGetLFNSTIntraMode(tu.blocks[compID], intraMode);
1033
600k
      bool      transposeFlag = xGetTransposeFlag(intraMode);
1034
600k
      const int sbSize        = whge3 ? 8 : 4;
1035
600k
      bool      tu4x4Flag     = (width == 4 && height == 4);
1036
600k
      bool      tu8x8Flag     = (width == 8 && height == 8);
1037
600k
      TCoeff *  lfnstTemp;
1038
600k
      TCoeff *  coeffTemp;
1039
600k
      int       y;
1040
600k
      lfnstTemp                  = m_tempInMatrix;   // inverse spectral rearrangement
1041
600k
      coeffTemp                  = m_plTempCoeff;
1042
600k
      TCoeff *           dst     = lfnstTemp;
1043
600k
      const ScanElement *scanPtr = scan;
1044
10.2M
      for (y = 0; y < 16; y++)
1045
9.60M
      {
1046
9.60M
        *dst++ = coeffTemp[scanPtr->idx];
1047
9.60M
        scanPtr++;
1048
9.60M
      }
1049
1050
600k
      m_invLfnstNxN( m_tempInMatrix, m_tempOutMatrix, g_lfnstLut[intraMode], lfnstIdx - 1, sbSize, ( tu4x4Flag || tu8x8Flag ) ? 8 : 16 );
1051
1052
600k
      lfnstTemp = m_tempOutMatrix;   // inverse spectral rearrangement
1053
1054
600k
      if (transposeFlag)
1055
54.5k
      {
1056
54.5k
        if (sbSize == 4)
1057
9.54k
        {
1058
47.7k
          for (y = 0; y < 4; y++)
1059
38.1k
          {
1060
38.1k
            coeffTemp[0] = lfnstTemp[0];
1061
38.1k
            coeffTemp[1] = lfnstTemp[4];
1062
38.1k
            coeffTemp[2] = lfnstTemp[8];
1063
38.1k
            coeffTemp[3] = lfnstTemp[12];
1064
38.1k
            lfnstTemp++;
1065
38.1k
            coeffTemp += width;
1066
38.1k
          }
1067
9.54k
        }
1068
44.9k
        else   // ( sbSize == 8 )
1069
44.9k
        {
1070
404k
          for (y = 0; y < 8; y++)
1071
359k
          {
1072
359k
            coeffTemp[0] = lfnstTemp[0];
1073
359k
            coeffTemp[1] = lfnstTemp[8];
1074
359k
            coeffTemp[2] = lfnstTemp[16];
1075
359k
            coeffTemp[3] = lfnstTemp[24];
1076
359k
            if (y < 4)
1077
179k
            {
1078
179k
              coeffTemp[4] = lfnstTemp[32];
1079
179k
              coeffTemp[5] = lfnstTemp[36];
1080
179k
              coeffTemp[6] = lfnstTemp[40];
1081
179k
              coeffTemp[7] = lfnstTemp[44];
1082
179k
            }
1083
359k
            lfnstTemp++;
1084
359k
            coeffTemp += width;
1085
359k
          }
1086
44.9k
        }
1087
54.5k
      }
1088
545k
      else
1089
545k
      {
1090
        // a constant size per row lets the compiler copy each row inline, without dispatching on the size
1091
545k
        if (sbSize == 4)
1092
202k
        {
1093
1.01M
          for (y = 0; y < 4; y++, lfnstTemp += 4, coeffTemp += width)
1094
809k
          {
1095
809k
            ::memcpy(coeffTemp, lfnstTemp, 4 * sizeof(TCoeff));
1096
809k
          }
1097
202k
        }
1098
343k
        else   // ( sbSize == 8 ): rows 0-3 hold 8 coefficients, rows 4-7 hold 4
1099
343k
        {
1100
1.71M
          for (y = 0; y < 4; y++, lfnstTemp += 8, coeffTemp += width)
1101
1.37M
          {
1102
1.37M
            ::memcpy(coeffTemp, lfnstTemp, 8 * sizeof(TCoeff));
1103
1.37M
          }
1104
1.71M
          for (; y < 8; y++, lfnstTemp += 4, coeffTemp += width)
1105
1.37M
          {
1106
1.37M
            ::memcpy(coeffTemp, lfnstTemp, 4 * sizeof(TCoeff));
1107
1.37M
          }
1108
343k
        }
1109
545k
      }
1110
600k
    }
1111
600k
  }
1112
982k
}
1113
1114
1115
void TrQuant::xFwdLfnst(const TransformUnit &tu, const ComponentID compID, const bool loadTr, const LfnstPrimary lfnstPrimary)
1116
2.37M
{
1117
2.37M
  const CompArea &area     = tu.blocks[compID];
1118
2.37M
  const uint32_t  width    = area.width;
1119
2.37M
  const uint32_t  height   = area.height;
1120
2.37M
  const uint32_t  lfnstIdx = tu.cu->lfnstIdx;
1121
2.37M
  if ((lfnstIdx || lfnstPrimary == LFNST_PRIMARY_KEEP) && tu.mtsIdx[compID] != MTS_SKIP && (CU::isSepTree(*tu.cu) ? true : isLuma(compID)))
1122
1.40M
  {
1123
1.40M
    const CodingUnit& cu = *tu.cu;
1124
1.40M
    CHECKD( tu.cs->getCU( area.pos(), toChannelType( compID ), TREE_D ) != tu.cu, "the TU's CU is not the CU at its position" );
1125
1.40M
    const bool         whge3 = width >= 8 && height >= 8;
1126
1.40M
    const ScanElement *scan =
1127
1.40M
      whge3
1128
1.40M
        ? g_coefTopLeftDiagScan8x8[Log2(width)] 
1129
1.40M
        : getScanOrder(SCAN_GROUPED_4x4, Log2(area.width), Log2(area.height));   
1130
1.40M
    uint32_t intraMode = CU::getFinalIntraMode(cu, toChannelType(compID));
1131
1132
1.40M
    if (CU::isLMCMode(cu.intraDir[toChannelType(compID)]))
1133
123k
    {
1134
123k
      intraMode = CU::getCoLocatedIntraLumaMode(cu);
1135
123k
    }
1136
1.40M
    if (CU::isMIP(cu, toChannelType(compID)))
1137
9.59k
    {
1138
9.59k
      intraMode = PLANAR_IDX;
1139
9.59k
    }
1140
1.40M
    CHECK(intraMode >= NUM_INTRA_MODE - 1, "Invalid intra mode");
1141
1142
1.40M
    if (lfnstIdx < 3)
1143
1.40M
    {
1144
1.40M
      if (tu.cu->ispMode && isLuma(compID))
1145
13.3k
      {
1146
13.3k
        intraMode = xGetLFNSTIntraMode(tu.cu->blocks[compID], intraMode);
1147
13.3k
      }
1148
1.39M
      else
1149
1.39M
      {
1150
1.39M
        intraMode = xGetLFNSTIntraMode(tu.blocks[compID], intraMode);
1151
1.39M
      }
1152
1.40M
      bool      transposeFlag = xGetTransposeFlag(intraMode);
1153
1.40M
      const int sbSize        = whge3 ? 8 : 4;
1154
1.40M
      bool      tu4x4Flag     = (width == 4 && height == 4);
1155
1.40M
      bool      tu8x8Flag     = (width == 8 && height == 8);
1156
1.40M
      TCoeff*   lfnstTemp;
1157
1.40M
      TCoeff*   coeffTemp;
1158
1.40M
      TCoeff*   tempCoeff     = loadTr ? m_mtsCoeffs[tu.mtsIdx[compID]] : m_plTempCoeff;
1159
1160
1.40M
      int y;
1161
1.40M
      lfnstTemp = m_lfnstFwdIn;   // forward low frequency non-separable transform
1162
1.40M
      coeffTemp = tempCoeff;
1163
1164
1.40M
      if (lfnstPrimary == LFNST_PRIMARY_KNOWN)
1165
37.1k
      {
1166
        // m_lfnstFwdIn holds it, and only the first coefficient group is quantized: the rest of the block isn't read
1167
37.1k
      }
1168
1.37M
      else if (transposeFlag)
1169
303k
      {
1170
303k
        if (sbSize == 4)
1171
86.1k
        {
1172
430k
          for (y = 0; y < 4; y++)
1173
344k
          {
1174
344k
            lfnstTemp[0]  = coeffTemp[0];
1175
344k
            lfnstTemp[4]  = coeffTemp[1];
1176
344k
            lfnstTemp[8]  = coeffTemp[2];
1177
344k
            lfnstTemp[12] = coeffTemp[3];
1178
344k
            lfnstTemp++;
1179
344k
            coeffTemp += width;
1180
344k
          }
1181
86.1k
        }
1182
217k
        else   // ( sbSize == 8 )
1183
217k
        {
1184
1.95M
          for (y = 0; y < 8; y++)
1185
1.74M
          {
1186
1.74M
            lfnstTemp[0]  = coeffTemp[0];
1187
1.74M
            lfnstTemp[8]  = coeffTemp[1];
1188
1.74M
            lfnstTemp[16] = coeffTemp[2];
1189
1.74M
            lfnstTemp[24] = coeffTemp[3];
1190
1.74M
            if (y < 4)
1191
870k
            {
1192
870k
              lfnstTemp[32] = coeffTemp[4];
1193
870k
              lfnstTemp[36] = coeffTemp[5];
1194
870k
              lfnstTemp[40] = coeffTemp[6];
1195
870k
              lfnstTemp[44] = coeffTemp[7];
1196
870k
            }
1197
1.74M
            lfnstTemp++;
1198
1.74M
            coeffTemp += width;
1199
1.74M
          }
1200
217k
        }
1201
303k
      }
1202
1.06M
      else
1203
1.06M
      {
1204
        // a constant size per row lets the compiler copy each row inline, without dispatching on the size
1205
1.06M
        if (sbSize == 4)
1206
385k
        {
1207
1.92M
          for (y = 0; y < 4; y++, lfnstTemp += 4, coeffTemp += width)
1208
1.54M
          {
1209
1.54M
            ::memcpy(lfnstTemp, coeffTemp, 4 * sizeof(TCoeff));
1210
1.54M
          }
1211
385k
        }
1212
681k
        else   // ( sbSize == 8 ): rows 0-3 hold 8 coefficients, rows 4-7 hold 4
1213
681k
        {
1214
3.40M
          for (y = 0; y < 4; y++, lfnstTemp += 8, coeffTemp += width)
1215
2.72M
          {
1216
2.72M
            ::memcpy(lfnstTemp, coeffTemp, 8 * sizeof(TCoeff));
1217
2.72M
          }
1218
3.40M
          for (; y < 8; y++, lfnstTemp += 4, coeffTemp += width)
1219
2.72M
          {
1220
2.72M
            ::memcpy(lfnstTemp, coeffTemp, 4 * sizeof(TCoeff));
1221
2.72M
          }
1222
681k
        }
1223
1.06M
      }
1224
1225
1.40M
      if (!lfnstIdx)
1226
0
      {
1227
0
        return;
1228
0
      }
1229
1230
1.40M
      const int zeroOutSize = (tu4x4Flag || tu8x8Flag) ? 8 : 16;   // outputs that can be nonzero; the transform zeroes the rest
1231
1.40M
      m_fwdLfnstNxN( m_lfnstFwdIn, m_tempOutMatrix, g_lfnstLut[intraMode], lfnstIdx - 1, sbSize, zeroOutSize );
1232
1233
      // forward spectral rearrangement: the first 16 (sbSize 4) or 48 (sbSize 8) positions of the scan take the outputs.
1234
      // They are the top-left 4x4 group, or the 8x8 group without its bottom-right 4x4: where some outputs are zero,
1235
      // clear that region row by row and place only the first zeroOutSize outputs.
1236
1.40M
      coeffTemp = tempCoeff;
1237
1.40M
      if (sbSize == 4)
1238
471k
      {
1239
471k
        if (zeroOutSize < 16)
1240
87.0k
        {
1241
435k
          for (y = 0; y < 4; y++)
1242
348k
          {
1243
348k
            ::memset(coeffTemp + y * width, 0, 4 * sizeof(TCoeff));
1244
348k
          }
1245
87.0k
        }
1246
471k
      }
1247
935k
      else   // ( sbSize == 8 ): 48 positions, rows 0-3 of 8 and rows 4-7 of 4
1248
935k
      {
1249
4.67M
        for (y = 0; y < 4; y++)
1250
3.74M
        {
1251
3.74M
          ::memset(coeffTemp + y * width, 0, 8 * sizeof(TCoeff));
1252
3.74M
        }
1253
4.67M
        for (; y < 8; y++)
1254
3.74M
        {
1255
3.74M
          ::memset(coeffTemp + y * width, 0, 4 * sizeof(TCoeff));
1256
3.74M
        }
1257
935k
      }
1258
21.8M
      for (y = 0; y < zeroOutSize; y++)
1259
20.4M
      {
1260
20.4M
        coeffTemp[scan[y].idx] = m_tempOutMatrix[y];
1261
20.4M
      }
1262
1.40M
    }
1263
1.40M
  }
1264
2.37M
}
1265
1266
void TrQuant::xTransformSkip(const TransformUnit& tu, const ComponentID& compID, const CPelBuf& resi, TCoeff* psCoeff)
1267
137k
{
1268
137k
  const CompArea& rect = tu.blocks[compID];
1269
137k
  const uint32_t width = rect.width;
1270
137k
  const uint32_t height = rect.height;
1271
1272
137k
#if ENABLE_SIMD_TRAFO
1273
137k
  if( ( width & 3 ) == 0 )
1274
137k
  {
1275
137k
    ( width & 7 ? g_tCoeffOps.cpyCoeff4 : g_tCoeffOps.cpyCoeff8 )( resi.buf, resi.stride, psCoeff, width, height );
1276
137k
    return;
1277
137k
  }
1278
0
#endif
1279
1280
0
  for (uint32_t y = 0, coefficientIndex = 0; y < height; y++)
1281
0
  {
1282
0
    for (uint32_t x = 0; x < width; x++, coefficientIndex++)
1283
0
    {
1284
0
      psCoeff[coefficientIndex] = TCoeff(resi.at(x, y));
1285
0
    }
1286
0
  }
1287
0
}
1288
} // namespace vvenc
1289
1290
//! \}
1291