shithub: libvpx

Download patch

ref: 92519a005ade01571e208224903832ee5295051f
parent: ccc5a483f45d0ac179bd18cdde33ea84348cc8e2
parent: 30dee8adfc2913daa2e0e22b5ea047b0d7b394a5
author: Jim Bankoski <jimbankoski@google.com>
date: Mon Oct 7 05:16:46 EDT 2013

Merge "cpplint problems resolved with vp9_firstpass.c"

--- a/vp9/encoder/vp9_firstpass.c
+++ b/vp9/encoder/vp9_firstpass.c
@@ -8,8 +8,9 @@
  *  be found in the AUTHORS file in the root of the source tree.
  */
 
-#include "math.h"
-#include "limits.h"
+#include <math.h>
+#include <limits.h>
+#include <stdio.h>
 #include "vp9/encoder/vp9_block.h"
 #include "vp9/encoder/vp9_onyx_int.h"
 #include "vp9/encoder/vp9_variance.h"
@@ -23,7 +24,6 @@
 #include "vp9/common/vp9_systemdependent.h"
 #include "vpx_mem/vpx_mem.h"
 #include "vpx_scale/yv12config.h"
-#include <stdio.h>
 #include "vp9/encoder/vp9_quantize.h"
 #include "vp9/encoder/vp9_rdopt.h"
 #include "vp9/encoder/vp9_ratectrl.h"
@@ -77,7 +77,8 @@
 }
 
 
-// Resets the first pass file to the given position using a relative seek from the current position
+// Resets the first pass file to the given position using a relative seek from
+// the current position.
 static void reset_fpf_position(VP9_COMP *cpi, FIRSTPASS_STATS *position) {
   cpi->twopass.stats_in = position;
 }
@@ -250,8 +251,10 @@
   section->duration   /= section->count;
 }
 
-// Calculate a modified Error used in distributing bits between easier and harder frames
-static double calculate_modified_err(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
+// Calculate a modified Error used in distributing bits between easier and
+// harder frames.
+static double calculate_modified_err(VP9_COMP *cpi,
+                                     FIRSTPASS_STATS *this_frame) {
   const FIRSTPASS_STATS *const stats = &cpi->twopass.total_stats;
   const double av_err = stats->ssim_weighted_pred_err / stats->count;
   const double this_err = this_frame->ssim_weighted_pred_err;
@@ -260,38 +263,43 @@
 }
 
 static const double weight_table[256] = {
-  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
-  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
-  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
-  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
-  0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750,
-  0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750,
-  0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
-  0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
-  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
+  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
+  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
+  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
+  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000,
+  0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.031250, 0.062500,
+  0.093750, 0.125000, 0.156250, 0.187500, 0.218750, 0.250000, 0.281250,
+  0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750, 0.500000,
+  0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750,
+  0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500,
+  0.968750, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,
+  1.000000, 1.000000, 1.000000, 1.000000
 };
 
 static double simple_weight(YV12_BUFFER_CONFIG *source) {
@@ -300,7 +308,8 @@
   uint8_t *src = source->y_buffer;
   double sum_weights = 0.0;
 
-  // Loop throught the Y plane raw examining levels and creating a weight for the image
+  // Loop through the Y plane examining levels and creating a weight for
+  // the image.
   i = source->y_height;
   do {
     j = source->y_width;
@@ -340,7 +349,9 @@
   output_stats(cpi, cpi->output_pkt_list, &cpi->twopass.total_stats);
 }
 
-static void zz_motion_search(VP9_COMP *cpi, MACROBLOCK *x, YV12_BUFFER_CONFIG *recon_buffer, int *best_motion_err, int recon_yoffset) {
+static void zz_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
+                             YV12_BUFFER_CONFIG *recon_buffer,
+                             int *best_motion_err, int recon_yoffset) {
   MACROBLOCKD *const xd = &x->e_mbd;
 
   // Set up pointers for this macro block recon buffer
@@ -444,9 +455,9 @@
   while (n < further_steps) {
     n++;
 
-    if (num00)
+    if (num00) {
       num00--;
-    else {
+    } else {
       tmp_err = cpi->diamond_search_sad(x, &ref_mv_full, &tmp_mv,
                                         step_param + n, x->sadperbit16,
                                         &num00, &v_fn_ptr,
@@ -574,9 +585,12 @@
       // do intra 16x16 prediction
       this_error = vp9_encode_intra(x, use_dc_pred);
 
-      // "intrapenalty" below deals with situations where the intra and inter error scores are very low (eg a plain black frame)
-      // We do not have special cases in first pass for 0,0 and nearest etc so all inter modes carry an overhead cost estimate fot the mv.
-      // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames.
+      // intrapenalty below deals with situations where the intra and inter
+      // error scores are very low (eg a plain black frame).
+      // We do not have special cases in first pass for 0,0 and nearest etc so
+      // all inter modes carry an overhead cost estimate for the mv.
+      // When the error score is very low this causes us to pick all or lots of
+      // INTRA modes and throw lots of key frames.
       // This penalty adds a cost matching that of a 0,0 mv to the intra case.
       this_error += intrapenalty;
 
@@ -583,7 +597,8 @@
       // Cumulative intra error total
       intra_error += (int64_t)this_error;
 
-      // Set up limit values for motion vectors to prevent them extending outside the UMV borders
+      // Set up limit values for motion vectors to prevent them extending
+      // outside the UMV borders.
       x->mv_col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
       x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16)
                       + BORDER_MV_PIXELS_B16;
@@ -604,7 +619,8 @@
                                  &mv.as_mv, lst_yv12,
                                  &motion_error, recon_yoffset);
 
-        // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
+        // If the current best reference mv is not centered on 0,0 then do a 0,0
+        // based search as well.
         if (best_ref_mv.as_int) {
           tmp_err = INT_MAX;
           first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv.as_mv,
@@ -644,9 +660,9 @@
             sr_coded_error += gf_motion_error;
           else
             sr_coded_error += this_error;
-        } else
+        } else {
           sr_coded_error += motion_error;
-
+        }
         /* Intra assumed best */
         best_ref_mv.as_int = 0;
 
@@ -718,9 +734,9 @@
             }
           }
         }
-      } else
+      } else {
         sr_coded_error += (int64_t)this_error;
-
+      }
       coded_error += (int64_t)this_error;
 
       // adjust to the next column of macroblocks
@@ -779,8 +795,10 @@
       fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
       fps.MVc = (double)sum_mvc / (double)mvcount;
       fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
-      fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (double)mvcount;
-      fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (double)mvcount;
+      fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) /
+                 (double)mvcount;
+      fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) /
+                 (double)mvcount;
       fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
       fps.new_mv_count = new_mv_count;
 
@@ -787,8 +805,9 @@
       fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
     }
 
-    // TODO:  handle the case when duration is set to 0, or something less
-    // than the full time between subsequent values of cpi->source_time_stamp.
+    // TODO(paulwilkins):  Handle the case when duration is set to 0, or
+    // something less than the full time between subsequent values of
+    // cpi->source_time_stamp.
     fps.duration = (double)(cpi->source->ts_end
                             - cpi->source->ts_start);
 
@@ -808,15 +827,16 @@
         2.0))) {
     vp8_yv12_copy_frame(lst_yv12, gld_yv12);
     cpi->twopass.sr_update_lag = 1;
-  } else
+  } else {
     cpi->twopass.sr_update_lag++;
-
+  }
   // swap frame pointers so last frame refers to the frame we just compressed
   swap_yv12(lst_yv12, new_yv12);
 
   vp9_extend_frame_borders(lst_yv12, cm->subsampling_x, cm->subsampling_y);
 
-  // Special case for the first frame. Copy into the GF buffer as a second reference.
+  // Special case for the first frame. Copy into the GF buffer as a second
+  // reference.
   if (cm->current_video_frame == 0)
     vp8_yv12_copy_frame(lst_yv12, gld_yv12);
 
@@ -824,7 +844,8 @@
   if (0) {
     char filename[512];
     FILE *recon_file;
-    sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
+    snprintf(filename, sizeof(filename), "enc%04d.yuv",
+             (int)cm->current_video_frame);
 
     if (cm->current_video_frame == 0)
       recon_file = fopen(filename, "wb");
@@ -836,7 +857,6 @@
   }
 
   cm->current_video_frame++;
-
 }
 
 // Estimate a cost per mb attributable to overheads such as the coding of
@@ -879,7 +899,7 @@
            (av_intra * intra_cost)) * cpi->common.MBs) << 9;
 
   // return mv_cost + mode_cost;
-  // TODO PGW Fix overhead costs for extended Q range
+  // TODO(paulwilkins): Fix overhead costs for extended Q range.
 #endif
   return 0;
 }
@@ -1103,8 +1123,8 @@
   FIRSTPASS_STATS *start_pos;
 
   double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.framerate;
-  double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
-                                      * cpi->oxcf.two_pass_vbrmin_section / 100);
+  double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth *
+                                      cpi->oxcf.two_pass_vbrmin_section / 100);
 
   if (two_pass_min_rate < lower_bounds_min_rate)
     two_pass_min_rate = lower_bounds_min_rate;
@@ -1142,15 +1162,17 @@
   // This variable monitors how far behind the second ref update is lagging
   cpi->twopass.sr_update_lag = 1;
 
-  // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence
+  // Scan the first pass file and calculate an average Intra / Inter error score
+  // ratio for the sequence.
   {
     double sum_iiratio = 0.0;
     double IIRatio;
 
-    start_pos = cpi->twopass.stats_in;               // Note starting "file" position
+    start_pos = cpi->twopass.stats_in;  // Note the starting "file" position.
 
     while (input_stats(cpi, &this_frame) != EOF) {
-      IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
+      IIRatio = this_frame.intra_error
+                / DOUBLE_DIVIDE_CHECK(this_frame.coded_error);
       IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
       sum_iiratio += IIRatio;
     }
@@ -1162,21 +1184,21 @@
     reset_fpf_position(cpi, start_pos);
   }
 
-  // Scan the first pass file and calculate a modified total error based upon the bias/power function
-  // used to allocate bits
+  // Scan the first pass file and calculate a modified total error based upon
+  // the bias/power function used to allocate bits.
   {
-    start_pos = cpi->twopass.stats_in;               // Note starting "file" position
+    start_pos = cpi->twopass.stats_in;  // Note starting "file" position
 
     cpi->twopass.modified_error_total = 0.0;
     cpi->twopass.modified_error_used = 0.0;
 
     while (input_stats(cpi, &this_frame) != EOF) {
-      cpi->twopass.modified_error_total += calculate_modified_err(cpi, &this_frame);
+      cpi->twopass.modified_error_total +=
+          calculate_modified_err(cpi, &this_frame);
     }
     cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
 
-    reset_fpf_position(cpi, start_pos);            // Reset file position
-
+    reset_fpf_position(cpi, start_pos);  // Reset file position
   }
 }
 
@@ -1322,7 +1344,6 @@
       (this_frame_mvc_ratio < this_frame->mvc_abs)
       ? (this_frame_mvc_ratio * motion_pct)
       : this_frame->mvc_abs * motion_pct;
-
   }
 }
 
@@ -1381,7 +1402,8 @@
     // Update the motion related elements to the boost calculation
     accumulate_frame_motion_stats(&this_frame,
                                   &this_frame_mv_in_out, &mv_in_out_accumulator,
-                                  &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
+                                  &abs_mv_in_out_accumulator,
+                                  &mv_ratio_accumulator);
 
     // We want to discount the flash frame itself and the recovery
     // frame that follows as both will have poor scores.
@@ -1417,7 +1439,8 @@
     // Update the motion related elements to the boost calculation
     accumulate_frame_motion_stats(&this_frame,
                                   &this_frame_mv_in_out, &mv_in_out_accumulator,
-                                  &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
+                                  &abs_mv_in_out_accumulator,
+                                  &mv_ratio_accumulator);
 
     // We want to discount the the flash frame itself and the recovery
     // frame that follows as both will have poor scores.
@@ -1433,7 +1456,6 @@
 
     boost_score += (decay_accumulator *
                     calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
-
   }
   *b_boost = (int)boost_score;
 
@@ -1667,7 +1689,8 @@
     // Update the motion related elements to the boost calculation
     accumulate_frame_motion_stats(&next_frame,
                                   &this_frame_mv_in_out, &mv_in_out_accumulator,
-                                  &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
+                                  &abs_mv_in_out_accumulator,
+                                  &mv_ratio_accumulator);
 
     // Cumulative effect of prediction quality decay
     if (!flash_detected) {
@@ -1710,8 +1733,7 @@
         ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
          (abs_mv_in_out_accumulator > 3.0) ||
          (mv_in_out_accumulator < -2.0) ||
-         ((boost_score - old_boost_score) < IIFACTOR))
-      )) {
+         ((boost_score - old_boost_score) < IIFACTOR)))) {
       boost_score = old_boost_score;
       break;
     }
@@ -1765,7 +1787,8 @@
        (mv_in_out_accumulator > -2.0)) &&
       (boost_score > 100)) {
     // Alternative boost calculation for alt ref
-    cpi->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, &b_boost);
+    cpi->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost,
+                                    &b_boost);
     cpi->source_alt_ref_pending = 1;
 
 #if CONFIG_MULTIPLE_ARF
@@ -1842,9 +1865,9 @@
     cpi->twopass.gf_group_bits =
       (int64_t)(cpi->twopass.kf_group_bits *
                 (gf_group_err / cpi->twopass.kf_group_error_left));
-  } else
+  } else {
     cpi->twopass.gf_group_bits = 0;
-
+  }
   cpi->twopass.gf_group_bits =
     (cpi->twopass.gf_group_bits < 0)
     ? 0
@@ -1908,11 +1931,10 @@
 
       if (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
-    // least receives an allocation in keeping with its relative error
-    // score, otherwise it may be worse off than an "un-boosted" frame
-    else {
+    } else {
+      // If it is harder than other frames in the group make sure it at
+      // least receives an allocation in keeping with its relative error
+      // score, otherwise it may be worse off than an "un-boosted" frame.
       int alt_gf_bits = (int)((double)cpi->twopass.kf_group_bits *
                         mod_frame_err /
                         DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left));
@@ -2024,9 +2046,9 @@
 
   // Clip target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at
   // the top end.
-  if (target_frame_size < 0)
+  if (target_frame_size < 0) {
     target_frame_size = 0;
-  else {
+  } else {
     if (target_frame_size > max_bits)
       target_frame_size = max_bits;
 
@@ -2249,16 +2271,17 @@
   if ((this_frame->pcnt_second_ref < 0.10) &&
       (next_frame->pcnt_second_ref < 0.10) &&
       ((this_frame->pcnt_inter < 0.05) ||
-       (
-         ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .35) &&
-         ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
-         ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVIDE_CHECK(this_frame->coded_error) > .40) ||
-          (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVIDE_CHECK(this_frame->intra_error) > .40) ||
-          ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5)
-         )
-       )
-      )
-     ) {
+       (((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .35) &&
+        ((this_frame->intra_error /
+          DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) < 2.5) &&
+        ((fabs(last_frame->coded_error - this_frame->coded_error) /
+              DOUBLE_DIVIDE_CHECK(this_frame->coded_error) >
+          .40) ||
+         (fabs(last_frame->intra_error - this_frame->intra_error) /
+              DOUBLE_DIVIDE_CHECK(this_frame->intra_error) >
+          .40) ||
+         ((next_frame->intra_error /
+           DOUBLE_DIVIDE_CHECK(next_frame->coded_error)) > 3.5))))) {
     int i;
     FIRSTPASS_STATS *start_pos;
 
@@ -2276,7 +2299,8 @@
 
     // Examine how well the key frame predicts subsequent frames
     for (i = 0; i < 16; i++) {
-      next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
+      next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error /
+                      DOUBLE_DIVIDE_CHECK(local_next_frame.coded_error));
 
       if (next_iiratio > RMAX)
         next_iiratio = RMAX;
@@ -2285,7 +2309,8 @@
       if (local_next_frame.pcnt_inter > 0.85)
         decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
       else
-        decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
+        decay_accumulator =
+            decay_accumulator * ((0.85 + local_next_frame.pcnt_inter) / 2.0);
 
       // decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
 
@@ -2313,9 +2338,9 @@
 
     // If there is tolerable prediction for at least the next 3 frames then
     // break out else discard this potential key frame and move on
-    if (boost_score > 30.0 && (i > 3))
+    if (boost_score > 30.0 && (i > 3)) {
       is_viable_kf = 1;
-    else {
+    } else {
       // Reset the file position
       reset_fpf_position(cpi, start_pos);
 
@@ -2375,8 +2400,9 @@
     // Accumulate kf group error
     kf_group_err += calculate_modified_err(cpi, this_frame);
 
-    // These figures keep intra and coded error counts for all frames including key frames in the group.
-    // The effect of the key frame itself can be subtracted out using the first_frame data collected above
+    // These figures keep intra and coded error counts for all frames including
+    // key frames in the group. The effect of the key frame itself can be
+    // subtracted out using the first_frame data collected above.
     kf_group_intra_err += this_frame->intra_error;
     kf_group_coded_err += this_frame->coded_error;
 
@@ -2416,9 +2442,9 @@
       // forcekeyframeevery intervals then break out of the loop.
       if (cpi->twopass.frames_to_key >= 2 * (int)cpi->key_frame_frequency)
         break;
-    } else
+    } else {
       cpi->twopass.frames_to_key++;
-
+    }
     i++;
   }
 
@@ -2458,22 +2484,24 @@
     reset_fpf_position(cpi, current_pos);
 
     cpi->next_key_frame_forced = 1;
-  } else
+  } else {
     cpi->next_key_frame_forced = 0;
-
+  }
   // Special case for the last frame of the file
   if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) {
     // Accumulate kf group error
     kf_group_err += calculate_modified_err(cpi, this_frame);
 
-    // These figures keep intra and coded error counts for all frames including key frames in the group.
-    // The effect of the key frame itself can be subtracted out using the first_frame data collected above
+    // These figures keep intra and coded error counts for all frames including
+    // key frames in the group. The effect of the key frame itself can be
+    // subtracted out using the first_frame data collected above.
     kf_group_intra_err += this_frame->intra_error;
     kf_group_coded_err += this_frame->coded_error;
   }
 
   // Calculate the number of bits that should be assigned to the kf group.
-  if ((cpi->twopass.bits_left > 0) && (cpi->twopass.modified_error_left > 0.0)) {
+  if ((cpi->twopass.bits_left > 0) &&
+      (cpi->twopass.modified_error_left > 0.0)) {
     // Max for a single normal frame (not key frame)
     int max_bits = frame_max_bits(cpi);
 
@@ -2490,13 +2518,14 @@
     max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
     if (cpi->twopass.kf_group_bits > max_grp_bits)
       cpi->twopass.kf_group_bits = max_grp_bits;
-  } else
+  } else {
     cpi->twopass.kf_group_bits = 0;
-
+  }
   // Reset the first pass file position
   reset_fpf_position(cpi, start_position);
 
-  // determine how big to make this keyframe based on how well the subsequent frames use inter blocks
+  // Determine how big to make this keyframe based on how well the subsequent
+  // frames use inter blocks.
   decay_accumulator = 1.0;
   boost_score = 0.0;
   loop_decay_rate = 1.00;       // Starting decay rate
@@ -2569,7 +2598,7 @@
     if (kf_boost < (cpi->twopass.frames_to_key * 3))
       kf_boost = (cpi->twopass.frames_to_key * 3);
 
-    if (kf_boost < 300) // Min KF boost
+    if (kf_boost < 300)  // Min KF boost
       kf_boost = 300;
 
     // Make a note of baseline boost and the zero motion
@@ -2604,10 +2633,13 @@
       allocation_chunks /= divisor;
     }
 
-    cpi->twopass.kf_group_bits = (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
+    cpi->twopass.kf_group_bits =
+        (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->twopass.kf_group_bits;
 
     // Calculate the number of bits to be spent on the key frame
-    cpi->twopass.kf_bits  = (int)((double)kf_boost * ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
+    cpi->twopass.kf_bits =
+        (int)((double)kf_boost *
+              ((double)cpi->twopass.kf_group_bits / (double)allocation_chunks));
 
     // If the key frame is actually easier than the average for the
     // kf group (which does sometimes happen... eg a blank intro frame)
@@ -2625,11 +2657,10 @@
       if (cpi->twopass.kf_bits > alt_kf_bits) {
         cpi->twopass.kf_bits = alt_kf_bits;
       }
-    }
+    } else {
     // Else if it is much harder than other frames in the group make sure
     // it at least receives an allocation in keeping with its relative
     // error score
-    else {
       alt_kf_bits =
         (int)((double)cpi->twopass.bits_left *
               (kf_mod_err /
@@ -2655,6 +2686,7 @@
   cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
 
   // Adjust the count of total modified error left.
-  // The count of bits left is adjusted elsewhere based on real coded frame sizes
+  // The count of bits left is adjusted elsewhere based on real coded frame
+  // sizes.
   cpi->twopass.modified_error_left -= kf_group_err;
 }