2002-10-29 22:37:11 +00:00
|
|
|
/*
|
2008-05-13 10:16:20 +00:00
|
|
|
* Copyright (C) 2002 Remi Guyomarch <rguyom@pobox.com>
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer 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.
|
|
|
|
*
|
|
|
|
* MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2002-10-29 22:37:11 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2005-11-18 14:39:25 +00:00
|
|
|
#include "config.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "core/mp_msg.h"
|
|
|
|
#include "core/cpudetect.h"
|
2002-10-29 22:37:11 +00:00
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "video/img_format.h"
|
|
|
|
#include "video/mp_image.h"
|
2002-10-29 22:37:11 +00:00
|
|
|
#include "vf.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "video/memcpy_pic.h"
|
2007-03-01 12:54:08 +00:00
|
|
|
#include "libavutil/common.h"
|
2002-10-29 22:37:11 +00:00
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
|
|
|
#define MIN_MATRIX_SIZE 3
|
|
|
|
#define MAX_MATRIX_SIZE 63
|
|
|
|
|
|
|
|
typedef struct FilterParam {
|
|
|
|
int msizeX, msizeY;
|
|
|
|
double amount;
|
|
|
|
uint32_t *SC[MAX_MATRIX_SIZE-1];
|
|
|
|
} FilterParam;
|
|
|
|
|
|
|
|
struct vf_priv_s {
|
|
|
|
FilterParam lumaParam;
|
|
|
|
FilterParam chromaParam;
|
|
|
|
unsigned int outfmt;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
|
|
|
/* This code is based on :
|
|
|
|
|
|
|
|
An Efficient algorithm for Gaussian blur using finite-state machines
|
|
|
|
Frederick M. Waltz and John W. V. Miller
|
|
|
|
|
|
|
|
SPIE Conf. on Machine Vision Systems for Inspection and Metrology VII
|
|
|
|
Originally published Boston, Nov 98
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void unsharp( uint8_t *dst, uint8_t *src, int dstStride, int srcStride, int width, int height, FilterParam *fp ) {
|
|
|
|
|
|
|
|
uint32_t **SC = fp->SC;
|
|
|
|
uint32_t SR[MAX_MATRIX_SIZE-1], Tmp1, Tmp2;
|
|
|
|
uint8_t* src2 = src; // avoid gcc warning
|
|
|
|
|
|
|
|
int32_t res;
|
|
|
|
int x, y, z;
|
|
|
|
int amount = fp->amount * 65536.0;
|
|
|
|
int stepsX = fp->msizeX/2;
|
|
|
|
int stepsY = fp->msizeY/2;
|
|
|
|
int scalebits = (stepsX+stepsY)*2;
|
2002-11-01 00:02:52 +00:00
|
|
|
int32_t halfscale = 1 << ((stepsX+stepsY)*2-1);
|
2002-10-29 22:37:11 +00:00
|
|
|
|
|
|
|
if( !fp->amount ) {
|
|
|
|
if( src == dst )
|
|
|
|
return;
|
2009-07-06 23:26:13 +00:00
|
|
|
if( dstStride == srcStride )
|
2012-11-09 00:31:49 +00:00
|
|
|
memcpy( dst, src, srcStride*height );
|
2002-10-29 22:37:11 +00:00
|
|
|
else
|
|
|
|
for( y=0; y<height; y++, dst+=dstStride, src+=srcStride )
|
2012-11-09 00:31:49 +00:00
|
|
|
memcpy( dst, src, width );
|
2002-10-29 22:37:11 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( y=0; y<2*stepsY; y++ )
|
|
|
|
memset( SC[y], 0, sizeof(SC[y][0]) * (width+2*stepsX) );
|
|
|
|
|
|
|
|
for( y=-stepsY; y<height+stepsY; y++ ) {
|
|
|
|
if( y < height ) src2 = src;
|
|
|
|
memset( SR, 0, sizeof(SR[0]) * (2*stepsX-1) );
|
|
|
|
for( x=-stepsX; x<width+stepsX; x++ ) {
|
|
|
|
Tmp1 = x<=0 ? src2[0] : x>=width ? src2[width-1] : src2[x];
|
|
|
|
for( z=0; z<stepsX*2; z+=2 ) {
|
|
|
|
Tmp2 = SR[z+0] + Tmp1; SR[z+0] = Tmp1;
|
|
|
|
Tmp1 = SR[z+1] + Tmp2; SR[z+1] = Tmp2;
|
|
|
|
}
|
|
|
|
for( z=0; z<stepsY*2; z+=2 ) {
|
|
|
|
Tmp2 = SC[z+0][x+stepsX] + Tmp1; SC[z+0][x+stepsX] = Tmp1;
|
|
|
|
Tmp1 = SC[z+1][x+stepsX] + Tmp2; SC[z+1][x+stepsX] = Tmp2;
|
|
|
|
}
|
|
|
|
if( x>=stepsX && y>=stepsY ) {
|
|
|
|
uint8_t* srx = src - stepsY*srcStride + x - stepsX;
|
|
|
|
uint8_t* dsx = dst - stepsY*dstStride + x - stepsX;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-10-29 22:37:11 +00:00
|
|
|
res = (int32_t)*srx + ( ( ( (int32_t)*srx - (int32_t)((Tmp1+halfscale) >> scalebits) ) * amount ) >> 16 );
|
|
|
|
*dsx = res>255 ? 255 : res<0 ? 0 : (uint8_t)res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( y >= 0 ) {
|
|
|
|
dst += dstStride;
|
|
|
|
src += srcStride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
2010-05-29 14:15:55 +00:00
|
|
|
static int config( struct vf_instance *vf,
|
2002-10-29 22:37:11 +00:00
|
|
|
int width, int height, int d_width, int d_height,
|
|
|
|
unsigned int flags, unsigned int outfmt ) {
|
|
|
|
|
|
|
|
int z, stepsX, stepsY;
|
|
|
|
FilterParam *fp;
|
2002-11-01 00:02:52 +00:00
|
|
|
char *effect;
|
2002-10-29 22:37:11 +00:00
|
|
|
|
|
|
|
// allocate buffers
|
|
|
|
|
|
|
|
fp = &vf->priv->lumaParam;
|
2002-11-01 00:02:52 +00:00
|
|
|
effect = fp->amount == 0 ? "don't touch" : fp->amount < 0 ? "blur" : "sharpen";
|
|
|
|
mp_msg( MSGT_VFILTER, MSGL_INFO, "unsharp: %dx%d:%0.2f (%s luma) \n", fp->msizeX, fp->msizeY, fp->amount, effect );
|
2002-10-29 22:37:11 +00:00
|
|
|
memset( fp->SC, 0, sizeof( fp->SC ) );
|
|
|
|
stepsX = fp->msizeX/2;
|
|
|
|
stepsY = fp->msizeY/2;
|
|
|
|
for( z=0; z<2*stepsY; z++ )
|
2010-04-20 04:33:00 +00:00
|
|
|
fp->SC[z] = av_malloc(sizeof(*(fp->SC[z])) * (width+2*stepsX));
|
2002-10-29 22:37:11 +00:00
|
|
|
|
|
|
|
fp = &vf->priv->chromaParam;
|
2002-11-01 00:02:52 +00:00
|
|
|
effect = fp->amount == 0 ? "don't touch" : fp->amount < 0 ? "blur" : "sharpen";
|
|
|
|
mp_msg( MSGT_VFILTER, MSGL_INFO, "unsharp: %dx%d:%0.2f (%s chroma)\n", fp->msizeX, fp->msizeY, fp->amount, effect );
|
2002-10-29 22:37:11 +00:00
|
|
|
memset( fp->SC, 0, sizeof( fp->SC ) );
|
|
|
|
stepsX = fp->msizeX/2;
|
|
|
|
stepsY = fp->msizeY/2;
|
|
|
|
for( z=0; z<2*stepsY; z++ )
|
2010-04-20 04:33:00 +00:00
|
|
|
fp->SC[z] = av_malloc(sizeof(*(fp->SC[z])) * (width+2*stepsX));
|
2002-10-29 22:37:11 +00:00
|
|
|
|
|
|
|
return vf_next_config( vf, width, height, d_width, d_height, flags, outfmt );
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
video/filter: change filter API, use refcounting, remove filter DR
Change the entire filter API to use reference counted images instead
of vf_get_image().
Remove filter "direct rendering". This was useful for vf_expand and (in
rare cases) vf_sub: DR allowed these filters to pass a cropped image to
the filters before them. Then, on filtering, the image was "uncropped",
so that black bars could be added around the image without copying. This
means that in some cases, vf_expand will be slower (-vf gradfun,expand
for example).
Note that another form of DR used for in-place filters has been replaced
by simpler logic. Instead of trying to do DR, filters can check if the
image is writeable (with mp_image_is_writeable()), and do true in-place
if that's the case. This affects filters like vf_gradfun and vf_sub.
Everything has to support strides now. If something doesn't, making a
copy of the image data is required.
2012-11-05 13:25:04 +00:00
|
|
|
static struct mp_image *filter(struct vf_instance *vf, struct mp_image *mpi)
|
|
|
|
{
|
|
|
|
struct mp_image *dmpi = mpi;
|
|
|
|
if (!mp_image_is_writeable(mpi)) {
|
|
|
|
dmpi = vf_alloc_out_image(vf);
|
|
|
|
mp_image_copy_attributes(dmpi, mpi);
|
2002-10-29 22:37:11 +00:00
|
|
|
}
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2002-10-29 22:37:11 +00:00
|
|
|
unsharp( dmpi->planes[0], mpi->planes[0], dmpi->stride[0], mpi->stride[0], mpi->w, mpi->h, &vf->priv->lumaParam );
|
|
|
|
unsharp( dmpi->planes[1], mpi->planes[1], dmpi->stride[1], mpi->stride[1], mpi->w/2, mpi->h/2, &vf->priv->chromaParam );
|
|
|
|
unsharp( dmpi->planes[2], mpi->planes[2], dmpi->stride[2], mpi->stride[2], mpi->w/2, mpi->h/2, &vf->priv->chromaParam );
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2009-01-16 09:21:21 +00:00
|
|
|
#if HAVE_MMX
|
2002-10-29 22:37:11 +00:00
|
|
|
if(gCpuCaps.hasMMX)
|
2008-10-16 18:59:27 +00:00
|
|
|
__asm__ volatile ("emms\n\t");
|
2002-10-29 22:37:11 +00:00
|
|
|
#endif
|
2009-01-16 09:21:21 +00:00
|
|
|
#if HAVE_MMX2
|
2002-10-29 22:37:11 +00:00
|
|
|
if(gCpuCaps.hasMMX2)
|
2008-10-16 18:59:27 +00:00
|
|
|
__asm__ volatile ("sfence\n\t");
|
2002-10-29 22:37:11 +00:00
|
|
|
#endif
|
2009-07-06 23:26:13 +00:00
|
|
|
|
video/filter: change filter API, use refcounting, remove filter DR
Change the entire filter API to use reference counted images instead
of vf_get_image().
Remove filter "direct rendering". This was useful for vf_expand and (in
rare cases) vf_sub: DR allowed these filters to pass a cropped image to
the filters before them. Then, on filtering, the image was "uncropped",
so that black bars could be added around the image without copying. This
means that in some cases, vf_expand will be slower (-vf gradfun,expand
for example).
Note that another form of DR used for in-place filters has been replaced
by simpler logic. Instead of trying to do DR, filters can check if the
image is writeable (with mp_image_is_writeable()), and do true in-place
if that's the case. This affects filters like vf_gradfun and vf_sub.
Everything has to support strides now. If something doesn't, making a
copy of the image data is required.
2012-11-05 13:25:04 +00:00
|
|
|
if (dmpi != mpi)
|
|
|
|
talloc_free(mpi);
|
|
|
|
return dmpi;
|
2002-10-29 22:37:11 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 14:15:55 +00:00
|
|
|
static void uninit( struct vf_instance *vf ) {
|
2002-10-29 22:37:11 +00:00
|
|
|
unsigned int z;
|
|
|
|
FilterParam *fp;
|
|
|
|
|
|
|
|
if( !vf->priv ) return;
|
|
|
|
|
|
|
|
fp = &vf->priv->lumaParam;
|
|
|
|
for( z=0; z<sizeof(fp->SC)/sizeof(fp->SC[0]); z++ ) {
|
2010-11-14 09:12:34 +00:00
|
|
|
av_free( fp->SC[z] );
|
2002-10-29 22:37:11 +00:00
|
|
|
fp->SC[z] = NULL;
|
|
|
|
}
|
|
|
|
fp = &vf->priv->chromaParam;
|
|
|
|
for( z=0; z<sizeof(fp->SC)/sizeof(fp->SC[0]); z++ ) {
|
2010-11-14 09:12:34 +00:00
|
|
|
av_free( fp->SC[z] );
|
2002-10-29 22:37:11 +00:00
|
|
|
fp->SC[z] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free( vf->priv );
|
|
|
|
vf->priv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
2010-05-29 14:15:55 +00:00
|
|
|
static int query_format( struct vf_instance *vf, unsigned int fmt ) {
|
2002-10-29 22:37:11 +00:00
|
|
|
switch(fmt) {
|
|
|
|
case IMGFMT_YV12:
|
|
|
|
case IMGFMT_I420:
|
|
|
|
case IMGFMT_IYUV:
|
|
|
|
return vf_next_query_format( vf, vf->priv->outfmt );
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
|
|
|
static void parse( FilterParam *fp, char* args ) {
|
|
|
|
|
|
|
|
// l7x5:0.8:c3x3:-0.2
|
|
|
|
|
|
|
|
char *z;
|
|
|
|
char *pos = args;
|
|
|
|
char *max = args + strlen(args);
|
|
|
|
|
|
|
|
// parse matrix sizes
|
|
|
|
fp->msizeX = ( pos && pos+1<max ) ? atoi( pos+1 ) : 0;
|
|
|
|
z = strchr( pos+1, 'x' );
|
|
|
|
fp->msizeY = ( z && z+1<max ) ? atoi( pos=z+1 ) : fp->msizeX;
|
|
|
|
|
|
|
|
// min/max & odd
|
2007-03-01 12:54:08 +00:00
|
|
|
fp->msizeX = 1 | av_clip(fp->msizeX, MIN_MATRIX_SIZE, MAX_MATRIX_SIZE);
|
|
|
|
fp->msizeY = 1 | av_clip(fp->msizeY, MIN_MATRIX_SIZE, MAX_MATRIX_SIZE);
|
2002-10-29 22:37:11 +00:00
|
|
|
|
|
|
|
// parse amount
|
|
|
|
pos = strchr( pos+1, ':' );
|
|
|
|
fp->amount = ( pos && pos+1<max ) ? atof( pos+1 ) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===========================================================================//
|
|
|
|
|
2008-04-26 13:35:40 +00:00
|
|
|
static const unsigned int fmt_list[] = {
|
2002-10-29 22:37:11 +00:00
|
|
|
IMGFMT_YV12,
|
|
|
|
IMGFMT_I420,
|
|
|
|
IMGFMT_IYUV,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2010-02-21 13:40:49 +00:00
|
|
|
static int vf_open( vf_instance_t *vf, char *args ) {
|
2002-10-29 22:37:11 +00:00
|
|
|
vf->config = config;
|
video/filter: change filter API, use refcounting, remove filter DR
Change the entire filter API to use reference counted images instead
of vf_get_image().
Remove filter "direct rendering". This was useful for vf_expand and (in
rare cases) vf_sub: DR allowed these filters to pass a cropped image to
the filters before them. Then, on filtering, the image was "uncropped",
so that black bars could be added around the image without copying. This
means that in some cases, vf_expand will be slower (-vf gradfun,expand
for example).
Note that another form of DR used for in-place filters has been replaced
by simpler logic. Instead of trying to do DR, filters can check if the
image is writeable (with mp_image_is_writeable()), and do true in-place
if that's the case. This affects filters like vf_gradfun and vf_sub.
Everything has to support strides now. If something doesn't, making a
copy of the image data is required.
2012-11-05 13:25:04 +00:00
|
|
|
vf->filter = filter;
|
2002-10-29 22:37:11 +00:00
|
|
|
vf->query_format = query_format;
|
|
|
|
vf->uninit = uninit;
|
|
|
|
vf->priv = malloc( sizeof(struct vf_priv_s) );
|
|
|
|
memset( vf->priv, 0, sizeof(struct vf_priv_s) );
|
|
|
|
|
|
|
|
if( args ) {
|
|
|
|
char *args2 = strchr( args, 'l' );
|
|
|
|
if( args2 )
|
|
|
|
parse( &vf->priv->lumaParam, args2 );
|
|
|
|
else {
|
2009-07-06 23:26:13 +00:00
|
|
|
vf->priv->lumaParam.amount =
|
|
|
|
vf->priv->lumaParam.msizeX =
|
2002-10-29 22:37:11 +00:00
|
|
|
vf->priv->lumaParam.msizeY = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
args2 = strchr( args, 'c' );
|
2009-07-06 23:26:13 +00:00
|
|
|
if( args2 )
|
2002-10-29 22:37:11 +00:00
|
|
|
parse( &vf->priv->chromaParam, args2 );
|
|
|
|
else {
|
2009-07-06 23:26:13 +00:00
|
|
|
vf->priv->chromaParam.amount =
|
|
|
|
vf->priv->chromaParam.msizeX =
|
2002-10-29 22:37:11 +00:00
|
|
|
vf->priv->chromaParam.msizeY = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !vf->priv->lumaParam.msizeX && !vf->priv->chromaParam.msizeX )
|
|
|
|
return 0; // nothing to do
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 :(
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-12-02 14:57:15 +00:00
|
|
|
const vf_info_t vf_info_unsharp = {
|
2002-11-01 00:02:52 +00:00
|
|
|
"unsharp mask & gaussian blur",
|
2002-10-29 22:37:11 +00:00
|
|
|
"unsharp",
|
2007-03-12 15:52:34 +00:00
|
|
|
"Remi Guyomarch",
|
2002-10-29 22:37:11 +00:00
|
|
|
"",
|
2010-02-21 13:40:49 +00:00
|
|
|
vf_open,
|
2003-03-15 18:01:02 +00:00
|
|
|
NULL
|
2002-10-29 22:37:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//===========================================================================//
|