2 Copyright (C) 2003 Daniel Moreno <comac@comac.darktech.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "img_format.h"
36 #define PARAM1_DEFAULT 4.0
37 #define PARAM2_DEFAULT 3.0
38 #define PARAM3_DEFAULT 6.0
40 //===========================================================================//
49 /***************************************************************************/
52 static int config(struct vf_instance_s
* vf
,
53 int width
, int height
, int d_width
, int d_height
,
54 unsigned int flags
, unsigned int outfmt
){
56 if(vf
->priv
->Line
) free(vf
->priv
->Line
);
57 vf
->priv
->Line
= malloc(width
);
59 // vf->default_caps &= !VFCAP_ACCEPT_STRIDE;
61 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
65 static void uninit(struct vf_instance_s
* vf
)
70 #define LowPass(Prev, Curr, Coef) (Curr + Coef[Prev - Curr])
72 static void deNoise(unsigned char *Frame
, // mpi->planes[x]
73 unsigned char *FramePrev
, // pmpi->planes[x]
74 unsigned char *FrameDest
, // dmpi->planes[x]
75 unsigned char *LineAnt
, // vf->priv->Line (width bytes)
76 int W
, int H
, int sStride
, int pStride
, int dStride
,
77 int *Horizontal
, int *Vertical
, int *Temporal
)
80 int sLineOffs
= 0, pLineOffs
= 0, dLineOffs
= 0;
81 unsigned char PixelAnt
;
83 /* First pixel has no left nor top neighbor. Only previous frame */
84 LineAnt
[0] = PixelAnt
= Frame
[0];
85 FrameDest
[0] = LowPass(FramePrev
[0], LineAnt
[0], Temporal
);
87 /* Fist line has no top neighbor. Only left one for each pixel and
89 for (X
= 1; X
< W
; X
++)
91 PixelAnt
= LowPass(PixelAnt
, Frame
[X
], Horizontal
);
92 LineAnt
[X
] = PixelAnt
;
93 FrameDest
[X
] = LowPass(FramePrev
[X
], LineAnt
[X
], Temporal
);
96 for (Y
= 1; Y
< H
; Y
++)
98 sLineOffs
+= sStride
, pLineOffs
+= pStride
, dLineOffs
+= dStride
;
99 /* First pixel on each line doesn't have previous pixel */
100 PixelAnt
= Frame
[sLineOffs
];
101 LineAnt
[0] = LowPass(LineAnt
[0], PixelAnt
, Vertical
);
102 FrameDest
[dLineOffs
] = LowPass(FramePrev
[pLineOffs
], LineAnt
[0], Temporal
);
104 for (X
= 1; X
< W
; X
++)
106 /* The rest are normal */
107 PixelAnt
= LowPass(PixelAnt
, Frame
[sLineOffs
+X
], Horizontal
);
108 LineAnt
[X
] = LowPass(LineAnt
[X
], PixelAnt
, Vertical
);
109 FrameDest
[dLineOffs
+X
] = LowPass(FramePrev
[pLineOffs
+X
], LineAnt
[X
], Temporal
);
116 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
117 int cw
= mpi
->w
>> mpi
->chroma_x_shift
;
118 int ch
= mpi
->h
>> mpi
->chroma_y_shift
;
119 int W
= mpi
->w
, H
= mpi
->h
;
121 mp_image_t
*dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
122 MP_IMGTYPE_IP
, MP_IMGFLAG_ACCEPT_STRIDE
|
123 MP_IMGFLAG_PRESERVE
| MP_IMGFLAG_READABLE
,
127 if (!vf
->priv
->pmpi
) vf
->priv
->pmpi
=mpi
;
129 deNoise(mpi
->planes
[0], vf
->priv
->pmpi
->planes
[0], dmpi
->planes
[0],
130 vf
->priv
->Line
, W
, H
,
131 mpi
->stride
[0], vf
->priv
->pmpi
->stride
[0], dmpi
->stride
[0],
132 vf
->priv
->Coefs
[0] + 256,
133 vf
->priv
->Coefs
[0] + 256,
134 vf
->priv
->Coefs
[1] + 256);
135 deNoise(mpi
->planes
[1], vf
->priv
->pmpi
->planes
[1], dmpi
->planes
[1],
136 vf
->priv
->Line
, cw
, ch
,
137 mpi
->stride
[1], vf
->priv
->pmpi
->stride
[1], dmpi
->stride
[1],
138 vf
->priv
->Coefs
[2] + 256,
139 vf
->priv
->Coefs
[2] + 256,
140 vf
->priv
->Coefs
[3] + 256);
141 deNoise(mpi
->planes
[2], vf
->priv
->pmpi
->planes
[2], dmpi
->planes
[2],
142 vf
->priv
->Line
, cw
, ch
,
143 mpi
->stride
[2], vf
->priv
->pmpi
->stride
[2], dmpi
->stride
[2],
144 vf
->priv
->Coefs
[2] + 256,
145 vf
->priv
->Coefs
[2] + 256,
146 vf
->priv
->Coefs
[3] + 256);
148 vf
->priv
->pmpi
=dmpi
; // save reference image
149 return vf_next_put_image(vf
,dmpi
, pts
);
152 //===========================================================================//
154 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
164 return vf_next_query_format(vf
, fmt
);
170 #define ABS(A) ( (A) > 0 ? (A) : -(A) )
172 static void PrecalcCoefs(int *Ct
, double Dist25
)
175 double Gamma
, Simil
, C
;
177 Gamma
= log(0.25) / log(1.0 - Dist25
/255.0);
179 for (i
= -256; i
<= 255; i
++)
181 Simil
= 1.0 - ABS(i
) / 255.0;
182 // Ct[256+i] = lround(pow(Simil, Gamma) * (double)i);
183 C
= pow(Simil
, Gamma
) * (double)i
;
184 Ct
[256+i
] = (C
<0) ? (C
-0.5) : (C
+0.5);
189 static int open(vf_instance_t
*vf
, char* args
){
190 double LumSpac
, LumTmp
, ChromSpac
, ChromTmp
;
191 double Param1
, Param2
, Param3
;
194 vf
->put_image
=put_image
;
195 vf
->query_format
=query_format
;
197 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
198 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
202 switch(sscanf(args
, "%lf:%lf:%lf",
203 &Param1
, &Param2
, &Param3
207 LumSpac
= PARAM1_DEFAULT
;
208 LumTmp
= PARAM3_DEFAULT
;
210 ChromSpac
= PARAM2_DEFAULT
;
211 ChromTmp
= LumTmp
* ChromSpac
/ LumSpac
;
216 LumTmp
= PARAM3_DEFAULT
* Param1
/ PARAM1_DEFAULT
;
218 ChromSpac
= PARAM2_DEFAULT
* Param1
/ PARAM1_DEFAULT
;
219 ChromTmp
= LumTmp
* ChromSpac
/ LumSpac
;
224 LumTmp
= PARAM3_DEFAULT
* Param1
/ PARAM1_DEFAULT
;
227 ChromTmp
= LumTmp
* ChromSpac
/ LumSpac
;
235 ChromTmp
= LumTmp
* ChromSpac
/ LumSpac
;
239 LumSpac
= PARAM1_DEFAULT
;
240 LumTmp
= PARAM3_DEFAULT
;
242 ChromSpac
= PARAM2_DEFAULT
;
243 ChromTmp
= LumTmp
* ChromSpac
/ LumSpac
;
248 LumSpac
= PARAM1_DEFAULT
;
249 LumTmp
= PARAM3_DEFAULT
;
251 ChromSpac
= PARAM2_DEFAULT
;
252 ChromTmp
= LumTmp
* ChromSpac
/ LumSpac
;
255 PrecalcCoefs(vf
->priv
->Coefs
[0], LumSpac
);
256 PrecalcCoefs(vf
->priv
->Coefs
[1], LumTmp
);
257 PrecalcCoefs(vf
->priv
->Coefs
[2], ChromSpac
);
258 PrecalcCoefs(vf
->priv
->Coefs
[3], ChromTmp
);
263 const vf_info_t vf_info_denoise3d
= {
264 "3D Denoiser (variable lowpass filter)",
272 //===========================================================================//