shithub: libvpx

Download patch

ref: d7a80012a06f67ee0130864b1f89e394778a1f02
parent: 7f7782bebdcb43708bda2f88e4ae21e4319bdf29
author: Marco Paniconi <marpan@google.com>
date: Wed May 30 09:55:28 EDT 2018

vp9: Refactor code for q adjustment in CBR mode.

Move the adjustment code to separate function.
Change-Id: I876b246a5c26095f262bb9a19f03d1f17077225d

--- a/vp9/encoder/vp9_aq_cyclicrefresh.c
+++ b/vp9/encoder/vp9_aq_cyclicrefresh.c
@@ -599,10 +599,11 @@
   cpi->refresh_alt_ref_frame = 1;
 }
 
-void vp9_cyclic_refresh_limit_q(CYCLIC_REFRESH *const cr, int prev_q, int *q) {
+void vp9_cyclic_refresh_limit_q(const VP9_COMP *cpi, int *q) {
+  CYCLIC_REFRESH *const cr = cpi->cyclic_refresh;
   // For now apply hard limit to frame-level decrease in q, if the cyclic
   // refresh is active (percent_refresh > 0).
-  if (cr->percent_refresh > 0 && prev_q - *q > 8) {
-    *q = prev_q - 8;
+  if (cr->percent_refresh > 0 && cpi->rc.q_1_frame - *q > 8) {
+    *q = cpi->rc.q_1_frame - 8;
   }
 }
--- a/vp9/encoder/vp9_aq_cyclicrefresh.h
+++ b/vp9/encoder/vp9_aq_cyclicrefresh.h
@@ -139,7 +139,7 @@
     return CR_SEGMENT_ID_BASE;
 }
 
-void vp9_cyclic_refresh_limit_q(CYCLIC_REFRESH *const cr, int prev_q, int *q);
+void vp9_cyclic_refresh_limit_q(const struct VP9_COMP *cpi, int *q);
 
 #ifdef __cplusplus
 }  // extern "C"
--- a/vp9/encoder/vp9_ratectrl.c
+++ b/vp9/encoder/vp9_ratectrl.c
@@ -554,6 +554,28 @@
   return 0;
 }
 
+static int adjust_q_cbr(const VP9_COMP *cpi, int q) {
+  // This makes sure q is between oscillating Qs to prevent resonance.
+  if (!cpi->rc.reset_high_source_sad &&
+      (!cpi->oxcf.gf_cbr_boost_pct ||
+       !(cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame)) &&
+      (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
+      cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
+    int qclamp = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
+                       VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
+    // If the previous frame had overshoot and the current q needs to increase
+    // above the clamped value, reduce the clamp for faster reaction to
+    // overshoot.
+    if (cpi->rc.rc_1_frame == -1 && q > qclamp)
+      q = (q + qclamp) >> 1;
+    else
+      q = qclamp;
+  }
+  if (cpi->oxcf.content == VP9E_CONTENT_SCREEN)
+    vp9_cyclic_refresh_limit_q(cpi, &q);
+  return q;
+}
+
 static double get_rate_correction_factor(const VP9_COMP *cpi) {
   const RATE_CONTROL *const rc = &cpi->rc;
   double rcf;
@@ -713,26 +735,8 @@
   } while (++i <= active_worst_quality);
 
   // Adjustment to q for CBR mode.
-  if (cpi->oxcf.rc_mode == VPX_CBR) {
-    // This makes sure q is between oscillating Qs to prevent resonance.
-    if (!cpi->rc.reset_high_source_sad &&
-        (!cpi->oxcf.gf_cbr_boost_pct ||
-         !(cpi->refresh_alt_ref_frame || cpi->refresh_golden_frame)) &&
-        (cpi->rc.rc_1_frame * cpi->rc.rc_2_frame == -1) &&
-        cpi->rc.q_1_frame != cpi->rc.q_2_frame) {
-      int qclamp = clamp(q, VPXMIN(cpi->rc.q_1_frame, cpi->rc.q_2_frame),
-                         VPXMAX(cpi->rc.q_1_frame, cpi->rc.q_2_frame));
-      // If the previous frame had overshoot and the current q needs to increase
-      // above the clamped value, reduce the clamp for faster reaction to
-      // overshoot.
-      if (cpi->rc.rc_1_frame == -1 && q > qclamp)
-        q = (q + qclamp) >> 1;
-      else
-        q = qclamp;
-    }
-    if (cpi->oxcf.content == VP9E_CONTENT_SCREEN)
-      vp9_cyclic_refresh_limit_q(cr, cpi->rc.q_1_frame, &q);
-  }
+  if (cpi->oxcf.rc_mode == VPX_CBR) return adjust_q_cbr(cpi, q);
+
   return q;
 }