cache2.c: Avoid warnings about discarding volatile
[mplayer.git] / libmpcodecs / vf_sab.c
blob7f05af92490661790cf33a8c39e37c7764e4afb8
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 "config.h"
28 #include "mp_msg.h"
30 #if HAVE_MALLOC_H
31 #include <malloc.h>
32 #endif
34 #include "libavutil/avutil.h"
35 #include "img_format.h"
36 #include "mp_image.h"
37 #include "vf.h"
38 #include "libswscale/swscale.h"
39 #include "vf_scale.h"
42 //===========================================================================//
44 typedef struct FilterParam{
45 float radius;
46 float preFilterRadius;
47 float strength;
48 float quality;
49 struct SwsContext *preFilterContext;
50 uint8_t *preFilterBuf;
51 int preFilterStride;
52 int distWidth;
53 int distStride;
54 int *distCoeff;
55 int colorDiffCoeff[512];
56 }FilterParam;
58 struct vf_priv_s {
59 FilterParam luma;
60 FilterParam chroma;
64 /***************************************************************************/
66 static int allocStuff(FilterParam *f, int width, int height){
67 int stride= (width+7)&~7;
68 SwsVector *vec;
69 SwsFilter swsF;
70 int i,x,y;
71 f->preFilterBuf= av_malloc(stride*height);
72 f->preFilterStride= stride;
74 vec = sws_getGaussianVec(f->preFilterRadius, f->quality);
75 swsF.lumH= swsF.lumV= vec;
76 swsF.chrH= swsF.chrV= NULL;
77 f->preFilterContext= sws_getContext(
78 width, height, PIX_FMT_GRAY8, width, height, PIX_FMT_GRAY8, get_sws_cpuflags()|SWS_POINT, &swsF, NULL, NULL);
80 sws_freeVec(vec);
81 vec = sws_getGaussianVec(f->strength, 5.0);
82 for(i=0; i<512; i++){
83 double d;
84 int index= i-256 + vec->length/2;
86 if(index<0 || index>=vec->length) d= 0.0;
87 else d= vec->coeff[index];
89 f->colorDiffCoeff[i]= (int)(d/vec->coeff[vec->length/2]*(1<<12) + 0.5);
91 sws_freeVec(vec);
92 vec = sws_getGaussianVec(f->radius, f->quality);
93 f->distWidth= vec->length;
94 f->distStride= (vec->length+7)&~7;
95 f->distCoeff= av_malloc(f->distWidth*f->distStride*sizeof(int32_t));
97 for(y=0; y<vec->length; y++){
98 for(x=0; x<vec->length; x++){
99 double d= vec->coeff[x] * vec->coeff[y];
101 f->distCoeff[x + y*f->distStride]= (int)(d*(1<<10) + 0.5);
102 // if(y==vec->length/2)
103 // printf("%6d ", f->distCoeff[x + y*f->distStride]);
106 sws_freeVec(vec);
108 return 0;
111 static int config(struct vf_instance *vf,
112 int width, int height, int d_width, int d_height,
113 unsigned int flags, unsigned int outfmt){
115 int sw, sh;
116 //__asm__ volatile("emms\n\t");
117 allocStuff(&vf->priv->luma, width, height);
119 mp_get_chroma_shift(outfmt, &sw, &sh);
120 allocStuff(&vf->priv->chroma, width>>sw, height>>sh);
122 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
125 static void freeBuffers(FilterParam *f){
126 if(f->preFilterContext) sws_freeContext(f->preFilterContext);
127 f->preFilterContext=NULL;
129 av_free(f->preFilterBuf);
130 f->preFilterBuf=NULL;
132 av_free(f->distCoeff);
133 f->distCoeff=NULL;
136 static void uninit(struct vf_instance *vf){
137 if(!vf->priv) return;
139 freeBuffers(&vf->priv->luma);
140 freeBuffers(&vf->priv->chroma);
142 free(vf->priv);
143 vf->priv=NULL;
146 static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){
147 int x, y;
148 FilterParam f= *fp;
149 const int radius= f.distWidth/2;
150 const uint8_t* const srcArray[MP_MAX_PLANES] = {src};
151 uint8_t *dstArray[MP_MAX_PLANES]= {f.preFilterBuf};
152 int srcStrideArray[MP_MAX_PLANES]= {srcStride};
153 int dstStrideArray[MP_MAX_PLANES]= {f.preFilterStride};
155 // f.preFilterContext->swScale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
156 sws_scale(f.preFilterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
158 for(y=0; y<h; y++){
159 for(x=0; x<w; x++){
160 int sum=0;
161 int div=0;
162 int dy;
163 const int preVal= f.preFilterBuf[x + y*f.preFilterStride];
164 #if 0
165 const int srcVal= src[x + y*srcStride];
166 if((x/32)&1){
167 dst[x + y*dstStride]= srcVal;
168 if(y%32==0) dst[x + y*dstStride]= 0;
169 continue;
171 #endif
172 if(x >= radius && x < w - radius){
173 for(dy=0; dy<radius*2+1; dy++){
174 int dx;
175 int iy= y+dy - radius;
176 if (iy<0) iy= -iy;
177 else if(iy>=h) iy= h+h-iy-1;
179 for(dx=0; dx<radius*2+1; dx++){
180 const int ix= x+dx - radius;
181 int factor;
183 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ]
184 *f.distCoeff[dx + dy*f.distStride];
185 sum+= src[ix + iy*srcStride] *factor;
186 div+= factor;
189 }else{
190 for(dy=0; dy<radius*2+1; dy++){
191 int dx;
192 int iy= y+dy - radius;
193 if (iy<0) iy= -iy;
194 else if(iy>=h) iy= h+h-iy-1;
196 for(dx=0; dx<radius*2+1; dx++){
197 int ix= x+dx - radius;
198 int factor;
199 if (ix<0) ix= -ix;
200 else if(ix>=w) ix= w+w-ix-1;
202 factor= f.colorDiffCoeff[256+preVal - f.preFilterBuf[ix + iy*f.preFilterStride] ]
203 *f.distCoeff[dx + dy*f.distStride];
204 sum+= src[ix + iy*srcStride] *factor;
205 div+= factor;
209 dst[x + y*dstStride]= (sum + div/2)/div;
214 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
215 int cw= mpi->w >> mpi->chroma_x_shift;
216 int ch= mpi->h >> mpi->chroma_y_shift;
218 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
219 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
220 mpi->w,mpi->h);
222 assert(mpi->flags&MP_IMGFLAG_PLANAR);
224 blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma);
225 blur(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma);
226 blur(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma);
228 return vf_next_put_image(vf,dmpi, pts);
231 //===========================================================================//
233 static int query_format(struct vf_instance *vf, unsigned int fmt){
234 switch(fmt)
236 case IMGFMT_YV12:
237 case IMGFMT_I420:
238 case IMGFMT_IYUV:
239 case IMGFMT_YVU9:
240 case IMGFMT_444P:
241 case IMGFMT_422P:
242 case IMGFMT_411P:
243 return vf_next_query_format(vf, fmt);
245 return 0;
248 static int vf_open(vf_instance_t *vf, char *args){
249 int e;
251 vf->config=config;
252 vf->put_image=put_image;
253 // vf->get_image=get_image;
254 vf->query_format=query_format;
255 vf->uninit=uninit;
256 vf->priv=malloc(sizeof(struct vf_priv_s));
257 memset(vf->priv, 0, sizeof(struct vf_priv_s));
259 if(args==NULL) return 0;
261 e=sscanf(args, "%f:%f:%f:%f:%f:%f",
262 &vf->priv->luma.radius,
263 &vf->priv->luma.preFilterRadius,
264 &vf->priv->luma.strength,
265 &vf->priv->chroma.radius,
266 &vf->priv->chroma.preFilterRadius,
267 &vf->priv->chroma.strength
270 vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
272 if(e==3){
273 vf->priv->chroma.radius= vf->priv->luma.radius;
274 vf->priv->chroma.preFilterRadius = vf->priv->luma.preFilterRadius;
275 vf->priv->chroma.strength= vf->priv->luma.strength;
276 }else if(e!=6)
277 return 0;
279 // if(vf->priv->luma.radius < 0) return 0;
280 // if(vf->priv->chroma.radius < 0) return 0;
282 return 1;
285 const vf_info_t vf_info_sab = {
286 "shape adaptive blur",
287 "sab",
288 "Michael Niedermayer",
290 vf_open,
291 NULL
294 //===========================================================================//