2 * Copyright (C) 2002 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.
28 #include "libavutil/avutil.h"
29 #include "img_format.h"
32 #include "libswscale/swscale.h"
35 //===========================================================================//
37 typedef struct FilterParam
{
42 struct SwsContext
*filterContext
;
51 /***************************************************************************/
53 //FIXME stupid code duplication
54 static void getSubSampleFactors(int *h
, int *v
, int format
){
80 static int allocStuff(FilterParam
*f
, int width
, int height
){
84 vec
= sws_getGaussianVec(f
->radius
, f
->quality
);
85 sws_scaleVec(vec
, f
->strength
);
86 vec
->coeff
[vec
->length
/2]+= 1.0 - f
->strength
;
87 swsF
.lumH
= swsF
.lumV
= vec
;
88 swsF
.chrH
= swsF
.chrV
= NULL
;
89 f
->filterContext
= sws_getContext(
90 width
, height
, PIX_FMT_GRAY8
, width
, height
, PIX_FMT_GRAY8
, SWS_BICUBIC
| get_sws_cpuflags(), &swsF
, NULL
, NULL
);
97 static int config(struct vf_instance
*vf
,
98 int width
, int height
, int d_width
, int d_height
,
99 unsigned int flags
, unsigned int outfmt
){
103 allocStuff(&vf
->priv
->luma
, width
, height
);
105 getSubSampleFactors(&sw
, &sh
, outfmt
);
106 allocStuff(&vf
->priv
->chroma
, width
>>sw
, height
>>sh
);
108 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
111 static void freeBuffers(FilterParam
*f
){
112 if(f
->filterContext
) sws_freeContext(f
->filterContext
);
113 f
->filterContext
=NULL
;
116 static void uninit(struct vf_instance
*vf
){
117 if(!vf
->priv
) return;
119 freeBuffers(&vf
->priv
->luma
);
120 freeBuffers(&vf
->priv
->chroma
);
126 static inline void blur(uint8_t *dst
, uint8_t *src
, int w
, int h
, int dstStride
, int srcStride
, FilterParam
*fp
){
129 uint8_t *srcArray
[MP_MAX_PLANES
]= {src
};
130 uint8_t *dstArray
[MP_MAX_PLANES
]= {dst
};
131 int srcStrideArray
[MP_MAX_PLANES
]= {srcStride
};
132 int dstStrideArray
[MP_MAX_PLANES
]= {dstStride
};
134 sws_scale(f
.filterContext
, srcArray
, srcStrideArray
, 0, h
, dstArray
, dstStrideArray
);
139 const int orig
= src
[x
+ y
*srcStride
];
140 const int filtered
= dst
[x
+ y
*dstStride
];
141 const int diff
= orig
- filtered
;
144 if(diff
> 2*f
.threshold
){
145 dst
[x
+ y
*dstStride
]= orig
;
146 }else if(diff
> f
.threshold
){
147 dst
[x
+ y
*dstStride
]= filtered
+ diff
- f
.threshold
;
150 if(-diff
> 2*f
.threshold
){
151 dst
[x
+ y
*dstStride
]= orig
;
152 }else if(-diff
> f
.threshold
){
153 dst
[x
+ y
*dstStride
]= filtered
+ diff
+ f
.threshold
;
158 }else if(f
.threshold
< 0){
161 const int orig
= src
[x
+ y
*srcStride
];
162 const int filtered
= dst
[x
+ y
*dstStride
];
163 const int diff
= orig
- filtered
;
166 if(diff
> -2*f
.threshold
){
167 }else if(diff
> -f
.threshold
){
168 dst
[x
+ y
*dstStride
]= orig
- diff
- f
.threshold
;
170 dst
[x
+ y
*dstStride
]= orig
;
172 if(diff
< 2*f
.threshold
){
173 }else if(diff
< f
.threshold
){
174 dst
[x
+ y
*dstStride
]= orig
- diff
+ f
.threshold
;
176 dst
[x
+ y
*dstStride
]= orig
;
183 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
){
184 int cw
= mpi
->w
>> mpi
->chroma_x_shift
;
185 int ch
= mpi
->h
>> mpi
->chroma_y_shift
;
186 FilterParam
*f
= &vf
->priv
;
188 mp_image_t
*dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
189 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
|
190 (f
->threshold
) ? MP_IMGFLAG_READABLE
: 0,
193 assert(mpi
->flags
&MP_IMGFLAG_PLANAR
);
195 blur(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
,mpi
->h
, dmpi
->stride
[0], mpi
->stride
[0], &vf
->priv
->luma
);
196 blur(dmpi
->planes
[1], mpi
->planes
[1], cw
, ch
, dmpi
->stride
[1], mpi
->stride
[1], &vf
->priv
->chroma
);
197 blur(dmpi
->planes
[2], mpi
->planes
[2], cw
, ch
, dmpi
->stride
[2], mpi
->stride
[2], &vf
->priv
->chroma
);
199 return vf_next_put_image(vf
,dmpi
, pts
);
202 //===========================================================================//
204 static int query_format(struct vf_instance
*vf
, unsigned int fmt
){
214 return vf_next_query_format(vf
, fmt
);
219 static int vf_open(vf_instance_t
*vf
, char *args
){
223 vf
->put_image
=put_image
;
224 // vf->get_image=get_image;
225 vf
->query_format
=query_format
;
227 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
228 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
230 if(args
==NULL
) return 0;
232 e
=sscanf(args
, "%f:%f:%d:%f:%f:%d",
233 &vf
->priv
->luma
.radius
,
234 &vf
->priv
->luma
.strength
,
235 &vf
->priv
->luma
.threshold
,
236 &vf
->priv
->chroma
.radius
,
237 &vf
->priv
->chroma
.strength
,
238 &vf
->priv
->chroma
.threshold
241 vf
->priv
->luma
.quality
= vf
->priv
->chroma
.quality
= 3.0;
244 vf
->priv
->chroma
.radius
= vf
->priv
->luma
.radius
;
245 vf
->priv
->chroma
.strength
= vf
->priv
->luma
.strength
;
246 vf
->priv
->chroma
.threshold
= vf
->priv
->luma
.threshold
;
253 const vf_info_t vf_info_smartblur
= {
256 "Michael Niedermayer",
262 //===========================================================================//