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"
35 #include "libvo/fastmemcpy.h"
37 #define PARAM1_DEFAULT 4.0
38 #define PARAM2_DEFAULT 3.0
39 #define PARAM3_DEFAULT 6.0
41 //===========================================================================//
50 /***************************************************************************/
53 static int config(struct vf_instance_s
* vf
,
54 int width
, int height
, int d_width
, int d_height
,
55 unsigned int flags
, unsigned int outfmt
){
57 if(vf
->priv
->Line
) free(vf
->priv
->Line
);
58 vf
->priv
->Line
= malloc(width
);
60 // vf->default_caps &= !VFCAP_ACCEPT_STRIDE;
62 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
66 static void uninit(struct vf_instance_s
* vf
)
71 #define LowPass(Prev, Curr, Coef) (Curr + Coef[Prev - Curr])
73 static void deNoise(unsigned char *Frame
, // mpi->planes[x]
74 unsigned char *FramePrev
, // pmpi->planes[x]
75 unsigned char *FrameDest
, // dmpi->planes[x]
76 unsigned char *LineAnt
, // vf->priv->Line (width bytes)
77 int W
, int H
, int sStride
, int pStride
, int dStride
,
78 int *Horizontal
, int *Vertical
, int *Temporal
)
81 int sLineOffs
= 0, pLineOffs
= 0, dLineOffs
= 0;
82 unsigned char PixelAnt
;
84 /* First pixel has no left nor top neighbor. Only previous frame */
85 LineAnt
[0] = PixelAnt
= Frame
[0];
86 FrameDest
[0] = LowPass(FramePrev
[0], LineAnt
[0], Temporal
);
88 /* Fist line has no top neighbor. Only left one for each pixel and
90 for (X
= 1; X
< W
; X
++)
92 PixelAnt
= LowPass(PixelAnt
, Frame
[X
], Horizontal
);
93 LineAnt
[X
] = PixelAnt
;
94 FrameDest
[X
] = LowPass(FramePrev
[X
], LineAnt
[X
], Temporal
);
97 for (Y
= 1; Y
< H
; Y
++)
99 sLineOffs
+= sStride
, pLineOffs
+= pStride
, dLineOffs
+= dStride
;
100 /* First pixel on each line doesn't have previous pixel */
101 PixelAnt
= Frame
[sLineOffs
];
102 LineAnt
[0] = LowPass(LineAnt
[0], PixelAnt
, Vertical
);
103 FrameDest
[dLineOffs
] = LowPass(FramePrev
[pLineOffs
], LineAnt
[0], Temporal
);
105 for (X
= 1; X
< W
; X
++)
107 /* The rest are normal */
108 PixelAnt
= LowPass(PixelAnt
, Frame
[sLineOffs
+X
], Horizontal
);
109 LineAnt
[X
] = LowPass(LineAnt
[X
], PixelAnt
, Vertical
);
110 FrameDest
[dLineOffs
+X
] = LowPass(FramePrev
[pLineOffs
+X
], LineAnt
[X
], Temporal
);
117 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
118 int cw
= mpi
->w
>> mpi
->chroma_x_shift
;
119 int ch
= mpi
->h
>> mpi
->chroma_y_shift
;
120 int W
= mpi
->w
, H
= mpi
->h
;
122 mp_image_t
*dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
123 MP_IMGTYPE_IP
, MP_IMGFLAG_ACCEPT_STRIDE
|
124 MP_IMGFLAG_PRESERVE
| MP_IMGFLAG_READABLE
,
128 if (!vf
->priv
->pmpi
) vf
->priv
->pmpi
=mpi
;
130 deNoise(mpi
->planes
[0], vf
->priv
->pmpi
->planes
[0], dmpi
->planes
[0],
131 vf
->priv
->Line
, W
, H
,
132 mpi
->stride
[0], vf
->priv
->pmpi
->stride
[0], dmpi
->stride
[0],
133 vf
->priv
->Coefs
[0] + 256,
134 vf
->priv
->Coefs
[0] + 256,
135 vf
->priv
->Coefs
[1] + 256);
136 deNoise(mpi
->planes
[1], vf
->priv
->pmpi
->planes
[1], dmpi
->planes
[1],
137 vf
->priv
->Line
, cw
, ch
,
138 mpi
->stride
[1], vf
->priv
->pmpi
->stride
[1], dmpi
->stride
[1],
139 vf
->priv
->Coefs
[2] + 256,
140 vf
->priv
->Coefs
[2] + 256,
141 vf
->priv
->Coefs
[3] + 256);
142 deNoise(mpi
->planes
[2], vf
->priv
->pmpi
->planes
[2], dmpi
->planes
[2],
143 vf
->priv
->Line
, cw
, ch
,
144 mpi
->stride
[2], vf
->priv
->pmpi
->stride
[2], dmpi
->stride
[2],
145 vf
->priv
->Coefs
[2] + 256,
146 vf
->priv
->Coefs
[2] + 256,
147 vf
->priv
->Coefs
[3] + 256);
149 vf
->priv
->pmpi
=dmpi
; // save reference image
150 return vf_next_put_image(vf
,dmpi
, pts
);
153 //===========================================================================//
155 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
165 return vf_next_query_format(vf
, fmt
);
171 #define ABS(A) ( (A) > 0 ? (A) : -(A) )
173 static void PrecalcCoefs(int *Ct
, double Dist25
)
176 double Gamma
, Simil
, C
;
178 Gamma
= log(0.25) / log(1.0 - Dist25
/255.0);
180 for (i
= -256; i
<= 255; i
++)
182 Simil
= 1.0 - ABS(i
) / 255.0;
183 // Ct[256+i] = lround(pow(Simil, Gamma) * (double)i);
184 C
= pow(Simil
, Gamma
) * (double)i
;
185 Ct
[256+i
] = (C
<0) ? (C
-0.5) : (C
+0.5);
190 static int open(vf_instance_t
*vf
, char* args
){
191 double LumSpac
, LumTmp
, ChromSpac
, ChromTmp
;
192 double Param1
, Param2
, Param3
;
195 vf
->put_image
=put_image
;
196 vf
->query_format
=query_format
;
198 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
199 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
203 switch(sscanf(args
, "%lf:%lf:%lf",
204 &Param1
, &Param2
, &Param3
208 LumSpac
= PARAM1_DEFAULT
;
209 LumTmp
= PARAM3_DEFAULT
;
211 ChromSpac
= PARAM2_DEFAULT
;
212 ChromTmp
= LumTmp
* ChromSpac
/ LumSpac
;
217 LumTmp
= PARAM3_DEFAULT
* Param1
/ PARAM1_DEFAULT
;
219 ChromSpac
= PARAM2_DEFAULT
* Param1
/ PARAM1_DEFAULT
;
220 ChromTmp
= LumTmp
* ChromSpac
/ LumSpac
;
225 LumTmp
= PARAM3_DEFAULT
* Param1
/ PARAM1_DEFAULT
;
228 ChromTmp
= LumTmp
* ChromSpac
/ LumSpac
;
236 ChromTmp
= LumTmp
* ChromSpac
/ LumSpac
;
240 LumSpac
= PARAM1_DEFAULT
;
241 LumTmp
= PARAM3_DEFAULT
;
243 ChromSpac
= PARAM2_DEFAULT
;
244 ChromTmp
= LumTmp
* ChromSpac
/ LumSpac
;
249 LumSpac
= PARAM1_DEFAULT
;
250 LumTmp
= PARAM3_DEFAULT
;
252 ChromSpac
= PARAM2_DEFAULT
;
253 ChromTmp
= LumTmp
* ChromSpac
/ LumSpac
;
256 PrecalcCoefs(vf
->priv
->Coefs
[0], LumSpac
);
257 PrecalcCoefs(vf
->priv
->Coefs
[1], LumTmp
);
258 PrecalcCoefs(vf
->priv
->Coefs
[2], ChromSpac
);
259 PrecalcCoefs(vf
->priv
->Coefs
[3], ChromTmp
);
264 vf_info_t vf_info_denoise3d
= {
265 "3D Denoiser (variable lowpass filter)",
273 //===========================================================================//