Fix some issues with delta q

Change-Id: I3f7a2cf54460f38bc5ea3c88be81ab36393dda81
diff --git a/av1/common/enums.h b/av1/common/enums.h
index b9767eb..500c56d 100644
--- a/av1/common/enums.h
+++ b/av1/common/enums.h
@@ -526,7 +526,9 @@
 
 #define DELTA_Q_SMALL 3
 #define DELTA_Q_PROBS (DELTA_Q_SMALL)
-#define DEFAULT_DELTA_Q_RES 4
+#define DEFAULT_DELTA_Q_RES_PERCEPTUAL 4
+#define DEFAULT_DELTA_Q_RES_OBJECTIVE 8
+
 #define DELTA_LF_SMALL 3
 #define DELTA_LF_PROBS (DELTA_LF_SMALL)
 #define DEFAULT_DELTA_LF_RES 2
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 8ca501d..c34cb01 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -4276,7 +4276,7 @@
   rk = (double)intra_cost / mc_dep_cost;
   beta = r0 / rk;
 
-  int offset = -(int)(log(beta) * 16.0);
+  int offset = -(int)(log(beta) * 8.0);
   offset = AOMMIN(offset, 16);
   offset = AOMMAX(offset, -16);
   int qindex = cm->base_qindex + offset;
@@ -4321,10 +4321,15 @@
   const int qmask = ~(delta_q_info->delta_q_res - 1);
   current_qindex = clamp(current_qindex, delta_q_info->delta_q_res,
                          256 - delta_q_info->delta_q_res);
-  current_qindex =
-      ((current_qindex - xd->current_qindex + delta_q_info->delta_q_res / 2) &
-       qmask) +
-      xd->current_qindex;
+
+  const int sign_deltaq_index =
+      current_qindex - xd->current_qindex >= 0 ? 1 : -1;
+
+  const int deltaq_deadzone = 0;  // delta_q_info->delta_q_res / 2;
+  int abs_deltaq_index = abs(current_qindex - xd->current_qindex);
+  abs_deltaq_index = (abs_deltaq_index + deltaq_deadzone) & qmask;
+  current_qindex = xd->current_qindex + sign_deltaq_index * abs_deltaq_index;
+  current_qindex = AOMMAX(current_qindex, MINQ + 1);
   assert(current_qindex > 0);
 
   xd->delta_qindex = current_qindex - cm->base_qindex;
@@ -5391,7 +5396,11 @@
   cm->tx_mode = select_tx_mode(cpi);
 
   // Fix delta q resolution for the moment
-  cm->delta_q_info.delta_q_res = DEFAULT_DELTA_Q_RES;
+  cm->delta_q_info.delta_q_res = 0;
+  if (cpi->oxcf.deltaq_mode == DELTA_Q_OBJECTIVE)
+    cm->delta_q_info.delta_q_res = DEFAULT_DELTA_Q_RES_OBJECTIVE;
+  else if (cpi->oxcf.deltaq_mode == DELTA_Q_PERCEPTUAL)
+    cm->delta_q_info.delta_q_res = DEFAULT_DELTA_Q_RES_PERCEPTUAL;
   // Set delta_q_present_flag before it is used for the first time
   cm->delta_q_info.delta_lf_res = DEFAULT_DELTA_LF_RES;
   cm->delta_q_info.delta_q_present_flag = cpi->oxcf.deltaq_mode != NO_DELTA_Q;