]> git.sesse.net Git - ffmpeg/blob - libavfilter/vf_gblur.c
avfilter/vf_gblur: add float format support
[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 <float.h>
29
30 #include "libavutil/imgutils.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/pixdesc.h"
33 #include "avfilter.h"
34 #include "formats.h"
35 #include "gblur.h"
36 #include "internal.h"
37 #include "video.h"
38
39 #define OFFSET(x) offsetof(GBlurContext, x)
40 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
41
42 static const AVOption gblur_options[] = {
43     { "sigma",  "set sigma",            OFFSET(sigma),  AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0.0, 1024, FLAGS },
44     { "steps",  "set number of steps",  OFFSET(steps),  AV_OPT_TYPE_INT,   {.i64=1},     1,    6, FLAGS },
45     { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT,   {.i64=0xF},   0,  0xF, FLAGS },
46     { "sigmaV", "set vertical sigma",   OFFSET(sigmaV), AV_OPT_TYPE_FLOAT, {.dbl=-1},   -1, 1024, FLAGS },
47     { NULL }
48 };
49
50 AVFILTER_DEFINE_CLASS(gblur);
51
52 typedef struct ThreadData {
53     int height;
54     int width;
55 } ThreadData;
56
57 static void horiz_slice_c(float *buffer, int width, int height, int steps,
58                           float nu, float bscale)
59 {
60     int step, x, y;
61     float *ptr;
62     for (y = 0; y < height; y++) {
63         for (step = 0; step < steps; step++) {
64             ptr = buffer + width * y;
65             ptr[0] *= bscale;
66
67             /* Filter rightwards */
68             for (x = 1; x < width; x++)
69                 ptr[x] += nu * ptr[x - 1];
70             ptr[x = width - 1] *= bscale;
71
72             /* Filter leftwards */
73             for (; x > 0; x--)
74                 ptr[x - 1] += nu * ptr[x];
75         }
76     }
77 }
78
79 static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
80 {
81     GBlurContext *s = ctx->priv;
82     ThreadData *td = arg;
83     const int height = td->height;
84     const int width = td->width;
85     const int slice_start = (height *  jobnr   ) / nb_jobs;
86     const int slice_end   = (height * (jobnr+1)) / nb_jobs;
87     const float boundaryscale = s->boundaryscale;
88     const int steps = s->steps;
89     const float nu = s->nu;
90     float *buffer = s->buffer;
91
92     s->horiz_slice(buffer + width * slice_start, width, slice_end - slice_start,
93                    steps, nu, boundaryscale);
94     emms_c();
95     return 0;
96 }
97
98 static void do_vertical_columns(float *buffer, int width, int height,
99                                 int column_begin, int column_end, int steps,
100                                 float nu, float boundaryscale, int column_step)
101 {
102     const int numpixels = width * height;
103     int i, x, k, step;
104     float *ptr;
105     for (x = column_begin; x < column_end;) {
106         for (step = 0; step < steps; step++) {
107             ptr = buffer + x;
108             for (k = 0; k < column_step; k++) {
109                 ptr[k] *= boundaryscale;
110             }
111             /* Filter downwards */
112             for (i = width; i < numpixels; i += width) {
113                 for (k = 0; k < column_step; k++) {
114                     ptr[i + k] += nu * ptr[i - width + k];
115                 }
116             }
117             i = numpixels - width;
118
119             for (k = 0; k < column_step; k++)
120                 ptr[i + k] *= boundaryscale;
121
122             /* Filter upwards */
123             for (; i > 0; i -= width) {
124                 for (k = 0; k < column_step; k++)
125                     ptr[i - width + k] += nu * ptr[i + k];
126             }
127         }
128         x += column_step;
129     }
130 }
131
132 static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
133 {
134     GBlurContext *s = ctx->priv;
135     ThreadData *td = arg;
136     const int height = td->height;
137     const int width = td->width;
138     const int slice_start = (width *  jobnr   ) / nb_jobs;
139     const int slice_end   = (width * (jobnr+1)) / nb_jobs;
140     const float boundaryscale = s->boundaryscaleV;
141     const int steps = s->steps;
142     const float nu = s->nuV;
143     float *buffer = s->buffer;
144     int aligned_end;
145
146     aligned_end = slice_start + (((slice_end - slice_start) >> 3) << 3);
147     /* Filter vertically along columns (process 8 columns in each step) */
148     do_vertical_columns(buffer, width, height, slice_start, aligned_end,
149                         steps, nu, boundaryscale, 8);
150
151     /* Filter un-aligned columns one by one */
152     do_vertical_columns(buffer, width, height, aligned_end, slice_end,
153                         steps, nu, boundaryscale, 1);
154     return 0;
155 }
156
157
158 static int filter_postscale(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
159 {
160     GBlurContext *s = ctx->priv;
161     ThreadData *td = arg;
162     const float max = s->flt ?  FLT_MAX : (1 << s->depth) - 1;
163     const float min = s->flt ? -FLT_MAX : 0.f;
164     const int height = td->height;
165     const int width = td->width;
166     const int64_t numpixels = width * (int64_t)height;
167     const unsigned slice_start = (numpixels *  jobnr   ) / nb_jobs;
168     const unsigned slice_end   = (numpixels * (jobnr+1)) / nb_jobs;
169     const float postscale = s->postscale * s->postscaleV;
170     float *buffer = s->buffer;
171     unsigned i;
172
173     for (i = slice_start; i < slice_end; i++) {
174         buffer[i] *= postscale;
175         buffer[i] = av_clipf(buffer[i], min, max);
176     }
177
178     return 0;
179 }
180
181 static void gaussianiir2d(AVFilterContext *ctx, int plane)
182 {
183     GBlurContext *s = ctx->priv;
184     const int width = s->planewidth[plane];
185     const int height = s->planeheight[plane];
186     const int nb_threads = ff_filter_get_nb_threads(ctx);
187     ThreadData td;
188
189     if (s->sigma <= 0 || s->steps < 0)
190         return;
191
192     td.width = width;
193     td.height = height;
194     ctx->internal->execute(ctx, filter_horizontally, &td, NULL, FFMIN(height, nb_threads));
195     ctx->internal->execute(ctx, filter_vertically, &td, NULL, FFMIN(width, nb_threads));
196     ctx->internal->execute(ctx, filter_postscale, &td, NULL, FFMIN(width * height, nb_threads));
197 }
198
199 static int query_formats(AVFilterContext *ctx)
200 {
201     static const enum AVPixelFormat pix_fmts[] = {
202         AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
203         AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
204         AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
205         AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
206         AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
207         AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
208         AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
209         AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
210         AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
211         AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
212         AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
213         AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
214         AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
215         AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
216         AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
217         AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
218         AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
219         AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
220         AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32,
221         AV_PIX_FMT_GRAYF32,
222         AV_PIX_FMT_NONE
223     };
224
225     return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
226 }
227
228 void ff_gblur_init(GBlurContext *s)
229 {
230     s->horiz_slice = horiz_slice_c;
231     if (ARCH_X86_64)
232         ff_gblur_init_x86(s);
233 }
234
235 static int config_input(AVFilterLink *inlink)
236 {
237     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
238     GBlurContext *s = inlink->dst->priv;
239
240     s->depth = desc->comp[0].depth;
241     s->flt = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT);
242     s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
243     s->planewidth[0] = s->planewidth[3] = inlink->w;
244     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
245     s->planeheight[0] = s->planeheight[3] = inlink->h;
246
247     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
248
249     s->buffer = av_malloc_array(FFALIGN(inlink->w, 16), FFALIGN(inlink->h, 16) * sizeof(*s->buffer));
250     if (!s->buffer)
251         return AVERROR(ENOMEM);
252
253     if (s->sigmaV < 0) {
254         s->sigmaV = s->sigma;
255     }
256     ff_gblur_init(s);
257
258     return 0;
259 }
260
261 static void set_params(float sigma, int steps, float *postscale, float *boundaryscale, float *nu)
262 {
263     double dnu, lambda;
264
265     lambda = (sigma * sigma) / (2.0 * steps);
266     dnu = (1.0 + 2.0 * lambda - sqrt(1.0 + 4.0 * lambda)) / (2.0 * lambda);
267     *postscale = pow(dnu / lambda, steps);
268     *boundaryscale = 1.0 / (1.0 - dnu);
269     *nu = (float)dnu;
270 }
271
272 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
273 {
274     AVFilterContext *ctx = inlink->dst;
275     GBlurContext *s = ctx->priv;
276     AVFilterLink *outlink = ctx->outputs[0];
277     AVFrame *out;
278     int plane;
279
280     set_params(s->sigma,  s->steps, &s->postscale,  &s->boundaryscale,  &s->nu);
281     set_params(s->sigmaV, s->steps, &s->postscaleV, &s->boundaryscaleV, &s->nuV);
282
283     if (av_frame_is_writable(in)) {
284         out = in;
285     } else {
286         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
287         if (!out) {
288             av_frame_free(&in);
289             return AVERROR(ENOMEM);
290         }
291         av_frame_copy_props(out, in);
292     }
293
294     for (plane = 0; plane < s->nb_planes; plane++) {
295         const int height = s->planeheight[plane];
296         const int width = s->planewidth[plane];
297         float *bptr = s->buffer;
298         const uint8_t *src = in->data[plane];
299         const uint16_t *src16 = (const uint16_t *)in->data[plane];
300         uint8_t *dst = out->data[plane];
301         uint16_t *dst16 = (uint16_t *)out->data[plane];
302         int y, x;
303
304         if (!s->sigma || !(s->planes & (1 << plane))) {
305             if (out != in)
306                 av_image_copy_plane(out->data[plane], out->linesize[plane],
307                                     in->data[plane], in->linesize[plane],
308                                     width * ((s->depth + 7) / 8), height);
309             continue;
310         }
311
312         if (s->flt) {
313             av_image_copy_plane((uint8_t *)bptr, width * sizeof(float),
314                                 in->data[plane], in->linesize[plane],
315                                 width * sizeof(float), height);
316         } else if (s->depth == 8) {
317             for (y = 0; y < height; y++) {
318                 for (x = 0; x < width; x++) {
319                     bptr[x] = src[x];
320                 }
321                 bptr += width;
322                 src += in->linesize[plane];
323             }
324         } else {
325             for (y = 0; y < height; y++) {
326                 for (x = 0; x < width; x++) {
327                     bptr[x] = src16[x];
328                 }
329                 bptr += width;
330                 src16 += in->linesize[plane] / 2;
331             }
332         }
333
334         gaussianiir2d(ctx, plane);
335
336         bptr = s->buffer;
337         if (s->flt) {
338             av_image_copy_plane(out->data[plane], out->linesize[plane],
339                                 (uint8_t *)bptr, width * sizeof(float),
340                                 width * sizeof(float), height);
341         } else if (s->depth == 8) {
342             for (y = 0; y < height; y++) {
343                 for (x = 0; x < width; x++) {
344                     dst[x] = bptr[x];
345                 }
346                 bptr += width;
347                 dst += out->linesize[plane];
348             }
349         } else {
350             for (y = 0; y < height; y++) {
351                 for (x = 0; x < width; x++) {
352                     dst16[x] = bptr[x];
353                 }
354                 bptr += width;
355                 dst16 += out->linesize[plane] / 2;
356             }
357         }
358     }
359
360     if (out != in)
361         av_frame_free(&in);
362     return ff_filter_frame(outlink, out);
363 }
364
365 static av_cold void uninit(AVFilterContext *ctx)
366 {
367     GBlurContext *s = ctx->priv;
368
369     av_freep(&s->buffer);
370 }
371
372 static const AVFilterPad gblur_inputs[] = {
373     {
374         .name         = "default",
375         .type         = AVMEDIA_TYPE_VIDEO,
376         .config_props = config_input,
377         .filter_frame = filter_frame,
378     },
379     { NULL }
380 };
381
382 static const AVFilterPad gblur_outputs[] = {
383     {
384         .name = "default",
385         .type = AVMEDIA_TYPE_VIDEO,
386     },
387     { NULL }
388 };
389
390 AVFilter ff_vf_gblur = {
391     .name          = "gblur",
392     .description   = NULL_IF_CONFIG_SMALL("Apply Gaussian Blur filter."),
393     .priv_size     = sizeof(GBlurContext),
394     .priv_class    = &gblur_class,
395     .uninit        = uninit,
396     .query_formats = query_formats,
397     .inputs        = gblur_inputs,
398     .outputs       = gblur_outputs,
399     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
400     .process_command = ff_filter_process_command,
401 };