Mercurial > mplayer.hg
annotate libmpcodecs/vf_sab.c @ 20946:bb4c952bc52c
forgotten 2 converters (yeah svn up, svn di svn ci isnt enough i should actually look at the code after svn up not just the diff ...)
author | michael |
---|---|
date | Thu, 16 Nov 2006 14:49:04 +0000 |
parents | 1e5cf11e8b1f |
children | bf1fbe97cc40 |
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 | |
17367
401b440a6d76
Update licensing information: The FSF changed postal address.
diego
parents:
17012
diff
changeset
|
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
8100 | 17 */ |
18 | |
19 #include <stdio.h> | |
20 #include <stdlib.h> | |
21 #include <string.h> | |
22 #include <inttypes.h> | |
23 #include <assert.h> | |
24 | |
17012 | 25 #include "config.h" |
26 #include "mp_msg.h" | |
8100 | 27 |
28 #ifdef HAVE_MALLOC_H | |
29 #include <malloc.h> | |
30 #endif | |
31 | |
19870
1e5cf11e8b1f
Use PIX_FMT_* instead of IMGFMT_* when calling sws_getContext()
lucabe
parents:
18861
diff
changeset
|
32 #include "avutil.h" |
8100 | 33 #include "img_format.h" |
34 #include "mp_image.h" | |
35 #include "vf.h" | |
17012 | 36 #include "libvo/fastmemcpy.h" |
18861 | 37 #include "libswscale/swscale.h" |
10233
35f52ad860a0
vf_scale.h & related cleanup & some small warning fix by dominik
michael
parents:
9975
diff
changeset
|
38 #include "vf_scale.h" |
8100 | 39 |
40 | |
41 //===========================================================================// | |
42 | |
43 typedef struct FilterParam{ | |
44 float radius; | |
45 float preFilterRadius; | |
46 float strength; | |
47 float quality; | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
48 struct SwsContext *preFilterContext; |
8100 | 49 uint8_t *preFilterBuf; |
50 int preFilterStride; | |
51 int distWidth; | |
52 int distStride; | |
53 int *distCoeff; | |
54 int colorDiffCoeff[512]; | |
55 }FilterParam; | |
56 | |
57 struct vf_priv_s { | |
58 FilterParam luma; | |
59 FilterParam chroma; | |
60 }; | |
61 | |
62 | |
63 /***************************************************************************/ | |
64 | |
65 //FIXME stupid code duplication | |
66 static void getSubSampleFactors(int *h, int *v, int format){ | |
67 switch(format){ | |
68 case IMGFMT_YV12: | |
69 case IMGFMT_I420: | |
70 *h=1; | |
71 *v=1; | |
72 break; | |
73 case IMGFMT_YVU9: | |
74 *h=2; | |
75 *v=2; | |
76 break; | |
77 case IMGFMT_444P: | |
78 *h=0; | |
79 *v=0; | |
80 break; | |
81 case IMGFMT_422P: | |
82 *h=1; | |
83 *v=0; | |
84 break; | |
85 case IMGFMT_411P: | |
86 *h=2; | |
87 *v=0; | |
88 break; | |
89 } | |
90 } | |
91 | |
92 static int allocStuff(FilterParam *f, int width, int height){ | |
93 int stride= (width+7)&~7; | |
94 SwsVector *vec; | |
95 SwsFilter swsF; | |
96 int i,x,y; | |
97 f->preFilterBuf= (uint8_t*)memalign(8, stride*height); | |
98 f->preFilterStride= stride; | |
99 | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
100 vec = sws_getGaussianVec(f->preFilterRadius, f->quality); |
8100 | 101 swsF.lumH= swsF.lumV= vec; |
102 swsF.chrH= swsF.chrV= NULL; | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
103 f->preFilterContext= sws_getContext( |
19870
1e5cf11e8b1f
Use PIX_FMT_* instead of IMGFMT_* when calling sws_getContext()
lucabe
parents:
18861
diff
changeset
|
104 width, height, PIX_FMT_GRAY8, width, height, PIX_FMT_GRAY8, get_sws_cpuflags(), &swsF, NULL, NULL); |
8100 | 105 |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
106 sws_freeVec(vec); |
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
107 vec = sws_getGaussianVec(f->strength, 5.0); |
8100 | 108 for(i=0; i<512; i++){ |
109 double d; | |
110 int index= i-256 + vec->length/2; | |
111 | |
112 if(index<0 || index>=vec->length) d= 0.0; | |
113 else d= vec->coeff[index]; | |
114 | |
115 f->colorDiffCoeff[i]= (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5); | |
116 } | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
117 sws_freeVec(vec); |
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
118 vec = sws_getGaussianVec(f->radius, f->quality); |
8100 | 119 f->distWidth= vec->length; |
120 f->distStride= (vec->length+7)&~7; | |
121 f->distCoeff= (int32_t*)memalign(8, f->distWidth*f->distStride*sizeof(int32_t)); | |
122 | |
123 for(y=0; y<vec->length; y++){ | |
124 for(x=0; x<vec->length; x++){ | |
125 double d= vec->coeff[x] * vec->coeff[y]; | |
126 | |
127 f->distCoeff[x + y*f->distStride]= (int)(d*(1<<10) + 0.5); | |
128 // if(y==vec->length/2) | |
129 // printf("%6d ", f->distCoeff[x + y*f->distStride]); | |
130 } | |
131 } | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
132 sws_freeVec(vec); |
8100 | 133 |
134 return 0; | |
135 } | |
136 | |
137 static int config(struct vf_instance_s* vf, | |
138 int width, int height, int d_width, int d_height, | |
139 unsigned int flags, unsigned int outfmt){ | |
140 | |
141 int sw, sh; | |
142 //asm volatile("emms\n\t"); | |
143 allocStuff(&vf->priv->luma, width, height); | |
144 | |
145 getSubSampleFactors(&sw, &sh, outfmt); | |
146 allocStuff(&vf->priv->chroma, width>>sw, height>>sh); | |
147 | |
148 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt); | |
149 } | |
150 | |
151 static void freeBuffers(FilterParam *f){ | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
152 if(f->preFilterContext) sws_freeContext(f->preFilterContext); |
8100 | 153 f->preFilterContext=NULL; |
154 | |
155 if(f->preFilterBuf) free(f->preFilterBuf); | |
156 f->preFilterBuf=NULL; | |
157 | |
158 if(f->distCoeff) free(f->distCoeff); | |
159 f->distCoeff=NULL; | |
160 } | |
161 | |
162 static void uninit(struct vf_instance_s* vf){ | |
163 if(!vf->priv) return; | |
164 | |
165 freeBuffers(&vf->priv->luma); | |
166 freeBuffers(&vf->priv->chroma); | |
167 | |
168 free(vf->priv); | |
169 vf->priv=NULL; | |
170 } | |
171 | |
172 static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){ | |
173 int x, y; | |
174 FilterParam f= *fp; | |
175 const int radius= f.distWidth/2; | |
176 uint8_t *srcArray[3]= {src, NULL, NULL}; | |
177 uint8_t *dstArray[3]= {f.preFilterBuf, NULL, NULL}; | |
178 int srcStrideArray[3]= {srcStride, 0, 0}; | |
179 int dstStrideArray[3]= {f.preFilterStride, 0, 0}; | |
180 | |
9494
543ab3909b78
sws_ prefix, more seperation between internal & external swscaler API
michael
parents:
8100
diff
changeset
|
181 // 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
|
182 sws_scale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray); |
8100 | 183 |
184 for(y=0; y<h; y++){ | |
185 for(x=0; x<w; x++){ | |
186 int sum=0; | |
187 int div=0; | |
188 int dy; | |
189 const int preVal= f.preFilterBuf[x + y*f.preFilterStride]; | |
190 #if 0 | |
191 const int srcVal= src[x + y*srcStride]; | |
192 if((x/32)&1){ | |
193 dst[x + y*dstStride]= srcVal; | |
194 if(y%32==0) dst[x + y*dstStride]= 0; | |
195 continue; | |
196 } | |
197 #endif | |
198 if(x >= radius && x < w - radius){ | |
199 for(dy=0; dy<radius*2+1; dy++){ | |
200 int dx; | |
201 int iy= y+dy - radius; | |
202 if (iy<0) iy= -iy; | |
203 else if(iy>=h) iy= h-iy-1; | |
204 | |
205 for(dx=0; dx<radius*2+1; dx++){ | |
206 const int ix= x+dx - radius; | |
207 int factor; | |
208 | |
209 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ] | |
210 *f.distCoeff[dx + dy*f.distStride]; | |
211 sum+= src[ix + iy*srcStride] *factor; | |
212 div+= factor; | |
213 } | |
214 } | |
215 }else{ | |
216 for(dy=0; dy<radius*2+1; dy++){ | |
217 int dx; | |
218 int iy= y+dy - radius; | |
219 if (iy<0) iy= -iy; | |
220 else if(iy>=h) iy= h-iy-1; | |
221 | |
222 for(dx=0; dx<radius*2+1; dx++){ | |
223 int ix= x+dx - radius; | |
224 int factor; | |
225 if (ix<0) ix= -ix; | |
226 else if(ix>=w) ix= w-ix-1; | |
227 | |
228 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ] | |
229 *f.distCoeff[dx + dy*f.distStride]; | |
230 sum+= src[ix + iy*srcStride] *factor; | |
231 div+= factor; | |
232 } | |
233 } | |
234 } | |
235 dst[x + y*dstStride]= (sum + div/2)/div; | |
236 } | |
237 } | |
238 } | |
239 | |
17906
20aca9baf5d8
passing pts through the filter layer (lets see if pts or cola comes out at the end)
michael
parents:
17367
diff
changeset
|
240 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi, double pts){ |
8100 | 241 int cw= mpi->w >> mpi->chroma_x_shift; |
242 int ch= mpi->h >> mpi->chroma_y_shift; | |
243 | |
244 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt, | |
245 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE, | |
246 mpi->w,mpi->h); | |
247 | |
248 assert(mpi->flags&MP_IMGFLAG_PLANAR); | |
249 | |
250 blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma); | |
251 blur(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma); | |
252 blur(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma); | |
253 | |
17906
20aca9baf5d8
passing pts through the filter layer (lets see if pts or cola comes out at the end)
michael
parents:
17367
diff
changeset
|
254 return vf_next_put_image(vf,dmpi, pts); |
8100 | 255 } |
256 | |
257 //===========================================================================// | |
258 | |
259 static int query_format(struct vf_instance_s* vf, unsigned int fmt){ | |
260 switch(fmt) | |
261 { | |
262 case IMGFMT_YV12: | |
263 case IMGFMT_I420: | |
264 case IMGFMT_IYUV: | |
265 case IMGFMT_YVU9: | |
266 case IMGFMT_444P: | |
267 case IMGFMT_422P: | |
268 case IMGFMT_411P: | |
269 return vf_next_query_format(vf, fmt); | |
270 } | |
271 return 0; | |
272 } | |
273 | |
274 static int open(vf_instance_t *vf, char* args){ | |
275 int e; | |
276 | |
277 vf->config=config; | |
278 vf->put_image=put_image; | |
279 // vf->get_image=get_image; | |
280 vf->query_format=query_format; | |
281 vf->uninit=uninit; | |
282 vf->priv=malloc(sizeof(struct vf_priv_s)); | |
283 memset(vf->priv, 0, sizeof(struct vf_priv_s)); | |
284 | |
285 if(args==NULL) return 0; | |
286 | |
287 e=sscanf(args, "%f:%f:%f:%f:%f:%f", | |
288 &vf->priv->luma.radius, | |
289 &vf->priv->luma.preFilterRadius, | |
290 &vf->priv->luma.strength, | |
291 &vf->priv->chroma.radius, | |
292 &vf->priv->chroma.preFilterRadius, | |
293 &vf->priv->chroma.strength | |
294 ); | |
295 | |
296 vf->priv->luma.quality = vf->priv->chroma.quality= 3.0; | |
297 | |
298 if(e==3){ | |
299 vf->priv->chroma.radius= vf->priv->luma.radius; | |
300 vf->priv->chroma.preFilterRadius = vf->priv->luma.preFilterRadius; | |
301 vf->priv->chroma.strength= vf->priv->luma.strength; | |
302 }else if(e!=6) | |
303 return 0; | |
304 | |
305 // if(vf->priv->luma.radius < 0) return 0; | |
306 // if(vf->priv->chroma.radius < 0) return 0; | |
307 | |
308 return 1; | |
309 } | |
310 | |
311 vf_info_t vf_info_sab = { | |
312 "shape adaptive blur", | |
313 "sab", | |
314 "Michael Niedermayer", | |
315 "", | |
9593
e9a2af584986
Add the new -vf option wich is the same as vop in reverse order.
albeu
parents:
9494
diff
changeset
|
316 open, |
e9a2af584986
Add the new -vf option wich is the same as vop in reverse order.
albeu
parents:
9494
diff
changeset
|
317 NULL |
8100 | 318 }; |
319 | |
320 //===========================================================================// |