]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/sharpen.c
LGPL
[vlc] / modules / video_filter / sharpen.c
index 1fbaea3e0f43d68e51b5762b8fc33a0b926e2e32..87146730f36bb62598969a239bb96a1f36f17d2e 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * sharpen.c: Sharpen video filter
  *****************************************************************************
- * Copyright (C) 2003-2007 the VideoLAN team
+ * Copyright (C) 2003-2007 VLC authors and VideoLAN
  * $Id$
  *
  * Author: Jérémy DEMEULE <dj_mulder at djduron dot no-ip dot org>
  *         Jean-Baptiste Kempf <jb at videolan dot org>
  *
- * 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
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /* The sharpen filter. */
@@ -40,7 +40,7 @@
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 
-#include "vlc_filter.h"
+#include <vlc_filter.h>
 #include "filter_picture.h"
 
 #define SIG_TEXT N_("Sharpen strength (0-2)")
@@ -56,18 +56,20 @@ static picture_t *Filter( filter_t *, picture_t * );
 static int SharpenCallback( vlc_object_t *, char const *,
                             vlc_value_t, vlc_value_t, void * );
 
+#define SHARPEN_HELP N_("Augment contrast between contours.")
 #define FILTER_PREFIX "sharpen-"
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
 vlc_module_begin ()
-    set_description( N_("Augment contrast between contours.") )
-    set_shortname( N_("Sharpen video filter") )
+    set_description( N_("Sharpen video filter") )
+    set_shortname( N_("Sharpen") )
+    set_help(SHARPEN_HELP)
     set_category( CAT_VIDEO )
     set_subcategory( SUBCAT_VIDEO_VFILTER )
     set_capability( "video filter2", 0 )
-    add_float_with_range( "sharpen-sigma", 0.05, 0.0, 2.0, NULL,
+    add_float_with_range( "sharpen-sigma", 0.05, 0.0, 2.0,
         SIG_TEXT, SIG_LONGTEXT, false )
     add_shortcut( "sharpen" )
     set_callbacks( Create, Destroy )
@@ -86,7 +88,7 @@ static const char *const ppsz_filter_options[] = {
 
 struct filter_sys_t
 {
-    float f_sigma;
+    vlc_mutex_t lock;
     int tab_precalc[512];
 };
 
@@ -98,10 +100,8 @@ inline static int32_t clip( int32_t a )
     return (a > 255) ? 255 : (a < 0) ? 0 : a;
 }
 
-static void init_precalc_table(filter_sys_t *p_filter)
+static void init_precalc_table(filter_sys_t *p_filter, float sigma)
 {
-    float sigma = p_filter->f_sigma;
-
     for(int i = 0; i < 512; ++i)
     {
         p_filter->tab_precalc[i] = (i - 256) * sigma;
@@ -117,6 +117,13 @@ static int Create( vlc_object_t *p_this )
 {
     filter_t *p_filter = (filter_t *)p_this;
 
+    const vlc_fourcc_t fourcc = p_filter->fmt_in.video.i_chroma;
+    const vlc_chroma_description_t *p_chroma = vlc_fourcc_GetChromaDescription( fourcc );
+    if( !p_chroma || p_chroma->plane_count != 3 || p_chroma->pixel_size != 1 ) {
+        msg_Err( p_filter, "Unsupported chroma (%4.4s)", (char*)&fourcc );
+        return VLC_EGENERIC;
+    }
+
     /* Allocate structure */
     p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
     if( p_filter->p_sys == NULL )
@@ -127,13 +134,13 @@ static int Create( vlc_object_t *p_this )
     config_ChainParse( p_filter, FILTER_PREFIX, ppsz_filter_options,
                    p_filter->p_cfg );
 
-    p_filter->p_sys->f_sigma =
-        var_CreateGetFloatCommand( p_filter, FILTER_PREFIX "sigma" );
+    float sigma = var_CreateGetFloatCommand( p_filter, FILTER_PREFIX "sigma" );
+    init_precalc_table(p_filter->p_sys, sigma);
+
+    vlc_mutex_init( &p_filter->p_sys->lock );
     var_AddCallback( p_filter, FILTER_PREFIX "sigma",
                      SharpenCallback, p_filter->p_sys );
 
-    init_precalc_table(p_filter->p_sys);
-
     return VLC_SUCCESS;
 }
 
@@ -146,9 +153,11 @@ static int Create( vlc_object_t *p_this )
 static void Destroy( vlc_object_t *p_this )
 {
     filter_t *p_filter = (filter_t *)p_this;
-    var_DelCallback( p_filter, FILTER_PREFIX "sigma",
-                     SharpenCallback, p_filter->p_sys );
-    free( p_filter->p_sys );
+    filter_sys_t *p_sys = p_filter->p_sys;
+
+    var_DelCallback( p_filter, FILTER_PREFIX "sigma", SharpenCallback, p_sys );
+    vlc_mutex_destroy( &p_sys->lock );
+    free( p_sys );
 }
 
 /*****************************************************************************
@@ -165,6 +174,7 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
     uint8_t *p_src = NULL;
     uint8_t *p_out = NULL;
     int i_src_pitch;
+    int i_out_pitch;
     int pix;
     const int v1 = -1;
     const int v2 = 3; /* 2^3 = 8 */
@@ -181,22 +191,24 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
     /* process the Y plane */
     p_src = p_pic->p[Y_PLANE].p_pixels;
     p_out = p_outpic->p[Y_PLANE].p_pixels;
-    i_src_pitch = p_pic->p[Y_PLANE].i_visible_pitch;
+    i_src_pitch = p_pic->p[Y_PLANE].i_pitch;
+    i_out_pitch = p_outpic->p[Y_PLANE].i_pitch;
 
     /* perform convolution only on Y plane. Avoid border line. */
+    vlc_mutex_lock( &p_filter->p_sys->lock );
     for( i = 0; i < p_pic->p[Y_PLANE].i_visible_lines; i++ )
     {
         if( (i == 0) || (i == p_pic->p[Y_PLANE].i_visible_lines - 1) )
         {
             for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
-                p_out[i * i_src_pitch + j] = clip( p_src[i * i_src_pitch + j] );
+                p_out[i * i_out_pitch + j] = clip( p_src[i * i_src_pitch + j] );
             continue ;
         }
         for( j = 0; j < p_pic->p[Y_PLANE].i_visible_pitch; j++ )
         {
             if( (j == 0) || (j == p_pic->p[Y_PLANE].i_visible_pitch - 1) )
             {
-                p_out[i * i_src_pitch + j] = p_src[i * i_src_pitch + j];
+                p_out[i * i_out_pitch + j] = p_src[i * i_src_pitch + j];
                 continue ;
             }
 
@@ -210,19 +222,15 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
                   (p_src[(i + 1) * i_src_pitch + j    ] * v1) +
                   (p_src[(i + 1) * i_src_pitch + j + 1] * v1);
 
-        pix = pix >= 0 ? clip(pix) : -clip(pix * -1);
-        p_out[i * i_src_pitch + j] = clip( p_src[i * i_src_pitch + j] +
-            p_filter->p_sys->tab_precalc[pix + 256] );
+           pix = pix >= 0 ? clip(pix) : -clip(pix * -1);
+           p_out[i * i_out_pitch + j] = clip( p_src[i * i_src_pitch + j] +
+               p_filter->p_sys->tab_precalc[pix + 256] );
         }
     }
+    vlc_mutex_unlock( &p_filter->p_sys->lock );
 
-
-    vlc_memcpy( p_outpic->p[U_PLANE].p_pixels, p_pic->p[U_PLANE].p_pixels,
-                p_outpic->p[U_PLANE].i_lines * p_outpic->p[U_PLANE].i_pitch );
-
-    vlc_memcpy( p_outpic->p[V_PLANE].p_pixels, p_pic->p[V_PLANE].p_pixels,
-                p_outpic->p[V_PLANE].i_lines * p_outpic->p[V_PLANE].i_pitch );
-
+    plane_CopyPixels( &p_outpic->p[U_PLANE], &p_pic->p[U_PLANE] );
+    plane_CopyPixels( &p_outpic->p[V_PLANE], &p_pic->p[V_PLANE] );
 
     return CopyInfoAndRelease( p_outpic, p_pic );
 }
@@ -231,10 +239,11 @@ static int SharpenCallback( vlc_object_t *p_this, char const *psz_var,
                             vlc_value_t oldval, vlc_value_t newval,
                             void *p_data )
 {
-    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
+    VLC_UNUSED(p_this); VLC_UNUSED(oldval); VLC_UNUSED(psz_var);
     filter_sys_t *p_sys = (filter_sys_t *)p_data;
-    if( !strcmp( psz_var, FILTER_PREFIX "sigma" ) )
-        p_sys->f_sigma = __MIN( 2., __MAX( 0., newval.f_float ) );
-    init_precalc_table(p_sys);
+
+    vlc_mutex_lock( &p_sys->lock );
+    init_precalc_table( p_sys,  VLC_CLIP( newval.f_float, 0., 2. ) );
+    vlc_mutex_unlock( &p_sys->lock );
     return VLC_SUCCESS;
 }