GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/vf_bilateral.c Lines: 0 67 0.0 %
Date: 2020-08-14 10:39:37 Branches: 0 70 0.0 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2017 Ming Yang
3
 * Copyright (c) 2019 Paul B Mahol
4
 *
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:
11
 *
12
 * The above copyright notice and this permission notice shall be included in all
13
 * copies or substantial portions of the Software.
14
 *
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
21
 * SOFTWARE.
22
 */
23
24
#include "libavutil/imgutils.h"
25
#include "libavutil/opt.h"
26
#include "libavutil/pixdesc.h"
27
#include "avfilter.h"
28
#include "formats.h"
29
#include "internal.h"
30
#include "video.h"
31
32
typedef struct BilateralContext {
33
    const AVClass *class;
34
35
    float sigmaS;
36
    float sigmaR;
37
    int planes;
38
39
    int nb_planes;
40
    int depth;
41
    int planewidth[4];
42
    int planeheight[4];
43
44
    float alpha;
45
    float range_table[65536];
46
47
    float *img_out_f;
48
    float *img_temp;
49
    float *map_factor_a;
50
    float *map_factor_b;
51
    float *slice_factor_a;
52
    float *slice_factor_b;
53
    float *line_factor_a;
54
    float *line_factor_b;
55
} BilateralContext;
56
57
#define OFFSET(x) offsetof(BilateralContext, x)
58
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
59
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 },
64
    { NULL }
65
};
66
67
AVFILTER_DEFINE_CLASS(bilateral);
68
69
static int query_formats(AVFilterContext *ctx)
70
{
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,
89
        AV_PIX_FMT_NONE
90
    };
91
92
    return ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
93
}
94
95
static int config_input(AVFilterLink *inlink)
96
{
97
    BilateralContext *s = inlink->dst->priv;
98
    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
99
    float inv_sigma_range;
100
101
    s->depth = desc->comp[0].depth;
102
    inv_sigma_range = 1.0f / (s->sigmaR * ((1 << s->depth) - 1));
103
    s->alpha = expf(-sqrtf(2.f) / s->sigmaS);
104
105
    //compute a lookup table
106
    for (int i = 0; i < (1 << s->depth); i++)
107
        s->range_table[i] = s->alpha * expf(-i * inv_sigma_range);
108
109
    s->planewidth[1] = s->planewidth[2] = AV_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
110
    s->planewidth[0] = s->planewidth[3] = inlink->w;
111
    s->planeheight[1] = s->planeheight[2] = AV_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
112
    s->planeheight[0] = s->planeheight[3] = inlink->h;
113
114
    s->nb_planes = av_pix_fmt_count_planes(inlink->format);
115
116
    s->img_out_f = av_calloc(inlink->w * inlink->h, sizeof(float));
117
    s->img_temp = av_calloc(inlink->w * inlink->h, sizeof(float));
118
    s->map_factor_a = av_calloc(inlink->w * inlink->h, sizeof(float));
119
    s->map_factor_b = av_calloc(inlink->w * inlink->h, sizeof(float));
120
    s->slice_factor_a = av_calloc(inlink->w, sizeof(float));
121
    s->slice_factor_b = av_calloc(inlink->w, sizeof(float));
122
    s->line_factor_a = av_calloc(inlink->w, sizeof(float));
123
    s->line_factor_b = av_calloc(inlink->w, sizeof(float));
124
125
    if (!s->img_out_f ||
126
        !s->img_temp ||
127
        !s->map_factor_a ||
128
        !s->map_factor_b ||
129
        !s->slice_factor_a ||
130
        !s->slice_factor_a ||
131
        !s->line_factor_a ||
132
        !s->line_factor_a)
133
        return AVERROR(ENOMEM);
134
135
    return 0;
136
}
137
138
#define BILATERAL(type, name)                                                           \
139
static void bilateral_##name(BilateralContext *s, const uint8_t *ssrc, uint8_t *ddst,   \
140
                             float sigma_spatial, float sigma_range,                    \
141
                             int width, int height, int src_linesize, int dst_linesize) \
142
{                                                                                       \
143
    type *dst = (type *)ddst;                                                           \
144
    const type *src = (const type *)ssrc;                                               \
145
    float *img_out_f = s->img_out_f, *img_temp = s->img_temp;                           \
146
    float *map_factor_a = s->map_factor_a, *map_factor_b = s->map_factor_b;             \
147
    float *slice_factor_a = s->slice_factor_a, *slice_factor_b = s->slice_factor_b;     \
148
    float *line_factor_a = s->line_factor_a, *line_factor_b = s->line_factor_b;         \
149
    const float *range_table = s->range_table;                                          \
150
    const float alpha = s->alpha;                                                       \
151
    float ypr, ycr, *ycy, *ypy, *xcy, fp, fc;                                           \
152
    const float inv_alpha_ = 1.f - alpha;                                               \
153
    float *ycf, *ypf, *xcf, *in_factor;                                                 \
154
    const type *tcy, *tpy;                                                                \
155
    int h1;                                                                               \
156
                                                                                          \
157
    for (int y = 0; y < height; y++) {                                                    \
158
        float *temp_factor_x, *temp_x = &img_temp[y * width];                             \
159
        const type *in_x = &src[y * src_linesize];                                        \
160
        const type *texture_x = &src[y * src_linesize];                                   \
161
        type tpr;                                                                         \
162
                                                                                          \
163
        *temp_x++ = ypr = *in_x++;                                                        \
164
        tpr = *texture_x++;                                                               \
165
                                                                                          \
166
        temp_factor_x = &map_factor_a[y * width];                                         \
167
        *temp_factor_x++ = fp = 1;                                                        \
168
                                                                                          \
169
        for (int x = 1; x < width; x++) {                                                 \
170
            float alpha_;                                                                 \
171
            int range_dist;                                                               \
172
            type tcr = *texture_x++;                                                      \
173
            type dr = abs(tcr - tpr);                                                     \
174
                                                                                          \
175
            range_dist = dr;                                                              \
176
            alpha_ = range_table[range_dist];                                             \
177
            *temp_x++ = ycr = inv_alpha_*(*in_x++) + alpha_*ypr;                          \
178
            tpr = tcr;                                                                    \
179
            ypr = ycr;                                                                    \
180
            *temp_factor_x++ = fc = inv_alpha_ + alpha_ * fp;                             \
181
            fp = fc;                                                                      \
182
        }                                                                                 \
183
        --temp_x; *temp_x = 0.5f*((*temp_x) + (*--in_x));                                 \
184
        tpr = *--texture_x;                                                               \
185
        ypr = *in_x;                                                                      \
186
                                                                                          \
187
        --temp_factor_x; *temp_factor_x = 0.5f*((*temp_factor_x) + 1);                    \
188
        fp = 1;                                                                           \
189
                                                                                          \
190
        for (int x = width - 2; x >= 0; x--) {                                            \
191
            type tcr = *--texture_x;                                                      \
192
            type dr = abs(tcr - tpr);                                                     \
193
            int range_dist = dr;                                                          \
194
            float alpha_ = range_table[range_dist];                                       \
195
                                                                                          \
196
            ycr = inv_alpha_ * (*--in_x) + alpha_ * ypr;                                  \
197
            --temp_x; *temp_x = 0.5f*((*temp_x) + ycr);                                   \
198
            tpr = tcr;                                                                    \
199
            ypr = ycr;                                                                    \
200
                                                                                          \
201
            fc = inv_alpha_ + alpha_*fp;                                                  \
202
            --temp_factor_x;                                                              \
203
            *temp_factor_x = 0.5f*((*temp_factor_x) + fc);                                \
204
            fp = fc;                                                                      \
205
        }                                                                                 \
206
    }                                                                                     \
207
    memcpy(img_out_f, img_temp, sizeof(float) * width);                                   \
208
                                                                                          \
209
    in_factor = map_factor_a;                                                             \
210
    memcpy(map_factor_b, in_factor, sizeof(float) * width);                               \
211
    for (int y = 1; y < height; y++) {                                                    \
212
        tpy = &src[(y - 1) * src_linesize];                                               \
213
        tcy = &src[y * src_linesize];                                                     \
214
        xcy = &img_temp[y * width];                                                       \
215
        ypy = &img_out_f[(y - 1) * width];                                                \
216
        ycy = &img_out_f[y * width];                                                      \
217
                                                                                          \
218
        xcf = &in_factor[y * width];                                                      \
219
        ypf = &map_factor_b[(y - 1) * width];                                             \
220
        ycf = &map_factor_b[y * width];                                                   \
221
        for (int x = 0; x < width; x++) {                                                 \
222
            type dr = abs((*tcy++) - (*tpy++));                                           \
223
            int range_dist = dr;                                                          \
224
            float alpha_ = range_table[range_dist];                                       \
225
                                                                                          \
226
            *ycy++ = inv_alpha_*(*xcy++) + alpha_*(*ypy++);                               \
227
            *ycf++ = inv_alpha_*(*xcf++) + alpha_*(*ypf++);                               \
228
        }                                                                                 \
229
    }                                                                                     \
230
    h1 = height - 1;                                                                      \
231
    ycf = line_factor_a;                                                                  \
232
    ypf = line_factor_b;                                                                  \
233
    memcpy(ypf, &in_factor[h1 * width], sizeof(float) * width);                           \
234
    for (int x = 0; x < width; x++)                                                       \
235
        map_factor_b[h1 * width + x] = 0.5f*(map_factor_b[h1 * width + x] + ypf[x]);      \
236
                                                                                          \
237
    ycy = slice_factor_a;                                                                 \
238
    ypy = slice_factor_b;                                                                 \
239
    memcpy(ypy, &img_temp[h1 * width], sizeof(float) * width);                            \
240
    for (int x = 0, k = 0; x < width; x++) {                                              \
241
        int idx = h1 * width + x;                                                         \
242
        img_out_f[idx] = 0.5f*(img_out_f[idx] + ypy[k++]) / map_factor_b[h1 * width + x]; \
243
    }                                                                                     \
244
                                                                                          \
245
    for (int y = h1 - 1; y >= 0; y--) {                                                   \
246
        float *ycf_, *ypf_, *factor_;                                                     \
247
        float *ycy_, *ypy_, *out_;                                                        \
248
                                                                                          \
249
        tpy = &src[(y + 1) * src_linesize];                                               \
250
        tcy = &src[y * src_linesize];                                                     \
251
        xcy = &img_temp[y * width];                                                       \
252
        ycy_ = ycy;                                                                       \
253
        ypy_ = ypy;                                                                       \
254
        out_ = &img_out_f[y * width];                                                     \
255
                                                                                          \
256
        xcf = &in_factor[y * width];                                                      \
257
        ycf_ = ycf;                                                                       \
258
        ypf_ = ypf;                                                                       \
259
        factor_ = &map_factor_b[y * width];                                               \
260
        for (int x = 0; x < width; x++) {                                                 \
261
            type dr = abs((*tcy++) - (*tpy++));                                           \
262
            int range_dist = dr;                                                          \
263
            float alpha_ = range_table[range_dist];                                       \
264
            float ycc, fcc = inv_alpha_*(*xcf++) + alpha_*(*ypf_++);                      \
265
                                                                                          \
266
            *ycf_++ = fcc;                                                                \
267
            *factor_ = 0.5f * (*factor_ + fcc);                                           \
268
                                                                                          \
269
            ycc = inv_alpha_*(*xcy++) + alpha_*(*ypy_++);                                 \
270
            *ycy_++ = ycc;                                                                \
271
            *out_ = 0.5f * (*out_ + ycc) / (*factor_);                                    \
272
            out_++;                                                                       \
273
            factor_++;                                                                    \
274
        }                                                                                 \
275
                                                                                          \
276
        ypy = ycy;                                                                        \
277
        ypf = ycf;                                                                        \
278
    }                                                                                     \
279
                                                                                          \
280
    for (int i = 0; i < height; i++)                                                      \
281
        for (int j = 0; j < width; j++)                                                   \
282
            dst[j + i * dst_linesize] = img_out_f[i * width + j];                         \
283
}
284
285
BILATERAL(uint8_t, byte)
286
BILATERAL(uint16_t, word)
287
288
static int filter_frame(AVFilterLink *inlink, AVFrame *in)
289
{
290
    AVFilterContext *ctx = inlink->dst;
291
    BilateralContext *s = ctx->priv;
292
    AVFilterLink *outlink = ctx->outputs[0];
293
    AVFrame *out;
294
295
    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
296
    if (!out) {
297
        av_frame_free(&in);
298
        return AVERROR(ENOMEM);
299
    }
300
    av_frame_copy_props(out, in);
301
302
    for (int plane = 0; plane < s->nb_planes; plane++) {
303
        if (!(s->planes & (1 << plane))) {
304
            av_image_copy_plane(out->data[plane], out->linesize[plane],
305
                                in->data[plane], in->linesize[plane],
306
                                s->planewidth[plane] * ((s->depth + 7) / 8), s->planeheight[plane]);
307
            continue;
308
        }
309
310
        if (s->depth <= 8)
311
           bilateral_byte(s, in->data[plane], out->data[plane], s->sigmaS, s->sigmaR,
312
                      s->planewidth[plane], s->planeheight[plane],
313
                      in->linesize[plane], out->linesize[plane]);
314
        else
315
           bilateral_word(s, in->data[plane], out->data[plane], s->sigmaS, s->sigmaR,
316
                      s->planewidth[plane], s->planeheight[plane],
317
                      in->linesize[plane] / 2, out->linesize[plane] / 2);
318
    }
319
320
    av_frame_free(&in);
321
    return ff_filter_frame(outlink, out);
322
}
323
324
static av_cold void uninit(AVFilterContext *ctx)
325
{
326
    BilateralContext *s = ctx->priv;
327
328
    av_freep(&s->img_out_f);
329
    av_freep(&s->img_temp);
330
    av_freep(&s->map_factor_a);
331
    av_freep(&s->map_factor_b);
332
    av_freep(&s->slice_factor_a);
333
    av_freep(&s->slice_factor_b);
334
    av_freep(&s->line_factor_a);
335
    av_freep(&s->line_factor_b);
336
}
337
338
static const AVFilterPad bilateral_inputs[] = {
339
    {
340
        .name         = "default",
341
        .type         = AVMEDIA_TYPE_VIDEO,
342
        .config_props = config_input,
343
        .filter_frame = filter_frame,
344
    },
345
    { NULL }
346
};
347
348
static const AVFilterPad bilateral_outputs[] = {
349
    {
350
        .name = "default",
351
        .type = AVMEDIA_TYPE_VIDEO,
352
    },
353
    { NULL }
354
};
355
356
AVFilter ff_vf_bilateral = {
357
    .name          = "bilateral",
358
    .description   = NULL_IF_CONFIG_SMALL("Apply Bilateral filter."),
359
    .priv_size     = sizeof(BilateralContext),
360
    .priv_class    = &bilateral_class,
361
    .uninit        = uninit,
362
    .query_formats = query_formats,
363
    .inputs        = bilateral_inputs,
364
    .outputs       = bilateral_outputs,
365
    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
366
};