• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2022 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/svc/scalability_mode_util.h"
12 
13 #include "absl/strings/string_view.h"
14 #include "absl/types/optional.h"
15 #include "api/video_codecs/scalability_mode.h"
16 #include "rtc_base/checks.h"
17 
18 namespace webrtc {
19 
ScalabilityModeFromString(absl::string_view mode_string)20 absl::optional<ScalabilityMode> ScalabilityModeFromString(
21     absl::string_view mode_string) {
22   if (mode_string == "L1T1")
23     return ScalabilityMode::kL1T1;
24   if (mode_string == "L1T2")
25     return ScalabilityMode::kL1T2;
26   if (mode_string == "L1T3")
27     return ScalabilityMode::kL1T3;
28 
29   if (mode_string == "L2T1")
30     return ScalabilityMode::kL2T1;
31   if (mode_string == "L2T1h")
32     return ScalabilityMode::kL2T1h;
33   if (mode_string == "L2T1_KEY")
34     return ScalabilityMode::kL2T1_KEY;
35 
36   if (mode_string == "L2T2")
37     return ScalabilityMode::kL2T2;
38   if (mode_string == "L2T2h")
39     return ScalabilityMode::kL2T2h;
40   if (mode_string == "L2T2_KEY")
41     return ScalabilityMode::kL2T2_KEY;
42   if (mode_string == "L2T2_KEY_SHIFT")
43     return ScalabilityMode::kL2T2_KEY_SHIFT;
44   if (mode_string == "L2T3")
45     return ScalabilityMode::kL2T3;
46   if (mode_string == "L2T3h")
47     return ScalabilityMode::kL2T3h;
48   if (mode_string == "L2T3_KEY")
49     return ScalabilityMode::kL2T3_KEY;
50 
51   if (mode_string == "L3T1")
52     return ScalabilityMode::kL3T1;
53   if (mode_string == "L3T1h")
54     return ScalabilityMode::kL3T1h;
55   if (mode_string == "L3T1_KEY")
56     return ScalabilityMode::kL3T1_KEY;
57 
58   if (mode_string == "L3T2")
59     return ScalabilityMode::kL3T2;
60   if (mode_string == "L3T2h")
61     return ScalabilityMode::kL3T2h;
62   if (mode_string == "L3T2_KEY")
63     return ScalabilityMode::kL3T2_KEY;
64 
65   if (mode_string == "L3T3")
66     return ScalabilityMode::kL3T3;
67   if (mode_string == "L3T3h")
68     return ScalabilityMode::kL3T3h;
69   if (mode_string == "L3T3_KEY")
70     return ScalabilityMode::kL3T3_KEY;
71 
72   if (mode_string == "S2T1")
73     return ScalabilityMode::kS2T1;
74   if (mode_string == "S2T1h")
75     return ScalabilityMode::kS2T1h;
76   if (mode_string == "S2T2")
77     return ScalabilityMode::kS2T2;
78   if (mode_string == "S2T2h")
79     return ScalabilityMode::kS2T2h;
80   if (mode_string == "S2T3")
81     return ScalabilityMode::kS2T3;
82   if (mode_string == "S2T3h")
83     return ScalabilityMode::kS2T3h;
84   if (mode_string == "S3T1")
85     return ScalabilityMode::kS3T1;
86   if (mode_string == "S3T1h")
87     return ScalabilityMode::kS3T1h;
88   if (mode_string == "S3T2")
89     return ScalabilityMode::kS3T2;
90   if (mode_string == "S3T2h")
91     return ScalabilityMode::kS3T2h;
92   if (mode_string == "S3T3")
93     return ScalabilityMode::kS3T3;
94   if (mode_string == "S3T3h")
95     return ScalabilityMode::kS3T3h;
96 
97   return absl::nullopt;
98 }
99 
ScalabilityModeToInterLayerPredMode(ScalabilityMode scalability_mode)100 InterLayerPredMode ScalabilityModeToInterLayerPredMode(
101     ScalabilityMode scalability_mode) {
102   switch (scalability_mode) {
103     case ScalabilityMode::kL1T1:
104     case ScalabilityMode::kL1T2:
105     case ScalabilityMode::kL1T3:
106     case ScalabilityMode::kL2T1:
107     case ScalabilityMode::kL2T1h:
108       return InterLayerPredMode::kOn;
109     case ScalabilityMode::kL2T1_KEY:
110       return InterLayerPredMode::kOnKeyPic;
111     case ScalabilityMode::kL2T2:
112     case ScalabilityMode::kL2T2h:
113       return InterLayerPredMode::kOn;
114     case ScalabilityMode::kL2T2_KEY:
115     case ScalabilityMode::kL2T2_KEY_SHIFT:
116       return InterLayerPredMode::kOnKeyPic;
117     case ScalabilityMode::kL2T3:
118     case ScalabilityMode::kL2T3h:
119       return InterLayerPredMode::kOn;
120     case ScalabilityMode::kL2T3_KEY:
121       return InterLayerPredMode::kOnKeyPic;
122     case ScalabilityMode::kL3T1:
123     case ScalabilityMode::kL3T1h:
124       return InterLayerPredMode::kOn;
125     case ScalabilityMode::kL3T1_KEY:
126       return InterLayerPredMode::kOnKeyPic;
127     case ScalabilityMode::kL3T2:
128     case ScalabilityMode::kL3T2h:
129       return InterLayerPredMode::kOn;
130     case ScalabilityMode::kL3T2_KEY:
131       return InterLayerPredMode::kOnKeyPic;
132     case ScalabilityMode::kL3T3:
133     case ScalabilityMode::kL3T3h:
134       return InterLayerPredMode::kOn;
135     case ScalabilityMode::kL3T3_KEY:
136       return InterLayerPredMode::kOnKeyPic;
137     case ScalabilityMode::kS2T1:
138     case ScalabilityMode::kS2T1h:
139     case ScalabilityMode::kS2T2:
140     case ScalabilityMode::kS2T2h:
141     case ScalabilityMode::kS2T3:
142     case ScalabilityMode::kS2T3h:
143     case ScalabilityMode::kS3T1:
144     case ScalabilityMode::kS3T1h:
145     case ScalabilityMode::kS3T2:
146     case ScalabilityMode::kS3T2h:
147     case ScalabilityMode::kS3T3:
148     case ScalabilityMode::kS3T3h:
149       return InterLayerPredMode::kOff;
150   }
151   RTC_CHECK_NOTREACHED();
152 }
153 
ScalabilityModeToNumSpatialLayers(ScalabilityMode scalability_mode)154 int ScalabilityModeToNumSpatialLayers(ScalabilityMode scalability_mode) {
155   switch (scalability_mode) {
156     case ScalabilityMode::kL1T1:
157     case ScalabilityMode::kL1T2:
158     case ScalabilityMode::kL1T3:
159       return 1;
160     case ScalabilityMode::kL2T1:
161     case ScalabilityMode::kL2T1h:
162     case ScalabilityMode::kL2T1_KEY:
163     case ScalabilityMode::kL2T2:
164     case ScalabilityMode::kL2T2h:
165     case ScalabilityMode::kL2T2_KEY:
166     case ScalabilityMode::kL2T2_KEY_SHIFT:
167     case ScalabilityMode::kL2T3:
168     case ScalabilityMode::kL2T3h:
169     case ScalabilityMode::kL2T3_KEY:
170       return 2;
171     case ScalabilityMode::kL3T1:
172     case ScalabilityMode::kL3T1h:
173     case ScalabilityMode::kL3T1_KEY:
174     case ScalabilityMode::kL3T2:
175     case ScalabilityMode::kL3T2h:
176     case ScalabilityMode::kL3T2_KEY:
177     case ScalabilityMode::kL3T3:
178     case ScalabilityMode::kL3T3h:
179     case ScalabilityMode::kL3T3_KEY:
180       return 3;
181     case ScalabilityMode::kS2T1:
182     case ScalabilityMode::kS2T1h:
183     case ScalabilityMode::kS2T2:
184     case ScalabilityMode::kS2T2h:
185     case ScalabilityMode::kS2T3:
186     case ScalabilityMode::kS2T3h:
187       return 2;
188     case ScalabilityMode::kS3T1:
189     case ScalabilityMode::kS3T1h:
190     case ScalabilityMode::kS3T2:
191     case ScalabilityMode::kS3T2h:
192     case ScalabilityMode::kS3T3:
193     case ScalabilityMode::kS3T3h:
194       return 3;
195   }
196   RTC_CHECK_NOTREACHED();
197 }
198 
ScalabilityModeToNumTemporalLayers(ScalabilityMode scalability_mode)199 int ScalabilityModeToNumTemporalLayers(ScalabilityMode scalability_mode) {
200   switch (scalability_mode) {
201     case ScalabilityMode::kL1T1:
202       return 1;
203     case ScalabilityMode::kL1T2:
204       return 2;
205     case ScalabilityMode::kL1T3:
206       return 3;
207     case ScalabilityMode::kL2T1:
208     case ScalabilityMode::kL2T1h:
209     case ScalabilityMode::kL2T1_KEY:
210       return 1;
211     case ScalabilityMode::kL2T2:
212     case ScalabilityMode::kL2T2h:
213     case ScalabilityMode::kL2T2_KEY:
214     case ScalabilityMode::kL2T2_KEY_SHIFT:
215       return 2;
216     case ScalabilityMode::kL2T3:
217     case ScalabilityMode::kL2T3h:
218     case ScalabilityMode::kL2T3_KEY:
219       return 3;
220     case ScalabilityMode::kL3T1:
221     case ScalabilityMode::kL3T1h:
222     case ScalabilityMode::kL3T1_KEY:
223       return 1;
224     case ScalabilityMode::kL3T2:
225     case ScalabilityMode::kL3T2h:
226     case ScalabilityMode::kL3T2_KEY:
227       return 2;
228     case ScalabilityMode::kL3T3:
229     case ScalabilityMode::kL3T3h:
230     case ScalabilityMode::kL3T3_KEY:
231       return 3;
232     case ScalabilityMode::kS2T1:
233     case ScalabilityMode::kS2T1h:
234     case ScalabilityMode::kS3T1:
235     case ScalabilityMode::kS3T1h:
236       return 1;
237     case ScalabilityMode::kS2T2:
238     case ScalabilityMode::kS2T2h:
239     case ScalabilityMode::kS3T2:
240     case ScalabilityMode::kS3T2h:
241       return 2;
242     case ScalabilityMode::kS2T3:
243     case ScalabilityMode::kS2T3h:
244     case ScalabilityMode::kS3T3:
245     case ScalabilityMode::kS3T3h:
246       return 3;
247   }
248   RTC_CHECK_NOTREACHED();
249 }
250 
ScalabilityModeToResolutionRatio(ScalabilityMode scalability_mode)251 absl::optional<ScalabilityModeResolutionRatio> ScalabilityModeToResolutionRatio(
252     ScalabilityMode scalability_mode) {
253   switch (scalability_mode) {
254     case ScalabilityMode::kL1T1:
255     case ScalabilityMode::kL1T2:
256     case ScalabilityMode::kL1T3:
257       return absl::nullopt;
258     case ScalabilityMode::kL2T1:
259     case ScalabilityMode::kL2T1_KEY:
260     case ScalabilityMode::kL2T2:
261     case ScalabilityMode::kL2T2_KEY:
262     case ScalabilityMode::kL2T2_KEY_SHIFT:
263     case ScalabilityMode::kL2T3:
264     case ScalabilityMode::kL2T3_KEY:
265     case ScalabilityMode::kL3T1:
266     case ScalabilityMode::kL3T1_KEY:
267     case ScalabilityMode::kL3T2:
268     case ScalabilityMode::kL3T2_KEY:
269     case ScalabilityMode::kL3T3:
270     case ScalabilityMode::kL3T3_KEY:
271     case ScalabilityMode::kS2T1:
272     case ScalabilityMode::kS2T2:
273     case ScalabilityMode::kS2T3:
274     case ScalabilityMode::kS3T1:
275     case ScalabilityMode::kS3T2:
276     case ScalabilityMode::kS3T3:
277       return ScalabilityModeResolutionRatio::kTwoToOne;
278     case ScalabilityMode::kL2T1h:
279     case ScalabilityMode::kL2T2h:
280     case ScalabilityMode::kL2T3h:
281     case ScalabilityMode::kL3T1h:
282     case ScalabilityMode::kL3T2h:
283     case ScalabilityMode::kL3T3h:
284     case ScalabilityMode::kS2T1h:
285     case ScalabilityMode::kS2T2h:
286     case ScalabilityMode::kS2T3h:
287     case ScalabilityMode::kS3T1h:
288     case ScalabilityMode::kS3T2h:
289     case ScalabilityMode::kS3T3h:
290       return ScalabilityModeResolutionRatio::kThreeToTwo;
291   }
292   RTC_CHECK_NOTREACHED();
293 }
294 
LimitNumSpatialLayers(ScalabilityMode scalability_mode,int max_spatial_layers)295 ScalabilityMode LimitNumSpatialLayers(ScalabilityMode scalability_mode,
296                                       int max_spatial_layers) {
297   int num_spatial_layers = ScalabilityModeToNumSpatialLayers(scalability_mode);
298   if (max_spatial_layers >= num_spatial_layers) {
299     return scalability_mode;
300   }
301 
302   switch (scalability_mode) {
303     case ScalabilityMode::kL1T1:
304       return ScalabilityMode::kL1T1;
305     case ScalabilityMode::kL1T2:
306       return ScalabilityMode::kL1T2;
307     case ScalabilityMode::kL1T3:
308       return ScalabilityMode::kL1T3;
309     case ScalabilityMode::kL2T1:
310       return ScalabilityMode::kL1T1;
311     case ScalabilityMode::kL2T1h:
312       return ScalabilityMode::kL1T1;
313     case ScalabilityMode::kL2T1_KEY:
314       return ScalabilityMode::kL1T1;
315     case ScalabilityMode::kL2T2:
316       return ScalabilityMode::kL1T2;
317     case ScalabilityMode::kL2T2h:
318       return ScalabilityMode::kL1T2;
319     case ScalabilityMode::kL2T2_KEY:
320       return ScalabilityMode::kL1T2;
321     case ScalabilityMode::kL2T2_KEY_SHIFT:
322       return ScalabilityMode::kL1T2;
323     case ScalabilityMode::kL2T3:
324       return ScalabilityMode::kL1T3;
325     case ScalabilityMode::kL2T3h:
326       return ScalabilityMode::kL1T3;
327     case ScalabilityMode::kL2T3_KEY:
328       return ScalabilityMode::kL1T3;
329     case ScalabilityMode::kL3T1:
330       return max_spatial_layers == 2 ? ScalabilityMode::kL2T1
331                                      : ScalabilityMode::kL1T1;
332     case ScalabilityMode::kL3T1h:
333       return max_spatial_layers == 2 ? ScalabilityMode::kL2T1h
334                                      : ScalabilityMode::kL1T1;
335     case ScalabilityMode::kL3T1_KEY:
336       return max_spatial_layers == 2 ? ScalabilityMode::kL2T1_KEY
337                                      : ScalabilityMode::kL1T1;
338     case ScalabilityMode::kL3T2:
339       return max_spatial_layers == 2 ? ScalabilityMode::kL2T2
340                                      : ScalabilityMode::kL1T2;
341     case ScalabilityMode::kL3T2h:
342       return max_spatial_layers == 2 ? ScalabilityMode::kL2T2h
343                                      : ScalabilityMode::kL1T2;
344     case ScalabilityMode::kL3T2_KEY:
345       return max_spatial_layers == 2 ? ScalabilityMode::kL2T2_KEY
346                                      : ScalabilityMode::kL1T2;
347     case ScalabilityMode::kL3T3:
348       return max_spatial_layers == 2 ? ScalabilityMode::kL2T3
349                                      : ScalabilityMode::kL1T3;
350     case ScalabilityMode::kL3T3h:
351       return max_spatial_layers == 2 ? ScalabilityMode::kL2T3h
352                                      : ScalabilityMode::kL1T3;
353     case ScalabilityMode::kL3T3_KEY:
354       return max_spatial_layers == 2 ? ScalabilityMode::kL2T3_KEY
355                                      : ScalabilityMode::kL1T3;
356     case ScalabilityMode::kS2T1:
357       return ScalabilityMode::kL1T1;
358     case ScalabilityMode::kS2T1h:
359       return ScalabilityMode::kL1T1;
360     case ScalabilityMode::kS2T2:
361       return ScalabilityMode::kL1T2;
362     case ScalabilityMode::kS2T2h:
363       return ScalabilityMode::kL1T2;
364     case ScalabilityMode::kS2T3:
365       return ScalabilityMode::kL1T3;
366     case ScalabilityMode::kS2T3h:
367       return ScalabilityMode::kL1T3;
368     case ScalabilityMode::kS3T1:
369       return max_spatial_layers == 2 ? ScalabilityMode::kS2T1
370                                      : ScalabilityMode::kL1T1;
371     case ScalabilityMode::kS3T1h:
372       return max_spatial_layers == 2 ? ScalabilityMode::kS2T1h
373                                      : ScalabilityMode::kL1T1;
374     case ScalabilityMode::kS3T2:
375       return max_spatial_layers == 2 ? ScalabilityMode::kS2T2
376                                      : ScalabilityMode::kL1T2;
377     case ScalabilityMode::kS3T2h:
378       return max_spatial_layers == 2 ? ScalabilityMode::kS2T2h
379                                      : ScalabilityMode::kL1T2;
380     case ScalabilityMode::kS3T3:
381       return max_spatial_layers == 2 ? ScalabilityMode::kS2T3
382                                      : ScalabilityMode::kL1T3;
383     case ScalabilityMode::kS3T3h:
384       return max_spatial_layers == 2 ? ScalabilityMode::kS2T3h
385                                      : ScalabilityMode::kL1T3;
386   }
387   RTC_CHECK_NOTREACHED();
388 }
389 
390 }  // namespace webrtc
391