| 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" |
| 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 Loading... |
| 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_kbit, |
| 105 int max_bitrate_kbit, | 107 int max_bitrate_kbit, |
| 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_kbit * 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 bitrates[i] -= sum; |
| 181 sum += layer_bitrate; |
| 182 |
| 183 if (sum == static_cast<uint32_t>(bitrate_kbit)) { |
| 184 // Sum adds up; any subsequent layers will be 0. |
| 185 bitrates.resize(i); |
| 186 break; |
| 187 } |
| 188 } |
| 189 |
| 190 return bitrates; |
| 191 } |
| 192 |
| 193 bool UpdateConfiguration(vpx_codec_enc_cfg_t* cfg) override { |
| 194 if (!new_bitrates_kbps_) |
| 195 return false; |
| 196 |
| 197 cfg->ts_number_layers = temporal_layers_; |
| 198 for (int tl = 0; tl < temporal_layers_; ++tl) { |
| 199 cfg->ts_target_bitrate[tl] = (*new_bitrates_kbps_)[tl]; |
| 200 } |
| 201 new_bitrates_kbps_ = rtc::Optional<std::vector<uint32_t>>(); |
| 202 |
| 203 cfg->ts_periodicity = layer_ids_length_; |
| 204 int decimator = 1; |
| 205 for (int i = temporal_layers_ - 1; i >= 0; --i, decimator *= 2) { |
| 206 cfg->ts_rate_decimator[i] = decimator; |
| 207 } |
| 208 |
| 182 memcpy(cfg->ts_layer_id, layer_ids_, | 209 memcpy(cfg->ts_layer_id, layer_ids_, |
| 183 sizeof(unsigned int) * layer_ids_length_); | 210 sizeof(unsigned int) * layer_ids_length_); |
| 211 |
| 184 return true; | 212 return true; |
| 185 } | 213 } |
| 186 | 214 |
| 187 int EncodeFlags(uint32_t timestamp) override { | 215 int EncodeFlags(uint32_t timestamp) override { |
| 188 frame_counter_++; | 216 frame_counter_++; |
| 189 return CurrentEncodeFlags(); | 217 return CurrentEncodeFlags(); |
| 190 } | 218 } |
| 191 | 219 |
| 192 int CurrentEncodeFlags() const { | 220 int CurrentEncodeFlags() const { |
| 193 assert(encode_flags_length_ > 0 && encode_flags_ != NULL); | 221 assert(encode_flags_length_ > 0 && encode_flags_ != NULL); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 timestamp_ = timestamp; | 269 timestamp_ = timestamp; |
| 242 tl0_pic_idx_++; | 270 tl0_pic_idx_++; |
| 243 } | 271 } |
| 244 last_base_layer_sync_ = base_layer_sync; | 272 last_base_layer_sync_ = base_layer_sync; |
| 245 vp8_info->tl0PicIdx = tl0_pic_idx_; | 273 vp8_info->tl0PicIdx = tl0_pic_idx_; |
| 246 } | 274 } |
| 247 } | 275 } |
| 248 | 276 |
| 249 void FrameEncoded(unsigned int size, uint32_t timestamp, int qp) override {} | 277 void FrameEncoded(unsigned int size, uint32_t timestamp, int qp) override {} |
| 250 | 278 |
| 251 bool UpdateConfiguration(vpx_codec_enc_cfg_t* cfg) override { return false; } | |
| 252 | |
| 253 private: | 279 private: |
| 254 int temporal_layers_; | 280 int temporal_layers_; |
| 255 int max_temporal_layers_; | 281 int max_temporal_layers_; |
| 256 | 282 |
| 257 int tl0_pic_idx_; | 283 int tl0_pic_idx_; |
| 258 unsigned int frame_counter_; | 284 unsigned int frame_counter_; |
| 259 uint32_t timestamp_; | 285 uint32_t timestamp_; |
| 260 bool last_base_layer_sync_; | 286 bool last_base_layer_sync_; |
| 261 | 287 |
| 262 // Pattern of temporal layer ids. | 288 // Pattern of temporal layer ids. |
| 263 int layer_ids_length_; | 289 int layer_ids_length_; |
| 264 const unsigned int* layer_ids_; | 290 const unsigned int* layer_ids_; |
| 265 | 291 |
| 266 // Pattern of encode flags. | 292 // Pattern of encode flags. |
| 267 int encode_flags_length_; | 293 int encode_flags_length_; |
| 268 const int* encode_flags_; | 294 const int* encode_flags_; |
| 295 |
| 296 rtc::Optional<std::vector<uint32_t>> new_bitrates_kbps_; |
| 269 }; | 297 }; |
| 270 } // namespace | 298 } // namespace |
| 271 | 299 |
| 272 TemporalLayers* RealTimeTemporalLayersFactory::Create( | 300 TemporalLayers* RealTimeTemporalLayersFactory::Create( |
| 301 int simulcast_id, |
| 273 int max_temporal_layers, | 302 int max_temporal_layers, |
| 274 uint8_t initial_tl0_pic_idx) const { | 303 uint8_t initial_tl0_pic_idx) const { |
| 275 return new RealTimeTemporalLayers(max_temporal_layers, initial_tl0_pic_idx); | 304 TemporalLayers* tl = |
| 305 new RealTimeTemporalLayers(max_temporal_layers, initial_tl0_pic_idx); |
| 306 if (listener_) |
| 307 listener_->OnTemporalLayersCreated(simulcast_id, tl); |
| 308 return tl; |
| 276 } | 309 } |
| 277 } // namespace webrtc | 310 } // namespace webrtc |
| OLD | NEW |