/src/vvenc/source/Lib/CommonLib/Reshape.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 Reshape.cpp |
45 | | \brief common reshaper class |
46 | | */ |
47 | | #include "Reshape.h" |
48 | | #include "UnitTools.h" |
49 | | #include <stdio.h> |
50 | | #include <string.h> |
51 | | #include <math.h> |
52 | | //! \ingroup CommonLib |
53 | | //! \{ |
54 | | |
55 | | namespace vvenc { |
56 | | |
57 | | // ===================================================================================================================== |
58 | | // ReshapeData |
59 | | // ===================================================================================================================== |
60 | | |
61 | | int ReshapeData::getPWLIdxInv( int lumaVal ) const |
62 | 0 | { |
63 | 0 | for (int idxS = m_sliceReshapeInfo.reshaperModelMinBinIdx; (idxS <= m_sliceReshapeInfo.reshaperModelMaxBinIdx); idxS++) |
64 | 0 | { |
65 | 0 | if (lumaVal < m_reshapePivot[idxS + 1]) |
66 | 0 | return idxS; |
67 | 0 | } |
68 | 0 | return PIC_CODE_CW_BINS-1; |
69 | 0 | } |
70 | | |
71 | | int ReshapeData::calculateChromaAdj( Pel avgLuma ) const |
72 | 0 | { |
73 | 0 | int iAdj = m_chromaAdjHelpLUT[getPWLIdxInv(avgLuma)]; |
74 | 0 | return(iAdj); |
75 | 0 | } |
76 | | |
77 | | int ReshapeData::calculateChromaAdjVpduNei( const TransformUnit& tu, const CompArea& areaY, const TreeType _treeType ) |
78 | 0 | { |
79 | 0 | CodingStructure &cs = *tu.cs; |
80 | 0 | int xPos = areaY.lumaPos().x; |
81 | 0 | int yPos = areaY.lumaPos().y; |
82 | 0 | int numNeighborLog = std::min<unsigned>(6,cs.pcv->maxCUSizeLog2); |
83 | 0 | int numNeighbor = 1<<numNeighborLog; |
84 | 0 | const int mask = 1-numNeighbor-1; |
85 | 0 | xPos &= mask; |
86 | 0 | yPos &= mask; |
87 | |
|
88 | 0 | if( !true/*isEncoder*/ ) |
89 | 0 | { |
90 | 0 | if( isVPDUprocessed( xPos, yPos ) ) |
91 | 0 | { |
92 | 0 | return m_chromaScale; |
93 | 0 | } |
94 | | |
95 | 0 | setVPDULoc( xPos, yPos ); |
96 | 0 | } |
97 | 0 | Position topLeft(xPos, yPos); |
98 | 0 | CodingStructure* pcs = (CS::isDualITree(cs) && cs.slice->sliceType == VVENC_I_SLICE) ? tu.cs->picture->cs : tu.cs; |
99 | 0 | CodingUnit *topLeftLuma = pcs->getCU(topLeft, CH_L, _treeType); |
100 | 0 | const CodingUnit *cuAbove = pcs->getCURestricted( topLeftLuma->lumaPos().offset(0, -1), topLeftLuma->lumaPos(), topLeftLuma->slice->independentSliceIdx, topLeftLuma->tileIdx, CH_L, _treeType ); |
101 | 0 | const CodingUnit *cuLeft = pcs->getCURestricted( topLeftLuma->lumaPos().offset(-1, 0), topLeftLuma->lumaPos(), topLeftLuma->slice->independentSliceIdx, topLeftLuma->tileIdx, CH_L, _treeType ); |
102 | |
|
103 | 0 | xPos = topLeftLuma->lumaPos().x; |
104 | 0 | yPos = topLeftLuma->lumaPos().y; |
105 | |
|
106 | 0 | const CPelBuf piRecoY = cs.picture->getRecoBuf(topLeftLuma->Y()); |
107 | 0 | const int strideY = piRecoY.stride; |
108 | |
|
109 | 0 | const Pel* recSrc0 = piRecoY.bufAt(0, 0); |
110 | 0 | const uint32_t picH = tu.cs->picture->lheight(); |
111 | 0 | const uint32_t picW = tu.cs->picture->lwidth(); |
112 | 0 | int32_t recLuma = 0; |
113 | |
|
114 | 0 | int pelnum = 0; |
115 | 0 | if (cuLeft != nullptr) |
116 | 0 | { |
117 | 0 | pelnum ++; |
118 | 0 | for (int i = 0; i < numNeighbor; i++) |
119 | 0 | { |
120 | 0 | int k = (yPos + i) >= picH ? (picH - yPos - 1) : i; |
121 | 0 | recLuma += recSrc0[-1 + k * strideY]; |
122 | 0 | } |
123 | 0 | } |
124 | 0 | if (cuAbove != nullptr) |
125 | 0 | { |
126 | 0 | pelnum++; |
127 | 0 | for (int i = 0; i < numNeighbor; i++) |
128 | 0 | { |
129 | 0 | int k = (xPos + i) >= picW ? (picW - xPos - 1) : i; |
130 | 0 | recLuma += recSrc0[-strideY + k]; |
131 | 0 | } |
132 | 0 | } |
133 | |
|
134 | 0 | int lumaValue; |
135 | 0 | if( pelnum ) |
136 | 0 | { |
137 | 0 | const int shift = numNeighborLog + pelnum - 1; |
138 | 0 | lumaValue = (recLuma + (1 << (shift - 1))) >> shift; |
139 | 0 | } |
140 | 0 | else |
141 | 0 | { |
142 | 0 | lumaValue = 1 << (cs.sps->bitDepths[CH_L] - 1); |
143 | 0 | } |
144 | |
|
145 | 0 | int chromaScale = calculateChromaAdj(lumaValue); |
146 | 0 | if( !true/*isEncoder*/) |
147 | 0 | { |
148 | 0 | m_chromaScale = chromaScale; |
149 | 0 | } |
150 | 0 | return chromaScale; |
151 | 0 | } |
152 | | |
153 | | // ===================================================================================================================== |
154 | | // Reshape |
155 | | // ===================================================================================================================== |
156 | | |
157 | | void Reshape::createDec(int bitDepth) |
158 | 0 | { |
159 | 0 | m_lumaBD = bitDepth; |
160 | 0 | m_reshapeLUTSize = 1 << m_lumaBD; |
161 | 0 | m_initCW = m_reshapeLUTSize / PIC_CODE_CW_BINS; |
162 | 0 | #if defined( TARGET_SIMD_X86 ) && ENABLE_SIMD_OPT_BUFFER |
163 | 0 | if( m_fwdLUT.empty() ) |
164 | 0 | m_fwdLUT.resize( m_reshapeLUTSize + 2, 0 ); |
165 | 0 | if( m_invLUT.empty() ) |
166 | 0 | m_invLUT.resize( m_reshapeLUTSize + 2, 0 ); |
167 | | #else |
168 | | if( m_fwdLUT.empty() ) |
169 | | m_fwdLUT.resize( m_reshapeLUTSize, 0 ); |
170 | | if( m_invLUT.empty() ) |
171 | | m_invLUT.resize( m_reshapeLUTSize, 0 ); |
172 | | #endif |
173 | 0 | if (m_binCW.empty()) |
174 | 0 | m_binCW.resize(PIC_CODE_CW_BINS, 0); |
175 | 0 | if (m_inputPivot.empty()) |
176 | 0 | m_inputPivot.resize(PIC_CODE_CW_BINS + 1, 0); |
177 | 0 | if (m_fwdScaleCoef.empty()) |
178 | 0 | m_fwdScaleCoef.resize(PIC_CODE_CW_BINS, 1 << FP_PREC); |
179 | 0 | if (m_invScaleCoef.empty()) |
180 | 0 | m_invScaleCoef.resize(PIC_CODE_CW_BINS, 1 << FP_PREC); |
181 | 0 | if (m_reshapePivot.empty()) |
182 | 0 | m_reshapePivot.resize(PIC_CODE_CW_BINS + 1, 0); |
183 | 0 | if (m_chromaAdjHelpLUT.empty()) |
184 | 0 | m_chromaAdjHelpLUT.resize(PIC_CODE_CW_BINS, 1<<CSCALE_FP_PREC); |
185 | 0 | } |
186 | | |
187 | | /** Construct reshaper from syntax |
188 | | * \param void |
189 | | * \return void |
190 | | */ |
191 | | void Reshape::constructReshaper() |
192 | 0 | { |
193 | 0 | int pwlFwdLUTsize = PIC_CODE_CW_BINS; |
194 | 0 | int pwlFwdBinLen = m_reshapeLUTSize / PIC_CODE_CW_BINS; |
195 | |
|
196 | 0 | for (int i = 0; i < m_sliceReshapeInfo.reshaperModelMinBinIdx; i++) |
197 | 0 | m_binCW[i] = 0; |
198 | 0 | for (int i = m_sliceReshapeInfo.reshaperModelMaxBinIdx + 1; i < PIC_CODE_CW_BINS; i++) |
199 | 0 | m_binCW[i] = 0; |
200 | 0 | for (int i = m_sliceReshapeInfo.reshaperModelMinBinIdx; i <= m_sliceReshapeInfo.reshaperModelMaxBinIdx; i++) |
201 | 0 | m_binCW[i] = (uint16_t)(m_sliceReshapeInfo.reshaperModelBinCWDelta[i] + (int)m_initCW); |
202 | |
|
203 | 0 | for (int i = 0; i < pwlFwdLUTsize; i++) |
204 | 0 | { |
205 | 0 | m_reshapePivot[i + 1] = m_reshapePivot[i] + m_binCW[i]; |
206 | 0 | m_inputPivot[i + 1] = m_inputPivot[i] + m_initCW; |
207 | 0 | m_fwdScaleCoef[i] = ((int32_t)m_binCW[i] * (1 << FP_PREC) + (1 << (floorLog2(pwlFwdBinLen) - 1))) >> floorLog2(pwlFwdBinLen); |
208 | 0 | if (m_binCW[i] == 0) |
209 | 0 | { |
210 | 0 | m_invScaleCoef[i] = 0; |
211 | 0 | m_chromaAdjHelpLUT[i] = 1 << CSCALE_FP_PREC; |
212 | 0 | } |
213 | 0 | else |
214 | 0 | { |
215 | 0 | m_invScaleCoef[i] = (int32_t)(m_initCW * (1 << FP_PREC) / m_binCW[i]); |
216 | 0 | m_chromaAdjHelpLUT[i] = (int32_t)(m_initCW * (1 << FP_PREC) / ( m_binCW[i] + m_sliceReshapeInfo.chrResScalingOffset ) ); |
217 | 0 | } |
218 | 0 | } |
219 | 0 | for (int lumaSample = 0; lumaSample < m_reshapeLUTSize; lumaSample++) |
220 | 0 | { |
221 | 0 | int idxY = lumaSample / m_initCW; |
222 | 0 | int tempVal = m_reshapePivot[idxY] + ((m_fwdScaleCoef[idxY] * (lumaSample - m_inputPivot[idxY]) + (1 << (FP_PREC - 1))) >> FP_PREC); |
223 | 0 | m_fwdLUT[lumaSample] = Clip3((Pel)0, (Pel)((1 << m_lumaBD) - 1), (Pel)(tempVal)); |
224 | |
|
225 | 0 | int idxYInv = getPWLIdxInv(lumaSample); |
226 | 0 | int invSample = m_inputPivot[idxYInv] + ((m_invScaleCoef[idxYInv] * (lumaSample - m_reshapePivot[idxYInv]) + (1 << (FP_PREC - 1))) >> FP_PREC); |
227 | 0 | m_invLUT[lumaSample] = Clip3((Pel)0, (Pel)((1 << m_lumaBD) - 1), (Pel)(invSample)); |
228 | 0 | } |
229 | 0 | } |
230 | | |
231 | | |
232 | | |
233 | | void Reshape::initLumaLevelToWeightTableReshape() |
234 | 0 | { |
235 | 0 | const int lutSize = 1 << m_lumaBD; |
236 | 0 | if (m_reshapeLumaLevelToWeightPLUT.empty()) |
237 | 0 | m_reshapeLumaLevelToWeightPLUT.resize(lutSize, 1.0); |
238 | |
|
239 | 0 | if (m_lumaLevelToWeightPLUT.empty()) |
240 | 0 | m_lumaLevelToWeightPLUT.resize(lutSize, 1.0); |
241 | |
|
242 | 0 | if (m_signalType == RESHAPE_SIGNAL_PQ) |
243 | 0 | { |
244 | 0 | for (int i = 0; i < lutSize; i++) |
245 | 0 | { |
246 | 0 | double x = m_lumaBD < 10 ? i << (10 - m_lumaBD) : m_lumaBD > 10 ? i >> (m_lumaBD - 10) : i; |
247 | 0 | double y; |
248 | 0 | y = 0.015*x - 1.5 - 6; |
249 | 0 | y = y < -3 ? -3 : (y > 6 ? 6 : y); |
250 | 0 | m_lumaLevelToWeightPLUT[i] = pow(2.0, y / 3.0); |
251 | 0 | m_reshapeLumaLevelToWeightPLUT[i] = (uint32_t)(m_lumaLevelToWeightPLUT[i]* (double)(1 << 16)); |
252 | 0 | } |
253 | 0 | } |
254 | 0 | } |
255 | | |
256 | | void Reshape::updateReshapeLumaLevelToWeightTableChromaMD( const Pel* ILUT) |
257 | 0 | { |
258 | 0 | const int lutSize = 1 << m_lumaBD; |
259 | 0 | for( int i = 0; i < lutSize; i++ ) |
260 | 0 | { |
261 | 0 | m_reshapeLumaLevelToWeightPLUT[i] = (uint32_t)(m_lumaLevelToWeightPLUT[ILUT[i]]* (double)(1 << 16)); |
262 | 0 | } |
263 | 0 | } |
264 | | |
265 | | void Reshape::restoreReshapeLumaLevelToWeightTable() |
266 | 0 | { |
267 | 0 | const int lutSize = 1 << m_lumaBD; |
268 | 0 | for (int i = 0; i < lutSize; i++) |
269 | 0 | { |
270 | 0 | m_reshapeLumaLevelToWeightPLUT[i] = (uint32_t)(m_lumaLevelToWeightPLUT[i]* (double)(1 << 16)); |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | | void Reshape::updateReshapeLumaLevelToWeightTable(LmcsParam &sliceReshape, Pel* wtTable, double cwt) |
275 | 0 | { |
276 | 0 | if (m_signalType == RESHAPE_SIGNAL_SDR || m_signalType == RESHAPE_SIGNAL_HLG) |
277 | 0 | { |
278 | 0 | if (sliceReshape.sliceReshaperModelPresent ) |
279 | 0 | { |
280 | 0 | double wBin = 1.0; |
281 | 0 | double weight = 1.0; |
282 | 0 | int histLens = (1 << m_lumaBD) / PIC_CODE_CW_BINS; |
283 | |
|
284 | 0 | for (int i = 0; i < PIC_CODE_CW_BINS; i++) |
285 | 0 | { |
286 | 0 | if ((i < sliceReshape.reshaperModelMinBinIdx) || (i > sliceReshape.reshaperModelMaxBinIdx)) |
287 | 0 | weight = 1.0; |
288 | 0 | else |
289 | 0 | { |
290 | 0 | if (sliceReshape.reshaperModelBinCWDelta[i] == 1 || (sliceReshape.reshaperModelBinCWDelta[i] == -1 * histLens)) |
291 | 0 | weight = wBin; |
292 | 0 | else |
293 | 0 | { |
294 | 0 | weight = (double)wtTable[i] / (double)histLens; |
295 | 0 | weight = weight*weight; |
296 | 0 | } |
297 | 0 | } |
298 | 0 | for (int j = 0; j < histLens; j++) |
299 | 0 | { |
300 | 0 | int ii = i*histLens + j; |
301 | 0 | m_reshapeLumaLevelToWeightPLUT[ii] = (uint32_t)(weight* (double)(1 << 16)); |
302 | 0 | } |
303 | 0 | } |
304 | 0 | m_chromaWeightRS = cwt; |
305 | 0 | } |
306 | 0 | else |
307 | 0 | { |
308 | 0 | THROW("updateReshapeLumaLevelToWeightTable ERROR!!"); |
309 | 0 | } |
310 | 0 | } |
311 | 0 | else |
312 | 0 | { |
313 | 0 | THROW("updateReshapeLumaLevelToWeightTable not support other signal types!!"); |
314 | 0 | } |
315 | 0 | } |
316 | | |
317 | | |
318 | | } // namespace vvenc |
319 | | |
320 | | // |
321 | | //! \} |