2 * Original copyright (c) 2002 Remi Guyomarch <rguyom@pobox.com>
3 * Port copyright (c) 2010 Daniel G. Taylor <dan@programmer-art.org>
4 * Relicensed to the LGPL with permission from Remi Guyomarch.
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * blur / sharpen filter, ported to FFmpeg from MPlayer
26 * libmpcodecs/unsharp.c.
28 * This code is based on:
30 * An Efficient algorithm for Gaussian blur using finite-state machines
31 * Frederick M. Waltz and John W. V. Miller
33 * SPIE Conf. on Machine Vision Systems for Inspection and Metrology VII
34 * Originally published Boston, Nov 98
36 * http://www.engin.umd.umich.edu/~jwvm/ece581/21_GBlur.pdf
39 #include <float.h> /* DBL_MAX */
45 #include "libavutil/common.h"
46 #include "libavutil/mem.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/pixdesc.h"
50 #define MIN_MATRIX_SIZE 3
51 #define MAX_MATRIX_SIZE 63
53 /* right-shift and round-up */
54 #define SHIFTUP(x,shift) (-((-(x))>>(shift)))
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
69 FilterParam luma; ///< luma parameters (width, height, amount)
70 FilterParam chroma; ///< chroma parameters (width, height, amount)
72 int luma_msize_x, luma_msize_y, chroma_msize_x, chroma_msize_y;
73 double luma_amount, chroma_amount;
76 #define OFFSET(x) offsetof(UnsharpContext, x)
77 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
79 static const AVOption unsharp_options[] = {
80 { "luma_msize_x", "set luma matrix x size", OFFSET(luma_msize_x), AV_OPT_TYPE_INT, {.i64=5}, 3, 63, .flags=FLAGS },
81 { "lx", "set luma matrix x size", OFFSET(luma_msize_x), AV_OPT_TYPE_INT, {.i64=5}, 3, 63, .flags=FLAGS },
82 { "luma_msize_y", "set luma matrix y size", OFFSET(luma_msize_y), AV_OPT_TYPE_INT, {.i64=5}, 3, 63, .flags=FLAGS },
83 { "ly", "set luma matrix y size", OFFSET(luma_msize_y), AV_OPT_TYPE_INT, {.i64=5}, 3, 63, .flags=FLAGS },
84 { "luma_amount", "set luma effect amount", OFFSET(luma_amount), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, -DBL_MAX, DBL_MAX, .flags=FLAGS },
85 { "la", "set luma effect amount", OFFSET(luma_amount), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, -DBL_MAX, DBL_MAX, .flags=FLAGS },
87 { "chroma_msize_x", "set chroma matrix x size", OFFSET(chroma_msize_x), AV_OPT_TYPE_INT, {.i64=5}, 3, 63, .flags=FLAGS },
88 { "cx", "set chroma matrix x size", OFFSET(chroma_msize_x), AV_OPT_TYPE_INT, {.i64=5}, 3, 63, .flags=FLAGS },
89 { "chroma_msize_y", "set chroma matrix y size", OFFSET(chroma_msize_y), AV_OPT_TYPE_INT, {.i64=5}, 3, 63, .flags=FLAGS },
90 { "cy" , "set chroma matrix y size", OFFSET(chroma_msize_y), AV_OPT_TYPE_INT, {.i64=5}, 3, 63, .flags=FLAGS },
91 { "chroma_amount", "set chroma effect strenght", OFFSET(chroma_amount), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -DBL_MAX, DBL_MAX, .flags=FLAGS },
92 { "ca", "set chroma effect strenght", OFFSET(chroma_amount), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -DBL_MAX, DBL_MAX, .flags=FLAGS },
97 AVFILTER_DEFINE_CLASS(unsharp);
99 static void apply_unsharp( uint8_t *dst, int dst_stride,
100 const uint8_t *src, int src_stride,
101 int width, int height, FilterParam *fp)
103 uint32_t **sc = fp->sc;
104 uint32_t sr[MAX_MATRIX_SIZE - 1], tmp1, tmp2;
108 const uint8_t *src2 = NULL; //silence a warning
109 const int amount = fp->amount;
110 const int steps_x = fp->steps_x;
111 const int steps_y = fp->steps_y;
112 const int scalebits = fp->scalebits;
113 const int32_t halfscale = fp->halfscale;
116 if (dst_stride == src_stride)
117 memcpy(dst, src, src_stride * height);
119 for (y = 0; y < height; y++, dst += dst_stride, src += src_stride)
120 memcpy(dst, src, width);
124 for (y = 0; y < 2 * steps_y; y++)
125 memset(sc[y], 0, sizeof(sc[y][0]) * (width + 2 * steps_x));
127 for (y = -steps_y; y < height + steps_y; y++) {
131 memset(sr, 0, sizeof(sr[0]) * (2 * steps_x - 1));
132 for (x = -steps_x; x < width + steps_x; x++) {
133 tmp1 = x <= 0 ? src2[0] : x >= width ? src2[width-1] : src2[x];
134 for (z = 0; z < steps_x * 2; z += 2) {
135 tmp2 = sr[z + 0] + tmp1; sr[z + 0] = tmp1;
136 tmp1 = sr[z + 1] + tmp2; sr[z + 1] = tmp2;
138 for (z = 0; z < steps_y * 2; z += 2) {
139 tmp2 = sc[z + 0][x + steps_x] + tmp1; sc[z + 0][x + steps_x] = tmp1;
140 tmp1 = sc[z + 1][x + steps_x] + tmp2; sc[z + 1][x + steps_x] = tmp2;
142 if (x >= steps_x && y >= steps_y) {
143 const uint8_t *srx = src - steps_y * src_stride + x - steps_x;
144 uint8_t *dsx = dst - steps_y * dst_stride + x - steps_x;
146 res = (int32_t)*srx + ((((int32_t) * srx - (int32_t)((tmp1 + halfscale) >> scalebits)) * amount) >> 16);
147 *dsx = av_clip_uint8(res);
157 static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, double amount)
159 fp->msize_x = msize_x;
160 fp->msize_y = msize_y;
161 fp->amount = amount * 65536.0;
163 fp->steps_x = msize_x / 2;
164 fp->steps_y = msize_y / 2;
165 fp->scalebits = (fp->steps_x + fp->steps_y) * 2;
166 fp->halfscale = 1 << (fp->scalebits - 1);
169 static av_cold int init(AVFilterContext *ctx, const char *args)
171 UnsharpContext *unsharp = ctx->priv;
173 set_filter_param(&unsharp->luma, unsharp->luma_msize_x, unsharp->luma_msize_y, unsharp->luma_amount);
174 set_filter_param(&unsharp->chroma, unsharp->chroma_msize_x, unsharp->chroma_msize_y, unsharp->chroma_amount);
179 static int query_formats(AVFilterContext *ctx)
181 static const enum AVPixelFormat pix_fmts[] = {
182 AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV410P,
183 AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV440P, AV_PIX_FMT_YUVJ420P, AV_PIX_FMT_YUVJ422P,
184 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, AV_PIX_FMT_NONE
187 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
192 static int init_filter_param(AVFilterContext *ctx, FilterParam *fp, const char *effect_type, int width)
195 const char *effect = fp->amount == 0 ? "none" : fp->amount < 0 ? "blur" : "sharpen";
197 if (!(fp->msize_x & fp->msize_y & 1)) {
198 av_log(ctx, AV_LOG_ERROR,
199 "Invalid even size for %s matrix size %dx%d\n",
200 effect_type, fp->msize_x, fp->msize_y);
201 return AVERROR(EINVAL);
204 av_log(ctx, AV_LOG_VERBOSE, "effect:%s type:%s msize_x:%d msize_y:%d amount:%0.2f\n",
205 effect, effect_type, fp->msize_x, fp->msize_y, fp->amount / 65535.0);
207 for (z = 0; z < 2 * fp->steps_y; z++)
208 if (!(fp->sc[z] = av_malloc(sizeof(*(fp->sc[z])) * (width + 2 * fp->steps_x))))
209 return AVERROR(ENOMEM);
214 static int config_props(AVFilterLink *link)
216 UnsharpContext *unsharp = link->dst->priv;
217 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
220 unsharp->hsub = desc->log2_chroma_w;
221 unsharp->vsub = desc->log2_chroma_h;
223 ret = init_filter_param(link->dst, &unsharp->luma, "luma", link->w);
226 ret = init_filter_param(link->dst, &unsharp->chroma, "chroma", SHIFTUP(link->w, unsharp->hsub));
233 static void free_filter_param(FilterParam *fp)
237 for (z = 0; z < 2 * fp->steps_y; z++)
241 static av_cold void uninit(AVFilterContext *ctx)
243 UnsharpContext *unsharp = ctx->priv;
245 free_filter_param(&unsharp->luma);
246 free_filter_param(&unsharp->chroma);
249 static int filter_frame(AVFilterLink *link, AVFrame *in)
251 UnsharpContext *unsharp = link->dst->priv;
252 AVFilterLink *outlink = link->dst->outputs[0];
254 int cw = SHIFTUP(link->w, unsharp->hsub);
255 int ch = SHIFTUP(link->h, unsharp->vsub);
257 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
260 return AVERROR(ENOMEM);
262 av_frame_copy_props(out, in);
264 apply_unsharp(out->data[0], out->linesize[0], in->data[0], in->linesize[0], link->w, link->h, &unsharp->luma);
265 apply_unsharp(out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw, ch, &unsharp->chroma);
266 apply_unsharp(out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw, ch, &unsharp->chroma);
269 return ff_filter_frame(outlink, out);
272 static const AVFilterPad avfilter_vf_unsharp_inputs[] = {
275 .type = AVMEDIA_TYPE_VIDEO,
276 .filter_frame = filter_frame,
277 .config_props = config_props,
282 static const AVFilterPad avfilter_vf_unsharp_outputs[] = {
285 .type = AVMEDIA_TYPE_VIDEO,
290 static const char *const shorthand[] = {
291 "luma_msize_x", "luma_msize_y", "luma_amount",
292 "chroma_msize_x", "chroma_msize_y", "chroma_amount",
296 AVFilter avfilter_vf_unsharp = {
298 .description = NULL_IF_CONFIG_SMALL("Sharpen or blur the input video."),
300 .priv_size = sizeof(UnsharpContext),
304 .query_formats = query_formats,
306 .inputs = avfilter_vf_unsharp_inputs,
308 .outputs = avfilter_vf_unsharp_outputs,
310 .priv_class = &unsharp_class,
311 .shorthand = shorthand,