#include "media/gpu/vaapi/vaapi_video_encode_accelerator.h"
#include <string.h>
#include <va/va.h>
#include <algorithm>
#include <memory>
#include <type_traits>
#include <utility>
#include <variant>
#include "base/bits.h"
#include "base/containers/contains.h"
#include "base/containers/fixed_flat_map.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/memory/unsafe_shared_memory_region.h"
#include "base/numerics/safe_conversions.h"
#include "base/ranges/algorithm.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
#include "media/base/format_utils.h"
#include "media/base/media_log.h"
#include "media/base/media_switches.h"
#include "media/base/platform_features.h"
#include "media/base/video_bitrate_allocation.h"
#include "media/gpu/chromeos/platform_video_frame_utils.h"
#include "media/gpu/gpu_video_encode_accelerator_helpers.h"
#include "media/gpu/h264_dpb.h"
#include "media/gpu/macros.h"
#include "media/gpu/vaapi/av1_vaapi_video_encoder_delegate.h"
#include "media/gpu/vaapi/h264_vaapi_video_encoder_delegate.h"
#include "media/gpu/vaapi/vaapi_common.h"
#include "media/gpu/vaapi/vaapi_utils.h"
#include "media/gpu/vaapi/vaapi_wrapper.h"
#include "media/gpu/vaapi/vp8_vaapi_video_encoder_delegate.h"
#include "media/gpu/vaapi/vp9_vaapi_video_encoder_delegate.h"
#include "media/gpu/vp8_reference_frame_vector.h"
#include "media/gpu/vp9_reference_frame_vector.h"
namespace media {
namespace {
constexpr size_t kMinNumFramesInFlight = …;
constexpr unsigned int kVaSurfaceFormat = …;
std::unique_ptr<ScopedVASurface> CreateScopedSurface(
VaapiWrapper& vaapi_wrapper,
const gfx::Size& encode_size,
const std::vector<VaapiWrapper::SurfaceUsageHint>& surface_usage_hints) { … }
}
struct VaapiVideoEncodeAccelerator::InputFrameRef { … };
class VaapiVideoEncodeAccelerator::ScopedVASurfaceWrapper { … };
base::AtomicRefCount VaapiVideoEncodeAccelerator::num_instances_(0);
VideoEncodeAccelerator::SupportedProfiles
VaapiVideoEncodeAccelerator::GetSupportedProfiles() { … }
VaapiVideoEncodeAccelerator::VaapiVideoEncodeAccelerator()
: … { … }
VaapiVideoEncodeAccelerator::~VaapiVideoEncodeAccelerator() { … }
bool VaapiVideoEncodeAccelerator::Initialize(
const Config& config,
Client* client,
std::unique_ptr<MediaLog> media_log) { … }
void VaapiVideoEncodeAccelerator::InitializeTask(const Config& config) { … }
void VaapiVideoEncodeAccelerator::RecycleInputScopedVASurface(
const gfx::Size& encode_size,
std::unique_ptr<ScopedVASurface> va_surface) { … }
void VaapiVideoEncodeAccelerator::RecycleEncodeScopedVASurface(
const gfx::Size& encode_size,
std::unique_ptr<ScopedVASurface> va_surface) { … }
void VaapiVideoEncodeAccelerator::TryToReturnBitstreamBuffers() { … }
void VaapiVideoEncodeAccelerator::ReturnBitstreamBuffer(
const EncodeResult& encode_result,
const BitstreamBuffer& buffer) { … }
void VaapiVideoEncodeAccelerator::Encode(scoped_refptr<VideoFrame> frame,
bool force_keyframe) { … }
void VaapiVideoEncodeAccelerator::EncodeTask(scoped_refptr<VideoFrame> frame,
bool force_keyframe) { … }
bool VaapiVideoEncodeAccelerator::CreateSurfacesForGpuMemoryBufferEncoding(
const VideoFrame& frame,
const std::vector<gfx::Size>& spatial_layer_resolutions,
std::vector<std::unique_ptr<ScopedVASurfaceWrapper>>* input_surfaces,
std::vector<std::unique_ptr<ScopedVASurfaceWrapper>>*
reconstructed_surfaces) { … }
bool VaapiVideoEncodeAccelerator::CreateSurfacesForShmemEncoding(
const VideoFrame& frame,
std::unique_ptr<ScopedVASurfaceWrapper>* input_surface,
std::unique_ptr<ScopedVASurfaceWrapper>* reconstructed_surface) { … }
std::unique_ptr<VaapiVideoEncodeAccelerator::ScopedVASurfaceWrapper>
VaapiVideoEncodeAccelerator::GetOrCreateInputSurface(
VaapiWrapper& vaapi_wrapper,
const gfx::Size& encode_size,
const std::vector<VaapiWrapper::SurfaceUsageHint>& surface_usage_hints) { … }
std::unique_ptr<VaapiVideoEncodeAccelerator::ScopedVASurfaceWrapper>
VaapiVideoEncodeAccelerator::GetOrCreateReconstructedSurface(
const gfx::Size& encode_size) { … }
scoped_refptr<VaapiWrapper>
VaapiVideoEncodeAccelerator::CreateVppVaapiWrapper() { … }
std::unique_ptr<VaapiVideoEncodeAccelerator::ScopedVASurfaceWrapper>
VaapiVideoEncodeAccelerator::ExecuteBlitSurface(
const ScopedVASurface* source_surface,
const gfx::Rect source_visible_rect,
const gfx::Size& encode_size) { … }
std::unique_ptr<VaapiVideoEncoderDelegate::EncodeJob>
VaapiVideoEncodeAccelerator::CreateEncodeJob(
bool force_keyframe,
base::TimeDelta frame_timestamp,
uint8_t spatial_index,
bool end_of_picture,
VASurfaceID input_surface_id,
std::unique_ptr<ScopedVASurfaceWrapper> reconstructed_surface) { … }
void VaapiVideoEncodeAccelerator::EncodePendingInputs() { … }
void VaapiVideoEncodeAccelerator::UseOutputBitstreamBuffer(
BitstreamBuffer buffer) { … }
void VaapiVideoEncodeAccelerator::UseOutputBitstreamBufferTask(
BitstreamBuffer buffer) { … }
void VaapiVideoEncodeAccelerator::RequestEncodingParametersChange(
const Bitrate& bitrate,
uint32_t framerate,
const std::optional<gfx::Size>& size) { … }
void VaapiVideoEncodeAccelerator::RequestEncodingParametersChange(
const VideoBitrateAllocation& bitrate_allocation,
uint32_t framerate,
const std::optional<gfx::Size>& size) { … }
void VaapiVideoEncodeAccelerator::RequestEncodingParametersChangeTask(
VideoBitrateAllocation bitrate_allocation,
uint32_t framerate,
const std::optional<gfx::Size>& size) { … }
void VaapiVideoEncodeAccelerator::Flush(FlushCallback flush_callback) { … }
void VaapiVideoEncodeAccelerator::FlushTask(FlushCallback flush_callback) { … }
bool VaapiVideoEncodeAccelerator::IsFlushSupported() { … }
void VaapiVideoEncodeAccelerator::Destroy() { … }
void VaapiVideoEncodeAccelerator::DestroyTask() { … }
void VaapiVideoEncodeAccelerator::SetState(State state) { … }
void VaapiVideoEncodeAccelerator::NotifyError(EncoderStatus status) { … }
bool VaapiVideoEncodeAccelerator::OnMemoryDump(
const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) { … }
}