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;
|
|
46 SwsContext *preFilterContext;
|
|
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
|
|
98 vec = getGaussianVec(f->preFilterRadius, f->quality);
|
|
99 swsF.lumH= swsF.lumV= vec;
|
|
100 swsF.chrH= swsF.chrV= NULL;
|
|
101 f->preFilterContext= getSwsContext(
|
|
102 width, height, IMGFMT_Y8, width, height, IMGFMT_Y8, 0, &swsF, NULL);
|
|
103
|
|
104 freeVec(vec);
|
|
105 vec = getGaussianVec(f->strength, 5.0);
|
|
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 }
|
|
115 freeVec(vec);
|
|
116 vec = getGaussianVec(f->radius, f->quality);
|
|
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 }
|
|
130 freeVec(vec);
|
|
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){
|
|
150 if(f->preFilterContext) freeSwsContext(f->preFilterContext);
|
|
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
|
|
179 f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
|
|
180
|
|
181 for(y=0; y<h; y++){
|
|
182 for(x=0; x<w; x++){
|
|
183 int sum=0;
|
|
184 int div=0;
|
|
185 int dy;
|
|
186 const int preVal= f.preFilterBuf[x + y*f.preFilterStride];
|
|
187 #if 0
|
|
188 const int srcVal= src[x + y*srcStride];
|
|
189 if((x/32)&1){
|
|
190 dst[x + y*dstStride]= srcVal;
|
|
191 if(y%32==0) dst[x + y*dstStride]= 0;
|
|
192 continue;
|
|
193 }
|
|
194 #endif
|
|
195 if(x >= radius && x < w - radius){
|
|
196 for(dy=0; dy<radius*2+1; dy++){
|
|
197 int dx;
|
|
198 int iy= y+dy - radius;
|
|
199 if (iy<0) iy= -iy;
|
|
200 else if(iy>=h) iy= h-iy-1;
|
|
201
|
|
202 for(dx=0; dx<radius*2+1; dx++){
|
|
203 const int ix= x+dx - radius;
|
|
204 int factor;
|
|
205
|
|
206 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ]
|
|
207 *f.distCoeff[dx + dy*f.distStride];
|
|
208 sum+= src[ix + iy*srcStride] *factor;
|
|
209 div+= factor;
|
|
210 }
|
|
211 }
|
|
212 }else{
|
|
213 for(dy=0; dy<radius*2+1; dy++){
|
|
214 int dx;
|
|
215 int iy= y+dy - radius;
|
|
216 if (iy<0) iy= -iy;
|
|
217 else if(iy>=h) iy= h-iy-1;
|
|
218
|
|
219 for(dx=0; dx<radius*2+1; dx++){
|
|
220 int ix= x+dx - radius;
|
|
221 int factor;
|
|
222 if (ix<0) ix= -ix;
|
|
223 else if(ix>=w) ix= w-ix-1;
|
|
224
|
|
225 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ]
|
|
226 *f.distCoeff[dx + dy*f.distStride];
|
|
227 sum+= src[ix + iy*srcStride] *factor;
|
|
228 div+= factor;
|
|
229 }
|
|
230 }
|
|
231 }
|
|
232 dst[x + y*dstStride]= (sum + div/2)/div;
|
|
233 }
|
|
234 }
|
|
235 }
|
|
236
|
|
237 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
|
|
238 int cw= mpi->w >> mpi->chroma_x_shift;
|
|
239 int ch= mpi->h >> mpi->chroma_y_shift;
|
|
240
|
|
241 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
|
242 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
|
|
243 mpi->w,mpi->h);
|
|
244
|
|
245 assert(mpi->flags&MP_IMGFLAG_PLANAR);
|
|
246
|
|
247 blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma);
|
|
248 blur(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma);
|
|
249 blur(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma);
|
|
250
|
|
251 return vf_next_put_image(vf,dmpi);
|
|
252 }
|
|
253
|
|
254 //===========================================================================//
|
|
255
|
|
256 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
|
|
257 switch(fmt)
|
|
258 {
|
|
259 case IMGFMT_YV12:
|
|
260 case IMGFMT_I420:
|
|
261 case IMGFMT_IYUV:
|
|
262 case IMGFMT_YVU9:
|
|
263 case IMGFMT_444P:
|
|
264 case IMGFMT_422P:
|
|
265 case IMGFMT_411P:
|
|
266 return vf_next_query_format(vf, fmt);
|
|
267 }
|
|
268 return 0;
|
|
269 }
|
|
270
|
|
271 static int open(vf_instance_t *vf, char* args){
|
|
272 int e;
|
|
273
|
|
274 vf->config=config;
|
|
275 vf->put_image=put_image;
|
|
276 // vf->get_image=get_image;
|
|
277 vf->query_format=query_format;
|
|
278 vf->uninit=uninit;
|
|
279 vf->priv=malloc(sizeof(struct vf_priv_s));
|
|
280 memset(vf->priv, 0, sizeof(struct vf_priv_s));
|
|
281
|
|
282 if(args==NULL) return 0;
|
|
283
|
|
284 e=sscanf(args, "%f:%f:%f:%f:%f:%f",
|
|
285 &vf->priv->luma.radius,
|
|
286 &vf->priv->luma.preFilterRadius,
|
|
287 &vf->priv->luma.strength,
|
|
288 &vf->priv->chroma.radius,
|
|
289 &vf->priv->chroma.preFilterRadius,
|
|
290 &vf->priv->chroma.strength
|
|
291 );
|
|
292
|
|
293 vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
|
|
294
|
|
295 if(e==3){
|
|
296 vf->priv->chroma.radius= vf->priv->luma.radius;
|
|
297 vf->priv->chroma.preFilterRadius = vf->priv->luma.preFilterRadius;
|
|
298 vf->priv->chroma.strength= vf->priv->luma.strength;
|
|
299 }else if(e!=6)
|
|
300 return 0;
|
|
301
|
|
302 // if(vf->priv->luma.radius < 0) return 0;
|
|
303 // if(vf->priv->chroma.radius < 0) return 0;
|
|
304
|
|
305 return 1;
|
|
306 }
|
|
307
|
|
308 vf_info_t vf_info_sab = {
|
|
309 "shape adaptive blur",
|
|
310 "sab",
|
|
311 "Michael Niedermayer",
|
|
312 "",
|
|
313 open
|
|
314 };
|
|
315
|
|
316 //===========================================================================//
|