Add unit test for ref_frame_info
authorangiebird <angiebird@google.com>
Tue, 3 Mar 2020 04:04:11 +0000 (20:04 -0800)
committerangiebird <angiebird@google.com>
Tue, 3 Mar 2020 18:57:03 +0000 (10:57 -0800)
Fix several bugs to make the test pass.
1) Move update_frame_indexes() out of show_frame check.
2) Init coding_indexes[i] to -1 when key frame appears
3) Fix a bug in PostUpdateRefFrameInfo()

Change-Id: Ie7c70a1d460e5b89475a1aef77416fc9a88387e1

test/simple_encode_test.cc
vp9/common/vp9_onyxc_int.h
vp9/encoder/vp9_encoder.c
vp9/simple_encode.cc
vp9/simple_encode.h

index edaf5bf..3dfc812 100644 (file)
@@ -92,6 +92,8 @@ TEST(SimpleEncode, EncodeFrame) {
       EXPECT_GE(encode_frame_result.psnr, 34)
           << "The psnr is supposed to be greater than 34 given the "
              "target_bitrate 1000 kbps";
+      EXPECT_EQ(encode_frame_result.ref_frame_info,
+                encode_frame_list[group_index].ref_frame_info);
       total_data_bit_size += encode_frame_result.coding_data_bit_size;
       ++frame_coding_index;
     }
index cc4267a..6f9c698 100644 (file)
@@ -283,6 +283,8 @@ static INLINE void init_frame_indexes(VP9_COMMON *cm) {
 
 static INLINE void update_frame_indexes(VP9_COMMON *cm, int show_frame) {
   if (show_frame) {
+    // Don't increment frame counters if this was an altref buffer
+    // update not a real frame
     ++cm->current_video_frame;
   }
 #if CONFIG_RATE_CTRL
index 44747db..a5eeeaa 100644 (file)
@@ -5394,11 +5394,9 @@ static void encode_frame_to_data_rate(
 
   if (cm->show_frame) {
     vp9_swap_mi_and_prev_mi(cm);
-    // Don't increment frame counters if this was an altref buffer
-    // update not a real frame
-    update_frame_indexes(cm, cm->show_frame);
     if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
   }
+  update_frame_indexes(cm, cm->show_frame);
 
   if (cpi->use_svc) {
     cpi->svc
@@ -7338,7 +7336,7 @@ static void update_encode_frame_result(
       encode_frame_result->ref_frame_coding_indexes[i] =
           ref_frame_bufs[i]->frame_coding_index;
       encode_frame_result->ref_frame_valid_list[i] =
-          !(ref_frame_flags & inter_ref_flags[i]);
+          (ref_frame_flags & inter_ref_flags[i]) != 0;
     }
   } else {
     // No reference frame is available when this is a key frame.
index 3efcd7a..6d9860f 100644 (file)
@@ -499,8 +499,8 @@ static void update_encode_frame_result(
       encode_frame_result->coding_data_byte_size * 8;
   encode_frame_result->show_idx = encode_frame_info->show_idx;
   encode_frame_result->coding_idx = encode_frame_info->frame_coding_index;
+  assert(kRefFrameTypeMax == MAX_INTER_REF_FRAMES);
   for (int i = 0; i < kRefFrameTypeMax; ++i) {
-    assert(kRefFrameTypeMax == MAX_INTER_REF_FRAMES);
     encode_frame_result->ref_frame_info.coding_indexes[i] =
         encode_frame_info->ref_frame_coding_indexes[i];
     encode_frame_result->ref_frame_info.valid_list[i] =
@@ -532,9 +532,18 @@ static int IsGroupOfPictureFinished(const GroupOfPicture &group_of_picture) {
          group_of_picture.encode_frame_list.size();
 }
 
+bool operator==(const RefFrameInfo &a, const RefFrameInfo &b) {
+  bool match = true;
+  for (int i = 0; i < kRefFrameTypeMax; ++i) {
+    match &= a.coding_indexes[i] == b.coding_indexes[i];
+    match &= a.valid_list[i] == b.valid_list[i];
+  }
+  return match;
+}
+
 static void InitRefFrameInfo(RefFrameInfo *ref_frame_info) {
   for (int i = 0; i < kRefFrameTypeMax; ++i) {
-    ref_frame_info->coding_indexes[i] = 0;
+    ref_frame_info->coding_indexes[i] = -1;
     ref_frame_info->valid_list[i] = 0;
   }
 }
@@ -587,7 +596,7 @@ static void PostUpdateRefFrameInfo(FrameType frame_type, int frame_coding_index,
   }
 
   if (past_index == last_index) {
-    ref_frame_valid_list[kRefFrameTypeLast] = 0;
+    ref_frame_valid_list[kRefFrameTypePast] = 0;
   }
 
   if (future_index == last_index) {
@@ -864,6 +873,8 @@ void SimpleEncode::UpdateKeyFrameGroup(int key_frame_show_index) {
       &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);
+  // Init the reference frame info when a new key frame group appears.
+  InitRefFrameInfo(&ref_frame_info_);
 }
 
 void SimpleEncode::PostUpdateKeyFrameGroupIndex(FrameType frame_type) {
@@ -895,16 +906,19 @@ void SimpleEncode::PostUpdateState(
     // 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_);
   }
+
+  IncreaseGroupOfPictureIndex(&group_of_picture_);
+  if (IsGroupOfPictureFinished(group_of_picture_)) {
+    // This function needs to be called after ref_frame_info_ is updated
+    // properly in PostUpdateRefFrameInfo() and UpdateKeyFrameGroup().
+    UpdateGroupOfPicture(impl_ptr_->cpi, frame_coding_index_, ref_frame_info_,
+                         &group_of_picture_);
+  }
 }
 
 void SimpleEncode::EncodeFrame(EncodeFrameResult *encode_frame_result) {
index 67007e5..452fc6a 100644 (file)
@@ -86,6 +86,8 @@ struct RefFrameInfo {
   int valid_list[kRefFrameTypeMax];
 };
 
+bool operator==(const RefFrameInfo &a, const RefFrameInfo &b);
+
 struct EncodeFrameInfo {
   int show_idx;
 
@@ -363,7 +365,8 @@ class SimpleEncode {
   // 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_.
+  // Update key_frame_group_size_, reset key_frame_group_index_ and init
+  // ref_frame_info_.
   void UpdateKeyFrameGroup(int key_frame_show_index);
 
   // Update key_frame_group_index_.