]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/colorthres.c
postproc: reduce lock contention in callback
[vlc] / modules / video_filter / colorthres.c
index c49ea7dc2bf44800ed1d91b334a37e08c1ebbaf2..3778639e32f59fae27ab4516d7a8d35d347a7c42 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * colorthres.c: Threshold color based on similarity to reference color
  *****************************************************************************
- * Copyright (C) 2000-2009 the VideoLAN team
+ * Copyright (C) 2000-2009 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Sigmund Augdal <dnumgis@videolan.org>
  *          Antoine Cellerier <dionoea 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.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -35,7 +35,7 @@
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_sout.h>
-
+#include <vlc_atomic.h>
 #include <vlc_filter.h>
 #include "filter_picture.h"
 
@@ -72,12 +72,12 @@ vlc_module_begin ()
     set_category( CAT_VIDEO )
     set_subcategory( SUBCAT_VIDEO_VFILTER )
     set_capability( "video filter2", 0 )
-    add_integer( CFG_PREFIX "color", 0x00FF0000, NULL, COLOR_TEXT,
+    add_rgb( CFG_PREFIX "color", 0x00FF0000, COLOR_TEXT,
                  COLOR_LONGTEXT, false )
-        change_integer_list( pi_color_values, ppsz_color_descriptions, NULL )
-    add_integer( CFG_PREFIX "saturationthres", 20, NULL,
-                 N_("Saturaton threshold"), "", false )
-    add_integer( CFG_PREFIX "similaritythres", 15, NULL,
+        change_integer_list( pi_color_values, ppsz_color_descriptions )
+    add_integer( CFG_PREFIX "saturationthres", 20,
+                 N_("Saturation threshold"), "", false )
+    add_integer( CFG_PREFIX "similaritythres", 15,
                  N_("Similarity threshold"), "", false )
     set_callbacks( Create, Destroy )
 vlc_module_end ()
@@ -98,10 +98,9 @@ static int FilterCallback( vlc_object_t *, char const *,
  *****************************************************************************/
 struct filter_sys_t
 {
-    int i_simthres;
-    int i_satthres;
-    int i_color;
-    vlc_mutex_t lock;
+    atomic_int i_simthres;
+    atomic_int i_satthres;
+    atomic_int i_color;
 };
 
 /*****************************************************************************
@@ -143,17 +142,16 @@ static int Create( vlc_object_t *p_this )
 
     config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
                        p_filter->p_cfg );
-    p_sys->i_color = var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "color" );
-    p_sys->i_simthres = var_CreateGetIntegerCommand( p_filter,
-                                                     CFG_PREFIX "similaritythres" );
-    p_sys->i_satthres = var_CreateGetIntegerCommand( p_filter,
-                                                     CFG_PREFIX "saturationthres" );
-
-    vlc_mutex_init( &p_sys->lock );
+    atomic_init( &p_sys->i_color,
+                 var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "color" ) );
+    atomic_init( &p_sys->i_simthres,
+       var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "similaritythres" ) );
+    atomic_init( &p_sys->i_satthres,
+       var_CreateGetIntegerCommand( p_filter, CFG_PREFIX "saturationthres" ) );
 
-    var_AddCallback( p_filter, CFG_PREFIX "color", FilterCallback, NULL );
-    var_AddCallback( p_filter, CFG_PREFIX "similaritythres", FilterCallback, NULL );
-    var_AddCallback( p_filter, CFG_PREFIX "saturationthres", FilterCallback, NULL );
+    var_AddCallback( p_filter, CFG_PREFIX "color", FilterCallback, p_sys );
+    var_AddCallback( p_filter, CFG_PREFIX "similaritythres", FilterCallback, p_sys );
+    var_AddCallback( p_filter, CFG_PREFIX "saturationthres", FilterCallback, p_sys );
 
     return VLC_SUCCESS;
 }
@@ -166,15 +164,40 @@ static int Create( vlc_object_t *p_this )
 static void Destroy( vlc_object_t *p_this )
 {
     filter_t *p_filter = (filter_t *)p_this;
+    filter_sys_t *p_sys = p_filter->p_sys;
 
-    var_DelCallback( p_filter, CFG_PREFIX "color", FilterCallback, NULL );
-    var_DelCallback( p_filter, CFG_PREFIX "similaritythres", FilterCallback, NULL );
-    var_DelCallback( p_filter, CFG_PREFIX "saturationthres", FilterCallback, NULL );
+    var_DelCallback( p_filter, CFG_PREFIX "color", FilterCallback, p_sys );
+    var_DelCallback( p_filter, CFG_PREFIX "similaritythres", FilterCallback, p_sys );
+    var_DelCallback( p_filter, CFG_PREFIX "saturationthres", FilterCallback, p_sys );
+    free( p_sys );
+}
 
-    vlc_mutex_destroy( &p_filter->p_sys->lock );
-    free( p_filter->p_sys );
+static void GetReference( int *refu, int *refv, int *reflength,
+                          uint32_t i_color )
+{
+    int i_red   = ( i_color & 0xFF0000 ) >> 16;
+    int i_green = ( i_color & 0x00FF00 ) >> 8;
+    int i_blue  = ( i_color & 0x0000FF );
+    int i_u = (int8_t)(( -38 * i_red - 74 * i_green + 112 * i_blue + 128) >> 8) + 128;
+    int i_v = (int8_t)(( 112 * i_red - 94 * i_green -  18 * i_blue + 128) >> 8) + 128;
+    *refu = i_u - 0x80;
+    *refv = i_v - 0x80;
+    *reflength = sqrt(*refu * *refu + *refv * *refv);
 }
 
+static bool IsSimilar( int u, int v,
+                       int refu, int refv, int reflength,
+                       int i_satthres, int i_simthres )
+{
+    int length = sqrt(u * u + v * v);
+
+    int diffu = refu * length - u * reflength;
+    int diffv = refv * length - v * reflength;
+    int64_t difflen2 = diffu * diffu + diffv * diffv;
+    int64_t thres = length * reflength;
+    thres *= thres;
+    return length > i_satthres && (difflen2 * i_simthres < thres);
+}
 /*****************************************************************************
  * Render: displays previously rendered output
  *****************************************************************************
@@ -186,12 +209,9 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
 {
     picture_t *p_outpic;
     filter_sys_t *p_sys = p_filter->p_sys;
-
-    vlc_mutex_lock( &p_sys->lock );
-    int i_simthres = p_sys->i_simthres;
-    int i_satthres = p_sys->i_satthres;
-    int i_color = p_sys->i_color;
-    vlc_mutex_unlock( &p_sys->lock );
+    int i_simthres = atomic_load( &p_sys->i_simthres );
+    int i_satthres = atomic_load( &p_sys->i_satthres );
+    int i_color = atomic_load( &p_sys->i_color );
 
     if( !p_pic ) return NULL;
 
@@ -208,17 +228,8 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
     /*
      * Do the U and V planes
      */
-    int i_red = ( i_color & 0xFF0000 ) >> 16;
-    int i_green = ( i_color & 0xFF00 ) >> 8;
-    int i_blue = i_color & 0xFF;
-    int i_u = (int8_t)(( -38 * i_red - 74 * i_green +
-                     112 * i_blue + 128) >> 8) + 128;
-    int i_v = (int8_t)(( 112 * i_red  -  94 * i_green -
-                      18 * i_blue + 128) >> 8) + 128;
-
-    int refu = i_u - 0x80;         /*bright red*/
-    int refv = i_v - 0x80;
-    int reflength = sqrt(refu*refu+refv*refv);
+    int refu, refv, reflength;
+    GetReference( &refu, &refv, &reflength, i_color );
 
     for( int y = 0; y < p_pic->p[U_PLANE].i_visible_lines; y++ )
     {
@@ -229,18 +240,10 @@ static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
 
         for( int x = 0; x < p_pic->p[U_PLANE].i_visible_pitch; x++ )
         {
-            /* Length of color vector */
-            int inu = *p_src_u - 0x80;
-            int inv = *p_src_v - 0x80;
-            int length = sqrt(inu*inu+inv*inv);
-
-            int diffu = refu * length - inu *reflength;
-            int diffv = refv * length - inv *reflength;
-            long long int difflen2=diffu*diffu;
-            difflen2 +=diffv*diffv;
-            long long int thres = length*reflength;
-            thres *= thres;
-            if( length > i_satthres && (difflen2*i_simthres< thres ) )
+            if( IsSimilar( *p_src_u - 0x80, *p_src_v - 0x80,
+                           refu, refv, reflength,
+                           i_satthres, i_simthres ) )
+
             {
                 *p_dst_u++ = *p_src_u;
                 *p_dst_v++ = *p_src_v;
@@ -262,12 +265,9 @@ static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
 {
     picture_t *p_outpic;
     filter_sys_t *p_sys = p_filter->p_sys;
-
-    vlc_mutex_lock( &p_sys->lock );
-    int i_simthres = p_sys->i_simthres;
-    int i_satthres = p_sys->i_satthres;
-    int i_color = p_sys->i_color;
-    vlc_mutex_unlock( &p_sys->lock );
+    int i_simthres = atomic_load( &p_sys->i_simthres );
+    int i_satthres = atomic_load( &p_sys->i_satthres );
+    int i_color = atomic_load( &p_sys->i_color );
 
     if( !p_pic ) return NULL;
 
@@ -279,22 +279,19 @@ static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
     }
 
     int i_y_offset, i_u_offset, i_v_offset;
-    GetPackedYuvOffsets( p_filter->fmt_in.video.i_chroma,
-                         &i_y_offset, &i_u_offset, &i_v_offset );
+    int i_ret = GetPackedYuvOffsets( p_filter->fmt_in.video.i_chroma,
+                                     &i_y_offset, &i_u_offset, &i_v_offset );
+    if( i_ret == VLC_EGENERIC )
+    {
+        picture_Release( p_pic );
+        return NULL;
+    }
 
     /*
      * Copy Y and do the U and V planes
      */
-    int i_red = ( i_color & 0xFF0000 ) >> 16;
-    int i_green = ( i_color & 0xFF00 ) >> 8;
-    int i_blue = i_color & 0xFF;
-    int i_u = (int8_t)(( -38 * i_red - 74 * i_green +
-                     112 * i_blue + 128) >> 8) + 128;
-    int i_v = (int8_t)(( 112 * i_red  -  94 * i_green -
-                      18 * i_blue + 128) >> 8) + 128;
-    int refu = i_u - 0x80;         /*bright red*/
-    int refv = i_v - 0x80;
-    int reflength = sqrt(refu*refu+refv*refv);
+    int refu, refv, reflength;
+    GetReference( &refu, &refv, &reflength, i_color );
 
     for( int y = 0; y < p_pic->p->i_visible_lines; y++ )
     {
@@ -306,18 +303,9 @@ static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
             p_dst[i_y_offset + 0] = p_src[i_y_offset + 0];
             p_dst[i_y_offset + 2] = p_src[i_y_offset + 2];
 
-            /* Length of color vector */
-            int inu = p_src[i_u_offset] - 0x80;
-            int inv = p_src[i_v_offset] - 0x80;
-            int length = sqrt(inu*inu+inv*inv);
-
-            int diffu = refu * length - inu *reflength;
-            int diffv = refv * length - inv *reflength;
-            long long int difflen2=diffu*diffu;
-            difflen2 +=diffv*diffv;
-            long long int thres = length*reflength;
-            thres *= thres;
-            if( length > i_satthres && (difflen2*i_simthres< thres ) )
+            if( IsSimilar( p_src[i_u_offset] - 0x80, p_src[i_v_offset] - 0x80,
+                           refu, refv, reflength,
+                           i_satthres, i_simthres ) )
             {
                 p_dst[i_u_offset] = p_src[i_u_offset];
                 p_dst[i_v_offset] = p_src[i_v_offset];
@@ -337,30 +325,17 @@ static picture_t *FilterPacked( filter_t *p_filter, picture_t *p_pic )
 }
 
 static int FilterCallback ( vlc_object_t *p_this, char const *psz_var,
-                            vlc_value_t oldval, vlc_value_t newval, void *p_data )
+                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
-    (void)oldval;    (void)p_data;
-    filter_t *p_filter = (filter_t*)p_this;
-    filter_sys_t *p_sys = p_filter->p_sys;
+    filter_sys_t *p_sys = p_data;
 
     if( !strcmp( psz_var, CFG_PREFIX "color" ) )
-    {
-        vlc_mutex_lock( &p_sys->lock );
-        p_sys->i_color = newval.i_int;
-        vlc_mutex_unlock( &p_sys->lock );
-    }
+        atomic_store( &p_sys->i_color, newval.i_int );
     else if( !strcmp( psz_var, CFG_PREFIX "similaritythres" ) )
-    {
-        vlc_mutex_lock( &p_sys->lock );
-        p_sys->i_simthres = newval.i_int;
-        vlc_mutex_unlock( &p_sys->lock );
-    }
+        atomic_store( &p_sys->i_simthres, newval.i_int );
     else /* CFG_PREFIX "saturationthres" */
-    {
-        vlc_mutex_lock( &p_sys->lock );
-        p_sys->i_satthres = newval.i_int;
-        vlc_mutex_unlock( &p_sys->lock );
-    }
+        atomic_store( &p_sys->i_satthres, newval.i_int );
 
+    (void)p_this; (void)oldval;
     return VLC_SUCCESS;
 }