Merge "Use SSE as BPRED distortion metric consistently"
diff --git a/build/make/gen_msvs_proj.sh b/build/make/gen_msvs_proj.sh
index fb948fd..ff9b665 100755
--- a/build/make/gen_msvs_proj.sh
+++ b/build/make/gen_msvs_proj.sh
@@ -365,7 +365,7 @@
                             DebugInformationFormat="1" \
                             Detect64BitPortabilityProblems="true" \
 
-                        $uses_asm && tag Tool Name="YASM"  IncludePaths="$incs" Debug="1"
+                        $uses_asm && tag Tool Name="YASM"  IncludePaths="$incs" Debug="true"
                     ;;
                     *)
                         tag Tool \
@@ -379,7 +379,7 @@
                             DebugInformationFormat="1" \
                             Detect64BitPortabilityProblems="true" \
 
-                        $uses_asm && tag Tool Name="YASM"  IncludePaths="$incs" Debug="1"
+                        $uses_asm && tag Tool Name="YASM"  IncludePaths="$incs" Debug="true"
                     ;;
                 esac
             ;;
diff --git a/vp8/decoder/threading.c b/vp8/decoder/threading.c
index 77c3f17..c1df823 100644
--- a/vp8/decoder/threading.c
+++ b/vp8/decoder/threading.c
@@ -135,7 +135,7 @@
         mb_init_dequantizer(pbi, xd);
 
     /* do prediction */
-    if (xd->frame_type == KEY_FRAME  ||  xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
+    if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
     {
         vp8mt_build_intra_predictors_mbuv(pbi, xd, mb_row, mb_col);
 
@@ -181,7 +181,7 @@
                          xd->predictor, xd->dst.y_buffer,
                          xd->dst.y_stride, xd->eobs, xd->block[24].diff);
     }
-    else if ((xd->frame_type == KEY_FRAME  ||  xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME) && xd->mode_info_context->mbmi.mode == B_PRED)
+    else if (xd->mode_info_context->mbmi.mode == B_PRED)
     {
         for (i = 0; i < 16; i++)
         {
@@ -334,7 +334,7 @@
                             {
                                 MODE_INFO *next = xd->mode_info_context +1;
 
-                                if (xd->frame_type == KEY_FRAME  ||  next->mbmi.ref_frame == INTRA_FRAME)
+                                if (next->mbmi.ref_frame == INTRA_FRAME)
                                 {
                                     for (i = 0; i < 16; i++)
                                         pbi->mt_yleft_col[mb_row][i] = xd->dst.y_buffer [i* recon_y_stride + 15];
@@ -824,7 +824,7 @@
                     {
                         MODE_INFO *next = xd->mode_info_context +1;
 
-                        if (xd->frame_type == KEY_FRAME  ||  next->mbmi.ref_frame == INTRA_FRAME)
+                        if (next->mbmi.ref_frame == INTRA_FRAME)
                         {
                             for (i = 0; i < 16; i++)
                                 pbi->mt_yleft_col[mb_row][i] = xd->dst.y_buffer [i* recon_y_stride + 15];
diff --git a/vp8/encoder/firstpass.c b/vp8/encoder/firstpass.c
index b5d3ff9..a045760 100644
--- a/vp8/encoder/firstpass.c
+++ b/vp8/encoder/firstpass.c
@@ -1722,14 +1722,15 @@
     cpi->twopass.modified_error_used += gf_group_err;
 
     // Assign  bits to the arf or gf.
-    {
+    for (i = 0; i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME); i++) {
         int Boost;
         int frames_in_section;
         int allocation_chunks;
         int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed_q;
+        int gf_bits;
 
         // For ARF frames
-        if (cpi->source_alt_ref_pending)
+        if (cpi->source_alt_ref_pending && i == 0)
         {
             Boost = (cpi->gfu_boost * 3 * GFQ_ADJUSTMENT) / (2 * 100);
             //Boost += (cpi->baseline_gf_interval * 25);
@@ -1768,7 +1769,7 @@
         }
 
         // Calculate the number of bits to be spent on the gf or arf based on the boost number
-        cpi->twopass.gf_bits = (int)((double)Boost * (cpi->twopass.gf_group_bits / (double)allocation_chunks));
+        gf_bits = (int)((double)Boost * (cpi->twopass.gf_group_bits / (double)allocation_chunks));
 
         // If the frame that is to be boosted is simpler than the average for
         // the gf/arf group then use an alternative calculation
@@ -1786,9 +1787,9 @@
             alt_gf_bits = (int)((double)Boost * (alt_gf_grp_bits /
                                                  (double)allocation_chunks));
 
-            if (cpi->twopass.gf_bits > alt_gf_bits)
+            if (gf_bits > alt_gf_bits)
             {
-                cpi->twopass.gf_bits = alt_gf_bits;
+                gf_bits = alt_gf_bits;
             }
         }
         // Else if it is harder than other frames in the group make sure it at
@@ -1801,16 +1802,29 @@
                       mod_frame_err /
                       DOUBLE_DIVIDE_CHECK((double)cpi->twopass.kf_group_error_left));
 
-            if (alt_gf_bits > cpi->twopass.gf_bits)
+            if (alt_gf_bits > gf_bits)
             {
-                cpi->twopass.gf_bits = alt_gf_bits;
+                gf_bits = alt_gf_bits;
             }
         }
 
         // Dont allow a negative value for gf_bits
-        if (cpi->twopass.gf_bits < 0)
-            cpi->twopass.gf_bits = 0;
+        if (gf_bits < 0)
+            gf_bits = 0;
 
+        gf_bits += cpi->min_frame_bandwidth;                     // Add in minimum for a frame
+
+        if (i == 0)
+        {
+            cpi->twopass.gf_bits = gf_bits;
+        }
+        if (i == 1 || (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)))
+        {
+            cpi->per_frame_bandwidth = gf_bits;                 // Per frame bit target for this frame
+        }
+    }
+
+    {
         // Adjust KF group bits and error remainin
         cpi->twopass.kf_group_error_left -= gf_group_err;
         cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
@@ -1825,7 +1839,7 @@
         else
             cpi->twopass.gf_group_error_left = gf_group_err;
 
-        cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits;
+        cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits - cpi->min_frame_bandwidth;
 
         if (cpi->twopass.gf_group_bits < 0)
             cpi->twopass.gf_group_bits = 0;
@@ -1841,13 +1855,6 @@
         }
         else
             cpi->twopass.mid_gf_extra_bits = 0;
-
-        cpi->twopass.gf_bits += cpi->min_frame_bandwidth;                                              // Add in minimum for a frame
-    }
-
-    if (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))                  // Normal GF and not a KF
-    {
-        cpi->per_frame_bandwidth = cpi->twopass.gf_bits;                                               // Per frame bit target for this frame
     }
 
     // Adjustment to estimate_max_q based on a measure of complexity of the section
@@ -1897,12 +1904,6 @@
 
     int max_bits = frame_max_bits(cpi);    // Max for a single frame
 
-    // The final few frames have special treatment
-    if (cpi->frames_till_gf_update_due >= (int)(cpi->twopass.total_stats->count - cpi->common.current_video_frame))
-    {
-        cpi->twopass.gf_group_bits = (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;;
-    }
-
     // Calculate modified prediction error used in bit allocation
     modified_err = calculate_modified_err(cpi, this_frame);
 
@@ -2004,22 +2005,10 @@
         if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME))
         {
             // Assign a standard frames worth of bits from those allocated to the GF group
+            int bak = cpi->per_frame_bandwidth;
             vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
             assign_std_frame_bits(cpi, &this_frame_copy);
-
-            // If appropriate (we are switching into ARF active but it was not previously active) apply a boost for the gf at the start of the group.
-            //if ( !cpi->source_alt_ref_active && (cpi->gfu_boost > 150) )
-            if (FALSE)
-            {
-                int extra_bits;
-                int pct_extra = (cpi->gfu_boost - 100) / 50;
-
-                pct_extra = (pct_extra > 20) ? 20 : pct_extra;
-
-                extra_bits = (cpi->twopass.gf_group_bits * pct_extra) / 100;
-                cpi->twopass.gf_group_bits -= extra_bits;
-                cpi->per_frame_bandwidth += extra_bits;
-            }
+            cpi->per_frame_bandwidth = bak;
         }
     }