Mercurial > mplayer.hg
comparison libmpcodecs/vf_denoise3d.c @ 9179:e93a0dd3ed56
new video filter: denoise3d - temporal&spatial noise reduction
patch by Daniel Moreno <comac2k@terra.es>
mpi/stride corrections and some bugfix by me
author | arpi |
---|---|
date | Thu, 30 Jan 2003 22:53:32 +0000 |
parents | |
children | df871e1c105d |
comparison
equal
deleted
inserted
replaced
9178:b2bcaf612d5f | 9179:e93a0dd3ed56 |
---|---|
1 /* | |
2 Copyright (C) 2003 Daniel Moreno <comac@comac.darktech.org> | |
3 | |
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. | |
8 | |
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. | |
13 | |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 */ | |
18 | |
19 #include <stdio.h> | |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 #include <inttypes.h> | |
23 #include <math.h> | |
24 | |
25 #include "../config.h" | |
26 #include "../mp_msg.h" | |
27 | |
28 #ifdef HAVE_MALLOC_H | |
29 #include <malloc.h> | |
30 #endif | |
31 | |
32 #include "img_format.h" | |
33 #include "mp_image.h" | |
34 #include "vf.h" | |
35 #include "../libvo/fastmemcpy.h" | |
36 | |
37 #define PARAM1_DEFAULT 4.0 | |
38 #define PARAM2_DEFAULT 3.0 | |
39 #define PARAM3_DEFAULT 6.0 | |
40 | |
41 //===========================================================================// | |
42 | |
43 struct vf_priv_s { | |
44 int Coefs[4][512]; | |
45 unsigned char *Line; | |
46 mp_image_t *pmpi; | |
47 }; | |
48 | |
49 | |
50 /***************************************************************************/ | |
51 | |
52 | |
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){ | |
56 | |
57 if(vf->priv->Line) free(vf->priv->Line); | |
58 vf->priv->Line = malloc(width); | |
59 vf->priv->pmpi=NULL; | |
60 // vf->default_caps &= !VFCAP_ACCEPT_STRIDE; | |
61 | |
62 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt); | |
63 } | |
64 | |
65 | |
66 void uninit(struct vf_instance_s* vf) | |
67 { | |
68 free(vf->priv->Line); | |
69 } | |
70 | |
71 | |
72 #define LowPass(Prev, Curr, Coef) (((Prev)*Coef[Prev - Curr] + (Curr)*(65536-(Coef[Prev - Curr]))) / 65536) | |
73 | |
74 static void deNoise(unsigned char *Frame, // mpi->planes[x] | |
75 unsigned char *FramePrev, // pmpi->planes[x] | |
76 unsigned char *FrameDest, // dmpi->planes[x] | |
77 unsigned char *LineAnt, // vf->priv->Line (width bytes) | |
78 int W, int H, int sStride, int pStride, int dStride, | |
79 int *Horizontal, int *Vertical, int *Temporal) | |
80 { | |
81 int X, Y; | |
82 int sLineOffs = 0, pLineOffs = 0, dLineOffs = 0; | |
83 unsigned char PixelAnt; | |
84 | |
85 /* First pixel has no left nor top neightbour. Only previous frame */ | |
86 LineAnt[0] = PixelAnt = Frame[0]; | |
87 FrameDest[0] = LowPass(FramePrev[0], LineAnt[0], Temporal); | |
88 | |
89 /* Fist line has no top neightbour. Only left one for each pixel and | |
90 * last frame */ | |
91 for (X = 1; X < W; X++) | |
92 { | |
93 PixelAnt = LowPass(PixelAnt, Frame[X], Horizontal); | |
94 LineAnt[X] = PixelAnt; | |
95 FrameDest[X] = LowPass(FramePrev[X], LineAnt[X], Temporal); | |
96 } | |
97 | |
98 for (Y = 1; Y < H; Y++) | |
99 { | |
100 sLineOffs += sStride, pLineOffs += pStride, dLineOffs += dStride; | |
101 /* First pixel on each line doesn't have previous pixel */ | |
102 PixelAnt = Frame[sLineOffs]; | |
103 LineAnt[0] = LowPass(LineAnt[0], PixelAnt, Vertical); | |
104 FrameDest[dLineOffs] = LowPass(FramePrev[pLineOffs], LineAnt[0], Temporal); | |
105 | |
106 for (X = 1; X < W; X++) | |
107 { | |
108 /* The rest are normal */ | |
109 PixelAnt = LowPass(PixelAnt, Frame[sLineOffs+X], Horizontal); | |
110 LineAnt[X] = LowPass(LineAnt[X], PixelAnt, Vertical); | |
111 FrameDest[dLineOffs+X] = LowPass(FramePrev[pLineOffs+X], LineAnt[X], Temporal); | |
112 } | |
113 } | |
114 } | |
115 | |
116 | |
117 | |
118 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){ | |
119 int cw= mpi->w >> mpi->chroma_x_shift; | |
120 int ch= mpi->h >> mpi->chroma_y_shift; | |
121 int W = mpi->w, H = mpi->h; | |
122 | |
123 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
124 MP_IMGTYPE_IP, MP_IMGFLAG_ACCEPT_STRIDE, | |
125 mpi->w,mpi->h); | |
126 | |
127 if(!dmpi) return 0; | |
128 if (!vf->priv->pmpi) vf->priv->pmpi=mpi; | |
129 | |
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); | |
148 | |
149 vf->priv->pmpi=mpi; // save reference image | |
150 return vf_next_put_image(vf,dmpi); | |
151 } | |
152 | |
153 //===========================================================================// | |
154 | |
155 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
156 switch(fmt) | |
157 { | |
158 case IMGFMT_YV12: | |
159 case IMGFMT_I420: | |
160 case IMGFMT_IYUV: | |
161 case IMGFMT_YVU9: | |
162 case IMGFMT_444P: | |
163 case IMGFMT_422P: | |
164 case IMGFMT_411P: | |
165 return vf_next_query_format(vf, fmt); | |
166 } | |
167 return 0; | |
168 } | |
169 | |
170 | |
171 #define ABS(A) ( (A) > 0 ? (A) : -(A) ) | |
172 | |
173 static void PrecalcCoefs(int *Ct, double Dist25) | |
174 { | |
175 int i; | |
176 double Gamma, Simil; | |
177 | |
178 Gamma = log(0.25) / log(1.0 - Dist25/255.0); | |
179 | |
180 for (i = -256; i <= 255; i++) | |
181 { | |
182 Simil = 1.0 - ABS(i) / 255.0; | |
183 Ct[256+i] = pow(Simil, Gamma) * 65536; | |
184 } | |
185 } | |
186 | |
187 | |
188 static int open(vf_instance_t *vf, char* args){ | |
189 double LumSpac, LumTmp, ChromSpac, ChromTmp; | |
190 double Param1, Param2, Param3; | |
191 | |
192 vf->config=config; | |
193 vf->put_image=put_image; | |
194 vf->query_format=query_format; | |
195 vf->uninit=uninit; | |
196 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
197 memset(vf->priv, 0, sizeof(struct vf_priv_s)); | |
198 | |
199 if (args) | |
200 { | |
201 switch(sscanf(args, "%lf:%lf:%lf", | |
202 &Param1, &Param2, &Param3 | |
203 )) | |
204 { | |
205 case 0: | |
206 LumSpac = PARAM1_DEFAULT; | |
207 LumTmp = PARAM3_DEFAULT; | |
208 | |
209 ChromSpac = PARAM2_DEFAULT; | |
210 ChromTmp = LumTmp * ChromSpac / LumSpac; | |
211 break; | |
212 | |
213 case 1: | |
214 LumSpac = Param1; | |
215 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT; | |
216 | |
217 ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT; | |
218 ChromTmp = LumTmp * ChromSpac / LumSpac; | |
219 break; | |
220 | |
221 case 2: | |
222 LumSpac = Param1; | |
223 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT; | |
224 | |
225 ChromSpac = Param2; | |
226 ChromTmp = LumTmp * ChromSpac / LumSpac; | |
227 break; | |
228 | |
229 case 3: | |
230 LumSpac = Param1; | |
231 LumTmp = Param3; | |
232 | |
233 ChromSpac = Param2; | |
234 ChromTmp = LumTmp * ChromSpac / LumSpac; | |
235 break; | |
236 | |
237 default: | |
238 LumSpac = PARAM1_DEFAULT; | |
239 LumTmp = PARAM3_DEFAULT; | |
240 | |
241 ChromSpac = PARAM2_DEFAULT; | |
242 ChromTmp = LumTmp * ChromSpac / LumSpac; | |
243 } | |
244 } | |
245 else | |
246 { | |
247 LumSpac = PARAM1_DEFAULT; | |
248 LumTmp = PARAM3_DEFAULT; | |
249 | |
250 ChromSpac = PARAM2_DEFAULT; | |
251 ChromTmp = LumTmp * ChromSpac / LumSpac; | |
252 } | |
253 | |
254 PrecalcCoefs(vf->priv->Coefs[0], LumSpac); | |
255 PrecalcCoefs(vf->priv->Coefs[1], LumTmp); | |
256 PrecalcCoefs(vf->priv->Coefs[2], ChromSpac); | |
257 PrecalcCoefs(vf->priv->Coefs[3], ChromTmp); | |
258 | |
259 return 1; | |
260 } | |
261 | |
262 vf_info_t vf_info_denoise3d = { | |
263 "3D Denoiser (variable lowpass filter)", | |
264 "denoise3d", | |
265 "Daniel Moreno", | |
266 "", | |
267 open | |
268 }; | |
269 | |
270 //===========================================================================// |