]> git.sesse.net Git - ffmpeg/blob - libavfilter/deshake_opencl.c
Merge commit 'c2cb01d418dd18e1cf997c038d37378d773121be'
[ffmpeg] / libavfilter / deshake_opencl.c
1 /*
2  * Copyright (C) 2013 Wei Gao <weigao@multicorewareinc.com>
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /**
22  * @file
23  * transform input video
24  */
25
26 #include "libavutil/common.h"
27 #include "libavutil/dict.h"
28 #include "libavutil/pixdesc.h"
29 #include "deshake_opencl.h"
30
31 #define MATRIX_SIZE 6
32 #define PLANE_NUM 3
33
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;                                                                             \
39     }
40
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;                                                                             \
46     }
47
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)
53 {
54     int arg_no, ret = 0;
55     const size_t global_work_size = width * height + 2 * ch * cw;
56     cl_kernel kernel;
57     cl_int status;
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));
60     if (ret < 0)
61         return ret;
62     ret = av_opencl_buffer_write(deshake->opencl_ctx.cl_matrix_uv, (uint8_t *)matrix_uv, deshake->opencl_ctx.matrix_size * sizeof(cl_float));
63     if (ret < 0)
64         return ret;
65     kernel = deshake->opencl_ctx.kernel_env.kernel;
66     arg_no = 0;
67
68     if ((unsigned int)interpolate > INTERPOLATE_BIQUADRATIC) {
69         av_log(ctx, AV_LOG_ERROR, "Selected interpolate method is invalid\n");
70         return AVERROR(EINVAL);
71     }
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);
92     if (ret < 0)
93         return ret;
94     return ret;
95 }
96
97 int ff_opencl_deshake_init(AVFilterContext *ctx)
98 {
99     int ret = 0;
100     DeshakeContext *deshake = ctx->priv;
101     ret = av_opencl_init(NULL);
102     if (ret < 0)
103         return ret;
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);
108     if (ret < 0)
109         return ret;
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);
112     if (ret < 0)
113         return ret;
114     if (!deshake->opencl_ctx.kernel_env.kernel) {
115         ret =  av_opencl_create_kernel(&deshake->opencl_ctx.kernel_env, "avfilter_transform");
116         if (ret < 0) {
117             av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel for name 'avfilter_transform'\n");
118             return ret;
119         }
120     }
121     return ret;
122 }
123
124 void ff_opencl_deshake_uninit(AVFilterContext *ctx)
125 {
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);
132     av_opencl_uninit();
133 }
134
135
136 int ff_opencl_deshake_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
137 {
138     int ret = 0;
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);
142
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);
160             if (ret < 0)
161                 return ret;
162         }
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);
167             if (ret < 0)
168                 return ret;
169         }
170     }
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);
175     if(ret < 0)
176         return ret;
177     return ret;
178 }