2 * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.com>
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * transform input video
26 #include "libavutil/common.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/pixdesc.h"
29 #include "deshake_opencl.h"
30 #include "libavutil/opencl_internal.h"
35 int ff_opencl_transform(AVFilterContext *ctx,
36 int width, int height, int cw, int ch,
37 const float *matrix_y, const float *matrix_uv,
38 enum InterpolateMethod interpolate,
39 enum FillMethod fill, AVFrame *in, AVFrame *out)
42 const size_t global_work_size = width * height + 2 * ch * cw;
44 DeshakeContext *deshake = ctx->priv;
45 FFOpenclParam opencl_param = {0};
47 opencl_param.ctx = ctx;
48 opencl_param.kernel = deshake->opencl_ctx.kernel;
49 ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_y, (uint8_t *)matrix_y, deshake->opencl_ctx.matrix_size * sizeof(cl_float));
52 ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_uv, (uint8_t *)matrix_uv, deshake->opencl_ctx.matrix_size * sizeof(cl_float));
56 if ((unsigned int)interpolate > INTERPOLATE_BIQUADRATIC) {
57 av_log(ctx, AV_LOG_ERROR, "Selected interpolate method is invalid\n");
58 return AVERROR(EINVAL);
60 ret = ff_opencl_set_parameter(&opencl_param,
61 FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_inbuf),
62 FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_outbuf),
63 FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_matrix_y),
64 FF_OPENCL_PARAM_INFO(deshake->opencl_ctx.cl_matrix_uv),
65 FF_OPENCL_PARAM_INFO(interpolate),
66 FF_OPENCL_PARAM_INFO(fill),
67 FF_OPENCL_PARAM_INFO(in->linesize[0]),
68 FF_OPENCL_PARAM_INFO(out->linesize[0]),
69 FF_OPENCL_PARAM_INFO(in->linesize[1]),
70 FF_OPENCL_PARAM_INFO(out->linesize[1]),
71 FF_OPENCL_PARAM_INFO(height),
72 FF_OPENCL_PARAM_INFO(width),
73 FF_OPENCL_PARAM_INFO(ch),
74 FF_OPENCL_PARAM_INFO(cw),
78 status = clEnqueueNDRangeKernel(deshake->opencl_ctx.command_queue,
79 deshake->opencl_ctx.kernel, 1, NULL,
80 &global_work_size, NULL, 0, NULL, NULL);
81 if (status != CL_SUCCESS) {
82 av_log(ctx, AV_LOG_ERROR, "OpenCL run kernel error occurred: %s\n", av_opencl_errstr(status));
83 return AVERROR_EXTERNAL;
85 clFinish(deshake->opencl_ctx.command_queue);
86 ret = av_opencl_buffer_read_image(out->data, deshake->opencl_ctx.out_plane_size,
87 deshake->opencl_ctx.plane_num, deshake->opencl_ctx.cl_outbuf,
88 deshake->opencl_ctx.cl_outbuf_size);
94 int ff_opencl_deshake_init(AVFilterContext *ctx)
97 DeshakeContext *deshake = ctx->priv;
98 ret = av_opencl_init(NULL);
101 deshake->opencl_ctx.matrix_size = MATRIX_SIZE;
102 deshake->opencl_ctx.plane_num = PLANE_NUM;
103 ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_matrix_y,
104 deshake->opencl_ctx.matrix_size*sizeof(cl_float), CL_MEM_READ_ONLY, NULL);
107 ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_matrix_uv,
108 deshake->opencl_ctx.matrix_size*sizeof(cl_float), CL_MEM_READ_ONLY, NULL);
111 deshake->opencl_ctx.command_queue = av_opencl_get_command_queue();
112 if (!deshake->opencl_ctx.command_queue) {
113 av_log(ctx, AV_LOG_ERROR, "Unable to get OpenCL command queue in filter 'deshake'\n");
114 return AVERROR(EINVAL);
116 deshake->opencl_ctx.program = av_opencl_compile("avfilter_transform", NULL);
117 if (!deshake->opencl_ctx.program) {
118 av_log(ctx, AV_LOG_ERROR, "OpenCL failed to compile program 'avfilter_transform'\n");
119 return AVERROR(EINVAL);
121 if (!deshake->opencl_ctx.kernel) {
122 deshake->opencl_ctx.kernel = clCreateKernel(deshake->opencl_ctx.program, "avfilter_transform", &ret);
123 if (ret != CL_SUCCESS) {
124 av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel 'avfilter_transform'\n");
125 return AVERROR(EINVAL);
131 void ff_opencl_deshake_uninit(AVFilterContext *ctx)
133 DeshakeContext *deshake = ctx->priv;
134 av_opencl_buffer_release(&deshake->opencl_ctx.cl_inbuf);
135 av_opencl_buffer_release(&deshake->opencl_ctx.cl_outbuf);
136 av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_y);
137 av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_uv);
138 clReleaseKernel(deshake->opencl_ctx.kernel);
139 clReleaseProgram(deshake->opencl_ctx.program);
140 deshake->opencl_ctx.command_queue = NULL;
144 int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
147 AVFilterLink *link = ctx->inputs[0];
148 DeshakeContext *deshake = ctx->priv;
149 const int hshift = av_pix_fmt_desc_get(link->format)->log2_chroma_h;
150 int chroma_height = FF_CEIL_RSHIFT(link->h, hshift);
152 if ((!deshake->opencl_ctx.cl_inbuf) || (!deshake->opencl_ctx.cl_outbuf)) {
153 deshake->opencl_ctx.in_plane_size[0] = (in->linesize[0] * in->height);
154 deshake->opencl_ctx.in_plane_size[1] = (in->linesize[1] * chroma_height);
155 deshake->opencl_ctx.in_plane_size[2] = (in->linesize[2] * chroma_height);
156 deshake->opencl_ctx.out_plane_size[0] = (out->linesize[0] * out->height);
157 deshake->opencl_ctx.out_plane_size[1] = (out->linesize[1] * chroma_height);
158 deshake->opencl_ctx.out_plane_size[2] = (out->linesize[2] * chroma_height);
159 deshake->opencl_ctx.cl_inbuf_size = deshake->opencl_ctx.in_plane_size[0] +
160 deshake->opencl_ctx.in_plane_size[1] +
161 deshake->opencl_ctx.in_plane_size[2];
162 deshake->opencl_ctx.cl_outbuf_size = deshake->opencl_ctx.out_plane_size[0] +
163 deshake->opencl_ctx.out_plane_size[1] +
164 deshake->opencl_ctx.out_plane_size[2];
165 if (!deshake->opencl_ctx.cl_inbuf) {
166 ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_inbuf,
167 deshake->opencl_ctx.cl_inbuf_size,
168 CL_MEM_READ_ONLY, NULL);
172 if (!deshake->opencl_ctx.cl_outbuf) {
173 ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_outbuf,
174 deshake->opencl_ctx.cl_outbuf_size,
175 CL_MEM_READ_WRITE, NULL);
180 ret = av_opencl_buffer_write_image(deshake->opencl_ctx.cl_inbuf,
181 deshake->opencl_ctx.cl_inbuf_size,
182 0, in->data,deshake->opencl_ctx.in_plane_size,
183 deshake->opencl_ctx.plane_num);