2002-06-14 03:14:56 +02:00
|
|
|
/*
|
|
|
|
Copyright (C) 2002 Michael Niedermayer <michaelni@gmx.at>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
#include "../config.h"
|
|
|
|
#include "../mp_msg.h"
|
|
|
|
#include "../cpudetect.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_MALLOC_H
|
|
|
|
#include <malloc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "img_format.h"
|
|
|
|
#include "mp_image.h"
|
|
|
|
#include "vf.h"
|
|
|
|
#include "../libvo/fastmemcpy.h"
|
|
|
|
|
|
|
|
#define MAX_NOISE 4096
|
|
|
|
#define MAX_SHIFT 1024
|
|
|
|
#define MAX_RES (MAX_NOISE-MAX_SHIFT)
|
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
|
|
|
static inline void lineNoise_C(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift);
|
2002-08-11 11:46:50 +02:00
|
|
|
static inline void lineNoiseAvg_C(uint8_t *dst, uint8_t *src, int len, int8_t **shift);
|
2002-06-14 03:14:56 +02:00
|
|
|
|
|
|
|
static void (*lineNoise)(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift)= lineNoise_C;
|
2002-08-11 11:46:50 +02:00
|
|
|
static void (*lineNoiseAvg)(uint8_t *dst, uint8_t *src, int len, int8_t **shift)= lineNoiseAvg_C;
|
2002-06-14 03:14:56 +02:00
|
|
|
|
|
|
|
typedef struct FilterParam{
|
|
|
|
int strength;
|
|
|
|
int uniform;
|
|
|
|
int temporal;
|
2002-06-16 15:54:36 +02:00
|
|
|
int quality;
|
2002-08-11 11:46:50 +02:00
|
|
|
int averaged;
|
|
|
|
int shiftptr;
|
2002-06-14 03:14:56 +02:00
|
|
|
int8_t *noise;
|
2002-08-11 11:52:23 +02:00
|
|
|
int8_t *prev_shift[MAX_RES][3];
|
2002-06-14 03:14:56 +02:00
|
|
|
}FilterParam;
|
|
|
|
|
|
|
|
struct vf_priv_s {
|
|
|
|
FilterParam lumaParam;
|
|
|
|
FilterParam chromaParam;
|
2002-06-16 15:45:52 +02:00
|
|
|
mp_image_t *dmpi;
|
|
|
|
unsigned int outfmt;
|
2002-06-14 03:14:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static int nonTempRandShift[MAX_RES]= {-1};
|
|
|
|
|
|
|
|
static int8_t *initNoise(FilterParam *fp){
|
|
|
|
int strength= fp->strength;
|
|
|
|
int uniform= fp->uniform;
|
2002-08-11 11:46:50 +02:00
|
|
|
int averaged= fp->averaged;
|
2002-06-16 15:45:52 +02:00
|
|
|
int8_t *noise= memalign(16, MAX_NOISE*sizeof(int8_t));
|
2002-08-11 11:46:50 +02:00
|
|
|
int i, j;
|
2002-06-14 03:14:56 +02:00
|
|
|
|
|
|
|
srand(123457);
|
|
|
|
|
|
|
|
for(i=0; i<MAX_NOISE; i++)
|
|
|
|
{
|
2002-08-11 11:46:50 +02:00
|
|
|
if(uniform) {
|
|
|
|
if (averaged) {
|
|
|
|
noise[i]= (((rand()/11)%strength) - strength/2)/3;
|
|
|
|
} else {
|
|
|
|
noise[i]= ((rand()/11)%strength) - strength/2;
|
|
|
|
}
|
|
|
|
} else {
|
2002-06-14 03:14:56 +02:00
|
|
|
double x1, x2, w, y1;
|
|
|
|
do {
|
|
|
|
x1 = 2.0 * rand()/(float)RAND_MAX - 1.0;
|
|
|
|
x2 = 2.0 * rand()/(float)RAND_MAX - 1.0;
|
|
|
|
w = x1 * x1 + x2 * x2;
|
|
|
|
} while ( w >= 1.0 );
|
|
|
|
|
|
|
|
w = sqrt( (-2.0 * log( w ) ) / w );
|
|
|
|
y1= x1 * w;
|
|
|
|
|
2002-08-11 11:46:50 +02:00
|
|
|
y1*= strength / sqrt(3.0);
|
2002-06-14 03:14:56 +02:00
|
|
|
if (y1<-128) y1=-128;
|
|
|
|
else if(y1> 127) y1= 127;
|
2002-08-11 11:46:50 +02:00
|
|
|
if (averaged) y1 /= 3.0;
|
2002-06-14 03:14:56 +02:00
|
|
|
noise[i]= (int)y1;
|
|
|
|
}
|
|
|
|
}
|
2002-08-11 11:46:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_RES; i++)
|
|
|
|
for (j = 0; j < 3; j++)
|
2002-08-11 11:52:23 +02:00
|
|
|
fp->prev_shift[i][j] = noise + (rand()&(MAX_SHIFT-1));
|
2002-06-14 03:14:56 +02:00
|
|
|
|
|
|
|
if(nonTempRandShift[0]==-1){
|
|
|
|
for(i=0; i<MAX_RES; i++){
|
|
|
|
nonTempRandShift[i]= rand()&(MAX_SHIFT-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fp->noise= noise;
|
2002-08-11 11:46:50 +02:00
|
|
|
fp->shiftptr= 0;
|
2002-06-14 03:14:56 +02:00
|
|
|
return noise;
|
|
|
|
}
|
|
|
|
|
2002-08-11 11:46:50 +02:00
|
|
|
/***************************************************************************/
|
|
|
|
|
2002-06-14 03:14:56 +02:00
|
|
|
#ifdef HAVE_MMX
|
|
|
|
static inline void lineNoise_MMX(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift){
|
|
|
|
int mmx_len= len&(~7);
|
|
|
|
noise+=shift;
|
|
|
|
|
|
|
|
asm volatile(
|
|
|
|
"movl %3, %%eax \n\t"
|
|
|
|
"pcmpeqb %%mm7, %%mm7 \n\t"
|
|
|
|
"psllw $15, %%mm7 \n\t"
|
|
|
|
"packsswb %%mm7, %%mm7 \n\t"
|
|
|
|
".balign 16 \n\t"
|
|
|
|
"1: \n\t"
|
|
|
|
"movq (%0, %%eax), %%mm0 \n\t"
|
|
|
|
"movq (%1, %%eax), %%mm1 \n\t"
|
|
|
|
"pxor %%mm7, %%mm0 \n\t"
|
|
|
|
"paddsb %%mm1, %%mm0 \n\t"
|
|
|
|
"pxor %%mm7, %%mm0 \n\t"
|
|
|
|
"movq %%mm0, (%2, %%eax) \n\t"
|
|
|
|
"addl $8, %%eax \n\t"
|
|
|
|
" js 1b \n\t"
|
|
|
|
:: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
|
|
|
|
: "%eax"
|
|
|
|
);
|
|
|
|
if(mmx_len!=len)
|
|
|
|
lineNoise_C(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//duplicate of previous except movntq
|
|
|
|
#ifdef HAVE_MMX2
|
|
|
|
static inline void lineNoise_MMX2(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift){
|
|
|
|
int mmx_len= len&(~7);
|
|
|
|
noise+=shift;
|
|
|
|
|
|
|
|
asm volatile(
|
|
|
|
"movl %3, %%eax \n\t"
|
|
|
|
"pcmpeqb %%mm7, %%mm7 \n\t"
|
|
|
|
"psllw $15, %%mm7 \n\t"
|
|
|
|
"packsswb %%mm7, %%mm7 \n\t"
|
|
|
|
".balign 16 \n\t"
|
|
|
|
"1: \n\t"
|
|
|
|
"movq (%0, %%eax), %%mm0 \n\t"
|
|
|
|
"movq (%1, %%eax), %%mm1 \n\t"
|
|
|
|
"pxor %%mm7, %%mm0 \n\t"
|
|
|
|
"paddsb %%mm1, %%mm0 \n\t"
|
|
|
|
"pxor %%mm7, %%mm0 \n\t"
|
|
|
|
"movntq %%mm0, (%2, %%eax) \n\t"
|
|
|
|
"addl $8, %%eax \n\t"
|
|
|
|
" js 1b \n\t"
|
|
|
|
:: "r" (src+mmx_len), "r" (noise+mmx_len), "r" (dst+mmx_len), "g" (-mmx_len)
|
|
|
|
: "%eax"
|
|
|
|
);
|
|
|
|
if(mmx_len!=len)
|
|
|
|
lineNoise_C(dst+mmx_len, src+mmx_len, noise+mmx_len, len-mmx_len, 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline void lineNoise_C(uint8_t *dst, uint8_t *src, int8_t *noise, int len, int shift){
|
|
|
|
int i;
|
|
|
|
noise+= shift;
|
|
|
|
for(i=0; i<len; i++)
|
|
|
|
{
|
|
|
|
int v= src[i]+ noise[i];
|
|
|
|
if(v>255) dst[i]=255; //FIXME optimize
|
|
|
|
else if(v<0) dst[i]=0;
|
|
|
|
else dst[i]=v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-11 11:46:50 +02:00
|
|
|
/***************************************************************************/
|
|
|
|
|
|
|
|
static inline void lineNoiseAvg_C(uint8_t *dst, uint8_t *src, int len, int8_t **shift){
|
2002-08-11 11:52:23 +02:00
|
|
|
int i, j, n;
|
2002-08-11 11:46:50 +02:00
|
|
|
|
|
|
|
for(i=0; i<len; i++)
|
|
|
|
{
|
|
|
|
for(j=0,n=0;j<3;j++)
|
|
|
|
n+=shift[j][i];
|
|
|
|
dst[i]= src[i]+n*(128-abs(128-src[i]))/128;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************/
|
|
|
|
|
2002-06-14 03:14:56 +02:00
|
|
|
static void noise(uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height, FilterParam *fp){
|
|
|
|
int8_t *noise= fp->noise;
|
|
|
|
int y;
|
|
|
|
int shift=0;
|
|
|
|
|
|
|
|
if(!noise)
|
|
|
|
{
|
2002-06-16 15:45:52 +02:00
|
|
|
if(src==dst) return;
|
|
|
|
|
2002-06-14 03:14:56 +02:00
|
|
|
if(dstStride==srcStride) memcpy(dst, src, srcStride*height);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(y=0; y<height; y++)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, width);
|
|
|
|
dst+= dstStride;
|
2002-06-16 15:54:36 +02:00
|
|
|
src+= srcStride;
|
2002-06-14 03:14:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(y=0; y<height; y++)
|
|
|
|
{
|
|
|
|
if(fp->temporal) shift= rand()&(MAX_SHIFT -1);
|
|
|
|
else shift= nonTempRandShift[y];
|
|
|
|
|
2002-06-16 15:54:36 +02:00
|
|
|
if(fp->quality==0) shift&= ~7;
|
2002-08-11 11:46:50 +02:00
|
|
|
if (fp->averaged) {
|
2002-08-11 11:52:23 +02:00
|
|
|
lineNoiseAvg(dst, src, width, fp->prev_shift[y]);
|
|
|
|
fp->prev_shift[y][fp->shiftptr] = noise + shift;
|
2002-08-11 11:46:50 +02:00
|
|
|
} else {
|
|
|
|
lineNoise(dst, src, noise, width, shift);
|
|
|
|
}
|
2002-06-14 03:14:56 +02:00
|
|
|
dst+= dstStride;
|
|
|
|
src+= srcStride;
|
|
|
|
}
|
2002-08-11 11:46:50 +02:00
|
|
|
fp->shiftptr++;
|
|
|
|
if (fp->shiftptr == 3) fp->shiftptr = 0;
|
2002-06-14 03:14:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int config(struct vf_instance_s* vf,
|
|
|
|
int width, int height, int d_width, int d_height,
|
|
|
|
unsigned int flags, unsigned int outfmt){
|
2002-06-16 15:54:36 +02:00
|
|
|
|
2002-06-14 03:14:56 +02:00
|
|
|
return vf_next_config(vf,width,height,d_width,d_height,flags,outfmt);
|
|
|
|
}
|
|
|
|
|
2002-06-16 15:45:52 +02:00
|
|
|
static void get_image(struct vf_instance_s* vf, mp_image_t *mpi){
|
|
|
|
if(mpi->flags&MP_IMGFLAG_PRESERVE) return; // don't change
|
2002-08-11 11:40:57 +02:00
|
|
|
if(mpi->imgfmt!=vf->priv->outfmt) return; // colorspace differ
|
2002-06-16 15:45:52 +02:00
|
|
|
// ok, we can do pp in-place (or pp disabled):
|
|
|
|
vf->priv->dmpi=vf_get_image(vf->next,mpi->imgfmt,
|
|
|
|
mpi->type, mpi->flags, mpi->w, mpi->h);
|
|
|
|
mpi->planes[0]=vf->priv->dmpi->planes[0];
|
|
|
|
mpi->stride[0]=vf->priv->dmpi->stride[0];
|
|
|
|
mpi->width=vf->priv->dmpi->width;
|
|
|
|
if(mpi->flags&MP_IMGFLAG_PLANAR){
|
|
|
|
mpi->planes[1]=vf->priv->dmpi->planes[1];
|
|
|
|
mpi->planes[2]=vf->priv->dmpi->planes[2];
|
|
|
|
mpi->stride[1]=vf->priv->dmpi->stride[1];
|
|
|
|
mpi->stride[2]=vf->priv->dmpi->stride[2];
|
|
|
|
}
|
|
|
|
mpi->flags|=MP_IMGFLAG_DIRECT;
|
|
|
|
}
|
|
|
|
|
2002-06-14 03:14:56 +02:00
|
|
|
static void put_image(struct vf_instance_s* vf, mp_image_t *mpi){
|
|
|
|
mp_image_t *dmpi;
|
|
|
|
|
2002-06-16 15:45:52 +02:00
|
|
|
if(!(mpi->flags&MP_IMGFLAG_DIRECT)){
|
|
|
|
// no DR, so get a new image! hope we'll get DR buffer:
|
|
|
|
vf->priv->dmpi=vf_get_image(vf->next,vf->priv->outfmt,
|
2002-06-14 03:14:56 +02:00
|
|
|
MP_IMGTYPE_TEMP, MP_IMGFLAG_ACCEPT_STRIDE,
|
2002-06-16 15:45:52 +02:00
|
|
|
mpi->w,mpi->h);
|
|
|
|
//printf("nodr\n");
|
|
|
|
}
|
|
|
|
//else printf("dr\n");
|
|
|
|
dmpi= vf->priv->dmpi;
|
2002-06-14 03:14:56 +02:00
|
|
|
|
|
|
|
noise(dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, &vf->priv->lumaParam);
|
|
|
|
noise(dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w/2, mpi->h/2, &vf->priv->chromaParam);
|
|
|
|
noise(dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w/2, mpi->h/2, &vf->priv->chromaParam);
|
|
|
|
|
|
|
|
dmpi->qscale=mpi->qscale;
|
|
|
|
dmpi->qstride=mpi->qstride;
|
|
|
|
|
|
|
|
#ifdef HAVE_MMX
|
|
|
|
if(gCpuCaps.hasMMX) asm volatile ("emms\n\t");
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_MMX2
|
|
|
|
if(gCpuCaps.hasMMX2) asm volatile ("sfence\n\t");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
vf_next_put_image(vf,dmpi);
|
|
|
|
}
|
|
|
|
|
2002-06-16 15:45:52 +02:00
|
|
|
static void uninit(struct vf_instance_s* vf){
|
|
|
|
if(!vf->priv) return;
|
|
|
|
|
|
|
|
if(vf->priv->chromaParam.noise) free(vf->priv->chromaParam.noise);
|
|
|
|
vf->priv->chromaParam.noise= NULL;
|
|
|
|
|
|
|
|
if(vf->priv->lumaParam.noise) free(vf->priv->lumaParam.noise);
|
|
|
|
vf->priv->lumaParam.noise= NULL;
|
|
|
|
|
|
|
|
free(vf->priv);
|
|
|
|
vf->priv=NULL;
|
|
|
|
}
|
|
|
|
|
2002-06-14 03:14:56 +02:00
|
|
|
//===========================================================================//
|
|
|
|
|
|
|
|
static int query_format(struct vf_instance_s* vf, unsigned int fmt){
|
|
|
|
switch(fmt)
|
|
|
|
{
|
|
|
|
case IMGFMT_YV12:
|
|
|
|
case IMGFMT_I420:
|
|
|
|
case IMGFMT_IYUV:
|
2002-06-16 15:45:52 +02:00
|
|
|
return vf_next_query_format(vf,vf->priv->outfmt);
|
2002-06-14 03:14:56 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse(FilterParam *fp, char* args){
|
|
|
|
char *pos;
|
|
|
|
char *max= strchr(args, ':');
|
|
|
|
|
|
|
|
if(!max) max= args + strlen(args);
|
|
|
|
|
|
|
|
fp->strength= atoi(args);
|
|
|
|
pos= strchr(args, 'u');
|
|
|
|
if(pos && pos<max) fp->uniform=1;
|
|
|
|
pos= strchr(args, 't');
|
|
|
|
if(pos && pos<max) fp->temporal=1;
|
2002-06-16 15:54:36 +02:00
|
|
|
pos= strchr(args, 'h');
|
|
|
|
if(pos && pos<max) fp->quality=1;
|
2002-08-11 11:46:50 +02:00
|
|
|
pos= strchr(args, 'a');
|
|
|
|
if(pos && pos<max) {
|
|
|
|
fp->temporal=1;
|
|
|
|
fp->averaged=1;
|
|
|
|
}
|
2002-06-14 03:14:56 +02:00
|
|
|
|
|
|
|
if(fp->strength) initNoise(fp);
|
|
|
|
}
|
|
|
|
|
2002-06-16 15:45:52 +02:00
|
|
|
static unsigned int fmt_list[]={
|
|
|
|
IMGFMT_YV12,
|
|
|
|
IMGFMT_I420,
|
|
|
|
IMGFMT_IYUV,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2002-06-14 03:14:56 +02:00
|
|
|
static int open(vf_instance_t *vf, char* args){
|
|
|
|
vf->config=config;
|
|
|
|
vf->put_image=put_image;
|
2002-06-16 15:45:52 +02:00
|
|
|
vf->get_image=get_image;
|
2002-06-14 03:14:56 +02:00
|
|
|
vf->query_format=query_format;
|
2002-06-16 15:45:52 +02:00
|
|
|
vf->uninit=uninit;
|
2002-06-14 03:14:56 +02:00
|
|
|
vf->priv=malloc(sizeof(struct vf_priv_s));
|
|
|
|
memset(vf->priv, 0, sizeof(struct vf_priv_s));
|
|
|
|
if(args)
|
|
|
|
{
|
|
|
|
char *arg2= strchr(args,':');
|
|
|
|
if(arg2) parse(&vf->priv->chromaParam, arg2+1);
|
|
|
|
parse(&vf->priv->lumaParam, args);
|
|
|
|
}
|
2002-06-16 15:45:52 +02:00
|
|
|
|
|
|
|
// check csp:
|
|
|
|
vf->priv->outfmt=vf_match_csp(&vf->next,fmt_list,IMGFMT_YV12);
|
|
|
|
if(!vf->priv->outfmt)
|
|
|
|
{
|
|
|
|
uninit(vf);
|
|
|
|
return 0; // no csp match :(
|
|
|
|
}
|
|
|
|
|
2002-06-14 03:14:56 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_MMX
|
|
|
|
if(gCpuCaps.hasMMX) lineNoise= lineNoise_MMX;
|
2002-08-11 11:46:50 +02:00
|
|
|
// if(gCpuCaps.hasMMX) lineNoiseAvg= lineNoiseAvg_MMX;
|
2002-06-14 03:14:56 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_MMX2
|
|
|
|
if(gCpuCaps.hasMMX2) lineNoise= lineNoise_MMX2;
|
2002-08-11 11:46:50 +02:00
|
|
|
// if(gCpuCaps.hasMMX) lineNoiseAvg= lineNoiseAvg_MMX2;
|
2002-06-14 03:14:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vf_info_t vf_info_noise = {
|
|
|
|
"noise genenerator",
|
|
|
|
"noise",
|
|
|
|
"Michael Niedermayer",
|
|
|
|
"",
|
|
|
|
open
|
|
|
|
};
|
|
|
|
|
|
|
|
//===========================================================================//
|