Index: branches/SHM-3.0-dev/source/App/TAppEncoder/TAppEncCfg.cpp
===================================================================
--- branches/SHM-3.0-dev/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 333)
+++ branches/SHM-3.0-dev/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 334)
@@ -817,4 +817,11 @@
     opts.addOptions()(cOSS.str(), m_GOPList[i-1], GOPEntry());
   }
+#if FINAL_RPL_CHANGE_N0082
+  for(Int i=1; i<MAX_GOP+1; i++) {
+    std::ostringstream cOSS;
+    cOSS<<"FrameEL"<<i;
+    opts.addOptions()(cOSS.str(), m_acLayerCfg[1].m_GOPListLayer[i-1], GOPEntry());
+  }
+#endif
   po::setDefaults(opts);
   const list<const Char*>& argv_unhandled = po::scanArgv(opts, argc, (const Char**) argv);
@@ -1460,8 +1467,12 @@
 #endif
   
+#if !FINAL_RPL_CHANGE_N0082
   Bool verifiedGOP=false;
+#endif
   Bool errorGOP=false;
+#if !FINAL_RPL_CHANGE_N0082
   Int checkGOP=1;
   Int numRefs = 1;
+#endif
   Int refList[MAX_NUM_REF_PICS+1];
   refList[0]=0;
@@ -1471,5 +1482,7 @@
     isOK[i]=false;
   }
+#if !FINAL_RPL_CHANGE_N0082
   Int numOK=0;
+#endif
 #if !SVC_EXTENSION
   xConfirmPara( m_iIntraPeriod >=0&&(m_iIntraPeriod%m_iGOPSize!=0), "Intra period must be a multiple of GOPSize, or -1" );
@@ -1514,5 +1527,23 @@
   }
 #endif
-
+#if FINAL_RPL_CHANGE_N0082
+  for(UInt layer=0; layer<m_numLayers; layer++)
+  {
+    if (m_acLayerCfg[layer].m_GOPListLayer[0].m_POC<0){
+      memcpy( m_acLayerCfg[layer].m_GOPListLayer, m_GOPList, sizeof(GOPEntry)*MAX_GOP );
+    }
+    errorGOP = xconfirmExtraGOP( m_acLayerCfg[layer].m_GOPListLayer );
+    xConfirmPara(errorGOP,"Invalid GOP structure given");
+  }
+  //tentative for encoder
+  if( m_acLayerCfg[1].m_GOPListLayer[5].m_POC == 6  && m_acLayerCfg[1].m_GOPListLayer[7].m_POC == 7 ){
+    //RA, POC5
+    m_acLayerCfg[1].m_GOPListLayer[5].m_usedByCurrPic[2] = 0;
+    m_acLayerCfg[1].m_GOPListLayer[5].m_refIdc[2] = 0;
+    //RA, POC7
+    m_acLayerCfg[1].m_GOPListLayer[7].m_usedByCurrPic[2] = 0;
+    m_acLayerCfg[1].m_GOPListLayer[7].m_refIdc[2] = 0;
+  }
+#else
   m_extraRPSs=0;
   //start looping through frames in coding order until we can verify that the GOP structure is correct.
@@ -1710,4 +1741,5 @@
   }
   xConfirmPara(errorGOP,"Invalid GOP structure given");
+#endif
   m_maxTempLayer = 1;
   for(Int i=0; i<m_iGOPSize; i++) 
@@ -2235,3 +2267,218 @@
 }
 #endif
+
+#if FINAL_RPL_CHANGE_N0082
+Bool  TAppEncCfg::xconfirmExtraGOP (GOPEntry * ge)
+{
+  Bool verifiedGOP=false;
+  Bool errorGOP=false;
+  Int checkGOP=1;
+  Int numRefs = 1;
+  Int refList[MAX_NUM_REF_PICS+1];
+  refList[0]=0;
+  Bool isOK[MAX_GOP];
+  for(Int i=0; i<MAX_GOP; i++) 
+  {
+    isOK[i]=false;
+  }
+  Int numOK=0;
+
+  m_extraRPSs=0;
+  //start looping through frames in coding order until we can verify that the GOP structure is correct.
+  while(!verifiedGOP&&!errorGOP) 
+  {
+    Int curGOP = (checkGOP-1)%m_iGOPSize;
+    Int curPOC = ((checkGOP-1)/m_iGOPSize)*m_iGOPSize + ge[curGOP].m_POC;    
+    if(ge[curGOP].m_POC<0) 
+    {
+      printf("\nError: found fewer Reference Picture Sets than GOPSize\n");
+      errorGOP=true;
+    }
+    else 
+    {
+      //check that all reference pictures are available, or have a POC < 0 meaning they might be available in the next GOP.
+      Bool beforeI = false;
+      for(Int i = 0; i< ge[curGOP].m_numRefPics; i++) 
+      {
+        Int absPOC = curPOC+ge[curGOP].m_referencePics[i];
+        if(absPOC < 0)
+        {
+          beforeI=true;
+        }
+        else 
+        {
+          Bool found=false;
+          for(Int j=0; j<numRefs; j++) 
+          {
+            if(refList[j]==absPOC) 
+            {
+              found=true;
+              for(Int k=0; k<m_iGOPSize; k++)
+              {
+                if(absPOC%m_iGOPSize == ge[k].m_POC%m_iGOPSize)
+                {
+                  if(ge[k].m_temporalId==ge[curGOP].m_temporalId)
+                  {
+                    ge[k].m_refPic = true;
+                  }
+                  ge[curGOP].m_usedByCurrPic[i]=ge[k].m_temporalId<=ge[curGOP].m_temporalId;
+                }
+              }
+            }
+          }
+          if(!found)
+          {
+            printf("\nError: ref pic %d is not available for GOP frame %d\n",ge[curGOP].m_referencePics[i],curGOP+1);
+            errorGOP=true;
+          }
+        }
+      }
+      if(!beforeI&&!errorGOP)
+      {
+        //all ref frames were present
+        if(!isOK[curGOP]) 
+        {
+          numOK++;
+          isOK[curGOP]=true;
+          if(numOK==m_iGOPSize)
+          {
+            verifiedGOP=true;
+          }
+        }
+      }
+      else 
+      {
+        //create a new GOPEntry for this frame containing all the reference pictures that were available (POC > 0)
+        ge[m_iGOPSize+m_extraRPSs]=ge[curGOP];
+        Int newRefs=0;
+        for(Int i = 0; i< ge[curGOP].m_numRefPics; i++) 
+        {
+          Int absPOC = curPOC+ge[curGOP].m_referencePics[i];
+          if(absPOC>=0)
+          {
+            ge[m_iGOPSize+m_extraRPSs].m_referencePics[newRefs]=ge[curGOP].m_referencePics[i];
+            ge[m_iGOPSize+m_extraRPSs].m_usedByCurrPic[newRefs]=ge[curGOP].m_usedByCurrPic[i];
+            newRefs++;
+          }
+        }
+        Int numPrefRefs = ge[curGOP].m_numRefPicsActive;
+        
+        for(Int offset = -1; offset>-checkGOP; offset--)
+        {
+          //step backwards in coding order and include any extra available pictures we might find useful to replace the ones with POC < 0.
+          Int offGOP = (checkGOP-1+offset)%m_iGOPSize;
+          Int offPOC = ((checkGOP-1+offset)/m_iGOPSize)*m_iGOPSize + ge[offGOP].m_POC;
+          if(offPOC>=0&&ge[offGOP].m_temporalId<=ge[curGOP].m_temporalId)
+          {
+            Bool newRef=false;
+            for(Int i=0; i<numRefs; i++)
+            {
+              if(refList[i]==offPOC)
+              {
+                newRef=true;
+              }
+            }
+            for(Int i=0; i<newRefs; i++) 
+            {
+              if(ge[m_iGOPSize+m_extraRPSs].m_referencePics[i]==offPOC-curPOC)
+              {
+                newRef=false;
+              }
+            }
+            if(newRef) 
+            {
+              Int insertPoint=newRefs;
+              //this picture can be added, find appropriate place in list and insert it.
+              if(ge[offGOP].m_temporalId==ge[curGOP].m_temporalId)
+              {
+                ge[offGOP].m_refPic = true;
+              }
+              for(Int j=0; j<newRefs; j++)
+              {
+                if(ge[m_iGOPSize+m_extraRPSs].m_referencePics[j]<offPOC-curPOC||ge[m_iGOPSize+m_extraRPSs].m_referencePics[j]>0)
+                {
+                  insertPoint = j;
+                  break;
+                }
+              }
+              Int prev = offPOC-curPOC;
+              Int prevUsed = ge[offGOP].m_temporalId<=ge[curGOP].m_temporalId;
+              for(Int j=insertPoint; j<newRefs+1; j++)
+              {
+                Int newPrev = ge[m_iGOPSize+m_extraRPSs].m_referencePics[j];
+                Int newUsed = ge[m_iGOPSize+m_extraRPSs].m_usedByCurrPic[j];
+                ge[m_iGOPSize+m_extraRPSs].m_referencePics[j]=prev;
+                ge[m_iGOPSize+m_extraRPSs].m_usedByCurrPic[j]=prevUsed;
+                prevUsed=newUsed;
+                prev=newPrev;
+              }
+              newRefs++;
+            }
+          }
+          if(newRefs>=numPrefRefs)
+          {
+            break;
+          }
+        }
+        ge[m_iGOPSize+m_extraRPSs].m_numRefPics=newRefs;
+        ge[m_iGOPSize+m_extraRPSs].m_POC = curPOC;
+        if (m_extraRPSs == 0)
+        {
+          ge[m_iGOPSize+m_extraRPSs].m_interRPSPrediction = 0;
+          ge[m_iGOPSize+m_extraRPSs].m_numRefIdc = 0;
+        }
+        else
+        {
+          Int rIdx =  m_iGOPSize + m_extraRPSs - 1;
+          Int refPOC = ge[rIdx].m_POC;
+          Int refPics = ge[rIdx].m_numRefPics;
+          Int newIdc=0;
+          for(Int i = 0; i<= refPics; i++) 
+          {
+            Int deltaPOC = ((i != refPics)? ge[rIdx].m_referencePics[i] : 0);  // check if the reference abs POC is >= 0
+            Int absPOCref = refPOC+deltaPOC;
+            Int refIdc = 0;
+            for (Int j = 0; j < ge[m_iGOPSize+m_extraRPSs].m_numRefPics; j++)
+            {
+              if ( (absPOCref - curPOC) == ge[m_iGOPSize+m_extraRPSs].m_referencePics[j])
+              {
+                if (ge[m_iGOPSize+m_extraRPSs].m_usedByCurrPic[j])
+                {
+                  refIdc = 1;
+                }
+                else
+                {
+                  refIdc = 2;
+                }
+              }
+            }
+            ge[m_iGOPSize+m_extraRPSs].m_refIdc[newIdc]=refIdc;
+            newIdc++;
+          }
+          ge[m_iGOPSize+m_extraRPSs].m_interRPSPrediction = 1;  
+          ge[m_iGOPSize+m_extraRPSs].m_numRefIdc = newIdc;
+          ge[m_iGOPSize+m_extraRPSs].m_deltaRPS = refPOC - ge[m_iGOPSize+m_extraRPSs].m_POC; 
+        }
+        curGOP=m_iGOPSize+m_extraRPSs;
+        m_extraRPSs++;
+      }
+      numRefs=0;
+      for(Int i = 0; i< ge[curGOP].m_numRefPics; i++) 
+      {
+        Int absPOC = curPOC+ge[curGOP].m_referencePics[i];
+        if(absPOC >= 0) 
+        {
+          refList[numRefs]=absPOC;
+          numRefs++;
+        }
+      }
+      refList[numRefs]=curPOC;
+      numRefs++;
+    }
+    checkGOP++;
+  }
+
+  return errorGOP; //update
+}
+#endif
 //! \}
Index: branches/SHM-3.0-dev/source/App/TAppEncoder/TAppEncCfg.h
===================================================================
--- branches/SHM-3.0-dev/source/App/TAppEncoder/TAppEncCfg.h	(revision 333)
+++ branches/SHM-3.0-dev/source/App/TAppEncoder/TAppEncCfg.h	(revision 334)
@@ -348,4 +348,7 @@
   Void  xPrintParameter ();                                   ///< print configuration values
   Void  xPrintUsage     ();                                   ///< print usage
+#if FINAL_RPL_CHANGE_N0082
+  Bool  xconfirmExtraGOP (GOPEntry * ge);
+#endif
 #if M0040_ADAPTIVE_RESOLUTION_CHANGE
   Int       m_adaptiveResolutionChange;                       ///< Indicate adaptive resolution change frame
Index: branches/SHM-3.0-dev/source/App/TAppEncoder/TAppEncLayerCfg.h
===================================================================
--- branches/SHM-3.0-dev/source/App/TAppEncoder/TAppEncLayerCfg.h	(revision 333)
+++ branches/SHM-3.0-dev/source/App/TAppEncoder/TAppEncLayerCfg.h	(revision 334)
@@ -81,4 +81,7 @@
   Int       m_scaledRefLayerBottomOffset[MAX_LAYERS];
 #endif  
+#if FINAL_RPL_CHANGE_N0082
+  GOPEntry  m_GOPListLayer[MAX_GOP];                            ///< for layer
+#endif
 public:
   TAppEncLayerCfg();
@@ -141,4 +144,7 @@
   Bool    getRCForceIntraQP()         {return m_RCForceIntraQP;        }
 #endif
+#if FINAL_RPL_CHANGE_N0082
+  GOPEntry getGOPEntry(Int i )        {return m_GOPListLayer[i];  }
+#endif
 }; // END CLASS DEFINITION TAppEncLayerCfg
 
Index: branches/SHM-3.0-dev/source/App/TAppEncoder/TAppEncTop.cpp
===================================================================
--- branches/SHM-3.0-dev/source/App/TAppEncoder/TAppEncTop.cpp	(revision 333)
+++ branches/SHM-3.0-dev/source/App/TAppEncoder/TAppEncTop.cpp	(revision 334)
@@ -136,5 +136,9 @@
     m_acTEncTop[layer].setDecodingRefreshType          ( m_iDecodingRefreshType );
     m_acTEncTop[layer].setGOPSize                      ( m_iGOPSize );
+#if FINAL_RPL_CHANGE_N0082
+    m_acTEncTop[layer].setGopList                      ( m_acLayerCfg[layer].m_GOPListLayer );
+#else
     m_acTEncTop[layer].setGopList                      ( m_GOPList );
+#endif
     m_acTEncTop[layer].setExtraRPSs                    ( m_extraRPSs );
     for(Int i = 0; i < MAX_TLAYER; i++)
Index: branches/SHM-3.0-dev/source/Lib/TLibCommon/TComSlice.cpp
===================================================================
--- branches/SHM-3.0-dev/source/Lib/TLibCommon/TComSlice.cpp	(revision 333)
+++ branches/SHM-3.0-dev/source/Lib/TLibCommon/TComSlice.cpp	(revision 334)
@@ -584,4 +584,24 @@
       rpsCurrList0[cIdx] = RefPicSetStCurr0[i];
     }
+#if RPL_INIT_N0316_N0082
+#if REF_IDX_FRAMEWORK
+    if( m_layerId > 0 )
+    {
+#if JCTVC_M0458_INTERLAYER_RPS_SIG
+      for( i = 0; i < m_activeNumILRRefIdx && cIdx < numPocTotalCurr; cIdx ++, i ++)      
+#else
+      for( i = 0; i < m_numILRRefIdx && cIdx < numPocTotalCurr; cIdx ++, i ++)
+#endif 
+      {
+        Int refLayerIdc = m_interLayerPredLayerIdc[i];
+#if ILR_RESTR
+        Int maxSubLayerForILPPlus1 = getVPS()->getMaxSublayerForIlpPlus1(ilpPic[refLayerIdc]->getSlice(0)->getLayerId());
+        if( ((Int)(ilpPic[refLayerIdc]->getSlice(0)->getTLayer())<=maxSubLayerForILPPlus1-1) || (maxSubLayerForILPPlus1==0 && ilpPic[refLayerIdc]->getSlice(0)->getRapPicFlag()) )
+#endif
+          rpsCurrList0[cIdx] = ilpPic[refLayerIdc];
+      }
+    }
+#endif
+#endif 
     for ( i=0; i<NumPocStCurr1; i++, cIdx++)
     {
@@ -592,5 +612,5 @@
       rpsCurrList0[cIdx] = RefPicSetLtCurr[i];
     }    
-
+#if !RPL_INIT_N0316_N0082
 #if REF_IDX_FRAMEWORK
     if( m_layerId > 0 )
@@ -611,4 +631,5 @@
     }
 #endif
+#endif
   assert(cIdx == numPocTotalCurr);
 
@@ -692,4 +713,7 @@
   TComRefPicListModification* refPicListModification = &m_RefPicListModification;
   Int numberOfRpsCurrTempList = this->getNumRpsCurrTempList();  // total number of ref pics in listTemp0 including inter-layer ref pics
+#if RPL_INIT_N0316_N0082
+  Int numberOfPocBeforeCurr = this->getNumNegativeRpsCurrTempList();  // number of negative temporal ref pics 
+#endif
 
   assert(m_aiNumRefIdx[REF_PIC_LIST_0] > 1);
@@ -697,5 +721,12 @@
 
   //set L0 inter-layer reference picture modification
+#if RPL_INIT_N0316_N0082
+  Bool hasModification = (m_aiNumRefIdx[REF_PIC_LIST_0] == (numberOfPocBeforeCurr + m_activeNumILRRefIdx)) ? false : true;
+#else
   Bool hasModification = (m_aiNumRefIdx[REF_PIC_LIST_0] == numberOfRpsCurrTempList) ? false : true;
+#endif
+#if FINAL_RPL_CHANGE_N0082
+  hasModification = false; //modification is not necessary
+#endif
   refPicListModification->setRefPicListModificationFlagL0(hasModification);
   if(hasModification)
@@ -721,5 +752,16 @@
 #endif 
       {
+#if RPL_INIT_N0316_N0082
+        if((numberOfPocBeforeCurr) >= m_aiNumRefIdx[REF_PIC_LIST_0])
+          refPicListModification->setRefPicSetIdxL0(m_aiNumRefIdx[REF_PIC_LIST_0] - i, numberOfPocBeforeCurr);
+        else
+        {
+          refPicListModification->setRefPicSetIdxL0(m_aiNumRefIdx[REF_PIC_LIST_0] - i, numberOfPocBeforeCurr);
+          for (Int j = numberOfPocBeforeCurr; j < (m_aiNumRefIdx[REF_PIC_LIST_0] - i); j++)
+            refPicListModification->setRefPicSetIdxL0(j, j + m_activeNumILRRefIdx);
+        }
+#else
         refPicListModification->setRefPicSetIdxL0(m_aiNumRefIdx[REF_PIC_LIST_0] - i, numberOfRpsCurrTempList - i);
+#endif
       }
     }
@@ -758,5 +800,24 @@
 }
 #endif
-
+#if RPL_INIT_N0316_N0082
+Int TComSlice::getNumNegativeRpsCurrTempList()
+{
+  if( m_eSliceType == I_SLICE ) 
+  {
+    return 0;
+  }
+
+  Int numPocBeforeCurr = 0;
+  for( UInt i = 0; i < m_pcRPS->getNumberOfNegativePictures(); i++ )
+  {
+    if(m_pcRPS->getUsed(i))
+    {
+      numPocBeforeCurr++;
+    }
+  }
+
+  return numPocBeforeCurr;
+}
+#endif
 Int TComSlice::getNumRpsCurrTempList()
 {
Index: branches/SHM-3.0-dev/source/Lib/TLibCommon/TComSlice.h
===================================================================
--- branches/SHM-3.0-dev/source/Lib/TLibCommon/TComSlice.h	(revision 333)
+++ branches/SHM-3.0-dev/source/Lib/TLibCommon/TComSlice.h	(revision 334)
@@ -1567,4 +1567,7 @@
   Bool      getMvdL1ZeroFlag ()                                  { return m_bLMvdL1Zero;    }
   Int       getNumRpsCurrTempList();
+#if RPL_INIT_N0316_N0082
+  Int       getNumNegativeRpsCurrTempList();
+#endif
   Int       getList1IdxToList0Idx ( Int list1Idx )               { return m_list1IdxToList0Idx[list1Idx]; }
   Void      setReferenced(Bool b)                               { m_bRefenced = b; }
Index: branches/SHM-3.0-dev/source/Lib/TLibCommon/TypeDef.h
===================================================================
--- branches/SHM-3.0-dev/source/Lib/TLibCommon/TypeDef.h	(revision 333)
+++ branches/SHM-3.0-dev/source/Lib/TLibCommon/TypeDef.h	(revision 334)
@@ -47,5 +47,6 @@
 #if SVC_EXTENSION
 #define MAX_LAYERS                       2      ///< max number of layers the codec is supposed to handle
-
+#define RPL_INIT_N0316_N0082             1      ///< N0316, N0082: initial reference picture list construction 
+#define FINAL_RPL_CHANGE_N0082           1      ///< N0082: final ref picture list change (encoder)
 #define M0464_TILE_BOUNDARY_ALIGNED_FLAG 1      ///< VUI flag to indicate tile boundary alignment
 #define M0463_VUI_EXT_ILP_REF            1      ///< VUI extension inter-layer dependency offset signalling
Index: branches/SHM-3.0-dev/source/Lib/TLibEncoder/TEncTop.cpp
===================================================================
--- branches/SHM-3.0-dev/source/Lib/TLibEncoder/TEncTop.cpp	(revision 333)
+++ branches/SHM-3.0-dev/source/Lib/TLibEncoder/TEncTop.cpp	(revision 334)
@@ -886,5 +886,9 @@
   for( Int i = 0; i < getGOPSize()+m_extraRPSs; i++) 
   {
+#if FINAL_RPL_CHANGE_N0082
+    GOPEntry ge = m_ppcTEncTop[m_cSPS.getLayerId()]->getGOPEntry(i);
+#else
     GOPEntry ge = getGOPEntry(i);
+#endif
     rps = rpsList->getReferencePictureSet(i);
     rps->setNumberOfPictures(ge.m_numRefPics);
