FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Functions | Variables
opencl.c File Reference
#include "opencl.h"
#include "avstring.h"
#include "log.h"
#include "avassert.h"
#include "opt.h"

Go to the source code of this file.

Data Structures

struct  KernelCode
 
struct  OpenclContext
 
struct  OpenclErrorMsg
 

Macros

#define LOCK_OPENCL
 
#define UNLOCK_OPENCL
 
#define MAX_KERNEL_CODE_NUM   200
 
#define OFFSET(x)   offsetof(OpenclContext, x)
 

Functions

const char * av_opencl_errstr (cl_int status)
 Get OpenCL error string.
 
static void free_device_list (AVOpenCLDeviceList *device_list)
 
static int get_device_list (AVOpenCLDeviceList *device_list)
 
int av_opencl_get_device_list (AVOpenCLDeviceList **device_list)
 Get OpenCL device list.
 
void av_opencl_free_device_list (AVOpenCLDeviceList **device_list)
 Free OpenCL device list.
 
static int init_opencl_mtx (void)
 
int av_opencl_set_option (const char *key, const char *val)
 Set option in the global OpenCL context.
 
int av_opencl_get_option (const char *key, uint8_t **out_val)
 Get option value from the global OpenCL context.
 
void av_opencl_free_option (void)
 Free option values of the global OpenCL context.
 
AVOpenCLExternalEnvav_opencl_alloc_external_env (void)
 Allocate OpenCL external environment.
 
void av_opencl_free_external_env (AVOpenCLExternalEnv **ext_opencl_env)
 Free OpenCL external environment.
 
int av_opencl_register_kernel_code (const char *kernel_code)
 Register kernel code.
 
cl_program av_opencl_compile (const char *program_name, const char *build_opts)
 compile specific OpenCL kernel source
 
cl_command_queue av_opencl_get_command_queue (void)
 get OpenCL command queue
 
int av_opencl_create_kernel (AVOpenCLKernelEnv *env, const char *kernel_name)
 Create kernel object in the specified kernel environment.
 
void av_opencl_release_kernel (AVOpenCLKernelEnv *env)
 Release kernel object.
 
static int init_opencl_env (OpenclContext *opencl_ctx, AVOpenCLExternalEnv *ext_opencl_env)
 
int av_opencl_init (AVOpenCLExternalEnv *ext_opencl_env)
 Initialize the run time OpenCL environment.
 
void av_opencl_uninit (void)
 Release OpenCL environment.
 
int av_opencl_buffer_create (cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr)
 Create OpenCL buffer.
 
void av_opencl_buffer_release (cl_mem *cl_buf)
 Release OpenCL buffer.
 
int av_opencl_buffer_write (cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size)
 Write OpenCL buffer with data from src_buf.
 
int av_opencl_buffer_read (uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size)
 Read data from OpenCL buffer to memory buffer.
 
int av_opencl_buffer_write_image (cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset, uint8_t **src_data, int *plane_size, int plane_num)
 Write image data from memory to OpenCL buffer.
 
int av_opencl_buffer_read_image (uint8_t **dst_data, int *plane_size, int plane_num, cl_mem src_cl_buf, size_t cl_buffer_size)
 Read image data from OpenCL buffer.
 
int64_t av_opencl_benchmark (AVOpenCLDeviceNode *device_node, cl_platform_id platform, int64_t(*benchmark)(AVOpenCLExternalEnv *ext_opencl_env))
 Benchmark an OpenCL device with a user defined callback function.
 

Variables

static const AVOption opencl_options []
 
static const AVClass openclutils_class
 
static OpenclContext opencl_ctx = {&openclutils_class}
 
static const cl_device_type device_type [] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU}
 
static const OpenclErrorMsg opencl_err_msg []
 

Macro Definition Documentation

#define LOCK_OPENCL
#define UNLOCK_OPENCL
#define MAX_KERNEL_CODE_NUM   200

Definition at line 48 of file opencl.c.

Referenced by av_opencl_register_kernel_code().

#define OFFSET (   x)    offsetof(OpenclContext, x)

Definition at line 84 of file opencl.c.

Function Documentation

const char* av_opencl_errstr ( cl_int  status)
static void free_device_list ( AVOpenCLDeviceList device_list)
static
static int get_device_list ( AVOpenCLDeviceList device_list)
static

Definition at line 202 of file opencl.c.

Referenced by av_opencl_get_device_list(), and init_opencl_env().

int av_opencl_get_device_list ( AVOpenCLDeviceList **  device_list)

Get OpenCL device list.

It must be freed with av_opencl_free_device_list().

Parameters
device_listpointer to OpenCL environment device list, should be released by av_opencl_free_device_list()
Returns
>=0 on success, a negative error code in case of failure

Definition at line 305 of file opencl.c.

Referenced by opt_opencl_bench().

void av_opencl_free_device_list ( AVOpenCLDeviceList **  device_list)

Free OpenCL device list.

Parameters
device_listpointer to OpenCL environment device list created by av_opencl_get_device_list()

Definition at line 323 of file opencl.c.

Referenced by opt_opencl_bench().

static int init_opencl_mtx ( void  )
inlinestatic

Definition at line 329 of file opencl.c.

Referenced by av_opencl_init(), av_opencl_register_kernel_code(), and av_opencl_set_option().

int av_opencl_set_option ( const char *  key,
const char *  val 
)

Set option in the global OpenCL context.

This options affect the operation performed by the next av_opencl_init() operation.

The currently accepted options are:

  • platform: set index of platform in device list
  • device: set index of device in device list

See reference "OpenCL Specification Version: 1.2 chapter 5.6.4".

Parameters
keyoption key
valoption value
Returns
>=0 on success, a negative error code in case of failure
See Also
av_opencl_get_option()

Definition at line 350 of file opencl.c.

Referenced by opt_opencl().

int av_opencl_get_option ( const char *  key,
uint8_t **  out_val 
)

Get option value from the global OpenCL context.

Parameters
keyoption key
out_valpointer to location where option value will be written, must be freed with av_freep()
Returns
>=0 on success, a negative error code in case of failure
See Also
av_opencl_set_option()

Definition at line 365 of file opencl.c.

void av_opencl_free_option ( void  )

Free option values of the global OpenCL context.

Definition at line 374 of file opencl.c.

AVOpenCLExternalEnv* av_opencl_alloc_external_env ( void  )

Allocate OpenCL external environment.

It must be freed with av_opencl_free_external_env().

Returns
pointer to allocated OpenCL external environment

Definition at line 382 of file opencl.c.

Referenced by av_opencl_benchmark().

void av_opencl_free_external_env ( AVOpenCLExternalEnv **  ext_opencl_env)

Free OpenCL external environment.

Parameters
ext_opencl_envpointer to OpenCL external environment created by av_opencl_alloc_external_env()

Definition at line 392 of file opencl.c.

Referenced by av_opencl_benchmark().

int av_opencl_register_kernel_code ( const char *  kernel_code)

Register kernel code.

The registered kernel code is stored in a global context, and compiled in the runtime environment when av_opencl_init() is called.

Parameters
kernel_codekernel code to be compiled in the OpenCL runtime environment
Returns
>=0 on success, a negative error code in case of failure

Definition at line 397 of file opencl.c.

cl_program av_opencl_compile ( const char *  program_name,
const char *  build_opts 
)

compile specific OpenCL kernel source

Parameters
program_namepointer to a program name used for identification
build_optspointer to a string that describes the preprocessor build options to be used for building the program
Returns
a cl_program object

Definition at line 424 of file opencl.c.

Referenced by ff_opencl_deshake_init(), and ff_opencl_unsharp_init().

cl_command_queue av_opencl_get_command_queue ( void  )

get OpenCL command queue

Returns
a cl_command_queue object

Definition at line 473 of file opencl.c.

Referenced by ff_opencl_deshake_init(), and ff_opencl_unsharp_init().

int av_opencl_create_kernel ( AVOpenCLKernelEnv env,
const char *  kernel_name 
)

Create kernel object in the specified kernel environment.

Parameters
envpointer to kernel environment which is filled with the environment used to run the kernel
kernel_namekernel function name
Returns
>=0 on success, a negative error code in case of failure
Deprecated:
, use clCreateKernel

Definition at line 479 of file opencl.c.

void av_opencl_release_kernel ( AVOpenCLKernelEnv env)

Release kernel object.

Parameters
envkernel environment where the kernel object was created with av_opencl_create_kernel()
Deprecated:
, use clReleaseKernel

Definition at line 485 of file opencl.c.

static int init_opencl_env ( OpenclContext opencl_ctx,
AVOpenCLExternalEnv ext_opencl_env 
)
static

Definition at line 491 of file opencl.c.

Referenced by av_opencl_init().

int av_opencl_init ( AVOpenCLExternalEnv ext_opencl_env)

Initialize the run time OpenCL environment.

Parameters
ext_opencl_envexternal OpenCL environment, created by an application program, ignored if set to NULL
Returns
>=0 on success, a negative error code in case of failure

Definition at line 584 of file opencl.c.

Referenced by ff_opencl_deshake_init(), and ff_opencl_unsharp_init().

void av_opencl_uninit ( void  )

Release OpenCL environment.

The OpenCL environment is effectively released only if all the created kernels had been released with av_opencl_release_kernel().

Definition at line 611 of file opencl.c.

Referenced by ff_opencl_deshake_uninit(), and ff_opencl_unsharp_uninit().

int av_opencl_buffer_create ( cl_mem *  cl_buf,
size_t  cl_buf_size,
int  flags,
void host_ptr 
)

Create OpenCL buffer.

The buffer is used to save the data used or created by an OpenCL kernel. The created buffer must be released with av_opencl_buffer_release().

See clCreateBuffer() function reference for more information about the parameters.

Parameters
cl_bufpointer to OpenCL buffer
cl_buf_sizesize in bytes of the OpenCL buffer to create
flagsflags used to control buffer attributes
host_ptrhost pointer of the OpenCL buffer
Returns
>=0 on success, a negative error code in case of failure

Definition at line 643 of file opencl.c.

Referenced by ff_opencl_deshake_process_inout_buf(), ff_opencl_unsharp_init(), and ff_opencl_unsharp_process_inout_buf().

void av_opencl_buffer_release ( cl_mem *  cl_buf)

Release OpenCL buffer.

Parameters
cl_bufpointer to OpenCL buffer to release, which was previously filled with av_opencl_buffer_create()

Definition at line 654 of file opencl.c.

Referenced by ff_opencl_deshake_uninit(), and ff_opencl_unsharp_uninit().

int av_opencl_buffer_write ( cl_mem  dst_cl_buf,
uint8_t src_buf,
size_t  buf_size 
)

Write OpenCL buffer with data from src_buf.

Parameters
dst_cl_bufpointer to OpenCL destination buffer
src_bufpointer to source buffer
buf_sizesize in bytes of the source and destination buffers
Returns
>=0 on success, a negative error code in case of failure

Definition at line 667 of file opencl.c.

Referenced by compute_mask_matrix().

int av_opencl_buffer_read ( uint8_t dst_buf,
cl_mem  src_cl_buf,
size_t  buf_size 
)

Read data from OpenCL buffer to memory buffer.

Parameters
dst_bufpointer to destination buffer (CPU memory)
src_cl_bufpointer to source OpenCL buffer
buf_sizesize in bytes of the source and destination buffers
Returns
>=0 on success, a negative error code in case of failure

Definition at line 690 of file opencl.c.

int av_opencl_buffer_write_image ( cl_mem  dst_cl_buf,
size_t  cl_buffer_size,
int  dst_cl_offset,
uint8_t **  src_data,
int *  plane_size,
int  plane_num 
)

Write image data from memory to OpenCL buffer.

The source must be an array of pointers to image plane buffers.

Parameters
dst_cl_bufpointer to destination OpenCL buffer
dst_cl_buf_sizesize in bytes of OpenCL buffer
dst_cl_buf_offsetthe offset of the OpenCL buffer start position
src_dataarray of pointers to source plane buffers
src_plane_sizesarray of sizes in bytes of the source plane buffers
src_plane_numnumber of source image planes
Returns
>=0 on success, a negative error code in case of failure

Definition at line 713 of file opencl.c.

Referenced by ff_opencl_deshake_process_inout_buf(), and ff_opencl_unsharp_process_inout_buf().

int av_opencl_buffer_read_image ( uint8_t **  dst_data,
int *  plane_size,
int  plane_num,
cl_mem  src_cl_buf,
size_t  cl_buffer_size 
)

Read image data from OpenCL buffer.

Parameters
dst_dataarray of pointers to destination plane buffers
dst_plane_sizesarray of pointers to destination plane buffers
dst_plane_numnumber of destination image planes
src_cl_bufpointer to source OpenCL buffer
src_cl_buf_sizesize in bytes of OpenCL buffer
Returns
>=0 on success, a negative error code in case of failure

Definition at line 754 of file opencl.c.

Referenced by ff_opencl_apply_unsharp(), and ff_opencl_transform().

int64_t av_opencl_benchmark ( AVOpenCLDeviceNode device,
cl_platform_id  platform,
int64_t(*)(AVOpenCLExternalEnv *ext_opencl_env)  benchmark 
)

Benchmark an OpenCL device with a user defined callback function.

This function sets up an external OpenCL environment including context and command queue on the device then tears it down in the end. The callback function should perform the rest of the work.

Parameters
devicepointer to the OpenCL device to be used
platformcl_platform_id handle to which the device belongs to
benchmarkcallback function to perform the benchmark, return a negative value in case of failure
Returns
the score passed from the callback function, a negative error code in case of failure

Definition at line 797 of file opencl.c.

Referenced by opt_opencl_bench().

Variable Documentation

const AVOption opencl_options[]
static
Initial value:
= {
{ "platform_idx", "set platform index value", OFFSET(platform_idx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX},
{ "device_idx", "set device index value", OFFSET(device_idx), AV_OPT_TYPE_INT, {.i64=-1}, -1, INT_MAX},
{ "build_options", "build options of opencl", OFFSET(build_options), AV_OPT_TYPE_STRING, {.str="-I."}, CHAR_MIN, CHAR_MAX},
{ NULL }
}

Definition at line 86 of file opencl.c.

const AVClass openclutils_class
static
Initial value:
= {
.class_name = "OPENCLUTILS",
.option = opencl_options,
.item_name = av_default_item_name,
.log_level_offset_offset = 0x42,
.parent_log_context_offset = 0x42,
}

Definition at line 95 of file opencl.c.

OpenclContext opencl_ctx = {&openclutils_class}
static

Definition at line 104 of file opencl.c.

const cl_device_type device_type[] = {CL_DEVICE_TYPE_GPU, CL_DEVICE_TYPE_CPU}
static

Definition at line 106 of file opencl.c.

Referenced by get_device_list().

const OpenclErrorMsg opencl_err_msg[]
static

Definition at line 113 of file opencl.c.