10l initial patch by Oded Shimon <ods15 at ods15.dyndns.org>
[mplayer/greg.git] / libmpcodecs / vf_denoise3d.c
blobea9e5f27a07130260c9ec602062eb1e45ffc594e
1 /*
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <inttypes.h>
23 #include <math.h>
25 #include "../config.h"
26 #include "../mp_msg.h"
28 #ifdef HAVE_MALLOC_H
29 #include <malloc.h>
30 #endif
32 #include "img_format.h"
33 #include "mp_image.h"
34 #include "vf.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 //===========================================================================//
43 struct vf_priv_s {
44 int Coefs[4][512];
45 unsigned char *Line;
46 mp_image_t *pmpi;
50 /***************************************************************************/
53 static int config(struct vf_instance_s* vf,
54 int width, int height, int d_width, int d_height,
55 unsigned int flags, unsigned int outfmt){
57 if(vf->priv->Line) free(vf->priv->Line);
58 vf->priv->Line = malloc(width);
59 vf->priv->pmpi=NULL;
60 // vf->default_caps &= !VFCAP_ACCEPT_STRIDE;
62 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
66 static void uninit(struct vf_instance_s* vf)
68 free(vf->priv->Line);
71 #define LowPass(Prev, Curr, Coef) (Curr + Coef[Prev - Curr])
73 static void deNoise(unsigned char *Frame, // mpi->planes[x]
74 unsigned char *FramePrev, // pmpi->planes[x]
75 unsigned char *FrameDest, // dmpi->planes[x]
76 unsigned char *LineAnt, // vf->priv->Line (width bytes)
77 int W, int H, int sStride, int pStride, int dStride,
78 int *Horizontal, int *Vertical, int *Temporal)
80 int X, Y;
81 int sLineOffs = 0, pLineOffs = 0, dLineOffs = 0;
82 unsigned char PixelAnt;
84 /* First pixel has no left nor top neightbour. Only previous frame */
85 LineAnt[0] = PixelAnt = Frame[0];
86 FrameDest[0] = LowPass(FramePrev[0], LineAnt[0], Temporal);
88 /* Fist line has no top neightbour. Only left one for each pixel and
89 * last frame */
90 for (X = 1; X < W; X++)
92 PixelAnt = LowPass(PixelAnt, Frame[X], Horizontal);
93 LineAnt[X] = PixelAnt;
94 FrameDest[X] = LowPass(FramePrev[X], LineAnt[X], Temporal);
97 for (Y = 1; Y < H; Y++)
99 sLineOffs += sStride, pLineOffs += pStride, dLineOffs += dStride;
100 /* First pixel on each line doesn't have previous pixel */
101 PixelAnt = Frame[sLineOffs];
102 LineAnt[0] = LowPass(LineAnt[0], PixelAnt, Vertical);
103 FrameDest[dLineOffs] = LowPass(FramePrev[pLineOffs], LineAnt[0], Temporal);
105 for (X = 1; X < W; X++)
107 /* The rest are normal */
108 PixelAnt = LowPass(PixelAnt, Frame[sLineOffs+X], Horizontal);
109 LineAnt[X] = LowPass(LineAnt[X], PixelAnt, Vertical);
110 FrameDest[dLineOffs+X] = LowPass(FramePrev[pLineOffs+X], LineAnt[X], Temporal);
117 static int put_image(struct vf_instance_s* vf, mp_image_t *mpi){
118 int cw= mpi->w >> mpi->chroma_x_shift;
119 int ch= mpi->h >> mpi->chroma_y_shift;
120 int W = mpi->w, H = mpi->h;
122 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
123 MP_IMGTYPE_IP, MP_IMGFLAG_ACCEPT_STRIDE,
124 mpi->w,mpi->h);
126 if(!dmpi) return 0;
127 if (!vf->priv->pmpi) vf->priv->pmpi=mpi;
129 deNoise(mpi->planes[0], vf->priv->pmpi->planes[0], dmpi->planes[0],
130 vf->priv->Line, W, H,
131 mpi->stride[0], vf->priv->pmpi->stride[0], dmpi->stride[0],
132 vf->priv->Coefs[0] + 256,
133 vf->priv->Coefs[0] + 256,
134 vf->priv->Coefs[1] + 256);
135 deNoise(mpi->planes[1], vf->priv->pmpi->planes[1], dmpi->planes[1],
136 vf->priv->Line, cw, ch,
137 mpi->stride[1], vf->priv->pmpi->stride[1], dmpi->stride[1],
138 vf->priv->Coefs[2] + 256,
139 vf->priv->Coefs[2] + 256,
140 vf->priv->Coefs[3] + 256);
141 deNoise(mpi->planes[2], vf->priv->pmpi->planes[2], dmpi->planes[2],
142 vf->priv->Line, cw, ch,
143 mpi->stride[2], vf->priv->pmpi->stride[2], dmpi->stride[2],
144 vf->priv->Coefs[2] + 256,
145 vf->priv->Coefs[2] + 256,
146 vf->priv->Coefs[3] + 256);
148 vf->priv->pmpi=dmpi; // save reference image
149 return vf_next_put_image(vf,dmpi);
152 //===========================================================================//
154 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
155 switch(fmt)
157 case IMGFMT_YV12:
158 case IMGFMT_I420:
159 case IMGFMT_IYUV:
160 case IMGFMT_YVU9:
161 case IMGFMT_444P:
162 case IMGFMT_422P:
163 case IMGFMT_411P:
164 return vf_next_query_format(vf, fmt);
166 return 0;
170 #define ABS(A) ( (A) > 0 ? (A) : -(A) )
172 static void PrecalcCoefs(int *Ct, double Dist25)
174 int i;
175 double Gamma, Simil, C;
177 Gamma = log(0.25) / log(1.0 - Dist25/255.0);
179 for (i = -256; i <= 255; i++)
181 Simil = 1.0 - ABS(i) / 255.0;
182 // Ct[256+i] = lround(pow(Simil, Gamma) * (double)i);
183 C = pow(Simil, Gamma) * (double)i;
184 Ct[256+i] = (C<0) ? (C-0.5) : (C+0.5);
189 static int open(vf_instance_t *vf, char* args){
190 double LumSpac, LumTmp, ChromSpac, ChromTmp;
191 double Param1, Param2, Param3;
193 vf->config=config;
194 vf->put_image=put_image;
195 vf->query_format=query_format;
196 vf->uninit=uninit;
197 vf->priv=malloc(sizeof(struct vf_priv_s));
198 memset(vf->priv, 0, sizeof(struct vf_priv_s));
200 if (args)
202 switch(sscanf(args, "%lf:%lf:%lf",
203 &Param1, &Param2, &Param3
206 case 0:
207 LumSpac = PARAM1_DEFAULT;
208 LumTmp = PARAM3_DEFAULT;
210 ChromSpac = PARAM2_DEFAULT;
211 ChromTmp = LumTmp * ChromSpac / LumSpac;
212 break;
214 case 1:
215 LumSpac = Param1;
216 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
218 ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT;
219 ChromTmp = LumTmp * ChromSpac / LumSpac;
220 break;
222 case 2:
223 LumSpac = Param1;
224 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
226 ChromSpac = Param2;
227 ChromTmp = LumTmp * ChromSpac / LumSpac;
228 break;
230 case 3:
231 LumSpac = Param1;
232 LumTmp = Param3;
234 ChromSpac = Param2;
235 ChromTmp = LumTmp * ChromSpac / LumSpac;
236 break;
238 default:
239 LumSpac = PARAM1_DEFAULT;
240 LumTmp = PARAM3_DEFAULT;
242 ChromSpac = PARAM2_DEFAULT;
243 ChromTmp = LumTmp * ChromSpac / LumSpac;
246 else
248 LumSpac = PARAM1_DEFAULT;
249 LumTmp = PARAM3_DEFAULT;
251 ChromSpac = PARAM2_DEFAULT;
252 ChromTmp = LumTmp * ChromSpac / LumSpac;
255 PrecalcCoefs(vf->priv->Coefs[0], LumSpac);
256 PrecalcCoefs(vf->priv->Coefs[1], LumTmp);
257 PrecalcCoefs(vf->priv->Coefs[2], ChromSpac);
258 PrecalcCoefs(vf->priv->Coefs[3], ChromTmp);
260 return 1;
263 vf_info_t vf_info_denoise3d = {
264 "3D Denoiser (variable lowpass filter)",
265 "denoise3d",
266 "Daniel Moreno",
268 open,
269 NULL
272 //===========================================================================//