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_key_shift.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 
27 constexpr DecodeTargetIndication kDtis[6][4] = {
28     {kSwitch, kSwitch, kSwitch, kSwitch},                   //    kKey, S0T0
29     {kNotPresent, kNotPresent, kSwitch, kSwitch},           //    kKey, S1T0
30     {kSwitch, kSwitch, kNotPresent, kNotPresent},           //    kDelta0, S0T0
31     {kNotPresent, kNotPresent, kNotPresent, kDiscardable},  //    kDelta0, S1T1
32     {kNotPresent, kDiscardable, kNotPresent, kNotPresent},  //    kDelta1, S0T1
33     {kNotPresent, kNotPresent, kSwitch, kSwitch},           //    kDelta1, S1T0
34 };
35 
36 }  // namespace
37 
38 ScalabilityStructureL2T2KeyShift::~ScalabilityStructureL2T2KeyShift() = default;
39 
40 ScalableVideoController::StreamLayersConfig
StreamConfig() const41 ScalabilityStructureL2T2KeyShift::StreamConfig() const {
42   StreamLayersConfig result;
43   result.num_spatial_layers = 2;
44   result.num_temporal_layers = 2;
45   result.scaling_factor_num[0] = 1;
46   result.scaling_factor_den[0] = 2;
47   return result;
48 }
49 
DependencyStructure() const50 FrameDependencyStructure ScalabilityStructureL2T2KeyShift::DependencyStructure()
51     const {
52   FrameDependencyStructure structure;
53   structure.num_decode_targets = 4;
54   structure.num_chains = 2;
55   structure.decode_target_protected_by_chain = {0, 0, 1, 1};
56   structure.templates.resize(7);
57   auto& templates = structure.templates;
58   templates[0].S(0).T(0).Dtis("SSSS").ChainDiffs({0, 0});
59   templates[1].S(0).T(0).Dtis("SS--").ChainDiffs({2, 1}).FrameDiffs({2});
60   templates[2].S(0).T(0).Dtis("SS--").ChainDiffs({4, 1}).FrameDiffs({4});
61   templates[3].S(0).T(1).Dtis("-D--").ChainDiffs({2, 3}).FrameDiffs({2});
62   templates[4].S(1).T(0).Dtis("--SS").ChainDiffs({1, 1}).FrameDiffs({1});
63   templates[5].S(1).T(0).Dtis("--SS").ChainDiffs({3, 4}).FrameDiffs({4});
64   templates[6].S(1).T(1).Dtis("---D").ChainDiffs({1, 2}).FrameDiffs({2});
65   return structure;
66 }
67 
68 ScalableVideoController::LayerFrameConfig
KeyFrameConfig() const69 ScalabilityStructureL2T2KeyShift::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 ScalabilityStructureL2T2KeyShift::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   switch (next_pattern_) {
83     case kKey:
84       result[0] = KeyFrameConfig();
85       result[1].Id(1).S(1).T(0).Reference(0).Update(1);
86       next_pattern_ = kDelta0;
87       break;
88     case kDelta0:
89       result[0].Id(2).S(0).T(0).ReferenceAndUpdate(0);
90       result[1].Id(3).S(1).T(1).Reference(1);
91       next_pattern_ = kDelta1;
92       break;
93     case kDelta1:
94       result[0].Id(4).S(0).T(1).Reference(0);
95       result[1].Id(5).S(1).T(0).ReferenceAndUpdate(1);
96       next_pattern_ = kDelta0;
97       break;
98   }
99   return result;
100 }
101 
OnEncodeDone(LayerFrameConfig config)102 absl::optional<GenericFrameInfo> ScalabilityStructureL2T2KeyShift::OnEncodeDone(
103     LayerFrameConfig config) {
104   if (config.IsKeyframe()) {
105     config = KeyFrameConfig();
106   }
107 
108   absl::optional<GenericFrameInfo> frame_info;
109   if (config.Id() < 0 || config.Id() >= int{ABSL_ARRAYSIZE(kDtis)}) {
110     RTC_LOG(LS_ERROR) << "Unexpected config id " << config.Id();
111     return frame_info;
112   }
113   frame_info.emplace();
114   frame_info->spatial_id = config.SpatialId();
115   frame_info->temporal_id = config.TemporalId();
116   frame_info->encoder_buffers = config.Buffers();
117   frame_info->decode_target_indications.assign(std::begin(kDtis[config.Id()]),
118                                                std::end(kDtis[config.Id()]));
119   if (config.IsKeyframe()) {
120     frame_info->part_of_chain = {true, true};
121   } else if (config.TemporalId() == 0) {
122     frame_info->part_of_chain = {config.SpatialId() == 0,
123                                  config.SpatialId() == 1};
124   } else {
125     frame_info->part_of_chain = {false, false};
126   }
127   return frame_info;
128 }
129 
130 }  // namespace webrtc
131