Browse code

avfilter: add deband filter

Signed-off-by: Paul B Mahol <onemda@gmail.com>

Paul B Mahol authored on 2015/07/11 01:16:53
Showing 6 changed files
... ...
@@ -20,6 +20,7 @@ version <next>:
20 20
 - Dynamic Audio Normalizer as dynaudnorm filter
21 21
 - Reverse filter
22 22
 - Random filter
23
+- deband filter
23 24
 
24 25
 
25 26
 version 2.7:
... ...
@@ -3878,6 +3878,43 @@ Violent denoise using a block size of @code{16x16}:
3878 3878
 dctdnoiz=15:n=4
3879 3879
 @end example
3880 3880
 
3881
+@section deband
3882
+
3883
+Remove banding artifacts from input video.
3884
+It works by replacing banded pixels with average value of referenced pixels.
3885
+
3886
+The filter accepts the following options:
3887
+
3888
+@table @option
3889
+@item 1thr
3890
+@item 2thr
3891
+@item 3thr
3892
+@item 4thr
3893
+Set banding detection threshold for each plane. Default is 0.02.
3894
+Valid range is 0.00003 to 0.5.
3895
+If difference between current pixel and reference pixel is less than threshold,
3896
+it will be considered as banded.
3897
+
3898
+@item range, r
3899
+Banding detection range in pixels. Default is 16. If positive, random number
3900
+in range 0 to set value will be used. If negative, exact absolute value
3901
+will be used.
3902
+The range defines square of four pixels around current pixel.
3903
+
3904
+@item direction, d
3905
+Set direction in radians from which four pixel will be compared. If positive,
3906
+random direction from 0 to set direction will be picked. If negative, exact of
3907
+absolute value will be picked. For example direction 0, -PI or -2*PI radians
3908
+will pick only pixels on same row and -PI/2 will pick only pixels on same
3909
+column.
3910
+
3911
+@item blur
3912
+If enabled, current pixel is compared with average value of all four
3913
+surrounding pixels. The default is enabled. If disabled current pixel is
3914
+compared with all four surrounding pixels. The pixel is considered banded
3915
+if only all four differences with surrounding pixels are less than threshold.
3916
+@end table
3917
+
3881 3918
 @anchor{decimate}
3882 3919
 @section decimate
3883 3920
 
... ...
@@ -112,6 +112,7 @@ OBJS-$(CONFIG_CROP_FILTER)                   += vf_crop.o
112 112
 OBJS-$(CONFIG_CROPDETECT_FILTER)             += vf_cropdetect.o
113 113
 OBJS-$(CONFIG_CURVES_FILTER)                 += vf_curves.o
114 114
 OBJS-$(CONFIG_DCTDNOIZ_FILTER)               += vf_dctdnoiz.o
115
+OBJS-$(CONFIG_DEBAND_FILTER)                 += vf_deband.o
115 116
 OBJS-$(CONFIG_DECIMATE_FILTER)               += vf_decimate.o
116 117
 OBJS-$(CONFIG_DEFLATE_FILTER)                += vf_neighbor.o
117 118
 OBJS-$(CONFIG_DEJUDDER_FILTER)               += vf_dejudder.o
... ...
@@ -128,6 +128,7 @@ void avfilter_register_all(void)
128 128
     REGISTER_FILTER(CROPDETECT,     cropdetect,     vf);
129 129
     REGISTER_FILTER(CURVES,         curves,         vf);
130 130
     REGISTER_FILTER(DCTDNOIZ,       dctdnoiz,       vf);
131
+    REGISTER_FILTER(DEBAND,         deband,         vf);
131 132
     REGISTER_FILTER(DECIMATE,       decimate,       vf);
132 133
     REGISTER_FILTER(DEFLATE,        deflate,        vf);
133 134
     REGISTER_FILTER(DEJUDDER,       dejudder,       vf);
... ...
@@ -30,7 +30,7 @@
30 30
 #include "libavutil/version.h"
31 31
 
32 32
 #define LIBAVFILTER_VERSION_MAJOR  5
33
-#define LIBAVFILTER_VERSION_MINOR  25
33
+#define LIBAVFILTER_VERSION_MINOR  27
34 34
 #define LIBAVFILTER_VERSION_MICRO 100
35 35
 
36 36
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
37 37
new file mode 100644
... ...
@@ -0,0 +1,312 @@
0
+/*
1
+ * Copyright (c) 2015 Niklas Haas
2
+ * Copyright (c) 2015 Paul B Mahol
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+#include "libavutil/opt.h"
24
+#include "libavutil/pixdesc.h"
25
+#include "avfilter.h"
26
+#include "internal.h"
27
+#include "video.h"
28
+
29
+typedef struct DebandContext {
30
+    const AVClass *class;
31
+
32
+    float threshold[4];
33
+    int range;
34
+    int blur;
35
+    float direction;
36
+
37
+    int nb_components;
38
+    int planewidth[4];
39
+    int planeheight[4];
40
+    int thr[4];
41
+
42
+    int *x_pos;
43
+    int *y_pos;
44
+
45
+    int (*deband)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs);
46
+} DebandContext;
47
+
48
+#define OFFSET(x) offsetof(DebandContext, x)
49
+#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
50
+
51
+static const AVOption deband_options[] = {
52
+    { "1thr",      "set 1st plane threshold", OFFSET(threshold[0]), AV_OPT_TYPE_FLOAT, {.dbl=0.02},  0.00003,     0.5, FLAGS },
53
+    { "2thr",      "set 2nd plane threshold", OFFSET(threshold[1]), AV_OPT_TYPE_FLOAT, {.dbl=0.02},  0.00003,     0.5, FLAGS },
54
+    { "3thr",      "set 3rd plane threshold", OFFSET(threshold[2]), AV_OPT_TYPE_FLOAT, {.dbl=0.02},  0.00003,     0.5, FLAGS },
55
+    { "4thr",      "set 4th plane threshold", OFFSET(threshold[3]), AV_OPT_TYPE_FLOAT, {.dbl=0.02},  0.00003,     0.5, FLAGS },
56
+    { "range",     "set range",               OFFSET(range),        AV_OPT_TYPE_INT,   {.i64=16},    INT_MIN, INT_MAX, FLAGS },
57
+    { "r",         "set range",               OFFSET(range),        AV_OPT_TYPE_INT,   {.i64=16},    INT_MIN, INT_MAX, FLAGS },
58
+    { "direction", "set direction",           OFFSET(direction),    AV_OPT_TYPE_FLOAT, {.dbl=2*M_PI},-2*M_PI,  2*M_PI, FLAGS },
59
+    { "d",         "set direction",           OFFSET(direction),    AV_OPT_TYPE_FLOAT, {.dbl=2*M_PI},-2*M_PI,  2*M_PI, FLAGS },
60
+    { "blur",      "enable blur",             OFFSET(blur),         AV_OPT_TYPE_INT,   {.i64=1},           0,       1, FLAGS },
61
+    { NULL }
62
+};
63
+
64
+AVFILTER_DEFINE_CLASS(deband);
65
+
66
+static int query_formats(AVFilterContext *ctx)
67
+{
68
+    static const enum AVPixelFormat pix_fmts[] = {
69
+        AV_PIX_FMT_GRAY8, AV_PIX_FMT_GRAY16,
70
+        AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P,
71
+        AV_PIX_FMT_YUV411P,  AV_PIX_FMT_YUV410P,  AV_PIX_FMT_YUV440P,
72
+        AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P,
73
+        AV_PIX_FMT_YUVJ411P, AV_PIX_FMT_YUVJ440P,
74
+        AV_PIX_FMT_YUVA420P, AV_PIX_FMT_YUVA422P, AV_PIX_FMT_YUVA444P,
75
+        AV_PIX_FMT_YUV420P9, AV_PIX_FMT_YUV422P9, AV_PIX_FMT_YUV444P9,
76
+        AV_PIX_FMT_YUVA420P9, AV_PIX_FMT_YUVA422P9, AV_PIX_FMT_YUVA444P9,
77
+        AV_PIX_FMT_YUVA420P10, AV_PIX_FMT_YUVA422P10, AV_PIX_FMT_YUVA444P10,
78
+        AV_PIX_FMT_YUV420P12, AV_PIX_FMT_YUV422P12, AV_PIX_FMT_YUV444P12,
79
+        AV_PIX_FMT_YUV420P14, AV_PIX_FMT_YUV422P14, AV_PIX_FMT_YUV444P14,
80
+        AV_PIX_FMT_GBRP, AV_PIX_FMT_GBRAP,
81
+        AV_PIX_FMT_GBRP9, AV_PIX_FMT_GBRP10,
82
+        AV_PIX_FMT_GBRP12, AV_PIX_FMT_GBRP14,
83
+        AV_PIX_FMT_GBRP16, AV_PIX_FMT_GBRAP16,
84
+        AV_PIX_FMT_YUV420P16, AV_PIX_FMT_YUV422P16, AV_PIX_FMT_YUV444P16,
85
+        AV_PIX_FMT_YUVA420P16, AV_PIX_FMT_YUVA422P16, AV_PIX_FMT_YUVA444P16,
86
+        AV_PIX_FMT_NONE
87
+    };
88
+    AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
89
+    if (!fmts_list)
90
+        return AVERROR(ENOMEM);
91
+
92
+    return ff_set_common_formats(ctx, fmts_list);
93
+}
94
+
95
+static float frand(int x, int y)
96
+{
97
+    const float r = sinf(x * 12.9898 + y * 78.233) * 43758.545;
98
+
99
+    return r - floorf(r);
100
+}
101
+
102
+static int inline get_avg(int ref0, int ref1, int ref2, int ref3)
103
+{
104
+    return (ref0 + ref1 + ref2 + ref3) / 4;
105
+}
106
+
107
+typedef struct ThreadData {
108
+    AVFrame *in, *out;
109
+} ThreadData;
110
+
111
+static int deband_8_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
112
+{
113
+    DebandContext *s = ctx->priv;
114
+    ThreadData *td = arg;
115
+    AVFrame *in = td->in;
116
+    AVFrame *out = td->out;
117
+    int x, y, p;
118
+
119
+    for (p = 0; p < s->nb_components; p++) {
120
+        const uint8_t *src_ptr = (const uint8_t *)in->data[p];
121
+        uint8_t *dst_ptr = (uint8_t *)out->data[p];
122
+        const int dst_linesize = out->linesize[p];
123
+        const int src_linesize = in->linesize[p];
124
+        const int thr = s->thr[p];
125
+        const int start = (s->planeheight[p] *  jobnr   ) / nb_jobs;
126
+        const int end   = (s->planeheight[p] * (jobnr+1)) / nb_jobs;
127
+        const int w = s->planewidth[p] - 1;
128
+        const int h = s->planeheight[p] - 1;
129
+
130
+        for (y = start; y < end; y++) {
131
+            const int pos = y * s->planeheight[0];
132
+
133
+            for (x = 0; x < s->planewidth[p]; x++) {
134
+                const int x_pos = s->x_pos[pos + x];
135
+                const int y_pos = s->y_pos[pos + x];
136
+                const int ref0 = src_ptr[av_clip(y +  y_pos, 0, h) * src_linesize + av_clip(x +  x_pos, 0, w)];
137
+                const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x +  x_pos, 0, w)];
138
+                const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
139
+                const int ref3 = src_ptr[av_clip(y +  y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
140
+                const int src0 = src_ptr[y * src_linesize + x];
141
+
142
+                if (s->blur) {
143
+                    const int avg = get_avg(ref0, ref1, ref2, ref3);
144
+                    const int diff = FFABS(src0 - avg);
145
+
146
+                    dst_ptr[y * dst_linesize + x] = diff < thr ? avg : src0;
147
+                } else {
148
+                    dst_ptr[y * dst_linesize + x] = (FFABS(src0 - ref0) < thr) &&
149
+                                                    (FFABS(src0 - ref1) < thr) &&
150
+                                                    (FFABS(src0 - ref2) < thr) &&
151
+                                                    (FFABS(src0 - ref3) < thr) ? get_avg(ref0, ref1, ref2, ref3) : src0;
152
+                }
153
+            }
154
+        }
155
+    }
156
+
157
+    return 0;
158
+}
159
+
160
+static int deband_16_c(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
161
+{
162
+    DebandContext *s = ctx->priv;
163
+    ThreadData *td = arg;
164
+    AVFrame *in = td->in;
165
+    AVFrame *out = td->out;
166
+    int x, y, p;
167
+
168
+    for (p = 0; p < s->nb_components; p++) {
169
+        const uint16_t *src_ptr = (const uint16_t *)in->data[p];
170
+        uint16_t *dst_ptr = (uint16_t *)out->data[p];
171
+        const int dst_linesize = out->linesize[p] / 2;
172
+        const int src_linesize = in->linesize[p] / 2;
173
+        const int thr = s->thr[p];
174
+        const int start = (s->planeheight[p] *  jobnr   ) / nb_jobs;
175
+        const int end   = (s->planeheight[p] * (jobnr+1)) / nb_jobs;
176
+        const int w = s->planewidth[p] - 1;
177
+        const int h = s->planeheight[p] - 1;
178
+
179
+        for (y = start; y < end; y++) {
180
+            const int pos = y * s->planeheight[0];
181
+
182
+            for (x = 0; x < s->planewidth[p]; x++) {
183
+                const int x_pos = s->x_pos[pos + x];
184
+                const int y_pos = s->y_pos[pos + x];
185
+                const int ref0 = src_ptr[av_clip(y +  y_pos, 0, h) * src_linesize + av_clip(x +  x_pos, 0, w)];
186
+                const int ref1 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x +  x_pos, 0, w)];
187
+                const int ref2 = src_ptr[av_clip(y + -y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
188
+                const int ref3 = src_ptr[av_clip(y +  y_pos, 0, h) * src_linesize + av_clip(x + -x_pos, 0, w)];
189
+                const int src0 = src_ptr[y * src_linesize + x];
190
+
191
+                if (s->blur) {
192
+                    const int avg = get_avg(ref0, ref1, ref2, ref3);
193
+                    const int diff = FFABS(src0 - avg);
194
+
195
+                    dst_ptr[y * dst_linesize + x] = diff < thr ? avg : src0;
196
+                } else {
197
+                    dst_ptr[y * dst_linesize + x] = (FFABS(src0 - ref0) < thr) &&
198
+                                                    (FFABS(src0 - ref1) < thr) &&
199
+                                                    (FFABS(src0 - ref2) < thr) &&
200
+                                                    (FFABS(src0 - ref3) < thr) ? get_avg(ref0, ref1, ref2, ref3) : src0;
201
+                }
202
+            }
203
+        }
204
+    }
205
+
206
+    return 0;
207
+}
208
+
209
+static int config_input(AVFilterLink *inlink)
210
+{
211
+    const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(inlink->format);
212
+    AVFilterContext *ctx = inlink->dst;
213
+    DebandContext *s = ctx->priv;
214
+    const float direction = s->direction;
215
+    const int range = s->range;
216
+    int x, y;
217
+
218
+    s->nb_components = desc->nb_components;
219
+
220
+    s->planeheight[1] = s->planeheight[2] = FF_CEIL_RSHIFT(inlink->h, desc->log2_chroma_h);
221
+    s->planeheight[0] = s->planeheight[3] = inlink->h;
222
+    s->planewidth[1]  = s->planewidth[2]  = FF_CEIL_RSHIFT(inlink->w, desc->log2_chroma_w);
223
+    s->planewidth[0]  = s->planewidth[3]  = inlink->w;
224
+
225
+    s->deband = desc->comp[0].depth_minus1 > 7 ? deband_16_c : deband_8_c;
226
+
227
+    s->thr[0] = ((1 << (desc->comp[0].depth_minus1 + 1)) - 1) * s->threshold[0];
228
+    s->thr[1] = ((1 << (desc->comp[1].depth_minus1 + 1)) - 1) * s->threshold[1];
229
+    s->thr[2] = ((1 << (desc->comp[2].depth_minus1 + 1)) - 1) * s->threshold[2];
230
+    s->thr[3] = ((1 << (desc->comp[3].depth_minus1 + 1)) - 1) * s->threshold[3];
231
+
232
+    s->x_pos = av_malloc(s->planewidth[0] * s->planeheight[0] * sizeof(*s->x_pos));
233
+    s->y_pos = av_malloc(s->planewidth[0] * s->planeheight[0] * sizeof(*s->y_pos));
234
+    if (!s->x_pos || !s->y_pos)
235
+        return AVERROR(ENOMEM);
236
+
237
+    for (y = 0; y < s->planeheight[0]; y++) {
238
+        for (x = 0; x < s->planewidth[0]; x++) {
239
+            const float r = frand(x, y);
240
+            const float dir = direction < 0 ? -direction : r * direction;
241
+            const int dist = range < 0 ? -range : r * range;
242
+
243
+            s->x_pos[y * s->planeheight[0] + x] = cosf(dir) * dist;
244
+            s->y_pos[y * s->planeheight[0] + x] = sinf(dir) * dist;
245
+        }
246
+    }
247
+
248
+    return 0;
249
+}
250
+
251
+static int filter_frame(AVFilterLink *inlink, AVFrame *in)
252
+{
253
+    AVFilterContext *ctx = inlink->dst;
254
+    AVFilterLink *outlink = ctx->outputs[0];
255
+    DebandContext *s = ctx->priv;
256
+    AVFrame *out;
257
+    ThreadData td;
258
+
259
+    out = ff_get_video_buffer(outlink, outlink->w, outlink->h);
260
+    if (!out) {
261
+        av_frame_free(&in);
262
+        return AVERROR(ENOMEM);
263
+    }
264
+    av_frame_copy_props(out, in);
265
+
266
+    td.in = in; td.out = out;
267
+    ctx->internal->execute(ctx, s->deband, &td, NULL, FFMIN3(s->planeheight[1],
268
+                                                             s->planeheight[2],
269
+                                                             ctx->graph->nb_threads));
270
+
271
+    av_frame_free(&in);
272
+    return ff_filter_frame(outlink, out);
273
+}
274
+
275
+static av_cold void uninit(AVFilterContext *ctx)
276
+{
277
+    DebandContext *s = ctx->priv;
278
+
279
+    av_freep(&s->x_pos);
280
+    av_freep(&s->y_pos);
281
+}
282
+
283
+static const AVFilterPad avfilter_vf_deband_inputs[] = {
284
+    {
285
+        .name         = "default",
286
+        .type         = AVMEDIA_TYPE_VIDEO,
287
+        .config_props = config_input,
288
+        .filter_frame = filter_frame,
289
+    },
290
+    { NULL }
291
+};
292
+
293
+static const AVFilterPad avfilter_vf_deband_outputs[] = {
294
+    {
295
+        .name = "default",
296
+        .type = AVMEDIA_TYPE_VIDEO,
297
+    },
298
+    { NULL }
299
+};
300
+
301
+AVFilter ff_vf_deband = {
302
+    .name          = "deband",
303
+    .description   = NULL_IF_CONFIG_SMALL("Debands video."),
304
+    .priv_size     = sizeof(DebandContext),
305
+    .priv_class    = &deband_class,
306
+    .uninit        = uninit,
307
+    .query_formats = query_formats,
308
+    .inputs        = avfilter_vf_deband_inputs,
309
+    .outputs       = avfilter_vf_deband_outputs,
310
+    .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
311
+};