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>
37 #include "img_format.h"
43 //===========================================================================//
45 typedef struct FilterParam
{
47 float preFilterRadius
;
50 struct SwsContext
*preFilterContext
;
51 uint8_t *preFilterBuf
;
56 int colorDiffCoeff
[512];
65 /***************************************************************************/
67 static int allocStuff(FilterParam
*f
, int width
, int height
){
68 int stride
= (width
+7)&~7;
72 f
->preFilterBuf
= av_malloc(stride
*height
);
73 f
->preFilterStride
= stride
;
75 vec
= sws_getGaussianVec(f
->preFilterRadius
, f
->quality
);
76 swsF
.lumH
= swsF
.lumV
= vec
;
77 swsF
.chrH
= swsF
.chrV
= NULL
;
78 f
->preFilterContext
= sws_getContext(
79 width
, height
, PIX_FMT_GRAY8
, width
, height
, PIX_FMT_GRAY8
, get_sws_cpuflags()|SWS_POINT
, &swsF
, NULL
, NULL
);
82 vec
= sws_getGaussianVec(f
->strength
, 5.0);
85 int index
= i
-256 + vec
->length
/2;
87 if(index
<0 || index
>=vec
->length
) d
= 0.0;
88 else d
= vec
->coeff
[index
];
90 f
->colorDiffCoeff
[i
]= (int)(d
/vec
->coeff
[vec
->length
/2]*(1<<12) + 0.5);
93 vec
= sws_getGaussianVec(f
->radius
, f
->quality
);
94 f
->distWidth
= vec
->length
;
95 f
->distStride
= (vec
->length
+7)&~7;
96 f
->distCoeff
= av_malloc(f
->distWidth
*f
->distStride
*sizeof(int32_t));
98 for(y
=0; y
<vec
->length
; y
++){
99 for(x
=0; x
<vec
->length
; x
++){
100 double d
= vec
->coeff
[x
] * vec
->coeff
[y
];
102 f
->distCoeff
[x
+ y
*f
->distStride
]= (int)(d
*(1<<10) + 0.5);
103 // if(y==vec->length/2)
104 // printf("%6d ", f->distCoeff[x + y*f->distStride]);
112 static int config(struct vf_instance
*vf
,
113 int width
, int height
, int d_width
, int d_height
,
114 unsigned int flags
, unsigned int outfmt
){
117 //__asm__ volatile("emms\n\t");
118 allocStuff(&vf
->priv
->luma
, width
, height
);
120 mp_get_chroma_shift(outfmt
, &sw
, &sh
, NULL
);
121 allocStuff(&vf
->priv
->chroma
, width
>>sw
, height
>>sh
);
123 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
126 static void freeBuffers(FilterParam
*f
){
127 if(f
->preFilterContext
) sws_freeContext(f
->preFilterContext
);
128 f
->preFilterContext
=NULL
;
130 av_free(f
->preFilterBuf
);
131 f
->preFilterBuf
=NULL
;
133 av_free(f
->distCoeff
);
137 static void uninit(struct vf_instance
*vf
){
138 if(!vf
->priv
) return;
140 freeBuffers(&vf
->priv
->luma
);
141 freeBuffers(&vf
->priv
->chroma
);
147 static inline void blur(uint8_t *dst
, uint8_t *src
, int w
, int h
, int dstStride
, int srcStride
, FilterParam
*fp
){
150 const int radius
= f
.distWidth
/2;
151 const uint8_t* const srcArray
[MP_MAX_PLANES
] = {src
};
152 uint8_t *dstArray
[MP_MAX_PLANES
]= {f
.preFilterBuf
};
153 int srcStrideArray
[MP_MAX_PLANES
]= {srcStride
};
154 int dstStrideArray
[MP_MAX_PLANES
]= {f
.preFilterStride
};
156 // f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
157 sws_scale(f
.preFilterContext
, srcArray
, srcStrideArray
, 0, h
, dstArray
, dstStrideArray
);
164 const int preVal
= f
.preFilterBuf
[x
+ y
*f
.preFilterStride
];
166 const int srcVal
= src
[x
+ y
*srcStride
];
168 dst
[x
+ y
*dstStride
]= srcVal
;
169 if(y
%32==0) dst
[x
+ y
*dstStride
]= 0;
173 if(x
>= radius
&& x
< w
- radius
){
174 for(dy
=0; dy
<radius
*2+1; dy
++){
176 int iy
= y
+dy
- radius
;
178 else if(iy
>=h
) iy
= h
+h
-iy
-1;
180 for(dx
=0; dx
<radius
*2+1; dx
++){
181 const int ix
= x
+dx
- radius
;
184 factor
= f
.colorDiffCoeff
[256+preVal
- f
.preFilterBuf
[ix
+ iy
*f
.preFilterStride
] ]
185 *f
.distCoeff
[dx
+ dy
*f
.distStride
];
186 sum
+= src
[ix
+ iy
*srcStride
] *factor
;
191 for(dy
=0; dy
<radius
*2+1; dy
++){
193 int iy
= y
+dy
- radius
;
195 else if(iy
>=h
) iy
= h
+h
-iy
-1;
197 for(dx
=0; dx
<radius
*2+1; dx
++){
198 int ix
= x
+dx
- radius
;
201 else if(ix
>=w
) ix
= w
+w
-ix
-1;
203 factor
= f
.colorDiffCoeff
[256+preVal
- f
.preFilterBuf
[ix
+ iy
*f
.preFilterStride
] ]
204 *f
.distCoeff
[dx
+ dy
*f
.distStride
];
205 sum
+= src
[ix
+ iy
*srcStride
] *factor
;
210 dst
[x
+ y
*dstStride
]= (sum
+ div
/2)/div
;
215 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
){
216 int cw
= mpi
->w
>> mpi
->chroma_x_shift
;
217 int ch
= mpi
->h
>> mpi
->chroma_y_shift
;
219 mp_image_t
*dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
220 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
223 assert(mpi
->flags
&MP_IMGFLAG_PLANAR
);
225 blur(dmpi
->planes
[0], mpi
->planes
[0], mpi
->w
,mpi
->h
, dmpi
->stride
[0], mpi
->stride
[0], &vf
->priv
->luma
);
226 blur(dmpi
->planes
[1], mpi
->planes
[1], cw
, ch
, dmpi
->stride
[1], mpi
->stride
[1], &vf
->priv
->chroma
);
227 blur(dmpi
->planes
[2], mpi
->planes
[2], cw
, ch
, dmpi
->stride
[2], mpi
->stride
[2], &vf
->priv
->chroma
);
229 return vf_next_put_image(vf
,dmpi
, pts
);
232 //===========================================================================//
234 static int query_format(struct vf_instance
*vf
, unsigned int fmt
){
244 return vf_next_query_format(vf
, fmt
);
249 static int vf_open(vf_instance_t
*vf
, char *args
){
253 vf
->put_image
=put_image
;
254 // vf->get_image=get_image;
255 vf
->query_format
=query_format
;
257 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
258 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
260 if(args
==NULL
) return 0;
262 e
=sscanf(args
, "%f:%f:%f:%f:%f:%f",
263 &vf
->priv
->luma
.radius
,
264 &vf
->priv
->luma
.preFilterRadius
,
265 &vf
->priv
->luma
.strength
,
266 &vf
->priv
->chroma
.radius
,
267 &vf
->priv
->chroma
.preFilterRadius
,
268 &vf
->priv
->chroma
.strength
271 vf
->priv
->luma
.quality
= vf
->priv
->chroma
.quality
= 3.0;
274 vf
->priv
->chroma
.radius
= vf
->priv
->luma
.radius
;
275 vf
->priv
->chroma
.preFilterRadius
= vf
->priv
->luma
.preFilterRadius
;
276 vf
->priv
->chroma
.strength
= vf
->priv
->luma
.strength
;
280 // if(vf->priv->luma.radius < 0) return 0;
281 // if(vf->priv->chroma.radius < 0) return 0;
286 const vf_info_t vf_info_sab
= {
287 "shape adaptive blur",
289 "Michael Niedermayer",
295 //===========================================================================//