stream.h: Add 2 prototypes instead of declaring them in cache2.c
[mplayer.git] / libmpcodecs / vf_smartblur.c
blobce98514fc73de9eb96ed8803412f820d47f436c2
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 #ifdef 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"
41 //===========================================================================//
43 typedef struct FilterParam{
44 float radius;
45 float strength;
46 int threshold;
47 float quality;
48 struct SwsContext *filterContext;
49 }FilterParam;
51 struct vf_priv_s {
52 FilterParam luma;
53 FilterParam chroma;
57 /***************************************************************************/
59 //FIXME stupid code duplication
60 static void getSubSampleFactors(int *h, int *v, int format){
61 switch(format){
62 case IMGFMT_YV12:
63 case IMGFMT_I420:
64 *h=1;
65 *v=1;
66 break;
67 case IMGFMT_YVU9:
68 *h=2;
69 *v=2;
70 break;
71 case IMGFMT_444P:
72 *h=0;
73 *v=0;
74 break;
75 case IMGFMT_422P:
76 *h=1;
77 *v=0;
78 break;
79 case IMGFMT_411P:
80 *h=2;
81 *v=0;
82 break;
86 static int allocStuff(FilterParam *f, int width, int height){
87 SwsVector *vec;
88 SwsFilter swsF;
90 vec = sws_getGaussianVec(f->radius, f->quality);
91 sws_scaleVec(vec, f->strength);
92 vec->coeff[vec->length/2]+= 1.0 - f->strength;
93 swsF.lumH= swsF.lumV= vec;
94 swsF.chrH= swsF.chrV= NULL;
95 f->filterContext= sws_getContext(
96 width, height, PIX_FMT_GRAY8, width, height, PIX_FMT_GRAY8, get_sws_cpuflags(), &swsF, NULL, NULL);
98 sws_freeVec(vec);
100 return 0;
103 static int config(struct vf_instance* vf,
104 int width, int height, int d_width, int d_height,
105 unsigned int flags, unsigned int outfmt){
107 int sw, sh;
109 allocStuff(&vf->priv->luma, width, height);
111 getSubSampleFactors(&sw, &sh, outfmt);
112 allocStuff(&vf->priv->chroma, width>>sw, height>>sh);
114 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
117 static void freeBuffers(FilterParam *f){
118 if(f->filterContext) sws_freeContext(f->filterContext);
119 f->filterContext=NULL;
122 static void uninit(struct vf_instance* vf){
123 if(!vf->priv) return;
125 freeBuffers(&vf->priv->luma);
126 freeBuffers(&vf->priv->chroma);
128 free(vf->priv);
129 vf->priv=NULL;
132 static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){
133 int x, y;
134 FilterParam f= *fp;
135 uint8_t *srcArray[3]= {src, NULL, NULL};
136 uint8_t *dstArray[3]= {dst, NULL, NULL};
137 int srcStrideArray[3]= {srcStride, 0, 0};
138 int dstStrideArray[3]= {dstStride, 0, 0};
140 sws_scale(f.filterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
142 if(f.threshold > 0){
143 for(y=0; y<h; y++){
144 for(x=0; x<w; x++){
145 const int orig= src[x + y*srcStride];
146 const int filtered= dst[x + y*dstStride];
147 const int diff= orig - filtered;
149 if(diff > 0){
150 if(diff > 2*f.threshold){
151 dst[x + y*dstStride]= orig;
152 }else if(diff > f.threshold){
153 dst[x + y*dstStride]= filtered + diff - f.threshold;
155 }else{
156 if(-diff > 2*f.threshold){
157 dst[x + y*dstStride]= orig;
158 }else if(-diff > f.threshold){
159 dst[x + y*dstStride]= filtered + diff + f.threshold;
164 }else if(f.threshold < 0){
165 for(y=0; y<h; y++){
166 for(x=0; x<w; x++){
167 const int orig= src[x + y*srcStride];
168 const int filtered= dst[x + y*dstStride];
169 const int diff= orig - filtered;
171 if(diff > 0){
172 if(diff > -2*f.threshold){
173 }else if(diff > -f.threshold){
174 dst[x + y*dstStride]= orig - diff - f.threshold;
175 }else
176 dst[x + y*dstStride]= orig;
177 }else{
178 if(diff < 2*f.threshold){
179 }else if(diff < f.threshold){
180 dst[x + y*dstStride]= orig - diff + f.threshold;
181 }else
182 dst[x + y*dstStride]= orig;
189 static int put_image(struct vf_instance* vf, mp_image_t *mpi, double pts){
190 int cw= mpi->w >> mpi->chroma_x_shift;
191 int ch= mpi->h >> mpi->chroma_y_shift;
192 FilterParam *f= &vf->priv;
194 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
195 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE|
196 (f->threshold) ? MP_IMGFLAG_READABLE : 0,
197 mpi->w,mpi->h);
199 assert(mpi->flags&MP_IMGFLAG_PLANAR);
201 blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma);
202 blur(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma);
203 blur(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma);
205 return vf_next_put_image(vf,dmpi, pts);
208 //===========================================================================//
210 static int query_format(struct vf_instance* vf, unsigned int fmt){
211 switch(fmt)
213 case IMGFMT_YV12:
214 case IMGFMT_I420:
215 case IMGFMT_IYUV:
216 case IMGFMT_YVU9:
217 case IMGFMT_444P:
218 case IMGFMT_422P:
219 case IMGFMT_411P:
220 return vf_next_query_format(vf, fmt);
222 return 0;
225 static int open(vf_instance_t *vf, char* args){
226 int e;
228 vf->config=config;
229 vf->put_image=put_image;
230 // vf->get_image=get_image;
231 vf->query_format=query_format;
232 vf->uninit=uninit;
233 vf->priv=malloc(sizeof(struct vf_priv_s));
234 memset(vf->priv, 0, sizeof(struct vf_priv_s));
236 if(args==NULL) return 0;
238 e=sscanf(args, "%f:%f:%d:%f:%f:%d",
239 &vf->priv->luma.radius,
240 &vf->priv->luma.strength,
241 &vf->priv->luma.threshold,
242 &vf->priv->chroma.radius,
243 &vf->priv->chroma.strength,
244 &vf->priv->chroma.threshold
247 vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
249 if(e==3){
250 vf->priv->chroma.radius= vf->priv->luma.radius;
251 vf->priv->chroma.strength= vf->priv->luma.strength;
252 vf->priv->chroma.threshold = vf->priv->luma.threshold;
253 }else if(e!=6)
254 return 0;
256 return 1;
259 const vf_info_t vf_info_smartblur = {
260 "smart blur",
261 "smartblur",
262 "Michael Niedermayer",
264 open,
265 NULL
268 //===========================================================================//