Browse code

Port libmpcodecs hqdn3d filter.

Patch by Baptiste with some fixes by me.

Originally committed as revision 25880 to svn://svn.ffmpeg.org/ffmpeg/trunk

Baptiste Coudurier authored on 2010/12/04 22:03:42
Showing 7 changed files
... ...
@@ -61,6 +61,7 @@ version <next>:
61 61
 - IEC 61937 demuxer
62 62
 - Mobotix .mxg demuxer
63 63
 - frei0r source added
64
+- hqdn3d filter added
64 65
 
65 66
 
66 67
 version 0.6:
... ...
@@ -1407,6 +1407,7 @@ blackframe_filter_deps="gpl"
1407 1407
 cropdetect_filter_deps="gpl"
1408 1408
 frei0r_filter_deps="frei0r dlopen strtok_r"
1409 1409
 frei0r_src_filter_deps="frei0r dlopen strtok_r"
1410
+hqdn3d_filter_deps="gpl"
1410 1411
 ocv_smooth_filter_deps="libopencv"
1411 1412
 yadif_filter_deps="gpl"
1412 1413
 
... ...
@@ -344,6 +344,33 @@ For example to horizontally flip the video in input with
344 344
 ffmpeg -i in.avi -vf "hflip" out.avi
345 345
 @end example
346 346
 
347
+@section hqdn3d
348
+
349
+High precision/quality 3d denoise filter. This filter aims to reduce
350
+image noise producing smooth images and making still images really
351
+still. It should enhance compressibility.
352
+
353
+It accepts the following optional parameters:
354
+@var{luma_spatial}:@var{chroma_spatial}:@var{luma_tmp}:@var{chroma_tmp}
355
+
356
+@table @option
357
+@item luma_spatial
358
+a non-negative float number which specifies spatial luma strength,
359
+defaults to 4.0
360
+
361
+@item chroma_spatial
362
+a non-negative float number which specifies spatial chroma strength,
363
+defaults to 3.0*@var{luma_spatial}/4.0
364
+
365
+@item luma_tmp
366
+a float number which specifies luma temporal strength, defaults to
367
+6.0*@var{luma_spatial}/4.0
368
+
369
+@item chroma_tmp
370
+a float number which specifies chroma temporal strength, defaults to
371
+@var{luma_tmp}*@var{chroma_spatial}/@var{luma_spatial}
372
+@end table
373
+
347 374
 @section noformat
348 375
 
349 376
 Force libavfilter not to use any of the specified pixel formats for the
... ...
@@ -27,6 +27,7 @@ OBJS-$(CONFIG_FIFO_FILTER)                   += vf_fifo.o
27 27
 OBJS-$(CONFIG_FORMAT_FILTER)                 += vf_format.o
28 28
 OBJS-$(CONFIG_FREI0R_FILTER)                 += vf_frei0r.o
29 29
 OBJS-$(CONFIG_HFLIP_FILTER)                  += vf_hflip.o
30
+OBJS-$(CONFIG_HQDN3D_FILTER)                 += vf_hqdn3d.o
30 31
 OBJS-$(CONFIG_NOFORMAT_FILTER)               += vf_format.o
31 32
 OBJS-$(CONFIG_NULL_FILTER)                   += vf_null.o
32 33
 OBJS-$(CONFIG_OCV_SMOOTH_FILTER)             += vf_libopencv.o
... ...
@@ -48,6 +48,7 @@ void avfilter_register_all(void)
48 48
     REGISTER_FILTER (FORMAT,      format,      vf);
49 49
     REGISTER_FILTER (FREI0R,      frei0r,      vf);
50 50
     REGISTER_FILTER (HFLIP,       hflip,       vf);
51
+    REGISTER_FILTER (HQDN3D,      hqdn3d,      vf);
51 52
     REGISTER_FILTER (NOFORMAT,    noformat,    vf);
52 53
     REGISTER_FILTER (NULL,        null,        vf);
53 54
     REGISTER_FILTER (OCV_SMOOTH,  ocv_smooth,  vf);
... ...
@@ -25,8 +25,8 @@
25 25
 #include "libavutil/avutil.h"
26 26
 
27 27
 #define LIBAVFILTER_VERSION_MAJOR  1
28
-#define LIBAVFILTER_VERSION_MINOR 67
29
-#define LIBAVFILTER_VERSION_MICRO  1
28
+#define LIBAVFILTER_VERSION_MINOR 68
29
+#define LIBAVFILTER_VERSION_MICRO  0
30 30
 
31 31
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
32 32
                                                LIBAVFILTER_VERSION_MINOR, \
33 33
new file mode 100644
... ...
@@ -0,0 +1,345 @@
0
+/*
1
+ * Copyright (c) 2003 Daniel Moreno <comac AT comac DOT darktech DOT org>
2
+ * Copyright (c) 2010 Baptiste Coudurier
3
+ *
4
+ * This file is part of FFmpeg, ported from MPlayer.
5
+ *
6
+ * FFmpeg is free software; you can redistribute it and/or modify
7
+ * it under the terms of the GNU General Public License as published by
8
+ * the Free Software Foundation; either version 2 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * FFmpeg is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU General Public License along
17
+ * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * high quality 3d video denoiser, ported from MPlayer
24
+ * libmpcodecs/vf_hqdn3d.c.
25
+ */
26
+
27
+#include "libavutil/pixdesc.h"
28
+#include "avfilter.h"
29
+
30
+typedef struct {
31
+    int Coefs[4][512*16];
32
+    unsigned int *Line;
33
+    unsigned short *Frame[3];
34
+    int hsub, vsub;
35
+} HQDN3DContext;
36
+
37
+static inline unsigned int LowPassMul(unsigned int PrevMul, unsigned int CurrMul, int *Coef)
38
+{
39
+    //    int dMul= (PrevMul&0xFFFFFF)-(CurrMul&0xFFFFFF);
40
+    int dMul= PrevMul-CurrMul;
41
+    unsigned int d=((dMul+0x10007FF)>>12);
42
+    return CurrMul + Coef[d];
43
+}
44
+
45
+static void deNoiseTemporal(unsigned char *FrameSrc,
46
+                            unsigned char *FrameDest,
47
+                            unsigned short *FrameAnt,
48
+                            int W, int H, int sStride, int dStride,
49
+                            int *Temporal)
50
+{
51
+    long X, Y;
52
+    unsigned int PixelDst;
53
+
54
+    for (Y = 0; Y < H; Y++) {
55
+        for (X = 0; X < W; X++) {
56
+            PixelDst = LowPassMul(FrameAnt[X]<<8, FrameSrc[X]<<16, Temporal);
57
+            FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
58
+            FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
59
+        }
60
+        FrameSrc  += sStride;
61
+        FrameDest += dStride;
62
+        FrameAnt += W;
63
+    }
64
+}
65
+
66
+static void deNoiseSpacial(unsigned char *Frame,
67
+                           unsigned char *FrameDest,
68
+                           unsigned int *LineAnt,
69
+                           int W, int H, int sStride, int dStride,
70
+                           int *Horizontal, int *Vertical)
71
+{
72
+    long X, Y;
73
+    long sLineOffs = 0, dLineOffs = 0;
74
+    unsigned int PixelAnt;
75
+    unsigned int PixelDst;
76
+
77
+    /* First pixel has no left nor top neighbor. */
78
+    PixelDst = LineAnt[0] = PixelAnt = Frame[0]<<16;
79
+    FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
80
+
81
+    /* First line has no top neighbor, only left. */
82
+    for (X = 1; X < W; X++) {
83
+        PixelDst = LineAnt[X] = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
84
+        FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
85
+    }
86
+
87
+    for (Y = 1; Y < H; Y++) {
88
+        unsigned int PixelAnt;
89
+        sLineOffs += sStride, dLineOffs += dStride;
90
+        /* First pixel on each line doesn't have previous pixel */
91
+        PixelAnt = Frame[sLineOffs]<<16;
92
+        PixelDst = LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
93
+        FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
94
+
95
+        for (X = 1; X < W; X++) {
96
+            unsigned int PixelDst;
97
+            /* The rest are normal */
98
+            PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
99
+            PixelDst = LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
100
+            FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
101
+        }
102
+    }
103
+}
104
+
105
+static void deNoise(unsigned char *Frame,
106
+                    unsigned char *FrameDest,
107
+                    unsigned int *LineAnt,
108
+                    unsigned short **FrameAntPtr,
109
+                    int W, int H, int sStride, int dStride,
110
+                    int *Horizontal, int *Vertical, int *Temporal)
111
+{
112
+    long X, Y;
113
+    long sLineOffs = 0, dLineOffs = 0;
114
+    unsigned int PixelAnt;
115
+    unsigned int PixelDst;
116
+    unsigned short* FrameAnt=(*FrameAntPtr);
117
+
118
+    if (!FrameAnt) {
119
+        (*FrameAntPtr) = FrameAnt = av_malloc(W*H*sizeof(unsigned short));
120
+        for (Y = 0; Y < H; Y++) {
121
+            unsigned short* dst=&FrameAnt[Y*W];
122
+            unsigned char* src=Frame+Y*sStride;
123
+            for (X = 0; X < W; X++) dst[X]=src[X]<<8;
124
+        }
125
+    }
126
+
127
+    if (!Horizontal[0] && !Vertical[0]) {
128
+        deNoiseTemporal(Frame, FrameDest, FrameAnt,
129
+                        W, H, sStride, dStride, Temporal);
130
+        return;
131
+    }
132
+    if (!Temporal[0]) {
133
+        deNoiseSpacial(Frame, FrameDest, LineAnt,
134
+                       W, H, sStride, dStride, Horizontal, Vertical);
135
+        return;
136
+    }
137
+
138
+    /* First pixel has no left nor top neighbor. Only previous frame */
139
+    LineAnt[0] = PixelAnt = Frame[0]<<16;
140
+    PixelDst = LowPassMul(FrameAnt[0]<<8, PixelAnt, Temporal);
141
+    FrameAnt[0] = ((PixelDst+0x1000007F)>>8);
142
+    FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
143
+
144
+    /* First line has no top neighbor. Only left one for each pixel and
145
+     * last frame */
146
+    for (X = 1; X < W; X++) {
147
+        LineAnt[X] = PixelAnt = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
148
+        PixelDst = LowPassMul(FrameAnt[X]<<8, PixelAnt, Temporal);
149
+        FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
150
+        FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
151
+    }
152
+
153
+    for (Y = 1; Y < H; Y++) {
154
+        unsigned int PixelAnt;
155
+        unsigned short* LinePrev=&FrameAnt[Y*W];
156
+        sLineOffs += sStride, dLineOffs += dStride;
157
+        /* First pixel on each line doesn't have previous pixel */
158
+        PixelAnt = Frame[sLineOffs]<<16;
159
+        LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
160
+        PixelDst = LowPassMul(LinePrev[0]<<8, LineAnt[0], Temporal);
161
+        LinePrev[0] = ((PixelDst+0x1000007F)>>8);
162
+        FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
163
+
164
+        for (X = 1; X < W; X++) {
165
+            unsigned int PixelDst;
166
+            /* The rest are normal */
167
+            PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
168
+            LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
169
+            PixelDst = LowPassMul(LinePrev[X]<<8, LineAnt[X], Temporal);
170
+            LinePrev[X] = ((PixelDst+0x1000007F)>>8);
171
+            FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
172
+        }
173
+    }
174
+}
175
+
176
+static void PrecalcCoefs(int *Ct, double Dist25)
177
+{
178
+    int i;
179
+    double Gamma, Simil, C;
180
+
181
+    Gamma = log(0.25) / log(1.0 - Dist25/255.0 - 0.00001);
182
+
183
+    for (i = -255*16; i <= 255*16; i++) {
184
+        Simil = 1.0 - FFABS(i) / (16*255.0);
185
+        C = pow(Simil, Gamma) * 65536.0 * i / 16.0;
186
+        Ct[16*256+i] = lrint(C);
187
+    }
188
+
189
+    Ct[0] = !!Dist25;
190
+}
191
+
192
+#define PARAM1_DEFAULT 4.0
193
+#define PARAM2_DEFAULT 3.0
194
+#define PARAM3_DEFAULT 6.0
195
+
196
+static int init(AVFilterContext *ctx, const char *args, void *opaque)
197
+{
198
+    HQDN3DContext *hqdn3d = ctx->priv;
199
+    double LumSpac, LumTmp, ChromSpac, ChromTmp;
200
+    double Param1, Param2, Param3, Param4;
201
+
202
+    LumSpac   = PARAM1_DEFAULT;
203
+    ChromSpac = PARAM2_DEFAULT;
204
+    LumTmp    = PARAM3_DEFAULT;
205
+    ChromTmp  = LumTmp * ChromSpac / LumSpac;
206
+
207
+    if (args) {
208
+        switch (sscanf(args, "%lf:%lf:%lf:%lf",
209
+                       &Param1, &Param2, &Param3, &Param4)) {
210
+        case 1:
211
+            LumSpac   = Param1;
212
+            ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT;
213
+            LumTmp    = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
214
+            ChromTmp  = LumTmp * ChromSpac / LumSpac;
215
+            break;
216
+        case 2:
217
+            LumSpac   = Param1;
218
+            ChromSpac = Param2;
219
+            LumTmp    = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
220
+            ChromTmp  = LumTmp * ChromSpac / LumSpac;
221
+            break;
222
+        case 3:
223
+            LumSpac   = Param1;
224
+            ChromSpac = Param2;
225
+            LumTmp    = Param3;
226
+            ChromTmp  = LumTmp * ChromSpac / LumSpac;
227
+            break;
228
+        case 4:
229
+            LumSpac   = Param1;
230
+            ChromSpac = Param2;
231
+            LumTmp    = Param3;
232
+            ChromTmp  = Param4;
233
+            break;
234
+        }
235
+    }
236
+
237
+    av_log(ctx, AV_LOG_INFO, "ls:%lf cs:%lf lt:%lf ct:%lf\n",
238
+           LumSpac, ChromSpac, LumTmp, ChromTmp);
239
+    if (LumSpac < 0 || ChromSpac < 0 || isnan(ChromTmp)) {
240
+        av_log(ctx, AV_LOG_ERROR,
241
+               "Invalid negative value for luma or chroma spatial strength, "
242
+               "or resulting value for chroma temporal strength is nan.\n");
243
+        return AVERROR(EINVAL);
244
+    }
245
+
246
+    PrecalcCoefs(hqdn3d->Coefs[0], LumSpac);
247
+    PrecalcCoefs(hqdn3d->Coefs[1], LumTmp);
248
+    PrecalcCoefs(hqdn3d->Coefs[2], ChromSpac);
249
+    PrecalcCoefs(hqdn3d->Coefs[3], ChromTmp);
250
+
251
+    return 0;
252
+}
253
+
254
+static void uninit(AVFilterContext *ctx)
255
+{
256
+    HQDN3DContext *hqdn3d = ctx->priv;
257
+
258
+    av_freep(&hqdn3d->Line);
259
+    av_freep(&hqdn3d->Frame[0]);
260
+    av_freep(&hqdn3d->Frame[1]);
261
+    av_freep(&hqdn3d->Frame[2]);
262
+}
263
+
264
+static int query_formats(AVFilterContext *ctx)
265
+{
266
+    static const enum PixelFormat pix_fmts[] = {
267
+        PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_NONE
268
+    };
269
+
270
+    avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
271
+
272
+    return 0;
273
+}
274
+
275
+static int config_input(AVFilterLink *inlink)
276
+{
277
+    HQDN3DContext *hqdn3d = inlink->dst->priv;
278
+
279
+    hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
280
+    hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
281
+
282
+    hqdn3d->Line = av_malloc(inlink->w * sizeof(*hqdn3d->Line));
283
+    if (!hqdn3d->Line)
284
+        return AVERROR(ENOMEM);
285
+
286
+    return 0;
287
+}
288
+
289
+static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
290
+
291
+static void end_frame(AVFilterLink *inlink)
292
+{
293
+    HQDN3DContext *hqdn3d = inlink->dst->priv;
294
+    AVFilterLink *outlink = inlink->dst->outputs[0];
295
+    AVFilterBufferRef *inpic  = inlink ->cur_buf;
296
+    AVFilterBufferRef *outpic = outlink->out_buf;
297
+    int cw = inpic->video->w >> hqdn3d->hsub;
298
+    int ch = inpic->video->h >> hqdn3d->vsub;
299
+
300
+    deNoise(inpic->data[0], outpic->data[0],
301
+            hqdn3d->Line, &hqdn3d->Frame[0], inpic->video->w, inpic->video->h,
302
+            inpic->linesize[0], outpic->linesize[0],
303
+            hqdn3d->Coefs[0],
304
+            hqdn3d->Coefs[0],
305
+            hqdn3d->Coefs[1]);
306
+    deNoise(inpic->data[1], outpic->data[1],
307
+            hqdn3d->Line, &hqdn3d->Frame[1], cw, ch,
308
+            inpic->linesize[1], outpic->linesize[1],
309
+            hqdn3d->Coefs[2],
310
+            hqdn3d->Coefs[2],
311
+            hqdn3d->Coefs[3]);
312
+    deNoise(inpic->data[2], outpic->data[2],
313
+            hqdn3d->Line, &hqdn3d->Frame[2], cw, ch,
314
+            inpic->linesize[2], outpic->linesize[2],
315
+            hqdn3d->Coefs[2],
316
+            hqdn3d->Coefs[2],
317
+            hqdn3d->Coefs[3]);
318
+
319
+    avfilter_draw_slice(outlink, 0, inpic->video->h, 1);
320
+    avfilter_end_frame(outlink);
321
+    avfilter_unref_buffer(inpic);
322
+    avfilter_unref_buffer(outpic);
323
+}
324
+
325
+AVFilter avfilter_vf_hqdn3d = {
326
+    .name          = "hqdn3d",
327
+    .description   = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
328
+
329
+    .priv_size     = sizeof(HQDN3DContext),
330
+    .init          = init,
331
+    .uninit        = uninit,
332
+    .query_formats = query_formats,
333
+
334
+    .inputs    = (AVFilterPad[]) {{ .name             = "default",
335
+                                    .type             = AVMEDIA_TYPE_VIDEO,
336
+                                    .draw_slice       = null_draw_slice,
337
+                                    .config_props     = config_input,
338
+                                    .end_frame        = end_frame },
339
+                                  { .name = NULL}},
340
+
341
+    .outputs   = (AVFilterPad[]) {{ .name             = "default",
342
+                                    .type             = AVMEDIA_TYPE_VIDEO },
343
+                                  { .name = NULL}},
344
+};