Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(511)

Side by Side Diff: webrtc/modules/video_coding/utility/simulcast_rate_allocator.cc

Issue 2434073003: Extract bitrate allocation of spatial/temporal layers out of codec impl. (Closed)
Patch Set: Updated tl listener registration. Fixed tests. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/modules/video_coding/utility/simulcast_rate_allocator.h" 11 #include "webrtc/modules/video_coding/utility/simulcast_rate_allocator.h"
12 12
13 #include <algorithm> 13 #include <algorithm>
14 #include <vector>
15
16 #include "webrtc/base/checks.h"
14 17
15 namespace webrtc { 18 namespace webrtc {
16 19
17 webrtc::SimulcastRateAllocator::SimulcastRateAllocator(const VideoCodec& codec) 20 SimulcastRateAllocator::SimulcastRateAllocator(const VideoCodec& codec)
18 : codec_(codec) {} 21 : codec_(codec) {}
19 22
20 std::vector<uint32_t> webrtc::SimulcastRateAllocator::GetAllocation( 23 void SimulcastRateAllocator::OnTemporalLayersCreated(int simulcast_id,
21 uint32_t bitrate_kbps) const { 24 TemporalLayers* layers) {
22 // Always allocate enough bitrate for the minimum bitrate of the first layer. 25 RTC_DCHECK(temporal_layers_.find(simulcast_id) == temporal_layers_.end());
23 // Suspending below min bitrate is controlled outside the codec implementation 26 temporal_layers_[simulcast_id] = layers;
24 // and is not overridden by this. 27 }
25 const uint32_t min_bitrate_bps = codec_.numberOfSimulcastStreams == 0
26 ? codec_.minBitrate
27 : codec_.simulcastStream[0].minBitrate;
28 uint32_t left_to_allocate = std::max(min_bitrate_bps, bitrate_kbps);
29 if (codec_.maxBitrate)
30 left_to_allocate = std::min(left_to_allocate, codec_.maxBitrate);
31 28
32 if (codec_.numberOfSimulcastStreams < 2) { 29 BitrateAllocation SimulcastRateAllocator::GetAllocation(
30 uint32_t total_bitrate_bps,
31 uint32_t framerate) {
32 uint32_t left_to_allocate = total_bitrate_bps;
33 if (codec_.maxBitrate && codec_.maxBitrate * 1000 < left_to_allocate)
34 left_to_allocate = codec_.maxBitrate * 1000;
35
36 BitrateAllocation allocated_bitrates_bps;
37 if (codec_.numberOfSimulcastStreams == 0) {
33 // No simulcast, just set the target as this has been capped already. 38 // No simulcast, just set the target as this has been capped already.
34 return std::vector<uint32_t>(1, left_to_allocate); 39 allocated_bitrates_bps.set_bitrate(
40 0, 0, std::max(codec_.minBitrate * 1000, left_to_allocate));
41 } else {
42 // Always allocate enough bitrate for the minimum bitrate of the first
43 // layer. Suspending below min bitrate is controlled outside the codec
44 // implementation and is not overridden by this.
45 left_to_allocate =
46 std::max(codec_.simulcastStream[0].minBitrate * 1000, left_to_allocate);
47
48 // Begin by allocating bitrate to simulcast streams, putting all bitrate in
49 // temporal layer 0. We'll then distribute this stream allocation is done.
50
51 // Allocate up to the target bitrate for each simulcast layer.
52 size_t layer = 0;
53 for (; layer < codec_.numberOfSimulcastStreams; ++layer) {
54 const SimulcastStream& stream = codec_.simulcastStream[layer];
55 if (left_to_allocate < stream.minBitrate * 1000)
56 break;
57 uint32_t allocation =
58 std::min(left_to_allocate, stream.targetBitrate * 1000);
59 allocated_bitrates_bps.set_bitrate(layer, 0, allocation);
60 left_to_allocate -= allocation;
61 }
62
63 // Next, try allocate remaining bitrate, up to max bitrate, in top stream.
64 // TODO(sprang): Allocate up to max bitrate for all layers once we have a
65 // better idea of possible performance implications.
66 if (left_to_allocate > 0) {
67 size_t active_layer = layer - 1;
68 const SimulcastStream& stream = codec_.simulcastStream[active_layer];
69 uint32_t bitrate_bps =
70 allocated_bitrates_bps.get_spatial_layer_sum(active_layer);
71 uint32_t allocation =
72 std::min(left_to_allocate, stream.maxBitrate * 1000 - bitrate_bps);
73 bitrate_bps += allocation;
74 left_to_allocate -= allocation;
75 allocated_bitrates_bps.set_bitrate(active_layer, 0, bitrate_bps);
76 }
35 } 77 }
36 78
37 // Initialize bitrates with zeroes. 79 const int num_spatial_streams =
38 std::vector<uint32_t> allocated_bitrates_bps(codec_.numberOfSimulcastStreams, 80 std::max(1, static_cast<int>(codec_.numberOfSimulcastStreams));
39 0);
40 81
41 // First try to allocate up to the target bitrate for each substream. 82 // Finally, distribute the bitrate for the simulcast streams across the
42 size_t layer = 0; 83 // available temporal layers.
43 for (; layer < codec_.numberOfSimulcastStreams; ++layer) { 84 for (int simulcast_id = 0; simulcast_id < num_spatial_streams;
44 const SimulcastStream& stream = codec_.simulcastStream[layer]; 85 ++simulcast_id) {
45 if (left_to_allocate < stream.minBitrate) 86 auto tl_it = temporal_layers_.find(simulcast_id);
46 break; 87 if (tl_it == temporal_layers_.end())
47 uint32_t allocation = std::min(left_to_allocate, stream.targetBitrate); 88 continue; // TODO(sprang): If > 1 SS, assume default TL alloc?
48 allocated_bitrates_bps[layer] = allocation;
49 left_to_allocate -= allocation;
50 }
51 89
52 // Next, try allocate remaining bitrate, up to max bitrate, in top layer. 90 uint32_t target_bitrate_kbps =
53 // TODO(sprang): Allocate up to max bitrate for all layers once we have a 91 allocated_bitrates_bps.get_bitrate(simulcast_id, 0) / 1000;
54 // better idea of possible performance implications. 92 RTC_DCHECK_EQ(
55 if (left_to_allocate > 0) { 93 target_bitrate_kbps,
56 size_t active_layer = layer - 1; 94 allocated_bitrates_bps.get_spatial_layer_sum(simulcast_id) / 1000);
57 const SimulcastStream& stream = codec_.simulcastStream[active_layer]; 95 uint32_t max_bitrate_kbps;
58 uint32_t allocation = 96 if (codec_.numberOfSimulcastStreams == 0) {
59 std::min(left_to_allocate, 97 max_bitrate_kbps = codec_.maxBitrate;
60 stream.maxBitrate - allocated_bitrates_bps[active_layer]); 98
61 left_to_allocate -= allocation; 99 // TODO(holmer): This is a temporary hack for screensharing, where we
62 allocated_bitrates_bps[active_layer] += allocation; 100 // interpret the startBitrate as the encoder target bitrate. This is
101 // to allow for a different max bitrate, so if the codec can't meet
102 // the target we still allow it to overshoot up to the max before dropping
103 // frames. This hack should be improved.
104 if (codec_.mode == kScreensharing && codec_.targetBitrate > 0 &&
105 (codec_.codecSpecific.VP8.numberOfTemporalLayers == 2 ||
106 codec_.simulcastStream[0].numberOfTemporalLayers == 2)) {
107 int tl0_bitrate = std::min(codec_.targetBitrate, target_bitrate_kbps);
108 max_bitrate_kbps = std::min(codec_.maxBitrate, target_bitrate_kbps);
109 target_bitrate_kbps = tl0_bitrate;
110 }
111 } else {
112 max_bitrate_kbps = codec_.simulcastStream[simulcast_id].maxBitrate;
113 }
114
115 std::vector<uint32_t> tl_allocation = tl_it->second->OnRatesUpdated(
116 target_bitrate_kbps, max_bitrate_kbps, framerate);
117
118 for (size_t tl_index = 0; tl_index < tl_allocation.size(); ++tl_index) {
119 allocated_bitrates_bps.set_bitrate(simulcast_id, tl_index,
120 tl_allocation[tl_index] * 1000);
121 }
63 } 122 }
64 123
65 return allocated_bitrates_bps; 124 return allocated_bitrates_bps;
66 } 125 }
67 126
68 uint32_t SimulcastRateAllocator::GetPreferedBitrate() const { 127 uint32_t SimulcastRateAllocator::GetPreferedBitrate(int frame_rate) {
69 std::vector<uint32_t> rates = GetAllocation(codec_.maxBitrate); 128 BitrateAllocation allocation =
70 uint32_t preferred_bitrate = 0; 129 GetAllocation(codec_.maxBitrate * 1000, frame_rate);
71 for (const uint32_t& rate : rates) { 130 return allocation.get_sum_kbps();
72 preferred_bitrate += rate;
73 }
74 return preferred_bitrate;
75 } 131 }
76 132
77 const VideoCodec& webrtc::SimulcastRateAllocator::GetCodec() const { 133 const VideoCodec& webrtc::SimulcastRateAllocator::GetCodec() const {
78 return codec_; 134 return codec_;
79 } 135 }
80 136
81 } // namespace webrtc 137 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698