]> git.sesse.net Git - vlc/blobdiff - modules/video_filter/crop.c
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / video_filter / crop.c
index 2584780d98bb8527f9aacf540721a4ed5b852833..497a46b0a3029eceeed8b748cdd03214024ff425 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>
 
-#include <vlc/vlc.h>
-#include <vlc_vout.h>
-#include <vlc_interface.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
-#include "filter_common.h"
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_vout.h>
+#include <vlc_dialog.h>
 
 #define BEST_AUTOCROP 1
 #ifdef BEST_AUTOCROP
-    #define RATIO_MAX 15000  // 10*4/3 for a 360°
+    #define RATIO_MAX 15000  // 10*4/3 for a 360
 #endif
 
 /*****************************************************************************
@@ -53,7 +54,7 @@ static void Render    ( vout_thread_t *, picture_t * );
 
 static void UpdateStats    ( vout_thread_t *, picture_t * );
 
-static int  SendEvents( vlc_object_t *, char const *,
+static int  MouseEvent( vlc_object_t *, char const *,
                         vlc_value_t, vlc_value_t, void * );
 
 #ifdef BEST_AUTOCROP
@@ -73,6 +74,8 @@ static int FilterCallback ( vlc_object_t *, char const *,
 #define AUTOCROP_TEXT N_("Automatic cropping")
 #define AUTOCROP_LONGTEXT N_("Automatically detect black borders and crop them.")
 
+#define CROP_HELP N_("Remove borders of the video and replace them by black borders")
+
 #ifdef BEST_AUTOCROP
 #define RATIOMAX_TEXT N_("Ratio max (x 1000)")
 #define RATIOMAX_LONGTEXT N_("Maximum image ratio. The crop plugin will never automatically crop to a higher ratio (ie, to a more \"flat\" image). The value is x1000: 1333 means 4/3.")
@@ -97,42 +100,43 @@ static int FilterCallback ( vlc_object_t *, char const *,
 #define LUM_LONGTEXT N_("Maximum luminance to consider a pixel as black (0-255).")
 #endif
 
-vlc_module_begin();
-    set_description( _("Crop video filter") );
-    set_shortname( _("Crop" ));
-    set_category( CAT_VIDEO );
-    set_subcategory( SUBCAT_VIDEO_VFILTER );
-    set_capability( "video filter", 0 );
+vlc_module_begin ()
+    set_description( N_("Crop video filter") )
+    set_shortname( N_("Crop" ))
+    set_help(CROP_HELP)
+    set_category( CAT_VIDEO )
+    set_subcategory( SUBCAT_VIDEO_VFILTER )
+    set_capability( "video filter", 0 )
 
     add_string( "crop-geometry", NULL, NULL, GEOMETRY_TEXT,
-                                             GEOMETRY_LONGTEXT, VLC_FALSE );
-    add_bool( "autocrop", 0, NULL, AUTOCROP_TEXT,
-                                   AUTOCROP_LONGTEXT, VLC_FALSE );
+                                             GEOMETRY_LONGTEXT, false )
+    add_bool( "autocrop", false, NULL, AUTOCROP_TEXT,
+                                   AUTOCROP_LONGTEXT, false )
 
 #ifdef BEST_AUTOCROP
     add_integer_with_range( "autocrop-ratio-max", 2405, 0, RATIO_MAX, NULL,
-                            RATIOMAX_TEXT, RATIOMAX_LONGTEXT, VLC_TRUE );
+                            RATIOMAX_TEXT, RATIOMAX_LONGTEXT, true )
 
     add_integer_with_range( "crop-ratio", 0, 0, RATIO_MAX, NULL, RATIO_TEXT,
-                            RATIO_LONGTEXT, VLC_FALSE );
+                            RATIO_LONGTEXT, false )
     add_integer( "autocrop-time", 25, NULL, TIME_TEXT,
-                 TIME_LONGTEXT, VLC_TRUE );
+                 TIME_LONGTEXT, true )
     add_integer( "autocrop-diff", 16, NULL, DIFF_TEXT,
-                                            DIFF_LONGTEXT, VLC_TRUE );
+                                            DIFF_LONGTEXT, true )
 
     add_integer( "autocrop-non-black-pixels", 3, NULL,
-                 NBP_TEXT, NBP_LONGTEXT, VLC_TRUE );
+                 NBP_TEXT, NBP_LONGTEXT, true )
 
     add_integer_with_range( "autocrop-skip-percent", 17, 0, 100, NULL,
-                            SKIP_TEXT, SKIP_LONGTEXT, VLC_TRUE );
+                            SKIP_TEXT, SKIP_LONGTEXT, true )
 
     add_integer_with_range( "autocrop-luminance-threshold", 40, 0, 128, NULL,
-                            LUM_TEXT, LUM_LONGTEXT, VLC_TRUE );
+                            LUM_TEXT, LUM_LONGTEXT, true )
 #endif //BEST_AUTOCROP
 
-    add_shortcut( "crop" );
-    set_callbacks( Create, Destroy );
-vlc_module_end();
+    add_shortcut( "crop" )
+    set_callbacks( Create, Destroy )
+vlc_module_end ()
 
 /*****************************************************************************
  * vout_sys_t: Crop video output method descriptor
@@ -142,16 +146,17 @@ vlc_module_end();
  *****************************************************************************/
 struct vout_sys_t
 {
+    vlc_mutex_t lock;
     vout_thread_t *p_vout;
 
     unsigned int i_x, i_y;
     unsigned int i_width, i_height, i_aspect;
 
-    vlc_bool_t b_autocrop;
+    bool b_autocrop;
 
     /* Autocrop specific variables */
     unsigned int i_lastchange;
-    vlc_bool_t   b_changed;
+    bool   b_changed;
 #ifdef BEST_AUTOCROP
     unsigned int i_ratio_max;
     unsigned int i_threshold, i_skipPercent, i_nonBlackPixel, i_diff, i_time;
@@ -180,10 +185,7 @@ static int Create( vlc_object_t *p_this )
     /* Allocate structure */
     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
     if( p_vout->p_sys == NULL )
-    {
-        msg_Err( p_vout, "out of memory" );
         return VLC_ENOMEM;
-    }
 
     p_vout->pf_init = Init;
     p_vout->pf_end = End;
@@ -200,15 +202,14 @@ static int Create( vlc_object_t *p_this )
  *****************************************************************************/
 static int Init( vout_thread_t *p_vout )
 {
-    int   i_index;
     char *psz_var;
-    picture_t *p_pic;
-    video_format_t fmt = {0};
+    video_format_t fmt;
 
     I_OUTPUTPICTURES = 0;
+    memset( &fmt, 0, sizeof(video_format_t) );
 
     p_vout->p_sys->i_lastchange = 0;
-    p_vout->p_sys->b_changed = VLC_FALSE;
+    p_vout->p_sys->b_changed = false;
 
     /* Initialize the output structure */
     p_vout->output.i_chroma = p_vout->render.i_chroma;
@@ -218,27 +219,27 @@ static int Init( vout_thread_t *p_vout )
     p_vout->fmt_out = p_vout->fmt_in;
 
     /* Shall we use autocrop ? */
-    p_vout->p_sys->b_autocrop = config_GetInt( p_vout, "autocrop" );
+    p_vout->p_sys->b_autocrop = var_InheritBool( p_vout, "autocrop" );
 #ifdef BEST_AUTOCROP
-    p_vout->p_sys->i_ratio_max = config_GetInt( p_vout, "autocrop-ratio-max" );
+    p_vout->p_sys->i_ratio_max =
+        var_InheritInteger( p_vout, "autocrop-ratio-max" );
     p_vout->p_sys->i_threshold =
-                    config_GetInt( p_vout, "autocrop-luminance-threshold" );
+        var_InheritInteger( p_vout, "autocrop-luminance-threshold" );
     p_vout->p_sys->i_skipPercent =
-                    config_GetInt( p_vout, "autocrop-skip-percent" );
+        var_InheritInteger( p_vout, "autocrop-skip-percent" );
     p_vout->p_sys->i_nonBlackPixel =
-                    config_GetInt( p_vout, "autocrop-non-black-pixels" );
-    p_vout->p_sys->i_diff = config_GetInt( p_vout, "autocrop-diff" );
-    p_vout->p_sys->i_time = config_GetInt( p_vout, "autocrop-time" );
-    vlc_value_t val={0};
-    var_Get( p_vout, "ratio-crop", &val );
-    val.psz_string = "0";
-    var_SetString( p_vout, "ratio-crop", val.psz_string);
+        var_InheritInteger( p_vout, "autocrop-non-black-pixels" );
+    p_vout->p_sys->i_diff =
+        var_InheritInteger( p_vout, "autocrop-diff" );
+    p_vout->p_sys->i_time =
+        var_InheritInteger( p_vout, "autocrop-time" );
+    var_SetString( p_vout, "ratio-crop", "0" );
 
     if (p_vout->p_sys->b_autocrop)
         p_vout->p_sys->i_ratio = 0;
     else
     {
-        p_vout->p_sys->i_ratio = config_GetInt( p_vout, "ratio" );
+        p_vout->p_sys->i_ratio = var_InheritInteger( p_vout, "crop-ratio" );
         // ratio < width / height => ratio = 0 (unchange ratio)
         if (p_vout->p_sys->i_ratio < (p_vout->output.i_width * 1000) / p_vout->output.i_height)
             p_vout->p_sys->i_ratio = 0;
@@ -247,7 +248,7 @@ static int Init( vout_thread_t *p_vout )
 
 
     /* Get geometry value from the user */
-    psz_var = config_GetPsz( p_vout, "crop-geometry" );
+    psz_var = var_InheritString( p_vout, "crop-geometry" );
     if( psz_var )
     {
         char *psz_parser, *psz_tmp;
@@ -353,42 +354,39 @@ static int Init( vout_thread_t *p_vout )
                      p_vout->p_sys->i_width, p_vout->p_sys->i_height,
                      p_vout->p_sys->i_x, p_vout->p_sys->i_y,
                      p_vout->p_sys->b_autocrop ? "" : "not " );
+    /* Set current output image properties */
+    p_vout->p_sys->i_aspect = (int64_t)VOUT_ASPECT_FACTOR *
+        p_vout->fmt_out.i_sar_num * p_vout->p_sys->i_width /
+        (p_vout->fmt_out.i_sar_den * p_vout->p_sys->i_height);
+
 #ifdef BEST_AUTOCROP
     msg_Info( p_vout, "ratio %d",  p_vout->p_sys->i_aspect / 432);
 #endif
-
-    /* Set current output image properties */
-    p_vout->p_sys->i_aspect = p_vout->fmt_out.i_aspect
-           * p_vout->fmt_out.i_visible_height / p_vout->p_sys->i_height
-           * p_vout->p_sys->i_width / p_vout->fmt_out.i_visible_width;
-
     fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
     fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
     fmt.i_x_offset = fmt.i_y_offset = 0;
     fmt.i_chroma = p_vout->render.i_chroma;
-    fmt.i_aspect = p_vout->p_sys->i_aspect;
-    fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
-    fmt.i_sar_den = VOUT_ASPECT_FACTOR;
+    fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height;
+    fmt.i_sar_den = VOUT_ASPECT_FACTOR * fmt.i_width;
 
     /* Try to open the real video output */
     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
     if( p_vout->p_sys->p_vout == NULL )
     {
         msg_Err( p_vout, "failed to create vout" );
-        intf_UserFatal( p_vout, VLC_FALSE, _("Cropping failed"),
+        dialog_Fatal( p_vout, _("Cropping failed"), "%s",
                         _("VLC could not open the video output module.") );
         return VLC_EGENERIC;
     }
 
-    ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
-
-    ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
-
+    vlc_mutex_init( &p_vout->p_sys->lock );
 #ifdef BEST_AUTOCROP
     var_AddCallback( p_vout, "ratio-crop", FilterCallback, NULL );
 #endif
 
-    ADD_PARENT_CALLBACKS( SendEventsToChild );
+    vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
+
+    vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
 
     return VLC_SUCCESS;
 }
@@ -398,14 +396,17 @@ static int Init( vout_thread_t *p_vout )
  *****************************************************************************/
 static void End( vout_thread_t *p_vout )
 {
-    int i_index;
+    vout_sys_t *p_sys = p_vout->p_sys;
 
-    /* Free the fake output buffers we allocated */
-    for( i_index = I_OUTPUTPICTURES ; i_index ; )
+    if( p_sys->p_vout )
     {
-        i_index--;
-        free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
+        vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
+        vout_CloseAndRelease( p_sys->p_vout );
     }
+
+    vout_filter_ReleaseDirectBuffers( p_vout );
+    var_DelCallback( p_vout, "ratio-crop", FilterCallback, NULL );
+    vlc_mutex_destroy( &p_sys->lock );
 }
 
 /*****************************************************************************
@@ -417,15 +418,6 @@ static void Destroy( vlc_object_t *p_this )
 {
     vout_thread_t *p_vout = (vout_thread_t *)p_this;
 
-    if( p_vout->p_sys->p_vout )
-    {
-        DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
-        vlc_object_detach( p_vout->p_sys->p_vout );
-        vout_Destroy( p_vout->p_sys->p_vout );
-    }
-
-    DEL_PARENT_CALLBACKS( SendEventsToChild );
-
     free( p_vout->p_sys );
 }
 
@@ -437,14 +429,17 @@ static void Destroy( vlc_object_t *p_this )
  *****************************************************************************/
 static int Manage( vout_thread_t *p_vout )
 {
-    video_format_t fmt = {0};
+    video_format_t fmt;
 
     if( !p_vout->p_sys->b_changed )
     {
         return VLC_SUCCESS;
     }
 
+    memset( &fmt, 0, sizeof(video_format_t) );
+
 #ifdef BEST_AUTOCROP
+    /* XXX: not thread-safe with FilterCallback */
     msg_Dbg( p_vout, "cropping at %ix%i+%i+%i, %sautocropping",
                      p_vout->p_sys->i_width, p_vout->p_sys->i_height,
                      p_vout->p_sys->i_x, p_vout->p_sys->i_y,
@@ -453,13 +448,16 @@ static int Manage( vout_thread_t *p_vout )
     msg_Info( p_vout, "ratio %d",  p_vout->p_sys->i_aspect / 432);
 #endif
 
-    vout_Destroy( p_vout->p_sys->p_vout );
+    if( p_vout->p_sys->p_vout )
+    {
+        vout_filter_DelChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
+        vout_CloseAndRelease( p_vout->p_sys->p_vout );
+    }
 
     fmt.i_width = fmt.i_visible_width = p_vout->p_sys->i_width;
     fmt.i_height = fmt.i_visible_height = p_vout->p_sys->i_height;
     fmt.i_x_offset = fmt.i_y_offset = 0;
     fmt.i_chroma = p_vout->render.i_chroma;
-    fmt.i_aspect = p_vout->p_sys->i_aspect;
     fmt.i_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
     fmt.i_sar_den = VOUT_ASPECT_FACTOR;
 
@@ -467,13 +465,16 @@ static int Manage( vout_thread_t *p_vout )
     if( p_vout->p_sys->p_vout == NULL )
     {
         msg_Err( p_vout, "failed to create vout" );
-        intf_UserFatal( p_vout, VLC_FALSE, _("Cropping failed"),
+        dialog_Fatal( p_vout, _("Cropping failed"), "%s",
                         _("VLC could not open the video output module.") );
         return VLC_EGENERIC;
     }
+    vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
 
-    p_vout->p_sys->b_changed = VLC_FALSE;
+    p_vout->p_sys->b_changed = false;
+    vlc_mutex_lock( &p_vout->p_sys->lock );
     p_vout->p_sys->i_lastchange = 0;
+    vlc_mutex_unlock( &p_vout->p_sys->lock );
 
     return VLC_SUCCESS;
 }
@@ -499,7 +500,7 @@ static void Render( vout_thread_t *p_vout, picture_t *p_pic )
                  vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
            ) == NULL )
     {
-        if( p_vout->b_die || p_vout->b_error )
+        if( !vlc_object_alive (p_vout) || p_vout->b_error )
         {
             vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
             return;
@@ -508,7 +509,7 @@ static void Render( vout_thread_t *p_vout, picture_t *p_pic )
         msleep( VOUT_OUTMEM_SLEEP );
     }
 
-    vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
+    p_outpic->date = p_pic->date;
     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
 
     for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
@@ -530,7 +531,7 @@ static void Render( vout_thread_t *p_vout, picture_t *p_pic )
 
         while( p_out < p_out_end )
         {
-            p_vout->p_libvlc->pf_memcpy( p_out, p_in, i_copy_pitch );
+            vlc_memcpy( p_out, p_in, i_copy_pitch );
             p_in += i_in_pitch;
             p_out += i_out_pitch;
         }
@@ -540,14 +541,14 @@ static void Render( vout_thread_t *p_vout, picture_t *p_pic )
     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
 
     /* The source image may still be in the cache ... parse it! */
+    vlc_mutex_lock( &p_vout->p_sys->lock );
     if( p_vout->p_sys->b_autocrop )
-    {
         UpdateStats( p_vout, p_pic );
-    }
+    vlc_mutex_unlock( &p_vout->p_sys->lock );
 }
 
 #ifdef BEST_AUTOCROP
-static vlc_bool_t NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
+static bool NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
                                int i_visible_pitch, int i_lines,
                                int i_lumThreshold, int i_skipCountPercent,
                                int i_nonBlackPixel, int i_chroma)
@@ -559,15 +560,13 @@ static vlc_bool_t NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
     switch(i_chroma)
     {
     // planar YUV
-        case VLC_FOURCC('I','4','4','4'):
-        case VLC_FOURCC('I','4','2','2'):
-        case VLC_FOURCC('I','4','2','0'):
-        case VLC_FOURCC('Y','V','1','2'):
-        case VLC_FOURCC('I','Y','U','V'):
-        case VLC_FOURCC('I','4','1','1'):
-        case VLC_FOURCC('I','4','1','0'):
-        case VLC_FOURCC('Y','V','U','9'):
-        case VLC_FOURCC('Y','U','V','A'):
+        case VLC_CODEC_I444:
+        case VLC_CODEC_I422:
+        case VLC_CODEC_I420:
+        case VLC_CODEC_YV12:
+        case VLC_CODEC_I411:
+        case VLC_CODEC_I410:
+        case VLC_CODEC_YUVA:
             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
             for (i_index = i_col/2 + i_skipCount/2;
                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
@@ -578,7 +577,7 @@ static vlc_bool_t NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
             }
             break;
     // packed RGB
-        case VLC_FOURCC('R','G','B','2'):    // packed by 1
+        case VLC_CODEC_RGB8:    // packed by 1
             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
             for (i_index = i_col/2 + i_skipCount/2;
                  i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2;
@@ -588,8 +587,8 @@ static vlc_bool_t NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
             if (i_count > i_nonBlackPixel) break;
             }
             break;
-        case VLC_FOURCC('R','V','1','5'):    // packed by 2
-        case VLC_FOURCC('R','V','1','6'):    // packed by 2
+        case VLC_CODEC_RGB15:    // packed by 2
+        case VLC_CODEC_RGB16:    // packed by 2
             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
             for (i_index = i_col/2 + i_skipCount/2 -
                                 (i_col/2 + i_skipCount/2) % 2;
@@ -601,7 +600,7 @@ static vlc_bool_t NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
             if (i_count > i_nonBlackPixel) break;
             }
             break;
-        case VLC_FOURCC('R','V','2','4'):    // packed by 3
+        case VLC_CODEC_RGB24:    // packed by 3
             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
             for (i_index = i_col/2 + i_skipCount/2 - (i_col/2 + i_skipCount/2) % 3; i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2; i_index+=3)
             {
@@ -611,7 +610,7 @@ static vlc_bool_t NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
             if (i_count > i_nonBlackPixel) break;
             }
             break;
-        case VLC_FOURCC('R','V','3','2'):    // packed by 4
+        case VLC_CODEC_RGB32:    // packed by 4
             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
             for (i_index = i_col/2 + i_skipCount/2 - (i_col/2 + i_skipCount/2) % 4; i_index <= i_visible_pitch/2 + i_col/2 - i_skipCount/2; i_index+=4)
             {
@@ -620,11 +619,8 @@ static vlc_bool_t NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
             }
             break;
     // packed YUV
-        case VLC_FOURCC('Y','U','Y','2'):    // packed by 2
-        case VLC_FOURCC('Y','U','N','V'):    // packed by 2
-        case VLC_FOURCC('U','Y','V','Y'):    // packed by 2
-        case VLC_FOURCC('U','Y','N','V'):    // packed by 2
-        case VLC_FOURCC('Y','4','2','2'):    // packed by 2
+        case VLC_CODEC_YUYV:    // packed by 2
+        case VLC_CODEC_UYVY:    // packed by 2
             i_skipCount = (i_pitch * i_skipCountPercent) / 100;
             for (i_index = (i_col/2 + i_skipCount/2) -
                            (i_col/2 + i_skipCount/2) % 2;
@@ -741,7 +737,7 @@ static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
     /* Determine where black borders are */
     switch( p_vout->output.i_chroma )
     {
-    case VLC_FOURCC('I','4','2','0'):
+    case VLC_CODEC_I420:
         /* XXX: Do not laugh ! I know this is very naive. But it's just a
          *      proof of concept code snippet... */
         for( i = i_lines ; i-- ; )
@@ -821,42 +817,26 @@ static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
                             * p_vout->output.i_height / p_vout->p_sys->i_height
                             * p_vout->p_sys->i_width / p_vout->output.i_width;
 
-    p_vout->p_sys->b_changed = VLC_TRUE;
+    p_vout->p_sys->b_changed = true;
 }
 
-/*****************************************************************************
- * SendEvents: forward mouse and keyboard events to the parent p_vout
- *****************************************************************************/
-static int SendEvents( vlc_object_t *p_this, char const *psz_var,
-                       vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
+/**
+ * Forward mouse event with proper conversion.
+ */
+static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
+                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
-    vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
-    vlc_value_t sentval = newval;
-
-    /* Translate the mouse coordinates */
-    if( !strcmp( psz_var, "mouse-x" ) )
-    {
-        sentval.i_int += p_vout->p_sys->i_x;
-    }
-    else if( !strcmp( psz_var, "mouse-y" ) )
-    {
-        sentval.i_int += p_vout->p_sys->i_y;
-    }
-
-    var_Set( p_vout, psz_var, sentval );
+    vout_thread_t *p_vout = p_data;
+    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
 
-    return VLC_SUCCESS;
-}
+    if( !strcmp( psz_var, "mouse-button-down" ) )
+        return var_SetChecked( p_vout, psz_var, VLC_VAR_INTEGER, newval );
 
-/*****************************************************************************
- * SendEventsToChild: forward events to the child/children vout
- *****************************************************************************/
-static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
-                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
-{
-    vout_thread_t *p_vout = (vout_thread_t *)p_this;
-    var_Set( p_vout->p_sys->p_vout, psz_var, newval );
-    return VLC_SUCCESS;
+    /* Translate the mouse coordinates
+     * FIXME missing lock */
+    newval.coords.x += p_vout->p_sys->i_x;
+    newval.coords.y += p_vout->p_sys->i_y;
+    return var_SetChecked( p_vout, psz_var, VLC_VAR_COORDS, newval );
 }
 
 #ifdef BEST_AUTOCROP
@@ -867,17 +847,19 @@ static int FilterCallback( vlc_object_t *p_this, char const *psz_var,
                            vlc_value_t oldval, vlc_value_t newval,
                            void *p_data )
 {
+    VLC_UNUSED(p_data); VLC_UNUSED(oldval);
     vout_thread_t * p_vout = (vout_thread_t *)p_this;
 
     if( !strcmp( psz_var, "ratio-crop" ) )
     {
+        vlc_mutex_lock( &p_vout->p_sys->lock );
         if ( !strcmp( newval.psz_string, "Auto" ) )
             p_vout->p_sys->i_ratio = 0;
         else
         {
             p_vout->p_sys->i_ratio = (unsigned int)atoi(newval.psz_string);
             p_vout->p_sys->i_lastchange = p_vout->p_sys->i_time;
-            p_vout->p_sys->b_autocrop = VLC_TRUE;
+            p_vout->p_sys->b_autocrop = true;
         }
         if (p_vout->p_sys->i_ratio)
         {
@@ -888,6 +870,7 @@ static int FilterCallback( vlc_object_t *p_this, char const *psz_var,
             if (p_vout->p_sys->i_ratio < p_vout->output.i_aspect / 432)
                 p_vout->p_sys->i_ratio = p_vout->output.i_aspect / 432;
         }
+        vlc_mutex_unlock( &p_vout->p_sys->lock );
      }
     return VLC_SUCCESS;
 }