Mercurial > mplayer.hg
annotate libmpcodecs/vf_sab.c @ 9720:5cd40fe6ba7e
Updated XviD CVS instructions by Dmitry Baryshkov <lumag@qnc.ru>.
correct spelling of XviD + small corrections
author | diego |
---|---|
date | Sat, 29 Mar 2003 01:45:21 +0000 |
parents | e9a2af584986 |
children | 3914afe5c0a7 |
rev | line source |
---|---|
8100 | 1 /* |
2 Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at> | |
3 | |
4 This program is free software; you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 2 of the License, or | |
7 (at your option) any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
15 along with this program; if not, write to the Free Software | |
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 */ | |
18 | |
19 #include <stdio.h> | |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 #include <inttypes.h> | |
23 #include <assert.h> | |
24 | |
25 #include "../config.h" | |
26 #include "../mp_msg.h" | |
27 | |
28 #ifdef HAVE_MALLOC_H | |
29 #include <malloc.h> | |
30 #endif | |
31 | |
32 #include "img_format.h" | |
33 #include "mp_image.h" | |
34 #include "vf.h" | |
35 #include "../libvo/fastmemcpy.h" | |
36 #include "../postproc/swscale.h" | |
37 | |
38 | |
39 //===========================================================================// | |
40 | |
41 typedef struct FilterParam{ | |
42 float radius; | |
43 float preFilterRadius; | |
44 float strength; | |
45 float quality; | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
46 struct SwsContext *preFilterContext; |
8100 | 47 uint8_t *preFilterBuf; |
48 int preFilterStride; | |
49 int distWidth; | |
50 int distStride; | |
51 int *distCoeff; | |
52 int colorDiffCoeff[512]; | |
53 }FilterParam; | |
54 | |
55 struct vf_priv_s { | |
56 FilterParam luma; | |
57 FilterParam chroma; | |
58 }; | |
59 | |
60 | |
61 /***************************************************************************/ | |
62 | |
63 //FIXME stupid code duplication | |
64 static void getSubSampleFactors(int *h, int *v, int format){ | |
65 switch(format){ | |
66 case IMGFMT_YV12: | |
67 case IMGFMT_I420: | |
68 *h=1; | |
69 *v=1; | |
70 break; | |
71 case IMGFMT_YVU9: | |
72 *h=2; | |
73 *v=2; | |
74 break; | |
75 case IMGFMT_444P: | |
76 *h=0; | |
77 *v=0; | |
78 break; | |
79 case IMGFMT_422P: | |
80 *h=1; | |
81 *v=0; | |
82 break; | |
83 case IMGFMT_411P: | |
84 *h=2; | |
85 *v=0; | |
86 break; | |
87 } | |
88 } | |
89 | |
90 static int allocStuff(FilterParam *f, int width, int height){ | |
91 int stride= (width+7)&~7; | |
92 SwsVector *vec; | |
93 SwsFilter swsF; | |
94 int i,x,y; | |
95 f->preFilterBuf= (uint8_t*)memalign(8, stride*height); | |
96 f->preFilterStride= stride; | |
97 | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
98 vec = sws_getGaussianVec(f->preFilterRadius, f->quality); |
8100 | 99 swsF.lumH= swsF.lumV= vec; |
100 swsF.chrH= swsF.chrV= NULL; | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
101 f->preFilterContext= sws_getContext( |
8100 | 102 width, height, IMGFMT_Y8, width, height, IMGFMT_Y8, 0, &swsF, NULL); |
103 | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
104 sws_freeVec(vec); |
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
105 vec = sws_getGaussianVec(f->strength, 5.0); |
8100 | 106 for(i=0; i<512; i++){ |
107 double d; | |
108 int index= i-256 + vec->length/2; | |
109 | |
110 if(index<0 || index>=vec->length) d= 0.0; | |
111 else d= vec->coeff[index]; | |
112 | |
113 f->colorDiffCoeff[i]= (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5); | |
114 } | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
115 sws_freeVec(vec); |
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
116 vec = sws_getGaussianVec(f->radius, f->quality); |
8100 | 117 f->distWidth= vec->length; |
118 f->distStride= (vec->length+7)&~7; | |
119 f->distCoeff= (int32_t*)memalign(8, f->distWidth*f->distStride*sizeof(int32_t)); | |
120 | |
121 for(y=0; y<vec->length; y++){ | |
122 for(x=0; x<vec->length; x++){ | |
123 double d= vec->coeff[x] * vec->coeff[y]; | |
124 | |
125 f->distCoeff[x + y*f->distStride]= (int)(d*(1<<10) + 0.5); | |
126 // if(y==vec->length/2) | |
127 // printf("%6d ", f->distCoeff[x + y*f->distStride]); | |
128 } | |
129 } | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
130 sws_freeVec(vec); |
8100 | 131 |
132 return 0; | |
133 } | |
134 | |
135 static int config(struct vf_instance_s* vf, | |
136 int width, int height, int d_width, int d_height, | |
137 unsigned int flags, unsigned int outfmt){ | |
138 | |
139 int sw, sh; | |
140 //asm volatile("emms\n\t"); | |
141 allocStuff(&vf->priv->luma, width, height); | |
142 | |
143 getSubSampleFactors(&sw, &sh, outfmt); | |
144 allocStuff(&vf->priv->chroma, width>>sw, height>>sh); | |
145 | |
146 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt); | |
147 } | |
148 | |
149 static void freeBuffers(FilterParam *f){ | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
150 if(f->preFilterContext) sws_freeContext(f->preFilterContext); |
8100 | 151 f->preFilterContext=NULL; |
152 | |
153 if(f->preFilterBuf) free(f->preFilterBuf); | |
154 f->preFilterBuf=NULL; | |
155 | |
156 if(f->distCoeff) free(f->distCoeff); | |
157 f->distCoeff=NULL; | |
158 } | |
159 | |
160 static void uninit(struct vf_instance_s* vf){ | |
161 if(!vf->priv) return; | |
162 | |
163 freeBuffers(&vf->priv->luma); | |
164 freeBuffers(&vf->priv->chroma); | |
165 | |
166 free(vf->priv); | |
167 vf->priv=NULL; | |
168 } | |
169 | |
170 static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){ | |
171 int x, y; | |
172 FilterParam f= *fp; | |
173 const int radius= f.distWidth/2; | |
174 uint8_t *srcArray[3]= {src, NULL, NULL}; | |
175 uint8_t *dstArray[3]= {f.preFilterBuf, NULL, NULL}; | |
176 int srcStrideArray[3]= {srcStride, 0, 0}; | |
177 int dstStrideArray[3]= {f.preFilterStride, 0, 0}; | |
178 | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
179 // f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray); |
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
180 sws_scale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray); |
8100 | 181 |
182 for(y=0; y<h; y++){ | |
183 for(x=0; x<w; x++){ | |
184 int sum=0; | |
185 int div=0; | |
186 int dy; | |
187 const int preVal= f.preFilterBuf[x + y*f.preFilterStride]; | |
188 #if 0 | |
189 const int srcVal= src[x + y*srcStride]; | |
190 if((x/32)&1){ | |
191 dst[x + y*dstStride]= srcVal; | |
192 if(y%32==0) dst[x + y*dstStride]= 0; | |
193 continue; | |
194 } | |
195 #endif | |
196 if(x >= radius && x < w - radius){ | |
197 for(dy=0; dy<radius*2+1; dy++){ | |
198 int dx; | |
199 int iy= y+dy - radius; | |
200 if (iy<0) iy= -iy; | |
201 else if(iy>=h) iy= h-iy-1; | |
202 | |
203 for(dx=0; dx<radius*2+1; dx++){ | |
204 const int ix= x+dx - radius; | |
205 int factor; | |
206 | |
207 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ] | |
208 *f.distCoeff[dx + dy*f.distStride]; | |
209 sum+= src[ix + iy*srcStride] *factor; | |
210 div+= factor; | |
211 } | |
212 } | |
213 }else{ | |
214 for(dy=0; dy<radius*2+1; dy++){ | |
215 int dx; | |
216 int iy= y+dy - radius; | |
217 if (iy<0) iy= -iy; | |
218 else if(iy>=h) iy= h-iy-1; | |
219 | |
220 for(dx=0; dx<radius*2+1; dx++){ | |
221 int ix= x+dx - radius; | |
222 int factor; | |
223 if (ix<0) ix= -ix; | |
224 else if(ix>=w) ix= w-ix-1; | |
225 | |
226 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ] | |
227 *f.distCoeff[dx + dy*f.distStride]; | |
228 sum+= src[ix + iy*srcStride] *factor; | |
229 div+= factor; | |
230 } | |
231 } | |
232 } | |
233 dst[x + y*dstStride]= (sum + div/2)/div; | |
234 } | |
235 } | |
236 } | |
237 | |
238 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){ | |
239 int cw= mpi->w >> mpi->chroma_x_shift; | |
240 int ch= mpi->h >> mpi->chroma_y_shift; | |
241 | |
242 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
243 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE, | |
244 mpi->w,mpi->h); | |
245 | |
246 assert(mpi->flags&MP_IMGFLAG_PLANAR); | |
247 | |
248 blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma); | |
249 blur(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma); | |
250 blur(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma); | |
251 | |
252 return vf_next_put_image(vf,dmpi); | |
253 } | |
254 | |
255 //===========================================================================// | |
256 | |
257 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
258 switch(fmt) | |
259 { | |
260 case IMGFMT_YV12: | |
261 case IMGFMT_I420: | |
262 case IMGFMT_IYUV: | |
263 case IMGFMT_YVU9: | |
264 case IMGFMT_444P: | |
265 case IMGFMT_422P: | |
266 case IMGFMT_411P: | |
267 return vf_next_query_format(vf, fmt); | |
268 } | |
269 return 0; | |
270 } | |
271 | |
272 static int open(vf_instance_t *vf, char* args){ | |
273 int e; | |
274 | |
275 vf->config=config; | |
276 vf->put_image=put_image; | |
277 // vf->get_image=get_image; | |
278 vf->query_format=query_format; | |
279 vf->uninit=uninit; | |
280 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
281 memset(vf->priv, 0, sizeof(struct vf_priv_s)); | |
282 | |
283 if(args==NULL) return 0; | |
284 | |
285 e=sscanf(args, "%f:%f:%f:%f:%f:%f", | |
286 &vf->priv->luma.radius, | |
287 &vf->priv->luma.preFilterRadius, | |
288 &vf->priv->luma.strength, | |
289 &vf->priv->chroma.radius, | |
290 &vf->priv->chroma.preFilterRadius, | |
291 &vf->priv->chroma.strength | |
292 ); | |
293 | |
294 vf->priv->luma.quality = vf->priv->chroma.quality= 3.0; | |
295 | |
296 if(e==3){ | |
297 vf->priv->chroma.radius= vf->priv->luma.radius; | |
298 vf->priv->chroma.preFilterRadius = vf->priv->luma.preFilterRadius; | |
299 vf->priv->chroma.strength= vf->priv->luma.strength; | |
300 }else if(e!=6) | |
301 return 0; | |
302 | |
303 // if(vf->priv->luma.radius < 0) return 0; | |
304 // if(vf->priv->chroma.radius < 0) return 0; | |
305 | |
306 return 1; | |
307 } | |
308 | |
309 vf_info_t vf_info_sab = { | |
310 "shape adaptive blur", | |
311 "sab", | |
312 "Michael Niedermayer", | |
313 "", | |
9593
e9a2af584986
Add the new -vf option wich is the same as vop in reverse order.
albeu
parents:
9494
diff
changeset
|
314 open, |
e9a2af584986
Add the new -vf option wich is the same as vop in reverse order.
albeu
parents:
9494
diff
changeset
|
315 NULL |
8100 | 316 }; |
317 | |
318 //===========================================================================// |