source: SHVCSoftware/branches/SHM-upgrade/source/Lib/TLibEncoder/TEncSlice.cpp @ 918

Last change on this file since 918 was 916, checked in by seregin, 10 years ago

initial porting

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