[FFmpeg-devel] [PATCH] libavfilter/vf_convolution_opencl.c: add opencl version of libavfilter/convolution.c filter
Hi there. Thank you for your advices. I implemented 4 matrix / 4 rdiv / 4 bias option for each plane of image, fixed mem-leaks, added error messages if the matrix is invalid. ___ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Re: [FFmpeg-devel] [PATCH] libavfilter/vf_convolution_opencl.c: add opencl version of libavfilter/convolution.c filter
Hi there. Thank you for your advices. I implemented 4 matrix / 4 rdiv / 4 bias support, fixed mem-leaks, add error messages if matrix is incorrect, removed local kernel. Kind regards. Danil ___ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
Re: [FFmpeg-devel] [PATCH] libavfilter/vf_convolution_opencl.c: add opencl version of libavfilter/convolution.c filter
On 07/03/18 22:32, Danil Iashchenko wrote: > --- > configure | 1 + > libavfilter/Makefile| 2 + > libavfilter/allfilters.c| 1 + > libavfilter/opencl/convolution.cl | 80 > libavfilter/opencl_source.h | 2 + > libavfilter/vf_convolution_opencl.c | 374 > > 6 files changed, 460 insertions(+) > create mode 100644 libavfilter/opencl/convolution.cl > create mode 100644 libavfilter/vf_convolution_opencl.c Nice! Works well for me on Beignet, I'll try to have a go with some other implementations in the next few days. Review comments follow. Thanks, - Mark > diff --git a/configure b/configure > index 6916b45..7c79e20 100755 > --- a/configure > +++ b/configure > @@ -3212,6 +3212,7 @@ bs2b_filter_deps="libbs2b" > colormatrix_filter_deps="gpl" > convolve_filter_deps="avcodec" > convolve_filter_select="fft" > +convolution_opencl_filter_deps="opencl" > coreimage_filter_deps="coreimage appkit" > coreimage_filter_extralibs="-framework OpenGL" > coreimagesrc_filter_deps="coreimage appkit" > diff --git a/libavfilter/Makefile b/libavfilter/Makefile > index 6a60836..f288f8e 100644 > --- a/libavfilter/Makefile > +++ b/libavfilter/Makefile > @@ -156,6 +156,8 @@ OBJS-$(CONFIG_COLORLEVELS_FILTER)+= > vf_colorlevels.o > OBJS-$(CONFIG_COLORMATRIX_FILTER)+= vf_colormatrix.o > OBJS-$(CONFIG_COLORSPACE_FILTER) += vf_colorspace.o > colorspacedsp.o > OBJS-$(CONFIG_CONVOLUTION_FILTER)+= vf_convolution.o > +OBJS-$(CONFIG_CONVOLUTION_OPENCL_FILTER) += vf_convolution_opencl.o > opencl.o \ Funny spacing? > +opencl/convolution.o > OBJS-$(CONFIG_CONVOLVE_FILTER) += vf_convolve.o framesync.o > OBJS-$(CONFIG_COPY_FILTER) += vf_copy.o > OBJS-$(CONFIG_COREIMAGE_FILTER) += vf_coreimage.o > diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c > index 9adb109..f2dc55e 100644 > --- a/libavfilter/allfilters.c > +++ b/libavfilter/allfilters.c > @@ -166,6 +166,7 @@ static void register_all(void) > REGISTER_FILTER(COLORMATRIX,colormatrix,vf); > REGISTER_FILTER(COLORSPACE, colorspace, vf); > REGISTER_FILTER(CONVOLUTION,convolution,vf); > +REGISTER_FILTER(CONVOLUTION_OPENCL, convolution_opencl, vf); > REGISTER_FILTER(CONVOLVE, convolve, vf); > REGISTER_FILTER(COPY, copy, vf); > REGISTER_FILTER(COREIMAGE, coreimage, vf); > diff --git a/libavfilter/opencl/convolution.cl > b/libavfilter/opencl/convolution.cl > new file mode 100644 > index 000..5bc5839 > --- /dev/null > +++ b/libavfilter/opencl/convolution.cl > @@ -0,0 +1,80 @@ > +/* > + * This file is part of FFmpeg. > + * > + * FFmpeg is free software; you can redistribute it and/or > + * modify it under the terms of the GNU Lesser General Public > + * License as published by the Free Software Foundation; either > + * version 2.1 of the License, or (at your option) any later version. > + * > + * FFmpeg is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * Lesser General Public License for more details. > + * > + * You should have received a copy of the GNU Lesser General Public > + * License along with FFmpeg; if not, write to the Free Software > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 > USA > + */ > + > +__kernel void convolution_global(__write_only image2d_t dst, > + __read_only image2d_t src, > + int coef_matrix_size, > + __constant float *coef_matrix, > + float div, > + float bias) > +{ > +const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE | > CLK_FILTER_NEAREST); > + > +const int half_matrix_size = (coef_matrix_size / 2); > +int2 loc = (int2)(get_global_id(0), get_global_id(1)); > +float4 convPix = (float4)(0.0f, 0.0f, 0.0f, 0.0f); > + > +for (int conv_i = -half_matrix_size; conv_i <= half_matrix_size; > conv_i++) { > +for (int conv_j = -half_matrix_size; conv_j <= half_matrix_size; > conv_j++) { > +float4 px = read_imagef(src, sampler, loc + (int2)(conv_j, > conv_i)); > +convPix += px * > coef_matrix[(conv_i+1)*coef_matrix_size+(conv_j+1)]; > +} > + } > + write_imagef(dst, loc, convPix * div + bias); > +} Looks good. > +__kernel void convolution_local(__write_only image2d_t dst, > + __read_only image2d_t src, > + int coef_matrix_size, > + __constant float *coef_matrix, > +
[FFmpeg-devel] [PATCH] libavfilter/vf_convolution_opencl.c: add opencl version of libavfilter/convolution.c filter
--- configure | 1 + libavfilter/Makefile| 2 + libavfilter/allfilters.c| 1 + libavfilter/opencl/convolution.cl | 80 libavfilter/opencl_source.h | 2 + libavfilter/vf_convolution_opencl.c | 374 6 files changed, 460 insertions(+) create mode 100644 libavfilter/opencl/convolution.cl create mode 100644 libavfilter/vf_convolution_opencl.c diff --git a/configure b/configure index 6916b45..7c79e20 100755 --- a/configure +++ b/configure @@ -3212,6 +3212,7 @@ bs2b_filter_deps="libbs2b" colormatrix_filter_deps="gpl" convolve_filter_deps="avcodec" convolve_filter_select="fft" +convolution_opencl_filter_deps="opencl" coreimage_filter_deps="coreimage appkit" coreimage_filter_extralibs="-framework OpenGL" coreimagesrc_filter_deps="coreimage appkit" diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 6a60836..f288f8e 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -156,6 +156,8 @@ OBJS-$(CONFIG_COLORLEVELS_FILTER)+= vf_colorlevels.o OBJS-$(CONFIG_COLORMATRIX_FILTER)+= vf_colormatrix.o OBJS-$(CONFIG_COLORSPACE_FILTER) += vf_colorspace.o colorspacedsp.o OBJS-$(CONFIG_CONVOLUTION_FILTER)+= vf_convolution.o +OBJS-$(CONFIG_CONVOLUTION_OPENCL_FILTER) += vf_convolution_opencl.o opencl.o \ +opencl/convolution.o OBJS-$(CONFIG_CONVOLVE_FILTER) += vf_convolve.o framesync.o OBJS-$(CONFIG_COPY_FILTER) += vf_copy.o OBJS-$(CONFIG_COREIMAGE_FILTER) += vf_coreimage.o diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 9adb109..f2dc55e 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -166,6 +166,7 @@ static void register_all(void) REGISTER_FILTER(COLORMATRIX,colormatrix,vf); REGISTER_FILTER(COLORSPACE, colorspace, vf); REGISTER_FILTER(CONVOLUTION,convolution,vf); +REGISTER_FILTER(CONVOLUTION_OPENCL, convolution_opencl, vf); REGISTER_FILTER(CONVOLVE, convolve, vf); REGISTER_FILTER(COPY, copy, vf); REGISTER_FILTER(COREIMAGE, coreimage, vf); diff --git a/libavfilter/opencl/convolution.cl b/libavfilter/opencl/convolution.cl new file mode 100644 index 000..5bc5839 --- /dev/null +++ b/libavfilter/opencl/convolution.cl @@ -0,0 +1,80 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +__kernel void convolution_global(__write_only image2d_t dst, + __read_only image2d_t src, + int coef_matrix_size, + __constant float *coef_matrix, + float div, + float bias) +{ +const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE | CLK_FILTER_NEAREST); + +const int half_matrix_size = (coef_matrix_size / 2); +int2 loc = (int2)(get_global_id(0), get_global_id(1)); +float4 convPix = (float4)(0.0f, 0.0f, 0.0f, 0.0f); + +for (int conv_i = -half_matrix_size; conv_i <= half_matrix_size; conv_i++) { +for (int conv_j = -half_matrix_size; conv_j <= half_matrix_size; conv_j++) { +float4 px = read_imagef(src, sampler, loc + (int2)(conv_j, conv_i)); +convPix += px * coef_matrix[(conv_i+1)*coef_matrix_size+(conv_j+1)]; +} + } + write_imagef(dst, loc, convPix * div + bias); +} + +__kernel void convolution_local(__write_only image2d_t dst, + __read_only image2d_t src, + int coef_matrix_size, + __constant float *coef_matrix, + float div, + float bias) +{ +const sampler_t sampler = (CLK_NORMALIZED_COORDS_FALSE | + CLK_ADDRESS_CLAMP_TO_EDGE | + CLK_FILTER_NEAREST); + +const int block_size = 16; + +const int block_x = get_group_id(0) * block_size; +const int block_y = get_group_id(1) * block_size; +const int local_x = get_local_id(0); +