9441
|
1 /*
|
26727
|
2 * Copyright (C) 2003 Daniel Moreno <comac@comac.darktech.org>
|
|
3 *
|
|
4 * This file is part of MPlayer.
|
|
5 *
|
|
6 * MPlayer is free software; you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation; either version 2 of the License, or
|
|
9 * (at your option) any later version.
|
|
10 *
|
|
11 * MPlayer is distributed in the hope that it will be useful,
|
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 * GNU General Public License for more details.
|
|
15 *
|
|
16 * You should have received a copy of the GNU General Public License along
|
|
17 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 */
|
9441
|
20
|
|
21 #include <stdio.h>
|
|
22 #include <stdlib.h>
|
|
23 #include <string.h>
|
|
24 #include <inttypes.h>
|
|
25 #include <math.h>
|
|
26
|
17012
|
27 #include "mp_msg.h"
|
9441
|
28 #include "img_format.h"
|
|
29 #include "mp_image.h"
|
|
30 #include "vf.h"
|
|
31
|
|
32 #define PARAM1_DEFAULT 4.0
|
|
33 #define PARAM2_DEFAULT 3.0
|
|
34 #define PARAM3_DEFAULT 6.0
|
|
35
|
|
36 //===========================================================================//
|
|
37
|
|
38 struct vf_priv_s {
|
|
39 int Coefs[4][512*16];
|
|
40 unsigned int *Line;
|
|
41 unsigned short *Frame[3];
|
|
42 };
|
|
43
|
|
44
|
|
45 /***************************************************************************/
|
|
46
|
30642
|
47 static void uninit(struct vf_instance *vf){
|
9441
|
48 if(vf->priv->Line){free(vf->priv->Line);vf->priv->Line=NULL;}
|
|
49 if(vf->priv->Frame[0]){free(vf->priv->Frame[0]);vf->priv->Frame[0]=NULL;}
|
|
50 if(vf->priv->Frame[1]){free(vf->priv->Frame[1]);vf->priv->Frame[1]=NULL;}
|
|
51 if(vf->priv->Frame[2]){free(vf->priv->Frame[2]);vf->priv->Frame[2]=NULL;}
|
|
52 }
|
|
53
|
30642
|
54 static int config(struct vf_instance *vf,
|
9441
|
55 int width, int height, int d_width, int d_height,
|
|
56 unsigned int flags, unsigned int outfmt){
|
|
57
|
|
58 uninit(vf);
|
|
59 vf->priv->Line = malloc(width*sizeof(int));
|
|
60
|
|
61 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
|
|
62 }
|
|
63
|
|
64 static inline unsigned int LowPassMul(unsigned int PrevMul, unsigned int CurrMul, int* Coef){
|
|
65 // int dMul= (PrevMul&0xFFFFFF)-(CurrMul&0xFFFFFF);
|
|
66 int dMul= PrevMul-CurrMul;
|
29372
|
67 unsigned int d=((dMul+0x10007FF)>>12);
|
9441
|
68 return CurrMul + Coef[d];
|
|
69 }
|
|
70
|
16682
|
71 static void deNoiseTemporal(
|
|
72 unsigned char *Frame, // mpi->planes[x]
|
|
73 unsigned char *FrameDest, // dmpi->planes[x]
|
|
74 unsigned short *FrameAnt,
|
|
75 int W, int H, int sStride, int dStride,
|
|
76 int *Temporal)
|
|
77 {
|
29372
|
78 long X, Y;
|
17965
|
79 unsigned int PixelDst;
|
16682
|
80
|
|
81 for (Y = 0; Y < H; Y++){
|
|
82 for (X = 0; X < W; X++){
|
|
83 PixelDst = LowPassMul(FrameAnt[X]<<8, Frame[X]<<16, Temporal);
|
17965
|
84 FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
|
|
85 FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
|
16682
|
86 }
|
|
87 Frame += sStride;
|
|
88 FrameDest += dStride;
|
|
89 FrameAnt += W;
|
|
90 }
|
|
91 }
|
|
92
|
|
93 static void deNoiseSpacial(
|
|
94 unsigned char *Frame, // mpi->planes[x]
|
|
95 unsigned char *FrameDest, // dmpi->planes[x]
|
|
96 unsigned int *LineAnt, // vf->priv->Line (width bytes)
|
|
97 int W, int H, int sStride, int dStride,
|
|
98 int *Horizontal, int *Vertical)
|
|
99 {
|
29372
|
100 long X, Y;
|
|
101 long sLineOffs = 0, dLineOffs = 0;
|
16682
|
102 unsigned int PixelAnt;
|
17965
|
103 unsigned int PixelDst;
|
29263
|
104
|
16684
|
105 /* First pixel has no left nor top neighbor. */
|
16682
|
106 PixelDst = LineAnt[0] = PixelAnt = Frame[0]<<16;
|
17965
|
107 FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
|
16682
|
108
|
16699
|
109 /* First line has no top neighbor, only left. */
|
16682
|
110 for (X = 1; X < W; X++){
|
|
111 PixelDst = LineAnt[X] = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
|
17965
|
112 FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
|
16682
|
113 }
|
|
114
|
|
115 for (Y = 1; Y < H; Y++){
|
|
116 unsigned int PixelAnt;
|
|
117 sLineOffs += sStride, dLineOffs += dStride;
|
|
118 /* First pixel on each line doesn't have previous pixel */
|
|
119 PixelAnt = Frame[sLineOffs]<<16;
|
|
120 PixelDst = LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
|
17965
|
121 FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
|
16682
|
122
|
|
123 for (X = 1; X < W; X++){
|
17965
|
124 unsigned int PixelDst;
|
16682
|
125 /* The rest are normal */
|
|
126 PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
|
|
127 PixelDst = LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
|
17965
|
128 FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
|
16682
|
129 }
|
|
130 }
|
|
131 }
|
|
132
|
9441
|
133 static void deNoise(unsigned char *Frame, // mpi->planes[x]
|
|
134 unsigned char *FrameDest, // dmpi->planes[x]
|
|
135 unsigned int *LineAnt, // vf->priv->Line (width bytes)
|
|
136 unsigned short **FrameAntPtr,
|
|
137 int W, int H, int sStride, int dStride,
|
|
138 int *Horizontal, int *Vertical, int *Temporal)
|
|
139 {
|
29372
|
140 long X, Y;
|
|
141 long sLineOffs = 0, dLineOffs = 0;
|
9441
|
142 unsigned int PixelAnt;
|
17965
|
143 unsigned int PixelDst;
|
9441
|
144 unsigned short* FrameAnt=(*FrameAntPtr);
|
29263
|
145
|
9441
|
146 if(!FrameAnt){
|
|
147 (*FrameAntPtr)=FrameAnt=malloc(W*H*sizeof(unsigned short));
|
|
148 for (Y = 0; Y < H; Y++){
|
|
149 unsigned short* dst=&FrameAnt[Y*W];
|
|
150 unsigned char* src=Frame+Y*sStride;
|
|
151 for (X = 0; X < W; X++) dst[X]=src[X]<<8;
|
|
152 }
|
|
153 }
|
|
154
|
16682
|
155 if(!Horizontal[0] && !Vertical[0]){
|
|
156 deNoiseTemporal(Frame, FrameDest, FrameAnt,
|
|
157 W, H, sStride, dStride, Temporal);
|
|
158 return;
|
|
159 }
|
|
160 if(!Temporal[0]){
|
|
161 deNoiseSpacial(Frame, FrameDest, LineAnt,
|
|
162 W, H, sStride, dStride, Horizontal, Vertical);
|
|
163 return;
|
|
164 }
|
|
165
|
16684
|
166 /* First pixel has no left nor top neighbor. Only previous frame */
|
9441
|
167 LineAnt[0] = PixelAnt = Frame[0]<<16;
|
|
168 PixelDst = LowPassMul(FrameAnt[0]<<8, PixelAnt, Temporal);
|
17965
|
169 FrameAnt[0] = ((PixelDst+0x1000007F)>>8);
|
|
170 FrameDest[0]= ((PixelDst+0x10007FFF)>>16);
|
9441
|
171
|
16699
|
172 /* First line has no top neighbor. Only left one for each pixel and
|
9441
|
173 * last frame */
|
|
174 for (X = 1; X < W; X++){
|
|
175 LineAnt[X] = PixelAnt = LowPassMul(PixelAnt, Frame[X]<<16, Horizontal);
|
|
176 PixelDst = LowPassMul(FrameAnt[X]<<8, PixelAnt, Temporal);
|
17965
|
177 FrameAnt[X] = ((PixelDst+0x1000007F)>>8);
|
|
178 FrameDest[X]= ((PixelDst+0x10007FFF)>>16);
|
9441
|
179 }
|
|
180
|
|
181 for (Y = 1; Y < H; Y++){
|
|
182 unsigned int PixelAnt;
|
|
183 unsigned short* LinePrev=&FrameAnt[Y*W];
|
|
184 sLineOffs += sStride, dLineOffs += dStride;
|
|
185 /* First pixel on each line doesn't have previous pixel */
|
|
186 PixelAnt = Frame[sLineOffs]<<16;
|
|
187 LineAnt[0] = LowPassMul(LineAnt[0], PixelAnt, Vertical);
|
|
188 PixelDst = LowPassMul(LinePrev[0]<<8, LineAnt[0], Temporal);
|
17965
|
189 LinePrev[0] = ((PixelDst+0x1000007F)>>8);
|
|
190 FrameDest[dLineOffs]= ((PixelDst+0x10007FFF)>>16);
|
9441
|
191
|
|
192 for (X = 1; X < W; X++){
|
17965
|
193 unsigned int PixelDst;
|
9441
|
194 /* The rest are normal */
|
|
195 PixelAnt = LowPassMul(PixelAnt, Frame[sLineOffs+X]<<16, Horizontal);
|
|
196 LineAnt[X] = LowPassMul(LineAnt[X], PixelAnt, Vertical);
|
|
197 PixelDst = LowPassMul(LinePrev[X]<<8, LineAnt[X], Temporal);
|
17965
|
198 LinePrev[X] = ((PixelDst+0x1000007F)>>8);
|
|
199 FrameDest[dLineOffs+X]= ((PixelDst+0x10007FFF)>>16);
|
9441
|
200 }
|
|
201 }
|
|
202 }
|
|
203
|
|
204
|
30642
|
205 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
|
9441
|
206 int cw= mpi->w >> mpi->chroma_x_shift;
|
|
207 int ch= mpi->h >> mpi->chroma_y_shift;
|
|
208 int W = mpi->w, H = mpi->h;
|
|
209
|
|
210 mp_image_t *dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
|
211 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
|
|
212 mpi->w,mpi->h);
|
|
213
|
|
214 if(!dmpi) return 0;
|
|
215
|
|
216 deNoise(mpi->planes[0], dmpi->planes[0],
|
|
217 vf->priv->Line, &vf->priv->Frame[0], W, H,
|
|
218 mpi->stride[0], dmpi->stride[0],
|
|
219 vf->priv->Coefs[0],
|
|
220 vf->priv->Coefs[0],
|
|
221 vf->priv->Coefs[1]);
|
|
222 deNoise(mpi->planes[1], dmpi->planes[1],
|
|
223 vf->priv->Line, &vf->priv->Frame[1], cw, ch,
|
|
224 mpi->stride[1], dmpi->stride[1],
|
|
225 vf->priv->Coefs[2],
|
|
226 vf->priv->Coefs[2],
|
|
227 vf->priv->Coefs[3]);
|
|
228 deNoise(mpi->planes[2], dmpi->planes[2],
|
|
229 vf->priv->Line, &vf->priv->Frame[2], cw, ch,
|
|
230 mpi->stride[2], dmpi->stride[2],
|
|
231 vf->priv->Coefs[2],
|
|
232 vf->priv->Coefs[2],
|
|
233 vf->priv->Coefs[3]);
|
|
234
|
17906
|
235 return vf_next_put_image(vf,dmpi, pts);
|
9441
|
236 }
|
|
237
|
|
238 //===========================================================================//
|
|
239
|
30642
|
240 static int query_format(struct vf_instance *vf, unsigned int fmt){
|
9441
|
241 switch(fmt)
|
|
242 {
|
|
243 case IMGFMT_YV12:
|
|
244 case IMGFMT_I420:
|
|
245 case IMGFMT_IYUV:
|
|
246 case IMGFMT_YVU9:
|
|
247 case IMGFMT_444P:
|
|
248 case IMGFMT_422P:
|
|
249 case IMGFMT_411P:
|
|
250 return vf_next_query_format(vf, fmt);
|
|
251 }
|
|
252 return 0;
|
|
253 }
|
|
254
|
|
255
|
|
256 #define ABS(A) ( (A) > 0 ? (A) : -(A) )
|
|
257
|
|
258 static void PrecalcCoefs(int *Ct, double Dist25)
|
|
259 {
|
|
260 int i;
|
|
261 double Gamma, Simil, C;
|
|
262
|
|
263 Gamma = log(0.25) / log(1.0 - Dist25/255.0 - 0.00001);
|
|
264
|
16682
|
265 for (i = -255*16; i <= 255*16; i++)
|
9441
|
266 {
|
|
267 Simil = 1.0 - ABS(i) / (16*255.0);
|
|
268 C = pow(Simil, Gamma) * 65536.0 * (double)i / 16.0;
|
|
269 Ct[16*256+i] = (C<0) ? (C-0.5) : (C+0.5);
|
|
270 }
|
16682
|
271
|
|
272 Ct[0] = (Dist25 != 0);
|
9441
|
273 }
|
|
274
|
|
275
|
30638
|
276 static int vf_open(vf_instance_t *vf, char *args){
|
9441
|
277 double LumSpac, LumTmp, ChromSpac, ChromTmp;
|
|
278 double Param1, Param2, Param3, Param4;
|
|
279
|
|
280 vf->config=config;
|
|
281 vf->put_image=put_image;
|
|
282 vf->query_format=query_format;
|
|
283 vf->uninit=uninit;
|
|
284 vf->priv=malloc(sizeof(struct vf_priv_s));
|
|
285 memset(vf->priv, 0, sizeof(struct vf_priv_s));
|
|
286
|
|
287 if (args)
|
|
288 {
|
|
289 switch(sscanf(args, "%lf:%lf:%lf:%lf",
|
|
290 &Param1, &Param2, &Param3, &Param4
|
|
291 ))
|
|
292 {
|
|
293 case 0:
|
|
294 LumSpac = PARAM1_DEFAULT;
|
|
295 LumTmp = PARAM3_DEFAULT;
|
|
296
|
|
297 ChromSpac = PARAM2_DEFAULT;
|
|
298 ChromTmp = LumTmp * ChromSpac / LumSpac;
|
|
299 break;
|
|
300
|
|
301 case 1:
|
|
302 LumSpac = Param1;
|
|
303 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
|
|
304
|
|
305 ChromSpac = PARAM2_DEFAULT * Param1 / PARAM1_DEFAULT;
|
|
306 ChromTmp = LumTmp * ChromSpac / LumSpac;
|
|
307 break;
|
|
308
|
|
309 case 2:
|
|
310 LumSpac = Param1;
|
|
311 LumTmp = PARAM3_DEFAULT * Param1 / PARAM1_DEFAULT;
|
|
312
|
|
313 ChromSpac = Param2;
|
|
314 ChromTmp = LumTmp * ChromSpac / LumSpac;
|
|
315 break;
|
|
316
|
|
317 case 3:
|
|
318 LumSpac = Param1;
|
|
319 LumTmp = Param3;
|
|
320
|
|
321 ChromSpac = Param2;
|
|
322 ChromTmp = LumTmp * ChromSpac / LumSpac;
|
|
323 break;
|
|
324
|
|
325 case 4:
|
|
326 LumSpac = Param1;
|
|
327 LumTmp = Param3;
|
|
328
|
|
329 ChromSpac = Param2;
|
|
330 ChromTmp = Param4;
|
|
331 break;
|
|
332
|
|
333 default:
|
|
334 LumSpac = PARAM1_DEFAULT;
|
|
335 LumTmp = PARAM3_DEFAULT;
|
|
336
|
|
337 ChromSpac = PARAM2_DEFAULT;
|
|
338 ChromTmp = LumTmp * ChromSpac / LumSpac;
|
|
339 }
|
|
340 }
|
|
341 else
|
|
342 {
|
|
343 LumSpac = PARAM1_DEFAULT;
|
|
344 LumTmp = PARAM3_DEFAULT;
|
|
345
|
|
346 ChromSpac = PARAM2_DEFAULT;
|
|
347 ChromTmp = LumTmp * ChromSpac / LumSpac;
|
|
348 }
|
|
349
|
|
350 PrecalcCoefs(vf->priv->Coefs[0], LumSpac);
|
|
351 PrecalcCoefs(vf->priv->Coefs[1], LumTmp);
|
|
352 PrecalcCoefs(vf->priv->Coefs[2], ChromSpac);
|
|
353 PrecalcCoefs(vf->priv->Coefs[3], ChromTmp);
|
|
354
|
|
355 return 1;
|
|
356 }
|
|
357
|
25221
|
358 const vf_info_t vf_info_hqdn3d = {
|
9441
|
359 "High Quality 3D Denoiser",
|
|
360 "hqdn3d",
|
|
361 "Daniel Moreno & A'rpi",
|
|
362 "",
|
30638
|
363 vf_open,
|
9593
|
364 NULL
|
9441
|
365 };
|
|
366
|
|
367 //===========================================================================//
|