Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | #include "rtc_base/experiments/quality_scaling_experiment.h" |
| 11 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 12 | #include <stdio.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 13 | |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 14 | #include <string> |
| 15 | |
| 16 | #include "rtc_base/logging.h" |
| 17 | #include "system_wrappers/include/field_trial.h" |
| 18 | |
| 19 | namespace webrtc { |
| 20 | namespace { |
| 21 | constexpr char kFieldTrial[] = "WebRTC-Video-QualityScaling"; |
| 22 | constexpr int kMinQp = 1; |
| 23 | constexpr int kMaxVp8Qp = 127; |
| 24 | constexpr int kMaxVp9Qp = 255; |
| 25 | constexpr int kMaxH264Qp = 51; |
| 26 | constexpr int kMaxGenericQp = 255; |
| 27 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 28 | absl::optional<VideoEncoder::QpThresholds> GetThresholds(int low, |
| 29 | int high, |
| 30 | int max) { |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 31 | if (low < kMinQp || high > max || high < low) |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 32 | return absl::nullopt; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 33 | |
| 34 | RTC_LOG(LS_INFO) << "QP thresholds: low: " << low << ", high: " << high; |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 35 | return absl::optional<VideoEncoder::QpThresholds>( |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 36 | VideoEncoder::QpThresholds(low, high)); |
| 37 | } |
| 38 | } // namespace |
| 39 | |
| 40 | bool QualityScalingExperiment::Enabled() { |
| 41 | return webrtc::field_trial::IsEnabled(kFieldTrial); |
| 42 | } |
| 43 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 44 | absl::optional<QualityScalingExperiment::Settings> |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 45 | QualityScalingExperiment::ParseSettings() { |
| 46 | const std::string group = webrtc::field_trial::FindFullName(kFieldTrial); |
| 47 | if (group.empty()) |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 48 | return absl::nullopt; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 49 | |
| 50 | Settings s; |
| 51 | if (sscanf(group.c_str(), "Enabled-%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%d", |
| 52 | &s.vp8_low, &s.vp8_high, &s.vp9_low, &s.vp9_high, &s.h264_low, |
| 53 | &s.h264_high, &s.generic_low, &s.generic_high, &s.alpha_high, |
| 54 | &s.alpha_low, &s.drop) != 11) { |
| 55 | RTC_LOG(LS_WARNING) << "Invalid number of parameters provided."; |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 56 | return absl::nullopt; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 57 | } |
| 58 | return s; |
| 59 | } |
| 60 | |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 61 | absl::optional<VideoEncoder::QpThresholds> |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 62 | QualityScalingExperiment::GetQpThresholds(VideoCodecType codec_type) { |
| 63 | const auto settings = ParseSettings(); |
| 64 | if (!settings) |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 65 | return absl::nullopt; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 66 | |
| 67 | switch (codec_type) { |
| 68 | case kVideoCodecVP8: |
| 69 | return GetThresholds(settings->vp8_low, settings->vp8_high, kMaxVp8Qp); |
| 70 | case kVideoCodecVP9: |
| 71 | return GetThresholds(settings->vp9_low, settings->vp9_high, kMaxVp9Qp); |
| 72 | case kVideoCodecH264: |
| 73 | return GetThresholds(settings->h264_low, settings->h264_high, kMaxH264Qp); |
| 74 | case kVideoCodecGeneric: |
| 75 | return GetThresholds(settings->generic_low, settings->generic_high, |
| 76 | kMaxGenericQp); |
| 77 | default: |
Danil Chapovalov | 0a1d189 | 2018-06-21 11:48:25 +0200 | [diff] [blame] | 78 | return absl::nullopt; |
Åsa Persson | a945aee | 2018-04-24 16:53:25 +0200 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | QualityScalingExperiment::Config QualityScalingExperiment::GetConfig() { |
| 83 | const auto settings = ParseSettings(); |
| 84 | if (!settings) |
| 85 | return Config(); |
| 86 | |
| 87 | Config config; |
| 88 | config.use_all_drop_reasons = settings->drop > 0; |
| 89 | |
| 90 | if (settings->alpha_high < 0 || settings->alpha_low < settings->alpha_high) { |
| 91 | RTC_LOG(LS_WARNING) << "Invalid alpha value provided, using default."; |
| 92 | return config; |
| 93 | } |
| 94 | config.alpha_high = settings->alpha_high; |
| 95 | config.alpha_low = settings->alpha_low; |
| 96 | return config; |
| 97 | } |
| 98 | |
| 99 | } // namespace webrtc |