/work/vvenc/source/Lib/CommonLib/MCTF.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 MCTF.cpp |
45 | | \brief MCTF class |
46 | | */ |
47 | | |
48 | | #include "MCTF.h" |
49 | | #include <math.h> |
50 | | #include "CommonLib/Picture.h" |
51 | | #include "CommonLib/dtrace_buffer.h" |
52 | | #include "Utilities/NoMallocThreadPool.h" |
53 | | |
54 | | namespace vvenc { |
55 | | |
56 | | #ifdef TRACE_ENABLE_ITT |
57 | | static __itt_string_handle* itt_handle_est = __itt_string_handle_create( "MCTF_est" ); |
58 | | static __itt_domain* itt_domain_MCTF_est = __itt_domain_create( "MCTFEst" ); |
59 | | static __itt_string_handle* itt_handle_flt = __itt_string_handle_create( "MCTF_flt" ); |
60 | | static __itt_domain* itt_domain_MCTF_flt = __itt_domain_create( "MCTFFlt" ); |
61 | | #endif |
62 | | |
63 | | // ==================================================================================================================== |
64 | | // Constructor / destructor / initialization / destroy |
65 | | // ==================================================================================================================== |
66 | | |
67 | | const double MCTF::m_chromaFactor = 0.55; |
68 | | const double MCTF::m_sigmaMultiplier = 9.0; |
69 | | const int MCTF::m_range = VVENC_MCTF_RANGE-2; |
70 | | const int MCTF::m_motionVectorFactor = 16; |
71 | | const int MCTF::m_padding = MCTF_PADDING; |
72 | | const int16_t MCTF::m_interpolationFilter8[16][8] = |
73 | | { |
74 | | { 0, 0, 0, 64, 0, 0, 0, 0 }, //0 |
75 | | { 0, 1, -3, 64, 4, -2, 0, 0 }, //1 -->--> |
76 | | { 0, 1, -6, 62, 9, -3, 1, 0 }, //2 --> |
77 | | { 0, 2, -8, 60, 14, -5, 1, 0 }, //3 -->--> |
78 | | { 0, 2, -9, 57, 19, -7, 2, 0 }, //4 |
79 | | { 0, 3, -10, 53, 24, -8, 2, 0 }, //5 -->--> |
80 | | { 0, 3, -11, 50, 29, -9, 2, 0 }, //6 --> |
81 | | { 0, 3, -11, 44, 35, -10, 3, 0 }, //7 -->--> |
82 | | { 0, 1, -7, 38, 38, -7, 1, 0 }, //8 |
83 | | { 0, 3, -10, 35, 44, -11, 3, 0 }, //9 -->--> |
84 | | { 0, 2, -9, 29, 50, -11, 3, 0 }, //10--> |
85 | | { 0, 2, -8, 24, 53, -10, 3, 0 }, //11-->--> |
86 | | { 0, 2, -7, 19, 57, -9, 2, 0 }, //12 |
87 | | { 0, 1, -5, 14, 60, -8, 2, 0 }, //13-->--> |
88 | | { 0, 1, -3, 9, 62, -6, 1, 0 }, //14--> |
89 | | { 0, 0, -2, 4, 64, -3, 1, 0 } //15-->--> |
90 | | }; |
91 | | |
92 | | const int16_t MCTF::m_interpolationFilter4[16][4] = |
93 | | { |
94 | | { 0, 64, 0, 0 }, //0 |
95 | | { -2, 62, 4, 0 }, //1 -->--> |
96 | | { -2, 58, 10, -2 }, //2 --> |
97 | | { -4, 56, 14, -2 }, //3 -->--> |
98 | | { -4, 54, 16, -2 }, //4 |
99 | | { -6, 52, 20, -2 }, //5 -->--> |
100 | | { -6, 46, 28, -4 }, //6 --> |
101 | | { -4, 42, 30, -4 }, //7 -->--> |
102 | | { -4, 36, 36, -4 }, //8 |
103 | | { -4, 30, 42, -4 }, //9 -->--> |
104 | | { -4, 28, 46, -6 }, //10--> |
105 | | { -2, 20, 52, -6 }, //11-->--> |
106 | | { -2, 16, 54, -4 }, //12 |
107 | | { -2, 14, 56, -4 }, //13-->--> |
108 | | { -2, 10, 58, -2 }, //14--> |
109 | | { 0, 4, 62, -2 }, //15-->--> |
110 | | }; |
111 | | |
112 | | const double MCTF::m_refStrengths[2][6] = // min(..., {3 or 5} / (1 + 2 * |POC offset|)) |
113 | | { // abs(POC offset) |
114 | | // 1 2 3 4 5 6 |
115 | | { 0.84375, 0.6, 0.4286, 0.3333, 0.2727, 0.2308 }, // RA |
116 | | { 1.12500, 1.0, 0.7143, 0.5556, 0.4545, 0.3846 } // LD |
117 | | }; |
118 | | |
119 | | const int MCTF::m_cuTreeThresh[4] = { 75, 60, 30, 15 }; |
120 | | const double MCTF::m_cuTreeCenter = 45; |
121 | | |
122 | | int motionErrorLumaInt( const Pel* org, const ptrdiff_t origStride, const Pel* buf, const ptrdiff_t buffStride, const int w, const int h, const int besterror ) |
123 | 0 | { |
124 | 0 | int error = 0; |
125 | |
|
126 | 0 | for( int y1 = 0; y1 < h; y1++ ) |
127 | 0 | { |
128 | 0 | const Pel* origRowStart = org + y1 * origStride; |
129 | 0 | const Pel* bufferRowStart = buf + y1 * buffStride; |
130 | |
|
131 | 0 | for( int x1 = 0; x1 < w; x1 += 2 ) |
132 | 0 | { |
133 | 0 | int diff = origRowStart[x1] - bufferRowStart[x1]; |
134 | 0 | error += diff * diff; |
135 | 0 | diff = origRowStart[x1 + 1] - bufferRowStart[x1 + 1]; |
136 | 0 | error += diff * diff; |
137 | 0 | } |
138 | 0 | if( error > besterror ) |
139 | 0 | { |
140 | 0 | return error; |
141 | 0 | } |
142 | 0 | } |
143 | | |
144 | 0 | return error; |
145 | 0 | } |
146 | | |
147 | | int motionErrorLumaFrac6( const Pel *org, const ptrdiff_t origStride, const Pel *buf, const ptrdiff_t buffStride, const int w, const int h, const int16_t *xFilter, const int16_t *yFilter, const int bitDepth, const int besterror ) |
148 | 0 | { |
149 | 0 | int error = 0; |
150 | 0 | Pel tempArray[64 + 8][64]; |
151 | 0 | int sum, base; |
152 | 0 | const Pel maxSampleValue = ( 1 << bitDepth ) - 1; |
153 | |
|
154 | 0 | for( int y1 = 1; y1 < h + 7; y1++ ) |
155 | 0 | { |
156 | 0 | const int yOffset = y1 - 3; |
157 | 0 | const Pel *sourceRow = buf + yOffset * buffStride; |
158 | 0 | for( int x1 = 0; x1 < w; x1++ ) |
159 | 0 | { |
160 | 0 | sum = 0; |
161 | 0 | base = x1 - 3; |
162 | 0 | const Pel *rowStart = sourceRow + base; |
163 | |
|
164 | 0 | sum += xFilter[1] * rowStart[1]; |
165 | 0 | sum += xFilter[2] * rowStart[2]; |
166 | 0 | sum += xFilter[3] * rowStart[3]; |
167 | 0 | sum += xFilter[4] * rowStart[4]; |
168 | 0 | sum += xFilter[5] * rowStart[5]; |
169 | 0 | sum += xFilter[6] * rowStart[6]; |
170 | |
|
171 | 0 | sum = ( sum + ( 1 << 5 ) ) >> 6; |
172 | 0 | sum = sum < 0 ? 0 : ( sum > maxSampleValue ? maxSampleValue : sum ); |
173 | |
|
174 | 0 | tempArray[y1][x1] = sum; |
175 | 0 | } |
176 | 0 | } |
177 | |
|
178 | 0 | for( int y1 = 0; y1 < h; y1++ ) |
179 | 0 | { |
180 | 0 | const Pel *origRow = org + y1 * origStride; |
181 | 0 | for( int x1 = 0; x1 < w; x1++ ) |
182 | 0 | { |
183 | 0 | sum = 0; |
184 | 0 | sum += yFilter[1] * tempArray[y1 + 1][x1]; |
185 | 0 | sum += yFilter[2] * tempArray[y1 + 2][x1]; |
186 | 0 | sum += yFilter[3] * tempArray[y1 + 3][x1]; |
187 | 0 | sum += yFilter[4] * tempArray[y1 + 4][x1]; |
188 | 0 | sum += yFilter[5] * tempArray[y1 + 5][x1]; |
189 | 0 | sum += yFilter[6] * tempArray[y1 + 6][x1]; |
190 | |
|
191 | 0 | sum = ( sum + ( 1 << 5 ) ) >> 6; |
192 | 0 | sum = sum < 0 ? 0 : ( sum > maxSampleValue ? maxSampleValue : sum ); |
193 | |
|
194 | 0 | error += ( sum - origRow[x1] ) * ( sum - origRow[x1] ); |
195 | 0 | } |
196 | 0 | if( error > besterror ) |
197 | 0 | { |
198 | 0 | return error; |
199 | 0 | } |
200 | 0 | } |
201 | | |
202 | 0 | return error; |
203 | 0 | } |
204 | | |
205 | | int motionErrorLumaFrac4( const Pel* org, const ptrdiff_t origStride, const Pel* buf, const ptrdiff_t buffStride, const int w, const int h, const int16_t* xFilter, const int16_t* yFilter, const int bitDepth, const int besterror ) |
206 | 0 | { |
207 | 0 | int error = 0; |
208 | 0 | Pel tempArray[64 + 4][64]; |
209 | 0 | int sum, base; |
210 | 0 | const Pel maxSampleValue = ( 1 << bitDepth ) - 1; |
211 | |
|
212 | 0 | for( int y1 = 0; y1 < h + 3; y1++ ) |
213 | 0 | { |
214 | 0 | const int yOffset = y1 - 1; |
215 | 0 | const Pel* sourceRow = buf + yOffset * buffStride; |
216 | 0 | for( int x1 = 0; x1 < w; x1++ ) |
217 | 0 | { |
218 | 0 | sum = 0; |
219 | 0 | base = x1 - 1; |
220 | 0 | const Pel* rowStart = sourceRow + base; |
221 | |
|
222 | 0 | sum += xFilter[0] * rowStart[0]; |
223 | 0 | sum += xFilter[1] * rowStart[1]; |
224 | 0 | sum += xFilter[2] * rowStart[2]; |
225 | 0 | sum += xFilter[3] * rowStart[3]; |
226 | |
|
227 | 0 | sum = ( sum + ( 1 << 5 ) ) >> 6; |
228 | 0 | sum = sum < 0 ? 0 : ( sum > maxSampleValue ? maxSampleValue : sum ); |
229 | |
|
230 | 0 | tempArray[y1][x1] = sum; |
231 | 0 | } |
232 | 0 | } |
233 | |
|
234 | 0 | for( int y1 = 0; y1 < h; y1++ ) |
235 | 0 | { |
236 | 0 | const Pel* origRow = org + y1 * origStride; |
237 | 0 | for( int x1 = 0; x1 < w; x1++ ) |
238 | 0 | { |
239 | 0 | sum = 0; |
240 | 0 | sum += yFilter[0] * tempArray[y1 + 0][x1]; |
241 | 0 | sum += yFilter[1] * tempArray[y1 + 1][x1]; |
242 | 0 | sum += yFilter[2] * tempArray[y1 + 2][x1]; |
243 | 0 | sum += yFilter[3] * tempArray[y1 + 3][x1]; |
244 | |
|
245 | 0 | sum = ( sum + ( 1 << 5 ) ) >> 6; |
246 | 0 | sum = sum < 0 ? 0 : ( sum > maxSampleValue ? maxSampleValue : sum ); |
247 | |
|
248 | 0 | error += ( sum - origRow[x1] ) * ( sum - origRow[x1] ); |
249 | 0 | } |
250 | 0 | if( error > besterror ) |
251 | 0 | { |
252 | 0 | return error; |
253 | 0 | } |
254 | 0 | } |
255 | | |
256 | 0 | return error; |
257 | 0 | } |
258 | | |
259 | | // MCTF::m_filterColumn4 in C: the horizontal pass of motionErrorLumaFrac4 for numRows rows, row r from source row r - 1. |
260 | | static void filterColumn4Core( const Pel* buf, const ptrdiff_t buffStride, const int w, const int numRows, const int16_t* xFilter, const int bitDepth, Pel* filtered ) |
261 | 0 | { |
262 | 0 | const Pel maxSampleValue = ( 1 << bitDepth ) - 1; |
263 | |
|
264 | 0 | for( int r = 0; r < numRows; r++ ) |
265 | 0 | { |
266 | 0 | const Pel* rowStart = buf + ( r - 1 ) * buffStride - 1; |
267 | 0 | for( int x = 0; x < w; x++ ) |
268 | 0 | { |
269 | 0 | int sum = xFilter[0] * rowStart[x] + xFilter[1] * rowStart[x + 1] + xFilter[2] * rowStart[x + 2] + xFilter[3] * rowStart[x + 3]; |
270 | 0 | sum = ( sum + ( 1 << 5 ) ) >> 6; |
271 | 0 | filtered[r * w + x] = sum < 0 ? 0 : ( sum > maxSampleValue ? maxSampleValue : sum ); |
272 | 0 | } |
273 | 0 | } |
274 | 0 | } |
275 | | |
276 | | // MCTF::m_motionErrorLumaFracColumn in C: the vertical pass of motionErrorLumaFrac4 for each vector, on the rows of |
277 | | // filterColumn4Core from rowOffset[i] on, and its exact error (no early exit). |
278 | | static void motionErrorLumaFracColumnCore( const Pel* org, const ptrdiff_t origStride, const Pel* filtered, const int w, const int h, const int num, |
279 | | const int16_t* const* yFilter, const int* rowOffset, const int bitDepth, int* error ) |
280 | 0 | { |
281 | 0 | const Pel maxSampleValue = ( 1 << bitDepth ) - 1; |
282 | |
|
283 | 0 | for( int i = 0; i < num; i++ ) |
284 | 0 | { |
285 | 0 | const int16_t* f = yFilter[i]; |
286 | 0 | const Pel* rows = filtered + rowOffset[i] * w; |
287 | 0 | int sumError = 0; |
288 | 0 | for( int y = 0; y < h; y++ ) |
289 | 0 | { |
290 | 0 | const Pel* row = rows + y * w; |
291 | 0 | const Pel* origRow = org + y * origStride; |
292 | 0 | for( int x = 0; x < w; x++ ) |
293 | 0 | { |
294 | 0 | int sum = f[0] * row[x] + f[1] * row[x + w] + f[2] * row[x + 2 * w] + f[3] * row[x + 3 * w]; |
295 | 0 | sum = ( sum + ( 1 << 5 ) ) >> 6; |
296 | 0 | sum = sum < 0 ? 0 : ( sum > maxSampleValue ? maxSampleValue : sum ); |
297 | 0 | sumError += ( sum - origRow[x] ) * ( sum - origRow[x] ); |
298 | 0 | } |
299 | 0 | } |
300 | 0 | error[i] = sumError; |
301 | 0 | } |
302 | 0 | } |
303 | | |
304 | | void applyFrac8Core_6Tap( const Pel* org, const ptrdiff_t origStride, Pel* dst, const ptrdiff_t dstStride, const int w, const int h, const int16_t* xFilter, const int16_t* yFilter, const int bitDepth ) |
305 | 0 | { |
306 | 0 | const int numFilterTaps = 7; |
307 | 0 | const int centreTapOffset = 3; |
308 | 0 | const int maxValue = ( 1 << bitDepth ) - 1; |
309 | |
|
310 | 0 | Pel tempArray[64 + numFilterTaps][64]; |
311 | |
|
312 | 0 | for( int by = 1; by < h + numFilterTaps - 1; by++ ) |
313 | 0 | { |
314 | 0 | const int yOffset = by - centreTapOffset; |
315 | 0 | const Pel *sourceRow = org + yOffset * origStride; |
316 | 0 | for( int bx = 0; bx < w; bx++ ) |
317 | 0 | { |
318 | 0 | int base = bx - centreTapOffset; |
319 | 0 | const Pel *rowStart = sourceRow + base; |
320 | |
|
321 | 0 | int sum = 0; |
322 | 0 | sum += xFilter[1] * rowStart[1]; |
323 | 0 | sum += xFilter[2] * rowStart[2]; |
324 | 0 | sum += xFilter[3] * rowStart[3]; |
325 | 0 | sum += xFilter[4] * rowStart[4]; |
326 | 0 | sum += xFilter[5] * rowStart[5]; |
327 | 0 | sum += xFilter[6] * rowStart[6]; |
328 | |
|
329 | 0 | sum = ( sum + ( 1 << 5 ) ) >> 6; |
330 | 0 | tempArray[by][bx] = sum; |
331 | 0 | } |
332 | 0 | } |
333 | |
|
334 | 0 | Pel *dstRow = dst; |
335 | 0 | for( int by = 0; by < h; by++, dstRow += dstStride ) |
336 | 0 | { |
337 | 0 | Pel *dstPel = dstRow; |
338 | 0 | for( int bx = 0; bx < w; bx++, dstPel++ ) |
339 | 0 | { |
340 | 0 | int sum = 0; |
341 | |
|
342 | 0 | sum += yFilter[1] * tempArray[by + 1][bx]; |
343 | 0 | sum += yFilter[2] * tempArray[by + 2][bx]; |
344 | 0 | sum += yFilter[3] * tempArray[by + 3][bx]; |
345 | 0 | sum += yFilter[4] * tempArray[by + 4][bx]; |
346 | 0 | sum += yFilter[5] * tempArray[by + 5][bx]; |
347 | 0 | sum += yFilter[6] * tempArray[by + 6][bx]; |
348 | |
|
349 | 0 | sum = ( sum + ( 1 << 5 ) ) >> 6; |
350 | 0 | sum = sum < 0 ? 0 : ( sum > maxValue ? maxValue : sum ); |
351 | 0 | *dstPel = sum; |
352 | 0 | } |
353 | 0 | } |
354 | 0 | } |
355 | | |
356 | | void applyFrac8Core_4Tap( const Pel* org, const ptrdiff_t origStride, Pel* dst, const ptrdiff_t dstStride, const int w, const int h, const int16_t* xFilter, const int16_t* yFilter, const int bitDepth ) |
357 | 0 | { |
358 | 0 | const int numFilterTaps = 3; |
359 | 0 | const int centreTapOffset = 1; |
360 | 0 | const int maxValue = ( 1 << bitDepth ) - 1; |
361 | |
|
362 | 0 | Pel tempArray[64 + numFilterTaps][64]; |
363 | |
|
364 | 0 | for( int by = 0; by < h + numFilterTaps; by++ ) |
365 | 0 | { |
366 | 0 | const int yOffset = by - centreTapOffset; |
367 | 0 | const Pel* sourceRow = org + yOffset * origStride; |
368 | |
|
369 | 0 | for( int bx = 0; bx < w; bx++ ) |
370 | 0 | { |
371 | 0 | int base = bx - centreTapOffset; |
372 | 0 | const Pel* rowStart = sourceRow + base; |
373 | |
|
374 | 0 | int sum = 0; |
375 | 0 | sum += xFilter[0] * rowStart[0]; |
376 | 0 | sum += xFilter[1] * rowStart[1]; |
377 | 0 | sum += xFilter[2] * rowStart[2]; |
378 | 0 | sum += xFilter[3] * rowStart[3]; |
379 | |
|
380 | 0 | sum = ( sum + ( 1 << 5 ) ) >> 6; |
381 | 0 | tempArray[by][bx] = sum; |
382 | 0 | } |
383 | 0 | } |
384 | |
|
385 | 0 | Pel* dstRow = dst; |
386 | 0 | for( int by = 0; by < h; by++, dstRow += dstStride ) |
387 | 0 | { |
388 | 0 | Pel* dstPel = dstRow; |
389 | 0 | for( int bx = 0; bx < w; bx++, dstPel++ ) |
390 | 0 | { |
391 | 0 | int sum = 0; |
392 | 0 | sum += yFilter[0] * tempArray[by + 0][bx]; |
393 | 0 | sum += yFilter[1] * tempArray[by + 1][bx]; |
394 | 0 | sum += yFilter[2] * tempArray[by + 2][bx]; |
395 | 0 | sum += yFilter[3] * tempArray[by + 3][bx]; |
396 | |
|
397 | 0 | sum = ( sum + ( 1 << 5 ) ) >> 6; |
398 | 0 | sum = sum < 0 ? 0 : ( sum > maxValue ? maxValue : sum ); |
399 | 0 | *dstPel = sum; |
400 | 0 | } |
401 | 0 | } |
402 | 0 | } |
403 | | |
404 | | inline static float fastExp( float n, float d ) |
405 | 0 | { |
406 | | // using the e^x ~= ( 1 + x/n )^n for n -> inf |
407 | 0 | float x = 1.0f + n / ( d * 1024 ); |
408 | 0 | x *= x; x *= x; x *= x; x *= x; |
409 | 0 | x *= x; x *= x; x *= x; x *= x; |
410 | 0 | x *= x; x *= x; |
411 | 0 | return x; |
412 | 0 | } |
413 | | |
414 | | static const int32_t xSzm[6] = {0, 1, 20, 336, 5440, 87296}; |
415 | | |
416 | | // works for bit depths up to incl. 12 and power-of-2 block dimensions in both directions |
417 | | void applyPlanarCorrectionCore( const Pel* refPel, const ptrdiff_t refStride, Pel* dstPel, const ptrdiff_t dstStride, const int32_t w, const int32_t h, const ClpRng& clpRng, const uint16_t motionError ) |
418 | 0 | { |
419 | 0 | const int32_t blockSize = w * h; |
420 | 0 | const int32_t log2Width = floorLog2 (w); |
421 | 0 | const int32_t maxPelVal = clpRng.max(); |
422 | 0 | const int32_t mWeight = std::min (512u, (uint32_t) motionError * (uint32_t) motionError); |
423 | 0 | const int32_t xSum = (blockSize * (w - 1)) >> 1; |
424 | 0 | int32_t x1yzm = 0, x2yzm = 0, ySum = 0; |
425 | 0 | int32_t b0, b1, b2; |
426 | 0 | int64_t numer, denom; |
427 | |
|
428 | 0 | for (int32_t y = 0; y < h; y++) // sum up dot-products between indices and sample diffs |
429 | 0 | { |
430 | 0 | for (int32_t x = 0; x < w; x++) |
431 | 0 | { |
432 | 0 | const Pel* pDst = dstPel + y * dstStride + x; |
433 | 0 | const Pel* pRef = refPel + y * refStride + x; |
434 | 0 | const int32_t z = *pDst - *pRef; |
435 | |
|
436 | 0 | x1yzm += x * z; x2yzm += y * z; ySum += z; |
437 | 0 | } |
438 | 0 | } |
439 | |
|
440 | 0 | denom = blockSize * xSzm[log2Width]; // plane-fit parameters, in fixed-point arithmetic |
441 | 0 | numer = (int64_t) mWeight * ((int64_t) x1yzm * blockSize - xSum * ySum); |
442 | 0 | b1 = int32_t ((numer < 0 ? numer - (denom >> 1) : numer + (denom >> 1)) / denom); |
443 | 0 | b1 = (b1 < INT16_MIN ? INT16_MIN : (b1 > INT16_MAX ? INT16_MAX : b1)); |
444 | 0 | numer = (int64_t) mWeight * ((int64_t) x2yzm * blockSize - xSum * ySum); |
445 | 0 | b2 = int32_t ((numer < 0 ? numer - (denom >> 1) : numer + (denom >> 1)) / denom); |
446 | 0 | b2 = (b2 > INT16_MAX ? INT16_MAX : (b2 < INT16_MIN ? INT16_MIN : b2)); |
447 | 0 | b0 = (mWeight * ySum - (b1 + b2) * xSum + (blockSize >> 1)) >> (log2Width << 1); |
448 | |
|
449 | 0 | if (b0 == 0 && b1 == 0 && b2 == 0) return; |
450 | | |
451 | 0 | for (int32_t y = 0; y < h; y++) // perform deblocking by adding fitted correction plane |
452 | 0 | { |
453 | 0 | for (int32_t x = 0; x < w; x++) |
454 | 0 | { |
455 | 0 | Pel* const pDst = dstPel + y * dstStride + x; |
456 | 0 | const int32_t p = (b0 + b1 * x + b2 * y + 256) >> 9; // fixed-point plane corrector |
457 | 0 | const int32_t z = *pDst - p; |
458 | |
|
459 | 0 | *pDst = Pel (z < 0 ? 0 : (z > maxPelVal ? maxPelVal : z)); |
460 | 0 | } |
461 | 0 | } |
462 | 0 | } |
463 | | |
464 | | void applyBlockCore( const CPelBuf& src, PelBuf& dst, const CompArea& blk, const ClpRng& clpRng, const Pel** correctedPics, int numRefs, const int* verror, const double* refStrenghts, double weightScaling, double sigmaSq ) |
465 | 0 | { |
466 | 0 | const int w = blk.width; |
467 | 0 | const int h = blk.height; |
468 | 0 | const int bx = blk.x; |
469 | 0 | const int by = blk.y; |
470 | |
|
471 | 0 | const ptrdiff_t srcStride = src.stride; |
472 | 0 | const ptrdiff_t dstStride = dst.stride; |
473 | |
|
474 | 0 | const Pel *srcPel = src.bufAt( bx, by ); |
475 | 0 | Pel *dstPel = dst.bufAt( bx, by ); |
476 | |
|
477 | 0 | const Pel maxSampleValue = clpRng.max(); |
478 | |
|
479 | 0 | int vnoise[2 * VVENC_MCTF_RANGE] = { 0, }; |
480 | 0 | float vsw [2 * VVENC_MCTF_RANGE] = { 0.0f, }; |
481 | 0 | float vww [2 * VVENC_MCTF_RANGE] = { 0.0f, }; |
482 | |
|
483 | 0 | int minError = INT32_MAX; |
484 | |
|
485 | 0 | for( int i = 0; i < numRefs; i++ ) |
486 | 0 | { |
487 | 0 | int64_t variance = 0, diffsum = 0; |
488 | 0 | const ptrdiff_t refStride = w; |
489 | 0 | const Pel * refPel = correctedPics[i]; |
490 | 0 | for( int y1 = 0; y1 < h; y1++ ) |
491 | 0 | { |
492 | 0 | for( int x1 = 0; x1 < w; x1++ ) |
493 | 0 | { |
494 | 0 | const Pel pix = *( srcPel + srcStride * y1 + x1 ); |
495 | 0 | const Pel ref = *( refPel + refStride * y1 + x1 ); |
496 | |
|
497 | 0 | const int diff = pix - ref; |
498 | 0 | variance += diff * diff; |
499 | 0 | if( x1 != w - 1 ) |
500 | 0 | { |
501 | 0 | const Pel pixR = *( srcPel + srcStride * y1 + x1 + 1 ); |
502 | 0 | const Pel refR = *( refPel + refStride * y1 + x1 + 1 ); |
503 | 0 | const int diffR = pixR - refR; |
504 | 0 | diffsum += ( diffR - diff ) * ( diffR - diff ); |
505 | 0 | } |
506 | 0 | if( y1 != h - 1 ) |
507 | 0 | { |
508 | 0 | const Pel pixD = *( srcPel + srcStride * y1 + x1 + srcStride ); |
509 | 0 | const Pel refD = *( refPel + refStride * y1 + x1 + refStride ); |
510 | 0 | const int diffD = pixD - refD; |
511 | 0 | diffsum += ( diffD - diff ) * ( diffD - diff ); |
512 | 0 | } |
513 | 0 | } |
514 | 0 | } |
515 | 0 | variance *= (int64_t) 1 << (2*(10-clpRng.bd)); |
516 | 0 | diffsum *= (int64_t) 1 << (2*(10-clpRng.bd)); |
517 | 0 | const int cntV = w * h; |
518 | 0 | const int cntD = 2 * cntV - w - h; |
519 | 0 | vnoise[i] = ( int ) round( ( 15.0 * cntD / cntV * variance + 5.0 ) / ( diffsum + 5.0 ) ); |
520 | 0 | minError = std::min( minError, verror[i] ); |
521 | 0 | } |
522 | |
|
523 | 0 | for( int i = 0; i < numRefs; i++ ) |
524 | 0 | { |
525 | 0 | const int error = verror[i]; |
526 | 0 | const int noise = vnoise[i]; |
527 | 0 | float ww = 1, sw = 1; |
528 | 0 | ww *= ( noise < 25 ) ? 1.0 : 0.6; |
529 | 0 | sw *= ( noise < 25 ) ? 1.0 : 0.8; |
530 | 0 | ww *= ( error < 50 ) ? 1.2 : ( ( error > 100 ) ? 0.6 : 1.0 ); |
531 | 0 | sw *= ( error < 50 ) ? 1.0 : 0.8; |
532 | 0 | ww *= ( ( minError + 1.0 ) / ( error + 1.0 ) ); |
533 | |
|
534 | 0 | vww[i] = ww * weightScaling * refStrenghts[i]; |
535 | 0 | vsw[i] = sw * 2 * sigmaSq; |
536 | 0 | } |
537 | |
|
538 | 0 | for( int y = 0; y < h; y++ ) |
539 | 0 | { |
540 | 0 | for( int x = 0; x < w; x++ ) |
541 | 0 | { |
542 | 0 | const Pel orgVal = *( srcPel + srcStride * y + x ); |
543 | 0 | float temporalWeightSum = 1.0; |
544 | 0 | float newVal = ( float ) orgVal; |
545 | |
|
546 | 0 | for( int i = 0; i < numRefs; i++ ) |
547 | 0 | { |
548 | 0 | const Pel* pCorrectedPelPtr = correctedPics[i] + y * w + x; |
549 | 0 | const int refVal = *pCorrectedPelPtr; |
550 | 0 | const int diff = refVal - orgVal; |
551 | 0 | const float diffSq = diff * diff; |
552 | |
|
553 | 0 | float weight = vww[i] * fastExp( -diffSq, vsw[i] ); |
554 | 0 | newVal += weight * refVal; |
555 | 0 | temporalWeightSum += weight; |
556 | 0 | } |
557 | 0 | newVal /= temporalWeightSum; |
558 | 0 | Pel sampleVal = ( Pel ) ( newVal + 0.5 ); |
559 | 0 | sampleVal = ( sampleVal < 0 ? 0 : ( sampleVal > maxSampleValue ? maxSampleValue : sampleVal ) ); |
560 | 0 | *( dstPel + dstStride * y + x ) = sampleVal; |
561 | 0 | } |
562 | 0 | } |
563 | 0 | } |
564 | | |
565 | | double calcVarCore( const Pel* org, const ptrdiff_t origStride, const int w, const int h ) |
566 | 0 | { |
567 | | // calculate average |
568 | 0 | int avg = 0; |
569 | 0 | for( int y1 = 0; y1 < h; y1++ ) |
570 | 0 | { |
571 | 0 | for( int x1 = 0; x1 < w; x1++ ) |
572 | 0 | { |
573 | 0 | avg = avg + *( org + x1 + y1 * origStride ); |
574 | 0 | } |
575 | 0 | } |
576 | 0 | avg <<= 4; |
577 | 0 | avg = avg / ( w * h ); |
578 | | |
579 | | // calculate variance |
580 | 0 | int64_t variance = 0; |
581 | 0 | for( int y1 = 0; y1 < h; y1++ ) |
582 | 0 | { |
583 | 0 | for( int x1 = 0; x1 < w; x1++ ) |
584 | 0 | { |
585 | 0 | int pix = *( org + x1 + y1 * origStride ) << 4; |
586 | 0 | variance = variance + ( pix - avg ) * ( pix - avg ); |
587 | 0 | } |
588 | 0 | } |
589 | |
|
590 | 0 | return variance / 256.0; |
591 | 0 | } |
592 | | |
593 | | MCTF::MCTF( bool enableOpt ) |
594 | 1.43k | : m_encCfg ( nullptr ) |
595 | 1.43k | , m_threadPool ( nullptr ) |
596 | 1.43k | , m_isFinalPass( true ) |
597 | 1.43k | , m_filterPoc ( 0 ) |
598 | 1.43k | , m_lastPicIn ( nullptr ) |
599 | 1.43k | { |
600 | 1.43k | m_motionErrorLumaIntX = motionErrorLumaInt; |
601 | 1.43k | m_motionErrorLumaInt8 = motionErrorLumaInt; |
602 | 1.43k | m_motionErrorLumaInt4 = motionErrorLumaInt4Loop<motionErrorLumaInt>; |
603 | 1.43k | m_motionErrorLumaFracX[0] = motionErrorLumaFrac6; |
604 | 1.43k | m_motionErrorLumaFrac8[0] = motionErrorLumaFrac6; |
605 | 1.43k | m_motionErrorLumaFracX[1] = motionErrorLumaFrac4; |
606 | 1.43k | m_motionErrorLumaFrac8[1] = motionErrorLumaFrac4; |
607 | 1.43k | m_filterColumn4 = filterColumn4Core; |
608 | 1.43k | m_motionErrorLumaFracColumn = motionErrorLumaFracColumnCore; |
609 | 1.43k | m_applyFrac[0][0] = applyFrac8Core_6Tap; |
610 | 1.43k | m_applyFrac[0][1] = applyFrac8Core_4Tap; |
611 | 1.43k | m_applyFrac[1][0] = applyFrac8Core_6Tap; |
612 | 1.43k | m_applyFrac[1][1] = applyFrac8Core_4Tap; |
613 | 1.43k | m_applyPlanarCorrection = applyPlanarCorrectionCore; |
614 | 1.43k | m_applyBlock = applyBlockCore; |
615 | 1.43k | FGAnalyzer::initKernels( m_fgaKernels ); |
616 | 1.43k | m_calcVar = calcVarCore; |
617 | | |
618 | 1.43k | if( enableOpt ) |
619 | 1.43k | { |
620 | | #if defined( TARGET_SIMD_X86 ) && ENABLE_SIMD_OPT_MCTF |
621 | | initMCTF_X86(); |
622 | | #endif |
623 | | #if defined( TARGET_SIMD_ARM ) && ENABLE_SIMD_OPT_MCTF |
624 | | initMCTF_ARM(); |
625 | | #endif |
626 | 1.43k | } |
627 | 1.43k | } |
628 | | |
629 | | MCTF::~MCTF() |
630 | 1.43k | { |
631 | 1.43k | } |
632 | | |
633 | | void MCTF::init( const VVEncCfg& encCfg, bool isFinalPass, NoMallocThreadPool* threadPool ) |
634 | 1.43k | { |
635 | 1.43k | CHECK( encCfg.m_vvencMCTF.numFrames != encCfg.m_vvencMCTF.numStrength, "should have been checked before" ); |
636 | | |
637 | 1.43k | m_encCfg = &encCfg; |
638 | 1.43k | m_threadPool = threadPool; |
639 | 1.43k | m_isFinalPass = isFinalPass; |
640 | 1.43k | m_filterPoc = 0; |
641 | 1.43k | m_area = Area( 0, 0, m_encCfg->m_PadSourceWidth, m_encCfg->m_PadSourceHeight ); |
642 | | |
643 | | // TLayer (TL) dependent definition of drop frames: TL = 4, TL = 3, TL = 2, TL = 1, TL = 0 |
644 | 1.43k | const static int sMCTFSpeed[5] { 0, 0, ((3<<12) + (2<<9) + (2<<6) + (0<<3) + 0), ((3<<12) + (2<<9) + (2<<6) + (0<<3) + 0), ((3<<12) + (3<<9) + (3<<6) + (2<<3) + 2) }; |
645 | | |
646 | 1.43k | m_MCTFSpeedVal = sMCTFSpeed[ m_encCfg->m_vvencMCTF.MCTFSpeed ]; |
647 | 1.43k | m_lowResFltSearch = m_encCfg->m_vvencMCTF.MCTFSpeed > 0; |
648 | 1.43k | m_searchPttrn = m_encCfg->m_vvencMCTF.MCTFSpeed > 0 ? ( m_encCfg->m_vvencMCTF.MCTFSpeed >= 3 ? 2 : 1 ) : 0; |
649 | 1.43k | m_mctfUnitSize = m_encCfg->m_vvencMCTF.MCTFUnitSize; |
650 | | |
651 | | // The filter strength per MCTF index, from the GOP's key pictures (the last index, the pictures most others predict |
652 | | // from) down the temporal levels: the configured strength, raised for the three upper levels with MCTFLevelTilt. A |
653 | | // cleaner reference pays most where more pictures predict from it. The configuration allows it in random access only |
654 | | // (checkCfgParameter): with GOP size 1 (all intra) every picture has MCTF index 0 and none is predicted from another, |
655 | | // and in low delay (no picture reordering) the raise costs bits (measured +0.8 to +0.9 % BD-rate on a grainy and a |
656 | | // clean clip). BIM keeps the configured strength, the film grain analysis its own fixed strength. |
657 | 1.43k | static constexpr double LEVEL_RAISE[ 4 ] = { 0.6, 0.46, 0.21, 0.0 }; // from the key pictures down |
658 | 1.43k | const bool levelTilt = m_encCfg->m_vvencMCTF.MCTFLevelTilt > 0; |
659 | 1.43k | const int numFrames = m_encCfg->m_vvencMCTF.numFrames; |
660 | 5.34k | for( int i = 0; i < numFrames; i++ ) |
661 | 3.91k | { |
662 | 3.91k | const int level = std::min( numFrames - 1 - i, 3 ); |
663 | 3.91k | m_filterStrength[i] = m_encCfg->m_vvencMCTF.MCTFStrengths[i] * ( levelTilt ? 1.0 + LEVEL_RAISE[level] : 1.0 ); |
664 | 3.91k | } |
665 | 1.43k | } |
666 | | |
667 | | // ==================================================================================================================== |
668 | | // Public member functions |
669 | | // ==================================================================================================================== |
670 | | |
671 | | |
672 | | void MCTF::initPicture( Picture* pic ) |
673 | 1.43k | { |
674 | 1.43k | pic->getOrigBuf().extendBorderPel( MCTF_FRAME_MARGIN, true ); |
675 | 1.43k | pic->setSccFlags( m_encCfg ); |
676 | 1.43k | } |
677 | | |
678 | | void MCTF::processPictures( const PicList& picList, AccessUnitList& auList, PicList& doneList, PicList& freeList ) |
679 | 10.0k | { |
680 | | // ensure this is only processed if necessary |
681 | 10.0k | if( picList.empty() || ( m_lastPicIn == picList.back() && ! picList.back()->isFlush )) |
682 | 0 | { |
683 | 0 | return; |
684 | 0 | } |
685 | 10.0k | m_lastPicIn = picList.back(); |
686 | | |
687 | | // filter one picture (either all or up to frames to be encoded) |
688 | 10.0k | if( picList.size() |
689 | 10.0k | && m_filterPoc <= picList.back()->poc |
690 | 1.43k | && ( m_encCfg->m_framesToBeEncoded <= 0 || m_filterPoc < m_encCfg->m_framesToBeEncoded ) ) |
691 | 1.43k | { |
692 | | // setup fifo of pictures to be filtered |
693 | 1.43k | std::deque<Picture*> picFifo; |
694 | 1.43k | int filterIdx = 0; |
695 | 1.43k | for( auto pic : picList ) |
696 | 1.43k | { |
697 | 1.43k | const int minPoc = m_filterPoc - VVENC_MCTF_RANGE; |
698 | 1.43k | const int maxPoc = m_encCfg->m_vvencMCTF.MCTFFutureReference ? m_filterPoc + VVENC_MCTF_RANGE : m_filterPoc; |
699 | 1.43k | if( pic->poc >= minPoc && pic->poc <= maxPoc ) |
700 | 1.43k | { |
701 | 1.43k | picFifo.push_back( pic ); |
702 | 1.43k | if( pic->poc < m_filterPoc ) |
703 | 0 | { |
704 | 0 | filterIdx += 1; |
705 | 0 | } |
706 | 1.43k | } |
707 | 1.43k | } |
708 | 1.43k | CHECK( picFifo.empty(), "MCTF: no pictures to be filtered found" ); |
709 | 1.43k | CHECK( filterIdx >= (int)picFifo.size(), "MCTF: picture filter error" ); |
710 | 1.43k | CHECK( picFifo[ filterIdx ]->poc != m_filterPoc, "MCTF: picture filter error" ); |
711 | | // filter picture (when more than 1 picture is available for processing) |
712 | 1.43k | if( picFifo.size() > 1 ) |
713 | 0 | { |
714 | 0 | filter( picFifo, filterIdx ); |
715 | 0 | } |
716 | | // set picture done |
717 | 1.43k | doneList.push_back( picFifo[ filterIdx ] ); |
718 | 1.43k | } |
719 | | |
720 | | // mark pictures not needed anymore |
721 | 10.0k | for( auto pic : picList ) |
722 | 10.0k | { |
723 | 10.0k | if( pic->poc > m_filterPoc - VVENC_MCTF_RANGE ) |
724 | 8.59k | break; |
725 | 1.43k | freeList.push_back( pic ); |
726 | 1.43k | } |
727 | 10.0k | m_filterPoc += 1; |
728 | 10.0k | } |
729 | | |
730 | | void MCTF::subsampleReference( ReferenceSearch& ref, const bool addLevel ) const |
731 | 0 | { |
732 | 0 | subsampleLuma( ref.srcPic->picBuffer, ref.bufferSub2 ); |
733 | 0 | subsampleLuma( ref.bufferSub2, ref.bufferSub4 ); |
734 | 0 | if( addLevel ) |
735 | 0 | { |
736 | 0 | subsampleLuma( ref.bufferSub4, ref.bufferSub8 ); |
737 | 0 | } |
738 | 0 | } |
739 | | |
740 | | void MCTF::subsampleReferences( std::deque<ReferenceSearch>& refs, const bool addLevel ) const |
741 | 0 | { |
742 | 0 | if( !m_threadPool || refs.size() < 2 ) |
743 | 0 | { |
744 | 0 | for( ReferenceSearch& ref : refs ) |
745 | 0 | { |
746 | 0 | subsampleReference( ref, addLevel ); |
747 | 0 | } |
748 | 0 | return; |
749 | 0 | } |
750 | | |
751 | 0 | struct SubsampleParams |
752 | 0 | { |
753 | 0 | const MCTF* mctf; |
754 | 0 | ReferenceSearch* ref; |
755 | 0 | bool addLevel; |
756 | 0 | }; |
757 | 0 | static auto task = []( int, void* taskParam ) |
758 | 0 | { |
759 | 0 | SubsampleParams* params = static_cast<SubsampleParams*>( taskParam ); |
760 | 0 | params->mctf->subsampleReference( *params->ref, params->addLevel ); |
761 | 0 | return true; |
762 | 0 | }; |
763 | |
|
764 | 0 | std::vector<SubsampleParams> params( refs.size() ); // sized up front: the tasks point into it |
765 | 0 | WaitCounter taskCounter; |
766 | 0 | for( size_t i = 0; i < refs.size(); i++ ) |
767 | 0 | { |
768 | 0 | params[i] = { this, &refs[i], addLevel }; |
769 | 0 | m_threadPool->addBarrierTask( task, ¶ms[i], &taskCounter ); |
770 | 0 | } |
771 | 0 | taskCounter.wait(); |
772 | 0 | } |
773 | | |
774 | | void MCTF::motionEstimationMCTF(const std::vector<Picture*>& refPics, std::deque<TemporalFilterSourcePicInfo> &srcFrameInfo, const PelStorage& origBuf, PelStorage& origSubsampled2, PelStorage& origSubsampled4, PelStorage& origSubsampled8, std::vector<double> &mvErr, double &minError, bool addLevel, bool calcErr) |
775 | 0 | { |
776 | 0 | if( refPics.empty() ) |
777 | 0 | { |
778 | 0 | return; |
779 | 0 | } |
780 | 0 | const int wInBlks = (m_area.width + m_mctfUnitSize - 1) / m_mctfUnitSize; |
781 | 0 | const int hInBlks = (m_area.height + m_mctfUnitSize - 1) / m_mctfUnitSize; |
782 | 0 | const int width = m_area.width; |
783 | 0 | const int height = m_area.height; |
784 | | |
785 | | // Each reference's srcPic points into srcFrameInfo, a deque, whose elements stay in place while more are appended. |
786 | 0 | std::deque<ReferenceSearch> refs; |
787 | 0 | for( Picture* refPic : refPics ) |
788 | 0 | { |
789 | 0 | srcFrameInfo.push_back(TemporalFilterSourcePicInfo()); |
790 | 0 | TemporalFilterSourcePicInfo& srcPic = srcFrameInfo.back(); |
791 | 0 | srcPic.picBuffer.createFromBuf(refPic->getOrigBuf()); |
792 | 0 | srcPic.mvs.allocate(wInBlks, hInBlks); |
793 | 0 | srcPic.index = std::min(5, std::abs(refPic->poc - m_filterPoc) - 1); |
794 | |
|
795 | 0 | refs.emplace_back(); |
796 | 0 | ReferenceSearch& ref = refs.back(); |
797 | 0 | ref.srcPic = &srcPic; |
798 | 0 | ref.mv_0.allocate(width / (m_mctfUnitSize * 8) + 1, height / (m_mctfUnitSize * 8) + 1); |
799 | 0 | ref.mv_1.allocate(width / (m_mctfUnitSize * 4) + 1, height / (m_mctfUnitSize * 4) + 1); |
800 | 0 | ref.mv_2.allocate(width / (m_mctfUnitSize * 2) + 1, height / (m_mctfUnitSize * 2) + 1); |
801 | 0 | if (addLevel) |
802 | 0 | { |
803 | 0 | ref.mv_m.allocate(width / (m_mctfUnitSize * 16) + 1, height / (m_mctfUnitSize * 16) + 1); |
804 | 0 | } |
805 | 0 | } |
806 | 0 | subsampleReferences( refs, addLevel ); |
807 | | |
808 | | // The search levels, coarse to fine: 1/8 resolution (only with addLevel), 1/4, 1/2, full resolution with blocks of |
809 | | // 2 x blockSize, full resolution with blockSize; each level starts from the previous level's vectors of the same |
810 | | // reference, so the levels run in turn, while the references of one level are independent and run together. |
811 | 0 | const int blockSize = m_mctfUnitSize; |
812 | 0 | std::vector<MotionSearchJob> jobs( refs.size() ); |
813 | 0 | for( int level = addLevel ? 0 : 1; level < 5; level++ ) |
814 | 0 | { |
815 | 0 | for( size_t i = 0; i < refs.size(); i++ ) |
816 | 0 | { |
817 | 0 | ReferenceSearch& r = refs[i]; |
818 | 0 | switch( level ) |
819 | 0 | { |
820 | 0 | case 0: jobs[i] = { &r.mv_m, &origSubsampled8, &r.bufferSub8, m_padding, 2 * blockSize, nullptr, 1, false }; break; |
821 | 0 | case 1: jobs[i] = { &r.mv_0, &origSubsampled4, &r.bufferSub4, m_padding, 2 * blockSize, addLevel ? &r.mv_m : nullptr, addLevel ? 2 : 1, false }; break; |
822 | 0 | case 2: jobs[i] = { &r.mv_1, &origSubsampled2, &r.bufferSub2, m_padding, 2 * blockSize, &r.mv_0, 2, false }; break; |
823 | 0 | case 3: jobs[i] = { &r.mv_2, &origBuf, &r.srcPic->picBuffer, MCTF_FRAME_MARGIN, 2 * blockSize, &r.mv_1, 2, false }; break; |
824 | 0 | default: jobs[i] = { &r.srcPic->mvs, &origBuf, &r.srcPic->picBuffer, MCTF_FRAME_MARGIN, blockSize, &r.mv_2, 1, true }; break; |
825 | 0 | } |
826 | 0 | } |
827 | 0 | motionEstimationLumaJobs( jobs ); |
828 | 0 | } |
829 | | |
830 | 0 | if (calcErr) |
831 | 0 | { |
832 | 0 | for( const ReferenceSearch& ref : refs ) |
833 | 0 | { |
834 | 0 | const Array2D<MotionVector>& mvs = ref.srcPic->mvs; |
835 | 0 | double sumErr = 0.0; |
836 | 0 | for (int y = 0; y < mvs.h(); y++) // going over ref pic in block steps |
837 | 0 | { |
838 | 0 | for (int x = 0; x < mvs.w(); x++) |
839 | 0 | { |
840 | 0 | sumErr += mvs.get(x, y).error; |
841 | 0 | } |
842 | 0 | } |
843 | 0 | double S = 1.0 / (mvs.w() * mvs.h()); |
844 | 0 | mvErr.push_back(sumErr * S); |
845 | 0 | minError = std::min(minError, sumErr * S); |
846 | 0 | } |
847 | 0 | } |
848 | 0 | } |
849 | | |
850 | | void MCTF::filter( const std::deque<Picture*>& picFifo, int filterIdx ) |
851 | 0 | { |
852 | 0 | PROFILER_SCOPE_AND_STAGE( 1, g_timeProfiler, P_MCTF ); |
853 | |
|
854 | 0 | Picture* pic = picFifo[ filterIdx ]; |
855 | | |
856 | | // first-pass temporal downsampling |
857 | 0 | if( ! m_isFinalPass && pic->gopEntry->m_skipFirstPass ) |
858 | 0 | { |
859 | 0 | return; |
860 | 0 | } |
861 | | |
862 | 0 | const int mctfIdx = pic->gopEntry ? pic->gopEntry->m_mctfIndex : -1; |
863 | 0 | const double overallStrength = mctfIdx >= 0 ? m_encCfg->m_vvencMCTF.MCTFStrengths[ mctfIdx ] : -1.0; |
864 | 0 | const double filterStrength = mctfIdx >= 0 ? m_filterStrength[ mctfIdx ] : -1.0; // with the level tilt |
865 | 0 | double meanRmsAcrossPic = 0.0; |
866 | 0 | uint64_t sumSRmsAcrossPic = 0; |
867 | 0 | uint16_t nMax = 0, maxRmsCTU = 0; |
868 | 0 | bool isFilterThisFrame = mctfIdx >= 0; |
869 | |
|
870 | 0 | int dropFrames = ( m_encCfg->m_usePerceptQPA ? VVENC_MCTF_RANGE >> 1 : 0 ); |
871 | 0 | if( mctfIdx >= 0 ) |
872 | 0 | { |
873 | 0 | const int idxTLayer = m_encCfg->m_vvencMCTF.numFrames - (mctfIdx + 1); |
874 | 0 | const int threshold = (m_MCTFSpeedVal >> (idxTLayer * 3)) & 7; |
875 | |
|
876 | 0 | dropFrames = std::min(VVENC_MCTF_RANGE, threshold); |
877 | 0 | isFilterThisFrame = threshold < VVENC_MCTF_RANGE; |
878 | 0 | } |
879 | |
|
880 | 0 | const int filterFrames = VVENC_MCTF_RANGE - 2 - dropFrames; |
881 | |
|
882 | 0 | int dropFramesFront = std::min( std::max( filterIdx - filterFrames, 0 ), dropFrames + 2 ); |
883 | 0 | int dropFramesBack = std::min( std::max( static_cast<int>( picFifo.size() ) - 1 - filterIdx - filterFrames, 0 ), dropFrames + 2 ); |
884 | |
|
885 | 0 | if( !pic->useMCTF && !pic->gopEntry->m_isStartOfGop ) |
886 | 0 | { |
887 | 0 | isFilterThisFrame = false; |
888 | 0 | } |
889 | |
|
890 | 0 | if ( isFilterThisFrame ) |
891 | 0 | { |
892 | 0 | bool useMCTFadaptation = true; |
893 | 0 | const bool condAddLevel = useMCTFadaptation && m_area.width >= 1920; |
894 | 0 | std::vector<double> mvErr; |
895 | 0 | double minError = MAX_DOUBLE; |
896 | |
|
897 | 0 | const PelStorage& origBuf = pic->getOrigBuffer(); |
898 | 0 | PelStorage& fltrBuf = pic->getFilteredOrigBuffer(); |
899 | | |
900 | | // subsample original picture so it only needs to be done once |
901 | 0 | PelStorage origSubsampled2; |
902 | 0 | PelStorage origSubsampled4; |
903 | 0 | PelStorage origSubsampled8; |
904 | 0 | subsampleLuma( origBuf, origSubsampled2 ); |
905 | 0 | subsampleLuma( origSubsampled2, origSubsampled4 ); |
906 | 0 | if (condAddLevel) |
907 | 0 | { |
908 | 0 | subsampleLuma(origSubsampled4, origSubsampled8); |
909 | 0 | } |
910 | | |
911 | | // determine motion vectors |
912 | 0 | std::deque<TemporalFilterSourcePicInfo> srcFrameInfo; |
913 | 0 | std::vector<Picture*> refPics; |
914 | 0 | for ( int i = dropFramesFront; i < picFifo.size() - dropFramesBack; i++ ) |
915 | 0 | { |
916 | 0 | Picture* curPic = picFifo[ i ]; |
917 | 0 | if ( curPic->poc == m_filterPoc ) |
918 | 0 | { |
919 | 0 | continue; |
920 | 0 | } |
921 | 0 | refPics.push_back( curPic ); |
922 | 0 | } |
923 | 0 | motionEstimationMCTF(refPics, srcFrameInfo, origBuf, origSubsampled2, origSubsampled4, origSubsampled8 ,mvErr, minError, condAddLevel, useMCTFadaptation); |
924 | |
|
925 | 0 | int lastIndexRefFr = -1; |
926 | 0 | if ((m_encCfg->m_vvencMCTF.MCTFSpeed < 4) && (minError > 80)) |
927 | 0 | { |
928 | 0 | useMCTFadaptation = false; |
929 | 0 | } |
930 | 0 | if (useMCTFadaptation && minError) |
931 | 0 | { |
932 | 0 | const double errThr = 0.75 * minError * srcFrameInfo.size(); |
933 | 0 | int avgErrCond = 0; |
934 | 0 | int minErrCond = 0; |
935 | 0 | double factErr = m_encCfg->m_vvencMCTF.MCTFSpeed < 4 ? 1.0 : 2.0 ; |
936 | 0 | double SizeThi = m_encCfg->m_vvencMCTF.MCTFSpeed < 4 ? filterFrames + 1 : 3.0; |
937 | |
|
938 | 0 | for (const double& framMvErr : mvErr) |
939 | 0 | { |
940 | 0 | if (factErr * framMvErr > errThr) |
941 | 0 | { |
942 | 0 | avgErrCond++; |
943 | 0 | } |
944 | 0 | if (framMvErr > SizeThi * minError) |
945 | 0 | { |
946 | 0 | minErrCond++; |
947 | 0 | } |
948 | 0 | } |
949 | 0 | int newFilterFrames = minErrCond ? filterFrames : (filterFrames + 2 - avgErrCond); |
950 | 0 | if (filterFrames <= 2 && newFilterFrames > 3) newFilterFrames = 3; |
951 | | |
952 | | // MCTFSpeed 4 stops at the first reference whose error is too large, so it searches them one at a time; otherwise |
953 | | // all further references are searched together after the loop |
954 | 0 | const bool stopOnError = m_encCfg->m_vvencMCTF.MCTFSpeed == 4; |
955 | 0 | std::vector<Picture*> furtherRefPics; |
956 | 0 | for (int curIdx = filterFrames + 1; (curIdx < newFilterFrames + 1)&&((lastIndexRefFr == -1)); curIdx++) |
957 | 0 | { |
958 | 0 | for (int i = 0; i < picFifo.size(); i++) |
959 | 0 | { |
960 | 0 | Picture* curPic = picFifo[i]; |
961 | 0 | if (curIdx == std::abs(curPic->poc - m_filterPoc)) |
962 | 0 | { |
963 | 0 | if( !stopOnError ) |
964 | 0 | { |
965 | 0 | furtherRefPics.push_back( curPic ); |
966 | 0 | continue; |
967 | 0 | } |
968 | 0 | motionEstimationMCTF({ curPic }, srcFrameInfo, origBuf, origSubsampled2, origSubsampled4, origSubsampled8, mvErr, minError, condAddLevel, true); |
969 | 0 | int nSize = (int(srcFrameInfo.size()) & 1) + int(srcFrameInfo.size()); |
970 | 0 | const double errThrcur = 0.75 * minError * nSize; |
971 | 0 | if (mvErr.back() > errThrcur) |
972 | 0 | { |
973 | 0 | lastIndexRefFr = curIdx; |
974 | 0 | break; |
975 | 0 | } |
976 | 0 | } |
977 | 0 | } |
978 | 0 | } |
979 | 0 | motionEstimationMCTF(furtherRefPics, srcFrameInfo, origBuf, origSubsampled2, origSubsampled4, origSubsampled8, mvErr, minError, condAddLevel, false); |
980 | 0 | if ((lastIndexRefFr != -1)) |
981 | 0 | { |
982 | 0 | for (auto it = srcFrameInfo.begin(); it != srcFrameInfo.end(); ) |
983 | 0 | { |
984 | 0 | if ((it->index + 1) >= lastIndexRefFr) |
985 | 0 | { |
986 | 0 | it = srcFrameInfo.erase(it); |
987 | 0 | } |
988 | 0 | else |
989 | 0 | { |
990 | 0 | ++it; |
991 | 0 | } |
992 | 0 | } |
993 | 0 | } |
994 | 0 | } |
995 | | |
996 | | // filter |
997 | 0 | if( pic->useMCTF ) |
998 | 0 | { |
999 | 0 | fltrBuf.create( m_encCfg->m_internChromaFormat, m_area, 0, MCTF_FRAME_MARGIN ); |
1000 | 0 | bilateralFilter( origBuf, srcFrameInfo, fltrBuf, filterStrength ); |
1001 | 0 | } |
1002 | | |
1003 | | // The film grain analysis compares the original with a filtered copy of fixed strength, independent of the |
1004 | | // filtering for the encoding, as the dedicated temporal filter of the VTM analysis does (strength 1.5). It analyses |
1005 | | // the start picture of every GOP, whose motion has just been estimated. |
1006 | 0 | if( m_encCfg->m_fga && m_isFinalPass && pic->gopEntry->m_isStartOfGop ) |
1007 | 0 | { |
1008 | 0 | static constexpr double FGA_FILTER_STRENGTH = 1.5; |
1009 | 0 | if( pic->useMCTF && filterStrength == FGA_FILTER_STRENGTH ) |
1010 | 0 | { |
1011 | | // the picture filtered for the encoding is exactly the one the analysis needs (from QP 20 on with MCTFLevelTilt |
1012 | | // off; the tilt raises the key pictures' strength, so the analysis filters its own copy) |
1013 | 0 | pic->m_picShared->fgaUsesFilteredBuf() = true; |
1014 | 0 | } |
1015 | 0 | else |
1016 | 0 | { |
1017 | 0 | PelStorage& fgaBuf = pic->m_picShared->getFgaFilteredBuf(); |
1018 | 0 | fgaBuf.create( m_encCfg->m_internChromaFormat, m_area ); |
1019 | 0 | bilateralFilter( origBuf, srcFrameInfo, fgaBuf, FGA_FILTER_STRENGTH ); |
1020 | 0 | } |
1021 | | |
1022 | | // The filtered picture gives the flat areas. With the improvements (--fga 2) the grain itself is measured from the |
1023 | | // temporal residual of the picture and its neighbours, whose motion has just been estimated. |
1024 | 0 | pic->m_picShared->fgaResidualWeight() = m_encCfg->m_fga >= 2 |
1025 | 0 | ? fgaTemporalResidual( origBuf, pic->poc, picFifo, srcFrameInfo, pic->m_picShared->getFgaResidualBuf() ) : 0; |
1026 | 0 | } |
1027 | |
|
1028 | 0 | if( m_encCfg->hasBlockQpOffsets() || m_encCfg->m_usePerceptQPA || pic->gopEntry->m_isStartOfGop ) |
1029 | 0 | { |
1030 | 0 | const int ctuSize = m_encCfg->m_bimCtuSize; |
1031 | 0 | const int widthInCtus = ( m_area.width + ctuSize - 1 ) / ctuSize; |
1032 | 0 | const int heightInCtus = ( m_area.height + ctuSize - 1 ) / ctuSize; |
1033 | 0 | const int numCtu = widthInCtus * heightInCtus; |
1034 | 0 | const int ctuBlocks = ctuSize / m_mctfUnitSize; |
1035 | |
|
1036 | 0 | std::vector<double> sumError( numCtu * 2, 0 ); |
1037 | 0 | std::vector<uint32_t> sumRMS( numCtu * 2, 0 ); // RMS of motion estimation error |
1038 | 0 | std::vector<uint16_t> maxRMS( numCtu * 2, 0 ); // maximum block estimation error |
1039 | 0 | std::vector<double> blkCount( numCtu * 2, 0 ); |
1040 | |
|
1041 | 0 | int distFactor[2] = { 3,3 }; |
1042 | |
|
1043 | 0 | for( auto& srcPic : srcFrameInfo ) |
1044 | 0 | { |
1045 | 0 | if( srcPic.index >= 2 ) |
1046 | 0 | { |
1047 | 0 | continue; |
1048 | 0 | } |
1049 | | |
1050 | 0 | int dist = srcPic.index; |
1051 | 0 | distFactor[dist]--; |
1052 | |
|
1053 | 0 | for( int y = 0; y < srcPic.mvs.h(); y++ ) // going over ref pic in block steps |
1054 | 0 | { |
1055 | 0 | for( int x = 0; x < srcPic.mvs.w(); x++ ) |
1056 | 0 | { |
1057 | 0 | const int ctuX = x / ctuBlocks; |
1058 | 0 | const int ctuY = y / ctuBlocks; |
1059 | 0 | const int ctuId = ctuY * widthInCtus + ctuX; |
1060 | 0 | const auto& mvBlk = srcPic.mvs.get( x, y ); |
1061 | 0 | sumError[dist * numCtu + ctuId] += mvBlk.error; |
1062 | 0 | sumRMS [dist * numCtu + ctuId] += mvBlk.rmsme; |
1063 | 0 | maxRMS [dist * numCtu + ctuId] = std::max( maxRMS[dist * numCtu + ctuId], mvBlk.rmsme ); |
1064 | 0 | blkCount[dist * numCtu + ctuId] += mvBlk.overlap; |
1065 | 0 | } |
1066 | 0 | } |
1067 | 0 | } |
1068 | |
|
1069 | 0 | if( distFactor[0] < 3 && distFactor[1] < 3 && ( m_encCfg->m_usePerceptQPA || pic->gopEntry->m_isStartOfGop ) ) |
1070 | 0 | { |
1071 | 0 | const double bd12bScale = double (m_encCfg->m_internalBitDepth[CH_L] < 12 ? 4 : 1); |
1072 | |
|
1073 | 0 | for( int i = 0; i < numCtu; i++ ) // start noise estimation with motion errors |
1074 | 0 | { |
1075 | 0 | const Position pos ((i % widthInCtus) * ctuSize, (i / widthInCtus) * ctuSize); |
1076 | 0 | const CompArea ctuArea = clipArea (CompArea (COMP_Y, pic->chromaFormat, Area (pos.x, pos.y, ctuSize, ctuSize)), pic->Y()); |
1077 | 0 | const unsigned avgIndex = pic->getOrigBuf (ctuArea).getAvg() >> (m_encCfg->m_internalBitDepth[CH_L] - 3); // one of 8 mean level regions |
1078 | 0 | double meanInCTU; |
1079 | |
|
1080 | 0 | sumRMS[i] = std::min (sumRMS[i], sumRMS[i + numCtu]); |
1081 | 0 | meanInCTU = bd12bScale * sumRMS[i] / blkCount[i]; |
1082 | 0 | meanRmsAcrossPic += meanInCTU; |
1083 | 0 | if (meanInCTU < pic->m_picShared->m_minNoiseLevels[avgIndex]) |
1084 | 0 | { |
1085 | 0 | pic->m_picShared->m_minNoiseLevels[avgIndex] = uint8_t (0.5 + meanInCTU); // scaled to 12 bit, see filterAndCalculateAverageActivity() |
1086 | 0 | } |
1087 | |
|
1088 | 0 | maxRMS[i] = std::min (maxRMS[i], maxRMS[i + numCtu]); |
1089 | 0 | maxRmsCTU = std::max (maxRmsCTU, maxRMS[i]); |
1090 | 0 | sumSRmsAcrossPic += (uint64_t) maxRMS[i] * maxRMS[i]; |
1091 | 0 | if (maxRMS[i] > 0) |
1092 | 0 | { |
1093 | 0 | nMax++; // count all CTUs with non-zero motion error (excludes e.g. black borders). CTU with the motion error peak is subtracted below |
1094 | 0 | } |
1095 | 0 | } |
1096 | 0 | pic->m_picShared->m_picMotEstError = uint16_t (0.5 + meanRmsAcrossPic / numCtu); |
1097 | |
|
1098 | 0 | if( pic->gopEntry->m_isStartOfGop && !pic->useMCTF && m_encCfg->m_vvencMCTF.MCTF > 0 && meanRmsAcrossPic > numCtu * 27.0 ) |
1099 | 0 | { |
1100 | | // check application (re-enabling) of MCTF filter for key pictures, in case MCTF has been disabled based on SCC detection |
1101 | 0 | bool allNoiseZero = true; |
1102 | 0 | for( int i = 0; i < QPA_MAX_NOISE_LEVELS; i++ ) |
1103 | 0 | { |
1104 | 0 | if( pic->m_picShared->m_minNoiseLevels[i] && pic->m_picShared->m_minNoiseLevels[i] < 255 ) |
1105 | 0 | { |
1106 | 0 | allNoiseZero = false; |
1107 | 0 | break; |
1108 | 0 | } |
1109 | 0 | } |
1110 | 0 | int numZeroRMSCtus = 0; |
1111 | 0 | if( allNoiseZero ) |
1112 | 0 | { |
1113 | 0 | for( int i = 0; i < numCtu; i++ ) |
1114 | 0 | { |
1115 | 0 | if( sumRMS[i] == 0 ) |
1116 | 0 | { |
1117 | 0 | numZeroRMSCtus += 1; |
1118 | 0 | } |
1119 | 0 | } |
1120 | 0 | } |
1121 | 0 | const bool doFilter = ( numZeroRMSCtus * 100 <= numCtu * 6 ); |
1122 | 0 | if( doFilter ) |
1123 | 0 | { |
1124 | 0 | fltrBuf.create( m_encCfg->m_internChromaFormat, m_area, 0, MCTF_FRAME_MARGIN ); |
1125 | 0 | bilateralFilter( origBuf, srcFrameInfo, fltrBuf, filterStrength ); |
1126 | 0 | } |
1127 | 0 | } |
1128 | 0 | } |
1129 | 0 | if (m_encCfg->m_forceScc <= 0) |
1130 | 0 | { |
1131 | 0 | bool forceSCC = false; |
1132 | 0 | if (pic->gopEntry->m_isStartOfGop) |
1133 | 0 | { |
1134 | 0 | forceSCC = true; |
1135 | 0 | for (int j = 0; j < QPA_MAX_NOISE_LEVELS; j++) |
1136 | 0 | { |
1137 | 0 | if (pic->m_picShared->m_minNoiseLevels[j] < 255 && pic->m_picShared->m_minNoiseLevels[j]) |
1138 | 0 | { |
1139 | 0 | forceSCC = false; |
1140 | 0 | break; |
1141 | 0 | } |
1142 | 0 | } |
1143 | 0 | if (forceSCC) |
1144 | 0 | { |
1145 | 0 | for (int s = 0; s < mvErr.size(); s++) |
1146 | 0 | { |
1147 | 0 | if (int(mvErr[s]) == 0) |
1148 | 0 | { |
1149 | 0 | forceSCC = false; |
1150 | 0 | break; |
1151 | 0 | } |
1152 | 0 | } |
1153 | 0 | } |
1154 | 0 | } |
1155 | 0 | pic->m_picShared->m_forceSCC = forceSCC; |
1156 | 0 | } |
1157 | | |
1158 | | // BIM 2 uses these offsets as they are. CU-tree (BIM 1) keeps their picture offset, m_picAuxQpOffset, and |
1159 | | // replaces the CTU offsets with its own. |
1160 | 0 | if( !m_encCfg->hasBlockQpOffsets() || !pic->useMCTF ) |
1161 | 0 | { |
1162 | 0 | CHECKD( !pic->m_picShared->m_ctuBimQpOffset.empty(), "BIM disabled, but offset vector not empty!" ); |
1163 | 0 | return; |
1164 | 0 | } |
1165 | | |
1166 | 0 | pic->m_picShared->m_ctuBimQpOffset.resize( numCtu, 0 ); |
1167 | |
|
1168 | 0 | if( distFactor[0] < 3 && distFactor[1] < 3 ) |
1169 | 0 | { |
1170 | 0 | const double weight = std::min( 1.0, overallStrength ); |
1171 | 0 | const double factor = std::min( 1.0, sqrt((1920.0 * 1080.0) / double (m_encCfg->m_SourceWidth * m_encCfg->m_SourceHeight)) ) * ( ( m_encCfg->m_QP + m_encCfg->m_QPFraction ) / (MAX_QP + 1.0) ); |
1172 | 0 | int sumCtuQpOffsets = 0; |
1173 | |
|
1174 | 0 | meanRmsAcrossPic = (!m_encCfg->m_usePerceptQPA || !m_encCfg->m_salienceBasedOpt || maxRmsCTU == 0 || nMax < 2 ? 65535.0 : sqrt (double (sumSRmsAcrossPic - (uint64_t) maxRmsCTU * maxRmsCTU) / (nMax - 1.0))); |
1175 | |
|
1176 | 0 | for( int i = 0; i < numCtu; i++ ) |
1177 | 0 | { |
1178 | 0 | const int avgErrD1 = ( int ) ( ( sumError[i ] / blkCount[i ] ) * distFactor[0] ); |
1179 | 0 | const int avgErrD2 = ( int ) ( ( sumError[i + numCtu] / blkCount[i + numCtu] ) * distFactor[1] ); |
1180 | 0 | int weightedErr = std::max( avgErrD1, avgErrD2 ) + abs( avgErrD2 - avgErrD1 ) * 3; |
1181 | 0 | weightedErr = ( int ) ( weightedErr * weight + ( 1 - weight ) * m_cuTreeCenter ); |
1182 | |
|
1183 | 0 | int qpOffset = 0; |
1184 | |
|
1185 | 0 | if( weightedErr > m_cuTreeThresh[0] ) |
1186 | 0 | { |
1187 | 0 | qpOffset = 2; |
1188 | 0 | } |
1189 | 0 | else if( weightedErr > m_cuTreeThresh[1] ) |
1190 | 0 | { |
1191 | 0 | qpOffset = 1; |
1192 | 0 | } |
1193 | 0 | else if( weightedErr < m_cuTreeThresh[3] ) |
1194 | 0 | { |
1195 | 0 | qpOffset = -2; |
1196 | 0 | } |
1197 | 0 | else if( weightedErr < m_cuTreeThresh[2] ) |
1198 | 0 | { |
1199 | 0 | qpOffset = -1; |
1200 | 0 | } |
1201 | |
|
1202 | 0 | if (meanRmsAcrossPic < maxRMS[i] * factor) |
1203 | 0 | { |
1204 | 0 | qpOffset += int (6.0 * log (std::max ((ctuSize > 64 ? 0.625 : 0.5) * maxRMS[i] * factor, meanRmsAcrossPic) / (maxRMS[i] * factor)) / (sqrt (weight) * log (2.0)) - 0.5); |
1205 | 0 | } |
1206 | |
|
1207 | 0 | pic->m_picShared->m_ctuBimQpOffset[i] = qpOffset; |
1208 | 0 | sumCtuQpOffsets += qpOffset; |
1209 | 0 | } |
1210 | |
|
1211 | 0 | pic->m_picShared->m_picAuxQpOffset = ( sumCtuQpOffsets + ( sumCtuQpOffsets < 0 ? -(numCtu >> 1) : numCtu >> 1 ) ) / numCtu; // pic average |
1212 | 0 | for( int i = 0; i < numCtu; i++ ) |
1213 | 0 | { |
1214 | 0 | pic->m_picShared->m_ctuBimQpOffset[i] -= pic->m_picShared->m_picAuxQpOffset; // delta-QP relative to above average, see xGetQPForPicture |
1215 | 0 | } |
1216 | 0 | } |
1217 | 0 | else |
1218 | 0 | { |
1219 | 0 | std::fill( pic->m_picShared->m_ctuBimQpOffset.begin(), pic->m_picShared->m_ctuBimQpOffset.end(), 0 ); |
1220 | 0 | } |
1221 | 0 | } |
1222 | 0 | } |
1223 | 0 | else |
1224 | 0 | { |
1225 | 0 | pic->m_picShared->m_ctuBimQpOffset.resize( 0 ); |
1226 | 0 | } |
1227 | 0 | } |
1228 | | |
1229 | | // ==================================================================================================================== |
1230 | | // Private member functions |
1231 | | // ==================================================================================================================== |
1232 | | |
1233 | | void MCTF::subsampleLuma(const PelStorage &input, PelStorage &output, const int factor) const |
1234 | 0 | { |
1235 | 0 | const int newWidth = input.Y().width / factor; |
1236 | 0 | const int newHeight = input.Y().height / factor; |
1237 | 0 | output.create(CHROMA_400, Area(0, 0, newWidth, newHeight), 0, m_padding); |
1238 | |
|
1239 | 0 | CHECK( factor != 2, "Only 2x2 subsampling is supported" ); |
1240 | 0 | g_pelBufOP.downsample2x2( input.Y().buf, input.Y().stride, output.Y().buf, output.Y().stride, newWidth, newHeight ); |
1241 | 0 | output.extendBorderPel(m_padding, m_padding); |
1242 | 0 | } |
1243 | | |
1244 | | // Film grain analysis (--fga 2): a neighbour whose mean RMS motion compensation error is above twice that of the other |
1245 | | // neighbour, plus this many 10-bit sample values, is left out of the temporal residual (a scene cut). |
1246 | | static constexpr double FGA_NEIGHBOUR_RMS_MARGIN = 1.0; |
1247 | | |
1248 | | // Mean RMS motion compensation error of the blocks with an estimate, in 10-bit sample values; -1 without any. |
1249 | | static double fgaMeanMotionRms( const Array2D<MotionVector>& mvs ) |
1250 | 0 | { |
1251 | 0 | double sum = 0.0; |
1252 | 0 | int num = 0; |
1253 | 0 | for( int y = 0; y < mvs.h(); y++ ) |
1254 | 0 | { |
1255 | 0 | for( int x = 0; x < mvs.w(); x++ ) |
1256 | 0 | { |
1257 | 0 | const MotionVector& mv = mvs.get( x, y ); |
1258 | 0 | if( mv.error < INT_LEAST32_MAX ) |
1259 | 0 | { |
1260 | 0 | sum += mv.rmsme; |
1261 | 0 | num++; |
1262 | 0 | } |
1263 | 0 | } |
1264 | 0 | } |
1265 | 0 | return num ? sum / num : -1.0; |
1266 | 0 | } |
1267 | | |
1268 | | // The w x h block of ref at (x, y), in full samples. The input pictures keep MCTF_FRAME_MARGIN samples of replicated |
1269 | | // border, scaled down for chroma to margin; a block that leaves it is copied into tmp with clamped coordinates, which |
1270 | | // gives the same samples as a wider replicated border. |
1271 | | static const Pel* fgaRefBlock( const CPelBuf& ref, int margin, int x, int y, int w, int h, Pel* tmp, ptrdiff_t& stride ) |
1272 | 0 | { |
1273 | 0 | if( x >= -margin && y >= -margin && x + w <= ( int ) ref.width + margin && y + h <= ( int ) ref.height + margin ) |
1274 | 0 | { |
1275 | 0 | stride = ref.stride; |
1276 | 0 | return ref.buf + y * ref.stride + x; |
1277 | 0 | } |
1278 | 0 | for( int r = 0; r < h; r++ ) |
1279 | 0 | { |
1280 | 0 | const Pel* src = ref.buf + Clip3( 0, ( int ) ref.height - 1, y + r ) * ref.stride; |
1281 | 0 | for( int c = 0; c < w; c++ ) |
1282 | 0 | { |
1283 | 0 | tmp[ r * w + c ] = src[ Clip3( 0, ( int ) ref.width - 1, x + c ) ]; |
1284 | 0 | } |
1285 | 0 | } |
1286 | 0 | stride = w; |
1287 | 0 | return tmp; |
1288 | 0 | } |
1289 | | |
1290 | | // The vector of a block for the temporal residual. MCTF refines its vectors on the pictures with their grain, so on |
1291 | | // static content they partly line the neighbour's grain up with this picture's, and the residual reads the grain low |
1292 | | // (5-15 % on Beekeeper and nocturne). A vector that rounds to within one luma sample of zero is taken as zero, for all |
1293 | | // components alike (decided on luma: a halved chroma vector would round differently by its sign). The choice does not |
1294 | | // look at the residual, so it adds no bias of its own. On flat blocks a motion below one sample misaligns nothing that |
1295 | | // matters; where the block has texture the analyzer leaves it out (FGAnalyzer::markMotionFailures). Known limit: a |
1296 | | // slow pan of about one sample per picture is measured with zero vectors. |
1297 | | static MotionVector fgaResidualVector( const MotionVector& mv ) |
1298 | 0 | { |
1299 | 0 | if( std::abs( ( mv.x + 8 ) >> 4 ) <= 1 && std::abs( ( mv.y + 8 ) >> 4 ) <= 1 ) |
1300 | 0 | { |
1301 | 0 | MotionVector zero = mv; |
1302 | 0 | zero.x = zero.y = 0; |
1303 | 0 | return zero; |
1304 | 0 | } |
1305 | 0 | return mv; |
1306 | 0 | } |
1307 | | |
1308 | | // For grain that is independent from picture to picture, the residual e = 2 O - P - N of a picture O and its motion |
1309 | | // compensated neighbours P and N has six times the grain variance and the spectrum of the grain, whatever the filter |
1310 | | // keeps of it (one neighbour: e = O - P, twice the grain variance). The vectors are the filter's own (hierarchical |
1311 | | // search over a wide range, 1/16 sample), rounded to full samples: interpolating the neighbours would low-pass their |
1312 | | // grain and bias the measured spectrum. |
1313 | | int MCTF::fgaTemporalResidual( const PelStorage& orig, int poc, const std::deque<Picture*>& picFifo, |
1314 | | const std::deque<TemporalFilterSourcePicInfo>& srcFrameInfo, |
1315 | | PelStorage& residual ) const |
1316 | 0 | { |
1317 | | // the entries of the neighbours: an entry's buffer wraps the original of its picture |
1318 | 0 | const TemporalFilterSourcePicInfo* prev = nullptr; |
1319 | 0 | const TemporalFilterSourcePicInfo* next = nullptr; |
1320 | 0 | for( const Picture* p : picFifo ) |
1321 | 0 | { |
1322 | 0 | if( p->poc != poc - 1 && p->poc != poc + 1 ) |
1323 | 0 | { |
1324 | 0 | continue; |
1325 | 0 | } |
1326 | 0 | for( const TemporalFilterSourcePicInfo& src : srcFrameInfo ) |
1327 | 0 | { |
1328 | 0 | if( src.picBuffer.Y().buf == p->getOrigBuf( COMP_Y ).buf ) |
1329 | 0 | { |
1330 | 0 | ( p->poc < poc ? prev : next ) = &src; |
1331 | 0 | } |
1332 | 0 | } |
1333 | 0 | } |
1334 | 0 | if( prev && next ) |
1335 | 0 | { |
1336 | 0 | const double rmsPrev = fgaMeanMotionRms( prev->mvs ); |
1337 | 0 | const double rmsNext = fgaMeanMotionRms( next->mvs ); |
1338 | 0 | if( rmsPrev > 2.0 * rmsNext + FGA_NEIGHBOUR_RMS_MARGIN && rmsNext >= 0.0 ) |
1339 | 0 | { |
1340 | 0 | prev = nullptr; |
1341 | 0 | } |
1342 | 0 | else if( rmsNext > 2.0 * rmsPrev + FGA_NEIGHBOUR_RMS_MARGIN && rmsPrev >= 0.0 ) |
1343 | 0 | { |
1344 | 0 | next = nullptr; |
1345 | 0 | } |
1346 | 0 | } |
1347 | 0 | if( !prev && !next ) |
1348 | 0 | { |
1349 | 0 | return 0; |
1350 | 0 | } |
1351 | | // the first neighbour of the residual, and the second one if both are used |
1352 | 0 | const TemporalFilterSourcePicInfo* first = prev ? prev : next; |
1353 | 0 | const TemporalFilterSourcePicInfo* second = prev ? next : nullptr; |
1354 | |
|
1355 | 0 | const ChromaFormat chromaFormat = m_encCfg->m_internChromaFormat; |
1356 | 0 | residual.destroy(); |
1357 | 0 | residual.create( chromaFormat, Area( 0, 0, orig.Y().width, orig.Y().height ) ); |
1358 | 0 | std::vector<Pel> tmpFirst( m_mctfUnitSize * m_mctfUnitSize ), tmpSecond( m_mctfUnitSize * m_mctfUnitSize ); |
1359 | 0 | for( int c = 0; c < getNumberValidComponents( chromaFormat ); c++ ) |
1360 | 0 | { |
1361 | 0 | const ComponentID compId = ComponentID( c ); |
1362 | 0 | const int csx = getComponentScaleX( compId, chromaFormat ); |
1363 | 0 | const int csy = getComponentScaleY( compId, chromaFormat ); |
1364 | 0 | const int bw = m_mctfUnitSize >> csx; |
1365 | 0 | const int bh = m_mctfUnitSize >> csy; |
1366 | 0 | const int margin = MCTF_FRAME_MARGIN >> std::max( csx, csy ); |
1367 | 0 | const CPelBuf o = orig.get( compId ); |
1368 | 0 | PelBuf r = residual.get( compId ); |
1369 | 0 | for( int by = 0; by < first->mvs.h(); by++ ) |
1370 | 0 | { |
1371 | 0 | const int y0 = by * bh; |
1372 | 0 | const int h = std::min( bh, ( int ) o.height - y0 ); |
1373 | 0 | for( int bx = 0; bx < first->mvs.w(); bx++ ) |
1374 | 0 | { |
1375 | 0 | const int x0 = bx * bw; |
1376 | 0 | const int w = std::min( bw, ( int ) o.width - x0 ); |
1377 | 0 | if( w <= 0 || h <= 0 ) |
1378 | 0 | { |
1379 | 0 | continue; |
1380 | 0 | } |
1381 | | // the vectors in full samples of this component, rounded (1/16 luma samples, ( 1 << csx ) luma samples per sample) |
1382 | 0 | const MotionVector mvFirst = fgaResidualVector( first->mvs.get( bx, by ) ); |
1383 | 0 | ptrdiff_t strideFirst = 0, strideSecond = 0; |
1384 | 0 | const Pel* f = fgaRefBlock( first->picBuffer.get( compId ), margin, x0 + ( ( mvFirst.x + ( 8 << csx ) ) >> ( 4 + csx ) ), |
1385 | 0 | y0 + ( ( mvFirst.y + ( 8 << csy ) ) >> ( 4 + csy ) ), w, h, tmpFirst.data(), strideFirst ); |
1386 | 0 | const Pel* s = nullptr; |
1387 | 0 | if( second ) |
1388 | 0 | { |
1389 | 0 | const MotionVector mvSecond = fgaResidualVector( second->mvs.get( bx, by ) ); |
1390 | 0 | s = fgaRefBlock( second->picBuffer.get( compId ), margin, x0 + ( ( mvSecond.x + ( 8 << csx ) ) >> ( 4 + csx ) ), |
1391 | 0 | y0 + ( ( mvSecond.y + ( 8 << csy ) ) >> ( 4 + csy ) ), w, h, tmpSecond.data(), strideSecond ); |
1392 | 0 | } |
1393 | 0 | for( int y = 0; y < h; y++ ) |
1394 | 0 | { |
1395 | 0 | m_fgaKernels.residualRow( o.bufAt( x0, y0 + y ), f + y * strideFirst, s ? s + y * strideSecond : nullptr, w, |
1396 | 0 | r.bufAt( x0, y0 + y ) ); |
1397 | 0 | } |
1398 | 0 | } |
1399 | 0 | } |
1400 | 0 | } |
1401 | 0 | return second ? 6 : 2; |
1402 | 0 | } |
1403 | | |
1404 | | // The interpolation kernels above read up to 2 samples before and 3 samples after a block. The |
1405 | | // bounds below include one more sample on each side. |
1406 | | static constexpr int REF_TAPS_BEFORE = 3; |
1407 | | static constexpr int REF_TAPS_AFTER = 4; |
1408 | | static constexpr int REF_MAX_BLOCK = 64; // largest block width and height the kernels accept |
1409 | | |
1410 | | // Copies the w x h reference block at (x, y) with its filter taps into a per-thread buffer, clamping |
1411 | | // the coordinates to the frame, and returns the block's first sample. |
1412 | | static const Pel* copyClampedRefBlock( const CPelBuf& ref, const int x, const int y, const int w, const int h, ptrdiff_t& stride ) |
1413 | 0 | { |
1414 | 0 | CHECK( w > REF_MAX_BLOCK || h > REF_MAX_BLOCK, "MCTF reference block too large" ); |
1415 | | |
1416 | | // A thread uses one copied block at a time, so one buffer per thread suffices. |
1417 | 0 | static thread_local Pel tmp[( REF_MAX_BLOCK + REF_TAPS_BEFORE + REF_TAPS_AFTER ) * ( REF_MAX_BLOCK + REF_TAPS_BEFORE + REF_TAPS_AFTER )]; |
1418 | |
|
1419 | 0 | const int left = x - REF_TAPS_BEFORE; |
1420 | 0 | const int top = y - REF_TAPS_BEFORE; |
1421 | 0 | const int cols = w + REF_TAPS_BEFORE + REF_TAPS_AFTER; |
1422 | 0 | const int rows = h + REF_TAPS_BEFORE + REF_TAPS_AFTER; |
1423 | 0 | const int width = ( int ) ref.width; |
1424 | | |
1425 | | // Columns [inBegin, inEnd) of the copy lie inside the frame; those left of it repeat the first |
1426 | | // sample of the row and those right of it the last one. |
1427 | 0 | const int inBegin = Clip3( 0, cols, -left ); |
1428 | 0 | const int inEnd = Clip3( 0, cols, width - left ); |
1429 | |
|
1430 | 0 | for( int r = 0; r < rows; r++ ) |
1431 | 0 | { |
1432 | 0 | const Pel* src = ref.buf + Clip3( 0, ( int ) ref.height - 1, top + r ) * ref.stride; |
1433 | 0 | Pel* dst = tmp + r * cols; |
1434 | 0 | if( inBegin < inEnd ) |
1435 | 0 | { |
1436 | 0 | std::fill_n( dst, inBegin, src[0] ); |
1437 | 0 | memcpy( dst + inBegin, src + left + inBegin, ( inEnd - inBegin ) * sizeof( Pel ) ); |
1438 | 0 | std::fill_n( dst + inEnd, cols - inEnd, src[width - 1] ); |
1439 | 0 | } |
1440 | 0 | else |
1441 | 0 | { |
1442 | | // The whole row of the copy lies right of the frame (inEnd == 0) or left of it. |
1443 | 0 | std::fill_n( dst, cols, inEnd == 0 ? src[width - 1] : src[0] ); |
1444 | 0 | } |
1445 | 0 | } |
1446 | |
|
1447 | 0 | stride = cols; |
1448 | 0 | return tmp + REF_TAPS_BEFORE * cols + REF_TAPS_BEFORE; |
1449 | 0 | } |
1450 | | |
1451 | | // Whether the w x h reference block at (x, y) and its filter taps lie inside the reference's border of margin samples. |
1452 | | static inline bool refBlockInBorder( const CPelBuf& ref, const int margin, const int x, const int y, const int w, const int h ) |
1453 | 0 | { |
1454 | 0 | return x - REF_TAPS_BEFORE >= -margin && y - REF_TAPS_BEFORE >= -margin |
1455 | 0 | && x + w + REF_TAPS_AFTER <= ( int ) ref.width + margin && y + h + REF_TAPS_AFTER <= ( int ) ref.height + margin; |
1456 | 0 | } |
1457 | | |
1458 | | // Returns the samples of the w x h reference block at (x, y) and sets stride. The input frames only |
1459 | | // keep MCTF_FRAME_MARGIN samples of replicated border, while the motion search reaches up to about |
1460 | | // 100 samples outside the frame. A block whose filter taps leave the border is copied with clamped |
1461 | | // coordinates, which gives the same samples as a wider replicated border. |
1462 | | static inline const Pel* getRefBlock( const CPelBuf& ref, const int margin, const int x, const int y, const int w, const int h, ptrdiff_t& stride ) |
1463 | 0 | { |
1464 | 0 | if( refBlockInBorder( ref, margin, x, y, w, h ) ) |
1465 | 0 | { |
1466 | 0 | stride = ref.stride; |
1467 | 0 | return ref.buf + y * ref.stride + x; |
1468 | 0 | } |
1469 | 0 | return copyClampedRefBlock( ref, x, y, w, h, stride ); |
1470 | 0 | } |
1471 | | |
1472 | | int MCTF::motionErrorLuma(const PelStorage &orig, |
1473 | | const PelStorage &buffer, |
1474 | | const int refMargin, |
1475 | | const int x, |
1476 | | const int y, |
1477 | | int dx, |
1478 | | int dy, |
1479 | | const int bs, |
1480 | | const int besterror = MAX_INT) const |
1481 | 0 | { |
1482 | 0 | int fx = dx & 0xf; |
1483 | 0 | int fy = dy & 0xf; |
1484 | |
|
1485 | 0 | int error = 0;// dx * 10 + dy * 10; |
1486 | |
|
1487 | 0 | CHECKD( bs & 7, "Blocksize has to be a multiple of 8!" ); |
1488 | |
|
1489 | 0 | const int w = std::min<int>( bs, orig.Y().width - x ) & ~7; |
1490 | 0 | const int h = std::min<int>( bs, orig.Y().height - y ) & ~7; |
1491 | |
|
1492 | 0 | CHECK( !w || !h, "Incompatible sizes!" ); |
1493 | |
|
1494 | 0 | const int origStride = orig.Y().stride; |
1495 | 0 | const Pel* org = orig.Y().buf + x + y * origStride; |
1496 | | // The motion vector is in 1/16 samples; the reference block starts at its integer part. |
1497 | 0 | ptrdiff_t buffStride = 0; |
1498 | 0 | const Pel* buf = getRefBlock( buffer.Y(), refMargin, x + ( dx >> 4 ), y + ( dy >> 4 ), w, h, buffStride ); |
1499 | |
|
1500 | 0 | if( ( fx | fy ) == 0 ) |
1501 | 0 | { |
1502 | 0 | return m_motionErrorLumaInt8( org, origStride, buf, buffStride, w, h, besterror ); |
1503 | 0 | } |
1504 | 0 | else if( m_lowResFltSearch ) |
1505 | 0 | { |
1506 | 0 | const int16_t *xFilter = m_interpolationFilter4[fx]; |
1507 | 0 | const int16_t *yFilter = m_interpolationFilter4[fy]; |
1508 | |
|
1509 | 0 | return m_motionErrorLumaFrac8[1]( org, origStride, buf, buffStride, w, h, xFilter, yFilter, m_encCfg->m_internalBitDepth[CH_L], besterror ); |
1510 | 0 | } |
1511 | 0 | else |
1512 | 0 | { |
1513 | 0 | const int16_t *xFilter = m_interpolationFilter8[fx]; |
1514 | 0 | const int16_t *yFilter = m_interpolationFilter8[fy]; |
1515 | |
|
1516 | 0 | return m_motionErrorLumaFrac8[0]( org, origStride, buf, buffStride, w,h, xFilter, yFilter, m_encCfg->m_internalBitDepth[CH_L], besterror ); |
1517 | 0 | } |
1518 | | |
1519 | 0 | return error; |
1520 | 0 | } |
1521 | | |
1522 | | // One refinement step of the subpel search: the vectors center + ( x2, y2 ) for x2 and y2 from -range to range in steps |
1523 | | // of step, except the center itself, tested in row order (only a smaller error replaces the best, so the first of equal |
1524 | | // errors wins). Where a column kernel exists, the vectors that share a fractional horizontal position are filtered |
1525 | | // horizontally once and their exact errors are replayed in the same order. That picks the same best vector with the same |
1526 | | // error as testing them one by one, on every SIMD level: every per-vector kernel (C, SSE, AVX2, NEON, SVE) returns the exact |
1527 | | // error when it is below the best error passed in and some value not below it otherwise, so a vector replaces the best |
1528 | | // in exactly the same cases. Every other vector is tested as before, with the early exit against the best error so far. |
1529 | | // The filtered rows of a column are kept in columns, so a later step of the same block that needs rows of the same |
1530 | | // horizontal position (the column of the best vector so far) takes them from there: they depend only on that position |
1531 | | // and the reference rows. |
1532 | | void MCTF::testSubpelLattice( const PelStorage& orig, const PelStorage& buffer, const int refMargin, const int x, const int y, const int bs, |
1533 | | const MotionVector& center, const int range, const int step, FilteredColumns& columns, MotionVector& best ) const |
1534 | 0 | { |
1535 | 0 | static constexpr int MAX_VECTORS = 48; // ( 2 * 12 / 4 + 1 )^2 - 1: the widest lattice (MCTFSpeed 0) |
1536 | 0 | static constexpr int MAX_COLUMN = 4; // vectors per column in the widest 4-tap lattice, y2 in { -6, -2, 2, 6 } |
1537 | |
|
1538 | 0 | int dx[MAX_VECTORS], dy[MAX_VECTORS], error[MAX_VECTORS]; |
1539 | 0 | bool known[MAX_VECTORS]; |
1540 | 0 | int num = 0; |
1541 | 0 | for( int y2 = -range; y2 <= range; y2 += step ) |
1542 | 0 | { |
1543 | 0 | for( int x2 = -range; x2 <= range; x2 += step ) |
1544 | 0 | { |
1545 | 0 | if( x2 || y2 ) |
1546 | 0 | { |
1547 | 0 | CHECKD( num == MAX_VECTORS, "Too many subpel vectors" ); |
1548 | 0 | dx[num] = center.x + x2; |
1549 | 0 | dy[num] = center.y + y2; |
1550 | 0 | known[num] = false; |
1551 | 0 | num++; |
1552 | 0 | } |
1553 | 0 | } |
1554 | 0 | } |
1555 | | |
1556 | 0 | const int w = std::min<int>( bs, orig.Y().width - x ) & ~7; |
1557 | 0 | const int h = std::min<int>( bs, orig.Y().height - y ) & ~7; |
1558 | 0 | if( m_lowResFltSearch && w >= 16 && ( w & 15 ) == 0 ) |
1559 | 0 | { |
1560 | 0 | CHECKD( w > FilteredColumns::MAX_WIDTH || h > FilteredColumns::MAX_WIDTH, "The block is larger than MCTFUnitSize allows" ); |
1561 | 0 | for( int i = 0; i < num; i++ ) |
1562 | 0 | { |
1563 | 0 | if( known[i] || ( dx[i] & 15 ) == 0 ) |
1564 | 0 | { |
1565 | 0 | continue; // done, or a full-sample horizontal position: no horizontal pass to share |
1566 | 0 | } |
1567 | 0 | int column[MAX_COLUMN], n = 0; |
1568 | 0 | int top = dy[i] >> 4, bottom = top; |
1569 | 0 | bool fits = true; |
1570 | 0 | for( int j = i; j < num; j++ ) |
1571 | 0 | { |
1572 | 0 | if( dx[j] == dx[i] ) |
1573 | 0 | { |
1574 | 0 | if( n == MAX_COLUMN ) |
1575 | 0 | { |
1576 | 0 | fits = false; |
1577 | 0 | break; |
1578 | 0 | } |
1579 | 0 | column[n++] = j; |
1580 | 0 | top = std::min( top, dy[j] >> 4 ); |
1581 | 0 | bottom = std::max( bottom, dy[j] >> 4 ); |
1582 | 0 | } |
1583 | 0 | } |
1584 | 0 | if( !fits || n < 2 || bottom - top > 1 ) |
1585 | 0 | { |
1586 | 0 | continue; // tested one by one below |
1587 | 0 | } |
1588 | | |
1589 | 0 | const int16_t* yFilter[MAX_COLUMN]; |
1590 | 0 | int rowOffset[MAX_COLUMN], columnError[MAX_COLUMN]; |
1591 | 0 | for( int k = 0; k < n; k++ ) |
1592 | 0 | { |
1593 | 0 | yFilter[k] = m_interpolationFilter4[dy[column[k]] & 15]; |
1594 | 0 | rowOffset[k] = ( dy[column[k]] >> 4 ) - top; |
1595 | 0 | } |
1596 | | // the filtered rows of all vectors of the column: a kept column of this position whose rows include them, else |
1597 | | // the horizontal pass of h plus the one-row spread of their vertical positions (and the vertical taps), kept in a |
1598 | | // free slot or, with none left, in the last one |
1599 | 0 | const Pel* filtered = columns.find( dx[i], top, bottom, w ); |
1600 | 0 | if( !filtered ) |
1601 | 0 | { |
1602 | 0 | Pel* rows = columns.add( dx[i], top, bottom ); |
1603 | 0 | ptrdiff_t buffStride = 0; |
1604 | 0 | const Pel* buf = getRefBlock( buffer.Y(), refMargin, x + ( dx[i] >> 4 ), y + top, w, h + bottom - top, buffStride ); |
1605 | 0 | m_filterColumn4( buf, buffStride, w, h + 3 + bottom - top, m_interpolationFilter4[dx[i] & 15], m_encCfg->m_internalBitDepth[CH_L], rows ); |
1606 | 0 | filtered = rows; |
1607 | 0 | } |
1608 | 0 | m_motionErrorLumaFracColumn( orig.Y().buf + x + y * orig.Y().stride, orig.Y().stride, filtered, w, h, n, yFilter, rowOffset, |
1609 | 0 | m_encCfg->m_internalBitDepth[CH_L], columnError ); |
1610 | 0 | for( int k = 0; k < n; k++ ) |
1611 | 0 | { |
1612 | 0 | error[column[k]] = columnError[k]; |
1613 | 0 | known[column[k]] = true; |
1614 | 0 | } |
1615 | 0 | } |
1616 | 0 | } |
1617 | | |
1618 | 0 | for( int i = 0; i < num; i++ ) |
1619 | 0 | { |
1620 | 0 | const int err = known[i] ? error[i] : motionErrorLuma( orig, buffer, refMargin, x, y, dx[i], dy[i], bs, best.error ); |
1621 | 0 | if( err < best.error ) |
1622 | 0 | { |
1623 | 0 | best.set( dx[i], dy[i], err ); |
1624 | 0 | } |
1625 | 0 | } |
1626 | 0 | } |
1627 | | |
1628 | | // Whether v is a tested vector at ( x, y ). Testing a vector again cannot change the search: its error is not below the |
1629 | | // best error at its first test, and the best error only decreases. |
1630 | | static inline bool isTestedVector( const MotionVector& v, const int x, const int y ) |
1631 | 0 | { |
1632 | 0 | return v.error != INT_LEAST32_MAX && v.x == x && v.y == y; |
1633 | 0 | } |
1634 | | |
1635 | | // Whether the integer search of estimateLumaLn tested v: a full-sample vector on its lattice, every step samples from |
1636 | | // center - range to center + range in both directions (positions in full samples). |
1637 | | static inline bool testedByIntegerSearch( const MotionVector& v, const int centerX, const int centerY, const int range, const int step, const int factor ) |
1638 | 0 | { |
1639 | 0 | if( v.x % factor || v.y % factor ) |
1640 | 0 | { |
1641 | 0 | return false; |
1642 | 0 | } |
1643 | 0 | const int ix = v.x / factor - ( centerX - range ); |
1644 | 0 | const int iy = v.y / factor - ( centerY - range ); |
1645 | 0 | return ix >= 0 && ix <= 2 * range && iy >= 0 && iy <= 2 * range && ix % step == 0 && iy % step == 0; |
1646 | 0 | } |
1647 | | |
1648 | | namespace |
1649 | | { |
1650 | | // Full-sample vectors of one block, tested four at a time with the four-position kernel where it takes the block, and |
1651 | | // compared in the order they were queued. That gives the same result as testing them one at a time: a partial sum is |
1652 | | // above the best error at the call, later best errors are not larger, so it is above them too; only exact errors can |
1653 | | // win. The queued reference blocks share one stride and must stay valid until flush() tests the last 1-3 of them. |
1654 | | class IntegerVectorQueue |
1655 | | { |
1656 | | public: |
1657 | | IntegerVectorQueue( const MCTF& mctf, const Pel* org, const ptrdiff_t orgStride, const int w, const int h ) |
1658 | 0 | : m_mctf( mctf ), m_org( org ), m_orgStride( orgStride ), m_w( w ), m_h( h ) |
1659 | 0 | , m_enabled( ( w & 15 ) == 0 && w * h <= MCTF::MAX_EXACT_INT_AREA ) |
1660 | 0 | { |
1661 | 0 | } |
1662 | | |
1663 | 0 | bool enabled() const { return m_enabled; } |
1664 | | |
1665 | | bool holds( const int vx, const int vy ) const |
1666 | 0 | { |
1667 | 0 | for( int k = 0; k < m_num; k++ ) |
1668 | 0 | { |
1669 | 0 | if( m_x[k] == vx && m_y[k] == vy ) |
1670 | 0 | { |
1671 | 0 | return true; |
1672 | 0 | } |
1673 | 0 | } |
1674 | 0 | return false; |
1675 | 0 | } |
1676 | | |
1677 | | void push( const int vx, const int vy, const Pel* ref, const ptrdiff_t refStride, MotionVector& best ) |
1678 | 0 | { |
1679 | 0 | CHECKD( !m_enabled || ( m_num && refStride != m_refStride ), "The queue takes no vectors or another stride" ); |
1680 | 0 | m_x[m_num] = vx; |
1681 | 0 | m_y[m_num] = vy; |
1682 | 0 | m_ref[m_num] = ref; |
1683 | 0 | m_refStride = refStride; |
1684 | 0 | if( ++m_num == 4 ) |
1685 | 0 | { |
1686 | 0 | int errors[4]; |
1687 | 0 | m_mctf.m_motionErrorLumaInt4( m_org, m_orgStride, m_ref, m_refStride, m_w, m_h, best.error, errors ); |
1688 | 0 | for( int k = 0; k < 4; k++ ) |
1689 | 0 | { |
1690 | 0 | if( errors[k] < best.error ) |
1691 | 0 | { |
1692 | 0 | best.set( m_x[k], m_y[k], errors[k] ); |
1693 | 0 | } |
1694 | 0 | } |
1695 | 0 | m_num = 0; |
1696 | 0 | } |
1697 | 0 | } |
1698 | | |
1699 | | void flush( MotionVector& best ) |
1700 | 0 | { |
1701 | 0 | for( int k = 0; k < m_num; k++ ) |
1702 | 0 | { |
1703 | 0 | const int error = m_mctf.m_motionErrorLumaInt8( m_org, m_orgStride, m_ref[k], m_refStride, m_w, m_h, best.error ); |
1704 | 0 | if( error < best.error ) |
1705 | 0 | { |
1706 | 0 | best.set( m_x[k], m_y[k], error ); |
1707 | 0 | } |
1708 | 0 | } |
1709 | 0 | m_num = 0; |
1710 | 0 | } |
1711 | | |
1712 | | private: |
1713 | | const MCTF& m_mctf; |
1714 | | const Pel* m_org; |
1715 | | const ptrdiff_t m_orgStride; |
1716 | | const int m_w; |
1717 | | const int m_h; |
1718 | | const bool m_enabled; |
1719 | | int m_x[4]; |
1720 | | int m_y[4]; |
1721 | | const Pel* m_ref[4]; |
1722 | | ptrdiff_t m_refStride = 0; |
1723 | | int m_num = 0; |
1724 | | }; |
1725 | | } // namespace |
1726 | | |
1727 | | // The integer search: the full-sample vectors ( centerX + x2, centerY + y2 ) for x2 and y2 from -range to range in |
1728 | | // steps of step, in row order, except the vector tested (tested already). All of them read inside one window around |
1729 | | // the block, so the window is checked against the reference's border, and copied with clamped coordinates where it |
1730 | | // leaves the border, once per block instead of once per vector. The samples are the same either way: the border |
1731 | | // replicates the picture's edge samples, as the clamped copy does. |
1732 | | void MCTF::testIntegerLattice( const PelStorage& orig, const PelStorage& buffer, const int refMargin, const int x, const int y, const int bs, |
1733 | | const int centerX, const int centerY, const int range, const int step, const MotionVector& tested, MotionVector& best ) const |
1734 | 0 | { |
1735 | 0 | const int w = std::min<int>( bs, orig.Y().width - x ) & ~7; |
1736 | 0 | const int h = std::min<int>( bs, orig.Y().height - y ) & ~7; |
1737 | 0 | CHECK( !w || !h, "Incompatible sizes!" ); |
1738 | |
|
1739 | 0 | const int origStride = orig.Y().stride; |
1740 | 0 | const Pel* org = orig.Y().buf + x + y * origStride; |
1741 | 0 | const int windowW = w + 2 * range; |
1742 | 0 | const int windowH = h + 2 * range; |
1743 | 0 | const bool oneWindow = windowW <= REF_MAX_BLOCK && windowH <= REF_MAX_BLOCK; // the size a clamped copy can hold |
1744 | 0 | const Pel* window = nullptr; // fetched at the first vector to test |
1745 | 0 | ptrdiff_t windowStride = 0; |
1746 | |
|
1747 | 0 | IntegerVectorQueue queue( *this, org, origStride, w, h ); |
1748 | 0 | const bool queueVectors = oneWindow && queue.enabled(); |
1749 | |
|
1750 | 0 | for( int y2 = -range; y2 <= range; y2 += step ) |
1751 | 0 | { |
1752 | 0 | for( int x2 = -range; x2 <= range; x2 += step ) |
1753 | 0 | { |
1754 | 0 | const int vx = ( centerX + x2 ) * m_motionVectorFactor; |
1755 | 0 | const int vy = ( centerY + y2 ) * m_motionVectorFactor; |
1756 | 0 | if( isTestedVector( tested, vx, vy ) ) |
1757 | 0 | { |
1758 | 0 | continue; |
1759 | 0 | } |
1760 | 0 | int error; |
1761 | 0 | if( oneWindow ) |
1762 | 0 | { |
1763 | 0 | if( !window ) |
1764 | 0 | { |
1765 | 0 | window = getRefBlock( buffer.Y(), refMargin, x + centerX - range, y + centerY - range, windowW, windowH, windowStride ); |
1766 | 0 | } |
1767 | 0 | if( queueVectors ) |
1768 | 0 | { |
1769 | 0 | queue.push( vx, vy, window + ( y2 + range ) * windowStride + x2 + range, windowStride, best ); |
1770 | 0 | continue; |
1771 | 0 | } |
1772 | 0 | error = m_motionErrorLumaInt8( org, origStride, window + ( y2 + range ) * windowStride + x2 + range, windowStride, w, h, best.error ); |
1773 | 0 | } |
1774 | 0 | else |
1775 | 0 | { |
1776 | 0 | error = motionErrorLuma( orig, buffer, refMargin, x, y, vx, vy, bs, best.error ); |
1777 | 0 | } |
1778 | 0 | if( error < best.error ) |
1779 | 0 | { |
1780 | 0 | best.set( vx, vy, error ); |
1781 | 0 | } |
1782 | 0 | } |
1783 | 0 | } |
1784 | 0 | queue.flush( best ); |
1785 | 0 | } |
1786 | | |
1787 | | bool MCTF::estimateLumaLn( std::atomic_int& blockX_, std::atomic_int* prevLineX, Array2D<MotionVector> &mvs, const PelStorage &orig, const PelStorage &buffer, const int refMargin, const int blockSize, |
1788 | | const Array2D<MotionVector> *previous, const int factor, const bool doubleRes, int blockY, int bitDepth ) const |
1789 | 0 | { |
1790 | 0 | PROFILER_SCOPE_AND_STAGE( 1, _TPROF, P_MCTF_SEARCH ); |
1791 | |
|
1792 | 0 | const int stepSize = blockSize; |
1793 | 0 | const int origWidth = orig.Y().width; |
1794 | |
|
1795 | 0 | for( int blockX = blockX_.load(); blockX + 8 <= origWidth; blockX += stepSize, blockX_.store( blockX) ) |
1796 | 0 | { |
1797 | 0 | if( prevLineX && blockX >= prevLineX->load() ) return false; |
1798 | | |
1799 | 0 | int range = doubleRes ? 0 : ( m_searchPttrn == 2 ? 3 : 5 ); |
1800 | 0 | const int stepSize = blockSize; |
1801 | |
|
1802 | 0 | MotionVector best; |
1803 | |
|
1804 | 0 | if (previous == NULL) |
1805 | 0 | { |
1806 | 0 | range = 8; |
1807 | 0 | } |
1808 | 0 | else |
1809 | 0 | { |
1810 | | // The start vectors: the previous level's vectors around the block, then the zero vector. The full-sample ones |
1811 | | // whose reference block lies inside the border are tested four at a time in the same order (the queue's rule); any |
1812 | | // other vector first tests the queued ones. A vector that is the best one or queued is a repeat: testing it again |
1813 | | // cannot change the search. |
1814 | 0 | const int w = std::min<int>( blockSize, origWidth - blockX ) & ~7; |
1815 | 0 | const int h = std::min<int>( blockSize, orig.Y().height - blockY ) & ~7; |
1816 | 0 | const CPelBuf ref = buffer.Y(); |
1817 | 0 | IntegerVectorQueue queue( *this, orig.Y().bufAt( blockX, blockY ), orig.Y().stride, w, h ); |
1818 | 0 | auto testStartVector = [&]( const int vx, const int vy ) |
1819 | 0 | { |
1820 | 0 | if( isTestedVector( best, vx, vy ) || queue.holds( vx, vy ) ) |
1821 | 0 | { |
1822 | 0 | return; |
1823 | 0 | } |
1824 | 0 | const int rx = blockX + ( vx >> 4 ); |
1825 | 0 | const int ry = blockY + ( vy >> 4 ); |
1826 | 0 | if( queue.enabled() && ( ( vx | vy ) & 15 ) == 0 && refBlockInBorder( ref, refMargin, rx, ry, w, h ) ) |
1827 | 0 | { |
1828 | 0 | queue.push( vx, vy, ref.bufAt( rx, ry ), ref.stride, best ); |
1829 | 0 | return; |
1830 | 0 | } |
1831 | 0 | queue.flush( best ); |
1832 | 0 | const int error = motionErrorLuma( orig, buffer, refMargin, blockX, blockY, vx, vy, blockSize, best.error ); |
1833 | 0 | if( error < best.error ) |
1834 | 0 | { |
1835 | 0 | best.set( vx, vy, error ); |
1836 | 0 | } |
1837 | 0 | }; |
1838 | |
|
1839 | 0 | for( int py = -1; py <= 1; py++ ) |
1840 | 0 | { |
1841 | 0 | int testy = blockY / (2 * blockSize) + py; |
1842 | 0 | if( (testy >= 0) && (testy < previous->h()) ) |
1843 | 0 | { |
1844 | 0 | for (int px = -1; px <= 1; px++) |
1845 | 0 | { |
1846 | 0 | int testx = blockX / (2 * blockSize) + px; |
1847 | 0 | if ((testx >= 0) && (testx < previous->w()) ) |
1848 | 0 | { |
1849 | 0 | const MotionVector& old = previous->get(testx, testy); |
1850 | 0 | testStartVector( old.x * factor, old.y * factor ); |
1851 | 0 | } |
1852 | 0 | } |
1853 | 0 | } |
1854 | 0 | } |
1855 | 0 | testStartVector( 0, 0 ); |
1856 | 0 | queue.flush( best ); |
1857 | 0 | } |
1858 | 0 | MotionVector prevBest = best; |
1859 | 0 | const int d = previous == NULL && m_searchPttrn == 2 ? 2 : 1; |
1860 | | // the integer search around the best start vector (tested already, except without a previous level) |
1861 | 0 | const int centerX = prevBest.x / m_motionVectorFactor; |
1862 | 0 | const int centerY = prevBest.y / m_motionVectorFactor; |
1863 | 0 | testIntegerLattice( orig, buffer, refMargin, blockX, blockY, blockSize, centerX, centerY, range, d, prevBest, best ); |
1864 | 0 | if (doubleRes) |
1865 | 0 | { // merge into one loop, probably with precision array (here [12, 3] or maybe [4, 1]) with setable number of iterations |
1866 | 0 | PROFILER_SCOPE_AND_STAGE( 1, _TPROF, P_MCTF_SEARCH_SUBPEL ); |
1867 | 0 | FilteredColumns columns; // the block's horizontally filtered columns, shared by the three iterations |
1868 | | |
1869 | | // first iteration, 49 - 1 or 16 checks or 9 - 1 checks |
1870 | 0 | prevBest = best; |
1871 | 0 | testSubpelLattice( orig, buffer, refMargin, blockX, blockY, blockSize, prevBest, m_searchPttrn ? 6 : 12, m_searchPttrn == 2 ? 6 : 4, columns, best ); |
1872 | | |
1873 | | // second iteration, 9 - 1 checks |
1874 | 0 | prevBest = best; |
1875 | 0 | testSubpelLattice( orig, buffer, refMargin, blockX, blockY, blockSize, prevBest, 2, 2, columns, best ); |
1876 | | |
1877 | | // third iteration, 9 - 1 checks |
1878 | 0 | prevBest = best; |
1879 | 0 | testSubpelLattice( orig, buffer, refMargin, blockX, blockY, blockSize, prevBest, 1, 1, columns, best ); |
1880 | 0 | } |
1881 | 0 | if( blockY > 0 ) |
1882 | 0 | { |
1883 | 0 | MotionVector aboveMV = mvs.get( blockX / stepSize, ( blockY - stepSize ) / stepSize ); |
1884 | 0 | if( !isTestedVector( best, aboveMV.x, aboveMV.y ) && !testedByIntegerSearch( aboveMV, centerX, centerY, range, d, m_motionVectorFactor ) ) |
1885 | 0 | { |
1886 | 0 | int error = motionErrorLuma( orig, buffer, refMargin, blockX, blockY, aboveMV.x, aboveMV.y, blockSize, best.error ); |
1887 | 0 | if( error < best.error ) |
1888 | 0 | { |
1889 | 0 | best.set( aboveMV.x, aboveMV.y, error ); |
1890 | 0 | } |
1891 | 0 | } |
1892 | 0 | } |
1893 | 0 | if( blockX > 0 ) |
1894 | 0 | { |
1895 | 0 | MotionVector leftMV = mvs.get( ( blockX - stepSize ) / stepSize, blockY / stepSize ); |
1896 | 0 | if( !isTestedVector( best, leftMV.x, leftMV.y ) && !testedByIntegerSearch( leftMV, centerX, centerY, range, d, m_motionVectorFactor ) ) |
1897 | 0 | { |
1898 | 0 | int error = motionErrorLuma( orig, buffer, refMargin, blockX, blockY, leftMV.x, leftMV.y, blockSize, best.error ); |
1899 | 0 | if( error < best.error ) |
1900 | 0 | { |
1901 | 0 | best.set( leftMV.x, leftMV.y, error ); |
1902 | 0 | } |
1903 | 0 | } |
1904 | 0 | } |
1905 | |
|
1906 | 0 | if( doubleRes ) |
1907 | 0 | { |
1908 | 0 | const int w = std::min<int>( blockSize, orig.Y().width - blockX ) & ~7; |
1909 | 0 | const int h = std::min<int>( blockSize, orig.Y().height - blockY ) & ~7; |
1910 | |
|
1911 | 0 | CHECKD(bitDepth>10, "unsupported internal bit depth (also in calcVar)" ); |
1912 | 0 | const double bdScale = double(1<<(2*(10-bitDepth))); |
1913 | 0 | const double dvar = m_calcVar( orig.Y().bufAt( blockX, blockY ), orig.Y().stride, w, h ) * bdScale; |
1914 | 0 | const double mse = best.error * bdScale / double( w * h ); |
1915 | |
|
1916 | 0 | best.error = ( int ) ( 20 * ( ( best.error*bdScale + 5.0 ) / ( dvar + 5.0 ) ) + mse / 50.0 ); |
1917 | 0 | best.rmsme = uint16_t( 0.5 + sqrt( mse ) ); |
1918 | 0 | best.overlap = ( ( double ) w * h ) / ( m_mctfUnitSize * m_mctfUnitSize ); |
1919 | 0 | } |
1920 | | |
1921 | 0 | mvs.get(blockX / stepSize, blockY / stepSize) = best; |
1922 | 0 | } |
1923 | | |
1924 | 0 | return true; |
1925 | 0 | } |
1926 | | |
1927 | | void MCTF::motionEstimationLuma(Array2D<MotionVector> &mvs, const PelStorage &orig, const PelStorage &buffer, const int refMargin, const int blockSize, const Array2D<MotionVector> *previous, const int factor, const bool doubleRes) const |
1928 | 0 | { |
1929 | 0 | motionEstimationLumaJobs( { MotionSearchJob{ &mvs, &orig, &buffer, refMargin, blockSize, previous, factor, doubleRes } } ); |
1930 | 0 | } |
1931 | | |
1932 | | void MCTF::motionEstimationLumaJobs( const std::vector<MotionSearchJob>& jobs ) const |
1933 | 0 | { |
1934 | 0 | const int bitDepth = m_encCfg->m_internalBitDepth[CH_L]; |
1935 | |
|
1936 | 0 | if( m_threadPool ) |
1937 | 0 | { |
1938 | 0 | struct EstParams |
1939 | 0 | { |
1940 | 0 | std::atomic_int blockX; |
1941 | 0 | std::atomic_int* prevLineX; |
1942 | 0 | Array2D<MotionVector> *mvs; |
1943 | 0 | const PelStorage* orig; |
1944 | 0 | const PelStorage* buffer; |
1945 | 0 | int refMargin; |
1946 | 0 | const Array2D<MotionVector> *previous; |
1947 | 0 | int blockSize; |
1948 | 0 | int factor; |
1949 | 0 | bool doubleRes; |
1950 | 0 | int blockY; |
1951 | 0 | int bitDepth; |
1952 | 0 | const MCTF* mctf; |
1953 | 0 | }; |
1954 | |
|
1955 | 0 | size_t numRows = 0; |
1956 | 0 | for( const MotionSearchJob& job : jobs ) |
1957 | 0 | { |
1958 | 0 | numRows += job.orig->Y().height / job.blockSize + 1; |
1959 | 0 | } |
1960 | 0 | std::vector<EstParams> EstParamsArray( numRows ); |
1961 | |
|
1962 | 0 | static auto task = []( int, void* task_param ) |
1963 | 0 | { |
1964 | 0 | ITT_TASKSTART( itt_domain_MCTF_est, itt_handle_est ); |
1965 | |
|
1966 | 0 | EstParams* params = static_cast<EstParams*>( task_param ); |
1967 | 0 | bool ret = params->mctf->estimateLumaLn( params->blockX, params->prevLineX, *params->mvs, *params->orig, *params->buffer, params->refMargin, params->blockSize, params->previous, params->factor, params->doubleRes, params->blockY, params->bitDepth ); |
1968 | |
|
1969 | 0 | ITT_TASKEND( itt_domain_MCTF_est, itt_handle_est ); |
1970 | 0 | return ret; |
1971 | 0 | }; |
1972 | | // Ready only when the line can make progress: it is the first line, it has no block left (the task then finishes |
1973 | | // at once; a level narrower than 8 samples has none, and the line above never moves), or the line above is ahead of |
1974 | | // it (the test estimateLumaLn makes first). A blocked line then costs a few compares in the pool's scan, instead of |
1975 | | // a task run that returns at once and the wake-up of the idle thread after it. |
1976 | 0 | static auto ready = []( int, void* task_param ) |
1977 | 0 | { |
1978 | 0 | const EstParams* params = static_cast<const EstParams*>( task_param ); |
1979 | 0 | const int blockX = params->blockX.load(); |
1980 | 0 | return !params->prevLineX || blockX + 8 > int( params->orig->Y().width ) || blockX < params->prevLineX->load(); |
1981 | 0 | }; |
1982 | |
|
1983 | 0 | WaitCounter taskCounter; |
1984 | |
|
1985 | 0 | int n = 0; |
1986 | 0 | for( const MotionSearchJob& job : jobs ) |
1987 | 0 | { |
1988 | 0 | const int origHeight = job.orig->Y().height; |
1989 | 0 | const int firstRow = n; // each job's rows wait only on the row above in the same job |
1990 | 0 | for( int blockY = 0; blockY + 8 <= origHeight; blockY += job.blockSize, n++ ) |
1991 | 0 | { |
1992 | 0 | EstParams& cEstParams = EstParamsArray[n]; |
1993 | 0 | cEstParams.blockX = 0; |
1994 | 0 | cEstParams.prevLineX = n == firstRow ? nullptr : &EstParamsArray[n-1].blockX; |
1995 | 0 | cEstParams.mvs = job.mvs; |
1996 | 0 | cEstParams.orig = job.orig; |
1997 | 0 | cEstParams.buffer = job.buffer; |
1998 | 0 | cEstParams.refMargin = job.refMargin; |
1999 | 0 | cEstParams.previous = job.previous; |
2000 | 0 | cEstParams.blockSize = job.blockSize; |
2001 | 0 | cEstParams.factor = job.factor; |
2002 | 0 | cEstParams.doubleRes = job.doubleRes; |
2003 | 0 | cEstParams.mctf = this; |
2004 | 0 | cEstParams.blockY = blockY; |
2005 | 0 | cEstParams.bitDepth = bitDepth; |
2006 | |
|
2007 | 0 | m_threadPool->addBarrierTask( task, &cEstParams, &taskCounter, nullptr, {}, ready ); |
2008 | 0 | } |
2009 | 0 | } |
2010 | 0 | pollStartStage(); |
2011 | 0 | taskCounter.wait(); |
2012 | 0 | } |
2013 | 0 | else |
2014 | 0 | { |
2015 | 0 | for( const MotionSearchJob& job : jobs ) |
2016 | 0 | { |
2017 | 0 | const PelStorage& orig = *job.orig; |
2018 | 0 | for( int blockY = 0; blockY + 8 <= orig.Y().height; blockY += job.blockSize ) |
2019 | 0 | { |
2020 | 0 | std::atomic_int blockX( 0 ), prevBlockX( orig.Y().width + job.blockSize ); |
2021 | 0 | estimateLumaLn( blockX, blockY ? &prevBlockX : nullptr, *job.mvs, orig, *job.buffer, job.refMargin, job.blockSize, job.previous, job.factor, job.doubleRes, blockY, bitDepth ); |
2022 | 0 | } |
2023 | 0 | } |
2024 | 0 | } |
2025 | 0 | } |
2026 | | |
2027 | | void MCTF::xFinalizeBlkLine( const PelStorage &orgPic, std::deque<TemporalFilterSourcePicInfo> &srcFrameInfo, PelStorage &newOrgPic, int yStart, const double sigmaSqCh[MAX_NUM_CH], double overallStrength ) const |
2028 | 0 | { |
2029 | 0 | PROFILER_SCOPE_AND_STAGE( 1, _TPROF, P_MCTF_APPLY ); |
2030 | |
|
2031 | 0 | const int numRefs = int(srcFrameInfo.size()); |
2032 | |
|
2033 | 0 | int refStrengthRow = m_encCfg->m_picReordering ? 0 : 1; |
2034 | | |
2035 | | // max 64*64*8*2 = 2^(6+6+3+1)=2^16=64kbps, usually 16*16*8*2=2^(4+4+3+1)=4kbps, and allow for overread of one line |
2036 | 0 | Pel* dstBufs = ( Pel* ) alloca( sizeof( Pel ) * ( numRefs * m_mctfUnitSize * m_mctfUnitSize + m_mctfUnitSize ) ); |
2037 | |
|
2038 | 0 | for( int c = 0; c < getNumberValidComponents( m_encCfg->m_internChromaFormat ); c++ ) |
2039 | 0 | { |
2040 | 0 | const ComponentID compID = ( ComponentID ) c; |
2041 | 0 | const int height = orgPic.bufs[c].height; |
2042 | 0 | const int width = orgPic.bufs[c].width; |
2043 | |
|
2044 | 0 | const double sigmaSq = sigmaSqCh[ toChannelType( compID) ]; |
2045 | 0 | const double weightScaling = overallStrength * ( isChroma( compID ) ? m_chromaFactor : 0.4 ); |
2046 | 0 | const ClpRng clpRng{ m_encCfg->m_internalBitDepth[toChannelType( compID )] }; |
2047 | |
|
2048 | 0 | const int blkSizeY = m_mctfUnitSize >> getComponentScaleY( compID, m_encCfg->m_internChromaFormat ); |
2049 | 0 | const int blkSizeX = m_mctfUnitSize >> getComponentScaleX( compID, m_encCfg->m_internChromaFormat ); |
2050 | 0 | const int yOut = yStart >> getComponentScaleY( compID, m_encCfg->m_internChromaFormat ); |
2051 | | // The chroma border is scaled like the chroma planes; take the narrower direction. |
2052 | 0 | const int refMargin = MCTF_FRAME_MARGIN >> std::max( getComponentScaleX( compID, m_encCfg->m_internChromaFormat ), getComponentScaleY( compID, m_encCfg->m_internChromaFormat ) ); |
2053 | |
|
2054 | 0 | for( int by = yOut, yBlkAddr = yStart / m_mctfUnitSize; by < std::min( yOut + blkSizeY, height ); by += blkSizeY, yBlkAddr++ ) |
2055 | 0 | { |
2056 | 0 | const int h = std::min( blkSizeY, height - by ); |
2057 | |
|
2058 | 0 | for( int bx = 0, xBlkAddr = 0; bx < width; bx += blkSizeX, xBlkAddr++ ) |
2059 | 0 | { |
2060 | 0 | const int w = std::min( blkSizeX, width - bx ); |
2061 | |
|
2062 | 0 | const int csx = getComponentScaleX( compID, m_encCfg->m_internChromaFormat ); |
2063 | 0 | const int csy = getComponentScaleY( compID, m_encCfg->m_internChromaFormat ); |
2064 | |
|
2065 | 0 | const Pel* correctedPics[2 * VVENC_MCTF_RANGE] = { nullptr, }; |
2066 | 0 | Pel* currDst = dstBufs; |
2067 | 0 | int verror [2 * VVENC_MCTF_RANGE] = { 0, }; |
2068 | 0 | double refStr [2 * VVENC_MCTF_RANGE] = { 0.0, }; |
2069 | |
|
2070 | 0 | for( int i = 0; i < numRefs; i++, currDst += w * h ) |
2071 | 0 | { |
2072 | 0 | Pel* dst = currDst; |
2073 | 0 | const int dstStride = w; |
2074 | 0 | correctedPics[i] = dst; |
2075 | |
|
2076 | 0 | const MotionVector& mv = srcFrameInfo[i].mvs.get( xBlkAddr, yBlkAddr); |
2077 | 0 | const int dx = mv.x >> csx; |
2078 | 0 | const int dy = mv.y >> csy; |
2079 | 0 | const int xInt = mv.x >> ( 4 + csx ); |
2080 | 0 | const int yInt = mv.y >> ( 4 + csy ); |
2081 | |
|
2082 | 0 | ptrdiff_t srcStride = 0; |
2083 | 0 | const Pel* src = getRefBlock( srcFrameInfo[i].picBuffer.bufs[compID], refMargin, bx + xInt, by + yInt, w, h, srcStride ); |
2084 | |
|
2085 | 0 | if( m_lowResFltApply ) // || isChroma( compID ) |
2086 | 0 | { |
2087 | 0 | const int16_t* xFilter = m_interpolationFilter4[dx & 0xf]; |
2088 | 0 | const int16_t* yFilter = m_interpolationFilter4[dy & 0xf]; // will add 6 bit. |
2089 | |
|
2090 | 0 | m_applyFrac[toChannelType( compID )][1]( src, srcStride, dst, dstStride, w, h, xFilter, yFilter, m_encCfg->m_internalBitDepth[toChannelType( compID )] ); |
2091 | 0 | } |
2092 | 0 | else |
2093 | 0 | { |
2094 | 0 | const int16_t* xFilter = m_interpolationFilter8[dx & 0xf]; |
2095 | 0 | const int16_t* yFilter = m_interpolationFilter8[dy & 0xf]; // will add 6 bit. |
2096 | |
|
2097 | 0 | m_applyFrac[toChannelType( compID )][0]( src, srcStride, dst, dstStride, w, h, xFilter, yFilter, m_encCfg->m_internalBitDepth[toChannelType( compID )] ); |
2098 | 0 | } |
2099 | |
|
2100 | 0 | if( mv.rmsme > 0 && m_encCfg->m_QP <= 32 && w == h && w <= 32 ) // "deblocking" |
2101 | 0 | { |
2102 | 0 | m_applyPlanarCorrection( orgPic.bufs[c].bufAt( bx, by ), orgPic.bufs[c].stride, dst, dstStride, w, h, clpRng, mv.rmsme ); |
2103 | 0 | } |
2104 | |
|
2105 | 0 | verror[i] = mv.error; |
2106 | 0 | refStr[i] = m_refStrengths[refStrengthRow][srcFrameInfo[i].index]; |
2107 | 0 | } |
2108 | |
|
2109 | 0 | m_applyBlock( orgPic.bufs[c], newOrgPic.bufs[c], CompArea( compID, orgPic.chromaFormat, Area( bx, by, w, h ) ), clpRng, correctedPics, numRefs, verror, refStr, weightScaling, sigmaSq ); |
2110 | 0 | } |
2111 | 0 | } |
2112 | 0 | } |
2113 | 0 | } |
2114 | | |
2115 | | void MCTF::bilateralFilter(const PelStorage& orgPic, std::deque<TemporalFilterSourcePicInfo>& srcFrameInfo, PelStorage& newOrgPic, double overallStrength) const |
2116 | 0 | { |
2117 | 0 | const double qp = m_encCfg->m_QP + m_encCfg->m_QPFraction; |
2118 | 0 | const double lumaSigmaSq = m_sigmaMultiplier * ( 128.0 + 3.0 / 256.0 * qp * qp * qp ); |
2119 | 0 | const double chromaSigmaSq = 30 * 30; |
2120 | |
|
2121 | 0 | double sigmaSqCh[MAX_NUM_CH]; |
2122 | 0 | for(int c=0; c< getNumberValidChannels(m_encCfg->m_internChromaFormat); c++) |
2123 | 0 | { |
2124 | 0 | const ChannelType ch=(ChannelType)c; |
2125 | 0 | const Pel maxSampleValue = (1<<m_encCfg->m_internalBitDepth[ch])-1; |
2126 | 0 | const double bitDepthDiffWeighting=1024.0 / (maxSampleValue+1); |
2127 | 0 | sigmaSqCh[ch] = ( isChroma( ch ) ? chromaSigmaSq : lumaSigmaSq ) / ( bitDepthDiffWeighting * bitDepthDiffWeighting ); |
2128 | 0 | } |
2129 | |
|
2130 | 0 | if( m_threadPool ) |
2131 | 0 | { |
2132 | 0 | struct FltParams |
2133 | 0 | { |
2134 | 0 | const PelStorage *orgPic; |
2135 | 0 | std::deque<TemporalFilterSourcePicInfo> *srcFrameInfo; |
2136 | 0 | PelStorage *newOrgPic; |
2137 | 0 | const double *sigmaSqCh; |
2138 | 0 | double overallStrength; |
2139 | 0 | const MCTF* mctf; |
2140 | 0 | int yStart; |
2141 | 0 | }; |
2142 | |
|
2143 | 0 | std::vector<FltParams> FltParamsArray( orgPic.Y().height / m_mctfUnitSize + 1 ); |
2144 | |
|
2145 | 0 | WaitCounter taskCounter; |
2146 | 0 | for( int n = 0, yStart = 0; yStart < orgPic.Y().height; yStart += m_mctfUnitSize, n++ ) |
2147 | 0 | { |
2148 | 0 | static auto task = []( int, void* task_param ) |
2149 | 0 | { |
2150 | 0 | ITT_TASKSTART( itt_domain_MCTF_flt, itt_handle_flt ); |
2151 | |
|
2152 | 0 | FltParams* params = static_cast<FltParams*>( task_param ); |
2153 | 0 | params->mctf->xFinalizeBlkLine( *params->orgPic, *params->srcFrameInfo, *params->newOrgPic, params->yStart, params->sigmaSqCh, params->overallStrength ); |
2154 | |
|
2155 | 0 | ITT_TASKEND( itt_domain_MCTF_flt, itt_handle_flt ); |
2156 | 0 | return true; |
2157 | 0 | }; |
2158 | |
|
2159 | 0 | FltParams& cFltParams = FltParamsArray[n]; |
2160 | 0 | cFltParams.orgPic = &orgPic; |
2161 | 0 | cFltParams.srcFrameInfo = &srcFrameInfo; |
2162 | 0 | cFltParams.newOrgPic = &newOrgPic; |
2163 | 0 | cFltParams.sigmaSqCh = sigmaSqCh; |
2164 | 0 | cFltParams.overallStrength = overallStrength; |
2165 | 0 | cFltParams.mctf = this; |
2166 | 0 | cFltParams.yStart = yStart; |
2167 | |
|
2168 | 0 | m_threadPool->addBarrierTask( task, &cFltParams, &taskCounter ); |
2169 | 0 | } |
2170 | 0 | pollStartStage(); |
2171 | 0 | taskCounter.wait(); |
2172 | 0 | } |
2173 | 0 | else |
2174 | 0 | { |
2175 | 0 | for (int yStart = 0; yStart < orgPic.Y().height; yStart += m_mctfUnitSize ) |
2176 | 0 | { |
2177 | 0 | xFinalizeBlkLine( orgPic, srcFrameInfo, newOrgPic, yStart, sigmaSqCh, overallStrength ); |
2178 | 0 | } |
2179 | 0 | } |
2180 | 0 | } |
2181 | | |
2182 | | } // namespace vvenc |
2183 | | |
2184 | | //! \} |