1 /*
2  *  Copyright (c) 2018 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 "modules/video_coding/codecs/vp9/svc_config.h"
12 
13 #include <cstddef>
14 #include <vector>
15 
16 #include "modules/video_coding/codecs/vp9/include/vp9_globals.h"
17 #include "test/gtest.h"
18 
19 namespace webrtc {
TEST(SvcConfig,NumSpatialLayers)20 TEST(SvcConfig, NumSpatialLayers) {
21   const size_t max_num_spatial_layers = 6;
22   const size_t first_active_layer = 0;
23   const size_t num_spatial_layers = 2;
24 
25   std::vector<SpatialLayer> spatial_layers =
26       GetSvcConfig(kMinVp9SpatialLayerWidth << (num_spatial_layers - 1),
27                    kMinVp9SpatialLayerHeight << (num_spatial_layers - 1), 30,
28                    first_active_layer, max_num_spatial_layers, 1, false);
29 
30   EXPECT_EQ(spatial_layers.size(), num_spatial_layers);
31 }
32 
TEST(SvcConfig,AlwaysSendsAtLeastOneLayer)33 TEST(SvcConfig, AlwaysSendsAtLeastOneLayer) {
34   const size_t max_num_spatial_layers = 6;
35   const size_t first_active_layer = 5;
36 
37   std::vector<SpatialLayer> spatial_layers =
38       GetSvcConfig(kMinVp9SpatialLayerWidth, kMinVp9SpatialLayerHeight, 30,
39                    first_active_layer, max_num_spatial_layers, 1, false);
40   EXPECT_EQ(spatial_layers.size(), 1u);
41   EXPECT_EQ(spatial_layers.back().width, kMinVp9SpatialLayerWidth);
42 }
43 
TEST(SvcConfig,EnforcesMinimalRequiredParity)44 TEST(SvcConfig, EnforcesMinimalRequiredParity) {
45   const size_t max_num_spatial_layers = 3;
46   const size_t kOddSize = 1023;
47 
48   std::vector<SpatialLayer> spatial_layers =
49       GetSvcConfig(kOddSize, kOddSize, 30,
50                    /*first_active_layer=*/1, max_num_spatial_layers, 1, false);
51   // Since there are 2 layers total (1, 2), divisiblity by 2 is required.
52   EXPECT_EQ(spatial_layers.back().width, kOddSize - 1);
53   EXPECT_EQ(spatial_layers.back().width, kOddSize - 1);
54 
55   spatial_layers =
56       GetSvcConfig(kOddSize, kOddSize, 30,
57                    /*first_active_layer=*/0, max_num_spatial_layers, 1, false);
58   // Since there are 3 layers total (0, 1, 2), divisiblity by 4 is required.
59   EXPECT_EQ(spatial_layers.back().width, kOddSize - 3);
60   EXPECT_EQ(spatial_layers.back().width, kOddSize - 3);
61 
62   spatial_layers =
63       GetSvcConfig(kOddSize, kOddSize, 30,
64                    /*first_active_layer=*/2, max_num_spatial_layers, 1, false);
65   // Since there is only 1 layer active (2), divisiblity by 1 is required.
66   EXPECT_EQ(spatial_layers.back().width, kOddSize);
67   EXPECT_EQ(spatial_layers.back().width, kOddSize);
68 }
69 
TEST(SvcConfig,SkipsInactiveLayers)70 TEST(SvcConfig, SkipsInactiveLayers) {
71   const size_t num_spatial_layers = 4;
72   const size_t first_active_layer = 2;
73 
74   std::vector<SpatialLayer> spatial_layers =
75       GetSvcConfig(kMinVp9SpatialLayerWidth << (num_spatial_layers - 1),
76                    kMinVp9SpatialLayerHeight << (num_spatial_layers - 1), 30,
77                    first_active_layer, num_spatial_layers, 1, false);
78   EXPECT_EQ(spatial_layers.size(), 2u);
79   EXPECT_EQ(spatial_layers.back().width,
80             kMinVp9SpatialLayerWidth << (num_spatial_layers - 1));
81 }
82 
TEST(SvcConfig,BitrateThresholds)83 TEST(SvcConfig, BitrateThresholds) {
84   const size_t first_active_layer = 0;
85   const size_t num_spatial_layers = 3;
86   std::vector<SpatialLayer> spatial_layers =
87       GetSvcConfig(kMinVp9SpatialLayerWidth << (num_spatial_layers - 1),
88                    kMinVp9SpatialLayerHeight << (num_spatial_layers - 1), 30,
89                    first_active_layer, num_spatial_layers, 1, false);
90 
91   EXPECT_EQ(spatial_layers.size(), num_spatial_layers);
92 
93   for (const SpatialLayer& layer : spatial_layers) {
94     EXPECT_LE(layer.minBitrate, layer.maxBitrate);
95     EXPECT_LE(layer.minBitrate, layer.targetBitrate);
96     EXPECT_LE(layer.targetBitrate, layer.maxBitrate);
97   }
98 }
99 
TEST(SvcConfig,ScreenSharing)100 TEST(SvcConfig, ScreenSharing) {
101   std::vector<SpatialLayer> spatial_layers =
102       GetSvcConfig(1920, 1080, 30, 1, 3, 3, true);
103 
104   EXPECT_EQ(spatial_layers.size(), 3UL);
105 
106   for (size_t i = 0; i < 3; ++i) {
107     const SpatialLayer& layer = spatial_layers[i];
108     EXPECT_EQ(layer.width, 1920);
109     EXPECT_EQ(layer.height, 1080);
110     EXPECT_EQ(layer.maxFramerate, (i < 1) ? 5 : (i < 2 ? 10 : 30));
111     EXPECT_EQ(layer.numberOfTemporalLayers, 1);
112     EXPECT_LE(layer.minBitrate, layer.maxBitrate);
113     EXPECT_LE(layer.minBitrate, layer.targetBitrate);
114     EXPECT_LE(layer.targetBitrate, layer.maxBitrate);
115   }
116 }
117 }  // namespace webrtc
118