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