Imported Upstream version 5.1.2
[platform/upstream/ffmpeg.git] / 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 "vf_gblur_init.h"
38 #include "video.h"
39
40 #define OFFSET(x) offsetof(GBlurContext, x)
41 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
42
43 static const AVOption gblur_options[] = {
44     { "sigma",  "set sigma",            OFFSET(sigma),  AV_OPT_TYPE_FLOAT, {.dbl=0.5}, 0.0, 1024, FLAGS },
45     { "steps",  "set number of steps",  OFFSET(steps),  AV_OPT_TYPE_INT,   {.i64=1},     1,    6, FLAGS },
46     { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT,   {.i64=0xF},   0,  0xF, FLAGS },
47     { "sigmaV", "set vertical sigma",   OFFSET(sigmaV), AV_OPT_TYPE_FLOAT, {.dbl=-1},   -1, 1024, FLAGS },
48     { NULL }
49 };
50
51 AVFILTER_DEFINE_CLASS(gblur);
52
53 typedef struct ThreadData {
54     int height;
55     int width;
56 } ThreadData;
57
58 static int filter_horizontally(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
59 {
60     GBlurContext *s = ctx->priv;
61     ThreadData *td = arg;
62     const int height = td->height;
63     const int width = td->width;
64     const int slice_start = (height *  jobnr   ) / nb_jobs;
65     const int slice_end   = (height * (jobnr+1)) / nb_jobs;
66     const float boundaryscale = s->boundaryscale;
67     const int steps = s->steps;
68     const float nu = s->nu;
69     float *buffer = s->buffer;
70     float *localbuf = NULL;
71
72     if (s->localbuf)
73         localbuf = s->localbuf + s->stride * width * slice_start;
74
75     s->horiz_slice(buffer + width * slice_start, width, slice_end - slice_start,
76                    steps, nu, boundaryscale, localbuf);
77     emms_c();
78     return 0;
79 }
80
81 static int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
82 {
83     GBlurContext *s = ctx->priv;
84     ThreadData *td = arg;
85     const int height = td->height;
86     const int width = td->width;
87     const int slice_start = (width *  jobnr   ) / nb_jobs;
88     const int slice_end   = (width * (jobnr+1)) / nb_jobs;
89     const float boundaryscale = s->boundaryscaleV;
90     const int steps = s->steps;
91     const float nu = s->nuV;
92     float *buffer = s->buffer;
93
94     s->verti_slice(buffer, width, height, slice_start, slice_end,
95                    steps, nu, boundaryscale);
96
97     return 0;
98 }
99
100 static int filter_postscale(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
101 {
102     GBlurContext *s = ctx->priv;
103     ThreadData *td = arg;
104     const float max = s->flt ?  FLT_MAX : (1 << s->depth) - 1;
105     const float min = s->flt ? -FLT_MAX : 0.f;
106     const int height = td->height;
107     const int width = td->width;
108     const int awidth = FFALIGN(width, 64);
109     const int slice_start = (height *  jobnr   ) / nb_jobs;
110     const int slice_end   = (height * (jobnr+1)) / nb_jobs;
111     const float postscale = s->postscale * s->postscaleV;
112     const int slice_size = slice_end - slice_start;
113
114     s->postscale_slice(s->buffer + slice_start * awidth,
115                        slice_size * awidth, postscale, min, max);
116
117     return 0;
118 }
119
120 static void gaussianiir2d(AVFilterContext *ctx, int plane)
121 {
122     GBlurContext *s = ctx->priv;
123     const int width = s->planewidth[plane];
124     const int height = s->planeheight[plane];
125     const int nb_threads = ff_filter_get_nb_threads(ctx);
126     ThreadData td;
127
128     if (s->sigma <= 0 || s->steps < 0)
129         return;
130
131     td.width = width;
132     td.height = height;
133     ff_filter_execute(ctx, filter_horizontally, &td,
134                       NULL, FFMIN(height, nb_threads));
135     ff_filter_execute(ctx, filter_vertically, &td,
136                       NULL, FFMIN(width, nb_threads));
137     ff_filter_execute(ctx, filter_postscale, &td,
138                       NULL, FFMIN(width * height, nb_threads));
139 }
140
141 static const enum AVPixelFormat pix_fmts[] = {
142     AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
143     AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
144     AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
145     AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
146     AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
147     AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
148     AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
149     AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
150     AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
151     AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
152     AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
153     AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
154     AV_PIX_FMT_YUVA422P12, AV_PIX_FMT_YUVA444P12,
155     AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
156     AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
157     AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
158     AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
159     AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
160     AV_PIX_FMT_GBRPF32, AV_PIX_FMT_GBRAPF32,
161     AV_PIX_FMT_GRAYF32,
162     AV_PIX_FMT_NONE
163 };
164
165 static av_cold void uninit(AVFilterContext *ctx)
166 {
167     GBlurContext *s = ctx->priv;
168
169     av_freep(&s->buffer);
170     av_freep(&s->localbuf);
171 }
172
173 static int config_input(AVFilterLink *inlink)
174 {
175     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
176     GBlurContext *s = inlink->dst->priv;
177
178     uninit(inlink->dst);
179
180     s->depth = desc->comp[0].depth;
181     s->flt = !!(desc->flags & AV_PIX_FMT_FLAG_FLOAT);
182     s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
183     s->planewidth[0] = s->planewidth[3] = inlink->w;
184     s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
185     s->planeheight[0] = s->planeheight[3] = inlink->h;
186
187     s->nb_planes = av_pix_fmt_count_planes(inlink->format);
188
189     s->buffer = av_malloc_array(FFALIGN(inlink->w, 64), FFALIGN(inlink->h, 64) * sizeof(*s->buffer));
190     if (!s->buffer)
191         return AVERROR(ENOMEM);
192
193     if (s->sigmaV < 0) {
194         s->sigmaV = s->sigma;
195     }
196     ff_gblur_init(s);
197
198     return 0;
199 }
200
201 static void set_params(float sigma, int steps, float *postscale, float *boundaryscale, float *nu)
202 {
203     double dnu, lambda;
204
205     lambda = (sigma * sigma) / (2.0 * steps);
206     dnu = (1.0 + 2.0 * lambda - sqrt(1.0 + 4.0 * lambda)) / (2.0 * lambda);
207     *postscale = pow(dnu / lambda, steps);
208     *boundaryscale = 1.0 / (1.0 - dnu);
209     *nu = (float)dnu;
210 }
211
212 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
213 {
214     AVFilterContext *ctx = inlink->dst;
215     GBlurContext *s = ctx->priv;
216     AVFilterLink *outlink = ctx->outputs[0];
217     AVFrame *out;
218     int plane;
219
220     set_params(s->sigma,  s->steps, &s->postscale,  &s->boundaryscale,  &s->nu);
221     set_params(s->sigmaV, s->steps, &s->postscaleV, &s->boundaryscaleV, &s->nuV);
222
223     if (av_frame_is_writable(in)) {
224         out = in;
225     } else {
226         out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
227         if (!out) {
228             av_frame_free(&in);
229             return AVERROR(ENOMEM);
230         }
231         av_frame_copy_props(out, in);
232     }
233
234     for (plane = 0; plane < s->nb_planes; plane++) {
235         const int height = s->planeheight[plane];
236         const int width = s->planewidth[plane];
237         float *bptr = s->buffer;
238         const uint8_t *src = in->data[plane];
239         const uint16_t *src16 = (const uint16_t *)in->data[plane];
240         uint8_t *dst = out->data[plane];
241         uint16_t *dst16 = (uint16_t *)out->data[plane];
242         int y, x;
243
244         if (!s->sigma || !(s->planes & (1 << plane))) {
245             if (out != in)
246                 av_image_copy_plane(out->data[plane], out->linesize[plane],
247                                     in->data[plane], in->linesize[plane],
248                                     width * ((s->depth + 7) / 8), height);
249             continue;
250         }
251
252         if (s->flt) {
253             av_image_copy_plane((uint8_t *)bptr, width * sizeof(float),
254                                 in->data[plane], in->linesize[plane],
255                                 width * sizeof(float), height);
256         } else if (s->depth == 8) {
257             for (y = 0; y < height; y++) {
258                 for (x = 0; x < width; x++) {
259                     bptr[x] = src[x];
260                 }
261                 bptr += width;
262                 src += in->linesize[plane];
263             }
264         } else {
265             for (y = 0; y < height; y++) {
266                 for (x = 0; x < width; x++) {
267                     bptr[x] = src16[x];
268                 }
269                 bptr += width;
270                 src16 += in->linesize[plane] / 2;
271             }
272         }
273
274         gaussianiir2d(ctx, plane);
275
276         bptr = s->buffer;
277         if (s->flt) {
278             av_image_copy_plane(out->data[plane], out->linesize[plane],
279                                 (uint8_t *)bptr, width * sizeof(float),
280                                 width * sizeof(float), height);
281         } else if (s->depth == 8) {
282             for (y = 0; y < height; y++) {
283                 for (x = 0; x < width; x++)
284                     dst[x] = lrintf(bptr[x]);
285                 bptr += width;
286                 dst += out->linesize[plane];
287             }
288         } else {
289             for (y = 0; y < height; y++) {
290                 for (x = 0; x < width; x++)
291                     dst16[x] = lrintf(bptr[x]);
292                 bptr += width;
293                 dst16 += out->linesize[plane] / 2;
294             }
295         }
296     }
297
298     if (out != in)
299         av_frame_free(&in);
300     return ff_filter_frame(outlink, out);
301 }
302
303 static const AVFilterPad gblur_inputs[] = {
304     {
305         .name         = "default",
306         .type         = AVMEDIA_TYPE_VIDEO,
307         .config_props = config_input,
308         .filter_frame = filter_frame,
309     },
310 };
311
312 static const AVFilterPad gblur_outputs[] = {
313     {
314         .name = "default",
315         .type = AVMEDIA_TYPE_VIDEO,
316     },
317 };
318
319 const AVFilter ff_vf_gblur = {
320     .name          = "gblur",
321     .description   = NULL_IF_CONFIG_SMALL("Apply Gaussian Blur filter."),
322     .priv_size     = sizeof(GBlurContext),
323     .priv_class    = &gblur_class,
324     .uninit        = uninit,
325     FILTER_INPUTS(gblur_inputs),
326     FILTER_OUTPUTS(gblur_outputs),
327     FILTER_PIXFMTS_ARRAY(pix_fmts),
328     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS,
329     .process_command = ff_filter_process_command,
330 };