Correct deltaq calculation

In encoding process, deltaq should be multiples of delta_q_res +
xd->current_qindex (q of last SB). In setup_delta_q(), first the
new qindex is calculated without restricting the delta to be
multiples of delta_q_res, then adjust the delta according to the
base value. This patch:
- correct the base value from frame level q cm->base_qindex to q
of last SB.
- Refactor the initial q calculation function to directly return
the new q, rather than the diff between q and framelevel baseq, so
an unnecessary intermediate variable offset_qindex can be removed.

Change-Id: I9f3130824dd3edb32d1ab8827436438dc07b4dfd
diff --git a/av1/encoder/aq_variance.c b/av1/encoder/aq_variance.c
index 9b58363..46b4971 100644
--- a/av1/encoder/aq_variance.c
+++ b/av1/encoder/aq_variance.c
@@ -180,8 +180,8 @@
   return clamp((int)round(energy), ENERGY_MIN, ENERGY_MAX);
 }
 
-int av1_compute_deltaq_from_energy_level(const AV1_COMP *const cpi,
-                                         int block_var_level) {
+int av1_compute_q_from_energy_level_deltaq_mode(const AV1_COMP *const cpi,
+                                                int block_var_level) {
   int rate_level;
   const AV1_COMMON *const cm = &cpi->common;
 
@@ -198,5 +198,5 @@
   if ((cm->base_qindex != 0) && ((cm->base_qindex + qindex_delta) == 0)) {
     qindex_delta = -cm->base_qindex + 1;
   }
-  return qindex_delta;
+  return cm->base_qindex + qindex_delta;
 }
diff --git a/av1/encoder/aq_variance.h b/av1/encoder/aq_variance.h
index 2d22b66..543eb0b 100644
--- a/av1/encoder/aq_variance.h
+++ b/av1/encoder/aq_variance.h
@@ -21,8 +21,8 @@
 void av1_vaq_frame_setup(AV1_COMP *cpi);
 
 int av1_log_block_var(const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs);
-int av1_compute_deltaq_from_energy_level(const AV1_COMP *const cpi,
-                                         int block_var_level);
+int av1_compute_q_from_energy_level_deltaq_mode(const AV1_COMP *const cpi,
+                                                int block_var_level);
 int av1_block_wavelet_energy_level(const AV1_COMP *cpi, MACROBLOCK *x,
                                    BLOCK_SIZE bs);
 
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index cbac2b2..2d58364 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -1392,6 +1392,7 @@
 
   int blk_row, blk_col;
 
+  assert(plane_bsize < BLOCK_SIZES_ALL);
   const int num_4x4_w = block_size_wide[plane_bsize] >> tx_size_wide_log2[0];
   const int num_4x4_h = block_size_high[plane_bsize] >> tx_size_high_log2[0];
 
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 61e1d2b..9a0bb20 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -4249,26 +4249,26 @@
   // Delta-q modulation based on variance
   av1_setup_src_planes(x, cpi->source, mi_row, mi_col, num_planes, sb_size);
 
-  int offset_qindex;
+  int current_qindex;
   if (DELTA_Q_PERCEPTUAL_MODULATION == 1) {
     const int block_wavelet_energy_level =
         av1_block_wavelet_energy_level(cpi, x, sb_size);
     x->sb_energy_level = block_wavelet_energy_level;
-    offset_qindex =
-        av1_compute_deltaq_from_energy_level(cpi, block_wavelet_energy_level);
+    current_qindex = av1_compute_q_from_energy_level_deltaq_mode(
+        cpi, block_wavelet_energy_level);
   } else {
     const int block_var_level = av1_log_block_var(cpi, x, sb_size);
     x->sb_energy_level = block_var_level;
-    offset_qindex = av1_compute_deltaq_from_energy_level(cpi, block_var_level);
+    current_qindex =
+        av1_compute_q_from_energy_level_deltaq_mode(cpi, block_var_level);
   }
   const int qmask = ~(delta_q_info->delta_q_res - 1);
-  int current_qindex =
-      clamp(cm->base_qindex + offset_qindex, delta_q_info->delta_q_res,
-            256 - delta_q_info->delta_q_res);
+  current_qindex = clamp(current_qindex, -delta_q_info->delta_q_res,
+                         256 - delta_q_info->delta_q_res);
   current_qindex =
-      ((current_qindex - cm->base_qindex + delta_q_info->delta_q_res / 2) &
+      ((current_qindex - xd->current_qindex + delta_q_info->delta_q_res / 2) &
        qmask) +
-      cm->base_qindex;
+      xd->current_qindex;
   assert(current_qindex > 0);
 
   xd->delta_qindex = current_qindex - cm->base_qindex;
@@ -4278,7 +4278,7 @@
   if (cpi->oxcf.deltaq_mode != NO_DELTA_Q && cpi->oxcf.deltalf_mode) {
     const int lfmask = ~(delta_q_info->delta_lf_res - 1);
     const int delta_lf_from_base =
-        ((offset_qindex / 2 + delta_q_info->delta_lf_res / 2) & lfmask);
+        ((xd->delta_qindex / 2 + delta_q_info->delta_lf_res / 2) & lfmask);
 
     // pre-set the delta lf for loop filter. Note that this value is set
     // before mi is assigned for each block in current superblock
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index c3d616d..4d9d00f 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -2023,6 +2023,7 @@
     struct macroblockd_plane *const pd = &xd->plane[plane];
     const BLOCK_SIZE plane_bsize =
         get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
+    assert(plane_bsize < BLOCK_SIZES_ALL);
     const int bw = block_size_wide[plane_bsize];
     const int bh = block_size_high[plane_bsize];
     int64_t sse;
@@ -5410,6 +5411,7 @@
   const struct macroblockd_plane *const pd = &xd->plane[0];
   const BLOCK_SIZE plane_bsize =
       get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
+  assert(plane_bsize < BLOCK_SIZES_ALL);
   const int mi_width = mi_size_wide[plane_bsize];
   const int mi_height = mi_size_high[plane_bsize];
   ENTROPY_CONTEXT ctxa[MAX_MIB_SIZE];
@@ -6031,6 +6033,7 @@
     struct macroblockd_plane *const pd = &xd->plane[plane];
     const BLOCK_SIZE plane_bsize =
         get_plane_block_size(bsize, pd->subsampling_x, pd->subsampling_y);
+    assert(plane_bsize < BLOCK_SIZES_ALL);
     const int bw = block_size_wide[plane_bsize];
     const int bh = block_size_high[plane_bsize];
     int64_t sse;