blob: 42ab4f77c6860639ce8e1fc503e2178b9c1212d7 [file] [log] [blame]
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001/*
2 * Copyright (c) 2014 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 */
11
Mirko Bonadei95adedb2018-11-19 09:52:37 +010012#ifdef RTC_ENABLE_VP9
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/video_coding/codecs/vp9/vp9_impl.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000015
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +020016#include <algorithm>
Sergey Silkind45b3452018-05-31 16:00:24 +020017#include <limits>
marpan@webrtc.org5b883172014-11-01 06:10:48 +000018#include <vector>
19
Karl Wiberg918f50c2018-07-05 11:40:33 +020020#include "absl/memory/memory.h"
Emircan Uysaler800787f2018-07-16 10:01:49 -070021#include "api/video/color_space.h"
Emircan Uysaler0823eec2018-07-13 17:10:00 -070022#include "api/video/i010_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "common_video/include/video_frame_buffer.h"
24#include "common_video/libyuv/include/webrtc_libyuv.h"
Sergey Silkind902d582018-05-18 17:31:19 +020025#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Sergey Silkin86684962018-03-28 19:32:37 +020026#include "modules/video_coding/codecs/vp9/svc_rate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/checks.h"
Erik Språng4b4266f2019-01-23 12:48:13 +010028#include "rtc_base/experiments/rate_control_settings.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/keep_ref_until_done.h"
30#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080031#include "rtc_base/time_utils.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/trace_event.h"
Sergey Silkin390f3582018-10-01 17:13:50 +020033#include "system_wrappers/include/field_trial.h"
Jonas Olssona4d87372019-07-05 19:08:33 +020034#include "vpx/vp8cx.h"
35#include "vpx/vp8dx.h"
36#include "vpx/vpx_decoder.h"
37#include "vpx/vpx_encoder.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000038
39namespace webrtc {
40
Sergey Silkind902d582018-05-18 17:31:19 +020041namespace {
Sergey Silkin390f3582018-10-01 17:13:50 +020042// Maps from gof_idx to encoder internal reference frame buffer index. These
43// maps work for 1,2 and 3 temporal layers with GOF length of 1,2 and 4 frames.
44uint8_t kRefBufIdx[4] = {0, 0, 0, 1};
45uint8_t kUpdBufIdx[4] = {0, 0, 1, 0};
46
Sergey Silkinb674cd12018-10-09 10:59:06 +020047int kMaxNumTiles4kVideo = 8;
48
Ilya Nikolaevskiy61170682019-03-06 16:04:32 +010049// Maximum allowed PID difference for differnet per-layer frame-rate case.
Ilya Nikolaevskiy30323e22019-09-06 13:12:52 +020050const int kMaxAllowedPidDiff = 30;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +010051
Erik Språng7a3fe892019-04-15 12:22:55 +020052constexpr double kLowRateFactor = 1.0;
53constexpr double kHighRateFactor = 2.0;
54
55// These settings correspond to the settings in vpx_codec_enc_cfg.
Erik Språngeb180f82019-05-23 13:55:24 +020056struct Vp9RateSettings {
Erik Språng7a3fe892019-04-15 12:22:55 +020057 uint32_t rc_undershoot_pct;
58 uint32_t rc_overshoot_pct;
59 uint32_t rc_buf_sz;
60 uint32_t rc_buf_optimal_sz;
61 uint32_t rc_dropframe_thresh;
62};
63
Marco6e89b252015-07-07 14:40:38 -070064// Only positive speeds, range for real-time coding currently is: 5 - 8.
65// Lower means slower/better quality, higher means fastest/lower quality.
66int GetCpuSpeed(int width, int height) {
Alex Glaznevfecb7c32016-03-31 14:23:27 -070067#if defined(WEBRTC_ARCH_ARM) || defined(WEBRTC_ARCH_ARM64) || defined(ANDROID)
Marco002f0d02015-12-17 09:49:31 -080068 return 8;
69#else
Marco6e89b252015-07-07 14:40:38 -070070 // For smaller resolutions, use lower speed setting (get some coding gain at
71 // the cost of increased encoding complexity).
72 if (width * height <= 352 * 288)
73 return 5;
74 else
75 return 7;
Marco002f0d02015-12-17 09:49:31 -080076#endif
Marco6e89b252015-07-07 14:40:38 -070077}
Emircan Uysaler715cc232018-07-23 17:50:43 -070078// Helper class for extracting VP9 colorspace.
79ColorSpace ExtractVP9ColorSpace(vpx_color_space_t space_t,
80 vpx_color_range_t range_t,
81 unsigned int bit_depth) {
Johannes Kronb47ccc32018-12-07 11:12:44 +010082 ColorSpace::PrimaryID primaries = ColorSpace::PrimaryID::kUnspecified;
83 ColorSpace::TransferID transfer = ColorSpace::TransferID::kUnspecified;
84 ColorSpace::MatrixID matrix = ColorSpace::MatrixID::kUnspecified;
Emircan Uysaler715cc232018-07-23 17:50:43 -070085 switch (space_t) {
86 case VPX_CS_BT_601:
87 case VPX_CS_SMPTE_170:
88 primaries = ColorSpace::PrimaryID::kSMPTE170M;
89 transfer = ColorSpace::TransferID::kSMPTE170M;
90 matrix = ColorSpace::MatrixID::kSMPTE170M;
91 break;
92 case VPX_CS_SMPTE_240:
93 primaries = ColorSpace::PrimaryID::kSMPTE240M;
94 transfer = ColorSpace::TransferID::kSMPTE240M;
95 matrix = ColorSpace::MatrixID::kSMPTE240M;
96 break;
97 case VPX_CS_BT_709:
98 primaries = ColorSpace::PrimaryID::kBT709;
99 transfer = ColorSpace::TransferID::kBT709;
100 matrix = ColorSpace::MatrixID::kBT709;
101 break;
102 case VPX_CS_BT_2020:
103 primaries = ColorSpace::PrimaryID::kBT2020;
104 switch (bit_depth) {
105 case 8:
106 transfer = ColorSpace::TransferID::kBT709;
107 break;
108 case 10:
109 transfer = ColorSpace::TransferID::kBT2020_10;
110 break;
111 default:
112 RTC_NOTREACHED();
113 break;
114 }
115 matrix = ColorSpace::MatrixID::kBT2020_NCL;
116 break;
117 case VPX_CS_SRGB:
118 primaries = ColorSpace::PrimaryID::kBT709;
119 transfer = ColorSpace::TransferID::kIEC61966_2_1;
120 matrix = ColorSpace::MatrixID::kBT709;
121 break;
122 default:
123 break;
124 }
125
126 ColorSpace::RangeID range = ColorSpace::RangeID::kInvalid;
127 switch (range_t) {
128 case VPX_CR_STUDIO_RANGE:
129 range = ColorSpace::RangeID::kLimited;
130 break;
131 case VPX_CR_FULL_RANGE:
132 range = ColorSpace::RangeID::kFull;
133 break;
134 default:
135 break;
136 }
137 return ColorSpace(primaries, transfer, matrix, range);
138}
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100139
140bool MoreLayersEnabled(const VideoBitrateAllocation& first,
141 const VideoBitrateAllocation& second) {
142 for (size_t sl_idx = 0; sl_idx < kMaxSpatialLayers; ++sl_idx) {
143 if (first.GetSpatialLayerSum(sl_idx) > 0 &&
144 second.GetSpatialLayerSum(sl_idx) == 0) {
145 return true;
146 }
147 }
148 return false;
149}
150
Erik Språng7a3fe892019-04-15 12:22:55 +0200151uint32_t Interpolate(uint32_t low,
152 uint32_t high,
153 double bandwidth_headroom_factor) {
154 RTC_DCHECK_GE(bandwidth_headroom_factor, kLowRateFactor);
155 RTC_DCHECK_LE(bandwidth_headroom_factor, kHighRateFactor);
156
157 // |factor| is between 0.0 and 1.0.
158 const double factor = bandwidth_headroom_factor - kLowRateFactor;
159
160 return static_cast<uint32_t>(((1.0 - factor) * low) + (factor * high) + 0.5);
161}
162
Erik Språngeb180f82019-05-23 13:55:24 +0200163Vp9RateSettings GetRateSettings(double bandwidth_headroom_factor) {
164 static const Vp9RateSettings low_settings{100u, 0u, 100u, 33u, 40u};
165 static const Vp9RateSettings high_settings{50u, 50u, 1000u, 700u, 5u};
Erik Språng7a3fe892019-04-15 12:22:55 +0200166
167 if (bandwidth_headroom_factor <= kLowRateFactor) {
168 return low_settings;
169 } else if (bandwidth_headroom_factor >= kHighRateFactor) {
170 return high_settings;
171 }
172
Erik Språngeb180f82019-05-23 13:55:24 +0200173 Vp9RateSettings settings;
Erik Språng7a3fe892019-04-15 12:22:55 +0200174 settings.rc_undershoot_pct =
175 Interpolate(low_settings.rc_undershoot_pct,
176 high_settings.rc_undershoot_pct, bandwidth_headroom_factor);
177 settings.rc_overshoot_pct =
178 Interpolate(low_settings.rc_overshoot_pct, high_settings.rc_overshoot_pct,
179 bandwidth_headroom_factor);
180 settings.rc_buf_sz =
181 Interpolate(low_settings.rc_buf_sz, high_settings.rc_buf_sz,
182 bandwidth_headroom_factor);
183 settings.rc_buf_optimal_sz =
184 Interpolate(low_settings.rc_buf_optimal_sz,
185 high_settings.rc_buf_optimal_sz, bandwidth_headroom_factor);
186 settings.rc_dropframe_thresh =
187 Interpolate(low_settings.rc_dropframe_thresh,
188 high_settings.rc_dropframe_thresh, bandwidth_headroom_factor);
189 return settings;
190}
191
192void UpdateRateSettings(vpx_codec_enc_cfg_t* config,
Erik Språngeb180f82019-05-23 13:55:24 +0200193 const Vp9RateSettings& new_settings) {
Erik Språng7a3fe892019-04-15 12:22:55 +0200194 config->rc_undershoot_pct = new_settings.rc_undershoot_pct;
195 config->rc_overshoot_pct = new_settings.rc_overshoot_pct;
196 config->rc_buf_sz = new_settings.rc_buf_sz;
197 config->rc_buf_optimal_sz = new_settings.rc_buf_optimal_sz;
198 config->rc_dropframe_thresh = new_settings.rc_dropframe_thresh;
199}
200
Emircan Uysaler715cc232018-07-23 17:50:43 -0700201} // namespace
Marco6e89b252015-07-07 14:40:38 -0700202
asaperssona9455ab2015-07-31 06:10:09 -0700203void VP9EncoderImpl::EncoderOutputCodedPacketCallback(vpx_codec_cx_pkt* pkt,
204 void* user_data) {
philipelcce46fc2015-12-21 03:04:49 -0800205 VP9EncoderImpl* enc = static_cast<VP9EncoderImpl*>(user_data);
asaperssona9455ab2015-07-31 06:10:09 -0700206 enc->GetEncodedLayerFrame(pkt);
207}
208
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700209VP9EncoderImpl::VP9EncoderImpl(const cricket::VideoCodec& codec)
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000210 : encoded_image_(),
sprang3958ed82017-08-17 08:12:10 -0700211 encoded_complete_callback_(nullptr),
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700212 profile_(
213 ParseSdpForVP9Profile(codec.params).value_or(VP9Profile::kProfile0)),
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000214 inited_(false),
215 timestamp_(0),
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000216 cpu_speed_(3),
217 rc_max_intra_target_(0),
sprang3958ed82017-08-17 08:12:10 -0700218 encoder_(nullptr),
219 config_(nullptr),
220 raw_(nullptr),
221 input_image_(nullptr),
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200222 force_key_frame_(true),
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200223 pics_since_key_(0),
asaperssona9455ab2015-07-31 06:10:09 -0700224 num_temporal_layers_(0),
philipelcfc319b2015-11-10 07:17:23 -0800225 num_spatial_layers_(0),
Sergey Silkin390f3582018-10-01 17:13:50 +0200226 num_active_spatial_layers_(0),
Erik Språngd3438aa2018-11-08 16:56:43 +0100227 layer_deactivation_requires_key_frame_(
228 field_trial::IsEnabled("WebRTC-Vp9IssueKeyFrameOnLayerDeactivation")),
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200229 is_svc_(false),
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200230 inter_layer_pred_(InterLayerPredMode::kOn),
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100231 external_ref_control_(false), // Set in InitEncode because of tests.
Erik Språng4b4266f2019-01-23 12:48:13 +0100232 trusted_rate_controller_(RateControlSettings::ParseFromFieldTrials()
233 .LibvpxVp9TrustedRateController()),
Erik Språng7a3fe892019-04-15 12:22:55 +0200234 dynamic_rate_settings_(
235 RateControlSettings::ParseFromFieldTrials().Vp9DynamicRateSettings()),
Ilya Nikolaevskiy1af0f902019-09-09 10:16:18 +0200236 layer_buffering_(false),
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100237 full_superframe_drop_(true),
238 first_frame_in_picture_(true),
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100239 ss_info_needed_(false),
Ilya Nikolaevskiy61170682019-03-06 16:04:32 +0100240 is_flexible_mode_(false),
241 variable_framerate_experiment_(ParseVariableFramerateConfig(
242 "WebRTC-VP9VariableFramerateScreenshare")),
243 variable_framerate_controller_(
244 variable_framerate_experiment_.framerate_limit),
245 num_steady_state_frames_(0) {
philipeld1d03592019-03-01 13:53:55 +0100246 codec_ = {};
johannkoenig8225c402017-01-26 13:23:44 -0800247 memset(&svc_params_, 0, sizeof(vpx_svc_extra_cfg_t));
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000248}
249
250VP9EncoderImpl::~VP9EncoderImpl() {
251 Release();
252}
253
Elad Alon8f01c4e2019-06-28 15:19:43 +0200254void VP9EncoderImpl::SetFecControllerOverride(
255 FecControllerOverride* fec_controller_override) {
256 // Ignored.
257}
258
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000259int VP9EncoderImpl::Release() {
Sergey Silkin3e871ea2018-03-02 13:11:04 +0100260 int ret_val = WEBRTC_VIDEO_CODEC_OK;
261
Niels Möllerdd1cc982019-02-07 18:52:50 +0100262 encoded_image_.Allocate(0);
sprang3958ed82017-08-17 08:12:10 -0700263 if (encoder_ != nullptr) {
Sergey Silkin90399692018-03-02 14:44:10 +0100264 if (inited_) {
265 if (vpx_codec_destroy(encoder_)) {
266 ret_val = WEBRTC_VIDEO_CODEC_MEMORY;
267 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000268 }
269 delete encoder_;
sprang3958ed82017-08-17 08:12:10 -0700270 encoder_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000271 }
sprang3958ed82017-08-17 08:12:10 -0700272 if (config_ != nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000273 delete config_;
sprang3958ed82017-08-17 08:12:10 -0700274 config_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000275 }
sprang3958ed82017-08-17 08:12:10 -0700276 if (raw_ != nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000277 vpx_img_free(raw_);
sprang3958ed82017-08-17 08:12:10 -0700278 raw_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000279 }
280 inited_ = false;
Sergey Silkin3e871ea2018-03-02 13:11:04 +0100281 return ret_val;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000282}
283
sprangce4aef12015-11-02 07:23:20 -0800284bool VP9EncoderImpl::ExplicitlyConfiguredSpatialLayers() const {
285 // We check target_bitrate_bps of the 0th layer to see if the spatial layers
286 // (i.e. bitrates) were explicitly configured.
Sergey Silkinbeba1b22018-09-11 11:40:25 +0200287 return codec_.spatialLayers[0].targetBitrate > 0;
sprangce4aef12015-11-02 07:23:20 -0800288}
289
Erik Språng566124a2018-04-23 12:32:22 +0200290bool VP9EncoderImpl::SetSvcRates(
291 const VideoBitrateAllocation& bitrate_allocation) {
Sergey Silkin86684962018-03-28 19:32:37 +0200292 config_->rc_target_bitrate = bitrate_allocation.get_sum_kbps();
Sergey Silkin86684962018-03-28 19:32:37 +0200293
sprangce4aef12015-11-02 07:23:20 -0800294 if (ExplicitlyConfiguredSpatialLayers()) {
Sergey Silkine7ce8882018-10-03 18:04:57 +0200295 const bool layer_activation_requires_key_frame =
296 inter_layer_pred_ == InterLayerPredMode::kOff ||
297 inter_layer_pred_ == InterLayerPredMode::kOnKeyPic;
298
Sergey Silkin86684962018-03-28 19:32:37 +0200299 for (size_t sl_idx = 0; sl_idx < num_spatial_layers_; ++sl_idx) {
Sergey Silkine7ce8882018-10-03 18:04:57 +0200300 const bool was_layer_active = (config_->ss_target_bitrate[sl_idx] > 0);
Sergey Silkin86684962018-03-28 19:32:37 +0200301 config_->ss_target_bitrate[sl_idx] =
302 bitrate_allocation.GetSpatialLayerSum(sl_idx) / 1000;
303
304 for (size_t tl_idx = 0; tl_idx < num_temporal_layers_; ++tl_idx) {
305 config_->layer_target_bitrate[sl_idx * num_temporal_layers_ + tl_idx] =
306 bitrate_allocation.GetTemporalLayerSum(sl_idx, tl_idx) / 1000;
307 }
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200308
Sergey Silkine7ce8882018-10-03 18:04:57 +0200309 const bool is_active_layer = (config_->ss_target_bitrate[sl_idx] > 0);
310 if (!was_layer_active && is_active_layer &&
311 layer_activation_requires_key_frame) {
312 force_key_frame_ = true;
313 } else if (was_layer_active && !is_active_layer &&
314 layer_deactivation_requires_key_frame_) {
315 force_key_frame_ = true;
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200316 }
Sergey Silkin96f2c972018-09-05 21:07:17 +0200317
Sergey Silkine7ce8882018-10-03 18:04:57 +0200318 if (!was_layer_active) {
Sergey Silkin96f2c972018-09-05 21:07:17 +0200319 // Reset frame rate controller if layer is resumed after pause.
320 framerate_controller_[sl_idx].Reset();
321 }
322
323 framerate_controller_[sl_idx].SetTargetRate(
Ilya Nikolaevskiy5c18a5f2019-05-24 11:51:31 +0200324 codec_.spatialLayers[sl_idx].maxFramerate);
sprangce4aef12015-11-02 07:23:20 -0800325 }
326 } else {
327 float rate_ratio[VPX_MAX_LAYERS] = {0};
328 float total = 0;
Sergey Silkin908689d2018-08-20 09:28:45 +0200329 for (int i = 0; i < num_spatial_layers_; ++i) {
johannkoenig8225c402017-01-26 13:23:44 -0800330 if (svc_params_.scaling_factor_num[i] <= 0 ||
331 svc_params_.scaling_factor_den[i] <= 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100332 RTC_LOG(LS_ERROR) << "Scaling factors not specified!";
sprangce4aef12015-11-02 07:23:20 -0800333 return false;
334 }
Yves Gerey665174f2018-06-19 15:03:05 +0200335 rate_ratio[i] = static_cast<float>(svc_params_.scaling_factor_num[i]) /
336 svc_params_.scaling_factor_den[i];
sprangce4aef12015-11-02 07:23:20 -0800337 total += rate_ratio[i];
338 }
339
Sergey Silkin908689d2018-08-20 09:28:45 +0200340 for (int i = 0; i < num_spatial_layers_; ++i) {
Rasmus Brandt58cd3852018-06-26 13:41:16 +0200341 RTC_CHECK_GT(total, 0);
sprangce4aef12015-11-02 07:23:20 -0800342 config_->ss_target_bitrate[i] = static_cast<unsigned int>(
343 config_->rc_target_bitrate * rate_ratio[i] / total);
344 if (num_temporal_layers_ == 1) {
345 config_->layer_target_bitrate[i] = config_->ss_target_bitrate[i];
346 } else if (num_temporal_layers_ == 2) {
347 config_->layer_target_bitrate[i * num_temporal_layers_] =
348 config_->ss_target_bitrate[i] * 2 / 3;
349 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] =
350 config_->ss_target_bitrate[i];
351 } else if (num_temporal_layers_ == 3) {
352 config_->layer_target_bitrate[i * num_temporal_layers_] =
353 config_->ss_target_bitrate[i] / 2;
354 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] =
355 config_->layer_target_bitrate[i * num_temporal_layers_] +
356 (config_->ss_target_bitrate[i] / 4);
357 config_->layer_target_bitrate[i * num_temporal_layers_ + 2] =
358 config_->ss_target_bitrate[i];
359 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100360 RTC_LOG(LS_ERROR) << "Unsupported number of temporal layers: "
361 << num_temporal_layers_;
sprangce4aef12015-11-02 07:23:20 -0800362 return false;
363 }
Sergey Silkin96f2c972018-09-05 21:07:17 +0200364
365 framerate_controller_[i].SetTargetRate(codec_.maxFramerate);
asaperssona9455ab2015-07-31 06:10:09 -0700366 }
367 }
Sergey Silkin908689d2018-08-20 09:28:45 +0200368
369 num_active_spatial_layers_ = 0;
370 for (int i = 0; i < num_spatial_layers_; ++i) {
371 if (config_->ss_target_bitrate[i] > 0) {
372 ++num_active_spatial_layers_;
373 }
374 }
375 RTC_DCHECK_GT(num_active_spatial_layers_, 0);
376
asaperssona9455ab2015-07-31 06:10:09 -0700377 return true;
378}
379
Erik Språng16cb8f52019-04-12 13:59:09 +0200380void VP9EncoderImpl::SetRates(const RateControlParameters& parameters) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000381 if (!inited_) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200382 RTC_LOG(LS_WARNING) << "SetRates() calll while uninitialzied.";
383 return;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000384 }
385 if (encoder_->err) {
Erik Språng16cb8f52019-04-12 13:59:09 +0200386 RTC_LOG(LS_WARNING) << "Encoder in error state: " << encoder_->err;
387 return;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000388 }
Erik Språng16cb8f52019-04-12 13:59:09 +0200389 if (parameters.framerate_fps < 1.0) {
390 RTC_LOG(LS_WARNING) << "Unsupported framerate: "
391 << parameters.framerate_fps;
392 return;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000393 }
Erik Språng08127a92016-11-16 16:41:30 +0100394
Erik Språng16cb8f52019-04-12 13:59:09 +0200395 codec_.maxFramerate = static_cast<uint32_t>(parameters.framerate_fps + 0.5);
Erik Språng7a3fe892019-04-15 12:22:55 +0200396 requested_rate_settings_ = parameters;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000397}
398
Elad Alon370f93a2019-06-11 14:57:57 +0200399// TODO(eladalon): s/inst/codec_settings/g.
Elad Alon11dfff02019-06-10 19:10:29 +0200400int VP9EncoderImpl::InitEncode(const VideoCodec* inst,
Elad Alon370f93a2019-06-11 14:57:57 +0200401 const Settings& settings) {
sprang3958ed82017-08-17 08:12:10 -0700402 if (inst == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000403 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
404 }
405 if (inst->maxFramerate < 1) {
406 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
407 }
408 // Allow zero to represent an unspecified maxBitRate
409 if (inst->maxBitrate > 0 && inst->startBitrate > inst->maxBitrate) {
410 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
411 }
412 if (inst->width < 1 || inst->height < 1) {
413 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
414 }
Elad Alon370f93a2019-06-11 14:57:57 +0200415 if (settings.number_of_cores < 1) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000416 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
417 }
hta257dc392016-10-25 09:05:06 -0700418 if (inst->VP9().numberOfTemporalLayers > 3) {
asaperssona9455ab2015-07-31 06:10:09 -0700419 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
420 }
ilnik2a8c2f52017-02-15 02:23:28 -0800421 // libvpx probably does not support more than 3 spatial layers.
422 if (inst->VP9().numberOfSpatialLayers > 3) {
asaperssona9455ab2015-07-31 06:10:09 -0700423 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
424 }
philipelcfc319b2015-11-10 07:17:23 -0800425
asapersson86956de2016-01-26 01:05:20 -0800426 int ret_val = Release();
427 if (ret_val < 0) {
428 return ret_val;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000429 }
sprang3958ed82017-08-17 08:12:10 -0700430 if (encoder_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000431 encoder_ = new vpx_codec_ctx_t;
432 }
sprang3958ed82017-08-17 08:12:10 -0700433 if (config_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000434 config_ = new vpx_codec_enc_cfg_t;
435 }
436 timestamp_ = 0;
437 if (&codec_ != inst) {
438 codec_ = *inst;
439 }
asaperssona9455ab2015-07-31 06:10:09 -0700440
Sergey Silkinb0bd03b2018-09-28 14:56:39 +0200441 force_key_frame_ = true;
442 pics_since_key_ = 0;
443
hta257dc392016-10-25 09:05:06 -0700444 num_spatial_layers_ = inst->VP9().numberOfSpatialLayers;
Niels Möller65fb4042018-04-25 14:46:06 +0200445 RTC_DCHECK_GT(num_spatial_layers_, 0);
hta257dc392016-10-25 09:05:06 -0700446 num_temporal_layers_ = inst->VP9().numberOfTemporalLayers;
Sergey Silkin96f2c972018-09-05 21:07:17 +0200447 if (num_temporal_layers_ == 0) {
asaperssona9455ab2015-07-31 06:10:09 -0700448 num_temporal_layers_ = 1;
Sergey Silkin042661b2018-09-10 10:39:35 +0000449 }
Sergey Silkinae9e1882018-09-05 21:07:17 +0200450
Sergey Silkin96f2c972018-09-05 21:07:17 +0200451 framerate_controller_ = std::vector<FramerateController>(
452 num_spatial_layers_, FramerateController(codec_.maxFramerate));
453
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200454 is_svc_ = (num_spatial_layers_ > 1 || num_temporal_layers_ > 1);
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200455
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000456 encoded_image_._completeFrame = true;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000457 // Populate encoder configuration with default values.
458 if (vpx_codec_enc_config_default(vpx_codec_vp9_cx(), config_, 0)) {
459 return WEBRTC_VIDEO_CODEC_ERROR;
460 }
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700461
462 vpx_img_fmt img_fmt = VPX_IMG_FMT_NONE;
463 unsigned int bits_for_storage = 8;
464 switch (profile_) {
465 case VP9Profile::kProfile0:
466 img_fmt = VPX_IMG_FMT_I420;
467 bits_for_storage = 8;
468 config_->g_bit_depth = VPX_BITS_8;
469 config_->g_profile = 0;
470 config_->g_input_bit_depth = 8;
471 break;
472 case VP9Profile::kProfile2:
473 img_fmt = VPX_IMG_FMT_I42016;
474 bits_for_storage = 16;
475 config_->g_bit_depth = VPX_BITS_10;
476 config_->g_profile = 2;
477 config_->g_input_bit_depth = 10;
478 break;
479 }
480
481 // Creating a wrapper to the image - setting image data to nullptr. Actual
482 // pointer will be set in encode. Setting align to 1, as it is meaningless
483 // (actual memory is not allocated).
484 raw_ =
485 vpx_img_wrap(nullptr, img_fmt, codec_.width, codec_.height, 1, nullptr);
486 raw_->bit_depth = bits_for_storage;
487
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000488 config_->g_w = codec_.width;
489 config_->g_h = codec_.height;
490 config_->rc_target_bitrate = inst->startBitrate; // in kbit/s
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200491 config_->g_error_resilient = is_svc_ ? VPX_ERROR_RESILIENT_DEFAULT : 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000492 // Setting the time base of the codec.
493 config_->g_timebase.num = 1;
494 config_->g_timebase.den = 90000;
495 config_->g_lag_in_frames = 0; // 0- no frame lagging
496 config_->g_threads = 1;
497 // Rate control settings.
hta257dc392016-10-25 09:05:06 -0700498 config_->rc_dropframe_thresh = inst->VP9().frameDroppingOn ? 30 : 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000499 config_->rc_end_usage = VPX_CBR;
500 config_->g_pass = VPX_RC_ONE_PASS;
Ilya Nikolaevskiy3a656d12019-02-14 14:44:22 +0100501 config_->rc_min_quantizer =
502 codec_.mode == VideoCodecMode::kScreensharing ? 8 : 2;
marpan@webrtc.orgdc8a9da2015-01-27 23:08:24 +0000503 config_->rc_max_quantizer = 52;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000504 config_->rc_undershoot_pct = 50;
505 config_->rc_overshoot_pct = 50;
506 config_->rc_buf_initial_sz = 500;
507 config_->rc_buf_optimal_sz = 600;
508 config_->rc_buf_sz = 1000;
509 // Set the maximum target size of any key-frame.
510 rc_max_intra_target_ = MaxIntraTarget(config_->rc_buf_optimal_sz);
Ilya Nikolaevskiy2ec0c652019-01-18 11:56:48 +0100511 // Key-frame interval is enforced manually by this wrapper.
512 config_->kf_mode = VPX_KF_DISABLED;
513 // TODO(webm:1592): work-around for libvpx issue, as it can still
514 // put some key-frames at will even in VPX_KF_DISABLED kf_mode.
515 config_->kf_max_dist = inst->VP9().keyFrameInterval;
516 config_->kf_min_dist = config_->kf_max_dist;
hta257dc392016-10-25 09:05:06 -0700517 config_->rc_resize_allowed = inst->VP9().automaticResizeOn ? 1 : 0;
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000518 // Determine number of threads based on the image size and #cores.
philipelcce46fc2015-12-21 03:04:49 -0800519 config_->g_threads =
Elad Alon370f93a2019-06-11 14:57:57 +0200520 NumberOfThreads(config_->g_w, config_->g_h, settings.number_of_cores);
asaperssona9455ab2015-07-31 06:10:09 -0700521
Marco6e89b252015-07-07 14:40:38 -0700522 cpu_speed_ = GetCpuSpeed(config_->g_w, config_->g_h);
asaperssona9455ab2015-07-31 06:10:09 -0700523
hta257dc392016-10-25 09:05:06 -0700524 is_flexible_mode_ = inst->VP9().flexibleMode;
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200525
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100526 inter_layer_pred_ = inst->VP9().interLayerPred;
527
Ilya Nikolaevskiy5c18a5f2019-05-24 11:51:31 +0200528 if (num_spatial_layers_ > 1 &&
529 codec_.mode == VideoCodecMode::kScreensharing && !is_flexible_mode_) {
530 RTC_LOG(LS_ERROR) << "Flexible mode is required for screenshare with "
531 "several spatial layers";
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100532 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
533 }
534
535 // External reference control is required for different frame rate on spatial
536 // layers because libvpx generates rtp incompatible references in this case.
537 external_ref_control_ = field_trial::IsEnabled("WebRTC-Vp9ExternalRefCtrl") ||
Ilya Nikolaevskiy5c18a5f2019-05-24 11:51:31 +0200538 (num_spatial_layers_ > 1 &&
539 codec_.mode == VideoCodecMode::kScreensharing) ||
Ilya Nikolaevskiy2ec0c652019-01-18 11:56:48 +0100540 inter_layer_pred_ == InterLayerPredMode::kOn;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100541
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200542 if (num_temporal_layers_ == 1) {
asaperssona9455ab2015-07-31 06:10:09 -0700543 gof_.SetGofInfoVP9(kTemporalStructureMode1);
544 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING;
545 config_->ts_number_layers = 1;
546 config_->ts_rate_decimator[0] = 1;
547 config_->ts_periodicity = 1;
548 config_->ts_layer_id[0] = 0;
549 } else if (num_temporal_layers_ == 2) {
550 gof_.SetGofInfoVP9(kTemporalStructureMode2);
551 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0101;
552 config_->ts_number_layers = 2;
553 config_->ts_rate_decimator[0] = 2;
554 config_->ts_rate_decimator[1] = 1;
555 config_->ts_periodicity = 2;
556 config_->ts_layer_id[0] = 0;
557 config_->ts_layer_id[1] = 1;
558 } else if (num_temporal_layers_ == 3) {
559 gof_.SetGofInfoVP9(kTemporalStructureMode3);
560 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0212;
561 config_->ts_number_layers = 3;
562 config_->ts_rate_decimator[0] = 4;
563 config_->ts_rate_decimator[1] = 2;
564 config_->ts_rate_decimator[2] = 1;
565 config_->ts_periodicity = 4;
566 config_->ts_layer_id[0] = 0;
567 config_->ts_layer_id[1] = 2;
568 config_->ts_layer_id[2] = 1;
569 config_->ts_layer_id[3] = 2;
570 } else {
571 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
572 }
573
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100574 if (external_ref_control_) {
575 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_BYPASS;
Ilya Nikolaevskiy5c18a5f2019-05-24 11:51:31 +0200576 if (num_temporal_layers_ > 1 && num_spatial_layers_ > 1 &&
577 codec_.mode == VideoCodecMode::kScreensharing) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100578 // External reference control for several temporal layers with different
579 // frame rates on spatial layers is not implemented yet.
580 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
581 }
582 }
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200583 ref_buf_.clear();
584
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000585 return InitAndSetControlSettings(inst);
586}
587
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000588int VP9EncoderImpl::NumberOfThreads(int width,
589 int height,
590 int number_of_cores) {
591 // Keep the number of encoder threads equal to the possible number of column
592 // tiles, which is (1, 2, 4, 8). See comments below for VP9E_SET_TILE_COLUMNS.
593 if (width * height >= 1280 * 720 && number_of_cores > 4) {
594 return 4;
jianj23173a32017-07-12 16:11:09 -0700595 } else if (width * height >= 640 * 360 && number_of_cores > 2) {
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000596 return 2;
597 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200598// Use 2 threads for low res on ARM.
Jerome Jiang831af372017-12-05 10:44:35 -0800599#if defined(WEBRTC_ARCH_ARM) || defined(WEBRTC_ARCH_ARM64) || \
600 defined(WEBRTC_ANDROID)
601 if (width * height >= 320 * 180 && number_of_cores > 2) {
602 return 2;
603 }
604#endif
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000605 // 1 thread less than VGA.
606 return 1;
607 }
608}
609
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000610int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) {
Åsa Perssonff24c042015-12-04 10:58:08 +0100611 // Set QP-min/max per spatial and temporal layer.
612 int tot_num_layers = num_spatial_layers_ * num_temporal_layers_;
613 for (int i = 0; i < tot_num_layers; ++i) {
johannkoenig8225c402017-01-26 13:23:44 -0800614 svc_params_.max_quantizers[i] = config_->rc_max_quantizer;
615 svc_params_.min_quantizers[i] = config_->rc_min_quantizer;
Åsa Perssonff24c042015-12-04 10:58:08 +0100616 }
asaperssona9455ab2015-07-31 06:10:09 -0700617 config_->ss_number_layers = num_spatial_layers_;
sprangce4aef12015-11-02 07:23:20 -0800618 if (ExplicitlyConfiguredSpatialLayers()) {
619 for (int i = 0; i < num_spatial_layers_; ++i) {
620 const auto& layer = codec_.spatialLayers[i];
Rasmus Brandt58cd3852018-06-26 13:41:16 +0200621 RTC_CHECK_GT(layer.width, 0);
Sergey Silkin13e74342018-03-02 12:28:00 +0100622 const int scale_factor = codec_.width / layer.width;
623 RTC_DCHECK_GT(scale_factor, 0);
624
625 // Ensure scaler factor is integer.
626 if (scale_factor * layer.width != codec_.width) {
627 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
628 }
629
630 // Ensure scale factor is the same in both dimensions.
631 if (scale_factor * layer.height != codec_.height) {
632 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
633 }
634
635 // Ensure scale factor is power of two.
636 const bool is_pow_of_two = (scale_factor & (scale_factor - 1)) == 0;
637 if (!is_pow_of_two) {
638 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
639 }
640
641 svc_params_.scaling_factor_num[i] = 1;
642 svc_params_.scaling_factor_den[i] = scale_factor;
Sergey Silkin96f2c972018-09-05 21:07:17 +0200643
644 RTC_DCHECK_GT(codec_.spatialLayers[i].maxFramerate, 0);
645 RTC_DCHECK_LE(codec_.spatialLayers[i].maxFramerate, codec_.maxFramerate);
646 if (i > 0) {
647 // Frame rate of high spatial layer is supposed to be equal or higher
648 // than frame rate of low spatial layer.
649 RTC_DCHECK_GE(codec_.spatialLayers[i].maxFramerate,
650 codec_.spatialLayers[i - 1].maxFramerate);
651 }
sprangce4aef12015-11-02 07:23:20 -0800652 }
653 } else {
654 int scaling_factor_num = 256;
655 for (int i = num_spatial_layers_ - 1; i >= 0; --i) {
sprangce4aef12015-11-02 07:23:20 -0800656 // 1:2 scaling in each dimension.
johannkoenig8225c402017-01-26 13:23:44 -0800657 svc_params_.scaling_factor_num[i] = scaling_factor_num;
658 svc_params_.scaling_factor_den[i] = 256;
sprangce4aef12015-11-02 07:23:20 -0800659 }
asaperssona9455ab2015-07-31 06:10:09 -0700660 }
661
Sergey Silkin86684962018-03-28 19:32:37 +0200662 SvcRateAllocator init_allocator(codec_);
Florent Castelli8bbdb5b2019-08-02 15:16:28 +0200663 current_bitrate_allocation_ =
664 init_allocator.Allocate(VideoBitrateAllocationParameters(
665 inst->startBitrate * 1000, inst->maxFramerate));
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100666 if (!SetSvcRates(current_bitrate_allocation_)) {
asaperssona9455ab2015-07-31 06:10:09 -0700667 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
668 }
669
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700670 const vpx_codec_err_t rv = vpx_codec_enc_init(
671 encoder_, vpx_codec_vp9_cx(), config_,
672 config_->g_bit_depth == VPX_BITS_8 ? 0 : VPX_CODEC_USE_HIGHBITDEPTH);
673 if (rv != VPX_CODEC_OK) {
674 RTC_LOG(LS_ERROR) << "Init error: " << vpx_codec_err_to_string(rv);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000675 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
676 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000677 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_);
678 vpx_codec_control(encoder_, VP8E_SET_MAX_INTRA_BITRATE_PCT,
679 rc_max_intra_target_);
680 vpx_codec_control(encoder_, VP9E_SET_AQ_MODE,
hta257dc392016-10-25 09:05:06 -0700681 inst->VP9().adaptiveQpMode ? 3 : 0);
asaperssona9455ab2015-07-31 06:10:09 -0700682
jianj822e5932017-07-12 16:09:58 -0700683 vpx_codec_control(encoder_, VP9E_SET_FRAME_PARALLEL_DECODING, 0);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100684 vpx_codec_control(encoder_, VP9E_SET_SVC_GF_TEMPORAL_REF, 0);
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200685
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200686 if (is_svc_) {
687 vpx_codec_control(encoder_, VP9E_SET_SVC, 1);
688 vpx_codec_control(encoder_, VP9E_SET_SVC_PARAMETERS, &svc_params_);
asaperssona9455ab2015-07-31 06:10:09 -0700689 }
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200690
691 if (num_spatial_layers_ > 1) {
692 switch (inter_layer_pred_) {
693 case InterLayerPredMode::kOn:
694 vpx_codec_control(encoder_, VP9E_SET_SVC_INTER_LAYER_PRED, 0);
695 break;
696 case InterLayerPredMode::kOff:
697 vpx_codec_control(encoder_, VP9E_SET_SVC_INTER_LAYER_PRED, 1);
698 break;
699 case InterLayerPredMode::kOnKeyPic:
700 vpx_codec_control(encoder_, VP9E_SET_SVC_INTER_LAYER_PRED, 2);
701 break;
702 default:
703 RTC_NOTREACHED();
704 }
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200705
Ilya Nikolaevskiy039a7142019-05-24 16:50:00 +0200706 memset(&svc_drop_frame_, 0, sizeof(svc_drop_frame_));
Ilya Nikolaevskiy1af0f902019-09-09 10:16:18 +0200707 const bool reverse_constrained_drop_mode =
708 inter_layer_pred_ == InterLayerPredMode::kOn &&
709 codec_.mode == VideoCodecMode::kScreensharing &&
710 num_spatial_layers_ > 1;
711 if (reverse_constrained_drop_mode) {
712 // Screenshare dropping mode: drop a layer only together with all lower
713 // layers. This ensures that drops on lower layers won't reduce frame-rate
714 // for higher layers and reference structure is RTP-compatible.
715 svc_drop_frame_.framedrop_mode = CONSTRAINED_FROM_ABOVE_DROP;
Ilya Nikolaevskiy039a7142019-05-24 16:50:00 +0200716 svc_drop_frame_.max_consec_drop = 5;
Ilya Nikolaevskiy1af0f902019-09-09 10:16:18 +0200717 for (size_t i = 0; i < num_spatial_layers_; ++i) {
718 svc_drop_frame_.framedrop_thresh[i] = config_->rc_dropframe_thresh;
Ilya Nikolaevskiy039a7142019-05-24 16:50:00 +0200719 }
Ilya Nikolaevskiy1af0f902019-09-09 10:16:18 +0200720 // No buffering is needed because the highest layer is always present in
721 // all frames in CONSTRAINED_FROM_ABOVE drop mode.
722 layer_buffering_ = false;
Ilya Nikolaevskiy039a7142019-05-24 16:50:00 +0200723 } else {
724 // Configure encoder to drop entire superframe whenever it needs to drop
725 // a layer. This mode is preferred over per-layer dropping which causes
726 // quality flickering and is not compatible with RTP non-flexible mode.
727 svc_drop_frame_.framedrop_mode =
728 full_superframe_drop_ ? FULL_SUPERFRAME_DROP : CONSTRAINED_LAYER_DROP;
Ilya Nikolaevskiy1af0f902019-09-09 10:16:18 +0200729 // Buffering is needed only for constrained layer drop, as it's not clear
730 // which frame is the last.
731 layer_buffering_ = !full_superframe_drop_;
Ilya Nikolaevskiy039a7142019-05-24 16:50:00 +0200732 svc_drop_frame_.max_consec_drop = std::numeric_limits<int>::max();
733 for (size_t i = 0; i < num_spatial_layers_; ++i) {
734 svc_drop_frame_.framedrop_thresh[i] = config_->rc_dropframe_thresh;
735 }
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200736 }
Ilya Nikolaevskiy039a7142019-05-24 16:50:00 +0200737 vpx_codec_control(encoder_, VP9E_SET_SVC_FRAME_DROP_LAYER,
738 &svc_drop_frame_);
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200739 }
740
asaperssona9455ab2015-07-31 06:10:09 -0700741 // Register callback for getting each spatial layer.
742 vpx_codec_priv_output_cx_pkt_cb_pair_t cbp = {
philipelcce46fc2015-12-21 03:04:49 -0800743 VP9EncoderImpl::EncoderOutputCodedPacketCallback,
744 reinterpret_cast<void*>(this)};
745 vpx_codec_control(encoder_, VP9E_REGISTER_CX_CALLBACK,
746 reinterpret_cast<void*>(&cbp));
asaperssona9455ab2015-07-31 06:10:09 -0700747
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000748 // Control function to set the number of column tiles in encoding a frame, in
749 // log2 unit: e.g., 0 = 1 tile column, 1 = 2 tile columns, 2 = 4 tile columns.
750 // The number tile columns will be capped by the encoder based on image size
751 // (minimum width of tile column is 256 pixels, maximum is 4096).
752 vpx_codec_control(encoder_, VP9E_SET_TILE_COLUMNS, (config_->g_threads >> 1));
jianjcb5d1152017-03-28 23:56:08 -0700753
754 // Turn on row-based multithreading.
755 vpx_codec_control(encoder_, VP9E_SET_ROW_MT, 1);
jianj6bf57e32017-06-05 13:43:49 -0700756
Alex Glaznevfecb7c32016-03-31 14:23:27 -0700757#if !defined(WEBRTC_ARCH_ARM) && !defined(WEBRTC_ARCH_ARM64) && \
Yves Gerey665174f2018-06-19 15:03:05 +0200758 !defined(ANDROID)
jianj6bf57e32017-06-05 13:43:49 -0700759 // Do not enable the denoiser on ARM since optimization is pending.
760 // Denoiser is on by default on other platforms.
marpan@webrtc.org16a87b92015-03-05 22:19:00 +0000761 vpx_codec_control(encoder_, VP9E_SET_NOISE_SENSITIVITY,
hta257dc392016-10-25 09:05:06 -0700762 inst->VP9().denoisingOn ? 1 : 0);
marpan@webrtc.org16a87b92015-03-05 22:19:00 +0000763#endif
jianj6bf57e32017-06-05 13:43:49 -0700764
Niels Möllere3cf3d02018-06-13 11:52:16 +0200765 if (codec_.mode == VideoCodecMode::kScreensharing) {
ivica242d6382015-09-04 06:13:23 -0700766 // Adjust internal parameters to screen content.
767 vpx_codec_control(encoder_, VP9E_SET_TUNE_CONTENT, 1);
ivica242d6382015-09-04 06:13:23 -0700768 }
Marco2520e722015-09-16 14:05:00 -0700769 // Enable encoder skip of static/low content blocks.
770 vpx_codec_control(encoder_, VP8E_SET_STATIC_THRESHOLD, 1);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000771 inited_ = true;
772 return WEBRTC_VIDEO_CODEC_OK;
773}
774
775uint32_t VP9EncoderImpl::MaxIntraTarget(uint32_t optimal_buffer_size) {
776 // Set max to the optimal buffer level (normalized by target BR),
777 // and scaled by a scale_par.
778 // Max target size = scale_par * optimal_buffer_size * targetBR[Kbps].
779 // This value is presented in percentage of perFrameBw:
780 // perFrameBw = targetBR[Kbps] * 1000 / framerate.
781 // The target in % is as follows:
782 float scale_par = 0.5;
783 uint32_t target_pct =
784 optimal_buffer_size * scale_par * codec_.maxFramerate / 10;
785 // Don't go below 3 times the per frame bandwidth.
786 const uint32_t min_intra_size = 300;
philipelcce46fc2015-12-21 03:04:49 -0800787 return (target_pct < min_intra_size) ? min_intra_size : target_pct;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000788}
789
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700790int VP9EncoderImpl::Encode(const VideoFrame& input_image,
Niels Möller87e2d782019-03-07 10:18:23 +0100791 const std::vector<VideoFrameType>* frame_types) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000792 if (!inited_) {
793 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
794 }
sprang3958ed82017-08-17 08:12:10 -0700795 if (encoded_complete_callback_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000796 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
797 }
Erik Språngbfa5d5d2019-02-04 16:40:19 +0100798 if (num_active_spatial_layers_ == 0) {
799 // All spatial layers are disabled, return without encoding anything.
800 return WEBRTC_VIDEO_CODEC_OK;
801 }
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200802
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000803 // We only support one stream at the moment.
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200804 if (frame_types && !frame_types->empty()) {
Niels Möller8f7ce222019-03-21 15:43:58 +0100805 if ((*frame_types)[0] == VideoFrameType::kVideoFrameKey) {
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200806 force_key_frame_ = true;
807 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000808 }
Sergey Silkind902d582018-05-18 17:31:19 +0200809
Ilya Nikolaevskiy2ec0c652019-01-18 11:56:48 +0100810 if (pics_since_key_ + 1 ==
811 static_cast<size_t>(codec_.VP9()->keyFrameInterval)) {
812 force_key_frame_ = true;
813 }
814
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100815 vpx_svc_layer_id_t layer_id = {0};
816 if (!force_key_frame_) {
Patrik Höglundd4d254f2018-11-29 15:09:31 +0000817 const size_t gof_idx = (pics_since_key_ + 1) % gof_.num_frames_in_gof;
818 layer_id.temporal_layer_id = gof_.temporal_idx[gof_idx];
Sergey Silkin96f2c972018-09-05 21:07:17 +0200819
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100820 if (VideoCodecMode::kScreensharing == codec_.mode) {
821 const uint32_t frame_timestamp_ms =
822 1000 * input_image.timestamp() / kVideoPayloadTypeFrequency;
Sergey Silkin96f2c972018-09-05 21:07:17 +0200823
Ilya Nikolaevskiy61170682019-03-06 16:04:32 +0100824 // To ensure that several rate-limiters with different limits don't
825 // interfere, they must be queried in order of increasing limit.
826
827 bool use_steady_state_limiter =
828 variable_framerate_experiment_.enabled &&
829 input_image.update_rect().IsEmpty() &&
830 num_steady_state_frames_ >=
831 variable_framerate_experiment_.frames_before_steady_state;
832
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100833 for (uint8_t sl_idx = 0; sl_idx < num_active_spatial_layers_; ++sl_idx) {
Ilya Nikolaevskiy61170682019-03-06 16:04:32 +0100834 const float layer_fps =
835 framerate_controller_[layer_id.spatial_layer_id].GetTargetRate();
836 // Use steady state rate-limiter at the correct place.
837 if (use_steady_state_limiter &&
838 layer_fps > variable_framerate_experiment_.framerate_limit - 1e-9) {
839 if (variable_framerate_controller_.DropFrame(frame_timestamp_ms)) {
840 layer_id.spatial_layer_id = num_active_spatial_layers_;
841 }
842 // Break always: if rate limiter triggered frame drop, no need to
843 // continue; otherwise, the rate is less than the next limiters.
844 break;
845 }
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100846 if (framerate_controller_[sl_idx].DropFrame(frame_timestamp_ms)) {
847 ++layer_id.spatial_layer_id;
848 } else {
849 break;
850 }
Sergey Silkin96f2c972018-09-05 21:07:17 +0200851 }
Ilya Nikolaevskiy61170682019-03-06 16:04:32 +0100852
853 if (use_steady_state_limiter &&
854 layer_id.spatial_layer_id < num_active_spatial_layers_) {
855 variable_framerate_controller_.AddFrame(frame_timestamp_ms);
856 }
Sergey Silkin96f2c972018-09-05 21:07:17 +0200857 }
Patrik Höglundd4d254f2018-11-29 15:09:31 +0000858
859 RTC_DCHECK_LE(layer_id.spatial_layer_id, num_active_spatial_layers_);
860 if (layer_id.spatial_layer_id >= num_active_spatial_layers_) {
861 // Drop entire picture.
862 return WEBRTC_VIDEO_CODEC_OK;
863 }
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100864 }
Patrik Höglundd4d254f2018-11-29 15:09:31 +0000865
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100866 for (int sl_idx = 0; sl_idx < num_active_spatial_layers_; ++sl_idx) {
867 layer_id.temporal_layer_id_per_spatial[sl_idx] = layer_id.temporal_layer_id;
868 }
869
870 vpx_codec_control(encoder_, VP9E_SET_SVC_LAYER_ID, &layer_id);
871
Erik Språng7a3fe892019-04-15 12:22:55 +0200872 if (requested_rate_settings_) {
873 if (dynamic_rate_settings_) {
874 // Tweak rate control settings based on available network headroom.
875 UpdateRateSettings(
876 config_,
877 GetRateSettings(
878 requested_rate_settings_->bandwidth_allocation.bps<double>() /
879 requested_rate_settings_->bitrate.get_sum_bps()));
880 }
881
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100882 bool more_layers_requested = MoreLayersEnabled(
Erik Språng7a3fe892019-04-15 12:22:55 +0200883 requested_rate_settings_->bitrate, current_bitrate_allocation_);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100884 bool less_layers_requested = MoreLayersEnabled(
Erik Språng7a3fe892019-04-15 12:22:55 +0200885 current_bitrate_allocation_, requested_rate_settings_->bitrate);
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100886 // In SVC can enable new layers only if all lower layers are encoded and at
887 // the base temporal layer.
888 // This will delay rate allocation change until the next frame on the base
889 // spatial layer.
890 // In KSVC or simulcast modes KF will be generated for a new layer, so can
891 // update allocation any time.
892 bool can_upswitch =
893 inter_layer_pred_ != InterLayerPredMode::kOn ||
894 (layer_id.spatial_layer_id == 0 && layer_id.temporal_layer_id == 0);
895 if (!more_layers_requested || can_upswitch) {
Erik Språng7a3fe892019-04-15 12:22:55 +0200896 current_bitrate_allocation_ = requested_rate_settings_->bitrate;
897 requested_rate_settings_ = absl::nullopt;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100898 if (!SetSvcRates(current_bitrate_allocation_)) {
899 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
900 }
901 if (less_layers_requested || more_layers_requested) {
902 ss_info_needed_ = true;
903 }
Ilya Nikolaevskiy039a7142019-05-24 16:50:00 +0200904 if (more_layers_requested && !force_key_frame_) {
905 // Prohibit drop of all layers for the next frame, so newly enabled
906 // layer would have a valid spatial reference.
907 for (size_t i = 0; i < num_spatial_layers_; ++i) {
908 svc_drop_frame_.framedrop_thresh[i] = 0;
909 }
910 }
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100911 }
912 }
913
Ilya Nikolaevskiy039a7142019-05-24 16:50:00 +0200914 if (num_spatial_layers_ > 1) {
915 // Update frame dropping settings as they may change on per-frame basis.
916 vpx_codec_control(encoder_, VP9E_SET_SVC_FRAME_DROP_LAYER,
917 &svc_drop_frame_);
918 }
919
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100920 if (vpx_codec_enc_config_set(encoder_, config_)) {
921 return WEBRTC_VIDEO_CODEC_ERROR;
Sergey Silkind902d582018-05-18 17:31:19 +0200922 }
923
kwiberg352444f2016-11-28 15:58:53 -0800924 RTC_DCHECK_EQ(input_image.width(), raw_->d_w);
925 RTC_DCHECK_EQ(input_image.height(), raw_->d_h);
asaperssona9455ab2015-07-31 06:10:09 -0700926
927 // Set input image for use in the callback.
928 // This was necessary since you need some information from input_image.
929 // You can save only the necessary information (such as timestamp) instead of
930 // doing this.
931 input_image_ = &input_image;
932
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700933 // Keep reference to buffer until encode completes.
934 rtc::scoped_refptr<I420BufferInterface> i420_buffer;
Ilya Nikolaevskiya8507e32019-05-03 11:39:26 +0200935 const I010BufferInterface* i010_buffer;
936 rtc::scoped_refptr<const I010BufferInterface> i010_copy;
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700937 switch (profile_) {
938 case VP9Profile::kProfile0: {
939 i420_buffer = input_image.video_frame_buffer()->ToI420();
940 // Image in vpx_image_t format.
941 // Input image is const. VPX's raw image is not defined as const.
942 raw_->planes[VPX_PLANE_Y] = const_cast<uint8_t*>(i420_buffer->DataY());
943 raw_->planes[VPX_PLANE_U] = const_cast<uint8_t*>(i420_buffer->DataU());
944 raw_->planes[VPX_PLANE_V] = const_cast<uint8_t*>(i420_buffer->DataV());
945 raw_->stride[VPX_PLANE_Y] = i420_buffer->StrideY();
946 raw_->stride[VPX_PLANE_U] = i420_buffer->StrideU();
947 raw_->stride[VPX_PLANE_V] = i420_buffer->StrideV();
948 break;
949 }
950 case VP9Profile::kProfile2: {
951 // We can inject kI010 frames directly for encode. All other formats
952 // should be converted to it.
953 switch (input_image.video_frame_buffer()->type()) {
954 case VideoFrameBuffer::Type::kI010: {
955 i010_buffer = input_image.video_frame_buffer()->GetI010();
956 break;
957 }
958 default: {
Ilya Nikolaevskiya8507e32019-05-03 11:39:26 +0200959 i010_copy =
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700960 I010Buffer::Copy(*input_image.video_frame_buffer()->ToI420());
Ilya Nikolaevskiya8507e32019-05-03 11:39:26 +0200961 i010_buffer = i010_copy.get();
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700962 }
963 }
964 raw_->planes[VPX_PLANE_Y] = const_cast<uint8_t*>(
965 reinterpret_cast<const uint8_t*>(i010_buffer->DataY()));
966 raw_->planes[VPX_PLANE_U] = const_cast<uint8_t*>(
967 reinterpret_cast<const uint8_t*>(i010_buffer->DataU()));
968 raw_->planes[VPX_PLANE_V] = const_cast<uint8_t*>(
969 reinterpret_cast<const uint8_t*>(i010_buffer->DataV()));
970 raw_->stride[VPX_PLANE_Y] = i010_buffer->StrideY() * 2;
971 raw_->stride[VPX_PLANE_U] = i010_buffer->StrideU() * 2;
972 raw_->stride[VPX_PLANE_V] = i010_buffer->StrideV() * 2;
973 break;
974 }
975 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000976
philipelcfc319b2015-11-10 07:17:23 -0800977 vpx_enc_frame_flags_t flags = 0;
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200978 if (force_key_frame_) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000979 flags = VPX_EFLAG_FORCE_KF;
980 }
philipelcfc319b2015-11-10 07:17:23 -0800981
Sergey Silkin390f3582018-10-01 17:13:50 +0200982 if (external_ref_control_) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +0100983 vpx_svc_ref_frame_config_t ref_config =
984 SetReferences(force_key_frame_, layer_id.spatial_layer_id);
Sergey Silkina85995a2018-10-02 10:28:10 +0200985
986 if (VideoCodecMode::kScreensharing == codec_.mode) {
987 for (uint8_t sl_idx = 0; sl_idx < num_active_spatial_layers_; ++sl_idx) {
988 ref_config.duration[sl_idx] = static_cast<int64_t>(
Ilya Nikolaevskiy5c18a5f2019-05-24 11:51:31 +0200989 90000 / (std::min(static_cast<float>(codec_.maxFramerate),
990 framerate_controller_[sl_idx].GetTargetRate())));
Sergey Silkina85995a2018-10-02 10:28:10 +0200991 }
992 }
993
Sergey Silkin390f3582018-10-01 17:13:50 +0200994 vpx_codec_control(encoder_, VP9E_SET_SVC_REF_FRAME_CONFIG, &ref_config);
995 }
996
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100997 first_frame_in_picture_ = true;
998
Sergey Silkin96f2c972018-09-05 21:07:17 +0200999 // TODO(ssilkin): Frame duration should be specified per spatial layer
1000 // since their frame rate can be different. For now calculate frame duration
1001 // based on target frame rate of the highest spatial layer, which frame rate
1002 // is supposed to be equal or higher than frame rate of low spatial layers.
1003 // Also, timestamp should represent actual time passed since previous frame
1004 // (not 'expected' time). Then rate controller can drain buffer more
1005 // accurately.
1006 RTC_DCHECK_GE(framerate_controller_.size(), num_active_spatial_layers_);
Sergey Silkinf87bb462018-09-17 09:37:37 +02001007 float target_framerate_fps =
1008 (codec_.mode == VideoCodecMode::kScreensharing)
Ilya Nikolaevskiy5c18a5f2019-05-24 11:51:31 +02001009 ? std::min(static_cast<float>(codec_.maxFramerate),
1010 framerate_controller_[num_active_spatial_layers_ - 1]
1011 .GetTargetRate())
Sergey Silkinf87bb462018-09-17 09:37:37 +02001012 : codec_.maxFramerate;
1013 uint32_t duration = static_cast<uint32_t>(90000 / target_framerate_fps);
Emircan Uysaler0823eec2018-07-13 17:10:00 -07001014 const vpx_codec_err_t rv = vpx_codec_encode(encoder_, raw_, timestamp_,
1015 duration, flags, VPX_DL_REALTIME);
1016 if (rv != VPX_CODEC_OK) {
1017 RTC_LOG(LS_ERROR) << "Encoding error: " << vpx_codec_err_to_string(rv)
1018 << "\n"
1019 << "Details: " << vpx_codec_error(encoder_) << "\n"
1020 << vpx_codec_error_detail(encoder_);
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001021 return WEBRTC_VIDEO_CODEC_ERROR;
1022 }
1023 timestamp_ += duration;
asaperssona9455ab2015-07-31 06:10:09 -07001024
Ilya Nikolaevskiy1af0f902019-09-09 10:16:18 +02001025 if (layer_buffering_) {
Sergey Silkin88ce4ef2018-11-23 13:59:24 +01001026 const bool end_of_picture = true;
1027 DeliverBufferedFrame(end_of_picture);
1028 }
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001029
asaperssona9455ab2015-07-31 06:10:09 -07001030 return WEBRTC_VIDEO_CODEC_OK;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001031}
1032
1033void VP9EncoderImpl::PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
Niels Möllerd3b8c632018-08-27 15:33:42 +02001034 absl::optional<int>* spatial_idx,
philipelcce46fc2015-12-21 03:04:49 -08001035 const vpx_codec_cx_pkt& pkt,
Sergey Silkin88ce4ef2018-11-23 13:59:24 +01001036 uint32_t timestamp) {
sprang3958ed82017-08-17 08:12:10 -07001037 RTC_CHECK(codec_specific != nullptr);
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001038 codec_specific->codecType = kVideoCodecVP9;
philipelcce46fc2015-12-21 03:04:49 -08001039 CodecSpecificInfoVP9* vp9_info = &(codec_specific->codecSpecific.VP9);
Sergey Silkin07f80cc2018-04-09 13:11:59 +02001040
Sergey Silkin88ce4ef2018-11-23 13:59:24 +01001041 vp9_info->first_frame_in_picture = first_frame_in_picture_;
Sergey Silkin738b7e92018-08-23 16:37:27 +02001042 vp9_info->flexible_mode = is_flexible_mode_;
asaperssona9455ab2015-07-31 06:10:09 -07001043
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001044 if (pkt.data.frame.flags & VPX_FRAME_IS_KEY) {
1045 pics_since_key_ = 0;
1046 } else if (first_frame_in_picture_) {
1047 ++pics_since_key_;
1048 }
1049
asaperssona9455ab2015-07-31 06:10:09 -07001050 vpx_svc_layer_id_t layer_id = {0};
1051 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
1052
Ilya Nikolaevskiy2ec0c652019-01-18 11:56:48 +01001053 // Can't have keyframe with non-zero temporal layer.
1054 RTC_DCHECK(pics_since_key_ != 0 || layer_id.temporal_layer_id == 0);
1055
sprang3958ed82017-08-17 08:12:10 -07001056 RTC_CHECK_GT(num_temporal_layers_, 0);
“Michael23c5a992018-06-21 11:07:21 -05001057 RTC_CHECK_GT(num_active_spatial_layers_, 0);
asaperssona9455ab2015-07-31 06:10:09 -07001058 if (num_temporal_layers_ == 1) {
sprang3958ed82017-08-17 08:12:10 -07001059 RTC_CHECK_EQ(layer_id.temporal_layer_id, 0);
asaperssona9455ab2015-07-31 06:10:09 -07001060 vp9_info->temporal_idx = kNoTemporalIdx;
1061 } else {
1062 vp9_info->temporal_idx = layer_id.temporal_layer_id;
1063 }
“Michael23c5a992018-06-21 11:07:21 -05001064 if (num_active_spatial_layers_ == 1) {
sprang3958ed82017-08-17 08:12:10 -07001065 RTC_CHECK_EQ(layer_id.spatial_layer_id, 0);
Niels Möllerd3b8c632018-08-27 15:33:42 +02001066 *spatial_idx = absl::nullopt;
asaperssona9455ab2015-07-31 06:10:09 -07001067 } else {
Niels Möllerd3b8c632018-08-27 15:33:42 +02001068 *spatial_idx = layer_id.spatial_layer_id;
asaperssona9455ab2015-07-31 06:10:09 -07001069 }
asaperssona9455ab2015-07-31 06:10:09 -07001070
asaperssona9455ab2015-07-31 06:10:09 -07001071 // TODO(asapersson): this info has to be obtained from the encoder.
asaperssoncb50c962015-11-18 01:58:55 -08001072 vp9_info->temporal_up_switch = false;
asaperssona9455ab2015-07-31 06:10:09 -07001073
Sergey Silkin6a8f30e2018-04-26 11:03:49 +02001074 const bool is_key_pic = (pics_since_key_ == 0);
1075 const bool is_inter_layer_pred_allowed =
1076 (inter_layer_pred_ == InterLayerPredMode::kOn ||
1077 (inter_layer_pred_ == InterLayerPredMode::kOnKeyPic && is_key_pic));
1078
1079 // Always set inter_layer_predicted to true on high layer frame if inter-layer
1080 // prediction (ILP) is allowed even if encoder didn't actually use it.
1081 // Setting inter_layer_predicted to false would allow receiver to decode high
1082 // layer frame without decoding low layer frame. If that would happen (e.g.
1083 // if low layer frame is lost) then receiver won't be able to decode next high
1084 // layer frame which uses ILP.
1085 vp9_info->inter_layer_predicted =
Sergey Silkin88ce4ef2018-11-23 13:59:24 +01001086 first_frame_in_picture_ ? false : is_inter_layer_pred_allowed;
Sergey Silkin6a8f30e2018-04-26 11:03:49 +02001087
Sergey Silkinc5f152d2018-09-26 13:28:50 +02001088 // Mark all low spatial layer frames as references (not just frames of
1089 // active low spatial layers) if inter-layer prediction is enabled since
1090 // these frames are indirect references of high spatial layer, which can
1091 // later be enabled without key frame.
Sergey Silkin6a8f30e2018-04-26 11:03:49 +02001092 vp9_info->non_ref_for_inter_layer_pred =
Sergey Silkinc5f152d2018-09-26 13:28:50 +02001093 !is_inter_layer_pred_allowed ||
1094 layer_id.spatial_layer_id + 1 == num_spatial_layers_;
asapersson00ac85e2015-11-11 05:30:48 -08001095
ivica7f6a6fc2015-09-08 02:40:29 -07001096 // Always populate this, so that the packetizer can properly set the marker
1097 // bit.
“Michael23c5a992018-06-21 11:07:21 -05001098 vp9_info->num_spatial_layers = num_active_spatial_layers_;
philipelcfc319b2015-11-10 07:17:23 -08001099
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001100 vp9_info->num_ref_pics = 0;
Ilya Nikolaevskiy2ec0c652019-01-18 11:56:48 +01001101 FillReferenceIndices(pkt, pics_since_key_, vp9_info->inter_layer_predicted,
1102 vp9_info);
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001103 if (vp9_info->flexible_mode) {
1104 vp9_info->gof_idx = kNoGofIdx;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001105 } else {
1106 vp9_info->gof_idx =
1107 static_cast<uint8_t>(pics_since_key_ % gof_.num_frames_in_gof);
1108 vp9_info->temporal_up_switch = gof_.temporal_up_switch[vp9_info->gof_idx];
Ilya Nikolaevskiy2ec0c652019-01-18 11:56:48 +01001109 RTC_DCHECK(vp9_info->num_ref_pics == gof_.num_ref_pics[vp9_info->gof_idx] ||
1110 vp9_info->num_ref_pics == 0);
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001111 }
1112
1113 vp9_info->inter_pic_predicted = (!is_key_pic && vp9_info->num_ref_pics > 0);
1114
Sergey Silkina323cc02019-04-23 10:09:28 +02001115 // Write SS on key frame of independently coded spatial layers and on base
1116 // temporal/spatial layer frame if number of layers changed without issuing
1117 // of key picture (inter-layer prediction is enabled).
1118 const bool is_key_frame = is_key_pic && !vp9_info->inter_layer_predicted;
1119 if (is_key_frame || (ss_info_needed_ && layer_id.temporal_layer_id == 0 &&
1120 layer_id.spatial_layer_id == 0)) {
1121 vp9_info->ss_data_available = true;
asaperssona9455ab2015-07-31 06:10:09 -07001122 vp9_info->spatial_layer_resolution_present = true;
“Michael23c5a992018-06-21 11:07:21 -05001123 for (size_t i = 0; i < num_active_spatial_layers_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +02001124 vp9_info->width[i] = codec_.width * svc_params_.scaling_factor_num[i] /
johannkoenig8225c402017-01-26 13:23:44 -08001125 svc_params_.scaling_factor_den[i];
Yves Gerey665174f2018-06-19 15:03:05 +02001126 vp9_info->height[i] = codec_.height * svc_params_.scaling_factor_num[i] /
johannkoenig8225c402017-01-26 13:23:44 -08001127 svc_params_.scaling_factor_den[i];
asaperssona9455ab2015-07-31 06:10:09 -07001128 }
Sergey Silkin738b7e92018-08-23 16:37:27 +02001129 if (vp9_info->flexible_mode) {
1130 vp9_info->gof.num_frames_in_gof = 0;
1131 } else {
asaperssona9455ab2015-07-31 06:10:09 -07001132 vp9_info->gof.CopyGofInfoVP9(gof_);
1133 }
Sergey Silkina323cc02019-04-23 10:09:28 +02001134
1135 ss_info_needed_ = false;
1136 } else {
1137 vp9_info->ss_data_available = false;
asaperssona9455ab2015-07-31 06:10:09 -07001138 }
Sergey Silkin88ce4ef2018-11-23 13:59:24 +01001139
1140 first_frame_in_picture_ = false;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001141}
1142
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001143void VP9EncoderImpl::FillReferenceIndices(const vpx_codec_cx_pkt& pkt,
1144 const size_t pic_num,
1145 const bool inter_layer_predicted,
1146 CodecSpecificInfoVP9* vp9_info) {
1147 vpx_svc_layer_id_t layer_id = {0};
1148 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
1149
Sergey Silkin76be2952018-08-21 12:30:33 +02001150 const bool is_key_frame =
1151 (pkt.data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001152
1153 std::vector<RefFrameBuffer> ref_buf_list;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001154
Sergey Silkin76be2952018-08-21 12:30:33 +02001155 if (is_svc_) {
1156 vpx_svc_ref_frame_config_t enc_layer_conf = {{0}};
1157 vpx_codec_control(encoder_, VP9E_GET_SVC_REF_FRAME_CONFIG, &enc_layer_conf);
Sergey Silkin38fabff2019-03-21 10:08:40 +01001158 int ref_buf_flags = 0;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001159
Sergey Silkin76be2952018-08-21 12:30:33 +02001160 if (enc_layer_conf.reference_last[layer_id.spatial_layer_id]) {
1161 const size_t fb_idx =
1162 enc_layer_conf.lst_fb_idx[layer_id.spatial_layer_id];
1163 RTC_DCHECK(ref_buf_.find(fb_idx) != ref_buf_.end());
Sergey Silkin82276592018-08-27 14:54:20 +02001164 if (std::find(ref_buf_list.begin(), ref_buf_list.end(),
1165 ref_buf_.at(fb_idx)) == ref_buf_list.end()) {
1166 ref_buf_list.push_back(ref_buf_.at(fb_idx));
Sergey Silkin38fabff2019-03-21 10:08:40 +01001167 ref_buf_flags |= 1 << fb_idx;
Sergey Silkin82276592018-08-27 14:54:20 +02001168 }
Sergey Silkin76be2952018-08-21 12:30:33 +02001169 }
1170
1171 if (enc_layer_conf.reference_alt_ref[layer_id.spatial_layer_id]) {
1172 const size_t fb_idx =
1173 enc_layer_conf.alt_fb_idx[layer_id.spatial_layer_id];
1174 RTC_DCHECK(ref_buf_.find(fb_idx) != ref_buf_.end());
Sergey Silkin82276592018-08-27 14:54:20 +02001175 if (std::find(ref_buf_list.begin(), ref_buf_list.end(),
1176 ref_buf_.at(fb_idx)) == ref_buf_list.end()) {
1177 ref_buf_list.push_back(ref_buf_.at(fb_idx));
Sergey Silkin38fabff2019-03-21 10:08:40 +01001178 ref_buf_flags |= 1 << fb_idx;
Sergey Silkin82276592018-08-27 14:54:20 +02001179 }
Sergey Silkin76be2952018-08-21 12:30:33 +02001180 }
1181
1182 if (enc_layer_conf.reference_golden[layer_id.spatial_layer_id]) {
1183 const size_t fb_idx =
1184 enc_layer_conf.gld_fb_idx[layer_id.spatial_layer_id];
1185 RTC_DCHECK(ref_buf_.find(fb_idx) != ref_buf_.end());
Sergey Silkin82276592018-08-27 14:54:20 +02001186 if (std::find(ref_buf_list.begin(), ref_buf_list.end(),
1187 ref_buf_.at(fb_idx)) == ref_buf_list.end()) {
1188 ref_buf_list.push_back(ref_buf_.at(fb_idx));
Sergey Silkin38fabff2019-03-21 10:08:40 +01001189 ref_buf_flags |= 1 << fb_idx;
Sergey Silkin82276592018-08-27 14:54:20 +02001190 }
Sergey Silkin76be2952018-08-21 12:30:33 +02001191 }
Sergey Silkin38fabff2019-03-21 10:08:40 +01001192
1193 RTC_LOG(LS_VERBOSE) << "Frame " << pic_num << " sl "
1194 << layer_id.spatial_layer_id << " tl "
1195 << layer_id.temporal_layer_id << " refered buffers "
1196 << (ref_buf_flags & (1 << 0) ? 1 : 0)
1197 << (ref_buf_flags & (1 << 1) ? 1 : 0)
1198 << (ref_buf_flags & (1 << 2) ? 1 : 0)
1199 << (ref_buf_flags & (1 << 3) ? 1 : 0)
1200 << (ref_buf_flags & (1 << 4) ? 1 : 0)
1201 << (ref_buf_flags & (1 << 5) ? 1 : 0)
1202 << (ref_buf_flags & (1 << 6) ? 1 : 0)
1203 << (ref_buf_flags & (1 << 7) ? 1 : 0);
1204
Sergey Silkin76be2952018-08-21 12:30:33 +02001205 } else if (!is_key_frame) {
1206 RTC_DCHECK_EQ(num_spatial_layers_, 1);
1207 RTC_DCHECK_EQ(num_temporal_layers_, 1);
1208 // In non-SVC mode encoder doesn't provide reference list. Assume each frame
1209 // refers previous one, which is stored in buffer 0.
1210 ref_buf_list.push_back(ref_buf_.at(0));
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001211 }
1212
1213 size_t max_ref_temporal_layer_id = 0;
1214
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001215 std::vector<size_t> ref_pid_list;
1216
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001217 vp9_info->num_ref_pics = 0;
1218 for (const RefFrameBuffer& ref_buf : ref_buf_list) {
1219 RTC_DCHECK_LE(ref_buf.pic_num, pic_num);
1220 if (ref_buf.pic_num < pic_num) {
1221 if (inter_layer_pred_ != InterLayerPredMode::kOn) {
1222 // RTP spec limits temporal prediction to the same spatial layer.
1223 // It is safe to ignore this requirement if inter-layer prediction is
1224 // enabled for all frames when all base frames are relayed to receiver.
1225 RTC_DCHECK_EQ(ref_buf.spatial_layer_id, layer_id.spatial_layer_id);
Ilya Nikolaevskiy5c18a5f2019-05-24 11:51:31 +02001226 } else {
1227 RTC_DCHECK_LE(ref_buf.spatial_layer_id, layer_id.spatial_layer_id);
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001228 }
1229 RTC_DCHECK_LE(ref_buf.temporal_layer_id, layer_id.temporal_layer_id);
1230
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001231 // Encoder may reference several spatial layers on the same previous
1232 // frame in case if some spatial layers are skipped on the current frame.
1233 // We shouldn't put duplicate references as it may break some old
1234 // clients and isn't RTP compatible.
1235 if (std::find(ref_pid_list.begin(), ref_pid_list.end(),
1236 ref_buf.pic_num) != ref_pid_list.end()) {
1237 continue;
1238 }
1239 ref_pid_list.push_back(ref_buf.pic_num);
1240
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001241 const size_t p_diff = pic_num - ref_buf.pic_num;
1242 RTC_DCHECK_LE(p_diff, 127UL);
1243
1244 vp9_info->p_diff[vp9_info->num_ref_pics] = static_cast<uint8_t>(p_diff);
1245 ++vp9_info->num_ref_pics;
1246
1247 max_ref_temporal_layer_id =
1248 std::max(max_ref_temporal_layer_id, ref_buf.temporal_layer_id);
1249 } else {
1250 RTC_DCHECK(inter_layer_predicted);
1251 // RTP spec only allows to use previous spatial layer for inter-layer
1252 // prediction.
1253 RTC_DCHECK_EQ(ref_buf.spatial_layer_id + 1, layer_id.spatial_layer_id);
1254 }
1255 }
1256
1257 vp9_info->temporal_up_switch =
1258 (max_ref_temporal_layer_id <
1259 static_cast<size_t>(layer_id.temporal_layer_id));
1260}
1261
1262void VP9EncoderImpl::UpdateReferenceBuffers(const vpx_codec_cx_pkt& pkt,
1263 const size_t pic_num) {
1264 vpx_svc_layer_id_t layer_id = {0};
1265 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
1266
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001267 RefFrameBuffer frame_buf(pic_num, layer_id.spatial_layer_id,
1268 layer_id.temporal_layer_id);
1269
Sergey Silkin38fabff2019-03-21 10:08:40 +01001270 if (is_svc_) {
Sergey Silkin76be2952018-08-21 12:30:33 +02001271 vpx_svc_ref_frame_config_t enc_layer_conf = {{0}};
1272 vpx_codec_control(encoder_, VP9E_GET_SVC_REF_FRAME_CONFIG, &enc_layer_conf);
Sergey Silkin38fabff2019-03-21 10:08:40 +01001273 const int update_buffer_slot =
1274 enc_layer_conf.update_buffer_slot[layer_id.spatial_layer_id];
Sergey Silkin76be2952018-08-21 12:30:33 +02001275
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001276 for (size_t i = 0; i < kNumVp9Buffers; ++i) {
Sergey Silkin38fabff2019-03-21 10:08:40 +01001277 if (update_buffer_slot & (1 << i)) {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001278 ref_buf_[i] = frame_buf;
1279 }
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001280 }
Sergey Silkin38fabff2019-03-21 10:08:40 +01001281
1282 RTC_LOG(LS_VERBOSE) << "Frame " << pic_num << " sl "
1283 << layer_id.spatial_layer_id << " tl "
1284 << layer_id.temporal_layer_id << " updated buffers "
1285 << (update_buffer_slot & (1 << 0) ? 1 : 0)
1286 << (update_buffer_slot & (1 << 1) ? 1 : 0)
1287 << (update_buffer_slot & (1 << 2) ? 1 : 0)
1288 << (update_buffer_slot & (1 << 3) ? 1 : 0)
1289 << (update_buffer_slot & (1 << 4) ? 1 : 0)
1290 << (update_buffer_slot & (1 << 5) ? 1 : 0)
1291 << (update_buffer_slot & (1 << 6) ? 1 : 0)
1292 << (update_buffer_slot & (1 << 7) ? 1 : 0);
Sergey Silkin76be2952018-08-21 12:30:33 +02001293 } else {
1294 RTC_DCHECK_EQ(num_spatial_layers_, 1);
1295 RTC_DCHECK_EQ(num_temporal_layers_, 1);
1296 // In non-svc mode encoder doesn't provide reference list. Assume each frame
1297 // is reference and stored in buffer 0.
1298 ref_buf_[0] = frame_buf;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001299 }
1300}
1301
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001302vpx_svc_ref_frame_config_t VP9EncoderImpl::SetReferences(
1303 bool is_key_pic,
1304 size_t first_active_spatial_layer_id) {
Sergey Silkin390f3582018-10-01 17:13:50 +02001305 // kRefBufIdx, kUpdBufIdx need to be updated to support longer GOFs.
1306 RTC_DCHECK_LE(gof_.num_frames_in_gof, 4);
1307
1308 vpx_svc_ref_frame_config_t ref_config;
1309 memset(&ref_config, 0, sizeof(ref_config));
1310
1311 const size_t num_temporal_refs = std::max(1, num_temporal_layers_ - 1);
1312 const bool is_inter_layer_pred_allowed =
1313 inter_layer_pred_ == InterLayerPredMode::kOn ||
1314 (inter_layer_pred_ == InterLayerPredMode::kOnKeyPic && is_key_pic);
1315 absl::optional<int> last_updated_buf_idx;
1316
1317 // Put temporal reference to LAST and spatial reference to GOLDEN. Update
1318 // frame buffer (i.e. store encoded frame) if current frame is a temporal
1319 // reference (i.e. it belongs to a low temporal layer) or it is a spatial
1320 // reference. In later case, always store spatial reference in the last
1321 // reference frame buffer.
1322 // For the case of 3 temporal and 3 spatial layers we need 6 frame buffers
1323 // for temporal references plus 1 buffer for spatial reference. 7 buffers
1324 // in total.
1325
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001326 for (size_t sl_idx = first_active_spatial_layer_id;
1327 sl_idx < num_active_spatial_layers_; ++sl_idx) {
1328 const size_t curr_pic_num = is_key_pic ? 0 : pics_since_key_ + 1;
1329 const size_t gof_idx = curr_pic_num % gof_.num_frames_in_gof;
Sergey Silkin390f3582018-10-01 17:13:50 +02001330
1331 if (!is_key_pic) {
1332 // Set up temporal reference.
1333 const int buf_idx = sl_idx * num_temporal_refs + kRefBufIdx[gof_idx];
1334
1335 // Last reference frame buffer is reserved for spatial reference. It is
1336 // not supposed to be used for temporal prediction.
1337 RTC_DCHECK_LT(buf_idx, kNumVp9Buffers - 1);
1338
Ilya Nikolaevskiy30323e22019-09-06 13:12:52 +02001339 const int pid_diff = curr_pic_num - ref_buf_[buf_idx].pic_num;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001340 // Incorrect spatial layer may be in the buffer due to a key-frame.
1341 const bool same_spatial_layer =
1342 ref_buf_[buf_idx].spatial_layer_id == sl_idx;
1343 bool correct_pid = false;
Ilya Nikolaevskiy5c18a5f2019-05-24 11:51:31 +02001344 if (is_flexible_mode_) {
Ilya Nikolaevskiy30323e22019-09-06 13:12:52 +02001345 correct_pid = pid_diff > 0 && pid_diff < kMaxAllowedPidDiff;
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001346 } else {
1347 // Below code assumes single temporal referecence.
1348 RTC_DCHECK_EQ(gof_.num_ref_pics[gof_idx], 1);
1349 correct_pid = pid_diff == gof_.pid_diff[gof_idx][0];
1350 }
Sergey Silkin390f3582018-10-01 17:13:50 +02001351
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001352 if (same_spatial_layer && correct_pid) {
Sergey Silkin390f3582018-10-01 17:13:50 +02001353 ref_config.lst_fb_idx[sl_idx] = buf_idx;
1354 ref_config.reference_last[sl_idx] = 1;
1355 } else {
1356 // This reference doesn't match with one specified by GOF. This can
1357 // only happen if spatial layer is enabled dynamically without key
1358 // frame. Spatial prediction is supposed to be enabled in this case.
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001359 RTC_DCHECK(is_inter_layer_pred_allowed &&
1360 sl_idx > first_active_spatial_layer_id);
Sergey Silkin390f3582018-10-01 17:13:50 +02001361 }
1362 }
1363
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001364 if (is_inter_layer_pred_allowed && sl_idx > first_active_spatial_layer_id) {
Sergey Silkin390f3582018-10-01 17:13:50 +02001365 // Set up spatial reference.
1366 RTC_DCHECK(last_updated_buf_idx);
1367 ref_config.gld_fb_idx[sl_idx] = *last_updated_buf_idx;
1368 ref_config.reference_golden[sl_idx] = 1;
1369 } else {
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001370 RTC_DCHECK(ref_config.reference_last[sl_idx] != 0 ||
1371 sl_idx == first_active_spatial_layer_id ||
Sergey Silkin390f3582018-10-01 17:13:50 +02001372 inter_layer_pred_ == InterLayerPredMode::kOff);
1373 }
1374
1375 last_updated_buf_idx.reset();
1376
Ilya Nikolaevskiy5546aef2018-12-04 15:54:52 +01001377 if (gof_.temporal_idx[gof_idx] < num_temporal_layers_ - 1 ||
1378 num_temporal_layers_ == 1) {
Sergey Silkin390f3582018-10-01 17:13:50 +02001379 last_updated_buf_idx = sl_idx * num_temporal_refs + kUpdBufIdx[gof_idx];
1380
1381 // Ensure last frame buffer is not used for temporal prediction (it is
1382 // reserved for spatial reference).
1383 RTC_DCHECK_LT(*last_updated_buf_idx, kNumVp9Buffers - 1);
1384 } else if (is_inter_layer_pred_allowed) {
1385 last_updated_buf_idx = kNumVp9Buffers - 1;
1386 }
1387
1388 if (last_updated_buf_idx) {
1389 ref_config.update_buffer_slot[sl_idx] = 1 << *last_updated_buf_idx;
1390 }
1391 }
1392
1393 return ref_config;
1394}
1395
asaperssona9455ab2015-07-31 06:10:09 -07001396int VP9EncoderImpl::GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt) {
asapersson86956de2016-01-26 01:05:20 -08001397 RTC_DCHECK_EQ(pkt->kind, VPX_CODEC_CX_FRAME_PKT);
asaperssona9455ab2015-07-31 06:10:09 -07001398
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001399 if (pkt->data.frame.sz == 0) {
1400 // Ignore dropped frame.
1401 return WEBRTC_VIDEO_CODEC_OK;
1402 }
1403
Sergey Silkin07f80cc2018-04-09 13:11:59 +02001404 vpx_svc_layer_id_t layer_id = {0};
1405 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
1406
Ilya Nikolaevskiy1af0f902019-09-09 10:16:18 +02001407 if (layer_buffering_) {
Sergey Silkin88ce4ef2018-11-23 13:59:24 +01001408 // Deliver buffered low spatial layer frame.
1409 const bool end_of_picture = false;
1410 DeliverBufferedFrame(end_of_picture);
1411 }
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001412
Niels Möller4d504c72019-06-18 15:56:56 +02001413 // TODO(nisse): Introduce some buffer cache or buffer pool, to reduce
1414 // allocations and/or copy operations.
1415 encoded_image_.SetEncodedData(EncodedImageBuffer::Create(
1416 static_cast<const uint8_t*>(pkt->data.frame.buf), pkt->data.frame.sz));
asaperssond9f641e2016-01-21 01:11:35 -08001417
Sergey Silkinbd0954e2018-05-03 14:14:09 +02001418 const bool is_key_frame =
1419 (pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
1420 // Ensure encoder issued key frame on request.
1421 RTC_DCHECK(is_key_frame || !force_key_frame_);
1422
asaperssona9455ab2015-07-31 06:10:09 -07001423 // Check if encoded frame is a key frame.
Niels Möller8f7ce222019-03-21 15:43:58 +01001424 encoded_image_._frameType = VideoFrameType::kVideoFrameDelta;
Sergey Silkinbd0954e2018-05-03 14:14:09 +02001425 if (is_key_frame) {
Niels Möller8f7ce222019-03-21 15:43:58 +01001426 encoded_image_._frameType = VideoFrameType::kVideoFrameKey;
Sergey Silkinbd0954e2018-05-03 14:14:09 +02001427 force_key_frame_ = false;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001428 }
Niels Möller77536a22019-01-15 08:50:01 +01001429 RTC_DCHECK_LE(encoded_image_.size(), encoded_image_.capacity());
asapersson86956de2016-01-26 01:05:20 -08001430
philipeld1d03592019-03-01 13:53:55 +01001431 codec_specific_ = {};
Niels Möllerd3b8c632018-08-27 15:33:42 +02001432 absl::optional<int> spatial_index;
1433 PopulateCodecSpecific(&codec_specific_, &spatial_index, *pkt,
Sergey Silkin88ce4ef2018-11-23 13:59:24 +01001434 input_image_->timestamp());
Niels Möllerd3b8c632018-08-27 15:33:42 +02001435 encoded_image_.SetSpatialIndex(spatial_index);
asaperssona9455ab2015-07-31 06:10:09 -07001436
Ilya Nikolaevskiy2ec0c652019-01-18 11:56:48 +01001437 UpdateReferenceBuffers(*pkt, pics_since_key_);
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001438
Niels Möller77536a22019-01-15 08:50:01 +01001439 TRACE_COUNTER1("webrtc", "EncodedFrameSize", encoded_image_.size());
Niels Möller23775882018-08-16 10:24:12 +02001440 encoded_image_.SetTimestamp(input_image_->timestamp());
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001441 encoded_image_._encodedHeight =
1442 pkt->data.frame.height[layer_id.spatial_layer_id];
1443 encoded_image_._encodedWidth =
1444 pkt->data.frame.width[layer_id.spatial_layer_id];
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001445 int qp = -1;
1446 vpx_codec_control(encoder_, VP8E_GET_LAST_QUANTIZER, &qp);
1447 encoded_image_.qp_ = qp;
ilnik04f4d122017-06-19 07:18:55 -07001448
Ilya Nikolaevskiy1af0f902019-09-09 10:16:18 +02001449 if (!layer_buffering_) {
Sergey Silkin88ce4ef2018-11-23 13:59:24 +01001450 const bool end_of_picture = encoded_image_.SpatialIndex().value_or(0) + 1 ==
1451 num_active_spatial_layers_;
1452 DeliverBufferedFrame(end_of_picture);
1453 }
1454
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001455 return WEBRTC_VIDEO_CODEC_OK;
1456}
1457
Sergey Silkinbc0f0d32018-04-24 21:29:14 +02001458void VP9EncoderImpl::DeliverBufferedFrame(bool end_of_picture) {
Niels Möller77536a22019-01-15 08:50:01 +01001459 if (encoded_image_.size() > 0) {
Ilya Nikolaevskiy039a7142019-05-24 16:50:00 +02001460 if (num_spatial_layers_ > 1) {
1461 // Restore frame dropping settings, as dropping may be temporary forbidden
1462 // due to dynamically enabled layers.
Ilya Nikolaevskiy1af0f902019-09-09 10:16:18 +02001463 for (size_t i = 0; i < num_spatial_layers_; ++i) {
1464 svc_drop_frame_.framedrop_thresh[i] = config_->rc_dropframe_thresh;
Ilya Nikolaevskiy039a7142019-05-24 16:50:00 +02001465 }
1466 }
1467
Sergey Silkinbc0f0d32018-04-24 21:29:14 +02001468 codec_specific_.codecSpecific.VP9.end_of_picture = end_of_picture;
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001469
1470 // No data partitioning in VP9, so 1 partition only.
1471 int part_idx = 0;
1472 RTPFragmentationHeader frag_info;
1473 frag_info.VerifyAndAllocateFragmentationHeader(1);
1474 frag_info.fragmentationOffset[part_idx] = 0;
Niels Möller77536a22019-01-15 08:50:01 +01001475 frag_info.fragmentationLength[part_idx] = encoded_image_.size();
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001476
1477 encoded_complete_callback_->OnEncodedImage(encoded_image_, &codec_specific_,
1478 &frag_info);
Sergey Silkind902d582018-05-18 17:31:19 +02001479
Sergey Silkin96f2c972018-09-05 21:07:17 +02001480 if (codec_.mode == VideoCodecMode::kScreensharing) {
1481 const uint8_t spatial_idx = encoded_image_.SpatialIndex().value_or(0);
1482 const uint32_t frame_timestamp_ms =
Niels Möller23775882018-08-16 10:24:12 +02001483 1000 * encoded_image_.Timestamp() / kVideoPayloadTypeFrequency;
Sergey Silkin96f2c972018-09-05 21:07:17 +02001484 framerate_controller_[spatial_idx].AddFrame(frame_timestamp_ms);
Ilya Nikolaevskiy61170682019-03-06 16:04:32 +01001485
1486 const size_t steady_state_size = SteadyStateSize(
1487 spatial_idx, codec_specific_.codecSpecific.VP9.temporal_idx);
1488
1489 // Only frames on spatial layers, which may be limited in a steady state
1490 // are considered for steady state detection.
1491 if (framerate_controller_[spatial_idx].GetTargetRate() >
1492 variable_framerate_experiment_.framerate_limit + 1e-9) {
1493 if (encoded_image_.qp_ <=
1494 variable_framerate_experiment_.steady_state_qp &&
1495 encoded_image_.size() <= steady_state_size) {
1496 ++num_steady_state_frames_;
1497 } else {
1498 num_steady_state_frames_ = 0;
1499 }
1500 }
Sergey Silkind902d582018-05-18 17:31:19 +02001501 }
Ilya Nikolaevskiy61170682019-03-06 16:04:32 +01001502 encoded_image_.set_size(0);
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001503 }
1504}
1505
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001506int VP9EncoderImpl::RegisterEncodeCompleteCallback(
1507 EncodedImageCallback* callback) {
1508 encoded_complete_callback_ = callback;
1509 return WEBRTC_VIDEO_CODEC_OK;
1510}
1511
Erik Språng727d1642018-11-07 16:54:15 +01001512VideoEncoder::EncoderInfo VP9EncoderImpl::GetEncoderInfo() const {
1513 EncoderInfo info;
1514 info.supports_native_handle = false;
1515 info.implementation_name = "libvpx";
1516 info.scaling_settings = VideoEncoder::ScalingSettings::kOff;
Erik Språngd3438aa2018-11-08 16:56:43 +01001517 info.has_trusted_rate_controller = trusted_rate_controller_;
Mirta Dvornicic897a9912018-11-30 13:12:21 +01001518 info.is_hardware_accelerated = false;
1519 info.has_internal_source = false;
Erik Språngdbdd8392019-01-17 15:27:50 +01001520 for (size_t si = 0; si < num_spatial_layers_; ++si) {
1521 info.fps_allocation[si].clear();
1522 if (!codec_.spatialLayers[si].active) {
1523 continue;
1524 }
1525 // This spatial layer may already use a fraction of the total frame rate.
1526 const float sl_fps_fraction =
1527 codec_.spatialLayers[si].maxFramerate / codec_.maxFramerate;
1528 for (size_t ti = 0; ti < num_temporal_layers_; ++ti) {
1529 const uint32_t decimator =
1530 num_temporal_layers_ <= 1 ? 1 : config_->ts_rate_decimator[ti];
1531 RTC_DCHECK_GT(decimator, 0);
1532 info.fps_allocation[si].push_back(rtc::saturated_cast<uint8_t>(
1533 EncoderInfo::kMaxFramerateFraction * (sl_fps_fraction / decimator)));
1534 }
1535 }
Erik Språng727d1642018-11-07 16:54:15 +01001536 return info;
Peter Boströmb7d9a972015-12-18 16:01:11 +01001537}
1538
Ilya Nikolaevskiy61170682019-03-06 16:04:32 +01001539size_t VP9EncoderImpl::SteadyStateSize(int sid, int tid) {
1540 const size_t bitrate_bps = current_bitrate_allocation_.GetBitrate(
1541 sid, tid == kNoTemporalIdx ? 0 : tid);
1542 const float fps = (codec_.mode == VideoCodecMode::kScreensharing)
Ilya Nikolaevskiy5c18a5f2019-05-24 11:51:31 +02001543 ? std::min(static_cast<float>(codec_.maxFramerate),
1544 framerate_controller_[sid].GetTargetRate())
Ilya Nikolaevskiy61170682019-03-06 16:04:32 +01001545 : codec_.maxFramerate;
1546 return static_cast<size_t>(
1547 bitrate_bps / (8 * fps) *
1548 (100 -
1549 variable_framerate_experiment_.steady_state_undershoot_percentage) /
1550 100 +
1551 0.5);
1552}
1553
1554// static
1555VP9EncoderImpl::VariableFramerateExperiment
1556VP9EncoderImpl::ParseVariableFramerateConfig(std::string group_name) {
1557 FieldTrialFlag enabled = FieldTrialFlag("Enabled");
1558 FieldTrialParameter<double> framerate_limit("min_fps", 5.0);
1559 FieldTrialParameter<int> qp("min_qp", 32);
1560 FieldTrialParameter<int> undershoot_percentage("undershoot", 30);
1561 FieldTrialParameter<int> frames_before_steady_state(
1562 "frames_before_steady_state", 5);
1563 ParseFieldTrial({&enabled, &framerate_limit, &qp, &undershoot_percentage,
1564 &frames_before_steady_state},
1565 field_trial::FindFullName(group_name));
1566 VariableFramerateExperiment config;
1567 config.enabled = enabled.Get();
1568 config.framerate_limit = framerate_limit.Get();
1569 config.steady_state_qp = qp.Get();
1570 config.steady_state_undershoot_percentage = undershoot_percentage.Get();
1571 config.frames_before_steady_state = frames_before_steady_state.Get();
1572
1573 return config;
1574}
1575
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001576VP9DecoderImpl::VP9DecoderImpl()
sprang3958ed82017-08-17 08:12:10 -07001577 : decode_complete_callback_(nullptr),
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001578 inited_(false),
sprang3958ed82017-08-17 08:12:10 -07001579 decoder_(nullptr),
philipel9d7d75b2018-04-04 13:03:01 +02001580 key_frame_required_(true) {}
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001581
1582VP9DecoderImpl::~VP9DecoderImpl() {
1583 inited_ = true; // in order to do the actual release
1584 Release();
Henrik Boström9695d852015-05-06 10:42:15 +02001585 int num_buffers_in_use = frame_buffer_pool_.GetNumBuffersInUse();
1586 if (num_buffers_in_use > 0) {
1587 // The frame buffers are reference counted and frames are exposed after
1588 // decoding. There may be valid usage cases where previous frames are still
1589 // referenced after ~VP9DecoderImpl that is not a leak.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001590 RTC_LOG(LS_INFO) << num_buffers_in_use << " Vp9FrameBuffers are still "
1591 << "referenced during ~VP9DecoderImpl.";
Henrik Boström9695d852015-05-06 10:42:15 +02001592 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001593}
1594
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001595int VP9DecoderImpl::InitDecode(const VideoCodec* inst, int number_of_cores) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001596 int ret_val = Release();
1597 if (ret_val < 0) {
1598 return ret_val;
1599 }
Sergey Silkinb674cd12018-10-09 10:59:06 +02001600
sprang3958ed82017-08-17 08:12:10 -07001601 if (decoder_ == nullptr) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +00001602 decoder_ = new vpx_codec_ctx_t;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001603 }
philipelcce46fc2015-12-21 03:04:49 -08001604 vpx_codec_dec_cfg_t cfg;
Sergey Silkinb674cd12018-10-09 10:59:06 +02001605 memset(&cfg, 0, sizeof(cfg));
1606
1607 // We want to use multithreading when decoding high resolution videos. But,
1608 // since we don't know resolution of input stream at this stage, we always
1609 // enable it.
1610 cfg.threads = std::min(number_of_cores, kMaxNumTiles4kVideo);
1611
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001612 vpx_codec_flags_t flags = 0;
1613 if (vpx_codec_dec_init(decoder_, vpx_codec_vp9_dx(), &cfg, flags)) {
1614 return WEBRTC_VIDEO_CODEC_MEMORY;
1615 }
Henrik Boström9695d852015-05-06 10:42:15 +02001616
1617 if (!frame_buffer_pool_.InitializeVpxUsePool(decoder_)) {
1618 return WEBRTC_VIDEO_CODEC_MEMORY;
1619 }
1620
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001621 inited_ = true;
1622 // Always start with a complete key frame.
1623 key_frame_required_ = true;
1624 return WEBRTC_VIDEO_CODEC_OK;
1625}
1626
1627int VP9DecoderImpl::Decode(const EncodedImage& input_image,
1628 bool missing_frames,
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001629 int64_t /*render_time_ms*/) {
1630 if (!inited_) {
1631 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
1632 }
sprang3958ed82017-08-17 08:12:10 -07001633 if (decode_complete_callback_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001634 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
1635 }
1636 // Always start with a complete key frame.
1637 if (key_frame_required_) {
Niels Möller8f7ce222019-03-21 15:43:58 +01001638 if (input_image._frameType != VideoFrameType::kVideoFrameKey)
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001639 return WEBRTC_VIDEO_CODEC_ERROR;
1640 // We have a key frame - is it complete?
1641 if (input_image._completeFrame) {
1642 key_frame_required_ = false;
1643 } else {
1644 return WEBRTC_VIDEO_CODEC_ERROR;
1645 }
1646 }
sprang3958ed82017-08-17 08:12:10 -07001647 vpx_codec_iter_t iter = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001648 vpx_image_t* img;
Niels Möller24871e42019-01-17 11:31:13 +01001649 const uint8_t* buffer = input_image.data();
Niels Möller77536a22019-01-15 08:50:01 +01001650 if (input_image.size() == 0) {
sprang3958ed82017-08-17 08:12:10 -07001651 buffer = nullptr; // Triggers full frame concealment.
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001652 }
Henrik Boström9695d852015-05-06 10:42:15 +02001653 // During decode libvpx may get and release buffers from |frame_buffer_pool_|.
1654 // In practice libvpx keeps a few (~3-4) buffers alive at a time.
philipelcce46fc2015-12-21 03:04:49 -08001655 if (vpx_codec_decode(decoder_, buffer,
Niels Möller77536a22019-01-15 08:50:01 +01001656 static_cast<unsigned int>(input_image.size()), 0,
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001657 VPX_DL_REALTIME)) {
1658 return WEBRTC_VIDEO_CODEC_ERROR;
1659 }
Henrik Boström9695d852015-05-06 10:42:15 +02001660 // |img->fb_priv| contains the image data, a reference counted Vp9FrameBuffer.
1661 // It may be released by libvpx during future vpx_codec_decode or
1662 // vpx_codec_destroy calls.
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001663 img = vpx_codec_get_frame(decoder_, &iter);
sakal7adadb12017-02-23 02:54:57 -08001664 int qp;
1665 vpx_codec_err_t vpx_ret =
1666 vpx_codec_control(decoder_, VPXD_GET_LAST_QUANTIZER, &qp);
1667 RTC_DCHECK_EQ(vpx_ret, VPX_CODEC_OK);
Ilya Nikolaevskiya0e26092019-07-12 11:20:47 +02001668 int ret =
1669 ReturnFrame(img, input_image.Timestamp(), qp, input_image.ColorSpace());
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001670 if (ret != 0) {
1671 return ret;
1672 }
1673 return WEBRTC_VIDEO_CODEC_OK;
1674}
1675
Ilya Nikolaevskiya0e26092019-07-12 11:20:47 +02001676int VP9DecoderImpl::ReturnFrame(
1677 const vpx_image_t* img,
1678 uint32_t timestamp,
1679 int qp,
1680 const webrtc::ColorSpace* explicit_color_space) {
sprang3958ed82017-08-17 08:12:10 -07001681 if (img == nullptr) {
1682 // Decoder OK and nullptr image => No show frame.
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001683 return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
1684 }
Henrik Boström9695d852015-05-06 10:42:15 +02001685
1686 // This buffer contains all of |img|'s image data, a reference counted
perkj14f41442015-11-30 22:15:45 -08001687 // Vp9FrameBuffer. (libvpx is done with the buffers after a few
Henrik Boström9695d852015-05-06 10:42:15 +02001688 // vpx_codec_decode calls or vpx_codec_destroy).
1689 Vp9FrameBufferPool::Vp9FrameBuffer* img_buffer =
1690 static_cast<Vp9FrameBufferPool::Vp9FrameBuffer*>(img->fb_priv);
Emircan Uysaler0823eec2018-07-13 17:10:00 -07001691
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07001692 // The buffer can be used directly by the VideoFrame (without copy) by
Emircan Uysaler0823eec2018-07-13 17:10:00 -07001693 // using a Wrapped*Buffer.
1694 rtc::scoped_refptr<VideoFrameBuffer> img_wrapped_buffer;
1695 switch (img->bit_depth) {
1696 case 8:
1697 img_wrapped_buffer = WrapI420Buffer(
philipelcce46fc2015-12-21 03:04:49 -08001698 img->d_w, img->d_h, img->planes[VPX_PLANE_Y],
1699 img->stride[VPX_PLANE_Y], img->planes[VPX_PLANE_U],
1700 img->stride[VPX_PLANE_U], img->planes[VPX_PLANE_V],
1701 img->stride[VPX_PLANE_V],
Henrik Boström9695d852015-05-06 10:42:15 +02001702 // WrappedI420Buffer's mechanism for allowing the release of its frame
1703 // buffer is through a callback function. This is where we should
1704 // release |img_buffer|.
Emircan Uysaler0823eec2018-07-13 17:10:00 -07001705 rtc::KeepRefUntilDone(img_buffer));
1706 break;
1707 case 10:
1708 img_wrapped_buffer = WrapI010Buffer(
1709 img->d_w, img->d_h,
1710 reinterpret_cast<const uint16_t*>(img->planes[VPX_PLANE_Y]),
1711 img->stride[VPX_PLANE_Y] / 2,
1712 reinterpret_cast<const uint16_t*>(img->planes[VPX_PLANE_U]),
1713 img->stride[VPX_PLANE_U] / 2,
1714 reinterpret_cast<const uint16_t*>(img->planes[VPX_PLANE_V]),
1715 img->stride[VPX_PLANE_V] / 2, rtc::KeepRefUntilDone(img_buffer));
1716 break;
1717 default:
1718 RTC_NOTREACHED();
1719 return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
1720 }
nisseca6d5d12016-06-17 05:03:04 -07001721
Johannes Kron4749e4e2018-11-21 10:18:18 +01001722 auto builder = VideoFrame::Builder()
1723 .set_video_frame_buffer(img_wrapped_buffer)
Ilya Nikolaevskiya0e26092019-07-12 11:20:47 +02001724 .set_timestamp_rtp(timestamp);
1725 if (explicit_color_space) {
1726 builder.set_color_space(*explicit_color_space);
1727 } else {
1728 builder.set_color_space(
1729 ExtractVP9ColorSpace(img->cs, img->range, img->bit_depth));
1730 }
Johannes Kron4749e4e2018-11-21 10:18:18 +01001731 VideoFrame decoded_image = builder.build();
1732
Danil Chapovalov0040b662018-06-18 10:48:16 +02001733 decode_complete_callback_->Decoded(decoded_image, absl::nullopt, qp);
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001734 return WEBRTC_VIDEO_CODEC_OK;
1735}
1736
1737int VP9DecoderImpl::RegisterDecodeCompleteCallback(
1738 DecodedImageCallback* callback) {
1739 decode_complete_callback_ = callback;
1740 return WEBRTC_VIDEO_CODEC_OK;
1741}
1742
1743int VP9DecoderImpl::Release() {
Sergey Silkin3e871ea2018-03-02 13:11:04 +01001744 int ret_val = WEBRTC_VIDEO_CODEC_OK;
1745
sprang3958ed82017-08-17 08:12:10 -07001746 if (decoder_ != nullptr) {
Sergey Silkin90399692018-03-02 14:44:10 +01001747 if (inited_) {
1748 // When a codec is destroyed libvpx will release any buffers of
1749 // |frame_buffer_pool_| it is currently using.
1750 if (vpx_codec_destroy(decoder_)) {
1751 ret_val = WEBRTC_VIDEO_CODEC_MEMORY;
1752 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001753 }
1754 delete decoder_;
sprang3958ed82017-08-17 08:12:10 -07001755 decoder_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001756 }
Henrik Boström9695d852015-05-06 10:42:15 +02001757 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers
1758 // still referenced externally are deleted once fully released, not returning
1759 // to the pool.
1760 frame_buffer_pool_.ClearPool();
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001761 inited_ = false;
Sergey Silkin3e871ea2018-03-02 13:11:04 +01001762 return ret_val;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001763}
Peter Boströmb7d9a972015-12-18 16:01:11 +01001764
1765const char* VP9DecoderImpl::ImplementationName() const {
1766 return "libvpx";
1767}
1768
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001769} // namespace webrtc
Mirko Bonadei95adedb2018-11-19 09:52:37 +01001770
1771#endif // RTC_ENABLE_VP9