2 * Copyright (c) 2011 Pascal Getreuer
3 * Copyright (c) 2016 Paul B Mahol
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials provided
13 * with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
36 typedef struct GBlurContext {
57 #define OFFSET(x) offsetof(GBlurContext, x)
58 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
60 static const AVOption gblur_options[] = {
61 { "sigma", "set sigma", OFFSET(sigma), AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0.0, 1024, FLAGS },
62 { "steps", "set number of steps", OFFSET(steps), AV_OPT_TYPE_INT, {.i64=1}, 1, 6, FLAGS },
63 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=0xF}, 0, 0xF, FLAGS },
64 { "sigmaV", "set vertical sigma", OFFSET(sigmaV), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 1024, FLAGS },
68 AVFILTER_DEFINE_CLASS(gblur);
70 typedef struct ThreadData {
75 static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
77 GBlurContext *s = ctx->priv;
79 const int height = td->height;
80 const int width = td->width;
81 const int slice_start = (height * jobnr ) / nb_jobs;
82 const int slice_end = (height * (jobnr+1)) / nb_jobs;
83 const float boundaryscale = s->boundaryscale;
84 const int steps = s->steps;
85 const float nu = s->nu;
86 float *buffer = s->buffer;
90 /* Filter horizontally along each row */
91 for (y = slice_start; y < slice_end; y++) {
92 for (step = 0; step < steps; step++) {
93 ptr = buffer + width * y;
94 ptr[0] *= boundaryscale;
96 /* Filter rightwards */
97 for (x = 1; x < width; x++)
98 ptr[x] += nu * ptr[x - 1];
100 ptr[x = width - 1] *= boundaryscale;
102 /* Filter leftwards */
104 ptr[x - 1] += nu * ptr[x];
111 static void do_vertical_columns(float *buffer, int width, int height,
112 int column_begin, int column_end, int steps,
113 float nu, float boundaryscale, int column_step)
115 const int numpixels = width * height;
118 for (x = column_begin; x < column_end;) {
119 for (step = 0; step < steps; step++) {
121 for (k = 0; k < column_step; k++) {
122 ptr[k] *= boundaryscale;
124 /* Filter downwards */
125 for (i = width; i < numpixels; i += width) {
126 for (k = 0; k < column_step; k++) {
127 ptr[i + k] += nu * ptr[i - width + k];
130 i = numpixels - width;
132 for (k = 0; k < column_step; k++)
133 ptr[i + k] *= boundaryscale;
136 for (; i > 0; i -= width) {
137 for (k = 0; k < column_step; k++)
138 ptr[i - width + k] += nu * ptr[i + k];
145 static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
147 GBlurContext *s = ctx->priv;
148 ThreadData *td = arg;
149 const int height = td->height;
150 const int width = td->width;
151 const int slice_start = (width * jobnr ) / nb_jobs;
152 const int slice_end = (width * (jobnr+1)) / nb_jobs;
153 const float boundaryscale = s->boundaryscaleV;
154 const int steps = s->steps;
155 const float nu = s->nuV;
156 float *buffer = s->buffer;
159 aligned_end = slice_start + (((slice_end - slice_start) >> 3) << 3);
160 /* Filter vertically along columns (process 8 columns in each step) */
161 do_vertical_columns(buffer, width, height, slice_start, aligned_end,
162 steps, nu, boundaryscale, 8);
164 /* Filter un-aligned columns one by one */
165 do_vertical_columns(buffer, width, height, aligned_end, slice_end,
166 steps, nu, boundaryscale, 1);
171 static int filter_postscale(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
173 GBlurContext *s = ctx->priv;
174 ThreadData *td = arg;
175 const int height = td->height;
176 const int width = td->width;
177 const int64_t numpixels = width * (int64_t)height;
178 const unsigned slice_start = (numpixels * jobnr ) / nb_jobs;
179 const unsigned slice_end = (numpixels * (jobnr+1)) / nb_jobs;
180 const float postscale = s->postscale * s->postscaleV;
181 float *buffer = s->buffer;
184 for (i = slice_start; i < slice_end; i++)
185 buffer[i] *= postscale;
190 static void gaussianiir2d(AVFilterContext *ctx, int plane)
192 GBlurContext *s = ctx->priv;
193 const int width = s->planewidth[plane];
194 const int height = s->planeheight[plane];
195 const int nb_threads = ff_filter_get_nb_threads(ctx);
198 if (s->sigma <= 0 || s->steps < 0)
203 ctx->internal->execute(ctx, filter_horizontally, &td, NULL, FFMIN(height, nb_threads));
204 ctx->internal->execute(ctx, filter_vertically, &td, NULL, FFMIN(width, nb_threads));
205 ctx->internal->execute(ctx, filter_postscale, &td, NULL, FFMIN(width * height, nb_threads));
208 static int query_formats(AVFilterContext *ctx)
210 static const enum AVPixelFormat pix_fmts[] = {
211 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
212 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
213 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
214 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
215 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
216 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
217 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
218 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
219 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
220 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
221 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
222 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
223 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
224 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
225 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
226 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
227 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
231 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
234 static int config_input(AVFilterLink *inlink)
236 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
237 GBlurContext *s = inlink->dst->priv;
239 s->depth = desc->comp[0].depth;
240 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
241 s->planewidth[0] = s->planewidth[3] = inlink->w;
242 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
243 s->planeheight[0] = s->planeheight[3] = inlink->h;
245 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
247 s->buffer = av_malloc_array(inlink->w, inlink->h * sizeof(*s->buffer));
249 return AVERROR(ENOMEM);
252 s->sigmaV = s->sigma;
258 static void set_params(float sigma, int steps, float *postscale, float *boundaryscale, float *nu)
262 lambda = (sigma * sigma) / (2.0 * steps);
263 dnu = (1.0 + 2.0 * lambda - sqrt(1.0 + 4.0 * lambda)) / (2.0 * lambda);
264 *postscale = pow(dnu / lambda, steps);
265 *boundaryscale = 1.0 / (1.0 - dnu);
269 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
271 AVFilterContext *ctx = inlink->dst;
272 GBlurContext *s = ctx->priv;
273 AVFilterLink *outlink = ctx->outputs[0];
277 set_params(s->sigma, s->steps, &s->postscale, &s->boundaryscale, &s->nu);
278 set_params(s->sigmaV, s->steps, &s->postscaleV, &s->boundaryscaleV, &s->nuV);
280 if (av_frame_is_writable(in)) {
283 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
286 return AVERROR(ENOMEM);
288 av_frame_copy_props(out, in);
291 for (plane = 0; plane < s->nb_planes; plane++) {
292 const int height = s->planeheight[plane];
293 const int width = s->planewidth[plane];
294 float *bptr = s->buffer;
295 const uint8_t *src = in->data[plane];
296 const uint16_t *src16 = (const uint16_t *)in->data[plane];
297 uint8_t *dst = out->data[plane];
298 uint16_t *dst16 = (uint16_t *)out->data[plane];
301 if (!s->sigma || !(s->planes & (1 << plane))) {
303 av_image_copy_plane(out->data[plane], out->linesize[plane],
304 in->data[plane], in->linesize[plane],
305 width * ((s->depth + 7) / 8), height);
310 for (y = 0; y < height; y++) {
311 for (x = 0; x < width; x++) {
315 src += in->linesize[plane];
318 for (y = 0; y < height; y++) {
319 for (x = 0; x < width; x++) {
323 src16 += in->linesize[plane] / 2;
327 gaussianiir2d(ctx, plane);
331 for (y = 0; y < height; y++) {
332 for (x = 0; x < width; x++) {
336 dst += out->linesize[plane];
339 for (y = 0; y < height; y++) {
340 for (x = 0; x < width; x++) {
344 dst16 += out->linesize[plane] / 2;
351 return ff_filter_frame(outlink, out);
354 static av_cold void uninit(AVFilterContext *ctx)
356 GBlurContext *s = ctx->priv;
358 av_freep(&s->buffer);
361 static const AVFilterPad gblur_inputs[] = {
364 .type = AVMEDIA_TYPE_VIDEO,
365 .config_props = config_input,
366 .filter_frame = filter_frame,
371 static const AVFilterPad gblur_outputs[] = {
374 .type = AVMEDIA_TYPE_VIDEO,
379 AVFilter ff_vf_gblur = {
381 .description = NULL_IF_CONFIG_SMALL("Apply Gaussian Blur filter."),
382 .priv_size = sizeof(GBlurContext),
383 .priv_class = &gblur_class,
385 .query_formats = query_formats,
386 .inputs = gblur_inputs,
387 .outputs = gblur_outputs,
388 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,