ad_libav: flush decoder output after last input packet
[mplayer.git] / libmpcodecs / vf_noise.c
blobc623e1156242bdcfcb20538707c5a37ea706fcb4
1 /*
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.
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <inttypes.h>
25 #include <math.h>
27 #include "config.h"
28 #include "mp_msg.h"
29 #include "cpudetect.h"
31 #include "img_format.h"
32 #include "mp_image.h"
33 #include "vf.h"
34 #include "libvo/fastmemcpy.h"
35 #include "libavutil/mem.h"
37 #define MAX_NOISE 4096
38 #define MAX_SHIFT 1024
39 #define MAX_RES (MAX_NOISE-MAX_SHIFT)
41 //===========================================================================//
43 static inline void lineNoise_C(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift);
44 static inline void lineNoiseAvg_C(uint8_t *dst, uint8_t *src, int len, int8_t **shift);
46 static void (*lineNoise)(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift)= lineNoise_C;
47 static void (*lineNoiseAvg)(uint8_t *dst, uint8_t *src, int len, int8_t **shift)= lineNoiseAvg_C;
49 typedef struct FilterParam{
50 int strength;
51 int uniform;
52 int temporal;
53 int quality;
54 int averaged;
55 int pattern;
56 int shiftptr;
57 int8_t *noise;
58 int8_t *prev_shift[MAX_RES][3];
59 }FilterParam;
61 struct vf_priv_s {
62 FilterParam lumaParam;
63 FilterParam chromaParam;
64 unsigned int outfmt;
67 static int nonTempRandShift_init;
68 static int nonTempRandShift[MAX_RES];
70 static int patt[4] = {
71 -1,0,1,0
74 #define RAND_N(range) ((int) ((double)range*rand()/(RAND_MAX+1.0)))
75 static int8_t *initNoise(FilterParam *fp){
76 int strength= fp->strength;
77 int uniform= fp->uniform;
78 int averaged= fp->averaged;
79 int pattern= fp->pattern;
80 int8_t *noise= av_malloc(MAX_NOISE*sizeof(int8_t));
81 int i, j;
83 srand(123457);
85 for(i=0,j=0; i<MAX_NOISE; i++,j++)
87 if(uniform) {
88 if (averaged) {
89 if (pattern) {
90 noise[i]= (RAND_N(strength) - strength/2)/6
91 +patt[j%4]*strength*0.25/3;
92 } else {
93 noise[i]= (RAND_N(strength) - strength/2)/3;
95 } else {
96 if (pattern) {
97 noise[i]= (RAND_N(strength) - strength/2)/2
98 + patt[j%4]*strength*0.25;
99 } else {
100 noise[i]= RAND_N(strength) - strength/2;
103 } else {
104 double x1, x2, w, y1;
105 do {
106 x1 = 2.0 * rand()/(float)RAND_MAX - 1.0;
107 x2 = 2.0 * rand()/(float)RAND_MAX - 1.0;
108 w = x1 * x1 + x2 * x2;
109 } while ( w >= 1.0 );
111 w = sqrt( (-2.0 * log( w ) ) / w );
112 y1= x1 * w;
113 y1*= strength / sqrt(3.0);
114 if (pattern) {
115 y1 /= 2;
116 y1 += patt[j%4]*strength*0.35;
118 if (y1<-128) y1=-128;
119 else if(y1> 127) y1= 127;
120 if (averaged) y1 /= 3.0;
121 noise[i]= (int)y1;
123 if (RAND_N(6) == 0) j--;
127 for (i = 0; i < MAX_RES; i++)
128 for (j = 0; j < 3; j++)
129 fp->prev_shift[i][j] = noise + (rand()&(MAX_SHIFT-1));
131 if(!nonTempRandShift_init){
132 for(i=0; i<MAX_RES; i++){
133 nonTempRandShift[i]= rand()&(MAX_SHIFT-1);
135 nonTempRandShift_init = 1;
138 fp->noise= noise;
139 fp->shiftptr= 0;
140 return noise;
143 /***************************************************************************/
145 #if HAVE_MMX
146 static inline void lineNoise_MMX(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift){
147 x86_reg mmx_len= len&(~7);
148 noise+=shift;
150 __asm__ volatile(
151 "mov %3, %%"REG_a" \n\t"
152 "pcmpeqb %%mm7, %%mm7 \n\t"
153 "psllw $15, %%mm7 \n\t"
154 "packsswb %%mm7, %%mm7 \n\t"
155 ASMALIGN(4)
156 "1: \n\t"
157 "movq (%0, %%"REG_a"), %%mm0 \n\t"
158 "movq (%1, %%"REG_a"), %%mm1 \n\t"
159 "pxor %%mm7, %%mm0 \n\t"
160 "paddsb %%mm1, %%mm0 \n\t"
161 "pxor %%mm7, %%mm0 \n\t"
162 "movq %%mm0, (%2, %%"REG_a") \n\t"
163 "add $8, %%"REG_a" \n\t"
164 " js 1b \n\t"
165 :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
166 : "%"REG_a
168 if(mmx_len!=len)
169 lineNoise_C(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
171 #endif
173 //duplicate of previous except movntq
174 #if HAVE_MMX2
175 static inline void lineNoise_MMX2(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift){
176 x86_reg mmx_len= len&(~7);
177 noise+=shift;
179 __asm__ volatile(
180 "mov %3, %%"REG_a" \n\t"
181 "pcmpeqb %%mm7, %%mm7 \n\t"
182 "psllw $15, %%mm7 \n\t"
183 "packsswb %%mm7, %%mm7 \n\t"
184 ASMALIGN(4)
185 "1: \n\t"
186 "movq (%0, %%"REG_a"), %%mm0 \n\t"
187 "movq (%1, %%"REG_a"), %%mm1 \n\t"
188 "pxor %%mm7, %%mm0 \n\t"
189 "paddsb %%mm1, %%mm0 \n\t"
190 "pxor %%mm7, %%mm0 \n\t"
191 "movntq %%mm0, (%2, %%"REG_a") \n\t"
192 "add $8, %%"REG_a" \n\t"
193 " js 1b \n\t"
194 :: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
195 : "%"REG_a
197 if(mmx_len!=len)
198 lineNoise_C(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
200 #endif
202 static inline void lineNoise_C(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift){
203 int i;
204 noise+= shift;
205 for(i=0; i<len; i++)
207 int v= src[i]+ noise[i];
208 if(v>255) dst[i]=255; //FIXME optimize
209 else if(v<0) dst[i]=0;
210 else dst[i]=v;
214 /***************************************************************************/
216 #if HAVE_MMX
217 static inline void lineNoiseAvg_MMX(uint8_t *dst, uint8_t *src, int len, int8_t **shift){
218 x86_reg mmx_len= len&(~7);
220 __asm__ volatile(
221 "mov %5, %%"REG_a" \n\t"
222 ASMALIGN(4)
223 "1: \n\t"
224 "movq (%1, %%"REG_a"), %%mm1 \n\t"
225 "movq (%0, %%"REG_a"), %%mm0 \n\t"
226 "paddb (%2, %%"REG_a"), %%mm1 \n\t"
227 "paddb (%3, %%"REG_a"), %%mm1 \n\t"
228 "movq %%mm0, %%mm2 \n\t"
229 "movq %%mm1, %%mm3 \n\t"
230 "punpcklbw %%mm0, %%mm0 \n\t"
231 "punpckhbw %%mm2, %%mm2 \n\t"
232 "punpcklbw %%mm1, %%mm1 \n\t"
233 "punpckhbw %%mm3, %%mm3 \n\t"
234 "pmulhw %%mm0, %%mm1 \n\t"
235 "pmulhw %%mm2, %%mm3 \n\t"
236 "paddw %%mm1, %%mm1 \n\t"
237 "paddw %%mm3, %%mm3 \n\t"
238 "paddw %%mm0, %%mm1 \n\t"
239 "paddw %%mm2, %%mm3 \n\t"
240 "psrlw $8, %%mm1 \n\t"
241 "psrlw $8, %%mm3 \n\t"
242 "packuswb %%mm3, %%mm1 \n\t"
243 "movq %%mm1, (%4, %%"REG_a") \n\t"
244 "add $8, %%"REG_a" \n\t"
245 " js 1b \n\t"
246 :: "r" (src+mmx_len), "r" (shift[0]+mmx_len), "r" (shift[1]+mmx_len), "r" (shift[2]+mmx_len),
247 "r" (dst+mmx_len), "g" (-mmx_len)
248 : "%"REG_a
251 if(mmx_len!=len){
252 int8_t *shift2[3]={shift[0]+mmx_len, shift[1]+mmx_len, shift[2]+mmx_len};
253 lineNoiseAvg_C(dst+mmx_len, src+mmx_len, len-mmx_len, shift2);
256 #endif
258 static inline void lineNoiseAvg_C(uint8_t *dst, uint8_t *src, int len, int8_t **shift){
259 int i;
260 int8_t *src2= (int8_t*)src;
262 for(i=0; i<len; i++)
264 const int n= shift[0][i] + shift[1][i] + shift[2][i];
265 dst[i]= src2[i]+((n*src2[i])>>7);
269 /***************************************************************************/
271 static void noise(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height, FilterParam *fp){
272 int8_t *noise= fp->noise;
273 int y;
274 int shift=0;
276 if(!noise)
278 if(src==dst) return;
280 if(dstStride==srcStride) fast_memcpy(dst, src, srcStride*height);
281 else
283 for(y=0; y<height; y++)
285 fast_memcpy(dst, src, width);
286 dst+= dstStride;
287 src+= srcStride;
290 return;
293 for(y=0; y<height; y++)
295 if(fp->temporal) shift= rand()&(MAX_SHIFT -1);
296 else shift= nonTempRandShift[y];
298 if(fp->quality==0) shift&= ~7;
299 if (fp->averaged) {
300 lineNoiseAvg(dst, src, width, fp->prev_shift[y]);
301 fp->prev_shift[y][fp->shiftptr] = noise + shift;
302 } else {
303 lineNoise(dst, src, noise, width, shift);
305 dst+= dstStride;
306 src+= srcStride;
308 fp->shiftptr++;
309 if (fp->shiftptr == 3) fp->shiftptr = 0;
312 static int config(struct vf_instance *vf,
313 int width, int height, int d_width, int d_height,
314 unsigned int flags, unsigned int outfmt){
316 return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
319 static void get_image(struct vf_instance *vf, mp_image_t *mpi){
320 if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
321 if(mpi->imgfmt!=vf->priv->outfmt) return; // colorspace differ
322 // ok, we can do pp in-place (or pp disabled):
323 vf->dmpi=vf_get_image(vf->next,mpi->imgfmt,
324 mpi->type, mpi->flags, mpi->w, mpi->h);
325 mpi->planes[0]=vf->dmpi->planes[0];
326 mpi->stride[0]=vf->dmpi->stride[0];
327 mpi->width=vf->dmpi->width;
328 if(mpi->flags&MP_IMGFLAG_PLANAR){
329 mpi->planes[1]=vf->dmpi->planes[1];
330 mpi->planes[2]=vf->dmpi->planes[2];
331 mpi->stride[1]=vf->dmpi->stride[1];
332 mpi->stride[2]=vf->dmpi->stride[2];
334 mpi->flags|=MP_IMGFLAG_DIRECT;
337 static int put_image(struct vf_instance *vf, mp_image_t *mpi, double pts){
338 mp_image_t *dmpi;
340 if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
341 // no DR, so get a new image! hope we'll get DR buffer:
342 vf->dmpi=vf_get_image(vf->next,vf->priv->outfmt,
343 MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
344 mpi->w,mpi->h);
345 //printf("nodr\n");
347 //else printf("dr\n");
348 dmpi= vf->dmpi;
350 noise(dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, &vf->priv->lumaParam);
351 noise(dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w/2, mpi->h/2, &vf->priv->chromaParam);
352 noise(dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w/2, mpi->h/2, &vf->priv->chromaParam);
354 vf_clone_mpi_attributes(dmpi, mpi);
356 #if HAVE_MMX
357 if(gCpuCaps.hasMMX) __asm__ volatile ("emms\n\t");
358 #endif
359 #if HAVE_MMX2
360 if(gCpuCaps.hasMMX2) __asm__ volatile ("sfence\n\t");
361 #endif
363 return vf_next_put_image(vf,dmpi, pts);
366 static void uninit(struct vf_instance *vf){
367 if(!vf->priv) return;
369 av_free(vf->priv->chromaParam.noise);
370 vf->priv->chromaParam.noise= NULL;
372 av_free(vf->priv->lumaParam.noise);
373 vf->priv->lumaParam.noise= NULL;
375 free(vf->priv);
376 vf->priv=NULL;
379 //===========================================================================//
381 static int query_format(struct vf_instance *vf, unsigned int fmt){
382 switch(fmt)
384 case IMGFMT_YV12:
385 case IMGFMT_I420:
386 case IMGFMT_IYUV:
387 return vf_next_query_format(vf,vf->priv->outfmt);
389 return 0;
392 static void parse(FilterParam *fp, char* args){
393 char *pos;
394 char *max= strchr(args, ':');
396 if(!max) max= args + strlen(args);
398 fp->strength= atoi(args);
399 pos= strchr(args, 'u');
400 if(pos && pos<max) fp->uniform=1;
401 pos= strchr(args, 't');
402 if(pos && pos<max) fp->temporal=1;
403 pos= strchr(args, 'h');
404 if(pos && pos<max) fp->quality=1;
405 pos= strchr(args, 'p');
406 if(pos && pos<max) fp->pattern=1;
407 pos= strchr(args, 'a');
408 if(pos && pos<max) {
409 fp->temporal=1;
410 fp->averaged=1;
413 if(fp->strength) initNoise(fp);
416 static const unsigned int fmt_list[]={
417 IMGFMT_YV12,
418 IMGFMT_I420,
419 IMGFMT_IYUV,
423 static int vf_open(vf_instance_t *vf, char *args){
424 vf->config=config;
425 vf->put_image=put_image;
426 vf->get_image=get_image;
427 vf->query_format=query_format;
428 vf->uninit=uninit;
429 vf->priv=malloc(sizeof(struct vf_priv_s));
430 memset(vf->priv, 0, sizeof(struct vf_priv_s));
431 if(args)
433 char *arg2= strchr(args,':');
434 if(arg2) parse(&vf->priv->chromaParam, arg2+1);
435 parse(&vf->priv->lumaParam, args);
438 // check csp:
439 vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
440 if(!vf->priv->outfmt)
442 uninit(vf);
443 return 0; // no csp match :(
447 #if HAVE_MMX
448 if(gCpuCaps.hasMMX){
449 lineNoise= lineNoise_MMX;
450 lineNoiseAvg= lineNoiseAvg_MMX;
452 #endif
453 #if HAVE_MMX2
454 if(gCpuCaps.hasMMX2) lineNoise= lineNoise_MMX2;
455 // if(gCpuCaps.hasMMX) lineNoiseAvg= lineNoiseAvg_MMX2;
456 #endif
458 return 1;
461 const vf_info_t vf_info_noise = {
462 "noise generator",
463 "noise",
464 "Michael Niedermayer",
466 vf_open,
467 NULL
470 //===========================================================================//