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.
34 #include "libavutil/avutil.h"
35 #include "img_format.h"
38 #include "libswscale/swscale.h"
41 //===========================================================================//
43 typedef struct FilterParam
{
48 struct SwsContext
*filterContext
;
57 /***************************************************************************/
59 //FIXME stupid code duplication
60 static void getSubSampleFactors(int *h
, int *v
, int format
){
86 static int allocStuff(FilterParam
*f
, int width
, int height
){
90 vec
= sws_getGaussianVec(f
->radius
, f
->quality
);
91 sws_scaleVec(vec
, f
->strength
);
92 vec
->coeff
[vec
->length
/2]+= 1.0 - f
->strength
;
93 swsF
.lumH
= swsF
.lumV
= vec
;
94 swsF
.chrH
= swsF
.chrV
= NULL
;
95 f
->filterContext
= sws_getContext(
96 width
, height
, PIX_FMT_GRAY8
, width
, height
, PIX_FMT_GRAY8
, get_sws_cpuflags(), &swsF
, NULL
, NULL
);
103 static int config(struct vf_instance
* vf
,
104 int width
, int height
, int d_width
, int d_height
,
105 unsigned int flags
, unsigned int outfmt
){
109 allocStuff(&vf
->priv
->luma
, width
, height
);
111 getSubSampleFactors(&sw
, &sh
, outfmt
);
112 allocStuff(&vf
->priv
->chroma
, width
>>sw
, height
>>sh
);
114 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
117 static void freeBuffers(FilterParam
*f
){
118 if(f
->filterContext
) sws_freeContext(f
->filterContext
);
119 f
->filterContext
=NULL
;
122 static void uninit(struct vf_instance
* vf
){
123 if(!vf
->priv
) return;
125 freeBuffers(&vf
->priv
->luma
);
126 freeBuffers(&vf
->priv
->chroma
);
132 static inline void blur(uint8_t *dst
, uint8_t *src
, int w
, int h
, int dstStride
, int srcStride
, FilterParam
*fp
){
135 uint8_t *srcArray
[3]= {src
, NULL
, NULL
};
136 uint8_t *dstArray
[3]= {dst
, NULL
, NULL
};
137 int srcStrideArray
[3]= {srcStride
, 0, 0};
138 int dstStrideArray
[3]= {dstStride
, 0, 0};
140 sws_scale(f
.filterContext
, srcArray
, srcStrideArray
, 0, h
, dstArray
, dstStrideArray
);
145 const int orig
= src
[x
+ y
*srcStride
];
146 const int filtered
= dst
[x
+ y
*dstStride
];
147 const int diff
= orig
- filtered
;
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
;
156 if(-diff
> 2*f
.threshold
){
157 dst
[x
+ y
*dstStride
]= orig
;
158 }else if(-diff
> f
.threshold
){
159 dst
[x
+ y
*dstStride
]= filtered
+ diff
+ f
.threshold
;
164 }else if(f
.threshold
< 0){
167 const int orig
= src
[x
+ y
*srcStride
];
168 const int filtered
= dst
[x
+ y
*dstStride
];
169 const int diff
= orig
- filtered
;
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
;
178 if(diff
< 2*f
.threshold
){
179 }else if(diff
< f
.threshold
){
180 dst
[x
+ y
*dstStride
]= orig
- diff
+ f
.threshold
;
182 dst
[x
+ y
*dstStride
]= orig
;
189 static int put_image(struct vf_instance
* vf
, mp_image_t
*mpi
, double pts
){
190 int cw
= mpi
->w
>> mpi
->chroma_x_shift
;
191 int ch
= mpi
->h
>> mpi
->chroma_y_shift
;
192 FilterParam
*f
= &vf
->priv
;
194 mp_image_t
*dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
195 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
|
196 (f
->threshold
) ? MP_IMGFLAG_READABLE
: 0,
199 assert(mpi
->flags
&MP_IMGFLAG_PLANAR
);
201 blur(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
,mpi
->h
, dmpi
->stride
[0], mpi
->stride
[0], &vf
->priv
->luma
);
202 blur(dmpi
->planes
[1], mpi
->planes
[1], cw
, ch
, dmpi
->stride
[1], mpi
->stride
[1], &vf
->priv
->chroma
);
203 blur(dmpi
->planes
[2], mpi
->planes
[2], cw
, ch
, dmpi
->stride
[2], mpi
->stride
[2], &vf
->priv
->chroma
);
205 return vf_next_put_image(vf
,dmpi
, pts
);
208 //===========================================================================//
210 static int query_format(struct vf_instance
* vf
, unsigned int fmt
){
220 return vf_next_query_format(vf
, fmt
);
225 static int open(vf_instance_t
*vf
, char* args
){
229 vf
->put_image
=put_image
;
230 // vf->get_image=get_image;
231 vf
->query_format
=query_format
;
233 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
234 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
236 if(args
==NULL
) return 0;
238 e
=sscanf(args
, "%f:%f:%d:%f:%f:%d",
239 &vf
->priv
->luma
.radius
,
240 &vf
->priv
->luma
.strength
,
241 &vf
->priv
->luma
.threshold
,
242 &vf
->priv
->chroma
.radius
,
243 &vf
->priv
->chroma
.strength
,
244 &vf
->priv
->chroma
.threshold
247 vf
->priv
->luma
.quality
= vf
->priv
->chroma
.quality
= 3.0;
250 vf
->priv
->chroma
.radius
= vf
->priv
->luma
.radius
;
251 vf
->priv
->chroma
.strength
= vf
->priv
->luma
.strength
;
252 vf
->priv
->chroma
.threshold
= vf
->priv
->luma
.threshold
;
259 const vf_info_t vf_info_smartblur
= {
262 "Michael Niedermayer",
268 //===========================================================================//