blob: ca58ba858acdb83aafb77263a09e0a5998a11768 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020013
Åsa Perssona945aee2018-04-24 16:53:25 +020014#include <string>
15
16#include "rtc_base/logging.h"
17#include "system_wrappers/include/field_trial.h"
18
19namespace webrtc {
20namespace {
21constexpr char kFieldTrial[] = "WebRTC-Video-QualityScaling";
22constexpr int kMinQp = 1;
23constexpr int kMaxVp8Qp = 127;
24constexpr int kMaxVp9Qp = 255;
25constexpr int kMaxH264Qp = 51;
26constexpr int kMaxGenericQp = 255;
27
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020028absl::optional<VideoEncoder::QpThresholds> GetThresholds(int low,
29 int high,
30 int max) {
Åsa Perssona945aee2018-04-24 16:53:25 +020031 if (low < kMinQp || high > max || high < low)
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020032 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020033
34 RTC_LOG(LS_INFO) << "QP thresholds: low: " << low << ", high: " << high;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020035 return absl::optional<VideoEncoder::QpThresholds>(
Åsa Perssona945aee2018-04-24 16:53:25 +020036 VideoEncoder::QpThresholds(low, high));
37}
38} // namespace
39
40bool QualityScalingExperiment::Enabled() {
41 return webrtc::field_trial::IsEnabled(kFieldTrial);
42}
43
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020044absl::optional<QualityScalingExperiment::Settings>
Åsa Perssona945aee2018-04-24 16:53:25 +020045QualityScalingExperiment::ParseSettings() {
46 const std::string group = webrtc::field_trial::FindFullName(kFieldTrial);
47 if (group.empty())
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020048 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020049
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 Chapovalov0a1d1892018-06-21 11:48:25 +020056 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020057 }
58 return s;
59}
60
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020061absl::optional<VideoEncoder::QpThresholds>
Åsa Perssona945aee2018-04-24 16:53:25 +020062QualityScalingExperiment::GetQpThresholds(VideoCodecType codec_type) {
63 const auto settings = ParseSettings();
64 if (!settings)
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020065 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020066
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 Chapovalov0a1d1892018-06-21 11:48:25 +020078 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020079 }
80}
81
82QualityScalingExperiment::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