From 9afb6700c20d1aae858f6e0177b40c3e1682b88c Mon Sep 17 00:00:00 2001
From: Paul Wilkins <paulwilkins@google.com>
Date: Thu, 4 Apr 2013 17:40:39 +0100
Subject: [PATCH] Adjust q range

Skip Q values between the q.0 mode and a real q of
2.0 as these are not valuable from an RD perspective.

Change-Id: I110c4858c57f97315953f4d88a2596d4764360df
---
 vp9/common/vp9_quant_common.c | 15 ++++++++++++---
 vp9/encoder/vp9_onyx_if.c     |  5 +++++
 vp9/encoder/vp9_quantize.c    |  4 ----
 3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/vp9/common/vp9_quant_common.c b/vp9/common/vp9_quant_common.c
index 5907b4f58f..295c8e7386 100644
--- a/vp9/common/vp9_quant_common.c
+++ b/vp9/common/vp9_quant_common.c
@@ -15,7 +15,7 @@
 static int16_t dc_qlookup[QINDEX_RANGE];
 static int16_t ac_qlookup[QINDEX_RANGE];
 
-#define ACDC_MIN 4
+#define ACDC_MIN 8
 
 // TODO(dkovalev) move to common and reuse
 static double poly3(double a, double b, double c, double d, double x) {
@@ -25,10 +25,19 @@ static double poly3(double a, double b, double c, double d, double x) {
 void vp9_init_quant_tables() {
   int i, val = 4;
 
-  for (i = 0; i < QINDEX_RANGE; i++) {
+  // A "real" q of 1.0 forces lossless mode.
+  // In practice non lossless Q's between 1.0 and 2.0 (represented here by
+  // integer values from 5-7 give poor rd results (lower psnr and often
+  // larger size than the lossless encode. To block out those "not very useful"
+  // values we increment the ac and dc q lookup values by 4 after position 0.
+  ac_qlookup[0] = val;
+  dc_qlookup[0] = val;
+  val += 4;
+
+  for (i = 1; i < QINDEX_RANGE; i++) {
     const int ac_val = val;
 
-    val = (int)(val * 1.02);
+    val = (int)(val * 1.01975);
     if (val == ac_val)
       ++val;
 
diff --git a/vp9/encoder/vp9_onyx_if.c b/vp9/encoder/vp9_onyx_if.c
index ffee34eb72..a47758bb73 100644
--- a/vp9/encoder/vp9_onyx_if.c
+++ b/vp9/encoder/vp9_onyx_if.c
@@ -161,6 +161,11 @@ static int calculate_minq_index(double maxq,
   const double minqtarget = MIN(((x3 * maxq + x2) * maxq + x1) * maxq + c,
                                 maxq);
 
+  // Special case handling to deal with the step from q2.0
+  // down to lossless mode represented by q 1.0.
+  if (minqtarget <= 2.0)
+    return 0;
+
   for (i = 0; i < QINDEX_RANGE; i++) {
     if (minqtarget <= vp9_convert_qindex_to_q(i))
       return i;
diff --git a/vp9/encoder/vp9_quantize.c b/vp9/encoder/vp9_quantize.c
index 4ed8f63269..a650b06998 100644
--- a/vp9/encoder/vp9_quantize.c
+++ b/vp9/encoder/vp9_quantize.c
@@ -263,10 +263,6 @@ void vp9_set_quantizer(struct VP9_COMP *cpi, int Q) {
 
   cm->base_qindex = Q;
 
-  // Set lossless mode
-  if (cm->base_qindex <= 4)
-    cm->base_qindex = 0;
-
   // if any of the delta_q values are changing update flag will
   // have to be set.
   cm->y_dc_delta_q = 0;
-- 
GitLab