shithub: libvpx

Download patch

ref: 2faff64866111fa7257a1188b863f56bb206d09e
parent: 679a1c2f5a2396413079d3976d8a6e8f8170c72d
parent: 0e1068a4bdc55c28c7b4f5918056a1f807167f2c
author: Paul Wilkins <[email protected]>
date: Wed Oct 8 00:23:30 EDT 2014

Merge "Improve two pass VBR accuracy."

--- a/vp9/encoder/vp9_firstpass.c
+++ b/vp9/encoder/vp9_firstpass.c
@@ -39,9 +39,9 @@
 #define ARF_STATS_OUTPUT    0
 
 #define BOOST_FACTOR        12.5
-#define ERR_DIVISOR         100.0
-#define FACTOR_PT_LOW       0.5
-#define FACTOR_PT_HIGH      0.9
+#define ERR_DIVISOR         125.0
+#define FACTOR_PT_LOW       0.70
+#define FACTOR_PT_HIGH      0.90
 #define FIRST_PASS_Q        10.0
 #define GF_MAX_BOOST        96.0
 #define INTRA_MODE_PENALTY  1024
@@ -1057,7 +1057,7 @@
 
   // Adjustment based on actual quantizer to power term.
   const double power_term =
-      MIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.0125 + pt_low, pt_high);
+      MIN(vp9_convert_qindex_to_q(q, bit_depth) * 0.01 + pt_low, pt_high);
 
   // Calculate correction factor.
   if (power_term < 1.0)
@@ -1066,6 +1066,11 @@
   return fclamp(pow(error_term, power_term), 0.05, 5.0);
 }
 
+// Larger image formats are expected to be a little harder to code relatively
+// given the same prediction error score. This in part at least relates to the
+// increased size and hence coding cost of motion vectors.
+#define EDIV_SIZE_FACTOR 800
+
 static int get_twopass_worst_quality(const VP9_COMP *cpi,
                                      const FIRSTPASS_STATS *stats,
                                      int section_target_bandwidth) {
@@ -1079,8 +1084,10 @@
     const double section_err = stats->coded_error / stats->count;
     const double err_per_mb = section_err / num_mbs;
     const double speed_term = 1.0 + 0.04 * oxcf->speed;
+    const double ediv_size_correction = num_mbs / EDIV_SIZE_FACTOR;
     const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth <<
                                          BPER_MB_NORMBITS) / num_mbs;
+
     int q;
     int is_svc_upper_layer = 0;
     if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0)
@@ -1090,7 +1097,7 @@
     // content at the given rate.
     for (q = rc->best_quality; q < rc->worst_quality; ++q) {
       const double factor =
-          calc_correction_factor(err_per_mb, ERR_DIVISOR,
+          calc_correction_factor(err_per_mb, ERR_DIVISOR - ediv_size_correction,
                                  is_svc_upper_layer ? SVC_FACTOR_PT_LOW :
                                  FACTOR_PT_LOW, FACTOR_PT_HIGH, q,
                                  cpi->common.bit_depth);
@@ -1735,7 +1742,7 @@
       // bits to spare and are better with a smaller interval and smaller boost.
       // At high Q when there are few bits to spare we are better with a longer
       // interval to spread the cost of the GF.
-      active_max_gf_interval = 12 + MIN(4, (int_max_q / 32));
+      active_max_gf_interval = 12 + MIN(4, (int_max_q / 24));
       if (active_max_gf_interval > rc->max_gf_interval)
         active_max_gf_interval = rc->max_gf_interval;
     }
--- a/vp9/encoder/vp9_ratectrl.c
+++ b/vp9/encoder/vp9_ratectrl.c
@@ -177,6 +177,9 @@
   const double q = vp9_convert_qindex_to_q(qindex, bit_depth);
   int enumerator = frame_type == KEY_FRAME ? 2700000 : 1800000;
 
+  assert(correction_factor <= MAX_BPB_FACTOR &&
+         correction_factor >= MIN_BPB_FACTOR);
+
   // q based adjustment to baseline enumerator
   enumerator += (int)(enumerator * q) >> 12;
   return (int)(enumerator * correction_factor / q);
@@ -187,7 +190,8 @@
                               vpx_bit_depth_t bit_depth) {
   const int bpm = (int)(vp9_rc_bits_per_mb(frame_type, q, correction_factor,
                                            bit_depth));
-  return ((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS;
+  return MAX(FRAME_OVERHEAD_BITS,
+             (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
 }
 
 int vp9_rc_clamp_pframe_target_size(const VP9_COMP *const cpi, int target) {
@@ -410,7 +414,7 @@
                                                  rate_correction_factor,
                                                  cm->bit_depth);
   // Work out a size correction factor.
-  if (projected_size_based_on_q > 0)
+  if (projected_size_based_on_q > FRAME_OVERHEAD_BITS)
     correction_factor = (100 * cpi->rc.projected_frame_size) /
                             projected_size_based_on_q;