LCOV - code coverage report
Current view: top level - media/webrtc/trunk/webrtc/modules/video_coding/codecs/vp9 - vp9_impl.cc (source / functions) Hit Total Coverage
Test: output.info Lines: 0 582 0.0 %
Date: 2017-07-14 16:53:18 Functions: 0 34 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       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             : 
      12             : #include "webrtc/modules/video_coding/codecs/vp9/vp9_impl.h"
      13             : 
      14             : #include <stdlib.h>
      15             : #include <string.h>
      16             : #include <time.h>
      17             : #include <vector>
      18             : 
      19             : #include "vpx/vpx_encoder.h"
      20             : #include "vpx/vpx_decoder.h"
      21             : #include "vpx/vp8cx.h"
      22             : #include "vpx/vp8dx.h"
      23             : 
      24             : #include "webrtc/base/checks.h"
      25             : #include "webrtc/base/timeutils.h"
      26             : #include "webrtc/base/keep_ref_until_done.h"
      27             : #include "webrtc/base/logging.h"
      28             : #include "webrtc/base/trace_event.h"
      29             : #include "webrtc/common_video/include/video_frame_buffer.h"
      30             : #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
      31             : #include "webrtc/modules/include/module_common_types.h"
      32             : #include "webrtc/modules/video_coding/codecs/vp9/screenshare_layers.h"
      33             : 
      34             : namespace webrtc {
      35             : 
      36             : // Only positive speeds, range for real-time coding currently is: 5 - 8.
      37             : // Lower means slower/better quality, higher means fastest/lower quality.
      38           0 : int GetCpuSpeed(int width, int height) {
      39             : #if defined(WEBRTC_ARCH_ARM) || defined(WEBRTC_ARCH_ARM64) || defined(ANDROID)
      40             :   return 8;
      41             : #else
      42             :   // For smaller resolutions, use lower speed setting (get some coding gain at
      43             :   // the cost of increased encoding complexity).
      44           0 :   if (width * height <= 352 * 288)
      45           0 :     return 5;
      46             :   else
      47           0 :     return 7;
      48             : #endif
      49             : }
      50             : 
      51           0 : bool VP9Encoder::IsSupported() {
      52           0 :   return true;
      53             : }
      54             : 
      55           0 : VP9Encoder* VP9Encoder::Create() {
      56           0 :   return new VP9EncoderImpl();
      57             : }
      58             : 
      59           0 : void VP9EncoderImpl::EncoderOutputCodedPacketCallback(vpx_codec_cx_pkt* pkt,
      60             :                                                       void* user_data) {
      61           0 :   VP9EncoderImpl* enc = static_cast<VP9EncoderImpl*>(user_data);
      62           0 :   enc->GetEncodedLayerFrame(pkt);
      63           0 : }
      64             : 
      65           0 : VP9EncoderImpl::VP9EncoderImpl()
      66             :     : encoded_image_(),
      67             :       encoded_complete_callback_(NULL),
      68             :       inited_(false),
      69             :       timestamp_(0),
      70             :       picture_id_(0),
      71             :       cpu_speed_(3),
      72             :       rc_max_intra_target_(0),
      73             :       encoder_(NULL),
      74             :       config_(NULL),
      75             :       raw_(NULL),
      76             :       input_image_(NULL),
      77             :       tl0_pic_idx_(0),
      78             :       frames_since_kf_(0),
      79             :       num_temporal_layers_(0),
      80             :       num_spatial_layers_(0),
      81             :       num_cores_(0),
      82             :       is_flexible_mode_(false),
      83             :       frames_encoded_(0),
      84             :       // Use two spatial when screensharing with flexible mode.
      85           0 :       spatial_layer_(new ScreenshareLayersVP9(2)) {
      86           0 :   memset(&codec_, 0, sizeof(codec_));
      87           0 :   memset(&svc_params_, 0, sizeof(vpx_svc_extra_cfg_t));
      88           0 :   uint32_t seed = rtc::Time32();
      89           0 :   srand(seed);
      90           0 : }
      91             : 
      92           0 : VP9EncoderImpl::~VP9EncoderImpl() {
      93           0 :   Release();
      94           0 : }
      95             : 
      96           0 : int VP9EncoderImpl::Release() {
      97           0 :   if (encoded_image_._buffer != NULL) {
      98           0 :     delete[] encoded_image_._buffer;
      99           0 :     encoded_image_._buffer = NULL;
     100             :   }
     101           0 :   if (encoder_ != NULL) {
     102           0 :     if (vpx_codec_destroy(encoder_)) {
     103           0 :       return WEBRTC_VIDEO_CODEC_MEMORY;
     104             :     }
     105           0 :     delete encoder_;
     106           0 :     encoder_ = NULL;
     107             :   }
     108           0 :   if (config_ != NULL) {
     109           0 :     delete config_;
     110           0 :     config_ = NULL;
     111             :   }
     112           0 :   if (raw_ != NULL) {
     113           0 :     vpx_img_free(raw_);
     114           0 :     raw_ = NULL;
     115             :   }
     116           0 :   inited_ = false;
     117           0 :   return WEBRTC_VIDEO_CODEC_OK;
     118             : }
     119             : 
     120           0 : bool VP9EncoderImpl::ExplicitlyConfiguredSpatialLayers() const {
     121             :   // We check target_bitrate_bps of the 0th layer to see if the spatial layers
     122             :   // (i.e. bitrates) were explicitly configured.
     123           0 :   return num_spatial_layers_ > 1 &&
     124           0 :          codec_.spatialLayers[0].target_bitrate_bps > 0;
     125             : }
     126             : 
     127           0 : bool VP9EncoderImpl::SetSvcRates() {
     128           0 :   uint8_t i = 0;
     129             : 
     130           0 :   if (ExplicitlyConfiguredSpatialLayers()) {
     131           0 :     if (num_temporal_layers_ > 1) {
     132           0 :       LOG(LS_ERROR) << "Multiple temporal layers when manually specifying "
     133           0 :                        "spatial layers not implemented yet!";
     134           0 :       return false;
     135             :     }
     136           0 :     int total_bitrate_bps = 0;
     137           0 :     for (i = 0; i < num_spatial_layers_; ++i)
     138           0 :       total_bitrate_bps += codec_.spatialLayers[i].target_bitrate_bps;
     139             :     // If total bitrate differs now from what has been specified at the
     140             :     // beginning, update the bitrates in the same ratio as before.
     141           0 :     for (i = 0; i < num_spatial_layers_; ++i) {
     142           0 :       config_->ss_target_bitrate[i] = config_->layer_target_bitrate[i] =
     143           0 :           static_cast<int>(static_cast<int64_t>(config_->rc_target_bitrate) *
     144           0 :                            codec_.spatialLayers[i].target_bitrate_bps /
     145             :                            total_bitrate_bps);
     146             :     }
     147             :   } else {
     148           0 :     float rate_ratio[VPX_MAX_LAYERS] = {0};
     149           0 :     float total = 0;
     150             : 
     151           0 :     for (i = 0; i < num_spatial_layers_; ++i) {
     152           0 :       if (svc_params_.scaling_factor_num[i] <= 0 ||
     153           0 :           svc_params_.scaling_factor_den[i] <= 0) {
     154           0 :         LOG(LS_ERROR) << "Scaling factors not specified!";
     155           0 :         return false;
     156             :       }
     157           0 :       rate_ratio[i] =
     158           0 :           static_cast<float>(svc_params_.scaling_factor_num[i]) /
     159           0 :           svc_params_.scaling_factor_den[i];
     160           0 :       total += rate_ratio[i];
     161             :     }
     162             : 
     163           0 :     for (i = 0; i < num_spatial_layers_; ++i) {
     164           0 :       config_->ss_target_bitrate[i] = static_cast<unsigned int>(
     165           0 :           config_->rc_target_bitrate * rate_ratio[i] / total);
     166           0 :       if (num_temporal_layers_ == 1) {
     167           0 :         config_->layer_target_bitrate[i] = config_->ss_target_bitrate[i];
     168           0 :       } else if (num_temporal_layers_ == 2) {
     169           0 :         config_->layer_target_bitrate[i * num_temporal_layers_] =
     170           0 :             config_->ss_target_bitrate[i] * 2 / 3;
     171           0 :         config_->layer_target_bitrate[i * num_temporal_layers_ + 1] =
     172           0 :             config_->ss_target_bitrate[i];
     173           0 :       } else if (num_temporal_layers_ == 3) {
     174           0 :         config_->layer_target_bitrate[i * num_temporal_layers_] =
     175           0 :             config_->ss_target_bitrate[i] / 2;
     176           0 :         config_->layer_target_bitrate[i * num_temporal_layers_ + 1] =
     177           0 :             config_->layer_target_bitrate[i * num_temporal_layers_] +
     178           0 :             (config_->ss_target_bitrate[i] / 4);
     179           0 :         config_->layer_target_bitrate[i * num_temporal_layers_ + 2] =
     180           0 :             config_->ss_target_bitrate[i];
     181             :       } else {
     182           0 :         LOG(LS_ERROR) << "Unsupported number of temporal layers: "
     183           0 :                       << num_temporal_layers_;
     184           0 :         return false;
     185             :       }
     186             :     }
     187             :   }
     188             : 
     189             :   // For now, temporal layers only supported when having one spatial layer.
     190           0 :   if (num_spatial_layers_ == 1) {
     191           0 :     for (i = 0; i < num_temporal_layers_; ++i) {
     192           0 :       config_->ts_target_bitrate[i] = config_->layer_target_bitrate[i];
     193             :     }
     194             :   }
     195             : 
     196           0 :   return true;
     197             : }
     198             : 
     199           0 : int VP9EncoderImpl::SetRateAllocation(
     200             :     const BitrateAllocation& bitrate_allocation,
     201             :     uint32_t frame_rate) {
     202           0 :   if (!inited_) {
     203           0 :     return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
     204             :   }
     205           0 :   if (encoder_->err) {
     206           0 :     return WEBRTC_VIDEO_CODEC_ERROR;
     207             :   }
     208           0 :   if (frame_rate < 1) {
     209           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     210             :   }
     211             :   // Update bit rate
     212           0 :   if (codec_.maxBitrate > 0 &&
     213           0 :       bitrate_allocation.get_sum_kbps() > codec_.maxBitrate) {
     214           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     215             :   }
     216             : 
     217             :   // TODO(sprang): Actually use BitrateAllocation layer info.
     218           0 :   config_->rc_target_bitrate = bitrate_allocation.get_sum_kbps();
     219           0 :   codec_.maxFramerate = frame_rate;
     220           0 :   spatial_layer_->ConfigureBitrate(bitrate_allocation.get_sum_kbps(), 0);
     221             : 
     222           0 :   if (!SetSvcRates()) {
     223           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     224             :   }
     225             : 
     226             :   // Update encoder context
     227           0 :   if (vpx_codec_enc_config_set(encoder_, config_)) {
     228           0 :     return WEBRTC_VIDEO_CODEC_ERROR;
     229             :   }
     230           0 :   return WEBRTC_VIDEO_CODEC_OK;
     231             : }
     232             : 
     233           0 : int VP9EncoderImpl::InitEncode(const VideoCodec* inst,
     234             :                                int number_of_cores,
     235             :                                size_t /*max_payload_size*/) {
     236           0 :   if (inst == NULL) {
     237           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     238             :   }
     239           0 :   if (inst->maxFramerate < 1) {
     240           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     241             :   }
     242             :   // Allow zero to represent an unspecified maxBitRate
     243           0 :   if (inst->maxBitrate > 0 && inst->startBitrate > inst->maxBitrate) {
     244           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     245             :   }
     246           0 :   if (inst->width < 1 || inst->height < 1) {
     247           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     248             :   }
     249           0 :   if (number_of_cores < 1 || number_of_cores > UINT8_MAX) {
     250           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     251             :   }
     252           0 :   if (inst->VP9().numberOfTemporalLayers > 3) {
     253           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     254             :   }
     255             :   // libvpx currently supports only one or two spatial layers.
     256           0 :   if (inst->VP9().numberOfSpatialLayers > 2) {
     257           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     258             :   }
     259             : 
     260           0 :   int ret_val = Release();
     261           0 :   if (ret_val < 0) {
     262           0 :     return ret_val;
     263             :   }
     264           0 :   if (encoder_ == NULL) {
     265           0 :     encoder_ = new vpx_codec_ctx_t;
     266             :     // Only randomize pid/tl0 the first time the encoder is initialized
     267             :     // in order to not make random jumps mid-stream.
     268           0 :     picture_id_ = static_cast<uint16_t>(rand()) & 0x7FFF;  // NOLINT
     269           0 :     tl0_pic_idx_ = static_cast<uint8_t>(rand());           // NOLINT
     270             :   }
     271           0 :   if (config_ == NULL) {
     272           0 :     config_ = new vpx_codec_enc_cfg_t;
     273             :   }
     274           0 :   timestamp_ = 0;
     275           0 :   if (&codec_ != inst) {
     276           0 :     codec_ = *inst;
     277             :   }
     278             : 
     279           0 :   num_cores_ = number_of_cores;
     280           0 :   num_spatial_layers_ = inst->VP9().numberOfSpatialLayers;
     281           0 :   num_temporal_layers_ = inst->VP9().numberOfTemporalLayers;
     282           0 :   if (num_temporal_layers_ == 0)
     283           0 :     num_temporal_layers_ = 1;
     284             : 
     285             :   // Allocate memory for encoded image
     286           0 :   if (encoded_image_._buffer != NULL) {
     287           0 :     delete[] encoded_image_._buffer;
     288             :   }
     289           0 :   encoded_image_._size = CalcBufferSize(kI420, codec_.width, codec_.height);
     290           0 :   encoded_image_._buffer = new uint8_t[encoded_image_._size];
     291           0 :   encoded_image_._completeFrame = true;
     292             :   // Creating a wrapper to the image - setting image data to NULL. Actual
     293             :   // pointer will be set in encode. Setting align to 1, as it is meaningless
     294             :   // (actual memory is not allocated).
     295           0 :   raw_ = vpx_img_wrap(NULL, VPX_IMG_FMT_I420, codec_.width, codec_.height, 1,
     296             :                       NULL);
     297             :   // Populate encoder configuration with default values.
     298           0 :   if (vpx_codec_enc_config_default(vpx_codec_vp9_cx(), config_, 0)) {
     299           0 :     return WEBRTC_VIDEO_CODEC_ERROR;
     300             :   }
     301           0 :   config_->g_w = codec_.width;
     302           0 :   config_->g_h = codec_.height;
     303           0 :   config_->rc_target_bitrate = inst->startBitrate;  // in kbit/s
     304           0 :   config_->g_error_resilient = 1;
     305             :   // Setting the time base of the codec.
     306           0 :   config_->g_timebase.num = 1;
     307           0 :   config_->g_timebase.den = 90000;
     308           0 :   config_->g_lag_in_frames = 0;  // 0- no frame lagging
     309           0 :   config_->g_threads = 1;
     310             :   // Rate control settings.
     311           0 :   config_->rc_dropframe_thresh = inst->VP9().frameDroppingOn ? 30 : 0;
     312           0 :   config_->rc_end_usage = VPX_CBR;
     313           0 :   config_->g_pass = VPX_RC_ONE_PASS;
     314           0 :   config_->rc_min_quantizer = 2;
     315           0 :   config_->rc_max_quantizer = 52;
     316           0 :   config_->rc_undershoot_pct = 50;
     317           0 :   config_->rc_overshoot_pct = 50;
     318           0 :   config_->rc_buf_initial_sz = 500;
     319           0 :   config_->rc_buf_optimal_sz = 600;
     320           0 :   config_->rc_buf_sz = 1000;
     321             :   // Set the maximum target size of any key-frame.
     322           0 :   rc_max_intra_target_ = MaxIntraTarget(config_->rc_buf_optimal_sz);
     323           0 :   if (inst->VP9().keyFrameInterval > 0) {
     324           0 :     config_->kf_mode = VPX_KF_AUTO;
     325           0 :     config_->kf_max_dist = inst->VP9().keyFrameInterval;
     326             :     // Needs to be set (in svc mode) to get correct periodic key frame interval
     327             :     // (will have no effect in non-svc).
     328           0 :     config_->kf_min_dist = config_->kf_max_dist;
     329             :   } else {
     330           0 :     config_->kf_mode = VPX_KF_DISABLED;
     331             :   }
     332           0 :   config_->rc_resize_allowed = inst->VP9().automaticResizeOn ? 1 : 0;
     333             :   // Determine number of threads based on the image size and #cores.
     334           0 :   config_->g_threads =
     335           0 :       NumberOfThreads(config_->g_w, config_->g_h, number_of_cores);
     336             : 
     337           0 :   cpu_speed_ = GetCpuSpeed(config_->g_w, config_->g_h);
     338             : 
     339             :   // TODO(asapersson): Check configuration of temporal switch up and increase
     340             :   // pattern length.
     341           0 :   is_flexible_mode_ = inst->VP9().flexibleMode;
     342           0 :   if (is_flexible_mode_) {
     343           0 :     config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_BYPASS;
     344           0 :     config_->ts_number_layers = num_temporal_layers_;
     345           0 :     if (codec_.mode == kScreensharing)
     346           0 :       spatial_layer_->ConfigureBitrate(inst->startBitrate, 0);
     347           0 :   } else if (num_temporal_layers_ == 1) {
     348           0 :     gof_.SetGofInfoVP9(kTemporalStructureMode1);
     349           0 :     config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_NOLAYERING;
     350           0 :     config_->ts_number_layers = 1;
     351           0 :     config_->ts_rate_decimator[0] = 1;
     352           0 :     config_->ts_periodicity = 1;
     353           0 :     config_->ts_layer_id[0] = 0;
     354           0 :   } else if (num_temporal_layers_ == 2) {
     355           0 :     gof_.SetGofInfoVP9(kTemporalStructureMode2);
     356           0 :     config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0101;
     357           0 :     config_->ts_number_layers = 2;
     358           0 :     config_->ts_rate_decimator[0] = 2;
     359           0 :     config_->ts_rate_decimator[1] = 1;
     360           0 :     config_->ts_periodicity = 2;
     361           0 :     config_->ts_layer_id[0] = 0;
     362           0 :     config_->ts_layer_id[1] = 1;
     363           0 :   } else if (num_temporal_layers_ == 3) {
     364           0 :     gof_.SetGofInfoVP9(kTemporalStructureMode3);
     365           0 :     config_->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_0212;
     366           0 :     config_->ts_number_layers = 3;
     367           0 :     config_->ts_rate_decimator[0] = 4;
     368           0 :     config_->ts_rate_decimator[1] = 2;
     369           0 :     config_->ts_rate_decimator[2] = 1;
     370           0 :     config_->ts_periodicity = 4;
     371           0 :     config_->ts_layer_id[0] = 0;
     372           0 :     config_->ts_layer_id[1] = 2;
     373           0 :     config_->ts_layer_id[2] = 1;
     374           0 :     config_->ts_layer_id[3] = 2;
     375             :   } else {
     376           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     377             :   }
     378             : 
     379           0 :   return InitAndSetControlSettings(inst);
     380             : }
     381             : 
     382           0 : int VP9EncoderImpl::NumberOfThreads(int width,
     383             :                                     int height,
     384             :                                     int number_of_cores) {
     385             :   // Keep the number of encoder threads equal to the possible number of column
     386             :   // tiles, which is (1, 2, 4, 8). See comments below for VP9E_SET_TILE_COLUMNS.
     387           0 :   if (width * height >= 1280 * 720 && number_of_cores > 4) {
     388           0 :     return 4;
     389           0 :   } else if (width * height >= 640 * 480 && number_of_cores > 2) {
     390           0 :     return 2;
     391             :   } else {
     392             :     // 1 thread less than VGA.
     393           0 :     return 1;
     394             :   }
     395             : }
     396             : 
     397           0 : int VP9EncoderImpl::InitAndSetControlSettings(const VideoCodec* inst) {
     398             :   // Set QP-min/max per spatial and temporal layer.
     399           0 :   int tot_num_layers = num_spatial_layers_ * num_temporal_layers_;
     400           0 :   for (int i = 0; i < tot_num_layers; ++i) {
     401           0 :     svc_params_.max_quantizers[i] = config_->rc_max_quantizer;
     402           0 :     svc_params_.min_quantizers[i] = config_->rc_min_quantizer;
     403             :   }
     404           0 :   config_->ss_number_layers = num_spatial_layers_;
     405           0 :   if (ExplicitlyConfiguredSpatialLayers()) {
     406           0 :     for (int i = 0; i < num_spatial_layers_; ++i) {
     407           0 :       const auto& layer = codec_.spatialLayers[i];
     408           0 :       svc_params_.scaling_factor_num[i] = layer.scaling_factor_num;
     409           0 :       svc_params_.scaling_factor_den[i] = layer.scaling_factor_den;
     410             :     }
     411             :   } else {
     412           0 :     int scaling_factor_num = 256;
     413           0 :     for (int i = num_spatial_layers_ - 1; i >= 0; --i) {
     414             :       // 1:2 scaling in each dimension.
     415           0 :       svc_params_.scaling_factor_num[i] = scaling_factor_num;
     416           0 :       svc_params_.scaling_factor_den[i] = 256;
     417           0 :       if (codec_.mode != kScreensharing)
     418           0 :         scaling_factor_num /= 2;
     419             :     }
     420             :   }
     421             : 
     422           0 :   if (!SetSvcRates()) {
     423           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     424             :   }
     425             : 
     426           0 :   if (vpx_codec_enc_init(encoder_, vpx_codec_vp9_cx(), config_, 0)) {
     427           0 :     return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
     428             :   }
     429           0 :   vpx_codec_control(encoder_, VP8E_SET_CPUUSED, cpu_speed_);
     430           0 :   vpx_codec_control(encoder_, VP8E_SET_MAX_INTRA_BITRATE_PCT,
     431           0 :                     rc_max_intra_target_);
     432           0 :   vpx_codec_control(encoder_, VP9E_SET_AQ_MODE,
     433           0 :                     inst->VP9().adaptiveQpMode ? 3 : 0);
     434             : 
     435           0 :   vpx_codec_control(
     436             :       encoder_, VP9E_SET_SVC,
     437           0 :       (num_temporal_layers_ > 1 || num_spatial_layers_ > 1) ? 1 : 0);
     438           0 :   if (num_temporal_layers_ > 1 || num_spatial_layers_ > 1) {
     439           0 :     vpx_codec_control(encoder_, VP9E_SET_SVC_PARAMETERS,
     440           0 :                       &svc_params_);
     441             :   }
     442             :   // Register callback for getting each spatial layer.
     443             :   vpx_codec_priv_output_cx_pkt_cb_pair_t cbp = {
     444             :       VP9EncoderImpl::EncoderOutputCodedPacketCallback,
     445           0 :       reinterpret_cast<void*>(this)};
     446           0 :   vpx_codec_control(encoder_, VP9E_REGISTER_CX_CALLBACK,
     447           0 :                     reinterpret_cast<void*>(&cbp));
     448             : 
     449             :   // Control function to set the number of column tiles in encoding a frame, in
     450             :   // log2 unit: e.g., 0 = 1 tile column, 1 = 2 tile columns, 2 = 4 tile columns.
     451             :   // The number tile columns will be capped by the encoder based on image size
     452             :   // (minimum width of tile column is 256 pixels, maximum is 4096).
     453           0 :   vpx_codec_control(encoder_, VP9E_SET_TILE_COLUMNS, (config_->g_threads >> 1));
     454             : #if !defined(WEBRTC_ARCH_ARM) && !defined(WEBRTC_ARCH_ARM64) && \
     455             :   !defined(ANDROID)
     456             :   // Note denoiser is still off by default until further testing/optimization,
     457             :   // i.e., VP9().denoisingOn == 0.
     458           0 :   vpx_codec_control(encoder_, VP9E_SET_NOISE_SENSITIVITY,
     459           0 :                     inst->VP9().denoisingOn ? 1 : 0);
     460             : #endif
     461           0 :   if (codec_.mode == kScreensharing) {
     462             :     // Adjust internal parameters to screen content.
     463           0 :     vpx_codec_control(encoder_, VP9E_SET_TUNE_CONTENT, 1);
     464             :   }
     465             :   // Enable encoder skip of static/low content blocks.
     466           0 :   vpx_codec_control(encoder_, VP8E_SET_STATIC_THRESHOLD, 1);
     467           0 :   inited_ = true;
     468           0 :   return WEBRTC_VIDEO_CODEC_OK;
     469             : }
     470             : 
     471           0 : uint32_t VP9EncoderImpl::MaxIntraTarget(uint32_t optimal_buffer_size) {
     472             :   // Set max to the optimal buffer level (normalized by target BR),
     473             :   // and scaled by a scale_par.
     474             :   // Max target size = scale_par * optimal_buffer_size * targetBR[Kbps].
     475             :   // This value is presented in percentage of perFrameBw:
     476             :   // perFrameBw = targetBR[Kbps] * 1000 / framerate.
     477             :   // The target in % is as follows:
     478           0 :   float scale_par = 0.5;
     479             :   uint32_t target_pct =
     480           0 :       optimal_buffer_size * scale_par * codec_.maxFramerate / 10;
     481             :   // Don't go below 3 times the per frame bandwidth.
     482           0 :   const uint32_t min_intra_size = 300;
     483           0 :   return (target_pct < min_intra_size) ? min_intra_size : target_pct;
     484             : }
     485             : 
     486           0 : int VP9EncoderImpl::Encode(const VideoFrame& input_image,
     487             :                            const CodecSpecificInfo* codec_specific_info,
     488             :                            const std::vector<FrameType>* frame_types) {
     489           0 :   if (!inited_) {
     490           0 :     return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
     491             :   }
     492           0 :   if (encoded_complete_callback_ == NULL) {
     493           0 :     return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
     494             :   }
     495           0 :   FrameType frame_type = kVideoFrameDelta;
     496             :   // We only support one stream at the moment.
     497           0 :   if (frame_types && frame_types->size() > 0) {
     498           0 :     frame_type = (*frame_types)[0];
     499             :   }
     500           0 :   if (input_image.width() != codec_.width ||
     501           0 :       input_image.height() != codec_.height) {
     502           0 :     int ret = UpdateCodecFrameSize(input_image);
     503           0 :     if (ret < 0) {
     504           0 :       return ret;
     505             :     }
     506             :   }
     507           0 :   RTC_DCHECK_EQ(input_image.width(), raw_->d_w);
     508           0 :   RTC_DCHECK_EQ(input_image.height(), raw_->d_h);
     509             : 
     510             :   // Set input image for use in the callback.
     511             :   // This was necessary since you need some information from input_image.
     512             :   // You can save only the necessary information (such as timestamp) instead of
     513             :   // doing this.
     514           0 :   input_image_ = &input_image;
     515             : 
     516             :   // Image in vpx_image_t format.
     517             :   // Input image is const. VPX's raw image is not defined as const.
     518           0 :   raw_->planes[VPX_PLANE_Y] =
     519           0 :       const_cast<uint8_t*>(input_image.video_frame_buffer()->DataY());
     520           0 :   raw_->planes[VPX_PLANE_U] =
     521           0 :       const_cast<uint8_t*>(input_image.video_frame_buffer()->DataU());
     522           0 :   raw_->planes[VPX_PLANE_V] =
     523           0 :       const_cast<uint8_t*>(input_image.video_frame_buffer()->DataV());
     524           0 :   raw_->stride[VPX_PLANE_Y] = input_image.video_frame_buffer()->StrideY();
     525           0 :   raw_->stride[VPX_PLANE_U] = input_image.video_frame_buffer()->StrideU();
     526           0 :   raw_->stride[VPX_PLANE_V] = input_image.video_frame_buffer()->StrideV();
     527             : 
     528           0 :   vpx_enc_frame_flags_t flags = 0;
     529           0 :   bool send_keyframe = (frame_type == kVideoFrameKey);
     530           0 :   if (send_keyframe) {
     531             :     // Key frame request from caller.
     532           0 :     flags = VPX_EFLAG_FORCE_KF;
     533             :   }
     534             : 
     535           0 :   if (is_flexible_mode_) {
     536           0 :     SuperFrameRefSettings settings;
     537             : 
     538             :     // These structs are copied when calling vpx_codec_control,
     539             :     // therefore it is ok for them to go out of scope.
     540             :     vpx_svc_ref_frame_config enc_layer_conf;
     541             :     vpx_svc_layer_id layer_id;
     542             : 
     543           0 :     if (codec_.mode == kRealtimeVideo) {
     544             :       // Real time video not yet implemented in flexible mode.
     545           0 :       RTC_NOTREACHED();
     546             :     } else {
     547             :       settings = spatial_layer_->GetSuperFrameSettings(input_image.timestamp(),
     548           0 :                                                        send_keyframe);
     549             :     }
     550           0 :     enc_layer_conf = GenerateRefsAndFlags(settings);
     551           0 :     layer_id.temporal_layer_id = 0;
     552           0 :     layer_id.spatial_layer_id = settings.start_layer;
     553           0 :     vpx_codec_control(encoder_, VP9E_SET_SVC_LAYER_ID, &layer_id);
     554           0 :     vpx_codec_control(encoder_, VP9E_SET_SVC_REF_FRAME_CONFIG, &enc_layer_conf);
     555             :   }
     556             : 
     557           0 :   assert(codec_.maxFramerate > 0);
     558           0 :   uint32_t duration = 90000 / codec_.maxFramerate;
     559           0 :   if (vpx_codec_encode(encoder_, raw_, timestamp_, duration, flags,
     560             :                        VPX_DL_REALTIME)) {
     561           0 :     return WEBRTC_VIDEO_CODEC_ERROR;
     562             :   }
     563           0 :   timestamp_ += duration;
     564             : 
     565           0 :   return WEBRTC_VIDEO_CODEC_OK;
     566             : }
     567             : 
     568           0 : int VP9EncoderImpl::UpdateCodecFrameSize(
     569             :     const VideoFrame& input_image) {
     570           0 :   fprintf(stderr, "Reconfiging VP( from %dx%d to %dx%d\n",
     571           0 :           codec_.width, codec_.height, input_image.width(), input_image.height());
     572             :   // Preserve latest bitrate/framerate setting
     573           0 :   uint32_t old_bitrate_kbit = config_->rc_target_bitrate;
     574           0 :   uint32_t old_framerate = codec_.maxFramerate;
     575             : 
     576           0 :   codec_.width = input_image.width();
     577           0 :   codec_.height = input_image.height();
     578             : 
     579           0 :   vpx_img_free(raw_);
     580           0 :   raw_ = vpx_img_wrap(NULL, VPX_IMG_FMT_I420, codec_.width, codec_.height,
     581             :                       1, NULL);
     582             :   // Update encoder context for new frame size.
     583           0 :   config_->g_w = codec_.width;
     584           0 :   config_->g_h = codec_.height;
     585             : 
     586             :   // Determine number of threads based on the image size and #cores.
     587           0 :   config_->g_threads = NumberOfThreads(codec_.width, codec_.height,
     588           0 :                                        num_cores_);
     589             :   // Update the cpu_speed setting for resolution change.
     590           0 :   cpu_speed_ = GetCpuSpeed(codec_.width, codec_.height);
     591             : 
     592             :   // NOTE: We would like to do this the same way vp8 does it
     593             :   // (with vpx_codec_enc_config_set()), but that causes asserts
     594             :   // in AQ 3 (cyclic); and in AQ 0 it works, but on a resize to smaller
     595             :   // than 1/2 x 1/2 original it asserts in convolve().  Given these
     596             :   // bugs in trying to do it the "right" way, we basically re-do
     597             :   // the initialization.
     598           0 :   vpx_codec_destroy(encoder_); // clean up old state
     599           0 :   int result = InitAndSetControlSettings(&codec_);
     600           0 :   if (result == WEBRTC_VIDEO_CODEC_OK) {
     601           0 :     return SetRates(old_bitrate_kbit, old_framerate);
     602             :   }
     603           0 :   return result;
     604             : }
     605             : 
     606           0 : void VP9EncoderImpl::PopulateCodecSpecific(CodecSpecificInfo* codec_specific,
     607             :                                            const vpx_codec_cx_pkt& pkt,
     608             :                                            uint32_t timestamp) {
     609           0 :   assert(codec_specific != NULL);
     610           0 :   codec_specific->codecType = kVideoCodecVP9;
     611           0 :   codec_specific->codec_name = ImplementationName();
     612           0 :   CodecSpecificInfoVP9* vp9_info = &(codec_specific->codecSpecific.VP9);
     613             :   // TODO(asapersson): Set correct value.
     614           0 :   vp9_info->inter_pic_predicted =
     615           0 :       (pkt.data.frame.flags & VPX_FRAME_IS_KEY) ? false : true;
     616           0 :   vp9_info->flexible_mode = codec_.VP9()->flexibleMode;
     617           0 :   vp9_info->ss_data_available =
     618           0 :       ((pkt.data.frame.flags & VPX_FRAME_IS_KEY) && !codec_.VP9()->flexibleMode)
     619           0 :           ? true
     620             :           : false;
     621             : 
     622           0 :   vpx_svc_layer_id_t layer_id = {0};
     623           0 :   vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
     624             : 
     625           0 :   assert(num_temporal_layers_ > 0);
     626           0 :   assert(num_spatial_layers_ > 0);
     627           0 :   if (num_temporal_layers_ == 1) {
     628           0 :     assert(layer_id.temporal_layer_id == 0);
     629           0 :     vp9_info->temporal_idx = kNoTemporalIdx;
     630             :   } else {
     631           0 :     vp9_info->temporal_idx = layer_id.temporal_layer_id;
     632             :   }
     633           0 :   if (num_spatial_layers_ == 1) {
     634           0 :     assert(layer_id.spatial_layer_id == 0);
     635           0 :     vp9_info->spatial_idx = kNoSpatialIdx;
     636             :   } else {
     637           0 :     vp9_info->spatial_idx = layer_id.spatial_layer_id;
     638             :   }
     639           0 :   if (layer_id.spatial_layer_id != 0) {
     640           0 :     vp9_info->ss_data_available = false;
     641             :   }
     642             : 
     643             :   // TODO(asapersson): this info has to be obtained from the encoder.
     644           0 :   vp9_info->temporal_up_switch = false;
     645             : 
     646           0 :   bool is_first_frame = false;
     647           0 :   if (is_flexible_mode_) {
     648           0 :     is_first_frame =
     649           0 :         layer_id.spatial_layer_id == spatial_layer_->GetStartLayer();
     650             :   } else {
     651           0 :     is_first_frame = layer_id.spatial_layer_id == 0;
     652             :   }
     653             : 
     654           0 :   if (is_first_frame) {
     655           0 :     picture_id_ = (picture_id_ + 1) & 0x7FFF;
     656             :     // TODO(asapersson): this info has to be obtained from the encoder.
     657           0 :     vp9_info->inter_layer_predicted = false;
     658           0 :     ++frames_since_kf_;
     659             :   } else {
     660             :     // TODO(asapersson): this info has to be obtained from the encoder.
     661           0 :     vp9_info->inter_layer_predicted = true;
     662             :   }
     663             : 
     664           0 :   if (pkt.data.frame.flags & VPX_FRAME_IS_KEY) {
     665           0 :     frames_since_kf_ = 0;
     666             :   }
     667             : 
     668           0 :   vp9_info->picture_id = picture_id_;
     669             : 
     670           0 :   if (!vp9_info->flexible_mode) {
     671           0 :     if (layer_id.temporal_layer_id == 0 && layer_id.spatial_layer_id == 0) {
     672           0 :       tl0_pic_idx_++;
     673             :     }
     674           0 :     vp9_info->tl0_pic_idx = tl0_pic_idx_;
     675             :   }
     676             : 
     677             :   // Always populate this, so that the packetizer can properly set the marker
     678             :   // bit.
     679           0 :   vp9_info->num_spatial_layers = num_spatial_layers_;
     680             : 
     681           0 :   vp9_info->num_ref_pics = 0;
     682           0 :   if (vp9_info->flexible_mode) {
     683           0 :     vp9_info->gof_idx = kNoGofIdx;
     684           0 :     vp9_info->num_ref_pics = num_ref_pics_[layer_id.spatial_layer_id];
     685           0 :     for (int i = 0; i < num_ref_pics_[layer_id.spatial_layer_id]; ++i) {
     686           0 :       vp9_info->p_diff[i] = p_diff_[layer_id.spatial_layer_id][i];
     687             :     }
     688             :   } else {
     689           0 :     vp9_info->gof_idx =
     690           0 :         static_cast<uint8_t>(frames_since_kf_ % gof_.num_frames_in_gof);
     691           0 :     vp9_info->temporal_up_switch = gof_.temporal_up_switch[vp9_info->gof_idx];
     692             :   }
     693             : 
     694           0 :   if (vp9_info->ss_data_available) {
     695           0 :     vp9_info->spatial_layer_resolution_present = true;
     696           0 :     for (size_t i = 0; i < vp9_info->num_spatial_layers; ++i) {
     697           0 :       vp9_info->width[i] = codec_.width *
     698           0 :                            svc_params_.scaling_factor_num[i] /
     699           0 :                            svc_params_.scaling_factor_den[i];
     700           0 :       vp9_info->height[i] = codec_.height *
     701           0 :                             svc_params_.scaling_factor_num[i] /
     702           0 :                             svc_params_.scaling_factor_den[i];
     703             :     }
     704           0 :     if (!vp9_info->flexible_mode) {
     705           0 :       vp9_info->gof.CopyGofInfoVP9(gof_);
     706             :     }
     707             :   }
     708           0 : }
     709             : 
     710           0 : int VP9EncoderImpl::GetEncodedLayerFrame(const vpx_codec_cx_pkt* pkt) {
     711           0 :   RTC_DCHECK_EQ(pkt->kind, VPX_CODEC_CX_FRAME_PKT);
     712             : 
     713           0 :   if (pkt->data.frame.sz > encoded_image_._size) {
     714           0 :     delete[] encoded_image_._buffer;
     715           0 :     encoded_image_._size = pkt->data.frame.sz;
     716           0 :     encoded_image_._buffer = new uint8_t[encoded_image_._size];
     717             :   }
     718           0 :   memcpy(encoded_image_._buffer, pkt->data.frame.buf, pkt->data.frame.sz);
     719           0 :   encoded_image_._length = pkt->data.frame.sz;
     720             : 
     721             :   // No data partitioning in VP9, so 1 partition only.
     722           0 :   int part_idx = 0;
     723           0 :   RTPFragmentationHeader frag_info;
     724           0 :   frag_info.VerifyAndAllocateFragmentationHeader(1);
     725           0 :   frag_info.fragmentationOffset[part_idx] = 0;
     726           0 :   frag_info.fragmentationLength[part_idx] = pkt->data.frame.sz;
     727           0 :   frag_info.fragmentationPlType[part_idx] = 0;
     728           0 :   frag_info.fragmentationTimeDiff[part_idx] = 0;
     729             : 
     730           0 :   vpx_svc_layer_id_t layer_id = {0};
     731           0 :   vpx_codec_control(encoder_, VP9E_GET_SVC_LAYER_ID, &layer_id);
     732           0 :   if (is_flexible_mode_ && codec_.mode == kScreensharing)
     733           0 :     spatial_layer_->LayerFrameEncoded(
     734           0 :         static_cast<unsigned int>(encoded_image_._length),
     735           0 :         layer_id.spatial_layer_id);
     736             : 
     737             :   // End of frame.
     738             :   // Check if encoded frame is a key frame.
     739           0 :   encoded_image_._frameType = kVideoFrameDelta;
     740           0 :   if (pkt->data.frame.flags & VPX_FRAME_IS_KEY) {
     741           0 :     encoded_image_._frameType = kVideoFrameKey;
     742             :   }
     743           0 :   RTC_DCHECK_LE(encoded_image_._length, encoded_image_._size);
     744             : 
     745           0 :   CodecSpecificInfo codec_specific;
     746           0 :   PopulateCodecSpecific(&codec_specific, *pkt, input_image_->timestamp());
     747             : 
     748           0 :   if (encoded_image_._length > 0) {
     749           0 :     TRACE_COUNTER1("webrtc", "EncodedFrameSize", encoded_image_._length);
     750           0 :     encoded_image_._timeStamp = input_image_->timestamp();
     751           0 :     encoded_image_.capture_time_ms_ = input_image_->render_time_ms();
     752           0 :     encoded_image_.rotation_ = input_image_->rotation();
     753           0 :     encoded_image_._encodedHeight = raw_->d_h;
     754           0 :     encoded_image_._encodedWidth = raw_->d_w;
     755           0 :     int qp = -1;
     756           0 :     vpx_codec_control(encoder_, VP8E_GET_LAST_QUANTIZER, &qp);
     757           0 :     encoded_image_.qp_ = qp;
     758           0 :     encoded_complete_callback_->OnEncodedImage(encoded_image_, &codec_specific,
     759           0 :                                                &frag_info);
     760             :   }
     761           0 :   return WEBRTC_VIDEO_CODEC_OK;
     762             : }
     763             : 
     764           0 : vpx_svc_ref_frame_config VP9EncoderImpl::GenerateRefsAndFlags(
     765             :     const SuperFrameRefSettings& settings) {
     766             :   static const vpx_enc_frame_flags_t kAllFlags =
     767             :       VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_LAST |
     768             :       VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF;
     769           0 :   vpx_svc_ref_frame_config sf_conf = {};
     770           0 :   if (settings.is_keyframe) {
     771             :     // Used later on to make sure we don't make any invalid references.
     772           0 :     memset(buffer_updated_at_frame_, -1, sizeof(buffer_updated_at_frame_));
     773           0 :     for (int layer = settings.start_layer; layer <= settings.stop_layer;
     774             :          ++layer) {
     775           0 :       num_ref_pics_[layer] = 0;
     776           0 :       buffer_updated_at_frame_[settings.layer[layer].upd_buf] = frames_encoded_;
     777             :       // When encoding a keyframe only the alt_fb_idx is used
     778             :       // to specify which layer ends up in which buffer.
     779           0 :       sf_conf.alt_fb_idx[layer] = settings.layer[layer].upd_buf;
     780             :     }
     781             :   } else {
     782           0 :     for (int layer_idx = settings.start_layer; layer_idx <= settings.stop_layer;
     783             :          ++layer_idx) {
     784           0 :       vpx_enc_frame_flags_t layer_flags = kAllFlags;
     785           0 :       num_ref_pics_[layer_idx] = 0;
     786           0 :       int8_t refs[3] = {settings.layer[layer_idx].ref_buf1,
     787           0 :                         settings.layer[layer_idx].ref_buf2,
     788           0 :                         settings.layer[layer_idx].ref_buf3};
     789             : 
     790           0 :       for (unsigned int ref_idx = 0; ref_idx < kMaxVp9RefPics; ++ref_idx) {
     791           0 :         if (refs[ref_idx] == -1)
     792           0 :           continue;
     793             : 
     794           0 :         RTC_DCHECK_GE(refs[ref_idx], 0);
     795           0 :         RTC_DCHECK_LE(refs[ref_idx], 7);
     796             :         // Easier to remove flags from all flags rather than having to
     797             :         // build the flags from 0.
     798           0 :         switch (num_ref_pics_[layer_idx]) {
     799             :           case 0: {
     800           0 :             sf_conf.lst_fb_idx[layer_idx] = refs[ref_idx];
     801           0 :             layer_flags &= ~VP8_EFLAG_NO_REF_LAST;
     802           0 :             break;
     803             :           }
     804             :           case 1: {
     805           0 :             sf_conf.gld_fb_idx[layer_idx] = refs[ref_idx];
     806           0 :             layer_flags &= ~VP8_EFLAG_NO_REF_GF;
     807           0 :             break;
     808             :           }
     809             :           case 2: {
     810           0 :             sf_conf.alt_fb_idx[layer_idx] = refs[ref_idx];
     811           0 :             layer_flags &= ~VP8_EFLAG_NO_REF_ARF;
     812           0 :             break;
     813             :           }
     814             :         }
     815             :         // Make sure we don't reference a buffer that hasn't been
     816             :         // used at all or hasn't been used since a keyframe.
     817           0 :         RTC_DCHECK_NE(buffer_updated_at_frame_[refs[ref_idx]], -1);
     818             : 
     819           0 :         p_diff_[layer_idx][num_ref_pics_[layer_idx]] =
     820           0 :             frames_encoded_ - buffer_updated_at_frame_[refs[ref_idx]];
     821           0 :         num_ref_pics_[layer_idx]++;
     822             :       }
     823             : 
     824           0 :       bool upd_buf_same_as_a_ref = false;
     825           0 :       if (settings.layer[layer_idx].upd_buf != -1) {
     826           0 :         for (unsigned int ref_idx = 0; ref_idx < kMaxVp9RefPics; ++ref_idx) {
     827           0 :           if (settings.layer[layer_idx].upd_buf == refs[ref_idx]) {
     828           0 :             switch (ref_idx) {
     829             :               case 0: {
     830           0 :                 layer_flags &= ~VP8_EFLAG_NO_UPD_LAST;
     831           0 :                 break;
     832             :               }
     833             :               case 1: {
     834           0 :                 layer_flags &= ~VP8_EFLAG_NO_UPD_GF;
     835           0 :                 break;
     836             :               }
     837             :               case 2: {
     838           0 :                 layer_flags &= ~VP8_EFLAG_NO_UPD_ARF;
     839           0 :                 break;
     840             :               }
     841             :             }
     842           0 :             upd_buf_same_as_a_ref = true;
     843           0 :             break;
     844             :           }
     845             :         }
     846           0 :         if (!upd_buf_same_as_a_ref) {
     847             :           // If we have three references and a buffer is specified to be
     848             :           // updated, then that buffer must be the same as one of the
     849             :           // three references.
     850           0 :           RTC_CHECK_LT(num_ref_pics_[layer_idx], kMaxVp9RefPics);
     851             : 
     852           0 :           sf_conf.alt_fb_idx[layer_idx] = settings.layer[layer_idx].upd_buf;
     853           0 :           layer_flags ^= VP8_EFLAG_NO_UPD_ARF;
     854             :         }
     855             : 
     856           0 :         int updated_buffer = settings.layer[layer_idx].upd_buf;
     857           0 :         buffer_updated_at_frame_[updated_buffer] = frames_encoded_;
     858           0 :         sf_conf.frame_flags[layer_idx] = layer_flags;
     859             :       }
     860             :     }
     861             :   }
     862           0 :   ++frames_encoded_;
     863           0 :   return sf_conf;
     864             : }
     865             : 
     866           0 : int VP9EncoderImpl::SetChannelParameters(uint32_t packet_loss, int64_t rtt) {
     867           0 :   return WEBRTC_VIDEO_CODEC_OK;
     868             : }
     869             : 
     870           0 : int VP9EncoderImpl::RegisterEncodeCompleteCallback(
     871             :     EncodedImageCallback* callback) {
     872           0 :   encoded_complete_callback_ = callback;
     873           0 :   return WEBRTC_VIDEO_CODEC_OK;
     874             : }
     875             : 
     876           0 : const char* VP9EncoderImpl::ImplementationName() const {
     877           0 :   return "libvpx";
     878             : }
     879             : 
     880           0 : bool VP9Decoder::IsSupported() {
     881           0 :   return true;
     882             : }
     883             : 
     884           0 : VP9Decoder* VP9Decoder::Create() {
     885           0 :   return new VP9DecoderImpl();
     886             : }
     887             : 
     888           0 : VP9DecoderImpl::VP9DecoderImpl()
     889             :     : decode_complete_callback_(NULL),
     890             :       inited_(false),
     891             :       decoder_(NULL),
     892           0 :       key_frame_required_(true) {
     893           0 :   memset(&codec_, 0, sizeof(codec_));
     894           0 : }
     895             : 
     896           0 : VP9DecoderImpl::~VP9DecoderImpl() {
     897           0 :   inited_ = true;  // in order to do the actual release
     898           0 :   Release();
     899           0 :   int num_buffers_in_use = frame_buffer_pool_.GetNumBuffersInUse();
     900           0 :   if (num_buffers_in_use > 0) {
     901             :     // The frame buffers are reference counted and frames are exposed after
     902             :     // decoding. There may be valid usage cases where previous frames are still
     903             :     // referenced after ~VP9DecoderImpl that is not a leak.
     904           0 :     LOG(LS_INFO) << num_buffers_in_use << " Vp9FrameBuffers are still "
     905           0 :                  << "referenced during ~VP9DecoderImpl.";
     906             :   }
     907           0 : }
     908             : 
     909           0 : int VP9DecoderImpl::InitDecode(const VideoCodec* inst, int number_of_cores) {
     910           0 :   if (inst == NULL) {
     911           0 :     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
     912             :   }
     913           0 :   int ret_val = Release();
     914           0 :   if (ret_val < 0) {
     915           0 :     return ret_val;
     916             :   }
     917           0 :   if (decoder_ == NULL) {
     918           0 :     decoder_ = new vpx_codec_ctx_t;
     919             :   }
     920             :   vpx_codec_dec_cfg_t cfg;
     921             :   // Setting number of threads to a constant value (1)
     922           0 :   cfg.threads = 1;
     923           0 :   cfg.h = cfg.w = 0;  // set after decode
     924           0 :   vpx_codec_flags_t flags = 0;
     925           0 :   if (vpx_codec_dec_init(decoder_, vpx_codec_vp9_dx(), &cfg, flags)) {
     926           0 :     return WEBRTC_VIDEO_CODEC_MEMORY;
     927             :   }
     928           0 :   if (&codec_ != inst) {
     929             :     // Save VideoCodec instance for later; mainly for duplicating the decoder.
     930           0 :     codec_ = *inst;
     931             :   }
     932             : 
     933           0 :   if (!frame_buffer_pool_.InitializeVpxUsePool(decoder_)) {
     934           0 :     return WEBRTC_VIDEO_CODEC_MEMORY;
     935             :   }
     936             : 
     937           0 :   inited_ = true;
     938             :   // Always start with a complete key frame.
     939           0 :   key_frame_required_ = true;
     940           0 :   return WEBRTC_VIDEO_CODEC_OK;
     941             : }
     942             : 
     943           0 : int VP9DecoderImpl::Decode(const EncodedImage& input_image,
     944             :                            bool missing_frames,
     945             :                            const RTPFragmentationHeader* fragmentation,
     946             :                            const CodecSpecificInfo* codec_specific_info,
     947             :                            int64_t /*render_time_ms*/) {
     948           0 :   if (!inited_) {
     949           0 :     return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
     950             :   }
     951           0 :   if (decode_complete_callback_ == NULL) {
     952           0 :     return WEBRTC_VIDEO_CODEC_UNINITIALIZED;
     953             :   }
     954             :   // Always start with a complete key frame.
     955           0 :   if (key_frame_required_) {
     956           0 :     if (input_image._frameType != kVideoFrameKey)
     957           0 :       return WEBRTC_VIDEO_CODEC_ERROR;
     958             :     // We have a key frame - is it complete?
     959           0 :     if (input_image._completeFrame) {
     960           0 :       key_frame_required_ = false;
     961             :     } else {
     962           0 :       return WEBRTC_VIDEO_CODEC_ERROR;
     963             :     }
     964             :   }
     965           0 :   vpx_codec_iter_t iter = NULL;
     966             :   vpx_image_t* img;
     967           0 :   uint8_t* buffer = input_image._buffer;
     968           0 :   if (input_image._length == 0) {
     969           0 :     buffer = NULL;  // Triggers full frame concealment.
     970             :   }
     971             :   // During decode libvpx may get and release buffers from |frame_buffer_pool_|.
     972             :   // In practice libvpx keeps a few (~3-4) buffers alive at a time.
     973           0 :   if (vpx_codec_decode(decoder_, buffer,
     974           0 :                        static_cast<unsigned int>(input_image._length), 0,
     975             :                        VPX_DL_REALTIME)) {
     976           0 :     return WEBRTC_VIDEO_CODEC_ERROR;
     977             :   }
     978             :   // |img->fb_priv| contains the image data, a reference counted Vp9FrameBuffer.
     979             :   // It may be released by libvpx during future vpx_codec_decode or
     980             :   // vpx_codec_destroy calls.
     981           0 :   img = vpx_codec_get_frame(decoder_, &iter);
     982           0 :   int ret = ReturnFrame(img, input_image._timeStamp, input_image.ntp_time_ms_);
     983           0 :   if (ret != 0) {
     984           0 :     return ret;
     985             :   }
     986           0 :   return WEBRTC_VIDEO_CODEC_OK;
     987             : }
     988             : 
     989           0 : int VP9DecoderImpl::ReturnFrame(const vpx_image_t* img,
     990             :                                 uint32_t timestamp,
     991             :                                 int64_t ntp_time_ms) {
     992           0 :   if (img == NULL) {
     993             :     // Decoder OK and NULL image => No show frame.
     994           0 :     return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
     995             :   }
     996             : 
     997             :   // This buffer contains all of |img|'s image data, a reference counted
     998             :   // Vp9FrameBuffer. (libvpx is done with the buffers after a few
     999             :   // vpx_codec_decode calls or vpx_codec_destroy).
    1000             :   Vp9FrameBufferPool::Vp9FrameBuffer* img_buffer =
    1001           0 :       static_cast<Vp9FrameBufferPool::Vp9FrameBuffer*>(img->fb_priv);
    1002             :   // The buffer can be used directly by the VideoFrame (without copy) by
    1003             :   // using a WrappedI420Buffer.
    1004             :   rtc::scoped_refptr<WrappedI420Buffer> img_wrapped_buffer(
    1005             :       new rtc::RefCountedObject<webrtc::WrappedI420Buffer>(
    1006             :           img->d_w, img->d_h, img->planes[VPX_PLANE_Y],
    1007             :           img->stride[VPX_PLANE_Y], img->planes[VPX_PLANE_U],
    1008             :           img->stride[VPX_PLANE_U], img->planes[VPX_PLANE_V],
    1009             :           img->stride[VPX_PLANE_V],
    1010             :           // WrappedI420Buffer's mechanism for allowing the release of its frame
    1011             :           // buffer is through a callback function. This is where we should
    1012             :           // release |img_buffer|.
    1013           0 :           rtc::KeepRefUntilDone(img_buffer)));
    1014             : 
    1015             :   VideoFrame decoded_image(img_wrapped_buffer, timestamp,
    1016           0 :                            0 /* render_time_ms */, webrtc::kVideoRotation_0);
    1017           0 :   decoded_image.set_ntp_time_ms(ntp_time_ms);
    1018           0 :   int ret = decode_complete_callback_->Decoded(decoded_image);
    1019           0 :   if (ret != 0)
    1020           0 :     return ret;
    1021           0 :   return WEBRTC_VIDEO_CODEC_OK;
    1022             : }
    1023             : 
    1024           0 : int VP9DecoderImpl::RegisterDecodeCompleteCallback(
    1025             :     DecodedImageCallback* callback) {
    1026           0 :   decode_complete_callback_ = callback;
    1027           0 :   return WEBRTC_VIDEO_CODEC_OK;
    1028             : }
    1029             : 
    1030           0 : int VP9DecoderImpl::Release() {
    1031           0 :   if (decoder_ != NULL) {
    1032             :     // When a codec is destroyed libvpx will release any buffers of
    1033             :     // |frame_buffer_pool_| it is currently using.
    1034           0 :     if (vpx_codec_destroy(decoder_)) {
    1035           0 :       return WEBRTC_VIDEO_CODEC_MEMORY;
    1036             :     }
    1037           0 :     delete decoder_;
    1038           0 :     decoder_ = NULL;
    1039             :   }
    1040             :   // Releases buffers from the pool. Any buffers not in use are deleted. Buffers
    1041             :   // still referenced externally are deleted once fully released, not returning
    1042             :   // to the pool.
    1043           0 :   frame_buffer_pool_.ClearPool();
    1044           0 :   inited_ = false;
    1045           0 :   return WEBRTC_VIDEO_CODEC_OK;
    1046             : }
    1047             : 
    1048           0 : const char* VP9DecoderImpl::ImplementationName() const {
    1049           0 :   return "libvpx";
    1050             : }
    1051             : 
    1052             : }  // namespace webrtc

Generated by: LCOV version 1.13