6424
|
1 /*
|
|
2 Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
|
|
3
|
|
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.
|
|
8
|
|
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.
|
|
13
|
|
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
|
|
17 */
|
|
18
|
|
19 #include <stdio.h>
|
|
20 #include <stdlib.h>
|
|
21 #include <string.h>
|
|
22 #include <inttypes.h>
|
|
23 #include <math.h>
|
|
24
|
|
25 #include "../config.h"
|
|
26 #include "../mp_msg.h"
|
|
27 #include "../cpudetect.h"
|
|
28
|
|
29 #ifdef HAVE_MALLOC_H
|
|
30 #include <malloc.h>
|
|
31 #endif
|
|
32
|
|
33 #include "img_format.h"
|
|
34 #include "mp_image.h"
|
|
35 #include "vf.h"
|
|
36 #include "../libvo/fastmemcpy.h"
|
|
37
|
|
38 #define MAX_NOISE 4096
|
|
39 #define MAX_SHIFT 1024
|
|
40 #define MAX_RES (MAX_NOISE-MAX_SHIFT)
|
|
41
|
|
42 //===========================================================================//
|
|
43
|
|
44 static inline void lineNoise_C(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift);
|
|
45
|
|
46 static void (*lineNoise)(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift)= lineNoise_C;
|
|
47
|
|
48 typedef struct FilterParam{
|
|
49 int strength;
|
|
50 int uniform;
|
|
51 int temporal;
|
|
52 int8_t *noise;
|
|
53 }FilterParam;
|
|
54
|
|
55 struct vf_priv_s {
|
|
56 FilterParam lumaParam;
|
|
57 FilterParam chromaParam;
|
|
58 };
|
|
59
|
|
60 static int nonTempRandShift[MAX_RES]= {-1};
|
|
61
|
|
62 static int8_t *initNoise(FilterParam *fp){
|
|
63 int strength= fp->strength;
|
|
64 int uniform= fp->uniform;
|
|
65 int8_t *noise= memalign(16, MAX_NOISE*sizeof(int8_t)); //FIXME deallocate
|
|
66 int i;
|
|
67
|
|
68 srand(123457);
|
|
69
|
|
70 for(i=0; i<MAX_NOISE; i++)
|
|
71 {
|
|
72 if(uniform)
|
|
73 noise[i]= ((rand()/11)%strength) - strength/2;
|
|
74 else
|
|
75 {
|
|
76 double x1, x2, w, y1;
|
|
77 do {
|
|
78 x1 = 2.0 * rand()/(float)RAND_MAX - 1.0;
|
|
79 x2 = 2.0 * rand()/(float)RAND_MAX - 1.0;
|
|
80 w = x1 * x1 + x2 * x2;
|
|
81 } while ( w >= 1.0 );
|
|
82
|
|
83 w = sqrt( (-2.0 * log( w ) ) / w );
|
|
84 y1= x1 * w;
|
|
85
|
|
86 y1*= strength / sqrt(3.0);
|
|
87 if (y1<-128) y1=-128;
|
|
88 else if(y1> 127) y1= 127;
|
|
89 noise[i]= (int)y1;
|
|
90 }
|
|
91 }
|
|
92
|
|
93 if(nonTempRandShift[0]==-1){
|
|
94 for(i=0; i<MAX_RES; i++){
|
|
95 nonTempRandShift[i]= rand()&(MAX_SHIFT-1);
|
|
96 }
|
|
97 }
|
|
98
|
|
99 fp->noise= noise;
|
|
100 return noise;
|
|
101 }
|
|
102
|
|
103 #ifdef HAVE_MMX
|
|
104 static inline void lineNoise_MMX(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift){
|
|
105 int mmx_len= len&(~7);
|
|
106 shift&= ~7;
|
|
107 noise+=shift;
|
|
108
|
|
109 asm volatile(
|
|
110 "movl %3, %%eax \n\t"
|
|
111 "pcmpeqb %%mm7, %%mm7 \n\t"
|
|
112 "psllw $15, %%mm7 \n\t"
|
|
113 "packsswb %%mm7, %%mm7 \n\t"
|
|
114 ".balign 16 \n\t"
|
|
115 "1: \n\t"
|
|
116 "movq (%0, %%eax), %%mm0 \n\t"
|
|
117 "movq (%1, %%eax), %%mm1 \n\t"
|
|
118 "pxor %%mm7, %%mm0 \n\t"
|
|
119 "paddsb %%mm1, %%mm0 \n\t"
|
|
120 "pxor %%mm7, %%mm0 \n\t"
|
|
121 "movq %%mm0, (%2, %%eax) \n\t"
|
|
122 "addl $8, %%eax \n\t"
|
|
123 " js 1b \n\t"
|
|
124 :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
|
|
125 : "%eax"
|
|
126 );
|
|
127 if(mmx_len!=len)
|
|
128 lineNoise_C(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
|
|
129 }
|
|
130 #endif
|
|
131
|
|
132 //duplicate of previous except movntq
|
|
133 #ifdef HAVE_MMX2
|
|
134 static inline void lineNoise_MMX2(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift){
|
|
135 int mmx_len= len&(~7);
|
|
136 shift&= ~7;
|
|
137 noise+=shift;
|
|
138
|
|
139 asm volatile(
|
|
140 "movl %3, %%eax \n\t"
|
|
141 "pcmpeqb %%mm7, %%mm7 \n\t"
|
|
142 "psllw $15, %%mm7 \n\t"
|
|
143 "packsswb %%mm7, %%mm7 \n\t"
|
|
144 ".balign 16 \n\t"
|
|
145 "1: \n\t"
|
|
146 "movq (%0, %%eax), %%mm0 \n\t"
|
|
147 "movq (%1, %%eax), %%mm1 \n\t"
|
|
148 "pxor %%mm7, %%mm0 \n\t"
|
|
149 "paddsb %%mm1, %%mm0 \n\t"
|
|
150 "pxor %%mm7, %%mm0 \n\t"
|
|
151 "movntq %%mm0, (%2, %%eax) \n\t"
|
|
152 "addl $8, %%eax \n\t"
|
|
153 " js 1b \n\t"
|
|
154 :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
|
|
155 : "%eax"
|
|
156 );
|
|
157 if(mmx_len!=len)
|
|
158 lineNoise_C(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
|
|
159 }
|
|
160 #endif
|
|
161
|
|
162 static inline void lineNoise_C(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift){
|
|
163 int i;
|
|
164 noise+= shift;
|
|
165 for(i=0; i<len; i++)
|
|
166 {
|
|
167 int v= src[i]+ noise[i];
|
|
168 if(v>255) dst[i]=255; //FIXME optimize
|
|
169 else if(v<0) dst[i]=0;
|
|
170 else dst[i]=v;
|
|
171 }
|
|
172 }
|
|
173
|
|
174 static void noise(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height, FilterParam *fp){
|
|
175 int8_t *noise= fp->noise;
|
|
176 int y;
|
|
177 int shift=0;
|
|
178
|
|
179 if(!noise)
|
|
180 {
|
|
181 if(dstStride==srcStride) memcpy(dst, src, srcStride*height);
|
|
182 else
|
|
183 {
|
|
184 for(y=0; y<height; y++)
|
|
185 {
|
|
186 memcpy(dst, src, width);
|
|
187 dst+= dstStride;
|
|
188 src+= srcStride;
|
|
189 }
|
|
190 }
|
|
191 return;
|
|
192 }
|
|
193
|
|
194 for(y=0; y<height; y++)
|
|
195 {
|
|
196 if(fp->temporal) shift= rand()&(MAX_SHIFT -1);
|
|
197 else shift= nonTempRandShift[y];
|
|
198
|
|
199 lineNoise(dst, src, noise, width, shift);
|
|
200 dst+= dstStride;
|
|
201 src+= srcStride;
|
|
202 }
|
|
203 }
|
|
204
|
|
205 static int config(struct vf_instance_s* vf,
|
|
206 int width, int height, int d_width, int d_height,
|
|
207 unsigned int flags, unsigned int outfmt){
|
|
208
|
|
209 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
|
|
210 }
|
|
211
|
|
212 static void put_image(struct vf_instance_s* vf, mp_image_t *mpi){
|
|
213 mp_image_t *dmpi;
|
|
214
|
|
215 // hope we'll get DR buffer:
|
|
216 dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
|
217 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
|
|
218 mpi->w, mpi->h);
|
|
219
|
|
220 noise(dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, &vf->priv->lumaParam);
|
|
221 noise(dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w/2, mpi->h/2, &vf->priv->chromaParam);
|
|
222 noise(dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w/2, mpi->h/2, &vf->priv->chromaParam);
|
|
223
|
|
224 dmpi->qscale=mpi->qscale;
|
|
225 dmpi->qstride=mpi->qstride;
|
|
226
|
|
227 #ifdef HAVE_MMX
|
|
228 if(gCpuCaps.hasMMX) asm volatile ("emms\n\t");
|
|
229 #endif
|
|
230 #ifdef HAVE_MMX2
|
|
231 if(gCpuCaps.hasMMX2) asm volatile ("sfence\n\t");
|
|
232 #endif
|
|
233
|
|
234 vf_next_put_image(vf,dmpi);
|
|
235 }
|
|
236
|
|
237 //===========================================================================//
|
|
238
|
|
239 static int query_format(struct vf_instance_s* vf, unsigned int fmt){
|
|
240 switch(fmt)
|
|
241 {
|
|
242 case IMGFMT_YV12:
|
|
243 case IMGFMT_I420:
|
|
244 case IMGFMT_IYUV:
|
|
245 return vf_next_query_format(vf,fmt);
|
|
246 }
|
|
247 return 0;
|
|
248 }
|
|
249
|
|
250 static void parse(FilterParam *fp, char* args){
|
|
251 char *pos;
|
|
252 char *max= strchr(args, ':');
|
|
253
|
|
254 if(!max) max= args + strlen(args);
|
|
255
|
|
256 fp->strength= atoi(args);
|
|
257 pos= strchr(args, 'u');
|
|
258 if(pos && pos<max) fp->uniform=1;
|
|
259 pos= strchr(args, 't');
|
|
260 if(pos && pos<max) fp->temporal=1;
|
|
261
|
|
262 if(fp->strength) initNoise(fp);
|
|
263 }
|
|
264
|
|
265 static int open(vf_instance_t *vf, char* args){
|
|
266 vf->config=config;
|
|
267 vf->put_image=put_image;
|
|
268 vf->query_format=query_format;
|
|
269 vf->priv=malloc(sizeof(struct vf_priv_s));
|
|
270 memset(vf->priv, 0, sizeof(struct vf_priv_s));
|
|
271 if(args)
|
|
272 {
|
|
273 char *arg2= strchr(args,':');
|
|
274 if(arg2) parse(&vf->priv->chromaParam, arg2+1);
|
|
275 parse(&vf->priv->lumaParam, args);
|
|
276 }
|
|
277
|
|
278 #ifdef HAVE_MMX
|
|
279 if(gCpuCaps.hasMMX) lineNoise= lineNoise_MMX;
|
|
280 #endif
|
|
281 #ifdef HAVE_MMX2
|
|
282 if(gCpuCaps.hasMMX2) lineNoise= lineNoise_MMX2;
|
|
283 #endif
|
|
284
|
|
285 return 1;
|
|
286 }
|
|
287
|
|
288 vf_info_t vf_info_noise = {
|
|
289 "noise genenerator",
|
|
290 "noise",
|
|
291 "Michael Niedermayer",
|
|
292 "",
|
|
293 open
|
|
294 };
|
|
295
|
|
296 //===========================================================================//
|