blob: f843e3f376fd89f3f2dc920d9926100b4c27c123
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 int filter_vertically(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
112 | { |
113 | GBlurContext *s = ctx->priv; |
114 | ThreadData *td = arg; |
115 | const int height = td->height; |
116 | const int width = td->width; |
117 | const int slice_start = (width * jobnr ) / nb_jobs; |
118 | const int slice_end = (width * (jobnr+1)) / nb_jobs; |
119 | const float boundaryscale = s->boundaryscaleV; |
120 | const int numpixels = width * height; |
121 | const int steps = s->steps; |
122 | const float nu = s->nuV; |
123 | float *buffer = s->buffer; |
124 | int i, x, step; |
125 | float *ptr; |
126 | |
127 | /* Filter vertically along each column */ |
128 | for (x = slice_start; x < slice_end; x++) { |
129 | for (step = 0; step < steps; step++) { |
130 | ptr = buffer + x; |
131 | ptr[0] *= boundaryscale; |
132 | |
133 | /* Filter downwards */ |
134 | for (i = width; i < numpixels; i += width) |
135 | ptr[i] += nu * ptr[i - width]; |
136 | |
137 | ptr[i = numpixels - width] *= boundaryscale; |
138 | |
139 | /* Filter upwards */ |
140 | for (; i > 0; i -= width) |
141 | ptr[i - width] += nu * ptr[i]; |
142 | } |
143 | } |
144 | |
145 | return 0; |
146 | } |
147 | |
148 | |
149 | static int filter_postscale(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs) |
150 | { |
151 | GBlurContext *s = ctx->priv; |
152 | ThreadData *td = arg; |
153 | const int height = td->height; |
154 | const int width = td->width; |
155 | const int64_t numpixels = width * (int64_t)height; |
156 | const unsigned slice_start = (numpixels * jobnr ) / nb_jobs; |
157 | const unsigned slice_end = (numpixels * (jobnr+1)) / nb_jobs; |
158 | const float postscale = s->postscale * s->postscaleV; |
159 | float *buffer = s->buffer; |
160 | unsigned i; |
161 | |
162 | for (i = slice_start; i < slice_end; i++) |
163 | buffer[i] *= postscale; |
164 | |
165 | return 0; |
166 | } |
167 | |
168 | static void gaussianiir2d(AVFilterContext *ctx, int plane) |
169 | { |
170 | GBlurContext *s = ctx->priv; |
171 | const int width = s->planewidth[plane]; |
172 | const int height = s->planeheight[plane]; |
173 | const int nb_threads = ff_filter_get_nb_threads(ctx); |
174 | ThreadData td; |
175 | |
176 | if (s->sigma <= 0 || s->steps < 0) |
177 | return; |
178 | |
179 | td.width = width; |
180 | td.height = height; |
181 | ctx->internal->execute(ctx, filter_horizontally, &td, NULL, FFMIN(height, nb_threads)); |
182 | ctx->internal->execute(ctx, filter_vertically, &td, NULL, FFMIN(width, nb_threads)); |
183 | ctx->internal->execute(ctx, filter_postscale, &td, NULL, FFMIN(width * height, nb_threads)); |
184 | } |
185 | |
186 | static int query_formats(AVFilterContext *ctx) |
187 | { |
188 | static const enum AVPixelFormat pix_fmts[] = { |
189 | AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P, |
190 | AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P, |
191 | AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P, |
192 | AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P, |
193 | AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P, |
194 | AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9, |
195 | AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10, |
196 | AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12, |
197 | AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14, |
198 | AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16, |
199 | AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9, |
200 | AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10, |
201 | AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16, |
202 | AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10, |
203 | AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16, |
204 | AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16, |
205 | AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16, |
206 | AV_PIX_FMT_NONE |
207 | }; |
208 | |
209 | return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); |
210 | } |
211 | |
212 | static int config_input(AVFilterLink *inlink) |
213 | { |
214 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format); |
215 | GBlurContext *s = inlink->dst->priv; |
216 | |
217 | s->depth = desc->comp[0].depth; |
218 | s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w); |
219 | s->planewidth[0] = s->planewidth[3] = inlink->w; |
220 | s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h); |
221 | s->planeheight[0] = s->planeheight[3] = inlink->h; |
222 | |
223 | s->nb_planes = av_pix_fmt_count_planes(inlink->format); |
224 | |
225 | s->buffer = av_malloc_array(inlink->w, inlink->h * sizeof(*s->buffer)); |
226 | if (!s->buffer) |
227 | return AVERROR(ENOMEM); |
228 | |
229 | if (s->sigmaV < 0) { |
230 | s->sigmaV = s->sigma; |
231 | } |
232 | |
233 | return 0; |
234 | } |
235 | |
236 | static void set_params(float sigma, int steps, float *postscale, float *boundaryscale, float *nu) |
237 | { |
238 | double dnu, lambda; |
239 | |
240 | lambda = (sigma * sigma) / (2.0 * steps); |
241 | dnu = (1.0 + 2.0 * lambda - sqrt(1.0 + 4.0 * lambda)) / (2.0 * lambda); |
242 | *postscale = pow(dnu / lambda, steps); |
243 | *boundaryscale = 1.0 / (1.0 - dnu); |
244 | *nu = (float)dnu; |
245 | } |
246 | |
247 | static int filter_frame(AVFilterLink *inlink, AVFrame *in) |
248 | { |
249 | AVFilterContext *ctx = inlink->dst; |
250 | GBlurContext *s = ctx->priv; |
251 | AVFilterLink *outlink = ctx->outputs[0]; |
252 | AVFrame *out; |
253 | int plane; |
254 | |
255 | set_params(s->sigma, s->steps, &s->postscale, &s->boundaryscale, &s->nu); |
256 | set_params(s->sigmaV, s->steps, &s->postscaleV, &s->boundaryscaleV, &s->nuV); |
257 | |
258 | if (av_frame_is_writable(in)) { |
259 | out = in; |
260 | } else { |
261 | out = ff_get_video_buffer(outlink, outlink->w, outlink->h); |
262 | if (!out) { |
263 | av_frame_free(&in); |
264 | return AVERROR(ENOMEM); |
265 | } |
266 | av_frame_copy_props(out, in); |
267 | } |
268 | |
269 | for (plane = 0; plane < s->nb_planes; plane++) { |
270 | const int height = s->planeheight[plane]; |
271 | const int width = s->planewidth[plane]; |
272 | float *bptr = s->buffer; |
273 | const uint8_t *src = in->data[plane]; |
274 | const uint16_t *src16 = (const uint16_t *)in->data[plane]; |
275 | uint8_t *dst = out->data[plane]; |
276 | uint16_t *dst16 = (uint16_t *)out->data[plane]; |
277 | int y, x; |
278 | |
279 | if (!s->sigma || !(s->planes & (1 << plane))) { |
280 | if (out != in) |
281 | av_image_copy_plane(out->data[plane], out->linesize[plane], |
282 | in->data[plane], in->linesize[plane], |
283 | width * ((s->depth + 7) / 8), height); |
284 | continue; |
285 | } |
286 | |
287 | if (s->depth == 8) { |
288 | for (y = 0; y < height; y++) { |
289 | for (x = 0; x < width; x++) { |
290 | bptr[x] = src[x]; |
291 | } |
292 | bptr += width; |
293 | src += in->linesize[plane]; |
294 | } |
295 | } else { |
296 | for (y = 0; y < height; y++) { |
297 | for (x = 0; x < width; x++) { |
298 | bptr[x] = src16[x]; |
299 | } |
300 | bptr += width; |
301 | src16 += in->linesize[plane] / 2; |
302 | } |
303 | } |
304 | |
305 | gaussianiir2d(ctx, plane); |
306 | |
307 | bptr = s->buffer; |
308 | if (s->depth == 8) { |
309 | for (y = 0; y < height; y++) { |
310 | for (x = 0; x < width; x++) { |
311 | dst[x] = bptr[x]; |
312 | } |
313 | bptr += width; |
314 | dst += out->linesize[plane]; |
315 | } |
316 | } else { |
317 | for (y = 0; y < height; y++) { |
318 | for (x = 0; x < width; x++) { |
319 | dst16[x] = bptr[x]; |
320 | } |
321 | bptr += width; |
322 | dst16 += out->linesize[plane] / 2; |
323 | } |
324 | } |
325 | } |
326 | |
327 | if (out != in) |
328 | av_frame_free(&in); |
329 | return ff_filter_frame(outlink, out); |
330 | } |
331 | |
332 | static av_cold void uninit(AVFilterContext *ctx) |
333 | { |
334 | GBlurContext *s = ctx->priv; |
335 | |
336 | av_freep(&s->buffer); |
337 | } |
338 | |
339 | static const AVFilterPad gblur_inputs[] = { |
340 | { |
341 | .name = "default", |
342 | .type = AVMEDIA_TYPE_VIDEO, |
343 | .config_props = config_input, |
344 | .filter_frame = filter_frame, |
345 | }, |
346 | { NULL } |
347 | }; |
348 | |
349 | static const AVFilterPad gblur_outputs[] = { |
350 | { |
351 | .name = "default", |
352 | .type = AVMEDIA_TYPE_VIDEO, |
353 | }, |
354 | { NULL } |
355 | }; |
356 | |
357 | AVFilter ff_vf_gblur = { |
358 | .name = "gblur", |
359 | .description = NULL_IF_CONFIG_SMALL("Apply Gaussian Blur filter."), |
360 | .priv_size = sizeof(GBlurContext), |
361 | .priv_class = &gblur_class, |
362 | .uninit = uninit, |
363 | .query_formats = query_formats, |
364 | .inputs = gblur_inputs, |
365 | .outputs = gblur_outputs, |
366 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC | AVFILTER_FLAG_SLICE_THREADS, |
367 | }; |
368 |