blob: f0747e56b893f6a63410c77ef766b9ce15e333ea [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 Bonadei92ea95e2017-09-15 06:47:31 +020012#include "modules/video_coding/codecs/vp9/vp9_impl.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000013
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +020014#include <algorithm>
Sergey Silkind45b3452018-05-31 16:00:24 +020015#include <limits>
marpan@webrtc.org5b883172014-11-01 06:10:48 +000016#include <vector>
17
marpan@webrtc.org5b883172014-11-01 06:10:48 +000018#include "vpx/vp8cx.h"
19#include "vpx/vp8dx.h"
Yves Gerey665174f2018-06-19 15:03:05 +020020#include "vpx/vpx_decoder.h"
21#include "vpx/vpx_encoder.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000022
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"
28#include "rtc_base/keep_ref_until_done.h"
29#include "rtc_base/logging.h"
Magnus Jedvert46a27652017-11-13 14:10:02 +010030#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "rtc_base/timeutils.h"
32#include "rtc_base/trace_event.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000033
34namespace webrtc {
35
Sergey Silkind902d582018-05-18 17:31:19 +020036namespace {
37const float kMaxScreenSharingFramerateFps = 5.0f;
38}
39
Marco6e89b252015-07-07 14:40:38 -070040// Only positive speeds, range for real-time coding currently is: 5 - 8.
41// Lower means slower/better quality, higher means fastest/lower quality.
42int GetCpuSpeed(int width, int height) {
Alex Glaznevfecb7c32016-03-31 14:23:27 -070043#if defined(WEBRTC_ARCH_ARM) || defined(WEBRTC_ARCH_ARM64) || defined(ANDROID)
Marco002f0d02015-12-17 09:49:31 -080044 return 8;
45#else
Marco6e89b252015-07-07 14:40:38 -070046 // For smaller resolutions, use lower speed setting (get some coding gain at
47 // the cost of increased encoding complexity).
48 if (width * height <= 352 * 288)
49 return 5;
50 else
51 return 7;
Marco002f0d02015-12-17 09:49:31 -080052#endif
Marco6e89b252015-07-07 14:40:38 -070053}
54
Peter Boström12996152016-05-14 02:03:18 +020055bool VP9Encoder::IsSupported() {
56 return true;
57}
58
Magnus Jedvert46a27652017-11-13 14:10:02 +010059std::unique_ptr<VP9Encoder> VP9Encoder::Create() {
60 return rtc::MakeUnique<VP9EncoderImpl>();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000061}
62
asaperssona9455ab2015-07-31 06:10:09 -070063void VP9EncoderImpl::EncoderOutputCodedPacketCallback(vpx_codec_cx_pkt* pkt,
64 void* user_data) {
philipelcce46fc2015-12-21 03:04:49 -080065 VP9EncoderImpl* enc = static_cast<VP9EncoderImpl*>(user_data);
asaperssona9455ab2015-07-31 06:10:09 -070066 enc->GetEncodedLayerFrame(pkt);
67}
68
marpan@webrtc.org5b883172014-11-01 06:10:48 +000069VP9EncoderImpl::VP9EncoderImpl()
70 : encoded_image_(),
sprang3958ed82017-08-17 08:12:10 -070071 encoded_complete_callback_(nullptr),
marpan@webrtc.org5b883172014-11-01 06:10:48 +000072 inited_(false),
73 timestamp_(0),
marpan@webrtc.org5b883172014-11-01 06:10:48 +000074 cpu_speed_(3),
75 rc_max_intra_target_(0),
sprang3958ed82017-08-17 08:12:10 -070076 encoder_(nullptr),
77 config_(nullptr),
78 raw_(nullptr),
79 input_image_(nullptr),
Sergey Silkinbd0954e2018-05-03 14:14:09 +020080 force_key_frame_(true),
Sergey Silkin6a8f30e2018-04-26 11:03:49 +020081 pics_since_key_(0),
asaperssona9455ab2015-07-31 06:10:09 -070082 num_temporal_layers_(0),
philipelcfc319b2015-11-10 07:17:23 -080083 num_spatial_layers_(0),
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +020084 is_svc_(false),
Sergey Silkin6a8f30e2018-04-26 11:03:49 +020085 inter_layer_pred_(InterLayerPredMode::kOn),
Sergey Silkind902d582018-05-18 17:31:19 +020086 output_framerate_(1000.0, 1000.0),
87 last_encoded_frame_rtp_timestamp_(0),
Sergey Silkinbe71a1e2018-05-17 16:46:43 +020088 is_flexible_mode_(false) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +000089 memset(&codec_, 0, sizeof(codec_));
johannkoenig8225c402017-01-26 13:23:44 -080090 memset(&svc_params_, 0, sizeof(vpx_svc_extra_cfg_t));
marpan@webrtc.org5b883172014-11-01 06:10:48 +000091}
92
93VP9EncoderImpl::~VP9EncoderImpl() {
94 Release();
95}
96
97int VP9EncoderImpl::Release() {
Sergey Silkin3e871ea2018-03-02 13:11:04 +010098 int ret_val = WEBRTC_VIDEO_CODEC_OK;
99
sprang3958ed82017-08-17 08:12:10 -0700100 if (encoded_image_._buffer != nullptr) {
philipelcce46fc2015-12-21 03:04:49 -0800101 delete[] encoded_image_._buffer;
sprang3958ed82017-08-17 08:12:10 -0700102 encoded_image_._buffer = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000103 }
sprang3958ed82017-08-17 08:12:10 -0700104 if (encoder_ != nullptr) {
Sergey Silkin90399692018-03-02 14:44:10 +0100105 if (inited_) {
106 if (vpx_codec_destroy(encoder_)) {
107 ret_val = WEBRTC_VIDEO_CODEC_MEMORY;
108 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000109 }
110 delete encoder_;
sprang3958ed82017-08-17 08:12:10 -0700111 encoder_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000112 }
sprang3958ed82017-08-17 08:12:10 -0700113 if (config_ != nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000114 delete config_;
sprang3958ed82017-08-17 08:12:10 -0700115 config_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000116 }
sprang3958ed82017-08-17 08:12:10 -0700117 if (raw_ != nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000118 vpx_img_free(raw_);
sprang3958ed82017-08-17 08:12:10 -0700119 raw_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000120 }
121 inited_ = false;
Sergey Silkin3e871ea2018-03-02 13:11:04 +0100122 return ret_val;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000123}
124
sprangce4aef12015-11-02 07:23:20 -0800125bool VP9EncoderImpl::ExplicitlyConfiguredSpatialLayers() const {
126 // We check target_bitrate_bps of the 0th layer to see if the spatial layers
127 // (i.e. bitrates) were explicitly configured.
Sergey Silkin13e74342018-03-02 12:28:00 +0100128 return num_spatial_layers_ > 1 && codec_.spatialLayers[0].targetBitrate > 0;
sprangce4aef12015-11-02 07:23:20 -0800129}
130
Erik Språng566124a2018-04-23 12:32:22 +0200131bool VP9EncoderImpl::SetSvcRates(
132 const VideoBitrateAllocation& bitrate_allocation) {
asaperssona9455ab2015-07-31 06:10:09 -0700133 uint8_t i = 0;
134
Sergey Silkin86684962018-03-28 19:32:37 +0200135 config_->rc_target_bitrate = bitrate_allocation.get_sum_kbps();
Sergey Silkin86684962018-03-28 19:32:37 +0200136
“Michael23c5a992018-06-21 11:07:21 -0500137 num_active_spatial_layers_ = 0;
138 for (i = 0; i < num_spatial_layers_; ++i)
139 num_active_spatial_layers_ += bitrate_allocation.IsSpatialLayerUsed(i);
140 RTC_DCHECK_GT(num_active_spatial_layers_, 0);
141 RTC_DCHECK_LE(num_active_spatial_layers_, num_spatial_layers_);
142
sprangce4aef12015-11-02 07:23:20 -0800143 if (ExplicitlyConfiguredSpatialLayers()) {
Sergey Silkin86684962018-03-28 19:32:37 +0200144 for (size_t sl_idx = 0; sl_idx < num_spatial_layers_; ++sl_idx) {
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200145 const bool was_layer_enabled = (config_->ss_target_bitrate[sl_idx] > 0);
Sergey Silkin86684962018-03-28 19:32:37 +0200146 config_->ss_target_bitrate[sl_idx] =
147 bitrate_allocation.GetSpatialLayerSum(sl_idx) / 1000;
148
149 for (size_t tl_idx = 0; tl_idx < num_temporal_layers_; ++tl_idx) {
150 config_->layer_target_bitrate[sl_idx * num_temporal_layers_ + tl_idx] =
151 bitrate_allocation.GetTemporalLayerSum(sl_idx, tl_idx) / 1000;
152 }
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200153
154 const bool is_layer_enabled = (config_->ss_target_bitrate[sl_idx] > 0);
155 if (is_layer_enabled && !was_layer_enabled) {
156 if (inter_layer_pred_ == InterLayerPredMode::kOff ||
157 inter_layer_pred_ == InterLayerPredMode::kOnKeyPic) {
158 // TODO(wemb:1526): remove key frame request when issue is fixed.
159 force_key_frame_ = true;
160 }
161 }
sprangce4aef12015-11-02 07:23:20 -0800162 }
163 } else {
164 float rate_ratio[VPX_MAX_LAYERS] = {0};
165 float total = 0;
“Michael23c5a992018-06-21 11:07:21 -0500166 for (i = 0; i < num_active_spatial_layers_; ++i) {
johannkoenig8225c402017-01-26 13:23:44 -0800167 if (svc_params_.scaling_factor_num[i] <= 0 ||
168 svc_params_.scaling_factor_den[i] <= 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100169 RTC_LOG(LS_ERROR) << "Scaling factors not specified!";
sprangce4aef12015-11-02 07:23:20 -0800170 return false;
171 }
Yves Gerey665174f2018-06-19 15:03:05 +0200172 rate_ratio[i] = static_cast<float>(svc_params_.scaling_factor_num[i]) /
173 svc_params_.scaling_factor_den[i];
sprangce4aef12015-11-02 07:23:20 -0800174 total += rate_ratio[i];
175 }
176
“Michael23c5a992018-06-21 11:07:21 -0500177 for (i = 0; i < num_active_spatial_layers_; ++i) {
Rasmus Brandt58cd3852018-06-26 13:41:16 +0200178 RTC_CHECK_GT(total, 0);
sprangce4aef12015-11-02 07:23:20 -0800179 config_->ss_target_bitrate[i] = static_cast<unsigned int>(
180 config_->rc_target_bitrate * rate_ratio[i] / total);
181 if (num_temporal_layers_ == 1) {
182 config_->layer_target_bitrate[i] = config_->ss_target_bitrate[i];
183 } else if (num_temporal_layers_ == 2) {
184 config_->layer_target_bitrate[i * num_temporal_layers_] =
185 config_->ss_target_bitrate[i] * 2 / 3;
186 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] =
187 config_->ss_target_bitrate[i];
188 } else if (num_temporal_layers_ == 3) {
189 config_->layer_target_bitrate[i * num_temporal_layers_] =
190 config_->ss_target_bitrate[i] / 2;
191 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] =
192 config_->layer_target_bitrate[i * num_temporal_layers_] +
193 (config_->ss_target_bitrate[i] / 4);
194 config_->layer_target_bitrate[i * num_temporal_layers_ + 2] =
195 config_->ss_target_bitrate[i];
196 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100197 RTC_LOG(LS_ERROR) << "Unsupported number of temporal layers: "
198 << num_temporal_layers_;
sprangce4aef12015-11-02 07:23:20 -0800199 return false;
200 }
asaperssona9455ab2015-07-31 06:10:09 -0700201 }
202 }
203
204 // For now, temporal layers only supported when having one spatial layer.
205 if (num_spatial_layers_ == 1) {
206 for (i = 0; i < num_temporal_layers_; ++i) {
207 config_->ts_target_bitrate[i] = config_->layer_target_bitrate[i];
208 }
209 }
210
211 return true;
212}
213
Erik Språng08127a92016-11-16 16:41:30 +0100214int VP9EncoderImpl::SetRateAllocation(
Erik Språng566124a2018-04-23 12:32:22 +0200215 const VideoBitrateAllocation& bitrate_allocation,
Erik Språng08127a92016-11-16 16:41:30 +0100216 uint32_t frame_rate) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000217 if (!inited_) {
218 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
219 }
220 if (encoder_->err) {
221 return WEBRTC_VIDEO_CODEC_ERROR;
222 }
Erik Språng08127a92016-11-16 16:41:30 +0100223 if (frame_rate < 1) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000224 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
225 }
226 // Update bit rate
Erik Språng08127a92016-11-16 16:41:30 +0100227 if (codec_.maxBitrate > 0 &&
228 bitrate_allocation.get_sum_kbps() > codec_.maxBitrate) {
229 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000230 }
Erik Språng08127a92016-11-16 16:41:30 +0100231
Erik Språng08127a92016-11-16 16:41:30 +0100232 codec_.maxFramerate = frame_rate;
asaperssona9455ab2015-07-31 06:10:09 -0700233
Sergey Silkin86684962018-03-28 19:32:37 +0200234 if (!SetSvcRates(bitrate_allocation)) {
asaperssona9455ab2015-07-31 06:10:09 -0700235 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
236 }
237
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000238 // Update encoder context
239 if (vpx_codec_enc_config_set(encoder_, config_)) {
240 return WEBRTC_VIDEO_CODEC_ERROR;
241 }
242 return WEBRTC_VIDEO_CODEC_OK;
243}
244
245int VP9EncoderImpl::InitEncode(const VideoCodec* inst,
246 int number_of_cores,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000247 size_t /*max_payload_size*/) {
sprang3958ed82017-08-17 08:12:10 -0700248 if (inst == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000249 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
250 }
251 if (inst->maxFramerate < 1) {
252 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
253 }
254 // Allow zero to represent an unspecified maxBitRate
255 if (inst->maxBitrate > 0 && inst->startBitrate > inst->maxBitrate) {
256 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
257 }
258 if (inst->width < 1 || inst->height < 1) {
259 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
260 }
261 if (number_of_cores < 1) {
262 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
263 }
hta257dc392016-10-25 09:05:06 -0700264 if (inst->VP9().numberOfTemporalLayers > 3) {
asaperssona9455ab2015-07-31 06:10:09 -0700265 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
266 }
ilnik2a8c2f52017-02-15 02:23:28 -0800267 // libvpx probably does not support more than 3 spatial layers.
268 if (inst->VP9().numberOfSpatialLayers > 3) {
asaperssona9455ab2015-07-31 06:10:09 -0700269 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
270 }
philipelcfc319b2015-11-10 07:17:23 -0800271
asapersson86956de2016-01-26 01:05:20 -0800272 int ret_val = Release();
273 if (ret_val < 0) {
274 return ret_val;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000275 }
sprang3958ed82017-08-17 08:12:10 -0700276 if (encoder_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000277 encoder_ = new vpx_codec_ctx_t;
278 }
sprang3958ed82017-08-17 08:12:10 -0700279 if (config_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000280 config_ = new vpx_codec_enc_cfg_t;
281 }
282 timestamp_ = 0;
283 if (&codec_ != inst) {
284 codec_ = *inst;
285 }
asaperssona9455ab2015-07-31 06:10:09 -0700286
hta257dc392016-10-25 09:05:06 -0700287 num_spatial_layers_ = inst->VP9().numberOfSpatialLayers;
Niels Möller65fb4042018-04-25 14:46:06 +0200288 RTC_DCHECK_GT(num_spatial_layers_, 0);
hta257dc392016-10-25 09:05:06 -0700289 num_temporal_layers_ = inst->VP9().numberOfTemporalLayers;
asaperssona9455ab2015-07-31 06:10:09 -0700290 if (num_temporal_layers_ == 0)
291 num_temporal_layers_ = 1;
292
Sergey Silkind902d582018-05-18 17:31:19 +0200293 // Init framerate controller.
294 output_framerate_.Reset();
Niels Möllere3cf3d02018-06-13 11:52:16 +0200295 if (codec_.mode == VideoCodecMode::kScreensharing) {
Sergey Silkind902d582018-05-18 17:31:19 +0200296 target_framerate_fps_ = kMaxScreenSharingFramerateFps;
297 } else {
298 target_framerate_fps_.reset();
299 }
300
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200301 is_svc_ = (num_spatial_layers_ > 1 || num_temporal_layers_ > 1);
302 // Flexible mode requires SVC to be enabled since libvpx API only allows
303 // to get reference list in SVC mode.
304 RTC_DCHECK(!inst->VP9().flexibleMode || is_svc_);
305
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000306 // Allocate memory for encoded image
sprang3958ed82017-08-17 08:12:10 -0700307 if (encoded_image_._buffer != nullptr) {
philipelcce46fc2015-12-21 03:04:49 -0800308 delete[] encoded_image_._buffer;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000309 }
nisseeb44b392017-04-28 07:18:05 -0700310 encoded_image_._size =
311 CalcBufferSize(VideoType::kI420, codec_.width, codec_.height);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000312 encoded_image_._buffer = new uint8_t[encoded_image_._size];
313 encoded_image_._completeFrame = true;
sprang3958ed82017-08-17 08:12:10 -0700314 // Creating a wrapper to the image - setting image data to nullptr. Actual
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000315 // pointer will be set in encode. Setting align to 1, as it is meaningless
316 // (actual memory is not allocated).
sprang3958ed82017-08-17 08:12:10 -0700317 raw_ = vpx_img_wrap(nullptr, VPX_IMG_FMT_I420, codec_.width, codec_.height, 1,
318 nullptr);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000319 // Populate encoder configuration with default values.
320 if (vpx_codec_enc_config_default(vpx_codec_vp9_cx(), config_, 0)) {
321 return WEBRTC_VIDEO_CODEC_ERROR;
322 }
323 config_->g_w = codec_.width;
324 config_->g_h = codec_.height;
325 config_->rc_target_bitrate = inst->startBitrate; // in kbit/s
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200326 config_->g_error_resilient = is_svc_ ? VPX_ERROR_RESILIENT_DEFAULT : 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000327 // Setting the time base of the codec.
328 config_->g_timebase.num = 1;
329 config_->g_timebase.den = 90000;
330 config_->g_lag_in_frames = 0; // 0- no frame lagging
331 config_->g_threads = 1;
332 // Rate control settings.
hta257dc392016-10-25 09:05:06 -0700333 config_->rc_dropframe_thresh = inst->VP9().frameDroppingOn ? 30 : 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000334 config_->rc_end_usage = VPX_CBR;
335 config_->g_pass = VPX_RC_ONE_PASS;
336 config_->rc_min_quantizer = 2;
marpan@webrtc.orgdc8a9da2015-01-27 23:08:24 +0000337 config_->rc_max_quantizer = 52;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000338 config_->rc_undershoot_pct = 50;
339 config_->rc_overshoot_pct = 50;
340 config_->rc_buf_initial_sz = 500;
341 config_->rc_buf_optimal_sz = 600;
342 config_->rc_buf_sz = 1000;
343 // Set the maximum target size of any key-frame.
344 rc_max_intra_target_ = MaxIntraTarget(config_->rc_buf_optimal_sz);
hta257dc392016-10-25 09:05:06 -0700345 if (inst->VP9().keyFrameInterval > 0) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000346 config_->kf_mode = VPX_KF_AUTO;
hta257dc392016-10-25 09:05:06 -0700347 config_->kf_max_dist = inst->VP9().keyFrameInterval;
Åsa Perssonff24c042015-12-04 10:58:08 +0100348 // Needs to be set (in svc mode) to get correct periodic key frame interval
349 // (will have no effect in non-svc).
350 config_->kf_min_dist = config_->kf_max_dist;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000351 } else {
352 config_->kf_mode = VPX_KF_DISABLED;
353 }
hta257dc392016-10-25 09:05:06 -0700354 config_->rc_resize_allowed = inst->VP9().automaticResizeOn ? 1 : 0;
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000355 // Determine number of threads based on the image size and #cores.
philipelcce46fc2015-12-21 03:04:49 -0800356 config_->g_threads =
357 NumberOfThreads(config_->g_w, config_->g_h, number_of_cores);
asaperssona9455ab2015-07-31 06:10:09 -0700358
Marco6e89b252015-07-07 14:40:38 -0700359 cpu_speed_ = GetCpuSpeed(config_->g_w, config_->g_h);
asaperssona9455ab2015-07-31 06:10:09 -0700360
361 // TODO(asapersson): Check configuration of temporal switch up and increase
362 // pattern length.
hta257dc392016-10-25 09:05:06 -0700363 is_flexible_mode_ = inst->VP9().flexibleMode;
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200364
365 // TODO(ssilkin): Only non-flexible mode is supported for now.
366 RTC_DCHECK(!is_flexible_mode_);
367
368 if (num_temporal_layers_ == 1) {
asaperssona9455ab2015-07-31 06:10:09 -0700369 gof_.SetGofInfoVP9(kTemporalStructureMode1);
370 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING;
371 config_->ts_number_layers = 1;
372 config_->ts_rate_decimator[0] = 1;
373 config_->ts_periodicity = 1;
374 config_->ts_layer_id[0] = 0;
375 } else if (num_temporal_layers_ == 2) {
376 gof_.SetGofInfoVP9(kTemporalStructureMode2);
377 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0101;
378 config_->ts_number_layers = 2;
379 config_->ts_rate_decimator[0] = 2;
380 config_->ts_rate_decimator[1] = 1;
381 config_->ts_periodicity = 2;
382 config_->ts_layer_id[0] = 0;
383 config_->ts_layer_id[1] = 1;
384 } else if (num_temporal_layers_ == 3) {
385 gof_.SetGofInfoVP9(kTemporalStructureMode3);
386 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0212;
387 config_->ts_number_layers = 3;
388 config_->ts_rate_decimator[0] = 4;
389 config_->ts_rate_decimator[1] = 2;
390 config_->ts_rate_decimator[2] = 1;
391 config_->ts_periodicity = 4;
392 config_->ts_layer_id[0] = 0;
393 config_->ts_layer_id[1] = 2;
394 config_->ts_layer_id[2] = 1;
395 config_->ts_layer_id[3] = 2;
396 } else {
397 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
398 }
399
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200400 inter_layer_pred_ = inst->VP9().interLayerPred;
401
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200402 ref_buf_.clear();
403
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000404 return InitAndSetControlSettings(inst);
405}
406
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000407int VP9EncoderImpl::NumberOfThreads(int width,
408 int height,
409 int number_of_cores) {
410 // Keep the number of encoder threads equal to the possible number of column
411 // tiles, which is (1, 2, 4, 8). See comments below for VP9E_SET_TILE_COLUMNS.
412 if (width * height >= 1280 * 720 && number_of_cores > 4) {
413 return 4;
jianj23173a32017-07-12 16:11:09 -0700414 } else if (width * height >= 640 * 360 && number_of_cores > 2) {
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000415 return 2;
416 } else {
Yves Gerey665174f2018-06-19 15:03:05 +0200417// Use 2 threads for low res on ARM.
Jerome Jiang831af372017-12-05 10:44:35 -0800418#if defined(WEBRTC_ARCH_ARM) || defined(WEBRTC_ARCH_ARM64) || \
419 defined(WEBRTC_ANDROID)
420 if (width * height >= 320 * 180 && number_of_cores > 2) {
421 return 2;
422 }
423#endif
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000424 // 1 thread less than VGA.
425 return 1;
426 }
427}
428
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000429int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) {
Åsa Perssonff24c042015-12-04 10:58:08 +0100430 // Set QP-min/max per spatial and temporal layer.
431 int tot_num_layers = num_spatial_layers_ * num_temporal_layers_;
432 for (int i = 0; i < tot_num_layers; ++i) {
johannkoenig8225c402017-01-26 13:23:44 -0800433 svc_params_.max_quantizers[i] = config_->rc_max_quantizer;
434 svc_params_.min_quantizers[i] = config_->rc_min_quantizer;
Åsa Perssonff24c042015-12-04 10:58:08 +0100435 }
asaperssona9455ab2015-07-31 06:10:09 -0700436 config_->ss_number_layers = num_spatial_layers_;
sprangce4aef12015-11-02 07:23:20 -0800437 if (ExplicitlyConfiguredSpatialLayers()) {
438 for (int i = 0; i < num_spatial_layers_; ++i) {
439 const auto& layer = codec_.spatialLayers[i];
Rasmus Brandt58cd3852018-06-26 13:41:16 +0200440 RTC_CHECK_GT(layer.width, 0);
Sergey Silkin13e74342018-03-02 12:28:00 +0100441 const int scale_factor = codec_.width / layer.width;
442 RTC_DCHECK_GT(scale_factor, 0);
443
444 // Ensure scaler factor is integer.
445 if (scale_factor * layer.width != codec_.width) {
446 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
447 }
448
449 // Ensure scale factor is the same in both dimensions.
450 if (scale_factor * layer.height != codec_.height) {
451 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
452 }
453
454 // Ensure scale factor is power of two.
455 const bool is_pow_of_two = (scale_factor & (scale_factor - 1)) == 0;
456 if (!is_pow_of_two) {
457 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
458 }
459
460 svc_params_.scaling_factor_num[i] = 1;
461 svc_params_.scaling_factor_den[i] = scale_factor;
sprangce4aef12015-11-02 07:23:20 -0800462 }
463 } else {
464 int scaling_factor_num = 256;
465 for (int i = num_spatial_layers_ - 1; i >= 0; --i) {
sprangce4aef12015-11-02 07:23:20 -0800466 // 1:2 scaling in each dimension.
johannkoenig8225c402017-01-26 13:23:44 -0800467 svc_params_.scaling_factor_num[i] = scaling_factor_num;
468 svc_params_.scaling_factor_den[i] = 256;
sprangce4aef12015-11-02 07:23:20 -0800469 }
asaperssona9455ab2015-07-31 06:10:09 -0700470 }
471
Sergey Silkin86684962018-03-28 19:32:37 +0200472 SvcRateAllocator init_allocator(codec_);
Erik Språng566124a2018-04-23 12:32:22 +0200473 VideoBitrateAllocation allocation = init_allocator.GetAllocation(
Sergey Silkin86684962018-03-28 19:32:37 +0200474 inst->startBitrate * 1000, inst->maxFramerate);
475 if (!SetSvcRates(allocation)) {
asaperssona9455ab2015-07-31 06:10:09 -0700476 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
477 }
478
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000479 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) {
480 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
481 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000482 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_);
483 vpx_codec_control(encoder_, VP8E_SET_MAX_INTRA_BITRATE_PCT,
484 rc_max_intra_target_);
485 vpx_codec_control(encoder_, VP9E_SET_AQ_MODE,
hta257dc392016-10-25 09:05:06 -0700486 inst->VP9().adaptiveQpMode ? 3 : 0);
asaperssona9455ab2015-07-31 06:10:09 -0700487
jianj822e5932017-07-12 16:09:58 -0700488 vpx_codec_control(encoder_, VP9E_SET_FRAME_PARALLEL_DECODING, 0);
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200489
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200490 if (is_svc_) {
491 vpx_codec_control(encoder_, VP9E_SET_SVC, 1);
492 vpx_codec_control(encoder_, VP9E_SET_SVC_PARAMETERS, &svc_params_);
asaperssona9455ab2015-07-31 06:10:09 -0700493 }
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200494
495 if (num_spatial_layers_ > 1) {
496 switch (inter_layer_pred_) {
497 case InterLayerPredMode::kOn:
498 vpx_codec_control(encoder_, VP9E_SET_SVC_INTER_LAYER_PRED, 0);
499 break;
500 case InterLayerPredMode::kOff:
501 vpx_codec_control(encoder_, VP9E_SET_SVC_INTER_LAYER_PRED, 1);
502 break;
503 case InterLayerPredMode::kOnKeyPic:
504 vpx_codec_control(encoder_, VP9E_SET_SVC_INTER_LAYER_PRED, 2);
505 break;
506 default:
507 RTC_NOTREACHED();
508 }
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200509
Sergey Silkinee203362018-05-30 11:34:08 +0200510 // Configure encoder to drop entire superframe whenever it needs to drop
511 // a layer. This mode is prefered over per-layer dropping which causes
512 // quality flickering and is not compatible with RTP non-flexible mode.
513 vpx_svc_frame_drop_t svc_drop_frame;
514 memset(&svc_drop_frame, 0, sizeof(svc_drop_frame));
515 svc_drop_frame.framedrop_mode = FULL_SUPERFRAME_DROP;
Sergey Silkind45b3452018-05-31 16:00:24 +0200516 svc_drop_frame.max_consec_drop = std::numeric_limits<int>::max();
Sergey Silkinee203362018-05-30 11:34:08 +0200517 for (size_t i = 0; i < num_spatial_layers_; ++i) {
518 svc_drop_frame.framedrop_thresh[i] = config_->rc_dropframe_thresh;
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200519 }
Sergey Silkinee203362018-05-30 11:34:08 +0200520 vpx_codec_control(encoder_, VP9E_SET_SVC_FRAME_DROP_LAYER, &svc_drop_frame);
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200521 }
522
asaperssona9455ab2015-07-31 06:10:09 -0700523 // Register callback for getting each spatial layer.
524 vpx_codec_priv_output_cx_pkt_cb_pair_t cbp = {
philipelcce46fc2015-12-21 03:04:49 -0800525 VP9EncoderImpl::EncoderOutputCodedPacketCallback,
526 reinterpret_cast<void*>(this)};
527 vpx_codec_control(encoder_, VP9E_REGISTER_CX_CALLBACK,
528 reinterpret_cast<void*>(&cbp));
asaperssona9455ab2015-07-31 06:10:09 -0700529
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000530 // Control function to set the number of column tiles in encoding a frame, in
531 // log2 unit: e.g., 0 = 1 tile column, 1 = 2 tile columns, 2 = 4 tile columns.
532 // The number tile columns will be capped by the encoder based on image size
533 // (minimum width of tile column is 256 pixels, maximum is 4096).
534 vpx_codec_control(encoder_, VP9E_SET_TILE_COLUMNS, (config_->g_threads >> 1));
jianjcb5d1152017-03-28 23:56:08 -0700535
536 // Turn on row-based multithreading.
537 vpx_codec_control(encoder_, VP9E_SET_ROW_MT, 1);
jianj6bf57e32017-06-05 13:43:49 -0700538
Alex Glaznevfecb7c32016-03-31 14:23:27 -0700539#if !defined(WEBRTC_ARCH_ARM) && !defined(WEBRTC_ARCH_ARM64) && \
Yves Gerey665174f2018-06-19 15:03:05 +0200540 !defined(ANDROID)
jianj6bf57e32017-06-05 13:43:49 -0700541 // Do not enable the denoiser on ARM since optimization is pending.
542 // Denoiser is on by default on other platforms.
marpan@webrtc.org16a87b92015-03-05 22:19:00 +0000543 vpx_codec_control(encoder_, VP9E_SET_NOISE_SENSITIVITY,
hta257dc392016-10-25 09:05:06 -0700544 inst->VP9().denoisingOn ? 1 : 0);
marpan@webrtc.org16a87b92015-03-05 22:19:00 +0000545#endif
jianj6bf57e32017-06-05 13:43:49 -0700546
Niels Möllere3cf3d02018-06-13 11:52:16 +0200547 if (codec_.mode == VideoCodecMode::kScreensharing) {
ivica242d6382015-09-04 06:13:23 -0700548 // Adjust internal parameters to screen content.
549 vpx_codec_control(encoder_, VP9E_SET_TUNE_CONTENT, 1);
ivica242d6382015-09-04 06:13:23 -0700550 }
Marco2520e722015-09-16 14:05:00 -0700551 // Enable encoder skip of static/low content blocks.
552 vpx_codec_control(encoder_, VP8E_SET_STATIC_THRESHOLD, 1);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000553 inited_ = true;
554 return WEBRTC_VIDEO_CODEC_OK;
555}
556
557uint32_t VP9EncoderImpl::MaxIntraTarget(uint32_t optimal_buffer_size) {
558 // Set max to the optimal buffer level (normalized by target BR),
559 // and scaled by a scale_par.
560 // Max target size = scale_par * optimal_buffer_size * targetBR[Kbps].
561 // This value is presented in percentage of perFrameBw:
562 // perFrameBw = targetBR[Kbps] * 1000 / framerate.
563 // The target in % is as follows:
564 float scale_par = 0.5;
565 uint32_t target_pct =
566 optimal_buffer_size * scale_par * codec_.maxFramerate / 10;
567 // Don't go below 3 times the per frame bandwidth.
568 const uint32_t min_intra_size = 300;
philipelcce46fc2015-12-21 03:04:49 -0800569 return (target_pct < min_intra_size) ? min_intra_size : target_pct;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000570}
571
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700572int VP9EncoderImpl::Encode(const VideoFrame& input_image,
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000573 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700574 const std::vector<FrameType>* frame_types) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000575 if (!inited_) {
576 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
577 }
sprang3958ed82017-08-17 08:12:10 -0700578 if (encoded_complete_callback_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000579 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
580 }
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200581
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000582 // We only support one stream at the moment.
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200583 if (frame_types && !frame_types->empty()) {
584 if ((*frame_types)[0] == kVideoFrameKey) {
585 force_key_frame_ = true;
586 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000587 }
Sergey Silkind902d582018-05-18 17:31:19 +0200588
Niels Möllere3cf3d02018-06-13 11:52:16 +0200589 if (VideoCodecMode::kScreensharing == codec_.mode && !force_key_frame_) {
Sergey Silkind902d582018-05-18 17:31:19 +0200590 if (DropFrame(input_image.timestamp())) {
591 return WEBRTC_VIDEO_CODEC_OK;
592 }
593 }
594
kwiberg352444f2016-11-28 15:58:53 -0800595 RTC_DCHECK_EQ(input_image.width(), raw_->d_w);
596 RTC_DCHECK_EQ(input_image.height(), raw_->d_h);
asaperssona9455ab2015-07-31 06:10:09 -0700597
598 // Set input image for use in the callback.
599 // This was necessary since you need some information from input_image.
600 // You can save only the necessary information (such as timestamp) instead of
601 // doing this.
602 input_image_ = &input_image;
603
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000604 rtc::scoped_refptr<I420BufferInterface> i420_buffer =
605 input_image.video_frame_buffer()->ToI420();
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000606 // Image in vpx_image_t format.
607 // Input image is const. VPX's raw image is not defined as const.
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000608 raw_->planes[VPX_PLANE_Y] = const_cast<uint8_t*>(i420_buffer->DataY());
609 raw_->planes[VPX_PLANE_U] = const_cast<uint8_t*>(i420_buffer->DataU());
610 raw_->planes[VPX_PLANE_V] = const_cast<uint8_t*>(i420_buffer->DataV());
611 raw_->stride[VPX_PLANE_Y] = i420_buffer->StrideY();
612 raw_->stride[VPX_PLANE_U] = i420_buffer->StrideU();
613 raw_->stride[VPX_PLANE_V] = i420_buffer->StrideV();
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000614
philipelcfc319b2015-11-10 07:17:23 -0800615 vpx_enc_frame_flags_t flags = 0;
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200616 if (force_key_frame_) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000617 flags = VPX_EFLAG_FORCE_KF;
618 }
philipelcfc319b2015-11-10 07:17:23 -0800619
sprang3958ed82017-08-17 08:12:10 -0700620 RTC_CHECK_GT(codec_.maxFramerate, 0);
Sergey Silkind902d582018-05-18 17:31:19 +0200621 uint32_t duration =
622 90000 / target_framerate_fps_.value_or(codec_.maxFramerate);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000623 if (vpx_codec_encode(encoder_, raw_, timestamp_, duration, flags,
624 VPX_DL_REALTIME)) {
625 return WEBRTC_VIDEO_CODEC_ERROR;
626 }
627 timestamp_ += duration;
asaperssona9455ab2015-07-31 06:10:09 -0700628
Sergey Silkinbc0f0d32018-04-24 21:29:14 +0200629 const bool end_of_picture = true;
630 DeliverBufferedFrame(end_of_picture);
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200631
asaperssona9455ab2015-07-31 06:10:09 -0700632 return WEBRTC_VIDEO_CODEC_OK;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000633}
634
635void VP9EncoderImpl::PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
philipelcce46fc2015-12-21 03:04:49 -0800636 const vpx_codec_cx_pkt& pkt,
Sergey Silkin07f80cc2018-04-09 13:11:59 +0200637 uint32_t timestamp,
638 bool first_frame_in_picture) {
sprang3958ed82017-08-17 08:12:10 -0700639 RTC_CHECK(codec_specific != nullptr);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000640 codec_specific->codecType = kVideoCodecVP9;
perkj275afc52016-09-01 00:21:16 -0700641 codec_specific->codec_name = ImplementationName();
philipelcce46fc2015-12-21 03:04:49 -0800642 CodecSpecificInfoVP9* vp9_info = &(codec_specific->codecSpecific.VP9);
Sergey Silkin07f80cc2018-04-09 13:11:59 +0200643
644 vp9_info->first_frame_in_picture = first_frame_in_picture;
hta257dc392016-10-25 09:05:06 -0700645 vp9_info->flexible_mode = codec_.VP9()->flexibleMode;
646 vp9_info->ss_data_available =
647 ((pkt.data.frame.flags & VPX_FRAME_IS_KEY) && !codec_.VP9()->flexibleMode)
648 ? true
649 : false;
asaperssona9455ab2015-07-31 06:10:09 -0700650
651 vpx_svc_layer_id_t layer_id = {0};
652 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
653
sprang3958ed82017-08-17 08:12:10 -0700654 RTC_CHECK_GT(num_temporal_layers_, 0);
“Michael23c5a992018-06-21 11:07:21 -0500655 RTC_CHECK_GT(num_active_spatial_layers_, 0);
asaperssona9455ab2015-07-31 06:10:09 -0700656 if (num_temporal_layers_ == 1) {
sprang3958ed82017-08-17 08:12:10 -0700657 RTC_CHECK_EQ(layer_id.temporal_layer_id, 0);
asaperssona9455ab2015-07-31 06:10:09 -0700658 vp9_info->temporal_idx = kNoTemporalIdx;
659 } else {
660 vp9_info->temporal_idx = layer_id.temporal_layer_id;
661 }
“Michael23c5a992018-06-21 11:07:21 -0500662 if (num_active_spatial_layers_ == 1) {
sprang3958ed82017-08-17 08:12:10 -0700663 RTC_CHECK_EQ(layer_id.spatial_layer_id, 0);
asaperssona9455ab2015-07-31 06:10:09 -0700664 vp9_info->spatial_idx = kNoSpatialIdx;
665 } else {
666 vp9_info->spatial_idx = layer_id.spatial_layer_id;
667 }
668 if (layer_id.spatial_layer_id != 0) {
669 vp9_info->ss_data_available = false;
670 }
671
asaperssona9455ab2015-07-31 06:10:09 -0700672 // TODO(asapersson): this info has to be obtained from the encoder.
asaperssoncb50c962015-11-18 01:58:55 -0800673 vp9_info->temporal_up_switch = false;
asaperssona9455ab2015-07-31 06:10:09 -0700674
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200675 if (pkt.data.frame.flags & VPX_FRAME_IS_KEY) {
676 pics_since_key_ = 0;
677 } else if (first_frame_in_picture) {
678 ++pics_since_key_;
asaperssona9455ab2015-07-31 06:10:09 -0700679 }
680
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200681 const bool is_key_pic = (pics_since_key_ == 0);
682 const bool is_inter_layer_pred_allowed =
683 (inter_layer_pred_ == InterLayerPredMode::kOn ||
684 (inter_layer_pred_ == InterLayerPredMode::kOnKeyPic && is_key_pic));
685
686 // Always set inter_layer_predicted to true on high layer frame if inter-layer
687 // prediction (ILP) is allowed even if encoder didn't actually use it.
688 // Setting inter_layer_predicted to false would allow receiver to decode high
689 // layer frame without decoding low layer frame. If that would happen (e.g.
690 // if low layer frame is lost) then receiver won't be able to decode next high
691 // layer frame which uses ILP.
692 vp9_info->inter_layer_predicted =
693 first_frame_in_picture ? false : is_inter_layer_pred_allowed;
694
695 const bool is_last_layer =
“Michael23c5a992018-06-21 11:07:21 -0500696 (layer_id.spatial_layer_id + 1 == num_active_spatial_layers_);
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200697 vp9_info->non_ref_for_inter_layer_pred =
698 is_last_layer ? true : !is_inter_layer_pred_allowed;
asapersson00ac85e2015-11-11 05:30:48 -0800699
ivica7f6a6fc2015-09-08 02:40:29 -0700700 // Always populate this, so that the packetizer can properly set the marker
701 // bit.
“Michael23c5a992018-06-21 11:07:21 -0500702 vp9_info->num_spatial_layers = num_active_spatial_layers_;
philipelcfc319b2015-11-10 07:17:23 -0800703
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200704 RTC_DCHECK(!vp9_info->flexible_mode);
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200705
706 vp9_info->num_ref_pics = 0;
707 if (vp9_info->flexible_mode) {
708 vp9_info->gof_idx = kNoGofIdx;
709 FillReferenceIndices(pkt, pics_since_key_, vp9_info->inter_layer_predicted,
710 vp9_info);
711 } else {
712 vp9_info->gof_idx =
713 static_cast<uint8_t>(pics_since_key_ % gof_.num_frames_in_gof);
714 vp9_info->temporal_up_switch = gof_.temporal_up_switch[vp9_info->gof_idx];
715 vp9_info->num_ref_pics = gof_.num_ref_pics[vp9_info->gof_idx];
716 }
717
718 vp9_info->inter_pic_predicted = (!is_key_pic && vp9_info->num_ref_pics > 0);
719
asaperssona9455ab2015-07-31 06:10:09 -0700720 if (vp9_info->ss_data_available) {
asaperssona9455ab2015-07-31 06:10:09 -0700721 vp9_info->spatial_layer_resolution_present = true;
“Michael23c5a992018-06-21 11:07:21 -0500722 for (size_t i = 0; i < num_active_spatial_layers_; ++i) {
Yves Gerey665174f2018-06-19 15:03:05 +0200723 vp9_info->width[i] = codec_.width * svc_params_.scaling_factor_num[i] /
johannkoenig8225c402017-01-26 13:23:44 -0800724 svc_params_.scaling_factor_den[i];
Yves Gerey665174f2018-06-19 15:03:05 +0200725 vp9_info->height[i] = codec_.height * svc_params_.scaling_factor_num[i] /
johannkoenig8225c402017-01-26 13:23:44 -0800726 svc_params_.scaling_factor_den[i];
asaperssona9455ab2015-07-31 06:10:09 -0700727 }
728 if (!vp9_info->flexible_mode) {
729 vp9_info->gof.CopyGofInfoVP9(gof_);
730 }
731 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000732}
733
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200734void VP9EncoderImpl::FillReferenceIndices(const vpx_codec_cx_pkt& pkt,
735 const size_t pic_num,
736 const bool inter_layer_predicted,
737 CodecSpecificInfoVP9* vp9_info) {
738 vpx_svc_layer_id_t layer_id = {0};
739 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
740
741 vpx_svc_ref_frame_config_t enc_layer_conf = {{0}};
742 vpx_codec_control(encoder_, VP9E_GET_SVC_REF_FRAME_CONFIG, &enc_layer_conf);
743
744 std::vector<RefFrameBuffer> ref_buf_list;
745 if (enc_layer_conf.reference_last[layer_id.spatial_layer_id]) {
746 const size_t fb_idx = enc_layer_conf.lst_fb_idx[layer_id.spatial_layer_id];
747 RTC_DCHECK(ref_buf_.find(fb_idx) != ref_buf_.end());
748 ref_buf_list.push_back(ref_buf_.at(fb_idx));
749 }
750
751 if (enc_layer_conf.reference_alt_ref[layer_id.spatial_layer_id]) {
752 const size_t fb_idx = enc_layer_conf.alt_fb_idx[layer_id.spatial_layer_id];
753 RTC_DCHECK(ref_buf_.find(fb_idx) != ref_buf_.end());
754 ref_buf_list.push_back(ref_buf_.at(fb_idx));
755 }
756
757 if (enc_layer_conf.reference_golden[layer_id.spatial_layer_id]) {
758 const size_t fb_idx = enc_layer_conf.gld_fb_idx[layer_id.spatial_layer_id];
759 RTC_DCHECK(ref_buf_.find(fb_idx) != ref_buf_.end());
760 ref_buf_list.push_back(ref_buf_.at(fb_idx));
761 }
762
763 size_t max_ref_temporal_layer_id = 0;
764
765 vp9_info->num_ref_pics = 0;
766 for (const RefFrameBuffer& ref_buf : ref_buf_list) {
767 RTC_DCHECK_LE(ref_buf.pic_num, pic_num);
768 if (ref_buf.pic_num < pic_num) {
769 if (inter_layer_pred_ != InterLayerPredMode::kOn) {
770 // RTP spec limits temporal prediction to the same spatial layer.
771 // It is safe to ignore this requirement if inter-layer prediction is
772 // enabled for all frames when all base frames are relayed to receiver.
773 RTC_DCHECK_EQ(ref_buf.spatial_layer_id, layer_id.spatial_layer_id);
774 }
775 RTC_DCHECK_LE(ref_buf.temporal_layer_id, layer_id.temporal_layer_id);
776
777 const size_t p_diff = pic_num - ref_buf.pic_num;
778 RTC_DCHECK_LE(p_diff, 127UL);
779
780 vp9_info->p_diff[vp9_info->num_ref_pics] = static_cast<uint8_t>(p_diff);
781 ++vp9_info->num_ref_pics;
782
783 max_ref_temporal_layer_id =
784 std::max(max_ref_temporal_layer_id, ref_buf.temporal_layer_id);
785 } else {
786 RTC_DCHECK(inter_layer_predicted);
787 // RTP spec only allows to use previous spatial layer for inter-layer
788 // prediction.
789 RTC_DCHECK_EQ(ref_buf.spatial_layer_id + 1, layer_id.spatial_layer_id);
790 }
791 }
792
793 vp9_info->temporal_up_switch =
794 (max_ref_temporal_layer_id <
795 static_cast<size_t>(layer_id.temporal_layer_id));
796}
797
798void VP9EncoderImpl::UpdateReferenceBuffers(const vpx_codec_cx_pkt& pkt,
799 const size_t pic_num) {
800 vpx_svc_layer_id_t layer_id = {0};
801 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
802
803 vpx_svc_ref_frame_config_t enc_layer_conf = {{0}};
804 vpx_codec_control(encoder_, VP9E_GET_SVC_REF_FRAME_CONFIG, &enc_layer_conf);
805
806 const bool is_key_frame =
807 (pkt.data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
808
809 RefFrameBuffer frame_buf(pic_num, layer_id.spatial_layer_id,
810 layer_id.temporal_layer_id);
811
812 if (is_key_frame && layer_id.spatial_layer_id == 0) {
813 // Key frame updates all ref buffers.
814 for (size_t i = 0; i < kNumVp9Buffers; ++i) {
815 ref_buf_[i] = frame_buf;
816 }
817 } else {
818 if (enc_layer_conf.update_last[layer_id.spatial_layer_id]) {
819 ref_buf_[enc_layer_conf.lst_fb_idx[layer_id.spatial_layer_id]] =
820 frame_buf;
821 }
822
823 if (enc_layer_conf.update_alt_ref[layer_id.spatial_layer_id]) {
824 ref_buf_[enc_layer_conf.alt_fb_idx[layer_id.spatial_layer_id]] =
825 frame_buf;
826 }
827
828 if (enc_layer_conf.update_golden[layer_id.spatial_layer_id]) {
829 ref_buf_[enc_layer_conf.gld_fb_idx[layer_id.spatial_layer_id]] =
830 frame_buf;
831 }
832 }
833}
834
asaperssona9455ab2015-07-31 06:10:09 -0700835int VP9EncoderImpl::GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt) {
asapersson86956de2016-01-26 01:05:20 -0800836 RTC_DCHECK_EQ(pkt->kind, VPX_CODEC_CX_FRAME_PKT);
asaperssona9455ab2015-07-31 06:10:09 -0700837
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200838 if (pkt->data.frame.sz == 0) {
839 // Ignore dropped frame.
840 return WEBRTC_VIDEO_CODEC_OK;
841 }
842
Sergey Silkin07f80cc2018-04-09 13:11:59 +0200843 vpx_svc_layer_id_t layer_id = {0};
844 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
845
846 const bool first_frame_in_picture = encoded_image_._length == 0;
847 // Ensure we don't buffer layers of previous picture (superframe).
848 RTC_DCHECK(first_frame_in_picture || layer_id.spatial_layer_id > 0);
849
Sergey Silkinbc0f0d32018-04-24 21:29:14 +0200850 const bool end_of_picture = false;
851 DeliverBufferedFrame(end_of_picture);
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200852
asaperssond9f641e2016-01-21 01:11:35 -0800853 if (pkt->data.frame.sz > encoded_image_._size) {
854 delete[] encoded_image_._buffer;
855 encoded_image_._size = pkt->data.frame.sz;
856 encoded_image_._buffer = new uint8_t[encoded_image_._size];
857 }
asapersson86956de2016-01-26 01:05:20 -0800858 memcpy(encoded_image_._buffer, pkt->data.frame.buf, pkt->data.frame.sz);
859 encoded_image_._length = pkt->data.frame.sz;
asaperssond9f641e2016-01-21 01:11:35 -0800860
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200861 const bool is_key_frame =
862 (pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
863 // Ensure encoder issued key frame on request.
864 RTC_DCHECK(is_key_frame || !force_key_frame_);
865
asaperssona9455ab2015-07-31 06:10:09 -0700866 // Check if encoded frame is a key frame.
asapersson86956de2016-01-26 01:05:20 -0800867 encoded_image_._frameType = kVideoFrameDelta;
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200868 if (is_key_frame) {
Peter Boström49e196a2015-10-23 15:58:18 +0200869 encoded_image_._frameType = kVideoFrameKey;
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200870 force_key_frame_ = false;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000871 }
asapersson86956de2016-01-26 01:05:20 -0800872 RTC_DCHECK_LE(encoded_image_._length, encoded_image_._size);
873
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200874 memset(&codec_specific_, 0, sizeof(codec_specific_));
Sergey Silkin07f80cc2018-04-09 13:11:59 +0200875 PopulateCodecSpecific(&codec_specific_, *pkt, input_image_->timestamp(),
876 first_frame_in_picture);
asaperssona9455ab2015-07-31 06:10:09 -0700877
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200878 if (is_flexible_mode_) {
879 UpdateReferenceBuffers(*pkt, pics_since_key_);
880 }
881
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200882 TRACE_COUNTER1("webrtc", "EncodedFrameSize", encoded_image_._length);
883 encoded_image_._timeStamp = input_image_->timestamp();
884 encoded_image_.capture_time_ms_ = input_image_->render_time_ms();
885 encoded_image_.rotation_ = input_image_->rotation();
Niels Möllere3cf3d02018-06-13 11:52:16 +0200886 encoded_image_.content_type_ = (codec_.mode == VideoCodecMode::kScreensharing)
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200887 ? VideoContentType::SCREENSHARE
888 : VideoContentType::UNSPECIFIED;
889 encoded_image_._encodedHeight =
890 pkt->data.frame.height[layer_id.spatial_layer_id];
891 encoded_image_._encodedWidth =
892 pkt->data.frame.width[layer_id.spatial_layer_id];
Ilya Nikolaevskiyb6c462d2018-06-05 15:21:32 +0200893 encoded_image_.timing_.flags = VideoSendTiming::kInvalid;
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200894 int qp = -1;
895 vpx_codec_control(encoder_, VP8E_GET_LAST_QUANTIZER, &qp);
896 encoded_image_.qp_ = qp;
ilnik04f4d122017-06-19 07:18:55 -0700897
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000898 return WEBRTC_VIDEO_CODEC_OK;
899}
900
Sergey Silkinbc0f0d32018-04-24 21:29:14 +0200901void VP9EncoderImpl::DeliverBufferedFrame(bool end_of_picture) {
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200902 if (encoded_image_._length > 0) {
Sergey Silkinbc0f0d32018-04-24 21:29:14 +0200903 codec_specific_.codecSpecific.VP9.end_of_picture = end_of_picture;
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200904
905 // No data partitioning in VP9, so 1 partition only.
906 int part_idx = 0;
907 RTPFragmentationHeader frag_info;
908 frag_info.VerifyAndAllocateFragmentationHeader(1);
909 frag_info.fragmentationOffset[part_idx] = 0;
910 frag_info.fragmentationLength[part_idx] = encoded_image_._length;
911 frag_info.fragmentationPlType[part_idx] = 0;
912 frag_info.fragmentationTimeDiff[part_idx] = 0;
913
914 encoded_complete_callback_->OnEncodedImage(encoded_image_, &codec_specific_,
915 &frag_info);
916 encoded_image_._length = 0;
Sergey Silkind902d582018-05-18 17:31:19 +0200917
918 if (end_of_picture) {
919 const uint32_t timestamp_ms =
920 1000 * encoded_image_._timeStamp / kVideoPayloadTypeFrequency;
921 output_framerate_.Update(1, timestamp_ms);
922 last_encoded_frame_rtp_timestamp_ = encoded_image_._timeStamp;
923 }
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200924 }
925}
926
Sergey Silkind902d582018-05-18 17:31:19 +0200927bool VP9EncoderImpl::DropFrame(uint32_t rtp_timestamp) {
928 if (target_framerate_fps_) {
929 if (rtp_timestamp < last_encoded_frame_rtp_timestamp_) {
930 // Timestamp has wrapped around. Reset framerate statistic.
931 output_framerate_.Reset();
932 return false;
933 }
934
935 const uint32_t timestamp_ms =
936 1000 * rtp_timestamp / kVideoPayloadTypeFrequency;
937 const uint32_t framerate_fps =
938 output_framerate_.Rate(timestamp_ms).value_or(0);
939 if (framerate_fps > *target_framerate_fps_) {
940 return true;
941 }
942
943 // Primarily check if frame interval is too short using frame timestamps,
944 // as if they are correct they won't be affected by queuing in webrtc.
945 const uint32_t expected_frame_interval =
946 kVideoPayloadTypeFrequency / *target_framerate_fps_;
947
948 const uint32_t ts_diff = rtp_timestamp - last_encoded_frame_rtp_timestamp_;
949 if (ts_diff < 85 * expected_frame_interval / 100) {
950 return true;
951 }
952 }
953
954 return false;
955}
956
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000957int VP9EncoderImpl::SetChannelParameters(uint32_t packet_loss, int64_t rtt) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000958 return WEBRTC_VIDEO_CODEC_OK;
959}
960
961int VP9EncoderImpl::RegisterEncodeCompleteCallback(
962 EncodedImageCallback* callback) {
963 encoded_complete_callback_ = callback;
964 return WEBRTC_VIDEO_CODEC_OK;
965}
966
Peter Boströmb7d9a972015-12-18 16:01:11 +0100967const char* VP9EncoderImpl::ImplementationName() const {
968 return "libvpx";
969}
970
Peter Boström12996152016-05-14 02:03:18 +0200971bool VP9Decoder::IsSupported() {
972 return true;
973}
974
Magnus Jedvert46a27652017-11-13 14:10:02 +0100975std::unique_ptr<VP9Decoder> VP9Decoder::Create() {
976 return rtc::MakeUnique<VP9DecoderImpl>();
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000977}
978
979VP9DecoderImpl::VP9DecoderImpl()
sprang3958ed82017-08-17 08:12:10 -0700980 : decode_complete_callback_(nullptr),
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000981 inited_(false),
sprang3958ed82017-08-17 08:12:10 -0700982 decoder_(nullptr),
philipel9d7d75b2018-04-04 13:03:01 +0200983 key_frame_required_(true) {}
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000984
985VP9DecoderImpl::~VP9DecoderImpl() {
986 inited_ = true; // in order to do the actual release
987 Release();
Henrik Boström9695d852015-05-06 10:42:15 +0200988 int num_buffers_in_use = frame_buffer_pool_.GetNumBuffersInUse();
989 if (num_buffers_in_use > 0) {
990 // The frame buffers are reference counted and frames are exposed after
991 // decoding. There may be valid usage cases where previous frames are still
992 // referenced after ~VP9DecoderImpl that is not a leak.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100993 RTC_LOG(LS_INFO) << num_buffers_in_use << " Vp9FrameBuffers are still "
994 << "referenced during ~VP9DecoderImpl.";
Henrik Boström9695d852015-05-06 10:42:15 +0200995 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000996}
997
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000998int VP9DecoderImpl::InitDecode(const VideoCodec* inst, int number_of_cores) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000999 int ret_val = Release();
1000 if (ret_val < 0) {
1001 return ret_val;
1002 }
sprang3958ed82017-08-17 08:12:10 -07001003 if (decoder_ == nullptr) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +00001004 decoder_ = new vpx_codec_ctx_t;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001005 }
philipelcce46fc2015-12-21 03:04:49 -08001006 vpx_codec_dec_cfg_t cfg;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001007 // Setting number of threads to a constant value (1)
1008 cfg.threads = 1;
1009 cfg.h = cfg.w = 0; // set after decode
1010 vpx_codec_flags_t flags = 0;
1011 if (vpx_codec_dec_init(decoder_, vpx_codec_vp9_dx(), &cfg, flags)) {
1012 return WEBRTC_VIDEO_CODEC_MEMORY;
1013 }
Henrik Boström9695d852015-05-06 10:42:15 +02001014
1015 if (!frame_buffer_pool_.InitializeVpxUsePool(decoder_)) {
1016 return WEBRTC_VIDEO_CODEC_MEMORY;
1017 }
1018
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001019 inited_ = true;
1020 // Always start with a complete key frame.
1021 key_frame_required_ = true;
1022 return WEBRTC_VIDEO_CODEC_OK;
1023}
1024
1025int VP9DecoderImpl::Decode(const EncodedImage& input_image,
1026 bool missing_frames,
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001027 const CodecSpecificInfo* codec_specific_info,
1028 int64_t /*render_time_ms*/) {
1029 if (!inited_) {
1030 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
1031 }
sprang3958ed82017-08-17 08:12:10 -07001032 if (decode_complete_callback_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001033 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
1034 }
1035 // Always start with a complete key frame.
1036 if (key_frame_required_) {
Peter Boström49e196a2015-10-23 15:58:18 +02001037 if (input_image._frameType != kVideoFrameKey)
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001038 return WEBRTC_VIDEO_CODEC_ERROR;
1039 // We have a key frame - is it complete?
1040 if (input_image._completeFrame) {
1041 key_frame_required_ = false;
1042 } else {
1043 return WEBRTC_VIDEO_CODEC_ERROR;
1044 }
1045 }
sprang3958ed82017-08-17 08:12:10 -07001046 vpx_codec_iter_t iter = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001047 vpx_image_t* img;
1048 uint8_t* buffer = input_image._buffer;
1049 if (input_image._length == 0) {
sprang3958ed82017-08-17 08:12:10 -07001050 buffer = nullptr; // Triggers full frame concealment.
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001051 }
Henrik Boström9695d852015-05-06 10:42:15 +02001052 // During decode libvpx may get and release buffers from |frame_buffer_pool_|.
1053 // In practice libvpx keeps a few (~3-4) buffers alive at a time.
philipelcce46fc2015-12-21 03:04:49 -08001054 if (vpx_codec_decode(decoder_, buffer,
1055 static_cast<unsigned int>(input_image._length), 0,
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001056 VPX_DL_REALTIME)) {
1057 return WEBRTC_VIDEO_CODEC_ERROR;
1058 }
Henrik Boström9695d852015-05-06 10:42:15 +02001059 // |img->fb_priv| contains the image data, a reference counted Vp9FrameBuffer.
1060 // It may be released by libvpx during future vpx_codec_decode or
1061 // vpx_codec_destroy calls.
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001062 img = vpx_codec_get_frame(decoder_, &iter);
sakal7adadb12017-02-23 02:54:57 -08001063 int qp;
1064 vpx_codec_err_t vpx_ret =
1065 vpx_codec_control(decoder_, VPXD_GET_LAST_QUANTIZER, &qp);
1066 RTC_DCHECK_EQ(vpx_ret, VPX_CODEC_OK);
1067 int ret =
1068 ReturnFrame(img, input_image._timeStamp, input_image.ntp_time_ms_, qp);
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001069 if (ret != 0) {
1070 return ret;
1071 }
1072 return WEBRTC_VIDEO_CODEC_OK;
1073}
1074
asapersson1490f7a2016-09-23 02:09:46 -07001075int VP9DecoderImpl::ReturnFrame(const vpx_image_t* img,
1076 uint32_t timestamp,
sakal7adadb12017-02-23 02:54:57 -08001077 int64_t ntp_time_ms,
1078 int qp) {
sprang3958ed82017-08-17 08:12:10 -07001079 if (img == nullptr) {
1080 // Decoder OK and nullptr image => No show frame.
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001081 return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
1082 }
Henrik Boström9695d852015-05-06 10:42:15 +02001083
1084 // This buffer contains all of |img|'s image data, a reference counted
perkj14f41442015-11-30 22:15:45 -08001085 // Vp9FrameBuffer. (libvpx is done with the buffers after a few
Henrik Boström9695d852015-05-06 10:42:15 +02001086 // vpx_codec_decode calls or vpx_codec_destroy).
1087 Vp9FrameBufferPool::Vp9FrameBuffer* img_buffer =
1088 static_cast<Vp9FrameBufferPool::Vp9FrameBuffer*>(img->fb_priv);
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07001089 // The buffer can be used directly by the VideoFrame (without copy) by
Henrik Boström9695d852015-05-06 10:42:15 +02001090 // using a WrappedI420Buffer.
1091 rtc::scoped_refptr<WrappedI420Buffer> img_wrapped_buffer(
1092 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
philipelcce46fc2015-12-21 03:04:49 -08001093 img->d_w, img->d_h, img->planes[VPX_PLANE_Y],
1094 img->stride[VPX_PLANE_Y], img->planes[VPX_PLANE_U],
1095 img->stride[VPX_PLANE_U], img->planes[VPX_PLANE_V],
1096 img->stride[VPX_PLANE_V],
Henrik Boström9695d852015-05-06 10:42:15 +02001097 // WrappedI420Buffer's mechanism for allowing the release of its frame
1098 // buffer is through a callback function. This is where we should
1099 // release |img_buffer|.
perkj14f41442015-11-30 22:15:45 -08001100 rtc::KeepRefUntilDone(img_buffer)));
Henrik Boström9695d852015-05-06 10:42:15 +02001101
nisseca6d5d12016-06-17 05:03:04 -07001102 VideoFrame decoded_image(img_wrapped_buffer, timestamp,
1103 0 /* render_time_ms */, webrtc::kVideoRotation_0);
asapersson1490f7a2016-09-23 02:09:46 -07001104 decoded_image.set_ntp_time_ms(ntp_time_ms);
nisseca6d5d12016-06-17 05:03:04 -07001105
Danil Chapovalov0040b662018-06-18 10:48:16 +02001106 decode_complete_callback_->Decoded(decoded_image, absl::nullopt, qp);
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001107 return WEBRTC_VIDEO_CODEC_OK;
1108}
1109
1110int VP9DecoderImpl::RegisterDecodeCompleteCallback(
1111 DecodedImageCallback* callback) {
1112 decode_complete_callback_ = callback;
1113 return WEBRTC_VIDEO_CODEC_OK;
1114}
1115
1116int VP9DecoderImpl::Release() {
Sergey Silkin3e871ea2018-03-02 13:11:04 +01001117 int ret_val = WEBRTC_VIDEO_CODEC_OK;
1118
sprang3958ed82017-08-17 08:12:10 -07001119 if (decoder_ != nullptr) {
Sergey Silkin90399692018-03-02 14:44:10 +01001120 if (inited_) {
1121 // When a codec is destroyed libvpx will release any buffers of
1122 // |frame_buffer_pool_| it is currently using.
1123 if (vpx_codec_destroy(decoder_)) {
1124 ret_val = WEBRTC_VIDEO_CODEC_MEMORY;
1125 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001126 }
1127 delete decoder_;
sprang3958ed82017-08-17 08:12:10 -07001128 decoder_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001129 }
Henrik Boström9695d852015-05-06 10:42:15 +02001130 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers
1131 // still referenced externally are deleted once fully released, not returning
1132 // to the pool.
1133 frame_buffer_pool_.ClearPool();
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001134 inited_ = false;
Sergey Silkin3e871ea2018-03-02 13:11:04 +01001135 return ret_val;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001136}
Peter Boströmb7d9a972015-12-18 16:01:11 +01001137
1138const char* VP9DecoderImpl::ImplementationName() const {
1139 return "libvpx";
1140}
1141
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001142} // namespace webrtc