OLD | NEW |
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" | |
17 #include "webrtc/modules/video_coding/include/video_codec_interface.h" | 15 #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
18 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h" | 16 #include "webrtc/modules/video_coding/codecs/vp8/include/vp8_common_types.h" |
19 #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h" | 17 #include "webrtc/modules/video_coding/codecs/vp8/temporal_layers.h" |
20 | 18 |
21 // This file implements logic to adapt the number of temporal layers based on | 19 // This file implements logic to adapt the number of temporal layers based on |
22 // input frame rate in order to avoid having the base layer being relaying at | 20 // input frame rate in order to avoid having the base layer being relaying at |
23 // a below acceptable framerate. | 21 // a below acceptable framerate. |
24 namespace webrtc { | 22 namespace webrtc { |
25 namespace { | 23 namespace { |
26 enum { | 24 enum { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 : temporal_layers_(1), | 88 : temporal_layers_(1), |
91 max_temporal_layers_(max_num_temporal_layers), | 89 max_temporal_layers_(max_num_temporal_layers), |
92 tl0_pic_idx_(initial_tl0_pic_idx), | 90 tl0_pic_idx_(initial_tl0_pic_idx), |
93 frame_counter_(static_cast<unsigned int>(-1)), | 91 frame_counter_(static_cast<unsigned int>(-1)), |
94 timestamp_(0), | 92 timestamp_(0), |
95 last_base_layer_sync_(0), | 93 last_base_layer_sync_(0), |
96 layer_ids_length_(0), | 94 layer_ids_length_(0), |
97 layer_ids_(NULL), | 95 layer_ids_(NULL), |
98 encode_flags_length_(0), | 96 encode_flags_length_(0), |
99 encode_flags_(NULL) { | 97 encode_flags_(NULL) { |
100 RTC_CHECK_GE(max_temporal_layers_, 1); | 98 assert(max_temporal_layers_ >= 1); |
101 RTC_CHECK_GE(max_temporal_layers_, 3); | 99 assert(max_temporal_layers_ <= 3); |
102 } | 100 } |
103 | 101 |
104 virtual ~RealTimeTemporalLayers() {} | 102 virtual ~RealTimeTemporalLayers() {} |
105 | 103 |
106 std::vector<uint32_t> OnRatesUpdated(int bitrate_kbps, | 104 bool ConfigureBitrates(int bitrate_kbit, |
107 int max_bitrate_kbps, | 105 int max_bitrate_kbit, |
108 int framerate) override { | 106 int framerate, |
| 107 vpx_codec_enc_cfg_t* cfg) override { |
109 temporal_layers_ = | 108 temporal_layers_ = |
110 CalculateNumberOfTemporalLayers(temporal_layers_, framerate); | 109 CalculateNumberOfTemporalLayers(temporal_layers_, framerate); |
111 temporal_layers_ = std::min(temporal_layers_, max_temporal_layers_); | 110 temporal_layers_ = std::min(temporal_layers_, max_temporal_layers_); |
112 RTC_CHECK_GE(temporal_layers_, 1); | 111 assert(temporal_layers_ >= 1 && temporal_layers_ <= 3); |
113 RTC_CHECK_LE(temporal_layers_, 3); | 112 |
| 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 } |
114 | 118 |
115 switch (temporal_layers_) { | 119 switch (temporal_layers_) { |
116 case 1: { | 120 case 1: { |
117 static const unsigned int layer_ids[] = {0u}; | 121 static const unsigned int layer_ids[] = {0u}; |
118 layer_ids_ = layer_ids; | 122 layer_ids_ = layer_ids; |
119 layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids); | 123 layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids); |
120 | 124 |
121 static const int encode_flags[] = {kTemporalUpdateLastRefAll}; | 125 static const int encode_flags[] = {kTemporalUpdateLastRefAll}; |
122 encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids); | 126 encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids); |
123 encode_flags_ = encode_flags; | 127 encode_flags_ = encode_flags; |
| 128 |
| 129 cfg->ts_rate_decimator[0] = 1; |
| 130 cfg->ts_periodicity = layer_ids_length_; |
124 } break; | 131 } break; |
125 | 132 |
126 case 2: { | 133 case 2: { |
127 static const unsigned int layer_ids[] = {0u, 1u}; | 134 static const unsigned int layer_ids[] = {0u, 1u}; |
128 layer_ids_ = layer_ids; | 135 layer_ids_ = layer_ids; |
129 layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids); | 136 layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids); |
130 | 137 |
131 static const int encode_flags[] = { | 138 static const int encode_flags[] = { |
132 kTemporalUpdateLastAndGoldenRefAltRef, | 139 kTemporalUpdateLastAndGoldenRefAltRef, |
133 kTemporalUpdateGoldenWithoutDependencyRefAltRef, | 140 kTemporalUpdateGoldenWithoutDependencyRefAltRef, |
134 kTemporalUpdateLastRefAltRef, | 141 kTemporalUpdateLastRefAltRef, |
135 kTemporalUpdateGoldenRefAltRef, | 142 kTemporalUpdateGoldenRefAltRef, |
136 kTemporalUpdateLastRefAltRef, | 143 kTemporalUpdateLastRefAltRef, |
137 kTemporalUpdateGoldenRefAltRef, | 144 kTemporalUpdateGoldenRefAltRef, |
138 kTemporalUpdateLastRefAltRef, | 145 kTemporalUpdateLastRefAltRef, |
139 kTemporalUpdateNone}; | 146 kTemporalUpdateNone}; |
140 encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids); | 147 encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids); |
141 encode_flags_ = encode_flags; | 148 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_; |
142 } break; | 153 } break; |
143 | 154 |
144 case 3: { | 155 case 3: { |
145 static const unsigned int layer_ids[] = {0u, 2u, 1u, 2u}; | 156 static const unsigned int layer_ids[] = {0u, 2u, 1u, 2u}; |
146 layer_ids_ = layer_ids; | 157 layer_ids_ = layer_ids; |
147 layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids); | 158 layer_ids_length_ = sizeof(layer_ids) / sizeof(*layer_ids); |
148 | 159 |
149 static const int encode_flags[] = { | 160 static const int encode_flags[] = { |
150 kTemporalUpdateLastAndGoldenRefAltRef, | 161 kTemporalUpdateLastAndGoldenRefAltRef, |
151 kTemporalUpdateNoneNoRefGoldenRefAltRef, | 162 kTemporalUpdateNoneNoRefGoldenRefAltRef, |
152 kTemporalUpdateGoldenWithoutDependencyRefAltRef, | 163 kTemporalUpdateGoldenWithoutDependencyRefAltRef, |
153 kTemporalUpdateNone, | 164 kTemporalUpdateNone, |
154 kTemporalUpdateLastRefAltRef, | 165 kTemporalUpdateLastRefAltRef, |
155 kTemporalUpdateNone, | 166 kTemporalUpdateNone, |
156 kTemporalUpdateGoldenRefAltRef, | 167 kTemporalUpdateGoldenRefAltRef, |
157 kTemporalUpdateNone}; | 168 kTemporalUpdateNone}; |
158 encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids); | 169 encode_flags_length_ = sizeof(encode_flags) / sizeof(*layer_ids); |
159 encode_flags_ = encode_flags; | 170 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_; |
160 } break; | 176 } break; |
161 | 177 |
162 default: | 178 default: |
163 RTC_NOTREACHED(); | 179 assert(false); |
164 return std::vector<uint32_t>(); | 180 return false; |
165 } | 181 } |
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 | |
210 memcpy(cfg->ts_layer_id, layer_ids_, | 182 memcpy(cfg->ts_layer_id, layer_ids_, |
211 sizeof(unsigned int) * layer_ids_length_); | 183 sizeof(unsigned int) * layer_ids_length_); |
212 | |
213 return true; | 184 return true; |
214 } | 185 } |
215 | 186 |
216 int EncodeFlags(uint32_t timestamp) override { | 187 int EncodeFlags(uint32_t timestamp) override { |
217 frame_counter_++; | 188 frame_counter_++; |
218 return CurrentEncodeFlags(); | 189 return CurrentEncodeFlags(); |
219 } | 190 } |
220 | 191 |
221 int CurrentEncodeFlags() const { | 192 int CurrentEncodeFlags() const { |
222 assert(encode_flags_length_ > 0 && encode_flags_ != NULL); | 193 assert(encode_flags_length_ > 0 && encode_flags_ != NULL); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 timestamp_ = timestamp; | 241 timestamp_ = timestamp; |
271 tl0_pic_idx_++; | 242 tl0_pic_idx_++; |
272 } | 243 } |
273 last_base_layer_sync_ = base_layer_sync; | 244 last_base_layer_sync_ = base_layer_sync; |
274 vp8_info->tl0PicIdx = tl0_pic_idx_; | 245 vp8_info->tl0PicIdx = tl0_pic_idx_; |
275 } | 246 } |
276 } | 247 } |
277 | 248 |
278 void FrameEncoded(unsigned int size, uint32_t timestamp, int qp) override {} | 249 void FrameEncoded(unsigned int size, uint32_t timestamp, int qp) override {} |
279 | 250 |
| 251 bool UpdateConfiguration(vpx_codec_enc_cfg_t* cfg) override { return false; } |
| 252 |
280 private: | 253 private: |
281 int temporal_layers_; | 254 int temporal_layers_; |
282 int max_temporal_layers_; | 255 int max_temporal_layers_; |
283 | 256 |
284 int tl0_pic_idx_; | 257 int tl0_pic_idx_; |
285 unsigned int frame_counter_; | 258 unsigned int frame_counter_; |
286 uint32_t timestamp_; | 259 uint32_t timestamp_; |
287 bool last_base_layer_sync_; | 260 bool last_base_layer_sync_; |
288 | 261 |
289 // Pattern of temporal layer ids. | 262 // Pattern of temporal layer ids. |
290 int layer_ids_length_; | 263 int layer_ids_length_; |
291 const unsigned int* layer_ids_; | 264 const unsigned int* layer_ids_; |
292 | 265 |
293 // Pattern of encode flags. | 266 // Pattern of encode flags. |
294 int encode_flags_length_; | 267 int encode_flags_length_; |
295 const int* encode_flags_; | 268 const int* encode_flags_; |
296 | |
297 rtc::Optional<std::vector<uint32_t>> new_bitrates_kbps_; | |
298 }; | 269 }; |
299 } // namespace | 270 } // namespace |
300 | 271 |
301 TemporalLayers* RealTimeTemporalLayersFactory::Create( | 272 TemporalLayers* RealTimeTemporalLayersFactory::Create( |
302 int simulcast_id, | |
303 int max_temporal_layers, | 273 int max_temporal_layers, |
304 uint8_t initial_tl0_pic_idx) const { | 274 uint8_t initial_tl0_pic_idx) const { |
305 TemporalLayers* tl = | 275 return new RealTimeTemporalLayers(max_temporal_layers, initial_tl0_pic_idx); |
306 new RealTimeTemporalLayers(max_temporal_layers, initial_tl0_pic_idx); | |
307 if (listener_) | |
308 listener_->OnTemporalLayersCreated(simulcast_id, tl); | |
309 return tl; | |
310 } | 276 } |
311 } // namespace webrtc | 277 } // namespace webrtc |
OLD | NEW |