mp_msg: print messages to stdout, statusline to stderr
[mplayer.git] / libmpcodecs / vf_smartblur.c
blob7c54231e40238e95d92c3e66be7bedc9f095b098
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>
26 #include <stdbool.h>
28 #include "mp_msg.h"
29 #include "libavutil/avutil.h"
30 #include "img_format.h"
31 #include "mp_image.h"
32 #include "vf.h"
33 #include "libswscale/swscale.h"
34 #include "vf_scale.h"
36 //===========================================================================//
38 typedef struct FilterParam{
39 float radius;
40 float strength;
41 int threshold;
42 float quality;
43 struct SwsContext *filterContext;
44 }FilterParam;
46 struct vf_priv_s {
47 FilterParam luma;
48 FilterParam chroma;
52 /***************************************************************************/
54 static int allocStuff(FilterParam *f, int width, int height){
55 SwsVector *vec;
56 SwsFilter swsF;
58 vec = sws_getGaussianVec(f->radius, f->quality);
59 sws_scaleVec(vec, f->strength);
60 vec->coeff[vec->length/2]+= 1.0 - f->strength;
61 swsF.lumH= swsF.lumV= vec;
62 swsF.chrH= swsF.chrV= NULL;
63 f->filterContext= sws_getContext(
64 width, height, PIX_FMT_GRAY8, width, height, PIX_FMT_GRAY8, SWS_BICUBIC | get_sws_cpuflags(), &swsF, NULL, NULL);
66 sws_freeVec(vec);
68 return 0;
71 static int config(struct vf_instance *vf,
72 int width, int height, int d_width, int d_height,
73 unsigned int flags, unsigned int outfmt){
75 int sw, sh;
77 allocStuff(&vf->priv->luma, width, height);
79 mp_get_chroma_shift(outfmt, &sw, &sh, NULL);
80 allocStuff(&vf->priv->chroma, width>>sw, height>>sh);
82 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
85 static void freeBuffers(FilterParam *f){
86 if(f->filterContext) sws_freeContext(f->filterContext);
87 f->filterContext=NULL;
90 static void uninit(struct vf_instance *vf){
91 if(!vf->priv) return;
93 freeBuffers(&vf->priv->luma);
94 freeBuffers(&vf->priv->chroma);
96 free(vf->priv);
97 vf->priv=NULL;
100 static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){
101 int x, y;
102 FilterParam f= *fp;
103 const uint8_t* const srcArray[MP_MAX_PLANES] = {src};
104 uint8_t *dstArray[MP_MAX_PLANES]= {dst};
105 int srcStrideArray[MP_MAX_PLANES]= {srcStride};
106 int dstStrideArray[MP_MAX_PLANES]= {dstStride};
108 sws_scale(f.filterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
110 if(f.threshold > 0){
111 for(y=0; y<h; y++){
112 for(x=0; x<w; x++){
113 const int orig= src[x + y*srcStride];
114 const int filtered= dst[x + y*dstStride];
115 const int diff= orig - filtered;
117 if(diff > 0){
118 if(diff > 2*f.threshold){
119 dst[x + y*dstStride]= orig;
120 }else if(diff > f.threshold){
121 dst[x + y*dstStride]= filtered + diff - f.threshold;
123 }else{
124 if(-diff > 2*f.threshold){
125 dst[x + y*dstStride]= orig;
126 }else if(-diff > f.threshold){
127 dst[x + y*dstStride]= filtered + diff + f.threshold;
132 }else if(f.threshold < 0){
133 for(y=0; y<h; y++){
134 for(x=0; x<w; x++){
135 const int orig= src[x + y*srcStride];
136 const int filtered= dst[x + y*dstStride];
137 const int diff= orig - filtered;
139 if(diff > 0){
140 if(diff > -2*f.threshold){
141 }else if(diff > -f.threshold){
142 dst[x + y*dstStride]= orig - diff - f.threshold;
143 }else
144 dst[x + y*dstStride]= orig;
145 }else{
146 if(diff < 2*f.threshold){
147 }else if(diff < f.threshold){
148 dst[x + y*dstStride]= orig - diff + f.threshold;
149 }else
150 dst[x + y*dstStride]= orig;
157 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
158 int cw= mpi->w >> mpi->chroma_x_shift;
159 int ch= mpi->h >> mpi->chroma_y_shift;
160 bool threshold = vf->priv->luma.threshold || vf->priv->chroma.threshold;
162 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
163 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE|
164 (threshold ? MP_IMGFLAG_READABLE : 0),
165 mpi->w,mpi->h);
167 assert(mpi->flags&MP_IMGFLAG_PLANAR);
169 blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma);
170 blur(dmpi->planes[1], mpi->planes[1], cw , ch , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma);
171 blur(dmpi->planes[2], mpi->planes[2], cw , ch , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma);
173 return vf_next_put_image(vf,dmpi, pts);
176 //===========================================================================//
178 static int query_format(struct vf_instance *vf, unsigned int fmt){
179 switch(fmt)
181 case IMGFMT_YV12:
182 case IMGFMT_I420:
183 case IMGFMT_IYUV:
184 case IMGFMT_YVU9:
185 case IMGFMT_444P:
186 case IMGFMT_422P:
187 case IMGFMT_411P:
188 return vf_next_query_format(vf, fmt);
190 return 0;
193 static int vf_open(vf_instance_t *vf, char *args){
194 int e;
196 vf->config=config;
197 vf->put_image=put_image;
198 // vf->get_image=get_image;
199 vf->query_format=query_format;
200 vf->uninit=uninit;
201 vf->priv=malloc(sizeof(struct vf_priv_s));
202 memset(vf->priv, 0, sizeof(struct vf_priv_s));
204 if(args==NULL) return 0;
206 e=sscanf(args, "%f:%f:%d:%f:%f:%d",
207 &vf->priv->luma.radius,
208 &vf->priv->luma.strength,
209 &vf->priv->luma.threshold,
210 &vf->priv->chroma.radius,
211 &vf->priv->chroma.strength,
212 &vf->priv->chroma.threshold
215 vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
217 if(e==3){
218 vf->priv->chroma.radius= vf->priv->luma.radius;
219 vf->priv->chroma.strength= vf->priv->luma.strength;
220 vf->priv->chroma.threshold = vf->priv->luma.threshold;
221 }else if(e!=6)
222 return 0;
224 return 1;
227 const vf_info_t vf_info_smartblur = {
228 "smart blur",
229 "smartblur",
230 "Michael Niedermayer",
232 vf_open,
233 NULL
236 //===========================================================================//