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 && !M0036_RC_IMPROVEMENT |
---|
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 | #if SVC_EXTENSION |
---|
214 | m_ppcTEncTop = pcEncTop->getLayerEnc(); |
---|
215 | for(UInt i=0 ; i< m_uhTotalDepth-1 ; i++) |
---|
216 | { |
---|
217 | m_ppcBestCU[i]->setLayerId(pcEncTop->getLayerId()); |
---|
218 | m_ppcTempCU[i]->setLayerId(pcEncTop->getLayerId()); |
---|
219 | } |
---|
220 | #endif |
---|
221 | |
---|
222 | m_pcEntropyCoder = pcEncTop->getEntropyCoder(); |
---|
223 | m_pcCavlcCoder = pcEncTop->getCavlcCoder(); |
---|
224 | m_pcSbacCoder = pcEncTop->getSbacCoder(); |
---|
225 | m_pcBinCABAC = pcEncTop->getBinCABAC(); |
---|
226 | |
---|
227 | m_pppcRDSbacCoder = pcEncTop->getRDSbacCoder(); |
---|
228 | m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder(); |
---|
229 | |
---|
230 | m_bUseSBACRD = pcEncTop->getUseSBACRD(); |
---|
231 | m_pcRateCtrl = pcEncTop->getRateCtrl(); |
---|
232 | } |
---|
233 | |
---|
234 | // ==================================================================================================================== |
---|
235 | // Public member functions |
---|
236 | // ==================================================================================================================== |
---|
237 | |
---|
238 | /** \param rpcCU pointer of CU data class |
---|
239 | */ |
---|
240 | Void TEncCu::compressCU( TComDataCU*& rpcCU ) |
---|
241 | { |
---|
242 | // initialize CU data |
---|
243 | m_ppcBestCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() ); |
---|
244 | m_ppcTempCU[0]->initCU( rpcCU->getPic(), rpcCU->getAddr() ); |
---|
245 | |
---|
246 | #if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT |
---|
247 | m_addSADDepth = 0; |
---|
248 | m_LCUPredictionSAD = 0; |
---|
249 | m_temporalSAD = 0; |
---|
250 | #endif |
---|
251 | |
---|
252 | // analysis of CU |
---|
253 | xCompressCU( m_ppcBestCU[0], m_ppcTempCU[0], 0 ); |
---|
254 | |
---|
255 | #if ADAPTIVE_QP_SELECTION |
---|
256 | if( m_pcEncCfg->getUseAdaptQpSelect() ) |
---|
257 | { |
---|
258 | if(rpcCU->getSlice()->getSliceType()!=I_SLICE) //IIII |
---|
259 | { |
---|
260 | xLcuCollectARLStats( rpcCU); |
---|
261 | } |
---|
262 | } |
---|
263 | #endif |
---|
264 | } |
---|
265 | /** \param pcCU pointer of CU data class |
---|
266 | */ |
---|
267 | Void TEncCu::encodeCU ( TComDataCU* pcCU ) |
---|
268 | { |
---|
269 | if ( pcCU->getSlice()->getPPS()->getUseDQP() ) |
---|
270 | { |
---|
271 | setdQPFlag(true); |
---|
272 | } |
---|
273 | |
---|
274 | // Encode CU data |
---|
275 | xEncodeCU( pcCU, 0, 0 ); |
---|
276 | } |
---|
277 | |
---|
278 | // ==================================================================================================================== |
---|
279 | // Protected member functions |
---|
280 | // ==================================================================================================================== |
---|
281 | /** Derive small set of test modes for AMP encoder speed-up |
---|
282 | *\param rpcBestCU |
---|
283 | *\param eParentPartSize |
---|
284 | *\param bTestAMP_Hor |
---|
285 | *\param bTestAMP_Ver |
---|
286 | *\param bTestMergeAMP_Hor |
---|
287 | *\param bTestMergeAMP_Ver |
---|
288 | *\returns Void |
---|
289 | */ |
---|
290 | #if AMP_ENC_SPEEDUP |
---|
291 | #if AMP_MRG |
---|
292 | Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver, Bool &bTestMergeAMP_Hor, Bool &bTestMergeAMP_Ver) |
---|
293 | #else |
---|
294 | Void TEncCu::deriveTestModeAMP (TComDataCU *&rpcBestCU, PartSize eParentPartSize, Bool &bTestAMP_Hor, Bool &bTestAMP_Ver) |
---|
295 | #endif |
---|
296 | { |
---|
297 | if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN ) |
---|
298 | { |
---|
299 | bTestAMP_Hor = true; |
---|
300 | } |
---|
301 | else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N ) |
---|
302 | { |
---|
303 | bTestAMP_Ver = true; |
---|
304 | } |
---|
305 | else if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->getMergeFlag(0) == false && rpcBestCU->isSkipped(0) == false ) |
---|
306 | { |
---|
307 | bTestAMP_Hor = true; |
---|
308 | bTestAMP_Ver = true; |
---|
309 | } |
---|
310 | |
---|
311 | #if AMP_MRG |
---|
312 | //! Utilizing the partition size of parent PU |
---|
313 | if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N ) |
---|
314 | { |
---|
315 | bTestMergeAMP_Hor = true; |
---|
316 | bTestMergeAMP_Ver = true; |
---|
317 | } |
---|
318 | |
---|
319 | if ( eParentPartSize == SIZE_NONE ) //! if parent is intra |
---|
320 | { |
---|
321 | if ( rpcBestCU->getPartitionSize(0) == SIZE_2NxN ) |
---|
322 | { |
---|
323 | bTestMergeAMP_Hor = true; |
---|
324 | } |
---|
325 | else if ( rpcBestCU->getPartitionSize(0) == SIZE_Nx2N ) |
---|
326 | { |
---|
327 | bTestMergeAMP_Ver = true; |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | if ( rpcBestCU->getPartitionSize(0) == SIZE_2Nx2N && rpcBestCU->isSkipped(0) == false ) |
---|
332 | { |
---|
333 | bTestMergeAMP_Hor = true; |
---|
334 | bTestMergeAMP_Ver = true; |
---|
335 | } |
---|
336 | |
---|
337 | if ( rpcBestCU->getWidth(0) == 64 ) |
---|
338 | { |
---|
339 | bTestAMP_Hor = false; |
---|
340 | bTestAMP_Ver = false; |
---|
341 | } |
---|
342 | #else |
---|
343 | //! Utilizing the partition size of parent PU |
---|
344 | if ( eParentPartSize >= SIZE_2NxnU && eParentPartSize <= SIZE_nRx2N ) |
---|
345 | { |
---|
346 | bTestAMP_Hor = true; |
---|
347 | bTestAMP_Ver = true; |
---|
348 | } |
---|
349 | |
---|
350 | if ( eParentPartSize == SIZE_2Nx2N ) |
---|
351 | { |
---|
352 | bTestAMP_Hor = false; |
---|
353 | bTestAMP_Ver = false; |
---|
354 | } |
---|
355 | #endif |
---|
356 | } |
---|
357 | #endif |
---|
358 | |
---|
359 | // ==================================================================================================================== |
---|
360 | // Protected member functions |
---|
361 | // ==================================================================================================================== |
---|
362 | /** Compress a CU block recursively with enabling sub-LCU-level delta QP |
---|
363 | *\param rpcBestCU |
---|
364 | *\param rpcTempCU |
---|
365 | *\param uiDepth |
---|
366 | *\returns Void |
---|
367 | * |
---|
368 | *- for loop of QP value to compress the current CU with all possible QP |
---|
369 | */ |
---|
370 | #if AMP_ENC_SPEEDUP |
---|
371 | Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth, PartSize eParentPartSize ) |
---|
372 | #else |
---|
373 | Void TEncCu::xCompressCU( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth ) |
---|
374 | #endif |
---|
375 | { |
---|
376 | TComPic* pcPic = rpcBestCU->getPic(); |
---|
377 | |
---|
378 | // get Original YUV data from picture |
---|
379 | m_ppcOrigYuv[uiDepth]->copyFromPicYuv( pcPic->getPicYuvOrg(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU() ); |
---|
380 | |
---|
381 | // variables for fast encoder decision |
---|
382 | Bool bEarlySkip = false; |
---|
383 | Bool bTrySplit = true; |
---|
384 | Double fRD_Skip = MAX_DOUBLE; |
---|
385 | |
---|
386 | // variable for Early CU determination |
---|
387 | Bool bSubBranch = true; |
---|
388 | |
---|
389 | // variable for Cbf fast mode PU decision |
---|
390 | Bool doNotBlockPu = true; |
---|
391 | Bool earlyDetectionSkipMode = false; |
---|
392 | |
---|
393 | Bool bTrySplitDQP = true; |
---|
394 | |
---|
395 | static Double afCost[ MAX_CU_DEPTH ]; |
---|
396 | static Int aiNum [ MAX_CU_DEPTH ]; |
---|
397 | |
---|
398 | if ( rpcBestCU->getAddr() == 0 ) |
---|
399 | { |
---|
400 | ::memset( afCost, 0, sizeof( afCost ) ); |
---|
401 | ::memset( aiNum, 0, sizeof( aiNum ) ); |
---|
402 | } |
---|
403 | |
---|
404 | Bool bBoundary = false; |
---|
405 | UInt uiLPelX = rpcBestCU->getCUPelX(); |
---|
406 | UInt uiRPelX = uiLPelX + rpcBestCU->getWidth(0) - 1; |
---|
407 | UInt uiTPelY = rpcBestCU->getCUPelY(); |
---|
408 | UInt uiBPelY = uiTPelY + rpcBestCU->getHeight(0) - 1; |
---|
409 | |
---|
410 | Int iBaseQP = xComputeQP( rpcBestCU, uiDepth ); |
---|
411 | Int iMinQP; |
---|
412 | Int iMaxQP; |
---|
413 | Bool isAddLowestQP = false; |
---|
414 | Int lowestQP = -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(); |
---|
415 | |
---|
416 | if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
417 | { |
---|
418 | Int idQP = m_pcEncCfg->getMaxDeltaQP(); |
---|
419 | iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP ); |
---|
420 | iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP ); |
---|
421 | if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() ) |
---|
422 | { |
---|
423 | isAddLowestQP = true; |
---|
424 | iMinQP = iMinQP - 1; |
---|
425 | } |
---|
426 | } |
---|
427 | else |
---|
428 | { |
---|
429 | iMinQP = rpcTempCU->getQP(0); |
---|
430 | iMaxQP = rpcTempCU->getQP(0); |
---|
431 | } |
---|
432 | |
---|
433 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
434 | if ( m_pcEncCfg->getUseRateCtrl() ) |
---|
435 | { |
---|
436 | iMinQP = m_pcRateCtrl->getRCQP(); |
---|
437 | iMaxQP = m_pcRateCtrl->getRCQP(); |
---|
438 | } |
---|
439 | #else |
---|
440 | if(m_pcEncCfg->getUseRateCtrl()) |
---|
441 | { |
---|
442 | Int qp = m_pcRateCtrl->getUnitQP(); |
---|
443 | iMinQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
444 | iMaxQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
445 | } |
---|
446 | #endif |
---|
447 | |
---|
448 | // If slice start or slice end is within this cu... |
---|
449 | TComSlice * pcSlice = rpcTempCU->getPic()->getSlice(rpcTempCU->getPic()->getCurrSliceIdx()); |
---|
450 | Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurStartCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart(); |
---|
451 | Bool bSliceEnd = (pcSlice->getSliceSegmentCurEndCUAddr()>rpcTempCU->getSCUAddr()&&pcSlice->getSliceSegmentCurEndCUAddr()<rpcTempCU->getSCUAddr()+rpcTempCU->getTotalNumPart()); |
---|
452 | Bool bInsidePicture = ( uiRPelX < rpcBestCU->getSlice()->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < rpcBestCU->getSlice()->getSPS()->getPicHeightInLumaSamples() ); |
---|
453 | // We need to split, so don't try these modes. |
---|
454 | if(!bSliceEnd && !bSliceStart && bInsidePicture ) |
---|
455 | { |
---|
456 | #if (ENCODER_FAST_MODE) |
---|
457 | Bool testInter = true; |
---|
458 | if (rpcBestCU->getLayerId() > 0) |
---|
459 | { |
---|
460 | if(pcSlice->getSliceType() == P_SLICE && pcSlice->getNumRefIdx(REF_PIC_LIST_0) == pcSlice->getActiveNumILRRefIdx()) |
---|
461 | testInter = false; |
---|
462 | if(pcSlice->getSliceType() == B_SLICE && pcSlice->getNumRefIdx(REF_PIC_LIST_0) == pcSlice->getActiveNumILRRefIdx() && pcSlice->getNumRefIdx(REF_PIC_LIST_1) == pcSlice->getActiveNumILRRefIdx()) |
---|
463 | testInter = false; |
---|
464 | #if M0457_IL_SAMPLE_PRED_ONLY_FLAG |
---|
465 | if( pcSlice->getInterLayerSamplePredOnlyFlag() ) |
---|
466 | testInter = false; |
---|
467 | #endif |
---|
468 | } |
---|
469 | #endif |
---|
470 | for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++) |
---|
471 | { |
---|
472 | if (isAddLowestQP && (iQP == iMinQP)) |
---|
473 | { |
---|
474 | iQP = lowestQP; |
---|
475 | } |
---|
476 | // variables for fast encoder decision |
---|
477 | bEarlySkip = false; |
---|
478 | bTrySplit = true; |
---|
479 | fRD_Skip = MAX_DOUBLE; |
---|
480 | |
---|
481 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
482 | |
---|
483 | // do inter modes, SKIP and 2Nx2N |
---|
484 | #if (ENCODER_FAST_MODE == 1) |
---|
485 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE && testInter ) |
---|
486 | #else |
---|
487 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE ) |
---|
488 | #endif |
---|
489 | { |
---|
490 | // 2Nx2N |
---|
491 | if(m_pcEncCfg->getUseEarlySkipDetection()) |
---|
492 | { |
---|
493 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); rpcTempCU->initEstData( uiDepth, iQP );//by Competition for inter_2Nx2N |
---|
494 | } |
---|
495 | // SKIP |
---|
496 | xCheckRDCostMerge2Nx2N( rpcBestCU, rpcTempCU, &earlyDetectionSkipMode );//by Merge for inter_2Nx2N |
---|
497 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
498 | |
---|
499 | // fast encoder decision for early skip |
---|
500 | if ( m_pcEncCfg->getUseFastEnc() ) |
---|
501 | { |
---|
502 | Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ]; |
---|
503 | if ( aiNum [ iIdx ] > 5 && fRD_Skip < EARLY_SKIP_THRES*afCost[ iIdx ]/aiNum[ iIdx ] ) |
---|
504 | { |
---|
505 | bEarlySkip = true; |
---|
506 | bTrySplit = false; |
---|
507 | } |
---|
508 | } |
---|
509 | #if (ENCODER_FAST_MODE == 2) |
---|
510 | if (testInter) |
---|
511 | { |
---|
512 | #endif |
---|
513 | |
---|
514 | if(!m_pcEncCfg->getUseEarlySkipDetection()) |
---|
515 | { |
---|
516 | // 2Nx2N, NxN |
---|
517 | if ( !bEarlySkip ) |
---|
518 | { |
---|
519 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); rpcTempCU->initEstData( uiDepth, iQP ); |
---|
520 | if(m_pcEncCfg->getUseCbfFastMode()) |
---|
521 | { |
---|
522 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
523 | } |
---|
524 | } |
---|
525 | } |
---|
526 | #if (ENCODER_FAST_MODE == 2) |
---|
527 | } |
---|
528 | #endif |
---|
529 | |
---|
530 | } |
---|
531 | |
---|
532 | if( (g_uiMaxCUWidth>>uiDepth) >= rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
533 | { |
---|
534 | if(iQP == iBaseQP) |
---|
535 | { |
---|
536 | bTrySplitDQP = bTrySplit; |
---|
537 | } |
---|
538 | } |
---|
539 | else |
---|
540 | { |
---|
541 | bTrySplitDQP = bTrySplit; |
---|
542 | } |
---|
543 | if (isAddLowestQP && (iQP == lowestQP)) |
---|
544 | { |
---|
545 | iQP = iMinQP; |
---|
546 | } |
---|
547 | } |
---|
548 | |
---|
549 | #if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT |
---|
550 | if ( uiDepth <= m_addSADDepth ) |
---|
551 | { |
---|
552 | m_LCUPredictionSAD += m_temporalSAD; |
---|
553 | m_addSADDepth = uiDepth; |
---|
554 | } |
---|
555 | #endif |
---|
556 | |
---|
557 | if(!earlyDetectionSkipMode) |
---|
558 | { |
---|
559 | for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++) |
---|
560 | { |
---|
561 | if (isAddLowestQP && (iQP == iMinQP)) |
---|
562 | { |
---|
563 | iQP = lowestQP; |
---|
564 | } |
---|
565 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
566 | |
---|
567 | // do inter modes, NxN, 2NxN, and Nx2N |
---|
568 | #if (ENCODER_FAST_MODE) |
---|
569 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE && testInter ) |
---|
570 | #else |
---|
571 | if( rpcBestCU->getSlice()->getSliceType() != I_SLICE ) |
---|
572 | #endif |
---|
573 | { |
---|
574 | // 2Nx2N, NxN |
---|
575 | if ( !bEarlySkip ) |
---|
576 | { |
---|
577 | if(!( (rpcBestCU->getWidth(0)==8) && (rpcBestCU->getHeight(0)==8) )) |
---|
578 | { |
---|
579 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth && doNotBlockPu) |
---|
580 | { |
---|
581 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_NxN ); |
---|
582 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
583 | } |
---|
584 | } |
---|
585 | } |
---|
586 | |
---|
587 | // 2NxN, Nx2N |
---|
588 | if(doNotBlockPu) |
---|
589 | { |
---|
590 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_Nx2N ); |
---|
591 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
592 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_Nx2N ) |
---|
593 | { |
---|
594 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
595 | } |
---|
596 | } |
---|
597 | if(doNotBlockPu) |
---|
598 | { |
---|
599 | xCheckRDCostInter ( rpcBestCU, rpcTempCU, SIZE_2NxN ); |
---|
600 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
601 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxN) |
---|
602 | { |
---|
603 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | #if 1 |
---|
608 | //! Try AMP (SIZE_2NxnU, SIZE_2NxnD, SIZE_nLx2N, SIZE_nRx2N) |
---|
609 | if( pcPic->getSlice(0)->getSPS()->getAMPAcc(uiDepth) ) |
---|
610 | { |
---|
611 | #if AMP_ENC_SPEEDUP |
---|
612 | Bool bTestAMP_Hor = false, bTestAMP_Ver = false; |
---|
613 | |
---|
614 | #if AMP_MRG |
---|
615 | Bool bTestMergeAMP_Hor = false, bTestMergeAMP_Ver = false; |
---|
616 | |
---|
617 | deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver, bTestMergeAMP_Hor, bTestMergeAMP_Ver); |
---|
618 | #else |
---|
619 | deriveTestModeAMP (rpcBestCU, eParentPartSize, bTestAMP_Hor, bTestAMP_Ver); |
---|
620 | #endif |
---|
621 | |
---|
622 | //! Do horizontal AMP |
---|
623 | if ( bTestAMP_Hor ) |
---|
624 | { |
---|
625 | if(doNotBlockPu) |
---|
626 | { |
---|
627 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU ); |
---|
628 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
629 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU ) |
---|
630 | { |
---|
631 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
632 | } |
---|
633 | } |
---|
634 | if(doNotBlockPu) |
---|
635 | { |
---|
636 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD ); |
---|
637 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
638 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD ) |
---|
639 | { |
---|
640 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
641 | } |
---|
642 | } |
---|
643 | } |
---|
644 | #if AMP_MRG |
---|
645 | else if ( bTestMergeAMP_Hor ) |
---|
646 | { |
---|
647 | if(doNotBlockPu) |
---|
648 | { |
---|
649 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU, true ); |
---|
650 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
651 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnU ) |
---|
652 | { |
---|
653 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
654 | } |
---|
655 | } |
---|
656 | if(doNotBlockPu) |
---|
657 | { |
---|
658 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD, true ); |
---|
659 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
660 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_2NxnD ) |
---|
661 | { |
---|
662 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
663 | } |
---|
664 | } |
---|
665 | } |
---|
666 | #endif |
---|
667 | |
---|
668 | //! Do horizontal AMP |
---|
669 | if ( bTestAMP_Ver ) |
---|
670 | { |
---|
671 | if(doNotBlockPu) |
---|
672 | { |
---|
673 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N ); |
---|
674 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
675 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N ) |
---|
676 | { |
---|
677 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
678 | } |
---|
679 | } |
---|
680 | if(doNotBlockPu) |
---|
681 | { |
---|
682 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N ); |
---|
683 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
684 | } |
---|
685 | } |
---|
686 | #if AMP_MRG |
---|
687 | else if ( bTestMergeAMP_Ver ) |
---|
688 | { |
---|
689 | if(doNotBlockPu) |
---|
690 | { |
---|
691 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N, true ); |
---|
692 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
693 | if(m_pcEncCfg->getUseCbfFastMode() && rpcBestCU->getPartitionSize(0) == SIZE_nLx2N ) |
---|
694 | { |
---|
695 | doNotBlockPu = rpcBestCU->getQtRootCbf( 0 ) != 0; |
---|
696 | } |
---|
697 | } |
---|
698 | if(doNotBlockPu) |
---|
699 | { |
---|
700 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N, true ); |
---|
701 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
702 | } |
---|
703 | } |
---|
704 | #endif |
---|
705 | |
---|
706 | #else |
---|
707 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnU ); |
---|
708 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
709 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_2NxnD ); |
---|
710 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
711 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nLx2N ); |
---|
712 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
713 | |
---|
714 | xCheckRDCostInter( rpcBestCU, rpcTempCU, SIZE_nRx2N ); |
---|
715 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
716 | |
---|
717 | #endif |
---|
718 | } |
---|
719 | #endif |
---|
720 | } |
---|
721 | |
---|
722 | // do normal intra modes |
---|
723 | if ( !bEarlySkip ) |
---|
724 | { |
---|
725 | // speedup for inter frames |
---|
726 | #if (ENCODER_FAST_MODE) |
---|
727 | if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || |
---|
728 | !testInter || |
---|
729 | rpcBestCU->getCbf( 0, TEXT_LUMA ) != 0 || |
---|
730 | rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0 || |
---|
731 | rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0 ) // avoid very complex intra if it is unlikely |
---|
732 | #else |
---|
733 | if( rpcBestCU->getSlice()->getSliceType() == I_SLICE || |
---|
734 | rpcBestCU->getCbf( 0, TEXT_LUMA ) != 0 || |
---|
735 | rpcBestCU->getCbf( 0, TEXT_CHROMA_U ) != 0 || |
---|
736 | rpcBestCU->getCbf( 0, TEXT_CHROMA_V ) != 0 ) // avoid very complex intra if it is unlikely |
---|
737 | #endif |
---|
738 | { |
---|
739 | xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_2Nx2N ); |
---|
740 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
741 | if( uiDepth == g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
742 | { |
---|
743 | if( rpcTempCU->getWidth(0) > ( 1 << rpcTempCU->getSlice()->getSPS()->getQuadtreeTULog2MinSize() ) ) |
---|
744 | { |
---|
745 | xCheckRDCostIntra( rpcBestCU, rpcTempCU, SIZE_NxN ); |
---|
746 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
747 | } |
---|
748 | } |
---|
749 | } |
---|
750 | } |
---|
751 | |
---|
752 | // test PCM |
---|
753 | if(pcPic->getSlice(0)->getSPS()->getUsePCM() |
---|
754 | && rpcTempCU->getWidth(0) <= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MaxSize()) |
---|
755 | && rpcTempCU->getWidth(0) >= (1<<pcPic->getSlice(0)->getSPS()->getPCMLog2MinSize()) ) |
---|
756 | { |
---|
757 | UInt uiRawBits = (2 * g_bitDepthY + g_bitDepthC) * rpcBestCU->getWidth(0) * rpcBestCU->getHeight(0) / 2; |
---|
758 | UInt uiBestBits = rpcBestCU->getTotalBits(); |
---|
759 | if((uiBestBits > uiRawBits) || (rpcBestCU->getTotalCost() > m_pcRdCost->calcRdCost(uiRawBits, 0))) |
---|
760 | { |
---|
761 | xCheckIntraPCM (rpcBestCU, rpcTempCU); |
---|
762 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
763 | } |
---|
764 | } |
---|
765 | #if INTRA_BL |
---|
766 | if(m_pcPicYuvRecBase) |
---|
767 | { |
---|
768 | xCheckRDCostIntraBL( rpcBestCU, rpcTempCU ); |
---|
769 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
770 | } |
---|
771 | #endif |
---|
772 | #if (ENCODER_FAST_MODE) |
---|
773 | if(pcPic->getLayerId() > 0) |
---|
774 | { |
---|
775 | for(Int refLayer = 0; refLayer < pcSlice->getActiveNumILRRefIdx(); refLayer++) |
---|
776 | { |
---|
777 | xCheckRDCostILRUni( rpcBestCU, rpcTempCU, pcSlice->getInterLayerPredLayerIdc(refLayer)); |
---|
778 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
779 | } |
---|
780 | } |
---|
781 | #endif |
---|
782 | |
---|
783 | if (isAddLowestQP && (iQP == lowestQP)) |
---|
784 | { |
---|
785 | iQP = iMinQP; |
---|
786 | } |
---|
787 | } |
---|
788 | } |
---|
789 | |
---|
790 | m_pcEntropyCoder->resetBits(); |
---|
791 | m_pcEntropyCoder->encodeSplitFlag( rpcBestCU, 0, uiDepth, true ); |
---|
792 | rpcBestCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits |
---|
793 | if(m_pcEncCfg->getUseSBACRD()) |
---|
794 | { |
---|
795 | rpcBestCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
796 | } |
---|
797 | rpcBestCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcBestCU->getTotalBits(), rpcBestCU->getTotalDistortion() ); |
---|
798 | |
---|
799 | // accumulate statistics for early skip |
---|
800 | if ( m_pcEncCfg->getUseFastEnc() ) |
---|
801 | { |
---|
802 | if ( rpcBestCU->isSkipped(0) ) |
---|
803 | { |
---|
804 | Int iIdx = g_aucConvertToBit[ rpcBestCU->getWidth(0) ]; |
---|
805 | afCost[ iIdx ] += rpcBestCU->getTotalCost(); |
---|
806 | aiNum [ iIdx ] ++; |
---|
807 | } |
---|
808 | } |
---|
809 | |
---|
810 | // Early CU determination |
---|
811 | if( m_pcEncCfg->getUseEarlyCU() && rpcBestCU->isSkipped(0) ) |
---|
812 | { |
---|
813 | bSubBranch = false; |
---|
814 | } |
---|
815 | else |
---|
816 | { |
---|
817 | bSubBranch = true; |
---|
818 | } |
---|
819 | } |
---|
820 | else if(!(bSliceEnd && bInsidePicture)) |
---|
821 | { |
---|
822 | bBoundary = true; |
---|
823 | #if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT |
---|
824 | m_addSADDepth++; |
---|
825 | #endif |
---|
826 | } |
---|
827 | |
---|
828 | // copy orginal YUV samples to PCM buffer |
---|
829 | if( rpcBestCU->isLosslessCoded(0) && (rpcBestCU->getIPCMFlag(0) == false)) |
---|
830 | { |
---|
831 | xFillPCMBuffer(rpcBestCU, m_ppcOrigYuv[uiDepth]); |
---|
832 | } |
---|
833 | if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
834 | { |
---|
835 | Int idQP = m_pcEncCfg->getMaxDeltaQP(); |
---|
836 | iMinQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP-idQP ); |
---|
837 | iMaxQP = Clip3( -rpcTempCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQP+idQP ); |
---|
838 | if ( (rpcTempCU->getSlice()->getSPS()->getUseLossless()) && (lowestQP < iMinQP) && rpcTempCU->getSlice()->getPPS()->getUseDQP() ) |
---|
839 | { |
---|
840 | isAddLowestQP = true; |
---|
841 | iMinQP = iMinQP - 1; |
---|
842 | } |
---|
843 | } |
---|
844 | else if( (g_uiMaxCUWidth>>uiDepth) > rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
845 | { |
---|
846 | iMinQP = iBaseQP; |
---|
847 | iMaxQP = iBaseQP; |
---|
848 | } |
---|
849 | else |
---|
850 | { |
---|
851 | Int iStartQP; |
---|
852 | if( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) == pcSlice->getSliceSegmentCurStartCUAddr()) |
---|
853 | { |
---|
854 | iStartQP = rpcTempCU->getQP(0); |
---|
855 | } |
---|
856 | else |
---|
857 | { |
---|
858 | UInt uiCurSliceStartPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU(); |
---|
859 | iStartQP = rpcTempCU->getQP(uiCurSliceStartPartIdx); |
---|
860 | } |
---|
861 | iMinQP = iStartQP; |
---|
862 | iMaxQP = iStartQP; |
---|
863 | } |
---|
864 | #if RATE_CONTROL_LAMBDA_DOMAIN |
---|
865 | if ( m_pcEncCfg->getUseRateCtrl() ) |
---|
866 | { |
---|
867 | iMinQP = m_pcRateCtrl->getRCQP(); |
---|
868 | iMaxQP = m_pcRateCtrl->getRCQP(); |
---|
869 | } |
---|
870 | #else |
---|
871 | if(m_pcEncCfg->getUseRateCtrl()) |
---|
872 | { |
---|
873 | Int qp = m_pcRateCtrl->getUnitQP(); |
---|
874 | iMinQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
875 | iMaxQP = Clip3( MIN_QP, MAX_QP, qp); |
---|
876 | } |
---|
877 | #endif |
---|
878 | for (Int iQP=iMinQP; iQP<=iMaxQP; iQP++) |
---|
879 | { |
---|
880 | if (isAddLowestQP && (iQP == iMinQP)) |
---|
881 | { |
---|
882 | iQP = lowestQP; |
---|
883 | } |
---|
884 | rpcTempCU->initEstData( uiDepth, iQP ); |
---|
885 | |
---|
886 | // further split |
---|
887 | if( bSubBranch && bTrySplitDQP && uiDepth < g_uiMaxCUDepth - g_uiAddCUDepth ) |
---|
888 | { |
---|
889 | UChar uhNextDepth = uiDepth+1; |
---|
890 | TComDataCU* pcSubBestPartCU = m_ppcBestCU[uhNextDepth]; |
---|
891 | TComDataCU* pcSubTempPartCU = m_ppcTempCU[uhNextDepth]; |
---|
892 | |
---|
893 | for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++ ) |
---|
894 | { |
---|
895 | pcSubBestPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP ); // clear sub partition datas or init. |
---|
896 | pcSubTempPartCU->initSubCU( rpcTempCU, uiPartUnitIdx, uhNextDepth, iQP ); // clear sub partition datas or init. |
---|
897 | |
---|
898 | Bool bInSlice = pcSubBestPartCU->getSCUAddr()+pcSubBestPartCU->getTotalNumPart()>pcSlice->getSliceSegmentCurStartCUAddr()&&pcSubBestPartCU->getSCUAddr()<pcSlice->getSliceSegmentCurEndCUAddr(); |
---|
899 | if(bInSlice && ( pcSubBestPartCU->getCUPelX() < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( pcSubBestPartCU->getCUPelY() < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
900 | { |
---|
901 | if( m_bUseSBACRD ) |
---|
902 | { |
---|
903 | if ( 0 == uiPartUnitIdx) //initialize RD with previous depth buffer |
---|
904 | { |
---|
905 | m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]); |
---|
906 | } |
---|
907 | else |
---|
908 | { |
---|
909 | m_pppcRDSbacCoder[uhNextDepth][CI_CURR_BEST]->load(m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]); |
---|
910 | } |
---|
911 | } |
---|
912 | |
---|
913 | #if AMP_ENC_SPEEDUP |
---|
914 | if ( rpcBestCU->isIntra(0) ) |
---|
915 | { |
---|
916 | xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, SIZE_NONE ); |
---|
917 | } |
---|
918 | else |
---|
919 | { |
---|
920 | xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth, rpcBestCU->getPartitionSize(0) ); |
---|
921 | } |
---|
922 | #else |
---|
923 | xCompressCU( pcSubBestPartCU, pcSubTempPartCU, uhNextDepth ); |
---|
924 | #endif |
---|
925 | |
---|
926 | rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth ); // Keep best part data to current temporary data. |
---|
927 | xCopyYuv2Tmp( pcSubBestPartCU->getTotalNumPart()*uiPartUnitIdx, uhNextDepth ); |
---|
928 | } |
---|
929 | else if (bInSlice) |
---|
930 | { |
---|
931 | pcSubBestPartCU->copyToPic( uhNextDepth ); |
---|
932 | rpcTempCU->copyPartFrom( pcSubBestPartCU, uiPartUnitIdx, uhNextDepth ); |
---|
933 | } |
---|
934 | } |
---|
935 | |
---|
936 | if( !bBoundary ) |
---|
937 | { |
---|
938 | m_pcEntropyCoder->resetBits(); |
---|
939 | m_pcEntropyCoder->encodeSplitFlag( rpcTempCU, 0, uiDepth, true ); |
---|
940 | |
---|
941 | rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // split bits |
---|
942 | if(m_pcEncCfg->getUseSBACRD()) |
---|
943 | { |
---|
944 | rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
945 | } |
---|
946 | } |
---|
947 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
948 | |
---|
949 | if( (g_uiMaxCUWidth>>uiDepth) == rpcTempCU->getSlice()->getPPS()->getMinCuDQPSize() && rpcTempCU->getSlice()->getPPS()->getUseDQP()) |
---|
950 | { |
---|
951 | Bool hasResidual = false; |
---|
952 | for( UInt uiBlkIdx = 0; uiBlkIdx < rpcTempCU->getTotalNumPart(); uiBlkIdx ++) |
---|
953 | { |
---|
954 | if( ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(uiBlkIdx+rpcTempCU->getZorderIdxInCU()) == rpcTempCU->getSlice()->getSliceSegmentCurStartCUAddr() ) && |
---|
955 | ( rpcTempCU->getCbf( uiBlkIdx, TEXT_LUMA ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_U ) || rpcTempCU->getCbf( uiBlkIdx, TEXT_CHROMA_V ) ) ) |
---|
956 | { |
---|
957 | hasResidual = true; |
---|
958 | break; |
---|
959 | } |
---|
960 | } |
---|
961 | |
---|
962 | UInt uiTargetPartIdx; |
---|
963 | if ( pcPic->getCU( rpcTempCU->getAddr() )->getSliceSegmentStartCU(rpcTempCU->getZorderIdxInCU()) != pcSlice->getSliceSegmentCurStartCUAddr() ) |
---|
964 | { |
---|
965 | uiTargetPartIdx = pcSlice->getSliceSegmentCurStartCUAddr() % pcPic->getNumPartInCU() - rpcTempCU->getZorderIdxInCU(); |
---|
966 | } |
---|
967 | else |
---|
968 | { |
---|
969 | uiTargetPartIdx = 0; |
---|
970 | } |
---|
971 | if ( hasResidual ) |
---|
972 | { |
---|
973 | #if !RDO_WITHOUT_DQP_BITS |
---|
974 | m_pcEntropyCoder->resetBits(); |
---|
975 | m_pcEntropyCoder->encodeQP( rpcTempCU, uiTargetPartIdx, false ); |
---|
976 | rpcTempCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits |
---|
977 | if(m_pcEncCfg->getUseSBACRD()) |
---|
978 | { |
---|
979 | rpcTempCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
980 | } |
---|
981 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
982 | #endif |
---|
983 | |
---|
984 | Bool foundNonZeroCbf = false; |
---|
985 | rpcTempCU->setQPSubCUs( rpcTempCU->getRefQP( uiTargetPartIdx ), rpcTempCU, 0, uiDepth, foundNonZeroCbf ); |
---|
986 | assert( foundNonZeroCbf ); |
---|
987 | } |
---|
988 | else |
---|
989 | { |
---|
990 | rpcTempCU->setQPSubParts( rpcTempCU->getRefQP( uiTargetPartIdx ), 0, uiDepth ); // set QP to default QP |
---|
991 | } |
---|
992 | } |
---|
993 | |
---|
994 | if( m_bUseSBACRD ) |
---|
995 | { |
---|
996 | m_pppcRDSbacCoder[uhNextDepth][CI_NEXT_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
997 | } |
---|
998 | Bool isEndOfSlice = rpcBestCU->getSlice()->getSliceMode()==FIXED_NUMBER_OF_BYTES |
---|
999 | && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceArgument()<<3); |
---|
1000 | Bool isEndOfSliceSegment = rpcBestCU->getSlice()->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES |
---|
1001 | && (rpcBestCU->getTotalBits()>rpcBestCU->getSlice()->getSliceSegmentArgument()<<3); |
---|
1002 | if(isEndOfSlice||isEndOfSliceSegment) |
---|
1003 | { |
---|
1004 | rpcBestCU->getTotalCost()=rpcTempCU->getTotalCost()+1; |
---|
1005 | } |
---|
1006 | xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth); // RD compare current larger prediction |
---|
1007 | } // with sub partitioned prediction. |
---|
1008 | if (isAddLowestQP && (iQP == lowestQP)) |
---|
1009 | { |
---|
1010 | iQP = iMinQP; |
---|
1011 | } |
---|
1012 | } |
---|
1013 | |
---|
1014 | rpcBestCU->copyToPic(uiDepth); // Copy Best data to Picture for next partition prediction. |
---|
1015 | |
---|
1016 | xCopyYuv2Pic( rpcBestCU->getPic(), rpcBestCU->getAddr(), rpcBestCU->getZorderIdxInCU(), uiDepth, uiDepth, rpcBestCU, uiLPelX, uiTPelY ); // Copy Yuv data to picture Yuv |
---|
1017 | if( bBoundary ||(bSliceEnd && bInsidePicture)) |
---|
1018 | { |
---|
1019 | return; |
---|
1020 | } |
---|
1021 | |
---|
1022 | // Assert if Best prediction mode is NONE |
---|
1023 | // Selected mode's RD-cost must be not MAX_DOUBLE. |
---|
1024 | assert( rpcBestCU->getPartitionSize ( 0 ) != SIZE_NONE ); |
---|
1025 | assert( rpcBestCU->getPredictionMode( 0 ) != MODE_NONE ); |
---|
1026 | assert( rpcBestCU->getTotalCost ( ) != MAX_DOUBLE ); |
---|
1027 | } |
---|
1028 | |
---|
1029 | /** finish encoding a cu and handle end-of-slice conditions |
---|
1030 | * \param pcCU |
---|
1031 | * \param uiAbsPartIdx |
---|
1032 | * \param uiDepth |
---|
1033 | * \returns Void |
---|
1034 | */ |
---|
1035 | Void TEncCu::finishCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
1036 | { |
---|
1037 | TComPic* pcPic = pcCU->getPic(); |
---|
1038 | TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx()); |
---|
1039 | |
---|
1040 | //Calculate end address |
---|
1041 | UInt uiCUAddr = pcCU->getSCUAddr()+uiAbsPartIdx; |
---|
1042 | |
---|
1043 | UInt uiInternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) % pcPic->getNumPartInCU(); |
---|
1044 | UInt uiExternalAddress = pcPic->getPicSym()->getPicSCUAddr(pcSlice->getSliceSegmentCurEndCUAddr()-1) / pcPic->getNumPartInCU(); |
---|
1045 | UInt uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
1046 | UInt uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
1047 | UInt uiWidth = pcSlice->getSPS()->getPicWidthInLumaSamples(); |
---|
1048 | UInt uiHeight = pcSlice->getSPS()->getPicHeightInLumaSamples(); |
---|
1049 | while(uiPosX>=uiWidth||uiPosY>=uiHeight) |
---|
1050 | { |
---|
1051 | uiInternalAddress--; |
---|
1052 | uiPosX = ( uiExternalAddress % pcPic->getFrameWidthInCU() ) * g_uiMaxCUWidth+ g_auiRasterToPelX[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
1053 | uiPosY = ( uiExternalAddress / pcPic->getFrameWidthInCU() ) * g_uiMaxCUHeight+ g_auiRasterToPelY[ g_auiZscanToRaster[uiInternalAddress] ]; |
---|
1054 | } |
---|
1055 | uiInternalAddress++; |
---|
1056 | if(uiInternalAddress==pcCU->getPic()->getNumPartInCU()) |
---|
1057 | { |
---|
1058 | uiInternalAddress = 0; |
---|
1059 | uiExternalAddress = pcPic->getPicSym()->getCUOrderMap(pcPic->getPicSym()->getInverseCUOrderMap(uiExternalAddress)+1); |
---|
1060 | } |
---|
1061 | UInt uiRealEndAddress = pcPic->getPicSym()->getPicSCUEncOrder(uiExternalAddress*pcPic->getNumPartInCU()+uiInternalAddress); |
---|
1062 | |
---|
1063 | // Encode slice finish |
---|
1064 | Bool bTerminateSlice = false; |
---|
1065 | if (uiCUAddr+(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1)) == uiRealEndAddress) |
---|
1066 | { |
---|
1067 | bTerminateSlice = true; |
---|
1068 | } |
---|
1069 | UInt uiGranularityWidth = g_uiMaxCUWidth; |
---|
1070 | uiPosX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
1071 | uiPosY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
1072 | Bool granularityBoundary=((uiPosX+pcCU->getWidth(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosX+pcCU->getWidth(uiAbsPartIdx)==uiWidth)) |
---|
1073 | &&((uiPosY+pcCU->getHeight(uiAbsPartIdx))%uiGranularityWidth==0||(uiPosY+pcCU->getHeight(uiAbsPartIdx)==uiHeight)); |
---|
1074 | |
---|
1075 | if(granularityBoundary) |
---|
1076 | { |
---|
1077 | // The 1-terminating bit is added to all streams, so don't add it here when it's 1. |
---|
1078 | if (!bTerminateSlice) |
---|
1079 | m_pcEntropyCoder->encodeTerminatingBit( bTerminateSlice ? 1 : 0 ); |
---|
1080 | } |
---|
1081 | |
---|
1082 | Int numberOfWrittenBits = 0; |
---|
1083 | if (m_pcBitCounter) |
---|
1084 | { |
---|
1085 | numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
1086 | } |
---|
1087 | |
---|
1088 | // Calculate slice end IF this CU puts us over slice bit size. |
---|
1089 | UInt iGranularitySize = pcCU->getPic()->getNumPartInCU(); |
---|
1090 | Int iGranularityEnd = ((pcCU->getSCUAddr()+uiAbsPartIdx)/iGranularitySize)*iGranularitySize; |
---|
1091 | if(iGranularityEnd<=pcSlice->getSliceSegmentCurStartCUAddr()) |
---|
1092 | { |
---|
1093 | iGranularityEnd+=max(iGranularitySize,(pcCU->getPic()->getNumPartInCU()>>(uiDepth<<1))); |
---|
1094 | } |
---|
1095 | // Set slice end parameter |
---|
1096 | if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceBits()+numberOfWrittenBits>pcSlice->getSliceArgument()<<3) |
---|
1097 | { |
---|
1098 | pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd); |
---|
1099 | pcSlice->setSliceCurEndCUAddr(iGranularityEnd); |
---|
1100 | return; |
---|
1101 | } |
---|
1102 | // Set dependent slice end parameter |
---|
1103 | if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES&&!pcSlice->getFinalized()&&pcSlice->getSliceSegmentBits()+numberOfWrittenBits > pcSlice->getSliceSegmentArgument()<<3) |
---|
1104 | { |
---|
1105 | pcSlice->setSliceSegmentCurEndCUAddr(iGranularityEnd); |
---|
1106 | return; |
---|
1107 | } |
---|
1108 | if(granularityBoundary) |
---|
1109 | { |
---|
1110 | pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) ); |
---|
1111 | pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits); |
---|
1112 | if (m_pcBitCounter) |
---|
1113 | { |
---|
1114 | m_pcEntropyCoder->resetBits(); |
---|
1115 | } |
---|
1116 | } |
---|
1117 | } |
---|
1118 | |
---|
1119 | /** Compute QP for each CU |
---|
1120 | * \param pcCU Target CU |
---|
1121 | * \param uiDepth CU depth |
---|
1122 | * \returns quantization parameter |
---|
1123 | */ |
---|
1124 | Int TEncCu::xComputeQP( TComDataCU* pcCU, UInt uiDepth ) |
---|
1125 | { |
---|
1126 | Int iBaseQp = pcCU->getSlice()->getSliceQp(); |
---|
1127 | Int iQpOffset = 0; |
---|
1128 | if ( m_pcEncCfg->getUseAdaptiveQP() ) |
---|
1129 | { |
---|
1130 | TEncPic* pcEPic = dynamic_cast<TEncPic*>( pcCU->getPic() ); |
---|
1131 | UInt uiAQDepth = min( uiDepth, pcEPic->getMaxAQDepth()-1 ); |
---|
1132 | TEncPicQPAdaptationLayer* pcAQLayer = pcEPic->getAQLayer( uiAQDepth ); |
---|
1133 | UInt uiAQUPosX = pcCU->getCUPelX() / pcAQLayer->getAQPartWidth(); |
---|
1134 | UInt uiAQUPosY = pcCU->getCUPelY() / pcAQLayer->getAQPartHeight(); |
---|
1135 | UInt uiAQUStride = pcAQLayer->getAQPartStride(); |
---|
1136 | TEncQPAdaptationUnit* acAQU = pcAQLayer->getQPAdaptationUnit(); |
---|
1137 | |
---|
1138 | Double dMaxQScale = pow(2.0, m_pcEncCfg->getQPAdaptationRange()/6.0); |
---|
1139 | Double dAvgAct = pcAQLayer->getAvgActivity(); |
---|
1140 | Double dCUAct = acAQU[uiAQUPosY * uiAQUStride + uiAQUPosX].getActivity(); |
---|
1141 | Double dNormAct = (dMaxQScale*dCUAct + dAvgAct) / (dCUAct + dMaxQScale*dAvgAct); |
---|
1142 | Double dQpOffset = log(dNormAct) / log(2.0) * 6.0; |
---|
1143 | iQpOffset = Int(floor( dQpOffset + 0.49999 )); |
---|
1144 | } |
---|
1145 | return Clip3(-pcCU->getSlice()->getSPS()->getQpBDOffsetY(), MAX_QP, iBaseQp+iQpOffset ); |
---|
1146 | } |
---|
1147 | |
---|
1148 | /** encode a CU block recursively |
---|
1149 | * \param pcCU |
---|
1150 | * \param uiAbsPartIdx |
---|
1151 | * \param uiDepth |
---|
1152 | * \returns Void |
---|
1153 | */ |
---|
1154 | Void TEncCu::xEncodeCU( TComDataCU* pcCU, UInt uiAbsPartIdx, UInt uiDepth ) |
---|
1155 | { |
---|
1156 | TComPic* pcPic = pcCU->getPic(); |
---|
1157 | |
---|
1158 | Bool bBoundary = false; |
---|
1159 | UInt uiLPelX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
1160 | UInt uiRPelX = uiLPelX + (g_uiMaxCUWidth>>uiDepth) - 1; |
---|
1161 | UInt uiTPelY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
1162 | UInt uiBPelY = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1; |
---|
1163 | |
---|
1164 | TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx()); |
---|
1165 | // If slice start is within this cu... |
---|
1166 | Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && |
---|
1167 | pcSlice->getSliceSegmentCurStartCUAddr() < pcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcPic->getNumPartInCU() >> (uiDepth<<1) ); |
---|
1168 | // We need to split, so don't try these modes. |
---|
1169 | if(!bSliceStart&&( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
1170 | { |
---|
1171 | m_pcEntropyCoder->encodeSplitFlag( pcCU, uiAbsPartIdx, uiDepth ); |
---|
1172 | } |
---|
1173 | else |
---|
1174 | { |
---|
1175 | bBoundary = true; |
---|
1176 | } |
---|
1177 | |
---|
1178 | if( ( ( uiDepth < pcCU->getDepth( uiAbsPartIdx ) ) && ( uiDepth < (g_uiMaxCUDepth-g_uiAddCUDepth) ) ) || bBoundary ) |
---|
1179 | { |
---|
1180 | UInt uiQNumParts = ( pcPic->getNumPartInCU() >> (uiDepth<<1) )>>2; |
---|
1181 | if( (g_uiMaxCUWidth>>uiDepth) == pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP()) |
---|
1182 | { |
---|
1183 | setdQPFlag(true); |
---|
1184 | } |
---|
1185 | for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts ) |
---|
1186 | { |
---|
1187 | uiLPelX = pcCU->getCUPelX() + g_auiRasterToPelX[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
1188 | uiTPelY = pcCU->getCUPelY() + g_auiRasterToPelY[ g_auiZscanToRaster[uiAbsPartIdx] ]; |
---|
1189 | Bool bInSlice = pcCU->getSCUAddr()+uiAbsPartIdx+uiQNumParts>pcSlice->getSliceSegmentCurStartCUAddr()&&pcCU->getSCUAddr()+uiAbsPartIdx<pcSlice->getSliceSegmentCurEndCUAddr(); |
---|
1190 | if(bInSlice&&( uiLPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
1191 | { |
---|
1192 | xEncodeCU( pcCU, uiAbsPartIdx, uiDepth+1 ); |
---|
1193 | } |
---|
1194 | } |
---|
1195 | return; |
---|
1196 | } |
---|
1197 | |
---|
1198 | if( (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() && pcCU->getSlice()->getPPS()->getUseDQP()) |
---|
1199 | { |
---|
1200 | setdQPFlag(true); |
---|
1201 | } |
---|
1202 | if (pcCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
1203 | { |
---|
1204 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( pcCU, uiAbsPartIdx ); |
---|
1205 | } |
---|
1206 | if( !pcCU->getSlice()->isIntra() ) |
---|
1207 | { |
---|
1208 | m_pcEntropyCoder->encodeSkipFlag( pcCU, uiAbsPartIdx ); |
---|
1209 | } |
---|
1210 | |
---|
1211 | if( pcCU->isSkipped( uiAbsPartIdx ) ) |
---|
1212 | { |
---|
1213 | m_pcEntropyCoder->encodeMergeIndex( pcCU, uiAbsPartIdx ); |
---|
1214 | finishCU(pcCU,uiAbsPartIdx,uiDepth); |
---|
1215 | return; |
---|
1216 | } |
---|
1217 | #if INTRA_BL |
---|
1218 | m_pcEntropyCoder->encodeIntraBLFlag( pcCU, uiAbsPartIdx ); |
---|
1219 | if ( !pcCU->isIntraBL( uiAbsPartIdx ) ) |
---|
1220 | { |
---|
1221 | #endif |
---|
1222 | m_pcEntropyCoder->encodePredMode( pcCU, uiAbsPartIdx ); |
---|
1223 | |
---|
1224 | m_pcEntropyCoder->encodePartSize( pcCU, uiAbsPartIdx, uiDepth ); |
---|
1225 | |
---|
1226 | if (pcCU->isIntra( uiAbsPartIdx ) && pcCU->getPartitionSize( uiAbsPartIdx ) == SIZE_2Nx2N ) |
---|
1227 | { |
---|
1228 | m_pcEntropyCoder->encodeIPCMInfo( pcCU, uiAbsPartIdx ); |
---|
1229 | |
---|
1230 | if(pcCU->getIPCMFlag(uiAbsPartIdx)) |
---|
1231 | { |
---|
1232 | // Encode slice finish |
---|
1233 | finishCU(pcCU,uiAbsPartIdx,uiDepth); |
---|
1234 | return; |
---|
1235 | } |
---|
1236 | } |
---|
1237 | |
---|
1238 | // prediction Info ( Intra : direction mode, Inter : Mv, reference idx ) |
---|
1239 | m_pcEntropyCoder->encodePredInfo( pcCU, uiAbsPartIdx ); |
---|
1240 | #if INTRA_BL |
---|
1241 | } |
---|
1242 | #endif |
---|
1243 | |
---|
1244 | // Encode Coefficients |
---|
1245 | Bool bCodeDQP = getdQPFlag(); |
---|
1246 | m_pcEntropyCoder->encodeCoeff( pcCU, uiAbsPartIdx, uiDepth, pcCU->getWidth (uiAbsPartIdx), pcCU->getHeight(uiAbsPartIdx), bCodeDQP ); |
---|
1247 | setdQPFlag( bCodeDQP ); |
---|
1248 | |
---|
1249 | // --- write terminating bit --- |
---|
1250 | finishCU(pcCU,uiAbsPartIdx,uiDepth); |
---|
1251 | } |
---|
1252 | |
---|
1253 | #if RATE_CONTROL_INTRA |
---|
1254 | Int xCalcHADs8x8_ISlice(Pel *piOrg, Int iStrideOrg) |
---|
1255 | { |
---|
1256 | Int k, i, j, jj; |
---|
1257 | Int diff[64], m1[8][8], m2[8][8], m3[8][8], iSumHad = 0; |
---|
1258 | |
---|
1259 | for( k = 0; k < 64; k += 8 ) |
---|
1260 | { |
---|
1261 | diff[k+0] = piOrg[0] ; |
---|
1262 | diff[k+1] = piOrg[1] ; |
---|
1263 | diff[k+2] = piOrg[2] ; |
---|
1264 | diff[k+3] = piOrg[3] ; |
---|
1265 | diff[k+4] = piOrg[4] ; |
---|
1266 | diff[k+5] = piOrg[5] ; |
---|
1267 | diff[k+6] = piOrg[6] ; |
---|
1268 | diff[k+7] = piOrg[7] ; |
---|
1269 | |
---|
1270 | piOrg += iStrideOrg; |
---|
1271 | } |
---|
1272 | |
---|
1273 | //horizontal |
---|
1274 | for (j=0; j < 8; j++) |
---|
1275 | { |
---|
1276 | jj = j << 3; |
---|
1277 | m2[j][0] = diff[jj ] + diff[jj+4]; |
---|
1278 | m2[j][1] = diff[jj+1] + diff[jj+5]; |
---|
1279 | m2[j][2] = diff[jj+2] + diff[jj+6]; |
---|
1280 | m2[j][3] = diff[jj+3] + diff[jj+7]; |
---|
1281 | m2[j][4] = diff[jj ] - diff[jj+4]; |
---|
1282 | m2[j][5] = diff[jj+1] - diff[jj+5]; |
---|
1283 | m2[j][6] = diff[jj+2] - diff[jj+6]; |
---|
1284 | m2[j][7] = diff[jj+3] - diff[jj+7]; |
---|
1285 | |
---|
1286 | m1[j][0] = m2[j][0] + m2[j][2]; |
---|
1287 | m1[j][1] = m2[j][1] + m2[j][3]; |
---|
1288 | m1[j][2] = m2[j][0] - m2[j][2]; |
---|
1289 | m1[j][3] = m2[j][1] - m2[j][3]; |
---|
1290 | m1[j][4] = m2[j][4] + m2[j][6]; |
---|
1291 | m1[j][5] = m2[j][5] + m2[j][7]; |
---|
1292 | m1[j][6] = m2[j][4] - m2[j][6]; |
---|
1293 | m1[j][7] = m2[j][5] - m2[j][7]; |
---|
1294 | |
---|
1295 | m2[j][0] = m1[j][0] + m1[j][1]; |
---|
1296 | m2[j][1] = m1[j][0] - m1[j][1]; |
---|
1297 | m2[j][2] = m1[j][2] + m1[j][3]; |
---|
1298 | m2[j][3] = m1[j][2] - m1[j][3]; |
---|
1299 | m2[j][4] = m1[j][4] + m1[j][5]; |
---|
1300 | m2[j][5] = m1[j][4] - m1[j][5]; |
---|
1301 | m2[j][6] = m1[j][6] + m1[j][7]; |
---|
1302 | m2[j][7] = m1[j][6] - m1[j][7]; |
---|
1303 | } |
---|
1304 | |
---|
1305 | //vertical |
---|
1306 | for (i=0; i < 8; i++) |
---|
1307 | { |
---|
1308 | m3[0][i] = m2[0][i] + m2[4][i]; |
---|
1309 | m3[1][i] = m2[1][i] + m2[5][i]; |
---|
1310 | m3[2][i] = m2[2][i] + m2[6][i]; |
---|
1311 | m3[3][i] = m2[3][i] + m2[7][i]; |
---|
1312 | m3[4][i] = m2[0][i] - m2[4][i]; |
---|
1313 | m3[5][i] = m2[1][i] - m2[5][i]; |
---|
1314 | m3[6][i] = m2[2][i] - m2[6][i]; |
---|
1315 | m3[7][i] = m2[3][i] - m2[7][i]; |
---|
1316 | |
---|
1317 | m1[0][i] = m3[0][i] + m3[2][i]; |
---|
1318 | m1[1][i] = m3[1][i] + m3[3][i]; |
---|
1319 | m1[2][i] = m3[0][i] - m3[2][i]; |
---|
1320 | m1[3][i] = m3[1][i] - m3[3][i]; |
---|
1321 | m1[4][i] = m3[4][i] + m3[6][i]; |
---|
1322 | m1[5][i] = m3[5][i] + m3[7][i]; |
---|
1323 | m1[6][i] = m3[4][i] - m3[6][i]; |
---|
1324 | m1[7][i] = m3[5][i] - m3[7][i]; |
---|
1325 | |
---|
1326 | m2[0][i] = m1[0][i] + m1[1][i]; |
---|
1327 | m2[1][i] = m1[0][i] - m1[1][i]; |
---|
1328 | m2[2][i] = m1[2][i] + m1[3][i]; |
---|
1329 | m2[3][i] = m1[2][i] - m1[3][i]; |
---|
1330 | m2[4][i] = m1[4][i] + m1[5][i]; |
---|
1331 | m2[5][i] = m1[4][i] - m1[5][i]; |
---|
1332 | m2[6][i] = m1[6][i] + m1[7][i]; |
---|
1333 | m2[7][i] = m1[6][i] - m1[7][i]; |
---|
1334 | } |
---|
1335 | |
---|
1336 | for (i = 0; i < 8; i++) |
---|
1337 | { |
---|
1338 | for (j = 0; j < 8; j++) |
---|
1339 | { |
---|
1340 | iSumHad += abs(m2[i][j]); |
---|
1341 | } |
---|
1342 | } |
---|
1343 | iSumHad -= abs(m2[0][0]); |
---|
1344 | iSumHad =(iSumHad+2)>>2; |
---|
1345 | return(iSumHad); |
---|
1346 | } |
---|
1347 | |
---|
1348 | Int TEncCu::updateLCUDataISlice(TComDataCU* pcCU, Int LCUIdx, Int width, Int height) |
---|
1349 | { |
---|
1350 | Int xBl, yBl; |
---|
1351 | const Int iBlkSize = 8; |
---|
1352 | |
---|
1353 | Pel* pOrgInit = pcCU->getPic()->getPicYuvOrg()->getLumaAddr(pcCU->getAddr(), 0); |
---|
1354 | Int iStrideOrig = pcCU->getPic()->getPicYuvOrg()->getStride(); |
---|
1355 | Pel *pOrg; |
---|
1356 | |
---|
1357 | Int iSumHad = 0; |
---|
1358 | for ( yBl=0; (yBl+iBlkSize)<=height; yBl+= iBlkSize) |
---|
1359 | { |
---|
1360 | for ( xBl=0; (xBl+iBlkSize)<=width; xBl+= iBlkSize) |
---|
1361 | { |
---|
1362 | pOrg = pOrgInit + iStrideOrig*yBl + xBl; |
---|
1363 | iSumHad += xCalcHADs8x8_ISlice(pOrg, iStrideOrig); |
---|
1364 | } |
---|
1365 | } |
---|
1366 | return(iSumHad); |
---|
1367 | } |
---|
1368 | #endif |
---|
1369 | |
---|
1370 | /** check RD costs for a CU block encoded with merge |
---|
1371 | * \param rpcBestCU |
---|
1372 | * \param rpcTempCU |
---|
1373 | * \returns Void |
---|
1374 | */ |
---|
1375 | Void TEncCu::xCheckRDCostMerge2Nx2N( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, Bool *earlyDetectionSkipMode ) |
---|
1376 | { |
---|
1377 | assert( rpcTempCU->getSlice()->getSliceType() != I_SLICE ); |
---|
1378 | TComMvField cMvFieldNeighbours[ 2 * MRG_MAX_NUM_CANDS ]; // double length for mv of both lists |
---|
1379 | UChar uhInterDirNeighbours[MRG_MAX_NUM_CANDS]; |
---|
1380 | Int numValidMergeCand = 0; |
---|
1381 | |
---|
1382 | for( UInt ui = 0; ui < rpcTempCU->getSlice()->getMaxNumMergeCand(); ++ui ) |
---|
1383 | { |
---|
1384 | uhInterDirNeighbours[ui] = 0; |
---|
1385 | } |
---|
1386 | UChar uhDepth = rpcTempCU->getDepth( 0 ); |
---|
1387 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level |
---|
1388 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
1389 | rpcTempCU->getInterMergeCandidates( 0, 0, cMvFieldNeighbours,uhInterDirNeighbours, numValidMergeCand ); |
---|
1390 | |
---|
1391 | Int mergeCandBuffer[MRG_MAX_NUM_CANDS]; |
---|
1392 | for( UInt ui = 0; ui < numValidMergeCand; ++ui ) |
---|
1393 | { |
---|
1394 | mergeCandBuffer[ui] = 0; |
---|
1395 | } |
---|
1396 | |
---|
1397 | Bool bestIsSkip = false; |
---|
1398 | |
---|
1399 | UInt iteration; |
---|
1400 | if ( rpcTempCU->isLosslessCoded(0)) |
---|
1401 | { |
---|
1402 | iteration = 1; |
---|
1403 | } |
---|
1404 | else |
---|
1405 | { |
---|
1406 | iteration = 2; |
---|
1407 | } |
---|
1408 | |
---|
1409 | for( UInt uiNoResidual = 0; uiNoResidual < iteration; ++uiNoResidual ) |
---|
1410 | { |
---|
1411 | for( UInt uiMergeCand = 0; uiMergeCand < numValidMergeCand; ++uiMergeCand ) |
---|
1412 | { |
---|
1413 | #if REF_IDX_ME_ZEROMV |
---|
1414 | Bool bZeroMVILR = rpcTempCU->xCheckZeroMVILRMerge(uhInterDirNeighbours[uiMergeCand], cMvFieldNeighbours[0 + 2*uiMergeCand], cMvFieldNeighbours[1 + 2*uiMergeCand]); |
---|
1415 | if(bZeroMVILR) |
---|
1416 | { |
---|
1417 | #endif |
---|
1418 | if(!(uiNoResidual==1 && mergeCandBuffer[uiMergeCand]==1)) |
---|
1419 | { |
---|
1420 | if( !(bestIsSkip && uiNoResidual == 0) ) |
---|
1421 | { |
---|
1422 | // set MC parameters |
---|
1423 | rpcTempCU->setPredModeSubParts( MODE_INTER, 0, uhDepth ); // interprets depth relative to LCU level |
---|
1424 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
1425 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uhDepth ); // interprets depth relative to LCU level |
---|
1426 | rpcTempCU->setMergeFlagSubParts( true, 0, 0, uhDepth ); // interprets depth relative to LCU level |
---|
1427 | rpcTempCU->setMergeIndexSubParts( uiMergeCand, 0, 0, uhDepth ); // interprets depth relative to LCU level |
---|
1428 | rpcTempCU->setInterDirSubParts( uhInterDirNeighbours[uiMergeCand], 0, 0, uhDepth ); // interprets depth relative to LCU level |
---|
1429 | rpcTempCU->getCUMvField( REF_PIC_LIST_0 )->setAllMvField( cMvFieldNeighbours[0 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level |
---|
1430 | rpcTempCU->getCUMvField( REF_PIC_LIST_1 )->setAllMvField( cMvFieldNeighbours[1 + 2*uiMergeCand], SIZE_2Nx2N, 0, 0 ); // interprets depth relative to rpcTempCU level |
---|
1431 | |
---|
1432 | // do MC |
---|
1433 | m_pcPredSearch->motionCompensation ( rpcTempCU, m_ppcPredYuvTemp[uhDepth] ); |
---|
1434 | // estimate residual and encode everything |
---|
1435 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, |
---|
1436 | m_ppcOrigYuv [uhDepth], |
---|
1437 | m_ppcPredYuvTemp[uhDepth], |
---|
1438 | m_ppcResiYuvTemp[uhDepth], |
---|
1439 | m_ppcResiYuvBest[uhDepth], |
---|
1440 | m_ppcRecoYuvTemp[uhDepth], |
---|
1441 | (uiNoResidual? true:false)); |
---|
1442 | |
---|
1443 | |
---|
1444 | if ( uiNoResidual == 0 && rpcTempCU->getQtRootCbf(0) == 0 ) |
---|
1445 | { |
---|
1446 | // If no residual when allowing for one, then set mark to not try case where residual is forced to 0 |
---|
1447 | mergeCandBuffer[uiMergeCand] = 1; |
---|
1448 | } |
---|
1449 | |
---|
1450 | rpcTempCU->setSkipFlagSubParts( rpcTempCU->getQtRootCbf(0) == 0, 0, uhDepth ); |
---|
1451 | Int orgQP = rpcTempCU->getQP( 0 ); |
---|
1452 | xCheckDQP( rpcTempCU ); |
---|
1453 | xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth); |
---|
1454 | rpcTempCU->initEstData( uhDepth, orgQP ); |
---|
1455 | |
---|
1456 | if( m_pcEncCfg->getUseFastDecisionForMerge() && !bestIsSkip ) |
---|
1457 | { |
---|
1458 | bestIsSkip = rpcBestCU->getQtRootCbf(0) == 0; |
---|
1459 | } |
---|
1460 | } |
---|
1461 | #if REF_IDX_ME_ZEROMV |
---|
1462 | } |
---|
1463 | #endif |
---|
1464 | } |
---|
1465 | } |
---|
1466 | |
---|
1467 | if(uiNoResidual == 0 && m_pcEncCfg->getUseEarlySkipDetection()) |
---|
1468 | { |
---|
1469 | if(rpcBestCU->getQtRootCbf( 0 ) == 0) |
---|
1470 | { |
---|
1471 | if( rpcBestCU->getMergeFlag( 0 )) |
---|
1472 | { |
---|
1473 | *earlyDetectionSkipMode = true; |
---|
1474 | } |
---|
1475 | else |
---|
1476 | { |
---|
1477 | Int absoulte_MV=0; |
---|
1478 | for ( UInt uiRefListIdx = 0; uiRefListIdx < 2; uiRefListIdx++ ) |
---|
1479 | { |
---|
1480 | if ( rpcBestCU->getSlice()->getNumRefIdx( RefPicList( uiRefListIdx ) ) > 0 ) |
---|
1481 | { |
---|
1482 | TComCUMvField* pcCUMvField = rpcBestCU->getCUMvField(RefPicList( uiRefListIdx )); |
---|
1483 | Int iHor = pcCUMvField->getMvd( 0 ).getAbsHor(); |
---|
1484 | Int iVer = pcCUMvField->getMvd( 0 ).getAbsVer(); |
---|
1485 | absoulte_MV+=iHor+iVer; |
---|
1486 | } |
---|
1487 | } |
---|
1488 | |
---|
1489 | if(absoulte_MV == 0) |
---|
1490 | { |
---|
1491 | *earlyDetectionSkipMode = true; |
---|
1492 | } |
---|
1493 | } |
---|
1494 | } |
---|
1495 | } |
---|
1496 | } |
---|
1497 | } |
---|
1498 | |
---|
1499 | |
---|
1500 | #if AMP_MRG |
---|
1501 | Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize, Bool bUseMRG) |
---|
1502 | #else |
---|
1503 | Void TEncCu::xCheckRDCostInter( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize ePartSize ) |
---|
1504 | #endif |
---|
1505 | { |
---|
1506 | UChar uhDepth = rpcTempCU->getDepth( 0 ); |
---|
1507 | |
---|
1508 | rpcTempCU->setDepthSubParts( uhDepth, 0 ); |
---|
1509 | |
---|
1510 | rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth ); |
---|
1511 | |
---|
1512 | rpcTempCU->setPartSizeSubParts ( ePartSize, 0, uhDepth ); |
---|
1513 | rpcTempCU->setPredModeSubParts ( MODE_INTER, 0, uhDepth ); |
---|
1514 | rpcTempCU->setCUTransquantBypassSubParts ( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
1515 | |
---|
1516 | #if AMP_MRG |
---|
1517 | rpcTempCU->setMergeAMP (true); |
---|
1518 | m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], false, bUseMRG ); |
---|
1519 | #else |
---|
1520 | m_pcPredSearch->predInterSearch ( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth] ); |
---|
1521 | #endif |
---|
1522 | |
---|
1523 | #if AMP_MRG |
---|
1524 | if ( !rpcTempCU->getMergeAMP() ) |
---|
1525 | { |
---|
1526 | return; |
---|
1527 | } |
---|
1528 | #endif |
---|
1529 | |
---|
1530 | #if RATE_CONTROL_LAMBDA_DOMAIN && !M0036_RC_IMPROVEMENT |
---|
1531 | if ( m_pcEncCfg->getUseRateCtrl() && m_pcEncCfg->getLCULevelRC() && ePartSize == SIZE_2Nx2N && uhDepth <= m_addSADDepth ) |
---|
1532 | { |
---|
1533 | UInt SAD = m_pcRdCost->getSADPart( g_bitDepthY, m_ppcPredYuvTemp[uhDepth]->getLumaAddr(), m_ppcPredYuvTemp[uhDepth]->getStride(), |
---|
1534 | m_ppcOrigYuv[uhDepth]->getLumaAddr(), m_ppcOrigYuv[uhDepth]->getStride(), |
---|
1535 | rpcTempCU->getWidth(0), rpcTempCU->getHeight(0) ); |
---|
1536 | m_temporalSAD = (Int)SAD; |
---|
1537 | } |
---|
1538 | #endif |
---|
1539 | |
---|
1540 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false ); |
---|
1541 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
1542 | |
---|
1543 | xCheckDQP( rpcTempCU ); |
---|
1544 | xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth); |
---|
1545 | } |
---|
1546 | |
---|
1547 | Void TEncCu::xCheckRDCostIntra( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, PartSize eSize ) |
---|
1548 | { |
---|
1549 | UInt uiDepth = rpcTempCU->getDepth( 0 ); |
---|
1550 | |
---|
1551 | rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth ); |
---|
1552 | |
---|
1553 | rpcTempCU->setPartSizeSubParts( eSize, 0, uiDepth ); |
---|
1554 | rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth ); |
---|
1555 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth ); |
---|
1556 | |
---|
1557 | Bool bSeparateLumaChroma = true; // choose estimation mode |
---|
1558 | UInt uiPreCalcDistC = 0; |
---|
1559 | if( !bSeparateLumaChroma ) |
---|
1560 | { |
---|
1561 | m_pcPredSearch->preestChromaPredMode( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth] ); |
---|
1562 | } |
---|
1563 | m_pcPredSearch ->estIntraPredQT ( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC, bSeparateLumaChroma ); |
---|
1564 | |
---|
1565 | m_ppcRecoYuvTemp[uiDepth]->copyToPicLuma(rpcTempCU->getPic()->getPicYuvRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU() ); |
---|
1566 | |
---|
1567 | m_pcPredSearch ->estIntraPredChromaQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth], uiPreCalcDistC ); |
---|
1568 | |
---|
1569 | m_pcEntropyCoder->resetBits(); |
---|
1570 | #if INTRA_BL |
---|
1571 | m_pcEntropyCoder->encodeIntraBLFlag ( rpcTempCU, 0, true ); |
---|
1572 | #endif |
---|
1573 | if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
1574 | { |
---|
1575 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0, true ); |
---|
1576 | } |
---|
1577 | m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0, true ); |
---|
1578 | m_pcEntropyCoder->encodePredMode( rpcTempCU, 0, true ); |
---|
1579 | m_pcEntropyCoder->encodePartSize( rpcTempCU, 0, uiDepth, true ); |
---|
1580 | m_pcEntropyCoder->encodePredInfo( rpcTempCU, 0, true ); |
---|
1581 | m_pcEntropyCoder->encodeIPCMInfo(rpcTempCU, 0, true ); |
---|
1582 | |
---|
1583 | // Encode Coefficients |
---|
1584 | Bool bCodeDQP = getdQPFlag(); |
---|
1585 | m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP ); |
---|
1586 | setdQPFlag( bCodeDQP ); |
---|
1587 | |
---|
1588 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
1589 | |
---|
1590 | rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
1591 | if(m_pcEncCfg->getUseSBACRD()) |
---|
1592 | { |
---|
1593 | rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
1594 | } |
---|
1595 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
1596 | |
---|
1597 | xCheckDQP( rpcTempCU ); |
---|
1598 | xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth); |
---|
1599 | } |
---|
1600 | |
---|
1601 | /** Check R-D costs for a CU with PCM mode. |
---|
1602 | * \param rpcBestCU pointer to best mode CU data structure |
---|
1603 | * \param rpcTempCU pointer to testing mode CU data structure |
---|
1604 | * \returns Void |
---|
1605 | * |
---|
1606 | * \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. |
---|
1607 | */ |
---|
1608 | Void TEncCu::xCheckIntraPCM( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU ) |
---|
1609 | { |
---|
1610 | UInt uiDepth = rpcTempCU->getDepth( 0 ); |
---|
1611 | |
---|
1612 | rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth ); |
---|
1613 | |
---|
1614 | rpcTempCU->setIPCMFlag(0, true); |
---|
1615 | rpcTempCU->setIPCMFlagSubParts (true, 0, rpcTempCU->getDepth(0)); |
---|
1616 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth ); |
---|
1617 | rpcTempCU->setPredModeSubParts( MODE_INTRA, 0, uiDepth ); |
---|
1618 | rpcTempCU->setTrIdxSubParts ( 0, 0, uiDepth ); |
---|
1619 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth ); |
---|
1620 | |
---|
1621 | m_pcPredSearch->IPCMSearch( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth]); |
---|
1622 | |
---|
1623 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->load(m_pppcRDSbacCoder[uiDepth][CI_CURR_BEST]); |
---|
1624 | |
---|
1625 | m_pcEntropyCoder->resetBits(); |
---|
1626 | #if INTRA_BL |
---|
1627 | m_pcEntropyCoder->encodeIntraBLFlag ( rpcTempCU, 0, true ); |
---|
1628 | #endif |
---|
1629 | if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
1630 | { |
---|
1631 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0, true ); |
---|
1632 | } |
---|
1633 | m_pcEntropyCoder->encodeSkipFlag ( rpcTempCU, 0, true ); |
---|
1634 | m_pcEntropyCoder->encodePredMode ( rpcTempCU, 0, true ); |
---|
1635 | m_pcEntropyCoder->encodePartSize ( rpcTempCU, 0, uiDepth, true ); |
---|
1636 | m_pcEntropyCoder->encodeIPCMInfo ( rpcTempCU, 0, true ); |
---|
1637 | |
---|
1638 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
1639 | |
---|
1640 | rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
1641 | if(m_pcEncCfg->getUseSBACRD()) |
---|
1642 | { |
---|
1643 | rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
1644 | } |
---|
1645 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
1646 | |
---|
1647 | xCheckDQP( rpcTempCU ); |
---|
1648 | xCheckBestMode( rpcBestCU, rpcTempCU, uiDepth ); |
---|
1649 | } |
---|
1650 | |
---|
1651 | /** check whether current try is the best with identifying the depth of current try |
---|
1652 | * \param rpcBestCU |
---|
1653 | * \param rpcTempCU |
---|
1654 | * \returns Void |
---|
1655 | */ |
---|
1656 | Void TEncCu::xCheckBestMode( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU, UInt uiDepth ) |
---|
1657 | { |
---|
1658 | if( rpcTempCU->getTotalCost() < rpcBestCU->getTotalCost() ) |
---|
1659 | { |
---|
1660 | TComYuv* pcYuv; |
---|
1661 | // Change Information data |
---|
1662 | TComDataCU* pcCU = rpcBestCU; |
---|
1663 | rpcBestCU = rpcTempCU; |
---|
1664 | rpcTempCU = pcCU; |
---|
1665 | |
---|
1666 | // Change Prediction data |
---|
1667 | pcYuv = m_ppcPredYuvBest[uiDepth]; |
---|
1668 | m_ppcPredYuvBest[uiDepth] = m_ppcPredYuvTemp[uiDepth]; |
---|
1669 | m_ppcPredYuvTemp[uiDepth] = pcYuv; |
---|
1670 | |
---|
1671 | // Change Reconstruction data |
---|
1672 | pcYuv = m_ppcRecoYuvBest[uiDepth]; |
---|
1673 | m_ppcRecoYuvBest[uiDepth] = m_ppcRecoYuvTemp[uiDepth]; |
---|
1674 | m_ppcRecoYuvTemp[uiDepth] = pcYuv; |
---|
1675 | |
---|
1676 | pcYuv = NULL; |
---|
1677 | pcCU = NULL; |
---|
1678 | |
---|
1679 | if( m_bUseSBACRD ) // store temp best CI for next CU coding |
---|
1680 | m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]->store(m_pppcRDSbacCoder[uiDepth][CI_NEXT_BEST]); |
---|
1681 | } |
---|
1682 | } |
---|
1683 | |
---|
1684 | Void TEncCu::xCheckDQP( TComDataCU* pcCU ) |
---|
1685 | { |
---|
1686 | UInt uiDepth = pcCU->getDepth( 0 ); |
---|
1687 | |
---|
1688 | if( pcCU->getSlice()->getPPS()->getUseDQP() && (g_uiMaxCUWidth>>uiDepth) >= pcCU->getSlice()->getPPS()->getMinCuDQPSize() ) |
---|
1689 | { |
---|
1690 | if ( pcCU->getCbf( 0, TEXT_LUMA, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_U, 0 ) || pcCU->getCbf( 0, TEXT_CHROMA_V, 0 ) ) |
---|
1691 | { |
---|
1692 | #if !RDO_WITHOUT_DQP_BITS |
---|
1693 | m_pcEntropyCoder->resetBits(); |
---|
1694 | m_pcEntropyCoder->encodeQP( pcCU, 0, false ); |
---|
1695 | pcCU->getTotalBits() += m_pcEntropyCoder->getNumberOfWrittenBits(); // dQP bits |
---|
1696 | if(m_pcEncCfg->getUseSBACRD()) |
---|
1697 | { |
---|
1698 | pcCU->getTotalBins() += ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
1699 | } |
---|
1700 | pcCU->getTotalCost() = m_pcRdCost->calcRdCost( pcCU->getTotalBits(), pcCU->getTotalDistortion() ); |
---|
1701 | #endif |
---|
1702 | } |
---|
1703 | else |
---|
1704 | { |
---|
1705 | pcCU->setQPSubParts( pcCU->getRefQP( 0 ), 0, uiDepth ); // set QP to default QP |
---|
1706 | } |
---|
1707 | } |
---|
1708 | } |
---|
1709 | |
---|
1710 | Void TEncCu::xCopyAMVPInfo (AMVPInfo* pSrc, AMVPInfo* pDst) |
---|
1711 | { |
---|
1712 | pDst->iN = pSrc->iN; |
---|
1713 | for (Int i = 0; i < pSrc->iN; i++) |
---|
1714 | { |
---|
1715 | pDst->m_acMvCand[i] = pSrc->m_acMvCand[i]; |
---|
1716 | } |
---|
1717 | } |
---|
1718 | Void TEncCu::xCopyYuv2Pic(TComPic* rpcPic, UInt uiCUAddr, UInt uiAbsPartIdx, UInt uiDepth, UInt uiSrcDepth, TComDataCU* pcCU, UInt uiLPelX, UInt uiTPelY ) |
---|
1719 | { |
---|
1720 | UInt uiRPelX = uiLPelX + (g_uiMaxCUWidth>>uiDepth) - 1; |
---|
1721 | UInt uiBPelY = uiTPelY + (g_uiMaxCUHeight>>uiDepth) - 1; |
---|
1722 | TComSlice * pcSlice = pcCU->getPic()->getSlice(pcCU->getPic()->getCurrSliceIdx()); |
---|
1723 | Bool bSliceStart = pcSlice->getSliceSegmentCurStartCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && |
---|
1724 | pcSlice->getSliceSegmentCurStartCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) ); |
---|
1725 | Bool bSliceEnd = pcSlice->getSliceSegmentCurEndCUAddr() > rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx && |
---|
1726 | pcSlice->getSliceSegmentCurEndCUAddr() < rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) ); |
---|
1727 | if(!bSliceEnd && !bSliceStart && ( uiRPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiBPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
1728 | { |
---|
1729 | UInt uiAbsPartIdxInRaster = g_auiZscanToRaster[uiAbsPartIdx]; |
---|
1730 | UInt uiSrcBlkWidth = rpcPic->getNumPartInWidth() >> (uiSrcDepth); |
---|
1731 | UInt uiBlkWidth = rpcPic->getNumPartInWidth() >> (uiDepth); |
---|
1732 | UInt uiPartIdxX = ( ( uiAbsPartIdxInRaster % rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth; |
---|
1733 | UInt uiPartIdxY = ( ( uiAbsPartIdxInRaster / rpcPic->getNumPartInWidth() ) % uiSrcBlkWidth) / uiBlkWidth; |
---|
1734 | UInt uiPartIdx = uiPartIdxY * ( uiSrcBlkWidth / uiBlkWidth ) + uiPartIdxX; |
---|
1735 | m_ppcRecoYuvBest[uiSrcDepth]->copyToPicYuv( rpcPic->getPicYuvRec (), uiCUAddr, uiAbsPartIdx, uiDepth - uiSrcDepth, uiPartIdx); |
---|
1736 | } |
---|
1737 | else |
---|
1738 | { |
---|
1739 | UInt uiQNumParts = ( pcCU->getPic()->getNumPartInCU() >> (uiDepth<<1) )>>2; |
---|
1740 | |
---|
1741 | for ( UInt uiPartUnitIdx = 0; uiPartUnitIdx < 4; uiPartUnitIdx++, uiAbsPartIdx+=uiQNumParts ) |
---|
1742 | { |
---|
1743 | UInt uiSubCULPelX = uiLPelX + ( g_uiMaxCUWidth >>(uiDepth+1) )*( uiPartUnitIdx & 1 ); |
---|
1744 | UInt uiSubCUTPelY = uiTPelY + ( g_uiMaxCUHeight>>(uiDepth+1) )*( uiPartUnitIdx >> 1 ); |
---|
1745 | |
---|
1746 | Bool bInSlice = rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx+uiQNumParts > pcSlice->getSliceSegmentCurStartCUAddr() && |
---|
1747 | rpcPic->getPicSym()->getInverseCUOrderMap(pcCU->getAddr())*pcCU->getPic()->getNumPartInCU()+uiAbsPartIdx < pcSlice->getSliceSegmentCurEndCUAddr(); |
---|
1748 | if(bInSlice&&( uiSubCULPelX < pcSlice->getSPS()->getPicWidthInLumaSamples() ) && ( uiSubCUTPelY < pcSlice->getSPS()->getPicHeightInLumaSamples() ) ) |
---|
1749 | { |
---|
1750 | xCopyYuv2Pic( rpcPic, uiCUAddr, uiAbsPartIdx, uiDepth+1, uiSrcDepth, pcCU, uiSubCULPelX, uiSubCUTPelY ); // Copy Yuv data to picture Yuv |
---|
1751 | } |
---|
1752 | } |
---|
1753 | } |
---|
1754 | } |
---|
1755 | |
---|
1756 | Void TEncCu::xCopyYuv2Tmp( UInt uiPartUnitIdx, UInt uiNextDepth ) |
---|
1757 | { |
---|
1758 | UInt uiCurrDepth = uiNextDepth - 1; |
---|
1759 | m_ppcRecoYuvBest[uiNextDepth]->copyToPartYuv( m_ppcRecoYuvTemp[uiCurrDepth], uiPartUnitIdx ); |
---|
1760 | } |
---|
1761 | |
---|
1762 | /** Function for filling the PCM buffer of a CU using its original sample array |
---|
1763 | * \param pcCU pointer to current CU |
---|
1764 | * \param pcOrgYuv pointer to original sample array |
---|
1765 | * \returns Void |
---|
1766 | */ |
---|
1767 | Void TEncCu::xFillPCMBuffer ( TComDataCU*& pCU, TComYuv* pOrgYuv ) |
---|
1768 | { |
---|
1769 | |
---|
1770 | UInt width = pCU->getWidth(0); |
---|
1771 | UInt height = pCU->getHeight(0); |
---|
1772 | |
---|
1773 | Pel* pSrcY = pOrgYuv->getLumaAddr(0, width); |
---|
1774 | Pel* pDstY = pCU->getPCMSampleY(); |
---|
1775 | UInt srcStride = pOrgYuv->getStride(); |
---|
1776 | |
---|
1777 | for(Int y = 0; y < height; y++ ) |
---|
1778 | { |
---|
1779 | for(Int x = 0; x < width; x++ ) |
---|
1780 | { |
---|
1781 | pDstY[x] = pSrcY[x]; |
---|
1782 | } |
---|
1783 | pDstY += width; |
---|
1784 | pSrcY += srcStride; |
---|
1785 | } |
---|
1786 | |
---|
1787 | Pel* pSrcCb = pOrgYuv->getCbAddr(); |
---|
1788 | Pel* pSrcCr = pOrgYuv->getCrAddr();; |
---|
1789 | |
---|
1790 | Pel* pDstCb = pCU->getPCMSampleCb(); |
---|
1791 | Pel* pDstCr = pCU->getPCMSampleCr();; |
---|
1792 | |
---|
1793 | UInt srcStrideC = pOrgYuv->getCStride(); |
---|
1794 | UInt heightC = height >> 1; |
---|
1795 | UInt widthC = width >> 1; |
---|
1796 | |
---|
1797 | for(Int y = 0; y < heightC; y++ ) |
---|
1798 | { |
---|
1799 | for(Int x = 0; x < widthC; x++ ) |
---|
1800 | { |
---|
1801 | pDstCb[x] = pSrcCb[x]; |
---|
1802 | pDstCr[x] = pSrcCr[x]; |
---|
1803 | } |
---|
1804 | pDstCb += widthC; |
---|
1805 | pDstCr += widthC; |
---|
1806 | pSrcCb += srcStrideC; |
---|
1807 | pSrcCr += srcStrideC; |
---|
1808 | } |
---|
1809 | } |
---|
1810 | |
---|
1811 | #if ADAPTIVE_QP_SELECTION |
---|
1812 | /** Collect ARL statistics from one block |
---|
1813 | */ |
---|
1814 | Int TEncCu::xTuCollectARLStats(TCoeff* rpcCoeff, Int* rpcArlCoeff, Int NumCoeffInCU, Double* cSum, UInt* numSamples ) |
---|
1815 | { |
---|
1816 | for( Int n = 0; n < NumCoeffInCU; n++ ) |
---|
1817 | { |
---|
1818 | Int u = abs( rpcCoeff[ n ] ); |
---|
1819 | Int absc = rpcArlCoeff[ n ]; |
---|
1820 | |
---|
1821 | if( u != 0 ) |
---|
1822 | { |
---|
1823 | if( u < LEVEL_RANGE ) |
---|
1824 | { |
---|
1825 | cSum[ u ] += ( Double )absc; |
---|
1826 | numSamples[ u ]++; |
---|
1827 | } |
---|
1828 | else |
---|
1829 | { |
---|
1830 | cSum[ LEVEL_RANGE ] += ( Double )absc - ( Double )( u << ARL_C_PRECISION ); |
---|
1831 | numSamples[ LEVEL_RANGE ]++; |
---|
1832 | } |
---|
1833 | } |
---|
1834 | } |
---|
1835 | |
---|
1836 | return 0; |
---|
1837 | } |
---|
1838 | |
---|
1839 | /** Collect ARL statistics from one LCU |
---|
1840 | * \param pcCU |
---|
1841 | */ |
---|
1842 | Void TEncCu::xLcuCollectARLStats(TComDataCU* rpcCU ) |
---|
1843 | { |
---|
1844 | Double cSum[ LEVEL_RANGE + 1 ]; //: the sum of DCT coefficients corresponding to datatype and quantization output |
---|
1845 | UInt numSamples[ LEVEL_RANGE + 1 ]; //: the number of coefficients corresponding to datatype and quantization output |
---|
1846 | |
---|
1847 | TCoeff* pCoeffY = rpcCU->getCoeffY(); |
---|
1848 | Int* pArlCoeffY = rpcCU->getArlCoeffY(); |
---|
1849 | |
---|
1850 | UInt uiMinCUWidth = g_uiMaxCUWidth >> g_uiMaxCUDepth; |
---|
1851 | UInt uiMinNumCoeffInCU = 1 << uiMinCUWidth; |
---|
1852 | |
---|
1853 | memset( cSum, 0, sizeof( Double )*(LEVEL_RANGE+1) ); |
---|
1854 | memset( numSamples, 0, sizeof( UInt )*(LEVEL_RANGE+1) ); |
---|
1855 | |
---|
1856 | // Collect stats to cSum[][] and numSamples[][] |
---|
1857 | for(Int i = 0; i < rpcCU->getTotalNumPart(); i ++ ) |
---|
1858 | { |
---|
1859 | UInt uiTrIdx = rpcCU->getTransformIdx(i); |
---|
1860 | |
---|
1861 | if(rpcCU->getPredictionMode(i) == MODE_INTER) |
---|
1862 | if( rpcCU->getCbf( i, TEXT_LUMA, uiTrIdx ) ) |
---|
1863 | { |
---|
1864 | xTuCollectARLStats(pCoeffY, pArlCoeffY, uiMinNumCoeffInCU, cSum, numSamples); |
---|
1865 | }//Note that only InterY is processed. QP rounding is based on InterY data only. |
---|
1866 | |
---|
1867 | pCoeffY += uiMinNumCoeffInCU; |
---|
1868 | pArlCoeffY += uiMinNumCoeffInCU; |
---|
1869 | } |
---|
1870 | |
---|
1871 | for(Int u=1; u<LEVEL_RANGE;u++) |
---|
1872 | { |
---|
1873 | m_pcTrQuant->getSliceSumC()[u] += cSum[ u ] ; |
---|
1874 | m_pcTrQuant->getSliceNSamples()[u] += numSamples[ u ] ; |
---|
1875 | } |
---|
1876 | m_pcTrQuant->getSliceSumC()[LEVEL_RANGE] += cSum[ LEVEL_RANGE ] ; |
---|
1877 | m_pcTrQuant->getSliceNSamples()[LEVEL_RANGE] += numSamples[ LEVEL_RANGE ] ; |
---|
1878 | } |
---|
1879 | #endif |
---|
1880 | |
---|
1881 | #if INTRA_BL |
---|
1882 | Void TEncCu::xCheckRDCostIntraBL( TComDataCU*& rpcBestCU, TComDataCU*& rpcTempCU ) |
---|
1883 | { |
---|
1884 | UInt uiDepth = rpcTempCU->getDepth( 0 ); |
---|
1885 | rpcTempCU->setSkipFlagSubParts( false, 0, uiDepth ); |
---|
1886 | rpcTempCU->setPartSizeSubParts( SIZE_2Nx2N, 0, uiDepth ); |
---|
1887 | rpcTempCU->setPredModeSubParts( MODE_INTRA_BL, 0, uiDepth ); |
---|
1888 | rpcTempCU->setCUTransquantBypassSubParts( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uiDepth ); |
---|
1889 | |
---|
1890 | m_pcPredSearch->setBaseRecPic( m_pcPicYuvRecBase ); |
---|
1891 | #if NO_RESIDUAL_FLAG_FOR_BLPRED |
---|
1892 | rpcTempCU->setDepthSubParts( uiDepth, 0 ); |
---|
1893 | // rpcTempCU->setLumaIntraDirSubParts( DC_IDX, 0, uiDepth ); |
---|
1894 | // rpcTempCU->setChromIntraDirSubParts( DC_IDX, 0, uiDepth ); |
---|
1895 | m_ppcPredYuvTemp[uiDepth]->copyFromPicLuma ( rpcTempCU->getSlice()->getFullPelBaseRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU(), 0, rpcTempCU->getWidth(0), rpcTempCU->getHeight(0)); |
---|
1896 | m_ppcPredYuvTemp[uiDepth]->copyFromPicChroma( rpcTempCU->getSlice()->getFullPelBaseRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU(), 0, (rpcTempCU->getWidth(0)>>1), (rpcTempCU->getHeight(0)>>1), 0); |
---|
1897 | m_ppcPredYuvTemp[uiDepth]->copyFromPicChroma( rpcTempCU->getSlice()->getFullPelBaseRec(), rpcTempCU->getAddr(), rpcTempCU->getZorderIdxInCU(), 0, (rpcTempCU->getWidth(0)>>1), (rpcTempCU->getHeight(0)>>1), 1); |
---|
1898 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcResiYuvBest[uiDepth], m_ppcRecoYuvTemp[uiDepth], false ); |
---|
1899 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
1900 | #else |
---|
1901 | |
---|
1902 | m_pcPredSearch->estIntraBLPredQT( rpcTempCU, m_ppcOrigYuv[uiDepth], m_ppcPredYuvTemp[uiDepth], m_ppcResiYuvTemp[uiDepth], m_ppcRecoYuvTemp[uiDepth] ); |
---|
1903 | |
---|
1904 | m_pcEntropyCoder->resetBits(); |
---|
1905 | m_pcEntropyCoder->encodeIntraBLFlag ( rpcTempCU, 0, true ); |
---|
1906 | m_pcEntropyCoder->encodeSkipFlag( rpcTempCU, 0, true ); |
---|
1907 | if ( rpcTempCU->getSlice()->getPPS()->getTransquantBypassEnableFlag()) |
---|
1908 | { |
---|
1909 | m_pcEntropyCoder->encodeCUTransquantBypassFlag( rpcTempCU, 0, true ); |
---|
1910 | } |
---|
1911 | |
---|
1912 | // Encode Coefficients |
---|
1913 | Bool bCodeDQP = getdQPFlag(); |
---|
1914 | m_pcEntropyCoder->encodeCoeff( rpcTempCU, 0, uiDepth, rpcTempCU->getWidth (0), rpcTempCU->getHeight(0), bCodeDQP ); |
---|
1915 | setdQPFlag( bCodeDQP ); |
---|
1916 | |
---|
1917 | if( m_bUseSBACRD ) m_pcRDGoOnSbacCoder->store(m_pppcRDSbacCoder[uiDepth][CI_TEMP_BEST]); |
---|
1918 | |
---|
1919 | rpcTempCU->getTotalBits() = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
1920 | if(m_pcEncCfg->getUseSBACRD()) |
---|
1921 | { |
---|
1922 | rpcTempCU->getTotalBins() = ((TEncBinCABAC *)((TEncSbac*)m_pcEntropyCoder->m_pcEntropyCoderIf)->getEncBinIf())->getBinsCoded(); |
---|
1923 | } |
---|
1924 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
1925 | #endif |
---|
1926 | |
---|
1927 | xCheckDQP( rpcTempCU ); |
---|
1928 | xCheckBestMode(rpcBestCU, rpcTempCU, uiDepth); |
---|
1929 | } |
---|
1930 | #endif |
---|
1931 | #if (ENCODER_FAST_MODE) |
---|
1932 | Void TEncCu::xCheckRDCostILRUni(TComDataCU *&rpcBestCU, TComDataCU *&rpcTempCU, UInt refLayerId) |
---|
1933 | { |
---|
1934 | UChar uhDepth = rpcTempCU->getDepth( 0 ); |
---|
1935 | rpcTempCU->setDepthSubParts( uhDepth, 0 ); |
---|
1936 | #if SKIP_FLAG |
---|
1937 | rpcTempCU->setSkipFlagSubParts( false, 0, uhDepth ); |
---|
1938 | #endif |
---|
1939 | rpcTempCU->setPartSizeSubParts ( SIZE_2Nx2N, 0, uhDepth ); //2Nx2N |
---|
1940 | rpcTempCU->setPredModeSubParts ( MODE_INTER, 0, uhDepth ); |
---|
1941 | rpcTempCU->setCUTransquantBypassSubParts ( m_pcEncCfg->getCUTransquantBypassFlagValue(), 0, uhDepth ); |
---|
1942 | Bool exitILR = m_pcPredSearch->predInterSearchILRUni( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcRecoYuvTemp[uhDepth], refLayerId ); |
---|
1943 | if(!exitILR) |
---|
1944 | { |
---|
1945 | return; |
---|
1946 | } |
---|
1947 | m_pcPredSearch->encodeResAndCalcRdInterCU( rpcTempCU, m_ppcOrigYuv[uhDepth], m_ppcPredYuvTemp[uhDepth], m_ppcResiYuvTemp[uhDepth], m_ppcResiYuvBest[uhDepth], m_ppcRecoYuvTemp[uhDepth], false ); |
---|
1948 | rpcTempCU->getTotalCost() = m_pcRdCost->calcRdCost( rpcTempCU->getTotalBits(), rpcTempCU->getTotalDistortion() ); |
---|
1949 | xCheckDQP( rpcTempCU ); |
---|
1950 | xCheckBestMode(rpcBestCU, rpcTempCU, uhDepth); |
---|
1951 | return; |
---|
1952 | } |
---|
1953 | #endif |
---|
1954 | //! \} |
---|