Re: [x265] [PATCH] rc: init second pass in multi pass encode

2014-07-11 Thread Steve Borho
On Thu, Jul 10, 2014 at 12:56 PM,  aar...@multicorewareinc.com wrote:
 # HG changeset patch
 # User Aarthi Thirumalaiaar...@multicorewareinc.com
 # Date 1404933617 -19800
 #  Thu Jul 10 00:50:17 2014 +0530
 # Node ID 418b68734fd83bf32dbdd4a097e51ce455267c3d
 # Parent  cbfe2ac89d418f7cf9cc53ad31b7eb2c06fbcb85
 rc: init second pass in multi pass encode

 read stat file from previous pass and compute the blurred
 complexity of each frame

 diff -r cbfe2ac89d41 -r 418b68734fd8 source/common/common.cpp
 --- a/source/common/common.cpp  Thu Jul 10 01:33:35 2014 -0500
 +++ b/source/common/common.cpp  Thu Jul 10 00:50:17 2014 +0530
 @@ -159,3 +159,51 @@

  return size;
  }
 +
 +char* x265_slurp_file(const char *filename)
 +{
 +if (!filename)
 +return NULL;
 +
 +int bError = 0;
 +size_t fSize;
 +char *buf = NULL;
 +
 +FILE *fh = fopen(filename, rb);
 +if (!fh)
 +{
 +x265_log(NULL, X265_LOG_ERROR, unable to open file %s \n, 
 filename);
 +return NULL;
 +}
 +
 +bError |= fseek(fh, 0, SEEK_END)  0;
 +bError |= (fSize = ftell(fh)) = 0;
 +bError |= fseek(fh, 0, SEEK_SET)  0;
 +if (bError)
 +goto error;
 +
 +buf = X265_MALLOC(char, fSize + 2);
 +if (!buf)
 +{
 +x265_log(NULL, X265_LOG_ERROR, unable to allocate memory \n);
 +goto error;
 +}
 +
 +bError |= fread(buf, 1, fSize, fh) != fSize;
 +if (buf[fSize - 1] != '\n')
 +buf[fSize++] = '\n';
 +buf[fSize] = 0;
 +fclose(fh);
 +
 +if (bError)
 +{
 +x265_log(NULL, X265_LOG_ERROR, unable to read the file\n);
 +free(buf);
 +buf = NULL;
 +}
 +return buf;
 +
 +error:
 +fclose(fh);
 +return NULL;
 +}
 diff -r cbfe2ac89d41 -r 418b68734fd8 source/common/common.h
 --- a/source/common/common.hThu Jul 10 01:33:35 2014 -0500
 +++ b/source/common/common.hThu Jul 10 00:50:17 2014 +0530
 @@ -204,5 +204,5 @@
  double x265_qp2qScale(double qp);

  uint32_t x265_picturePlaneSize(int csp, int width, int height, int plane);
 -
 +char* x265_slurp_file(const char *filename);
  #endif // ifndef X265_COMMON_H

can you split this into another patch?

 diff -r cbfe2ac89d41 -r 418b68734fd8 source/common/param.cpp
 --- a/source/common/param.cpp   Thu Jul 10 01:33:35 2014 -0500
 +++ b/source/common/param.cpp   Thu Jul 10 00:50:17 2014 +0530
 @@ -1212,9 +1212,10 @@

  #define BOOL(param, cliopt) \
  s += sprintf(s,  %s, (param) ? cliopt : no-cliopt);
 -
 +s += sprintf(s, %dx%d, p-sourceWidth,p-sourceHeight);
 +s += sprintf(s,  fps=%u/%u, p-fpsNum, p-fpsDenom);
 +s += sprintf(s,  bitdepth=%d,p-internalBitDepth);
  BOOL(p-bEnableWavefront, wpp);
 -s += sprintf(s,  fps=%d/%d, p-fpsNum, p-fpsDenom);
  s += sprintf(s,  ctu=%d, p-maxCUSize);
  s += sprintf(s,  tu-intra-depth=%d, p-tuQTMaxIntraDepth);
  s += sprintf(s,  tu-inter-depth=%d, p-tuQTMaxInterDepth);
 @@ -1286,7 +1287,7 @@
  {
  s += sprintf(s,  ip_ratio=%.2f, p-rc.ipFactor);
  if (p-bframes)
 -s += sprintf(s,  pb_ratio=%.2f, p-rc.pbFactor);
 +s += sprintf(s,  pb_ratio=%.2f , p-rc.pbFactor);

is there a reason for the trailing space here?

in param_parse, these are ipratio/pbratio or ip-factor/pb-factor

  }
  #undef BOOL
  return buf;
 diff -r cbfe2ac89d41 -r 418b68734fd8 source/encoder/encoder.cpp
 --- a/source/encoder/encoder.cppThu Jul 10 01:33:35 2014 -0500
 +++ b/source/encoder/encoder.cppThu Jul 10 00:50:17 2014 +0530
 @@ -1346,7 +1346,7 @@
  p-rc.aqStrength = 0.0;
  }

 -if (p-lookaheadDepth == 0  p-rc.cuTree)
 +if (p-lookaheadDepth == 0  p-rc.cuTree  !p-rc.bStatRead)
  {
  x265_log(p, X265_LOG_WARNING, cuTree disabled, requires lookahead 
 to be enabled\n);
  p-rc.cuTree = 0;
 diff -r cbfe2ac89d41 -r 418b68734fd8 source/encoder/frameencoder.cpp
 --- a/source/encoder/frameencoder.cpp   Thu Jul 10 01:33:35 2014 -0500
 +++ b/source/encoder/frameencoder.cpp   Thu Jul 10 00:50:17 2014 +0530
 @@ -490,6 +490,7 @@
  compressCTURows();

  if (m_param-rc.bStatWrite)
 +{
  // accumulate intra,inter,skip cu count per frame for 2 pass
  for (int i = 0; i  m_numRows; i++)
  {
 @@ -497,7 +498,11 @@
  m_frameStats.cuCount_p += m_rows[i].m_pCuCnt;
  m_frameStats.cuCount_skip += m_rows[i].m_skipCuCnt;
  }
 -
 +double totalCuCount = m_frameStats.cuCount_i + 
 m_frameStats.cuCount_p + m_frameStats.cuCount_skip;
 +m_frameStats.cuCount_i /= totalCuCount;
 +m_frameStats.cuCount_p /= totalCuCount;
 +m_frameStats.cuCount_skip /= totalCuCount;
 +}
  if (m_sps.getUseSAO())
  {
  SAOParam* saoParam = m_frame-getPicSym()-getSaoParam();
 diff -r cbfe2ac89d41 -r 418b68734fd8 source/encoder/frameencoder.h
 --- a/source/encoder/frameencoder.h Thu Jul 10 01:33:35 2014 -0500
 +++ b/source/encoder/frameencoder.h 

[x265] [PATCH] rc: init second pass in multi pass encode

2014-07-10 Thread aarthi
# HG changeset patch
# User Aarthi Thirumalaiaar...@multicorewareinc.com
# Date 1404933617 -19800
#  Thu Jul 10 00:50:17 2014 +0530
# Node ID 418b68734fd83bf32dbdd4a097e51ce455267c3d
# Parent  cbfe2ac89d418f7cf9cc53ad31b7eb2c06fbcb85
rc: init second pass in multi pass encode

read stat file from previous pass and compute the blurred
complexity of each frame

diff -r cbfe2ac89d41 -r 418b68734fd8 source/common/common.cpp
--- a/source/common/common.cpp  Thu Jul 10 01:33:35 2014 -0500
+++ b/source/common/common.cpp  Thu Jul 10 00:50:17 2014 +0530
@@ -159,3 +159,51 @@
 
 return size;
 }
+
+char* x265_slurp_file(const char *filename)
+{
+if (!filename)
+return NULL;
+
+int bError = 0;
+size_t fSize;
+char *buf = NULL;
+
+FILE *fh = fopen(filename, rb);
+if (!fh)
+{
+x265_log(NULL, X265_LOG_ERROR, unable to open file %s \n, filename);
+return NULL;
+}
+
+bError |= fseek(fh, 0, SEEK_END)  0;
+bError |= (fSize = ftell(fh)) = 0;
+bError |= fseek(fh, 0, SEEK_SET)  0;
+if (bError)
+goto error;
+
+buf = X265_MALLOC(char, fSize + 2);
+if (!buf)
+{
+x265_log(NULL, X265_LOG_ERROR, unable to allocate memory \n);
+goto error;
+}
+
+bError |= fread(buf, 1, fSize, fh) != fSize;
+if (buf[fSize - 1] != '\n')
+buf[fSize++] = '\n';
+buf[fSize] = 0;
+fclose(fh);
+
+if (bError)
+{
+x265_log(NULL, X265_LOG_ERROR, unable to read the file\n);
+free(buf);
+buf = NULL;
+}
+return buf;
+
+error:
+fclose(fh);
+return NULL;
+}
diff -r cbfe2ac89d41 -r 418b68734fd8 source/common/common.h
--- a/source/common/common.hThu Jul 10 01:33:35 2014 -0500
+++ b/source/common/common.hThu Jul 10 00:50:17 2014 +0530
@@ -204,5 +204,5 @@
 double x265_qp2qScale(double qp);
 
 uint32_t x265_picturePlaneSize(int csp, int width, int height, int plane);
-
+char* x265_slurp_file(const char *filename);
 #endif // ifndef X265_COMMON_H
diff -r cbfe2ac89d41 -r 418b68734fd8 source/common/param.cpp
--- a/source/common/param.cpp   Thu Jul 10 01:33:35 2014 -0500
+++ b/source/common/param.cpp   Thu Jul 10 00:50:17 2014 +0530
@@ -1212,9 +1212,10 @@
 
 #define BOOL(param, cliopt) \
 s += sprintf(s,  %s, (param) ? cliopt : no-cliopt);
-
+s += sprintf(s, %dx%d, p-sourceWidth,p-sourceHeight);
+s += sprintf(s,  fps=%u/%u, p-fpsNum, p-fpsDenom);
+s += sprintf(s,  bitdepth=%d,p-internalBitDepth);
 BOOL(p-bEnableWavefront, wpp);
-s += sprintf(s,  fps=%d/%d, p-fpsNum, p-fpsDenom);
 s += sprintf(s,  ctu=%d, p-maxCUSize);
 s += sprintf(s,  tu-intra-depth=%d, p-tuQTMaxIntraDepth);
 s += sprintf(s,  tu-inter-depth=%d, p-tuQTMaxInterDepth);
@@ -1286,7 +1287,7 @@
 {
 s += sprintf(s,  ip_ratio=%.2f, p-rc.ipFactor);
 if (p-bframes)
-s += sprintf(s,  pb_ratio=%.2f, p-rc.pbFactor);
+s += sprintf(s,  pb_ratio=%.2f , p-rc.pbFactor);
 }
 #undef BOOL
 return buf;
diff -r cbfe2ac89d41 -r 418b68734fd8 source/encoder/encoder.cpp
--- a/source/encoder/encoder.cppThu Jul 10 01:33:35 2014 -0500
+++ b/source/encoder/encoder.cppThu Jul 10 00:50:17 2014 +0530
@@ -1346,7 +1346,7 @@
 p-rc.aqStrength = 0.0;
 }
 
-if (p-lookaheadDepth == 0  p-rc.cuTree)
+if (p-lookaheadDepth == 0  p-rc.cuTree  !p-rc.bStatRead)
 {
 x265_log(p, X265_LOG_WARNING, cuTree disabled, requires lookahead to 
be enabled\n);
 p-rc.cuTree = 0;
diff -r cbfe2ac89d41 -r 418b68734fd8 source/encoder/frameencoder.cpp
--- a/source/encoder/frameencoder.cpp   Thu Jul 10 01:33:35 2014 -0500
+++ b/source/encoder/frameencoder.cpp   Thu Jul 10 00:50:17 2014 +0530
@@ -490,6 +490,7 @@
 compressCTURows();
 
 if (m_param-rc.bStatWrite)
+{
 // accumulate intra,inter,skip cu count per frame for 2 pass
 for (int i = 0; i  m_numRows; i++)
 {
@@ -497,7 +498,11 @@
 m_frameStats.cuCount_p += m_rows[i].m_pCuCnt;
 m_frameStats.cuCount_skip += m_rows[i].m_skipCuCnt;
 }
-
+double totalCuCount = m_frameStats.cuCount_i + m_frameStats.cuCount_p 
+ m_frameStats.cuCount_skip;
+m_frameStats.cuCount_i /= totalCuCount;
+m_frameStats.cuCount_p /= totalCuCount;
+m_frameStats.cuCount_skip /= totalCuCount;
+}
 if (m_sps.getUseSAO())
 {
 SAOParam* saoParam = m_frame-getPicSym()-getSaoParam();
diff -r cbfe2ac89d41 -r 418b68734fd8 source/encoder/frameencoder.h
--- a/source/encoder/frameencoder.h Thu Jul 10 01:33:35 2014 -0500
+++ b/source/encoder/frameencoder.h Thu Jul 10 00:50:17 2014 +0530
@@ -61,7 +61,7 @@
 /* Texture bits (DCT coefs) */
 int coeffBits;
 int miscBits;
-/* CU type counts */
+/* CU type counts stored as percentage*/
 double  cuCount_i;
 double  cuCount_p;
 double  cuCount_skip;
diff -r cbfe2ac89d41 -r 418b68734fd8