[x265] [PATCH] rc: write frame stats and cu stats to file in first pass

aarthi at multicorewareinc.com aarthi at multicorewareinc.com
Wed Jul 2 20:05:03 CEST 2014


# HG changeset patch
# User Aarthi Thirumalai<aarthi at multicorewareinc.com>
# Date 1404146150 -19800
#      Mon Jun 30 22:05:50 2014 +0530
# Node ID af5360464d2e751bf17e795b3c314445431d59e0
# Parent  97150d9a406f9a40a240fcf6417385bc4b788b00
rc: write frame stats and cu stats to file in first pass

diff -r 97150d9a406f -r af5360464d2e source/encoder/encoder.cpp
--- a/source/encoder/encoder.cpp	Thu Jun 26 17:49:26 2014 +0530
+++ b/source/encoder/encoder.cpp	Mon Jun 30 22:05:50 2014 +0530
@@ -408,9 +408,12 @@
                 bytes -= (!i || type == NAL_UNIT_SPS || type == NAL_UNIT_PPS) ? 4 : 3;
             }
         }
-        m_rateControl->rateControlEnd(out, bytes << 3, &curEncoder->m_rce);
+        if (m_rateControl->rateControlEnd(out, bytes << 3, &curEncoder->m_rce, &curEncoder->m_frameStats) < 0)
+        {
+            m_aborted = true;
+            return -1;
+        }
         finishFrameStats(out, curEncoder, bytes << 3);
-
         // Allow this frame to be recycled if no frame encoders are using it for reference
         if (!pic_out)
         {
@@ -445,8 +448,7 @@
                 fenc->getPicSym()->allocSaoParam(m_frameEncoder->getSAO());
         }
         fenc->getSlice()->setPOC(fenc->m_POC);
-
-        m_encodedFrameNum++;
+        curEncoder->m_rce.encodeOrder = m_encodedFrameNum++;
         if (m_bframeDelay)
         {
             int64_t *prevReorderedPts = m_prevReorderedPts;
diff -r 97150d9a406f -r af5360464d2e source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp	Thu Jun 26 17:49:26 2014 +0530
+++ b/source/encoder/ratecontrol.cpp	Mon Jun 30 22:05:50 2014 +0530
@@ -28,6 +28,7 @@
 #include "frame.h"
 
 #include "encoder.h"
+#include "frameencoder.h"
 #include "slicetype.h"
 #include "ratecontrol.h"
 #include "sei.h"
@@ -1224,7 +1225,7 @@
 }
 
 /* After encoding one frame, update rate control state */
-int RateControl::rateControlEnd(Frame* pic, int64_t bits, RateControlEntry* rce)
+int RateControl::rateControlEnd(Frame* pic, int64_t bits, RateControlEntry* rce, FrameStats* stats)
 {
     int64_t actualBits = bits;
     if (m_isAbr)
@@ -1273,7 +1274,38 @@
                     pic->m_avgQpAq /= (pic->getFrameHeightInCU() * pic->getFrameWidthInCU());
                 }
             }
-
+            // Write frame stats into the stats file if 2 pass is enabled.
+            if (m_param->rc.bStatWrite)
+            {
+                char cType = rce->sliceType == I_SLICE ? (rce->poc == 0 ? 'I' : 'i')
+                            : rce->sliceType == P_SLICE ? (pic->getSlice()->isReferenced()? 'P' : 'p')
+                            : pic->getSlice()->isReferenced()? 'B' : 'b';
+                if (fprintf(m_statFileOut,
+                         "in:%d out:%d type:%c dur:%.3f q:%.2f q-aq:%.2f tex:%d mv:%d misc:%d imb:%.2f pmb:%.2f smb:%.2f ",
+                         rce->poc, rce->encodeOrder,
+                         cType, m_frameDuration,
+                         pic->m_avgQpRc, pic->m_avgQpAq,
+                         stats->coeffBits,
+                         stats->mvBits,
+                         stats->miscBits,
+                         stats->cuCount_i,
+                         stats->cuCount_p,
+                         stats->cuCount_skip) < 0)
+                    goto writeFailure;
+                if (fprintf(m_statFileOut, ";\n") < 0)
+                    goto writeFailure;
+                /* Don't re-write the data in multi-pass mode. */
+                if (m_param->rc.cuTree && pic->getSlice()->isReferenced() && !m_param->rc.bStatRead)
+                {
+                    uint8_t sliceType = (uint8_t)rce->sliceType;
+                    for (int i = 0; i < m_ncu; i++)
+                         m_cuTreeStats.qpBuffer[0][i] = ((uint16_t)pic->m_lowres.qpCuTreeOffset[i]) * 256.0;
+                    if (fwrite(&sliceType, 1, 1, m_cutreeStatFileOut) < 1)
+                        goto writeFailure;
+                    if (fwrite(m_cuTreeStats.qpBuffer[0], sizeof(uint16_t), m_ncu, m_cutreeStatFileOut) < m_ncu)
+                        goto writeFailure;
+                }
+            }
             /* amortize part of each I slice over the next several frames, up to
              * keyint-max, to avoid over-compensating for the large I slice cost */
             if (rce->sliceType == I_SLICE)
@@ -1354,6 +1386,10 @@
     }
     rce->isActive = false;
     return 0;
+
+writeFailure:
+    x265_log(m_param, X265_LOG_ERROR, "RatecontrolEnd: stats file could not be written to\n");
+    return 1;
 }
 
 #if defined(_MSC_VER)
diff -r 97150d9a406f -r af5360464d2e source/encoder/ratecontrol.h
--- a/source/encoder/ratecontrol.h	Thu Jun 26 17:49:26 2014 +0530
+++ b/source/encoder/ratecontrol.h	Mon Jun 30 22:05:50 2014 +0530
@@ -34,7 +34,7 @@
 class Frame;
 class TComSPS;
 class SEIBufferingPeriod;
-
+struct FrameStats;
 #define BASE_FRAME_DURATION 0.04
 
 /* Arbitrary limitations as a sanity check. */
@@ -59,6 +59,7 @@
     int mvBits;
     int bframes;
     int poc;
+    uint64_t encodeOrder;
     int64_t leadingNoBSatd;
     bool bLastMiniGopBFrame;
     double blurredComplexity;
@@ -157,7 +158,7 @@
     // to be called for each frame to process RateControl and set QP
     void rateControlStart(Frame* pic, Lookahead *, RateControlEntry* rce, Encoder* enc);
     void calcAdaptiveQuantFrame(Frame *pic);
-    int rateControlEnd(Frame* pic, int64_t bits, RateControlEntry* rce);
+    int rateControlEnd(Frame* pic, int64_t bits, RateControlEntry* rce, FrameStats* stats);
     int rowDiagonalVbvRateControl(Frame* pic, uint32_t row, RateControlEntry* rce, double& qpVbv);
     void hrdFullness(SEIBufferingPeriod* sei);
     bool init(TComSPS* sps);


More information about the x265-devel mailing list