[x265] [PATCH] uncrustify, code cleanup , continue porting x264 rate control to x265

aarthi at multicorewareinc.com aarthi at multicorewareinc.com
Sun Aug 11 19:27:17 CEST 2013


# HG changeset patch
# User Aarthi<aarthi at multicorewareinc.com>
# Date 1376242010 -19800
#      Sun Aug 11 22:56:50 2013 +0530
# Node ID 8e235134c5527c83acf9991d5fee328ef0d92d1b
# Parent  88e0c10bf47b9c9629a38b402a87bc1011b7d49f
uncrustify, code cleanup , continue porting  x264 rate control to x265.

diff -r 88e0c10bf47b -r 8e235134c552 source/encoder/ratecontrol.cpp
--- a/source/encoder/ratecontrol.cpp	Fri Aug 09 17:49:29 2013 -0500
+++ b/source/encoder/ratecontrol.cpp	Sun Aug 11 22:56:50 2013 +0530
@@ -18,7 +18,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
  *
- * This program is also available under a commercial proprietary license.
+ * This program is also available under a commercial proprietary license.oh
  * For more information, contact us at licensing at multicorewareinc.com.
  *****************************************************************************/
 
@@ -26,14 +26,38 @@
 
 using namespace x265;
 
-void RateControl::rateControlInit(TComSlice* frame, float curFrameDuration, x265_param_t *param)
+RateControl::RateControl(x265_param_t * param)
+{
+    keyFrameInterval = param->keyframeInterval;
+    fps = param->frameRate;
+    bframes = param->bframes;
+    //TODO : set default value here,later can get from  param as done in x264?
+    rateTolerance = 1;
+    //TODO : introduce bitrate var in param and cli options
+    bitrate = param->bitrate * 1000;
+    fps = param->frameRate;
+    isAbrEnabled = true; // set from param later.
+    ncu = (param->sourceHeight * param->sourceWidth) / pow(2, param->maxCUSize);
+    lastNonBPictType = -1;
+    //TODO : need  to confirm default val if mbtree is disabled
+    qCompress = 1;
+    if (isAbrEnabled)
+    {
+        //TODO : confirm this value. obtained it from x264 when crf is disabled , abr enabled.
+#define ABR_INIT_QP (24  + 6 * (param->internalBitDepth - 8));
+        accumPNorm = .01;
+        accumPQp = (ABR_INIT_QP)*accumPNorm;
+        /* estimated ratio that produces a reasonable QP for the first I-frame */
+        cplxrSum = .01 * pow(7.0e5, qCompress) * pow(ncu, 0.5);
+        wantedBitsWindow = 1.0 * bitrate / fps;
+        lastNonBPictType = I_SLICE;
+    }
+}
+
+void RateControl::rateControlInit(TComSlice* frame)
 {
     curFrame = curFrame;
     frameType = curFrame->getSliceType();
-    frameNum = param->frameRate;
-    keyFrameInterval = param->keyframeInterval;
-    fps = param->frameRate;
-    bframes = param->bframes;
 }
 
 void RateControl::rateControlStart(LookaheadFrame *lFrame)
@@ -42,44 +66,44 @@
     float q;
 
     //Always enabling ABR
-    if (1) // rc->b_abr )
+    if (isAbrEnabled)
     {
         q = qScale2qp(rateEstimateQscale(lFrame));
     }
-    q = Clip3(q, MIN_QP, MAX_QP);
+    q = Clip3((int)q, MIN_QP, MAX_QP);
 
-    qp = x265_clip3(q + 0.5f, 0, MAX_QP);
+    qp = Clip3((int)(q + 0.5f), 0, MAX_QP);
 
     qpm = q;
     if (rce)
-        rce->new_qp = qp;
+        rce->newQp = qp;
 
-    accum_p_qp_update();
+    accumPQpUpdate();
 
     if (frameType != B_SLICE)
-        last_non_b_pict_type = frameType;
+        lastNonBPictType = frameType;
 }
 
-void RateControl::accum_p_qp_update()
+void RateControl::accumPQpUpdate()
 {
     //x264_ratecontrol_t *rc = h->rc;
-    accum_p_qp   *= .95;
-    accum_p_norm *= .95;
-    accum_p_norm += 1;
+    accumPQp   *= .95;
+    accumPNorm *= .95;
+    accumPNorm += 1;
     if (frameType == I_SLICE)
-        accum_p_qp += qpm + ip_offset;
+        accumPQp += qpm + ipOffset;
     else
-        accum_p_qp += qpm;
+        accumPQp += qpm;
 }
 
 float RateControl::rateEstimateQscale(LookaheadFrame *lframe)
 {
     float q;
     // ratecontrol_entry_t rce = UNINIT(rce);
-    int pict_type = frameType;
-    int64_t total_bits = 0; //CHECK:for ABR
+    int pictType = frameType;
+    int64_t totalBits = 0; //CHECK:for ABR
 
-    if (pict_type == B_SLICE)
+    if (pictType == B_SLICE)
     {
         /* B-frames don't have independent ratecontrol, but rather get the
          * average QP of the two adjacent P-frames + an offset */
@@ -94,7 +118,7 @@
         float q1 = h->fref_nearest[1]->f_qp_avg_rc;
 
         if (i0 && i1)
-            q = (q0 + q1) / 2 + ip_offset;
+            q = (q0 + q1) / 2 + ipOffset;
         else if (i0)
             q = q1;
         else if (i1)
@@ -103,16 +127,16 @@
             q = (q0 * dt1 + q1 * dt0) / (dt0 + dt1);
 
         if (h->fenc->b_kept_as_ref)
-            q += pb_offset / 2;
+            q += pbOffset / 2;
         else
-            q += pb_offset;
+            q += pbOffset;
 
-        qp_novbv = q;
+        qpNoVbv = q;
         return qp2qScale(q);
     }
     else
     {
-        double abr_buffer = 2 * rate_tolerance * bitrate;
+        double abrBuffer = 2 * rateTolerance * bitrate;
 
         /* 1pass ABR */
 
@@ -126,81 +150,81 @@
          * tradeoff between quality and bitrate precision. But at large
          * tolerances, the bit distribution approaches that of 2pass. */
 
-        double wanted_bits, overflow = 1;
+        double wantedBits, overflow = 1;
 
-        last_satd = 0;     //need to get this from lookahead  //x264_rc_analyse_slice( h );
-        short_term_cplxsum *= 0.5;
-        short_term_cplxcount *= 0.5;
+        lastSatd = 0;     //need to get this from lookahead  //x264_rc_analyse_slice( h );
+        shortTermCplxSum *= 0.5;
+        shortTermCplxCount *= 0.5;
         //TODO:need to get the duration for each frame
         //short_term_cplxsum += last_satd / (CLIP_DURATION(h->fenc->f_duration) / BASE_FRAME_DURATION);
-        short_term_cplxcount++;
+        shortTermCplxCount++;
 
-        rce->p_count = ncu;
+        rce->pCount = ncu;
 
-        rce->pict_type = pict_type;
+        rce->pictType = pictType;
 
         //TODO:wanted_bits_window, fps , h->fenc->i_reordered_pts, h->i_reordered_pts_delay, h->fref_nearest[0]->f_qp_avg_rc, h->param.rc.f_ip_factor, h->sh.i_type
         //need to checked where it is initialized
-        q = getQScale(rce, wanted_bits_window / cplxr_sum, frameNum);
+        q = getQScale(rce, wantedBitsWindow / cplxrSum, fps);
 
         /* ABR code can potentially be counterproductive in CBR, so just don't bother.
          * Don't run it if the frame complexity is zero either. */
-        if ( /*!rcc->b_vbv_min_rate && */ last_satd)
+        if ( /*!rcc->b_vbv_min_rate && */ lastSatd)
         {
             // TODO: need to check the thread_frames
-            int i_frame_done = frameNum + 1 - h->i_thread_frames;
-            double time_done = i_frame_done / fps;
-            if (i_frame_done > 0)
+            int iFrameDone = fps + 1 - h->i_thread_frames;
+            double timeDone = iFrameDone / fps;
+            if (iFrameDone > 0)
             {
                 //time_done = ((double)(h->fenc->i_reordered_pts - h->i_reordered_pts_delay)) * h->param.i_timebase_num / h->param.i_timebase_den;
-                time_done = ((double)(h->fenc->i_reordered_pts - h->i_reordered_pts_delay)) * (1 / fps);
+                timeDone = ((double)(h->fenc->i_reordered_pts - h->i_reordered_pts_delay)) * (1 / fps);
             }
-            wanted_bits = time_done * bitrate;
-            if (wanted_bits > 0)
+            wantedBits = timeDone * bitrate;
+            if (wantedBits > 0)
             {
-                abr_buffer *= X265_MAX(1, sqrt(time_done));
-                overflow = x265_clip3f(1.0 + (total_bits - wanted_bits) / abr_buffer, .5, 2);
+                abrBuffer *= X265_MAX(1, sqrt(timeDone));
+                overflow = Clip3(1.0 + (totalBits - wantedBits) / abrBuffer, .5, 2.0);
                 q *= overflow;
             }
         }
 
-        if (pict_type == I_SLICE && keyFrameInterval > 1
+        if (pictType == I_SLICE && keyFrameInterval > 1
             /* should test _next_ pict type, but that isn't decided yet */
-            && last_non_b_pict_type != I_SLICE)
+            && lastNonBPictType != I_SLICE)
         {
-            q = qp2qScale(accum_p_qp / accum_p_norm);
+            q = qp2qScale(accumPQp / accumPNorm);
             q /= fabs(h->param.rc.f_ip_factor);
         }
-        else if (frameNum > 0)
+        else if (fps > 0)
         {
             if (1)     //assume that for ABR this is always enabled h->param.rc.i_rc_method != X264_RC_CRF )
             {
                 /* Asymmetric clipping, because symmetric would prevent
                  * overflow control in areas of rapidly oscillating complexity */
-                double lmin = last_qscale_for[pict_type] / lstep;
-                double lmax = last_qscale_for[pict_type] * lstep;
-                if (overflow > 1.1 && frameNum > 3)
+                float lmin = lastQScaleFor[pictType] / lstep;
+                float lmax = lastQScaleFor[pictType] * lstep;
+                if (overflow > 1.1 && fps > 3)
                     lmax *= lstep;
                 else if (overflow < 0.9)
                     lmin /= lstep;
 
-                q = x265_clip3f(q, lmin, lmax);
+                q = Clip3(q, lmin, lmax);
             }
         }
 
-        qp_novbv = qScale2qp(q);
+        qpNoVbv = qScale2qp(q);
 
         //FIXME use get_diff_limited_q() ?
         //q = clip_qscale( h, pict_type, q );
-        double lmin_1 = lmin[pict_type];
-        double lmax_1 = lmax[pict_type];
-        x265_clip3f(q, lmin_1, lmax_1);
+        float lmin1 = lmin[pictType];
+        float lmax1 = lmax[pictType];
+        Clip3(q, lmin1, lmax1);
 
-        last_qscale_for[pict_type] =
-            last_qscale = q;
+        lastQScaleFor[pictType] =
+            lastQScale = q;
 
-        if (!frameNum == 0)
-            last_qscale_for[P_SLICE] = q * fabs(h->param.rc.f_ip_factor);
+        if (!fps == 0)
+            lastQScaleFor[P_SLICE] = q * fabs(h->param.rc.f_ip_factor);
 
         //this is for VBV stuff
         //frame_size_planned = predict_size( &pred[h->sh.i_type], q, last_satd );
@@ -212,22 +236,22 @@
 /**
  * modify the bitrate curve from pass1 for one frame
  */
-double RateControl::getQScale(RateControlEntry *rce, double rate_factor, int frame_num)
+double RateControl::getQScale(RateControlEntry *rce, double rateFactor)
 {
     double q;
 
     //x264_zone_t *zone = get_zone( h, frame_num );
 
-    q = pow(rce->blurred_complexity, 1 - qcompress);
+    q = pow(rce->blurredComplexity, 1 - qCompress);
 
     // avoid NaN's in the rc_eq
-    if (rce->tex_bits + rce->mv_bits == 0)
-        q = last_qscale_for[rce->pict_type];
+    if (rce->texBits + rce->mvBits == 0)
+        q = lastQScaleFor[rce->pictType];
     else
     {
-        last_rceq = q;
-        q /= rate_factor;
-        last_qscale = q;
+        lastRceq = q;
+        q /= rateFactor;
+        lastQScale = q;
     }
 
     /*TODO: need to check zone is required */
diff -r 88e0c10bf47b -r 8e235134c552 source/encoder/ratecontrol.h
--- a/source/encoder/ratecontrol.h	Fri Aug 09 17:49:29 2013 -0500
+++ b/source/encoder/ratecontrol.h	Sun Aug 11 22:56:50 2013 +0530
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * Copyright (C) 2013 x265 project
  *
- * Authors: Sumalatha <sumalatha at multicorewareinc.com>
- *          Aarthi <aarthi at multicorewareinc.com>
+ * Authors: Sumalatha Polureddy <sumalatha at multicorewareinc.com>
+ *          Aarthi Priya Thirumalai <aarthi at multicorewareinc.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -27,19 +27,18 @@
 
 #include <stdint.h>
 #include "TLibEncoder/TEncTop.h"
-
+#include "TLibCommon/TComRom.h"
 #include "math.h"
 
 namespace x265 {
-
 struct RateControlEntry
 {
-    int pict_type;
-    int p_count;
-    int new_qp;
-    int tex_bits;
-    int mv_bits;
-    float blurred_complexity;
+    int pictType;
+    int pCount;
+    int newQp;
+    int texBits;
+    int mvBits;
+    float blurredComplexity;
 };
 
 struct Predictor
@@ -61,8 +60,9 @@
     TComSlice *curFrame;        /* all info abt the current frame */
     SliceType frameType;        /* Current frame type */
     float frameDuration;        /* current frame duration in seconds */
-    int frameNum;               /* current frame number TODO: need to initaialize in init */
+    int fps;          /* current frame number TODO: need to initaialize in init */
     int keyFrameInterval;       /* TODO: need to initialize in init */
+    bool isAbrEnabled;
 
     /* current frame */
     RateControlEntry *rce;
@@ -70,45 +70,45 @@
     float qpm;                  /* qp for current macroblock: precise float for AQ */
 
     double bitrate;
-    double rate_tolerance;
-    double qcompress;
+    double rateTolerance;
+    double qCompress;
+    int bframes;
+    /* ABR stuff */
+    int    lastSatd;
+    double lastRceq;
+    double cplxrSum;           /* sum of bits*qscale/rceq */
+    double expectedBitsSum;   /* sum of qscale2bits after rceq, ratefactor, and overflow, only includes finished frames */
+    int64_t fillerBitsSum;    /* sum in bits of finished frames' filler data */
+    double wantedBitsWindow;  /* target bitrate * window */
+    double cbrDecay;
+    double shortTermCplxSum;
+    double shortTermCplxCount;
+    double rateFactorConstant;
+    double ipOffset;
+    double pbOffset;
 
-    /* ABR stuff */
-    int    last_satd;
-    double last_rceq;
-    double cplxr_sum;           /* sum of bits*qscale/rceq */
-    double expected_bits_sum;   /* sum of qscale2bits after rceq, ratefactor, and overflow, only includes finished frames */
-    int64_t filler_bits_sum;    /* sum in bits of finished frames' filler data */
-    double wanted_bits_window;  /* target bitrate * window */
-    double cbr_decay;
-    double short_term_cplxsum;
-    double short_term_cplxcount;
-    double rate_factor_constant;
-    double ip_offset;
-    double pb_offset;
+    int lastNonBPictType;
+    double accumPQp;          /* for determining I-frame quant */
+    double accumPNorm;
+    double lastQScale;
+    double lastQScaleFor[3];  /* last qscale for a specific pict type, used for max_diff & ipb factor stuff */
 
-    int last_non_b_pict_type;
-    double accum_p_qp;          /* for determining I-frame quant */
-    double accum_p_norm;
-    double last_qscale;
-    double last_qscale_for[3];  /* last qscale for a specific pict type, used for max_diff & ipb factor stuff */
-
-    double fps;
     double lstep;
-    float qp_novbv;             /* QP for the current frame if 1-pass VBV was disabled. */
+    float qpNoVbv;             /* QP for the current frame if 1-pass VBV was disabled. */
     double lmin[3];             /* min qscale by frame type */
     double lmax[3];
-    double frame_size_planned;
+    double frameSizePlanned;
 
-    void rateControlInit(TComSlice* frame, float dur, x265_param_t *param); // to be called for each frame to set the reqired parameters for rateControl.
+    RateControl(x265_param_t * param);    // constructor for initializing values for ratecontrol vars
+    void rateControlInit(TComSlice* frame);   // to be called for each frame to set the reqired parameters for rateControl.
     void rateControlStart(LookaheadFrame* lframe);                          // to be called for each frame to process RateCOntrol and set QP
     float rateEstimateQscale(LookaheadFrame* lframe);                       // main logic for calculating QP based on ABR
-    void accum_p_qp_update();
-    double getQScale(RateControlEntry *rce, double rate_factor, int frame_num);
+    void accumPQpUpdate();
+    double getQScale(RateControlEntry *rce, double rateFactor);
 
-    float qScale2qp(float qscale)
+    float qScale2qp(float qScale)
     {
-        return 12.0f + 6.0f * logf(qscale / 0.85f);
+        return 12.0f + 6.0f * logf(qScale / 0.85f);
     }
 
     float qp2qScale(float qp)


More information about the x265-devel mailing list