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-2013, 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 TEncCu.cpp |
---|
35 | \brief Coding Unit (CU) encoder class |
---|
36 | */ |
---|
37 | |
---|
38 | #include <stdio.h> |
---|
39 | #include "TEncTop.h" |
---|
40 | #include "TEncCu.h" |
---|
41 | #include "TEncAnalyze.h" |
---|
42 | |
---|
43 | #include <cmath> |
---|
44 | #include <algorithm> |
---|
45 | using namespace std; |
---|
46 | |
---|
47 | //! \ingroup TLibEncoder |
---|
48 | //! \{ |
---|
49 | |
---|
50 | // ==================================================================================================================== |
---|
51 | // Constructor / destructor / create / destroy |
---|
52 | // ==================================================================================================================== |
---|
53 | |
---|
54 | /** |
---|
55 | \param uiTotalDepth total number of allowable depth |
---|
56 | \param uiMaxWidth largest CU width |
---|
57 | \param uiMaxHeight largest CU height |
---|
58 | */ |
---|
59 | Void TEncCu::create(UChar uhTotalDepth, UInt uiMaxWidth, UInt uiMaxHeight) |
---|
60 | { |
---|
61 | Int i; |
---|
62 | |
---|
63 | m_uhTotalDepth = uhTotalDepth + 1; |
---|
64 | m_ppcBestCU = new TComDataCU*[m_uhTotalDepth-1]; |
---|
65 | m_ppcTempCU = new TComDataCU*[m_uhTotalDepth-1]; |
---|
66 | |
---|
67 | m_ppcPredYuvBest = new TComYuv*[m_uhTotalDepth-1]; |
---|
68 | m_ppcResiYuvBest = new TComYuv*[m_uhTotalDepth-1]; |
---|
69 | m_ppcRecoYuvBest = new TComYuv*[m_uhTotalDepth-1]; |
---|
70 | m_ppcPredYuvTemp = new TComYuv*[m_uhTotalDepth-1]; |
---|
71 | m_ppcResiYuvTemp = new TComYuv*[m_uhTotalDepth-1]; |
---|
72 | m_ppcRecoYuvTemp = new TComYuv*[m_uhTotalDepth-1]; |
---|
73 | m_ppcOrigYuv = new TComYuv*[m_uhTotalDepth-1]; |
---|
74 | |
---|
75 | UInt uiNumPartitions; |
---|
76 | for( i=0 ; i<m_uhTotalDepth-1 ; i++) |
---|
77 | { |
---|
78 | uiNumPartitions = 1<<( ( m_uhTotalDepth - i - 1 )<<1 ); |
---|
79 | UInt uiWidth = uiMaxWidth >> i; |
---|
80 | UInt uiHeight = uiMaxHeight >> i; |
---|
81 | |
---|
82 | m_ppcBestCU[i] = new TComDataCU; m_ppcBestCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) ); |
---|
83 | m_ppcTempCU[i] = new TComDataCU; m_ppcTempCU[i]->create( uiNumPartitions, uiWidth, uiHeight, false, uiMaxWidth >> (m_uhTotalDepth - 1) ); |
---|
84 | |
---|
85 | m_ppcPredYuvBest[i] = new TComYuv; m_ppcPredYuvBest[i]->create(uiWidth, uiHeight); |
---|
86 | m_ppcResiYuvBest[i] = new TComYuv; m_ppcResiYuvBest[i]->create(uiWidth, uiHeight); |
---|
87 | m_ppcRecoYuvBest[i] = new TComYuv; m_ppcRecoYuvBest[i]->create(uiWidth, uiHeight); |
---|
88 | |
---|
89 | m_ppcPredYuvTemp[i] = new TComYuv; m_ppcPredYuvTemp[i]->create(uiWidth, uiHeight); |
---|
90 | m_ppcResiYuvTemp[i] = new TComYuv; m_ppcResiYuvTemp[i]->create(uiWidth, uiHeight); |
---|
91 | m_ppcRecoYuvTemp[i] = new TComYuv; m_ppcRecoYuvTemp[i]->create(uiWidth, uiHeight); |
---|
92 | |
---|
93 | m_ppcOrigYuv [i] = new TComYuv; m_ppcOrigYuv [i]->create(uiWidth, uiHeight); |
---|
94 | } |
---|
95 | |
---|
96 | m_bEncodeDQP = false; |
---|
97 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
98 | m_LCUPredictionSAD = 0; |
---|
99 | m_addSADDepth = 0; |
---|
100 | m_temporalSAD = 0; |
---|
101 | #endif |
---|
102 | |
---|
103 | // initialize partition order. |
---|
104 | UInt* piTmp = &g_auiZscanToRaster[0]; |
---|
105 | initZscanToRaster( m_uhTotalDepth, 1, 0, piTmp); |
---|
106 | initRasterToZscan( uiMaxWidth, uiMaxHeight, m_uhTotalDepth ); |
---|
107 | |
---|
108 | // initialize conversion matrix from partition index to pel |
---|
109 | initRasterToPelXY( uiMaxWidth, uiMaxHeight, m_uhTotalDepth ); |
---|
110 | } |
---|
111 | |
---|
112 | Void TEncCu::destroy() |
---|
113 | { |
---|
114 | Int i; |
---|
115 | |
---|
116 | for( i=0 ; i<m_uhTotalDepth-1 ; i++) |
---|
117 | { |
---|
118 | if(m_ppcBestCU[i]) |
---|
119 | { |
---|
120 | m_ppcBestCU[i]->destroy(); delete m_ppcBestCU[i]; m_ppcBestCU[i] = NULL; |
---|
121 | } |
---|
122 | if(m_ppcTempCU[i]) |
---|
123 | { |
---|
124 | m_ppcTempCU[i]->destroy(); delete m_ppcTempCU[i]; m_ppcTempCU[i] = NULL; |
---|
125 | } |
---|
126 | if(m_ppcPredYuvBest[i]) |
---|
127 | { |
---|
128 | m_ppcPredYuvBest[i]->destroy(); delete m_ppcPredYuvBest[i]; m_ppcPredYuvBest[i] = NULL; |
---|
129 | } |
---|
130 | if(m_ppcResiYuvBest[i]) |
---|
131 | { |
---|
132 | m_ppcResiYuvBest[i]->destroy(); delete m_ppcResiYuvBest[i]; m_ppcResiYuvBest[i] = NULL; |
---|
133 | } |
---|
134 | if(m_ppcRecoYuvBest[i]) |
---|
135 | { |
---|
136 | m_ppcRecoYuvBest[i]->destroy(); delete m_ppcRecoYuvBest[i]; m_ppcRecoYuvBest[i] = NULL; |
---|
137 | } |
---|
138 | if(m_ppcPredYuvTemp[i]) |
---|
139 | { |
---|
140 | m_ppcPredYuvTemp[i]->destroy(); delete m_ppcPredYuvTemp[i]; m_ppcPredYuvTemp[i] = NULL; |
---|
141 | } |
---|
142 | if(m_ppcResiYuvTemp[i]) |
---|
143 | { |
---|
144 | m_ppcResiYuvTemp[i]->destroy(); delete m_ppcResiYuvTemp[i]; m_ppcResiYuvTemp[i] = NULL; |
---|
145 | } |
---|
146 | if(m_ppcRecoYuvTemp[i]) |
---|
147 | { |
---|
148 | m_ppcRecoYuvTemp[i]->destroy(); delete m_ppcRecoYuvTemp[i]; m_ppcRecoYuvTemp[i] = NULL; |
---|
149 | } |
---|
150 | if(m_ppcOrigYuv[i]) |
---|
151 | { |
---|
152 | m_ppcOrigYuv[i]->destroy(); delete m_ppcOrigYuv[i]; m_ppcOrigYuv[i] = NULL; |
---|
153 | } |
---|
154 | } |
---|
155 | if(m_ppcBestCU) |
---|
156 | { |
---|
157 | delete [] m_ppcBestCU; |
---|
158 | m_ppcBestCU = NULL; |
---|
159 | } |
---|
160 | if(m_ppcTempCU) |
---|
161 | { |
---|
162 | delete [] m_ppcTempCU; |
---|
163 | m_ppcTempCU = NULL; |
---|
164 | } |
---|
165 | |
---|
166 | if(m_ppcPredYuvBest) |
---|
167 | { |
---|
168 | delete [] m_ppcPredYuvBest; |
---|
169 | m_ppcPredYuvBest = NULL; |
---|
170 | } |
---|
171 | if(m_ppcResiYuvBest) |
---|
172 | { |
---|
173 | delete [] m_ppcResiYuvBest; |
---|
174 | m_ppcResiYuvBest = NULL; |
---|
175 | } |
---|
176 | if(m_ppcRecoYuvBest) |
---|
177 | { |
---|
178 | delete [] m_ppcRecoYuvBest; |
---|
179 | m_ppcRecoYuvBest = NULL; |
---|
180 | } |
---|
181 | if(m_ppcPredYuvTemp) |
---|
182 | { |
---|
183 | delete [] m_ppcPredYuvTemp; |
---|
184 | m_ppcPredYuvTemp = NULL; |
---|
185 | } |
---|
186 | if(m_ppcResiYuvTemp) |
---|
187 | { |
---|
188 | delete [] m_ppcResiYuvTemp; |
---|
189 | m_ppcResiYuvTemp = NULL; |
---|
190 | } |
---|
191 | if(m_ppcRecoYuvTemp) |
---|
192 | { |
---|
193 | delete [] m_ppcRecoYuvTemp; |
---|
194 | m_ppcRecoYuvTemp = NULL; |
---|
195 | } |
---|
196 | if(m_ppcOrigYuv) |
---|
197 | { |
---|
198 | delete [] m_ppcOrigYuv; |
---|
199 | m_ppcOrigYuv = NULL; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | /** \param pcEncTop pointer of encoder class |
---|
204 | */ |
---|
205 | Void TEncCu::init( TEncTop* pcEncTop ) |
---|
206 | { |
---|
207 | m_pcEncCfg = pcEncTop; |
---|
208 | m_pcPredSearch = pcEncTop->getPredSearch(); |
---|
209 | m_pcTrQuant = pcEncTop->getTrQuant(); |
---|
210 | m_pcBitCounter = pcEncTop->getBitCounter(); |
---|
211 | m_pcRdCost = pcEncTop->getRdCost(); |
---|
212 | |
---|
213 | m_pcEntropyCoder = pcEncTop->getEntropyCoder(); |
---|
214 | m_pcCavlcCoder = pcEncTop->getCavlcCoder(); |
---|
215 | m_pcSbacCoder = pcEncTop->getSbacCoder(); |
---|
216 | m_pcBinCABAC = pcEncTop->getBinCABAC(); |
---|
217 | |
---|
218 | m_pppcRDSbacCoder = pcEncTop->getRDSbacCoder(); |
---|
219 | m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder(); |
---|
220 | |
---|
221 | m_bUseSBACRD = pcEncTop->getUseSBACRD(); |
---|
222 | m_pcRateCtrl = pcEncTop->getRateCtrl(); |
---|
223 | } |
---|
224 | |
---|
225 | // ==================================================================================================================== |
---|
226 | // Public member functions |
---|
227 | // ==================================================================================================================== |
---|
228 | |
---|
229 | /** \param rpcCU pointer of CU data class |
---|
230 | */ |
---|
231 | Void TEncCu::compressCU( TComDataCU*& rpcCU ) |
---|
232 | { |
---|
233 | // initialize CU data |
---|
234 | m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() ); |
---|
235 | m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() ); |
---|
236 | |
---|
237 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
238 | m_addSADDepth = 0; |
---|
239 | m_LCUPredictionSAD = 0; |
---|
240 | m_temporalSAD = 0; |
---|
241 | #endif |
---|
242 | |
---|
243 | // analysis of CU |
---|
244 | xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 ); |
---|
245 | |
---|
246 | #if ADAPTIVE_QP_SELECTION |
---|
247 | if( m_pcEncCfg->getUseAdaptQpSelect() ) |
---|
248 | { |
---|
249 | if(rpcCU->getSlice()->getSliceType()!=I_SLICE) //IIII |
---|
250 | { |
---|
251 | xLcuCollectARLStats( rpcCU); |
---|
252 | } |
---|
253 | } |
---|
254 | #endif |
---|
255 | } |
---|
256 | /** \param pcCU pointer of CU data class |
---|
257 | */ |
---|
258 | Void TEncCu::encodeCU ( TComDataCU* pcCU ) |
---|
259 | { |
---|
260 | if ( pcCU->getSlice()->getPPS()->getUseDQP() ) |
---|
261 | { |
---|
262 | setdQPFlag(true); |
---|
263 | } |
---|
264 | |
---|
265 | // Encode CU data |
---|
266 | xEncodeCU( pcCU, 0, 0 ); |
---|
267 | } |
---|
268 | |
---|
269 | // ==================================================================================================================== |
---|
270 | // Protected member functions |
---|
271 | // ==================================================================================================================== |
---|
272 | /** Derive small set of test modes for AMP encoder speed-up |
---|
273 | *\param rpcBestCU |
---|
274 | *\param eParentPartSize |
---|
275 | *\param bTestAMP_Hor |
---|
276 | *\param bTestAMP_Ver |
---|
277 | *\param bTestMergeAMP_Hor |
---|
278 | *\param bTestMergeAMP_Ver |
---|
279 | *\returns Void |
---|
280 | */ |
---|
281 | #if AMP_ENC_SPEEDUP |
---|
282 | #if AMP_MRG |
---|
283 | Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver, Bool &bTestMergeAMP_Hor, Bool &bTestMergeAMP_Ver) |
---|
284 | #else |
---|
285 | Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver) |
---|
286 | #endif |
---|
287 | { |
---|
288 | if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN ) |
---|
289 | { |
---|
290 | bTestAMP_Hor = true; |
---|
291 | } |
---|
292 | else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N ) |
---|
293 | { |
---|
294 | bTestAMP_Ver = true; |
---|
295 | } |
---|
296 | else if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->getMergeFlag(0) == false && rpcBestCU->isSkipped(0) == false ) |
---|
297 | { |
---|
298 | bTestAMP_Hor = true; |
---|
299 | bTestAMP_Ver = true; |
---|
300 | } |
---|
301 | |
---|
302 | #if AMP_MRG |
---|
303 | //! Utilizing the partition size of parent PU |
---|
304 | if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N ) |
---|
305 | { |
---|
306 | bTestMergeAMP_Hor = true; |
---|
307 | bTestMergeAMP_Ver = true; |
---|
308 | } |
---|
309 | |
---|
310 | if ( eParentPartSize == SIZE_NONE ) //! if parent is intra |
---|
311 | { |
---|
312 | if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN ) |
---|
313 | { |
---|
314 | bTestMergeAMP_Hor = true; |
---|
315 | } |
---|
316 | else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N ) |
---|
317 | { |
---|
318 | bTestMergeAMP_Ver = true; |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->isSkipped(0) == false ) |
---|
323 | { |
---|
324 | bTestMergeAMP_Hor = true; |
---|
325 | bTestMergeAMP_Ver = true; |
---|
326 | } |
---|
327 | |
---|
328 | if ( rpcBestCU->getWidth(0) == 64 ) |
---|
329 | { |
---|
330 | bTestAMP_Hor = false; |
---|
331 | bTestAMP_Ver = false; |
---|
332 | } |
---|
333 | #else |
---|
334 | //! Utilizing the partition size of parent PU |
---|
335 | if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N ) |
---|
336 | { |
---|
337 | bTestAMP_Hor = true; |
---|
338 | bTestAMP_Ver = true; |
---|
339 | } |
---|
340 | |
---|
341 | if ( eParentPartSize == SIZE_2Nx2N ) |
---|
342 | { |
---|
343 | bTestAMP_Hor = false; |
---|
344 | bTestAMP_Ver = false; |
---|
345 | } |
---|
346 | #endif |
---|
347 | } |
---|
348 | #endif |
---|
349 | |
---|
350 | // ==================================================================================================================== |
---|
351 | // Protected member functions |
---|
352 | // ==================================================================================================================== |
---|
353 | /** Compress a CU block recursively with enabling sub-LCU-level delta QP |
---|
354 | *\param rpcBestCU |
---|
355 | *\param rpcTempCU |
---|
356 | *\param uiDepth |
---|
357 | *\returns Void |
---|
358 | * |
---|
359 | *- for loop of QP value to compress the current CU with all possible QP |
---|
360 | */ |
---|
361 | #if AMP_ENC_SPEEDUP |
---|
362 | Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth, PartSize eParentPartSize ) |
---|
363 | #else |
---|
364 | Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth ) |
---|
365 | #endif |
---|
366 | { |
---|
367 | TComPic* pcPic = rpcBestCU->getPic(); |
---|
368 | |
---|
369 | // get Original YUV data from picture |
---|
370 | m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() ); |
---|
371 | |
---|
372 | // variables for fast encoder decision |
---|
373 | Bool bEarlySkip = false; |
---|
374 | Bool bTrySplit = true; |
---|
375 | Double fRD_Skip = MAX_DOUBLE; |
---|
376 | |
---|
377 | // variable for Early CU determination |
---|
378 | Bool bSubBranch = true; |
---|
379 | |
---|
380 | // variable for Cbf fast mode PU decision |
---|
381 | Bool doNotBlockPu = true; |
---|
382 | Bool earlyDetectionSkipMode = false; |
---|
383 | |
---|
384 | Bool bTrySplitDQP = true; |
---|
385 | |
---|
386 | static Double afCost[ MAX_CU_DEPTH ]; |
---|
387 | static Int aiNum [ MAX_CU_DEPTH ]; |
---|
388 | |
---|
389 | if ( rpcBestCU->getAddr() == 0 ) |
---|
390 | { |
---|
391 | ::memset( afCost, 0, sizeof( afCost ) ); |
---|
392 | ::memset( aiNum, 0, sizeof( aiNum ) ); |
---|
393 | } |
---|
394 | |
---|
395 | Bool bBoundary = false; |
---|
396 | UInt uiLPelX = rpcBestCU->getCUPelX(); |
---|
397 | UInt uiRPelX = uiLPelX + rpcBestCU->getWidth(0) - 1; |
---|
398 | UInt uiTPelY = rpcBestCU->getCUPelY(); |
---|
399 | UInt uiBPelY = uiTPelY + rpcBestCU->getHeight(0) - 1; |
---|
400 | |
---|
401 | Int iBaseQP = xComputeQP( rpcBestCU, uiDepth ); |
---|
402 | Int iMinQP; |
---|
403 | Int iMaxQP; |
---|
404 | Bool isAddLowestQP = false; |
---|
405 | Int lowestQP = -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(); |
---|
406 | |
---|
407 | if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
408 | { |
---|
409 | Int idQP = m_pcEncCfg->getMaxDeltaQP(); |
---|
410 | iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP ); |
---|
411 | iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP ); |
---|
412 | if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() ) |
---|
413 | { |
---|
414 | isAddLowestQP = true; |
---|
415 | iMinQP = iMinQP - 1; |
---|
416 | } |
---|
417 | } |
---|
418 | else |
---|
419 | { |
---|
420 | iMinQP = rpcTempCU->getQP(0); |
---|
421 | iMaxQP = rpcTempCU->getQP(0); |
---|
422 | } |
---|
423 | |
---|
424 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
425 | if ( m_pcEncCfg->getUseRateCtrl() ) |
---|
426 | { |
---|
427 | iMinQP = m_pcRateCtrl->getRCQP(); |
---|
428 | iMaxQP = m_pcRateCtrl->getRCQP(); |
---|
429 | } |
---|
430 | #else |
---|
431 | if(m_pcEncCfg->getUseRateCtrl()) |
---|
432 | { |
---|
433 | Int qp = m_pcRateCtrl->getUnitQP(); |
---|
434 | iMinQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
435 | iMaxQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
436 | } |
---|
437 | #endif |
---|
438 | |
---|
439 | // If slice start or slice end is within this cu... |
---|
440 | TComSlice * pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx()); |
---|
441 | Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurStartCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart(); |
---|
442 | Bool bSliceEnd = (pcSlice->getSliceSegmentCurEndCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurEndCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart()); |
---|
443 | Bool bInsidePicture = ( uiRPelX < rpcBestCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < rpcBestCU->getSlice()->getSPS()->getPicHeightInLumaSamples() ); |
---|
444 | // We need to split, so don't try these modes. |
---|
445 | if(!bSliceEnd && !bSliceStart && bInsidePicture ) |
---|
446 | { |
---|
447 | for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++) |
---|
448 | { |
---|
449 | if (isAddLowestQP && (iQP == iMinQP)) |
---|
450 | { |
---|
451 | iQP = lowestQP; |
---|
452 | } |
---|
453 | // variables for fast encoder decision |
---|
454 | bEarlySkip = false; |
---|
455 | bTrySplit = true; |
---|
456 | fRD_Skip = MAX_DOUBLE; |
---|
457 | |
---|
458 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
459 | |
---|
460 | // do inter modes, SKIP and 2Nx2N |
---|
461 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE ) |
---|
462 | { |
---|
463 | // 2Nx2N |
---|
464 | if(m_pcEncCfg->getUseEarlySkipDetection()) |
---|
465 | { |
---|
466 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); rpcTempCU->initEstData( uiDepth, iQP );//by Competition for inter_2Nx2N |
---|
467 | } |
---|
468 | // SKIP |
---|
469 | xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, &earlyDetectionSkipMode );//by Merge for inter_2Nx2N |
---|
470 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
471 | |
---|
472 | // fast encoder decision for early skip |
---|
473 | if ( m_pcEncCfg->getUseFastEnc() ) |
---|
474 | { |
---|
475 | Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ]; |
---|
476 | if ( aiNum [ iIdx ] > 5 && fRD_Skip < EARLY_SKIP_THRES*afCost[ iIdx ]/aiNum[ iIdx ] ) |
---|
477 | { |
---|
478 | bEarlySkip = true; |
---|
479 | bTrySplit = false; |
---|
480 | } |
---|
481 | } |
---|
482 | |
---|
483 | if(!m_pcEncCfg->getUseEarlySkipDetection()) |
---|
484 | { |
---|
485 | // 2Nx2N, NxN |
---|
486 | if ( !bEarlySkip ) |
---|
487 | { |
---|
488 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); rpcTempCU->initEstData( uiDepth, iQP ); |
---|
489 | if(m_pcEncCfg->getUseCbfFastMode()) |
---|
490 | { |
---|
491 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
492 | } |
---|
493 | } |
---|
494 | } |
---|
495 | } |
---|
496 | |
---|
497 | if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
498 | { |
---|
499 | if(iQP == iBaseQP) |
---|
500 | { |
---|
501 | bTrySplitDQP = bTrySplit; |
---|
502 | } |
---|
503 | } |
---|
504 | else |
---|
505 | { |
---|
506 | bTrySplitDQP = bTrySplit; |
---|
507 | } |
---|
508 | if (isAddLowestQP && (iQP == lowestQP)) |
---|
509 | { |
---|
510 | iQP = iMinQP; |
---|
511 | } |
---|
512 | } |
---|
513 | |
---|
514 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
515 | if ( uiDepth <= m_addSADDepth ) |
---|
516 | { |
---|
517 | m_LCUPredictionSAD += m_temporalSAD; |
---|
518 | m_addSADDepth = uiDepth; |
---|
519 | } |
---|
520 | #endif |
---|
521 | |
---|
522 | if(!earlyDetectionSkipMode) |
---|
523 | { |
---|
524 | for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++) |
---|
525 | { |
---|
526 | if (isAddLowestQP && (iQP == iMinQP)) |
---|
527 | { |
---|
528 | iQP = lowestQP; |
---|
529 | } |
---|
530 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
531 | |
---|
532 | // do inter modes, NxN, 2NxN, and Nx2N |
---|
533 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE ) |
---|
534 | { |
---|
535 | // 2Nx2N, NxN |
---|
536 | if ( !bEarlySkip ) |
---|
537 | { |
---|
538 | if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) )) |
---|
539 | { |
---|
540 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu) |
---|
541 | { |
---|
542 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN ); |
---|
543 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
544 | } |
---|
545 | } |
---|
546 | } |
---|
547 | |
---|
548 | // 2NxN, Nx2N |
---|
549 | if(doNotBlockPu) |
---|
550 | { |
---|
551 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N ); |
---|
552 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
553 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N ) |
---|
554 | { |
---|
555 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
556 | } |
---|
557 | } |
---|
558 | if(doNotBlockPu) |
---|
559 | { |
---|
560 | xCheckRDCostInter ( rpcBestCU, rpcTempCU, SIZE_2NxN ); |
---|
561 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
562 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN) |
---|
563 | { |
---|
564 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
565 | } |
---|
566 | } |
---|
567 | |
---|
568 | #if 1 |
---|
569 | //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N) |
---|
570 | if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) ) |
---|
571 | { |
---|
572 | #if AMP_ENC_SPEEDUP |
---|
573 | Bool bTestAMP_Hor = false, bTestAMP_Ver = false; |
---|
574 | |
---|
575 | #if AMP_MRG |
---|
576 | Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false; |
---|
577 | |
---|
578 | deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver); |
---|
579 | #else |
---|
580 | deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver); |
---|
581 | #endif |
---|
582 | |
---|
583 | //! Do horizontal AMP |
---|
584 | if ( bTestAMP_Hor ) |
---|
585 | { |
---|
586 | if(doNotBlockPu) |
---|
587 | { |
---|
588 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU ); |
---|
589 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
590 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU ) |
---|
591 | { |
---|
592 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
593 | } |
---|
594 | } |
---|
595 | if(doNotBlockPu) |
---|
596 | { |
---|
597 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD ); |
---|
598 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
599 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD ) |
---|
600 | { |
---|
601 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
602 | } |
---|
603 | } |
---|
604 | } |
---|
605 | #if AMP_MRG |
---|
606 | else if ( bTestMergeAMP_Hor ) |
---|
607 | { |
---|
608 | if(doNotBlockPu) |
---|
609 | { |
---|
610 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true ); |
---|
611 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
612 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU ) |
---|
613 | { |
---|
614 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
615 | } |
---|
616 | } |
---|
617 | if(doNotBlockPu) |
---|
618 | { |
---|
619 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true ); |
---|
620 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
621 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD ) |
---|
622 | { |
---|
623 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
624 | } |
---|
625 | } |
---|
626 | } |
---|
627 | #endif |
---|
628 | |
---|
629 | //! Do horizontal AMP |
---|
630 | if ( bTestAMP_Ver ) |
---|
631 | { |
---|
632 | if(doNotBlockPu) |
---|
633 | { |
---|
634 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N ); |
---|
635 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
636 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N ) |
---|
637 | { |
---|
638 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
639 | } |
---|
640 | } |
---|
641 | if(doNotBlockPu) |
---|
642 | { |
---|
643 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N ); |
---|
644 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
645 | } |
---|
646 | } |
---|
647 | #if AMP_MRG |
---|
648 | else if ( bTestMergeAMP_Ver ) |
---|
649 | { |
---|
650 | if(doNotBlockPu) |
---|
651 | { |
---|
652 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true ); |
---|
653 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
654 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N ) |
---|
655 | { |
---|
656 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
657 | } |
---|
658 | } |
---|
659 | if(doNotBlockPu) |
---|
660 | { |
---|
661 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true ); |
---|
662 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
663 | } |
---|
664 | } |
---|
665 | #endif |
---|
666 | |
---|
667 | #else |
---|
668 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU ); |
---|
669 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
670 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD ); |
---|
671 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
672 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N ); |
---|
673 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
674 | |
---|
675 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N ); |
---|
676 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
677 | |
---|
678 | #endif |
---|
679 | } |
---|
680 | #endif |
---|
681 | } |
---|
682 | |
---|
683 | // do normal intra modes |
---|
684 | if ( !bEarlySkip ) |
---|
685 | { |
---|
686 | // speedup for inter frames |
---|
687 | if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || |
---|
688 | rpcBestCU->getCbf( 0, TEXT_LUMA ) != 0 || |
---|
689 | rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0 || |
---|
690 | rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0 ) // avoid very complex intra if it is unlikely |
---|
691 | { |
---|
692 | xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); |
---|
693 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
694 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
695 | { |
---|
696 | if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) ) |
---|
697 | { |
---|
698 | xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN ); |
---|
699 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
700 | } |
---|
701 | } |
---|
702 | } |
---|
703 | } |
---|
704 | |
---|
705 | // test PCM |
---|
706 | if(pcPic->getSlice(0)->getSPS()->getUsePCM() |
---|
707 | && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize()) |
---|
708 | && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) ) |
---|
709 | { |
---|
710 | UInt uiRawBits = (2 * g_bitDepthY + g_bitDepthC) * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) / 2; |
---|
711 | UInt uiBestBits = rpcBestCU->getTotalBits(); |
---|
712 | if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0))) |
---|
713 | { |
---|
714 | xCheckIntraPCM (rpcBestCU, rpcTempCU); |
---|
715 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
716 | } |
---|
717 | } |
---|
718 | if (isAddLowestQP && (iQP == lowestQP)) |
---|
719 | { |
---|
720 | iQP = iMinQP; |
---|
721 | } |
---|
722 | } |
---|
723 | } |
---|
724 | |
---|
725 | m_pcEntropyCoder->resetBits(); |
---|
726 | m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true ); |
---|
727 | rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits |
---|
728 | if(m_pcEncCfg->getUseSBACRD()) |
---|
729 | { |
---|
730 | rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
731 | } |
---|
732 | rpcBestCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() ); |
---|
733 | |
---|
734 | // accumulate statistics for early skip |
---|
735 | if ( m_pcEncCfg->getUseFastEnc() ) |
---|
736 | { |
---|
737 | if ( rpcBestCU->isSkipped(0) ) |
---|
738 | { |
---|
739 | Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ]; |
---|
740 | afCost[ iIdx ] += rpcBestCU->getTotalCost(); |
---|
741 | aiNum [ iIdx ] ++; |
---|
742 | } |
---|
743 | } |
---|
744 | |
---|
745 | // Early CU determination |
---|
746 | if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) ) |
---|
747 | { |
---|
748 | bSubBranch = false; |
---|
749 | } |
---|
750 | else |
---|
751 | { |
---|
752 | bSubBranch = true; |
---|
753 | } |
---|
754 | } |
---|
755 | else if(!(bSliceEnd && bInsidePicture)) |
---|
756 | { |
---|
757 | bBoundary = true; |
---|
758 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
759 | m_addSADDepth++; |
---|
760 | #endif |
---|
761 | } |
---|
762 | |
---|
763 | // copy orginal YUV samples to PCM buffer |
---|
764 | if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false)) |
---|
765 | { |
---|
766 | xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]); |
---|
767 | } |
---|
768 | if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
769 | { |
---|
770 | Int idQP = m_pcEncCfg->getMaxDeltaQP(); |
---|
771 | iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP ); |
---|
772 | iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP ); |
---|
773 | if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() ) |
---|
774 | { |
---|
775 | isAddLowestQP = true; |
---|
776 | iMinQP = iMinQP - 1; |
---|
777 | } |
---|
778 | } |
---|
779 | else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
780 | { |
---|
781 | iMinQP = iBaseQP; |
---|
782 | iMaxQP = iBaseQP; |
---|
783 | } |
---|
784 | else |
---|
785 | { |
---|
786 | Int iStartQP; |
---|
787 | if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr()) |
---|
788 | { |
---|
789 | iStartQP = rpcTempCU->getQP(0); |
---|
790 | } |
---|
791 | else |
---|
792 | { |
---|
793 | UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU(); |
---|
794 | iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx); |
---|
795 | } |
---|
796 | iMinQP = iStartQP; |
---|
797 | iMaxQP = iStartQP; |
---|
798 | } |
---|
799 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
800 | if ( m_pcEncCfg->getUseRateCtrl() ) |
---|
801 | { |
---|
802 | iMinQP = m_pcRateCtrl->getRCQP(); |
---|
803 | iMaxQP = m_pcRateCtrl->getRCQP(); |
---|
804 | } |
---|
805 | #else |
---|
806 | if(m_pcEncCfg->getUseRateCtrl()) |
---|
807 | { |
---|
808 | Int qp = m_pcRateCtrl->getUnitQP(); |
---|
809 | iMinQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
810 | iMaxQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
811 | } |
---|
812 | #endif |
---|
813 | for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++) |
---|
814 | { |
---|
815 | if (isAddLowestQP && (iQP == iMinQP)) |
---|
816 | { |
---|
817 | iQP = lowestQP; |
---|
818 | } |
---|
819 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
820 | |
---|
821 | // further split |
---|
822 | if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
823 | { |
---|
824 | UChar uhNextDepth = uiDepth+1; |
---|
825 | TComDataCU* pcSubBestPartCU = m_ppcBestCU[uhNextDepth]; |
---|
826 | TComDataCU* pcSubTempPartCU = m_ppcTempCU[uhNextDepth]; |
---|
827 | |
---|
828 | for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ ) |
---|
829 | { |
---|
830 | pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP ); // clear sub partition datas or init. |
---|
831 | pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP ); // clear sub partition datas or init. |
---|
832 | |
---|
833 | Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr(); |
---|
834 | if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
835 | { |
---|
836 | if( m_bUseSBACRD ) |
---|
837 | { |
---|
838 | if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer |
---|
839 | { |
---|
840 | m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]); |
---|
841 | } |
---|
842 | else |
---|
843 | { |
---|
844 | m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]); |
---|
845 | } |
---|
846 | } |
---|
847 | |
---|
848 | #if AMP_ENC_SPEEDUP |
---|
849 | if ( rpcBestCU->isIntra(0) ) |
---|
850 | { |
---|
851 | xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE ); |
---|
852 | } |
---|
853 | else |
---|
854 | { |
---|
855 | xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) ); |
---|
856 | } |
---|
857 | #else |
---|
858 | xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth ); |
---|
859 | #endif |
---|
860 | |
---|
861 | rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth ); // Keep best part data to current temporary data. |
---|
862 | xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth ); |
---|
863 | } |
---|
864 | else if (bInSlice) |
---|
865 | { |
---|
866 | pcSubBestPartCU->copyToPic( uhNextDepth ); |
---|
867 | rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth ); |
---|
868 | } |
---|
869 | } |
---|
870 | |
---|
871 | if( !bBoundary ) |
---|
872 | { |
---|
873 | m_pcEntropyCoder->resetBits(); |
---|
874 | m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true ); |
---|
875 | |
---|
876 | rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits |
---|
877 | if(m_pcEncCfg->getUseSBACRD()) |
---|
878 | { |
---|
879 | rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
880 | } |
---|
881 | } |
---|
882 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
883 | |
---|
884 | if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP()) |
---|
885 | { |
---|
886 | Bool hasResidual = false; |
---|
887 | for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++) |
---|
888 | { |
---|
889 | if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && |
---|
890 | ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) ) |
---|
891 | { |
---|
892 | hasResidual = true; |
---|
893 | break; |
---|
894 | } |
---|
895 | } |
---|
896 | |
---|
897 | UInt uiTargetPartIdx; |
---|
898 | if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() ) |
---|
899 | { |
---|
900 | uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU(); |
---|
901 | } |
---|
902 | else |
---|
903 | { |
---|
904 | uiTargetPartIdx = 0; |
---|
905 | } |
---|
906 | if ( hasResidual ) |
---|
907 | { |
---|
908 | #if !RDO_WITHOUT_DQP_BITS |
---|
909 | m_pcEntropyCoder->resetBits(); |
---|
910 | m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false ); |
---|
911 | rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits |
---|
912 | if(m_pcEncCfg->getUseSBACRD()) |
---|
913 | { |
---|
914 | rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
915 | } |
---|
916 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
917 | #endif |
---|
918 | |
---|
919 | Bool foundNonZeroCbf = false; |
---|
920 | rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf ); |
---|
921 | assert( foundNonZeroCbf ); |
---|
922 | } |
---|
923 | else |
---|
924 | { |
---|
925 | rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP |
---|
926 | } |
---|
927 | } |
---|
928 | |
---|
929 | if( m_bUseSBACRD ) |
---|
930 | { |
---|
931 | m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
932 | } |
---|
933 | Bool isEndOfSlice = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES |
---|
934 | && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3); |
---|
935 | Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES |
---|
936 | && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3); |
---|
937 | if(isEndOfSlice||isEndOfSliceSegment) |
---|
938 | { |
---|
939 | rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1; |
---|
940 | } |
---|
941 | xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth); // RD compare current larger prediction |
---|
942 | } // with sub partitioned prediction. |
---|
943 | if (isAddLowestQP && (iQP == lowestQP)) |
---|
944 | { |
---|
945 | iQP = iMinQP; |
---|
946 | } |
---|
947 | } |
---|
948 | |
---|
949 | rpcBestCU->copyToPic(uiDepth); // Copy Best data to Picture for next partition prediction. |
---|
950 | |
---|
951 | xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY ); // Copy Yuv data to picture Yuv |
---|
952 | if( bBoundary ||(bSliceEnd && bInsidePicture)) |
---|
953 | { |
---|
954 | return; |
---|
955 | } |
---|
956 | |
---|
957 | // Assert if Best prediction mode is NONE |
---|
958 | // Selected mode's RD-cost must be not MAX_DOUBLE. |
---|
959 | assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE ); |
---|
960 | assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE ); |
---|
961 | assert( rpcBestCU->getTotalCost ( ) != MAX_DOUBLE ); |
---|
962 | } |
---|
963 | |
---|
964 | /** finish encoding a cu and handle end-of-slice conditions |
---|
965 | * \param pcCU |
---|
966 | * \param uiAbsPartIdx |
---|
967 | * \param uiDepth |
---|
968 | * \returns Void |
---|
969 | */ |
---|
970 | Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
971 | { |
---|
972 | TComPic* pcPic = pcCU->getPic(); |
---|
973 | TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx()); |
---|
974 | |
---|
975 | //Calculate end address |
---|
976 | UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx; |
---|
977 | |
---|
978 | UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU(); |
---|
979 | UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU(); |
---|
980 | UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
981 | UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
982 | UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples(); |
---|
983 | UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples(); |
---|
984 | while(uiPosX>=uiWidth||uiPosY>=uiHeight) |
---|
985 | { |
---|
986 | uiInternalAddress--; |
---|
987 | uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
988 | uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
989 | } |
---|
990 | uiInternalAddress++; |
---|
991 | if(uiInternalAddress==pcCU->getPic()->getNumPartInCU()) |
---|
992 | { |
---|
993 | uiInternalAddress = 0; |
---|
994 | uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1); |
---|
995 | } |
---|
996 | UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress); |
---|
997 | |
---|
998 | // Encode slice finish |
---|
999 | Bool bTerminateSlice = false; |
---|
1000 | if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress) |
---|
1001 | { |
---|
1002 | bTerminateSlice = true; |
---|
1003 | } |
---|
1004 | UInt uiGranularityWidth = g_uiMaxCUWidth; |
---|
1005 | uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
1006 | uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
1007 | Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth)) |
---|
1008 | &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight)); |
---|
1009 | |
---|
1010 | if(granularityBoundary) |
---|
1011 | { |
---|
1012 | // The 1-terminating bit is added to all streams, so don't add it here when it's 1. |
---|
1013 | if (!bTerminateSlice) |
---|
1014 | m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 ); |
---|
1015 | } |
---|
1016 | |
---|
1017 | Int numberOfWrittenBits = 0; |
---|
1018 | if (m_pcBitCounter) |
---|
1019 | { |
---|
1020 | numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
1021 | } |
---|
1022 | |
---|
1023 | // Calculate slice end IF this CU puts us over slice bit size. |
---|
1024 | UInt iGranularitySize = pcCU->getPic()->getNumPartInCU(); |
---|
1025 | Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize; |
---|
1026 | if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) |
---|
1027 | { |
---|
1028 | iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1))); |
---|
1029 | } |
---|
1030 | // Set slice end parameter |
---|
1031 | if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) |
---|
1032 | { |
---|
1033 | pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd); |
---|
1034 | pcSlice->setSliceCurEndCUAddr(iGranularityEnd); |
---|
1035 | return; |
---|
1036 | } |
---|
1037 | // Set dependent slice end parameter |
---|
1038 | if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) |
---|
1039 | { |
---|
1040 | pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd); |
---|
1041 | return; |
---|
1042 | } |
---|
1043 | if(granularityBoundary) |
---|
1044 | { |
---|
1045 | pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) ); |
---|
1046 | pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits); |
---|
1047 | if (m_pcBitCounter) |
---|
1048 | { |
---|
1049 | m_pcEntropyCoder->resetBits(); |
---|
1050 | } |
---|
1051 | } |
---|
1052 | } |
---|
1053 | |
---|
1054 | /** Compute QP for each CU |
---|
1055 | * \param pcCU Target CU |
---|
1056 | * \param uiDepth CU depth |
---|
1057 | * \returns quantization parameter |
---|
1058 | */ |
---|
1059 | Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth ) |
---|
1060 | { |
---|
1061 | Int iBaseQp = pcCU->getSlice()->getSliceQp(); |
---|
1062 | Int iQpOffset = 0; |
---|
1063 | if ( m_pcEncCfg->getUseAdaptiveQP() ) |
---|
1064 | { |
---|
1065 | TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() ); |
---|
1066 | UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 ); |
---|
1067 | TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth ); |
---|
1068 | UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth(); |
---|
1069 | UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight(); |
---|
1070 | UInt uiAQUStride = pcAQLayer->getAQPartStride(); |
---|
1071 | TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit(); |
---|
1072 | |
---|
1073 | Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0); |
---|
1074 | Double dAvgAct = pcAQLayer->getAvgActivity(); |
---|
1075 | Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity(); |
---|
1076 | Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct); |
---|
1077 | Double dQpOffset = log(dNormAct) / log(2.0) * 6.0; |
---|
1078 | iQpOffset = Int(floor( dQpOffset + 0.49999 )); |
---|
1079 | } |
---|
1080 | return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset ); |
---|
1081 | } |
---|
1082 | |
---|
1083 | /** encode a CU block recursively |
---|
1084 | * \param pcCU |
---|
1085 | * \param uiAbsPartIdx |
---|
1086 | * \param uiDepth |
---|
1087 | * \returns Void |
---|
1088 | */ |
---|
1089 | Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
1090 | { |
---|
1091 | TComPic* pcPic = pcCU->getPic(); |
---|
1092 | |
---|
1093 | Bool bBoundary = false; |
---|
1094 | UInt uiLPelX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
1095 | UInt uiRPelX = uiLPelX + (g_uiMaxCUWidth>>uiDepth) - 1; |
---|
1096 | UInt uiTPelY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
1097 | UInt uiBPelY = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1; |
---|
1098 | |
---|
1099 | TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx()); |
---|
1100 | // If slice start is within this cu... |
---|
1101 | Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && |
---|
1102 | pcSlice->getSliceSegmentCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) ); |
---|
1103 | // We need to split, so don't try these modes. |
---|
1104 | if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
1105 | { |
---|
1106 | m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth ); |
---|
1107 | } |
---|
1108 | else |
---|
1109 | { |
---|
1110 | bBoundary = true; |
---|
1111 | } |
---|
1112 | |
---|
1113 | if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary ) |
---|
1114 | { |
---|
1115 | UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2; |
---|
1116 | if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP()) |
---|
1117 | { |
---|
1118 | setdQPFlag(true); |
---|
1119 | } |
---|
1120 | for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts ) |
---|
1121 | { |
---|
1122 | uiLPelX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
1123 | uiTPelY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
1124 | Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr(); |
---|
1125 | if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
1126 | { |
---|
1127 | xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 ); |
---|
1128 | } |
---|
1129 | } |
---|
1130 | return; |
---|
1131 | } |
---|
1132 | |
---|
1133 | if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP()) |
---|
1134 | { |
---|
1135 | setdQPFlag(true); |
---|
1136 | } |
---|
1137 | if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
1138 | { |
---|
1139 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx ); |
---|
1140 | } |
---|
1141 | if( !pcCU->getSlice()->isIntra() ) |
---|
1142 | { |
---|
1143 | m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx ); |
---|
1144 | } |
---|
1145 | |
---|
1146 | if( pcCU->isSkipped( uiAbsPartIdx ) ) |
---|
1147 | { |
---|
1148 | m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx ); |
---|
1149 | finishCU(pcCU,uiAbsPartIdx,uiDepth); |
---|
1150 | return; |
---|
1151 | } |
---|
1152 | m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx ); |
---|
1153 | |
---|
1154 | m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth ); |
---|
1155 | |
---|
1156 | if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N ) |
---|
1157 | { |
---|
1158 | m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx ); |
---|
1159 | |
---|
1160 | if(pcCU->getIPCMFlag(uiAbsPartIdx)) |
---|
1161 | { |
---|
1162 | // Encode slice finish |
---|
1163 | finishCU(pcCU,uiAbsPartIdx,uiDepth); |
---|
1164 | return; |
---|
1165 | } |
---|
1166 | } |
---|
1167 | |
---|
1168 | // prediction Info ( Intra : direction mode, Inter : Mv, reference idx ) |
---|
1169 | m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx ); |
---|
1170 | |
---|
1171 | // Encode Coefficients |
---|
1172 | Bool bCodeDQP = getdQPFlag(); |
---|
1173 | m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP ); |
---|
1174 | setdQPFlag( bCodeDQP ); |
---|
1175 | |
---|
1176 | // --- write terminating bit --- |
---|
1177 | finishCU(pcCU,uiAbsPartIdx,uiDepth); |
---|
1178 | } |
---|
1179 | |
---|
1180 | /** check RD costs for a CU block encoded with merge |
---|
1181 | * \param rpcBestCU |
---|
1182 | * \param rpcTempCU |
---|
1183 | * \returns Void |
---|
1184 | */ |
---|
1185 | Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode ) |
---|
1186 | { |
---|
1187 | assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE ); |
---|
1188 | TComMvField cMvFieldNeighbours[MRG_MAX_NUM_CANDS << 1]; // double length for mv of both lists |
---|
1189 | UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS]; |
---|
1190 | Int numValidMergeCand = 0; |
---|
1191 | |
---|
1192 | for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui ) |
---|
1193 | { |
---|
1194 | uhInterDirNeighbours[ui] = 0; |
---|
1195 | } |
---|
1196 | UChar uhDepth = rpcTempCU->getDepth( 0 ); |
---|
1197 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level |
---|
1198 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
1199 | rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand ); |
---|
1200 | |
---|
1201 | Int mergeCandBuffer[MRG_MAX_NUM_CANDS]; |
---|
1202 | for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui ) |
---|
1203 | { |
---|
1204 | mergeCandBuffer[ui] = 0; |
---|
1205 | } |
---|
1206 | |
---|
1207 | Bool bestIsSkip = false; |
---|
1208 | |
---|
1209 | UInt iteration; |
---|
1210 | if ( rpcTempCU->isLosslessCoded(0)) |
---|
1211 | { |
---|
1212 | iteration = 1; |
---|
1213 | } |
---|
1214 | else |
---|
1215 | { |
---|
1216 | iteration = 2; |
---|
1217 | } |
---|
1218 | |
---|
1219 | for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual ) |
---|
1220 | { |
---|
1221 | for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand ) |
---|
1222 | { |
---|
1223 | { |
---|
1224 | if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1)) |
---|
1225 | { |
---|
1226 | |
---|
1227 | if( !(bestIsSkip && uiNoResidual == 0) ) |
---|
1228 | { |
---|
1229 | // set MC parameters |
---|
1230 | rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level |
---|
1231 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
1232 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level |
---|
1233 | rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level |
---|
1234 | rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level |
---|
1235 | rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level |
---|
1236 | rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level |
---|
1237 | rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level |
---|
1238 | |
---|
1239 | // do MC |
---|
1240 | m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] ); |
---|
1241 | // estimate residual and encode everything |
---|
1242 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, |
---|
1243 | m_ppcOrigYuv [uhDepth], |
---|
1244 | m_ppcPredYuvTemp[uhDepth], |
---|
1245 | m_ppcResiYuvTemp[uhDepth], |
---|
1246 | m_ppcResiYuvBest[uhDepth], |
---|
1247 | m_ppcRecoYuvTemp[uhDepth], |
---|
1248 | (uiNoResidual? true:false)); |
---|
1249 | |
---|
1250 | |
---|
1251 | if(uiNoResidual==0) |
---|
1252 | { |
---|
1253 | if(rpcTempCU->getQtRootCbf(0) == 0) |
---|
1254 | { |
---|
1255 | mergeCandBuffer[uiMergeCand] = 1; |
---|
1256 | } |
---|
1257 | } |
---|
1258 | |
---|
1259 | rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth ); |
---|
1260 | Int orgQP = rpcTempCU->getQP( 0 ); |
---|
1261 | xCheckDQP( rpcTempCU ); |
---|
1262 | xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth); |
---|
1263 | rpcTempCU->initEstData( uhDepth, orgQP ); |
---|
1264 | |
---|
1265 | |
---|
1266 | if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip ) |
---|
1267 | { |
---|
1268 | bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0; |
---|
1269 | } |
---|
1270 | |
---|
1271 | } |
---|
1272 | } |
---|
1273 | } |
---|
1274 | } |
---|
1275 | |
---|
1276 | if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection()) |
---|
1277 | { |
---|
1278 | if(rpcBestCU->getQtRootCbf( 0 ) == 0) |
---|
1279 | { |
---|
1280 | if( rpcBestCU->getMergeFlag( 0 )) |
---|
1281 | { |
---|
1282 | *earlyDetectionSkipMode = true; |
---|
1283 | } |
---|
1284 | else |
---|
1285 | { |
---|
1286 | Int absoulte_MV=0; |
---|
1287 | for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ ) |
---|
1288 | { |
---|
1289 | if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 ) |
---|
1290 | { |
---|
1291 | TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx )); |
---|
1292 | Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor(); |
---|
1293 | Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer(); |
---|
1294 | absoulte_MV+=iHor+iVer; |
---|
1295 | } |
---|
1296 | } |
---|
1297 | |
---|
1298 | if(absoulte_MV == 0) |
---|
1299 | { |
---|
1300 | *earlyDetectionSkipMode = true; |
---|
1301 | } |
---|
1302 | } |
---|
1303 | } |
---|
1304 | } |
---|
1305 | } |
---|
1306 | } |
---|
1307 | |
---|
1308 | |
---|
1309 | #if AMP_MRG |
---|
1310 | Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG) |
---|
1311 | #else |
---|
1312 | Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize ) |
---|
1313 | #endif |
---|
1314 | { |
---|
1315 | UChar uhDepth = rpcTempCU->getDepth( 0 ); |
---|
1316 | |
---|
1317 | rpcTempCU->setDepthSubParts( uhDepth, 0 ); |
---|
1318 | |
---|
1319 | rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth ); |
---|
1320 | |
---|
1321 | rpcTempCU->setPartSizeSubParts ( ePartSize, 0, uhDepth ); |
---|
1322 | rpcTempCU->setPredModeSubParts ( MODE_INTER, 0, uhDepth ); |
---|
1323 | rpcTempCU->setCUTransquantBypassSubParts ( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
1324 | |
---|
1325 | #if AMP_MRG |
---|
1326 | rpcTempCU->setMergeAMP (true); |
---|
1327 | m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG ); |
---|
1328 | #else |
---|
1329 | m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] ); |
---|
1330 | #endif |
---|
1331 | |
---|
1332 | #if AMP_MRG |
---|
1333 | if ( !rpcTempCU->getMergeAMP() ) |
---|
1334 | { |
---|
1335 | return; |
---|
1336 | } |
---|
1337 | #endif |
---|
1338 | |
---|
1339 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
1340 | if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth ) |
---|
1341 | { |
---|
1342 | UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(), |
---|
1343 | m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(), |
---|
1344 | rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) ); |
---|
1345 | m_temporalSAD = (Int)SAD; |
---|
1346 | } |
---|
1347 | #endif |
---|
1348 | |
---|
1349 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false ); |
---|
1350 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
1351 | |
---|
1352 | xCheckDQP( rpcTempCU ); |
---|
1353 | xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth); |
---|
1354 | } |
---|
1355 | |
---|
1356 | Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize ) |
---|
1357 | { |
---|
1358 | UInt uiDepth = rpcTempCU->getDepth( 0 ); |
---|
1359 | |
---|
1360 | rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth ); |
---|
1361 | |
---|
1362 | rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth ); |
---|
1363 | rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth ); |
---|
1364 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth ); |
---|
1365 | |
---|
1366 | Bool bSeparateLumaChroma = true; // choose estimation mode |
---|
1367 | UInt uiPreCalcDistC = 0; |
---|
1368 | if( !bSeparateLumaChroma ) |
---|
1369 | { |
---|
1370 | m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] ); |
---|
1371 | } |
---|
1372 | m_pcPredSearch ->estIntraPredQT ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma ); |
---|
1373 | |
---|
1374 | m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() ); |
---|
1375 | |
---|
1376 | m_pcPredSearch ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC ); |
---|
1377 | |
---|
1378 | m_pcEntropyCoder->resetBits(); |
---|
1379 | if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
1380 | { |
---|
1381 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0, true ); |
---|
1382 | } |
---|
1383 | m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0, true ); |
---|
1384 | m_pcEntropyCoder->encodePredMode( rpcTempCU, 0, true ); |
---|
1385 | m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true ); |
---|
1386 | m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0, true ); |
---|
1387 | m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true ); |
---|
1388 | |
---|
1389 | // Encode Coefficients |
---|
1390 | Bool bCodeDQP = getdQPFlag(); |
---|
1391 | m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP ); |
---|
1392 | setdQPFlag( bCodeDQP ); |
---|
1393 | |
---|
1394 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
1395 | |
---|
1396 | rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
1397 | if(m_pcEncCfg->getUseSBACRD()) |
---|
1398 | { |
---|
1399 | rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
1400 | } |
---|
1401 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
1402 | |
---|
1403 | xCheckDQP( rpcTempCU ); |
---|
1404 | xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth); |
---|
1405 | } |
---|
1406 | |
---|
1407 | /** Check R-D costs for a CU with PCM mode. |
---|
1408 | * \param rpcBestCU pointer to best mode CU data structure |
---|
1409 | * \param rpcTempCU pointer to testing mode CU data structure |
---|
1410 | * \returns Void |
---|
1411 | * |
---|
1412 | * \note Current PCM implementation encodes sample values in a lossless way. The distortion of PCM mode CUs are zero. PCM mode is selected if the best mode yields bits greater than that of PCM mode. |
---|
1413 | */ |
---|
1414 | Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU ) |
---|
1415 | { |
---|
1416 | UInt uiDepth = rpcTempCU->getDepth( 0 ); |
---|
1417 | |
---|
1418 | rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth ); |
---|
1419 | |
---|
1420 | rpcTempCU->setIPCMFlag(0, true); |
---|
1421 | rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0)); |
---|
1422 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth ); |
---|
1423 | rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth ); |
---|
1424 | rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth ); |
---|
1425 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth ); |
---|
1426 | |
---|
1427 | m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]); |
---|
1428 | |
---|
1429 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]); |
---|
1430 | |
---|
1431 | m_pcEntropyCoder->resetBits(); |
---|
1432 | if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
1433 | { |
---|
1434 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0, true ); |
---|
1435 | } |
---|
1436 | m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0, true ); |
---|
1437 | m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0, true ); |
---|
1438 | m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true ); |
---|
1439 | m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true ); |
---|
1440 | |
---|
1441 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
1442 | |
---|
1443 | rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
1444 | if(m_pcEncCfg->getUseSBACRD()) |
---|
1445 | { |
---|
1446 | rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
1447 | } |
---|
1448 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
1449 | |
---|
1450 | xCheckDQP( rpcTempCU ); |
---|
1451 | xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth ); |
---|
1452 | } |
---|
1453 | |
---|
1454 | /** check whether current try is the best with identifying the depth of current try |
---|
1455 | * \param rpcBestCU |
---|
1456 | * \param rpcTempCU |
---|
1457 | * \returns Void |
---|
1458 | */ |
---|
1459 | Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth ) |
---|
1460 | { |
---|
1461 | if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() ) |
---|
1462 | { |
---|
1463 | TComYuv* pcYuv; |
---|
1464 | // Change Information data |
---|
1465 | TComDataCU* pcCU = rpcBestCU; |
---|
1466 | rpcBestCU = rpcTempCU; |
---|
1467 | rpcTempCU = pcCU; |
---|
1468 | |
---|
1469 | // Change Prediction data |
---|
1470 | pcYuv = m_ppcPredYuvBest[uiDepth]; |
---|
1471 | m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth]; |
---|
1472 | m_ppcPredYuvTemp[uiDepth] = pcYuv; |
---|
1473 | |
---|
1474 | // Change Reconstruction data |
---|
1475 | pcYuv = m_ppcRecoYuvBest[uiDepth]; |
---|
1476 | m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth]; |
---|
1477 | m_ppcRecoYuvTemp[uiDepth] = pcYuv; |
---|
1478 | |
---|
1479 | pcYuv = NULL; |
---|
1480 | pcCU = NULL; |
---|
1481 | |
---|
1482 | if( m_bUseSBACRD ) // store temp best CI for next CU coding |
---|
1483 | m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]); |
---|
1484 | } |
---|
1485 | } |
---|
1486 | |
---|
1487 | Void TEncCu::xCheckDQP( TComDataCU* pcCU ) |
---|
1488 | { |
---|
1489 | UInt uiDepth = pcCU->getDepth( 0 ); |
---|
1490 | |
---|
1491 | if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
1492 | { |
---|
1493 | if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) ) |
---|
1494 | { |
---|
1495 | #if !RDO_WITHOUT_DQP_BITS |
---|
1496 | m_pcEntropyCoder->resetBits(); |
---|
1497 | m_pcEntropyCoder->encodeQP( pcCU, 0, false ); |
---|
1498 | pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits |
---|
1499 | if(m_pcEncCfg->getUseSBACRD()) |
---|
1500 | { |
---|
1501 | pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
1502 | } |
---|
1503 | pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() ); |
---|
1504 | #endif |
---|
1505 | } |
---|
1506 | else |
---|
1507 | { |
---|
1508 | pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP |
---|
1509 | } |
---|
1510 | } |
---|
1511 | } |
---|
1512 | |
---|
1513 | Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst) |
---|
1514 | { |
---|
1515 | pDst->iN = pSrc->iN; |
---|
1516 | for (Int i = 0; i < pSrc->iN; i++) |
---|
1517 | { |
---|
1518 | pDst->m_acMvCand[i] = pSrc->m_acMvCand[i]; |
---|
1519 | } |
---|
1520 | } |
---|
1521 | Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY ) |
---|
1522 | { |
---|
1523 | UInt uiRPelX = uiLPelX + (g_uiMaxCUWidth>>uiDepth) - 1; |
---|
1524 | UInt uiBPelY = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1; |
---|
1525 | TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx()); |
---|
1526 | Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && |
---|
1527 | pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) ); |
---|
1528 | Bool bSliceEnd = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && |
---|
1529 | pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) ); |
---|
1530 | if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
1531 | { |
---|
1532 | UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx]; |
---|
1533 | UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth); |
---|
1534 | UInt uiBlkWidth = rpcPic->getNumPartInWidth() >> (uiDepth); |
---|
1535 | UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth; |
---|
1536 | UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth; |
---|
1537 | UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX; |
---|
1538 | m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx); |
---|
1539 | } |
---|
1540 | else |
---|
1541 | { |
---|
1542 | UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2; |
---|
1543 | |
---|
1544 | for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts ) |
---|
1545 | { |
---|
1546 | UInt uiSubCULPelX = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx & 1 ); |
---|
1547 | UInt uiSubCUTPelY = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 ); |
---|
1548 | |
---|
1549 | Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && |
---|
1550 | rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr(); |
---|
1551 | if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
1552 | { |
---|
1553 | xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY ); // Copy Yuv data to picture Yuv |
---|
1554 | } |
---|
1555 | } |
---|
1556 | } |
---|
1557 | } |
---|
1558 | |
---|
1559 | Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth ) |
---|
1560 | { |
---|
1561 | UInt uiCurrDepth = uiNextDepth - 1; |
---|
1562 | m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx ); |
---|
1563 | } |
---|
1564 | |
---|
1565 | /** Function for filling the PCM buffer of a CU using its original sample array |
---|
1566 | * \param pcCU pointer to current CU |
---|
1567 | * \param pcOrgYuv pointer to original sample array |
---|
1568 | * \returns Void |
---|
1569 | */ |
---|
1570 | Void TEncCu::xFillPCMBuffer ( TComDataCU*& pCU, TComYuv* pOrgYuv ) |
---|
1571 | { |
---|
1572 | |
---|
1573 | UInt width = pCU->getWidth(0); |
---|
1574 | UInt height = pCU->getHeight(0); |
---|
1575 | |
---|
1576 | Pel* pSrcY = pOrgYuv->getLumaAddr(0, width); |
---|
1577 | Pel* pDstY = pCU->getPCMSampleY(); |
---|
1578 | UInt srcStride = pOrgYuv->getStride(); |
---|
1579 | |
---|
1580 | for(Int y = 0; y < height; y++ ) |
---|
1581 | { |
---|
1582 | for(Int x = 0; x < width; x++ ) |
---|
1583 | { |
---|
1584 | pDstY[x] = pSrcY[x]; |
---|
1585 | } |
---|
1586 | pDstY += width; |
---|
1587 | pSrcY += srcStride; |
---|
1588 | } |
---|
1589 | |
---|
1590 | Pel* pSrcCb = pOrgYuv->getCbAddr(); |
---|
1591 | Pel* pSrcCr = pOrgYuv->getCrAddr();; |
---|
1592 | |
---|
1593 | Pel* pDstCb = pCU->getPCMSampleCb(); |
---|
1594 | Pel* pDstCr = pCU->getPCMSampleCr();; |
---|
1595 | |
---|
1596 | UInt srcStrideC = pOrgYuv->getCStride(); |
---|
1597 | UInt heightC = height >> 1; |
---|
1598 | UInt widthC = width >> 1; |
---|
1599 | |
---|
1600 | for(Int y = 0; y < heightC; y++ ) |
---|
1601 | { |
---|
1602 | for(Int x = 0; x < widthC; x++ ) |
---|
1603 | { |
---|
1604 | pDstCb[x] = pSrcCb[x]; |
---|
1605 | pDstCr[x] = pSrcCr[x]; |
---|
1606 | } |
---|
1607 | pDstCb += widthC; |
---|
1608 | pDstCr += widthC; |
---|
1609 | pSrcCb += srcStrideC; |
---|
1610 | pSrcCr += srcStrideC; |
---|
1611 | } |
---|
1612 | } |
---|
1613 | |
---|
1614 | #if ADAPTIVE_QP_SELECTION |
---|
1615 | /** Collect ARL statistics from one block |
---|
1616 | */ |
---|
1617 | Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples ) |
---|
1618 | { |
---|
1619 | for( Int n = 0; n < NumCoeffInCU; n++ ) |
---|
1620 | { |
---|
1621 | Int u = abs( rpcCoeff[ n ] ); |
---|
1622 | Int absc = rpcArlCoeff[ n ]; |
---|
1623 | |
---|
1624 | if( u != 0 ) |
---|
1625 | { |
---|
1626 | if( u < LEVEL_RANGE ) |
---|
1627 | { |
---|
1628 | cSum[ u ] += ( Double )absc; |
---|
1629 | numSamples[ u ]++; |
---|
1630 | } |
---|
1631 | else |
---|
1632 | { |
---|
1633 | cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION ); |
---|
1634 | numSamples[ LEVEL_RANGE ]++; |
---|
1635 | } |
---|
1636 | } |
---|
1637 | } |
---|
1638 | |
---|
1639 | return 0; |
---|
1640 | } |
---|
1641 | |
---|
1642 | /** Collect ARL statistics from one LCU |
---|
1643 | * \param pcCU |
---|
1644 | */ |
---|
1645 | Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU ) |
---|
1646 | { |
---|
1647 | Double cSum[ LEVEL_RANGE + 1 ]; //: the sum of DCT coefficients corresponding to datatype and quantization output |
---|
1648 | UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output |
---|
1649 | |
---|
1650 | TCoeff* pCoeffY = rpcCU->getCoeffY(); |
---|
1651 | Int* pArlCoeffY = rpcCU->getArlCoeffY(); |
---|
1652 | |
---|
1653 | UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth; |
---|
1654 | UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth; |
---|
1655 | |
---|
1656 | memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) ); |
---|
1657 | memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) ); |
---|
1658 | |
---|
1659 | // Collect stats to cSum[][] and numSamples[][] |
---|
1660 | for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ ) |
---|
1661 | { |
---|
1662 | UInt uiTrIdx = rpcCU->getTransformIdx(i); |
---|
1663 | |
---|
1664 | if(rpcCU->getPredictionMode(i) == MODE_INTER) |
---|
1665 | if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) ) |
---|
1666 | { |
---|
1667 | xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples); |
---|
1668 | }//Note that only InterY is processed. QP rounding is based on InterY data only. |
---|
1669 | |
---|
1670 | pCoeffY += uiMinNumCoeffInCU; |
---|
1671 | pArlCoeffY += uiMinNumCoeffInCU; |
---|
1672 | } |
---|
1673 | |
---|
1674 | for(Int u=1; u<LEVEL_RANGE;u++) |
---|
1675 | { |
---|
1676 | m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ; |
---|
1677 | m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ; |
---|
1678 | } |
---|
1679 | m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ; |
---|
1680 | m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ; |
---|
1681 | } |
---|
1682 | #endif |
---|
1683 | //! \} |
---|