blob: c13de3f46bffe65a5c1e23e142feb44ce4e1bc71 [file] [log] [blame]
Åsa Perssona945aee2018-04-24 16:53:25 +02001/*
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
12#include <string>
13
14#include "rtc_base/logging.h"
15#include "system_wrappers/include/field_trial.h"
16
17namespace webrtc {
18namespace {
19constexpr char kFieldTrial[] = "WebRTC-Video-QualityScaling";
20constexpr int kMinQp = 1;
21constexpr int kMaxVp8Qp = 127;
22constexpr int kMaxVp9Qp = 255;
23constexpr int kMaxH264Qp = 51;
24constexpr int kMaxGenericQp = 255;
25
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020026absl::optional<VideoEncoder::QpThresholds> GetThresholds(int low,
27 int high,
28 int max) {
Åsa Perssona945aee2018-04-24 16:53:25 +020029 if (low < kMinQp || high > max || high < low)
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020030 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020031
32 RTC_LOG(LS_INFO) << "QP thresholds: low: " << low << ", high: " << high;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020033 return absl::optional<VideoEncoder::QpThresholds>(
Åsa Perssona945aee2018-04-24 16:53:25 +020034 VideoEncoder::QpThresholds(low, high));
35}
36} // namespace
37
38bool QualityScalingExperiment::Enabled() {
39 return webrtc::field_trial::IsEnabled(kFieldTrial);
40}
41
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020042absl::optional<QualityScalingExperiment::Settings>
Åsa Perssona945aee2018-04-24 16:53:25 +020043QualityScalingExperiment::ParseSettings() {
44 const std::string group = webrtc::field_trial::FindFullName(kFieldTrial);
45 if (group.empty())
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020046 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020047
48 Settings s;
49 if (sscanf(group.c_str(), "Enabled-%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%d",
50 &s.vp8_low, &s.vp8_high, &s.vp9_low, &s.vp9_high, &s.h264_low,
51 &s.h264_high, &s.generic_low, &s.generic_high, &s.alpha_high,
52 &s.alpha_low, &s.drop) != 11) {
53 RTC_LOG(LS_WARNING) << "Invalid number of parameters provided.";
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020054 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020055 }
56 return s;
57}
58
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020059absl::optional<VideoEncoder::QpThresholds>
Åsa Perssona945aee2018-04-24 16:53:25 +020060QualityScalingExperiment::GetQpThresholds(VideoCodecType codec_type) {
61 const auto settings = ParseSettings();
62 if (!settings)
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020063 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020064
65 switch (codec_type) {
66 case kVideoCodecVP8:
67 return GetThresholds(settings->vp8_low, settings->vp8_high, kMaxVp8Qp);
68 case kVideoCodecVP9:
69 return GetThresholds(settings->vp9_low, settings->vp9_high, kMaxVp9Qp);
70 case kVideoCodecH264:
71 return GetThresholds(settings->h264_low, settings->h264_high, kMaxH264Qp);
72 case kVideoCodecGeneric:
73 return GetThresholds(settings->generic_low, settings->generic_high,
74 kMaxGenericQp);
75 default:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020076 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020077 }
78}
79
80QualityScalingExperiment::Config QualityScalingExperiment::GetConfig() {
81 const auto settings = ParseSettings();
82 if (!settings)
83 return Config();
84
85 Config config;
86 config.use_all_drop_reasons = settings->drop > 0;
87
88 if (settings->alpha_high < 0 || settings->alpha_low < settings->alpha_high) {
89 RTC_LOG(LS_WARNING) << "Invalid alpha value provided, using default.";
90 return config;
91 }
92 config.alpha_high = settings->alpha_high;
93 config.alpha_low = settings->alpha_low;
94 return config;
95}
96
97} // namespace webrtc