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

Side by Side Diff: webrtc/modules/video_coding/codecs/vp8/realtime_temporal_layers.cc

Issue 2510583002: Reland #2 of Issue 2434073003: Extract bitrate allocation ... (Closed)
Patch Set: Addressed comments Created 4 years, 1 month 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 /* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 1 /* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
2 * 2 *
3 * Use of this source code is governed by a BSD-style license 3 * Use of this source code is governed by a BSD-style license
4 * that can be found in the LICENSE file in the root of the source 4 * that can be found in the LICENSE file in the root of the source
5 * tree. An additional intellectual property rights grant can be found 5 * tree. An additional intellectual property rights grant can be found
6 * in the file PATENTS. All contributing project authors may 6 * in the file PATENTS. All contributing project authors may
7 * be found in the AUTHORS file in the root of the source tree. 7 * be found in the AUTHORS file in the root of the source tree.
8 */ 8 */
9 9
10 #include <stdlib.h> 10 #include <stdlib.h>
11 #include <algorithm> 11 #include <algorithm>
12 12
13 #include "vpx/vpx_encoder.h" 13 #include "vpx/vpx_encoder.h"
14 #include "vpx/vp8cx.h" 14 #include "vpx/vp8cx.h"
15 #include "webrtc/base/checks.h"
16 #include "webrtc/base/optional.h"
15 #include "webrtc/modules/video_coding/include/video_codec_interface.h" 17 #include "webrtc/modules/video_coding/include/video_codec_interface.h"
16 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h" 18 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h"
17 #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h" 19 #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h"
18 20
19 // This file implements logic to adapt the number of temporal layers based on 21 // This file implements logic to adapt the number of temporal layers based on
20 // input frame rate in order to avoid having the base layer being relaying at 22 // input frame rate in order to avoid having the base layer being relaying at
21 // a below acceptable framerate. 23 // a below acceptable framerate.
22 namespace webrtc { 24 namespace webrtc {
23 namespace { 25 namespace {
24 enum { 26 enum {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 : temporal_layers_(1), 90 : temporal_layers_(1),
89 max_temporal_layers_(max_num_temporal_layers), 91 max_temporal_layers_(max_num_temporal_layers),
90 tl0_pic_idx_(initial_tl0_pic_idx), 92 tl0_pic_idx_(initial_tl0_pic_idx),
91 frame_counter_(static_cast<unsigned int>(-1)), 93 frame_counter_(static_cast<unsigned int>(-1)),
92 timestamp_(0), 94 timestamp_(0),
93 last_base_layer_sync_(0), 95 last_base_layer_sync_(0),
94 layer_ids_length_(0), 96 layer_ids_length_(0),
95 layer_ids_(NULL), 97 layer_ids_(NULL),
96 encode_flags_length_(0), 98 encode_flags_length_(0),
97 encode_flags_(NULL) { 99 encode_flags_(NULL) {
98 assert(max_temporal_layers_ >= 1); 100 RTC_CHECK_GE(max_temporal_layers_, 1);
99 assert(max_temporal_layers_ <= 3); 101 RTC_CHECK_GE(max_temporal_layers_, 3);
100 } 102 }
101 103
102 virtual ~RealTimeTemporalLayers() {} 104 virtual ~RealTimeTemporalLayers() {}
103 105
104 bool ConfigureBitrates(int bitrate_kbit, 106 std::vector<uint32_t> OnRatesUpdated(int bitrate_kbps,
105 int max_bitrate_kbit, 107 int max_bitrate_kbps,
106 int framerate, 108 int framerate) override {
107 vpx_codec_enc_cfg_t* cfg) override {
108 temporal_layers_ = 109 temporal_layers_ =
109 CalculateNumberOfTemporalLayers(temporal_layers_, framerate); 110 CalculateNumberOfTemporalLayers(temporal_layers_, framerate);
110 temporal_layers_ = std::min(temporal_layers_, max_temporal_layers_); 111 temporal_layers_ = std::min(temporal_layers_, max_temporal_layers_);
111 assert(temporal_layers_ >= 1 && temporal_layers_ <= 3); 112 RTC_CHECK_GE(temporal_layers_, 1);
112 113 RTC_CHECK_LE(temporal_layers_, 3);
113 cfg->ts_number_layers = temporal_layers_;
114 for (int tl = 0; tl < temporal_layers_; ++tl) {
115 cfg->ts_target_bitrate[tl] =
116 bitrate_kbit * kVp8LayerRateAlloction[temporal_layers_ - 1][tl];
117 }
118 114
119 switch (temporal_layers_) { 115 switch (temporal_layers_) {
120 case 1: { 116 case 1: {
121 static const unsigned int layer_ids[] = {0u}; 117 static const unsigned int layer_ids[] = {0u};
122 layer_ids_ = layer_ids; 118 layer_ids_ = layer_ids;
123 layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids); 119 layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids);
124 120
125 static const int encode_flags[] = {kTemporalUpdateLastRefAll}; 121 static const int encode_flags[] = {kTemporalUpdateLastRefAll};
126 encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids); 122 encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids);
127 encode_flags_ = encode_flags; 123 encode_flags_ = encode_flags;
128
129 cfg->ts_rate_decimator[0] = 1;
130 cfg->ts_periodicity = layer_ids_length_;
131 } break; 124 } break;
132 125
133 case 2: { 126 case 2: {
134 static const unsigned int layer_ids[] = {0u, 1u}; 127 static const unsigned int layer_ids[] = {0u, 1u};
135 layer_ids_ = layer_ids; 128 layer_ids_ = layer_ids;
136 layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids); 129 layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids);
137 130
138 static const int encode_flags[] = { 131 static const int encode_flags[] = {
139 kTemporalUpdateLastAndGoldenRefAltRef, 132 kTemporalUpdateLastAndGoldenRefAltRef,
140 kTemporalUpdateGoldenWithoutDependencyRefAltRef, 133 kTemporalUpdateGoldenWithoutDependencyRefAltRef,
141 kTemporalUpdateLastRefAltRef, 134 kTemporalUpdateLastRefAltRef,
142 kTemporalUpdateGoldenRefAltRef, 135 kTemporalUpdateGoldenRefAltRef,
143 kTemporalUpdateLastRefAltRef, 136 kTemporalUpdateLastRefAltRef,
144 kTemporalUpdateGoldenRefAltRef, 137 kTemporalUpdateGoldenRefAltRef,
145 kTemporalUpdateLastRefAltRef, 138 kTemporalUpdateLastRefAltRef,
146 kTemporalUpdateNone}; 139 kTemporalUpdateNone};
147 encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids); 140 encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids);
148 encode_flags_ = encode_flags; 141 encode_flags_ = encode_flags;
149
150 cfg->ts_rate_decimator[0] = 2;
151 cfg->ts_rate_decimator[1] = 1;
152 cfg->ts_periodicity = layer_ids_length_;
153 } break; 142 } break;
154 143
155 case 3: { 144 case 3: {
156 static const unsigned int layer_ids[] = {0u, 2u, 1u, 2u}; 145 static const unsigned int layer_ids[] = {0u, 2u, 1u, 2u};
157 layer_ids_ = layer_ids; 146 layer_ids_ = layer_ids;
158 layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids); 147 layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids);
159 148
160 static const int encode_flags[] = { 149 static const int encode_flags[] = {
161 kTemporalUpdateLastAndGoldenRefAltRef, 150 kTemporalUpdateLastAndGoldenRefAltRef,
162 kTemporalUpdateNoneNoRefGoldenRefAltRef, 151 kTemporalUpdateNoneNoRefGoldenRefAltRef,
163 kTemporalUpdateGoldenWithoutDependencyRefAltRef, 152 kTemporalUpdateGoldenWithoutDependencyRefAltRef,
164 kTemporalUpdateNone, 153 kTemporalUpdateNone,
165 kTemporalUpdateLastRefAltRef, 154 kTemporalUpdateLastRefAltRef,
166 kTemporalUpdateNone, 155 kTemporalUpdateNone,
167 kTemporalUpdateGoldenRefAltRef, 156 kTemporalUpdateGoldenRefAltRef,
168 kTemporalUpdateNone}; 157 kTemporalUpdateNone};
169 encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids); 158 encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids);
170 encode_flags_ = encode_flags; 159 encode_flags_ = encode_flags;
171
172 cfg->ts_rate_decimator[0] = 4;
173 cfg->ts_rate_decimator[1] = 2;
174 cfg->ts_rate_decimator[2] = 1;
175 cfg->ts_periodicity = layer_ids_length_;
176 } break; 160 } break;
177 161
178 default: 162 default:
179 assert(false); 163 RTC_NOTREACHED();
180 return false; 164 return std::vector<uint32_t>();
181 } 165 }
166
167 std::vector<uint32_t> bitrates;
168 const int num_layers = std::max(1, temporal_layers_);
169 for (int i = 0; i < num_layers; ++i) {
170 float layer_bitrate =
171 bitrate_kbps * kVp8LayerRateAlloction[num_layers - 1][i];
172 bitrates.push_back(static_cast<uint32_t>(layer_bitrate + 0.5));
173 }
174 new_bitrates_kbps_ = rtc::Optional<std::vector<uint32_t>>(bitrates);
175
176 // Allocation table is of aggregates, transform to individual rates.
177 uint32_t sum = 0;
178 for (int i = 0; i < num_layers; ++i) {
179 uint32_t layer_bitrate = bitrates[i];
180 RTC_DCHECK_LE(sum, bitrates[i]);
181 bitrates[i] -= sum;
182 sum += layer_bitrate;
183
184 if (sum == static_cast<uint32_t>(bitrate_kbps)) {
185 // Sum adds up; any subsequent layers will be 0.
186 bitrates.resize(i);
187 break;
188 }
189 }
190
191 return bitrates;
192 }
193
194 bool UpdateConfiguration(vpx_codec_enc_cfg_t* cfg) override {
195 if (!new_bitrates_kbps_)
196 return false;
197
198 cfg->ts_number_layers = temporal_layers_;
199 for (int tl = 0; tl < temporal_layers_; ++tl) {
200 cfg->ts_target_bitrate[tl] = (*new_bitrates_kbps_)[tl];
201 }
202 new_bitrates_kbps_ = rtc::Optional<std::vector<uint32_t>>();
203
204 cfg->ts_periodicity = layer_ids_length_;
205 int decimator = 1;
206 for (int i = temporal_layers_ - 1; i >= 0; --i, decimator *= 2) {
207 cfg->ts_rate_decimator[i] = decimator;
208 }
209
182 memcpy(cfg->ts_layer_id, layer_ids_, 210 memcpy(cfg->ts_layer_id, layer_ids_,
183 sizeof(unsigned int) * layer_ids_length_); 211 sizeof(unsigned int) * layer_ids_length_);
212
184 return true; 213 return true;
185 } 214 }
186 215
187 int EncodeFlags(uint32_t timestamp) override { 216 int EncodeFlags(uint32_t timestamp) override {
188 frame_counter_++; 217 frame_counter_++;
189 return CurrentEncodeFlags(); 218 return CurrentEncodeFlags();
190 } 219 }
191 220
192 int CurrentEncodeFlags() const { 221 int CurrentEncodeFlags() const {
193 assert(encode_flags_length_ > 0 && encode_flags_ != NULL); 222 assert(encode_flags_length_ > 0 && encode_flags_ != NULL);
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 timestamp_ = timestamp; 270 timestamp_ = timestamp;
242 tl0_pic_idx_++; 271 tl0_pic_idx_++;
243 } 272 }
244 last_base_layer_sync_ = base_layer_sync; 273 last_base_layer_sync_ = base_layer_sync;
245 vp8_info->tl0PicIdx = tl0_pic_idx_; 274 vp8_info->tl0PicIdx = tl0_pic_idx_;
246 } 275 }
247 } 276 }
248 277
249 void FrameEncoded(unsigned int size, uint32_t timestamp, int qp) override {} 278 void FrameEncoded(unsigned int size, uint32_t timestamp, int qp) override {}
250 279
251 bool UpdateConfiguration(vpx_codec_enc_cfg_t* cfg) override { return false; }
252
253 private: 280 private:
254 int temporal_layers_; 281 int temporal_layers_;
255 int max_temporal_layers_; 282 int max_temporal_layers_;
256 283
257 int tl0_pic_idx_; 284 int tl0_pic_idx_;
258 unsigned int frame_counter_; 285 unsigned int frame_counter_;
259 uint32_t timestamp_; 286 uint32_t timestamp_;
260 bool last_base_layer_sync_; 287 bool last_base_layer_sync_;
261 288
262 // Pattern of temporal layer ids. 289 // Pattern of temporal layer ids.
263 int layer_ids_length_; 290 int layer_ids_length_;
264 const unsigned int* layer_ids_; 291 const unsigned int* layer_ids_;
265 292
266 // Pattern of encode flags. 293 // Pattern of encode flags.
267 int encode_flags_length_; 294 int encode_flags_length_;
268 const int* encode_flags_; 295 const int* encode_flags_;
296
297 rtc::Optional<std::vector<uint32_t>> new_bitrates_kbps_;
269 }; 298 };
270 } // namespace 299 } // namespace
271 300
272 TemporalLayers* RealTimeTemporalLayersFactory::Create( 301 TemporalLayers* RealTimeTemporalLayersFactory::Create(
302 int simulcast_id,
273 int max_temporal_layers, 303 int max_temporal_layers,
274 uint8_t initial_tl0_pic_idx) const { 304 uint8_t initial_tl0_pic_idx) const {
275 return new RealTimeTemporalLayers(max_temporal_layers, initial_tl0_pic_idx); 305 TemporalLayers* tl =
306 new RealTimeTemporalLayers(max_temporal_layers, initial_tl0_pic_idx);
307 if (listener_)
308 listener_->OnTemporalLayersCreated(simulcast_id, tl);
309 return tl;
276 } 310 }
277 } // namespace webrtc 311 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698