audio: add af_lavrresample, remove old resampling filters
[mplayer2.git] / libmpcodecs / vf_sab.c
blob33f675455546358a3f6116adadbf2a98d9c99910
1 /*
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.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <assert.h>
27 #include <libavutil/mem.h>
28 #include <libswscale/swscale.h>
30 #include "config.h"
31 #include "mp_msg.h"
33 #include "img_format.h"
34 #include "mp_image.h"
35 #include "vf.h"
36 #include "vf_scale.h"
39 //===========================================================================//
41 typedef struct FilterParam{
42 float radius;
43 float preFilterRadius;
44 float strength;
45 float quality;
46 struct SwsContext *preFilterContext;
47 uint8_t *preFilterBuf;
48 int preFilterStride;
49 int distWidth;
50 int distStride;
51 int *distCoeff;
52 int colorDiffCoeff[512];
53 }FilterParam;
55 struct vf_priv_s {
56 FilterParam luma;
57 FilterParam chroma;
61 /***************************************************************************/
63 static int allocStuff(FilterParam *f, int width, int height){
64 int stride= (width+7)&~7;
65 SwsVector *vec;
66 SwsFilter swsF;
67 int i,x,y;
68 f->preFilterBuf= av_malloc(stride*height);
69 f->preFilterStride= stride;
71 vec = sws_getGaussianVec(f->preFilterRadius, f->quality);
72 swsF.lumH= swsF.lumV= vec;
73 swsF.chrH= swsF.chrV= NULL;
74 f->preFilterContext= sws_getContext(
75 width, height, PIX_FMT_GRAY8, width, height, PIX_FMT_GRAY8, get_sws_cpuflags()|SWS_POINT, &swsF, NULL, NULL);
77 sws_freeVec(vec);
78 vec = sws_getGaussianVec(f->strength, 5.0);
79 for(i=0; i<512; i++){
80 double d;
81 int index= i-256 + vec->length/2;
83 if(index<0 || index>=vec->length) d= 0.0;
84 else d= vec->coeff[index];
86 f->colorDiffCoeff[i]= (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5);
88 sws_freeVec(vec);
89 vec = sws_getGaussianVec(f->radius, f->quality);
90 f->distWidth= vec->length;
91 f->distStride= (vec->length+7)&~7;
92 f->distCoeff= av_malloc(f->distWidth*f->distStride*sizeof(int32_t));
94 for(y=0; y<vec->length; y++){
95 for(x=0; x<vec->length; x++){
96 double d= vec->coeff[x] * vec->coeff[y];
98 f->distCoeff[x + y*f->distStride]= (int)(d*(1<<10) + 0.5);
99 // if(y==vec->length/2)
100 // printf("%6d ", f->distCoeff[x + y*f->distStride]);
103 sws_freeVec(vec);
105 return 0;
108 static int config(struct vf_instance *vf,
109 int width, int height, int d_width, int d_height,
110 unsigned int flags, unsigned int outfmt){
112 int sw, sh;
113 //__asm__ volatile("emms\n\t");
114 allocStuff(&vf->priv->luma, width, height);
116 mp_get_chroma_shift(outfmt, &sw, &sh, NULL);
117 allocStuff(&vf->priv->chroma, width>>sw, height>>sh);
119 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
122 static void freeBuffers(FilterParam *f){
123 if(f->preFilterContext) sws_freeContext(f->preFilterContext);
124 f->preFilterContext=NULL;
126 av_free(f->preFilterBuf);
127 f->preFilterBuf=NULL;
129 av_free(f->distCoeff);
130 f->distCoeff=NULL;
133 static void uninit(struct vf_instance *vf){
134 if(!vf->priv) return;
136 freeBuffers(&vf->priv->luma);
137 freeBuffers(&vf->priv->chroma);
139 free(vf->priv);
140 vf->priv=NULL;
143 static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){
144 int x, y;
145 FilterParam f= *fp;
146 const int radius= f.distWidth/2;
147 const uint8_t* const srcArray[MP_MAX_PLANES] = {src};
148 uint8_t *dstArray[MP_MAX_PLANES]= {f.preFilterBuf};
149 int srcStrideArray[MP_MAX_PLANES]= {srcStride};
150 int dstStrideArray[MP_MAX_PLANES]= {f.preFilterStride};
152 // f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
153 sws_scale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
155 for(y=0; y<h; y++){
156 for(x=0; x<w; x++){
157 int sum=0;
158 int div=0;
159 int dy;
160 const int preVal= f.preFilterBuf[x + y*f.preFilterStride];
161 #if 0
162 const int srcVal= src[x + y*srcStride];
163 if((x/32)&1){
164 dst[x + y*dstStride]= srcVal;
165 if(y%32==0) dst[x + y*dstStride]= 0;
166 continue;
168 #endif
169 if(x >= radius && x < w - radius){
170 for(dy=0; dy<radius*2+1; dy++){
171 int dx;
172 int iy= y+dy - radius;
173 if (iy<0) iy= -iy;
174 else if(iy>=h) iy= h+h-iy-1;
176 for(dx=0; dx<radius*2+1; dx++){
177 const int ix= x+dx - radius;
178 int factor;
180 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ]
181 *f.distCoeff[dx + dy*f.distStride];
182 sum+= src[ix + iy*srcStride] *factor;
183 div+= factor;
186 }else{
187 for(dy=0; dy<radius*2+1; dy++){
188 int dx;
189 int iy= y+dy - radius;
190 if (iy<0) iy= -iy;
191 else if(iy>=h) iy= h+h-iy-1;
193 for(dx=0; dx<radius*2+1; dx++){
194 int ix= x+dx - radius;
195 int factor;
196 if (ix<0) ix= -ix;
197 else if(ix>=w) ix= w+w-ix-1;
199 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ]
200 *f.distCoeff[dx + dy*f.distStride];
201 sum+= src[ix + iy*srcStride] *factor;
202 div+= factor;
206 dst[x + y*dstStride]= (sum + div/2)/div;
211 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
212 int cw= mpi->w >> mpi->chroma_x_shift;
213 int ch= mpi->h >> mpi->chroma_y_shift;
215 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
216 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
217 mpi->w,mpi->h);
219 assert(mpi->flags&MP_IMGFLAG_PLANAR);
221 blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma);
222 blur(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma);
223 blur(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma);
225 return vf_next_put_image(vf,dmpi, pts);
228 //===========================================================================//
230 static int query_format(struct vf_instance *vf, unsigned int fmt){
231 switch(fmt)
233 case IMGFMT_YV12:
234 case IMGFMT_I420:
235 case IMGFMT_IYUV:
236 case IMGFMT_YVU9:
237 case IMGFMT_444P:
238 case IMGFMT_422P:
239 case IMGFMT_411P:
240 return vf_next_query_format(vf, fmt);
242 return 0;
245 static int vf_open(vf_instance_t *vf, char *args){
246 int e;
248 vf->config=config;
249 vf->put_image=put_image;
250 // vf->get_image=get_image;
251 vf->query_format=query_format;
252 vf->uninit=uninit;
253 vf->priv=malloc(sizeof(struct vf_priv_s));
254 memset(vf->priv, 0, sizeof(struct vf_priv_s));
256 if(args==NULL) return 0;
258 e=sscanf(args, "%f:%f:%f:%f:%f:%f",
259 &vf->priv->luma.radius,
260 &vf->priv->luma.preFilterRadius,
261 &vf->priv->luma.strength,
262 &vf->priv->chroma.radius,
263 &vf->priv->chroma.preFilterRadius,
264 &vf->priv->chroma.strength
267 vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
269 if(e==3){
270 vf->priv->chroma.radius= vf->priv->luma.radius;
271 vf->priv->chroma.preFilterRadius = vf->priv->luma.preFilterRadius;
272 vf->priv->chroma.strength= vf->priv->luma.strength;
273 }else if(e!=6)
274 return 0;
276 // if(vf->priv->luma.radius < 0) return 0;
277 // if(vf->priv->chroma.radius < 0) return 0;
279 return 1;
282 const vf_info_t vf_info_sab = {
283 "shape adaptive blur",
284 "sab",
285 "Michael Niedermayer",
287 vf_open,
288 NULL
291 //===========================================================================//