Index: /branches/SHM-dev/source/Lib/TLibCommon/TComBitCounter.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TComBitCounter.h	(revision 1351)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TComBitCounter.h	(revision 1352)
@@ -63,6 +63,8 @@
 
   Void        write                 ( UInt /*uiBits*/, UInt uiNumberOfBits )  { m_uiBitCounter += uiNumberOfBits; }
-  Void        resetBits             ()                                    { m_uiBitCounter = 0;               }
-  UInt getNumberOfWrittenBits() const { return m_uiBitCounter; }
+  Void        resetBits             ()                                        { m_uiBitCounter = 0;               }
+  UInt        getNumberOfWrittenBits() const                                  { return m_uiBitCounter; }
+  Int         getNumBitsUntilByteAligned() const                              { return (8 - m_uiBitCounter) & 0x7;}
+
 };
 
Index: /branches/SHM-dev/source/Lib/TLibCommon/TComBitStream.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibCommon/TComBitStream.h	(revision 1351)
+++ /branches/SHM-dev/source/Lib/TLibCommon/TComBitStream.h	(revision 1352)
@@ -63,5 +63,6 @@
   virtual Void        write                 ( UInt uiBits, UInt uiNumberOfBits )  = 0;
   virtual Void        resetBits             ()                                    = 0;
-  virtual UInt getNumberOfWrittenBits() const = 0;
+  virtual UInt        getNumberOfWrittenBits() const = 0;
+  virtual Int         getNumBitsUntilByteAligned() const = 0;
   virtual ~TComBitIf() {}
 };
@@ -130,5 +131,5 @@
    * achieve byte alignment.
    */
-  Int getNumBitsUntilByteAligned() { return (8 - m_num_held_bits) & 0x7; }
+  Int getNumBitsUntilByteAligned() const { return (8 - m_num_held_bits) & 0x7; }
 
   /**
Index: /branches/SHM-dev/source/Lib/TLibDecoder/SEIread.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibDecoder/SEIread.cpp	(revision 1351)
+++ /branches/SHM-dev/source/Lib/TLibDecoder/SEIread.cpp	(revision 1352)
@@ -139,7 +139,5 @@
   while (m_pcBitstream->getNumBitsLeft() > 8);
 
-  UInt rbspTrailingBits;
-  sei_read_code(NULL, 8, rbspTrailingBits, "rbsp_trailing_bits");
-  assert(rbspTrailingBits == 0x80);
+  xReadRbspTrailingBits();
 }
 
Index: /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 1351)
+++ /branches/SHM-dev/source/Lib/TLibDecoder/TDecCAVLC.cpp	(revision 1352)
@@ -492,4 +492,5 @@
     }
   }
+  xReadRbspTrailingBits();
 }
 
@@ -1026,4 +1027,6 @@
     }
   }
+
+  xReadRbspTrailingBits();
 }
 
@@ -1178,5 +1181,5 @@
 #endif
 
-  return;
+  xReadRbspTrailingBits();
 }
 
Index: /branches/SHM-dev/source/Lib/TLibEncoder/NALwrite.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/NALwrite.cpp	(revision 1351)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/NALwrite.cpp	(revision 1352)
@@ -123,12 +123,3 @@
 }
 
-/**
- * Write rbsp_trailing_bits to bs causing it to become byte-aligned
- */
-Void writeRBSPTrailingBits(TComOutputBitstream& bs)
-{
-  bs.write( 1, 1 );
-  bs.writeAlignZero();
-}
-
 //! \}
Index: /branches/SHM-dev/source/Lib/TLibEncoder/NALwrite.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/NALwrite.h	(revision 1351)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/NALwrite.h	(revision 1352)
@@ -84,5 +84,4 @@
 
 Void write(std::ostream& out, OutputNALUnit& nalu);
-Void writeRBSPTrailingBits(TComOutputBitstream& bs);
 
 inline NALUnitEBSP::NALUnitEBSP(OutputNALUnit& nalu)
Index: /branches/SHM-dev/source/Lib/TLibEncoder/SEIwrite.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/SEIwrite.cpp	(revision 1351)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/SEIwrite.cpp	(revision 1352)
@@ -216,7 +216,7 @@
  */
 #if O0164_MULTI_LAYER_HRD
-Void SEIWriter::writeSEImessages(TComBitIf& bs, const SEIMessages &seiList, const TComVPS *vps, const TComSPS *sps, const SEIScalableNesting* nestingSei, const SEIBspNesting* bspNestingSei)
+Void SEIWriter::writeSEImessages(TComBitIf& bs, const SEIMessages &seiList, const TComVPS *vps, const TComSPS *sps, Bool isNested, const SEIScalableNesting* nestingSei, const SEIBspNesting* bspNestingSei)
 #else
-Void SEIWriter::writeSEImessages(TComBitIf& bs, const SEIMessages &seiList, const TComSPS *sps)
+Void SEIWriter::writeSEImessages(TComBitIf& bs, const SEIMessages &seiList, const TComSPS *sps, Bool isNested)
 #endif
 {
@@ -276,4 +276,8 @@
     xWriteSEIpayloadData(bs, **sei, sps);
 #endif
+  }
+  if (!isNested)
+  {
+    xWriteRbspTrailingBits();
   }
 }
@@ -793,8 +797,8 @@
 
   // write nested SEI messages
-#if O0164_MULTI_LAYER_HRD  
-  writeSEImessages(bs, sei.m_nestedSEIs, vps, sps, &sei);
+#if O0164_MULTI_LAYER_HRD
+  writeSEImessages(bs, sei.m_nestedSEIs, vps, sps, true, &sei);
 #else
-  writeSEImessages(bs, sei.m_nestedSEIs, sps);
+  writeSEImessages(bs, sei.m_nestedSEIs, sps, true);
 #endif
 }
@@ -1145,5 +1149,5 @@
   
   // write nested SEI messages
-  writeSEImessages(bs, sei.m_nestedSEIs, vps, sps, &nestingSei, &sei);
+  writeSEImessages(bs, sei.m_nestedSEIs, vps, sps, true, &nestingSei, &sei);
 }
 
Index: /branches/SHM-dev/source/Lib/TLibEncoder/SEIwrite.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/SEIwrite.h	(revision 1351)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/SEIwrite.h	(revision 1352)
@@ -51,7 +51,7 @@
 
 #if O0164_MULTI_LAYER_HRD
-  Void writeSEImessages(TComBitIf& bs, const SEIMessages &seiList, const TComVPS *vps, const TComSPS *sps, const SEIScalableNesting* nestingSei=NULL, const SEIBspNesting* bspNestingSei=NULL);
+  Void writeSEImessages(TComBitIf& bs, const SEIMessages &seiList, const TComVPS *vps, const TComSPS *sps, Bool isNested, const SEIScalableNesting* nestingSei=NULL, const SEIBspNesting* bspNestingSei=NULL);
 #else
-  Void writeSEImessages(TComBitIf& bs, const SEIMessages &seiList, const TComSPS *sps);
+  Void writeSEImessages(TComBitIf& bs, const SEIMessages &seiList, const TComSPS *sps, Bool isNested);
 #endif
 
@@ -64,5 +64,4 @@
   Void xWriteSEIuserDataUnregistered(const SEIuserDataUnregistered &sei);
   Void xWriteSEIActiveParameterSets(const SEIActiveParameterSets& sei);
-  Void xWriteSEIDecodedPictureHash(const SEIDecodedPictureHash& sei);
 #if SVC_EXTENSION
   Void xWriteSEIDecodingUnitInfo(const SEIDecodingUnitInfo& sei, const TComSPS *sps, const SEIScalableNesting* nestingSei, const SEIBspNesting* bspNestingSei, const TComVPS *vps);
@@ -71,4 +70,7 @@
 #else
   Void xWriteSEIDecodingUnitInfo(const SEIDecodingUnitInfo& sei, const TComSPS *sps);
+#endif
+  Void xWriteSEIDecodedPictureHash(const SEIDecodedPictureHash& sei);
+#if !SVC_EXTENSION
   Void xWriteSEIBufferingPeriod(const SEIBufferingPeriod& sei, const TComSPS *sps);
   Void xWriteSEIPictureTiming(const SEIPictureTiming& sei, const TComSPS *sps);
Index: /branches/SHM-dev/source/Lib/TLibEncoder/SyntaxElementWriter.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/SyntaxElementWriter.cpp	(revision 1351)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/SyntaxElementWriter.cpp	(revision 1352)
@@ -142,4 +142,16 @@
 }
 
+Void SyntaxElementWriter::xWriteRbspTrailingBits()
+{
+  WRITE_FLAG( 1, "rbsp_stop_one_bit");
+  Int cnt = 0;
+  while (m_pcBitIf->getNumBitsUntilByteAligned())
+  {
+    WRITE_FLAG( 0, "rbsp_alignment_zero_bit");
+    cnt++;
+  }
+  assert(cnt<8);
+}
+
 #if Q0096_OVERLAY_SEI
 Void  SyntaxElementWriter::xWriteString( UChar* sCode, UInt uiLength)
Index: /branches/SHM-dev/source/Lib/TLibEncoder/SyntaxElementWriter.h
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/SyntaxElementWriter.h	(revision 1351)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/SyntaxElementWriter.h	(revision 1352)
@@ -100,4 +100,5 @@
 #endif
 #endif
+  Void xWriteRbspTrailingBits();
 
   UInt  xConvertToUInt        ( Int iValue ) {  return ( iValue <= 0) ? -iValue<<1 : (iValue<<1)-1; }
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 1351)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncCavlc.cpp	(revision 1352)
@@ -369,4 +369,5 @@
     } // loop over PPS flags
   } // pps_extension_present_flag is non-zero
+  xWriteRbspTrailingBits();
 }
 
@@ -780,4 +781,5 @@
     }
   }
+  xWriteRbspTrailingBits();
 }
 
@@ -891,8 +893,8 @@
 #else
   WRITE_FLAG( 0,                     "vps_extension_flag" );
-#endif  
+#endif    
+
   //future extensions here..
-
-  return;
+  xWriteRbspTrailingBits();
 }
 
Index: /branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp
===================================================================
--- /branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 1351)
+++ /branches/SHM-dev/source/Lib/TLibEncoder/TEncGOP.cpp	(revision 1352)
@@ -231,5 +231,4 @@
   m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
   m_pcEntropyCoder->encodeVPS(vps);
-  writeRBSPTrailingBits(nalu.m_Bitstream);
   accessUnit.push_back(new NALUnitEBSP(nalu));
   return (Int)(accessUnit.back()->m_nalUnitData.str().size()) * 8;
@@ -253,5 +252,4 @@
   m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
   m_pcEntropyCoder->encodeSPS(sps);
-  writeRBSPTrailingBits(nalu.m_Bitstream);
   accessUnit.push_back(new NALUnitEBSP(nalu));
   return (Int)(accessUnit.back()->m_nalUnitData.str().size()) * 8;
@@ -280,6 +278,5 @@
 #else
   m_pcEntropyCoder->encodePPS(pps);
-#endif
-  writeRBSPTrailingBits(nalu.m_Bitstream);
+#endif  
   accessUnit.push_back(new NALUnitEBSP(nalu));
   return (Int)(accessUnit.back()->m_nalUnitData.str().size()) * 8;
@@ -312,10 +309,9 @@
 #if O0164_MULTI_LAYER_HRD
   OutputNALUnit nalu(naluType, temporalId, sps->getLayerId());
-  m_seiWriter.writeSEImessages(nalu.m_Bitstream, seiMessages, vps, sps, nestingSei, bspNestingSei);
+  m_seiWriter.writeSEImessages(nalu.m_Bitstream, seiMessages, vps, sps, false, nestingSei, bspNestingSei);
 #else
   OutputNALUnit nalu(naluType, temporalId);
-  m_seiWriter.writeSEImessages(nalu.m_Bitstream, seiMessages, sps);
-#endif
-  writeRBSPTrailingBits(nalu.m_Bitstream);
+  m_seiWriter.writeSEImessages(nalu.m_Bitstream, seiMessages, sps, false);
+#endif  
   auPos = accessUnit.insert(auPos, new NALUnitEBSP(nalu));
   auPos++;
@@ -339,10 +335,9 @@
 #if O0164_MULTI_LAYER_HRD
     OutputNALUnit nalu(naluType, temporalId, sps->getLayerId());
-    m_seiWriter.writeSEImessages(nalu.m_Bitstream, tmpMessages, vps, sps, nestingSei, bspNestingSei);
+    m_seiWriter.writeSEImessages(nalu.m_Bitstream, tmpMessages, vps, sps, false, nestingSei, bspNestingSei);
 #else
     OutputNALUnit nalu(naluType, temporalId);
-    m_seiWriter.writeSEImessages(nalu.m_Bitstream, tmpMessages, sps);
-#endif
-    writeRBSPTrailingBits(nalu.m_Bitstream);
+    m_seiWriter.writeSEImessages(nalu.m_Bitstream, tmpMessages, sps, false);
+#endif
     auPos = accessUnit.insert(auPos, new NALUnitEBSP(nalu));
     auPos++;
@@ -2577,6 +2572,5 @@
       OutputNALUnit nalu(NAL_UNIT_PPS, 0, m_layerId);
       m_pcEntropyCoder->setBitstream(&nalu.m_Bitstream);
-      m_pcEntropyCoder->encodePPS(pcSlice->getPPS() , &m_Enc3DAsymLUTPPS );
-      writeRBSPTrailingBits(nalu.m_Bitstream);
+      m_pcEntropyCoder->encodePPS(pcSlice->getPPS() , &m_Enc3DAsymLUTPPS );      
       accessUnit.push_back(new NALUnitEBSP(nalu));
     }
