2 // from ffmpeg vf_boxblur
4 template<class dst_t, class src_t> static inline
5 void blurt(dst_t *dst, int dst_step, src_t *src, int src_step,
6 int len, int radius, float s)
10 if( radius > n ) radius = n;
11 const int length = radius*2 + 1;
12 const int inv = s * ((1<<16) + length/2)/length;
13 int x, sum = src[radius*src_step];
15 for( x=0; x<radius; ++x )
16 sum += src[x*src_step]<<1;
17 sum = sum*inv + (1<<15);
18 for( x=0; x<=radius; ++x ) {
19 sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv;
20 dst[x*dst_step] = sum>>16;
24 sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv;
25 dst[x*dst_step] = sum >>16;
27 for ( ; x<len; ++x ) {
28 sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv;
29 dst[x*dst_step] = sum>>16;
32 // specialize const float *src
33 template<class dst_t> static inline
34 void blurt(dst_t *dst, int dst_step, const float *src, int src_step,
35 int len, int radius, float s)
37 const float length = radius*2 + 1;
38 const float inv = s / length;
40 float sum = src[radius*src_step];
41 for( x=0; x<radius; ++x )
42 sum += src[x*src_step]*2;
44 for( x=0; x<=radius; ++x ) {
45 sum += (src[(radius+x)*src_step] - src[(radius-x)*src_step])*inv;
46 dst[x*dst_step] = sum;
50 sum += (src[(radius+x)*src_step] - src[(x-radius-1)*src_step])*inv;
51 dst[x*dst_step] = sum;
53 for ( ; x<len; ++x ) {
54 sum += (src[(2*len-radius-x-1)*src_step] - src[(x-radius-1)*src_step])*inv;
55 dst[x*dst_step] = sum;
59 // s scales dst=src first pass
60 template<class dst_t, class src_t> static inline
61 void blur_power(dst_t *dst, int dst_step, src_t *src, int src_step,
62 int len, int radius, int power, float s)
64 dst_t atemp[len], btemp[len];
65 dst_t *a = atemp, *b = btemp;
66 blurt(a, 1, src, src_step, len, radius, s);
67 while( power-- > 2 ) {
68 blurt(b, 1, (const dst_t*)a, 1, len, radius, 1);
69 dst_t *t = a; a = b; b = t;
72 blurt(dst, dst_step, (const dst_t*)a, 1, len, radius, 1);
74 for( int i = 0; i<len; ++i ) dst[i*dst_step] = a[i];
78 BoxBlurPackage::BoxBlurPackage()
83 BoxBlurUnit::BoxBlurUnit(BoxBlur *box_blur)
84 : LoadClient(box_blur)
88 template<class dst_t, class src_t>
89 void BoxBlurUnit::blurt_package(LoadPackage *package)
91 BoxBlur *box_blur = (BoxBlur *)server;
92 src_t *src_data = (src_t *)box_blur->src_data;
93 dst_t *dst_data = (dst_t *)box_blur->dst_data;
94 int radius = box_blur->radius;
95 int power = box_blur->power;
96 int vlen = box_blur->vlen;
97 int c0 = box_blur->c0, c1 = box_blur->c1;
98 int src_ustep = box_blur->src_ustep;
99 int dst_ustep = box_blur->dst_ustep;
100 int src_vstep = box_blur->src_vstep;
101 int dst_vstep = box_blur->dst_vstep;
102 BoxBlurPackage *pkg = (BoxBlurPackage*)package;
103 int u1 = pkg->u1, u2 = pkg->u2, v = box_blur->v1;
105 if( sizeof(src_t) != sizeof(dst_t) ) {
106 switch( sizeof(dst_t) ) {
107 case 1: s = sizeof(src_t)==2 ? 1/256. : 255.; break;
108 case 2: s = sizeof(src_t)==1 ? 256. : 65535.; break;
109 case 4: s = sizeof(src_t)==1 ? 1/256. : 1/65535.; break;
112 for( int u=u1; u<u2; ++u ) {
113 src_t *sp = src_data + u*src_ustep + v*src_vstep;
114 dst_t *dp = dst_data + u*dst_ustep + v*dst_vstep;
115 for( int c=c0; c<=c1; ++c ) {
116 blur_power(dp+c, dst_vstep, sp+c, src_vstep,
117 vlen, radius, power, s);
122 void BoxBlurUnit::process_package(LoadPackage *package)
124 BoxBlur *box_blur = (BoxBlur *)server;
125 int src_bpc = box_blur->src_bpc, dst_bpc = box_blur->dst_bpc;
127 case 1: switch( dst_bpc ) {
128 case 1: blurt_package<uint8_t, const uint8_t>(package); break;
129 case 2: blurt_package<uint16_t, const uint8_t>(package); break;
130 case 4: blurt_package<float, const uint8_t>(package); break;
133 case 2: switch( dst_bpc ) {
134 case 1: blurt_package<uint8_t, const uint16_t>(package); break;
135 case 2: blurt_package<uint16_t, const uint16_t>(package); break;
136 case 4: blurt_package<float, const uint16_t>(package); break;
139 case 4: switch( dst_bpc ) {
140 case 1: blurt_package<uint8_t, const float>(package); break;
141 case 2: blurt_package<uint16_t, const float>(package); break;
142 case 4: blurt_package<float, const float>(package); break;
148 BoxBlur::BoxBlur(int cpus)
149 : LoadServer(cpus, cpus)
156 LoadClient* BoxBlur::new_client() { return new BoxBlurUnit(this); }
157 LoadPackage* BoxBlur::new_package() { return new BoxBlurPackage(); }
159 void BoxBlur::init_packages()
162 for( int i=0,n=LoadServer::get_total_packages(); i<n; ) {
163 BoxBlurPackage *pkg = (BoxBlurPackage*)get_package(i);
165 u = u1 + (++i * ulen) / n;
170 //dst can equal src, requires geom(dst)==geom(src)
171 //uv: 0=hblur, 1=vblur; comp: -1=rgb,0=r,1=g,2=b
172 void BoxBlur::process(VFrame *dst, VFrame *src, int uv,
173 int radius, int power, int comp)
175 this->radius = radius;
178 this->u1 = !uv ? y1 : x1;
179 this->u2 = !uv ? y2 : x2;
180 this->v1 = !uv ? x1 : y1;
181 this->v2 = !uv ? x2 : y2;
182 if( (ulen = u2 - u1) <= 0 ) return;
183 if( (vlen = v2 - v1) <= 0 ) return;
184 c0 = comp<0 ? 0 : comp;
185 c1 = comp<0 ? 2 : comp;
186 src_data = src->get_data();
187 dst_data = dst->get_data();
188 int src_pixsz = BC_CModels::calculate_pixelsize(src->get_color_model());
189 int src_comps = BC_CModels::components(src->get_color_model());
190 src_bpc = src_pixsz / src_comps;
191 int dst_pixsz = BC_CModels::calculate_pixelsize(dst->get_color_model());
192 int dst_comps = BC_CModels::components(dst->get_color_model());
193 dst_bpc = dst_pixsz / dst_comps;
194 int dst_linsz = dst->get_bytes_per_line() / dst_bpc;
195 int src_linsz = src->get_bytes_per_line() / src_bpc;
196 src_ustep = !uv ? src_linsz : src_comps;
197 dst_ustep = !uv ? dst_linsz : dst_comps;
198 src_vstep = !uv ? src_comps : src_linsz;
199 dst_vstep = !uv ? dst_comps : dst_linsz;
204 int BoxBlur::init_box(VFrame *dst, int x, int y, int w, int h)
210 int dw = dst->get_w(), dh = dst->get_h();
211 if( x2 > dw ) x2 = dw;
212 if( y2 > dh ) y2 = dh;
213 return x2 > x1 && y2 > y1 ? 1 : 0;
216 void BoxBlur::hblur(VFrame *dst, VFrame *src, int radius, int power, int comp,
217 int x, int y, int w, int h)
219 if( !init_box(dst, x, y, w, h) ) return;
220 process(dst, src, 0, radius, power, comp);
222 void BoxBlur::hblur(VFrame *dst, VFrame *src, int radius, int power, int comp)
224 return hblur(dst, src, radius, power, comp,
225 0, 0, dst->get_w(), dst->get_h());
228 void BoxBlur::vblur(VFrame *dst, VFrame *src, int radius, int power, int comp,
229 int x, int y, int w, int h)
231 if( !init_box(dst, x, y, w, h) ) return;
232 process(dst, src, 1, radius, power, comp);
234 void BoxBlur::vblur(VFrame *dst, VFrame *src, int radius, int power, int comp)
236 return vblur(dst, src, radius, power, comp,
237 0, 0, dst->get_w(), dst->get_h());
240 void BoxBlur::blur(VFrame *dst, VFrame *src, int radius, int power, int comp,
241 int x, int y, int w, int h)
243 if( !init_box(dst, x, y, w, h) ) return;
244 process(dst, src, 0, radius, power, comp);
245 process(dst, dst, 1, radius, power, comp);
247 void BoxBlur::blur(VFrame *dst, VFrame *src, int radius, int power, int comp)
249 return blur(dst, src, radius, power, comp,
250 0, 0, dst->get_w(), dst->get_h());