blob: 4a50115262e7631c17e0cdf7dc3f560a0b7dcfbd [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
Yves Gerey3e707812018-11-28 16:47:49 +010012#include <stdio.h>
Åsa Perssona945aee2018-04-24 16:53:25 +020013#include <string>
14
15#include "rtc_base/logging.h"
16#include "system_wrappers/include/field_trial.h"
17
18namespace webrtc {
19namespace {
20constexpr char kFieldTrial[] = "WebRTC-Video-QualityScaling";
21constexpr int kMinQp = 1;
22constexpr int kMaxVp8Qp = 127;
23constexpr int kMaxVp9Qp = 255;
24constexpr int kMaxH264Qp = 51;
25constexpr int kMaxGenericQp = 255;
26
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020027absl::optional<VideoEncoder::QpThresholds> GetThresholds(int low,
28 int high,
29 int max) {
Åsa Perssona945aee2018-04-24 16:53:25 +020030 if (low < kMinQp || high > max || high < low)
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020031 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020032
33 RTC_LOG(LS_INFO) << "QP thresholds: low: " << low << ", high: " << high;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020034 return absl::optional<VideoEncoder::QpThresholds>(
Åsa Perssona945aee2018-04-24 16:53:25 +020035 VideoEncoder::QpThresholds(low, high));
36}
37} // namespace
38
39bool QualityScalingExperiment::Enabled() {
40 return webrtc::field_trial::IsEnabled(kFieldTrial);
41}
42
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020043absl::optional<QualityScalingExperiment::Settings>
Åsa Perssona945aee2018-04-24 16:53:25 +020044QualityScalingExperiment::ParseSettings() {
45 const std::string group = webrtc::field_trial::FindFullName(kFieldTrial);
46 if (group.empty())
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020047 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020048
49 Settings s;
50 if (sscanf(group.c_str(), "Enabled-%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%d",
51 &s.vp8_low, &s.vp8_high, &s.vp9_low, &s.vp9_high, &s.h264_low,
52 &s.h264_high, &s.generic_low, &s.generic_high, &s.alpha_high,
53 &s.alpha_low, &s.drop) != 11) {
54 RTC_LOG(LS_WARNING) << "Invalid number of parameters provided.";
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020055 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020056 }
57 return s;
58}
59
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020060absl::optional<VideoEncoder::QpThresholds>
Åsa Perssona945aee2018-04-24 16:53:25 +020061QualityScalingExperiment::GetQpThresholds(VideoCodecType codec_type) {
62 const auto settings = ParseSettings();
63 if (!settings)
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020064 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020065
66 switch (codec_type) {
67 case kVideoCodecVP8:
68 return GetThresholds(settings->vp8_low, settings->vp8_high, kMaxVp8Qp);
69 case kVideoCodecVP9:
70 return GetThresholds(settings->vp9_low, settings->vp9_high, kMaxVp9Qp);
71 case kVideoCodecH264:
72 return GetThresholds(settings->h264_low, settings->h264_high, kMaxH264Qp);
73 case kVideoCodecGeneric:
74 return GetThresholds(settings->generic_low, settings->generic_high,
75 kMaxGenericQp);
76 default:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020077 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020078 }
79}
80
81QualityScalingExperiment::Config QualityScalingExperiment::GetConfig() {
82 const auto settings = ParseSettings();
83 if (!settings)
84 return Config();
85
86 Config config;
87 config.use_all_drop_reasons = settings->drop > 0;
88
89 if (settings->alpha_high < 0 || settings->alpha_low < settings->alpha_high) {
90 RTC_LOG(LS_WARNING) << "Invalid alpha value provided, using default.";
91 return config;
92 }
93 config.alpha_high = settings->alpha_high;
94 config.alpha_low = settings->alpha_low;
95 return config;
96}
97
98} // namespace webrtc