[x265] [PATCH] Included the computeLambdaqp()-- reset the lambda based on new QP

sumalatha at multicorewareinc.com sumalatha at multicorewareinc.com
Tue Aug 27 09:20:30 CEST 2013


# HG changeset patch
# User sumalatha
# Date 1377588013 -19800
# Node ID 3657c491f6adbb691fb5d7a9e3bc4eb3a12fa2d9
# Parent  273b1face64ce9e0c391713165776c773ec54774
Included the computeLambdaqp()-- reset the lambda based on new QP

diff -r 273b1face64c -r 3657c491f6ad source/Lib/TLibCommon/TComRom.cpp
--- a/source/Lib/TLibCommon/TComRom.cpp	Mon Aug 26 15:00:18 2013 -0500
+++ b/source/Lib/TLibCommon/TComRom.cpp	Tue Aug 27 12:50:13 2013 +0530
@@ -514,4 +514,15 @@
 const Int g_winUnitX[] = { 1, 2, 2, 1 };
 const Int g_winUnitY[] = { 1, 2, 1, 1 };
 
+const int x265_HM_lambda2_tab[MAX_QP+1] =
+{
+   1,   1,   1,   2,   2,  3,   4,   6, /*  0- 7 */
+   6,   7,   9,   15,   15,   19,   23,   37, /*  8-15 */
+   37,   47,   59,   93,   94,   118,   149,   235, /* 16-23 */
+   237,   323,   438,   740,   795,   1064,   1420,   2361, /* 24-31 */
+  2505,  3314,  4375,  7203,  7576,  9545,  12026,  18939, /* 32-39 */
+  19090,  24052,  30303,  47726,  48104,  60608,  76361,  120261, /* 40-47 */
+  121215,  152721,  192417,  303038
+};
+
 //! \}
diff -r 273b1face64c -r 3657c491f6ad source/Lib/TLibCommon/TComRom.h
--- a/source/Lib/TLibCommon/TComRom.h	Mon Aug 26 15:00:18 2013 -0500
+++ b/source/Lib/TLibCommon/TComRom.h	Tue Aug 27 12:50:13 2013 +0530
@@ -290,5 +290,5 @@
 // Map Luma samples to chroma samples
 extern const Int g_winUnitX[MAX_CHROMA_FORMAT_IDC + 1];
 extern const Int g_winUnitY[MAX_CHROMA_FORMAT_IDC + 1];
-
+extern const int x265_HM_lambda2_tab[MAX_QP+1];
 #endif  //__TCOMROM__
diff -r 273b1face64c -r 3657c491f6ad source/Lib/TLibEncoder/TEncTop.cpp
--- a/source/Lib/TLibEncoder/TEncTop.cpp	Mon Aug 26 15:00:18 2013 -0500
+++ b/source/Lib/TLibEncoder/TEncTop.cpp	Tue Aug 27 12:50:13 2013 +0530
@@ -47,7 +47,7 @@
 #include "frameencoder.h"
 #include "ratecontrol.h"
 #include "dpb.h"
-
+#include "TLibCommon/TComRom.h"
 #include <math.h> // log10
 
 using namespace x265;
@@ -254,8 +254,11 @@
         // determine references, set QP, etc
         m_dpb->prepareEncode(fenc, curEncoder);
 
-        //m_lookahead->getEstimatedPictureCost(fenc);  // TODO: move into rate control
-        //m_rateControl->rateControlStart(fenc);
+
+        /*uncomment the below function for enabling Ratecontrol code*/
+        //int lookAheadCost = m_lookahead->getEstimatedPictureCost(fenc);  
+        //m_rateControl->rateControlStart(fenc, lookAheadCost);
+		//computeLambdaForQp(fenc->getSlice());
 
         // main encode processing, TBD multi-threading
         curEncoder->compressFrame(fenc);
@@ -943,4 +946,41 @@
     }
 }
 
+
+Void TEncTop::computeLambdaForQp( TComSlice* slice)
+{  
+	FrameEncoder *curEncoder = &m_frameEncoder[m_curEncoder];
+	Int lambda;
+	Int qp = slice->getSliceQp();
+	lambda = x265_HM_lambda2_tab[qp];
+
+		  // for RDO
+    // in RdCost there is only one lambda because the luma and chroma bits are not separated,
+    // instead we weight the distortion of chroma.
+    Double weight = 1.0;
+    Int qpc;
+    Int chromaQPOffset;
+
+    chromaQPOffset = slice->getPPS()->getChromaCbQpOffset() + slice->getSliceQpDeltaCb();
+    qpc = Clip3(0, 57, qp + chromaQPOffset);
+    weight = pow(2.0, (qp - g_chromaScale[qpc]) / 3.0); // takes into account of the chroma qp mapping and chroma qp Offset
+    curEncoder->setCbDistortionWeight(weight);
+
+    chromaQPOffset = slice->getPPS()->getChromaCrQpOffset() + slice->getSliceQpDeltaCr();
+    qpc = Clip3(0, 57, qp + chromaQPOffset);
+    weight = pow(2.0, (qp - g_chromaScale[qpc]) / 3.0); // takes into account of the chroma qp mapping and chroma qp Offset
+    curEncoder->setCrDistortionWeight(weight);
+
+    // for RDOQ
+    curEncoder->setQPLambda(qp, lambda, lambda / weight, slice->getDepth());
+
+    // For SAO
+    slice->setLambda(lambda, lambda / weight);
+
+    slice->setSliceQpBase(qp);
+    slice->setSliceQpDelta(0);
+    slice->setSliceQpDeltaCb(0);
+    slice->setSliceQpDeltaCr(0);
+}
+
 //! \}
diff -r 273b1face64c -r 3657c491f6ad source/Lib/TLibEncoder/TEncTop.h
--- a/source/Lib/TLibEncoder/TEncTop.h	Mon Aug 26 15:00:18 2013 -0500
+++ b/source/Lib/TLibEncoder/TEncTop.h	Tue Aug 27 12:50:13 2013 +0530
@@ -113,6 +113,7 @@
     void setThreadPool(x265::ThreadPool* p) { m_threadPool = p; }
 
     Double calculateHashAndPSNR(TComPic* pic, AccessUnit&); //Returns number of bits in current encoded pic
+	Void TEncTop::computeLambdaForQp( TComSlice* slice);
 };
 
 //! \}
diff -r 273b1face64c -r 3657c491f6ad source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp	Mon Aug 26 15:00:18 2013 -0500
+++ b/source/encoder/ratecontrol.cpp	Tue Aug 27 12:50:13 2013 +0530
@@ -96,17 +96,24 @@
     cbrDecay = 1.0;
 }
 
-void RateControl::rateControlStart(TComPic* pic)
+void RateControl::rateControlStart(TComPic* pic, int lookAheadCost)
 {
     curFrame = pic->getSlice();
     frameType = curFrame->getSliceType();
     rce = new RateControlEntry();
+    lastSatd = lookAheadCost;
     double q = 0;
 
     switch (rateControlMode)
     {
     case X265_RC_ABR:
-        q = qScale2qp(rateEstimateQscale(&pic->m_lowres));
+        q = qScale2qp(rateEstimateQscale());
+        q = Clip3((double)MIN_QP, (double)(MAX_QP+18), q);
+        qp = Clip3(0, (MAX_QP+18), (int)(q + 0.5f));
+        qpaRc = qpm = q;    // qpaRc is set in the rate_control_mb call in x264. we are updating here itself.
+        if (rce)
+            rce->newQp = qp;
+        accumPQpUpdate();
         break;
     case X265_RC_CQP:
         q = baseQp;
@@ -117,13 +124,7 @@
         break;
     }
 
-    q = Clip3((double)MIN_QP, (double)MAX_QP, q);
-    qp = Clip3(0, MAX_QP, (int)(q + 0.5f));
-    qpaRc = qpm = q;    // qpaRc is set in the rate_control_mb call in x264. we are updating here itself.
-    if (rce)
-        rce->newQp = qp;
-
-    accumPQpUpdate();
+    
 
     if (frameType != B_SLICE)
         lastNonBPictType = frameType;
@@ -142,7 +143,7 @@
         accumPQp += qpm;
 }
 
-double RateControl::rateEstimateQscale(Lowres* lframe)
+double RateControl::rateEstimateQscale()
 {
     double q;
     // ratecontrol_entry_t rce = UNINIT(rce);
@@ -195,10 +196,7 @@
          * tolerances, the bit distribution approaches that of 2pass. */
 
         double wantedBits, overflow = 1;
-        int p0 = curFrame->getRefPic(REF_PIC_LIST_0, 0)->getPOC();
-        int p1 = curFrame->getRefPic(REF_PIC_LIST_1, 0)->getPOC();
-        lastSatd = lframe->costEst[curFrame->getPOC() - p0][p1 - curFrame->getPOC()] ;     //TODO:need to get this from lookahead  //x264_rc_analyse_slice( h );
-        rce->pCount = ncu;
+		rce->pCount = ncu;
 
         shortTermCplxSum *= 0.5;
         shortTermCplxCount *= 0.5;
diff -r 273b1face64c -r 3657c491f6ad source/encoder/ratecontrol.h
--- a/source/encoder/ratecontrol.h	Mon Aug 26 15:00:18 2013 -0500
+++ b/source/encoder/ratecontrol.h	Tue Aug 27 12:50:13 2013 +0530
@@ -83,9 +83,9 @@
     int64_t totalBits;   /* totalbits used for already encoded frames */
 
     RateControl(x265_param_t * param);    // constructor for initializing values for ratecontrol vars
-    void rateControlStart(TComPic* pic);  // to be called for each frame to process RateCOntrol and set QP
+    void rateControlStart(TComPic* pic, int lookAheadCost);  // to be called for each frame to process RateCOntrol and set QP
     int rateControlEnd(int64_t bits);
-    double rateEstimateQscale(Lowres* lframe); // main logic for calculating QP based on ABR
+    double rateEstimateQscale(); // main logic for calculating QP based on ABR
     void accumPQpUpdate();
     double getQScale(double rateFactor);
 };


More information about the x265-devel mailing list