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_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 kSwitch = DecodeTargetIndication::kSwitch;
25
26 constexpr DecodeTargetIndication kDtis[3][2] = {
27 {kSwitch, kSwitch}, // Key, S0
28 {kSwitch, kNotPresent}, // Delta, S0
29 {kNotPresent, kSwitch}, // Key and Delta, S1
30 };
31
32 } // namespace
33
34 ScalabilityStructureL2T1Key::~ScalabilityStructureL2T1Key() = default;
35
36 ScalableVideoController::StreamLayersConfig
StreamConfig() const37 ScalabilityStructureL2T1Key::StreamConfig() const {
38 StreamLayersConfig result;
39 result.num_spatial_layers = 2;
40 result.num_temporal_layers = 1;
41 result.scaling_factor_num[0] = 1;
42 result.scaling_factor_den[0] = 2;
43 return result;
44 }
45
DependencyStructure() const46 FrameDependencyStructure ScalabilityStructureL2T1Key::DependencyStructure()
47 const {
48 FrameDependencyStructure structure;
49 structure.num_decode_targets = 2;
50 structure.num_chains = 2;
51 structure.decode_target_protected_by_chain = {0, 1};
52 structure.templates.resize(4);
53 structure.templates[0].S(0).Dtis("S-").ChainDiffs({2, 1}).FrameDiffs({2});
54 structure.templates[1].S(0).Dtis("SS").ChainDiffs({0, 0});
55 structure.templates[2].S(1).Dtis("-S").ChainDiffs({1, 2}).FrameDiffs({2});
56 structure.templates[3].S(1).Dtis("-S").ChainDiffs({1, 1}).FrameDiffs({1});
57 return structure;
58 }
59
60 ScalableVideoController::LayerFrameConfig
KeyFrameConfig() const61 ScalabilityStructureL2T1Key::KeyFrameConfig() const {
62 return LayerFrameConfig().Id(0).S(0).Keyframe().Update(0);
63 }
64
65 std::vector<ScalableVideoController::LayerFrameConfig>
NextFrameConfig(bool restart)66 ScalabilityStructureL2T1Key::NextFrameConfig(bool restart) {
67 std::vector<LayerFrameConfig> result(2);
68
69 // Buffer0 keeps latest S0T0 frame, Buffer1 keeps latest S1T0 frame.
70 if (restart || keyframe_) {
71 result[0] = KeyFrameConfig();
72 result[1].Id(2).S(1).Reference(0).Update(1);
73 keyframe_ = false;
74 } else {
75 result[0].Id(1).S(0).ReferenceAndUpdate(0);
76 result[1].Id(2).S(1).ReferenceAndUpdate(1);
77 }
78 return result;
79 }
80
OnEncodeDone(LayerFrameConfig config)81 absl::optional<GenericFrameInfo> ScalabilityStructureL2T1Key::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 if (config.IsKeyframe()) {
99 frame_info->part_of_chain = {true, true};
100 } else {
101 frame_info->part_of_chain = {config.SpatialId() == 0,
102 config.SpatialId() == 1};
103 }
104 return frame_info;
105 }
106
107 } // namespace webrtc
108