Index: branches/SHM-dev/source/App/TAppDownConvert/DownConvert.cpp
===================================================================
--- branches/SHM-dev/source/App/TAppDownConvert/DownConvert.cpp	(revision 1572)
+++ branches/SHM-dev/source/App/TAppDownConvert/DownConvert.cpp	(revision 1587)
@@ -92,7 +92,7 @@
 
 void
-DownConvert::downsamplingSVC( unsigned char*    pucBufferY,   int   iStrideY,
-                              unsigned char*    pucBufferU,   int   iStrideU,
-                              unsigned char*    pucBufferV,   int   iStrideV,
+DownConvert::downsamplingSVC( short*    pucBufferY,   int   iStrideY,
+                              short*    pucBufferU,   int   iStrideU,
+                              short*    pucBufferV,   int   iStrideV,
                               ResizeParameters* pcParameters, bool  bBotCoincided )
 {
@@ -130,6 +130,6 @@
   if( !bTopAndBottomResampling )
   {
-    unsigned char* pSrc = pucBufferY + iStrideY * iCurrBot;
-    unsigned char* pDes = pucBufferY + iStrideY * iBaseBot;
+    short* pSrc = pucBufferY + iStrideY * iCurrBot;
+    short* pDes = pucBufferY + iStrideY * iBaseBot;
     xCopyToImageBuffer  ( pSrc,         iCurrW, iCurrH >> iCurrField, iStrideY << iCurrField );
     xCompDownsampling   ( pcParameters, false,  bBotFieldFlag,        bVerticalDownsampling  );
@@ -148,6 +148,6 @@
   {
     //===== U =====
-    unsigned char* pSrc = pucBufferU + iStrideU * iCurrBot;
-    unsigned char* pDes = pucBufferU + iStrideU * iBaseBot;
+    short* pSrc = pucBufferU + iStrideU * iCurrBot;
+    short* pDes = pucBufferU + iStrideU * iBaseBot;
     xCopyToImageBuffer  ( pSrc,         iCurrW, iCurrH >> iCurrField, iStrideU << iCurrField );
     xCompDownsampling   ( pcParameters, true,   bBotFieldFlag,        bVerticalDownsampling  );
@@ -195,5 +195,5 @@
 
 void
-DownConvert::xCopyToImageBuffer( unsigned char* pucSrc, int iWidth, int iHeight, int iStride )
+DownConvert::xCopyToImageBuffer( short* pucSrc, int iWidth, int iHeight, int iStride )
 {
   int* piDes = m_paiImageBuffer;
@@ -210,5 +210,5 @@
 
 void
-DownConvert::xCopyFromImageBuffer( unsigned char* pucDes, int iWidth, int iHeight, int iStride )
+DownConvert::xCopyFromImageBuffer( short* pucDes, int iWidth, int iHeight, int iStride )
 {
   int* piSrc = m_paiImageBuffer;
@@ -217,5 +217,5 @@
     for( int i = 0; i < iWidth;  i++ )
     {
-      pucDes[i] = (unsigned char)piSrc[i];
+      pucDes[i] = (short)piSrc[i];
     }
     pucDes  += iStride;
@@ -262,4 +262,8 @@
   int   iScaleX     = ( ( (unsigned int)iScaledW << iShiftX ) + ( iRefW >> 1 ) ) / iRefW;
   int   iScaleY     = ( ( (unsigned int)iScaledH << iShiftY ) + ( iRefH >> 1 ) ) / iRefH;
+
+  int   inputBitDepth = pcParameters->inputBitDepth;
+  int   outputBitDepth = pcParameters->outputBitDepth;
+
   if( ! pcParameters->m_bFrameMbsOnlyFlag || ! pcParameters->m_bRefLayerFrameMbsOnlyFlag )
   {
@@ -304,5 +308,5 @@
   if( bVerticalDownsampling )
   {
-    xVertDownsampling( iCurrW, iCurrH, bBotFlag );
+    xVertDownsampling( iCurrW, iCurrH, bBotFlag, inputBitDepth, outputBitDepth);
   }
 
@@ -311,5 +315,6 @@
                       iLOffset, iTOffset, iROffset, iBOffset,
                       iShiftX,  iShiftY,  iScaleX,  iScaleY,
-                      iAddX,    iAddY,    iDeltaX,  iDeltaY );
+                      iAddX,    iAddY,    iDeltaX,  iDeltaY,
+                      inputBitDepth, outputBitDepth);
 }
 
@@ -317,5 +322,5 @@
 DownConvert::xVertDownsampling( int   iBaseW,
                                 int   iBaseH,
-                                bool  bBotFlag )
+                                bool  bBotFlag, int inputBitDepth, int outputBitDepth)
 {
   int aiVertFilter[13]  = { 2, 0, -4, -3, 5, 19, 26, 19, 5, -3, -4, 0, 2 };
@@ -323,4 +328,7 @@
   int iCurrW            = iBaseW;
   int iCurrH            = iBaseH << 1;
+
+  int shift = 6 + (inputBitDepth - outputBitDepth);
+  int shift_round = (1 << (shift -1));
 
   //===== vertical downsampling =====
@@ -336,11 +344,11 @@
         m_paiTmp1dBuffer[i] += aiVertFilter[k] * piSrc[m*m_iImageStride];
       }
-      m_paiTmp1dBuffer[i] = ( m_paiTmp1dBuffer[i] + 32 ) >> 6;
+      m_paiTmp1dBuffer[i] = ( m_paiTmp1dBuffer[i] + shift_round ) >> shift;
     }
     //--- clip and copy back to image buffer ---
-    for( int n = 0; n < iBaseH; n++ )
-    {
-      piSrc[n*m_iImageStride] = xClip( m_paiTmp1dBuffer[n], 0, 255 );
-    }
+      for( int n = 0; n < iBaseH; n++ )
+      {
+        piSrc[n*m_iImageStride] = xClip( m_paiTmp1dBuffer[n], 0, ((1<<outputBitDepth)-1) );
+      }
   }
 }
@@ -350,5 +358,5 @@
                                  int iLOffset, int iTOffset, int iROffset, int iBOffset,
                                  int iShiftX,  int iShiftY,  int iScaleX,  int iScaleY,
-                                 int iAddX,    int iAddY,    int iDeltaX,  int iDeltaY )
+                                 int iAddX,    int iAddY,    int iDeltaX,  int iDeltaY, int inputBitDepth, int outputBitDepth )
 {
   const int filter16[8][16][12] =
@@ -527,4 +535,7 @@
   int iShiftYM4 = iShiftY - 4;
 
+  int shift = 14 + (inputBitDepth - outputBitDepth);
+  int shift_round = (1 << (shift -1));
+
   //===== horizontal downsampling =====
   {
@@ -567,16 +578,16 @@
           m_paiTmp1dBuffer[j] += filter16[iVerFilter][iPhase][k] * piSrc[m*m_iImageStride];
         }
-        m_paiTmp1dBuffer[j] = ( m_paiTmp1dBuffer[j] + 8192 ) >> 14;
+        m_paiTmp1dBuffer[j] = ( m_paiTmp1dBuffer[j] + shift_round ) >> shift;
       }
       //--- clip and copy back to image buffer ---
-      for( int n = 0; n < iBaseH; n++ )
-      {
-        piSrc[n*m_iImageStride] = xClip( m_paiTmp1dBuffer[n], 0, 255 );
-      }
-    }
-  }
-}
-
-
-
-
+        for( int n = 0; n < iBaseH; n++ )
+        {
+          piSrc[n*m_iImageStride] = xClip( m_paiTmp1dBuffer[n], 0, ((1<<outputBitDepth)-1));
+        }
+    }
+  }
+}
+
+
+
+
Index: branches/SHM-dev/source/App/TAppDownConvert/DownConvert.h
===================================================================
--- branches/SHM-dev/source/App/TAppDownConvert/DownConvert.h	(revision 1572)
+++ branches/SHM-dev/source/App/TAppDownConvert/DownConvert.h	(revision 1587)
@@ -94,4 +94,6 @@
     , m_iRefLayerWidthInSamples     ( 0 )
     , m_iRefLayerHeightInSamples    ( 0 )
+    , inputBitDepth     ( 8 )
+    , outputBitDepth    ( 8 )
   { 
   };
@@ -129,4 +131,8 @@
   Int   m_iRefLayerWidthInSamples;
   Int   m_iRefLayerHeightInSamples;
+
+  //===== parameters for bit depth =====
+  int inputBitDepth;
+  int outputBitDepth;
 };
 
@@ -146,7 +152,7 @@
   // main functions for DownConvert Tool
   //=====================================
-  void  downsamplingSVC             ( unsigned char*          pucBufferY,    int   iStrideY,
-                                      unsigned char*          pucBufferU,    int   iStrideU,
-                                      unsigned char*          pucBufferV,    int   iStrideV,
+  void  downsamplingSVC             ( short*          pucBufferY,    int   iStrideY,
+                                      short*          pucBufferU,    int   iStrideU,
+                                      short*          pucBufferV,    int   iStrideV,
                                       ResizeParameters*       pcParameters,  bool  bBotCoincided = false );
 
@@ -164,9 +170,9 @@
   //=======================================
   //--- place to and get from image buffer ---
-  void  xCopyToImageBuffer          ( unsigned char*        pucSrc,
+  void  xCopyToImageBuffer          ( short*        pucSrc,
                                       int                   iWidth,
                                       int                   iHeight,
                                       int                   iStride );
-  void  xCopyFromImageBuffer        ( unsigned char*        pucDes,
+  void  xCopyFromImageBuffer        ( short*        pucDes,
                                       int                   iWidth,
                                       int                   iHeight,
@@ -179,9 +185,12 @@
   void  xVertDownsampling           ( int                   iBaseW,
                                       int                   iBaseH,
-                                      bool                  bBotFlag );
+                                      bool                  bBotFlag,
+                                      int                   inputBitDepth, 
+                                      int                   outputBitDepth);
   void  xBasicDownsampling          ( int  iBaseW,   int  iBaseH,   int  iCurrW,   int  iCurrH,
                                       int  iLOffset, int  iTOffset, int  iROffset, int  iBOffset,
                                       int  iShiftX,  int  iShiftY,  int  iScaleX,  int  iScaleY,
-                                      int  iAddX,    int  iAddY,    int  iDeltaX,  int  iDeltaY );
+                                      int  iAddX,    int  iAddY,    int  iDeltaX,  int  iDeltaY,
+                                      int inputBitDepth, int outputBitDepth);
 private:
   //===== member variables =====
Index: branches/SHM-dev/source/App/TAppDownConvert/TAppDownConvert.cpp
===================================================================
--- branches/SHM-dev/source/App/TAppDownConvert/TAppDownConvert.cpp	(revision 1572)
+++ branches/SHM-dev/source/App/TAppDownConvert/TAppDownConvert.cpp	(revision 1587)
@@ -48,10 +48,12 @@
   int            stride;
   int            lines;
-  unsigned char* data;
-  unsigned char* data2;
+  short* data;
+  short* data2;
 } ColorComponent;
 
 typedef struct
 {
+  int inputBitDepth;
+  int outputBitDepth;
   ColorComponent y;
   ColorComponent u;
@@ -68,6 +70,6 @@
   c.stride  = maxwidth;
   c.lines   = maxheight;
-  c.data    = new unsigned char [ size ];
-  c.data2   = new unsigned char [ size ];
+  c.data    = new short [ size ];
+  c.data2   = new short [ size ];
 
   if( ! c.data || ! c.data2 )
@@ -90,9 +92,10 @@
 
 int
-  readColorComponent( ColorComponent& c, FILE* file, int width, int height, bool second )
+  readColorComponent( ColorComponent& c, FILE* file, int width, int height, int inputBitDepth, bool second )
 {
   assert( width  <= c.stride );
   assert( height <= c.lines  );
-
+  unsigned char *temp=NULL;
+  
   int iMaxPadWidth  = gMin( c.stride, ( ( width  + 15 ) >> 4 ) << 4 );
   int iMaxPadHeight = gMin( c.lines,  ( ( height + 31 ) >> 5 ) << 5 );
@@ -100,6 +103,19 @@
   for( int i = 0; i < height; i++ )
   {
-    unsigned char* buffer = ( second ? c.data2 : c.data ) + i * c.stride;
-    int            rsize  = (int)fread( buffer, sizeof(unsigned char), width, file );
+    short* buffer = ( second ? c.data2 : c.data ) + i * c.stride;
+    int            rsize;
+    
+    if (inputBitDepth == 10)
+    {
+       rsize = (int)fread( buffer, sizeof(short), width, file );
+    }
+    else
+    {
+       if (temp == NULL)
+         temp = (unsigned char * )calloc(width, sizeof(unsigned char));
+       rsize = (int)fread( temp, sizeof(unsigned char), width, file );
+       for (int j = 0; j < width; j++)
+         buffer[j] = (short) temp[j];
+    }
     if( rsize != width )
     {
@@ -113,6 +129,6 @@
   for( int yp = height; yp < iMaxPadHeight; yp++ )
   {
-    unsigned char* buffer  = ( second ? c.data2 : c.data ) + yp * c.stride;
-    unsigned char* bufferX = buffer - c.stride;
+    short* buffer  = ( second ? c.data2 : c.data ) + yp * c.stride;
+    short* bufferX = buffer - c.stride;
     for( int xp = 0; xp < c.stride; xp++ )
     {
@@ -120,4 +136,6 @@
     }
   }
+  if (temp!=NULL)
+    free(temp);
   return 0;
 }
@@ -126,5 +144,5 @@
   duplicateColorComponent( ColorComponent& c )
 {
-  memcpy( c.data2, c.data, c.stride * c.lines * sizeof(unsigned char) );
+  memcpy( c.data2, c.data, c.stride * c.lines * sizeof(short) );
 }
 
@@ -133,22 +151,36 @@
 {
   int            offs = ( bBotField ? c.stride : 0 );
-  unsigned char* pDes = c.data  + offs;
-  unsigned char* pSrc = c.data2 + offs;
+  short* pDes = c.data  + offs;
+  short* pSrc = c.data2 + offs;
   for( int i = 0; i < c.lines / 2; i++, pDes += 2*c.stride, pSrc += 2*c.stride )
   {
-    memcpy( pDes, pSrc, c.stride * sizeof(unsigned char) );
-  }
-}
-
-void
-  writeColorComponent( ColorComponent& c, FILE* file, int width, int height, bool second )
+    memcpy( pDes, pSrc, c.stride * sizeof(short) );
+  }
+}
+
+void
+  writeColorComponent( ColorComponent& c, FILE* file, int width, int height, int outputBitDepth, bool second )
 {
   assert( width  <= c.stride );
   assert( height <= c.lines  );
-
+  unsigned char* temp = NULL;
+       
   for( int i = 0; i < height; i++ )
   {
-    unsigned char* buffer = ( second ? c.data2 : c.data ) + i * c.stride;
-    int            wsize  = (int)fwrite( buffer, sizeof(unsigned char), width, file );
+    short* buffer = ( second ? c.data2 : c.data ) + i * c.stride;
+    int            wsize;
+    
+    if ( outputBitDepth == 8 )
+    {
+      if (temp == NULL)
+        temp = (unsigned char * )calloc(width, sizeof(unsigned char));
+      for (int j = 0; j < width; j ++)
+        temp[j] = (unsigned char)buffer[j];
+      wsize = (int)fwrite( temp, sizeof(unsigned char), width, file );
+    }
+    else
+    {
+      wsize = (int)fwrite( buffer, sizeof(short), width, file );
+    }
 
     if( wsize != width )
@@ -162,6 +194,8 @@
 
 void
-  createFrame( YuvFrame& f, int width, int height )
-{
+  createFrame( YuvFrame& f, int width, int height, int inputBitDepth, int outputBitDepth )
+{
+  f.inputBitDepth = inputBitDepth;
+  f.outputBitDepth = outputBitDepth;
   createColorComponent( f.y, width,      height      );
   createColorComponent( f.u, width >> 1, height >> 1 );
@@ -180,7 +214,7 @@
   readFrame( YuvFrame& f, FILE* file, int width, int height, bool second = false )
 {
-  ROTRS( readColorComponent( f.y, file, width,      height,      second ), 1 );
-  ROTRS( readColorComponent( f.u, file, width >> 1, height >> 1, second ), 1 );
-  ROTRS( readColorComponent( f.v, file, width >> 1, height >> 1, second ), 1 );
+  ROTRS( readColorComponent( f.y, file, width,      height,      f.inputBitDepth, second ), 1 );
+  ROTRS( readColorComponent( f.u, file, width >> 1, height >> 1, f.inputBitDepth, second ), 1 );
+  ROTRS( readColorComponent( f.v, file, width >> 1, height >> 1, f.inputBitDepth, second ), 1 );
   return 0;
 }
@@ -205,13 +239,13 @@
   writeFrame( YuvFrame& f, FILE* file, int width, int height, bool both = false )
 {
-  writeColorComponent( f.y, file, width,      height,      false );
-  writeColorComponent( f.u, file, width >> 1, height >> 1, false );
-  writeColorComponent( f.v, file, width >> 1, height >> 1, false );
+  writeColorComponent( f.y, file, width,      height,      f.outputBitDepth, false );
+  writeColorComponent( f.u, file, width >> 1, height >> 1, f.outputBitDepth, false );
+  writeColorComponent( f.v, file, width >> 1, height >> 1, f.outputBitDepth, false );
 
   if( both )
   {
-    writeColorComponent( f.y, file, width,      height,      true );
-    writeColorComponent( f.u, file, width >> 1, height >> 1, true );
-    writeColorComponent( f.v, file, width >> 1, height >> 1, true );
+    writeColorComponent( f.y, file, width,      height,      f.outputBitDepth, true );
+    writeColorComponent( f.u, file, width >> 1, height >> 1, f.outputBitDepth, true );
+    writeColorComponent( f.v, file, width >> 1, height >> 1, f.outputBitDepth, true );
   }
 }
@@ -227,5 +261,5 @@
       fprintf ( stderr, "\nERROR: %s\n", message );
     }
-    fprintf (   stderr, "\nUsage: %s <win> <hin> <in> <wout> <hout> <out> [<t> [<skip> [<frms>]]] [[-phase <args>] ]\n\n", name );
+    fprintf (   stderr, "\nUsage: %s <win> <hin> <in> <wout> <hout> <out> [<bin> <bout> <method> <t> [<skip> [<frms>]]] [[-phase <args>] ]\n\n", name );
     fprintf (   stderr, "  win     : input width  (luma samples)\n" );
     fprintf (   stderr, "  hin     : input height (luma samples)\n" );
@@ -235,4 +269,7 @@
     fprintf (   stderr, "  out     : output file\n" );
     fprintf (   stderr, "\n--------------------------- OPTIONAL ---------------------------\n\n" );
+    fprintf (   stderr, "  bin     : input bit depth (8 or 10) (default: 8)\n" );
+    fprintf (   stderr, "  bout    : output bit depth (8 or 10) (default: 8)\n" );
+    fprintf (   stderr, "  method  : sampling method (0, 1, 2, 3, 4) (default: 0)\n" );
     fprintf (   stderr, "  t       : number of temporal downsampling stages (default: 0)\n" );
     fprintf (   stderr, "  skip    : number of frames to skip at start (default: 0)\n" );
@@ -244,4 +281,6 @@
     fprintf (   stderr, "   out_uv_ph_x: output chroma phase shift in horizontal direction (default:-1)\n" );
     fprintf (   stderr, "   out_uv_ph_y: output chroma phase shift in vertical   direction (default: 0)\n" );
+    fprintf (   stderr, "\n-------------------------- EXAMPLE --------------------------\n\n" );
+    fprintf (   stderr, "\nUsage: %s 4096 2048 input_video_4096x2048_10bits.yuv 2048 1024 output_video_2048x1024_8bits.yuv 10 8\n\n", name );
     fprintf (   stderr, "\n\n");
     exit    (   1 );
@@ -304,4 +343,6 @@
     cRP.m_iScaledRefFrmWidth  = iScaledRefFrmW;
     cRP.m_iScaledRefFrmHeight = iScaledRefFrmH;
+    cRP.inputBitDepth         = rcFrame.inputBitDepth;
+    cRP.outputBitDepth        = rcFrame.outputBitDepth;
   }
   assert( resamplingMethod == 0 );
@@ -354,4 +395,6 @@
   FILE* outputFile                  = 0;
   FILE* croppingParametersFile      = 0;
+  int   inputBitDepth               = 8;
+  int   outputBitDepth              = 8;
   int   resamplingMethod            = 0;
   int   resamplingMode              = 0;
@@ -396,5 +439,15 @@
       print_usage_and_exit( cRP.m_iChromaPhaseY         > 1 || cRP.m_iChromaPhaseY         < -1, argv[0], "wrong phase x parameters (range : [-1, 1])");
     }
-    else if (i == 7)
+    else if (i == 7) // input bit depth
+    {
+      inputBitDepth = atoi( argv[i++] );
+      print_usage_and_exit( inputBitDepth != 8 && inputBitDepth != 10, argv[0], "wrong input bit depth (8 bit or 10 bit)");
+    }
+    else if (i == 8) // output bit depth
+    {
+      outputBitDepth = atoi( argv[i++] );
+      print_usage_and_exit( outputBitDepth != 8 && outputBitDepth != 10, argv[0], "wrong output bit depth (8 bit or 10 bit)");
+    }
+    else if (i == 9) // downsampling methods
     {
       methodInitialized = true;
@@ -439,5 +492,5 @@
       }
     }
-    else if( i == 8 )
+    else if( i == 10 ) // temporal stages
     {
       int TStages = atoi( argv[i++] );
@@ -445,10 +498,10 @@
       print_usage_and_exit( TStages < 0,              argv[0], "negative number of temporal stages" );
     }
-    else if( i == 9 )
+    else if( i == 11 ) // skipped frames
     {
       skipAtStart = atoi( argv[i++] );
       print_usage_and_exit( skipAtStart < 0,          argv[0], "negative number of skipped frames at start" );
     }
-    else if( i == 10 )
+    else if( i == 12 ) // output frames
     {
       maxNumOutputFrames = atoi( argv[i++] );
@@ -515,5 +568,5 @@
     maxWidth      = ( ( maxWidth  * minWRnd16 + ( minWidth  << 4 ) - 1 ) / ( minWidth  << 4 ) ) << 4;
     maxHeight     = ( ( maxHeight * minHRnd32 + ( minHeight << 4 ) - 1 ) / ( minHeight << 4 ) ) << 4;
-    createFrame( cFrame, maxWidth, maxHeight );
+    createFrame( cFrame, maxWidth, maxHeight, inputBitDepth, outputBitDepth );
     cDownConvert.init(   maxWidth, maxHeight );
   }
