Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
with
278 additions
and 1 deletion.
- +5 −0 doc/filters.texi
- +95 −0 libavfilter/chromakey_opencl_kernel.h
- +2 −0 libavfilter/opencl_allkernels.c
- +176 −1 libavfilter/vf_chromakey.c
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Copyright (c) 2015 Timo Rothenpieler <timo@rothenpieler.org> | ||
| * | ||
| * 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 | ||
| */ | ||
|
|
||
| #ifndef AVFILTER_CHROMAKEY_OPENCL_KERNEL_H | ||
| #define AVFILTER_CHROMAKEY_OPENCL_KERNEL_H | ||
|
|
||
| #include "libavutil/opencl.h" | ||
|
|
||
| const char *ff_kernel_chromakey_opencl = AV_OPENCL_KERNEL( | ||
|
|
||
| inline unsigned char get_pixel(global unsigned char *src, | ||
| int x, | ||
| int y, | ||
| int w, | ||
| int h, | ||
| int linesize, | ||
| int hsub_log2, | ||
| int vsub_log2, | ||
| unsigned char def) | ||
| { | ||
| if (x < 0 || x >= w || y < 0 || x >= w) | ||
| return def; | ||
|
|
||
| x >>= hsub_log2; | ||
| y >>= vsub_log2; | ||
|
|
||
| return src[linesize * y + x]; | ||
| } | ||
|
|
||
| kernel void chromakey(global unsigned char *src_u, | ||
| global unsigned char *src_v, | ||
| global unsigned char *dst, | ||
| int linesize_u, | ||
| int linesize_v, | ||
| int linesize_a, | ||
| int height, | ||
| int width, | ||
| int hsub_log2, | ||
| int vsub_log2, | ||
| unsigned char chromakey_u, | ||
| unsigned char chromakey_v, | ||
| float similarity, | ||
| float blend | ||
| ) | ||
| { | ||
| int x = get_global_id(0); | ||
| int y = get_global_id(1); | ||
| unsigned char res; | ||
|
|
||
| int xo, yo, du, dv; | ||
| double diff = 0.0; | ||
|
|
||
| for (yo = 0; yo < 3; yo++) { | ||
| for (xo = 0; xo < 3; xo++) { | ||
| du = get_pixel(src_u, x + xo - 1, y + yo - 1, width, height, linesize_u, hsub_log2, vsub_log2, chromakey_u); | ||
| dv = get_pixel(src_v, x + xo - 1, y + yo - 1, width, height, linesize_v, hsub_log2, vsub_log2, chromakey_v); | ||
|
|
||
| du -= chromakey_u; | ||
| dv -= chromakey_v; | ||
|
|
||
| diff += sqrt((du * du + dv * dv) / (double)(255.0 * 255.0)); | ||
| } | ||
| } | ||
|
|
||
| diff /= 9.0; | ||
|
|
||
| if (blend > 0.0001) { | ||
| res = clamp((diff - similarity) / blend, 0.0, 1.0) * 255.0; | ||
| } else { | ||
| res = (diff > similarity) ? 255 : 0; | ||
| } | ||
|
|
||
| dst[linesize_a * y + x] = res; | ||
| } | ||
|
|
||
| ); | ||
|
|
||
| #endif /* AVFILTER_CHROMAKEY_OPENCL_KERNEL_H */ |