One of the objectives of VVAS is to provide a framework for the advanced developers that enables them to develop their own kernel and integrate it into GStreamer-based applications. Kernel developers might not be experts in software development, and there is always a learning curve to understand the frameworks like Xilinx Run Time (XRT), GStreamer, and so on. This framework abstracts the complexities of the XRT and GStreamer framework and provides simple and easy to understand APIs to hook their kernels developed on an AMD FPGA into complex applications. Developers need to implement these interfaces and make them available in the form of a library, called as an acceleration software library.
This section covers the details about developing an acceleration software library to control the kernel from the GStreamer application. The acceleration software library manages the kernel state machine as well as provide hooks to control the kernel from the GStreamer plug-ins or an application.
There are different types of acceleration kernels supported by VVAS and details about these are covered as follows.
Types of Kernels
There are three types of acceleration kernels based on the implementation.
Hard Kernel
A hard kernel is made up of high-level synthesis (HLS) and register transfer level (RTL) kernels.
Software Kernel
Software kernels are software processing modules that execute fully on the host CPU. Exposing the software kernel through the acceleration software library interface allows them to integrate into the GStreamer framework without an in-depth knowledge of the GStreamer framework.
Kernel Management Modes
Kernels can be managed in two different modes. In XRT managed mode, kernel execution (starting/stopping/configuration) is managed by the underlying Xilinx Run Time (XRT). In this mode, the synchronization between different kernel instances executing simultaneously in different thread/process contexts is managed by XRT. This mode of operation is multi thread/multiprocess safe. XRT ensures that at any point, only one kernel context is given access to the actual kernel hardware resources.
The second mode of operation is user managed mode. In this mode, you are responsible to start, stop, and configure kernel using register read/write APIs. This mode is allowed only if the kernel object is created in exclusive mode. If the kernel is opened in exclusive mode, then only one instance of the kernel object can be created/opened.
It is strongly recommended to use a kernel in XRT managed mode by opening the kernel in shared mode as this is multi thread/multi-process safe and easy to configure.
Interfaces for Acceleration Software Libraries
VVAS has defined a simplified, easy to use interface so that developers can easily hook kernels into GStreamer based applications. The interface has been divided into two parts, core APIs and utility APIs. Core APIs are mandatory APIs, to be implemented by the acceleration software libraries so that these can be controlled from the GStreamer Infrastructure plug-ins as well as applications. Utility APIs are provided to abstract the XRT interface from the developer and provide easy to use interface to interact with the kernel.
Core API
Each acceleration software library must implement these core APIs. The GStreamer VVAS infrastructure plug-ins (vvas_xfilter and vvas_xmultisrc) call these core APIs to perform operations on the kernel. The following is a list of the core APIs.
xlnx_kernel_init
This API is called by the VVAS infrastructure plug-in only once at the time of plug-in initialization. The acceleration software library can perform all the one-time initialization in this function.
VvasReturnType xlnx_kernel_init (VVASKernel * handle)
Parameters:
handle - VVAS kernel handle which has kernel context
Return:
VvasReturnType
xlnx_kernel_start
This API is called by infrastructure plug-in for each input frame/buffer it has received for further processing by the kernel. The acceleration software library can perform per frame operations like updating the state machine, reading/writing the registers of the IP, and then instructing the kernel to process the input. Steps to start the kernel depends on the type of the kernel and are covered in detail later in this section.
VvasReturnType xlnx_kernel_start (VVASKernel * handle, int start, VVASFrame *
input[MAX_NUM_OBJECT], VVASFrame * output[MAX_NUM_OBJECT])
Parameters:
handle - VVAS kernel handle which has kernel context
start – flag to indicate start of the kernel. Mainly useful in
streaming kernel mode
input[MAX_NUM_OBJECT] – Array of input frames populated in VVASFrame
handle
output[MAX_NUM_OBJECT] – Array of output frames populated in VVASFrame
handle
Return:
VvasReturnType
Note:
1. MAX_NUM_OBJECT is 512 and same is assigned in vvas_kernel.h
2. Input and output of array is NULL terminated to know number of input
& output frames received to start function
xlnx_kernel_done
To know whether the kernel has finished processing the current frame/buffer, an infrastructure plug-in or an application will call this API. The acceleration software library can implement the logic to know the status of the kernel in this API.
If the kernel is being used in XRT managed mode, then you must call the vvas_kernel_done utility API to know if the kernel has finished processing the current job. This API takes time-out svalue for the maximum amount of time this API will wait before returning from this function.
If the kernel is being used in user managed mode, then you can implement the logic where it can monitor the status of AP_DONE bit of the kernel in polling mode to know the status of the current kernel task.
VvasReturnType xlnx_kernel_done (VVASKernel * handle)
Parameters:
handle - VVAS kernel handle which has kernel context
kernel.
Return:
VvasReturnType
xlnx_kernel_deinit
This API is called by the infrastructure plug-in when the plug-in is deinitializing. The acceleration software library must perform any clean-up and deinitialization tasks, such as freeing private handles and internal memory allocation, as part of the library initialization process.
VvasReturnType xlnx_kernel_deinit (VVASKernel * handle)
Parameters:
handle - VVAS kernel handle which has kernel context
Return:
VvasReturnType
Utility APIs
This section covers details about the utility infrastructure required to develop the kernel libraries for a new kernel and to integrate these acceleration software libraries into the GStreamer framework.
The acceleration software libraries are developed using the following utility APIs.
The utility API sources are hosted in the vvas-utils folder of the VVAS sources tree.
Memory Management APIs
The hardware kernel works on the physically contiguous memory. If the acceleration software library need physically contiguous memory, then the following utility APIs can be used:
VVASFrame* vvas_alloc_buffer (VVASKernel *handle, uint32_t size,
VVASMemoryType mem_type, VVASFrameProps *props)
Parameters:
handle - VVAS kernel handle which has kernel context
size - memory size to be allocated
mem_type - memory can be VVAS_FRAME_MEMORY or VVAS_INTERNAL_MEMORY
props – required when requesting VVAS_FRAME_MEMORY
Return:
VVASFrame handle on success or NULL on failure
If there is frequent memory allocation/deallocation is required, it is recommmended that you allocate from the memory pool. Allocate the memory from the memory pool managed by the GStreamer infrastructure plug-in. To allocate memory from a memory pool managed by the GStreamer Infrastructure plug-in, you must pass the “props” parameter value as VVAS_FRAME_MEMORY. If the “props” parameter value is set to VVAS_INTERNAL_MEMORY, then the memory is not allocated from a memory pool.
The following API is to free the memory that is allocated using the vvas_alloc_buffer() API.
void vvas_free_buffer (VVASKernel * handle, VVASFrame *vvas_frame)
Parameters:
handle - VVAS kernel handle which has kernel context
vvas_frame – VVASFrame handle allocated using vvas_alloc_buffer() API
Return:
None
Read/Write Register APIs
These APIs can be used only if the kernel object is created in exclusive mode. Moreover, ensure that the “is_multiprocess” parameter is set to “false” in the VVASKernel object in the acceleration software library.
If you decide to go for the option of programing the kernel using register programming, then the only option to start a kernel is to set the “ap_start” bit by writing into the control register.
Important
These APIs are deprecated and will be removed from next VVAS releases. VVAS supports only XRT managed kernel mode.
void vvas_register_write (VVASKernel *handle, void *src, size_t size,
size_t offset)
Parameters:
handle - VVAS kernel handle which has kernel context
src – pointer to data to be written at offset in ERT command buffer or
register at offset from base address of IP
size – size of the data pointer src
offset – offset at which data to be written
Return:
None
The following API used to read from the registers of an IP/kernel. Use this API to read the interrupt status register to know the status of the task in polling mode.
void vvas_register_read (VVASKernel *handle, void *src, size_t size, size_t
offset)
Parameters:
handle - VVAS kernel handle which has kernel context
src – pointer to data which will be updated after read
size – size of the data pointer src
offset – offset from base address of an IP
Return:
None
Execution APIs
These APIs must be used if the kernel is being used in XRT managed mode. Execution APIs are used to start kernel execution and then wait for the completion of the kernel current task. These APIs are multi-process/multi-thread safe. These APIs are only used when is_multiprocess is set to true during kernel initialization. Use the following API to start IP/kernel execution.
vvas_kernel_start
Use this API to start the kernel. This API internally uses Xilinx Run Time (XRT) APIs to start the kernel.
VvasReturnType vvas_kernel_start (VVASKernel * handle, const char *format, ...);
Parameters:
handle - VVAS kernel handle which has kernel context
format - Variable arguments for the list of arguments that the kernel takes.
Return:
VvasReturnType
Use the following format specifiers for kernel arguments for the “format” argument:
For example: For passing three arguments of types int, unsigned int, and a pointer, then the format specifier string would be “iup”
If you want to skip the middle argument in that case, then the format specifier string would be “isp”.
Use the following API to check whether the IP or kernel has finished execution. This function internally loops for MAX_EXEC_WAIT_RETRY_CNT times until a timeout before returning an error.
VvasReturnType vvas_kernel_done (VVASKernel * handle, int32_t timeout);
Parameters:
handle - VVAS kernel handle which has kernel context
timeout - Timeout in milliseconds
Return:
VvasReturnType
VVAS Data Structures
The following sections list the core structures and enumerations used in the VVAS framework.
VVASKernel
The VVASKernel is a structure to hold the kernel context and is created and passed to the core APIs by the GStreamer infrastructure plug-ins (for example, the vvas_xfilter).
typedef struct _vvas_kernel VVASKernel;
struct _vvas_kernel {
void *xcl_handle; /* XRT handle provided by GStreamer infrastructure plug-in */
uint32_t cu_idx; /* compute unit index of IP/soft-kernel */
json_t *kernel_config; /* kernel specific config from app */
void *kernel_priv; /* to store kernel specific structures */
json_t *kernel_dyn_config; /* Dynamically changed kernel configuration */
xrt_buffer *ert_cmd_buf; /* ERT command buffer used to submit execution commands to XRT */
size_t min_offset;
size_t max_offset;
VVASBufAllocCBFunc alloc_func; /* callback function to allocate memory from GstBufferPool */
VVASBufFreeCBFunc free_func; /* callback function to free memory allocated by alloc_func */
void *cb_user_data; /* handle to be passed along with alloc_func & free_func callback */
vvaspads *padinfo;
uint8_t is_multiprocess; /* if true, ERT command buffer will be used to start kernel,
else direct register programming will be used */
uint8_t *name;
uint16_t in_mem_bank; /* Memory bank to which input port of kernel is attached to */
uint16_t out_mem_bank; /* Memory bank to which output port of kernel is attached to */
};
VVASVideoFormat
The VVASVideoFormat represents the video color formats supported by the VVAS framework. The GStreamer infrastructure plug-ins support the mapping of the following formats and corresponding GStreamer color formats:
typedef enum {
VVAS_VMFT_UNKNOWN = 0,
VVAS_VFMT_RGBX8,
VVAS_VFMT_YUVX8,
VVAS_VFMT_YUYV8, // YUYV
VVAS_VFMT_ABGR8,
VVAS_VFMT_RGBX10,
VVAS_VFMT_YUVX10,
VVAS_VFMT_Y_UV8,
VVAS_VFMT_Y_UV8_420, // NV12
VVAS_VFMT_RGB8,
VVAS_VFMT_YUVA8,
VVAS_VFMT_YUV8,
VVAS_VFMT_Y_UV10,
VVAS_VFMT_Y_UV10_420,
VVAS_VFMT_Y8,
VVAS_VFMT_Y10,
VVAS_VFMT_ARGB8,
VVAS_VFMT_BGRX8,
VVAS_VFMT_UYVY8,
VVAS_VFMT_BGR8, // BGR
VVAS_VFMT_RGBX12,
VVAS_VFMT_RGB16,
VVAS_VFMT_I420
} VVASVideoFormat;
VVASFrame
The VVASFrame stores information related to a video frame. The GStreamer infrastructure plug-ins allocate the VVASFrame handle for input and output video frames and sends them to the VVAS kernel processing APIs. Also, the VVASFrame can be allocated by the kernel libraries for internal memory requirements (that is, memory for filter coefficients).
typedef struct _vvas_frame_props VVASFrameProps;
typedef struct _vvas_frame VVASFrame;
// frame properties hold information about video frame
struct _vvas_frame_props {
uint32_t width;
uint32_t height;
uint32_t stride;
VVASVideoFormat fmt;
};
struct _vvas_frame {
uint32_t bo[VIDEO_MAX_PLANES]; // ignore: currently not used
void *vaddr[VIDEO_MAX_PLANES]; // virtual/user space address of
//video frame memory
uint64_t paddr[VIDEO_MAX_PLANES]; // physical address of video frame
uint32_t size[VIDEO_MAX_PLANES];
void *meta_data;
VVASFrameProps props; /* properties of video frame */
/* application's private data */
void *app_priv; /* assigned to GstBuffer by GStreamer infrastructure plugin */
VVASMemoryType mem_type;
/*number of planes in props.fmt */
uint32_t n_planes; // number of planes based on color format
};
Other VVAS Declarations
#define MAX_NUM_OBJECT 512 /* max number of video frames/objects
handled by VVAS */
#define MAX_EXEC_WAIT_RETRY_CNT 10 /* retry count on xclExecWait failure */
#define VIDEO_MAX_PLANES 4
#define DEFAULT_MEM_BANK 0
typedef enum {
VVAS_UNKNOWN_MEMORY,
VVAS_FRAME_MEMORY, /* use for input and output buffers */
VVAS_INTERNAL_MEMORY, /* use for internal memory of IP */
} VVASMemoryType;
typedef struct buffer {
unsigned int bo; /* XRT Buffer object */
void* user_ptr; /* userspace/virtual pointer */
uint64_t phy_addr; /* physical address */
unsigned int size; /* size of XRT memory */
} xrt_buffer;
The VVAS acceleration software libraries APIs are broadly categorized into two API types, Core API and Utility API.
Acceleration Software Library for Hard Kernels
This section covers the steps to develop an acceleration software library for hard kernels.
Note
It is assumed that hard kernel work only on physical address. Hence, infrastructure plug-ins will only provide a physical address for the input/output buffers. If for any reason you want to access the input/output buffers in the software acceleration library, then map the buffer and get the virtual address.
The virtual address is populated by infrastructure plug-ins only for the software acceleration library for “software only” kernels.
Memory Allocation
A hard kernel works on the physically contiguous memory. Use the vvas_alloc_buffer API to allocate physically contiguous memory on the device (FPGA).
Starting the Kernel
The APIs used to start a kernel depend on the mode in which the kernel object is created.
XRT Managed Kernel
vvas_kernel_startmust be used to start the kernel execution. It takes all kernel parameters required to be programmed as function arguments.
User Managed Mode
Use the vvas_register_write API to set the AP_START bit in the kernel control register.
Important
vvas_register_write is deprecated and will be removed from the next release. VVAS supports only XRT managed kernels.
Checking the Kernel Task Status
When a kernel is started, the next step is to check if the task is completed or not. Depending on the mode in which kernel object is created, different mechanisms are used.
XRT Managed Kernel
In this mode, call the vvas_kernel_done API. This API will return when the kernel finishes processing the current task. Provide the “time-out” interval value indicating how long this API has to wait before it can return if the kernel has not finished processing.
User Managed Mode
In this mode, there is no callback or interrupt notification mechanism available that can be used to notify the task completion. The acceleration software library must continuously poll the kernel status register using vvas_register_read.
Important
vvas_register_read is deprecated and will be removed from next release. VVAS supports only XRT managed kernels.
Acceleration Software Library for the Software Kernel
Software kernels are software modules that run on the application processor. The acceleration software library for these processing modules does not interact with the XRT interface. The interface APIs that abstract the XRT interface are not needed. You must implement the core API in the acceleration software library for use in the GStreamer application through VVAS infrastructure plug-ins.
Capability Negotiation Support in the Acceleration Software Library
Kernel capability negotiation is an important functionality that should be accepted between the upstream element and infrastructure plug-ins to achieve an optimal solution. Because the infrastructure plug-ins are generic, the acceleration software library is responsible to populate the required kernel capability during xlnx_kernel_init(), which is negotiated between the infrastructure plug-ins and the upstream element. The infrastructure plug-in suggests a format on its sink pad and arranges the recommendation in priority order as per the kernel capability in the result from the CAPS query that is performed on the sink pad. Only the vvas_xfilter plug-in is currently supports the kernel specific capability negotiation.
The following section explains the data structures exchange between the acceleration software libraries and the infrastructure plug-ins for capability negotiation.
typedef struct caps
{
uint8_t range_height; /* true/false if height is specified in range */
uint32_t lower_height; /* lower range of height supported,
range_height=false then this value specified the fixed height supported
*/
uint32_t upper_height; /* upper range of height supported */
uint8_t range_width; /* true/false if width is specified in range */
uint32_t lower_width; /* lower range of width supported,
range_width=false then this value specified the fixed width supported */
uint32_t upper_width; /* upper range of width supported */
uint8_t num_fmt; /* number of format support by kernel */
VVASVideoFormat *fmt; /* list of formats */
} kernelcaps;
typedef struct kernelpad
{
uint8_t nu_caps; /* number of different caps supported */
kernelcaps **kcaps; /* lsit of caps */
} kernelpads;
The following user friendly APIs are provided for the kernel to set the capabilities defined in the preceding structures.
API to create new caps with input parameters:
vvas_caps_new() - Create new caps with input parameters
range_height
- true : if kernel support range of height
- false : if kernel support fixed height
lower_height : lower value of height supported by kernel
if range_height is false, this holds the fixed value
upper_height : higher value of hight supported by kernel
if range_height is false, this should be 0
range_width : same as above
lower_width :
upper_width :
... : variable range of format supported terminated by 0
make sure to add 0 at end otherwise
code will take format till it gets 0
kernelcaps * vvas_caps_new (uint8_t range_height,
uint32_t lower_height,
uint32_t upper_height,
uint8_t range_width,
uint32_t lower_width,
uint32_t upper_width, ...)
API to add new caps to sink pad. Only one pad is supported in this release:
bool vvas_caps_add_to_sink (VVASKernel * handle, kernelcaps * kcaps, int sinkpad_num)
API to add new caps to the src pad. Only one pad is currently supported:
bool vvas_caps_add_to_src (VVASKernel * handle, kernelcaps * kcaps, int sinkpad_num)
Example using xstub (Development of an Acceleration Software Library)
This section describes the steps for creating an acceleration software library using VVAS APIs for a kernel. As an example, a sample software kernel is used, and this library is for controlling this vvas_xstub kernel.
The vvas_xstub kernel is a software kernel supporting three operation modes.
PASSTHROUGH : pass through the input without any modification
INPLACE : updating the buffer in place to specific value (currently supported with input format rgb and bgr)
TRANSFORM : generates the new buffer at outputs and validates it
Note
The vvas_xstub kernel is primarily used for illustrative purposes. Delving deep into discussions about the `vvas_xstub` kenel falls beyond the scope of this topic.
Each software acceleration library must expose the mandatory core APIs. GStreamer infrastructure plug-ins, as well as applications, call these core APIs to control the kernel. The mandatory core APIs are as follows.
/********* CORE APIs ************/
VvasReturnType xlnx_kernel_init (VVASKernel * handle);
VvasReturnType xlnx_kernel_start (VVASKernel * handle, int start,
VVASFrame * input[MAX_NUM_OBJECT], VVASFrame * output[MAX_NUM_OBJECT]);
VvasReturnType xlnx_kernel_done (VVASKernel * handle);
VvasReturnType xlnx_kernel_deinit (VVASKernel * handle);
All the necessary one-time initialization procedures must be executed within the xlnx_kernel_init API. The subsequent private structure is employed for the purpose of retaining and managing all these initializations. It is left to your discretion to implement your own storage classes for the management of these one-time initializations.
/*
* Structure to store all initializations
*/
typedef struct _vvas_xstub
{
int log_level;
Vvas_XKernelMode kernel_type;
Vvas_XElementMode element_mode;
} VVASXStub;
xlnx_kernel_init is responsible for initializing the kernel parameters and preparing them for further operations. It extracts configuration information from the provided VVASKernel handle, initializes the kernel parameters, and prepares the environment required for the kernel’s functionality.
VvasReturnType
xlnx_kernel_init (VVASKernel * handle)
{
VVASXStub *kpriv;
json_t *val;
char *static_cfg;
/* kernel specification info is required to validate kernel library */
if (handle->kernel_config == NULL) {
LOG_MESSAGE (LOG_LEVEL_ERROR, LOG_LEVEL_WARNING,
"json object for static kernel configuration is NOT available");
return VVAS_RET_ERROR;
}
kpriv = (VVASXStub *) malloc (sizeof (VVASXStub));
if (!kpriv) {
LOG_MESSAGE (LOG_LEVEL_ERROR, LOG_LEVEL_WARNING,
"failed to allocate private handle\n");
return VVAS_RET_ERROR;
}
.
.
.
handle->kernel_priv = (void *) kpriv;
.
.
return VVAS_RET_SUCCESS;
}
Kernel configuration to configure the vvas_xstub kernel:
This json demostrates the inplace mode. Given mode can be changed by replacing ‘inplace’ string to ‘transform’ or ‘passthrough’ for other configurations.
{
"xclbin-location":"/run/media/mmcblk0p1/x_plus_ml.xclbin",
"vvas-library-repo": "/usr/lib",
"element-mode":"inplace",
"kernel" :
{
"library-name":"libvvas_xstub.so",
"config": {
"kernel-lib-type" : "soft-lib",
"element-mode" : "inplace",
"src-stride-align" : 1,
"src-height-align" : 1,
"sink-stride-align" : 1,
"sink-height-align" : 1,
"debug-level" : 4
}
}
}
The following section breaks down the function flow of the acceleration library’s xlnx_kernel_init API.
Allocate memory for the VVASXStub structure (kpriv) to store the kernel parameters. If memory allocation fails, an error message is logged, and the function returns an error code.
kpriv = (VVASXStub *) malloc (sizeof (VVASXStub));
if (!kpriv) {
LOG_MESSAGE (LOG_LEVEL_ERROR, LOG_LEVEL_WARNING,
"failed to allocate private handle\n");
return VVAS_RET_ERROR;
}
Retrieve the debug log level from the kernel configuration. If not provided, set a default warning log level. Otherwise, set the value that is extracted from the configuration.
/*
* Get the kernel configuration from application through JSON File.
*/
val = json_object_get (handle->kernel_config, "debug-level");
if (!val || !json_is_integer (val))
kpriv->log_level = LOG_LEVEL_WARNING;
else
kpriv->log_level = json_integer_value (val);
Memory bank indexes for input and output GstMemory are retrieved from the configuration. If not the indexes are not provided, the default values are used.
/*
* Get and set the memory bank index where to allocate input GstMemory
* by infrastructure plugin
*/
val = json_object_get (handle->kernel_config, "in-mem-bank");
if (!json_is_integer (val)) {
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"in-mem-bank is not set, setting it to %d", STUB_DEFAULT_MEM_BANK);
handle->in_mem_bank = STUB_DEFAULT_MEM_BANK;
} else {
handle->in_mem_bank = json_integer_value (val);
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level, "in-mem-bank is set to %d",
json_integer_value (val));
}
/*
* Get and set the memory bank index where to allocate output GstMemory
* by infrastructure plugin
*/
val = json_object_get (handle->kernel_config, "out-mem-bank");
if (!json_is_integer (val)) {
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"out-mem-bank is not set, setting it to %d", STUB_DEFAULT_MEM_BANK);
handle->out_mem_bank = STUB_DEFAULT_MEM_BANK;
} else {
handle->out_mem_bank = json_integer_value (val);
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level, "out-mem-bank is set to %di",
json_integer_value (val));
}
As per different functionalities of the vvas_xstub kernel, there are various configuration options and parameters that gets parsed from json.
kpriv->kernel_type = get_kernel_lib_type (json_string_value (val));
if (kpriv->kernel_type == VVAS_KERNEL_LIB_TYPE_UNKNOWN) {
LOG_MESSAGE (LOG_LEVEL_ERROR, kpriv->log_level,
"unknown kernel lib type : %s\n", json_string_value (val));
return VVAS_RET_ERROR;
}
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level, "kernel library type : %s\n",
json_string_value (val));
if (kpriv->kernel_type == VVAS_KERNEL_LIB_TYPE_SOFTLIB) {
/* kernel library is for software library */
if (handle->dev_handle) {
LOG_MESSAGE (LOG_LEVEL_WARNING, kpriv->log_level,
"avoid setting xclbin-location in root json");
}
.
.
.
.
LOG_MESSAGE (LOG_LEVEL_DEBUG, kpriv->log_level, "STUB Init Successful");
return VVAS_RET_SUCCESS;
After completing all the necessary initializations in the xlnx_kernel_init API, the next step is to execute the kernel using the xlnx_kernel_start API. This API is called by the infrastructure plug-in for each input frame or buffer received, indicating that the kernel needs to process the provided data.
VvasReturnType
xlnx_kernel_start (VVASKernel * handle, int start,
VVASFrame * input[MAX_NUM_OBJECT], VVASFrame * output[MAX_NUM_OBJECT])
{
VVASXStub *kpriv = (VVASXStub *) handle->kernel_priv;
.
.
.
.
return VVAS_RET_SUCCESS;
}
The following section breaks down the function flow of the acceleration library’s xlnx_kernel_start API.
The API begins by retrieving the kernel’s private data (kpriv) that was initialized during the xlnx_kernel_init API. This data contains various parameters required for the kernel’s execution.
/*
* Get the VVASXStub which was initialized during xlnx_kernel_init api
*/
VVASXStub *kpriv = (VVASXStub *) handle->kernel_priv;
Perform the validation for input frames.
/**************************************************
*********** Input frames validation **************
**************************************************/
while (idx < MAX_NUM_OBJECT) {
if (input[idx]) {
VVASFrame *inframe = input[idx];
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"----- Input frame index %d -----", idx);
ret = validate_vvas_frame (handle, inframe);
if (ret < 0) {
return VVAS_RET_ERROR;
}
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"----------------------------");
idx++;
} else
break;
}
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"number of input frames received : %d", idx);
if (output[0] == NULL) {
LOG_MESSAGE (LOG_LEVEL_ERROR, kpriv->log_level,
"Kernel needs output buffer as it works only in transform mode.");
return VVAS_RET_ERROR;
}
Perform the validation for output frames.
/**************************************************
*********** Output frames validation *************
**************************************************/
idx = 0;
if (output[idx]) {
while (idx < MAX_NUM_OBJECT) {
if (output[idx]) {
VVASFrame *outframe = output[idx];
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"----- Output frame index %d -----", idx);
ret = validate_vvas_frame (handle, outframe);
if (ret < 0) {
return VVAS_RET_ERROR;
}
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"----------------------------");
idx++;
} else
break;
}
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"number of output frames received : %d", idx);
} else {
Handling of output buffer in case of passthrough or inplace mode
} else {
/* no output buffers in case of passthrough/inplace mode */
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"number of output frames received : %d", idx);
idx = 0;
while (idx < MAX_NUM_OBJECT) {
if (input[idx]) {
if (kpriv->element_mode == VVAS_ELEMENT_MODE_IN_PLACE) {
VVASFrame *inframe = input[idx];
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"Modifying the input %d in inplace mode", idx);
modify_input_frame_inplace (handle, inframe);
} else if (kpriv->element_mode == VVAS_ELEMENT_MODE_PASSTHROUGH) {
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"input %d is passthrough", idx);
}
idx++;
} else
break;
}
}
Allocate frames using VVAS MEMORY MANGEMENT APIs. This example demonstrates the allocation with specific memory types and properties.
/*
* Allocate VVAS frames using VVAS MEMORY MANGEMENT APIs
*/
/*
* In this example we are going to allocate frame as VVAS_FRAME_MEMORY
* and VVAS_INTERNAL_MEMORY using VVAS MEMORY MANGEMENT APIs
*/
/* VVAS_FRAME_MEMORY validation */
/* In case there is frequent memory allocation/de-allocation is required,
then we can allocate memory from the gstreamer memory pool.
To allocate memory from memory pool managed by the GStreamer Infrastructure plug-in,
developer must pass mem_type parameter value as VVAS_FRAME_MEMORY */
/*
* Set the frame properties like height, width and format of the frame
* which could be used by infrastructure plugin to create the memory pool
*/
user_frame_props.height = user_height;
user_frame_props.width = user_width;
user_frame_props.fmt = VVAS_VFMT_Y_UV8_420;
user_mem_type = VVAS_FRAME_MEMORY;
user_mem_bank = STUB_DEFAULT_MEM_BANK;
/* If memtype is a VVAS_FRAME_MEMORY size is calculated by gstreamer plugin, so setting it to 0 */
user_buf_size = 0;
user_frame =
vvas_alloc_buffer (handle, user_buf_size, user_mem_type, user_mem_bank,
&user_frame_props);
if (user_frame == NULL) {
LOG_MESSAGE (LOG_LEVEL_ERROR, kpriv->log_level,
"Couldn't allocate vvas frame");
return VVAS_RET_ERROR;
}
if (user_frame->app_priv) {
GstVideoMeta *vmeta;
GstBuffer *buffer = (GstBuffer *) user_frame->app_priv;
int p;
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"Allocated vvas frame from gst memory pool %d", user_frame->n_planes);
vmeta = gst_buffer_get_video_meta (buffer);
if (vmeta == NULL) {
LOG_MESSAGE (LOG_LEVEL_ERROR, kpriv->log_level,
"Could not find vmeta in gst buffer");
return VVAS_RET_ERROR;
}
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"Gst Buffer height %d width %d Planes %d", vmeta->height, vmeta->width,
vmeta->n_planes);
for (p = 0; p < vmeta->n_planes; p++) {
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"stride of plane_%d is %d ", p, vmeta->stride[p]);
}
for (p = 0; p < user_frame->n_planes; p++) {
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"plane %d : bo %p vaddr %p paddr %p size %u", p, user_frame->bo[p],
user_frame->vaddr[p], (void *) user_frame->paddr[p],
user_frame->size[p]);
}
}
vvas_free_buffer (handle, user_frame);
/* VVAS_INTERNAL_MEMORY validation */
user_mem_type = VVAS_INTERNAL_MEMORY;
user_buf_size = user_height * user_width * 1.5;
/* No need to pass VVASFrameProps while using VVAS_INTERNAL_MEMORY */
user_frame =
vvas_alloc_buffer (handle, user_buf_size, user_mem_type, user_mem_bank,
NULL);
if (user_frame == NULL) {
LOG_MESSAGE (LOG_LEVEL_ERROR, kpriv->log_level,
"Couldn't allocate vvas frame");
return VVAS_RET_ERROR;
}
if (user_frame->bo[0]) {
int p = 0;
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"Allocated vvas frame from internal memory");
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"plane %d : bo %p vaddr %p paddr %p size %u", p, user_frame->bo[0],
user_frame->vaddr[0], (void *) user_frame->paddr[0],
user_frame->size[0]);
}
vvas_free_buffer (handle, user_frame);
The function is used to validate the vvas frame data. This function checks various frame properties and accordingly prints the log information and in case of error returns the negative result.
int32_t
validate_vvas_frame (VVASKernel * handle, VVASFrame * frame)
{
int p;
VVASXStub *kpriv = (VVASXStub *) handle->kernel_priv;
if (frame->n_planes > VIDEO_MAX_PLANES) {
LOG_MESSAGE (LOG_LEVEL_ERROR, kpriv->log_level,
"wrong number of planes in frame %u", frame->n_planes);
return -1;
}
for (p = 0; p < frame->n_planes; p++) {
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"plane %d : bo %p vaddr %p paddr %p size %u", p, frame->bo[p],
frame->vaddr[p], (void *) frame->paddr[p], frame->size[p]);
if (kpriv->kernel_type == VVAS_KERNEL_LIB_TYPE_HARDKERNEL
|| kpriv->kernel_type == VVAS_KERNEL_LIB_TYPE_PSKERNEL) {
if (frame->paddr[p] == 0 || frame->paddr[p] == (uint64_t) - 1) {
LOG_MESSAGE (LOG_LEVEL_ERROR, kpriv->log_level,
"not valid physical address %lu", frame->paddr[p]);
return -1;
}
/* vaddr not required in case hardkernel and ps_kernel
* but, if gstplugin assigns this it might have resulted DMA copy in PCIe platforms
*/
if (frame->vaddr[p]) {
LOG_MESSAGE (LOG_LEVEL_WARNING, kpriv->log_level,
"vaddr %p available..possibility of DMA copy and hence performance drop",
frame->vaddr[p]);
}
} else {
if (frame->vaddr[p] == 0 || (uint64_t) frame->vaddr[p] == (uint64_t) - 1) {
LOG_MESSAGE (LOG_LEVEL_ERROR, kpriv->log_level,
"not valid virtual address %p", (void *) frame->paddr[p]);
return -1;
}
if (frame->paddr[p]) {
LOG_MESSAGE (LOG_LEVEL_WARNING, kpriv->log_level,
"paddr %p available.. but not required here as kernel type is softlib",
frame->vaddr[p]);
}
}
}
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"frame props : width %u, height %u, stride %u, fmt %u",
frame->props.width, frame->props.height, frame->props.stride,
frame->props.fmt);
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"meta_data %p, app_priv %p, mem_type %u, n_planes %u", frame->meta_data,
frame->app_priv, frame->mem_type, frame->n_planes);
return 0;
}
The following function is used with inplace mode to update the value of frame data.
void
modify_input_frame_inplace (VVASKernel * handle, VVASFrame * frame)
{
int p;
VVASXStub *kpriv = (VVASXStub *) handle->kernel_priv;
for (p = 0; p < frame->n_planes; p++) {
LOG_MESSAGE (LOG_LEVEL_INFO, kpriv->log_level,
"plane %d : bo %p vaddr %p paddr %p size %u", p, frame->bo[p],
frame->vaddr[p], (void *) frame->paddr[p], frame->size[p]);
if (kpriv->kernel_type == VVAS_KERNEL_LIB_TYPE_SOFTLIB
&& (frame->props.fmt == VVAS_VFMT_RGB8
|| frame->props.fmt == VVAS_VFMT_BGR8)) {
memset (frame->vaddr[p], 255, frame->props.stride * frame->props.height);
}
}
}
To determine whether the kernel has completed processing the ongoing frame or buffer, the infrastructure plug-in or an application can invoke the xlnx_kernel_done API.
VvasReturnType
xlnx_kernel_done (VVASKernel * handle)
{
VVASXStub *kpriv = (VVASXStub *) handle->kernel_priv;
LOG_MESSAGE (LOG_LEVEL_DEBUG, kpriv->log_level, "Kernel Done\n");
return VVAS_RET_SUCCESS;
}
xlnx_kernel_deinit is responsible for releasing any resources allocated during the initialization and execution. It performs necessary clean up tasks ,such as freeing private handles and releasing any internally allocated memory.
VvasReturnType
xlnx_kernel_deinit (VVASKernel * handle)
{
VVASXStub *kpriv = (VVASXStub *) handle->kernel_priv;
if (kpriv) {
LOG_MESSAGE (LOG_LEVEL_DEBUG, kpriv->log_level, "enter");
free (kpriv);
}
return VVAS_RET_SUCCESS;
}
Example pipeline using passthrough mode. Pipeline can be updated to other modes with similar formation changes and the json update.
gst-launch-1.0 filesrc location=input.nv12 ! rawvideoparse width=1920 height=1080 format=nv12 ! queue ! \
vvas_xfilter kernels-config=/etc/vvas/configs/xfilter/softlib_stub.json name=xfilter ! queue ! \
fpsdisplaysink video-sink="filesink location=output_passthrough.nv12" text-overlay=false sync=false -v