1 /*
2  *  Copyright (c) 2020 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 #include "modules/video_coding/codecs/av1/scalability_structure_l2t2.h"
11 
12 #include <utility>
13 #include <vector>
14 
15 #include "absl/base/macros.h"
16 #include "api/transport/rtp/dependency_descriptor.h"
17 #include "rtc_base/checks.h"
18 #include "rtc_base/logging.h"
19 
20 namespace webrtc {
21 namespace {
22 
23 constexpr auto kNotPresent = DecodeTargetIndication::kNotPresent;
24 constexpr auto kDiscardable = DecodeTargetIndication::kDiscardable;
25 constexpr auto kSwitch = DecodeTargetIndication::kSwitch;
26 constexpr auto kRequired = DecodeTargetIndication::kRequired;
27 
28 // decode targets: S0T0, S0T1, S1T0, S1T1
29 constexpr DecodeTargetIndication kDtis[6][4] = {
30     {kSwitch, kSwitch, kSwitch, kSwitch},                   // kKey, S0
31     {kNotPresent, kNotPresent, kSwitch, kSwitch},           // kKey, S1
32     {kNotPresent, kDiscardable, kNotPresent, kRequired},    // kDeltaT1, S0
33     {kNotPresent, kNotPresent, kNotPresent, kDiscardable},  // kDeltaT1, S1
34     {kSwitch, kSwitch, kRequired, kRequired},               // kDeltaT0, S0
35     {kNotPresent, kNotPresent, kSwitch, kSwitch},           // kDeltaT0, S1
36 };
37 
38 }  // namespace
39 
40 ScalabilityStructureL2T2::~ScalabilityStructureL2T2() = default;
41 
42 ScalableVideoController::StreamLayersConfig
StreamConfig() const43 ScalabilityStructureL2T2::StreamConfig() const {
44   StreamLayersConfig result;
45   result.num_spatial_layers = 2;
46   result.num_temporal_layers = 2;
47   result.scaling_factor_num[0] = 1;
48   result.scaling_factor_den[0] = 2;
49   return result;
50 }
51 
DependencyStructure() const52 FrameDependencyStructure ScalabilityStructureL2T2::DependencyStructure() const {
53   FrameDependencyStructure structure;
54   structure.num_decode_targets = 4;
55   structure.num_chains = 2;
56   structure.decode_target_protected_by_chain = {0, 0, 1, 1};
57   structure.templates.resize(6);
58   auto& templates = structure.templates;
59   templates[0].S(0).T(0).Dtis("SSSS").ChainDiffs({0, 0});
60   templates[1].S(0).T(0).Dtis("SSRR").ChainDiffs({4, 3}).FrameDiffs({4});
61   templates[2].S(0).T(1).Dtis("-D-R").ChainDiffs({2, 1}).FrameDiffs({2});
62   templates[3].S(1).T(0).Dtis("--SS").ChainDiffs({1, 1}).FrameDiffs({1});
63   templates[4].S(1).T(0).Dtis("--SS").ChainDiffs({1, 1}).FrameDiffs({4, 1});
64   templates[5].S(1).T(1).Dtis("---D").ChainDiffs({3, 2}).FrameDiffs({2, 1});
65   return structure;
66 }
67 
68 ScalableVideoController::LayerFrameConfig
KeyFrameConfig() const69 ScalabilityStructureL2T2::KeyFrameConfig() const {
70   return LayerFrameConfig().Id(0).Keyframe().S(0).T(0).Update(0);
71 }
72 
73 std::vector<ScalableVideoController::LayerFrameConfig>
NextFrameConfig(bool restart)74 ScalabilityStructureL2T2::NextFrameConfig(bool restart) {
75   if (restart) {
76     next_pattern_ = kKey;
77   }
78   std::vector<LayerFrameConfig> result(2);
79 
80   // Buffer0 keeps latest S0T0 frame,
81   // Buffer1 keeps latest S1T0 frame.
82   // Buffer2 keeps latest S0T1 frame.
83   switch (next_pattern_) {
84     case kKey:
85       result[0] = KeyFrameConfig();
86       result[1].Id(1).S(1).T(0).Reference(0).Update(1);
87       next_pattern_ = kDeltaT1;
88       break;
89     case kDeltaT1:
90       result[0].Id(2).S(0).T(1).Reference(0).Update(2);
91       result[1].Id(3).S(1).T(1).Reference(2).Reference(1);
92       next_pattern_ = kDeltaT0;
93       break;
94     case kDeltaT0:
95       result[0].Id(4).S(0).T(0).ReferenceAndUpdate(0);
96       result[1].Id(5).S(1).T(0).Reference(0).ReferenceAndUpdate(1);
97       next_pattern_ = kDeltaT1;
98       break;
99   }
100   return result;
101 }
102 
OnEncodeDone(LayerFrameConfig config)103 absl::optional<GenericFrameInfo> ScalabilityStructureL2T2::OnEncodeDone(
104     LayerFrameConfig config) {
105   if (config.IsKeyframe()) {
106     config = KeyFrameConfig();
107   }
108 
109   absl::optional<GenericFrameInfo> frame_info;
110   if (config.Id() < 0 || config.Id() >= int{ABSL_ARRAYSIZE(kDtis)}) {
111     RTC_LOG(LS_ERROR) << "Unexpected config id " << config.Id();
112     return frame_info;
113   }
114   frame_info.emplace();
115   frame_info->spatial_id = config.SpatialId();
116   frame_info->temporal_id = config.TemporalId();
117   frame_info->encoder_buffers = config.Buffers();
118   frame_info->decode_target_indications.assign(std::begin(kDtis[config.Id()]),
119                                                std::end(kDtis[config.Id()]));
120   if (config.TemporalId() == 0) {
121     frame_info->part_of_chain = {config.SpatialId() == 0, true};
122   } else {
123     frame_info->part_of_chain = {false, false};
124   }
125   return frame_info;
126 }
127 
128 }  // namespace webrtc
129