2 * Copyright (c) 2017 Ming Yang
3 * Copyright (c) 2019 Paul B Mahol
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #include "libavutil/imgutils.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/pixdesc.h"
32 typedef struct BilateralContext {
45 float range_table[65536];
51 float *slice_factor_a;
52 float *slice_factor_b;
57 #define OFFSET(x) offsetof(BilateralContext, x)
58 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
60 static const AVOption bilateral_options[] = {
61 { "sigmaS", "set spatial sigma", OFFSET(sigmaS), AV_OPT_TYPE_FLOAT, {.dbl=0.1}, 0.0, 512, FLAGS },
62 { "sigmaR", "set range sigma", OFFSET(sigmaR), AV_OPT_TYPE_FLOAT, {.dbl=0.1}, 0.0, 1, FLAGS },
63 { "planes", "set planes to filter", OFFSET(planes), AV_OPT_TYPE_INT, {.i64=1}, 0, 0xF, FLAGS },
67 AVFILTER_DEFINE_CLASS(bilateral);
69 static int query_formats(AVFilterContext *ctx)
71 static const enum AVPixelFormat pix_fmts[] = {
72 AV_PIX_FMT_YUVA444P, AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV440P,
73 AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ440P,
74 AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUV420P,
75 AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
76 AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUV411P, AV_PIX_FMT_YUV410P,
77 AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
78 AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV444P10,
79 AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12, AV_PIX_FMT_YUV440P12,
80 AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
81 AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
82 AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
83 AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
84 AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
85 AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
86 AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14, AV_PIX_FMT_GBRP16,
87 AV_PIX_FMT_GBRAP, AV_PIX_FMT_GBRAP10, AV_PIX_FMT_GBRAP12, AV_PIX_FMT_GBRAP16,
88 AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY9, AV_PIX_FMT_GRAY10, AV_PIX_FMT_GRAY12, AV_PIX_FMT_GRAY14, AV_PIX_FMT_GRAY16,
92 return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
95 static int config_params(AVFilterContext *ctx)
97 BilateralContext *s = ctx->priv;
98 float inv_sigma_range;
100 inv_sigma_range = 1.0f / (s->sigmaR * ((1 << s->depth) - 1));
101 s->alpha = expf(-sqrtf(2.f) / s->sigmaS);
103 //compute a lookup table
104 for (int i = 0; i < (1 << s->depth); i++)
105 s->range_table[i] = s->alpha * expf(-i * inv_sigma_range);
110 static int config_input(AVFilterLink *inlink)
112 AVFilterContext *ctx = inlink->dst;
113 BilateralContext *s = ctx->priv;
114 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
116 s->depth = desc->comp[0].depth;
120 s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
121 s->planewidth[0] = s->planewidth[3] = inlink->w;
122 s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
123 s->planeheight[0] = s->planeheight[3] = inlink->h;
125 s->nb_planes = av_pix_fmt_count_planes(inlink->format);
127 s->img_out_f = av_calloc(inlink->w * inlink->h, sizeof(float));
128 s->img_temp = av_calloc(inlink->w * inlink->h, sizeof(float));
129 s->map_factor_a = av_calloc(inlink->w * inlink->h, sizeof(float));
130 s->map_factor_b = av_calloc(inlink->w * inlink->h, sizeof(float));
131 s->slice_factor_a = av_calloc(inlink->w, sizeof(float));
132 s->slice_factor_b = av_calloc(inlink->w, sizeof(float));
133 s->line_factor_a = av_calloc(inlink->w, sizeof(float));
134 s->line_factor_b = av_calloc(inlink->w, sizeof(float));
140 !s->slice_factor_a ||
141 !s->slice_factor_a ||
144 return AVERROR(ENOMEM);
149 #define BILATERAL(type, name) \
150 static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *ddst, \
151 float sigma_spatial, float sigma_range, \
152 int width, int height, int src_linesize, int dst_linesize) \
154 type *dst = (type *)ddst; \
155 const type *src = (const type *)ssrc; \
156 float *img_out_f = s->img_out_f, *img_temp = s->img_temp; \
157 float *map_factor_a = s->map_factor_a, *map_factor_b = s->map_factor_b; \
158 float *slice_factor_a = s->slice_factor_a, *slice_factor_b = s->slice_factor_b; \
159 float *line_factor_a = s->line_factor_a, *line_factor_b = s->line_factor_b; \
160 const float *range_table = s->range_table; \
161 const float alpha = s->alpha; \
162 float ypr, ycr, *ycy, *ypy, *xcy, fp, fc; \
163 const float inv_alpha_ = 1.f - alpha; \
164 float *ycf, *ypf, *xcf, *in_factor; \
165 const type *tcy, *tpy; \
168 for (int y = 0; y < height; y++) { \
169 float *temp_factor_x, *temp_x = &img_temp[y * width]; \
170 const type *in_x = &src[y * src_linesize]; \
171 const type *texture_x = &src[y * src_linesize]; \
174 *temp_x++ = ypr = *in_x++; \
175 tpr = *texture_x++; \
177 temp_factor_x = &map_factor_a[y * width]; \
178 *temp_factor_x++ = fp = 1; \
180 for (int x = 1; x < width; x++) { \
183 type tcr = *texture_x++; \
184 type dr = abs(tcr - tpr); \
187 alpha_ = range_table[range_dist]; \
188 *temp_x++ = ycr = inv_alpha_*(*in_x++) + alpha_*ypr; \
191 *temp_factor_x++ = fc = inv_alpha_ + alpha_ * fp; \
194 --temp_x; *temp_x = 0.5f*((*temp_x) + (*--in_x)); \
195 tpr = *--texture_x; \
198 --temp_factor_x; *temp_factor_x = 0.5f*((*temp_factor_x) + 1); \
201 for (int x = width - 2; x >= 0; x--) { \
202 type tcr = *--texture_x; \
203 type dr = abs(tcr - tpr); \
204 int range_dist = dr; \
205 float alpha_ = range_table[range_dist]; \
207 ycr = inv_alpha_ * (*--in_x) + alpha_ * ypr; \
208 --temp_x; *temp_x = 0.5f*((*temp_x) + ycr); \
212 fc = inv_alpha_ + alpha_*fp; \
214 *temp_factor_x = 0.5f*((*temp_factor_x) + fc); \
218 memcpy(img_out_f, img_temp, sizeof(float) * width); \
220 in_factor = map_factor_a; \
221 memcpy(map_factor_b, in_factor, sizeof(float) * width); \
222 for (int y = 1; y < height; y++) { \
223 tpy = &src[(y - 1) * src_linesize]; \
224 tcy = &src[y * src_linesize]; \
225 xcy = &img_temp[y * width]; \
226 ypy = &img_out_f[(y - 1) * width]; \
227 ycy = &img_out_f[y * width]; \
229 xcf = &in_factor[y * width]; \
230 ypf = &map_factor_b[(y - 1) * width]; \
231 ycf = &map_factor_b[y * width]; \
232 for (int x = 0; x < width; x++) { \
233 type dr = abs((*tcy++) - (*tpy++)); \
234 int range_dist = dr; \
235 float alpha_ = range_table[range_dist]; \
237 *ycy++ = inv_alpha_*(*xcy++) + alpha_*(*ypy++); \
238 *ycf++ = inv_alpha_*(*xcf++) + alpha_*(*ypf++); \
242 ycf = line_factor_a; \
243 ypf = line_factor_b; \
244 memcpy(ypf, &in_factor[h1 * width], sizeof(float) * width); \
245 for (int x = 0; x < width; x++) \
246 map_factor_b[h1 * width + x] = 0.5f*(map_factor_b[h1 * width + x] + ypf[x]); \
248 ycy = slice_factor_a; \
249 ypy = slice_factor_b; \
250 memcpy(ypy, &img_temp[h1 * width], sizeof(float) * width); \
251 for (int x = 0, k = 0; x < width; x++) { \
252 int idx = h1 * width + x; \
253 img_out_f[idx] = 0.5f*(img_out_f[idx] + ypy[k++]) / map_factor_b[h1 * width + x]; \
256 for (int y = h1 - 1; y >= 0; y--) { \
257 float *ycf_, *ypf_, *factor_; \
258 float *ycy_, *ypy_, *out_; \
260 tpy = &src[(y + 1) * src_linesize]; \
261 tcy = &src[y * src_linesize]; \
262 xcy = &img_temp[y * width]; \
265 out_ = &img_out_f[y * width]; \
267 xcf = &in_factor[y * width]; \
270 factor_ = &map_factor_b[y * width]; \
271 for (int x = 0; x < width; x++) { \
272 type dr = abs((*tcy++) - (*tpy++)); \
273 int range_dist = dr; \
274 float alpha_ = range_table[range_dist]; \
275 float ycc, fcc = inv_alpha_*(*xcf++) + alpha_*(*ypf_++); \
278 *factor_ = 0.5f * (*factor_ + fcc); \
280 ycc = inv_alpha_*(*xcy++) + alpha_*(*ypy_++); \
282 *out_ = 0.5f * (*out_ + ycc) / (*factor_); \
291 for (int i = 0; i < height; i++) \
292 for (int j = 0; j < width; j++) \
293 dst[j + i * dst_linesize] = img_out_f[i * width + j]; \
296 BILATERAL(uint8_t, byte)
297 BILATERAL(uint16_t, word)
299 static int filter_frame(AVFilterLink *inlink, AVFrame *in)
301 AVFilterContext *ctx = inlink->dst;
302 BilateralContext *s = ctx->priv;
303 AVFilterLink *outlink = ctx->outputs[0];
306 out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
309 return AVERROR(ENOMEM);
311 av_frame_copy_props(out, in);
313 for (int plane = 0; plane < s->nb_planes; plane++) {
314 if (!(s->planes & (1 << plane))) {
315 av_image_copy_plane(out->data[plane], out->linesize[plane],
316 in->data[plane], in->linesize[plane],
317 s->planewidth[plane] * ((s->depth + 7) / 8), s->planeheight[plane]);
322 bilateral_byte(s, in->data[plane], out->data[plane], s->sigmaS, s->sigmaR,
323 s->planewidth[plane], s->planeheight[plane],
324 in->linesize[plane], out->linesize[plane]);
326 bilateral_word(s, in->data[plane], out->data[plane], s->sigmaS, s->sigmaR,
327 s->planewidth[plane], s->planeheight[plane],
328 in->linesize[plane] / 2, out->linesize[plane] / 2);
332 return ff_filter_frame(outlink, out);
335 static av_cold void uninit(AVFilterContext *ctx)
337 BilateralContext *s = ctx->priv;
339 av_freep(&s->img_out_f);
340 av_freep(&s->img_temp);
341 av_freep(&s->map_factor_a);
342 av_freep(&s->map_factor_b);
343 av_freep(&s->slice_factor_a);
344 av_freep(&s->slice_factor_b);
345 av_freep(&s->line_factor_a);
346 av_freep(&s->line_factor_b);
349 static int process_command(AVFilterContext *ctx,
356 int ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags);
361 return config_params(ctx);
364 static const AVFilterPad bilateral_inputs[] = {
367 .type = AVMEDIA_TYPE_VIDEO,
368 .config_props = config_input,
369 .filter_frame = filter_frame,
374 static const AVFilterPad bilateral_outputs[] = {
377 .type = AVMEDIA_TYPE_VIDEO,
382 const AVFilter ff_vf_bilateral = {
384 .description = NULL_IF_CONFIG_SMALL("Apply Bilateral filter."),
385 .priv_size = sizeof(BilateralContext),
386 .priv_class = &bilateral_class,
388 .query_formats = query_formats,
389 .inputs = bilateral_inputs,
390 .outputs = bilateral_outputs,
391 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
392 .process_command = process_command,