[vlc-commits] demux: adaptative: change rate based algorithm

Francois Cartegnie git at videolan.org
Thu Nov 19 14:47:38 CET 2015


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Tue Nov 10 21:27:36 2015 +0100| [422cceae88e203d831ea6c0cc72fb929e030d6a7] | committer: Francois Cartegnie

demux: adaptative: change rate based algorithm

Drops the current long term average to a moving
average/vertical horizontal filter with an
observation window of 250ms.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=422cceae88e203d831ea6c0cc72fb929e030d6a7
---

 .../adaptative/logic/RateBasedAdaptationLogic.cpp  |   59 +++++++++++++++++---
 .../adaptative/logic/RateBasedAdaptationLogic.h    |   14 ++++-
 2 files changed, 62 insertions(+), 11 deletions(-)

diff --git a/modules/demux/adaptative/logic/RateBasedAdaptationLogic.cpp b/modules/demux/adaptative/logic/RateBasedAdaptationLogic.cpp
index 09d51ff..51d096c 100644
--- a/modules/demux/adaptative/logic/RateBasedAdaptationLogic.cpp
+++ b/modules/demux/adaptative/logic/RateBasedAdaptationLogic.cpp
@@ -37,13 +37,18 @@ using namespace adaptative::logic;
 
 RateBasedAdaptationLogic::RateBasedAdaptationLogic  (vlc_object_t *p_obj_, int w, int h) :
                           AbstractAdaptationLogic   (),
-                          bpsAvg(0), bpsRemainder(0), bpsSamplecount(0),
+                          bpsAvg(0),
                           currentBps(0)
 {
     width  = w;
     height = h;
     usedBps = 0;
+    dllength = 0;
     p_obj = p_obj_;
+    for(unsigned i=0; i<10; i++) window[i].bw = window[i].diff = 0;
+    window_idx = 0;
+    prevbps = 0;
+    dlsize = 0;
 }
 
 BaseRepresentation *RateBasedAdaptationLogic::getNextRepresentation(BaseAdaptationSet *adaptSet, BaseRepresentation *currep) const
@@ -71,26 +76,62 @@ BaseRepresentation *RateBasedAdaptationLogic::getNextRepresentation(BaseAdaptati
 
 void RateBasedAdaptationLogic::updateDownloadRate(size_t size, mtime_t time)
 {
-    if(unlikely(time == 0) || size < (HTTPChunkSource::CHUNK_SIZE>>1) )
+    if(unlikely(time == 0))
         return;
+    /* Accumulate up to observation window */
+    dllength += time;
+    dlsize += size;
 
-    size_t current = bpsRemainder + CLOCK_FREQ * size * 8 / time;
+    if(dllength < CLOCK_FREQ / 4)
+        return;
+
+    const size_t bps = CLOCK_FREQ * dlsize * 8 / dllength;
 
-    if (current >= bpsAvg)
+    /* set window value */
+    if(window[0].bw == 0)
     {
-        bpsAvg += (current - bpsAvg) / ++bpsSamplecount;
-        bpsRemainder = (current - bpsAvg) % bpsSamplecount;
+        for(unsigned i=0; i<TOTALOBS; i++) window[i].bw = bps;
     }
     else
     {
-        bpsAvg -= (bpsAvg - current) / ++bpsSamplecount;
-        bpsRemainder = (bpsAvg - current) % bpsSamplecount;
+        window_idx = (window_idx + 1) % TOTALOBS;
+        window[window_idx].bw = bps;
+        window[window_idx].diff = bps >= prevbps ? bps - prevbps : prevbps - bps;
     }
 
+    /* compute for deltamax */
+    size_t diffsum = 0;
+    size_t omin = SIZE_MAX;
+    size_t omax = 0;
+    for(unsigned i=0; i < TOTALOBS; i++)
+    {
+        /* Find max and min */
+        if(window[i].bw > omax)
+            omax = window[i].bw;
+        if(window[i].bw < omin)
+            omin = window[i].bw;
+        diffsum += window[i].diff;
+    }
+
+    /* Vertical Horizontal Filter / Moving Average
+     *
+     * Bandwith stability during observation window alters the alpha parameter
+     * and then defines how fast we adapt to current bandwidth */
+    const size_t deltamax = omax - omin;
+    double alpha = (diffsum) ? 0.33 * ((double)deltamax / diffsum) : 0.5;
+    bpsAvg = alpha * bpsAvg + (1.0 - alpha) * bps;
+
+    BwDebug(msg_Dbg(p_obj, "alpha1 %lf alpha0 %lf dmax %ld ds %ld", alpha,
+                    (double)deltamax / diffsum, deltamax, diffsum));
+    BwDebug(msg_Dbg(p_obj, "bw estimation bps %zu -> avg %zu",
+                            bps / 8192, bpsAvg / 8192));
+
     currentBps = bpsAvg * 3/4;
+    dlsize = dllength = 0;
+    prevbps = bps;
 
     BwDebug(msg_Info(p_obj, "Current bandwidth %zu KiB/s using %u%%",
-                    (bpsAvg / 8192), (bpsAvg) ? (unsigned)(usedBps * 100.0 / bpsAvg) : 0 ));
+                    (bpsAvg / 8192), (bpsAvg) ? (unsigned)(usedBps * 100.0 / bpsAvg) : 0));
 }
 
 void RateBasedAdaptationLogic::trackerEvent(const SegmentTrackerEvent &event)
diff --git a/modules/demux/adaptative/logic/RateBasedAdaptationLogic.h b/modules/demux/adaptative/logic/RateBasedAdaptationLogic.h
index 417c4df..e3d934b 100644
--- a/modules/demux/adaptative/logic/RateBasedAdaptationLogic.h
+++ b/modules/demux/adaptative/logic/RateBasedAdaptationLogic.h
@@ -45,11 +45,21 @@ namespace adaptative
                 int                     width;
                 int                     height;
                 size_t                  bpsAvg;
-                size_t                  bpsRemainder;
-                size_t                  bpsSamplecount;
                 size_t                  currentBps;
                 size_t                  usedBps;
                 vlc_object_t *          p_obj;
+
+                static const unsigned   TOTALOBS = 10;
+                struct
+                {
+                    size_t bw;
+                    size_t diff;
+                } window[TOTALOBS];
+                unsigned                window_idx;
+                size_t                  prevbps;
+
+                size_t                  dlsize;
+                mtime_t                 dllength;
         };
 
         class FixedRateAdaptationLogic : public AbstractAdaptationLogic



More information about the vlc-commits mailing list