]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_unsharp.c
Merge commit 'b4d24b471bc52f1f78a43ee330199e70483e51c3'
[ffmpeg] / libavfilter / vf_unsharp.c
1 /*
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.
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 /**
24  * @file
25  * blur / sharpen filter, ported to FFmpeg from MPlayer
26  * libmpcodecs/unsharp.c.
27  *
28  * This code is based on:
29  *
30  * An Efficient algorithm for Gaussian blur using finite-state machines
31  * Frederick M. Waltz and John W. V. Miller
32  *
33  * SPIE Conf. on Machine Vision Systems for Inspection and Metrology VII
34  * Originally published Boston, Nov 98
35  *
36  * http://www.engin.umd.umich.edu/~jwvm/ece581/21_GBlur.pdf
37  */
38
39 #include <float.h> /* DBL_MAX */
40
41 #include "avfilter.h"
42 #include "formats.h"
43 #include "internal.h"
44 #include "video.h"
45 #include "libavutil/common.h"
46 #include "libavutil/mem.h"
47 #include "libavutil/opt.h"
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     FilterParam luma;   ///< luma parameters (width, height, amount)
70     FilterParam chroma; ///< chroma parameters (width, height, amount)
71     int hsub, vsub;
72     int luma_msize_x, luma_msize_y, chroma_msize_x, chroma_msize_y;
73     double luma_amount, chroma_amount;
74 } UnsharpContext;
75
76 #define OFFSET(x) offsetof(UnsharpContext, x)
77 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
78
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 },
86
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 },
93
94     { NULL }
95 };
96
97 AVFILTER_DEFINE_CLASS(unsharp);
98
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)
102 {
103     uint32_t **sc = fp->sc;
104     uint32_t sr[MAX_MATRIX_SIZE - 1], tmp1, tmp2;
105
106     int32_t res;
107     int x, y, z;
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;
114
115     if (!amount) {
116         if (dst_stride == src_stride)
117             memcpy(dst, src, src_stride * height);
118         else
119             for (y = 0; y < height; y++, dst += dst_stride, src += src_stride)
120                 memcpy(dst, src, width);
121         return;
122     }
123
124     for (y = 0; y < 2 * steps_y; y++)
125         memset(sc[y], 0, sizeof(sc[y][0]) * (width + 2 * steps_x));
126
127     for (y = -steps_y; y < height + steps_y; y++) {
128         if (y < height)
129             src2 = src;
130
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;
137             }
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;
141             }
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;
145
146                 res = (int32_t)*srx + ((((int32_t) * srx - (int32_t)((tmp1 + halfscale) >> scalebits)) * amount) >> 16);
147                 *dsx = av_clip_uint8(res);
148             }
149         }
150         if (y >= 0) {
151             dst += dst_stride;
152             src += src_stride;
153         }
154     }
155 }
156
157 static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, double amount)
158 {
159     fp->msize_x = msize_x;
160     fp->msize_y = msize_y;
161     fp->amount = amount * 65536.0;
162
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);
167 }
168
169 static av_cold int init(AVFilterContext *ctx, const char *args)
170 {
171     UnsharpContext *unsharp = ctx->priv;
172
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);
175
176     return 0;
177 }
178
179 static int query_formats(AVFilterContext *ctx)
180 {
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
185     };
186
187     ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
188
189     return 0;
190 }
191
192 static int init_filter_param(AVFilterContext *ctx, FilterParam *fp, const char *effect_type, int width)
193 {
194     int z;
195     const char *effect = fp->amount == 0 ? "none" : fp->amount < 0 ? "blur" : "sharpen";
196
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);
202     }
203
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);
206
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);
210
211     return 0;
212 }
213
214 static int config_props(AVFilterLink *link)
215 {
216     UnsharpContext *unsharp = link->dst->priv;
217     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
218     int ret;
219
220     unsharp->hsub = desc->log2_chroma_w;
221     unsharp->vsub = desc->log2_chroma_h;
222
223     ret = init_filter_param(link->dst, &unsharp->luma,   "luma",   link->w);
224     if (ret < 0)
225         return ret;
226     ret = init_filter_param(link->dst, &unsharp->chroma, "chroma", SHIFTUP(link->w, unsharp->hsub));
227     if (ret < 0)
228         return ret;
229
230     return 0;
231 }
232
233 static void free_filter_param(FilterParam *fp)
234 {
235     int z;
236
237     for (z = 0; z < 2 * fp->steps_y; z++)
238         av_free(fp->sc[z]);
239 }
240
241 static av_cold void uninit(AVFilterContext *ctx)
242 {
243     UnsharpContext *unsharp = ctx->priv;
244
245     free_filter_param(&unsharp->luma);
246     free_filter_param(&unsharp->chroma);
247 }
248
249 static int filter_frame(AVFilterLink *link, AVFrame *in)
250 {
251     UnsharpContext *unsharp = link->dst->priv;
252     AVFilterLink *outlink   = link->dst->outputs[0];
253     AVFrame *out;
254     int cw = SHIFTUP(link->w, unsharp->hsub);
255     int ch = SHIFTUP(link->h, unsharp->vsub);
256
257     out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
258     if (!out) {
259         av_frame_free(&in);
260         return AVERROR(ENOMEM);
261     }
262     av_frame_copy_props(out, in);
263
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);
267
268     av_frame_free(&in);
269     return ff_filter_frame(outlink, out);
270 }
271
272 static const AVFilterPad avfilter_vf_unsharp_inputs[] = {
273     {
274         .name         = "default",
275         .type         = AVMEDIA_TYPE_VIDEO,
276         .filter_frame = filter_frame,
277         .config_props = config_props,
278     },
279     { NULL }
280 };
281
282 static const AVFilterPad avfilter_vf_unsharp_outputs[] = {
283     {
284         .name = "default",
285         .type = AVMEDIA_TYPE_VIDEO,
286     },
287     { NULL }
288 };
289
290 static const char *const shorthand[] = {
291     "luma_msize_x", "luma_msize_y", "luma_amount",
292     "chroma_msize_x", "chroma_msize_y", "chroma_amount",
293     NULL
294 };
295
296 AVFilter avfilter_vf_unsharp = {
297     .name      = "unsharp",
298     .description = NULL_IF_CONFIG_SMALL("Sharpen or blur the input video."),
299
300     .priv_size = sizeof(UnsharpContext),
301
302     .init = init,
303     .uninit = uninit,
304     .query_formats = query_formats,
305
306     .inputs    = avfilter_vf_unsharp_inputs,
307
308     .outputs   = avfilter_vf_unsharp_outputs,
309
310     .priv_class = &unsharp_class,
311     .shorthand = shorthand,
312 };