comparison libmpcodecs/vf_boxblur.c @ 8010:329e9a5a154f

boxblur, another useless filter
author michael
date Thu, 31 Oct 2002 23:26:11 +0000
parents
children 9fc45fe0d444
comparison
equal deleted inserted replaced
8009:302708769c69 8010:329e9a5a154f
1 /*
2 Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
23 #include <assert.h>
24
25 #include "../config.h"
26 #include "../mp_msg.h"
27
28 #ifdef HAVE_MALLOC_H
29 #include <malloc.h>
30 #endif
31
32 #include "img_format.h"
33 #include "mp_image.h"
34 #include "vf.h"
35 #include "../libvo/fastmemcpy.h"
36
37
38 //===========================================================================//
39
40 typedef struct FilterParam{
41 int radius;
42 int power;
43 }FilterParam;
44
45 struct vf_priv_s {
46 FilterParam lumaParam;
47 FilterParam chromaParam;
48 };
49
50
51 /***************************************************************************/
52
53
54 static int config(struct vf_instance_s* vf,
55 int width, int height, int d_width, int d_height,
56 unsigned int flags, unsigned int outfmt){
57
58 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
59 }
60
61 static inline void blur(uint8_t *dst, uint8_t *src, int w, int radius, int dstStep, int srcStep){
62 int x;
63 const int length= radius*2 + 1;
64 const int inv= ((1<<16) + length/2)/length;
65
66 int sum= 0;
67
68 for(x=0; x<radius; x++){
69 sum+= src[x*srcStep]<<1;
70 }
71 sum+= src[radius*srcStep];
72
73 for(x=0; x<=radius; x++){
74 sum+= src[(radius+x)*srcStep] - src[(radius-x)*srcStep];
75 dst[x*dstStep]= (sum*inv + (1<<15))>>16;
76 }
77
78 for(; x<w-radius; x++){
79 sum+= src[(radius+x)*srcStep] - src[(x-radius-1)*srcStep];
80 dst[x*dstStep]= (sum*inv + (1<<15))>>16;
81 }
82
83 for(; x<w; x++){
84 sum+= src[(2*w-radius-x-1)*srcStep] - src[(x-radius-1)*srcStep];
85 dst[x*dstStep]= (sum*inv + (1<<15))>>16;
86 }
87 }
88
89 static inline void blur2(uint8_t *dst, uint8_t *src, int w, int radius, int power, int dstStep, int srcStep){
90 uint8_t temp[2][4096];
91 uint8_t *a= temp[0], *b=temp[1];
92
93 if(radius){
94 blur(a, src, w, radius, 1, srcStep);
95 for(; power>2; power--){
96 uint8_t *c;
97 blur(b, a, w, radius, 1, 1);
98 c=a; a=b; b=c;
99 }
100 if(power>1)
101 blur(dst, a, w, radius, dstStep, 1);
102 else{
103 int i;
104 for(i=0; i<w; i++)
105 dst[i*dstStep]= a[i];
106 }
107 }else{
108 int i;
109 for(i=0; i<w; i++)
110 dst[i*dstStep]= src[i*srcStep];
111 }
112 }
113
114 static void hBlur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, int radius, int power){
115 int y;
116
117 if(radius==0 && dst==src) return;
118
119 for(y=0; y<h; y++){
120 blur2(dst + y*dstStride, src + y*srcStride, w, radius, power, 1, 1);
121 }
122 }
123
124 //FIXME optimize (x before y !!!)
125 static void vBlur(uint8_t *dst, uint8_t *src, int w, int h, int dstStride, int srcStride, int radius, int power){
126 int x;
127
128 if(radius==0 && dst==src) return;
129
130 for(x=0; x<w; x++){
131 blur2(dst + x, src + x, h, radius, power, dstStride, srcStride);
132 }
133 }
134
135 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
136 int cw= mpi->w >> mpi->chroma_x_shift;
137 int ch= mpi->h >> mpi->chroma_y_shift;
138
139 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
140 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
141 mpi->w,mpi->h);
142
143 assert(mpi->flags&MP_IMGFLAG_PLANAR);
144
145 hBlur(dmpi->planes[0], mpi->planes[0], mpi->w,mpi->h,
146 dmpi->stride[0], mpi->stride[0], vf->priv->lumaParam.radius, vf->priv->lumaParam.power);
147 hBlur(dmpi->planes[1], mpi->planes[1], cw,ch,
148 dmpi->stride[1], mpi->stride[1], vf->priv->chromaParam.radius, vf->priv->chromaParam.power);
149 hBlur(dmpi->planes[2], mpi->planes[2], cw,ch,
150 dmpi->stride[2], mpi->stride[2], vf->priv->chromaParam.radius, vf->priv->chromaParam.power);
151
152 vBlur(dmpi->planes[0], dmpi->planes[0], mpi->w,mpi->h,
153 dmpi->stride[0], dmpi->stride[0], vf->priv->lumaParam.radius, vf->priv->lumaParam.power);
154 vBlur(dmpi->planes[1], dmpi->planes[1], cw,ch,
155 dmpi->stride[1], dmpi->stride[1], vf->priv->chromaParam.radius, vf->priv->chromaParam.power);
156 vBlur(dmpi->planes[2], dmpi->planes[2], cw,ch,
157 dmpi->stride[2], dmpi->stride[2], vf->priv->chromaParam.radius, vf->priv->chromaParam.power);
158
159 return vf_next_put_image(vf,dmpi);
160 }
161
162 //===========================================================================//
163
164 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
165 switch(fmt)
166 {
167 case IMGFMT_YV12:
168 case IMGFMT_I420:
169 case IMGFMT_IYUV:
170 case IMGFMT_YVU9:
171 case IMGFMT_444P:
172 case IMGFMT_422P:
173 case IMGFMT_411P:
174 return vf_next_query_format(vf, fmt);
175 }
176 return 0;
177 }
178
179 static int open(vf_instance_t *vf, char* args){
180 char *pos, *max;
181 int e;
182
183 vf->config=config;
184 vf->put_image=put_image;
185 // vf->get_image=get_image;
186 vf->query_format=query_format;
187 vf->priv=malloc(sizeof(struct vf_priv_s));
188 memset(vf->priv, 0, sizeof(struct vf_priv_s));
189
190 if(args==NULL) return 0;
191
192 e=sscanf(args, "%d:%d:%d:%d",
193 &vf->priv->lumaParam.radius,
194 &vf->priv->lumaParam.power,
195 &vf->priv->chromaParam.radius,
196 &vf->priv->chromaParam.power
197 );
198
199 if(e==2){
200 vf->priv->chromaParam.radius= vf->priv->lumaParam.radius;
201 vf->priv->chromaParam.power = vf->priv->lumaParam.power;
202 }else if(e!=4)
203 return 0;
204
205 if(vf->priv->lumaParam.radius < 0) return 0;
206 if(vf->priv->chromaParam.radius < 0) return 0;
207
208 return 1;
209 }
210
211 vf_info_t vf_info_boxblur = {
212 "box blur",
213 "boxblur",
214 "Michael Niedermayer",
215 "",
216 open
217 };
218
219 //===========================================================================//