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