[x265] [PATCH] modified x265_lambda2_tab[] , adjusted some rc factors, modifed chroma weight for chromaLambda

aarthi at multicorewareinc.com aarthi at multicorewareinc.com
Thu Aug 29 20:47:27 CEST 2013


# HG changeset patch
# User Aarthi Thirumalai<aarthi at multicorewareinc.com>
# Date 1377802018 -19800
#      Fri Aug 30 00:16:58 2013 +0530
# Node ID fefa76460f05df381411534961ef15f356eb487a
# Parent  8b78d8cff9d8ab78b88ab813de0847fdaff804cf
modified x265_lambda2_tab[] , adjusted some rc factors, modifed chroma weight for chromaLambda

diff -r 8b78d8cff9d8 -r fefa76460f05 source/Lib/TLibCommon/TComRom.cpp
--- a/source/Lib/TLibCommon/TComRom.cpp	Thu Aug 29 11:23:16 2013 -0500
+++ b/source/Lib/TLibCommon/TComRom.cpp	Fri Aug 30 00:16:58 2013 +0530
@@ -514,15 +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
-};
+const int x265_lambda2_tab[MAX_QP+1] =
+{14,	18,		23,		30,		39,		50,		64,		83,
+106,	137,	176,	226,	291,	374,	480,	617,
+794,	1020,	1311,	1686,	2165,	2783,	3576,	4594,
+5904,	7586,	9747,	12524,	16094,	20679,	26571,	34143,
+43871,	56371,  72432,	93070,	119587,	153661,	197441,	253697,
+325982,	418861,	538203,	691549,	888585,	1141764,1467077,1885077,
+2422177,3112305};
+
+
 
 //! \}
diff -r 8b78d8cff9d8 -r fefa76460f05 source/Lib/TLibCommon/TComRom.h
--- a/source/Lib/TLibCommon/TComRom.h	Thu Aug 29 11:23:16 2013 -0500
+++ b/source/Lib/TLibCommon/TComRom.h	Fri Aug 30 00:16:58 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];
+extern const int x265_lambda2_tab[MAX_QP+1];
 #endif  //__TCOMROM__
diff -r 8b78d8cff9d8 -r fefa76460f05 source/Lib/TLibEncoder/TEncTop.cpp
--- a/source/Lib/TLibEncoder/TEncTop.cpp	Thu Aug 29 11:23:16 2013 -0500
+++ b/source/Lib/TLibEncoder/TEncTop.cpp	Fri Aug 30 00:16:58 2013 +0530
@@ -845,73 +845,7 @@
     FrameEncoder *curEncoder = &m_frameEncoder[m_curEncoder];
     Int lambda;
     Int qp = slice->getSliceQp();
-
-    // compute lambda value
-    Int    NumberBFrames = (getGOPSize() - 1);
-    Int    SHIFT_QP = 12;
-    Double lambda_scale = 1.0 - Clip3(0.0, 0.5, 0.05 * (Double)NumberBFrames);
-    Double qpFactor;
-
-#if FULL_NBIT
-    Int    bitdepth_luma_qp_scale = 6 * (X265_DEPTH - 8);
-    Double qp_temp_orig = (Double)dQP - SHIFT_QP;
-#else
-    Int    bitdepth_luma_qp_scale = 0;
-#endif
-    Double qp_temp = (Double)qp + bitdepth_luma_qp_scale - SHIFT_QP;
-
-    // Case #1: I or P-slices (key-frame)
-    if (slice->getPOC() % 4 == 3)
-    {
-        qpFactor = 0.578;
-    }
-    else
-    {
-        qpFactor = 0.4624;
-    }
-    if (slice->getSliceType() == I_SLICE)
-    {
-        qpFactor = 0.57 * lambda_scale;
-    }
-    lambda = qpFactor * pow(2.0, qp_temp / 3.0);
-
-    // depth computation based on GOP size
-    Int depth = 0;
-    Int poc = slice->getPOC() % getGOPSize();
-    if (poc)
-    {
-        Int step = getGOPSize();
-        for (Int i = step >> 1; i >= 1; i >>= 1)
-        {
-            for (Int j = i; j < getGOPSize(); j += step)
-            {
-                if (j == poc)
-                {
-                    i = 0;
-                    break;
-                }
-            }
-
-            step >>= 1;
-            depth++;
-        }
-    }
-
-    if (depth > 0)
-    {
-#if FULL_NBIT
-        lambda *= Clip3(2.00, 4.00, (qp_temp_orig / 6.0));
-#else
-        lambda *= Clip3(2.00, 4.00, (qp_temp / 6.0));
-#endif
-    }
-
-    //qp = max(-m_sps.getQpBDOffsetY(), min(MAX_QP, (Int)floor(qpdouble + 0.5)));
-
-    if (slice->getSliceType() != I_SLICE)
-    {
-        lambda *= getLambdaModifier(0); // temporal layer 0
-    }
+    Int lambda = x265_lambda2_tab[qp] / 256;
 
     // for RDO
     // in RdCost there is only one lambda because the luma and chroma bits are not separated,
@@ -922,12 +856,11 @@
 
     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
+    weight = pow(2.0, (qp - g_chromaScale[qpc]) / 1.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
+    weight = pow(2.0, (qp - g_chromaScale[qpc]) / 1.0); // takes into account of the chroma qp mapping and chroma qp Offset
     curEncoder->setCrDistortionWeight(weight);
 
     // for RDOQ
diff -r 8b78d8cff9d8 -r fefa76460f05 source/common/common.cpp
--- a/source/common/common.cpp	Thu Aug 29 11:23:16 2013 -0500
+++ b/source/common/common.cpp	Fri Aug 30 00:16:58 2013 +0530
@@ -58,25 +58,29 @@
 {
     return _aligned_malloc(size, ALIGNBYTES);
 }
+
 void x265_free(void *ptr)
 {
     if (ptr) _aligned_free(ptr);
 }
-#else
+
+#else // if _WIN32
 void *x265_malloc(size_t size)
 {
     void *ptr;
+
     if (posix_memalign((void**)&ptr, ALIGNBYTES, size) == 0)
         return ptr;
     else
         return NULL;
 }
+
 void x265_free(void *ptr)
 {
     if (ptr) free(ptr);
 }
-#endif
 
+#endif // if _WIN32
 
 void x265_log(x265_param_t *param, int level, const char *fmt, ...)
 {
@@ -139,8 +143,8 @@
     param->bEnableTSkipFast = 1;
     param->bFrameAdaptive = X265_B_ADAPT_FAST;
     param->lookaheadDepth = 10;
-    param->rc.bitrate = 1000;
-    param->rc.rateTolerance = 1;
+    param->rc.bitrate = 500;
+    param->rc.rateTolerance = 0.1;
     param->rc.qCompress = 0.6;
     param->rc.ipFactor = 1.4f;
     param->rc.pbFactor = 1.3f;
@@ -155,8 +159,7 @@
     if (!profile)
         return 0;
     if (!strcmp(profile, "main"))
-    {
-    }
+    {}
     else if (!strcmp(profile, "main10"))
     {
 #if HIGH_BIT_DEPTH
@@ -249,7 +252,7 @@
             "Picture width must be an integer multiple of the specified chroma subsampling");
     CONFIRM(param->sourceHeight % TComSPS::getWinUnitY(CHROMA_420) != 0,
             "Picture height must be an integer multiple of the specified chroma subsampling");
-    CONFIRM(param->rc.rateControlMode < X265_RC_ABR || param->rc.rateControlMode > X265_RC_CRF,
+    CONFIRM(param->rc.rateControlMode<X265_RC_ABR || param->rc.rateControlMode> X265_RC_CRF,
             "Rate control mode is out of range");
 
     // max CU size should be power of 2
@@ -272,6 +275,7 @@
     uint32_t tuQTMinLog2Size = 2; //log2(4)
 
     static int once /* = 0 */;
+
     if (once)
     {
         if (param->maxCUSize != g_maxCUWidth)
@@ -332,7 +336,7 @@
         x265_log(param, X265_LOG_INFO, "Keyframe min / max           : open-gop\n");
     else
         x265_log(param, X265_LOG_INFO, "Keyframe min / max           : %d / %d\n", param->keyframeMin, param->keyframeMax);
-    switch(param->rc.rateControlMode)
+    switch (param->rc.rateControlMode)
     {
     case X265_RC_ABR:
         x265_log(param, X265_LOG_INFO, "Rate Control                 : ABR-%d kbps\n", param->rc.bitrate);
@@ -344,6 +348,7 @@
         x265_log(param, X265_LOG_INFO, "Rate Control                 : CRF-%d\n", param->rc.rateFactor);
         break;
     }
+
     if (param->cbQpOffset || param->crQpOffset)
     {
         x265_log(param, X265_LOG_INFO, "Cb/Cr QP Offset              : %d / %d\n", param->cbQpOffset, param->crQpOffset);
diff -r 8b78d8cff9d8 -r fefa76460f05 source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp	Thu Aug 29 11:23:16 2013 -0500
+++ b/source/encoder/ratecontrol.cpp	Fri Aug 30 00:16:58 2013 +0530
@@ -35,9 +35,9 @@
 #define MAX_FRAME_DURATION 1.00
 #define MIN_FRAME_DURATION 0.01
 
-#define CLIP_DURATION(f) Clip3( MIN_FRAME_DURATION, MAX_FRAME_DURATION, f)
+#define CLIP_DURATION(f) Clip3(MIN_FRAME_DURATION, MAX_FRAME_DURATION, f)
 
-/* The qscale - qp conversion is specified in the standards. 
+/* The qscale - qp conversion is specified in the standards.
 Approx qscale increases by 12%  with every qp increment */
 static inline double qScale2qp(double qScale)
 {
@@ -59,14 +59,14 @@
     rateTolerance = param->rc.rateTolerance;
     bitrate = param->rc.bitrate * 1000;
     frameDuration = 1.0 / param->frameRate;
-    rateControlMode = (RcMethod) (param->rc.rateControlMode);
+    rateControlMode = (RcMethod)(param->rc.rateControlMode);
     ncu = (int)((param->sourceHeight * param->sourceWidth) / pow((int)param->maxCUSize, 2.0));
     lastNonBPictType = -1;
     baseQp = param->rc.qp;
     qpm = qp = baseQp;
 
     // heuristics- encoder specific
-    qCompress = param->rc.qCompress; // tweak and test for x265. 
+    qCompress = param->rc.qCompress; // tweak and test for x265.
     ipFactor = param->rc.ipFactor;
     pbFactor = param->rc.pbFactor;
     totalBits = 0;
@@ -80,7 +80,7 @@
         accumPNorm = .01;
         accumPQp = (ABR_INIT_QP)*accumPNorm;
         /* estimated ratio that produces a reasonable QP for the first I-frame  - needs to be tweaked for x265*/
-        cplxrSum = .01 * pow(7.0e5, qCompress) * pow(ncu, 0.6);
+        cplxrSum = .01 * pow(7.0e5, qCompress) * pow(4 * ncu, 0.5);
         wantedBitsWindow = bitrate * frameDuration;
         lastNonBPictType = I_SLICE;
     }
@@ -90,7 +90,7 @@
     {
         lastQScaleFor[i] = qp2qScale(ABR_INIT_QP);
         lmin[i] = qp2qScale(MIN_QP);
-        lmax[i] = qp2qScale(MAX_QP+18);  // maxQP val in x264 = 51+18
+        lmax[i] = qp2qScale(MAX_QP + 18);  // maxQP val in x264 = 51+18
     }
 
     //qstep - value set as encoder specific.
@@ -110,8 +110,8 @@
     {
     case X265_RC_ABR:
         q = qScale2qp(rateEstimateQscale());
-        q = Clip3((double)MIN_QP, (double)(MAX_QP+18), q);
-        qp = Clip3(0, (MAX_QP+18), (int)(q + 0.5f));
+        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;
@@ -124,12 +124,12 @@
     default:
         assert(!"unimplemented");
         break;
-    }   
+    }
 
     if (frameType != B_SLICE)
         lastNonBPictType = frameType;
     /* set the final changed QP to slice structure*/
-    curFrame->setSliceQp(qp); 
+    curFrame->setSliceQp(qp);
 }
 
 void RateControl::accumPQpUpdate()


More information about the x265-devel mailing list