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"
34 #define TRANSFORM_OPENCL_CHECK(method, ...) \
35 status = method(__VA_ARGS__); \
36 if (status != CL_SUCCESS) { \
37 av_log(ctx, AV_LOG_ERROR, "error %s %d\n", # method, status); \
38 return AVERROR_EXTERNAL; \
41 #define TRANSFORM_OPENCL_SET_KERNEL_ARG(arg_ptr) \
42 status = clSetKernelArg((kernel),(arg_no++),(sizeof(arg_ptr)),(void*)(&(arg_ptr))); \
43 if (status != CL_SUCCESS) { \
44 av_log(ctx, AV_LOG_ERROR, "cannot set kernel argument: %d\n", status ); \
45 return AVERROR_EXTERNAL; \
48 int ff_opencl_transform(AVFilterContext *ctx,
49 int width, int height, int cw, int ch,
50 const float *matrix_y, const float *matrix_uv,
51 enum InterpolateMethod interpolate,
52 enum FillMethod fill, AVFrame *in, AVFrame *out)
55 const size_t global_work_size = width * height + 2 * ch * cw;
58 DeshakeContext *deshake = ctx->priv;
59 ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_y, (uint8_t *)matrix_y, deshake->opencl_ctx.matrix_size * sizeof(cl_float));
62 ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_uv, (uint8_t *)matrix_uv, deshake->opencl_ctx.matrix_size * sizeof(cl_float));
65 kernel = deshake->opencl_ctx.kernel_env.kernel;
68 if ((unsigned int)interpolate > INTERPOLATE_BIQUADRATIC) {
69 av_log(ctx, AV_LOG_ERROR, "Selected interpolate method is invalid\n");
70 return AVERROR(EINVAL);
72 TRANSFORM_OPENCL_SET_KERNEL_ARG(deshake->opencl_ctx.cl_inbuf);
73 TRANSFORM_OPENCL_SET_KERNEL_ARG(deshake->opencl_ctx.cl_outbuf);
74 TRANSFORM_OPENCL_SET_KERNEL_ARG(deshake->opencl_ctx.cl_matrix_y);
75 TRANSFORM_OPENCL_SET_KERNEL_ARG(deshake->opencl_ctx.cl_matrix_uv);
76 TRANSFORM_OPENCL_SET_KERNEL_ARG(interpolate);
77 TRANSFORM_OPENCL_SET_KERNEL_ARG(fill);
78 TRANSFORM_OPENCL_SET_KERNEL_ARG(in->linesize[0]);
79 TRANSFORM_OPENCL_SET_KERNEL_ARG(out->linesize[0]);
80 TRANSFORM_OPENCL_SET_KERNEL_ARG(in->linesize[1]);
81 TRANSFORM_OPENCL_SET_KERNEL_ARG(out->linesize[1]);
82 TRANSFORM_OPENCL_SET_KERNEL_ARG(height);
83 TRANSFORM_OPENCL_SET_KERNEL_ARG(width);
84 TRANSFORM_OPENCL_SET_KERNEL_ARG(ch);
85 TRANSFORM_OPENCL_SET_KERNEL_ARG(cw);
86 TRANSFORM_OPENCL_CHECK(clEnqueueNDRangeKernel, deshake->opencl_ctx.kernel_env.command_queue, deshake->opencl_ctx.kernel_env.kernel, 1, NULL,
87 &global_work_size, NULL, 0, NULL, NULL);
88 clFinish(deshake->opencl_ctx.kernel_env.command_queue);
89 ret = av_opencl_buffer_read_image(out->data, deshake->opencl_ctx.out_plane_size,
90 deshake->opencl_ctx.plane_num, deshake->opencl_ctx.cl_outbuf,
91 deshake->opencl_ctx.cl_outbuf_size);
97 int ff_opencl_deshake_init(AVFilterContext *ctx)
100 DeshakeContext *deshake = ctx->priv;
101 ret = av_opencl_init(NULL);
104 deshake->opencl_ctx.matrix_size = MATRIX_SIZE;
105 deshake->opencl_ctx.plane_num = PLANE_NUM;
106 ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_matrix_y,
107 deshake->opencl_ctx.matrix_size*sizeof(cl_float), CL_MEM_READ_ONLY, NULL);
110 ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_matrix_uv,
111 deshake->opencl_ctx.matrix_size*sizeof(cl_float), CL_MEM_READ_ONLY, NULL);
114 if (!deshake->opencl_ctx.kernel_env.kernel) {
115 ret = av_opencl_create_kernel(&deshake->opencl_ctx.kernel_env, "avfilter_transform");
117 av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel for name 'avfilter_transform'\n");
124 void ff_opencl_deshake_uninit(AVFilterContext *ctx)
126 DeshakeContext *deshake = ctx->priv;
127 av_opencl_buffer_release(&deshake->opencl_ctx.cl_inbuf);
128 av_opencl_buffer_release(&deshake->opencl_ctx.cl_outbuf);
129 av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_y);
130 av_opencl_buffer_release(&deshake->opencl_ctx.cl_matrix_uv);
131 av_opencl_release_kernel(&deshake->opencl_ctx.kernel_env);
136 int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
139 AVFilterLink *link = ctx->inputs[0];
140 DeshakeContext *deshake = ctx->priv;
141 int chroma_height = -((-link->h) >> av_pix_fmt_desc_get(link->format)->log2_chroma_h);
143 if ((!deshake->opencl_ctx.cl_inbuf) || (!deshake->opencl_ctx.cl_outbuf)) {
144 deshake->opencl_ctx.in_plane_size[0] = (in->linesize[0] * in->height);
145 deshake->opencl_ctx.in_plane_size[1] = (in->linesize[1] * chroma_height);
146 deshake->opencl_ctx.in_plane_size[2] = (in->linesize[2] * chroma_height);
147 deshake->opencl_ctx.out_plane_size[0] = (out->linesize[0] * out->height);
148 deshake->opencl_ctx.out_plane_size[1] = (out->linesize[1] * chroma_height);
149 deshake->opencl_ctx.out_plane_size[2] = (out->linesize[2] * chroma_height);
150 deshake->opencl_ctx.cl_inbuf_size = deshake->opencl_ctx.in_plane_size[0] +
151 deshake->opencl_ctx.in_plane_size[1] +
152 deshake->opencl_ctx.in_plane_size[2];
153 deshake->opencl_ctx.cl_outbuf_size = deshake->opencl_ctx.out_plane_size[0] +
154 deshake->opencl_ctx.out_plane_size[1] +
155 deshake->opencl_ctx.out_plane_size[2];
156 if (!deshake->opencl_ctx.cl_inbuf) {
157 ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_inbuf,
158 deshake->opencl_ctx.cl_inbuf_size,
159 CL_MEM_READ_ONLY, NULL);
163 if (!deshake->opencl_ctx.cl_outbuf) {
164 ret = av_opencl_buffer_create(&deshake->opencl_ctx.cl_outbuf,
165 deshake->opencl_ctx.cl_outbuf_size,
166 CL_MEM_READ_WRITE, NULL);
171 ret = av_opencl_buffer_write_image(deshake->opencl_ctx.cl_inbuf,
172 deshake->opencl_ctx.cl_inbuf_size,
173 0, in->data,deshake->opencl_ctx.in_plane_size,
174 deshake->opencl_ctx.plane_num);