input: make slave command file descriptors nonblocking
[mplayer/greg.git] / libmpcodecs / vf_smartblur.c
blob4083dfbda05aa780e40b33670f26a6673775eac0
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 "mp_msg.h"
28 #include "libavutil/avutil.h"
29 #include "img_format.h"
30 #include "mp_image.h"
31 #include "vf.h"
32 #include "libswscale/swscale.h"
33 #include "vf_scale.h"
35 //===========================================================================//
37 typedef struct FilterParam{
38 float radius;
39 float strength;
40 int threshold;
41 float quality;
42 struct SwsContext *filterContext;
43 }FilterParam;
45 struct vf_priv_s {
46 FilterParam luma;
47 FilterParam chroma;
51 /***************************************************************************/
53 static int allocStuff(FilterParam *f, int width, int height){
54 SwsVector *vec;
55 SwsFilter swsF;
57 vec = sws_getGaussianVec(f->radius, f->quality);
58 sws_scaleVec(vec, f->strength);
59 vec->coeff[vec->length/2]+= 1.0 - f->strength;
60 swsF.lumH= swsF.lumV= vec;
61 swsF.chrH= swsF.chrV= NULL;
62 f->filterContext= sws_getContext(
63 width, height, PIX_FMT_GRAY8, width, height, PIX_FMT_GRAY8, SWS_BICUBIC | get_sws_cpuflags(), &swsF, NULL, NULL);
65 sws_freeVec(vec);
67 return 0;
70 static int config(struct vf_instance *vf,
71 int width, int height, int d_width, int d_height,
72 unsigned int flags, unsigned int outfmt){
74 int sw, sh;
76 allocStuff(&vf->priv->luma, width, height);
78 mp_get_chroma_shift(outfmt, &sw, &sh);
79 allocStuff(&vf->priv->chroma, width>>sw, height>>sh);
81 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
84 static void freeBuffers(FilterParam *f){
85 if(f->filterContext) sws_freeContext(f->filterContext);
86 f->filterContext=NULL;
89 static void uninit(struct vf_instance *vf){
90 if(!vf->priv) return;
92 freeBuffers(&vf->priv->luma);
93 freeBuffers(&vf->priv->chroma);
95 free(vf->priv);
96 vf->priv=NULL;
99 static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){
100 int x, y;
101 FilterParam f= *fp;
102 const uint8_t* const srcArray[MP_MAX_PLANES] = {src};
103 uint8_t *dstArray[MP_MAX_PLANES]= {dst};
104 int srcStrideArray[MP_MAX_PLANES]= {srcStride};
105 int dstStrideArray[MP_MAX_PLANES]= {dstStride};
107 sws_scale(f.filterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
109 if(f.threshold > 0){
110 for(y=0; y<h; y++){
111 for(x=0; x<w; x++){
112 const int orig= src[x + y*srcStride];
113 const int filtered= dst[x + y*dstStride];
114 const int diff= orig - filtered;
116 if(diff > 0){
117 if(diff > 2*f.threshold){
118 dst[x + y*dstStride]= orig;
119 }else if(diff > f.threshold){
120 dst[x + y*dstStride]= filtered + diff - f.threshold;
122 }else{
123 if(-diff > 2*f.threshold){
124 dst[x + y*dstStride]= orig;
125 }else if(-diff > f.threshold){
126 dst[x + y*dstStride]= filtered + diff + f.threshold;
131 }else if(f.threshold < 0){
132 for(y=0; y<h; y++){
133 for(x=0; x<w; x++){
134 const int orig= src[x + y*srcStride];
135 const int filtered= dst[x + y*dstStride];
136 const int diff= orig - filtered;
138 if(diff > 0){
139 if(diff > -2*f.threshold){
140 }else if(diff > -f.threshold){
141 dst[x + y*dstStride]= orig - diff - f.threshold;
142 }else
143 dst[x + y*dstStride]= orig;
144 }else{
145 if(diff < 2*f.threshold){
146 }else if(diff < f.threshold){
147 dst[x + y*dstStride]= orig - diff + f.threshold;
148 }else
149 dst[x + y*dstStride]= orig;
156 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
157 int cw= mpi->w >> mpi->chroma_x_shift;
158 int ch= mpi->h >> mpi->chroma_y_shift;
159 FilterParam *f= &vf->priv;
161 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
162 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE|
163 (f->threshold) ? MP_IMGFLAG_READABLE : 0,
164 mpi->w,mpi->h);
166 assert(mpi->flags&MP_IMGFLAG_PLANAR);
168 blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma);
169 blur(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma);
170 blur(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma);
172 return vf_next_put_image(vf,dmpi, pts);
175 //===========================================================================//
177 static int query_format(struct vf_instance *vf, unsigned int fmt){
178 switch(fmt)
180 case IMGFMT_YV12:
181 case IMGFMT_I420:
182 case IMGFMT_IYUV:
183 case IMGFMT_YVU9:
184 case IMGFMT_444P:
185 case IMGFMT_422P:
186 case IMGFMT_411P:
187 return vf_next_query_format(vf, fmt);
189 return 0;
192 static int vf_open(vf_instance_t *vf, char *args){
193 int e;
195 vf->config=config;
196 vf->put_image=put_image;
197 // vf->get_image=get_image;
198 vf->query_format=query_format;
199 vf->uninit=uninit;
200 vf->priv=malloc(sizeof(struct vf_priv_s));
201 memset(vf->priv, 0, sizeof(struct vf_priv_s));
203 if(args==NULL) return 0;
205 e=sscanf(args, "%f:%f:%d:%f:%f:%d",
206 &vf->priv->luma.radius,
207 &vf->priv->luma.strength,
208 &vf->priv->luma.threshold,
209 &vf->priv->chroma.radius,
210 &vf->priv->chroma.strength,
211 &vf->priv->chroma.threshold
214 vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
216 if(e==3){
217 vf->priv->chroma.radius= vf->priv->luma.radius;
218 vf->priv->chroma.strength= vf->priv->luma.strength;
219 vf->priv->chroma.threshold = vf->priv->luma.threshold;
220 }else if(e!=6)
221 return 0;
223 return 1;
226 const vf_info_t vf_info_smartblur = {
227 "smart blur",
228 "smartblur",
229 "Michael Niedermayer",
231 vf_open,
232 NULL
235 //===========================================================================//