Sunshine master
Self-hosted game stream host for Moonlight.
vaapi.cpp File Reference

Definitions for VA-API hardware accelerated capture. More...

#include <fcntl.h>
#include <format>
#include <sstream>
#include <string>
#include <libavcodec/avcodec.h>
#include <libavutil/pixdesc.h>
#include <va/va.h>
#include <va/va_drm.h>
#include "graphics.h"
#include "misc.h"
#include "src/config.h"
#include "src/logging.h"
#include "src/platform/common.h"
#include "src/utility.h"
#include "src/video.h"
Include dependency graph for vaapi.cpp:

Classes

struct  va::AVVAAPIDeviceContext
 
struct  va::DRMPRIMESurfaceDescriptor
 DRM PRIME descriptor imported from a VAAPI surface. More...
 
class  va::va_ram_t
 VAAPI encode path that copies converted frames through system memory. More...
 
class  va::va_t
 VAAPI encode device that imports captured frames into VA surfaces. More...
 
class  va::va_vram_t
 VAAPI encode path that keeps converted frames in GPU memory. More...
 
struct  va::VAAPIDevicePriv
 

Typedefs

typedef struct va::AVVAAPIDeviceContext va::AVVAAPIDeviceContext
 
using va::display_t = util::safe_ptr_v2<void, VAStatus, vaTerminate>
 VA display handle released with vaTerminate.
 
typedef struct va::VAAPIDevicePriv va::VAAPIDevicePriv
 
using va::VADisplay = void *
 Native VA display handle.
 
using va::VAGenericID = unsigned int
 Generic numeric VAAPI object identifier.
 
using va::VAStatus = int
 Status code returned by VAAPI functions.
 
using va::VASurfaceID = VAGenericID
 VAAPI surface identifier.
 

Functions

std::unique_ptr< platf::avcodec_encode_device_tva::make_avcodec_encode_device (int width, int height, bool vram)
 
std::unique_ptr< platf::avcodec_encode_device_tva::make_avcodec_encode_device (int width, int height, file_t &&card, int offset_x, int offset_y, bool vram)
 Create AVCodec encode device.
 
std::unique_ptr< platf::avcodec_encode_device_tva::make_avcodec_encode_device (int width, int height, int offset_x, int offset_y, bool vram)
 Create AVCodec encode device.
 
int va::vaapi_init_avcodec_hardware_input_buffer (platf::avcodec_encode_device_t *encode_device, AVBufferRef **hw_device_buf)
 Create an FFmpeg VA-API hardware device context from a Sunshine encode device.
 
bool va::validate (int fd)
 Validate that the configured VAAPI device can be used.
 
VAStatus vaMapBuffer2 (VADisplay dpy, VABufferID buf_id, void **pbuf, uint32_t flags)
 Stub vaMapBuffer2 when building against libva before 2.21.0.
 
VAStatus vaSyncBuffer (VADisplay dpy, VABufferID buf_id, uint64_t timeout_ns)
 Stub vaSyncBuffer when building against libva before 2.9.0.
 

Variables

constexpr auto va::EXPORT_SURFACE_SEPARATE_LAYERS = 0x0004
 GameStream port offset for export surface separate layers.
 
constexpr auto va::EXPORT_SURFACE_WRITE_ONLY = 0x0002
 GameStream port offset for export surface write only.
 
constexpr auto va::SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2 = 0x40000000
 Protocol or platform constant for surface attrib mem type drm prime 2.
 

Detailed Description

Definitions for VA-API hardware accelerated capture.

Typedef Documentation

◆ AVVAAPIDeviceContext

typedef struct va::AVVAAPIDeviceContext va::AVVAAPIDeviceContext

VAAPI connection details.

Allocated as AVHWDeviceContext.hwctx

◆ VAAPIDevicePriv

typedef struct va::VAAPIDevicePriv va::VAAPIDevicePriv

This is a private structure of FFmpeg, I need this to manually create a VAAPI hardware context

xdisplay will not be used internally by FFmpeg

Function Documentation

◆ make_avcodec_encode_device() [1/3]

std::unique_ptr< platf::avcodec_encode_device_t > va::make_avcodec_encode_device ( int width,
int height,
bool vram )

Width --> Width of the image Height --> Height of the image offset_x --> Horizontal offset of the image in the texture offset_y --> Vertical offset of the image in the texture file_t card --> The file descriptor of the render device used for encoding

Parameters
widthFrame or display width in pixels.
heightFrame or display height in pixels.
vramWhether the image should use GPU memory instead of system memory.
Returns
Constructed AVCodec encode device object.

◆ make_avcodec_encode_device() [2/3]

std::unique_ptr< platf::avcodec_encode_device_t > va::make_avcodec_encode_device ( int width,
int height,
file_t && card,
int offset_x,
int offset_y,
bool vram )

Create AVCodec encode device.

Parameters
widthFrame or display width in pixels.
heightFrame or display height in pixels.
cardVideo device path or render node used for VAAPI.
offset_xOffset x.
offset_yOffset y.
vramWhether the image should use GPU memory instead of system memory.
Returns
Constructed AVCodec encode device object.

◆ make_avcodec_encode_device() [3/3]

std::unique_ptr< platf::avcodec_encode_device_t > va::make_avcodec_encode_device ( int width,
int height,
int offset_x,
int offset_y,
bool vram )

Create AVCodec encode device.

Parameters
widthFrame or display width in pixels.
heightFrame or display height in pixels.
offset_xOffset x.
offset_yOffset y.
vramWhether the image should use GPU memory instead of system memory.
Returns
Constructed AVCodec encode device object.

◆ vaapi_init_avcodec_hardware_input_buffer()

int va::vaapi_init_avcodec_hardware_input_buffer ( platf::avcodec_encode_device_t * encode_device,
AVBufferRef ** hw_device_buf )

Create an FFmpeg VA-API hardware device context from a Sunshine encode device.

Initialize FFmpeg's VA-API hardware device buffer.

Parameters
encode_deviceEncode device.
hw_device_bufOutput FFmpeg hardware device buffer.
Returns
0 when the buffer is initialized; negative FFmpeg error code on failure.

◆ validate()

bool va::validate ( int fd)

Validate that the configured VAAPI device can be used.

Parameters
fdNative file descriptor to wrap or inspect.
Returns
True when the VAAPI device is usable for capture or encode.

◆ vaMapBuffer2()

VAStatus vaMapBuffer2 ( VADisplay dpy,
VABufferID buf_id,
void ** pbuf,
uint32_t flags )

Stub vaMapBuffer2 when building against libva before 2.21.0.

Parameters
dpyVA display.
buf_idVA buffer ID.
pbufOutput mapped buffer pointer.
flagsMapping flags.
Returns
VA status code.

◆ vaSyncBuffer()

VAStatus vaSyncBuffer ( VADisplay dpy,
VABufferID buf_id,
uint64_t timeout_ns )

Stub vaSyncBuffer when building against libva before 2.9.0.

Parameters
dpyVA display.
buf_idVA buffer ID.
timeout_nsSync timeout in nanoseconds.
Returns
VA status code.