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