]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_gblur.c
lavfi/gblur: doing several columns at the same time
[ffmpeg] / libavfilter / vf_gblur.c
1 /*
2  * Copyright (c) 2011 Pascal Getreuer
3  * Copyright (c) 2016 Paul B Mahol
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
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.
14  *
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.
26  */
27
28 #include "libavutil/imgutils.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/pixdesc.h"
31 #include "avfilter.h"
32 #include "formats.h"
33 #include "internal.h"
34 #include "video.h"
35
36 typedef struct GBlurContext {
37     const AVClass *class;
38
39     float sigma;
40     float sigmaV;
41     int steps;
42     int planes;
43
44     int depth;
45     int planewidth[4];
46     int planeheight[4];
47     float *buffer;
48     float boundaryscale;
49     float boundaryscaleV;
50     float postscale;
51     float postscaleV;
52     float nu;
53     float nuV;
54     int nb_planes;
55 } GBlurContext;
56
57 #define OFFSET(x) offsetof(GBlurContext, x)
58 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59
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 },
65     { NULL }
66 };
67
68 AVFILTER_DEFINE_CLASS(gblur);
69
70 typedef struct ThreadData {
71     int height;
72     int width;
73 } ThreadData;
74
75 static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
76 {
77     GBlurContext *s = ctx->priv;
78     ThreadData *td = arg;
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;
87     int y, x, step;
88     float *ptr;
89
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;
95
96             /* Filter rightwards */
97             for (x = 1; x < width; x++)
98                 ptr[x] += nu * ptr[x - 1];
99
100             ptr[x = width - 1] *= boundaryscale;
101
102             /* Filter leftwards */
103             for (; x > 0; x--)
104                 ptr[x - 1] += nu * ptr[x];
105         }
106     }
107
108     return 0;
109 }
110
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)
114 {
115     const int numpixels = width * height;
116     int i, x, k, step;
117     float *ptr;
118     for (x = column_begin; x < column_end;) {
119         for (step = 0; step < steps; step++) {
120             ptr = buffer + x;
121             for (k = 0; k < column_step; k++) {
122                 ptr[k] *= boundaryscale;
123             }
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];
128                 }
129             }
130             i = numpixels - width;
131
132             for (k = 0; k < column_step; k++)
133                 ptr[i + k] *= boundaryscale;
134
135             /* Filter upwards */
136             for (; i > 0; i -= width) {
137                 for (k = 0; k < column_step; k++)
138                     ptr[i - width + k] += nu * ptr[i + k];
139             }
140         }
141         x += column_step;
142     }
143 }
144
145 static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
146 {
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;
157     int aligned_end;
158
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);
163
164     /* Filter un-aligned columns one by one */
165     do_vertical_columns(buffer, width, height, aligned_end, slice_end,
166                         steps, nu, boundaryscale, 1);
167     return 0;
168 }
169
170
171 static int filter_postscale(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
172 {
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;
182     unsigned i;
183
184     for (i = slice_start; i < slice_end; i++)
185         buffer[i] *= postscale;
186
187     return 0;
188 }
189
190 static void gaussianiir2d(AVFilterContext *ctx, int plane)
191 {
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);
196     ThreadData td;
197
198     if (s->sigma <= 0 || s->steps < 0)
199         return;
200
201     td.width = width;
202     td.height = height;
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));
206 }
207
208 static int query_formats(AVFilterContext *ctx)
209 {
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,
228         AV_PIX_FMT_NONE
229     };
230
231     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
232 }
233
234 static int config_input(AVFilterLink *inlink)
235 {
236     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
237     GBlurContext *s = inlink->dst->priv;
238
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;
244
245     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
246
247     s->buffer = av_malloc_array(inlink->w, inlink->h * sizeof(*s->buffer));
248     if (!s->buffer)
249         return AVERROR(ENOMEM);
250
251     if (s->sigmaV < 0) {
252         s->sigmaV = s->sigma;
253     }
254
255     return 0;
256 }
257
258 static void set_params(float sigma, int steps, float *postscale, float *boundaryscale, float *nu)
259 {
260     double dnu, lambda;
261
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);
266     *nu = (float)dnu;
267 }
268
269 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
270 {
271     AVFilterContext *ctx = inlink->dst;
272     GBlurContext *s = ctx->priv;
273     AVFilterLink *outlink = ctx->outputs[0];
274     AVFrame *out;
275     int plane;
276
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);
279
280     if (av_frame_is_writable(in)) {
281         out = in;
282     } else {
283         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
284         if (!out) {
285             av_frame_free(&in);
286             return AVERROR(ENOMEM);
287         }
288         av_frame_copy_props(out, in);
289     }
290
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];
299         int y, x;
300
301         if (!s->sigma || !(s->planes & (1 << plane))) {
302             if (out != in)
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);
306             continue;
307         }
308
309         if (s->depth == 8) {
310             for (y = 0; y < height; y++) {
311                 for (x = 0; x < width; x++) {
312                     bptr[x] = src[x];
313                 }
314                 bptr += width;
315                 src += in->linesize[plane];
316             }
317         } else {
318             for (y = 0; y < height; y++) {
319                 for (x = 0; x < width; x++) {
320                     bptr[x] = src16[x];
321                 }
322                 bptr += width;
323                 src16 += in->linesize[plane] / 2;
324             }
325         }
326
327         gaussianiir2d(ctx, plane);
328
329         bptr = s->buffer;
330         if (s->depth == 8) {
331             for (y = 0; y < height; y++) {
332                 for (x = 0; x < width; x++) {
333                     dst[x] = bptr[x];
334                 }
335                 bptr += width;
336                 dst += out->linesize[plane];
337             }
338         } else {
339             for (y = 0; y < height; y++) {
340                 for (x = 0; x < width; x++) {
341                     dst16[x] = bptr[x];
342                 }
343                 bptr += width;
344                 dst16 += out->linesize[plane] / 2;
345             }
346         }
347     }
348
349     if (out != in)
350         av_frame_free(&in);
351     return ff_filter_frame(outlink, out);
352 }
353
354 static av_cold void uninit(AVFilterContext *ctx)
355 {
356     GBlurContext *s = ctx->priv;
357
358     av_freep(&s->buffer);
359 }
360
361 static const AVFilterPad gblur_inputs[] = {
362     {
363         .name         = "default",
364         .type         = AVMEDIA_TYPE_VIDEO,
365         .config_props = config_input,
366         .filter_frame = filter_frame,
367     },
368     { NULL }
369 };
370
371 static const AVFilterPad gblur_outputs[] = {
372     {
373         .name = "default",
374         .type = AVMEDIA_TYPE_VIDEO,
375     },
376     { NULL }
377 };
378
379 AVFilter ff_vf_gblur = {
380     .name          = "gblur",
381     .description   = NULL_IF_CONFIG_SMALL("Apply Gaussian Blur filter."),
382     .priv_size     = sizeof(GBlurContext),
383     .priv_class    = &gblur_class,
384     .uninit        = uninit,
385     .query_formats = query_formats,
386     .inputs        = gblur_inputs,
387     .outputs       = gblur_outputs,
388     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
389 };