shithub: libvpx

Download patch

ref: 412e4954c121b483b87db6eb3f21db1e40b0edb4
parent: 0d21d79bbccfb72b06fa48a9507af109a933b5e4
author: Deb Mukherjee <debargha@google.com>
date: Wed Jan 8 12:55:07 EST 2014

Cleanups on refresh flags

Cleanups on frame refresh flags and external overrides.

Change-Id: Ia6a56fe1bde906b1dc3fcbf4ef1c7b207cd2df2d

--- a/vp9/encoder/vp9_firstpass.c
+++ b/vp9/encoder/vp9_firstpass.c
@@ -2033,8 +2033,8 @@
   } else {
     cm->frame_type = INTER_FRAME;
   }
-  cpi->rc.frames_to_key = INT_MAX;
   // Do not use periodic key frames
+  cpi->rc.frames_to_key = INT_MAX;
 }
 
 void vp9_get_second_pass_params(VP9_COMP *cpi) {
--- a/vp9/encoder/vp9_onyx_if.c
+++ b/vp9/encoder/vp9_onyx_if.c
@@ -1350,8 +1350,6 @@
 
   cpi->ref_frame_flags = VP9_ALT_FLAG | VP9_GOLD_FLAG | VP9_LAST_FLAG;
 
-  // cpi->use_golden_frame_only = 0;
-  // cpi->use_last_frame_only = 0;
   cpi->refresh_golden_frame = 0;
   cpi->refresh_last_frame = 1;
   cm->refresh_frame_context = 1;
@@ -1453,6 +1451,9 @@
 #endif
 
   set_tile_limits(cpi);
+
+  cpi->ext_refresh_frame_flags_pending = 0;
+  cpi->ext_refresh_frame_context_pending = 0;
 }
 
 #define M_LOG2_E 0.693147180559945309417
@@ -2249,19 +2250,20 @@
   if (ref_frame_flags > 7)
     return -1;
 
-  cpi->refresh_golden_frame = 0;
-  cpi->refresh_alt_ref_frame = 0;
-  cpi->refresh_last_frame   = 0;
+  cpi->ext_refresh_golden_frame = 0;
+  cpi->ext_refresh_alt_ref_frame = 0;
+  cpi->ext_refresh_last_frame   = 0;
 
   if (ref_frame_flags & VP9_LAST_FLAG)
-    cpi->refresh_last_frame = 1;
+    cpi->ext_refresh_last_frame = 1;
 
   if (ref_frame_flags & VP9_GOLD_FLAG)
-    cpi->refresh_golden_frame = 1;
+    cpi->ext_refresh_golden_frame = 1;
 
   if (ref_frame_flags & VP9_ALT_FLAG)
-    cpi->refresh_alt_ref_frame = 1;
+    cpi->ext_refresh_alt_ref_frame = 1;
 
+  cpi->ext_refresh_frame_flags_pending = 1;
   return 0;
 }
 
@@ -2318,7 +2320,8 @@
 }
 
 int vp9_update_entropy(VP9_PTR comp, int update) {
-  ((VP9_COMP *)comp)->common.refresh_frame_context = update;
+  ((VP9_COMP *)comp)->ext_refresh_frame_context = update;
+  ((VP9_COMP *)comp)->ext_refresh_frame_context_pending = 1;
   return 0;
 }
 
@@ -2970,6 +2973,23 @@
     cpi->ref_frame_flags &= ~VP9_ALT_FLAG;
 }
 
+static void set_ext_overrides(VP9_COMP *cpi) {
+  // Overrides the defaults with the externally supplied values with
+  // vp9_update_reference() and vp9_update_entropy() calls
+  // Note: The overrides are valid only for the next frame passed
+  // to encode_frame_to_data_rate() function
+  if (cpi->ext_refresh_frame_context_pending) {
+    cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
+    cpi->ext_refresh_frame_context_pending = 0;
+  }
+  if (cpi->ext_refresh_frame_flags_pending) {
+    cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
+    cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
+    cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
+    cpi->ext_refresh_frame_flags_pending = 0;
+  }
+}
+
 static void encode_frame_to_data_rate(VP9_COMP *cpi,
                                       size_t *size,
                                       uint8_t *dest,
@@ -2986,6 +3006,8 @@
   unsigned int max_mv_def = MIN(cpi->common.width, cpi->common.height);
   struct segmentation *const seg = &cm->seg;
 
+  set_ext_overrides(cpi);
+
   /* Scale the source buffer, if required. */
   if (cm->mi_cols * 8 != cpi->un_scaled_source->y_width ||
       cm->mi_rows * 8 != cpi->un_scaled_source->y_height) {
@@ -3161,6 +3183,7 @@
     cpi->ambient_err = vp9_calc_ss_err(cpi->Source, get_frame_new_buffer(cm));
   }
 
+  // If the encoder forced a KEY_FRAME decision
   if (cm->frame_type == KEY_FRAME)
     cpi->refresh_last_frame = 1;
 
@@ -3391,6 +3414,44 @@
 }
 #endif
 
+void adjust_frame_rate(VP9_COMP *cpi) {
+  int64_t this_duration;
+  int step = 0;
+
+  if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
+    this_duration = cpi->source->ts_end - cpi->source->ts_start;
+    step = 1;
+  } else {
+    int64_t last_duration = cpi->last_end_time_stamp_seen
+        - cpi->last_time_stamp_seen;
+
+    this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
+
+    // do a step update if the duration changes by 10%
+    if (last_duration)
+      step = (int)((this_duration - last_duration) * 10 / last_duration);
+  }
+
+  if (this_duration) {
+    if (step) {
+      vp9_new_framerate(cpi, 10000000.0 / this_duration);
+    } else {
+      // Average this frame's rate into the last second's average
+      // frame rate. If we haven't seen 1 second yet, then average
+      // over the whole interval seen.
+      const double interval = MIN((double)(cpi->source->ts_end
+                                   - cpi->first_time_stamp_ever), 10000000.0);
+      double avg_duration = 10000000.0 / cpi->oxcf.framerate;
+      avg_duration *= (interval - avg_duration + this_duration);
+      avg_duration /= interval;
+
+      vp9_new_framerate(cpi, 10000000.0 / avg_duration);
+    }
+  }
+  cpi->last_time_stamp_seen = cpi->source->ts_start;
+  cpi->last_end_time_stamp_seen = cpi->source->ts_end;
+}
+
 int vp9_get_compressed_data(VP9_PTR ptr, unsigned int *frame_flags,
                             size_t *size, uint8_t *dest,
                             int64_t *time_stamp, int64_t *time_end, int flush) {
@@ -3411,6 +3472,13 @@
 
   set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
 
+  // Normal defaults
+  cm->reset_frame_context = 0;
+  cm->refresh_frame_context = 1;
+  cpi->refresh_last_frame = 1;
+  cpi->refresh_golden_frame = 0;
+  cpi->refresh_alt_ref_frame = 0;
+
   // Should we code an alternate reference frame.
   if (cpi->oxcf.play_alternate && cpi->rc.source_alt_ref_pending) {
     int frames_to_arf;
@@ -3421,7 +3489,7 @@
 
     if (cpi->multi_arf_enabled && (cpi->pass == 2))
       frames_to_arf = (-cpi->frame_coding_order[cpi->sequence_number])
-        - cpi->next_frame_in_order;
+          - cpi->next_frame_in_order;
     else
 #endif
       frames_to_arf = cpi->rc.frames_till_gf_update_due;
@@ -3505,19 +3573,7 @@
     *time_end = cpi->source->ts_end;
     *frame_flags = cpi->source->flags;
 
-    // fprintf(fp_out, "   Frame:%d", cm->current_video_frame);
 #if CONFIG_MULTIPLE_ARF
-    if (cpi->multi_arf_enabled) {
-      // fprintf(fp_out, "   seq_no:%d  this_frame_weight:%d",
-      //         cpi->sequence_number, cpi->this_frame_weight);
-    } else {
-      // fprintf(fp_out, "\n");
-    }
-#else
-    // fprintf(fp_out, "\n");
-#endif
-
-#if CONFIG_MULTIPLE_ARF
     if ((cm->frame_type != KEY_FRAME) && (cpi->pass == 2))
       cpi->rc.source_alt_ref_pending = is_next_frame_arf(cpi);
 #endif
@@ -3538,43 +3594,8 @@
   }
 
   // adjust frame rates based on timestamps given
-  if (!cpi->refresh_alt_ref_frame) {
-    int64_t this_duration;
-    int step = 0;
-
-    if (cpi->source->ts_start == cpi->first_time_stamp_ever) {
-      this_duration = cpi->source->ts_end - cpi->source->ts_start;
-      step = 1;
-    } else {
-      int64_t last_duration = cpi->last_end_time_stamp_seen
-                                - cpi->last_time_stamp_seen;
-
-      this_duration = cpi->source->ts_end - cpi->last_end_time_stamp_seen;
-
-      // do a step update if the duration changes by 10%
-      if (last_duration)
-        step = (int)((this_duration - last_duration) * 10 / last_duration);
-    }
-
-    if (this_duration) {
-      if (step) {
-        vp9_new_framerate(cpi, 10000000.0 / this_duration);
-      } else {
-        // Average this frame's rate into the last second's average
-        // frame rate. If we haven't seen 1 second yet, then average
-        // over the whole interval seen.
-        const double interval = MIN((double)(cpi->source->ts_end
-                                     - cpi->first_time_stamp_ever), 10000000.0);
-        double avg_duration = 10000000.0 / cpi->oxcf.framerate;
-        avg_duration *= (interval - avg_duration + this_duration);
-        avg_duration /= interval;
-
-        vp9_new_framerate(cpi, 10000000.0 / avg_duration);
-      }
-    }
-
-    cpi->last_time_stamp_seen = cpi->source->ts_start;
-    cpi->last_end_time_stamp_seen = cpi->source->ts_end;
+  if (cm->show_frame) {
+    adjust_frame_rate(cpi);
   }
 
   // start with a 0 size frame
@@ -3600,21 +3621,6 @@
   }
 #endif
 
-#if 0  // CONFIG_MULTIPLE_ARF
-  if (cpi->multi_arf_enabled) {
-    fprintf(fp_out, "      idx(%d, %d, %d, %d) active(%d, %d, %d)",
-        cpi->lst_fb_idx, cpi->gld_fb_idx, cpi->alt_fb_idx, cm->new_fb_idx,
-        cm->ref_frame_map[cpi->lst_fb_idx],
-        cm->ref_frame_map[cpi->gld_fb_idx],
-        cm->ref_frame_map[cpi->alt_fb_idx]);
-    if (cpi->refresh_alt_ref_frame)
-      fprintf(fp_out, "  type:ARF");
-    if (cpi->rc.is_src_frame_alt_ref)
-      fprintf(fp_out, "  type:OVERLAY[%d]", cpi->alt_fb_idx);
-    fprintf(fp_out, "\n");
-  }
-#endif
-
   cm->frame_flags = *frame_flags;
 
   // Reset the frame pointers to the current frame size
@@ -3665,15 +3671,7 @@
   }
 
   if (*size > 0) {
-    // if its a dropped frame honor the requests on subsequent frames
     cpi->droppable = !frame_is_reference(cpi);
-
-    // return to normal state
-    cm->reset_frame_context = 0;
-    cm->refresh_frame_context = 1;
-    cpi->refresh_alt_ref_frame = 0;
-    cpi->refresh_golden_frame = 0;
-    cpi->refresh_last_frame = 1;
   }
 
   vpx_usec_timer_mark(&cmptimer);
--- a/vp9/encoder/vp9_onyx_int.h
+++ b/vp9/encoder/vp9_onyx_int.h
@@ -400,6 +400,15 @@
   int refresh_last_frame;
   int refresh_golden_frame;
   int refresh_alt_ref_frame;
+
+  int ext_refresh_frame_flags_pending;
+  int ext_refresh_last_frame;
+  int ext_refresh_golden_frame;
+  int ext_refresh_alt_ref_frame;
+
+  int ext_refresh_frame_context_pending;
+  int ext_refresh_frame_context;
+
   YV12_BUFFER_CONFIG last_frame_uf;
 
   TOKENEXTRA *tok;
--- a/vp9/encoder/vp9_ratectrl.c
+++ b/vp9/encoder/vp9_ratectrl.c
@@ -184,8 +184,6 @@
 
   vp9_setup_past_independence(cm);
 
-  // interval before next GF
-  cpi->rc.frames_till_gf_update_due = cpi->rc.baseline_gf_interval;
   /* All buffers are implicitly updated on key frames. */
   cpi->refresh_golden_frame = 1;
   cpi->refresh_alt_ref_frame = 1;
@@ -855,7 +853,6 @@
   // Update the Golden frame usage counts.
   if (cpi->refresh_golden_frame) {
     // this frame refreshes means next frames don't unless specified by user
-    cpi->refresh_golden_frame = 0;
     cpi->rc.frames_since_golden = 0;
 
     if (!cpi->rc.source_alt_ref_pending)