mixer: fix lowering hw volume while muted
[mplayer.git] / libmpcodecs / vf_sab.c
blob1f2b17a05255355546fe4e2c2175f3d156eabf9b
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 #if HAVE_MALLOC_H
34 #include <malloc.h>
35 #endif
37 #include "img_format.h"
38 #include "mp_image.h"
39 #include "vf.h"
40 #include "vf_scale.h"
43 //===========================================================================//
45 typedef struct FilterParam{
46 float radius;
47 float preFilterRadius;
48 float strength;
49 float quality;
50 struct SwsContext *preFilterContext;
51 uint8_t *preFilterBuf;
52 int preFilterStride;
53 int distWidth;
54 int distStride;
55 int *distCoeff;
56 int colorDiffCoeff[512];
57 }FilterParam;
59 struct vf_priv_s {
60 FilterParam luma;
61 FilterParam chroma;
65 /***************************************************************************/
67 static int allocStuff(FilterParam *f, int width, int height){
68 int stride= (width+7)&~7;
69 SwsVector *vec;
70 SwsFilter swsF;
71 int i,x,y;
72 f->preFilterBuf= av_malloc(stride*height);
73 f->preFilterStride= stride;
75 vec = sws_getGaussianVec(f->preFilterRadius, f->quality);
76 swsF.lumH= swsF.lumV= vec;
77 swsF.chrH= swsF.chrV= NULL;
78 f->preFilterContext= sws_getContext(
79 width, height, PIX_FMT_GRAY8, width, height, PIX_FMT_GRAY8, get_sws_cpuflags()|SWS_POINT, &swsF, NULL, NULL);
81 sws_freeVec(vec);
82 vec = sws_getGaussianVec(f->strength, 5.0);
83 for(i=0; i<512; i++){
84 double d;
85 int index= i-256 + vec->length/2;
87 if(index<0 || index>=vec->length) d= 0.0;
88 else d= vec->coeff[index];
90 f->colorDiffCoeff[i]= (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5);
92 sws_freeVec(vec);
93 vec = sws_getGaussianVec(f->radius, f->quality);
94 f->distWidth= vec->length;
95 f->distStride= (vec->length+7)&~7;
96 f->distCoeff= av_malloc(f->distWidth*f->distStride*sizeof(int32_t));
98 for(y=0; y<vec->length; y++){
99 for(x=0; x<vec->length; x++){
100 double d= vec->coeff[x] * vec->coeff[y];
102 f->distCoeff[x + y*f->distStride]= (int)(d*(1<<10) + 0.5);
103 // if(y==vec->length/2)
104 // printf("%6d ", f->distCoeff[x + y*f->distStride]);
107 sws_freeVec(vec);
109 return 0;
112 static int config(struct vf_instance *vf,
113 int width, int height, int d_width, int d_height,
114 unsigned int flags, unsigned int outfmt){
116 int sw, sh;
117 //__asm__ volatile("emms\n\t");
118 allocStuff(&vf->priv->luma, width, height);
120 mp_get_chroma_shift(outfmt, &sw, &sh, NULL);
121 allocStuff(&vf->priv->chroma, width>>sw, height>>sh);
123 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
126 static void freeBuffers(FilterParam *f){
127 if(f->preFilterContext) sws_freeContext(f->preFilterContext);
128 f->preFilterContext=NULL;
130 av_free(f->preFilterBuf);
131 f->preFilterBuf=NULL;
133 av_free(f->distCoeff);
134 f->distCoeff=NULL;
137 static void uninit(struct vf_instance *vf){
138 if(!vf->priv) return;
140 freeBuffers(&vf->priv->luma);
141 freeBuffers(&vf->priv->chroma);
143 free(vf->priv);
144 vf->priv=NULL;
147 static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){
148 int x, y;
149 FilterParam f= *fp;
150 const int radius= f.distWidth/2;
151 const uint8_t* const srcArray[MP_MAX_PLANES] = {src};
152 uint8_t *dstArray[MP_MAX_PLANES]= {f.preFilterBuf};
153 int srcStrideArray[MP_MAX_PLANES]= {srcStride};
154 int dstStrideArray[MP_MAX_PLANES]= {f.preFilterStride};
156 // f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
157 sws_scale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
159 for(y=0; y<h; y++){
160 for(x=0; x<w; x++){
161 int sum=0;
162 int div=0;
163 int dy;
164 const int preVal= f.preFilterBuf[x + y*f.preFilterStride];
165 #if 0
166 const int srcVal= src[x + y*srcStride];
167 if((x/32)&1){
168 dst[x + y*dstStride]= srcVal;
169 if(y%32==0) dst[x + y*dstStride]= 0;
170 continue;
172 #endif
173 if(x >= radius && x < w - radius){
174 for(dy=0; dy<radius*2+1; dy++){
175 int dx;
176 int iy= y+dy - radius;
177 if (iy<0) iy= -iy;
178 else if(iy>=h) iy= h+h-iy-1;
180 for(dx=0; dx<radius*2+1; dx++){
181 const int ix= x+dx - radius;
182 int factor;
184 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ]
185 *f.distCoeff[dx + dy*f.distStride];
186 sum+= src[ix + iy*srcStride] *factor;
187 div+= factor;
190 }else{
191 for(dy=0; dy<radius*2+1; dy++){
192 int dx;
193 int iy= y+dy - radius;
194 if (iy<0) iy= -iy;
195 else if(iy>=h) iy= h+h-iy-1;
197 for(dx=0; dx<radius*2+1; dx++){
198 int ix= x+dx - radius;
199 int factor;
200 if (ix<0) ix= -ix;
201 else if(ix>=w) ix= w+w-ix-1;
203 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ]
204 *f.distCoeff[dx + dy*f.distStride];
205 sum+= src[ix + iy*srcStride] *factor;
206 div+= factor;
210 dst[x + y*dstStride]= (sum + div/2)/div;
215 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
216 int cw= mpi->w >> mpi->chroma_x_shift;
217 int ch= mpi->h >> mpi->chroma_y_shift;
219 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
220 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
221 mpi->w,mpi->h);
223 assert(mpi->flags&MP_IMGFLAG_PLANAR);
225 blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma);
226 blur(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma);
227 blur(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma);
229 return vf_next_put_image(vf,dmpi, pts);
232 //===========================================================================//
234 static int query_format(struct vf_instance *vf, unsigned int fmt){
235 switch(fmt)
237 case IMGFMT_YV12:
238 case IMGFMT_I420:
239 case IMGFMT_IYUV:
240 case IMGFMT_YVU9:
241 case IMGFMT_444P:
242 case IMGFMT_422P:
243 case IMGFMT_411P:
244 return vf_next_query_format(vf, fmt);
246 return 0;
249 static int vf_open(vf_instance_t *vf, char *args){
250 int e;
252 vf->config=config;
253 vf->put_image=put_image;
254 // vf->get_image=get_image;
255 vf->query_format=query_format;
256 vf->uninit=uninit;
257 vf->priv=malloc(sizeof(struct vf_priv_s));
258 memset(vf->priv, 0, sizeof(struct vf_priv_s));
260 if(args==NULL) return 0;
262 e=sscanf(args, "%f:%f:%f:%f:%f:%f",
263 &vf->priv->luma.radius,
264 &vf->priv->luma.preFilterRadius,
265 &vf->priv->luma.strength,
266 &vf->priv->chroma.radius,
267 &vf->priv->chroma.preFilterRadius,
268 &vf->priv->chroma.strength
271 vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
273 if(e==3){
274 vf->priv->chroma.radius= vf->priv->luma.radius;
275 vf->priv->chroma.preFilterRadius = vf->priv->luma.preFilterRadius;
276 vf->priv->chroma.strength= vf->priv->luma.strength;
277 }else if(e!=6)
278 return 0;
280 // if(vf->priv->luma.radius < 0) return 0;
281 // if(vf->priv->chroma.radius < 0) return 0;
283 return 1;
286 const vf_info_t vf_info_sab = {
287 "shape adaptive blur",
288 "sab",
289 "Michael Niedermayer",
291 vf_open,
292 NULL
295 //===========================================================================//