OLD | NEW |
(Empty) | |
| 1 /* Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 2 * |
| 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 |
| 5 * tree. An additional intellectual property rights grant can be found |
| 6 * in the file PATENTS. All contributing project authors may |
| 7 * be found in the AUTHORS file in the root of the source tree. |
| 8 */ |
| 9 |
| 10 #include <algorithm> |
| 11 #include "webrtc/modules/video_coding/codecs/vp9/screenshare_layers.h" |
| 12 #include "webrtc/base/checks.h" |
| 13 |
| 14 namespace webrtc { |
| 15 |
| 16 ScreenshareLayersVP9::ScreenshareLayersVP9(uint8_t num_layers) |
| 17 : num_layers_(num_layers), current_layer_(0), last_ts_(0) { |
| 18 DCHECK_GT(num_layers, 0); |
| 19 DCHECK_LE(num_layers, kMaxVp9NumberOfSpatialLayers); |
| 20 memset(bits_used_, 0, sizeof(bits_used_)); |
| 21 memset(threshold_kbps_, 0, sizeof(threshold_kbps_)); |
| 22 } |
| 23 |
| 24 uint8_t ScreenshareLayersVP9::GetStartLayer() const { |
| 25 return current_layer_; |
| 26 } |
| 27 |
| 28 void ScreenshareLayersVP9::ConfigureBitrate(int threshold_kbps, |
| 29 uint8_t layer_id) { |
| 30 // The upper layer can't have a max bitrate, |
| 31 // and therefore it can't be set. |
| 32 DCHECK_LT(layer_id, num_layers_ - 1); |
| 33 threshold_kbps_[layer_id] = threshold_kbps; |
| 34 } |
| 35 |
| 36 void ScreenshareLayersVP9::LayerFrameEncoded(unsigned int size_bytes, |
| 37 uint8_t layer_id) { |
| 38 DCHECK_LT(layer_id, num_layers_); |
| 39 bits_used_[layer_id] += size_bytes * 8; |
| 40 } |
| 41 |
| 42 VP9EncoderImpl::SuperFrameRefSettings |
| 43 ScreenshareLayersVP9::GetSuperFrameSettings(uint32_t timestamp, |
| 44 bool is_keyframe) { |
| 45 VP9EncoderImpl::SuperFrameRefSettings settings; |
| 46 if (last_ts_ == 0) |
| 47 last_ts_ = timestamp; |
| 48 float time_diff = (timestamp - last_ts_) / 90.f; |
| 49 float total_bits_used = 0; |
| 50 float total_threshold_kbps = 0; |
| 51 current_layer_ = 0; |
| 52 |
| 53 // Up to (num_layers - 1) because we only have |
| 54 // (num_layers - 1) thresholds to check. |
| 55 for (uint8_t layer_id = 0; layer_id < num_layers_ - 1; ++layer_id) { |
| 56 bits_used_[layer_id] = std::max( |
| 57 0.f, bits_used_[layer_id] - time_diff * threshold_kbps_[layer_id]); |
| 58 total_bits_used += bits_used_[layer_id]; |
| 59 total_threshold_kbps += threshold_kbps_[layer_id]; |
| 60 if (!is_keyframe) { |
| 61 settings.layer[layer_id].ref_buf1 = layer_id; |
| 62 if (total_bits_used > total_threshold_kbps * 1000) |
| 63 current_layer_ = layer_id + 1; |
| 64 } |
| 65 settings.layer[layer_id].upd_buf = layer_id; |
| 66 } |
| 67 if (!is_keyframe) |
| 68 settings.layer[num_layers_ - 1].ref_buf1 = num_layers_ - 1; |
| 69 settings.layer[num_layers_ - 1].upd_buf = num_layers_ - 1; |
| 70 settings.is_keyframe = is_keyframe; |
| 71 settings.start_layer = current_layer_; |
| 72 settings.stop_layer = num_layers_ - 1; |
| 73 last_ts_ = timestamp; |
| 74 return settings; |
| 75 } |
| 76 |
| 77 } // namespace webrtc |
OLD | NEW |