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