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 | /** \file TComSampleAdaptiveOffset.cpp |
---|
35 | \brief sample adaptive offset class |
---|
36 | */ |
---|
37 | |
---|
38 | #include "TComSampleAdaptiveOffset.h" |
---|
39 | #include <string.h> |
---|
40 | #include <stdlib.h> |
---|
41 | #include <stdio.h> |
---|
42 | #include <math.h> |
---|
43 | |
---|
44 | //! \ingroup TLibCommon |
---|
45 | //! \{ |
---|
46 | |
---|
47 | SAOOffset::SAOOffset() |
---|
48 | { |
---|
49 | reset(); |
---|
50 | } |
---|
51 | |
---|
52 | SAOOffset::~SAOOffset() |
---|
53 | { |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | Void SAOOffset::reset() |
---|
58 | { |
---|
59 | modeIdc = SAO_MODE_OFF; |
---|
60 | typeIdc = -1; |
---|
61 | typeAuxInfo = -1; |
---|
62 | ::memset(offset, 0, sizeof(Int)* MAX_NUM_SAO_CLASSES); |
---|
63 | } |
---|
64 | |
---|
65 | const SAOOffset& SAOOffset::operator= (const SAOOffset& src) |
---|
66 | { |
---|
67 | modeIdc = src.modeIdc; |
---|
68 | typeIdc = src.typeIdc; |
---|
69 | typeAuxInfo = src.typeAuxInfo; |
---|
70 | ::memcpy(offset, src.offset, sizeof(Int)* MAX_NUM_SAO_CLASSES); |
---|
71 | |
---|
72 | return *this; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | SAOBlkParam::SAOBlkParam() |
---|
77 | { |
---|
78 | reset(); |
---|
79 | } |
---|
80 | |
---|
81 | SAOBlkParam::~SAOBlkParam() |
---|
82 | { |
---|
83 | |
---|
84 | } |
---|
85 | |
---|
86 | Void SAOBlkParam::reset() |
---|
87 | { |
---|
88 | for(Int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
89 | { |
---|
90 | offsetParam[compIdx].reset(); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | const SAOBlkParam& SAOBlkParam::operator= (const SAOBlkParam& src) |
---|
95 | { |
---|
96 | for(Int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
97 | { |
---|
98 | offsetParam[compIdx] = src.offsetParam[compIdx]; |
---|
99 | } |
---|
100 | return *this; |
---|
101 | |
---|
102 | } |
---|
103 | |
---|
104 | TComSampleAdaptiveOffset::TComSampleAdaptiveOffset() |
---|
105 | { |
---|
106 | m_tempPicYuv = NULL; |
---|
107 | m_lineBufWidth = 0; |
---|
108 | m_signLineBuf1 = NULL; |
---|
109 | m_signLineBuf2 = NULL; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | TComSampleAdaptiveOffset::~TComSampleAdaptiveOffset() |
---|
114 | { |
---|
115 | destroy(); |
---|
116 | |
---|
117 | if (m_signLineBuf1) |
---|
118 | { |
---|
119 | delete[] m_signLineBuf1; |
---|
120 | m_signLineBuf1 = NULL; |
---|
121 | } |
---|
122 | if (m_signLineBuf2) |
---|
123 | { |
---|
124 | delete[] m_signLineBuf2; |
---|
125 | m_signLineBuf2 = NULL; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | Void TComSampleAdaptiveOffset::create( Int picWidth, Int picHeight, ChromaFormat format, UInt maxCUWidth, UInt maxCUHeight, UInt maxCUDepth, UInt lumaBitShift, UInt chromaBitShift ) |
---|
130 | { |
---|
131 | destroy(); |
---|
132 | |
---|
133 | m_picWidth = picWidth; |
---|
134 | m_picHeight = picHeight; |
---|
135 | m_chromaFormatIDC = format; |
---|
136 | m_maxCUWidth = maxCUWidth; |
---|
137 | m_maxCUHeight = maxCUHeight; |
---|
138 | |
---|
139 | m_numCTUInWidth = (m_picWidth/m_maxCUWidth) + ((m_picWidth % m_maxCUWidth)?1:0); |
---|
140 | m_numCTUInHeight = (m_picHeight/m_maxCUHeight) + ((m_picHeight % m_maxCUHeight)?1:0); |
---|
141 | m_numCTUsPic = m_numCTUInHeight*m_numCTUInWidth; |
---|
142 | |
---|
143 | //temporary picture buffer |
---|
144 | if ( !m_tempPicYuv ) |
---|
145 | { |
---|
146 | m_tempPicYuv = new TComPicYuv; |
---|
147 | m_tempPicYuv->create( m_picWidth, m_picHeight, m_chromaFormatIDC, m_maxCUWidth, m_maxCUHeight, maxCUDepth, true ); |
---|
148 | } |
---|
149 | |
---|
150 | //bit-depth related |
---|
151 | for(Int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
152 | { |
---|
153 | m_offsetStepLog2 [compIdx] = isLuma(ComponentID(compIdx))? lumaBitShift : chromaBitShift; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | Void TComSampleAdaptiveOffset::destroy() |
---|
158 | { |
---|
159 | if ( m_tempPicYuv ) |
---|
160 | { |
---|
161 | m_tempPicYuv->destroy(); |
---|
162 | delete m_tempPicYuv; |
---|
163 | m_tempPicYuv = NULL; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | Void TComSampleAdaptiveOffset::invertQuantOffsets(ComponentID compIdx, Int typeIdc, Int typeAuxInfo, Int* dstOffsets, Int* srcOffsets) |
---|
168 | { |
---|
169 | Int codedOffset[MAX_NUM_SAO_CLASSES]; |
---|
170 | |
---|
171 | ::memcpy(codedOffset, srcOffsets, sizeof(Int)*MAX_NUM_SAO_CLASSES); |
---|
172 | ::memset(dstOffsets, 0, sizeof(Int)*MAX_NUM_SAO_CLASSES); |
---|
173 | |
---|
174 | if(typeIdc == SAO_TYPE_START_BO) |
---|
175 | { |
---|
176 | for(Int i=0; i< 4; i++) |
---|
177 | { |
---|
178 | dstOffsets[(typeAuxInfo+ i)%NUM_SAO_BO_CLASSES] = codedOffset[(typeAuxInfo+ i)%NUM_SAO_BO_CLASSES]*(1<<m_offsetStepLog2[compIdx]); |
---|
179 | } |
---|
180 | } |
---|
181 | else //EO |
---|
182 | { |
---|
183 | for(Int i=0; i< NUM_SAO_EO_CLASSES; i++) |
---|
184 | { |
---|
185 | dstOffsets[i] = codedOffset[i] *(1<<m_offsetStepLog2[compIdx]); |
---|
186 | } |
---|
187 | assert(dstOffsets[SAO_CLASS_EO_PLAIN] == 0); //keep EO plain offset as zero |
---|
188 | } |
---|
189 | |
---|
190 | } |
---|
191 | |
---|
192 | Int TComSampleAdaptiveOffset::getMergeList(TComPic* pic, Int ctuRsAddr, SAOBlkParam* blkParams, SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES]) |
---|
193 | { |
---|
194 | Int ctuX = ctuRsAddr % m_numCTUInWidth; |
---|
195 | Int ctuY = ctuRsAddr / m_numCTUInWidth; |
---|
196 | Int mergedCTUPos; |
---|
197 | Int numValidMergeCandidates = 0; |
---|
198 | |
---|
199 | for(Int mergeType=0; mergeType< NUM_SAO_MERGE_TYPES; mergeType++) |
---|
200 | { |
---|
201 | SAOBlkParam* mergeCandidate = NULL; |
---|
202 | |
---|
203 | switch(mergeType) |
---|
204 | { |
---|
205 | case SAO_MERGE_ABOVE: |
---|
206 | { |
---|
207 | if(ctuY > 0) |
---|
208 | { |
---|
209 | mergedCTUPos = ctuRsAddr- m_numCTUInWidth; |
---|
210 | if( pic->getSAOMergeAvailability(ctuRsAddr, mergedCTUPos) ) |
---|
211 | { |
---|
212 | mergeCandidate = &(blkParams[mergedCTUPos]); |
---|
213 | } |
---|
214 | } |
---|
215 | } |
---|
216 | break; |
---|
217 | case SAO_MERGE_LEFT: |
---|
218 | { |
---|
219 | if(ctuX > 0) |
---|
220 | { |
---|
221 | mergedCTUPos = ctuRsAddr- 1; |
---|
222 | if( pic->getSAOMergeAvailability(ctuRsAddr, mergedCTUPos) ) |
---|
223 | { |
---|
224 | mergeCandidate = &(blkParams[mergedCTUPos]); |
---|
225 | } |
---|
226 | } |
---|
227 | } |
---|
228 | break; |
---|
229 | default: |
---|
230 | { |
---|
231 | printf("not a supported merge type"); |
---|
232 | assert(0); |
---|
233 | exit(-1); |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | mergeList[mergeType]=mergeCandidate; |
---|
238 | if (mergeCandidate != NULL) |
---|
239 | { |
---|
240 | numValidMergeCandidates++; |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | return numValidMergeCandidates; |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | Void TComSampleAdaptiveOffset::reconstructBlkSAOParam(SAOBlkParam& recParam, SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES]) |
---|
249 | { |
---|
250 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
251 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
252 | { |
---|
253 | const ComponentID component = ComponentID(compIdx); |
---|
254 | SAOOffset& offsetParam = recParam[component]; |
---|
255 | |
---|
256 | if(offsetParam.modeIdc == SAO_MODE_OFF) |
---|
257 | { |
---|
258 | continue; |
---|
259 | } |
---|
260 | |
---|
261 | switch(offsetParam.modeIdc) |
---|
262 | { |
---|
263 | case SAO_MODE_NEW: |
---|
264 | { |
---|
265 | invertQuantOffsets(component, offsetParam.typeIdc, offsetParam.typeAuxInfo, offsetParam.offset, offsetParam.offset); |
---|
266 | } |
---|
267 | break; |
---|
268 | case SAO_MODE_MERGE: |
---|
269 | { |
---|
270 | SAOBlkParam* mergeTarget = mergeList[offsetParam.typeIdc]; |
---|
271 | assert(mergeTarget != NULL); |
---|
272 | |
---|
273 | offsetParam = (*mergeTarget)[component]; |
---|
274 | } |
---|
275 | break; |
---|
276 | default: |
---|
277 | { |
---|
278 | printf("Not a supported mode"); |
---|
279 | assert(0); |
---|
280 | exit(-1); |
---|
281 | } |
---|
282 | } |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | Void TComSampleAdaptiveOffset::reconstructBlkSAOParams(TComPic* pic, SAOBlkParam* saoBlkParams) |
---|
287 | { |
---|
288 | for(Int compIdx = 0; compIdx < MAX_NUM_COMPONENT; compIdx++) |
---|
289 | { |
---|
290 | m_picSAOEnabled[compIdx] = false; |
---|
291 | } |
---|
292 | |
---|
293 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
294 | |
---|
295 | for(Int ctuRsAddr=0; ctuRsAddr< m_numCTUsPic; ctuRsAddr++) |
---|
296 | { |
---|
297 | SAOBlkParam* mergeList[NUM_SAO_MERGE_TYPES] = { NULL }; |
---|
298 | getMergeList(pic, ctuRsAddr, saoBlkParams, mergeList); |
---|
299 | |
---|
300 | reconstructBlkSAOParam(saoBlkParams[ctuRsAddr], mergeList); |
---|
301 | |
---|
302 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
303 | { |
---|
304 | if(saoBlkParams[ctuRsAddr][compIdx].modeIdc != SAO_MODE_OFF) |
---|
305 | { |
---|
306 | m_picSAOEnabled[compIdx] = true; |
---|
307 | } |
---|
308 | } |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | |
---|
313 | Void TComSampleAdaptiveOffset::offsetBlock(const Int channelBitDepth, Int typeIdx, Int* offset |
---|
314 | , Pel* srcBlk, Pel* resBlk, Int srcStride, Int resStride, Int width, Int height |
---|
315 | , Bool isLeftAvail, Bool isRightAvail, Bool isAboveAvail, Bool isBelowAvail, Bool isAboveLeftAvail, Bool isAboveRightAvail, Bool isBelowLeftAvail, Bool isBelowRightAvail) |
---|
316 | { |
---|
317 | if(m_lineBufWidth != m_maxCUWidth) |
---|
318 | { |
---|
319 | m_lineBufWidth = m_maxCUWidth; |
---|
320 | |
---|
321 | if (m_signLineBuf1) |
---|
322 | { |
---|
323 | delete[] m_signLineBuf1; |
---|
324 | m_signLineBuf1 = NULL; |
---|
325 | } |
---|
326 | m_signLineBuf1 = new SChar[m_lineBufWidth+1]; |
---|
327 | |
---|
328 | if (m_signLineBuf2) |
---|
329 | { |
---|
330 | delete[] m_signLineBuf2; |
---|
331 | m_signLineBuf2 = NULL; |
---|
332 | } |
---|
333 | m_signLineBuf2 = new SChar[m_lineBufWidth+1]; |
---|
334 | } |
---|
335 | |
---|
336 | const Int maxSampleValueIncl = (1<< channelBitDepth )-1; |
---|
337 | |
---|
338 | Int x,y, startX, startY, endX, endY, edgeType; |
---|
339 | Int firstLineStartX, firstLineEndX, lastLineStartX, lastLineEndX; |
---|
340 | SChar signLeft, signRight, signDown; |
---|
341 | |
---|
342 | Pel* srcLine = srcBlk; |
---|
343 | Pel* resLine = resBlk; |
---|
344 | |
---|
345 | switch(typeIdx) |
---|
346 | { |
---|
347 | case SAO_TYPE_EO_0: |
---|
348 | { |
---|
349 | offset += 2; |
---|
350 | startX = isLeftAvail ? 0 : 1; |
---|
351 | endX = isRightAvail ? width : (width -1); |
---|
352 | for (y=0; y< height; y++) |
---|
353 | { |
---|
354 | signLeft = (SChar)sgn(srcLine[startX] - srcLine[startX-1]); |
---|
355 | for (x=startX; x< endX; x++) |
---|
356 | { |
---|
357 | signRight = (SChar)sgn(srcLine[x] - srcLine[x+1]); |
---|
358 | edgeType = signRight + signLeft; |
---|
359 | signLeft = -signRight; |
---|
360 | |
---|
361 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
362 | } |
---|
363 | srcLine += srcStride; |
---|
364 | resLine += resStride; |
---|
365 | } |
---|
366 | |
---|
367 | } |
---|
368 | break; |
---|
369 | case SAO_TYPE_EO_90: |
---|
370 | { |
---|
371 | offset += 2; |
---|
372 | SChar *signUpLine = m_signLineBuf1; |
---|
373 | |
---|
374 | startY = isAboveAvail ? 0 : 1; |
---|
375 | endY = isBelowAvail ? height : height-1; |
---|
376 | if (!isAboveAvail) |
---|
377 | { |
---|
378 | srcLine += srcStride; |
---|
379 | resLine += resStride; |
---|
380 | } |
---|
381 | |
---|
382 | Pel* srcLineAbove= srcLine- srcStride; |
---|
383 | for (x=0; x< width; x++) |
---|
384 | { |
---|
385 | signUpLine[x] = (SChar)sgn(srcLine[x] - srcLineAbove[x]); |
---|
386 | } |
---|
387 | |
---|
388 | Pel* srcLineBelow; |
---|
389 | for (y=startY; y<endY; y++) |
---|
390 | { |
---|
391 | srcLineBelow= srcLine+ srcStride; |
---|
392 | |
---|
393 | for (x=0; x< width; x++) |
---|
394 | { |
---|
395 | signDown = (SChar)sgn(srcLine[x] - srcLineBelow[x]); |
---|
396 | edgeType = signDown + signUpLine[x]; |
---|
397 | signUpLine[x]= -signDown; |
---|
398 | |
---|
399 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
400 | } |
---|
401 | srcLine += srcStride; |
---|
402 | resLine += resStride; |
---|
403 | } |
---|
404 | |
---|
405 | } |
---|
406 | break; |
---|
407 | case SAO_TYPE_EO_135: |
---|
408 | { |
---|
409 | offset += 2; |
---|
410 | SChar *signUpLine, *signDownLine, *signTmpLine; |
---|
411 | |
---|
412 | signUpLine = m_signLineBuf1; |
---|
413 | signDownLine= m_signLineBuf2; |
---|
414 | |
---|
415 | startX = isLeftAvail ? 0 : 1 ; |
---|
416 | endX = isRightAvail ? width : (width-1); |
---|
417 | |
---|
418 | //prepare 2nd line's upper sign |
---|
419 | Pel* srcLineBelow= srcLine+ srcStride; |
---|
420 | for (x=startX; x< endX+1; x++) |
---|
421 | { |
---|
422 | signUpLine[x] = (SChar)sgn(srcLineBelow[x] - srcLine[x- 1]); |
---|
423 | } |
---|
424 | |
---|
425 | //1st line |
---|
426 | Pel* srcLineAbove= srcLine- srcStride; |
---|
427 | firstLineStartX = isAboveLeftAvail ? 0 : 1; |
---|
428 | firstLineEndX = isAboveAvail? endX: 1; |
---|
429 | for(x= firstLineStartX; x< firstLineEndX; x++) |
---|
430 | { |
---|
431 | edgeType = sgn(srcLine[x] - srcLineAbove[x- 1]) - signUpLine[x+1]; |
---|
432 | |
---|
433 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
434 | } |
---|
435 | srcLine += srcStride; |
---|
436 | resLine += resStride; |
---|
437 | |
---|
438 | |
---|
439 | //middle lines |
---|
440 | for (y= 1; y< height-1; y++) |
---|
441 | { |
---|
442 | srcLineBelow= srcLine+ srcStride; |
---|
443 | |
---|
444 | for (x=startX; x<endX; x++) |
---|
445 | { |
---|
446 | signDown = (SChar)sgn(srcLine[x] - srcLineBelow[x+ 1]); |
---|
447 | edgeType = signDown + signUpLine[x]; |
---|
448 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
449 | |
---|
450 | signDownLine[x+1] = -signDown; |
---|
451 | } |
---|
452 | signDownLine[startX] = (SChar)sgn(srcLineBelow[startX] - srcLine[startX-1]); |
---|
453 | |
---|
454 | signTmpLine = signUpLine; |
---|
455 | signUpLine = signDownLine; |
---|
456 | signDownLine = signTmpLine; |
---|
457 | |
---|
458 | srcLine += srcStride; |
---|
459 | resLine += resStride; |
---|
460 | } |
---|
461 | |
---|
462 | //last line |
---|
463 | srcLineBelow= srcLine+ srcStride; |
---|
464 | lastLineStartX = isBelowAvail ? startX : (width -1); |
---|
465 | lastLineEndX = isBelowRightAvail ? width : (width -1); |
---|
466 | for(x= lastLineStartX; x< lastLineEndX; x++) |
---|
467 | { |
---|
468 | edgeType = sgn(srcLine[x] - srcLineBelow[x+ 1]) + signUpLine[x]; |
---|
469 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
470 | |
---|
471 | } |
---|
472 | } |
---|
473 | break; |
---|
474 | case SAO_TYPE_EO_45: |
---|
475 | { |
---|
476 | offset += 2; |
---|
477 | SChar *signUpLine = m_signLineBuf1+1; |
---|
478 | |
---|
479 | startX = isLeftAvail ? 0 : 1; |
---|
480 | endX = isRightAvail ? width : (width -1); |
---|
481 | |
---|
482 | //prepare 2nd line upper sign |
---|
483 | Pel* srcLineBelow= srcLine+ srcStride; |
---|
484 | for (x=startX-1; x< endX; x++) |
---|
485 | { |
---|
486 | signUpLine[x] = (SChar)sgn(srcLineBelow[x] - srcLine[x+1]); |
---|
487 | } |
---|
488 | |
---|
489 | |
---|
490 | //first line |
---|
491 | Pel* srcLineAbove= srcLine- srcStride; |
---|
492 | firstLineStartX = isAboveAvail ? startX : (width -1 ); |
---|
493 | firstLineEndX = isAboveRightAvail ? width : (width-1); |
---|
494 | for(x= firstLineStartX; x< firstLineEndX; x++) |
---|
495 | { |
---|
496 | edgeType = sgn(srcLine[x] - srcLineAbove[x+1]) -signUpLine[x-1]; |
---|
497 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
498 | } |
---|
499 | srcLine += srcStride; |
---|
500 | resLine += resStride; |
---|
501 | |
---|
502 | //middle lines |
---|
503 | for (y= 1; y< height-1; y++) |
---|
504 | { |
---|
505 | srcLineBelow= srcLine+ srcStride; |
---|
506 | |
---|
507 | for(x= startX; x< endX; x++) |
---|
508 | { |
---|
509 | signDown = (SChar)sgn(srcLine[x] - srcLineBelow[x-1]); |
---|
510 | edgeType = signDown + signUpLine[x]; |
---|
511 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
512 | signUpLine[x-1] = -signDown; |
---|
513 | } |
---|
514 | signUpLine[endX-1] = (SChar)sgn(srcLineBelow[endX-1] - srcLine[endX]); |
---|
515 | srcLine += srcStride; |
---|
516 | resLine += resStride; |
---|
517 | } |
---|
518 | |
---|
519 | //last line |
---|
520 | srcLineBelow= srcLine+ srcStride; |
---|
521 | lastLineStartX = isBelowLeftAvail ? 0 : 1; |
---|
522 | lastLineEndX = isBelowAvail ? endX : 1; |
---|
523 | for(x= lastLineStartX; x< lastLineEndX; x++) |
---|
524 | { |
---|
525 | edgeType = sgn(srcLine[x] - srcLineBelow[x-1]) + signUpLine[x]; |
---|
526 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[edgeType]); |
---|
527 | |
---|
528 | } |
---|
529 | } |
---|
530 | break; |
---|
531 | case SAO_TYPE_BO: |
---|
532 | { |
---|
533 | const Int shiftBits = channelBitDepth - NUM_SAO_BO_CLASSES_LOG2; |
---|
534 | for (y=0; y< height; y++) |
---|
535 | { |
---|
536 | for (x=0; x< width; x++) |
---|
537 | { |
---|
538 | resLine[x] = Clip3<Int>(0, maxSampleValueIncl, srcLine[x] + offset[srcLine[x] >> shiftBits] ); |
---|
539 | } |
---|
540 | srcLine += srcStride; |
---|
541 | resLine += resStride; |
---|
542 | } |
---|
543 | } |
---|
544 | break; |
---|
545 | default: |
---|
546 | { |
---|
547 | printf("Not a supported SAO types\n"); |
---|
548 | assert(0); |
---|
549 | exit(-1); |
---|
550 | } |
---|
551 | } |
---|
552 | } |
---|
553 | |
---|
554 | Void TComSampleAdaptiveOffset::offsetCTU(Int ctuRsAddr, TComPicYuv* srcYuv, TComPicYuv* resYuv, SAOBlkParam& saoblkParam, TComPic* pPic) |
---|
555 | { |
---|
556 | Bool isLeftAvail,isRightAvail,isAboveAvail,isBelowAvail,isAboveLeftAvail,isAboveRightAvail,isBelowLeftAvail,isBelowRightAvail; |
---|
557 | |
---|
558 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
559 | Bool bAllOff=true; |
---|
560 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
561 | { |
---|
562 | if (saoblkParam[compIdx].modeIdc != SAO_MODE_OFF) |
---|
563 | { |
---|
564 | bAllOff=false; |
---|
565 | } |
---|
566 | } |
---|
567 | if (bAllOff) |
---|
568 | { |
---|
569 | return; |
---|
570 | } |
---|
571 | |
---|
572 | //block boundary availability |
---|
573 | pPic->getPicSym()->deriveLoopFilterBoundaryAvailibility(ctuRsAddr, isLeftAvail,isRightAvail,isAboveAvail,isBelowAvail,isAboveLeftAvail,isAboveRightAvail,isBelowLeftAvail,isBelowRightAvail); |
---|
574 | |
---|
575 | Int yPos = (ctuRsAddr / m_numCTUInWidth)*m_maxCUHeight; |
---|
576 | Int xPos = (ctuRsAddr % m_numCTUInWidth)*m_maxCUWidth; |
---|
577 | Int height = (yPos + m_maxCUHeight > m_picHeight)?(m_picHeight- yPos):m_maxCUHeight; |
---|
578 | Int width = (xPos + m_maxCUWidth > m_picWidth )?(m_picWidth - xPos):m_maxCUWidth; |
---|
579 | |
---|
580 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
581 | { |
---|
582 | const ComponentID component = ComponentID(compIdx); |
---|
583 | SAOOffset& ctbOffset = saoblkParam[compIdx]; |
---|
584 | |
---|
585 | if(ctbOffset.modeIdc != SAO_MODE_OFF) |
---|
586 | { |
---|
587 | const UInt componentScaleX = getComponentScaleX(component, pPic->getChromaFormat()); |
---|
588 | const UInt componentScaleY = getComponentScaleY(component, pPic->getChromaFormat()); |
---|
589 | |
---|
590 | Int blkWidth = (width >> componentScaleX); |
---|
591 | Int blkHeight = (height >> componentScaleY); |
---|
592 | Int blkXPos = (xPos >> componentScaleX); |
---|
593 | Int blkYPos = (yPos >> componentScaleY); |
---|
594 | |
---|
595 | Int srcStride = srcYuv->getStride(component); |
---|
596 | Pel* srcBlk = srcYuv->getAddr(component) + blkYPos*srcStride + blkXPos; |
---|
597 | |
---|
598 | Int resStride = resYuv->getStride(component); |
---|
599 | Pel* resBlk = resYuv->getAddr(component) + blkYPos*resStride + blkXPos; |
---|
600 | |
---|
601 | #if SVC_EXTENSION |
---|
602 | offsetBlock( pPic->getSlice(0)->getBitDepth(toChannelType(component)), ctbOffset.typeIdc, ctbOffset.offset |
---|
603 | #else |
---|
604 | offsetBlock( pPic->getPicSym()->getSPS().getBitDepth(toChannelType(component)), ctbOffset.typeIdc, ctbOffset.offset |
---|
605 | #endif |
---|
606 | , srcBlk, resBlk, srcStride, resStride, blkWidth, blkHeight |
---|
607 | , isLeftAvail, isRightAvail |
---|
608 | , isAboveAvail, isBelowAvail |
---|
609 | , isAboveLeftAvail, isAboveRightAvail |
---|
610 | , isBelowLeftAvail, isBelowRightAvail |
---|
611 | ); |
---|
612 | } |
---|
613 | } //compIdx |
---|
614 | |
---|
615 | } |
---|
616 | |
---|
617 | |
---|
618 | Void TComSampleAdaptiveOffset::SAOProcess(TComPic* pDecPic) |
---|
619 | { |
---|
620 | const Int numberOfComponents = getNumberValidComponents(m_chromaFormatIDC); |
---|
621 | Bool bAllDisabled=true; |
---|
622 | for(Int compIdx = 0; compIdx < numberOfComponents; compIdx++) |
---|
623 | { |
---|
624 | if (m_picSAOEnabled[compIdx]) |
---|
625 | { |
---|
626 | bAllDisabled=false; |
---|
627 | } |
---|
628 | } |
---|
629 | if (bAllDisabled) |
---|
630 | { |
---|
631 | return; |
---|
632 | } |
---|
633 | |
---|
634 | TComPicYuv* resYuv = pDecPic->getPicYuvRec(); |
---|
635 | TComPicYuv* srcYuv = m_tempPicYuv; |
---|
636 | resYuv->copyToPic(srcYuv); |
---|
637 | for(Int ctuRsAddr= 0; ctuRsAddr < m_numCTUsPic; ctuRsAddr++) |
---|
638 | { |
---|
639 | offsetCTU(ctuRsAddr, srcYuv, resYuv, (pDecPic->getPicSym()->getSAOBlkParam())[ctuRsAddr], pDecPic); |
---|
640 | } //ctu |
---|
641 | } |
---|
642 | |
---|
643 | |
---|
644 | /** PCM LF disable process. |
---|
645 | * \param pcPic picture (TComPic) pointer |
---|
646 | * |
---|
647 | * \note Replace filtered sample values of PCM mode blocks with the transmitted and reconstructed ones. |
---|
648 | */ |
---|
649 | Void TComSampleAdaptiveOffset::PCMLFDisableProcess (TComPic* pcPic) |
---|
650 | { |
---|
651 | xPCMRestoration(pcPic); |
---|
652 | } |
---|
653 | |
---|
654 | /** Picture-level PCM restoration. |
---|
655 | * \param pcPic picture (TComPic) pointer |
---|
656 | */ |
---|
657 | Void TComSampleAdaptiveOffset::xPCMRestoration(TComPic* pcPic) |
---|
658 | { |
---|
659 | Bool bPCMFilter = (pcPic->getSlice(0)->getSPS()->getUsePCM() && pcPic->getSlice(0)->getSPS()->getPCMFilterDisableFlag())? true : false; |
---|
660 | |
---|
661 | if(bPCMFilter || pcPic->getSlice(0)->getPPS()->getTransquantBypassEnableFlag()) |
---|
662 | { |
---|
663 | for( UInt ctuRsAddr = 0; ctuRsAddr < pcPic->getNumberOfCtusInFrame() ; ctuRsAddr++ ) |
---|
664 | { |
---|
665 | TComDataCU* pcCU = pcPic->getCtu(ctuRsAddr); |
---|
666 | |
---|
667 | xPCMCURestoration(pcCU, 0, 0); |
---|
668 | } |
---|
669 | } |
---|
670 | } |
---|
671 | |
---|
672 | /** PCM CU restoration. |
---|
673 | * \param pcCU pointer to current CU |
---|
674 | * \param uiAbsZorderIdx part index |
---|
675 | * \param uiDepth CU depth |
---|
676 | */ |
---|
677 | Void TComSampleAdaptiveOffset::xPCMCURestoration ( TComDataCU* pcCU, UInt uiAbsZorderIdx, UInt uiDepth ) |
---|
678 | { |
---|
679 | TComPic* pcPic = pcCU->getPic(); |
---|
680 | UInt uiCurNumParts = pcPic->getNumPartitionsInCtu() >> (uiDepth<<1); |
---|
681 | UInt uiQNumParts = uiCurNumParts>>2; |
---|
682 | |
---|
683 | // go to sub-CU |
---|
684 | if( pcCU->getDepth(uiAbsZorderIdx) > uiDepth ) |
---|
685 | { |
---|
686 | for ( UInt uiPartIdx = 0; uiPartIdx < 4; uiPartIdx++, uiAbsZorderIdx+=uiQNumParts ) |
---|
687 | { |
---|
688 | UInt uiLPelX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsZorderIdx] ]; |
---|
689 | UInt uiTPelY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsZorderIdx] ]; |
---|
690 | #if SVC_EXTENSION |
---|
691 | if( ( uiLPelX < pcCU->getSlice()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcCU->getSlice()->getPicHeightInLumaSamples() ) ) |
---|
692 | #else |
---|
693 | if( ( uiLPelX < pcCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcCU->getSlice()->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
694 | #endif |
---|
695 | { |
---|
696 | xPCMCURestoration( pcCU, uiAbsZorderIdx, uiDepth+1 ); |
---|
697 | } |
---|
698 | } |
---|
699 | return; |
---|
700 | } |
---|
701 | |
---|
702 | // restore PCM samples |
---|
703 | if ((pcCU->getIPCMFlag(uiAbsZorderIdx)&& pcPic->getSlice(0)->getSPS()->getPCMFilterDisableFlag()) || pcCU->isLosslessCoded( uiAbsZorderIdx)) |
---|
704 | { |
---|
705 | const UInt numComponents=pcPic->getNumberValidComponents(); |
---|
706 | for(UInt comp=0; comp<numComponents; comp++) |
---|
707 | { |
---|
708 | xPCMSampleRestoration (pcCU, uiAbsZorderIdx, uiDepth, ComponentID(comp)); |
---|
709 | } |
---|
710 | } |
---|
711 | } |
---|
712 | |
---|
713 | /** PCM sample restoration. |
---|
714 | * \param pcCU pointer to current CU |
---|
715 | * \param uiAbsZorderIdx part index |
---|
716 | * \param uiDepth CU depth |
---|
717 | * \param compID texture component type |
---|
718 | */ |
---|
719 | Void TComSampleAdaptiveOffset::xPCMSampleRestoration (TComDataCU* pcCU, UInt uiAbsZorderIdx, UInt uiDepth, const ComponentID compID) |
---|
720 | { |
---|
721 | TComPicYuv* pcPicYuvRec = pcCU->getPic()->getPicYuvRec(); |
---|
722 | UInt uiPcmLeftShiftBit; |
---|
723 | const UInt uiMinCoeffSize = pcCU->getPic()->getMinCUWidth()*pcCU->getPic()->getMinCUHeight(); |
---|
724 | const UInt csx=pcPicYuvRec->getComponentScaleX(compID); |
---|
725 | const UInt csy=pcPicYuvRec->getComponentScaleY(compID); |
---|
726 | const UInt uiOffset = (uiMinCoeffSize*uiAbsZorderIdx)>>(csx+csy); |
---|
727 | |
---|
728 | Pel *piSrc = pcPicYuvRec->getAddr(compID, pcCU->getCtuRsAddr(), uiAbsZorderIdx); |
---|
729 | const Pel *piPcm = pcCU->getPCMSample(compID) + uiOffset; |
---|
730 | const UInt uiStride = pcPicYuvRec->getStride(compID); |
---|
731 | const TComSPS &sps = *(pcCU->getSlice()->getSPS()); |
---|
732 | const UInt uiWidth = ((sps.getMaxCUWidth() >> uiDepth) >> csx); |
---|
733 | const UInt uiHeight = ((sps.getMaxCUHeight() >> uiDepth) >> csy); |
---|
734 | |
---|
735 | if ( pcCU->isLosslessCoded(uiAbsZorderIdx) && !pcCU->getIPCMFlag(uiAbsZorderIdx) ) |
---|
736 | { |
---|
737 | uiPcmLeftShiftBit = 0; |
---|
738 | } |
---|
739 | else |
---|
740 | { |
---|
741 | #if SVC_EXTENSION |
---|
742 | uiPcmLeftShiftBit = pcCU->getSlice()->getBitDepth(toChannelType(compID)) - pcCU->getSlice()->getSPS()->getPCMBitDepth(toChannelType(compID)); |
---|
743 | #else |
---|
744 | uiPcmLeftShiftBit = sps.getBitDepth(toChannelType(compID)) - sps.getPCMBitDepth(toChannelType(compID)); |
---|
745 | #endif |
---|
746 | } |
---|
747 | |
---|
748 | for(UInt uiY = 0; uiY < uiHeight; uiY++ ) |
---|
749 | { |
---|
750 | for(UInt uiX = 0; uiX < uiWidth; uiX++ ) |
---|
751 | { |
---|
752 | piSrc[uiX] = (piPcm[uiX] << uiPcmLeftShiftBit); |
---|
753 | } |
---|
754 | piPcm += uiWidth; |
---|
755 | piSrc += uiStride; |
---|
756 | } |
---|
757 | } |
---|
758 | |
---|
759 | //! \} |
---|