2 * Copyright (C) 2007 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of MPlayer.
6 * MPlayer is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * MPlayer is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * @todo try to change to int
23 * @todo try lifting based implementation
24 * @todo optimize optimize optimize
25 * @todo hard tresholding
26 * @todo use QP to decide filter strength
27 * @todo wavelet normalization / least squares optimal signal vs. noise thresholds
43 #include "img_format.h"
47 //===========================================================================//
48 static const uint8_t __attribute__((aligned(8))) dither
[8][8]={
49 { 0, 48, 12, 60, 3, 51, 15, 63, },
50 { 32, 16, 44, 28, 35, 19, 47, 31, },
51 { 8, 56, 4, 52, 11, 59, 7, 55, },
52 { 40, 24, 36, 20, 43, 27, 39, 23, },
53 { 2, 50, 14, 62, 1, 49, 13, 61, },
54 { 34, 18, 46, 30, 33, 17, 45, 29, },
55 { 10, 58, 6, 54, 9, 57, 5, 53, },
56 { 42, 26, 38, 22, 41, 25, 37, 21, },
58 //FIXME the above is duplicated in many filters
69 #define S 1.41421356237 //sqrt(2)
71 static const double coeff
[2][5]={
73 0.6029490182363579 *S
,
74 0.2668641184428723 *S
,
75 -0.07822326652898785 *S
,
76 -0.01686411844287495 *S
,
77 0.02674875741080976 *S
80 -0.5912717631142470 /S
,
81 -0.05754352622849957 /S
,
82 0.09127176311424948 /S
86 static const double icoeff
[2][5]={
89 0.5912717631142470 /S
,
90 -0.05754352622849957 /S
,
91 -0.09127176311424948 /S
93 0.6029490182363579 *S
,
94 -0.2668641184428723 *S
,
95 -0.07822326652898785 *S
,
96 0.01686411844287495 *S
,
97 0.02674875741080976 *S
102 static inline int mirror(int x
, int w
){
103 while((unsigned)x
> (unsigned)w
){
110 static inline void decompose(float *dstL
, float *dstH
, float *src
, int stride
, int w
){
113 double sumL
= src
[x
*stride
] * coeff
[0][0];
114 double sumH
= src
[x
*stride
] * coeff
[1][0];
116 double s
= (src
[mirror(x
-i
, w
-1)*stride
] + src
[mirror(x
+i
, w
-1)*stride
]);
118 sumL
+= coeff
[0][i
]*s
;
119 sumH
+= coeff
[1][i
]*s
;
121 dstL
[x
*stride
]= sumL
;
122 dstH
[x
*stride
]= sumH
;
126 static inline void compose(float *dst
, float *srcL
, float *srcH
, int stride
, int w
){
129 double sumL
= srcL
[x
*stride
] * icoeff
[0][0];
130 double sumH
= srcH
[x
*stride
] * icoeff
[1][0];
132 int x0
= mirror(x
-i
, w
-1)*stride
;
133 int x1
= mirror(x
+i
, w
-1)*stride
;
135 sumL
+= icoeff
[0][i
]*(srcL
[x0
] + srcL
[x1
]);
136 sumH
+= icoeff
[1][i
]*(srcH
[x0
] + srcH
[x1
]);
138 dst
[x
*stride
]= (sumL
+ sumH
)*0.5;
142 static inline void decompose2D(float *dstL
, float *dstH
, float *src
, int xstride
, int ystride
, int step
, int w
, int h
){
145 for(x
=0; x
<step
; x
++)
146 decompose(dstL
+ ystride
*y
+ xstride
*x
, dstH
+ ystride
*y
+ xstride
*x
, src
+ ystride
*y
+ xstride
*x
, step
*xstride
, (w
-x
+step
-1)/step
);
149 static inline void compose2D(float *dst
, float *srcL
, float *srcH
, int xstride
, int ystride
, int step
, int w
, int h
){
152 for(x
=0; x
<step
; x
++)
153 compose(dst
+ ystride
*y
+ xstride
*x
, srcL
+ ystride
*y
+ xstride
*x
, srcH
+ ystride
*y
+ xstride
*x
, step
*xstride
, (w
-x
+step
-1)/step
);
156 static void decompose2D2(float *dst
[4], float *src
, float *temp
[2], int stride
, int step
, int w
, int h
){
157 decompose2D(temp
[0], temp
[1], src
, 1, stride
, step
, w
, h
);
158 decompose2D( dst
[0], dst
[1], temp
[0], stride
, 1, step
, h
, w
);
159 decompose2D( dst
[2], dst
[3], temp
[1], stride
, 1, step
, h
, w
);
162 static void compose2D2(float *dst
, float *src
[4], float *temp
[2], int stride
, int step
, int w
, int h
){
163 compose2D(temp
[0], src
[0], src
[1], stride
, 1, step
, h
, w
);
164 compose2D(temp
[1], src
[2], src
[3], stride
, 1, step
, h
, w
);
165 compose2D(dst
, temp
[0], temp
[1], 1, stride
, step
, w
, h
);
168 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
){
171 double s
= p
->strength
[!is_luma
];
174 while(1<<depth
> width
|| 1<<depth
> height
)
177 for(y
=0; y
<height
; y
++)
178 for(x
=0; x
<width
; x
++)
179 p
->plane
[0][0][x
+ y
*p
->stride
]= src
[x
+ y
*src_stride
];
181 for(i
=0; i
<depth
; i
++){
182 decompose2D2(p
->plane
[i
+1], p
->plane
[i
][0], p
->plane
[0]+1,p
->stride
, 1<<i
, width
, height
);
184 for(i
=0; i
<depth
; i
++){
186 for(y
=0; y
<height
; y
++){
187 for(x
=0; x
<width
; x
++){
188 double v
= p
->plane
[i
+1][j
][x
+ y
*p
->stride
];
192 p
->plane
[i
+1][j
][x
+ y
*p
->stride
]= v
;
197 for(i
=depth
-1; i
>=0; i
--){
198 compose2D2(p
->plane
[i
][0], p
->plane
[i
+1], p
->plane
[0]+1, p
->stride
, 1<<i
, width
, height
);
201 for(y
=0; y
<height
; y
++)
202 for(x
=0; x
<width
; x
++){
203 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 :)
204 // double e= i - src[x + y*src_stride];
206 if((unsigned)i
> 255U) i
= ~(i
>>31);
207 dst
[x
+ y
*dst_stride
]= i
;
210 // printf("%f\n", sum/height/width);
213 static int config(struct vf_instance_s
* vf
, int width
, int height
, int d_width
, int d_height
, unsigned int flags
, unsigned int outfmt
){
214 int h
= (height
+15)&(~15);
217 vf
->priv
->stride
= (width
+15)&(~15);
219 for(i
=0; i
<=vf
->priv
->depth
; i
++)
220 vf
->priv
->plane
[i
][j
]= malloc(vf
->priv
->stride
*h
*sizeof(vf
->priv
->plane
[0][0][0]));
223 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
226 static void get_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
){
227 if(mpi
->flags
&MP_IMGFLAG_PRESERVE
) return; // don't change
228 // ok, we can do pp in-place (or pp disabled):
229 vf
->dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
230 mpi
->type
, mpi
->flags
| MP_IMGFLAG_READABLE
, mpi
->width
, mpi
->height
);
231 mpi
->planes
[0]=vf
->dmpi
->planes
[0];
232 mpi
->stride
[0]=vf
->dmpi
->stride
[0];
233 mpi
->width
=vf
->dmpi
->width
;
234 if(mpi
->flags
&MP_IMGFLAG_PLANAR
){
235 mpi
->planes
[1]=vf
->dmpi
->planes
[1];
236 mpi
->planes
[2]=vf
->dmpi
->planes
[2];
237 mpi
->stride
[1]=vf
->dmpi
->stride
[1];
238 mpi
->stride
[2]=vf
->dmpi
->stride
[2];
240 mpi
->flags
|=MP_IMGFLAG_DIRECT
;
243 static int put_image(struct vf_instance_s
* vf
, mp_image_t
*mpi
, double pts
){
246 if(!(mpi
->flags
&MP_IMGFLAG_DIRECT
)){
247 // no DR, so get a new image! hope we'll get DR buffer:
248 dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
250 MP_IMGFLAG_ACCEPT_STRIDE
|MP_IMGFLAG_PREFER_ALIGNED_STRIDE
,
251 mpi
->width
,mpi
->height
);
252 vf_clone_mpi_attributes(dmpi
, mpi
);
257 filter(vf
->priv
, dmpi
->planes
[0], mpi
->planes
[0], dmpi
->stride
[0], mpi
->stride
[0], mpi
->w
, mpi
->h
, 1);
258 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);
259 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);
261 return vf_next_put_image(vf
,dmpi
, pts
);
264 static void uninit(struct vf_instance_s
* vf
){
266 if(!vf
->priv
) return;
270 free(vf
->priv
->plane
[i
][j
]);
271 vf
->priv
->plane
[i
][j
]= NULL
;
279 //===========================================================================//
280 static int query_format(struct vf_instance_s
* vf
, unsigned int fmt
){
293 return vf_next_query_format(vf
,fmt
);
299 static int open(vf_instance_t
*vf
, char* args
){
301 vf
->put_image
=put_image
;
302 vf
->get_image
=get_image
;
303 vf
->query_format
=query_format
;
305 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
306 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
309 vf
->priv
->strength
[0]= 1.0;
310 vf
->priv
->strength
[1]= 1.0;
311 vf
->priv
->delta
= 1.0;
313 if (args
) sscanf(args
, "%d:%f:%f:%d:%f", &vf
->priv
->depth
,
314 &vf
->priv
->strength
[0],
315 &vf
->priv
->strength
[1],
322 const vf_info_t vf_info_ow
= {
323 "overcomplete wavelet denoiser",
325 "Michael Niedermayer",