blob: 61542c508211496297b36cecde4794b46284e605 [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
marpan@webrtc.org5b883172014-11-01 06:10:48 +000020#include "vpx/vp8cx.h"
21#include "vpx/vp8dx.h"
Yves Gerey665174f2018-06-19 15:03:05 +020022#include "vpx/vpx_decoder.h"
23#include "vpx/vpx_encoder.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000024
Karl Wiberg918f50c2018-07-05 11:40:33 +020025#include "absl/memory/memory.h"
Emircan Uysaler800787f2018-07-16 10:01:49 -070026#include "api/video/color_space.h"
Emircan Uysaler0823eec2018-07-13 17:10:00 -070027#include "api/video/i010_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "common_video/include/video_frame_buffer.h"
29#include "common_video/libyuv/include/webrtc_libyuv.h"
Sergey Silkind902d582018-05-18 17:31:19 +020030#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Sergey Silkin86684962018-03-28 19:32:37 +020031#include "modules/video_coding/codecs/vp9/svc_rate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "rtc_base/checks.h"
33#include "rtc_base/keep_ref_until_done.h"
34#include "rtc_base/logging.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020035#include "rtc_base/timeutils.h"
36#include "rtc_base/trace_event.h"
Sergey Silkin390f3582018-10-01 17:13:50 +020037#include "system_wrappers/include/field_trial.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000038
39namespace webrtc {
40
Sergey Silkind902d582018-05-18 17:31:19 +020041namespace {
Erik Språngd3438aa2018-11-08 16:56:43 +010042const char kVp9TrustedRateControllerFieldTrial[] =
43 "WebRTC-LibvpxVp9TrustedRateController";
44
Sergey Silkin390f3582018-10-01 17:13:50 +020045// Maps from gof_idx to encoder internal reference frame buffer index. These
46// maps work for 1,2 and 3 temporal layers with GOF length of 1,2 and 4 frames.
47uint8_t kRefBufIdx[4] = {0, 0, 0, 1};
48uint8_t kUpdBufIdx[4] = {0, 0, 1, 0};
49
Sergey Silkinb674cd12018-10-09 10:59:06 +020050int kMaxNumTiles4kVideo = 8;
51
Marco6e89b252015-07-07 14:40:38 -070052// Only positive speeds, range for real-time coding currently is: 5 - 8.
53// Lower means slower/better quality, higher means fastest/lower quality.
54int GetCpuSpeed(int width, int height) {
Alex Glaznevfecb7c32016-03-31 14:23:27 -070055#if defined(WEBRTC_ARCH_ARM) || defined(WEBRTC_ARCH_ARM64) || defined(ANDROID)
Marco002f0d02015-12-17 09:49:31 -080056 return 8;
57#else
Marco6e89b252015-07-07 14:40:38 -070058 // For smaller resolutions, use lower speed setting (get some coding gain at
59 // the cost of increased encoding complexity).
60 if (width * height <= 352 * 288)
61 return 5;
62 else
63 return 7;
Marco002f0d02015-12-17 09:49:31 -080064#endif
Marco6e89b252015-07-07 14:40:38 -070065}
Emircan Uysaler715cc232018-07-23 17:50:43 -070066// Helper class for extracting VP9 colorspace.
67ColorSpace ExtractVP9ColorSpace(vpx_color_space_t space_t,
68 vpx_color_range_t range_t,
69 unsigned int bit_depth) {
70 ColorSpace::PrimaryID primaries = ColorSpace::PrimaryID::kInvalid;
71 ColorSpace::TransferID transfer = ColorSpace::TransferID::kInvalid;
72 ColorSpace::MatrixID matrix = ColorSpace::MatrixID::kInvalid;
73 switch (space_t) {
74 case VPX_CS_BT_601:
75 case VPX_CS_SMPTE_170:
76 primaries = ColorSpace::PrimaryID::kSMPTE170M;
77 transfer = ColorSpace::TransferID::kSMPTE170M;
78 matrix = ColorSpace::MatrixID::kSMPTE170M;
79 break;
80 case VPX_CS_SMPTE_240:
81 primaries = ColorSpace::PrimaryID::kSMPTE240M;
82 transfer = ColorSpace::TransferID::kSMPTE240M;
83 matrix = ColorSpace::MatrixID::kSMPTE240M;
84 break;
85 case VPX_CS_BT_709:
86 primaries = ColorSpace::PrimaryID::kBT709;
87 transfer = ColorSpace::TransferID::kBT709;
88 matrix = ColorSpace::MatrixID::kBT709;
89 break;
90 case VPX_CS_BT_2020:
91 primaries = ColorSpace::PrimaryID::kBT2020;
92 switch (bit_depth) {
93 case 8:
94 transfer = ColorSpace::TransferID::kBT709;
95 break;
96 case 10:
97 transfer = ColorSpace::TransferID::kBT2020_10;
98 break;
99 default:
100 RTC_NOTREACHED();
101 break;
102 }
103 matrix = ColorSpace::MatrixID::kBT2020_NCL;
104 break;
105 case VPX_CS_SRGB:
106 primaries = ColorSpace::PrimaryID::kBT709;
107 transfer = ColorSpace::TransferID::kIEC61966_2_1;
108 matrix = ColorSpace::MatrixID::kBT709;
109 break;
110 default:
111 break;
112 }
113
114 ColorSpace::RangeID range = ColorSpace::RangeID::kInvalid;
115 switch (range_t) {
116 case VPX_CR_STUDIO_RANGE:
117 range = ColorSpace::RangeID::kLimited;
118 break;
119 case VPX_CR_FULL_RANGE:
120 range = ColorSpace::RangeID::kFull;
121 break;
122 default:
123 break;
124 }
125 return ColorSpace(primaries, transfer, matrix, range);
126}
127} // namespace
Marco6e89b252015-07-07 14:40:38 -0700128
asaperssona9455ab2015-07-31 06:10:09 -0700129void VP9EncoderImpl::EncoderOutputCodedPacketCallback(vpx_codec_cx_pkt* pkt,
130 void* user_data) {
philipelcce46fc2015-12-21 03:04:49 -0800131 VP9EncoderImpl* enc = static_cast<VP9EncoderImpl*>(user_data);
asaperssona9455ab2015-07-31 06:10:09 -0700132 enc->GetEncodedLayerFrame(pkt);
133}
134
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700135VP9EncoderImpl::VP9EncoderImpl(const cricket::VideoCodec& codec)
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000136 : encoded_image_(),
sprang3958ed82017-08-17 08:12:10 -0700137 encoded_complete_callback_(nullptr),
Emircan Uysaler98badbc2018-06-28 10:59:02 -0700138 profile_(
139 ParseSdpForVP9Profile(codec.params).value_or(VP9Profile::kProfile0)),
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000140 inited_(false),
141 timestamp_(0),
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000142 cpu_speed_(3),
143 rc_max_intra_target_(0),
sprang3958ed82017-08-17 08:12:10 -0700144 encoder_(nullptr),
145 config_(nullptr),
146 raw_(nullptr),
147 input_image_(nullptr),
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200148 force_key_frame_(true),
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200149 pics_since_key_(0),
asaperssona9455ab2015-07-31 06:10:09 -0700150 num_temporal_layers_(0),
philipelcfc319b2015-11-10 07:17:23 -0800151 num_spatial_layers_(0),
Sergey Silkin390f3582018-10-01 17:13:50 +0200152 num_active_spatial_layers_(0),
Erik Språngd3438aa2018-11-08 16:56:43 +0100153 layer_deactivation_requires_key_frame_(
154 field_trial::IsEnabled("WebRTC-Vp9IssueKeyFrameOnLayerDeactivation")),
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200155 is_svc_(false),
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200156 inter_layer_pred_(InterLayerPredMode::kOn),
Sergey Silkin390f3582018-10-01 17:13:50 +0200157 external_ref_control_(
Erik Språngd3438aa2018-11-08 16:56:43 +0100158 field_trial::IsEnabled("WebRTC-Vp9ExternalRefCtrl")),
159 trusted_rate_controller_(
160 field_trial::IsEnabled(kVp9TrustedRateControllerFieldTrial)),
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100161 full_superframe_drop_(true),
162 first_frame_in_picture_(true),
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200163 is_flexible_mode_(false) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000164 memset(&codec_, 0, sizeof(codec_));
johannkoenig8225c402017-01-26 13:23:44 -0800165 memset(&svc_params_, 0, sizeof(vpx_svc_extra_cfg_t));
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000166}
167
168VP9EncoderImpl::~VP9EncoderImpl() {
169 Release();
170}
171
172int VP9EncoderImpl::Release() {
Sergey Silkin3e871ea2018-03-02 13:11:04 +0100173 int ret_val = WEBRTC_VIDEO_CODEC_OK;
174
sprang3958ed82017-08-17 08:12:10 -0700175 if (encoded_image_._buffer != nullptr) {
philipelcce46fc2015-12-21 03:04:49 -0800176 delete[] encoded_image_._buffer;
sprang3958ed82017-08-17 08:12:10 -0700177 encoded_image_._buffer = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000178 }
sprang3958ed82017-08-17 08:12:10 -0700179 if (encoder_ != nullptr) {
Sergey Silkin90399692018-03-02 14:44:10 +0100180 if (inited_) {
181 if (vpx_codec_destroy(encoder_)) {
182 ret_val = WEBRTC_VIDEO_CODEC_MEMORY;
183 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000184 }
185 delete encoder_;
sprang3958ed82017-08-17 08:12:10 -0700186 encoder_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000187 }
sprang3958ed82017-08-17 08:12:10 -0700188 if (config_ != nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000189 delete config_;
sprang3958ed82017-08-17 08:12:10 -0700190 config_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000191 }
sprang3958ed82017-08-17 08:12:10 -0700192 if (raw_ != nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000193 vpx_img_free(raw_);
sprang3958ed82017-08-17 08:12:10 -0700194 raw_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000195 }
196 inited_ = false;
Sergey Silkin3e871ea2018-03-02 13:11:04 +0100197 return ret_val;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000198}
199
sprangce4aef12015-11-02 07:23:20 -0800200bool VP9EncoderImpl::ExplicitlyConfiguredSpatialLayers() const {
201 // We check target_bitrate_bps of the 0th layer to see if the spatial layers
202 // (i.e. bitrates) were explicitly configured.
Sergey Silkinbeba1b22018-09-11 11:40:25 +0200203 return codec_.spatialLayers[0].targetBitrate > 0;
sprangce4aef12015-11-02 07:23:20 -0800204}
205
Erik Språng566124a2018-04-23 12:32:22 +0200206bool VP9EncoderImpl::SetSvcRates(
207 const VideoBitrateAllocation& bitrate_allocation) {
Sergey Silkin86684962018-03-28 19:32:37 +0200208 config_->rc_target_bitrate = bitrate_allocation.get_sum_kbps();
Sergey Silkin86684962018-03-28 19:32:37 +0200209
sprangce4aef12015-11-02 07:23:20 -0800210 if (ExplicitlyConfiguredSpatialLayers()) {
Sergey Silkine7ce8882018-10-03 18:04:57 +0200211 const bool layer_activation_requires_key_frame =
212 inter_layer_pred_ == InterLayerPredMode::kOff ||
213 inter_layer_pred_ == InterLayerPredMode::kOnKeyPic;
214
Sergey Silkin86684962018-03-28 19:32:37 +0200215 for (size_t sl_idx = 0; sl_idx < num_spatial_layers_; ++sl_idx) {
Sergey Silkine7ce8882018-10-03 18:04:57 +0200216 const bool was_layer_active = (config_->ss_target_bitrate[sl_idx] > 0);
Sergey Silkin86684962018-03-28 19:32:37 +0200217 config_->ss_target_bitrate[sl_idx] =
218 bitrate_allocation.GetSpatialLayerSum(sl_idx) / 1000;
219
220 for (size_t tl_idx = 0; tl_idx < num_temporal_layers_; ++tl_idx) {
221 config_->layer_target_bitrate[sl_idx * num_temporal_layers_ + tl_idx] =
222 bitrate_allocation.GetTemporalLayerSum(sl_idx, tl_idx) / 1000;
223 }
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200224
Sergey Silkine7ce8882018-10-03 18:04:57 +0200225 const bool is_active_layer = (config_->ss_target_bitrate[sl_idx] > 0);
226 if (!was_layer_active && is_active_layer &&
227 layer_activation_requires_key_frame) {
228 force_key_frame_ = true;
229 } else if (was_layer_active && !is_active_layer &&
230 layer_deactivation_requires_key_frame_) {
231 force_key_frame_ = true;
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200232 }
Sergey Silkin96f2c972018-09-05 21:07:17 +0200233
Sergey Silkine7ce8882018-10-03 18:04:57 +0200234 if (!was_layer_active) {
Sergey Silkin96f2c972018-09-05 21:07:17 +0200235 // Reset frame rate controller if layer is resumed after pause.
236 framerate_controller_[sl_idx].Reset();
237 }
238
239 framerate_controller_[sl_idx].SetTargetRate(
Sergey Silkinbeba1b22018-09-11 11:40:25 +0200240 std::min(static_cast<float>(codec_.maxFramerate),
241 codec_.spatialLayers[sl_idx].maxFramerate));
sprangce4aef12015-11-02 07:23:20 -0800242 }
243 } else {
244 float rate_ratio[VPX_MAX_LAYERS] = {0};
245 float total = 0;
Sergey Silkin908689d2018-08-20 09:28:45 +0200246 for (int i = 0; i < num_spatial_layers_; ++i) {
johannkoenig8225c402017-01-26 13:23:44 -0800247 if (svc_params_.scaling_factor_num[i] <= 0 ||
248 svc_params_.scaling_factor_den[i] <= 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100249 RTC_LOG(LS_ERROR) << "Scaling factors not specified!";
sprangce4aef12015-11-02 07:23:20 -0800250 return false;
251 }
Yves Gerey665174f2018-06-19 15:03:05 +0200252 rate_ratio[i] = static_cast<float>(svc_params_.scaling_factor_num[i]) /
253 svc_params_.scaling_factor_den[i];
sprangce4aef12015-11-02 07:23:20 -0800254 total += rate_ratio[i];
255 }
256
Sergey Silkin908689d2018-08-20 09:28:45 +0200257 for (int i = 0; i < num_spatial_layers_; ++i) {
Rasmus Brandt58cd3852018-06-26 13:41:16 +0200258 RTC_CHECK_GT(total, 0);
sprangce4aef12015-11-02 07:23:20 -0800259 config_->ss_target_bitrate[i] = static_cast<unsigned int>(
260 config_->rc_target_bitrate * rate_ratio[i] / total);
261 if (num_temporal_layers_ == 1) {
262 config_->layer_target_bitrate[i] = config_->ss_target_bitrate[i];
263 } else if (num_temporal_layers_ == 2) {
264 config_->layer_target_bitrate[i * num_temporal_layers_] =
265 config_->ss_target_bitrate[i] * 2 / 3;
266 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] =
267 config_->ss_target_bitrate[i];
268 } else if (num_temporal_layers_ == 3) {
269 config_->layer_target_bitrate[i * num_temporal_layers_] =
270 config_->ss_target_bitrate[i] / 2;
271 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] =
272 config_->layer_target_bitrate[i * num_temporal_layers_] +
273 (config_->ss_target_bitrate[i] / 4);
274 config_->layer_target_bitrate[i * num_temporal_layers_ + 2] =
275 config_->ss_target_bitrate[i];
276 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100277 RTC_LOG(LS_ERROR) << "Unsupported number of temporal layers: "
278 << num_temporal_layers_;
sprangce4aef12015-11-02 07:23:20 -0800279 return false;
280 }
Sergey Silkin96f2c972018-09-05 21:07:17 +0200281
282 framerate_controller_[i].SetTargetRate(codec_.maxFramerate);
asaperssona9455ab2015-07-31 06:10:09 -0700283 }
284 }
Sergey Silkin908689d2018-08-20 09:28:45 +0200285
286 num_active_spatial_layers_ = 0;
287 for (int i = 0; i < num_spatial_layers_; ++i) {
288 if (config_->ss_target_bitrate[i] > 0) {
289 ++num_active_spatial_layers_;
290 }
291 }
292 RTC_DCHECK_GT(num_active_spatial_layers_, 0);
293
asaperssona9455ab2015-07-31 06:10:09 -0700294 return true;
295}
296
Erik Språng08127a92016-11-16 16:41:30 +0100297int VP9EncoderImpl::SetRateAllocation(
Erik Språng566124a2018-04-23 12:32:22 +0200298 const VideoBitrateAllocation& bitrate_allocation,
Erik Språng08127a92016-11-16 16:41:30 +0100299 uint32_t frame_rate) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000300 if (!inited_) {
301 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
302 }
303 if (encoder_->err) {
304 return WEBRTC_VIDEO_CODEC_ERROR;
305 }
Erik Språng08127a92016-11-16 16:41:30 +0100306 if (frame_rate < 1) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000307 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
308 }
309 // Update bit rate
Erik Språng08127a92016-11-16 16:41:30 +0100310 if (codec_.maxBitrate > 0 &&
311 bitrate_allocation.get_sum_kbps() > codec_.maxBitrate) {
312 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000313 }
Erik Språng08127a92016-11-16 16:41:30 +0100314
Erik Språng08127a92016-11-16 16:41:30 +0100315 codec_.maxFramerate = frame_rate;
asaperssona9455ab2015-07-31 06:10:09 -0700316
Sergey Silkin86684962018-03-28 19:32:37 +0200317 if (!SetSvcRates(bitrate_allocation)) {
asaperssona9455ab2015-07-31 06:10:09 -0700318 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
319 }
320
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000321 // Update encoder context
322 if (vpx_codec_enc_config_set(encoder_, config_)) {
323 return WEBRTC_VIDEO_CODEC_ERROR;
324 }
325 return WEBRTC_VIDEO_CODEC_OK;
326}
327
328int VP9EncoderImpl::InitEncode(const VideoCodec* inst,
329 int number_of_cores,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000330 size_t /*max_payload_size*/) {
sprang3958ed82017-08-17 08:12:10 -0700331 if (inst == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000332 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
333 }
334 if (inst->maxFramerate < 1) {
335 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
336 }
337 // Allow zero to represent an unspecified maxBitRate
338 if (inst->maxBitrate > 0 && inst->startBitrate > inst->maxBitrate) {
339 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
340 }
341 if (inst->width < 1 || inst->height < 1) {
342 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
343 }
344 if (number_of_cores < 1) {
345 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
346 }
hta257dc392016-10-25 09:05:06 -0700347 if (inst->VP9().numberOfTemporalLayers > 3) {
asaperssona9455ab2015-07-31 06:10:09 -0700348 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
349 }
ilnik2a8c2f52017-02-15 02:23:28 -0800350 // libvpx probably does not support more than 3 spatial layers.
351 if (inst->VP9().numberOfSpatialLayers > 3) {
asaperssona9455ab2015-07-31 06:10:09 -0700352 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
353 }
philipelcfc319b2015-11-10 07:17:23 -0800354
asapersson86956de2016-01-26 01:05:20 -0800355 int ret_val = Release();
356 if (ret_val < 0) {
357 return ret_val;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000358 }
sprang3958ed82017-08-17 08:12:10 -0700359 if (encoder_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000360 encoder_ = new vpx_codec_ctx_t;
361 }
sprang3958ed82017-08-17 08:12:10 -0700362 if (config_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000363 config_ = new vpx_codec_enc_cfg_t;
364 }
365 timestamp_ = 0;
366 if (&codec_ != inst) {
367 codec_ = *inst;
368 }
asaperssona9455ab2015-07-31 06:10:09 -0700369
Sergey Silkinb0bd03b2018-09-28 14:56:39 +0200370 force_key_frame_ = true;
371 pics_since_key_ = 0;
372
hta257dc392016-10-25 09:05:06 -0700373 num_spatial_layers_ = inst->VP9().numberOfSpatialLayers;
Niels Möller65fb4042018-04-25 14:46:06 +0200374 RTC_DCHECK_GT(num_spatial_layers_, 0);
hta257dc392016-10-25 09:05:06 -0700375 num_temporal_layers_ = inst->VP9().numberOfTemporalLayers;
Sergey Silkin96f2c972018-09-05 21:07:17 +0200376 if (num_temporal_layers_ == 0) {
asaperssona9455ab2015-07-31 06:10:09 -0700377 num_temporal_layers_ = 1;
Sergey Silkin042661b2018-09-10 10:39:35 +0000378 }
Sergey Silkinae9e1882018-09-05 21:07:17 +0200379
Sergey Silkin96f2c972018-09-05 21:07:17 +0200380 framerate_controller_ = std::vector<FramerateController>(
381 num_spatial_layers_, FramerateController(codec_.maxFramerate));
382
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200383 is_svc_ = (num_spatial_layers_ > 1 || num_temporal_layers_ > 1);
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200384
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000385 // Allocate memory for encoded image
sprang3958ed82017-08-17 08:12:10 -0700386 if (encoded_image_._buffer != nullptr) {
philipelcce46fc2015-12-21 03:04:49 -0800387 delete[] encoded_image_._buffer;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000388 }
nisseeb44b392017-04-28 07:18:05 -0700389 encoded_image_._size =
390 CalcBufferSize(VideoType::kI420, codec_.width, codec_.height);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000391 encoded_image_._buffer = new uint8_t[encoded_image_._size];
392 encoded_image_._completeFrame = true;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000393 // Populate encoder configuration with default values.
394 if (vpx_codec_enc_config_default(vpx_codec_vp9_cx(), config_, 0)) {
395 return WEBRTC_VIDEO_CODEC_ERROR;
396 }
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700397
398 vpx_img_fmt img_fmt = VPX_IMG_FMT_NONE;
399 unsigned int bits_for_storage = 8;
400 switch (profile_) {
401 case VP9Profile::kProfile0:
402 img_fmt = VPX_IMG_FMT_I420;
403 bits_for_storage = 8;
404 config_->g_bit_depth = VPX_BITS_8;
405 config_->g_profile = 0;
406 config_->g_input_bit_depth = 8;
407 break;
408 case VP9Profile::kProfile2:
409 img_fmt = VPX_IMG_FMT_I42016;
410 bits_for_storage = 16;
411 config_->g_bit_depth = VPX_BITS_10;
412 config_->g_profile = 2;
413 config_->g_input_bit_depth = 10;
414 break;
415 }
416
417 // Creating a wrapper to the image - setting image data to nullptr. Actual
418 // pointer will be set in encode. Setting align to 1, as it is meaningless
419 // (actual memory is not allocated).
420 raw_ =
421 vpx_img_wrap(nullptr, img_fmt, codec_.width, codec_.height, 1, nullptr);
422 raw_->bit_depth = bits_for_storage;
423
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000424 config_->g_w = codec_.width;
425 config_->g_h = codec_.height;
426 config_->rc_target_bitrate = inst->startBitrate; // in kbit/s
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200427 config_->g_error_resilient = is_svc_ ? VPX_ERROR_RESILIENT_DEFAULT : 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000428 // Setting the time base of the codec.
429 config_->g_timebase.num = 1;
430 config_->g_timebase.den = 90000;
431 config_->g_lag_in_frames = 0; // 0- no frame lagging
432 config_->g_threads = 1;
433 // Rate control settings.
hta257dc392016-10-25 09:05:06 -0700434 config_->rc_dropframe_thresh = inst->VP9().frameDroppingOn ? 30 : 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000435 config_->rc_end_usage = VPX_CBR;
436 config_->g_pass = VPX_RC_ONE_PASS;
437 config_->rc_min_quantizer = 2;
marpan@webrtc.orgdc8a9da2015-01-27 23:08:24 +0000438 config_->rc_max_quantizer = 52;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000439 config_->rc_undershoot_pct = 50;
440 config_->rc_overshoot_pct = 50;
441 config_->rc_buf_initial_sz = 500;
442 config_->rc_buf_optimal_sz = 600;
443 config_->rc_buf_sz = 1000;
444 // Set the maximum target size of any key-frame.
445 rc_max_intra_target_ = MaxIntraTarget(config_->rc_buf_optimal_sz);
hta257dc392016-10-25 09:05:06 -0700446 if (inst->VP9().keyFrameInterval > 0) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000447 config_->kf_mode = VPX_KF_AUTO;
hta257dc392016-10-25 09:05:06 -0700448 config_->kf_max_dist = inst->VP9().keyFrameInterval;
Åsa Perssonff24c042015-12-04 10:58:08 +0100449 // Needs to be set (in svc mode) to get correct periodic key frame interval
450 // (will have no effect in non-svc).
451 config_->kf_min_dist = config_->kf_max_dist;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000452 } else {
453 config_->kf_mode = VPX_KF_DISABLED;
454 }
hta257dc392016-10-25 09:05:06 -0700455 config_->rc_resize_allowed = inst->VP9().automaticResizeOn ? 1 : 0;
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000456 // Determine number of threads based on the image size and #cores.
philipelcce46fc2015-12-21 03:04:49 -0800457 config_->g_threads =
458 NumberOfThreads(config_->g_w, config_->g_h, number_of_cores);
asaperssona9455ab2015-07-31 06:10:09 -0700459
Marco6e89b252015-07-07 14:40:38 -0700460 cpu_speed_ = GetCpuSpeed(config_->g_w, config_->g_h);
asaperssona9455ab2015-07-31 06:10:09 -0700461
hta257dc392016-10-25 09:05:06 -0700462 is_flexible_mode_ = inst->VP9().flexibleMode;
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200463
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200464 if (num_temporal_layers_ == 1) {
asaperssona9455ab2015-07-31 06:10:09 -0700465 gof_.SetGofInfoVP9(kTemporalStructureMode1);
466 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING;
467 config_->ts_number_layers = 1;
468 config_->ts_rate_decimator[0] = 1;
469 config_->ts_periodicity = 1;
470 config_->ts_layer_id[0] = 0;
471 } else if (num_temporal_layers_ == 2) {
472 gof_.SetGofInfoVP9(kTemporalStructureMode2);
473 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0101;
474 config_->ts_number_layers = 2;
475 config_->ts_rate_decimator[0] = 2;
476 config_->ts_rate_decimator[1] = 1;
477 config_->ts_periodicity = 2;
478 config_->ts_layer_id[0] = 0;
479 config_->ts_layer_id[1] = 1;
480 } else if (num_temporal_layers_ == 3) {
481 gof_.SetGofInfoVP9(kTemporalStructureMode3);
482 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0212;
483 config_->ts_number_layers = 3;
484 config_->ts_rate_decimator[0] = 4;
485 config_->ts_rate_decimator[1] = 2;
486 config_->ts_rate_decimator[2] = 1;
487 config_->ts_periodicity = 4;
488 config_->ts_layer_id[0] = 0;
489 config_->ts_layer_id[1] = 2;
490 config_->ts_layer_id[2] = 1;
491 config_->ts_layer_id[3] = 2;
492 } else {
493 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
494 }
495
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200496 inter_layer_pred_ = inst->VP9().interLayerPred;
497
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200498 ref_buf_.clear();
499
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000500 return InitAndSetControlSettings(inst);
501}
502
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000503int VP9EncoderImpl::NumberOfThreads(int width,
504 int height,
505 int number_of_cores) {
506 // Keep the number of encoder threads equal to the possible number of column
507 // tiles, which is (1, 2, 4, 8). See comments below for VP9E_SET_TILE_COLUMNS.
508 if (width * height >= 1280 * 720 && number_of_cores > 4) {
509 return 4;
jianj23173a32017-07-12 16:11:09 -0700510 } else if (width * height >= 640 * 360 && number_of_cores > 2) {
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000511 return 2;
512 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200513// Use 2 threads for low res on ARM.
Jerome Jiang831af372017-12-05 10:44:35 -0800514#if defined(WEBRTC_ARCH_ARM) || defined(WEBRTC_ARCH_ARM64) || \
515 defined(WEBRTC_ANDROID)
516 if (width * height >= 320 * 180 && number_of_cores > 2) {
517 return 2;
518 }
519#endif
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000520 // 1 thread less than VGA.
521 return 1;
522 }
523}
524
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000525int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) {
Åsa Perssonff24c042015-12-04 10:58:08 +0100526 // Set QP-min/max per spatial and temporal layer.
527 int tot_num_layers = num_spatial_layers_ * num_temporal_layers_;
528 for (int i = 0; i < tot_num_layers; ++i) {
johannkoenig8225c402017-01-26 13:23:44 -0800529 svc_params_.max_quantizers[i] = config_->rc_max_quantizer;
530 svc_params_.min_quantizers[i] = config_->rc_min_quantizer;
Åsa Perssonff24c042015-12-04 10:58:08 +0100531 }
asaperssona9455ab2015-07-31 06:10:09 -0700532 config_->ss_number_layers = num_spatial_layers_;
sprangce4aef12015-11-02 07:23:20 -0800533 if (ExplicitlyConfiguredSpatialLayers()) {
534 for (int i = 0; i < num_spatial_layers_; ++i) {
535 const auto& layer = codec_.spatialLayers[i];
Rasmus Brandt58cd3852018-06-26 13:41:16 +0200536 RTC_CHECK_GT(layer.width, 0);
Sergey Silkin13e74342018-03-02 12:28:00 +0100537 const int scale_factor = codec_.width / layer.width;
538 RTC_DCHECK_GT(scale_factor, 0);
539
540 // Ensure scaler factor is integer.
541 if (scale_factor * layer.width != codec_.width) {
542 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
543 }
544
545 // Ensure scale factor is the same in both dimensions.
546 if (scale_factor * layer.height != codec_.height) {
547 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
548 }
549
550 // Ensure scale factor is power of two.
551 const bool is_pow_of_two = (scale_factor & (scale_factor - 1)) == 0;
552 if (!is_pow_of_two) {
553 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
554 }
555
556 svc_params_.scaling_factor_num[i] = 1;
557 svc_params_.scaling_factor_den[i] = scale_factor;
Sergey Silkin96f2c972018-09-05 21:07:17 +0200558
559 RTC_DCHECK_GT(codec_.spatialLayers[i].maxFramerate, 0);
560 RTC_DCHECK_LE(codec_.spatialLayers[i].maxFramerate, codec_.maxFramerate);
561 if (i > 0) {
562 // Frame rate of high spatial layer is supposed to be equal or higher
563 // than frame rate of low spatial layer.
564 RTC_DCHECK_GE(codec_.spatialLayers[i].maxFramerate,
565 codec_.spatialLayers[i - 1].maxFramerate);
566 }
sprangce4aef12015-11-02 07:23:20 -0800567 }
568 } else {
569 int scaling_factor_num = 256;
570 for (int i = num_spatial_layers_ - 1; i >= 0; --i) {
sprangce4aef12015-11-02 07:23:20 -0800571 // 1:2 scaling in each dimension.
johannkoenig8225c402017-01-26 13:23:44 -0800572 svc_params_.scaling_factor_num[i] = scaling_factor_num;
573 svc_params_.scaling_factor_den[i] = 256;
sprangce4aef12015-11-02 07:23:20 -0800574 }
asaperssona9455ab2015-07-31 06:10:09 -0700575 }
576
Sergey Silkin86684962018-03-28 19:32:37 +0200577 SvcRateAllocator init_allocator(codec_);
Erik Språng566124a2018-04-23 12:32:22 +0200578 VideoBitrateAllocation allocation = init_allocator.GetAllocation(
Sergey Silkin86684962018-03-28 19:32:37 +0200579 inst->startBitrate * 1000, inst->maxFramerate);
580 if (!SetSvcRates(allocation)) {
asaperssona9455ab2015-07-31 06:10:09 -0700581 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
582 }
583
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700584 const vpx_codec_err_t rv = vpx_codec_enc_init(
585 encoder_, vpx_codec_vp9_cx(), config_,
586 config_->g_bit_depth == VPX_BITS_8 ? 0 : VPX_CODEC_USE_HIGHBITDEPTH);
587 if (rv != VPX_CODEC_OK) {
588 RTC_LOG(LS_ERROR) << "Init error: " << vpx_codec_err_to_string(rv);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000589 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
590 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000591 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_);
592 vpx_codec_control(encoder_, VP8E_SET_MAX_INTRA_BITRATE_PCT,
593 rc_max_intra_target_);
594 vpx_codec_control(encoder_, VP9E_SET_AQ_MODE,
hta257dc392016-10-25 09:05:06 -0700595 inst->VP9().adaptiveQpMode ? 3 : 0);
asaperssona9455ab2015-07-31 06:10:09 -0700596
jianj822e5932017-07-12 16:09:58 -0700597 vpx_codec_control(encoder_, VP9E_SET_FRAME_PARALLEL_DECODING, 0);
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200598
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200599 if (is_svc_) {
600 vpx_codec_control(encoder_, VP9E_SET_SVC, 1);
601 vpx_codec_control(encoder_, VP9E_SET_SVC_PARAMETERS, &svc_params_);
asaperssona9455ab2015-07-31 06:10:09 -0700602 }
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200603
604 if (num_spatial_layers_ > 1) {
605 switch (inter_layer_pred_) {
606 case InterLayerPredMode::kOn:
607 vpx_codec_control(encoder_, VP9E_SET_SVC_INTER_LAYER_PRED, 0);
608 break;
609 case InterLayerPredMode::kOff:
610 vpx_codec_control(encoder_, VP9E_SET_SVC_INTER_LAYER_PRED, 1);
611 break;
612 case InterLayerPredMode::kOnKeyPic:
613 vpx_codec_control(encoder_, VP9E_SET_SVC_INTER_LAYER_PRED, 2);
614 break;
615 default:
616 RTC_NOTREACHED();
617 }
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200618
Sergey Silkinee203362018-05-30 11:34:08 +0200619 // Configure encoder to drop entire superframe whenever it needs to drop
620 // a layer. This mode is prefered over per-layer dropping which causes
621 // quality flickering and is not compatible with RTP non-flexible mode.
622 vpx_svc_frame_drop_t svc_drop_frame;
623 memset(&svc_drop_frame, 0, sizeof(svc_drop_frame));
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100624 svc_drop_frame.framedrop_mode =
625 full_superframe_drop_ ? FULL_SUPERFRAME_DROP : CONSTRAINED_LAYER_DROP;
Sergey Silkind45b3452018-05-31 16:00:24 +0200626 svc_drop_frame.max_consec_drop = std::numeric_limits<int>::max();
Sergey Silkinee203362018-05-30 11:34:08 +0200627 for (size_t i = 0; i < num_spatial_layers_; ++i) {
628 svc_drop_frame.framedrop_thresh[i] = config_->rc_dropframe_thresh;
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200629 }
Sergey Silkinee203362018-05-30 11:34:08 +0200630 vpx_codec_control(encoder_, VP9E_SET_SVC_FRAME_DROP_LAYER, &svc_drop_frame);
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200631 }
632
asaperssona9455ab2015-07-31 06:10:09 -0700633 // Register callback for getting each spatial layer.
634 vpx_codec_priv_output_cx_pkt_cb_pair_t cbp = {
philipelcce46fc2015-12-21 03:04:49 -0800635 VP9EncoderImpl::EncoderOutputCodedPacketCallback,
636 reinterpret_cast<void*>(this)};
637 vpx_codec_control(encoder_, VP9E_REGISTER_CX_CALLBACK,
638 reinterpret_cast<void*>(&cbp));
asaperssona9455ab2015-07-31 06:10:09 -0700639
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000640 // Control function to set the number of column tiles in encoding a frame, in
641 // log2 unit: e.g., 0 = 1 tile column, 1 = 2 tile columns, 2 = 4 tile columns.
642 // The number tile columns will be capped by the encoder based on image size
643 // (minimum width of tile column is 256 pixels, maximum is 4096).
644 vpx_codec_control(encoder_, VP9E_SET_TILE_COLUMNS, (config_->g_threads >> 1));
jianjcb5d1152017-03-28 23:56:08 -0700645
646 // Turn on row-based multithreading.
647 vpx_codec_control(encoder_, VP9E_SET_ROW_MT, 1);
jianj6bf57e32017-06-05 13:43:49 -0700648
Alex Glaznevfecb7c32016-03-31 14:23:27 -0700649#if !defined(WEBRTC_ARCH_ARM) && !defined(WEBRTC_ARCH_ARM64) && \
Yves Gerey665174f2018-06-19 15:03:05 +0200650 !defined(ANDROID)
jianj6bf57e32017-06-05 13:43:49 -0700651 // Do not enable the denoiser on ARM since optimization is pending.
652 // Denoiser is on by default on other platforms.
marpan@webrtc.org16a87b92015-03-05 22:19:00 +0000653 vpx_codec_control(encoder_, VP9E_SET_NOISE_SENSITIVITY,
hta257dc392016-10-25 09:05:06 -0700654 inst->VP9().denoisingOn ? 1 : 0);
marpan@webrtc.org16a87b92015-03-05 22:19:00 +0000655#endif
jianj6bf57e32017-06-05 13:43:49 -0700656
Niels Möllere3cf3d02018-06-13 11:52:16 +0200657 if (codec_.mode == VideoCodecMode::kScreensharing) {
ivica242d6382015-09-04 06:13:23 -0700658 // Adjust internal parameters to screen content.
659 vpx_codec_control(encoder_, VP9E_SET_TUNE_CONTENT, 1);
ivica242d6382015-09-04 06:13:23 -0700660 }
Marco2520e722015-09-16 14:05:00 -0700661 // Enable encoder skip of static/low content blocks.
662 vpx_codec_control(encoder_, VP8E_SET_STATIC_THRESHOLD, 1);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000663 inited_ = true;
664 return WEBRTC_VIDEO_CODEC_OK;
665}
666
667uint32_t VP9EncoderImpl::MaxIntraTarget(uint32_t optimal_buffer_size) {
668 // Set max to the optimal buffer level (normalized by target BR),
669 // and scaled by a scale_par.
670 // Max target size = scale_par * optimal_buffer_size * targetBR[Kbps].
671 // This value is presented in percentage of perFrameBw:
672 // perFrameBw = targetBR[Kbps] * 1000 / framerate.
673 // The target in % is as follows:
674 float scale_par = 0.5;
675 uint32_t target_pct =
676 optimal_buffer_size * scale_par * codec_.maxFramerate / 10;
677 // Don't go below 3 times the per frame bandwidth.
678 const uint32_t min_intra_size = 300;
philipelcce46fc2015-12-21 03:04:49 -0800679 return (target_pct < min_intra_size) ? min_intra_size : target_pct;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000680}
681
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700682int VP9EncoderImpl::Encode(const VideoFrame& input_image,
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000683 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700684 const std::vector<FrameType>* frame_types) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000685 if (!inited_) {
686 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
687 }
sprang3958ed82017-08-17 08:12:10 -0700688 if (encoded_complete_callback_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000689 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
690 }
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200691
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000692 // We only support one stream at the moment.
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200693 if (frame_types && !frame_types->empty()) {
694 if ((*frame_types)[0] == kVideoFrameKey) {
695 force_key_frame_ = true;
696 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000697 }
Sergey Silkind902d582018-05-18 17:31:19 +0200698
Ilya Nikolaevskiyba2840c2018-11-27 11:42:08 +0100699 size_t first_active_spatial_layer_id = 0;
700 if (VideoCodecMode::kScreensharing == codec_.mode) {
Sergey Silkin96f2c972018-09-05 21:07:17 +0200701 vpx_svc_layer_id_t layer_id = {0};
Ilya Nikolaevskiyba2840c2018-11-27 11:42:08 +0100702 if (!force_key_frame_) {
703 // Skip encoding spatial layer frames if their target frame rate is lower
704 // than actual input frame rate.
705 const size_t gof_idx = (pics_since_key_ + 1) % gof_.num_frames_in_gof;
706 layer_id.temporal_layer_id = gof_.temporal_idx[gof_idx];
Sergey Silkin96f2c972018-09-05 21:07:17 +0200707
Ilya Nikolaevskiyba2840c2018-11-27 11:42:08 +0100708 const uint32_t frame_timestamp_ms =
709 1000 * input_image.timestamp() / kVideoPayloadTypeFrequency;
Sergey Silkin96f2c972018-09-05 21:07:17 +0200710
Ilya Nikolaevskiyba2840c2018-11-27 11:42:08 +0100711 for (uint8_t sl_idx = 0; sl_idx < num_active_spatial_layers_; ++sl_idx) {
712 if (framerate_controller_[sl_idx].DropFrame(frame_timestamp_ms)) {
713 ++layer_id.spatial_layer_id;
714 } else {
715 break;
716 }
717 }
718
719 RTC_DCHECK_LE(layer_id.spatial_layer_id, num_active_spatial_layers_);
720 if (layer_id.spatial_layer_id >= num_active_spatial_layers_) {
721 // Drop entire picture.
722 return WEBRTC_VIDEO_CODEC_OK;
Sergey Silkin96f2c972018-09-05 21:07:17 +0200723 }
724 }
Ilya Nikolaevskiyba2840c2018-11-27 11:42:08 +0100725 first_active_spatial_layer_id = layer_id.spatial_layer_id;
Sergey Silkin96f2c972018-09-05 21:07:17 +0200726 vpx_codec_control(encoder_, VP9E_SET_SVC_LAYER_ID, &layer_id);
Sergey Silkind902d582018-05-18 17:31:19 +0200727 }
728
kwiberg352444f2016-11-28 15:58:53 -0800729 RTC_DCHECK_EQ(input_image.width(), raw_->d_w);
730 RTC_DCHECK_EQ(input_image.height(), raw_->d_h);
asaperssona9455ab2015-07-31 06:10:09 -0700731
732 // Set input image for use in the callback.
733 // This was necessary since you need some information from input_image.
734 // You can save only the necessary information (such as timestamp) instead of
735 // doing this.
736 input_image_ = &input_image;
737
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700738 // Keep reference to buffer until encode completes.
739 rtc::scoped_refptr<I420BufferInterface> i420_buffer;
740 rtc::scoped_refptr<I010BufferInterface> i010_buffer;
741 switch (profile_) {
742 case VP9Profile::kProfile0: {
743 i420_buffer = input_image.video_frame_buffer()->ToI420();
744 // Image in vpx_image_t format.
745 // Input image is const. VPX's raw image is not defined as const.
746 raw_->planes[VPX_PLANE_Y] = const_cast<uint8_t*>(i420_buffer->DataY());
747 raw_->planes[VPX_PLANE_U] = const_cast<uint8_t*>(i420_buffer->DataU());
748 raw_->planes[VPX_PLANE_V] = const_cast<uint8_t*>(i420_buffer->DataV());
749 raw_->stride[VPX_PLANE_Y] = i420_buffer->StrideY();
750 raw_->stride[VPX_PLANE_U] = i420_buffer->StrideU();
751 raw_->stride[VPX_PLANE_V] = i420_buffer->StrideV();
752 break;
753 }
754 case VP9Profile::kProfile2: {
755 // We can inject kI010 frames directly for encode. All other formats
756 // should be converted to it.
757 switch (input_image.video_frame_buffer()->type()) {
758 case VideoFrameBuffer::Type::kI010: {
759 i010_buffer = input_image.video_frame_buffer()->GetI010();
760 break;
761 }
762 default: {
763 i010_buffer =
764 I010Buffer::Copy(*input_image.video_frame_buffer()->ToI420());
765 }
766 }
767 raw_->planes[VPX_PLANE_Y] = const_cast<uint8_t*>(
768 reinterpret_cast<const uint8_t*>(i010_buffer->DataY()));
769 raw_->planes[VPX_PLANE_U] = const_cast<uint8_t*>(
770 reinterpret_cast<const uint8_t*>(i010_buffer->DataU()));
771 raw_->planes[VPX_PLANE_V] = const_cast<uint8_t*>(
772 reinterpret_cast<const uint8_t*>(i010_buffer->DataV()));
773 raw_->stride[VPX_PLANE_Y] = i010_buffer->StrideY() * 2;
774 raw_->stride[VPX_PLANE_U] = i010_buffer->StrideU() * 2;
775 raw_->stride[VPX_PLANE_V] = i010_buffer->StrideV() * 2;
776 break;
777 }
778 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000779
philipelcfc319b2015-11-10 07:17:23 -0800780 vpx_enc_frame_flags_t flags = 0;
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200781 if (force_key_frame_) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000782 flags = VPX_EFLAG_FORCE_KF;
783 }
philipelcfc319b2015-11-10 07:17:23 -0800784
Sergey Silkin390f3582018-10-01 17:13:50 +0200785 if (external_ref_control_) {
Ilya Nikolaevskiyba2840c2018-11-27 11:42:08 +0100786 vpx_svc_ref_frame_config_t ref_config =
787 SetReferences(force_key_frame_, first_active_spatial_layer_id);
Sergey Silkina85995a2018-10-02 10:28:10 +0200788
789 if (VideoCodecMode::kScreensharing == codec_.mode) {
790 for (uint8_t sl_idx = 0; sl_idx < num_active_spatial_layers_; ++sl_idx) {
791 ref_config.duration[sl_idx] = static_cast<int64_t>(
792 90000 / framerate_controller_[sl_idx].GetTargetRate());
793 }
794 }
795
Sergey Silkin390f3582018-10-01 17:13:50 +0200796 vpx_codec_control(encoder_, VP9E_SET_SVC_REF_FRAME_CONFIG, &ref_config);
797 }
798
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100799 first_frame_in_picture_ = true;
800
Sergey Silkin96f2c972018-09-05 21:07:17 +0200801 // TODO(ssilkin): Frame duration should be specified per spatial layer
802 // since their frame rate can be different. For now calculate frame duration
803 // based on target frame rate of the highest spatial layer, which frame rate
804 // is supposed to be equal or higher than frame rate of low spatial layers.
805 // Also, timestamp should represent actual time passed since previous frame
806 // (not 'expected' time). Then rate controller can drain buffer more
807 // accurately.
808 RTC_DCHECK_GE(framerate_controller_.size(), num_active_spatial_layers_);
Sergey Silkinf87bb462018-09-17 09:37:37 +0200809 float target_framerate_fps =
810 (codec_.mode == VideoCodecMode::kScreensharing)
811 ? framerate_controller_[num_active_spatial_layers_ - 1]
812 .GetTargetRate()
813 : codec_.maxFramerate;
814 uint32_t duration = static_cast<uint32_t>(90000 / target_framerate_fps);
Emircan Uysaler0823eec2018-07-13 17:10:00 -0700815 const vpx_codec_err_t rv = vpx_codec_encode(encoder_, raw_, timestamp_,
816 duration, flags, VPX_DL_REALTIME);
817 if (rv != VPX_CODEC_OK) {
818 RTC_LOG(LS_ERROR) << "Encoding error: " << vpx_codec_err_to_string(rv)
819 << "\n"
820 << "Details: " << vpx_codec_error(encoder_) << "\n"
821 << vpx_codec_error_detail(encoder_);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000822 return WEBRTC_VIDEO_CODEC_ERROR;
823 }
824 timestamp_ += duration;
asaperssona9455ab2015-07-31 06:10:09 -0700825
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100826 if (!full_superframe_drop_) {
827 const bool end_of_picture = true;
828 DeliverBufferedFrame(end_of_picture);
829 }
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200830
asaperssona9455ab2015-07-31 06:10:09 -0700831 return WEBRTC_VIDEO_CODEC_OK;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000832}
833
834void VP9EncoderImpl::PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
Niels Möllerd3b8c632018-08-27 15:33:42 +0200835 absl::optional<int>* spatial_idx,
philipelcce46fc2015-12-21 03:04:49 -0800836 const vpx_codec_cx_pkt& pkt,
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100837 uint32_t timestamp) {
sprang3958ed82017-08-17 08:12:10 -0700838 RTC_CHECK(codec_specific != nullptr);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000839 codec_specific->codecType = kVideoCodecVP9;
philipelcce46fc2015-12-21 03:04:49 -0800840 CodecSpecificInfoVP9* vp9_info = &(codec_specific->codecSpecific.VP9);
Sergey Silkin07f80cc2018-04-09 13:11:59 +0200841
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100842 vp9_info->first_frame_in_picture = first_frame_in_picture_;
Sergey Silkin738b7e92018-08-23 16:37:27 +0200843 vp9_info->flexible_mode = is_flexible_mode_;
hta257dc392016-10-25 09:05:06 -0700844 vp9_info->ss_data_available =
Sergey Silkin738b7e92018-08-23 16:37:27 +0200845 (pkt.data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
asaperssona9455ab2015-07-31 06:10:09 -0700846
847 vpx_svc_layer_id_t layer_id = {0};
848 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
849
sprang3958ed82017-08-17 08:12:10 -0700850 RTC_CHECK_GT(num_temporal_layers_, 0);
“Michael23c5a992018-06-21 11:07:21 -0500851 RTC_CHECK_GT(num_active_spatial_layers_, 0);
asaperssona9455ab2015-07-31 06:10:09 -0700852 if (num_temporal_layers_ == 1) {
sprang3958ed82017-08-17 08:12:10 -0700853 RTC_CHECK_EQ(layer_id.temporal_layer_id, 0);
asaperssona9455ab2015-07-31 06:10:09 -0700854 vp9_info->temporal_idx = kNoTemporalIdx;
855 } else {
856 vp9_info->temporal_idx = layer_id.temporal_layer_id;
857 }
“Michael23c5a992018-06-21 11:07:21 -0500858 if (num_active_spatial_layers_ == 1) {
sprang3958ed82017-08-17 08:12:10 -0700859 RTC_CHECK_EQ(layer_id.spatial_layer_id, 0);
Niels Möllerd3b8c632018-08-27 15:33:42 +0200860 *spatial_idx = absl::nullopt;
asaperssona9455ab2015-07-31 06:10:09 -0700861 } else {
Niels Möllerd3b8c632018-08-27 15:33:42 +0200862 *spatial_idx = layer_id.spatial_layer_id;
asaperssona9455ab2015-07-31 06:10:09 -0700863 }
864 if (layer_id.spatial_layer_id != 0) {
865 vp9_info->ss_data_available = false;
866 }
867
asaperssona9455ab2015-07-31 06:10:09 -0700868 // TODO(asapersson): this info has to be obtained from the encoder.
asaperssoncb50c962015-11-18 01:58:55 -0800869 vp9_info->temporal_up_switch = false;
asaperssona9455ab2015-07-31 06:10:09 -0700870
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200871 if (pkt.data.frame.flags & VPX_FRAME_IS_KEY) {
872 pics_since_key_ = 0;
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100873 } else if (first_frame_in_picture_) {
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200874 ++pics_since_key_;
asaperssona9455ab2015-07-31 06:10:09 -0700875 }
876
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200877 const bool is_key_pic = (pics_since_key_ == 0);
878 const bool is_inter_layer_pred_allowed =
879 (inter_layer_pred_ == InterLayerPredMode::kOn ||
880 (inter_layer_pred_ == InterLayerPredMode::kOnKeyPic && is_key_pic));
881
882 // Always set inter_layer_predicted to true on high layer frame if inter-layer
883 // prediction (ILP) is allowed even if encoder didn't actually use it.
884 // Setting inter_layer_predicted to false would allow receiver to decode high
885 // layer frame without decoding low layer frame. If that would happen (e.g.
886 // if low layer frame is lost) then receiver won't be able to decode next high
887 // layer frame which uses ILP.
888 vp9_info->inter_layer_predicted =
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100889 first_frame_in_picture_ ? false : is_inter_layer_pred_allowed;
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200890
Sergey Silkinc5f152d2018-09-26 13:28:50 +0200891 // Mark all low spatial layer frames as references (not just frames of
892 // active low spatial layers) if inter-layer prediction is enabled since
893 // these frames are indirect references of high spatial layer, which can
894 // later be enabled without key frame.
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200895 vp9_info->non_ref_for_inter_layer_pred =
Sergey Silkinc5f152d2018-09-26 13:28:50 +0200896 !is_inter_layer_pred_allowed ||
897 layer_id.spatial_layer_id + 1 == num_spatial_layers_;
asapersson00ac85e2015-11-11 05:30:48 -0800898
ivica7f6a6fc2015-09-08 02:40:29 -0700899 // Always populate this, so that the packetizer can properly set the marker
900 // bit.
“Michael23c5a992018-06-21 11:07:21 -0500901 vp9_info->num_spatial_layers = num_active_spatial_layers_;
philipelcfc319b2015-11-10 07:17:23 -0800902
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200903 vp9_info->num_ref_pics = 0;
904 if (vp9_info->flexible_mode) {
905 vp9_info->gof_idx = kNoGofIdx;
906 FillReferenceIndices(pkt, pics_since_key_, vp9_info->inter_layer_predicted,
907 vp9_info);
Sergey Silkin390f3582018-10-01 17:13:50 +0200908 // TODO(webrtc:9794): Add fake reference to empty reference list to
909 // workaround the frame buffer issue on receiver.
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200910 } else {
911 vp9_info->gof_idx =
912 static_cast<uint8_t>(pics_since_key_ % gof_.num_frames_in_gof);
913 vp9_info->temporal_up_switch = gof_.temporal_up_switch[vp9_info->gof_idx];
914 vp9_info->num_ref_pics = gof_.num_ref_pics[vp9_info->gof_idx];
915 }
916
917 vp9_info->inter_pic_predicted = (!is_key_pic && vp9_info->num_ref_pics > 0);
918
asaperssona9455ab2015-07-31 06:10:09 -0700919 if (vp9_info->ss_data_available) {
asaperssona9455ab2015-07-31 06:10:09 -0700920 vp9_info->spatial_layer_resolution_present = true;
“Michael23c5a992018-06-21 11:07:21 -0500921 for (size_t i = 0; i < num_active_spatial_layers_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200922 vp9_info->width[i] = codec_.width * svc_params_.scaling_factor_num[i] /
johannkoenig8225c402017-01-26 13:23:44 -0800923 svc_params_.scaling_factor_den[i];
Yves Gerey665174f2018-06-19 15:03:05 +0200924 vp9_info->height[i] = codec_.height * svc_params_.scaling_factor_num[i] /
johannkoenig8225c402017-01-26 13:23:44 -0800925 svc_params_.scaling_factor_den[i];
asaperssona9455ab2015-07-31 06:10:09 -0700926 }
Sergey Silkin738b7e92018-08-23 16:37:27 +0200927 if (vp9_info->flexible_mode) {
928 vp9_info->gof.num_frames_in_gof = 0;
929 } else {
asaperssona9455ab2015-07-31 06:10:09 -0700930 vp9_info->gof.CopyGofInfoVP9(gof_);
931 }
932 }
Sergey Silkin88ce4ef2018-11-23 13:59:24 +0100933
934 first_frame_in_picture_ = false;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000935}
936
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200937void VP9EncoderImpl::FillReferenceIndices(const vpx_codec_cx_pkt& pkt,
938 const size_t pic_num,
939 const bool inter_layer_predicted,
940 CodecSpecificInfoVP9* vp9_info) {
941 vpx_svc_layer_id_t layer_id = {0};
942 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
943
Sergey Silkin76be2952018-08-21 12:30:33 +0200944 const bool is_key_frame =
945 (pkt.data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200946
947 std::vector<RefFrameBuffer> ref_buf_list;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200948
Sergey Silkin76be2952018-08-21 12:30:33 +0200949 if (is_svc_) {
950 vpx_svc_ref_frame_config_t enc_layer_conf = {{0}};
951 vpx_codec_control(encoder_, VP9E_GET_SVC_REF_FRAME_CONFIG, &enc_layer_conf);
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200952
Sergey Silkin76be2952018-08-21 12:30:33 +0200953 if (enc_layer_conf.reference_last[layer_id.spatial_layer_id]) {
954 const size_t fb_idx =
955 enc_layer_conf.lst_fb_idx[layer_id.spatial_layer_id];
956 RTC_DCHECK(ref_buf_.find(fb_idx) != ref_buf_.end());
Sergey Silkin82276592018-08-27 14:54:20 +0200957 if (std::find(ref_buf_list.begin(), ref_buf_list.end(),
958 ref_buf_.at(fb_idx)) == ref_buf_list.end()) {
959 ref_buf_list.push_back(ref_buf_.at(fb_idx));
960 }
Sergey Silkin76be2952018-08-21 12:30:33 +0200961 }
962
963 if (enc_layer_conf.reference_alt_ref[layer_id.spatial_layer_id]) {
964 const size_t fb_idx =
965 enc_layer_conf.alt_fb_idx[layer_id.spatial_layer_id];
966 RTC_DCHECK(ref_buf_.find(fb_idx) != ref_buf_.end());
Sergey Silkin82276592018-08-27 14:54:20 +0200967 if (std::find(ref_buf_list.begin(), ref_buf_list.end(),
968 ref_buf_.at(fb_idx)) == ref_buf_list.end()) {
969 ref_buf_list.push_back(ref_buf_.at(fb_idx));
970 }
Sergey Silkin76be2952018-08-21 12:30:33 +0200971 }
972
973 if (enc_layer_conf.reference_golden[layer_id.spatial_layer_id]) {
974 const size_t fb_idx =
975 enc_layer_conf.gld_fb_idx[layer_id.spatial_layer_id];
976 RTC_DCHECK(ref_buf_.find(fb_idx) != ref_buf_.end());
Sergey Silkin82276592018-08-27 14:54:20 +0200977 if (std::find(ref_buf_list.begin(), ref_buf_list.end(),
978 ref_buf_.at(fb_idx)) == ref_buf_list.end()) {
979 ref_buf_list.push_back(ref_buf_.at(fb_idx));
980 }
Sergey Silkin76be2952018-08-21 12:30:33 +0200981 }
982 } else if (!is_key_frame) {
983 RTC_DCHECK_EQ(num_spatial_layers_, 1);
984 RTC_DCHECK_EQ(num_temporal_layers_, 1);
985 // In non-SVC mode encoder doesn't provide reference list. Assume each frame
986 // refers previous one, which is stored in buffer 0.
987 ref_buf_list.push_back(ref_buf_.at(0));
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200988 }
989
990 size_t max_ref_temporal_layer_id = 0;
991
Ilya Nikolaevskiyba2840c2018-11-27 11:42:08 +0100992 std::vector<size_t> ref_pid_list;
993
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200994 vp9_info->num_ref_pics = 0;
995 for (const RefFrameBuffer& ref_buf : ref_buf_list) {
996 RTC_DCHECK_LE(ref_buf.pic_num, pic_num);
997 if (ref_buf.pic_num < pic_num) {
998 if (inter_layer_pred_ != InterLayerPredMode::kOn) {
999 // RTP spec limits temporal prediction to the same spatial layer.
1000 // It is safe to ignore this requirement if inter-layer prediction is
1001 // enabled for all frames when all base frames are relayed to receiver.
1002 RTC_DCHECK_EQ(ref_buf.spatial_layer_id, layer_id.spatial_layer_id);
1003 }
1004 RTC_DCHECK_LE(ref_buf.temporal_layer_id, layer_id.temporal_layer_id);
1005
Ilya Nikolaevskiyba2840c2018-11-27 11:42:08 +01001006 // Encoder may reference several spatial layers on the same previous
1007 // frame in case if some spatial layers are skipped on the current frame.
1008 // We shouldn't put duplicate references as it may break some old
1009 // clients and isn't RTP compatible.
1010 if (std::find(ref_pid_list.begin(), ref_pid_list.end(),
1011 ref_buf.pic_num) != ref_pid_list.end()) {
1012 continue;
1013 }
1014 ref_pid_list.push_back(ref_buf.pic_num);
1015
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001016 const size_t p_diff = pic_num - ref_buf.pic_num;
1017 RTC_DCHECK_LE(p_diff, 127UL);
1018
1019 vp9_info->p_diff[vp9_info->num_ref_pics] = static_cast<uint8_t>(p_diff);
1020 ++vp9_info->num_ref_pics;
1021
1022 max_ref_temporal_layer_id =
1023 std::max(max_ref_temporal_layer_id, ref_buf.temporal_layer_id);
1024 } else {
1025 RTC_DCHECK(inter_layer_predicted);
1026 // RTP spec only allows to use previous spatial layer for inter-layer
1027 // prediction.
1028 RTC_DCHECK_EQ(ref_buf.spatial_layer_id + 1, layer_id.spatial_layer_id);
1029 }
1030 }
1031
1032 vp9_info->temporal_up_switch =
1033 (max_ref_temporal_layer_id <
1034 static_cast<size_t>(layer_id.temporal_layer_id));
1035}
1036
1037void VP9EncoderImpl::UpdateReferenceBuffers(const vpx_codec_cx_pkt& pkt,
1038 const size_t pic_num) {
1039 vpx_svc_layer_id_t layer_id = {0};
1040 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
1041
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001042 const bool is_key_frame =
1043 (pkt.data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
1044
1045 RefFrameBuffer frame_buf(pic_num, layer_id.spatial_layer_id,
1046 layer_id.temporal_layer_id);
1047
1048 if (is_key_frame && layer_id.spatial_layer_id == 0) {
1049 // Key frame updates all ref buffers.
1050 for (size_t i = 0; i < kNumVp9Buffers; ++i) {
1051 ref_buf_[i] = frame_buf;
1052 }
Sergey Silkin76be2952018-08-21 12:30:33 +02001053 } else if (is_svc_) {
1054 vpx_svc_ref_frame_config_t enc_layer_conf = {{0}};
1055 vpx_codec_control(encoder_, VP9E_GET_SVC_REF_FRAME_CONFIG, &enc_layer_conf);
1056
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001057 if (enc_layer_conf.update_last[layer_id.spatial_layer_id]) {
1058 ref_buf_[enc_layer_conf.lst_fb_idx[layer_id.spatial_layer_id]] =
1059 frame_buf;
1060 }
1061
1062 if (enc_layer_conf.update_alt_ref[layer_id.spatial_layer_id]) {
1063 ref_buf_[enc_layer_conf.alt_fb_idx[layer_id.spatial_layer_id]] =
1064 frame_buf;
1065 }
1066
1067 if (enc_layer_conf.update_golden[layer_id.spatial_layer_id]) {
1068 ref_buf_[enc_layer_conf.gld_fb_idx[layer_id.spatial_layer_id]] =
1069 frame_buf;
1070 }
Sergey Silkin76be2952018-08-21 12:30:33 +02001071 } else {
1072 RTC_DCHECK_EQ(num_spatial_layers_, 1);
1073 RTC_DCHECK_EQ(num_temporal_layers_, 1);
1074 // In non-svc mode encoder doesn't provide reference list. Assume each frame
1075 // is reference and stored in buffer 0.
1076 ref_buf_[0] = frame_buf;
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001077 }
1078}
1079
Ilya Nikolaevskiyba2840c2018-11-27 11:42:08 +01001080vpx_svc_ref_frame_config_t VP9EncoderImpl::SetReferences(
1081 bool is_key_pic,
1082 size_t first_active_spatial_layer_id) {
Sergey Silkin390f3582018-10-01 17:13:50 +02001083 // kRefBufIdx, kUpdBufIdx need to be updated to support longer GOFs.
1084 RTC_DCHECK_LE(gof_.num_frames_in_gof, 4);
1085
1086 vpx_svc_ref_frame_config_t ref_config;
1087 memset(&ref_config, 0, sizeof(ref_config));
1088
1089 const size_t num_temporal_refs = std::max(1, num_temporal_layers_ - 1);
1090 const bool is_inter_layer_pred_allowed =
1091 inter_layer_pred_ == InterLayerPredMode::kOn ||
1092 (inter_layer_pred_ == InterLayerPredMode::kOnKeyPic && is_key_pic);
1093 absl::optional<int> last_updated_buf_idx;
1094
1095 // Put temporal reference to LAST and spatial reference to GOLDEN. Update
1096 // frame buffer (i.e. store encoded frame) if current frame is a temporal
1097 // reference (i.e. it belongs to a low temporal layer) or it is a spatial
1098 // reference. In later case, always store spatial reference in the last
1099 // reference frame buffer.
1100 // For the case of 3 temporal and 3 spatial layers we need 6 frame buffers
1101 // for temporal references plus 1 buffer for spatial reference. 7 buffers
1102 // in total.
1103
1104 for (size_t sl_idx = 0; sl_idx < num_active_spatial_layers_; ++sl_idx) {
1105 const size_t gof_idx = pics_since_key_ % gof_.num_frames_in_gof;
1106
1107 if (!is_key_pic) {
1108 // Set up temporal reference.
1109 const int buf_idx = sl_idx * num_temporal_refs + kRefBufIdx[gof_idx];
1110
1111 // Last reference frame buffer is reserved for spatial reference. It is
1112 // not supposed to be used for temporal prediction.
1113 RTC_DCHECK_LT(buf_idx, kNumVp9Buffers - 1);
1114
1115 // Sanity check that reference picture number is smaller than current
1116 // picture number.
1117 const size_t curr_pic_num = pics_since_key_ + 1;
1118 RTC_DCHECK_LT(ref_buf_[buf_idx].pic_num, curr_pic_num);
1119 const size_t pid_diff = curr_pic_num - ref_buf_[buf_idx].pic_num;
1120
1121 // Below code assumes single temporal referecence.
1122 RTC_DCHECK_EQ(gof_.num_ref_pics[gof_idx], 1);
1123 if (pid_diff == gof_.pid_diff[gof_idx][0]) {
1124 ref_config.lst_fb_idx[sl_idx] = buf_idx;
1125 ref_config.reference_last[sl_idx] = 1;
1126 } else {
1127 // This reference doesn't match with one specified by GOF. This can
1128 // only happen if spatial layer is enabled dynamically without key
1129 // frame. Spatial prediction is supposed to be enabled in this case.
1130 RTC_DCHECK(is_inter_layer_pred_allowed);
1131 }
1132 }
1133
Ilya Nikolaevskiyba2840c2018-11-27 11:42:08 +01001134 if (is_inter_layer_pred_allowed && sl_idx > first_active_spatial_layer_id) {
Sergey Silkin390f3582018-10-01 17:13:50 +02001135 // Set up spatial reference.
1136 RTC_DCHECK(last_updated_buf_idx);
1137 ref_config.gld_fb_idx[sl_idx] = *last_updated_buf_idx;
1138 ref_config.reference_golden[sl_idx] = 1;
1139 } else {
Ilya Nikolaevskiyba2840c2018-11-27 11:42:08 +01001140 RTC_DCHECK(ref_config.reference_last[sl_idx] != 0 ||
1141 sl_idx == first_active_spatial_layer_id ||
Sergey Silkin390f3582018-10-01 17:13:50 +02001142 inter_layer_pred_ == InterLayerPredMode::kOff);
1143 }
1144
1145 last_updated_buf_idx.reset();
1146
1147 if (gof_.temporal_idx[gof_idx] <= num_temporal_layers_ - 1) {
1148 last_updated_buf_idx = sl_idx * num_temporal_refs + kUpdBufIdx[gof_idx];
1149
1150 // Ensure last frame buffer is not used for temporal prediction (it is
1151 // reserved for spatial reference).
1152 RTC_DCHECK_LT(*last_updated_buf_idx, kNumVp9Buffers - 1);
1153 } else if (is_inter_layer_pred_allowed) {
1154 last_updated_buf_idx = kNumVp9Buffers - 1;
1155 }
1156
1157 if (last_updated_buf_idx) {
1158 ref_config.update_buffer_slot[sl_idx] = 1 << *last_updated_buf_idx;
1159 }
1160 }
1161
1162 return ref_config;
1163}
1164
asaperssona9455ab2015-07-31 06:10:09 -07001165int VP9EncoderImpl::GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt) {
asapersson86956de2016-01-26 01:05:20 -08001166 RTC_DCHECK_EQ(pkt->kind, VPX_CODEC_CX_FRAME_PKT);
asaperssona9455ab2015-07-31 06:10:09 -07001167
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001168 if (pkt->data.frame.sz == 0) {
1169 // Ignore dropped frame.
1170 return WEBRTC_VIDEO_CODEC_OK;
1171 }
1172
Sergey Silkin07f80cc2018-04-09 13:11:59 +02001173 vpx_svc_layer_id_t layer_id = {0};
1174 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
1175
Sergey Silkin88ce4ef2018-11-23 13:59:24 +01001176 if (!full_superframe_drop_) {
1177 // Deliver buffered low spatial layer frame.
1178 const bool end_of_picture = false;
1179 DeliverBufferedFrame(end_of_picture);
1180 }
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001181
asaperssond9f641e2016-01-21 01:11:35 -08001182 if (pkt->data.frame.sz > encoded_image_._size) {
1183 delete[] encoded_image_._buffer;
1184 encoded_image_._size = pkt->data.frame.sz;
1185 encoded_image_._buffer = new uint8_t[encoded_image_._size];
1186 }
asapersson86956de2016-01-26 01:05:20 -08001187 memcpy(encoded_image_._buffer, pkt->data.frame.buf, pkt->data.frame.sz);
1188 encoded_image_._length = pkt->data.frame.sz;
asaperssond9f641e2016-01-21 01:11:35 -08001189
Sergey Silkinbd0954e2018-05-03 14:14:09 +02001190 const bool is_key_frame =
1191 (pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
1192 // Ensure encoder issued key frame on request.
1193 RTC_DCHECK(is_key_frame || !force_key_frame_);
1194
asaperssona9455ab2015-07-31 06:10:09 -07001195 // Check if encoded frame is a key frame.
asapersson86956de2016-01-26 01:05:20 -08001196 encoded_image_._frameType = kVideoFrameDelta;
Sergey Silkinbd0954e2018-05-03 14:14:09 +02001197 if (is_key_frame) {
Peter Boström49e196a2015-10-23 15:58:18 +02001198 encoded_image_._frameType = kVideoFrameKey;
Sergey Silkinbd0954e2018-05-03 14:14:09 +02001199 force_key_frame_ = false;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001200 }
asapersson86956de2016-01-26 01:05:20 -08001201 RTC_DCHECK_LE(encoded_image_._length, encoded_image_._size);
1202
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001203 memset(&codec_specific_, 0, sizeof(codec_specific_));
Niels Möllerd3b8c632018-08-27 15:33:42 +02001204 absl::optional<int> spatial_index;
1205 PopulateCodecSpecific(&codec_specific_, &spatial_index, *pkt,
Sergey Silkin88ce4ef2018-11-23 13:59:24 +01001206 input_image_->timestamp());
Niels Möllerd3b8c632018-08-27 15:33:42 +02001207 encoded_image_.SetSpatialIndex(spatial_index);
asaperssona9455ab2015-07-31 06:10:09 -07001208
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +02001209 if (is_flexible_mode_) {
1210 UpdateReferenceBuffers(*pkt, pics_since_key_);
1211 }
1212
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001213 TRACE_COUNTER1("webrtc", "EncodedFrameSize", encoded_image_._length);
Niels Möller23775882018-08-16 10:24:12 +02001214 encoded_image_.SetTimestamp(input_image_->timestamp());
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001215 encoded_image_.capture_time_ms_ = input_image_->render_time_ms();
1216 encoded_image_.rotation_ = input_image_->rotation();
Niels Möllere3cf3d02018-06-13 11:52:16 +02001217 encoded_image_.content_type_ = (codec_.mode == VideoCodecMode::kScreensharing)
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001218 ? VideoContentType::SCREENSHARE
1219 : VideoContentType::UNSPECIFIED;
1220 encoded_image_._encodedHeight =
1221 pkt->data.frame.height[layer_id.spatial_layer_id];
1222 encoded_image_._encodedWidth =
1223 pkt->data.frame.width[layer_id.spatial_layer_id];
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +02001224 encoded_image_.timing_.flags = VideoSendTiming::kInvalid;
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001225 int qp = -1;
1226 vpx_codec_control(encoder_, VP8E_GET_LAST_QUANTIZER, &qp);
1227 encoded_image_.qp_ = qp;
Johannes Kron4749e4e2018-11-21 10:18:18 +01001228 encoded_image_.SetColorSpace(input_image_->color_space());
ilnik04f4d122017-06-19 07:18:55 -07001229
Sergey Silkin88ce4ef2018-11-23 13:59:24 +01001230 if (full_superframe_drop_) {
1231 const bool end_of_picture = encoded_image_.SpatialIndex().value_or(0) + 1 ==
1232 num_active_spatial_layers_;
1233 DeliverBufferedFrame(end_of_picture);
1234 }
1235
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001236 return WEBRTC_VIDEO_CODEC_OK;
1237}
1238
Sergey Silkinbc0f0d32018-04-24 21:29:14 +02001239void VP9EncoderImpl::DeliverBufferedFrame(bool end_of_picture) {
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001240 if (encoded_image_._length > 0) {
Sergey Silkinbc0f0d32018-04-24 21:29:14 +02001241 codec_specific_.codecSpecific.VP9.end_of_picture = end_of_picture;
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001242
1243 // No data partitioning in VP9, so 1 partition only.
1244 int part_idx = 0;
1245 RTPFragmentationHeader frag_info;
1246 frag_info.VerifyAndAllocateFragmentationHeader(1);
1247 frag_info.fragmentationOffset[part_idx] = 0;
1248 frag_info.fragmentationLength[part_idx] = encoded_image_._length;
1249 frag_info.fragmentationPlType[part_idx] = 0;
1250 frag_info.fragmentationTimeDiff[part_idx] = 0;
1251
1252 encoded_complete_callback_->OnEncodedImage(encoded_image_, &codec_specific_,
1253 &frag_info);
1254 encoded_image_._length = 0;
Sergey Silkind902d582018-05-18 17:31:19 +02001255
Sergey Silkin96f2c972018-09-05 21:07:17 +02001256 if (codec_.mode == VideoCodecMode::kScreensharing) {
1257 const uint8_t spatial_idx = encoded_image_.SpatialIndex().value_or(0);
1258 const uint32_t frame_timestamp_ms =
Niels Möller23775882018-08-16 10:24:12 +02001259 1000 * encoded_image_.Timestamp() / kVideoPayloadTypeFrequency;
Sergey Silkin96f2c972018-09-05 21:07:17 +02001260 framerate_controller_[spatial_idx].AddFrame(frame_timestamp_ms);
Sergey Silkind902d582018-05-18 17:31:19 +02001261 }
Sergey Silkin2a1f1832018-04-04 11:45:41 +02001262 }
1263}
1264
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001265int VP9EncoderImpl::RegisterEncodeCompleteCallback(
1266 EncodedImageCallback* callback) {
1267 encoded_complete_callback_ = callback;
1268 return WEBRTC_VIDEO_CODEC_OK;
1269}
1270
Erik Språng727d1642018-11-07 16:54:15 +01001271VideoEncoder::EncoderInfo VP9EncoderImpl::GetEncoderInfo() const {
1272 EncoderInfo info;
1273 info.supports_native_handle = false;
1274 info.implementation_name = "libvpx";
1275 info.scaling_settings = VideoEncoder::ScalingSettings::kOff;
Erik Språngd3438aa2018-11-08 16:56:43 +01001276 info.has_trusted_rate_controller = trusted_rate_controller_;
Erik Språng727d1642018-11-07 16:54:15 +01001277 return info;
Peter Boströmb7d9a972015-12-18 16:01:11 +01001278}
1279
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001280VP9DecoderImpl::VP9DecoderImpl()
sprang3958ed82017-08-17 08:12:10 -07001281 : decode_complete_callback_(nullptr),
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001282 inited_(false),
sprang3958ed82017-08-17 08:12:10 -07001283 decoder_(nullptr),
philipel9d7d75b2018-04-04 13:03:01 +02001284 key_frame_required_(true) {}
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001285
1286VP9DecoderImpl::~VP9DecoderImpl() {
1287 inited_ = true; // in order to do the actual release
1288 Release();
Henrik Boström9695d852015-05-06 10:42:15 +02001289 int num_buffers_in_use = frame_buffer_pool_.GetNumBuffersInUse();
1290 if (num_buffers_in_use > 0) {
1291 // The frame buffers are reference counted and frames are exposed after
1292 // decoding. There may be valid usage cases where previous frames are still
1293 // referenced after ~VP9DecoderImpl that is not a leak.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001294 RTC_LOG(LS_INFO) << num_buffers_in_use << " Vp9FrameBuffers are still "
1295 << "referenced during ~VP9DecoderImpl.";
Henrik Boström9695d852015-05-06 10:42:15 +02001296 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001297}
1298
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001299int VP9DecoderImpl::InitDecode(const VideoCodec* inst, int number_of_cores) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001300 int ret_val = Release();
1301 if (ret_val < 0) {
1302 return ret_val;
1303 }
Sergey Silkinb674cd12018-10-09 10:59:06 +02001304
sprang3958ed82017-08-17 08:12:10 -07001305 if (decoder_ == nullptr) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +00001306 decoder_ = new vpx_codec_ctx_t;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001307 }
philipelcce46fc2015-12-21 03:04:49 -08001308 vpx_codec_dec_cfg_t cfg;
Sergey Silkinb674cd12018-10-09 10:59:06 +02001309 memset(&cfg, 0, sizeof(cfg));
1310
1311 // We want to use multithreading when decoding high resolution videos. But,
1312 // since we don't know resolution of input stream at this stage, we always
1313 // enable it.
1314 cfg.threads = std::min(number_of_cores, kMaxNumTiles4kVideo);
1315
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001316 vpx_codec_flags_t flags = 0;
1317 if (vpx_codec_dec_init(decoder_, vpx_codec_vp9_dx(), &cfg, flags)) {
1318 return WEBRTC_VIDEO_CODEC_MEMORY;
1319 }
Henrik Boström9695d852015-05-06 10:42:15 +02001320
1321 if (!frame_buffer_pool_.InitializeVpxUsePool(decoder_)) {
1322 return WEBRTC_VIDEO_CODEC_MEMORY;
1323 }
1324
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001325 inited_ = true;
1326 // Always start with a complete key frame.
1327 key_frame_required_ = true;
1328 return WEBRTC_VIDEO_CODEC_OK;
1329}
1330
1331int VP9DecoderImpl::Decode(const EncodedImage& input_image,
1332 bool missing_frames,
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001333 const CodecSpecificInfo* codec_specific_info,
1334 int64_t /*render_time_ms*/) {
1335 if (!inited_) {
1336 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
1337 }
sprang3958ed82017-08-17 08:12:10 -07001338 if (decode_complete_callback_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001339 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
1340 }
1341 // Always start with a complete key frame.
1342 if (key_frame_required_) {
Peter Boström49e196a2015-10-23 15:58:18 +02001343 if (input_image._frameType != kVideoFrameKey)
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001344 return WEBRTC_VIDEO_CODEC_ERROR;
1345 // We have a key frame - is it complete?
1346 if (input_image._completeFrame) {
1347 key_frame_required_ = false;
1348 } else {
1349 return WEBRTC_VIDEO_CODEC_ERROR;
1350 }
1351 }
sprang3958ed82017-08-17 08:12:10 -07001352 vpx_codec_iter_t iter = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001353 vpx_image_t* img;
1354 uint8_t* buffer = input_image._buffer;
1355 if (input_image._length == 0) {
sprang3958ed82017-08-17 08:12:10 -07001356 buffer = nullptr; // Triggers full frame concealment.
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001357 }
Henrik Boström9695d852015-05-06 10:42:15 +02001358 // During decode libvpx may get and release buffers from |frame_buffer_pool_|.
1359 // In practice libvpx keeps a few (~3-4) buffers alive at a time.
philipelcce46fc2015-12-21 03:04:49 -08001360 if (vpx_codec_decode(decoder_, buffer,
1361 static_cast<unsigned int>(input_image._length), 0,
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001362 VPX_DL_REALTIME)) {
1363 return WEBRTC_VIDEO_CODEC_ERROR;
1364 }
Henrik Boström9695d852015-05-06 10:42:15 +02001365 // |img->fb_priv| contains the image data, a reference counted Vp9FrameBuffer.
1366 // It may be released by libvpx during future vpx_codec_decode or
1367 // vpx_codec_destroy calls.
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001368 img = vpx_codec_get_frame(decoder_, &iter);
sakal7adadb12017-02-23 02:54:57 -08001369 int qp;
1370 vpx_codec_err_t vpx_ret =
1371 vpx_codec_control(decoder_, VPXD_GET_LAST_QUANTIZER, &qp);
1372 RTC_DCHECK_EQ(vpx_ret, VPX_CODEC_OK);
Johannes Kron9973fa82018-11-07 14:39:26 +01001373 int ret = ReturnFrame(img, input_image.Timestamp(), input_image.ntp_time_ms_,
Johannes Kron4749e4e2018-11-21 10:18:18 +01001374 qp, input_image.ColorSpace());
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001375 if (ret != 0) {
1376 return ret;
1377 }
1378 return WEBRTC_VIDEO_CODEC_OK;
1379}
1380
asapersson1490f7a2016-09-23 02:09:46 -07001381int VP9DecoderImpl::ReturnFrame(const vpx_image_t* img,
1382 uint32_t timestamp,
sakal7adadb12017-02-23 02:54:57 -08001383 int64_t ntp_time_ms,
Johannes Kron9973fa82018-11-07 14:39:26 +01001384 int qp,
Johannes Kron4749e4e2018-11-21 10:18:18 +01001385 const ColorSpace* explicit_color_space) {
sprang3958ed82017-08-17 08:12:10 -07001386 if (img == nullptr) {
1387 // Decoder OK and nullptr image => No show frame.
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001388 return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
1389 }
Henrik Boström9695d852015-05-06 10:42:15 +02001390
1391 // This buffer contains all of |img|'s image data, a reference counted
perkj14f41442015-11-30 22:15:45 -08001392 // Vp9FrameBuffer. (libvpx is done with the buffers after a few
Henrik Boström9695d852015-05-06 10:42:15 +02001393 // vpx_codec_decode calls or vpx_codec_destroy).
1394 Vp9FrameBufferPool::Vp9FrameBuffer* img_buffer =
1395 static_cast<Vp9FrameBufferPool::Vp9FrameBuffer*>(img->fb_priv);
Emircan Uysaler0823eec2018-07-13 17:10:00 -07001396
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07001397 // The buffer can be used directly by the VideoFrame (without copy) by
Emircan Uysaler0823eec2018-07-13 17:10:00 -07001398 // using a Wrapped*Buffer.
1399 rtc::scoped_refptr<VideoFrameBuffer> img_wrapped_buffer;
1400 switch (img->bit_depth) {
1401 case 8:
1402 img_wrapped_buffer = WrapI420Buffer(
philipelcce46fc2015-12-21 03:04:49 -08001403 img->d_w, img->d_h, img->planes[VPX_PLANE_Y],
1404 img->stride[VPX_PLANE_Y], img->planes[VPX_PLANE_U],
1405 img->stride[VPX_PLANE_U], img->planes[VPX_PLANE_V],
1406 img->stride[VPX_PLANE_V],
Henrik Boström9695d852015-05-06 10:42:15 +02001407 // WrappedI420Buffer's mechanism for allowing the release of its frame
1408 // buffer is through a callback function. This is where we should
1409 // release |img_buffer|.
Emircan Uysaler0823eec2018-07-13 17:10:00 -07001410 rtc::KeepRefUntilDone(img_buffer));
1411 break;
1412 case 10:
1413 img_wrapped_buffer = WrapI010Buffer(
1414 img->d_w, img->d_h,
1415 reinterpret_cast<const uint16_t*>(img->planes[VPX_PLANE_Y]),
1416 img->stride[VPX_PLANE_Y] / 2,
1417 reinterpret_cast<const uint16_t*>(img->planes[VPX_PLANE_U]),
1418 img->stride[VPX_PLANE_U] / 2,
1419 reinterpret_cast<const uint16_t*>(img->planes[VPX_PLANE_V]),
1420 img->stride[VPX_PLANE_V] / 2, rtc::KeepRefUntilDone(img_buffer));
1421 break;
1422 default:
1423 RTC_NOTREACHED();
1424 return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
1425 }
nisseca6d5d12016-06-17 05:03:04 -07001426
Johannes Kron4749e4e2018-11-21 10:18:18 +01001427 auto builder = VideoFrame::Builder()
1428 .set_video_frame_buffer(img_wrapped_buffer)
1429 .set_timestamp_ms(0)
1430 .set_timestamp_rtp(timestamp)
1431 .set_ntp_time_ms(ntp_time_ms)
1432 .set_rotation(webrtc::kVideoRotation_0);
1433 if (explicit_color_space) {
1434 builder.set_color_space(explicit_color_space);
1435 } else {
1436 builder.set_color_space(
1437 ExtractVP9ColorSpace(img->cs, img->range, img->bit_depth));
1438 }
1439
1440 VideoFrame decoded_image = builder.build();
1441
Danil Chapovalov0040b662018-06-18 10:48:16 +02001442 decode_complete_callback_->Decoded(decoded_image, absl::nullopt, qp);
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001443 return WEBRTC_VIDEO_CODEC_OK;
1444}
1445
1446int VP9DecoderImpl::RegisterDecodeCompleteCallback(
1447 DecodedImageCallback* callback) {
1448 decode_complete_callback_ = callback;
1449 return WEBRTC_VIDEO_CODEC_OK;
1450}
1451
1452int VP9DecoderImpl::Release() {
Sergey Silkin3e871ea2018-03-02 13:11:04 +01001453 int ret_val = WEBRTC_VIDEO_CODEC_OK;
1454
sprang3958ed82017-08-17 08:12:10 -07001455 if (decoder_ != nullptr) {
Sergey Silkin90399692018-03-02 14:44:10 +01001456 if (inited_) {
1457 // When a codec is destroyed libvpx will release any buffers of
1458 // |frame_buffer_pool_| it is currently using.
1459 if (vpx_codec_destroy(decoder_)) {
1460 ret_val = WEBRTC_VIDEO_CODEC_MEMORY;
1461 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001462 }
1463 delete decoder_;
sprang3958ed82017-08-17 08:12:10 -07001464 decoder_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001465 }
Henrik Boström9695d852015-05-06 10:42:15 +02001466 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers
1467 // still referenced externally are deleted once fully released, not returning
1468 // to the pool.
1469 frame_buffer_pool_.ClearPool();
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001470 inited_ = false;
Sergey Silkin3e871ea2018-03-02 13:11:04 +01001471 return ret_val;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001472}
Peter Boströmb7d9a972015-12-18 16:01:11 +01001473
1474const char* VP9DecoderImpl::ImplementationName() const {
1475 return "libvpx";
1476}
1477
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001478} // namespace webrtc
Mirko Bonadei95adedb2018-11-19 09:52:37 +01001479
1480#endif // RTC_ENABLE_VP9