/work/vvdec/source/Lib/CommonLib/MatrixIntraPrediction.cpp
Line | Count | Source |
1 | | /* ----------------------------------------------------------------------------- |
2 | | The copyright in this software is being made available under the Clear BSD |
3 | | License, included below. No patent rights, trademark rights and/or |
4 | | other Intellectual Property Rights other than the copyrights concerning |
5 | | the Software are granted under this license. |
6 | | |
7 | | The Clear BSD License |
8 | | |
9 | | Copyright (c) 2018-2026, Fraunhofer-Gesellschaft zur Förderung der angewandten Forschung e.V. & The VVdeC Authors. |
10 | | All rights reserved. |
11 | | |
12 | | Redistribution and use in source and binary forms, with or without modification, |
13 | | are permitted (subject to the limitations in the disclaimer below) provided that |
14 | | the following conditions are met: |
15 | | |
16 | | * Redistributions of source code must retain the above copyright notice, |
17 | | this list of conditions and the following disclaimer. |
18 | | |
19 | | * Redistributions in binary form must reproduce the above copyright |
20 | | notice, this list of conditions and the following disclaimer in the |
21 | | documentation and/or other materials provided with the distribution. |
22 | | |
23 | | * Neither the name of the copyright holder nor the names of its |
24 | | contributors may be used to endorse or promote products derived from this |
25 | | software without specific prior written permission. |
26 | | |
27 | | NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY |
28 | | THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
29 | | CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
31 | | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
32 | | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
33 | | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
34 | | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
35 | | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
36 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
37 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
38 | | POSSIBILITY OF SUCH DAMAGE. |
39 | | |
40 | | |
41 | | ------------------------------------------------------------------------------------------- */ |
42 | | |
43 | | /** \file MatrixIntraPrediction.cpp |
44 | | \brief matrix-based intra prediction class |
45 | | */ |
46 | | |
47 | | #include "MatrixIntraPrediction.h" |
48 | | #include "dtrace_next.h" |
49 | | |
50 | | #include "UnitTools.h" |
51 | | #include "MipData.h" |
52 | | |
53 | | namespace vvdec |
54 | | { |
55 | | |
56 | | namespace Mip |
57 | | { |
58 | | static void MatrixMulCore( Pel* const result, const Pel* const input, const uint8_t* matrix, const int inputSize, const int outputSize, |
59 | | const bool skipFirstInput, const int offset, const int inputOffset, const int bitDepth ) |
60 | 0 | { |
61 | 0 | const int redSize = skipFirstInput ? 1 : 0; |
62 | 0 | const uint8_t* weight = matrix; |
63 | |
|
64 | 0 | for( int posRes = 0; posRes < outputSize; posRes++ ) |
65 | 0 | { |
66 | 0 | int tmp0 = redSize ? 0 : ( input[0] * weight[0] ); |
67 | 0 | int tmp1 = input[1] * weight[1 - redSize]; |
68 | 0 | int tmp2 = input[2] * weight[2 - redSize]; |
69 | 0 | int tmp3 = input[3] * weight[3 - redSize]; |
70 | 0 | for( int i = 4; i < inputSize; i += 4 ) |
71 | 0 | { |
72 | 0 | tmp0 += input[i ] * weight[i - redSize]; |
73 | 0 | tmp1 += input[i + 1] * weight[i + 1 - redSize]; |
74 | 0 | tmp2 += input[i + 2] * weight[i + 2 - redSize]; |
75 | 0 | tmp3 += input[i + 3] * weight[i + 3 - redSize]; |
76 | 0 | } |
77 | 0 | result[posRes] = ClipBD<int>( ( ( tmp0 + tmp1 + tmp2 + tmp3 + offset ) >> MIP_SHIFT_MATRIX ) + inputOffset, bitDepth ); |
78 | |
|
79 | 0 | weight += inputSize - redSize; |
80 | 0 | } |
81 | 0 | } |
82 | | |
83 | | PredictorMIP::PredictorMIP(): |
84 | 37.8k | MatrixMul( MatrixMulCore ), |
85 | 37.8k | UpsampleHor( predictionUpsamplingHorCore ), |
86 | 37.8k | m_blockSize( 0, 0 ), |
87 | 37.8k | m_sizeId( 0 ), |
88 | 37.8k | m_reducedBdrySize( 0 ), |
89 | 37.8k | m_reducedPredSize( 0 ), |
90 | 37.8k | m_upsmpFactorHor( 0 ), |
91 | 37.8k | m_upsmpFactorVer( 0 ) |
92 | 37.8k | { |
93 | 37.8k | } |
94 | | |
95 | | void PredictorMIP::deriveBoundaryData(const CPelBuf& src, const Area& block, const int bitDepth) |
96 | 0 | { |
97 | | // Step 1: Save block size and calculate dependent values |
98 | 0 | initPredBlockParams(block); |
99 | | |
100 | | // Step 2: Get the input data (left and top reference samples) |
101 | | // m_refSamplesTop.resize(block.width); |
102 | 0 | for (int x = 0; x < block.width; x++) |
103 | 0 | { |
104 | 0 | m_refSamplesTop[x] = src.at(x + 1, 0); |
105 | 0 | } |
106 | | |
107 | | // m_refSamplesLeft.resize(block.height); |
108 | 0 | for (int y = 0; y < block.height; y++) |
109 | 0 | { |
110 | 0 | m_refSamplesLeft[y] = src.at(0, y + 1); |
111 | 0 | } |
112 | | |
113 | | // Step 3: Compute the reduced boundary via Haar-downsampling (input for the prediction) |
114 | | // m_reducedBoundary .resize( m_reducedBoundarySize.width + m_reducedBoundarySize.height ); |
115 | | // m_reducedBoundaryTransposed.resize( m_reducedBoundarySize.width + m_reducedBoundarySize.height ); |
116 | | |
117 | | // m_reducedBoundary .resize( inputSize ); |
118 | | // m_reducedBoundaryTransposed.resize( inputSize ); |
119 | | |
120 | 0 | Pel* const topReduced = m_reducedBoundary; |
121 | 0 | boundaryDownsampling1D( topReduced, m_refSamplesTop, block.width, m_reducedBdrySize ); |
122 | | |
123 | 0 | Pel* const leftReduced = m_reducedBoundary + m_reducedBdrySize; |
124 | 0 | boundaryDownsampling1D( leftReduced, m_refSamplesLeft, block.height, m_reducedBdrySize ); |
125 | | |
126 | 0 | Pel* const leftReducedTransposed = m_reducedBoundaryTransposed; |
127 | 0 | Pel* const topReducedTransposed = m_reducedBoundaryTransposed + m_reducedBdrySize; |
128 | 0 | for( int x = 0; x < m_reducedBdrySize; x++ ) |
129 | 0 | { |
130 | 0 | topReducedTransposed[x] = topReduced[x]; |
131 | 0 | } |
132 | 0 | for( int y = 0; y < m_reducedBdrySize; y++ ) |
133 | 0 | { |
134 | 0 | leftReducedTransposed[y] = leftReduced[y]; |
135 | 0 | } |
136 | | |
137 | | // Step 4: Rebase the reduced boundary |
138 | 0 | const int inputSize = 2 * m_reducedBdrySize; |
139 | | |
140 | 0 | m_inputOffset = m_reducedBoundary[0]; |
141 | 0 | m_inputOffsetTransp = m_reducedBoundaryTransposed[0]; |
142 | | |
143 | 0 | const bool hasFirstCol = (m_sizeId < 2); |
144 | 0 | m_reducedBoundary [0] = hasFirstCol ? ((1 << (bitDepth - 1)) - m_inputOffset ) : 0; // first column of matrix not needed for large blocks |
145 | 0 | m_reducedBoundaryTransposed[0] = hasFirstCol ? ((1 << (bitDepth - 1)) - m_inputOffsetTransp) : 0; |
146 | 0 | for (int i = 1; i < inputSize; i++) |
147 | 0 | { |
148 | 0 | m_reducedBoundary [i] -= m_inputOffset; |
149 | 0 | m_reducedBoundaryTransposed[i] -= m_inputOffsetTransp; |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | | void PredictorMIP::getPrediction(Pel* const result, const int modeIdx, const bool transpose, const int bitDepth) |
154 | 0 | { |
155 | 0 | const bool needUpsampling = ( m_upsmpFactorHor > 1 ) || ( m_upsmpFactorVer > 1 ); |
156 | |
|
157 | 0 | const uint8_t* matrix = getMatrixData( modeIdx ); |
158 | |
|
159 | 0 | Pel bufReducedPred[MIP_MAX_REDUCED_OUTPUT_SAMPLES]; |
160 | 0 | Pel* const reducedPred = needUpsampling ? bufReducedPred : result; |
161 | 0 | const Pel* const reducedBoundary = transpose ? m_reducedBoundaryTransposed : m_reducedBoundary; |
162 | 0 | computeReducedPred( reducedPred, reducedBoundary, matrix, transpose, bitDepth ); |
163 | 0 | if( needUpsampling ) |
164 | 0 | { |
165 | 0 | predictionUpsampling( result, reducedPred ); |
166 | 0 | } |
167 | 0 | } |
168 | | |
169 | | |
170 | | void PredictorMIP::initPredBlockParams(const Size& block) |
171 | 0 | { |
172 | 0 | m_blockSize = block; |
173 | | // init size index |
174 | 0 | m_sizeId = getMipSizeId( m_blockSize ); |
175 | | |
176 | | // init reduced boundary size |
177 | 0 | m_reducedBdrySize = (m_sizeId == 0) ? 2 : 4; |
178 | | |
179 | | // init reduced prediction size |
180 | 0 | m_reducedPredSize = ( m_sizeId < 2 ) ? 4 : 8; |
181 | | |
182 | | |
183 | | // init upsampling factors |
184 | 0 | m_upsmpFactorHor = m_blockSize.width / m_reducedPredSize; |
185 | 0 | m_upsmpFactorVer = m_blockSize.height / m_reducedPredSize; |
186 | |
|
187 | 0 | CHECKD( (m_upsmpFactorHor < 1) || ((m_upsmpFactorHor & (m_upsmpFactorHor - 1)) != 0), "Need power of two horizontal upsampling factor." ); |
188 | 0 | CHECKD( (m_upsmpFactorVer < 1) || ((m_upsmpFactorVer & (m_upsmpFactorVer - 1)) != 0), "Need power of two vertical upsampling factor." ); |
189 | 0 | } |
190 | | |
191 | | |
192 | | |
193 | | void PredictorMIP::boundaryDownsampling1D(Pel* reducedDst, const Pel* const fullSrc, const SizeType srcLen, const SizeType dstLen) |
194 | 0 | { |
195 | 0 | if (dstLen < srcLen) |
196 | 0 | { |
197 | | // Create reduced boundary by downsampling |
198 | 0 | const SizeType downsmpFactor = srcLen / dstLen; |
199 | 0 | const int log2DownsmpFactor = getLog2(downsmpFactor); |
200 | 0 | const int roundingOffset = (1 << (log2DownsmpFactor - 1)); |
201 | |
|
202 | 0 | SizeType srcIdx = 0; |
203 | 0 | for( SizeType dstIdx = 0; dstIdx < dstLen; dstIdx++ ) |
204 | 0 | { |
205 | 0 | int sum = 0; |
206 | 0 | for( int k = 0; k < downsmpFactor; k++ ) |
207 | 0 | { |
208 | 0 | sum += fullSrc[srcIdx++]; |
209 | 0 | } |
210 | 0 | reducedDst[dstIdx] = (sum + roundingOffset) >> log2DownsmpFactor; |
211 | 0 | } |
212 | 0 | } |
213 | 0 | else |
214 | 0 | { |
215 | 0 | memcpy( reducedDst, fullSrc, dstLen * sizeof( Pel ) ); |
216 | 0 | } |
217 | 0 | } |
218 | | |
219 | | |
220 | | void PredictorMIP::predictionUpsampling1D( Pel* const dst, const Pel* const src, const Pel* const bndry, |
221 | | const SizeType srcSizeUpsmpDim, const SizeType srcSizeOrthDim, |
222 | | const SizeType srcStep, const SizeType srcStride, |
223 | | const SizeType dstStep, const SizeType dstStride, |
224 | | const SizeType bndryStep, |
225 | | const unsigned int upsmpFactor ) |
226 | 0 | { |
227 | 0 | const Pel log2UpsmpFactor = getLog2( upsmpFactor ); |
228 | 0 | CHECKD( upsmpFactor <= 1, "Upsampling factor must be at least 2." ); |
229 | 0 | const int roundingOffset = 1 << ( log2UpsmpFactor - 1 ); |
230 | |
|
231 | 0 | const Pel* srcLine = src; |
232 | 0 | Pel* dstLine = dst; |
233 | 0 | const Pel* bndryLine = bndry + bndryStep - 1; |
234 | |
|
235 | 0 | for( int k = 0; k < srcSizeOrthDim; k++ ) |
236 | 0 | { |
237 | 0 | const Pel* before = bndryLine; |
238 | 0 | const Pel* behind = srcLine; |
239 | 0 | Pel* currDst = dstLine; |
240 | |
|
241 | 0 | for( int j = 0; j < srcSizeUpsmpDim; j++ ) |
242 | 0 | { |
243 | 0 | Pel valBehind = *behind; |
244 | 0 | Pel valBefore = *before; |
245 | 0 | Pel valDiff = valBehind - valBefore; |
246 | 0 | Pel scaledVal = valBefore * ( 1 << log2UpsmpFactor ) + roundingOffset; |
247 | |
|
248 | 0 | for( int i = 0; i < upsmpFactor; i++ ) |
249 | 0 | { |
250 | 0 | scaledVal += valDiff; |
251 | 0 | *currDst = scaledVal >> log2UpsmpFactor; |
252 | 0 | currDst += dstStep; |
253 | 0 | } |
254 | |
|
255 | 0 | before = behind; |
256 | 0 | behind += srcStep; |
257 | 0 | } |
258 | |
|
259 | 0 | srcLine += srcStride; |
260 | 0 | dstLine += dstStride; |
261 | 0 | bndryLine += bndryStep; |
262 | 0 | } |
263 | 0 | } |
264 | | |
265 | | |
266 | | void PredictorMIP::predictionUpsamplingHorCore( Pel* const dst, const ptrdiff_t dstStride, const Pel* const src, const Pel* const bndry, |
267 | | const int bndryStep, const int reducedSize, const int upsmpFactor ) |
268 | 0 | { |
269 | 0 | predictionUpsampling1D( dst, src, bndry, reducedSize, reducedSize, 1, reducedSize, 1, (SizeType) dstStride, bndryStep, upsmpFactor ); |
270 | 0 | } |
271 | | |
272 | | void PredictorMIP::predictionUpsampling( Pel* const dst, const Pel* const src ) const |
273 | 0 | { |
274 | 0 | const Pel* verSrc = src; |
275 | 0 | SizeType verSrcStep = m_blockSize.width; |
276 | | |
277 | 0 | if( m_upsmpFactorHor > 1 ) |
278 | 0 | { |
279 | 0 | Pel* const horDst = dst + (m_upsmpFactorVer - 1) * m_blockSize.width; |
280 | 0 | verSrc = horDst; |
281 | 0 | verSrcStep *= m_upsmpFactorVer; |
282 | | |
283 | 0 | UpsampleHor( horDst, verSrcStep, src, m_refSamplesLeft, m_upsmpFactorVer, m_reducedPredSize, m_upsmpFactorHor ); |
284 | 0 | } |
285 | | |
286 | 0 | if( m_upsmpFactorVer > 1 ) |
287 | 0 | { |
288 | 0 | predictionUpsamplingVer( dst, verSrc, verSrcStep ); |
289 | 0 | } |
290 | 0 | } |
291 | | |
292 | | // Vertical upsampling with the same arithmetic as predictionUpsampling1D(), but computed row by row instead of column |
293 | | // by column, so that the inner loops run over contiguous samples and can be vectorized. |
294 | | void PredictorMIP::predictionUpsamplingVer( Pel* const dst, const Pel* const src, const SizeType srcStep ) const |
295 | 0 | { |
296 | 0 | const int width = m_blockSize.width; |
297 | 0 | const int upsmpFactor = m_upsmpFactorVer; |
298 | 0 | const int log2Factor = getLog2( upsmpFactor ); |
299 | 0 | const int roundingOffset = 1 << ( log2Factor - 1 ); |
300 | | |
301 | | // For each column: the source sample above the current group of rows, the change from one output row to the next, |
302 | | // and the scaled output value before the final shift. |
303 | 0 | Pel before [MIP_MAX_WIDTH]; |
304 | 0 | Pel diff [MIP_MAX_WIDTH]; |
305 | 0 | Pel scaled [MIP_MAX_WIDTH]; |
306 | 0 | std::copy_n( m_refSamplesTop, width, before ); |
307 | |
|
308 | 0 | for( int j = 0; j < m_reducedPredSize; j++ ) |
309 | 0 | { |
310 | | // The source row lies in dst (it is the last output row of its group), so it is read completely before the group |
311 | | // is written. |
312 | 0 | const Pel* behind = src + j * srcStep; |
313 | 0 | for( int x = 0; x < width; x++ ) |
314 | 0 | { |
315 | 0 | diff [x] = behind[x] - before[x]; |
316 | 0 | scaled[x] = before[x] * ( 1 << log2Factor ) + roundingOffset; |
317 | 0 | before[x] = behind[x]; |
318 | 0 | } |
319 | |
|
320 | 0 | Pel* dstLine = dst + j * upsmpFactor * width; |
321 | 0 | for( int i = 0; i < upsmpFactor; i++, dstLine += width ) |
322 | 0 | { |
323 | 0 | for( int x = 0; x < width; x++ ) |
324 | 0 | { |
325 | 0 | scaled[x] += diff[x]; |
326 | 0 | dstLine[x] = scaled[x] >> log2Factor; |
327 | 0 | } |
328 | 0 | } |
329 | 0 | } |
330 | 0 | } |
331 | | |
332 | | const uint8_t* PredictorMIP::getMatrixData(const int modeIdx) const |
333 | 0 | { |
334 | 0 | switch( m_sizeId ) |
335 | 0 | { |
336 | 0 | case 0: return &mipMatrix4x4[modeIdx][0][0]; |
337 | | |
338 | 0 | case 1: return &mipMatrix8x8[modeIdx][0][0]; |
339 | | |
340 | 0 | case 2: return &mipMatrix16x16[modeIdx][0][0]; |
341 | | |
342 | 0 | default: THROW_FATAL( "Invalid mipSizeId" ); |
343 | 0 | } |
344 | 0 | } |
345 | | |
346 | | void PredictorMIP::computeReducedPred( Pel* const result, const Pel* const input, const uint8_t* matrix, const bool transpose, const int bitDepth ) |
347 | 0 | { |
348 | 0 | const int inputSize = 2 * m_reducedBdrySize; |
349 | | |
350 | | // use local buffer for transposed result |
351 | 0 | Pel resBufTransposed[MIP_MAX_REDUCED_OUTPUT_SAMPLES]; |
352 | 0 | Pel* const resPtr = ( transpose ) ? resBufTransposed : result; |
353 | |
|
354 | 0 | int sum = 0; |
355 | 0 | for( int i = 0; i < inputSize; i++ ) |
356 | 0 | { |
357 | 0 | sum += input[i]; |
358 | 0 | } |
359 | 0 | const int offset = ( 1 << ( MIP_SHIFT_MATRIX - 1 ) ) - MIP_OFFSET_MATRIX * sum; |
360 | 0 | CHECK( inputSize != 4 * ( inputSize >> 2 ), "Error, input size not divisible by four" ); |
361 | |
|
362 | 0 | const int inputOffset = transpose ? m_inputOffsetTransp : m_inputOffset; |
363 | |
|
364 | 0 | MatrixMul( resPtr, input, matrix, inputSize, m_reducedPredSize * m_reducedPredSize, m_sizeId == 2, offset, inputOffset, bitDepth ); |
365 | |
|
366 | 0 | if( transpose ) |
367 | 0 | { |
368 | 0 | for( int y = 0; y < m_reducedPredSize; y++ ) |
369 | 0 | { |
370 | 0 | for( int x = 0; x < m_reducedPredSize; x++ ) |
371 | 0 | { |
372 | 0 | result[y * m_reducedPredSize + x] = resPtr[x * m_reducedPredSize + y]; |
373 | 0 | } |
374 | 0 | } |
375 | 0 | } |
376 | 0 | } |
377 | | } // namespace Mip |
378 | | |
379 | | MatrixIntraPrediction::MatrixIntraPrediction() |
380 | 37.8k | { |
381 | 37.8k | } |
382 | | |
383 | | void MatrixIntraPrediction::prepareInputForPred(const CPelBuf &src, const Area& puArea, const int bitDepth, const ComponentID compId) |
384 | 0 | { |
385 | 0 | m_component = compId; |
386 | |
|
387 | 0 | m_predictorMip.deriveBoundaryData(src, puArea, bitDepth); |
388 | 0 | } |
389 | | |
390 | | void MatrixIntraPrediction::predBlock( const Size &puSize, const int intraMode, PelBuf& dst, const bool transpose, const int bitDepth, const ComponentID compId, Pel* const resultMip ) |
391 | 0 | { |
392 | 0 | CHECK( m_component != compId, "Boundary has not been prepared for this component." ); |
393 | |
|
394 | 0 | m_predictorMip.getPrediction( resultMip, intraMode, transpose, bitDepth ); |
395 | |
|
396 | 0 | for( int y = 0; y < puSize.height; y++ ) |
397 | 0 | { |
398 | 0 | Pel* const resultLine = &resultMip[y * puSize.width]; |
399 | 0 | Pel* dstLine = dst.bufAt( 0, y ); |
400 | |
|
401 | 0 | for( int x = 0; x < puSize.width; x += 4 ) |
402 | 0 | { |
403 | 0 | dstLine[x + 0] = Pel( resultLine[x + 0] ); |
404 | 0 | dstLine[x + 1] = Pel( resultLine[x + 1] ); |
405 | 0 | dstLine[x + 2] = Pel( resultLine[x + 2] ); |
406 | 0 | dstLine[x + 3] = Pel( resultLine[x + 3] ); |
407 | 0 | } |
408 | 0 | } |
409 | 0 | } |
410 | | |
411 | | } |