Index: branches/SHM-dev/source/Lib/TLibCommon/TypeDef.h
===================================================================
--- branches/SHM-dev/source/Lib/TLibCommon/TypeDef.h	(revision 817)
+++ branches/SHM-dev/source/Lib/TLibCommon/TypeDef.h	(revision 818)
@@ -48,5 +48,5 @@
 #define O0137_MAX_LAYERID                1      ///< JCTVC-O0137, JCTVC-O0200, JCTVC-O0223: restrict nuh_layer_id and vps_max_layers_minus1
 
-#define Q0108_TSA_STSA                   0      ///< JCTVC-Q0108, Remove cross-layer alignment constraints of TSA and STSA pictures, enable to have different prediction structures in different layers
+#define Q0108_TSA_STSA                   1      ///< JCTVC-Q0108, Remove cross-layer alignment constraints of TSA and STSA pictures, enable to have different prediction structures in different layers
 #define Q0177_SPS_TEMP_NESTING_FIX       1      ///< JCTVC-Q0177; Fix the inference value of sps_temporal_id_nesting_flag when it is not present
 #define Q0177_EOS_CHECKS                 1      ///< JCTVC-Q0177; Put checks on handling EOS
Index: branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp
===================================================================
--- branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 817)
+++ branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 818)
@@ -609,7 +609,4 @@
   TEncSbac* pcSbacCoders = NULL;
   TComOutputBitstream* pcSubstreamsOut = NULL;
-#if Q0108_TSA_STSA
-  Int flagTSTA = 0;
-#endif
 
   xInitGOP( iPOCLast, iNumPicRcvd, rcListPic, rcListPicYuvRecOut, isField );
@@ -1563,7 +1560,62 @@
       }
     }
+
+    arrangeLongtermPicturesInRPS(pcSlice, rcListPic);
+    TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
+    refPicListModification->setRefPicListModificationFlagL0(0);
+    refPicListModification->setRefPicListModificationFlagL1(0);
+    pcSlice->setNumRefIdx(REF_PIC_LIST_0,min(m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive,pcSlice->getRPS()->getNumberOfPictures()));
+    pcSlice->setNumRefIdx(REF_PIC_LIST_1,min(m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive,pcSlice->getRPS()->getNumberOfPictures()));
+
+#if SVC_EXTENSION
+    if( m_layerId > 0 && pcSlice->getActiveNumILRRefIdx() )
+    {
+#if POC_RESET_FLAG || POC_RESET_IDC_ENCODER
+      if ( pocCurr > 0 && pcSlice->isRADL() && pcPic->getSlice(0)->getBaseColPic(pcPic->getSlice(0)->getInterLayerPredLayerIdc(0))->getSlice(0)->isRASL())
+#else
+      if (pcSlice->getPOC()>0  && pcSlice->isRADL() && pcPic->getSlice(0)->getBaseColPic(pcPic->getSlice(0)->getInterLayerPredLayerIdc(0))->getSlice(0)->isRASL())
+#endif
+      {
+        pcSlice->setActiveNumILRRefIdx(0);
+        pcSlice->setInterLayerPredEnabledFlag(0);
+      }
+      if( pcSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && pcSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA )
+      {
+        pcSlice->setNumRefIdx(REF_PIC_LIST_0, pcSlice->getActiveNumILRRefIdx());
+        pcSlice->setNumRefIdx(REF_PIC_LIST_1, pcSlice->getActiveNumILRRefIdx());
+      }
+      else
+      {
+        pcSlice->setNumRefIdx(REF_PIC_LIST_0, pcSlice->getNumRefIdx(REF_PIC_LIST_0)+pcSlice->getActiveNumILRRefIdx());
+        pcSlice->setNumRefIdx(REF_PIC_LIST_1, pcSlice->getNumRefIdx(REF_PIC_LIST_1)+pcSlice->getActiveNumILRRefIdx());
+      }
+
+      // check for the reference pictures whether there is at least one either temporal picture or ILRP with sample prediction type
+      if( pcSlice->getNumRefIdx( REF_PIC_LIST_0 ) - pcSlice->getActiveNumILRRefIdx() == 0 && pcSlice->getNumRefIdx( REF_PIC_LIST_1 ) - pcSlice->getActiveNumILRRefIdx() == 0 )
+      {
+        Bool foundSamplePredPicture = false;                
+
+        for( Int i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
+        {
+          if( m_ppcTEncTop[m_layerId]->getSamplePredEnabledFlag( pcSlice->getVPS()->getRefLayerId( m_layerId, pcSlice->getInterLayerPredLayerIdc(i) ) ) )
+          {
+            foundSamplePredPicture = true;
+            break;
+          }
+        }
+
+        if( !foundSamplePredPicture )
+        {
+          pcSlice->setSliceType(I_SLICE);
+          pcSlice->setInterLayerPredEnabledFlag(0);
+          pcSlice->setActiveNumILRRefIdx(0);
+        }
+      }
+    }
+#endif //SVC_EXTENSION
+
 #if Q0108_TSA_STSA
-    else if( ( pcSlice->getTLayer() == 0 && pcSlice->getLayerId() > 0)    // only for enhancement layer and with temporal layer 0
-       && !( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N     
+   if( ( pcSlice->getTLayer() == 0 && pcSlice->getLayerId() > 0  )    // only for enhancement layer and with temporal layer 0
+     && !( pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_N     
           || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RADL_R
           || pcSlice->getNalUnitType() == NAL_UNIT_CODED_SLICE_RASL_N
@@ -1576,4 +1628,19 @@
     {
         Bool isSTSA=true;
+        Bool isIntra=false;
+
+        for( Int i = 0; i < pcSlice->getLayerId(); i++)
+        {
+          TComList<TComPic *> *cListPic = m_ppcTEncTop[i]->getListPic();
+          TComPic *lowerLayerPic = pcSlice->getRefPic(*cListPic, pcSlice->getPOC());
+          if( lowerLayerPic && pcSlice->getVPS()->getDirectDependencyFlag(pcSlice->getLayerId(), i) )
+          {
+            if( lowerLayerPic->getSlice(0)->getSliceType() == I_SLICE)
+            { 
+              isIntra = true;
+            }
+          }
+        }
+
         for(Int ii=iGOPid+1; ii < m_pcCfg->getGOPSize() && isSTSA; ii++)
         {
@@ -1605,135 +1672,17 @@
           }
         }
-        if(isSTSA==true)
+        if(isSTSA==true && isIntra == false)
         {    
-#if !Q0108_TSA_STSA
-          if( pcSlice->getLayerId() > 0 )
-          {
-            Bool oneRefLayerSTSA = false, oneRefLayerNotSTSA = false;
-            for( Int i = 0; i < pcSlice->getLayerId(); i++)
-            {
-              TComList<TComPic *> *cListPic = m_ppcTEncTop[i]->getListPic();
-              TComPic *lowerLayerPic = pcSlice->getRefPic(*cListPic, pcSlice->getPOC());
-              if( lowerLayerPic && pcSlice->getVPS()->getDirectDependencyFlag(pcSlice->getLayerId(), i) )
-              {
-                if( ( lowerLayerPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ) ||
-                    ( lowerLayerPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R ) 
-                  )
-                {
-                  if(pcSlice->getTemporalLayerNonReferenceFlag() )
-                  {
-                    pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_STSA_N);
-                  }
-                  else
-                  {
-                    pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_STSA_R );
-                  }
-                  oneRefLayerSTSA = true;
-                }
-                else
-                {
-                  oneRefLayerNotSTSA = true;
-                }
-              }
-            }
-            assert( !( oneRefLayerNotSTSA && oneRefLayerSTSA ) ); // Only one variable should be true - failure of this assert means
-                                                                  // that two independent reference layers that are not dependent on
-                                                                  // each other, but are reference for current layer have inconsistency
-            if( oneRefLayerNotSTSA /*&& !oneRefLayerSTSA*/ )          // No reference layer is STSA - set current as TRAIL
-            {
-              if(pcSlice->getTemporalLayerNonReferenceFlag() )
-              {
-                pcSlice->setNalUnitType( NAL_UNIT_CODED_SLICE_TRAIL_N );
-              }
-              else
-              {
-                pcSlice->setNalUnitType( NAL_UNIT_CODED_SLICE_TRAIL_R );
-              }
-            }
-            else  // This means there is no reference layer picture for current picture in this AU
-            {
-              if(pcSlice->getTemporalLayerNonReferenceFlag() )
-              {
-                pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_STSA_N);
-              }
-              else
-              {
-                pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_STSA_R );
-              }
-            }
-          }
-#else
           if(pcSlice->getTemporalLayerNonReferenceFlag())
           {
             pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_STSA_N);
-            flagTSTA = 1;
           }
           else
           {
             pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_STSA_R);
-            flagTSTA = 1;
-          }
-#endif
-        }
-    }
-#endif
-
-    arrangeLongtermPicturesInRPS(pcSlice, rcListPic);
-    TComRefPicListModification* refPicListModification = pcSlice->getRefPicListModification();
-    refPicListModification->setRefPicListModificationFlagL0(0);
-    refPicListModification->setRefPicListModificationFlagL1(0);
-    pcSlice->setNumRefIdx(REF_PIC_LIST_0,min(m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive,pcSlice->getRPS()->getNumberOfPictures()));
-    pcSlice->setNumRefIdx(REF_PIC_LIST_1,min(m_pcCfg->getGOPEntry(iGOPid).m_numRefPicsActive,pcSlice->getRPS()->getNumberOfPictures()));
-
-#if SVC_EXTENSION
-    if( m_layerId > 0 && pcSlice->getActiveNumILRRefIdx() )
-    {
-#if POC_RESET_FLAG || POC_RESET_IDC_ENCODER
-      if ( pocCurr > 0 && pcSlice->isRADL() && pcPic->getSlice(0)->getBaseColPic(pcPic->getSlice(0)->getInterLayerPredLayerIdc(0))->getSlice(0)->isRASL())
-#else
-      if (pcSlice->getPOC()>0  && pcSlice->isRADL() && pcPic->getSlice(0)->getBaseColPic(pcPic->getSlice(0)->getInterLayerPredLayerIdc(0))->getSlice(0)->isRASL())
-#endif
-      {
-        pcSlice->setActiveNumILRRefIdx(0);
-        pcSlice->setInterLayerPredEnabledFlag(0);
-      }
-#if Q0108_TSA_STSA
-     if( ( pcSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && pcSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA ) || flagTSTA == 1 )
-#else
-     if( pcSlice->getNalUnitType() >= NAL_UNIT_CODED_SLICE_BLA_W_LP && pcSlice->getNalUnitType() <= NAL_UNIT_CODED_SLICE_CRA )
-#endif
-      {
-        pcSlice->setNumRefIdx(REF_PIC_LIST_0, pcSlice->getActiveNumILRRefIdx());
-        pcSlice->setNumRefIdx(REF_PIC_LIST_1, pcSlice->getActiveNumILRRefIdx());
-      }
-      else
-      {
-        pcSlice->setNumRefIdx(REF_PIC_LIST_0, pcSlice->getNumRefIdx(REF_PIC_LIST_0)+pcSlice->getActiveNumILRRefIdx());
-        pcSlice->setNumRefIdx(REF_PIC_LIST_1, pcSlice->getNumRefIdx(REF_PIC_LIST_1)+pcSlice->getActiveNumILRRefIdx());
-      }
-
-      // check for the reference pictures whether there is at least one either temporal picture or ILRP with sample prediction type
-      if( pcSlice->getNumRefIdx( REF_PIC_LIST_0 ) - pcSlice->getActiveNumILRRefIdx() == 0 && pcSlice->getNumRefIdx( REF_PIC_LIST_1 ) - pcSlice->getActiveNumILRRefIdx() == 0 )
-      {
-        Bool foundSamplePredPicture = false;                
-
-        for( Int i = 0; i < pcSlice->getActiveNumILRRefIdx(); i++ )
-        {
-          if( m_ppcTEncTop[m_layerId]->getSamplePredEnabledFlag( pcSlice->getVPS()->getRefLayerId( m_layerId, pcSlice->getInterLayerPredLayerIdc(i) ) ) )
-          {
-            foundSamplePredPicture = true;
-            break;
-          }
-        }
-
-        if( !foundSamplePredPicture )
-        {
-          pcSlice->setSliceType(I_SLICE);
-          pcSlice->setInterLayerPredEnabledFlag(0);
-          pcSlice->setActiveNumILRRefIdx(0);
-        }
-      }
-    }
-#endif //SVC_EXTENSION
+          }
+        }
+    }
+#endif
 
 #if ADAPTIVE_QP_SELECTION
