Vp9 flexible mode fixes

- Enable vp9 flexible mode in VideoEngine if 3 spatial layers are set.
- Enable flexible mode in loopback tools and quality tests.
- Reset first active spatial layer on keyframe in encoder.
- Ensure duplicate references are not set by the sender in video header.
- Set references manually for flexible mode in vp9 encoder.
- Delay new activated layers until next base layer frame.
- On receive side put each spatial layer as a separate frame to FrameBuffer
  and return several frames combined from FrameBuffer.

Bug: webrtc:10049,webrtc:9794,webrtc:9784
Change-Id: I01e69f134cc145deba666ccc92deb1d37a324ede
Reviewed-on: https://webrtc-review.googlesource.com/c/112289
Commit-Queue: Ilya Nikolaevskiy <ilnik@webrtc.org>
Reviewed-by: Sergey Silkin <ssilkin@webrtc.org>
Reviewed-by: Philip Eliasson <philipel@webrtc.org>
Reviewed-by: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25895}
diff --git a/modules/video_coding/frame_buffer2.h b/modules/video_coding/frame_buffer2.h
index dc5e5a2..c311bc8 100644
--- a/modules/video_coding/frame_buffer2.h
+++ b/modules/video_coding/frame_buffer2.h
@@ -15,6 +15,7 @@
 #include <map>
 #include <memory>
 #include <utility>
+#include <vector>
 
 #include "api/video/encoded_frame.h"
 #include "modules/video_coding/include/video_coding_defines.h"
@@ -156,6 +157,13 @@
   bool HasBadRenderTiming(const EncodedFrame& frame, int64_t now_ms)
       RTC_EXCLUSIVE_LOCKS_REQUIRED(crit_);
 
+  // The cleaner solution would be to have the NextFrame function return a
+  // vector of frames, but until the decoding pipeline can support decoding
+  // multiple frames at the same time we combine all frames to one frame and
+  // return it. See bugs.webrtc.org/10064
+  EncodedFrame* CombineAndDeleteFrames(
+      const std::vector<EncodedFrame*>& frames) const;
+
   FrameMap frames_ RTC_GUARDED_BY(crit_);
 
   rtc::CriticalSection crit_;
@@ -167,7 +175,7 @@
   absl::optional<uint32_t> last_decoded_frame_timestamp_ RTC_GUARDED_BY(crit_);
   FrameMap::iterator last_decoded_frame_it_ RTC_GUARDED_BY(crit_);
   FrameMap::iterator last_continuous_frame_it_ RTC_GUARDED_BY(crit_);
-  FrameMap::iterator next_frame_it_ RTC_GUARDED_BY(crit_);
+  std::vector<FrameMap::iterator> frames_to_decode_ RTC_GUARDED_BY(crit_);
   int num_frames_history_ RTC_GUARDED_BY(crit_);
   int num_frames_buffered_ RTC_GUARDED_BY(crit_);
   bool stopped_ RTC_GUARDED_BY(crit_);