shithub: libvpx

Download patch

ref: 483bcb310dea5a4fcb91af343408b14a9158ae29
parent: fc898231f126a38db8c789be16c5d9e8dc72c293
author: angiebird <angiebird@google.com>
date: Mon Mar 2 11:50:39 EST 2020

Add key frame group info to SimpleEncode

Change-Id: I2c5abbe23c84c6d794e06ed6429136b10fb18683

--- a/vp9/simple_encode.cc
+++ b/vp9/simple_encode.cc
@@ -701,7 +701,13 @@
   frame_rate_den_ = frame_rate_den;
   target_bitrate_ = target_bitrate;
   num_frames_ = num_frames;
+
   frame_coding_index_ = 0;
+  show_frame_count_ = 0;
+
+  key_frame_group_index_ = 0;
+  key_frame_group_size_ = 0;
+
   // TODO(angirbid): Should we keep a file pointer here or keep the file_path?
   assert(infile_path != nullptr);
   in_file_ = fopen(infile_path, "r");
@@ -822,9 +828,15 @@
   impl_ptr_->cpi = init_encoder(&oxcf, impl_ptr_->img_fmt);
   vpx_img_alloc(&impl_ptr_->tmp_img, impl_ptr_->img_fmt, frame_width_,
                 frame_height_, 1);
+
   frame_coding_index_ = 0;
+  show_frame_count_ = 0;
+
   encode_command_set_external_arf_indexes(&impl_ptr_->cpi->encode_command,
                                           GetVectorData(external_arf_indexes_));
+
+  UpdateKeyFrameGroup(show_frame_count_);
+
   UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_,
                        &group_of_picture_);
   rewind(in_file_);
@@ -845,13 +857,24 @@
   rewind(in_file_);
 }
 
-int SimpleEncode::GetKeyFrameGroupSize(int key_frame_index) const {
+void SimpleEncode::UpdateKeyFrameGroup(int key_frame_show_index) {
   const VP9_COMP *cpi = impl_ptr_->cpi;
-  return vp9_get_frames_to_next_key(&cpi->oxcf, &cpi->frame_info,
-                                    &cpi->twopass.first_pass_info,
-                                    key_frame_index, cpi->rc.min_gf_interval);
+  key_frame_group_index_ = 0;
+  key_frame_group_size_ = vp9_get_frames_to_next_key(
+      &cpi->oxcf, &cpi->frame_info, &cpi->twopass.first_pass_info,
+      key_frame_show_index, cpi->rc.min_gf_interval);
+  assert(key_frame_group_size_ > 0);
 }
 
+void SimpleEncode::PostUpdateKeyFrameGroupIndex(FrameType frame_type) {
+  if (frame_type != kFrameTypeAltRef) {
+    // key_frame_group_index_ only counts show frames
+    ++key_frame_group_index_;
+  }
+}
+
+int SimpleEncode::GetKeyFrameGroupSize() const { return key_frame_group_size_; }
+
 GroupOfPicture SimpleEncode::ObserveGroupOfPicture() const {
   return group_of_picture_;
 }
@@ -868,10 +891,19 @@
   PostUpdateRefFrameInfo(encode_frame_result.frame_type, frame_coding_index_,
                          &ref_frame_info_);
   ++frame_coding_index_;
+  if (encode_frame_result.frame_type != kFrameTypeAltRef) {
+    // Only kFrameTypeAltRef is not a show frame
+    ++show_frame_count_;
+  }
   IncreaseGroupOfPictureIndex(&group_of_picture_);
   if (IsGroupOfPictureFinished(group_of_picture_)) {
     UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_,
                          &group_of_picture_);
+  }
+
+  PostUpdateKeyFrameGroupIndex(encode_frame_result.frame_type);
+  if (key_frame_group_index_ == key_frame_group_size_) {
+    UpdateKeyFrameGroup(show_frame_count_);
   }
 }
 
--- a/vp9/simple_encode.h
+++ b/vp9/simple_encode.h
@@ -306,12 +306,11 @@
   // This function should be called after StartEncode() or EncodeFrame().
   void EndEncode();
 
-  // Given a key_frame_index, computes this key frame group's size.
   // The key frame group size includes one key frame plus the number of
   // following inter frames. Note that the key frame group size only counts the
   // show frames. The number of no show frames like alternate refereces are not
   // counted.
-  int GetKeyFrameGroupSize(int key_frame_index) const;
+  int GetKeyFrameGroupSize() const;
 
   // Provides the group of pictures that the next coding frame is in.
   // Only call this function between StartEncode() and EndEncode()
@@ -355,10 +354,28 @@
   std::vector<int> external_arf_indexes_;
   GroupOfPicture group_of_picture_;
 
+  // The key frame group size includes one key frame plus the number of
+  // following inter frames. Note that the key frame group size only counts the
+  // show frames. The number of no show frames like alternate refereces are not
+  // counted.
+  int key_frame_group_size_;
+
+  // The index for the to-be-coded show frame in the key frame group.
+  int key_frame_group_index_;
+
+  // Update key_frame_group_size_ and reset key_frame_group_index_.
+  void UpdateKeyFrameGroup(int key_frame_show_index);
+
+  // Update key_frame_group_index_.
+  void PostUpdateKeyFrameGroupIndex(FrameType frame_type);
+
   // Each show or no show frame is assigned with a coding index based on its
   // coding order (starting from zero) in the coding process of the entire
   // video. The coding index of the to-be-coded frame.
   int frame_coding_index_;
+
+  // Number of show frames we have coded so far.
+  int show_frame_count_;
 
   // TODO(angiebird): Do we need to reset ref_frames_info_ when the next key
   // frame appears?