]> granicus.if.org Git - libvpx/commitdiff
WIP sandbox/jkoleszar/new-rate-control
authorJohn Koleszar <jkoleszar@google.com>
Mon, 3 Oct 2011 15:09:07 +0000 (11:09 -0400)
committerJohn Koleszar <jkoleszar@google.com>
Wed, 5 Oct 2011 16:10:16 +0000 (12:10 -0400)
Change-Id: I55c5930495624afc180f12824634c5479e60d38c

vp8/common/onyxd.h
vp8/encoder/firstpass.c
vp8/encoder/onyx_if.c
vp8/encoder/onyx_int.h
vp8/encoder/ratectrl.c

index 08f1cca803e253a0f66a1189ac8211e933a160ef..d3e5c2fa750ad94c591a93bf1291815b9c7d162d 100644 (file)
@@ -18,6 +18,7 @@
 extern "C"
 {
 #endif
+#include "vpx/vpx_codec.h"
 #include "type_aliases.h"
 #include "vpx_scale/yv12config.h"
 #include "ppflags.h"
index 8559142c99891cf6922cb2fb0e9bab401c5083c9..36406b0723e654ca383c3fb09b6fc48203492c63 100644 (file)
@@ -357,33 +357,58 @@ static int frame_max_bits(VP8_COMP *cpi)
     int max_bits;
 
     // For CBR we need to also consider buffer fullness.
-    // If we are running below the optimal level then we need to gradually tighten up on max_bits.
     if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
     {
-        double buffer_fullness_ratio = (double)cpi->buffer_level / DOUBLE_DIVIDE_CHECK((double)cpi->oxcf.optimal_buffer_level);
+        max_bits = 2 * cpi->av_per_frame_bandwidth;
+        max_bits -= cpi->estimated_display_frame_size;
+        max_bits *= ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0);
+    }
+    // VBR
+    else
+    {
+        // For VBR base this on the bits and frames left plus the two_pass_vbrmax_section rate passed in by the user
+        max_bits = (int)(((double)cpi->twopass.bits_left / (cpi->twopass.total_stats->count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
+    }
+
+    // Trap case where we are out of bits
+    if (max_bits < 0)
+        max_bits = 0;
 
-        // For CBR base this on the target average bits per frame plus the maximum sedction rate passed in by the user
-        max_bits = (int)(cpi->av_per_frame_bandwidth * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
+    return max_bits;
+}
 
-        // If our buffer is below the optimum level
-        if (buffer_fullness_ratio < 1.0)
-        {
-            // The lower of max_bits / 4 or cpi->av_per_frame_bandwidth / 4.
-            int min_max_bits = ((cpi->av_per_frame_bandwidth >> 2) < (max_bits >> 2)) ? cpi->av_per_frame_bandwidth >> 2 : max_bits >> 2;
 
-            max_bits = (int)(max_bits * buffer_fullness_ratio);
+static int gf_group_max_bits(VP8_COMP *cpi)
+{
+    // Max allocation for a golden frame group
+    int max_bits;
 
-            if (max_bits < min_max_bits)
-                max_bits = min_max_bits;       // Lowest value we will set ... which should allow the buffer to refil.
+    // For CBR we need to also consider buffer fullness.
+    if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
+    {
+        max_bits = cpi->av_per_frame_bandwidth * cpi->baseline_gf_interval;
+        if (max_bits > cpi->oxcf.optimal_buffer_level)
+        {
+            max_bits -= cpi->oxcf.optimal_buffer_level;
+            max_bits += cpi->buffer_level;
         }
+        else
+        {
+            max_bits -= (cpi->estimated_display_frame_size
+                         - cpi->av_per_frame_bandwidth)
+                        * cpi->baseline_gf_interval;
+        }
+
+        max_bits *= ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0);
     }
-    // VBR
     else
     {
         // For VBR base this on the bits and frames left plus the two_pass_vbrmax_section rate passed in by the user
         max_bits = (int)(((double)cpi->twopass.bits_left / (cpi->twopass.total_stats->count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_vbrmax_section / 100.0));
+        max_bits *=  cpi->baseline_gf_interval;
     }
 
+
     // Trap case where we are out of bits
     if (max_bits < 0)
         max_bits = 0;
@@ -1600,7 +1625,7 @@ static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
     double abs_mv_in_out_accumulator = 0.0;
     double mod_err_per_mb_accumulator = 0.0;
 
-    int max_bits = frame_max_bits(cpi);     // Max for a single frame
+    int max_group_bits;
 
     unsigned int allow_alt_ref =
                     cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
@@ -1962,8 +1987,9 @@ static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
 
     // Clip cpi->twopass.gf_group_bits based on user supplied data rate
     // variability limit (cpi->oxcf.two_pass_vbrmax_section)
-    if (cpi->twopass.gf_group_bits > max_bits * cpi->baseline_gf_interval)
-        cpi->twopass.gf_group_bits = max_bits * cpi->baseline_gf_interval;
+    max_group_bits = gf_group_max_bits(cpi);
+    if (cpi->twopass.gf_group_bits > max_group_bits)
+        cpi->twopass.gf_group_bits = max_group_bits;
 
     // Reset the file position
     reset_fpf_position(cpi, start_pos);
@@ -2063,13 +2089,6 @@ static void define_gf_group(VP8_COMP *cpi, FIRSTPASS_STATS *this_frame)
             }
         }
 
-        // Apply an additional limit for CBR
-        if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
-        {
-            if (cpi->twopass.gf_bits > (cpi->buffer_level >> 1))
-                cpi->twopass.gf_bits = cpi->buffer_level >> 1;
-        }
-
         // Dont allow a negative value for gf_bits
         if (gf_bits < 0)
             gf_bits = 0;
index 35e187e545a5d2502751f33ff8140e31938bf849..117fe542a4303da6396c0434d16d64d504631a09 100644 (file)
@@ -1460,6 +1460,7 @@ static void init_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
     cpi->rolling_actual_bits          = cpi->av_per_frame_bandwidth;
     cpi->long_rolling_target_bits     = cpi->av_per_frame_bandwidth;
     cpi->long_rolling_actual_bits     = cpi->av_per_frame_bandwidth;
+    cpi->estimated_display_frame_size = cpi->av_per_frame_bandwidth;
 
     cpi->total_actual_bits            = 0;
     cpi->total_target_vs_actual       = 0;
@@ -1555,7 +1556,7 @@ void vp8_change_config(VP8_PTR ptr, VP8_CONFIG *oxcf)
         break;
     }
 
-    if (cpi->pass == 0)
+    if (cpi->pass == 0 && cpi->oxcf.end_usage != USAGE_STREAM_FROM_SERVER)
         cpi->auto_worst_q = 1;
 
     cpi->oxcf.worst_allowed_q = q_trans[oxcf->worst_allowed_q];
@@ -3197,6 +3198,68 @@ void loopfilter_frame(VP8_COMP *cpi, VP8_COMMON *cm)
 
 }
 
+
+#define MIN(x,y) (((x)<(y))?(x):(y))
+static void update_buffer_levels(VP8_COMP *cpi)
+{
+    int max_network_xfer, actual_network_xfter;
+    int64_t tmp;
+
+    /* Model the size of the displayed frame at t-n.
+     *
+     * The decoder lags the encoder by the starting_buffer_level, so we
+     * calculate an average per-frame bitrate over that window.
+     *
+     * It is calculated accordingly:
+     *
+     * A = Average Bits Per Frame In The Buffer
+     * P = New Frame Size
+     * N = Number of bits in the buffer
+     * W = Weight of this frame
+     *
+     * We recalculate the average as so:
+     *      (N-W)*A + W*P
+     * A' = -------------
+     *            N
+     */
+    tmp = (cpi->oxcf.starting_buffer_level - cpi->av_per_frame_bandwidth)
+          * cpi->estimated_display_frame_size;
+    tmp += cpi->av_per_frame_bandwidth * cpi->projected_frame_size;
+    cpi->estimated_display_frame_size = tmp / cpi->oxcf.starting_buffer_level;
+
+
+    /* Update the network buffer model (leaky bucket) */
+    max_network_xfer = cpi->av_per_frame_bandwidth;
+    actual_network_xfter = MIN(max_network_xfer, cpi->network_buffer_level);
+    cpi->network_buffer_level += cpi->projected_frame_size;
+    cpi->network_buffer_level -= actual_network_xfter;
+
+    if(cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
+    {
+        /* In CBR mode, the rate at which the buffer is refilled is
+         * constrained by the network bandwidth and the actual number of
+         * bits that have previously been transmitted. This is modeling
+         * the live streaming case.
+         */
+
+         /* Update the client buffer model (leaky bucket) */
+         cpi->client_buffer_level += actual_network_xfter;
+         if (cpi->total_byte_count > cpi->oxcf.starting_buffer_level / 8)
+             cpi->client_buffer_level -= cpi->estimated_display_frame_size;
+
+         /* The control variable is the buffer on the client's side */
+         cpi->buffer_level = cpi->client_buffer_level;
+    }
+    else
+    {
+        /* In VBR mode, the available bandwidth is essentially unlimited
+         * so the buffer level can grow and be consumed without bound.
+         */
+        cpi->buffer_level = cpi->bits_off_target;
+    }
+}
+
+
 static void encode_frame_to_data_rate
 (
     VP8_COMP *cpi,
@@ -3443,7 +3506,8 @@ static void encode_frame_to_data_rate
     // For CBR if the buffer reaches its maximum level then we can no longer
     // save up bits for later frames so we might as well use them up
     // on the current frame.
-    if ((cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
+    if (cpi->pass == 2
+        && (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) &&
         (cpi->buffer_level >= cpi->oxcf.optimal_buffer_level) && cpi->buffered_mode)
     {
         int Adjustment = cpi->active_worst_quality / 4;       // Max adjustment is 1/4
@@ -3534,6 +3598,10 @@ static void encode_frame_to_data_rate
         }
         else
         {
+            if(cpi->pass != 2)
+                Q = cpi->auto_worst_q?
+                    cpi->active_worst_quality:cpi->avg_frame_qindex;
+
             cpi->active_best_quality = inter_minq[Q];
 
             // For the constant/constrained quality mode we dont want
@@ -3834,15 +3902,17 @@ static void encode_frame_to_data_rate
             (cpi->active_worst_quality < cpi->worst_quality)      &&
             (cpi->projected_frame_size > frame_over_shoot_limit))
         {
-            int over_size_percent = ((cpi->projected_frame_size - frame_over_shoot_limit) * 100) / frame_over_shoot_limit;
+            /* step down active_worst_quality such that the corresponding
+             * active_best_quality will be equal to the current
+             * active_worst_quality + 1. Once the limit on active_best_quality
+             * is reached, active_worst_quality will equal worst_quality.
+             */
+            int i;
 
-            // If so is there any scope for relaxing it
-            while ((cpi->active_worst_quality < cpi->worst_quality) && (over_size_percent > 0))
-            {
-                cpi->active_worst_quality++;
-                top_index = cpi->active_worst_quality;
-                over_size_percent = (int)(over_size_percent * 0.96);        // Assume 1 qstep = about 4% on frame size.
-            }
+            for(i=cpi->active_worst_quality; i<cpi->worst_quality; i++)
+                if(inter_minq[i] >= cpi->active_worst_quality + 1)
+                    break;
+            cpi->active_worst_quality = i;
 
             // If we have updated the active max Q do not call vp8_update_rate_correction_factors() this loop.
             active_worst_qchanged = TRUE;
@@ -4230,10 +4300,9 @@ static void encode_frame_to_data_rate
 
     // Update the buffer level variable.
     // Non-viewable frames are a special case and are treated as pure overhead.
-    if ( !cm->show_frame )
-        cpi->bits_off_target -= cpi->projected_frame_size;
-    else
-        cpi->bits_off_target += cpi->av_per_frame_bandwidth - cpi->projected_frame_size;
+    if ( cm->show_frame )
+        cpi->bits_off_target += cpi->av_per_frame_bandwidth;
+    cpi->bits_off_target -= cpi->projected_frame_size;
 
     // Rolling monitors of whether we are over or underspending used to help regulate min and Max Q in two pass.
     cpi->rolling_target_bits = ((cpi->rolling_target_bits * 3) + cpi->this_frame_target + 2) / 4;
@@ -4247,7 +4316,7 @@ static void encode_frame_to_data_rate
     // Debug stats
     cpi->total_target_vs_actual += (cpi->this_frame_target - cpi->projected_frame_size);
 
-    cpi->buffer_level = cpi->bits_off_target;
+    update_buffer_levels(cpi);
 
     // Update bits left to the kf and gf groups to account for overshoot or undershoot on these frames
     if (cm->frame_type == KEY_FRAME)
index 968ebf83312bf431eea91db7cb38c9668ea13411..3818ca544a7f62ebace0a3bf503eb2c8574d5b7c 100644 (file)
@@ -348,6 +348,8 @@ typedef struct VP8_COMP
     int per_frame_bandwidth;          // Current section per frame bandwidth target
     int av_per_frame_bandwidth;        // Average frame size target for clip
     int min_frame_bandwidth;          // Minimum allocation that should be used for any frame
+    int estimated_display_frame_size; // Recent average bitrate
+
     int inter_frame_target;
     double output_frame_rate;
     int64_t last_time_stamp_seen;
@@ -369,6 +371,8 @@ typedef struct VP8_COMP
 
     int buffer_level;
     int bits_off_target;
+    int network_buffer_level;
+    int client_buffer_level;
 
     int rolling_target_bits;
     int rolling_actual_bits;
index 46e1d9dd9efc298612826c0cf9f35fb84fc485f2..a557d90d58bdce3d9f9147fc6475f200633032dd 100644 (file)
@@ -608,7 +608,7 @@ static void calc_pframe_target_size(VP8_COMP *cpi)
     int min_frame_target;
     int Adjustment;
 
-    min_frame_target = 0;
+    min_frame_target = 1;
 
     if (cpi->pass == 2)
     {
@@ -617,9 +617,11 @@ static void calc_pframe_target_size(VP8_COMP *cpi)
         if (min_frame_target < (cpi->av_per_frame_bandwidth >> 5))
             min_frame_target = cpi->av_per_frame_bandwidth >> 5;
     }
-    else if (min_frame_target < cpi->per_frame_bandwidth / 4)
-        min_frame_target = cpi->per_frame_bandwidth / 4;
-
+    else
+    {
+        if (min_frame_target < cpi->per_frame_bandwidth / 4)
+            min_frame_target = cpi->per_frame_bandwidth / 4;
+    }
 
     // Special alt reference frame case
     if (cpi->common.refresh_alt_ref_frame)
@@ -776,6 +778,52 @@ static void calc_pframe_target_size(VP8_COMP *cpi)
     // One Pass specific code
     if (cpi->pass == 0)
     {
+#if 1
+        int one_pct_bits, bits_off_optimum, pct_off_optimum;
+        
+        one_pct_bits = 1 + cpi->oxcf.optimal_buffer_level / 100;
+        bits_off_optimum = cpi->buffer_level - cpi->oxcf.optimal_buffer_level;
+        pct_off_optimum = bits_off_optimum / one_pct_bits;
+        
+        /* Clamp error to limit response agressiveness */
+        if (pct_off_optimum > cpi->oxcf.over_shoot_pct)
+            pct_off_optimum = cpi->oxcf.over_shoot_pct;
+        else if (pct_off_optimum < -cpi->oxcf.under_shoot_pct)
+            pct_off_optimum = -cpi->oxcf.under_shoot_pct;
+
+printf("%d\t%d\t%d\t",cpi->client_buffer_level, cpi->network_buffer_level, cpi->buffer_level);
+printf("%d\t%d\t%d\t",cpi->oxcf.optimal_buffer_level, bits_off_optimum, pct_off_optimum);
+printf("%d\t",cpi->this_frame_target);
+
+        if (cpi->network_buffer_level > cpi->estimated_display_frame_size)
+        {
+            /* Network buffer is full, the client buffer will be filled at
+             * the maximum rate. To increase the buffer level, we must reduce
+             * the average frame size. To decrease the buffer level, we must
+             * increase the recent bitrate.
+             */
+            pct_off_optimum *= -1;
+        }
+        else
+        {
+            /* Network buffer is empty. To increase the buffer level,
+             * we must send data faster than the network. To decrease the
+             * buffer level, we must send less than the recent bitrate.
+             */
+            cpi->this_frame_target = cpi->estimated_display_frame_size;
+        }
+        
+        cpi->this_frame_target -= (cpi->this_frame_target * pct_off_optimum)
+                                  / 125;
+
+printf("%d\n",cpi->this_frame_target);
+
+
+                    cpi->active_worst_quality = cpi->worst_quality;
+            // Worst quality obviously must not be better than best quality
+            if (cpi->active_worst_quality <= cpi->active_best_quality)
+                cpi->active_worst_quality = cpi->active_best_quality + 1;
+#else
         // Adapt target frame size with respect to any buffering constraints:
         if (cpi->buffered_mode)
         {
@@ -943,6 +991,7 @@ static void calc_pframe_target_size(VP8_COMP *cpi)
         {
             cpi->active_worst_quality = cpi->cq_target_quality;
         }
+#endif
     }
 
     // Test to see if we have to drop a frame
@@ -1112,6 +1161,35 @@ static void calc_pframe_target_size(VP8_COMP *cpi)
 
         }
     }
+
+#if 0
+    if (cpi->pass==0
+        && cpi->common.refresh_golden_frame
+        && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
+        int64_t adjust;
+
+        /*
+        frames_in_buffer = cpi->oxcf.maximum_buffer_size
+                           / cpi->av_per_frame_bandwidth;
+        gf_in_buffer = frames_in_buffer /
+                       cpi->frames_till_gf_update_due;
+        overshoot_per_gf = cpi->accumulated_overshoot / gf_in_buffer;
+
+        */
+
+        adjust = cpi->accumulated_overshoot;
+        adjust *= cpi->frames_till_gf_update_due + 1;
+        adjust *= cpi->av_per_frame_bandwidth;
+        adjust /= cpi->oxcf.maximum_buffer_size;
+
+        if (adjust > (cpi->this_frame_target - min_frame_target))
+            adjust = (cpi->this_frame_target - min_frame_target);
+        else if (adjust < 0)
+            adjust = 0;
+
+        cpi->this_frame_target -= adjust;
+    }
+#endif
 }