00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00030 #include <stdio.h>
00031 #include <string.h>
00032 #include <inttypes.h>
00033 #include <math.h>
00034
00035 #include "mp_msg.h"
00036 #include "img_format.h"
00037 #include "mp_image.h"
00038 #include "vf.h"
00039
00040
00041 static const uint8_t __attribute__((aligned(8))) dither[8][8]={
00042 { 0, 48, 12, 60, 3, 51, 15, 63, },
00043 { 32, 16, 44, 28, 35, 19, 47, 31, },
00044 { 8, 56, 4, 52, 11, 59, 7, 55, },
00045 { 40, 24, 36, 20, 43, 27, 39, 23, },
00046 { 2, 50, 14, 62, 1, 49, 13, 61, },
00047 { 34, 18, 46, 30, 33, 17, 45, 29, },
00048 { 10, 58, 6, 54, 9, 57, 5, 53, },
00049 { 42, 26, 38, 22, 41, 25, 37, 21, },
00050 };
00051
00052
00053 struct vf_priv_s {
00054 float strength[2];
00055 float delta;
00056 int mode;
00057 int depth;
00058 float *plane[16][4];
00059 int stride;
00060 };
00061
00062 #define S 1.41421356237 //sqrt(2)
00063
00064 static const double coeff[2][5]={
00065 {
00066 0.6029490182363579 *S,
00067 0.2668641184428723 *S,
00068 -0.07822326652898785 *S,
00069 -0.01686411844287495 *S,
00070 0.02674875741080976 *S
00071 },{
00072 1.115087052456994 /S,
00073 -0.5912717631142470 /S,
00074 -0.05754352622849957 /S,
00075 0.09127176311424948 /S
00076 }
00077 };
00078
00079 static const double icoeff[2][5]={
00080 {
00081 1.115087052456994 /S,
00082 0.5912717631142470 /S,
00083 -0.05754352622849957 /S,
00084 -0.09127176311424948 /S
00085 },{
00086 0.6029490182363579 *S,
00087 -0.2668641184428723 *S,
00088 -0.07822326652898785 *S,
00089 0.01686411844287495 *S,
00090 0.02674875741080976 *S
00091 }
00092 };
00093 #undef S
00094
00095 static inline int mirror(int x, int w){
00096 while((unsigned)x > (unsigned)w){
00097 x=-x;
00098 if(x<0) x+= 2*w;
00099 }
00100 return x;
00101 }
00102
00103 static inline void decompose(float *dstL, float *dstH, float *src, int stride, int w){
00104 int x, i;
00105 for(x=0; x<w; x++){
00106 double sumL= src[x*stride] * coeff[0][0];
00107 double sumH= src[x*stride] * coeff[1][0];
00108 for(i=1; i<=4; i++){
00109 double s= (src[mirror(x-i, w-1)*stride] + src[mirror(x+i, w-1)*stride]);
00110
00111 sumL+= coeff[0][i]*s;
00112 sumH+= coeff[1][i]*s;
00113 }
00114 dstL[x*stride]= sumL;
00115 dstH[x*stride]= sumH;
00116 }
00117 }
00118
00119 static inline void compose(float *dst, float *srcL, float *srcH, int stride, int w){
00120 int x, i;
00121 for(x=0; x<w; x++){
00122 double sumL= srcL[x*stride] * icoeff[0][0];
00123 double sumH= srcH[x*stride] * icoeff[1][0];
00124 for(i=1; i<=4; i++){
00125 int x0= mirror(x-i, w-1)*stride;
00126 int x1= mirror(x+i, w-1)*stride;
00127
00128 sumL+= icoeff[0][i]*(srcL[x0] + srcL[x1]);
00129 sumH+= icoeff[1][i]*(srcH[x0] + srcH[x1]);
00130 }
00131 dst[x*stride]= (sumL + sumH)*0.5;
00132 }
00133 }
00134
00135 static inline void decompose2D(float *dstL, float *dstH, float *src, int xstride, int ystride, int step, int w, int h){
00136 int y, x;
00137 for(y=0; y<h; y++)
00138 for(x=0; x<step; x++)
00139 decompose(dstL + ystride*y + xstride*x, dstH + ystride*y + xstride*x, src + ystride*y + xstride*x, step*xstride, (w-x+step-1)/step);
00140 }
00141
00142 static inline void compose2D(float *dst, float *srcL, float *srcH, int xstride, int ystride, int step, int w, int h){
00143 int y, x;
00144 for(y=0; y<h; y++)
00145 for(x=0; x<step; x++)
00146 compose(dst + ystride*y + xstride*x, srcL + ystride*y + xstride*x, srcH + ystride*y + xstride*x, step*xstride, (w-x+step-1)/step);
00147 }
00148
00149 static void decompose2D2(float *dst[4], float *src, float *temp[2], int stride, int step, int w, int h){
00150 decompose2D(temp[0], temp[1], src , 1, stride, step , w, h);
00151 decompose2D( dst[0], dst[1], temp[0], stride, 1, step , h, w);
00152 decompose2D( dst[2], dst[3], temp[1], stride, 1, step , h, w);
00153 }
00154
00155 static void compose2D2(float *dst, float *src[4], float *temp[2], int stride, int step, int w, int h){
00156 compose2D(temp[0], src[0], src[1], stride, 1, step , h, w);
00157 compose2D(temp[1], src[2], src[3], stride, 1, step , h, w);
00158 compose2D(dst , temp[0], temp[1], 1, stride, step , w, h);
00159 }
00160
00161 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){
00162 int x,y, i, j;
00163
00164 double s= p->strength[!is_luma];
00165 int depth= p->depth;
00166
00167 while(1<<depth > width || 1<<depth > height)
00168 depth--;
00169
00170 for(y=0; y<height; y++)
00171 for(x=0; x<width; x++)
00172 p->plane[0][0][x + y*p->stride]= src[x + y*src_stride];
00173
00174 for(i=0; i<depth; i++){
00175 decompose2D2(p->plane[i+1], p->plane[i][0], p->plane[0]+1,p->stride, 1<<i, width, height);
00176 }
00177 for(i=0; i<depth; i++){
00178 for(j=1; j<4; j++){
00179 for(y=0; y<height; y++){
00180 for(x=0; x<width; x++){
00181 double v= p->plane[i+1][j][x + y*p->stride];
00182 if (v> s) v-=s;
00183 else if(v<-s) v+=s;
00184 else v =0;
00185 p->plane[i+1][j][x + y*p->stride]= v;
00186 }
00187 }
00188 }
00189 }
00190 for(i=depth-1; i>=0; i--){
00191 compose2D2(p->plane[i][0], p->plane[i+1], p->plane[0]+1, p->stride, 1<<i, width, height);
00192 }
00193
00194 for(y=0; y<height; y++)
00195 for(x=0; x<width; x++){
00196 i= p->plane[0][0][x + y*p->stride] + dither[x&7][y&7]*(1.0/64) + 1.0/128;
00197
00198
00199 if((unsigned)i > 255U) i= ~(i>>31);
00200 dst[x + y*dst_stride]= i;
00201 }
00202
00203
00204 }
00205
00206 static int config(struct vf_instance *vf, int width, int height, int d_width, int d_height, unsigned int flags, unsigned int outfmt){
00207 int h= (height+15)&(~15);
00208 int i,j;
00209
00210 vf->priv->stride= (width+15)&(~15);
00211 for(j=0; j<4; j++){
00212 for(i=0; i<=vf->priv->depth; i++)
00213 vf->priv->plane[i][j]= malloc(vf->priv->stride*h*sizeof(vf->priv->plane[0][0][0]));
00214 }
00215
00216 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
00217 }
00218
00219 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
00220 if(mpi->flags&MP_IMGFLAG_PRESERVE) return;
00221
00222 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
00223 mpi->type, mpi->flags | MP_IMGFLAG_READABLE, mpi->width, mpi->height);
00224 mpi->planes[0]=vf->dmpi->planes[0];
00225 mpi->stride[0]=vf->dmpi->stride[0];
00226 mpi->width=vf->dmpi->width;
00227 if(mpi->flags&MP_IMGFLAG_PLANAR){
00228 mpi->planes[1]=vf->dmpi->planes[1];
00229 mpi->planes[2]=vf->dmpi->planes[2];
00230 mpi->stride[1]=vf->dmpi->stride[1];
00231 mpi->stride[2]=vf->dmpi->stride[2];
00232 }
00233 mpi->flags|=MP_IMGFLAG_DIRECT;
00234 }
00235
00236 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
00237 mp_image_t *dmpi;
00238
00239 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
00240
00241 dmpi=vf_get_image(vf->next,mpi->imgfmt,
00242 MP_IMGTYPE_TEMP,
00243 MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
00244 mpi->width,mpi->height);
00245 vf_clone_mpi_attributes(dmpi, mpi);
00246 }else{
00247 dmpi=vf->dmpi;
00248 }
00249
00250 filter(vf->priv, dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, 1);
00251 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);
00252 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);
00253
00254 return vf_next_put_image(vf,dmpi, pts);
00255 }
00256
00257 static void uninit(struct vf_instance *vf){
00258 int i,j;
00259 if(!vf->priv) return;
00260
00261 for(j=0; j<4; j++){
00262 for(i=0; i<16; i++){
00263 free(vf->priv->plane[i][j]);
00264 vf->priv->plane[i][j]= NULL;
00265 }
00266 }
00267
00268 free(vf->priv);
00269 vf->priv=NULL;
00270 }
00271
00272
00273 static int query_format(struct vf_instance *vf, unsigned int fmt){
00274 switch(fmt){
00275 case IMGFMT_YVU9:
00276 case IMGFMT_IF09:
00277 case IMGFMT_YV12:
00278 case IMGFMT_I420:
00279 case IMGFMT_IYUV:
00280 case IMGFMT_CLPL:
00281 case IMGFMT_Y800:
00282 case IMGFMT_Y8:
00283 case IMGFMT_444P:
00284 case IMGFMT_422P:
00285 case IMGFMT_411P:
00286 return vf_next_query_format(vf,fmt);
00287 }
00288 return 0;
00289 }
00290
00291
00292 static int vf_open(vf_instance_t *vf, char *args){
00293 vf->config=config;
00294 vf->put_image=put_image;
00295 vf->get_image=get_image;
00296 vf->query_format=query_format;
00297 vf->uninit=uninit;
00298 vf->priv=malloc(sizeof(struct vf_priv_s));
00299 memset(vf->priv, 0, sizeof(struct vf_priv_s));
00300
00301 vf->priv->depth= 8;
00302 vf->priv->strength[0]= 1.0;
00303 vf->priv->strength[1]= 1.0;
00304 vf->priv->delta= 1.0;
00305
00306 if (args) sscanf(args, "%d:%f:%f:%d:%f", &vf->priv->depth,
00307 &vf->priv->strength[0],
00308 &vf->priv->strength[1],
00309 &vf->priv->mode,
00310 &vf->priv->delta);
00311
00312 return 1;
00313 }
00314
00315 const vf_info_t vf_info_ow = {
00316 "overcomplete wavelet denoiser",
00317 "ow",
00318 "Michael Niedermayer",
00319 "",
00320 vf_open,
00321 NULL
00322 };