Add initial bitrate and frame resolution parameters to quality scaler.

- Scale down to VGA immediately if call starts with HD resolution
and bitrate below 500 kbps.
- Adjust QP threshold for HW VP8 encoder to scale down faster.

BUG=b/26504665
R=mflodman@webrtc.org, pbos@webrtc.org, sprang@google.com, stefan@webrtc.org

Review URL: https://codereview.webrtc.org/1672173002 .

Cr-Commit-Position: refs/heads/master@{#11692}
diff --git a/webrtc/api/java/jni/androidmediaencoder_jni.cc b/webrtc/api/java/jni/androidmediaencoder_jni.cc
index 7fa93de..71e297c 100644
--- a/webrtc/api/java/jni/androidmediaencoder_jni.cc
+++ b/webrtc/api/java/jni/androidmediaencoder_jni.cc
@@ -45,6 +45,7 @@
 using webrtc::kVideoCodecH264;
 using webrtc::kVideoCodecVP8;
 using webrtc::kVideoCodecVP9;
+using webrtc::QualityScaler;
 
 namespace webrtc_jni {
 
@@ -242,7 +243,7 @@
   bool drop_next_input_frame_;
   // Global references; must be deleted in Release().
   std::vector<jobject> input_buffers_;
-  webrtc::QualityScaler quality_scaler_;
+  QualityScaler quality_scaler_;
   // Dynamic resolution change, off by default.
   bool scale_;
 
@@ -355,7 +356,6 @@
     size_t /* max_payload_size */) {
   const int kMinWidth = 320;
   const int kMinHeight = 180;
-  const int kLowQpThresholdDenominator = 3;
   if (codec_settings == NULL) {
     ALOGE << "NULL VideoCodec instance";
     return WEBRTC_VIDEO_CODEC_ERR_PARAMETER;
@@ -365,40 +365,52 @@
       << "Unsupported codec " << codec_settings->codecType << " for "
       << codecType_;
 
-  ALOGD << "InitEncode request";
   codec_mode_ = codec_settings->mode;
+  int init_width = codec_settings->width;
+  int init_height = codec_settings->height;
   scale_ = (codecType_ != kVideoCodecVP9) && (webrtc::field_trial::FindFullName(
         "WebRTC-MediaCodecVideoEncoder-AutomaticResize") == "Enabled");
+
+  ALOGD << "InitEncode request: " << init_width << " x " << init_height;
   ALOGD << "Encoder automatic resize " << (scale_ ? "enabled" : "disabled");
+
   if (scale_) {
     if (codecType_ == kVideoCodecVP8) {
       // QP is obtained from VP8-bitstream for HW, so the QP corresponds to the
       // (internal) range: [0, 127]. And we cannot change QP_max in HW, so it is
       // always = 127. Note that in SW, QP is that of the user-level range [0,
       // 63].
-      const int kMaxQp = 127;
-      const int kBadQpThreshold = 95;
-      quality_scaler_.Init(
-          kMaxQp / kLowQpThresholdDenominator, kBadQpThreshold, false);
+      const int kLowQpThreshold = 32;
+      const int kBadQpThreshold = 92;
+      quality_scaler_.Init(kLowQpThreshold, kBadQpThreshold, false,
+          codec_settings->startBitrate,
+          codec_settings->width, codec_settings->height);
     } else if (codecType_ == kVideoCodecH264) {
       // H264 QP is in the range [0, 51].
-      const int kMaxQp = 51;
+      const int kLowQpThreshold = 17;
       const int kBadQpThreshold = 40;
-      quality_scaler_.Init(
-          kMaxQp / kLowQpThresholdDenominator, kBadQpThreshold, false);
+      quality_scaler_.Init(kLowQpThreshold, kBadQpThreshold, false,
+          codec_settings->startBitrate,
+          codec_settings->width, codec_settings->height);
     } else {
       // When adding codec support to additional hardware codecs, also configure
       // their QP thresholds for scaling.
       RTC_NOTREACHED() << "Unsupported codec without configured QP thresholds.";
+      scale_ = false;
     }
     quality_scaler_.SetMinResolution(kMinWidth, kMinHeight);
     quality_scaler_.ReportFramerate(codec_settings->maxFramerate);
+    QualityScaler::Resolution res = quality_scaler_.GetScaledResolution();
+    init_width = std::max(res.width, kMinWidth);
+    init_height = std::max(res.height, kMinHeight);
+    ALOGD << "Scaled resolution: " << init_width << " x " << init_height;
   }
+
   return codec_thread_->Invoke<int32_t>(
       Bind(&MediaCodecVideoEncoder::InitEncodeOnCodecThread,
            this,
-           codec_settings->width,
-           codec_settings->height,
+           init_width,
+           init_height,
            codec_settings->startBitrate,
            codec_settings->maxFramerate,
            false /* use_surface */));