1 | /* The copyright in this software is being made available under the BSD |
---|
2 | * License, included below. This software may be subject to other third party |
---|
3 | * and contributor rights, including patent rights, and no such rights are |
---|
4 | * granted under this license. |
---|
5 | * |
---|
6 | * Copyright (c) 2010-2014, ITU/ISO/IEC |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * |
---|
12 | * * Redistributions of source code must retain the above copyright notice, |
---|
13 | * this list of conditions and the following disclaimer. |
---|
14 | * * Redistributions in binary form must reproduce the above copyright notice, |
---|
15 | * this list of conditions and the following disclaimer in the documentation |
---|
16 | * and/or other materials provided with the distribution. |
---|
17 | * * Neither the name of the ITU/ISO/IEC nor the names of its contributors may |
---|
18 | * be used to endorse or promote products derived from this software without |
---|
19 | * specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
---|
25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | */ |
---|
33 | |
---|
34 | /** \file TEncSlice.cpp |
---|
35 | \brief slice encoder class |
---|
36 | */ |
---|
37 | |
---|
38 | #include "TEncTop.h" |
---|
39 | #include "TEncSlice.h" |
---|
40 | #include <math.h> |
---|
41 | |
---|
42 | //! \ingroup TLibEncoder |
---|
43 | //! \{ |
---|
44 | |
---|
45 | // ==================================================================================================================== |
---|
46 | // Constructor / destructor / create / destroy |
---|
47 | // ==================================================================================================================== |
---|
48 | |
---|
49 | TEncSlice::TEncSlice() |
---|
50 | { |
---|
51 | m_apcPicYuvPred = NULL; |
---|
52 | m_apcPicYuvResi = NULL; |
---|
53 | |
---|
54 | m_pdRdPicLambda = NULL; |
---|
55 | m_pdRdPicQp = NULL; |
---|
56 | m_piRdPicQp = NULL; |
---|
57 | } |
---|
58 | |
---|
59 | TEncSlice::~TEncSlice() |
---|
60 | { |
---|
61 | } |
---|
62 | |
---|
63 | Void TEncSlice::create( Int iWidth, Int iHeight, ChromaFormat chromaFormat, UInt iMaxCUWidth, UInt iMaxCUHeight, UChar uhTotalDepth ) |
---|
64 | { |
---|
65 | // create prediction picture |
---|
66 | if ( m_apcPicYuvPred == NULL ) |
---|
67 | { |
---|
68 | m_apcPicYuvPred = new TComPicYuv; |
---|
69 | m_apcPicYuvPred->create( iWidth, iHeight, chromaFormat, iMaxCUWidth, iMaxCUHeight, uhTotalDepth ); |
---|
70 | } |
---|
71 | |
---|
72 | // create residual picture |
---|
73 | if( m_apcPicYuvResi == NULL ) |
---|
74 | { |
---|
75 | m_apcPicYuvResi = new TComPicYuv; |
---|
76 | m_apcPicYuvResi->create( iWidth, iHeight, chromaFormat, iMaxCUWidth, iMaxCUHeight, uhTotalDepth ); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | Void TEncSlice::destroy() |
---|
81 | { |
---|
82 | // destroy prediction picture |
---|
83 | if ( m_apcPicYuvPred ) |
---|
84 | { |
---|
85 | m_apcPicYuvPred->destroy(); |
---|
86 | delete m_apcPicYuvPred; |
---|
87 | m_apcPicYuvPred = NULL; |
---|
88 | } |
---|
89 | |
---|
90 | // destroy residual picture |
---|
91 | if ( m_apcPicYuvResi ) |
---|
92 | { |
---|
93 | m_apcPicYuvResi->destroy(); |
---|
94 | delete m_apcPicYuvResi; |
---|
95 | m_apcPicYuvResi = NULL; |
---|
96 | } |
---|
97 | |
---|
98 | // free lambda and QP arrays |
---|
99 | if ( m_pdRdPicLambda ) { xFree( m_pdRdPicLambda ); m_pdRdPicLambda = NULL; } |
---|
100 | if ( m_pdRdPicQp ) { xFree( m_pdRdPicQp ); m_pdRdPicQp = NULL; } |
---|
101 | if ( m_piRdPicQp ) { xFree( m_piRdPicQp ); m_piRdPicQp = NULL; } |
---|
102 | } |
---|
103 | |
---|
104 | Void TEncSlice::init( TEncTop* pcEncTop ) |
---|
105 | { |
---|
106 | m_pcCfg = pcEncTop; |
---|
107 | m_pcListPic = pcEncTop->getListPic(); |
---|
108 | #if SVC_EXTENSION |
---|
109 | m_ppcTEncTop = pcEncTop->getLayerEnc(); |
---|
110 | #endif |
---|
111 | m_pcGOPEncoder = pcEncTop->getGOPEncoder(); |
---|
112 | m_pcCuEncoder = pcEncTop->getCuEncoder(); |
---|
113 | m_pcPredSearch = pcEncTop->getPredSearch(); |
---|
114 | |
---|
115 | m_pcEntropyCoder = pcEncTop->getEntropyCoder(); |
---|
116 | m_pcSbacCoder = pcEncTop->getSbacCoder(); |
---|
117 | m_pcBinCABAC = pcEncTop->getBinCABAC(); |
---|
118 | m_pcTrQuant = pcEncTop->getTrQuant(); |
---|
119 | |
---|
120 | m_pcRdCost = pcEncTop->getRdCost(); |
---|
121 | m_pppcRDSbacCoder = pcEncTop->getRDSbacCoder(); |
---|
122 | m_pcRDGoOnSbacCoder = pcEncTop->getRDGoOnSbacCoder(); |
---|
123 | |
---|
124 | // create lambda and QP arrays |
---|
125 | m_pdRdPicLambda = (Double*)xMalloc( Double, m_pcCfg->getDeltaQpRD() * 2 + 1 ); |
---|
126 | m_pdRdPicQp = (Double*)xMalloc( Double, m_pcCfg->getDeltaQpRD() * 2 + 1 ); |
---|
127 | m_piRdPicQp = (Int* )xMalloc( Int, m_pcCfg->getDeltaQpRD() * 2 + 1 ); |
---|
128 | m_pcRateCtrl = pcEncTop->getRateCtrl(); |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | Void |
---|
134 | #if JCTVC_M0259_LAMBDAREFINEMENT |
---|
135 | TEncSlice::setUpLambda(TComSlice* slice, Double dLambda, Int iQP, Int depth) |
---|
136 | #else |
---|
137 | TEncSlice::setUpLambda(TComSlice* slice, const Double dLambda, Int iQP) |
---|
138 | #endif |
---|
139 | { |
---|
140 | #if JCTVC_M0259_LAMBDAREFINEMENT |
---|
141 | Int layerIdx = slice->getLayerIdx(); |
---|
142 | |
---|
143 | if( slice->getLayerId() > 0 && m_ppcTEncTop[layerIdx]->getNumActiveRefLayers() && depth >= 3 && m_pcCfg->getGOPSize() == ( 1 << depth ) ) |
---|
144 | { |
---|
145 | UInt prevLayerIdx = 0; |
---|
146 | if( m_ppcTEncTop[layerIdx]->getNumActiveRefLayers() > 0 ) |
---|
147 | { |
---|
148 | prevLayerIdx = m_ppcTEncTop[layerIdx]->getPredLayerIdx( m_ppcTEncTop[layerIdx]->getNumActiveRefLayers() - 1); |
---|
149 | } |
---|
150 | |
---|
151 | Double gamma = xCalEnhLambdaFactor( m_ppcTEncTop[prevLayerIdx]->getQP() - m_ppcTEncTop[layerIdx]->getQP() , |
---|
152 | 1.0 * m_ppcTEncTop[layerIdx]->getSourceWidth() * m_ppcTEncTop[layerIdx]->getSourceHeight() |
---|
153 | / m_ppcTEncTop[prevLayerIdx]->getSourceWidth() / m_ppcTEncTop[prevLayerIdx]->getSourceHeight() ); |
---|
154 | |
---|
155 | dLambda *= gamma; |
---|
156 | } |
---|
157 | #endif |
---|
158 | |
---|
159 | // store lambda |
---|
160 | m_pcRdCost ->setLambda( dLambda ); |
---|
161 | |
---|
162 | // for RDO |
---|
163 | // in RdCost there is only one lambda because the luma and chroma bits are not separated, instead we weight the distortion of chroma. |
---|
164 | Double dLambdas[MAX_NUM_COMPONENT] = { dLambda }; |
---|
165 | for(UInt compIdx=1; compIdx<MAX_NUM_COMPONENT; compIdx++) |
---|
166 | { |
---|
167 | const ComponentID compID=ComponentID(compIdx); |
---|
168 | Int chromaQPOffset = slice->getPPS()->getQpOffset(compID) + slice->getSliceChromaQpDelta(compID); |
---|
169 | Int qpc=(iQP + chromaQPOffset < 0) ? iQP : getScaledChromaQP(iQP + chromaQPOffset, m_pcCfg->getChromaFormatIdc()); |
---|
170 | Double tmpWeight = pow( 2.0, (iQP-qpc)/3.0 ); // takes into account of the chroma qp mapping and chroma qp Offset |
---|
171 | |
---|
172 | #if JCTVC_M0259_LAMBDAREFINEMENT |
---|
173 | if( slice->getLayerId() > 0 && m_ppcTEncTop[slice->getLayerIdx()]->getNumActiveRefLayers() && m_pcCfg->getGOPSize() >= 8 && slice->isIntra() == false && depth == 0 ) |
---|
174 | { |
---|
175 | dLambdas[0] = dLambda * 1.1; |
---|
176 | m_pcRdCost->setLambda( dLambdas[0] ); |
---|
177 | |
---|
178 | m_pcRdCost->setDistortionWeight(compID, tmpWeight * 1.15); |
---|
179 | dLambdas[compIdx] = dLambdas[0] / tmpWeight / 1.15; |
---|
180 | } |
---|
181 | else |
---|
182 | { |
---|
183 | #endif |
---|
184 | m_pcRdCost->setDistortionWeight(compID, tmpWeight); |
---|
185 | dLambdas[compIdx]=dLambda/tmpWeight; |
---|
186 | #if JCTVC_M0259_LAMBDAREFINEMENT |
---|
187 | } |
---|
188 | #endif |
---|
189 | } |
---|
190 | |
---|
191 | #if RDOQ_CHROMA_LAMBDA |
---|
192 | // for RDOQ |
---|
193 | m_pcTrQuant->setLambdas( dLambdas ); |
---|
194 | #else |
---|
195 | m_pcTrQuant->setLambda( dLambda ); |
---|
196 | #endif |
---|
197 | |
---|
198 | // For SAO |
---|
199 | slice ->setLambdas( dLambdas ); |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | |
---|
204 | /** |
---|
205 | - non-referenced frame marking |
---|
206 | - QP computation based on temporal structure |
---|
207 | - lambda computation based on QP |
---|
208 | - set temporal layer ID and the parameter sets |
---|
209 | . |
---|
210 | \param pcPic picture class |
---|
211 | \param pocLast POC of last picture |
---|
212 | \param pocCurr current POC |
---|
213 | \param iNumPicRcvd number of received pictures |
---|
214 | \param iTimeOffset POC offset for hierarchical structure |
---|
215 | \param iDepth temporal layer depth |
---|
216 | \param rpcSlice slice header class |
---|
217 | \param pSPS SPS associated with the slice |
---|
218 | \param pPPS PPS associated with the slice |
---|
219 | */ |
---|
220 | #if SVC_EXTENSION |
---|
221 | //\param vps VPS associated with the slice |
---|
222 | Void TEncSlice::initEncSlice( TComPic* pcPic, Int pocLast, Int pocCurr, Int iNumPicRcvd, Int iGOPid, TComSlice*& rpcSlice, TComSPS* pSPS, TComPPS *pPPS, TComVPS *vps, Bool isField ) |
---|
223 | #else |
---|
224 | Void TEncSlice::initEncSlice( TComPic* pcPic, Int pocLast, Int pocCurr, Int iNumPicRcvd, Int iGOPid, TComSlice*& rpcSlice, TComSPS* pSPS, TComPPS *pPPS, Bool isField ) |
---|
225 | #endif |
---|
226 | { |
---|
227 | Double dQP; |
---|
228 | Double dLambda; |
---|
229 | |
---|
230 | rpcSlice = pcPic->getSlice(0); |
---|
231 | rpcSlice->setSPS( pSPS ); |
---|
232 | rpcSlice->setPPS( pPPS ); |
---|
233 | rpcSlice->setSliceBits(0); |
---|
234 | rpcSlice->setPic( pcPic ); |
---|
235 | #if SVC_EXTENSION |
---|
236 | UInt layerId = pcPic->getLayerId(); |
---|
237 | rpcSlice->setVPS( vps ); |
---|
238 | rpcSlice->initSlice( layerId ); |
---|
239 | #else |
---|
240 | rpcSlice->initSlice(); |
---|
241 | #endif |
---|
242 | rpcSlice->setPicOutputFlag( true ); |
---|
243 | rpcSlice->setPOC( pocCurr ); |
---|
244 | |
---|
245 | // depth computation based on GOP size |
---|
246 | Int depth; |
---|
247 | { |
---|
248 | Int poc = rpcSlice->getPOC(); |
---|
249 | if(isField) |
---|
250 | { |
---|
251 | poc = (poc/2) % (m_pcCfg->getGOPSize()/2); |
---|
252 | } |
---|
253 | else |
---|
254 | { |
---|
255 | poc = poc % m_pcCfg->getGOPSize(); |
---|
256 | } |
---|
257 | |
---|
258 | if ( poc == 0 ) |
---|
259 | { |
---|
260 | depth = 0; |
---|
261 | } |
---|
262 | else |
---|
263 | { |
---|
264 | Int step = m_pcCfg->getGOPSize(); |
---|
265 | depth = 0; |
---|
266 | for( Int i=step>>1; i>=1; i>>=1 ) |
---|
267 | { |
---|
268 | for ( Int j=i; j<m_pcCfg->getGOPSize(); j+=step ) |
---|
269 | { |
---|
270 | if ( j == poc ) |
---|
271 | { |
---|
272 | i=0; |
---|
273 | break; |
---|
274 | } |
---|
275 | } |
---|
276 | step >>= 1; |
---|
277 | depth++; |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | #if HARMONIZE_GOP_FIRST_FIELD_COUPLE |
---|
282 | if(poc != 0) |
---|
283 | { |
---|
284 | #endif |
---|
285 | if (isField && ((rpcSlice->getPOC() % 2) == 1)) |
---|
286 | { |
---|
287 | depth ++; |
---|
288 | } |
---|
289 | #if HARMONIZE_GOP_FIRST_FIELD_COUPLE |
---|
290 | } |
---|
291 | #endif |
---|
292 | } |
---|
293 | |
---|
294 | // slice type |
---|
295 | SliceType eSliceType; |
---|
296 | |
---|
297 | eSliceType=B_SLICE; |
---|
298 | #if EFFICIENT_FIELD_IRAP |
---|
299 | if(!(isField && pocLast == 1)) |
---|
300 | { |
---|
301 | #endif // EFFICIENT_FIELD_IRAP |
---|
302 | #if ALLOW_RECOVERY_POINT_AS_RAP |
---|
303 | if(m_pcCfg->getDecodingRefreshType() == 3) |
---|
304 | { |
---|
305 | eSliceType = (pocLast == 0 || pocCurr % m_pcCfg->getIntraPeriod() == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType; |
---|
306 | } |
---|
307 | else |
---|
308 | { |
---|
309 | #endif |
---|
310 | eSliceType = (pocLast == 0 || (pocCurr - (isField ? 1 : 0)) % m_pcCfg->getIntraPeriod() == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType; |
---|
311 | #if ALLOW_RECOVERY_POINT_AS_RAP |
---|
312 | } |
---|
313 | #endif |
---|
314 | #if EFFICIENT_FIELD_IRAP |
---|
315 | } |
---|
316 | #endif |
---|
317 | |
---|
318 | rpcSlice->setSliceType ( eSliceType ); |
---|
319 | |
---|
320 | // ------------------------------------------------------------------------------------------------------------------ |
---|
321 | // Non-referenced frame marking |
---|
322 | // ------------------------------------------------------------------------------------------------------------------ |
---|
323 | |
---|
324 | if(pocLast == 0) |
---|
325 | { |
---|
326 | rpcSlice->setTemporalLayerNonReferenceFlag(false); |
---|
327 | } |
---|
328 | else |
---|
329 | { |
---|
330 | rpcSlice->setTemporalLayerNonReferenceFlag(!m_pcCfg->getGOPEntry(iGOPid).m_refPic); |
---|
331 | } |
---|
332 | rpcSlice->setReferenced(true); |
---|
333 | |
---|
334 | // ------------------------------------------------------------------------------------------------------------------ |
---|
335 | // QP setting |
---|
336 | // ------------------------------------------------------------------------------------------------------------------ |
---|
337 | |
---|
338 | dQP = m_pcCfg->getQP(); |
---|
339 | if(eSliceType!=I_SLICE) |
---|
340 | { |
---|
341 | #if REPN_FORMAT_IN_VPS |
---|
342 | if (!(( m_pcCfg->getMaxDeltaQP() == 0 ) && (dQP == -rpcSlice->getQpBDOffsetY() ) && (rpcSlice->getPPS()->getTransquantBypassEnableFlag()))) |
---|
343 | #else |
---|
344 | if (!(( m_pcCfg->getMaxDeltaQP() == 0 ) && (dQP == -rpcSlice->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA) ) && (rpcSlice->getPPS()->getTransquantBypassEnableFlag()))) |
---|
345 | #endif |
---|
346 | { |
---|
347 | dQP += m_pcCfg->getGOPEntry(iGOPid).m_QPOffset; |
---|
348 | } |
---|
349 | } |
---|
350 | |
---|
351 | // modify QP |
---|
352 | Int* pdQPs = m_pcCfg->getdQPs(); |
---|
353 | if ( pdQPs ) |
---|
354 | { |
---|
355 | dQP += pdQPs[ rpcSlice->getPOC() ]; |
---|
356 | } |
---|
357 | |
---|
358 | if (m_pcCfg->getCostMode()==COST_LOSSLESS_CODING) |
---|
359 | { |
---|
360 | dQP=LOSSLESS_AND_MIXED_LOSSLESS_RD_COST_TEST_QP; |
---|
361 | m_pcCfg->setDeltaQpRD(0); |
---|
362 | } |
---|
363 | |
---|
364 | // ------------------------------------------------------------------------------------------------------------------ |
---|
365 | // Lambda computation |
---|
366 | // ------------------------------------------------------------------------------------------------------------------ |
---|
367 | |
---|
368 | Int iQP; |
---|
369 | Double dOrigQP = dQP; |
---|
370 | |
---|
371 | // pre-compute lambda and QP values for all possible QP candidates |
---|
372 | for ( Int iDQpIdx = 0; iDQpIdx < 2 * m_pcCfg->getDeltaQpRD() + 1; iDQpIdx++ ) |
---|
373 | { |
---|
374 | // compute QP value |
---|
375 | dQP = dOrigQP + ((iDQpIdx+1)>>1)*(iDQpIdx%2 ? -1 : 1); |
---|
376 | |
---|
377 | // compute lambda value |
---|
378 | Int NumberBFrames = ( m_pcCfg->getGOPSize() - 1 ); |
---|
379 | Int SHIFT_QP = 12; |
---|
380 | |
---|
381 | Double dLambda_scale = 1.0 - Clip3( 0.0, 0.5, 0.05*(Double)(isField ? NumberBFrames/2 : NumberBFrames) ); |
---|
382 | |
---|
383 | #if FULL_NBIT |
---|
384 | Int bitdepth_luma_qp_scale = 6 * (g_bitDepth[CHANNEL_TYPE_LUMA] - 8); |
---|
385 | #else |
---|
386 | Int bitdepth_luma_qp_scale = 0; |
---|
387 | #endif |
---|
388 | Double qp_temp = (Double) dQP + bitdepth_luma_qp_scale - SHIFT_QP; |
---|
389 | #if FULL_NBIT |
---|
390 | Double qp_temp_orig = (Double) dQP - SHIFT_QP; |
---|
391 | #endif |
---|
392 | // Case #1: I or P-slices (key-frame) |
---|
393 | Double dQPFactor = m_pcCfg->getGOPEntry(iGOPid).m_QPFactor; |
---|
394 | if ( eSliceType==I_SLICE ) |
---|
395 | { |
---|
396 | dQPFactor=0.57*dLambda_scale; |
---|
397 | } |
---|
398 | dLambda = dQPFactor*pow( 2.0, qp_temp/3.0 ); |
---|
399 | |
---|
400 | if ( depth>0 ) |
---|
401 | { |
---|
402 | #if FULL_NBIT |
---|
403 | dLambda *= Clip3( 2.00, 4.00, (qp_temp_orig / 6.0) ); // (j == B_SLICE && p_cur_frm->layer != 0 ) |
---|
404 | #else |
---|
405 | dLambda *= Clip3( 2.00, 4.00, (qp_temp / 6.0) ); // (j == B_SLICE && p_cur_frm->layer != 0 ) |
---|
406 | #endif |
---|
407 | } |
---|
408 | |
---|
409 | // if hadamard is used in ME process |
---|
410 | if ( !m_pcCfg->getUseHADME() && rpcSlice->getSliceType( ) != I_SLICE ) |
---|
411 | { |
---|
412 | dLambda *= 0.95; |
---|
413 | } |
---|
414 | |
---|
415 | #if REPN_FORMAT_IN_VPS |
---|
416 | iQP = max( -rpcSlice->getQpBDOffsetY(), min( MAX_QP, (Int) floor( dQP + 0.5 ) ) ); |
---|
417 | #else |
---|
418 | iQP = max( -pSPS->getQpBDOffset(CHANNEL_TYPE_LUMA), min( MAX_QP, (Int) floor( dQP + 0.5 ) ) ); |
---|
419 | #endif |
---|
420 | |
---|
421 | m_pdRdPicLambda[iDQpIdx] = dLambda; |
---|
422 | m_pdRdPicQp [iDQpIdx] = dQP; |
---|
423 | m_piRdPicQp [iDQpIdx] = iQP; |
---|
424 | } |
---|
425 | |
---|
426 | // obtain dQP = 0 case |
---|
427 | dLambda = m_pdRdPicLambda[0]; |
---|
428 | dQP = m_pdRdPicQp [0]; |
---|
429 | iQP = m_piRdPicQp [0]; |
---|
430 | |
---|
431 | if( rpcSlice->getSliceType( ) != I_SLICE ) |
---|
432 | { |
---|
433 | dLambda *= m_pcCfg->getLambdaModifier( m_pcCfg->getGOPEntry(iGOPid).m_temporalId ); |
---|
434 | } |
---|
435 | |
---|
436 | #if JCTVC_M0259_LAMBDAREFINEMENT |
---|
437 | setUpLambda(rpcSlice, dLambda, iQP, depth); |
---|
438 | #else |
---|
439 | setUpLambda(rpcSlice, dLambda, iQP); |
---|
440 | #endif |
---|
441 | #if HB_LAMBDA_FOR_LDC |
---|
442 | // restore original slice type |
---|
443 | |
---|
444 | #if EFFICIENT_FIELD_IRAP |
---|
445 | if(!(isField && pocLast == 1)) |
---|
446 | { |
---|
447 | #endif // EFFICIENT_FIELD_IRAP |
---|
448 | #if ALLOW_RECOVERY_POINT_AS_RAP |
---|
449 | if(m_pcCfg->getDecodingRefreshType() == 3) |
---|
450 | { |
---|
451 | eSliceType = (pocLast == 0 || (pocCurr) % m_pcCfg->getIntraPeriod() == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType; |
---|
452 | } |
---|
453 | else |
---|
454 | { |
---|
455 | #endif |
---|
456 | eSliceType = (pocLast == 0 || (pocCurr - (isField ? 1 : 0)) % m_pcCfg->getIntraPeriod() == 0 || m_pcGOPEncoder->getGOPSize() == 0) ? I_SLICE : eSliceType; |
---|
457 | #if ALLOW_RECOVERY_POINT_AS_RAP |
---|
458 | } |
---|
459 | #endif |
---|
460 | #if EFFICIENT_FIELD_IRAP |
---|
461 | } |
---|
462 | #endif // EFFICIENT_FIELD_IRAP |
---|
463 | |
---|
464 | #if SVC_EXTENSION |
---|
465 | if( m_pcCfg->getLayerId() > 0 && m_pcCfg->getNumActiveRefLayers() > 0 ) |
---|
466 | { |
---|
467 | eSliceType=B_SLICE; |
---|
468 | } |
---|
469 | #endif |
---|
470 | rpcSlice->setSliceType ( eSliceType ); |
---|
471 | #endif |
---|
472 | |
---|
473 | if (m_pcCfg->getUseRecalculateQPAccordingToLambda()) |
---|
474 | { |
---|
475 | dQP = xGetQPValueAccordingToLambda( dLambda ); |
---|
476 | #if REPN_FORMAT_IN_VPS |
---|
477 | iQP = max( -rpcSlice->getQpBDOffsetY(), min( MAX_QP, (Int) floor( dQP + 0.5 ) ) ); |
---|
478 | #else |
---|
479 | iQP = max( -pSPS->getQpBDOffset(CHANNEL_TYPE_LUMA), min( MAX_QP, (Int) floor( dQP + 0.5 ) ) ); |
---|
480 | #endif |
---|
481 | } |
---|
482 | |
---|
483 | rpcSlice->setSliceQp ( iQP ); |
---|
484 | #if ADAPTIVE_QP_SELECTION |
---|
485 | rpcSlice->setSliceQpBase ( iQP ); |
---|
486 | #endif |
---|
487 | rpcSlice->setSliceQpDelta ( 0 ); |
---|
488 | rpcSlice->setSliceChromaQpDelta( COMPONENT_Cb, 0 ); |
---|
489 | rpcSlice->setSliceChromaQpDelta( COMPONENT_Cr, 0 ); |
---|
490 | rpcSlice->setUseChromaQpAdj( pPPS->getChromaQpAdjTableSize() > 0 ); |
---|
491 | rpcSlice->setNumRefIdx(REF_PIC_LIST_0,m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive); |
---|
492 | rpcSlice->setNumRefIdx(REF_PIC_LIST_1,m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive); |
---|
493 | |
---|
494 | if ( m_pcCfg->getDeblockingFilterMetric() ) |
---|
495 | { |
---|
496 | rpcSlice->setDeblockingFilterOverrideFlag(true); |
---|
497 | rpcSlice->setDeblockingFilterDisable(false); |
---|
498 | rpcSlice->setDeblockingFilterBetaOffsetDiv2( 0 ); |
---|
499 | rpcSlice->setDeblockingFilterTcOffsetDiv2( 0 ); |
---|
500 | } |
---|
501 | else if (rpcSlice->getPPS()->getDeblockingFilterControlPresentFlag()) |
---|
502 | { |
---|
503 | rpcSlice->getPPS()->setDeblockingFilterOverrideEnabledFlag( !m_pcCfg->getLoopFilterOffsetInPPS() ); |
---|
504 | rpcSlice->setDeblockingFilterOverrideFlag( !m_pcCfg->getLoopFilterOffsetInPPS() ); |
---|
505 | rpcSlice->getPPS()->setPicDisableDeblockingFilterFlag( m_pcCfg->getLoopFilterDisable() ); |
---|
506 | rpcSlice->setDeblockingFilterDisable( m_pcCfg->getLoopFilterDisable() ); |
---|
507 | if ( !rpcSlice->getDeblockingFilterDisable()) |
---|
508 | { |
---|
509 | if ( !m_pcCfg->getLoopFilterOffsetInPPS() && eSliceType!=I_SLICE) |
---|
510 | { |
---|
511 | rpcSlice->getPPS()->setDeblockingFilterBetaOffsetDiv2( m_pcCfg->getGOPEntry(iGOPid).m_betaOffsetDiv2 + m_pcCfg->getLoopFilterBetaOffset() ); |
---|
512 | rpcSlice->getPPS()->setDeblockingFilterTcOffsetDiv2( m_pcCfg->getGOPEntry(iGOPid).m_tcOffsetDiv2 + m_pcCfg->getLoopFilterTcOffset() ); |
---|
513 | rpcSlice->setDeblockingFilterBetaOffsetDiv2( m_pcCfg->getGOPEntry(iGOPid).m_betaOffsetDiv2 + m_pcCfg->getLoopFilterBetaOffset() ); |
---|
514 | rpcSlice->setDeblockingFilterTcOffsetDiv2( m_pcCfg->getGOPEntry(iGOPid).m_tcOffsetDiv2 + m_pcCfg->getLoopFilterTcOffset() ); |
---|
515 | } |
---|
516 | else |
---|
517 | { |
---|
518 | rpcSlice->getPPS()->setDeblockingFilterBetaOffsetDiv2( m_pcCfg->getLoopFilterBetaOffset() ); |
---|
519 | rpcSlice->getPPS()->setDeblockingFilterTcOffsetDiv2( m_pcCfg->getLoopFilterTcOffset() ); |
---|
520 | rpcSlice->setDeblockingFilterBetaOffsetDiv2( m_pcCfg->getLoopFilterBetaOffset() ); |
---|
521 | rpcSlice->setDeblockingFilterTcOffsetDiv2( m_pcCfg->getLoopFilterTcOffset() ); |
---|
522 | } |
---|
523 | } |
---|
524 | } |
---|
525 | else |
---|
526 | { |
---|
527 | rpcSlice->setDeblockingFilterOverrideFlag( false ); |
---|
528 | rpcSlice->setDeblockingFilterDisable( false ); |
---|
529 | rpcSlice->setDeblockingFilterBetaOffsetDiv2( 0 ); |
---|
530 | rpcSlice->setDeblockingFilterTcOffsetDiv2( 0 ); |
---|
531 | } |
---|
532 | |
---|
533 | rpcSlice->setDepth ( depth ); |
---|
534 | |
---|
535 | pcPic->setTLayer( m_pcCfg->getGOPEntry(iGOPid).m_temporalId ); |
---|
536 | if(eSliceType==I_SLICE) |
---|
537 | { |
---|
538 | pcPic->setTLayer(0); |
---|
539 | } |
---|
540 | rpcSlice->setTLayer( pcPic->getTLayer() ); |
---|
541 | |
---|
542 | assert( m_apcPicYuvPred ); |
---|
543 | assert( m_apcPicYuvResi ); |
---|
544 | |
---|
545 | pcPic->setPicYuvPred( m_apcPicYuvPred ); |
---|
546 | pcPic->setPicYuvResi( m_apcPicYuvResi ); |
---|
547 | rpcSlice->setSliceMode ( m_pcCfg->getSliceMode() ); |
---|
548 | rpcSlice->setSliceArgument ( m_pcCfg->getSliceArgument() ); |
---|
549 | rpcSlice->setSliceSegmentMode ( m_pcCfg->getSliceSegmentMode() ); |
---|
550 | rpcSlice->setSliceSegmentArgument ( m_pcCfg->getSliceSegmentArgument() ); |
---|
551 | rpcSlice->setMaxNumMergeCand ( m_pcCfg->getMaxNumMergeCand() ); |
---|
552 | |
---|
553 | #if HIGHER_LAYER_IRAP_SKIP_FLAG |
---|
554 | if( m_pcCfg->getSkipPictureAtArcSwitch() && m_pcCfg->getAdaptiveResolutionChange() > 0 && rpcSlice->getLayerId() == 1 && rpcSlice->getPOC() == m_pcCfg->getAdaptiveResolutionChange() ) |
---|
555 | { |
---|
556 | rpcSlice->setMaxNumMergeCand ( 1 ); |
---|
557 | } |
---|
558 | #endif |
---|
559 | |
---|
560 | xStoreWPparam( pPPS->getUseWP(), pPPS->getWPBiPred() ); |
---|
561 | |
---|
562 | #if SVC_EXTENSION |
---|
563 | if( layerId > 0 ) |
---|
564 | { |
---|
565 | if( rpcSlice->getNumILRRefIdx() > 0 ) |
---|
566 | { |
---|
567 | rpcSlice->setActiveNumILRRefIdx( m_ppcTEncTop[rpcSlice->getVPS()->getLayerIdxInVps(layerId)]->getNumActiveRefLayers() ); |
---|
568 | for( Int i = 0; i < rpcSlice->getActiveNumILRRefIdx(); i++ ) |
---|
569 | { |
---|
570 | rpcSlice->setInterLayerPredLayerIdc( m_ppcTEncTop[rpcSlice->getVPS()->getLayerIdxInVps(layerId)]->getPredLayerIdx(i), i ); |
---|
571 | } |
---|
572 | rpcSlice->setInterLayerPredEnabledFlag(1); |
---|
573 | } |
---|
574 | rpcSlice->setMFMEnabledFlag(m_ppcTEncTop[rpcSlice->getVPS()->getLayerIdxInVps(layerId)]->getMFMEnabledFlag()); |
---|
575 | } |
---|
576 | |
---|
577 | #endif |
---|
578 | } |
---|
579 | |
---|
580 | Void TEncSlice::resetQP( TComPic* pic, Int sliceQP, Double lambda ) |
---|
581 | { |
---|
582 | TComSlice* slice = pic->getSlice(0); |
---|
583 | |
---|
584 | // store lambda |
---|
585 | slice->setSliceQp( sliceQP ); |
---|
586 | #if ADAPTIVE_QP_SELECTION |
---|
587 | slice->setSliceQpBase ( sliceQP ); |
---|
588 | #endif |
---|
589 | setUpLambda(slice, lambda, sliceQP); |
---|
590 | } |
---|
591 | |
---|
592 | // ==================================================================================================================== |
---|
593 | // Public member functions |
---|
594 | // ==================================================================================================================== |
---|
595 | |
---|
596 | Void TEncSlice::setSearchRange( TComSlice* pcSlice ) |
---|
597 | { |
---|
598 | Int iCurrPOC = pcSlice->getPOC(); |
---|
599 | Int iRefPOC; |
---|
600 | Int iGOPSize = m_pcCfg->getGOPSize(); |
---|
601 | Int iOffset = (iGOPSize >> 1); |
---|
602 | Int iMaxSR = m_pcCfg->getSearchRange(); |
---|
603 | Int iNumPredDir = pcSlice->isInterP() ? 1 : 2; |
---|
604 | |
---|
605 | for (Int iDir = 0; iDir <= iNumPredDir; iDir++) |
---|
606 | { |
---|
607 | //RefPicList e = (RefPicList)iDir; |
---|
608 | RefPicList e = ( iDir ? REF_PIC_LIST_1 : REF_PIC_LIST_0 ); |
---|
609 | for (Int iRefIdx = 0; iRefIdx < pcSlice->getNumRefIdx(e); iRefIdx++) |
---|
610 | { |
---|
611 | iRefPOC = pcSlice->getRefPic(e, iRefIdx)->getPOC(); |
---|
612 | Int iNewSR = Clip3(8, iMaxSR, (iMaxSR*ADAPT_SR_SCALE*abs(iCurrPOC - iRefPOC)+iOffset)/iGOPSize); |
---|
613 | m_pcPredSearch->setAdaptiveSearchRange(iDir, iRefIdx, iNewSR); |
---|
614 | } |
---|
615 | } |
---|
616 | } |
---|
617 | |
---|
618 | /** |
---|
619 | - multi-loop slice encoding for different slice QP |
---|
620 | . |
---|
621 | \param rpcPic picture class |
---|
622 | */ |
---|
623 | Void TEncSlice::precompressSlice( TComPic* pcPic ) |
---|
624 | { |
---|
625 | // if deltaQP RD is not used, simply return |
---|
626 | if ( m_pcCfg->getDeltaQpRD() == 0 ) |
---|
627 | { |
---|
628 | return; |
---|
629 | } |
---|
630 | |
---|
631 | if ( m_pcCfg->getUseRateCtrl() ) |
---|
632 | { |
---|
633 | printf( "\nMultiple QP optimization is not allowed when rate control is enabled." ); |
---|
634 | assert(0); |
---|
635 | } |
---|
636 | |
---|
637 | TComSlice* pcSlice = pcPic->getSlice(getSliceIdx()); |
---|
638 | Double dPicRdCostBest = MAX_DOUBLE; |
---|
639 | UInt uiQpIdxBest = 0; |
---|
640 | |
---|
641 | Double dFrameLambda; |
---|
642 | #if FULL_NBIT |
---|
643 | Int SHIFT_QP = 12 + 6 * (g_bitDepth[CHANNEL_TYPE_LUMA] - 8); |
---|
644 | #else |
---|
645 | Int SHIFT_QP = 12; |
---|
646 | #endif |
---|
647 | |
---|
648 | // set frame lambda |
---|
649 | if (m_pcCfg->getGOPSize() > 1) |
---|
650 | { |
---|
651 | dFrameLambda = 0.68 * pow (2, (m_piRdPicQp[0] - SHIFT_QP) / 3.0) * (pcSlice->isInterB()? 2 : 1); |
---|
652 | } |
---|
653 | else |
---|
654 | { |
---|
655 | dFrameLambda = 0.68 * pow (2, (m_piRdPicQp[0] - SHIFT_QP) / 3.0); |
---|
656 | } |
---|
657 | m_pcRdCost ->setFrameLambda(dFrameLambda); |
---|
658 | |
---|
659 | const UInt initialSliceQp=pcSlice->getSliceQp(); |
---|
660 | // for each QP candidate |
---|
661 | for ( UInt uiQpIdx = 0; uiQpIdx < 2 * m_pcCfg->getDeltaQpRD() + 1; uiQpIdx++ ) |
---|
662 | { |
---|
663 | pcSlice ->setSliceQp ( m_piRdPicQp [uiQpIdx] ); |
---|
664 | #if ADAPTIVE_QP_SELECTION |
---|
665 | pcSlice ->setSliceQpBase ( m_piRdPicQp [uiQpIdx] ); |
---|
666 | #endif |
---|
667 | setUpLambda(pcSlice, m_pdRdPicLambda[uiQpIdx], m_piRdPicQp [uiQpIdx]); |
---|
668 | |
---|
669 | // try compress |
---|
670 | compressSlice ( pcPic ); |
---|
671 | |
---|
672 | Double dPicRdCost; |
---|
673 | UInt64 uiPicDist = m_uiPicDist; |
---|
674 | // TODO: will this work if multiple slices are being used? There may not be any reconstruction data yet. |
---|
675 | // Will this also be ideal if a byte-restriction is placed on the slice? |
---|
676 | // - what if the last CTU was sometimes included, sometimes not, and that had all the distortion? |
---|
677 | m_pcGOPEncoder->preLoopFilterPicAll( pcPic, uiPicDist ); |
---|
678 | |
---|
679 | // compute RD cost and choose the best |
---|
680 | dPicRdCost = m_pcRdCost->calcRdCost64( m_uiPicTotalBits, uiPicDist, true, DF_SSE_FRAME); |
---|
681 | |
---|
682 | if ( dPicRdCost < dPicRdCostBest ) |
---|
683 | { |
---|
684 | uiQpIdxBest = uiQpIdx; |
---|
685 | dPicRdCostBest = dPicRdCost; |
---|
686 | } |
---|
687 | } |
---|
688 | |
---|
689 | if (pcSlice->getDependentSliceSegmentFlag() && initialSliceQp!=m_piRdPicQp[uiQpIdxBest] ) |
---|
690 | { |
---|
691 | // TODO: this won't work with dependent slices: they do not have their own QP. |
---|
692 | fprintf(stderr,"ERROR - attempt to change QP for a dependent slice-segment, having already coded the slice\n"); |
---|
693 | assert(pcSlice->getDependentSliceSegmentFlag()==false || initialSliceQp==m_piRdPicQp[uiQpIdxBest]); |
---|
694 | } |
---|
695 | // set best values |
---|
696 | pcSlice ->setSliceQp ( m_piRdPicQp [uiQpIdxBest] ); |
---|
697 | #if ADAPTIVE_QP_SELECTION |
---|
698 | pcSlice ->setSliceQpBase ( m_piRdPicQp [uiQpIdxBest] ); |
---|
699 | #endif |
---|
700 | setUpLambda(pcSlice, m_pdRdPicLambda[uiQpIdxBest], m_piRdPicQp [uiQpIdxBest]); |
---|
701 | } |
---|
702 | |
---|
703 | Void TEncSlice::calCostSliceI(TComPic* pcPic) |
---|
704 | { |
---|
705 | UInt ctuRsAddr; |
---|
706 | UInt startCtuTsAddr; |
---|
707 | UInt boundingCtuTsAddr; |
---|
708 | Int iSumHad, shift = g_bitDepth[CHANNEL_TYPE_LUMA]-8, offset = (shift>0)?(1<<(shift-1)):0;; |
---|
709 | Double iSumHadSlice = 0; |
---|
710 | |
---|
711 | pcPic->getSlice(getSliceIdx())->setSliceSegmentBits(0); |
---|
712 | TComSlice* pcSlice = pcPic->getSlice(getSliceIdx()); |
---|
713 | xDetermineStartAndBoundingCtuTsAddr ( startCtuTsAddr, boundingCtuTsAddr, pcPic, false ); |
---|
714 | |
---|
715 | UInt ctuTsAddr; |
---|
716 | ctuRsAddr = pcPic->getPicSym()->getCtuTsToRsAddrMap( startCtuTsAddr); |
---|
717 | for( ctuTsAddr = startCtuTsAddr; ctuTsAddr < boundingCtuTsAddr; ctuRsAddr = pcPic->getPicSym()->getCtuTsToRsAddrMap(++ctuTsAddr) ) |
---|
718 | { |
---|
719 | // initialize CU encoder |
---|
720 | TComDataCU* pCtu = pcPic->getCtu( ctuRsAddr ); |
---|
721 | pCtu->initCtu( pcPic, ctuRsAddr ); |
---|
722 | |
---|
723 | #if REPN_FORMAT_IN_VPS |
---|
724 | Int height = min( pcSlice->getSPS()->getMaxCUHeight(),pcSlice->getPicHeightInLumaSamples() - ctuRsAddr / pcPic->getFrameWidthInCtus() * pcSlice->getSPS()->getMaxCUHeight() ); |
---|
725 | Int width = min( pcSlice->getSPS()->getMaxCUWidth(),pcSlice->getPicWidthInLumaSamples() - ctuRsAddr % pcPic->getFrameWidthInCtus() * pcSlice->getSPS()->getMaxCUWidth() ); |
---|
726 | #else |
---|
727 | Int height = min( pcSlice->getSPS()->getMaxCUHeight(),pcSlice->getSPS()->getPicHeightInLumaSamples() - ctuRsAddr / pcPic->getFrameWidthInCtus() * pcSlice->getSPS()->getMaxCUHeight() ); |
---|
728 | Int width = min( pcSlice->getSPS()->getMaxCUWidth(),pcSlice->getSPS()->getPicWidthInLumaSamples() - ctuRsAddr % pcPic->getFrameWidthInCtus() * pcSlice->getSPS()->getMaxCUWidth() ); |
---|
729 | #endif |
---|
730 | |
---|
731 | iSumHad = m_pcCuEncoder->updateCtuDataISlice(pCtu, width, height); |
---|
732 | |
---|
733 | (m_pcRateCtrl->getRCPic()->getLCU(ctuRsAddr)).m_costIntra=(iSumHad+offset)>>shift; |
---|
734 | iSumHadSlice += (m_pcRateCtrl->getRCPic()->getLCU(ctuRsAddr)).m_costIntra; |
---|
735 | |
---|
736 | } |
---|
737 | m_pcRateCtrl->getRCPic()->setTotalIntraCost(iSumHadSlice); |
---|
738 | } |
---|
739 | |
---|
740 | /** \param rpcPic picture class |
---|
741 | */ |
---|
742 | Void TEncSlice::compressSlice( TComPic* pcPic ) |
---|
743 | { |
---|
744 | UInt startCtuTsAddr; |
---|
745 | UInt boundingCtuTsAddr; |
---|
746 | TComSlice* pcSlice = pcPic->getSlice(getSliceIdx()); |
---|
747 | pcSlice->setSliceSegmentBits(0); |
---|
748 | xDetermineStartAndBoundingCtuTsAddr ( startCtuTsAddr, boundingCtuTsAddr, pcPic, false ); |
---|
749 | |
---|
750 | // initialize cost values - these are used by precompressSlice (they should be parameters). |
---|
751 | m_uiPicTotalBits = 0; |
---|
752 | m_dPicRdCost = 0; // NOTE: This is a write-only variable! |
---|
753 | m_uiPicDist = 0; |
---|
754 | |
---|
755 | m_pcEntropyCoder->setEntropyCoder ( m_pppcRDSbacCoder[0][CI_CURR_BEST], pcSlice ); |
---|
756 | m_pcEntropyCoder->resetEntropy (); |
---|
757 | |
---|
758 | TEncBinCABAC* pRDSbacCoder = (TEncBinCABAC *) m_pppcRDSbacCoder[0][CI_CURR_BEST]->getEncBinIf(); |
---|
759 | pRDSbacCoder->setBinCountingEnableFlag( false ); |
---|
760 | pRDSbacCoder->setBinsCoded( 0 ); |
---|
761 | |
---|
762 | TComBitCounter tempBitCounter; |
---|
763 | const UInt frameWidthInCtus = pcPic->getPicSym()->getFrameWidthInCtus(); |
---|
764 | |
---|
765 | //------------------------------------------------------------------------------ |
---|
766 | // Weighted Prediction parameters estimation. |
---|
767 | //------------------------------------------------------------------------------ |
---|
768 | // calculate AC/DC values for current picture |
---|
769 | if( pcSlice->getPPS()->getUseWP() || pcSlice->getPPS()->getWPBiPred() ) |
---|
770 | { |
---|
771 | xCalcACDCParamSlice(pcSlice); |
---|
772 | } |
---|
773 | #if O0194_WEIGHTED_PREDICTION_CGS |
---|
774 | else if( m_ppcTEncTop[pcSlice->getLayerIdx()]->getInterLayerWeightedPredFlag() ) |
---|
775 | { |
---|
776 | // Calculate for the base layer to be used in EL as Inter layer reference |
---|
777 | estimateILWpParam( pcSlice ); |
---|
778 | } |
---|
779 | #endif |
---|
780 | |
---|
781 | const Bool bWp_explicit = (pcSlice->getSliceType()==P_SLICE && pcSlice->getPPS()->getUseWP()) || (pcSlice->getSliceType()==B_SLICE && pcSlice->getPPS()->getWPBiPred()); |
---|
782 | |
---|
783 | if ( bWp_explicit ) |
---|
784 | { |
---|
785 | //------------------------------------------------------------------------------ |
---|
786 | // Weighted Prediction implemented at Slice level. SliceMode=2 is not supported yet. |
---|
787 | //------------------------------------------------------------------------------ |
---|
788 | if ( pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES || pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES ) |
---|
789 | { |
---|
790 | printf("Weighted Prediction is not supported with slice mode determined by max number of bins.\n"); exit(0); |
---|
791 | } |
---|
792 | |
---|
793 | xEstimateWPParamSlice( pcSlice ); |
---|
794 | pcSlice->initWpScaling(); |
---|
795 | |
---|
796 | // check WP on/off |
---|
797 | xCheckWPEnable( pcSlice ); |
---|
798 | } |
---|
799 | |
---|
800 | #if ADAPTIVE_QP_SELECTION |
---|
801 | if( m_pcCfg->getUseAdaptQpSelect() && !(pcSlice->getDependentSliceSegmentFlag())) |
---|
802 | { |
---|
803 | // TODO: this won't work with dependent slices: they do not have their own QP. Check fix to mask clause execution with && !(pcSlice->getDependentSliceSegmentFlag()) |
---|
804 | m_pcTrQuant->clearSliceARLCnt(); |
---|
805 | if(pcSlice->getSliceType()!=I_SLICE) |
---|
806 | { |
---|
807 | Int qpBase = pcSlice->getSliceQpBase(); |
---|
808 | pcSlice->setSliceQp(qpBase + m_pcTrQuant->getQpDelta(qpBase)); |
---|
809 | } |
---|
810 | } |
---|
811 | #endif |
---|
812 | |
---|
813 | |
---|
814 | |
---|
815 | // Adjust initial state if this is the start of a dependent slice. |
---|
816 | { |
---|
817 | const UInt ctuRsAddr = pcPic->getPicSym()->getCtuTsToRsAddrMap( startCtuTsAddr); |
---|
818 | const UInt currentTileIdx = pcPic->getPicSym()->getTileIdxMap(ctuRsAddr); |
---|
819 | const TComTile *pCurrentTile = pcPic->getPicSym()->getTComTile(currentTileIdx); |
---|
820 | const UInt firstCtuRsAddrOfTile = pCurrentTile->getFirstCtuRsAddr(); |
---|
821 | if( pcSlice->getDependentSliceSegmentFlag() && ctuRsAddr != firstCtuRsAddrOfTile ) |
---|
822 | { |
---|
823 | // This will only occur if dependent slice-segments (m_entropyCodingSyncContextState=true) are being used. |
---|
824 | if( pCurrentTile->getTileWidthInCtus() >= 2 || !m_pcCfg->getWaveFrontsynchro() ) |
---|
825 | { |
---|
826 | m_pppcRDSbacCoder[0][CI_CURR_BEST]->loadContexts( &m_lastSliceSegmentEndContextState ); |
---|
827 | } |
---|
828 | } |
---|
829 | } |
---|
830 | |
---|
831 | // for every CTU in the slice segment (may terminate sooner if there is a byte limit on the slice-segment) |
---|
832 | |
---|
833 | for( UInt ctuTsAddr = startCtuTsAddr; ctuTsAddr < boundingCtuTsAddr; ++ctuTsAddr ) |
---|
834 | { |
---|
835 | const UInt ctuRsAddr = pcPic->getPicSym()->getCtuTsToRsAddrMap(ctuTsAddr); |
---|
836 | // initialize CTU encoder |
---|
837 | TComDataCU* pCtu = pcPic->getCtu( ctuRsAddr ); |
---|
838 | pCtu->initCtu( pcPic, ctuRsAddr ); |
---|
839 | |
---|
840 | // update CABAC state |
---|
841 | const UInt firstCtuRsAddrOfTile = pcPic->getPicSym()->getTComTile(pcPic->getPicSym()->getTileIdxMap(ctuRsAddr))->getFirstCtuRsAddr(); |
---|
842 | const UInt tileXPosInCtus = firstCtuRsAddrOfTile % frameWidthInCtus; |
---|
843 | const UInt ctuXPosInCtus = ctuRsAddr % frameWidthInCtus; |
---|
844 | |
---|
845 | if (ctuRsAddr == firstCtuRsAddrOfTile) |
---|
846 | { |
---|
847 | m_pppcRDSbacCoder[0][CI_CURR_BEST]->resetEntropy(); |
---|
848 | } |
---|
849 | else if ( ctuXPosInCtus == tileXPosInCtus && m_pcCfg->getWaveFrontsynchro()) |
---|
850 | { |
---|
851 | // reset and then update contexts to the state at the end of the top-right CTU (if within current slice and tile). |
---|
852 | m_pppcRDSbacCoder[0][CI_CURR_BEST]->resetEntropy(); |
---|
853 | // Sync if the Top-Right is available. |
---|
854 | TComDataCU *pCtuUp = pCtu->getCtuAbove(); |
---|
855 | if ( pCtuUp && ((ctuRsAddr%frameWidthInCtus+1) < frameWidthInCtus) ) |
---|
856 | { |
---|
857 | TComDataCU *pCtuTR = pcPic->getCtu( ctuRsAddr - frameWidthInCtus + 1 ); |
---|
858 | if ( pCtu->CUIsFromSameSliceAndTile(pCtuTR) ) |
---|
859 | { |
---|
860 | // Top-Right is available, we use it. |
---|
861 | m_pppcRDSbacCoder[0][CI_CURR_BEST]->loadContexts( &m_entropyCodingSyncContextState ); |
---|
862 | } |
---|
863 | } |
---|
864 | } |
---|
865 | |
---|
866 | // set go-on entropy coder (used for all trial encodings - the cu encoder and encoder search also have a copy of the same pointer) |
---|
867 | m_pcEntropyCoder->setEntropyCoder ( m_pcRDGoOnSbacCoder, pcSlice ); |
---|
868 | m_pcEntropyCoder->setBitstream( &tempBitCounter ); |
---|
869 | tempBitCounter.resetBits(); |
---|
870 | m_pcRDGoOnSbacCoder->load( m_pppcRDSbacCoder[0][CI_CURR_BEST] ); // this copy is not strictly necessary here, but indicates that the GoOnSbacCoder |
---|
871 | // is reset to a known state before every decision process. |
---|
872 | |
---|
873 | ((TEncBinCABAC*)m_pcRDGoOnSbacCoder->getEncBinIf())->setBinCountingEnableFlag(true); |
---|
874 | |
---|
875 | Double oldLambda = m_pcRdCost->getLambda(); |
---|
876 | if ( m_pcCfg->getUseRateCtrl() ) |
---|
877 | { |
---|
878 | Int estQP = pcSlice->getSliceQp(); |
---|
879 | Double estLambda = -1.0; |
---|
880 | Double bpp = -1.0; |
---|
881 | |
---|
882 | if ( ( pcPic->getSlice( 0 )->getSliceType() == I_SLICE && m_pcCfg->getForceIntraQP() ) || !m_pcCfg->getLCULevelRC() ) |
---|
883 | { |
---|
884 | estQP = pcSlice->getSliceQp(); |
---|
885 | } |
---|
886 | else |
---|
887 | { |
---|
888 | bpp = m_pcRateCtrl->getRCPic()->getLCUTargetBpp(pcSlice->getSliceType()); |
---|
889 | if ( pcPic->getSlice( 0 )->getSliceType() == I_SLICE) |
---|
890 | { |
---|
891 | estLambda = m_pcRateCtrl->getRCPic()->getLCUEstLambdaAndQP(bpp, pcSlice->getSliceQp(), &estQP); |
---|
892 | } |
---|
893 | else |
---|
894 | { |
---|
895 | estLambda = m_pcRateCtrl->getRCPic()->getLCUEstLambda( bpp ); |
---|
896 | estQP = m_pcRateCtrl->getRCPic()->getLCUEstQP ( estLambda, pcSlice->getSliceQp() ); |
---|
897 | } |
---|
898 | |
---|
899 | #if REPN_FORMAT_IN_VPS |
---|
900 | estQP = Clip3( -pcSlice->getQpBDOffsetY(), MAX_QP, estQP ); |
---|
901 | #else |
---|
902 | estQP = Clip3( -pcSlice->getSPS()->getQpBDOffset(CHANNEL_TYPE_LUMA), MAX_QP, estQP ); |
---|
903 | #endif |
---|
904 | |
---|
905 | m_pcRdCost->setLambda(estLambda); |
---|
906 | |
---|
907 | #if RDOQ_CHROMA_LAMBDA |
---|
908 | // set lambda for RDOQ |
---|
909 | const Double chromaLambda = estLambda / m_pcRdCost->getChromaWeight(); |
---|
910 | const Double lambdaArray[MAX_NUM_COMPONENT] = { estLambda, chromaLambda, chromaLambda }; |
---|
911 | m_pcTrQuant->setLambdas( lambdaArray ); |
---|
912 | #else |
---|
913 | m_pcTrQuant->setLambda( estLambda ); |
---|
914 | #endif |
---|
915 | } |
---|
916 | |
---|
917 | m_pcRateCtrl->setRCQP( estQP ); |
---|
918 | #if ADAPTIVE_QP_SELECTION |
---|
919 | pCtu->getSlice()->setSliceQpBase( estQP ); |
---|
920 | #endif |
---|
921 | } |
---|
922 | |
---|
923 | // run CTU trial encoder |
---|
924 | m_pcCuEncoder->compressCtu( pCtu ); |
---|
925 | |
---|
926 | |
---|
927 | // All CTU decisions have now been made. Restore entropy coder to an initial stage, ready to make a true encode, |
---|
928 | // which will result in the state of the contexts being correct. It will also count up the number of bits coded, |
---|
929 | // which is used if there is a limit of the number of bytes per slice-segment. |
---|
930 | |
---|
931 | m_pcEntropyCoder->setEntropyCoder ( m_pppcRDSbacCoder[0][CI_CURR_BEST], pcSlice ); |
---|
932 | m_pcEntropyCoder->setBitstream( &tempBitCounter ); |
---|
933 | pRDSbacCoder->setBinCountingEnableFlag( true ); |
---|
934 | m_pppcRDSbacCoder[0][CI_CURR_BEST]->resetBits(); |
---|
935 | pRDSbacCoder->setBinsCoded( 0 ); |
---|
936 | |
---|
937 | // encode CTU and calculate the true bit counters. |
---|
938 | m_pcCuEncoder->encodeCtu( pCtu ); |
---|
939 | |
---|
940 | |
---|
941 | pRDSbacCoder->setBinCountingEnableFlag( false ); |
---|
942 | |
---|
943 | const Int numberOfWrittenBits = m_pcEntropyCoder->getNumberOfWrittenBits(); |
---|
944 | |
---|
945 | // Calculate if this CTU puts us over slice bit size. |
---|
946 | // cannot terminate if current slice/slice-segment would be 0 Ctu in size, |
---|
947 | const UInt validEndOfSliceCtuTsAddr = ctuTsAddr + (ctuTsAddr == startCtuTsAddr ? 1 : 0); |
---|
948 | // Set slice end parameter |
---|
949 | if(pcSlice->getSliceMode()==FIXED_NUMBER_OF_BYTES && pcSlice->getSliceBits()+numberOfWrittenBits > (pcSlice->getSliceArgument()<<3)) |
---|
950 | { |
---|
951 | pcSlice->setSliceSegmentCurEndCtuTsAddr(validEndOfSliceCtuTsAddr); |
---|
952 | pcSlice->setSliceCurEndCtuTsAddr(validEndOfSliceCtuTsAddr); |
---|
953 | boundingCtuTsAddr=validEndOfSliceCtuTsAddr; |
---|
954 | } |
---|
955 | else if(pcSlice->getSliceSegmentMode()==FIXED_NUMBER_OF_BYTES && pcSlice->getSliceSegmentBits()+numberOfWrittenBits > (pcSlice->getSliceSegmentArgument()<<3)) |
---|
956 | { |
---|
957 | pcSlice->setSliceSegmentCurEndCtuTsAddr(validEndOfSliceCtuTsAddr); |
---|
958 | boundingCtuTsAddr=validEndOfSliceCtuTsAddr; |
---|
959 | } |
---|
960 | |
---|
961 | if (boundingCtuTsAddr <= ctuTsAddr) |
---|
962 | break; |
---|
963 | |
---|
964 | pcSlice->setSliceBits( (UInt)(pcSlice->getSliceBits() + numberOfWrittenBits) ); |
---|
965 | pcSlice->setSliceSegmentBits(pcSlice->getSliceSegmentBits()+numberOfWrittenBits); |
---|
966 | |
---|
967 | // Store probabilities of second CTU in line into buffer - used only if wavefront-parallel-processing is enabled. |
---|
968 | if ( ctuXPosInCtus == tileXPosInCtus+1 && m_pcCfg->getWaveFrontsynchro()) |
---|
969 | { |
---|
970 | m_entropyCodingSyncContextState.loadContexts(m_pppcRDSbacCoder[0][CI_CURR_BEST]); |
---|
971 | } |
---|
972 | |
---|
973 | |
---|
974 | if ( m_pcCfg->getUseRateCtrl() ) |
---|
975 | { |
---|
976 | Int actualQP = g_RCInvalidQPValue; |
---|
977 | Double actualLambda = m_pcRdCost->getLambda(); |
---|
978 | Int actualBits = pCtu->getTotalBits(); |
---|
979 | Int numberOfEffectivePixels = 0; |
---|
980 | for ( Int idx = 0; idx < pcPic->getNumPartitionsInCtu(); idx++ ) |
---|
981 | { |
---|
982 | if ( pCtu->getPredictionMode( idx ) != NUMBER_OF_PREDICTION_MODES && ( !pCtu->isSkipped( idx ) ) ) |
---|
983 | { |
---|
984 | numberOfEffectivePixels = numberOfEffectivePixels + 16; |
---|
985 | break; |
---|
986 | } |
---|
987 | } |
---|
988 | |
---|
989 | if ( numberOfEffectivePixels == 0 ) |
---|
990 | { |
---|
991 | actualQP = g_RCInvalidQPValue; |
---|
992 | } |
---|
993 | else |
---|
994 | { |
---|
995 | actualQP = pCtu->getQP( 0 ); |
---|
996 | } |
---|
997 | m_pcRdCost->setLambda(oldLambda); |
---|
998 | m_pcRateCtrl->getRCPic()->updateAfterCTU( m_pcRateCtrl->getRCPic()->getLCUCoded(), actualBits, actualQP, actualLambda, |
---|
999 | pCtu->getSlice()->getSliceType() == I_SLICE ? 0 : m_pcCfg->getLCULevelRC() ); |
---|
1000 | } |
---|
1001 | |
---|
1002 | m_uiPicTotalBits += pCtu->getTotalBits(); |
---|
1003 | m_dPicRdCost += pCtu->getTotalCost(); |
---|
1004 | m_uiPicDist += pCtu->getTotalDistortion(); |
---|
1005 | } |
---|
1006 | |
---|
1007 | // store context state at the end of this slice-segment, in case the next slice is a dependent slice and continues using the CABAC contexts. |
---|
1008 | if( pcSlice->getPPS()->getDependentSliceSegmentsEnabledFlag() ) |
---|
1009 | { |
---|
1010 | m_lastSliceSegmentEndContextState.loadContexts( m_pppcRDSbacCoder[0][CI_CURR_BEST] );//ctx end of dep.slice |
---|
1011 | } |
---|
1012 | xRestoreWPparam( pcSlice ); |
---|
1013 | |
---|
1014 | // stop use of temporary bit counter object. |
---|
1015 | m_pppcRDSbacCoder[0][CI_CURR_BEST]->setBitstream(NULL); |
---|
1016 | m_pcRDGoOnSbacCoder->setBitstream(NULL); // stop use of tempBitCounter. |
---|
1017 | } |
---|
1018 | |
---|
1019 | /** |
---|
1020 | \param rpcPic picture class |
---|
1021 | \retval rpcBitstream bitstream class |
---|
1022 | */ |
---|
1023 | Void TEncSlice::encodeSlice ( TComPic* pcPic, TComOutputBitstream* pcSubstreams, UInt &numBinsCoded ) |
---|
1024 | { |
---|
1025 | TComSlice* pcSlice = pcPic->getSlice(getSliceIdx()); |
---|
1026 | |
---|
1027 | const UInt startCtuTsAddr = pcSlice->getSliceSegmentCurStartCtuTsAddr(); |
---|
1028 | const UInt boundingCtuTsAddr = pcSlice->getSliceSegmentCurEndCtuTsAddr(); |
---|
1029 | |
---|
1030 | const UInt frameWidthInCtus = pcPic->getPicSym()->getFrameWidthInCtus(); |
---|
1031 | const Bool depSliceSegmentsEnabled = pcSlice->getPPS()->getDependentSliceSegmentsEnabledFlag(); |
---|
1032 | const Bool wavefrontsEnabled = pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag(); |
---|
1033 | |
---|
1034 | // initialise entropy coder for the slice |
---|
1035 | m_pcSbacCoder->init( (TEncBinIf*)m_pcBinCABAC ); |
---|
1036 | m_pcEntropyCoder->setEntropyCoder ( m_pcSbacCoder, pcSlice ); |
---|
1037 | m_pcEntropyCoder->resetEntropy (); |
---|
1038 | |
---|
1039 | numBinsCoded = 0; |
---|
1040 | m_pcBinCABAC->setBinCountingEnableFlag( true ); |
---|
1041 | m_pcBinCABAC->setBinsCoded(0); |
---|
1042 | |
---|
1043 | #if ENC_DEC_TRACE |
---|
1044 | g_bJustDoIt = g_bEncDecTraceEnable; |
---|
1045 | #endif |
---|
1046 | DTRACE_CABAC_VL( g_nSymbolCounter++ ); |
---|
1047 | DTRACE_CABAC_T( "\tPOC: " ); |
---|
1048 | DTRACE_CABAC_V( pcPic->getPOC() ); |
---|
1049 | DTRACE_CABAC_T( "\n" ); |
---|
1050 | #if ENC_DEC_TRACE |
---|
1051 | g_bJustDoIt = g_bEncDecTraceDisable; |
---|
1052 | #endif |
---|
1053 | |
---|
1054 | |
---|
1055 | if (depSliceSegmentsEnabled) |
---|
1056 | { |
---|
1057 | // modify initial contexts with previous slice segment if this is a dependent slice. |
---|
1058 | const UInt ctuRsAddr = pcPic->getPicSym()->getCtuTsToRsAddrMap( startCtuTsAddr ); |
---|
1059 | const UInt currentTileIdx=pcPic->getPicSym()->getTileIdxMap(ctuRsAddr); |
---|
1060 | const TComTile *pCurrentTile=pcPic->getPicSym()->getTComTile(currentTileIdx); |
---|
1061 | const UInt firstCtuRsAddrOfTile = pCurrentTile->getFirstCtuRsAddr(); |
---|
1062 | |
---|
1063 | if( pcSlice->getDependentSliceSegmentFlag() && ctuRsAddr != firstCtuRsAddrOfTile ) |
---|
1064 | { |
---|
1065 | if( pCurrentTile->getTileWidthInCtus() >= 2 || !wavefrontsEnabled ) |
---|
1066 | { |
---|
1067 | m_pcSbacCoder->loadContexts(&m_lastSliceSegmentEndContextState); |
---|
1068 | } |
---|
1069 | } |
---|
1070 | } |
---|
1071 | |
---|
1072 | // for every CTU in the slice segment... |
---|
1073 | |
---|
1074 | for( UInt ctuTsAddr = startCtuTsAddr; ctuTsAddr < boundingCtuTsAddr; ++ctuTsAddr ) |
---|
1075 | { |
---|
1076 | const UInt ctuRsAddr = pcPic->getPicSym()->getCtuTsToRsAddrMap(ctuTsAddr); |
---|
1077 | const TComTile ¤tTile = *(pcPic->getPicSym()->getTComTile(pcPic->getPicSym()->getTileIdxMap(ctuRsAddr))); |
---|
1078 | const UInt firstCtuRsAddrOfTile = currentTile.getFirstCtuRsAddr(); |
---|
1079 | const UInt tileXPosInCtus = firstCtuRsAddrOfTile % frameWidthInCtus; |
---|
1080 | const UInt tileYPosInCtus = firstCtuRsAddrOfTile / frameWidthInCtus; |
---|
1081 | const UInt ctuXPosInCtus = ctuRsAddr % frameWidthInCtus; |
---|
1082 | const UInt ctuYPosInCtus = ctuRsAddr / frameWidthInCtus; |
---|
1083 | const UInt uiSubStrm=pcPic->getSubstreamForCtuAddr(ctuRsAddr, true, pcSlice); |
---|
1084 | TComDataCU* pCtu = pcPic->getCtu( ctuRsAddr ); |
---|
1085 | |
---|
1086 | m_pcEntropyCoder->setBitstream( &pcSubstreams[uiSubStrm] ); |
---|
1087 | |
---|
1088 | // set up CABAC contexts' state for this CTU |
---|
1089 | if (ctuRsAddr == firstCtuRsAddrOfTile) |
---|
1090 | { |
---|
1091 | if (ctuTsAddr != startCtuTsAddr) // if it is the first CTU, then the entropy coder has already been reset |
---|
1092 | { |
---|
1093 | m_pcEntropyCoder->resetEntropy(); |
---|
1094 | } |
---|
1095 | } |
---|
1096 | else if (ctuXPosInCtus == tileXPosInCtus && wavefrontsEnabled) |
---|
1097 | { |
---|
1098 | // Synchronize cabac probabilities with upper-right CTU if it's available and at the start of a line. |
---|
1099 | if (ctuTsAddr != startCtuTsAddr) // if it is the first CTU, then the entropy coder has already been reset |
---|
1100 | { |
---|
1101 | m_pcEntropyCoder->resetEntropy(); |
---|
1102 | } |
---|
1103 | TComDataCU *pCtuUp = pCtu->getCtuAbove(); |
---|
1104 | if ( pCtuUp && ((ctuRsAddr%frameWidthInCtus+1) < frameWidthInCtus) ) |
---|
1105 | { |
---|
1106 | TComDataCU *pCtuTR = pcPic->getCtu( ctuRsAddr - frameWidthInCtus + 1 ); |
---|
1107 | if ( pCtu->CUIsFromSameSliceAndTile(pCtuTR) ) |
---|
1108 | { |
---|
1109 | // Top-right is available, so use it. |
---|
1110 | m_pcSbacCoder->loadContexts( &m_entropyCodingSyncContextState ); |
---|
1111 | } |
---|
1112 | } |
---|
1113 | } |
---|
1114 | |
---|
1115 | |
---|
1116 | if ( pcSlice->getSPS()->getUseSAO() ) |
---|
1117 | { |
---|
1118 | Bool bIsSAOSliceEnabled = false; |
---|
1119 | Bool sliceEnabled[MAX_NUM_COMPONENT]; |
---|
1120 | for(Int comp=0; comp < MAX_NUM_COMPONENT; comp++) |
---|
1121 | { |
---|
1122 | ComponentID compId=ComponentID(comp); |
---|
1123 | sliceEnabled[compId] = pcSlice->getSaoEnabledFlag(toChannelType(compId)) && (comp < pcPic->getNumberValidComponents()); |
---|
1124 | if (sliceEnabled[compId]) bIsSAOSliceEnabled=true; |
---|
1125 | } |
---|
1126 | if (bIsSAOSliceEnabled) |
---|
1127 | { |
---|
1128 | SAOBlkParam& saoblkParam = (pcPic->getPicSym()->getSAOBlkParam())[ctuRsAddr]; |
---|
1129 | |
---|
1130 | Bool leftMergeAvail = false; |
---|
1131 | Bool aboveMergeAvail= false; |
---|
1132 | //merge left condition |
---|
1133 | Int rx = (ctuRsAddr % frameWidthInCtus); |
---|
1134 | if(rx > 0) |
---|
1135 | { |
---|
1136 | leftMergeAvail = pcPic->getSAOMergeAvailability(ctuRsAddr, ctuRsAddr-1); |
---|
1137 | } |
---|
1138 | |
---|
1139 | //merge up condition |
---|
1140 | Int ry = (ctuRsAddr / frameWidthInCtus); |
---|
1141 | if(ry > 0) |
---|
1142 | { |
---|
1143 | aboveMergeAvail = pcPic->getSAOMergeAvailability(ctuRsAddr, ctuRsAddr-frameWidthInCtus); |
---|
1144 | } |
---|
1145 | |
---|
1146 | #if SVC_EXTENSION |
---|
1147 | m_pcEntropyCoder->encodeSAOBlkParam(saoblkParam, m_ppcTEncTop[pcSlice->getLayerIdx()]->getSAO()->getSaoMaxOffsetQVal(), sliceEnabled, leftMergeAvail, aboveMergeAvail); |
---|
1148 | #else |
---|
1149 | m_pcEntropyCoder->encodeSAOBlkParam(saoblkParam, sliceEnabled, leftMergeAvail, aboveMergeAvail); |
---|
1150 | #endif |
---|
1151 | } |
---|
1152 | } |
---|
1153 | |
---|
1154 | #if ENC_DEC_TRACE |
---|
1155 | g_bJustDoIt = g_bEncDecTraceEnable; |
---|
1156 | #endif |
---|
1157 | m_pcCuEncoder->encodeCtu( pCtu ); |
---|
1158 | #if ENC_DEC_TRACE |
---|
1159 | g_bJustDoIt = g_bEncDecTraceDisable; |
---|
1160 | #endif |
---|
1161 | |
---|
1162 | //Store probabilities of second CTU in line into buffer |
---|
1163 | if ( ctuXPosInCtus == tileXPosInCtus+1 && wavefrontsEnabled) |
---|
1164 | { |
---|
1165 | m_entropyCodingSyncContextState.loadContexts( m_pcSbacCoder ); |
---|
1166 | } |
---|
1167 | |
---|
1168 | // terminate the sub-stream, if required (end of slice-segment, end of tile, end of wavefront-CTU-row): |
---|
1169 | if (ctuTsAddr+1 == boundingCtuTsAddr || |
---|
1170 | ( ctuXPosInCtus + 1 == tileXPosInCtus + currentTile.getTileWidthInCtus() && |
---|
1171 | ( ctuYPosInCtus + 1 == tileYPosInCtus + currentTile.getTileHeightInCtus() || wavefrontsEnabled) |
---|
1172 | ) |
---|
1173 | ) |
---|
1174 | { |
---|
1175 | m_pcEntropyCoder->encodeTerminatingBit(1); |
---|
1176 | m_pcEntropyCoder->encodeSliceFinish(); |
---|
1177 | // Byte-alignment in slice_data() when new tile |
---|
1178 | pcSubstreams[uiSubStrm].writeByteAlignment(); |
---|
1179 | |
---|
1180 | // write sub-stream size |
---|
1181 | if (ctuTsAddr+1 != boundingCtuTsAddr) |
---|
1182 | { |
---|
1183 | pcSlice->addSubstreamSize( (pcSubstreams[uiSubStrm].getNumberOfWrittenBits() >> 3) + pcSubstreams[uiSubStrm].countStartCodeEmulations() ); |
---|
1184 | } |
---|
1185 | } |
---|
1186 | } // CTU-loop |
---|
1187 | |
---|
1188 | if( depSliceSegmentsEnabled ) |
---|
1189 | { |
---|
1190 | m_lastSliceSegmentEndContextState.loadContexts( m_pcSbacCoder );//ctx end of dep.slice |
---|
1191 | } |
---|
1192 | |
---|
1193 | #if ADAPTIVE_QP_SELECTION |
---|
1194 | if( m_pcCfg->getUseAdaptQpSelect() ) |
---|
1195 | { |
---|
1196 | m_pcTrQuant->storeSliceQpNext(pcSlice); |
---|
1197 | } |
---|
1198 | #endif |
---|
1199 | |
---|
1200 | if (pcSlice->getPPS()->getCabacInitPresentFlag()) |
---|
1201 | { |
---|
1202 | if (pcSlice->getPPS()->getDependentSliceSegmentsEnabledFlag()) |
---|
1203 | { |
---|
1204 | pcSlice->getPPS()->setEncCABACTableIdx( pcSlice->getSliceType() ); |
---|
1205 | } |
---|
1206 | else |
---|
1207 | { |
---|
1208 | m_pcEntropyCoder->determineCabacInitIdx(); |
---|
1209 | } |
---|
1210 | } |
---|
1211 | numBinsCoded = m_pcBinCABAC->getBinsCoded(); |
---|
1212 | } |
---|
1213 | |
---|
1214 | Void TEncSlice::calculateBoundingCtuTsAddrForSlice(UInt &startCtuTSAddrSlice, UInt &boundingCtuTSAddrSlice, Bool &haveReachedTileBoundary, |
---|
1215 | TComPic* pcPic, const Bool encodingSlice, const Int sliceMode, const Int sliceArgument, const UInt sliceCurEndCtuTSAddr) |
---|
1216 | { |
---|
1217 | TComSlice* pcSlice = pcPic->getSlice(getSliceIdx()); |
---|
1218 | const UInt numberOfCtusInFrame = pcPic->getNumberOfCtusInFrame(); |
---|
1219 | boundingCtuTSAddrSlice=0; |
---|
1220 | haveReachedTileBoundary=false; |
---|
1221 | |
---|
1222 | switch (sliceMode) |
---|
1223 | { |
---|
1224 | case FIXED_NUMBER_OF_CTU: |
---|
1225 | { |
---|
1226 | UInt ctuAddrIncrement = sliceArgument; |
---|
1227 | boundingCtuTSAddrSlice = ((startCtuTSAddrSlice + ctuAddrIncrement) < numberOfCtusInFrame) ? (startCtuTSAddrSlice + ctuAddrIncrement) : numberOfCtusInFrame; |
---|
1228 | } |
---|
1229 | break; |
---|
1230 | case FIXED_NUMBER_OF_BYTES: |
---|
1231 | if (encodingSlice) |
---|
1232 | boundingCtuTSAddrSlice = sliceCurEndCtuTSAddr; |
---|
1233 | else |
---|
1234 | boundingCtuTSAddrSlice = numberOfCtusInFrame; |
---|
1235 | break; |
---|
1236 | case FIXED_NUMBER_OF_TILES: |
---|
1237 | { |
---|
1238 | const UInt tileIdx = pcPic->getPicSym()->getTileIdxMap( pcPic->getPicSym()->getCtuTsToRsAddrMap(startCtuTSAddrSlice) ); |
---|
1239 | const UInt tileTotalCount = (pcPic->getPicSym()->getNumTileColumnsMinus1()+1) * (pcPic->getPicSym()->getNumTileRowsMinus1()+1); |
---|
1240 | UInt ctuAddrIncrement = 0; |
---|
1241 | |
---|
1242 | for(UInt tileIdxIncrement = 0; tileIdxIncrement < sliceArgument; tileIdxIncrement++) |
---|
1243 | { |
---|
1244 | if((tileIdx + tileIdxIncrement) < tileTotalCount) |
---|
1245 | { |
---|
1246 | UInt tileWidthInCtus = pcPic->getPicSym()->getTComTile(tileIdx + tileIdxIncrement)->getTileWidthInCtus(); |
---|
1247 | UInt tileHeightInCtus = pcPic->getPicSym()->getTComTile(tileIdx + tileIdxIncrement)->getTileHeightInCtus(); |
---|
1248 | ctuAddrIncrement += (tileWidthInCtus * tileHeightInCtus); |
---|
1249 | } |
---|
1250 | } |
---|
1251 | |
---|
1252 | boundingCtuTSAddrSlice = ((startCtuTSAddrSlice + ctuAddrIncrement) < numberOfCtusInFrame) ? (startCtuTSAddrSlice + ctuAddrIncrement) : numberOfCtusInFrame; |
---|
1253 | } |
---|
1254 | break; |
---|
1255 | default: |
---|
1256 | boundingCtuTSAddrSlice = numberOfCtusInFrame; |
---|
1257 | break; |
---|
1258 | } |
---|
1259 | |
---|
1260 | // Adjust for tiles and wavefronts. |
---|
1261 | if ((sliceMode == FIXED_NUMBER_OF_CTU || sliceMode == FIXED_NUMBER_OF_BYTES) && |
---|
1262 | (m_pcCfg->getNumRowsMinus1() > 0 || m_pcCfg->getNumColumnsMinus1() > 0)) |
---|
1263 | { |
---|
1264 | const UInt ctuRSAddr = pcPic->getPicSym()->getCtuTsToRsAddrMap(startCtuTSAddrSlice); |
---|
1265 | const UInt startTileIdx = pcPic->getPicSym()->getTileIdxMap(ctuRSAddr); |
---|
1266 | const Bool wavefrontsAreEnabled = m_pcCfg->getWaveFrontsynchro(); |
---|
1267 | |
---|
1268 | const TComTile *pStartingTile = pcPic->getPicSym()->getTComTile(startTileIdx); |
---|
1269 | const UInt tileStartTsAddr = pcPic->getPicSym()->getCtuRsToTsAddrMap(pStartingTile->getFirstCtuRsAddr()); |
---|
1270 | const UInt tileStartWidth = pStartingTile->getTileWidthInCtus(); |
---|
1271 | const UInt tileStartHeight = pStartingTile->getTileHeightInCtus(); |
---|
1272 | const UInt tileLastTsAddr_excl = tileStartTsAddr + tileStartWidth*tileStartHeight; |
---|
1273 | const UInt tileBoundingCtuTsAddrSlice = tileLastTsAddr_excl; |
---|
1274 | |
---|
1275 | const UInt ctuColumnOfStartingTile = ((startCtuTSAddrSlice-tileStartTsAddr)%tileStartWidth); |
---|
1276 | if (wavefrontsAreEnabled && ctuColumnOfStartingTile!=0) |
---|
1277 | { |
---|
1278 | // WPP: if a slice does not start at the beginning of a CTB row, it must end within the same CTB row |
---|
1279 | const UInt numberOfCTUsToEndOfRow = tileStartWidth - ctuColumnOfStartingTile; |
---|
1280 | const UInt wavefrontTileBoundingCtuAddrSlice = startCtuTSAddrSlice + numberOfCTUsToEndOfRow; |
---|
1281 | if (wavefrontTileBoundingCtuAddrSlice < boundingCtuTSAddrSlice) |
---|
1282 | { |
---|
1283 | boundingCtuTSAddrSlice = wavefrontTileBoundingCtuAddrSlice; |
---|
1284 | } |
---|
1285 | } |
---|
1286 | |
---|
1287 | if (tileBoundingCtuTsAddrSlice < boundingCtuTSAddrSlice) |
---|
1288 | { |
---|
1289 | boundingCtuTSAddrSlice = tileBoundingCtuTsAddrSlice; |
---|
1290 | haveReachedTileBoundary = true; |
---|
1291 | } |
---|
1292 | } |
---|
1293 | else if ((sliceMode == FIXED_NUMBER_OF_CTU || sliceMode == FIXED_NUMBER_OF_BYTES) && pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag() && ((startCtuTSAddrSlice % pcPic->getFrameWidthInCtus()) != 0)) |
---|
1294 | { |
---|
1295 | // Adjust for wavefronts (no tiles). |
---|
1296 | // WPP: if a slice does not start at the beginning of a CTB row, it must end within the same CTB row |
---|
1297 | boundingCtuTSAddrSlice = min(boundingCtuTSAddrSlice, startCtuTSAddrSlice - (startCtuTSAddrSlice % pcPic->getFrameWidthInCtus()) + (pcPic->getFrameWidthInCtus())); |
---|
1298 | } |
---|
1299 | } |
---|
1300 | |
---|
1301 | /** Determines the starting and bounding CTU address of current slice / dependent slice |
---|
1302 | * \param bEncodeSlice Identifies if the calling function is compressSlice() [false] or encodeSlice() [true] |
---|
1303 | * \returns Updates startCtuTsAddr, boundingCtuTsAddr with appropriate CTU address |
---|
1304 | */ |
---|
1305 | Void TEncSlice::xDetermineStartAndBoundingCtuTsAddr ( UInt& startCtuTsAddr, UInt& boundingCtuTsAddr, TComPic* pcPic, const Bool encodingSlice ) |
---|
1306 | { |
---|
1307 | TComSlice* pcSlice = pcPic->getSlice(getSliceIdx()); |
---|
1308 | |
---|
1309 | // Non-dependent slice |
---|
1310 | UInt startCtuTsAddrSlice = pcSlice->getSliceCurStartCtuTsAddr(); |
---|
1311 | Bool haveReachedTileBoundarySlice = false; |
---|
1312 | UInt boundingCtuTsAddrSlice; |
---|
1313 | calculateBoundingCtuTsAddrForSlice(startCtuTsAddrSlice, boundingCtuTsAddrSlice, haveReachedTileBoundarySlice, pcPic, |
---|
1314 | encodingSlice, m_pcCfg->getSliceMode(), m_pcCfg->getSliceArgument(), pcSlice->getSliceCurEndCtuTsAddr()); |
---|
1315 | pcSlice->setSliceCurEndCtuTsAddr( boundingCtuTsAddrSlice ); |
---|
1316 | pcSlice->setSliceCurStartCtuTsAddr( startCtuTsAddrSlice ); |
---|
1317 | |
---|
1318 | // Dependent slice |
---|
1319 | UInt startCtuTsAddrSliceSegment = pcSlice->getSliceSegmentCurStartCtuTsAddr(); |
---|
1320 | Bool haveReachedTileBoundarySliceSegment = false; |
---|
1321 | UInt boundingCtuTsAddrSliceSegment; |
---|
1322 | calculateBoundingCtuTsAddrForSlice(startCtuTsAddrSliceSegment, boundingCtuTsAddrSliceSegment, haveReachedTileBoundarySliceSegment, pcPic, |
---|
1323 | encodingSlice, m_pcCfg->getSliceSegmentMode(), m_pcCfg->getSliceSegmentArgument(), pcSlice->getSliceSegmentCurEndCtuTsAddr()); |
---|
1324 | if (boundingCtuTsAddrSliceSegment>boundingCtuTsAddrSlice) |
---|
1325 | { |
---|
1326 | boundingCtuTsAddrSliceSegment = boundingCtuTsAddrSlice; |
---|
1327 | } |
---|
1328 | pcSlice->setSliceSegmentCurEndCtuTsAddr( boundingCtuTsAddrSliceSegment ); |
---|
1329 | pcSlice->setSliceSegmentCurStartCtuTsAddr(startCtuTsAddrSliceSegment); |
---|
1330 | |
---|
1331 | // Make a joint decision based on reconstruction and dependent slice bounds |
---|
1332 | startCtuTsAddr = max(startCtuTsAddrSlice , startCtuTsAddrSliceSegment ); |
---|
1333 | boundingCtuTsAddr = boundingCtuTsAddrSliceSegment; |
---|
1334 | } |
---|
1335 | |
---|
1336 | Double TEncSlice::xGetQPValueAccordingToLambda ( Double lambda ) |
---|
1337 | { |
---|
1338 | return 4.2005*log(lambda) + 13.7122; |
---|
1339 | } |
---|
1340 | |
---|
1341 | #if SVC_EXTENSION |
---|
1342 | #if JCTVC_M0259_LAMBDAREFINEMENT |
---|
1343 | Double TEncSlice::xCalEnhLambdaFactor( Double deltaQP , Double beta ) |
---|
1344 | { |
---|
1345 | double tmp = beta * pow( 2.0 , deltaQP / 6 ); |
---|
1346 | double gamma = tmp / ( tmp + 1 ); |
---|
1347 | return( gamma ); |
---|
1348 | } |
---|
1349 | #endif |
---|
1350 | #if O0194_WEIGHTED_PREDICTION_CGS |
---|
1351 | Void TEncSlice::estimateILWpParam( TComSlice* pcSlice ) |
---|
1352 | { |
---|
1353 | xCalcACDCParamSlice(pcSlice); |
---|
1354 | WPACDCParam * temp_weightACDCParam; |
---|
1355 | |
---|
1356 | pcSlice->getWpAcDcParam(temp_weightACDCParam); |
---|
1357 | g_refWeightACDCParam = (void *) temp_weightACDCParam; |
---|
1358 | } |
---|
1359 | #endif |
---|
1360 | #endif //SVC_EXTENSION |
---|
1361 | //! \} |
---|