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