Index: /branches/SHM-dev/source/Lib/TLibCommon/TComSlice.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TComSlice.cpp	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TComSlice.cpp	(revision 1291)
@@ -85,7 +85,4 @@
 , m_pcPPS                         ( NULL )
 , m_pcPic                         ( NULL )
-#if ADAPTIVE_QP_SELECTION
-, m_pcTrQuant                     ( NULL )
-#endif
 , m_colFromL0Flag                 ( true )
 , m_noOutputPriorPicsFlag         ( false )
Index: /branches/SHM-dev/source/Lib/TLibCommon/TComSlice.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TComSlice.h	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TComSlice.h	(revision 1291)
@@ -2049,10 +2049,4 @@
   const TComPPS*              getPPS() const                                         { return m_pcPPS;                                               }
 
-#if ADAPTIVE_QP_SELECTION
-  Void                        setTrQuant( TComTrQuant* pcTrQuant )                   { m_pcTrQuant = pcTrQuant;                                      }
-  TComTrQuant*                getTrQuant()                                           { return m_pcTrQuant;                                           }
-  const TComTrQuant*          getTrQuant() const                                     { return m_pcTrQuant;                                           }
-#endif
-
   Void                        setPPSId( Int PPSId )                                  { m_iPPSId = PPSId;                                             }
   Int                         getPPSId() const                                       { return m_iPPSId;                                              }
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 1291)
@@ -82,5 +82,5 @@
 // ====================================================================================================================
 
-Void TEncCavlc::resetEntropy()
+Void TEncCavlc::resetEntropy(const TComSlice *pSlice)
 {
 }
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.h	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.h	(revision 1291)
@@ -73,6 +73,4 @@
 
 protected:
-  TComSlice*    m_pcSlice;
-
   Void codeShortTermRefPicSet              ( const TComReferencePictureSet* pcRPS, Bool calledFromSliceHeader, Int idx );
   Bool findMatchingLTRP ( TComSlice* pcSlice, UInt *ltrpsIndex, Int ltrpPOC, Bool usedFlag );
@@ -80,9 +78,8 @@
 public:
 
-  Void  resetEntropy          ();
-  SliceType determineCabacInitIdx  () { assert(0); return I_SLICE; };
+  Void  resetEntropy          (const TComSlice *pSlice);
+  SliceType determineCabacInitIdx  (const TComSlice *pSlice) { assert(0); return I_SLICE; };
 
   Void  setBitstream          ( TComBitIf* p )  { m_pcBitIf = p;  }
-  Void  setSlice              ( TComSlice* p )  { m_pcSlice = p;  }
   Void  resetBits             ()                { m_pcBitIf->resetBits(); }
   UInt  getNumberOfWrittenBits()                { return  m_pcBitIf->getNumberOfWrittenBits();  }
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncEntropy.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncEntropy.cpp	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncEntropy.cpp	(revision 1291)
@@ -49,8 +49,7 @@
 //! \{
 
-Void TEncEntropy::setEntropyCoder ( TEncEntropyIf* e, TComSlice* pcSlice )
+Void TEncEntropy::setEntropyCoder ( TEncEntropyIf* e )
 {
   m_pcEntropyCoderIf = e;
-  m_pcEntropyCoderIf->setSlice ( pcSlice );
 }
 
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncEntropy.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncEntropy.h	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncEntropy.h	(revision 1291)
@@ -63,8 +63,7 @@
 {
 public:
-  virtual Void  resetEntropy          ()                = 0;
-  virtual SliceType determineCabacInitIdx ()                = 0;
+  virtual Void  resetEntropy          (const TComSlice *pSlice)                = 0;
+  virtual SliceType determineCabacInitIdx (const TComSlice *pSlice)                = 0;
   virtual Void  setBitstream          ( TComBitIf* p )  = 0;
-  virtual Void  setSlice              ( TComSlice* p )  = 0;
   virtual Void  resetBits             ()                = 0;
   virtual UInt  getNumberOfWrittenBits()                = 0;
@@ -133,10 +132,10 @@
 {
 public:
-  Void    setEntropyCoder           ( TEncEntropyIf* e, TComSlice* pcSlice );
+  Void    setEntropyCoder           ( TEncEntropyIf* e );
   Void    setBitstream              ( TComBitIf* p )          { m_pcEntropyCoderIf->setBitstream(p);  }
   Void    resetBits                 ()                        { m_pcEntropyCoderIf->resetBits();      }
   UInt    getNumberOfWrittenBits    ()                        { return m_pcEntropyCoderIf->getNumberOfWrittenBits(); }
-  Void    resetEntropy              ()                        { m_pcEntropyCoderIf->resetEntropy();  }
-  SliceType determineCabacInitIdx   ()                        { return m_pcEntropyCoderIf->determineCabacInitIdx(); }
+  Void    resetEntropy              (const TComSlice *pSlice) { m_pcEntropyCoderIf->resetEntropy(pSlice);  }
+  SliceType determineCabacInitIdx   (const TComSlice *pSlice) { return m_pcEntropyCoderIf->determineCabacInitIdx(pSlice); }
 
   Void    encodeSliceHeader         ( TComSlice* pcSlice );
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 1291)
@@ -1242,4 +1242,241 @@
 }
 
+static Void
+cabac_zero_word_padding(TComSlice *const pcSlice, TComPic *const pcPic, const std::size_t binCountsInNalUnits, const std::size_t numBytesInVclNalUnits, std::ostringstream &nalUnitData, const Bool cabacZeroWordPaddingEnabled)
+{
+#if !SVC_EXTENSION
+  const TComSPS &sps=*(pcSlice->getSPS());
+#endif
+  const Int log2subWidthCxsubHeightC = (pcPic->getComponentScaleX(COMPONENT_Cb)+pcPic->getComponentScaleY(COMPONENT_Cb));
+  const Int minCuWidth  = pcPic->getMinCUWidth();
+  const Int minCuHeight = pcPic->getMinCUHeight();
+#if SVC_EXTENSION
+  const Int paddedWidth = ((pcSlice->getPicWidthInLumaSamples()  + minCuWidth  - 1) / minCuWidth) * minCuWidth;
+  const Int paddedHeight= ((pcSlice->getPicHeightInLumaSamples() + minCuHeight - 1) / minCuHeight) * minCuHeight;
+  const Int rawBits = paddedWidth * paddedHeight *
+                         (pcSlice->getBitDepth(CHANNEL_TYPE_LUMA) + 2*(pcSlice->getBitDepth(CHANNEL_TYPE_CHROMA)>>log2subWidthCxsubHeightC));
+#else
+  const Int paddedWidth = ((sps.getPicWidthInLumaSamples()  + minCuWidth  - 1) / minCuWidth) * minCuWidth;
+  const Int paddedHeight= ((sps.getPicHeightInLumaSamples() + minCuHeight - 1) / minCuHeight) * minCuHeight;
+  const Int rawBits = paddedWidth * paddedHeight *
+                         (sps.getBitDepth(CHANNEL_TYPE_LUMA) + 2*(sps.getBitDepth(CHANNEL_TYPE_CHROMA)>>log2subWidthCxsubHeightC));
+#endif
+  const std::size_t threshold = (32/3)*numBytesInVclNalUnits + (rawBits/32);
+  if (binCountsInNalUnits >= threshold)
+  {
+    // need to add additional cabac zero words (each one accounts for 3 bytes (=00 00 03)) to increase numBytesInVclNalUnits
+    const std::size_t targetNumBytesInVclNalUnits = ((binCountsInNalUnits - (rawBits/32))*3+31)/32;
+
+    if (targetNumBytesInVclNalUnits>numBytesInVclNalUnits) // It should be!
+    {
+      const std::size_t numberOfAdditionalBytesNeeded=targetNumBytesInVclNalUnits - numBytesInVclNalUnits;
+      const std::size_t numberOfAdditionalCabacZeroWords=(numberOfAdditionalBytesNeeded+2)/3;
+      const std::size_t numberOfAdditionalCabacZeroBytes=numberOfAdditionalCabacZeroWords*3;
+      if (cabacZeroWordPaddingEnabled)
+      {
+        std::vector<Char> zeroBytesPadding(numberOfAdditionalCabacZeroBytes, Char(0));
+        for(std::size_t i=0; i<numberOfAdditionalCabacZeroWords; i++)
+        {
+          zeroBytesPadding[i*3+2]=3;  // 00 00 03
+        }
+        nalUnitData.write(&(zeroBytesPadding[0]), numberOfAdditionalCabacZeroBytes);
+        printf("Adding %d bytes of padding\n", UInt(numberOfAdditionalCabacZeroWords*3));
+      }
+      else
+      {
+        printf("Standard would normally require adding %d bytes of padding\n", UInt(numberOfAdditionalCabacZeroWords*3));
+      }
+    }
+  }
+}
+
+#if EFFICIENT_FIELD_IRAP
+class EfficientFieldIRAPMapping
+{
+  private:
+    Int  IRAPGOPid;
+    Bool IRAPtoReorder;
+    Bool swapIRAPForward;
+
+  public:
+    EfficientFieldIRAPMapping() :
+      IRAPGOPid(-1),
+      IRAPtoReorder(false),
+      swapIRAPForward(false)
+    { }
+
+    Void initialize(const Bool isField, const Int picIdInGOP, const Int gopSize, const Int POCLast, const Int numPicRcvd, const Int lastIDR, TEncGOP *pEncGop, TEncCfg *pCfg);
+
+    Int adjustGOPid(const Int gopID);
+    Int restoreGOPid(const Int gopID);
+    Int GetIRAPGOPid() const { return IRAPGOPid; }
+};
+
+#if SVC_EXTENSION
+Void EfficientFieldIRAPMapping::initialize(const Bool isField, const Int picIdInGOP, const Int gopSize, const Int POCLast, const Int numPicRcvd, const Int lastIDR, TEncGOP *pEncGop, TEncCfg *pCfg )
+#else
+Void EfficientFieldIRAPMapping::initialize(const Bool isField, const Int gopSize, const Int POCLast, const Int numPicRcvd, const Int lastIDR, TEncGOP *pEncGop, TEncCfg *pCfg )
+#endif
+{
+  if(isField)
+  {
+    Int pocCurr;
+#if SVC_EXTENSION
+    for ( Int iGOPid=picIdInGOP; iGOPid < picIdInGOP+1; iGOPid++ )
+#else
+    for ( Int iGOPid=0; iGOPid < gopSize; iGOPid++ )
+#endif    
+    {
+      // determine actual POC
+      if(POCLast == 0) //case first frame or first top field
+      {
+        pocCurr=0;
+      }
+      else if(POCLast == 1 && isField) //case first bottom field, just like the first frame, the poc computation is not right anymore, we set the right value
+      {
+        pocCurr = 1;
+      }
+      else
+      {
+        pocCurr = POCLast - numPicRcvd + pCfg->getGOPEntry(iGOPid).m_POC - isField;
+      }
+
+      // check if POC corresponds to IRAP
+      NalUnitType tmpUnitType = pEncGop->getNalUnitType(pocCurr, lastIDR, isField);
+      if(tmpUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP && tmpUnitType <= NAL_UNIT_CODED_SLICE_CRA) // if picture is an IRAP
+      {
+        if(pocCurr%2 == 0 && iGOPid < gopSize-1 && pCfg->getGOPEntry(iGOPid).m_POC == pCfg->getGOPEntry(iGOPid+1).m_POC-1)
+        { // if top field and following picture in enc order is associated bottom field
+          IRAPGOPid = iGOPid;
+          IRAPtoReorder = true;
+          swapIRAPForward = true; 
+          break;
+        }
+        if(pocCurr%2 != 0 && iGOPid > 0 && pCfg->getGOPEntry(iGOPid).m_POC == pCfg->getGOPEntry(iGOPid-1).m_POC+1)
+        {
+          // if picture is an IRAP remember to process it first
+          IRAPGOPid = iGOPid;
+          IRAPtoReorder = true;
+          swapIRAPForward = false; 
+          break;
+        }
+      }
+    }
+  }
+}
+
+Int EfficientFieldIRAPMapping::adjustGOPid(const Int GOPid)
+{
+  if(IRAPtoReorder)
+  {
+    if(swapIRAPForward)
+    {
+      if(GOPid == IRAPGOPid)
+      {
+        return IRAPGOPid +1;
+      }
+      else if(GOPid == IRAPGOPid +1)
+      {
+        return IRAPGOPid;
+      }
+    }
+    else
+    {
+      if(GOPid == IRAPGOPid -1)
+      {
+        return IRAPGOPid;
+      }
+      else if(GOPid == IRAPGOPid)
+      {
+        return IRAPGOPid -1;
+      }
+    }
+  }
+  return GOPid;
+}
+
+Int EfficientFieldIRAPMapping::restoreGOPid(const Int GOPid)
+{
+  if(IRAPtoReorder)
+  {
+    if(swapIRAPForward)
+    {
+      if(GOPid == IRAPGOPid)
+      {
+        IRAPtoReorder = false;
+        return IRAPGOPid +1;
+      }
+      else if(GOPid == IRAPGOPid +1)
+      {
+        return GOPid -1;
+      }
+    }
+    else
+    {
+      if(GOPid == IRAPGOPid)
+      {
+        return IRAPGOPid -1;
+      }
+      else if(GOPid == IRAPGOPid -1)
+      {
+        IRAPtoReorder = false;
+        return IRAPGOPid;
+      }
+    }
+  }
+  return GOPid;
+}
+
+#endif
+
+static UInt calculateCollocatedFromL1Flag(TEncCfg *pCfg, const Int GOPid, const Int gopSize)
+{
+  Int iCloseLeft=1, iCloseRight=-1;
+  for(Int i = 0; i<pCfg->getGOPEntry(GOPid).m_numRefPics; i++)
+  {
+    Int iRef = pCfg->getGOPEntry(GOPid).m_referencePics[i];
+    if(iRef>0&&(iRef<iCloseRight||iCloseRight==-1))
+    {
+      iCloseRight=iRef;
+    }
+    else if(iRef<0&&(iRef>iCloseLeft||iCloseLeft==1))
+    {
+      iCloseLeft=iRef;
+    }
+  }
+  if(iCloseRight>-1)
+  {
+    iCloseRight=iCloseRight+pCfg->getGOPEntry(GOPid).m_POC-1;
+  }
+  if(iCloseLeft<1)
+  {
+    iCloseLeft=iCloseLeft+pCfg->getGOPEntry(GOPid).m_POC-1;
+    while(iCloseLeft<0)
+    {
+      iCloseLeft+=gopSize;
+    }
+  }
+  Int iLeftQP=0, iRightQP=0;
+  for(Int i=0; i<gopSize; i++)
+  {
+    if(pCfg->getGOPEntry(i).m_POC==(iCloseLeft%gopSize)+1)
+    {
+      iLeftQP= pCfg->getGOPEntry(i).m_QPOffset;
+    }
+    if (pCfg->getGOPEntry(i).m_POC==(iCloseRight%gopSize)+1)
+    {
+      iRightQP=pCfg->getGOPEntry(i).m_QPOffset;
+    }
+  }
+  if(iCloseRight>-1&&iRightQP<iLeftQP)
+  {
+    return 0;
+  }
+  else
+  {
+    return 1;
+  }
+}
+
 // ====================================================================================================================
 // Public member functions
@@ -1247,11 +1484,10 @@
 #if SVC_EXTENSION
 Void TEncGOP::compressGOP( Int iPicIdInGOP, Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic,
+#else
+Void TEncGOP::compressGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic,
+#endif
                            TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsInGOP,
                            Bool isField, Bool isTff, const InputColourSpaceConversion snr_conversion, const Bool printFrameMSE )
-#else
-Void TEncGOP::compressGOP( Int iPOCLast, Int iNumPicRcvd, TComList<TComPic*>& rcListPic,
-                           TComList<TComPicYuv*>& rcListPicYuvRecOut, std::list<AccessUnit>& accessUnitsInGOP,
-                           Bool isField, Bool isTff, const InputColourSpaceConversion snr_conversion, const Bool printFrameMSE )
-#endif
+
 {
   // TODO: Split this function up.
@@ -1275,53 +1511,12 @@
 
 #if EFFICIENT_FIELD_IRAP
-  Int IRAPGOPid = -1;
-  Bool IRAPtoReorder = false;
-  Bool swapIRAPForward = false;
-  if(isField)
-  {
-    Int pocCurr;
+  EfficientFieldIRAPMapping effFieldIRAPMap;
 #if SVC_EXTENSION
-    for ( Int iGOPid=iPicIdInGOP; iGOPid < iPicIdInGOP+1; iGOPid++ )
-#else
-    for ( Int iGOPid=0; iGOPid < m_iGopSize; iGOPid++ )
-#endif    
-    {
-      // determine actual POC
-      if(iPOCLast == 0) //case first frame or first top field
-      {
-        pocCurr=0;
-      }
-      else if(iPOCLast == 1 && isField) //case first bottom field, just like the first frame, the poc computation is not right anymore, we set the right value
-      {
-        pocCurr = 1;
-      }
-      else
-      {
-        pocCurr = iPOCLast - iNumPicRcvd + m_pcCfg->getGOPEntry(iGOPid).m_POC - isField;
-      }
-
-      // check if POC corresponds to IRAP
-      NalUnitType tmpUnitType = getNalUnitType(pocCurr, m_iLastIDR, isField);
-      if(tmpUnitType >= NAL_UNIT_CODED_SLICE_BLA_W_LP && tmpUnitType <= NAL_UNIT_CODED_SLICE_CRA) // if picture is an IRAP
-      {
-        if(pocCurr%2 == 0 && iGOPid < m_iGopSize-1 && m_pcCfg->getGOPEntry(iGOPid).m_POC == m_pcCfg->getGOPEntry(iGOPid+1).m_POC-1)
-        { // if top field and following picture in enc order is associated bottom field
-          IRAPGOPid = iGOPid;
-          IRAPtoReorder = true;
-          swapIRAPForward = true; 
-          break;
-        }
-        if(pocCurr%2 != 0 && iGOPid > 0 && m_pcCfg->getGOPEntry(iGOPid).m_POC == m_pcCfg->getGOPEntry(iGOPid-1).m_POC+1)
-        {
-          // if picture is an IRAP remember to process it first
-          IRAPGOPid = iGOPid;
-          IRAPtoReorder = true;
-          swapIRAPForward = false; 
-          break;
-        }
-      }
-    }
-  }
-#endif
+  effFieldIRAPMap.initialize(isField, iPicIdInGOP, m_iGopSize, iPOCLast, iNumPicRcvd, m_iLastIDR, this, m_pcCfg);
+#else
+  effFieldIRAPMap.initialize(isField, m_iGopSize, iPOCLast, iNumPicRcvd, m_iLastIDR, this, m_pcCfg);
+#endif
+#endif
+
   // reset flag indicating whether pictures have been encoded
   for ( Int iGOPid=0; iGOPid < m_iGopSize; iGOPid++ )
@@ -1336,77 +1531,11 @@
   {
 #if EFFICIENT_FIELD_IRAP
-    if(IRAPtoReorder)
-    {
-      if(swapIRAPForward)
-      {
-        if(iGOPid == IRAPGOPid)
-        {
-          iGOPid = IRAPGOPid +1;
-        }
-        else if(iGOPid == IRAPGOPid +1)
-        {
-          iGOPid = IRAPGOPid;
-        }
-      }
-      else
-      {
-        if(iGOPid == IRAPGOPid -1)
-        {
-          iGOPid = IRAPGOPid;
-        }
-        else if(iGOPid == IRAPGOPid)
-        {
-          iGOPid = IRAPGOPid -1;
-        }
-      }
-    }
-#endif
-
-    UInt uiColDir = 1;
+    iGOPid=effFieldIRAPMap.adjustGOPid(iGOPid);
+#endif
+
     //-- For time output for each slice
     clock_t iBeforeTime = clock();
 
-    //select uiColDir
-    Int iCloseLeft=1, iCloseRight=-1;
-    for(Int i = 0; i<m_pcCfg->getGOPEntry(iGOPid).m_numRefPics; i++)
-    {
-      Int iRef = m_pcCfg->getGOPEntry(iGOPid).m_referencePics[i];
-      if(iRef>0&&(iRef<iCloseRight||iCloseRight==-1))
-      {
-        iCloseRight=iRef;
-      }
-      else if(iRef<0&&(iRef>iCloseLeft||iCloseLeft==1))
-      {
-        iCloseLeft=iRef;
-      }
-    }
-    if(iCloseRight>-1)
-    {
-      iCloseRight=iCloseRight+m_pcCfg->getGOPEntry(iGOPid).m_POC-1;
-    }
-    if(iCloseLeft<1)
-    {
-      iCloseLeft=iCloseLeft+m_pcCfg->getGOPEntry(iGOPid).m_POC-1;
-      while(iCloseLeft<0)
-      {
-        iCloseLeft+=m_iGopSize;
-      }
-    }
-    Int iLeftQP=0, iRightQP=0;
-    for(Int i=0; i<m_iGopSize; i++)
-    {
-      if(m_pcCfg->getGOPEntry(i).m_POC==(iCloseLeft%m_iGopSize)+1)
-      {
-        iLeftQP= m_pcCfg->getGOPEntry(i).m_QPOffset;
-      }
-      if (m_pcCfg->getGOPEntry(i).m_POC==(iCloseRight%m_iGopSize)+1)
-      {
-        iRightQP=m_pcCfg->getGOPEntry(i).m_QPOffset;
-      }
-    }
-    if(iCloseRight>-1&&iRightQP<iLeftQP)
-    {
-      uiColDir=0;
-    }
+    UInt uiColDir = calculateCollocatedFromL1Flag(m_pcCfg, iGOPid, m_iGopSize);
 
     /////////////////////////////////////////////////////////////////////////////////////////////////// Initial to start encoding
@@ -1433,31 +1562,5 @@
     {
 #if EFFICIENT_FIELD_IRAP
-      if(IRAPtoReorder)
-      {
-        if(swapIRAPForward)
-        {
-          if(iGOPid == IRAPGOPid)
-          {
-            iGOPid = IRAPGOPid +1;
-            IRAPtoReorder = false;
-          }
-          else if(iGOPid == IRAPGOPid +1)
-          {
-            iGOPid --;
-          }
-        }
-        else
-        {
-          if(iGOPid == IRAPGOPid)
-          {
-            iGOPid = IRAPGOPid -1;
-          }
-          else if(iGOPid == IRAPGOPid -1)
-          {
-            iGOPid = IRAPGOPid;
-            IRAPtoReorder = false;
-          }
-        }
-      }
+      iGOPid=effFieldIRAPMap.restoreGOPid(iGOPid);
 #endif
       continue;
@@ -2051,11 +2154,5 @@
         }
     }
-#endif //SVC_EXTENSION
-
-#if ADAPTIVE_QP_SELECTION
-    pcSlice->setTrQuant( m_pcEncTop->getTrQuant() );
-#endif      
-
-#if SVC_EXTENSION
+
     if( pcSlice->getSliceType() == B_SLICE )
     {
@@ -2474,5 +2571,5 @@
     /////////////////////////////////////////////////////////////////////////////////////////////////// File writing
     // Set entropy coder
-    m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice );
+    m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder );
 
     if ( m_bSeqFirst )
@@ -2557,6 +2654,6 @@
       }
 
-      m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice );
-      m_pcEntropyCoder->resetEntropy      ();
+      m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder );
+      m_pcEntropyCoder->resetEntropy      ( pcSlice );
       /* start slice NALunit */
 #if SVC_EXTENSION
@@ -2619,5 +2716,5 @@
         // The final bitstream is either nalu.m_Bitstream or pcBitstreamRedirect;
         // Complete the slice header info.
-        m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder, pcSlice );
+        m_pcEntropyCoder->setEntropyCoder   ( m_pcCavlcCoder );
         m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
 #if SVC_EXTENSION
@@ -2675,50 +2772,5 @@
 
     // cabac_zero_words processing
-    {
-#if !SVC_EXTENSION
-      const TComSPS &sps=*(pcSlice->getSPS());
-#endif
-      const Int log2subWidthCxsubHeightC = (pcPic->getComponentScaleX(COMPONENT_Cb)+pcPic->getComponentScaleY(COMPONENT_Cb));
-      const Int minCuWidth  = pcPic->getMinCUWidth();
-      const Int minCuHeight = pcPic->getMinCUHeight();
-#if SVC_EXTENSION
-      const Int paddedWidth = ((pcSlice->getPicWidthInLumaSamples()  + minCuWidth  - 1) / minCuWidth) * minCuWidth;
-      const Int paddedHeight= ((pcSlice->getPicHeightInLumaSamples() + minCuHeight - 1) / minCuHeight) * minCuHeight;
-      const Int rawBits = paddedWidth * paddedHeight *
-                             (pcSlice->getBitDepth(CHANNEL_TYPE_LUMA) + 2*(pcSlice->getBitDepth(CHANNEL_TYPE_CHROMA)>>log2subWidthCxsubHeightC));
-#else
-      const Int paddedWidth = ((pcSlice->getSPS()->getPicWidthInLumaSamples()  + minCuWidth  - 1) / minCuWidth) * minCuWidth;
-      const Int paddedHeight= ((pcSlice->getSPS()->getPicHeightInLumaSamples() + minCuHeight - 1) / minCuHeight) * minCuHeight;
-      const Int rawBits = paddedWidth * paddedHeight *
-                             (sps.getBitDepth(CHANNEL_TYPE_LUMA) + 2*(sps.getBitDepth(CHANNEL_TYPE_CHROMA)>>log2subWidthCxsubHeightC));
-#endif
-      const std::size_t threshold = (32/3)*numBytesInVclNalUnits + (rawBits/32);
-      if (binCountsInNalUnits >= threshold)
-      {
-        // need to add additional cabac zero words (each one accounts for 3 bytes (=00 00 03)) to increase numBytesInVclNalUnits
-        const std::size_t targetNumBytesInVclNalUnits = ((binCountsInNalUnits - (rawBits/32))*3+31)/32;
-
-        if (targetNumBytesInVclNalUnits>numBytesInVclNalUnits) // It should be!
-        {
-          const std::size_t numberOfAdditionalBytesNeeded=targetNumBytesInVclNalUnits - numBytesInVclNalUnits;
-          const std::size_t numberOfAdditionalCabacZeroWords=(numberOfAdditionalBytesNeeded+2)/3;
-          const std::size_t numberOfAdditionalCabacZeroBytes=numberOfAdditionalCabacZeroWords*3;
-          if (m_pcCfg->getCabacZeroWordPaddingEnabled())
-          {
-            std::vector<Char> zeroBytesPadding(numberOfAdditionalCabacZeroBytes, Char(0));
-            for(std::size_t i=0; i<numberOfAdditionalCabacZeroWords; i++)
-            {
-              zeroBytesPadding[i*3+2]=3;  // 00 00 03
-            }
-            accessUnit.back()->m_nalUnitData.write(&(zeroBytesPadding[0]), numberOfAdditionalCabacZeroBytes);
-            printf("Adding %d bytes of padding\n", UInt(numberOfAdditionalCabacZeroWords*3));
-          }
-          else
-          {
-            printf("Standard would normally require adding %d bytes of padding\n", UInt(numberOfAdditionalCabacZeroWords*3));
-          }
-        }
-      }
-    }
+    cabac_zero_word_padding(pcSlice, pcPic, binCountsInNalUnits, numBytesInVclNalUnits, accessUnit.back()->m_nalUnitData, m_pcCfg->getCabacZeroWordPaddingEnabled());
 
     pcPic->compressMotion();
@@ -2747,67 +2799,6 @@
 
     m_pcCfg->setEncodedFlag(iGOPid, true);
-    xCalculateAddPSNR( pcPic, pcPic->getPicYuvRec(), accessUnit, dEncTime, snr_conversion, printFrameMSE );
-
-    //In case of field coding, compute the interlaced PSNR for both fields
-    if(isField)
-    {
-      Bool bothFieldsAreEncoded = false;
-      Int correspondingFieldPOC = pcPic->getPOC();
-      Int currentPicGOPPoc = m_pcCfg->getGOPEntry(iGOPid).m_POC;
-      if(pcPic->getPOC() == 0)
-      {
-        // particular case for POC 0 and 1. 
-        // If they are not encoded first and separately from other pictures, we need to change this 
-        // POC 0 is always encoded first then POC 1 is encoded
-        bothFieldsAreEncoded = false;
-      }
-      else if(pcPic->getPOC() == 1)
-      {
-        // if we are at POC 1, POC 0 has been encoded for sure
-        correspondingFieldPOC = 0;
-        bothFieldsAreEncoded = true;
-      }
-      else 
-      {
-        if(pcPic->getPOC()%2 == 1)
-        {
-          correspondingFieldPOC -= 1; // all odd POC are associated with the preceding even POC (e.g poc 1 is associated to poc 0)
-          currentPicGOPPoc      -= 1;
-        }
-        else
-        {
-          correspondingFieldPOC += 1; // all even POC are associated with the following odd POC (e.g poc 0 is associated to poc 1)
-          currentPicGOPPoc      += 1;
-        }
-        for(Int i = 0; i < m_iGopSize; i ++)
-        {
-          if(m_pcCfg->getGOPEntry(i).m_POC == currentPicGOPPoc)
-          {
-            bothFieldsAreEncoded = m_pcCfg->getGOPEntry(i).m_isEncoded;
-            break;
-          }
-        }
-      }
-
-      if(bothFieldsAreEncoded)
-      {        
-        //get complementary top field
-        TComList<TComPic*>::iterator   iterPic = rcListPic.begin();
-        while ((*iterPic)->getPOC() != correspondingFieldPOC)
-        {
-          iterPic ++;
-        }
-        TComPic* correspondingFieldPic = *(iterPic);
-
-        if( (pcPic->isTopField() && isTff) || (!pcPic->isTopField() && !isTff))
-        {
-          xCalculateInterlacedAddPSNR(pcPic, correspondingFieldPic, pcPic->getPicYuvRec(), correspondingFieldPic->getPicYuvRec(), accessUnit, dEncTime, snr_conversion, printFrameMSE );
-        }
-        else
-        {
-          xCalculateInterlacedAddPSNR(correspondingFieldPic, pcPic, correspondingFieldPic->getPicYuvRec(), pcPic->getPicYuvRec(), accessUnit, dEncTime, snr_conversion, printFrameMSE );
-        }
-      }
-    }
+
+    xCalculateAddPSNRs( isField, isTff, iGOPid, pcPic, accessUnit, rcListPic, dEncTime, snr_conversion, printFrameMSE );
 
     if (!digestStr.empty())
@@ -2850,5 +2841,9 @@
     }
 
-    xCreatePictureTimingSEI(IRAPGOPid, leadingSeiMessages, nestedSeiMessages, duInfoSeiMessages, accessUnit, pcSlice, isField, duData);
+#if EFFICIENT_FIELD_IRAP
+    xCreatePictureTimingSEI(effFieldIRAPMap.GetIRAPGOPid(), leadingSeiMessages, nestedSeiMessages, duInfoSeiMessages, accessUnit, pcSlice, isField, duData);
+#else
+    xCreatePictureTimingSEI(0, leadingSeiMessages, nestedSeiMessages, duInfoSeiMessages, accessUnit, pcSlice, isField, duData);
+#endif
     if (m_pcCfg->getScalableNestingSEIEnabled())
     {
@@ -2884,5 +2879,5 @@
       {
         OutputNALUnit nalu(NAL_UNIT_EOS, 0, pcSlice->getLayerId());
-        m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder, pcSlice);
+        m_pcEntropyCoder->setEntropyCoder(m_pcCavlcCoder);
         accessUnit.push_back(new NALUnitEBSP(nalu));
         m_prevPicHasEos = true;
@@ -2910,31 +2905,5 @@
 
 #if EFFICIENT_FIELD_IRAP
-    if(IRAPtoReorder)
-    {
-      if(swapIRAPForward)
-      {
-        if(iGOPid == IRAPGOPid)
-        {
-          iGOPid = IRAPGOPid +1;
-          IRAPtoReorder = false;
-        }
-        else if(iGOPid == IRAPGOPid +1)
-        {
-          iGOPid --;
-        }
-      }
-      else
-      {
-        if(iGOPid == IRAPGOPid)
-        {
-          iGOPid = IRAPGOPid -1;
-        }
-        else if(iGOPid == IRAPGOPid -1)
-        {
-          iGOPid = IRAPGOPid;
-          IRAPtoReorder = false;
-        }
-      }
-    }
+    iGOPid=effFieldIRAPMap.restoreGOPid(iGOPid);
 #endif
   } // iGOPid-loop
@@ -3152,4 +3121,71 @@
 #endif
 
+Void TEncGOP::xCalculateAddPSNRs( const Bool isField, const Bool isFieldTopFieldFirst, const Int iGOPid, TComPic* pcPic, const AccessUnit&accessUnit, TComList<TComPic*> &rcListPic, const Double dEncTime, const InputColourSpaceConversion snr_conversion, const Bool printFrameMSE )
+{
+  xCalculateAddPSNR( pcPic, pcPic->getPicYuvRec(), accessUnit, dEncTime, snr_conversion, printFrameMSE );
+
+  //In case of field coding, compute the interlaced PSNR for both fields
+  if(isField)
+  {
+    Bool bothFieldsAreEncoded = false;
+    Int correspondingFieldPOC = pcPic->getPOC();
+    Int currentPicGOPPoc = m_pcCfg->getGOPEntry(iGOPid).m_POC;
+    if(pcPic->getPOC() == 0)
+    {
+      // particular case for POC 0 and 1.
+      // If they are not encoded first and separately from other pictures, we need to change this
+      // POC 0 is always encoded first then POC 1 is encoded
+      bothFieldsAreEncoded = false;
+    }
+    else if(pcPic->getPOC() == 1)
+    {
+      // if we are at POC 1, POC 0 has been encoded for sure
+      correspondingFieldPOC = 0;
+      bothFieldsAreEncoded = true;
+    }
+    else
+    {
+      if(pcPic->getPOC()%2 == 1)
+      {
+        correspondingFieldPOC -= 1; // all odd POC are associated with the preceding even POC (e.g poc 1 is associated to poc 0)
+        currentPicGOPPoc      -= 1;
+      }
+      else
+      {
+        correspondingFieldPOC += 1; // all even POC are associated with the following odd POC (e.g poc 0 is associated to poc 1)
+        currentPicGOPPoc      += 1;
+      }
+      for(Int i = 0; i < m_iGopSize; i ++)
+      {
+        if(m_pcCfg->getGOPEntry(i).m_POC == currentPicGOPPoc)
+        {
+          bothFieldsAreEncoded = m_pcCfg->getGOPEntry(i).m_isEncoded;
+          break;
+        }
+      }
+    }
+
+    if(bothFieldsAreEncoded)
+    {
+      //get complementary top field
+      TComList<TComPic*>::iterator   iterPic = rcListPic.begin();
+      while ((*iterPic)->getPOC() != correspondingFieldPOC)
+      {
+        iterPic ++;
+      }
+      TComPic* correspondingFieldPic = *(iterPic);
+
+      if( (pcPic->isTopField() && isFieldTopFieldFirst) || (!pcPic->isTopField() && !isFieldTopFieldFirst))
+      {
+        xCalculateInterlacedAddPSNR(pcPic, correspondingFieldPic, pcPic->getPicYuvRec(), correspondingFieldPic->getPicYuvRec(), accessUnit, dEncTime, snr_conversion, printFrameMSE );
+      }
+      else
+      {
+        xCalculateInterlacedAddPSNR(correspondingFieldPic, pcPic, correspondingFieldPic->getPicYuvRec(), pcPic->getPicYuvRec(), accessUnit, dEncTime, snr_conversion, printFrameMSE );
+      }
+    }
+  }
+}
+
 Void TEncGOP::xCalculateAddPSNR( TComPic* pcPic, TComPicYuv* pcPicD, const AccessUnit& accessUnit, Double dEncTime, const InputColourSpaceConversion conversion, const Bool printFrameMSE )
 {
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.h	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.h	(revision 1291)
@@ -219,4 +219,5 @@
   Void  xGetBuffer        ( TComList<TComPic*>& rcListPic, TComList<TComPicYuv*>& rcListPicYuvRecOut, Int iNumPicRcvd, Int iTimeOffset, TComPic*& rpcPic, TComPicYuv*& rpcPicYuvRecOut, Int pocCurr, Bool isField );
 
+  Void  xCalculateAddPSNRs         ( const Bool isField, const Bool isFieldTopFieldFirst, const Int iGOPid, TComPic* pcPic, const AccessUnit&accessUnit, TComList<TComPic*> &rcListPic, Double dEncTime, const InputColourSpaceConversion snr_conversion, const Bool printFrameMSE );
   Void  xCalculateAddPSNR          ( TComPic* pcPic, TComPicYuv* pcPicD, const AccessUnit&, Double dEncTime, const InputColourSpaceConversion snr_conversion, const Bool printFrameMSE );
   Void  xCalculateInterlacedAddPSNR( TComPic* pcPicOrgFirstField, TComPic* pcPicOrgSecondField,
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncSampleAdaptiveOffset.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncSampleAdaptiveOffset.cpp	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncSampleAdaptiveOffset.cpp	(revision 1291)
@@ -246,6 +246,5 @@
 {
   m_pcRDGoOnSbacCoder = pcRDGoOnSbacCoder;
-  m_pcRDGoOnSbacCoder->setSlice(pcSlice);
-  m_pcRDGoOnSbacCoder->resetEntropy();
+  m_pcRDGoOnSbacCoder->resetEntropy(pcSlice);
   m_pcRDGoOnSbacCoder->resetBits();
 
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncSbac.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncSbac.cpp	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncSbac.cpp	(revision 1291)
@@ -58,5 +58,4 @@
 // new structure here
 : m_pcBitIf                            ( NULL )
-, m_pcSlice                            ( NULL )
 , m_pcBinIf                            ( NULL )
 , m_numContextModels                   ( 0 )
@@ -104,11 +103,11 @@
 // ====================================================================================================================
 
-Void TEncSbac::resetEntropy           ()
-{
-  Int  iQp              = m_pcSlice->getSliceQp();
-  SliceType eSliceType  = m_pcSlice->getSliceType();
-
-  SliceType encCABACTableIdx = m_pcSlice->getEncCABACTableIdx();
-  if (!m_pcSlice->isIntra() && (encCABACTableIdx==B_SLICE || encCABACTableIdx==P_SLICE) && m_pcSlice->getPPS()->getCabacInitPresentFlag())
+Void TEncSbac::resetEntropy           (const TComSlice *pSlice)
+{
+  Int  iQp              = pSlice->getSliceQp();
+  SliceType eSliceType  = pSlice->getSliceType();
+
+  SliceType encCABACTableIdx = pSlice->getEncCABACTableIdx();
+  if (!pSlice->isIntra() && (encCABACTableIdx==B_SLICE || encCABACTableIdx==P_SLICE) && pSlice->getPPS()->getCabacInitPresentFlag())
   {
     eSliceType = encCABACTableIdx;
@@ -161,9 +160,9 @@
  * stores the index of the closest table.  This index is used for the next P/B slice when cabac_init_present_flag is true.
  */
-SliceType TEncSbac::determineCabacInitIdx()
-{
-  Int  qp              = m_pcSlice->getSliceQp();
-
-  if (!m_pcSlice->isIntra())
+SliceType TEncSbac::determineCabacInitIdx(const TComSlice *pSlice)
+{
+  Int  qp              = pSlice->getSliceQp();
+
+  if (!pSlice->isIntra())
   {
     SliceType aSliceTypeChoices[] = {B_SLICE, P_SLICE};
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncSbac.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncSbac.h	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncSbac.h	(revision 1291)
@@ -74,8 +74,7 @@
 
   //  Virtual list
-  Void  resetEntropy           ();
-  SliceType determineCabacInitIdx  ();
+  Void  resetEntropy           (const TComSlice *pSlice);
+  SliceType determineCabacInitIdx  (const TComSlice *pSlice);
   Void  setBitstream           ( TComBitIf* p )  { m_pcBitIf = p; m_pcBinIf->init( p ); }
-  Void  setSlice               ( TComSlice* p )  { m_pcSlice = p;                       }
 
   Void  load                   ( const TEncSbac* pSrc  );
@@ -130,5 +129,4 @@
 protected:
   TComBitIf*    m_pcBitIf;
-  TComSlice*    m_pcSlice;
   TEncBinIf*    m_pcBinIf;
 
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncSlice.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncSlice.cpp	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncSlice.cpp	(revision 1291)
@@ -119,7 +119,5 @@
   m_pcCfg             = pcEncTop;
   m_pcListPic         = pcEncTop->getListPic();
-#if SVC_EXTENSION
-  m_ppcTEncTop        = pcEncTop->getLayerEnc();
-#endif
+
   m_pcGOPEncoder      = pcEncTop->getGOPEncoder();
   m_pcCuEncoder       = pcEncTop->getCuEncoder();
@@ -140,4 +138,8 @@
   m_piRdPicQp         = (Int*   )xMalloc( Int,    m_pcCfg->getDeltaQpRD() * 2 + 1 );
   m_pcRateCtrl        = pcEncTop->getRateCtrl();
+
+#if SVC_EXTENSION
+  m_ppcTEncTop        = pcEncTop->getLayerEnc();
+#endif
 }
 
@@ -724,5 +726,5 @@
 
   UInt startCtuTsAddr, boundingCtuTsAddr;
-  xDetermineStartAndBoundingCtuTsAddr ( startCtuTsAddr, boundingCtuTsAddr, pcPic, false );
+  xDetermineStartAndBoundingCtuTsAddr ( startCtuTsAddr, boundingCtuTsAddr, pcPic );
 
   for( UInt ctuTsAddr = startCtuTsAddr, ctuRsAddr = pcPic->getPicSym()->getCtuTsToRsAddrMap( startCtuTsAddr);
@@ -757,7 +759,7 @@
   UInt   startCtuTsAddr;
   UInt   boundingCtuTsAddr;
-  TComSlice* pcSlice            = pcPic->getSlice(getSliceIdx());
+  TComSlice* const pcSlice            = pcPic->getSlice(getSliceIdx());
   pcSlice->setSliceSegmentBits(0);
-  xDetermineStartAndBoundingCtuTsAddr ( startCtuTsAddr, boundingCtuTsAddr, pcPic, false );
+  xDetermineStartAndBoundingCtuTsAddr ( startCtuTsAddr, boundingCtuTsAddr, pcPic );
 
   // initialize cost values - these are used by precompressSlice (they should be parameters).
@@ -766,6 +768,6 @@
   m_uiPicDist       = 0;
 
-  m_pcEntropyCoder->setEntropyCoder   ( m_pppcRDSbacCoder[0][CI_CURR_BEST], pcSlice );
-  m_pcEntropyCoder->resetEntropy      ();
+  m_pcEntropyCoder->setEntropyCoder   ( m_pppcRDSbacCoder[0][CI_CURR_BEST] );
+  m_pcEntropyCoder->resetEntropy      ( pcSlice );
 
   TEncBinCABAC* pRDSbacCoder = (TEncBinCABAC *) m_pppcRDSbacCoder[0][CI_CURR_BEST]->getEncBinIf();
@@ -858,10 +860,10 @@
     if (ctuRsAddr == firstCtuRsAddrOfTile)
     {
-      m_pppcRDSbacCoder[0][CI_CURR_BEST]->resetEntropy();
+      m_pppcRDSbacCoder[0][CI_CURR_BEST]->resetEntropy(pcSlice);
     }
     else if ( ctuXPosInCtus == tileXPosInCtus && m_pcCfg->getWaveFrontsynchro())
     {
       // reset and then update contexts to the state at the end of the top-right CTU (if within current slice and tile).
-      m_pppcRDSbacCoder[0][CI_CURR_BEST]->resetEntropy();
+      m_pppcRDSbacCoder[0][CI_CURR_BEST]->resetEntropy(pcSlice);
       // Sync if the Top-Right is available.
       TComDataCU *pCtuUp = pCtu->getCtuAbove();
@@ -878,5 +880,5 @@
 
     // set go-on entropy coder (used for all trial encodings - the cu encoder and encoder search also have a copy of the same pointer)
-    m_pcEntropyCoder->setEntropyCoder ( m_pcRDGoOnSbacCoder, pcSlice );
+    m_pcEntropyCoder->setEntropyCoder ( m_pcRDGoOnSbacCoder );
     m_pcEntropyCoder->setBitstream( &tempBitCounter );
     tempBitCounter.resetBits();
@@ -944,5 +946,5 @@
     // which is used if there is a limit of the number of bytes per slice-segment.
 
-    m_pcEntropyCoder->setEntropyCoder ( m_pppcRDSbacCoder[0][CI_CURR_BEST], pcSlice );
+    m_pcEntropyCoder->setEntropyCoder ( m_pppcRDSbacCoder[0][CI_CURR_BEST] );
     m_pcEntropyCoder->setBitstream( &tempBitCounter );
     pRDSbacCoder->setBinCountingEnableFlag( true );
@@ -1049,5 +1051,5 @@
 Void TEncSlice::encodeSlice   ( TComPic* pcPic, TComOutputBitstream* pcSubstreams, UInt &numBinsCoded )
 {
-  TComSlice* pcSlice                 = pcPic->getSlice(getSliceIdx());
+  TComSlice *const pcSlice           = pcPic->getSlice(getSliceIdx());
 
   const UInt startCtuTsAddr          = pcSlice->getSliceSegmentCurStartCtuTsAddr();
@@ -1060,6 +1062,6 @@
   // initialise entropy coder for the slice
   m_pcSbacCoder->init( (TEncBinIf*)m_pcBinCABAC );
-  m_pcEntropyCoder->setEntropyCoder ( m_pcSbacCoder, pcSlice );
-  m_pcEntropyCoder->resetEntropy      ();
+  m_pcEntropyCoder->setEntropyCoder ( m_pcSbacCoder );
+  m_pcEntropyCoder->resetEntropy    ( pcSlice );
 
   numBinsCoded = 0;
@@ -1117,5 +1119,5 @@
       if (ctuTsAddr != startCtuTsAddr) // if it is the first CTU, then the entropy coder has already been reset
       {
-        m_pcEntropyCoder->resetEntropy();
+        m_pcEntropyCoder->resetEntropy(pcSlice);
       }
     }
@@ -1125,5 +1127,5 @@
       if (ctuTsAddr != startCtuTsAddr) // if it is the first CTU, then the entropy coder has already been reset
       {
-        m_pcEntropyCoder->resetEntropy();
+        m_pcEntropyCoder->resetEntropy(pcSlice);
       }
       TComDataCU *pCtuUp = pCtu->getCtuAbove();
@@ -1229,5 +1231,5 @@
   if (pcSlice->getPPS()->getCabacInitPresentFlag() && !pcSlice->getPPS()->getDependentSliceSegmentsEnabledFlag())
   {
-    m_encCABACTableIdx = m_pcEntropyCoder->determineCabacInitIdx();
+    m_encCABACTableIdx = m_pcEntropyCoder->determineCabacInitIdx(pcSlice);
   }
   else
@@ -1240,8 +1242,9 @@
 
 Void TEncSlice::calculateBoundingCtuTsAddrForSlice(UInt &startCtuTSAddrSlice, UInt &boundingCtuTSAddrSlice, Bool &haveReachedTileBoundary,
-                                                   TComPic* pcPic, const Bool encodingSlice, const Int sliceMode, const Int sliceArgument, const UInt sliceCurEndCtuTSAddr)
+                                                   TComPic* pcPic, const Int sliceMode, const Int sliceArgument, const UInt sliceCurEndCtuTSAddr)
 {
   TComSlice* pcSlice = pcPic->getSlice(getSliceIdx());
   const UInt numberOfCtusInFrame = pcPic->getNumberOfCtusInFrame();
+  const TComPPS &pps=*(pcSlice->getPPS());
   boundingCtuTSAddrSlice=0;
   haveReachedTileBoundary=false;
@@ -1256,5 +1259,5 @@
       break;
     case FIXED_NUMBER_OF_BYTES:
-      boundingCtuTSAddrSlice  = (encodingSlice) ? sliceCurEndCtuTSAddr : numberOfCtusInFrame;
+      boundingCtuTSAddrSlice  = numberOfCtusInFrame; // This will be adjusted later if required.
       break;
     case FIXED_NUMBER_OF_TILES:
@@ -1283,10 +1286,11 @@
 
   // Adjust for tiles and wavefronts.
+  const Bool wavefrontsAreEnabled = pps.getEntropyCodingSyncEnabledFlag();
+
   if ((sliceMode == FIXED_NUMBER_OF_CTU || sliceMode == FIXED_NUMBER_OF_BYTES) &&
-      (m_pcCfg->getNumRowsMinus1() > 0 || m_pcCfg->getNumColumnsMinus1() > 0))
+      (pps.getNumTileRowsMinus1() > 0 || pps.getNumTileColumnsMinus1() > 0))
   {
     const UInt ctuRSAddr                  = pcPic->getPicSym()->getCtuTsToRsAddrMap(startCtuTSAddrSlice);
     const UInt startTileIdx               = pcPic->getPicSym()->getTileIdxMap(ctuRSAddr);
-    const Bool wavefrontsAreEnabled       = m_pcCfg->getWaveFrontsynchro();
 
     const TComTile *pStartingTile         = pcPic->getPicSym()->getTComTile(startTileIdx);
@@ -1315,5 +1319,5 @@
     }
   }
-  else if ((sliceMode == FIXED_NUMBER_OF_CTU || sliceMode == FIXED_NUMBER_OF_BYTES) && pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag() && ((startCtuTSAddrSlice % pcPic->getFrameWidthInCtus()) != 0))
+  else if ((sliceMode == FIXED_NUMBER_OF_CTU || sliceMode == FIXED_NUMBER_OF_BYTES) && wavefrontsAreEnabled && ((startCtuTSAddrSlice % pcPic->getFrameWidthInCtus()) != 0))
   {
     // Adjust for wavefronts (no tiles).
@@ -1331,5 +1335,5 @@
  * Updates startCtuTsAddr, boundingCtuTsAddr with appropriate CTU address
  */
-Void TEncSlice::xDetermineStartAndBoundingCtuTsAddr  ( UInt& startCtuTsAddr, UInt& boundingCtuTsAddr, TComPic* pcPic, const Bool encodingSlice ) // TODO: this is now only ever called with encodingSlice=false
+Void TEncSlice::xDetermineStartAndBoundingCtuTsAddr  ( UInt& startCtuTsAddr, UInt& boundingCtuTsAddr, TComPic* pcPic )
 {
   TComSlice* pcSlice                 = pcPic->getSlice(getSliceIdx());
@@ -1340,5 +1344,5 @@
   UInt boundingCtuTsAddrSlice;
   calculateBoundingCtuTsAddrForSlice(startCtuTsAddrSlice, boundingCtuTsAddrSlice, haveReachedTileBoundarySlice, pcPic,
-                                     encodingSlice, m_pcCfg->getSliceMode(), m_pcCfg->getSliceArgument(), pcSlice->getSliceCurEndCtuTsAddr());
+                                     m_pcCfg->getSliceMode(), m_pcCfg->getSliceArgument(), pcSlice->getSliceCurEndCtuTsAddr());
   pcSlice->setSliceCurEndCtuTsAddr(   boundingCtuTsAddrSlice );
   pcSlice->setSliceCurStartCtuTsAddr( startCtuTsAddrSlice    );
@@ -1349,5 +1353,5 @@
   UInt boundingCtuTsAddrSliceSegment;
   calculateBoundingCtuTsAddrForSlice(startCtuTsAddrSliceSegment, boundingCtuTsAddrSliceSegment, haveReachedTileBoundarySliceSegment, pcPic,
-                                     encodingSlice, m_pcCfg->getSliceSegmentMode(), m_pcCfg->getSliceSegmentArgument(), pcSlice->getSliceSegmentCurEndCtuTsAddr());
+                                     m_pcCfg->getSliceSegmentMode(), m_pcCfg->getSliceSegmentArgument(), pcSlice->getSliceSegmentCurEndCtuTsAddr());
   if (boundingCtuTsAddrSliceSegment>boundingCtuTsAddrSlice)
   {
@@ -1381,4 +1385,4 @@
   xCalcACDCParamSlice(pcSlice);
 }
-#endif //SVC_EXTENSION
+#endif
 //! \}
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncSlice.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncSlice.h	(revision 1290)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncSlice.h	(revision 1291)
@@ -105,5 +105,5 @@
   Void     setUpLambda(TComSlice* slice, const Double dLambda, Int iQP);
 #endif
-  Void     calculateBoundingCtuTsAddrForSlice(UInt &startCtuTSAddrSlice, UInt &boundingCtuTSAddrSlice, Bool &haveReachedTileBoundary, TComPic* pcPic, const Bool encodingSlice, const Int sliceMode, const Int sliceArgument, const UInt uiSliceCurEndCtuTSAddr);
+  Void     calculateBoundingCtuTsAddrForSlice(UInt &startCtuTSAddrSlice, UInt &boundingCtuTSAddrSlice, Bool &haveReachedTileBoundary, TComPic* pcPic, const Int sliceMode, const Int sliceArgument, const UInt uiSliceCurEndCtuTSAddr);
 
 #if SVC_EXTENSION
@@ -133,5 +133,5 @@
 
   TEncCu*        getCUEncoder() { return m_pcCuEncoder; }                        ///< CU encoder
-  Void    xDetermineStartAndBoundingCtuTsAddr  ( UInt& startCtuTsAddr, UInt& boundingCtuTsAddr, TComPic* pcPic, const Bool encodingSlice );
+  Void    xDetermineStartAndBoundingCtuTsAddr  ( UInt& startCtuTsAddr, UInt& boundingCtuTsAddr, TComPic* pcPic );
   UInt    getSliceIdx()         { return m_uiSliceIdx;                    }
   Void    setSliceIdx(UInt i)   { m_uiSliceIdx = i;                       }
@@ -146,9 +146,7 @@
   Double  xGetQPValueAccordingToLambda ( Double lambda );
 
-#if SVC_EXTENSION
-#if JCTVC_M0259_LAMBDAREFINEMENT
+#if SVC_EXTENSION && JCTVC_M0259_LAMBDAREFINEMENT
   Double  xCalEnhLambdaFactor( Double deltaQP , Double beta );
 #endif
-#endif //SVC_EXTENSION
 };
 
