Debian Codegraph @

src/filter/blur/IIRblur.c

raw symbols SCIP
1 /*
2 IIRblur.c
3
4 This Frei0r plugin implements fast IIR blurring of video
5
6 Version 0.1 jul 2011
7
8 Copyright (C) 2011 Marko Cebokli http://lea.hamradio.si/~s57uuu
9
10
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 */
26
27
28 //compile: gcc -c -fPIC -Wall IIRblur.c -o IIRblur.o
29 //link: gcc -shared -o IIRblur.so IIRblur.o
30
31 //stdio samo za debug izpise
32 //#include <stdio.h>
33 #include <frei0r.h>
34 #include <stdlib.h>
35 #include <math.h>
36 #include <assert.h>
37 #include <inttypes.h>
38 #include <string.h>
39
40 double PI=3.14159265358979;
41
42 typedef struct
43 {
44 float r;
45 float g;
46 float b;
47 float a;
48 } float_rgba;
49
50 #include "fibe.h"
51
52
53 //----------------------------------------
54 //struktura za instanco efekta
55 typedef struct
56 {
57 //status
58 int h;
59 int w;
60
61 //parameters
62 float am; //amount of blur
63 int ty; //type of blur [0..2]
64 int ec; //edge compensation (BOOL)
65
66 //video buffers
67 float_rgba *img;
68
69 //internal variables
70 float a1,a2,a3;
71 float rd1,rd2,rs1,rs2,rc1,rc2;
72
73 } inst;
74
75 //--------------------------------------------------------
76 //Aitken-Neville interpolacija iz 4 tock (tretjega reda)
77 //t = stevilo tock v arrayu
78 //array xt naj bo v rastocem zaporedju, lahko neekvidistanten
79 float AitNev3(int t, float xt[], float yt[], float x)
80 {
81 float p[10];
82 int i,j,m;
83
84 if ((x<xt[0])||(x>xt[t-1]))
85 {
86 // printf("\n\n x=%f je izven mej tabele!",x);
87 // Return a reasonable value instead of dividing by zero
88 if (x<xt[0]) return yt[0];
89 else return yt[t-1];
90 }
91
92 //poisce, katere tocke bo uporabil
93 m=0; while (x>xt[m++]);
94 m=m-4/2-1; if (m<0) m=0; if ((m+4)>(t-1)) m=t-4;
95
96 for (i=0;i<4;i++)
97 p[i]=yt[i+m];
98
99 for (j=1;j<4;j++)
100 for (i=(4-1);i>=j;i--)
101 {
102 // Check for division by zero
103 float denominator = xt[i+m]-xt[i-j+m];
104 if (denominator == 0.0f) {
105 // If denominator is zero, skip this iteration to avoid undefined behavior
106 continue;
107 }
108 p[i]=p[i]+(x-xt[i+m])/denominator*(p[i]-p[i-1]);
109 }
110 return p[4-1];
111 }
112
113
114 //-----------------------------------------------------
115 //stretch [0...1] to parameter range [min...max] linear
116 float map_value_forward(double v, float min, float max)
117 {
118 return min+(max-min)*v;
119 }
120
121 //-----------------------------------------------------
122 //collapse from parameter range [min...max] to [0...1] linear
123 double map_value_backward(float v, float min, float max)
124 {
125 return (v-min)/(max-min);
126 }
127
128 //-----------------------------------------------------
129 //stretch [0...1] to parameter range [min...max] logarithmic
130 //min and max must be positive!
131 float map_value_forward_log(double v, float min, float max)
132 {
133 float sr,k;
134
135 sr=sqrtf(min*max);
136 k=2.0*log(max/sr);
137 return sr*expf(k*(v-0.5));
138 }
139
140 //-----------------------------------------------------
141 //collapse from parameter range [min...max] to [0...1] logarithmic
142 //min and max must be positive!
143 double map_value_backward_log(float v, float min, float max)
144 {
145 float sr,k;
146
147 sr=sqrtf(min*max);
148 k=2.0*log(max/sr);
149 return logf(v/sr)/k+0.5;
150 }
151
152 //***********************************************
153 // OBVEZNE FREI0R FUNKCIJE
154
155 //-----------------------------------------------
156 int f0r_init()
157 {
158 return 1;
159 }
160
161 //------------------------------------------------
162 void f0r_deinit()
163 {
164 }
165
166 //-----------------------------------------------
167 void f0r_get_plugin_info(f0r_plugin_info_t* info)
168 {
169
170 info->name="IIR blur";
171 info->author="Marko Cebokli";
172 info->plugin_type=F0R_PLUGIN_TYPE_FILTER;
173 info->color_model=F0R_COLOR_MODEL_RGBA8888;
174 info->frei0r_version=FREI0R_MAJOR_VERSION;
175 info->major_version=0;
176 info->minor_version=2;
177 info->num_params=3;
178 info->explanation="Three types of fast IIR blurring";
179 }
180
181 //--------------------------------------------------
182 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
183 {
184 switch(param_index)
185 {
186 case 0:
187 info->name = "Amount";
188 info->type = F0R_PARAM_DOUBLE;
189 info->explanation = "Amount of blur";
190 break;
191 case 1:
192 info->name = "Type";
193 info->type = F0R_PARAM_DOUBLE;
194 info->explanation = "Blur type";
195 break;
196 case 2:
197 info->name = "Edge";
198 info->type = F0R_PARAM_BOOL;
199 info->explanation = "Edge compensation";
200 break;
201 }
202 }
203
204 //----------------------------------------------
205 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
206 {
207 inst *in;
208
209 in=calloc(1,sizeof(inst));
210 in->w=width;
211 in->h=height;
212
213 in->img=calloc(width*height*4,sizeof(float));
214
215 in->am=map_value_forward_log(0.2, 0.5, 100.0);
216 in->a1=-0.796093; in->a2=0.186308;
217 in->ty=1;
218 in->ec=1;
219
220 return (f0r_instance_t)in;
221 }
222
223 //---------------------------------------------------
224 void f0r_destruct(f0r_instance_t instance)
225 {
226 inst *in;
227
228 in=(inst*)instance;
229
230 free(in->img);
231
232 free(instance);
233 }
234
235 //-----------------------------------------------------
236 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t parm, int param_index)
237 {
238 inst *p;
239 double tmpf;
240 int chg,tmpi;
241 float a0,b0,b1,b2,f,q,s;
242
243 float am1[]={0.499999,0.7,1.0,1.5,2.0,3.0,4.0,5.0,7.0,10.0,
244 15.0,20.0,30.0,40.0,50.0,70.0,100.0,150.0,200.00001};
245 //float iir2f[]={0.448,0.4,0.31,0.25,0.21,0.15,0.1,0.075,
246 // 0.055,0.039,0.026,0.02,0.013,0.01,0.008,0.006,
247 // 0.0042,0.0029,0.00205};
248 //float iir2q[]={0.53,0.53,0.54,0.54,0.54,0.55,0.6,0.7,0.7,
249 // 0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7,0.7};
250 float iir2q[]={0.53,0.53,0.54,0.54,0.54,0.55,0.6,0.6,0.6,
251 0.6,0.6,0.6,0.6,0.6,0.6,0.6,0.6,0.6,0.6};
252 //float iir1a1[]={0.167,0.3,0.5,0.65,0.7,0.8,0.88,0.92,0.95,
253 // 0.96,0.97,0.98,0.985,0.988,0.99,0.992,0.993,0.9955,
254 // 0.997};
255
256 float iir1a1[]={0.138,0.24,0.34,0.45,0.55, 0.65,0.728,0.775,0.834,0.88,
257 0.92,0.937,0.958,0.968,0.9745,
258 0.98,0.986,0.991,0.9931}; //po sigmi
259
260 float iir2f[]={0.475,0.39,0.325,0.26,0.21,
261 0.155,0.112,0.0905,0.065,0.0458,
262 0.031,0.0234,0.01575,0.0118,0.0093,
263 0.00725,0.00505,0.0033,0.0025}; //po sigmi
264
265 float iir3si[]={0.5,0.7,1.0,1.5,2.0,
266 3.0,4.0,5.0,7.0,10.0,
267 15.0,20.0,30.0,40.0,50.0,
268 70.0,100.0,150.0,186.5};
269
270
271 p=(inst*)instance;
272
273 chg=0;
274 switch(param_index)
275 {
276 case 0:
277 // tmpf=map_value_forward(*((double*)parm), 0.5, 100.0);
278 if (*((double*)parm) == 0.0)
279 tmpf = 0.0;
280 else
281 tmpf=map_value_forward_log(*((double*)parm), 0.5, 100.0);
282 if (tmpf!=p->am) chg=1;
283 p->am=tmpf;
284 break;
285 case 1:
286 tmpf=*((double*)parm);
287 if (tmpf>=1.0)
288 tmpi=(int)tmpf;
289 else
290 tmpi = map_value_forward(tmpf, 0.0, 2.9999);
291 if ((tmpi<0)||(tmpi>2.0)) break;
292 if (p->ty != tmpi) chg=1;
293 p->ty = tmpi;
294 break;
295 case 2:
296 p->ec=map_value_forward(*((double*)parm), 0.0, 1.0); //BOOL!!
297 break;
298 }
299
300 if (chg==0) return;
301
302 switch(p->ty)
303 {
304 case 0: //FIBE-1
305 p->a1=AitNev3(19, am1, iir1a1, p->am);
306 //printf("Set parm FIBE-1 a1=%f (p->am=%f)\n",p->a1,p->am);
307 break;
308 case 1: //FIBE-2
309 f=AitNev3(19, am1, iir2f, p->am);
310 q=AitNev3(19, am1, iir2q, p->am);
311 calcab_lp1(f, q, &a0, &p->a1, &p->a2, &b0, &b1, &b2);
312 p->a1=p->a1/a0; p->a2=p->a2/a0;
313 rep(-0.5, 0.5, 0.0, &p->rd1, &p->rd2, 256, p->a1, p->a2);
314 rep(1.0, 1.0, 0.0, &p->rs1, &p->rs2, 256, p->a1, p->a2);
315 rep(0.0, 0.0, 1.0, &p->rc1, &p->rc2, 256, p->a1, p->a2);
316 //printf("Set parm FIBE-2 a1=%f a2=%f\n",p->a1,p->a2);
317 break;
318 case 2: //FIBE-3
319 s=AitNev3(19, am1, iir3si, p->am);
320 young_vliet(s, &a0, &p->a1, &p->a2, &p->a3);
321 p->a1=-p->a1/a0;
322 p->a2=-p->a2/a0;
323 p->a3=-p->a3/a0;
324 //printf("Set parm FIBE-3 a1=%f a2=%f a3=%f\n",p->a1,p->a2,p->a3);
325 break;
326 }
327 }
328
329 //--------------------------------------------------
330 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
331 {
332 inst *p;
333
334 p=(inst*)instance;
335
336 switch(param_index)
337 {
338 case 0:
339 // *((double*)param)=map_value_backward(p->am, 0.5, 100.0);
340 *((double*)param)=map_value_backward_log(p->am, 0.5, 100.0);
341 break;
342 case 1:
343 *((double*)param)=map_value_backward(p->ty, 0.0, 2.9999);
344 break;
345 case 2:
346 *((double*)param)=map_value_backward(p->ec, 0.0, 1.0);//BOOL!!
347 break;
348 }
349 }
350
351 //-------------------------------------------------
352 void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)
353 {
354 inst *in;
355 int i;
356
357 assert(instance);
358 in=(inst*)instance;
359
360 if (in->am==0.0) //zero blur, just copy and return
361 {
362 memcpy(outframe, inframe, in->w * in->h * sizeof(uint32_t));
363 return;
364 }
365 //do the blur
366 switch(in->ty)
367 {
368 case 0:
369 fibe1o_8(inframe, outframe, in->img, in->w, in->h, in->a1, in->ec);
370 break;
371 case 1:
372 fibe2o_8(inframe, outframe, in->img, in->w, in->h, in->a1, in->a2, in->rd1, in->rd2, in->rs1, in->rs2, in->rc1, in->rc2, in->ec);
373 break;
374 case 2:
375 fibe3_8(inframe, outframe, in->img, in->w, in->h, in->a1, in->a2, in->a3, in->ec);
376 // The bottom 3 lines were not updated, and outframe may be initialized with garbage.
377 // Copy the 4th line from the bottom to the bottom 3 lines.
378 for (i = 0; i < 3; i++)
379 memcpy(&outframe[in->w * (in->h - 3 + i)], &outframe[in->w * (in->h - 4)], in->w * 4);
380 break;
381 }
382 //copy alpha
383 for (i=0;i<in->w*in->h;i++)
384 {
385 outframe[i]=(outframe[i]&0x00FFFFFF) | (inframe[i]&0xFF000000);
386 }
387 }