Support for delta-q at superblock level

Change-Id: I4128af44776d1f361bddc1fdffb75ed2224dbfa5
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index e4f2ee7..e3c7698 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1209,6 +1209,26 @@
   }
 #endif  // CONFIG_SUPERTX
 
+#if CONFIG_DELTA_Q
+  if (cm->delta_q_present_flag) {
+    int i;
+    for (i = 0; i < MAX_SEGMENTS; i++) {
+      xd->plane[0].seg_dequant[i][0] =
+          av1_dc_quant(xd->current_qindex, cm->y_dc_delta_q, cm->bit_depth);
+      xd->plane[0].seg_dequant[i][1] =
+          av1_ac_quant(xd->current_qindex, 0, cm->bit_depth);
+      xd->plane[1].seg_dequant[i][0] =
+          av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
+      xd->plane[1].seg_dequant[i][1] =
+          av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
+      xd->plane[2].seg_dequant[i][0] =
+          av1_dc_quant(xd->current_qindex, cm->uv_dc_delta_q, cm->bit_depth);
+      xd->plane[2].seg_dequant[i][1] =
+          av1_ac_quant(xd->current_qindex, cm->uv_ac_delta_q, cm->bit_depth);
+    }
+  }
+#endif
+
   if (mbmi->skip) {
     dec_reset_skip_context(xd);
   }
@@ -3485,6 +3505,27 @@
 
   setup_segmentation(cm, rb);
 
+#if CONFIG_DELTA_Q
+  {
+    struct segmentation *const seg = &cm->seg;
+    int segment_quantizer_active = 0;
+    for (i = 0; i < MAX_SEGMENTS; i++) {
+      if (segfeature_active(seg, i, SEG_LVL_ALT_Q)) {
+        segment_quantizer_active = 1;
+      }
+    }
+
+    if (segment_quantizer_active == 0) {
+      cm->delta_q_present_flag = aom_rb_read_bit(rb);
+    } else {
+      cm->delta_q_present_flag = 0;
+    }
+    if (cm->delta_q_present_flag) {
+      xd->prev_qindex = cm->base_qindex;
+    }
+  }
+#endif
+
   for (i = 0; i < MAX_SEGMENTS; ++i) {
     const int qindex = cm->seg.enabled
                            ? av1_get_qindex(&cm->seg, i, cm->base_qindex)
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index 66056c0..37bc0f4 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -598,6 +598,38 @@
 
   mbmi->segment_id = read_intra_segment_id(cm, xd, mi_offset, x_mis, y_mis, r);
   mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
+
+#if CONFIG_DELTA_Q
+  if (cm->delta_q_present_flag) {
+    int b_col = mi_col & 7;
+    int b_row = mi_row & 7;
+    int read_delta_q_flag = (b_col == 0 && b_row == 0);
+    if ((bsize != BLOCK_64X64 || mbmi->skip == 0) && read_delta_q_flag) {
+      int sign, abs, tmp, delta_qindex;
+
+      abs = 0;
+      tmp = aom_read_bit(r, ACCT_STR);
+      while (tmp == 0 && abs < 2) {
+        tmp = aom_read_bit(r, ACCT_STR);
+        abs++;
+      }
+      if (tmp == 0) {
+        abs = aom_read_literal(r, 6, ACCT_STR);
+      }
+
+      if (abs) {
+        sign = aom_read_bit(r, ACCT_STR);
+      } else {
+        sign = 1;
+      }
+
+      delta_qindex = sign ? -abs : abs;
+      xd->current_qindex = xd->prev_qindex + delta_qindex;
+      xd->prev_qindex = xd->current_qindex;
+    }
+  }
+#endif
+
   mbmi->tx_size = read_tx_size_intra(cm, xd, r);
   mbmi->ref_frame[0] = INTRA_FRAME;
   mbmi->ref_frame[1] = NONE;
@@ -1655,6 +1687,35 @@
   if (!supertx_enabled) {
 #endif  // CONFIG_SUPERTX
     mbmi->skip = read_skip(cm, xd, mbmi->segment_id, r);
+#if CONFIG_DELTA_Q
+    if (cm->delta_q_present_flag) {
+      BLOCK_SIZE bsize = mbmi->sb_type;
+      int b_col = mi_col & 7;
+      int b_row = mi_row & 7;
+      int read_delta_q_flag = (b_col == 0 && b_row == 0);
+      if ((bsize != BLOCK_64X64 || mbmi->skip == 0) && read_delta_q_flag) {
+        int sign, abs, tmp, delta_qindex;
+
+        abs = 0;
+        tmp = aom_read_bit(r, ACCT_STR);
+        while (tmp == 0 && abs < 2) {
+          tmp = aom_read_bit(r, ACCT_STR);
+          abs++;
+        }
+        if (tmp == 0) {
+          abs = aom_read_literal(r, 6, ACCT_STR);
+        }
+        if (abs) {
+          sign = aom_read_bit(r, ACCT_STR);
+        } else {
+          sign = 1;
+        }
+        delta_qindex = sign ? -abs : abs;
+        xd->current_qindex = xd->prev_qindex + delta_qindex;
+        xd->prev_qindex = xd->current_qindex;
+      }
+    }
+#endif
     inter_block = read_is_inter_block(cm, xd, mbmi->segment_id, r);
 
 #if CONFIG_VAR_TX