FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vf_unsharp.c
Go to the documentation of this file.
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;
75 
76 #define OFFSET(x) offsetof(UnsharpContext, x)
77 
78 static const AVOption unsharp_options[] = {
79  { "luma_msize_x", "set luma matrix x size", OFFSET(luma_msize_x), AV_OPT_TYPE_INT, {.i64=5}, 3, 63 },
80  { "lx", "set luma matrix x size", OFFSET(luma_msize_x), AV_OPT_TYPE_INT, {.i64=5}, 3, 63 },
81  { "luma_msize_y", "set luma matrix y size", OFFSET(luma_msize_y), AV_OPT_TYPE_INT, {.i64=5}, 3, 63 },
82  { "ly", "set luma matrix y size", OFFSET(luma_msize_y), AV_OPT_TYPE_INT, {.i64=5}, 3, 63 },
83  { "luma_amount", "set luma effect amount", OFFSET(luma_amount), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, -DBL_MAX, DBL_MAX },
84  { "la", "set luma effect amount", OFFSET(luma_amount), AV_OPT_TYPE_DOUBLE, {.dbl=1.0}, -DBL_MAX, DBL_MAX },
85 
86  { "chroma_msize_x", "set chroma matrix x size", OFFSET(chroma_msize_x), AV_OPT_TYPE_INT, {.i64=5}, 3, 63 },
87  { "cx", "set chroma matrix x size", OFFSET(chroma_msize_x), AV_OPT_TYPE_INT, {.i64=5}, 3, 63 },
88  { "chroma_msize_y", "set chroma matrix y size", OFFSET(chroma_msize_y), AV_OPT_TYPE_INT, {.i64=5}, 3, 63 },
89  { "cy" , "set chroma matrix y size", OFFSET(chroma_msize_y), AV_OPT_TYPE_INT, {.i64=5}, 3, 63 },
90  { "chroma_amount", "set chroma effect strenght", OFFSET(chroma_amount), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -DBL_MAX, DBL_MAX },
91  { "ca", "set chroma effect strenght", OFFSET(chroma_amount), AV_OPT_TYPE_DOUBLE, {.dbl=0.0}, -DBL_MAX, DBL_MAX },
92 
93  { NULL }
94 };
95 
96 AVFILTER_DEFINE_CLASS(unsharp);
97 
98 static void apply_unsharp( uint8_t *dst, int dst_stride,
99  const uint8_t *src, int src_stride,
100  int width, int height, FilterParam *fp)
101 {
102  uint32_t **sc = fp->sc;
103  uint32_t sr[MAX_MATRIX_SIZE - 1], tmp1, tmp2;
104 
105  int32_t res;
106  int x, y, z;
107  const uint8_t *src2 = NULL; //silence a warning
108  const int amount = fp->amount;
109  const int steps_x = fp->steps_x;
110  const int steps_y = fp->steps_y;
111  const int scalebits = fp->scalebits;
112  const int32_t halfscale = fp->halfscale;
113 
114  if (!amount) {
115  if (dst_stride == src_stride)
116  memcpy(dst, src, src_stride * height);
117  else
118  for (y = 0; y < height; y++, dst += dst_stride, src += src_stride)
119  memcpy(dst, src, width);
120  return;
121  }
122 
123  for (y = 0; y < 2 * steps_y; y++)
124  memset(sc[y], 0, sizeof(sc[y][0]) * (width + 2 * steps_x));
125 
126  for (y = -steps_y; y < height + steps_y; y++) {
127  if (y < height)
128  src2 = src;
129 
130  memset(sr, 0, sizeof(sr[0]) * (2 * steps_x - 1));
131  for (x = -steps_x; x < width + steps_x; x++) {
132  tmp1 = x <= 0 ? src2[0] : x >= width ? src2[width-1] : src2[x];
133  for (z = 0; z < steps_x * 2; z += 2) {
134  tmp2 = sr[z + 0] + tmp1; sr[z + 0] = tmp1;
135  tmp1 = sr[z + 1] + tmp2; sr[z + 1] = tmp2;
136  }
137  for (z = 0; z < steps_y * 2; z += 2) {
138  tmp2 = sc[z + 0][x + steps_x] + tmp1; sc[z + 0][x + steps_x] = tmp1;
139  tmp1 = sc[z + 1][x + steps_x] + tmp2; sc[z + 1][x + steps_x] = tmp2;
140  }
141  if (x >= steps_x && y >= steps_y) {
142  const uint8_t *srx = src - steps_y * src_stride + x - steps_x;
143  uint8_t *dsx = dst - steps_y * dst_stride + x - steps_x;
144 
145  res = (int32_t)*srx + ((((int32_t) * srx - (int32_t)((tmp1 + halfscale) >> scalebits)) * amount) >> 16);
146  *dsx = av_clip_uint8(res);
147  }
148  }
149  if (y >= 0) {
150  dst += dst_stride;
151  src += src_stride;
152  }
153  }
154 }
155 
156 static void set_filter_param(FilterParam *fp, int msize_x, int msize_y, double amount)
157 {
158  fp->msize_x = msize_x;
159  fp->msize_y = msize_y;
160  fp->amount = amount * 65536.0;
161 
162  fp->steps_x = msize_x / 2;
163  fp->steps_y = msize_y / 2;
164  fp->scalebits = (fp->steps_x + fp->steps_y) * 2;
165  fp->halfscale = 1 << (fp->scalebits - 1);
166 }
167 
168 static av_cold int init(AVFilterContext *ctx, const char *args)
169 {
170  UnsharpContext *unsharp = ctx->priv;
171  static const char *shorthand[] = {
172  "luma_msize_x", "luma_msize_y", "luma_amount",
173  "chroma_msize_x", "chroma_msize_y", "chroma_amount",
174  NULL
175  };
176  int ret;
177 
178  unsharp->class = &unsharp_class;
179  av_opt_set_defaults(unsharp);
180 
181  if ((ret = av_opt_set_from_string(unsharp, args, shorthand, "=", ":")) < 0)
182  return ret;
183 
184  set_filter_param(&unsharp->luma, unsharp->luma_msize_x, unsharp->luma_msize_y, unsharp->luma_amount);
185  set_filter_param(&unsharp->chroma, unsharp->chroma_msize_x, unsharp->chroma_msize_y, unsharp->chroma_amount);
186 
187  return 0;
188 }
189 
191 {
192  static const enum AVPixelFormat pix_fmts[] = {
196  };
197 
199 
200  return 0;
201 }
202 
203 static int init_filter_param(AVFilterContext *ctx, FilterParam *fp, const char *effect_type, int width)
204 {
205  int z;
206  const char *effect = fp->amount == 0 ? "none" : fp->amount < 0 ? "blur" : "sharpen";
207 
208  if (!(fp->msize_x & fp->msize_y & 1)) {
209  av_log(ctx, AV_LOG_ERROR,
210  "Invalid even size for %s matrix size %dx%d\n",
211  effect_type, fp->msize_x, fp->msize_y);
212  return AVERROR(EINVAL);
213  }
214 
215  av_log(ctx, AV_LOG_VERBOSE, "effect:%s type:%s msize_x:%d msize_y:%d amount:%0.2f\n",
216  effect, effect_type, fp->msize_x, fp->msize_y, fp->amount / 65535.0);
217 
218  for (z = 0; z < 2 * fp->steps_y; z++)
219  if (!(fp->sc[z] = av_malloc(sizeof(*(fp->sc[z])) * (width + 2 * fp->steps_x))))
220  return AVERROR(ENOMEM);
221 
222  return 0;
223 }
224 
225 static int config_props(AVFilterLink *link)
226 {
227  UnsharpContext *unsharp = link->dst->priv;
228  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(link->format);
229  int ret;
230 
231  unsharp->hsub = desc->log2_chroma_w;
232  unsharp->vsub = desc->log2_chroma_h;
233 
234  ret = init_filter_param(link->dst, &unsharp->luma, "luma", link->w);
235  if (ret < 0)
236  return ret;
237  ret = init_filter_param(link->dst, &unsharp->chroma, "chroma", SHIFTUP(link->w, unsharp->hsub));
238  if (ret < 0)
239  return ret;
240 
241  return 0;
242 }
243 
245 {
246  int z;
247 
248  for (z = 0; z < 2 * fp->steps_y; z++)
249  av_free(fp->sc[z]);
250 }
251 
252 static av_cold void uninit(AVFilterContext *ctx)
253 {
254  UnsharpContext *unsharp = ctx->priv;
255 
256  free_filter_param(&unsharp->luma);
257  free_filter_param(&unsharp->chroma);
258  av_opt_free(unsharp);
259 }
260 
262 {
263  UnsharpContext *unsharp = link->dst->priv;
264  AVFilterLink *outlink = link->dst->outputs[0];
266  int cw = SHIFTUP(link->w, unsharp->hsub);
267  int ch = SHIFTUP(link->h, unsharp->vsub);
268 
269  out = ff_get_video_buffer(outlink, AV_PERM_WRITE, outlink->w, outlink->h);
270  if (!out) {
272  return AVERROR(ENOMEM);
273  }
275 
276  apply_unsharp(out->data[0], out->linesize[0], in->data[0], in->linesize[0], link->w, link->h, &unsharp->luma);
277  apply_unsharp(out->data[1], out->linesize[1], in->data[1], in->linesize[1], cw, ch, &unsharp->chroma);
278  apply_unsharp(out->data[2], out->linesize[2], in->data[2], in->linesize[2], cw, ch, &unsharp->chroma);
279 
281  return ff_filter_frame(outlink, out);
282 }
283 
285  {
286  .name = "default",
287  .type = AVMEDIA_TYPE_VIDEO,
288  .filter_frame = filter_frame,
289  .config_props = config_props,
290  .min_perms = AV_PERM_READ,
291  },
292  { NULL }
293 };
294 
296  {
297  .name = "default",
298  .type = AVMEDIA_TYPE_VIDEO,
299  },
300  { NULL }
301 };
302 
304  .name = "unsharp",
305  .description = NULL_IF_CONFIG_SMALL("Sharpen or blur the input video."),
306 
307  .priv_size = sizeof(UnsharpContext),
308 
309  .init = init,
310  .uninit = uninit,
312 
313  .inputs = avfilter_vf_unsharp_inputs,
314 
315  .outputs = avfilter_vf_unsharp_outputs,
316 
317  .priv_class = &unsharp_class,
318 };