Don't configure SVC params without per layer bitrate configured.

Change-Id: Ieb200ce1a710078e380047ed8af73db0c5e0c751
Bug: none
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/239442
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Commit-Queue: Philip Eliasson <philipel@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35457}
diff --git a/modules/video_coding/codecs/av1/libaom_av1_encoder.cc b/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
index 11aa3a9..967ec7d 100644
--- a/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
+++ b/modules/video_coding/codecs/av1/libaom_av1_encoder.cc
@@ -108,6 +108,7 @@
 
   std::unique_ptr<ScalableVideoController> svc_controller_;
   bool inited_;
+  bool rates_configured_;
   absl::optional<aom_svc_params_t> svc_params_;
   VideoCodec encoder_settings_;
   aom_image_t* frame_for_encode_;
@@ -143,6 +144,7 @@
 
 LibaomAv1Encoder::LibaomAv1Encoder()
     : inited_(false),
+      rates_configured_(false),
       frame_for_encode_(nullptr),
       encoded_image_callback_(nullptr) {}
 
@@ -283,15 +285,6 @@
                         << " on control AV1E_SET_AQ_MODE.";
     return WEBRTC_VIDEO_CODEC_ERROR;
   }
-  if (SvcEnabled()) {
-    ret = aom_codec_control(&ctx_, AV1E_SET_SVC_PARAMS, &*svc_params_);
-    if (ret != AOM_CODEC_OK) {
-      RTC_LOG(LS_WARNING) << "LibaomAV1Encoder::EncodeInit returned " << ret
-                          << " on control AV1E_SET_SVC_PARAMS.";
-      return false;
-    }
-  }
-
   ret = aom_codec_control(&ctx_, AOME_SET_MAX_INTRA_BITRATE_PCT, 300);
   if (ret != AOM_CODEC_OK) {
     RTC_LOG(LS_WARNING) << "LibaomAv1Encoder::EncodeInit returned " << ret
@@ -558,13 +551,14 @@
     }
     inited_ = false;
   }
+  rates_configured_ = false;
   return WEBRTC_VIDEO_CODEC_OK;
 }
 
 int32_t LibaomAv1Encoder::Encode(
     const VideoFrame& frame,
     const std::vector<VideoFrameType>* frame_types) {
-  if (!inited_ || encoded_image_callback_ == nullptr) {
+  if (!inited_ || encoded_image_callback_ == nullptr || !rates_configured_) {
     return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
   }
 
@@ -797,6 +791,8 @@
     aom_codec_control(&ctx_, AV1E_SET_SVC_PARAMS, &*svc_params_);
   }
 
+  rates_configured_ = true;
+
   // Set frame rate to closest integer value.
   encoder_settings_.maxFramerate =
       static_cast<uint32_t>(parameters.framerate_fps + 0.5);
diff --git a/modules/video_coding/codecs/av1/libaom_av1_unittest.cc b/modules/video_coding/codecs/av1/libaom_av1_unittest.cc
index e893bfd..f97808e 100644
--- a/modules/video_coding/codecs/av1/libaom_av1_unittest.cc
+++ b/modules/video_coding/codecs/av1/libaom_av1_unittest.cc
@@ -137,6 +137,11 @@
   ASSERT_EQ(encoder->InitEncode(&codec_settings, DefaultEncoderSettings()),
             WEBRTC_VIDEO_CODEC_OK);
 
+  VideoBitrateAllocation allocation;
+  allocation.SetBitrate(0, 0, 300000);
+  encoder->SetRates(VideoEncoder::RateControlParameters(
+      allocation, codec_settings.maxFramerate));
+
   std::vector<EncodedVideoFrameProducer::EncodedFrame> encoded_frames =
       EncodedVideoFrameProducer(*encoder).SetNumInputFrames(4).Encode();
   for (size_t frame_id = 0; frame_id < encoded_frames.size(); ++frame_id) {