Unified · Split
| @@ -6197,6 +6197,11 @@ Negative values will blur the input video, while positive values will · 6196 unmodified lines | ||
| 6197 | 6197 | sharpen it, a value of zero will disable the effect. |
| 6198 | 6198 | |
| 6199 | 6199 | Default value is 0.0. |
| 6200 | + | |
| 6201 | +@item opencl | |
| 6202 | +If set to 1, specify using OpenCL capabilities, only available if | |
| 6203 | +FFmpeg was configured with @code{--enable-opencl}. Default value is 0. | |
| 6204 | + | |
| 6200 | 6205 | @end table |
| 6201 | 6206 | |
| 6202 | 6207 | All parameters are optional and default to the equivalent of the |
| @@ -149,7 +149,7 @@ OBJS-$(CONFIG_NOFORMAT_FILTER) += vf_format.o · 148 unmodified lines | ||
| 149 | 149 | OBJS-$(CONFIG_NOISE_FILTER) += vf_noise.o |
| 150 | 150 | OBJS-$(CONFIG_NULL_FILTER) += vf_null.o |
| 151 | 151 | OBJS-$(CONFIG_OCV_FILTER) += vf_libopencv.o |
| 152 | −OBJS-$(CONFIG_OPENCL) += deshake_opencl.o | |
| 152 | +OBJS-$(CONFIG_OPENCL) += deshake_opencl.o unsharp_opencl.o | |
| 153 | 153 | OBJS-$(CONFIG_OVERLAY_FILTER) += vf_overlay.o |
| 154 | 154 | OBJS-$(CONFIG_PAD_FILTER) += vf_pad.o |
| 155 | 155 | OBJS-$(CONFIG_PERMS_FILTER) += f_perms.o |
| @@ -22,6 +22,7 @@ · 21 unmodified lines | ||
| 22 | 22 | #if CONFIG_OPENCL |
| 23 | 23 | #include "libavutil/opencl.h" |
| 24 | 24 | #include "deshake_kernel.h" |
| 25 | +#include "unsharp_kernel.h" | |
| 25 | 26 | #endif |
| 26 | 27 | |
| 27 | 28 | #define OPENCL_REGISTER_KERNEL_CODE(X, x) \ |
| @@ -35,5 +36,6 @@ void ff_opencl_register_filter_kernel_code_all(void) · 7 unmodified lines | ||
| 35 | 36 | { |
| 36 | 37 | #if CONFIG_OPENCL |
| 37 | 38 | OPENCL_REGISTER_KERNEL_CODE(DESHAKE, deshake); |
| 39 | + OPENCL_REGISTER_KERNEL_CODE(UNSHARP, unsharp); | |
| 38 | 40 | #endif |
| 39 | 41 | } |
| @@ -0,0 +1,78 @@ | ||
| 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 | +#ifndef AVFILTER_UNSHARP_H | |
| 22 | +#define AVFILTER_UNSHARP_H | |
| 23 | + | |
| 24 | +#include "config.h" | |
| 25 | +#include "avfilter.h" | |
| 26 | +#if CONFIG_OPENCL | |
| 27 | +#include "libavutil/opencl.h" | |
| 28 | +#endif | |
| 29 | + | |
| 30 | +#define MIN_MATRIX_SIZE 3 | |
| 31 | +#define MAX_MATRIX_SIZE 63 | |
| 32 | + | |
| 33 | +/* right-shift and round-up */ | |
| 34 | +#define SHIFTUP(x,shift) (-((-(x))>>(shift))) | |
| 35 | + | |
| 36 | +#if CONFIG_OPENCL | |
| 37 | + | |
| 38 | +typedef struct { | |
| 39 | + cl_mem cl_luma_mask; | |
| 40 | + cl_mem cl_chroma_mask; | |
| 41 | + int in_plane_size[8]; | |
| 42 | + int out_plane_size[8]; | |
| 43 | + int plane_num; | |
| 44 | + cl_mem cl_inbuf; | |
| 45 | + size_t cl_inbuf_size; | |
| 46 | + cl_mem cl_outbuf; | |
| 47 | + size_t cl_outbuf_size; | |
| 48 | + AVOpenCLKernelEnv kernel_env; | |
| 49 | +} UnsharpOpenclContext; | |
| 50 | + | |
| 51 | +#endif | |
| 52 | + | |
| 53 | +typedef struct UnsharpFilterParam { | |
| 54 | + int msize_x; ///< matrix width | |
| 55 | + int msize_y; ///< matrix height | |
| 56 | + int amount; ///< effect amount | |
| 57 | + int steps_x; ///< horizontal step count | |
| 58 | + int steps_y; ///< vertical step count | |
| 59 | + int scalebits; ///< bits to shift pixel | |
| 60 | + int32_t halfscale; ///< amount to add to pixel | |
| 61 | + uint32_t *sc[MAX_MATRIX_SIZE - 1]; ///< finite state machine storage | |
| 62 | +} UnsharpFilterParam; | |
| 63 | + | |
| 64 | +typedef struct { | |
| 65 | + const AVClass *class; | |
| 66 | + int lmsize_x, lmsize_y, cmsize_x, cmsize_y; | |
| 67 | + float lamount, camount; | |
| 68 | + UnsharpFilterParam luma; ///< luma parameters (width, height, amount) | |
| 69 | + UnsharpFilterParam chroma; ///< chroma parameters (width, height, amount) | |
| 70 | + int hsub, vsub; | |
| 71 | + int opencl; | |
| 72 | +#if CONFIG_OPENCL | |
| 73 | + UnsharpOpenclContext opencl_ctx; | |
| 74 | +#endif | |
| 75 | + int (* apply_unsharp)(AVFilterContext *ctx, AVFrame *in, AVFrame *out); | |
| 76 | +} UnsharpContext; | |
| 77 | + | |
| 78 | +#endif /* AVFILTER_UNSHARP_H */ | |
| @@ -0,0 +1,137 @@ | ||
| 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 | +#ifndef AVFILTER_UNSHARP_KERNEL_H | |
| 22 | +#define AVFILTER_UNSHARP_KERNEL_H | |
| 23 | + | |
| 24 | +#include "libavutil/opencl.h" | |
| 25 | + | |
| 26 | +const char *ff_kernel_unsharp_opencl = AV_OPENCL_KERNEL( | |
| 27 | +inline unsigned char clip_uint8(int a) | |
| 28 | +{ | |
| 29 | + if (a & (~0xFF)) | |
| 30 | + return (-a)>>31; | |
| 31 | + else | |
| 32 | + return a; | |
| 33 | +} | |
| 34 | + | |
| 35 | +kernel void unsharp(global unsigned char *src, | |
| 36 | + global unsigned char *dst, | |
| 37 | + const global unsigned int *mask_lu, | |
| 38 | + const global unsigned int *mask_ch, | |
| 39 | + int amount_lu, | |
| 40 | + int amount_ch, | |
| 41 | + int step_x_lu, | |
| 42 | + int step_y_lu, | |
| 43 | + int step_x_ch, | |
| 44 | + int step_y_ch, | |
| 45 | + int scalebits_lu, | |
| 46 | + int scalebits_ch, | |
| 47 | + int halfscale_lu, | |
| 48 | + int halfscale_ch, | |
| 49 | + int src_stride_lu, | |
| 50 | + int src_stride_ch, | |
| 51 | + int dst_stride_lu, | |
| 52 | + int dst_stride_ch, | |
| 53 | + int height, | |
| 54 | + int width, | |
| 55 | + int ch, | |
| 56 | + int cw) | |
| 57 | +{ | |
| 58 | + global unsigned char *dst_y = dst; | |
| 59 | + global unsigned char *dst_u = dst_y + height * dst_stride_lu; | |
| 60 | + global unsigned char *dst_v = dst_u + ch * dst_stride_ch; | |
| 61 | + | |
| 62 | + global unsigned char *src_y = src; | |
| 63 | + global unsigned char *src_u = src_y + height * src_stride_lu; | |
| 64 | + global unsigned char *src_v = src_u + ch * src_stride_ch; | |
| 65 | + | |
| 66 | + global unsigned char *temp_dst; | |
| 67 | + global unsigned char *temp_src; | |
| 68 | + const global unsigned int *temp_mask; | |
| 69 | + int global_id = get_global_id(0); | |
| 70 | + int i, j, x, y, temp_src_stride, temp_dst_stride, temp_height, temp_width, temp_steps_x, temp_steps_y, | |
| 71 | + temp_amount, temp_scalebits, temp_halfscale, sum, idx_x, idx_y, temp, res; | |
| 72 | + if (global_id < width * height) { | |
| 73 | + y = global_id / width; | |
| 74 | + x = global_id % width; | |
| 75 | + temp_dst = dst_y; | |
| 76 | + temp_src = src_y; | |
| 77 | + temp_src_stride = src_stride_lu; | |
| 78 | + temp_dst_stride = dst_stride_lu; | |
| 79 | + temp_height = height; | |
| 80 | + temp_width = width; | |
| 81 | + temp_steps_x = step_x_lu; | |
| 82 | + temp_steps_y = step_y_lu; | |
| 83 | + temp_mask = mask_lu; | |
| 84 | + temp_amount = amount_lu; | |
| 85 | + temp_scalebits = scalebits_lu; | |
| 86 | + temp_halfscale = halfscale_lu; | |
| 87 | + } else if ((global_id >= width * height) && (global_id < width * height + ch * cw)) { | |
| 88 | + y = (global_id - width * height) / cw; | |
| 89 | + x = (global_id - width * height) % cw; | |
| 90 | + temp_dst = dst_u; | |
| 91 | + temp_src = src_u; | |
| 92 | + temp_src_stride = src_stride_ch; | |
| 93 | + temp_dst_stride = dst_stride_ch; | |
| 94 | + temp_height = ch; | |
| 95 | + temp_width = cw; | |
| 96 | + temp_steps_x = step_x_ch; | |
| 97 | + temp_steps_y = step_y_ch; | |
| 98 | + temp_mask = mask_ch; | |
| 99 | + temp_amount = amount_ch; | |
| 100 | + temp_scalebits = scalebits_ch; | |
| 101 | + temp_halfscale = halfscale_ch; | |
| 102 | + } else { | |
| 103 | + y = (global_id - width * height - ch * cw) / cw; | |
| 104 | + x = (global_id - width * height - ch * cw) % cw; | |
| 105 | + temp_dst = dst_v; | |
| 106 | + temp_src = src_v; | |
| 107 | + temp_src_stride = src_stride_ch; | |
| 108 | + temp_dst_stride = dst_stride_ch; | |
| 109 | + temp_height = ch; | |
| 110 | + temp_width = cw; | |
| 111 | + temp_steps_x = step_x_ch; | |
| 112 | + temp_steps_y = step_y_ch; | |
| 113 | + temp_mask = mask_ch; | |
| 114 | + temp_amount = amount_ch; | |
| 115 | + temp_scalebits = scalebits_ch; | |
| 116 | + temp_halfscale = halfscale_ch; | |
| 117 | + } | |
| 118 | + if (temp_amount) { | |
| 119 | + sum = 0; | |
| 120 | + for (j = 0; j <= 2 * temp_steps_y; j++) { | |
| 121 | + idx_y = (y - temp_steps_y + j) <= 0 ? 0 : (y - temp_steps_y + j) >= temp_height ? temp_height-1 : y - temp_steps_y + j; | |
| 122 | + for (i = 0; i <= 2 * temp_steps_x; i++) { | |
| 123 | + idx_x = (x - temp_steps_x + i) <= 0 ? 0 : (x - temp_steps_x + i) >= temp_width ? temp_width-1 : x - temp_steps_x + i; | |
| 124 | + sum += temp_mask[i + j * (2 * temp_steps_x + 1)] * temp_src[idx_x + idx_y * temp_src_stride]; | |
| 125 | + } | |
| 126 | + } | |
| 127 | + temp = (int)temp_src[x + y * temp_src_stride]; | |
| 128 | + res = temp + (((temp - (int)((sum + temp_halfscale) >> temp_scalebits)) * temp_amount) >> 16); | |
| 129 | + temp_dst[x + y * temp_dst_stride] = clip_uint8(res); | |
| 130 | + } else { | |
| 131 | + temp_dst[x + y * temp_dst_stride] = temp_src[x + y * temp_src_stride]; | |
| 132 | + } | |
| 133 | +} | |
| 134 | + | |
| 135 | +); | |
| 136 | + | |
| 137 | +#endif /* AVFILTER_UNSHARP_KERNEL_H */ | |
| @@ -0,0 +1,282 @@ | ||
| 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 | + * unsharp input video | |
| 24 | + */ | |
| 25 | + | |
| 26 | +#include "unsharp_opencl.h" | |
| 27 | +#include "libavutil/opencl_internal.h" | |
| 28 | + | |
| 29 | +#define PLANE_NUM 3 | |
| 30 | + | |
| 31 | +static inline void add_mask_counter(uint32_t *dst, uint32_t *counter1, uint32_t *counter2, int len) | |
| 32 | +{ | |
| 33 | + int i; | |
| 34 | + for (i = 0; i < len; i++) { | |
| 35 | + dst[i] = counter1[i] + counter2[i]; | |
| 36 | + } | |
| 37 | +} | |
| 38 | + | |
| 39 | +static int compute_mask(int step, uint32_t *mask) | |
| 40 | +{ | |
| 41 | + int i, z, ret = 0; | |
| 42 | + int counter_size = sizeof(uint32_t) * (2 * step + 1); | |
| 43 | + uint32_t *temp1_counter, *temp2_counter, **counter; | |
| 44 | + temp1_counter = av_mallocz(counter_size); | |
| 45 | + if (!temp1_counter) { | |
| 46 | + ret = AVERROR(ENOMEM); | |
| 47 | + goto end; | |
| 48 | + } | |
| 49 | + temp2_counter = av_mallocz(counter_size); | |
| 50 | + if (!temp2_counter) { | |
| 51 | + ret = AVERROR(ENOMEM); | |
| 52 | + goto end; | |
| 53 | + } | |
| 54 | + counter = av_mallocz(counter_size); | |
| 55 | + if (!counter) { | |
| 56 | + ret = AVERROR(ENOMEM); | |
| 57 | + goto end; | |
| 58 | + } | |
| 59 | + for (i = 0; i < 2 * step + 1; i++) { | |
| 60 | + counter[i] = av_mallocz(counter_size); | |
| 61 | + if (!counter[i]) { | |
| 62 | + ret = AVERROR(ENOMEM); | |
| 63 | + goto end; | |
| 64 | + } | |
| 65 | + } | |
| 66 | + for (i = 0; i < 2 * step + 1; i++) { | |
| 67 | + memset(temp1_counter, 0, counter_size); | |
| 68 | + temp1_counter[i] = 1; | |
| 69 | + for (z = 0; z < step * 2; z += 2) { | |
| 70 | + add_mask_counter(temp2_counter, counter[z], temp1_counter, step * 2); | |
| 71 | + memcpy(counter[z], temp1_counter, counter_size); | |
| 72 | + add_mask_counter(temp1_counter, counter[z + 1], temp2_counter, step * 2); | |
| 73 | + memcpy(counter[z + 1], temp2_counter, counter_size); | |
| 74 | + } | |
| 75 | + } | |
| 76 | + memcpy(mask, temp1_counter, counter_size); | |
| 77 | +end: | |
| 78 | + av_freep(&temp1_counter); | |
| 79 | + av_freep(&temp2_counter); | |
| 80 | + for (i = 0; i < 2 * step + 1; i++) { | |
| 81 | + av_freep(&counter[i]); | |
| 82 | + } | |
| 83 | + av_freep(&counter); | |
| 84 | + return ret; | |
| 85 | +} | |
| 86 | + | |
| 87 | +static int compute_mask_matrix(cl_mem cl_mask_matrix, int step_x, int step_y) | |
| 88 | +{ | |
| 89 | + int i, j, ret = 0; | |
| 90 | + uint32_t *mask_matrix, *mask_x, *mask_y; | |
| 91 | + size_t size_matrix = sizeof(uint32_t) * (2 * step_x + 1) * (2 * step_y + 1); | |
| 92 | + mask_x = av_mallocz(sizeof(uint32_t) * (2 * step_x + 1)); | |
| 93 | + if (!mask_x) { | |
| 94 | + ret = AVERROR(ENOMEM); | |
| 95 | + goto end; | |
| 96 | + } | |
| 97 | + mask_y = av_mallocz(sizeof(uint32_t) * (2 * step_y + 1)); | |
| 98 | + if (!mask_y) { | |
| 99 | + ret = AVERROR(ENOMEM); | |
| 100 | + goto end; | |
| 101 | + } | |
| 102 | + mask_matrix = av_mallocz(size_matrix); | |
| 103 | + if (!mask_matrix) { | |
| 104 | + ret = AVERROR(ENOMEM); | |
| 105 | + goto end; | |
| 106 | + } | |
| 107 | + ret = compute_mask(step_x, mask_x); | |
| 108 | + if (ret < 0) | |
| 109 | + goto end; | |
| 110 | + ret = compute_mask(step_y, mask_y); | |
| 111 | + if (ret < 0) | |
| 112 | + goto end; | |
| 113 | + for (j = 0; j < 2 * step_y + 1; j++) { | |
| 114 | + for (i = 0; i < 2 * step_x + 1; i++) { | |
| 115 | + mask_matrix[i + j * (2 * step_x + 1)] = mask_y[j] * mask_x[i]; | |
| 116 | + } | |
| 117 | + } | |
| 118 | + ret = av_opencl_buffer_write(cl_mask_matrix, (uint8_t *)mask_matrix, size_matrix); | |
| 119 | +end: | |
| 120 | + av_freep(&mask_x); | |
| 121 | + av_freep(&mask_y); | |
| 122 | + av_freep(&mask_matrix); | |
| 123 | + return ret; | |
| 124 | +} | |
| 125 | + | |
| 126 | +static int generate_mask(AVFilterContext *ctx) | |
| 127 | +{ | |
| 128 | + UnsharpContext *unsharp = ctx->priv; | |
| 129 | + int i, ret = 0, step_x[2], step_y[2]; | |
| 130 | + cl_mem mask_matrix[2]; | |
| 131 | + mask_matrix[0] = unsharp->opencl_ctx.cl_luma_mask; | |
| 132 | + mask_matrix[1] = unsharp->opencl_ctx.cl_chroma_mask; | |
| 133 | + step_x[0] = unsharp->luma.steps_x; | |
| 134 | + step_x[1] = unsharp->chroma.steps_x; | |
| 135 | + step_y[0] = unsharp->luma.steps_y; | |
| 136 | + step_y[1] = unsharp->chroma.steps_y; | |
| 137 | + if (!mask_matrix[0] || !mask_matrix[1]) { | |
| 138 | + av_log(ctx, AV_LOG_ERROR, "Luma mask and chroma mask should not be NULL\n"); | |
| 139 | + return AVERROR(EINVAL); | |
| 140 | + } | |
| 141 | + for (i = 0; i < 2; i++) { | |
| 142 | + ret = compute_mask_matrix(mask_matrix[i], step_x[i], step_y[i]); | |
| 143 | + if (ret < 0) | |
| 144 | + return ret; | |
| 145 | + } | |
| 146 | + return ret; | |
| 147 | +} | |
| 148 | + | |
| 149 | +int ff_opencl_apply_unsharp(AVFilterContext *ctx, AVFrame *in, AVFrame *out) | |
| 150 | +{ | |
| 151 | + int ret; | |
| 152 | + AVFilterLink *link = ctx->inputs[0]; | |
| 153 | + UnsharpContext *unsharp = ctx->priv; | |
| 154 | + cl_int status; | |
| 155 | + int cw = SHIFTUP(link->w, unsharp->hsub); | |
| 156 | + int ch = SHIFTUP(link->h, unsharp->vsub); | |
| 157 | + const size_t global_work_size = link->w * link->h + 2 * ch * cw; | |
| 158 | + FFOpenclParam opencl_param = {0}; | |
| 159 | + | |
| 160 | + opencl_param.ctx = ctx; | |
| 161 | + opencl_param.kernel = unsharp->opencl_ctx.kernel_env.kernel; | |
| 162 | + ret = ff_opencl_set_parameter(&opencl_param, | |
| 163 | + FF_OPENCL_PARAM_INFO(unsharp->opencl_ctx.cl_inbuf), | |
| 164 | + FF_OPENCL_PARAM_INFO(unsharp->opencl_ctx.cl_outbuf), | |
| 165 | + FF_OPENCL_PARAM_INFO(unsharp->opencl_ctx.cl_luma_mask), | |
| 166 | + FF_OPENCL_PARAM_INFO(unsharp->opencl_ctx.cl_chroma_mask), | |
| 167 | + FF_OPENCL_PARAM_INFO(unsharp->luma.amount), | |
| 168 | + FF_OPENCL_PARAM_INFO(unsharp->chroma.amount), | |
| 169 | + FF_OPENCL_PARAM_INFO(unsharp->luma.steps_x), | |
| 170 | + FF_OPENCL_PARAM_INFO(unsharp->luma.steps_y), | |
| 171 | + FF_OPENCL_PARAM_INFO(unsharp->chroma.steps_x), | |
| 172 | + FF_OPENCL_PARAM_INFO(unsharp->chroma.steps_y), | |
| 173 | + FF_OPENCL_PARAM_INFO(unsharp->luma.scalebits), | |
| 174 | + FF_OPENCL_PARAM_INFO(unsharp->chroma.scalebits), | |
| 175 | + FF_OPENCL_PARAM_INFO(unsharp->luma.halfscale), | |
| 176 | + FF_OPENCL_PARAM_INFO(unsharp->chroma.halfscale), | |
| 177 | + FF_OPENCL_PARAM_INFO(in->linesize[0]), | |
| 178 | + FF_OPENCL_PARAM_INFO(in->linesize[1]), | |
| 179 | + FF_OPENCL_PARAM_INFO(out->linesize[0]), | |
| 180 | + FF_OPENCL_PARAM_INFO(out->linesize[1]), | |
| 181 | + FF_OPENCL_PARAM_INFO(link->h), | |
| 182 | + FF_OPENCL_PARAM_INFO(link->w), | |
| 183 | + FF_OPENCL_PARAM_INFO(ch), | |
| 184 | + FF_OPENCL_PARAM_INFO(cw), | |
| 185 | + NULL); | |
| 186 | + if (ret < 0) | |
| 187 | + return ret; | |
| 188 | + status = clEnqueueNDRangeKernel(unsharp->opencl_ctx.kernel_env.command_queue, | |
| 189 | + unsharp->opencl_ctx.kernel_env.kernel, 1, NULL, | |
| 190 | + &global_work_size, NULL, 0, NULL, NULL); | |
| 191 | + if (status != CL_SUCCESS) { | |
| 192 | + av_log(ctx, AV_LOG_ERROR, "OpenCL run kernel error occurred: %d\n", status); | |
| 193 | + return AVERROR_EXTERNAL; | |
| 194 | + } | |
| 195 | + clFinish(unsharp->opencl_ctx.kernel_env.command_queue); | |
| 196 | + return av_opencl_buffer_read_image(out->data, unsharp->opencl_ctx.out_plane_size, | |
| 197 | + unsharp->opencl_ctx.plane_num, unsharp->opencl_ctx.cl_outbuf, | |
| 198 | + unsharp->opencl_ctx.cl_outbuf_size); | |
| 199 | +} | |
| 200 | + | |
| 201 | +int ff_opencl_unsharp_init(AVFilterContext *ctx) | |
| 202 | +{ | |
| 203 | + int ret = 0; | |
| 204 | + UnsharpContext *unsharp = ctx->priv; | |
| 205 | + ret = av_opencl_init(NULL); | |
| 206 | + if (ret < 0) | |
| 207 | + return ret; | |
| 208 | + ret = av_opencl_buffer_create(&unsharp->opencl_ctx.cl_luma_mask, | |
| 209 | + sizeof(uint32_t) * (2 * unsharp->luma.steps_x + 1) * (2 * unsharp->luma.steps_y + 1), | |
| 210 | + CL_MEM_READ_ONLY, NULL); | |
| 211 | + if (ret < 0) | |
| 212 | + return ret; | |
| 213 | + ret = av_opencl_buffer_create(&unsharp->opencl_ctx.cl_chroma_mask, | |
| 214 | + sizeof(uint32_t) * (2 * unsharp->chroma.steps_x + 1) * (2 * unsharp->chroma.steps_y + 1), | |
| 215 | + CL_MEM_READ_ONLY, NULL); | |
| 216 | + if (ret < 0) | |
| 217 | + return ret; | |
| 218 | + ret = generate_mask(ctx); | |
| 219 | + if (ret < 0) | |
| 220 | + return ret; | |
| 221 | + unsharp->opencl_ctx.plane_num = PLANE_NUM; | |
| 222 | + if (!unsharp->opencl_ctx.kernel_env.kernel) { | |
| 223 | + ret = av_opencl_create_kernel(&unsharp->opencl_ctx.kernel_env, "unsharp"); | |
| 224 | + if (ret < 0) { | |
| 225 | + av_log(ctx, AV_LOG_ERROR, "OpenCL failed to create kernel with name 'unsharp'\n"); | |
| 226 | + return ret; | |
| 227 | + } | |
| 228 | + } | |
| 229 | + return ret; | |
| 230 | +} | |
| 231 | + | |
| 232 | +void ff_opencl_unsharp_uninit(AVFilterContext *ctx) | |
| 233 | +{ | |
| 234 | + UnsharpContext *unsharp = ctx->priv; | |
| 235 | + av_opencl_buffer_release(&unsharp->opencl_ctx.cl_inbuf); | |
| 236 | + av_opencl_buffer_release(&unsharp->opencl_ctx.cl_outbuf); | |
| 237 | + av_opencl_buffer_release(&unsharp->opencl_ctx.cl_luma_mask); | |
| 238 | + av_opencl_buffer_release(&unsharp->opencl_ctx.cl_chroma_mask); | |
| 239 | + av_opencl_release_kernel(&unsharp->opencl_ctx.kernel_env); | |
| 240 | + av_opencl_uninit(); | |
| 241 | +} | |
| 242 | + | |
| 243 | +int ff_opencl_unsharp_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out) | |
| 244 | +{ | |
| 245 | + int ret = 0; | |
| 246 | + AVFilterLink *link = ctx->inputs[0]; | |
| 247 | + UnsharpContext *unsharp = ctx->priv; | |
| 248 | + int ch = SHIFTUP(link->h, unsharp->vsub); | |
| 249 | + | |
| 250 | + if ((!unsharp->opencl_ctx.cl_inbuf) || (!unsharp->opencl_ctx.cl_outbuf)) { | |
| 251 | + unsharp->opencl_ctx.in_plane_size[0] = (in->linesize[0] * in->height); | |
| 252 | + unsharp->opencl_ctx.in_plane_size[1] = (in->linesize[1] * ch); | |
| 253 | + unsharp->opencl_ctx.in_plane_size[2] = (in->linesize[2] * ch); | |
| 254 | + unsharp->opencl_ctx.out_plane_size[0] = (out->linesize[0] * out->height); | |
| 255 | + unsharp->opencl_ctx.out_plane_size[1] = (out->linesize[1] * ch); | |
| 256 | + unsharp->opencl_ctx.out_plane_size[2] = (out->linesize[2] * ch); | |
| 257 | + unsharp->opencl_ctx.cl_inbuf_size = unsharp->opencl_ctx.in_plane_size[0] + | |
| 258 | + unsharp->opencl_ctx.in_plane_size[1] + | |
| 259 | + unsharp->opencl_ctx.in_plane_size[2]; | |
| 260 | + unsharp->opencl_ctx.cl_outbuf_size = unsharp->opencl_ctx.out_plane_size[0] + | |
| 261 | + unsharp->opencl_ctx.out_plane_size[1] + | |
| 262 | + unsharp->opencl_ctx.out_plane_size[2]; | |
| 263 | + if (!unsharp->opencl_ctx.cl_inbuf) { | |
| 264 | + ret = av_opencl_buffer_create(&unsharp->opencl_ctx.cl_inbuf, | |
| 265 | + unsharp->opencl_ctx.cl_inbuf_size, | |
| 266 | + CL_MEM_READ_ONLY, NULL); | |
| 267 | + if (ret < 0) | |
| 268 | + return ret; | |
| 269 | + } | |
| 270 | + if (!unsharp->opencl_ctx.cl_outbuf) { | |
| 271 | + ret = av_opencl_buffer_create(&unsharp->opencl_ctx.cl_outbuf, | |
| 272 | + unsharp->opencl_ctx.cl_outbuf_size, | |
| 273 | + CL_MEM_READ_WRITE, NULL); | |
| 274 | + if (ret < 0) | |
| 275 | + return ret; | |
| 276 | + } | |
| 277 | + } | |
| 278 | + return av_opencl_buffer_write_image(unsharp->opencl_ctx.cl_inbuf, | |
| 279 | + unsharp->opencl_ctx.cl_inbuf_size, | |
| 280 | + 0, in->data, unsharp->opencl_ctx.in_plane_size, | |
| 281 | + unsharp->opencl_ctx.plane_num); | |
| 282 | +} | |
| @@ -0,0 +1,34 @@ | ||
| 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 | +#ifndef AVFILTER_UNSHARP_OPENCL_H | |
| 22 | +#define AVFILTER_UNSHARP_OPENCL_H | |
| 23 | + | |
| 24 | +#include "unsharp.h" | |
| 25 | + | |
| 26 | +int ff_opencl_unsharp_init(AVFilterContext *ctx); | |
| 27 | + | |
| 28 | +void ff_opencl_unsharp_uninit(AVFilterContext *ctx); | |
| 29 | + | |
| 30 | +int ff_opencl_unsharp_process_inout_buf(AVFilterContext *ctx, AVFrame *in, AVFrame *out); | |
| 31 | + | |
| 32 | +int ff_opencl_apply_unsharp(AVFilterContext *ctx, AVFrame *in, AVFrame *out); | |
| 33 | + | |
| 34 | +#endif /* AVFILTER_UNSHARP_OPENCL_H */ | |
| @@ -30,7 +30,7 @@ · 29 unmodified lines | ||
| 30 | 30 | |
| 31 | 31 | #define LIBAVFILTER_VERSION_MAJOR 3 |
| 32 | 32 | #define LIBAVFILTER_VERSION_MINOR 60 |
| 33 | −#define LIBAVFILTER_VERSION_MICRO 101 | |
| 33 | +#define LIBAVFILTER_VERSION_MICRO 102 | |
| 34 | 34 | |
| 35 | 35 | #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |
| 36 | 36 | LIBAVFILTER_VERSION_MINOR, \ |
| @@ -46,36 +46,12 @@ · 45 unmodified lines | ||
| 46 | 46 | #include "libavutil/mem.h" |
| 47 | 47 | #include "libavutil/opt.h" |
| 48 | 48 | #include "libavutil/pixdesc.h" |
| 49 | − | |
| 50 | −#define MIN_MATRIX_SIZE 3 | |
| 51 | −#define MAX_MATRIX_SIZE 63 | |
| 52 | − | |
| 53 | −/* right-shift and round-up */ | |
| 54 | −#define SHIFTUP(x,shift) (-((-(x))>>(shift))) | |
| 55 | − | |
| 56 | −typedef struct FilterParam { | |
| 57 | − int msize_x; ///< matrix width | |
| 58 | − int msize_y; ///< matrix height | |
| 59 | − int amount; ///< effect amount | |
| 60 | − int steps_x; ///< horizontal step count | |
| 61 | − int steps_y; ///< vertical step count | |
| 62 | − int scalebits; ///< bits to shift pixel | |
| 63 | − int32_t halfscale; ///< amount to add to pixel | |
| 64 | − uint32_t *sc[MAX_MATRIX_SIZE - 1]; ///< finite state machine storage | |
| 65 | −} FilterParam; | |
| 66 | − | |
| 67 | −typedef struct { | |
| 68 | − const AVClass *class; | |
| 69 | − int lmsize_x, lmsize_y, cmsize_x, cmsize_y; | |
| 70 | − float lamount, camount; | |
| 71 | − FilterParam luma; ///< luma parameters (width, height, amount) | |
| 72 | − FilterParam chroma; ///< chroma parameters (width, height, amount) | |
| 73 | − int hsub, vsub; | |
| 74 | −} UnsharpContext; | |
| 49 | +#include "unsharp.h" | |
| 50 | +#include "unsharp_opencl.h" | |
| 75 | 51 | |
| 76 | 52 | static void apply_unsharp( uint8_t *dst, int dst_stride, |
| 77 | 53 | const uint8_t *src, int src_stride, |
| 78 | − int width, int height, FilterParam *fp) | |
| 54 | + int width, int height, UnsharpFilterParam *fp) | |
| 79 | 55 | { |
| 80 | 56 | uint32_t **sc = fp->sc; |
| 81 | 57 | uint32_t sr[MAX_MATRIX_SIZE - 1], tmp1, tmp2; |
| @@ -131,7 +107,25 @@ static void apply_unsharp( uint8_t *dst, int dst_stride, · 49 unmodified lines | ||
| 131 | 107 | } |
| 132 | 108 | } |
| 133 | 109 | |
| 134 | −static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, float amount) | |
| 110 | +static int apply_unsharp_c(AVFilterContext *ctx, AVFrame *in, AVFrame *out) | |
| 111 | +{ | |
| 112 | + AVFilterLink *inlink = ctx->inputs[0]; | |
| 113 | + UnsharpContext *unsharp = ctx->priv; | |
| 114 | + int i, plane_w[3], plane_h[3]; | |
| 115 | + UnsharpFilterParam *fp[3]; | |
| 116 | + plane_w[0] = inlink->w; | |
| 117 | + plane_w[1] = plane_w[2] = SHIFTUP(inlink->w, unsharp->hsub); | |
| 118 | + plane_h[0] = inlink->h; | |
| 119 | + plane_h[1] = plane_h[2] = SHIFTUP(inlink->h, unsharp->vsub); | |
| 120 | + fp[0] = &unsharp->luma; | |
| 121 | + fp[1] = fp[2] = &unsharp->chroma; | |
| 122 | + for (i = 0; i < 3; i++) { | |
| 123 | + apply_unsharp(out->data[i], out->linesize[i], in->data[i], in->linesize[i], plane_w[i], plane_h[i], fp[i]); | |
| 124 | + } | |
| 125 | + return 0; | |
| 126 | +} | |
| 127 | + | |
| 128 | +static void set_filter_param(UnsharpFilterParam *fp, int msize_x, int msize_y, float amount) | |
| 135 | 129 | { |
| 136 | 130 | fp->msize_x = msize_x; |
| 137 | 131 | fp->msize_y = msize_y; |
| @@ -145,12 +139,24 @@ static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, float am · 7 unmodified lines | ||
| 145 | 139 | |
| 146 | 140 | static av_cold int init(AVFilterContext *ctx) |
| 147 | 141 | { |
| 142 | + int ret = 0; | |
| 148 | 143 | UnsharpContext *unsharp = ctx->priv; |
| 149 | 144 | |
| 150 | 145 | |
| 151 | 146 | set_filter_param(&unsharp->luma, unsharp->lmsize_x, unsharp->lmsize_y, unsharp->lamount); |
| 152 | 147 | set_filter_param(&unsharp->chroma, unsharp->cmsize_x, unsharp->cmsize_y, unsharp->camount); |
| 153 | 148 | |
| 149 | + unsharp->apply_unsharp = apply_unsharp_c; | |
| 150 | + if (!CONFIG_OPENCL && unsharp->opencl) { | |
| 151 | + av_log(ctx, AV_LOG_ERROR, "OpenCL support was not enabled in this build, cannot be selected\n"); | |
| 152 | + return AVERROR(EINVAL); | |
| 153 | + } | |
| 154 | + if (CONFIG_OPENCL && unsharp->opencl) { | |
| 155 | + unsharp->apply_unsharp = ff_opencl_apply_unsharp; | |
| 156 | + ret = ff_opencl_unsharp_init(ctx); | |
| 157 | + if (ret < 0) | |
| 158 | + return ret; | |
| 159 | + } | |
| 154 | 160 | return 0; |
| 155 | 161 | } |
| 156 | 162 | |
| @@ -167,7 +173,7 @@ static int query_formats(AVFilterContext *ctx) · 10 unmodified lines | ||
| 167 | 173 | return 0; |
| 168 | 174 | } |
| 169 | 175 | |
| 170 | −static int init_filter_param(AVFilterContext *ctx, FilterParam *fp, const char *effect_type, int width) | |
| 176 | +static int init_filter_param(AVFilterContext *ctx, UnsharpFilterParam *fp, const char *effect_type, int width) | |
| 171 | 177 | { |
| 172 | 178 | int z; |
| 173 | 179 | const char *effect = fp->amount == 0 ? "none" : fp->amount < 0 ? "blur" : "sharpen"; |
| @@ -208,7 +214,7 @@ static int config_props(AVFilterLink *link) · 34 unmodified lines | ||
| 208 | 214 | return 0; |
| 209 | 215 | } |
| 210 | 216 | |
| 211 | −static void free_filter_param(FilterParam *fp) | |
| 217 | +static void free_filter_param(UnsharpFilterParam *fp) | |
| 212 | 218 | { |
| 213 | 219 | int z; |
| 214 | 220 | |
| @@ -220,6 +226,10 @@ static av_cold void uninit(AVFilterContext *ctx) · 5 unmodified lines | ||
| 220 | 226 | { |
| 221 | 227 | UnsharpContext *unsharp = ctx->priv; |
| 222 | 228 | |
| 229 | + if (CONFIG_OPENCL && unsharp->opencl) { | |
| 230 | + ff_opencl_unsharp_uninit(ctx); | |
| 231 | + } | |
| 232 | + | |
| 223 | 233 | free_filter_param(&unsharp->luma); |
| 224 | 234 | free_filter_param(&unsharp->chroma); |
| 225 | 235 | } |
| @@ -229,8 +239,7 @@ static int filter_frame(AVFilterLink *link, AVFrame *in) · 3 unmodified lines | ||
| 229 | 239 | UnsharpContext *unsharp = link->dst->priv; |
| 230 | 240 | AVFilterLink *outlink = link->dst->outputs[0]; |
| 231 | 241 | AVFrame *out; |
| 232 | − int cw = SHIFTUP(link->w, unsharp->hsub); | |
| 233 | − int ch = SHIFTUP(link->h, unsharp->vsub); | |
| 242 | + int ret = 0; | |
| 234 | 243 | |
| 235 | 244 | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
| 236 | 245 | if (!out) { |
| @@ -238,12 +247,18 @@ static int filter_frame(AVFilterLink *link, AVFrame *in) · 1 unmodified line | ||
| 238 | 247 | return AVERROR(ENOMEM); |
| 239 | 248 | } |
| 240 | 249 | av_frame_copy_props(out, in); |
| 250 | + if (CONFIG_OPENCL && unsharp->opencl) { | |
| 251 | + ret = ff_opencl_unsharp_process_inout_buf(link->dst, in, out); | |
| 252 | + if (ret < 0) | |
| 253 | + goto end; | |
| 254 | + } | |
| 241 | 255 | |
| 242 | − apply_unsharp(out->data[0], out->linesize[0], in->data[0], in->linesize[0], link->w, link->h, &unsharp->luma); | |
| 243 | − apply_unsharp(out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw, ch, &unsharp->chroma); | |
| 244 | − apply_unsharp(out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw, ch, &unsharp->chroma); | |
| 245 | − | |
| 256 | + ret = unsharp->apply_unsharp(link->dst, in, out); | |
| 257 | +end: | |
| 246 | 258 | av_frame_free(&in); |
| 259 | + | |
| 260 | + if (ret < 0) | |
| 261 | + return ret; | |
| 247 | 262 | return ff_filter_frame(outlink, out); |
| 248 | 263 | } |
| 249 | 264 | |
| @@ -264,6 +279,7 @@ static const AVOption unsharp_options[] = { · 14 unmodified lines | ||
| 264 | 279 | { "cy", "chroma matrix vertical size", OFFSET(cmsize_y), AV_OPT_TYPE_INT, { .i64 = 5 }, MIN_SIZE, MAX_SIZE, FLAGS }, |
| 265 | 280 | { "chroma_amount", "chroma effect strength", OFFSET(camount), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -2, 5, FLAGS }, |
| 266 | 281 | { "ca", "chroma effect strength", OFFSET(camount), AV_OPT_TYPE_FLOAT, { .dbl = 0 }, -2, 5, FLAGS }, |
| 282 | + { "opencl", "use OpenCL filtering capabilities", OFFSET(opencl), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS }, | |
| 267 | 283 | { NULL }, |
| 268 | 284 | }; |
| 269 | 285 | |