#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "media/gpu/vaapi/vp9_vaapi_video_encoder_delegate.h"
#include <algorithm>
#include <numeric>
#include <va/va.h>
#include "base/bits.h"
#include "base/memory/ref_counted_memory.h"
#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "media/gpu/gpu_video_encode_accelerator_helpers.h"
#include "media/gpu/macros.h"
#include "media/gpu/vaapi/vaapi_common.h"
#include "media/gpu/vaapi/vaapi_wrapper.h"
#include "media/gpu/vp9_svc_layers.h"
#include "third_party/libvpx/source/libvpx/vp9/ratectrl_rtc.h"
namespace media {
namespace {
constexpr size_t kKFPeriod = …;
constexpr uint8_t kMinQP = …;
constexpr uint8_t kMaxQP = …;
constexpr uint8_t kScreenMinQP = …;
constexpr uint8_t kScreenMaxQP = …;
uint8_t QindexToQuantizer(uint8_t q_index) { … }
int GCD(int a, int b) { … }
uint32_t MaxSizeOfKeyframeAsPercentage(uint32_t optimal_buffer_size,
uint32_t max_framerate) { … }
libvpx::VP9RateControlRtcConfig CreateRateControlConfig(
const VP9VaapiVideoEncoderDelegate::EncodeParams& encode_params,
const VideoBitrateAllocation& bitrate_allocation,
const size_t num_temporal_layers,
const std::vector<gfx::Size>& spatial_layer_resolutions) { … }
scoped_refptr<VP9Picture> GetVP9Picture(
const VaapiVideoEncoderDelegate::EncodeJob& job) { … }
bool ValidateBitrates(const VideoBitrateAllocation& bitrate_allocation,
size_t begin_active_spatial_layer,
size_t end_active_spatial_layer,
size_t num_temporal_layers) { … }
bool ValidateAndGetActiveLayers(
const VideoBitrateAllocation& bitrate_allocation,
size_t& begin_active_spatial_layer,
size_t& end_active_spatial_layer,
size_t& num_temporal_layers) { … }
}
std::unique_ptr<VP9RateControlWrapper> VP9RateControlWrapper::Create(
const libvpx::VP9RateControlRtcConfig& config) { … }
VP9RateControlWrapper::VP9RateControlWrapper() = default;
VP9RateControlWrapper::VP9RateControlWrapper(
std::unique_ptr<libvpx::VP9RateControlRTC> impl)
: … { … }
void VP9RateControlWrapper::UpdateRateControl(
const libvpx::VP9RateControlRtcConfig& rate_control_config) { … }
VP9RateControlWrapper::~VP9RateControlWrapper() = default;
libvpx::FrameDropDecision VP9RateControlWrapper::ComputeQP(
const libvpx::VP9FrameParamsQpRTC& frame_params) { … }
int VP9RateControlWrapper::GetQP() const { … }
void VP9RateControlWrapper::PostEncodeUpdate(
uint64_t encoded_frame_size,
const libvpx::VP9FrameParamsQpRTC& frame_params) { … }
int VP9RateControlWrapper::GetLoopfilterLevel() const { … }
VP9VaapiVideoEncoderDelegate::EncodeParams::EncodeParams()
: … { … }
void VP9VaapiVideoEncoderDelegate::set_rate_ctrl_for_testing(
std::unique_ptr<VP9RateControlWrapper> rate_ctrl) { … }
VP9VaapiVideoEncoderDelegate::VP9VaapiVideoEncoderDelegate(
scoped_refptr<VaapiWrapper> vaapi_wrapper,
base::RepeatingClosure error_cb)
: … { … }
VP9VaapiVideoEncoderDelegate::~VP9VaapiVideoEncoderDelegate() = default;
bool VP9VaapiVideoEncoderDelegate::Initialize(
const VideoEncodeAccelerator::Config& config,
const VaapiVideoEncoderDelegate::Config& ave_config) { … }
gfx::Size VP9VaapiVideoEncoderDelegate::GetCodedSize() const { … }
size_t VP9VaapiVideoEncoderDelegate::GetMaxNumOfRefFrames() const { … }
VaapiVideoEncoderDelegate::PrepareEncodeJobResult
VP9VaapiVideoEncoderDelegate::PrepareEncodeJob(EncodeJob& encode_job) { … }
BitstreamBufferMetadata VP9VaapiVideoEncoderDelegate::GetMetadata(
const EncodeJob& encode_job,
size_t payload_size) { … }
std::vector<gfx::Size> VP9VaapiVideoEncoderDelegate::GetSVCLayerResolutions() { … }
void VP9VaapiVideoEncoderDelegate::BitrateControlUpdate(
const BitstreamBufferMetadata& metadata) { … }
bool VP9VaapiVideoEncoderDelegate::RecreateSVCLayersIfNeeded(
VideoBitrateAllocation& bitrate_allocation) { … }
bool VP9VaapiVideoEncoderDelegate::ApplyPendingUpdateRates() { … }
bool VP9VaapiVideoEncoderDelegate::UpdateRates(
const VideoBitrateAllocation& bitrate_allocation,
uint32_t framerate) { … }
Vp9FrameHeader VP9VaapiVideoEncoderDelegate::GetDefaultFrameHeader(
const bool keyframe) const { … }
VaapiVideoEncoderDelegate::PrepareEncodeJobResult
VP9VaapiVideoEncoderDelegate::SetFrameHeader(
bool keyframe,
VP9Picture* picture,
std::array<bool, kVp9NumRefsPerFrame>* ref_frames_used) { … }
void VP9VaapiVideoEncoderDelegate::UpdateReferenceFrames(
scoped_refptr<VP9Picture> picture) { … }
bool VP9VaapiVideoEncoderDelegate::SubmitFrameParameters(
EncodeJob& job,
const EncodeParams& encode_params,
scoped_refptr<VP9Picture> pic,
const Vp9ReferenceFrameVector& ref_frames,
const std::array<bool, kVp9NumRefsPerFrame>& ref_frames_used) { … }
}