Index: trunk/source/App/TAppEncoder/TAppEncCfg.cpp
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 345)
+++ trunk/source/App/TAppEncoder/TAppEncCfg.cpp	(revision 442)
@@ -69,10 +69,11 @@
 , m_avcBaseLayerFlag(0)
 #endif
+#if N0120_MAX_TID_REF_CFG
+, m_maxTidRefPresentFlag(1)
+#endif 
 , m_pColumnWidth()
 , m_pRowHeight()
 , m_scalingListFile()
-#if REF_IDX_FRAMEWORK
 , m_elRapSliceBEnabled(0)
-#endif
 {
   for(UInt layer=0; layer<MAX_LAYERS; layer++)
@@ -217,4 +218,16 @@
     {
       in>>entry.m_refIdc[i];
+    }
+  }
+#endif
+#if EXTERNAL_USEDBYCURR_N0082
+  if(entry.m_numRefPics>0){
+    in>>entry.m_UseExtusedByCurrPic;
+    if(entry.m_UseExtusedByCurrPic)
+    {
+      for ( Int i = 0; i < entry.m_numRefPics; i++ )
+      {
+        in>>entry.m_ExtusedByCurrPic[i];
+      }
     }
   }
@@ -335,10 +348,13 @@
   
 #if SVC_EXTENSION
-  string  cfg_LayerCfgFile  [MAX_LAYERS];
+  string  cfg_LayerCfgFile   [MAX_LAYERS];
   string  cfg_BitstreamFile;
-  string* cfg_InputFile     [MAX_LAYERS];
-  string* cfg_ReconFile     [MAX_LAYERS];
-  Double* cfg_fQP           [MAX_LAYERS];
-
+  string* cfg_InputFile      [MAX_LAYERS];
+  string* cfg_ReconFile      [MAX_LAYERS];
+  Double* cfg_fQP            [MAX_LAYERS];
+
+#if REPN_FORMAT_IN_VPS
+  Int*    cfg_repFormatIdx  [MAX_LAYERS];
+#endif
   Int*    cfg_SourceWidth   [MAX_LAYERS]; 
   Int*    cfg_SourceHeight  [MAX_LAYERS];
@@ -384,4 +400,7 @@
   Bool*   cfg_RCForceIntraQP       [MAX_LAYERS];
 #endif
+#if N0120_MAX_TID_REF_CFG
+  Int*    cfg_maxTidIlRefPicsPlus1[MAX_LAYERS]; 
+#endif 
   for(UInt layer = 0; layer < MAX_LAYERS; layer++)
   {
@@ -389,4 +408,7 @@
     cfg_ReconFile[layer]    = &m_acLayerCfg[layer].m_cReconFile;
     cfg_fQP[layer]          = &m_acLayerCfg[layer].m_fQP;
+#if REPN_FORMAT_IN_VPS
+    cfg_repFormatIdx[layer] = &m_acLayerCfg[layer].m_repFormatIdx;
+#endif
     cfg_SourceWidth[layer]  = &m_acLayerCfg[layer].m_iSourceWidth;
     cfg_SourceHeight[layer] = &m_acLayerCfg[layer].m_iSourceHeight;
@@ -426,4 +448,7 @@
     cfg_RCForceIntraQP[layer]        = &m_acLayerCfg[layer].m_RCForceIntraQP;
 #endif
+#if N0120_MAX_TID_REF_CFG
+    cfg_maxTidIlRefPicsPlus1[layer] = &m_acLayerCfg[layer].m_maxTidIlRefPicsPlus1; 
+#endif 
   }
 #if AVC_BASE
@@ -432,4 +457,7 @@
 #if AVC_SYNTAX
   string  cfg_BLSyntaxFile;
+#endif
+#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
+  string  cfg_tileSets;
 #endif
 #else
@@ -459,4 +487,7 @@
   ("FrameRate%d,-fr%d",       cfg_FrameRate,  0, MAX_LAYERS, "Frame rate for layer %d")
   ("LambdaModifier%d,-LM%d",  m_adLambdaModifier, ( double )1.0, MAX_TLAYER, "Lambda modifier for temporal layer %d")
+#if REPN_FORMAT_IN_VPS
+  ("RepFormatIdx%d",          cfg_repFormatIdx, -1, MAX_LAYERS, "Index to the representation format structure used from the VPS")
+#endif
 #if VPS_EXTN_DIRECT_REF_LAYERS
 #if M0457_PREDICTION_INDICATIONS
@@ -474,6 +505,11 @@
   ("NumLayers",               m_numLayers, 1, "Number of layers to code")
   ("ConformanceMode%d",       cfg_conformanceMode,0, MAX_LAYERS, "Window conformance mode (0: no cropping, 1:automatic padding, 2: padding, 3:cropping")
+#if SCALABILITY_MASK_E0104
+  ("ScalabilityMask1",        m_scalabilityMask[1], 0, "scalability_mask[1] (multiview)")
+  ("ScalabilityMask2",        m_scalabilityMask[2], 1, "scalability_mask[2] (scalable)" )
+#else
   ("ScalabilityMask0",        m_scalabilityMask[0], 0, "scalability_mask[0] (multiview)")
   ("ScalabilityMask1",        m_scalabilityMask[1], 1, "scalability_mask[1] (scalable)" )
+#endif
   ("BitstreamFile,b",         cfg_BitstreamFile, string(""), "Bitstream output file name")
   ("InputBitDepth",           m_inputBitDepthY,    8, "Bit-depth of input file")
@@ -495,4 +531,8 @@
                                                                  " bottom-right luma sample of the EL picture, in units of two luma samples")
 #endif
+#if N0120_MAX_TID_REF_CFG
+  ("MaxTidRefPresentFlag", m_maxTidRefPresentFlag, true, "max_tid_ref_present_flag (0: not present, 1: present(default)) " )
+  ("MaxTidIlRefPicsPlus1%d", cfg_maxTidIlRefPicsPlus1, 1, MAX_LAYERS, "allowed maximum temporal_id for inter-layer prediction")
+#endif 
 #if AVC_BASE
   ("AvcBase,-avc",            m_avcBaseLayerFlag,     0, "avc_base_layer_flag")
@@ -502,11 +542,9 @@
 #endif
 #endif
-#if REF_IDX_FRAMEWORK
   ("EnableElRapB,-use-rap-b",  m_elRapSliceBEnabled, 0, "Set ILP over base-layer I picture to B picture (default is P picture)")
-#endif  
 #if M0457_IL_SAMPLE_PRED_ONLY_FLAG
   ("IlSampleOnlyPred%d",       m_ilSampleOnlyPred, 0, MAX_LAYERS, "Set inter_layer_sample_pred_only_flag for all slices")
 #endif
-#else  
+#else
   ("InputFile,i",           cfg_InputFile,     string(""), "Original YUV input file name")
   ("BitstreamFile,b",       cfg_BitstreamFile, string(""), "Bitstream output file name")
@@ -530,4 +568,8 @@
   ("FrameRate,-fr",         m_iFrameRate,          0, "Frame rate")
 #endif
+
+  //Field coding parameters
+  ("FieldCoding", m_isField, false, "Signals if it's a field based coding")
+  ("TopFieldFirst, Tff", m_isTopFieldFirst, false, "In case of field based coding, signals whether if it's a top field first or not")
   ("FrameSkip,-fs",         m_FrameSkip,          0u, "Number of frames to skip at start of input YUV")
   ("FramesToBeEncoded,f",   m_framesToBeEncoded,   0, "Number of frames to be encoded (default=all)")
@@ -810,4 +852,9 @@
   ("AdaptiveResolutionChange",     m_adaptiveResolutionChange, 0, "Adaptive resolution change frame number. Should coincide with EL RAP picture. (0: disable)")
 #endif
+#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
+  ("SEIInterLayerConstrainedTileSets", m_interLayerConstrainedTileSetsSEIEnabled, false, "Control generation of inter layer constrained tile sets SEI message")
+  ("IlNumSetsInMessage",               m_ilNumSetsInMessage,                         0u, "Number of inter layer constrained tile sets")
+  ("TileSetsArray",                    cfg_tileSets,                         string(""), "Array containing tile sets params (TopLeftTileIndex, BottonRightTileIndex and ilcIdc for each set) ")
+#endif
   ;
   
@@ -827,4 +874,24 @@
   const list<const Char*>& argv_unhandled = po::scanArgv(opts, argc, (const Char**) argv);
 
+  if(m_isField)
+  {
+#if SVC_EXTENSION
+    for(Int layer = 0; layer < MAX_LAYERS; layer++)
+    {
+      //Frame height
+      m_acLayerCfg[layer].m_iSourceHeightOrg = m_acLayerCfg[layer].m_iSourceHeight;
+      //Field height
+      m_acLayerCfg[layer].m_iSourceHeight = m_acLayerCfg[layer].m_iSourceHeight >> 1;
+    }
+#else
+    //Frame height
+    m_iSourceHeightOrg = m_iSourceHeight;
+    //Field height
+    m_iSourceHeight = m_iSourceHeight >> 1;
+#endif
+    //number of fields to encode
+    m_framesToBeEncoded *= 2;
+  }
+  
   for (list<const Char*>::const_iterator it = argv_unhandled.begin(); it != argv_unhandled.end(); it++)
   {
@@ -1119,5 +1186,12 @@
 #endif
   m_scalingListFile = cfg_ScalingListFile.empty() ? NULL : strdup(cfg_ScalingListFile.c_str());
-  
+
+#if REPN_FORMAT_IN_VPS_123
+  for(Int layer = 0; layer < MAX_LAYERS; layer++)
+  {
+    m_acLayerCfg[layer].setSourceHeight( m_repFormatCfg[ m_acLayerCfg[layer].getRepFormatIdx() ].m_picHeightInLumaSamples);
+    m_acLayerCfg[layer].setSourceWidth ( m_repFormatCfg[ m_acLayerCfg[layer].getRepFormatIdx() ].m_picWidthInLumaSamples );
+  }
+#endif
   /* rules for input, output and internal bitdepths as per help text */
   if (!m_internalBitDepthY) { m_internalBitDepthY = m_inputBitDepthY; }
@@ -1151,4 +1225,9 @@
         m_aiPad[1] = m_confBottom = ((m_iSourceHeight / minCuSize) + 1) * minCuSize - m_iSourceHeight;
         m_iSourceHeight += m_confBottom;
+        if ( m_isField )
+        {
+          m_iSourceHeightOrg += m_confBottom << 1;
+          m_aiPad[1] = m_confBottom << 1;
+        }
       }
       if (m_aiPad[0] % TComSPS::getWinUnitX(CHROMA_420) != 0)
@@ -1284,4 +1363,48 @@
     }
   }
+#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
+  if (m_interLayerConstrainedTileSetsSEIEnabled)
+  {
+    if (m_iNumColumnsMinus1 == 0 && m_iNumRowsMinus1 == 0)
+    {
+      printf( "Tiles are not defined (needed for inter-layer comnstrained tile sets SEI).\n" );
+      exit( EXIT_FAILURE );
+    }
+    Char* pTileSets = cfg_tileSets.empty() ? NULL : strdup(cfg_tileSets.c_str());
+    int i = 0;
+    char *topLeftTileIndex = strtok(pTileSets, " ,");
+    while(topLeftTileIndex != NULL)
+    {
+      if( i >= m_ilNumSetsInMessage )
+      {
+        printf( "The number of tile sets is larger than defined by IlNumSetsInMessage.\n" );
+        exit( EXIT_FAILURE );
+      }
+      *( m_topLeftTileIndex + i ) = atoi( topLeftTileIndex );
+      char *bottonRightTileIndex = strtok(NULL, " ,");
+      if( bottonRightTileIndex == NULL )
+      {
+        printf( "BottonRightTileIndex is missing in the tile sets.\n" );
+        exit( EXIT_FAILURE );
+      }
+      *( m_bottomRightTileIndex + i ) = atoi( bottonRightTileIndex );
+      char *ilcIdc = strtok(NULL, " ,");
+      if( ilcIdc == NULL )
+      {
+        printf( "IlcIdc is missing in the tile sets.\n" );
+        exit( EXIT_FAILURE );
+      }
+      *( m_ilcIdc + i ) = atoi( ilcIdc );
+      topLeftTileIndex = strtok(NULL, " ,");
+      i++;
+    }
+    if( i < m_ilNumSetsInMessage )
+    {
+      printf( "The number of tile sets is smaller than defined by IlNumSetsInMessage.\n" );
+      exit( EXIT_FAILURE );
+    }
+    m_skippedTileSetPresentFlag = false;
+  }
+#endif
   // check validity of input parameters
   xCheckParameter();
@@ -1467,14 +1590,18 @@
 #endif
   
-#if !FINAL_RPL_CHANGE_N0082
+#if EXTERNAL_USEDBYCURR_N0082|| !FINAL_RPL_CHANGE_N0082
   Bool verifiedGOP=false;
 #endif
   Bool errorGOP=false;
-#if !FINAL_RPL_CHANGE_N0082
+#if  EXTERNAL_USEDBYCURR_N0082|| !FINAL_RPL_CHANGE_N0082
   Int checkGOP=1;
-  Int numRefs = 1;
+  Int numRefs = m_isField ? 2 : 1;
 #endif
   Int refList[MAX_NUM_REF_PICS+1];
   refList[0]=0;
+  if(m_isField)
+  {
+    refList[1] = 1;
+  }
   Bool isOK[MAX_GOP];
   for(Int i=0; i<MAX_GOP; i++) 
@@ -1482,5 +1609,5 @@
     isOK[i]=false;
   }
-#if !FINAL_RPL_CHANGE_N0082
+#if  EXTERNAL_USEDBYCURR_N0082|| !FINAL_RPL_CHANGE_N0082
   Int numOK=0;
 #endif
@@ -1504,5 +1631,5 @@
   for(UInt layer=0; layer<m_numLayers; layer++)
   {
-    if(m_acLayerCfg[layer].xCheckParameter())
+    if(m_acLayerCfg[layer].xCheckParameter(m_isField))
     {
       printf("\nError: invalid configuration parameter found in layer %d \n", layer);
@@ -1530,5 +1657,6 @@
   for(UInt layer=0; layer<m_numLayers; layer++)
   {
-    if (m_acLayerCfg[layer].m_GOPListLayer[0].m_POC<0){
+    if (m_acLayerCfg[layer].m_GOPListLayer[0].m_POC<0)
+    {
       memcpy( m_acLayerCfg[layer].m_GOPListLayer, m_GOPList, sizeof(GOPEntry)*MAX_GOP );
     }
@@ -1536,6 +1664,12 @@
     xConfirmPara(errorGOP,"Invalid GOP structure given");
   }
+#if TEMP_SCALABILITY_FIX
+  if( m_acLayerCfg[1].m_GOPListLayer[5].m_POC == 6  && m_acLayerCfg[1].m_GOPListLayer[7].m_POC == 7  && 
+    m_acLayerCfg[1].m_GOPListLayer[5].m_temporalId == 0 && m_acLayerCfg[1].m_GOPListLayer[7].m_temporalId == 0)
+#else
   //tentative for encoder
-  if( m_acLayerCfg[1].m_GOPListLayer[5].m_POC == 6  && m_acLayerCfg[1].m_GOPListLayer[7].m_POC == 7 ){
+  if( m_acLayerCfg[1].m_GOPListLayer[5].m_POC == 6  && m_acLayerCfg[1].m_GOPListLayer[7].m_POC == 7 )
+#endif
+  {
     //RA, POC5
     m_acLayerCfg[1].m_GOPListLayer[5].m_usedByCurrPic[2] = 0;
@@ -1545,5 +1679,6 @@
     m_acLayerCfg[1].m_GOPListLayer[7].m_refIdc[2] = 0;
   }
-#else
+#endif
+#if  EXTERNAL_USEDBYCURR_N0082|| !FINAL_RPL_CHANGE_N0082
   m_extraRPSs=0;
   //start looping through frames in coding order until we can verify that the GOP structure is correct.
@@ -1742,4 +1877,19 @@
   xConfirmPara(errorGOP,"Invalid GOP structure given");
 #endif
+#if EXTERNAL_USEDBYCURR_N0082
+  for(UInt layer=0; layer<m_numLayers; layer++)
+  {
+    for (Int i=0; i< m_iGOPSize; i++){
+      if (m_acLayerCfg[layer].m_GOPListLayer[i].m_UseExtusedByCurrPic == 1 )
+      {
+        for(Int j=0; j<m_acLayerCfg[layer].m_GOPListLayer[i].m_numRefPics; j++ )
+        {
+          m_acLayerCfg[layer].m_GOPListLayer[i].m_usedByCurrPic[j] = m_acLayerCfg[layer].m_GOPListLayer[i].m_ExtusedByCurrPic[j];
+          m_acLayerCfg[layer].m_GOPListLayer[i].m_refIdc[j] = m_acLayerCfg[layer].m_GOPListLayer[i].m_ExtusedByCurrPic[j];
+        }
+      }
+    }
+  }
+#endif
   m_maxTempLayer = 1;
   for(Int i=0; i<m_iGOPSize; i++) 
@@ -2045,4 +2195,10 @@
   }
 #endif
+#if N0120_MAX_TID_REF_CFG
+  for (UInt layer=0; layer < MAX_LAYERS-1; layer++)
+  {
+    xConfirmPara(m_acLayerCfg[layer].m_maxTidIlRefPicsPlus1 < 0 || m_acLayerCfg[layer].m_maxTidIlRefPicsPlus1 > 7, "MaxTidIlRefPicsPlus1 must be in range 0 to 7");
+  }
+#endif 
 #undef xConfirmPara
   if (check_failed)
@@ -2081,6 +2237,11 @@
 #if SVC_EXTENSION  
   printf("Total number of layers        : %d\n", m_numLayers       );
+#if SCALABILITY_MASK_E0104
+  printf("Multiview                     : %d\n", m_scalabilityMask[1] );
+  printf("Scalable                      : %d\n", m_scalabilityMask[2] );
+#else
   printf("Multiview                     : %d\n", m_scalabilityMask[0] );
   printf("Scalable                      : %d\n", m_scalabilityMask[1] );
+#endif
 #if M0040_ADAPTIVE_RESOLUTION_CHANGE
   printf("Adaptive Resolution Change    : %d\n", m_adaptiveResolutionChange );
@@ -2105,5 +2266,22 @@
   printf("Internal Format              : %dx%d %dHz\n", m_iSourceWidth, m_iSourceHeight, m_iFrameRate );
 #endif
-  printf("Frame index                  : %u - %d (%d frames)\n", m_FrameSkip, m_FrameSkip+m_framesToBeEncoded-1, m_framesToBeEncoded );
+  if (m_isField)
+  {
+    printf("Frame/Field          : Field based coding\n");
+    printf("Field index          : %u - %d (%d fields)\n", m_FrameSkip, m_FrameSkip+m_framesToBeEncoded-1, m_framesToBeEncoded );
+    if (m_isTopFieldFirst)
+    {
+      printf("Field Order            : Top field first\n");
+    }
+    else
+    {
+      printf("Field Order            : Bottom field first\n");
+    }
+  }
+  else
+  {
+    printf("Frame/Field                  : Frame based coding\n");
+    printf("Frame index                  : %u - %d (%d frames)\n", m_FrameSkip, m_FrameSkip+m_framesToBeEncoded-1, m_framesToBeEncoded );
+  }
   printf("CU size / depth              : %d / %d\n", m_uiMaxCUWidth, m_uiMaxCUDepth );
   printf("RQT trans. size (min / max)  : %d / %d\n", 1 << m_uiQuadtreeTULog2MinSize, 1 << m_uiQuadtreeTULog2MaxSize );
@@ -2209,11 +2387,8 @@
   printf("AvcBase:%d ", 0);
 #endif
-#if REF_IDX_FRAMEWORK
-  printf("REF_IDX_FRAMEWORK:%d ", REF_IDX_FRAMEWORK);
   printf("EL_RAP_SliceType: %d ", m_elRapSliceBEnabled);
   printf("REF_IDX_ME_ZEROMV: %d ", REF_IDX_ME_ZEROMV);
   printf("ENCODER_FAST_MODE: %d ", ENCODER_FAST_MODE);
   printf("REF_IDX_MFM: %d ", REF_IDX_MFM);
-#endif
 #else
   printf("RecalQP:%d", m_recalculateQPAccordingToLambda ? 1 : 0 );
Index: trunk/source/App/TAppEncoder/TAppEncCfg.h
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncCfg.h	(revision 345)
+++ trunk/source/App/TAppEncoder/TAppEncCfg.h	(revision 442)
@@ -59,5 +59,5 @@
   // file I/O
 #if SVC_EXTENSION
-  TAppEncLayerCfg m_acLayerCfg [MAX_LAYERS]; 
+  TAppEncLayerCfg m_acLayerCfg [MAX_LAYERS];
   Int       m_numLayers;                                      ///< number of layers
   Int       m_scalabilityMask[MAX_VPS_NUM_SCALABILITY_TYPES]; ///< scalability_mask
@@ -68,5 +68,5 @@
   Int       m_framesToBeEncoded;                              ///< number of encoded frames
 #if AVC_BASE
-  Int       m_avcBaseLayerFlag;                               ///< AVC_BASElayer_flag
+  Int       m_avcBaseLayerFlag;                               ///< avc_baselayer_flag
 #endif
 #if AVC_SYNTAX
@@ -76,4 +76,7 @@
   Int       m_ilSampleOnlyPred[ MAX_LAYERS ];
 #endif
+#if N0120_MAX_TID_REF_CFG
+  Bool      m_maxTidRefPresentFlag; 
+#endif 
 #else
   Char*     m_pchInputFile;                                   ///< source file name
@@ -85,5 +88,9 @@
   UInt      m_FrameSkip;                                      ///< number of skipped frames from the beginning
   Int       m_iSourceWidth;                                   ///< source width in pixel
-  Int       m_iSourceHeight;                                  ///< source height in pixel
+  Int       m_iSourceHeight;                                  ///< source height in pixel (when interlaced = field height)
+  
+  Int       m_iSourceHeightOrg;                               ///< original source height in pixel (when interlaced = frame height)
+  
+  
   Int       m_conformanceMode;
   Int       m_confLeft;
@@ -94,4 +101,7 @@
   Int       m_aiPad[2];                                       ///< number of padded pixels for width and height
 #endif  
+
+  Bool      m_isField;                                        ///< enable field coding
+  Bool      m_isTopFieldFirst;
 
   // profile/level
@@ -340,5 +350,5 @@
   Int       m_log2MaxMvLengthVertical;                        ///< Indicate the maximum absolute value of a decoded vertical MV component in quarter-pel luma units
 
-#if REF_IDX_FRAMEWORK
+#if SVC_EXTENSION
   Int       m_elRapSliceBEnabled;
 #endif
@@ -353,4 +363,15 @@
 #if M0040_ADAPTIVE_RESOLUTION_CHANGE
   Int       m_adaptiveResolutionChange;                       ///< Indicate adaptive resolution change frame
+#endif
+#if REPN_FORMAT_IN_VPS
+  RepFormatCfg m_repFormatCfg[16];                            ///< Rep_format structures
+#endif
+#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
+  Bool      m_interLayerConstrainedTileSetsSEIEnabled;
+  UInt      m_ilNumSetsInMessage;
+  Bool      m_skippedTileSetPresentFlag;
+  UInt      m_topLeftTileIndex[1024];
+  UInt      m_bottomRightTileIndex[1024];
+  UInt      m_ilcIdc[1024];
 #endif
 public:
@@ -381,4 +402,7 @@
   Void cfgStringToArray(Int **arr, string cfgString, Int numEntries, const char* logString);
 #endif
+#if REPN_FORMAT_IN_VPS
+  RepFormatCfg* getRepFormatCfg(Int i)  { return &m_repFormatCfg[i]; }
+#endif
 #endif
 };// END CLASS DEFINITION TAppEncCfg
Index: trunk/source/App/TAppEncoder/TAppEncLayerCfg.cpp
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncLayerCfg.cpp	(revision 345)
+++ trunk/source/App/TAppEncoder/TAppEncLayerCfg.cpp	(revision 442)
@@ -32,4 +32,7 @@
   m_conformanceMode( 0 ),
   m_aidQP(NULL)
+#if REPN_FORMAT_IN_VPS
+, m_repFormatIdx (-1)
+#endif
 {
   m_confLeft = m_confRight = m_confTop = m_confBottom = 0;
@@ -158,5 +161,5 @@
 Bool confirmPara(Bool bflag, const char* message);
 
-Bool TAppEncLayerCfg::xCheckParameter()
+Bool TAppEncLayerCfg::xCheckParameter( Bool isField )
 {
   switch (m_conformanceMode)
@@ -182,4 +185,9 @@
         m_aiPad[1] = m_confBottom = ((m_iSourceHeight / minCuSize) + 1) * minCuSize - m_iSourceHeight;
         m_iSourceHeight += m_confBottom;
+        if ( isField )
+        {
+          m_iSourceHeightOrg += m_confBottom << 1;
+          m_aiPad[1] = m_confBottom << 1;
+        }
       }
       break;
Index: trunk/source/App/TAppEncoder/TAppEncLayerCfg.h
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncLayerCfg.h	(revision 345)
+++ trunk/source/App/TAppEncoder/TAppEncLayerCfg.h	(revision 442)
@@ -33,5 +33,6 @@
   Int       m_iFrameRate;                                     ///< source frame-rates (Hz)
   Int       m_iSourceWidth;                                   ///< source width in pixel
-  Int       m_iSourceHeight;                                  ///< source height in pixel
+  Int       m_iSourceHeight;                                  ///< source height in pixel (when interlaced = field height)
+  Int       m_iSourceHeightOrg;                               ///< original source height in pixel (when interlaced = frame height)
   Int       m_conformanceMode;
   Int       m_confLeft;
@@ -66,4 +67,7 @@
 #endif
 
+#if N0120_MAX_TID_REF_CFG
+  Int       m_maxTidIlRefPicsPlus1;
+#endif 
 #if SVC_EXTENSION
   Int       m_iWaveFrontSubstreams; //< If iWaveFrontSynchro, this is the number of substreams per frame (dependent tiles) or per tile (independent tiles).
@@ -84,4 +88,7 @@
   GOPEntry  m_GOPListLayer[MAX_GOP];                            ///< for layer
 #endif
+#if REPN_FORMAT_IN_VPS
+  Int       m_repFormatIdx;
+#endif
 public:
   TAppEncLayerCfg();
@@ -98,5 +105,5 @@
   Void  xPrintParameter();
 #endif
-  Bool  xCheckParameter();
+  Bool  xCheckParameter( Bool isField );
 
   Void    setAppEncCfg(TAppEncCfg* p) {m_cAppEncCfg = p;          }
@@ -107,4 +114,5 @@
   Int     getSourceWidth()            {return m_iSourceWidth;     }
   Int     getSourceHeight()           {return m_iSourceHeight;    }
+  Int     getSourceHeightOrg()        {return m_iSourceHeightOrg; }
   Int     getConformanceMode()        { return m_conformanceMode; }
   Int*    getPad()                    {return m_aiPad;            }
@@ -147,4 +155,13 @@
   GOPEntry getGOPEntry(Int i )        {return m_GOPListLayer[i];  }
 #endif
+#if REPN_FORMAT_IN_VPS
+  Int     getRepFormatIdx()           { return m_repFormatIdx;  }
+  Void    setRepFormatIdx(Int x)      { m_repFormatIdx = x;     }
+  Void    setSourceWidth(Int x)       { m_iSourceWidth = x;     }
+  Void    setSourceHeight(Int x)      { m_iSourceHeight = x;    }
+#endif
+#if N0120_MAX_TID_REF_CFG
+  Int     getMaxTidIlRefPicsPlus1()   { return m_maxTidIlRefPicsPlus1; }
+#endif 
 }; // END CLASS DEFINITION TAppEncLayerCfg
 
Index: trunk/source/App/TAppEncoder/TAppEncTop.cpp
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncTop.cpp	(revision 345)
+++ trunk/source/App/TAppEncoder/TAppEncTop.cpp	(revision 442)
@@ -90,5 +90,65 @@
     vps->setMaxDecPicBuffering             ( m_maxDecPicBuffering[i], i );
   }
-
+#if REPN_FORMAT_IN_VPS
+  vps->setRepFormatIdxPresentFlag( true );   // Could be disabled to optimize in some cases.
+  Int maxRepFormatIdx = -1;
+  Int formatIdx = -1;
+  for(UInt layer=0; layer < m_numLayers; layer++)
+  {
+    // Auto generation of the format index
+    if( m_acLayerCfg[layer].getRepFormatIdx() == -1 )
+    {
+      // Currently only picture width and height are considred. It has to be updated if different chroma format and bit-depth will are used.
+      Bool found = false;
+      for( UInt idx = 0; idx < layer; idx++ )
+      {
+        if( m_acLayerCfg[layer].getSourceWidth() == m_acLayerCfg[idx].getSourceWidth() && m_acLayerCfg[layer].getSourceHeight() == m_acLayerCfg[idx].getSourceHeight() )
+        {
+          found = true;
+          break;
+        }
+      }
+      if( !found )
+      {
+        formatIdx++;
+      }
+
+      m_acLayerCfg[layer].setRepFormatIdx( formatIdx );
+    }
+
+    assert( m_acLayerCfg[layer].getRepFormatIdx() != -1 && "RepFormatIdx not assigned for a layer" );   
+    vps->setVpsRepFormatIdx( layer, m_acLayerCfg[layer].getRepFormatIdx() );
+    maxRepFormatIdx = std::max( m_acLayerCfg[layer].getRepFormatIdx(), maxRepFormatIdx );
+  }
+  assert( vps->getVpsRepFormatIdx( 0 ) == 0 );  // Base layer should point to the first one.
+  Int* mapIdxToLayer = new Int[maxRepFormatIdx + 1];
+  // Check that all the indices from 0 to maxRepFormatIdx are used in the VPS
+  for(Int i = 0; i <= maxRepFormatIdx; i++)
+  {
+    mapIdxToLayer[i] = -1;
+    UInt layer;
+    for(layer=0; layer < m_numLayers; layer++)
+    {
+      if( vps->getVpsRepFormatIdx(layer) == i )
+      {
+        mapIdxToLayer[i] = layer;
+        break;
+      }
+    }
+    assert( layer != m_numLayers );   // One of the VPS Rep format indices not set
+  }
+  vps->setVpsNumRepFormats( maxRepFormatIdx + 1 );
+  for(UInt idx=0; idx < vps->getVpsNumRepFormats(); idx++)
+  {
+    RepFormat *repFormat = vps->getVpsRepFormat( idx );    
+    repFormat->setPicWidthVpsInLumaSamples  ( m_acLayerCfg[mapIdxToLayer[idx]].getSourceWidth()   );
+    repFormat->setPicHeightVpsInLumaSamples ( m_acLayerCfg[mapIdxToLayer[idx]].getSourceHeight()  );
+    repFormat->setChromaFormatVpsIdc        ( 1                                             );  // Need modification to change for each layer - corresponds to 420
+    repFormat->setSeparateColourPlaneVpsFlag( 0                                             );  // Need modification to change for each layer
+    repFormat->setBitDepthVpsLuma           ( getInternalBitDepthY()                        );  // Need modification to change for each layer
+    repFormat->setBitDepthVpsChroma         ( getInternalBitDepthC()                        );  // Need modification to change for each layer
+  }
+  delete [] mapIdxToLayer;
+#endif
   for(UInt layer=0; layer<m_numLayers; layer++)
   {
@@ -157,4 +217,10 @@
     m_acTEncTop[layer].setMaxTempLayer                 ( m_maxTempLayer );
     m_acTEncTop[layer].setUseAMP( m_enableAMP );
+#if N0120_MAX_TID_REF_CFG
+    if (layer<m_numLayers-1)
+    {
+       m_acTEncTop[layer].setMaxTidIlRefPicsPlus1 ( m_acLayerCfg[layer].getMaxTidIlRefPicsPlus1()); 
+    }
+#endif 
 #if VPS_EXTN_DIRECT_REF_LAYERS
     if(layer)
@@ -195,5 +261,5 @@
         {
           m_acTEncTop[layer].setMotionPredRefLayerId   ( i, m_acLayerCfg[layer].getMotionPredRefLayerId(i));
-          m_acTEncTop[layer].setMotionPredEnabledFlag  (m_acLayerCfg[layer].getSamplePredRefLayerId(i), true);
+          m_acTEncTop[layer].setMotionPredEnabledFlag  (m_acLayerCfg[layer].getMotionPredRefLayerId(i), true);
         }        
       }
@@ -397,4 +463,12 @@
     m_acTEncTop[layer].setSOPDescriptionSEIEnabled( m_SOPDescriptionSEIEnabled );
     m_acTEncTop[layer].setScalableNestingSEIEnabled( m_scalableNestingSEIEnabled );
+#if N0383_IL_CONSTRAINED_TILE_SETS_SEI
+    m_acTEncTop[layer].setInterLayerConstrainedTileSetsSEIEnabled( m_interLayerConstrainedTileSetsSEIEnabled );
+    m_acTEncTop[layer].setIlNumSetsInMessage( m_ilNumSetsInMessage );
+    m_acTEncTop[layer].setSkippedTileSetPresentFlag( m_skippedTileSetPresentFlag );
+    m_acTEncTop[layer].setTopLeftTileIndex( m_topLeftTileIndex );
+    m_acTEncTop[layer].setBottomRightTileIndex( m_bottomRightTileIndex );
+    m_acTEncTop[layer].setIlcIdc( m_ilcIdc );
+#endif
     m_acTEncTop[layer].setUniformSpacingIdr          ( m_iUniformSpacingIdr );
     m_acTEncTop[layer].setNumColumnsMinus1           ( m_iNumColumnsMinus1 );
@@ -475,7 +549,5 @@
     m_acTEncTop[layer].setLog2MaxMvLengthHorizontal( m_log2MaxMvLengthHorizontal );
     m_acTEncTop[layer].setLog2MaxMvLengthVertical( m_log2MaxMvLengthVertical );
-#if REF_IDX_FRAMEWORK
     m_acTEncTop[layer].setElRapSliceTypeB(layer == 0? 0 : m_elRapSliceBEnabled);
-#endif
 #if SCALED_REF_LAYER_OFFSETS
     if( layer > 0 )
@@ -824,10 +896,10 @@
 }
 
-Void TAppEncTop::xInitLib()
+Void TAppEncTop::xInitLib(Bool isFieldCoding)
 {
 #if SVC_EXTENSION
   for(UInt layer=0; layer<m_numLayers; layer++)
   {
-    m_acTEncTop[layer].init();
+    m_acTEncTop[layer].init(isFieldCoding);
   }
 #if VPS_RENAME
@@ -889,10 +961,18 @@
 #endif
 #if N0120_MAX_TID_REF_PRESENT_FLAG
-  vps->setMaxTidIlRefPicsPlus1PresentFlag(true);
-  if (vps->getMaxTidIlRefPicsPlus1PresentFlag())
+#if N0120_MAX_TID_REF_CFG
+  vps->setMaxTidRefPresentFlag(m_maxTidRefPresentFlag);
+#else 
+  vps->setMaxTidRefPresentFlag(true);
+#endif 
+  if (vps->getMaxTidRefPresentFlag())
   {
     for( i = 0; i < MAX_VPS_LAYER_ID_PLUS1 - 1; i++ )
     {
-      vps->setMaxSublayerForIlpPlus1(i, vps->getMaxTLayers()+1);
+#if N0120_MAX_TID_REF_CFG
+      vps->setMaxTidIlRefPicsPlus1(i, m_acTEncTop[i].getMaxTidIlRefPicsPlus1()); 
+#else 
+      vps->setMaxTidIlRefPicsPlus1(i, vps->getMaxTLayers()+1);
+#endif 
     }
   }
@@ -901,5 +981,5 @@
     for( i = 0; i < MAX_VPS_LAYER_ID_PLUS1 - 1; i++ )
     {
-      vps->setMaxSublayerForIlpPlus1(i, 7);
+      vps->setMaxTidIlRefPicsPlus1(i, 7);
     }
   }
@@ -983,14 +1063,49 @@
 #endif
   }
+#if IL_SL_SIGNALLING_N0371
+  for(i = 1; i < vps->getMaxLayers(); i++)
+  {
+    for(Int j = 0; j < i; j++)
+    {
+      vps->setScalingListLayerDependency( i, j, vps->checkLayerDependency( i,j ) );
+    }
+  }
+#endif
 #endif
 #if JCTVC_M0458_INTERLAYER_RPS_SIG        
     vps->setMaxOneActiveRefLayerFlag(maxDirectRefLayers > 1 ? false : true); 
 #endif 
+#if N0147_IRAP_ALIGN_FLAG    
+    vps->setCrossLayerIrapAlignFlag(true); 
+    for(UInt layerCtr = 1;layerCtr <= vps->getMaxLayers() - 1; layerCtr++)
+    {
+      for(Int refLayerCtr = 0; refLayerCtr < layerCtr; refLayerCtr++)
+      {
+        if (vps->getDirectDependencyFlag( layerCtr, refLayerCtr))
+        {          
+          if(m_acTEncTop[layerCtr].getIntraPeriod() !=  m_acTEncTop[refLayerCtr].getIntraPeriod())
+          {
+            vps->setCrossLayerIrapAlignFlag(false);
+            break;
+          }
+        }
+      }
+    }
+
+    if( vps->getAvcBaseLayerFlag() )
+    {
+      vps->setCrossLayerIrapAlignFlag(false);
+    }
+#endif 
 #if M0040_ADAPTIVE_RESOLUTION_CHANGE
   vps->setSingleLayerForNonIrapFlag(m_adaptiveResolutionChange > 0 ? true : false);
 #endif
-#else
-  m_cTEncTop.init();
-#endif
+#if VPS_EXTN_OFFSET
+  // to be updated according to the current semantics
+  vps->setExtensionOffset( 0xffff );
+#endif
+#else //SVC_EXTENSION
+  m_cTEncTop.init(isFieldCoding);
+#endif //SVC_EXTENSION
 }
 
@@ -1023,5 +1138,5 @@
   xInitLibCfg();
   xCreateLib();
-  xInitLib();
+  xInitLib(m_isField);
 
   // main encoder loop
@@ -1035,9 +1150,20 @@
     // allocate original YUV buffer
     pcPicYuvOrg[layer] = new TComPicYuv;
+    if( m_isField )
+    {
 #if SVC_UPSAMPLING
-    pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
-#else
-    pcPicYuvOrg->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
-#endif
+      pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeightOrg(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
+#else
+      pcPicYuvOrg->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeightOrg(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
+#endif
+    }
+    else
+    {
+#if SVC_UPSAMPLING
+      pcPicYuvOrg[layer]->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth, NULL );
+#else
+      pcPicYuvOrg->create( m_acLayerCfg[layer].getSourceWidth(), m_acLayerCfg[layer].getSourceHeight(), m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
+#endif
+    }
   }
 
@@ -1081,8 +1207,15 @@
           m_iFrameRcvd++;
           // check end of file
-          bEos = (m_iFrameRcvd == m_framesToBeEncoded);
+          bEos = (m_isField && (m_iFrameRcvd == (m_framesToBeEncoded >> 1) )) || ( !m_isField && (m_iFrameRcvd == m_framesToBeEncoded) );
         }
 
-        m_acTEncTop[layer].encodePrep( pcPicYuvOrg[layer] );
+        if ( m_isField )
+        {
+          m_acTEncTop[layer].encodePrep( pcPicYuvOrg[layer], m_isTopFieldFirst );
+        }
+        else
+        {
+          m_acTEncTop[layer].encodePrep( pcPicYuvOrg[layer] );
+        }
       }
 
@@ -1131,5 +1264,12 @@
       {
         // call encoding function for one frame
-        m_acTEncTop[layer].encode( flush ? 0 : pcPicYuvOrg[layer], m_acListPicYuvRec[layer], outputAccessUnits, iPicIdInGOP );
+        if ( m_isField )
+        {
+          m_acTEncTop[layer].encode( flush ? 0 : pcPicYuvOrg[layer], m_acListPicYuvRec[layer], outputAccessUnits, iPicIdInGOP, m_isTopFieldFirst );
+        }
+        else
+        {
+          m_acTEncTop[layer].encode( flush ? 0 : pcPicYuvOrg[layer], m_acListPicYuvRec[layer], outputAccessUnits, iPicIdInGOP );
+        }
       }
     }
@@ -1168,5 +1308,5 @@
     if (bEos)
     {
-      printOutSummary();
+      printOutSummary(m_isTopFieldFirst);
     }
 
@@ -1200,5 +1340,5 @@
 }
 
-Void TAppEncTop::printOutSummary()
+Void TAppEncTop::printOutSummary(Bool isField)
 {
   UInt layer;
@@ -1207,8 +1347,18 @@
   for(layer = 0; layer < m_numLayers; layer++)
   {
-    m_gcAnalyzeAll[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate());
-    m_gcAnalyzeI[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() );
-    m_gcAnalyzeP[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() );
-    m_gcAnalyzeB[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() );
+    if(isField)
+    {
+      m_gcAnalyzeAll[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() * 2);
+      m_gcAnalyzeI[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() * 2 );
+      m_gcAnalyzeP[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() * 2 );
+      m_gcAnalyzeB[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() * 2 );
+    }
+    else
+    {
+      m_gcAnalyzeAll[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate());
+      m_gcAnalyzeI[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() );
+      m_gcAnalyzeP[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() );
+      m_gcAnalyzeB[layer].setFrmRate( m_acLayerCfg[layer].getFrameRate() );
+    }
   }
 
@@ -1241,4 +1391,19 @@
     m_gcAnalyzeB[layer].printOut('b', layer);
   }
+
+  if(isField)
+  {
+    for(layer = 0; layer < m_numLayers; layer++)
+    {
+      //-- interlaced summary
+      m_gcAnalyzeAll_in.setFrmRate( m_acLayerCfg[layer].getFrameRate());
+      printf( "\n\nSUMMARY INTERLACED ---------------------------------------------\n" );
+      m_gcAnalyzeAll_in.printOutInterlaced('a',  m_gcAnalyzeAll[layer].getBits());
+
+#if _SUMMARY_OUT_
+      m_gcAnalyzeAll_in.printSummaryOutInterlaced();
+#endif
+    }
+  }
 }
 
@@ -1259,5 +1424,5 @@
   xInitLibCfg();
   xCreateLib();
-  xInitLib();
+  xInitLib(m_isField);
 
   // main encoder loop
@@ -1268,5 +1433,12 @@
 
   // allocate original YUV buffer
-  pcPicYuvOrg->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
+  if( m_isField )
+  {
+    pcPicYuvOrg->create( m_iSourceWidth, m_iSourceHeightOrg, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
+  }
+  else
+  {
+    pcPicYuvOrg->create( m_iSourceWidth, m_iSourceHeight, m_uiMaxCUWidth, m_uiMaxCUHeight, m_uiMaxCUDepth );
+  }
 
   while ( !bEos )
@@ -1281,6 +1453,5 @@
     m_iFrameRcvd++;
 
-    bEos = (m_iFrameRcvd == m_framesToBeEncoded);
-
+    bEos = (m_isField && (m_iFrameRcvd == (m_framesToBeEncoded >> 1) )) || ( !m_isField && (m_iFrameRcvd == m_framesToBeEncoded) );
     Bool flush = 0;
     // if end of file (which is only detected on a read failure) flush the encoder of any queued pictures
@@ -1294,5 +1465,12 @@
 
     // call encoding function for one frame
+    if ( m_isField )
+    {
+      m_cTEncTop.encode( bEos, flush ? 0 : pcPicYuvOrg, m_cListPicYuvRec, outputAccessUnits, iNumEncoded, m_isTopFieldFirst);
+    }
+    else
+    {
     m_cTEncTop.encode( bEos, flush ? 0 : pcPicYuvOrg, m_cListPicYuvRec, outputAccessUnits, iNumEncoded );
+    }
 
     // write bistream to file if necessary
@@ -1304,5 +1482,5 @@
   }
 
-  m_cTEncTop.printSummary();
+  m_cTEncTop.printSummary(m_isField);
 
   // delete original YUV buffer
@@ -1378,24 +1556,53 @@
 Void TAppEncTop::xWriteRecon(UInt layer, Int iNumEncoded)
 {
-  Int i;
-
-  TComList<TComPicYuv*>::iterator iterPicYuvRec = m_acListPicYuvRec[layer].end();
-
-  for ( i = 0; i < iNumEncoded; i++ )
-  {
-    --iterPicYuvRec;
-  }
-
-  for ( i = 0; i < iNumEncoded; i++ )
-  {
-    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
+  if (m_isField)
+  {
+    //Reinterlace fields
+    Int i;
+    TComList<TComPicYuv*>::iterator iterPicYuvRec = m_acListPicYuvRec[layer].end();
+
+    for ( i = 0; i < iNumEncoded; i++ )
+    {
+      --iterPicYuvRec;
+    }
+
+    for ( i = 0; i < iNumEncoded/2; i++ )
+    {
+      TComPicYuv*  pcPicYuvRecTop  = *(iterPicYuvRec++);
+      TComPicYuv*  pcPicYuvRecBottom  = *(iterPicYuvRec++);
+
 #if M0040_ADAPTIVE_RESOLUTION_CHANGE
-    if (!m_acLayerCfg[layer].getReconFile().empty() && pcPicYuvRec->isReconstructed())
-#else
-    if (!m_acLayerCfg[layer].getReconFile().empty())
-#endif
-    {
-      m_acTVideoIOYuvReconFile[layer].write( pcPicYuvRec, m_acLayerCfg[layer].getConfLeft(), m_acLayerCfg[layer].getConfRight(), 
-        m_acLayerCfg[layer].getConfTop(), m_acLayerCfg[layer].getConfBottom() );
+      if (!m_acLayerCfg[layer].getReconFile().empty() && pcPicYuvRecTop->isReconstructed() && pcPicYuvRecBottom->isReconstructed())
+#else
+      if (!m_acLayerCfg[layer].getReconFile().empty())
+#endif
+      {
+        m_acTVideoIOYuvReconFile[layer].write( pcPicYuvRecTop, pcPicYuvRecBottom, m_acLayerCfg[layer].getConfLeft(), m_acLayerCfg[layer].getConfRight(), m_acLayerCfg[layer].getConfTop(), m_acLayerCfg[layer].getConfBottom(), m_isTopFieldFirst );
+      }
+    }
+  }
+  else
+  {
+    Int i;
+
+    TComList<TComPicYuv*>::iterator iterPicYuvRec = m_acListPicYuvRec[layer].end();
+
+    for ( i = 0; i < iNumEncoded; i++ )
+    {
+      --iterPicYuvRec;
+    }
+
+    for ( i = 0; i < iNumEncoded; i++ )
+    {
+      TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
+#if M0040_ADAPTIVE_RESOLUTION_CHANGE
+      if (!m_acLayerCfg[layer].getReconFile().empty() && pcPicYuvRec->isReconstructed())
+#else
+      if (!m_acLayerCfg[layer].getReconFile().empty())
+#endif
+      {
+        m_acTVideoIOYuvReconFile[layer].write( pcPicYuvRec, m_acLayerCfg[layer].getConfLeft(), m_acLayerCfg[layer].getConfRight(), 
+          m_acLayerCfg[layer].getConfTop(), m_acLayerCfg[layer].getConfBottom() );
+      }
     }
   }
@@ -1404,17 +1611,41 @@
 Void TAppEncTop::xWriteStream(std::ostream& bitstreamFile, Int iNumEncoded, const std::list<AccessUnit>& accessUnits)
 {
-  Int i;
-
-  list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
+  if (m_isField)
+  {
+    //Reinterlace fields
+    Int i;
+    list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
 
 #if M0040_ADAPTIVE_RESOLUTION_CHANGE
-  for ( i = 0; i < iNumEncoded && iterBitstream != accessUnits.end(); i++ )
-#else
-  for ( i = 0; i < iNumEncoded; i++ )
-#endif
-  {
-    const AccessUnit& au = *(iterBitstream++);
-    const vector<UInt>& stats = writeAnnexB(bitstreamFile, au);
-    rateStatsAccum(au, stats);
+    for ( i = 0; i < iNumEncoded/2 && iterBitstream != accessUnits.end(); i++ )
+#else
+    for ( i = 0; i < iNumEncoded/2; i++ )
+#endif
+    {      
+      const AccessUnit& auTop = *(iterBitstream++);
+      const vector<UInt>& statsTop = writeAnnexB(bitstreamFile, auTop);
+      rateStatsAccum(auTop, statsTop);
+
+      const AccessUnit& auBottom = *(iterBitstream++);
+      const vector<UInt>& statsBottom = writeAnnexB(bitstreamFile, auBottom);
+      rateStatsAccum(auBottom, statsBottom);
+    }
+  }
+  else
+  {
+    Int i;
+
+    list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
+
+#if M0040_ADAPTIVE_RESOLUTION_CHANGE
+    for ( i = 0; i < iNumEncoded && iterBitstream != accessUnits.end(); i++ )
+#else
+    for ( i = 0; i < iNumEncoded; i++ )
+#endif
+    {
+      const AccessUnit& au = *(iterBitstream++);
+      const vector<UInt>& stats = writeAnnexB(bitstreamFile, au);
+      rateStatsAccum(au, stats);
+    }
   }
 }
@@ -1460,29 +1691,61 @@
 Void TAppEncTop::xWriteOutput(std::ostream& bitstreamFile, Int iNumEncoded, const std::list<AccessUnit>& accessUnits)
 {
-  Int i;
-
-  TComList<TComPicYuv*>::iterator iterPicYuvRec = m_cListPicYuvRec.end();
-  list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
-
-  for ( i = 0; i < iNumEncoded; i++ )
-  {
-    --iterPicYuvRec;
-  }
-
-  for ( i = 0; i < iNumEncoded; i++ )
-  {
-    TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
-    if (m_pchReconFile)
-    {
-#if SYNTAX_OUTPUT && ILP_DECODED_PICTURE
-      m_cTVideoIOYuvReconFile.write( pcPicYuvRec );
-#else
-      m_cTVideoIOYuvReconFile.write( pcPicYuvRec, m_confLeft, m_confRight, m_confTop, m_confBottom );
-#endif
-    }
-
-    const AccessUnit& au = *(iterBitstream++);
-    const vector<UInt>& stats = writeAnnexB(bitstreamFile, au);
-    rateStatsAccum(au, stats);
+  if (m_isField)
+  {
+    //Reinterlace fields
+    Int i;
+    TComList<TComPicYuv*>::iterator iterPicYuvRec = m_cListPicYuvRec.end();
+    list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
+
+    for ( i = 0; i < iNumEncoded; i++ )
+    {
+      --iterPicYuvRec;
+    }
+
+    for ( i = 0; i < iNumEncoded/2; i++ )
+    {
+      TComPicYuv*  pcPicYuvRecTop  = *(iterPicYuvRec++);
+      TComPicYuv*  pcPicYuvRecBottom  = *(iterPicYuvRec++);
+
+      if (m_pchReconFile)
+      {
+        m_cTVideoIOYuvReconFile.write( pcPicYuvRecTop, pcPicYuvRecBottom, m_confLeft, m_confRight, m_confTop, m_confBottom, m_isTopFieldFirst );
+      }
+
+      const AccessUnit& auTop = *(iterBitstream++);
+      const vector<UInt>& statsTop = writeAnnexB(bitstreamFile, auTop);
+      rateStatsAccum(auTop, statsTop);
+
+      const AccessUnit& auBottom = *(iterBitstream++);
+      const vector<UInt>& statsBottom = writeAnnexB(bitstreamFile, auBottom);
+      rateStatsAccum(auBottom, statsBottom);
+    }
+  }
+  else
+  {
+    Int i;
+
+    TComList<TComPicYuv*>::iterator iterPicYuvRec = m_cListPicYuvRec.end();
+    list<AccessUnit>::const_iterator iterBitstream = accessUnits.begin();
+
+    for ( i = 0; i < iNumEncoded; i++ )
+    {
+      --iterPicYuvRec;
+    }
+
+    for ( i = 0; i < iNumEncoded; i++ )
+    {
+      TComPicYuv*  pcPicYuvRec  = *(iterPicYuvRec++);
+      if (m_pchReconFile)
+      {
+#if SYNTAX_OUTPUT
+        m_cTVideoIOYuvReconFile.write( pcPicYuvRec, m_confLeft, m_confRight, m_confTop, m_confBottom );
+#endif
+      }
+
+      const AccessUnit& au = *(iterBitstream++);
+      const vector<UInt>& stats = writeAnnexB(bitstreamFile, au);
+      rateStatsAccum(au, stats);
+    }
   }
 }
Index: trunk/source/App/TAppEncoder/TAppEncTop.h
===================================================================
--- trunk/source/App/TAppEncoder/TAppEncTop.h	(revision 345)
+++ trunk/source/App/TAppEncoder/TAppEncTop.h	(revision 442)
@@ -82,5 +82,5 @@
   Void  xCreateLib        ();                               ///< create files & encoder class
   Void  xInitLibCfg       ();                               ///< initialize internal variables
-  Void  xInitLib          ();                               ///< initialize encoder class
+  Void  xInitLib          (Bool isFieldCoding);             ///< initialize encoder class
   Void  xDestroyLib       ();                               ///< destroy encoder class
   
@@ -99,5 +99,5 @@
   Void xWriteRecon(UInt layer, Int iNumEncoded);
   Void xWriteStream(std::ostream& bitstreamFile, Int iNumEncoded, const std::list<AccessUnit>& accessUnits);
-  Void printOutSummary();
+  Void printOutSummary(Bool isField);
 #else
   Void xWriteOutput(std::ostream& bitstreamFile, Int iNumEncoded, const std::list<AccessUnit>& accessUnits); ///< write bitstream to file
