Initial wireup of VideoBitrateAllocation in VaapiVideoEncodeAccelerator.

This is a first step just to get the allocation signal down into the
VP8 encoder. Temporal layer handling will follow upcoming CLs.

Bug: chromium:794608
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:mac_optional_gpu_tests_rel;luci.chromium.try:win_optional_gpu_tests_rel
Change-Id: Ie0a120a7b906dc5218834e355d3acf8c4f24ac36
Reviewed-on: https://chromium-review.googlesource.com/1065898
Commit-Queue: Erik Språng <sprang@chromium.org>
Reviewed-by: Pawel Osciak <posciak@chromium.org>
Reviewed-by: Dale Curtis <dalecurtis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#561853}
diff --git a/media/base/video_bitrate_allocation.cc b/media/base/video_bitrate_allocation.cc
index 629092e..4c5cdf9 100644
--- a/media/base/video_bitrate_allocation.cc
+++ b/media/base/video_bitrate_allocation.cc
@@ -51,4 +51,9 @@
   return sum;
 }
 
+bool VideoBitrateAllocation::operator==(
+    const VideoBitrateAllocation& other) const {
+  return memcmp(bitrates_, other.bitrates_, sizeof(bitrates_)) == 0;
+}
+
 }  // namespace media
diff --git a/media/base/video_bitrate_allocation.h b/media/base/video_bitrate_allocation.h
index 6341ce3..46d142a 100644
--- a/media/base/video_bitrate_allocation.h
+++ b/media/base/video_bitrate_allocation.h
@@ -33,9 +33,13 @@
   // Sum of all bitrates.
   int32_t GetSumBps() const;
 
+  bool operator==(const VideoBitrateAllocation& other) const;
+  inline bool operator!=(const VideoBitrateAllocation& other) const {
+    return !(*this == other);
+  }
+
  private:
   int bitrates_[kMaxSpatialLayers][kMaxTemporalLayers];
-  DISALLOW_COPY_AND_ASSIGN(VideoBitrateAllocation);
 };
 
 }  // namespace media
diff --git a/media/base/video_bitrate_allocation_unittest.cc b/media/base/video_bitrate_allocation_unittest.cc
index ce9e8ac..df8b3ae 100644
--- a/media/base/video_bitrate_allocation_unittest.cc
+++ b/media/base/video_bitrate_allocation_unittest.cc
@@ -67,4 +67,17 @@
   EXPECT_EQ(std::numeric_limits<int>::max() - 1, allocation.GetSumBps());
 }
 
+TEST(VideoBitrateAllocationTest, CanCopyAndCompare) {
+  VideoBitrateAllocation allocation;
+  EXPECT_TRUE(allocation.SetBitrate(0, 0, 1000));
+  EXPECT_TRUE(allocation.SetBitrate(
+      VideoBitrateAllocation::kMaxSpatialLayers - 1,
+      VideoBitrateAllocation::kMaxTemporalLayers - 1, 2000));
+
+  VideoBitrateAllocation copy = allocation;
+  EXPECT_EQ(copy, allocation);
+  copy.SetBitrate(0, 0, 0);
+  EXPECT_NE(copy, allocation);
+}
+
 }  // namespace media
diff --git a/media/gpu/vaapi/accelerated_video_encoder.h b/media/gpu/vaapi/accelerated_video_encoder.h
index 3564b24..c823104 100644
--- a/media/gpu/vaapi/accelerated_video_encoder.h
+++ b/media/gpu/vaapi/accelerated_video_encoder.h
@@ -12,6 +12,7 @@
 #include "base/macros.h"
 #include "base/memory/ref_counted.h"
 #include "base/time/time.h"
+#include "media/base/video_bitrate_allocation.h"
 #include "media/base/video_codecs.h"
 #include "media/gpu/codec_picture.h"
 #include "ui/gfx/geometry/size.h"
@@ -122,8 +123,9 @@
                           uint32_t initial_framerate) = 0;
 
   // Updates current framerate and/or bitrate to |framerate| in FPS
-  // and |bitrate| in bps.
-  virtual bool UpdateRates(uint32_t bitrate, uint32_t framerate) = 0;
+  // and the specified video bitrate allocation.
+  virtual bool UpdateRates(const VideoBitrateAllocation& bitrate_allocation,
+                           uint32_t framerate) = 0;
 
   // Returns coded size for the input buffers required to encode, in pixels;
   // typically visible size adjusted to match codec alignment requirements.
diff --git a/media/gpu/vaapi/h264_encoder.cc b/media/gpu/vaapi/h264_encoder.cc
index d865ac8..1dae058 100644
--- a/media/gpu/vaapi/h264_encoder.cc
+++ b/media/gpu/vaapi/h264_encoder.cc
@@ -88,7 +88,9 @@
   mb_height_ = coded_size_.height() / kH264MacroblockSizeInPixels;
 
   profile_ = profile;
-  if (!UpdateRates(initial_bitrate, initial_framerate))
+  VideoBitrateAllocation initial_bitrate_allocation;
+  initial_bitrate_allocation.SetBitrate(0, 0, initial_bitrate);
+  if (!UpdateRates(initial_bitrate_allocation, initial_framerate))
     return false;
 
   UpdateSPS();
@@ -191,9 +193,11 @@
   return true;
 }
 
-bool H264Encoder::UpdateRates(uint32_t bitrate, uint32_t framerate) {
+bool H264Encoder::UpdateRates(const VideoBitrateAllocation& bitrate_allocation,
+                              uint32_t framerate) {
   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
 
+  uint32_t bitrate = bitrate_allocation.GetSumBps();
   if (bitrate == 0 || framerate == 0)
     return false;
 
diff --git a/media/gpu/vaapi/h264_encoder.h b/media/gpu/vaapi/h264_encoder.h
index 83e9613..a86ee42 100644
--- a/media/gpu/vaapi/h264_encoder.h
+++ b/media/gpu/vaapi/h264_encoder.h
@@ -99,7 +99,8 @@
                   VideoCodecProfile profile,
                   uint32_t initial_bitrate,
                   uint32_t initial_framerate) override;
-  bool UpdateRates(uint32_t bitrate, uint32_t framerate) override;
+  bool UpdateRates(const VideoBitrateAllocation& bitrate_allocation,
+                   uint32_t framerate) override;
   gfx::Size GetCodedSize() const override;
   size_t GetBitstreamBufferSize() const override;
   size_t GetMaxNumOfRefFrames() const override;
diff --git a/media/gpu/vaapi/vaapi_video_encode_accelerator.cc b/media/gpu/vaapi/vaapi_video_encode_accelerator.cc
index a1ebebe..0fc47f46 100644
--- a/media/gpu/vaapi/vaapi_video_encode_accelerator.cc
+++ b/media/gpu/vaapi/vaapi_video_encode_accelerator.cc
@@ -25,6 +25,7 @@
 #include "base/threading/thread_task_runner_handle.h"
 #include "base/trace_event/trace_event.h"
 #include "media/base/bind_to_current_loop.h"
+#include "media/base/video_bitrate_allocation.h"
 #include "media/gpu/h264_dpb.h"
 #include "media/gpu/shared_memory_region.h"
 #include "media/gpu/vaapi/h264_encoder.h"
@@ -571,22 +572,41 @@
   VLOGF(2) << "bitrate: " << bitrate << " framerate: " << framerate;
   DCHECK(child_task_runner_->BelongsToCurrentThread());
 
+  VideoBitrateAllocation allocation;
+  allocation.SetBitrate(0, 0, bitrate);
   encoder_thread_task_runner_->PostTask(
       FROM_HERE,
       base::Bind(
           &VaapiVideoEncodeAccelerator::RequestEncodingParametersChangeTask,
-          base::Unretained(this), bitrate, framerate));
+          base::Unretained(this), allocation, framerate));
+}
+
+void VaapiVideoEncodeAccelerator::RequestEncodingParametersChange(
+    const VideoBitrateAllocation& bitrate_allocation,
+    uint32_t framerate) {
+  VLOGF(2) << "bitrate: " << bitrate_allocation.GetSumBps()
+           << " framerate: " << framerate;
+  DCHECK(child_task_runner_->BelongsToCurrentThread());
+
+  encoder_thread_task_runner_->PostTask(
+      FROM_HERE,
+      base::Bind(
+          &VaapiVideoEncodeAccelerator::RequestEncodingParametersChangeTask,
+          base::Unretained(this), bitrate_allocation, framerate));
 }
 
 void VaapiVideoEncodeAccelerator::RequestEncodingParametersChangeTask(
-    uint32_t bitrate,
+    VideoBitrateAllocation bitrate_allocation,
     uint32_t framerate) {
-  VLOGF(2) << "bitrate: " << bitrate << " framerate: " << framerate;
+  VLOGF(2) << "bitrate: " << bitrate_allocation.GetSumBps()
+           << " framerate: " << framerate;
   DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
   DCHECK_NE(state_, kUninitialized);
 
-  if (!encoder_->UpdateRates(bitrate, framerate))
-    VLOGF(1) << "Failed to update rates to " << bitrate << " " << framerate;
+  if (!encoder_->UpdateRates(bitrate_allocation, framerate)) {
+    VLOGF(1) << "Failed to update rates to " << bitrate_allocation.GetSumBps()
+             << " " << framerate;
+  }
 }
 
 void VaapiVideoEncodeAccelerator::Flush(FlushCallback flush_callback) {
@@ -938,7 +958,7 @@
   seq_param.frame_width_scale = frame_header->horizontal_scale;
   seq_param.frame_height_scale = frame_header->vertical_scale;
   seq_param.error_resilient = 1;
-  seq_param.bits_per_second = encode_params.bitrate_bps;
+  seq_param.bits_per_second = encode_params.bitrate_allocation.GetSumBps();
   seq_param.intra_period = encode_params.kf_period_frames;
 
   VAEncPictureParameterBufferVP8 pic_param = {};
@@ -1035,7 +1055,8 @@
       frame_header->quantization_hdr.uv_ac_delta;
 
   VAEncMiscParameterRateControl rate_control_param = {};
-  rate_control_param.bits_per_second = encode_params.bitrate_bps;
+  rate_control_param.bits_per_second =
+      encode_params.bitrate_allocation.GetSumBps();
   rate_control_param.target_percentage = kTargetBitratePercentage;
   rate_control_param.window_size = encode_params.cpb_window_size_ms;
   rate_control_param.initial_qp = encode_params.initial_qp;
diff --git a/media/gpu/vaapi/vaapi_video_encode_accelerator.h b/media/gpu/vaapi/vaapi_video_encode_accelerator.h
index bcd4fcc..f2e8ee82 100644
--- a/media/gpu/vaapi/vaapi_video_encode_accelerator.h
+++ b/media/gpu/vaapi/vaapi_video_encode_accelerator.h
@@ -44,6 +44,9 @@
   void UseOutputBitstreamBuffer(const BitstreamBuffer& buffer) override;
   void RequestEncodingParametersChange(uint32_t bitrate,
                                        uint32_t framerate) override;
+  void RequestEncodingParametersChange(
+      const VideoBitrateAllocation& bitrate_allocation,
+      uint32_t framerate) override;
   void Destroy() override;
   void Flush(FlushCallback flush_callback) override;
 
@@ -80,8 +83,10 @@
   void UseOutputBitstreamBufferTask(
       std::unique_ptr<BitstreamBufferRef> buffer_ref);
 
-  void RequestEncodingParametersChangeTask(uint32_t bitrate,
-                                           uint32_t framerate);
+  void RequestEncodingParametersChangeTask(
+      VideoBitrateAllocation bitrate_allocation,
+      uint32_t framerate);
+
   void DestroyTask();
   void FlushTask();
 
diff --git a/media/gpu/vaapi/vp8_encoder.cc b/media/gpu/vaapi/vp8_encoder.cc
index a7b8cd41..2463f23 100644
--- a/media/gpu/vaapi/vp8_encoder.cc
+++ b/media/gpu/vaapi/vp8_encoder.cc
@@ -25,7 +25,6 @@
 
 VP8Encoder::EncodeParams::EncodeParams()
     : kf_period_frames(kKFPeriod),
-      bitrate_bps(0),
       framerate(0),
       cpb_window_size_ms(kCPBWindowSizeMs),
       cpb_size_bits(0),
@@ -67,7 +66,9 @@
 
   Reset();
 
-  return UpdateRates(initial_bitrate, initial_framerate);
+  VideoBitrateAllocation initial_bitrate_allocation;
+  initial_bitrate_allocation.SetBitrate(0, 0, initial_bitrate);
+  return UpdateRates(initial_bitrate_allocation, initial_framerate);
 }
 
 gfx::Size VP8Encoder::GetCodedSize() const {
@@ -118,22 +119,24 @@
   return true;
 }
 
-bool VP8Encoder::UpdateRates(uint32_t bitrate, uint32_t framerate) {
+bool VP8Encoder::UpdateRates(const VideoBitrateAllocation& bitrate_allocation,
+                             uint32_t framerate) {
   DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
 
-  if (bitrate == 0 || framerate == 0)
+  if (bitrate_allocation.GetSumBps() == 0 || framerate == 0)
     return false;
 
-  if (current_params_.bitrate_bps == bitrate &&
+  if (current_params_.bitrate_allocation == bitrate_allocation &&
       current_params_.framerate == framerate) {
     return true;
   }
 
-  current_params_.bitrate_bps = bitrate;
+  current_params_.bitrate_allocation = bitrate_allocation;
   current_params_.framerate = framerate;
 
   current_params_.cpb_size_bits =
-      current_params_.bitrate_bps * current_params_.cpb_window_size_ms / 1000;
+      current_params_.bitrate_allocation.GetSumBps() *
+      current_params_.cpb_window_size_ms / 1000;
 
   return true;
 }
diff --git a/media/gpu/vaapi/vp8_encoder.h b/media/gpu/vaapi/vp8_encoder.h
index 66abde9..a5f9e5e 100644
--- a/media/gpu/vaapi/vp8_encoder.h
+++ b/media/gpu/vaapi/vp8_encoder.h
@@ -10,6 +10,7 @@
 
 #include "base/macros.h"
 #include "base/sequence_checker.h"
+#include "media/base/video_bitrate_allocation.h"
 #include "media/filters/vp8_parser.h"
 #include "media/gpu/vaapi/accelerated_video_encoder.h"
 #include "media/gpu/vp8_picture.h"
@@ -25,8 +26,8 @@
     // Produce a keyframe at least once per this many frames.
     size_t kf_period_frames;
 
-    // Bitrate in bps.
-    uint32_t bitrate_bps;
+    // Bitrate allocation in bps.
+    VideoBitrateAllocation bitrate_allocation;
 
     // Framerate in FPS.
     uint32_t framerate;
@@ -75,7 +76,8 @@
                   VideoCodecProfile profile,
                   uint32_t initial_bitrate,
                   uint32_t initial_framerate) override;
-  bool UpdateRates(uint32_t bitrate, uint32_t framerate) override;
+  bool UpdateRates(const VideoBitrateAllocation& bitrate_allocation,
+                   uint32_t framerate) override;
   gfx::Size GetCodedSize() const override;
   size_t GetBitstreamBufferSize() const override;
   size_t GetMaxNumOfRefFrames() const override;
diff --git a/media/video/video_encode_accelerator.cc b/media/video/video_encode_accelerator.cc
index 1f6c0dc..e31f4ac 100644
--- a/media/video/video_encode_accelerator.cc
+++ b/media/video/video_encode_accelerator.cc
@@ -25,9 +25,9 @@
 }
 
 void VideoEncodeAccelerator::RequestEncodingParametersChange(
-    const VideoBitrateAllocation& bitrate,
+    const VideoBitrateAllocation& bitrate_allocation,
     uint32_t framerate) {
-  RequestEncodingParametersChange(bitrate.GetSumBps(), framerate);
+  RequestEncodingParametersChange(bitrate_allocation.GetSumBps(), framerate);
 }
 
 }  // namespace media