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.
27 #include <libavutil/mem.h>
28 #include <libswscale/swscale.h>
33 #include "img_format.h"
39 //===========================================================================//
41 typedef struct FilterParam
{
43 float preFilterRadius
;
46 struct SwsContext
*preFilterContext
;
47 uint8_t *preFilterBuf
;
52 int colorDiffCoeff
[512];
61 /***************************************************************************/
63 static int allocStuff(FilterParam
*f
, int width
, int height
){
64 int stride
= (width
+7)&~7;
68 f
->preFilterBuf
= av_malloc(stride
*height
);
69 f
->preFilterStride
= stride
;
71 vec
= sws_getGaussianVec(f
->preFilterRadius
, f
->quality
);
72 swsF
.lumH
= swsF
.lumV
= vec
;
73 swsF
.chrH
= swsF
.chrV
= NULL
;
74 f
->preFilterContext
= sws_getContext(
75 width
, height
, PIX_FMT_GRAY8
, width
, height
, PIX_FMT_GRAY8
, get_sws_cpuflags()|SWS_POINT
, &swsF
, NULL
, NULL
);
78 vec
= sws_getGaussianVec(f
->strength
, 5.0);
81 int index
= i
-256 + vec
->length
/2;
83 if(index
<0 || index
>=vec
->length
) d
= 0.0;
84 else d
= vec
->coeff
[index
];
86 f
->colorDiffCoeff
[i
]= (int)(d
/vec
->coeff
[vec
->length
/2]*(1<<12) + 0.5);
89 vec
= sws_getGaussianVec(f
->radius
, f
->quality
);
90 f
->distWidth
= vec
->length
;
91 f
->distStride
= (vec
->length
+7)&~7;
92 f
->distCoeff
= av_malloc(f
->distWidth
*f
->distStride
*sizeof(int32_t));
94 for(y
=0; y
<vec
->length
; y
++){
95 for(x
=0; x
<vec
->length
; x
++){
96 double d
= vec
->coeff
[x
] * vec
->coeff
[y
];
98 f
->distCoeff
[x
+ y
*f
->distStride
]= (int)(d
*(1<<10) + 0.5);
99 // if(y==vec->length/2)
100 // printf("%6d ", f->distCoeff[x + y*f->distStride]);
108 static int config(struct vf_instance
*vf
,
109 int width
, int height
, int d_width
, int d_height
,
110 unsigned int flags
, unsigned int outfmt
){
113 //__asm__ volatile("emms\n\t");
114 allocStuff(&vf
->priv
->luma
, width
, height
);
116 mp_get_chroma_shift(outfmt
, &sw
, &sh
, NULL
);
117 allocStuff(&vf
->priv
->chroma
, width
>>sw
, height
>>sh
);
119 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
122 static void freeBuffers(FilterParam
*f
){
123 if(f
->preFilterContext
) sws_freeContext(f
->preFilterContext
);
124 f
->preFilterContext
=NULL
;
126 av_free(f
->preFilterBuf
);
127 f
->preFilterBuf
=NULL
;
129 av_free(f
->distCoeff
);
133 static void uninit(struct vf_instance
*vf
){
134 if(!vf
->priv
) return;
136 freeBuffers(&vf
->priv
->luma
);
137 freeBuffers(&vf
->priv
->chroma
);
143 static inline void blur(uint8_t *dst
, uint8_t *src
, int w
, int h
, int dstStride
, int srcStride
, FilterParam
*fp
){
146 const int radius
= f
.distWidth
/2;
147 const uint8_t* const srcArray
[MP_MAX_PLANES
] = {src
};
148 uint8_t *dstArray
[MP_MAX_PLANES
]= {f
.preFilterBuf
};
149 int srcStrideArray
[MP_MAX_PLANES
]= {srcStride
};
150 int dstStrideArray
[MP_MAX_PLANES
]= {f
.preFilterStride
};
152 // f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
153 sws_scale(f
.preFilterContext
, srcArray
, srcStrideArray
, 0, h
, dstArray
, dstStrideArray
);
160 const int preVal
= f
.preFilterBuf
[x
+ y
*f
.preFilterStride
];
162 const int srcVal
= src
[x
+ y
*srcStride
];
164 dst
[x
+ y
*dstStride
]= srcVal
;
165 if(y
%32==0) dst
[x
+ y
*dstStride
]= 0;
169 if(x
>= radius
&& x
< w
- radius
){
170 for(dy
=0; dy
<radius
*2+1; dy
++){
172 int iy
= y
+dy
- radius
;
174 else if(iy
>=h
) iy
= h
+h
-iy
-1;
176 for(dx
=0; dx
<radius
*2+1; dx
++){
177 const int ix
= x
+dx
- radius
;
180 factor
= f
.colorDiffCoeff
[256+preVal
- f
.preFilterBuf
[ix
+ iy
*f
.preFilterStride
] ]
181 *f
.distCoeff
[dx
+ dy
*f
.distStride
];
182 sum
+= src
[ix
+ iy
*srcStride
] *factor
;
187 for(dy
=0; dy
<radius
*2+1; dy
++){
189 int iy
= y
+dy
- radius
;
191 else if(iy
>=h
) iy
= h
+h
-iy
-1;
193 for(dx
=0; dx
<radius
*2+1; dx
++){
194 int ix
= x
+dx
- radius
;
197 else if(ix
>=w
) ix
= w
+w
-ix
-1;
199 factor
= f
.colorDiffCoeff
[256+preVal
- f
.preFilterBuf
[ix
+ iy
*f
.preFilterStride
] ]
200 *f
.distCoeff
[dx
+ dy
*f
.distStride
];
201 sum
+= src
[ix
+ iy
*srcStride
] *factor
;
206 dst
[x
+ y
*dstStride
]= (sum
+ div
/2)/div
;
211 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
){
212 int cw
= mpi
->w
>> mpi
->chroma_x_shift
;
213 int ch
= mpi
->h
>> mpi
->chroma_y_shift
;
215 mp_image_t
*dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
216 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
219 assert(mpi
->flags
&MP_IMGFLAG_PLANAR
);
221 blur(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
,mpi
->h
, dmpi
->stride
[0], mpi
->stride
[0], &vf
->priv
->luma
);
222 blur(dmpi
->planes
[1], mpi
->planes
[1], cw
, ch
, dmpi
->stride
[1], mpi
->stride
[1], &vf
->priv
->chroma
);
223 blur(dmpi
->planes
[2], mpi
->planes
[2], cw
, ch
, dmpi
->stride
[2], mpi
->stride
[2], &vf
->priv
->chroma
);
225 return vf_next_put_image(vf
,dmpi
, pts
);
228 //===========================================================================//
230 static int query_format(struct vf_instance
*vf
, unsigned int fmt
){
240 return vf_next_query_format(vf
, fmt
);
245 static int vf_open(vf_instance_t
*vf
, char *args
){
249 vf
->put_image
=put_image
;
250 // vf->get_image=get_image;
251 vf
->query_format
=query_format
;
253 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
254 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
256 if(args
==NULL
) return 0;
258 e
=sscanf(args
, "%f:%f:%f:%f:%f:%f",
259 &vf
->priv
->luma
.radius
,
260 &vf
->priv
->luma
.preFilterRadius
,
261 &vf
->priv
->luma
.strength
,
262 &vf
->priv
->chroma
.radius
,
263 &vf
->priv
->chroma
.preFilterRadius
,
264 &vf
->priv
->chroma
.strength
267 vf
->priv
->luma
.quality
= vf
->priv
->chroma
.quality
= 3.0;
270 vf
->priv
->chroma
.radius
= vf
->priv
->luma
.radius
;
271 vf
->priv
->chroma
.preFilterRadius
= vf
->priv
->luma
.preFilterRadius
;
272 vf
->priv
->chroma
.strength
= vf
->priv
->luma
.strength
;
276 // if(vf->priv->luma.radius < 0) return 0;
277 // if(vf->priv->chroma.radius < 0) return 0;
282 const vf_info_t vf_info_sab
= {
283 "shape adaptive blur",
285 "Michael Niedermayer",
291 //===========================================================================//