Commit eed0f181 authored by Erik Språng's avatar Erik Språng Committed by Commit Bot

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: default avatarPawel Osciak <posciak@chromium.org>
Reviewed-by: default avatarDale Curtis <dalecurtis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#561853}
parent 60845387
...@@ -51,4 +51,9 @@ int VideoBitrateAllocation::GetSumBps() const { ...@@ -51,4 +51,9 @@ int VideoBitrateAllocation::GetSumBps() const {
return sum; return sum;
} }
bool VideoBitrateAllocation::operator==(
const VideoBitrateAllocation& other) const {
return memcmp(bitrates_, other.bitrates_, sizeof(bitrates_)) == 0;
}
} // namespace media } // namespace media
...@@ -33,9 +33,13 @@ class MEDIA_EXPORT VideoBitrateAllocation { ...@@ -33,9 +33,13 @@ class MEDIA_EXPORT VideoBitrateAllocation {
// Sum of all bitrates. // Sum of all bitrates.
int32_t GetSumBps() const; int32_t GetSumBps() const;
bool operator==(const VideoBitrateAllocation& other) const;
inline bool operator!=(const VideoBitrateAllocation& other) const {
return !(*this == other);
}
private: private:
int bitrates_[kMaxSpatialLayers][kMaxTemporalLayers]; int bitrates_[kMaxSpatialLayers][kMaxTemporalLayers];
DISALLOW_COPY_AND_ASSIGN(VideoBitrateAllocation);
}; };
} // namespace media } // namespace media
......
...@@ -67,4 +67,17 @@ TEST(VideoBitrateAllocationTest, ValidatesSumWhenOverwriting) { ...@@ -67,4 +67,17 @@ TEST(VideoBitrateAllocationTest, ValidatesSumWhenOverwriting) {
EXPECT_EQ(std::numeric_limits<int>::max() - 1, allocation.GetSumBps()); 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 } // namespace media
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "media/base/video_bitrate_allocation.h"
#include "media/base/video_codecs.h" #include "media/base/video_codecs.h"
#include "media/gpu/codec_picture.h" #include "media/gpu/codec_picture.h"
#include "ui/gfx/geometry/size.h" #include "ui/gfx/geometry/size.h"
...@@ -122,8 +123,9 @@ class AcceleratedVideoEncoder { ...@@ -122,8 +123,9 @@ class AcceleratedVideoEncoder {
uint32_t initial_framerate) = 0; uint32_t initial_framerate) = 0;
// Updates current framerate and/or bitrate to |framerate| in FPS // Updates current framerate and/or bitrate to |framerate| in FPS
// and |bitrate| in bps. // and the specified video bitrate allocation.
virtual bool UpdateRates(uint32_t bitrate, uint32_t framerate) = 0; virtual bool UpdateRates(const VideoBitrateAllocation& bitrate_allocation,
uint32_t framerate) = 0;
// Returns coded size for the input buffers required to encode, in pixels; // Returns coded size for the input buffers required to encode, in pixels;
// typically visible size adjusted to match codec alignment requirements. // typically visible size adjusted to match codec alignment requirements.
......
...@@ -88,7 +88,9 @@ bool H264Encoder::Initialize(const gfx::Size& visible_size, ...@@ -88,7 +88,9 @@ bool H264Encoder::Initialize(const gfx::Size& visible_size,
mb_height_ = coded_size_.height() / kH264MacroblockSizeInPixels; mb_height_ = coded_size_.height() / kH264MacroblockSizeInPixels;
profile_ = profile; 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; return false;
UpdateSPS(); UpdateSPS();
...@@ -191,9 +193,11 @@ bool H264Encoder::PrepareEncodeJob(EncodeJob* encode_job) { ...@@ -191,9 +193,11 @@ bool H264Encoder::PrepareEncodeJob(EncodeJob* encode_job) {
return true; 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_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
uint32_t bitrate = bitrate_allocation.GetSumBps();
if (bitrate == 0 || framerate == 0) if (bitrate == 0 || framerate == 0)
return false; return false;
......
...@@ -99,7 +99,8 @@ class H264Encoder : public AcceleratedVideoEncoder { ...@@ -99,7 +99,8 @@ class H264Encoder : public AcceleratedVideoEncoder {
VideoCodecProfile profile, VideoCodecProfile profile,
uint32_t initial_bitrate, uint32_t initial_bitrate,
uint32_t initial_framerate) override; 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; gfx::Size GetCodedSize() const override;
size_t GetBitstreamBufferSize() const override; size_t GetBitstreamBufferSize() const override;
size_t GetMaxNumOfRefFrames() const override; size_t GetMaxNumOfRefFrames() const override;
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "base/trace_event/trace_event.h" #include "base/trace_event/trace_event.h"
#include "media/base/bind_to_current_loop.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/h264_dpb.h"
#include "media/gpu/shared_memory_region.h" #include "media/gpu/shared_memory_region.h"
#include "media/gpu/vaapi/h264_encoder.h" #include "media/gpu/vaapi/h264_encoder.h"
...@@ -571,22 +572,41 @@ void VaapiVideoEncodeAccelerator::RequestEncodingParametersChange( ...@@ -571,22 +572,41 @@ void VaapiVideoEncodeAccelerator::RequestEncodingParametersChange(
VLOGF(2) << "bitrate: " << bitrate << " framerate: " << framerate; VLOGF(2) << "bitrate: " << bitrate << " framerate: " << framerate;
DCHECK(child_task_runner_->BelongsToCurrentThread()); DCHECK(child_task_runner_->BelongsToCurrentThread());
VideoBitrateAllocation allocation;
allocation.SetBitrate(0, 0, bitrate);
encoder_thread_task_runner_->PostTask( encoder_thread_task_runner_->PostTask(
FROM_HERE, FROM_HERE,
base::Bind( base::Bind(
&VaapiVideoEncodeAccelerator::RequestEncodingParametersChangeTask, &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( void VaapiVideoEncodeAccelerator::RequestEncodingParametersChangeTask(
uint32_t bitrate, VideoBitrateAllocation bitrate_allocation,
uint32_t framerate) { uint32_t framerate) {
VLOGF(2) << "bitrate: " << bitrate << " framerate: " << framerate; VLOGF(2) << "bitrate: " << bitrate_allocation.GetSumBps()
<< " framerate: " << framerate;
DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread()); DCHECK(encoder_thread_task_runner_->BelongsToCurrentThread());
DCHECK_NE(state_, kUninitialized); DCHECK_NE(state_, kUninitialized);
if (!encoder_->UpdateRates(bitrate, framerate)) if (!encoder_->UpdateRates(bitrate_allocation, framerate)) {
VLOGF(1) << "Failed to update rates to " << bitrate << " " << framerate; VLOGF(1) << "Failed to update rates to " << bitrate_allocation.GetSumBps()
<< " " << framerate;
}
} }
void VaapiVideoEncodeAccelerator::Flush(FlushCallback flush_callback) { void VaapiVideoEncodeAccelerator::Flush(FlushCallback flush_callback) {
...@@ -938,7 +958,7 @@ bool VaapiVideoEncodeAccelerator::VP8Accelerator::SubmitFrameParameters( ...@@ -938,7 +958,7 @@ bool VaapiVideoEncodeAccelerator::VP8Accelerator::SubmitFrameParameters(
seq_param.frame_width_scale = frame_header->horizontal_scale; seq_param.frame_width_scale = frame_header->horizontal_scale;
seq_param.frame_height_scale = frame_header->vertical_scale; seq_param.frame_height_scale = frame_header->vertical_scale;
seq_param.error_resilient = 1; 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; seq_param.intra_period = encode_params.kf_period_frames;
VAEncPictureParameterBufferVP8 pic_param = {}; VAEncPictureParameterBufferVP8 pic_param = {};
...@@ -1035,7 +1055,8 @@ bool VaapiVideoEncodeAccelerator::VP8Accelerator::SubmitFrameParameters( ...@@ -1035,7 +1055,8 @@ bool VaapiVideoEncodeAccelerator::VP8Accelerator::SubmitFrameParameters(
frame_header->quantization_hdr.uv_ac_delta; frame_header->quantization_hdr.uv_ac_delta;
VAEncMiscParameterRateControl rate_control_param = {}; 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.target_percentage = kTargetBitratePercentage;
rate_control_param.window_size = encode_params.cpb_window_size_ms; rate_control_param.window_size = encode_params.cpb_window_size_ms;
rate_control_param.initial_qp = encode_params.initial_qp; rate_control_param.initial_qp = encode_params.initial_qp;
......
...@@ -44,6 +44,9 @@ class MEDIA_GPU_EXPORT VaapiVideoEncodeAccelerator ...@@ -44,6 +44,9 @@ class MEDIA_GPU_EXPORT VaapiVideoEncodeAccelerator
void UseOutputBitstreamBuffer(const BitstreamBuffer& buffer) override; void UseOutputBitstreamBuffer(const BitstreamBuffer& buffer) override;
void RequestEncodingParametersChange(uint32_t bitrate, void RequestEncodingParametersChange(uint32_t bitrate,
uint32_t framerate) override; uint32_t framerate) override;
void RequestEncodingParametersChange(
const VideoBitrateAllocation& bitrate_allocation,
uint32_t framerate) override;
void Destroy() override; void Destroy() override;
void Flush(FlushCallback flush_callback) override; void Flush(FlushCallback flush_callback) override;
...@@ -80,8 +83,10 @@ class MEDIA_GPU_EXPORT VaapiVideoEncodeAccelerator ...@@ -80,8 +83,10 @@ class MEDIA_GPU_EXPORT VaapiVideoEncodeAccelerator
void UseOutputBitstreamBufferTask( void UseOutputBitstreamBufferTask(
std::unique_ptr<BitstreamBufferRef> buffer_ref); std::unique_ptr<BitstreamBufferRef> buffer_ref);
void RequestEncodingParametersChangeTask(uint32_t bitrate, void RequestEncodingParametersChangeTask(
uint32_t framerate); VideoBitrateAllocation bitrate_allocation,
uint32_t framerate);
void DestroyTask(); void DestroyTask();
void FlushTask(); void FlushTask();
......
...@@ -25,7 +25,6 @@ const int kDefaultQP = (3 * kMinQP + kMaxQP) / 4; ...@@ -25,7 +25,6 @@ const int kDefaultQP = (3 * kMinQP + kMaxQP) / 4;
VP8Encoder::EncodeParams::EncodeParams() VP8Encoder::EncodeParams::EncodeParams()
: kf_period_frames(kKFPeriod), : kf_period_frames(kKFPeriod),
bitrate_bps(0),
framerate(0), framerate(0),
cpb_window_size_ms(kCPBWindowSizeMs), cpb_window_size_ms(kCPBWindowSizeMs),
cpb_size_bits(0), cpb_size_bits(0),
...@@ -67,7 +66,9 @@ bool VP8Encoder::Initialize(const gfx::Size& visible_size, ...@@ -67,7 +66,9 @@ bool VP8Encoder::Initialize(const gfx::Size& visible_size,
Reset(); 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 { gfx::Size VP8Encoder::GetCodedSize() const {
...@@ -118,22 +119,24 @@ bool VP8Encoder::PrepareEncodeJob(EncodeJob* encode_job) { ...@@ -118,22 +119,24 @@ bool VP8Encoder::PrepareEncodeJob(EncodeJob* encode_job) {
return true; 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_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (bitrate == 0 || framerate == 0) if (bitrate_allocation.GetSumBps() == 0 || framerate == 0)
return false; return false;
if (current_params_.bitrate_bps == bitrate && if (current_params_.bitrate_allocation == bitrate_allocation &&
current_params_.framerate == framerate) { current_params_.framerate == framerate) {
return true; return true;
} }
current_params_.bitrate_bps = bitrate; current_params_.bitrate_allocation = bitrate_allocation;
current_params_.framerate = framerate; current_params_.framerate = framerate;
current_params_.cpb_size_bits = 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; return true;
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/sequence_checker.h" #include "base/sequence_checker.h"
#include "media/base/video_bitrate_allocation.h"
#include "media/filters/vp8_parser.h" #include "media/filters/vp8_parser.h"
#include "media/gpu/vaapi/accelerated_video_encoder.h" #include "media/gpu/vaapi/accelerated_video_encoder.h"
#include "media/gpu/vp8_picture.h" #include "media/gpu/vp8_picture.h"
...@@ -25,8 +26,8 @@ class VP8Encoder : public AcceleratedVideoEncoder { ...@@ -25,8 +26,8 @@ class VP8Encoder : public AcceleratedVideoEncoder {
// Produce a keyframe at least once per this many frames. // Produce a keyframe at least once per this many frames.
size_t kf_period_frames; size_t kf_period_frames;
// Bitrate in bps. // Bitrate allocation in bps.
uint32_t bitrate_bps; VideoBitrateAllocation bitrate_allocation;
// Framerate in FPS. // Framerate in FPS.
uint32_t framerate; uint32_t framerate;
...@@ -75,7 +76,8 @@ class VP8Encoder : public AcceleratedVideoEncoder { ...@@ -75,7 +76,8 @@ class VP8Encoder : public AcceleratedVideoEncoder {
VideoCodecProfile profile, VideoCodecProfile profile,
uint32_t initial_bitrate, uint32_t initial_bitrate,
uint32_t initial_framerate) override; 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; gfx::Size GetCodedSize() const override;
size_t GetBitstreamBufferSize() const override; size_t GetBitstreamBufferSize() const override;
size_t GetMaxNumOfRefFrames() const override; size_t GetMaxNumOfRefFrames() const override;
......
...@@ -25,9 +25,9 @@ void VideoEncodeAccelerator::Flush(FlushCallback flush_callback) { ...@@ -25,9 +25,9 @@ void VideoEncodeAccelerator::Flush(FlushCallback flush_callback) {
} }
void VideoEncodeAccelerator::RequestEncodingParametersChange( void VideoEncodeAccelerator::RequestEncodingParametersChange(
const VideoBitrateAllocation& bitrate, const VideoBitrateAllocation& bitrate_allocation,
uint32_t framerate) { uint32_t framerate) {
RequestEncodingParametersChange(bitrate.GetSumBps(), framerate); RequestEncodingParametersChange(bitrate_allocation.GetSumBps(), framerate);
} }
} // namespace media } // namespace media
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment