• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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 // decode targets: S0T0, S0T1, S1T0, S1T1
28 constexpr DecodeTargetIndication kDtis[6][4] = {
29     {kSwitch, kSwitch, kSwitch, kSwitch},                   //    kKey, S0
30     {kNotPresent, kNotPresent, kSwitch, kSwitch},           //    kKey, S1
31     {kNotPresent, kDiscardable, kNotPresent, kNotPresent},  //    kDeltaT1, S0
32     {kNotPresent, kNotPresent, kNotPresent, kDiscardable},  //    kDeltaT1, S1
33     {kSwitch, kSwitch, kNotPresent, kNotPresent},           //    kDeltaT0, S0
34     {kNotPresent, kNotPresent, kSwitch, kSwitch},           //    kDeltaT0, S1
35 };
36 
37 }  // namespace
38 
39 ScalabilityStructureL2T2Key::~ScalabilityStructureL2T2Key() = default;
40 
41 ScalableVideoController::StreamLayersConfig
StreamConfig() const42 ScalabilityStructureL2T2Key::StreamConfig() const {
43   StreamLayersConfig result;
44   result.num_spatial_layers = 2;
45   result.num_temporal_layers = 2;
46   result.scaling_factor_num[0] = 1;
47   result.scaling_factor_den[0] = 2;
48   return result;
49 }
50 
DependencyStructure() const51 FrameDependencyStructure ScalabilityStructureL2T2Key::DependencyStructure()
52     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("SS--").ChainDiffs({4, 3}).FrameDiffs({4});
61   templates[2].S(0).T(1).Dtis("-D--").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, 4}).FrameDiffs({4});
64   templates[5].S(1).T(1).Dtis("---D").ChainDiffs({3, 2}).FrameDiffs({2});
65   return structure;
66 }
67 
68 ScalableVideoController::LayerFrameConfig
KeyFrameConfig() const69 ScalabilityStructureL2T2Key::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 ScalabilityStructureL2T2Key::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_ = kDeltaT1;
87       break;
88     case kDeltaT1:
89       result[0].Id(2).S(0).T(1).Reference(0);
90       result[1].Id(3).S(1).T(1).Reference(1);
91       next_pattern_ = kDeltaT0;
92       break;
93     case kDeltaT0:
94       result[0].Id(4).S(0).T(0).ReferenceAndUpdate(0);
95       result[1].Id(5).S(1).T(0).ReferenceAndUpdate(1);
96       next_pattern_ = kDeltaT1;
97       break;
98   }
99   return result;
100 }
101 
OnEncodeDone(LayerFrameConfig config)102 absl::optional<GenericFrameInfo> ScalabilityStructureL2T2Key::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