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