vp9: Refactor code for q adjustment in CBR mode.
authorMarco Paniconi <marpan@google.com>
Wed, 30 May 2018 20:55:28 +0000 (13:55 -0700)
committerMarco Paniconi <marpan@google.com>
Wed, 30 May 2018 21:58:48 +0000 (14:58 -0700)
Move the adjustment code to separate function.
Change-Id: I876b246a5c26095f262bb9a19f03d1f17077225d

vp9/encoder/vp9_aq_cyclicrefresh.c
vp9/encoder/vp9_aq_cyclicrefresh.h
vp9/encoder/vp9_ratectrl.c

index 3aaec2e..3b4fee7 100644 (file)
@@ -599,10 +599,11 @@ void vp9_cyclic_refresh_reset_resize(VP9_COMP *const cpi) {
   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;
   }
 }
index ce72fcb..f59f193 100644 (file)
@@ -139,7 +139,7 @@ static INLINE int cyclic_refresh_segment_id(int segment_id) {
     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"
index 2b2f17b..70b3a7a 100644 (file)
@@ -554,6 +554,28 @@ int vp9_rc_drop_frame(VP9_COMP *cpi) {
   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 @@ int vp9_rc_regulate_q(const VP9_COMP *cpi, int target_bits_per_frame,
   } 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;
 }