In RtpFrameReferenceFinder VP8 clean not yet received before filling it

To make it generally faster, specially in case of very large picture id gaps.

Bug: None
Change-Id: Ib0c49c17bd1281190da986def43bea8fc3440c0f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/168055
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Philip Eliasson <philipel@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30438}
diff --git a/modules/video_coding/rtp_frame_reference_finder.cc b/modules/video_coding/rtp_frame_reference_finder.cc
index 3767161..013b6e3 100644
--- a/modules/video_coding/rtp_frame_reference_finder.cc
+++ b/modules/video_coding/rtp_frame_reference_finder.cc
@@ -291,6 +291,16 @@
   if (last_picture_id_ == -1)
     last_picture_id_ = frame->id.picture_id;
 
+  // Clean up info about not yet received frames that are too old.
+  uint16_t old_picture_id =
+      Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames);
+  auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
+  not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
+                                 clean_frames_to);
+  // Avoid re-adding picture ids that were just erased.
+  if (AheadOf<uint16_t, kPicIdLength>(old_picture_id, last_picture_id_)) {
+    last_picture_id_ = old_picture_id;
+  }
   // Find if there has been a gap in fully received frames and save the picture
   // id of those frames in |not_yet_received_frames_|.
   if (AheadOf<uint16_t, kPicIdLength>(frame->id.picture_id, last_picture_id_)) {
@@ -307,13 +317,6 @@
   auto clean_layer_info_to = layer_info_.lower_bound(old_tl0_pic_idx);
   layer_info_.erase(layer_info_.begin(), clean_layer_info_to);
 
-  // Clean up info about not yet received frames that are too old.
-  uint16_t old_picture_id =
-      Subtract<kPicIdLength>(frame->id.picture_id, kMaxNotYetReceivedFrames);
-  auto clean_frames_to = not_yet_received_frames_.lower_bound(old_picture_id);
-  not_yet_received_frames_.erase(not_yet_received_frames_.begin(),
-                                 clean_frames_to);
-
   if (frame->frame_type() == VideoFrameType::kVideoFrameKey) {
     if (codec_header.temporalIdx != 0) {
       return kDrop;