2 * Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
4 * This file is part of MPlayer.
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.
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.
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.
29 #include "cpudetect.h"
35 #include "img_format.h"
38 #include "libvo/fastmemcpy.h"
39 #include "libavutil/mem.h"
41 #define MAX_NOISE 4096
42 #define MAX_SHIFT 1024
43 #define MAX_RES (MAX_NOISE-MAX_SHIFT)
45 //===========================================================================//
47 static inline void lineNoise_C(uint8_t *dst
, uint8_t *src
, int8_t *noise
, int len
, int shift
);
48 static inline void lineNoiseAvg_C(uint8_t *dst
, uint8_t *src
, int len
, int8_t **shift
);
50 static void (*lineNoise
)(uint8_t *dst
, uint8_t *src
, int8_t *noise
, int len
, int shift
)= lineNoise_C
;
51 static void (*lineNoiseAvg
)(uint8_t *dst
, uint8_t *src
, int len
, int8_t **shift
)= lineNoiseAvg_C
;
53 typedef struct FilterParam
{
62 int8_t *prev_shift
[MAX_RES
][3];
66 FilterParam lumaParam
;
67 FilterParam chromaParam
;
71 static int nonTempRandShift_init
;
72 static int nonTempRandShift
[MAX_RES
];
74 static int patt
[4] = {
78 #define RAND_N(range) ((int) ((double)range*rand()/(RAND_MAX+1.0)))
79 static int8_t *initNoise(FilterParam
*fp
){
80 int strength
= fp
->strength
;
81 int uniform
= fp
->uniform
;
82 int averaged
= fp
->averaged
;
83 int pattern
= fp
->pattern
;
84 int8_t *noise
= av_malloc(MAX_NOISE
*sizeof(int8_t));
89 for(i
=0,j
=0; i
<MAX_NOISE
; i
++,j
++)
94 noise
[i
]= (RAND_N(strength
) - strength
/2)/6
95 +patt
[j
%4]*strength
*0.25/3;
97 noise
[i
]= (RAND_N(strength
) - strength
/2)/3;
101 noise
[i
]= (RAND_N(strength
) - strength
/2)/2
102 + patt
[j
%4]*strength
*0.25;
104 noise
[i
]= RAND_N(strength
) - strength
/2;
108 double x1
, x2
, w
, y1
;
110 x1
= 2.0 * rand()/(float)RAND_MAX
- 1.0;
111 x2
= 2.0 * rand()/(float)RAND_MAX
- 1.0;
112 w
= x1
* x1
+ x2
* x2
;
113 } while ( w
>= 1.0 );
115 w
= sqrt( (-2.0 * log( w
) ) / w
);
117 y1
*= strength
/ sqrt(3.0);
120 y1
+= patt
[j
%4]*strength
*0.35;
122 if (y1
<-128) y1
=-128;
123 else if(y1
> 127) y1
= 127;
124 if (averaged
) y1
/= 3.0;
127 if (RAND_N(6) == 0) j
--;
131 for (i
= 0; i
< MAX_RES
; i
++)
132 for (j
= 0; j
< 3; j
++)
133 fp
->prev_shift
[i
][j
] = noise
+ (rand()&(MAX_SHIFT
-1));
135 if(!nonTempRandShift_init
){
136 for(i
=0; i
<MAX_RES
; i
++){
137 nonTempRandShift
[i
]= rand()&(MAX_SHIFT
-1);
139 nonTempRandShift_init
= 1;
147 /***************************************************************************/
150 static inline void lineNoise_MMX(uint8_t *dst
, uint8_t *src
, int8_t *noise
, int len
, int shift
){
151 x86_reg mmx_len
= len
&(~7);
155 "mov %3, %%"REG_a
" \n\t"
156 "pcmpeqb %%mm7, %%mm7 \n\t"
157 "psllw $15, %%mm7 \n\t"
158 "packsswb %%mm7, %%mm7 \n\t"
161 "movq (%0, %%"REG_a
"), %%mm0 \n\t"
162 "movq (%1, %%"REG_a
"), %%mm1 \n\t"
163 "pxor %%mm7, %%mm0 \n\t"
164 "paddsb %%mm1, %%mm0 \n\t"
165 "pxor %%mm7, %%mm0 \n\t"
166 "movq %%mm0, (%2, %%"REG_a
") \n\t"
167 "add $8, %%"REG_a
" \n\t"
169 :: "r" (src
+mmx_len
), "r" (noise
+mmx_len
), "r" (dst
+mmx_len
), "g" (-mmx_len
)
173 lineNoise_C(dst
+mmx_len
, src
+mmx_len
, noise
+mmx_len
, len
-mmx_len
, 0);
177 //duplicate of previous except movntq
179 static inline void lineNoise_MMX2(uint8_t *dst
, uint8_t *src
, int8_t *noise
, int len
, int shift
){
180 x86_reg mmx_len
= len
&(~7);
184 "mov %3, %%"REG_a
" \n\t"
185 "pcmpeqb %%mm7, %%mm7 \n\t"
186 "psllw $15, %%mm7 \n\t"
187 "packsswb %%mm7, %%mm7 \n\t"
190 "movq (%0, %%"REG_a
"), %%mm0 \n\t"
191 "movq (%1, %%"REG_a
"), %%mm1 \n\t"
192 "pxor %%mm7, %%mm0 \n\t"
193 "paddsb %%mm1, %%mm0 \n\t"
194 "pxor %%mm7, %%mm0 \n\t"
195 "movntq %%mm0, (%2, %%"REG_a
") \n\t"
196 "add $8, %%"REG_a
" \n\t"
198 :: "r" (src
+mmx_len
), "r" (noise
+mmx_len
), "r" (dst
+mmx_len
), "g" (-mmx_len
)
202 lineNoise_C(dst
+mmx_len
, src
+mmx_len
, noise
+mmx_len
, len
-mmx_len
, 0);
206 static inline void lineNoise_C(uint8_t *dst
, uint8_t *src
, int8_t *noise
, int len
, int shift
){
211 int v
= src
[i
]+ noise
[i
];
212 if(v
>255) dst
[i
]=255; //FIXME optimize
213 else if(v
<0) dst
[i
]=0;
218 /***************************************************************************/
221 static inline void lineNoiseAvg_MMX(uint8_t *dst
, uint8_t *src
, int len
, int8_t **shift
){
222 x86_reg mmx_len
= len
&(~7);
225 "mov %5, %%"REG_a
" \n\t"
228 "movq (%1, %%"REG_a
"), %%mm1 \n\t"
229 "movq (%0, %%"REG_a
"), %%mm0 \n\t"
230 "paddb (%2, %%"REG_a
"), %%mm1 \n\t"
231 "paddb (%3, %%"REG_a
"), %%mm1 \n\t"
232 "movq %%mm0, %%mm2 \n\t"
233 "movq %%mm1, %%mm3 \n\t"
234 "punpcklbw %%mm0, %%mm0 \n\t"
235 "punpckhbw %%mm2, %%mm2 \n\t"
236 "punpcklbw %%mm1, %%mm1 \n\t"
237 "punpckhbw %%mm3, %%mm3 \n\t"
238 "pmulhw %%mm0, %%mm1 \n\t"
239 "pmulhw %%mm2, %%mm3 \n\t"
240 "paddw %%mm1, %%mm1 \n\t"
241 "paddw %%mm3, %%mm3 \n\t"
242 "paddw %%mm0, %%mm1 \n\t"
243 "paddw %%mm2, %%mm3 \n\t"
244 "psrlw $8, %%mm1 \n\t"
245 "psrlw $8, %%mm3 \n\t"
246 "packuswb %%mm3, %%mm1 \n\t"
247 "movq %%mm1, (%4, %%"REG_a
") \n\t"
248 "add $8, %%"REG_a
" \n\t"
250 :: "r" (src
+mmx_len
), "r" (shift
[0]+mmx_len
), "r" (shift
[1]+mmx_len
), "r" (shift
[2]+mmx_len
),
251 "r" (dst
+mmx_len
), "g" (-mmx_len
)
256 int8_t *shift2
[3]={shift
[0]+mmx_len
, shift
[1]+mmx_len
, shift
[2]+mmx_len
};
257 lineNoiseAvg_C(dst
+mmx_len
, src
+mmx_len
, len
-mmx_len
, shift2
);
262 static inline void lineNoiseAvg_C(uint8_t *dst
, uint8_t *src
, int len
, int8_t **shift
){
264 int8_t *src2
= (int8_t*)src
;
268 const int n
= shift
[0][i
] + shift
[1][i
] + shift
[2][i
];
269 dst
[i
]= src2
[i
]+((n
*src2
[i
])>>7);
273 /***************************************************************************/
275 static void noise(uint8_t *dst
, uint8_t *src
, int dstStride
, int srcStride
, int width
, int height
, FilterParam
*fp
){
276 int8_t *noise
= fp
->noise
;
284 if(dstStride
==srcStride
) fast_memcpy(dst
, src
, srcStride
*height
);
287 for(y
=0; y
<height
; y
++)
289 fast_memcpy(dst
, src
, width
);
297 for(y
=0; y
<height
; y
++)
299 if(fp
->temporal
) shift
= rand()&(MAX_SHIFT
-1);
300 else shift
= nonTempRandShift
[y
];
302 if(fp
->quality
==0) shift
&= ~7;
304 lineNoiseAvg(dst
, src
, width
, fp
->prev_shift
[y
]);
305 fp
->prev_shift
[y
][fp
->shiftptr
] = noise
+ shift
;
307 lineNoise(dst
, src
, noise
, width
, shift
);
313 if (fp
->shiftptr
== 3) fp
->shiftptr
= 0;
316 static int config(struct vf_instance
*vf
,
317 int width
, int height
, int d_width
, int d_height
,
318 unsigned int flags
, unsigned int outfmt
){
320 return vf_next_config(vf
,width
,height
,d_width
,d_height
,flags
,outfmt
);
323 static void get_image(struct vf_instance
*vf
, mp_image_t
*mpi
){
324 if(mpi
->flags
&MP_IMGFLAG_PRESERVE
) return; // don't change
325 if(mpi
->imgfmt
!=vf
->priv
->outfmt
) return; // colorspace differ
326 // ok, we can do pp in-place (or pp disabled):
327 vf
->dmpi
=vf_get_image(vf
->next
,mpi
->imgfmt
,
328 mpi
->type
, mpi
->flags
, mpi
->w
, mpi
->h
);
329 mpi
->planes
[0]=vf
->dmpi
->planes
[0];
330 mpi
->stride
[0]=vf
->dmpi
->stride
[0];
331 mpi
->width
=vf
->dmpi
->width
;
332 if(mpi
->flags
&MP_IMGFLAG_PLANAR
){
333 mpi
->planes
[1]=vf
->dmpi
->planes
[1];
334 mpi
->planes
[2]=vf
->dmpi
->planes
[2];
335 mpi
->stride
[1]=vf
->dmpi
->stride
[1];
336 mpi
->stride
[2]=vf
->dmpi
->stride
[2];
338 mpi
->flags
|=MP_IMGFLAG_DIRECT
;
341 static int put_image(struct vf_instance
*vf
, mp_image_t
*mpi
, double pts
){
344 if(!(mpi
->flags
&MP_IMGFLAG_DIRECT
)){
345 // no DR, so get a new image! hope we'll get DR buffer:
346 vf
->dmpi
=vf_get_image(vf
->next
,vf
->priv
->outfmt
,
347 MP_IMGTYPE_TEMP
, MP_IMGFLAG_ACCEPT_STRIDE
,
351 //else printf("dr\n");
354 noise(dmpi
->planes
[0], mpi
->planes
[0], dmpi
->stride
[0], mpi
->stride
[0], mpi
->w
, mpi
->h
, &vf
->priv
->lumaParam
);
355 noise(dmpi
->planes
[1], mpi
->planes
[1], dmpi
->stride
[1], mpi
->stride
[1], mpi
->w
/2, mpi
->h
/2, &vf
->priv
->chromaParam
);
356 noise(dmpi
->planes
[2], mpi
->planes
[2], dmpi
->stride
[2], mpi
->stride
[2], mpi
->w
/2, mpi
->h
/2, &vf
->priv
->chromaParam
);
358 vf_clone_mpi_attributes(dmpi
, mpi
);
361 if(gCpuCaps
.hasMMX
) __asm__
volatile ("emms\n\t");
364 if(gCpuCaps
.hasMMX2
) __asm__
volatile ("sfence\n\t");
367 return vf_next_put_image(vf
,dmpi
, pts
);
370 static void uninit(struct vf_instance
*vf
){
371 if(!vf
->priv
) return;
373 if(vf
->priv
->chromaParam
.noise
) av_free(vf
->priv
->chromaParam
.noise
);
374 vf
->priv
->chromaParam
.noise
= NULL
;
376 if(vf
->priv
->lumaParam
.noise
) av_free(vf
->priv
->lumaParam
.noise
);
377 vf
->priv
->lumaParam
.noise
= NULL
;
383 //===========================================================================//
385 static int query_format(struct vf_instance
*vf
, unsigned int fmt
){
391 return vf_next_query_format(vf
,vf
->priv
->outfmt
);
396 static void parse(FilterParam
*fp
, char* args
){
398 char *max
= strchr(args
, ':');
400 if(!max
) max
= args
+ strlen(args
);
402 fp
->strength
= atoi(args
);
403 pos
= strchr(args
, 'u');
404 if(pos
&& pos
<max
) fp
->uniform
=1;
405 pos
= strchr(args
, 't');
406 if(pos
&& pos
<max
) fp
->temporal
=1;
407 pos
= strchr(args
, 'h');
408 if(pos
&& pos
<max
) fp
->quality
=1;
409 pos
= strchr(args
, 'p');
410 if(pos
&& pos
<max
) fp
->pattern
=1;
411 pos
= strchr(args
, 'a');
417 if(fp
->strength
) initNoise(fp
);
420 static const unsigned int fmt_list
[]={
427 static int vf_open(vf_instance_t
*vf
, char *args
){
429 vf
->put_image
=put_image
;
430 vf
->get_image
=get_image
;
431 vf
->query_format
=query_format
;
433 vf
->priv
=malloc(sizeof(struct vf_priv_s
));
434 memset(vf
->priv
, 0, sizeof(struct vf_priv_s
));
437 char *arg2
= strchr(args
,':');
438 if(arg2
) parse(&vf
->priv
->chromaParam
, arg2
+1);
439 parse(&vf
->priv
->lumaParam
, args
);
443 vf
->priv
->outfmt
=vf_match_csp(&vf
->next
,fmt_list
,IMGFMT_YV12
);
444 if(!vf
->priv
->outfmt
)
447 return 0; // no csp match :(
453 lineNoise
= lineNoise_MMX
;
454 lineNoiseAvg
= lineNoiseAvg_MMX
;
458 if(gCpuCaps
.hasMMX2
) lineNoise
= lineNoise_MMX2
;
459 // if(gCpuCaps.hasMMX) lineNoiseAvg= lineNoiseAvg_MMX2;
465 const vf_info_t vf_info_noise
= {
468 "Michael Niedermayer",
474 //===========================================================================//