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