Merge "keep values in registers during quantization"
diff --git a/build/make/gen_msvs_proj.sh b/build/make/gen_msvs_proj.sh
index c2ef44a..fb948fd 100755
--- a/build/make/gen_msvs_proj.sh
+++ b/build/make/gen_msvs_proj.sh
@@ -447,6 +447,8 @@
                     obj_int_extract)
                         tag Tool \
                             Name="VCCLCompilerTool" \
+                            Optimization="2" \
+                            FavorSizeorSpeed="1" \
                             AdditionalIncludeDirectories="$incs" \
                             PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE" \
                             RuntimeLibrary="$release_runtime" \
@@ -462,6 +464,8 @@
 
                         tag Tool \
                             Name="VCCLCompilerTool" \
+                            Optimization="2" \
+                            FavorSizeorSpeed="1" \
                             AdditionalIncludeDirectories="$incs" \
                             PreprocessorDefinitions="WIN32;NDEBUG;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;$defines" \
                             RuntimeLibrary="$release_runtime" \
@@ -476,6 +480,8 @@
                         tag Tool \
                             Name="VCCLCompilerTool" \
                             AdditionalIncludeDirectories="$incs" \
+                            Optimization="2" \
+                            FavorSizeorSpeed="1" \
                             PreprocessorDefinitions="WIN32;NDEBUG;_CRT_SECURE_NO_WARNINGS;_CRT_SECURE_NO_DEPRECATE;$defines" \
                             RuntimeLibrary="$release_runtime" \
                             UsePrecompiledHeader="0" \
diff --git a/vp8/common/blockd.h b/vp8/common/blockd.h
index cfe9e15..aef6927 100644
--- a/vp8/common/blockd.h
+++ b/vp8/common/blockd.h
@@ -169,9 +169,7 @@
 
     unsigned char partitioning;
     unsigned char mb_skip_coeff;                                /* does this mb has coefficients at all, 1=no coefficients, 0=need decode tokens */
-    unsigned char dc_diff;
     unsigned char need_to_clamp_mvs;
-
     unsigned char segment_id;                  /* Which set of segmentation parameters should be used for this MB */
 } MB_MODE_INFO;
 
diff --git a/vp8/common/loopfilter.c b/vp8/common/loopfilter.c
index 37c5b77..221998b 100644
--- a/vp8/common/loopfilter.c
+++ b/vp8/common/loopfilter.c
@@ -353,6 +353,9 @@
         for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
         {
             int Segment = (alt_flt_enabled) ? mbd->mode_info_context->mbmi.segment_id : 0;
+            int skip_lf = (mbd->mode_info_context->mbmi.mode != B_PRED &&
+                            mbd->mode_info_context->mbmi.mode != SPLITMV &&
+                            mbd->mode_info_context->mbmi.mb_skip_coeff);
 
             filter_level = baseline_filter_level[Segment];
 
@@ -367,14 +370,14 @@
                 if (mb_col > 0)
                     cm->lf_mbv(y_ptr, u_ptr, v_ptr, post->y_stride, post->uv_stride, &lfi[filter_level], cm->simpler_lpf);
 
-                if (mbd->mode_info_context->mbmi.dc_diff > 0)
+                if (!skip_lf)
                     cm->lf_bv(y_ptr, u_ptr, v_ptr, post->y_stride, post->uv_stride, &lfi[filter_level], cm->simpler_lpf);
 
                 /* don't apply across umv border */
                 if (mb_row > 0)
                     cm->lf_mbh(y_ptr, u_ptr, v_ptr, post->y_stride, post->uv_stride, &lfi[filter_level], cm->simpler_lpf);
 
-                if (mbd->mode_info_context->mbmi.dc_diff > 0)
+                if (!skip_lf)
                     cm->lf_bh(y_ptr, u_ptr, v_ptr, post->y_stride, post->uv_stride, &lfi[filter_level], cm->simpler_lpf);
             }
 
@@ -457,6 +460,10 @@
         for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
         {
             int Segment = (alt_flt_enabled) ? mbd->mode_info_context->mbmi.segment_id : 0;
+            int skip_lf = (mbd->mode_info_context->mbmi.mode != B_PRED &&
+                            mbd->mode_info_context->mbmi.mode != SPLITMV &&
+                            mbd->mode_info_context->mbmi.mb_skip_coeff);
+
             filter_level = baseline_filter_level[Segment];
 
             /* Apply any context driven MB level adjustment */
@@ -467,14 +474,14 @@
                 if (mb_col > 0)
                     cm->lf_mbv(y_ptr, 0, 0, post->y_stride, 0, &lfi[filter_level], 0);
 
-                if (mbd->mode_info_context->mbmi.dc_diff > 0)
+                if (!skip_lf)
                     cm->lf_bv(y_ptr, 0, 0, post->y_stride, 0, &lfi[filter_level], 0);
 
                 /* don't apply across umv border */
                 if (mb_row > 0)
                     cm->lf_mbh(y_ptr, 0, 0, post->y_stride, 0, &lfi[filter_level], 0);
 
-                if (mbd->mode_info_context->mbmi.dc_diff > 0)
+                if (!skip_lf)
                     cm->lf_bh(y_ptr, 0, 0, post->y_stride, 0, &lfi[filter_level], 0);
             }
 
@@ -565,6 +572,10 @@
         for (mb_col = 0; mb_col < mb_cols; mb_col++)
         {
             int Segment = (alt_flt_enabled) ? mbd->mode_info_context->mbmi.segment_id : 0;
+            int skip_lf = (mbd->mode_info_context->mbmi.mode != B_PRED &&
+                            mbd->mode_info_context->mbmi.mode != SPLITMV &&
+                            mbd->mode_info_context->mbmi.mb_skip_coeff);
+
             filter_level = baseline_filter_level[Segment];
 
             if (filter_level)
@@ -572,12 +583,12 @@
                 if (mb_col > 0)
                     cm->lf_mbv(y_ptr, 0, 0, post->y_stride, 0, &lfi[filter_level], 0);
 
-                if (mbd->mode_info_context->mbmi.dc_diff > 0)
+                if (!skip_lf)
                     cm->lf_bv(y_ptr, 0, 0, post->y_stride, 0, &lfi[filter_level], 0);
 
                 cm->lf_mbh(y_ptr, 0, 0, post->y_stride, 0, &lfi[filter_level], 0);
 
-                if (mbd->mode_info_context->mbmi.dc_diff > 0)
+                if (!skip_lf)
                     cm->lf_bh(y_ptr, 0, 0, post->y_stride, 0, &lfi[filter_level], 0);
             }
 
diff --git a/vp8/common/postproc.c b/vp8/common/postproc.c
index 5bfc7d6..660880b 100644
--- a/vp8/common/postproc.c
+++ b/vp8/common/postproc.c
@@ -804,11 +804,14 @@
             for (j = 0; j < mb_cols; j++)
             {
                 char zz[4];
+                int dc_diff = !(mi[mb_index].mbmi.mode != B_PRED &&
+                              mi[mb_index].mbmi.mode != SPLITMV &&
+                              mi[mb_index].mbmi.mb_skip_coeff));
 
                 if (oci->frame_type == KEY_FRAME)
                     sprintf(zz, "a");
                 else
-                    sprintf(zz, "%c", mi[mb_index].mbmi.dc_diff + '0');
+                    sprintf(zz, "%c", dc_diff + '0');
 
                 vp8_blit_text(zz, y_ptr, post->y_stride);
                 mb_index ++;
diff --git a/vp8/decoder/decodframe.c b/vp8/decoder/decodframe.c
index c1faaf1..9598508 100644
--- a/vp8/decoder/decodframe.c
+++ b/vp8/decoder/decodframe.c
@@ -111,9 +111,8 @@
  */
 static void skip_recon_mb(VP8D_COMP *pbi, MACROBLOCKD *xd)
 {
-    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)
     {
-
         vp8_build_intra_predictors_mbuv_s(xd);
         RECON_INVOKE(&pbi->common.rtcd.recon,
                      build_intra_predictors_mby_s)(xd);
@@ -195,11 +194,10 @@
         clamp_mvs(xd);
     }
 
-    xd->mode_info_context->mbmi.dc_diff = 1;
-
-    if (xd->mode_info_context->mbmi.mode != B_PRED && xd->mode_info_context->mbmi.mode != SPLITMV && eobtotal == 0)
+    eobtotal |= (xd->mode_info_context->mbmi.mode == B_PRED ||
+                  xd->mode_info_context->mbmi.mode == SPLITMV);
+    if (!eobtotal)
     {
-        xd->mode_info_context->mbmi.dc_diff = 0;
         skip_recon_mb(pbi, xd);
         return;
     }
@@ -208,7 +206,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)
     {
         vp8_build_intra_predictors_mbuv(xd);
 
@@ -255,7 +253,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++)
         {
diff --git a/vp8/decoder/threading.c b/vp8/decoder/threading.c
index 57af079..c92de00 100644
--- a/vp8/decoder/threading.c
+++ b/vp8/decoder/threading.c
@@ -108,12 +108,10 @@
         clamp_mvs(xd);
     }
 
-    xd->mode_info_context->mbmi.dc_diff = 1;
-
-    if (xd->mode_info_context->mbmi.mode != B_PRED && xd->mode_info_context->mbmi.mode != SPLITMV && eobtotal == 0)
+    eobtotal |= (xd->mode_info_context->mbmi.mode == B_PRED ||
+                  xd->mode_info_context->mbmi.mode == SPLITMV);
+    if (!eobtotal)
     {
-        xd->mode_info_context->mbmi.dc_diff = 0;
-
         /*mt_skip_recon_mb(pbi, xd, mb_row, mb_col);*/
         if (xd->frame_type == KEY_FRAME  ||  xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME)
         {
@@ -322,6 +320,7 @@
 
                         if (pbi->common.filter_level)
                         {
+                            int skip_lf;
                             if( mb_row != pc->mb_rows-1 )
                             {
                                 /* Save decoded MB last row data for next-row decoding */
@@ -349,6 +348,10 @@
 
                             /* update loopfilter info */
                             Segment = (alt_flt_enabled) ? xd->mode_info_context->mbmi.segment_id : 0;
+                            skip_lf = (xd->mode_info_context->mbmi.mode != B_PRED &&
+                                            xd->mode_info_context->mbmi.mode != SPLITMV &&
+                                            xd->mode_info_context->mbmi.mb_skip_coeff);
+
                             filter_level = pbi->mt_baseline_filter_level[Segment];
                             /* Distance of Mb to the various image edges.
                              * These are specified to 8th pel as they are always compared to values that are in 1/8th pel units
@@ -362,14 +365,14 @@
                                 if (mb_col > 0)
                                     pc->lf_mbv(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level], pc->simpler_lpf);
 
-                                if (xd->mode_info_context->mbmi.dc_diff > 0)
+                                if (!skip_lf)
                                     pc->lf_bv(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level], pc->simpler_lpf);
 
                                 /* don't apply across umv border */
                                 if (mb_row > 0)
                                     pc->lf_mbh(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level], pc->simpler_lpf);
 
-                                if (xd->mode_info_context->mbmi.dc_diff > 0)
+                                if (!skip_lf)
                                     pc->lf_bh(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level], pc->simpler_lpf);
                             }
                         }
@@ -810,6 +813,7 @@
 
                 if (pbi->common.filter_level)
                 {
+                    int skip_lf;
                     /* Save decoded MB last row data for next-row decoding */
                     if(mb_row != pc->mb_rows-1)
                     {
@@ -837,6 +841,9 @@
 
                     /* update loopfilter info */
                     Segment = (alt_flt_enabled) ? xd->mode_info_context->mbmi.segment_id : 0;
+                    skip_lf = (xd->mode_info_context->mbmi.mode != B_PRED &&
+                                    xd->mode_info_context->mbmi.mode != SPLITMV &&
+                                    xd->mode_info_context->mbmi.mb_skip_coeff);
                     filter_level = pbi->mt_baseline_filter_level[Segment];
                     /* Distance of Mb to the various image edges.
                      * These are specified to 8th pel as they are always compared to values that are in 1/8th pel units
@@ -850,14 +857,14 @@
                         if (mb_col > 0)
                             pc->lf_mbv(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level], pc->simpler_lpf);
 
-                        if (xd->mode_info_context->mbmi.dc_diff > 0)
+                        if (!skip_lf)
                             pc->lf_bv(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level], pc->simpler_lpf);
 
                         /* don't apply across umv border */
                         if (mb_row > 0)
                             pc->lf_mbh(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level], pc->simpler_lpf);
 
-                        if (xd->mode_info_context->mbmi.dc_diff > 0)
+                        if (!skip_lf)
                             pc->lf_bh(xd->dst.y_buffer, xd->dst.u_buffer, xd->dst.v_buffer, recon_y_stride, recon_uv_stride, &lfi[filter_level], pc->simpler_lpf);
                     }
                 }
diff --git a/vp8/encoder/encodeframe.c b/vp8/encoder/encodeframe.c
index 1ae3325..f48a1fc 100644
--- a/vp8/encoder/encodeframe.c
+++ b/vp8/encoder/encodeframe.c
@@ -1400,11 +1400,6 @@
     {
         if (cpi->common.mb_no_coeff_skip)
         {
-            if (xd->mode_info_context->mbmi.mode != B_PRED && xd->mode_info_context->mbmi.mode != SPLITMV)
-                xd->mode_info_context->mbmi.dc_diff = 0;
-            else
-                xd->mode_info_context->mbmi.dc_diff = 1;
-
             xd->mode_info_context->mbmi.mb_skip_coeff = 1;
             cpi->skip_true_count ++;
             vp8_fix_contexts(xd);
diff --git a/vp8/encoder/onyx_if.c b/vp8/encoder/onyx_if.c
index 92e0cbb..7461edc 100644
--- a/vp8/encoder/onyx_if.c
+++ b/vp8/encoder/onyx_if.c
@@ -1699,9 +1699,6 @@
     cm->horiz_scale  = cpi->horiz_scale;
     cm->vert_scale   = cpi->vert_scale ;
 
-    // As per VP8
-    cpi->intra_frame_target = (4 * (cm->Width + cm->Height) / 15) * 1000;
-
     // VP8 sharpness level mapping 0-7 (vs 0-10 in general VPx dialogs)
     if (cpi->oxcf.Sharpness > 7)
         cpi->oxcf.Sharpness = 7;
@@ -1731,10 +1728,6 @@
         vp8_alloc_compressor_data(cpi);
     }
 
-    // Clamp KF frame size to quarter of data rate
-    if (cpi->intra_frame_target > cpi->target_bandwidth >> 2)
-        cpi->intra_frame_target = cpi->target_bandwidth >> 2;
-
     if (cpi->oxcf.fixed_q >= 0)
     {
         cpi->last_q[0] = cpi->oxcf.fixed_q;
@@ -1960,7 +1953,6 @@
 
     cpi->frames_till_gf_update_due      = 0;
     cpi->key_frame_count              = 1;
-    cpi->tot_key_frame_bits            = 0;
 
     cpi->ni_av_qi                     = cpi->oxcf.worst_allowed_q;
     cpi->ni_tot_qi                    = 0;
@@ -1986,7 +1978,6 @@
 
     for (i = 0; i < KEY_FRAME_CONTEXT; i++)
     {
-        cpi->prior_key_frame_size[i]     = cpi->intra_frame_target;
         cpi->prior_key_frame_distance[i] = (int)cpi->output_frame_rate;
     }
 
@@ -4370,9 +4361,9 @@
         vp8_clear_system_state();  //__asm emms;
 
         if (cpi->total_coded_error_left != 0.0)
-            fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
-                       "%6ld %6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
-                       "%10.3f %8ld\n",
+            fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
+                       "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
+                       "%10.3f %8d\n",
                        cpi->common.current_video_frame, cpi->this_frame_target,
                        cpi->projected_frame_size,
                        (cpi->projected_frame_size - cpi->this_frame_target),
@@ -4389,9 +4380,9 @@
                        (double)cpi->bits_left / cpi->total_coded_error_left,
                        cpi->tot_recode_hits);
         else
-            fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6ld %6ld"
-                       "%6ld %6ld %6ld %5ld %5ld %5ld %8ld %8.2f %10d %10.3f"
-                       "%8ld\n",
+            fprintf(f, "%10d %10d %10d %10d %10d %10d %10d %10d %6d %6d"
+                       "%6d %6d %6d %5d %5d %5d %8d %8.2f %10d %10.3f"
+                       "%8d\n",
                        cpi->common.current_video_frame,
                        cpi->this_frame_target, cpi->projected_frame_size,
                        (cpi->projected_frame_size - cpi->this_frame_target),
diff --git a/vp8/encoder/onyx_int.h b/vp8/encoder/onyx_int.h
index 4c18542..fdd3b99 100644
--- a/vp8/encoder/onyx_int.h
+++ b/vp8/encoder/onyx_int.h
@@ -385,14 +385,11 @@
     int active_arnr_frames;           // <= cpi->oxcf.arnr_max_frames
 
     INT64 key_frame_count;
-    INT64 tot_key_frame_bits;
-    int prior_key_frame_size[KEY_FRAME_CONTEXT];
     int prior_key_frame_distance[KEY_FRAME_CONTEXT];
     int per_frame_bandwidth;          // Current section per frame bandwidth target
     int av_per_frame_bandwidth;        // Average frame size target for clip
     int min_frame_bandwidth;          // Minimum allocation that should be used for any frame
     int last_key_frame_size;
-    int intra_frame_target;
     int inter_frame_target;
     double output_frame_rate;
     long long last_time_stamp_seen;
diff --git a/vp8/encoder/pickinter.c b/vp8/encoder/pickinter.c
index c56593e..765ff26 100644
--- a/vp8/encoder/pickinter.c
+++ b/vp8/encoder/pickinter.c
@@ -938,7 +938,6 @@
         best_mbmode.uv_mode = 0;
         best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
         best_mbmode.partitioning = 0;
-        best_mbmode.dc_diff = 0;
 
         vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
         vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
diff --git a/vp8/encoder/ratectrl.c b/vp8/encoder/ratectrl.c
index 9821d29..9c8e86b 100644
--- a/vp8/encoder/ratectrl.c
+++ b/vp8/encoder/ratectrl.c
@@ -1452,86 +1452,83 @@
     return (bits_per_mb_at_max_q * cpi->common.MBs) >> BPER_MB_NORMBITS;
 }
 
-void vp8_adjust_key_frame_context(VP8_COMP *cpi)
+
+static int estimate_keyframe_frequency(VP8_COMP *cpi)
 {
     int i;
-    int av_key_frames_per_second;
 
-    // Average key frame frequency and size
-    unsigned int total_weight = 0;
-    unsigned int av_key_frame_frequency = 0;
-    unsigned int av_key_frame_bits = 0;
+    // Average key frame frequency
+    int av_key_frame_frequency = 0;
 
-    unsigned int output_frame_rate = (unsigned int)(100 * cpi->output_frame_rate);
-    unsigned int target_bandwidth = (unsigned int)(100 * cpi->target_bandwidth);
-
-    // Clear down mmx registers to allow floating point in what follows
-    vp8_clear_system_state();  //__asm emms;
-
-    // Update the count of total key frame bits
-    cpi->tot_key_frame_bits += cpi->projected_frame_size;
-
-    // First key frame at start of sequence is a special case. We have no frequency data.
+    /* First key frame at start of sequence is a special case. We have no
+     * frequency data.
+     */
     if (cpi->key_frame_count == 1)
     {
-        av_key_frame_frequency = (int)cpi->output_frame_rate * 2;            // Assume a default of 1 kf every 2 seconds
-        av_key_frame_bits = cpi->projected_frame_size;
-        av_key_frames_per_second  = output_frame_rate / av_key_frame_frequency;  // Note output_frame_rate not cpi->output_frame_rate
+        /* Assume a default of 1 kf every 2 seconds, or the max kf interval,
+         * whichever is smaller.
+         */
+        av_key_frame_frequency = (int)cpi->output_frame_rate * 2;
+        if (av_key_frame_frequency > cpi->oxcf.key_freq)
+            av_key_frame_frequency = cpi->oxcf.key_freq;
+
+        cpi->prior_key_frame_distance[KEY_FRAME_CONTEXT - 1]
+            = av_key_frame_frequency;
     }
     else
     {
+        unsigned int total_weight = 0;
         int last_kf_interval =
                 (cpi->frames_since_key > 0) ? cpi->frames_since_key : 1;
 
-        // reset keyframe context and calculate weighted average of last KEY_FRAME_CONTEXT keyframes
+        /* reset keyframe context and calculate weighted average of last
+         * KEY_FRAME_CONTEXT keyframes
+         */
         for (i = 0; i < KEY_FRAME_CONTEXT; i++)
         {
             if (i < KEY_FRAME_CONTEXT - 1)
-            {
-                cpi->prior_key_frame_size[i]     = cpi->prior_key_frame_size[i+1];
-                cpi->prior_key_frame_distance[i] = cpi->prior_key_frame_distance[i+1];
-            }
+                cpi->prior_key_frame_distance[i]
+                    = cpi->prior_key_frame_distance[i+1];
             else
-            {
-                cpi->prior_key_frame_size[i]     = cpi->projected_frame_size;
                 cpi->prior_key_frame_distance[i] = last_kf_interval;
-            }
 
-            av_key_frame_bits      += prior_key_frame_weight[i] * cpi->prior_key_frame_size[i];
-            av_key_frame_frequency += prior_key_frame_weight[i] * cpi->prior_key_frame_distance[i];
-            total_weight         += prior_key_frame_weight[i];
+            av_key_frame_frequency += prior_key_frame_weight[i]
+                                      * cpi->prior_key_frame_distance[i];
+            total_weight += prior_key_frame_weight[i];
         }
 
-        av_key_frame_bits       /= total_weight;
         av_key_frame_frequency  /= total_weight;
-        av_key_frames_per_second  = output_frame_rate / av_key_frame_frequency;
 
     }
+    return av_key_frame_frequency;
+}
+
+
+void vp8_adjust_key_frame_context(VP8_COMP *cpi)
+{
+    // Clear down mmx registers to allow floating point in what follows
+    vp8_clear_system_state();
 
     // Do we have any key frame overspend to recover?
-    if ((cpi->pass != 2) && (cpi->projected_frame_size > cpi->per_frame_bandwidth))
+    // Two-pass overspend handled elsewhere.
+    if ((cpi->pass != 2)
+         && (cpi->projected_frame_size > cpi->per_frame_bandwidth))
     {
-        // Update the count of key frame overspend to be recovered in subsequent frames
-        // A portion of the KF overspend is treated as gf overspend (and hence recovered more quickly)
-        // as the kf is also a gf. Otherwise the few frames following each kf tend to get more bits
-        // allocated than those following other gfs.
-        cpi->kf_overspend_bits += (cpi->projected_frame_size - cpi->per_frame_bandwidth) * 7 / 8;
-        cpi->gf_overspend_bits += (cpi->projected_frame_size - cpi->per_frame_bandwidth) * 1 / 8;
-        if(!av_key_frame_frequency)
-            av_key_frame_frequency = 60;
+        int overspend;
 
-        // Work out how much to try and recover per frame.
-        // For one pass we estimate the number of frames to spread it over based upon past history.
-        // For two pass we know how many frames there will be till the next kf.
-        if (cpi->pass == 2)
-        {
-            if (cpi->frames_to_key > 16)
-                cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits / (int)cpi->frames_to_key;
-            else
-                cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits / 16;
-        }
-        else
-            cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits / (int)av_key_frame_frequency;
+        /* Update the count of key frame overspend to be recovered in
+         * subsequent frames. A portion of the KF overspend is treated as gf
+         * overspend (and hence recovered more quickly) as the kf is also a
+         * gf. Otherwise the few frames following each kf tend to get more
+         * bits allocated than those following other gfs.
+         */
+        overspend = (cpi->projected_frame_size - cpi->per_frame_bandwidth);
+        cpi->kf_overspend_bits += overspend * 7 / 8;
+        cpi->gf_overspend_bits += overspend * 1 / 8;
+
+        /* Work out how much to try and recover per frame. */
+        cpi->kf_bitrate_adjustment = cpi->kf_overspend_bits
+                                     / estimate_keyframe_frequency(cpi);
     }
 
     cpi->frames_since_key = 0;
@@ -1539,6 +1536,7 @@
     cpi->key_frame_count++;
 }
 
+
 void vp8_compute_frame_size_bounds(VP8_COMP *cpi, int *frame_under_shoot_limit, int *frame_over_shoot_limit)
 {
     // Set-up bounds on acceptable frame size:
diff --git a/vp8/encoder/rdopt.c b/vp8/encoder/rdopt.c
index 6c4879b..1f329d7 100644
--- a/vp8/encoder/rdopt.c
+++ b/vp8/encoder/rdopt.c
@@ -2461,7 +2461,6 @@
         best_mbmode.uv_mode = 0;
         best_mbmode.mb_skip_coeff = (cpi->common.mb_no_coeff_skip) ? 1 : 0;
         best_mbmode.partitioning = 0;
-        best_mbmode.dc_diff = 0;
 
         vpx_memcpy(&x->e_mbd.mode_info_context->mbmi, &best_mbmode, sizeof(MB_MODE_INFO));
         vpx_memcpy(x->partition_info, &best_partition, sizeof(PARTITION_INFO));
diff --git a/vp8/encoder/tokenize.c b/vp8/encoder/tokenize.c
index ccf0c7f..1c59238 100644
--- a/vp8/encoder/tokenize.c
+++ b/vp8/encoder/tokenize.c
@@ -224,18 +224,9 @@
     int plane_type;
     int b;
 
-    TOKENEXTRA *start = *t;
-    TOKENEXTRA *tp = *t;
-
-    x->mode_info_context->mbmi.dc_diff = 1;
-
-
-#if 1
-
     x->mode_info_context->mbmi.mb_skip_coeff = mb_is_skippable(x);
     if (x->mode_info_context->mbmi.mb_skip_coeff)
     {
-
         cpi->skip_true_count++;
 
         if (!cpi->common.mb_no_coeff_skip)
@@ -245,17 +236,11 @@
             vp8_fix_contexts(x);
         }
 
-        if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
-            x->mode_info_context->mbmi.dc_diff = 0;
-        else
-            x->mode_info_context->mbmi.dc_diff = 1;
-
-
         return;
     }
 
     cpi->skip_false_count++;
-#endif
+
 #if 0
     vpx_memcpy(cpi->coef_counts_backup, cpi->coef_counts, sizeof(cpi->coef_counts));
 #endif
@@ -282,42 +267,6 @@
                             A + vp8_block2above[b],
                             L + vp8_block2left[b], cpi);
 
-#if 0
-
-    if (cpi->common.mb_no_coeff_skip)
-    {
-        int skip = 1;
-
-        while ((tp != *t) && skip)
-        {
-            skip = (skip && (tp->Token == DCT_EOB_TOKEN));
-            tp ++;
-        }
-
-        if (skip != x->mbmi.mb_skip_coeff)
-            skip += 0;
-
-        x->mbmi.mb_skip_coeff = skip;
-
-        if (x->mbmi.mb_skip_coeff == 1)
-        {
-            x->mbmi.dc_diff = 0;
-            //redo the coutnts
-            vpx_memcpy(cpi->coef_counts, cpi->coef_counts_backup, sizeof(cpi->coef_counts));
-
-            *t = start;
-            cpi->skip_true_count++;
-            //skip_true_count++;
-        }
-        else
-        {
-
-            cpi->skip_false_count++;
-            //skip_false_count++;
-        }
-    }
-
-#endif
 }
 
 
@@ -500,13 +449,6 @@
                      A + vp8_block2above[24], L + vp8_block2left[24], cpi);
     plane_type = 0;
 
-
-    if (x->mode_info_context->mbmi.mode != B_PRED && x->mode_info_context->mbmi.mode != SPLITMV)
-        x->mode_info_context->mbmi.dc_diff = 0;
-    else
-        x->mode_info_context->mbmi.dc_diff = 1;
-
-
     for (b = 0; b < 16; b++)
         stuff1st_order_b(x->block + b, t, plane_type, x->frame_type,
                          A + vp8_block2above[b],
diff --git a/vp8/encoder/x86/temporal_filter_apply_sse2.asm b/vp8/encoder/x86/temporal_filter_apply_sse2.asm
index 30674c8..b777ef5 100644
--- a/vp8/encoder/x86/temporal_filter_apply_sse2.asm
+++ b/vp8/encoder/x86/temporal_filter_apply_sse2.asm
@@ -164,10 +164,10 @@
         movdqa      xmm6,           [rdi+32]
         movdqa      xmm7,           [rdi+48]
         ; += modifier
-        paddw       xmm4,           xmm0
-        paddw       xmm5,           xmm2
-        paddw       xmm6,           xmm1
-        paddw       xmm7,           xmm3
+        paddd       xmm4,           xmm0
+        paddd       xmm5,           xmm2
+        paddd       xmm6,           xmm1
+        paddd       xmm7,           xmm3
         ; write back
         movdqa      [rdi],          xmm4
         movdqa      [rdi+16],       xmm5