int ff_opencl_filter_load_program_from_file(AVFilterContext *avctx, const char *filename) { FILE *file; char *src = NULL; size_t pos, len, rb; const char *src_const; int err; file = fopen(filename, "r"); if (!file) { av_log(avctx, AV_LOG_ERROR, "Unable to open program " "source file \"%s\".\n", filename); return AVERROR(ENOENT); } len = 1 << 16; pos = 0; err = av_reallocp(&src, len); if (err < 0) goto fail; err = snprintf(src, len, "#line 1 \"%s\"\n", filename); if (err < 0) { err = AVERROR(errno); goto fail; } if (err > len / 2) { err = AVERROR(EINVAL); goto fail; } pos = err; while (1) { rb = fread(src + pos, 1, len - pos - 1, file); if (rb == 0 && ferror(file)) { err = AVERROR(EIO); goto fail; } pos += rb; if (pos < len) break; len <<= 1; err = av_reallocp(&src, len); if (err < 0) goto fail; } src[pos] = 0; src_const = src; err = ff_opencl_filter_load_program(avctx, &src_const, 1); fail: fclose(file); av_freep(&src); return err; }
static int colorkey_opencl_init(AVFilterContext *avctx) { ColorkeyOpenCLContext *ctx = avctx->priv; cl_int cle; int err; err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_colorkey, 1); if (err < 0) goto fail; ctx->command_queue = clCreateCommandQueue( ctx->ocf.hwctx->context, ctx->ocf.hwctx->device_id, 0, &cle ); CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL command queue %d.\n", cle); if (ctx->blend > 0.0001) { ctx->kernel_colorkey = clCreateKernel(ctx->ocf.program, "colorkey_blend", &cle); CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create colorkey_blend kernel: %d.\n", cle); } else { ctx->kernel_colorkey = clCreateKernel(ctx->ocf.program, "colorkey", &cle); CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create colorkey kernel: %d.\n", cle); } for (int i = 0; i < 4; ++i) { ctx->colorkey_rgba_float.s[i] = (float)ctx->colorkey_rgba[i] / 255.0; } ctx->initialized = 1; return 0; fail: if (ctx->command_queue) clReleaseCommandQueue(ctx->command_queue); if (ctx->kernel_colorkey) clReleaseKernel(ctx->kernel_colorkey); return err; }
static int convolution_opencl_init(AVFilterContext *avctx) { ConvolutionOpenCLContext *ctx = avctx->priv; cl_int cle; int err; err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_convolution, 1); if (err < 0) goto fail; ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context, ctx->ocf.hwctx->device_id, 0, &cle); if (!ctx->command_queue) { av_log(avctx, AV_LOG_ERROR, "Failed to create OpenCL " "command queue: %d.\n", cle); err = AVERROR(EIO); goto fail; } ctx->kernel = clCreateKernel(ctx->ocf.program, "convolution_global", &cle); if (!ctx->kernel) { av_log(avctx, AV_LOG_ERROR, "Failed to create kernel: %d.\n", cle); err = AVERROR(EIO); goto fail; } ctx->initialised = 1; return 0; fail: if (ctx->command_queue) clReleaseCommandQueue(ctx->command_queue); if (ctx->kernel) clReleaseKernel(ctx->kernel); return err; }
static int neighbor_opencl_init(AVFilterContext *avctx) { NeighborOpenCLContext *ctx = avctx->priv; const char *kernel_name; cl_int cle; int err; err = ff_opencl_filter_load_program(avctx, &ff_opencl_source_neighbor, 1); if (err < 0) goto fail; ctx->command_queue = clCreateCommandQueue(ctx->ocf.hwctx->context, ctx->ocf.hwctx->device_id, 0, &cle); CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create OpenCL " "command queue %d.\n", cle); if (!strcmp(avctx->filter->name, "erosion_opencl")){ kernel_name = "erosion_global"; } else if (!strcmp(avctx->filter->name, "dilation_opencl")){ kernel_name = "dilation_global"; } ctx->kernel = clCreateKernel(ctx->ocf.program, kernel_name, &cle); CL_FAIL_ON_ERROR(AVERROR(EIO), "Failed to create " "kernel %d.\n", cle); ctx->initialised = 1; return 0; fail: if (ctx->command_queue) clReleaseCommandQueue(ctx->command_queue); if (ctx->kernel) clReleaseKernel(ctx->kernel); return err; }