[x265] [PATCH 1 of 2] RateControl bug fixes for frame parallelism

aarthi at multicorewareinc.com aarthi at multicorewareinc.com
Thu Sep 19 20:43:32 CEST 2013


# HG changeset patch
# User Aarthi Thirumalai
# Date 1379614542 -19800
#      Thu Sep 19 23:45:42 2013 +0530
# Node ID e26b439a4031dd93f8aab60f0bdddc7f4a38ae2a
# Parent  e51ecfcabcaa373ad95b8da781501ea4fec1d482
RateControl bug fixes for frame parallelism

1. Added lastRceq in ratecotrol structure and copied the value into thread local rate control entry obj
so that Bframes can get latest lastRceq value.

2. Added framesDone state - to maintain a serial order of frames in RateCotrol to get a correct estimate of wantedBits.

diff -r e51ecfcabcaa -r e26b439a4031 source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp	Thu Sep 19 14:44:28 2013 +0530
+++ b/source/encoder/ratecontrol.cpp	Thu Sep 19 23:45:42 2013 +0530
@@ -70,11 +70,12 @@
     totalBits = 0;
     shortTermCplxSum = 0;
     shortTermCplxCount = 0;
+    framesDone = 0;
     if (rateControlMode == X265_RC_ABR)
     {
         // Adjust the first frame in order to stabilize the quality level compared to the rest.
 #define ABR_INIT_QP_MIN (24 + QP_BD_OFFSET)
-#define ABR_INIT_QP_MAX (38 + QP_BD_OFFSET)
+#define ABR_INIT_QP_MAX (34 + QP_BD_OFFSET)
         accumPNorm = .01;
         accumPQp = (ABR_INIT_QP_MIN) * accumPNorm;
         /* estimated ratio that produces a reasonable QP for the first I-frame  */
@@ -131,7 +132,7 @@
 
     if (frameType != B_SLICE)
         lastNonBPictType = frameType;
-
+    framesDone++;
     /* set the final QP to slice structure */
     curFrame->setSliceQp(qp);
     curFrame->setSliceQpBase(qp);
@@ -217,7 +218,8 @@
          * Don't run it if the frame complexity is zero either. */
         if (lastSatd)
         {
-            double iFrameDone = curFrame->getPOC() + 1 - frameThreads;
+            /* use framesDone istead of POC as poc count is not serial with bframes enabled */
+            double iFrameDone = framesDone - frameThreads + 1;
             double timeDone = iFrameDone / framerate;
             wantedBits = timeDone * bitrate;
             if (wantedBits > 0)
@@ -285,7 +287,7 @@
         q = lastQScaleFor[rce->pictType];
     else
     {
-        rce->lastRceq = q;
+        lastRceq = q;
         q /= rateFactor;
     }
     return q;
@@ -299,12 +301,12 @@
         if (frameType != B_SLICE)
             /* The factor 1.5 is to tune up the actual bits, otherwise the cplxrSum is scaled too low
              * to improve short term compensation for next frame. */
-            cplxrSum += 1.5 *bits * qp2qScale(rce->qpaRc) / rce->lastRceq;
+            cplxrSum += 1.5 * bits * qp2qScale(rce->qpaRc) / rce->qRceq;
         else
         {
             /* Depends on the fact that B-frame's QP is an offset from the following P-frame's.
              * Not perfectly accurate with B-refs, but good enough. */
-            cplxrSum += bits * qp2qScale(rce->qpaRc) / (rce->lastRceq * fabs(pbFactor));
+            cplxrSum += bits * qp2qScale(rce->qpaRc) / (rce->qRceq * fabs(pbFactor));
         }
         wantedBitsWindow += frameDuration * bitrate;
         rce = NULL;
diff -r e51ecfcabcaa -r e26b439a4031 source/encoder/ratecontrol.h
--- a/source/encoder/ratecontrol.h	Thu Sep 19 14:44:28 2013 +0530
+++ b/source/encoder/ratecontrol.h	Thu Sep 19 23:45:42 2013 +0530
@@ -28,7 +28,6 @@
 #include "TLibCommon/CommonDef.h"
 
 namespace x265 {
-
 struct Lookahead;
 class TComPic;
 
@@ -39,7 +38,7 @@
     int mvBits;
     double blurredComplexity;
     double qpaRc;
-    double lastRceq;
+    double qRceq;
 };
 
 struct RateControl
@@ -52,7 +51,7 @@
     int keyFrameInterval;       /* TODO: need to initialize in init */
     int qp;                     /* updated qp for current frame */
     int baseQp;                 /* CQP base QP */
-    double frameDuration;        /* current frame duration in seconds */ 
+    double frameDuration;        /* current frame duration in seconds */
     double bitrate;
     double rateTolerance;
     double qCompress;
@@ -75,7 +74,8 @@
     double shortTermCplxCount;
     RcMethod rateControlMode;
     int64_t totalBits;   /* totalbits used for already encoded frames */
-
+    double lastRceq;
+    int framesDone;   /* framesDone keeps track of # of frames passed through RateCotrol already */
     RateControl(x265_param_t * param);
 
     // to be called for each frame to process RateCOntrol and set QP
@@ -84,6 +84,7 @@
     int rateControlEnd(int64_t bits, RateControlEntry* rce);
 
 protected:
+
     double getQScale(RateControlEntry *rce, double rateFactor);
     double rateEstimateQscale(RateControlEntry *rce); // main logic for calculating QP based on ABR
     void accumPQpUpdate();


More information about the x265-devel mailing list