yading@11: /* yading@11: * Copyright (C) 2012 Peng Gao yading@11: * Copyright (C) 2012 Li Cao yading@11: * Copyright (C) 2012 Wei Gao yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * OpenCL wrapper yading@11: * yading@11: * This interface is considered still experimental and its API and ABI may yading@11: * change without prior notice. yading@11: */ yading@11: yading@11: #ifndef LIBAVUTIL_OPENCL_H yading@11: #define LIBAVUTIL_OPENCL_H yading@11: yading@11: #include yading@11: #include "config.h" yading@11: #include "dict.h" yading@11: yading@11: #define AV_OPENCL_KERNEL( ... )# __VA_ARGS__ yading@11: yading@11: #define AV_OPENCL_MAX_KERNEL_NAME_SIZE 150 yading@11: yading@11: #define AV_OPENCL_MAX_DEVICE_NAME_SIZE 100 yading@11: yading@11: #define AV_OPENCL_MAX_PLATFORM_NAME_SIZE 100 yading@11: yading@11: typedef struct { yading@11: int device_type; yading@11: char device_name[AV_OPENCL_MAX_DEVICE_NAME_SIZE]; yading@11: cl_device_id device_id; yading@11: } AVOpenCLDeviceNode; yading@11: yading@11: typedef struct { yading@11: cl_platform_id platform_id; yading@11: char platform_name[AV_OPENCL_MAX_PLATFORM_NAME_SIZE]; yading@11: int device_num; yading@11: AVOpenCLDeviceNode **device_node; yading@11: } AVOpenCLPlatformNode; yading@11: yading@11: typedef struct { yading@11: int platform_num; yading@11: AVOpenCLPlatformNode **platform_node; yading@11: } AVOpenCLDeviceList; yading@11: yading@11: typedef struct { yading@11: cl_command_queue command_queue; yading@11: cl_kernel kernel; yading@11: char kernel_name[AV_OPENCL_MAX_KERNEL_NAME_SIZE]; yading@11: } AVOpenCLKernelEnv; yading@11: yading@11: typedef struct { yading@11: cl_platform_id platform_id; yading@11: cl_device_type device_type; yading@11: cl_context context; yading@11: cl_device_id device_id; yading@11: cl_command_queue command_queue; yading@11: char *platform_name; yading@11: } AVOpenCLExternalEnv; yading@11: yading@11: /** yading@11: * Get OpenCL device list. yading@11: * yading@11: * It must be freed with av_opencl_free_device_list(). yading@11: * yading@11: * @param device_list pointer to OpenCL environment device list, yading@11: * should be released by av_opencl_free_device_list() yading@11: * yading@11: * @return >=0 on success, a negative error code in case of failure yading@11: */ yading@11: int av_opencl_get_device_list(AVOpenCLDeviceList **device_list); yading@11: yading@11: /** yading@11: * Free OpenCL device list. yading@11: * yading@11: * @param device_list pointer to OpenCL environment device list yading@11: * created by av_opencl_get_device_list() yading@11: */ yading@11: void av_opencl_free_device_list(AVOpenCLDeviceList **device_list); yading@11: yading@11: /** yading@11: * Set option in the global OpenCL context. yading@11: * yading@11: * This options affect the operation performed by the next yading@11: * av_opencl_init() operation. yading@11: * yading@11: * The currently accepted options are: yading@11: * - build_options: set options to compile registered kernels code yading@11: * - platform: set index of platform in device list yading@11: * - device: set index of device in device list yading@11: * yading@11: * See reference "OpenCL Specification Version: 1.2 chapter 5.6.4". yading@11: * yading@11: * @param key option key yading@11: * @param val option value yading@11: * @return >=0 on success, a negative error code in case of failure yading@11: * @see av_opencl_get_option() yading@11: */ yading@11: int av_opencl_set_option(const char *key, const char *val); yading@11: yading@11: /** yading@11: * Get option value from the global OpenCL context. yading@11: * yading@11: * @param key option key yading@11: * @param out_val pointer to location where option value will be yading@11: * written, must be freed with av_freep() yading@11: * @return >=0 on success, a negative error code in case of failure yading@11: * @see av_opencl_set_option() yading@11: */ yading@11: int av_opencl_get_option(const char *key, uint8_t **out_val); yading@11: yading@11: /** yading@11: * Free option values of the global OpenCL context. yading@11: * yading@11: */ yading@11: void av_opencl_free_option(void); yading@11: yading@11: /** yading@11: * Allocate OpenCL external environment. yading@11: * yading@11: * It must be freed with av_opencl_free_external_env(). yading@11: * yading@11: * @return pointer to allocated OpenCL external environment yading@11: */ yading@11: AVOpenCLExternalEnv *av_opencl_alloc_external_env(void); yading@11: yading@11: /** yading@11: * Free OpenCL external environment. yading@11: * yading@11: * @param ext_opencl_env pointer to OpenCL external environment yading@11: * created by av_opencl_alloc_external_env() yading@11: */ yading@11: void av_opencl_free_external_env(AVOpenCLExternalEnv **ext_opencl_env); yading@11: yading@11: /** yading@11: * Register kernel code. yading@11: * yading@11: * The registered kernel code is stored in a global context, and compiled yading@11: * in the runtime environment when av_opencl_init() is called. yading@11: * yading@11: * @param kernel_code kernel code to be compiled in the OpenCL runtime environment yading@11: * @return >=0 on success, a negative error code in case of failure yading@11: */ yading@11: int av_opencl_register_kernel_code(const char *kernel_code); yading@11: yading@11: /** yading@11: * Initialize the run time OpenCL environment and compile the kernel yading@11: * code registered with av_opencl_register_kernel_code(). yading@11: * yading@11: * @param ext_opencl_env external OpenCL environment, created by an yading@11: * application program, ignored if set to NULL yading@11: * @return >=0 on success, a negative error code in case of failure yading@11: */ yading@11: int av_opencl_init(AVOpenCLExternalEnv *ext_opencl_env); yading@11: yading@11: /** yading@11: * Create kernel object in the specified kernel environment. yading@11: * yading@11: * @param env pointer to kernel environment which is filled with yading@11: * the environment used to run the kernel yading@11: * @param kernel_name kernel function name yading@11: * @return >=0 on success, a negative error code in case of failure yading@11: */ yading@11: int av_opencl_create_kernel(AVOpenCLKernelEnv *env, const char *kernel_name); yading@11: yading@11: /** yading@11: * Create OpenCL buffer. yading@11: * yading@11: * The buffer is used to save the data used or created by an OpenCL yading@11: * kernel. yading@11: * The created buffer must be released with av_opencl_buffer_release(). yading@11: * yading@11: * See clCreateBuffer() function reference for more information about yading@11: * the parameters. yading@11: * yading@11: * @param cl_buf pointer to OpenCL buffer yading@11: * @param cl_buf_size size in bytes of the OpenCL buffer to create yading@11: * @param flags flags used to control buffer attributes yading@11: * @param host_ptr host pointer of the OpenCL buffer yading@11: * @return >=0 on success, a negative error code in case of failure yading@11: */ yading@11: int av_opencl_buffer_create(cl_mem *cl_buf, size_t cl_buf_size, int flags, void *host_ptr); yading@11: yading@11: /** yading@11: * Write OpenCL buffer with data from src_buf. yading@11: * yading@11: * @param dst_cl_buf pointer to OpenCL destination buffer yading@11: * @param src_buf pointer to source buffer yading@11: * @param buf_size size in bytes of the source and destination buffers yading@11: * @return >=0 on success, a negative error code in case of failure yading@11: */ yading@11: int av_opencl_buffer_write(cl_mem dst_cl_buf, uint8_t *src_buf, size_t buf_size); yading@11: yading@11: /** yading@11: * Read data from OpenCL buffer to memory buffer. yading@11: * yading@11: * @param dst_buf pointer to destination buffer (CPU memory) yading@11: * @param src_cl_buf pointer to source OpenCL buffer yading@11: * @param buf_size size in bytes of the source and destination buffers yading@11: * @return >=0 on success, a negative error code in case of failure yading@11: */ yading@11: int av_opencl_buffer_read(uint8_t *dst_buf, cl_mem src_cl_buf, size_t buf_size); yading@11: yading@11: /** yading@11: * Write image data from memory to OpenCL buffer. yading@11: * yading@11: * The source must be an array of pointers to image plane buffers. yading@11: * yading@11: * @param dst_cl_buf pointer to destination OpenCL buffer yading@11: * @param dst_cl_buf_size size in bytes of OpenCL buffer yading@11: * @param dst_cl_buf_offset the offset of the OpenCL buffer start position yading@11: * @param src_data array of pointers to source plane buffers yading@11: * @param src_plane_sizes array of sizes in bytes of the source plane buffers yading@11: * @param src_plane_num number of source image planes yading@11: * @return >=0 on success, a negative error code in case of failure yading@11: */ yading@11: int av_opencl_buffer_write_image(cl_mem dst_cl_buf, size_t cl_buffer_size, int dst_cl_offset, yading@11: uint8_t **src_data, int *plane_size, int plane_num); yading@11: yading@11: /** yading@11: * Read image data from OpenCL buffer. yading@11: * yading@11: * @param dst_data array of pointers to destination plane buffers yading@11: * @param dst_plane_sizes array of pointers to destination plane buffers yading@11: * @param dst_plane_num number of destination image planes yading@11: * @param src_cl_buf pointer to source OpenCL buffer yading@11: * @param src_cl_buf_size size in bytes of OpenCL buffer yading@11: * @return >=0 on success, a negative error code in case of failure yading@11: */ yading@11: int av_opencl_buffer_read_image(uint8_t **dst_data, int *plane_size, int plane_num, yading@11: cl_mem src_cl_buf, size_t cl_buffer_size); yading@11: yading@11: /** yading@11: * Release OpenCL buffer. yading@11: * yading@11: * @param cl_buf pointer to OpenCL buffer to release, which was yading@11: * previously filled with av_opencl_buffer_create() yading@11: */ yading@11: void av_opencl_buffer_release(cl_mem *cl_buf); yading@11: yading@11: /** yading@11: * Release kernel object. yading@11: * yading@11: * @param env kernel environment where the kernel object was created yading@11: * with av_opencl_create_kernel() yading@11: */ yading@11: void av_opencl_release_kernel(AVOpenCLKernelEnv *env); yading@11: yading@11: /** yading@11: * Release OpenCL environment. yading@11: * yading@11: * The OpenCL environment is effectively released only if all the created yading@11: * kernels had been released with av_opencl_release_kernel(). yading@11: */ yading@11: void av_opencl_uninit(void); yading@11: yading@11: #endif /* LIBAVUTIL_OPENCL_H */