Index: /branches/SHM-3.1-dev/source/Lib/TLibDecoder/TDecTop.cpp
===================================================================
--- /branches/SHM-3.1-dev/source/Lib/TLibDecoder/TDecTop.cpp	(revision 433)
+++ /branches/SHM-3.1-dev/source/Lib/TLibDecoder/TDecTop.cpp	(revision 434)
@@ -842,4 +842,42 @@
   }
 #endif
+#if ALIGN_TSA_STSA_PICS
+  if( m_apcSlicePilot->getLayerId() > 0 )
+  {
+    // Check for TSA alignment
+    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ||
+        m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R 
+         )
+    {
+      for(Int dependentLayerIdx = 0; dependentLayerIdx < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
+      {
+        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
+        TComPic* refpicLayer = m_apcSlicePilot->getRefPic(*cListPic, m_apcSlicePilot->getPOC() );
+        if( refpicLayer )
+        {
+          assert( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ||
+                    m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R );    // TSA pictures should be aligned among depenedent layers
+        } 
+      }
+    }
+    // Check for STSA alignment
+    if( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ||
+         m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R 
+         )
+    {
+      for(Int dependentLayerIdx = 0; dependentLayerIdx < m_apcSlicePilot->getVPS()->getNumDirectRefLayers(m_layerId); dependentLayerIdx++)
+      {
+        TComList<TComPic*> *cListPic = getRefLayerDec( dependentLayerIdx )->getListPic();
+        TComPic* refpicLayer = m_apcSlicePilot->getRefPic(*cListPic, m_apcSlicePilot->getPOC() ); // STSA pictures should be aligned among dependent layers
+        if( refpicLayer )
+
+        {
+          assert( m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ||
+                    m_apcSlicePilot->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R );
+        }
+      }
+    }
+  }
+#endif
   //detect lost reference picture and insert copy of earlier frame.
   Int lostPoc;
Index: /branches/SHM-3.1-dev/source/Lib/TLibEncoder/TEncGOP.cpp
===================================================================
--- /branches/SHM-3.1-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 433)
+++ /branches/SHM-3.1-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 434)
@@ -874,26 +874,68 @@
       if(pcSlice->isTemporalLayerSwitchingPoint(rcListPic) || pcSlice->getSPS()->getTemporalIdNestingFlag())
       {
-        if(pcSlice->getTemporalLayerNonReferenceFlag())
-        {
-          pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TSA_N);
-        }
-        else
-        {
-          pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TLA_R);
-        }
 #if ALIGN_TSA_STSA_PICS
         if( pcSlice->getLayerId() > 0 )
         {
-          for(Int i = 0; i < pcSlice->getLayerId(); i++)
+          Bool oneRefLayerTSA = false, oneRefLayerNotTSA = 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 ) // If picture exists in Layer i
+            TComList<TComPic *> *cListPic = m_ppcTEncTop[i]->getListPic();
+            TComPic *lowerLayerPic = pcSlice->getRefPic(*cListPic, pcSlice->getPOC());
+            if( lowerLayerPic && pcSlice->getVPS()->getDirectDependencyFlag(pcSlice->getLayerId(), i) )
             {
-              assert( ( lowerLayerPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ) ||
-                      ( lowerLayerPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R ) );
-              // TSA pictures are aligned within an access unit.
+              if( ( lowerLayerPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_TSA_N ) ||
+                  ( lowerLayerPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_TLA_R ) 
+                )
+              {
+                if(pcSlice->getTemporalLayerNonReferenceFlag() )
+                {
+                  pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TSA_N);
+                }
+                else
+                {
+                  pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TLA_R );
+                }
+                oneRefLayerTSA = true;
+              }
+              else
+              {
+                oneRefLayerNotTSA = true;
+              }
             }
           }
+          assert( !( oneRefLayerNotTSA && oneRefLayerTSA ) ); // 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( oneRefLayerNotTSA /*&& !oneRefLayerTSA*/ )          // No reference layer is TSA - 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_TSA_N);
+            }
+            else
+            {
+              pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TLA_R );
+            }
+          }
+        }
+#else
+        if(pcSlice->getTemporalLayerNonReferenceFlag())
+        {
+          pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TSA_N);
+        }
+        else
+        {
+          pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_TLA_R);
         }
 #endif
@@ -931,4 +973,61 @@
         if(isSTSA==true)
         {    
+#if ALIGN_TSA_STSA_PICS
+          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())
           {
@@ -938,19 +1037,4 @@
           {
             pcSlice->setNalUnitType(NAL_UNIT_CODED_SLICE_STSA_R);
-          }
-#if ALIGN_TSA_STSA_PICS
-          if( pcSlice->getLayerId() > 0 )
-          {
-            for(Int i = 0; i < pcSlice->getLayerId(); i++)
-            {
-              TComList<TComPic*> *cListPic = m_ppcTEncTop[i]->getListPic();
-              TComPic* lowerLayerPic = pcSlice->getRefPic(*cListPic, pcSlice->getPOC() );
-              if( lowerLayerPic ) // If picture exists in Layer i
-              {
-                assert( ( lowerLayerPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_N ) ||
-                        ( lowerLayerPic->getSlice(0)->getNalUnitType() == NAL_UNIT_CODED_SLICE_STSA_R ) );
-              }
-              // STSA pictures are aligned within an access unit.
-            }
           }
 #endif
