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"
36 unsigned short *Frame[3];
40 static inline unsigned int LowPassMul(unsigned int PrevMul, unsigned int CurrMul, int *Coef)
42 // int dMul= (PrevMul&0xFFFFFF)-(CurrMul&0xFFFFFF);
43 int dMul= PrevMul-CurrMul;
44 unsigned int d=((dMul+0x10007FF)>>12);
45 return CurrMul + Coef[d];
48 static void deNoiseTemporal(unsigned char *FrameSrc,
49 unsigned char *FrameDest,
50 unsigned short *FrameAnt,
51 int W, int H, int sStride, int dStride,
55 unsigned int PixelDst;
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);
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)
76 long sLineOffs = 0, dLineOffs = 0;
77 unsigned int PixelAnt;
78 unsigned int PixelDst;
80 /* First pixel has no left nor top neighbor. */
81 PixelDst = LineAnt[0] = PixelAnt = Frame[0]<<16;
82 FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
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);
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);
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);
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)
116 long sLineOffs = 0, dLineOffs = 0;
117 unsigned int PixelAnt;
118 unsigned int PixelDst;
119 unsigned short* FrameAnt=(*FrameAntPtr);
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;
130 if (!Horizontal[0] && !Vertical[0]) {
131 deNoiseTemporal(Frame, FrameDest, FrameAnt,
132 W, H, sStride, dStride, Temporal);
136 deNoiseSpacial(Frame, FrameDest, LineAnt,
137 W, H, sStride, dStride, Horizontal, Vertical);
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);
147 /* First line has no top neighbor. Only left one for each pixel and
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);
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);
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);
179 static void PrecalcCoefs(int *Ct, double Dist25)
182 double Gamma, Simil, C;
184 Gamma = log(0.25) / log(1.0 - Dist25/255.0 - 0.00001);
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);
195 #define PARAM1_DEFAULT 4.0
196 #define PARAM2_DEFAULT 3.0
197 #define PARAM3_DEFAULT 6.0
199 static int init(AVFilterContext *ctx, const char *args, void *opaque)
201 HQDN3DContext *hqdn3d = ctx->priv;
202 double LumSpac, LumTmp, ChromSpac, ChromTmp;
203 double Param1, Param2, Param3, Param4;
205 LumSpac = PARAM1_DEFAULT;
206 ChromSpac = PARAM2_DEFAULT;
207 LumTmp = PARAM3_DEFAULT;
208 ChromTmp = LumTmp * ChromSpac / LumSpac;
211 switch (sscanf(args, "%lf:%lf:%lf:%lf",
212 &Param1, &Param2, &Param3, &Param4)) {
215 ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT;
216 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
217 ChromTmp = LumTmp * ChromSpac / LumSpac;
222 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
223 ChromTmp = LumTmp * ChromSpac / LumSpac;
229 ChromTmp = LumTmp * ChromSpac / LumSpac;
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);
249 PrecalcCoefs(hqdn3d->Coefs[0], LumSpac);
250 PrecalcCoefs(hqdn3d->Coefs[1], LumTmp);
251 PrecalcCoefs(hqdn3d->Coefs[2], ChromSpac);
252 PrecalcCoefs(hqdn3d->Coefs[3], ChromTmp);
257 static void uninit(AVFilterContext *ctx)
259 HQDN3DContext *hqdn3d = ctx->priv;
261 av_freep(&hqdn3d->Line);
262 av_freep(&hqdn3d->Frame[0]);
263 av_freep(&hqdn3d->Frame[1]);
264 av_freep(&hqdn3d->Frame[2]);
267 static int query_formats(AVFilterContext *ctx)
269 static const enum PixelFormat pix_fmts[] = {
270 PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_NONE
273 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
278 static int config_input(AVFilterLink *inlink)
280 HQDN3DContext *hqdn3d = inlink->dst->priv;
282 hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
283 hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
285 hqdn3d->Line = av_malloc(inlink->w * sizeof(*hqdn3d->Line));
287 return AVERROR(ENOMEM);
292 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
294 static void end_frame(AVFilterLink *inlink)
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;
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],
309 deNoise(inpic->data[1], outpic->data[1],
310 hqdn3d->Line, &hqdn3d->Frame[1], cw, ch,
311 inpic->linesize[1], outpic->linesize[1],
315 deNoise(inpic->data[2], outpic->data[2],
316 hqdn3d->Line, &hqdn3d->Frame[2], cw, ch,
317 inpic->linesize[2], outpic->linesize[2],
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);
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 },