FFmpeg
Data Structures | Macros | Functions
opencl.h File Reference
#include "libavutil/bprint.h"
#include "libavutil/buffer.h"
#include "libavutil/hwcontext.h"
#include "libavutil/hwcontext_opencl.h"
#include "libavutil/pixfmt.h"
#include "avfilter.h"

Go to the source code of this file.

Data Structures

struct  OpenCLFilterContext
 
struct  OpenCLKernelArg
 

Macros

#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
 
#define CL_SET_KERNEL_ARG(kernel, arg_num, type, arg)
 set argument to specific Kernel. More...
 
#define CL_FAIL_ON_ERROR(errcode, ...)
 A helper macro to handle OpenCL errors. More...
 
#define CL_CREATE_KERNEL(ctx, kernel_name)
 Create a kernel with the given name. More...
 
#define CL_RELEASE_KERNEL(k)
 release an OpenCL Kernel More...
 
#define CL_RELEASE_MEMORY(m)
 release an OpenCL Memory Object More...
 
#define CL_RELEASE_QUEUE(q)
 release an OpenCL Command Queue More...
 
#define CL_ENQUEUE_KERNEL_WITH_ARGS(queue, kernel, global_work_size, local_work_size, event, ...)
 Enqueue a kernel with the given information. More...
 
#define CL_RUN_KERNEL_WITH_ARGS(queue, kernel, global_work_size, local_work_size, event, ...)
 Uses the above macro to enqueue the given kernel and then additionally runs it to completion via clFinish. More...
 
#define CL_CREATE_BUFFER_FLAGS(ctx, buffer_name, flags, size, host_ptr)
 Create a buffer with the given information. More...
 
#define CL_BLOCKING_WRITE_BUFFER(queue, buffer, size, host_ptr, event)
 Perform a blocking write to a buffer. More...
 
#define CL_CREATE_BUFFER(ctx, buffer_name, size)   CL_CREATE_BUFFER_FLAGS(ctx, buffer_name, 0, size, NULL)
 Create a buffer with the given information. More...
 

Functions

int ff_opencl_filter_config_input (AVFilterLink *inlink)
 Check that the input link contains a suitable hardware frames context and extract the device from it. More...
 
int ff_opencl_filter_config_output (AVFilterLink *outlink)
 Create a suitable hardware frames context for the output. More...
 
int ff_opencl_filter_init (AVFilterContext *avctx)
 Initialise an OpenCL filter context. More...
 
void ff_opencl_filter_uninit (AVFilterContext *avctx)
 Uninitialise an OpenCL filter context. More...
 
int ff_opencl_filter_load_program (AVFilterContext *avctx, const char **program_source_array, int nb_strings)
 Load a new OpenCL program from strings in memory. More...
 
int ff_opencl_filter_load_program_from_file (AVFilterContext *avctx, const char *filename)
 Load a new OpenCL program from a file. More...
 
int ff_opencl_filter_work_size_from_image (AVFilterContext *avctx, size_t *work_size, AVFrame *frame, int plane, int block_alignment)
 Find the work size needed needed for a given plane of an image. More...
 
void ff_opencl_print_const_matrix_3x3 (AVBPrint *buf, const char *name_str, double mat[3][3])
 Print a 3x3 matrix into a buffer as __constant array, which could be included in an OpenCL program. More...
 
cl_ulong ff_opencl_get_event_time (cl_event event)
 Gets the command start and end times for the given event and returns the difference (the time that the event took). More...
 

Macro Definition Documentation

◆ CL_USE_DEPRECATED_OPENCL_1_2_APIS

#define CL_USE_DEPRECATED_OPENCL_1_2_APIS

Definition at line 26 of file opencl.h.

◆ CL_SET_KERNEL_ARG

#define CL_SET_KERNEL_ARG (   kernel,
  arg_num,
  type,
  arg 
)
Value:
cle = clSetKernelArg(kernel, arg_num, sizeof(type), arg); \
if (cle != CL_SUCCESS) { \
av_log(avctx, AV_LOG_ERROR, "Failed to set kernel " \
"argument %d: error %d.\n", arg_num, cle); \
err = AVERROR(EIO); \
goto fail; \
}

set argument to specific Kernel.

This macro relies on usage of local label "fail" and variables: avctx, cle and err.

Definition at line 61 of file opencl.h.

◆ CL_FAIL_ON_ERROR

#define CL_FAIL_ON_ERROR (   errcode,
  ... 
)
Value:
do { \
if (cle != CL_SUCCESS) { \
av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
err = errcode; \
goto fail; \
} \
} while(0)

A helper macro to handle OpenCL errors.

It will assign errcode to variable err, log error msg, and jump to fail label on error.

Definition at line 74 of file opencl.h.

◆ CL_CREATE_KERNEL

#define CL_CREATE_KERNEL (   ctx,
  kernel_name 
)
Value:
do { \
ctx->kernel_ ## kernel_name = clCreateKernel(ctx->ocf.program, #kernel_name, &cle); \
CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create %s kernel: %d.\n", #kernel_name, cle); \
} while(0)

Create a kernel with the given name.

The kernel variable in the context structure must have a name of the form kernel_<kernel_name>.

The OpenCLFilterContext variable in the context structure must be named ocf.

Requires the presence of a local cl_int variable named cle and a fail label for error handling.

Definition at line 93 of file opencl.h.

◆ CL_RELEASE_KERNEL

#define CL_RELEASE_KERNEL (   k)
Value:
do { \
if (k) { \
cle = clReleaseKernel(k); \
if (cle != CL_SUCCESS) \
av_log(avctx, AV_LOG_ERROR, "Failed to release " \
"OpenCL kernel: %d.\n", cle); \
} \
} while(0)

release an OpenCL Kernel

Definition at line 101 of file opencl.h.

◆ CL_RELEASE_MEMORY

#define CL_RELEASE_MEMORY (   m)
Value:
do { \
if (m) { \
cle = clReleaseMemObject(m); \
if (cle != CL_SUCCESS) \
av_log(avctx, AV_LOG_ERROR, "Failed to release " \
"OpenCL memory: %d.\n", cle); \
} \
} while(0)

release an OpenCL Memory Object

Definition at line 114 of file opencl.h.

◆ CL_RELEASE_QUEUE

#define CL_RELEASE_QUEUE (   q)
Value:
do { \
if (q) { \
cle = clReleaseCommandQueue(q); \
if (cle != CL_SUCCESS) \
av_log(avctx, AV_LOG_ERROR, "Failed to release " \
"OpenCL command queue: %d.\n", cle); \
} \
} while(0)

release an OpenCL Command Queue

Definition at line 127 of file opencl.h.

◆ CL_ENQUEUE_KERNEL_WITH_ARGS

#define CL_ENQUEUE_KERNEL_WITH_ARGS (   queue,
  kernel,
  global_work_size,
  local_work_size,
  event,
  ... 
)
Value:
do { \
OpenCLKernelArg args[] = {__VA_ARGS__}; \
for (int i = 0; i < FF_ARRAY_ELEMS(args); i++) { \
cle = clSetKernelArg(kernel, i, args[i].arg_size, args[i].arg_val); \
if (cle != CL_SUCCESS) { \
av_log(avctx, AV_LOG_ERROR, "Failed to set kernel " \
"argument %d: error %d.\n", i, cle); \
err = AVERROR(EIO); \
goto fail; \
} \
} \
\
cle = clEnqueueNDRangeKernel( \
queue, \
kernel, \
FF_ARRAY_ELEMS(global_work_size), \
NULL, \
global_work_size, \
local_work_size, \
0, \
NULL, \
); \
CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to enqueue kernel: %d.\n", cle); \
} while (0)

Enqueue a kernel with the given information.

Kernel arguments are provided as KernelArg structures and are set in the order that they are passed.

Requires the presence of a local cl_int variable named cle and a fail label for error handling.

Definition at line 146 of file opencl.h.

◆ CL_RUN_KERNEL_WITH_ARGS

#define CL_RUN_KERNEL_WITH_ARGS (   queue,
  kernel,
  global_work_size,
  local_work_size,
  event,
  ... 
)
Value:
do { \
CL_ENQUEUE_KERNEL_WITH_ARGS( \
queue, kernel, global_work_size, local_work_size, event, __VA_ARGS__ \
); \
\
cle = clFinish(queue); \
CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to finish command queue: %d.\n", cle); \
} while (0)

Uses the above macro to enqueue the given kernel and then additionally runs it to completion via clFinish.

Requires the presence of a local cl_int variable named cle and a fail label for error handling.

Definition at line 180 of file opencl.h.

◆ CL_CREATE_BUFFER_FLAGS

#define CL_CREATE_BUFFER_FLAGS (   ctx,
  buffer_name,
  flags,
  size,
  host_ptr 
)
Value:
do { \
ctx->buffer_name = clCreateBuffer( \
ctx->ocf.hwctx->context, \
flags, \
size, \
host_ptr, \
&cle \
); \
CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create buffer %s: %d.\n", #buffer_name, cle); \
} while(0)

Create a buffer with the given information.

The buffer variable in the context structure must be named <buffer_name>.

Requires the presence of a local cl_int variable named cle and a fail label for error handling.

Definition at line 197 of file opencl.h.

◆ CL_BLOCKING_WRITE_BUFFER

#define CL_BLOCKING_WRITE_BUFFER (   queue,
  buffer,
  size,
  host_ptr,
  event 
)
Value:
do { \
cle = clEnqueueWriteBuffer( \
queue, \
buffer, \
CL_TRUE, \
0, \
size, \
host_ptr, \
0, \
NULL, \
); \
CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to write buffer to device: %d.\n", cle); \
} while(0)

Perform a blocking write to a buffer.

Requires the presence of a local cl_int variable named cle and a fail label for error handling.

Definition at line 214 of file opencl.h.

◆ CL_CREATE_BUFFER

#define CL_CREATE_BUFFER (   ctx,
  buffer_name,
  size 
)    CL_CREATE_BUFFER_FLAGS(ctx, buffer_name, 0, size, NULL)

Create a buffer with the given information.

The buffer variable in the context structure must be named <buffer_name>.

Requires the presence of a local cl_int variable named cle and a fail label for error handling.

Definition at line 237 of file opencl.h.

Function Documentation

◆ ff_opencl_filter_config_input()

int ff_opencl_filter_config_input ( AVFilterLink inlink)

Check that the input link contains a suitable hardware frames context and extract the device from it.

Definition at line 45 of file opencl.c.

Referenced by program_opencl_init().

◆ ff_opencl_filter_config_output()

int ff_opencl_filter_config_output ( AVFilterLink outlink)

◆ ff_opencl_filter_init()

int ff_opencl_filter_init ( AVFilterContext avctx)

Initialise an OpenCL filter context.

Definition at line 132 of file opencl.c.

Referenced by overlay_opencl_init(), program_opencl_init(), and remap_opencl_init().

◆ ff_opencl_filter_uninit()

void ff_opencl_filter_uninit ( AVFilterContext avctx)

◆ ff_opencl_filter_load_program()

int ff_opencl_filter_load_program ( AVFilterContext avctx,
const char **  program_source_array,
int  nb_strings 
)

Load a new OpenCL program from strings in memory.

Creates a new program and compiles it for the current device. Will log any build errors if compilation fails.

Definition at line 156 of file opencl.c.

Referenced by avgblur_opencl_init(), colorkey_opencl_init(), convolution_opencl_init(), deshake_opencl_init(), ff_opencl_filter_load_program_from_file(), neighbor_opencl_init(), nlmeans_opencl_init(), overlay_opencl_load(), pad_opencl_init(), remap_opencl_load(), tonemap_opencl_init(), transpose_opencl_init(), unsharp_opencl_init(), and xfade_opencl_load().

◆ ff_opencl_filter_load_program_from_file()

int ff_opencl_filter_load_program_from_file ( AVFilterContext avctx,
const char *  filename 
)

Load a new OpenCL program from a file.

Same as ff_opencl_filter_load_program(), but from a file.

Definition at line 204 of file opencl.c.

Referenced by program_opencl_load(), and xfade_opencl_load().

◆ ff_opencl_filter_work_size_from_image()

int ff_opencl_filter_work_size_from_image ( AVFilterContext avctx,
size_t *  work_size,
AVFrame frame,
int  plane,
int  block_alignment 
)

◆ ff_opencl_print_const_matrix_3x3()

void ff_opencl_print_const_matrix_3x3 ( AVBPrint *  buf,
const char *  name_str,
double  mat[3][3] 
)

Print a 3x3 matrix into a buffer as __constant array, which could be included in an OpenCL program.

Definition at line 326 of file opencl.c.

Referenced by tonemap_opencl_init().

◆ ff_opencl_get_event_time()

cl_ulong ff_opencl_get_event_time ( cl_event  event)

Gets the command start and end times for the given event and returns the difference (the time that the event took).

Definition at line 339 of file opencl.c.

Referenced by filter_frame().

AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
fail
#define fail()
Definition: checkasm.h:179
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
ctx
AVFormatContext * ctx
Definition: movenc.c:48
vfw_ctx::event
HANDLE event
Definition: vfwcap.c:48
arg
const char * arg
Definition: jacosubdec.c:67
NULL
#define NULL
Definition: coverity.c:32
size
int size
Definition: twinvq_data.h:10344
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
buffer
the frame and frame reference mechanism is intended to as much as expensive copies of that data while still allowing the filters to produce correct results The data is stored in buffers represented by AVFrame structures Several references can point to the same frame buffer
Definition: filter_design.txt:49
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:482