1 | /* The copyright in this software is being made available under the BSD |
---|
2 | * License, included below. This software may be subject to other third party |
---|
3 | * and contributor rights, including patent rights, and no such rights are |
---|
4 | * granted under this license. |
---|
5 | * |
---|
6 | * Copyright (c) 2010-2015, ITU/ISO/IEC |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * |
---|
12 | * * Redistributions of source code must retain the above copyright notice, |
---|
13 | * this list of conditions and the following disclaimer. |
---|
14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
15 | * this list of conditions and the following disclaimer in the documentation |
---|
16 | * and/or other materials provided with the distribution. |
---|
17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
18 | * be used to endorse or promote products derived from this software without |
---|
19 | * specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | */ |
---|
33 | |
---|
34 | /** |
---|
35 | \file TEncSampleAdaptiveOffset.cpp |
---|
36 | \brief estimation part of sample adaptive offset class |
---|
37 | */ |
---|
38 | #include "TEncSampleAdaptiveOffset.h" |
---|
39 | #include <string.h> |
---|
40 | #include <stdlib.h> |
---|
41 | #include <stdio.h> |
---|
42 | #include <math.h> |
---|
43 | |
---|
44 | //! \ingroup TLibEncoder |
---|
45 | //! \{ |
---|
46 | |
---|
47 | |
---|
48 | //! rounding with IBDI |
---|
49 | inline Double xRoundIbdi2(Int bitDepth, Double x) |
---|
50 | { |
---|
51 | return ((x)>0) ? (Int)(((Int)(x)+(1<<(bitDepth-8-1)))/(1<<(bitDepth-8))) : ((Int)(((Int)(x)-(1<<(bitDepth-8-1)))/(1<<(bitDepth-8)))); |
---|
52 | } |
---|
53 | |
---|
54 | inline Double xRoundIbdi(Int bitDepth, Double x) |
---|
55 | { |
---|
56 | return (bitDepth > 8 ? xRoundIbdi2(bitDepth, (x)) : ((x)>=0 ? ((Int)((x)+0.5)) : ((Int)((x)-0.5)))) ; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | TEncSampleAdaptiveOffset::TEncSampleAdaptiveOffset() |
---|
61 | { |
---|
62 | m_pppcRDSbacCoder = NULL; |
---|
63 | m_pcRDGoOnSbacCoder = NULL; |
---|
64 | m_pppcBinCoderCABAC = NULL; |
---|
65 | m_statData = NULL; |
---|
66 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
67 | m_preDBFstatData = NULL; |
---|
68 | #endif |
---|
69 | } |
---|
70 | |
---|
71 | TEncSampleAdaptiveOffset::~TEncSampleAdaptiveOffset() |
---|
72 | { |
---|
73 | destroyEncData(); |
---|
74 | } |
---|
75 | |
---|
76 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
77 | Void TEncSampleAdaptiveOffset::createEncData(Bool isPreDBFSamplesUsed) |
---|
78 | #else |
---|
79 | Void TEncSampleAdaptiveOffset::createEncData() |
---|
80 | #endif |
---|
81 | { |
---|
82 | |
---|
83 | //cabac coder for RDO |
---|
84 | m_pppcRDSbacCoder = new TEncSbac* [NUM_SAO_CABACSTATE_LABELS]; |
---|
85 | #if FAST_BIT_EST |
---|
86 | m_pppcBinCoderCABAC = new TEncBinCABACCounter* [NUM_SAO_CABACSTATE_LABELS]; |
---|
87 | #else |
---|
88 | m_pppcBinCoderCABAC = new TEncBinCABAC* [NUM_SAO_CABACSTATE_LABELS]; |
---|
89 | #endif |
---|
90 | |
---|
91 | for(Int cs=0; cs < NUM_SAO_CABACSTATE_LABELS; cs++) |
---|
92 | { |
---|
93 | m_pppcRDSbacCoder[cs] = new TEncSbac; |
---|
94 | #if FAST_BIT_EST |
---|
95 | m_pppcBinCoderCABAC[cs] = new TEncBinCABACCounter; |
---|
96 | #else |
---|
97 | m_pppcBinCoderCABAC[cs] = new TEncBinCABAC; |
---|
98 | #endif |
---|
99 | m_pppcRDSbacCoder [cs]->init( m_pppcBinCoderCABAC [cs] ); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | //statistics |
---|
104 | m_statData = new SAOStatData**[m_numCTUsPic]; |
---|
105 | for(Int i=0; i< m_numCTUsPic; i++) |
---|
106 | { |
---|
107 | m_statData[i] = new SAOStatData*[MAX_NUM_COMPONENT]; |
---|
108 | for(Int compIdx=0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
109 | { |
---|
110 | m_statData[i][compIdx] = new SAOStatData[NUM_SAO_NEW_TYPES]; |
---|
111 | } |
---|
112 | } |
---|
113 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
114 | if(isPreDBFSamplesUsed) |
---|
115 | { |
---|
116 | m_preDBFstatData = new SAOStatData**[m_numCTUsPic]; |
---|
117 | for(Int i=0; i< m_numCTUsPic; i++) |
---|
118 | { |
---|
119 | m_preDBFstatData[i] = new SAOStatData*[MAX_NUM_COMPONENT]; |
---|
120 | for(Int compIdx=0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
121 | { |
---|
122 | m_preDBFstatData[i][compIdx] = new SAOStatData[NUM_SAO_NEW_TYPES]; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | } |
---|
127 | #endif |
---|
128 | |
---|
129 | #if SAO_ENCODING_CHOICE |
---|
130 | ::memset(m_saoDisabledRate, 0, sizeof(m_saoDisabledRate)); |
---|
131 | #endif |
---|
132 | |
---|
133 | for(Int typeIdc=0; typeIdc < NUM_SAO_NEW_TYPES; typeIdc++) |
---|
134 | { |
---|
135 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 5; |
---|
136 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 3; |
---|
137 | |
---|
138 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 4; |
---|
139 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 2; |
---|
140 | |
---|
141 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
142 | if(isPreDBFSamplesUsed) |
---|
143 | { |
---|
144 | switch(typeIdc) |
---|
145 | { |
---|
146 | case SAO_TYPE_EO_0: |
---|
147 | { |
---|
148 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 5; |
---|
149 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 3; |
---|
150 | |
---|
151 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 3; |
---|
152 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 1; |
---|
153 | } |
---|
154 | break; |
---|
155 | case SAO_TYPE_EO_90: |
---|
156 | { |
---|
157 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 4; |
---|
158 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 2; |
---|
159 | |
---|
160 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 4; |
---|
161 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 2; |
---|
162 | } |
---|
163 | break; |
---|
164 | case SAO_TYPE_EO_135: |
---|
165 | case SAO_TYPE_EO_45: |
---|
166 | { |
---|
167 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 5; |
---|
168 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 3; |
---|
169 | |
---|
170 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 4; |
---|
171 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 2; |
---|
172 | } |
---|
173 | break; |
---|
174 | case SAO_TYPE_BO: |
---|
175 | { |
---|
176 | m_skipLinesR[COMPONENT_Y ][typeIdc]= 4; |
---|
177 | m_skipLinesR[COMPONENT_Cb][typeIdc]= m_skipLinesR[COMPONENT_Cr][typeIdc]= 2; |
---|
178 | |
---|
179 | m_skipLinesB[COMPONENT_Y ][typeIdc]= 3; |
---|
180 | m_skipLinesB[COMPONENT_Cb][typeIdc]= m_skipLinesB[COMPONENT_Cr][typeIdc]= 1; |
---|
181 | } |
---|
182 | break; |
---|
183 | default: |
---|
184 | { |
---|
185 | printf("Not a supported type"); |
---|
186 | assert(0); |
---|
187 | exit(-1); |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | #endif |
---|
192 | } |
---|
193 | |
---|
194 | } |
---|
195 | |
---|
196 | Void TEncSampleAdaptiveOffset::destroyEncData() |
---|
197 | { |
---|
198 | if(m_pppcRDSbacCoder != NULL) |
---|
199 | { |
---|
200 | for (Int cs = 0; cs < NUM_SAO_CABACSTATE_LABELS; cs ++ ) |
---|
201 | { |
---|
202 | delete m_pppcRDSbacCoder[cs]; |
---|
203 | } |
---|
204 | delete[] m_pppcRDSbacCoder; m_pppcRDSbacCoder = NULL; |
---|
205 | } |
---|
206 | |
---|
207 | if(m_pppcBinCoderCABAC != NULL) |
---|
208 | { |
---|
209 | for (Int cs = 0; cs < NUM_SAO_CABACSTATE_LABELS; cs ++ ) |
---|
210 | { |
---|
211 | delete m_pppcBinCoderCABAC[cs]; |
---|
212 | } |
---|
213 | delete[] m_pppcBinCoderCABAC; m_pppcBinCoderCABAC = NULL; |
---|
214 | } |
---|
215 | |
---|
216 | if(m_statData != NULL) |
---|
217 | { |
---|
218 | for(Int i=0; i< m_numCTUsPic; i++) |
---|
219 | { |
---|
220 | for(Int compIdx=0; compIdx< MAX_NUM_COMPONENT; compIdx++) |
---|
221 | { |
---|
222 | delete[] m_statData[i][compIdx]; |
---|
223 | } |
---|
224 | delete[] m_statData[i]; |
---|
225 | } |
---|
226 | delete[] m_statData; m_statData = NULL; |
---|
227 | } |
---|
228 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
229 | if(m_preDBFstatData != NULL) |
---|
230 | { |
---|
231 | for(Int i=0; i< m_numCTUsPic; i++) |
---|
232 | { |
---|
233 | for(Int compIdx=0; compIdx< MAX_NUM_COMPONENT; compIdx++) |
---|
234 | { |
---|
235 | delete[] m_preDBFstatData[i][compIdx]; |
---|
236 | } |
---|
237 | delete[] m_preDBFstatData[i]; |
---|
238 | } |
---|
239 | delete[] m_preDBFstatData; m_preDBFstatData = NULL; |
---|
240 | } |
---|
241 | |
---|
242 | #endif |
---|
243 | } |
---|
244 | |
---|
245 | Void TEncSampleAdaptiveOffset::initRDOCabacCoder(TEncSbac* pcRDGoOnSbacCoder, TComSlice* pcSlice) |
---|
246 | { |
---|
247 | m_pcRDGoOnSbacCoder = pcRDGoOnSbacCoder; |
---|
248 | m_pcRDGoOnSbacCoder->resetEntropy(pcSlice); |
---|
249 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
250 | |
---|
251 | m_pcRDGoOnSbacCoder->store( m_pppcRDSbacCoder[SAO_CABACSTATE_PIC_INIT]); |
---|
252 | } |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | Void TEncSampleAdaptiveOffset::SAOProcess(TComPic* pPic, Bool* sliceEnabled, const Double *lambdas |
---|
257 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
258 | , Bool isPreDBFSamplesUsed |
---|
259 | #endif |
---|
260 | ) |
---|
261 | { |
---|
262 | TComPicYuv* orgYuv= pPic->getPicYuvOrg(); |
---|
263 | TComPicYuv* resYuv= pPic->getPicYuvRec(); |
---|
264 | memcpy(m_lambda, lambdas, sizeof(m_lambda)); |
---|
265 | TComPicYuv* srcYuv = m_tempPicYuv; |
---|
266 | resYuv->copyToPic(srcYuv); |
---|
267 | srcYuv->setBorderExtension(false); |
---|
268 | srcYuv->extendPicBorder(); |
---|
269 | |
---|
270 | //collect statistics |
---|
271 | getStatistics(m_statData, orgYuv, srcYuv, pPic); |
---|
272 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
273 | if(isPreDBFSamplesUsed) |
---|
274 | { |
---|
275 | addPreDBFStatistics(m_statData); |
---|
276 | } |
---|
277 | #endif |
---|
278 | //slice on/off |
---|
279 | decidePicParams(sliceEnabled, pPic->getSlice(0)->getDepth()); |
---|
280 | |
---|
281 | //block on/off |
---|
282 | SAOBlkParam* reconParams = new SAOBlkParam[m_numCTUsPic]; //temporary parameter buffer for storing reconstructed SAO parameters |
---|
283 | decideBlkParams(pPic, sliceEnabled, m_statData, srcYuv, resYuv, reconParams, pPic->getPicSym()->getSAOBlkParam()); |
---|
284 | delete[] reconParams; |
---|
285 | } |
---|
286 | |
---|
287 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
288 | Void TEncSampleAdaptiveOffset::getPreDBFStatistics(TComPic* pPic) |
---|
289 | { |
---|
290 | getStatistics(m_preDBFstatData, pPic->getPicYuvOrg(), pPic->getPicYuvRec(), pPic, true); |
---|
291 | } |
---|
292 | |
---|
293 | Void TEncSampleAdaptiveOffset::addPreDBFStatistics(SAOStatData*** blkStats) |
---|
294 | { |
---|
295 | for(Int n=0; n< m_numCTUsPic; n++) |
---|
296 | { |
---|
297 | for(Int compIdx=0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
298 | { |
---|
299 | for(Int typeIdc=0; typeIdc < NUM_SAO_NEW_TYPES; typeIdc++) |
---|
300 | { |
---|
301 | blkStats[n][compIdx][typeIdc] += m_preDBFstatData[n][compIdx][typeIdc]; |
---|
302 | } |
---|
303 | } |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | #endif |
---|
308 | |
---|
309 | Void TEncSampleAdaptiveOffset::getStatistics(SAOStatData*** blkStats, TComPicYuv* orgYuv, TComPicYuv* srcYuv, TComPic* pPic |
---|
310 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
311 | , Bool isCalculatePreDeblockSamples |
---|
312 | #endif |
---|
313 | ) |
---|
314 | { |
---|
315 | Bool isLeftAvail,isRightAvail,isAboveAvail,isBelowAvail,isAboveLeftAvail,isAboveRightAvail,isBelowLeftAvail,isBelowRightAvail; |
---|
316 | |
---|
317 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
318 | |
---|
319 | for(Int ctuRsAddr= 0; ctuRsAddr < m_numCTUsPic; ctuRsAddr++) |
---|
320 | { |
---|
321 | Int yPos = (ctuRsAddr / m_numCTUInWidth)*m_maxCUHeight; |
---|
322 | Int xPos = (ctuRsAddr % m_numCTUInWidth)*m_maxCUWidth; |
---|
323 | Int height = (yPos + m_maxCUHeight > m_picHeight)?(m_picHeight- yPos):m_maxCUHeight; |
---|
324 | Int width = (xPos + m_maxCUWidth > m_picWidth )?(m_picWidth - xPos):m_maxCUWidth; |
---|
325 | |
---|
326 | pPic->getPicSym()->deriveLoopFilterBoundaryAvailibility(ctuRsAddr, isLeftAvail,isRightAvail,isAboveAvail,isBelowAvail,isAboveLeftAvail,isAboveRightAvail,isBelowLeftAvail,isBelowRightAvail); |
---|
327 | |
---|
328 | //NOTE: The number of skipped lines during gathering CTU statistics depends on the slice boundary availabilities. |
---|
329 | //For simplicity, here only picture boundaries are considered. |
---|
330 | |
---|
331 | isRightAvail = (xPos + m_maxCUWidth < m_picWidth ); |
---|
332 | isBelowAvail = (yPos + m_maxCUHeight < m_picHeight); |
---|
333 | isBelowRightAvail = (isRightAvail && isBelowAvail); |
---|
334 | isBelowLeftAvail = ((xPos > 0) && (isBelowAvail)); |
---|
335 | isAboveRightAvail = ((yPos > 0) && (isRightAvail)); |
---|
336 | |
---|
337 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
338 | { |
---|
339 | const ComponentID component = ComponentID(compIdx); |
---|
340 | |
---|
341 | const UInt componentScaleX = getComponentScaleX(component, pPic->getChromaFormat()); |
---|
342 | const UInt componentScaleY = getComponentScaleY(component, pPic->getChromaFormat()); |
---|
343 | |
---|
344 | Int srcStride = srcYuv->getStride(component); |
---|
345 | Pel* srcBlk = srcYuv->getAddr(component) + ((yPos >> componentScaleY) * srcStride) + (xPos >> componentScaleX); |
---|
346 | |
---|
347 | Int orgStride = orgYuv->getStride(component); |
---|
348 | Pel* orgBlk = orgYuv->getAddr(component) + ((yPos >> componentScaleY) * orgStride) + (xPos >> componentScaleX); |
---|
349 | |
---|
350 | #if SVC_EXTENSION |
---|
351 | getBlkStats(component, pPic->getSlice(0)->getBitDepth(toChannelType(component)), blkStats[ctuRsAddr][component] |
---|
352 | #else |
---|
353 | getBlkStats(component, pPic->getPicSym()->getSPS().getBitDepth(toChannelType(component)), blkStats[ctuRsAddr][component] |
---|
354 | #endif |
---|
355 | , srcBlk, orgBlk, srcStride, orgStride, (width >> componentScaleX), (height >> componentScaleY) |
---|
356 | , isLeftAvail, isRightAvail, isAboveAvail, isBelowAvail, isAboveLeftAvail, isAboveRightAvail |
---|
357 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
358 | , isCalculatePreDeblockSamples |
---|
359 | #endif |
---|
360 | ); |
---|
361 | |
---|
362 | } |
---|
363 | } |
---|
364 | } |
---|
365 | |
---|
366 | Void TEncSampleAdaptiveOffset::decidePicParams(Bool* sliceEnabled, Int picTempLayer) |
---|
367 | { |
---|
368 | //decide sliceEnabled[compIdx] |
---|
369 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
370 | for (Int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
371 | { |
---|
372 | sliceEnabled[compIdx] = false; |
---|
373 | } |
---|
374 | |
---|
375 | for (Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
376 | { |
---|
377 | // reset flags & counters |
---|
378 | sliceEnabled[compIdx] = true; |
---|
379 | |
---|
380 | #if SAO_ENCODING_CHOICE |
---|
381 | #if SAO_ENCODING_CHOICE_CHROMA |
---|
382 | // decide slice-level on/off based on previous results |
---|
383 | if( (picTempLayer > 0) |
---|
384 | && (m_saoDisabledRate[compIdx][picTempLayer-1] > ((compIdx==COMPONENT_Y) ? SAO_ENCODING_RATE : SAO_ENCODING_RATE_CHROMA)) ) |
---|
385 | { |
---|
386 | sliceEnabled[compIdx] = false; |
---|
387 | } |
---|
388 | #else |
---|
389 | // decide slice-level on/off based on previous results |
---|
390 | if( (picTempLayer > 0) |
---|
391 | && (m_saoDisabledRate[COMPONENT_Y][0] > SAO_ENCODING_RATE) ) |
---|
392 | { |
---|
393 | sliceEnabled[compIdx] = false; |
---|
394 | } |
---|
395 | #endif |
---|
396 | #endif |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | Int64 TEncSampleAdaptiveOffset::getDistortion(const Int channelBitDepth, Int typeIdc, Int typeAuxInfo, Int* invQuantOffset, SAOStatData& statData) |
---|
401 | { |
---|
402 | Int64 dist = 0; |
---|
403 | Int shift = 2 * DISTORTION_PRECISION_ADJUSTMENT(channelBitDepth - 8); |
---|
404 | |
---|
405 | switch(typeIdc) |
---|
406 | { |
---|
407 | case SAO_TYPE_EO_0: |
---|
408 | case SAO_TYPE_EO_90: |
---|
409 | case SAO_TYPE_EO_135: |
---|
410 | case SAO_TYPE_EO_45: |
---|
411 | { |
---|
412 | for (Int offsetIdx=0; offsetIdx<NUM_SAO_EO_CLASSES; offsetIdx++) |
---|
413 | { |
---|
414 | dist += estSaoDist( statData.count[offsetIdx], invQuantOffset[offsetIdx], statData.diff[offsetIdx], shift); |
---|
415 | } |
---|
416 | } |
---|
417 | break; |
---|
418 | case SAO_TYPE_BO: |
---|
419 | { |
---|
420 | for (Int offsetIdx=typeAuxInfo; offsetIdx<typeAuxInfo+4; offsetIdx++) |
---|
421 | { |
---|
422 | Int bandIdx = offsetIdx % NUM_SAO_BO_CLASSES ; |
---|
423 | dist += estSaoDist( statData.count[bandIdx], invQuantOffset[bandIdx], statData.diff[bandIdx], shift); |
---|
424 | } |
---|
425 | } |
---|
426 | break; |
---|
427 | default: |
---|
428 | { |
---|
429 | printf("Not a supported type"); |
---|
430 | assert(0); |
---|
431 | exit(-1); |
---|
432 | } |
---|
433 | } |
---|
434 | |
---|
435 | return dist; |
---|
436 | } |
---|
437 | |
---|
438 | inline Int64 TEncSampleAdaptiveOffset::estSaoDist(Int64 count, Int64 offset, Int64 diffSum, Int shift) |
---|
439 | { |
---|
440 | return (( count*offset*offset-diffSum*offset*2 ) >> shift); |
---|
441 | } |
---|
442 | |
---|
443 | |
---|
444 | inline Int TEncSampleAdaptiveOffset::estIterOffset(Int typeIdx, Double lambda, Int offsetInput, Int64 count, Int64 diffSum, Int shift, Int bitIncrease, Int64& bestDist, Double& bestCost, Int offsetTh ) |
---|
445 | { |
---|
446 | Int iterOffset, tempOffset; |
---|
447 | Int64 tempDist, tempRate; |
---|
448 | Double tempCost, tempMinCost; |
---|
449 | Int offsetOutput = 0; |
---|
450 | iterOffset = offsetInput; |
---|
451 | // Assuming sending quantized value 0 results in zero offset and sending the value zero needs 1 bit. entropy coder can be used to measure the exact rate here. |
---|
452 | tempMinCost = lambda; |
---|
453 | while (iterOffset != 0) |
---|
454 | { |
---|
455 | // Calculate the bits required for signaling the offset |
---|
456 | tempRate = (typeIdx == SAO_TYPE_BO) ? (abs((Int)iterOffset)+2) : (abs((Int)iterOffset)+1); |
---|
457 | if (abs((Int)iterOffset)==offsetTh) //inclusive |
---|
458 | { |
---|
459 | tempRate --; |
---|
460 | } |
---|
461 | // Do the dequantization before distortion calculation |
---|
462 | tempOffset = iterOffset << bitIncrease; |
---|
463 | tempDist = estSaoDist( count, tempOffset, diffSum, shift); |
---|
464 | tempCost = ((Double)tempDist + lambda * (Double) tempRate); |
---|
465 | if(tempCost < tempMinCost) |
---|
466 | { |
---|
467 | tempMinCost = tempCost; |
---|
468 | offsetOutput = iterOffset; |
---|
469 | bestDist = tempDist; |
---|
470 | bestCost = tempCost; |
---|
471 | } |
---|
472 | iterOffset = (iterOffset > 0) ? (iterOffset-1):(iterOffset+1); |
---|
473 | } |
---|
474 | return offsetOutput; |
---|
475 | } |
---|
476 | |
---|
477 | Void TEncSampleAdaptiveOffset::deriveOffsets(ComponentID compIdx, const Int channelBitDepth, Int typeIdc, SAOStatData& statData, Int* quantOffsets, Int& typeAuxInfo) |
---|
478 | { |
---|
479 | Int bitDepth = channelBitDepth; |
---|
480 | Int shift = 2 * DISTORTION_PRECISION_ADJUSTMENT(bitDepth-8); |
---|
481 | Int offsetTh = TComSampleAdaptiveOffset::getMaxOffsetQVal(channelBitDepth); //inclusive |
---|
482 | |
---|
483 | ::memset(quantOffsets, 0, sizeof(Int)*MAX_NUM_SAO_CLASSES); |
---|
484 | |
---|
485 | //derive initial offsets |
---|
486 | Int numClasses = (typeIdc == SAO_TYPE_BO)?((Int)NUM_SAO_BO_CLASSES):((Int)NUM_SAO_EO_CLASSES); |
---|
487 | for(Int classIdx=0; classIdx< numClasses; classIdx++) |
---|
488 | { |
---|
489 | if( (typeIdc != SAO_TYPE_BO) && (classIdx==SAO_CLASS_EO_PLAIN) ) |
---|
490 | { |
---|
491 | continue; //offset will be zero |
---|
492 | } |
---|
493 | |
---|
494 | if(statData.count[classIdx] == 0) |
---|
495 | { |
---|
496 | continue; //offset will be zero |
---|
497 | } |
---|
498 | |
---|
499 | quantOffsets[classIdx] = (Int) xRoundIbdi(bitDepth, (Double)( statData.diff[classIdx]<<(bitDepth-8)) |
---|
500 | / |
---|
501 | (Double)( statData.count[classIdx]<< m_offsetStepLog2[compIdx]) |
---|
502 | ); |
---|
503 | quantOffsets[classIdx] = Clip3(-offsetTh, offsetTh, quantOffsets[classIdx]); |
---|
504 | } |
---|
505 | |
---|
506 | // adjust offsets |
---|
507 | switch(typeIdc) |
---|
508 | { |
---|
509 | case SAO_TYPE_EO_0: |
---|
510 | case SAO_TYPE_EO_90: |
---|
511 | case SAO_TYPE_EO_135: |
---|
512 | case SAO_TYPE_EO_45: |
---|
513 | { |
---|
514 | Int64 classDist; |
---|
515 | Double classCost; |
---|
516 | for(Int classIdx=0; classIdx<NUM_SAO_EO_CLASSES; classIdx++) |
---|
517 | { |
---|
518 | if(classIdx==SAO_CLASS_EO_FULL_VALLEY && quantOffsets[classIdx] < 0) |
---|
519 | { |
---|
520 | quantOffsets[classIdx] =0; |
---|
521 | } |
---|
522 | if(classIdx==SAO_CLASS_EO_HALF_VALLEY && quantOffsets[classIdx] < 0) |
---|
523 | { |
---|
524 | quantOffsets[classIdx] =0; |
---|
525 | } |
---|
526 | if(classIdx==SAO_CLASS_EO_HALF_PEAK && quantOffsets[classIdx] > 0) |
---|
527 | { |
---|
528 | quantOffsets[classIdx] =0; |
---|
529 | } |
---|
530 | if(classIdx==SAO_CLASS_EO_FULL_PEAK && quantOffsets[classIdx] > 0) |
---|
531 | { |
---|
532 | quantOffsets[classIdx] =0; |
---|
533 | } |
---|
534 | |
---|
535 | if( quantOffsets[classIdx] != 0 ) //iterative adjustment only when derived offset is not zero |
---|
536 | { |
---|
537 | quantOffsets[classIdx] = estIterOffset( typeIdc, m_lambda[compIdx], quantOffsets[classIdx], statData.count[classIdx], statData.diff[classIdx], shift, m_offsetStepLog2[compIdx], classDist , classCost , offsetTh ); |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | typeAuxInfo =0; |
---|
542 | } |
---|
543 | break; |
---|
544 | case SAO_TYPE_BO: |
---|
545 | { |
---|
546 | Int64 distBOClasses[NUM_SAO_BO_CLASSES]; |
---|
547 | Double costBOClasses[NUM_SAO_BO_CLASSES]; |
---|
548 | ::memset(distBOClasses, 0, sizeof(Int64)*NUM_SAO_BO_CLASSES); |
---|
549 | for(Int classIdx=0; classIdx< NUM_SAO_BO_CLASSES; classIdx++) |
---|
550 | { |
---|
551 | costBOClasses[classIdx]= m_lambda[compIdx]; |
---|
552 | if( quantOffsets[classIdx] != 0 ) //iterative adjustment only when derived offset is not zero |
---|
553 | { |
---|
554 | quantOffsets[classIdx] = estIterOffset( typeIdc, m_lambda[compIdx], quantOffsets[classIdx], statData.count[classIdx], statData.diff[classIdx], shift, m_offsetStepLog2[compIdx], distBOClasses[classIdx], costBOClasses[classIdx], offsetTh ); |
---|
555 | } |
---|
556 | } |
---|
557 | |
---|
558 | //decide the starting band index |
---|
559 | Double minCost = MAX_DOUBLE, cost; |
---|
560 | for(Int band=0; band< NUM_SAO_BO_CLASSES- 4+ 1; band++) |
---|
561 | { |
---|
562 | cost = costBOClasses[band ]; |
---|
563 | cost += costBOClasses[band+1]; |
---|
564 | cost += costBOClasses[band+2]; |
---|
565 | cost += costBOClasses[band+3]; |
---|
566 | |
---|
567 | if(cost < minCost) |
---|
568 | { |
---|
569 | minCost = cost; |
---|
570 | typeAuxInfo = band; |
---|
571 | } |
---|
572 | } |
---|
573 | //clear those unused classes |
---|
574 | Int clearQuantOffset[NUM_SAO_BO_CLASSES]; |
---|
575 | ::memset(clearQuantOffset, 0, sizeof(Int)*NUM_SAO_BO_CLASSES); |
---|
576 | for(Int i=0; i< 4; i++) |
---|
577 | { |
---|
578 | Int band = (typeAuxInfo+i)%NUM_SAO_BO_CLASSES; |
---|
579 | clearQuantOffset[band] = quantOffsets[band]; |
---|
580 | } |
---|
581 | ::memcpy(quantOffsets, clearQuantOffset, sizeof(Int)*NUM_SAO_BO_CLASSES); |
---|
582 | } |
---|
583 | break; |
---|
584 | default: |
---|
585 | { |
---|
586 | printf("Not a supported type"); |
---|
587 | assert(0); |
---|
588 | exit(-1); |
---|
589 | } |
---|
590 | |
---|
591 | } |
---|
592 | |
---|
593 | |
---|
594 | } |
---|
595 | |
---|
596 | Void TEncSampleAdaptiveOffset::deriveModeNewRDO(const BitDepths &bitDepths, Int ctuRsAddr, SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES], Bool* sliceEnabled, SAOStatData*** blkStats, SAOBlkParam& modeParam, Double& modeNormCost, TEncSbac** cabacCoderRDO, Int inCabacLabel) |
---|
597 | { |
---|
598 | Double minCost, cost; |
---|
599 | UInt previousWrittenBits; |
---|
600 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
601 | |
---|
602 | Int64 dist[MAX_NUM_COMPONENT], modeDist[MAX_NUM_COMPONENT]; |
---|
603 | SAOOffset testOffset[MAX_NUM_COMPONENT]; |
---|
604 | Int invQuantOffset[MAX_NUM_SAO_CLASSES]; |
---|
605 | for(Int comp=0; comp < MAX_NUM_COMPONENT; comp++) |
---|
606 | { |
---|
607 | modeDist[comp] = 0; |
---|
608 | } |
---|
609 | |
---|
610 | //pre-encode merge flags |
---|
611 | modeParam[COMPONENT_Y].modeIdc = SAO_MODE_OFF; |
---|
612 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[inCabacLabel]); |
---|
613 | m_pcRDGoOnSbacCoder->codeSAOBlkParam(modeParam, bitDepths, sliceEnabled, (mergeList[SAO_MERGE_LEFT]!= NULL), (mergeList[SAO_MERGE_ABOVE]!= NULL), true); |
---|
614 | m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]); |
---|
615 | |
---|
616 | //------ luma --------// |
---|
617 | { |
---|
618 | const ComponentID compIdx = COMPONENT_Y; |
---|
619 | //"off" case as initial cost |
---|
620 | modeParam[compIdx].modeIdc = SAO_MODE_OFF; |
---|
621 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
622 | m_pcRDGoOnSbacCoder->codeSAOOffsetParam(compIdx, modeParam[compIdx], sliceEnabled[compIdx], bitDepths.recon[CHANNEL_TYPE_LUMA]); |
---|
623 | modeDist[compIdx] = 0; |
---|
624 | minCost= m_lambda[compIdx]*((Double)m_pcRDGoOnSbacCoder->getNumberOfWrittenBits()); |
---|
625 | m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]); |
---|
626 | if(sliceEnabled[compIdx]) |
---|
627 | { |
---|
628 | for(Int typeIdc=0; typeIdc< NUM_SAO_NEW_TYPES; typeIdc++) |
---|
629 | { |
---|
630 | testOffset[compIdx].modeIdc = SAO_MODE_NEW; |
---|
631 | testOffset[compIdx].typeIdc = typeIdc; |
---|
632 | |
---|
633 | //derive coded offset |
---|
634 | deriveOffsets(compIdx, bitDepths.recon[CHANNEL_TYPE_LUMA], typeIdc, blkStats[ctuRsAddr][compIdx][typeIdc], testOffset[compIdx].offset, testOffset[compIdx].typeAuxInfo); |
---|
635 | |
---|
636 | //inversed quantized offsets |
---|
637 | invertQuantOffsets(compIdx, typeIdc, testOffset[compIdx].typeAuxInfo, invQuantOffset, testOffset[compIdx].offset); |
---|
638 | |
---|
639 | //get distortion |
---|
640 | dist[compIdx] = getDistortion(bitDepths.recon[CHANNEL_TYPE_LUMA], testOffset[compIdx].typeIdc, testOffset[compIdx].typeAuxInfo, invQuantOffset, blkStats[ctuRsAddr][compIdx][typeIdc]); |
---|
641 | |
---|
642 | //get rate |
---|
643 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]); |
---|
644 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
645 | m_pcRDGoOnSbacCoder->codeSAOOffsetParam(compIdx, testOffset[compIdx], sliceEnabled[compIdx], bitDepths.recon[CHANNEL_TYPE_LUMA]); |
---|
646 | Int rate = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
---|
647 | cost = (Double)dist[compIdx] + m_lambda[compIdx]*((Double)rate); |
---|
648 | if(cost < minCost) |
---|
649 | { |
---|
650 | minCost = cost; |
---|
651 | modeDist[compIdx] = dist[compIdx]; |
---|
652 | modeParam[compIdx]= testOffset[compIdx]; |
---|
653 | m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]); |
---|
654 | } |
---|
655 | } |
---|
656 | } |
---|
657 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]); |
---|
658 | m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]); |
---|
659 | } |
---|
660 | |
---|
661 | //------ chroma --------// |
---|
662 | //"off" case as initial cost |
---|
663 | cost = 0; |
---|
664 | previousWrittenBits = 0; |
---|
665 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
666 | for(UInt componentIndex = COMPONENT_Cb; componentIndex < numberOfComponents; componentIndex++) |
---|
667 | { |
---|
668 | const ComponentID component = ComponentID(componentIndex); |
---|
669 | |
---|
670 | modeParam[component].modeIdc = SAO_MODE_OFF; |
---|
671 | modeDist [component] = 0; |
---|
672 | m_pcRDGoOnSbacCoder->codeSAOOffsetParam(component, modeParam[component], sliceEnabled[component], bitDepths.recon[CHANNEL_TYPE_CHROMA]); |
---|
673 | |
---|
674 | const UInt currentWrittenBits = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
---|
675 | cost += m_lambda[component] * (currentWrittenBits - previousWrittenBits); |
---|
676 | previousWrittenBits = currentWrittenBits; |
---|
677 | } |
---|
678 | |
---|
679 | minCost = cost; |
---|
680 | |
---|
681 | //doesn't need to store cabac status here since the whole CTU parameters will be re-encoded at the end of this function |
---|
682 | |
---|
683 | for(Int typeIdc=0; typeIdc< NUM_SAO_NEW_TYPES; typeIdc++) |
---|
684 | { |
---|
685 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_MID]); |
---|
686 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
687 | previousWrittenBits = 0; |
---|
688 | cost = 0; |
---|
689 | |
---|
690 | for(UInt componentIndex = COMPONENT_Cb; componentIndex < numberOfComponents; componentIndex++) |
---|
691 | { |
---|
692 | const ComponentID component = ComponentID(componentIndex); |
---|
693 | if(!sliceEnabled[component]) |
---|
694 | { |
---|
695 | testOffset[component].modeIdc = SAO_MODE_OFF; |
---|
696 | dist[component]= 0; |
---|
697 | continue; |
---|
698 | } |
---|
699 | testOffset[component].modeIdc = SAO_MODE_NEW; |
---|
700 | testOffset[component].typeIdc = typeIdc; |
---|
701 | |
---|
702 | //derive offset & get distortion |
---|
703 | deriveOffsets(component, bitDepths.recon[CHANNEL_TYPE_CHROMA], typeIdc, blkStats[ctuRsAddr][component][typeIdc], testOffset[component].offset, testOffset[component].typeAuxInfo); |
---|
704 | invertQuantOffsets(component, typeIdc, testOffset[component].typeAuxInfo, invQuantOffset, testOffset[component].offset); |
---|
705 | dist[component] = getDistortion(bitDepths.recon[CHANNEL_TYPE_CHROMA], typeIdc, testOffset[component].typeAuxInfo, invQuantOffset, blkStats[ctuRsAddr][component][typeIdc]); |
---|
706 | |
---|
707 | m_pcRDGoOnSbacCoder->codeSAOOffsetParam(component, testOffset[component], sliceEnabled[component], bitDepths.recon[CHANNEL_TYPE_CHROMA]); |
---|
708 | |
---|
709 | const UInt currentWrittenBits = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
---|
710 | cost += dist[component] + (m_lambda[component] * (currentWrittenBits - previousWrittenBits)); |
---|
711 | previousWrittenBits = currentWrittenBits; |
---|
712 | } |
---|
713 | |
---|
714 | if(cost < minCost) |
---|
715 | { |
---|
716 | minCost = cost; |
---|
717 | for(UInt componentIndex = COMPONENT_Cb; componentIndex < numberOfComponents; componentIndex++) |
---|
718 | { |
---|
719 | modeDist[componentIndex] = dist[componentIndex]; |
---|
720 | modeParam[componentIndex] = testOffset[componentIndex]; |
---|
721 | } |
---|
722 | } |
---|
723 | |
---|
724 | } // SAO_TYPE loop |
---|
725 | |
---|
726 | //----- re-gen rate & normalized cost----// |
---|
727 | modeNormCost = 0; |
---|
728 | for(UInt componentIndex = COMPONENT_Y; componentIndex < numberOfComponents; componentIndex++) |
---|
729 | { |
---|
730 | modeNormCost += (Double)modeDist[componentIndex] / m_lambda[componentIndex]; |
---|
731 | } |
---|
732 | |
---|
733 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[inCabacLabel]); |
---|
734 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
735 | m_pcRDGoOnSbacCoder->codeSAOBlkParam(modeParam, bitDepths, sliceEnabled, (mergeList[SAO_MERGE_LEFT]!= NULL), (mergeList[SAO_MERGE_ABOVE]!= NULL), false); |
---|
736 | modeNormCost += (Double)m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
---|
737 | } |
---|
738 | |
---|
739 | Void TEncSampleAdaptiveOffset::deriveModeMergeRDO(const BitDepths &bitDepths, Int ctuRsAddr, SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES], Bool* sliceEnabled, SAOStatData*** blkStats, SAOBlkParam& modeParam, Double& modeNormCost, TEncSbac** cabacCoderRDO, Int inCabacLabel) |
---|
740 | { |
---|
741 | modeNormCost = MAX_DOUBLE; |
---|
742 | |
---|
743 | Double cost; |
---|
744 | SAOBlkParam testBlkParam; |
---|
745 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
746 | |
---|
747 | for(Int mergeType=0; mergeType< NUM_SAO_MERGE_TYPES; mergeType++) |
---|
748 | { |
---|
749 | if(mergeList[mergeType] == NULL) |
---|
750 | { |
---|
751 | continue; |
---|
752 | } |
---|
753 | |
---|
754 | testBlkParam = *(mergeList[mergeType]); |
---|
755 | //normalized distortion |
---|
756 | Double normDist=0; |
---|
757 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
758 | { |
---|
759 | testBlkParam[compIdx].modeIdc = SAO_MODE_MERGE; |
---|
760 | testBlkParam[compIdx].typeIdc = mergeType; |
---|
761 | |
---|
762 | SAOOffset& mergedOffsetParam = (*(mergeList[mergeType]))[compIdx]; |
---|
763 | |
---|
764 | if( mergedOffsetParam.modeIdc != SAO_MODE_OFF) |
---|
765 | { |
---|
766 | //offsets have been reconstructed. Don't call inversed quantization function. |
---|
767 | normDist += (((Double)getDistortion(bitDepths.recon[toChannelType(ComponentID(compIdx))], mergedOffsetParam.typeIdc, mergedOffsetParam.typeAuxInfo, mergedOffsetParam.offset, blkStats[ctuRsAddr][compIdx][mergedOffsetParam.typeIdc])) |
---|
768 | /m_lambda[compIdx] |
---|
769 | ); |
---|
770 | } |
---|
771 | |
---|
772 | } |
---|
773 | |
---|
774 | //rate |
---|
775 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[inCabacLabel]); |
---|
776 | m_pcRDGoOnSbacCoder->resetBits(); |
---|
777 | m_pcRDGoOnSbacCoder->codeSAOBlkParam(testBlkParam, bitDepths, sliceEnabled, (mergeList[SAO_MERGE_LEFT]!= NULL), (mergeList[SAO_MERGE_ABOVE]!= NULL), false); |
---|
778 | Int rate = m_pcRDGoOnSbacCoder->getNumberOfWrittenBits(); |
---|
779 | |
---|
780 | cost = normDist+(Double)rate; |
---|
781 | |
---|
782 | if(cost < modeNormCost) |
---|
783 | { |
---|
784 | modeNormCost = cost; |
---|
785 | modeParam = testBlkParam; |
---|
786 | m_pcRDGoOnSbacCoder->store(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]); |
---|
787 | } |
---|
788 | } |
---|
789 | |
---|
790 | m_pcRDGoOnSbacCoder->load(cabacCoderRDO[SAO_CABACSTATE_BLK_TEMP]); |
---|
791 | } |
---|
792 | |
---|
793 | Void TEncSampleAdaptiveOffset::decideBlkParams(TComPic* pic, Bool* sliceEnabled, SAOStatData*** blkStats, TComPicYuv* srcYuv, TComPicYuv* resYuv, SAOBlkParam* reconParams, SAOBlkParam* codedParams) |
---|
794 | { |
---|
795 | Bool allBlksDisabled = true; |
---|
796 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
797 | for(Int compId = COMPONENT_Y; compId < numberOfComponents; compId++) |
---|
798 | { |
---|
799 | if (sliceEnabled[compId]) |
---|
800 | { |
---|
801 | allBlksDisabled = false; |
---|
802 | } |
---|
803 | } |
---|
804 | |
---|
805 | m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[ SAO_CABACSTATE_PIC_INIT ]); |
---|
806 | |
---|
807 | SAOBlkParam modeParam; |
---|
808 | Double minCost, modeCost; |
---|
809 | |
---|
810 | |
---|
811 | #if RD_TEST_SAO_DISABLE_AT_PICTURE_LEVEL |
---|
812 | Double totalCost = 0; |
---|
813 | #endif |
---|
814 | |
---|
815 | for(Int ctuRsAddr=0; ctuRsAddr< m_numCTUsPic; ctuRsAddr++) |
---|
816 | { |
---|
817 | if(allBlksDisabled) |
---|
818 | { |
---|
819 | codedParams[ctuRsAddr].reset(); |
---|
820 | continue; |
---|
821 | } |
---|
822 | |
---|
823 | m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[ SAO_CABACSTATE_BLK_CUR ]); |
---|
824 | |
---|
825 | //get merge list |
---|
826 | SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES] = { NULL }; |
---|
827 | getMergeList(pic, ctuRsAddr, reconParams, mergeList); |
---|
828 | |
---|
829 | minCost = MAX_DOUBLE; |
---|
830 | for(Int mode=0; mode < NUM_SAO_MODES; mode++) |
---|
831 | { |
---|
832 | switch(mode) |
---|
833 | { |
---|
834 | case SAO_MODE_OFF: |
---|
835 | { |
---|
836 | continue; //not necessary, since all-off case will be tested in SAO_MODE_NEW case. |
---|
837 | } |
---|
838 | break; |
---|
839 | case SAO_MODE_NEW: |
---|
840 | { |
---|
841 | #if SVC_EXTENSION |
---|
842 | deriveModeNewRDO(pic->getSlice(0)->getBitDepths(), ctuRsAddr, mergeList, sliceEnabled, blkStats, modeParam, modeCost, m_pppcRDSbacCoder, SAO_CABACSTATE_BLK_CUR); |
---|
843 | #else |
---|
844 | deriveModeNewRDO(pic->getPicSym()->getSPS().getBitDepths(), ctuRsAddr, mergeList, sliceEnabled, blkStats, modeParam, modeCost, m_pppcRDSbacCoder, SAO_CABACSTATE_BLK_CUR); |
---|
845 | #endif |
---|
846 | |
---|
847 | } |
---|
848 | break; |
---|
849 | case SAO_MODE_MERGE: |
---|
850 | { |
---|
851 | #if SVC_EXTENSION |
---|
852 | deriveModeMergeRDO(pic->getSlice(0)->getBitDepths(), ctuRsAddr, mergeList, sliceEnabled, blkStats , modeParam, modeCost, m_pppcRDSbacCoder, SAO_CABACSTATE_BLK_CUR); |
---|
853 | #else |
---|
854 | deriveModeMergeRDO(pic->getPicSym()->getSPS().getBitDepths(), ctuRsAddr, mergeList, sliceEnabled, blkStats , modeParam, modeCost, m_pppcRDSbacCoder, SAO_CABACSTATE_BLK_CUR); |
---|
855 | #endif |
---|
856 | } |
---|
857 | break; |
---|
858 | default: |
---|
859 | { |
---|
860 | printf("Not a supported SAO mode\n"); |
---|
861 | assert(0); |
---|
862 | exit(-1); |
---|
863 | } |
---|
864 | } |
---|
865 | |
---|
866 | if(modeCost < minCost) |
---|
867 | { |
---|
868 | minCost = modeCost; |
---|
869 | codedParams[ctuRsAddr] = modeParam; |
---|
870 | m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[ SAO_CABACSTATE_BLK_NEXT ]); |
---|
871 | } |
---|
872 | } //mode |
---|
873 | |
---|
874 | #if RD_TEST_SAO_DISABLE_AT_PICTURE_LEVEL |
---|
875 | totalCost += minCost; |
---|
876 | #endif |
---|
877 | |
---|
878 | m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[ SAO_CABACSTATE_BLK_NEXT ]); |
---|
879 | |
---|
880 | //apply reconstructed offsets |
---|
881 | reconParams[ctuRsAddr] = codedParams[ctuRsAddr]; |
---|
882 | reconstructBlkSAOParam(reconParams[ctuRsAddr], mergeList); |
---|
883 | offsetCTU(ctuRsAddr, srcYuv, resYuv, reconParams[ctuRsAddr], pic); |
---|
884 | } //ctuRsAddr |
---|
885 | |
---|
886 | #if RD_TEST_SAO_DISABLE_AT_PICTURE_LEVEL |
---|
887 | if (!allBlksDisabled && (totalCost >= 0)) //SAO is not beneficial - disable it |
---|
888 | { |
---|
889 | for(Int ctuRsAddr = 0; ctuRsAddr < m_numCTUsPic; ctuRsAddr++) |
---|
890 | { |
---|
891 | codedParams[ctuRsAddr].reset(); |
---|
892 | } |
---|
893 | |
---|
894 | for (UInt componentIndex = 0; componentIndex < MAX_NUM_COMPONENT; componentIndex++) |
---|
895 | { |
---|
896 | sliceEnabled[componentIndex] = false; |
---|
897 | } |
---|
898 | |
---|
899 | m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[ SAO_CABACSTATE_PIC_INIT ]); |
---|
900 | } |
---|
901 | #endif |
---|
902 | |
---|
903 | #if SAO_ENCODING_CHOICE |
---|
904 | Int picTempLayer = pic->getSlice(0)->getDepth(); |
---|
905 | Int numCtusForSAOOff[MAX_NUM_COMPONENT]; |
---|
906 | |
---|
907 | for (Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
908 | { |
---|
909 | numCtusForSAOOff[compIdx] = 0; |
---|
910 | for(Int ctuRsAddr=0; ctuRsAddr< m_numCTUsPic; ctuRsAddr++) |
---|
911 | { |
---|
912 | if( reconParams[ctuRsAddr][compIdx].modeIdc == SAO_MODE_OFF) |
---|
913 | { |
---|
914 | numCtusForSAOOff[compIdx]++; |
---|
915 | } |
---|
916 | } |
---|
917 | } |
---|
918 | #if SAO_ENCODING_CHOICE_CHROMA |
---|
919 | for (Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
920 | { |
---|
921 | m_saoDisabledRate[compIdx][picTempLayer] = (Double)numCtusForSAOOff[compIdx]/(Double)m_numCTUsPic; |
---|
922 | } |
---|
923 | #else |
---|
924 | if (picTempLayer == 0) |
---|
925 | { |
---|
926 | m_saoDisabledRate[COMPONENT_Y][0] = (Double)(numCtusForSAOOff[COMPONENT_Y]+numCtusForSAOOff[COMPONENT_Cb]+numCtusForSAOOff[COMPONENT_Cr])/(Double)(m_numCTUsPic*3); |
---|
927 | } |
---|
928 | #endif |
---|
929 | #endif |
---|
930 | } |
---|
931 | |
---|
932 | |
---|
933 | Void TEncSampleAdaptiveOffset::getBlkStats(const ComponentID compIdx, const Int channelBitDepth, SAOStatData* statsDataTypes |
---|
934 | , Pel* srcBlk, Pel* orgBlk, Int srcStride, Int orgStride, Int width, Int height |
---|
935 | , Bool isLeftAvail, Bool isRightAvail, Bool isAboveAvail, Bool isBelowAvail, Bool isAboveLeftAvail, Bool isAboveRightAvail |
---|
936 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
937 | , Bool isCalculatePreDeblockSamples |
---|
938 | #endif |
---|
939 | ) |
---|
940 | { |
---|
941 | if(m_lineBufWidth != m_maxCUWidth) |
---|
942 | { |
---|
943 | m_lineBufWidth = m_maxCUWidth; |
---|
944 | |
---|
945 | if (m_signLineBuf1) |
---|
946 | { |
---|
947 | delete[] m_signLineBuf1; |
---|
948 | m_signLineBuf1 = NULL; |
---|
949 | } |
---|
950 | m_signLineBuf1 = new Char[m_lineBufWidth+1]; |
---|
951 | |
---|
952 | if (m_signLineBuf2) |
---|
953 | { |
---|
954 | delete[] m_signLineBuf2; |
---|
955 | m_signLineBuf2 = NULL; |
---|
956 | } |
---|
957 | m_signLineBuf2 = new Char[m_lineBufWidth+1]; |
---|
958 | } |
---|
959 | |
---|
960 | Int x,y, startX, startY, endX, endY, edgeType, firstLineStartX, firstLineEndX; |
---|
961 | Char signLeft, signRight, signDown; |
---|
962 | Int64 *diff, *count; |
---|
963 | Pel *srcLine, *orgLine; |
---|
964 | Int* skipLinesR = m_skipLinesR[compIdx]; |
---|
965 | Int* skipLinesB = m_skipLinesB[compIdx]; |
---|
966 | |
---|
967 | for(Int typeIdx=0; typeIdx< NUM_SAO_NEW_TYPES; typeIdx++) |
---|
968 | { |
---|
969 | SAOStatData& statsData= statsDataTypes[typeIdx]; |
---|
970 | statsData.reset(); |
---|
971 | |
---|
972 | srcLine = srcBlk; |
---|
973 | orgLine = orgBlk; |
---|
974 | diff = statsData.diff; |
---|
975 | count = statsData.count; |
---|
976 | switch(typeIdx) |
---|
977 | { |
---|
978 | case SAO_TYPE_EO_0: |
---|
979 | { |
---|
980 | diff +=2; |
---|
981 | count+=2; |
---|
982 | endY = (isBelowAvail) ? (height - skipLinesB[typeIdx]) : height; |
---|
983 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
984 | startX = (!isCalculatePreDeblockSamples) ? (isLeftAvail ? 0 : 1) |
---|
985 | : (isRightAvail ? (width - skipLinesR[typeIdx]) : (width - 1)) |
---|
986 | ; |
---|
987 | #else |
---|
988 | startX = isLeftAvail ? 0 : 1; |
---|
989 | #endif |
---|
990 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
991 | endX = (!isCalculatePreDeblockSamples) ? (isRightAvail ? (width - skipLinesR[typeIdx]) : (width - 1)) |
---|
992 | : (isRightAvail ? width : (width - 1)) |
---|
993 | ; |
---|
994 | #else |
---|
995 | endX = isRightAvail ? (width - skipLinesR[typeIdx]): (width - 1); |
---|
996 | #endif |
---|
997 | for (y=0; y<endY; y++) |
---|
998 | { |
---|
999 | signLeft = (Char)sgn(srcLine[startX] - srcLine[startX-1]); |
---|
1000 | for (x=startX; x<endX; x++) |
---|
1001 | { |
---|
1002 | signRight = (Char)sgn(srcLine[x] - srcLine[x+1]); |
---|
1003 | edgeType = signRight + signLeft; |
---|
1004 | signLeft = -signRight; |
---|
1005 | |
---|
1006 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
1007 | count[edgeType] ++; |
---|
1008 | } |
---|
1009 | srcLine += srcStride; |
---|
1010 | orgLine += orgStride; |
---|
1011 | } |
---|
1012 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1013 | if(isCalculatePreDeblockSamples) |
---|
1014 | { |
---|
1015 | if(isBelowAvail) |
---|
1016 | { |
---|
1017 | startX = isLeftAvail ? 0 : 1; |
---|
1018 | endX = isRightAvail ? width : (width -1); |
---|
1019 | |
---|
1020 | for(y=0; y<skipLinesB[typeIdx]; y++) |
---|
1021 | { |
---|
1022 | signLeft = (Char)sgn(srcLine[startX] - srcLine[startX-1]); |
---|
1023 | for (x=startX; x<endX; x++) |
---|
1024 | { |
---|
1025 | signRight = (Char)sgn(srcLine[x] - srcLine[x+1]); |
---|
1026 | edgeType = signRight + signLeft; |
---|
1027 | signLeft = -signRight; |
---|
1028 | |
---|
1029 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
1030 | count[edgeType] ++; |
---|
1031 | } |
---|
1032 | srcLine += srcStride; |
---|
1033 | orgLine += orgStride; |
---|
1034 | } |
---|
1035 | } |
---|
1036 | } |
---|
1037 | #endif |
---|
1038 | } |
---|
1039 | break; |
---|
1040 | case SAO_TYPE_EO_90: |
---|
1041 | { |
---|
1042 | diff +=2; |
---|
1043 | count+=2; |
---|
1044 | Char *signUpLine = m_signLineBuf1; |
---|
1045 | |
---|
1046 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1047 | startX = (!isCalculatePreDeblockSamples) ? 0 |
---|
1048 | : (isRightAvail ? (width - skipLinesR[typeIdx]) : width) |
---|
1049 | ; |
---|
1050 | #endif |
---|
1051 | startY = isAboveAvail ? 0 : 1; |
---|
1052 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1053 | endX = (!isCalculatePreDeblockSamples) ? (isRightAvail ? (width - skipLinesR[typeIdx]) : width) |
---|
1054 | : width |
---|
1055 | ; |
---|
1056 | #else |
---|
1057 | endX = isRightAvail ? (width - skipLinesR[typeIdx]) : width ; |
---|
1058 | #endif |
---|
1059 | endY = isBelowAvail ? (height - skipLinesB[typeIdx]) : (height - 1); |
---|
1060 | if (!isAboveAvail) |
---|
1061 | { |
---|
1062 | srcLine += srcStride; |
---|
1063 | orgLine += orgStride; |
---|
1064 | } |
---|
1065 | |
---|
1066 | Pel* srcLineAbove = srcLine - srcStride; |
---|
1067 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1068 | for (x=startX; x<endX; x++) |
---|
1069 | #else |
---|
1070 | for (x=0; x< endX; x++) |
---|
1071 | #endif |
---|
1072 | { |
---|
1073 | signUpLine[x] = (Char)sgn(srcLine[x] - srcLineAbove[x]); |
---|
1074 | } |
---|
1075 | |
---|
1076 | Pel* srcLineBelow; |
---|
1077 | for (y=startY; y<endY; y++) |
---|
1078 | { |
---|
1079 | srcLineBelow = srcLine + srcStride; |
---|
1080 | |
---|
1081 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1082 | for (x=startX; x<endX; x++) |
---|
1083 | #else |
---|
1084 | for (x=0; x<endX; x++) |
---|
1085 | #endif |
---|
1086 | { |
---|
1087 | signDown = (Char)sgn(srcLine[x] - srcLineBelow[x]); |
---|
1088 | edgeType = signDown + signUpLine[x]; |
---|
1089 | signUpLine[x]= -signDown; |
---|
1090 | |
---|
1091 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
1092 | count[edgeType] ++; |
---|
1093 | } |
---|
1094 | srcLine += srcStride; |
---|
1095 | orgLine += orgStride; |
---|
1096 | } |
---|
1097 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1098 | if(isCalculatePreDeblockSamples) |
---|
1099 | { |
---|
1100 | if(isBelowAvail) |
---|
1101 | { |
---|
1102 | startX = 0; |
---|
1103 | endX = width; |
---|
1104 | |
---|
1105 | for(y=0; y<skipLinesB[typeIdx]; y++) |
---|
1106 | { |
---|
1107 | srcLineBelow = srcLine + srcStride; |
---|
1108 | srcLineAbove = srcLine - srcStride; |
---|
1109 | |
---|
1110 | for (x=startX; x<endX; x++) |
---|
1111 | { |
---|
1112 | edgeType = sgn(srcLine[x] - srcLineBelow[x]) + sgn(srcLine[x] - srcLineAbove[x]); |
---|
1113 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
1114 | count[edgeType] ++; |
---|
1115 | } |
---|
1116 | srcLine += srcStride; |
---|
1117 | orgLine += orgStride; |
---|
1118 | } |
---|
1119 | } |
---|
1120 | } |
---|
1121 | #endif |
---|
1122 | |
---|
1123 | } |
---|
1124 | break; |
---|
1125 | case SAO_TYPE_EO_135: |
---|
1126 | { |
---|
1127 | diff +=2; |
---|
1128 | count+=2; |
---|
1129 | Char *signUpLine, *signDownLine, *signTmpLine; |
---|
1130 | |
---|
1131 | signUpLine = m_signLineBuf1; |
---|
1132 | signDownLine= m_signLineBuf2; |
---|
1133 | |
---|
1134 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1135 | startX = (!isCalculatePreDeblockSamples) ? (isLeftAvail ? 0 : 1) |
---|
1136 | : (isRightAvail ? (width - skipLinesR[typeIdx]) : (width - 1)) |
---|
1137 | ; |
---|
1138 | #else |
---|
1139 | startX = isLeftAvail ? 0 : 1 ; |
---|
1140 | #endif |
---|
1141 | |
---|
1142 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1143 | endX = (!isCalculatePreDeblockSamples) ? (isRightAvail ? (width - skipLinesR[typeIdx]): (width - 1)) |
---|
1144 | : (isRightAvail ? width : (width - 1)) |
---|
1145 | ; |
---|
1146 | #else |
---|
1147 | endX = isRightAvail ? (width - skipLinesR[typeIdx]): (width - 1); |
---|
1148 | #endif |
---|
1149 | endY = isBelowAvail ? (height - skipLinesB[typeIdx]) : (height - 1); |
---|
1150 | |
---|
1151 | //prepare 2nd line's upper sign |
---|
1152 | Pel* srcLineBelow = srcLine + srcStride; |
---|
1153 | for (x=startX; x<endX+1; x++) |
---|
1154 | { |
---|
1155 | signUpLine[x] = (Char)sgn(srcLineBelow[x] - srcLine[x-1]); |
---|
1156 | } |
---|
1157 | |
---|
1158 | //1st line |
---|
1159 | Pel* srcLineAbove = srcLine - srcStride; |
---|
1160 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1161 | firstLineStartX = (!isCalculatePreDeblockSamples) ? (isAboveLeftAvail ? 0 : 1) : startX; |
---|
1162 | firstLineEndX = (!isCalculatePreDeblockSamples) ? (isAboveAvail ? endX : 1) : endX; |
---|
1163 | #else |
---|
1164 | firstLineStartX = isAboveLeftAvail ? 0 : 1; |
---|
1165 | firstLineEndX = isAboveAvail ? endX : 1; |
---|
1166 | #endif |
---|
1167 | for(x=firstLineStartX; x<firstLineEndX; x++) |
---|
1168 | { |
---|
1169 | edgeType = sgn(srcLine[x] - srcLineAbove[x-1]) - signUpLine[x+1]; |
---|
1170 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
1171 | count[edgeType] ++; |
---|
1172 | } |
---|
1173 | srcLine += srcStride; |
---|
1174 | orgLine += orgStride; |
---|
1175 | |
---|
1176 | |
---|
1177 | //middle lines |
---|
1178 | for (y=1; y<endY; y++) |
---|
1179 | { |
---|
1180 | srcLineBelow = srcLine + srcStride; |
---|
1181 | |
---|
1182 | for (x=startX; x<endX; x++) |
---|
1183 | { |
---|
1184 | signDown = (Char)sgn(srcLine[x] - srcLineBelow[x+1]); |
---|
1185 | edgeType = signDown + signUpLine[x]; |
---|
1186 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
1187 | count[edgeType] ++; |
---|
1188 | |
---|
1189 | signDownLine[x+1] = -signDown; |
---|
1190 | } |
---|
1191 | signDownLine[startX] = (Char)sgn(srcLineBelow[startX] - srcLine[startX-1]); |
---|
1192 | |
---|
1193 | signTmpLine = signUpLine; |
---|
1194 | signUpLine = signDownLine; |
---|
1195 | signDownLine = signTmpLine; |
---|
1196 | |
---|
1197 | srcLine += srcStride; |
---|
1198 | orgLine += orgStride; |
---|
1199 | } |
---|
1200 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1201 | if(isCalculatePreDeblockSamples) |
---|
1202 | { |
---|
1203 | if(isBelowAvail) |
---|
1204 | { |
---|
1205 | startX = isLeftAvail ? 0 : 1 ; |
---|
1206 | endX = isRightAvail ? width : (width -1); |
---|
1207 | |
---|
1208 | for(y=0; y<skipLinesB[typeIdx]; y++) |
---|
1209 | { |
---|
1210 | srcLineBelow = srcLine + srcStride; |
---|
1211 | srcLineAbove = srcLine - srcStride; |
---|
1212 | |
---|
1213 | for (x=startX; x< endX; x++) |
---|
1214 | { |
---|
1215 | edgeType = sgn(srcLine[x] - srcLineBelow[x+1]) + sgn(srcLine[x] - srcLineAbove[x-1]); |
---|
1216 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
1217 | count[edgeType] ++; |
---|
1218 | } |
---|
1219 | srcLine += srcStride; |
---|
1220 | orgLine += orgStride; |
---|
1221 | } |
---|
1222 | } |
---|
1223 | } |
---|
1224 | #endif |
---|
1225 | } |
---|
1226 | break; |
---|
1227 | case SAO_TYPE_EO_45: |
---|
1228 | { |
---|
1229 | diff +=2; |
---|
1230 | count+=2; |
---|
1231 | Char *signUpLine = m_signLineBuf1+1; |
---|
1232 | |
---|
1233 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1234 | startX = (!isCalculatePreDeblockSamples) ? (isLeftAvail ? 0 : 1) |
---|
1235 | : (isRightAvail ? (width - skipLinesR[typeIdx]) : (width - 1)) |
---|
1236 | ; |
---|
1237 | #else |
---|
1238 | startX = isLeftAvail ? 0 : 1; |
---|
1239 | #endif |
---|
1240 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1241 | endX = (!isCalculatePreDeblockSamples) ? (isRightAvail ? (width - skipLinesR[typeIdx]) : (width - 1)) |
---|
1242 | : (isRightAvail ? width : (width - 1)) |
---|
1243 | ; |
---|
1244 | #else |
---|
1245 | endX = isRightAvail ? (width - skipLinesR[typeIdx]) : (width - 1); |
---|
1246 | #endif |
---|
1247 | endY = isBelowAvail ? (height - skipLinesB[typeIdx]) : (height - 1); |
---|
1248 | |
---|
1249 | //prepare 2nd line upper sign |
---|
1250 | Pel* srcLineBelow = srcLine + srcStride; |
---|
1251 | for (x=startX-1; x<endX; x++) |
---|
1252 | { |
---|
1253 | signUpLine[x] = (Char)sgn(srcLineBelow[x] - srcLine[x+1]); |
---|
1254 | } |
---|
1255 | |
---|
1256 | |
---|
1257 | //first line |
---|
1258 | Pel* srcLineAbove = srcLine - srcStride; |
---|
1259 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1260 | firstLineStartX = (!isCalculatePreDeblockSamples) ? (isAboveAvail ? startX : endX) |
---|
1261 | : startX |
---|
1262 | ; |
---|
1263 | firstLineEndX = (!isCalculatePreDeblockSamples) ? ((!isRightAvail && isAboveRightAvail) ? width : endX) |
---|
1264 | : endX |
---|
1265 | ; |
---|
1266 | #else |
---|
1267 | firstLineStartX = isAboveAvail ? startX : endX; |
---|
1268 | firstLineEndX = (!isRightAvail && isAboveRightAvail) ? width : endX; |
---|
1269 | #endif |
---|
1270 | for(x=firstLineStartX; x<firstLineEndX; x++) |
---|
1271 | { |
---|
1272 | edgeType = sgn(srcLine[x] - srcLineAbove[x+1]) - signUpLine[x-1]; |
---|
1273 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
1274 | count[edgeType] ++; |
---|
1275 | } |
---|
1276 | |
---|
1277 | srcLine += srcStride; |
---|
1278 | orgLine += orgStride; |
---|
1279 | |
---|
1280 | //middle lines |
---|
1281 | for (y=1; y<endY; y++) |
---|
1282 | { |
---|
1283 | srcLineBelow = srcLine + srcStride; |
---|
1284 | |
---|
1285 | for(x=startX; x<endX; x++) |
---|
1286 | { |
---|
1287 | signDown = (Char)sgn(srcLine[x] - srcLineBelow[x-1]); |
---|
1288 | edgeType = signDown + signUpLine[x]; |
---|
1289 | |
---|
1290 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
1291 | count[edgeType] ++; |
---|
1292 | |
---|
1293 | signUpLine[x-1] = -signDown; |
---|
1294 | } |
---|
1295 | signUpLine[endX-1] = (Char)sgn(srcLineBelow[endX-1] - srcLine[endX]); |
---|
1296 | srcLine += srcStride; |
---|
1297 | orgLine += orgStride; |
---|
1298 | } |
---|
1299 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1300 | if(isCalculatePreDeblockSamples) |
---|
1301 | { |
---|
1302 | if(isBelowAvail) |
---|
1303 | { |
---|
1304 | startX = isLeftAvail ? 0 : 1 ; |
---|
1305 | endX = isRightAvail ? width : (width -1); |
---|
1306 | |
---|
1307 | for(y=0; y<skipLinesB[typeIdx]; y++) |
---|
1308 | { |
---|
1309 | srcLineBelow = srcLine + srcStride; |
---|
1310 | srcLineAbove = srcLine - srcStride; |
---|
1311 | |
---|
1312 | for (x=startX; x<endX; x++) |
---|
1313 | { |
---|
1314 | edgeType = sgn(srcLine[x] - srcLineBelow[x-1]) + sgn(srcLine[x] - srcLineAbove[x+1]); |
---|
1315 | diff [edgeType] += (orgLine[x] - srcLine[x]); |
---|
1316 | count[edgeType] ++; |
---|
1317 | } |
---|
1318 | srcLine += srcStride; |
---|
1319 | orgLine += orgStride; |
---|
1320 | } |
---|
1321 | } |
---|
1322 | } |
---|
1323 | #endif |
---|
1324 | } |
---|
1325 | break; |
---|
1326 | case SAO_TYPE_BO: |
---|
1327 | { |
---|
1328 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1329 | startX = (!isCalculatePreDeblockSamples)?0 |
---|
1330 | :( isRightAvail?(width- skipLinesR[typeIdx]):width) |
---|
1331 | ; |
---|
1332 | endX = (!isCalculatePreDeblockSamples)?(isRightAvail ? (width - skipLinesR[typeIdx]) : width ) |
---|
1333 | :width |
---|
1334 | ; |
---|
1335 | #else |
---|
1336 | endX = isRightAvail ? (width- skipLinesR[typeIdx]) : width; |
---|
1337 | #endif |
---|
1338 | endY = isBelowAvail ? (height- skipLinesB[typeIdx]) : height; |
---|
1339 | Int shiftBits = channelBitDepth - NUM_SAO_BO_CLASSES_LOG2; |
---|
1340 | for (y=0; y< endY; y++) |
---|
1341 | { |
---|
1342 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1343 | for (x=startX; x< endX; x++) |
---|
1344 | #else |
---|
1345 | for (x=0; x< endX; x++) |
---|
1346 | #endif |
---|
1347 | { |
---|
1348 | |
---|
1349 | Int bandIdx= srcLine[x] >> shiftBits; |
---|
1350 | diff [bandIdx] += (orgLine[x] - srcLine[x]); |
---|
1351 | count[bandIdx] ++; |
---|
1352 | } |
---|
1353 | srcLine += srcStride; |
---|
1354 | orgLine += orgStride; |
---|
1355 | } |
---|
1356 | #if SAO_ENCODE_ALLOW_USE_PREDEBLOCK |
---|
1357 | if(isCalculatePreDeblockSamples) |
---|
1358 | { |
---|
1359 | if(isBelowAvail) |
---|
1360 | { |
---|
1361 | startX = 0; |
---|
1362 | endX = width; |
---|
1363 | |
---|
1364 | for(y= 0; y< skipLinesB[typeIdx]; y++) |
---|
1365 | { |
---|
1366 | for (x=startX; x< endX; x++) |
---|
1367 | { |
---|
1368 | Int bandIdx= srcLine[x] >> shiftBits; |
---|
1369 | diff [bandIdx] += (orgLine[x] - srcLine[x]); |
---|
1370 | count[bandIdx] ++; |
---|
1371 | } |
---|
1372 | srcLine += srcStride; |
---|
1373 | orgLine += orgStride; |
---|
1374 | |
---|
1375 | } |
---|
1376 | |
---|
1377 | } |
---|
1378 | } |
---|
1379 | #endif |
---|
1380 | } |
---|
1381 | break; |
---|
1382 | default: |
---|
1383 | { |
---|
1384 | printf("Not a supported SAO types\n"); |
---|
1385 | assert(0); |
---|
1386 | exit(-1); |
---|
1387 | } |
---|
1388 | } |
---|
1389 | } |
---|
1390 | } |
---|
1391 | |
---|
1392 | |
---|
1393 | //! \} |
---|