blob: c7c52bb809c4f9244b40e505574dc2d7c14c461d [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>
marpan@webrtc.org5b883172014-11-01 06:10:48 +000015#include <vector>
16
17#include "vpx/vpx_encoder.h"
18#include "vpx/vpx_decoder.h"
19#include "vpx/vp8cx.h"
20#include "vpx/vp8dx.h"
21
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "common_video/include/video_frame_buffer.h"
23#include "common_video/libyuv/include/webrtc_libyuv.h"
Sergey Silkind902d582018-05-18 17:31:19 +020024#include "modules/rtp_rtcp/include/rtp_rtcp_defines.h"
Sergey Silkin86684962018-03-28 19:32:37 +020025#include "modules/video_coding/codecs/vp9/svc_rate_allocator.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "rtc_base/checks.h"
27#include "rtc_base/keep_ref_until_done.h"
28#include "rtc_base/logging.h"
Magnus Jedvert46a27652017-11-13 14:10:02 +010029#include "rtc_base/ptr_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020030#include "rtc_base/timeutils.h"
31#include "rtc_base/trace_event.h"
marpan@webrtc.org5b883172014-11-01 06:10:48 +000032
33namespace webrtc {
34
Sergey Silkind902d582018-05-18 17:31:19 +020035namespace {
36const float kMaxScreenSharingFramerateFps = 5.0f;
37}
38
Marco6e89b252015-07-07 14:40:38 -070039// Only positive speeds, range for real-time coding currently is: 5 - 8.
40// Lower means slower/better quality, higher means fastest/lower quality.
41int GetCpuSpeed(int width, int height) {
Alex Glaznevfecb7c32016-03-31 14:23:27 -070042#if defined(WEBRTC_ARCH_ARM) || defined(WEBRTC_ARCH_ARM64) || defined(ANDROID)
Marco002f0d02015-12-17 09:49:31 -080043 return 8;
44#else
Marco6e89b252015-07-07 14:40:38 -070045 // For smaller resolutions, use lower speed setting (get some coding gain at
46 // the cost of increased encoding complexity).
47 if (width * height <= 352 * 288)
48 return 5;
49 else
50 return 7;
Marco002f0d02015-12-17 09:49:31 -080051#endif
Marco6e89b252015-07-07 14:40:38 -070052}
53
Peter Boström12996152016-05-14 02:03:18 +020054bool VP9Encoder::IsSupported() {
55 return true;
56}
57
Magnus Jedvert46a27652017-11-13 14:10:02 +010058std::unique_ptr<VP9Encoder> VP9Encoder::Create() {
59 return rtc::MakeUnique<VP9EncoderImpl>();
marpan@webrtc.org5b883172014-11-01 06:10:48 +000060}
61
asaperssona9455ab2015-07-31 06:10:09 -070062void VP9EncoderImpl::EncoderOutputCodedPacketCallback(vpx_codec_cx_pkt* pkt,
63 void* user_data) {
philipelcce46fc2015-12-21 03:04:49 -080064 VP9EncoderImpl* enc = static_cast<VP9EncoderImpl*>(user_data);
asaperssona9455ab2015-07-31 06:10:09 -070065 enc->GetEncodedLayerFrame(pkt);
66}
67
marpan@webrtc.org5b883172014-11-01 06:10:48 +000068VP9EncoderImpl::VP9EncoderImpl()
69 : encoded_image_(),
sprang3958ed82017-08-17 08:12:10 -070070 encoded_complete_callback_(nullptr),
marpan@webrtc.org5b883172014-11-01 06:10:48 +000071 inited_(false),
72 timestamp_(0),
marpan@webrtc.org5b883172014-11-01 06:10:48 +000073 cpu_speed_(3),
74 rc_max_intra_target_(0),
sprang3958ed82017-08-17 08:12:10 -070075 encoder_(nullptr),
76 config_(nullptr),
77 raw_(nullptr),
78 input_image_(nullptr),
Sergey Silkinbd0954e2018-05-03 14:14:09 +020079 force_key_frame_(true),
Sergey Silkin6a8f30e2018-04-26 11:03:49 +020080 pics_since_key_(0),
asaperssona9455ab2015-07-31 06:10:09 -070081 num_temporal_layers_(0),
philipelcfc319b2015-11-10 07:17:23 -080082 num_spatial_layers_(0),
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +020083 is_svc_(false),
Sergey Silkin6a8f30e2018-04-26 11:03:49 +020084 inter_layer_pred_(InterLayerPredMode::kOn),
Sergey Silkind902d582018-05-18 17:31:19 +020085 output_framerate_(1000.0, 1000.0),
86 last_encoded_frame_rtp_timestamp_(0),
Sergey Silkinbe71a1e2018-05-17 16:46:43 +020087 is_flexible_mode_(false) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +000088 memset(&codec_, 0, sizeof(codec_));
johannkoenig8225c402017-01-26 13:23:44 -080089 memset(&svc_params_, 0, sizeof(vpx_svc_extra_cfg_t));
marpan@webrtc.org5b883172014-11-01 06:10:48 +000090}
91
92VP9EncoderImpl::~VP9EncoderImpl() {
93 Release();
94}
95
96int VP9EncoderImpl::Release() {
Sergey Silkin3e871ea2018-03-02 13:11:04 +010097 int ret_val = WEBRTC_VIDEO_CODEC_OK;
98
sprang3958ed82017-08-17 08:12:10 -070099 if (encoded_image_._buffer != nullptr) {
philipelcce46fc2015-12-21 03:04:49 -0800100 delete[] encoded_image_._buffer;
sprang3958ed82017-08-17 08:12:10 -0700101 encoded_image_._buffer = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000102 }
sprang3958ed82017-08-17 08:12:10 -0700103 if (encoder_ != nullptr) {
Sergey Silkin90399692018-03-02 14:44:10 +0100104 if (inited_) {
105 if (vpx_codec_destroy(encoder_)) {
106 ret_val = WEBRTC_VIDEO_CODEC_MEMORY;
107 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000108 }
109 delete encoder_;
sprang3958ed82017-08-17 08:12:10 -0700110 encoder_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000111 }
sprang3958ed82017-08-17 08:12:10 -0700112 if (config_ != nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000113 delete config_;
sprang3958ed82017-08-17 08:12:10 -0700114 config_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000115 }
sprang3958ed82017-08-17 08:12:10 -0700116 if (raw_ != nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000117 vpx_img_free(raw_);
sprang3958ed82017-08-17 08:12:10 -0700118 raw_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000119 }
120 inited_ = false;
Sergey Silkin3e871ea2018-03-02 13:11:04 +0100121 return ret_val;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000122}
123
sprangce4aef12015-11-02 07:23:20 -0800124bool VP9EncoderImpl::ExplicitlyConfiguredSpatialLayers() const {
125 // We check target_bitrate_bps of the 0th layer to see if the spatial layers
126 // (i.e. bitrates) were explicitly configured.
Sergey Silkin13e74342018-03-02 12:28:00 +0100127 return num_spatial_layers_ > 1 && codec_.spatialLayers[0].targetBitrate > 0;
sprangce4aef12015-11-02 07:23:20 -0800128}
129
Erik Språng566124a2018-04-23 12:32:22 +0200130bool VP9EncoderImpl::SetSvcRates(
131 const VideoBitrateAllocation& bitrate_allocation) {
asaperssona9455ab2015-07-31 06:10:09 -0700132 uint8_t i = 0;
133
Sergey Silkin86684962018-03-28 19:32:37 +0200134 config_->rc_target_bitrate = bitrate_allocation.get_sum_kbps();
Sergey Silkin86684962018-03-28 19:32:37 +0200135
sprangce4aef12015-11-02 07:23:20 -0800136 if (ExplicitlyConfiguredSpatialLayers()) {
Sergey Silkin86684962018-03-28 19:32:37 +0200137 for (size_t sl_idx = 0; sl_idx < num_spatial_layers_; ++sl_idx) {
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200138 const bool was_layer_enabled = (config_->ss_target_bitrate[sl_idx] > 0);
Sergey Silkin86684962018-03-28 19:32:37 +0200139 config_->ss_target_bitrate[sl_idx] =
140 bitrate_allocation.GetSpatialLayerSum(sl_idx) / 1000;
141
142 for (size_t tl_idx = 0; tl_idx < num_temporal_layers_; ++tl_idx) {
143 config_->layer_target_bitrate[sl_idx * num_temporal_layers_ + tl_idx] =
144 bitrate_allocation.GetTemporalLayerSum(sl_idx, tl_idx) / 1000;
145 }
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200146
147 const bool is_layer_enabled = (config_->ss_target_bitrate[sl_idx] > 0);
148 if (is_layer_enabled && !was_layer_enabled) {
149 if (inter_layer_pred_ == InterLayerPredMode::kOff ||
150 inter_layer_pred_ == InterLayerPredMode::kOnKeyPic) {
151 // TODO(wemb:1526): remove key frame request when issue is fixed.
152 force_key_frame_ = true;
153 }
154 }
sprangce4aef12015-11-02 07:23:20 -0800155 }
156 } else {
157 float rate_ratio[VPX_MAX_LAYERS] = {0};
158 float total = 0;
asaperssona9455ab2015-07-31 06:10:09 -0700159
sprangce4aef12015-11-02 07:23:20 -0800160 for (i = 0; i < num_spatial_layers_; ++i) {
johannkoenig8225c402017-01-26 13:23:44 -0800161 if (svc_params_.scaling_factor_num[i] <= 0 ||
162 svc_params_.scaling_factor_den[i] <= 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100163 RTC_LOG(LS_ERROR) << "Scaling factors not specified!";
sprangce4aef12015-11-02 07:23:20 -0800164 return false;
165 }
166 rate_ratio[i] =
johannkoenig8225c402017-01-26 13:23:44 -0800167 static_cast<float>(svc_params_.scaling_factor_num[i]) /
168 svc_params_.scaling_factor_den[i];
sprangce4aef12015-11-02 07:23:20 -0800169 total += rate_ratio[i];
170 }
171
172 for (i = 0; i < num_spatial_layers_; ++i) {
173 config_->ss_target_bitrate[i] = static_cast<unsigned int>(
174 config_->rc_target_bitrate * rate_ratio[i] / total);
175 if (num_temporal_layers_ == 1) {
176 config_->layer_target_bitrate[i] = config_->ss_target_bitrate[i];
177 } else if (num_temporal_layers_ == 2) {
178 config_->layer_target_bitrate[i * num_temporal_layers_] =
179 config_->ss_target_bitrate[i] * 2 / 3;
180 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] =
181 config_->ss_target_bitrate[i];
182 } else if (num_temporal_layers_ == 3) {
183 config_->layer_target_bitrate[i * num_temporal_layers_] =
184 config_->ss_target_bitrate[i] / 2;
185 config_->layer_target_bitrate[i * num_temporal_layers_ + 1] =
186 config_->layer_target_bitrate[i * num_temporal_layers_] +
187 (config_->ss_target_bitrate[i] / 4);
188 config_->layer_target_bitrate[i * num_temporal_layers_ + 2] =
189 config_->ss_target_bitrate[i];
190 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100191 RTC_LOG(LS_ERROR) << "Unsupported number of temporal layers: "
192 << num_temporal_layers_;
sprangce4aef12015-11-02 07:23:20 -0800193 return false;
194 }
asaperssona9455ab2015-07-31 06:10:09 -0700195 }
196 }
197
198 // For now, temporal layers only supported when having one spatial layer.
199 if (num_spatial_layers_ == 1) {
200 for (i = 0; i < num_temporal_layers_; ++i) {
201 config_->ts_target_bitrate[i] = config_->layer_target_bitrate[i];
202 }
203 }
204
205 return true;
206}
207
Erik Språng08127a92016-11-16 16:41:30 +0100208int VP9EncoderImpl::SetRateAllocation(
Erik Språng566124a2018-04-23 12:32:22 +0200209 const VideoBitrateAllocation& bitrate_allocation,
Erik Språng08127a92016-11-16 16:41:30 +0100210 uint32_t frame_rate) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000211 if (!inited_) {
212 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
213 }
214 if (encoder_->err) {
215 return WEBRTC_VIDEO_CODEC_ERROR;
216 }
Erik Språng08127a92016-11-16 16:41:30 +0100217 if (frame_rate < 1) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000218 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
219 }
220 // Update bit rate
Erik Språng08127a92016-11-16 16:41:30 +0100221 if (codec_.maxBitrate > 0 &&
222 bitrate_allocation.get_sum_kbps() > codec_.maxBitrate) {
223 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000224 }
Erik Språng08127a92016-11-16 16:41:30 +0100225
Erik Språng08127a92016-11-16 16:41:30 +0100226 codec_.maxFramerate = frame_rate;
asaperssona9455ab2015-07-31 06:10:09 -0700227
Sergey Silkin86684962018-03-28 19:32:37 +0200228 if (!SetSvcRates(bitrate_allocation)) {
asaperssona9455ab2015-07-31 06:10:09 -0700229 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
230 }
231
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000232 // Update encoder context
233 if (vpx_codec_enc_config_set(encoder_, config_)) {
234 return WEBRTC_VIDEO_CODEC_ERROR;
235 }
236 return WEBRTC_VIDEO_CODEC_OK;
237}
238
239int VP9EncoderImpl::InitEncode(const VideoCodec* inst,
240 int number_of_cores,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000241 size_t /*max_payload_size*/) {
sprang3958ed82017-08-17 08:12:10 -0700242 if (inst == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000243 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
244 }
245 if (inst->maxFramerate < 1) {
246 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
247 }
248 // Allow zero to represent an unspecified maxBitRate
249 if (inst->maxBitrate > 0 && inst->startBitrate > inst->maxBitrate) {
250 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
251 }
252 if (inst->width < 1 || inst->height < 1) {
253 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
254 }
255 if (number_of_cores < 1) {
256 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
257 }
hta257dc392016-10-25 09:05:06 -0700258 if (inst->VP9().numberOfTemporalLayers > 3) {
asaperssona9455ab2015-07-31 06:10:09 -0700259 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
260 }
ilnik2a8c2f52017-02-15 02:23:28 -0800261 // libvpx probably does not support more than 3 spatial layers.
262 if (inst->VP9().numberOfSpatialLayers > 3) {
asaperssona9455ab2015-07-31 06:10:09 -0700263 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
264 }
philipelcfc319b2015-11-10 07:17:23 -0800265
asapersson86956de2016-01-26 01:05:20 -0800266 int ret_val = Release();
267 if (ret_val < 0) {
268 return ret_val;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000269 }
sprang3958ed82017-08-17 08:12:10 -0700270 if (encoder_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000271 encoder_ = new vpx_codec_ctx_t;
272 }
sprang3958ed82017-08-17 08:12:10 -0700273 if (config_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000274 config_ = new vpx_codec_enc_cfg_t;
275 }
276 timestamp_ = 0;
277 if (&codec_ != inst) {
278 codec_ = *inst;
279 }
asaperssona9455ab2015-07-31 06:10:09 -0700280
hta257dc392016-10-25 09:05:06 -0700281 num_spatial_layers_ = inst->VP9().numberOfSpatialLayers;
Niels Möller65fb4042018-04-25 14:46:06 +0200282 RTC_DCHECK_GT(num_spatial_layers_, 0);
hta257dc392016-10-25 09:05:06 -0700283 num_temporal_layers_ = inst->VP9().numberOfTemporalLayers;
asaperssona9455ab2015-07-31 06:10:09 -0700284 if (num_temporal_layers_ == 0)
285 num_temporal_layers_ = 1;
286
Sergey Silkind902d582018-05-18 17:31:19 +0200287 // Init framerate controller.
288 output_framerate_.Reset();
289 if (codec_.mode == kScreensharing) {
290 target_framerate_fps_ = kMaxScreenSharingFramerateFps;
291 } else {
292 target_framerate_fps_.reset();
293 }
294
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200295 is_svc_ = (num_spatial_layers_ > 1 || num_temporal_layers_ > 1);
296 // Flexible mode requires SVC to be enabled since libvpx API only allows
297 // to get reference list in SVC mode.
298 RTC_DCHECK(!inst->VP9().flexibleMode || is_svc_);
299
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000300 // Allocate memory for encoded image
sprang3958ed82017-08-17 08:12:10 -0700301 if (encoded_image_._buffer != nullptr) {
philipelcce46fc2015-12-21 03:04:49 -0800302 delete[] encoded_image_._buffer;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000303 }
nisseeb44b392017-04-28 07:18:05 -0700304 encoded_image_._size =
305 CalcBufferSize(VideoType::kI420, codec_.width, codec_.height);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000306 encoded_image_._buffer = new uint8_t[encoded_image_._size];
307 encoded_image_._completeFrame = true;
sprang3958ed82017-08-17 08:12:10 -0700308 // Creating a wrapper to the image - setting image data to nullptr. Actual
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000309 // pointer will be set in encode. Setting align to 1, as it is meaningless
310 // (actual memory is not allocated).
sprang3958ed82017-08-17 08:12:10 -0700311 raw_ = vpx_img_wrap(nullptr, VPX_IMG_FMT_I420, codec_.width, codec_.height, 1,
312 nullptr);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000313 // Populate encoder configuration with default values.
314 if (vpx_codec_enc_config_default(vpx_codec_vp9_cx(), config_, 0)) {
315 return WEBRTC_VIDEO_CODEC_ERROR;
316 }
317 config_->g_w = codec_.width;
318 config_->g_h = codec_.height;
319 config_->rc_target_bitrate = inst->startBitrate; // in kbit/s
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200320 config_->g_error_resilient = is_svc_ ? VPX_ERROR_RESILIENT_DEFAULT : 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000321 // Setting the time base of the codec.
322 config_->g_timebase.num = 1;
323 config_->g_timebase.den = 90000;
324 config_->g_lag_in_frames = 0; // 0- no frame lagging
325 config_->g_threads = 1;
326 // Rate control settings.
hta257dc392016-10-25 09:05:06 -0700327 config_->rc_dropframe_thresh = inst->VP9().frameDroppingOn ? 30 : 0;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000328 config_->rc_end_usage = VPX_CBR;
329 config_->g_pass = VPX_RC_ONE_PASS;
330 config_->rc_min_quantizer = 2;
marpan@webrtc.orgdc8a9da2015-01-27 23:08:24 +0000331 config_->rc_max_quantizer = 52;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000332 config_->rc_undershoot_pct = 50;
333 config_->rc_overshoot_pct = 50;
334 config_->rc_buf_initial_sz = 500;
335 config_->rc_buf_optimal_sz = 600;
336 config_->rc_buf_sz = 1000;
337 // Set the maximum target size of any key-frame.
338 rc_max_intra_target_ = MaxIntraTarget(config_->rc_buf_optimal_sz);
hta257dc392016-10-25 09:05:06 -0700339 if (inst->VP9().keyFrameInterval > 0) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000340 config_->kf_mode = VPX_KF_AUTO;
hta257dc392016-10-25 09:05:06 -0700341 config_->kf_max_dist = inst->VP9().keyFrameInterval;
Åsa Perssonff24c042015-12-04 10:58:08 +0100342 // Needs to be set (in svc mode) to get correct periodic key frame interval
343 // (will have no effect in non-svc).
344 config_->kf_min_dist = config_->kf_max_dist;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000345 } else {
346 config_->kf_mode = VPX_KF_DISABLED;
347 }
hta257dc392016-10-25 09:05:06 -0700348 config_->rc_resize_allowed = inst->VP9().automaticResizeOn ? 1 : 0;
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000349 // Determine number of threads based on the image size and #cores.
philipelcce46fc2015-12-21 03:04:49 -0800350 config_->g_threads =
351 NumberOfThreads(config_->g_w, config_->g_h, number_of_cores);
asaperssona9455ab2015-07-31 06:10:09 -0700352
Marco6e89b252015-07-07 14:40:38 -0700353 cpu_speed_ = GetCpuSpeed(config_->g_w, config_->g_h);
asaperssona9455ab2015-07-31 06:10:09 -0700354
355 // TODO(asapersson): Check configuration of temporal switch up and increase
356 // pattern length.
hta257dc392016-10-25 09:05:06 -0700357 is_flexible_mode_ = inst->VP9().flexibleMode;
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200358
359 // TODO(ssilkin): Only non-flexible mode is supported for now.
360 RTC_DCHECK(!is_flexible_mode_);
361
362 if (num_temporal_layers_ == 1) {
asaperssona9455ab2015-07-31 06:10:09 -0700363 gof_.SetGofInfoVP9(kTemporalStructureMode1);
364 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING;
365 config_->ts_number_layers = 1;
366 config_->ts_rate_decimator[0] = 1;
367 config_->ts_periodicity = 1;
368 config_->ts_layer_id[0] = 0;
369 } else if (num_temporal_layers_ == 2) {
370 gof_.SetGofInfoVP9(kTemporalStructureMode2);
371 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0101;
372 config_->ts_number_layers = 2;
373 config_->ts_rate_decimator[0] = 2;
374 config_->ts_rate_decimator[1] = 1;
375 config_->ts_periodicity = 2;
376 config_->ts_layer_id[0] = 0;
377 config_->ts_layer_id[1] = 1;
378 } else if (num_temporal_layers_ == 3) {
379 gof_.SetGofInfoVP9(kTemporalStructureMode3);
380 config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0212;
381 config_->ts_number_layers = 3;
382 config_->ts_rate_decimator[0] = 4;
383 config_->ts_rate_decimator[1] = 2;
384 config_->ts_rate_decimator[2] = 1;
385 config_->ts_periodicity = 4;
386 config_->ts_layer_id[0] = 0;
387 config_->ts_layer_id[1] = 2;
388 config_->ts_layer_id[2] = 1;
389 config_->ts_layer_id[3] = 2;
390 } else {
391 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
392 }
393
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200394 inter_layer_pred_ = inst->VP9().interLayerPred;
395
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200396 ref_buf_.clear();
397
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000398 return InitAndSetControlSettings(inst);
399}
400
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000401int VP9EncoderImpl::NumberOfThreads(int width,
402 int height,
403 int number_of_cores) {
404 // Keep the number of encoder threads equal to the possible number of column
405 // tiles, which is (1, 2, 4, 8). See comments below for VP9E_SET_TILE_COLUMNS.
406 if (width * height >= 1280 * 720 && number_of_cores > 4) {
407 return 4;
jianj23173a32017-07-12 16:11:09 -0700408 } else if (width * height >= 640 * 360 && number_of_cores > 2) {
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000409 return 2;
410 } else {
Jerome Jiang831af372017-12-05 10:44:35 -0800411 // Use 2 threads for low res on ARM.
412#if defined(WEBRTC_ARCH_ARM) || defined(WEBRTC_ARCH_ARM64) || \
413 defined(WEBRTC_ANDROID)
414 if (width * height >= 320 * 180 && number_of_cores > 2) {
415 return 2;
416 }
417#endif
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000418 // 1 thread less than VGA.
419 return 1;
420 }
421}
422
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000423int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) {
Åsa Perssonff24c042015-12-04 10:58:08 +0100424 // Set QP-min/max per spatial and temporal layer.
425 int tot_num_layers = num_spatial_layers_ * num_temporal_layers_;
426 for (int i = 0; i < tot_num_layers; ++i) {
johannkoenig8225c402017-01-26 13:23:44 -0800427 svc_params_.max_quantizers[i] = config_->rc_max_quantizer;
428 svc_params_.min_quantizers[i] = config_->rc_min_quantizer;
Åsa Perssonff24c042015-12-04 10:58:08 +0100429 }
asaperssona9455ab2015-07-31 06:10:09 -0700430 config_->ss_number_layers = num_spatial_layers_;
sprangce4aef12015-11-02 07:23:20 -0800431 if (ExplicitlyConfiguredSpatialLayers()) {
432 for (int i = 0; i < num_spatial_layers_; ++i) {
433 const auto& layer = codec_.spatialLayers[i];
Sergey Silkin13e74342018-03-02 12:28:00 +0100434 const int scale_factor = codec_.width / layer.width;
435 RTC_DCHECK_GT(scale_factor, 0);
436
437 // Ensure scaler factor is integer.
438 if (scale_factor * layer.width != codec_.width) {
439 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
440 }
441
442 // Ensure scale factor is the same in both dimensions.
443 if (scale_factor * layer.height != codec_.height) {
444 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
445 }
446
447 // Ensure scale factor is power of two.
448 const bool is_pow_of_two = (scale_factor & (scale_factor - 1)) == 0;
449 if (!is_pow_of_two) {
450 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
451 }
452
453 svc_params_.scaling_factor_num[i] = 1;
454 svc_params_.scaling_factor_den[i] = scale_factor;
sprangce4aef12015-11-02 07:23:20 -0800455 }
456 } else {
457 int scaling_factor_num = 256;
458 for (int i = num_spatial_layers_ - 1; i >= 0; --i) {
sprangce4aef12015-11-02 07:23:20 -0800459 // 1:2 scaling in each dimension.
johannkoenig8225c402017-01-26 13:23:44 -0800460 svc_params_.scaling_factor_num[i] = scaling_factor_num;
461 svc_params_.scaling_factor_den[i] = 256;
sprangce4aef12015-11-02 07:23:20 -0800462 }
asaperssona9455ab2015-07-31 06:10:09 -0700463 }
464
Sergey Silkin86684962018-03-28 19:32:37 +0200465 SvcRateAllocator init_allocator(codec_);
Erik Språng566124a2018-04-23 12:32:22 +0200466 VideoBitrateAllocation allocation = init_allocator.GetAllocation(
Sergey Silkin86684962018-03-28 19:32:37 +0200467 inst->startBitrate * 1000, inst->maxFramerate);
468 if (!SetSvcRates(allocation)) {
asaperssona9455ab2015-07-31 06:10:09 -0700469 return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
470 }
471
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000472 if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) {
473 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
474 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000475 vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_);
476 vpx_codec_control(encoder_, VP8E_SET_MAX_INTRA_BITRATE_PCT,
477 rc_max_intra_target_);
478 vpx_codec_control(encoder_, VP9E_SET_AQ_MODE,
hta257dc392016-10-25 09:05:06 -0700479 inst->VP9().adaptiveQpMode ? 3 : 0);
asaperssona9455ab2015-07-31 06:10:09 -0700480
jianj822e5932017-07-12 16:09:58 -0700481 vpx_codec_control(encoder_, VP9E_SET_FRAME_PARALLEL_DECODING, 0);
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200482
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200483 if (is_svc_) {
484 vpx_codec_control(encoder_, VP9E_SET_SVC, 1);
485 vpx_codec_control(encoder_, VP9E_SET_SVC_PARAMETERS, &svc_params_);
asaperssona9455ab2015-07-31 06:10:09 -0700486 }
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200487
488 if (num_spatial_layers_ > 1) {
489 switch (inter_layer_pred_) {
490 case InterLayerPredMode::kOn:
491 vpx_codec_control(encoder_, VP9E_SET_SVC_INTER_LAYER_PRED, 0);
492 break;
493 case InterLayerPredMode::kOff:
494 vpx_codec_control(encoder_, VP9E_SET_SVC_INTER_LAYER_PRED, 1);
495 break;
496 case InterLayerPredMode::kOnKeyPic:
497 vpx_codec_control(encoder_, VP9E_SET_SVC_INTER_LAYER_PRED, 2);
498 break;
499 default:
500 RTC_NOTREACHED();
501 }
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200502
503 if (!is_flexible_mode_) {
504 // In RTP non-flexible mode, frame dropping of individual layers in a
505 // superframe leads to incorrect reference picture ID values in the
506 // RTP header. Dropping the entire superframe if the base is dropped
507 // or not dropping upper layers if base is not dropped mitigates
508 // the problem.
509 vpx_svc_frame_drop_t svc_drop_frame;
Jerome Jiang0c2e8ce2018-05-25 22:26:43 -0700510 memset(&svc_drop_frame, 0, sizeof(svc_drop_frame));
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200511 svc_drop_frame.framedrop_mode = CONSTRAINED_LAYER_DROP;
512 for (size_t i = 0; i < num_spatial_layers_; ++i) {
513 svc_drop_frame.framedrop_thresh[i] =
514 (i == 0) ? config_->rc_dropframe_thresh : 0;
515 }
516 vpx_codec_control(encoder_, VP9E_SET_SVC_FRAME_DROP_LAYER,
517 &svc_drop_frame);
518 }
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200519 }
520
asaperssona9455ab2015-07-31 06:10:09 -0700521 // Register callback for getting each spatial layer.
522 vpx_codec_priv_output_cx_pkt_cb_pair_t cbp = {
philipelcce46fc2015-12-21 03:04:49 -0800523 VP9EncoderImpl::EncoderOutputCodedPacketCallback,
524 reinterpret_cast<void*>(this)};
525 vpx_codec_control(encoder_, VP9E_REGISTER_CX_CALLBACK,
526 reinterpret_cast<void*>(&cbp));
asaperssona9455ab2015-07-31 06:10:09 -0700527
marpan@webrtc.org38d11b82015-01-26 15:21:36 +0000528 // Control function to set the number of column tiles in encoding a frame, in
529 // log2 unit: e.g., 0 = 1 tile column, 1 = 2 tile columns, 2 = 4 tile columns.
530 // The number tile columns will be capped by the encoder based on image size
531 // (minimum width of tile column is 256 pixels, maximum is 4096).
532 vpx_codec_control(encoder_, VP9E_SET_TILE_COLUMNS, (config_->g_threads >> 1));
jianjcb5d1152017-03-28 23:56:08 -0700533
534 // Turn on row-based multithreading.
535 vpx_codec_control(encoder_, VP9E_SET_ROW_MT, 1);
jianj6bf57e32017-06-05 13:43:49 -0700536
Alex Glaznevfecb7c32016-03-31 14:23:27 -0700537#if !defined(WEBRTC_ARCH_ARM) && !defined(WEBRTC_ARCH_ARM64) && \
538 !defined(ANDROID)
jianj6bf57e32017-06-05 13:43:49 -0700539 // Do not enable the denoiser on ARM since optimization is pending.
540 // Denoiser is on by default on other platforms.
marpan@webrtc.org16a87b92015-03-05 22:19:00 +0000541 vpx_codec_control(encoder_, VP9E_SET_NOISE_SENSITIVITY,
hta257dc392016-10-25 09:05:06 -0700542 inst->VP9().denoisingOn ? 1 : 0);
marpan@webrtc.org16a87b92015-03-05 22:19:00 +0000543#endif
jianj6bf57e32017-06-05 13:43:49 -0700544
ivica242d6382015-09-04 06:13:23 -0700545 if (codec_.mode == kScreensharing) {
546 // Adjust internal parameters to screen content.
547 vpx_codec_control(encoder_, VP9E_SET_TUNE_CONTENT, 1);
ivica242d6382015-09-04 06:13:23 -0700548 }
Marco2520e722015-09-16 14:05:00 -0700549 // Enable encoder skip of static/low content blocks.
550 vpx_codec_control(encoder_, VP8E_SET_STATIC_THRESHOLD, 1);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000551 inited_ = true;
552 return WEBRTC_VIDEO_CODEC_OK;
553}
554
555uint32_t VP9EncoderImpl::MaxIntraTarget(uint32_t optimal_buffer_size) {
556 // Set max to the optimal buffer level (normalized by target BR),
557 // and scaled by a scale_par.
558 // Max target size = scale_par * optimal_buffer_size * targetBR[Kbps].
559 // This value is presented in percentage of perFrameBw:
560 // perFrameBw = targetBR[Kbps] * 1000 / framerate.
561 // The target in % is as follows:
562 float scale_par = 0.5;
563 uint32_t target_pct =
564 optimal_buffer_size * scale_par * codec_.maxFramerate / 10;
565 // Don't go below 3 times the per frame bandwidth.
566 const uint32_t min_intra_size = 300;
philipelcce46fc2015-12-21 03:04:49 -0800567 return (target_pct < min_intra_size) ? min_intra_size : target_pct;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000568}
569
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700570int VP9EncoderImpl::Encode(const VideoFrame& input_image,
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000571 const CodecSpecificInfo* codec_specific_info,
pbos22993e12015-10-19 02:39:06 -0700572 const std::vector<FrameType>* frame_types) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000573 if (!inited_) {
574 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
575 }
sprang3958ed82017-08-17 08:12:10 -0700576 if (encoded_complete_callback_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000577 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
578 }
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200579
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000580 // We only support one stream at the moment.
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200581 if (frame_types && !frame_types->empty()) {
582 if ((*frame_types)[0] == kVideoFrameKey) {
583 force_key_frame_ = true;
584 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000585 }
Sergey Silkind902d582018-05-18 17:31:19 +0200586
587 if (kScreensharing == codec_.mode && !force_key_frame_) {
588 if (DropFrame(input_image.timestamp())) {
589 return WEBRTC_VIDEO_CODEC_OK;
590 }
591 }
592
kwiberg352444f2016-11-28 15:58:53 -0800593 RTC_DCHECK_EQ(input_image.width(), raw_->d_w);
594 RTC_DCHECK_EQ(input_image.height(), raw_->d_h);
asaperssona9455ab2015-07-31 06:10:09 -0700595
596 // Set input image for use in the callback.
597 // This was necessary since you need some information from input_image.
598 // You can save only the necessary information (such as timestamp) instead of
599 // doing this.
600 input_image_ = &input_image;
601
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000602 rtc::scoped_refptr<I420BufferInterface> i420_buffer =
603 input_image.video_frame_buffer()->ToI420();
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000604 // Image in vpx_image_t format.
605 // Input image is const. VPX's raw image is not defined as const.
Magnus Jedvert72dbe2a2017-06-10 17:03:37 +0000606 raw_->planes[VPX_PLANE_Y] = const_cast<uint8_t*>(i420_buffer->DataY());
607 raw_->planes[VPX_PLANE_U] = const_cast<uint8_t*>(i420_buffer->DataU());
608 raw_->planes[VPX_PLANE_V] = const_cast<uint8_t*>(i420_buffer->DataV());
609 raw_->stride[VPX_PLANE_Y] = i420_buffer->StrideY();
610 raw_->stride[VPX_PLANE_U] = i420_buffer->StrideU();
611 raw_->stride[VPX_PLANE_V] = i420_buffer->StrideV();
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000612
philipelcfc319b2015-11-10 07:17:23 -0800613 vpx_enc_frame_flags_t flags = 0;
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200614 if (force_key_frame_) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000615 flags = VPX_EFLAG_FORCE_KF;
616 }
philipelcfc319b2015-11-10 07:17:23 -0800617
sprang3958ed82017-08-17 08:12:10 -0700618 RTC_CHECK_GT(codec_.maxFramerate, 0);
Sergey Silkind902d582018-05-18 17:31:19 +0200619 uint32_t duration =
620 90000 / target_framerate_fps_.value_or(codec_.maxFramerate);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000621 if (vpx_codec_encode(encoder_, raw_, timestamp_, duration, flags,
622 VPX_DL_REALTIME)) {
623 return WEBRTC_VIDEO_CODEC_ERROR;
624 }
625 timestamp_ += duration;
asaperssona9455ab2015-07-31 06:10:09 -0700626
Sergey Silkinbc0f0d32018-04-24 21:29:14 +0200627 const bool end_of_picture = true;
628 DeliverBufferedFrame(end_of_picture);
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200629
asaperssona9455ab2015-07-31 06:10:09 -0700630 return WEBRTC_VIDEO_CODEC_OK;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000631}
632
633void VP9EncoderImpl::PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
philipelcce46fc2015-12-21 03:04:49 -0800634 const vpx_codec_cx_pkt& pkt,
Sergey Silkin07f80cc2018-04-09 13:11:59 +0200635 uint32_t timestamp,
636 bool first_frame_in_picture) {
sprang3958ed82017-08-17 08:12:10 -0700637 RTC_CHECK(codec_specific != nullptr);
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000638 codec_specific->codecType = kVideoCodecVP9;
perkj275afc52016-09-01 00:21:16 -0700639 codec_specific->codec_name = ImplementationName();
philipelcce46fc2015-12-21 03:04:49 -0800640 CodecSpecificInfoVP9* vp9_info = &(codec_specific->codecSpecific.VP9);
Sergey Silkin07f80cc2018-04-09 13:11:59 +0200641
642 vp9_info->first_frame_in_picture = first_frame_in_picture;
hta257dc392016-10-25 09:05:06 -0700643 vp9_info->flexible_mode = codec_.VP9()->flexibleMode;
644 vp9_info->ss_data_available =
645 ((pkt.data.frame.flags & VPX_FRAME_IS_KEY) && !codec_.VP9()->flexibleMode)
646 ? true
647 : false;
asaperssona9455ab2015-07-31 06:10:09 -0700648
649 vpx_svc_layer_id_t layer_id = {0};
650 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
651
sprang3958ed82017-08-17 08:12:10 -0700652 RTC_CHECK_GT(num_temporal_layers_, 0);
653 RTC_CHECK_GT(num_spatial_layers_, 0);
asaperssona9455ab2015-07-31 06:10:09 -0700654 if (num_temporal_layers_ == 1) {
sprang3958ed82017-08-17 08:12:10 -0700655 RTC_CHECK_EQ(layer_id.temporal_layer_id, 0);
asaperssona9455ab2015-07-31 06:10:09 -0700656 vp9_info->temporal_idx = kNoTemporalIdx;
657 } else {
658 vp9_info->temporal_idx = layer_id.temporal_layer_id;
659 }
660 if (num_spatial_layers_ == 1) {
sprang3958ed82017-08-17 08:12:10 -0700661 RTC_CHECK_EQ(layer_id.spatial_layer_id, 0);
asaperssona9455ab2015-07-31 06:10:09 -0700662 vp9_info->spatial_idx = kNoSpatialIdx;
663 } else {
664 vp9_info->spatial_idx = layer_id.spatial_layer_id;
665 }
666 if (layer_id.spatial_layer_id != 0) {
667 vp9_info->ss_data_available = false;
668 }
669
asaperssona9455ab2015-07-31 06:10:09 -0700670 // TODO(asapersson): this info has to be obtained from the encoder.
asaperssoncb50c962015-11-18 01:58:55 -0800671 vp9_info->temporal_up_switch = false;
asaperssona9455ab2015-07-31 06:10:09 -0700672
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200673 if (pkt.data.frame.flags & VPX_FRAME_IS_KEY) {
674 pics_since_key_ = 0;
675 } else if (first_frame_in_picture) {
676 ++pics_since_key_;
asaperssona9455ab2015-07-31 06:10:09 -0700677 }
678
Sergey Silkin6a8f30e2018-04-26 11:03:49 +0200679 const bool is_key_pic = (pics_since_key_ == 0);
680 const bool is_inter_layer_pred_allowed =
681 (inter_layer_pred_ == InterLayerPredMode::kOn ||
682 (inter_layer_pred_ == InterLayerPredMode::kOnKeyPic && is_key_pic));
683
684 // Always set inter_layer_predicted to true on high layer frame if inter-layer
685 // prediction (ILP) is allowed even if encoder didn't actually use it.
686 // Setting inter_layer_predicted to false would allow receiver to decode high
687 // layer frame without decoding low layer frame. If that would happen (e.g.
688 // if low layer frame is lost) then receiver won't be able to decode next high
689 // layer frame which uses ILP.
690 vp9_info->inter_layer_predicted =
691 first_frame_in_picture ? false : is_inter_layer_pred_allowed;
692
693 const bool is_last_layer =
694 (layer_id.spatial_layer_id + 1 == num_spatial_layers_);
695 vp9_info->non_ref_for_inter_layer_pred =
696 is_last_layer ? true : !is_inter_layer_pred_allowed;
asapersson00ac85e2015-11-11 05:30:48 -0800697
ivica7f6a6fc2015-09-08 02:40:29 -0700698 // Always populate this, so that the packetizer can properly set the marker
699 // bit.
700 vp9_info->num_spatial_layers = num_spatial_layers_;
philipelcfc319b2015-11-10 07:17:23 -0800701
Sergey Silkinbe71a1e2018-05-17 16:46:43 +0200702 RTC_DCHECK(!vp9_info->flexible_mode);
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200703
704 vp9_info->num_ref_pics = 0;
705 if (vp9_info->flexible_mode) {
706 vp9_info->gof_idx = kNoGofIdx;
707 FillReferenceIndices(pkt, pics_since_key_, vp9_info->inter_layer_predicted,
708 vp9_info);
709 } else {
710 vp9_info->gof_idx =
711 static_cast<uint8_t>(pics_since_key_ % gof_.num_frames_in_gof);
712 vp9_info->temporal_up_switch = gof_.temporal_up_switch[vp9_info->gof_idx];
713 vp9_info->num_ref_pics = gof_.num_ref_pics[vp9_info->gof_idx];
714 }
715
716 vp9_info->inter_pic_predicted = (!is_key_pic && vp9_info->num_ref_pics > 0);
717
718 vp9_info->num_spatial_layers = num_spatial_layers_;
philipelcfc319b2015-11-10 07:17:23 -0800719
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;
722 for (size_t i = 0; i < vp9_info->num_spatial_layers; ++i) {
723 vp9_info->width[i] = codec_.width *
johannkoenig8225c402017-01-26 13:23:44 -0800724 svc_params_.scaling_factor_num[i] /
725 svc_params_.scaling_factor_den[i];
asaperssona9455ab2015-07-31 06:10:09 -0700726 vp9_info->height[i] = codec_.height *
johannkoenig8225c402017-01-26 13:23:44 -0800727 svc_params_.scaling_factor_num[i] /
728 svc_params_.scaling_factor_den[i];
asaperssona9455ab2015-07-31 06:10:09 -0700729 }
730 if (!vp9_info->flexible_mode) {
731 vp9_info->gof.CopyGofInfoVP9(gof_);
732 }
733 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000734}
735
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200736void VP9EncoderImpl::FillReferenceIndices(const vpx_codec_cx_pkt& pkt,
737 const size_t pic_num,
738 const bool inter_layer_predicted,
739 CodecSpecificInfoVP9* vp9_info) {
740 vpx_svc_layer_id_t layer_id = {0};
741 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
742
743 vpx_svc_ref_frame_config_t enc_layer_conf = {{0}};
744 vpx_codec_control(encoder_, VP9E_GET_SVC_REF_FRAME_CONFIG, &enc_layer_conf);
745
746 std::vector<RefFrameBuffer> ref_buf_list;
747 if (enc_layer_conf.reference_last[layer_id.spatial_layer_id]) {
748 const size_t fb_idx = enc_layer_conf.lst_fb_idx[layer_id.spatial_layer_id];
749 RTC_DCHECK(ref_buf_.find(fb_idx) != ref_buf_.end());
750 ref_buf_list.push_back(ref_buf_.at(fb_idx));
751 }
752
753 if (enc_layer_conf.reference_alt_ref[layer_id.spatial_layer_id]) {
754 const size_t fb_idx = enc_layer_conf.alt_fb_idx[layer_id.spatial_layer_id];
755 RTC_DCHECK(ref_buf_.find(fb_idx) != ref_buf_.end());
756 ref_buf_list.push_back(ref_buf_.at(fb_idx));
757 }
758
759 if (enc_layer_conf.reference_golden[layer_id.spatial_layer_id]) {
760 const size_t fb_idx = enc_layer_conf.gld_fb_idx[layer_id.spatial_layer_id];
761 RTC_DCHECK(ref_buf_.find(fb_idx) != ref_buf_.end());
762 ref_buf_list.push_back(ref_buf_.at(fb_idx));
763 }
764
765 size_t max_ref_temporal_layer_id = 0;
766
767 vp9_info->num_ref_pics = 0;
768 for (const RefFrameBuffer& ref_buf : ref_buf_list) {
769 RTC_DCHECK_LE(ref_buf.pic_num, pic_num);
770 if (ref_buf.pic_num < pic_num) {
771 if (inter_layer_pred_ != InterLayerPredMode::kOn) {
772 // RTP spec limits temporal prediction to the same spatial layer.
773 // It is safe to ignore this requirement if inter-layer prediction is
774 // enabled for all frames when all base frames are relayed to receiver.
775 RTC_DCHECK_EQ(ref_buf.spatial_layer_id, layer_id.spatial_layer_id);
776 }
777 RTC_DCHECK_LE(ref_buf.temporal_layer_id, layer_id.temporal_layer_id);
778
779 const size_t p_diff = pic_num - ref_buf.pic_num;
780 RTC_DCHECK_LE(p_diff, 127UL);
781
782 vp9_info->p_diff[vp9_info->num_ref_pics] = static_cast<uint8_t>(p_diff);
783 ++vp9_info->num_ref_pics;
784
785 max_ref_temporal_layer_id =
786 std::max(max_ref_temporal_layer_id, ref_buf.temporal_layer_id);
787 } else {
788 RTC_DCHECK(inter_layer_predicted);
789 // RTP spec only allows to use previous spatial layer for inter-layer
790 // prediction.
791 RTC_DCHECK_EQ(ref_buf.spatial_layer_id + 1, layer_id.spatial_layer_id);
792 }
793 }
794
795 vp9_info->temporal_up_switch =
796 (max_ref_temporal_layer_id <
797 static_cast<size_t>(layer_id.temporal_layer_id));
798}
799
800void VP9EncoderImpl::UpdateReferenceBuffers(const vpx_codec_cx_pkt& pkt,
801 const size_t pic_num) {
802 vpx_svc_layer_id_t layer_id = {0};
803 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
804
805 vpx_svc_ref_frame_config_t enc_layer_conf = {{0}};
806 vpx_codec_control(encoder_, VP9E_GET_SVC_REF_FRAME_CONFIG, &enc_layer_conf);
807
808 const bool is_key_frame =
809 (pkt.data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
810
811 RefFrameBuffer frame_buf(pic_num, layer_id.spatial_layer_id,
812 layer_id.temporal_layer_id);
813
814 if (is_key_frame && layer_id.spatial_layer_id == 0) {
815 // Key frame updates all ref buffers.
816 for (size_t i = 0; i < kNumVp9Buffers; ++i) {
817 ref_buf_[i] = frame_buf;
818 }
819 } else {
820 if (enc_layer_conf.update_last[layer_id.spatial_layer_id]) {
821 ref_buf_[enc_layer_conf.lst_fb_idx[layer_id.spatial_layer_id]] =
822 frame_buf;
823 }
824
825 if (enc_layer_conf.update_alt_ref[layer_id.spatial_layer_id]) {
826 ref_buf_[enc_layer_conf.alt_fb_idx[layer_id.spatial_layer_id]] =
827 frame_buf;
828 }
829
830 if (enc_layer_conf.update_golden[layer_id.spatial_layer_id]) {
831 ref_buf_[enc_layer_conf.gld_fb_idx[layer_id.spatial_layer_id]] =
832 frame_buf;
833 }
834 }
835}
836
asaperssona9455ab2015-07-31 06:10:09 -0700837int VP9EncoderImpl::GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt) {
asapersson86956de2016-01-26 01:05:20 -0800838 RTC_DCHECK_EQ(pkt->kind, VPX_CODEC_CX_FRAME_PKT);
asaperssona9455ab2015-07-31 06:10:09 -0700839
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200840 if (pkt->data.frame.sz == 0) {
841 // Ignore dropped frame.
842 return WEBRTC_VIDEO_CODEC_OK;
843 }
844
Sergey Silkin07f80cc2018-04-09 13:11:59 +0200845 vpx_svc_layer_id_t layer_id = {0};
846 vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
847
848 const bool first_frame_in_picture = encoded_image_._length == 0;
849 // Ensure we don't buffer layers of previous picture (superframe).
850 RTC_DCHECK(first_frame_in_picture || layer_id.spatial_layer_id > 0);
851
Sergey Silkinbc0f0d32018-04-24 21:29:14 +0200852 const bool end_of_picture = false;
853 DeliverBufferedFrame(end_of_picture);
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200854
asaperssond9f641e2016-01-21 01:11:35 -0800855 if (pkt->data.frame.sz > encoded_image_._size) {
856 delete[] encoded_image_._buffer;
857 encoded_image_._size = pkt->data.frame.sz;
858 encoded_image_._buffer = new uint8_t[encoded_image_._size];
859 }
asapersson86956de2016-01-26 01:05:20 -0800860 memcpy(encoded_image_._buffer, pkt->data.frame.buf, pkt->data.frame.sz);
861 encoded_image_._length = pkt->data.frame.sz;
asaperssond9f641e2016-01-21 01:11:35 -0800862
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200863 const bool is_key_frame =
864 (pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? true : false;
865 // Ensure encoder issued key frame on request.
866 RTC_DCHECK(is_key_frame || !force_key_frame_);
867
asaperssona9455ab2015-07-31 06:10:09 -0700868 // Check if encoded frame is a key frame.
asapersson86956de2016-01-26 01:05:20 -0800869 encoded_image_._frameType = kVideoFrameDelta;
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200870 if (is_key_frame) {
Peter Boström49e196a2015-10-23 15:58:18 +0200871 encoded_image_._frameType = kVideoFrameKey;
Sergey Silkinbd0954e2018-05-03 14:14:09 +0200872 force_key_frame_ = false;
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000873 }
asapersson86956de2016-01-26 01:05:20 -0800874 RTC_DCHECK_LE(encoded_image_._length, encoded_image_._size);
875
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200876 memset(&codec_specific_, 0, sizeof(codec_specific_));
Sergey Silkin07f80cc2018-04-09 13:11:59 +0200877 PopulateCodecSpecific(&codec_specific_, *pkt, input_image_->timestamp(),
878 first_frame_in_picture);
asaperssona9455ab2015-07-31 06:10:09 -0700879
Sergey Silkin4e6cd5e2018-05-28 12:26:36 +0200880 if (is_flexible_mode_) {
881 UpdateReferenceBuffers(*pkt, pics_since_key_);
882 }
883
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200884 TRACE_COUNTER1("webrtc", "EncodedFrameSize", encoded_image_._length);
885 encoded_image_._timeStamp = input_image_->timestamp();
886 encoded_image_.capture_time_ms_ = input_image_->render_time_ms();
887 encoded_image_.rotation_ = input_image_->rotation();
888 encoded_image_.content_type_ = (codec_.mode == kScreensharing)
889 ? VideoContentType::SCREENSHARE
890 : VideoContentType::UNSPECIFIED;
891 encoded_image_._encodedHeight =
892 pkt->data.frame.height[layer_id.spatial_layer_id];
893 encoded_image_._encodedWidth =
894 pkt->data.frame.width[layer_id.spatial_layer_id];
895 encoded_image_.timing_.flags = TimingFrameFlags::kInvalid;
896 int qp = -1;
897 vpx_codec_control(encoder_, VP8E_GET_LAST_QUANTIZER, &qp);
898 encoded_image_.qp_ = qp;
ilnik04f4d122017-06-19 07:18:55 -0700899
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000900 return WEBRTC_VIDEO_CODEC_OK;
901}
902
Sergey Silkinbc0f0d32018-04-24 21:29:14 +0200903void VP9EncoderImpl::DeliverBufferedFrame(bool end_of_picture) {
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200904 if (encoded_image_._length > 0) {
Sergey Silkinbc0f0d32018-04-24 21:29:14 +0200905 codec_specific_.codecSpecific.VP9.end_of_picture = end_of_picture;
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200906
907 // No data partitioning in VP9, so 1 partition only.
908 int part_idx = 0;
909 RTPFragmentationHeader frag_info;
910 frag_info.VerifyAndAllocateFragmentationHeader(1);
911 frag_info.fragmentationOffset[part_idx] = 0;
912 frag_info.fragmentationLength[part_idx] = encoded_image_._length;
913 frag_info.fragmentationPlType[part_idx] = 0;
914 frag_info.fragmentationTimeDiff[part_idx] = 0;
915
916 encoded_complete_callback_->OnEncodedImage(encoded_image_, &codec_specific_,
917 &frag_info);
918 encoded_image_._length = 0;
Sergey Silkind902d582018-05-18 17:31:19 +0200919
920 if (end_of_picture) {
921 const uint32_t timestamp_ms =
922 1000 * encoded_image_._timeStamp / kVideoPayloadTypeFrequency;
923 output_framerate_.Update(1, timestamp_ms);
924 last_encoded_frame_rtp_timestamp_ = encoded_image_._timeStamp;
925 }
Sergey Silkin2a1f1832018-04-04 11:45:41 +0200926 }
927}
928
Sergey Silkind902d582018-05-18 17:31:19 +0200929bool VP9EncoderImpl::DropFrame(uint32_t rtp_timestamp) {
930 if (target_framerate_fps_) {
931 if (rtp_timestamp < last_encoded_frame_rtp_timestamp_) {
932 // Timestamp has wrapped around. Reset framerate statistic.
933 output_framerate_.Reset();
934 return false;
935 }
936
937 const uint32_t timestamp_ms =
938 1000 * rtp_timestamp / kVideoPayloadTypeFrequency;
939 const uint32_t framerate_fps =
940 output_framerate_.Rate(timestamp_ms).value_or(0);
941 if (framerate_fps > *target_framerate_fps_) {
942 return true;
943 }
944
945 // Primarily check if frame interval is too short using frame timestamps,
946 // as if they are correct they won't be affected by queuing in webrtc.
947 const uint32_t expected_frame_interval =
948 kVideoPayloadTypeFrequency / *target_framerate_fps_;
949
950 const uint32_t ts_diff = rtp_timestamp - last_encoded_frame_rtp_timestamp_;
951 if (ts_diff < 85 * expected_frame_interval / 100) {
952 return true;
953 }
954 }
955
956 return false;
957}
958
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000959int VP9EncoderImpl::SetChannelParameters(uint32_t packet_loss, int64_t rtt) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000960 return WEBRTC_VIDEO_CODEC_OK;
961}
962
963int VP9EncoderImpl::RegisterEncodeCompleteCallback(
964 EncodedImageCallback* callback) {
965 encoded_complete_callback_ = callback;
966 return WEBRTC_VIDEO_CODEC_OK;
967}
968
Peter Boströmb7d9a972015-12-18 16:01:11 +0100969const char* VP9EncoderImpl::ImplementationName() const {
970 return "libvpx";
971}
972
Peter Boström12996152016-05-14 02:03:18 +0200973bool VP9Decoder::IsSupported() {
974 return true;
975}
976
Magnus Jedvert46a27652017-11-13 14:10:02 +0100977std::unique_ptr<VP9Decoder> VP9Decoder::Create() {
978 return rtc::MakeUnique<VP9DecoderImpl>();
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000979}
980
981VP9DecoderImpl::VP9DecoderImpl()
sprang3958ed82017-08-17 08:12:10 -0700982 : decode_complete_callback_(nullptr),
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000983 inited_(false),
sprang3958ed82017-08-17 08:12:10 -0700984 decoder_(nullptr),
philipel9d7d75b2018-04-04 13:03:01 +0200985 key_frame_required_(true) {}
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000986
987VP9DecoderImpl::~VP9DecoderImpl() {
988 inited_ = true; // in order to do the actual release
989 Release();
Henrik Boström9695d852015-05-06 10:42:15 +0200990 int num_buffers_in_use = frame_buffer_pool_.GetNumBuffersInUse();
991 if (num_buffers_in_use > 0) {
992 // The frame buffers are reference counted and frames are exposed after
993 // decoding. There may be valid usage cases where previous frames are still
994 // referenced after ~VP9DecoderImpl that is not a leak.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100995 RTC_LOG(LS_INFO) << num_buffers_in_use << " Vp9FrameBuffers are still "
996 << "referenced during ~VP9DecoderImpl.";
Henrik Boström9695d852015-05-06 10:42:15 +0200997 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +0000998}
999
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001000int VP9DecoderImpl::InitDecode(const VideoCodec* inst, int number_of_cores) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001001 int ret_val = Release();
1002 if (ret_val < 0) {
1003 return ret_val;
1004 }
sprang3958ed82017-08-17 08:12:10 -07001005 if (decoder_ == nullptr) {
pbos@webrtc.orge728ee02014-12-17 13:43:55 +00001006 decoder_ = new vpx_codec_ctx_t;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001007 }
philipelcce46fc2015-12-21 03:04:49 -08001008 vpx_codec_dec_cfg_t cfg;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001009 // Setting number of threads to a constant value (1)
1010 cfg.threads = 1;
1011 cfg.h = cfg.w = 0; // set after decode
1012 vpx_codec_flags_t flags = 0;
1013 if (vpx_codec_dec_init(decoder_, vpx_codec_vp9_dx(), &cfg, flags)) {
1014 return WEBRTC_VIDEO_CODEC_MEMORY;
1015 }
Henrik Boström9695d852015-05-06 10:42:15 +02001016
1017 if (!frame_buffer_pool_.InitializeVpxUsePool(decoder_)) {
1018 return WEBRTC_VIDEO_CODEC_MEMORY;
1019 }
1020
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001021 inited_ = true;
1022 // Always start with a complete key frame.
1023 key_frame_required_ = true;
1024 return WEBRTC_VIDEO_CODEC_OK;
1025}
1026
1027int VP9DecoderImpl::Decode(const EncodedImage& input_image,
1028 bool missing_frames,
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001029 const CodecSpecificInfo* codec_specific_info,
1030 int64_t /*render_time_ms*/) {
1031 if (!inited_) {
1032 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
1033 }
sprang3958ed82017-08-17 08:12:10 -07001034 if (decode_complete_callback_ == nullptr) {
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001035 return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
1036 }
1037 // Always start with a complete key frame.
1038 if (key_frame_required_) {
Peter Boström49e196a2015-10-23 15:58:18 +02001039 if (input_image._frameType != kVideoFrameKey)
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001040 return WEBRTC_VIDEO_CODEC_ERROR;
1041 // We have a key frame - is it complete?
1042 if (input_image._completeFrame) {
1043 key_frame_required_ = false;
1044 } else {
1045 return WEBRTC_VIDEO_CODEC_ERROR;
1046 }
1047 }
sprang3958ed82017-08-17 08:12:10 -07001048 vpx_codec_iter_t iter = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001049 vpx_image_t* img;
1050 uint8_t* buffer = input_image._buffer;
1051 if (input_image._length == 0) {
sprang3958ed82017-08-17 08:12:10 -07001052 buffer = nullptr; // Triggers full frame concealment.
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001053 }
Henrik Boström9695d852015-05-06 10:42:15 +02001054 // During decode libvpx may get and release buffers from |frame_buffer_pool_|.
1055 // In practice libvpx keeps a few (~3-4) buffers alive at a time.
philipelcce46fc2015-12-21 03:04:49 -08001056 if (vpx_codec_decode(decoder_, buffer,
1057 static_cast<unsigned int>(input_image._length), 0,
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001058 VPX_DL_REALTIME)) {
1059 return WEBRTC_VIDEO_CODEC_ERROR;
1060 }
Henrik Boström9695d852015-05-06 10:42:15 +02001061 // |img->fb_priv| contains the image data, a reference counted Vp9FrameBuffer.
1062 // It may be released by libvpx during future vpx_codec_decode or
1063 // vpx_codec_destroy calls.
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001064 img = vpx_codec_get_frame(decoder_, &iter);
sakal7adadb12017-02-23 02:54:57 -08001065 int qp;
1066 vpx_codec_err_t vpx_ret =
1067 vpx_codec_control(decoder_, VPXD_GET_LAST_QUANTIZER, &qp);
1068 RTC_DCHECK_EQ(vpx_ret, VPX_CODEC_OK);
1069 int ret =
1070 ReturnFrame(img, input_image._timeStamp, input_image.ntp_time_ms_, qp);
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001071 if (ret != 0) {
1072 return ret;
1073 }
1074 return WEBRTC_VIDEO_CODEC_OK;
1075}
1076
asapersson1490f7a2016-09-23 02:09:46 -07001077int VP9DecoderImpl::ReturnFrame(const vpx_image_t* img,
1078 uint32_t timestamp,
sakal7adadb12017-02-23 02:54:57 -08001079 int64_t ntp_time_ms,
1080 int qp) {
sprang3958ed82017-08-17 08:12:10 -07001081 if (img == nullptr) {
1082 // Decoder OK and nullptr image => No show frame.
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001083 return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
1084 }
Henrik Boström9695d852015-05-06 10:42:15 +02001085
1086 // This buffer contains all of |img|'s image data, a reference counted
perkj14f41442015-11-30 22:15:45 -08001087 // Vp9FrameBuffer. (libvpx is done with the buffers after a few
Henrik Boström9695d852015-05-06 10:42:15 +02001088 // vpx_codec_decode calls or vpx_codec_destroy).
1089 Vp9FrameBufferPool::Vp9FrameBuffer* img_buffer =
1090 static_cast<Vp9FrameBufferPool::Vp9FrameBuffer*>(img->fb_priv);
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -07001091 // The buffer can be used directly by the VideoFrame (without copy) by
Henrik Boström9695d852015-05-06 10:42:15 +02001092 // using a WrappedI420Buffer.
1093 rtc::scoped_refptr<WrappedI420Buffer> img_wrapped_buffer(
1094 new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
philipelcce46fc2015-12-21 03:04:49 -08001095 img->d_w, img->d_h, img->planes[VPX_PLANE_Y],
1096 img->stride[VPX_PLANE_Y], img->planes[VPX_PLANE_U],
1097 img->stride[VPX_PLANE_U], img->planes[VPX_PLANE_V],
1098 img->stride[VPX_PLANE_V],
Henrik Boström9695d852015-05-06 10:42:15 +02001099 // WrappedI420Buffer's mechanism for allowing the release of its frame
1100 // buffer is through a callback function. This is where we should
1101 // release |img_buffer|.
perkj14f41442015-11-30 22:15:45 -08001102 rtc::KeepRefUntilDone(img_buffer)));
Henrik Boström9695d852015-05-06 10:42:15 +02001103
nisseca6d5d12016-06-17 05:03:04 -07001104 VideoFrame decoded_image(img_wrapped_buffer, timestamp,
1105 0 /* render_time_ms */, webrtc::kVideoRotation_0);
asapersson1490f7a2016-09-23 02:09:46 -07001106 decoded_image.set_ntp_time_ms(ntp_time_ms);
nisseca6d5d12016-06-17 05:03:04 -07001107
Oskar Sundbom6bd39022017-11-16 10:54:49 +01001108 decode_complete_callback_->Decoded(decoded_image, rtc::nullopt, qp);
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001109 return WEBRTC_VIDEO_CODEC_OK;
1110}
1111
1112int VP9DecoderImpl::RegisterDecodeCompleteCallback(
1113 DecodedImageCallback* callback) {
1114 decode_complete_callback_ = callback;
1115 return WEBRTC_VIDEO_CODEC_OK;
1116}
1117
1118int VP9DecoderImpl::Release() {
Sergey Silkin3e871ea2018-03-02 13:11:04 +01001119 int ret_val = WEBRTC_VIDEO_CODEC_OK;
1120
sprang3958ed82017-08-17 08:12:10 -07001121 if (decoder_ != nullptr) {
Sergey Silkin90399692018-03-02 14:44:10 +01001122 if (inited_) {
1123 // When a codec is destroyed libvpx will release any buffers of
1124 // |frame_buffer_pool_| it is currently using.
1125 if (vpx_codec_destroy(decoder_)) {
1126 ret_val = WEBRTC_VIDEO_CODEC_MEMORY;
1127 }
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001128 }
1129 delete decoder_;
sprang3958ed82017-08-17 08:12:10 -07001130 decoder_ = nullptr;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001131 }
Henrik Boström9695d852015-05-06 10:42:15 +02001132 // Releases buffers from the pool. Any buffers not in use are deleted. Buffers
1133 // still referenced externally are deleted once fully released, not returning
1134 // to the pool.
1135 frame_buffer_pool_.ClearPool();
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001136 inited_ = false;
Sergey Silkin3e871ea2018-03-02 13:11:04 +01001137 return ret_val;
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001138}
Peter Boströmb7d9a972015-12-18 16:01:11 +01001139
1140const char* VP9DecoderImpl::ImplementationName() const {
1141 return "libvpx";
1142}
1143
marpan@webrtc.org5b883172014-11-01 06:10:48 +00001144} // namespace webrtc