• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "api/test/create_frame_generator.h"
12 #include "api/test/frame_generator_interface.h"
13 #include "api/video/color_space.h"
14 #include "api/video/i420_buffer.h"
15 #include "api/video_codecs/video_encoder.h"
16 #include "common_video/libyuv/include/webrtc_libyuv.h"
17 #include "media/base/vp9_profile.h"
18 #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
19 #include "modules/video_coding/codecs/test/encoded_video_frame_producer.h"
20 #include "modules/video_coding/codecs/test/video_codec_unittest.h"
21 #include "modules/video_coding/codecs/vp9/include/vp9.h"
22 #include "modules/video_coding/codecs/vp9/svc_config.h"
23 #include "test/field_trial.h"
24 #include "test/gmock.h"
25 #include "test/gtest.h"
26 #include "test/video_codec_settings.h"
27 
28 namespace webrtc {
29 namespace {
30 
31 using ::testing::ElementsAreArray;
32 using ::testing::SizeIs;
33 using ::testing::UnorderedElementsAreArray;
34 using EncoderInfo = webrtc::VideoEncoder::EncoderInfo;
35 using FramerateFractions =
36     absl::InlinedVector<uint8_t, webrtc::kMaxTemporalStreams>;
37 
38 constexpr size_t kWidth = 1280;
39 constexpr size_t kHeight = 720;
40 
41 const VideoEncoder::Capabilities kCapabilities(false);
42 const VideoEncoder::Settings kSettings(kCapabilities,
43                                        /*number_of_cores=*/1,
44                                        /*max_payload_size=*/0);
45 
DefaultCodecSettings()46 VideoCodec DefaultCodecSettings() {
47   VideoCodec codec_settings;
48   webrtc::test::CodecSettings(kVideoCodecVP9, &codec_settings);
49   codec_settings.width = kWidth;
50   codec_settings.height = kHeight;
51   codec_settings.VP9()->numberOfTemporalLayers = 1;
52   codec_settings.VP9()->numberOfSpatialLayers = 1;
53   return codec_settings;
54 }
55 
56 }  // namespace
57 
58 class TestVp9Impl : public VideoCodecUnitTest {
59  protected:
CreateEncoder()60   std::unique_ptr<VideoEncoder> CreateEncoder() override {
61     return VP9Encoder::Create();
62   }
63 
CreateDecoder()64   std::unique_ptr<VideoDecoder> CreateDecoder() override {
65     return VP9Decoder::Create();
66   }
67 
ModifyCodecSettings(VideoCodec * codec_settings)68   void ModifyCodecSettings(VideoCodec* codec_settings) override {
69     webrtc::test::CodecSettings(kVideoCodecVP9, codec_settings);
70     codec_settings->width = kWidth;
71     codec_settings->height = kHeight;
72     codec_settings->VP9()->numberOfTemporalLayers = 1;
73     codec_settings->VP9()->numberOfSpatialLayers = 1;
74   }
75 
ConfigureSvc(size_t num_spatial_layers,size_t num_temporal_layers=1)76   void ConfigureSvc(size_t num_spatial_layers, size_t num_temporal_layers = 1) {
77     codec_settings_.VP9()->numberOfSpatialLayers =
78         static_cast<unsigned char>(num_spatial_layers);
79     codec_settings_.VP9()->numberOfTemporalLayers = num_temporal_layers;
80     codec_settings_.VP9()->frameDroppingOn = false;
81 
82     std::vector<SpatialLayer> layers =
83         GetSvcConfig(codec_settings_.width, codec_settings_.height,
84                      codec_settings_.maxFramerate, /*first_active_layer=*/0,
85                      num_spatial_layers, num_temporal_layers, false);
86     for (size_t i = 0; i < layers.size(); ++i) {
87       codec_settings_.spatialLayers[i] = layers[i];
88     }
89   }
90 };
91 
92 // Disabled on ios as flake, see https://crbug.com/webrtc/7057
93 #if defined(WEBRTC_IOS)
TEST_F(TestVp9Impl,DISABLED_EncodeDecode)94 TEST_F(TestVp9Impl, DISABLED_EncodeDecode) {
95 #else
96 TEST_F(TestVp9Impl, EncodeDecode) {
97 #endif
98   VideoFrame input_frame = NextInputFrame();
99   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(input_frame, nullptr));
100   EncodedImage encoded_frame;
101   CodecSpecificInfo codec_specific_info;
102   ASSERT_TRUE(WaitForEncodedFrame(&encoded_frame, &codec_specific_info));
103   // First frame should be a key frame.
104   encoded_frame._frameType = VideoFrameType::kVideoFrameKey;
105   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encoded_frame, false, 0));
106   std::unique_ptr<VideoFrame> decoded_frame;
107   absl::optional<uint8_t> decoded_qp;
108   ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp));
109   ASSERT_TRUE(decoded_frame);
110   EXPECT_GT(I420PSNR(&input_frame, decoded_frame.get()), 36);
111 
112   const ColorSpace color_space = *decoded_frame->color_space();
113   EXPECT_EQ(ColorSpace::PrimaryID::kUnspecified, color_space.primaries());
114   EXPECT_EQ(ColorSpace::TransferID::kUnspecified, color_space.transfer());
115   EXPECT_EQ(ColorSpace::MatrixID::kUnspecified, color_space.matrix());
116   EXPECT_EQ(ColorSpace::RangeID::kLimited, color_space.range());
117   EXPECT_EQ(ColorSpace::ChromaSiting::kUnspecified,
118             color_space.chroma_siting_horizontal());
119   EXPECT_EQ(ColorSpace::ChromaSiting::kUnspecified,
120             color_space.chroma_siting_vertical());
121 }
122 
123 TEST_F(TestVp9Impl, DecodedColorSpaceFromBitstream) {
124   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
125   EncodedImage encoded_frame;
126   CodecSpecificInfo codec_specific_info;
127   ASSERT_TRUE(WaitForEncodedFrame(&encoded_frame, &codec_specific_info));
128 
129   // Encoded frame without explicit color space information.
130   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encoded_frame, false, 0));
131   std::unique_ptr<VideoFrame> decoded_frame;
132   absl::optional<uint8_t> decoded_qp;
133   ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp));
134   ASSERT_TRUE(decoded_frame);
135   // Color space present from encoded bitstream.
136   ASSERT_TRUE(decoded_frame->color_space());
137   // No HDR metadata present.
138   EXPECT_FALSE(decoded_frame->color_space()->hdr_metadata());
139 }
140 
141 TEST_F(TestVp9Impl, DecodedQpEqualsEncodedQp) {
142   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
143   EncodedImage encoded_frame;
144   CodecSpecificInfo codec_specific_info;
145   ASSERT_TRUE(WaitForEncodedFrame(&encoded_frame, &codec_specific_info));
146   // First frame should be a key frame.
147   encoded_frame._frameType = VideoFrameType::kVideoFrameKey;
148   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encoded_frame, false, 0));
149   std::unique_ptr<VideoFrame> decoded_frame;
150   absl::optional<uint8_t> decoded_qp;
151   ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp));
152   ASSERT_TRUE(decoded_frame);
153   ASSERT_TRUE(decoded_qp);
154   EXPECT_EQ(encoded_frame.qp_, *decoded_qp);
155 }
156 
157 TEST(Vp9ImplTest, ParserQpEqualsEncodedQp) {
158   std::unique_ptr<VideoEncoder> encoder = VP9Encoder::Create();
159   VideoCodec codec_settings = DefaultCodecSettings();
160   encoder->InitEncode(&codec_settings, kSettings);
161 
162   std::vector<EncodedVideoFrameProducer::EncodedFrame> frames =
163       EncodedVideoFrameProducer(*encoder)
164           .SetNumInputFrames(1)
165           .SetResolution({kWidth, kHeight})
166           .Encode();
167   ASSERT_THAT(frames, SizeIs(1));
168   const auto& encoded_frame = frames.front().encoded_image;
169   int qp = 0;
170   ASSERT_TRUE(vp9::GetQp(encoded_frame.data(), encoded_frame.size(), &qp));
171   EXPECT_EQ(encoded_frame.qp_, qp);
172 }
173 
174 TEST(Vp9ImplTest, EncoderWith2TemporalLayers) {
175   std::unique_ptr<VideoEncoder> encoder = VP9Encoder::Create();
176   VideoCodec codec_settings = DefaultCodecSettings();
177   codec_settings.VP9()->numberOfTemporalLayers = 2;
178   // Tl0PidIdx is only used in non-flexible mode.
179   codec_settings.VP9()->flexibleMode = false;
180   EXPECT_EQ(encoder->InitEncode(&codec_settings, kSettings),
181             WEBRTC_VIDEO_CODEC_OK);
182 
183   std::vector<EncodedVideoFrameProducer::EncodedFrame> frames =
184       EncodedVideoFrameProducer(*encoder)
185           .SetNumInputFrames(4)
186           .SetResolution({kWidth, kHeight})
187           .Encode();
188 
189   ASSERT_THAT(frames, SizeIs(4));
190   EXPECT_EQ(frames[0].codec_specific_info.codecSpecific.VP9.temporal_idx, 0);
191   EXPECT_EQ(frames[1].codec_specific_info.codecSpecific.VP9.temporal_idx, 1);
192   EXPECT_EQ(frames[2].codec_specific_info.codecSpecific.VP9.temporal_idx, 0);
193   EXPECT_EQ(frames[3].codec_specific_info.codecSpecific.VP9.temporal_idx, 1);
194 }
195 
196 TEST(Vp9ImplTest, EncoderWith2SpatialLayers) {
197   std::unique_ptr<VideoEncoder> encoder = VP9Encoder::Create();
198   VideoCodec codec_settings = DefaultCodecSettings();
199   codec_settings.VP9()->numberOfSpatialLayers = 2;
200   EXPECT_EQ(encoder->InitEncode(&codec_settings, kSettings),
201             WEBRTC_VIDEO_CODEC_OK);
202 
203   std::vector<EncodedVideoFrameProducer::EncodedFrame> frames =
204       EncodedVideoFrameProducer(*encoder)
205           .SetNumInputFrames(1)
206           .SetResolution({kWidth, kHeight})
207           .Encode();
208 
209   ASSERT_THAT(frames, SizeIs(2));
210   EXPECT_EQ(frames[0].encoded_image.SpatialIndex(), 0);
211   EXPECT_EQ(frames[1].encoded_image.SpatialIndex(), 1);
212 }
213 
214 TEST_F(TestVp9Impl, EncoderExplicitLayering) {
215   // Override default settings.
216   codec_settings_.VP9()->numberOfTemporalLayers = 1;
217   codec_settings_.VP9()->numberOfSpatialLayers = 2;
218 
219   codec_settings_.width = 960;
220   codec_settings_.height = 540;
221   codec_settings_.spatialLayers[0].minBitrate = 200;
222   codec_settings_.spatialLayers[0].maxBitrate = 500;
223   codec_settings_.spatialLayers[0].targetBitrate =
224       (codec_settings_.spatialLayers[0].minBitrate +
225        codec_settings_.spatialLayers[0].maxBitrate) /
226       2;
227   codec_settings_.spatialLayers[0].active = true;
228 
229   codec_settings_.spatialLayers[1].minBitrate = 400;
230   codec_settings_.spatialLayers[1].maxBitrate = 1500;
231   codec_settings_.spatialLayers[1].targetBitrate =
232       (codec_settings_.spatialLayers[1].minBitrate +
233        codec_settings_.spatialLayers[1].maxBitrate) /
234       2;
235   codec_settings_.spatialLayers[1].active = true;
236 
237   codec_settings_.spatialLayers[0].width = codec_settings_.width / 2;
238   codec_settings_.spatialLayers[0].height = codec_settings_.height / 2;
239   codec_settings_.spatialLayers[0].maxFramerate = codec_settings_.maxFramerate;
240   codec_settings_.spatialLayers[1].width = codec_settings_.width;
241   codec_settings_.spatialLayers[1].height = codec_settings_.height;
242   codec_settings_.spatialLayers[1].maxFramerate = codec_settings_.maxFramerate;
243 
244   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
245             encoder_->InitEncode(&codec_settings_, kSettings));
246 
247   // Ensure it fails if scaling factors in horz/vert dimentions are different.
248   codec_settings_.spatialLayers[0].width = codec_settings_.width;
249   codec_settings_.spatialLayers[0].height = codec_settings_.height / 2;
250   codec_settings_.spatialLayers[1].width = codec_settings_.width;
251   codec_settings_.spatialLayers[1].height = codec_settings_.height;
252   EXPECT_EQ(WEBRTC_VIDEO_CODEC_ERR_PARAMETER,
253             encoder_->InitEncode(&codec_settings_, kSettings));
254 
255   // Ensure it fails if scaling factor is not power of two.
256   codec_settings_.spatialLayers[0].width = codec_settings_.width / 3;
257   codec_settings_.spatialLayers[0].height = codec_settings_.height / 3;
258   codec_settings_.spatialLayers[1].width = codec_settings_.width;
259   codec_settings_.spatialLayers[1].height = codec_settings_.height;
260   EXPECT_EQ(WEBRTC_VIDEO_CODEC_ERR_PARAMETER,
261             encoder_->InitEncode(&codec_settings_, kSettings));
262 }
263 
264 TEST_F(TestVp9Impl, EnableDisableSpatialLayers) {
265   // Configure encoder to produce N spatial layers. Encode frames of layer 0
266   // then enable layer 1 and encode more frames and so on until layer N-1.
267   // Then disable layers one by one in the same way.
268   // Note: bit rate allocation is high to avoid frame dropping due to rate
269   // control, the encoder should always produce a frame. A dropped
270   // frame indicates a problem and the test will fail.
271   const size_t num_spatial_layers = 3;
272   const size_t num_frames_to_encode = 5;
273 
274   ConfigureSvc(num_spatial_layers);
275   codec_settings_.VP9()->frameDroppingOn = true;
276 
277   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
278             encoder_->InitEncode(&codec_settings_, kSettings));
279 
280   VideoBitrateAllocation bitrate_allocation;
281   for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
282     // Allocate high bit rate to avoid frame dropping due to rate control.
283     bitrate_allocation.SetBitrate(
284         sl_idx, 0,
285         codec_settings_.spatialLayers[sl_idx].targetBitrate * 1000 * 2);
286     encoder_->SetRates(VideoEncoder::RateControlParameters(
287         bitrate_allocation, codec_settings_.maxFramerate));
288 
289     for (size_t frame_num = 0; frame_num < num_frames_to_encode; ++frame_num) {
290       SetWaitForEncodedFramesThreshold(sl_idx + 1);
291       EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
292                 encoder_->Encode(NextInputFrame(), nullptr));
293       std::vector<EncodedImage> encoded_frame;
294       std::vector<CodecSpecificInfo> codec_specific_info;
295       ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
296       EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.ss_data_available,
297                 frame_num == 0);
298     }
299   }
300 
301   for (size_t i = 0; i < num_spatial_layers - 1; ++i) {
302     const size_t sl_idx = num_spatial_layers - i - 1;
303     bitrate_allocation.SetBitrate(sl_idx, 0, 0);
304     encoder_->SetRates(VideoEncoder::RateControlParameters(
305         bitrate_allocation, codec_settings_.maxFramerate));
306 
307     for (size_t frame_num = 0; frame_num < num_frames_to_encode; ++frame_num) {
308       SetWaitForEncodedFramesThreshold(sl_idx);
309       EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
310                 encoder_->Encode(NextInputFrame(), nullptr));
311       std::vector<EncodedImage> encoded_frame;
312       std::vector<CodecSpecificInfo> codec_specific_info;
313       ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
314       EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.ss_data_available,
315                 frame_num == 0);
316     }
317   }
318 }
319 
320 TEST_F(TestVp9Impl, DisableEnableBaseLayerTriggersKeyFrame) {
321   // Configure encoder to produce N spatial layers. Encode frames for all
322   // layers. Then disable all but the last layer. Then reenable all back again.
323   test::ScopedFieldTrials override_field_trials(
324       "WebRTC-Vp9ExternalRefCtrl/Enabled/");
325   const size_t num_spatial_layers = 3;
326   const size_t num_temporal_layers = 3;
327   // Must not be multiple of temporal period to exercise all code paths.
328   const size_t num_frames_to_encode = 5;
329 
330   ConfigureSvc(num_spatial_layers, num_temporal_layers);
331   codec_settings_.VP9()->frameDroppingOn = false;
332   codec_settings_.VP9()->flexibleMode = false;
333   codec_settings_.VP9()->interLayerPred = InterLayerPredMode::kOnKeyPic;
334   codec_settings_.mode = VideoCodecMode::kRealtimeVideo;
335 
336   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
337             encoder_->InitEncode(&codec_settings_, kSettings));
338 
339   VideoBitrateAllocation bitrate_allocation;
340   for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
341     for (size_t tl_idx = 0; tl_idx < num_temporal_layers; ++tl_idx) {
342       // Allocate high bit rate to avoid frame dropping due to rate control.
343       bitrate_allocation.SetBitrate(
344           sl_idx, tl_idx,
345           codec_settings_.spatialLayers[sl_idx].targetBitrate * 1000 * 2);
346     }
347   }
348   encoder_->SetRates(VideoEncoder::RateControlParameters(
349       bitrate_allocation, codec_settings_.maxFramerate));
350 
351   for (size_t frame_num = 0; frame_num < num_frames_to_encode; ++frame_num) {
352     SetWaitForEncodedFramesThreshold(num_spatial_layers);
353     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
354               encoder_->Encode(NextInputFrame(), nullptr));
355     std::vector<EncodedImage> encoded_frame;
356     std::vector<CodecSpecificInfo> codec_specific_info;
357     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
358     EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.ss_data_available,
359               frame_num == 0);
360   }
361 
362   // Disable all but top layer.
363   for (size_t sl_idx = 0; sl_idx < num_spatial_layers - 1; ++sl_idx) {
364     for (size_t tl_idx = 0; tl_idx < num_temporal_layers; ++tl_idx) {
365       bitrate_allocation.SetBitrate(sl_idx, tl_idx, 0);
366     }
367   }
368   encoder_->SetRates(VideoEncoder::RateControlParameters(
369       bitrate_allocation, codec_settings_.maxFramerate));
370 
371   bool seen_ss_data = false;
372   for (size_t frame_num = 0; frame_num < num_frames_to_encode; ++frame_num) {
373     SetWaitForEncodedFramesThreshold(1);
374     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
375               encoder_->Encode(NextInputFrame(), nullptr));
376     std::vector<EncodedImage> encoded_frame;
377     std::vector<CodecSpecificInfo> codec_specific_info;
378     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
379     // SS available immediatly after switching on base temporal layer.
380     if (seen_ss_data) {
381       EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.ss_data_available,
382                 false);
383     } else {
384       EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.ss_data_available,
385                 codec_specific_info[0].codecSpecific.VP9.temporal_idx == 0);
386       seen_ss_data |=
387           codec_specific_info[0].codecSpecific.VP9.ss_data_available;
388     }
389     // No key-frames generated for disabling layers.
390     EXPECT_EQ(encoded_frame[0]._frameType, VideoFrameType::kVideoFrameDelta);
391     EXPECT_EQ(encoded_frame[0].SpatialIndex().value_or(-1), 2);
392   }
393   EXPECT_TRUE(seen_ss_data);
394 
395   // Force key-frame.
396   std::vector<VideoFrameType> frame_types = {VideoFrameType::kVideoFrameKey};
397   SetWaitForEncodedFramesThreshold(1);
398   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
399             encoder_->Encode(NextInputFrame(), &frame_types));
400   std::vector<EncodedImage> encoded_frame;
401   std::vector<CodecSpecificInfo> codec_specific_info;
402   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
403   // Key-frame should be produced.
404   EXPECT_EQ(encoded_frame[0]._frameType, VideoFrameType::kVideoFrameKey);
405   EXPECT_EQ(encoded_frame[0].SpatialIndex().value_or(-1), 2);
406 
407   // Encode some more frames.
408   for (size_t frame_num = 0; frame_num < num_frames_to_encode; ++frame_num) {
409     SetWaitForEncodedFramesThreshold(1);
410     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
411               encoder_->Encode(NextInputFrame(), nullptr));
412     std::vector<EncodedImage> encoded_frame;
413     std::vector<CodecSpecificInfo> codec_specific_info;
414     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
415     EXPECT_EQ(encoded_frame[0]._frameType, VideoFrameType::kVideoFrameDelta);
416     EXPECT_EQ(encoded_frame[0].SpatialIndex().value_or(-1), 2);
417   }
418 
419   // Enable the second layer back.
420   // Allocate high bit rate to avoid frame dropping due to rate control.
421   for (size_t tl_idx = 0; tl_idx < num_temporal_layers; ++tl_idx) {
422     bitrate_allocation.SetBitrate(
423         1, tl_idx, codec_settings_.spatialLayers[0].targetBitrate * 1000 * 2);
424   }
425   encoder_->SetRates(VideoEncoder::RateControlParameters(
426       bitrate_allocation, codec_settings_.maxFramerate));
427 
428   for (size_t frame_num = 0; frame_num < num_frames_to_encode; ++frame_num) {
429     SetWaitForEncodedFramesThreshold(2);
430     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
431               encoder_->Encode(NextInputFrame(), nullptr));
432     std::vector<EncodedImage> encoded_frame;
433     std::vector<CodecSpecificInfo> codec_specific_info;
434     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
435     ASSERT_EQ(encoded_frame.size(), 2u);
436     // SS available immediatly after switching on.
437     EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.ss_data_available,
438               frame_num == 0);
439     // Keyframe should be generated when enabling lower layers.
440     const VideoFrameType expected_type = frame_num == 0
441                                              ? VideoFrameType::kVideoFrameKey
442                                              : VideoFrameType::kVideoFrameDelta;
443     EXPECT_EQ(encoded_frame[0]._frameType, expected_type);
444     EXPECT_EQ(encoded_frame[0].SpatialIndex().value_or(-1), 1);
445     EXPECT_EQ(encoded_frame[1].SpatialIndex().value_or(-1), 2);
446   }
447 
448   // Enable the first layer back.
449   // Allocate high bit rate to avoid frame dropping due to rate control.
450   for (size_t tl_idx = 0; tl_idx < num_temporal_layers; ++tl_idx) {
451     bitrate_allocation.SetBitrate(
452         0, tl_idx, codec_settings_.spatialLayers[1].targetBitrate * 1000 * 2);
453   }
454   encoder_->SetRates(VideoEncoder::RateControlParameters(
455       bitrate_allocation, codec_settings_.maxFramerate));
456 
457   for (size_t frame_num = 0; frame_num < num_frames_to_encode; ++frame_num) {
458     SetWaitForEncodedFramesThreshold(num_spatial_layers);
459     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
460               encoder_->Encode(NextInputFrame(), nullptr));
461     std::vector<EncodedImage> encoded_frame;
462     std::vector<CodecSpecificInfo> codec_specific_info;
463     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
464     ASSERT_EQ(encoded_frame.size(), 3u);
465     // SS available immediatly after switching on.
466     EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.ss_data_available,
467               frame_num == 0);
468     // Keyframe should be generated when enabling lower layers.
469     const VideoFrameType expected_type = frame_num == 0
470                                              ? VideoFrameType::kVideoFrameKey
471                                              : VideoFrameType::kVideoFrameDelta;
472     EXPECT_EQ(encoded_frame[0]._frameType, expected_type);
473   }
474 }
475 
476 TEST_F(TestVp9Impl, DisableEnableBaseLayerTriggersKeyFrameForScreenshare) {
477   // Configure encoder to produce N spatial layers. Encode frames for all
478   // layers. Then disable all but the last layer. Then reenable all back again.
479   const size_t num_spatial_layers = 3;
480   const size_t num_frames_to_encode = 5;
481 
482   ConfigureSvc(num_spatial_layers);
483   codec_settings_.VP9()->frameDroppingOn = false;
484   codec_settings_.mode = VideoCodecMode::kScreensharing;
485   codec_settings_.VP9()->interLayerPred = InterLayerPredMode::kOn;
486   codec_settings_.VP9()->flexibleMode = true;
487 
488   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
489             encoder_->InitEncode(&codec_settings_, kSettings));
490 
491   VideoBitrateAllocation bitrate_allocation;
492   for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
493     // Allocate high bit rate to avoid frame dropping due to rate control.
494     bitrate_allocation.SetBitrate(
495         sl_idx, 0,
496         codec_settings_.spatialLayers[sl_idx].targetBitrate * 1000 * 2);
497   }
498   encoder_->SetRates(VideoEncoder::RateControlParameters(
499       bitrate_allocation, codec_settings_.maxFramerate));
500 
501   for (size_t frame_num = 0; frame_num < num_frames_to_encode; ++frame_num) {
502     SetWaitForEncodedFramesThreshold(num_spatial_layers);
503     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
504               encoder_->Encode(NextInputFrame(), nullptr));
505     std::vector<EncodedImage> encoded_frame;
506     std::vector<CodecSpecificInfo> codec_specific_info;
507     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
508     EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.ss_data_available,
509               frame_num == 0);
510   }
511 
512   // Disable all but top layer.
513   for (size_t sl_idx = 0; sl_idx < num_spatial_layers - 1; ++sl_idx) {
514     bitrate_allocation.SetBitrate(sl_idx, 0, 0);
515   }
516   encoder_->SetRates(VideoEncoder::RateControlParameters(
517       bitrate_allocation, codec_settings_.maxFramerate));
518 
519   for (size_t frame_num = 0; frame_num < num_frames_to_encode; ++frame_num) {
520     SetWaitForEncodedFramesThreshold(1);
521     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
522               encoder_->Encode(NextInputFrame(), nullptr));
523     std::vector<EncodedImage> encoded_frame;
524     std::vector<CodecSpecificInfo> codec_specific_info;
525     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
526     // SS available immediatly after switching off.
527     EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.ss_data_available,
528               frame_num == 0);
529     // No key-frames generated for disabling layers.
530     EXPECT_EQ(encoded_frame[0]._frameType, VideoFrameType::kVideoFrameDelta);
531     EXPECT_EQ(encoded_frame[0].SpatialIndex().value_or(-1), 2);
532   }
533 
534   // Force key-frame.
535   std::vector<VideoFrameType> frame_types = {VideoFrameType::kVideoFrameKey};
536   SetWaitForEncodedFramesThreshold(1);
537   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
538             encoder_->Encode(NextInputFrame(), &frame_types));
539   std::vector<EncodedImage> encoded_frame;
540   std::vector<CodecSpecificInfo> codec_specific_info;
541   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
542   // Key-frame should be produced.
543   EXPECT_EQ(encoded_frame[0]._frameType, VideoFrameType::kVideoFrameKey);
544 
545   // Enable the second layer back.
546   // Allocate high bit rate to avoid frame dropping due to rate control.
547   bitrate_allocation.SetBitrate(
548       1, 0, codec_settings_.spatialLayers[0].targetBitrate * 1000 * 2);
549   encoder_->SetRates(VideoEncoder::RateControlParameters(
550       bitrate_allocation, codec_settings_.maxFramerate));
551 
552   for (size_t frame_num = 0; frame_num < num_frames_to_encode; ++frame_num) {
553     SetWaitForEncodedFramesThreshold(2);
554     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
555               encoder_->Encode(NextInputFrame(), nullptr));
556     std::vector<EncodedImage> encoded_frame;
557     std::vector<CodecSpecificInfo> codec_specific_info;
558     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
559     ASSERT_EQ(encoded_frame.size(), 2u);
560     // SS available immediatly after switching on.
561     EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.ss_data_available,
562               frame_num == 0);
563     // Keyframe should be generated when enabling lower layers.
564     const VideoFrameType expected_type = frame_num == 0
565                                              ? VideoFrameType::kVideoFrameKey
566                                              : VideoFrameType::kVideoFrameDelta;
567     EXPECT_EQ(encoded_frame[0]._frameType, expected_type);
568     EXPECT_EQ(encoded_frame[0].SpatialIndex().value_or(-1), 1);
569     EXPECT_EQ(encoded_frame[1].SpatialIndex().value_or(-1), 2);
570   }
571 
572   // Enable the first layer back.
573   // Allocate high bit rate to avoid frame dropping due to rate control.
574   bitrate_allocation.SetBitrate(
575       0, 0, codec_settings_.spatialLayers[1].targetBitrate * 1000 * 2);
576   encoder_->SetRates(VideoEncoder::RateControlParameters(
577       bitrate_allocation, codec_settings_.maxFramerate));
578 
579   for (size_t frame_num = 0; frame_num < num_frames_to_encode; ++frame_num) {
580     SetWaitForEncodedFramesThreshold(num_spatial_layers);
581     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
582               encoder_->Encode(NextInputFrame(), nullptr));
583     std::vector<EncodedImage> encoded_frame;
584     std::vector<CodecSpecificInfo> codec_specific_info;
585     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
586     ASSERT_EQ(encoded_frame.size(), 3u);
587     // SS available immediatly after switching on.
588     EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.ss_data_available,
589               frame_num == 0);
590     // Keyframe should be generated when enabling lower layers.
591     const VideoFrameType expected_type = frame_num == 0
592                                              ? VideoFrameType::kVideoFrameKey
593                                              : VideoFrameType::kVideoFrameDelta;
594     EXPECT_EQ(encoded_frame[0]._frameType, expected_type);
595   }
596 }
597 
598 TEST_F(TestVp9Impl, EndOfPicture) {
599   const size_t num_spatial_layers = 2;
600   ConfigureSvc(num_spatial_layers);
601 
602   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
603             encoder_->InitEncode(&codec_settings_, kSettings));
604 
605   // Encode both base and upper layers. Check that end-of-superframe flag is
606   // set on upper layer frame but not on base layer frame.
607   VideoBitrateAllocation bitrate_allocation;
608   bitrate_allocation.SetBitrate(
609       0, 0, codec_settings_.spatialLayers[0].targetBitrate * 1000);
610   bitrate_allocation.SetBitrate(
611       1, 0, codec_settings_.spatialLayers[1].targetBitrate * 1000);
612   encoder_->SetRates(VideoEncoder::RateControlParameters(
613       bitrate_allocation, codec_settings_.maxFramerate));
614   SetWaitForEncodedFramesThreshold(2);
615   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
616 
617   std::vector<EncodedImage> frames;
618   std::vector<CodecSpecificInfo> codec_specific;
619   ASSERT_TRUE(WaitForEncodedFrames(&frames, &codec_specific));
620   EXPECT_FALSE(codec_specific[0].codecSpecific.VP9.end_of_picture);
621   EXPECT_TRUE(codec_specific[1].codecSpecific.VP9.end_of_picture);
622 
623   // Encode only base layer. Check that end-of-superframe flag is
624   // set on base layer frame.
625   bitrate_allocation.SetBitrate(1, 0, 0);
626   encoder_->SetRates(VideoEncoder::RateControlParameters(
627       bitrate_allocation, codec_settings_.maxFramerate));
628   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
629             encoder_->InitEncode(&codec_settings_, kSettings));
630 
631   SetWaitForEncodedFramesThreshold(1);
632   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
633 
634   ASSERT_TRUE(WaitForEncodedFrames(&frames, &codec_specific));
635   EXPECT_FALSE(frames[0].SpatialIndex());
636   EXPECT_TRUE(codec_specific[0].codecSpecific.VP9.end_of_picture);
637 }
638 
639 TEST_F(TestVp9Impl, InterLayerPred) {
640   const size_t num_spatial_layers = 2;
641   ConfigureSvc(num_spatial_layers);
642   codec_settings_.VP9()->frameDroppingOn = false;
643 
644   VideoBitrateAllocation bitrate_allocation;
645   for (size_t i = 0; i < num_spatial_layers; ++i) {
646     bitrate_allocation.SetBitrate(
647         i, 0, codec_settings_.spatialLayers[i].targetBitrate * 1000);
648   }
649 
650   const std::vector<InterLayerPredMode> inter_layer_pred_modes = {
651       InterLayerPredMode::kOff, InterLayerPredMode::kOn,
652       InterLayerPredMode::kOnKeyPic};
653 
654   for (const InterLayerPredMode inter_layer_pred : inter_layer_pred_modes) {
655     codec_settings_.VP9()->interLayerPred = inter_layer_pred;
656     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
657               encoder_->InitEncode(&codec_settings_, kSettings));
658 
659     encoder_->SetRates(VideoEncoder::RateControlParameters(
660         bitrate_allocation, codec_settings_.maxFramerate));
661 
662     SetWaitForEncodedFramesThreshold(2);
663     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
664               encoder_->Encode(NextInputFrame(), nullptr));
665 
666     std::vector<EncodedImage> frames;
667     std::vector<CodecSpecificInfo> codec_specific;
668     ASSERT_TRUE(WaitForEncodedFrames(&frames, &codec_specific));
669 
670     // Key frame.
671     ASSERT_EQ(frames[0].SpatialIndex(), 0);
672     ASSERT_FALSE(codec_specific[0].codecSpecific.VP9.inter_pic_predicted);
673     EXPECT_FALSE(codec_specific[0].codecSpecific.VP9.inter_layer_predicted);
674     EXPECT_EQ(codec_specific[0].codecSpecific.VP9.non_ref_for_inter_layer_pred,
675               inter_layer_pred == InterLayerPredMode::kOff);
676     EXPECT_TRUE(codec_specific[0].codecSpecific.VP9.ss_data_available);
677 
678     ASSERT_EQ(frames[1].SpatialIndex(), 1);
679     ASSERT_FALSE(codec_specific[1].codecSpecific.VP9.inter_pic_predicted);
680     EXPECT_EQ(codec_specific[1].codecSpecific.VP9.inter_layer_predicted,
681               inter_layer_pred == InterLayerPredMode::kOn ||
682                   inter_layer_pred == InterLayerPredMode::kOnKeyPic);
683     EXPECT_EQ(codec_specific[1].codecSpecific.VP9.ss_data_available,
684               inter_layer_pred == InterLayerPredMode::kOff);
685     EXPECT_TRUE(
686         codec_specific[1].codecSpecific.VP9.non_ref_for_inter_layer_pred);
687 
688     // Delta frame.
689     SetWaitForEncodedFramesThreshold(2);
690     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
691               encoder_->Encode(NextInputFrame(), nullptr));
692     ASSERT_TRUE(WaitForEncodedFrames(&frames, &codec_specific));
693 
694     ASSERT_EQ(frames[0].SpatialIndex(), 0);
695     ASSERT_TRUE(codec_specific[0].codecSpecific.VP9.inter_pic_predicted);
696     EXPECT_FALSE(codec_specific[0].codecSpecific.VP9.inter_layer_predicted);
697     EXPECT_EQ(codec_specific[0].codecSpecific.VP9.non_ref_for_inter_layer_pred,
698               inter_layer_pred != InterLayerPredMode::kOn);
699     EXPECT_FALSE(codec_specific[0].codecSpecific.VP9.ss_data_available);
700 
701     ASSERT_EQ(frames[1].SpatialIndex(), 1);
702     ASSERT_TRUE(codec_specific[1].codecSpecific.VP9.inter_pic_predicted);
703     EXPECT_EQ(codec_specific[1].codecSpecific.VP9.inter_layer_predicted,
704               inter_layer_pred == InterLayerPredMode::kOn);
705     EXPECT_TRUE(
706         codec_specific[1].codecSpecific.VP9.non_ref_for_inter_layer_pred);
707     EXPECT_FALSE(codec_specific[1].codecSpecific.VP9.ss_data_available);
708   }
709 }
710 
711 TEST_F(TestVp9Impl,
712        EnablingUpperLayerTriggersKeyFrameIfInterLayerPredIsDisabled) {
713   const size_t num_spatial_layers = 3;
714   const size_t num_frames_to_encode = 2;
715 
716   ConfigureSvc(num_spatial_layers);
717   codec_settings_.VP9()->frameDroppingOn = false;
718 
719   const std::vector<InterLayerPredMode> inter_layer_pred_modes = {
720       InterLayerPredMode::kOff, InterLayerPredMode::kOn,
721       InterLayerPredMode::kOnKeyPic};
722 
723   for (const InterLayerPredMode inter_layer_pred : inter_layer_pred_modes) {
724     codec_settings_.VP9()->interLayerPred = inter_layer_pred;
725     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
726               encoder_->InitEncode(&codec_settings_, kSettings));
727 
728     VideoBitrateAllocation bitrate_allocation;
729     for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
730       bitrate_allocation.SetBitrate(
731           sl_idx, 0,
732           codec_settings_.spatialLayers[sl_idx].targetBitrate * 1000);
733       encoder_->SetRates(VideoEncoder::RateControlParameters(
734           bitrate_allocation, codec_settings_.maxFramerate));
735 
736       for (size_t frame_num = 0; frame_num < num_frames_to_encode;
737            ++frame_num) {
738         SetWaitForEncodedFramesThreshold(sl_idx + 1);
739         EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
740                   encoder_->Encode(NextInputFrame(), nullptr));
741         std::vector<EncodedImage> encoded_frame;
742         std::vector<CodecSpecificInfo> codec_specific_info;
743         ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
744 
745         const bool is_first_upper_layer_frame = (sl_idx > 0 && frame_num == 0);
746         if (is_first_upper_layer_frame) {
747           if (inter_layer_pred == InterLayerPredMode::kOn) {
748             EXPECT_EQ(encoded_frame[0]._frameType,
749                       VideoFrameType::kVideoFrameDelta);
750           } else {
751             EXPECT_EQ(encoded_frame[0]._frameType,
752                       VideoFrameType::kVideoFrameKey);
753           }
754         } else if (sl_idx == 0 && frame_num == 0) {
755           EXPECT_EQ(encoded_frame[0]._frameType,
756                     VideoFrameType::kVideoFrameKey);
757         } else {
758           for (size_t i = 0; i <= sl_idx; ++i) {
759             EXPECT_EQ(encoded_frame[i]._frameType,
760                       VideoFrameType::kVideoFrameDelta);
761           }
762         }
763       }
764     }
765   }
766 }
767 
768 TEST_F(TestVp9Impl,
769        EnablingUpperLayerUnsetsInterPicPredictedInInterlayerPredModeOn) {
770   const size_t num_spatial_layers = 3;
771   const size_t num_frames_to_encode = 2;
772 
773   ConfigureSvc(num_spatial_layers);
774   codec_settings_.VP9()->frameDroppingOn = false;
775   codec_settings_.VP9()->flexibleMode = false;
776 
777   const std::vector<InterLayerPredMode> inter_layer_pred_modes = {
778       InterLayerPredMode::kOff, InterLayerPredMode::kOn,
779       InterLayerPredMode::kOnKeyPic};
780 
781   for (const InterLayerPredMode inter_layer_pred : inter_layer_pred_modes) {
782     codec_settings_.VP9()->interLayerPred = inter_layer_pred;
783     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
784               encoder_->InitEncode(&codec_settings_, kSettings));
785 
786     VideoBitrateAllocation bitrate_allocation;
787     for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
788       bitrate_allocation.SetBitrate(
789           sl_idx, 0,
790           codec_settings_.spatialLayers[sl_idx].targetBitrate * 1000);
791       encoder_->SetRates(VideoEncoder::RateControlParameters(
792           bitrate_allocation, codec_settings_.maxFramerate));
793 
794       for (size_t frame_num = 0; frame_num < num_frames_to_encode;
795            ++frame_num) {
796         SetWaitForEncodedFramesThreshold(sl_idx + 1);
797         EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
798                   encoder_->Encode(NextInputFrame(), nullptr));
799         std::vector<EncodedImage> encoded_frame;
800         std::vector<CodecSpecificInfo> codec_specific_info;
801         ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
802 
803         ASSERT_EQ(codec_specific_info.size(), sl_idx + 1);
804 
805         for (size_t i = 0; i <= sl_idx; ++i) {
806           const bool is_keyframe =
807               encoded_frame[0]._frameType == VideoFrameType::kVideoFrameKey;
808           const bool is_first_upper_layer_frame =
809               (i == sl_idx && frame_num == 0);
810           // Interframe references are there, unless it's a keyframe,
811           // or it's a first activated frame in a upper layer
812           const bool expect_no_references =
813               is_keyframe || (is_first_upper_layer_frame &&
814                               inter_layer_pred == InterLayerPredMode::kOn);
815           EXPECT_EQ(
816               codec_specific_info[i].codecSpecific.VP9.inter_pic_predicted,
817               !expect_no_references);
818         }
819       }
820     }
821   }
822 }
823 
824 TEST_F(TestVp9Impl, EnablingDisablingUpperLayerInTheSameGof) {
825   const size_t num_spatial_layers = 2;
826   const size_t num_temporal_layers = 2;
827 
828   ConfigureSvc(num_spatial_layers, num_temporal_layers);
829   codec_settings_.VP9()->frameDroppingOn = false;
830   codec_settings_.VP9()->flexibleMode = false;
831 
832   codec_settings_.VP9()->interLayerPred = InterLayerPredMode::kOn;
833   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
834             encoder_->InitEncode(&codec_settings_, kSettings));
835 
836   VideoBitrateAllocation bitrate_allocation;
837 
838   // Enable both spatial and both temporal layers.
839   bitrate_allocation.SetBitrate(
840       0, 0, codec_settings_.spatialLayers[0].targetBitrate * 1000 / 2);
841   bitrate_allocation.SetBitrate(
842       0, 1, codec_settings_.spatialLayers[0].targetBitrate * 1000 / 2);
843   bitrate_allocation.SetBitrate(
844       1, 0, codec_settings_.spatialLayers[1].targetBitrate * 1000 / 2);
845   bitrate_allocation.SetBitrate(
846       1, 1, codec_settings_.spatialLayers[1].targetBitrate * 1000 / 2);
847   encoder_->SetRates(VideoEncoder::RateControlParameters(
848       bitrate_allocation, codec_settings_.maxFramerate));
849 
850   std::vector<EncodedImage> encoded_frame;
851   std::vector<CodecSpecificInfo> codec_specific_info;
852 
853   // Encode 3 frames.
854   for (int i = 0; i < 3; ++i) {
855     SetWaitForEncodedFramesThreshold(2);
856     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
857               encoder_->Encode(NextInputFrame(), nullptr));
858     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
859     ASSERT_EQ(codec_specific_info.size(), 2u);
860   }
861 
862   // Disable SL1 layer.
863   bitrate_allocation.SetBitrate(1, 0, 0);
864   bitrate_allocation.SetBitrate(1, 1, 0);
865   encoder_->SetRates(VideoEncoder::RateControlParameters(
866       bitrate_allocation, codec_settings_.maxFramerate));
867 
868   // Encode 1 frame.
869   SetWaitForEncodedFramesThreshold(1);
870   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
871   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
872   ASSERT_EQ(codec_specific_info.size(), 1u);
873   EXPECT_EQ(encoded_frame[0]._frameType, VideoFrameType::kVideoFrameDelta);
874   EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.temporal_idx, 1);
875   EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.inter_pic_predicted, true);
876 
877   // Enable SL1 layer.
878   bitrate_allocation.SetBitrate(
879       1, 0, codec_settings_.spatialLayers[1].targetBitrate * 1000 / 2);
880   bitrate_allocation.SetBitrate(
881       1, 1, codec_settings_.spatialLayers[1].targetBitrate * 1000 / 2);
882   encoder_->SetRates(VideoEncoder::RateControlParameters(
883       bitrate_allocation, codec_settings_.maxFramerate));
884 
885   // Encode 1 frame.
886   SetWaitForEncodedFramesThreshold(2);
887   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
888   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
889   ASSERT_EQ(codec_specific_info.size(), 2u);
890   EXPECT_EQ(encoded_frame[0]._frameType, VideoFrameType::kVideoFrameDelta);
891   EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.temporal_idx, 0);
892   EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.inter_pic_predicted, true);
893   EXPECT_EQ(codec_specific_info[1].codecSpecific.VP9.inter_pic_predicted, true);
894 }
895 
896 TEST_F(TestVp9Impl, EnablingDisablingUpperLayerAccrossGof) {
897   const size_t num_spatial_layers = 2;
898   const size_t num_temporal_layers = 2;
899 
900   ConfigureSvc(num_spatial_layers, num_temporal_layers);
901   codec_settings_.VP9()->frameDroppingOn = false;
902   codec_settings_.VP9()->flexibleMode = false;
903 
904   codec_settings_.VP9()->interLayerPred = InterLayerPredMode::kOn;
905   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
906             encoder_->InitEncode(&codec_settings_, kSettings));
907 
908   VideoBitrateAllocation bitrate_allocation;
909 
910   // Enable both spatial and both temporal layers.
911   bitrate_allocation.SetBitrate(
912       0, 0, codec_settings_.spatialLayers[0].targetBitrate * 1000 / 2);
913   bitrate_allocation.SetBitrate(
914       0, 1, codec_settings_.spatialLayers[0].targetBitrate * 1000 / 2);
915   bitrate_allocation.SetBitrate(
916       1, 0, codec_settings_.spatialLayers[1].targetBitrate * 1000 / 2);
917   bitrate_allocation.SetBitrate(
918       1, 1, codec_settings_.spatialLayers[1].targetBitrate * 1000 / 2);
919   encoder_->SetRates(VideoEncoder::RateControlParameters(
920       bitrate_allocation, codec_settings_.maxFramerate));
921 
922   std::vector<EncodedImage> encoded_frame;
923   std::vector<CodecSpecificInfo> codec_specific_info;
924 
925   // Encode 3 frames.
926   for (int i = 0; i < 3; ++i) {
927     SetWaitForEncodedFramesThreshold(2);
928     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
929               encoder_->Encode(NextInputFrame(), nullptr));
930     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
931     ASSERT_EQ(codec_specific_info.size(), 2u);
932   }
933 
934   // Disable SL1 layer.
935   bitrate_allocation.SetBitrate(1, 0, 0);
936   bitrate_allocation.SetBitrate(1, 1, 0);
937   encoder_->SetRates(VideoEncoder::RateControlParameters(
938       bitrate_allocation, codec_settings_.maxFramerate));
939 
940   // Encode 11 frames. More than Gof length 2, and odd to end at TL1 frame.
941   for (int i = 0; i < 11; ++i) {
942     SetWaitForEncodedFramesThreshold(1);
943     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
944               encoder_->Encode(NextInputFrame(), nullptr));
945     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
946     ASSERT_EQ(codec_specific_info.size(), 1u);
947     EXPECT_EQ(encoded_frame[0]._frameType, VideoFrameType::kVideoFrameDelta);
948     EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.temporal_idx, 1 - i % 2);
949     EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.inter_pic_predicted,
950               true);
951   }
952 
953   // Enable SL1 layer.
954   bitrate_allocation.SetBitrate(
955       1, 0, codec_settings_.spatialLayers[1].targetBitrate * 1000 / 2);
956   bitrate_allocation.SetBitrate(
957       1, 1, codec_settings_.spatialLayers[1].targetBitrate * 1000 / 2);
958   encoder_->SetRates(VideoEncoder::RateControlParameters(
959       bitrate_allocation, codec_settings_.maxFramerate));
960 
961   // Encode 1 frame.
962   SetWaitForEncodedFramesThreshold(2);
963   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
964   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frame, &codec_specific_info));
965   ASSERT_EQ(codec_specific_info.size(), 2u);
966   EXPECT_EQ(encoded_frame[0]._frameType, VideoFrameType::kVideoFrameDelta);
967   EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.temporal_idx, 0);
968   EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.inter_pic_predicted, true);
969   EXPECT_EQ(codec_specific_info[1].codecSpecific.VP9.inter_pic_predicted,
970             false);
971 }
972 
973 TEST_F(TestVp9Impl, EnablingNewLayerInScreenshareForcesAllLayersWithSS) {
974   const size_t num_spatial_layers = 3;
975   // Chosen by hand, the 2nd frame is dropped with configured per-layer max
976   // framerate.
977   const size_t num_frames_to_encode_before_drop = 1;
978 
979   codec_settings_.maxFramerate = 30;
980   ConfigureSvc(num_spatial_layers);
981   codec_settings_.spatialLayers[0].maxFramerate = 5.0;
982   // use 30 for the SL 1 instead of 10, so even if SL 0 frame is dropped due to
983   // framerate capping we would still get back at least a middle layer. It
984   // simplifies the test.
985   codec_settings_.spatialLayers[1].maxFramerate = 30.0;
986   codec_settings_.spatialLayers[2].maxFramerate = 30.0;
987   codec_settings_.VP9()->frameDroppingOn = false;
988   codec_settings_.mode = VideoCodecMode::kScreensharing;
989   codec_settings_.VP9()->interLayerPred = InterLayerPredMode::kOn;
990   codec_settings_.VP9()->flexibleMode = true;
991   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
992             encoder_->InitEncode(&codec_settings_, kSettings));
993 
994   // Enable all but the last layer.
995   VideoBitrateAllocation bitrate_allocation;
996   for (size_t sl_idx = 0; sl_idx < num_spatial_layers - 1; ++sl_idx) {
997     bitrate_allocation.SetBitrate(
998         sl_idx, 0, codec_settings_.spatialLayers[sl_idx].targetBitrate * 1000);
999   }
1000   encoder_->SetRates(VideoEncoder::RateControlParameters(
1001       bitrate_allocation, codec_settings_.maxFramerate));
1002 
1003   // Encode enough frames to force drop due to framerate capping.
1004   for (size_t frame_num = 0; frame_num < num_frames_to_encode_before_drop;
1005        ++frame_num) {
1006     SetWaitForEncodedFramesThreshold(num_spatial_layers - 1);
1007     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1008               encoder_->Encode(NextInputFrame(), nullptr));
1009     std::vector<EncodedImage> encoded_frames;
1010     std::vector<CodecSpecificInfo> codec_specific_info;
1011     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific_info));
1012   }
1013 
1014   // Enable the last layer.
1015   bitrate_allocation.SetBitrate(
1016       num_spatial_layers - 1, 0,
1017       codec_settings_.spatialLayers[num_spatial_layers - 1].targetBitrate *
1018           1000);
1019   encoder_->SetRates(VideoEncoder::RateControlParameters(
1020       bitrate_allocation, codec_settings_.maxFramerate));
1021 
1022   // All layers are encoded, even though frame dropping should happen.
1023   SetWaitForEncodedFramesThreshold(num_spatial_layers);
1024   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
1025   // Now all 3 layers should be encoded.
1026   std::vector<EncodedImage> encoded_frames;
1027   std::vector<CodecSpecificInfo> codec_specific_info;
1028   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific_info));
1029   EXPECT_EQ(encoded_frames.size(), 3u);
1030   // Scalability structure has to be triggered.
1031   EXPECT_TRUE(codec_specific_info[0].codecSpecific.VP9.ss_data_available);
1032 }
1033 
1034 TEST_F(TestVp9Impl, ScreenshareFrameDropping) {
1035   const int num_spatial_layers = 3;
1036   const int num_frames_to_detect_drops = 2;
1037 
1038   codec_settings_.maxFramerate = 30;
1039   ConfigureSvc(num_spatial_layers);
1040   // use 30 for the SL0 and SL1 because it simplifies the test.
1041   codec_settings_.spatialLayers[0].maxFramerate = 30.0;
1042   codec_settings_.spatialLayers[1].maxFramerate = 30.0;
1043   codec_settings_.spatialLayers[2].maxFramerate = 30.0;
1044   codec_settings_.VP9()->frameDroppingOn = true;
1045   codec_settings_.mode = VideoCodecMode::kScreensharing;
1046   codec_settings_.VP9()->interLayerPred = InterLayerPredMode::kOn;
1047   codec_settings_.VP9()->flexibleMode = true;
1048   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1049             encoder_->InitEncode(&codec_settings_, kSettings));
1050 
1051   // Enable all but the last layer.
1052   VideoBitrateAllocation bitrate_allocation;
1053   // Very low bitrate for the lowest spatial layer to ensure rate-control drops.
1054   bitrate_allocation.SetBitrate(0, 0, 1000);
1055   bitrate_allocation.SetBitrate(
1056       1, 0, codec_settings_.spatialLayers[1].targetBitrate * 1000);
1057   // Disable highest layer.
1058   bitrate_allocation.SetBitrate(2, 0, 0);
1059 
1060   encoder_->SetRates(VideoEncoder::RateControlParameters(
1061       bitrate_allocation, codec_settings_.maxFramerate));
1062 
1063   bool frame_dropped = false;
1064   // Encode enough frames to force drop due to rate-control.
1065   for (size_t frame_num = 0; frame_num < num_frames_to_detect_drops;
1066        ++frame_num) {
1067     SetWaitForEncodedFramesThreshold(1);
1068     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1069               encoder_->Encode(NextInputFrame(), nullptr));
1070     std::vector<EncodedImage> encoded_frames;
1071     std::vector<CodecSpecificInfo> codec_specific_info;
1072     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific_info));
1073     EXPECT_LE(encoded_frames.size(), 2u);
1074     EXPECT_GE(encoded_frames.size(), 1u);
1075     if (encoded_frames.size() == 1) {
1076       frame_dropped = true;
1077       // Dropped frame is on the SL0.
1078       EXPECT_EQ(encoded_frames[0].SpatialIndex(), 1);
1079     }
1080   }
1081   EXPECT_TRUE(frame_dropped);
1082 
1083   // Enable the last layer.
1084   bitrate_allocation.SetBitrate(
1085       2, 0, codec_settings_.spatialLayers[2].targetBitrate * 1000);
1086   encoder_->SetRates(VideoEncoder::RateControlParameters(
1087       bitrate_allocation, codec_settings_.maxFramerate));
1088   SetWaitForEncodedFramesThreshold(1);
1089   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
1090   std::vector<EncodedImage> encoded_frames;
1091   std::vector<CodecSpecificInfo> codec_specific_info;
1092   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific_info));
1093   // No drop allowed.
1094   EXPECT_EQ(encoded_frames.size(), 3u);
1095 
1096   // Verify that frame-dropping is re-enabled back.
1097   frame_dropped = false;
1098   // Encode enough frames to force drop due to rate-control.
1099   for (size_t frame_num = 0; frame_num < num_frames_to_detect_drops;
1100        ++frame_num) {
1101     SetWaitForEncodedFramesThreshold(1);
1102     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1103               encoder_->Encode(NextInputFrame(), nullptr));
1104     std::vector<EncodedImage> encoded_frames;
1105     std::vector<CodecSpecificInfo> codec_specific_info;
1106     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific_info));
1107     EXPECT_LE(encoded_frames.size(), 3u);
1108     EXPECT_GE(encoded_frames.size(), 2u);
1109     if (encoded_frames.size() == 2) {
1110       frame_dropped = true;
1111       // Dropped frame is on the SL0.
1112       EXPECT_EQ(encoded_frames[0].SpatialIndex(), 1);
1113       EXPECT_EQ(encoded_frames[1].SpatialIndex(), 2);
1114     }
1115   }
1116   EXPECT_TRUE(frame_dropped);
1117 }
1118 
1119 TEST_F(TestVp9Impl, RemovingLayerIsNotDelayedInScreenshareAndAddsSsInfo) {
1120   const size_t num_spatial_layers = 3;
1121   // Chosen by hand, the 2nd frame is dropped with configured per-layer max
1122   // framerate.
1123   const size_t num_frames_to_encode_before_drop = 1;
1124   // Chosen by hand, exactly 5 frames are dropped for input fps=30 and max
1125   // framerate = 5.
1126   const size_t num_dropped_frames = 5;
1127 
1128   codec_settings_.maxFramerate = 30;
1129   ConfigureSvc(num_spatial_layers);
1130   codec_settings_.spatialLayers[0].maxFramerate = 5.0;
1131   // use 30 for the SL 1 instead of 5, so even if SL 0 frame is dropped due to
1132   // framerate capping we would still get back at least a middle layer. It
1133   // simplifies the test.
1134   codec_settings_.spatialLayers[1].maxFramerate = 30.0;
1135   codec_settings_.spatialLayers[2].maxFramerate = 30.0;
1136   codec_settings_.VP9()->frameDroppingOn = false;
1137   codec_settings_.mode = VideoCodecMode::kScreensharing;
1138   codec_settings_.VP9()->interLayerPred = InterLayerPredMode::kOn;
1139   codec_settings_.VP9()->flexibleMode = true;
1140   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1141             encoder_->InitEncode(&codec_settings_, kSettings));
1142 
1143   // All layers are enabled from the start.
1144   VideoBitrateAllocation bitrate_allocation;
1145   for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
1146     bitrate_allocation.SetBitrate(
1147         sl_idx, 0, codec_settings_.spatialLayers[sl_idx].targetBitrate * 1000);
1148   }
1149   encoder_->SetRates(VideoEncoder::RateControlParameters(
1150       bitrate_allocation, codec_settings_.maxFramerate));
1151 
1152   // Encode enough frames to force drop due to framerate capping.
1153   for (size_t frame_num = 0; frame_num < num_frames_to_encode_before_drop;
1154        ++frame_num) {
1155     SetWaitForEncodedFramesThreshold(num_spatial_layers);
1156     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1157               encoder_->Encode(NextInputFrame(), nullptr));
1158     std::vector<EncodedImage> encoded_frames;
1159     std::vector<CodecSpecificInfo> codec_specific_info;
1160     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific_info));
1161   }
1162 
1163   // Now the first layer should not have frames in it.
1164   for (size_t frame_num = 0; frame_num < num_dropped_frames - 2; ++frame_num) {
1165     SetWaitForEncodedFramesThreshold(2);
1166     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1167               encoder_->Encode(NextInputFrame(), nullptr));
1168     // First layer is dropped due to frame rate cap. The last layer should not
1169     // be enabled yet.
1170     std::vector<EncodedImage> encoded_frames;
1171     std::vector<CodecSpecificInfo> codec_specific_info;
1172     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific_info));
1173     // First layer is skipped.
1174     EXPECT_EQ(encoded_frames[0].SpatialIndex().value_or(-1), 1);
1175   }
1176 
1177   // Disable the last layer.
1178   bitrate_allocation.SetBitrate(num_spatial_layers - 1, 0, 0);
1179   encoder_->SetRates(VideoEncoder::RateControlParameters(
1180       bitrate_allocation, codec_settings_.maxFramerate));
1181 
1182   // Still expected to drop first layer. Last layer has to be disable also.
1183   for (size_t frame_num = num_dropped_frames - 2;
1184        frame_num < num_dropped_frames; ++frame_num) {
1185     // Expect back one frame.
1186     SetWaitForEncodedFramesThreshold(1);
1187     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1188               encoder_->Encode(NextInputFrame(), nullptr));
1189     // First layer is dropped due to frame rate cap. The last layer should not
1190     // be enabled yet.
1191     std::vector<EncodedImage> encoded_frames;
1192     std::vector<CodecSpecificInfo> codec_specific_info;
1193     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific_info));
1194     // First layer is skipped.
1195     EXPECT_EQ(encoded_frames[0].SpatialIndex().value_or(-1), 1);
1196     // No SS data on non-base spatial layer.
1197     EXPECT_FALSE(codec_specific_info[0].codecSpecific.VP9.ss_data_available);
1198   }
1199 
1200   SetWaitForEncodedFramesThreshold(2);
1201   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
1202   std::vector<EncodedImage> encoded_frames;
1203   std::vector<CodecSpecificInfo> codec_specific_info;
1204   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific_info));
1205   // First layer is not skipped now.
1206   EXPECT_EQ(encoded_frames[0].SpatialIndex().value_or(-1), 0);
1207   // SS data should be present.
1208   EXPECT_TRUE(codec_specific_info[0].codecSpecific.VP9.ss_data_available);
1209 }
1210 
1211 TEST_F(TestVp9Impl, DisableNewLayerInVideoDelaysSsInfoTillTL0) {
1212   const size_t num_spatial_layers = 3;
1213   const size_t num_temporal_layers = 2;
1214   // Chosen by hand, the 2nd frame is dropped with configured per-layer max
1215   // framerate.
1216   ConfigureSvc(num_spatial_layers, num_temporal_layers);
1217   codec_settings_.VP9()->frameDroppingOn = false;
1218   codec_settings_.mode = VideoCodecMode::kRealtimeVideo;
1219   codec_settings_.VP9()->interLayerPred = InterLayerPredMode::kOnKeyPic;
1220   codec_settings_.VP9()->flexibleMode = false;
1221   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1222             encoder_->InitEncode(&codec_settings_, kSettings));
1223 
1224   // Enable all the layers.
1225   VideoBitrateAllocation bitrate_allocation;
1226   for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
1227     for (size_t tl_idx = 0; tl_idx < num_temporal_layers; ++tl_idx) {
1228       bitrate_allocation.SetBitrate(
1229           sl_idx, tl_idx,
1230           codec_settings_.spatialLayers[sl_idx].targetBitrate * 1000 /
1231               num_temporal_layers);
1232     }
1233   }
1234   encoder_->SetRates(VideoEncoder::RateControlParameters(
1235       bitrate_allocation, codec_settings_.maxFramerate));
1236 
1237   std::vector<EncodedImage> encoded_frames;
1238   std::vector<CodecSpecificInfo> codec_specific_info;
1239 
1240   // Encode one TL0 frame
1241   SetWaitForEncodedFramesThreshold(num_spatial_layers);
1242   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
1243   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific_info));
1244   EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.temporal_idx, 0u);
1245 
1246   // Disable the last layer.
1247   for (size_t tl_idx = 0; tl_idx < num_temporal_layers; ++tl_idx) {
1248     bitrate_allocation.SetBitrate(num_spatial_layers - 1, tl_idx, 0);
1249   }
1250   encoder_->SetRates(VideoEncoder::RateControlParameters(
1251       bitrate_allocation, codec_settings_.maxFramerate));
1252 
1253   // Next is TL1 frame. The last layer is disabled immediately, but SS structure
1254   // is not provided here.
1255   SetWaitForEncodedFramesThreshold(num_spatial_layers - 1);
1256   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
1257   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific_info));
1258   EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.temporal_idx, 1u);
1259   EXPECT_FALSE(codec_specific_info[0].codecSpecific.VP9.ss_data_available);
1260 
1261   // Next is TL0 frame, which should have delayed SS structure.
1262   SetWaitForEncodedFramesThreshold(num_spatial_layers - 1);
1263   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
1264   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific_info));
1265   EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.temporal_idx, 0u);
1266   EXPECT_TRUE(codec_specific_info[0].codecSpecific.VP9.ss_data_available);
1267   EXPECT_TRUE(codec_specific_info[0]
1268                   .codecSpecific.VP9.spatial_layer_resolution_present);
1269   EXPECT_EQ(codec_specific_info[0].codecSpecific.VP9.num_spatial_layers,
1270             num_spatial_layers - 1);
1271 }
1272 
1273 TEST_F(TestVp9Impl,
1274        LowLayerMarkedAsRefIfHighLayerNotEncodedAndInterLayerPredIsEnabled) {
1275   ConfigureSvc(3);
1276   codec_settings_.VP9()->frameDroppingOn = false;
1277   codec_settings_.VP9()->interLayerPred = InterLayerPredMode::kOn;
1278 
1279   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1280             encoder_->InitEncode(&codec_settings_, kSettings));
1281 
1282   VideoBitrateAllocation bitrate_allocation;
1283   bitrate_allocation.SetBitrate(
1284       0, 0, codec_settings_.spatialLayers[0].targetBitrate * 1000);
1285   encoder_->SetRates(VideoEncoder::RateControlParameters(
1286       bitrate_allocation, codec_settings_.maxFramerate));
1287   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
1288   EncodedImage encoded_frame;
1289   CodecSpecificInfo codec_info;
1290   ASSERT_TRUE(WaitForEncodedFrame(&encoded_frame, &codec_info));
1291   EXPECT_TRUE(codec_info.codecSpecific.VP9.ss_data_available);
1292   EXPECT_FALSE(codec_info.codecSpecific.VP9.non_ref_for_inter_layer_pred);
1293 }
1294 
1295 TEST_F(TestVp9Impl, ScalabilityStructureIsAvailableInFlexibleMode) {
1296   codec_settings_.VP9()->flexibleMode = true;
1297   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1298             encoder_->InitEncode(&codec_settings_, kSettings));
1299 
1300   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
1301   EncodedImage encoded_frame;
1302   CodecSpecificInfo codec_specific_info;
1303   ASSERT_TRUE(WaitForEncodedFrame(&encoded_frame, &codec_specific_info));
1304   EXPECT_TRUE(codec_specific_info.codecSpecific.VP9.ss_data_available);
1305 }
1306 
1307 TEST_F(TestVp9Impl, EncoderInfoFpsAllocation) {
1308   const uint8_t kNumSpatialLayers = 3;
1309   const uint8_t kNumTemporalLayers = 3;
1310 
1311   codec_settings_.maxFramerate = 30;
1312   codec_settings_.VP9()->numberOfSpatialLayers = kNumSpatialLayers;
1313   codec_settings_.VP9()->numberOfTemporalLayers = kNumTemporalLayers;
1314 
1315   for (uint8_t sl_idx = 0; sl_idx < kNumSpatialLayers; ++sl_idx) {
1316     codec_settings_.spatialLayers[sl_idx].width = codec_settings_.width;
1317     codec_settings_.spatialLayers[sl_idx].height = codec_settings_.height;
1318     codec_settings_.spatialLayers[sl_idx].minBitrate =
1319         codec_settings_.startBitrate;
1320     codec_settings_.spatialLayers[sl_idx].maxBitrate =
1321         codec_settings_.startBitrate;
1322     codec_settings_.spatialLayers[sl_idx].targetBitrate =
1323         codec_settings_.startBitrate;
1324     codec_settings_.spatialLayers[sl_idx].active = true;
1325     codec_settings_.spatialLayers[sl_idx].maxFramerate =
1326         codec_settings_.maxFramerate;
1327   }
1328 
1329   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1330             encoder_->InitEncode(&codec_settings_, kSettings));
1331 
1332   FramerateFractions expected_fps_allocation[kMaxSpatialLayers];
1333   expected_fps_allocation[0].push_back(EncoderInfo::kMaxFramerateFraction / 4);
1334   expected_fps_allocation[0].push_back(EncoderInfo::kMaxFramerateFraction / 2);
1335   expected_fps_allocation[0].push_back(EncoderInfo::kMaxFramerateFraction);
1336   expected_fps_allocation[1] = expected_fps_allocation[0];
1337   expected_fps_allocation[2] = expected_fps_allocation[0];
1338   EXPECT_THAT(encoder_->GetEncoderInfo().fps_allocation,
1339               ::testing::ElementsAreArray(expected_fps_allocation));
1340 }
1341 
1342 TEST_F(TestVp9Impl, EncoderInfoFpsAllocationFlexibleMode) {
1343   const uint8_t kNumSpatialLayers = 3;
1344 
1345   codec_settings_.maxFramerate = 30;
1346   codec_settings_.VP9()->numberOfSpatialLayers = kNumSpatialLayers;
1347   codec_settings_.VP9()->numberOfTemporalLayers = 1;
1348   codec_settings_.VP9()->flexibleMode = true;
1349 
1350   VideoEncoder::RateControlParameters rate_params;
1351   for (uint8_t sl_idx = 0; sl_idx < kNumSpatialLayers; ++sl_idx) {
1352     codec_settings_.spatialLayers[sl_idx].width = codec_settings_.width;
1353     codec_settings_.spatialLayers[sl_idx].height = codec_settings_.height;
1354     codec_settings_.spatialLayers[sl_idx].minBitrate =
1355         codec_settings_.startBitrate;
1356     codec_settings_.spatialLayers[sl_idx].maxBitrate =
1357         codec_settings_.startBitrate;
1358     codec_settings_.spatialLayers[sl_idx].targetBitrate =
1359         codec_settings_.startBitrate;
1360     codec_settings_.spatialLayers[sl_idx].active = true;
1361     // Force different frame rates for different layers, to verify that total
1362     // fraction is correct.
1363     codec_settings_.spatialLayers[sl_idx].maxFramerate =
1364         codec_settings_.maxFramerate / (kNumSpatialLayers - sl_idx);
1365     rate_params.bitrate.SetBitrate(sl_idx, 0,
1366                                    codec_settings_.startBitrate * 1000);
1367   }
1368   rate_params.bandwidth_allocation =
1369       DataRate::BitsPerSec(rate_params.bitrate.get_sum_bps());
1370   rate_params.framerate_fps = codec_settings_.maxFramerate;
1371 
1372   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1373             encoder_->InitEncode(&codec_settings_, kSettings));
1374 
1375   // No temporal layers allowed when spatial layers have different fps targets.
1376   FramerateFractions expected_fps_allocation[kMaxSpatialLayers];
1377   expected_fps_allocation[0].push_back(EncoderInfo::kMaxFramerateFraction / 3);
1378   expected_fps_allocation[1].push_back(EncoderInfo::kMaxFramerateFraction / 2);
1379   expected_fps_allocation[2].push_back(EncoderInfo::kMaxFramerateFraction);
1380   EXPECT_THAT(encoder_->GetEncoderInfo().fps_allocation,
1381               ::testing::ElementsAreArray(expected_fps_allocation));
1382 
1383   // SetRates with current fps does not alter outcome.
1384   encoder_->SetRates(rate_params);
1385   EXPECT_THAT(encoder_->GetEncoderInfo().fps_allocation,
1386               ::testing::ElementsAreArray(expected_fps_allocation));
1387 
1388   // Higher fps than the codec wants, should still not affect outcome.
1389   rate_params.framerate_fps *= 2;
1390   encoder_->SetRates(rate_params);
1391   EXPECT_THAT(encoder_->GetEncoderInfo().fps_allocation,
1392               ::testing::ElementsAreArray(expected_fps_allocation));
1393 }
1394 
1395 class Vp9ImplWithLayeringTest
1396     : public ::testing::TestWithParam<std::tuple<int, int, bool>> {
1397  protected:
1398   Vp9ImplWithLayeringTest()
1399       : num_spatial_layers_(std::get<0>(GetParam())),
1400         num_temporal_layers_(std::get<1>(GetParam())),
1401         override_field_trials_(std::get<2>(GetParam())
1402                                    ? "WebRTC-Vp9ExternalRefCtrl/Enabled/"
1403                                    : "") {}
1404 
1405   const uint8_t num_spatial_layers_;
1406   const uint8_t num_temporal_layers_;
1407   const test::ScopedFieldTrials override_field_trials_;
1408 };
1409 
1410 TEST_P(Vp9ImplWithLayeringTest, FlexibleMode) {
1411   // In flexible mode encoder wrapper obtains actual list of references from
1412   // encoder and writes it into RTP payload descriptor. Check that reference
1413   // list in payload descriptor matches the predefined one, which is used
1414   // in non-flexible mode.
1415   std::unique_ptr<VideoEncoder> encoder = VP9Encoder::Create();
1416   VideoCodec codec_settings = DefaultCodecSettings();
1417   codec_settings.VP9()->flexibleMode = true;
1418   codec_settings.VP9()->frameDroppingOn = false;
1419   codec_settings.VP9()->numberOfSpatialLayers = num_spatial_layers_;
1420   codec_settings.VP9()->numberOfTemporalLayers = num_temporal_layers_;
1421   EXPECT_EQ(encoder->InitEncode(&codec_settings, kSettings),
1422             WEBRTC_VIDEO_CODEC_OK);
1423 
1424   GofInfoVP9 gof;
1425   if (num_temporal_layers_ == 1) {
1426     gof.SetGofInfoVP9(kTemporalStructureMode1);
1427   } else if (num_temporal_layers_ == 2) {
1428     gof.SetGofInfoVP9(kTemporalStructureMode2);
1429   } else if (num_temporal_layers_ == 3) {
1430     gof.SetGofInfoVP9(kTemporalStructureMode3);
1431   }
1432 
1433   // Encode at least (num_frames_in_gof + 1) frames to verify references
1434   // of non-key frame with gof_idx = 0.
1435   int num_input_frames = gof.num_frames_in_gof + 1;
1436   std::vector<EncodedVideoFrameProducer::EncodedFrame> frames =
1437       EncodedVideoFrameProducer(*encoder)
1438           .SetNumInputFrames(num_input_frames)
1439           .SetResolution({kWidth, kHeight})
1440           .Encode();
1441   ASSERT_THAT(frames, SizeIs(num_input_frames * num_spatial_layers_));
1442 
1443   for (size_t i = 0; i < frames.size(); ++i) {
1444     const EncodedVideoFrameProducer::EncodedFrame& frame = frames[i];
1445     const size_t picture_idx = i / num_spatial_layers_;
1446     const size_t gof_idx = picture_idx % gof.num_frames_in_gof;
1447 
1448     const CodecSpecificInfoVP9& vp9 =
1449         frame.codec_specific_info.codecSpecific.VP9;
1450     EXPECT_EQ(frame.encoded_image.SpatialIndex(),
1451               num_spatial_layers_ == 1
1452                   ? absl::nullopt
1453                   : absl::optional<int>(i % num_spatial_layers_))
1454         << "Frame " << i;
1455     EXPECT_EQ(vp9.temporal_idx, num_temporal_layers_ == 1
1456                                     ? kNoTemporalIdx
1457                                     : gof.temporal_idx[gof_idx])
1458         << "Frame " << i;
1459     EXPECT_EQ(vp9.temporal_up_switch, gof.temporal_up_switch[gof_idx])
1460         << "Frame " << i;
1461     if (picture_idx == 0) {
1462       EXPECT_EQ(vp9.num_ref_pics, 0) << "Frame " << i;
1463     } else {
1464       EXPECT_THAT(rtc::MakeArrayView(vp9.p_diff, vp9.num_ref_pics),
1465                   UnorderedElementsAreArray(gof.pid_diff[gof_idx],
1466                                             gof.num_ref_pics[gof_idx]))
1467           << "Frame " << i;
1468     }
1469   }
1470 }
1471 
1472 INSTANTIATE_TEST_SUITE_P(All,
1473                          Vp9ImplWithLayeringTest,
1474                          ::testing::Combine(::testing::Values(1, 2, 3),
1475                                             ::testing::Values(1, 2, 3),
1476                                             ::testing::Bool()));
1477 
1478 class TestVp9ImplFrameDropping : public TestVp9Impl {
1479  protected:
1480   void ModifyCodecSettings(VideoCodec* codec_settings) override {
1481     webrtc::test::CodecSettings(kVideoCodecVP9, codec_settings);
1482     // We need to encode quite a lot of frames in this test. Use low resolution
1483     // to reduce execution time.
1484     codec_settings->width = 64;
1485     codec_settings->height = 64;
1486     codec_settings->mode = VideoCodecMode::kScreensharing;
1487   }
1488 };
1489 
1490 TEST_F(TestVp9ImplFrameDropping, PreEncodeFrameDropping) {
1491   const size_t num_frames_to_encode = 100;
1492   const float input_framerate_fps = 30.0;
1493   const float video_duration_secs = num_frames_to_encode / input_framerate_fps;
1494   const float expected_framerate_fps = 5.0f;
1495   const float max_abs_framerate_error_fps = expected_framerate_fps * 0.1f;
1496 
1497   codec_settings_.maxFramerate = static_cast<uint32_t>(expected_framerate_fps);
1498   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1499             encoder_->InitEncode(&codec_settings_, kSettings));
1500 
1501   VideoFrame input_frame = NextInputFrame();
1502   for (size_t frame_num = 0; frame_num < num_frames_to_encode; ++frame_num) {
1503     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(input_frame, nullptr));
1504     const size_t timestamp = input_frame.timestamp() +
1505                              kVideoPayloadTypeFrequency / input_framerate_fps;
1506     input_frame.set_timestamp(static_cast<uint32_t>(timestamp));
1507   }
1508 
1509   const size_t num_encoded_frames = GetNumEncodedFrames();
1510   const float encoded_framerate_fps = num_encoded_frames / video_duration_secs;
1511   EXPECT_NEAR(encoded_framerate_fps, expected_framerate_fps,
1512               max_abs_framerate_error_fps);
1513 }
1514 
1515 TEST_F(TestVp9ImplFrameDropping, DifferentFrameratePerSpatialLayer) {
1516   // Assign different frame rate to spatial layers and check that result frame
1517   // rate is close to the assigned one.
1518   const uint8_t num_spatial_layers = 3;
1519   const float input_framerate_fps = 30.0;
1520   const size_t video_duration_secs = 3;
1521   const size_t num_input_frames = video_duration_secs * input_framerate_fps;
1522 
1523   codec_settings_.VP9()->numberOfSpatialLayers = num_spatial_layers;
1524   codec_settings_.VP9()->frameDroppingOn = false;
1525   codec_settings_.VP9()->flexibleMode = true;
1526 
1527   VideoBitrateAllocation bitrate_allocation;
1528   for (uint8_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
1529     // Frame rate increases from low to high layer.
1530     const uint32_t framerate_fps = 10 * (sl_idx + 1);
1531 
1532     codec_settings_.spatialLayers[sl_idx].width = codec_settings_.width;
1533     codec_settings_.spatialLayers[sl_idx].height = codec_settings_.height;
1534     codec_settings_.spatialLayers[sl_idx].maxFramerate = framerate_fps;
1535     codec_settings_.spatialLayers[sl_idx].minBitrate =
1536         codec_settings_.startBitrate;
1537     codec_settings_.spatialLayers[sl_idx].maxBitrate =
1538         codec_settings_.startBitrate;
1539     codec_settings_.spatialLayers[sl_idx].targetBitrate =
1540         codec_settings_.startBitrate;
1541     codec_settings_.spatialLayers[sl_idx].active = true;
1542 
1543     bitrate_allocation.SetBitrate(
1544         sl_idx, 0, codec_settings_.spatialLayers[sl_idx].targetBitrate * 1000);
1545   }
1546 
1547   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1548             encoder_->InitEncode(&codec_settings_, kSettings));
1549 
1550   encoder_->SetRates(VideoEncoder::RateControlParameters(
1551       bitrate_allocation, codec_settings_.maxFramerate));
1552 
1553   VideoFrame input_frame = NextInputFrame();
1554   for (size_t frame_num = 0; frame_num < num_input_frames; ++frame_num) {
1555     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(input_frame, nullptr));
1556     const size_t timestamp = input_frame.timestamp() +
1557                              kVideoPayloadTypeFrequency / input_framerate_fps;
1558     input_frame.set_timestamp(static_cast<uint32_t>(timestamp));
1559   }
1560 
1561   std::vector<EncodedImage> encoded_frames;
1562   std::vector<CodecSpecificInfo> codec_infos;
1563   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_infos));
1564 
1565   std::vector<size_t> num_encoded_frames(num_spatial_layers, 0);
1566   for (EncodedImage& encoded_frame : encoded_frames) {
1567     ++num_encoded_frames[encoded_frame.SpatialIndex().value_or(0)];
1568   }
1569 
1570   for (uint8_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
1571     const float layer_target_framerate_fps =
1572         codec_settings_.spatialLayers[sl_idx].maxFramerate;
1573     const float layer_output_framerate_fps =
1574         static_cast<float>(num_encoded_frames[sl_idx]) / video_duration_secs;
1575     const float max_framerate_error_fps = layer_target_framerate_fps * 0.1f;
1576     EXPECT_NEAR(layer_output_framerate_fps, layer_target_framerate_fps,
1577                 max_framerate_error_fps);
1578   }
1579 }
1580 
1581 class TestVp9ImplProfile2 : public TestVp9Impl {
1582  protected:
1583   void SetUp() override {
1584     // Profile 2 might not be available on some platforms until
1585     // https://bugs.chromium.org/p/webm/issues/detail?id=1544 is solved.
1586     bool profile_2_is_supported = false;
1587     for (const auto& codec : SupportedVP9Codecs()) {
1588       if (ParseSdpForVP9Profile(codec.parameters)
1589               .value_or(VP9Profile::kProfile0) == VP9Profile::kProfile2) {
1590         profile_2_is_supported = true;
1591       }
1592     }
1593     if (!profile_2_is_supported)
1594       return;
1595 
1596     TestVp9Impl::SetUp();
1597     input_frame_generator_ = test::CreateSquareFrameGenerator(
1598         codec_settings_.width, codec_settings_.height,
1599         test::FrameGeneratorInterface::OutputType::kI010,
1600         absl::optional<int>());
1601   }
1602 
1603   std::unique_ptr<VideoEncoder> CreateEncoder() override {
1604     cricket::VideoCodec profile2_codec;
1605     profile2_codec.SetParam(kVP9FmtpProfileId,
1606                             VP9ProfileToString(VP9Profile::kProfile2));
1607     return VP9Encoder::Create(profile2_codec);
1608   }
1609 
1610   std::unique_ptr<VideoDecoder> CreateDecoder() override {
1611     return VP9Decoder::Create();
1612   }
1613 };
1614 
1615 TEST_F(TestVp9ImplProfile2, EncodeDecode) {
1616   if (!encoder_)
1617     return;
1618 
1619   VideoFrame input_frame = NextInputFrame();
1620   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(input_frame, nullptr));
1621   EncodedImage encoded_frame;
1622   CodecSpecificInfo codec_specific_info;
1623   ASSERT_TRUE(WaitForEncodedFrame(&encoded_frame, &codec_specific_info));
1624   // First frame should be a key frame.
1625   encoded_frame._frameType = VideoFrameType::kVideoFrameKey;
1626   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder_->Decode(encoded_frame, false, 0));
1627   std::unique_ptr<VideoFrame> decoded_frame;
1628   absl::optional<uint8_t> decoded_qp;
1629   ASSERT_TRUE(WaitForDecodedFrame(&decoded_frame, &decoded_qp));
1630   ASSERT_TRUE(decoded_frame);
1631 
1632   // TODO(emircan): Add PSNR for different color depths.
1633   EXPECT_GT(I420PSNR(*input_frame.video_frame_buffer()->ToI420(),
1634                      *decoded_frame->video_frame_buffer()->ToI420()),
1635             31);
1636 }
1637 
1638 TEST_F(TestVp9Impl, EncodeWithDynamicRate) {
1639   // Configured dynamic rate field trial and re-create the encoder.
1640   test::ScopedFieldTrials field_trials(
1641       "WebRTC-VideoRateControl/vp9_dynamic_rate:true/");
1642   SetUp();
1643 
1644   // Set 300kbps target with 100% headroom.
1645   VideoEncoder::RateControlParameters params;
1646   params.bandwidth_allocation = DataRate::BitsPerSec(300000);
1647   params.bitrate.SetBitrate(0, 0, params.bandwidth_allocation.bps());
1648   params.framerate_fps = 30.0;
1649 
1650   encoder_->SetRates(params);
1651   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
1652   EncodedImage encoded_frame;
1653   CodecSpecificInfo codec_specific_info;
1654   ASSERT_TRUE(WaitForEncodedFrame(&encoded_frame, &codec_specific_info));
1655 
1656   // Set no headroom and encode again.
1657   params.bandwidth_allocation = DataRate::Zero();
1658   encoder_->SetRates(params);
1659   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
1660   ASSERT_TRUE(WaitForEncodedFrame(&encoded_frame, &codec_specific_info));
1661 }
1662 
1663 TEST_F(TestVp9Impl, ReenablingUpperLayerAfterKFWithInterlayerPredIsEnabled) {
1664   const size_t num_spatial_layers = 2;
1665   const int num_frames_to_encode = 10;
1666   codec_settings_.VP9()->flexibleMode = true;
1667   codec_settings_.VP9()->frameDroppingOn = false;
1668   codec_settings_.VP9()->numberOfSpatialLayers = num_spatial_layers;
1669   codec_settings_.VP9()->numberOfTemporalLayers = 1;
1670   codec_settings_.VP9()->interLayerPred = InterLayerPredMode::kOn;
1671   // Force low frame-rate, so all layers are present for all frames.
1672   codec_settings_.maxFramerate = 5;
1673 
1674   ConfigureSvc(num_spatial_layers);
1675 
1676   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1677             encoder_->InitEncode(&codec_settings_, kSettings));
1678 
1679   VideoBitrateAllocation bitrate_allocation;
1680   for (size_t sl_idx = 0; sl_idx < num_spatial_layers; ++sl_idx) {
1681     bitrate_allocation.SetBitrate(
1682         sl_idx, 0, codec_settings_.spatialLayers[sl_idx].targetBitrate * 1000);
1683   }
1684   encoder_->SetRates(VideoEncoder::RateControlParameters(
1685       bitrate_allocation, codec_settings_.maxFramerate));
1686 
1687   std::vector<EncodedImage> encoded_frames;
1688   std::vector<CodecSpecificInfo> codec_specific;
1689 
1690   for (int i = 0; i < num_frames_to_encode; ++i) {
1691     SetWaitForEncodedFramesThreshold(num_spatial_layers);
1692     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1693               encoder_->Encode(NextInputFrame(), nullptr));
1694     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific));
1695     EXPECT_EQ(encoded_frames.size(), num_spatial_layers);
1696   }
1697 
1698   // Disable the last layer.
1699   bitrate_allocation.SetBitrate(num_spatial_layers - 1, 0, 0);
1700   encoder_->SetRates(VideoEncoder::RateControlParameters(
1701       bitrate_allocation, codec_settings_.maxFramerate));
1702 
1703   for (int i = 0; i < num_frames_to_encode; ++i) {
1704     SetWaitForEncodedFramesThreshold(num_spatial_layers - 1);
1705     EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1706               encoder_->Encode(NextInputFrame(), nullptr));
1707     ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific));
1708     EXPECT_EQ(encoded_frames.size(), num_spatial_layers - 1);
1709   }
1710 
1711   std::vector<VideoFrameType> frame_types = {VideoFrameType::kVideoFrameKey};
1712 
1713   // Force a key-frame with the last layer still disabled.
1714   SetWaitForEncodedFramesThreshold(num_spatial_layers - 1);
1715   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1716             encoder_->Encode(NextInputFrame(), &frame_types));
1717   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific));
1718   EXPECT_EQ(encoded_frames.size(), num_spatial_layers - 1);
1719   ASSERT_EQ(encoded_frames[0]._frameType, VideoFrameType::kVideoFrameKey);
1720 
1721   // Re-enable the last layer.
1722   bitrate_allocation.SetBitrate(
1723       num_spatial_layers - 1, 0,
1724       codec_settings_.spatialLayers[num_spatial_layers - 1].targetBitrate *
1725           1000);
1726   encoder_->SetRates(VideoEncoder::RateControlParameters(
1727       bitrate_allocation, codec_settings_.maxFramerate));
1728 
1729   SetWaitForEncodedFramesThreshold(num_spatial_layers);
1730   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, encoder_->Encode(NextInputFrame(), nullptr));
1731   ASSERT_TRUE(WaitForEncodedFrames(&encoded_frames, &codec_specific));
1732   EXPECT_EQ(encoded_frames.size(), num_spatial_layers);
1733   EXPECT_EQ(encoded_frames[0]._frameType, VideoFrameType::kVideoFrameDelta);
1734 }
1735 
1736 TEST_F(TestVp9Impl, HandlesEmptyInitDecode) {
1737   std::unique_ptr<VideoDecoder> decoder = CreateDecoder();
1738   // Check that nullptr settings are ok for decoder.
1739   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK,
1740             decoder->InitDecode(/*codec_settings=*/nullptr, 1));
1741   EXPECT_EQ(WEBRTC_VIDEO_CODEC_OK, decoder->Release());
1742 }
1743 
1744 }  // namespace webrtc
1745