]> git.sesse.net Git - ffmpeg/blob - libavfilter/libmpcodecs/vf_smartblur.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavfilter / libmpcodecs / vf_smartblur.c
1 /*
2  * Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
3  *
4  * This file is part of MPlayer.
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <assert.h>
26
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"
34
35 //===========================================================================//
36
37 typedef struct FilterParam{
38     float radius;
39     float strength;
40     int threshold;
41     float quality;
42     struct SwsContext *filterContext;
43 }FilterParam;
44
45 struct vf_priv_s {
46     FilterParam luma;
47     FilterParam chroma;
48 };
49
50
51 /***************************************************************************/
52
53 //FIXME stupid code duplication
54 static void getSubSampleFactors(int *h, int *v, int format){
55     switch(format){
56     default:
57         assert(0);
58     case IMGFMT_YV12:
59     case IMGFMT_I420:
60         *h=1;
61         *v=1;
62         break;
63     case IMGFMT_YVU9:
64         *h=2;
65         *v=2;
66         break;
67     case IMGFMT_444P:
68         *h=0;
69         *v=0;
70         break;
71     case IMGFMT_422P:
72         *h=1;
73         *v=0;
74         break;
75     case IMGFMT_411P:
76         *h=2;
77         *v=0;
78         break;
79     }
80 }
81
82 static int allocStuff(FilterParam *f, int width, int height){
83     SwsVector *vec;
84     SwsFilter swsF;
85
86     vec = sws_getGaussianVec(f->radius, f->quality);
87     sws_scaleVec(vec, f->strength);
88     vec->coeff[vec->length/2]+= 1.0 - f->strength;
89     swsF.lumH= swsF.lumV= vec;
90     swsF.chrH= swsF.chrV= NULL;
91     f->filterContext= sws_getContext(
92         width, height, PIX_FMT_GRAY8, width, height, PIX_FMT_GRAY8, SWS_BICUBIC, &swsF, NULL, NULL);
93
94     sws_freeVec(vec);
95
96     return 0;
97 }
98
99 static int config(struct vf_instance *vf,
100     int width, int height, int d_width, int d_height,
101     unsigned int flags, unsigned int outfmt){
102
103     int sw, sh;
104
105     allocStuff(&vf->priv->luma, width, height);
106
107     getSubSampleFactors(&sw, &sh, outfmt);
108     allocStuff(&vf->priv->chroma, width>>sw, height>>sh);
109
110     return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
111 }
112
113 static void freeBuffers(FilterParam *f){
114     if(f->filterContext) sws_freeContext(f->filterContext);
115     f->filterContext=NULL;
116 }
117
118 static void uninit(struct vf_instance *vf){
119     if(!vf->priv) return;
120
121     freeBuffers(&vf->priv->luma);
122     freeBuffers(&vf->priv->chroma);
123
124     free(vf->priv);
125     vf->priv=NULL;
126 }
127
128 static inline void blur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, FilterParam *fp){
129     int x, y;
130     FilterParam f= *fp;
131     const uint8_t* const srcArray[MP_MAX_PLANES] = {src};
132     uint8_t *dstArray[MP_MAX_PLANES]= {dst};
133     int srcStrideArray[MP_MAX_PLANES]= {srcStride};
134     int dstStrideArray[MP_MAX_PLANES]= {dstStride};
135
136     sws_scale(f.filterContext, srcArray, srcStrideArray, 0, h, dstArray, dstStrideArray);
137
138     if(f.threshold > 0){
139         for(y=0; y<h; y++){
140             for(x=0; x<w; x++){
141                 const int orig= src[x + y*srcStride];
142                 const int filtered= dst[x + y*dstStride];
143                 const int diff= orig - filtered;
144
145                 if(diff > 0){
146                     if(diff > 2*f.threshold){
147                         dst[x + y*dstStride]= orig;
148                     }else if(diff > f.threshold){
149                         dst[x + y*dstStride]= filtered + diff - f.threshold;
150                     }
151                 }else{
152                     if(-diff > 2*f.threshold){
153                         dst[x + y*dstStride]= orig;
154                     }else if(-diff > f.threshold){
155                         dst[x + y*dstStride]= filtered + diff + f.threshold;
156                     }
157                 }
158             }
159         }
160     }else if(f.threshold < 0){
161         for(y=0; y<h; y++){
162             for(x=0; x<w; x++){
163                 const int orig= src[x + y*srcStride];
164                 const int filtered= dst[x + y*dstStride];
165                 const int diff= orig - filtered;
166
167                 if(diff > 0){
168                     if(diff > -2*f.threshold){
169                     }else if(diff > -f.threshold){
170                         dst[x + y*dstStride]= orig - diff - f.threshold;
171                     }else
172                         dst[x + y*dstStride]= orig;
173                 }else{
174                     if(diff < 2*f.threshold){
175                     }else if(diff < f.threshold){
176                         dst[x + y*dstStride]= orig - diff + f.threshold;
177                     }else
178                         dst[x + y*dstStride]= orig;
179                 }
180             }
181         }
182     }
183 }
184
185 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
186     int cw= mpi->w >> mpi->chroma_x_shift;
187     int ch= mpi->h >> mpi->chroma_y_shift;
188     int threshold = vf->priv->luma.threshold || vf->priv->chroma.threshold;
189
190     mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
191         MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE|
192         (threshold ? MP_IMGFLAG_READABLE : 0),
193         mpi->w,mpi->h);
194
195     assert(mpi->flags&MP_IMGFLAG_PLANAR);
196
197     blur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h, dmpi->stride[0], mpi->stride[0], &vf->priv->luma);
198     blur(dmpi->planes[1], mpi->planes[1], cw    , ch   , dmpi->stride[1], mpi->stride[1], &vf->priv->chroma);
199     blur(dmpi->planes[2], mpi->planes[2], cw    , ch   , dmpi->stride[2], mpi->stride[2], &vf->priv->chroma);
200
201     return vf_next_put_image(vf,dmpi, pts);
202 }
203
204 //===========================================================================//
205
206 static int query_format(struct vf_instance *vf, unsigned int fmt){
207     switch(fmt)
208     {
209     case IMGFMT_YV12:
210     case IMGFMT_I420:
211     case IMGFMT_IYUV:
212     case IMGFMT_YVU9:
213     case IMGFMT_444P:
214     case IMGFMT_422P:
215     case IMGFMT_411P:
216         return vf_next_query_format(vf, fmt);
217     }
218     return 0;
219 }
220
221 static int vf_open(vf_instance_t *vf, char *args){
222     int e;
223
224     vf->config=config;
225     vf->put_image=put_image;
226 //    vf->get_image=get_image;
227     vf->query_format=query_format;
228     vf->uninit=uninit;
229     vf->priv=malloc(sizeof(struct vf_priv_s));
230     memset(vf->priv, 0, sizeof(struct vf_priv_s));
231
232     if(args==NULL) return 0;
233
234     e=sscanf(args, "%f:%f:%d:%f:%f:%d",
235         &vf->priv->luma.radius,
236         &vf->priv->luma.strength,
237         &vf->priv->luma.threshold,
238         &vf->priv->chroma.radius,
239         &vf->priv->chroma.strength,
240         &vf->priv->chroma.threshold
241         );
242
243     vf->priv->luma.quality = vf->priv->chroma.quality= 3.0;
244
245     if(e==3){
246         vf->priv->chroma.radius= vf->priv->luma.radius;
247         vf->priv->chroma.strength= vf->priv->luma.strength;
248         vf->priv->chroma.threshold = vf->priv->luma.threshold;
249     }else if(e!=6)
250         return 0;
251
252     return 1;
253 }
254
255 const vf_info_t vf_info_smartblur = {
256     "smart blur",
257     "smartblur",
258     "Michael Niedermayer",
259     "",
260     vf_open,
261     NULL
262 };
263
264 //===========================================================================//