blob: 8c999543c242c26dd682d7299b614851fbcc3e1c [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
26rtc::Optional<VideoEncoder::QpThresholds> GetThresholds(int low,
27 int high,
28 int max) {
29 if (low < kMinQp || high > max || high < low)
30 return rtc::nullopt;
31
32 RTC_LOG(LS_INFO) << "QP thresholds: low: " << low << ", high: " << high;
33 return rtc::Optional<VideoEncoder::QpThresholds>(
34 VideoEncoder::QpThresholds(low, high));
35}
36} // namespace
37
38bool QualityScalingExperiment::Enabled() {
39 return webrtc::field_trial::IsEnabled(kFieldTrial);
40}
41
42rtc::Optional<QualityScalingExperiment::Settings>
43QualityScalingExperiment::ParseSettings() {
44 const std::string group = webrtc::field_trial::FindFullName(kFieldTrial);
45 if (group.empty())
46 return rtc::nullopt;
47
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.";
54 return rtc::nullopt;
55 }
56 return s;
57}
58
59rtc::Optional<VideoEncoder::QpThresholds>
60QualityScalingExperiment::GetQpThresholds(VideoCodecType codec_type) {
61 const auto settings = ParseSettings();
62 if (!settings)
63 return rtc::nullopt;
64
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:
76 return rtc::nullopt;
77 }
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