Merge "Fixing include order in vp9_quantize.c"
diff --git a/vp9/encoder/vp9_encodeframe.c b/vp9/encoder/vp9_encodeframe.c
index 46072a2..bba0c53 100644
--- a/vp9/encoder/vp9_encodeframe.c
+++ b/vp9/encoder/vp9_encodeframe.c
@@ -995,7 +995,7 @@
   if (rows_left <= 0 || cols_left <= 0) {
     return MIN(bsize, BLOCK_8X8);
   } else {
-    for (; bsize > 0; --bsize) {
+    for (; bsize > 0; bsize -= 3) {
       *bh = num_8x8_blocks_high_lookup[bsize];
       *bw = num_8x8_blocks_wide_lookup[bsize];
       if ((*bh <= rows_left) && (*bw <= cols_left)) {
@@ -2332,8 +2332,6 @@
   int mis = cm->mode_info_stride;
   int br, bc;
   int i, j;
-  int chosen_rate = INT_MAX;
-  int64_t chosen_dist = INT64_MAX;
   MB_PREDICTION_MODE mode = DC_PRED;
   int rows = MIN(MI_BLOCK_SIZE, tile->mi_row_end - mi_row);
   int cols = MIN(MI_BLOCK_SIZE, tile->mi_col_end - mi_col);
@@ -2354,6 +2352,7 @@
 
       BLOCK_SIZE bs = find_partition_size(bsize, rows - br, cols - bc,
                                           &bh, &bw);
+
       set_offsets(cpi, tile, row, col, bs);
 
       if (cm->frame_type != KEY_FRAME)
@@ -2365,16 +2364,10 @@
       *dist += bdist;
 
       for (j = 0; j < bh; ++j)
-        for (i = 0; i < bw; ++i) {
+        for (i = 0; i < bw; ++i)
           xd->mi_8x8[j * mis + i] = xd->mi_8x8[0];
-        }
     }
   }
-
-  *rate = chosen_rate;
-  *dist = chosen_dist;
-
-  encode_sb_rt(cpi, tile, tp, mi_row, mi_col, 1, BLOCK_64X64);
 }
 
 static void encode_nonrd_sb_row(VP9_COMP *cpi, const TileInfo *const tile,
@@ -2397,6 +2390,7 @@
       nonrd_use_partition(cpi, tile, tp, mi_row, mi_col,
                           cpi->sf.always_this_block_size,
                           &dummy_rate, &dummy_dist);
+      encode_sb_rt(cpi, tile, tp, mi_row, mi_col, 1, BLOCK_64X64);
     } else if (cpi->sf.partition_search_type == VAR_BASED_FIXED_PARTITION ||
                cpi->sf.partition_search_type == VAR_BASED_PARTITION) {
       // TODO(debargha): Implement VAR_BASED_PARTITION as a separate case.
@@ -2407,6 +2401,7 @@
                                                              mi_col);
       nonrd_use_partition(cpi, tile, tp, mi_row, mi_col,
                           bsize, &dummy_rate, &dummy_dist);
+      encode_sb_rt(cpi, tile, tp, mi_row, mi_col, 1, BLOCK_64X64);
     } else {
       assert(0);
     }
diff --git a/vp9/encoder/vp9_pickmode.c b/vp9/encoder/vp9_pickmode.c
index 87f20fa..0f2e303 100644
--- a/vp9/encoder/vp9_pickmode.c
+++ b/vp9/encoder/vp9_pickmode.c
@@ -270,12 +270,21 @@
                                 &frame_mv[NEWMV][ref_frame]);
       }
 
-      mbmi->mode = this_mode;
-      mbmi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
-      vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
+      if (frame_mv[this_mode][ref_frame].as_int == 0) {
+        dist = x->mode_sad[ref_frame][INTER_OFFSET(ZEROMV)];
+      } else if (this_mode != NEARESTMV &&
+                 frame_mv[NEARESTMV][ref_frame].as_int ==
+                     frame_mv[this_mode][ref_frame].as_int) {
+        dist = x->mode_sad[ref_frame][INTER_OFFSET(NEARESTMV)];
+      } else {
+        mbmi->mode = this_mode;
+        mbmi->mv[0].as_int = frame_mv[this_mode][ref_frame].as_int;
+        vp9_build_inter_predictors_sby(xd, mi_row, mi_col, bsize);
+        dist = x->mode_sad[ref_frame][INTER_OFFSET(this_mode)] =
+            cpi->fn_ptr[bsize].sdf(p->src.buf, p->src.stride,
+                                   pd->dst.buf, pd->dst.stride, INT_MAX);
+      }
 
-      dist = cpi->fn_ptr[bsize].sdf(p->src.buf, p->src.stride,
-                                    pd->dst.buf, pd->dst.stride, INT_MAX);
       this_rd = rate + dist;
 
       if (this_rd < best_rd) {
diff --git a/vpx/src/svc_encodeframe.c b/vpx/src/svc_encodeframe.c
index 5537fb5..c783724 100644
--- a/vpx/src/svc_encodeframe.c
+++ b/vpx/src/svc_encodeframe.c
@@ -548,15 +548,20 @@
 
     for (i = 0; i < si->layers; ++i) {
       int pos = i + VPX_SS_MAX_LAYERS - svc_ctx->spatial_layers;
-      alloc_ratio[i] = si->scaling_factor_num[pos] * 1.0 /
-                       si->scaling_factor_den[pos];
-      alloc_ratio[i] *= alloc_ratio[i];
-      total += alloc_ratio[i];
+      if (pos < VPX_SS_MAX_LAYERS && si->scaling_factor_den[pos] > 0) {
+        alloc_ratio[i] = (float)(si->scaling_factor_num[pos] * 1.0 /
+            si->scaling_factor_den[pos]);
+
+        alloc_ratio[i] *= alloc_ratio[i];
+        total += alloc_ratio[i];
+      }
     }
 
     for (i = 0; i < si->layers; ++i) {
-      enc_cfg->ss_target_bitrate[i] = enc_cfg->rc_target_bitrate *
-          alloc_ratio[i] / total;
+      if (total > 0) {
+        enc_cfg->ss_target_bitrate[i] = (unsigned int)
+            (enc_cfg->rc_target_bitrate * alloc_ratio[i] / total);
+      }
     }
   }
 
diff --git a/vpx/src/vpx_encoder.c b/vpx/src/vpx_encoder.c
index 23742c8..e69d96e 100644
--- a/vpx/src/vpx_encoder.c
+++ b/vpx/src/vpx_encoder.c
@@ -255,8 +255,8 @@
 }
 
 
-const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t   *ctx,
-                                                vpx_codec_iter_t  *iter) {
+const vpx_codec_cx_pkt_t *vpx_codec_get_cx_data(vpx_codec_ctx_t *ctx,
+                                                vpx_codec_iter_t *iter) {
   const vpx_codec_cx_pkt_t *pkt = NULL;
 
   if (ctx) {
@@ -271,32 +271,30 @@
   }
 
   if (pkt && pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
-    /* If the application has specified a destination area for the
-     * compressed data, and the codec has not placed the data there,
-     * and it fits, copy it.
-     */
-    char *dst_buf = ctx->priv->enc.cx_data_dst_buf.buf;
+    // If the application has specified a destination area for the
+    // compressed data, and the codec has not placed the data there,
+    // and it fits, copy it.
+    vpx_codec_priv_t *const priv = ctx->priv;
+    char *const dst_buf = (char *)priv->enc.cx_data_dst_buf.buf;
 
-    if (dst_buf
-        && pkt->data.raw.buf != dst_buf
-        && pkt->data.raw.sz
-        + ctx->priv->enc.cx_data_pad_before
-        + ctx->priv->enc.cx_data_pad_after
-        <= ctx->priv->enc.cx_data_dst_buf.sz) {
-      vpx_codec_cx_pkt_t *modified_pkt = &ctx->priv->enc.cx_data_pkt;
+    if (dst_buf &&
+        pkt->data.raw.buf != dst_buf &&
+        pkt->data.raw.sz + priv->enc.cx_data_pad_before +
+            priv->enc.cx_data_pad_after <= priv->enc.cx_data_dst_buf.sz) {
+      vpx_codec_cx_pkt_t *modified_pkt = &priv->enc.cx_data_pkt;
 
-      memcpy(dst_buf + ctx->priv->enc.cx_data_pad_before,
-             pkt->data.raw.buf, pkt->data.raw.sz);
+      memcpy(dst_buf + priv->enc.cx_data_pad_before, pkt->data.raw.buf,
+             pkt->data.raw.sz);
       *modified_pkt = *pkt;
       modified_pkt->data.raw.buf = dst_buf;
-      modified_pkt->data.raw.sz += ctx->priv->enc.cx_data_pad_before
-                                   + ctx->priv->enc.cx_data_pad_after;
+      modified_pkt->data.raw.sz += priv->enc.cx_data_pad_before +
+                                       priv->enc.cx_data_pad_after;
       pkt = modified_pkt;
     }
 
     if (dst_buf == pkt->data.raw.buf) {
-      ctx->priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
-      ctx->priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
+      priv->enc.cx_data_dst_buf.buf = dst_buf + pkt->data.raw.sz;
+      priv->enc.cx_data_dst_buf.sz -= pkt->data.raw.sz;
     }
   }