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