#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "media/gpu/vaapi/h264_vaapi_video_encoder_delegate.h"
#include <va/va.h>
#include <va/va_enc_h264.h>
#include <climits>
#include <utility>
#include "base/bits.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted_memory.h"
#include "base/numerics/checked_math.h"
#include "build/build_config.h"
#include "media/base/media_switches.h"
#include "media/base/video_bitrate_allocation.h"
#include "media/gpu/gpu_video_encode_accelerator_helpers.h"
#include "media/gpu/h264_builder.h"
#include "media/gpu/macros.h"
#include "media/gpu/vaapi/vaapi_common.h"
#include "media/gpu/vaapi/vaapi_wrapper.h"
#include "media/parsers/h264_level_limits.h"
#include "media/video/video_encode_accelerator.h"
namespace media {
namespace {
constexpr uint32_t kIDRPeriod = …;
static_assert …;
constexpr uint32_t kIPeriod = …;
constexpr uint32_t kIPPeriod = …;
constexpr uint8_t kDefaultQP = …;
constexpr uint8_t kMinQP = …;
constexpr uint8_t kScreenMinQP = …;
constexpr uint8_t kMaxQP = …;
constexpr uint32_t kCPBWindowSizeMs = …;
constexpr size_t kMaxRefIdxL0Size = …;
constexpr int kBitRateScale = …;
constexpr int kCPBSizeScale = …;
constexpr int kChromaFormatIDC = …;
constexpr uint8_t kMinSupportedH264TemporalLayers = …;
constexpr uint8_t kMaxSupportedH264TemporalLayers = …;
template <typename VAEncMiscParam>
VAEncMiscParam& AllocateMiscParameterBuffer(
std::vector<uint8_t>& misc_buffer,
VAEncMiscParameterType misc_param_type) { … }
void CreateVAEncRateControlParams(uint32_t bps,
uint32_t target_percentage,
uint32_t window_size,
uint32_t initial_qp,
uint32_t min_qp,
uint32_t max_qp,
uint32_t framerate,
uint32_t buffer_size,
std::vector<uint8_t> misc_buffers[3]) { … }
static void InitVAPictureH264(VAPictureH264* va_pic) { … }
void UpdateAndSetFrameNum(H264Picture& pic, unsigned int& frame_num) { … }
void UpdatePictureForTemporalLayerEncoding(
const size_t num_layers,
H264Picture& pic,
unsigned int& frame_num,
std::optional<size_t>& ref_frame_idx,
const unsigned int num_encoded_frames,
const base::circular_deque<scoped_refptr<H264Picture>>& ref_pic_list0) { … }
scoped_refptr<H264Picture> GetH264Picture(
const VaapiVideoEncoderDelegate::EncodeJob& job) { … }
std::optional<H264RateControlConfigRTC> CreateRateControlConfig(
const gfx::Size encode_size,
const H264VaapiVideoEncoderDelegate::EncodeParams& encode_params,
const VideoBitrateAllocation& bitrate_allocation,
const size_t& num_temporal_layers) { … }
}
std::unique_ptr<H264RateControlWrapper> H264RateControlWrapper::Create(
const H264RateControlConfigRTC& config) { … }
H264RateControlWrapper::H264RateControlWrapper() = default;
H264RateControlWrapper::H264RateControlWrapper(
std::unique_ptr<H264RateCtrlRTC> impl)
: … { … }
H264RateControlWrapper::~H264RateControlWrapper() = default;
void H264RateControlWrapper::UpdateRateControl(
const H264RateControlConfigRTC& config) { … }
H264RateCtrlRTC::FrameDropDecision H264RateControlWrapper::ComputeQP(
const H264FrameParamsRTC& frame_params) { … }
int H264RateControlWrapper::GetQP() const { … }
void H264RateControlWrapper::PostEncodeUpdate(
uint64_t encoded_frame_size,
const H264FrameParamsRTC& frame_params) { … }
H264VaapiVideoEncoderDelegate::EncodeParams::EncodeParams()
: … { … }
H264VaapiVideoEncoderDelegate::H264VaapiVideoEncoderDelegate(
scoped_refptr<VaapiWrapper> vaapi_wrapper,
base::RepeatingClosure error_cb)
: … { … }
H264VaapiVideoEncoderDelegate::~H264VaapiVideoEncoderDelegate() = default;
void H264VaapiVideoEncoderDelegate::set_rate_ctrl_for_testing(
std::unique_ptr<H264RateControlWrapper> rate_ctrl) { … }
bool H264VaapiVideoEncoderDelegate::Initialize(
const VideoEncodeAccelerator::Config& config,
const VaapiVideoEncoderDelegate::Config& ave_config) { … }
gfx::Size H264VaapiVideoEncoderDelegate::GetCodedSize() const { … }
size_t H264VaapiVideoEncoderDelegate::GetMaxNumOfRefFrames() const { … }
std::vector<gfx::Size> H264VaapiVideoEncoderDelegate::GetSVCLayerResolutions() { … }
bool H264VaapiVideoEncoderDelegate::UseSoftwareRateController(
const VideoEncodeAccelerator::Config& config) { … }
BitstreamBufferMetadata H264VaapiVideoEncoderDelegate::GetMetadata(
const EncodeJob& encode_job,
size_t payload_size) { … }
VaapiVideoEncoderDelegate::PrepareEncodeJobResult
H264VaapiVideoEncoderDelegate::PrepareEncodeJob(EncodeJob& encode_job) { … }
bool H264VaapiVideoEncoderDelegate::UpdateRates(
const VideoBitrateAllocation& bitrate_allocation,
uint32_t framerate) { … }
void H264VaapiVideoEncoderDelegate::UpdateSPS() { … }
void H264VaapiVideoEncoderDelegate::UpdatePPS() { … }
void H264VaapiVideoEncoderDelegate::GeneratePackedSliceHeader(
H26xAnnexBBitstreamBuilder& packed_slice_header,
const VAEncPictureParameterBufferH264& pic_param,
const VAEncSliceParameterBufferH264& slice_param,
const H264Picture& pic) { … }
bool H264VaapiVideoEncoderDelegate::SubmitFrameParameters(
EncodeJob& job,
const H264VaapiVideoEncoderDelegate::EncodeParams& encode_params,
const H264SPS& sps,
const H264PPS& pps,
scoped_refptr<H264Picture> pic,
const base::circular_deque<scoped_refptr<H264Picture>>& ref_pic_list0,
const std::optional<size_t>& ref_frame_index,
const std::optional<int>& qp) { … }
bool H264VaapiVideoEncoderDelegate::SubmitPackedHeaders(
const H26xAnnexBBitstreamBuilder& packed_sps,
const H26xAnnexBBitstreamBuilder& packed_pps) { … }
void H264VaapiVideoEncoderDelegate::BitrateControlUpdate(
const BitstreamBufferMetadata& metadata) { … }
}