[x265] [PATCH] rc: initialize the 2 pass states in rc

aarthi at multicorewareinc.com aarthi at multicorewareinc.com
Tue Jun 17 16:39:25 CEST 2014


# HG changeset patch
# User Aarthi Thirumalai<aarthi at multicorewareinc.com>
# Date 1403015762 -19800
#      Tue Jun 17 20:06:02 2014 +0530
# Node ID c7581482b1a89737b91cf0bb45250a5e8145bd9b
# Parent  dcccf4b46c3da05eb86e3d42b11d0aa282298ad1
rc: initialize the 2 pass states in rc

diff -r dcccf4b46c3d -r c7581482b1a8 source/encoder/encoder.cpp
--- a/source/encoder/encoder.cpp	Tue Jun 17 18:44:11 2014 +0530
+++ b/source/encoder/encoder.cpp	Tue Jun 17 20:06:02 2014 +0530
@@ -185,7 +185,8 @@
             }
         }
     }
-    m_rateControl->init(&m_frameEncoder[0].m_sps);
+    if (!m_rateControl->init(&m_frameEncoder[0].m_sps))
+        m_aborted = true;
     m_lookahead->init();
     m_encodeStartTime = x265_mdate();
 }
diff -r dcccf4b46c3d -r c7581482b1a8 source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp	Tue Jun 17 18:44:11 2014 +0530
+++ b/source/encoder/ratecontrol.cpp	Tue Jun 17 20:06:02 2014 +0530
@@ -27,8 +27,8 @@
 #include "encoder.h"
 #include "slicetype.h"
 #include "ratecontrol.h"
+#include "param.h"
 #include "TLibCommon/SEI.h"
-
 #define BR_SHIFT  6
 #define CPB_SHIFT 4
 
@@ -81,6 +81,17 @@
     *n /= b;
     *d /= b;
 }
+
+inline char *strcatFilename(char *input, char *suffix)
+{
+    char *output = X265_MALLOC(char, strlen(input) + strlen(suffix) + 1);
+    if (!output)
+        return NULL;
+    strcpy(output, input);
+    strcat(output, suffix);
+    return output;
+}
+
 }  // end anonymous namespace
 /* Compute variance to derive AC energy of each block */
 static inline uint32_t acEnergyVar(TComPic *pic, uint64_t sum_ssd, int shift, int i)
@@ -280,8 +291,8 @@
         if (m_param->rc.rfConstantMin)
             m_rateFactorMaxDecrement = m_param->rc.rfConstant - m_param->rc.rfConstantMin;
     }
-
-    m_isAbr = m_param->rc.rateControlMode != X265_RC_CQP; // later add 2pass option
+    m_isAbr = m_param->rc.rateControlMode != X265_RC_CQP && !m_param->rc.statInFileName;
+    m_2pass =  m_param->rc.rateControlMode == X265_RC_ABR && m_param->rc.statInFileName;
     m_bitrate = m_param->rc.bitrate * 1000;
     m_frameDuration = (double)m_param->fpsDenom / m_param->fpsNum;
     m_qp = m_param->rc.qp;
@@ -291,6 +302,8 @@
     m_lastNonBPictType = I_SLICE;
     m_isAbrReset = false;
     m_lastAbrResetPoc = -1;
+    m_statFileOut = NULL;
+    m_cutreeStatFileOut = m_cutreeStatFileIn = NULL;
     // vbv initialization
     m_param->rc.vbvBufferSize = Clip3(0, 2000000, m_param->rc.vbvBufferSize);
     m_param->rc.vbvMaxBitrate = Clip3(0, 2000000, m_param->rc.vbvMaxBitrate);
@@ -330,7 +343,7 @@
         m_param->rc.vbvMaxBitrate = 0;
     }
     m_isVbv = m_param->rc.vbvMaxBitrate > 0 && m_param->rc.vbvBufferSize > 0;
-    m_isCbr = m_param->rc.rateControlMode == X265_RC_ABR && m_isVbv && m_param->rc.vbvMaxBitrate == m_param->rc.bitrate;
+    m_isCbr = m_param->rc.rateControlMode == X265_RC_ABR && m_isVbv && m_param->rc.vbvMaxBitrate <= m_param->rc.bitrate;
     m_bframes = m_param->bframes;
     m_bframeBits = 0;
     m_leadingNoBSatd = 0;
@@ -362,8 +375,56 @@
     m_lstep = pow(2, m_param->rc.qpStep / 6.0);
 }
 
-void RateControl::init(TComSPS *sps)
+bool RateControl::init(TComSPS *sps)
 {
+    if (!m_statFileOut)
+    {
+        /* Open output file */
+        /* If input and output files are the same, output to a temp file
+         * and move it to the real name only when it's complete */
+        if (m_param->rc.statOutFileName)
+        {
+            char *p;
+            m_statFileTmpname = strcatFilename(m_param->rc.statOutFileName, ".temp");
+            if (!m_statFileTmpname)
+                return false;
+            m_statFileOut = fopen(m_statFileTmpname, "wb");
+            if (!m_statFileOut)
+            {
+                x265_log(m_param, X265_LOG_ERROR, "RateControl Init: can't open stats file\n");
+                return false;
+            }
+
+            p = x265_param2string(m_param);
+            if (p)
+                fprintf(m_statFileOut, "#options: %s\n", p);
+            X265_FREE(p);
+            if (m_param->rc.cuTree && !m_param->rc.statInFileName)
+            {
+                m_cutreeStatFileTmpname = strcatFilename(m_param->rc.statOutFileName, ".cutree.temp");
+                if (!m_cutreeStatFileTmpname)
+                    return false;
+
+                m_cutreeStatFileOut = fopen(m_cutreeStatFileTmpname, "wb");
+                if (!m_cutreeStatFileOut)
+                {
+                    x265_log(m_param, X265_LOG_ERROR, "RateControl Init: can't open mbtree stats file\n");
+                    return false;
+                }
+            }
+        }
+        if (m_param->rc.cuTree && (m_param->rc.statOutFileName || m_param->rc.statInFileName))
+        {
+            m_cuTreeStats.qpBuffer[0] = X265_MALLOC(uint16_t, m_ncu * sizeof(uint16_t));
+            if (m_param->bBPyramid && m_param->rc.statInFileName)
+                m_cuTreeStats.qpBuffer[1] = X265_MALLOC(uint16_t, m_ncu * sizeof(uint16_t));
+            m_cuTreeStats.qpBufPos = -1;
+        }
+    }
+
+    if (m_2pass)
+        return true;
+
     if (m_isVbv)
     {
         double fps = (double)m_param->fpsNum / m_param->fpsDenom;
@@ -421,10 +482,9 @@
         m_pred[i].decay = 0.5;
         m_pred[i].offset = 0.0;
     }
-
     m_predBfromP = m_pred[0];
+    return true;
 }
-
 void RateControl::initHRD(TComSPS *sps)
 {
     int vbvBufferSize = m_param->rc.vbvBufferSize * 1000;
diff -r dcccf4b46c3d -r c7581482b1a8 source/encoder/ratecontrol.h
--- a/source/encoder/ratecontrol.h	Tue Jun 17 18:44:11 2014 +0530
+++ b/source/encoder/ratecontrol.h	Tue Jun 17 20:06:02 2014 +0530
@@ -158,11 +158,9 @@
     void calcAdaptiveQuantFrame(TComPic *pic);
     int rateControlEnd(TComPic* pic, int64_t bits, RateControlEntry* rce);
     int rowDiagonalVbvRateControl(TComPic* pic, uint32_t row, RateControlEntry* rce, double& qpVbv);
-
     void hrdFullness(SEIBufferingPeriod* sei);
-    void init(TComSPS* sps);
+    bool init(TComSPS* sps);
     void initHRD(TComSPS* sps);
-
 protected:
 
     static const double s_amortizeFraction;


More information about the x265-devel mailing list