[x265] [PATCH 2 of 3] vbv: add rc states used for vbv cu level ratecontrol

aarthi at multicorewareinc.com aarthi at multicorewareinc.com
Sun Feb 16 19:47:40 CET 2014


# HG changeset patch
# User Aarthi Thirumalai<aarthi at multicorewareinc.com>
# Date 1392573699 -19800
#      Sun Feb 16 23:31:39 2014 +0530
# Node ID e7e527d13d2fdd9b08915b3245a248332df29882
# Parent  6ea3bf6cbed041417ae407b40651557386f5be5a
vbv: add rc states used for vbv cu level ratecontrol

diff -r 6ea3bf6cbed0 -r e7e527d13d2f source/encoder/encoder.cpp
--- a/source/encoder/encoder.cpp	Sun Feb 16 23:05:13 2014 +0530
+++ b/source/encoder/encoder.cpp	Sun Feb 16 23:31:39 2014 +0530
@@ -276,8 +276,10 @@
             }
         }
         else
+        {
             pic = m_freeList.popBack();
-
+            pic->reInit(this);
+        }
         /* Copy input picture into a TComPic, send to lookahead */
         pic->getSlice()->setPOC(++m_pocLast);
         pic->getPicYuvOrg()->copyFromPicture(*pic_in, m_pad);
diff -r 6ea3bf6cbed0 -r e7e527d13d2f source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp	Sun Feb 16 23:05:13 2014 +0530
+++ b/source/encoder/ratecontrol.cpp	Sun Feb 16 23:31:39 2014 +0530
@@ -215,6 +215,8 @@
 
     // validate for cfg->param.rc, maybe it is need to add a function like x265_parameters_valiate()
     cfg->param.rc.rfConstant = Clip3((double)-QP_BD_OFFSET, (double)51, cfg->param.rc.rfConstant);
+    cfg->param.rc.rfConstantMax = Clip3((double)-QP_BD_OFFSET, (double)51, cfg->param.rc.rfConstant);
+    rateFactorMaxIncrement = 0;
     if (cfg->param.rc.rateControlMode == X265_RC_CRF)
     {
         cfg->param.rc.qp = (int)cfg->param.rc.rfConstant + QP_BD_OFFSET;
@@ -224,6 +226,15 @@
         double mbtree_offset = cfg->param.rc.cuTree ? (1.0 - cfg->param.rc.qCompress) * 13.5 : 0;
         rateFactorConstant = pow(baseCplx, 1 - qCompress) /
             qp2qScale(cfg->param.rc.rfConstant + mbtree_offset + QP_BD_OFFSET);
+        if (cfg->param.rc.rfConstantMax)
+        {
+            rateFactorMaxIncrement = cfg->param.rc.rfConstantMax - cfg->param.rc.rfConstant;
+            if (rateFactorMaxIncrement <= 0)
+            {
+                x265_log(&cfg->param, X265_LOG_WARNING, "CRF max must be greater than CRF\n");
+                rateFactorMaxIncrement = 0;
+            }
+        }
     }
 
     isAbr = cfg->param.rc.rateControlMode != X265_RC_CQP; // later add 2pass option
@@ -314,6 +325,14 @@
         pred[i].offset = 0.0;
     }
 
+    for (int i = 0; i < 4; i++)
+    {
+        rowPreds[i].coeff = 0.25;
+        rowPreds[i].count = 1.0;
+        rowPreds[i].decay = 0.5;
+        rowPreds[i].offset = 0.0;
+    }
+
     predBfromP = pred[0];
     bframes = cfg->param.bframes;
     bframeBits = 0;
@@ -372,7 +391,11 @@
     rce->poc = curSlice->getPOC();
 
     if (isVbv)
+    {
+        rowPred[0] = &rowPreds[sliceType];
+        rowPred[1] = &rowPreds[3];
         updateVbvPlan(enc);
+    }
 
     if (isAbr) //ABR,CRF
     {
@@ -380,9 +403,10 @@
         rce->lastSatd = lastSatd;
         double q = qScale2qp(rateEstimateQscale(pic, rce));
         qp = Clip3(MIN_QP, MAX_MAX_QP, (int)(q + 0.5));
-        rce->qpaRc = q;
+        rce->qpaRc = pic->m_avgQpRc = q;
         /* copy value of lastRceq into thread local rce struct *to be used in RateControlEnd() */
         rce->qRceq = lastRceq;
+        rce->qpNoVbv = qpNoVbv;
         accumPQpUpdate();
     }
     else //CQP
@@ -394,7 +418,10 @@
     }
 
     if (sliceType != B_SLICE)
+    {
         lastNonBPictType = sliceType;
+        leadingNoBSatd = lastSatd;
+    }
     framesDone++;
     /* set the final QP to slice structure */
     curSlice->setSliceQp(qp);
@@ -457,9 +484,9 @@
         else
             q += pbOffset;
 
-        rce->frameSizePlanned = predictSize(&predBfromP, qp2qScale(q), leadingNoBSatd);
-
-        return qp2qScale(q);
+        rce->frameSizePlanned = predictSize(&predBfromP, qp2qScale(q), (double)leadingNoBSatd);
+        qpNoVbv = qp2qScale(q);
+        return qpNoVbv;
     }
     else
     {
@@ -549,7 +576,7 @@
             if (qCompress != 1 && framesDone == 0)
                 q = qp2qScale(ABR_INIT_QP) / fabs(cfg->param.rc.ipFactor);
         }
-
+        qpNoVbv = qScale2qp(q);
         double lmin1 = lmin[sliceType];
         double lmax1 = lmax[sliceType];
         q = Clip3(lmin1, lmax1, q);
diff -r 6ea3bf6cbed0 -r e7e527d13d2f source/encoder/ratecontrol.h
--- a/source/encoder/ratecontrol.h	Sun Feb 16 23:05:13 2014 +0530
+++ b/source/encoder/ratecontrol.h	Sun Feb 16 23:31:39 2014 +0530
@@ -61,6 +61,7 @@
     double frameSizePlanned;
     double bufferRate;
     double movingAvgSum;
+    double qpNoVbv;
 };
 
 struct Predictor
@@ -91,13 +92,16 @@
     double vbvMaxRate;       /* in kbps */
     double vbvMinRate;       /* in kbps */
     bool singleFrameVbv;
+    double rateFactorMaxIncrement; /* Don't allow RF above (CRF + this value). */
     bool isVbv;
     double fps;
     Predictor pred[5];
     Predictor predBfromP;
+    Predictor rowPreds[4];
+    Predictor *rowPred[2];
     int bframes;
     int bframeBits;
-    double leadingNoBSatd;
+    int64_t leadingNoBSatd;
     bool isAbrReset;
     int lastAbrResetPoc;
     int64_t lastSatd;
@@ -119,6 +123,7 @@
     double lastRceq;
     int framesDone;           /* framesDone keeps track of # of frames passed through RateCotrol already */
     double qCompress;
+    double qpNoVbv;             /* QP for the current frame if 1-pass VBV was disabled. */
     RateControl(TEncCfg * _cfg);
 
     // to be called for each frame to process RateControl and set QP
diff -r 6ea3bf6cbed0 -r e7e527d13d2f source/x265.h
--- a/source/x265.h	Sun Feb 16 23:05:13 2014 +0530
+++ b/source/x265.h	Sun Feb 16 23:31:39 2014 +0530
@@ -659,6 +659,8 @@
          * across frames and assigns more bits to these CUs. Improves encode efficiency.
          * Default: OFF (0) */
         int       cuTree;
+        /* In CRF mode, maximum CRF as caused by VBV */
+        double    rfConstantMax;
     } rc;
 } x265_param;
 


More information about the x265-devel mailing list