Debian Codegraph @

src/filter/denoise/denoise_hqdn3d.c

raw symbols SCIP
1 /*
2 hqdn3d.c
3
4 This frei0r plugin is a port of Mplayer's hqdb3d denoiser
5 original by Daniel Moreno <[email protected]>
6
7 Version 0.1 nov 2010
8
9 Copyright (C) 2010 Marko Cebokli http://lea.hamradio.si/~s57uuu
10
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26 */
27
28
29 //compile: gcc -c -fPIC -Wall hqdn3d.c -o hqdn3d.o
30 //link: gcc -shared -o hqdn3d.so hqdn3d.o
31
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
39 #define MIN_MATRIX_SIZE 3
40 #define MAX_MATRIX_SIZE 63
41
42
43 //----------------------------------------
44 typedef struct {
45 int Coefs[4][512*16];
46 unsigned int *Line;
47 unsigned short *Frame[3];
48 }vf_priv_s;
49
50 //----------------------------------------
51 //struktura za instanco efekta
52 typedef struct
53 {
54 int h;
55 int w;
56
57 double LumSpac,LumTmp;
58 vf_priv_s vps;
59
60 unsigned char *Rplani,*Gplani,*Bplani,*Rplano,*Gplano,*Bplano;
61 } inst;
62
63
64 //========================================================
65 //functions LowPassMul, deNoiseTemporal, deNoiseSpacial,
66 //deNoise and PrecalaCoefs are from Mplayer "hqdn3d" filter
67 //by Daniel Moreno <[email protected]>
68
69 static inline unsigned int LowPassMul(unsigned int PrevMul, unsigned int CurrMul, int* Coef){
70 // int dMul= (PrevMul&0xFFFFFF)-(CurrMul&0xFFFFFF);
71 int dMul= PrevMul-CurrMul;
72 unsigned int d=((dMul+0x10007FF)>>12);
73 return CurrMul + Coef[d];
74 }
75
76 void deNoiseTemporal(
77 unsigned char *Frame, // mpi->planes[x]
78 unsigned char *FrameDest, // dmpi->planes[x]
79 unsigned short *FrameAnt,
80 int W, int H, int sStride, int dStride,
81 int *Temporal)
82 {
83 long X, Y;
84 unsigned int PixelDst;
85
86 for (Y = 0; Y < H; Y++){
87 for (X = 0; X < W; X++){
88 PixelDst = LowPassMul(FrameAnt[X]<<8, Frame[X]<<16, Temporal);
89 FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
90 FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
91 }
92 Frame += sStride;
93 FrameDest += dStride;
94 FrameAnt += W;
95 }
96 }
97
98 void deNoiseSpacial(
99 unsigned char *Frame, // mpi->planes[x]
100 unsigned char *FrameDest, // dmpi->planes[x]
101 unsigned int *LineAnt, // vf->priv->Line (width bytes)
102 int W, int H, int sStride, int dStride,
103 int *Horizontal, int *Vertical)
104 {
105 long X, Y;
106 long sLineOffs = 0, dLineOffs = 0;
107 unsigned int PixelAnt;
108 unsigned int PixelDst;
109
110 /* First pixel has no left nor top neighbor. */
111 PixelDst = LineAnt[0] = PixelAnt = Frame[0]<<16;
112 FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
113
114 /* First line has no top neighbor, only left. */
115 for (X = 1; X < W; X++){
116 PixelDst = LineAnt[X] = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
117 FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
118 }
119
120 for (Y = 1; Y < H; Y++){
121 unsigned int PixelAnt;
122 sLineOffs += sStride, dLineOffs += dStride;
123 /* First pixel on each line doesn't have previous pixel */
124 PixelAnt = Frame[sLineOffs]<<16;
125 PixelDst = LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
126 FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
127
128 for (X = 1; X < W; X++){
129 unsigned int PixelDst;
130 /* The rest are normal */
131 PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
132 PixelDst = LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
133 FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
134 }
135 }
136 }
137
138 void deNoise(unsigned char *Frame, // mpi->planes[x]
139 unsigned char *FrameDest, // dmpi->planes[x]
140 unsigned int *LineAnt, // vf->priv->Line (width bytes)
141 unsigned short **FrameAntPtr,
142 int W, int H, int sStride, int dStride,
143 int *Horizontal, int *Vertical, int *Temporal)
144 {
145 long X, Y;
146 long sLineOffs = 0, dLineOffs = 0;
147 unsigned int PixelAnt;
148 unsigned int PixelDst;
149 unsigned short* FrameAnt=(*FrameAntPtr);
150
151 if(!FrameAnt){
152 (*FrameAntPtr)=FrameAnt=malloc(W*H*sizeof(unsigned short));
153 for (Y = 0; Y < H; Y++){
154 unsigned short* dst=&FrameAnt[Y*W];
155 unsigned char* src=Frame+Y*sStride;
156 for (X = 0; X < W; X++) dst[X]=src[X]<<8;
157 }
158 }
159
160 if(!Horizontal[0] && !Vertical[0]){
161 deNoiseTemporal(Frame, FrameDest, FrameAnt,
162 W, H, sStride, dStride, Temporal);
163 return;
164 }
165 if(!Temporal[0]){
166 deNoiseSpacial(Frame, FrameDest, LineAnt,
167 W, H, sStride, dStride, Horizontal, Vertical);
168 return;
169 }
170
171 /* First pixel has no left nor top neighbor. Only previous frame */
172 LineAnt[0] = PixelAnt = Frame[0]<<16;
173 PixelDst = LowPassMul(FrameAnt[0]<<8, PixelAnt, Temporal);
174 FrameAnt[0] = ((PixelDst+0x1000007F)>>8);
175 FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
176
177 /* First line has no top neighbor. Only left one for each pixel and
178 * last frame */
179 for (X = 1; X < W; X++){
180 LineAnt[X] = PixelAnt = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
181 PixelDst = LowPassMul(FrameAnt[X]<<8, PixelAnt, Temporal);
182 FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
183 FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
184 }
185
186 for (Y = 1; Y < H; Y++){
187 unsigned int PixelAnt;
188 unsigned short* LinePrev=&FrameAnt[Y*W];
189 sLineOffs += sStride, dLineOffs += dStride;
190 /* First pixel on each line doesn't have previous pixel */
191 PixelAnt = Frame[sLineOffs]<<16;
192 LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
193 PixelDst = LowPassMul(LinePrev[0]<<8, LineAnt[0], Temporal);
194 LinePrev[0] = ((PixelDst+0x1000007F)>>8);
195 FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
196
197 for (X = 1; X < W; X++){
198 unsigned int PixelDst;
199 /* The rest are normal */
200 PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
201 LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
202 PixelDst = LowPassMul(LinePrev[X]<<8, LineAnt[X], Temporal);
203 LinePrev[X] = ((PixelDst+0x1000007F)>>8);
204 FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
205 }
206 }
207 }
208
209 #define ABS(A) ( (A) > 0 ? (A) : -(A) )
210
211 static void PrecalcCoefs(int *Ct, double Dist25)
212 {
213 int i;
214 double Gamma, Simil, C;
215
216 Gamma = log(0.25) / log(1.0 - Dist25/255.0 - 0.00001);
217
218 for (i = -255*16; i <= 255*16; i++)
219 {
220 Simil = 1.0 - ABS(i) / (16*255.0);
221 C = pow(Simil, Gamma) * 65536.0 * (double)i / 16.0;
222 Ct[16*256+i] = (C<0) ? (C-0.5) : (C+0.5);
223 }
224
225 Ct[0] = (Dist25 != 0);
226 }
227
228 //end of hqdn3d functions
229 //===============================================
230
231
232
233 //-----------------------------------------------------
234 //stretch [0...1] to parameter range [min...max] linear
235 float map_value_forward(double v, float min, float max)
236 {
237 return min+(max-min)*v;
238 }
239
240 //-----------------------------------------------------
241 //collapse from parameter range [min...max] to [0...1] linear
242 double map_value_backward(float v, float min, float max)
243 {
244 return (v-min)/(max-min);
245 }
246
247 //***********************************************
248 // OBVEZNE FREI0R FUNKCIJE
249
250 //-----------------------------------------------
251 int f0r_init()
252 {
253 return 1;
254 }
255
256 //------------------------------------------------
257 void f0r_deinit()
258 {
259 }
260
261 //-----------------------------------------------
262 void f0r_get_plugin_info(f0r_plugin_info_t* info)
263 {
264
265 info->name="hqdn3d";
266 info->author="Marko Cebokli, Daniel Moreno";
267 info->plugin_type=F0R_PLUGIN_TYPE_FILTER;
268 info->color_model=F0R_COLOR_MODEL_RGBA8888;
269 info->frei0r_version=FREI0R_MAJOR_VERSION;
270 info->major_version=0;
271 info->minor_version=1;
272 info->num_params=2;
273 info->explanation="High quality 3D denoiser from Mplayer";
274 }
275
276 //--------------------------------------------------
277 void f0r_get_param_info(f0r_param_info_t* info, int param_index)
278 {
279 switch(param_index)
280 {
281 case 0:
282 info->name = "Spatial";
283 info->type = F0R_PARAM_DOUBLE;
284 info->explanation = "Amount of spatial filtering";
285 break;
286 case 1:
287 info->name = "Temporal";
288 info->type = F0R_PARAM_DOUBLE;
289 info->explanation = "Amount of temporal filtering";
290 break;
291 }
292 }
293
294 //----------------------------------------------
295 f0r_instance_t f0r_construct(unsigned int width, unsigned int height)
296 {
297 inst *in;
298
299 in=calloc(1,sizeof(inst));
300 in->w=width;
301 in->h=height;
302
303 in->LumSpac=4;
304 in->LumTmp=6;
305 in->vps.Line=calloc(width,sizeof(int));
306 in->Rplani=calloc(width*height,sizeof(unsigned char));
307 in->Gplani=calloc(width*height,sizeof(unsigned char));
308 in->Bplani=calloc(width*height,sizeof(unsigned char));
309 in->Rplano=calloc(width*height,sizeof(unsigned char));
310 in->Gplano=calloc(width*height,sizeof(unsigned char));
311 in->Bplano=calloc(width*height,sizeof(unsigned char));
312
313 PrecalcCoefs(in->vps.Coefs[0],in->LumSpac);
314 PrecalcCoefs(in->vps.Coefs[1],in->LumTmp);
315
316 return (f0r_instance_t)in;
317 }
318
319 //---------------------------------------------------
320 void f0r_destruct(f0r_instance_t instance)
321 {
322 inst *in;
323
324 in=(inst*)instance;
325
326 free(in->vps.Line);
327 free(in->vps.Frame[0]);
328 free(in->vps.Frame[1]);
329 free(in->vps.Frame[2]);
330 free(in->Rplani);
331 free(in->Gplani);
332 free(in->Bplani);
333 free(in->Rplano);
334 free(in->Gplano);
335 free(in->Bplano);
336
337 free(instance);
338 }
339
340 //-----------------------------------------------------
341 void f0r_set_param_value(f0r_instance_t instance, f0r_param_t parm, int param_index)
342 {
343 inst *p;
344 double tmpf;
345 int chg;
346
347 p=(inst*)instance;
348
349 chg=0;
350 switch(param_index)
351 {
352 case 0:
353 tmpf=map_value_forward(*((double*)parm), 0.0, 100.0);
354 if (tmpf!=p->LumSpac) chg=1;
355 p->LumSpac=tmpf;
356 break;
357 case 1:
358 tmpf=map_value_forward(*((double*)parm), 0.0, 100.0);
359 if (tmpf!=p->LumTmp) chg=1;
360 p->LumTmp=tmpf;
361 break;
362 }
363
364 if (chg==0) return;
365
366 PrecalcCoefs(p->vps.Coefs[0],p->LumSpac);
367 PrecalcCoefs(p->vps.Coefs[1],p->LumTmp);
368
369 }
370
371 //--------------------------------------------------
372 void f0r_get_param_value(f0r_instance_t instance, f0r_param_t param, int param_index)
373 {
374 inst *p;
375
376 p=(inst*)instance;
377
378 switch(param_index)
379 {
380 case 0:
381 *((double*)param)=map_value_backward(p->LumSpac, 0.0, 100.0);//BOOL!!
382 break;
383 case 1:
384 *((double*)param)=map_value_backward(p->LumTmp, 0.0, 100.0);//BOOL!!
385 break;
386 }
387 }
388
389 //-------------------------------------------------
390 void f0r_update(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)
391 {
392 inst *in;
393 int i;
394
395 assert(instance);
396 in=(inst*)instance;
397
398 //Frei0r works with packed color, Mplayer with planar color
399 //I decided to copy data rather than modify the hqdn3d functions
400 //this takes some time, but future inmprovements in hqdn3d
401 //functions can be simply copied here without modification
402
403 for (i=0;i<(in->w*in->h);i++) //copy to planar
404 {
405 in->Rplani[i]=inframe[i]&255;
406 in->Gplani[i]=(inframe[i]>>8)&255;
407 in->Bplani[i]=(inframe[i]>>16)&255;
408 }
409
410 deNoise(in->Rplani, in->Rplano, in->vps.Line, &in->vps.Frame[0], in->w, in->h, in->w, in->w, in->vps.Coefs[0], in->vps.Coefs[0], in->vps.Coefs[1]);
411 deNoise(in->Gplani, in->Gplano, in->vps.Line, &in->vps.Frame[1], in->w, in->h, in->w, in->w, in->vps.Coefs[0], in->vps.Coefs[0], in->vps.Coefs[1]);
412 deNoise(in->Bplani, in->Bplano, in->vps.Line, &in->vps.Frame[2], in->w, in->h, in->w, in->w, in->vps.Coefs[0], in->vps.Coefs[0], in->vps.Coefs[1]);
413
414 for (i=0;i<(in->w*in->h);i++) //copy to packed, preserve alpha
415 {
416 outframe[i]=((uint32_t)in->Rplano[i])|((uint32_t)in->Gplano[i]<<8)|((uint32_t)in->Bplano[i]<<16)|(inframe[i]&0xFF000000);
417 }
418
419
420 }
421