[vlc-devel] [PATCH 3/7] Reactivated an improved version of the crop-module.

VlcVelope 1034-135 at online.de
Sun Apr 22 22:38:12 CEST 2012


The crop-module along is a mix of the crop-module with the croppadd-module.
Main issue is to watch letterbox videos with automatic cropping enabled.
Cropping is performed within the first (thirty seconds default) seconds of the
clip.
---
 modules/video_filter/Modules.am |    1 +
 modules/video_filter/crop.c     |  966 ++++++++++++++-------------------------
 2 files changed, 355 insertions(+), 612 deletions(-)

diff --git a/modules/video_filter/Modules.am b/modules/video_filter/Modules.am
index 47eaa13..3d3b02e 100644
--- a/modules/video_filter/Modules.am
+++ b/modules/video_filter/Modules.am
@@ -112,6 +112,7 @@ libvlc_LTLIBRARIES += \
 	libchain_plugin.la \
 	libclone_plugin.la \
 	libcolorthres_plugin.la \
+	libcrop_plugin.la \
 	libcroppadd_plugin.la \
 	libdeinterlace_plugin.la \
 	liberase_plugin.la \
diff --git a/modules/video_filter/crop.c b/modules/video_filter/crop.c
index b5e9460..0a8e119 100644
--- a/modules/video_filter/crop.c
+++ b/modules/video_filter/crop.c
@@ -33,41 +33,31 @@
 
 #include <vlc_common.h>
 #include <vlc_plugin.h>
-#include <vlc_vout.h>
-#include <vlc_dialog.h>
+#include <vlc_filter.h>
+#include <vlc_input.h>
+#include <vlc_playlist.h>
+#include <vlc_vout_wrapper.h>
+#include "filter_picture.h"
 
-#define BEST_AUTOCROP 1
-#ifdef BEST_AUTOCROP
-    #define RATIO_MAX 15000  // 10*4/3 for a 360
-#endif
+#define RATIO_MAX 15000  // 10*4/3 for a 360
 
-/*****************************************************************************
+/****************************************************************************
  * Local prototypes
- *****************************************************************************/
-static int  Create    ( vlc_object_t * );
-static void Destroy   ( vlc_object_t * );
+ ****************************************************************************/
+static int  OpenFilter ( vlc_object_t * );
+static void CloseFilter( vlc_object_t * );
 
-static int  Init      ( vout_thread_t * );
-static void End       ( vout_thread_t * );
-static int  Manage    ( vout_thread_t * );
-static void Render    ( vout_thread_t *, picture_t * );
+static picture_t *Filter( filter_t *, picture_t * );
 
-static void UpdateStats    ( vout_thread_t *, picture_t * );
+static void UpdateStats    ( filter_t *, picture_t * );
+
+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);
 
-static int  MouseEvent( vlc_object_t *, char const *,
-                        vlc_value_t, vlc_value_t, void * );
 
-#ifdef BEST_AUTOCROP
-/*****************************************************************************
- * Callback prototypes
- *****************************************************************************/
-static int FilterCallback ( vlc_object_t *, char const *,
-                            vlc_value_t, vlc_value_t, void * );
-#endif
 
-/*****************************************************************************
- * Module descriptor
- *****************************************************************************/
 #define GEOMETRY_TEXT N_("Crop geometry (pixels)")
 #define GEOMETRY_LONGTEXT N_("Set the geometry of the zone to crop. This is set as <width> x <height> + <left offset> + <top offset>.")
 
@@ -76,7 +66,6 @@ static int FilterCallback ( vlc_object_t *, char const *,
 
 #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.")
 
@@ -94,61 +83,71 @@ static int FilterCallback ( vlc_object_t *, char const *,
                         " that the line is black.")
 
 #define SKIP_TEXT N_("Skip percentage (%)")
-#define SKIP_LONGTEXT N_("Percentage of the line to consider while checking for black lines. This allows skipping logos in black borders and crop them anyway.")
+#define SKIP_LONGTEXT N_("Percentage of the line to consider while checking for black lines. This allows to skip logos in black borders and crop them anyway.")
 
 #define LUM_TEXT N_("Luminance threshold ")
 #define LUM_LONGTEXT N_("Maximum luminance to consider a pixel as black (0-255).")
-#endif
 
+#define MAXSECONDS_TEXT N_("up to second ")
+#define MAXSECONDS_LONGTEXT N_("Up to (n) seconds autocrop is active (0...).")
+
+#define SLOWCHANGE_TEXT N_("performe change slowly ")
+#define SLOWCHANGE_LONGTEXT N_("The change is done in steps of 2px until finished.")
+
+#define CFG_PREFIX "crop-"
+
+/*****************************************************************************
+ * Module descriptor
+ *****************************************************************************/
 vlc_module_begin ()
     set_description( N_("Crop video filter") )
     set_shortname( N_("Crop" ))
+    set_capability( "video filter2", 0 )
+    set_callbacks( OpenFilter, CloseFilter )
     set_help(CROP_HELP)
     set_category( CAT_VIDEO )
     set_subcategory( SUBCAT_VIDEO_VFILTER )
-    set_capability( "video filter", 0 )
 
-    add_string( "crop-geometry", NULL, GEOMETRY_TEXT,
+    add_string( CFG_PREFIX "crop-geometry", NULL, GEOMETRY_TEXT,
                                              GEOMETRY_LONGTEXT, false )
-    add_bool( "autocrop", false, AUTOCROP_TEXT,
+
+    set_section( N_("autocrop"), NULL )
+
+    add_bool( CFG_PREFIX "autocrop-enabled", false, AUTOCROP_TEXT,
                                    AUTOCROP_LONGTEXT, false )
 
-#ifdef BEST_AUTOCROP
-    add_integer_with_range( "autocrop-ratio-max", 2405, 0, RATIO_MAX,
+    add_integer_with_range( CFG_PREFIX "autocrop-ratio-max", 2405, 0, RATIO_MAX,
                             RATIOMAX_TEXT, RATIOMAX_LONGTEXT, true )
 
-    add_integer_with_range( "crop-ratio", 0, 0, RATIO_MAX, RATIO_TEXT,
+    add_integer_with_range( CFG_PREFIX "crop-ratio", 0, 0, RATIO_MAX, RATIO_TEXT,
                             RATIO_LONGTEXT, false )
-    add_integer( "autocrop-time", 25, TIME_TEXT,
+    add_integer( CFG_PREFIX "autocrop-time", 50, TIME_TEXT,
                  TIME_LONGTEXT, true )
-    add_integer( "autocrop-diff", 16, DIFF_TEXT,
+    add_integer( CFG_PREFIX "autocrop-diff", 8, DIFF_TEXT,
                                             DIFF_LONGTEXT, true )
 
-    add_integer( "autocrop-non-black-pixels", 3,
+    add_integer( CFG_PREFIX "autocrop-non-black-pixels", 3,
                  NBP_TEXT, NBP_LONGTEXT, true )
 
-    add_integer_with_range( "autocrop-skip-percent", 17, 0, 100,
+    add_integer_with_range( CFG_PREFIX "autocrop-skip-percent", 30, 0, 100,
                             SKIP_TEXT, SKIP_LONGTEXT, true )
 
-    add_integer_with_range( "autocrop-luminance-threshold", 40, 0, 128,
+    add_integer_with_range( CFG_PREFIX "autocrop-luminance-threshold", 30, 0, 128,
                             LUM_TEXT, LUM_LONGTEXT, true )
-#endif //BEST_AUTOCROP
+    add_integer_with_range( CFG_PREFIX "autocrop-seconds", 30, 0, 65536, 
+                            MAXSECONDS_TEXT, MAXSECONDS_LONGTEXT, false )
+    add_bool( CFG_PREFIX "autocrop-slow-change", true, SLOWCHANGE_TEXT,
+                            SLOWCHANGE_LONGTEXT, false )
 
-    add_shortcut( "crop" )
-    set_callbacks( Create, Destroy )
 vlc_module_end ()
 
-/*****************************************************************************
- * vout_sys_t: Crop video output method descriptor
- *****************************************************************************
- * This structure is part of the video output thread descriptor.
- * It describes the Crop specific properties of an output thread.
- *****************************************************************************/
-struct vout_sys_t
-{
-    vlc_mutex_t lock;
-    vout_thread_t *p_vout;
+static const char *const ppsz_filter_options[] = {
+    "autocrop-enabled", "autocrop-ratio-max", "crop-ratio", "autocrop-time", "autocrop-diff", "autocrop-non-black-pixels", "autocrop-skip-percent", "autocrop-luminance-threshold", "autocrop-seconds", "autocrop-slow-change",
+    NULL
+};
 
+struct filter_sys_t
+{
     unsigned int i_x, i_y;
     unsigned int i_width, i_height, i_aspect;
 
@@ -156,398 +155,379 @@ struct vout_sys_t
 
     /* Autocrop specific variables */
     unsigned int i_lastchange;
-    bool   b_changed;
-#ifdef BEST_AUTOCROP
+    bool         b_changed;
+
     unsigned int i_ratio_max;
     unsigned int i_threshold, i_skipPercent, i_nonBlackPixel, i_diff, i_time;
     unsigned int i_ratio;
-#endif
-
+    unsigned int i_frames;
+    unsigned int i_maxseconds;
+    bool         b_slowchange;
+    unsigned int i_restore_crop_top, i_restore_crop_bottom;
 };
 
 /*****************************************************************************
- * Control: control facility for the vout (forwards to child vout)
- *****************************************************************************/
-static int Control( vout_thread_t *p_vout, int i_query, va_list args )
-{
-    return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
-}
-
-/*****************************************************************************
- * Create: allocates Crop video thread output method
- *****************************************************************************
- * This function allocates and initializes a Crop vout method.
+ * OpenFilter: probe the filter and return score
  *****************************************************************************/
-static int Create( vlc_object_t *p_this )
+static int OpenFilter( vlc_object_t *p_this )
 {
-    vout_thread_t *p_vout = (vout_thread_t *)p_this;
+    filter_t *p_filter = (filter_t*)p_this;
+    filter_sys_t *p_sys;
 
-    /* Allocate structure */
-    p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
-    if( p_vout->p_sys == NULL )
-        return VLC_ENOMEM;
-
-    p_vout->pf_init = Init;
-    p_vout->pf_end = End;
-    p_vout->pf_manage = Manage;
-    p_vout->pf_render = Render;
-    p_vout->pf_display = NULL;
-    p_vout->pf_control = Control;
-
-    return VLC_SUCCESS;
-}
+    if( !p_filter->b_allow_fmt_out_change )
+    {
+        msg_Err( p_filter, "Picture format change isn't allowed" );
+        return VLC_EGENERIC;
+    }
 
-/*****************************************************************************
- * Init: initialize Crop video thread output method
- *****************************************************************************/
-static int Init( vout_thread_t *p_vout )
-{
-    char *psz_var;
-    video_format_t fmt;
+    if( p_filter->fmt_in.video.i_chroma != p_filter->fmt_out.video.i_chroma )
+    {
+        msg_Err( p_filter, "Input and output chromas don't match" );
+        /* In fact we don't really care about this since we're allowed
+         * to change the output format ... FIXME? */
+        return VLC_EGENERIC;
+    }
 
-    I_OUTPUTPICTURES = 0;
-    memset( &fmt, 0, sizeof(video_format_t) );
+    p_filter->p_sys = (filter_sys_t *)malloc( sizeof( filter_sys_t ) );
+    if( !p_filter->p_sys ) return VLC_ENOMEM;
 
-    p_vout->p_sys->i_lastchange = 0;
-    p_vout->p_sys->b_changed = false;
+    config_ChainParse( p_filter, CFG_PREFIX, ppsz_filter_options,
+                       p_filter->p_cfg );
 
-    /* Initialize the output structure */
-    p_vout->output.i_chroma = p_vout->render.i_chroma;
-    p_vout->output.i_width  = p_vout->render.i_width;
-    p_vout->output.i_height = p_vout->render.i_height;
-    p_vout->output.i_aspect = p_vout->render.i_aspect;
-    p_vout->fmt_out = p_vout->fmt_in;
+    p_sys = p_filter->p_sys;
 
     /* Shall we use autocrop ? */
-    p_vout->p_sys->b_autocrop = var_InheritBool( p_vout, "autocrop" );
-#ifdef BEST_AUTOCROP
-    p_vout->p_sys->i_ratio_max =
-        var_InheritInteger( p_vout, "autocrop-ratio-max" );
-    p_vout->p_sys->i_threshold =
-        var_InheritInteger( p_vout, "autocrop-luminance-threshold" );
-    p_vout->p_sys->i_skipPercent =
-        var_InheritInteger( p_vout, "autocrop-skip-percent" );
-    p_vout->p_sys->i_nonBlackPixel =
-        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;
+    p_sys->b_autocrop =
+        var_CreateGetBool( p_filter, CFG_PREFIX "autocrop-enabled" );
+    p_sys->i_ratio_max =
+        var_CreateGetInteger( p_filter, CFG_PREFIX "autocrop-ratio-max" );
+    p_sys->i_threshold =
+        var_CreateGetInteger( p_filter, CFG_PREFIX "autocrop-luminance-threshold" );
+    p_sys->i_skipPercent =
+        var_CreateGetInteger( p_filter, CFG_PREFIX "autocrop-skip-percent" );
+    p_sys->i_nonBlackPixel =
+        var_CreateGetInteger( p_filter, CFG_PREFIX "autocrop-non-black-pixels" );
+    p_sys->i_diff =
+        var_CreateGetInteger( p_filter, CFG_PREFIX "autocrop-diff" );
+    p_sys->i_time =
+        var_CreateGetInteger( p_filter, CFG_PREFIX "autocrop-time" );
+    p_sys->b_slowchange =
+        var_CreateGetBool( p_filter, CFG_PREFIX "autocrop-slow-change" );
+
+    if (p_sys->b_autocrop)
+        p_sys->i_ratio = 0;
     else
     {
-        p_vout->p_sys->i_ratio = var_InheritInteger( p_vout, "crop-ratio" );
+        p_sys->i_ratio = var_CreateGetInteger( p_filter, CFG_PREFIX "autocrop-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;
+        if (p_sys->i_ratio < (p_filter->fmt_in.video.i_width * 1000) / p_filter->fmt_in.video.i_height)
+            p_sys->i_ratio = 0;
     }
-#endif
 
+    p_sys->i_maxseconds = var_CreateGetInteger( p_filter, CFG_PREFIX "autocrop-seconds" );
+    p_sys->i_frames = 0;
 
-    /* Get geometry value from the user */
-    psz_var = var_InheritString( p_vout, "crop-geometry" );
-    if( psz_var )
-    {
-        char *psz_parser, *psz_tmp;
-
-        psz_parser = psz_tmp = psz_var;
-        while( *psz_tmp && *psz_tmp != 'x' ) psz_tmp++;
+    p_sys->i_restore_crop_top = var_InheritInteger( p_this, "crop-top" );
+    p_sys->i_restore_crop_bottom = var_InheritInteger( p_this, "crop-bottom" );
 
-        if( *psz_tmp )
-        {
-            psz_tmp[0] = '\0';
-            p_vout->p_sys->i_width = atoi( psz_parser );
 
-            psz_parser = ++psz_tmp;
-            while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
+    p_filter->fmt_out.video.i_height =
+    p_filter->fmt_out.video.i_visible_height =
+        p_filter->fmt_in.video.i_visible_height;
 
-            if( *psz_tmp )
-            {
-                psz_tmp[0] = '\0';
-                p_vout->p_sys->i_height = atoi( psz_parser );
+    p_filter->fmt_out.video.i_width =
+    p_filter->fmt_out.video.i_visible_width =
+        p_filter->fmt_in.video.i_visible_width;
 
-                psz_parser = ++psz_tmp;
-                while( *psz_tmp && *psz_tmp != '+' ) psz_tmp++;
+    p_filter->pf_video_filter = Filter;
 
-                if( *psz_tmp )
-                {
-                    psz_tmp[0] = '\0';
-                    p_vout->p_sys->i_x = atoi( psz_parser );
-                    p_vout->p_sys->i_y = atoi( ++psz_tmp );
-                }
-                else
-                {
-                    p_vout->p_sys->i_x = atoi( psz_parser );
-                    p_vout->p_sys->i_y =
-                     ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
-                }
-            }
-            else
-            {
-                p_vout->p_sys->i_height = atoi( psz_parser );
-                p_vout->p_sys->i_x =
-                     ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
-                p_vout->p_sys->i_y =
-                     ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
-            }
-        }
-        else
-        {
-            p_vout->p_sys->i_width = atoi( psz_parser );
-            p_vout->p_sys->i_height = p_vout->output.i_height;
-            p_vout->p_sys->i_x =
-                     ( p_vout->output.i_width - p_vout->p_sys->i_width ) / 2;
-            p_vout->p_sys->i_y =
-                     ( p_vout->output.i_height - p_vout->p_sys->i_height ) / 2;
-        }
+    p_sys->i_x = p_sys->i_y = 0;
+    p_sys->i_width = p_filter->fmt_in.video.i_width;
+    p_sys->i_height = p_filter->fmt_in.video.i_width;
+    p_sys->i_aspect = (int64_t)VOUT_ASPECT_FACTOR *
+        p_filter->fmt_in.video.i_sar_num * p_filter->fmt_in.video.i_width /
+        (p_filter->fmt_in.video.i_sar_den * p_filter->fmt_in.video.i_height);
 
-        /* Check for validity */
-        if( p_vout->p_sys->i_x + p_vout->p_sys->i_width
-                                                   > p_vout->output.i_width )
-        {
-            p_vout->p_sys->i_x = 0;
-            if( p_vout->p_sys->i_width > p_vout->output.i_width )
-            {
-                p_vout->p_sys->i_width = p_vout->output.i_width;
-            }
-        }
-
-        if( p_vout->p_sys->i_y + p_vout->p_sys->i_height
-                                                   > p_vout->output.i_height )
-        {
-            p_vout->p_sys->i_y = 0;
-            if( p_vout->p_sys->i_height > p_vout->output.i_height )
-            {
-                p_vout->p_sys->i_height = p_vout->output.i_height;
-            }
-        }
-
-        free( psz_var );
-    }
-    else
-#ifdef BEST_AUTOCROP
-    if (p_vout->p_sys->i_ratio)
-    {
-        p_vout->p_sys->i_aspect    =  p_vout->p_sys->i_ratio * 432;
-        p_vout->p_sys->i_width  = p_vout->fmt_out.i_visible_width;
-        p_vout->p_sys->i_height = p_vout->output.i_aspect
-                                * p_vout->output.i_height / p_vout->p_sys->i_aspect
-                                * p_vout->p_sys->i_width / p_vout->output.i_width;
-        p_vout->p_sys->i_height += p_vout->p_sys->i_height % 2;
-        p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
-        p_vout->p_sys->i_y = (p_vout->output.i_height - p_vout->p_sys->i_height) / 2;
-    }
-    else
-#endif
-    {
-        p_vout->p_sys->i_width  = p_vout->fmt_out.i_visible_width;
-        p_vout->p_sys->i_height = p_vout->fmt_out.i_visible_height;
-        p_vout->p_sys->i_x = p_vout->fmt_out.i_x_offset;
-        p_vout->p_sys->i_y = p_vout->fmt_out.i_y_offset;
-    }
-
-    /* Pheeew. Parsing done. */
-    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,
-                     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
-    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_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" );
-        dialog_Fatal( p_vout, _("Cropping failed"), "%s",
-                        _("VLC could not open the video output module.") );
-        return VLC_EGENERIC;
-    }
-
-    vlc_mutex_init( &p_vout->p_sys->lock );
-#ifdef BEST_AUTOCROP
-    var_AddCallback( p_vout, "ratio-crop", FilterCallback, NULL );
-#endif
-
-    vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
-
-    vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
+    p_sys->i_lastchange = 0;
+    p_sys->b_changed = 0;
 
     return VLC_SUCCESS;
 }
 
 /*****************************************************************************
- * End: terminate Crop video thread output method
+ * CloseFilter: clean up the filter
  *****************************************************************************/
-static void End( vout_thread_t *p_vout )
+static void CloseFilter( vlc_object_t *p_this )
 {
-    vout_sys_t *p_sys = p_vout->p_sys;
-
-    if( p_sys->p_vout )
-    {
-        vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
-        vout_CloseAndRelease( p_sys->p_vout );
-    }
+    filter_t *p_filter = (filter_t *)p_this;
+    /* the input is dying / has died => restore crop... */
+    var_SetInteger( (vlc_object_t*)p_filter->p_owner, "crop-top", p_filter->p_sys->i_restore_crop_top );
+    var_SetInteger( (vlc_object_t*)p_filter->p_owner, "crop-bottom", p_filter->p_sys->i_restore_crop_bottom );
 
-    vout_filter_ReleaseDirectBuffers( p_vout );
-    var_DelCallback( p_vout, "ratio-crop", FilterCallback, NULL );
-    vlc_mutex_destroy( &p_sys->lock );
+    free( p_filter->p_sys );
 }
 
-/*****************************************************************************
- * Destroy: destroy Crop video thread output method
- *****************************************************************************
- * Terminate an output method created by CropCreateOutputMethod
- *****************************************************************************/
-static void Destroy( vlc_object_t *p_this )
+/****************************************************************************
+ * Filter: the whole thing
+ ****************************************************************************/
+static picture_t *Filter( filter_t *p_filter, picture_t *p_pic )
 {
-    vout_thread_t *p_vout = (vout_thread_t *)p_this;
+    filter_sys_t *p_sys = p_filter->p_sys;
+    picture_t *p_outpic;
+    int i_plane;
+    int i_width, i_height,
+        i_outwidth, i_outheight;
 
-    free( p_vout->p_sys );
-}
+    if( !p_pic ) return NULL;
 
-/*****************************************************************************
- * Manage: handle Crop events
- *****************************************************************************
- * This function should be called regularly by video output thread. It manages
- * console events. It returns a non null value on error.
- *****************************************************************************/
-static int Manage( vout_thread_t *p_vout )
-{
-    video_format_t fmt;
+    if( p_sys->b_autocrop )
+        UpdateStats( p_filter, p_pic );
 
-    if( !p_vout->p_sys->b_changed )
+    if( p_sys->b_changed )
     {
-        return VLC_SUCCESS;
+        msg_Dbg( p_filter, "cropping at %ix%i+%i+%i, %sautocropping p_sys->i_frames=%d",
+                         p_sys->i_width, p_sys->i_height,
+                         p_sys->i_x, p_sys->i_y,
+                         p_sys->b_autocrop ? "" : "not ", p_sys->i_frames );
+
+        msg_Info( p_filter, "ratio %d (raw:%d) num %d den %d",  p_sys->i_aspect / 432,  p_sys->i_aspect,
+            p_filter->fmt_out.video.i_sar_num,
+            p_filter->fmt_out.video.i_sar_den );
+		p_sys->i_lastchange = 0;
+
+        int top = var_GetInteger( (vlc_object_t*)p_filter->p_owner, "crop-top" );
+        int bottom = var_GetInteger( (vlc_object_t*)p_filter->p_owner, "crop-bottom" );
+        int direction = p_sys->i_y-top;
+        if( p_sys->b_slowchange && direction )  top += direction / abs( direction ) * 2;
+        else                                    top += direction;
+        if( abs( top-p_sys->i_y ) < 2 )	        top = p_sys->i_y;
+        int newbottom=p_filter->fmt_in.video.i_height-(p_sys->i_height+p_sys->i_y);
+        direction=newbottom-bottom;
+        if( p_sys->b_slowchange && direction )  bottom += direction / abs( direction ) * 2;
+        else                                    bottom += direction;
+        if( abs( bottom - newbottom ) < 2 )     bottom = newbottom;
+        if( top == p_sys->i_y && bottom == newbottom )
+            p_sys->b_changed = false;
+        /* apply new crop */
+        var_SetInteger( (vlc_object_t*)p_filter->p_owner, "crop-top", top );
+        var_SetInteger( (vlc_object_t*)p_filter->p_owner, "crop-bottom", bottom );
+	}
+
+    p_filter->fmt_out.video.i_height =
+    p_filter->fmt_out.video.i_visible_height =
+        p_filter->fmt_in.video.i_visible_height;
+
+    p_filter->fmt_out.video.i_width =
+    p_filter->fmt_out.video.i_visible_width =
+        p_filter->fmt_in.video.i_visible_width;
+
+    /* Request output picture */
+    p_outpic = filter_NewPicture( p_filter );
+    if( !p_outpic )
+    {
+        picture_Release( p_pic );
+        return NULL;
     }
 
-    memset( &fmt, 0, sizeof(video_format_t) );
+    for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
+    /* p_pic and p_outpic have the same chroma/number of planes but that's
+     * about it. */
+    {
+        plane_t *p_plane = p_pic->p+i_plane;
+        plane_t *p_outplane = p_outpic->p+i_plane;
+        uint8_t *p_in = p_plane->p_pixels;
+        uint8_t *p_out = p_outplane->p_pixels;
+        int i_pixel_pitch = p_plane->i_pixel_pitch;
+
+        /* These assignments assume that the first plane always has
+         * a width and height equal to the picture's */
+        i_width =     ( ( p_filter->fmt_in.video.i_visible_width
+                           )
+                        * p_plane->i_visible_pitch )
+                      / p_pic->p->i_visible_pitch;
+        i_height =    ( ( p_filter->fmt_in.video.i_visible_height
+                           )
+                        * p_plane->i_visible_lines )
+                      / p_pic->p->i_visible_lines;
+        i_outwidth =  ( p_filter->fmt_out.video.i_visible_width
+                        * p_outplane->i_visible_pitch )
+                      / p_outpic->p->i_visible_pitch;
+        i_outheight = ( p_filter->fmt_out.video.i_visible_height
+                        * p_outplane->i_visible_lines )
+                      / p_outpic->p->i_visible_lines;
+
+        int i_line;
+        for( i_line = 0; i_line < i_height; i_line++ )
+        {
+            uint8_t *p_in_next = p_in + p_plane->i_pitch;
+            uint8_t *p_out_next = p_out + p_outplane->i_pitch;
 
-#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,
-                     p_vout->p_sys->b_autocrop ? "" : "not " );
+            /* Copy the image */
+            vlc_memcpy( p_out, p_in, i_width * i_pixel_pitch );
+            p_out += i_width * i_pixel_pitch;
+            p_in += i_width * i_pixel_pitch;
 
-    msg_Info( p_vout, "ratio %d",  p_vout->p_sys->i_aspect / 432);
-#endif
+            /* Got to begining of the next line */
+            p_in = p_in_next;
+            p_out = p_out_next;
+        }
 
-    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_sar_num = p_vout->p_sys->i_aspect * fmt.i_height / fmt.i_width;
-    fmt.i_sar_den = VOUT_ASPECT_FACTOR;
+    return CopyInfoAndRelease( p_outpic, p_pic );
+}
 
-    p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
-    if( p_vout->p_sys->p_vout == NULL )
+/* to save processing time frame analysis can be skipped */
+#define every_x_frame 5
+
+static void UpdateStats( filter_t *p_filter, picture_t *p_pic )
+{
+    filter_sys_t *p_sys = p_filter->p_sys;
+    p_sys->i_frames ++;
+    if( p_sys->i_frames % every_x_frame )
+        return;
+    double fps = p_filter->fmt_in.video.i_frame_rate;
+    if( p_filter->fmt_in.video.i_frame_rate_base > 0 )
+        fps = fps / p_filter->fmt_in.video.i_frame_rate_base;
+    if( fps < 1e-3 )
+        fps = 1;
+    /* as first guess use the frame counter */
+    double seconds = p_sys->i_frames / fps;
+    if( p_sys->i_maxseconds > 0 )
     {
-        msg_Err( p_vout, "failed to create vout" );
-        dialog_Fatal( p_vout, _("Cropping failed"), "%s",
-                        _("VLC could not open the video output module.") );
-        return VLC_EGENERIC;
+        /* get the p_input pointer (to access video times) */
+        input_thread_t* input = pl_CurrentInput( p_filter );
+        if( input )
+        {
+            int64_t time = var_GetTime( input, "time" );
+            int64_t len = var_GetTime( input, "length" );
+            /* seems to be valid! */
+            if( time > 0 && len > 0 )
+            {
+                seconds = (double) time / 1000000.;
+                /* well, reset framecounter during the first half of the autocrop time gate */
+                if( seconds <= p_sys->i_maxseconds / 2. )
+                    p_sys->i_frames = 0;
+                /* But if the viewer skipped right into the video, then 'seconds' will be large
+                   and would inhibit the autocrop.
+                   Then the (small!) framecounter will be used to check if the clip is playing just shortly. */
+                if( p_sys->i_frames * 2 < p_sys->i_maxseconds * fps)
+                    seconds = 0;
+            }
+            vlc_object_release( input );
+        }
     }
-    vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
 
-    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 );
+    if( p_sys->i_maxseconds > 0 && p_sys->i_frames > ( p_sys->i_maxseconds * fps ) )
+        return;
 
-    return VLC_SUCCESS;
-}
+    uint8_t *p_in = p_pic->p[0].p_pixels;
+    int i_pitch = p_pic->p[0].i_pitch;
+    int i_visible_pitch = p_pic->p[0].i_visible_pitch;
+    int i_lines = p_pic->p[0].i_visible_lines;
+    int i_firstwhite = -1, i_lastwhite = -1, i;
 
-/*****************************************************************************
- * Render: display previously rendered output
- *****************************************************************************
- * This function sends the currently rendered image to Crop image, waits
- * until it is displayed and switches the two rendering buffers, preparing next
- * frame.
- *****************************************************************************/
-static void Render( vout_thread_t *p_vout, picture_t *p_pic )
-{
-    picture_t *p_outpic = NULL;
-    int i_plane;
+    int i_time = p_sys->i_time;
+    int i_diff = p_sys->i_diff;
 
-    if( p_vout->p_sys->b_changed )
+    if ( !p_sys->i_ratio )
     {
-        return;
-    }
+        /* Determine where black borders are (but only 200px from top/bottom*/
+        int max = i_lines / 2;
+        if( max > 200 )
+            max = 200;
+        for( i = 0 ; i < max ; i += 2 )
+        {
+            if (NonBlackLine(p_in, i, i_pitch, i_visible_pitch, i_lines,
+                             p_sys->i_threshold,
+                             p_sys->i_skipPercent,
+                             p_sys->i_nonBlackPixel,
+                             p_pic->format.i_chroma)
+						||
+                NonBlackLine(p_in, i_lines-i, i_pitch, i_visible_pitch, i_lines,
+                             p_sys->i_threshold,
+                             p_sys->i_skipPercent,
+                             p_sys->i_nonBlackPixel,
+                             p_pic->format.i_chroma))
+                {
+                    i_firstwhite = i;
+                    i_lastwhite = i_lines - i;
+                    break;
+                }
+                p_in += 2 * i_pitch;
+        }
 
-    while( ( p_outpic =
-                 vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 )
-           ) == NULL )
-    {
-        if( !vlc_object_alive (p_vout) || p_vout->b_error )
+        /* Decide whether it's worth changing the size */
+        if( i_lastwhite == -1 )
         {
-            vout_DestroyPicture( p_vout->p_sys->p_vout, p_outpic );
+            p_sys->i_lastchange = 0;
             return;
         }
 
-        msleep( VOUT_OUTMEM_SLEEP );
-    }
+        if( (i_lastwhite - i_firstwhite) < (int) (p_sys->i_height / 2) )
+        {
+            p_sys->i_lastchange = 0;
+            return;
+        }
 
-    p_outpic->date = p_pic->date;
-    vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
+        if( (i_lastwhite - i_firstwhite) <
+                        (int) (p_sys->i_height + i_diff)
+             && (i_lastwhite - i_firstwhite + i_diff) >
+                        (int) p_sys->i_height )
+        {
+            p_sys->i_lastchange = 0;
+            return;
+        }
 
-    for( i_plane = 0 ; i_plane < p_pic->i_planes ; i_plane++ )
+        /* We need at least 'i_time' images to make up our mind */
+        p_sys->i_lastchange+=every_x_frame;
+        if( p_sys->i_lastchange < (unsigned int)i_time )
+        {
+            return;
+        }
+    }
+    else
     {
-        uint8_t *p_in, *p_out, *p_out_end;
-        int i_in_pitch = p_pic->p[i_plane].i_pitch;
-        const int i_out_pitch = p_outpic->p[i_plane].i_pitch;
-        const int i_copy_pitch = p_outpic->p[i_plane].i_visible_pitch;
-
-        p_in = p_pic->p[i_plane].p_pixels
-                /* Skip the right amount of lines */
-                + i_in_pitch * ( p_pic->p[i_plane].i_visible_lines *
-                                 p_vout->p_sys->i_y / p_vout->output.i_height )
-                /* Skip the right amount of columns */
-                + i_in_pitch * p_vout->p_sys->i_x / p_vout->output.i_width;
-
-        p_out = p_outpic->p[i_plane].p_pixels;
-        p_out_end = p_out + i_out_pitch * p_outpic->p[i_plane].i_visible_lines;
-
-        while( p_out < p_out_end )
+        if ( p_sys->i_lastchange >= (unsigned int)i_time )
         {
-            vlc_memcpy( p_out, p_in, i_copy_pitch );
-            p_in += i_in_pitch;
-            p_out += i_out_pitch;
+            p_sys->i_aspect    =  p_sys->i_ratio * 432;
+            int i_height =  p_pic->format.i_height
+                                    * p_sys->i_width /
+                                        p_pic->format.i_width;
+            i_firstwhite = (p_pic->format.i_height - i_height) / 2;
+            i_lastwhite =  p_pic->format.i_height - i_firstwhite;
+        }
+        else
+        {
+            return;
         }
     }
 
-    vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
-    vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
+    /* Tune a few values */
+    if( i_firstwhite & 1 )
+    {
+        i_firstwhite--;
+    }
+
+    if( !(i_lastwhite & 1) )
+    {
+        i_lastwhite++;
+    }
 
-    /* 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 );
+    /* Change size */
+    p_sys->i_y = i_firstwhite;
+    p_sys->i_height = i_lastwhite - i_firstwhite + 1;
+
+    /* check p_sys->i_height <= p_pic->format.i_height */
+    if (p_sys->i_height > p_pic->format.i_height)
+        p_sys->i_height = p_pic->format.i_height;
+
+    p_sys->i_aspect = p_pic->format.i_height / p_sys->i_height
+                            * p_sys->i_width / p_pic->format.i_width;
+
+    p_sys->b_changed = true;
 }
 
-#ifdef BEST_AUTOCROP
 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,
@@ -636,242 +616,4 @@ static bool NonBlackLine(uint8_t *p_in, int i_line, int i_pitch,
     }
     return (i_count > i_nonBlackPixel);
 }
-#endif
 
-static void UpdateStats( vout_thread_t *p_vout, picture_t *p_pic )
-{
-   uint8_t *p_in = p_pic->p[0].p_pixels;
-    int i_pitch = p_pic->p[0].i_pitch;
-    int i_visible_pitch = p_pic->p[0].i_visible_pitch;
-    int i_lines = p_pic->p[0].i_visible_lines;
-    int i_firstwhite = -1, i_lastwhite = -1, i;
-#ifdef BEST_AUTOCROP
-    int i_time = p_vout->p_sys->i_time;
-    int i_diff = p_vout->p_sys->i_diff;
-
-    if (!p_vout->p_sys->i_ratio)
-    {
-        /* Determine where black borders are */
-        for( i = 0 ; i < i_lines ; i++)
-        {
-                   if (NonBlackLine(p_in, i, i_pitch, i_visible_pitch, i_lines,
-                            p_vout->p_sys->i_threshold,
-                            p_vout->p_sys->i_skipPercent,
-                            p_vout->p_sys->i_nonBlackPixel,
-                            p_vout->output.i_chroma))
-                {
-                    i_firstwhite = i;
-                    i_lastwhite = i_lines - i;
-                    break;
-                }
-                p_in += i_pitch;
-        }
-
-        /* Decide whether it's worth changing the size */
-        if( i_lastwhite == -1 )
-        {
-            p_vout->p_sys->i_lastchange = 0;
-            return;
-        }
-
-        if( (i_lastwhite - i_firstwhite) < (int) (p_vout->p_sys->i_height / 2) )
-        {
-            p_vout->p_sys->i_lastchange = 0;
-            return;
-        }
-
-        if (p_vout->output.i_aspect
-                            * p_vout->output.i_height /
-                                (i_lastwhite - i_firstwhite + 1)
-                            * p_vout->p_sys->i_width /
-                               p_vout->output.i_width >
-                                    p_vout->p_sys->i_ratio_max * 432)
-        {
-            int i_height = ((p_vout->output.i_aspect / 432) *
-                           p_vout->output.i_height * p_vout->p_sys->i_width) /
-                          (p_vout->output.i_width * p_vout->p_sys->i_ratio_max);
-            i_firstwhite = (p_vout->output.i_height - i_height) / 2;
-            i_lastwhite =  p_vout->output.i_height - i_firstwhite;
-/*
-            p_vout->p_sys->i_lastchange = 0;
-            return;
-*/
-        }
-
-        if( (i_lastwhite - i_firstwhite) <
-                        (int) (p_vout->p_sys->i_height + i_diff)
-             && (i_lastwhite - i_firstwhite + i_diff) >
-                        (int) p_vout->p_sys->i_height )
-        {
-            p_vout->p_sys->i_lastchange = 0;
-            return;
-        }
-
-        /* We need at least 'i_time' images to make up our mind */
-        p_vout->p_sys->i_lastchange++;
-        if( p_vout->p_sys->i_lastchange < (unsigned int)i_time )
-        {
-            return;
-        }
-    }
-    else
-    {
-        if ( p_vout->p_sys->i_lastchange >= (unsigned int)i_time )
-        {
-            p_vout->p_sys->i_aspect    =  p_vout->p_sys->i_ratio * 432;
-            int i_height = p_vout->output.i_aspect
-                                    * p_vout->output.i_height /
-                                        p_vout->p_sys->i_aspect
-                                    * p_vout->p_sys->i_width /
-                                        p_vout->output.i_width;
-            i_firstwhite = (p_vout->output.i_height - i_height) / 2;
-            i_lastwhite =  p_vout->output.i_height - i_firstwhite;
-        }
-        else
-        {
-            return;
-        }
-    }
-
-#else
-    /* Determine where black borders are */
-    switch( p_vout->output.i_chroma )
-    {
-    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-- ; )
-        {
-            const int i_col = i * i_pitch / i_lines;
-
-            if( p_in[i_col/2] > 40
-                 && p_in[i_visible_pitch/2] > 40
-                 && p_in[i_visible_pitch/2 + i_col/2] > 40 )
-            {
-                if( i_lastwhite == -1 )
-                {
-                    i_lastwhite = i;
-                }
-                i_firstwhite = i;
-            }
-            p_in += i_pitch;
-        }
-        break;
-
-    default:
-        break;
-    }
-
-    /* Decide whether it's worth changing the size */
-    if( i_lastwhite == -1 )
-    {
-        p_vout->p_sys->i_lastchange = 0;
-        return;
-    }
-
-    if( (unsigned int)(i_lastwhite - i_firstwhite)
-                                           < p_vout->p_sys->i_height / 2 )
-    {
-        p_vout->p_sys->i_lastchange = 0;
-        return;
-    }
-
-    if( (unsigned int)(i_lastwhite - i_firstwhite)
-                                          < p_vout->p_sys->i_height + 16
-         && (unsigned int)(i_lastwhite - i_firstwhite + 16)
-                                                > p_vout->p_sys->i_height )
-    {
-        p_vout->p_sys->i_lastchange = 0;
-        return;
-    }
-
-    /* We need at least 25 images to make up our mind */
-    p_vout->p_sys->i_lastchange++;
-    if( p_vout->p_sys->i_lastchange < 25 )
-    {
-        return;
-    }
-#endif //BEST_AUTOCROP
-
-    /* Tune a few values */
-    if( i_firstwhite & 1 )
-    {
-        i_firstwhite--;
-    }
-
-    if( !(i_lastwhite & 1) )
-    {
-        i_lastwhite++;
-    }
-
-    /* Change size */
-    p_vout->p_sys->i_y = i_firstwhite;
-    p_vout->p_sys->i_height = i_lastwhite - i_firstwhite + 1;
-#ifdef BEST_AUTOCROP
-    // check p_vout->p_sys->i_height <= p_vout->output.i_height
-    if (p_vout->p_sys->i_height > p_vout->output.i_height)
-        p_vout->p_sys->i_height = p_vout->output.i_height;
-#endif
-
-    p_vout->p_sys->i_aspect = p_vout->output.i_aspect
-                            * 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 = true;
-}
-
-/**
- * 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 = p_data;
-    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
-
-    if( !strcmp( psz_var, "mouse-button-down" ) )
-        return var_SetChecked( p_vout, psz_var, VLC_VAR_INTEGER, newval );
-
-    /* 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
-/*****************************************************************************
- * FilterCallback: called when changing the ratio on the fly.
- *****************************************************************************/
-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 = true;
-        }
-        if (p_vout->p_sys->i_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 = (p_vout->output.i_width * 1000) /
-                                    p_vout->output.i_height;
-            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;
-}
-#endif
-- 
1.7.5.4




More information about the vlc-devel mailing list