blob: 64347fe9f5399e57acaeab2b6475aff64fba9e1b [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";
Ilya Nikolaevskiy066b5b62021-01-28 14:58:24 +010022constexpr char kDefaultQualityScalingSetttings[] =
23 "Enabled-29,95,149,205,24,37,26,36,0.9995,0.9999,1";
Åsa Perssona945aee2018-04-24 16:53:25 +020024constexpr int kMinQp = 1;
25constexpr int kMaxVp8Qp = 127;
26constexpr int kMaxVp9Qp = 255;
27constexpr int kMaxH264Qp = 51;
28constexpr int kMaxGenericQp = 255;
29
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020030absl::optional<VideoEncoder::QpThresholds> GetThresholds(int low,
31 int high,
32 int max) {
Åsa Perssona945aee2018-04-24 16:53:25 +020033 if (low < kMinQp || high > max || high < low)
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020034 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020035
36 RTC_LOG(LS_INFO) << "QP thresholds: low: " << low << ", high: " << high;
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020037 return absl::optional<VideoEncoder::QpThresholds>(
Åsa Perssona945aee2018-04-24 16:53:25 +020038 VideoEncoder::QpThresholds(low, high));
39}
40} // namespace
41
42bool QualityScalingExperiment::Enabled() {
Ilya Nikolaevskiy066b5b62021-01-28 14:58:24 +010043 return !webrtc::field_trial::IsDisabled(kFieldTrial);
Åsa Perssona945aee2018-04-24 16:53:25 +020044}
45
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020046absl::optional<QualityScalingExperiment::Settings>
Åsa Perssona945aee2018-04-24 16:53:25 +020047QualityScalingExperiment::ParseSettings() {
Ilya Nikolaevskiy066b5b62021-01-28 14:58:24 +010048 std::string group = webrtc::field_trial::FindFullName(kFieldTrial);
49 // TODO(http:crbug.org/webrtc/12401): Completely remove the experiment code
50 // after few releases.
Åsa Perssona945aee2018-04-24 16:53:25 +020051 if (group.empty())
Ilya Nikolaevskiy066b5b62021-01-28 14:58:24 +010052 group = kDefaultQualityScalingSetttings;
Åsa Perssona945aee2018-04-24 16:53:25 +020053
54 Settings s;
55 if (sscanf(group.c_str(), "Enabled-%d,%d,%d,%d,%d,%d,%d,%d,%f,%f,%d",
56 &s.vp8_low, &s.vp8_high, &s.vp9_low, &s.vp9_high, &s.h264_low,
57 &s.h264_high, &s.generic_low, &s.generic_high, &s.alpha_high,
58 &s.alpha_low, &s.drop) != 11) {
59 RTC_LOG(LS_WARNING) << "Invalid number of parameters provided.";
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020060 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020061 }
62 return s;
63}
64
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020065absl::optional<VideoEncoder::QpThresholds>
Åsa Perssona945aee2018-04-24 16:53:25 +020066QualityScalingExperiment::GetQpThresholds(VideoCodecType codec_type) {
67 const auto settings = ParseSettings();
68 if (!settings)
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020069 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020070
71 switch (codec_type) {
72 case kVideoCodecVP8:
73 return GetThresholds(settings->vp8_low, settings->vp8_high, kMaxVp8Qp);
74 case kVideoCodecVP9:
75 return GetThresholds(settings->vp9_low, settings->vp9_high, kMaxVp9Qp);
76 case kVideoCodecH264:
77 return GetThresholds(settings->h264_low, settings->h264_high, kMaxH264Qp);
78 case kVideoCodecGeneric:
79 return GetThresholds(settings->generic_low, settings->generic_high,
80 kMaxGenericQp);
81 default:
Danil Chapovalov0a1d1892018-06-21 11:48:25 +020082 return absl::nullopt;
Åsa Perssona945aee2018-04-24 16:53:25 +020083 }
84}
85
86QualityScalingExperiment::Config QualityScalingExperiment::GetConfig() {
87 const auto settings = ParseSettings();
88 if (!settings)
89 return Config();
90
91 Config config;
92 config.use_all_drop_reasons = settings->drop > 0;
93
94 if (settings->alpha_high < 0 || settings->alpha_low < settings->alpha_high) {
95 RTC_LOG(LS_WARNING) << "Invalid alpha value provided, using default.";
96 return config;
97 }
98 config.alpha_high = settings->alpha_high;
99 config.alpha_low = settings->alpha_low;
100 return config;
101}
102
103} // namespace webrtc