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