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_l2t1.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 kSwitch = DecodeTargetIndication::kSwitch;
25 constexpr auto kRequired = DecodeTargetIndication::kRequired;
26 
27 constexpr DecodeTargetIndication kDtis[4][2] = {
28     {kSwitch, kSwitch},        // Key, S0
29     {kNotPresent, kSwitch},    // Key, S1
30     {kSwitch, kRequired},      // Delta, S0
31     {kNotPresent, kRequired},  // Delta, S1
32 };
33 
34 }  // namespace
35 
36 ScalabilityStructureL2T1::~ScalabilityStructureL2T1() = default;
37 
38 ScalableVideoController::StreamLayersConfig
StreamConfig() const39 ScalabilityStructureL2T1::StreamConfig() const {
40   StreamLayersConfig result;
41   result.num_spatial_layers = 2;
42   result.num_temporal_layers = 1;
43   result.scaling_factor_num[0] = 1;
44   result.scaling_factor_den[0] = 2;
45   return result;
46 }
47 
DependencyStructure() const48 FrameDependencyStructure ScalabilityStructureL2T1::DependencyStructure() const {
49   FrameDependencyStructure structure;
50   structure.num_decode_targets = 2;
51   structure.num_chains = 2;
52   structure.decode_target_protected_by_chain = {0, 1};
53   structure.templates.resize(4);
54   structure.templates[0].S(0).Dtis("SR").ChainDiffs({2, 1}).FrameDiffs({2});
55   structure.templates[1].S(0).Dtis("SS").ChainDiffs({0, 0});
56   structure.templates[2].S(1).Dtis("-R").ChainDiffs({1, 1}).FrameDiffs({2, 1});
57   structure.templates[3].S(1).Dtis("-S").ChainDiffs({1, 1}).FrameDiffs({1});
58   return structure;
59 }
60 
61 ScalableVideoController::LayerFrameConfig
KeyFrameConfig() const62 ScalabilityStructureL2T1::KeyFrameConfig() const {
63   return LayerFrameConfig().Id(0).S(0).Keyframe().Update(0);
64 }
65 
66 std::vector<ScalableVideoController::LayerFrameConfig>
NextFrameConfig(bool restart)67 ScalabilityStructureL2T1::NextFrameConfig(bool restart) {
68   std::vector<LayerFrameConfig> result(2);
69   // Buffer0 keeps latest S0 frame, Buffer1 keeps latest S1 frame.
70   if (restart || keyframe_) {
71     result[0] = KeyFrameConfig();
72     result[1].Id(1).S(1).Reference(0).Update(1);
73     keyframe_ = false;
74   } else {
75     result[0].Id(2).S(0).ReferenceAndUpdate(0);
76     result[1].Id(3).S(1).Reference(0).ReferenceAndUpdate(1);
77   }
78   return result;
79 }
80 
OnEncodeDone(LayerFrameConfig config)81 absl::optional<GenericFrameInfo> ScalabilityStructureL2T1::OnEncodeDone(
82     LayerFrameConfig config) {
83   absl::optional<GenericFrameInfo> frame_info;
84   if (config.IsKeyframe()) {
85     config = KeyFrameConfig();
86   }
87 
88   if (config.Id() < 0 || config.Id() >= int{ABSL_ARRAYSIZE(kDtis)}) {
89     RTC_LOG(LS_ERROR) << "Unexpected config id " << config.Id();
90     return frame_info;
91   }
92   frame_info.emplace();
93   frame_info->spatial_id = config.SpatialId();
94   frame_info->temporal_id = config.TemporalId();
95   frame_info->encoder_buffers = std::move(config.Buffers());
96   frame_info->decode_target_indications.assign(std::begin(kDtis[config.Id()]),
97                                                std::end(kDtis[config.Id()]));
98   frame_info->part_of_chain = {config.SpatialId() == 0, true};
99   return frame_info;
100 }
101 
102 }  // namespace webrtc
103