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"
34 unsigned short *Frame[3];
38 static inline unsigned int LowPassMul(unsigned int PrevMul, unsigned int CurrMul, int *Coef)
40 // int dMul= (PrevMul&0xFFFFFF)-(CurrMul&0xFFFFFF);
41 int dMul= PrevMul-CurrMul;
42 unsigned int d=((dMul+0x10007FF)>>12);
43 return CurrMul + Coef[d];
46 static void deNoiseTemporal(unsigned char *FrameSrc,
47 unsigned char *FrameDest,
48 unsigned short *FrameAnt,
49 int W, int H, int sStride, int dStride,
53 unsigned int PixelDst;
55 for (Y = 0; Y < H; Y++) {
56 for (X = 0; X < W; X++) {
57 PixelDst = LowPassMul(FrameAnt[X]<<8, FrameSrc[X]<<16, Temporal);
58 FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
59 FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
67 static void deNoiseSpacial(unsigned char *Frame,
68 unsigned char *FrameDest,
69 unsigned int *LineAnt,
70 int W, int H, int sStride, int dStride,
71 int *Horizontal, int *Vertical)
74 long sLineOffs = 0, dLineOffs = 0;
75 unsigned int PixelAnt;
76 unsigned int PixelDst;
78 /* First pixel has no left nor top neighbor. */
79 PixelDst = LineAnt[0] = PixelAnt = Frame[0]<<16;
80 FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
82 /* First line has no top neighbor, only left. */
83 for (X = 1; X < W; X++) {
84 PixelDst = LineAnt[X] = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
85 FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
88 for (Y = 1; Y < H; Y++) {
89 unsigned int PixelAnt;
90 sLineOffs += sStride, dLineOffs += dStride;
91 /* First pixel on each line doesn't have previous pixel */
92 PixelAnt = Frame[sLineOffs]<<16;
93 PixelDst = LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
94 FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
96 for (X = 1; X < W; X++) {
97 unsigned int PixelDst;
98 /* The rest are normal */
99 PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
100 PixelDst = LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
101 FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
106 static void deNoise(unsigned char *Frame,
107 unsigned char *FrameDest,
108 unsigned int *LineAnt,
109 unsigned short **FrameAntPtr,
110 int W, int H, int sStride, int dStride,
111 int *Horizontal, int *Vertical, int *Temporal)
114 long sLineOffs = 0, dLineOffs = 0;
115 unsigned int PixelAnt;
116 unsigned int PixelDst;
117 unsigned short* FrameAnt=(*FrameAntPtr);
120 (*FrameAntPtr) = FrameAnt = av_malloc(W*H*sizeof(unsigned short));
121 for (Y = 0; Y < H; Y++) {
122 unsigned short* dst=&FrameAnt[Y*W];
123 unsigned char* src=Frame+Y*sStride;
124 for (X = 0; X < W; X++) dst[X]=src[X]<<8;
128 if (!Horizontal[0] && !Vertical[0]) {
129 deNoiseTemporal(Frame, FrameDest, FrameAnt,
130 W, H, sStride, dStride, Temporal);
134 deNoiseSpacial(Frame, FrameDest, LineAnt,
135 W, H, sStride, dStride, Horizontal, Vertical);
139 /* First pixel has no left nor top neighbor. Only previous frame */
140 LineAnt[0] = PixelAnt = Frame[0]<<16;
141 PixelDst = LowPassMul(FrameAnt[0]<<8, PixelAnt, Temporal);
142 FrameAnt[0] = ((PixelDst+0x1000007F)>>8);
143 FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
145 /* First line has no top neighbor. Only left one for each pixel and
147 for (X = 1; X < W; X++) {
148 LineAnt[X] = PixelAnt = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
149 PixelDst = LowPassMul(FrameAnt[X]<<8, PixelAnt, Temporal);
150 FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
151 FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
154 for (Y = 1; Y < H; Y++) {
155 unsigned int PixelAnt;
156 unsigned short* LinePrev=&FrameAnt[Y*W];
157 sLineOffs += sStride, dLineOffs += dStride;
158 /* First pixel on each line doesn't have previous pixel */
159 PixelAnt = Frame[sLineOffs]<<16;
160 LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
161 PixelDst = LowPassMul(LinePrev[0]<<8, LineAnt[0], Temporal);
162 LinePrev[0] = ((PixelDst+0x1000007F)>>8);
163 FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
165 for (X = 1; X < W; X++) {
166 unsigned int PixelDst;
167 /* The rest are normal */
168 PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
169 LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
170 PixelDst = LowPassMul(LinePrev[X]<<8, LineAnt[X], Temporal);
171 LinePrev[X] = ((PixelDst+0x1000007F)>>8);
172 FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
177 static void PrecalcCoefs(int *Ct, double Dist25)
180 double Gamma, Simil, C;
182 Gamma = log(0.25) / log(1.0 - Dist25/255.0 - 0.00001);
184 for (i = -255*16; i <= 255*16; i++) {
185 Simil = 1.0 - FFABS(i) / (16*255.0);
186 C = pow(Simil, Gamma) * 65536.0 * i / 16.0;
187 Ct[16*256+i] = lrint(C);
193 #define PARAM1_DEFAULT 4.0
194 #define PARAM2_DEFAULT 3.0
195 #define PARAM3_DEFAULT 6.0
197 static int init(AVFilterContext *ctx, const char *args, void *opaque)
199 HQDN3DContext *hqdn3d = ctx->priv;
200 double LumSpac, LumTmp, ChromSpac, ChromTmp;
201 double Param1, Param2, Param3, Param4;
203 LumSpac = PARAM1_DEFAULT;
204 ChromSpac = PARAM2_DEFAULT;
205 LumTmp = PARAM3_DEFAULT;
206 ChromTmp = LumTmp * ChromSpac / LumSpac;
209 switch (sscanf(args, "%lf:%lf:%lf:%lf",
210 &Param1, &Param2, &Param3, &Param4)) {
213 ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT;
214 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
215 ChromTmp = LumTmp * ChromSpac / LumSpac;
220 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
221 ChromTmp = LumTmp * ChromSpac / LumSpac;
227 ChromTmp = LumTmp * ChromSpac / LumSpac;
238 av_log(ctx, AV_LOG_INFO, "ls:%lf cs:%lf lt:%lf ct:%lf\n",
239 LumSpac, ChromSpac, LumTmp, ChromTmp);
240 if (LumSpac < 0 || ChromSpac < 0 || isnan(ChromTmp)) {
241 av_log(ctx, AV_LOG_ERROR,
242 "Invalid negative value for luma or chroma spatial strength, "
243 "or resulting value for chroma temporal strength is nan.\n");
244 return AVERROR(EINVAL);
247 PrecalcCoefs(hqdn3d->Coefs[0], LumSpac);
248 PrecalcCoefs(hqdn3d->Coefs[1], LumTmp);
249 PrecalcCoefs(hqdn3d->Coefs[2], ChromSpac);
250 PrecalcCoefs(hqdn3d->Coefs[3], ChromTmp);
255 static void uninit(AVFilterContext *ctx)
257 HQDN3DContext *hqdn3d = ctx->priv;
259 av_freep(&hqdn3d->Line);
260 av_freep(&hqdn3d->Frame[0]);
261 av_freep(&hqdn3d->Frame[1]);
262 av_freep(&hqdn3d->Frame[2]);
265 static int query_formats(AVFilterContext *ctx)
267 static const enum PixelFormat pix_fmts[] = {
268 PIX_FMT_YUV420P, PIX_FMT_YUV422P, PIX_FMT_YUV411P, PIX_FMT_NONE
271 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
276 static int config_input(AVFilterLink *inlink)
278 HQDN3DContext *hqdn3d = inlink->dst->priv;
280 hqdn3d->hsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_w;
281 hqdn3d->vsub = av_pix_fmt_descriptors[inlink->format].log2_chroma_h;
283 hqdn3d->Line = av_malloc(inlink->w * sizeof(*hqdn3d->Line));
285 return AVERROR(ENOMEM);
290 static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir) { }
292 static void end_frame(AVFilterLink *inlink)
294 HQDN3DContext *hqdn3d = inlink->dst->priv;
295 AVFilterLink *outlink = inlink->dst->outputs[0];
296 AVFilterBufferRef *inpic = inlink ->cur_buf;
297 AVFilterBufferRef *outpic = outlink->out_buf;
298 int cw = inpic->video->w >> hqdn3d->hsub;
299 int ch = inpic->video->h >> hqdn3d->vsub;
301 deNoise(inpic->data[0], outpic->data[0],
302 hqdn3d->Line, &hqdn3d->Frame[0], inpic->video->w, inpic->video->h,
303 inpic->linesize[0], outpic->linesize[0],
307 deNoise(inpic->data[1], outpic->data[1],
308 hqdn3d->Line, &hqdn3d->Frame[1], cw, ch,
309 inpic->linesize[1], outpic->linesize[1],
313 deNoise(inpic->data[2], outpic->data[2],
314 hqdn3d->Line, &hqdn3d->Frame[2], cw, ch,
315 inpic->linesize[2], outpic->linesize[2],
320 avfilter_draw_slice(outlink, 0, inpic->video->h, 1);
321 avfilter_end_frame(outlink);
322 avfilter_unref_buffer(inpic);
323 avfilter_unref_buffer(outpic);
326 AVFilter avfilter_vf_hqdn3d = {
328 .description = NULL_IF_CONFIG_SMALL("Apply a High Quality 3D Denoiser."),
330 .priv_size = sizeof(HQDN3DContext),
333 .query_formats = query_formats,
335 .inputs = (AVFilterPad[]) {{ .name = "default",
336 .type = AVMEDIA_TYPE_VIDEO,
337 .draw_slice = null_draw_slice,
338 .config_props = config_input,
339 .end_frame = end_frame },
342 .outputs = (AVFilterPad[]) {{ .name = "default",
343 .type = AVMEDIA_TYPE_VIDEO },