2 * Copyright (c) 2003 Daniel Moreno <comac AT comac DOT darktech DOT org>
3 * Copyright (c) 2010 Baptiste Coudurier
5 * This file is part of Libav, ported from MPlayer.
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.
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.
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.
24 * high quality 3d video denoiser, ported from MPlayer
25 * libmpcodecs/vf_hqdn3d.c.
28 #include "libavutil/pixdesc.h"
37 unsigned short *Frame[3];
41 static inline unsigned int LowPassMul(unsigned int PrevMul, unsigned int CurrMul, int *Coef)
43 // int dMul= (PrevMul&0xFFFFFF)-(CurrMul&0xFFFFFF);
44 int dMul= PrevMul-CurrMul;
45 unsigned int d=((dMul+0x10007FF)>>12);
46 return CurrMul + Coef[d];
49 static void deNoiseTemporal(unsigned char *FrameSrc,
50 unsigned char *FrameDest,
51 unsigned short *FrameAnt,
52 int W, int H, int sStride, int dStride,
56 unsigned int PixelDst;
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);
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)
77 long sLineOffs = 0, dLineOffs = 0;
78 unsigned int PixelAnt;
79 unsigned int PixelDst;
81 /* First pixel has no left nor top neighbor. */
82 PixelDst = LineAnt[0] = PixelAnt = Frame[0]<<16;
83 FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
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);
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);
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);
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)
117 long sLineOffs = 0, dLineOffs = 0;
118 unsigned int PixelAnt;
119 unsigned int PixelDst;
120 unsigned short* FrameAnt=(*FrameAntPtr);
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;
131 if (!Horizontal[0] && !Vertical[0]) {
132 deNoiseTemporal(Frame, FrameDest, FrameAnt,
133 W, H, sStride, dStride, Temporal);
137 deNoiseSpacial(Frame, FrameDest, LineAnt,
138 W, H, sStride, dStride, Horizontal, Vertical);
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);
148 /* First line has no top neighbor. Only left one for each pixel and
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);
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);
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);
180 static void PrecalcCoefs(int *Ct, double Dist25)
183 double Gamma, Simil, C;
185 Gamma = log(0.25) / log(1.0 - Dist25/255.0 - 0.00001);
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);
196 #define PARAM1_DEFAULT 4.0
197 #define PARAM2_DEFAULT 3.0
198 #define PARAM3_DEFAULT 6.0
200 static int init(AVFilterContext *ctx, const char *args)
202 HQDN3DContext *hqdn3d = ctx->priv;
203 double LumSpac, LumTmp, ChromSpac, ChromTmp;
204 double Param1, Param2, Param3, Param4;
206 LumSpac = PARAM1_DEFAULT;
207 ChromSpac = PARAM2_DEFAULT;
208 LumTmp = PARAM3_DEFAULT;
209 ChromTmp = LumTmp * ChromSpac / LumSpac;
212 switch (sscanf(args, "%lf:%lf:%lf:%lf",
213 &Param1, &Param2, &Param3, &Param4)) {
216 ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT;
217 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
218 ChromTmp = LumTmp * ChromSpac / LumSpac;
223 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
224 ChromTmp = LumTmp * ChromSpac / LumSpac;
230 ChromTmp = LumTmp * ChromSpac / LumSpac;
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);
250 PrecalcCoefs(hqdn3d->Coefs[0], LumSpac);
251 PrecalcCoefs(hqdn3d->Coefs[1], LumTmp);
252 PrecalcCoefs(hqdn3d->Coefs[2], ChromSpac);
253 PrecalcCoefs(hqdn3d->Coefs[3], ChromTmp);
258 static void uninit(AVFilterContext *ctx)
260 HQDN3DContext *hqdn3d = ctx->priv;
262 av_freep(&hqdn3d->Line);
263 av_freep(&hqdn3d->Frame[0]);
264 av_freep(&hqdn3d->Frame[1]);
265 av_freep(&hqdn3d->Frame[2]);
268 static int query_formats(AVFilterContext *ctx)
270 static const enum PixelFormat pix_fmts[] = {
271 PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_NONE
274 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
279 static int config_input(AVFilterLink *inlink)
281 HQDN3DContext *hqdn3d = inlink->dst->priv;
283 hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
284 hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
286 hqdn3d->Line = av_malloc(inlink->w * sizeof(*hqdn3d->Line));
288 return AVERROR(ENOMEM);
293 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
295 static void end_frame(AVFilterLink *inlink)
297 HQDN3DContext *hqdn3d = inlink->dst->priv;
298 AVFilterLink *outlink = inlink->dst->outputs[0];
299 AVFilterBufferRef *inpic = inlink ->cur_buf;
300 AVFilterBufferRef *outpic = outlink->out_buf;
301 int cw = inpic->video->w >> hqdn3d->hsub;
302 int ch = inpic->video->h >> hqdn3d->vsub;
304 deNoise(inpic->data[0], outpic->data[0],
305 hqdn3d->Line, &hqdn3d->Frame[0], inpic->video->w, inpic->video->h,
306 inpic->linesize[0], outpic->linesize[0],
310 deNoise(inpic->data[1], outpic->data[1],
311 hqdn3d->Line, &hqdn3d->Frame[1], cw, ch,
312 inpic->linesize[1], outpic->linesize[1],
316 deNoise(inpic->data[2], outpic->data[2],
317 hqdn3d->Line, &hqdn3d->Frame[2], cw, ch,
318 inpic->linesize[2], outpic->linesize[2],
323 ff_draw_slice(outlink, 0, inpic->video->h, 1);
324 ff_end_frame(outlink);
325 avfilter_unref_buffer(outpic);
328 AVFilter avfilter_vf_hqdn3d = {
330 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
332 .priv_size = sizeof(HQDN3DContext),
335 .query_formats = query_formats,
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 },
344 .outputs = (AVFilterPad[]) {{ .name = "default",
345 .type = AVMEDIA_TYPE_VIDEO },