2
Copyright (C) 2003 Daniel Moreno <comac@comac.darktech.org>
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32
#include "img_format.h"
35
#include "libvo/fastmemcpy.h"
37
#define PARAM1_DEFAULT 4.0
38
#define PARAM2_DEFAULT 3.0
39
#define PARAM3_DEFAULT 6.0
41
//===========================================================================//
46
unsigned short *Frame[3];
50
/***************************************************************************/
52
static void uninit(struct vf_instance_s* vf){
53
if(vf->priv->Line){free(vf->priv->Line);vf->priv->Line=NULL;}
54
if(vf->priv->Frame[0]){free(vf->priv->Frame[0]);vf->priv->Frame[0]=NULL;}
55
if(vf->priv->Frame[1]){free(vf->priv->Frame[1]);vf->priv->Frame[1]=NULL;}
56
if(vf->priv->Frame[2]){free(vf->priv->Frame[2]);vf->priv->Frame[2]=NULL;}
59
static int config(struct vf_instance_s* vf,
60
int width, int height, int d_width, int d_height,
61
unsigned int flags, unsigned int outfmt){
64
vf->priv->Line = malloc(width*sizeof(int));
66
return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
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
int d=((dMul+0x10007FF)/(65536/16));
73
return CurrMul + Coef[d];
76
static 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,
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)/256);
90
FrameDest[X]= ((PixelDst+0x10007FFF)/65536);
98
static 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)
106
int sLineOffs = 0, dLineOffs = 0;
107
unsigned int PixelAnt;
110
/* First pixel has no left nor top neighbor. */
111
PixelDst = LineAnt[0] = PixelAnt = Frame[0]<<16;
112
FrameDest[0]= ((PixelDst+0x10007FFF)/65536);
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)/65536);
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)/65536);
128
for (X = 1; X < W; X++){
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)/65536);
138
static 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)
146
int sLineOffs = 0, dLineOffs = 0;
147
unsigned int PixelAnt;
149
unsigned short* FrameAnt=(*FrameAntPtr);
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;
160
if(!Horizontal[0] && !Vertical[0]){
161
deNoiseTemporal(Frame, FrameDest, FrameAnt,
162
W, H, sStride, dStride, Temporal);
166
deNoiseSpacial(Frame, FrameDest, LineAnt,
167
W, H, sStride, dStride, Horizontal, Vertical);
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)/256);
175
FrameDest[0]= ((PixelDst+0x10007FFF)/65536);
177
/* First line has no top neighbor. Only left one for each pixel and
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)/256);
183
FrameDest[X]= ((PixelDst+0x10007FFF)/65536);
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)/256);
195
FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)/65536);
197
for (X = 1; X < W; X++){
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)/256);
204
FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)/65536);
210
static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
211
int cw= mpi->w >> mpi->chroma_x_shift;
212
int ch= mpi->h >> mpi->chroma_y_shift;
213
int W = mpi->w, H = mpi->h;
215
mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
216
MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
221
deNoise(mpi->planes[0], dmpi->planes[0],
222
vf->priv->Line, &vf->priv->Frame[0], W, H,
223
mpi->stride[0], dmpi->stride[0],
227
deNoise(mpi->planes[1], dmpi->planes[1],
228
vf->priv->Line, &vf->priv->Frame[1], cw, ch,
229
mpi->stride[1], dmpi->stride[1],
233
deNoise(mpi->planes[2], dmpi->planes[2],
234
vf->priv->Line, &vf->priv->Frame[2], cw, ch,
235
mpi->stride[2], dmpi->stride[2],
240
return vf_next_put_image(vf,dmpi);
243
//===========================================================================//
245
static int query_format(struct vf_instance_s* vf, unsigned int fmt){
255
return vf_next_query_format(vf, fmt);
261
#define ABS(A) ( (A) > 0 ? (A) : -(A) )
263
static void PrecalcCoefs(int *Ct, double Dist25)
266
double Gamma, Simil, C;
268
Gamma = log(0.25) / log(1.0 - Dist25/255.0 - 0.00001);
270
for (i = -255*16; i <= 255*16; i++)
272
Simil = 1.0 - ABS(i) / (16*255.0);
273
C = pow(Simil, Gamma) * 65536.0 * (double)i / 16.0;
274
Ct[16*256+i] = (C<0) ? (C-0.5) : (C+0.5);
277
Ct[0] = (Dist25 != 0);
281
static int open(vf_instance_t *vf, char* args){
282
double LumSpac, LumTmp, ChromSpac, ChromTmp;
283
double Param1, Param2, Param3, Param4;
286
vf->put_image=put_image;
287
vf->query_format=query_format;
289
vf->priv=malloc(sizeof(struct vf_priv_s));
290
memset(vf->priv, 0, sizeof(struct vf_priv_s));
294
switch(sscanf(args, "%lf:%lf:%lf:%lf",
295
&Param1, &Param2, &Param3, &Param4
299
LumSpac = PARAM1_DEFAULT;
300
LumTmp = PARAM3_DEFAULT;
302
ChromSpac = PARAM2_DEFAULT;
303
ChromTmp = LumTmp * ChromSpac / LumSpac;
308
LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
310
ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT;
311
ChromTmp = LumTmp * ChromSpac / LumSpac;
316
LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
319
ChromTmp = LumTmp * ChromSpac / LumSpac;
327
ChromTmp = LumTmp * ChromSpac / LumSpac;
339
LumSpac = PARAM1_DEFAULT;
340
LumTmp = PARAM3_DEFAULT;
342
ChromSpac = PARAM2_DEFAULT;
343
ChromTmp = LumTmp * ChromSpac / LumSpac;
348
LumSpac = PARAM1_DEFAULT;
349
LumTmp = PARAM3_DEFAULT;
351
ChromSpac = PARAM2_DEFAULT;
352
ChromTmp = LumTmp * ChromSpac / LumSpac;
355
PrecalcCoefs(vf->priv->Coefs[0], LumSpac);
356
PrecalcCoefs(vf->priv->Coefs[1], LumTmp);
357
PrecalcCoefs(vf->priv->Coefs[2], ChromSpac);
358
PrecalcCoefs(vf->priv->Coefs[3], ChromTmp);
363
vf_info_t vf_info_hqdn3d = {
364
"High Quality 3D Denoiser",
366
"Daniel Moreno & A'rpi",
372
//===========================================================================//