VP9 encoder: mark only base spatial layer as keyframe

Bug: webrtc:13576
Change-Id: Ia4173d3aa2c8e546a818eccc5d6c09d1150dcaf4
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/247188
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Commit-Queue: Erik Språng <sprang@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35736}
diff --git a/modules/video_coding/codecs/vp9/libvpx_vp9_encoder.cc b/modules/video_coding/codecs/vp9/libvpx_vp9_encoder.cc
index 75c2f16..0c3196c 100644
--- a/modules/video_coding/codecs/vp9/libvpx_vp9_encoder.cc
+++ b/modules/video_coding/codecs/vp9/libvpx_vp9_encoder.cc
@@ -1634,18 +1634,6 @@
   encoded_image_.SetEncodedData(EncodedImageBuffer::Create(
       static_cast<const uint8_t*>(pkt->data.frame.buf), pkt->data.frame.sz));
 
-  const bool is_key_frame =
-      (pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
-  // Ensure encoder issued key frame on request.
-  RTC_DCHECK(is_key_frame || !force_key_frame_);
-
-  // Check if encoded frame is a key frame.
-  encoded_image_._frameType = VideoFrameType::kVideoFrameDelta;
-  if (is_key_frame) {
-    encoded_image_._frameType = VideoFrameType::kVideoFrameKey;
-    force_key_frame_ = false;
-  }
-
   codec_specific_ = {};
   absl::optional<int> spatial_index;
   if (!PopulateCodecSpecific(&codec_specific_, &spatial_index, *pkt,
@@ -1656,6 +1644,20 @@
   }
   encoded_image_.SetSpatialIndex(spatial_index);
 
+  const bool is_key_frame =
+      ((pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? true : false) &&
+      !codec_specific_.codecSpecific.VP9.inter_layer_predicted;
+
+  // Ensure encoder issued key frame on request.
+  RTC_DCHECK(is_key_frame || !force_key_frame_);
+
+  // Check if encoded frame is a key frame.
+  encoded_image_._frameType = VideoFrameType::kVideoFrameDelta;
+  if (is_key_frame) {
+    encoded_image_._frameType = VideoFrameType::kVideoFrameKey;
+    force_key_frame_ = false;
+  }
+
   UpdateReferenceBuffers(*pkt, pics_since_key_);
 
   TRACE_COUNTER1("webrtc", "EncodedFrameSize", encoded_image_.size());