Mercurial > mplayer.hg
annotate libmpcodecs/vf_ow.c @ 24757:cc80a9169d90
Add -lavfdopts cryptokey
author | reimar |
---|---|
date | Sun, 14 Oct 2007 12:11:28 +0000 |
parents | 4d56303b85ab |
children | 1318366cf2da |
rev | line source |
---|---|
24677 | 1 /* |
2 Copyright (C) 2007 Michael Niedermayer <michaelni@gmx.at> | |
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
17 */ | |
18 | |
19 /** | |
24680
4d56303b85ab
change double arrays to float (this should be accurate enough)
michael
parents:
24679
diff
changeset
|
20 * @todo try to change to int |
24677 | 21 * @todo try lifting based implementation |
22 * @todo optimize optimize optimize | |
23 * @todo hard tresholding | |
24 * @todo use QP to decide filter strength | |
25 * @todo wavelet normalization / least squares optimal signal vs. noise thresholds | |
26 */ | |
27 | |
28 #include <stdio.h> | |
29 #include <string.h> | |
30 #include <inttypes.h> | |
31 #include <math.h> | |
32 | |
33 #include "config.h" | |
34 | |
35 #include "mp_msg.h" | |
36 | |
37 #ifdef HAVE_MALLOC_H | |
38 #include <malloc.h> | |
39 #endif | |
40 | |
41 #include "img_format.h" | |
42 #include "mp_image.h" | |
43 #include "vf.h" | |
44 | |
45 //===========================================================================// | |
46 static const uint8_t __attribute__((aligned(8))) dither[8][8]={ | |
47 { 0, 48, 12, 60, 3, 51, 15, 63, }, | |
48 { 32, 16, 44, 28, 35, 19, 47, 31, }, | |
49 { 8, 56, 4, 52, 11, 59, 7, 55, }, | |
50 { 40, 24, 36, 20, 43, 27, 39, 23, }, | |
51 { 2, 50, 14, 62, 1, 49, 13, 61, }, | |
52 { 34, 18, 46, 30, 33, 17, 45, 29, }, | |
53 { 10, 58, 6, 54, 9, 57, 5, 53, }, | |
54 { 42, 26, 38, 22, 41, 25, 37, 21, }, | |
55 }; | |
56 //FIXME the above is duplicated in many filters | |
57 | |
58 struct vf_priv_s { | |
59 float strength[2]; | |
60 float delta; | |
61 int mode; | |
62 int depth; | |
24680
4d56303b85ab
change double arrays to float (this should be accurate enough)
michael
parents:
24679
diff
changeset
|
63 float *plane[16][4]; |
24677 | 64 int stride; |
65 }; | |
66 | |
67 #define S 1.41421356237 //sqrt(2) | |
68 | |
69 static const double coeff[2][5]={ | |
70 { | |
71 0.6029490182363579 *S, | |
72 0.2668641184428723 *S, | |
73 -0.07822326652898785 *S, | |
74 -0.01686411844287495 *S, | |
75 0.02674875741080976 *S | |
76 },{ | |
77 1.115087052456994 /S, | |
78 -0.5912717631142470 /S, | |
79 -0.05754352622849957 /S, | |
80 0.09127176311424948 /S | |
81 } | |
82 }; | |
83 | |
84 static const double icoeff[2][5]={ | |
85 { | |
86 1.115087052456994 /S, | |
87 0.5912717631142470 /S, | |
88 -0.05754352622849957 /S, | |
89 -0.09127176311424948 /S | |
90 },{ | |
91 0.6029490182363579 *S, | |
92 -0.2668641184428723 *S, | |
93 -0.07822326652898785 *S, | |
94 0.01686411844287495 *S, | |
95 0.02674875741080976 *S | |
96 } | |
97 }; | |
98 #undef S | |
99 | |
100 static inline int mirror(int x, int w){ | |
101 while((unsigned)x > (unsigned)w){ | |
102 x=-x; | |
103 if(x<0) x+= 2*w; | |
104 } | |
105 return x; | |
106 } | |
107 | |
24680
4d56303b85ab
change double arrays to float (this should be accurate enough)
michael
parents:
24679
diff
changeset
|
108 static inline void decompose(float *dstL, float *dstH, float *src, int stride, int w){ |
24677 | 109 int x, i; |
110 for(x=0; x<w; x++){ | |
111 double sumL= src[x*stride] * coeff[0][0]; | |
112 double sumH= src[x*stride] * coeff[1][0]; | |
113 for(i=1; i<=4; i++){ | |
114 double s= (src[mirror(x-i, w-1)*stride] + src[mirror(x+i, w-1)*stride]); | |
115 | |
116 sumL+= coeff[0][i]*s; | |
117 sumH+= coeff[1][i]*s; | |
118 } | |
119 dstL[x*stride]= sumL; | |
120 dstH[x*stride]= sumH; | |
121 } | |
122 } | |
123 | |
24680
4d56303b85ab
change double arrays to float (this should be accurate enough)
michael
parents:
24679
diff
changeset
|
124 static inline void compose(float *dst, float *srcL, float *srcH, int stride, int w){ |
24677 | 125 int x, i; |
126 for(x=0; x<w; x++){ | |
127 double sumL= srcL[x*stride] * icoeff[0][0]; | |
128 double sumH= srcH[x*stride] * icoeff[1][0]; | |
129 for(i=1; i<=4; i++){ | |
130 int x0= mirror(x-i, w-1)*stride; | |
131 int x1= mirror(x+i, w-1)*stride; | |
132 | |
133 sumL+= icoeff[0][i]*(srcL[x0] + srcL[x1]); | |
134 sumH+= icoeff[1][i]*(srcH[x0] + srcH[x1]); | |
135 } | |
136 dst[x*stride]= (sumL + sumH)*0.5; | |
137 } | |
138 } | |
139 | |
24680
4d56303b85ab
change double arrays to float (this should be accurate enough)
michael
parents:
24679
diff
changeset
|
140 static inline void decompose2D(float *dstL, float *dstH, float *src, int xstride, int ystride, int step, int w, int h){ |
24677 | 141 int y, x; |
142 for(y=0; y<h; y++) | |
143 for(x=0; x<step; x++) | |
144 decompose(dstL + ystride*y + xstride*x, dstH + ystride*y + xstride*x, src + ystride*y + xstride*x, step*xstride, (w-x+step-1)/step); | |
145 } | |
146 | |
24680
4d56303b85ab
change double arrays to float (this should be accurate enough)
michael
parents:
24679
diff
changeset
|
147 static inline void compose2D(float *dst, float *srcL, float *srcH, int xstride, int ystride, int step, int w, int h){ |
24677 | 148 int y, x; |
149 for(y=0; y<h; y++) | |
150 for(x=0; x<step; x++) | |
151 compose(dst + ystride*y + xstride*x, srcL + ystride*y + xstride*x, srcH + ystride*y + xstride*x, step*xstride, (w-x+step-1)/step); | |
152 } | |
153 | |
24680
4d56303b85ab
change double arrays to float (this should be accurate enough)
michael
parents:
24679
diff
changeset
|
154 static void decompose2D2(float *dst[4], float *src, float *temp[2], int stride, int step, int w, int h){ |
24677 | 155 decompose2D(temp[0], temp[1], src , 1, stride, step , w, h); |
156 decompose2D( dst[0], dst[1], temp[0], stride, 1, step , h, w); | |
157 decompose2D( dst[2], dst[3], temp[1], stride, 1, step , h, w); | |
158 } | |
159 | |
24680
4d56303b85ab
change double arrays to float (this should be accurate enough)
michael
parents:
24679
diff
changeset
|
160 static void compose2D2(float *dst, float *src[4], float *temp[2], int stride, int step, int w, int h){ |
24677 | 161 compose2D(temp[0], src[0], src[1], stride, 1, step , h, w); |
162 compose2D(temp[1], src[2], src[3], stride, 1, step , h, w); | |
163 compose2D(dst , temp[0], temp[1], 1, stride, step , w, h); | |
164 } | |
165 | |
166 static void filter(struct vf_priv_s *p, uint8_t *dst, uint8_t *src, int dst_stride, int src_stride, int width, int height, int is_luma){ | |
167 int x,y, i, j; | |
168 double sum=0; | |
169 double s= p->strength[!is_luma]; | |
24679 | 170 int depth= p->depth; |
171 | |
172 while(1<<depth > width || 1<<depth > height) | |
173 depth--; | |
24677 | 174 |
175 for(y=0; y<height; y++) | |
176 for(x=0; x<width; x++) | |
177 p->plane[0][0][x + y*p->stride]= src[x + y*src_stride]; | |
178 | |
24679 | 179 for(i=0; i<depth; i++){ |
24677 | 180 decompose2D2(p->plane[i+1], p->plane[i][0], p->plane[0]+1,p->stride, 1<<i, width, height); |
181 } | |
24679 | 182 for(i=0; i<depth; i++){ |
24677 | 183 for(j=1; j<4; j++){ |
184 for(y=0; y<height; y++){ | |
185 for(x=0; x<width; x++){ | |
186 double v= p->plane[i+1][j][x + y*p->stride]; | |
187 if (v> s) v-=s; | |
188 else if(v<-s) v+=s; | |
189 else v =0; | |
190 p->plane[i+1][j][x + y*p->stride]= v; | |
191 } | |
192 } | |
193 } | |
194 } | |
24679 | 195 for(i=depth-1; i>=0; i--){ |
24677 | 196 compose2D2(p->plane[i][0], p->plane[i+1], p->plane[0]+1, p->stride, 1<<i, width, height); |
197 } | |
198 | |
199 for(y=0; y<height; y++) | |
200 for(x=0; x<width; x++){ | |
201 i= p->plane[0][0][x + y*p->stride] + dither[x&7][y&7]*(1.0/64) + 1.0/128; //yes the rounding is insane but optimal :) | |
202 // double e= i - src[x + y*src_stride]; | |
203 // sum += e*e; | |
204 if((unsigned)i > 255U) i= ~(i>>31); | |
205 dst[x + y*dst_stride]= i; | |
206 } | |
207 | |
208 // printf("%f\n", sum/height/width); | |
209 } | |
210 | |
211 static int config(struct vf_instance_s* vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt){ | |
212 int h= (height+15)&(~15); | |
213 int i,j; | |
214 | |
215 vf->priv->stride= (width+15)&(~15); | |
216 for(j=0; j<4; j++){ | |
217 for(i=0; i<=vf->priv->depth; i++) | |
218 vf->priv->plane[i][j]= malloc(vf->priv->stride*h*sizeof(vf->priv->plane[0][0][0])); | |
219 } | |
220 | |
221 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt); | |
222 } | |
223 | |
224 static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){ | |
225 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change | |
226 // ok, we can do pp in-place (or pp disabled): | |
227 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
228 mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height); | |
229 mpi->planes[0]=vf->dmpi->planes[0]; | |
230 mpi->stride[0]=vf->dmpi->stride[0]; | |
231 mpi->width=vf->dmpi->width; | |
232 if(mpi->flags&MP_IMGFLAG_PLANAR){ | |
233 mpi->planes[1]=vf->dmpi->planes[1]; | |
234 mpi->planes[2]=vf->dmpi->planes[2]; | |
235 mpi->stride[1]=vf->dmpi->stride[1]; | |
236 mpi->stride[2]=vf->dmpi->stride[2]; | |
237 } | |
238 mpi->flags|=MP_IMGFLAG_DIRECT; | |
239 } | |
240 | |
241 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){ | |
242 mp_image_t *dmpi; | |
243 | |
244 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){ | |
245 // no DR, so get a new image! hope we'll get DR buffer: | |
246 dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
247 MP_IMGTYPE_TEMP, | |
248 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE, | |
249 mpi->width,mpi->height); | |
250 vf_clone_mpi_attributes(dmpi, mpi); | |
251 }else{ | |
252 dmpi=vf->dmpi; | |
253 } | |
254 | |
255 filter(vf->priv, dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, 1); | |
256 filter(vf->priv, dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, 0); | |
257 filter(vf->priv, dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w>>mpi->chroma_x_shift, mpi->h>>mpi->chroma_y_shift, 0); | |
258 | |
259 return vf_next_put_image(vf,dmpi, pts); | |
260 } | |
261 | |
262 static void uninit(struct vf_instance_s* vf){ | |
263 int i,j; | |
264 if(!vf->priv) return; | |
265 | |
266 for(j=0; j<4; j++){ | |
267 for(i=0; i<16; i++){ | |
268 free(vf->priv->plane[i][j]); | |
269 vf->priv->plane[i][j]= NULL; | |
270 } | |
271 } | |
272 | |
273 free(vf->priv); | |
274 vf->priv=NULL; | |
275 } | |
276 | |
277 //===========================================================================// | |
278 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
279 switch(fmt){ | |
280 case IMGFMT_YVU9: | |
281 case IMGFMT_IF09: | |
282 case IMGFMT_YV12: | |
283 case IMGFMT_I420: | |
284 case IMGFMT_IYUV: | |
285 case IMGFMT_CLPL: | |
286 case IMGFMT_Y800: | |
287 case IMGFMT_Y8: | |
288 case IMGFMT_444P: | |
289 case IMGFMT_422P: | |
290 case IMGFMT_411P: | |
291 return vf_next_query_format(vf,fmt); | |
292 } | |
293 return 0; | |
294 } | |
295 | |
296 static unsigned int fmt_list[]={ | |
297 IMGFMT_YVU9, | |
298 IMGFMT_IF09, | |
299 IMGFMT_YV12, | |
300 IMGFMT_I420, | |
301 IMGFMT_IYUV, | |
302 IMGFMT_CLPL, | |
303 IMGFMT_Y800, | |
304 IMGFMT_Y8, | |
305 IMGFMT_444P, | |
306 IMGFMT_422P, | |
307 IMGFMT_411P, | |
308 0 | |
309 }; | |
310 | |
311 | |
312 static int open(vf_instance_t *vf, char* args){ | |
313 vf->config=config; | |
314 vf->put_image=put_image; | |
315 vf->get_image=get_image; | |
316 vf->query_format=query_format; | |
317 vf->uninit=uninit; | |
318 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
319 memset(vf->priv, 0, sizeof(struct vf_priv_s)); | |
320 | |
321 vf->priv->depth= 8; | |
322 vf->priv->strength[0]= 1.0; | |
323 vf->priv->strength[1]= 1.0; | |
324 vf->priv->delta= 1.0; | |
325 | |
326 if (args) sscanf(args, "%d:%f:%f:%d:%f", &vf->priv->depth, | |
327 &vf->priv->strength[0], | |
328 &vf->priv->strength[1], | |
329 &vf->priv->mode, | |
330 &vf->priv->delta); | |
331 | |
332 return 1; | |
333 } | |
334 | |
335 vf_info_t vf_info_ow = { | |
336 "overcomplete wavelet denoiser", | |
337 "ow", | |
338 "Michael Niedermayer", | |
339 "", | |
340 open, | |
341 NULL | |
342 }; |